1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
9 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
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.
22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
39 #define MLOG_MASK_PREFIX ML_XATTR
40 #include <cluster/masklog.h>
53 #include "buffer_head_io.h"
58 struct ocfs2_xattr_def_value_root {
59 struct ocfs2_xattr_value_root xv;
60 struct ocfs2_extent_rec er;
63 struct ocfs2_xattr_bucket {
64 struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
65 struct ocfs2_xattr_header *xh;
68 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
69 #define OCFS2_XATTR_INLINE_SIZE 80
71 static struct ocfs2_xattr_def_value_root def_xv = {
72 .xv.xr_list.l_count = cpu_to_le16(1),
75 struct xattr_handler *ocfs2_xattr_handlers[] = {
76 &ocfs2_xattr_user_handler,
77 &ocfs2_xattr_trusted_handler,
81 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
82 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
83 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
86 struct ocfs2_xattr_info {
93 struct ocfs2_xattr_search {
94 struct buffer_head *inode_bh;
96 * xattr_bh point to the block buffer head which has extended attribute
97 * when extended attribute in inode, xattr_bh is equal to inode_bh.
99 struct buffer_head *xattr_bh;
100 struct ocfs2_xattr_header *header;
101 struct ocfs2_xattr_bucket bucket;
104 struct ocfs2_xattr_entry *here;
108 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
109 struct ocfs2_xattr_header *xh,
114 static int ocfs2_xattr_index_block_find(struct inode *inode,
115 struct buffer_head *root_bh,
118 struct ocfs2_xattr_search *xs);
120 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
121 struct ocfs2_xattr_tree_root *xt,
125 static int ocfs2_xattr_create_index_block(struct inode *inode,
126 struct ocfs2_xattr_search *xs);
128 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
129 struct ocfs2_xattr_info *xi,
130 struct ocfs2_xattr_search *xs);
132 static int ocfs2_delete_xattr_index_block(struct inode *inode,
133 struct buffer_head *xb_bh);
135 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
137 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
140 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
142 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
145 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
147 u16 len = sb->s_blocksize -
148 offsetof(struct ocfs2_xattr_header, xh_entries);
150 return len / sizeof(struct ocfs2_xattr_entry);
153 static inline const char *ocfs2_xattr_prefix(int name_index)
155 struct xattr_handler *handler = NULL;
157 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
158 handler = ocfs2_xattr_handler_map[name_index];
160 return handler ? handler->prefix : NULL;
163 static u32 ocfs2_xattr_name_hash(struct inode *inode,
167 /* Get hash value of uuid from super block */
168 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
171 /* hash extended attribute name */
172 for (i = 0; i < name_len; i++) {
173 hash = (hash << OCFS2_HASH_SHIFT) ^
174 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
182 * ocfs2_xattr_hash_entry()
184 * Compute the hash of an extended attribute.
186 static void ocfs2_xattr_hash_entry(struct inode *inode,
187 struct ocfs2_xattr_header *header,
188 struct ocfs2_xattr_entry *entry)
191 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
193 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
194 entry->xe_name_hash = cpu_to_le32(hash);
199 static int ocfs2_xattr_extend_allocation(struct inode *inode,
201 struct buffer_head *xattr_bh,
202 struct ocfs2_xattr_value_root *xv)
205 int restart_func = 0;
207 handle_t *handle = NULL;
208 struct ocfs2_alloc_context *data_ac = NULL;
209 struct ocfs2_alloc_context *meta_ac = NULL;
210 enum ocfs2_alloc_restarted why;
211 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
212 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
213 struct ocfs2_extent_tree et;
215 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
217 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
221 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
228 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
230 handle = ocfs2_start_trans(osb, credits);
231 if (IS_ERR(handle)) {
232 status = PTR_ERR(handle);
238 restarted_transaction:
239 status = ocfs2_journal_access(handle, inode, xattr_bh,
240 OCFS2_JOURNAL_ACCESS_WRITE);
246 prev_clusters = le32_to_cpu(xv->xr_clusters);
247 status = ocfs2_add_clusters_in_btree(osb,
257 if ((status < 0) && (status != -EAGAIN)) {
258 if (status != -ENOSPC)
263 status = ocfs2_journal_dirty(handle, xattr_bh);
269 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
271 if (why != RESTART_NONE && clusters_to_add) {
272 if (why == RESTART_META) {
273 mlog(0, "restarting function.\n");
276 BUG_ON(why != RESTART_TRANS);
278 mlog(0, "restarting transaction.\n");
279 /* TODO: This can be more intelligent. */
280 credits = ocfs2_calc_extend_credits(osb->sb,
283 status = ocfs2_extend_trans(handle, credits);
285 /* handle still has to be committed at
291 goto restarted_transaction;
297 ocfs2_commit_trans(osb, handle);
301 ocfs2_free_alloc_context(data_ac);
305 ocfs2_free_alloc_context(meta_ac);
308 if ((!status) && restart_func) {
316 static int __ocfs2_remove_xattr_range(struct inode *inode,
317 struct buffer_head *root_bh,
318 struct ocfs2_xattr_value_root *xv,
319 u32 cpos, u32 phys_cpos, u32 len,
320 struct ocfs2_cached_dealloc_ctxt *dealloc)
323 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
324 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
325 struct inode *tl_inode = osb->osb_tl_inode;
327 struct ocfs2_alloc_context *meta_ac = NULL;
328 struct ocfs2_extent_tree et;
330 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
332 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
338 mutex_lock(&tl_inode->i_mutex);
340 if (ocfs2_truncate_log_needs_flush(osb)) {
341 ret = __ocfs2_flush_truncate_log(osb);
348 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
349 if (IS_ERR(handle)) {
350 ret = PTR_ERR(handle);
355 ret = ocfs2_journal_access(handle, inode, root_bh,
356 OCFS2_JOURNAL_ACCESS_WRITE);
362 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
369 le32_add_cpu(&xv->xr_clusters, -len);
371 ret = ocfs2_journal_dirty(handle, root_bh);
377 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
382 ocfs2_commit_trans(osb, handle);
384 mutex_unlock(&tl_inode->i_mutex);
387 ocfs2_free_alloc_context(meta_ac);
392 static int ocfs2_xattr_shrink_size(struct inode *inode,
395 struct buffer_head *root_bh,
396 struct ocfs2_xattr_value_root *xv)
399 u32 trunc_len, cpos, phys_cpos, alloc_size;
401 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
402 struct ocfs2_cached_dealloc_ctxt dealloc;
404 ocfs2_init_dealloc_ctxt(&dealloc);
406 if (old_clusters <= new_clusters)
410 trunc_len = old_clusters - new_clusters;
412 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
413 &alloc_size, &xv->xr_list);
419 if (alloc_size > trunc_len)
420 alloc_size = trunc_len;
422 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
423 phys_cpos, alloc_size,
430 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
431 ocfs2_remove_xattr_clusters_from_cache(inode, block,
434 trunc_len -= alloc_size;
438 ocfs2_schedule_truncate_log_flush(osb, 1);
439 ocfs2_run_deallocs(osb, &dealloc);
444 static int ocfs2_xattr_value_truncate(struct inode *inode,
445 struct buffer_head *root_bh,
446 struct ocfs2_xattr_value_root *xv,
450 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
451 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
453 if (new_clusters == old_clusters)
456 if (new_clusters > old_clusters)
457 ret = ocfs2_xattr_extend_allocation(inode,
458 new_clusters - old_clusters,
461 ret = ocfs2_xattr_shrink_size(inode,
462 old_clusters, new_clusters,
468 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
469 size_t *result, const char *prefix,
470 const char *name, int name_len)
472 char *p = buffer + *result;
473 int prefix_len = strlen(prefix);
474 int total_len = prefix_len + name_len + 1;
476 *result += total_len;
478 /* we are just looking for how big our buffer needs to be */
485 memcpy(p, prefix, prefix_len);
486 memcpy(p + prefix_len, name, name_len);
487 p[prefix_len + name_len] = '\0';
492 static int ocfs2_xattr_list_entries(struct inode *inode,
493 struct ocfs2_xattr_header *header,
494 char *buffer, size_t buffer_size)
498 const char *prefix, *name;
500 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
501 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
502 type = ocfs2_xattr_get_type(entry);
503 prefix = ocfs2_xattr_prefix(type);
506 name = (const char *)header +
507 le16_to_cpu(entry->xe_name_offset);
509 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
510 &result, prefix, name,
520 static int ocfs2_xattr_ibody_list(struct inode *inode,
521 struct ocfs2_dinode *di,
525 struct ocfs2_xattr_header *header = NULL;
526 struct ocfs2_inode_info *oi = OCFS2_I(inode);
529 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
532 header = (struct ocfs2_xattr_header *)
533 ((void *)di + inode->i_sb->s_blocksize -
534 le16_to_cpu(di->i_xattr_inline_size));
536 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
541 static int ocfs2_xattr_block_list(struct inode *inode,
542 struct ocfs2_dinode *di,
546 struct buffer_head *blk_bh = NULL;
547 struct ocfs2_xattr_block *xb;
550 if (!di->i_xattr_loc)
553 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
559 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
560 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
565 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
566 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
567 ret = ocfs2_xattr_list_entries(inode, header,
568 buffer, buffer_size);
570 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
571 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
572 buffer, buffer_size);
580 ssize_t ocfs2_listxattr(struct dentry *dentry,
584 int ret = 0, i_ret = 0, b_ret = 0;
585 struct buffer_head *di_bh = NULL;
586 struct ocfs2_dinode *di = NULL;
587 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
589 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
592 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
595 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
601 di = (struct ocfs2_dinode *)di_bh->b_data;
603 down_read(&oi->ip_xattr_sem);
604 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
612 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
617 up_read(&oi->ip_xattr_sem);
618 ocfs2_inode_unlock(dentry->d_inode, 0);
622 return i_ret + b_ret;
625 static int ocfs2_xattr_find_entry(int name_index,
627 struct ocfs2_xattr_search *xs)
629 struct ocfs2_xattr_entry *entry;
636 name_len = strlen(name);
638 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
639 cmp = name_index - ocfs2_xattr_get_type(entry);
641 cmp = name_len - entry->xe_name_len;
643 cmp = memcmp(name, (xs->base +
644 le16_to_cpu(entry->xe_name_offset)),
652 return cmp ? -ENODATA : 0;
655 static int ocfs2_xattr_get_value_outside(struct inode *inode,
656 struct ocfs2_xattr_value_root *xv,
660 u32 cpos, p_cluster, num_clusters, bpc, clusters;
663 size_t cplen, blocksize;
664 struct buffer_head *bh = NULL;
665 struct ocfs2_extent_list *el;
668 clusters = le32_to_cpu(xv->xr_clusters);
669 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
670 blocksize = inode->i_sb->s_blocksize;
673 while (cpos < clusters) {
674 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
681 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
682 /* Copy ocfs2_xattr_value */
683 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
684 ret = ocfs2_read_block(inode, blkno, &bh);
690 cplen = len >= blocksize ? blocksize : len;
691 memcpy(buffer, bh->b_data, cplen);
700 cpos += num_clusters;
706 static int ocfs2_xattr_ibody_get(struct inode *inode,
711 struct ocfs2_xattr_search *xs)
713 struct ocfs2_inode_info *oi = OCFS2_I(inode);
714 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
715 struct ocfs2_xattr_value_root *xv;
719 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
722 xs->end = (void *)di + inode->i_sb->s_blocksize;
723 xs->header = (struct ocfs2_xattr_header *)
724 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
725 xs->base = (void *)xs->header;
726 xs->here = xs->header->xh_entries;
728 ret = ocfs2_xattr_find_entry(name_index, name, xs);
731 size = le64_to_cpu(xs->here->xe_value_size);
733 if (size > buffer_size)
735 if (ocfs2_xattr_is_local(xs->here)) {
736 memcpy(buffer, (void *)xs->base +
737 le16_to_cpu(xs->here->xe_name_offset) +
738 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
740 xv = (struct ocfs2_xattr_value_root *)
741 (xs->base + le16_to_cpu(
742 xs->here->xe_name_offset) +
743 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
744 ret = ocfs2_xattr_get_value_outside(inode, xv,
756 static int ocfs2_xattr_block_get(struct inode *inode,
761 struct ocfs2_xattr_search *xs)
763 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
764 struct buffer_head *blk_bh = NULL;
765 struct ocfs2_xattr_block *xb;
766 struct ocfs2_xattr_value_root *xv;
768 int ret = -ENODATA, name_offset, name_len, block_off, i;
770 if (!di->i_xattr_loc)
773 memset(&xs->bucket, 0, sizeof(xs->bucket));
775 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
781 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
782 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
787 xs->xattr_bh = blk_bh;
789 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
790 xs->header = &xb->xb_attrs.xb_header;
791 xs->base = (void *)xs->header;
792 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
793 xs->here = xs->header->xh_entries;
795 ret = ocfs2_xattr_find_entry(name_index, name, xs);
797 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
803 size = le64_to_cpu(xs->here->xe_value_size);
806 if (size > buffer_size)
809 name_offset = le16_to_cpu(xs->here->xe_name_offset);
810 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
811 i = xs->here - xs->header->xh_entries;
813 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
814 ret = ocfs2_xattr_bucket_get_name_value(inode,
819 xs->base = xs->bucket.bhs[block_off]->b_data;
821 if (ocfs2_xattr_is_local(xs->here)) {
822 memcpy(buffer, (void *)xs->base +
823 name_offset + name_len, size);
825 xv = (struct ocfs2_xattr_value_root *)
826 (xs->base + name_offset + name_len);
827 ret = ocfs2_xattr_get_value_outside(inode, xv,
837 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
838 brelse(xs->bucket.bhs[i]);
839 memset(&xs->bucket, 0, sizeof(xs->bucket));
847 * Copy an extended attribute into the buffer provided.
848 * Buffer is NULL to compute the size of buffer required.
850 static int ocfs2_xattr_get(struct inode *inode,
857 struct ocfs2_dinode *di = NULL;
858 struct buffer_head *di_bh = NULL;
859 struct ocfs2_inode_info *oi = OCFS2_I(inode);
860 struct ocfs2_xattr_search xis = {
861 .not_found = -ENODATA,
863 struct ocfs2_xattr_search xbs = {
864 .not_found = -ENODATA,
867 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
870 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
873 ret = ocfs2_inode_lock(inode, &di_bh, 0);
878 xis.inode_bh = xbs.inode_bh = di_bh;
879 di = (struct ocfs2_dinode *)di_bh->b_data;
881 down_read(&oi->ip_xattr_sem);
882 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
885 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
887 up_read(&oi->ip_xattr_sem);
888 ocfs2_inode_unlock(inode, 0);
895 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
896 struct ocfs2_xattr_value_root *xv,
900 int ret = 0, i, cp_len, credits;
901 u16 blocksize = inode->i_sb->s_blocksize;
902 u32 p_cluster, num_clusters;
903 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
904 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
906 struct buffer_head *bh = NULL;
909 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
911 credits = clusters * bpc;
912 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
913 if (IS_ERR(handle)) {
914 ret = PTR_ERR(handle);
919 while (cpos < clusters) {
920 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
921 &num_clusters, &xv->xr_list);
927 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
929 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
930 ret = ocfs2_read_block(inode, blkno, &bh);
936 ret = ocfs2_journal_access(handle,
939 OCFS2_JOURNAL_ACCESS_WRITE);
945 cp_len = value_len > blocksize ? blocksize : value_len;
946 memcpy(bh->b_data, value, cp_len);
949 if (cp_len < blocksize)
950 memset(bh->b_data + cp_len, 0,
953 ret = ocfs2_journal_dirty(handle, bh);
962 * XXX: do we need to empty all the following
963 * blocks in this cluster?
968 cpos += num_clusters;
971 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
978 static int ocfs2_xattr_cleanup(struct inode *inode,
979 struct ocfs2_xattr_info *xi,
980 struct ocfs2_xattr_search *xs,
983 handle_t *handle = NULL;
985 size_t name_len = strlen(xi->name);
986 void *val = xs->base + offs;
987 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
989 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
990 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
991 if (IS_ERR(handle)) {
992 ret = PTR_ERR(handle);
996 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
997 OCFS2_JOURNAL_ACCESS_WRITE);
1002 /* Decrease xattr count */
1003 le16_add_cpu(&xs->header->xh_count, -1);
1004 /* Remove the xattr entry and tree root which has already be set*/
1005 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1006 memset(val, 0, size);
1008 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1012 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1017 static int ocfs2_xattr_update_entry(struct inode *inode,
1018 struct ocfs2_xattr_info *xi,
1019 struct ocfs2_xattr_search *xs,
1022 handle_t *handle = NULL;
1025 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1026 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1027 if (IS_ERR(handle)) {
1028 ret = PTR_ERR(handle);
1032 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1033 OCFS2_JOURNAL_ACCESS_WRITE);
1039 xs->here->xe_name_offset = cpu_to_le16(offs);
1040 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1041 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1042 ocfs2_xattr_set_local(xs->here, 1);
1044 ocfs2_xattr_set_local(xs->here, 0);
1045 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1047 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1051 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1057 * ocfs2_xattr_set_value_outside()
1059 * Set large size value in B tree.
1061 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1062 struct ocfs2_xattr_info *xi,
1063 struct ocfs2_xattr_search *xs,
1066 size_t name_len = strlen(xi->name);
1067 void *val = xs->base + offs;
1068 struct ocfs2_xattr_value_root *xv = NULL;
1069 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1072 memset(val, 0, size);
1073 memcpy(val, xi->name, name_len);
1074 xv = (struct ocfs2_xattr_value_root *)
1075 (val + OCFS2_XATTR_SIZE(name_len));
1076 xv->xr_clusters = 0;
1077 xv->xr_last_eb_blk = 0;
1078 xv->xr_list.l_tree_depth = 0;
1079 xv->xr_list.l_count = cpu_to_le16(1);
1080 xv->xr_list.l_next_free_rec = 0;
1082 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1088 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1094 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1102 * ocfs2_xattr_set_entry_local()
1104 * Set, replace or remove extended attribute in local.
1106 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1107 struct ocfs2_xattr_info *xi,
1108 struct ocfs2_xattr_search *xs,
1109 struct ocfs2_xattr_entry *last,
1112 size_t name_len = strlen(xi->name);
1115 if (xi->value && xs->not_found) {
1116 /* Insert the new xattr entry. */
1117 le16_add_cpu(&xs->header->xh_count, 1);
1118 ocfs2_xattr_set_type(last, xi->name_index);
1119 ocfs2_xattr_set_local(last, 1);
1120 last->xe_name_len = name_len;
1126 first_val = xs->base + min_offs;
1127 offs = le16_to_cpu(xs->here->xe_name_offset);
1128 val = xs->base + offs;
1130 if (le64_to_cpu(xs->here->xe_value_size) >
1131 OCFS2_XATTR_INLINE_SIZE)
1132 size = OCFS2_XATTR_SIZE(name_len) +
1133 OCFS2_XATTR_ROOT_SIZE;
1135 size = OCFS2_XATTR_SIZE(name_len) +
1136 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1138 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1139 OCFS2_XATTR_SIZE(xi->value_len)) {
1140 /* The old and the new value have the
1141 same size. Just replace the value. */
1142 ocfs2_xattr_set_local(xs->here, 1);
1143 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1144 /* Clear value bytes. */
1145 memset(val + OCFS2_XATTR_SIZE(name_len),
1147 OCFS2_XATTR_SIZE(xi->value_len));
1148 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1153 /* Remove the old name+value. */
1154 memmove(first_val + size, first_val, val - first_val);
1155 memset(first_val, 0, size);
1156 xs->here->xe_name_hash = 0;
1157 xs->here->xe_name_offset = 0;
1158 ocfs2_xattr_set_local(xs->here, 1);
1159 xs->here->xe_value_size = 0;
1163 /* Adjust all value offsets. */
1164 last = xs->header->xh_entries;
1165 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1166 size_t o = le16_to_cpu(last->xe_name_offset);
1169 last->xe_name_offset = cpu_to_le16(o + size);
1174 /* Remove the old entry. */
1176 memmove(xs->here, xs->here + 1,
1177 (void *)last - (void *)xs->here);
1178 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1179 le16_add_cpu(&xs->header->xh_count, -1);
1183 /* Insert the new name+value. */
1184 size_t size = OCFS2_XATTR_SIZE(name_len) +
1185 OCFS2_XATTR_SIZE(xi->value_len);
1186 void *val = xs->base + min_offs - size;
1188 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1189 memset(val, 0, size);
1190 memcpy(val, xi->name, name_len);
1191 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1194 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1195 ocfs2_xattr_set_local(xs->here, 1);
1196 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1203 * ocfs2_xattr_set_entry()
1205 * Set extended attribute entry into inode or block.
1207 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1208 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1209 * then set value in B tree with set_value_outside().
1211 static int ocfs2_xattr_set_entry(struct inode *inode,
1212 struct ocfs2_xattr_info *xi,
1213 struct ocfs2_xattr_search *xs,
1216 struct ocfs2_xattr_entry *last;
1217 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1218 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1219 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1221 handle_t *handle = NULL;
1223 struct ocfs2_xattr_info xi_l = {
1224 .name_index = xi->name_index,
1227 .value_len = xi->value_len,
1230 /* Compute min_offs, last and free space. */
1231 last = xs->header->xh_entries;
1233 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1234 size_t offs = le16_to_cpu(last->xe_name_offset);
1235 if (offs < min_offs)
1240 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1244 if (!xs->not_found) {
1246 if (ocfs2_xattr_is_local(xs->here))
1247 size = OCFS2_XATTR_SIZE(name_len) +
1248 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1250 size = OCFS2_XATTR_SIZE(name_len) +
1251 OCFS2_XATTR_ROOT_SIZE;
1252 free += (size + sizeof(struct ocfs2_xattr_entry));
1254 /* Check free space in inode or block */
1255 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1256 if (free < sizeof(struct ocfs2_xattr_entry) +
1257 OCFS2_XATTR_SIZE(name_len) +
1258 OCFS2_XATTR_ROOT_SIZE) {
1262 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1263 xi_l.value = (void *)&def_xv;
1264 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1265 } else if (xi->value) {
1266 if (free < sizeof(struct ocfs2_xattr_entry) +
1267 OCFS2_XATTR_SIZE(name_len) +
1268 OCFS2_XATTR_SIZE(xi->value_len)) {
1274 if (!xs->not_found) {
1275 /* For existing extended attribute */
1276 size_t size = OCFS2_XATTR_SIZE(name_len) +
1277 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1278 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1279 void *val = xs->base + offs;
1281 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1282 /* Replace existing local xattr with tree root */
1283 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1288 } else if (!ocfs2_xattr_is_local(xs->here)) {
1289 /* For existing xattr which has value outside */
1290 struct ocfs2_xattr_value_root *xv = NULL;
1291 xv = (struct ocfs2_xattr_value_root *)(val +
1292 OCFS2_XATTR_SIZE(name_len));
1294 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1296 * If new value need set outside also,
1297 * first truncate old value to new value,
1298 * then set new value with set_value_outside().
1300 ret = ocfs2_xattr_value_truncate(inode,
1309 ret = __ocfs2_xattr_set_value_outside(inode,
1318 ret = ocfs2_xattr_update_entry(inode,
1327 * If new value need set in local,
1328 * just trucate old value to zero.
1330 ret = ocfs2_xattr_value_truncate(inode,
1340 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1341 OCFS2_INODE_UPDATE_CREDITS);
1342 if (IS_ERR(handle)) {
1343 ret = PTR_ERR(handle);
1348 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1349 OCFS2_JOURNAL_ACCESS_WRITE);
1355 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1356 /* set extended attribute in external block. */
1357 ret = ocfs2_extend_trans(handle,
1358 OCFS2_INODE_UPDATE_CREDITS +
1359 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1364 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1365 OCFS2_JOURNAL_ACCESS_WRITE);
1373 * Set value in local, include set tree root in local.
1374 * This is the first step for value size >INLINE_SIZE.
1376 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1378 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1379 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1386 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1387 (flag & OCFS2_INLINE_XATTR_FL)) {
1388 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1389 unsigned int xattrsize = osb->s_xattr_inline_size;
1392 * Adjust extent record count or inline data size
1393 * to reserve space for extended attribute.
1395 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1396 struct ocfs2_inline_data *idata = &di->id2.i_data;
1397 le16_add_cpu(&idata->id_count, -xattrsize);
1398 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1399 struct ocfs2_extent_list *el = &di->id2.i_list;
1400 le16_add_cpu(&el->l_count, -(xattrsize /
1401 sizeof(struct ocfs2_extent_rec)));
1403 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1405 /* Update xattr flag */
1406 spin_lock(&oi->ip_lock);
1407 oi->ip_dyn_features |= flag;
1408 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1409 spin_unlock(&oi->ip_lock);
1410 /* Update inode ctime */
1411 inode->i_ctime = CURRENT_TIME;
1412 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1413 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1415 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1420 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1422 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1424 * Set value outside in B tree.
1425 * This is the second step for value size > INLINE_SIZE.
1427 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1428 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1434 * If set value outside failed, we have to clean
1435 * the junk tree root we have already set in local.
1437 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1447 static int ocfs2_remove_value_outside(struct inode*inode,
1448 struct buffer_head *bh,
1449 struct ocfs2_xattr_header *header)
1453 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1454 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1456 if (!ocfs2_xattr_is_local(entry)) {
1457 struct ocfs2_xattr_value_root *xv;
1460 val = (void *)header +
1461 le16_to_cpu(entry->xe_name_offset);
1462 xv = (struct ocfs2_xattr_value_root *)
1463 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1464 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1475 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1476 struct buffer_head *di_bh)
1479 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1480 struct ocfs2_xattr_header *header;
1483 header = (struct ocfs2_xattr_header *)
1484 ((void *)di + inode->i_sb->s_blocksize -
1485 le16_to_cpu(di->i_xattr_inline_size));
1487 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1492 static int ocfs2_xattr_block_remove(struct inode *inode,
1493 struct buffer_head *blk_bh)
1495 struct ocfs2_xattr_block *xb;
1498 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1499 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1500 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1501 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1503 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1508 static int ocfs2_xattr_free_block(struct inode *inode,
1511 struct inode *xb_alloc_inode;
1512 struct buffer_head *xb_alloc_bh = NULL;
1513 struct buffer_head *blk_bh = NULL;
1514 struct ocfs2_xattr_block *xb;
1515 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1521 ret = ocfs2_read_block(inode, block, &blk_bh);
1527 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1528 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1533 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1539 blk = le64_to_cpu(xb->xb_blkno);
1540 bit = le16_to_cpu(xb->xb_suballoc_bit);
1541 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1543 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1544 EXTENT_ALLOC_SYSTEM_INODE,
1545 le16_to_cpu(xb->xb_suballoc_slot));
1546 if (!xb_alloc_inode) {
1551 mutex_lock(&xb_alloc_inode->i_mutex);
1553 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1559 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1560 if (IS_ERR(handle)) {
1561 ret = PTR_ERR(handle);
1566 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1571 ocfs2_commit_trans(osb, handle);
1573 ocfs2_inode_unlock(xb_alloc_inode, 1);
1574 brelse(xb_alloc_bh);
1576 mutex_unlock(&xb_alloc_inode->i_mutex);
1577 iput(xb_alloc_inode);
1584 * ocfs2_xattr_remove()
1586 * Free extended attribute resources associated with this inode.
1588 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1590 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1591 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1595 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1598 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1601 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1602 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1609 if (di->i_xattr_loc) {
1610 ret = ocfs2_xattr_free_block(inode,
1611 le64_to_cpu(di->i_xattr_loc));
1618 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1619 OCFS2_INODE_UPDATE_CREDITS);
1620 if (IS_ERR(handle)) {
1621 ret = PTR_ERR(handle);
1625 ret = ocfs2_journal_access(handle, inode, di_bh,
1626 OCFS2_JOURNAL_ACCESS_WRITE);
1632 di->i_xattr_loc = 0;
1634 spin_lock(&oi->ip_lock);
1635 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1636 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1637 spin_unlock(&oi->ip_lock);
1639 ret = ocfs2_journal_dirty(handle, di_bh);
1643 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1648 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1649 struct ocfs2_dinode *di)
1651 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1652 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1655 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1658 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1659 struct ocfs2_inline_data *idata = &di->id2.i_data;
1660 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1661 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1662 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1663 le64_to_cpu(di->i_size);
1665 struct ocfs2_extent_list *el = &di->id2.i_list;
1666 free = (le16_to_cpu(el->l_count) -
1667 le16_to_cpu(el->l_next_free_rec)) *
1668 sizeof(struct ocfs2_extent_rec);
1670 if (free >= xattrsize)
1677 * ocfs2_xattr_ibody_find()
1679 * Find extended attribute in inode block and
1680 * fill search info into struct ocfs2_xattr_search.
1682 static int ocfs2_xattr_ibody_find(struct inode *inode,
1685 struct ocfs2_xattr_search *xs)
1687 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1688 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1692 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1695 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1696 down_read(&oi->ip_alloc_sem);
1697 has_space = ocfs2_xattr_has_space_inline(inode, di);
1698 up_read(&oi->ip_alloc_sem);
1703 xs->xattr_bh = xs->inode_bh;
1704 xs->end = (void *)di + inode->i_sb->s_blocksize;
1705 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1706 xs->header = (struct ocfs2_xattr_header *)
1707 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1709 xs->header = (struct ocfs2_xattr_header *)
1710 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1711 xs->base = (void *)xs->header;
1712 xs->here = xs->header->xh_entries;
1714 /* Find the named attribute. */
1715 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1716 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1717 if (ret && ret != -ENODATA)
1719 xs->not_found = ret;
1726 * ocfs2_xattr_ibody_set()
1728 * Set, replace or remove an extended attribute into inode block.
1731 static int ocfs2_xattr_ibody_set(struct inode *inode,
1732 struct ocfs2_xattr_info *xi,
1733 struct ocfs2_xattr_search *xs)
1735 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1736 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1739 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1742 down_write(&oi->ip_alloc_sem);
1743 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1744 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1750 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1751 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1753 up_write(&oi->ip_alloc_sem);
1759 * ocfs2_xattr_block_find()
1761 * Find extended attribute in external block and
1762 * fill search info into struct ocfs2_xattr_search.
1764 static int ocfs2_xattr_block_find(struct inode *inode,
1767 struct ocfs2_xattr_search *xs)
1769 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1770 struct buffer_head *blk_bh = NULL;
1771 struct ocfs2_xattr_block *xb;
1774 if (!di->i_xattr_loc)
1777 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
1783 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1784 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1789 xs->xattr_bh = blk_bh;
1791 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1792 xs->header = &xb->xb_attrs.xb_header;
1793 xs->base = (void *)xs->header;
1794 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1795 xs->here = xs->header->xh_entries;
1797 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1799 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1803 if (ret && ret != -ENODATA) {
1804 xs->xattr_bh = NULL;
1807 xs->not_found = ret;
1816 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1817 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1820 static int ocfs2_restore_xattr_block(struct inode *inode,
1821 struct ocfs2_xattr_search *xs)
1825 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1826 struct ocfs2_xattr_block *xb =
1827 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1828 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
1829 u16 xb_flags = le16_to_cpu(xb->xb_flags);
1831 BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
1832 le16_to_cpu(el->l_next_free_rec) != 0);
1834 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1835 if (IS_ERR(handle)) {
1836 ret = PTR_ERR(handle);
1841 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1842 OCFS2_JOURNAL_ACCESS_WRITE);
1848 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
1849 offsetof(struct ocfs2_xattr_block, xb_attrs));
1851 xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
1853 ocfs2_journal_dirty(handle, xs->xattr_bh);
1856 ocfs2_commit_trans(osb, handle);
1862 * ocfs2_xattr_block_set()
1864 * Set, replace or remove an extended attribute into external block.
1867 static int ocfs2_xattr_block_set(struct inode *inode,
1868 struct ocfs2_xattr_info *xi,
1869 struct ocfs2_xattr_search *xs)
1871 struct buffer_head *new_bh = NULL;
1872 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1873 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1874 struct ocfs2_alloc_context *meta_ac = NULL;
1875 handle_t *handle = NULL;
1876 struct ocfs2_xattr_block *xblk = NULL;
1877 u16 suballoc_bit_start;
1882 if (!xs->xattr_bh) {
1884 * Alloc one external block for extended attribute
1887 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1892 handle = ocfs2_start_trans(osb,
1893 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1894 if (IS_ERR(handle)) {
1895 ret = PTR_ERR(handle);
1899 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1900 OCFS2_JOURNAL_ACCESS_CREATE);
1906 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1907 &suballoc_bit_start, &num_got,
1914 new_bh = sb_getblk(inode->i_sb, first_blkno);
1915 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1917 ret = ocfs2_journal_access(handle, inode, new_bh,
1918 OCFS2_JOURNAL_ACCESS_CREATE);
1924 /* Initialize ocfs2_xattr_block */
1925 xs->xattr_bh = new_bh;
1926 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1927 memset(xblk, 0, inode->i_sb->s_blocksize);
1928 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1929 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1930 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1931 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1932 xblk->xb_blkno = cpu_to_le64(first_blkno);
1934 xs->header = &xblk->xb_attrs.xb_header;
1935 xs->base = (void *)xs->header;
1936 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1937 xs->here = xs->header->xh_entries;
1940 ret = ocfs2_journal_dirty(handle, new_bh);
1945 di->i_xattr_loc = cpu_to_le64(first_blkno);
1946 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1950 ocfs2_commit_trans(osb, handle);
1953 ocfs2_free_alloc_context(meta_ac);
1957 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1959 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1960 /* Set extended attribute into external block */
1961 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1962 if (!ret || ret != -ENOSPC)
1965 ret = ocfs2_xattr_create_index_block(inode, xs);
1970 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1971 if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
1972 ret = ocfs2_restore_xattr_block(inode, xs);
1982 * Set, replace or remove an extended attribute for this inode.
1983 * value is NULL to remove an existing extended attribute, else either
1984 * create or replace an extended attribute.
1986 int ocfs2_xattr_set(struct inode *inode,
1993 struct buffer_head *di_bh = NULL;
1994 struct ocfs2_dinode *di;
1996 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1998 struct ocfs2_xattr_info xi = {
1999 .name_index = name_index,
2002 .value_len = value_len,
2005 struct ocfs2_xattr_search xis = {
2006 .not_found = -ENODATA,
2009 struct ocfs2_xattr_search xbs = {
2010 .not_found = -ENODATA,
2013 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2016 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2021 xis.inode_bh = xbs.inode_bh = di_bh;
2022 di = (struct ocfs2_dinode *)di_bh->b_data;
2024 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2026 * Scan inode and external block to find the same name
2027 * extended attribute and collect search infomation.
2029 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2032 if (xis.not_found) {
2033 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2038 if (xis.not_found && xbs.not_found) {
2040 if (flags & XATTR_REPLACE)
2047 if (flags & XATTR_CREATE)
2052 /* Remove existing extended attribute */
2054 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2055 else if (!xbs.not_found)
2056 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2058 /* We always try to set extended attribute into inode first*/
2059 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2060 if (!ret && !xbs.not_found) {
2062 * If succeed and that extended attribute existing in
2063 * external block, then we will remove it.
2067 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2068 } else if (ret == -ENOSPC) {
2069 if (di->i_xattr_loc && !xbs.xattr_bh) {
2070 ret = ocfs2_xattr_block_find(inode, name_index,
2076 * If no space in inode, we will set extended attribute
2077 * into external block.
2079 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2082 if (!xis.not_found) {
2084 * If succeed and that extended attribute
2085 * existing in inode, we will remove it.
2089 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2094 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2095 ocfs2_inode_unlock(inode, 1);
2097 brelse(xbs.xattr_bh);
2098 for (i = 0; i < blk_per_bucket; i++)
2099 brelse(xbs.bucket.bhs[i]);
2105 * Find the xattr extent rec which may contains name_hash.
2106 * e_cpos will be the first name hash of the xattr rec.
2107 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2109 static int ocfs2_xattr_get_rec(struct inode *inode,
2114 struct ocfs2_extent_list *el)
2117 struct buffer_head *eb_bh = NULL;
2118 struct ocfs2_extent_block *eb;
2119 struct ocfs2_extent_rec *rec = NULL;
2122 if (el->l_tree_depth) {
2123 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2129 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2132 if (el->l_tree_depth) {
2133 ocfs2_error(inode->i_sb,
2134 "Inode %lu has non zero tree depth in "
2135 "xattr tree block %llu\n", inode->i_ino,
2136 (unsigned long long)eb_bh->b_blocknr);
2142 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2143 rec = &el->l_recs[i];
2145 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2146 e_blkno = le64_to_cpu(rec->e_blkno);
2152 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2153 "record (%u, %u, 0) in xattr", inode->i_ino,
2154 le32_to_cpu(rec->e_cpos),
2155 ocfs2_rec_clusters(el, rec));
2160 *p_blkno = le64_to_cpu(rec->e_blkno);
2161 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2163 *e_cpos = le32_to_cpu(rec->e_cpos);
2169 typedef int (xattr_bucket_func)(struct inode *inode,
2170 struct ocfs2_xattr_bucket *bucket,
2173 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2174 struct buffer_head *header_bh,
2181 int i, ret = 0, cmp = 1, block_off, new_offset;
2182 struct ocfs2_xattr_header *xh =
2183 (struct ocfs2_xattr_header *)header_bh->b_data;
2184 size_t name_len = strlen(name);
2185 struct ocfs2_xattr_entry *xe = NULL;
2186 struct buffer_head *name_bh = NULL;
2190 * We don't use binary search in the bucket because there
2191 * may be multiple entries with the same name hash.
2193 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2194 xe = &xh->xh_entries[i];
2196 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2198 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2201 cmp = name_index - ocfs2_xattr_get_type(xe);
2203 cmp = name_len - xe->xe_name_len;
2207 ret = ocfs2_xattr_bucket_get_name_value(inode,
2217 ret = ocfs2_read_block(inode, header_bh->b_blocknr + block_off,
2223 xe_name = name_bh->b_data + new_offset;
2225 cmp = memcmp(name, xe_name, name_len);
2241 * Find the specified xattr entry in a series of buckets.
2242 * This series start from p_blkno and last for num_clusters.
2243 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2244 * the num of the valid buckets.
2246 * Return the buffer_head this xattr should reside in. And if the xattr's
2247 * hash is in the gap of 2 buckets, return the lower bucket.
2249 static int ocfs2_xattr_bucket_find(struct inode *inode,
2256 struct ocfs2_xattr_search *xs)
2259 struct buffer_head *bh = NULL;
2260 struct buffer_head *lower_bh = NULL;
2261 struct ocfs2_xattr_header *xh = NULL;
2262 struct ocfs2_xattr_entry *xe = NULL;
2264 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2265 int low_bucket = 0, bucket, high_bucket;
2269 ret = ocfs2_read_block(inode, p_blkno, &bh);
2275 xh = (struct ocfs2_xattr_header *)bh->b_data;
2276 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2278 while (low_bucket <= high_bucket) {
2281 bucket = (low_bucket + high_bucket) / 2;
2283 blkno = p_blkno + bucket * blk_per_bucket;
2285 ret = ocfs2_read_block(inode, blkno, &bh);
2291 xh = (struct ocfs2_xattr_header *)bh->b_data;
2292 xe = &xh->xh_entries[0];
2293 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2294 high_bucket = bucket - 1;
2299 * Check whether the hash of the last entry in our
2300 * bucket is larger than the search one. for an empty
2301 * bucket, the last one is also the first one.
2304 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2306 last_hash = le32_to_cpu(xe->xe_name_hash);
2308 /* record lower_bh which may be the insert place. */
2313 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2314 low_bucket = bucket + 1;
2318 /* the searched xattr should reside in this bucket if exists. */
2319 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2320 name_index, name, name_hash,
2330 * Record the bucket we have found.
2331 * When the xattr's hash value is in the gap of 2 buckets, we will
2332 * always set it to the previous bucket.
2336 * We can't find any bucket whose first name_hash is less
2337 * than the find name_hash.
2339 BUG_ON(bh->b_blocknr != p_blkno);
2343 xs->bucket.bhs[0] = lower_bh;
2344 xs->bucket.xh = (struct ocfs2_xattr_header *)
2345 xs->bucket.bhs[0]->b_data;
2348 xs->header = xs->bucket.xh;
2349 xs->base = xs->bucket.bhs[0]->b_data;
2350 xs->end = xs->base + inode->i_sb->s_blocksize;
2354 * If we have found the xattr enty, read all the blocks in
2357 ret = ocfs2_read_blocks(inode, xs->bucket.bhs[0]->b_blocknr + 1,
2358 blk_per_bucket - 1, &xs->bucket.bhs[1],
2365 xs->here = &xs->header->xh_entries[index];
2366 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2367 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2377 static int ocfs2_xattr_index_block_find(struct inode *inode,
2378 struct buffer_head *root_bh,
2381 struct ocfs2_xattr_search *xs)
2384 struct ocfs2_xattr_block *xb =
2385 (struct ocfs2_xattr_block *)root_bh->b_data;
2386 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2387 struct ocfs2_extent_list *el = &xb_root->xt_list;
2389 u32 first_hash, num_clusters = 0;
2390 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
2392 if (le16_to_cpu(el->l_next_free_rec) == 0)
2395 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2396 name, name_hash, name_index);
2398 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2405 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2407 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2408 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2410 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2411 p_blkno, first_hash, num_clusters, xs);
2417 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2420 xattr_bucket_func *func,
2424 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2425 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2426 u32 num_buckets = clusters * bpc;
2427 struct ocfs2_xattr_bucket bucket;
2429 memset(&bucket, 0, sizeof(bucket));
2431 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2434 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2435 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket,
2442 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2444 * The real bucket num in this series of blocks is stored
2445 * in the 1st bucket.
2448 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2450 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
2451 le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
2453 ret = func(inode, &bucket, para);
2460 for (j = 0; j < blk_per_bucket; j++)
2461 brelse(bucket.bhs[j]);
2462 memset(&bucket, 0, sizeof(bucket));
2466 for (j = 0; j < blk_per_bucket; j++)
2467 brelse(bucket.bhs[j]);
2472 struct ocfs2_xattr_tree_list {
2478 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2479 struct ocfs2_xattr_header *xh,
2486 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2489 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2491 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2492 *new_offset = name_offset % inode->i_sb->s_blocksize;
2497 static int ocfs2_list_xattr_bucket(struct inode *inode,
2498 struct ocfs2_xattr_bucket *bucket,
2502 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2503 int i, block_off, new_offset;
2504 const char *prefix, *name;
2506 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2507 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2508 type = ocfs2_xattr_get_type(entry);
2509 prefix = ocfs2_xattr_prefix(type);
2512 ret = ocfs2_xattr_bucket_get_name_value(inode,
2520 name = (const char *)bucket->bhs[block_off]->b_data +
2522 ret = ocfs2_xattr_list_entry(xl->buffer,
2526 entry->xe_name_len);
2535 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2536 struct ocfs2_xattr_tree_root *xt,
2540 struct ocfs2_extent_list *el = &xt->xt_list;
2542 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2544 struct ocfs2_xattr_tree_list xl = {
2546 .buffer_size = buffer_size,
2550 if (le16_to_cpu(el->l_next_free_rec) == 0)
2553 while (name_hash > 0) {
2554 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2555 &e_cpos, &num_clusters, el);
2561 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2562 ocfs2_list_xattr_bucket,
2572 name_hash = e_cpos - 1;
2580 static int cmp_xe(const void *a, const void *b)
2582 const struct ocfs2_xattr_entry *l = a, *r = b;
2583 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2584 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2586 if (l_hash > r_hash)
2588 if (l_hash < r_hash)
2593 static void swap_xe(void *a, void *b, int size)
2595 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2598 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2599 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2603 * When the ocfs2_xattr_block is filled up, new bucket will be created
2604 * and all the xattr entries will be moved to the new bucket.
2605 * Note: we need to sort the entries since they are not saved in order
2606 * in the ocfs2_xattr_block.
2608 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2609 struct buffer_head *xb_bh,
2610 struct buffer_head *xh_bh,
2611 struct buffer_head *data_bh)
2613 int i, blocksize = inode->i_sb->s_blocksize;
2614 u16 offset, size, off_change;
2615 struct ocfs2_xattr_entry *xe;
2616 struct ocfs2_xattr_block *xb =
2617 (struct ocfs2_xattr_block *)xb_bh->b_data;
2618 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2619 struct ocfs2_xattr_header *xh =
2620 (struct ocfs2_xattr_header *)xh_bh->b_data;
2621 u16 count = le16_to_cpu(xb_xh->xh_count);
2622 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2624 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2625 (unsigned long long)xb_bh->b_blocknr,
2626 (unsigned long long)xh_bh->b_blocknr);
2628 memset(xh_bh->b_data, 0, blocksize);
2630 memset(data_bh->b_data, 0, blocksize);
2632 * Since the xe_name_offset is based on ocfs2_xattr_header,
2633 * there is a offset change corresponding to the change of
2634 * ocfs2_xattr_header's position.
2636 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2637 xe = &xb_xh->xh_entries[count - 1];
2638 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2639 size = blocksize - offset;
2641 /* copy all the names and values. */
2643 target = data_bh->b_data;
2644 memcpy(target + offset, src + offset, size);
2646 /* Init new header now. */
2647 xh->xh_count = xb_xh->xh_count;
2648 xh->xh_num_buckets = cpu_to_le16(1);
2649 xh->xh_name_value_len = cpu_to_le16(size);
2650 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2652 /* copy all the entries. */
2653 target = xh_bh->b_data;
2654 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2655 size = count * sizeof(struct ocfs2_xattr_entry);
2656 memcpy(target + offset, (char *)xb_xh + offset, size);
2658 /* Change the xe offset for all the xe because of the move. */
2659 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2660 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2661 for (i = 0; i < count; i++)
2662 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2664 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2665 offset, size, off_change);
2667 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2672 * After we move xattr from block to index btree, we have to
2673 * update ocfs2_xattr_search to the new xe and base.
2675 * When the entry is in xattr block, xattr_bh indicates the storage place.
2676 * While if the entry is in index b-tree, "bucket" indicates the
2677 * real place of the xattr.
2679 static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2680 struct ocfs2_xattr_search *xs,
2681 struct buffer_head *old_bh,
2682 struct buffer_head *new_bh)
2685 char *buf = old_bh->b_data;
2686 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2687 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2688 int i, blocksize = inode->i_sb->s_blocksize;
2689 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2691 xs->bucket.bhs[0] = new_bh;
2693 xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2694 xs->header = xs->bucket.xh;
2696 xs->base = new_bh->b_data;
2697 xs->end = xs->base + inode->i_sb->s_blocksize;
2699 if (!xs->not_found) {
2700 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2701 ret = ocfs2_read_blocks(inode,
2702 xs->bucket.bhs[0]->b_blocknr + 1,
2703 blk_per_bucket - 1, &xs->bucket.bhs[1],
2710 i = xs->here - old_xh->xh_entries;
2711 xs->here = &xs->header->xh_entries[i];
2718 static int ocfs2_xattr_create_index_block(struct inode *inode,
2719 struct ocfs2_xattr_search *xs)
2721 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2725 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2726 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2727 struct ocfs2_alloc_context *data_ac;
2728 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2729 struct buffer_head *xb_bh = xs->xattr_bh;
2730 struct ocfs2_xattr_block *xb =
2731 (struct ocfs2_xattr_block *)xb_bh->b_data;
2732 struct ocfs2_xattr_tree_root *xr;
2733 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2734 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2736 mlog(0, "create xattr index block for %llu\n",
2737 (unsigned long long)xb_bh->b_blocknr);
2739 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2741 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2749 * We can use this lock for now, and maybe move to a dedicated mutex
2750 * if performance becomes a problem later.
2752 down_write(&oi->ip_alloc_sem);
2755 * 3 more credits, one for xattr block update, one for the 1st block
2756 * of the new xattr bucket and one for the value/data.
2759 handle = ocfs2_start_trans(osb, credits);
2760 if (IS_ERR(handle)) {
2761 ret = PTR_ERR(handle);
2766 ret = ocfs2_journal_access(handle, inode, xb_bh,
2767 OCFS2_JOURNAL_ACCESS_WRITE);
2773 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2780 * The bucket may spread in many blocks, and
2781 * we will only touch the 1st block and the last block
2782 * in the whole bucket(one for entry and one for data).
2784 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2786 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2788 xh_bh = sb_getblk(inode->i_sb, blkno);
2795 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2797 ret = ocfs2_journal_access(handle, inode, xh_bh,
2798 OCFS2_JOURNAL_ACCESS_CREATE);
2805 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2812 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2814 ret = ocfs2_journal_access(handle, inode, data_bh,
2815 OCFS2_JOURNAL_ACCESS_CREATE);
2822 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2824 ocfs2_journal_dirty(handle, xh_bh);
2826 ocfs2_journal_dirty(handle, data_bh);
2828 ret = ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2834 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2835 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2836 offsetof(struct ocfs2_xattr_block, xb_attrs));
2838 xr = &xb->xb_attrs.xb_root;
2839 xr->xt_clusters = cpu_to_le32(1);
2840 xr->xt_last_eb_blk = 0;
2841 xr->xt_list.l_tree_depth = 0;
2842 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2843 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2845 xr->xt_list.l_recs[0].e_cpos = 0;
2846 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2847 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2849 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2851 ret = ocfs2_journal_dirty(handle, xb_bh);
2858 ocfs2_commit_trans(osb, handle);
2861 up_write(&oi->ip_alloc_sem);
2865 ocfs2_free_alloc_context(data_ac);
2873 static int cmp_xe_offset(const void *a, const void *b)
2875 const struct ocfs2_xattr_entry *l = a, *r = b;
2876 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2877 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2879 if (l_name_offset < r_name_offset)
2881 if (l_name_offset > r_name_offset)
2887 * defrag a xattr bucket if we find that the bucket has some
2888 * holes beteen name/value pairs.
2889 * We will move all the name/value pairs to the end of the bucket
2890 * so that we can spare some space for insertion.
2892 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2893 struct ocfs2_xattr_bucket *bucket)
2896 size_t end, offset, len, value_len;
2897 struct ocfs2_xattr_header *xh;
2898 char *entries, *buf, *bucket_buf = NULL;
2899 u64 blkno = bucket->bhs[0]->b_blocknr;
2900 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2902 size_t blocksize = inode->i_sb->s_blocksize;
2904 struct buffer_head **bhs;
2905 struct ocfs2_xattr_entry *xe;
2907 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2912 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket, bhs, 0);
2917 * In order to make the operation more efficient and generic,
2918 * we copy all the blocks into a contiguous memory and do the
2919 * defragment there, so if anything is error, we will not touch
2922 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2929 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2930 memcpy(buf, bhs[i]->b_data, blocksize);
2932 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2933 if (IS_ERR(handle)) {
2934 ret = PTR_ERR(handle);
2940 for (i = 0; i < blk_per_bucket; i++) {
2941 ret = ocfs2_journal_access(handle, inode, bhs[i],
2942 OCFS2_JOURNAL_ACCESS_WRITE);
2949 xh = (struct ocfs2_xattr_header *)bucket_buf;
2950 entries = (char *)xh->xh_entries;
2951 xh_free_start = le16_to_cpu(xh->xh_free_start);
2953 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2954 "xh_free_start = %u, xh_name_value_len = %u.\n",
2955 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2956 le16_to_cpu(xh->xh_name_value_len));
2959 * sort all the entries by their offset.
2960 * the largest will be the first, so that we can
2961 * move them to the end one by one.
2963 sort(entries, le16_to_cpu(xh->xh_count),
2964 sizeof(struct ocfs2_xattr_entry),
2965 cmp_xe_offset, swap_xe);
2967 /* Move all name/values to the end of the bucket. */
2968 xe = xh->xh_entries;
2969 end = OCFS2_XATTR_BUCKET_SIZE;
2970 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2971 offset = le16_to_cpu(xe->xe_name_offset);
2972 if (ocfs2_xattr_is_local(xe))
2973 value_len = OCFS2_XATTR_SIZE(
2974 le64_to_cpu(xe->xe_value_size));
2976 value_len = OCFS2_XATTR_ROOT_SIZE;
2977 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2980 * We must make sure that the name/value pair
2981 * exist in the same block. So adjust end to
2982 * the previous block end if needed.
2984 if (((end - len) / blocksize !=
2985 (end - 1) / blocksize))
2986 end = end - end % blocksize;
2988 if (end > offset + len) {
2989 memmove(bucket_buf + end - len,
2990 bucket_buf + offset, len);
2991 xe->xe_name_offset = cpu_to_le16(end - len);
2994 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2995 "bucket %llu\n", (unsigned long long)blkno);
3000 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3001 "bucket %llu\n", (unsigned long long)blkno);
3003 if (xh_free_start == end)
3006 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3007 xh->xh_free_start = cpu_to_le16(end);
3009 /* sort the entries by their name_hash. */
3010 sort(entries, le16_to_cpu(xh->xh_count),
3011 sizeof(struct ocfs2_xattr_entry),
3015 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
3016 memcpy(bhs[i]->b_data, buf, blocksize);
3017 ocfs2_journal_dirty(handle, bhs[i]);
3021 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3025 for (i = 0; i < blk_per_bucket; i++)
3035 * Move half nums of the xattr bucket in the previous cluster to this new
3036 * cluster. We only touch the last cluster of the previous extend record.
3038 * first_bh is the first buffer_head of a series of bucket in the same
3039 * extent rec and header_bh is the header of one bucket in this cluster.
3040 * They will be updated if we move the data header_bh contains to the new
3041 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3043 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3045 struct buffer_head **first_bh,
3046 struct buffer_head **header_bh,
3052 int i, ret, credits;
3053 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3054 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3055 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3056 int blocksize = inode->i_sb->s_blocksize;
3057 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3058 struct ocfs2_xattr_header *new_xh;
3059 struct ocfs2_xattr_header *xh =
3060 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3062 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3063 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3065 prev_bh = *first_bh;
3067 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3069 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3071 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3072 prev_blkno, new_blkno);
3075 * We need to update the 1st half of the new cluster and
3076 * 1 more for the update of the 1st bucket of the previous
3079 credits = bpc / 2 + 1;
3080 ret = ocfs2_extend_trans(handle, credits);
3086 ret = ocfs2_journal_access(handle, inode, prev_bh,
3087 OCFS2_JOURNAL_ACCESS_WRITE);
3093 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3094 old_bh = new_bh = NULL;
3095 new_bh = sb_getblk(inode->i_sb, new_blkno);
3102 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3104 ret = ocfs2_journal_access(handle, inode, new_bh,
3105 OCFS2_JOURNAL_ACCESS_CREATE);
3112 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
3119 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3122 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3123 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3126 *first_hash = le32_to_cpu(
3127 new_xh->xh_entries[0].xe_name_hash);
3128 new_first_bh = new_bh;
3129 get_bh(new_first_bh);
3132 ocfs2_journal_dirty(handle, new_bh);
3134 if (*header_bh == old_bh) {
3136 *header_bh = new_bh;
3140 *first_bh = new_first_bh;
3147 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3149 ocfs2_journal_dirty(handle, prev_bh);
3152 brelse(new_first_bh);
3156 static int ocfs2_read_xattr_bucket(struct inode *inode,
3158 struct buffer_head **bhs,
3162 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3165 return ocfs2_read_blocks(inode, blkno,
3166 blk_per_bucket, bhs, 0);
3168 for (i = 0; i < blk_per_bucket; i++) {
3169 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3170 if (bhs[i] == NULL) {
3175 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3182 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3183 * first_hash will record the 1st hash of the new bucket.
3185 static int ocfs2_half_xattr_bucket(struct inode *inode,
3190 int new_bucket_head)
3193 u16 count, start, len, name_value_len, xe_len, name_offset;
3194 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3195 struct buffer_head **s_bhs, **t_bhs = NULL;
3196 struct ocfs2_xattr_header *xh;
3197 struct ocfs2_xattr_entry *xe;
3198 int blocksize = inode->i_sb->s_blocksize;
3200 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3203 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3207 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3213 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3214 OCFS2_JOURNAL_ACCESS_WRITE);
3220 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3226 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3232 for (i = 0; i < blk_per_bucket; i++) {
3233 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3234 OCFS2_JOURNAL_ACCESS_CREATE);
3241 /* copy the whole bucket to the new first. */
3242 for (i = 0; i < blk_per_bucket; i++)
3243 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3245 /* update the new bucket. */
3246 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3247 count = le16_to_cpu(xh->xh_count);
3251 * Calculate the total name/value len and xh_free_start for
3252 * the old bucket first.
3254 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3256 for (i = 0; i < start; i++) {
3257 xe = &xh->xh_entries[i];
3258 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3259 if (ocfs2_xattr_is_local(xe))
3261 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3263 xe_len += OCFS2_XATTR_ROOT_SIZE;
3264 name_value_len += xe_len;
3265 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3266 name_offset = le16_to_cpu(xe->xe_name_offset);
3270 * Now begin the modification to the new bucket.
3272 * In the new bucket, We just move the xattr entry to the beginning
3273 * and don't touch the name/value. So there will be some holes in the
3274 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3277 xe = &xh->xh_entries[start];
3278 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3279 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3280 (int)((char *)xe - (char *)xh),
3281 (int)((char *)xh->xh_entries - (char *)xh));
3282 memmove((char *)xh->xh_entries, (char *)xe, len);
3283 xe = &xh->xh_entries[count - start];
3284 len = sizeof(struct ocfs2_xattr_entry) * start;
3285 memset((char *)xe, 0, len);
3287 le16_add_cpu(&xh->xh_count, -start);
3288 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3290 /* Calculate xh_free_start for the new bucket. */
3291 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3292 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3293 xe = &xh->xh_entries[i];
3294 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3295 if (ocfs2_xattr_is_local(xe))
3297 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3299 xe_len += OCFS2_XATTR_ROOT_SIZE;
3300 if (le16_to_cpu(xe->xe_name_offset) <
3301 le16_to_cpu(xh->xh_free_start))
3302 xh->xh_free_start = xe->xe_name_offset;
3305 /* set xh->xh_num_buckets for the new xh. */
3306 if (new_bucket_head)
3307 xh->xh_num_buckets = cpu_to_le16(1);
3309 xh->xh_num_buckets = 0;
3311 for (i = 0; i < blk_per_bucket; i++) {
3312 ocfs2_journal_dirty(handle, t_bhs[i]);
3317 /* store the first_hash of the new bucket. */
3319 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3322 * Now only update the 1st block of the old bucket.
3323 * Please note that the entry has been sorted already above.
3325 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3326 memset(&xh->xh_entries[start], 0,
3327 sizeof(struct ocfs2_xattr_entry) * (count - start));
3328 xh->xh_count = cpu_to_le16(start);
3329 xh->xh_free_start = cpu_to_le16(name_offset);
3330 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3332 ocfs2_journal_dirty(handle, s_bhs[0]);
3338 for (i = 0; i < blk_per_bucket; i++)
3344 for (i = 0; i < blk_per_bucket; i++)
3353 * Copy xattr from one bucket to another bucket.
3355 * The caller must make sure that the journal transaction
3356 * has enough space for journaling.
3358 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3365 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3366 int blocksize = inode->i_sb->s_blocksize;
3367 struct buffer_head **s_bhs, **t_bhs = NULL;
3369 BUG_ON(s_blkno == t_blkno);
3371 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3372 s_blkno, t_blkno, t_is_new);
3374 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3379 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3383 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3390 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3394 for (i = 0; i < blk_per_bucket; i++) {
3395 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3396 OCFS2_JOURNAL_ACCESS_WRITE);
3401 for (i = 0; i < blk_per_bucket; i++) {
3402 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3403 ocfs2_journal_dirty(handle, t_bhs[i]);
3408 for (i = 0; i < blk_per_bucket; i++)
3414 for (i = 0; i < blk_per_bucket; i++)
3423 * Copy one xattr cluster from src_blk to to_blk.
3424 * The to_blk will become the first bucket header of the cluster, so its
3425 * xh_num_buckets will be initialized as the bucket num in the cluster.
3427 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3429 struct buffer_head *first_bh,
3434 int i, ret, credits;
3435 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3436 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3437 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3438 struct buffer_head *bh = NULL;
3439 struct ocfs2_xattr_header *xh;
3440 u64 to_blk_start = to_blk;
3442 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3445 * We need to update the new cluster and 1 more for the update of
3446 * the 1st bucket of the previous extent rec.
3449 ret = ocfs2_extend_trans(handle, credits);
3455 ret = ocfs2_journal_access(handle, inode, first_bh,
3456 OCFS2_JOURNAL_ACCESS_WRITE);
3462 for (i = 0; i < num_buckets; i++) {
3463 ret = ocfs2_cp_xattr_bucket(inode, handle,
3464 src_blk, to_blk, 1);
3470 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3471 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3474 /* update the old bucket header. */
3475 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3476 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3478 ocfs2_journal_dirty(handle, first_bh);
3480 /* update the new bucket header. */
3481 ret = ocfs2_read_block(inode, to_blk_start, &bh);
3487 ret = ocfs2_journal_access(handle, inode, bh,
3488 OCFS2_JOURNAL_ACCESS_WRITE);
3494 xh = (struct ocfs2_xattr_header *)bh->b_data;
3495 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3497 ocfs2_journal_dirty(handle, bh);
3500 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3507 * Move half of the xattrs in this cluster to the new cluster.
3508 * This function should only be called when bucket size == cluster size.
3509 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3511 static int ocfs2_half_xattr_cluster(struct inode *inode,
3517 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3518 int ret, credits = 2 * blk_per_bucket;
3520 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3522 ret = ocfs2_extend_trans(handle, credits);
3528 /* Move half of the xattr in start_blk to the next bucket. */
3529 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3530 new_blk, first_hash, 1);
3534 * Move some xattrs from the old cluster to the new one since they are not
3535 * contiguous in ocfs2 xattr tree.
3537 * new_blk starts a new separate cluster, and we will move some xattrs from
3538 * prev_blk to it. v_start will be set as the first name hash value in this
3539 * new cluster so that it can be used as e_cpos during tree insertion and
3540 * don't collide with our original b-tree operations. first_bh and header_bh
3541 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3542 * to extend the insert bucket.
3544 * The problem is how much xattr should we move to the new one and when should
3545 * we update first_bh and header_bh?
3546 * 1. If cluster size > bucket size, that means the previous cluster has more
3547 * than 1 bucket, so just move half nums of bucket into the new cluster and
3548 * update the first_bh and header_bh if the insert bucket has been moved
3549 * to the new cluster.
3550 * 2. If cluster_size == bucket_size:
3551 * a) If the previous extent rec has more than one cluster and the insert
3552 * place isn't in the last cluster, copy the entire last cluster to the
3553 * new one. This time, we don't need to upate the first_bh and header_bh
3554 * since they will not be moved into the new cluster.
3555 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3556 * the new one. And we set the extend flag to zero if the insert place is
3557 * moved into the new allocated cluster since no extend is needed.
3559 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3561 struct buffer_head **first_bh,
3562 struct buffer_head **header_bh,
3570 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3572 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3573 prev_blk, prev_clusters, new_blk);
3575 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3576 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3585 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3587 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3588 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3592 ret = ocfs2_half_xattr_cluster(inode, handle,
3596 if ((*header_bh)->b_blocknr == last_blk && extend)
3605 * Add a new cluster for xattr storage.
3607 * If the new cluster is contiguous with the previous one, it will be
3608 * appended to the same extent record, and num_clusters will be updated.
3609 * If not, we will insert a new extent for it and move some xattrs in
3610 * the last cluster into the new allocated one.
3611 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3612 * lose the benefits of hashing because we'll have to search large leaves.
3613 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3616 * first_bh is the first block of the previous extent rec and header_bh
3617 * indicates the bucket we will insert the new xattrs. They will be updated
3618 * when the header_bh is moved into the new cluster.
3620 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3621 struct buffer_head *root_bh,
3622 struct buffer_head **first_bh,
3623 struct buffer_head **header_bh,
3630 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3631 u32 prev_clusters = *num_clusters;
3632 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3634 handle_t *handle = NULL;
3635 struct ocfs2_alloc_context *data_ac = NULL;
3636 struct ocfs2_alloc_context *meta_ac = NULL;
3637 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3638 struct ocfs2_extent_tree et;
3640 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3641 "previous xattr blkno = %llu\n",
3642 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3643 prev_cpos, prev_blkno);
3645 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
3647 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3648 &data_ac, &meta_ac);
3654 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3656 handle = ocfs2_start_trans(osb, credits);
3657 if (IS_ERR(handle)) {
3658 ret = PTR_ERR(handle);
3664 ret = ocfs2_journal_access(handle, inode, root_bh,
3665 OCFS2_JOURNAL_ACCESS_WRITE);
3671 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3672 clusters_to_add, &bit_off, &num_bits);
3679 BUG_ON(num_bits > clusters_to_add);
3681 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3682 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3683 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3685 if (prev_blkno + prev_clusters * bpc == block &&
3686 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3687 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3689 * If this cluster is contiguous with the old one and
3690 * adding this new cluster, we don't surpass the limit of
3691 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3692 * initialized and used like other buckets in the previous
3694 * So add it as a contiguous one. The caller will handle
3697 v_start = prev_cpos + prev_clusters;
3698 *num_clusters = prev_clusters + num_bits;
3699 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3702 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3717 if (handle->h_buffer_credits < credits) {
3719 * The journal has been restarted before, and don't
3720 * have enough space for the insertion, so extend it
3723 ret = ocfs2_extend_trans(handle, credits);
3729 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3730 num_bits, block, v_start);
3731 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3732 num_bits, 0, meta_ac);
3738 ret = ocfs2_journal_dirty(handle, root_bh);
3746 ocfs2_commit_trans(osb, handle);
3748 ocfs2_free_alloc_context(data_ac);
3750 ocfs2_free_alloc_context(meta_ac);
3756 * Extend a new xattr bucket and move xattrs to the end one by one until
3757 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3759 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3760 struct buffer_head *first_bh,
3761 struct buffer_head *start_bh,
3765 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3766 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3767 u64 start_blk = start_bh->b_blocknr, end_blk;
3768 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3770 struct ocfs2_xattr_header *first_xh =
3771 (struct ocfs2_xattr_header *)first_bh->b_data;
3772 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3774 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3775 "from %llu, len = %u\n", start_blk,
3776 (unsigned long long)first_bh->b_blocknr, num_clusters);
3778 BUG_ON(bucket >= num_buckets);
3780 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3783 * We will touch all the buckets after the start_bh(include it).
3784 * Add one more bucket and modify the first_bh.
3786 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3787 handle = ocfs2_start_trans(osb, credits);
3788 if (IS_ERR(handle)) {
3789 ret = PTR_ERR(handle);
3795 ret = ocfs2_journal_access(handle, inode, first_bh,
3796 OCFS2_JOURNAL_ACCESS_WRITE);
3802 while (end_blk != start_blk) {
3803 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3804 end_blk + blk_per_bucket, 0);
3807 end_blk -= blk_per_bucket;
3810 /* Move half of the xattr in start_blk to the next bucket. */
3811 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3812 start_blk + blk_per_bucket, NULL, 0);
3814 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3815 ocfs2_journal_dirty(handle, first_bh);
3818 ocfs2_commit_trans(osb, handle);
3824 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3825 * xb_bh is the ocfs2_xattr_block.
3826 * We will move all the buckets starting from header_bh to the next place. As
3827 * for this one, half num of its xattrs will be moved to the next one.
3829 * We will allocate a new cluster if current cluster is full and adjust
3830 * header_bh and first_bh if the insert place is moved to the new cluster.
3832 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3833 struct buffer_head *xb_bh,
3834 struct buffer_head *header_bh)
3836 struct ocfs2_xattr_header *first_xh = NULL;
3837 struct buffer_head *first_bh = NULL;
3838 struct ocfs2_xattr_block *xb =
3839 (struct ocfs2_xattr_block *)xb_bh->b_data;
3840 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3841 struct ocfs2_extent_list *el = &xb_root->xt_list;
3842 struct ocfs2_xattr_header *xh =
3843 (struct ocfs2_xattr_header *)header_bh->b_data;
3844 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3845 struct super_block *sb = inode->i_sb;
3846 struct ocfs2_super *osb = OCFS2_SB(sb);
3847 int ret, num_buckets, extend = 1;
3849 u32 e_cpos, num_clusters;
3851 mlog(0, "Add new xattr bucket starting form %llu\n",
3852 (unsigned long long)header_bh->b_blocknr);
3855 * Add refrence for header_bh here because it may be
3856 * changed in ocfs2_add_new_xattr_cluster and we need
3857 * to free it in the end.
3861 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3868 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
3874 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3875 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3877 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3878 ret = ocfs2_add_new_xattr_cluster(inode,
3893 ret = ocfs2_extend_xattr_bucket(inode,
3905 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3906 struct ocfs2_xattr_bucket *bucket,
3909 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3911 offs = offs % inode->i_sb->s_blocksize;
3912 return bucket->bhs[block_off]->b_data + offs;
3916 * Handle the normal xattr set, including replace, delete and new.
3918 * Note: "local" indicates the real data's locality. So we can't
3919 * just its bucket locality by its length.
3921 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3922 struct ocfs2_xattr_info *xi,
3923 struct ocfs2_xattr_search *xs,
3927 struct ocfs2_xattr_entry *last, *xe;
3928 int name_len = strlen(xi->name);
3929 struct ocfs2_xattr_header *xh = xs->header;
3930 u16 count = le16_to_cpu(xh->xh_count), start;
3931 size_t blocksize = inode->i_sb->s_blocksize;
3933 size_t offs, size, new_size;
3935 last = &xh->xh_entries[count];
3936 if (!xs->not_found) {
3938 offs = le16_to_cpu(xe->xe_name_offset);
3939 if (ocfs2_xattr_is_local(xe))
3940 size = OCFS2_XATTR_SIZE(name_len) +
3941 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3943 size = OCFS2_XATTR_SIZE(name_len) +
3944 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3947 * If the new value will be stored outside, xi->value has been
3948 * initalized as an empty ocfs2_xattr_value_root, and the same
3949 * goes with xi->value_len, so we can set new_size safely here.
3950 * See ocfs2_xattr_set_in_bucket.
3952 new_size = OCFS2_XATTR_SIZE(name_len) +
3953 OCFS2_XATTR_SIZE(xi->value_len);
3955 le16_add_cpu(&xh->xh_name_value_len, -size);
3957 if (new_size > size)
3958 goto set_new_name_value;
3960 /* Now replace the old value with new one. */
3962 xe->xe_value_size = cpu_to_le64(xi->value_len);
3964 xe->xe_value_size = 0;
3966 val = ocfs2_xattr_bucket_get_val(inode,
3968 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3969 size - OCFS2_XATTR_SIZE(name_len));
3970 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3971 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3972 xi->value, xi->value_len);
3974 le16_add_cpu(&xh->xh_name_value_len, new_size);
3975 ocfs2_xattr_set_local(xe, local);
3979 * Remove the old entry if there is more than one.
3980 * We don't remove the last entry so that we can
3981 * use it to indicate the hash value of the empty
3985 le16_add_cpu(&xh->xh_count, -1);
3988 (void *)last - (void *)xe);
3990 sizeof(struct ocfs2_xattr_entry));
3993 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3998 /* find a new entry for insert. */
3999 int low = 0, high = count - 1, tmp;
4000 struct ocfs2_xattr_entry *tmp_xe;
4002 while (low <= high && count) {
4003 tmp = (low + high) / 2;
4004 tmp_xe = &xh->xh_entries[tmp];
4006 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4008 else if (name_hash <
4009 le32_to_cpu(tmp_xe->xe_name_hash))
4017 xe = &xh->xh_entries[low];
4019 memmove(xe + 1, xe, (void *)last - (void *)xe);
4021 le16_add_cpu(&xh->xh_count, 1);
4022 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4023 xe->xe_name_hash = cpu_to_le32(name_hash);
4024 xe->xe_name_len = name_len;
4025 ocfs2_xattr_set_type(xe, xi->name_index);
4029 /* Insert the new name+value. */
4030 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4033 * We must make sure that the name/value pair
4034 * exists in the same block.
4036 offs = le16_to_cpu(xh->xh_free_start);
4037 start = offs - size;
4039 if (start >> inode->i_sb->s_blocksize_bits !=
4040 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4041 offs = offs - offs % blocksize;
4042 xh->xh_free_start = cpu_to_le16(offs);
4045 val = ocfs2_xattr_bucket_get_val(inode,
4046 &xs->bucket, offs - size);
4047 xe->xe_name_offset = cpu_to_le16(offs - size);
4049 memset(val, 0, size);
4050 memcpy(val, xi->name, name_len);
4051 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4053 xe->xe_value_size = cpu_to_le64(xi->value_len);
4054 ocfs2_xattr_set_local(xe, local);
4056 le16_add_cpu(&xh->xh_free_start, -size);
4057 le16_add_cpu(&xh->xh_name_value_len, size);
4062 static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4064 struct ocfs2_xattr_search *xs,
4065 struct buffer_head **bhs,
4068 int ret = 0, off, block_off;
4069 struct ocfs2_xattr_entry *xe = xs->here;
4072 * First calculate all the blocks we should journal_access
4073 * and journal_dirty. The first block should always be touched.
4075 ret = ocfs2_journal_dirty(handle, bhs[0]);
4079 /* calc the data. */
4080 off = le16_to_cpu(xe->xe_name_offset);
4081 block_off = off >> inode->i_sb->s_blocksize_bits;
4082 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4090 * Set the xattr entry in the specified bucket.
4091 * The bucket is indicated by xs->bucket and it should have the enough
4092 * space for the xattr insertion.
4094 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4095 struct ocfs2_xattr_info *xi,
4096 struct ocfs2_xattr_search *xs,
4101 handle_t *handle = NULL;
4102 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4103 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4105 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4106 (unsigned long)xi->value_len, xi->name_index,
4107 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4109 if (!xs->bucket.bhs[1]) {
4110 ret = ocfs2_read_blocks(inode,
4111 xs->bucket.bhs[0]->b_blocknr + 1,
4112 blk_per_bucket - 1, &xs->bucket.bhs[1],
4120 handle = ocfs2_start_trans(osb, blk_per_bucket);
4121 if (IS_ERR(handle)) {
4122 ret = PTR_ERR(handle);
4128 for (i = 0; i < blk_per_bucket; i++) {
4129 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4130 OCFS2_JOURNAL_ACCESS_WRITE);
4137 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4139 /*Only dirty the blocks we have touched in set xattr. */
4140 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4141 xs->bucket.bhs, blk_per_bucket);
4145 ocfs2_commit_trans(osb, handle);
4150 static int ocfs2_xattr_value_update_size(struct inode *inode,
4151 struct buffer_head *xe_bh,
4152 struct ocfs2_xattr_entry *xe,
4156 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4157 handle_t *handle = NULL;
4159 handle = ocfs2_start_trans(osb, 1);
4160 if (handle == NULL) {
4166 ret = ocfs2_journal_access(handle, inode, xe_bh,
4167 OCFS2_JOURNAL_ACCESS_WRITE);
4173 xe->xe_value_size = cpu_to_le64(new_size);
4175 ret = ocfs2_journal_dirty(handle, xe_bh);
4180 ocfs2_commit_trans(osb, handle);
4186 * Truncate the specified xe_off entry in xattr bucket.
4187 * bucket is indicated by header_bh and len is the new length.
4188 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4190 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4192 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4193 struct buffer_head *header_bh,
4199 struct buffer_head *value_bh = NULL;
4200 struct ocfs2_xattr_value_root *xv;
4201 struct ocfs2_xattr_entry *xe;
4202 struct ocfs2_xattr_header *xh =
4203 (struct ocfs2_xattr_header *)header_bh->b_data;
4204 size_t blocksize = inode->i_sb->s_blocksize;
4206 xe = &xh->xh_entries[xe_off];
4208 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4210 offset = le16_to_cpu(xe->xe_name_offset) +
4211 OCFS2_XATTR_SIZE(xe->xe_name_len);
4213 value_blk = offset / blocksize;
4215 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4216 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4217 value_blk += header_bh->b_blocknr;
4219 ret = ocfs2_read_block(inode, value_blk, &value_bh);
4225 xv = (struct ocfs2_xattr_value_root *)
4226 (value_bh->b_data + offset % blocksize);
4228 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4229 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4230 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4236 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4247 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4248 struct ocfs2_xattr_search *xs,
4252 struct ocfs2_xattr_entry *xe = xs->here;
4253 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4255 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4257 offset = xe - xh->xh_entries;
4258 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4266 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4267 struct ocfs2_xattr_search *xs,
4272 struct ocfs2_xattr_value_root *xv;
4273 struct ocfs2_xattr_entry *xe = xs->here;
4275 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4277 offset = le16_to_cpu(xe->xe_name_offset) +
4278 OCFS2_XATTR_SIZE(xe->xe_name_len);
4280 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4282 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4285 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4286 struct buffer_head *root_bh,
4292 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4293 struct inode *tl_inode = osb->osb_tl_inode;
4295 struct ocfs2_xattr_block *xb =
4296 (struct ocfs2_xattr_block *)root_bh->b_data;
4297 struct ocfs2_alloc_context *meta_ac = NULL;
4298 struct ocfs2_cached_dealloc_ctxt dealloc;
4299 struct ocfs2_extent_tree et;
4301 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4303 ocfs2_init_dealloc_ctxt(&dealloc);
4305 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4306 cpos, len, (unsigned long long)blkno);
4308 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4310 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4316 mutex_lock(&tl_inode->i_mutex);
4318 if (ocfs2_truncate_log_needs_flush(osb)) {
4319 ret = __ocfs2_flush_truncate_log(osb);
4326 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4327 if (handle == NULL) {
4333 ret = ocfs2_journal_access(handle, inode, root_bh,
4334 OCFS2_JOURNAL_ACCESS_WRITE);
4340 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4347 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4349 ret = ocfs2_journal_dirty(handle, root_bh);
4355 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4360 ocfs2_commit_trans(osb, handle);
4362 ocfs2_schedule_truncate_log_flush(osb, 1);
4364 mutex_unlock(&tl_inode->i_mutex);
4367 ocfs2_free_alloc_context(meta_ac);
4369 ocfs2_run_deallocs(osb, &dealloc);
4374 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4375 struct ocfs2_xattr_search *xs)
4377 handle_t *handle = NULL;
4378 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4379 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4380 le16_to_cpu(xh->xh_count) - 1];
4383 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4384 if (IS_ERR(handle)) {
4385 ret = PTR_ERR(handle);
4390 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4391 OCFS2_JOURNAL_ACCESS_WRITE);
4397 /* Remove the old entry. */
4398 memmove(xs->here, xs->here + 1,
4399 (void *)last - (void *)xs->here);
4400 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4401 le16_add_cpu(&xh->xh_count, -1);
4403 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4407 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4411 * Set the xattr name/value in the bucket specified in xs.
4413 * As the new value in xi may be stored in the bucket or in an outside cluster,
4414 * we divide the whole process into 3 steps:
4415 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4416 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4417 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4418 * 4. If the clusters for the new outside value can't be allocated, we need
4419 * to free the xattr we allocated in set.
4421 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4422 struct ocfs2_xattr_info *xi,
4423 struct ocfs2_xattr_search *xs)
4427 char *val = (char *)xi->value;
4428 struct ocfs2_xattr_entry *xe = xs->here;
4429 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4432 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4434 * We need to truncate the xattr storage first.
4436 * If both the old and new value are stored to
4437 * outside block, we only need to truncate
4438 * the storage and then set the value outside.
4440 * If the new value should be stored within block,
4441 * we should free all the outside block first and
4442 * the modification to the xattr block will be done
4443 * by following steps.
4445 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4446 value_len = xi->value_len;
4450 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4456 goto set_value_outside;
4459 value_len = xi->value_len;
4460 /* So we have to handle the inside block change now. */
4461 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4463 * If the new value will be stored outside of block,
4464 * initalize a new empty value root and insert it first.
4467 xi->value = &def_xv;
4468 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4471 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
4477 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4480 /* allocate the space now for the outside block storage. */
4481 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4486 if (xs->not_found) {
4488 * We can't allocate enough clusters for outside
4489 * storage and we have allocated xattr already,
4490 * so need to remove it.
4492 ocfs2_xattr_bucket_remove_xs(inode, xs);
4498 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4503 /* check whether the xattr bucket is filled up with the same hash value. */
4504 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4505 struct ocfs2_xattr_bucket *bucket)
4507 struct ocfs2_xattr_header *xh = bucket->xh;
4509 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4510 xh->xh_entries[0].xe_name_hash) {
4511 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4513 (unsigned long long)bucket->bhs[0]->b_blocknr,
4514 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4521 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4522 struct ocfs2_xattr_info *xi,
4523 struct ocfs2_xattr_search *xs)
4525 struct ocfs2_xattr_header *xh;
4526 struct ocfs2_xattr_entry *xe;
4527 u16 count, header_size, xh_free_start;
4528 int i, free, max_free, need, old;
4529 size_t value_size = 0, name_len = strlen(xi->name);
4530 size_t blocksize = inode->i_sb->s_blocksize;
4531 int ret, allocation = 0;
4532 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4534 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4538 count = le16_to_cpu(xh->xh_count);
4539 xh_free_start = le16_to_cpu(xh->xh_free_start);
4540 header_size = sizeof(struct ocfs2_xattr_header) +
4541 count * sizeof(struct ocfs2_xattr_entry);
4542 max_free = OCFS2_XATTR_BUCKET_SIZE -
4543 le16_to_cpu(xh->xh_name_value_len) - header_size;
4545 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4546 "of %u which exceed block size\n",
4547 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4550 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4551 value_size = OCFS2_XATTR_ROOT_SIZE;
4553 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4556 need = sizeof(struct ocfs2_xattr_entry) +
4557 OCFS2_XATTR_SIZE(name_len) + value_size;
4559 need = value_size + OCFS2_XATTR_SIZE(name_len);
4562 * We only replace the old value if the new length is smaller
4563 * than the old one. Otherwise we will allocate new space in the
4564 * bucket to store it.
4567 if (ocfs2_xattr_is_local(xe))
4568 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4570 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4572 if (old >= value_size)
4576 free = xh_free_start - header_size;
4578 * We need to make sure the new name/value pair
4579 * can exist in the same block.
4581 if (xh_free_start % blocksize < need)
4582 free -= xh_free_start % blocksize;
4584 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4585 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4586 " %u\n", xs->not_found,
4587 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4588 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4589 le16_to_cpu(xh->xh_name_value_len));
4591 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4592 if (need <= max_free &&
4593 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4595 * We can create the space by defragment. Since only the
4596 * name/value will be moved, the xe shouldn't be changed
4599 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4605 xh_free_start = le16_to_cpu(xh->xh_free_start);
4606 free = xh_free_start - header_size;
4607 if (xh_free_start % blocksize < need)
4608 free -= xh_free_start % blocksize;
4613 mlog(0, "Can't get enough space for xattr insert by "
4614 "defragment. Need %u bytes, but we have %d, so "
4615 "allocate new bucket for it.\n", need, free);
4619 * We have to add new buckets or clusters and one
4620 * allocation should leave us enough space for insert.
4625 * We do not allow for overlapping ranges between buckets. And
4626 * the maximum number of collisions we will allow for then is
4627 * one bucket's worth, so check it here whether we need to
4628 * add a new bucket for the insert.
4630 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4636 ret = ocfs2_add_new_xattr_bucket(inode,
4644 for (i = 0; i < blk_per_bucket; i++)
4645 brelse(xs->bucket.bhs[i]);
4647 memset(&xs->bucket, 0, sizeof(xs->bucket));
4649 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4652 if (ret && ret != -ENODATA)
4654 xs->not_found = ret;
4660 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4666 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4667 struct ocfs2_xattr_bucket *bucket,
4671 struct ocfs2_xattr_header *xh = bucket->xh;
4673 struct ocfs2_xattr_entry *xe;
4675 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4676 xe = &xh->xh_entries[i];
4677 if (ocfs2_xattr_is_local(xe))
4680 ret = ocfs2_xattr_bucket_value_truncate(inode,
4692 static int ocfs2_delete_xattr_index_block(struct inode *inode,
4693 struct buffer_head *xb_bh)
4695 struct ocfs2_xattr_block *xb =
4696 (struct ocfs2_xattr_block *)xb_bh->b_data;
4697 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4699 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4702 if (le16_to_cpu(el->l_next_free_rec) == 0)
4705 while (name_hash > 0) {
4706 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4707 &e_cpos, &num_clusters, el);
4713 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4714 ocfs2_delete_xattr_in_bucket,
4721 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4722 p_blkno, e_cpos, num_clusters);
4731 name_hash = e_cpos - 1;
4739 * 'trusted' attributes support
4741 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4742 size_t list_size, const char *name,
4745 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
4746 const size_t total_len = prefix_len + name_len + 1;
4748 if (list && total_len <= list_size) {
4749 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4750 memcpy(list + prefix_len, name, name_len);
4751 list[prefix_len + name_len] = '\0';
4756 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4757 void *buffer, size_t size)
4759 if (strcmp(name, "") == 0)
4761 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4765 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4766 const void *value, size_t size, int flags)
4768 if (strcmp(name, "") == 0)
4771 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4775 struct xattr_handler ocfs2_xattr_trusted_handler = {
4776 .prefix = XATTR_TRUSTED_PREFIX,
4777 .list = ocfs2_xattr_trusted_list,
4778 .get = ocfs2_xattr_trusted_get,
4779 .set = ocfs2_xattr_trusted_set,
4783 * 'user' attributes support
4785 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4786 size_t list_size, const char *name,
4789 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
4790 const size_t total_len = prefix_len + name_len + 1;
4791 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4793 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4796 if (list && total_len <= list_size) {
4797 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4798 memcpy(list + prefix_len, name, name_len);
4799 list[prefix_len + name_len] = '\0';
4804 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4805 void *buffer, size_t size)
4807 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4809 if (strcmp(name, "") == 0)
4811 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4813 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4817 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4818 const void *value, size_t size, int flags)
4820 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4822 if (strcmp(name, "") == 0)
4824 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4827 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4831 struct xattr_handler ocfs2_xattr_user_handler = {
4832 .prefix = XATTR_USER_PREFIX,
4833 .list = ocfs2_xattr_user_list,
4834 .get = ocfs2_xattr_user_get,
4835 .set = ocfs2_xattr_user_set,