1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
9 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/security.h>
40 #define MLOG_MASK_PREFIX ML_XATTR
41 #include <cluster/masklog.h>
45 #include "blockcheck.h"
55 #include "buffer_head_io.h"
58 #include "refcounttree.h"
61 struct ocfs2_xattr_def_value_root {
62 struct ocfs2_xattr_value_root xv;
63 struct ocfs2_extent_rec er;
66 struct ocfs2_xattr_bucket {
67 /* The inode these xattrs are associated with */
68 struct inode *bu_inode;
70 /* The actual buffers that make up the bucket */
71 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
73 /* How many blocks make up one bucket for this filesystem */
77 struct ocfs2_xattr_set_ctxt {
79 struct ocfs2_alloc_context *meta_ac;
80 struct ocfs2_alloc_context *data_ac;
81 struct ocfs2_cached_dealloc_ctxt dealloc;
84 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
85 #define OCFS2_XATTR_INLINE_SIZE 80
86 #define OCFS2_XATTR_HEADER_GAP 4
87 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
88 - sizeof(struct ocfs2_xattr_header) \
89 - OCFS2_XATTR_HEADER_GAP)
90 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
91 - sizeof(struct ocfs2_xattr_block) \
92 - sizeof(struct ocfs2_xattr_header) \
93 - OCFS2_XATTR_HEADER_GAP)
95 static struct ocfs2_xattr_def_value_root def_xv = {
96 .xv.xr_list.l_count = cpu_to_le16(1),
99 struct xattr_handler *ocfs2_xattr_handlers[] = {
100 &ocfs2_xattr_user_handler,
101 &ocfs2_xattr_acl_access_handler,
102 &ocfs2_xattr_acl_default_handler,
103 &ocfs2_xattr_trusted_handler,
104 &ocfs2_xattr_security_handler,
108 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
109 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
110 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
111 = &ocfs2_xattr_acl_access_handler,
112 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
113 = &ocfs2_xattr_acl_default_handler,
114 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
115 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
118 struct ocfs2_xattr_info {
122 const void *xi_value;
126 struct ocfs2_xattr_search {
127 struct buffer_head *inode_bh;
129 * xattr_bh point to the block buffer head which has extended attribute
130 * when extended attribute in inode, xattr_bh is equal to inode_bh.
132 struct buffer_head *xattr_bh;
133 struct ocfs2_xattr_header *header;
134 struct ocfs2_xattr_bucket *bucket;
137 struct ocfs2_xattr_entry *here;
141 /* Operations on struct ocfs2_xa_entry */
143 struct ocfs2_xa_loc_operations {
147 int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
149 void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
152 * Return a pointer to the appropriate buffer in loc->xl_storage
153 * at the given offset from loc->xl_header.
155 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
157 /* Can we reuse the existing entry for the new value? */
158 int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
159 struct ocfs2_xattr_info *xi);
161 /* How much space is needed for the new value? */
162 int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
163 struct ocfs2_xattr_info *xi);
166 * Return the offset of the first name+value pair. This is
167 * the start of our downward-filling free space.
169 int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
172 * Remove the name+value at this location. Do whatever is
173 * appropriate with the remaining name+value pairs.
175 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
177 /* Fill xl_entry with a new entry */
178 void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
180 /* Add name+value storage to an entry */
181 void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
184 * Initialize the value buf's access and bh fields for this entry.
185 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
187 void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
188 struct ocfs2_xattr_value_buf *vb);
192 * Describes an xattr entry location. This is a memory structure
193 * tracking the on-disk structure.
195 struct ocfs2_xa_loc {
196 /* This xattr belongs to this inode */
197 struct inode *xl_inode;
199 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
200 struct ocfs2_xattr_header *xl_header;
202 /* Bytes from xl_header to the end of the storage */
206 * The ocfs2_xattr_entry this location describes. If this is
207 * NULL, this location describes the on-disk structure where it
210 struct ocfs2_xattr_entry *xl_entry;
213 * Internal housekeeping
216 /* Buffer(s) containing this entry */
219 /* Operations on the storage backing this location */
220 const struct ocfs2_xa_loc_operations *xl_ops;
224 * Convenience functions to calculate how much space is needed for a
225 * given name+value pair
227 static int namevalue_size(int name_len, uint64_t value_len)
229 if (value_len > OCFS2_XATTR_INLINE_SIZE)
230 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
232 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
235 static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
237 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
240 static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
242 u64 value_len = le64_to_cpu(xe->xe_value_size);
244 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
245 ocfs2_xattr_is_local(xe));
246 return namevalue_size(xe->xe_name_len, value_len);
250 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
251 struct ocfs2_xattr_header *xh,
256 static int ocfs2_xattr_block_find(struct inode *inode,
259 struct ocfs2_xattr_search *xs);
260 static int ocfs2_xattr_index_block_find(struct inode *inode,
261 struct buffer_head *root_bh,
264 struct ocfs2_xattr_search *xs);
266 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
267 struct buffer_head *blk_bh,
271 static int ocfs2_xattr_create_index_block(struct inode *inode,
272 struct ocfs2_xattr_search *xs,
273 struct ocfs2_xattr_set_ctxt *ctxt);
275 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
276 struct ocfs2_xattr_info *xi,
277 struct ocfs2_xattr_search *xs,
278 struct ocfs2_xattr_set_ctxt *ctxt);
280 typedef int (xattr_tree_rec_func)(struct inode *inode,
281 struct buffer_head *root_bh,
282 u64 blkno, u32 cpos, u32 len, void *para);
283 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
284 struct buffer_head *root_bh,
285 xattr_tree_rec_func *rec_func,
287 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
288 struct ocfs2_xattr_bucket *bucket,
290 static int ocfs2_rm_xattr_cluster(struct inode *inode,
291 struct buffer_head *root_bh,
297 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
298 u64 src_blk, u64 last_blk, u64 to_blk,
299 unsigned int start_bucket,
301 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
302 struct ocfs2_dinode *di,
303 struct ocfs2_xattr_info *xi,
304 struct ocfs2_xattr_search *xis,
305 struct ocfs2_xattr_search *xbs,
306 struct ocfs2_refcount_tree **ref_tree,
309 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
310 struct ocfs2_xattr_bucket *bucket,
312 struct ocfs2_xattr_value_root **xv,
313 struct buffer_head **bh);
315 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
317 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
320 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
322 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
325 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
327 u16 len = sb->s_blocksize -
328 offsetof(struct ocfs2_xattr_header, xh_entries);
330 return len / sizeof(struct ocfs2_xattr_entry);
333 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
334 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
335 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
337 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
339 struct ocfs2_xattr_bucket *bucket;
340 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
342 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
344 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
346 bucket->bu_inode = inode;
347 bucket->bu_blocks = blks;
353 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
357 for (i = 0; i < bucket->bu_blocks; i++) {
358 brelse(bucket->bu_bhs[i]);
359 bucket->bu_bhs[i] = NULL;
363 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
366 ocfs2_xattr_bucket_relse(bucket);
367 bucket->bu_inode = NULL;
373 * A bucket that has never been written to disk doesn't need to be
374 * read. We just need the buffer_heads. Don't call this for
375 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
378 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
383 for (i = 0; i < bucket->bu_blocks; i++) {
384 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
386 if (!bucket->bu_bhs[i]) {
392 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
394 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
399 ocfs2_xattr_bucket_relse(bucket);
403 /* Read the xattr bucket at xb_blkno */
404 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
409 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
410 bucket->bu_blocks, bucket->bu_bhs, 0,
413 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
414 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
417 &bucket_xh(bucket)->xh_check);
418 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
424 ocfs2_xattr_bucket_relse(bucket);
428 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
429 struct ocfs2_xattr_bucket *bucket,
434 for (i = 0; i < bucket->bu_blocks; i++) {
435 rc = ocfs2_journal_access(handle,
436 INODE_CACHE(bucket->bu_inode),
437 bucket->bu_bhs[i], type);
447 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
448 struct ocfs2_xattr_bucket *bucket)
452 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
453 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
454 bucket->bu_bhs, bucket->bu_blocks,
455 &bucket_xh(bucket)->xh_check);
456 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
458 for (i = 0; i < bucket->bu_blocks; i++)
459 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
462 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
463 struct ocfs2_xattr_bucket *src)
466 int blocksize = src->bu_inode->i_sb->s_blocksize;
468 BUG_ON(dest->bu_blocks != src->bu_blocks);
469 BUG_ON(dest->bu_inode != src->bu_inode);
471 for (i = 0; i < src->bu_blocks; i++) {
472 memcpy(bucket_block(dest, i), bucket_block(src, i),
477 static int ocfs2_validate_xattr_block(struct super_block *sb,
478 struct buffer_head *bh)
481 struct ocfs2_xattr_block *xb =
482 (struct ocfs2_xattr_block *)bh->b_data;
484 mlog(0, "Validating xattr block %llu\n",
485 (unsigned long long)bh->b_blocknr);
487 BUG_ON(!buffer_uptodate(bh));
490 * If the ecc fails, we return the error but otherwise
491 * leave the filesystem running. We know any error is
492 * local to this block.
494 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
499 * Errors after here are fatal
502 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
504 "Extended attribute block #%llu has bad "
506 (unsigned long long)bh->b_blocknr, 7,
511 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
513 "Extended attribute block #%llu has an "
514 "invalid xb_blkno of %llu",
515 (unsigned long long)bh->b_blocknr,
516 (unsigned long long)le64_to_cpu(xb->xb_blkno));
520 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
522 "Extended attribute block #%llu has an invalid "
523 "xb_fs_generation of #%u",
524 (unsigned long long)bh->b_blocknr,
525 le32_to_cpu(xb->xb_fs_generation));
532 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
533 struct buffer_head **bh)
536 struct buffer_head *tmp = *bh;
538 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
539 ocfs2_validate_xattr_block);
541 /* If ocfs2_read_block() got us a new bh, pass it up. */
548 static inline const char *ocfs2_xattr_prefix(int name_index)
550 struct xattr_handler *handler = NULL;
552 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
553 handler = ocfs2_xattr_handler_map[name_index];
555 return handler ? handler->prefix : NULL;
558 static u32 ocfs2_xattr_name_hash(struct inode *inode,
562 /* Get hash value of uuid from super block */
563 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
566 /* hash extended attribute name */
567 for (i = 0; i < name_len; i++) {
568 hash = (hash << OCFS2_HASH_SHIFT) ^
569 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
576 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
578 return namevalue_size(name_len, value_len) +
579 sizeof(struct ocfs2_xattr_entry);
582 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
584 return namevalue_size_xi(xi) +
585 sizeof(struct ocfs2_xattr_entry);
588 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
590 return namevalue_size_xe(xe) +
591 sizeof(struct ocfs2_xattr_entry);
594 int ocfs2_calc_security_init(struct inode *dir,
595 struct ocfs2_security_xattr_info *si,
598 struct ocfs2_alloc_context **xattr_ac)
601 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
602 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
606 * The max space of security xattr taken inline is
607 * 256(name) + 80(value) + 16(entry) = 352 bytes,
608 * So reserve one metadata block for it is ok.
610 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
611 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
612 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
617 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
620 /* reserve clusters for xattr value which will be set in B tree*/
621 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
622 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
625 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
627 *want_clusters += new_clusters;
632 int ocfs2_calc_xattr_init(struct inode *dir,
633 struct buffer_head *dir_bh,
635 struct ocfs2_security_xattr_info *si,
641 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
642 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
645 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
648 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
649 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
650 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
653 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
656 } else if (acl_len != 0 && acl_len != -ENODATA) {
662 if (!(s_size + a_size))
666 * The max space of security xattr taken inline is
667 * 256(name) + 80(value) + 16(entry) = 352 bytes,
668 * The max space of acl xattr taken inline is
669 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
670 * when blocksize = 512, may reserve one more cluser for
671 * xattr bucket, otherwise reserve one metadata block
673 * If this is a new directory with inline data,
674 * we choose to reserve the entire inline area for
675 * directory contents and force an external xattr block.
677 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
678 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
679 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
680 *want_meta = *want_meta + 1;
681 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
684 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
685 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
687 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
691 * reserve credits and clusters for xattrs which has large value
692 * and have to be set outside
694 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
695 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
697 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
699 *want_clusters += new_clusters;
701 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
702 acl_len > OCFS2_XATTR_INLINE_SIZE) {
703 /* for directory, it has DEFAULT and ACCESS two types of acls */
704 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
705 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
706 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
708 *want_clusters += new_clusters;
714 static int ocfs2_xattr_extend_allocation(struct inode *inode,
716 struct ocfs2_xattr_value_buf *vb,
717 struct ocfs2_xattr_set_ctxt *ctxt)
720 handle_t *handle = ctxt->handle;
721 enum ocfs2_alloc_restarted why;
722 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
723 struct ocfs2_extent_tree et;
725 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
727 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
729 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
730 OCFS2_JOURNAL_ACCESS_WRITE);
736 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
737 status = ocfs2_add_clusters_in_btree(handle,
750 status = ocfs2_journal_dirty(handle, vb->vb_bh);
756 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
759 * We should have already allocated enough space before the transaction,
760 * so no need to restart.
762 BUG_ON(why != RESTART_NONE || clusters_to_add);
769 static int __ocfs2_remove_xattr_range(struct inode *inode,
770 struct ocfs2_xattr_value_buf *vb,
771 u32 cpos, u32 phys_cpos, u32 len,
772 unsigned int ext_flags,
773 struct ocfs2_xattr_set_ctxt *ctxt)
776 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
777 handle_t *handle = ctxt->handle;
778 struct ocfs2_extent_tree et;
780 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
782 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
783 OCFS2_JOURNAL_ACCESS_WRITE);
789 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
796 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
798 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
804 if (ext_flags & OCFS2_EXT_REFCOUNTED)
805 ret = ocfs2_decrease_refcount(inode, handle,
806 ocfs2_blocks_to_clusters(inode->i_sb,
808 len, ctxt->meta_ac, &ctxt->dealloc, 1);
810 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
819 static int ocfs2_xattr_shrink_size(struct inode *inode,
822 struct ocfs2_xattr_value_buf *vb,
823 struct ocfs2_xattr_set_ctxt *ctxt)
826 unsigned int ext_flags;
827 u32 trunc_len, cpos, phys_cpos, alloc_size;
830 if (old_clusters <= new_clusters)
834 trunc_len = old_clusters - new_clusters;
836 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
838 &vb->vb_xv->xr_list, &ext_flags);
844 if (alloc_size > trunc_len)
845 alloc_size = trunc_len;
847 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
848 phys_cpos, alloc_size,
855 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
856 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
859 trunc_len -= alloc_size;
866 static int ocfs2_xattr_value_truncate(struct inode *inode,
867 struct ocfs2_xattr_value_buf *vb,
869 struct ocfs2_xattr_set_ctxt *ctxt)
872 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
873 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
875 if (new_clusters == old_clusters)
878 if (new_clusters > old_clusters)
879 ret = ocfs2_xattr_extend_allocation(inode,
880 new_clusters - old_clusters,
883 ret = ocfs2_xattr_shrink_size(inode,
884 old_clusters, new_clusters,
890 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
891 size_t *result, const char *prefix,
892 const char *name, int name_len)
894 char *p = buffer + *result;
895 int prefix_len = strlen(prefix);
896 int total_len = prefix_len + name_len + 1;
898 *result += total_len;
900 /* we are just looking for how big our buffer needs to be */
907 memcpy(p, prefix, prefix_len);
908 memcpy(p + prefix_len, name, name_len);
909 p[prefix_len + name_len] = '\0';
914 static int ocfs2_xattr_list_entries(struct inode *inode,
915 struct ocfs2_xattr_header *header,
916 char *buffer, size_t buffer_size)
920 const char *prefix, *name;
922 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
923 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
924 type = ocfs2_xattr_get_type(entry);
925 prefix = ocfs2_xattr_prefix(type);
928 name = (const char *)header +
929 le16_to_cpu(entry->xe_name_offset);
931 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
932 &result, prefix, name,
942 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
943 struct ocfs2_dinode *di)
945 struct ocfs2_xattr_header *xh;
948 xh = (struct ocfs2_xattr_header *)
949 ((void *)di + inode->i_sb->s_blocksize -
950 le16_to_cpu(di->i_xattr_inline_size));
952 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
953 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
959 static int ocfs2_xattr_ibody_list(struct inode *inode,
960 struct ocfs2_dinode *di,
964 struct ocfs2_xattr_header *header = NULL;
965 struct ocfs2_inode_info *oi = OCFS2_I(inode);
968 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
971 header = (struct ocfs2_xattr_header *)
972 ((void *)di + inode->i_sb->s_blocksize -
973 le16_to_cpu(di->i_xattr_inline_size));
975 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
980 static int ocfs2_xattr_block_list(struct inode *inode,
981 struct ocfs2_dinode *di,
985 struct buffer_head *blk_bh = NULL;
986 struct ocfs2_xattr_block *xb;
989 if (!di->i_xattr_loc)
992 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
999 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1000 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1001 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
1002 ret = ocfs2_xattr_list_entries(inode, header,
1003 buffer, buffer_size);
1005 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
1006 buffer, buffer_size);
1013 ssize_t ocfs2_listxattr(struct dentry *dentry,
1017 int ret = 0, i_ret = 0, b_ret = 0;
1018 struct buffer_head *di_bh = NULL;
1019 struct ocfs2_dinode *di = NULL;
1020 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1022 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1025 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1028 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1034 di = (struct ocfs2_dinode *)di_bh->b_data;
1036 down_read(&oi->ip_xattr_sem);
1037 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1045 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1050 up_read(&oi->ip_xattr_sem);
1051 ocfs2_inode_unlock(dentry->d_inode, 0);
1055 return i_ret + b_ret;
1058 static int ocfs2_xattr_find_entry(int name_index,
1060 struct ocfs2_xattr_search *xs)
1062 struct ocfs2_xattr_entry *entry;
1069 name_len = strlen(name);
1071 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1072 cmp = name_index - ocfs2_xattr_get_type(entry);
1074 cmp = name_len - entry->xe_name_len;
1076 cmp = memcmp(name, (xs->base +
1077 le16_to_cpu(entry->xe_name_offset)),
1085 return cmp ? -ENODATA : 0;
1088 static int ocfs2_xattr_get_value_outside(struct inode *inode,
1089 struct ocfs2_xattr_value_root *xv,
1093 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1096 size_t cplen, blocksize;
1097 struct buffer_head *bh = NULL;
1098 struct ocfs2_extent_list *el;
1101 clusters = le32_to_cpu(xv->xr_clusters);
1102 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1103 blocksize = inode->i_sb->s_blocksize;
1106 while (cpos < clusters) {
1107 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1108 &num_clusters, el, NULL);
1114 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1115 /* Copy ocfs2_xattr_value */
1116 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1117 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1124 cplen = len >= blocksize ? blocksize : len;
1125 memcpy(buffer, bh->b_data, cplen);
1134 cpos += num_clusters;
1140 static int ocfs2_xattr_ibody_get(struct inode *inode,
1145 struct ocfs2_xattr_search *xs)
1147 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1148 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1149 struct ocfs2_xattr_value_root *xv;
1153 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1156 xs->end = (void *)di + inode->i_sb->s_blocksize;
1157 xs->header = (struct ocfs2_xattr_header *)
1158 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1159 xs->base = (void *)xs->header;
1160 xs->here = xs->header->xh_entries;
1162 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1165 size = le64_to_cpu(xs->here->xe_value_size);
1167 if (size > buffer_size)
1169 if (ocfs2_xattr_is_local(xs->here)) {
1170 memcpy(buffer, (void *)xs->base +
1171 le16_to_cpu(xs->here->xe_name_offset) +
1172 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1174 xv = (struct ocfs2_xattr_value_root *)
1175 (xs->base + le16_to_cpu(
1176 xs->here->xe_name_offset) +
1177 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1178 ret = ocfs2_xattr_get_value_outside(inode, xv,
1190 static int ocfs2_xattr_block_get(struct inode *inode,
1195 struct ocfs2_xattr_search *xs)
1197 struct ocfs2_xattr_block *xb;
1198 struct ocfs2_xattr_value_root *xv;
1200 int ret = -ENODATA, name_offset, name_len, i;
1201 int uninitialized_var(block_off);
1203 xs->bucket = ocfs2_xattr_bucket_new(inode);
1210 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1216 if (xs->not_found) {
1221 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1222 size = le64_to_cpu(xs->here->xe_value_size);
1225 if (size > buffer_size)
1228 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1229 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1230 i = xs->here - xs->header->xh_entries;
1232 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1233 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1234 bucket_xh(xs->bucket),
1238 xs->base = bucket_block(xs->bucket, block_off);
1240 if (ocfs2_xattr_is_local(xs->here)) {
1241 memcpy(buffer, (void *)xs->base +
1242 name_offset + name_len, size);
1244 xv = (struct ocfs2_xattr_value_root *)
1245 (xs->base + name_offset + name_len);
1246 ret = ocfs2_xattr_get_value_outside(inode, xv,
1256 ocfs2_xattr_bucket_free(xs->bucket);
1258 brelse(xs->xattr_bh);
1259 xs->xattr_bh = NULL;
1263 int ocfs2_xattr_get_nolock(struct inode *inode,
1264 struct buffer_head *di_bh,
1271 struct ocfs2_dinode *di = NULL;
1272 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1273 struct ocfs2_xattr_search xis = {
1274 .not_found = -ENODATA,
1276 struct ocfs2_xattr_search xbs = {
1277 .not_found = -ENODATA,
1280 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1283 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1286 xis.inode_bh = xbs.inode_bh = di_bh;
1287 di = (struct ocfs2_dinode *)di_bh->b_data;
1289 down_read(&oi->ip_xattr_sem);
1290 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1292 if (ret == -ENODATA && di->i_xattr_loc)
1293 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1295 up_read(&oi->ip_xattr_sem);
1300 /* ocfs2_xattr_get()
1302 * Copy an extended attribute into the buffer provided.
1303 * Buffer is NULL to compute the size of buffer required.
1305 static int ocfs2_xattr_get(struct inode *inode,
1312 struct buffer_head *di_bh = NULL;
1314 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1319 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1320 name, buffer, buffer_size);
1322 ocfs2_inode_unlock(inode, 0);
1329 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1331 struct ocfs2_xattr_value_buf *vb,
1335 int ret = 0, i, cp_len;
1336 u16 blocksize = inode->i_sb->s_blocksize;
1337 u32 p_cluster, num_clusters;
1338 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1339 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1341 struct buffer_head *bh = NULL;
1342 unsigned int ext_flags;
1343 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1345 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1347 while (cpos < clusters) {
1348 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1349 &num_clusters, &xv->xr_list,
1356 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1358 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1360 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1361 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1368 ret = ocfs2_journal_access(handle,
1371 OCFS2_JOURNAL_ACCESS_WRITE);
1377 cp_len = value_len > blocksize ? blocksize : value_len;
1378 memcpy(bh->b_data, value, cp_len);
1379 value_len -= cp_len;
1381 if (cp_len < blocksize)
1382 memset(bh->b_data + cp_len, 0,
1383 blocksize - cp_len);
1385 ret = ocfs2_journal_dirty(handle, bh);
1394 * XXX: do we need to empty all the following
1395 * blocks in this cluster?
1400 cpos += num_clusters;
1408 static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1416 free_space = free_start -
1417 sizeof(struct ocfs2_xattr_header) -
1418 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1419 OCFS2_XATTR_HEADER_GAP;
1422 if (free_space < needed_space)
1428 static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1431 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1434 static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1436 loc->xl_ops->xlo_journal_dirty(handle, loc);
1439 /* Give a pointer into the storage for the given offset */
1440 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1442 BUG_ON(offset >= loc->xl_size);
1443 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1447 * Wipe the name+value pair and allow the storage to reclaim it. This
1448 * must be followed by either removal of the entry or a call to
1449 * ocfs2_xa_add_namevalue().
1451 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1453 loc->xl_ops->xlo_wipe_namevalue(loc);
1457 * Find lowest offset to a name+value pair. This is the start of our
1458 * downward-growing free space.
1460 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1462 return loc->xl_ops->xlo_get_free_start(loc);
1465 /* Can we reuse loc->xl_entry for xi? */
1466 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1467 struct ocfs2_xattr_info *xi)
1469 return loc->xl_ops->xlo_can_reuse(loc, xi);
1472 /* How much free space is needed to set the new value */
1473 static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1474 struct ocfs2_xattr_info *xi)
1476 return loc->xl_ops->xlo_check_space(loc, xi);
1479 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1481 loc->xl_ops->xlo_add_entry(loc, name_hash);
1482 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1484 * We can't leave the new entry's xe_name_offset at zero or
1485 * add_namevalue() will go nuts. We set it to the size of our
1486 * storage so that it can never be less than any other entry.
1488 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1491 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1492 struct ocfs2_xattr_info *xi)
1494 int size = namevalue_size_xi(xi);
1498 loc->xl_ops->xlo_add_namevalue(loc, size);
1499 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1500 loc->xl_entry->xe_name_len = xi->xi_name_len;
1501 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1502 ocfs2_xattr_set_local(loc->xl_entry,
1503 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1505 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1506 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1507 memset(nameval_buf, 0, size);
1508 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1511 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1512 struct ocfs2_xattr_value_buf *vb)
1514 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1515 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1517 /* Value bufs are for value trees */
1518 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
1519 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1520 (name_size + OCFS2_XATTR_ROOT_SIZE));
1522 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1524 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1529 static int ocfs2_xa_block_journal_access(handle_t *handle,
1530 struct ocfs2_xa_loc *loc, int type)
1532 struct buffer_head *bh = loc->xl_storage;
1533 ocfs2_journal_access_func access;
1535 if (loc->xl_size == (bh->b_size -
1536 offsetof(struct ocfs2_xattr_block,
1537 xb_attrs.xb_header)))
1538 access = ocfs2_journal_access_xb;
1540 access = ocfs2_journal_access_di;
1541 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1544 static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1545 struct ocfs2_xa_loc *loc)
1547 struct buffer_head *bh = loc->xl_storage;
1549 ocfs2_journal_dirty(handle, bh);
1552 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1555 return (char *)loc->xl_header + offset;
1558 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1559 struct ocfs2_xattr_info *xi)
1562 * Block storage is strict. If the sizes aren't exact, we will
1563 * remove the old one and reinsert the new.
1565 return namevalue_size_xe(loc->xl_entry) ==
1566 namevalue_size_xi(xi);
1569 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1571 struct ocfs2_xattr_header *xh = loc->xl_header;
1572 int i, count = le16_to_cpu(xh->xh_count);
1573 int offset, free_start = loc->xl_size;
1575 for (i = 0; i < count; i++) {
1576 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1577 if (offset < free_start)
1578 free_start = offset;
1584 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1585 struct ocfs2_xattr_info *xi)
1587 int count = le16_to_cpu(loc->xl_header->xh_count);
1588 int free_start = ocfs2_xa_get_free_start(loc);
1589 int needed_space = ocfs2_xi_entry_usage(xi);
1592 * Block storage will reclaim the original entry before inserting
1593 * the new value, so we only need the difference. If the new
1594 * entry is smaller than the old one, we don't need anything.
1596 if (loc->xl_entry) {
1597 /* Don't need space if we're reusing! */
1598 if (ocfs2_xa_can_reuse_entry(loc, xi))
1601 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1603 if (needed_space < 0)
1605 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1609 * Block storage for xattrs keeps the name+value pairs compacted. When
1610 * we remove one, we have to shift any that preceded it towards the end.
1612 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1615 int namevalue_offset, first_namevalue_offset, namevalue_size;
1616 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1617 struct ocfs2_xattr_header *xh = loc->xl_header;
1618 int count = le16_to_cpu(xh->xh_count);
1620 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1621 namevalue_size = namevalue_size_xe(entry);
1622 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1624 /* Shift the name+value pairs */
1625 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1626 (char *)xh + first_namevalue_offset,
1627 namevalue_offset - first_namevalue_offset);
1628 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1630 /* Now tell xh->xh_entries about it */
1631 for (i = 0; i < count; i++) {
1632 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1633 if (offset < namevalue_offset)
1634 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1639 * Note that we don't update xh_free_start or xh_name_value_len
1640 * because they're not used in block-stored xattrs.
1644 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1646 int count = le16_to_cpu(loc->xl_header->xh_count);
1647 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1648 le16_add_cpu(&loc->xl_header->xh_count, 1);
1649 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1652 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1654 int free_start = ocfs2_xa_get_free_start(loc);
1656 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1659 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1660 struct ocfs2_xattr_value_buf *vb)
1662 struct buffer_head *bh = loc->xl_storage;
1664 if (loc->xl_size == (bh->b_size -
1665 offsetof(struct ocfs2_xattr_block,
1666 xb_attrs.xb_header)))
1667 vb->vb_access = ocfs2_journal_access_xb;
1669 vb->vb_access = ocfs2_journal_access_di;
1674 * Operations for xattrs stored in blocks. This includes inline inode
1675 * storage and unindexed ocfs2_xattr_blocks.
1677 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1678 .xlo_journal_access = ocfs2_xa_block_journal_access,
1679 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
1680 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
1681 .xlo_check_space = ocfs2_xa_block_check_space,
1682 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1683 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
1684 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
1685 .xlo_add_entry = ocfs2_xa_block_add_entry,
1686 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
1687 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
1690 static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1691 struct ocfs2_xa_loc *loc, int type)
1693 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1695 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1698 static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1699 struct ocfs2_xa_loc *loc)
1701 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1703 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1706 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1709 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1710 int block, block_offset;
1712 /* The header is at the front of the bucket */
1713 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1714 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
1716 return bucket_block(bucket, block) + block_offset;
1719 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1720 struct ocfs2_xattr_info *xi)
1722 return namevalue_size_xe(loc->xl_entry) >=
1723 namevalue_size_xi(xi);
1726 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1728 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1729 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1732 static int ocfs2_bucket_align_free_start(struct super_block *sb,
1733 int free_start, int size)
1736 * We need to make sure that the name+value pair fits within
1739 if (((free_start - size) >> sb->s_blocksize_bits) !=
1740 ((free_start - 1) >> sb->s_blocksize_bits))
1741 free_start -= free_start % sb->s_blocksize;
1746 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1747 struct ocfs2_xattr_info *xi)
1750 int count = le16_to_cpu(loc->xl_header->xh_count);
1751 int free_start = ocfs2_xa_get_free_start(loc);
1752 int needed_space = ocfs2_xi_entry_usage(xi);
1753 int size = namevalue_size_xi(xi);
1754 struct super_block *sb = loc->xl_inode->i_sb;
1757 * Bucket storage does not reclaim name+value pairs it cannot
1758 * reuse. They live as holes until the bucket fills, and then
1759 * the bucket is defragmented. However, the bucket can reclaim
1760 * the ocfs2_xattr_entry.
1762 if (loc->xl_entry) {
1763 /* Don't need space if we're reusing! */
1764 if (ocfs2_xa_can_reuse_entry(loc, xi))
1767 needed_space -= sizeof(struct ocfs2_xattr_entry);
1769 BUG_ON(needed_space < 0);
1771 if (free_start < size) {
1776 * First we check if it would fit in the first place.
1777 * Below, we align the free start to a block. This may
1778 * slide us below the minimum gap. By checking unaligned
1779 * first, we avoid that error.
1781 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1785 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1788 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1791 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1793 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1794 -namevalue_size_xe(loc->xl_entry));
1797 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1799 struct ocfs2_xattr_header *xh = loc->xl_header;
1800 int count = le16_to_cpu(xh->xh_count);
1801 int low = 0, high = count - 1, tmp;
1802 struct ocfs2_xattr_entry *tmp_xe;
1805 * We keep buckets sorted by name_hash, so we need to find
1808 while (low <= high && count) {
1809 tmp = (low + high) / 2;
1810 tmp_xe = &xh->xh_entries[tmp];
1812 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1814 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1823 memmove(&xh->xh_entries[low + 1],
1824 &xh->xh_entries[low],
1825 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1827 le16_add_cpu(&xh->xh_count, 1);
1828 loc->xl_entry = &xh->xh_entries[low];
1829 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1832 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1834 int free_start = ocfs2_xa_get_free_start(loc);
1835 struct ocfs2_xattr_header *xh = loc->xl_header;
1836 struct super_block *sb = loc->xl_inode->i_sb;
1839 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1840 nameval_offset = free_start - size;
1841 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1842 xh->xh_free_start = cpu_to_le16(nameval_offset);
1843 le16_add_cpu(&xh->xh_name_value_len, size);
1847 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1848 struct ocfs2_xattr_value_buf *vb)
1850 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1851 struct super_block *sb = loc->xl_inode->i_sb;
1852 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1853 int size = namevalue_size_xe(loc->xl_entry);
1854 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1856 /* Values are not allowed to straddle block boundaries */
1857 BUG_ON(block_offset !=
1858 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1859 /* We expect the bucket to be filled in */
1860 BUG_ON(!bucket->bu_bhs[block_offset]);
1862 vb->vb_access = ocfs2_journal_access;
1863 vb->vb_bh = bucket->bu_bhs[block_offset];
1866 /* Operations for xattrs stored in buckets. */
1867 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1868 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1869 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
1870 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
1871 .xlo_check_space = ocfs2_xa_bucket_check_space,
1872 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1873 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
1874 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
1875 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1876 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
1877 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
1880 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1881 struct ocfs2_xattr_set_ctxt *ctxt)
1883 int trunc_rc, access_rc;
1884 struct ocfs2_xattr_value_buf vb;
1886 ocfs2_xa_fill_value_buf(loc, &vb);
1887 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1891 * The caller of ocfs2_xa_value_truncate() has already called
1892 * ocfs2_xa_journal_access on the loc. However, The truncate code
1893 * calls ocfs2_extend_trans(). This may commit the previous
1894 * transaction and open a new one. If this is a bucket, truncate
1895 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1896 * the caller is expecting to dirty the entire bucket. So we must
1897 * reset the journal work. We do this even if truncate has failed,
1898 * as it could have failed after committing the extend.
1900 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1901 OCFS2_JOURNAL_ACCESS_WRITE);
1903 /* Errors in truncate take precedence */
1904 return trunc_rc ? trunc_rc : access_rc;
1907 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1910 struct ocfs2_xattr_header *xh = loc->xl_header;
1911 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1913 ocfs2_xa_wipe_namevalue(loc);
1914 loc->xl_entry = NULL;
1916 le16_add_cpu(&xh->xh_count, -1);
1917 count = le16_to_cpu(xh->xh_count);
1920 * Only zero out the entry if there are more remaining. This is
1921 * important for an empty bucket, as it keeps track of the
1922 * bucket's hash value. It doesn't hurt empty block storage.
1925 index = ((char *)entry - (char *)&xh->xh_entries) /
1926 sizeof(struct ocfs2_xattr_entry);
1927 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1928 (count - index) * sizeof(struct ocfs2_xattr_entry));
1929 memset(&xh->xh_entries[count], 0,
1930 sizeof(struct ocfs2_xattr_entry));
1934 static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1935 struct ocfs2_xattr_set_ctxt *ctxt)
1939 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
1940 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
1947 ocfs2_xa_remove_entry(loc);
1953 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
1955 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1958 nameval_buf = ocfs2_xa_offset_pointer(loc,
1959 le16_to_cpu(loc->xl_entry->xe_name_offset));
1960 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
1964 * Take an existing entry and make it ready for the new value. This
1965 * won't allocate space, but it may free space. It should be ready for
1966 * ocfs2_xa_prepare_entry() to finish the work.
1968 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
1969 struct ocfs2_xattr_info *xi,
1970 struct ocfs2_xattr_set_ctxt *ctxt)
1973 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
1975 int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
1976 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
1978 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
1981 nameval_buf = ocfs2_xa_offset_pointer(loc,
1982 le16_to_cpu(loc->xl_entry->xe_name_offset));
1984 memset(nameval_buf + name_size, 0,
1985 namevalue_size_xe(loc->xl_entry) - name_size);
1987 ocfs2_xa_install_value_root(loc);
1990 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
1995 memset(nameval_buf + name_size, 0,
1996 namevalue_size_xe(loc->xl_entry) -
1998 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2000 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2009 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2010 ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2017 * Prepares loc->xl_entry to receive the new xattr. This includes
2018 * properly setting up the name+value pair region. If loc->xl_entry
2019 * already exists, it will take care of modifying it appropriately.
2021 * Note that this modifies the data. You did journal_access already,
2024 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2025 struct ocfs2_xattr_info *xi,
2027 struct ocfs2_xattr_set_ctxt *ctxt)
2031 rc = ocfs2_xa_check_space(loc, xi);
2035 if (loc->xl_entry) {
2036 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
2037 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2043 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2044 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2050 ocfs2_xa_wipe_namevalue(loc);
2052 ocfs2_xa_add_entry(loc, name_hash);
2055 * If we get here, we have a blank entry. Fill it. We grow our
2056 * name+value pair back from the end.
2058 ocfs2_xa_add_namevalue(loc, xi);
2059 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2060 ocfs2_xa_install_value_root(loc);
2063 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2064 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2074 * Store the value portion of the name+value pair. This will skip
2075 * values that are stored externally. Their tree roots were set up
2076 * by ocfs2_xa_prepare_entry().
2078 static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2079 struct ocfs2_xattr_info *xi,
2080 struct ocfs2_xattr_set_ctxt *ctxt)
2083 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2084 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2086 struct ocfs2_xattr_value_buf vb;
2088 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
2089 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2090 ocfs2_xa_fill_value_buf(loc, &vb);
2091 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2096 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2101 static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2102 struct ocfs2_xattr_info *xi,
2103 struct ocfs2_xattr_set_ctxt *ctxt)
2106 u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2109 ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2110 OCFS2_JOURNAL_ACCESS_WRITE);
2116 /* Don't worry, we are never called with !xi_value and !xl_entry */
2117 if (!xi->xi_value) {
2118 ret = ocfs2_xa_remove(loc, ctxt);
2122 ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2129 ret = ocfs2_xa_store_value(loc, xi, ctxt);
2135 ocfs2_xa_journal_dirty(ctxt->handle, loc);
2141 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2142 struct inode *inode,
2143 struct buffer_head *bh,
2144 struct ocfs2_xattr_entry *entry)
2146 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2148 loc->xl_inode = inode;
2149 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2150 loc->xl_storage = bh;
2151 loc->xl_entry = entry;
2153 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2154 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2157 loc->xl_size = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2160 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2164 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2165 struct inode *inode,
2166 struct buffer_head *bh,
2167 struct ocfs2_xattr_entry *entry)
2169 struct ocfs2_xattr_block *xb =
2170 (struct ocfs2_xattr_block *)bh->b_data;
2172 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2174 loc->xl_inode = inode;
2175 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2176 loc->xl_storage = bh;
2177 loc->xl_header = &(xb->xb_attrs.xb_header);
2178 loc->xl_entry = entry;
2179 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2180 xb_attrs.xb_header);
2183 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2184 struct ocfs2_xattr_bucket *bucket,
2185 struct ocfs2_xattr_entry *entry)
2187 loc->xl_inode = bucket->bu_inode;
2188 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2189 loc->xl_storage = bucket;
2190 loc->xl_header = bucket_xh(bucket);
2191 loc->xl_entry = entry;
2192 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2197 * ocfs2_xattr_set_entry()
2199 * Set extended attribute entry into inode or block.
2201 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
2202 * We first insert tree root(ocfs2_xattr_value_root) like a normal value,
2203 * then set value in B tree with set_value_outside().
2205 static int ocfs2_xattr_set_entry(struct inode *inode,
2206 struct ocfs2_xattr_info *xi,
2207 struct ocfs2_xattr_search *xs,
2208 struct ocfs2_xattr_set_ctxt *ctxt,
2211 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2212 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2213 handle_t *handle = ctxt->handle;
2215 struct ocfs2_xa_loc loc;
2217 if (!(flag & OCFS2_INLINE_XATTR_FL))
2218 BUG_ON(xs->xattr_bh == xs->inode_bh);
2220 BUG_ON(xs->xattr_bh != xs->inode_bh);
2222 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
2223 OCFS2_JOURNAL_ACCESS_WRITE);
2229 if (xs->xattr_bh == xs->inode_bh)
2230 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2231 xs->not_found ? NULL : xs->here);
2233 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2234 xs->not_found ? NULL : xs->here);
2236 ret = ocfs2_xa_set(&loc, xi, ctxt);
2242 xs->here = loc.xl_entry;
2244 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
2245 (flag & OCFS2_INLINE_XATTR_FL)) {
2246 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2247 unsigned int xattrsize = osb->s_xattr_inline_size;
2250 * Adjust extent record count or inline data size
2251 * to reserve space for extended attribute.
2253 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2254 struct ocfs2_inline_data *idata = &di->id2.i_data;
2255 le16_add_cpu(&idata->id_count, -xattrsize);
2256 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2257 struct ocfs2_extent_list *el = &di->id2.i_list;
2258 le16_add_cpu(&el->l_count, -(xattrsize /
2259 sizeof(struct ocfs2_extent_rec)));
2261 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2263 /* Update xattr flag */
2264 spin_lock(&oi->ip_lock);
2265 oi->ip_dyn_features |= flag;
2266 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2267 spin_unlock(&oi->ip_lock);
2269 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
2278 * In xattr remove, if it is stored outside and refcounted, we may have
2279 * the chance to split the refcount tree. So need the allocators.
2281 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2282 struct ocfs2_xattr_value_root *xv,
2283 struct ocfs2_caching_info *ref_ci,
2284 struct buffer_head *ref_root_bh,
2285 struct ocfs2_alloc_context **meta_ac,
2288 int ret, meta_add = 0;
2289 u32 p_cluster, num_clusters;
2290 unsigned int ext_flags;
2293 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2302 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2305 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2307 &meta_add, ref_credits);
2313 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2322 static int ocfs2_remove_value_outside(struct inode*inode,
2323 struct ocfs2_xattr_value_buf *vb,
2324 struct ocfs2_xattr_header *header,
2325 struct ocfs2_caching_info *ref_ci,
2326 struct buffer_head *ref_root_bh)
2328 int ret = 0, i, ref_credits;
2329 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2330 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2333 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2335 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2336 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2338 if (ocfs2_xattr_is_local(entry))
2341 val = (void *)header +
2342 le16_to_cpu(entry->xe_name_offset);
2343 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2344 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2346 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2347 ref_ci, ref_root_bh,
2351 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2352 ocfs2_remove_extent_credits(osb->sb));
2353 if (IS_ERR(ctxt.handle)) {
2354 ret = PTR_ERR(ctxt.handle);
2359 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2365 ocfs2_commit_trans(osb, ctxt.handle);
2367 ocfs2_free_alloc_context(ctxt.meta_ac);
2368 ctxt.meta_ac = NULL;
2373 ocfs2_free_alloc_context(ctxt.meta_ac);
2374 ocfs2_schedule_truncate_log_flush(osb, 1);
2375 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2379 static int ocfs2_xattr_ibody_remove(struct inode *inode,
2380 struct buffer_head *di_bh,
2381 struct ocfs2_caching_info *ref_ci,
2382 struct buffer_head *ref_root_bh)
2385 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2386 struct ocfs2_xattr_header *header;
2388 struct ocfs2_xattr_value_buf vb = {
2390 .vb_access = ocfs2_journal_access_di,
2393 header = (struct ocfs2_xattr_header *)
2394 ((void *)di + inode->i_sb->s_blocksize -
2395 le16_to_cpu(di->i_xattr_inline_size));
2397 ret = ocfs2_remove_value_outside(inode, &vb, header,
2398 ref_ci, ref_root_bh);
2403 struct ocfs2_rm_xattr_bucket_para {
2404 struct ocfs2_caching_info *ref_ci;
2405 struct buffer_head *ref_root_bh;
2408 static int ocfs2_xattr_block_remove(struct inode *inode,
2409 struct buffer_head *blk_bh,
2410 struct ocfs2_caching_info *ref_ci,
2411 struct buffer_head *ref_root_bh)
2413 struct ocfs2_xattr_block *xb;
2415 struct ocfs2_xattr_value_buf vb = {
2417 .vb_access = ocfs2_journal_access_xb,
2419 struct ocfs2_rm_xattr_bucket_para args = {
2421 .ref_root_bh = ref_root_bh,
2424 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2425 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2426 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2427 ret = ocfs2_remove_value_outside(inode, &vb, header,
2428 ref_ci, ref_root_bh);
2430 ret = ocfs2_iterate_xattr_index_block(inode,
2432 ocfs2_rm_xattr_cluster,
2438 static int ocfs2_xattr_free_block(struct inode *inode,
2440 struct ocfs2_caching_info *ref_ci,
2441 struct buffer_head *ref_root_bh)
2443 struct inode *xb_alloc_inode;
2444 struct buffer_head *xb_alloc_bh = NULL;
2445 struct buffer_head *blk_bh = NULL;
2446 struct ocfs2_xattr_block *xb;
2447 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2453 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2459 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2465 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2466 blk = le64_to_cpu(xb->xb_blkno);
2467 bit = le16_to_cpu(xb->xb_suballoc_bit);
2468 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2470 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2471 EXTENT_ALLOC_SYSTEM_INODE,
2472 le16_to_cpu(xb->xb_suballoc_slot));
2473 if (!xb_alloc_inode) {
2478 mutex_lock(&xb_alloc_inode->i_mutex);
2480 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2486 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2487 if (IS_ERR(handle)) {
2488 ret = PTR_ERR(handle);
2493 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2498 ocfs2_commit_trans(osb, handle);
2500 ocfs2_inode_unlock(xb_alloc_inode, 1);
2501 brelse(xb_alloc_bh);
2503 mutex_unlock(&xb_alloc_inode->i_mutex);
2504 iput(xb_alloc_inode);
2511 * ocfs2_xattr_remove()
2513 * Free extended attribute resources associated with this inode.
2515 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2517 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2518 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2519 struct ocfs2_refcount_tree *ref_tree = NULL;
2520 struct buffer_head *ref_root_bh = NULL;
2521 struct ocfs2_caching_info *ref_ci = NULL;
2525 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2528 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2531 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2532 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2533 le64_to_cpu(di->i_refcount_loc),
2534 1, &ref_tree, &ref_root_bh);
2539 ref_ci = &ref_tree->rf_ci;
2543 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2544 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2545 ref_ci, ref_root_bh);
2552 if (di->i_xattr_loc) {
2553 ret = ocfs2_xattr_free_block(inode,
2554 le64_to_cpu(di->i_xattr_loc),
2555 ref_ci, ref_root_bh);
2562 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2563 OCFS2_INODE_UPDATE_CREDITS);
2564 if (IS_ERR(handle)) {
2565 ret = PTR_ERR(handle);
2569 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2570 OCFS2_JOURNAL_ACCESS_WRITE);
2576 di->i_xattr_loc = 0;
2578 spin_lock(&oi->ip_lock);
2579 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2580 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2581 spin_unlock(&oi->ip_lock);
2583 ret = ocfs2_journal_dirty(handle, di_bh);
2587 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2590 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2591 brelse(ref_root_bh);
2595 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2596 struct ocfs2_dinode *di)
2598 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2599 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2602 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2605 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2606 struct ocfs2_inline_data *idata = &di->id2.i_data;
2607 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2608 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2609 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2610 le64_to_cpu(di->i_size);
2612 struct ocfs2_extent_list *el = &di->id2.i_list;
2613 free = (le16_to_cpu(el->l_count) -
2614 le16_to_cpu(el->l_next_free_rec)) *
2615 sizeof(struct ocfs2_extent_rec);
2617 if (free >= xattrsize)
2624 * ocfs2_xattr_ibody_find()
2626 * Find extended attribute in inode block and
2627 * fill search info into struct ocfs2_xattr_search.
2629 static int ocfs2_xattr_ibody_find(struct inode *inode,
2632 struct ocfs2_xattr_search *xs)
2634 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2635 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2639 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2642 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2643 down_read(&oi->ip_alloc_sem);
2644 has_space = ocfs2_xattr_has_space_inline(inode, di);
2645 up_read(&oi->ip_alloc_sem);
2650 xs->xattr_bh = xs->inode_bh;
2651 xs->end = (void *)di + inode->i_sb->s_blocksize;
2652 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2653 xs->header = (struct ocfs2_xattr_header *)
2654 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2656 xs->header = (struct ocfs2_xattr_header *)
2657 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2658 xs->base = (void *)xs->header;
2659 xs->here = xs->header->xh_entries;
2661 /* Find the named attribute. */
2662 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2663 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2664 if (ret && ret != -ENODATA)
2666 xs->not_found = ret;
2673 * ocfs2_xattr_ibody_set()
2675 * Set, replace or remove an extended attribute into inode block.
2678 static int ocfs2_xattr_ibody_set(struct inode *inode,
2679 struct ocfs2_xattr_info *xi,
2680 struct ocfs2_xattr_search *xs,
2681 struct ocfs2_xattr_set_ctxt *ctxt)
2683 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2684 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2687 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2690 down_write(&oi->ip_alloc_sem);
2691 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2692 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2698 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2699 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2701 up_write(&oi->ip_alloc_sem);
2707 * ocfs2_xattr_block_find()
2709 * Find extended attribute in external block and
2710 * fill search info into struct ocfs2_xattr_search.
2712 static int ocfs2_xattr_block_find(struct inode *inode,
2715 struct ocfs2_xattr_search *xs)
2717 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2718 struct buffer_head *blk_bh = NULL;
2719 struct ocfs2_xattr_block *xb;
2722 if (!di->i_xattr_loc)
2725 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2732 xs->xattr_bh = blk_bh;
2733 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2735 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2736 xs->header = &xb->xb_attrs.xb_header;
2737 xs->base = (void *)xs->header;
2738 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2739 xs->here = xs->header->xh_entries;
2741 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2743 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2747 if (ret && ret != -ENODATA) {
2748 xs->xattr_bh = NULL;
2751 xs->not_found = ret;
2759 static int ocfs2_create_xattr_block(handle_t *handle,
2760 struct inode *inode,
2761 struct buffer_head *inode_bh,
2762 struct ocfs2_alloc_context *meta_ac,
2763 struct buffer_head **ret_bh,
2767 u16 suballoc_bit_start;
2770 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2771 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2772 struct buffer_head *new_bh = NULL;
2773 struct ocfs2_xattr_block *xblk;
2775 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2776 OCFS2_JOURNAL_ACCESS_CREATE);
2782 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2783 &suballoc_bit_start, &num_got,
2790 new_bh = sb_getblk(inode->i_sb, first_blkno);
2791 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2793 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2795 OCFS2_JOURNAL_ACCESS_CREATE);
2801 /* Initialize ocfs2_xattr_block */
2802 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2803 memset(xblk, 0, inode->i_sb->s_blocksize);
2804 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2805 xblk->xb_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
2806 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2807 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2808 xblk->xb_blkno = cpu_to_le64(first_blkno);
2811 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2812 xr->xt_clusters = cpu_to_le32(1);
2813 xr->xt_last_eb_blk = 0;
2814 xr->xt_list.l_tree_depth = 0;
2815 xr->xt_list.l_count = cpu_to_le16(
2816 ocfs2_xattr_recs_per_xb(inode->i_sb));
2817 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2818 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2821 ret = ocfs2_journal_dirty(handle, new_bh);
2826 di->i_xattr_loc = cpu_to_le64(first_blkno);
2827 ocfs2_journal_dirty(handle, inode_bh);
2838 * ocfs2_xattr_block_set()
2840 * Set, replace or remove an extended attribute into external block.
2843 static int ocfs2_xattr_block_set(struct inode *inode,
2844 struct ocfs2_xattr_info *xi,
2845 struct ocfs2_xattr_search *xs,
2846 struct ocfs2_xattr_set_ctxt *ctxt)
2848 struct buffer_head *new_bh = NULL;
2849 handle_t *handle = ctxt->handle;
2850 struct ocfs2_xattr_block *xblk = NULL;
2853 if (!xs->xattr_bh) {
2854 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
2855 ctxt->meta_ac, &new_bh, 0);
2861 xs->xattr_bh = new_bh;
2862 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2863 xs->header = &xblk->xb_attrs.xb_header;
2864 xs->base = (void *)xs->header;
2865 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2866 xs->here = xs->header->xh_entries;
2868 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2870 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2871 /* Set extended attribute into external block */
2872 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2873 OCFS2_HAS_XATTR_FL);
2874 if (!ret || ret != -ENOSPC)
2877 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2882 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2889 /* Check whether the new xattr can be inserted into the inode. */
2890 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2891 struct ocfs2_xattr_info *xi,
2892 struct ocfs2_xattr_search *xs)
2894 struct ocfs2_xattr_entry *last;
2896 size_t min_offs = xs->end - xs->base;
2901 last = xs->header->xh_entries;
2903 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2904 size_t offs = le16_to_cpu(last->xe_name_offset);
2905 if (offs < min_offs)
2910 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2914 BUG_ON(!xs->not_found);
2916 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
2922 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2923 struct ocfs2_dinode *di,
2924 struct ocfs2_xattr_info *xi,
2925 struct ocfs2_xattr_search *xis,
2926 struct ocfs2_xattr_search *xbs,
2931 int ret = 0, old_in_xb = 0;
2932 int clusters_add = 0, meta_add = 0, credits = 0;
2933 struct buffer_head *bh = NULL;
2934 struct ocfs2_xattr_block *xb = NULL;
2935 struct ocfs2_xattr_entry *xe = NULL;
2936 struct ocfs2_xattr_value_root *xv = NULL;
2938 int name_offset, name_len = 0;
2939 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2944 * Calculate the clusters we need to write.
2945 * No matter whether we replace an old one or add a new one,
2946 * we need this for writing.
2948 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2949 credits += new_clusters *
2950 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2952 if (xis->not_found && xbs->not_found) {
2953 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2955 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2956 clusters_add += new_clusters;
2957 credits += ocfs2_calc_extend_credits(inode->i_sb,
2965 if (!xis->not_found) {
2967 name_offset = le16_to_cpu(xe->xe_name_offset);
2968 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2970 credits += OCFS2_INODE_UPDATE_CREDITS;
2972 int i, block_off = 0;
2973 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2975 name_offset = le16_to_cpu(xe->xe_name_offset);
2976 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2977 i = xbs->here - xbs->header->xh_entries;
2980 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2981 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
2982 bucket_xh(xbs->bucket),
2985 base = bucket_block(xbs->bucket, block_off);
2986 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2989 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2994 * delete a xattr doesn't need metadata and cluster allocation.
2995 * so just calculate the credits and return.
2997 * The credits for removing the value tree will be extended
2998 * by ocfs2_remove_extent itself.
3000 if (!xi->xi_value) {
3001 if (!ocfs2_xattr_is_local(xe))
3002 credits += ocfs2_remove_extent_credits(inode->i_sb);
3007 /* do cluster allocation guess first. */
3008 value_size = le64_to_cpu(xe->xe_value_size);
3012 * In xattr set, we always try to set the xe in inode first,
3013 * so if it can be inserted into inode successfully, the old
3014 * one will be removed from the xattr block, and this xattr
3015 * will be inserted into inode as a new xattr in inode.
3017 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3018 clusters_add += new_clusters;
3019 credits += ocfs2_remove_extent_credits(inode->i_sb) +
3020 OCFS2_INODE_UPDATE_CREDITS;
3021 if (!ocfs2_xattr_is_local(xe))
3022 credits += ocfs2_calc_extend_credits(
3030 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3031 /* the new values will be stored outside. */
3032 u32 old_clusters = 0;
3034 if (!ocfs2_xattr_is_local(xe)) {
3035 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3037 xv = (struct ocfs2_xattr_value_root *)
3038 (base + name_offset + name_len);
3039 value_size = OCFS2_XATTR_ROOT_SIZE;
3043 if (old_clusters >= new_clusters) {
3044 credits += ocfs2_remove_extent_credits(inode->i_sb);
3047 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3048 clusters_add += new_clusters - old_clusters;
3049 credits += ocfs2_calc_extend_credits(inode->i_sb,
3053 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3058 * Now the new value will be stored inside. So if the new
3059 * value is smaller than the size of value root or the old
3060 * value, we don't need any allocation, otherwise we have
3061 * to guess metadata allocation.
3063 if ((ocfs2_xattr_is_local(xe) &&
3064 (value_size >= xi->xi_value_len)) ||
3065 (!ocfs2_xattr_is_local(xe) &&
3066 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
3071 /* calculate metadata allocation. */
3072 if (di->i_xattr_loc) {
3073 if (!xbs->xattr_bh) {
3074 ret = ocfs2_read_xattr_block(inode,
3075 le64_to_cpu(di->i_xattr_loc),
3082 xb = (struct ocfs2_xattr_block *)bh->b_data;
3084 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3087 * If there is already an xattr tree, good, we can calculate
3088 * like other b-trees. Otherwise we may have the chance of
3089 * create a tree, the credit calculation is borrowed from
3090 * ocfs2_calc_extend_credits with root_el = NULL. And the
3091 * new tree will be cluster based, so no meta is needed.
3093 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3094 struct ocfs2_extent_list *el =
3095 &xb->xb_attrs.xb_root.xt_list;
3096 meta_add += ocfs2_extend_meta_needed(el);
3097 credits += ocfs2_calc_extend_credits(inode->i_sb,
3100 credits += OCFS2_SUBALLOC_ALLOC + 1;
3103 * This cluster will be used either for new bucket or for
3105 * If the cluster size is the same as the bucket size, one
3106 * more is needed since we may need to extend the bucket
3110 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3111 if (OCFS2_XATTR_BUCKET_SIZE ==
3112 OCFS2_SB(inode->i_sb)->s_clustersize) {
3113 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3118 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3122 *clusters_need = clusters_add;
3124 *meta_need = meta_add;
3126 *credits_need = credits;
3131 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3132 struct ocfs2_dinode *di,
3133 struct ocfs2_xattr_info *xi,
3134 struct ocfs2_xattr_search *xis,
3135 struct ocfs2_xattr_search *xbs,
3136 struct ocfs2_xattr_set_ctxt *ctxt,
3140 int clusters_add, meta_add, ret;
3141 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3143 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3145 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3147 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
3148 &clusters_add, &meta_add, credits);
3154 meta_add += extra_meta;
3155 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
3156 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
3159 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3168 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3174 if (ctxt->meta_ac) {
3175 ocfs2_free_alloc_context(ctxt->meta_ac);
3176 ctxt->meta_ac = NULL;
3180 * We cannot have an error and a non null ctxt->data_ac.
3187 static int __ocfs2_xattr_set_handle(struct inode *inode,
3188 struct ocfs2_dinode *di,
3189 struct ocfs2_xattr_info *xi,
3190 struct ocfs2_xattr_search *xis,
3191 struct ocfs2_xattr_search *xbs,
3192 struct ocfs2_xattr_set_ctxt *ctxt)
3194 int ret = 0, credits, old_found;
3196 if (!xi->xi_value) {
3197 /* Remove existing extended attribute */
3198 if (!xis->not_found)
3199 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3200 else if (!xbs->not_found)
3201 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3203 /* We always try to set extended attribute into inode first*/
3204 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3205 if (!ret && !xbs->not_found) {
3207 * If succeed and that extended attribute existing in
3208 * external block, then we will remove it.
3210 xi->xi_value = NULL;
3211 xi->xi_value_len = 0;
3213 old_found = xis->not_found;
3214 xis->not_found = -ENODATA;
3215 ret = ocfs2_calc_xattr_set_need(inode,
3223 xis->not_found = old_found;
3229 ret = ocfs2_extend_trans(ctxt->handle, credits +
3230 ctxt->handle->h_buffer_credits);
3235 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3236 } else if (ret == -ENOSPC) {
3237 if (di->i_xattr_loc && !xbs->xattr_bh) {
3238 ret = ocfs2_xattr_block_find(inode,
3244 old_found = xis->not_found;
3245 xis->not_found = -ENODATA;
3246 ret = ocfs2_calc_xattr_set_need(inode,
3254 xis->not_found = old_found;
3260 ret = ocfs2_extend_trans(ctxt->handle, credits +
3261 ctxt->handle->h_buffer_credits);
3268 * If no space in inode, we will set extended attribute
3269 * into external block.
3271 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3274 if (!xis->not_found) {
3276 * If succeed and that extended attribute
3277 * existing in inode, we will remove it.
3279 xi->xi_value = NULL;
3280 xi->xi_value_len = 0;
3281 xbs->not_found = -ENODATA;
3282 ret = ocfs2_calc_xattr_set_need(inode,
3295 ret = ocfs2_extend_trans(ctxt->handle, credits +
3296 ctxt->handle->h_buffer_credits);
3301 ret = ocfs2_xattr_ibody_set(inode, xi,
3308 /* Update inode ctime. */
3309 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3311 OCFS2_JOURNAL_ACCESS_WRITE);
3317 inode->i_ctime = CURRENT_TIME;
3318 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3319 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3320 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3327 * This function only called duing creating inode
3328 * for init security/acl xattrs of the new inode.
3329 * All transanction credits have been reserved in mknod.
3331 int ocfs2_xattr_set_handle(handle_t *handle,
3332 struct inode *inode,
3333 struct buffer_head *di_bh,
3339 struct ocfs2_alloc_context *meta_ac,
3340 struct ocfs2_alloc_context *data_ac)
3342 struct ocfs2_dinode *di;
3345 struct ocfs2_xattr_info xi = {
3346 .xi_name_index = name_index,
3348 .xi_name_len = strlen(name),
3350 .xi_value_len = value_len,
3353 struct ocfs2_xattr_search xis = {
3354 .not_found = -ENODATA,
3357 struct ocfs2_xattr_search xbs = {
3358 .not_found = -ENODATA,
3361 struct ocfs2_xattr_set_ctxt ctxt = {
3367 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3371 * In extreme situation, may need xattr bucket when
3372 * block size is too small. And we have already reserved
3373 * the credits for bucket in mknod.
3375 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3376 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3378 mlog_errno(-ENOMEM);
3383 xis.inode_bh = xbs.inode_bh = di_bh;
3384 di = (struct ocfs2_dinode *)di_bh->b_data;
3386 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3388 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3391 if (xis.not_found) {
3392 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3397 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3400 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3401 brelse(xbs.xattr_bh);
3402 ocfs2_xattr_bucket_free(xbs.bucket);
3410 * Set, replace or remove an extended attribute for this inode.
3411 * value is NULL to remove an existing extended attribute, else either
3412 * create or replace an extended attribute.
3414 int ocfs2_xattr_set(struct inode *inode,
3421 struct buffer_head *di_bh = NULL;
3422 struct ocfs2_dinode *di;
3423 int ret, credits, ref_meta = 0, ref_credits = 0;
3424 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3425 struct inode *tl_inode = osb->osb_tl_inode;
3426 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
3427 struct ocfs2_refcount_tree *ref_tree = NULL;
3429 struct ocfs2_xattr_info xi = {
3430 .xi_name_index = name_index,
3432 .xi_name_len = strlen(name),
3434 .xi_value_len = value_len,
3437 struct ocfs2_xattr_search xis = {
3438 .not_found = -ENODATA,
3441 struct ocfs2_xattr_search xbs = {
3442 .not_found = -ENODATA,
3445 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3449 * Only xbs will be used on indexed trees. xis doesn't need a
3452 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3454 mlog_errno(-ENOMEM);
3458 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3461 goto cleanup_nolock;
3463 xis.inode_bh = xbs.inode_bh = di_bh;
3464 di = (struct ocfs2_dinode *)di_bh->b_data;
3466 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3468 * Scan inode and external block to find the same name
3469 * extended attribute and collect search infomation.
3471 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3474 if (xis.not_found) {
3475 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3480 if (xis.not_found && xbs.not_found) {
3482 if (flags & XATTR_REPLACE)
3489 if (flags & XATTR_CREATE)
3493 /* Check whether the value is refcounted and do some prepartion. */
3494 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3495 (!xis.not_found || !xbs.not_found)) {
3496 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3497 &xis, &xbs, &ref_tree,
3498 &ref_meta, &ref_credits);
3505 mutex_lock(&tl_inode->i_mutex);
3507 if (ocfs2_truncate_log_needs_flush(osb)) {
3508 ret = __ocfs2_flush_truncate_log(osb);
3510 mutex_unlock(&tl_inode->i_mutex);
3515 mutex_unlock(&tl_inode->i_mutex);
3517 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3518 &xbs, &ctxt, ref_meta, &credits);
3524 /* we need to update inode's ctime field, so add credit for it. */
3525 credits += OCFS2_INODE_UPDATE_CREDITS;
3526 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3527 if (IS_ERR(ctxt.handle)) {
3528 ret = PTR_ERR(ctxt.handle);
3533 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3535 ocfs2_commit_trans(osb, ctxt.handle);
3538 ocfs2_free_alloc_context(ctxt.data_ac);
3540 ocfs2_free_alloc_context(ctxt.meta_ac);
3541 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3542 ocfs2_schedule_truncate_log_flush(osb, 1);
3543 ocfs2_run_deallocs(osb, &ctxt.dealloc);
3547 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3548 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3549 if (!value && !ret) {
3550 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3554 ocfs2_inode_unlock(inode, 1);
3557 brelse(xbs.xattr_bh);
3558 ocfs2_xattr_bucket_free(xbs.bucket);
3564 * Find the xattr extent rec which may contains name_hash.
3565 * e_cpos will be the first name hash of the xattr rec.
3566 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3568 static int ocfs2_xattr_get_rec(struct inode *inode,
3573 struct ocfs2_extent_list *el)
3576 struct buffer_head *eb_bh = NULL;
3577 struct ocfs2_extent_block *eb;
3578 struct ocfs2_extent_rec *rec = NULL;
3581 if (el->l_tree_depth) {
3582 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3589 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3592 if (el->l_tree_depth) {
3593 ocfs2_error(inode->i_sb,
3594 "Inode %lu has non zero tree depth in "
3595 "xattr tree block %llu\n", inode->i_ino,
3596 (unsigned long long)eb_bh->b_blocknr);
3602 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3603 rec = &el->l_recs[i];
3605 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3606 e_blkno = le64_to_cpu(rec->e_blkno);
3612 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3613 "record (%u, %u, 0) in xattr", inode->i_ino,
3614 le32_to_cpu(rec->e_cpos),
3615 ocfs2_rec_clusters(el, rec));
3620 *p_blkno = le64_to_cpu(rec->e_blkno);
3621 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3623 *e_cpos = le32_to_cpu(rec->e_cpos);
3629 typedef int (xattr_bucket_func)(struct inode *inode,
3630 struct ocfs2_xattr_bucket *bucket,
3633 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3634 struct ocfs2_xattr_bucket *bucket,
3641 int i, ret = 0, cmp = 1, block_off, new_offset;
3642 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3643 size_t name_len = strlen(name);
3644 struct ocfs2_xattr_entry *xe = NULL;
3648 * We don't use binary search in the bucket because there
3649 * may be multiple entries with the same name hash.
3651 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3652 xe = &xh->xh_entries[i];
3654 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3656 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3659 cmp = name_index - ocfs2_xattr_get_type(xe);
3661 cmp = name_len - xe->xe_name_len;
3665 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3676 xe_name = bucket_block(bucket, block_off) + new_offset;
3677 if (!memcmp(name, xe_name, name_len)) {
3689 * Find the specified xattr entry in a series of buckets.
3690 * This series start from p_blkno and last for num_clusters.
3691 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3692 * the num of the valid buckets.
3694 * Return the buffer_head this xattr should reside in. And if the xattr's
3695 * hash is in the gap of 2 buckets, return the lower bucket.
3697 static int ocfs2_xattr_bucket_find(struct inode *inode,
3704 struct ocfs2_xattr_search *xs)
3707 struct ocfs2_xattr_header *xh = NULL;
3708 struct ocfs2_xattr_entry *xe = NULL;
3710 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3711 int low_bucket = 0, bucket, high_bucket;
3712 struct ocfs2_xattr_bucket *search;
3714 u64 blkno, lower_blkno = 0;
3716 search = ocfs2_xattr_bucket_new(inode);
3723 ret = ocfs2_read_xattr_bucket(search, p_blkno);
3729 xh = bucket_xh(search);
3730 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3731 while (low_bucket <= high_bucket) {
3732 ocfs2_xattr_bucket_relse(search);
3734 bucket = (low_bucket + high_bucket) / 2;
3735 blkno = p_blkno + bucket * blk_per_bucket;
3736 ret = ocfs2_read_xattr_bucket(search, blkno);
3742 xh = bucket_xh(search);
3743 xe = &xh->xh_entries[0];
3744 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3745 high_bucket = bucket - 1;
3750 * Check whether the hash of the last entry in our
3751 * bucket is larger than the search one. for an empty
3752 * bucket, the last one is also the first one.
3755 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3757 last_hash = le32_to_cpu(xe->xe_name_hash);
3759 /* record lower_blkno which may be the insert place. */
3760 lower_blkno = blkno;
3762 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3763 low_bucket = bucket + 1;
3767 /* the searched xattr should reside in this bucket if exists. */
3768 ret = ocfs2_find_xe_in_bucket(inode, search,
3769 name_index, name, name_hash,
3779 * Record the bucket we have found.
3780 * When the xattr's hash value is in the gap of 2 buckets, we will
3781 * always set it to the previous bucket.
3784 lower_blkno = p_blkno;
3786 /* This should be in cache - we just read it during the search */
3787 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3793 xs->header = bucket_xh(xs->bucket);
3794 xs->base = bucket_block(xs->bucket, 0);
3795 xs->end = xs->base + inode->i_sb->s_blocksize;
3798 xs->here = &xs->header->xh_entries[index];
3799 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3800 (unsigned long long)bucket_blkno(xs->bucket), index);
3805 ocfs2_xattr_bucket_free(search);
3809 static int ocfs2_xattr_index_block_find(struct inode *inode,
3810 struct buffer_head *root_bh,
3813 struct ocfs2_xattr_search *xs)
3816 struct ocfs2_xattr_block *xb =
3817 (struct ocfs2_xattr_block *)root_bh->b_data;
3818 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3819 struct ocfs2_extent_list *el = &xb_root->xt_list;
3821 u32 first_hash, num_clusters = 0;
3822 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3824 if (le16_to_cpu(el->l_next_free_rec) == 0)
3827 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3828 name, name_hash, name_index);
3830 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3837 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3839 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3840 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3843 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3844 p_blkno, first_hash, num_clusters, xs);
3850 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3853 xattr_bucket_func *func,
3857 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3858 u32 num_buckets = clusters * bpc;
3859 struct ocfs2_xattr_bucket *bucket;
3861 bucket = ocfs2_xattr_bucket_new(inode);
3863 mlog_errno(-ENOMEM);
3867 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3868 clusters, (unsigned long long)blkno);
3870 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3871 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3878 * The real bucket num in this series of blocks is stored
3879 * in the 1st bucket.
3882 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3884 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3885 (unsigned long long)blkno,
3886 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3888 ret = func(inode, bucket, para);
3889 if (ret && ret != -ERANGE)
3891 /* Fall through to bucket_relse() */
3894 ocfs2_xattr_bucket_relse(bucket);
3899 ocfs2_xattr_bucket_free(bucket);
3903 struct ocfs2_xattr_tree_list {
3909 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
3910 struct ocfs2_xattr_header *xh,
3917 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3920 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3922 *block_off = name_offset >> sb->s_blocksize_bits;
3923 *new_offset = name_offset % sb->s_blocksize;
3928 static int ocfs2_list_xattr_bucket(struct inode *inode,
3929 struct ocfs2_xattr_bucket *bucket,
3933 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
3934 int i, block_off, new_offset;
3935 const char *prefix, *name;
3937 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3938 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
3939 type = ocfs2_xattr_get_type(entry);
3940 prefix = ocfs2_xattr_prefix(type);
3943 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3951 name = (const char *)bucket_block(bucket, block_off) +
3953 ret = ocfs2_xattr_list_entry(xl->buffer,
3957 entry->xe_name_len);
3966 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
3967 struct buffer_head *blk_bh,
3968 xattr_tree_rec_func *rec_func,
3971 struct ocfs2_xattr_block *xb =
3972 (struct ocfs2_xattr_block *)blk_bh->b_data;
3973 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
3975 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3978 if (!el->l_next_free_rec || !rec_func)
3981 while (name_hash > 0) {
3982 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3983 &e_cpos, &num_clusters, el);
3989 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
3990 num_clusters, para);
4000 name_hash = e_cpos - 1;
4007 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4008 struct buffer_head *root_bh,
4009 u64 blkno, u32 cpos, u32 len, void *para)
4011 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4012 ocfs2_list_xattr_bucket, para);
4015 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4016 struct buffer_head *blk_bh,
4021 struct ocfs2_xattr_tree_list xl = {
4023 .buffer_size = buffer_size,
4027 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4028 ocfs2_list_xattr_tree_rec, &xl);
4039 static int cmp_xe(const void *a, const void *b)
4041 const struct ocfs2_xattr_entry *l = a, *r = b;
4042 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4043 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4045 if (l_hash > r_hash)
4047 if (l_hash < r_hash)
4052 static void swap_xe(void *a, void *b, int size)
4054 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4057 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4058 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4062 * When the ocfs2_xattr_block is filled up, new bucket will be created
4063 * and all the xattr entries will be moved to the new bucket.
4064 * The header goes at the start of the bucket, and the names+values are
4065 * filled from the end. This is why *target starts as the last buffer.
4066 * Note: we need to sort the entries since they are not saved in order
4067 * in the ocfs2_xattr_block.
4069 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4070 struct buffer_head *xb_bh,
4071 struct ocfs2_xattr_bucket *bucket)
4073 int i, blocksize = inode->i_sb->s_blocksize;
4074 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4075 u16 offset, size, off_change;
4076 struct ocfs2_xattr_entry *xe;
4077 struct ocfs2_xattr_block *xb =
4078 (struct ocfs2_xattr_block *)xb_bh->b_data;
4079 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
4080 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4081 u16 count = le16_to_cpu(xb_xh->xh_count);
4082 char *src = xb_bh->b_data;
4083 char *target = bucket_block(bucket, blks - 1);
4085 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4086 (unsigned long long)xb_bh->b_blocknr,
4087 (unsigned long long)bucket_blkno(bucket));
4089 for (i = 0; i < blks; i++)
4090 memset(bucket_block(bucket, i), 0, blocksize);
4093 * Since the xe_name_offset is based on ocfs2_xattr_header,
4094 * there is a offset change corresponding to the change of
4095 * ocfs2_xattr_header's position.
4097 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4098 xe = &xb_xh->xh_entries[count - 1];
4099 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4100 size = blocksize - offset;
4102 /* copy all the names and values. */
4103 memcpy(target + offset, src + offset, size);
4105 /* Init new header now. */
4106 xh->xh_count = xb_xh->xh_count;
4107 xh->xh_num_buckets = cpu_to_le16(1);
4108 xh->xh_name_value_len = cpu_to_le16(size);
4109 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4111 /* copy all the entries. */
4112 target = bucket_block(bucket, 0);
4113 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4114 size = count * sizeof(struct ocfs2_xattr_entry);
4115 memcpy(target + offset, (char *)xb_xh + offset, size);
4117 /* Change the xe offset for all the xe because of the move. */
4118 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4119 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4120 for (i = 0; i < count; i++)
4121 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4123 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4124 offset, size, off_change);
4126 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4131 * After we move xattr from block to index btree, we have to
4132 * update ocfs2_xattr_search to the new xe and base.
4134 * When the entry is in xattr block, xattr_bh indicates the storage place.
4135 * While if the entry is in index b-tree, "bucket" indicates the
4136 * real place of the xattr.
4138 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4139 struct ocfs2_xattr_search *xs,
4140 struct buffer_head *old_bh)
4142 char *buf = old_bh->b_data;
4143 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4144 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
4147 xs->header = bucket_xh(xs->bucket);
4148 xs->base = bucket_block(xs->bucket, 0);
4149 xs->end = xs->base + inode->i_sb->s_blocksize;
4154 i = xs->here - old_xh->xh_entries;
4155 xs->here = &xs->header->xh_entries[i];
4158 static int ocfs2_xattr_create_index_block(struct inode *inode,
4159 struct ocfs2_xattr_search *xs,
4160 struct ocfs2_xattr_set_ctxt *ctxt)
4165 handle_t *handle = ctxt->handle;
4166 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4167 struct ocfs2_inode_info *oi = OCFS2_I(inode);
4168 struct buffer_head *xb_bh = xs->xattr_bh;
4169 struct ocfs2_xattr_block *xb =
4170 (struct ocfs2_xattr_block *)xb_bh->b_data;
4171 struct ocfs2_xattr_tree_root *xr;
4172 u16 xb_flags = le16_to_cpu(xb->xb_flags);
4174 mlog(0, "create xattr index block for %llu\n",
4175 (unsigned long long)xb_bh->b_blocknr);
4177 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
4178 BUG_ON(!xs->bucket);
4182 * We can use this lock for now, and maybe move to a dedicated mutex
4183 * if performance becomes a problem later.
4185 down_write(&oi->ip_alloc_sem);
4187 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
4188 OCFS2_JOURNAL_ACCESS_WRITE);
4194 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
4195 1, 1, &bit_off, &len);
4202 * The bucket may spread in many blocks, and
4203 * we will only touch the 1st block and the last block
4204 * in the whole bucket(one for entry and one for data).
4206 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4208 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4209 (unsigned long long)blkno);
4211 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
4217 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4218 OCFS2_JOURNAL_ACCESS_CREATE);
4224 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4225 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4227 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4229 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4230 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4231 offsetof(struct ocfs2_xattr_block, xb_attrs));
4233 xr = &xb->xb_attrs.xb_root;
4234 xr->xt_clusters = cpu_to_le32(1);
4235 xr->xt_last_eb_blk = 0;
4236 xr->xt_list.l_tree_depth = 0;
4237 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4238 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4240 xr->xt_list.l_recs[0].e_cpos = 0;
4241 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4242 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4244 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4246 ocfs2_journal_dirty(handle, xb_bh);
4249 up_write(&oi->ip_alloc_sem);
4254 static int cmp_xe_offset(const void *a, const void *b)
4256 const struct ocfs2_xattr_entry *l = a, *r = b;
4257 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4258 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4260 if (l_name_offset < r_name_offset)
4262 if (l_name_offset > r_name_offset)
4268 * defrag a xattr bucket if we find that the bucket has some
4269 * holes beteen name/value pairs.
4270 * We will move all the name/value pairs to the end of the bucket
4271 * so that we can spare some space for insertion.
4273 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
4275 struct ocfs2_xattr_bucket *bucket)
4278 size_t end, offset, len;
4279 struct ocfs2_xattr_header *xh;
4280 char *entries, *buf, *bucket_buf = NULL;
4281 u64 blkno = bucket_blkno(bucket);
4283 size_t blocksize = inode->i_sb->s_blocksize;
4284 struct ocfs2_xattr_entry *xe;
4287 * In order to make the operation more efficient and generic,
4288 * we copy all the blocks into a contiguous memory and do the
4289 * defragment there, so if anything is error, we will not touch
4292 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4299 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4300 memcpy(buf, bucket_block(bucket, i), blocksize);
4302 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4303 OCFS2_JOURNAL_ACCESS_WRITE);
4309 xh = (struct ocfs2_xattr_header *)bucket_buf;
4310 entries = (char *)xh->xh_entries;
4311 xh_free_start = le16_to_cpu(xh->xh_free_start);
4313 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4314 "xh_free_start = %u, xh_name_value_len = %u.\n",
4315 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4316 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4319 * sort all the entries by their offset.
4320 * the largest will be the first, so that we can
4321 * move them to the end one by one.
4323 sort(entries, le16_to_cpu(xh->xh_count),
4324 sizeof(struct ocfs2_xattr_entry),
4325 cmp_xe_offset, swap_xe);
4327 /* Move all name/values to the end of the bucket. */
4328 xe = xh->xh_entries;
4329 end = OCFS2_XATTR_BUCKET_SIZE;
4330 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4331 offset = le16_to_cpu(xe->xe_name_offset);
4332 len = namevalue_size_xe(xe);
4335 * We must make sure that the name/value pair
4336 * exist in the same block. So adjust end to
4337 * the previous block end if needed.
4339 if (((end - len) / blocksize !=
4340 (end - 1) / blocksize))
4341 end = end - end % blocksize;
4343 if (end > offset + len) {
4344 memmove(bucket_buf + end - len,
4345 bucket_buf + offset, len);
4346 xe->xe_name_offset = cpu_to_le16(end - len);
4349 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4350 "bucket %llu\n", (unsigned long long)blkno);
4355 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4356 "bucket %llu\n", (unsigned long long)blkno);
4358 if (xh_free_start == end)
4361 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4362 xh->xh_free_start = cpu_to_le16(end);
4364 /* sort the entries by their name_hash. */
4365 sort(entries, le16_to_cpu(xh->xh_count),
4366 sizeof(struct ocfs2_xattr_entry),
4370 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4371 memcpy(bucket_block(bucket, i), buf, blocksize);
4372 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4380 * prev_blkno points to the start of an existing extent. new_blkno
4381 * points to a newly allocated extent. Because we know each of our
4382 * clusters contains more than bucket, we can easily split one cluster
4383 * at a bucket boundary. So we take the last cluster of the existing
4384 * extent and split it down the middle. We move the last half of the
4385 * buckets in the last cluster of the existing extent over to the new
4388 * first_bh is the buffer at prev_blkno so we can update the existing
4389 * extent's bucket count. header_bh is the bucket were we were hoping
4390 * to insert our xattr. If the bucket move places the target in the new
4391 * extent, we'll update first_bh and header_bh after modifying the old
4394 * first_hash will be set as the 1st xe's name_hash in the new extent.
4396 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4398 struct ocfs2_xattr_bucket *first,
4399 struct ocfs2_xattr_bucket *target,
4405 struct super_block *sb = inode->i_sb;
4406 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4407 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4408 int to_move = num_buckets / 2;
4410 u64 last_cluster_blkno = bucket_blkno(first) +
4411 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4413 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4414 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4416 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4417 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
4419 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4420 last_cluster_blkno, new_blkno,
4421 to_move, first_hash);
4427 /* This is the first bucket that got moved */
4428 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4431 * If the target bucket was part of the moved buckets, we need to
4432 * update first and target.
4434 if (bucket_blkno(target) >= src_blkno) {
4435 /* Find the block for the new target bucket */
4436 src_blkno = new_blkno +
4437 (bucket_blkno(target) - src_blkno);
4439 ocfs2_xattr_bucket_relse(first);
4440 ocfs2_xattr_bucket_relse(target);
4443 * These shouldn't fail - the buffers are in the
4444 * journal from ocfs2_cp_xattr_bucket().
4446 ret = ocfs2_read_xattr_bucket(first, new_blkno);
4451 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4462 * Find the suitable pos when we divide a bucket into 2.
4463 * We have to make sure the xattrs with the same hash value exist
4464 * in the same bucket.
4466 * If this ocfs2_xattr_header covers more than one hash value, find a
4467 * place where the hash value changes. Try to find the most even split.
4468 * The most common case is that all entries have different hash values,
4469 * and the first check we make will find a place to split.
4471 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4473 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4474 int count = le16_to_cpu(xh->xh_count);
4475 int delta, middle = count / 2;
4478 * We start at the middle. Each step gets farther away in both
4479 * directions. We therefore hit the change in hash value
4480 * nearest to the middle. Note that this loop does not execute for
4483 for (delta = 0; delta < middle; delta++) {
4484 /* Let's check delta earlier than middle */
4485 if (cmp_xe(&entries[middle - delta - 1],
4486 &entries[middle - delta]))
4487 return middle - delta;
4489 /* For even counts, don't walk off the end */
4490 if ((middle + delta + 1) == count)
4493 /* Now try delta past middle */
4494 if (cmp_xe(&entries[middle + delta],
4495 &entries[middle + delta + 1]))
4496 return middle + delta + 1;
4499 /* Every entry had the same hash */
4504 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4505 * first_hash will record the 1st hash of the new bucket.
4507 * Normally half of the xattrs will be moved. But we have to make
4508 * sure that the xattrs with the same hash value are stored in the
4509 * same bucket. If all the xattrs in this bucket have the same hash
4510 * value, the new bucket will be initialized as an empty one and the
4511 * first_hash will be initialized as (hash_value+1).
4513 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4518 int new_bucket_head)
4521 int count, start, len, name_value_len = 0, name_offset = 0;
4522 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4523 struct ocfs2_xattr_header *xh;
4524 struct ocfs2_xattr_entry *xe;
4525 int blocksize = inode->i_sb->s_blocksize;
4527 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4528 (unsigned long long)blk, (unsigned long long)new_blk);
4530 s_bucket = ocfs2_xattr_bucket_new(inode);
4531 t_bucket = ocfs2_xattr_bucket_new(inode);
4532 if (!s_bucket || !t_bucket) {
4538 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4544 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4545 OCFS2_JOURNAL_ACCESS_WRITE);
4552 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4553 * there's no need to read it.
4555 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4562 * Hey, if we're overwriting t_bucket, what difference does
4563 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4564 * same part of ocfs2_cp_xattr_bucket().
4566 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4568 OCFS2_JOURNAL_ACCESS_CREATE :
4569 OCFS2_JOURNAL_ACCESS_WRITE);
4575 xh = bucket_xh(s_bucket);
4576 count = le16_to_cpu(xh->xh_count);
4577 start = ocfs2_xattr_find_divide_pos(xh);
4579 if (start == count) {
4580 xe = &xh->xh_entries[start-1];
4583 * initialized a new empty bucket here.
4584 * The hash value is set as one larger than
4585 * that of the last entry in the previous bucket.
4587 for (i = 0; i < t_bucket->bu_blocks; i++)
4588 memset(bucket_block(t_bucket, i), 0, blocksize);
4590 xh = bucket_xh(t_bucket);
4591 xh->xh_free_start = cpu_to_le16(blocksize);
4592 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4593 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4595 goto set_num_buckets;
4598 /* copy the whole bucket to the new first. */
4599 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4601 /* update the new bucket. */
4602 xh = bucket_xh(t_bucket);
4605 * Calculate the total name/value len and xh_free_start for
4606 * the old bucket first.
4608 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4610 for (i = 0; i < start; i++) {
4611 xe = &xh->xh_entries[i];
4612 name_value_len += namevalue_size_xe(xe);
4613 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4614 name_offset = le16_to_cpu(xe->xe_name_offset);
4618 * Now begin the modification to the new bucket.
4620 * In the new bucket, We just move the xattr entry to the beginning
4621 * and don't touch the name/value. So there will be some holes in the
4622 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4625 xe = &xh->xh_entries[start];
4626 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4627 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
4628 (int)((char *)xe - (char *)xh),
4629 (int)((char *)xh->xh_entries - (char *)xh));
4630 memmove((char *)xh->xh_entries, (char *)xe, len);
4631 xe = &xh->xh_entries[count - start];
4632 len = sizeof(struct ocfs2_xattr_entry) * start;
4633 memset((char *)xe, 0, len);
4635 le16_add_cpu(&xh->xh_count, -start);
4636 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4638 /* Calculate xh_free_start for the new bucket. */
4639 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4640 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4641 xe = &xh->xh_entries[i];
4642 if (le16_to_cpu(xe->xe_name_offset) <
4643 le16_to_cpu(xh->xh_free_start))
4644 xh->xh_free_start = xe->xe_name_offset;
4648 /* set xh->xh_num_buckets for the new xh. */
4649 if (new_bucket_head)
4650 xh->xh_num_buckets = cpu_to_le16(1);
4652 xh->xh_num_buckets = 0;
4654 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4656 /* store the first_hash of the new bucket. */
4658 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4661 * Now only update the 1st block of the old bucket. If we
4662 * just added a new empty bucket, there is no need to modify
4668 xh = bucket_xh(s_bucket);
4669 memset(&xh->xh_entries[start], 0,
4670 sizeof(struct ocfs2_xattr_entry) * (count - start));
4671 xh->xh_count = cpu_to_le16(start);
4672 xh->xh_free_start = cpu_to_le16(name_offset);
4673 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4675 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4678 ocfs2_xattr_bucket_free(s_bucket);
4679 ocfs2_xattr_bucket_free(t_bucket);
4685 * Copy xattr from one bucket to another bucket.
4687 * The caller must make sure that the journal transaction
4688 * has enough space for journaling.
4690 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4697 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4699 BUG_ON(s_blkno == t_blkno);
4701 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4702 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4705 s_bucket = ocfs2_xattr_bucket_new(inode);
4706 t_bucket = ocfs2_xattr_bucket_new(inode);
4707 if (!s_bucket || !t_bucket) {
4713 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4718 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4719 * there's no need to read it.
4721 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4726 * Hey, if we're overwriting t_bucket, what difference does
4727 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4728 * cluster to fill, we came here from
4729 * ocfs2_mv_xattr_buckets(), and it is really new -
4730 * ACCESS_CREATE is required. But we also might have moved data
4731 * out of t_bucket before extending back into it.
4732 * ocfs2_add_new_xattr_bucket() can do this - its call to
4733 * ocfs2_add_new_xattr_cluster() may have created a new extent
4734 * and copied out the end of the old extent. Then it re-extends
4735 * the old extent back to create space for new xattrs. That's
4736 * how we get here, and the bucket isn't really new.
4738 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4740 OCFS2_JOURNAL_ACCESS_CREATE :
4741 OCFS2_JOURNAL_ACCESS_WRITE);
4745 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4746 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4749 ocfs2_xattr_bucket_free(t_bucket);
4750 ocfs2_xattr_bucket_free(s_bucket);
4756 * src_blk points to the start of an existing extent. last_blk points to
4757 * last cluster in that extent. to_blk points to a newly allocated
4758 * extent. We copy the buckets from the cluster at last_blk to the new
4759 * extent. If start_bucket is non-zero, we skip that many buckets before
4760 * we start copying. The new extent's xh_num_buckets gets set to the
4761 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4762 * by the same amount.
4764 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4765 u64 src_blk, u64 last_blk, u64 to_blk,
4766 unsigned int start_bucket,
4769 int i, ret, credits;
4770 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4771 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4772 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4773 struct ocfs2_xattr_bucket *old_first, *new_first;
4775 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4776 (unsigned long long)last_blk, (unsigned long long)to_blk);
4778 BUG_ON(start_bucket >= num_buckets);
4780 num_buckets -= start_bucket;
4781 last_blk += (start_bucket * blks_per_bucket);
4784 /* The first bucket of the original extent */
4785 old_first = ocfs2_xattr_bucket_new(inode);
4786 /* The first bucket of the new extent */
4787 new_first = ocfs2_xattr_bucket_new(inode);
4788 if (!old_first || !new_first) {
4794 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4801 * We need to update the first bucket of the old extent and all
4802 * the buckets going to the new extent.
4804 credits = ((num_buckets + 1) * blks_per_bucket) +
4805 handle->h_buffer_credits;
4806 ret = ocfs2_extend_trans(handle, credits);
4812 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4813 OCFS2_JOURNAL_ACCESS_WRITE);
4819 for (i = 0; i < num_buckets; i++) {
4820 ret = ocfs2_cp_xattr_bucket(inode, handle,
4821 last_blk + (i * blks_per_bucket),
4822 to_blk + (i * blks_per_bucket),
4831 * Get the new bucket ready before we dirty anything
4832 * (This actually shouldn't fail, because we already dirtied
4833 * it once in ocfs2_cp_xattr_bucket()).
4835 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4840 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4841 OCFS2_JOURNAL_ACCESS_WRITE);
4847 /* Now update the headers */
4848 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4849 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4851 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4852 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4855 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4858 ocfs2_xattr_bucket_free(new_first);
4859 ocfs2_xattr_bucket_free(old_first);
4864 * Move some xattrs in this cluster to the new cluster.
4865 * This function should only be called when bucket size == cluster size.
4866 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4868 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4874 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4875 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
4877 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4879 ret = ocfs2_extend_trans(handle, credits);
4885 /* Move half of the xattr in start_blk to the next bucket. */
4886 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4887 new_blk, first_hash, 1);
4891 * Move some xattrs from the old cluster to the new one since they are not
4892 * contiguous in ocfs2 xattr tree.
4894 * new_blk starts a new separate cluster, and we will move some xattrs from
4895 * prev_blk to it. v_start will be set as the first name hash value in this
4896 * new cluster so that it can be used as e_cpos during tree insertion and
4897 * don't collide with our original b-tree operations. first_bh and header_bh
4898 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4899 * to extend the insert bucket.
4901 * The problem is how much xattr should we move to the new one and when should
4902 * we update first_bh and header_bh?
4903 * 1. If cluster size > bucket size, that means the previous cluster has more
4904 * than 1 bucket, so just move half nums of bucket into the new cluster and
4905 * update the first_bh and header_bh if the insert bucket has been moved
4906 * to the new cluster.
4907 * 2. If cluster_size == bucket_size:
4908 * a) If the previous extent rec has more than one cluster and the insert
4909 * place isn't in the last cluster, copy the entire last cluster to the
4910 * new one. This time, we don't need to upate the first_bh and header_bh
4911 * since they will not be moved into the new cluster.
4912 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4913 * the new one. And we set the extend flag to zero if the insert place is
4914 * moved into the new allocated cluster since no extend is needed.
4916 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4918 struct ocfs2_xattr_bucket *first,
4919 struct ocfs2_xattr_bucket *target,
4927 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4928 (unsigned long long)bucket_blkno(first), prev_clusters,
4929 (unsigned long long)new_blk);
4931 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
4932 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4941 /* The start of the last cluster in the first extent */
4942 u64 last_blk = bucket_blkno(first) +
4943 ((prev_clusters - 1) *
4944 ocfs2_clusters_to_blocks(inode->i_sb, 1));
4946 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
4947 ret = ocfs2_mv_xattr_buckets(inode, handle,
4948 bucket_blkno(first),
4949 last_blk, new_blk, 0,
4954 ret = ocfs2_divide_xattr_cluster(inode, handle,
4960 if ((bucket_blkno(target) == last_blk) && extend)
4969 * Add a new cluster for xattr storage.
4971 * If the new cluster is contiguous with the previous one, it will be
4972 * appended to the same extent record, and num_clusters will be updated.
4973 * If not, we will insert a new extent for it and move some xattrs in
4974 * the last cluster into the new allocated one.
4975 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4976 * lose the benefits of hashing because we'll have to search large leaves.
4977 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4980 * first_bh is the first block of the previous extent rec and header_bh
4981 * indicates the bucket we will insert the new xattrs. They will be updated
4982 * when the header_bh is moved into the new cluster.
4984 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4985 struct buffer_head *root_bh,
4986 struct ocfs2_xattr_bucket *first,
4987 struct ocfs2_xattr_bucket *target,
4991 struct ocfs2_xattr_set_ctxt *ctxt)
4994 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4995 u32 prev_clusters = *num_clusters;
4996 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4998 handle_t *handle = ctxt->handle;
4999 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5000 struct ocfs2_extent_tree et;
5002 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5003 "previous xattr blkno = %llu\n",
5004 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5005 prev_cpos, (unsigned long long)bucket_blkno(first));
5007 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5009 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5010 OCFS2_JOURNAL_ACCESS_WRITE);
5016 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
5017 clusters_to_add, &bit_off, &num_bits);
5024 BUG_ON(num_bits > clusters_to_add);
5026 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5027 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5028 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5030 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
5031 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5032 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5034 * If this cluster is contiguous with the old one and
5035 * adding this new cluster, we don't surpass the limit of
5036 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5037 * initialized and used like other buckets in the previous
5039 * So add it as a contiguous one. The caller will handle
5042 v_start = prev_cpos + prev_clusters;
5043 *num_clusters = prev_clusters + num_bits;
5044 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5047 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5061 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
5062 num_bits, (unsigned long long)block, v_start);
5063 ret = ocfs2_insert_extent(handle, &et, v_start, block,
5064 num_bits, 0, ctxt->meta_ac);
5070 ret = ocfs2_journal_dirty(handle, root_bh);
5079 * We are given an extent. 'first' is the bucket at the very front of
5080 * the extent. The extent has space for an additional bucket past
5081 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5082 * of the target bucket. We wish to shift every bucket past the target
5083 * down one, filling in that additional space. When we get back to the
5084 * target, we split the target between itself and the now-empty bucket
5085 * at target+1 (aka, target_blkno + blks_per_bucket).
5087 static int ocfs2_extend_xattr_bucket(struct inode *inode,
5089 struct ocfs2_xattr_bucket *first,
5094 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5095 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5097 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
5099 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
5100 "from %llu, len = %u\n", (unsigned long long)target_blk,
5101 (unsigned long long)bucket_blkno(first), num_clusters);
5103 /* The extent must have room for an additional bucket */
5104 BUG_ON(new_bucket >=
5105 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
5107 /* end_blk points to the last existing bucket */
5108 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
5111 * end_blk is the start of the last existing bucket.
5112 * Thus, (end_blk - target_blk) covers the target bucket and
5113 * every bucket after it up to, but not including, the last
5114 * existing bucket. Then we add the last existing bucket, the
5115 * new bucket, and the first bucket (3 * blk_per_bucket).
5117 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
5118 handle->h_buffer_credits;
5119 ret = ocfs2_extend_trans(handle, credits);
5125 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5126 OCFS2_JOURNAL_ACCESS_WRITE);
5132 while (end_blk != target_blk) {
5133 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5134 end_blk + blk_per_bucket, 0);
5137 end_blk -= blk_per_bucket;
5140 /* Move half of the xattr in target_blkno to the next bucket. */
5141 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5142 target_blk + blk_per_bucket, NULL, 0);
5144 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5145 ocfs2_xattr_bucket_journal_dirty(handle, first);
5152 * Add new xattr bucket in an extent record and adjust the buckets
5153 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5154 * bucket we want to insert into.
5156 * In the easy case, we will move all the buckets after target down by
5157 * one. Half of target's xattrs will be moved to the next bucket.
5159 * If current cluster is full, we'll allocate a new one. This may not
5160 * be contiguous. The underlying calls will make sure that there is
5161 * space for the insert, shifting buckets around if necessary.
5162 * 'target' may be moved by those calls.
5164 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5165 struct buffer_head *xb_bh,
5166 struct ocfs2_xattr_bucket *target,
5167 struct ocfs2_xattr_set_ctxt *ctxt)
5169 struct ocfs2_xattr_block *xb =
5170 (struct ocfs2_xattr_block *)xb_bh->b_data;
5171 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5172 struct ocfs2_extent_list *el = &xb_root->xt_list;
5174 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
5175 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5176 int ret, num_buckets, extend = 1;
5178 u32 e_cpos, num_clusters;
5179 /* The bucket at the front of the extent */
5180 struct ocfs2_xattr_bucket *first;
5182 mlog(0, "Add new xattr bucket starting from %llu\n",
5183 (unsigned long long)bucket_blkno(target));
5185 /* The first bucket of the original extent */
5186 first = ocfs2_xattr_bucket_new(inode);
5193 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5200 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5206 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5207 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5209 * This can move first+target if the target bucket moves
5210 * to the new extent.
5212 ret = ocfs2_add_new_xattr_cluster(inode,
5227 ret = ocfs2_extend_xattr_bucket(inode,
5230 bucket_blkno(target),
5237 ocfs2_xattr_bucket_free(first);
5242 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5243 struct ocfs2_xattr_bucket *bucket,
5246 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5248 offs = offs % inode->i_sb->s_blocksize;
5249 return bucket_block(bucket, block_off) + offs;
5253 * Truncate the specified xe_off entry in xattr bucket.
5254 * bucket is indicated by header_bh and len is the new length.
5255 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5257 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5259 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5260 struct ocfs2_xattr_bucket *bucket,
5263 struct ocfs2_xattr_set_ctxt *ctxt)
5267 struct ocfs2_xattr_entry *xe;
5268 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5269 size_t blocksize = inode->i_sb->s_blocksize;
5270 struct ocfs2_xattr_value_buf vb = {
5271 .vb_access = ocfs2_journal_access,
5274 xe = &xh->xh_entries[xe_off];
5276 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5278 offset = le16_to_cpu(xe->xe_name_offset) +
5279 OCFS2_XATTR_SIZE(xe->xe_name_len);
5281 value_blk = offset / blocksize;
5283 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5284 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
5286 vb.vb_bh = bucket->bu_bhs[value_blk];
5289 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5290 (vb.vb_bh->b_data + offset % blocksize);
5293 * From here on out we have to dirty the bucket. The generic
5294 * value calls only modify one of the bucket's bhs, but we need
5295 * to send the bucket at once. So if they error, they *could* have
5296 * modified something. We have to assume they did, and dirty
5297 * the whole bucket. This leaves us in a consistent state.
5299 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5300 xe_off, (unsigned long long)bucket_blkno(bucket), len);
5301 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5307 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5308 OCFS2_JOURNAL_ACCESS_WRITE);
5314 xe->xe_value_size = cpu_to_le64(len);
5316 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5322 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5323 struct buffer_head *root_bh,
5330 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5331 struct inode *tl_inode = osb->osb_tl_inode;
5333 struct ocfs2_xattr_block *xb =
5334 (struct ocfs2_xattr_block *)root_bh->b_data;
5335 struct ocfs2_alloc_context *meta_ac = NULL;
5336 struct ocfs2_cached_dealloc_ctxt dealloc;
5337 struct ocfs2_extent_tree et;
5339 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5340 ocfs2_delete_xattr_in_bucket, para);
5346 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5348 ocfs2_init_dealloc_ctxt(&dealloc);
5350 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5351 cpos, len, (unsigned long long)blkno);
5353 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5356 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5362 mutex_lock(&tl_inode->i_mutex);
5364 if (ocfs2_truncate_log_needs_flush(osb)) {
5365 ret = __ocfs2_flush_truncate_log(osb);
5372 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5373 if (IS_ERR(handle)) {
5379 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5380 OCFS2_JOURNAL_ACCESS_WRITE);
5386 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5393 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5395 ret = ocfs2_journal_dirty(handle, root_bh);
5401 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5406 ocfs2_commit_trans(osb, handle);
5408 ocfs2_schedule_truncate_log_flush(osb, 1);
5410 mutex_unlock(&tl_inode->i_mutex);
5413 ocfs2_free_alloc_context(meta_ac);
5415 ocfs2_run_deallocs(osb, &dealloc);
5421 * Set the xattr name/value in the bucket specified in xs.
5423 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5424 struct ocfs2_xattr_info *xi,
5425 struct ocfs2_xattr_search *xs,
5426 struct ocfs2_xattr_set_ctxt *ctxt)
5430 struct ocfs2_xa_loc loc;
5432 if (!xs->bucket->bu_bhs[1]) {
5433 blkno = bucket_blkno(xs->bucket);
5434 ocfs2_xattr_bucket_relse(xs->bucket);
5435 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
5442 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5443 xs->not_found ? NULL : xs->here);
5444 ret = ocfs2_xa_set(&loc, xi, ctxt);
5450 xs->here = loc.xl_entry;
5457 * check whether the xattr bucket is filled up with the same hash value.
5458 * If we want to insert the xattr with the same hash, return -ENOSPC.
5459 * If we want to insert a xattr with different hash value, go ahead
5460 * and ocfs2_divide_xattr_bucket will handle this.
5462 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5463 struct ocfs2_xattr_bucket *bucket,
5466 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5467 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5469 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5472 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5473 xh->xh_entries[0].xe_name_hash) {
5474 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5476 (unsigned long long)bucket_blkno(bucket),
5477 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5484 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5485 struct ocfs2_xattr_info *xi,
5486 struct ocfs2_xattr_search *xs,
5487 struct ocfs2_xattr_set_ctxt *ctxt)
5489 struct ocfs2_xattr_header *xh;
5490 struct ocfs2_xattr_entry *xe;
5491 u16 count, header_size, xh_free_start;
5492 int free, max_free, need, old;
5493 size_t value_size = 0;
5494 size_t blocksize = inode->i_sb->s_blocksize;
5495 int ret, allocation = 0;
5497 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
5501 count = le16_to_cpu(xh->xh_count);
5502 xh_free_start = le16_to_cpu(xh->xh_free_start);
5503 header_size = sizeof(struct ocfs2_xattr_header) +
5504 count * sizeof(struct ocfs2_xattr_entry);
5505 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5506 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
5508 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5509 "of %u which exceed block size\n",
5510 (unsigned long long)bucket_blkno(xs->bucket),
5513 if (xi->xi_value && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
5514 value_size = OCFS2_XATTR_ROOT_SIZE;
5515 else if (xi->xi_value)
5516 value_size = OCFS2_XATTR_SIZE(xi->xi_value_len);
5519 need = sizeof(struct ocfs2_xattr_entry) +
5520 OCFS2_XATTR_SIZE(xi->xi_name_len) + value_size;
5522 need = value_size + OCFS2_XATTR_SIZE(xi->xi_name_len);
5525 * We only replace the old value if the new length is smaller
5526 * than the old one. Otherwise we will allocate new space in the
5527 * bucket to store it.
5530 if (ocfs2_xattr_is_local(xe))
5531 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5533 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5535 if (old >= value_size)
5539 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
5541 * We need to make sure the new name/value pair
5542 * can exist in the same block.
5544 if (xh_free_start % blocksize < need)
5545 free -= xh_free_start % blocksize;
5547 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5548 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5549 " %u\n", xs->not_found,
5550 (unsigned long long)bucket_blkno(xs->bucket),
5551 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5552 le16_to_cpu(xh->xh_name_value_len));
5556 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
5557 if (need <= max_free &&
5558 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5560 * We can create the space by defragment. Since only the
5561 * name/value will be moved, the xe shouldn't be changed
5564 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5571 xh_free_start = le16_to_cpu(xh->xh_free_start);
5572 free = xh_free_start - header_size
5573 - OCFS2_XATTR_HEADER_GAP;
5574 if (xh_free_start % blocksize < need)
5575 free -= xh_free_start % blocksize;
5580 mlog(0, "Can't get enough space for xattr insert by "
5581 "defragment. Need %u bytes, but we have %d, so "
5582 "allocate new bucket for it.\n", need, free);
5586 * We have to add new buckets or clusters and one
5587 * allocation should leave us enough space for insert.
5592 * We do not allow for overlapping ranges between buckets. And
5593 * the maximum number of collisions we will allow for then is
5594 * one bucket's worth, so check it here whether we need to
5595 * add a new bucket for the insert.
5597 ret = ocfs2_check_xattr_bucket_collision(inode,
5605 ret = ocfs2_add_new_xattr_bucket(inode,
5615 * ocfs2_add_new_xattr_bucket() will have updated
5616 * xs->bucket if it moved, but it will not have updated
5617 * any of the other search fields. Thus, we drop it and
5618 * re-search. Everything should be cached, so it'll be
5621 ocfs2_xattr_bucket_relse(xs->bucket);
5622 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5625 if (ret && ret != -ENODATA)
5627 xs->not_found = ret;
5633 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
5639 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5640 struct ocfs2_xattr_bucket *bucket,
5643 int ret = 0, ref_credits;
5644 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5646 struct ocfs2_xattr_entry *xe;
5647 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5648 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5649 int credits = ocfs2_remove_extent_credits(osb->sb) +
5650 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5651 struct ocfs2_xattr_value_root *xv;
5652 struct ocfs2_rm_xattr_bucket_para *args =
5653 (struct ocfs2_rm_xattr_bucket_para *)para;
5655 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5657 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5658 xe = &xh->xh_entries[i];
5659 if (ocfs2_xattr_is_local(xe))
5662 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5665 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5671 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5672 if (IS_ERR(ctxt.handle)) {
5673 ret = PTR_ERR(ctxt.handle);
5678 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5681 ocfs2_commit_trans(osb, ctxt.handle);
5683 ocfs2_free_alloc_context(ctxt.meta_ac);
5684 ctxt.meta_ac = NULL;
5693 ocfs2_free_alloc_context(ctxt.meta_ac);
5694 ocfs2_schedule_truncate_log_flush(osb, 1);
5695 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5700 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5701 * or change the extent record flag), we need to recalculate
5702 * the metaecc for the whole bucket. So it is done here.
5705 * We have to give the extra credits for the caller.
5707 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5712 struct ocfs2_xattr_bucket *bucket =
5713 (struct ocfs2_xattr_bucket *)para;
5715 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5716 OCFS2_JOURNAL_ACCESS_WRITE);
5722 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5728 * Special action we need if the xattr value is refcounted.
5730 * 1. If the xattr is refcounted, lock the tree.
5731 * 2. CoW the xattr if we are setting the new value and the value
5732 * will be stored outside.
5733 * 3. In other case, decrease_refcount will work for us, so just
5734 * lock the refcount tree, calculate the meta and credits is OK.
5736 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5737 * currently CoW is a completed transaction, while this function
5738 * will also lock the allocators and let us deadlock. So we will
5739 * CoW the whole xattr value.
5741 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5742 struct ocfs2_dinode *di,
5743 struct ocfs2_xattr_info *xi,
5744 struct ocfs2_xattr_search *xis,
5745 struct ocfs2_xattr_search *xbs,
5746 struct ocfs2_refcount_tree **ref_tree,
5751 struct ocfs2_xattr_block *xb;
5752 struct ocfs2_xattr_entry *xe;
5754 u32 p_cluster, num_clusters;
5755 unsigned int ext_flags;
5756 int name_offset, name_len;
5757 struct ocfs2_xattr_value_buf vb;
5758 struct ocfs2_xattr_bucket *bucket = NULL;
5759 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5760 struct ocfs2_post_refcount refcount;
5761 struct ocfs2_post_refcount *p = NULL;
5762 struct buffer_head *ref_root_bh = NULL;
5764 if (!xis->not_found) {
5766 name_offset = le16_to_cpu(xe->xe_name_offset);
5767 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5769 vb.vb_bh = xis->inode_bh;
5770 vb.vb_access = ocfs2_journal_access_di;
5772 int i, block_off = 0;
5773 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5775 name_offset = le16_to_cpu(xe->xe_name_offset);
5776 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5777 i = xbs->here - xbs->header->xh_entries;
5779 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5780 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5781 bucket_xh(xbs->bucket),
5788 base = bucket_block(xbs->bucket, block_off);
5789 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5790 vb.vb_access = ocfs2_journal_access;
5792 if (ocfs2_meta_ecc(osb)) {
5793 /*create parameters for ocfs2_post_refcount. */
5794 bucket = xbs->bucket;
5795 refcount.credits = bucket->bu_blocks;
5796 refcount.para = bucket;
5798 ocfs2_xattr_bucket_post_refcount;
5803 vb.vb_bh = xbs->xattr_bh;
5804 vb.vb_access = ocfs2_journal_access_xb;
5808 if (ocfs2_xattr_is_local(xe))
5811 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5812 (base + name_offset + name_len);
5814 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5815 &num_clusters, &vb.vb_xv->xr_list,
5823 * We just need to check the 1st extent record, since we always
5824 * CoW the whole xattr. So there shouldn't be a xattr with
5825 * some REFCOUNT extent recs after the 1st one.
5827 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5830 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5831 1, ref_tree, &ref_root_bh);
5838 * If we are deleting the xattr or the new size will be stored inside,
5839 * cool, leave it there, the xattr truncate process will remove them
5840 * for us(it still needs the refcount tree lock and the meta, credits).
5841 * And the worse case is that every cluster truncate will split the
5842 * refcount tree, and make the original extent become 3. So we will need
5843 * 2 * cluster more extent recs at most.
5845 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
5847 ret = ocfs2_refcounted_xattr_delete_need(inode,
5848 &(*ref_tree)->rf_ci,
5849 ref_root_bh, vb.vb_xv,
5856 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5857 *ref_tree, ref_root_bh, 0,
5858 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5863 brelse(ref_root_bh);
5868 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5869 * The physical clusters will be added to refcount tree.
5871 static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5872 struct ocfs2_xattr_value_root *xv,
5873 struct ocfs2_extent_tree *value_et,
5874 struct ocfs2_caching_info *ref_ci,
5875 struct buffer_head *ref_root_bh,
5876 struct ocfs2_cached_dealloc_ctxt *dealloc,
5877 struct ocfs2_post_refcount *refcount)
5880 u32 clusters = le32_to_cpu(xv->xr_clusters);
5881 u32 cpos, p_cluster, num_clusters;
5882 struct ocfs2_extent_list *el = &xv->xr_list;
5883 unsigned int ext_flags;
5886 while (cpos < clusters) {
5887 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5888 &num_clusters, el, &ext_flags);
5890 cpos += num_clusters;
5891 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5896 ret = ocfs2_add_refcount_flag(inode, value_et,
5897 ref_ci, ref_root_bh,
5898 cpos - num_clusters,
5899 p_cluster, num_clusters,
5911 * Given a normal ocfs2_xattr_header, refcount all the entries which
5912 * have value stored outside.
5913 * Used for xattrs stored in inode and ocfs2_xattr_block.
5915 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5916 struct ocfs2_xattr_value_buf *vb,
5917 struct ocfs2_xattr_header *header,
5918 struct ocfs2_caching_info *ref_ci,
5919 struct buffer_head *ref_root_bh,
5920 struct ocfs2_cached_dealloc_ctxt *dealloc)
5923 struct ocfs2_xattr_entry *xe;
5924 struct ocfs2_xattr_value_root *xv;
5925 struct ocfs2_extent_tree et;
5928 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5929 xe = &header->xh_entries[i];
5931 if (ocfs2_xattr_is_local(xe))
5934 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5935 le16_to_cpu(xe->xe_name_offset) +
5936 OCFS2_XATTR_SIZE(xe->xe_name_len));
5939 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5941 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5942 ref_ci, ref_root_bh,
5953 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5954 struct buffer_head *fe_bh,
5955 struct ocfs2_caching_info *ref_ci,
5956 struct buffer_head *ref_root_bh,
5957 struct ocfs2_cached_dealloc_ctxt *dealloc)
5959 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5960 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5961 (fe_bh->b_data + inode->i_sb->s_blocksize -
5962 le16_to_cpu(di->i_xattr_inline_size));
5963 struct ocfs2_xattr_value_buf vb = {
5965 .vb_access = ocfs2_journal_access_di,
5968 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5969 ref_ci, ref_root_bh, dealloc);
5972 struct ocfs2_xattr_tree_value_refcount_para {
5973 struct ocfs2_caching_info *ref_ci;
5974 struct buffer_head *ref_root_bh;
5975 struct ocfs2_cached_dealloc_ctxt *dealloc;
5978 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5979 struct ocfs2_xattr_bucket *bucket,
5981 struct ocfs2_xattr_value_root **xv,
5982 struct buffer_head **bh)
5984 int ret, block_off, name_offset;
5985 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5986 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5989 ret = ocfs2_xattr_bucket_get_name_value(sb,
5999 base = bucket_block(bucket, block_off);
6001 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6002 OCFS2_XATTR_SIZE(xe->xe_name_len));
6005 *bh = bucket->bu_bhs[block_off];
6011 * For a given xattr bucket, refcount all the entries which
6012 * have value stored outside.
6014 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6015 struct ocfs2_xattr_bucket *bucket,
6019 struct ocfs2_extent_tree et;
6020 struct ocfs2_xattr_tree_value_refcount_para *ref =
6021 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6022 struct ocfs2_xattr_header *xh =
6023 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6024 struct ocfs2_xattr_entry *xe;
6025 struct ocfs2_xattr_value_buf vb = {
6026 .vb_access = ocfs2_journal_access,
6028 struct ocfs2_post_refcount refcount = {
6029 .credits = bucket->bu_blocks,
6031 .func = ocfs2_xattr_bucket_post_refcount,
6033 struct ocfs2_post_refcount *p = NULL;
6035 /* We only need post_refcount if we support metaecc. */
6036 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6039 mlog(0, "refcount bucket %llu, count = %u\n",
6040 (unsigned long long)bucket_blkno(bucket),
6041 le16_to_cpu(xh->xh_count));
6042 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6043 xe = &xh->xh_entries[i];
6045 if (ocfs2_xattr_is_local(xe))
6048 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6049 &vb.vb_xv, &vb.vb_bh);
6055 ocfs2_init_xattr_value_extent_tree(&et,
6056 INODE_CACHE(inode), &vb);
6058 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6072 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6073 struct buffer_head *root_bh,
6074 u64 blkno, u32 cpos, u32 len, void *para)
6076 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6077 ocfs2_xattr_bucket_value_refcount,
6081 static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6082 struct buffer_head *blk_bh,
6083 struct ocfs2_caching_info *ref_ci,
6084 struct buffer_head *ref_root_bh,
6085 struct ocfs2_cached_dealloc_ctxt *dealloc)
6088 struct ocfs2_xattr_block *xb =
6089 (struct ocfs2_xattr_block *)blk_bh->b_data;
6091 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6092 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6093 struct ocfs2_xattr_value_buf vb = {
6095 .vb_access = ocfs2_journal_access_xb,
6098 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6099 ref_ci, ref_root_bh,
6102 struct ocfs2_xattr_tree_value_refcount_para para = {
6104 .ref_root_bh = ref_root_bh,
6108 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6109 ocfs2_refcount_xattr_tree_rec,
6116 int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6117 struct buffer_head *fe_bh,
6118 struct ocfs2_caching_info *ref_ci,
6119 struct buffer_head *ref_root_bh,
6120 struct ocfs2_cached_dealloc_ctxt *dealloc)
6123 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6124 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6125 struct buffer_head *blk_bh = NULL;
6127 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6128 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6129 ref_ci, ref_root_bh,
6137 if (!di->i_xattr_loc)
6140 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6147 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6148 ref_root_bh, dealloc);
6158 typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
6160 * Store the information we need in xattr reflink.
6161 * old_bh and new_bh are inode bh for the old and new inode.
6163 struct ocfs2_xattr_reflink {
6164 struct inode *old_inode;
6165 struct inode *new_inode;
6166 struct buffer_head *old_bh;
6167 struct buffer_head *new_bh;
6168 struct ocfs2_caching_info *ref_ci;
6169 struct buffer_head *ref_root_bh;
6170 struct ocfs2_cached_dealloc_ctxt *dealloc;
6171 should_xattr_reflinked *xattr_reflinked;
6175 * Given a xattr header and xe offset,
6176 * return the proper xv and the corresponding bh.
6177 * xattr in inode, block and xattr tree have different implementaions.
6179 typedef int (get_xattr_value_root)(struct super_block *sb,
6180 struct buffer_head *bh,
6181 struct ocfs2_xattr_header *xh,
6183 struct ocfs2_xattr_value_root **xv,
6184 struct buffer_head **ret_bh,
6188 * Calculate all the xattr value root metadata stored in this xattr header and
6189 * credits we need if we create them from the scratch.
6190 * We use get_xattr_value_root so that all types of xattr container can use it.
6192 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6193 struct buffer_head *bh,
6194 struct ocfs2_xattr_header *xh,
6195 int *metas, int *credits,
6197 get_xattr_value_root *func,
6201 struct ocfs2_xattr_value_root *xv;
6202 struct ocfs2_xattr_entry *xe;
6204 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6205 xe = &xh->xh_entries[i];
6206 if (ocfs2_xattr_is_local(xe))
6209 ret = func(sb, bh, xh, i, &xv, NULL, para);
6215 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6216 le16_to_cpu(xv->xr_list.l_next_free_rec);
6218 *credits += ocfs2_calc_extend_credits(sb,
6220 le32_to_cpu(xv->xr_clusters));
6223 * If the value is a tree with depth > 1, We don't go deep
6224 * to the extent block, so just calculate a maximum record num.
6226 if (!xv->xr_list.l_tree_depth)
6227 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
6229 *num_recs += ocfs2_clusters_for_bytes(sb,
6236 /* Used by xattr inode and block to return the right xv and buffer_head. */
6237 static int ocfs2_get_xattr_value_root(struct super_block *sb,
6238 struct buffer_head *bh,
6239 struct ocfs2_xattr_header *xh,
6241 struct ocfs2_xattr_value_root **xv,
6242 struct buffer_head **ret_bh,
6245 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6247 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6248 le16_to_cpu(xe->xe_name_offset) +
6249 OCFS2_XATTR_SIZE(xe->xe_name_len));
6258 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6259 * It is only used for inline xattr and xattr block.
6261 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6262 struct ocfs2_xattr_header *xh,
6263 struct buffer_head *ref_root_bh,
6265 struct ocfs2_alloc_context **meta_ac)
6267 int ret, meta_add = 0, num_recs = 0;
6268 struct ocfs2_refcount_block *rb =
6269 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6273 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6274 &meta_add, credits, &num_recs,
6275 ocfs2_get_xattr_value_root,
6283 * We need to add/modify num_recs in refcount tree, so just calculate
6284 * an approximate number we need for refcount tree change.
6285 * Sometimes we need to split the tree, and after split, half recs
6286 * will be moved to the new block, and a new block can only provide
6287 * half number of recs. So we multiple new blocks by 2.
6289 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6290 meta_add += num_recs;
6291 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6292 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6293 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6294 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6298 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6307 * Given a xattr header, reflink all the xattrs in this container.
6308 * It can be used for inode, block and bucket.
6311 * Before we call this function, the caller has memcpy the xattr in
6312 * old_xh to the new_xh.
6314 * If args.xattr_reflinked is set, call it to decide whether the xe should
6315 * be reflinked or not. If not, remove it from the new xattr header.
6317 static int ocfs2_reflink_xattr_header(handle_t *handle,
6318 struct ocfs2_xattr_reflink *args,
6319 struct buffer_head *old_bh,
6320 struct ocfs2_xattr_header *xh,
6321 struct buffer_head *new_bh,
6322 struct ocfs2_xattr_header *new_xh,
6323 struct ocfs2_xattr_value_buf *vb,
6324 struct ocfs2_alloc_context *meta_ac,
6325 get_xattr_value_root *func,
6329 struct super_block *sb = args->old_inode->i_sb;
6330 struct buffer_head *value_bh;
6331 struct ocfs2_xattr_entry *xe, *last;
6332 struct ocfs2_xattr_value_root *xv, *new_xv;
6333 struct ocfs2_extent_tree data_et;
6334 u32 clusters, cpos, p_cluster, num_clusters;
6335 unsigned int ext_flags = 0;
6337 mlog(0, "reflink xattr in container %llu, count = %u\n",
6338 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
6340 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6341 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
6342 xe = &xh->xh_entries[i];
6344 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6345 xe = &new_xh->xh_entries[j];
6347 le16_add_cpu(&new_xh->xh_count, -1);
6348 if (new_xh->xh_count) {
6350 (void *)last - (void *)xe);
6352 sizeof(struct ocfs2_xattr_entry));
6356 * We don't want j to increase in the next round since
6357 * it is already moved ahead.
6363 if (ocfs2_xattr_is_local(xe))
6366 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6372 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6379 * For the xattr which has l_tree_depth = 0, all the extent
6380 * recs have already be copied to the new xh with the
6381 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6382 * increase the refount count int the refcount tree.
6384 * For the xattr which has l_tree_depth > 0, we need
6385 * to initialize it to the empty default value root,
6386 * and then insert the extents one by one.
6388 if (xv->xr_list.l_tree_depth) {
6389 memcpy(new_xv, &def_xv, sizeof(def_xv));
6391 vb->vb_bh = value_bh;
6392 ocfs2_init_xattr_value_extent_tree(&data_et,
6393 INODE_CACHE(args->new_inode), vb);
6396 clusters = le32_to_cpu(xv->xr_clusters);
6398 while (cpos < clusters) {
6399 ret = ocfs2_xattr_get_clusters(args->old_inode,
6412 if (xv->xr_list.l_tree_depth) {
6413 ret = ocfs2_insert_extent(handle,
6415 ocfs2_clusters_to_blocks(
6416 args->old_inode->i_sb,
6418 num_clusters, ext_flags,
6426 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6428 p_cluster, num_clusters,
6429 meta_ac, args->dealloc);
6435 cpos += num_clusters;
6443 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6445 int ret = 0, credits = 0;
6447 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6448 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6449 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6450 int header_off = osb->sb->s_blocksize - inline_size;
6451 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6452 (args->old_bh->b_data + header_off);
6453 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6454 (args->new_bh->b_data + header_off);
6455 struct ocfs2_alloc_context *meta_ac = NULL;
6456 struct ocfs2_inode_info *new_oi;
6457 struct ocfs2_dinode *new_di;
6458 struct ocfs2_xattr_value_buf vb = {
6459 .vb_bh = args->new_bh,
6460 .vb_access = ocfs2_journal_access_di,
6463 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6464 &credits, &meta_ac);
6470 handle = ocfs2_start_trans(osb, credits);
6471 if (IS_ERR(handle)) {
6472 ret = PTR_ERR(handle);
6477 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6478 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6484 memcpy(args->new_bh->b_data + header_off,
6485 args->old_bh->b_data + header_off, inline_size);
6487 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6488 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6490 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6491 args->new_bh, new_xh, &vb, meta_ac,
6492 ocfs2_get_xattr_value_root, NULL);
6498 new_oi = OCFS2_I(args->new_inode);
6499 spin_lock(&new_oi->ip_lock);
6500 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6501 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6502 spin_unlock(&new_oi->ip_lock);
6504 ocfs2_journal_dirty(handle, args->new_bh);
6507 ocfs2_commit_trans(osb, handle);
6511 ocfs2_free_alloc_context(meta_ac);
6515 static int ocfs2_create_empty_xattr_block(struct inode *inode,
6516 struct buffer_head *fe_bh,
6517 struct buffer_head **ret_bh,
6522 struct ocfs2_alloc_context *meta_ac;
6523 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6525 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
6531 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6532 if (IS_ERR(handle)) {
6533 ret = PTR_ERR(handle);
6538 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6539 (unsigned long long)fe_bh->b_blocknr, indexed);
6540 ret = ocfs2_create_xattr_block(handle, inode, fe_bh,
6541 meta_ac, ret_bh, indexed);
6545 ocfs2_commit_trans(osb, handle);
6547 ocfs2_free_alloc_context(meta_ac);
6551 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6552 struct buffer_head *blk_bh,
6553 struct buffer_head *new_blk_bh)
6555 int ret = 0, credits = 0;
6557 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6558 struct ocfs2_dinode *new_di;
6559 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6560 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6561 struct ocfs2_xattr_block *xb =
6562 (struct ocfs2_xattr_block *)blk_bh->b_data;
6563 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6564 struct ocfs2_xattr_block *new_xb =
6565 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6566 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6567 struct ocfs2_alloc_context *meta_ac;
6568 struct ocfs2_xattr_value_buf vb = {
6569 .vb_bh = new_blk_bh,
6570 .vb_access = ocfs2_journal_access_xb,
6573 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6574 &credits, &meta_ac);
6580 /* One more credits in case we need to add xattr flags in new inode. */
6581 handle = ocfs2_start_trans(osb, credits + 1);
6582 if (IS_ERR(handle)) {
6583 ret = PTR_ERR(handle);
6588 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6589 ret = ocfs2_journal_access_di(handle,
6590 INODE_CACHE(args->new_inode),
6592 OCFS2_JOURNAL_ACCESS_WRITE);
6599 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6600 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6606 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6607 osb->sb->s_blocksize - header_off);
6609 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6610 new_blk_bh, new_xh, &vb, meta_ac,
6611 ocfs2_get_xattr_value_root, NULL);
6617 ocfs2_journal_dirty(handle, new_blk_bh);
6619 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6620 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6621 spin_lock(&new_oi->ip_lock);
6622 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6623 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6624 spin_unlock(&new_oi->ip_lock);
6626 ocfs2_journal_dirty(handle, args->new_bh);
6630 ocfs2_commit_trans(osb, handle);
6633 ocfs2_free_alloc_context(meta_ac);
6637 struct ocfs2_reflink_xattr_tree_args {
6638 struct ocfs2_xattr_reflink *reflink;
6639 struct buffer_head *old_blk_bh;
6640 struct buffer_head *new_blk_bh;
6641 struct ocfs2_xattr_bucket *old_bucket;
6642 struct ocfs2_xattr_bucket *new_bucket;
6647 * We have to handle the case that both old bucket and new bucket
6648 * will call this function to get the right ret_bh.
6649 * So The caller must give us the right bh.
6651 static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6652 struct buffer_head *bh,
6653 struct ocfs2_xattr_header *xh,
6655 struct ocfs2_xattr_value_root **xv,
6656 struct buffer_head **ret_bh,
6659 struct ocfs2_reflink_xattr_tree_args *args =
6660 (struct ocfs2_reflink_xattr_tree_args *)para;
6661 struct ocfs2_xattr_bucket *bucket;
6663 if (bh == args->old_bucket->bu_bhs[0])
6664 bucket = args->old_bucket;
6666 bucket = args->new_bucket;
6668 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6672 struct ocfs2_value_tree_metas {
6678 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6679 struct buffer_head *bh,
6680 struct ocfs2_xattr_header *xh,
6682 struct ocfs2_xattr_value_root **xv,
6683 struct buffer_head **ret_bh,
6686 struct ocfs2_xattr_bucket *bucket =
6687 (struct ocfs2_xattr_bucket *)para;
6689 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6693 static int ocfs2_calc_value_tree_metas(struct inode *inode,
6694 struct ocfs2_xattr_bucket *bucket,
6697 struct ocfs2_value_tree_metas *metas =
6698 (struct ocfs2_value_tree_metas *)para;
6699 struct ocfs2_xattr_header *xh =
6700 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6702 /* Add the credits for this bucket first. */
6703 metas->credits += bucket->bu_blocks;
6704 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6705 xh, &metas->num_metas,
6706 &metas->credits, &metas->num_recs,
6707 ocfs2_value_tree_metas_in_bucket,
6712 * Given a xattr extent rec starting from blkno and having len clusters,
6713 * iterate all the buckets calculate how much metadata we need for reflinking
6714 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6716 static int ocfs2_lock_reflink_xattr_rec_allocators(
6717 struct ocfs2_reflink_xattr_tree_args *args,
6718 struct ocfs2_extent_tree *xt_et,
6719 u64 blkno, u32 len, int *credits,
6720 struct ocfs2_alloc_context **meta_ac,
6721 struct ocfs2_alloc_context **data_ac)
6723 int ret, num_free_extents;
6724 struct ocfs2_value_tree_metas metas;
6725 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6726 struct ocfs2_refcount_block *rb;
6728 memset(&metas, 0, sizeof(metas));
6730 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6731 ocfs2_calc_value_tree_metas, &metas);
6737 *credits = metas.credits;
6740 * Calculate we need for refcount tree change.
6742 * We need to add/modify num_recs in refcount tree, so just calculate
6743 * an approximate number we need for refcount tree change.
6744 * Sometimes we need to split the tree, and after split, half recs
6745 * will be moved to the new block, and a new block can only provide
6746 * half number of recs. So we multiple new blocks by 2.
6747 * In the end, we have to add credits for modifying the already
6748 * existed refcount block.
6750 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6752 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6753 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6754 metas.num_metas += metas.num_recs;
6755 *credits += metas.num_recs +
6756 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6757 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6758 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6759 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6763 /* count in the xattr tree change. */
6764 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6765 if (num_free_extents < 0) {
6766 ret = num_free_extents;
6771 if (num_free_extents < len)
6772 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6774 *credits += ocfs2_calc_extend_credits(osb->sb,
6775 xt_et->et_root_el, len);
6777 if (metas.num_metas) {
6778 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6787 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6794 ocfs2_free_alloc_context(*meta_ac);
6802 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6803 u64 blkno, u64 new_blkno, u32 clusters,
6804 struct ocfs2_alloc_context *meta_ac,
6805 struct ocfs2_alloc_context *data_ac,
6806 struct ocfs2_reflink_xattr_tree_args *args)
6809 struct super_block *sb = args->reflink->old_inode->i_sb;
6810 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6811 u32 num_buckets = clusters * bpc;
6812 int bpb = args->old_bucket->bu_blocks;
6813 struct ocfs2_xattr_value_buf vb = {
6814 .vb_access = ocfs2_journal_access,
6817 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6818 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6824 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6831 * The real bucket num in this series of blocks is stored
6832 * in the 1st bucket.
6835 num_buckets = le16_to_cpu(
6836 bucket_xh(args->old_bucket)->xh_num_buckets);
6838 ret = ocfs2_xattr_bucket_journal_access(handle,
6840 OCFS2_JOURNAL_ACCESS_CREATE);
6846 for (j = 0; j < bpb; j++)
6847 memcpy(bucket_block(args->new_bucket, j),
6848 bucket_block(args->old_bucket, j),
6851 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6853 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6854 args->old_bucket->bu_bhs[0],
6855 bucket_xh(args->old_bucket),
6856 args->new_bucket->bu_bhs[0],
6857 bucket_xh(args->new_bucket),
6859 ocfs2_get_reflink_xattr_value_root,
6867 * Re-access and dirty the bucket to calculate metaecc.
6868 * Because we may extend the transaction in reflink_xattr_header
6869 * which will let the already accessed block gone.
6871 ret = ocfs2_xattr_bucket_journal_access(handle,
6873 OCFS2_JOURNAL_ACCESS_WRITE);
6879 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6880 ocfs2_xattr_bucket_relse(args->old_bucket);
6881 ocfs2_xattr_bucket_relse(args->new_bucket);
6884 ocfs2_xattr_bucket_relse(args->old_bucket);
6885 ocfs2_xattr_bucket_relse(args->new_bucket);
6889 * Create the same xattr extent record in the new inode's xattr tree.
6891 static int ocfs2_reflink_xattr_rec(struct inode *inode,
6892 struct buffer_head *root_bh,
6898 int ret, credits = 0;
6899 u32 p_cluster, num_clusters;
6902 struct ocfs2_reflink_xattr_tree_args *args =
6903 (struct ocfs2_reflink_xattr_tree_args *)para;
6904 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6905 struct ocfs2_alloc_context *meta_ac = NULL;
6906 struct ocfs2_alloc_context *data_ac = NULL;
6907 struct ocfs2_extent_tree et;
6909 ocfs2_init_xattr_tree_extent_tree(&et,
6910 INODE_CACHE(args->reflink->new_inode),
6913 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6915 &meta_ac, &data_ac);
6921 handle = ocfs2_start_trans(osb, credits);
6922 if (IS_ERR(handle)) {
6923 ret = PTR_ERR(handle);
6928 ret = ocfs2_claim_clusters(osb, handle, data_ac,
6929 len, &p_cluster, &num_clusters);
6935 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6937 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6938 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6939 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6940 meta_ac, data_ac, args);
6946 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6947 (unsigned long long)new_blkno, len, cpos);
6948 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6954 ocfs2_commit_trans(osb, handle);
6958 ocfs2_free_alloc_context(meta_ac);
6960 ocfs2_free_alloc_context(data_ac);
6965 * Create reflinked xattr buckets.
6966 * We will add bucket one by one, and refcount all the xattrs in the bucket
6967 * if they are stored outside.
6969 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6970 struct buffer_head *blk_bh,
6971 struct buffer_head *new_blk_bh)
6974 struct ocfs2_reflink_xattr_tree_args para;
6976 memset(¶, 0, sizeof(para));
6977 para.reflink = args;
6978 para.old_blk_bh = blk_bh;
6979 para.new_blk_bh = new_blk_bh;
6981 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
6982 if (!para.old_bucket) {
6983 mlog_errno(-ENOMEM);
6987 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
6988 if (!para.new_bucket) {
6994 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
6995 ocfs2_reflink_xattr_rec,
7001 ocfs2_xattr_bucket_free(para.old_bucket);
7002 ocfs2_xattr_bucket_free(para.new_bucket);
7006 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7007 struct buffer_head *blk_bh)
7009 int ret, indexed = 0;
7010 struct buffer_head *new_blk_bh = NULL;
7011 struct ocfs2_xattr_block *xb =
7012 (struct ocfs2_xattr_block *)blk_bh->b_data;
7015 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7018 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7019 &new_blk_bh, indexed);
7025 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7026 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7028 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7037 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7039 int type = ocfs2_xattr_get_type(xe);
7041 return type != OCFS2_XATTR_INDEX_SECURITY &&
7042 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7043 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7046 int ocfs2_reflink_xattrs(struct inode *old_inode,
7047 struct buffer_head *old_bh,
7048 struct inode *new_inode,
7049 struct buffer_head *new_bh,
7050 bool preserve_security)
7053 struct ocfs2_xattr_reflink args;
7054 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7055 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7056 struct buffer_head *blk_bh = NULL;
7057 struct ocfs2_cached_dealloc_ctxt dealloc;
7058 struct ocfs2_refcount_tree *ref_tree;
7059 struct buffer_head *ref_root_bh = NULL;
7061 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7062 le64_to_cpu(di->i_refcount_loc),
7063 1, &ref_tree, &ref_root_bh);
7069 ocfs2_init_dealloc_ctxt(&dealloc);
7071 args.old_inode = old_inode;
7072 args.new_inode = new_inode;
7073 args.old_bh = old_bh;
7074 args.new_bh = new_bh;
7075 args.ref_ci = &ref_tree->rf_ci;
7076 args.ref_root_bh = ref_root_bh;
7077 args.dealloc = &dealloc;
7078 if (preserve_security)
7079 args.xattr_reflinked = NULL;
7081 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
7083 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7084 ret = ocfs2_reflink_xattr_inline(&args);
7091 if (!di->i_xattr_loc)
7094 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7101 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7108 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7110 brelse(ref_root_bh);
7112 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7113 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7114 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7122 * Initialize security and acl for a already created inode.
7123 * Used for reflink a non-preserve-security file.
7125 * It uses common api like ocfs2_xattr_set, so the caller
7126 * must not hold any lock expect i_mutex.
7128 int ocfs2_init_security_and_acl(struct inode *dir,
7129 struct inode *inode)
7132 struct buffer_head *dir_bh = NULL;
7133 struct ocfs2_security_xattr_info si = {
7137 ret = ocfs2_init_security_get(inode, dir, &si);
7139 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7140 si.name, si.value, si.value_len,
7146 } else if (ret != -EOPNOTSUPP) {
7151 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7157 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7161 ocfs2_inode_unlock(dir, 0);
7167 * 'security' attributes support
7169 static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
7170 size_t list_size, const char *name,
7171 size_t name_len, int type)
7173 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7174 const size_t total_len = prefix_len + name_len + 1;
7176 if (list && total_len <= list_size) {
7177 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7178 memcpy(list + prefix_len, name, name_len);
7179 list[prefix_len + name_len] = '\0';
7184 static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7185 void *buffer, size_t size, int type)
7187 if (strcmp(name, "") == 0)
7189 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7190 name, buffer, size);
7193 static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7194 const void *value, size_t size, int flags, int type)
7196 if (strcmp(name, "") == 0)
7199 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7200 name, value, size, flags);
7203 int ocfs2_init_security_get(struct inode *inode,
7205 struct ocfs2_security_xattr_info *si)
7207 /* check whether ocfs2 support feature xattr */
7208 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7210 return security_inode_init_security(inode, dir, &si->name, &si->value,
7214 int ocfs2_init_security_set(handle_t *handle,
7215 struct inode *inode,
7216 struct buffer_head *di_bh,
7217 struct ocfs2_security_xattr_info *si,
7218 struct ocfs2_alloc_context *xattr_ac,
7219 struct ocfs2_alloc_context *data_ac)
7221 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7222 OCFS2_XATTR_INDEX_SECURITY,
7223 si->name, si->value, si->value_len, 0,
7227 struct xattr_handler ocfs2_xattr_security_handler = {
7228 .prefix = XATTR_SECURITY_PREFIX,
7229 .list = ocfs2_xattr_security_list,
7230 .get = ocfs2_xattr_security_get,
7231 .set = ocfs2_xattr_security_set,
7235 * 'trusted' attributes support
7237 static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
7238 size_t list_size, const char *name,
7239 size_t name_len, int type)
7241 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
7242 const size_t total_len = prefix_len + name_len + 1;
7244 if (list && total_len <= list_size) {
7245 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7246 memcpy(list + prefix_len, name, name_len);
7247 list[prefix_len + name_len] = '\0';
7252 static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7253 void *buffer, size_t size, int type)
7255 if (strcmp(name, "") == 0)
7257 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7258 name, buffer, size);
7261 static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7262 const void *value, size_t size, int flags, int type)
7264 if (strcmp(name, "") == 0)
7267 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7268 name, value, size, flags);
7271 struct xattr_handler ocfs2_xattr_trusted_handler = {
7272 .prefix = XATTR_TRUSTED_PREFIX,
7273 .list = ocfs2_xattr_trusted_list,
7274 .get = ocfs2_xattr_trusted_get,
7275 .set = ocfs2_xattr_trusted_set,
7279 * 'user' attributes support
7281 static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
7282 size_t list_size, const char *name,
7283 size_t name_len, int type)
7285 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7286 const size_t total_len = prefix_len + name_len + 1;
7287 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7289 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7292 if (list && total_len <= list_size) {
7293 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7294 memcpy(list + prefix_len, name, name_len);
7295 list[prefix_len + name_len] = '\0';
7300 static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7301 void *buffer, size_t size, int type)
7303 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7305 if (strcmp(name, "") == 0)
7307 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7309 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
7313 static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7314 const void *value, size_t size, int flags, int type)
7316 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7318 if (strcmp(name, "") == 0)
7320 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7323 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7324 name, value, size, flags);
7327 struct xattr_handler ocfs2_xattr_user_handler = {
7328 .prefix = XATTR_USER_PREFIX,
7329 .list = ocfs2_xattr_user_list,
7330 .get = ocfs2_xattr_user_get,
7331 .set = ocfs2_xattr_user_set,