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_block_find(struct inode *inode,
117 struct ocfs2_xattr_search *xs);
118 static int ocfs2_xattr_index_block_find(struct inode *inode,
119 struct buffer_head *root_bh,
122 struct ocfs2_xattr_search *xs);
124 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
125 struct ocfs2_xattr_tree_root *xt,
129 static int ocfs2_xattr_create_index_block(struct inode *inode,
130 struct ocfs2_xattr_search *xs);
132 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
133 struct ocfs2_xattr_info *xi,
134 struct ocfs2_xattr_search *xs);
136 static int ocfs2_delete_xattr_index_block(struct inode *inode,
137 struct buffer_head *xb_bh);
139 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
141 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
144 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
146 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
149 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
151 u16 len = sb->s_blocksize -
152 offsetof(struct ocfs2_xattr_header, xh_entries);
154 return len / sizeof(struct ocfs2_xattr_entry);
157 static inline const char *ocfs2_xattr_prefix(int name_index)
159 struct xattr_handler *handler = NULL;
161 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
162 handler = ocfs2_xattr_handler_map[name_index];
164 return handler ? handler->prefix : NULL;
167 static u32 ocfs2_xattr_name_hash(struct inode *inode,
171 /* Get hash value of uuid from super block */
172 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
175 /* hash extended attribute name */
176 for (i = 0; i < name_len; i++) {
177 hash = (hash << OCFS2_HASH_SHIFT) ^
178 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
186 * ocfs2_xattr_hash_entry()
188 * Compute the hash of an extended attribute.
190 static void ocfs2_xattr_hash_entry(struct inode *inode,
191 struct ocfs2_xattr_header *header,
192 struct ocfs2_xattr_entry *entry)
195 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
197 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
198 entry->xe_name_hash = cpu_to_le32(hash);
203 static int ocfs2_xattr_extend_allocation(struct inode *inode,
205 struct buffer_head *xattr_bh,
206 struct ocfs2_xattr_value_root *xv)
209 int restart_func = 0;
211 handle_t *handle = NULL;
212 struct ocfs2_alloc_context *data_ac = NULL;
213 struct ocfs2_alloc_context *meta_ac = NULL;
214 enum ocfs2_alloc_restarted why;
215 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
216 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
217 struct ocfs2_extent_tree et;
219 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
221 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
225 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
232 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
234 handle = ocfs2_start_trans(osb, credits);
235 if (IS_ERR(handle)) {
236 status = PTR_ERR(handle);
242 restarted_transaction:
243 status = ocfs2_journal_access(handle, inode, xattr_bh,
244 OCFS2_JOURNAL_ACCESS_WRITE);
250 prev_clusters = le32_to_cpu(xv->xr_clusters);
251 status = ocfs2_add_clusters_in_btree(osb,
261 if ((status < 0) && (status != -EAGAIN)) {
262 if (status != -ENOSPC)
267 status = ocfs2_journal_dirty(handle, xattr_bh);
273 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
275 if (why != RESTART_NONE && clusters_to_add) {
276 if (why == RESTART_META) {
277 mlog(0, "restarting function.\n");
280 BUG_ON(why != RESTART_TRANS);
282 mlog(0, "restarting transaction.\n");
283 /* TODO: This can be more intelligent. */
284 credits = ocfs2_calc_extend_credits(osb->sb,
287 status = ocfs2_extend_trans(handle, credits);
289 /* handle still has to be committed at
295 goto restarted_transaction;
301 ocfs2_commit_trans(osb, handle);
305 ocfs2_free_alloc_context(data_ac);
309 ocfs2_free_alloc_context(meta_ac);
312 if ((!status) && restart_func) {
320 static int __ocfs2_remove_xattr_range(struct inode *inode,
321 struct buffer_head *root_bh,
322 struct ocfs2_xattr_value_root *xv,
323 u32 cpos, u32 phys_cpos, u32 len,
324 struct ocfs2_cached_dealloc_ctxt *dealloc)
327 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
328 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
329 struct inode *tl_inode = osb->osb_tl_inode;
331 struct ocfs2_alloc_context *meta_ac = NULL;
332 struct ocfs2_extent_tree et;
334 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
336 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
342 mutex_lock(&tl_inode->i_mutex);
344 if (ocfs2_truncate_log_needs_flush(osb)) {
345 ret = __ocfs2_flush_truncate_log(osb);
352 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
353 if (IS_ERR(handle)) {
354 ret = PTR_ERR(handle);
359 ret = ocfs2_journal_access(handle, inode, root_bh,
360 OCFS2_JOURNAL_ACCESS_WRITE);
366 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
373 le32_add_cpu(&xv->xr_clusters, -len);
375 ret = ocfs2_journal_dirty(handle, root_bh);
381 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
386 ocfs2_commit_trans(osb, handle);
388 mutex_unlock(&tl_inode->i_mutex);
391 ocfs2_free_alloc_context(meta_ac);
396 static int ocfs2_xattr_shrink_size(struct inode *inode,
399 struct buffer_head *root_bh,
400 struct ocfs2_xattr_value_root *xv)
403 u32 trunc_len, cpos, phys_cpos, alloc_size;
405 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
406 struct ocfs2_cached_dealloc_ctxt dealloc;
408 ocfs2_init_dealloc_ctxt(&dealloc);
410 if (old_clusters <= new_clusters)
414 trunc_len = old_clusters - new_clusters;
416 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
417 &alloc_size, &xv->xr_list);
423 if (alloc_size > trunc_len)
424 alloc_size = trunc_len;
426 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
427 phys_cpos, alloc_size,
434 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
435 ocfs2_remove_xattr_clusters_from_cache(inode, block,
438 trunc_len -= alloc_size;
442 ocfs2_schedule_truncate_log_flush(osb, 1);
443 ocfs2_run_deallocs(osb, &dealloc);
448 static int ocfs2_xattr_value_truncate(struct inode *inode,
449 struct buffer_head *root_bh,
450 struct ocfs2_xattr_value_root *xv,
454 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
455 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
457 if (new_clusters == old_clusters)
460 if (new_clusters > old_clusters)
461 ret = ocfs2_xattr_extend_allocation(inode,
462 new_clusters - old_clusters,
465 ret = ocfs2_xattr_shrink_size(inode,
466 old_clusters, new_clusters,
472 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
473 size_t *result, const char *prefix,
474 const char *name, int name_len)
476 char *p = buffer + *result;
477 int prefix_len = strlen(prefix);
478 int total_len = prefix_len + name_len + 1;
480 *result += total_len;
482 /* we are just looking for how big our buffer needs to be */
489 memcpy(p, prefix, prefix_len);
490 memcpy(p + prefix_len, name, name_len);
491 p[prefix_len + name_len] = '\0';
496 static int ocfs2_xattr_list_entries(struct inode *inode,
497 struct ocfs2_xattr_header *header,
498 char *buffer, size_t buffer_size)
502 const char *prefix, *name;
504 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
505 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
506 type = ocfs2_xattr_get_type(entry);
507 prefix = ocfs2_xattr_prefix(type);
510 name = (const char *)header +
511 le16_to_cpu(entry->xe_name_offset);
513 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
514 &result, prefix, name,
524 static int ocfs2_xattr_ibody_list(struct inode *inode,
525 struct ocfs2_dinode *di,
529 struct ocfs2_xattr_header *header = NULL;
530 struct ocfs2_inode_info *oi = OCFS2_I(inode);
533 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
536 header = (struct ocfs2_xattr_header *)
537 ((void *)di + inode->i_sb->s_blocksize -
538 le16_to_cpu(di->i_xattr_inline_size));
540 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
545 static int ocfs2_xattr_block_list(struct inode *inode,
546 struct ocfs2_dinode *di,
550 struct buffer_head *blk_bh = NULL;
551 struct ocfs2_xattr_block *xb;
554 if (!di->i_xattr_loc)
557 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
563 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
564 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
569 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
570 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
571 ret = ocfs2_xattr_list_entries(inode, header,
572 buffer, buffer_size);
574 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
575 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
576 buffer, buffer_size);
584 ssize_t ocfs2_listxattr(struct dentry *dentry,
588 int ret = 0, i_ret = 0, b_ret = 0;
589 struct buffer_head *di_bh = NULL;
590 struct ocfs2_dinode *di = NULL;
591 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
593 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
596 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
599 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
605 di = (struct ocfs2_dinode *)di_bh->b_data;
607 down_read(&oi->ip_xattr_sem);
608 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
616 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
621 up_read(&oi->ip_xattr_sem);
622 ocfs2_inode_unlock(dentry->d_inode, 0);
626 return i_ret + b_ret;
629 static int ocfs2_xattr_find_entry(int name_index,
631 struct ocfs2_xattr_search *xs)
633 struct ocfs2_xattr_entry *entry;
640 name_len = strlen(name);
642 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
643 cmp = name_index - ocfs2_xattr_get_type(entry);
645 cmp = name_len - entry->xe_name_len;
647 cmp = memcmp(name, (xs->base +
648 le16_to_cpu(entry->xe_name_offset)),
656 return cmp ? -ENODATA : 0;
659 static int ocfs2_xattr_get_value_outside(struct inode *inode,
660 struct ocfs2_xattr_value_root *xv,
664 u32 cpos, p_cluster, num_clusters, bpc, clusters;
667 size_t cplen, blocksize;
668 struct buffer_head *bh = NULL;
669 struct ocfs2_extent_list *el;
672 clusters = le32_to_cpu(xv->xr_clusters);
673 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
674 blocksize = inode->i_sb->s_blocksize;
677 while (cpos < clusters) {
678 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
685 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
686 /* Copy ocfs2_xattr_value */
687 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
688 ret = ocfs2_read_block(inode, blkno, &bh);
694 cplen = len >= blocksize ? blocksize : len;
695 memcpy(buffer, bh->b_data, cplen);
704 cpos += num_clusters;
710 static int ocfs2_xattr_ibody_get(struct inode *inode,
715 struct ocfs2_xattr_search *xs)
717 struct ocfs2_inode_info *oi = OCFS2_I(inode);
718 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
719 struct ocfs2_xattr_value_root *xv;
723 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
726 xs->end = (void *)di + inode->i_sb->s_blocksize;
727 xs->header = (struct ocfs2_xattr_header *)
728 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
729 xs->base = (void *)xs->header;
730 xs->here = xs->header->xh_entries;
732 ret = ocfs2_xattr_find_entry(name_index, name, xs);
735 size = le64_to_cpu(xs->here->xe_value_size);
737 if (size > buffer_size)
739 if (ocfs2_xattr_is_local(xs->here)) {
740 memcpy(buffer, (void *)xs->base +
741 le16_to_cpu(xs->here->xe_name_offset) +
742 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
744 xv = (struct ocfs2_xattr_value_root *)
745 (xs->base + le16_to_cpu(
746 xs->here->xe_name_offset) +
747 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
748 ret = ocfs2_xattr_get_value_outside(inode, xv,
760 static int ocfs2_xattr_block_get(struct inode *inode,
765 struct ocfs2_xattr_search *xs)
767 struct ocfs2_xattr_block *xb;
768 struct ocfs2_xattr_value_root *xv;
770 int ret = -ENODATA, name_offset, name_len, block_off, i;
772 memset(&xs->bucket, 0, sizeof(xs->bucket));
774 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
780 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
781 size = le64_to_cpu(xs->here->xe_value_size);
784 if (size > buffer_size)
787 name_offset = le16_to_cpu(xs->here->xe_name_offset);
788 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
789 i = xs->here - xs->header->xh_entries;
791 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
792 ret = ocfs2_xattr_bucket_get_name_value(inode,
797 xs->base = xs->bucket.bhs[block_off]->b_data;
799 if (ocfs2_xattr_is_local(xs->here)) {
800 memcpy(buffer, (void *)xs->base +
801 name_offset + name_len, size);
803 xv = (struct ocfs2_xattr_value_root *)
804 (xs->base + name_offset + name_len);
805 ret = ocfs2_xattr_get_value_outside(inode, xv,
815 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
816 brelse(xs->bucket.bhs[i]);
817 memset(&xs->bucket, 0, sizeof(xs->bucket));
819 brelse(xs->xattr_bh);
826 * Copy an extended attribute into the buffer provided.
827 * Buffer is NULL to compute the size of buffer required.
829 static int ocfs2_xattr_get(struct inode *inode,
836 struct ocfs2_dinode *di = NULL;
837 struct buffer_head *di_bh = NULL;
838 struct ocfs2_inode_info *oi = OCFS2_I(inode);
839 struct ocfs2_xattr_search xis = {
840 .not_found = -ENODATA,
842 struct ocfs2_xattr_search xbs = {
843 .not_found = -ENODATA,
846 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
849 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
852 ret = ocfs2_inode_lock(inode, &di_bh, 0);
857 xis.inode_bh = xbs.inode_bh = di_bh;
858 di = (struct ocfs2_dinode *)di_bh->b_data;
860 down_read(&oi->ip_xattr_sem);
861 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
864 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
866 up_read(&oi->ip_xattr_sem);
867 ocfs2_inode_unlock(inode, 0);
874 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
875 struct ocfs2_xattr_value_root *xv,
879 int ret = 0, i, cp_len, credits;
880 u16 blocksize = inode->i_sb->s_blocksize;
881 u32 p_cluster, num_clusters;
882 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
883 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
885 struct buffer_head *bh = NULL;
888 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
890 credits = clusters * bpc;
891 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
892 if (IS_ERR(handle)) {
893 ret = PTR_ERR(handle);
898 while (cpos < clusters) {
899 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
900 &num_clusters, &xv->xr_list);
906 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
908 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
909 ret = ocfs2_read_block(inode, blkno, &bh);
915 ret = ocfs2_journal_access(handle,
918 OCFS2_JOURNAL_ACCESS_WRITE);
924 cp_len = value_len > blocksize ? blocksize : value_len;
925 memcpy(bh->b_data, value, cp_len);
928 if (cp_len < blocksize)
929 memset(bh->b_data + cp_len, 0,
932 ret = ocfs2_journal_dirty(handle, bh);
941 * XXX: do we need to empty all the following
942 * blocks in this cluster?
947 cpos += num_clusters;
950 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
957 static int ocfs2_xattr_cleanup(struct inode *inode,
958 struct ocfs2_xattr_info *xi,
959 struct ocfs2_xattr_search *xs,
962 handle_t *handle = NULL;
964 size_t name_len = strlen(xi->name);
965 void *val = xs->base + offs;
966 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
968 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
969 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
970 if (IS_ERR(handle)) {
971 ret = PTR_ERR(handle);
975 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
976 OCFS2_JOURNAL_ACCESS_WRITE);
981 /* Decrease xattr count */
982 le16_add_cpu(&xs->header->xh_count, -1);
983 /* Remove the xattr entry and tree root which has already be set*/
984 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
985 memset(val, 0, size);
987 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
991 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
996 static int ocfs2_xattr_update_entry(struct inode *inode,
997 struct ocfs2_xattr_info *xi,
998 struct ocfs2_xattr_search *xs,
1001 handle_t *handle = NULL;
1004 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1005 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1006 if (IS_ERR(handle)) {
1007 ret = PTR_ERR(handle);
1011 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1012 OCFS2_JOURNAL_ACCESS_WRITE);
1018 xs->here->xe_name_offset = cpu_to_le16(offs);
1019 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1020 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1021 ocfs2_xattr_set_local(xs->here, 1);
1023 ocfs2_xattr_set_local(xs->here, 0);
1024 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1026 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1030 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1036 * ocfs2_xattr_set_value_outside()
1038 * Set large size value in B tree.
1040 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1041 struct ocfs2_xattr_info *xi,
1042 struct ocfs2_xattr_search *xs,
1045 size_t name_len = strlen(xi->name);
1046 void *val = xs->base + offs;
1047 struct ocfs2_xattr_value_root *xv = NULL;
1048 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1051 memset(val, 0, size);
1052 memcpy(val, xi->name, name_len);
1053 xv = (struct ocfs2_xattr_value_root *)
1054 (val + OCFS2_XATTR_SIZE(name_len));
1055 xv->xr_clusters = 0;
1056 xv->xr_last_eb_blk = 0;
1057 xv->xr_list.l_tree_depth = 0;
1058 xv->xr_list.l_count = cpu_to_le16(1);
1059 xv->xr_list.l_next_free_rec = 0;
1061 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1067 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1073 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1081 * ocfs2_xattr_set_entry_local()
1083 * Set, replace or remove extended attribute in local.
1085 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1086 struct ocfs2_xattr_info *xi,
1087 struct ocfs2_xattr_search *xs,
1088 struct ocfs2_xattr_entry *last,
1091 size_t name_len = strlen(xi->name);
1094 if (xi->value && xs->not_found) {
1095 /* Insert the new xattr entry. */
1096 le16_add_cpu(&xs->header->xh_count, 1);
1097 ocfs2_xattr_set_type(last, xi->name_index);
1098 ocfs2_xattr_set_local(last, 1);
1099 last->xe_name_len = name_len;
1105 first_val = xs->base + min_offs;
1106 offs = le16_to_cpu(xs->here->xe_name_offset);
1107 val = xs->base + offs;
1109 if (le64_to_cpu(xs->here->xe_value_size) >
1110 OCFS2_XATTR_INLINE_SIZE)
1111 size = OCFS2_XATTR_SIZE(name_len) +
1112 OCFS2_XATTR_ROOT_SIZE;
1114 size = OCFS2_XATTR_SIZE(name_len) +
1115 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1117 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1118 OCFS2_XATTR_SIZE(xi->value_len)) {
1119 /* The old and the new value have the
1120 same size. Just replace the value. */
1121 ocfs2_xattr_set_local(xs->here, 1);
1122 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1123 /* Clear value bytes. */
1124 memset(val + OCFS2_XATTR_SIZE(name_len),
1126 OCFS2_XATTR_SIZE(xi->value_len));
1127 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1132 /* Remove the old name+value. */
1133 memmove(first_val + size, first_val, val - first_val);
1134 memset(first_val, 0, size);
1135 xs->here->xe_name_hash = 0;
1136 xs->here->xe_name_offset = 0;
1137 ocfs2_xattr_set_local(xs->here, 1);
1138 xs->here->xe_value_size = 0;
1142 /* Adjust all value offsets. */
1143 last = xs->header->xh_entries;
1144 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1145 size_t o = le16_to_cpu(last->xe_name_offset);
1148 last->xe_name_offset = cpu_to_le16(o + size);
1153 /* Remove the old entry. */
1155 memmove(xs->here, xs->here + 1,
1156 (void *)last - (void *)xs->here);
1157 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1158 le16_add_cpu(&xs->header->xh_count, -1);
1162 /* Insert the new name+value. */
1163 size_t size = OCFS2_XATTR_SIZE(name_len) +
1164 OCFS2_XATTR_SIZE(xi->value_len);
1165 void *val = xs->base + min_offs - size;
1167 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1168 memset(val, 0, size);
1169 memcpy(val, xi->name, name_len);
1170 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1173 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1174 ocfs2_xattr_set_local(xs->here, 1);
1175 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1182 * ocfs2_xattr_set_entry()
1184 * Set extended attribute entry into inode or block.
1186 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1187 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1188 * then set value in B tree with set_value_outside().
1190 static int ocfs2_xattr_set_entry(struct inode *inode,
1191 struct ocfs2_xattr_info *xi,
1192 struct ocfs2_xattr_search *xs,
1195 struct ocfs2_xattr_entry *last;
1196 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1197 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1198 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1200 handle_t *handle = NULL;
1202 struct ocfs2_xattr_info xi_l = {
1203 .name_index = xi->name_index,
1206 .value_len = xi->value_len,
1209 /* Compute min_offs, last and free space. */
1210 last = xs->header->xh_entries;
1212 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1213 size_t offs = le16_to_cpu(last->xe_name_offset);
1214 if (offs < min_offs)
1219 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1223 if (!xs->not_found) {
1225 if (ocfs2_xattr_is_local(xs->here))
1226 size = OCFS2_XATTR_SIZE(name_len) +
1227 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1229 size = OCFS2_XATTR_SIZE(name_len) +
1230 OCFS2_XATTR_ROOT_SIZE;
1231 free += (size + sizeof(struct ocfs2_xattr_entry));
1233 /* Check free space in inode or block */
1234 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1235 if (free < sizeof(struct ocfs2_xattr_entry) +
1236 OCFS2_XATTR_SIZE(name_len) +
1237 OCFS2_XATTR_ROOT_SIZE) {
1241 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1242 xi_l.value = (void *)&def_xv;
1243 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1244 } else if (xi->value) {
1245 if (free < sizeof(struct ocfs2_xattr_entry) +
1246 OCFS2_XATTR_SIZE(name_len) +
1247 OCFS2_XATTR_SIZE(xi->value_len)) {
1253 if (!xs->not_found) {
1254 /* For existing extended attribute */
1255 size_t size = OCFS2_XATTR_SIZE(name_len) +
1256 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1257 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1258 void *val = xs->base + offs;
1260 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1261 /* Replace existing local xattr with tree root */
1262 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1267 } else if (!ocfs2_xattr_is_local(xs->here)) {
1268 /* For existing xattr which has value outside */
1269 struct ocfs2_xattr_value_root *xv = NULL;
1270 xv = (struct ocfs2_xattr_value_root *)(val +
1271 OCFS2_XATTR_SIZE(name_len));
1273 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1275 * If new value need set outside also,
1276 * first truncate old value to new value,
1277 * then set new value with set_value_outside().
1279 ret = ocfs2_xattr_value_truncate(inode,
1288 ret = __ocfs2_xattr_set_value_outside(inode,
1297 ret = ocfs2_xattr_update_entry(inode,
1306 * If new value need set in local,
1307 * just trucate old value to zero.
1309 ret = ocfs2_xattr_value_truncate(inode,
1319 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1320 OCFS2_INODE_UPDATE_CREDITS);
1321 if (IS_ERR(handle)) {
1322 ret = PTR_ERR(handle);
1327 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1328 OCFS2_JOURNAL_ACCESS_WRITE);
1334 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1335 /* set extended attribute in external block. */
1336 ret = ocfs2_extend_trans(handle,
1337 OCFS2_INODE_UPDATE_CREDITS +
1338 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1343 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1344 OCFS2_JOURNAL_ACCESS_WRITE);
1352 * Set value in local, include set tree root in local.
1353 * This is the first step for value size >INLINE_SIZE.
1355 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1357 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1358 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1365 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1366 (flag & OCFS2_INLINE_XATTR_FL)) {
1367 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1368 unsigned int xattrsize = osb->s_xattr_inline_size;
1371 * Adjust extent record count or inline data size
1372 * to reserve space for extended attribute.
1374 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1375 struct ocfs2_inline_data *idata = &di->id2.i_data;
1376 le16_add_cpu(&idata->id_count, -xattrsize);
1377 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1378 struct ocfs2_extent_list *el = &di->id2.i_list;
1379 le16_add_cpu(&el->l_count, -(xattrsize /
1380 sizeof(struct ocfs2_extent_rec)));
1382 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1384 /* Update xattr flag */
1385 spin_lock(&oi->ip_lock);
1386 oi->ip_dyn_features |= flag;
1387 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1388 spin_unlock(&oi->ip_lock);
1389 /* Update inode ctime */
1390 inode->i_ctime = CURRENT_TIME;
1391 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1392 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1394 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1399 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1401 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1403 * Set value outside in B tree.
1404 * This is the second step for value size > INLINE_SIZE.
1406 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1407 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1413 * If set value outside failed, we have to clean
1414 * the junk tree root we have already set in local.
1416 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1426 static int ocfs2_remove_value_outside(struct inode*inode,
1427 struct buffer_head *bh,
1428 struct ocfs2_xattr_header *header)
1432 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1433 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1435 if (!ocfs2_xattr_is_local(entry)) {
1436 struct ocfs2_xattr_value_root *xv;
1439 val = (void *)header +
1440 le16_to_cpu(entry->xe_name_offset);
1441 xv = (struct ocfs2_xattr_value_root *)
1442 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1443 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1454 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1455 struct buffer_head *di_bh)
1458 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1459 struct ocfs2_xattr_header *header;
1462 header = (struct ocfs2_xattr_header *)
1463 ((void *)di + inode->i_sb->s_blocksize -
1464 le16_to_cpu(di->i_xattr_inline_size));
1466 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1471 static int ocfs2_xattr_block_remove(struct inode *inode,
1472 struct buffer_head *blk_bh)
1474 struct ocfs2_xattr_block *xb;
1477 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1478 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1479 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1480 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1482 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1487 static int ocfs2_xattr_free_block(struct inode *inode,
1490 struct inode *xb_alloc_inode;
1491 struct buffer_head *xb_alloc_bh = NULL;
1492 struct buffer_head *blk_bh = NULL;
1493 struct ocfs2_xattr_block *xb;
1494 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1500 ret = ocfs2_read_block(inode, block, &blk_bh);
1506 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1507 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1512 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1518 blk = le64_to_cpu(xb->xb_blkno);
1519 bit = le16_to_cpu(xb->xb_suballoc_bit);
1520 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1522 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1523 EXTENT_ALLOC_SYSTEM_INODE,
1524 le16_to_cpu(xb->xb_suballoc_slot));
1525 if (!xb_alloc_inode) {
1530 mutex_lock(&xb_alloc_inode->i_mutex);
1532 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1538 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1539 if (IS_ERR(handle)) {
1540 ret = PTR_ERR(handle);
1545 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1550 ocfs2_commit_trans(osb, handle);
1552 ocfs2_inode_unlock(xb_alloc_inode, 1);
1553 brelse(xb_alloc_bh);
1555 mutex_unlock(&xb_alloc_inode->i_mutex);
1556 iput(xb_alloc_inode);
1563 * ocfs2_xattr_remove()
1565 * Free extended attribute resources associated with this inode.
1567 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1569 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1570 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1574 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1577 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1580 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1581 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1588 if (di->i_xattr_loc) {
1589 ret = ocfs2_xattr_free_block(inode,
1590 le64_to_cpu(di->i_xattr_loc));
1597 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1598 OCFS2_INODE_UPDATE_CREDITS);
1599 if (IS_ERR(handle)) {
1600 ret = PTR_ERR(handle);
1604 ret = ocfs2_journal_access(handle, inode, di_bh,
1605 OCFS2_JOURNAL_ACCESS_WRITE);
1611 di->i_xattr_loc = 0;
1613 spin_lock(&oi->ip_lock);
1614 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1615 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1616 spin_unlock(&oi->ip_lock);
1618 ret = ocfs2_journal_dirty(handle, di_bh);
1622 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1627 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1628 struct ocfs2_dinode *di)
1630 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1631 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1634 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1637 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1638 struct ocfs2_inline_data *idata = &di->id2.i_data;
1639 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1640 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1641 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1642 le64_to_cpu(di->i_size);
1644 struct ocfs2_extent_list *el = &di->id2.i_list;
1645 free = (le16_to_cpu(el->l_count) -
1646 le16_to_cpu(el->l_next_free_rec)) *
1647 sizeof(struct ocfs2_extent_rec);
1649 if (free >= xattrsize)
1656 * ocfs2_xattr_ibody_find()
1658 * Find extended attribute in inode block and
1659 * fill search info into struct ocfs2_xattr_search.
1661 static int ocfs2_xattr_ibody_find(struct inode *inode,
1664 struct ocfs2_xattr_search *xs)
1666 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1667 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1671 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1674 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1675 down_read(&oi->ip_alloc_sem);
1676 has_space = ocfs2_xattr_has_space_inline(inode, di);
1677 up_read(&oi->ip_alloc_sem);
1682 xs->xattr_bh = xs->inode_bh;
1683 xs->end = (void *)di + inode->i_sb->s_blocksize;
1684 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1685 xs->header = (struct ocfs2_xattr_header *)
1686 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1688 xs->header = (struct ocfs2_xattr_header *)
1689 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1690 xs->base = (void *)xs->header;
1691 xs->here = xs->header->xh_entries;
1693 /* Find the named attribute. */
1694 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1695 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1696 if (ret && ret != -ENODATA)
1698 xs->not_found = ret;
1705 * ocfs2_xattr_ibody_set()
1707 * Set, replace or remove an extended attribute into inode block.
1710 static int ocfs2_xattr_ibody_set(struct inode *inode,
1711 struct ocfs2_xattr_info *xi,
1712 struct ocfs2_xattr_search *xs)
1714 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1715 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1718 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1721 down_write(&oi->ip_alloc_sem);
1722 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1723 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1729 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1730 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1732 up_write(&oi->ip_alloc_sem);
1738 * ocfs2_xattr_block_find()
1740 * Find extended attribute in external block and
1741 * fill search info into struct ocfs2_xattr_search.
1743 static int ocfs2_xattr_block_find(struct inode *inode,
1746 struct ocfs2_xattr_search *xs)
1748 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1749 struct buffer_head *blk_bh = NULL;
1750 struct ocfs2_xattr_block *xb;
1753 if (!di->i_xattr_loc)
1756 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
1762 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1763 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1768 xs->xattr_bh = blk_bh;
1770 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1771 xs->header = &xb->xb_attrs.xb_header;
1772 xs->base = (void *)xs->header;
1773 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1774 xs->here = xs->header->xh_entries;
1776 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1778 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1782 if (ret && ret != -ENODATA) {
1783 xs->xattr_bh = NULL;
1786 xs->not_found = ret;
1795 * ocfs2_xattr_block_set()
1797 * Set, replace or remove an extended attribute into external block.
1800 static int ocfs2_xattr_block_set(struct inode *inode,
1801 struct ocfs2_xattr_info *xi,
1802 struct ocfs2_xattr_search *xs)
1804 struct buffer_head *new_bh = NULL;
1805 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1806 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1807 struct ocfs2_alloc_context *meta_ac = NULL;
1808 handle_t *handle = NULL;
1809 struct ocfs2_xattr_block *xblk = NULL;
1810 u16 suballoc_bit_start;
1815 if (!xs->xattr_bh) {
1817 * Alloc one external block for extended attribute
1820 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1825 handle = ocfs2_start_trans(osb,
1826 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1827 if (IS_ERR(handle)) {
1828 ret = PTR_ERR(handle);
1832 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1833 OCFS2_JOURNAL_ACCESS_CREATE);
1839 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1840 &suballoc_bit_start, &num_got,
1847 new_bh = sb_getblk(inode->i_sb, first_blkno);
1848 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1850 ret = ocfs2_journal_access(handle, inode, new_bh,
1851 OCFS2_JOURNAL_ACCESS_CREATE);
1857 /* Initialize ocfs2_xattr_block */
1858 xs->xattr_bh = new_bh;
1859 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1860 memset(xblk, 0, inode->i_sb->s_blocksize);
1861 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1862 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1863 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1864 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1865 xblk->xb_blkno = cpu_to_le64(first_blkno);
1867 xs->header = &xblk->xb_attrs.xb_header;
1868 xs->base = (void *)xs->header;
1869 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1870 xs->here = xs->header->xh_entries;
1873 ret = ocfs2_journal_dirty(handle, new_bh);
1878 di->i_xattr_loc = cpu_to_le64(first_blkno);
1879 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1883 ocfs2_commit_trans(osb, handle);
1886 ocfs2_free_alloc_context(meta_ac);
1890 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1892 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1893 /* Set extended attribute into external block */
1894 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1895 if (!ret || ret != -ENOSPC)
1898 ret = ocfs2_xattr_create_index_block(inode, xs);
1903 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1913 * Set, replace or remove an extended attribute for this inode.
1914 * value is NULL to remove an existing extended attribute, else either
1915 * create or replace an extended attribute.
1917 int ocfs2_xattr_set(struct inode *inode,
1924 struct buffer_head *di_bh = NULL;
1925 struct ocfs2_dinode *di;
1927 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1929 struct ocfs2_xattr_info xi = {
1930 .name_index = name_index,
1933 .value_len = value_len,
1936 struct ocfs2_xattr_search xis = {
1937 .not_found = -ENODATA,
1940 struct ocfs2_xattr_search xbs = {
1941 .not_found = -ENODATA,
1944 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1947 ret = ocfs2_inode_lock(inode, &di_bh, 1);
1952 xis.inode_bh = xbs.inode_bh = di_bh;
1953 di = (struct ocfs2_dinode *)di_bh->b_data;
1955 down_write(&OCFS2_I(inode)->ip_xattr_sem);
1957 * Scan inode and external block to find the same name
1958 * extended attribute and collect search infomation.
1960 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
1963 if (xis.not_found) {
1964 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
1969 if (xis.not_found && xbs.not_found) {
1971 if (flags & XATTR_REPLACE)
1978 if (flags & XATTR_CREATE)
1983 /* Remove existing extended attribute */
1985 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
1986 else if (!xbs.not_found)
1987 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
1989 /* We always try to set extended attribute into inode first*/
1990 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
1991 if (!ret && !xbs.not_found) {
1993 * If succeed and that extended attribute existing in
1994 * external block, then we will remove it.
1998 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
1999 } else if (ret == -ENOSPC) {
2000 if (di->i_xattr_loc && !xbs.xattr_bh) {
2001 ret = ocfs2_xattr_block_find(inode, name_index,
2007 * If no space in inode, we will set extended attribute
2008 * into external block.
2010 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2013 if (!xis.not_found) {
2015 * If succeed and that extended attribute
2016 * existing in inode, we will remove it.
2020 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2025 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2026 ocfs2_inode_unlock(inode, 1);
2028 brelse(xbs.xattr_bh);
2029 for (i = 0; i < blk_per_bucket; i++)
2030 brelse(xbs.bucket.bhs[i]);
2036 * Find the xattr extent rec which may contains name_hash.
2037 * e_cpos will be the first name hash of the xattr rec.
2038 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2040 static int ocfs2_xattr_get_rec(struct inode *inode,
2045 struct ocfs2_extent_list *el)
2048 struct buffer_head *eb_bh = NULL;
2049 struct ocfs2_extent_block *eb;
2050 struct ocfs2_extent_rec *rec = NULL;
2053 if (el->l_tree_depth) {
2054 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2060 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2063 if (el->l_tree_depth) {
2064 ocfs2_error(inode->i_sb,
2065 "Inode %lu has non zero tree depth in "
2066 "xattr tree block %llu\n", inode->i_ino,
2067 (unsigned long long)eb_bh->b_blocknr);
2073 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2074 rec = &el->l_recs[i];
2076 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2077 e_blkno = le64_to_cpu(rec->e_blkno);
2083 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2084 "record (%u, %u, 0) in xattr", inode->i_ino,
2085 le32_to_cpu(rec->e_cpos),
2086 ocfs2_rec_clusters(el, rec));
2091 *p_blkno = le64_to_cpu(rec->e_blkno);
2092 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2094 *e_cpos = le32_to_cpu(rec->e_cpos);
2100 typedef int (xattr_bucket_func)(struct inode *inode,
2101 struct ocfs2_xattr_bucket *bucket,
2104 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2105 struct buffer_head *header_bh,
2112 int i, ret = 0, cmp = 1, block_off, new_offset;
2113 struct ocfs2_xattr_header *xh =
2114 (struct ocfs2_xattr_header *)header_bh->b_data;
2115 size_t name_len = strlen(name);
2116 struct ocfs2_xattr_entry *xe = NULL;
2117 struct buffer_head *name_bh = NULL;
2121 * We don't use binary search in the bucket because there
2122 * may be multiple entries with the same name hash.
2124 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2125 xe = &xh->xh_entries[i];
2127 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2129 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2132 cmp = name_index - ocfs2_xattr_get_type(xe);
2134 cmp = name_len - xe->xe_name_len;
2138 ret = ocfs2_xattr_bucket_get_name_value(inode,
2148 ret = ocfs2_read_block(inode, header_bh->b_blocknr + block_off,
2154 xe_name = name_bh->b_data + new_offset;
2156 cmp = memcmp(name, xe_name, name_len);
2172 * Find the specified xattr entry in a series of buckets.
2173 * This series start from p_blkno and last for num_clusters.
2174 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2175 * the num of the valid buckets.
2177 * Return the buffer_head this xattr should reside in. And if the xattr's
2178 * hash is in the gap of 2 buckets, return the lower bucket.
2180 static int ocfs2_xattr_bucket_find(struct inode *inode,
2187 struct ocfs2_xattr_search *xs)
2190 struct buffer_head *bh = NULL;
2191 struct buffer_head *lower_bh = NULL;
2192 struct ocfs2_xattr_header *xh = NULL;
2193 struct ocfs2_xattr_entry *xe = NULL;
2195 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2196 int low_bucket = 0, bucket, high_bucket;
2200 ret = ocfs2_read_block(inode, p_blkno, &bh);
2206 xh = (struct ocfs2_xattr_header *)bh->b_data;
2207 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2209 while (low_bucket <= high_bucket) {
2212 bucket = (low_bucket + high_bucket) / 2;
2214 blkno = p_blkno + bucket * blk_per_bucket;
2216 ret = ocfs2_read_block(inode, blkno, &bh);
2222 xh = (struct ocfs2_xattr_header *)bh->b_data;
2223 xe = &xh->xh_entries[0];
2224 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2225 high_bucket = bucket - 1;
2230 * Check whether the hash of the last entry in our
2231 * bucket is larger than the search one. for an empty
2232 * bucket, the last one is also the first one.
2235 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2237 last_hash = le32_to_cpu(xe->xe_name_hash);
2239 /* record lower_bh which may be the insert place. */
2244 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2245 low_bucket = bucket + 1;
2249 /* the searched xattr should reside in this bucket if exists. */
2250 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2251 name_index, name, name_hash,
2261 * Record the bucket we have found.
2262 * When the xattr's hash value is in the gap of 2 buckets, we will
2263 * always set it to the previous bucket.
2267 * We can't find any bucket whose first name_hash is less
2268 * than the find name_hash.
2270 BUG_ON(bh->b_blocknr != p_blkno);
2274 xs->bucket.bhs[0] = lower_bh;
2275 xs->bucket.xh = (struct ocfs2_xattr_header *)
2276 xs->bucket.bhs[0]->b_data;
2279 xs->header = xs->bucket.xh;
2280 xs->base = xs->bucket.bhs[0]->b_data;
2281 xs->end = xs->base + inode->i_sb->s_blocksize;
2285 * If we have found the xattr enty, read all the blocks in
2288 ret = ocfs2_read_blocks(inode, xs->bucket.bhs[0]->b_blocknr + 1,
2289 blk_per_bucket - 1, &xs->bucket.bhs[1],
2296 xs->here = &xs->header->xh_entries[index];
2297 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2298 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2308 static int ocfs2_xattr_index_block_find(struct inode *inode,
2309 struct buffer_head *root_bh,
2312 struct ocfs2_xattr_search *xs)
2315 struct ocfs2_xattr_block *xb =
2316 (struct ocfs2_xattr_block *)root_bh->b_data;
2317 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2318 struct ocfs2_extent_list *el = &xb_root->xt_list;
2320 u32 first_hash, num_clusters = 0;
2321 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
2323 if (le16_to_cpu(el->l_next_free_rec) == 0)
2326 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2327 name, name_hash, name_index);
2329 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2336 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2338 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2339 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2341 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2342 p_blkno, first_hash, num_clusters, xs);
2348 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2351 xattr_bucket_func *func,
2355 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2356 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2357 u32 num_buckets = clusters * bpc;
2358 struct ocfs2_xattr_bucket bucket;
2360 memset(&bucket, 0, sizeof(bucket));
2362 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2365 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2366 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket,
2373 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2375 * The real bucket num in this series of blocks is stored
2376 * in the 1st bucket.
2379 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2381 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
2382 le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
2384 ret = func(inode, &bucket, para);
2391 for (j = 0; j < blk_per_bucket; j++)
2392 brelse(bucket.bhs[j]);
2393 memset(&bucket, 0, sizeof(bucket));
2397 for (j = 0; j < blk_per_bucket; j++)
2398 brelse(bucket.bhs[j]);
2403 struct ocfs2_xattr_tree_list {
2409 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2410 struct ocfs2_xattr_header *xh,
2417 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2420 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2422 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2423 *new_offset = name_offset % inode->i_sb->s_blocksize;
2428 static int ocfs2_list_xattr_bucket(struct inode *inode,
2429 struct ocfs2_xattr_bucket *bucket,
2433 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2434 int i, block_off, new_offset;
2435 const char *prefix, *name;
2437 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2438 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2439 type = ocfs2_xattr_get_type(entry);
2440 prefix = ocfs2_xattr_prefix(type);
2443 ret = ocfs2_xattr_bucket_get_name_value(inode,
2451 name = (const char *)bucket->bhs[block_off]->b_data +
2453 ret = ocfs2_xattr_list_entry(xl->buffer,
2457 entry->xe_name_len);
2466 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2467 struct ocfs2_xattr_tree_root *xt,
2471 struct ocfs2_extent_list *el = &xt->xt_list;
2473 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2475 struct ocfs2_xattr_tree_list xl = {
2477 .buffer_size = buffer_size,
2481 if (le16_to_cpu(el->l_next_free_rec) == 0)
2484 while (name_hash > 0) {
2485 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2486 &e_cpos, &num_clusters, el);
2492 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2493 ocfs2_list_xattr_bucket,
2503 name_hash = e_cpos - 1;
2511 static int cmp_xe(const void *a, const void *b)
2513 const struct ocfs2_xattr_entry *l = a, *r = b;
2514 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2515 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2517 if (l_hash > r_hash)
2519 if (l_hash < r_hash)
2524 static void swap_xe(void *a, void *b, int size)
2526 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2529 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2530 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2534 * When the ocfs2_xattr_block is filled up, new bucket will be created
2535 * and all the xattr entries will be moved to the new bucket.
2536 * Note: we need to sort the entries since they are not saved in order
2537 * in the ocfs2_xattr_block.
2539 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2540 struct buffer_head *xb_bh,
2541 struct buffer_head *xh_bh,
2542 struct buffer_head *data_bh)
2544 int i, blocksize = inode->i_sb->s_blocksize;
2545 u16 offset, size, off_change;
2546 struct ocfs2_xattr_entry *xe;
2547 struct ocfs2_xattr_block *xb =
2548 (struct ocfs2_xattr_block *)xb_bh->b_data;
2549 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2550 struct ocfs2_xattr_header *xh =
2551 (struct ocfs2_xattr_header *)xh_bh->b_data;
2552 u16 count = le16_to_cpu(xb_xh->xh_count);
2553 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2555 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2556 (unsigned long long)xb_bh->b_blocknr,
2557 (unsigned long long)xh_bh->b_blocknr);
2559 memset(xh_bh->b_data, 0, blocksize);
2561 memset(data_bh->b_data, 0, blocksize);
2563 * Since the xe_name_offset is based on ocfs2_xattr_header,
2564 * there is a offset change corresponding to the change of
2565 * ocfs2_xattr_header's position.
2567 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2568 xe = &xb_xh->xh_entries[count - 1];
2569 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2570 size = blocksize - offset;
2572 /* copy all the names and values. */
2574 target = data_bh->b_data;
2575 memcpy(target + offset, src + offset, size);
2577 /* Init new header now. */
2578 xh->xh_count = xb_xh->xh_count;
2579 xh->xh_num_buckets = cpu_to_le16(1);
2580 xh->xh_name_value_len = cpu_to_le16(size);
2581 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2583 /* copy all the entries. */
2584 target = xh_bh->b_data;
2585 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2586 size = count * sizeof(struct ocfs2_xattr_entry);
2587 memcpy(target + offset, (char *)xb_xh + offset, size);
2589 /* Change the xe offset for all the xe because of the move. */
2590 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2591 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2592 for (i = 0; i < count; i++)
2593 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2595 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2596 offset, size, off_change);
2598 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2603 * After we move xattr from block to index btree, we have to
2604 * update ocfs2_xattr_search to the new xe and base.
2606 * When the entry is in xattr block, xattr_bh indicates the storage place.
2607 * While if the entry is in index b-tree, "bucket" indicates the
2608 * real place of the xattr.
2610 static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2611 struct ocfs2_xattr_search *xs,
2612 struct buffer_head *old_bh,
2613 struct buffer_head *new_bh)
2616 char *buf = old_bh->b_data;
2617 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2618 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2619 int i, blocksize = inode->i_sb->s_blocksize;
2620 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2622 xs->bucket.bhs[0] = new_bh;
2624 xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2625 xs->header = xs->bucket.xh;
2627 xs->base = new_bh->b_data;
2628 xs->end = xs->base + inode->i_sb->s_blocksize;
2630 if (!xs->not_found) {
2631 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2632 ret = ocfs2_read_blocks(inode,
2633 xs->bucket.bhs[0]->b_blocknr + 1,
2634 blk_per_bucket - 1, &xs->bucket.bhs[1],
2641 i = xs->here - old_xh->xh_entries;
2642 xs->here = &xs->header->xh_entries[i];
2649 static int ocfs2_xattr_create_index_block(struct inode *inode,
2650 struct ocfs2_xattr_search *xs)
2652 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2656 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2657 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2658 struct ocfs2_alloc_context *data_ac;
2659 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2660 struct buffer_head *xb_bh = xs->xattr_bh;
2661 struct ocfs2_xattr_block *xb =
2662 (struct ocfs2_xattr_block *)xb_bh->b_data;
2663 struct ocfs2_xattr_tree_root *xr;
2664 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2665 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2667 mlog(0, "create xattr index block for %llu\n",
2668 (unsigned long long)xb_bh->b_blocknr);
2670 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2672 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2680 * We can use this lock for now, and maybe move to a dedicated mutex
2681 * if performance becomes a problem later.
2683 down_write(&oi->ip_alloc_sem);
2686 * 3 more credits, one for xattr block update, one for the 1st block
2687 * of the new xattr bucket and one for the value/data.
2690 handle = ocfs2_start_trans(osb, credits);
2691 if (IS_ERR(handle)) {
2692 ret = PTR_ERR(handle);
2697 ret = ocfs2_journal_access(handle, inode, xb_bh,
2698 OCFS2_JOURNAL_ACCESS_WRITE);
2704 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2711 * The bucket may spread in many blocks, and
2712 * we will only touch the 1st block and the last block
2713 * in the whole bucket(one for entry and one for data).
2715 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2717 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2719 xh_bh = sb_getblk(inode->i_sb, blkno);
2726 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2728 ret = ocfs2_journal_access(handle, inode, xh_bh,
2729 OCFS2_JOURNAL_ACCESS_CREATE);
2736 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2743 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2745 ret = ocfs2_journal_access(handle, inode, data_bh,
2746 OCFS2_JOURNAL_ACCESS_CREATE);
2753 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2755 ocfs2_journal_dirty(handle, xh_bh);
2757 ocfs2_journal_dirty(handle, data_bh);
2759 ret = ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2765 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2766 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2767 offsetof(struct ocfs2_xattr_block, xb_attrs));
2769 xr = &xb->xb_attrs.xb_root;
2770 xr->xt_clusters = cpu_to_le32(1);
2771 xr->xt_last_eb_blk = 0;
2772 xr->xt_list.l_tree_depth = 0;
2773 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2774 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2776 xr->xt_list.l_recs[0].e_cpos = 0;
2777 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2778 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2780 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2782 ret = ocfs2_journal_dirty(handle, xb_bh);
2789 ocfs2_commit_trans(osb, handle);
2792 up_write(&oi->ip_alloc_sem);
2796 ocfs2_free_alloc_context(data_ac);
2804 static int cmp_xe_offset(const void *a, const void *b)
2806 const struct ocfs2_xattr_entry *l = a, *r = b;
2807 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2808 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2810 if (l_name_offset < r_name_offset)
2812 if (l_name_offset > r_name_offset)
2818 * defrag a xattr bucket if we find that the bucket has some
2819 * holes beteen name/value pairs.
2820 * We will move all the name/value pairs to the end of the bucket
2821 * so that we can spare some space for insertion.
2823 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2824 struct ocfs2_xattr_bucket *bucket)
2827 size_t end, offset, len, value_len;
2828 struct ocfs2_xattr_header *xh;
2829 char *entries, *buf, *bucket_buf = NULL;
2830 u64 blkno = bucket->bhs[0]->b_blocknr;
2831 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2833 size_t blocksize = inode->i_sb->s_blocksize;
2835 struct buffer_head **bhs;
2836 struct ocfs2_xattr_entry *xe;
2838 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2843 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket, bhs, 0);
2848 * In order to make the operation more efficient and generic,
2849 * we copy all the blocks into a contiguous memory and do the
2850 * defragment there, so if anything is error, we will not touch
2853 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2860 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2861 memcpy(buf, bhs[i]->b_data, blocksize);
2863 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2864 if (IS_ERR(handle)) {
2865 ret = PTR_ERR(handle);
2871 for (i = 0; i < blk_per_bucket; i++) {
2872 ret = ocfs2_journal_access(handle, inode, bhs[i],
2873 OCFS2_JOURNAL_ACCESS_WRITE);
2880 xh = (struct ocfs2_xattr_header *)bucket_buf;
2881 entries = (char *)xh->xh_entries;
2882 xh_free_start = le16_to_cpu(xh->xh_free_start);
2884 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2885 "xh_free_start = %u, xh_name_value_len = %u.\n",
2886 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2887 le16_to_cpu(xh->xh_name_value_len));
2890 * sort all the entries by their offset.
2891 * the largest will be the first, so that we can
2892 * move them to the end one by one.
2894 sort(entries, le16_to_cpu(xh->xh_count),
2895 sizeof(struct ocfs2_xattr_entry),
2896 cmp_xe_offset, swap_xe);
2898 /* Move all name/values to the end of the bucket. */
2899 xe = xh->xh_entries;
2900 end = OCFS2_XATTR_BUCKET_SIZE;
2901 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2902 offset = le16_to_cpu(xe->xe_name_offset);
2903 if (ocfs2_xattr_is_local(xe))
2904 value_len = OCFS2_XATTR_SIZE(
2905 le64_to_cpu(xe->xe_value_size));
2907 value_len = OCFS2_XATTR_ROOT_SIZE;
2908 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2911 * We must make sure that the name/value pair
2912 * exist in the same block. So adjust end to
2913 * the previous block end if needed.
2915 if (((end - len) / blocksize !=
2916 (end - 1) / blocksize))
2917 end = end - end % blocksize;
2919 if (end > offset + len) {
2920 memmove(bucket_buf + end - len,
2921 bucket_buf + offset, len);
2922 xe->xe_name_offset = cpu_to_le16(end - len);
2925 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2926 "bucket %llu\n", (unsigned long long)blkno);
2931 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2932 "bucket %llu\n", (unsigned long long)blkno);
2934 if (xh_free_start == end)
2937 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
2938 xh->xh_free_start = cpu_to_le16(end);
2940 /* sort the entries by their name_hash. */
2941 sort(entries, le16_to_cpu(xh->xh_count),
2942 sizeof(struct ocfs2_xattr_entry),
2946 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
2947 memcpy(bhs[i]->b_data, buf, blocksize);
2948 ocfs2_journal_dirty(handle, bhs[i]);
2952 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2956 for (i = 0; i < blk_per_bucket; i++)
2966 * Move half nums of the xattr bucket in the previous cluster to this new
2967 * cluster. We only touch the last cluster of the previous extend record.
2969 * first_bh is the first buffer_head of a series of bucket in the same
2970 * extent rec and header_bh is the header of one bucket in this cluster.
2971 * They will be updated if we move the data header_bh contains to the new
2972 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
2974 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
2976 struct buffer_head **first_bh,
2977 struct buffer_head **header_bh,
2983 int i, ret, credits;
2984 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2985 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
2986 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
2987 int blocksize = inode->i_sb->s_blocksize;
2988 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
2989 struct ocfs2_xattr_header *new_xh;
2990 struct ocfs2_xattr_header *xh =
2991 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
2993 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
2994 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
2996 prev_bh = *first_bh;
2998 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3000 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3002 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3003 prev_blkno, new_blkno);
3006 * We need to update the 1st half of the new cluster and
3007 * 1 more for the update of the 1st bucket of the previous
3010 credits = bpc / 2 + 1;
3011 ret = ocfs2_extend_trans(handle, credits);
3017 ret = ocfs2_journal_access(handle, inode, prev_bh,
3018 OCFS2_JOURNAL_ACCESS_WRITE);
3024 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3025 old_bh = new_bh = NULL;
3026 new_bh = sb_getblk(inode->i_sb, new_blkno);
3033 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3035 ret = ocfs2_journal_access(handle, inode, new_bh,
3036 OCFS2_JOURNAL_ACCESS_CREATE);
3043 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
3050 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3053 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3054 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3057 *first_hash = le32_to_cpu(
3058 new_xh->xh_entries[0].xe_name_hash);
3059 new_first_bh = new_bh;
3060 get_bh(new_first_bh);
3063 ocfs2_journal_dirty(handle, new_bh);
3065 if (*header_bh == old_bh) {
3067 *header_bh = new_bh;
3071 *first_bh = new_first_bh;
3078 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3080 ocfs2_journal_dirty(handle, prev_bh);
3083 brelse(new_first_bh);
3087 static int ocfs2_read_xattr_bucket(struct inode *inode,
3089 struct buffer_head **bhs,
3093 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3096 return ocfs2_read_blocks(inode, blkno,
3097 blk_per_bucket, bhs, 0);
3099 for (i = 0; i < blk_per_bucket; i++) {
3100 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3101 if (bhs[i] == NULL) {
3106 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3113 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3114 * first_hash will record the 1st hash of the new bucket.
3116 static int ocfs2_half_xattr_bucket(struct inode *inode,
3121 int new_bucket_head)
3124 u16 count, start, len, name_value_len, xe_len, name_offset;
3125 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3126 struct buffer_head **s_bhs, **t_bhs = NULL;
3127 struct ocfs2_xattr_header *xh;
3128 struct ocfs2_xattr_entry *xe;
3129 int blocksize = inode->i_sb->s_blocksize;
3131 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3134 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3138 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3144 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3145 OCFS2_JOURNAL_ACCESS_WRITE);
3151 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3157 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3163 for (i = 0; i < blk_per_bucket; i++) {
3164 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3166 OCFS2_JOURNAL_ACCESS_CREATE :
3167 OCFS2_JOURNAL_ACCESS_WRITE);
3174 /* copy the whole bucket to the new first. */
3175 for (i = 0; i < blk_per_bucket; i++)
3176 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3178 /* update the new bucket. */
3179 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3180 count = le16_to_cpu(xh->xh_count);
3184 * Calculate the total name/value len and xh_free_start for
3185 * the old bucket first.
3187 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3189 for (i = 0; i < start; i++) {
3190 xe = &xh->xh_entries[i];
3191 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3192 if (ocfs2_xattr_is_local(xe))
3194 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3196 xe_len += OCFS2_XATTR_ROOT_SIZE;
3197 name_value_len += xe_len;
3198 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3199 name_offset = le16_to_cpu(xe->xe_name_offset);
3203 * Now begin the modification to the new bucket.
3205 * In the new bucket, We just move the xattr entry to the beginning
3206 * and don't touch the name/value. So there will be some holes in the
3207 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3210 xe = &xh->xh_entries[start];
3211 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3212 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3213 (int)((char *)xe - (char *)xh),
3214 (int)((char *)xh->xh_entries - (char *)xh));
3215 memmove((char *)xh->xh_entries, (char *)xe, len);
3216 xe = &xh->xh_entries[count - start];
3217 len = sizeof(struct ocfs2_xattr_entry) * start;
3218 memset((char *)xe, 0, len);
3220 le16_add_cpu(&xh->xh_count, -start);
3221 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3223 /* Calculate xh_free_start for the new bucket. */
3224 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3225 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3226 xe = &xh->xh_entries[i];
3227 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3228 if (ocfs2_xattr_is_local(xe))
3230 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3232 xe_len += OCFS2_XATTR_ROOT_SIZE;
3233 if (le16_to_cpu(xe->xe_name_offset) <
3234 le16_to_cpu(xh->xh_free_start))
3235 xh->xh_free_start = xe->xe_name_offset;
3238 /* set xh->xh_num_buckets for the new xh. */
3239 if (new_bucket_head)
3240 xh->xh_num_buckets = cpu_to_le16(1);
3242 xh->xh_num_buckets = 0;
3244 for (i = 0; i < blk_per_bucket; i++) {
3245 ocfs2_journal_dirty(handle, t_bhs[i]);
3250 /* store the first_hash of the new bucket. */
3252 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3255 * Now only update the 1st block of the old bucket.
3256 * Please note that the entry has been sorted already above.
3258 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3259 memset(&xh->xh_entries[start], 0,
3260 sizeof(struct ocfs2_xattr_entry) * (count - start));
3261 xh->xh_count = cpu_to_le16(start);
3262 xh->xh_free_start = cpu_to_le16(name_offset);
3263 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3265 ocfs2_journal_dirty(handle, s_bhs[0]);
3271 for (i = 0; i < blk_per_bucket; i++)
3277 for (i = 0; i < blk_per_bucket; i++)
3286 * Copy xattr from one bucket to another bucket.
3288 * The caller must make sure that the journal transaction
3289 * has enough space for journaling.
3291 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3298 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3299 int blocksize = inode->i_sb->s_blocksize;
3300 struct buffer_head **s_bhs, **t_bhs = NULL;
3302 BUG_ON(s_blkno == t_blkno);
3304 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3305 s_blkno, t_blkno, t_is_new);
3307 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3312 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3316 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3323 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3327 for (i = 0; i < blk_per_bucket; i++) {
3328 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3330 OCFS2_JOURNAL_ACCESS_CREATE :
3331 OCFS2_JOURNAL_ACCESS_WRITE);
3336 for (i = 0; i < blk_per_bucket; i++) {
3337 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3338 ocfs2_journal_dirty(handle, t_bhs[i]);
3343 for (i = 0; i < blk_per_bucket; i++)
3349 for (i = 0; i < blk_per_bucket; i++)
3358 * Copy one xattr cluster from src_blk to to_blk.
3359 * The to_blk will become the first bucket header of the cluster, so its
3360 * xh_num_buckets will be initialized as the bucket num in the cluster.
3362 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3364 struct buffer_head *first_bh,
3369 int i, ret, credits;
3370 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3371 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3372 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3373 struct buffer_head *bh = NULL;
3374 struct ocfs2_xattr_header *xh;
3375 u64 to_blk_start = to_blk;
3377 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3380 * We need to update the new cluster and 1 more for the update of
3381 * the 1st bucket of the previous extent rec.
3384 ret = ocfs2_extend_trans(handle, credits);
3390 ret = ocfs2_journal_access(handle, inode, first_bh,
3391 OCFS2_JOURNAL_ACCESS_WRITE);
3397 for (i = 0; i < num_buckets; i++) {
3398 ret = ocfs2_cp_xattr_bucket(inode, handle,
3399 src_blk, to_blk, 1);
3405 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3406 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3409 /* update the old bucket header. */
3410 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3411 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3413 ocfs2_journal_dirty(handle, first_bh);
3415 /* update the new bucket header. */
3416 ret = ocfs2_read_block(inode, to_blk_start, &bh);
3422 ret = ocfs2_journal_access(handle, inode, bh,
3423 OCFS2_JOURNAL_ACCESS_WRITE);
3429 xh = (struct ocfs2_xattr_header *)bh->b_data;
3430 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3432 ocfs2_journal_dirty(handle, bh);
3435 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3442 * Move half of the xattrs in this cluster to the new cluster.
3443 * This function should only be called when bucket size == cluster size.
3444 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3446 static int ocfs2_half_xattr_cluster(struct inode *inode,
3452 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3453 int ret, credits = 2 * blk_per_bucket;
3455 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3457 ret = ocfs2_extend_trans(handle, credits);
3463 /* Move half of the xattr in start_blk to the next bucket. */
3464 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3465 new_blk, first_hash, 1);
3469 * Move some xattrs from the old cluster to the new one since they are not
3470 * contiguous in ocfs2 xattr tree.
3472 * new_blk starts a new separate cluster, and we will move some xattrs from
3473 * prev_blk to it. v_start will be set as the first name hash value in this
3474 * new cluster so that it can be used as e_cpos during tree insertion and
3475 * don't collide with our original b-tree operations. first_bh and header_bh
3476 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3477 * to extend the insert bucket.
3479 * The problem is how much xattr should we move to the new one and when should
3480 * we update first_bh and header_bh?
3481 * 1. If cluster size > bucket size, that means the previous cluster has more
3482 * than 1 bucket, so just move half nums of bucket into the new cluster and
3483 * update the first_bh and header_bh if the insert bucket has been moved
3484 * to the new cluster.
3485 * 2. If cluster_size == bucket_size:
3486 * a) If the previous extent rec has more than one cluster and the insert
3487 * place isn't in the last cluster, copy the entire last cluster to the
3488 * new one. This time, we don't need to upate the first_bh and header_bh
3489 * since they will not be moved into the new cluster.
3490 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3491 * the new one. And we set the extend flag to zero if the insert place is
3492 * moved into the new allocated cluster since no extend is needed.
3494 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3496 struct buffer_head **first_bh,
3497 struct buffer_head **header_bh,
3505 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3507 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3508 prev_blk, prev_clusters, new_blk);
3510 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3511 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3520 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3522 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3523 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3527 ret = ocfs2_half_xattr_cluster(inode, handle,
3531 if ((*header_bh)->b_blocknr == last_blk && extend)
3540 * Add a new cluster for xattr storage.
3542 * If the new cluster is contiguous with the previous one, it will be
3543 * appended to the same extent record, and num_clusters will be updated.
3544 * If not, we will insert a new extent for it and move some xattrs in
3545 * the last cluster into the new allocated one.
3546 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3547 * lose the benefits of hashing because we'll have to search large leaves.
3548 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3551 * first_bh is the first block of the previous extent rec and header_bh
3552 * indicates the bucket we will insert the new xattrs. They will be updated
3553 * when the header_bh is moved into the new cluster.
3555 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3556 struct buffer_head *root_bh,
3557 struct buffer_head **first_bh,
3558 struct buffer_head **header_bh,
3565 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3566 u32 prev_clusters = *num_clusters;
3567 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3569 handle_t *handle = NULL;
3570 struct ocfs2_alloc_context *data_ac = NULL;
3571 struct ocfs2_alloc_context *meta_ac = NULL;
3572 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3573 struct ocfs2_extent_tree et;
3575 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3576 "previous xattr blkno = %llu\n",
3577 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3578 prev_cpos, prev_blkno);
3580 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
3582 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3583 &data_ac, &meta_ac);
3589 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3591 handle = ocfs2_start_trans(osb, credits);
3592 if (IS_ERR(handle)) {
3593 ret = PTR_ERR(handle);
3599 ret = ocfs2_journal_access(handle, inode, root_bh,
3600 OCFS2_JOURNAL_ACCESS_WRITE);
3606 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3607 clusters_to_add, &bit_off, &num_bits);
3614 BUG_ON(num_bits > clusters_to_add);
3616 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3617 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3618 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3620 if (prev_blkno + prev_clusters * bpc == block &&
3621 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3622 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3624 * If this cluster is contiguous with the old one and
3625 * adding this new cluster, we don't surpass the limit of
3626 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3627 * initialized and used like other buckets in the previous
3629 * So add it as a contiguous one. The caller will handle
3632 v_start = prev_cpos + prev_clusters;
3633 *num_clusters = prev_clusters + num_bits;
3634 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3637 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3652 if (handle->h_buffer_credits < credits) {
3654 * The journal has been restarted before, and don't
3655 * have enough space for the insertion, so extend it
3658 ret = ocfs2_extend_trans(handle, credits);
3664 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3665 num_bits, block, v_start);
3666 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3667 num_bits, 0, meta_ac);
3673 ret = ocfs2_journal_dirty(handle, root_bh);
3681 ocfs2_commit_trans(osb, handle);
3683 ocfs2_free_alloc_context(data_ac);
3685 ocfs2_free_alloc_context(meta_ac);
3691 * Extend a new xattr bucket and move xattrs to the end one by one until
3692 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3694 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3695 struct buffer_head *first_bh,
3696 struct buffer_head *start_bh,
3700 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3701 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3702 u64 start_blk = start_bh->b_blocknr, end_blk;
3703 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3705 struct ocfs2_xattr_header *first_xh =
3706 (struct ocfs2_xattr_header *)first_bh->b_data;
3707 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3709 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3710 "from %llu, len = %u\n", start_blk,
3711 (unsigned long long)first_bh->b_blocknr, num_clusters);
3713 BUG_ON(bucket >= num_buckets);
3715 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3718 * We will touch all the buckets after the start_bh(include it).
3719 * Add one more bucket and modify the first_bh.
3721 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3722 handle = ocfs2_start_trans(osb, credits);
3723 if (IS_ERR(handle)) {
3724 ret = PTR_ERR(handle);
3730 ret = ocfs2_journal_access(handle, inode, first_bh,
3731 OCFS2_JOURNAL_ACCESS_WRITE);
3737 while (end_blk != start_blk) {
3738 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3739 end_blk + blk_per_bucket, 0);
3742 end_blk -= blk_per_bucket;
3745 /* Move half of the xattr in start_blk to the next bucket. */
3746 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3747 start_blk + blk_per_bucket, NULL, 0);
3749 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3750 ocfs2_journal_dirty(handle, first_bh);
3753 ocfs2_commit_trans(osb, handle);
3759 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3760 * xb_bh is the ocfs2_xattr_block.
3761 * We will move all the buckets starting from header_bh to the next place. As
3762 * for this one, half num of its xattrs will be moved to the next one.
3764 * We will allocate a new cluster if current cluster is full and adjust
3765 * header_bh and first_bh if the insert place is moved to the new cluster.
3767 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3768 struct buffer_head *xb_bh,
3769 struct buffer_head *header_bh)
3771 struct ocfs2_xattr_header *first_xh = NULL;
3772 struct buffer_head *first_bh = NULL;
3773 struct ocfs2_xattr_block *xb =
3774 (struct ocfs2_xattr_block *)xb_bh->b_data;
3775 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3776 struct ocfs2_extent_list *el = &xb_root->xt_list;
3777 struct ocfs2_xattr_header *xh =
3778 (struct ocfs2_xattr_header *)header_bh->b_data;
3779 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3780 struct super_block *sb = inode->i_sb;
3781 struct ocfs2_super *osb = OCFS2_SB(sb);
3782 int ret, num_buckets, extend = 1;
3784 u32 e_cpos, num_clusters;
3786 mlog(0, "Add new xattr bucket starting form %llu\n",
3787 (unsigned long long)header_bh->b_blocknr);
3790 * Add refrence for header_bh here because it may be
3791 * changed in ocfs2_add_new_xattr_cluster and we need
3792 * to free it in the end.
3796 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3803 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
3809 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3810 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3812 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3813 ret = ocfs2_add_new_xattr_cluster(inode,
3828 ret = ocfs2_extend_xattr_bucket(inode,
3840 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3841 struct ocfs2_xattr_bucket *bucket,
3844 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3846 offs = offs % inode->i_sb->s_blocksize;
3847 return bucket->bhs[block_off]->b_data + offs;
3851 * Handle the normal xattr set, including replace, delete and new.
3853 * Note: "local" indicates the real data's locality. So we can't
3854 * just its bucket locality by its length.
3856 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3857 struct ocfs2_xattr_info *xi,
3858 struct ocfs2_xattr_search *xs,
3862 struct ocfs2_xattr_entry *last, *xe;
3863 int name_len = strlen(xi->name);
3864 struct ocfs2_xattr_header *xh = xs->header;
3865 u16 count = le16_to_cpu(xh->xh_count), start;
3866 size_t blocksize = inode->i_sb->s_blocksize;
3868 size_t offs, size, new_size;
3870 last = &xh->xh_entries[count];
3871 if (!xs->not_found) {
3873 offs = le16_to_cpu(xe->xe_name_offset);
3874 if (ocfs2_xattr_is_local(xe))
3875 size = OCFS2_XATTR_SIZE(name_len) +
3876 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3878 size = OCFS2_XATTR_SIZE(name_len) +
3879 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3882 * If the new value will be stored outside, xi->value has been
3883 * initalized as an empty ocfs2_xattr_value_root, and the same
3884 * goes with xi->value_len, so we can set new_size safely here.
3885 * See ocfs2_xattr_set_in_bucket.
3887 new_size = OCFS2_XATTR_SIZE(name_len) +
3888 OCFS2_XATTR_SIZE(xi->value_len);
3890 le16_add_cpu(&xh->xh_name_value_len, -size);
3892 if (new_size > size)
3893 goto set_new_name_value;
3895 /* Now replace the old value with new one. */
3897 xe->xe_value_size = cpu_to_le64(xi->value_len);
3899 xe->xe_value_size = 0;
3901 val = ocfs2_xattr_bucket_get_val(inode,
3903 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3904 size - OCFS2_XATTR_SIZE(name_len));
3905 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3906 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3907 xi->value, xi->value_len);
3909 le16_add_cpu(&xh->xh_name_value_len, new_size);
3910 ocfs2_xattr_set_local(xe, local);
3914 * Remove the old entry if there is more than one.
3915 * We don't remove the last entry so that we can
3916 * use it to indicate the hash value of the empty
3920 le16_add_cpu(&xh->xh_count, -1);
3923 (void *)last - (void *)xe);
3925 sizeof(struct ocfs2_xattr_entry));
3928 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3933 /* find a new entry for insert. */
3934 int low = 0, high = count - 1, tmp;
3935 struct ocfs2_xattr_entry *tmp_xe;
3937 while (low <= high && count) {
3938 tmp = (low + high) / 2;
3939 tmp_xe = &xh->xh_entries[tmp];
3941 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
3943 else if (name_hash <
3944 le32_to_cpu(tmp_xe->xe_name_hash))
3952 xe = &xh->xh_entries[low];
3954 memmove(xe + 1, xe, (void *)last - (void *)xe);
3956 le16_add_cpu(&xh->xh_count, 1);
3957 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
3958 xe->xe_name_hash = cpu_to_le32(name_hash);
3959 xe->xe_name_len = name_len;
3960 ocfs2_xattr_set_type(xe, xi->name_index);
3964 /* Insert the new name+value. */
3965 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
3968 * We must make sure that the name/value pair
3969 * exists in the same block.
3971 offs = le16_to_cpu(xh->xh_free_start);
3972 start = offs - size;
3974 if (start >> inode->i_sb->s_blocksize_bits !=
3975 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
3976 offs = offs - offs % blocksize;
3977 xh->xh_free_start = cpu_to_le16(offs);
3980 val = ocfs2_xattr_bucket_get_val(inode,
3981 &xs->bucket, offs - size);
3982 xe->xe_name_offset = cpu_to_le16(offs - size);
3984 memset(val, 0, size);
3985 memcpy(val, xi->name, name_len);
3986 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
3988 xe->xe_value_size = cpu_to_le64(xi->value_len);
3989 ocfs2_xattr_set_local(xe, local);
3991 le16_add_cpu(&xh->xh_free_start, -size);
3992 le16_add_cpu(&xh->xh_name_value_len, size);
3997 static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
3999 struct ocfs2_xattr_search *xs,
4000 struct buffer_head **bhs,
4003 int ret = 0, off, block_off;
4004 struct ocfs2_xattr_entry *xe = xs->here;
4007 * First calculate all the blocks we should journal_access
4008 * and journal_dirty. The first block should always be touched.
4010 ret = ocfs2_journal_dirty(handle, bhs[0]);
4014 /* calc the data. */
4015 off = le16_to_cpu(xe->xe_name_offset);
4016 block_off = off >> inode->i_sb->s_blocksize_bits;
4017 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4025 * Set the xattr entry in the specified bucket.
4026 * The bucket is indicated by xs->bucket and it should have the enough
4027 * space for the xattr insertion.
4029 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4030 struct ocfs2_xattr_info *xi,
4031 struct ocfs2_xattr_search *xs,
4036 handle_t *handle = NULL;
4037 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4038 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4040 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4041 (unsigned long)xi->value_len, xi->name_index,
4042 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4044 if (!xs->bucket.bhs[1]) {
4045 ret = ocfs2_read_blocks(inode,
4046 xs->bucket.bhs[0]->b_blocknr + 1,
4047 blk_per_bucket - 1, &xs->bucket.bhs[1],
4055 handle = ocfs2_start_trans(osb, blk_per_bucket);
4056 if (IS_ERR(handle)) {
4057 ret = PTR_ERR(handle);
4063 for (i = 0; i < blk_per_bucket; i++) {
4064 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4065 OCFS2_JOURNAL_ACCESS_WRITE);
4072 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4074 /*Only dirty the blocks we have touched in set xattr. */
4075 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4076 xs->bucket.bhs, blk_per_bucket);
4080 ocfs2_commit_trans(osb, handle);
4085 static int ocfs2_xattr_value_update_size(struct inode *inode,
4086 struct buffer_head *xe_bh,
4087 struct ocfs2_xattr_entry *xe,
4091 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4092 handle_t *handle = NULL;
4094 handle = ocfs2_start_trans(osb, 1);
4095 if (handle == NULL) {
4101 ret = ocfs2_journal_access(handle, inode, xe_bh,
4102 OCFS2_JOURNAL_ACCESS_WRITE);
4108 xe->xe_value_size = cpu_to_le64(new_size);
4110 ret = ocfs2_journal_dirty(handle, xe_bh);
4115 ocfs2_commit_trans(osb, handle);
4121 * Truncate the specified xe_off entry in xattr bucket.
4122 * bucket is indicated by header_bh and len is the new length.
4123 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4125 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4127 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4128 struct buffer_head *header_bh,
4134 struct buffer_head *value_bh = NULL;
4135 struct ocfs2_xattr_value_root *xv;
4136 struct ocfs2_xattr_entry *xe;
4137 struct ocfs2_xattr_header *xh =
4138 (struct ocfs2_xattr_header *)header_bh->b_data;
4139 size_t blocksize = inode->i_sb->s_blocksize;
4141 xe = &xh->xh_entries[xe_off];
4143 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4145 offset = le16_to_cpu(xe->xe_name_offset) +
4146 OCFS2_XATTR_SIZE(xe->xe_name_len);
4148 value_blk = offset / blocksize;
4150 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4151 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4152 value_blk += header_bh->b_blocknr;
4154 ret = ocfs2_read_block(inode, value_blk, &value_bh);
4160 xv = (struct ocfs2_xattr_value_root *)
4161 (value_bh->b_data + offset % blocksize);
4163 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4164 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4165 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4171 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4182 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4183 struct ocfs2_xattr_search *xs,
4187 struct ocfs2_xattr_entry *xe = xs->here;
4188 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4190 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4192 offset = xe - xh->xh_entries;
4193 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4201 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4202 struct ocfs2_xattr_search *xs,
4207 struct ocfs2_xattr_value_root *xv;
4208 struct ocfs2_xattr_entry *xe = xs->here;
4210 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4212 offset = le16_to_cpu(xe->xe_name_offset) +
4213 OCFS2_XATTR_SIZE(xe->xe_name_len);
4215 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4217 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4220 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4221 struct buffer_head *root_bh,
4227 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4228 struct inode *tl_inode = osb->osb_tl_inode;
4230 struct ocfs2_xattr_block *xb =
4231 (struct ocfs2_xattr_block *)root_bh->b_data;
4232 struct ocfs2_alloc_context *meta_ac = NULL;
4233 struct ocfs2_cached_dealloc_ctxt dealloc;
4234 struct ocfs2_extent_tree et;
4236 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4238 ocfs2_init_dealloc_ctxt(&dealloc);
4240 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4241 cpos, len, (unsigned long long)blkno);
4243 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4245 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4251 mutex_lock(&tl_inode->i_mutex);
4253 if (ocfs2_truncate_log_needs_flush(osb)) {
4254 ret = __ocfs2_flush_truncate_log(osb);
4261 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4262 if (handle == NULL) {
4268 ret = ocfs2_journal_access(handle, inode, root_bh,
4269 OCFS2_JOURNAL_ACCESS_WRITE);
4275 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4282 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4284 ret = ocfs2_journal_dirty(handle, root_bh);
4290 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4295 ocfs2_commit_trans(osb, handle);
4297 ocfs2_schedule_truncate_log_flush(osb, 1);
4299 mutex_unlock(&tl_inode->i_mutex);
4302 ocfs2_free_alloc_context(meta_ac);
4304 ocfs2_run_deallocs(osb, &dealloc);
4309 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4310 struct ocfs2_xattr_search *xs)
4312 handle_t *handle = NULL;
4313 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4314 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4315 le16_to_cpu(xh->xh_count) - 1];
4318 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4319 if (IS_ERR(handle)) {
4320 ret = PTR_ERR(handle);
4325 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4326 OCFS2_JOURNAL_ACCESS_WRITE);
4332 /* Remove the old entry. */
4333 memmove(xs->here, xs->here + 1,
4334 (void *)last - (void *)xs->here);
4335 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4336 le16_add_cpu(&xh->xh_count, -1);
4338 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4342 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4346 * Set the xattr name/value in the bucket specified in xs.
4348 * As the new value in xi may be stored in the bucket or in an outside cluster,
4349 * we divide the whole process into 3 steps:
4350 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4351 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4352 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4353 * 4. If the clusters for the new outside value can't be allocated, we need
4354 * to free the xattr we allocated in set.
4356 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4357 struct ocfs2_xattr_info *xi,
4358 struct ocfs2_xattr_search *xs)
4362 char *val = (char *)xi->value;
4363 struct ocfs2_xattr_entry *xe = xs->here;
4364 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4367 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4369 * We need to truncate the xattr storage first.
4371 * If both the old and new value are stored to
4372 * outside block, we only need to truncate
4373 * the storage and then set the value outside.
4375 * If the new value should be stored within block,
4376 * we should free all the outside block first and
4377 * the modification to the xattr block will be done
4378 * by following steps.
4380 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4381 value_len = xi->value_len;
4385 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4391 goto set_value_outside;
4394 value_len = xi->value_len;
4395 /* So we have to handle the inside block change now. */
4396 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4398 * If the new value will be stored outside of block,
4399 * initalize a new empty value root and insert it first.
4402 xi->value = &def_xv;
4403 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4406 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
4412 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4415 /* allocate the space now for the outside block storage. */
4416 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4421 if (xs->not_found) {
4423 * We can't allocate enough clusters for outside
4424 * storage and we have allocated xattr already,
4425 * so need to remove it.
4427 ocfs2_xattr_bucket_remove_xs(inode, xs);
4433 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4438 /* check whether the xattr bucket is filled up with the same hash value. */
4439 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4440 struct ocfs2_xattr_bucket *bucket)
4442 struct ocfs2_xattr_header *xh = bucket->xh;
4444 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4445 xh->xh_entries[0].xe_name_hash) {
4446 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4448 (unsigned long long)bucket->bhs[0]->b_blocknr,
4449 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4456 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4457 struct ocfs2_xattr_info *xi,
4458 struct ocfs2_xattr_search *xs)
4460 struct ocfs2_xattr_header *xh;
4461 struct ocfs2_xattr_entry *xe;
4462 u16 count, header_size, xh_free_start;
4463 int i, free, max_free, need, old;
4464 size_t value_size = 0, name_len = strlen(xi->name);
4465 size_t blocksize = inode->i_sb->s_blocksize;
4466 int ret, allocation = 0;
4467 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4469 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4473 count = le16_to_cpu(xh->xh_count);
4474 xh_free_start = le16_to_cpu(xh->xh_free_start);
4475 header_size = sizeof(struct ocfs2_xattr_header) +
4476 count * sizeof(struct ocfs2_xattr_entry);
4477 max_free = OCFS2_XATTR_BUCKET_SIZE -
4478 le16_to_cpu(xh->xh_name_value_len) - header_size;
4480 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4481 "of %u which exceed block size\n",
4482 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4485 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4486 value_size = OCFS2_XATTR_ROOT_SIZE;
4488 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4491 need = sizeof(struct ocfs2_xattr_entry) +
4492 OCFS2_XATTR_SIZE(name_len) + value_size;
4494 need = value_size + OCFS2_XATTR_SIZE(name_len);
4497 * We only replace the old value if the new length is smaller
4498 * than the old one. Otherwise we will allocate new space in the
4499 * bucket to store it.
4502 if (ocfs2_xattr_is_local(xe))
4503 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4505 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4507 if (old >= value_size)
4511 free = xh_free_start - header_size;
4513 * We need to make sure the new name/value pair
4514 * can exist in the same block.
4516 if (xh_free_start % blocksize < need)
4517 free -= xh_free_start % blocksize;
4519 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4520 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4521 " %u\n", xs->not_found,
4522 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4523 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4524 le16_to_cpu(xh->xh_name_value_len));
4526 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4527 if (need <= max_free &&
4528 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4530 * We can create the space by defragment. Since only the
4531 * name/value will be moved, the xe shouldn't be changed
4534 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4540 xh_free_start = le16_to_cpu(xh->xh_free_start);
4541 free = xh_free_start - header_size;
4542 if (xh_free_start % blocksize < need)
4543 free -= xh_free_start % blocksize;
4548 mlog(0, "Can't get enough space for xattr insert by "
4549 "defragment. Need %u bytes, but we have %d, so "
4550 "allocate new bucket for it.\n", need, free);
4554 * We have to add new buckets or clusters and one
4555 * allocation should leave us enough space for insert.
4560 * We do not allow for overlapping ranges between buckets. And
4561 * the maximum number of collisions we will allow for then is
4562 * one bucket's worth, so check it here whether we need to
4563 * add a new bucket for the insert.
4565 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4571 ret = ocfs2_add_new_xattr_bucket(inode,
4579 for (i = 0; i < blk_per_bucket; i++)
4580 brelse(xs->bucket.bhs[i]);
4582 memset(&xs->bucket, 0, sizeof(xs->bucket));
4584 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4587 if (ret && ret != -ENODATA)
4589 xs->not_found = ret;
4595 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4601 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4602 struct ocfs2_xattr_bucket *bucket,
4606 struct ocfs2_xattr_header *xh = bucket->xh;
4608 struct ocfs2_xattr_entry *xe;
4610 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4611 xe = &xh->xh_entries[i];
4612 if (ocfs2_xattr_is_local(xe))
4615 ret = ocfs2_xattr_bucket_value_truncate(inode,
4627 static int ocfs2_delete_xattr_index_block(struct inode *inode,
4628 struct buffer_head *xb_bh)
4630 struct ocfs2_xattr_block *xb =
4631 (struct ocfs2_xattr_block *)xb_bh->b_data;
4632 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4634 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4637 if (le16_to_cpu(el->l_next_free_rec) == 0)
4640 while (name_hash > 0) {
4641 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4642 &e_cpos, &num_clusters, el);
4648 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4649 ocfs2_delete_xattr_in_bucket,
4656 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4657 p_blkno, e_cpos, num_clusters);
4666 name_hash = e_cpos - 1;
4674 * 'trusted' attributes support
4676 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4677 size_t list_size, const char *name,
4680 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
4681 const size_t total_len = prefix_len + name_len + 1;
4683 if (list && total_len <= list_size) {
4684 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4685 memcpy(list + prefix_len, name, name_len);
4686 list[prefix_len + name_len] = '\0';
4691 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4692 void *buffer, size_t size)
4694 if (strcmp(name, "") == 0)
4696 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4700 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4701 const void *value, size_t size, int flags)
4703 if (strcmp(name, "") == 0)
4706 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4710 struct xattr_handler ocfs2_xattr_trusted_handler = {
4711 .prefix = XATTR_TRUSTED_PREFIX,
4712 .list = ocfs2_xattr_trusted_list,
4713 .get = ocfs2_xattr_trusted_get,
4714 .set = ocfs2_xattr_trusted_set,
4718 * 'user' attributes support
4720 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4721 size_t list_size, const char *name,
4724 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
4725 const size_t total_len = prefix_len + name_len + 1;
4726 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4728 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4731 if (list && total_len <= list_size) {
4732 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4733 memcpy(list + prefix_len, name, name_len);
4734 list[prefix_len + name_len] = '\0';
4739 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4740 void *buffer, size_t size)
4742 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4744 if (strcmp(name, "") == 0)
4746 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4748 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4752 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4753 const void *value, size_t size, int flags)
4755 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4757 if (strcmp(name, "") == 0)
4759 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4762 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4766 struct xattr_handler ocfs2_xattr_user_handler = {
4767 .prefix = XATTR_USER_PREFIX,
4768 .list = ocfs2_xattr_user_list,
4769 .get = ocfs2_xattr_user_get,
4770 .set = ocfs2_xattr_user_set,