1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2008 Oracle. All rights reserved.
9 * Lots of code in this file is taken from ext3.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
27 #include <linux/capability.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/highmem.h>
32 #include <linux/pagemap.h>
33 #include <linux/uio.h>
34 #include <linux/sched.h>
35 #include <linux/splice.h>
36 #include <linux/mount.h>
37 #include <linux/writeback.h>
38 #include <linux/falloc.h>
39 #include <linux/sort.h>
40 #include <linux/init.h>
41 #include <linux/module.h>
42 #include <linux/string.h>
44 #define MLOG_MASK_PREFIX ML_XATTR
45 #include <cluster/masklog.h>
58 #include "buffer_head_io.h"
63 struct ocfs2_xattr_def_value_root {
64 struct ocfs2_xattr_value_root xv;
65 struct ocfs2_extent_rec er;
68 struct ocfs2_xattr_bucket {
69 struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
70 struct ocfs2_xattr_header *xh;
73 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
74 #define OCFS2_XATTR_INLINE_SIZE 80
76 static struct ocfs2_xattr_def_value_root def_xv = {
77 .xv.xr_list.l_count = cpu_to_le16(1),
80 struct xattr_handler *ocfs2_xattr_handlers[] = {
81 &ocfs2_xattr_user_handler,
82 &ocfs2_xattr_trusted_handler,
86 static struct xattr_handler *ocfs2_xattr_handler_map[] = {
87 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
88 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
91 struct ocfs2_xattr_info {
98 struct ocfs2_xattr_search {
99 struct buffer_head *inode_bh;
101 * xattr_bh point to the block buffer head which has extended attribute
102 * when extended attribute in inode, xattr_bh is equal to inode_bh.
104 struct buffer_head *xattr_bh;
105 struct ocfs2_xattr_header *header;
106 struct ocfs2_xattr_bucket bucket;
109 struct ocfs2_xattr_entry *here;
113 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
114 struct ocfs2_xattr_header *xh,
119 static int ocfs2_xattr_index_block_find(struct inode *inode,
120 struct buffer_head *root_bh,
123 struct ocfs2_xattr_search *xs);
125 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
126 struct ocfs2_xattr_tree_root *xt,
130 static int ocfs2_xattr_create_index_block(struct inode *inode,
131 struct ocfs2_xattr_search *xs);
133 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
134 struct ocfs2_xattr_info *xi,
135 struct ocfs2_xattr_search *xs);
137 static int ocfs2_delete_xattr_index_block(struct inode *inode,
138 struct buffer_head *xb_bh);
140 static inline const char *ocfs2_xattr_prefix(int name_index)
142 struct xattr_handler *handler = NULL;
144 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
145 handler = ocfs2_xattr_handler_map[name_index];
147 return handler ? handler->prefix : NULL;
150 static u32 ocfs2_xattr_name_hash(struct inode *inode,
154 /* Get hash value of uuid from super block */
155 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
158 /* hash extended attribute name */
159 for (i = 0; i < name_len; i++) {
160 hash = (hash << OCFS2_HASH_SHIFT) ^
161 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
169 * ocfs2_xattr_hash_entry()
171 * Compute the hash of an extended attribute.
173 static void ocfs2_xattr_hash_entry(struct inode *inode,
174 struct ocfs2_xattr_header *header,
175 struct ocfs2_xattr_entry *entry)
178 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
180 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
181 entry->xe_name_hash = cpu_to_le32(hash);
186 static int ocfs2_xattr_extend_allocation(struct inode *inode,
188 struct buffer_head *xattr_bh,
189 struct ocfs2_xattr_value_root *xv)
192 int restart_func = 0;
194 handle_t *handle = NULL;
195 struct ocfs2_alloc_context *data_ac = NULL;
196 struct ocfs2_alloc_context *meta_ac = NULL;
197 enum ocfs2_alloc_restarted why;
198 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
199 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
200 struct ocfs2_extent_tree et;
202 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
204 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
208 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
215 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
217 handle = ocfs2_start_trans(osb, credits);
218 if (IS_ERR(handle)) {
219 status = PTR_ERR(handle);
225 restarted_transaction:
226 status = ocfs2_journal_access(handle, inode, xattr_bh,
227 OCFS2_JOURNAL_ACCESS_WRITE);
233 prev_clusters = le32_to_cpu(xv->xr_clusters);
234 status = ocfs2_add_clusters_in_btree(osb,
244 if ((status < 0) && (status != -EAGAIN)) {
245 if (status != -ENOSPC)
250 status = ocfs2_journal_dirty(handle, xattr_bh);
256 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
258 if (why != RESTART_NONE && clusters_to_add) {
259 if (why == RESTART_META) {
260 mlog(0, "restarting function.\n");
263 BUG_ON(why != RESTART_TRANS);
265 mlog(0, "restarting transaction.\n");
266 /* TODO: This can be more intelligent. */
267 credits = ocfs2_calc_extend_credits(osb->sb,
270 status = ocfs2_extend_trans(handle, credits);
272 /* handle still has to be committed at
278 goto restarted_transaction;
284 ocfs2_commit_trans(osb, handle);
288 ocfs2_free_alloc_context(data_ac);
292 ocfs2_free_alloc_context(meta_ac);
295 if ((!status) && restart_func) {
303 static int __ocfs2_remove_xattr_range(struct inode *inode,
304 struct buffer_head *root_bh,
305 struct ocfs2_xattr_value_root *xv,
306 u32 cpos, u32 phys_cpos, u32 len,
307 struct ocfs2_cached_dealloc_ctxt *dealloc)
310 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
311 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
312 struct inode *tl_inode = osb->osb_tl_inode;
314 struct ocfs2_alloc_context *meta_ac = NULL;
315 struct ocfs2_extent_tree et;
317 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
319 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
325 mutex_lock(&tl_inode->i_mutex);
327 if (ocfs2_truncate_log_needs_flush(osb)) {
328 ret = __ocfs2_flush_truncate_log(osb);
335 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
336 if (IS_ERR(handle)) {
337 ret = PTR_ERR(handle);
342 ret = ocfs2_journal_access(handle, inode, root_bh,
343 OCFS2_JOURNAL_ACCESS_WRITE);
349 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
356 le32_add_cpu(&xv->xr_clusters, -len);
358 ret = ocfs2_journal_dirty(handle, root_bh);
364 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
369 ocfs2_commit_trans(osb, handle);
371 mutex_unlock(&tl_inode->i_mutex);
374 ocfs2_free_alloc_context(meta_ac);
379 static int ocfs2_xattr_shrink_size(struct inode *inode,
382 struct buffer_head *root_bh,
383 struct ocfs2_xattr_value_root *xv)
386 u32 trunc_len, cpos, phys_cpos, alloc_size;
388 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
389 struct ocfs2_cached_dealloc_ctxt dealloc;
391 ocfs2_init_dealloc_ctxt(&dealloc);
393 if (old_clusters <= new_clusters)
397 trunc_len = old_clusters - new_clusters;
399 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
400 &alloc_size, &xv->xr_list);
406 if (alloc_size > trunc_len)
407 alloc_size = trunc_len;
409 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
410 phys_cpos, alloc_size,
417 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
418 ocfs2_remove_xattr_clusters_from_cache(inode, block,
421 trunc_len -= alloc_size;
425 ocfs2_schedule_truncate_log_flush(osb, 1);
426 ocfs2_run_deallocs(osb, &dealloc);
431 static int ocfs2_xattr_value_truncate(struct inode *inode,
432 struct buffer_head *root_bh,
433 struct ocfs2_xattr_value_root *xv,
437 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
438 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
440 if (new_clusters == old_clusters)
443 if (new_clusters > old_clusters)
444 ret = ocfs2_xattr_extend_allocation(inode,
445 new_clusters - old_clusters,
448 ret = ocfs2_xattr_shrink_size(inode,
449 old_clusters, new_clusters,
455 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
456 size_t *result, const char *prefix,
457 const char *name, int name_len)
459 char *p = buffer + *result;
460 int prefix_len = strlen(prefix);
461 int total_len = prefix_len + name_len + 1;
463 *result += total_len;
465 /* we are just looking for how big our buffer needs to be */
472 memcpy(p, prefix, prefix_len);
473 memcpy(p + prefix_len, name, name_len);
474 p[prefix_len + name_len] = '\0';
479 static int ocfs2_xattr_list_entries(struct inode *inode,
480 struct ocfs2_xattr_header *header,
481 char *buffer, size_t buffer_size)
485 const char *prefix, *name;
487 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
488 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
489 type = ocfs2_xattr_get_type(entry);
490 prefix = ocfs2_xattr_prefix(type);
493 name = (const char *)header +
494 le16_to_cpu(entry->xe_name_offset);
496 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
497 &result, prefix, name,
507 static int ocfs2_xattr_ibody_list(struct inode *inode,
508 struct ocfs2_dinode *di,
512 struct ocfs2_xattr_header *header = NULL;
513 struct ocfs2_inode_info *oi = OCFS2_I(inode);
516 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
519 header = (struct ocfs2_xattr_header *)
520 ((void *)di + inode->i_sb->s_blocksize -
521 le16_to_cpu(di->i_xattr_inline_size));
523 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
528 static int ocfs2_xattr_block_list(struct inode *inode,
529 struct ocfs2_dinode *di,
533 struct buffer_head *blk_bh = NULL;
534 struct ocfs2_xattr_block *xb;
537 if (!di->i_xattr_loc)
540 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
545 /*Verify the signature of xattr block*/
546 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
547 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
552 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
554 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
555 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
556 ret = ocfs2_xattr_list_entries(inode, header,
557 buffer, buffer_size);
559 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
560 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
561 buffer, buffer_size);
569 ssize_t ocfs2_listxattr(struct dentry *dentry,
573 int ret = 0, i_ret = 0, b_ret = 0;
574 struct buffer_head *di_bh = NULL;
575 struct ocfs2_dinode *di = NULL;
576 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
578 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
581 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
584 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
590 di = (struct ocfs2_dinode *)di_bh->b_data;
592 down_read(&oi->ip_xattr_sem);
593 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
601 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
606 up_read(&oi->ip_xattr_sem);
607 ocfs2_inode_unlock(dentry->d_inode, 0);
611 return i_ret + b_ret;
614 static int ocfs2_xattr_find_entry(int name_index,
616 struct ocfs2_xattr_search *xs)
618 struct ocfs2_xattr_entry *entry;
625 name_len = strlen(name);
627 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
628 cmp = name_index - ocfs2_xattr_get_type(entry);
630 cmp = name_len - entry->xe_name_len;
632 cmp = memcmp(name, (xs->base +
633 le16_to_cpu(entry->xe_name_offset)),
641 return cmp ? -ENODATA : 0;
644 static int ocfs2_xattr_get_value_outside(struct inode *inode,
645 struct ocfs2_xattr_value_root *xv,
649 u32 cpos, p_cluster, num_clusters, bpc, clusters;
652 size_t cplen, blocksize;
653 struct buffer_head *bh = NULL;
654 struct ocfs2_extent_list *el;
657 clusters = le32_to_cpu(xv->xr_clusters);
658 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
659 blocksize = inode->i_sb->s_blocksize;
662 while (cpos < clusters) {
663 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
670 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
671 /* Copy ocfs2_xattr_value */
672 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
673 ret = ocfs2_read_block(inode, blkno, &bh);
679 cplen = len >= blocksize ? blocksize : len;
680 memcpy(buffer, bh->b_data, cplen);
689 cpos += num_clusters;
695 static int ocfs2_xattr_ibody_get(struct inode *inode,
700 struct ocfs2_xattr_search *xs)
702 struct ocfs2_inode_info *oi = OCFS2_I(inode);
703 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
704 struct ocfs2_xattr_value_root *xv;
708 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
711 xs->end = (void *)di + inode->i_sb->s_blocksize;
712 xs->header = (struct ocfs2_xattr_header *)
713 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
714 xs->base = (void *)xs->header;
715 xs->here = xs->header->xh_entries;
717 ret = ocfs2_xattr_find_entry(name_index, name, xs);
720 size = le64_to_cpu(xs->here->xe_value_size);
722 if (size > buffer_size)
724 if (ocfs2_xattr_is_local(xs->here)) {
725 memcpy(buffer, (void *)xs->base +
726 le16_to_cpu(xs->here->xe_name_offset) +
727 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
729 xv = (struct ocfs2_xattr_value_root *)
730 (xs->base + le16_to_cpu(
731 xs->here->xe_name_offset) +
732 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
733 ret = ocfs2_xattr_get_value_outside(inode, xv,
745 static int ocfs2_xattr_block_get(struct inode *inode,
750 struct ocfs2_xattr_search *xs)
752 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
753 struct buffer_head *blk_bh = NULL;
754 struct ocfs2_xattr_block *xb;
755 struct ocfs2_xattr_value_root *xv;
757 int ret = -ENODATA, name_offset, name_len, block_off, i;
759 if (!di->i_xattr_loc)
762 memset(&xs->bucket, 0, sizeof(xs->bucket));
764 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
769 /*Verify the signature of xattr block*/
770 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
771 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
776 xs->xattr_bh = blk_bh;
777 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
779 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
780 xs->header = &xb->xb_attrs.xb_header;
781 xs->base = (void *)xs->header;
782 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
783 xs->here = xs->header->xh_entries;
785 ret = ocfs2_xattr_find_entry(name_index, name, xs);
787 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
793 size = le64_to_cpu(xs->here->xe_value_size);
796 if (size > buffer_size)
799 name_offset = le16_to_cpu(xs->here->xe_name_offset);
800 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
801 i = xs->here - xs->header->xh_entries;
803 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
804 ret = ocfs2_xattr_bucket_get_name_value(inode,
809 xs->base = xs->bucket.bhs[block_off]->b_data;
811 if (ocfs2_xattr_is_local(xs->here)) {
812 memcpy(buffer, (void *)xs->base +
813 name_offset + name_len, size);
815 xv = (struct ocfs2_xattr_value_root *)
816 (xs->base + name_offset + name_len);
817 ret = ocfs2_xattr_get_value_outside(inode, xv,
827 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
828 brelse(xs->bucket.bhs[i]);
829 memset(&xs->bucket, 0, sizeof(xs->bucket));
837 * Copy an extended attribute into the buffer provided.
838 * Buffer is NULL to compute the size of buffer required.
840 int ocfs2_xattr_get(struct inode *inode,
847 struct ocfs2_dinode *di = NULL;
848 struct buffer_head *di_bh = NULL;
849 struct ocfs2_inode_info *oi = OCFS2_I(inode);
850 struct ocfs2_xattr_search xis = {
851 .not_found = -ENODATA,
853 struct ocfs2_xattr_search xbs = {
854 .not_found = -ENODATA,
857 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
860 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
863 ret = ocfs2_inode_lock(inode, &di_bh, 0);
868 xis.inode_bh = xbs.inode_bh = di_bh;
869 di = (struct ocfs2_dinode *)di_bh->b_data;
871 down_read(&oi->ip_xattr_sem);
872 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
875 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
877 up_read(&oi->ip_xattr_sem);
878 ocfs2_inode_unlock(inode, 0);
885 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
886 struct ocfs2_xattr_value_root *xv,
890 int ret = 0, i, cp_len, credits;
891 u16 blocksize = inode->i_sb->s_blocksize;
892 u32 p_cluster, num_clusters;
893 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
894 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
896 struct buffer_head *bh = NULL;
899 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
901 credits = clusters * bpc;
902 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
903 if (IS_ERR(handle)) {
904 ret = PTR_ERR(handle);
909 while (cpos < clusters) {
910 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
911 &num_clusters, &xv->xr_list);
917 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
919 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
920 ret = ocfs2_read_block(inode, blkno, &bh);
926 ret = ocfs2_journal_access(handle,
929 OCFS2_JOURNAL_ACCESS_WRITE);
935 cp_len = value_len > blocksize ? blocksize : value_len;
936 memcpy(bh->b_data, value, cp_len);
939 if (cp_len < blocksize)
940 memset(bh->b_data + cp_len, 0,
943 ret = ocfs2_journal_dirty(handle, bh);
952 * XXX: do we need to empty all the following
953 * blocks in this cluster?
958 cpos += num_clusters;
961 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
968 static int ocfs2_xattr_cleanup(struct inode *inode,
969 struct ocfs2_xattr_info *xi,
970 struct ocfs2_xattr_search *xs,
973 handle_t *handle = NULL;
975 size_t name_len = strlen(xi->name);
976 void *val = xs->base + offs;
977 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
979 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
980 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
981 if (IS_ERR(handle)) {
982 ret = PTR_ERR(handle);
986 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
987 OCFS2_JOURNAL_ACCESS_WRITE);
992 /* Decrease xattr count */
993 le16_add_cpu(&xs->header->xh_count, -1);
994 /* Remove the xattr entry and tree root which has already be set*/
995 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
996 memset(val, 0, size);
998 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1002 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1007 static int ocfs2_xattr_update_entry(struct inode *inode,
1008 struct ocfs2_xattr_info *xi,
1009 struct ocfs2_xattr_search *xs,
1012 handle_t *handle = NULL;
1015 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1016 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1017 if (IS_ERR(handle)) {
1018 ret = PTR_ERR(handle);
1022 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1023 OCFS2_JOURNAL_ACCESS_WRITE);
1029 xs->here->xe_name_offset = cpu_to_le16(offs);
1030 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1031 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1032 ocfs2_xattr_set_local(xs->here, 1);
1034 ocfs2_xattr_set_local(xs->here, 0);
1035 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1037 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1041 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1047 * ocfs2_xattr_set_value_outside()
1049 * Set large size value in B tree.
1051 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1052 struct ocfs2_xattr_info *xi,
1053 struct ocfs2_xattr_search *xs,
1056 size_t name_len = strlen(xi->name);
1057 void *val = xs->base + offs;
1058 struct ocfs2_xattr_value_root *xv = NULL;
1059 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1062 memset(val, 0, size);
1063 memcpy(val, xi->name, name_len);
1064 xv = (struct ocfs2_xattr_value_root *)
1065 (val + OCFS2_XATTR_SIZE(name_len));
1066 xv->xr_clusters = 0;
1067 xv->xr_last_eb_blk = 0;
1068 xv->xr_list.l_tree_depth = 0;
1069 xv->xr_list.l_count = cpu_to_le16(1);
1070 xv->xr_list.l_next_free_rec = 0;
1072 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1078 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1084 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1092 * ocfs2_xattr_set_entry_local()
1094 * Set, replace or remove extended attribute in local.
1096 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1097 struct ocfs2_xattr_info *xi,
1098 struct ocfs2_xattr_search *xs,
1099 struct ocfs2_xattr_entry *last,
1102 size_t name_len = strlen(xi->name);
1105 if (xi->value && xs->not_found) {
1106 /* Insert the new xattr entry. */
1107 le16_add_cpu(&xs->header->xh_count, 1);
1108 ocfs2_xattr_set_type(last, xi->name_index);
1109 ocfs2_xattr_set_local(last, 1);
1110 last->xe_name_len = name_len;
1116 first_val = xs->base + min_offs;
1117 offs = le16_to_cpu(xs->here->xe_name_offset);
1118 val = xs->base + offs;
1120 if (le64_to_cpu(xs->here->xe_value_size) >
1121 OCFS2_XATTR_INLINE_SIZE)
1122 size = OCFS2_XATTR_SIZE(name_len) +
1123 OCFS2_XATTR_ROOT_SIZE;
1125 size = OCFS2_XATTR_SIZE(name_len) +
1126 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1128 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1129 OCFS2_XATTR_SIZE(xi->value_len)) {
1130 /* The old and the new value have the
1131 same size. Just replace the value. */
1132 ocfs2_xattr_set_local(xs->here, 1);
1133 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1134 /* Clear value bytes. */
1135 memset(val + OCFS2_XATTR_SIZE(name_len),
1137 OCFS2_XATTR_SIZE(xi->value_len));
1138 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1143 /* Remove the old name+value. */
1144 memmove(first_val + size, first_val, val - first_val);
1145 memset(first_val, 0, size);
1146 xs->here->xe_name_hash = 0;
1147 xs->here->xe_name_offset = 0;
1148 ocfs2_xattr_set_local(xs->here, 1);
1149 xs->here->xe_value_size = 0;
1153 /* Adjust all value offsets. */
1154 last = xs->header->xh_entries;
1155 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1156 size_t o = le16_to_cpu(last->xe_name_offset);
1159 last->xe_name_offset = cpu_to_le16(o + size);
1164 /* Remove the old entry. */
1166 memmove(xs->here, xs->here + 1,
1167 (void *)last - (void *)xs->here);
1168 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1169 le16_add_cpu(&xs->header->xh_count, -1);
1173 /* Insert the new name+value. */
1174 size_t size = OCFS2_XATTR_SIZE(name_len) +
1175 OCFS2_XATTR_SIZE(xi->value_len);
1176 void *val = xs->base + min_offs - size;
1178 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1179 memset(val, 0, size);
1180 memcpy(val, xi->name, name_len);
1181 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1184 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1185 ocfs2_xattr_set_local(xs->here, 1);
1186 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1193 * ocfs2_xattr_set_entry()
1195 * Set extended attribute entry into inode or block.
1197 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1198 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1199 * then set value in B tree with set_value_outside().
1201 static int ocfs2_xattr_set_entry(struct inode *inode,
1202 struct ocfs2_xattr_info *xi,
1203 struct ocfs2_xattr_search *xs,
1206 struct ocfs2_xattr_entry *last;
1207 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1208 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1209 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1211 handle_t *handle = NULL;
1213 struct ocfs2_xattr_info xi_l = {
1214 .name_index = xi->name_index,
1217 .value_len = xi->value_len,
1220 /* Compute min_offs, last and free space. */
1221 last = xs->header->xh_entries;
1223 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1224 size_t offs = le16_to_cpu(last->xe_name_offset);
1225 if (offs < min_offs)
1230 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1234 if (!xs->not_found) {
1236 if (ocfs2_xattr_is_local(xs->here))
1237 size = OCFS2_XATTR_SIZE(name_len) +
1238 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1240 size = OCFS2_XATTR_SIZE(name_len) +
1241 OCFS2_XATTR_ROOT_SIZE;
1242 free += (size + sizeof(struct ocfs2_xattr_entry));
1244 /* Check free space in inode or block */
1245 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1246 if (free < sizeof(struct ocfs2_xattr_entry) +
1247 OCFS2_XATTR_SIZE(name_len) +
1248 OCFS2_XATTR_ROOT_SIZE) {
1252 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1253 xi_l.value = (void *)&def_xv;
1254 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1255 } else if (xi->value) {
1256 if (free < sizeof(struct ocfs2_xattr_entry) +
1257 OCFS2_XATTR_SIZE(name_len) +
1258 OCFS2_XATTR_SIZE(xi->value_len)) {
1264 if (!xs->not_found) {
1265 /* For existing extended attribute */
1266 size_t size = OCFS2_XATTR_SIZE(name_len) +
1267 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1268 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1269 void *val = xs->base + offs;
1271 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1272 /* Replace existing local xattr with tree root */
1273 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1278 } else if (!ocfs2_xattr_is_local(xs->here)) {
1279 /* For existing xattr which has value outside */
1280 struct ocfs2_xattr_value_root *xv = NULL;
1281 xv = (struct ocfs2_xattr_value_root *)(val +
1282 OCFS2_XATTR_SIZE(name_len));
1284 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1286 * If new value need set outside also,
1287 * first truncate old value to new value,
1288 * then set new value with set_value_outside().
1290 ret = ocfs2_xattr_value_truncate(inode,
1299 ret = __ocfs2_xattr_set_value_outside(inode,
1308 ret = ocfs2_xattr_update_entry(inode,
1317 * If new value need set in local,
1318 * just trucate old value to zero.
1320 ret = ocfs2_xattr_value_truncate(inode,
1330 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1331 OCFS2_INODE_UPDATE_CREDITS);
1332 if (IS_ERR(handle)) {
1333 ret = PTR_ERR(handle);
1338 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1339 OCFS2_JOURNAL_ACCESS_WRITE);
1345 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1346 /* set extended attribute in external block. */
1347 ret = ocfs2_extend_trans(handle,
1348 OCFS2_INODE_UPDATE_CREDITS +
1349 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1354 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1355 OCFS2_JOURNAL_ACCESS_WRITE);
1363 * Set value in local, include set tree root in local.
1364 * This is the first step for value size >INLINE_SIZE.
1366 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1368 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1369 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1376 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1377 (flag & OCFS2_INLINE_XATTR_FL)) {
1378 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1379 unsigned int xattrsize = osb->s_xattr_inline_size;
1382 * Adjust extent record count or inline data size
1383 * to reserve space for extended attribute.
1385 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1386 struct ocfs2_inline_data *idata = &di->id2.i_data;
1387 le16_add_cpu(&idata->id_count, -xattrsize);
1388 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1389 struct ocfs2_extent_list *el = &di->id2.i_list;
1390 le16_add_cpu(&el->l_count, -(xattrsize /
1391 sizeof(struct ocfs2_extent_rec)));
1393 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1395 /* Update xattr flag */
1396 spin_lock(&oi->ip_lock);
1397 oi->ip_dyn_features |= flag;
1398 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1399 spin_unlock(&oi->ip_lock);
1400 /* Update inode ctime */
1401 inode->i_ctime = CURRENT_TIME;
1402 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1403 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1405 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1410 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1412 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1414 * Set value outside in B tree.
1415 * This is the second step for value size > INLINE_SIZE.
1417 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1418 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1424 * If set value outside failed, we have to clean
1425 * the junk tree root we have already set in local.
1427 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1437 static int ocfs2_remove_value_outside(struct inode*inode,
1438 struct buffer_head *bh,
1439 struct ocfs2_xattr_header *header)
1443 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1444 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1446 if (!ocfs2_xattr_is_local(entry)) {
1447 struct ocfs2_xattr_value_root *xv;
1450 val = (void *)header +
1451 le16_to_cpu(entry->xe_name_offset);
1452 xv = (struct ocfs2_xattr_value_root *)
1453 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1454 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1465 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1466 struct buffer_head *di_bh)
1469 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1470 struct ocfs2_xattr_header *header;
1473 header = (struct ocfs2_xattr_header *)
1474 ((void *)di + inode->i_sb->s_blocksize -
1475 le16_to_cpu(di->i_xattr_inline_size));
1477 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1482 static int ocfs2_xattr_block_remove(struct inode *inode,
1483 struct buffer_head *blk_bh)
1485 struct ocfs2_xattr_block *xb;
1488 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1489 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1490 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1491 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1493 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1498 static int ocfs2_xattr_free_block(struct inode *inode,
1501 struct inode *xb_alloc_inode;
1502 struct buffer_head *xb_alloc_bh = NULL;
1503 struct buffer_head *blk_bh = NULL;
1504 struct ocfs2_xattr_block *xb;
1505 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1511 ret = ocfs2_read_block(inode, block, &blk_bh);
1517 /*Verify the signature of xattr block*/
1518 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1519 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1524 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1530 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1531 blk = le64_to_cpu(xb->xb_blkno);
1532 bit = le16_to_cpu(xb->xb_suballoc_bit);
1533 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1535 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1536 EXTENT_ALLOC_SYSTEM_INODE,
1537 le16_to_cpu(xb->xb_suballoc_slot));
1538 if (!xb_alloc_inode) {
1543 mutex_lock(&xb_alloc_inode->i_mutex);
1545 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1551 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1552 if (IS_ERR(handle)) {
1553 ret = PTR_ERR(handle);
1558 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1563 ocfs2_commit_trans(osb, handle);
1565 ocfs2_inode_unlock(xb_alloc_inode, 1);
1566 brelse(xb_alloc_bh);
1568 mutex_unlock(&xb_alloc_inode->i_mutex);
1569 iput(xb_alloc_inode);
1576 * ocfs2_xattr_remove()
1578 * Free extended attribute resources associated with this inode.
1580 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1582 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1583 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1587 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1590 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1593 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1594 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1601 if (di->i_xattr_loc) {
1602 ret = ocfs2_xattr_free_block(inode,
1603 le64_to_cpu(di->i_xattr_loc));
1610 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1611 OCFS2_INODE_UPDATE_CREDITS);
1612 if (IS_ERR(handle)) {
1613 ret = PTR_ERR(handle);
1617 ret = ocfs2_journal_access(handle, inode, di_bh,
1618 OCFS2_JOURNAL_ACCESS_WRITE);
1624 di->i_xattr_loc = 0;
1626 spin_lock(&oi->ip_lock);
1627 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1628 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1629 spin_unlock(&oi->ip_lock);
1631 ret = ocfs2_journal_dirty(handle, di_bh);
1635 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1640 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1641 struct ocfs2_dinode *di)
1643 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1644 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1647 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1650 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1651 struct ocfs2_inline_data *idata = &di->id2.i_data;
1652 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1653 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1654 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1655 le64_to_cpu(di->i_size);
1657 struct ocfs2_extent_list *el = &di->id2.i_list;
1658 free = (le16_to_cpu(el->l_count) -
1659 le16_to_cpu(el->l_next_free_rec)) *
1660 sizeof(struct ocfs2_extent_rec);
1662 if (free >= xattrsize)
1669 * ocfs2_xattr_ibody_find()
1671 * Find extended attribute in inode block and
1672 * fill search info into struct ocfs2_xattr_search.
1674 static int ocfs2_xattr_ibody_find(struct inode *inode,
1677 struct ocfs2_xattr_search *xs)
1679 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1680 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1684 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1687 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1688 down_read(&oi->ip_alloc_sem);
1689 has_space = ocfs2_xattr_has_space_inline(inode, di);
1690 up_read(&oi->ip_alloc_sem);
1695 xs->xattr_bh = xs->inode_bh;
1696 xs->end = (void *)di + inode->i_sb->s_blocksize;
1697 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1698 xs->header = (struct ocfs2_xattr_header *)
1699 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1701 xs->header = (struct ocfs2_xattr_header *)
1702 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1703 xs->base = (void *)xs->header;
1704 xs->here = xs->header->xh_entries;
1706 /* Find the named attribute. */
1707 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1708 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1709 if (ret && ret != -ENODATA)
1711 xs->not_found = ret;
1718 * ocfs2_xattr_ibody_set()
1720 * Set, replace or remove an extended attribute into inode block.
1723 static int ocfs2_xattr_ibody_set(struct inode *inode,
1724 struct ocfs2_xattr_info *xi,
1725 struct ocfs2_xattr_search *xs)
1727 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1728 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1731 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1734 down_write(&oi->ip_alloc_sem);
1735 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1736 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1742 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1743 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1745 up_write(&oi->ip_alloc_sem);
1751 * ocfs2_xattr_block_find()
1753 * Find extended attribute in external block and
1754 * fill search info into struct ocfs2_xattr_search.
1756 static int ocfs2_xattr_block_find(struct inode *inode,
1759 struct ocfs2_xattr_search *xs)
1761 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1762 struct buffer_head *blk_bh = NULL;
1763 struct ocfs2_xattr_block *xb;
1766 if (!di->i_xattr_loc)
1769 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
1774 /*Verify the signature of xattr block*/
1775 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1776 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1781 xs->xattr_bh = blk_bh;
1782 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1784 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1785 xs->header = &xb->xb_attrs.xb_header;
1786 xs->base = (void *)xs->header;
1787 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1788 xs->here = xs->header->xh_entries;
1790 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1792 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1796 if (ret && ret != -ENODATA) {
1797 xs->xattr_bh = NULL;
1800 xs->not_found = ret;
1809 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1810 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1813 static int ocfs2_restore_xattr_block(struct inode *inode,
1814 struct ocfs2_xattr_search *xs)
1818 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1819 struct ocfs2_xattr_block *xb =
1820 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1821 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
1822 u16 xb_flags = le16_to_cpu(xb->xb_flags);
1824 BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
1825 le16_to_cpu(el->l_next_free_rec) != 0);
1827 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1828 if (IS_ERR(handle)) {
1829 ret = PTR_ERR(handle);
1834 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1835 OCFS2_JOURNAL_ACCESS_WRITE);
1841 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
1842 offsetof(struct ocfs2_xattr_block, xb_attrs));
1844 xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
1846 ocfs2_journal_dirty(handle, xs->xattr_bh);
1849 ocfs2_commit_trans(osb, handle);
1855 * ocfs2_xattr_block_set()
1857 * Set, replace or remove an extended attribute into external block.
1860 static int ocfs2_xattr_block_set(struct inode *inode,
1861 struct ocfs2_xattr_info *xi,
1862 struct ocfs2_xattr_search *xs)
1864 struct buffer_head *new_bh = NULL;
1865 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1866 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1867 struct ocfs2_alloc_context *meta_ac = NULL;
1868 handle_t *handle = NULL;
1869 struct ocfs2_xattr_block *xblk = NULL;
1870 u16 suballoc_bit_start;
1875 if (!xs->xattr_bh) {
1877 * Alloc one external block for extended attribute
1880 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1885 handle = ocfs2_start_trans(osb,
1886 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1887 if (IS_ERR(handle)) {
1888 ret = PTR_ERR(handle);
1892 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1893 OCFS2_JOURNAL_ACCESS_CREATE);
1899 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1900 &suballoc_bit_start, &num_got,
1907 new_bh = sb_getblk(inode->i_sb, first_blkno);
1908 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1910 ret = ocfs2_journal_access(handle, inode, new_bh,
1911 OCFS2_JOURNAL_ACCESS_CREATE);
1917 /* Initialize ocfs2_xattr_block */
1918 xs->xattr_bh = new_bh;
1919 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1920 memset(xblk, 0, inode->i_sb->s_blocksize);
1921 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1922 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1923 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1924 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1925 xblk->xb_blkno = cpu_to_le64(first_blkno);
1927 xs->header = &xblk->xb_attrs.xb_header;
1928 xs->base = (void *)xs->header;
1929 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1930 xs->here = xs->header->xh_entries;
1933 ret = ocfs2_journal_dirty(handle, new_bh);
1938 di->i_xattr_loc = cpu_to_le64(first_blkno);
1939 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1943 ocfs2_commit_trans(osb, handle);
1946 ocfs2_free_alloc_context(meta_ac);
1950 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1952 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1953 /* Set extended attribute into external block */
1954 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1955 if (!ret || ret != -ENOSPC)
1958 ret = ocfs2_xattr_create_index_block(inode, xs);
1963 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1964 if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
1965 ret = ocfs2_restore_xattr_block(inode, xs);
1975 * Set, replace or remove an extended attribute for this inode.
1976 * value is NULL to remove an existing extended attribute, else either
1977 * create or replace an extended attribute.
1979 int ocfs2_xattr_set(struct inode *inode,
1986 struct buffer_head *di_bh = NULL;
1987 struct ocfs2_dinode *di;
1989 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1991 struct ocfs2_xattr_info xi = {
1992 .name_index = name_index,
1995 .value_len = value_len,
1998 struct ocfs2_xattr_search xis = {
1999 .not_found = -ENODATA,
2002 struct ocfs2_xattr_search xbs = {
2003 .not_found = -ENODATA,
2006 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2009 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2014 xis.inode_bh = xbs.inode_bh = di_bh;
2015 di = (struct ocfs2_dinode *)di_bh->b_data;
2017 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2019 * Scan inode and external block to find the same name
2020 * extended attribute and collect search infomation.
2022 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2025 if (xis.not_found) {
2026 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2031 if (xis.not_found && xbs.not_found) {
2033 if (flags & XATTR_REPLACE)
2040 if (flags & XATTR_CREATE)
2045 /* Remove existing extended attribute */
2047 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2048 else if (!xbs.not_found)
2049 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2051 /* We always try to set extended attribute into inode first*/
2052 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2053 if (!ret && !xbs.not_found) {
2055 * If succeed and that extended attribute existing in
2056 * external block, then we will remove it.
2060 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2061 } else if (ret == -ENOSPC) {
2062 if (di->i_xattr_loc && !xbs.xattr_bh) {
2063 ret = ocfs2_xattr_block_find(inode, name_index,
2069 * If no space in inode, we will set extended attribute
2070 * into external block.
2072 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2075 if (!xis.not_found) {
2077 * If succeed and that extended attribute
2078 * existing in inode, we will remove it.
2082 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2087 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2088 ocfs2_inode_unlock(inode, 1);
2090 brelse(xbs.xattr_bh);
2091 for (i = 0; i < blk_per_bucket; i++)
2092 brelse(xbs.bucket.bhs[i]);
2098 * Find the xattr extent rec which may contains name_hash.
2099 * e_cpos will be the first name hash of the xattr rec.
2100 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2102 static int ocfs2_xattr_get_rec(struct inode *inode,
2107 struct ocfs2_extent_list *el)
2110 struct buffer_head *eb_bh = NULL;
2111 struct ocfs2_extent_block *eb;
2112 struct ocfs2_extent_rec *rec = NULL;
2115 if (el->l_tree_depth) {
2116 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2122 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2125 if (el->l_tree_depth) {
2126 ocfs2_error(inode->i_sb,
2127 "Inode %lu has non zero tree depth in "
2128 "xattr tree block %llu\n", inode->i_ino,
2129 (unsigned long long)eb_bh->b_blocknr);
2135 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2136 rec = &el->l_recs[i];
2138 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2139 e_blkno = le64_to_cpu(rec->e_blkno);
2145 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2146 "record (%u, %u, 0) in xattr", inode->i_ino,
2147 le32_to_cpu(rec->e_cpos),
2148 ocfs2_rec_clusters(el, rec));
2153 *p_blkno = le64_to_cpu(rec->e_blkno);
2154 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2156 *e_cpos = le32_to_cpu(rec->e_cpos);
2162 typedef int (xattr_bucket_func)(struct inode *inode,
2163 struct ocfs2_xattr_bucket *bucket,
2166 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2167 struct buffer_head *header_bh,
2174 int i, ret = 0, cmp = 1, block_off, new_offset;
2175 struct ocfs2_xattr_header *xh =
2176 (struct ocfs2_xattr_header *)header_bh->b_data;
2177 size_t name_len = strlen(name);
2178 struct ocfs2_xattr_entry *xe = NULL;
2179 struct buffer_head *name_bh = NULL;
2183 * We don't use binary search in the bucket because there
2184 * may be multiple entries with the same name hash.
2186 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2187 xe = &xh->xh_entries[i];
2189 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2191 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2194 cmp = name_index - ocfs2_xattr_get_type(xe);
2196 cmp = name_len - xe->xe_name_len;
2200 ret = ocfs2_xattr_bucket_get_name_value(inode,
2210 ret = ocfs2_read_block(inode, header_bh->b_blocknr + block_off,
2216 xe_name = name_bh->b_data + new_offset;
2218 cmp = memcmp(name, xe_name, name_len);
2234 * Find the specified xattr entry in a series of buckets.
2235 * This series start from p_blkno and last for num_clusters.
2236 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2237 * the num of the valid buckets.
2239 * Return the buffer_head this xattr should reside in. And if the xattr's
2240 * hash is in the gap of 2 buckets, return the lower bucket.
2242 static int ocfs2_xattr_bucket_find(struct inode *inode,
2249 struct ocfs2_xattr_search *xs)
2252 struct buffer_head *bh = NULL;
2253 struct buffer_head *lower_bh = NULL;
2254 struct ocfs2_xattr_header *xh = NULL;
2255 struct ocfs2_xattr_entry *xe = NULL;
2257 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2258 int low_bucket = 0, bucket, high_bucket;
2262 ret = ocfs2_read_block(inode, p_blkno, &bh);
2268 xh = (struct ocfs2_xattr_header *)bh->b_data;
2269 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2271 while (low_bucket <= high_bucket) {
2274 bucket = (low_bucket + high_bucket) / 2;
2276 blkno = p_blkno + bucket * blk_per_bucket;
2278 ret = ocfs2_read_block(inode, blkno, &bh);
2284 xh = (struct ocfs2_xattr_header *)bh->b_data;
2285 xe = &xh->xh_entries[0];
2286 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2287 high_bucket = bucket - 1;
2292 * Check whether the hash of the last entry in our
2293 * bucket is larger than the search one. for an empty
2294 * bucket, the last one is also the first one.
2297 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2299 last_hash = le32_to_cpu(xe->xe_name_hash);
2301 /* record lower_bh which may be the insert place. */
2306 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2307 low_bucket = bucket + 1;
2311 /* the searched xattr should reside in this bucket if exists. */
2312 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2313 name_index, name, name_hash,
2323 * Record the bucket we have found.
2324 * When the xattr's hash value is in the gap of 2 buckets, we will
2325 * always set it to the previous bucket.
2329 * We can't find any bucket whose first name_hash is less
2330 * than the find name_hash.
2332 BUG_ON(bh->b_blocknr != p_blkno);
2336 xs->bucket.bhs[0] = lower_bh;
2337 xs->bucket.xh = (struct ocfs2_xattr_header *)
2338 xs->bucket.bhs[0]->b_data;
2341 xs->header = xs->bucket.xh;
2342 xs->base = xs->bucket.bhs[0]->b_data;
2343 xs->end = xs->base + inode->i_sb->s_blocksize;
2347 * If we have found the xattr enty, read all the blocks in
2350 ret = ocfs2_read_blocks(inode, xs->bucket.bhs[0]->b_blocknr + 1,
2351 blk_per_bucket - 1, &xs->bucket.bhs[1],
2358 xs->here = &xs->header->xh_entries[index];
2359 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2360 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2370 static int ocfs2_xattr_index_block_find(struct inode *inode,
2371 struct buffer_head *root_bh,
2374 struct ocfs2_xattr_search *xs)
2377 struct ocfs2_xattr_block *xb =
2378 (struct ocfs2_xattr_block *)root_bh->b_data;
2379 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2380 struct ocfs2_extent_list *el = &xb_root->xt_list;
2382 u32 first_hash, num_clusters = 0;
2383 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
2385 if (le16_to_cpu(el->l_next_free_rec) == 0)
2388 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2389 name, name_hash, name_index);
2391 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2398 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2400 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2401 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2403 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2404 p_blkno, first_hash, num_clusters, xs);
2410 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2413 xattr_bucket_func *func,
2417 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2418 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2419 u32 num_buckets = clusters * bpc;
2420 struct ocfs2_xattr_bucket bucket;
2422 memset(&bucket, 0, sizeof(bucket));
2424 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2427 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2428 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket,
2435 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2437 * The real bucket num in this series of blocks is stored
2438 * in the 1st bucket.
2441 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2443 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
2444 le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
2446 ret = func(inode, &bucket, para);
2453 for (j = 0; j < blk_per_bucket; j++)
2454 brelse(bucket.bhs[j]);
2455 memset(&bucket, 0, sizeof(bucket));
2459 for (j = 0; j < blk_per_bucket; j++)
2460 brelse(bucket.bhs[j]);
2465 struct ocfs2_xattr_tree_list {
2471 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2472 struct ocfs2_xattr_header *xh,
2479 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2482 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2484 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2485 *new_offset = name_offset % inode->i_sb->s_blocksize;
2490 static int ocfs2_list_xattr_bucket(struct inode *inode,
2491 struct ocfs2_xattr_bucket *bucket,
2495 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2496 int i, block_off, new_offset;
2497 const char *prefix, *name;
2499 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2500 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2501 type = ocfs2_xattr_get_type(entry);
2502 prefix = ocfs2_xattr_prefix(type);
2505 ret = ocfs2_xattr_bucket_get_name_value(inode,
2513 name = (const char *)bucket->bhs[block_off]->b_data +
2515 ret = ocfs2_xattr_list_entry(xl->buffer,
2519 entry->xe_name_len);
2528 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2529 struct ocfs2_xattr_tree_root *xt,
2533 struct ocfs2_extent_list *el = &xt->xt_list;
2535 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2537 struct ocfs2_xattr_tree_list xl = {
2539 .buffer_size = buffer_size,
2543 if (le16_to_cpu(el->l_next_free_rec) == 0)
2546 while (name_hash > 0) {
2547 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2548 &e_cpos, &num_clusters, el);
2554 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2555 ocfs2_list_xattr_bucket,
2565 name_hash = e_cpos - 1;
2573 static int cmp_xe(const void *a, const void *b)
2575 const struct ocfs2_xattr_entry *l = a, *r = b;
2576 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2577 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2579 if (l_hash > r_hash)
2581 if (l_hash < r_hash)
2586 static void swap_xe(void *a, void *b, int size)
2588 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2591 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2592 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2596 * When the ocfs2_xattr_block is filled up, new bucket will be created
2597 * and all the xattr entries will be moved to the new bucket.
2598 * Note: we need to sort the entries since they are not saved in order
2599 * in the ocfs2_xattr_block.
2601 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2602 struct buffer_head *xb_bh,
2603 struct buffer_head *xh_bh,
2604 struct buffer_head *data_bh)
2606 int i, blocksize = inode->i_sb->s_blocksize;
2607 u16 offset, size, off_change;
2608 struct ocfs2_xattr_entry *xe;
2609 struct ocfs2_xattr_block *xb =
2610 (struct ocfs2_xattr_block *)xb_bh->b_data;
2611 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2612 struct ocfs2_xattr_header *xh =
2613 (struct ocfs2_xattr_header *)xh_bh->b_data;
2614 u16 count = le16_to_cpu(xb_xh->xh_count);
2615 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2617 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2618 (unsigned long long)xb_bh->b_blocknr,
2619 (unsigned long long)xh_bh->b_blocknr);
2621 memset(xh_bh->b_data, 0, blocksize);
2623 memset(data_bh->b_data, 0, blocksize);
2625 * Since the xe_name_offset is based on ocfs2_xattr_header,
2626 * there is a offset change corresponding to the change of
2627 * ocfs2_xattr_header's position.
2629 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2630 xe = &xb_xh->xh_entries[count - 1];
2631 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2632 size = blocksize - offset;
2634 /* copy all the names and values. */
2636 target = data_bh->b_data;
2637 memcpy(target + offset, src + offset, size);
2639 /* Init new header now. */
2640 xh->xh_count = xb_xh->xh_count;
2641 xh->xh_num_buckets = cpu_to_le16(1);
2642 xh->xh_name_value_len = cpu_to_le16(size);
2643 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2645 /* copy all the entries. */
2646 target = xh_bh->b_data;
2647 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2648 size = count * sizeof(struct ocfs2_xattr_entry);
2649 memcpy(target + offset, (char *)xb_xh + offset, size);
2651 /* Change the xe offset for all the xe because of the move. */
2652 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2653 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2654 for (i = 0; i < count; i++)
2655 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2657 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2658 offset, size, off_change);
2660 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2665 * After we move xattr from block to index btree, we have to
2666 * update ocfs2_xattr_search to the new xe and base.
2668 * When the entry is in xattr block, xattr_bh indicates the storage place.
2669 * While if the entry is in index b-tree, "bucket" indicates the
2670 * real place of the xattr.
2672 static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2673 struct ocfs2_xattr_search *xs,
2674 struct buffer_head *old_bh,
2675 struct buffer_head *new_bh)
2678 char *buf = old_bh->b_data;
2679 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2680 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2681 int i, blocksize = inode->i_sb->s_blocksize;
2682 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2684 xs->bucket.bhs[0] = new_bh;
2686 xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2687 xs->header = xs->bucket.xh;
2689 xs->base = new_bh->b_data;
2690 xs->end = xs->base + inode->i_sb->s_blocksize;
2692 if (!xs->not_found) {
2693 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2694 ret = ocfs2_read_blocks(inode,
2695 xs->bucket.bhs[0]->b_blocknr + 1,
2696 blk_per_bucket - 1, &xs->bucket.bhs[1],
2703 i = xs->here - old_xh->xh_entries;
2704 xs->here = &xs->header->xh_entries[i];
2711 static int ocfs2_xattr_create_index_block(struct inode *inode,
2712 struct ocfs2_xattr_search *xs)
2714 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2718 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2719 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2720 struct ocfs2_alloc_context *data_ac;
2721 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2722 struct buffer_head *xb_bh = xs->xattr_bh;
2723 struct ocfs2_xattr_block *xb =
2724 (struct ocfs2_xattr_block *)xb_bh->b_data;
2725 struct ocfs2_xattr_tree_root *xr;
2726 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2727 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2729 mlog(0, "create xattr index block for %llu\n",
2730 (unsigned long long)xb_bh->b_blocknr);
2732 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2734 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2742 * We can use this lock for now, and maybe move to a dedicated mutex
2743 * if performance becomes a problem later.
2745 down_write(&oi->ip_alloc_sem);
2748 * 3 more credits, one for xattr block update, one for the 1st block
2749 * of the new xattr bucket and one for the value/data.
2752 handle = ocfs2_start_trans(osb, credits);
2753 if (IS_ERR(handle)) {
2754 ret = PTR_ERR(handle);
2759 ret = ocfs2_journal_access(handle, inode, xb_bh,
2760 OCFS2_JOURNAL_ACCESS_WRITE);
2766 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2773 * The bucket may spread in many blocks, and
2774 * we will only touch the 1st block and the last block
2775 * in the whole bucket(one for entry and one for data).
2777 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2779 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2781 xh_bh = sb_getblk(inode->i_sb, blkno);
2788 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2790 ret = ocfs2_journal_access(handle, inode, xh_bh,
2791 OCFS2_JOURNAL_ACCESS_CREATE);
2798 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2805 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2807 ret = ocfs2_journal_access(handle, inode, data_bh,
2808 OCFS2_JOURNAL_ACCESS_CREATE);
2815 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2817 ocfs2_journal_dirty(handle, xh_bh);
2819 ocfs2_journal_dirty(handle, data_bh);
2821 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2823 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2824 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2825 offsetof(struct ocfs2_xattr_block, xb_attrs));
2827 xr = &xb->xb_attrs.xb_root;
2828 xr->xt_clusters = cpu_to_le32(1);
2829 xr->xt_last_eb_blk = 0;
2830 xr->xt_list.l_tree_depth = 0;
2831 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2832 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2834 xr->xt_list.l_recs[0].e_cpos = 0;
2835 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2836 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2838 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2840 ret = ocfs2_journal_dirty(handle, xb_bh);
2847 ocfs2_commit_trans(osb, handle);
2850 up_write(&oi->ip_alloc_sem);
2854 ocfs2_free_alloc_context(data_ac);
2862 static int cmp_xe_offset(const void *a, const void *b)
2864 const struct ocfs2_xattr_entry *l = a, *r = b;
2865 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2866 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2868 if (l_name_offset < r_name_offset)
2870 if (l_name_offset > r_name_offset)
2876 * defrag a xattr bucket if we find that the bucket has some
2877 * holes beteen name/value pairs.
2878 * We will move all the name/value pairs to the end of the bucket
2879 * so that we can spare some space for insertion.
2881 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2882 struct ocfs2_xattr_bucket *bucket)
2885 size_t end, offset, len, value_len;
2886 struct ocfs2_xattr_header *xh;
2887 char *entries, *buf, *bucket_buf = NULL;
2888 u64 blkno = bucket->bhs[0]->b_blocknr;
2889 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2891 size_t blocksize = inode->i_sb->s_blocksize;
2893 struct buffer_head **bhs;
2894 struct ocfs2_xattr_entry *xe;
2896 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2901 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket, bhs, 0);
2906 * In order to make the operation more efficient and generic,
2907 * we copy all the blocks into a contiguous memory and do the
2908 * defragment there, so if anything is error, we will not touch
2911 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2918 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2919 memcpy(buf, bhs[i]->b_data, blocksize);
2921 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2922 if (IS_ERR(handle)) {
2923 ret = PTR_ERR(handle);
2929 for (i = 0; i < blk_per_bucket; i++) {
2930 ret = ocfs2_journal_access(handle, inode, bhs[i],
2931 OCFS2_JOURNAL_ACCESS_WRITE);
2938 xh = (struct ocfs2_xattr_header *)bucket_buf;
2939 entries = (char *)xh->xh_entries;
2940 xh_free_start = le16_to_cpu(xh->xh_free_start);
2942 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2943 "xh_free_start = %u, xh_name_value_len = %u.\n",
2944 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2945 le16_to_cpu(xh->xh_name_value_len));
2948 * sort all the entries by their offset.
2949 * the largest will be the first, so that we can
2950 * move them to the end one by one.
2952 sort(entries, le16_to_cpu(xh->xh_count),
2953 sizeof(struct ocfs2_xattr_entry),
2954 cmp_xe_offset, swap_xe);
2956 /* Move all name/values to the end of the bucket. */
2957 xe = xh->xh_entries;
2958 end = OCFS2_XATTR_BUCKET_SIZE;
2959 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2960 offset = le16_to_cpu(xe->xe_name_offset);
2961 if (ocfs2_xattr_is_local(xe))
2962 value_len = OCFS2_XATTR_SIZE(
2963 le64_to_cpu(xe->xe_value_size));
2965 value_len = OCFS2_XATTR_ROOT_SIZE;
2966 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2969 * We must make sure that the name/value pair
2970 * exist in the same block. So adjust end to
2971 * the previous block end if needed.
2973 if (((end - len) / blocksize !=
2974 (end - 1) / blocksize))
2975 end = end - end % blocksize;
2977 if (end > offset + len) {
2978 memmove(bucket_buf + end - len,
2979 bucket_buf + offset, len);
2980 xe->xe_name_offset = cpu_to_le16(end - len);
2983 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2984 "bucket %llu\n", (unsigned long long)blkno);
2989 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2990 "bucket %llu\n", (unsigned long long)blkno);
2992 if (xh_free_start == end)
2995 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
2996 xh->xh_free_start = cpu_to_le16(end);
2998 /* sort the entries by their name_hash. */
2999 sort(entries, le16_to_cpu(xh->xh_count),
3000 sizeof(struct ocfs2_xattr_entry),
3004 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
3005 memcpy(bhs[i]->b_data, buf, blocksize);
3006 ocfs2_journal_dirty(handle, bhs[i]);
3010 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3014 for (i = 0; i < blk_per_bucket; i++)
3024 * Move half nums of the xattr bucket in the previous cluster to this new
3025 * cluster. We only touch the last cluster of the previous extend record.
3027 * first_bh is the first buffer_head of a series of bucket in the same
3028 * extent rec and header_bh is the header of one bucket in this cluster.
3029 * They will be updated if we move the data header_bh contains to the new
3030 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3032 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3034 struct buffer_head **first_bh,
3035 struct buffer_head **header_bh,
3041 int i, ret, credits;
3042 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3043 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3044 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3045 int blocksize = inode->i_sb->s_blocksize;
3046 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3047 struct ocfs2_xattr_header *new_xh;
3048 struct ocfs2_xattr_header *xh =
3049 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3051 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3052 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3054 prev_bh = *first_bh;
3056 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3058 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3060 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3061 prev_blkno, new_blkno);
3064 * We need to update the 1st half of the new cluster and
3065 * 1 more for the update of the 1st bucket of the previous
3068 credits = bpc / 2 + 1;
3069 ret = ocfs2_extend_trans(handle, credits);
3075 ret = ocfs2_journal_access(handle, inode, prev_bh,
3076 OCFS2_JOURNAL_ACCESS_WRITE);
3082 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3083 old_bh = new_bh = NULL;
3084 new_bh = sb_getblk(inode->i_sb, new_blkno);
3091 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3093 ret = ocfs2_journal_access(handle, inode, new_bh,
3094 OCFS2_JOURNAL_ACCESS_CREATE);
3101 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
3108 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3111 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3112 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3115 *first_hash = le32_to_cpu(
3116 new_xh->xh_entries[0].xe_name_hash);
3117 new_first_bh = new_bh;
3118 get_bh(new_first_bh);
3121 ocfs2_journal_dirty(handle, new_bh);
3123 if (*header_bh == old_bh) {
3125 *header_bh = new_bh;
3129 *first_bh = new_first_bh;
3136 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3138 ocfs2_journal_dirty(handle, prev_bh);
3141 brelse(new_first_bh);
3145 static int ocfs2_read_xattr_bucket(struct inode *inode,
3147 struct buffer_head **bhs,
3151 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3154 return ocfs2_read_blocks(inode, blkno,
3155 blk_per_bucket, bhs, 0);
3157 for (i = 0; i < blk_per_bucket; i++) {
3158 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3159 if (bhs[i] == NULL) {
3164 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3171 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3172 * first_hash will record the 1st hash of the new bucket.
3174 static int ocfs2_half_xattr_bucket(struct inode *inode,
3179 int new_bucket_head)
3182 u16 count, start, len, name_value_len, xe_len, name_offset;
3183 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3184 struct buffer_head **s_bhs, **t_bhs = NULL;
3185 struct ocfs2_xattr_header *xh;
3186 struct ocfs2_xattr_entry *xe;
3187 int blocksize = inode->i_sb->s_blocksize;
3189 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3192 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3196 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3202 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3203 OCFS2_JOURNAL_ACCESS_WRITE);
3209 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3215 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3221 for (i = 0; i < blk_per_bucket; i++) {
3222 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3223 OCFS2_JOURNAL_ACCESS_CREATE);
3230 /* copy the whole bucket to the new first. */
3231 for (i = 0; i < blk_per_bucket; i++)
3232 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3234 /* update the new bucket. */
3235 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3236 count = le16_to_cpu(xh->xh_count);
3240 * Calculate the total name/value len and xh_free_start for
3241 * the old bucket first.
3243 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3245 for (i = 0; i < start; i++) {
3246 xe = &xh->xh_entries[i];
3247 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3248 if (ocfs2_xattr_is_local(xe))
3250 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3252 xe_len += OCFS2_XATTR_ROOT_SIZE;
3253 name_value_len += xe_len;
3254 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3255 name_offset = le16_to_cpu(xe->xe_name_offset);
3259 * Now begin the modification to the new bucket.
3261 * In the new bucket, We just move the xattr entry to the beginning
3262 * and don't touch the name/value. So there will be some holes in the
3263 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3266 xe = &xh->xh_entries[start];
3267 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3268 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3269 (int)((char *)xe - (char *)xh),
3270 (int)((char *)xh->xh_entries - (char *)xh));
3271 memmove((char *)xh->xh_entries, (char *)xe, len);
3272 xe = &xh->xh_entries[count - start];
3273 len = sizeof(struct ocfs2_xattr_entry) * start;
3274 memset((char *)xe, 0, len);
3276 le16_add_cpu(&xh->xh_count, -start);
3277 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3279 /* Calculate xh_free_start for the new bucket. */
3280 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3281 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3282 xe = &xh->xh_entries[i];
3283 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3284 if (ocfs2_xattr_is_local(xe))
3286 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3288 xe_len += OCFS2_XATTR_ROOT_SIZE;
3289 if (le16_to_cpu(xe->xe_name_offset) <
3290 le16_to_cpu(xh->xh_free_start))
3291 xh->xh_free_start = xe->xe_name_offset;
3294 /* set xh->xh_num_buckets for the new xh. */
3295 if (new_bucket_head)
3296 xh->xh_num_buckets = cpu_to_le16(1);
3298 xh->xh_num_buckets = 0;
3300 for (i = 0; i < blk_per_bucket; i++) {
3301 ocfs2_journal_dirty(handle, t_bhs[i]);
3306 /* store the first_hash of the new bucket. */
3308 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3311 * Now only update the 1st block of the old bucket.
3312 * Please note that the entry has been sorted already above.
3314 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3315 memset(&xh->xh_entries[start], 0,
3316 sizeof(struct ocfs2_xattr_entry) * (count - start));
3317 xh->xh_count = cpu_to_le16(start);
3318 xh->xh_free_start = cpu_to_le16(name_offset);
3319 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3321 ocfs2_journal_dirty(handle, s_bhs[0]);
3327 for (i = 0; i < blk_per_bucket; i++)
3333 for (i = 0; i < blk_per_bucket; i++)
3342 * Copy xattr from one bucket to another bucket.
3344 * The caller must make sure that the journal transaction
3345 * has enough space for journaling.
3347 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3354 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3355 int blocksize = inode->i_sb->s_blocksize;
3356 struct buffer_head **s_bhs, **t_bhs = NULL;
3358 BUG_ON(s_blkno == t_blkno);
3360 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3361 s_blkno, t_blkno, t_is_new);
3363 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3368 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3372 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3379 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3383 for (i = 0; i < blk_per_bucket; i++) {
3384 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3385 OCFS2_JOURNAL_ACCESS_WRITE);
3390 for (i = 0; i < blk_per_bucket; i++) {
3391 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3392 ocfs2_journal_dirty(handle, t_bhs[i]);
3397 for (i = 0; i < blk_per_bucket; i++)
3403 for (i = 0; i < blk_per_bucket; i++)
3412 * Copy one xattr cluster from src_blk to to_blk.
3413 * The to_blk will become the first bucket header of the cluster, so its
3414 * xh_num_buckets will be initialized as the bucket num in the cluster.
3416 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3418 struct buffer_head *first_bh,
3423 int i, ret, credits;
3424 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3425 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3426 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3427 struct buffer_head *bh = NULL;
3428 struct ocfs2_xattr_header *xh;
3429 u64 to_blk_start = to_blk;
3431 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3434 * We need to update the new cluster and 1 more for the update of
3435 * the 1st bucket of the previous extent rec.
3438 ret = ocfs2_extend_trans(handle, credits);
3444 ret = ocfs2_journal_access(handle, inode, first_bh,
3445 OCFS2_JOURNAL_ACCESS_WRITE);
3451 for (i = 0; i < num_buckets; i++) {
3452 ret = ocfs2_cp_xattr_bucket(inode, handle,
3453 src_blk, to_blk, 1);
3459 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3460 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3463 /* update the old bucket header. */
3464 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3465 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3467 ocfs2_journal_dirty(handle, first_bh);
3469 /* update the new bucket header. */
3470 ret = ocfs2_read_block(inode, to_blk_start, &bh);
3476 ret = ocfs2_journal_access(handle, inode, bh,
3477 OCFS2_JOURNAL_ACCESS_WRITE);
3483 xh = (struct ocfs2_xattr_header *)bh->b_data;
3484 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3486 ocfs2_journal_dirty(handle, bh);
3489 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3496 * Move half of the xattrs in this cluster to the new cluster.
3497 * This function should only be called when bucket size == cluster size.
3498 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3500 static int ocfs2_half_xattr_cluster(struct inode *inode,
3506 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3507 int ret, credits = 2 * blk_per_bucket;
3509 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3511 ret = ocfs2_extend_trans(handle, credits);
3517 /* Move half of the xattr in start_blk to the next bucket. */
3518 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3519 new_blk, first_hash, 1);
3523 * Move some xattrs from the old cluster to the new one since they are not
3524 * contiguous in ocfs2 xattr tree.
3526 * new_blk starts a new separate cluster, and we will move some xattrs from
3527 * prev_blk to it. v_start will be set as the first name hash value in this
3528 * new cluster so that it can be used as e_cpos during tree insertion and
3529 * don't collide with our original b-tree operations. first_bh and header_bh
3530 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3531 * to extend the insert bucket.
3533 * The problem is how much xattr should we move to the new one and when should
3534 * we update first_bh and header_bh?
3535 * 1. If cluster size > bucket size, that means the previous cluster has more
3536 * than 1 bucket, so just move half nums of bucket into the new cluster and
3537 * update the first_bh and header_bh if the insert bucket has been moved
3538 * to the new cluster.
3539 * 2. If cluster_size == bucket_size:
3540 * a) If the previous extent rec has more than one cluster and the insert
3541 * place isn't in the last cluster, copy the entire last cluster to the
3542 * new one. This time, we don't need to upate the first_bh and header_bh
3543 * since they will not be moved into the new cluster.
3544 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3545 * the new one. And we set the extend flag to zero if the insert place is
3546 * moved into the new allocated cluster since no extend is needed.
3548 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3550 struct buffer_head **first_bh,
3551 struct buffer_head **header_bh,
3559 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3561 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3562 prev_blk, prev_clusters, new_blk);
3564 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3565 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3574 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3576 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3577 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3581 ret = ocfs2_half_xattr_cluster(inode, handle,
3585 if ((*header_bh)->b_blocknr == last_blk && extend)
3594 * Add a new cluster for xattr storage.
3596 * If the new cluster is contiguous with the previous one, it will be
3597 * appended to the same extent record, and num_clusters will be updated.
3598 * If not, we will insert a new extent for it and move some xattrs in
3599 * the last cluster into the new allocated one.
3600 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3601 * lose the benefits of hashing because we'll have to search large leaves.
3602 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3605 * first_bh is the first block of the previous extent rec and header_bh
3606 * indicates the bucket we will insert the new xattrs. They will be updated
3607 * when the header_bh is moved into the new cluster.
3609 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3610 struct buffer_head *root_bh,
3611 struct buffer_head **first_bh,
3612 struct buffer_head **header_bh,
3619 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3620 u32 prev_clusters = *num_clusters;
3621 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3623 handle_t *handle = NULL;
3624 struct ocfs2_alloc_context *data_ac = NULL;
3625 struct ocfs2_alloc_context *meta_ac = NULL;
3626 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3627 struct ocfs2_extent_tree et;
3629 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3630 "previous xattr blkno = %llu\n",
3631 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3632 prev_cpos, prev_blkno);
3634 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
3636 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3637 &data_ac, &meta_ac);
3643 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3645 handle = ocfs2_start_trans(osb, credits);
3646 if (IS_ERR(handle)) {
3647 ret = PTR_ERR(handle);
3653 ret = ocfs2_journal_access(handle, inode, root_bh,
3654 OCFS2_JOURNAL_ACCESS_WRITE);
3660 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3661 clusters_to_add, &bit_off, &num_bits);
3668 BUG_ON(num_bits > clusters_to_add);
3670 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3671 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3672 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3674 if (prev_blkno + prev_clusters * bpc == block &&
3675 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3676 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3678 * If this cluster is contiguous with the old one and
3679 * adding this new cluster, we don't surpass the limit of
3680 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3681 * initialized and used like other buckets in the previous
3683 * So add it as a contiguous one. The caller will handle
3686 v_start = prev_cpos + prev_clusters;
3687 *num_clusters = prev_clusters + num_bits;
3688 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3691 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3706 if (handle->h_buffer_credits < credits) {
3708 * The journal has been restarted before, and don't
3709 * have enough space for the insertion, so extend it
3712 ret = ocfs2_extend_trans(handle, credits);
3718 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3719 num_bits, block, v_start);
3720 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3721 num_bits, 0, meta_ac);
3727 ret = ocfs2_journal_dirty(handle, root_bh);
3735 ocfs2_commit_trans(osb, handle);
3737 ocfs2_free_alloc_context(data_ac);
3739 ocfs2_free_alloc_context(meta_ac);
3745 * Extend a new xattr bucket and move xattrs to the end one by one until
3746 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3748 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3749 struct buffer_head *first_bh,
3750 struct buffer_head *start_bh,
3754 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3755 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3756 u64 start_blk = start_bh->b_blocknr, end_blk;
3757 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3759 struct ocfs2_xattr_header *first_xh =
3760 (struct ocfs2_xattr_header *)first_bh->b_data;
3761 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3763 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3764 "from %llu, len = %u\n", start_blk,
3765 (unsigned long long)first_bh->b_blocknr, num_clusters);
3767 BUG_ON(bucket >= num_buckets);
3769 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3772 * We will touch all the buckets after the start_bh(include it).
3773 * Add one more bucket and modify the first_bh.
3775 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3776 handle = ocfs2_start_trans(osb, credits);
3777 if (IS_ERR(handle)) {
3778 ret = PTR_ERR(handle);
3784 ret = ocfs2_journal_access(handle, inode, first_bh,
3785 OCFS2_JOURNAL_ACCESS_WRITE);
3791 while (end_blk != start_blk) {
3792 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3793 end_blk + blk_per_bucket, 0);
3796 end_blk -= blk_per_bucket;
3799 /* Move half of the xattr in start_blk to the next bucket. */
3800 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3801 start_blk + blk_per_bucket, NULL, 0);
3803 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3804 ocfs2_journal_dirty(handle, first_bh);
3807 ocfs2_commit_trans(osb, handle);
3813 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3814 * xb_bh is the ocfs2_xattr_block.
3815 * We will move all the buckets starting from header_bh to the next place. As
3816 * for this one, half num of its xattrs will be moved to the next one.
3818 * We will allocate a new cluster if current cluster is full and adjust
3819 * header_bh and first_bh if the insert place is moved to the new cluster.
3821 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3822 struct buffer_head *xb_bh,
3823 struct buffer_head *header_bh)
3825 struct ocfs2_xattr_header *first_xh = NULL;
3826 struct buffer_head *first_bh = NULL;
3827 struct ocfs2_xattr_block *xb =
3828 (struct ocfs2_xattr_block *)xb_bh->b_data;
3829 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3830 struct ocfs2_extent_list *el = &xb_root->xt_list;
3831 struct ocfs2_xattr_header *xh =
3832 (struct ocfs2_xattr_header *)header_bh->b_data;
3833 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3834 struct super_block *sb = inode->i_sb;
3835 struct ocfs2_super *osb = OCFS2_SB(sb);
3836 int ret, num_buckets, extend = 1;
3838 u32 e_cpos, num_clusters;
3840 mlog(0, "Add new xattr bucket starting form %llu\n",
3841 (unsigned long long)header_bh->b_blocknr);
3844 * Add refrence for header_bh here because it may be
3845 * changed in ocfs2_add_new_xattr_cluster and we need
3846 * to free it in the end.
3850 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3857 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
3863 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3864 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3866 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3867 ret = ocfs2_add_new_xattr_cluster(inode,
3882 ret = ocfs2_extend_xattr_bucket(inode,
3894 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3895 struct ocfs2_xattr_bucket *bucket,
3898 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3900 offs = offs % inode->i_sb->s_blocksize;
3901 return bucket->bhs[block_off]->b_data + offs;
3905 * Handle the normal xattr set, including replace, delete and new.
3907 * Note: "local" indicates the real data's locality. So we can't
3908 * just its bucket locality by its length.
3910 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3911 struct ocfs2_xattr_info *xi,
3912 struct ocfs2_xattr_search *xs,
3916 struct ocfs2_xattr_entry *last, *xe;
3917 int name_len = strlen(xi->name);
3918 struct ocfs2_xattr_header *xh = xs->header;
3919 u16 count = le16_to_cpu(xh->xh_count), start;
3920 size_t blocksize = inode->i_sb->s_blocksize;
3922 size_t offs, size, new_size;
3924 last = &xh->xh_entries[count];
3925 if (!xs->not_found) {
3927 offs = le16_to_cpu(xe->xe_name_offset);
3928 if (ocfs2_xattr_is_local(xe))
3929 size = OCFS2_XATTR_SIZE(name_len) +
3930 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3932 size = OCFS2_XATTR_SIZE(name_len) +
3933 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3936 * If the new value will be stored outside, xi->value has been
3937 * initalized as an empty ocfs2_xattr_value_root, and the same
3938 * goes with xi->value_len, so we can set new_size safely here.
3939 * See ocfs2_xattr_set_in_bucket.
3941 new_size = OCFS2_XATTR_SIZE(name_len) +
3942 OCFS2_XATTR_SIZE(xi->value_len);
3944 le16_add_cpu(&xh->xh_name_value_len, -size);
3946 if (new_size > size)
3947 goto set_new_name_value;
3949 /* Now replace the old value with new one. */
3951 xe->xe_value_size = cpu_to_le64(xi->value_len);
3953 xe->xe_value_size = 0;
3955 val = ocfs2_xattr_bucket_get_val(inode,
3957 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3958 size - OCFS2_XATTR_SIZE(name_len));
3959 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3960 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3961 xi->value, xi->value_len);
3963 le16_add_cpu(&xh->xh_name_value_len, new_size);
3964 ocfs2_xattr_set_local(xe, local);
3968 * Remove the old entry if there is more than one.
3969 * We don't remove the last entry so that we can
3970 * use it to indicate the hash value of the empty
3974 le16_add_cpu(&xh->xh_count, -1);
3977 (void *)last - (void *)xe);
3979 sizeof(struct ocfs2_xattr_entry));
3982 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3987 /* find a new entry for insert. */
3988 int low = 0, high = count - 1, tmp;
3989 struct ocfs2_xattr_entry *tmp_xe;
3991 while (low <= high && count) {
3992 tmp = (low + high) / 2;
3993 tmp_xe = &xh->xh_entries[tmp];
3995 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
3997 else if (name_hash <
3998 le32_to_cpu(tmp_xe->xe_name_hash))
4006 xe = &xh->xh_entries[low];
4008 memmove(xe + 1, xe, (void *)last - (void *)xe);
4010 le16_add_cpu(&xh->xh_count, 1);
4011 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4012 xe->xe_name_hash = cpu_to_le32(name_hash);
4013 xe->xe_name_len = name_len;
4014 ocfs2_xattr_set_type(xe, xi->name_index);
4018 /* Insert the new name+value. */
4019 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4022 * We must make sure that the name/value pair
4023 * exists in the same block.
4025 offs = le16_to_cpu(xh->xh_free_start);
4026 start = offs - size;
4028 if (start >> inode->i_sb->s_blocksize_bits !=
4029 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4030 offs = offs - offs % blocksize;
4031 xh->xh_free_start = cpu_to_le16(offs);
4034 val = ocfs2_xattr_bucket_get_val(inode,
4035 &xs->bucket, offs - size);
4036 xe->xe_name_offset = cpu_to_le16(offs - size);
4038 memset(val, 0, size);
4039 memcpy(val, xi->name, name_len);
4040 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4042 xe->xe_value_size = cpu_to_le64(xi->value_len);
4043 ocfs2_xattr_set_local(xe, local);
4045 le16_add_cpu(&xh->xh_free_start, -size);
4046 le16_add_cpu(&xh->xh_name_value_len, size);
4051 static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4053 struct ocfs2_xattr_search *xs,
4054 struct buffer_head **bhs,
4057 int ret = 0, off, block_off;
4058 struct ocfs2_xattr_entry *xe = xs->here;
4061 * First calculate all the blocks we should journal_access
4062 * and journal_dirty. The first block should always be touched.
4064 ret = ocfs2_journal_dirty(handle, bhs[0]);
4068 /* calc the data. */
4069 off = le16_to_cpu(xe->xe_name_offset);
4070 block_off = off >> inode->i_sb->s_blocksize_bits;
4071 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4079 * Set the xattr entry in the specified bucket.
4080 * The bucket is indicated by xs->bucket and it should have the enough
4081 * space for the xattr insertion.
4083 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4084 struct ocfs2_xattr_info *xi,
4085 struct ocfs2_xattr_search *xs,
4090 handle_t *handle = NULL;
4091 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4092 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4094 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4095 (unsigned long)xi->value_len, xi->name_index,
4096 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4098 if (!xs->bucket.bhs[1]) {
4099 ret = ocfs2_read_blocks(inode,
4100 xs->bucket.bhs[0]->b_blocknr + 1,
4101 blk_per_bucket - 1, &xs->bucket.bhs[1],
4109 handle = ocfs2_start_trans(osb, blk_per_bucket);
4110 if (IS_ERR(handle)) {
4111 ret = PTR_ERR(handle);
4117 for (i = 0; i < blk_per_bucket; i++) {
4118 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4119 OCFS2_JOURNAL_ACCESS_WRITE);
4126 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4128 /*Only dirty the blocks we have touched in set xattr. */
4129 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4130 xs->bucket.bhs, blk_per_bucket);
4134 ocfs2_commit_trans(osb, handle);
4139 static int ocfs2_xattr_value_update_size(struct inode *inode,
4140 struct buffer_head *xe_bh,
4141 struct ocfs2_xattr_entry *xe,
4145 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4146 handle_t *handle = NULL;
4148 handle = ocfs2_start_trans(osb, 1);
4149 if (handle == NULL) {
4155 ret = ocfs2_journal_access(handle, inode, xe_bh,
4156 OCFS2_JOURNAL_ACCESS_WRITE);
4162 xe->xe_value_size = cpu_to_le64(new_size);
4164 ret = ocfs2_journal_dirty(handle, xe_bh);
4169 ocfs2_commit_trans(osb, handle);
4175 * Truncate the specified xe_off entry in xattr bucket.
4176 * bucket is indicated by header_bh and len is the new length.
4177 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4179 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4181 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4182 struct buffer_head *header_bh,
4188 struct buffer_head *value_bh = NULL;
4189 struct ocfs2_xattr_value_root *xv;
4190 struct ocfs2_xattr_entry *xe;
4191 struct ocfs2_xattr_header *xh =
4192 (struct ocfs2_xattr_header *)header_bh->b_data;
4193 size_t blocksize = inode->i_sb->s_blocksize;
4195 xe = &xh->xh_entries[xe_off];
4197 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4199 offset = le16_to_cpu(xe->xe_name_offset) +
4200 OCFS2_XATTR_SIZE(xe->xe_name_len);
4202 value_blk = offset / blocksize;
4204 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4205 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4206 value_blk += header_bh->b_blocknr;
4208 ret = ocfs2_read_block(inode, value_blk, &value_bh);
4214 xv = (struct ocfs2_xattr_value_root *)
4215 (value_bh->b_data + offset % blocksize);
4217 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4218 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4219 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4225 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4236 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4237 struct ocfs2_xattr_search *xs,
4241 struct ocfs2_xattr_entry *xe = xs->here;
4242 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4244 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4246 offset = xe - xh->xh_entries;
4247 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4255 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4256 struct ocfs2_xattr_search *xs,
4261 struct ocfs2_xattr_value_root *xv;
4262 struct ocfs2_xattr_entry *xe = xs->here;
4264 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4266 offset = le16_to_cpu(xe->xe_name_offset) +
4267 OCFS2_XATTR_SIZE(xe->xe_name_len);
4269 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4271 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4274 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4275 struct buffer_head *root_bh,
4281 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4282 struct inode *tl_inode = osb->osb_tl_inode;
4284 struct ocfs2_xattr_block *xb =
4285 (struct ocfs2_xattr_block *)root_bh->b_data;
4286 struct ocfs2_alloc_context *meta_ac = NULL;
4287 struct ocfs2_cached_dealloc_ctxt dealloc;
4288 struct ocfs2_extent_tree et;
4290 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4292 ocfs2_init_dealloc_ctxt(&dealloc);
4294 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4295 cpos, len, (unsigned long long)blkno);
4297 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4299 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4305 mutex_lock(&tl_inode->i_mutex);
4307 if (ocfs2_truncate_log_needs_flush(osb)) {
4308 ret = __ocfs2_flush_truncate_log(osb);
4315 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4316 if (handle == NULL) {
4322 ret = ocfs2_journal_access(handle, inode, root_bh,
4323 OCFS2_JOURNAL_ACCESS_WRITE);
4329 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4336 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4338 ret = ocfs2_journal_dirty(handle, root_bh);
4344 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4349 ocfs2_commit_trans(osb, handle);
4351 ocfs2_schedule_truncate_log_flush(osb, 1);
4353 mutex_unlock(&tl_inode->i_mutex);
4356 ocfs2_free_alloc_context(meta_ac);
4358 ocfs2_run_deallocs(osb, &dealloc);
4363 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4364 struct ocfs2_xattr_search *xs)
4366 handle_t *handle = NULL;
4367 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4368 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4369 le16_to_cpu(xh->xh_count) - 1];
4372 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4373 if (IS_ERR(handle)) {
4374 ret = PTR_ERR(handle);
4379 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4380 OCFS2_JOURNAL_ACCESS_WRITE);
4386 /* Remove the old entry. */
4387 memmove(xs->here, xs->here + 1,
4388 (void *)last - (void *)xs->here);
4389 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4390 le16_add_cpu(&xh->xh_count, -1);
4392 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4396 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4400 * Set the xattr name/value in the bucket specified in xs.
4402 * As the new value in xi may be stored in the bucket or in an outside cluster,
4403 * we divide the whole process into 3 steps:
4404 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4405 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4406 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4407 * 4. If the clusters for the new outside value can't be allocated, we need
4408 * to free the xattr we allocated in set.
4410 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4411 struct ocfs2_xattr_info *xi,
4412 struct ocfs2_xattr_search *xs)
4416 char *val = (char *)xi->value;
4417 struct ocfs2_xattr_entry *xe = xs->here;
4418 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4421 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4423 * We need to truncate the xattr storage first.
4425 * If both the old and new value are stored to
4426 * outside block, we only need to truncate
4427 * the storage and then set the value outside.
4429 * If the new value should be stored within block,
4430 * we should free all the outside block first and
4431 * the modification to the xattr block will be done
4432 * by following steps.
4434 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4435 value_len = xi->value_len;
4439 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4445 goto set_value_outside;
4448 value_len = xi->value_len;
4449 /* So we have to handle the inside block change now. */
4450 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4452 * If the new value will be stored outside of block,
4453 * initalize a new empty value root and insert it first.
4456 xi->value = &def_xv;
4457 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4460 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
4466 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4469 /* allocate the space now for the outside block storage. */
4470 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4475 if (xs->not_found) {
4477 * We can't allocate enough clusters for outside
4478 * storage and we have allocated xattr already,
4479 * so need to remove it.
4481 ocfs2_xattr_bucket_remove_xs(inode, xs);
4487 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4492 /* check whether the xattr bucket is filled up with the same hash value. */
4493 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4494 struct ocfs2_xattr_bucket *bucket)
4496 struct ocfs2_xattr_header *xh = bucket->xh;
4498 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4499 xh->xh_entries[0].xe_name_hash) {
4500 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4502 (unsigned long long)bucket->bhs[0]->b_blocknr,
4503 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4510 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4511 struct ocfs2_xattr_info *xi,
4512 struct ocfs2_xattr_search *xs)
4514 struct ocfs2_xattr_header *xh;
4515 struct ocfs2_xattr_entry *xe;
4516 u16 count, header_size, xh_free_start;
4517 int i, free, max_free, need, old;
4518 size_t value_size = 0, name_len = strlen(xi->name);
4519 size_t blocksize = inode->i_sb->s_blocksize;
4520 int ret, allocation = 0;
4521 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4523 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4527 count = le16_to_cpu(xh->xh_count);
4528 xh_free_start = le16_to_cpu(xh->xh_free_start);
4529 header_size = sizeof(struct ocfs2_xattr_header) +
4530 count * sizeof(struct ocfs2_xattr_entry);
4531 max_free = OCFS2_XATTR_BUCKET_SIZE -
4532 le16_to_cpu(xh->xh_name_value_len) - header_size;
4534 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4535 "of %u which exceed block size\n",
4536 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4539 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4540 value_size = OCFS2_XATTR_ROOT_SIZE;
4542 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4545 need = sizeof(struct ocfs2_xattr_entry) +
4546 OCFS2_XATTR_SIZE(name_len) + value_size;
4548 need = value_size + OCFS2_XATTR_SIZE(name_len);
4551 * We only replace the old value if the new length is smaller
4552 * than the old one. Otherwise we will allocate new space in the
4553 * bucket to store it.
4556 if (ocfs2_xattr_is_local(xe))
4557 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4559 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4561 if (old >= value_size)
4565 free = xh_free_start - header_size;
4567 * We need to make sure the new name/value pair
4568 * can exist in the same block.
4570 if (xh_free_start % blocksize < need)
4571 free -= xh_free_start % blocksize;
4573 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4574 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4575 " %u\n", xs->not_found,
4576 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4577 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4578 le16_to_cpu(xh->xh_name_value_len));
4580 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4581 if (need <= max_free &&
4582 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4584 * We can create the space by defragment. Since only the
4585 * name/value will be moved, the xe shouldn't be changed
4588 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4594 xh_free_start = le16_to_cpu(xh->xh_free_start);
4595 free = xh_free_start - header_size;
4596 if (xh_free_start % blocksize < need)
4597 free -= xh_free_start % blocksize;
4602 mlog(0, "Can't get enough space for xattr insert by "
4603 "defragment. Need %u bytes, but we have %d, so "
4604 "allocate new bucket for it.\n", need, free);
4608 * We have to add new buckets or clusters and one
4609 * allocation should leave us enough space for insert.
4614 * We do not allow for overlapping ranges between buckets. And
4615 * the maximum number of collisions we will allow for then is
4616 * one bucket's worth, so check it here whether we need to
4617 * add a new bucket for the insert.
4619 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4625 ret = ocfs2_add_new_xattr_bucket(inode,
4633 for (i = 0; i < blk_per_bucket; i++)
4634 brelse(xs->bucket.bhs[i]);
4636 memset(&xs->bucket, 0, sizeof(xs->bucket));
4638 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4641 if (ret && ret != -ENODATA)
4643 xs->not_found = ret;
4649 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4655 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4656 struct ocfs2_xattr_bucket *bucket,
4660 struct ocfs2_xattr_header *xh = bucket->xh;
4662 struct ocfs2_xattr_entry *xe;
4664 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4665 xe = &xh->xh_entries[i];
4666 if (ocfs2_xattr_is_local(xe))
4669 ret = ocfs2_xattr_bucket_value_truncate(inode,
4681 static int ocfs2_delete_xattr_index_block(struct inode *inode,
4682 struct buffer_head *xb_bh)
4684 struct ocfs2_xattr_block *xb =
4685 (struct ocfs2_xattr_block *)xb_bh->b_data;
4686 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4688 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4691 if (le16_to_cpu(el->l_next_free_rec) == 0)
4694 while (name_hash > 0) {
4695 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4696 &e_cpos, &num_clusters, el);
4702 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4703 ocfs2_delete_xattr_in_bucket,
4710 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4711 p_blkno, e_cpos, num_clusters);
4720 name_hash = e_cpos - 1;
4728 * 'trusted' attributes support
4731 #define XATTR_TRUSTED_PREFIX "trusted."
4733 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4734 size_t list_size, const char *name,
4737 const size_t prefix_len = sizeof(XATTR_TRUSTED_PREFIX) - 1;
4738 const size_t total_len = prefix_len + name_len + 1;
4740 if (list && total_len <= list_size) {
4741 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4742 memcpy(list + prefix_len, name, name_len);
4743 list[prefix_len + name_len] = '\0';
4748 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4749 void *buffer, size_t size)
4751 if (strcmp(name, "") == 0)
4753 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4757 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4758 const void *value, size_t size, int flags)
4760 if (strcmp(name, "") == 0)
4763 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4767 struct xattr_handler ocfs2_xattr_trusted_handler = {
4768 .prefix = XATTR_TRUSTED_PREFIX,
4769 .list = ocfs2_xattr_trusted_list,
4770 .get = ocfs2_xattr_trusted_get,
4771 .set = ocfs2_xattr_trusted_set,
4776 * 'user' attributes support
4779 #define XATTR_USER_PREFIX "user."
4781 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4782 size_t list_size, const char *name,
4785 const size_t prefix_len = sizeof(XATTR_USER_PREFIX) - 1;
4786 const size_t total_len = prefix_len + name_len + 1;
4787 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4789 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4792 if (list && total_len <= list_size) {
4793 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4794 memcpy(list + prefix_len, name, name_len);
4795 list[prefix_len + name_len] = '\0';
4800 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4801 void *buffer, size_t size)
4803 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4805 if (strcmp(name, "") == 0)
4807 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4809 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4813 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4814 const void *value, size_t size, int flags)
4816 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4818 if (strcmp(name, "") == 0)
4820 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4823 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4827 struct xattr_handler ocfs2_xattr_user_handler = {
4828 .prefix = XATTR_USER_PREFIX,
4829 .list = ocfs2_xattr_user_list,
4830 .get = ocfs2_xattr_user_get,
4831 .set = ocfs2_xattr_user_set,