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 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
326 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
327 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
329 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
331 struct ocfs2_xattr_bucket *bucket;
332 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
334 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
336 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
338 bucket->bu_inode = inode;
339 bucket->bu_blocks = blks;
345 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
349 for (i = 0; i < bucket->bu_blocks; i++) {
350 brelse(bucket->bu_bhs[i]);
351 bucket->bu_bhs[i] = NULL;
355 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
358 ocfs2_xattr_bucket_relse(bucket);
359 bucket->bu_inode = NULL;
365 * A bucket that has never been written to disk doesn't need to be
366 * read. We just need the buffer_heads. Don't call this for
367 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
370 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
375 for (i = 0; i < bucket->bu_blocks; i++) {
376 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
378 if (!bucket->bu_bhs[i]) {
384 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
386 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
391 ocfs2_xattr_bucket_relse(bucket);
395 /* Read the xattr bucket at xb_blkno */
396 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
401 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
402 bucket->bu_blocks, bucket->bu_bhs, 0,
405 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
406 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
409 &bucket_xh(bucket)->xh_check);
410 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
416 ocfs2_xattr_bucket_relse(bucket);
420 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
421 struct ocfs2_xattr_bucket *bucket,
426 for (i = 0; i < bucket->bu_blocks; i++) {
427 rc = ocfs2_journal_access(handle,
428 INODE_CACHE(bucket->bu_inode),
429 bucket->bu_bhs[i], type);
439 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
440 struct ocfs2_xattr_bucket *bucket)
444 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
445 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
446 bucket->bu_bhs, bucket->bu_blocks,
447 &bucket_xh(bucket)->xh_check);
448 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
450 for (i = 0; i < bucket->bu_blocks; i++)
451 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
454 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
455 struct ocfs2_xattr_bucket *src)
458 int blocksize = src->bu_inode->i_sb->s_blocksize;
460 BUG_ON(dest->bu_blocks != src->bu_blocks);
461 BUG_ON(dest->bu_inode != src->bu_inode);
463 for (i = 0; i < src->bu_blocks; i++) {
464 memcpy(bucket_block(dest, i), bucket_block(src, i),
469 static int ocfs2_validate_xattr_block(struct super_block *sb,
470 struct buffer_head *bh)
473 struct ocfs2_xattr_block *xb =
474 (struct ocfs2_xattr_block *)bh->b_data;
476 mlog(0, "Validating xattr block %llu\n",
477 (unsigned long long)bh->b_blocknr);
479 BUG_ON(!buffer_uptodate(bh));
482 * If the ecc fails, we return the error but otherwise
483 * leave the filesystem running. We know any error is
484 * local to this block.
486 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
491 * Errors after here are fatal
494 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
496 "Extended attribute block #%llu has bad "
498 (unsigned long long)bh->b_blocknr, 7,
503 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
505 "Extended attribute block #%llu has an "
506 "invalid xb_blkno of %llu",
507 (unsigned long long)bh->b_blocknr,
508 (unsigned long long)le64_to_cpu(xb->xb_blkno));
512 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
514 "Extended attribute block #%llu has an invalid "
515 "xb_fs_generation of #%u",
516 (unsigned long long)bh->b_blocknr,
517 le32_to_cpu(xb->xb_fs_generation));
524 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
525 struct buffer_head **bh)
528 struct buffer_head *tmp = *bh;
530 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
531 ocfs2_validate_xattr_block);
533 /* If ocfs2_read_block() got us a new bh, pass it up. */
540 static inline const char *ocfs2_xattr_prefix(int name_index)
542 struct xattr_handler *handler = NULL;
544 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
545 handler = ocfs2_xattr_handler_map[name_index];
547 return handler ? handler->prefix : NULL;
550 static u32 ocfs2_xattr_name_hash(struct inode *inode,
554 /* Get hash value of uuid from super block */
555 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
558 /* hash extended attribute name */
559 for (i = 0; i < name_len; i++) {
560 hash = (hash << OCFS2_HASH_SHIFT) ^
561 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
568 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
570 return namevalue_size(name_len, value_len) +
571 sizeof(struct ocfs2_xattr_entry);
574 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
576 return namevalue_size_xi(xi) +
577 sizeof(struct ocfs2_xattr_entry);
580 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
582 return namevalue_size_xe(xe) +
583 sizeof(struct ocfs2_xattr_entry);
586 int ocfs2_calc_security_init(struct inode *dir,
587 struct ocfs2_security_xattr_info *si,
590 struct ocfs2_alloc_context **xattr_ac)
593 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
594 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
598 * The max space of security xattr taken inline is
599 * 256(name) + 80(value) + 16(entry) = 352 bytes,
600 * So reserve one metadata block for it is ok.
602 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
603 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
604 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
609 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
612 /* reserve clusters for xattr value which will be set in B tree*/
613 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
614 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
617 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
619 *want_clusters += new_clusters;
624 int ocfs2_calc_xattr_init(struct inode *dir,
625 struct buffer_head *dir_bh,
627 struct ocfs2_security_xattr_info *si,
633 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
634 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
637 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
640 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
641 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
642 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
645 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
648 } else if (acl_len != 0 && acl_len != -ENODATA) {
654 if (!(s_size + a_size))
658 * The max space of security xattr taken inline is
659 * 256(name) + 80(value) + 16(entry) = 352 bytes,
660 * The max space of acl xattr taken inline is
661 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
662 * when blocksize = 512, may reserve one more cluser for
663 * xattr bucket, otherwise reserve one metadata block
665 * If this is a new directory with inline data,
666 * we choose to reserve the entire inline area for
667 * directory contents and force an external xattr block.
669 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
670 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
671 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
672 *want_meta = *want_meta + 1;
673 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
676 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
677 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
679 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
683 * reserve credits and clusters for xattrs which has large value
684 * and have to be set outside
686 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
687 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
689 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
691 *want_clusters += new_clusters;
693 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
694 acl_len > OCFS2_XATTR_INLINE_SIZE) {
695 /* for directory, it has DEFAULT and ACCESS two types of acls */
696 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
697 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
698 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
700 *want_clusters += new_clusters;
706 static int ocfs2_xattr_extend_allocation(struct inode *inode,
708 struct ocfs2_xattr_value_buf *vb,
709 struct ocfs2_xattr_set_ctxt *ctxt)
712 handle_t *handle = ctxt->handle;
713 enum ocfs2_alloc_restarted why;
714 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
715 struct ocfs2_extent_tree et;
717 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
719 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
721 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
722 OCFS2_JOURNAL_ACCESS_WRITE);
728 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
729 status = ocfs2_add_clusters_in_btree(handle,
742 ocfs2_journal_dirty(handle, vb->vb_bh);
744 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
747 * We should have already allocated enough space before the transaction,
748 * so no need to restart.
750 BUG_ON(why != RESTART_NONE || clusters_to_add);
757 static int __ocfs2_remove_xattr_range(struct inode *inode,
758 struct ocfs2_xattr_value_buf *vb,
759 u32 cpos, u32 phys_cpos, u32 len,
760 unsigned int ext_flags,
761 struct ocfs2_xattr_set_ctxt *ctxt)
764 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
765 handle_t *handle = ctxt->handle;
766 struct ocfs2_extent_tree et;
768 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
770 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
771 OCFS2_JOURNAL_ACCESS_WRITE);
777 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
784 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
785 ocfs2_journal_dirty(handle, vb->vb_bh);
787 if (ext_flags & OCFS2_EXT_REFCOUNTED)
788 ret = ocfs2_decrease_refcount(inode, handle,
789 ocfs2_blocks_to_clusters(inode->i_sb,
791 len, ctxt->meta_ac, &ctxt->dealloc, 1);
793 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
802 static int ocfs2_xattr_shrink_size(struct inode *inode,
805 struct ocfs2_xattr_value_buf *vb,
806 struct ocfs2_xattr_set_ctxt *ctxt)
809 unsigned int ext_flags;
810 u32 trunc_len, cpos, phys_cpos, alloc_size;
813 if (old_clusters <= new_clusters)
817 trunc_len = old_clusters - new_clusters;
819 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
821 &vb->vb_xv->xr_list, &ext_flags);
827 if (alloc_size > trunc_len)
828 alloc_size = trunc_len;
830 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
831 phys_cpos, alloc_size,
838 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
839 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
842 trunc_len -= alloc_size;
849 static int ocfs2_xattr_value_truncate(struct inode *inode,
850 struct ocfs2_xattr_value_buf *vb,
852 struct ocfs2_xattr_set_ctxt *ctxt)
855 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
856 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
858 if (new_clusters == old_clusters)
861 if (new_clusters > old_clusters)
862 ret = ocfs2_xattr_extend_allocation(inode,
863 new_clusters - old_clusters,
866 ret = ocfs2_xattr_shrink_size(inode,
867 old_clusters, new_clusters,
873 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
874 size_t *result, const char *prefix,
875 const char *name, int name_len)
877 char *p = buffer + *result;
878 int prefix_len = strlen(prefix);
879 int total_len = prefix_len + name_len + 1;
881 *result += total_len;
883 /* we are just looking for how big our buffer needs to be */
890 memcpy(p, prefix, prefix_len);
891 memcpy(p + prefix_len, name, name_len);
892 p[prefix_len + name_len] = '\0';
897 static int ocfs2_xattr_list_entries(struct inode *inode,
898 struct ocfs2_xattr_header *header,
899 char *buffer, size_t buffer_size)
903 const char *prefix, *name;
905 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
906 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
907 type = ocfs2_xattr_get_type(entry);
908 prefix = ocfs2_xattr_prefix(type);
911 name = (const char *)header +
912 le16_to_cpu(entry->xe_name_offset);
914 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
915 &result, prefix, name,
925 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
926 struct ocfs2_dinode *di)
928 struct ocfs2_xattr_header *xh;
931 xh = (struct ocfs2_xattr_header *)
932 ((void *)di + inode->i_sb->s_blocksize -
933 le16_to_cpu(di->i_xattr_inline_size));
935 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
936 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
942 static int ocfs2_xattr_ibody_list(struct inode *inode,
943 struct ocfs2_dinode *di,
947 struct ocfs2_xattr_header *header = NULL;
948 struct ocfs2_inode_info *oi = OCFS2_I(inode);
951 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
954 header = (struct ocfs2_xattr_header *)
955 ((void *)di + inode->i_sb->s_blocksize -
956 le16_to_cpu(di->i_xattr_inline_size));
958 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
963 static int ocfs2_xattr_block_list(struct inode *inode,
964 struct ocfs2_dinode *di,
968 struct buffer_head *blk_bh = NULL;
969 struct ocfs2_xattr_block *xb;
972 if (!di->i_xattr_loc)
975 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
982 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
983 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
984 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
985 ret = ocfs2_xattr_list_entries(inode, header,
986 buffer, buffer_size);
988 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
989 buffer, buffer_size);
996 ssize_t ocfs2_listxattr(struct dentry *dentry,
1000 int ret = 0, i_ret = 0, b_ret = 0;
1001 struct buffer_head *di_bh = NULL;
1002 struct ocfs2_dinode *di = NULL;
1003 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1005 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1008 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1011 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1017 di = (struct ocfs2_dinode *)di_bh->b_data;
1019 down_read(&oi->ip_xattr_sem);
1020 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1028 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1033 up_read(&oi->ip_xattr_sem);
1034 ocfs2_inode_unlock(dentry->d_inode, 0);
1038 return i_ret + b_ret;
1041 static int ocfs2_xattr_find_entry(int name_index,
1043 struct ocfs2_xattr_search *xs)
1045 struct ocfs2_xattr_entry *entry;
1052 name_len = strlen(name);
1054 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1055 cmp = name_index - ocfs2_xattr_get_type(entry);
1057 cmp = name_len - entry->xe_name_len;
1059 cmp = memcmp(name, (xs->base +
1060 le16_to_cpu(entry->xe_name_offset)),
1068 return cmp ? -ENODATA : 0;
1071 static int ocfs2_xattr_get_value_outside(struct inode *inode,
1072 struct ocfs2_xattr_value_root *xv,
1076 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1079 size_t cplen, blocksize;
1080 struct buffer_head *bh = NULL;
1081 struct ocfs2_extent_list *el;
1084 clusters = le32_to_cpu(xv->xr_clusters);
1085 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1086 blocksize = inode->i_sb->s_blocksize;
1089 while (cpos < clusters) {
1090 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1091 &num_clusters, el, NULL);
1097 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1098 /* Copy ocfs2_xattr_value */
1099 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1100 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1107 cplen = len >= blocksize ? blocksize : len;
1108 memcpy(buffer, bh->b_data, cplen);
1117 cpos += num_clusters;
1123 static int ocfs2_xattr_ibody_get(struct inode *inode,
1128 struct ocfs2_xattr_search *xs)
1130 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1131 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1132 struct ocfs2_xattr_value_root *xv;
1136 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1139 xs->end = (void *)di + inode->i_sb->s_blocksize;
1140 xs->header = (struct ocfs2_xattr_header *)
1141 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1142 xs->base = (void *)xs->header;
1143 xs->here = xs->header->xh_entries;
1145 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1148 size = le64_to_cpu(xs->here->xe_value_size);
1150 if (size > buffer_size)
1152 if (ocfs2_xattr_is_local(xs->here)) {
1153 memcpy(buffer, (void *)xs->base +
1154 le16_to_cpu(xs->here->xe_name_offset) +
1155 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1157 xv = (struct ocfs2_xattr_value_root *)
1158 (xs->base + le16_to_cpu(
1159 xs->here->xe_name_offset) +
1160 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1161 ret = ocfs2_xattr_get_value_outside(inode, xv,
1173 static int ocfs2_xattr_block_get(struct inode *inode,
1178 struct ocfs2_xattr_search *xs)
1180 struct ocfs2_xattr_block *xb;
1181 struct ocfs2_xattr_value_root *xv;
1183 int ret = -ENODATA, name_offset, name_len, i;
1184 int uninitialized_var(block_off);
1186 xs->bucket = ocfs2_xattr_bucket_new(inode);
1193 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1199 if (xs->not_found) {
1204 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1205 size = le64_to_cpu(xs->here->xe_value_size);
1208 if (size > buffer_size)
1211 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1212 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1213 i = xs->here - xs->header->xh_entries;
1215 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1216 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1217 bucket_xh(xs->bucket),
1221 xs->base = bucket_block(xs->bucket, block_off);
1223 if (ocfs2_xattr_is_local(xs->here)) {
1224 memcpy(buffer, (void *)xs->base +
1225 name_offset + name_len, size);
1227 xv = (struct ocfs2_xattr_value_root *)
1228 (xs->base + name_offset + name_len);
1229 ret = ocfs2_xattr_get_value_outside(inode, xv,
1239 ocfs2_xattr_bucket_free(xs->bucket);
1241 brelse(xs->xattr_bh);
1242 xs->xattr_bh = NULL;
1246 int ocfs2_xattr_get_nolock(struct inode *inode,
1247 struct buffer_head *di_bh,
1254 struct ocfs2_dinode *di = NULL;
1255 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1256 struct ocfs2_xattr_search xis = {
1257 .not_found = -ENODATA,
1259 struct ocfs2_xattr_search xbs = {
1260 .not_found = -ENODATA,
1263 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1266 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1269 xis.inode_bh = xbs.inode_bh = di_bh;
1270 di = (struct ocfs2_dinode *)di_bh->b_data;
1272 down_read(&oi->ip_xattr_sem);
1273 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1275 if (ret == -ENODATA && di->i_xattr_loc)
1276 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1278 up_read(&oi->ip_xattr_sem);
1283 /* ocfs2_xattr_get()
1285 * Copy an extended attribute into the buffer provided.
1286 * Buffer is NULL to compute the size of buffer required.
1288 static int ocfs2_xattr_get(struct inode *inode,
1295 struct buffer_head *di_bh = NULL;
1297 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1302 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1303 name, buffer, buffer_size);
1305 ocfs2_inode_unlock(inode, 0);
1312 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1314 struct ocfs2_xattr_value_buf *vb,
1318 int ret = 0, i, cp_len;
1319 u16 blocksize = inode->i_sb->s_blocksize;
1320 u32 p_cluster, num_clusters;
1321 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1322 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1324 struct buffer_head *bh = NULL;
1325 unsigned int ext_flags;
1326 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1328 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1330 while (cpos < clusters) {
1331 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1332 &num_clusters, &xv->xr_list,
1339 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1341 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1343 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1344 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1351 ret = ocfs2_journal_access(handle,
1354 OCFS2_JOURNAL_ACCESS_WRITE);
1360 cp_len = value_len > blocksize ? blocksize : value_len;
1361 memcpy(bh->b_data, value, cp_len);
1362 value_len -= cp_len;
1364 if (cp_len < blocksize)
1365 memset(bh->b_data + cp_len, 0,
1366 blocksize - cp_len);
1368 ocfs2_journal_dirty(handle, bh);
1373 * XXX: do we need to empty all the following
1374 * blocks in this cluster?
1379 cpos += num_clusters;
1387 static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1395 free_space = free_start -
1396 sizeof(struct ocfs2_xattr_header) -
1397 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1398 OCFS2_XATTR_HEADER_GAP;
1401 if (free_space < needed_space)
1407 static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1410 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1413 static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1415 loc->xl_ops->xlo_journal_dirty(handle, loc);
1418 /* Give a pointer into the storage for the given offset */
1419 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1421 BUG_ON(offset >= loc->xl_size);
1422 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1426 * Wipe the name+value pair and allow the storage to reclaim it. This
1427 * must be followed by either removal of the entry or a call to
1428 * ocfs2_xa_add_namevalue().
1430 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1432 loc->xl_ops->xlo_wipe_namevalue(loc);
1436 * Find lowest offset to a name+value pair. This is the start of our
1437 * downward-growing free space.
1439 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1441 return loc->xl_ops->xlo_get_free_start(loc);
1444 /* Can we reuse loc->xl_entry for xi? */
1445 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1446 struct ocfs2_xattr_info *xi)
1448 return loc->xl_ops->xlo_can_reuse(loc, xi);
1451 /* How much free space is needed to set the new value */
1452 static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1453 struct ocfs2_xattr_info *xi)
1455 return loc->xl_ops->xlo_check_space(loc, xi);
1458 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1460 loc->xl_ops->xlo_add_entry(loc, name_hash);
1461 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1463 * We can't leave the new entry's xe_name_offset at zero or
1464 * add_namevalue() will go nuts. We set it to the size of our
1465 * storage so that it can never be less than any other entry.
1467 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1470 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1471 struct ocfs2_xattr_info *xi)
1473 int size = namevalue_size_xi(xi);
1477 loc->xl_ops->xlo_add_namevalue(loc, size);
1478 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1479 loc->xl_entry->xe_name_len = xi->xi_name_len;
1480 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1481 ocfs2_xattr_set_local(loc->xl_entry,
1482 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1484 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1485 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1486 memset(nameval_buf, 0, size);
1487 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1490 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1491 struct ocfs2_xattr_value_buf *vb)
1493 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1494 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1496 /* Value bufs are for value trees */
1497 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
1498 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1499 (name_size + OCFS2_XATTR_ROOT_SIZE));
1501 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1503 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1508 static int ocfs2_xa_block_journal_access(handle_t *handle,
1509 struct ocfs2_xa_loc *loc, int type)
1511 struct buffer_head *bh = loc->xl_storage;
1512 ocfs2_journal_access_func access;
1514 if (loc->xl_size == (bh->b_size -
1515 offsetof(struct ocfs2_xattr_block,
1516 xb_attrs.xb_header)))
1517 access = ocfs2_journal_access_xb;
1519 access = ocfs2_journal_access_di;
1520 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1523 static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1524 struct ocfs2_xa_loc *loc)
1526 struct buffer_head *bh = loc->xl_storage;
1528 ocfs2_journal_dirty(handle, bh);
1531 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1534 return (char *)loc->xl_header + offset;
1537 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1538 struct ocfs2_xattr_info *xi)
1541 * Block storage is strict. If the sizes aren't exact, we will
1542 * remove the old one and reinsert the new.
1544 return namevalue_size_xe(loc->xl_entry) ==
1545 namevalue_size_xi(xi);
1548 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1550 struct ocfs2_xattr_header *xh = loc->xl_header;
1551 int i, count = le16_to_cpu(xh->xh_count);
1552 int offset, free_start = loc->xl_size;
1554 for (i = 0; i < count; i++) {
1555 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1556 if (offset < free_start)
1557 free_start = offset;
1563 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1564 struct ocfs2_xattr_info *xi)
1566 int count = le16_to_cpu(loc->xl_header->xh_count);
1567 int free_start = ocfs2_xa_get_free_start(loc);
1568 int needed_space = ocfs2_xi_entry_usage(xi);
1571 * Block storage will reclaim the original entry before inserting
1572 * the new value, so we only need the difference. If the new
1573 * entry is smaller than the old one, we don't need anything.
1575 if (loc->xl_entry) {
1576 /* Don't need space if we're reusing! */
1577 if (ocfs2_xa_can_reuse_entry(loc, xi))
1580 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1582 if (needed_space < 0)
1584 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1588 * Block storage for xattrs keeps the name+value pairs compacted. When
1589 * we remove one, we have to shift any that preceded it towards the end.
1591 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1594 int namevalue_offset, first_namevalue_offset, namevalue_size;
1595 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1596 struct ocfs2_xattr_header *xh = loc->xl_header;
1597 int count = le16_to_cpu(xh->xh_count);
1599 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1600 namevalue_size = namevalue_size_xe(entry);
1601 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1603 /* Shift the name+value pairs */
1604 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1605 (char *)xh + first_namevalue_offset,
1606 namevalue_offset - first_namevalue_offset);
1607 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1609 /* Now tell xh->xh_entries about it */
1610 for (i = 0; i < count; i++) {
1611 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1612 if (offset <= namevalue_offset)
1613 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1618 * Note that we don't update xh_free_start or xh_name_value_len
1619 * because they're not used in block-stored xattrs.
1623 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1625 int count = le16_to_cpu(loc->xl_header->xh_count);
1626 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1627 le16_add_cpu(&loc->xl_header->xh_count, 1);
1628 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1631 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1633 int free_start = ocfs2_xa_get_free_start(loc);
1635 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1638 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1639 struct ocfs2_xattr_value_buf *vb)
1641 struct buffer_head *bh = loc->xl_storage;
1643 if (loc->xl_size == (bh->b_size -
1644 offsetof(struct ocfs2_xattr_block,
1645 xb_attrs.xb_header)))
1646 vb->vb_access = ocfs2_journal_access_xb;
1648 vb->vb_access = ocfs2_journal_access_di;
1653 * Operations for xattrs stored in blocks. This includes inline inode
1654 * storage and unindexed ocfs2_xattr_blocks.
1656 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1657 .xlo_journal_access = ocfs2_xa_block_journal_access,
1658 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
1659 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
1660 .xlo_check_space = ocfs2_xa_block_check_space,
1661 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1662 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
1663 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
1664 .xlo_add_entry = ocfs2_xa_block_add_entry,
1665 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
1666 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
1669 static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1670 struct ocfs2_xa_loc *loc, int type)
1672 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1674 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1677 static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1678 struct ocfs2_xa_loc *loc)
1680 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1682 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1685 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1688 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1689 int block, block_offset;
1691 /* The header is at the front of the bucket */
1692 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1693 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
1695 return bucket_block(bucket, block) + block_offset;
1698 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1699 struct ocfs2_xattr_info *xi)
1701 return namevalue_size_xe(loc->xl_entry) >=
1702 namevalue_size_xi(xi);
1705 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1707 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1708 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1711 static int ocfs2_bucket_align_free_start(struct super_block *sb,
1712 int free_start, int size)
1715 * We need to make sure that the name+value pair fits within
1718 if (((free_start - size) >> sb->s_blocksize_bits) !=
1719 ((free_start - 1) >> sb->s_blocksize_bits))
1720 free_start -= free_start % sb->s_blocksize;
1725 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1726 struct ocfs2_xattr_info *xi)
1729 int count = le16_to_cpu(loc->xl_header->xh_count);
1730 int free_start = ocfs2_xa_get_free_start(loc);
1731 int needed_space = ocfs2_xi_entry_usage(xi);
1732 int size = namevalue_size_xi(xi);
1733 struct super_block *sb = loc->xl_inode->i_sb;
1736 * Bucket storage does not reclaim name+value pairs it cannot
1737 * reuse. They live as holes until the bucket fills, and then
1738 * the bucket is defragmented. However, the bucket can reclaim
1739 * the ocfs2_xattr_entry.
1741 if (loc->xl_entry) {
1742 /* Don't need space if we're reusing! */
1743 if (ocfs2_xa_can_reuse_entry(loc, xi))
1746 needed_space -= sizeof(struct ocfs2_xattr_entry);
1748 BUG_ON(needed_space < 0);
1750 if (free_start < size) {
1755 * First we check if it would fit in the first place.
1756 * Below, we align the free start to a block. This may
1757 * slide us below the minimum gap. By checking unaligned
1758 * first, we avoid that error.
1760 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1764 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1767 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1770 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1772 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1773 -namevalue_size_xe(loc->xl_entry));
1776 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1778 struct ocfs2_xattr_header *xh = loc->xl_header;
1779 int count = le16_to_cpu(xh->xh_count);
1780 int low = 0, high = count - 1, tmp;
1781 struct ocfs2_xattr_entry *tmp_xe;
1784 * We keep buckets sorted by name_hash, so we need to find
1787 while (low <= high && count) {
1788 tmp = (low + high) / 2;
1789 tmp_xe = &xh->xh_entries[tmp];
1791 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1793 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1802 memmove(&xh->xh_entries[low + 1],
1803 &xh->xh_entries[low],
1804 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1806 le16_add_cpu(&xh->xh_count, 1);
1807 loc->xl_entry = &xh->xh_entries[low];
1808 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1811 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1813 int free_start = ocfs2_xa_get_free_start(loc);
1814 struct ocfs2_xattr_header *xh = loc->xl_header;
1815 struct super_block *sb = loc->xl_inode->i_sb;
1818 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1819 nameval_offset = free_start - size;
1820 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1821 xh->xh_free_start = cpu_to_le16(nameval_offset);
1822 le16_add_cpu(&xh->xh_name_value_len, size);
1826 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1827 struct ocfs2_xattr_value_buf *vb)
1829 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1830 struct super_block *sb = loc->xl_inode->i_sb;
1831 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1832 int size = namevalue_size_xe(loc->xl_entry);
1833 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1835 /* Values are not allowed to straddle block boundaries */
1836 BUG_ON(block_offset !=
1837 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1838 /* We expect the bucket to be filled in */
1839 BUG_ON(!bucket->bu_bhs[block_offset]);
1841 vb->vb_access = ocfs2_journal_access;
1842 vb->vb_bh = bucket->bu_bhs[block_offset];
1845 /* Operations for xattrs stored in buckets. */
1846 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1847 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1848 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
1849 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
1850 .xlo_check_space = ocfs2_xa_bucket_check_space,
1851 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1852 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
1853 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
1854 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1855 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
1856 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
1859 static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1861 struct ocfs2_xattr_value_buf vb;
1863 if (ocfs2_xattr_is_local(loc->xl_entry))
1866 ocfs2_xa_fill_value_buf(loc, &vb);
1867 return le32_to_cpu(vb.vb_xv->xr_clusters);
1870 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1871 struct ocfs2_xattr_set_ctxt *ctxt)
1873 int trunc_rc, access_rc;
1874 struct ocfs2_xattr_value_buf vb;
1876 ocfs2_xa_fill_value_buf(loc, &vb);
1877 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1881 * The caller of ocfs2_xa_value_truncate() has already called
1882 * ocfs2_xa_journal_access on the loc. However, The truncate code
1883 * calls ocfs2_extend_trans(). This may commit the previous
1884 * transaction and open a new one. If this is a bucket, truncate
1885 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1886 * the caller is expecting to dirty the entire bucket. So we must
1887 * reset the journal work. We do this even if truncate has failed,
1888 * as it could have failed after committing the extend.
1890 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1891 OCFS2_JOURNAL_ACCESS_WRITE);
1893 /* Errors in truncate take precedence */
1894 return trunc_rc ? trunc_rc : access_rc;
1897 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1900 struct ocfs2_xattr_header *xh = loc->xl_header;
1901 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1903 ocfs2_xa_wipe_namevalue(loc);
1904 loc->xl_entry = NULL;
1906 le16_add_cpu(&xh->xh_count, -1);
1907 count = le16_to_cpu(xh->xh_count);
1910 * Only zero out the entry if there are more remaining. This is
1911 * important for an empty bucket, as it keeps track of the
1912 * bucket's hash value. It doesn't hurt empty block storage.
1915 index = ((char *)entry - (char *)&xh->xh_entries) /
1916 sizeof(struct ocfs2_xattr_entry);
1917 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1918 (count - index) * sizeof(struct ocfs2_xattr_entry));
1919 memset(&xh->xh_entries[count], 0,
1920 sizeof(struct ocfs2_xattr_entry));
1925 * If we have a problem adjusting the size of an external value during
1926 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1927 * in an intermediate state. For example, the value may be partially
1930 * If the value tree hasn't changed, the extend/truncate went nowhere.
1931 * We have nothing to do. The caller can treat it as a straight error.
1933 * If the value tree got partially truncated, we now have a corrupted
1934 * extended attribute. We're going to wipe its entry and leak the
1935 * clusters. Better to leak some storage than leave a corrupt entry.
1937 * If the value tree grew, it obviously didn't grow enough for the
1938 * new entry. We're not going to try and reclaim those clusters either.
1939 * If there was already an external value there (orig_clusters != 0),
1940 * the new clusters are attached safely and we can just leave the old
1941 * value in place. If there was no external value there, we remove
1944 * This way, the xattr block we store in the journal will be consistent.
1945 * If the size change broke because of the journal, no changes will hit
1948 static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1950 unsigned int orig_clusters)
1952 unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1953 char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1954 le16_to_cpu(loc->xl_entry->xe_name_offset));
1956 if (new_clusters < orig_clusters) {
1958 "Partial truncate while %s xattr %.*s. Leaking "
1959 "%u clusters and removing the entry\n",
1960 what, loc->xl_entry->xe_name_len, nameval_buf,
1961 orig_clusters - new_clusters);
1962 ocfs2_xa_remove_entry(loc);
1963 } else if (!orig_clusters) {
1965 "Unable to allocate an external value for xattr "
1966 "%.*s safely. Leaking %u clusters and removing the "
1968 loc->xl_entry->xe_name_len, nameval_buf,
1969 new_clusters - orig_clusters);
1970 ocfs2_xa_remove_entry(loc);
1971 } else if (new_clusters > orig_clusters)
1973 "Unable to grow xattr %.*s safely. %u new clusters "
1974 "have been added, but the value will not be "
1976 loc->xl_entry->xe_name_len, nameval_buf,
1977 new_clusters - orig_clusters);
1980 static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1981 struct ocfs2_xattr_set_ctxt *ctxt)
1984 unsigned int orig_clusters;
1986 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
1987 orig_clusters = ocfs2_xa_value_clusters(loc);
1988 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
1992 * Since this is remove, we can return 0 if
1993 * ocfs2_xa_cleanup_value_truncate() is going to
1994 * wipe the entry anyway. So we check the
1995 * cluster count as well.
1997 if (orig_clusters != ocfs2_xa_value_clusters(loc))
1999 ocfs2_xa_cleanup_value_truncate(loc, "removing",
2006 ocfs2_xa_remove_entry(loc);
2012 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
2014 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
2017 nameval_buf = ocfs2_xa_offset_pointer(loc,
2018 le16_to_cpu(loc->xl_entry->xe_name_offset));
2019 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
2023 * Take an existing entry and make it ready for the new value. This
2024 * won't allocate space, but it may free space. It should be ready for
2025 * ocfs2_xa_prepare_entry() to finish the work.
2027 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
2028 struct ocfs2_xattr_info *xi,
2029 struct ocfs2_xattr_set_ctxt *ctxt)
2032 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2033 unsigned int orig_clusters;
2035 int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
2036 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
2038 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
2041 nameval_buf = ocfs2_xa_offset_pointer(loc,
2042 le16_to_cpu(loc->xl_entry->xe_name_offset));
2044 memset(nameval_buf + name_size, 0,
2045 namevalue_size_xe(loc->xl_entry) - name_size);
2047 ocfs2_xa_install_value_root(loc);
2049 orig_clusters = ocfs2_xa_value_clusters(loc);
2051 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2055 memset(nameval_buf + name_size, 0,
2056 namevalue_size_xe(loc->xl_entry) -
2058 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2060 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2067 ocfs2_xa_cleanup_value_truncate(loc, "reusing",
2073 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2074 ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2081 * Prepares loc->xl_entry to receive the new xattr. This includes
2082 * properly setting up the name+value pair region. If loc->xl_entry
2083 * already exists, it will take care of modifying it appropriately.
2085 * Note that this modifies the data. You did journal_access already,
2088 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2089 struct ocfs2_xattr_info *xi,
2091 struct ocfs2_xattr_set_ctxt *ctxt)
2094 unsigned int orig_clusters;
2095 __le64 orig_value_size = 0;
2097 rc = ocfs2_xa_check_space(loc, xi);
2101 if (loc->xl_entry) {
2102 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
2103 orig_value_size = loc->xl_entry->xe_value_size;
2104 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2110 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2111 orig_clusters = ocfs2_xa_value_clusters(loc);
2112 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2115 ocfs2_xa_cleanup_value_truncate(loc,
2121 ocfs2_xa_wipe_namevalue(loc);
2123 ocfs2_xa_add_entry(loc, name_hash);
2126 * If we get here, we have a blank entry. Fill it. We grow our
2127 * name+value pair back from the end.
2129 ocfs2_xa_add_namevalue(loc, xi);
2130 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2131 ocfs2_xa_install_value_root(loc);
2134 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2135 orig_clusters = ocfs2_xa_value_clusters(loc);
2136 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2138 ocfs2_xa_cleanup_value_truncate(loc, "growing",
2141 * If we were growing an existing value,
2142 * ocfs2_xa_cleanup_value_truncate() won't remove
2143 * the entry. We need to restore the original value
2146 if (loc->xl_entry) {
2147 BUG_ON(!orig_value_size);
2148 loc->xl_entry->xe_value_size = orig_value_size;
2159 * Store the value portion of the name+value pair. This will skip
2160 * values that are stored externally. Their tree roots were set up
2161 * by ocfs2_xa_prepare_entry().
2163 static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2164 struct ocfs2_xattr_info *xi,
2165 struct ocfs2_xattr_set_ctxt *ctxt)
2168 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2169 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2171 struct ocfs2_xattr_value_buf vb;
2173 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
2174 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2175 ocfs2_xa_fill_value_buf(loc, &vb);
2176 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2181 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2186 static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2187 struct ocfs2_xattr_info *xi,
2188 struct ocfs2_xattr_set_ctxt *ctxt)
2191 u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2194 ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2195 OCFS2_JOURNAL_ACCESS_WRITE);
2202 * From here on out, everything is going to modify the buffer a
2203 * little. Errors are going to leave the xattr header in a
2204 * sane state. Thus, even with errors we dirty the sucker.
2207 /* Don't worry, we are never called with !xi_value and !xl_entry */
2208 if (!xi->xi_value) {
2209 ret = ocfs2_xa_remove(loc, ctxt);
2213 ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2220 ret = ocfs2_xa_store_value(loc, xi, ctxt);
2225 ocfs2_xa_journal_dirty(ctxt->handle, loc);
2231 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2232 struct inode *inode,
2233 struct buffer_head *bh,
2234 struct ocfs2_xattr_entry *entry)
2236 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2238 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2240 loc->xl_inode = inode;
2241 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2242 loc->xl_storage = bh;
2243 loc->xl_entry = entry;
2244 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2246 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2250 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2251 struct inode *inode,
2252 struct buffer_head *bh,
2253 struct ocfs2_xattr_entry *entry)
2255 struct ocfs2_xattr_block *xb =
2256 (struct ocfs2_xattr_block *)bh->b_data;
2258 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2260 loc->xl_inode = inode;
2261 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2262 loc->xl_storage = bh;
2263 loc->xl_header = &(xb->xb_attrs.xb_header);
2264 loc->xl_entry = entry;
2265 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2266 xb_attrs.xb_header);
2269 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2270 struct ocfs2_xattr_bucket *bucket,
2271 struct ocfs2_xattr_entry *entry)
2273 loc->xl_inode = bucket->bu_inode;
2274 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2275 loc->xl_storage = bucket;
2276 loc->xl_header = bucket_xh(bucket);
2277 loc->xl_entry = entry;
2278 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2282 * In xattr remove, if it is stored outside and refcounted, we may have
2283 * the chance to split the refcount tree. So need the allocators.
2285 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2286 struct ocfs2_xattr_value_root *xv,
2287 struct ocfs2_caching_info *ref_ci,
2288 struct buffer_head *ref_root_bh,
2289 struct ocfs2_alloc_context **meta_ac,
2292 int ret, meta_add = 0;
2293 u32 p_cluster, num_clusters;
2294 unsigned int ext_flags;
2297 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2306 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2309 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2311 &meta_add, ref_credits);
2317 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2326 static int ocfs2_remove_value_outside(struct inode*inode,
2327 struct ocfs2_xattr_value_buf *vb,
2328 struct ocfs2_xattr_header *header,
2329 struct ocfs2_caching_info *ref_ci,
2330 struct buffer_head *ref_root_bh)
2332 int ret = 0, i, ref_credits;
2333 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2334 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2337 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2339 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2340 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2342 if (ocfs2_xattr_is_local(entry))
2345 val = (void *)header +
2346 le16_to_cpu(entry->xe_name_offset);
2347 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2348 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2350 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2351 ref_ci, ref_root_bh,
2355 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2356 ocfs2_remove_extent_credits(osb->sb));
2357 if (IS_ERR(ctxt.handle)) {
2358 ret = PTR_ERR(ctxt.handle);
2363 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2369 ocfs2_commit_trans(osb, ctxt.handle);
2371 ocfs2_free_alloc_context(ctxt.meta_ac);
2372 ctxt.meta_ac = NULL;
2377 ocfs2_free_alloc_context(ctxt.meta_ac);
2378 ocfs2_schedule_truncate_log_flush(osb, 1);
2379 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2383 static int ocfs2_xattr_ibody_remove(struct inode *inode,
2384 struct buffer_head *di_bh,
2385 struct ocfs2_caching_info *ref_ci,
2386 struct buffer_head *ref_root_bh)
2389 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2390 struct ocfs2_xattr_header *header;
2392 struct ocfs2_xattr_value_buf vb = {
2394 .vb_access = ocfs2_journal_access_di,
2397 header = (struct ocfs2_xattr_header *)
2398 ((void *)di + inode->i_sb->s_blocksize -
2399 le16_to_cpu(di->i_xattr_inline_size));
2401 ret = ocfs2_remove_value_outside(inode, &vb, header,
2402 ref_ci, ref_root_bh);
2407 struct ocfs2_rm_xattr_bucket_para {
2408 struct ocfs2_caching_info *ref_ci;
2409 struct buffer_head *ref_root_bh;
2412 static int ocfs2_xattr_block_remove(struct inode *inode,
2413 struct buffer_head *blk_bh,
2414 struct ocfs2_caching_info *ref_ci,
2415 struct buffer_head *ref_root_bh)
2417 struct ocfs2_xattr_block *xb;
2419 struct ocfs2_xattr_value_buf vb = {
2421 .vb_access = ocfs2_journal_access_xb,
2423 struct ocfs2_rm_xattr_bucket_para args = {
2425 .ref_root_bh = ref_root_bh,
2428 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2429 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2430 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2431 ret = ocfs2_remove_value_outside(inode, &vb, header,
2432 ref_ci, ref_root_bh);
2434 ret = ocfs2_iterate_xattr_index_block(inode,
2436 ocfs2_rm_xattr_cluster,
2442 static int ocfs2_xattr_free_block(struct inode *inode,
2444 struct ocfs2_caching_info *ref_ci,
2445 struct buffer_head *ref_root_bh)
2447 struct inode *xb_alloc_inode;
2448 struct buffer_head *xb_alloc_bh = NULL;
2449 struct buffer_head *blk_bh = NULL;
2450 struct ocfs2_xattr_block *xb;
2451 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2457 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2463 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2469 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2470 blk = le64_to_cpu(xb->xb_blkno);
2471 bit = le16_to_cpu(xb->xb_suballoc_bit);
2472 if (xb->xb_suballoc_loc)
2473 bg_blkno = le64_to_cpu(xb->xb_suballoc_loc);
2475 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2477 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2478 EXTENT_ALLOC_SYSTEM_INODE,
2479 le16_to_cpu(xb->xb_suballoc_slot));
2480 if (!xb_alloc_inode) {
2485 mutex_lock(&xb_alloc_inode->i_mutex);
2487 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2493 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2494 if (IS_ERR(handle)) {
2495 ret = PTR_ERR(handle);
2500 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2505 ocfs2_commit_trans(osb, handle);
2507 ocfs2_inode_unlock(xb_alloc_inode, 1);
2508 brelse(xb_alloc_bh);
2510 mutex_unlock(&xb_alloc_inode->i_mutex);
2511 iput(xb_alloc_inode);
2518 * ocfs2_xattr_remove()
2520 * Free extended attribute resources associated with this inode.
2522 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2524 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2525 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2526 struct ocfs2_refcount_tree *ref_tree = NULL;
2527 struct buffer_head *ref_root_bh = NULL;
2528 struct ocfs2_caching_info *ref_ci = NULL;
2532 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2535 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2538 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2539 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2540 le64_to_cpu(di->i_refcount_loc),
2541 1, &ref_tree, &ref_root_bh);
2546 ref_ci = &ref_tree->rf_ci;
2550 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2551 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2552 ref_ci, ref_root_bh);
2559 if (di->i_xattr_loc) {
2560 ret = ocfs2_xattr_free_block(inode,
2561 le64_to_cpu(di->i_xattr_loc),
2562 ref_ci, ref_root_bh);
2569 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2570 OCFS2_INODE_UPDATE_CREDITS);
2571 if (IS_ERR(handle)) {
2572 ret = PTR_ERR(handle);
2576 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2577 OCFS2_JOURNAL_ACCESS_WRITE);
2583 di->i_xattr_loc = 0;
2585 spin_lock(&oi->ip_lock);
2586 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2587 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2588 spin_unlock(&oi->ip_lock);
2590 ocfs2_journal_dirty(handle, di_bh);
2592 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2595 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2596 brelse(ref_root_bh);
2600 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2601 struct ocfs2_dinode *di)
2603 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2604 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2607 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2610 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2611 struct ocfs2_inline_data *idata = &di->id2.i_data;
2612 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2613 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2614 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2615 le64_to_cpu(di->i_size);
2617 struct ocfs2_extent_list *el = &di->id2.i_list;
2618 free = (le16_to_cpu(el->l_count) -
2619 le16_to_cpu(el->l_next_free_rec)) *
2620 sizeof(struct ocfs2_extent_rec);
2622 if (free >= xattrsize)
2629 * ocfs2_xattr_ibody_find()
2631 * Find extended attribute in inode block and
2632 * fill search info into struct ocfs2_xattr_search.
2634 static int ocfs2_xattr_ibody_find(struct inode *inode,
2637 struct ocfs2_xattr_search *xs)
2639 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2640 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2644 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2647 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2648 down_read(&oi->ip_alloc_sem);
2649 has_space = ocfs2_xattr_has_space_inline(inode, di);
2650 up_read(&oi->ip_alloc_sem);
2655 xs->xattr_bh = xs->inode_bh;
2656 xs->end = (void *)di + inode->i_sb->s_blocksize;
2657 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2658 xs->header = (struct ocfs2_xattr_header *)
2659 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2661 xs->header = (struct ocfs2_xattr_header *)
2662 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2663 xs->base = (void *)xs->header;
2664 xs->here = xs->header->xh_entries;
2666 /* Find the named attribute. */
2667 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2668 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2669 if (ret && ret != -ENODATA)
2671 xs->not_found = ret;
2677 static int ocfs2_xattr_ibody_init(struct inode *inode,
2678 struct buffer_head *di_bh,
2679 struct ocfs2_xattr_set_ctxt *ctxt)
2682 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2683 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2684 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2685 unsigned int xattrsize = osb->s_xattr_inline_size;
2687 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2692 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2693 OCFS2_JOURNAL_ACCESS_WRITE);
2700 * Adjust extent record count or inline data size
2701 * to reserve space for extended attribute.
2703 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2704 struct ocfs2_inline_data *idata = &di->id2.i_data;
2705 le16_add_cpu(&idata->id_count, -xattrsize);
2706 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2707 struct ocfs2_extent_list *el = &di->id2.i_list;
2708 le16_add_cpu(&el->l_count, -(xattrsize /
2709 sizeof(struct ocfs2_extent_rec)));
2711 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2713 spin_lock(&oi->ip_lock);
2714 oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2715 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2716 spin_unlock(&oi->ip_lock);
2718 ocfs2_journal_dirty(ctxt->handle, di_bh);
2725 * ocfs2_xattr_ibody_set()
2727 * Set, replace or remove an extended attribute into inode block.
2730 static int ocfs2_xattr_ibody_set(struct inode *inode,
2731 struct ocfs2_xattr_info *xi,
2732 struct ocfs2_xattr_search *xs,
2733 struct ocfs2_xattr_set_ctxt *ctxt)
2736 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2737 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2738 struct ocfs2_xa_loc loc;
2740 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2743 down_write(&oi->ip_alloc_sem);
2744 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2745 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2751 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2752 ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2760 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2761 xs->not_found ? NULL : xs->here);
2762 ret = ocfs2_xa_set(&loc, xi, ctxt);
2768 xs->here = loc.xl_entry;
2771 up_write(&oi->ip_alloc_sem);
2777 * ocfs2_xattr_block_find()
2779 * Find extended attribute in external block and
2780 * fill search info into struct ocfs2_xattr_search.
2782 static int ocfs2_xattr_block_find(struct inode *inode,
2785 struct ocfs2_xattr_search *xs)
2787 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2788 struct buffer_head *blk_bh = NULL;
2789 struct ocfs2_xattr_block *xb;
2792 if (!di->i_xattr_loc)
2795 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2802 xs->xattr_bh = blk_bh;
2803 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2805 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2806 xs->header = &xb->xb_attrs.xb_header;
2807 xs->base = (void *)xs->header;
2808 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2809 xs->here = xs->header->xh_entries;
2811 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2813 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2817 if (ret && ret != -ENODATA) {
2818 xs->xattr_bh = NULL;
2821 xs->not_found = ret;
2829 static int ocfs2_create_xattr_block(struct inode *inode,
2830 struct buffer_head *inode_bh,
2831 struct ocfs2_xattr_set_ctxt *ctxt,
2833 struct buffer_head **ret_bh)
2836 u16 suballoc_bit_start;
2838 u64 suballoc_loc, first_blkno;
2839 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2840 struct buffer_head *new_bh = NULL;
2841 struct ocfs2_xattr_block *xblk;
2843 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2844 inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
2850 ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
2851 &suballoc_loc, &suballoc_bit_start,
2852 &num_got, &first_blkno);
2858 new_bh = sb_getblk(inode->i_sb, first_blkno);
2859 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2861 ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
2863 OCFS2_JOURNAL_ACCESS_CREATE);
2869 /* Initialize ocfs2_xattr_block */
2870 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2871 memset(xblk, 0, inode->i_sb->s_blocksize);
2872 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2873 xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
2874 xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc);
2875 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2876 xblk->xb_fs_generation =
2877 cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
2878 xblk->xb_blkno = cpu_to_le64(first_blkno);
2880 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2881 xr->xt_clusters = cpu_to_le32(1);
2882 xr->xt_last_eb_blk = 0;
2883 xr->xt_list.l_tree_depth = 0;
2884 xr->xt_list.l_count = cpu_to_le16(
2885 ocfs2_xattr_recs_per_xb(inode->i_sb));
2886 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2887 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2889 ocfs2_journal_dirty(ctxt->handle, new_bh);
2891 /* Add it to the inode */
2892 di->i_xattr_loc = cpu_to_le64(first_blkno);
2894 spin_lock(&OCFS2_I(inode)->ip_lock);
2895 OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2896 di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2897 spin_unlock(&OCFS2_I(inode)->ip_lock);
2899 ocfs2_journal_dirty(ctxt->handle, inode_bh);
2910 * ocfs2_xattr_block_set()
2912 * Set, replace or remove an extended attribute into external block.
2915 static int ocfs2_xattr_block_set(struct inode *inode,
2916 struct ocfs2_xattr_info *xi,
2917 struct ocfs2_xattr_search *xs,
2918 struct ocfs2_xattr_set_ctxt *ctxt)
2920 struct buffer_head *new_bh = NULL;
2921 struct ocfs2_xattr_block *xblk = NULL;
2923 struct ocfs2_xa_loc loc;
2925 if (!xs->xattr_bh) {
2926 ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2933 xs->xattr_bh = new_bh;
2934 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2935 xs->header = &xblk->xb_attrs.xb_header;
2936 xs->base = (void *)xs->header;
2937 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2938 xs->here = xs->header->xh_entries;
2940 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2942 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2943 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2944 xs->not_found ? NULL : xs->here);
2946 ret = ocfs2_xa_set(&loc, xi, ctxt);
2948 xs->here = loc.xl_entry;
2949 else if (ret != -ENOSPC)
2952 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2958 if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2959 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2965 /* Check whether the new xattr can be inserted into the inode. */
2966 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2967 struct ocfs2_xattr_info *xi,
2968 struct ocfs2_xattr_search *xs)
2970 struct ocfs2_xattr_entry *last;
2972 size_t min_offs = xs->end - xs->base;
2977 last = xs->header->xh_entries;
2979 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2980 size_t offs = le16_to_cpu(last->xe_name_offset);
2981 if (offs < min_offs)
2986 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2990 BUG_ON(!xs->not_found);
2992 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
2998 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2999 struct ocfs2_dinode *di,
3000 struct ocfs2_xattr_info *xi,
3001 struct ocfs2_xattr_search *xis,
3002 struct ocfs2_xattr_search *xbs,
3007 int ret = 0, old_in_xb = 0;
3008 int clusters_add = 0, meta_add = 0, credits = 0;
3009 struct buffer_head *bh = NULL;
3010 struct ocfs2_xattr_block *xb = NULL;
3011 struct ocfs2_xattr_entry *xe = NULL;
3012 struct ocfs2_xattr_value_root *xv = NULL;
3014 int name_offset, name_len = 0;
3015 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3020 * Calculate the clusters we need to write.
3021 * No matter whether we replace an old one or add a new one,
3022 * we need this for writing.
3024 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
3025 credits += new_clusters *
3026 ocfs2_clusters_to_blocks(inode->i_sb, 1);
3028 if (xis->not_found && xbs->not_found) {
3029 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3031 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3032 clusters_add += new_clusters;
3033 credits += ocfs2_calc_extend_credits(inode->i_sb,
3041 if (!xis->not_found) {
3043 name_offset = le16_to_cpu(xe->xe_name_offset);
3044 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3046 credits += OCFS2_INODE_UPDATE_CREDITS;
3048 int i, block_off = 0;
3049 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3051 name_offset = le16_to_cpu(xe->xe_name_offset);
3052 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3053 i = xbs->here - xbs->header->xh_entries;
3056 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3057 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3058 bucket_xh(xbs->bucket),
3061 base = bucket_block(xbs->bucket, block_off);
3062 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3065 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3070 * delete a xattr doesn't need metadata and cluster allocation.
3071 * so just calculate the credits and return.
3073 * The credits for removing the value tree will be extended
3074 * by ocfs2_remove_extent itself.
3076 if (!xi->xi_value) {
3077 if (!ocfs2_xattr_is_local(xe))
3078 credits += ocfs2_remove_extent_credits(inode->i_sb);
3083 /* do cluster allocation guess first. */
3084 value_size = le64_to_cpu(xe->xe_value_size);
3088 * In xattr set, we always try to set the xe in inode first,
3089 * so if it can be inserted into inode successfully, the old
3090 * one will be removed from the xattr block, and this xattr
3091 * will be inserted into inode as a new xattr in inode.
3093 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3094 clusters_add += new_clusters;
3095 credits += ocfs2_remove_extent_credits(inode->i_sb) +
3096 OCFS2_INODE_UPDATE_CREDITS;
3097 if (!ocfs2_xattr_is_local(xe))
3098 credits += ocfs2_calc_extend_credits(
3106 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3107 /* the new values will be stored outside. */
3108 u32 old_clusters = 0;
3110 if (!ocfs2_xattr_is_local(xe)) {
3111 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3113 xv = (struct ocfs2_xattr_value_root *)
3114 (base + name_offset + name_len);
3115 value_size = OCFS2_XATTR_ROOT_SIZE;
3119 if (old_clusters >= new_clusters) {
3120 credits += ocfs2_remove_extent_credits(inode->i_sb);
3123 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3124 clusters_add += new_clusters - old_clusters;
3125 credits += ocfs2_calc_extend_credits(inode->i_sb,
3129 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3134 * Now the new value will be stored inside. So if the new
3135 * value is smaller than the size of value root or the old
3136 * value, we don't need any allocation, otherwise we have
3137 * to guess metadata allocation.
3139 if ((ocfs2_xattr_is_local(xe) &&
3140 (value_size >= xi->xi_value_len)) ||
3141 (!ocfs2_xattr_is_local(xe) &&
3142 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
3147 /* calculate metadata allocation. */
3148 if (di->i_xattr_loc) {
3149 if (!xbs->xattr_bh) {
3150 ret = ocfs2_read_xattr_block(inode,
3151 le64_to_cpu(di->i_xattr_loc),
3158 xb = (struct ocfs2_xattr_block *)bh->b_data;
3160 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3163 * If there is already an xattr tree, good, we can calculate
3164 * like other b-trees. Otherwise we may have the chance of
3165 * create a tree, the credit calculation is borrowed from
3166 * ocfs2_calc_extend_credits with root_el = NULL. And the
3167 * new tree will be cluster based, so no meta is needed.
3169 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3170 struct ocfs2_extent_list *el =
3171 &xb->xb_attrs.xb_root.xt_list;
3172 meta_add += ocfs2_extend_meta_needed(el);
3173 credits += ocfs2_calc_extend_credits(inode->i_sb,
3176 credits += OCFS2_SUBALLOC_ALLOC + 1;
3179 * This cluster will be used either for new bucket or for
3181 * If the cluster size is the same as the bucket size, one
3182 * more is needed since we may need to extend the bucket
3186 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3187 if (OCFS2_XATTR_BUCKET_SIZE ==
3188 OCFS2_SB(inode->i_sb)->s_clustersize) {
3189 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3194 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3198 *clusters_need = clusters_add;
3200 *meta_need = meta_add;
3202 *credits_need = credits;
3207 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3208 struct ocfs2_dinode *di,
3209 struct ocfs2_xattr_info *xi,
3210 struct ocfs2_xattr_search *xis,
3211 struct ocfs2_xattr_search *xbs,
3212 struct ocfs2_xattr_set_ctxt *ctxt,
3216 int clusters_add, meta_add, ret;
3217 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3219 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3221 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3223 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
3224 &clusters_add, &meta_add, credits);
3230 meta_add += extra_meta;
3231 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
3232 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
3235 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3244 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3250 if (ctxt->meta_ac) {
3251 ocfs2_free_alloc_context(ctxt->meta_ac);
3252 ctxt->meta_ac = NULL;
3256 * We cannot have an error and a non null ctxt->data_ac.
3263 static int __ocfs2_xattr_set_handle(struct inode *inode,
3264 struct ocfs2_dinode *di,
3265 struct ocfs2_xattr_info *xi,
3266 struct ocfs2_xattr_search *xis,
3267 struct ocfs2_xattr_search *xbs,
3268 struct ocfs2_xattr_set_ctxt *ctxt)
3270 int ret = 0, credits, old_found;
3272 if (!xi->xi_value) {
3273 /* Remove existing extended attribute */
3274 if (!xis->not_found)
3275 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3276 else if (!xbs->not_found)
3277 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3279 /* We always try to set extended attribute into inode first*/
3280 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3281 if (!ret && !xbs->not_found) {
3283 * If succeed and that extended attribute existing in
3284 * external block, then we will remove it.
3286 xi->xi_value = NULL;
3287 xi->xi_value_len = 0;
3289 old_found = xis->not_found;
3290 xis->not_found = -ENODATA;
3291 ret = ocfs2_calc_xattr_set_need(inode,
3299 xis->not_found = old_found;
3305 ret = ocfs2_extend_trans(ctxt->handle, credits);
3310 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3311 } else if (ret == -ENOSPC) {
3312 if (di->i_xattr_loc && !xbs->xattr_bh) {
3313 ret = ocfs2_xattr_block_find(inode,
3319 old_found = xis->not_found;
3320 xis->not_found = -ENODATA;
3321 ret = ocfs2_calc_xattr_set_need(inode,
3329 xis->not_found = old_found;
3335 ret = ocfs2_extend_trans(ctxt->handle, credits);
3342 * If no space in inode, we will set extended attribute
3343 * into external block.
3345 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3348 if (!xis->not_found) {
3350 * If succeed and that extended attribute
3351 * existing in inode, we will remove it.
3353 xi->xi_value = NULL;
3354 xi->xi_value_len = 0;
3355 xbs->not_found = -ENODATA;
3356 ret = ocfs2_calc_xattr_set_need(inode,
3369 ret = ocfs2_extend_trans(ctxt->handle, credits);
3374 ret = ocfs2_xattr_ibody_set(inode, xi,
3381 /* Update inode ctime. */
3382 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3384 OCFS2_JOURNAL_ACCESS_WRITE);
3390 inode->i_ctime = CURRENT_TIME;
3391 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3392 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3393 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3400 * This function only called duing creating inode
3401 * for init security/acl xattrs of the new inode.
3402 * All transanction credits have been reserved in mknod.
3404 int ocfs2_xattr_set_handle(handle_t *handle,
3405 struct inode *inode,
3406 struct buffer_head *di_bh,
3412 struct ocfs2_alloc_context *meta_ac,
3413 struct ocfs2_alloc_context *data_ac)
3415 struct ocfs2_dinode *di;
3418 struct ocfs2_xattr_info xi = {
3419 .xi_name_index = name_index,
3421 .xi_name_len = strlen(name),
3423 .xi_value_len = value_len,
3426 struct ocfs2_xattr_search xis = {
3427 .not_found = -ENODATA,
3430 struct ocfs2_xattr_search xbs = {
3431 .not_found = -ENODATA,
3434 struct ocfs2_xattr_set_ctxt ctxt = {
3440 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3444 * In extreme situation, may need xattr bucket when
3445 * block size is too small. And we have already reserved
3446 * the credits for bucket in mknod.
3448 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3449 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3451 mlog_errno(-ENOMEM);
3456 xis.inode_bh = xbs.inode_bh = di_bh;
3457 di = (struct ocfs2_dinode *)di_bh->b_data;
3459 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3461 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3464 if (xis.not_found) {
3465 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3470 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3473 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3474 brelse(xbs.xattr_bh);
3475 ocfs2_xattr_bucket_free(xbs.bucket);
3483 * Set, replace or remove an extended attribute for this inode.
3484 * value is NULL to remove an existing extended attribute, else either
3485 * create or replace an extended attribute.
3487 int ocfs2_xattr_set(struct inode *inode,
3494 struct buffer_head *di_bh = NULL;
3495 struct ocfs2_dinode *di;
3496 int ret, credits, ref_meta = 0, ref_credits = 0;
3497 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3498 struct inode *tl_inode = osb->osb_tl_inode;
3499 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
3500 struct ocfs2_refcount_tree *ref_tree = NULL;
3502 struct ocfs2_xattr_info xi = {
3503 .xi_name_index = name_index,
3505 .xi_name_len = strlen(name),
3507 .xi_value_len = value_len,
3510 struct ocfs2_xattr_search xis = {
3511 .not_found = -ENODATA,
3514 struct ocfs2_xattr_search xbs = {
3515 .not_found = -ENODATA,
3518 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3522 * Only xbs will be used on indexed trees. xis doesn't need a
3525 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3527 mlog_errno(-ENOMEM);
3531 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3534 goto cleanup_nolock;
3536 xis.inode_bh = xbs.inode_bh = di_bh;
3537 di = (struct ocfs2_dinode *)di_bh->b_data;
3539 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3541 * Scan inode and external block to find the same name
3542 * extended attribute and collect search infomation.
3544 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3547 if (xis.not_found) {
3548 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3553 if (xis.not_found && xbs.not_found) {
3555 if (flags & XATTR_REPLACE)
3562 if (flags & XATTR_CREATE)
3566 /* Check whether the value is refcounted and do some prepartion. */
3567 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3568 (!xis.not_found || !xbs.not_found)) {
3569 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3570 &xis, &xbs, &ref_tree,
3571 &ref_meta, &ref_credits);
3578 mutex_lock(&tl_inode->i_mutex);
3580 if (ocfs2_truncate_log_needs_flush(osb)) {
3581 ret = __ocfs2_flush_truncate_log(osb);
3583 mutex_unlock(&tl_inode->i_mutex);
3588 mutex_unlock(&tl_inode->i_mutex);
3590 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3591 &xbs, &ctxt, ref_meta, &credits);
3597 /* we need to update inode's ctime field, so add credit for it. */
3598 credits += OCFS2_INODE_UPDATE_CREDITS;
3599 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3600 if (IS_ERR(ctxt.handle)) {
3601 ret = PTR_ERR(ctxt.handle);
3606 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3608 ocfs2_commit_trans(osb, ctxt.handle);
3611 ocfs2_free_alloc_context(ctxt.data_ac);
3613 ocfs2_free_alloc_context(ctxt.meta_ac);
3614 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3615 ocfs2_schedule_truncate_log_flush(osb, 1);
3616 ocfs2_run_deallocs(osb, &ctxt.dealloc);
3620 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3621 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3622 if (!value && !ret) {
3623 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3627 ocfs2_inode_unlock(inode, 1);
3630 brelse(xbs.xattr_bh);
3631 ocfs2_xattr_bucket_free(xbs.bucket);
3637 * Find the xattr extent rec which may contains name_hash.
3638 * e_cpos will be the first name hash of the xattr rec.
3639 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3641 static int ocfs2_xattr_get_rec(struct inode *inode,
3646 struct ocfs2_extent_list *el)
3649 struct buffer_head *eb_bh = NULL;
3650 struct ocfs2_extent_block *eb;
3651 struct ocfs2_extent_rec *rec = NULL;
3654 if (el->l_tree_depth) {
3655 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3662 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3665 if (el->l_tree_depth) {
3666 ocfs2_error(inode->i_sb,
3667 "Inode %lu has non zero tree depth in "
3668 "xattr tree block %llu\n", inode->i_ino,
3669 (unsigned long long)eb_bh->b_blocknr);
3675 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3676 rec = &el->l_recs[i];
3678 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3679 e_blkno = le64_to_cpu(rec->e_blkno);
3685 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3686 "record (%u, %u, 0) in xattr", inode->i_ino,
3687 le32_to_cpu(rec->e_cpos),
3688 ocfs2_rec_clusters(el, rec));
3693 *p_blkno = le64_to_cpu(rec->e_blkno);
3694 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3696 *e_cpos = le32_to_cpu(rec->e_cpos);
3702 typedef int (xattr_bucket_func)(struct inode *inode,
3703 struct ocfs2_xattr_bucket *bucket,
3706 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3707 struct ocfs2_xattr_bucket *bucket,
3714 int i, ret = 0, cmp = 1, block_off, new_offset;
3715 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3716 size_t name_len = strlen(name);
3717 struct ocfs2_xattr_entry *xe = NULL;
3721 * We don't use binary search in the bucket because there
3722 * may be multiple entries with the same name hash.
3724 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3725 xe = &xh->xh_entries[i];
3727 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3729 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3732 cmp = name_index - ocfs2_xattr_get_type(xe);
3734 cmp = name_len - xe->xe_name_len;
3738 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3749 xe_name = bucket_block(bucket, block_off) + new_offset;
3750 if (!memcmp(name, xe_name, name_len)) {
3762 * Find the specified xattr entry in a series of buckets.
3763 * This series start from p_blkno and last for num_clusters.
3764 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3765 * the num of the valid buckets.
3767 * Return the buffer_head this xattr should reside in. And if the xattr's
3768 * hash is in the gap of 2 buckets, return the lower bucket.
3770 static int ocfs2_xattr_bucket_find(struct inode *inode,
3777 struct ocfs2_xattr_search *xs)
3780 struct ocfs2_xattr_header *xh = NULL;
3781 struct ocfs2_xattr_entry *xe = NULL;
3783 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3784 int low_bucket = 0, bucket, high_bucket;
3785 struct ocfs2_xattr_bucket *search;
3787 u64 blkno, lower_blkno = 0;
3789 search = ocfs2_xattr_bucket_new(inode);
3796 ret = ocfs2_read_xattr_bucket(search, p_blkno);
3802 xh = bucket_xh(search);
3803 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3804 while (low_bucket <= high_bucket) {
3805 ocfs2_xattr_bucket_relse(search);
3807 bucket = (low_bucket + high_bucket) / 2;
3808 blkno = p_blkno + bucket * blk_per_bucket;
3809 ret = ocfs2_read_xattr_bucket(search, blkno);
3815 xh = bucket_xh(search);
3816 xe = &xh->xh_entries[0];
3817 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3818 high_bucket = bucket - 1;
3823 * Check whether the hash of the last entry in our
3824 * bucket is larger than the search one. for an empty
3825 * bucket, the last one is also the first one.
3828 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3830 last_hash = le32_to_cpu(xe->xe_name_hash);
3832 /* record lower_blkno which may be the insert place. */
3833 lower_blkno = blkno;
3835 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3836 low_bucket = bucket + 1;
3840 /* the searched xattr should reside in this bucket if exists. */
3841 ret = ocfs2_find_xe_in_bucket(inode, search,
3842 name_index, name, name_hash,
3852 * Record the bucket we have found.
3853 * When the xattr's hash value is in the gap of 2 buckets, we will
3854 * always set it to the previous bucket.
3857 lower_blkno = p_blkno;
3859 /* This should be in cache - we just read it during the search */
3860 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3866 xs->header = bucket_xh(xs->bucket);
3867 xs->base = bucket_block(xs->bucket, 0);
3868 xs->end = xs->base + inode->i_sb->s_blocksize;
3871 xs->here = &xs->header->xh_entries[index];
3872 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3873 (unsigned long long)bucket_blkno(xs->bucket), index);
3878 ocfs2_xattr_bucket_free(search);
3882 static int ocfs2_xattr_index_block_find(struct inode *inode,
3883 struct buffer_head *root_bh,
3886 struct ocfs2_xattr_search *xs)
3889 struct ocfs2_xattr_block *xb =
3890 (struct ocfs2_xattr_block *)root_bh->b_data;
3891 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3892 struct ocfs2_extent_list *el = &xb_root->xt_list;
3894 u32 first_hash, num_clusters = 0;
3895 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3897 if (le16_to_cpu(el->l_next_free_rec) == 0)
3900 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3901 name, name_hash, name_index);
3903 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3910 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3912 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3913 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3916 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3917 p_blkno, first_hash, num_clusters, xs);
3923 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3926 xattr_bucket_func *func,
3930 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3931 u32 num_buckets = clusters * bpc;
3932 struct ocfs2_xattr_bucket *bucket;
3934 bucket = ocfs2_xattr_bucket_new(inode);
3936 mlog_errno(-ENOMEM);
3940 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3941 clusters, (unsigned long long)blkno);
3943 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3944 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3951 * The real bucket num in this series of blocks is stored
3952 * in the 1st bucket.
3955 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3957 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3958 (unsigned long long)blkno,
3959 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3961 ret = func(inode, bucket, para);
3962 if (ret && ret != -ERANGE)
3964 /* Fall through to bucket_relse() */
3967 ocfs2_xattr_bucket_relse(bucket);
3972 ocfs2_xattr_bucket_free(bucket);
3976 struct ocfs2_xattr_tree_list {
3982 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
3983 struct ocfs2_xattr_header *xh,
3990 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3993 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3995 *block_off = name_offset >> sb->s_blocksize_bits;
3996 *new_offset = name_offset % sb->s_blocksize;
4001 static int ocfs2_list_xattr_bucket(struct inode *inode,
4002 struct ocfs2_xattr_bucket *bucket,
4006 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
4007 int i, block_off, new_offset;
4008 const char *prefix, *name;
4010 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
4011 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
4012 type = ocfs2_xattr_get_type(entry);
4013 prefix = ocfs2_xattr_prefix(type);
4016 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
4024 name = (const char *)bucket_block(bucket, block_off) +
4026 ret = ocfs2_xattr_list_entry(xl->buffer,
4030 entry->xe_name_len);
4039 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4040 struct buffer_head *blk_bh,
4041 xattr_tree_rec_func *rec_func,
4044 struct ocfs2_xattr_block *xb =
4045 (struct ocfs2_xattr_block *)blk_bh->b_data;
4046 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4048 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4051 if (!el->l_next_free_rec || !rec_func)
4054 while (name_hash > 0) {
4055 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4056 &e_cpos, &num_clusters, el);
4062 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4063 num_clusters, para);
4073 name_hash = e_cpos - 1;
4080 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4081 struct buffer_head *root_bh,
4082 u64 blkno, u32 cpos, u32 len, void *para)
4084 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4085 ocfs2_list_xattr_bucket, para);
4088 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4089 struct buffer_head *blk_bh,
4094 struct ocfs2_xattr_tree_list xl = {
4096 .buffer_size = buffer_size,
4100 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4101 ocfs2_list_xattr_tree_rec, &xl);
4112 static int cmp_xe(const void *a, const void *b)
4114 const struct ocfs2_xattr_entry *l = a, *r = b;
4115 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4116 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4118 if (l_hash > r_hash)
4120 if (l_hash < r_hash)
4125 static void swap_xe(void *a, void *b, int size)
4127 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4130 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4131 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4135 * When the ocfs2_xattr_block is filled up, new bucket will be created
4136 * and all the xattr entries will be moved to the new bucket.
4137 * The header goes at the start of the bucket, and the names+values are
4138 * filled from the end. This is why *target starts as the last buffer.
4139 * Note: we need to sort the entries since they are not saved in order
4140 * in the ocfs2_xattr_block.
4142 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4143 struct buffer_head *xb_bh,
4144 struct ocfs2_xattr_bucket *bucket)
4146 int i, blocksize = inode->i_sb->s_blocksize;
4147 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4148 u16 offset, size, off_change;
4149 struct ocfs2_xattr_entry *xe;
4150 struct ocfs2_xattr_block *xb =
4151 (struct ocfs2_xattr_block *)xb_bh->b_data;
4152 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
4153 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4154 u16 count = le16_to_cpu(xb_xh->xh_count);
4155 char *src = xb_bh->b_data;
4156 char *target = bucket_block(bucket, blks - 1);
4158 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4159 (unsigned long long)xb_bh->b_blocknr,
4160 (unsigned long long)bucket_blkno(bucket));
4162 for (i = 0; i < blks; i++)
4163 memset(bucket_block(bucket, i), 0, blocksize);
4166 * Since the xe_name_offset is based on ocfs2_xattr_header,
4167 * there is a offset change corresponding to the change of
4168 * ocfs2_xattr_header's position.
4170 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4171 xe = &xb_xh->xh_entries[count - 1];
4172 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4173 size = blocksize - offset;
4175 /* copy all the names and values. */
4176 memcpy(target + offset, src + offset, size);
4178 /* Init new header now. */
4179 xh->xh_count = xb_xh->xh_count;
4180 xh->xh_num_buckets = cpu_to_le16(1);
4181 xh->xh_name_value_len = cpu_to_le16(size);
4182 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4184 /* copy all the entries. */
4185 target = bucket_block(bucket, 0);
4186 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4187 size = count * sizeof(struct ocfs2_xattr_entry);
4188 memcpy(target + offset, (char *)xb_xh + offset, size);
4190 /* Change the xe offset for all the xe because of the move. */
4191 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4192 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4193 for (i = 0; i < count; i++)
4194 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4196 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4197 offset, size, off_change);
4199 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4204 * After we move xattr from block to index btree, we have to
4205 * update ocfs2_xattr_search to the new xe and base.
4207 * When the entry is in xattr block, xattr_bh indicates the storage place.
4208 * While if the entry is in index b-tree, "bucket" indicates the
4209 * real place of the xattr.
4211 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4212 struct ocfs2_xattr_search *xs,
4213 struct buffer_head *old_bh)
4215 char *buf = old_bh->b_data;
4216 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4217 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
4220 xs->header = bucket_xh(xs->bucket);
4221 xs->base = bucket_block(xs->bucket, 0);
4222 xs->end = xs->base + inode->i_sb->s_blocksize;
4227 i = xs->here - old_xh->xh_entries;
4228 xs->here = &xs->header->xh_entries[i];
4231 static int ocfs2_xattr_create_index_block(struct inode *inode,
4232 struct ocfs2_xattr_search *xs,
4233 struct ocfs2_xattr_set_ctxt *ctxt)
4238 handle_t *handle = ctxt->handle;
4239 struct ocfs2_inode_info *oi = OCFS2_I(inode);
4240 struct buffer_head *xb_bh = xs->xattr_bh;
4241 struct ocfs2_xattr_block *xb =
4242 (struct ocfs2_xattr_block *)xb_bh->b_data;
4243 struct ocfs2_xattr_tree_root *xr;
4244 u16 xb_flags = le16_to_cpu(xb->xb_flags);
4246 mlog(0, "create xattr index block for %llu\n",
4247 (unsigned long long)xb_bh->b_blocknr);
4249 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
4250 BUG_ON(!xs->bucket);
4254 * We can use this lock for now, and maybe move to a dedicated mutex
4255 * if performance becomes a problem later.
4257 down_write(&oi->ip_alloc_sem);
4259 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
4260 OCFS2_JOURNAL_ACCESS_WRITE);
4266 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
4267 1, 1, &bit_off, &len);
4274 * The bucket may spread in many blocks, and
4275 * we will only touch the 1st block and the last block
4276 * in the whole bucket(one for entry and one for data).
4278 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4280 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4281 (unsigned long long)blkno);
4283 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
4289 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4290 OCFS2_JOURNAL_ACCESS_CREATE);
4296 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4297 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4299 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4301 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4302 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4303 offsetof(struct ocfs2_xattr_block, xb_attrs));
4305 xr = &xb->xb_attrs.xb_root;
4306 xr->xt_clusters = cpu_to_le32(1);
4307 xr->xt_last_eb_blk = 0;
4308 xr->xt_list.l_tree_depth = 0;
4309 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4310 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4312 xr->xt_list.l_recs[0].e_cpos = 0;
4313 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4314 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4316 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4318 ocfs2_journal_dirty(handle, xb_bh);
4321 up_write(&oi->ip_alloc_sem);
4326 static int cmp_xe_offset(const void *a, const void *b)
4328 const struct ocfs2_xattr_entry *l = a, *r = b;
4329 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4330 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4332 if (l_name_offset < r_name_offset)
4334 if (l_name_offset > r_name_offset)
4340 * defrag a xattr bucket if we find that the bucket has some
4341 * holes beteen name/value pairs.
4342 * We will move all the name/value pairs to the end of the bucket
4343 * so that we can spare some space for insertion.
4345 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
4347 struct ocfs2_xattr_bucket *bucket)
4350 size_t end, offset, len;
4351 struct ocfs2_xattr_header *xh;
4352 char *entries, *buf, *bucket_buf = NULL;
4353 u64 blkno = bucket_blkno(bucket);
4355 size_t blocksize = inode->i_sb->s_blocksize;
4356 struct ocfs2_xattr_entry *xe;
4359 * In order to make the operation more efficient and generic,
4360 * we copy all the blocks into a contiguous memory and do the
4361 * defragment there, so if anything is error, we will not touch
4364 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4371 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4372 memcpy(buf, bucket_block(bucket, i), blocksize);
4374 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4375 OCFS2_JOURNAL_ACCESS_WRITE);
4381 xh = (struct ocfs2_xattr_header *)bucket_buf;
4382 entries = (char *)xh->xh_entries;
4383 xh_free_start = le16_to_cpu(xh->xh_free_start);
4385 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4386 "xh_free_start = %u, xh_name_value_len = %u.\n",
4387 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4388 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4391 * sort all the entries by their offset.
4392 * the largest will be the first, so that we can
4393 * move them to the end one by one.
4395 sort(entries, le16_to_cpu(xh->xh_count),
4396 sizeof(struct ocfs2_xattr_entry),
4397 cmp_xe_offset, swap_xe);
4399 /* Move all name/values to the end of the bucket. */
4400 xe = xh->xh_entries;
4401 end = OCFS2_XATTR_BUCKET_SIZE;
4402 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4403 offset = le16_to_cpu(xe->xe_name_offset);
4404 len = namevalue_size_xe(xe);
4407 * We must make sure that the name/value pair
4408 * exist in the same block. So adjust end to
4409 * the previous block end if needed.
4411 if (((end - len) / blocksize !=
4412 (end - 1) / blocksize))
4413 end = end - end % blocksize;
4415 if (end > offset + len) {
4416 memmove(bucket_buf + end - len,
4417 bucket_buf + offset, len);
4418 xe->xe_name_offset = cpu_to_le16(end - len);
4421 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4422 "bucket %llu\n", (unsigned long long)blkno);
4427 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4428 "bucket %llu\n", (unsigned long long)blkno);
4430 if (xh_free_start == end)
4433 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4434 xh->xh_free_start = cpu_to_le16(end);
4436 /* sort the entries by their name_hash. */
4437 sort(entries, le16_to_cpu(xh->xh_count),
4438 sizeof(struct ocfs2_xattr_entry),
4442 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4443 memcpy(bucket_block(bucket, i), buf, blocksize);
4444 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4452 * prev_blkno points to the start of an existing extent. new_blkno
4453 * points to a newly allocated extent. Because we know each of our
4454 * clusters contains more than bucket, we can easily split one cluster
4455 * at a bucket boundary. So we take the last cluster of the existing
4456 * extent and split it down the middle. We move the last half of the
4457 * buckets in the last cluster of the existing extent over to the new
4460 * first_bh is the buffer at prev_blkno so we can update the existing
4461 * extent's bucket count. header_bh is the bucket were we were hoping
4462 * to insert our xattr. If the bucket move places the target in the new
4463 * extent, we'll update first_bh and header_bh after modifying the old
4466 * first_hash will be set as the 1st xe's name_hash in the new extent.
4468 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4470 struct ocfs2_xattr_bucket *first,
4471 struct ocfs2_xattr_bucket *target,
4477 struct super_block *sb = inode->i_sb;
4478 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4479 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4480 int to_move = num_buckets / 2;
4482 u64 last_cluster_blkno = bucket_blkno(first) +
4483 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4485 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4486 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4488 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4489 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
4491 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4492 last_cluster_blkno, new_blkno,
4493 to_move, first_hash);
4499 /* This is the first bucket that got moved */
4500 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4503 * If the target bucket was part of the moved buckets, we need to
4504 * update first and target.
4506 if (bucket_blkno(target) >= src_blkno) {
4507 /* Find the block for the new target bucket */
4508 src_blkno = new_blkno +
4509 (bucket_blkno(target) - src_blkno);
4511 ocfs2_xattr_bucket_relse(first);
4512 ocfs2_xattr_bucket_relse(target);
4515 * These shouldn't fail - the buffers are in the
4516 * journal from ocfs2_cp_xattr_bucket().
4518 ret = ocfs2_read_xattr_bucket(first, new_blkno);
4523 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4534 * Find the suitable pos when we divide a bucket into 2.
4535 * We have to make sure the xattrs with the same hash value exist
4536 * in the same bucket.
4538 * If this ocfs2_xattr_header covers more than one hash value, find a
4539 * place where the hash value changes. Try to find the most even split.
4540 * The most common case is that all entries have different hash values,
4541 * and the first check we make will find a place to split.
4543 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4545 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4546 int count = le16_to_cpu(xh->xh_count);
4547 int delta, middle = count / 2;
4550 * We start at the middle. Each step gets farther away in both
4551 * directions. We therefore hit the change in hash value
4552 * nearest to the middle. Note that this loop does not execute for
4555 for (delta = 0; delta < middle; delta++) {
4556 /* Let's check delta earlier than middle */
4557 if (cmp_xe(&entries[middle - delta - 1],
4558 &entries[middle - delta]))
4559 return middle - delta;
4561 /* For even counts, don't walk off the end */
4562 if ((middle + delta + 1) == count)
4565 /* Now try delta past middle */
4566 if (cmp_xe(&entries[middle + delta],
4567 &entries[middle + delta + 1]))
4568 return middle + delta + 1;
4571 /* Every entry had the same hash */
4576 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4577 * first_hash will record the 1st hash of the new bucket.
4579 * Normally half of the xattrs will be moved. But we have to make
4580 * sure that the xattrs with the same hash value are stored in the
4581 * same bucket. If all the xattrs in this bucket have the same hash
4582 * value, the new bucket will be initialized as an empty one and the
4583 * first_hash will be initialized as (hash_value+1).
4585 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4590 int new_bucket_head)
4593 int count, start, len, name_value_len = 0, name_offset = 0;
4594 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4595 struct ocfs2_xattr_header *xh;
4596 struct ocfs2_xattr_entry *xe;
4597 int blocksize = inode->i_sb->s_blocksize;
4599 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4600 (unsigned long long)blk, (unsigned long long)new_blk);
4602 s_bucket = ocfs2_xattr_bucket_new(inode);
4603 t_bucket = ocfs2_xattr_bucket_new(inode);
4604 if (!s_bucket || !t_bucket) {
4610 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4616 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4617 OCFS2_JOURNAL_ACCESS_WRITE);
4624 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4625 * there's no need to read it.
4627 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4634 * Hey, if we're overwriting t_bucket, what difference does
4635 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4636 * same part of ocfs2_cp_xattr_bucket().
4638 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4640 OCFS2_JOURNAL_ACCESS_CREATE :
4641 OCFS2_JOURNAL_ACCESS_WRITE);
4647 xh = bucket_xh(s_bucket);
4648 count = le16_to_cpu(xh->xh_count);
4649 start = ocfs2_xattr_find_divide_pos(xh);
4651 if (start == count) {
4652 xe = &xh->xh_entries[start-1];
4655 * initialized a new empty bucket here.
4656 * The hash value is set as one larger than
4657 * that of the last entry in the previous bucket.
4659 for (i = 0; i < t_bucket->bu_blocks; i++)
4660 memset(bucket_block(t_bucket, i), 0, blocksize);
4662 xh = bucket_xh(t_bucket);
4663 xh->xh_free_start = cpu_to_le16(blocksize);
4664 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4665 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4667 goto set_num_buckets;
4670 /* copy the whole bucket to the new first. */
4671 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4673 /* update the new bucket. */
4674 xh = bucket_xh(t_bucket);
4677 * Calculate the total name/value len and xh_free_start for
4678 * the old bucket first.
4680 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4682 for (i = 0; i < start; i++) {
4683 xe = &xh->xh_entries[i];
4684 name_value_len += namevalue_size_xe(xe);
4685 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4686 name_offset = le16_to_cpu(xe->xe_name_offset);
4690 * Now begin the modification to the new bucket.
4692 * In the new bucket, We just move the xattr entry to the beginning
4693 * and don't touch the name/value. So there will be some holes in the
4694 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4697 xe = &xh->xh_entries[start];
4698 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4699 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
4700 (int)((char *)xe - (char *)xh),
4701 (int)((char *)xh->xh_entries - (char *)xh));
4702 memmove((char *)xh->xh_entries, (char *)xe, len);
4703 xe = &xh->xh_entries[count - start];
4704 len = sizeof(struct ocfs2_xattr_entry) * start;
4705 memset((char *)xe, 0, len);
4707 le16_add_cpu(&xh->xh_count, -start);
4708 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4710 /* Calculate xh_free_start for the new bucket. */
4711 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4712 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4713 xe = &xh->xh_entries[i];
4714 if (le16_to_cpu(xe->xe_name_offset) <
4715 le16_to_cpu(xh->xh_free_start))
4716 xh->xh_free_start = xe->xe_name_offset;
4720 /* set xh->xh_num_buckets for the new xh. */
4721 if (new_bucket_head)
4722 xh->xh_num_buckets = cpu_to_le16(1);
4724 xh->xh_num_buckets = 0;
4726 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4728 /* store the first_hash of the new bucket. */
4730 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4733 * Now only update the 1st block of the old bucket. If we
4734 * just added a new empty bucket, there is no need to modify
4740 xh = bucket_xh(s_bucket);
4741 memset(&xh->xh_entries[start], 0,
4742 sizeof(struct ocfs2_xattr_entry) * (count - start));
4743 xh->xh_count = cpu_to_le16(start);
4744 xh->xh_free_start = cpu_to_le16(name_offset);
4745 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4747 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4750 ocfs2_xattr_bucket_free(s_bucket);
4751 ocfs2_xattr_bucket_free(t_bucket);
4757 * Copy xattr from one bucket to another bucket.
4759 * The caller must make sure that the journal transaction
4760 * has enough space for journaling.
4762 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4769 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4771 BUG_ON(s_blkno == t_blkno);
4773 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4774 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4777 s_bucket = ocfs2_xattr_bucket_new(inode);
4778 t_bucket = ocfs2_xattr_bucket_new(inode);
4779 if (!s_bucket || !t_bucket) {
4785 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4790 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4791 * there's no need to read it.
4793 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4798 * Hey, if we're overwriting t_bucket, what difference does
4799 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4800 * cluster to fill, we came here from
4801 * ocfs2_mv_xattr_buckets(), and it is really new -
4802 * ACCESS_CREATE is required. But we also might have moved data
4803 * out of t_bucket before extending back into it.
4804 * ocfs2_add_new_xattr_bucket() can do this - its call to
4805 * ocfs2_add_new_xattr_cluster() may have created a new extent
4806 * and copied out the end of the old extent. Then it re-extends
4807 * the old extent back to create space for new xattrs. That's
4808 * how we get here, and the bucket isn't really new.
4810 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4812 OCFS2_JOURNAL_ACCESS_CREATE :
4813 OCFS2_JOURNAL_ACCESS_WRITE);
4817 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4818 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4821 ocfs2_xattr_bucket_free(t_bucket);
4822 ocfs2_xattr_bucket_free(s_bucket);
4828 * src_blk points to the start of an existing extent. last_blk points to
4829 * last cluster in that extent. to_blk points to a newly allocated
4830 * extent. We copy the buckets from the cluster at last_blk to the new
4831 * extent. If start_bucket is non-zero, we skip that many buckets before
4832 * we start copying. The new extent's xh_num_buckets gets set to the
4833 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4834 * by the same amount.
4836 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4837 u64 src_blk, u64 last_blk, u64 to_blk,
4838 unsigned int start_bucket,
4841 int i, ret, credits;
4842 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4843 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4844 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4845 struct ocfs2_xattr_bucket *old_first, *new_first;
4847 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4848 (unsigned long long)last_blk, (unsigned long long)to_blk);
4850 BUG_ON(start_bucket >= num_buckets);
4852 num_buckets -= start_bucket;
4853 last_blk += (start_bucket * blks_per_bucket);
4856 /* The first bucket of the original extent */
4857 old_first = ocfs2_xattr_bucket_new(inode);
4858 /* The first bucket of the new extent */
4859 new_first = ocfs2_xattr_bucket_new(inode);
4860 if (!old_first || !new_first) {
4866 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4873 * We need to update the first bucket of the old extent and all
4874 * the buckets going to the new extent.
4876 credits = ((num_buckets + 1) * blks_per_bucket);
4877 ret = ocfs2_extend_trans(handle, credits);
4883 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4884 OCFS2_JOURNAL_ACCESS_WRITE);
4890 for (i = 0; i < num_buckets; i++) {
4891 ret = ocfs2_cp_xattr_bucket(inode, handle,
4892 last_blk + (i * blks_per_bucket),
4893 to_blk + (i * blks_per_bucket),
4902 * Get the new bucket ready before we dirty anything
4903 * (This actually shouldn't fail, because we already dirtied
4904 * it once in ocfs2_cp_xattr_bucket()).
4906 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4911 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4912 OCFS2_JOURNAL_ACCESS_WRITE);
4918 /* Now update the headers */
4919 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4920 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4922 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4923 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4926 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4929 ocfs2_xattr_bucket_free(new_first);
4930 ocfs2_xattr_bucket_free(old_first);
4935 * Move some xattrs in this cluster to the new cluster.
4936 * This function should only be called when bucket size == cluster size.
4937 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4939 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4945 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4946 int ret, credits = 2 * blk_per_bucket;
4948 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4950 ret = ocfs2_extend_trans(handle, credits);
4956 /* Move half of the xattr in start_blk to the next bucket. */
4957 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4958 new_blk, first_hash, 1);
4962 * Move some xattrs from the old cluster to the new one since they are not
4963 * contiguous in ocfs2 xattr tree.
4965 * new_blk starts a new separate cluster, and we will move some xattrs from
4966 * prev_blk to it. v_start will be set as the first name hash value in this
4967 * new cluster so that it can be used as e_cpos during tree insertion and
4968 * don't collide with our original b-tree operations. first_bh and header_bh
4969 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4970 * to extend the insert bucket.
4972 * The problem is how much xattr should we move to the new one and when should
4973 * we update first_bh and header_bh?
4974 * 1. If cluster size > bucket size, that means the previous cluster has more
4975 * than 1 bucket, so just move half nums of bucket into the new cluster and
4976 * update the first_bh and header_bh if the insert bucket has been moved
4977 * to the new cluster.
4978 * 2. If cluster_size == bucket_size:
4979 * a) If the previous extent rec has more than one cluster and the insert
4980 * place isn't in the last cluster, copy the entire last cluster to the
4981 * new one. This time, we don't need to upate the first_bh and header_bh
4982 * since they will not be moved into the new cluster.
4983 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4984 * the new one. And we set the extend flag to zero if the insert place is
4985 * moved into the new allocated cluster since no extend is needed.
4987 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4989 struct ocfs2_xattr_bucket *first,
4990 struct ocfs2_xattr_bucket *target,
4998 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4999 (unsigned long long)bucket_blkno(first), prev_clusters,
5000 (unsigned long long)new_blk);
5002 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
5003 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
5012 /* The start of the last cluster in the first extent */
5013 u64 last_blk = bucket_blkno(first) +
5014 ((prev_clusters - 1) *
5015 ocfs2_clusters_to_blocks(inode->i_sb, 1));
5017 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
5018 ret = ocfs2_mv_xattr_buckets(inode, handle,
5019 bucket_blkno(first),
5020 last_blk, new_blk, 0,
5025 ret = ocfs2_divide_xattr_cluster(inode, handle,
5031 if ((bucket_blkno(target) == last_blk) && extend)
5040 * Add a new cluster for xattr storage.
5042 * If the new cluster is contiguous with the previous one, it will be
5043 * appended to the same extent record, and num_clusters will be updated.
5044 * If not, we will insert a new extent for it and move some xattrs in
5045 * the last cluster into the new allocated one.
5046 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5047 * lose the benefits of hashing because we'll have to search large leaves.
5048 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5051 * first_bh is the first block of the previous extent rec and header_bh
5052 * indicates the bucket we will insert the new xattrs. They will be updated
5053 * when the header_bh is moved into the new cluster.
5055 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5056 struct buffer_head *root_bh,
5057 struct ocfs2_xattr_bucket *first,
5058 struct ocfs2_xattr_bucket *target,
5062 struct ocfs2_xattr_set_ctxt *ctxt)
5065 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
5066 u32 prev_clusters = *num_clusters;
5067 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5069 handle_t *handle = ctxt->handle;
5070 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5071 struct ocfs2_extent_tree et;
5073 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5074 "previous xattr blkno = %llu\n",
5075 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5076 prev_cpos, (unsigned long long)bucket_blkno(first));
5078 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5080 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5081 OCFS2_JOURNAL_ACCESS_WRITE);
5087 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1,
5088 clusters_to_add, &bit_off, &num_bits);
5095 BUG_ON(num_bits > clusters_to_add);
5097 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5098 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5099 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5101 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
5102 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5103 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5105 * If this cluster is contiguous with the old one and
5106 * adding this new cluster, we don't surpass the limit of
5107 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5108 * initialized and used like other buckets in the previous
5110 * So add it as a contiguous one. The caller will handle
5113 v_start = prev_cpos + prev_clusters;
5114 *num_clusters = prev_clusters + num_bits;
5115 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5118 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5132 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
5133 num_bits, (unsigned long long)block, v_start);
5134 ret = ocfs2_insert_extent(handle, &et, v_start, block,
5135 num_bits, 0, ctxt->meta_ac);
5141 ocfs2_journal_dirty(handle, root_bh);
5148 * We are given an extent. 'first' is the bucket at the very front of
5149 * the extent. The extent has space for an additional bucket past
5150 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5151 * of the target bucket. We wish to shift every bucket past the target
5152 * down one, filling in that additional space. When we get back to the
5153 * target, we split the target between itself and the now-empty bucket
5154 * at target+1 (aka, target_blkno + blks_per_bucket).
5156 static int ocfs2_extend_xattr_bucket(struct inode *inode,
5158 struct ocfs2_xattr_bucket *first,
5163 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5164 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5166 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
5168 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
5169 "from %llu, len = %u\n", (unsigned long long)target_blk,
5170 (unsigned long long)bucket_blkno(first), num_clusters);
5172 /* The extent must have room for an additional bucket */
5173 BUG_ON(new_bucket >=
5174 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
5176 /* end_blk points to the last existing bucket */
5177 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
5180 * end_blk is the start of the last existing bucket.
5181 * Thus, (end_blk - target_blk) covers the target bucket and
5182 * every bucket after it up to, but not including, the last
5183 * existing bucket. Then we add the last existing bucket, the
5184 * new bucket, and the first bucket (3 * blk_per_bucket).
5186 credits = (end_blk - target_blk) + (3 * blk_per_bucket);
5187 ret = ocfs2_extend_trans(handle, credits);
5193 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5194 OCFS2_JOURNAL_ACCESS_WRITE);
5200 while (end_blk != target_blk) {
5201 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5202 end_blk + blk_per_bucket, 0);
5205 end_blk -= blk_per_bucket;
5208 /* Move half of the xattr in target_blkno to the next bucket. */
5209 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5210 target_blk + blk_per_bucket, NULL, 0);
5212 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5213 ocfs2_xattr_bucket_journal_dirty(handle, first);
5220 * Add new xattr bucket in an extent record and adjust the buckets
5221 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5222 * bucket we want to insert into.
5224 * In the easy case, we will move all the buckets after target down by
5225 * one. Half of target's xattrs will be moved to the next bucket.
5227 * If current cluster is full, we'll allocate a new one. This may not
5228 * be contiguous. The underlying calls will make sure that there is
5229 * space for the insert, shifting buckets around if necessary.
5230 * 'target' may be moved by those calls.
5232 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5233 struct buffer_head *xb_bh,
5234 struct ocfs2_xattr_bucket *target,
5235 struct ocfs2_xattr_set_ctxt *ctxt)
5237 struct ocfs2_xattr_block *xb =
5238 (struct ocfs2_xattr_block *)xb_bh->b_data;
5239 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5240 struct ocfs2_extent_list *el = &xb_root->xt_list;
5242 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
5243 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5244 int ret, num_buckets, extend = 1;
5246 u32 e_cpos, num_clusters;
5247 /* The bucket at the front of the extent */
5248 struct ocfs2_xattr_bucket *first;
5250 mlog(0, "Add new xattr bucket starting from %llu\n",
5251 (unsigned long long)bucket_blkno(target));
5253 /* The first bucket of the original extent */
5254 first = ocfs2_xattr_bucket_new(inode);
5261 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5268 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5274 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5275 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5277 * This can move first+target if the target bucket moves
5278 * to the new extent.
5280 ret = ocfs2_add_new_xattr_cluster(inode,
5295 ret = ocfs2_extend_xattr_bucket(inode,
5298 bucket_blkno(target),
5305 ocfs2_xattr_bucket_free(first);
5310 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5311 struct ocfs2_xattr_bucket *bucket,
5314 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5316 offs = offs % inode->i_sb->s_blocksize;
5317 return bucket_block(bucket, block_off) + offs;
5321 * Truncate the specified xe_off entry in xattr bucket.
5322 * bucket is indicated by header_bh and len is the new length.
5323 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5325 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5327 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5328 struct ocfs2_xattr_bucket *bucket,
5331 struct ocfs2_xattr_set_ctxt *ctxt)
5335 struct ocfs2_xattr_entry *xe;
5336 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5337 size_t blocksize = inode->i_sb->s_blocksize;
5338 struct ocfs2_xattr_value_buf vb = {
5339 .vb_access = ocfs2_journal_access,
5342 xe = &xh->xh_entries[xe_off];
5344 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5346 offset = le16_to_cpu(xe->xe_name_offset) +
5347 OCFS2_XATTR_SIZE(xe->xe_name_len);
5349 value_blk = offset / blocksize;
5351 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5352 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
5354 vb.vb_bh = bucket->bu_bhs[value_blk];
5357 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5358 (vb.vb_bh->b_data + offset % blocksize);
5361 * From here on out we have to dirty the bucket. The generic
5362 * value calls only modify one of the bucket's bhs, but we need
5363 * to send the bucket at once. So if they error, they *could* have
5364 * modified something. We have to assume they did, and dirty
5365 * the whole bucket. This leaves us in a consistent state.
5367 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5368 xe_off, (unsigned long long)bucket_blkno(bucket), len);
5369 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5375 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5376 OCFS2_JOURNAL_ACCESS_WRITE);
5382 xe->xe_value_size = cpu_to_le64(len);
5384 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5390 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5391 struct buffer_head *root_bh,
5398 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5399 struct inode *tl_inode = osb->osb_tl_inode;
5401 struct ocfs2_xattr_block *xb =
5402 (struct ocfs2_xattr_block *)root_bh->b_data;
5403 struct ocfs2_alloc_context *meta_ac = NULL;
5404 struct ocfs2_cached_dealloc_ctxt dealloc;
5405 struct ocfs2_extent_tree et;
5407 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5408 ocfs2_delete_xattr_in_bucket, para);
5414 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5416 ocfs2_init_dealloc_ctxt(&dealloc);
5418 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5419 cpos, len, (unsigned long long)blkno);
5421 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5424 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5430 mutex_lock(&tl_inode->i_mutex);
5432 if (ocfs2_truncate_log_needs_flush(osb)) {
5433 ret = __ocfs2_flush_truncate_log(osb);
5440 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5441 if (IS_ERR(handle)) {
5447 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5448 OCFS2_JOURNAL_ACCESS_WRITE);
5454 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5461 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5462 ocfs2_journal_dirty(handle, root_bh);
5464 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5469 ocfs2_commit_trans(osb, handle);
5471 ocfs2_schedule_truncate_log_flush(osb, 1);
5473 mutex_unlock(&tl_inode->i_mutex);
5476 ocfs2_free_alloc_context(meta_ac);
5478 ocfs2_run_deallocs(osb, &dealloc);
5484 * check whether the xattr bucket is filled up with the same hash value.
5485 * If we want to insert the xattr with the same hash, return -ENOSPC.
5486 * If we want to insert a xattr with different hash value, go ahead
5487 * and ocfs2_divide_xattr_bucket will handle this.
5489 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5490 struct ocfs2_xattr_bucket *bucket,
5493 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5494 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5496 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5499 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5500 xh->xh_entries[0].xe_name_hash) {
5501 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5503 (unsigned long long)bucket_blkno(bucket),
5504 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5512 * Try to set the entry in the current bucket. If we fail, the caller
5513 * will handle getting us another bucket.
5515 static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
5516 struct ocfs2_xattr_info *xi,
5517 struct ocfs2_xattr_search *xs,
5518 struct ocfs2_xattr_set_ctxt *ctxt)
5521 struct ocfs2_xa_loc loc;
5523 mlog_entry("Set xattr %s in xattr bucket\n", xi->xi_name);
5525 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5526 xs->not_found ? NULL : xs->here);
5527 ret = ocfs2_xa_set(&loc, xi, ctxt);
5529 xs->here = loc.xl_entry;
5532 if (ret != -ENOSPC) {
5537 /* Ok, we need space. Let's try defragmenting the bucket. */
5538 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5545 ret = ocfs2_xa_set(&loc, xi, ctxt);
5547 xs->here = loc.xl_entry;
5559 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5560 struct ocfs2_xattr_info *xi,
5561 struct ocfs2_xattr_search *xs,
5562 struct ocfs2_xattr_set_ctxt *ctxt)
5566 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
5568 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5571 if (ret != -ENOSPC) {
5576 /* Ack, need more space. Let's try to get another bucket! */
5579 * We do not allow for overlapping ranges between buckets. And
5580 * the maximum number of collisions we will allow for then is
5581 * one bucket's worth, so check it here whether we need to
5582 * add a new bucket for the insert.
5584 ret = ocfs2_check_xattr_bucket_collision(inode,
5592 ret = ocfs2_add_new_xattr_bucket(inode,
5602 * ocfs2_add_new_xattr_bucket() will have updated
5603 * xs->bucket if it moved, but it will not have updated
5604 * any of the other search fields. Thus, we drop it and
5605 * re-search. Everything should be cached, so it'll be
5608 ocfs2_xattr_bucket_relse(xs->bucket);
5609 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5612 if (ret && ret != -ENODATA)
5614 xs->not_found = ret;
5616 /* Ok, we have a new bucket, let's try again */
5617 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5618 if (ret && (ret != -ENOSPC))
5626 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5627 struct ocfs2_xattr_bucket *bucket,
5630 int ret = 0, ref_credits;
5631 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5633 struct ocfs2_xattr_entry *xe;
5634 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5635 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5636 int credits = ocfs2_remove_extent_credits(osb->sb) +
5637 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5638 struct ocfs2_xattr_value_root *xv;
5639 struct ocfs2_rm_xattr_bucket_para *args =
5640 (struct ocfs2_rm_xattr_bucket_para *)para;
5642 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5644 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5645 xe = &xh->xh_entries[i];
5646 if (ocfs2_xattr_is_local(xe))
5649 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5652 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5658 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5659 if (IS_ERR(ctxt.handle)) {
5660 ret = PTR_ERR(ctxt.handle);
5665 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5668 ocfs2_commit_trans(osb, ctxt.handle);
5670 ocfs2_free_alloc_context(ctxt.meta_ac);
5671 ctxt.meta_ac = NULL;
5680 ocfs2_free_alloc_context(ctxt.meta_ac);
5681 ocfs2_schedule_truncate_log_flush(osb, 1);
5682 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5687 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5688 * or change the extent record flag), we need to recalculate
5689 * the metaecc for the whole bucket. So it is done here.
5692 * We have to give the extra credits for the caller.
5694 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5699 struct ocfs2_xattr_bucket *bucket =
5700 (struct ocfs2_xattr_bucket *)para;
5702 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5703 OCFS2_JOURNAL_ACCESS_WRITE);
5709 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5715 * Special action we need if the xattr value is refcounted.
5717 * 1. If the xattr is refcounted, lock the tree.
5718 * 2. CoW the xattr if we are setting the new value and the value
5719 * will be stored outside.
5720 * 3. In other case, decrease_refcount will work for us, so just
5721 * lock the refcount tree, calculate the meta and credits is OK.
5723 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5724 * currently CoW is a completed transaction, while this function
5725 * will also lock the allocators and let us deadlock. So we will
5726 * CoW the whole xattr value.
5728 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5729 struct ocfs2_dinode *di,
5730 struct ocfs2_xattr_info *xi,
5731 struct ocfs2_xattr_search *xis,
5732 struct ocfs2_xattr_search *xbs,
5733 struct ocfs2_refcount_tree **ref_tree,
5738 struct ocfs2_xattr_block *xb;
5739 struct ocfs2_xattr_entry *xe;
5741 u32 p_cluster, num_clusters;
5742 unsigned int ext_flags;
5743 int name_offset, name_len;
5744 struct ocfs2_xattr_value_buf vb;
5745 struct ocfs2_xattr_bucket *bucket = NULL;
5746 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5747 struct ocfs2_post_refcount refcount;
5748 struct ocfs2_post_refcount *p = NULL;
5749 struct buffer_head *ref_root_bh = NULL;
5751 if (!xis->not_found) {
5753 name_offset = le16_to_cpu(xe->xe_name_offset);
5754 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5756 vb.vb_bh = xis->inode_bh;
5757 vb.vb_access = ocfs2_journal_access_di;
5759 int i, block_off = 0;
5760 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5762 name_offset = le16_to_cpu(xe->xe_name_offset);
5763 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5764 i = xbs->here - xbs->header->xh_entries;
5766 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5767 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5768 bucket_xh(xbs->bucket),
5775 base = bucket_block(xbs->bucket, block_off);
5776 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5777 vb.vb_access = ocfs2_journal_access;
5779 if (ocfs2_meta_ecc(osb)) {
5780 /*create parameters for ocfs2_post_refcount. */
5781 bucket = xbs->bucket;
5782 refcount.credits = bucket->bu_blocks;
5783 refcount.para = bucket;
5785 ocfs2_xattr_bucket_post_refcount;
5790 vb.vb_bh = xbs->xattr_bh;
5791 vb.vb_access = ocfs2_journal_access_xb;
5795 if (ocfs2_xattr_is_local(xe))
5798 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5799 (base + name_offset + name_len);
5801 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5802 &num_clusters, &vb.vb_xv->xr_list,
5810 * We just need to check the 1st extent record, since we always
5811 * CoW the whole xattr. So there shouldn't be a xattr with
5812 * some REFCOUNT extent recs after the 1st one.
5814 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5817 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5818 1, ref_tree, &ref_root_bh);
5825 * If we are deleting the xattr or the new size will be stored inside,
5826 * cool, leave it there, the xattr truncate process will remove them
5827 * for us(it still needs the refcount tree lock and the meta, credits).
5828 * And the worse case is that every cluster truncate will split the
5829 * refcount tree, and make the original extent become 3. So we will need
5830 * 2 * cluster more extent recs at most.
5832 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
5834 ret = ocfs2_refcounted_xattr_delete_need(inode,
5835 &(*ref_tree)->rf_ci,
5836 ref_root_bh, vb.vb_xv,
5843 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5844 *ref_tree, ref_root_bh, 0,
5845 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5850 brelse(ref_root_bh);
5855 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5856 * The physical clusters will be added to refcount tree.
5858 static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5859 struct ocfs2_xattr_value_root *xv,
5860 struct ocfs2_extent_tree *value_et,
5861 struct ocfs2_caching_info *ref_ci,
5862 struct buffer_head *ref_root_bh,
5863 struct ocfs2_cached_dealloc_ctxt *dealloc,
5864 struct ocfs2_post_refcount *refcount)
5867 u32 clusters = le32_to_cpu(xv->xr_clusters);
5868 u32 cpos, p_cluster, num_clusters;
5869 struct ocfs2_extent_list *el = &xv->xr_list;
5870 unsigned int ext_flags;
5873 while (cpos < clusters) {
5874 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5875 &num_clusters, el, &ext_flags);
5877 cpos += num_clusters;
5878 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5883 ret = ocfs2_add_refcount_flag(inode, value_et,
5884 ref_ci, ref_root_bh,
5885 cpos - num_clusters,
5886 p_cluster, num_clusters,
5898 * Given a normal ocfs2_xattr_header, refcount all the entries which
5899 * have value stored outside.
5900 * Used for xattrs stored in inode and ocfs2_xattr_block.
5902 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5903 struct ocfs2_xattr_value_buf *vb,
5904 struct ocfs2_xattr_header *header,
5905 struct ocfs2_caching_info *ref_ci,
5906 struct buffer_head *ref_root_bh,
5907 struct ocfs2_cached_dealloc_ctxt *dealloc)
5910 struct ocfs2_xattr_entry *xe;
5911 struct ocfs2_xattr_value_root *xv;
5912 struct ocfs2_extent_tree et;
5915 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5916 xe = &header->xh_entries[i];
5918 if (ocfs2_xattr_is_local(xe))
5921 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5922 le16_to_cpu(xe->xe_name_offset) +
5923 OCFS2_XATTR_SIZE(xe->xe_name_len));
5926 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5928 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5929 ref_ci, ref_root_bh,
5940 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5941 struct buffer_head *fe_bh,
5942 struct ocfs2_caching_info *ref_ci,
5943 struct buffer_head *ref_root_bh,
5944 struct ocfs2_cached_dealloc_ctxt *dealloc)
5946 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5947 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5948 (fe_bh->b_data + inode->i_sb->s_blocksize -
5949 le16_to_cpu(di->i_xattr_inline_size));
5950 struct ocfs2_xattr_value_buf vb = {
5952 .vb_access = ocfs2_journal_access_di,
5955 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5956 ref_ci, ref_root_bh, dealloc);
5959 struct ocfs2_xattr_tree_value_refcount_para {
5960 struct ocfs2_caching_info *ref_ci;
5961 struct buffer_head *ref_root_bh;
5962 struct ocfs2_cached_dealloc_ctxt *dealloc;
5965 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5966 struct ocfs2_xattr_bucket *bucket,
5968 struct ocfs2_xattr_value_root **xv,
5969 struct buffer_head **bh)
5971 int ret, block_off, name_offset;
5972 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5973 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5976 ret = ocfs2_xattr_bucket_get_name_value(sb,
5986 base = bucket_block(bucket, block_off);
5988 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
5989 OCFS2_XATTR_SIZE(xe->xe_name_len));
5992 *bh = bucket->bu_bhs[block_off];
5998 * For a given xattr bucket, refcount all the entries which
5999 * have value stored outside.
6001 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6002 struct ocfs2_xattr_bucket *bucket,
6006 struct ocfs2_extent_tree et;
6007 struct ocfs2_xattr_tree_value_refcount_para *ref =
6008 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6009 struct ocfs2_xattr_header *xh =
6010 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6011 struct ocfs2_xattr_entry *xe;
6012 struct ocfs2_xattr_value_buf vb = {
6013 .vb_access = ocfs2_journal_access,
6015 struct ocfs2_post_refcount refcount = {
6016 .credits = bucket->bu_blocks,
6018 .func = ocfs2_xattr_bucket_post_refcount,
6020 struct ocfs2_post_refcount *p = NULL;
6022 /* We only need post_refcount if we support metaecc. */
6023 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6026 mlog(0, "refcount bucket %llu, count = %u\n",
6027 (unsigned long long)bucket_blkno(bucket),
6028 le16_to_cpu(xh->xh_count));
6029 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6030 xe = &xh->xh_entries[i];
6032 if (ocfs2_xattr_is_local(xe))
6035 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6036 &vb.vb_xv, &vb.vb_bh);
6042 ocfs2_init_xattr_value_extent_tree(&et,
6043 INODE_CACHE(inode), &vb);
6045 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6059 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6060 struct buffer_head *root_bh,
6061 u64 blkno, u32 cpos, u32 len, void *para)
6063 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6064 ocfs2_xattr_bucket_value_refcount,
6068 static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6069 struct buffer_head *blk_bh,
6070 struct ocfs2_caching_info *ref_ci,
6071 struct buffer_head *ref_root_bh,
6072 struct ocfs2_cached_dealloc_ctxt *dealloc)
6075 struct ocfs2_xattr_block *xb =
6076 (struct ocfs2_xattr_block *)blk_bh->b_data;
6078 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6079 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6080 struct ocfs2_xattr_value_buf vb = {
6082 .vb_access = ocfs2_journal_access_xb,
6085 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6086 ref_ci, ref_root_bh,
6089 struct ocfs2_xattr_tree_value_refcount_para para = {
6091 .ref_root_bh = ref_root_bh,
6095 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6096 ocfs2_refcount_xattr_tree_rec,
6103 int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6104 struct buffer_head *fe_bh,
6105 struct ocfs2_caching_info *ref_ci,
6106 struct buffer_head *ref_root_bh,
6107 struct ocfs2_cached_dealloc_ctxt *dealloc)
6110 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6111 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6112 struct buffer_head *blk_bh = NULL;
6114 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6115 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6116 ref_ci, ref_root_bh,
6124 if (!di->i_xattr_loc)
6127 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6134 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6135 ref_root_bh, dealloc);
6145 typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
6147 * Store the information we need in xattr reflink.
6148 * old_bh and new_bh are inode bh for the old and new inode.
6150 struct ocfs2_xattr_reflink {
6151 struct inode *old_inode;
6152 struct inode *new_inode;
6153 struct buffer_head *old_bh;
6154 struct buffer_head *new_bh;
6155 struct ocfs2_caching_info *ref_ci;
6156 struct buffer_head *ref_root_bh;
6157 struct ocfs2_cached_dealloc_ctxt *dealloc;
6158 should_xattr_reflinked *xattr_reflinked;
6162 * Given a xattr header and xe offset,
6163 * return the proper xv and the corresponding bh.
6164 * xattr in inode, block and xattr tree have different implementaions.
6166 typedef int (get_xattr_value_root)(struct super_block *sb,
6167 struct buffer_head *bh,
6168 struct ocfs2_xattr_header *xh,
6170 struct ocfs2_xattr_value_root **xv,
6171 struct buffer_head **ret_bh,
6175 * Calculate all the xattr value root metadata stored in this xattr header and
6176 * credits we need if we create them from the scratch.
6177 * We use get_xattr_value_root so that all types of xattr container can use it.
6179 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6180 struct buffer_head *bh,
6181 struct ocfs2_xattr_header *xh,
6182 int *metas, int *credits,
6184 get_xattr_value_root *func,
6188 struct ocfs2_xattr_value_root *xv;
6189 struct ocfs2_xattr_entry *xe;
6191 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6192 xe = &xh->xh_entries[i];
6193 if (ocfs2_xattr_is_local(xe))
6196 ret = func(sb, bh, xh, i, &xv, NULL, para);
6202 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6203 le16_to_cpu(xv->xr_list.l_next_free_rec);
6205 *credits += ocfs2_calc_extend_credits(sb,
6207 le32_to_cpu(xv->xr_clusters));
6210 * If the value is a tree with depth > 1, We don't go deep
6211 * to the extent block, so just calculate a maximum record num.
6213 if (!xv->xr_list.l_tree_depth)
6214 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
6216 *num_recs += ocfs2_clusters_for_bytes(sb,
6223 /* Used by xattr inode and block to return the right xv and buffer_head. */
6224 static int ocfs2_get_xattr_value_root(struct super_block *sb,
6225 struct buffer_head *bh,
6226 struct ocfs2_xattr_header *xh,
6228 struct ocfs2_xattr_value_root **xv,
6229 struct buffer_head **ret_bh,
6232 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6234 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6235 le16_to_cpu(xe->xe_name_offset) +
6236 OCFS2_XATTR_SIZE(xe->xe_name_len));
6245 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6246 * It is only used for inline xattr and xattr block.
6248 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6249 struct ocfs2_xattr_header *xh,
6250 struct buffer_head *ref_root_bh,
6252 struct ocfs2_alloc_context **meta_ac)
6254 int ret, meta_add = 0, num_recs = 0;
6255 struct ocfs2_refcount_block *rb =
6256 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6260 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6261 &meta_add, credits, &num_recs,
6262 ocfs2_get_xattr_value_root,
6270 * We need to add/modify num_recs in refcount tree, so just calculate
6271 * an approximate number we need for refcount tree change.
6272 * Sometimes we need to split the tree, and after split, half recs
6273 * will be moved to the new block, and a new block can only provide
6274 * half number of recs. So we multiple new blocks by 2.
6276 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6277 meta_add += num_recs;
6278 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6279 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6280 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6281 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6285 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6294 * Given a xattr header, reflink all the xattrs in this container.
6295 * It can be used for inode, block and bucket.
6298 * Before we call this function, the caller has memcpy the xattr in
6299 * old_xh to the new_xh.
6301 * If args.xattr_reflinked is set, call it to decide whether the xe should
6302 * be reflinked or not. If not, remove it from the new xattr header.
6304 static int ocfs2_reflink_xattr_header(handle_t *handle,
6305 struct ocfs2_xattr_reflink *args,
6306 struct buffer_head *old_bh,
6307 struct ocfs2_xattr_header *xh,
6308 struct buffer_head *new_bh,
6309 struct ocfs2_xattr_header *new_xh,
6310 struct ocfs2_xattr_value_buf *vb,
6311 struct ocfs2_alloc_context *meta_ac,
6312 get_xattr_value_root *func,
6316 struct super_block *sb = args->old_inode->i_sb;
6317 struct buffer_head *value_bh;
6318 struct ocfs2_xattr_entry *xe, *last;
6319 struct ocfs2_xattr_value_root *xv, *new_xv;
6320 struct ocfs2_extent_tree data_et;
6321 u32 clusters, cpos, p_cluster, num_clusters;
6322 unsigned int ext_flags = 0;
6324 mlog(0, "reflink xattr in container %llu, count = %u\n",
6325 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
6327 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6328 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
6329 xe = &xh->xh_entries[i];
6331 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6332 xe = &new_xh->xh_entries[j];
6334 le16_add_cpu(&new_xh->xh_count, -1);
6335 if (new_xh->xh_count) {
6337 (void *)last - (void *)xe);
6339 sizeof(struct ocfs2_xattr_entry));
6343 * We don't want j to increase in the next round since
6344 * it is already moved ahead.
6350 if (ocfs2_xattr_is_local(xe))
6353 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6359 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6366 * For the xattr which has l_tree_depth = 0, all the extent
6367 * recs have already be copied to the new xh with the
6368 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6369 * increase the refount count int the refcount tree.
6371 * For the xattr which has l_tree_depth > 0, we need
6372 * to initialize it to the empty default value root,
6373 * and then insert the extents one by one.
6375 if (xv->xr_list.l_tree_depth) {
6376 memcpy(new_xv, &def_xv, sizeof(def_xv));
6378 vb->vb_bh = value_bh;
6379 ocfs2_init_xattr_value_extent_tree(&data_et,
6380 INODE_CACHE(args->new_inode), vb);
6383 clusters = le32_to_cpu(xv->xr_clusters);
6385 while (cpos < clusters) {
6386 ret = ocfs2_xattr_get_clusters(args->old_inode,
6399 if (xv->xr_list.l_tree_depth) {
6400 ret = ocfs2_insert_extent(handle,
6402 ocfs2_clusters_to_blocks(
6403 args->old_inode->i_sb,
6405 num_clusters, ext_flags,
6413 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6415 p_cluster, num_clusters,
6416 meta_ac, args->dealloc);
6422 cpos += num_clusters;
6430 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6432 int ret = 0, credits = 0;
6434 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6435 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6436 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6437 int header_off = osb->sb->s_blocksize - inline_size;
6438 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6439 (args->old_bh->b_data + header_off);
6440 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6441 (args->new_bh->b_data + header_off);
6442 struct ocfs2_alloc_context *meta_ac = NULL;
6443 struct ocfs2_inode_info *new_oi;
6444 struct ocfs2_dinode *new_di;
6445 struct ocfs2_xattr_value_buf vb = {
6446 .vb_bh = args->new_bh,
6447 .vb_access = ocfs2_journal_access_di,
6450 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6451 &credits, &meta_ac);
6457 handle = ocfs2_start_trans(osb, credits);
6458 if (IS_ERR(handle)) {
6459 ret = PTR_ERR(handle);
6464 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6465 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6471 memcpy(args->new_bh->b_data + header_off,
6472 args->old_bh->b_data + header_off, inline_size);
6474 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6475 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6477 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6478 args->new_bh, new_xh, &vb, meta_ac,
6479 ocfs2_get_xattr_value_root, NULL);
6485 new_oi = OCFS2_I(args->new_inode);
6486 spin_lock(&new_oi->ip_lock);
6487 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6488 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6489 spin_unlock(&new_oi->ip_lock);
6491 ocfs2_journal_dirty(handle, args->new_bh);
6494 ocfs2_commit_trans(osb, handle);
6498 ocfs2_free_alloc_context(meta_ac);
6502 static int ocfs2_create_empty_xattr_block(struct inode *inode,
6503 struct buffer_head *fe_bh,
6504 struct buffer_head **ret_bh,
6508 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6509 struct ocfs2_xattr_set_ctxt ctxt;
6511 memset(&ctxt, 0, sizeof(ctxt));
6512 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
6518 ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6519 if (IS_ERR(ctxt.handle)) {
6520 ret = PTR_ERR(ctxt.handle);
6525 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6526 (unsigned long long)fe_bh->b_blocknr, indexed);
6527 ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6532 ocfs2_commit_trans(osb, ctxt.handle);
6534 ocfs2_free_alloc_context(ctxt.meta_ac);
6538 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6539 struct buffer_head *blk_bh,
6540 struct buffer_head *new_blk_bh)
6542 int ret = 0, credits = 0;
6544 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6545 struct ocfs2_dinode *new_di;
6546 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6547 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6548 struct ocfs2_xattr_block *xb =
6549 (struct ocfs2_xattr_block *)blk_bh->b_data;
6550 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6551 struct ocfs2_xattr_block *new_xb =
6552 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6553 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6554 struct ocfs2_alloc_context *meta_ac;
6555 struct ocfs2_xattr_value_buf vb = {
6556 .vb_bh = new_blk_bh,
6557 .vb_access = ocfs2_journal_access_xb,
6560 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6561 &credits, &meta_ac);
6567 /* One more credits in case we need to add xattr flags in new inode. */
6568 handle = ocfs2_start_trans(osb, credits + 1);
6569 if (IS_ERR(handle)) {
6570 ret = PTR_ERR(handle);
6575 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6576 ret = ocfs2_journal_access_di(handle,
6577 INODE_CACHE(args->new_inode),
6579 OCFS2_JOURNAL_ACCESS_WRITE);
6586 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6587 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6593 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6594 osb->sb->s_blocksize - header_off);
6596 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6597 new_blk_bh, new_xh, &vb, meta_ac,
6598 ocfs2_get_xattr_value_root, NULL);
6604 ocfs2_journal_dirty(handle, new_blk_bh);
6606 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6607 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6608 spin_lock(&new_oi->ip_lock);
6609 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6610 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6611 spin_unlock(&new_oi->ip_lock);
6613 ocfs2_journal_dirty(handle, args->new_bh);
6617 ocfs2_commit_trans(osb, handle);
6620 ocfs2_free_alloc_context(meta_ac);
6624 struct ocfs2_reflink_xattr_tree_args {
6625 struct ocfs2_xattr_reflink *reflink;
6626 struct buffer_head *old_blk_bh;
6627 struct buffer_head *new_blk_bh;
6628 struct ocfs2_xattr_bucket *old_bucket;
6629 struct ocfs2_xattr_bucket *new_bucket;
6634 * We have to handle the case that both old bucket and new bucket
6635 * will call this function to get the right ret_bh.
6636 * So The caller must give us the right bh.
6638 static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6639 struct buffer_head *bh,
6640 struct ocfs2_xattr_header *xh,
6642 struct ocfs2_xattr_value_root **xv,
6643 struct buffer_head **ret_bh,
6646 struct ocfs2_reflink_xattr_tree_args *args =
6647 (struct ocfs2_reflink_xattr_tree_args *)para;
6648 struct ocfs2_xattr_bucket *bucket;
6650 if (bh == args->old_bucket->bu_bhs[0])
6651 bucket = args->old_bucket;
6653 bucket = args->new_bucket;
6655 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6659 struct ocfs2_value_tree_metas {
6665 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6666 struct buffer_head *bh,
6667 struct ocfs2_xattr_header *xh,
6669 struct ocfs2_xattr_value_root **xv,
6670 struct buffer_head **ret_bh,
6673 struct ocfs2_xattr_bucket *bucket =
6674 (struct ocfs2_xattr_bucket *)para;
6676 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6680 static int ocfs2_calc_value_tree_metas(struct inode *inode,
6681 struct ocfs2_xattr_bucket *bucket,
6684 struct ocfs2_value_tree_metas *metas =
6685 (struct ocfs2_value_tree_metas *)para;
6686 struct ocfs2_xattr_header *xh =
6687 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6689 /* Add the credits for this bucket first. */
6690 metas->credits += bucket->bu_blocks;
6691 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6692 xh, &metas->num_metas,
6693 &metas->credits, &metas->num_recs,
6694 ocfs2_value_tree_metas_in_bucket,
6699 * Given a xattr extent rec starting from blkno and having len clusters,
6700 * iterate all the buckets calculate how much metadata we need for reflinking
6701 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6703 static int ocfs2_lock_reflink_xattr_rec_allocators(
6704 struct ocfs2_reflink_xattr_tree_args *args,
6705 struct ocfs2_extent_tree *xt_et,
6706 u64 blkno, u32 len, int *credits,
6707 struct ocfs2_alloc_context **meta_ac,
6708 struct ocfs2_alloc_context **data_ac)
6710 int ret, num_free_extents;
6711 struct ocfs2_value_tree_metas metas;
6712 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6713 struct ocfs2_refcount_block *rb;
6715 memset(&metas, 0, sizeof(metas));
6717 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6718 ocfs2_calc_value_tree_metas, &metas);
6724 *credits = metas.credits;
6727 * Calculate we need for refcount tree change.
6729 * We need to add/modify num_recs in refcount tree, so just calculate
6730 * an approximate number we need for refcount tree change.
6731 * Sometimes we need to split the tree, and after split, half recs
6732 * will be moved to the new block, and a new block can only provide
6733 * half number of recs. So we multiple new blocks by 2.
6734 * In the end, we have to add credits for modifying the already
6735 * existed refcount block.
6737 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6739 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6740 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6741 metas.num_metas += metas.num_recs;
6742 *credits += metas.num_recs +
6743 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6744 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6745 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6746 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6750 /* count in the xattr tree change. */
6751 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6752 if (num_free_extents < 0) {
6753 ret = num_free_extents;
6758 if (num_free_extents < len)
6759 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6761 *credits += ocfs2_calc_extend_credits(osb->sb,
6762 xt_et->et_root_el, len);
6764 if (metas.num_metas) {
6765 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6774 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6781 ocfs2_free_alloc_context(*meta_ac);
6789 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6790 u64 blkno, u64 new_blkno, u32 clusters,
6791 struct ocfs2_alloc_context *meta_ac,
6792 struct ocfs2_alloc_context *data_ac,
6793 struct ocfs2_reflink_xattr_tree_args *args)
6796 struct super_block *sb = args->reflink->old_inode->i_sb;
6797 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6798 u32 num_buckets = clusters * bpc;
6799 int bpb = args->old_bucket->bu_blocks;
6800 struct ocfs2_xattr_value_buf vb = {
6801 .vb_access = ocfs2_journal_access,
6804 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6805 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6811 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6818 * The real bucket num in this series of blocks is stored
6819 * in the 1st bucket.
6822 num_buckets = le16_to_cpu(
6823 bucket_xh(args->old_bucket)->xh_num_buckets);
6825 ret = ocfs2_xattr_bucket_journal_access(handle,
6827 OCFS2_JOURNAL_ACCESS_CREATE);
6833 for (j = 0; j < bpb; j++)
6834 memcpy(bucket_block(args->new_bucket, j),
6835 bucket_block(args->old_bucket, j),
6838 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6840 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6841 args->old_bucket->bu_bhs[0],
6842 bucket_xh(args->old_bucket),
6843 args->new_bucket->bu_bhs[0],
6844 bucket_xh(args->new_bucket),
6846 ocfs2_get_reflink_xattr_value_root,
6854 * Re-access and dirty the bucket to calculate metaecc.
6855 * Because we may extend the transaction in reflink_xattr_header
6856 * which will let the already accessed block gone.
6858 ret = ocfs2_xattr_bucket_journal_access(handle,
6860 OCFS2_JOURNAL_ACCESS_WRITE);
6866 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6867 ocfs2_xattr_bucket_relse(args->old_bucket);
6868 ocfs2_xattr_bucket_relse(args->new_bucket);
6871 ocfs2_xattr_bucket_relse(args->old_bucket);
6872 ocfs2_xattr_bucket_relse(args->new_bucket);
6876 * Create the same xattr extent record in the new inode's xattr tree.
6878 static int ocfs2_reflink_xattr_rec(struct inode *inode,
6879 struct buffer_head *root_bh,
6885 int ret, credits = 0;
6886 u32 p_cluster, num_clusters;
6889 struct ocfs2_reflink_xattr_tree_args *args =
6890 (struct ocfs2_reflink_xattr_tree_args *)para;
6891 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6892 struct ocfs2_alloc_context *meta_ac = NULL;
6893 struct ocfs2_alloc_context *data_ac = NULL;
6894 struct ocfs2_extent_tree et;
6896 ocfs2_init_xattr_tree_extent_tree(&et,
6897 INODE_CACHE(args->reflink->new_inode),
6900 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6902 &meta_ac, &data_ac);
6908 handle = ocfs2_start_trans(osb, credits);
6909 if (IS_ERR(handle)) {
6910 ret = PTR_ERR(handle);
6915 ret = ocfs2_claim_clusters(handle, data_ac,
6916 len, &p_cluster, &num_clusters);
6922 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6924 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6925 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6926 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6927 meta_ac, data_ac, args);
6933 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6934 (unsigned long long)new_blkno, len, cpos);
6935 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6941 ocfs2_commit_trans(osb, handle);
6945 ocfs2_free_alloc_context(meta_ac);
6947 ocfs2_free_alloc_context(data_ac);
6952 * Create reflinked xattr buckets.
6953 * We will add bucket one by one, and refcount all the xattrs in the bucket
6954 * if they are stored outside.
6956 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6957 struct buffer_head *blk_bh,
6958 struct buffer_head *new_blk_bh)
6961 struct ocfs2_reflink_xattr_tree_args para;
6963 memset(¶, 0, sizeof(para));
6964 para.reflink = args;
6965 para.old_blk_bh = blk_bh;
6966 para.new_blk_bh = new_blk_bh;
6968 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
6969 if (!para.old_bucket) {
6970 mlog_errno(-ENOMEM);
6974 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
6975 if (!para.new_bucket) {
6981 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
6982 ocfs2_reflink_xattr_rec,
6988 ocfs2_xattr_bucket_free(para.old_bucket);
6989 ocfs2_xattr_bucket_free(para.new_bucket);
6993 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
6994 struct buffer_head *blk_bh)
6996 int ret, indexed = 0;
6997 struct buffer_head *new_blk_bh = NULL;
6998 struct ocfs2_xattr_block *xb =
6999 (struct ocfs2_xattr_block *)blk_bh->b_data;
7002 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7005 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7006 &new_blk_bh, indexed);
7012 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7013 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7015 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7024 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7026 int type = ocfs2_xattr_get_type(xe);
7028 return type != OCFS2_XATTR_INDEX_SECURITY &&
7029 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7030 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7033 int ocfs2_reflink_xattrs(struct inode *old_inode,
7034 struct buffer_head *old_bh,
7035 struct inode *new_inode,
7036 struct buffer_head *new_bh,
7037 bool preserve_security)
7040 struct ocfs2_xattr_reflink args;
7041 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7042 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7043 struct buffer_head *blk_bh = NULL;
7044 struct ocfs2_cached_dealloc_ctxt dealloc;
7045 struct ocfs2_refcount_tree *ref_tree;
7046 struct buffer_head *ref_root_bh = NULL;
7048 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7049 le64_to_cpu(di->i_refcount_loc),
7050 1, &ref_tree, &ref_root_bh);
7056 ocfs2_init_dealloc_ctxt(&dealloc);
7058 args.old_inode = old_inode;
7059 args.new_inode = new_inode;
7060 args.old_bh = old_bh;
7061 args.new_bh = new_bh;
7062 args.ref_ci = &ref_tree->rf_ci;
7063 args.ref_root_bh = ref_root_bh;
7064 args.dealloc = &dealloc;
7065 if (preserve_security)
7066 args.xattr_reflinked = NULL;
7068 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
7070 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7071 ret = ocfs2_reflink_xattr_inline(&args);
7078 if (!di->i_xattr_loc)
7081 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7088 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7095 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7097 brelse(ref_root_bh);
7099 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7100 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7101 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7109 * Initialize security and acl for a already created inode.
7110 * Used for reflink a non-preserve-security file.
7112 * It uses common api like ocfs2_xattr_set, so the caller
7113 * must not hold any lock expect i_mutex.
7115 int ocfs2_init_security_and_acl(struct inode *dir,
7116 struct inode *inode)
7119 struct buffer_head *dir_bh = NULL;
7120 struct ocfs2_security_xattr_info si = {
7124 ret = ocfs2_init_security_get(inode, dir, &si);
7126 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7127 si.name, si.value, si.value_len,
7133 } else if (ret != -EOPNOTSUPP) {
7138 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7144 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7148 ocfs2_inode_unlock(dir, 0);
7154 * 'security' attributes support
7156 static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
7157 size_t list_size, const char *name,
7158 size_t name_len, int type)
7160 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7161 const size_t total_len = prefix_len + name_len + 1;
7163 if (list && total_len <= list_size) {
7164 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7165 memcpy(list + prefix_len, name, name_len);
7166 list[prefix_len + name_len] = '\0';
7171 static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7172 void *buffer, size_t size, int type)
7174 if (strcmp(name, "") == 0)
7176 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7177 name, buffer, size);
7180 static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7181 const void *value, size_t size, int flags, int type)
7183 if (strcmp(name, "") == 0)
7186 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7187 name, value, size, flags);
7190 int ocfs2_init_security_get(struct inode *inode,
7192 struct ocfs2_security_xattr_info *si)
7194 /* check whether ocfs2 support feature xattr */
7195 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7197 return security_inode_init_security(inode, dir, &si->name, &si->value,
7201 int ocfs2_init_security_set(handle_t *handle,
7202 struct inode *inode,
7203 struct buffer_head *di_bh,
7204 struct ocfs2_security_xattr_info *si,
7205 struct ocfs2_alloc_context *xattr_ac,
7206 struct ocfs2_alloc_context *data_ac)
7208 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7209 OCFS2_XATTR_INDEX_SECURITY,
7210 si->name, si->value, si->value_len, 0,
7214 struct xattr_handler ocfs2_xattr_security_handler = {
7215 .prefix = XATTR_SECURITY_PREFIX,
7216 .list = ocfs2_xattr_security_list,
7217 .get = ocfs2_xattr_security_get,
7218 .set = ocfs2_xattr_security_set,
7222 * 'trusted' attributes support
7224 static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
7225 size_t list_size, const char *name,
7226 size_t name_len, int type)
7228 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
7229 const size_t total_len = prefix_len + name_len + 1;
7231 if (list && total_len <= list_size) {
7232 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7233 memcpy(list + prefix_len, name, name_len);
7234 list[prefix_len + name_len] = '\0';
7239 static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7240 void *buffer, size_t size, int type)
7242 if (strcmp(name, "") == 0)
7244 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7245 name, buffer, size);
7248 static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7249 const void *value, size_t size, int flags, int type)
7251 if (strcmp(name, "") == 0)
7254 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7255 name, value, size, flags);
7258 struct xattr_handler ocfs2_xattr_trusted_handler = {
7259 .prefix = XATTR_TRUSTED_PREFIX,
7260 .list = ocfs2_xattr_trusted_list,
7261 .get = ocfs2_xattr_trusted_get,
7262 .set = ocfs2_xattr_trusted_set,
7266 * 'user' attributes support
7268 static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
7269 size_t list_size, const char *name,
7270 size_t name_len, int type)
7272 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7273 const size_t total_len = prefix_len + name_len + 1;
7274 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7276 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7279 if (list && total_len <= list_size) {
7280 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7281 memcpy(list + prefix_len, name, name_len);
7282 list[prefix_len + name_len] = '\0';
7287 static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7288 void *buffer, size_t size, int type)
7290 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7292 if (strcmp(name, "") == 0)
7294 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7296 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
7300 static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7301 const void *value, size_t size, int flags, 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)
7310 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7311 name, value, size, flags);
7314 struct xattr_handler ocfs2_xattr_user_handler = {
7315 .prefix = XATTR_USER_PREFIX,
7316 .list = ocfs2_xattr_user_list,
7317 .get = ocfs2_xattr_user_get,
7318 .set = ocfs2_xattr_user_set,