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 int ocfs2_delete_xattr_index_block(struct inode *inode,
135 struct buffer_head *xb_bh);
137 static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
139 struct xattr_handler *handler = NULL;
141 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
142 handler = ocfs2_xattr_handler_map[name_index];
147 static inline u32 ocfs2_xattr_name_hash(struct inode *inode,
153 /* Get hash value of uuid from super block */
154 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
157 /* hash extended attribute prefix */
158 for (i = 0; i < prefix_len; i++) {
159 hash = (hash << OCFS2_HASH_SHIFT) ^
160 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
163 /* hash extended attribute name */
164 for (i = 0; i < name_len; i++) {
165 hash = (hash << OCFS2_HASH_SHIFT) ^
166 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
174 * ocfs2_xattr_hash_entry()
176 * Compute the hash of an extended attribute.
178 static void ocfs2_xattr_hash_entry(struct inode *inode,
179 struct ocfs2_xattr_header *header,
180 struct ocfs2_xattr_entry *entry)
183 struct xattr_handler *handler =
184 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
185 char *prefix = handler->prefix;
186 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
187 int prefix_len = strlen(handler->prefix);
189 hash = ocfs2_xattr_name_hash(inode, prefix, prefix_len, name,
191 entry->xe_name_hash = cpu_to_le32(hash);
196 static int ocfs2_xattr_extend_allocation(struct inode *inode,
198 struct buffer_head *xattr_bh,
199 struct ocfs2_xattr_value_root *xv)
202 int restart_func = 0;
204 handle_t *handle = NULL;
205 struct ocfs2_alloc_context *data_ac = NULL;
206 struct ocfs2_alloc_context *meta_ac = NULL;
207 enum ocfs2_alloc_restarted why;
208 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
209 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
210 struct ocfs2_extent_tree et;
212 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
214 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
218 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
225 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
227 handle = ocfs2_start_trans(osb, credits);
228 if (IS_ERR(handle)) {
229 status = PTR_ERR(handle);
235 restarted_transaction:
236 status = ocfs2_journal_access(handle, inode, xattr_bh,
237 OCFS2_JOURNAL_ACCESS_WRITE);
243 prev_clusters = le32_to_cpu(xv->xr_clusters);
244 status = ocfs2_add_clusters_in_btree(osb,
254 if ((status < 0) && (status != -EAGAIN)) {
255 if (status != -ENOSPC)
260 status = ocfs2_journal_dirty(handle, xattr_bh);
266 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
268 if (why != RESTART_NONE && clusters_to_add) {
269 if (why == RESTART_META) {
270 mlog(0, "restarting function.\n");
273 BUG_ON(why != RESTART_TRANS);
275 mlog(0, "restarting transaction.\n");
276 /* TODO: This can be more intelligent. */
277 credits = ocfs2_calc_extend_credits(osb->sb,
280 status = ocfs2_extend_trans(handle, credits);
282 /* handle still has to be committed at
288 goto restarted_transaction;
294 ocfs2_commit_trans(osb, handle);
298 ocfs2_free_alloc_context(data_ac);
302 ocfs2_free_alloc_context(meta_ac);
305 if ((!status) && restart_func) {
313 static int __ocfs2_remove_xattr_range(struct inode *inode,
314 struct buffer_head *root_bh,
315 struct ocfs2_xattr_value_root *xv,
316 u32 cpos, u32 phys_cpos, u32 len,
317 struct ocfs2_cached_dealloc_ctxt *dealloc)
320 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
321 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
322 struct inode *tl_inode = osb->osb_tl_inode;
324 struct ocfs2_alloc_context *meta_ac = NULL;
325 struct ocfs2_extent_tree et;
327 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
329 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
335 mutex_lock(&tl_inode->i_mutex);
337 if (ocfs2_truncate_log_needs_flush(osb)) {
338 ret = __ocfs2_flush_truncate_log(osb);
345 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
346 if (IS_ERR(handle)) {
347 ret = PTR_ERR(handle);
352 ret = ocfs2_journal_access(handle, inode, root_bh,
353 OCFS2_JOURNAL_ACCESS_WRITE);
359 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
366 le32_add_cpu(&xv->xr_clusters, -len);
368 ret = ocfs2_journal_dirty(handle, root_bh);
374 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
379 ocfs2_commit_trans(osb, handle);
381 mutex_unlock(&tl_inode->i_mutex);
384 ocfs2_free_alloc_context(meta_ac);
389 static int ocfs2_xattr_shrink_size(struct inode *inode,
392 struct buffer_head *root_bh,
393 struct ocfs2_xattr_value_root *xv)
396 u32 trunc_len, cpos, phys_cpos, alloc_size;
398 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
399 struct ocfs2_cached_dealloc_ctxt dealloc;
401 ocfs2_init_dealloc_ctxt(&dealloc);
403 if (old_clusters <= new_clusters)
407 trunc_len = old_clusters - new_clusters;
409 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
410 &alloc_size, &xv->xr_list);
416 if (alloc_size > trunc_len)
417 alloc_size = trunc_len;
419 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
420 phys_cpos, alloc_size,
427 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
428 ocfs2_remove_xattr_clusters_from_cache(inode, block,
431 trunc_len -= alloc_size;
435 ocfs2_schedule_truncate_log_flush(osb, 1);
436 ocfs2_run_deallocs(osb, &dealloc);
441 static int ocfs2_xattr_value_truncate(struct inode *inode,
442 struct buffer_head *root_bh,
443 struct ocfs2_xattr_value_root *xv,
447 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
448 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
450 if (new_clusters == old_clusters)
453 if (new_clusters > old_clusters)
454 ret = ocfs2_xattr_extend_allocation(inode,
455 new_clusters - old_clusters,
458 ret = ocfs2_xattr_shrink_size(inode,
459 old_clusters, new_clusters,
465 static int ocfs2_xattr_list_entries(struct inode *inode,
466 struct ocfs2_xattr_header *header,
467 char *buffer, size_t buffer_size)
469 size_t rest = buffer_size;
472 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
473 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
474 struct xattr_handler *handler =
475 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
478 size_t size = handler->list(inode, buffer, rest,
480 le16_to_cpu(entry->xe_name_offset)),
491 return buffer_size - rest;
494 static int ocfs2_xattr_ibody_list(struct inode *inode,
495 struct ocfs2_dinode *di,
499 struct ocfs2_xattr_header *header = NULL;
500 struct ocfs2_inode_info *oi = OCFS2_I(inode);
503 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
506 header = (struct ocfs2_xattr_header *)
507 ((void *)di + inode->i_sb->s_blocksize -
508 le16_to_cpu(di->i_xattr_inline_size));
510 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
515 static int ocfs2_xattr_block_list(struct inode *inode,
516 struct ocfs2_dinode *di,
520 struct buffer_head *blk_bh = NULL;
521 struct ocfs2_xattr_block *xb;
524 if (!di->i_xattr_loc)
527 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
528 le64_to_cpu(di->i_xattr_loc),
529 &blk_bh, OCFS2_BH_CACHED, inode);
534 /*Verify the signature of xattr block*/
535 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
536 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
541 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
543 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
544 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
545 ret = ocfs2_xattr_list_entries(inode, header,
546 buffer, buffer_size);
548 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
549 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
550 buffer, buffer_size);
558 ssize_t ocfs2_listxattr(struct dentry *dentry,
562 int ret = 0, i_ret = 0, b_ret = 0;
563 struct buffer_head *di_bh = NULL;
564 struct ocfs2_dinode *di = NULL;
565 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
567 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
570 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
573 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
579 di = (struct ocfs2_dinode *)di_bh->b_data;
581 down_read(&oi->ip_xattr_sem);
582 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
590 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
595 up_read(&oi->ip_xattr_sem);
596 ocfs2_inode_unlock(dentry->d_inode, 0);
600 return i_ret + b_ret;
603 static int ocfs2_xattr_find_entry(int name_index,
605 struct ocfs2_xattr_search *xs)
607 struct ocfs2_xattr_entry *entry;
614 name_len = strlen(name);
616 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
617 cmp = name_index - ocfs2_xattr_get_type(entry);
619 cmp = name_len - entry->xe_name_len;
621 cmp = memcmp(name, (xs->base +
622 le16_to_cpu(entry->xe_name_offset)),
630 return cmp ? -ENODATA : 0;
633 static int ocfs2_xattr_get_value_outside(struct inode *inode,
634 struct ocfs2_xattr_value_root *xv,
638 u32 cpos, p_cluster, num_clusters, bpc, clusters;
641 size_t cplen, blocksize;
642 struct buffer_head *bh = NULL;
643 struct ocfs2_extent_list *el;
646 clusters = le32_to_cpu(xv->xr_clusters);
647 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
648 blocksize = inode->i_sb->s_blocksize;
651 while (cpos < clusters) {
652 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
659 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
660 /* Copy ocfs2_xattr_value */
661 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
662 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
663 &bh, OCFS2_BH_CACHED, inode);
669 cplen = len >= blocksize ? blocksize : len;
670 memcpy(buffer, bh->b_data, cplen);
679 cpos += num_clusters;
685 static int ocfs2_xattr_ibody_get(struct inode *inode,
690 struct ocfs2_xattr_search *xs)
692 struct ocfs2_inode_info *oi = OCFS2_I(inode);
693 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
694 struct ocfs2_xattr_value_root *xv;
698 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
701 xs->end = (void *)di + inode->i_sb->s_blocksize;
702 xs->header = (struct ocfs2_xattr_header *)
703 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
704 xs->base = (void *)xs->header;
705 xs->here = xs->header->xh_entries;
707 ret = ocfs2_xattr_find_entry(name_index, name, xs);
710 size = le64_to_cpu(xs->here->xe_value_size);
712 if (size > buffer_size)
714 if (ocfs2_xattr_is_local(xs->here)) {
715 memcpy(buffer, (void *)xs->base +
716 le16_to_cpu(xs->here->xe_name_offset) +
717 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
719 xv = (struct ocfs2_xattr_value_root *)
720 (xs->base + le16_to_cpu(
721 xs->here->xe_name_offset) +
722 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
723 ret = ocfs2_xattr_get_value_outside(inode, xv,
735 static int ocfs2_xattr_block_get(struct inode *inode,
740 struct ocfs2_xattr_search *xs)
742 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
743 struct buffer_head *blk_bh = NULL;
744 struct ocfs2_xattr_block *xb;
745 struct ocfs2_xattr_value_root *xv;
747 int ret = -ENODATA, name_offset, name_len, block_off, i;
749 if (!di->i_xattr_loc)
752 memset(&xs->bucket, 0, sizeof(xs->bucket));
754 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
755 le64_to_cpu(di->i_xattr_loc),
756 &blk_bh, OCFS2_BH_CACHED, inode);
761 /*Verify the signature of xattr block*/
762 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
763 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
768 xs->xattr_bh = blk_bh;
769 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
771 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
772 xs->header = &xb->xb_attrs.xb_header;
773 xs->base = (void *)xs->header;
774 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
775 xs->here = xs->header->xh_entries;
777 ret = ocfs2_xattr_find_entry(name_index, name, xs);
779 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
785 size = le64_to_cpu(xs->here->xe_value_size);
788 if (size > buffer_size)
791 name_offset = le16_to_cpu(xs->here->xe_name_offset);
792 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
793 i = xs->here - xs->header->xh_entries;
795 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
796 ret = ocfs2_xattr_bucket_get_name_value(inode,
801 xs->base = xs->bucket.bhs[block_off]->b_data;
803 if (ocfs2_xattr_is_local(xs->here)) {
804 memcpy(buffer, (void *)xs->base +
805 name_offset + name_len, size);
807 xv = (struct ocfs2_xattr_value_root *)
808 (xs->base + name_offset + name_len);
809 ret = ocfs2_xattr_get_value_outside(inode, xv,
819 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
820 brelse(xs->bucket.bhs[i]);
821 memset(&xs->bucket, 0, sizeof(xs->bucket));
829 * Copy an extended attribute into the buffer provided.
830 * Buffer is NULL to compute the size of buffer required.
832 int ocfs2_xattr_get(struct inode *inode,
839 struct ocfs2_dinode *di = NULL;
840 struct buffer_head *di_bh = NULL;
841 struct ocfs2_inode_info *oi = OCFS2_I(inode);
842 struct ocfs2_xattr_search xis = {
843 .not_found = -ENODATA,
845 struct ocfs2_xattr_search xbs = {
846 .not_found = -ENODATA,
849 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
852 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
855 ret = ocfs2_inode_lock(inode, &di_bh, 0);
860 xis.inode_bh = xbs.inode_bh = di_bh;
861 di = (struct ocfs2_dinode *)di_bh->b_data;
863 down_read(&oi->ip_xattr_sem);
864 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
867 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
869 up_read(&oi->ip_xattr_sem);
870 ocfs2_inode_unlock(inode, 0);
877 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
878 struct ocfs2_xattr_value_root *xv,
882 int ret = 0, i, cp_len, credits;
883 u16 blocksize = inode->i_sb->s_blocksize;
884 u32 p_cluster, num_clusters;
885 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
886 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
888 struct buffer_head *bh = NULL;
891 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
893 credits = clusters * bpc;
894 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
895 if (IS_ERR(handle)) {
896 ret = PTR_ERR(handle);
901 while (cpos < clusters) {
902 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
903 &num_clusters, &xv->xr_list);
909 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
911 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
912 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
913 &bh, OCFS2_BH_CACHED, inode);
919 ret = ocfs2_journal_access(handle,
922 OCFS2_JOURNAL_ACCESS_WRITE);
928 cp_len = value_len > blocksize ? blocksize : value_len;
929 memcpy(bh->b_data, value, cp_len);
932 if (cp_len < blocksize)
933 memset(bh->b_data + cp_len, 0,
936 ret = ocfs2_journal_dirty(handle, bh);
945 * XXX: do we need to empty all the following
946 * blocks in this cluster?
951 cpos += num_clusters;
954 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
961 static int ocfs2_xattr_cleanup(struct inode *inode,
962 struct ocfs2_xattr_info *xi,
963 struct ocfs2_xattr_search *xs,
966 handle_t *handle = NULL;
968 size_t name_len = strlen(xi->name);
969 void *val = xs->base + offs;
970 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
972 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
973 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
974 if (IS_ERR(handle)) {
975 ret = PTR_ERR(handle);
979 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
980 OCFS2_JOURNAL_ACCESS_WRITE);
985 /* Decrease xattr count */
986 le16_add_cpu(&xs->header->xh_count, -1);
987 /* Remove the xattr entry and tree root which has already be set*/
988 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
989 memset(val, 0, size);
991 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
995 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1000 static int ocfs2_xattr_update_entry(struct inode *inode,
1001 struct ocfs2_xattr_info *xi,
1002 struct ocfs2_xattr_search *xs,
1005 handle_t *handle = NULL;
1008 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1009 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1010 if (IS_ERR(handle)) {
1011 ret = PTR_ERR(handle);
1015 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1016 OCFS2_JOURNAL_ACCESS_WRITE);
1022 xs->here->xe_name_offset = cpu_to_le16(offs);
1023 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1024 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1025 ocfs2_xattr_set_local(xs->here, 1);
1027 ocfs2_xattr_set_local(xs->here, 0);
1028 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1030 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1034 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1040 * ocfs2_xattr_set_value_outside()
1042 * Set large size value in B tree.
1044 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1045 struct ocfs2_xattr_info *xi,
1046 struct ocfs2_xattr_search *xs,
1049 size_t name_len = strlen(xi->name);
1050 void *val = xs->base + offs;
1051 struct ocfs2_xattr_value_root *xv = NULL;
1052 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1055 memset(val, 0, size);
1056 memcpy(val, xi->name, name_len);
1057 xv = (struct ocfs2_xattr_value_root *)
1058 (val + OCFS2_XATTR_SIZE(name_len));
1059 xv->xr_clusters = 0;
1060 xv->xr_last_eb_blk = 0;
1061 xv->xr_list.l_tree_depth = 0;
1062 xv->xr_list.l_count = cpu_to_le16(1);
1063 xv->xr_list.l_next_free_rec = 0;
1065 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1071 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1077 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1085 * ocfs2_xattr_set_entry_local()
1087 * Set, replace or remove extended attribute in local.
1089 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1090 struct ocfs2_xattr_info *xi,
1091 struct ocfs2_xattr_search *xs,
1092 struct ocfs2_xattr_entry *last,
1095 size_t name_len = strlen(xi->name);
1098 if (xi->value && xs->not_found) {
1099 /* Insert the new xattr entry. */
1100 le16_add_cpu(&xs->header->xh_count, 1);
1101 ocfs2_xattr_set_type(last, xi->name_index);
1102 ocfs2_xattr_set_local(last, 1);
1103 last->xe_name_len = name_len;
1109 first_val = xs->base + min_offs;
1110 offs = le16_to_cpu(xs->here->xe_name_offset);
1111 val = xs->base + offs;
1113 if (le64_to_cpu(xs->here->xe_value_size) >
1114 OCFS2_XATTR_INLINE_SIZE)
1115 size = OCFS2_XATTR_SIZE(name_len) +
1116 OCFS2_XATTR_ROOT_SIZE;
1118 size = OCFS2_XATTR_SIZE(name_len) +
1119 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1121 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1122 OCFS2_XATTR_SIZE(xi->value_len)) {
1123 /* The old and the new value have the
1124 same size. Just replace the value. */
1125 ocfs2_xattr_set_local(xs->here, 1);
1126 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1127 /* Clear value bytes. */
1128 memset(val + OCFS2_XATTR_SIZE(name_len),
1130 OCFS2_XATTR_SIZE(xi->value_len));
1131 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1136 /* Remove the old name+value. */
1137 memmove(first_val + size, first_val, val - first_val);
1138 memset(first_val, 0, size);
1139 xs->here->xe_name_hash = 0;
1140 xs->here->xe_name_offset = 0;
1141 ocfs2_xattr_set_local(xs->here, 1);
1142 xs->here->xe_value_size = 0;
1146 /* Adjust all value offsets. */
1147 last = xs->header->xh_entries;
1148 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1149 size_t o = le16_to_cpu(last->xe_name_offset);
1152 last->xe_name_offset = cpu_to_le16(o + size);
1157 /* Remove the old entry. */
1159 memmove(xs->here, xs->here + 1,
1160 (void *)last - (void *)xs->here);
1161 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1162 le16_add_cpu(&xs->header->xh_count, -1);
1166 /* Insert the new name+value. */
1167 size_t size = OCFS2_XATTR_SIZE(name_len) +
1168 OCFS2_XATTR_SIZE(xi->value_len);
1169 void *val = xs->base + min_offs - size;
1171 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1172 memset(val, 0, size);
1173 memcpy(val, xi->name, name_len);
1174 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1177 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1178 ocfs2_xattr_set_local(xs->here, 1);
1179 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1186 * ocfs2_xattr_set_entry()
1188 * Set extended attribute entry into inode or block.
1190 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1191 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1192 * then set value in B tree with set_value_outside().
1194 static int ocfs2_xattr_set_entry(struct inode *inode,
1195 struct ocfs2_xattr_info *xi,
1196 struct ocfs2_xattr_search *xs,
1199 struct ocfs2_xattr_entry *last;
1200 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1201 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1202 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1204 handle_t *handle = NULL;
1206 struct ocfs2_xattr_info xi_l = {
1207 .name_index = xi->name_index,
1210 .value_len = xi->value_len,
1213 /* Compute min_offs, last and free space. */
1214 last = xs->header->xh_entries;
1216 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1217 size_t offs = le16_to_cpu(last->xe_name_offset);
1218 if (offs < min_offs)
1223 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1227 if (!xs->not_found) {
1229 if (ocfs2_xattr_is_local(xs->here))
1230 size = OCFS2_XATTR_SIZE(name_len) +
1231 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1233 size = OCFS2_XATTR_SIZE(name_len) +
1234 OCFS2_XATTR_ROOT_SIZE;
1235 free += (size + sizeof(struct ocfs2_xattr_entry));
1237 /* Check free space in inode or block */
1238 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1239 if (free < sizeof(struct ocfs2_xattr_entry) +
1240 OCFS2_XATTR_SIZE(name_len) +
1241 OCFS2_XATTR_ROOT_SIZE) {
1245 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1246 xi_l.value = (void *)&def_xv;
1247 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1248 } else if (xi->value) {
1249 if (free < sizeof(struct ocfs2_xattr_entry) +
1250 OCFS2_XATTR_SIZE(name_len) +
1251 OCFS2_XATTR_SIZE(xi->value_len)) {
1257 if (!xs->not_found) {
1258 /* For existing extended attribute */
1259 size_t size = OCFS2_XATTR_SIZE(name_len) +
1260 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1261 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1262 void *val = xs->base + offs;
1264 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1265 /* Replace existing local xattr with tree root */
1266 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1271 } else if (!ocfs2_xattr_is_local(xs->here)) {
1272 /* For existing xattr which has value outside */
1273 struct ocfs2_xattr_value_root *xv = NULL;
1274 xv = (struct ocfs2_xattr_value_root *)(val +
1275 OCFS2_XATTR_SIZE(name_len));
1277 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1279 * If new value need set outside also,
1280 * first truncate old value to new value,
1281 * then set new value with set_value_outside().
1283 ret = ocfs2_xattr_value_truncate(inode,
1292 ret = __ocfs2_xattr_set_value_outside(inode,
1301 ret = ocfs2_xattr_update_entry(inode,
1310 * If new value need set in local,
1311 * just trucate old value to zero.
1313 ret = ocfs2_xattr_value_truncate(inode,
1323 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1324 OCFS2_INODE_UPDATE_CREDITS);
1325 if (IS_ERR(handle)) {
1326 ret = PTR_ERR(handle);
1331 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1332 OCFS2_JOURNAL_ACCESS_WRITE);
1338 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1339 /* set extended attribute in external block. */
1340 ret = ocfs2_extend_trans(handle,
1341 OCFS2_INODE_UPDATE_CREDITS +
1342 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1347 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1348 OCFS2_JOURNAL_ACCESS_WRITE);
1356 * Set value in local, include set tree root in local.
1357 * This is the first step for value size >INLINE_SIZE.
1359 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1361 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1362 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1369 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1370 (flag & OCFS2_INLINE_XATTR_FL)) {
1371 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1372 unsigned int xattrsize = osb->s_xattr_inline_size;
1375 * Adjust extent record count or inline data size
1376 * to reserve space for extended attribute.
1378 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1379 struct ocfs2_inline_data *idata = &di->id2.i_data;
1380 le16_add_cpu(&idata->id_count, -xattrsize);
1381 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1382 struct ocfs2_extent_list *el = &di->id2.i_list;
1383 le16_add_cpu(&el->l_count, -(xattrsize /
1384 sizeof(struct ocfs2_extent_rec)));
1386 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1388 /* Update xattr flag */
1389 spin_lock(&oi->ip_lock);
1390 oi->ip_dyn_features |= flag;
1391 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1392 spin_unlock(&oi->ip_lock);
1393 /* Update inode ctime */
1394 inode->i_ctime = CURRENT_TIME;
1395 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1396 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1398 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1403 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1405 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1407 * Set value outside in B tree.
1408 * This is the second step for value size > INLINE_SIZE.
1410 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1411 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1417 * If set value outside failed, we have to clean
1418 * the junk tree root we have already set in local.
1420 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1430 static int ocfs2_remove_value_outside(struct inode*inode,
1431 struct buffer_head *bh,
1432 struct ocfs2_xattr_header *header)
1436 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1437 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1439 if (!ocfs2_xattr_is_local(entry)) {
1440 struct ocfs2_xattr_value_root *xv;
1443 val = (void *)header +
1444 le16_to_cpu(entry->xe_name_offset);
1445 xv = (struct ocfs2_xattr_value_root *)
1446 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1447 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1458 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1459 struct buffer_head *di_bh)
1462 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1463 struct ocfs2_xattr_header *header;
1466 header = (struct ocfs2_xattr_header *)
1467 ((void *)di + inode->i_sb->s_blocksize -
1468 le16_to_cpu(di->i_xattr_inline_size));
1470 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1475 static int ocfs2_xattr_block_remove(struct inode *inode,
1476 struct buffer_head *blk_bh)
1478 struct ocfs2_xattr_block *xb;
1481 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1482 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1483 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1484 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1486 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1491 static int ocfs2_xattr_free_block(struct inode *inode,
1494 struct inode *xb_alloc_inode;
1495 struct buffer_head *xb_alloc_bh = NULL;
1496 struct buffer_head *blk_bh = NULL;
1497 struct ocfs2_xattr_block *xb;
1498 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1504 ret = ocfs2_read_block(osb, block, &blk_bh,
1505 OCFS2_BH_CACHED, inode);
1511 /*Verify the signature of xattr block*/
1512 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1513 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1518 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1524 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1525 blk = le64_to_cpu(xb->xb_blkno);
1526 bit = le16_to_cpu(xb->xb_suballoc_bit);
1527 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1529 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1530 EXTENT_ALLOC_SYSTEM_INODE,
1531 le16_to_cpu(xb->xb_suballoc_slot));
1532 if (!xb_alloc_inode) {
1537 mutex_lock(&xb_alloc_inode->i_mutex);
1539 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1545 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1546 if (IS_ERR(handle)) {
1547 ret = PTR_ERR(handle);
1552 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1557 ocfs2_commit_trans(osb, handle);
1559 ocfs2_inode_unlock(xb_alloc_inode, 1);
1560 brelse(xb_alloc_bh);
1562 mutex_unlock(&xb_alloc_inode->i_mutex);
1563 iput(xb_alloc_inode);
1570 * ocfs2_xattr_remove()
1572 * Free extended attribute resources associated with this inode.
1574 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1576 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1577 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1581 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1584 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1587 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1588 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1595 if (di->i_xattr_loc) {
1596 ret = ocfs2_xattr_free_block(inode,
1597 le64_to_cpu(di->i_xattr_loc));
1604 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1605 OCFS2_INODE_UPDATE_CREDITS);
1606 if (IS_ERR(handle)) {
1607 ret = PTR_ERR(handle);
1611 ret = ocfs2_journal_access(handle, inode, di_bh,
1612 OCFS2_JOURNAL_ACCESS_WRITE);
1618 di->i_xattr_loc = 0;
1620 spin_lock(&oi->ip_lock);
1621 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1622 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1623 spin_unlock(&oi->ip_lock);
1625 ret = ocfs2_journal_dirty(handle, di_bh);
1629 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1634 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1635 struct ocfs2_dinode *di)
1637 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1638 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1641 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1644 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1645 struct ocfs2_inline_data *idata = &di->id2.i_data;
1646 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1647 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1648 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1649 le64_to_cpu(di->i_size);
1651 struct ocfs2_extent_list *el = &di->id2.i_list;
1652 free = (le16_to_cpu(el->l_count) -
1653 le16_to_cpu(el->l_next_free_rec)) *
1654 sizeof(struct ocfs2_extent_rec);
1656 if (free >= xattrsize)
1663 * ocfs2_xattr_ibody_find()
1665 * Find extended attribute in inode block and
1666 * fill search info into struct ocfs2_xattr_search.
1668 static int ocfs2_xattr_ibody_find(struct inode *inode,
1671 struct ocfs2_xattr_search *xs)
1673 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1674 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1678 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1681 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1682 down_read(&oi->ip_alloc_sem);
1683 has_space = ocfs2_xattr_has_space_inline(inode, di);
1684 up_read(&oi->ip_alloc_sem);
1689 xs->xattr_bh = xs->inode_bh;
1690 xs->end = (void *)di + inode->i_sb->s_blocksize;
1691 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1692 xs->header = (struct ocfs2_xattr_header *)
1693 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1695 xs->header = (struct ocfs2_xattr_header *)
1696 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1697 xs->base = (void *)xs->header;
1698 xs->here = xs->header->xh_entries;
1700 /* Find the named attribute. */
1701 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1702 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1703 if (ret && ret != -ENODATA)
1705 xs->not_found = ret;
1712 * ocfs2_xattr_ibody_set()
1714 * Set, replace or remove an extended attribute into inode block.
1717 static int ocfs2_xattr_ibody_set(struct inode *inode,
1718 struct ocfs2_xattr_info *xi,
1719 struct ocfs2_xattr_search *xs)
1721 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1722 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1725 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1728 down_write(&oi->ip_alloc_sem);
1729 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1730 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1736 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1737 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1739 up_write(&oi->ip_alloc_sem);
1745 * ocfs2_xattr_block_find()
1747 * Find extended attribute in external block and
1748 * fill search info into struct ocfs2_xattr_search.
1750 static int ocfs2_xattr_block_find(struct inode *inode,
1753 struct ocfs2_xattr_search *xs)
1755 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1756 struct buffer_head *blk_bh = NULL;
1757 struct ocfs2_xattr_block *xb;
1760 if (!di->i_xattr_loc)
1763 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1764 le64_to_cpu(di->i_xattr_loc),
1765 &blk_bh, OCFS2_BH_CACHED, inode);
1770 /*Verify the signature of xattr block*/
1771 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1772 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1777 xs->xattr_bh = blk_bh;
1778 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1780 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1781 xs->header = &xb->xb_attrs.xb_header;
1782 xs->base = (void *)xs->header;
1783 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1784 xs->here = xs->header->xh_entries;
1786 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1788 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1792 if (ret && ret != -ENODATA) {
1793 xs->xattr_bh = NULL;
1796 xs->not_found = ret;
1805 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1806 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1809 static int ocfs2_restore_xattr_block(struct inode *inode,
1810 struct ocfs2_xattr_search *xs)
1814 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1815 struct ocfs2_xattr_block *xb =
1816 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1817 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
1818 u16 xb_flags = le16_to_cpu(xb->xb_flags);
1820 BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
1821 le16_to_cpu(el->l_next_free_rec) != 0);
1823 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1824 if (IS_ERR(handle)) {
1825 ret = PTR_ERR(handle);
1830 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1831 OCFS2_JOURNAL_ACCESS_WRITE);
1837 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
1838 offsetof(struct ocfs2_xattr_block, xb_attrs));
1840 xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
1842 ocfs2_journal_dirty(handle, xs->xattr_bh);
1845 ocfs2_commit_trans(osb, handle);
1851 * ocfs2_xattr_block_set()
1853 * Set, replace or remove an extended attribute into external block.
1856 static int ocfs2_xattr_block_set(struct inode *inode,
1857 struct ocfs2_xattr_info *xi,
1858 struct ocfs2_xattr_search *xs)
1860 struct buffer_head *new_bh = NULL;
1861 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1862 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1863 struct ocfs2_alloc_context *meta_ac = NULL;
1864 handle_t *handle = NULL;
1865 struct ocfs2_xattr_block *xblk = NULL;
1866 u16 suballoc_bit_start;
1871 if (!xs->xattr_bh) {
1873 * Alloc one external block for extended attribute
1876 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1881 handle = ocfs2_start_trans(osb,
1882 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1883 if (IS_ERR(handle)) {
1884 ret = PTR_ERR(handle);
1888 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1889 OCFS2_JOURNAL_ACCESS_CREATE);
1895 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1896 &suballoc_bit_start, &num_got,
1903 new_bh = sb_getblk(inode->i_sb, first_blkno);
1904 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1906 ret = ocfs2_journal_access(handle, inode, new_bh,
1907 OCFS2_JOURNAL_ACCESS_CREATE);
1913 /* Initialize ocfs2_xattr_block */
1914 xs->xattr_bh = new_bh;
1915 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1916 memset(xblk, 0, inode->i_sb->s_blocksize);
1917 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1918 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1919 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1920 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1921 xblk->xb_blkno = cpu_to_le64(first_blkno);
1923 xs->header = &xblk->xb_attrs.xb_header;
1924 xs->base = (void *)xs->header;
1925 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1926 xs->here = xs->header->xh_entries;
1929 ret = ocfs2_journal_dirty(handle, new_bh);
1934 di->i_xattr_loc = cpu_to_le64(first_blkno);
1935 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1939 ocfs2_commit_trans(osb, handle);
1942 ocfs2_free_alloc_context(meta_ac);
1946 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1948 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1949 /* Set extended attribute into external block */
1950 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1951 if (!ret || ret != -ENOSPC)
1954 ret = ocfs2_xattr_create_index_block(inode, xs);
1959 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1960 if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
1961 ret = ocfs2_restore_xattr_block(inode, xs);
1971 * Set, replace or remove an extended attribute for this inode.
1972 * value is NULL to remove an existing extended attribute, else either
1973 * create or replace an extended attribute.
1975 int ocfs2_xattr_set(struct inode *inode,
1982 struct buffer_head *di_bh = NULL;
1983 struct ocfs2_dinode *di;
1985 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1987 struct ocfs2_xattr_info xi = {
1988 .name_index = name_index,
1991 .value_len = value_len,
1994 struct ocfs2_xattr_search xis = {
1995 .not_found = -ENODATA,
1998 struct ocfs2_xattr_search xbs = {
1999 .not_found = -ENODATA,
2002 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2005 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2010 xis.inode_bh = xbs.inode_bh = di_bh;
2011 di = (struct ocfs2_dinode *)di_bh->b_data;
2013 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2015 * Scan inode and external block to find the same name
2016 * extended attribute and collect search infomation.
2018 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2021 if (xis.not_found) {
2022 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2027 if (xis.not_found && xbs.not_found) {
2029 if (flags & XATTR_REPLACE)
2036 if (flags & XATTR_CREATE)
2041 /* Remove existing extended attribute */
2043 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2044 else if (!xbs.not_found)
2045 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2047 /* We always try to set extended attribute into inode first*/
2048 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2049 if (!ret && !xbs.not_found) {
2051 * If succeed and that extended attribute existing in
2052 * external block, then we will remove it.
2056 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2057 } else if (ret == -ENOSPC) {
2058 if (di->i_xattr_loc && !xbs.xattr_bh) {
2059 ret = ocfs2_xattr_block_find(inode, name_index,
2065 * If no space in inode, we will set extended attribute
2066 * into external block.
2068 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2071 if (!xis.not_found) {
2073 * If succeed and that extended attribute
2074 * existing in inode, we will remove it.
2078 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2083 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2084 ocfs2_inode_unlock(inode, 1);
2086 brelse(xbs.xattr_bh);
2087 for (i = 0; i < blk_per_bucket; i++)
2088 brelse(xbs.bucket.bhs[i]);
2093 static inline u32 ocfs2_xattr_hash_by_name(struct inode *inode,
2095 const char *suffix_name)
2097 struct xattr_handler *handler = ocfs2_xattr_handler(name_index);
2098 char *prefix = handler->prefix;
2099 int prefix_len = strlen(handler->prefix);
2101 return ocfs2_xattr_name_hash(inode, prefix, prefix_len,
2102 (char *)suffix_name, strlen(suffix_name));
2106 * Find the xattr extent rec which may contains name_hash.
2107 * e_cpos will be the first name hash of the xattr rec.
2108 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2110 static int ocfs2_xattr_get_rec(struct inode *inode,
2115 struct ocfs2_extent_list *el)
2118 struct buffer_head *eb_bh = NULL;
2119 struct ocfs2_extent_block *eb;
2120 struct ocfs2_extent_rec *rec = NULL;
2123 if (el->l_tree_depth) {
2124 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2130 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2133 if (el->l_tree_depth) {
2134 ocfs2_error(inode->i_sb,
2135 "Inode %lu has non zero tree depth in "
2136 "xattr tree block %llu\n", inode->i_ino,
2137 (unsigned long long)eb_bh->b_blocknr);
2143 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2144 rec = &el->l_recs[i];
2146 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2147 e_blkno = le64_to_cpu(rec->e_blkno);
2153 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2154 "record (%u, %u, 0) in xattr", inode->i_ino,
2155 le32_to_cpu(rec->e_cpos),
2156 ocfs2_rec_clusters(el, rec));
2161 *p_blkno = le64_to_cpu(rec->e_blkno);
2162 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2164 *e_cpos = le32_to_cpu(rec->e_cpos);
2170 typedef int (xattr_bucket_func)(struct inode *inode,
2171 struct ocfs2_xattr_bucket *bucket,
2174 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2175 struct buffer_head *header_bh,
2182 int i, ret = 0, cmp = 1, block_off, new_offset;
2183 struct ocfs2_xattr_header *xh =
2184 (struct ocfs2_xattr_header *)header_bh->b_data;
2185 size_t name_len = strlen(name);
2186 struct ocfs2_xattr_entry *xe = NULL;
2187 struct buffer_head *name_bh = NULL;
2191 * We don't use binary search in the bucket because there
2192 * may be multiple entries with the same name hash.
2194 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2195 xe = &xh->xh_entries[i];
2197 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2199 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2202 cmp = name_index - ocfs2_xattr_get_type(xe);
2204 cmp = name_len - xe->xe_name_len;
2208 ret = ocfs2_xattr_bucket_get_name_value(inode,
2218 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2219 header_bh->b_blocknr + block_off,
2220 &name_bh, OCFS2_BH_CACHED, inode);
2225 xe_name = name_bh->b_data + new_offset;
2227 cmp = memcmp(name, xe_name, name_len);
2243 * Find the specified xattr entry in a series of buckets.
2244 * This series start from p_blkno and last for num_clusters.
2245 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2246 * the num of the valid buckets.
2248 * Return the buffer_head this xattr should reside in. And if the xattr's
2249 * hash is in the gap of 2 buckets, return the lower bucket.
2251 static int ocfs2_xattr_bucket_find(struct inode *inode,
2258 struct ocfs2_xattr_search *xs)
2261 struct buffer_head *bh = NULL;
2262 struct buffer_head *lower_bh = NULL;
2263 struct ocfs2_xattr_header *xh = NULL;
2264 struct ocfs2_xattr_entry *xe = NULL;
2266 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2267 int low_bucket = 0, bucket, high_bucket;
2271 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
2272 &bh, OCFS2_BH_CACHED, inode);
2278 xh = (struct ocfs2_xattr_header *)bh->b_data;
2279 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2281 while (low_bucket <= high_bucket) {
2284 bucket = (low_bucket + high_bucket) / 2;
2286 blkno = p_blkno + bucket * blk_per_bucket;
2288 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
2289 &bh, OCFS2_BH_CACHED, inode);
2295 xh = (struct ocfs2_xattr_header *)bh->b_data;
2296 xe = &xh->xh_entries[0];
2297 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2298 high_bucket = bucket - 1;
2303 * Check whether the hash of the last entry in our
2304 * bucket is larger than the search one. for an empty
2305 * bucket, the last one is also the first one.
2308 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2310 last_hash = le32_to_cpu(xe->xe_name_hash);
2312 /* record lower_bh which may be the insert place. */
2317 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2318 low_bucket = bucket + 1;
2322 /* the searched xattr should reside in this bucket if exists. */
2323 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2324 name_index, name, name_hash,
2334 * Record the bucket we have found.
2335 * When the xattr's hash value is in the gap of 2 buckets, we will
2336 * always set it to the previous bucket.
2340 * We can't find any bucket whose first name_hash is less
2341 * than the find name_hash.
2343 BUG_ON(bh->b_blocknr != p_blkno);
2347 xs->bucket.bhs[0] = lower_bh;
2348 xs->bucket.xh = (struct ocfs2_xattr_header *)
2349 xs->bucket.bhs[0]->b_data;
2352 xs->header = xs->bucket.xh;
2353 xs->base = xs->bucket.bhs[0]->b_data;
2354 xs->end = xs->base + inode->i_sb->s_blocksize;
2358 * If we have found the xattr enty, read all the blocks in
2361 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2362 xs->bucket.bhs[0]->b_blocknr + 1,
2363 blk_per_bucket - 1, &xs->bucket.bhs[1],
2364 OCFS2_BH_CACHED, inode);
2370 xs->here = &xs->header->xh_entries[index];
2371 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2372 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2382 static int ocfs2_xattr_index_block_find(struct inode *inode,
2383 struct buffer_head *root_bh,
2386 struct ocfs2_xattr_search *xs)
2389 struct ocfs2_xattr_block *xb =
2390 (struct ocfs2_xattr_block *)root_bh->b_data;
2391 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2392 struct ocfs2_extent_list *el = &xb_root->xt_list;
2394 u32 first_hash, num_clusters = 0;
2395 u32 name_hash = ocfs2_xattr_hash_by_name(inode, name_index, name);
2397 if (le16_to_cpu(el->l_next_free_rec) == 0)
2400 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2401 name, name_hash, name_index);
2403 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2410 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2412 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2413 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2415 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2416 p_blkno, first_hash, num_clusters, xs);
2422 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2425 xattr_bucket_func *func,
2429 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2430 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2431 u32 num_buckets = clusters * bpc;
2432 struct ocfs2_xattr_bucket bucket;
2434 memset(&bucket, 0, sizeof(bucket));
2436 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2439 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2440 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2441 blkno, blk_per_bucket,
2442 bucket.bhs, OCFS2_BH_CACHED, inode);
2448 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2450 * The real bucket num in this series of blocks is stored
2451 * in the 1st bucket.
2454 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2456 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
2457 le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
2459 ret = func(inode, &bucket, para);
2466 for (j = 0; j < blk_per_bucket; j++)
2467 brelse(bucket.bhs[j]);
2468 memset(&bucket, 0, sizeof(bucket));
2472 for (j = 0; j < blk_per_bucket; j++)
2473 brelse(bucket.bhs[j]);
2478 struct ocfs2_xattr_tree_list {
2483 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2484 struct ocfs2_xattr_header *xh,
2491 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2494 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2496 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2497 *new_offset = name_offset % inode->i_sb->s_blocksize;
2502 static int ocfs2_list_xattr_bucket(struct inode *inode,
2503 struct ocfs2_xattr_bucket *bucket,
2507 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2509 int i, block_off, new_offset;
2511 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2512 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2513 struct xattr_handler *handler =
2514 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
2517 ret = ocfs2_xattr_bucket_get_name_value(inode,
2524 size = handler->list(inode, xl->buffer, xl->buffer_size,
2525 bucket->bhs[block_off]->b_data +
2527 entry->xe_name_len);
2529 if (size > xl->buffer_size)
2533 xl->buffer_size -= size;
2540 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2541 struct ocfs2_xattr_tree_root *xt,
2545 struct ocfs2_extent_list *el = &xt->xt_list;
2547 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2549 struct ocfs2_xattr_tree_list xl = {
2551 .buffer_size = buffer_size,
2554 if (le16_to_cpu(el->l_next_free_rec) == 0)
2557 while (name_hash > 0) {
2558 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2559 &e_cpos, &num_clusters, el);
2565 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2566 ocfs2_list_xattr_bucket,
2576 name_hash = e_cpos - 1;
2579 ret = buffer_size - xl.buffer_size;
2584 static int cmp_xe(const void *a, const void *b)
2586 const struct ocfs2_xattr_entry *l = a, *r = b;
2587 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2588 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2590 if (l_hash > r_hash)
2592 if (l_hash < r_hash)
2597 static void swap_xe(void *a, void *b, int size)
2599 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2602 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2603 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2607 * When the ocfs2_xattr_block is filled up, new bucket will be created
2608 * and all the xattr entries will be moved to the new bucket.
2609 * Note: we need to sort the entries since they are not saved in order
2610 * in the ocfs2_xattr_block.
2612 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2613 struct buffer_head *xb_bh,
2614 struct buffer_head *xh_bh,
2615 struct buffer_head *data_bh)
2617 int i, blocksize = inode->i_sb->s_blocksize;
2618 u16 offset, size, off_change;
2619 struct ocfs2_xattr_entry *xe;
2620 struct ocfs2_xattr_block *xb =
2621 (struct ocfs2_xattr_block *)xb_bh->b_data;
2622 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2623 struct ocfs2_xattr_header *xh =
2624 (struct ocfs2_xattr_header *)xh_bh->b_data;
2625 u16 count = le16_to_cpu(xb_xh->xh_count);
2626 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2628 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2629 (unsigned long long)xb_bh->b_blocknr,
2630 (unsigned long long)xh_bh->b_blocknr);
2632 memset(xh_bh->b_data, 0, blocksize);
2634 memset(data_bh->b_data, 0, blocksize);
2636 * Since the xe_name_offset is based on ocfs2_xattr_header,
2637 * there is a offset change corresponding to the change of
2638 * ocfs2_xattr_header's position.
2640 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2641 xe = &xb_xh->xh_entries[count - 1];
2642 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2643 size = blocksize - offset;
2645 /* copy all the names and values. */
2647 target = data_bh->b_data;
2648 memcpy(target + offset, src + offset, size);
2650 /* Init new header now. */
2651 xh->xh_count = xb_xh->xh_count;
2652 xh->xh_num_buckets = cpu_to_le16(1);
2653 xh->xh_name_value_len = cpu_to_le16(size);
2654 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2656 /* copy all the entries. */
2657 target = xh_bh->b_data;
2658 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2659 size = count * sizeof(struct ocfs2_xattr_entry);
2660 memcpy(target + offset, (char *)xb_xh + offset, size);
2662 /* Change the xe offset for all the xe because of the move. */
2663 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2664 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2665 for (i = 0; i < count; i++)
2666 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2668 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2669 offset, size, off_change);
2671 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2676 * After we move xattr from block to index btree, we have to
2677 * update ocfs2_xattr_search to the new xe and base.
2679 * When the entry is in xattr block, xattr_bh indicates the storage place.
2680 * While if the entry is in index b-tree, "bucket" indicates the
2681 * real place of the xattr.
2683 static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2684 struct ocfs2_xattr_search *xs,
2685 struct buffer_head *old_bh,
2686 struct buffer_head *new_bh)
2689 char *buf = old_bh->b_data;
2690 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2691 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2692 int i, blocksize = inode->i_sb->s_blocksize;
2693 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2695 xs->bucket.bhs[0] = new_bh;
2697 xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2698 xs->header = xs->bucket.xh;
2700 xs->base = new_bh->b_data;
2701 xs->end = xs->base + inode->i_sb->s_blocksize;
2703 if (!xs->not_found) {
2704 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2705 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2706 xs->bucket.bhs[0]->b_blocknr + 1,
2707 blk_per_bucket - 1, &xs->bucket.bhs[1],
2708 OCFS2_BH_CACHED, inode);
2714 i = xs->here - old_xh->xh_entries;
2715 xs->here = &xs->header->xh_entries[i];
2722 static int ocfs2_xattr_create_index_block(struct inode *inode,
2723 struct ocfs2_xattr_search *xs)
2725 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2729 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2730 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2731 struct ocfs2_alloc_context *data_ac;
2732 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2733 struct buffer_head *xb_bh = xs->xattr_bh;
2734 struct ocfs2_xattr_block *xb =
2735 (struct ocfs2_xattr_block *)xb_bh->b_data;
2736 struct ocfs2_xattr_tree_root *xr;
2737 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2738 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2740 mlog(0, "create xattr index block for %llu\n",
2741 (unsigned long long)xb_bh->b_blocknr);
2743 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2745 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2753 * We can use this lock for now, and maybe move to a dedicated mutex
2754 * if performance becomes a problem later.
2756 down_write(&oi->ip_alloc_sem);
2759 * 3 more credits, one for xattr block update, one for the 1st block
2760 * of the new xattr bucket and one for the value/data.
2763 handle = ocfs2_start_trans(osb, credits);
2764 if (IS_ERR(handle)) {
2765 ret = PTR_ERR(handle);
2770 ret = ocfs2_journal_access(handle, inode, xb_bh,
2771 OCFS2_JOURNAL_ACCESS_WRITE);
2777 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2784 * The bucket may spread in many blocks, and
2785 * we will only touch the 1st block and the last block
2786 * in the whole bucket(one for entry and one for data).
2788 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2790 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2792 xh_bh = sb_getblk(inode->i_sb, blkno);
2799 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2801 ret = ocfs2_journal_access(handle, inode, xh_bh,
2802 OCFS2_JOURNAL_ACCESS_CREATE);
2809 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2816 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2818 ret = ocfs2_journal_access(handle, inode, data_bh,
2819 OCFS2_JOURNAL_ACCESS_CREATE);
2826 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2828 ocfs2_journal_dirty(handle, xh_bh);
2830 ocfs2_journal_dirty(handle, data_bh);
2832 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2834 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2835 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2836 offsetof(struct ocfs2_xattr_block, xb_attrs));
2838 xr = &xb->xb_attrs.xb_root;
2839 xr->xt_clusters = cpu_to_le32(1);
2840 xr->xt_last_eb_blk = 0;
2841 xr->xt_list.l_tree_depth = 0;
2842 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2843 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2845 xr->xt_list.l_recs[0].e_cpos = 0;
2846 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2847 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2849 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2851 ret = ocfs2_journal_dirty(handle, xb_bh);
2858 ocfs2_commit_trans(osb, handle);
2861 up_write(&oi->ip_alloc_sem);
2865 ocfs2_free_alloc_context(data_ac);
2873 static int cmp_xe_offset(const void *a, const void *b)
2875 const struct ocfs2_xattr_entry *l = a, *r = b;
2876 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2877 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2879 if (l_name_offset < r_name_offset)
2881 if (l_name_offset > r_name_offset)
2887 * defrag a xattr bucket if we find that the bucket has some
2888 * holes beteen name/value pairs.
2889 * We will move all the name/value pairs to the end of the bucket
2890 * so that we can spare some space for insertion.
2892 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2893 struct ocfs2_xattr_bucket *bucket)
2896 size_t end, offset, len, value_len;
2897 struct ocfs2_xattr_header *xh;
2898 char *entries, *buf, *bucket_buf = NULL;
2899 u64 blkno = bucket->bhs[0]->b_blocknr;
2900 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2902 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2903 size_t blocksize = inode->i_sb->s_blocksize;
2905 struct buffer_head **bhs;
2906 struct ocfs2_xattr_entry *xe;
2908 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2913 ret = ocfs2_read_blocks(osb, blkno, blk_per_bucket, bhs,
2914 OCFS2_BH_CACHED, inode);
2919 * In order to make the operation more efficient and generic,
2920 * we copy all the blocks into a contiguous memory and do the
2921 * defragment there, so if anything is error, we will not touch
2924 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2931 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2932 memcpy(buf, bhs[i]->b_data, blocksize);
2934 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2935 if (IS_ERR(handle)) {
2936 ret = PTR_ERR(handle);
2942 for (i = 0; i < blk_per_bucket; i++) {
2943 ret = ocfs2_journal_access(handle, inode, bhs[i],
2944 OCFS2_JOURNAL_ACCESS_WRITE);
2951 xh = (struct ocfs2_xattr_header *)bucket_buf;
2952 entries = (char *)xh->xh_entries;
2953 xh_free_start = le16_to_cpu(xh->xh_free_start);
2955 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2956 "xh_free_start = %u, xh_name_value_len = %u.\n",
2957 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2958 le16_to_cpu(xh->xh_name_value_len));
2961 * sort all the entries by their offset.
2962 * the largest will be the first, so that we can
2963 * move them to the end one by one.
2965 sort(entries, le16_to_cpu(xh->xh_count),
2966 sizeof(struct ocfs2_xattr_entry),
2967 cmp_xe_offset, swap_xe);
2969 /* Move all name/values to the end of the bucket. */
2970 xe = xh->xh_entries;
2971 end = OCFS2_XATTR_BUCKET_SIZE;
2972 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2973 offset = le16_to_cpu(xe->xe_name_offset);
2974 if (ocfs2_xattr_is_local(xe))
2975 value_len = OCFS2_XATTR_SIZE(
2976 le64_to_cpu(xe->xe_value_size));
2978 value_len = OCFS2_XATTR_ROOT_SIZE;
2979 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2982 * We must make sure that the name/value pair
2983 * exist in the same block. So adjust end to
2984 * the previous block end if needed.
2986 if (((end - len) / blocksize !=
2987 (end - 1) / blocksize))
2988 end = end - end % blocksize;
2990 if (end > offset + len) {
2991 memmove(bucket_buf + end - len,
2992 bucket_buf + offset, len);
2993 xe->xe_name_offset = cpu_to_le16(end - len);
2996 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2997 "bucket %llu\n", (unsigned long long)blkno);
3002 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3003 "bucket %llu\n", (unsigned long long)blkno);
3005 if (xh_free_start == end)
3008 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3009 xh->xh_free_start = cpu_to_le16(end);
3011 /* sort the entries by their name_hash. */
3012 sort(entries, le16_to_cpu(xh->xh_count),
3013 sizeof(struct ocfs2_xattr_entry),
3017 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
3018 memcpy(bhs[i]->b_data, buf, blocksize);
3019 ocfs2_journal_dirty(handle, bhs[i]);
3023 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3027 for (i = 0; i < blk_per_bucket; i++)
3037 * Move half nums of the xattr bucket in the previous cluster to this new
3038 * cluster. We only touch the last cluster of the previous extend record.
3040 * first_bh is the first buffer_head of a series of bucket in the same
3041 * extent rec and header_bh is the header of one bucket in this cluster.
3042 * They will be updated if we move the data header_bh contains to the new
3043 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3045 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3047 struct buffer_head **first_bh,
3048 struct buffer_head **header_bh,
3054 int i, ret, credits;
3055 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3056 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3057 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3058 int blocksize = inode->i_sb->s_blocksize;
3059 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3060 struct ocfs2_xattr_header *new_xh;
3061 struct ocfs2_xattr_header *xh =
3062 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3064 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3065 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3067 prev_bh = *first_bh;
3069 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3071 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3073 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3074 prev_blkno, new_blkno);
3077 * We need to update the 1st half of the new cluster and
3078 * 1 more for the update of the 1st bucket of the previous
3081 credits = bpc / 2 + 1;
3082 ret = ocfs2_extend_trans(handle, credits);
3088 ret = ocfs2_journal_access(handle, inode, prev_bh,
3089 OCFS2_JOURNAL_ACCESS_WRITE);
3095 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3096 old_bh = new_bh = NULL;
3097 new_bh = sb_getblk(inode->i_sb, new_blkno);
3104 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3106 ret = ocfs2_journal_access(handle, inode, new_bh,
3107 OCFS2_JOURNAL_ACCESS_CREATE);
3114 ret = ocfs2_read_block(osb, prev_blkno,
3115 &old_bh, OCFS2_BH_CACHED, inode);
3122 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3125 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3126 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3129 *first_hash = le32_to_cpu(
3130 new_xh->xh_entries[0].xe_name_hash);
3131 new_first_bh = new_bh;
3132 get_bh(new_first_bh);
3135 ocfs2_journal_dirty(handle, new_bh);
3137 if (*header_bh == old_bh) {
3139 *header_bh = new_bh;
3143 *first_bh = new_first_bh;
3150 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3152 ocfs2_journal_dirty(handle, prev_bh);
3155 brelse(new_first_bh);
3159 static int ocfs2_read_xattr_bucket(struct inode *inode,
3161 struct buffer_head **bhs,
3165 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3168 return ocfs2_read_blocks(OCFS2_SB(inode->i_sb), blkno,
3169 blk_per_bucket, bhs,
3170 OCFS2_BH_CACHED, inode);
3172 for (i = 0; i < blk_per_bucket; i++) {
3173 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3174 if (bhs[i] == NULL) {
3179 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3186 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3187 * first_hash will record the 1st hash of the new bucket.
3189 static int ocfs2_half_xattr_bucket(struct inode *inode,
3194 int new_bucket_head)
3197 u16 count, start, len, name_value_len, xe_len, name_offset;
3198 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3199 struct buffer_head **s_bhs, **t_bhs = NULL;
3200 struct ocfs2_xattr_header *xh;
3201 struct ocfs2_xattr_entry *xe;
3202 int blocksize = inode->i_sb->s_blocksize;
3204 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3207 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3211 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3217 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3218 OCFS2_JOURNAL_ACCESS_WRITE);
3224 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3230 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3236 for (i = 0; i < blk_per_bucket; i++) {
3237 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3238 OCFS2_JOURNAL_ACCESS_CREATE);
3245 /* copy the whole bucket to the new first. */
3246 for (i = 0; i < blk_per_bucket; i++)
3247 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3249 /* update the new bucket. */
3250 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3251 count = le16_to_cpu(xh->xh_count);
3255 * Calculate the total name/value len and xh_free_start for
3256 * the old bucket first.
3258 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3260 for (i = 0; i < start; i++) {
3261 xe = &xh->xh_entries[i];
3262 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3263 if (ocfs2_xattr_is_local(xe))
3265 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3267 xe_len += OCFS2_XATTR_ROOT_SIZE;
3268 name_value_len += xe_len;
3269 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3270 name_offset = le16_to_cpu(xe->xe_name_offset);
3274 * Now begin the modification to the new bucket.
3276 * In the new bucket, We just move the xattr entry to the beginning
3277 * and don't touch the name/value. So there will be some holes in the
3278 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3281 xe = &xh->xh_entries[start];
3282 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3283 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3284 (int)((char *)xe - (char *)xh),
3285 (int)((char *)xh->xh_entries - (char *)xh));
3286 memmove((char *)xh->xh_entries, (char *)xe, len);
3287 xe = &xh->xh_entries[count - start];
3288 len = sizeof(struct ocfs2_xattr_entry) * start;
3289 memset((char *)xe, 0, len);
3291 le16_add_cpu(&xh->xh_count, -start);
3292 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3294 /* Calculate xh_free_start for the new bucket. */
3295 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3296 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3297 xe = &xh->xh_entries[i];
3298 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3299 if (ocfs2_xattr_is_local(xe))
3301 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3303 xe_len += OCFS2_XATTR_ROOT_SIZE;
3304 if (le16_to_cpu(xe->xe_name_offset) <
3305 le16_to_cpu(xh->xh_free_start))
3306 xh->xh_free_start = xe->xe_name_offset;
3309 /* set xh->xh_num_buckets for the new xh. */
3310 if (new_bucket_head)
3311 xh->xh_num_buckets = cpu_to_le16(1);
3313 xh->xh_num_buckets = 0;
3315 for (i = 0; i < blk_per_bucket; i++) {
3316 ocfs2_journal_dirty(handle, t_bhs[i]);
3321 /* store the first_hash of the new bucket. */
3323 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3326 * Now only update the 1st block of the old bucket.
3327 * Please note that the entry has been sorted already above.
3329 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3330 memset(&xh->xh_entries[start], 0,
3331 sizeof(struct ocfs2_xattr_entry) * (count - start));
3332 xh->xh_count = cpu_to_le16(start);
3333 xh->xh_free_start = cpu_to_le16(name_offset);
3334 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3336 ocfs2_journal_dirty(handle, s_bhs[0]);
3342 for (i = 0; i < blk_per_bucket; i++)
3348 for (i = 0; i < blk_per_bucket; i++)
3357 * Copy xattr from one bucket to another bucket.
3359 * The caller must make sure that the journal transaction
3360 * has enough space for journaling.
3362 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3369 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3370 int blocksize = inode->i_sb->s_blocksize;
3371 struct buffer_head **s_bhs, **t_bhs = NULL;
3373 BUG_ON(s_blkno == t_blkno);
3375 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3376 s_blkno, t_blkno, t_is_new);
3378 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3383 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3387 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3394 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3398 for (i = 0; i < blk_per_bucket; i++) {
3399 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3400 OCFS2_JOURNAL_ACCESS_WRITE);
3405 for (i = 0; i < blk_per_bucket; i++) {
3406 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3407 ocfs2_journal_dirty(handle, t_bhs[i]);
3412 for (i = 0; i < blk_per_bucket; i++)
3418 for (i = 0; i < blk_per_bucket; i++)
3427 * Copy one xattr cluster from src_blk to to_blk.
3428 * The to_blk will become the first bucket header of the cluster, so its
3429 * xh_num_buckets will be initialized as the bucket num in the cluster.
3431 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3433 struct buffer_head *first_bh,
3438 int i, ret, credits;
3439 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3440 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3441 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3442 struct buffer_head *bh = NULL;
3443 struct ocfs2_xattr_header *xh;
3444 u64 to_blk_start = to_blk;
3446 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3449 * We need to update the new cluster and 1 more for the update of
3450 * the 1st bucket of the previous extent rec.
3453 ret = ocfs2_extend_trans(handle, credits);
3459 ret = ocfs2_journal_access(handle, inode, first_bh,
3460 OCFS2_JOURNAL_ACCESS_WRITE);
3466 for (i = 0; i < num_buckets; i++) {
3467 ret = ocfs2_cp_xattr_bucket(inode, handle,
3468 src_blk, to_blk, 1);
3474 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3475 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3478 /* update the old bucket header. */
3479 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3480 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3482 ocfs2_journal_dirty(handle, first_bh);
3484 /* update the new bucket header. */
3485 ret = ocfs2_read_block(osb, to_blk_start, &bh, OCFS2_BH_CACHED, inode);
3491 ret = ocfs2_journal_access(handle, inode, bh,
3492 OCFS2_JOURNAL_ACCESS_WRITE);
3498 xh = (struct ocfs2_xattr_header *)bh->b_data;
3499 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3501 ocfs2_journal_dirty(handle, bh);
3504 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3511 * Move half of the xattrs in this cluster to the new cluster.
3512 * This function should only be called when bucket size == cluster size.
3513 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3515 static int ocfs2_half_xattr_cluster(struct inode *inode,
3521 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3522 int ret, credits = 2 * blk_per_bucket;
3524 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3526 ret = ocfs2_extend_trans(handle, credits);
3532 /* Move half of the xattr in start_blk to the next bucket. */
3533 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3534 new_blk, first_hash, 1);
3538 * Move some xattrs from the old cluster to the new one since they are not
3539 * contiguous in ocfs2 xattr tree.
3541 * new_blk starts a new separate cluster, and we will move some xattrs from
3542 * prev_blk to it. v_start will be set as the first name hash value in this
3543 * new cluster so that it can be used as e_cpos during tree insertion and
3544 * don't collide with our original b-tree operations. first_bh and header_bh
3545 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3546 * to extend the insert bucket.
3548 * The problem is how much xattr should we move to the new one and when should
3549 * we update first_bh and header_bh?
3550 * 1. If cluster size > bucket size, that means the previous cluster has more
3551 * than 1 bucket, so just move half nums of bucket into the new cluster and
3552 * update the first_bh and header_bh if the insert bucket has been moved
3553 * to the new cluster.
3554 * 2. If cluster_size == bucket_size:
3555 * a) If the previous extent rec has more than one cluster and the insert
3556 * place isn't in the last cluster, copy the entire last cluster to the
3557 * new one. This time, we don't need to upate the first_bh and header_bh
3558 * since they will not be moved into the new cluster.
3559 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3560 * the new one. And we set the extend flag to zero if the insert place is
3561 * moved into the new allocated cluster since no extend is needed.
3563 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3565 struct buffer_head **first_bh,
3566 struct buffer_head **header_bh,
3574 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3576 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3577 prev_blk, prev_clusters, new_blk);
3579 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3580 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3589 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3591 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3592 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3596 ret = ocfs2_half_xattr_cluster(inode, handle,
3600 if ((*header_bh)->b_blocknr == last_blk && extend)
3609 * Add a new cluster for xattr storage.
3611 * If the new cluster is contiguous with the previous one, it will be
3612 * appended to the same extent record, and num_clusters will be updated.
3613 * If not, we will insert a new extent for it and move some xattrs in
3614 * the last cluster into the new allocated one.
3615 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3616 * lose the benefits of hashing because we'll have to search large leaves.
3617 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3620 * first_bh is the first block of the previous extent rec and header_bh
3621 * indicates the bucket we will insert the new xattrs. They will be updated
3622 * when the header_bh is moved into the new cluster.
3624 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3625 struct buffer_head *root_bh,
3626 struct buffer_head **first_bh,
3627 struct buffer_head **header_bh,
3634 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3635 u32 prev_clusters = *num_clusters;
3636 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3638 handle_t *handle = NULL;
3639 struct ocfs2_alloc_context *data_ac = NULL;
3640 struct ocfs2_alloc_context *meta_ac = NULL;
3641 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3642 struct ocfs2_extent_tree et;
3644 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3645 "previous xattr blkno = %llu\n",
3646 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3647 prev_cpos, prev_blkno);
3649 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
3651 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3652 &data_ac, &meta_ac);
3658 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3660 handle = ocfs2_start_trans(osb, credits);
3661 if (IS_ERR(handle)) {
3662 ret = PTR_ERR(handle);
3668 ret = ocfs2_journal_access(handle, inode, root_bh,
3669 OCFS2_JOURNAL_ACCESS_WRITE);
3675 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3676 clusters_to_add, &bit_off, &num_bits);
3683 BUG_ON(num_bits > clusters_to_add);
3685 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3686 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3687 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3689 if (prev_blkno + prev_clusters * bpc == block &&
3690 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3691 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3693 * If this cluster is contiguous with the old one and
3694 * adding this new cluster, we don't surpass the limit of
3695 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3696 * initialized and used like other buckets in the previous
3698 * So add it as a contiguous one. The caller will handle
3701 v_start = prev_cpos + prev_clusters;
3702 *num_clusters = prev_clusters + num_bits;
3703 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3706 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3721 if (handle->h_buffer_credits < credits) {
3723 * The journal has been restarted before, and don't
3724 * have enough space for the insertion, so extend it
3727 ret = ocfs2_extend_trans(handle, credits);
3733 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3734 num_bits, block, v_start);
3735 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3736 num_bits, 0, meta_ac);
3742 ret = ocfs2_journal_dirty(handle, root_bh);
3750 ocfs2_commit_trans(osb, handle);
3752 ocfs2_free_alloc_context(data_ac);
3754 ocfs2_free_alloc_context(meta_ac);
3760 * Extend a new xattr bucket and move xattrs to the end one by one until
3761 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3763 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3764 struct buffer_head *first_bh,
3765 struct buffer_head *start_bh,
3769 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3770 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3771 u64 start_blk = start_bh->b_blocknr, end_blk;
3772 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3774 struct ocfs2_xattr_header *first_xh =
3775 (struct ocfs2_xattr_header *)first_bh->b_data;
3776 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3778 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3779 "from %llu, len = %u\n", start_blk,
3780 (unsigned long long)first_bh->b_blocknr, num_clusters);
3782 BUG_ON(bucket >= num_buckets);
3784 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3787 * We will touch all the buckets after the start_bh(include it).
3788 * Add one more bucket and modify the first_bh.
3790 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3791 handle = ocfs2_start_trans(osb, credits);
3792 if (IS_ERR(handle)) {
3793 ret = PTR_ERR(handle);
3799 ret = ocfs2_journal_access(handle, inode, first_bh,
3800 OCFS2_JOURNAL_ACCESS_WRITE);
3806 while (end_blk != start_blk) {
3807 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3808 end_blk + blk_per_bucket, 0);
3811 end_blk -= blk_per_bucket;
3814 /* Move half of the xattr in start_blk to the next bucket. */
3815 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3816 start_blk + blk_per_bucket, NULL, 0);
3818 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3819 ocfs2_journal_dirty(handle, first_bh);
3822 ocfs2_commit_trans(osb, handle);
3828 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3829 * xb_bh is the ocfs2_xattr_block.
3830 * We will move all the buckets starting from header_bh to the next place. As
3831 * for this one, half num of its xattrs will be moved to the next one.
3833 * We will allocate a new cluster if current cluster is full and adjust
3834 * header_bh and first_bh if the insert place is moved to the new cluster.
3836 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3837 struct buffer_head *xb_bh,
3838 struct buffer_head *header_bh)
3840 struct ocfs2_xattr_header *first_xh = NULL;
3841 struct buffer_head *first_bh = NULL;
3842 struct ocfs2_xattr_block *xb =
3843 (struct ocfs2_xattr_block *)xb_bh->b_data;
3844 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3845 struct ocfs2_extent_list *el = &xb_root->xt_list;
3846 struct ocfs2_xattr_header *xh =
3847 (struct ocfs2_xattr_header *)header_bh->b_data;
3848 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3849 struct super_block *sb = inode->i_sb;
3850 struct ocfs2_super *osb = OCFS2_SB(sb);
3851 int ret, num_buckets, extend = 1;
3853 u32 e_cpos, num_clusters;
3855 mlog(0, "Add new xattr bucket starting form %llu\n",
3856 (unsigned long long)header_bh->b_blocknr);
3859 * Add refrence for header_bh here because it may be
3860 * changed in ocfs2_add_new_xattr_cluster and we need
3861 * to free it in the end.
3865 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3872 ret = ocfs2_read_block(osb, p_blkno,
3873 &first_bh, OCFS2_BH_CACHED, inode);
3879 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3880 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3882 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3883 ret = ocfs2_add_new_xattr_cluster(inode,
3898 ret = ocfs2_extend_xattr_bucket(inode,
3910 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3911 struct ocfs2_xattr_bucket *bucket,
3914 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3916 offs = offs % inode->i_sb->s_blocksize;
3917 return bucket->bhs[block_off]->b_data + offs;
3921 * Handle the normal xattr set, including replace, delete and new.
3923 * Note: "local" indicates the real data's locality. So we can't
3924 * just its bucket locality by its length.
3926 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3927 struct ocfs2_xattr_info *xi,
3928 struct ocfs2_xattr_search *xs,
3932 struct ocfs2_xattr_entry *last, *xe;
3933 int name_len = strlen(xi->name);
3934 struct ocfs2_xattr_header *xh = xs->header;
3935 u16 count = le16_to_cpu(xh->xh_count), start;
3936 size_t blocksize = inode->i_sb->s_blocksize;
3938 size_t offs, size, new_size;
3940 last = &xh->xh_entries[count];
3941 if (!xs->not_found) {
3943 offs = le16_to_cpu(xe->xe_name_offset);
3944 if (ocfs2_xattr_is_local(xe))
3945 size = OCFS2_XATTR_SIZE(name_len) +
3946 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3948 size = OCFS2_XATTR_SIZE(name_len) +
3949 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3952 * If the new value will be stored outside, xi->value has been
3953 * initalized as an empty ocfs2_xattr_value_root, and the same
3954 * goes with xi->value_len, so we can set new_size safely here.
3955 * See ocfs2_xattr_set_in_bucket.
3957 new_size = OCFS2_XATTR_SIZE(name_len) +
3958 OCFS2_XATTR_SIZE(xi->value_len);
3960 le16_add_cpu(&xh->xh_name_value_len, -size);
3962 if (new_size > size)
3963 goto set_new_name_value;
3965 /* Now replace the old value with new one. */
3967 xe->xe_value_size = cpu_to_le64(xi->value_len);
3969 xe->xe_value_size = 0;
3971 val = ocfs2_xattr_bucket_get_val(inode,
3973 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3974 size - OCFS2_XATTR_SIZE(name_len));
3975 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3976 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3977 xi->value, xi->value_len);
3979 le16_add_cpu(&xh->xh_name_value_len, new_size);
3980 ocfs2_xattr_set_local(xe, local);
3984 * Remove the old entry if there is more than one.
3985 * We don't remove the last entry so that we can
3986 * use it to indicate the hash value of the empty
3990 le16_add_cpu(&xh->xh_count, -1);
3993 (void *)last - (void *)xe);
3995 sizeof(struct ocfs2_xattr_entry));
3998 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4003 /* find a new entry for insert. */
4004 int low = 0, high = count - 1, tmp;
4005 struct ocfs2_xattr_entry *tmp_xe;
4007 while (low <= high && count) {
4008 tmp = (low + high) / 2;
4009 tmp_xe = &xh->xh_entries[tmp];
4011 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4013 else if (name_hash <
4014 le32_to_cpu(tmp_xe->xe_name_hash))
4022 xe = &xh->xh_entries[low];
4024 memmove(xe + 1, xe, (void *)last - (void *)xe);
4026 le16_add_cpu(&xh->xh_count, 1);
4027 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4028 xe->xe_name_hash = cpu_to_le32(name_hash);
4029 xe->xe_name_len = name_len;
4030 ocfs2_xattr_set_type(xe, xi->name_index);
4034 /* Insert the new name+value. */
4035 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4038 * We must make sure that the name/value pair
4039 * exists in the same block.
4041 offs = le16_to_cpu(xh->xh_free_start);
4042 start = offs - size;
4044 if (start >> inode->i_sb->s_blocksize_bits !=
4045 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4046 offs = offs - offs % blocksize;
4047 xh->xh_free_start = cpu_to_le16(offs);
4050 val = ocfs2_xattr_bucket_get_val(inode,
4051 &xs->bucket, offs - size);
4052 xe->xe_name_offset = cpu_to_le16(offs - size);
4054 memset(val, 0, size);
4055 memcpy(val, xi->name, name_len);
4056 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4058 xe->xe_value_size = cpu_to_le64(xi->value_len);
4059 ocfs2_xattr_set_local(xe, local);
4061 le16_add_cpu(&xh->xh_free_start, -size);
4062 le16_add_cpu(&xh->xh_name_value_len, size);
4067 static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4069 struct ocfs2_xattr_search *xs,
4070 struct buffer_head **bhs,
4073 int ret = 0, off, block_off;
4074 struct ocfs2_xattr_entry *xe = xs->here;
4077 * First calculate all the blocks we should journal_access
4078 * and journal_dirty. The first block should always be touched.
4080 ret = ocfs2_journal_dirty(handle, bhs[0]);
4084 /* calc the data. */
4085 off = le16_to_cpu(xe->xe_name_offset);
4086 block_off = off >> inode->i_sb->s_blocksize_bits;
4087 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4095 * Set the xattr entry in the specified bucket.
4096 * The bucket is indicated by xs->bucket and it should have the enough
4097 * space for the xattr insertion.
4099 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4100 struct ocfs2_xattr_info *xi,
4101 struct ocfs2_xattr_search *xs,
4106 handle_t *handle = NULL;
4107 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4108 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4110 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4111 (unsigned long)xi->value_len, xi->name_index,
4112 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4114 if (!xs->bucket.bhs[1]) {
4115 ret = ocfs2_read_blocks(osb,
4116 xs->bucket.bhs[0]->b_blocknr + 1,
4117 blk_per_bucket - 1, &xs->bucket.bhs[1],
4118 OCFS2_BH_CACHED, inode);
4125 handle = ocfs2_start_trans(osb, blk_per_bucket);
4126 if (IS_ERR(handle)) {
4127 ret = PTR_ERR(handle);
4133 for (i = 0; i < blk_per_bucket; i++) {
4134 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4135 OCFS2_JOURNAL_ACCESS_WRITE);
4142 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4144 /*Only dirty the blocks we have touched in set xattr. */
4145 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4146 xs->bucket.bhs, blk_per_bucket);
4150 ocfs2_commit_trans(osb, handle);
4155 static int ocfs2_xattr_value_update_size(struct inode *inode,
4156 struct buffer_head *xe_bh,
4157 struct ocfs2_xattr_entry *xe,
4161 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4162 handle_t *handle = NULL;
4164 handle = ocfs2_start_trans(osb, 1);
4165 if (handle == NULL) {
4171 ret = ocfs2_journal_access(handle, inode, xe_bh,
4172 OCFS2_JOURNAL_ACCESS_WRITE);
4178 xe->xe_value_size = cpu_to_le64(new_size);
4180 ret = ocfs2_journal_dirty(handle, xe_bh);
4185 ocfs2_commit_trans(osb, handle);
4191 * Truncate the specified xe_off entry in xattr bucket.
4192 * bucket is indicated by header_bh and len is the new length.
4193 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4195 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4197 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4198 struct buffer_head *header_bh,
4204 struct buffer_head *value_bh = NULL;
4205 struct ocfs2_xattr_value_root *xv;
4206 struct ocfs2_xattr_entry *xe;
4207 struct ocfs2_xattr_header *xh =
4208 (struct ocfs2_xattr_header *)header_bh->b_data;
4209 size_t blocksize = inode->i_sb->s_blocksize;
4211 xe = &xh->xh_entries[xe_off];
4213 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4215 offset = le16_to_cpu(xe->xe_name_offset) +
4216 OCFS2_XATTR_SIZE(xe->xe_name_len);
4218 value_blk = offset / blocksize;
4220 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4221 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4222 value_blk += header_bh->b_blocknr;
4224 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), value_blk,
4225 &value_bh, OCFS2_BH_CACHED, inode);
4231 xv = (struct ocfs2_xattr_value_root *)
4232 (value_bh->b_data + offset % blocksize);
4234 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4235 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4236 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4242 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4253 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4254 struct ocfs2_xattr_search *xs,
4258 struct ocfs2_xattr_entry *xe = xs->here;
4259 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4261 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4263 offset = xe - xh->xh_entries;
4264 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4272 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4273 struct ocfs2_xattr_search *xs,
4278 struct ocfs2_xattr_value_root *xv;
4279 struct ocfs2_xattr_entry *xe = xs->here;
4281 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4283 offset = le16_to_cpu(xe->xe_name_offset) +
4284 OCFS2_XATTR_SIZE(xe->xe_name_len);
4286 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4288 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4291 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4292 struct buffer_head *root_bh,
4298 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4299 struct inode *tl_inode = osb->osb_tl_inode;
4301 struct ocfs2_xattr_block *xb =
4302 (struct ocfs2_xattr_block *)root_bh->b_data;
4303 struct ocfs2_alloc_context *meta_ac = NULL;
4304 struct ocfs2_cached_dealloc_ctxt dealloc;
4305 struct ocfs2_extent_tree et;
4307 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4309 ocfs2_init_dealloc_ctxt(&dealloc);
4311 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4312 cpos, len, (unsigned long long)blkno);
4314 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4316 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4322 mutex_lock(&tl_inode->i_mutex);
4324 if (ocfs2_truncate_log_needs_flush(osb)) {
4325 ret = __ocfs2_flush_truncate_log(osb);
4332 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4333 if (handle == NULL) {
4339 ret = ocfs2_journal_access(handle, inode, root_bh,
4340 OCFS2_JOURNAL_ACCESS_WRITE);
4346 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4353 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4355 ret = ocfs2_journal_dirty(handle, root_bh);
4361 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4366 ocfs2_commit_trans(osb, handle);
4368 ocfs2_schedule_truncate_log_flush(osb, 1);
4370 mutex_unlock(&tl_inode->i_mutex);
4373 ocfs2_free_alloc_context(meta_ac);
4375 ocfs2_run_deallocs(osb, &dealloc);
4380 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4381 struct ocfs2_xattr_search *xs)
4383 handle_t *handle = NULL;
4384 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4385 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4386 le16_to_cpu(xh->xh_count) - 1];
4389 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4390 if (IS_ERR(handle)) {
4391 ret = PTR_ERR(handle);
4396 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4397 OCFS2_JOURNAL_ACCESS_WRITE);
4403 /* Remove the old entry. */
4404 memmove(xs->here, xs->here + 1,
4405 (void *)last - (void *)xs->here);
4406 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4407 le16_add_cpu(&xh->xh_count, -1);
4409 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4413 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4417 * Set the xattr name/value in the bucket specified in xs.
4419 * As the new value in xi may be stored in the bucket or in an outside cluster,
4420 * we divide the whole process into 3 steps:
4421 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4422 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4423 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4424 * 4. If the clusters for the new outside value can't be allocated, we need
4425 * to free the xattr we allocated in set.
4427 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4428 struct ocfs2_xattr_info *xi,
4429 struct ocfs2_xattr_search *xs)
4433 char *val = (char *)xi->value;
4434 struct ocfs2_xattr_entry *xe = xs->here;
4435 u32 name_hash = ocfs2_xattr_hash_by_name(inode,
4436 xi->name_index, xi->name);
4438 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4440 * We need to truncate the xattr storage first.
4442 * If both the old and new value are stored to
4443 * outside block, we only need to truncate
4444 * the storage and then set the value outside.
4446 * If the new value should be stored within block,
4447 * we should free all the outside block first and
4448 * the modification to the xattr block will be done
4449 * by following steps.
4451 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4452 value_len = xi->value_len;
4456 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4462 goto set_value_outside;
4465 value_len = xi->value_len;
4466 /* So we have to handle the inside block change now. */
4467 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4469 * If the new value will be stored outside of block,
4470 * initalize a new empty value root and insert it first.
4473 xi->value = &def_xv;
4474 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4477 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
4483 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4486 /* allocate the space now for the outside block storage. */
4487 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4492 if (xs->not_found) {
4494 * We can't allocate enough clusters for outside
4495 * storage and we have allocated xattr already,
4496 * so need to remove it.
4498 ocfs2_xattr_bucket_remove_xs(inode, xs);
4504 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4509 /* check whether the xattr bucket is filled up with the same hash value. */
4510 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4511 struct ocfs2_xattr_bucket *bucket)
4513 struct ocfs2_xattr_header *xh = bucket->xh;
4515 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4516 xh->xh_entries[0].xe_name_hash) {
4517 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4519 (unsigned long long)bucket->bhs[0]->b_blocknr,
4520 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4527 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4528 struct ocfs2_xattr_info *xi,
4529 struct ocfs2_xattr_search *xs)
4531 struct ocfs2_xattr_header *xh;
4532 struct ocfs2_xattr_entry *xe;
4533 u16 count, header_size, xh_free_start;
4534 int i, free, max_free, need, old;
4535 size_t value_size = 0, name_len = strlen(xi->name);
4536 size_t blocksize = inode->i_sb->s_blocksize;
4537 int ret, allocation = 0;
4538 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4540 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4544 count = le16_to_cpu(xh->xh_count);
4545 xh_free_start = le16_to_cpu(xh->xh_free_start);
4546 header_size = sizeof(struct ocfs2_xattr_header) +
4547 count * sizeof(struct ocfs2_xattr_entry);
4548 max_free = OCFS2_XATTR_BUCKET_SIZE -
4549 le16_to_cpu(xh->xh_name_value_len) - header_size;
4551 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4552 "of %u which exceed block size\n",
4553 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4556 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4557 value_size = OCFS2_XATTR_ROOT_SIZE;
4559 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4562 need = sizeof(struct ocfs2_xattr_entry) +
4563 OCFS2_XATTR_SIZE(name_len) + value_size;
4565 need = value_size + OCFS2_XATTR_SIZE(name_len);
4568 * We only replace the old value if the new length is smaller
4569 * than the old one. Otherwise we will allocate new space in the
4570 * bucket to store it.
4573 if (ocfs2_xattr_is_local(xe))
4574 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4576 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4578 if (old >= value_size)
4582 free = xh_free_start - header_size;
4584 * We need to make sure the new name/value pair
4585 * can exist in the same block.
4587 if (xh_free_start % blocksize < need)
4588 free -= xh_free_start % blocksize;
4590 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4591 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4592 " %u\n", xs->not_found,
4593 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4594 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4595 le16_to_cpu(xh->xh_name_value_len));
4597 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4598 if (need <= max_free &&
4599 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4601 * We can create the space by defragment. Since only the
4602 * name/value will be moved, the xe shouldn't be changed
4605 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4611 xh_free_start = le16_to_cpu(xh->xh_free_start);
4612 free = xh_free_start - header_size;
4613 if (xh_free_start % blocksize < need)
4614 free -= xh_free_start % blocksize;
4619 mlog(0, "Can't get enough space for xattr insert by "
4620 "defragment. Need %u bytes, but we have %d, so "
4621 "allocate new bucket for it.\n", need, free);
4625 * We have to add new buckets or clusters and one
4626 * allocation should leave us enough space for insert.
4631 * We do not allow for overlapping ranges between buckets. And
4632 * the maximum number of collisions we will allow for then is
4633 * one bucket's worth, so check it here whether we need to
4634 * add a new bucket for the insert.
4636 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4642 ret = ocfs2_add_new_xattr_bucket(inode,
4650 for (i = 0; i < blk_per_bucket; i++)
4651 brelse(xs->bucket.bhs[i]);
4653 memset(&xs->bucket, 0, sizeof(xs->bucket));
4655 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4658 if (ret && ret != -ENODATA)
4660 xs->not_found = ret;
4666 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4672 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4673 struct ocfs2_xattr_bucket *bucket,
4677 struct ocfs2_xattr_header *xh = bucket->xh;
4679 struct ocfs2_xattr_entry *xe;
4681 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4682 xe = &xh->xh_entries[i];
4683 if (ocfs2_xattr_is_local(xe))
4686 ret = ocfs2_xattr_bucket_value_truncate(inode,
4698 static int ocfs2_delete_xattr_index_block(struct inode *inode,
4699 struct buffer_head *xb_bh)
4701 struct ocfs2_xattr_block *xb =
4702 (struct ocfs2_xattr_block *)xb_bh->b_data;
4703 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4705 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4708 if (le16_to_cpu(el->l_next_free_rec) == 0)
4711 while (name_hash > 0) {
4712 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4713 &e_cpos, &num_clusters, el);
4719 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4720 ocfs2_delete_xattr_in_bucket,
4727 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4728 p_blkno, e_cpos, num_clusters);
4737 name_hash = e_cpos - 1;