1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2008 Oracle. All rights reserved.
9 * Lots of code in this file is taken from ext3.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
27 #include <linux/capability.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/highmem.h>
32 #include <linux/pagemap.h>
33 #include <linux/uio.h>
34 #include <linux/sched.h>
35 #include <linux/splice.h>
36 #include <linux/mount.h>
37 #include <linux/writeback.h>
38 #include <linux/falloc.h>
39 #include <linux/sort.h>
41 #define MLOG_MASK_PREFIX ML_XATTR
42 #include <cluster/masklog.h>
55 #include "buffer_head_io.h"
60 struct ocfs2_xattr_def_value_root {
61 struct ocfs2_xattr_value_root xv;
62 struct ocfs2_extent_rec er;
65 struct ocfs2_xattr_bucket {
66 struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
67 struct ocfs2_xattr_header *xh;
70 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
71 #define OCFS2_XATTR_INLINE_SIZE 80
73 static struct ocfs2_xattr_def_value_root def_xv = {
74 .xv.xr_list.l_count = cpu_to_le16(1),
77 struct xattr_handler *ocfs2_xattr_handlers[] = {
78 &ocfs2_xattr_user_handler,
79 &ocfs2_xattr_trusted_handler,
83 static struct xattr_handler *ocfs2_xattr_handler_map[] = {
84 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
85 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
88 struct ocfs2_xattr_info {
95 struct ocfs2_xattr_search {
96 struct buffer_head *inode_bh;
98 * xattr_bh point to the block buffer head which has extended attribute
99 * when extended attribute in inode, xattr_bh is equal to inode_bh.
101 struct buffer_head *xattr_bh;
102 struct ocfs2_xattr_header *header;
103 struct ocfs2_xattr_bucket bucket;
106 struct ocfs2_xattr_entry *here;
110 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
111 struct ocfs2_xattr_header *xh,
116 static int ocfs2_xattr_index_block_find(struct inode *inode,
117 struct buffer_head *root_bh,
120 struct ocfs2_xattr_search *xs);
122 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
123 struct ocfs2_xattr_tree_root *xt,
127 static int ocfs2_xattr_create_index_block(struct inode *inode,
128 struct ocfs2_xattr_search *xs);
130 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
131 struct ocfs2_xattr_info *xi,
132 struct ocfs2_xattr_search *xs);
134 static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
136 struct xattr_handler *handler = NULL;
138 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
139 handler = ocfs2_xattr_handler_map[name_index];
144 static inline u32 ocfs2_xattr_name_hash(struct inode *inode,
150 /* Get hash value of uuid from super block */
151 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
154 /* hash extended attribute prefix */
155 for (i = 0; i < prefix_len; i++) {
156 hash = (hash << OCFS2_HASH_SHIFT) ^
157 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
160 /* hash extended attribute name */
161 for (i = 0; i < name_len; i++) {
162 hash = (hash << OCFS2_HASH_SHIFT) ^
163 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
171 * ocfs2_xattr_hash_entry()
173 * Compute the hash of an extended attribute.
175 static void ocfs2_xattr_hash_entry(struct inode *inode,
176 struct ocfs2_xattr_header *header,
177 struct ocfs2_xattr_entry *entry)
180 struct xattr_handler *handler =
181 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
182 char *prefix = handler->prefix;
183 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
184 int prefix_len = strlen(handler->prefix);
186 hash = ocfs2_xattr_name_hash(inode, prefix, prefix_len, name,
188 entry->xe_name_hash = cpu_to_le32(hash);
193 static int ocfs2_xattr_extend_allocation(struct inode *inode,
195 struct buffer_head *xattr_bh,
196 struct ocfs2_xattr_value_root *xv)
199 int restart_func = 0;
201 handle_t *handle = NULL;
202 struct ocfs2_alloc_context *data_ac = NULL;
203 struct ocfs2_alloc_context *meta_ac = NULL;
204 enum ocfs2_alloc_restarted why;
205 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
206 struct ocfs2_extent_list *root_el = &xv->xr_list;
207 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
209 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
213 status = ocfs2_lock_allocators(inode, xattr_bh, root_el,
214 clusters_to_add, 0, &data_ac,
215 &meta_ac, OCFS2_XATTR_VALUE_EXTENT, xv);
221 credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add);
222 handle = ocfs2_start_trans(osb, credits);
223 if (IS_ERR(handle)) {
224 status = PTR_ERR(handle);
230 restarted_transaction:
231 status = ocfs2_journal_access(handle, inode, xattr_bh,
232 OCFS2_JOURNAL_ACCESS_WRITE);
238 prev_clusters = le32_to_cpu(xv->xr_clusters);
239 status = ocfs2_add_clusters_in_btree(osb,
250 OCFS2_XATTR_VALUE_EXTENT,
252 if ((status < 0) && (status != -EAGAIN)) {
253 if (status != -ENOSPC)
258 status = ocfs2_journal_dirty(handle, xattr_bh);
264 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
266 if (why != RESTART_NONE && clusters_to_add) {
267 if (why == RESTART_META) {
268 mlog(0, "restarting function.\n");
271 BUG_ON(why != RESTART_TRANS);
273 mlog(0, "restarting transaction.\n");
274 /* TODO: This can be more intelligent. */
275 credits = ocfs2_calc_extend_credits(osb->sb,
278 status = ocfs2_extend_trans(handle, credits);
280 /* handle still has to be committed at
286 goto restarted_transaction;
292 ocfs2_commit_trans(osb, handle);
296 ocfs2_free_alloc_context(data_ac);
300 ocfs2_free_alloc_context(meta_ac);
303 if ((!status) && restart_func) {
311 static int __ocfs2_remove_xattr_range(struct inode *inode,
312 struct buffer_head *root_bh,
313 struct ocfs2_xattr_value_root *xv,
314 u32 cpos, u32 phys_cpos, u32 len,
315 struct ocfs2_cached_dealloc_ctxt *dealloc)
318 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
319 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
320 struct inode *tl_inode = osb->osb_tl_inode;
322 struct ocfs2_alloc_context *meta_ac = NULL;
324 ret = ocfs2_lock_allocators(inode, root_bh, &xv->xr_list,
325 0, 1, NULL, &meta_ac,
326 OCFS2_XATTR_VALUE_EXTENT, xv);
332 mutex_lock(&tl_inode->i_mutex);
334 if (ocfs2_truncate_log_needs_flush(osb)) {
335 ret = __ocfs2_flush_truncate_log(osb);
342 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
343 if (IS_ERR(handle)) {
344 ret = PTR_ERR(handle);
349 ret = ocfs2_journal_access(handle, inode, root_bh,
350 OCFS2_JOURNAL_ACCESS_WRITE);
356 ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac,
357 dealloc, OCFS2_XATTR_VALUE_EXTENT, xv);
363 le32_add_cpu(&xv->xr_clusters, -len);
365 ret = ocfs2_journal_dirty(handle, root_bh);
371 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
376 ocfs2_commit_trans(osb, handle);
378 mutex_unlock(&tl_inode->i_mutex);
381 ocfs2_free_alloc_context(meta_ac);
386 static int ocfs2_xattr_shrink_size(struct inode *inode,
389 struct buffer_head *root_bh,
390 struct ocfs2_xattr_value_root *xv)
393 u32 trunc_len, cpos, phys_cpos, alloc_size;
395 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
396 struct ocfs2_cached_dealloc_ctxt dealloc;
398 ocfs2_init_dealloc_ctxt(&dealloc);
400 if (old_clusters <= new_clusters)
404 trunc_len = old_clusters - new_clusters;
406 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
407 &alloc_size, &xv->xr_list);
413 if (alloc_size > trunc_len)
414 alloc_size = trunc_len;
416 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
417 phys_cpos, alloc_size,
424 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
425 ocfs2_remove_xattr_clusters_from_cache(inode, block,
428 trunc_len -= alloc_size;
432 ocfs2_schedule_truncate_log_flush(osb, 1);
433 ocfs2_run_deallocs(osb, &dealloc);
438 static int ocfs2_xattr_value_truncate(struct inode *inode,
439 struct buffer_head *root_bh,
440 struct ocfs2_xattr_value_root *xv,
444 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
445 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
447 if (new_clusters == old_clusters)
450 if (new_clusters > old_clusters)
451 ret = ocfs2_xattr_extend_allocation(inode,
452 new_clusters - old_clusters,
455 ret = ocfs2_xattr_shrink_size(inode,
456 old_clusters, new_clusters,
462 static int ocfs2_xattr_list_entries(struct inode *inode,
463 struct ocfs2_xattr_header *header,
464 char *buffer, size_t buffer_size)
466 size_t rest = buffer_size;
469 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
470 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
471 struct xattr_handler *handler =
472 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
475 size_t size = handler->list(inode, buffer, rest,
477 le16_to_cpu(entry->xe_name_offset)),
488 return buffer_size - rest;
491 static int ocfs2_xattr_ibody_list(struct inode *inode,
492 struct ocfs2_dinode *di,
496 struct ocfs2_xattr_header *header = NULL;
497 struct ocfs2_inode_info *oi = OCFS2_I(inode);
500 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
503 header = (struct ocfs2_xattr_header *)
504 ((void *)di + inode->i_sb->s_blocksize -
505 le16_to_cpu(di->i_xattr_inline_size));
507 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
512 static int ocfs2_xattr_block_list(struct inode *inode,
513 struct ocfs2_dinode *di,
517 struct buffer_head *blk_bh = NULL;
518 struct ocfs2_xattr_block *xb;
521 if (!di->i_xattr_loc)
524 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
525 le64_to_cpu(di->i_xattr_loc),
526 &blk_bh, OCFS2_BH_CACHED, inode);
531 /*Verify the signature of xattr block*/
532 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
533 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
538 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
540 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
541 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
542 ret = ocfs2_xattr_list_entries(inode, header,
543 buffer, buffer_size);
545 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
546 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
547 buffer, buffer_size);
555 ssize_t ocfs2_listxattr(struct dentry *dentry,
559 int ret = 0, i_ret = 0, b_ret = 0;
560 struct buffer_head *di_bh = NULL;
561 struct ocfs2_dinode *di = NULL;
562 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
564 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
567 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
573 di = (struct ocfs2_dinode *)di_bh->b_data;
575 down_read(&oi->ip_xattr_sem);
576 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
584 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
589 up_read(&oi->ip_xattr_sem);
590 ocfs2_inode_unlock(dentry->d_inode, 0);
594 return i_ret + b_ret;
597 static int ocfs2_xattr_find_entry(int name_index,
599 struct ocfs2_xattr_search *xs)
601 struct ocfs2_xattr_entry *entry;
608 name_len = strlen(name);
610 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
611 cmp = name_index - ocfs2_xattr_get_type(entry);
613 cmp = name_len - entry->xe_name_len;
615 cmp = memcmp(name, (xs->base +
616 le16_to_cpu(entry->xe_name_offset)),
624 return cmp ? -ENODATA : 0;
627 static int ocfs2_xattr_get_value_outside(struct inode *inode,
628 struct ocfs2_xattr_value_root *xv,
632 u32 cpos, p_cluster, num_clusters, bpc, clusters;
635 size_t cplen, blocksize;
636 struct buffer_head *bh = NULL;
637 struct ocfs2_extent_list *el;
640 clusters = le32_to_cpu(xv->xr_clusters);
641 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
642 blocksize = inode->i_sb->s_blocksize;
645 while (cpos < clusters) {
646 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
653 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
654 /* Copy ocfs2_xattr_value */
655 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
656 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
657 &bh, OCFS2_BH_CACHED, inode);
663 cplen = len >= blocksize ? blocksize : len;
664 memcpy(buffer, bh->b_data, cplen);
673 cpos += num_clusters;
679 static int ocfs2_xattr_ibody_get(struct inode *inode,
684 struct ocfs2_xattr_search *xs)
686 struct ocfs2_inode_info *oi = OCFS2_I(inode);
687 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
688 struct ocfs2_xattr_value_root *xv;
692 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
695 xs->end = (void *)di + inode->i_sb->s_blocksize;
696 xs->header = (struct ocfs2_xattr_header *)
697 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
698 xs->base = (void *)xs->header;
699 xs->here = xs->header->xh_entries;
701 ret = ocfs2_xattr_find_entry(name_index, name, xs);
704 size = le64_to_cpu(xs->here->xe_value_size);
706 if (size > buffer_size)
708 if (ocfs2_xattr_is_local(xs->here)) {
709 memcpy(buffer, (void *)xs->base +
710 le16_to_cpu(xs->here->xe_name_offset) +
711 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
713 xv = (struct ocfs2_xattr_value_root *)
714 (xs->base + le16_to_cpu(
715 xs->here->xe_name_offset) +
716 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
717 ret = ocfs2_xattr_get_value_outside(inode, xv,
729 static int ocfs2_xattr_block_get(struct inode *inode,
734 struct ocfs2_xattr_search *xs)
736 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
737 struct buffer_head *blk_bh = NULL;
738 struct ocfs2_xattr_block *xb;
739 struct ocfs2_xattr_value_root *xv;
741 int ret = -ENODATA, name_offset, name_len, block_off, i;
743 if (!di->i_xattr_loc)
746 memset(&xs->bucket, 0, sizeof(xs->bucket));
748 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
749 le64_to_cpu(di->i_xattr_loc),
750 &blk_bh, OCFS2_BH_CACHED, inode);
755 /*Verify the signature of xattr block*/
756 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
757 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
762 xs->xattr_bh = blk_bh;
763 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
765 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
766 xs->header = &xb->xb_attrs.xb_header;
767 xs->base = (void *)xs->header;
768 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
769 xs->here = xs->header->xh_entries;
771 ret = ocfs2_xattr_find_entry(name_index, name, xs);
773 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
779 size = le64_to_cpu(xs->here->xe_value_size);
782 if (size > buffer_size)
785 name_offset = le16_to_cpu(xs->here->xe_name_offset);
786 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
787 i = xs->here - xs->header->xh_entries;
789 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
790 ret = ocfs2_xattr_bucket_get_name_value(inode,
795 xs->base = xs->bucket.bhs[block_off]->b_data;
797 if (ocfs2_xattr_is_local(xs->here)) {
798 memcpy(buffer, (void *)xs->base +
799 name_offset + name_len, size);
801 xv = (struct ocfs2_xattr_value_root *)
802 (xs->base + name_offset + name_len);
803 ret = ocfs2_xattr_get_value_outside(inode, xv,
813 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
814 brelse(xs->bucket.bhs[i]);
815 memset(&xs->bucket, 0, sizeof(xs->bucket));
823 * Copy an extended attribute into the buffer provided.
824 * Buffer is NULL to compute the size of buffer required.
826 int ocfs2_xattr_get(struct inode *inode,
833 struct ocfs2_dinode *di = NULL;
834 struct buffer_head *di_bh = NULL;
835 struct ocfs2_inode_info *oi = OCFS2_I(inode);
836 struct ocfs2_xattr_search xis = {
837 .not_found = -ENODATA,
839 struct ocfs2_xattr_search xbs = {
840 .not_found = -ENODATA,
843 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
846 ret = ocfs2_inode_lock(inode, &di_bh, 0);
851 xis.inode_bh = xbs.inode_bh = di_bh;
852 di = (struct ocfs2_dinode *)di_bh->b_data;
854 down_read(&oi->ip_xattr_sem);
855 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
858 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
860 up_read(&oi->ip_xattr_sem);
861 ocfs2_inode_unlock(inode, 0);
868 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
869 struct ocfs2_xattr_value_root *xv,
873 int ret = 0, i, cp_len, credits;
874 u16 blocksize = inode->i_sb->s_blocksize;
875 u32 p_cluster, num_clusters;
876 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
877 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
879 struct buffer_head *bh = NULL;
882 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
884 credits = clusters * bpc;
885 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
886 if (IS_ERR(handle)) {
887 ret = PTR_ERR(handle);
892 while (cpos < clusters) {
893 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
894 &num_clusters, &xv->xr_list);
900 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
902 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
903 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
904 &bh, OCFS2_BH_CACHED, inode);
910 ret = ocfs2_journal_access(handle,
913 OCFS2_JOURNAL_ACCESS_WRITE);
919 cp_len = value_len > blocksize ? blocksize : value_len;
920 memcpy(bh->b_data, value, cp_len);
923 if (cp_len < blocksize)
924 memset(bh->b_data + cp_len, 0,
927 ret = ocfs2_journal_dirty(handle, bh);
936 * XXX: do we need to empty all the following
937 * blocks in this cluster?
942 cpos += num_clusters;
945 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
952 static int ocfs2_xattr_cleanup(struct inode *inode,
953 struct ocfs2_xattr_info *xi,
954 struct ocfs2_xattr_search *xs,
957 handle_t *handle = NULL;
959 size_t name_len = strlen(xi->name);
960 void *val = xs->base + offs;
961 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
963 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
964 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
965 if (IS_ERR(handle)) {
966 ret = PTR_ERR(handle);
970 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
971 OCFS2_JOURNAL_ACCESS_WRITE);
976 /* Decrease xattr count */
977 le16_add_cpu(&xs->header->xh_count, -1);
978 /* Remove the xattr entry and tree root which has already be set*/
979 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
980 memset(val, 0, size);
982 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
986 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
991 static int ocfs2_xattr_update_entry(struct inode *inode,
992 struct ocfs2_xattr_info *xi,
993 struct ocfs2_xattr_search *xs,
996 handle_t *handle = NULL;
999 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1000 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1001 if (IS_ERR(handle)) {
1002 ret = PTR_ERR(handle);
1006 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1007 OCFS2_JOURNAL_ACCESS_WRITE);
1013 xs->here->xe_name_offset = cpu_to_le16(offs);
1014 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1015 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1016 ocfs2_xattr_set_local(xs->here, 1);
1018 ocfs2_xattr_set_local(xs->here, 0);
1019 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1021 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1025 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1031 * ocfs2_xattr_set_value_outside()
1033 * Set large size value in B tree.
1035 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1036 struct ocfs2_xattr_info *xi,
1037 struct ocfs2_xattr_search *xs,
1040 size_t name_len = strlen(xi->name);
1041 void *val = xs->base + offs;
1042 struct ocfs2_xattr_value_root *xv = NULL;
1043 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1046 memset(val, 0, size);
1047 memcpy(val, xi->name, name_len);
1048 xv = (struct ocfs2_xattr_value_root *)
1049 (val + OCFS2_XATTR_SIZE(name_len));
1050 xv->xr_clusters = 0;
1051 xv->xr_last_eb_blk = 0;
1052 xv->xr_list.l_tree_depth = 0;
1053 xv->xr_list.l_count = cpu_to_le16(1);
1054 xv->xr_list.l_next_free_rec = 0;
1056 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1062 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1068 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1076 * ocfs2_xattr_set_entry_local()
1078 * Set, replace or remove extended attribute in local.
1080 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1081 struct ocfs2_xattr_info *xi,
1082 struct ocfs2_xattr_search *xs,
1083 struct ocfs2_xattr_entry *last,
1086 size_t name_len = strlen(xi->name);
1089 if (xi->value && xs->not_found) {
1090 /* Insert the new xattr entry. */
1091 le16_add_cpu(&xs->header->xh_count, 1);
1092 ocfs2_xattr_set_type(last, xi->name_index);
1093 ocfs2_xattr_set_local(last, 1);
1094 last->xe_name_len = name_len;
1100 first_val = xs->base + min_offs;
1101 offs = le16_to_cpu(xs->here->xe_name_offset);
1102 val = xs->base + offs;
1104 if (le64_to_cpu(xs->here->xe_value_size) >
1105 OCFS2_XATTR_INLINE_SIZE)
1106 size = OCFS2_XATTR_SIZE(name_len) +
1107 OCFS2_XATTR_ROOT_SIZE;
1109 size = OCFS2_XATTR_SIZE(name_len) +
1110 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1112 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1113 OCFS2_XATTR_SIZE(xi->value_len)) {
1114 /* The old and the new value have the
1115 same size. Just replace the value. */
1116 ocfs2_xattr_set_local(xs->here, 1);
1117 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1118 /* Clear value bytes. */
1119 memset(val + OCFS2_XATTR_SIZE(name_len),
1121 OCFS2_XATTR_SIZE(xi->value_len));
1122 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1127 /* Remove the old name+value. */
1128 memmove(first_val + size, first_val, val - first_val);
1129 memset(first_val, 0, size);
1130 xs->here->xe_name_hash = 0;
1131 xs->here->xe_name_offset = 0;
1132 ocfs2_xattr_set_local(xs->here, 1);
1133 xs->here->xe_value_size = 0;
1137 /* Adjust all value offsets. */
1138 last = xs->header->xh_entries;
1139 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1140 size_t o = le16_to_cpu(last->xe_name_offset);
1143 last->xe_name_offset = cpu_to_le16(o + size);
1148 /* Remove the old entry. */
1150 memmove(xs->here, xs->here + 1,
1151 (void *)last - (void *)xs->here);
1152 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1153 le16_add_cpu(&xs->header->xh_count, -1);
1157 /* Insert the new name+value. */
1158 size_t size = OCFS2_XATTR_SIZE(name_len) +
1159 OCFS2_XATTR_SIZE(xi->value_len);
1160 void *val = xs->base + min_offs - size;
1162 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1163 memset(val, 0, size);
1164 memcpy(val, xi->name, name_len);
1165 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1168 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1169 ocfs2_xattr_set_local(xs->here, 1);
1170 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1177 * ocfs2_xattr_set_entry()
1179 * Set extended attribute entry into inode or block.
1181 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1182 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1183 * then set value in B tree with set_value_outside().
1185 static int ocfs2_xattr_set_entry(struct inode *inode,
1186 struct ocfs2_xattr_info *xi,
1187 struct ocfs2_xattr_search *xs,
1190 struct ocfs2_xattr_entry *last;
1191 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1192 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1193 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1195 handle_t *handle = NULL;
1197 struct ocfs2_xattr_info xi_l = {
1198 .name_index = xi->name_index,
1201 .value_len = xi->value_len,
1204 /* Compute min_offs, last and free space. */
1205 last = xs->header->xh_entries;
1207 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1208 size_t offs = le16_to_cpu(last->xe_name_offset);
1209 if (offs < min_offs)
1214 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1218 if (!xs->not_found) {
1220 if (ocfs2_xattr_is_local(xs->here))
1221 size = OCFS2_XATTR_SIZE(name_len) +
1222 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1224 size = OCFS2_XATTR_SIZE(name_len) +
1225 OCFS2_XATTR_ROOT_SIZE;
1226 free += (size + sizeof(struct ocfs2_xattr_entry));
1228 /* Check free space in inode or block */
1229 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1230 if (free < sizeof(struct ocfs2_xattr_entry) +
1231 OCFS2_XATTR_SIZE(name_len) +
1232 OCFS2_XATTR_ROOT_SIZE) {
1236 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1237 xi_l.value = (void *)&def_xv;
1238 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1239 } else if (xi->value) {
1240 if (free < sizeof(struct ocfs2_xattr_entry) +
1241 OCFS2_XATTR_SIZE(name_len) +
1242 OCFS2_XATTR_SIZE(xi->value_len)) {
1248 if (!xs->not_found) {
1249 /* For existing extended attribute */
1250 size_t size = OCFS2_XATTR_SIZE(name_len) +
1251 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1252 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1253 void *val = xs->base + offs;
1255 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1256 /* Replace existing local xattr with tree root */
1257 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1262 } else if (!ocfs2_xattr_is_local(xs->here)) {
1263 /* For existing xattr which has value outside */
1264 struct ocfs2_xattr_value_root *xv = NULL;
1265 xv = (struct ocfs2_xattr_value_root *)(val +
1266 OCFS2_XATTR_SIZE(name_len));
1268 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1270 * If new value need set outside also,
1271 * first truncate old value to new value,
1272 * then set new value with set_value_outside().
1274 ret = ocfs2_xattr_value_truncate(inode,
1283 ret = __ocfs2_xattr_set_value_outside(inode,
1292 ret = ocfs2_xattr_update_entry(inode,
1301 * If new value need set in local,
1302 * just trucate old value to zero.
1304 ret = ocfs2_xattr_value_truncate(inode,
1314 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1315 OCFS2_INODE_UPDATE_CREDITS);
1316 if (IS_ERR(handle)) {
1317 ret = PTR_ERR(handle);
1322 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1323 OCFS2_JOURNAL_ACCESS_WRITE);
1329 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1330 /*set extended attribue in external blcok*/
1331 ret = ocfs2_extend_trans(handle,
1332 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1337 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1338 OCFS2_JOURNAL_ACCESS_WRITE);
1346 * Set value in local, include set tree root in local.
1347 * This is the first step for value size >INLINE_SIZE.
1349 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1351 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1352 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1359 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1360 (flag & OCFS2_INLINE_XATTR_FL)) {
1361 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1362 unsigned int xattrsize = osb->s_xattr_inline_size;
1365 * Adjust extent record count or inline data size
1366 * to reserve space for extended attribute.
1368 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1369 struct ocfs2_inline_data *idata = &di->id2.i_data;
1370 le16_add_cpu(&idata->id_count, -xattrsize);
1371 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1372 struct ocfs2_extent_list *el = &di->id2.i_list;
1373 le16_add_cpu(&el->l_count, -(xattrsize /
1374 sizeof(struct ocfs2_extent_rec)));
1376 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1378 /* Update xattr flag */
1379 spin_lock(&oi->ip_lock);
1380 oi->ip_dyn_features |= flag;
1381 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1382 spin_unlock(&oi->ip_lock);
1383 /* Update inode ctime */
1384 inode->i_ctime = CURRENT_TIME;
1385 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1386 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1388 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1393 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1395 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1397 * Set value outside in B tree.
1398 * This is the second step for value size > INLINE_SIZE.
1400 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1401 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1407 * If set value outside failed, we have to clean
1408 * the junk tree root we have already set in local.
1410 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1420 static int ocfs2_xattr_free_block(handle_t *handle,
1421 struct ocfs2_super *osb,
1422 struct ocfs2_xattr_block *xb)
1424 struct inode *xb_alloc_inode;
1425 struct buffer_head *xb_alloc_bh = NULL;
1426 u64 blk = le64_to_cpu(xb->xb_blkno);
1427 u16 bit = le16_to_cpu(xb->xb_suballoc_bit);
1428 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1431 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1432 EXTENT_ALLOC_SYSTEM_INODE,
1433 le16_to_cpu(xb->xb_suballoc_slot));
1434 if (!xb_alloc_inode) {
1439 mutex_lock(&xb_alloc_inode->i_mutex);
1441 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1446 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
1451 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1456 ocfs2_inode_unlock(xb_alloc_inode, 1);
1457 brelse(xb_alloc_bh);
1459 mutex_unlock(&xb_alloc_inode->i_mutex);
1460 iput(xb_alloc_inode);
1465 static int ocfs2_remove_value_outside(struct inode*inode,
1466 struct buffer_head *bh,
1467 struct ocfs2_xattr_header *header)
1471 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1472 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1474 if (!ocfs2_xattr_is_local(entry)) {
1475 struct ocfs2_xattr_value_root *xv;
1478 val = (void *)header +
1479 le16_to_cpu(entry->xe_name_offset);
1480 xv = (struct ocfs2_xattr_value_root *)
1481 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1482 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1493 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1494 struct buffer_head *di_bh)
1497 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1498 struct ocfs2_xattr_header *header;
1501 header = (struct ocfs2_xattr_header *)
1502 ((void *)di + inode->i_sb->s_blocksize -
1503 le16_to_cpu(di->i_xattr_inline_size));
1505 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1510 static int ocfs2_xattr_block_remove(struct inode *inode,
1511 struct buffer_head *blk_bh)
1513 struct ocfs2_xattr_block *xb;
1514 struct ocfs2_xattr_header *header;
1517 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1518 header = &(xb->xb_attrs.xb_header);
1520 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1526 * ocfs2_xattr_remove()
1528 * Free extended attribute resources associated with this inode.
1530 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1532 struct ocfs2_xattr_block *xb;
1533 struct buffer_head *blk_bh = NULL;
1534 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1535 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1536 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1540 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1543 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1544 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1550 if (di->i_xattr_loc) {
1551 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1552 le64_to_cpu(di->i_xattr_loc),
1553 &blk_bh, OCFS2_BH_CACHED, inode);
1558 /*Verify the signature of xattr block*/
1559 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1560 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1565 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1572 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1573 OCFS2_INODE_UPDATE_CREDITS);
1574 if (IS_ERR(handle)) {
1575 ret = PTR_ERR(handle);
1579 ret = ocfs2_journal_access(handle, inode, di_bh,
1580 OCFS2_JOURNAL_ACCESS_WRITE);
1586 if (di->i_xattr_loc) {
1587 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1588 ocfs2_xattr_free_block(handle, osb, xb);
1589 di->i_xattr_loc = cpu_to_le64(0);
1592 spin_lock(&oi->ip_lock);
1593 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1594 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1595 spin_unlock(&oi->ip_lock);
1597 ret = ocfs2_journal_dirty(handle, di_bh);
1601 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1608 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1609 struct ocfs2_dinode *di)
1611 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1612 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1615 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1618 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1619 struct ocfs2_inline_data *idata = &di->id2.i_data;
1620 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1621 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1622 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1623 le64_to_cpu(di->i_size);
1625 struct ocfs2_extent_list *el = &di->id2.i_list;
1626 free = (le16_to_cpu(el->l_count) -
1627 le16_to_cpu(el->l_next_free_rec)) *
1628 sizeof(struct ocfs2_extent_rec);
1630 if (free >= xattrsize)
1637 * ocfs2_xattr_ibody_find()
1639 * Find extended attribute in inode block and
1640 * fill search info into struct ocfs2_xattr_search.
1642 static int ocfs2_xattr_ibody_find(struct inode *inode,
1645 struct ocfs2_xattr_search *xs)
1647 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1648 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1652 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1655 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1656 down_read(&oi->ip_alloc_sem);
1657 has_space = ocfs2_xattr_has_space_inline(inode, di);
1658 up_read(&oi->ip_alloc_sem);
1663 xs->xattr_bh = xs->inode_bh;
1664 xs->end = (void *)di + inode->i_sb->s_blocksize;
1665 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1666 xs->header = (struct ocfs2_xattr_header *)
1667 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1669 xs->header = (struct ocfs2_xattr_header *)
1670 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1671 xs->base = (void *)xs->header;
1672 xs->here = xs->header->xh_entries;
1674 /* Find the named attribute. */
1675 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1676 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1677 if (ret && ret != -ENODATA)
1679 xs->not_found = ret;
1686 * ocfs2_xattr_ibody_set()
1688 * Set, replace or remove an extended attribute into inode block.
1691 static int ocfs2_xattr_ibody_set(struct inode *inode,
1692 struct ocfs2_xattr_info *xi,
1693 struct ocfs2_xattr_search *xs)
1695 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1696 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1699 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1702 down_write(&oi->ip_alloc_sem);
1703 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1704 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1710 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1711 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1713 up_write(&oi->ip_alloc_sem);
1719 * ocfs2_xattr_block_find()
1721 * Find extended attribute in external block and
1722 * fill search info into struct ocfs2_xattr_search.
1724 static int ocfs2_xattr_block_find(struct inode *inode,
1727 struct ocfs2_xattr_search *xs)
1729 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1730 struct buffer_head *blk_bh = NULL;
1731 struct ocfs2_xattr_block *xb;
1734 if (!di->i_xattr_loc)
1737 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1738 le64_to_cpu(di->i_xattr_loc),
1739 &blk_bh, OCFS2_BH_CACHED, inode);
1744 /*Verify the signature of xattr block*/
1745 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1746 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1751 xs->xattr_bh = blk_bh;
1752 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1754 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1755 xs->header = &xb->xb_attrs.xb_header;
1756 xs->base = (void *)xs->header;
1757 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1758 xs->here = xs->header->xh_entries;
1760 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1762 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1766 if (ret && ret != -ENODATA) {
1767 xs->xattr_bh = NULL;
1770 xs->not_found = ret;
1779 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1780 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1783 static int ocfs2_restore_xattr_block(struct inode *inode,
1784 struct ocfs2_xattr_search *xs)
1788 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1789 struct ocfs2_xattr_block *xb =
1790 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1791 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
1792 u16 xb_flags = le16_to_cpu(xb->xb_flags);
1794 BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
1795 le16_to_cpu(el->l_next_free_rec) != 0);
1797 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1798 if (IS_ERR(handle)) {
1799 ret = PTR_ERR(handle);
1804 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1805 OCFS2_JOURNAL_ACCESS_WRITE);
1811 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
1812 offsetof(struct ocfs2_xattr_block, xb_attrs));
1814 xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
1816 ocfs2_journal_dirty(handle, xs->xattr_bh);
1819 ocfs2_commit_trans(osb, handle);
1825 * ocfs2_xattr_block_set()
1827 * Set, replace or remove an extended attribute into external block.
1830 static int ocfs2_xattr_block_set(struct inode *inode,
1831 struct ocfs2_xattr_info *xi,
1832 struct ocfs2_xattr_search *xs)
1834 struct buffer_head *new_bh = NULL;
1835 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1836 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1837 struct ocfs2_alloc_context *meta_ac = NULL;
1838 handle_t *handle = NULL;
1839 struct ocfs2_xattr_block *xblk = NULL;
1840 u16 suballoc_bit_start;
1845 if (!xs->xattr_bh) {
1847 * Alloc one external block for extended attribute
1850 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1855 handle = ocfs2_start_trans(osb,
1856 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1857 if (IS_ERR(handle)) {
1858 ret = PTR_ERR(handle);
1862 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1863 OCFS2_JOURNAL_ACCESS_CREATE);
1869 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1870 &suballoc_bit_start, &num_got,
1877 new_bh = sb_getblk(inode->i_sb, first_blkno);
1878 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1880 ret = ocfs2_journal_access(handle, inode, new_bh,
1881 OCFS2_JOURNAL_ACCESS_CREATE);
1887 /* Initialize ocfs2_xattr_block */
1888 xs->xattr_bh = new_bh;
1889 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1890 memset(xblk, 0, inode->i_sb->s_blocksize);
1891 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1892 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1893 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1894 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1895 xblk->xb_blkno = cpu_to_le64(first_blkno);
1897 xs->header = &xblk->xb_attrs.xb_header;
1898 xs->base = (void *)xs->header;
1899 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1900 xs->here = xs->header->xh_entries;
1903 ret = ocfs2_journal_dirty(handle, new_bh);
1908 di->i_xattr_loc = cpu_to_le64(first_blkno);
1909 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1913 ocfs2_commit_trans(osb, handle);
1916 ocfs2_free_alloc_context(meta_ac);
1920 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1922 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1923 /* Set extended attribute into external block */
1924 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1925 if (!ret || ret != -ENOSPC)
1928 ret = ocfs2_xattr_create_index_block(inode, xs);
1933 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1934 if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
1935 ret = ocfs2_restore_xattr_block(inode, xs);
1945 * Set, replace or remove an extended attribute for this inode.
1946 * value is NULL to remove an existing extended attribute, else either
1947 * create or replace an extended attribute.
1949 int ocfs2_xattr_set(struct inode *inode,
1956 struct buffer_head *di_bh = NULL;
1957 struct ocfs2_dinode *di;
1959 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1961 struct ocfs2_xattr_info xi = {
1962 .name_index = name_index,
1965 .value_len = value_len,
1968 struct ocfs2_xattr_search xis = {
1969 .not_found = -ENODATA,
1972 struct ocfs2_xattr_search xbs = {
1973 .not_found = -ENODATA,
1976 ret = ocfs2_inode_lock(inode, &di_bh, 1);
1981 xis.inode_bh = xbs.inode_bh = di_bh;
1982 di = (struct ocfs2_dinode *)di_bh->b_data;
1984 down_write(&OCFS2_I(inode)->ip_xattr_sem);
1986 * Scan inode and external block to find the same name
1987 * extended attribute and collect search infomation.
1989 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
1992 if (xis.not_found) {
1993 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
1998 if (xis.not_found && xbs.not_found) {
2000 if (flags & XATTR_REPLACE)
2007 if (flags & XATTR_CREATE)
2012 /* Remove existing extended attribute */
2014 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2015 else if (!xbs.not_found)
2016 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2018 /* We always try to set extended attribute into inode first*/
2019 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2020 if (!ret && !xbs.not_found) {
2022 * If succeed and that extended attribute existing in
2023 * external block, then we will remove it.
2027 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2028 } else if (ret == -ENOSPC) {
2029 if (di->i_xattr_loc && !xbs.xattr_bh) {
2030 ret = ocfs2_xattr_block_find(inode, name_index,
2036 * If no space in inode, we will set extended attribute
2037 * into external block.
2039 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2042 if (!xis.not_found) {
2044 * If succeed and that extended attribute
2045 * existing in inode, we will remove it.
2049 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2054 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2055 ocfs2_inode_unlock(inode, 1);
2057 brelse(xbs.xattr_bh);
2058 for (i = 0; i < blk_per_bucket; i++)
2059 brelse(xbs.bucket.bhs[i]);
2064 static inline u32 ocfs2_xattr_hash_by_name(struct inode *inode,
2066 const char *suffix_name)
2068 struct xattr_handler *handler = ocfs2_xattr_handler(name_index);
2069 char *prefix = handler->prefix;
2070 int prefix_len = strlen(handler->prefix);
2072 return ocfs2_xattr_name_hash(inode, prefix, prefix_len,
2073 (char *)suffix_name, strlen(suffix_name));
2077 * Find the xattr extent rec which may contains name_hash.
2078 * e_cpos will be the first name hash of the xattr rec.
2079 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2081 static int ocfs2_xattr_get_rec(struct inode *inode,
2086 struct ocfs2_extent_list *el)
2089 struct buffer_head *eb_bh = NULL;
2090 struct ocfs2_extent_block *eb;
2091 struct ocfs2_extent_rec *rec = NULL;
2094 if (el->l_tree_depth) {
2095 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2101 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2104 if (el->l_tree_depth) {
2105 ocfs2_error(inode->i_sb,
2106 "Inode %lu has non zero tree depth in "
2107 "xattr tree block %llu\n", inode->i_ino,
2108 (unsigned long long)eb_bh->b_blocknr);
2114 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2115 rec = &el->l_recs[i];
2117 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2118 e_blkno = le64_to_cpu(rec->e_blkno);
2124 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2125 "record (%u, %u, 0) in xattr", inode->i_ino,
2126 le32_to_cpu(rec->e_cpos),
2127 ocfs2_rec_clusters(el, rec));
2132 *p_blkno = le64_to_cpu(rec->e_blkno);
2133 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2135 *e_cpos = le32_to_cpu(rec->e_cpos);
2141 typedef int (xattr_bucket_func)(struct inode *inode,
2142 struct ocfs2_xattr_bucket *bucket,
2145 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2146 struct buffer_head *header_bh,
2153 int i, ret = 0, cmp = 1, block_off, new_offset;
2154 struct ocfs2_xattr_header *xh =
2155 (struct ocfs2_xattr_header *)header_bh->b_data;
2156 size_t name_len = strlen(name);
2157 struct ocfs2_xattr_entry *xe = NULL;
2158 struct buffer_head *name_bh = NULL;
2162 * We don't use binary search in the bucket because there
2163 * may be multiple entries with the same name hash.
2165 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2166 xe = &xh->xh_entries[i];
2168 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2170 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2173 cmp = name_index - ocfs2_xattr_get_type(xe);
2175 cmp = name_len - xe->xe_name_len;
2179 ret = ocfs2_xattr_bucket_get_name_value(inode,
2189 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2190 header_bh->b_blocknr + block_off,
2191 &name_bh, OCFS2_BH_CACHED, inode);
2196 xe_name = name_bh->b_data + new_offset;
2198 cmp = memcmp(name, xe_name, name_len);
2214 * Find the specified xattr entry in a series of buckets.
2215 * This series start from p_blkno and last for num_clusters.
2216 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2217 * the num of the valid buckets.
2219 * Return the buffer_head this xattr should reside in. And if the xattr's
2220 * hash is in the gap of 2 buckets, return the lower bucket.
2222 static int ocfs2_xattr_bucket_find(struct inode *inode,
2229 struct ocfs2_xattr_search *xs)
2232 struct buffer_head *bh = NULL;
2233 struct buffer_head *lower_bh = NULL;
2234 struct ocfs2_xattr_header *xh = NULL;
2235 struct ocfs2_xattr_entry *xe = NULL;
2237 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2238 int low_bucket = 0, bucket, high_bucket;
2242 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
2243 &bh, OCFS2_BH_CACHED, inode);
2249 xh = (struct ocfs2_xattr_header *)bh->b_data;
2250 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2252 while (low_bucket <= high_bucket) {
2255 bucket = (low_bucket + high_bucket) / 2;
2257 blkno = p_blkno + bucket * blk_per_bucket;
2259 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
2260 &bh, OCFS2_BH_CACHED, inode);
2266 xh = (struct ocfs2_xattr_header *)bh->b_data;
2267 xe = &xh->xh_entries[0];
2268 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2269 high_bucket = bucket - 1;
2274 * Check whether the hash of the last entry in our
2275 * bucket is larger than the search one.
2277 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2278 last_hash = le32_to_cpu(xe->xe_name_hash);
2280 /* record lower_bh which may be the insert place. */
2285 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2286 low_bucket = bucket + 1;
2290 /* the searched xattr should reside in this bucket if exists. */
2291 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2292 name_index, name, name_hash,
2302 * Record the bucket we have found.
2303 * When the xattr's hash value is in the gap of 2 buckets, we will
2304 * always set it to the previous bucket.
2308 * We can't find any bucket whose first name_hash is less
2309 * than the find name_hash.
2311 BUG_ON(bh->b_blocknr != p_blkno);
2315 xs->bucket.bhs[0] = lower_bh;
2316 xs->bucket.xh = (struct ocfs2_xattr_header *)
2317 xs->bucket.bhs[0]->b_data;
2320 xs->header = xs->bucket.xh;
2321 xs->base = xs->bucket.bhs[0]->b_data;
2322 xs->end = xs->base + inode->i_sb->s_blocksize;
2326 * If we have found the xattr enty, read all the blocks in
2329 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2330 xs->bucket.bhs[0]->b_blocknr + 1,
2331 blk_per_bucket - 1, &xs->bucket.bhs[1],
2332 OCFS2_BH_CACHED, inode);
2338 xs->here = &xs->header->xh_entries[index];
2339 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2340 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2350 static int ocfs2_xattr_index_block_find(struct inode *inode,
2351 struct buffer_head *root_bh,
2354 struct ocfs2_xattr_search *xs)
2357 struct ocfs2_xattr_block *xb =
2358 (struct ocfs2_xattr_block *)root_bh->b_data;
2359 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2360 struct ocfs2_extent_list *el = &xb_root->xt_list;
2362 u32 first_hash, num_clusters = 0;
2363 u32 name_hash = ocfs2_xattr_hash_by_name(inode, name_index, name);
2365 if (le16_to_cpu(el->l_next_free_rec) == 0)
2368 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2369 name, name_hash, name_index);
2371 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2378 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2380 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2381 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2383 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2384 p_blkno, first_hash, num_clusters, xs);
2390 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2393 xattr_bucket_func *func,
2397 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2398 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2399 u32 num_buckets = clusters * bpc;
2400 struct ocfs2_xattr_bucket bucket;
2402 memset(&bucket, 0, sizeof(bucket));
2404 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2407 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2408 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2409 blkno, blk_per_bucket,
2410 bucket.bhs, OCFS2_BH_CACHED, inode);
2416 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2418 * The real bucket num in this series of blocks is stored
2419 * in the 1st bucket.
2422 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2424 mlog(0, "iterating xattr bucket %llu\n", blkno);
2426 ret = func(inode, &bucket, para);
2433 for (j = 0; j < blk_per_bucket; j++)
2434 brelse(bucket.bhs[j]);
2435 memset(&bucket, 0, sizeof(bucket));
2439 for (j = 0; j < blk_per_bucket; j++)
2440 brelse(bucket.bhs[j]);
2445 struct ocfs2_xattr_tree_list {
2450 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2451 struct ocfs2_xattr_header *xh,
2458 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2461 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2463 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2464 *new_offset = name_offset % inode->i_sb->s_blocksize;
2469 static int ocfs2_list_xattr_bucket(struct inode *inode,
2470 struct ocfs2_xattr_bucket *bucket,
2474 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2476 int i, block_off, new_offset;
2478 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2479 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2480 struct xattr_handler *handler =
2481 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
2484 ret = ocfs2_xattr_bucket_get_name_value(inode,
2491 size = handler->list(inode, xl->buffer, xl->buffer_size,
2492 bucket->bhs[block_off]->b_data +
2494 entry->xe_name_len);
2496 if (size > xl->buffer_size)
2500 xl->buffer_size -= size;
2507 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2508 struct ocfs2_xattr_tree_root *xt,
2512 struct ocfs2_extent_list *el = &xt->xt_list;
2514 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2516 struct ocfs2_xattr_tree_list xl = {
2518 .buffer_size = buffer_size,
2521 if (le16_to_cpu(el->l_next_free_rec) == 0)
2524 while (name_hash > 0) {
2525 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2526 &e_cpos, &num_clusters, el);
2532 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2533 ocfs2_list_xattr_bucket,
2543 name_hash = e_cpos - 1;
2546 ret = buffer_size - xl.buffer_size;
2551 static int cmp_xe(const void *a, const void *b)
2553 const struct ocfs2_xattr_entry *l = a, *r = b;
2554 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2555 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2557 if (l_hash > r_hash)
2559 if (l_hash < r_hash)
2564 static void swap_xe(void *a, void *b, int size)
2566 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2569 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2570 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2574 * When the ocfs2_xattr_block is filled up, new bucket will be created
2575 * and all the xattr entries will be moved to the new bucket.
2576 * Note: we need to sort the entries since they are not saved in order
2577 * in the ocfs2_xattr_block.
2579 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2580 struct buffer_head *xb_bh,
2581 struct buffer_head *xh_bh,
2582 struct buffer_head *data_bh)
2584 int i, blocksize = inode->i_sb->s_blocksize;
2585 u16 offset, size, off_change;
2586 struct ocfs2_xattr_entry *xe;
2587 struct ocfs2_xattr_block *xb =
2588 (struct ocfs2_xattr_block *)xb_bh->b_data;
2589 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2590 struct ocfs2_xattr_header *xh =
2591 (struct ocfs2_xattr_header *)xh_bh->b_data;
2592 u16 count = le16_to_cpu(xb_xh->xh_count);
2593 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2595 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2596 (unsigned long long)xb_bh->b_blocknr,
2597 (unsigned long long)xh_bh->b_blocknr);
2599 memset(xh_bh->b_data, 0, blocksize);
2601 memset(data_bh->b_data, 0, blocksize);
2603 * Since the xe_name_offset is based on ocfs2_xattr_header,
2604 * there is a offset change corresponding to the change of
2605 * ocfs2_xattr_header's position.
2607 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2608 xe = &xb_xh->xh_entries[count - 1];
2609 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2610 size = blocksize - offset;
2612 /* copy all the names and values. */
2614 target = data_bh->b_data;
2615 memcpy(target + offset, src + offset, size);
2617 /* Init new header now. */
2618 xh->xh_count = xb_xh->xh_count;
2619 xh->xh_num_buckets = cpu_to_le16(1);
2620 xh->xh_name_value_len = cpu_to_le16(size);
2621 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2623 /* copy all the entries. */
2624 target = xh_bh->b_data;
2625 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2626 size = count * sizeof(struct ocfs2_xattr_entry);
2627 memcpy(target + offset, (char *)xb_xh + offset, size);
2629 /* Change the xe offset for all the xe because of the move. */
2630 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2631 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2632 for (i = 0; i < count; i++)
2633 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2635 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2636 offset, size, off_change);
2638 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2643 * After we move xattr from block to index btree, we have to
2644 * update ocfs2_xattr_search to the new xe and base.
2646 * When the entry is in xattr block, xattr_bh indicates the storage place.
2647 * While if the entry is in index b-tree, "bucket" indicates the
2648 * real place of the xattr.
2650 static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2651 struct ocfs2_xattr_search *xs,
2652 struct buffer_head *old_bh,
2653 struct buffer_head *new_bh)
2656 char *buf = old_bh->b_data;
2657 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2658 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2659 int i, blocksize = inode->i_sb->s_blocksize;
2660 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2662 xs->bucket.bhs[0] = new_bh;
2664 xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2665 xs->header = xs->bucket.xh;
2667 xs->base = new_bh->b_data;
2668 xs->end = xs->base + inode->i_sb->s_blocksize;
2670 if (!xs->not_found) {
2671 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2672 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2673 xs->bucket.bhs[0]->b_blocknr + 1,
2674 blk_per_bucket - 1, &xs->bucket.bhs[1],
2675 OCFS2_BH_CACHED, inode);
2681 i = xs->here - old_xh->xh_entries;
2682 xs->here = &xs->header->xh_entries[i];
2689 static int ocfs2_xattr_create_index_block(struct inode *inode,
2690 struct ocfs2_xattr_search *xs)
2692 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2696 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2697 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2698 struct ocfs2_alloc_context *data_ac;
2699 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2700 struct buffer_head *xb_bh = xs->xattr_bh;
2701 struct ocfs2_xattr_block *xb =
2702 (struct ocfs2_xattr_block *)xb_bh->b_data;
2703 struct ocfs2_xattr_tree_root *xr;
2704 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2705 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2707 mlog(0, "create xattr index block for %llu\n",
2708 (unsigned long long)xb_bh->b_blocknr);
2710 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2712 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2720 * We can use this lock for now, and maybe move to a dedicated mutex
2721 * if performance becomes a problem later.
2723 down_write(&oi->ip_alloc_sem);
2726 * 3 more credits, one for xattr block update, one for the 1st block
2727 * of the new xattr bucket and one for the value/data.
2730 handle = ocfs2_start_trans(osb, credits);
2731 if (IS_ERR(handle)) {
2732 ret = PTR_ERR(handle);
2737 ret = ocfs2_journal_access(handle, inode, xb_bh,
2738 OCFS2_JOURNAL_ACCESS_WRITE);
2744 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2751 * The bucket may spread in many blocks, and
2752 * we will only touch the 1st block and the last block
2753 * in the whole bucket(one for entry and one for data).
2755 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2757 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2759 xh_bh = sb_getblk(inode->i_sb, blkno);
2766 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2768 ret = ocfs2_journal_access(handle, inode, xh_bh,
2769 OCFS2_JOURNAL_ACCESS_CREATE);
2776 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2783 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2785 ret = ocfs2_journal_access(handle, inode, data_bh,
2786 OCFS2_JOURNAL_ACCESS_CREATE);
2793 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2795 ocfs2_journal_dirty(handle, xh_bh);
2797 ocfs2_journal_dirty(handle, data_bh);
2799 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2801 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2802 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2803 offsetof(struct ocfs2_xattr_block, xb_attrs));
2805 xr = &xb->xb_attrs.xb_root;
2806 xr->xt_clusters = cpu_to_le32(1);
2807 xr->xt_last_eb_blk = 0;
2808 xr->xt_list.l_tree_depth = 0;
2809 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2810 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2812 xr->xt_list.l_recs[0].e_cpos = 0;
2813 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2814 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2816 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2818 ret = ocfs2_journal_dirty(handle, xb_bh);
2825 ocfs2_commit_trans(osb, handle);
2828 up_write(&oi->ip_alloc_sem);
2832 ocfs2_free_alloc_context(data_ac);
2840 static int cmp_xe_offset(const void *a, const void *b)
2842 const struct ocfs2_xattr_entry *l = a, *r = b;
2843 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2844 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2846 if (l_name_offset < r_name_offset)
2848 if (l_name_offset > r_name_offset)
2854 * defrag a xattr bucket if we find that the bucket has some
2855 * holes beteen name/value pairs.
2856 * We will move all the name/value pairs to the end of the bucket
2857 * so that we can spare some space for insertion.
2859 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2860 struct ocfs2_xattr_bucket *bucket)
2863 size_t end, offset, len, value_len;
2864 struct ocfs2_xattr_header *xh;
2865 char *entries, *buf, *bucket_buf = NULL;
2866 u64 blkno = bucket->bhs[0]->b_blocknr;
2867 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2869 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2870 size_t blocksize = inode->i_sb->s_blocksize;
2872 struct buffer_head **bhs;
2873 struct ocfs2_xattr_entry *xe;
2875 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2880 ret = ocfs2_read_blocks(osb, blkno, blk_per_bucket, bhs,
2881 OCFS2_BH_CACHED, inode);
2886 * In order to make the operation more efficient and generic,
2887 * we copy all the blocks into a contiguous memory and do the
2888 * defragment there, so if anything is error, we will not touch
2891 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2898 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2899 memcpy(buf, bhs[i]->b_data, blocksize);
2901 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2902 if (IS_ERR(handle)) {
2903 ret = PTR_ERR(handle);
2909 for (i = 0; i < blk_per_bucket; i++) {
2910 ret = ocfs2_journal_access(handle, inode, bhs[i],
2911 OCFS2_JOURNAL_ACCESS_WRITE);
2918 xh = (struct ocfs2_xattr_header *)bucket_buf;
2919 entries = (char *)xh->xh_entries;
2920 xh_free_start = le16_to_cpu(xh->xh_free_start);
2922 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2923 "xh_free_start = %u, xh_name_value_len = %u.\n",
2924 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2925 le16_to_cpu(xh->xh_name_value_len));
2928 * sort all the entries by their offset.
2929 * the largest will be the first, so that we can
2930 * move them to the end one by one.
2932 sort(entries, le16_to_cpu(xh->xh_count),
2933 sizeof(struct ocfs2_xattr_entry),
2934 cmp_xe_offset, swap_xe);
2936 /* Move all name/values to the end of the bucket. */
2937 xe = xh->xh_entries;
2938 end = OCFS2_XATTR_BUCKET_SIZE;
2939 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2940 offset = le16_to_cpu(xe->xe_name_offset);
2941 if (ocfs2_xattr_is_local(xe))
2942 value_len = OCFS2_XATTR_SIZE(
2943 le64_to_cpu(xe->xe_value_size));
2945 value_len = OCFS2_XATTR_ROOT_SIZE;
2946 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2949 * We must make sure that the name/value pair
2950 * exist in the same block. So adjust end to
2951 * the previous block end if needed.
2953 if (((end - len) / blocksize !=
2954 (end - 1) / blocksize))
2955 end = end - end % blocksize;
2957 if (end > offset + len) {
2958 memmove(bucket_buf + end - len,
2959 bucket_buf + offset, len);
2960 xe->xe_name_offset = cpu_to_le16(end - len);
2963 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2964 "bucket %llu\n", (unsigned long long)blkno);
2969 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2970 "bucket %llu\n", (unsigned long long)blkno);
2972 if (xh_free_start == end)
2975 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
2976 xh->xh_free_start = cpu_to_le16(end);
2978 /* sort the entries by their name_hash. */
2979 sort(entries, le16_to_cpu(xh->xh_count),
2980 sizeof(struct ocfs2_xattr_entry),
2984 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
2985 memcpy(bhs[i]->b_data, buf, blocksize);
2986 ocfs2_journal_dirty(handle, bhs[i]);
2990 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2994 for (i = 0; i < blk_per_bucket; i++)
3004 * Move half nums of the xattr bucket in the previous cluster to this new
3005 * cluster. We only touch the last cluster of the previous extend record.
3007 * first_bh is the first buffer_head of a series of bucket in the same
3008 * extent rec and header_bh is the header of one bucket in this cluster.
3009 * They will be updated if we move the data header_bh contains to the new
3010 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3012 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3014 struct buffer_head **first_bh,
3015 struct buffer_head **header_bh,
3021 int i, ret, credits;
3022 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3023 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3024 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3025 int blocksize = inode->i_sb->s_blocksize;
3026 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3027 struct ocfs2_xattr_header *new_xh;
3028 struct ocfs2_xattr_header *xh =
3029 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3031 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3032 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3034 prev_bh = *first_bh;
3036 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3038 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3040 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3041 prev_blkno, new_blkno);
3044 * We need to update the 1st half of the new cluster and
3045 * 1 more for the update of the 1st bucket of the previous
3048 credits = bpc / 2 + 1;
3049 ret = ocfs2_extend_trans(handle, credits);
3055 ret = ocfs2_journal_access(handle, inode, prev_bh,
3056 OCFS2_JOURNAL_ACCESS_WRITE);
3062 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3063 old_bh = new_bh = NULL;
3064 new_bh = sb_getblk(inode->i_sb, new_blkno);
3071 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3073 ret = ocfs2_journal_access(handle, inode, new_bh,
3074 OCFS2_JOURNAL_ACCESS_CREATE);
3081 ret = ocfs2_read_block(osb, prev_blkno,
3082 &old_bh, OCFS2_BH_CACHED, inode);
3089 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3092 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3093 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3096 *first_hash = le32_to_cpu(
3097 new_xh->xh_entries[0].xe_name_hash);
3098 new_first_bh = new_bh;
3099 get_bh(new_first_bh);
3102 ocfs2_journal_dirty(handle, new_bh);
3104 if (*header_bh == old_bh) {
3106 *header_bh = new_bh;
3110 *first_bh = new_first_bh;
3117 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3119 ocfs2_journal_dirty(handle, prev_bh);
3122 brelse(new_first_bh);
3126 static int ocfs2_read_xattr_bucket(struct inode *inode,
3128 struct buffer_head **bhs,
3132 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3135 return ocfs2_read_blocks(OCFS2_SB(inode->i_sb), blkno,
3136 blk_per_bucket, bhs,
3137 OCFS2_BH_CACHED, inode);
3139 for (i = 0; i < blk_per_bucket; i++) {
3140 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3141 if (bhs[i] == NULL) {
3146 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3153 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3154 * first_hash will record the 1st hash of the new bucket.
3156 static int ocfs2_half_xattr_bucket(struct inode *inode,
3161 int new_bucket_head)
3164 u16 count, start, len, name_value_len, xe_len, name_offset;
3165 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3166 struct buffer_head **s_bhs, **t_bhs = NULL;
3167 struct ocfs2_xattr_header *xh;
3168 struct ocfs2_xattr_entry *xe;
3169 int blocksize = inode->i_sb->s_blocksize;
3171 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3174 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3178 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3184 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3185 OCFS2_JOURNAL_ACCESS_WRITE);
3191 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3197 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3203 for (i = 0; i < blk_per_bucket; i++) {
3204 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3205 OCFS2_JOURNAL_ACCESS_CREATE);
3212 /* copy the whole bucket to the new first. */
3213 for (i = 0; i < blk_per_bucket; i++)
3214 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3216 /* update the new bucket. */
3217 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3218 count = le16_to_cpu(xh->xh_count);
3222 * Calculate the total name/value len and xh_free_start for
3223 * the old bucket first.
3225 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3227 for (i = 0; i < start; i++) {
3228 xe = &xh->xh_entries[i];
3229 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3230 if (ocfs2_xattr_is_local(xe))
3232 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3234 xe_len += OCFS2_XATTR_ROOT_SIZE;
3235 name_value_len += xe_len;
3236 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3237 name_offset = le16_to_cpu(xe->xe_name_offset);
3241 * Now begin the modification to the new bucket.
3243 * In the new bucket, We just move the xattr entry to the beginning
3244 * and don't touch the name/value. So there will be some holes in the
3245 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3248 xe = &xh->xh_entries[start];
3249 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3250 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3251 (char *)xe - (char *)xh, (char *)xh->xh_entries - (char *)xh);
3252 memmove((char *)xh->xh_entries, (char *)xe, len);
3253 xe = &xh->xh_entries[count - start];
3254 len = sizeof(struct ocfs2_xattr_entry) * start;
3255 memset((char *)xe, 0, len);
3257 le16_add_cpu(&xh->xh_count, -start);
3258 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3260 /* Calculate xh_free_start for the new bucket. */
3261 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3262 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3263 xe = &xh->xh_entries[i];
3264 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3265 if (ocfs2_xattr_is_local(xe))
3267 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3269 xe_len += OCFS2_XATTR_ROOT_SIZE;
3270 if (le16_to_cpu(xe->xe_name_offset) <
3271 le16_to_cpu(xh->xh_free_start))
3272 xh->xh_free_start = xe->xe_name_offset;
3275 /* set xh->xh_num_buckets for the new xh. */
3276 if (new_bucket_head)
3277 xh->xh_num_buckets = cpu_to_le16(1);
3279 xh->xh_num_buckets = 0;
3281 for (i = 0; i < blk_per_bucket; i++) {
3282 ocfs2_journal_dirty(handle, t_bhs[i]);
3287 /* store the first_hash of the new bucket. */
3289 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3292 * Now only update the 1st block of the old bucket.
3293 * Please note that the entry has been sorted already above.
3295 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3296 memset(&xh->xh_entries[start], 0,
3297 sizeof(struct ocfs2_xattr_entry) * (count - start));
3298 xh->xh_count = cpu_to_le16(start);
3299 xh->xh_free_start = cpu_to_le16(name_offset);
3300 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3302 ocfs2_journal_dirty(handle, s_bhs[0]);
3308 for (i = 0; i < blk_per_bucket; i++)
3314 for (i = 0; i < blk_per_bucket; i++)
3323 * Copy xattr from one bucket to another bucket.
3325 * The caller must make sure that the journal transaction
3326 * has enough space for journaling.
3328 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3335 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3336 int blocksize = inode->i_sb->s_blocksize;
3337 struct buffer_head **s_bhs, **t_bhs = NULL;
3339 BUG_ON(s_blkno == t_blkno);
3341 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3342 s_blkno, t_blkno, t_is_new);
3344 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3349 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3353 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3360 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3364 for (i = 0; i < blk_per_bucket; i++) {
3365 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3366 OCFS2_JOURNAL_ACCESS_WRITE);
3371 for (i = 0; i < blk_per_bucket; i++) {
3372 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3373 ocfs2_journal_dirty(handle, t_bhs[i]);
3378 for (i = 0; i < blk_per_bucket; i++)
3384 for (i = 0; i < blk_per_bucket; i++)
3393 * Copy one xattr cluster from src_blk to to_blk.
3394 * The to_blk will become the first bucket header of the cluster, so its
3395 * xh_num_buckets will be initialized as the bucket num in the cluster.
3397 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3399 struct buffer_head *first_bh,
3404 int i, ret, credits;
3405 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3406 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3407 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3408 struct buffer_head *bh = NULL;
3409 struct ocfs2_xattr_header *xh;
3410 u64 to_blk_start = to_blk;
3412 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3415 * We need to update the new cluster and 1 more for the update of
3416 * the 1st bucket of the previous extent rec.
3419 ret = ocfs2_extend_trans(handle, credits);
3425 ret = ocfs2_journal_access(handle, inode, first_bh,
3426 OCFS2_JOURNAL_ACCESS_WRITE);
3432 for (i = 0; i < num_buckets; i++) {
3433 ret = ocfs2_cp_xattr_bucket(inode, handle,
3434 src_blk, to_blk, 1);
3440 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3441 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3444 /* update the old bucket header. */
3445 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3446 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3448 ocfs2_journal_dirty(handle, first_bh);
3450 /* update the new bucket header. */
3451 ret = ocfs2_read_block(osb, to_blk_start, &bh, OCFS2_BH_CACHED, inode);
3457 ret = ocfs2_journal_access(handle, inode, bh,
3458 OCFS2_JOURNAL_ACCESS_WRITE);
3464 xh = (struct ocfs2_xattr_header *)bh->b_data;
3465 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3467 ocfs2_journal_dirty(handle, bh);
3470 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3477 * Move half of the xattrs in this cluster to the new cluster.
3478 * This function should only be called when bucket size == cluster size.
3479 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3481 static int ocfs2_half_xattr_cluster(struct inode *inode,
3487 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3488 int ret, credits = 2 * blk_per_bucket;
3490 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3492 ret = ocfs2_extend_trans(handle, credits);
3498 /* Move half of the xattr in start_blk to the next bucket. */
3499 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3500 new_blk, first_hash, 1);
3504 * Move some xattrs from the old cluster to the new one since they are not
3505 * contiguous in ocfs2 xattr tree.
3507 * new_blk starts a new separate cluster, and we will move some xattrs from
3508 * prev_blk to it. v_start will be set as the first name hash value in this
3509 * new cluster so that it can be used as e_cpos during tree insertion and
3510 * don't collide with our original b-tree operations. first_bh and header_bh
3511 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3512 * to extend the insert bucket.
3514 * The problem is how much xattr should we move to the new one and when should
3515 * we update first_bh and header_bh?
3516 * 1. If cluster size > bucket size, that means the previous cluster has more
3517 * than 1 bucket, so just move half nums of bucket into the new cluster and
3518 * update the first_bh and header_bh if the insert bucket has been moved
3519 * to the new cluster.
3520 * 2. If cluster_size == bucket_size:
3521 * a) If the previous extent rec has more than one cluster and the insert
3522 * place isn't in the last cluster, copy the entire last cluster to the
3523 * new one. This time, we don't need to upate the first_bh and header_bh
3524 * since they will not be moved into the new cluster.
3525 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3526 * the new one. And we set the extend flag to zero if the insert place is
3527 * moved into the new allocated cluster since no extend is needed.
3529 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3531 struct buffer_head **first_bh,
3532 struct buffer_head **header_bh,
3540 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3542 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3543 prev_blk, prev_clusters, new_blk);
3545 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3546 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3555 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3557 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3558 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3562 ret = ocfs2_half_xattr_cluster(inode, handle,
3566 if ((*header_bh)->b_blocknr == last_blk && extend)
3575 * Add a new cluster for xattr storage.
3577 * If the new cluster is contiguous with the previous one, it will be
3578 * appended to the same extent record, and num_clusters will be updated.
3579 * If not, we will insert a new extent for it and move some xattrs in
3580 * the last cluster into the new allocated one.
3581 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3582 * lose the benefits of hashing because we'll have to search large leaves.
3583 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3586 * first_bh is the first block of the previous extent rec and header_bh
3587 * indicates the bucket we will insert the new xattrs. They will be updated
3588 * when the header_bh is moved into the new cluster.
3590 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3591 struct buffer_head *root_bh,
3592 struct buffer_head **first_bh,
3593 struct buffer_head **header_bh,
3600 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3601 u32 prev_clusters = *num_clusters;
3602 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3604 handle_t *handle = NULL;
3605 struct ocfs2_alloc_context *data_ac = NULL;
3606 struct ocfs2_alloc_context *meta_ac = NULL;
3607 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3608 struct ocfs2_xattr_block *xb =
3609 (struct ocfs2_xattr_block *)root_bh->b_data;
3610 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3611 struct ocfs2_extent_list *root_el = &xb_root->xt_list;
3612 enum ocfs2_extent_tree_type type = OCFS2_XATTR_TREE_EXTENT;
3614 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3615 "previous xattr blkno = %llu\n",
3616 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3617 prev_cpos, prev_blkno);
3619 ret = ocfs2_lock_allocators(inode, root_bh, root_el,
3620 clusters_to_add, 0, &data_ac,
3621 &meta_ac, type, NULL);
3627 credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add);
3628 handle = ocfs2_start_trans(osb, credits);
3629 if (IS_ERR(handle)) {
3630 ret = PTR_ERR(handle);
3636 ret = ocfs2_journal_access(handle, inode, root_bh,
3637 OCFS2_JOURNAL_ACCESS_WRITE);
3643 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3644 clusters_to_add, &bit_off, &num_bits);
3651 BUG_ON(num_bits > clusters_to_add);
3653 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3654 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3655 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3657 if (prev_blkno + prev_clusters * bpc == block &&
3658 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3659 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3661 * If this cluster is contiguous with the old one and
3662 * adding this new cluster, we don't surpass the limit of
3663 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3664 * initialized and used like other buckets in the previous
3666 * So add it as a contiguous one. The caller will handle
3669 v_start = prev_cpos + prev_clusters;
3670 *num_clusters = prev_clusters + num_bits;
3671 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3674 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3689 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3690 num_bits, block, v_start);
3691 ret = ocfs2_xattr_tree_insert_extent(osb, handle, inode, root_bh,
3692 v_start, block, num_bits,
3699 ret = ocfs2_journal_dirty(handle, root_bh);
3707 ocfs2_commit_trans(osb, handle);
3709 ocfs2_free_alloc_context(data_ac);
3711 ocfs2_free_alloc_context(meta_ac);
3717 * Extend a new xattr bucket and move xattrs to the end one by one until
3718 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3720 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3721 struct buffer_head *first_bh,
3722 struct buffer_head *start_bh,
3726 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3727 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3728 u64 start_blk = start_bh->b_blocknr, end_blk;
3729 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3731 struct ocfs2_xattr_header *first_xh =
3732 (struct ocfs2_xattr_header *)first_bh->b_data;
3733 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3735 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3736 "from %llu, len = %u\n", start_blk,
3737 (unsigned long long)first_bh->b_blocknr, num_clusters);
3739 BUG_ON(bucket >= num_buckets);
3741 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3744 * We will touch all the buckets after the start_bh(include it).
3745 * Add one more bucket and modify the first_bh.
3747 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3748 handle = ocfs2_start_trans(osb, credits);
3749 if (IS_ERR(handle)) {
3750 ret = PTR_ERR(handle);
3756 ret = ocfs2_journal_access(handle, inode, first_bh,
3757 OCFS2_JOURNAL_ACCESS_WRITE);
3763 while (end_blk != start_blk) {
3764 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3765 end_blk + blk_per_bucket, 0);
3768 end_blk -= blk_per_bucket;
3771 /* Move half of the xattr in start_blk to the next bucket. */
3772 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3773 start_blk + blk_per_bucket, NULL, 0);
3775 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3776 ocfs2_journal_dirty(handle, first_bh);
3779 ocfs2_commit_trans(osb, handle);
3785 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3786 * xb_bh is the ocfs2_xattr_block.
3787 * We will move all the buckets starting from header_bh to the next place. As
3788 * for this one, half num of its xattrs will be moved to the next one.
3790 * We will allocate a new cluster if current cluster is full and adjust
3791 * header_bh and first_bh if the insert place is moved to the new cluster.
3793 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3794 struct buffer_head *xb_bh,
3795 struct buffer_head *header_bh)
3797 struct ocfs2_xattr_header *first_xh = NULL;
3798 struct buffer_head *first_bh = NULL;
3799 struct ocfs2_xattr_block *xb =
3800 (struct ocfs2_xattr_block *)xb_bh->b_data;
3801 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3802 struct ocfs2_extent_list *el = &xb_root->xt_list;
3803 struct ocfs2_xattr_header *xh =
3804 (struct ocfs2_xattr_header *)header_bh->b_data;
3805 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3806 struct super_block *sb = inode->i_sb;
3807 struct ocfs2_super *osb = OCFS2_SB(sb);
3808 int ret, num_buckets, extend = 1;
3810 u32 e_cpos, num_clusters;
3812 mlog(0, "Add new xattr bucket starting form %llu\n",
3813 (unsigned long long)header_bh->b_blocknr);
3816 * Add refrence for header_bh here because it may be
3817 * changed in ocfs2_add_new_xattr_cluster and we need
3818 * to free it in the end.
3822 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3829 ret = ocfs2_read_block(osb, p_blkno,
3830 &first_bh, OCFS2_BH_CACHED, inode);
3836 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3837 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3839 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3840 ret = ocfs2_add_new_xattr_cluster(inode,
3855 ret = ocfs2_extend_xattr_bucket(inode,
3867 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3868 struct ocfs2_xattr_bucket *bucket,
3871 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3873 offs = offs % inode->i_sb->s_blocksize;
3874 return bucket->bhs[block_off]->b_data + offs;
3878 * Handle the normal xattr set, including replace, delete and new.
3879 * When the bucket is empty, "is_empty" is set and the caller can
3882 * Note: "local" indicates the real data's locality. So we can't
3883 * just its bucket locality by its length.
3885 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3886 struct ocfs2_xattr_info *xi,
3887 struct ocfs2_xattr_search *xs,
3892 struct ocfs2_xattr_entry *last, *xe;
3893 int name_len = strlen(xi->name);
3894 struct ocfs2_xattr_header *xh = xs->header;
3895 u16 count = le16_to_cpu(xh->xh_count), start;
3896 size_t blocksize = inode->i_sb->s_blocksize;
3898 size_t offs, size, new_size;
3900 last = &xh->xh_entries[count];
3901 if (!xs->not_found) {
3903 offs = le16_to_cpu(xe->xe_name_offset);
3904 if (ocfs2_xattr_is_local(xe))
3905 size = OCFS2_XATTR_SIZE(name_len) +
3906 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3908 size = OCFS2_XATTR_SIZE(name_len) +
3909 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3912 * If the new value will be stored outside, xi->value has been
3913 * initalized as an empty ocfs2_xattr_value_root, and the same
3914 * goes with xi->value_len, so we can set new_size safely here.
3915 * See ocfs2_xattr_set_in_bucket.
3917 new_size = OCFS2_XATTR_SIZE(name_len) +
3918 OCFS2_XATTR_SIZE(xi->value_len);
3920 le16_add_cpu(&xh->xh_name_value_len, -size);
3922 if (new_size > size)
3923 goto set_new_name_value;
3925 /* Now replace the old value with new one. */
3927 xe->xe_value_size = cpu_to_le64(xi->value_len);
3929 xe->xe_value_size = 0;
3931 val = ocfs2_xattr_bucket_get_val(inode,
3933 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3934 size - OCFS2_XATTR_SIZE(name_len));
3935 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3936 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3937 xi->value, xi->value_len);
3939 le16_add_cpu(&xh->xh_name_value_len, new_size);
3940 ocfs2_xattr_set_local(xe, local);
3943 /* Remove the old entry. */
3946 (void *)last - (void *)xe);
3947 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
3948 le16_add_cpu(&xh->xh_count, -1);
3949 if (xh->xh_count == 0 && is_empty)
3954 /* find a new entry for insert. */
3955 int low = 0, high = count - 1, tmp;
3956 struct ocfs2_xattr_entry *tmp_xe;
3958 while (low <= high) {
3959 tmp = (low + high) / 2;
3960 tmp_xe = &xh->xh_entries[tmp];
3962 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
3964 else if (name_hash <
3965 le32_to_cpu(tmp_xe->xe_name_hash))
3971 xe = &xh->xh_entries[low];
3973 memmove(xe + 1, xe, (void *)last - (void *)xe);
3975 le16_add_cpu(&xh->xh_count, 1);
3976 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
3977 xe->xe_name_hash = cpu_to_le32(name_hash);
3978 xe->xe_name_len = name_len;
3979 ocfs2_xattr_set_type(xe, xi->name_index);
3983 /* Insert the new name+value. */
3984 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
3987 * We must make sure that the name/value pair
3988 * exists in the same block.
3990 offs = le16_to_cpu(xh->xh_free_start);
3991 start = offs - size;
3993 if (start >> inode->i_sb->s_blocksize_bits !=
3994 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
3995 offs = offs - offs % blocksize;
3996 xh->xh_free_start = cpu_to_le16(offs);
3999 val = ocfs2_xattr_bucket_get_val(inode,
4000 &xs->bucket, offs - size);
4001 xe->xe_name_offset = cpu_to_le16(offs - size);
4003 memset(val, 0, size);
4004 memcpy(val, xi->name, name_len);
4005 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4007 xe->xe_value_size = cpu_to_le64(xi->value_len);
4008 ocfs2_xattr_set_local(xe, local);
4010 le16_add_cpu(&xh->xh_free_start, -size);
4011 le16_add_cpu(&xh->xh_name_value_len, size);
4016 static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4018 struct ocfs2_xattr_search *xs,
4019 struct buffer_head **bhs,
4022 int ret = 0, off, block_off;
4023 struct ocfs2_xattr_entry *xe = xs->here;
4026 * First calculate all the blocks we should journal_access
4027 * and journal_dirty. The first block should always be touched.
4029 ret = ocfs2_journal_dirty(handle, bhs[0]);
4033 /* calc the data. */
4034 off = le16_to_cpu(xe->xe_name_offset);
4035 block_off = off >> inode->i_sb->s_blocksize_bits;
4036 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4044 * Set the xattr entry in the specified bucket.
4045 * The bucket is indicated by xs->bucket and it should have the enough
4046 * space for the xattr insertion.
4048 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4049 struct ocfs2_xattr_info *xi,
4050 struct ocfs2_xattr_search *xs,
4056 handle_t *handle = NULL;
4057 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4058 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4060 mlog(0, "Set xattr entry len = %d index = %d in bucket %llu\n",
4061 xi->value_len, xi->name_index,
4062 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4064 if (!xs->bucket.bhs[1]) {
4065 ret = ocfs2_read_blocks(osb,
4066 xs->bucket.bhs[0]->b_blocknr + 1,
4067 blk_per_bucket - 1, &xs->bucket.bhs[1],
4068 OCFS2_BH_CACHED, inode);
4075 handle = ocfs2_start_trans(osb, blk_per_bucket);
4076 if (IS_ERR(handle)) {
4077 ret = PTR_ERR(handle);
4083 for (i = 0; i < blk_per_bucket; i++) {
4084 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4085 OCFS2_JOURNAL_ACCESS_WRITE);
4092 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash,
4093 local, bucket_empty);
4095 /*Only dirty the blocks we have touched in set xattr. */
4096 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4097 xs->bucket.bhs, blk_per_bucket);
4101 ocfs2_commit_trans(osb, handle);
4106 static int ocfs2_xattr_value_update_size(struct inode *inode,
4107 struct buffer_head *xe_bh,
4108 struct ocfs2_xattr_entry *xe,
4112 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4113 handle_t *handle = NULL;
4115 handle = ocfs2_start_trans(osb, 1);
4116 if (handle == NULL) {
4122 ret = ocfs2_journal_access(handle, inode, xe_bh,
4123 OCFS2_JOURNAL_ACCESS_WRITE);
4129 xe->xe_value_size = cpu_to_le64(new_size);
4131 ret = ocfs2_journal_dirty(handle, xe_bh);
4136 ocfs2_commit_trans(osb, handle);
4142 * Truncate the specified xe_off entry in xattr bucket.
4143 * bucket is indicated by header_bh and len is the new length.
4144 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4146 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4148 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4149 struct buffer_head *header_bh,
4155 struct buffer_head *value_bh = NULL;
4156 struct ocfs2_xattr_value_root *xv;
4157 struct ocfs2_xattr_entry *xe;
4158 struct ocfs2_xattr_header *xh =
4159 (struct ocfs2_xattr_header *)header_bh->b_data;
4160 size_t blocksize = inode->i_sb->s_blocksize;
4162 xe = &xh->xh_entries[xe_off];
4164 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4166 offset = le16_to_cpu(xe->xe_name_offset) +
4167 OCFS2_XATTR_SIZE(xe->xe_name_len);
4169 value_blk = offset / blocksize;
4171 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4172 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4173 value_blk += header_bh->b_blocknr;
4175 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), value_blk,
4176 &value_bh, OCFS2_BH_CACHED, inode);
4182 xv = (struct ocfs2_xattr_value_root *)
4183 (value_bh->b_data + offset % blocksize);
4185 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4186 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4187 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4193 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4204 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4205 struct ocfs2_xattr_search *xs,
4209 struct ocfs2_xattr_entry *xe = xs->here;
4210 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4212 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4214 offset = xe - xh->xh_entries;
4215 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4223 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4224 struct ocfs2_xattr_search *xs,
4229 struct ocfs2_xattr_value_root *xv;
4230 struct ocfs2_xattr_entry *xe = xs->here;
4232 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4234 offset = le16_to_cpu(xe->xe_name_offset) +
4235 OCFS2_XATTR_SIZE(xe->xe_name_len);
4237 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4239 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4243 * Remove the xattr bucket pointed by bucket_bh.
4244 * All the buckets after it in the same xattr extent rec will be
4245 * move forward one by one.
4247 static int ocfs2_rm_xattr_bucket(struct inode *inode,
4248 struct buffer_head *first_bh,
4249 struct ocfs2_xattr_bucket *bucket)
4251 int ret = 0, credits;
4252 struct ocfs2_xattr_header *xh =
4253 (struct ocfs2_xattr_header *)first_bh->b_data;
4254 u16 bucket_num = le16_to_cpu(xh->xh_num_buckets);
4255 u64 end, start = bucket->bhs[0]->b_blocknr;
4256 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4258 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4260 end = first_bh->b_blocknr + (bucket_num - 1) * blk_per_bucket;
4262 mlog(0, "rm xattr bucket %llu\n", start);
4264 * We need to update the first xattr_header and all the buckets starting
4265 * from start in this xattr rec.
4267 * XXX: Should we empty the old last bucket here?
4269 credits = 1 + end - start;
4270 handle = ocfs2_start_trans(osb, credits);
4271 if (IS_ERR(handle)) {
4272 ret = PTR_ERR(handle);
4277 ret = ocfs2_journal_access(handle, inode, first_bh,
4278 OCFS2_JOURNAL_ACCESS_WRITE);
4285 while (start < end) {
4286 ret = ocfs2_cp_xattr_bucket(inode, handle,
4287 start + blk_per_bucket,
4293 start += blk_per_bucket;
4296 /* update the first_bh. */
4297 xh->xh_num_buckets = cpu_to_le16(bucket_num - 1);
4298 ocfs2_journal_dirty(handle, first_bh);
4301 ocfs2_commit_trans(osb, handle);
4305 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4306 struct buffer_head *root_bh,
4312 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4313 struct inode *tl_inode = osb->osb_tl_inode;
4315 struct ocfs2_xattr_block *xb =
4316 (struct ocfs2_xattr_block *)root_bh->b_data;
4317 struct ocfs2_extent_list *root_el = &xb->xb_attrs.xb_root.xt_list;
4318 struct ocfs2_alloc_context *meta_ac = NULL;
4319 struct ocfs2_cached_dealloc_ctxt dealloc;
4321 ocfs2_init_dealloc_ctxt(&dealloc);
4323 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4324 cpos, len, (unsigned long long)blkno);
4326 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4328 ret = ocfs2_lock_allocators(inode, root_bh, root_el,
4329 0, 1, NULL, &meta_ac,
4330 OCFS2_XATTR_TREE_EXTENT, NULL);
4336 mutex_lock(&tl_inode->i_mutex);
4338 if (ocfs2_truncate_log_needs_flush(osb)) {
4339 ret = __ocfs2_flush_truncate_log(osb);
4346 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4347 if (handle == NULL) {
4353 ret = ocfs2_journal_access(handle, inode, root_bh,
4354 OCFS2_JOURNAL_ACCESS_WRITE);
4360 ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac,
4361 &dealloc, OCFS2_XATTR_TREE_EXTENT, NULL);
4367 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4369 ret = ocfs2_journal_dirty(handle, root_bh);
4375 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4380 ocfs2_commit_trans(osb, handle);
4382 ocfs2_schedule_truncate_log_flush(osb, 1);
4384 mutex_unlock(&tl_inode->i_mutex);
4387 ocfs2_free_alloc_context(meta_ac);
4389 ocfs2_run_deallocs(osb, &dealloc);
4395 * Free the xattr bucket indicated by xs->bucket and if all the buckets
4396 * in the clusters is free, free the clusters also.
4398 static int ocfs2_xattr_bucket_shrink(struct inode *inode,
4399 struct ocfs2_xattr_info *xi,
4400 struct ocfs2_xattr_search *xs,
4404 u32 e_cpos, num_clusters;
4406 struct buffer_head *first_bh = NULL;
4407 struct ocfs2_xattr_header *first_xh;
4408 struct ocfs2_xattr_block *xb =
4409 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
4411 BUG_ON(xs->header->xh_count != 0);
4413 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4414 &e_cpos, &num_clusters,
4415 &xb->xb_attrs.xb_root.xt_list);
4421 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
4422 &first_bh, OCFS2_BH_CACHED, inode);
4428 ret = ocfs2_rm_xattr_bucket(inode, first_bh, &xs->bucket);
4434 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
4435 if (first_xh->xh_num_buckets == 0)
4436 ret = ocfs2_rm_xattr_cluster(inode, xs->xattr_bh,
4445 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4446 struct ocfs2_xattr_search *xs)
4448 handle_t *handle = NULL;
4449 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4450 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4451 le16_to_cpu(xh->xh_count) - 1];
4454 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4455 if (IS_ERR(handle)) {
4456 ret = PTR_ERR(handle);
4461 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4462 OCFS2_JOURNAL_ACCESS_WRITE);
4468 /* Remove the old entry. */
4469 memmove(xs->here, xs->here + 1,
4470 (void *)last - (void *)xs->here);
4471 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4472 le16_add_cpu(&xh->xh_count, -1);
4474 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4478 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4482 * Set the xattr name/value in the bucket specified in xs.
4484 * As the new value in xi may be stored in the bucket or in an outside cluster,
4485 * we divide the whole process into 3 steps:
4486 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4487 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4488 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4489 * 4. If the clusters for the new outside value can't be allocated, we need
4490 * to free the xattr we allocated in set.
4492 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4493 struct ocfs2_xattr_info *xi,
4494 struct ocfs2_xattr_search *xs)
4496 int ret, local = 1, bucket_empty = 0;
4498 char *val = (char *)xi->value;
4499 struct ocfs2_xattr_entry *xe = xs->here;
4500 u32 name_hash = ocfs2_xattr_hash_by_name(inode,
4501 xi->name_index, xi->name);
4503 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4505 * We need to truncate the xattr storage first.
4507 * If both the old and new value are stored to
4508 * outside block, we only need to truncate
4509 * the storage and then set the value outside.
4511 * If the new value should be stored within block,
4512 * we should free all the outside block first and
4513 * the modification to the xattr block will be done
4514 * by following steps.
4516 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4517 value_len = xi->value_len;
4521 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4527 goto set_value_outside;
4530 value_len = xi->value_len;
4531 /* So we have to handle the inside block change now. */
4532 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4534 * If the new value will be stored outside of block,
4535 * initalize a new empty value root and insert it first.
4538 xi->value = &def_xv;
4539 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4542 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash,
4543 local, &bucket_empty);
4549 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4550 /* allocate the space now for the outside block storage. */
4551 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4556 if (xs->not_found) {
4558 * We can't allocate enough clusters for outside
4559 * storage and we have allocated xattr already,
4560 * so need to remove it.
4562 ocfs2_xattr_bucket_remove_xs(inode, xs);
4568 ret = ocfs2_xattr_bucket_shrink(inode, xi,
4574 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4579 /* check whether the xattr bucket is filled up with the same hash value. */
4580 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4581 struct ocfs2_xattr_bucket *bucket)
4583 struct ocfs2_xattr_header *xh = bucket->xh;
4585 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4586 xh->xh_entries[0].xe_name_hash) {
4587 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4589 (unsigned long long)bucket->bhs[0]->b_blocknr,
4590 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4597 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4598 struct ocfs2_xattr_info *xi,
4599 struct ocfs2_xattr_search *xs)
4601 struct ocfs2_xattr_header *xh;
4602 struct ocfs2_xattr_entry *xe;
4603 u16 count, header_size, xh_free_start;
4604 int i, free, max_free, need, old;
4605 size_t value_size = 0, name_len = strlen(xi->name);
4606 size_t blocksize = inode->i_sb->s_blocksize;
4607 int ret, allocation = 0;
4608 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4610 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4614 count = le16_to_cpu(xh->xh_count);
4615 xh_free_start = le16_to_cpu(xh->xh_free_start);
4616 header_size = sizeof(struct ocfs2_xattr_header) +
4617 count * sizeof(struct ocfs2_xattr_entry);
4618 max_free = OCFS2_XATTR_BUCKET_SIZE -
4619 le16_to_cpu(xh->xh_name_value_len) - header_size;
4621 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4622 "of %u which exceed block size\n",
4623 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4626 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4627 value_size = OCFS2_XATTR_ROOT_SIZE;
4629 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4632 need = sizeof(struct ocfs2_xattr_entry) +
4633 OCFS2_XATTR_SIZE(name_len) + value_size;
4635 need = value_size + OCFS2_XATTR_SIZE(name_len);
4638 * We only replace the old value if the new length is smaller
4639 * than the old one. Otherwise we will allocate new space in the
4640 * bucket to store it.
4643 if (ocfs2_xattr_is_local(xe))
4644 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4646 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4648 if (old >= value_size)
4652 free = xh_free_start - header_size;
4654 * We need to make sure the new name/value pair
4655 * can exist in the same block.
4657 if (xh_free_start % blocksize < need)
4658 free -= xh_free_start % blocksize;
4660 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4661 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4662 " %u\n", xs->not_found,
4663 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4664 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4665 le16_to_cpu(xh->xh_name_value_len));
4667 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4668 if (need <= max_free &&
4669 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4671 * We can create the space by defragment. Since only the
4672 * name/value will be moved, the xe shouldn't be changed
4675 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4681 xh_free_start = le16_to_cpu(xh->xh_free_start);
4682 free = xh_free_start - header_size;
4683 if (xh_free_start % blocksize < need)
4684 free -= xh_free_start % blocksize;
4689 mlog(0, "Can't get enough space for xattr insert by "
4690 "defragment. Need %u bytes, but we have %d, so "
4691 "allocate new bucket for it.\n", need, free);
4695 * We have to add new buckets or clusters and one
4696 * allocation should leave us enough space for insert.
4701 * We do not allow for overlapping ranges between buckets. And
4702 * the maximum number of collisions we will allow for then is
4703 * one bucket's worth, so check it here whether we need to
4704 * add a new bucket for the insert.
4706 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4712 ret = ocfs2_add_new_xattr_bucket(inode,
4720 for (i = 0; i < blk_per_bucket; i++)
4721 brelse(xs->bucket.bhs[i]);
4723 memset(&xs->bucket, 0, sizeof(xs->bucket));
4725 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4728 if (ret && ret != -ENODATA)
4730 xs->not_found = ret;
4736 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);