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;
85 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
86 #define OCFS2_XATTR_INLINE_SIZE 80
87 #define OCFS2_XATTR_HEADER_GAP 4
88 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
89 - sizeof(struct ocfs2_xattr_header) \
90 - OCFS2_XATTR_HEADER_GAP)
91 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
92 - sizeof(struct ocfs2_xattr_block) \
93 - sizeof(struct ocfs2_xattr_header) \
94 - OCFS2_XATTR_HEADER_GAP)
96 static struct ocfs2_xattr_def_value_root def_xv = {
97 .xv.xr_list.l_count = cpu_to_le16(1),
100 const struct xattr_handler *ocfs2_xattr_handlers[] = {
101 &ocfs2_xattr_user_handler,
102 &ocfs2_xattr_acl_access_handler,
103 &ocfs2_xattr_acl_default_handler,
104 &ocfs2_xattr_trusted_handler,
105 &ocfs2_xattr_security_handler,
109 static const struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
110 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
111 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
112 = &ocfs2_xattr_acl_access_handler,
113 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
114 = &ocfs2_xattr_acl_default_handler,
115 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
116 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
119 struct ocfs2_xattr_info {
123 const void *xi_value;
127 struct ocfs2_xattr_search {
128 struct buffer_head *inode_bh;
130 * xattr_bh point to the block buffer head which has extended attribute
131 * when extended attribute in inode, xattr_bh is equal to inode_bh.
133 struct buffer_head *xattr_bh;
134 struct ocfs2_xattr_header *header;
135 struct ocfs2_xattr_bucket *bucket;
138 struct ocfs2_xattr_entry *here;
142 /* Operations on struct ocfs2_xa_entry */
144 struct ocfs2_xa_loc_operations {
148 int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
150 void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
153 * Return a pointer to the appropriate buffer in loc->xl_storage
154 * at the given offset from loc->xl_header.
156 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
158 /* Can we reuse the existing entry for the new value? */
159 int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
160 struct ocfs2_xattr_info *xi);
162 /* How much space is needed for the new value? */
163 int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
164 struct ocfs2_xattr_info *xi);
167 * Return the offset of the first name+value pair. This is
168 * the start of our downward-filling free space.
170 int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
173 * Remove the name+value at this location. Do whatever is
174 * appropriate with the remaining name+value pairs.
176 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
178 /* Fill xl_entry with a new entry */
179 void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
181 /* Add name+value storage to an entry */
182 void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
185 * Initialize the value buf's access and bh fields for this entry.
186 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
188 void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
189 struct ocfs2_xattr_value_buf *vb);
193 * Describes an xattr entry location. This is a memory structure
194 * tracking the on-disk structure.
196 struct ocfs2_xa_loc {
197 /* This xattr belongs to this inode */
198 struct inode *xl_inode;
200 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
201 struct ocfs2_xattr_header *xl_header;
203 /* Bytes from xl_header to the end of the storage */
207 * The ocfs2_xattr_entry this location describes. If this is
208 * NULL, this location describes the on-disk structure where it
211 struct ocfs2_xattr_entry *xl_entry;
214 * Internal housekeeping
217 /* Buffer(s) containing this entry */
220 /* Operations on the storage backing this location */
221 const struct ocfs2_xa_loc_operations *xl_ops;
225 * Convenience functions to calculate how much space is needed for a
226 * given name+value pair
228 static int namevalue_size(int name_len, uint64_t value_len)
230 if (value_len > OCFS2_XATTR_INLINE_SIZE)
231 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
233 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
236 static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
238 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
241 static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
243 u64 value_len = le64_to_cpu(xe->xe_value_size);
245 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
246 ocfs2_xattr_is_local(xe));
247 return namevalue_size(xe->xe_name_len, value_len);
251 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
252 struct ocfs2_xattr_header *xh,
257 static int ocfs2_xattr_block_find(struct inode *inode,
260 struct ocfs2_xattr_search *xs);
261 static int ocfs2_xattr_index_block_find(struct inode *inode,
262 struct buffer_head *root_bh,
265 struct ocfs2_xattr_search *xs);
267 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
268 struct buffer_head *blk_bh,
272 static int ocfs2_xattr_create_index_block(struct inode *inode,
273 struct ocfs2_xattr_search *xs,
274 struct ocfs2_xattr_set_ctxt *ctxt);
276 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
277 struct ocfs2_xattr_info *xi,
278 struct ocfs2_xattr_search *xs,
279 struct ocfs2_xattr_set_ctxt *ctxt);
281 typedef int (xattr_tree_rec_func)(struct inode *inode,
282 struct buffer_head *root_bh,
283 u64 blkno, u32 cpos, u32 len, void *para);
284 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
285 struct buffer_head *root_bh,
286 xattr_tree_rec_func *rec_func,
288 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
289 struct ocfs2_xattr_bucket *bucket,
291 static int ocfs2_rm_xattr_cluster(struct inode *inode,
292 struct buffer_head *root_bh,
298 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
299 u64 src_blk, u64 last_blk, u64 to_blk,
300 unsigned int start_bucket,
302 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
303 struct ocfs2_dinode *di,
304 struct ocfs2_xattr_info *xi,
305 struct ocfs2_xattr_search *xis,
306 struct ocfs2_xattr_search *xbs,
307 struct ocfs2_refcount_tree **ref_tree,
310 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
311 struct ocfs2_xattr_bucket *bucket,
313 struct ocfs2_xattr_value_root **xv,
314 struct buffer_head **bh);
316 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
318 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
321 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
323 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
326 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
327 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
328 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
330 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
332 struct ocfs2_xattr_bucket *bucket;
333 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
335 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
337 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
339 bucket->bu_inode = inode;
340 bucket->bu_blocks = blks;
346 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
350 for (i = 0; i < bucket->bu_blocks; i++) {
351 brelse(bucket->bu_bhs[i]);
352 bucket->bu_bhs[i] = NULL;
356 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
359 ocfs2_xattr_bucket_relse(bucket);
360 bucket->bu_inode = NULL;
366 * A bucket that has never been written to disk doesn't need to be
367 * read. We just need the buffer_heads. Don't call this for
368 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
371 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
376 for (i = 0; i < bucket->bu_blocks; i++) {
377 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
379 if (!bucket->bu_bhs[i]) {
385 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
387 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
392 ocfs2_xattr_bucket_relse(bucket);
396 /* Read the xattr bucket at xb_blkno */
397 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
402 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
403 bucket->bu_blocks, bucket->bu_bhs, 0,
406 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
407 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
410 &bucket_xh(bucket)->xh_check);
411 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
417 ocfs2_xattr_bucket_relse(bucket);
421 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
422 struct ocfs2_xattr_bucket *bucket,
427 for (i = 0; i < bucket->bu_blocks; i++) {
428 rc = ocfs2_journal_access(handle,
429 INODE_CACHE(bucket->bu_inode),
430 bucket->bu_bhs[i], type);
440 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
441 struct ocfs2_xattr_bucket *bucket)
445 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
446 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
447 bucket->bu_bhs, bucket->bu_blocks,
448 &bucket_xh(bucket)->xh_check);
449 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
451 for (i = 0; i < bucket->bu_blocks; i++)
452 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
455 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
456 struct ocfs2_xattr_bucket *src)
459 int blocksize = src->bu_inode->i_sb->s_blocksize;
461 BUG_ON(dest->bu_blocks != src->bu_blocks);
462 BUG_ON(dest->bu_inode != src->bu_inode);
464 for (i = 0; i < src->bu_blocks; i++) {
465 memcpy(bucket_block(dest, i), bucket_block(src, i),
470 static int ocfs2_validate_xattr_block(struct super_block *sb,
471 struct buffer_head *bh)
474 struct ocfs2_xattr_block *xb =
475 (struct ocfs2_xattr_block *)bh->b_data;
477 mlog(0, "Validating xattr block %llu\n",
478 (unsigned long long)bh->b_blocknr);
480 BUG_ON(!buffer_uptodate(bh));
483 * If the ecc fails, we return the error but otherwise
484 * leave the filesystem running. We know any error is
485 * local to this block.
487 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
492 * Errors after here are fatal
495 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
497 "Extended attribute block #%llu has bad "
499 (unsigned long long)bh->b_blocknr, 7,
504 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
506 "Extended attribute block #%llu has an "
507 "invalid xb_blkno of %llu",
508 (unsigned long long)bh->b_blocknr,
509 (unsigned long long)le64_to_cpu(xb->xb_blkno));
513 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
515 "Extended attribute block #%llu has an invalid "
516 "xb_fs_generation of #%u",
517 (unsigned long long)bh->b_blocknr,
518 le32_to_cpu(xb->xb_fs_generation));
525 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
526 struct buffer_head **bh)
529 struct buffer_head *tmp = *bh;
531 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
532 ocfs2_validate_xattr_block);
534 /* If ocfs2_read_block() got us a new bh, pass it up. */
541 static inline const char *ocfs2_xattr_prefix(int name_index)
543 const struct xattr_handler *handler = NULL;
545 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
546 handler = ocfs2_xattr_handler_map[name_index];
548 return handler ? handler->prefix : NULL;
551 static u32 ocfs2_xattr_name_hash(struct inode *inode,
555 /* Get hash value of uuid from super block */
556 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
559 /* hash extended attribute name */
560 for (i = 0; i < name_len; i++) {
561 hash = (hash << OCFS2_HASH_SHIFT) ^
562 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
569 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
571 return namevalue_size(name_len, value_len) +
572 sizeof(struct ocfs2_xattr_entry);
575 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
577 return namevalue_size_xi(xi) +
578 sizeof(struct ocfs2_xattr_entry);
581 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
583 return namevalue_size_xe(xe) +
584 sizeof(struct ocfs2_xattr_entry);
587 int ocfs2_calc_security_init(struct inode *dir,
588 struct ocfs2_security_xattr_info *si,
591 struct ocfs2_alloc_context **xattr_ac)
594 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
595 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
599 * The max space of security xattr taken inline is
600 * 256(name) + 80(value) + 16(entry) = 352 bytes,
601 * So reserve one metadata block for it is ok.
603 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
604 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
605 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
610 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
613 /* reserve clusters for xattr value which will be set in B tree*/
614 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
615 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
618 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
620 *want_clusters += new_clusters;
625 int ocfs2_calc_xattr_init(struct inode *dir,
626 struct buffer_head *dir_bh,
628 struct ocfs2_security_xattr_info *si,
634 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
635 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
638 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
641 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
642 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
643 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
646 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
649 } else if (acl_len != 0 && acl_len != -ENODATA) {
655 if (!(s_size + a_size))
659 * The max space of security xattr taken inline is
660 * 256(name) + 80(value) + 16(entry) = 352 bytes,
661 * The max space of acl xattr taken inline is
662 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
663 * when blocksize = 512, may reserve one more cluser for
664 * xattr bucket, otherwise reserve one metadata block
666 * If this is a new directory with inline data,
667 * we choose to reserve the entire inline area for
668 * directory contents and force an external xattr block.
670 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
671 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
672 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
673 *want_meta = *want_meta + 1;
674 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
677 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
678 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
680 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
684 * reserve credits and clusters for xattrs which has large value
685 * and have to be set outside
687 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
688 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
690 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
692 *want_clusters += new_clusters;
694 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
695 acl_len > OCFS2_XATTR_INLINE_SIZE) {
696 /* for directory, it has DEFAULT and ACCESS two types of acls */
697 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
698 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
699 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
701 *want_clusters += new_clusters;
707 static int ocfs2_xattr_extend_allocation(struct inode *inode,
709 struct ocfs2_xattr_value_buf *vb,
710 struct ocfs2_xattr_set_ctxt *ctxt)
712 int status = 0, credits;
713 handle_t *handle = ctxt->handle;
714 enum ocfs2_alloc_restarted why;
715 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
716 struct ocfs2_extent_tree et;
718 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
720 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
722 while (clusters_to_add) {
723 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
724 OCFS2_JOURNAL_ACCESS_WRITE);
730 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
731 status = ocfs2_add_clusters_in_btree(handle,
739 if ((status < 0) && (status != -EAGAIN)) {
740 if (status != -ENOSPC)
745 ocfs2_journal_dirty(handle, vb->vb_bh);
747 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) -
750 if (why != RESTART_NONE && clusters_to_add) {
752 * We can only fail in case the alloc file doesn't give
753 * up enough clusters.
755 BUG_ON(why == RESTART_META);
757 mlog(0, "restarting xattr value extension for %u"
758 " clusters,.\n", clusters_to_add);
759 credits = ocfs2_calc_extend_credits(inode->i_sb,
762 status = ocfs2_extend_trans(handle, credits);
774 static int __ocfs2_remove_xattr_range(struct inode *inode,
775 struct ocfs2_xattr_value_buf *vb,
776 u32 cpos, u32 phys_cpos, u32 len,
777 unsigned int ext_flags,
778 struct ocfs2_xattr_set_ctxt *ctxt)
781 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
782 handle_t *handle = ctxt->handle;
783 struct ocfs2_extent_tree et;
785 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
787 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
788 OCFS2_JOURNAL_ACCESS_WRITE);
794 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
801 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
802 ocfs2_journal_dirty(handle, vb->vb_bh);
804 if (ext_flags & OCFS2_EXT_REFCOUNTED)
805 ret = ocfs2_decrease_refcount(inode, handle,
806 ocfs2_blocks_to_clusters(inode->i_sb,
808 len, ctxt->meta_ac, &ctxt->dealloc, 1);
810 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
819 static int ocfs2_xattr_shrink_size(struct inode *inode,
822 struct ocfs2_xattr_value_buf *vb,
823 struct ocfs2_xattr_set_ctxt *ctxt)
826 unsigned int ext_flags;
827 u32 trunc_len, cpos, phys_cpos, alloc_size;
830 if (old_clusters <= new_clusters)
834 trunc_len = old_clusters - new_clusters;
836 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
838 &vb->vb_xv->xr_list, &ext_flags);
844 if (alloc_size > trunc_len)
845 alloc_size = trunc_len;
847 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
848 phys_cpos, alloc_size,
855 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
856 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
859 trunc_len -= alloc_size;
866 static int ocfs2_xattr_value_truncate(struct inode *inode,
867 struct ocfs2_xattr_value_buf *vb,
869 struct ocfs2_xattr_set_ctxt *ctxt)
872 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
873 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
875 if (new_clusters == old_clusters)
878 if (new_clusters > old_clusters)
879 ret = ocfs2_xattr_extend_allocation(inode,
880 new_clusters - old_clusters,
883 ret = ocfs2_xattr_shrink_size(inode,
884 old_clusters, new_clusters,
890 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
891 size_t *result, const char *prefix,
892 const char *name, int name_len)
894 char *p = buffer + *result;
895 int prefix_len = strlen(prefix);
896 int total_len = prefix_len + name_len + 1;
898 *result += total_len;
900 /* we are just looking for how big our buffer needs to be */
907 memcpy(p, prefix, prefix_len);
908 memcpy(p + prefix_len, name, name_len);
909 p[prefix_len + name_len] = '\0';
914 static int ocfs2_xattr_list_entries(struct inode *inode,
915 struct ocfs2_xattr_header *header,
916 char *buffer, size_t buffer_size)
920 const char *prefix, *name;
922 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
923 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
924 type = ocfs2_xattr_get_type(entry);
925 prefix = ocfs2_xattr_prefix(type);
928 name = (const char *)header +
929 le16_to_cpu(entry->xe_name_offset);
931 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
932 &result, prefix, name,
942 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
943 struct ocfs2_dinode *di)
945 struct ocfs2_xattr_header *xh;
948 xh = (struct ocfs2_xattr_header *)
949 ((void *)di + inode->i_sb->s_blocksize -
950 le16_to_cpu(di->i_xattr_inline_size));
952 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
953 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
959 static int ocfs2_xattr_ibody_list(struct inode *inode,
960 struct ocfs2_dinode *di,
964 struct ocfs2_xattr_header *header = NULL;
965 struct ocfs2_inode_info *oi = OCFS2_I(inode);
968 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
971 header = (struct ocfs2_xattr_header *)
972 ((void *)di + inode->i_sb->s_blocksize -
973 le16_to_cpu(di->i_xattr_inline_size));
975 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
980 static int ocfs2_xattr_block_list(struct inode *inode,
981 struct ocfs2_dinode *di,
985 struct buffer_head *blk_bh = NULL;
986 struct ocfs2_xattr_block *xb;
989 if (!di->i_xattr_loc)
992 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
999 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1000 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1001 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
1002 ret = ocfs2_xattr_list_entries(inode, header,
1003 buffer, buffer_size);
1005 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
1006 buffer, buffer_size);
1013 ssize_t ocfs2_listxattr(struct dentry *dentry,
1017 int ret = 0, i_ret = 0, b_ret = 0;
1018 struct buffer_head *di_bh = NULL;
1019 struct ocfs2_dinode *di = NULL;
1020 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1022 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1025 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1028 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1034 di = (struct ocfs2_dinode *)di_bh->b_data;
1036 down_read(&oi->ip_xattr_sem);
1037 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1045 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1050 up_read(&oi->ip_xattr_sem);
1051 ocfs2_inode_unlock(dentry->d_inode, 0);
1055 return i_ret + b_ret;
1058 static int ocfs2_xattr_find_entry(int name_index,
1060 struct ocfs2_xattr_search *xs)
1062 struct ocfs2_xattr_entry *entry;
1069 name_len = strlen(name);
1071 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1072 cmp = name_index - ocfs2_xattr_get_type(entry);
1074 cmp = name_len - entry->xe_name_len;
1076 cmp = memcmp(name, (xs->base +
1077 le16_to_cpu(entry->xe_name_offset)),
1085 return cmp ? -ENODATA : 0;
1088 static int ocfs2_xattr_get_value_outside(struct inode *inode,
1089 struct ocfs2_xattr_value_root *xv,
1093 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1096 size_t cplen, blocksize;
1097 struct buffer_head *bh = NULL;
1098 struct ocfs2_extent_list *el;
1101 clusters = le32_to_cpu(xv->xr_clusters);
1102 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1103 blocksize = inode->i_sb->s_blocksize;
1106 while (cpos < clusters) {
1107 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1108 &num_clusters, el, NULL);
1114 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1115 /* Copy ocfs2_xattr_value */
1116 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1117 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1124 cplen = len >= blocksize ? blocksize : len;
1125 memcpy(buffer, bh->b_data, cplen);
1134 cpos += num_clusters;
1140 static int ocfs2_xattr_ibody_get(struct inode *inode,
1145 struct ocfs2_xattr_search *xs)
1147 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1148 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1149 struct ocfs2_xattr_value_root *xv;
1153 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1156 xs->end = (void *)di + inode->i_sb->s_blocksize;
1157 xs->header = (struct ocfs2_xattr_header *)
1158 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1159 xs->base = (void *)xs->header;
1160 xs->here = xs->header->xh_entries;
1162 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1165 size = le64_to_cpu(xs->here->xe_value_size);
1167 if (size > buffer_size)
1169 if (ocfs2_xattr_is_local(xs->here)) {
1170 memcpy(buffer, (void *)xs->base +
1171 le16_to_cpu(xs->here->xe_name_offset) +
1172 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1174 xv = (struct ocfs2_xattr_value_root *)
1175 (xs->base + le16_to_cpu(
1176 xs->here->xe_name_offset) +
1177 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1178 ret = ocfs2_xattr_get_value_outside(inode, xv,
1190 static int ocfs2_xattr_block_get(struct inode *inode,
1195 struct ocfs2_xattr_search *xs)
1197 struct ocfs2_xattr_block *xb;
1198 struct ocfs2_xattr_value_root *xv;
1200 int ret = -ENODATA, name_offset, name_len, i;
1201 int uninitialized_var(block_off);
1203 xs->bucket = ocfs2_xattr_bucket_new(inode);
1210 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1216 if (xs->not_found) {
1221 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1222 size = le64_to_cpu(xs->here->xe_value_size);
1225 if (size > buffer_size)
1228 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1229 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1230 i = xs->here - xs->header->xh_entries;
1232 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1233 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1234 bucket_xh(xs->bucket),
1238 xs->base = bucket_block(xs->bucket, block_off);
1240 if (ocfs2_xattr_is_local(xs->here)) {
1241 memcpy(buffer, (void *)xs->base +
1242 name_offset + name_len, size);
1244 xv = (struct ocfs2_xattr_value_root *)
1245 (xs->base + name_offset + name_len);
1246 ret = ocfs2_xattr_get_value_outside(inode, xv,
1256 ocfs2_xattr_bucket_free(xs->bucket);
1258 brelse(xs->xattr_bh);
1259 xs->xattr_bh = NULL;
1263 int ocfs2_xattr_get_nolock(struct inode *inode,
1264 struct buffer_head *di_bh,
1271 struct ocfs2_dinode *di = NULL;
1272 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1273 struct ocfs2_xattr_search xis = {
1274 .not_found = -ENODATA,
1276 struct ocfs2_xattr_search xbs = {
1277 .not_found = -ENODATA,
1280 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1283 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1286 xis.inode_bh = xbs.inode_bh = di_bh;
1287 di = (struct ocfs2_dinode *)di_bh->b_data;
1289 down_read(&oi->ip_xattr_sem);
1290 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1292 if (ret == -ENODATA && di->i_xattr_loc)
1293 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1295 up_read(&oi->ip_xattr_sem);
1300 /* ocfs2_xattr_get()
1302 * Copy an extended attribute into the buffer provided.
1303 * Buffer is NULL to compute the size of buffer required.
1305 static int ocfs2_xattr_get(struct inode *inode,
1312 struct buffer_head *di_bh = NULL;
1314 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1319 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1320 name, buffer, buffer_size);
1322 ocfs2_inode_unlock(inode, 0);
1329 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1331 struct ocfs2_xattr_value_buf *vb,
1335 int ret = 0, i, cp_len;
1336 u16 blocksize = inode->i_sb->s_blocksize;
1337 u32 p_cluster, num_clusters;
1338 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1339 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1341 struct buffer_head *bh = NULL;
1342 unsigned int ext_flags;
1343 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1345 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1347 while (cpos < clusters) {
1348 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1349 &num_clusters, &xv->xr_list,
1356 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1358 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1360 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1361 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1368 ret = ocfs2_journal_access(handle,
1371 OCFS2_JOURNAL_ACCESS_WRITE);
1377 cp_len = value_len > blocksize ? blocksize : value_len;
1378 memcpy(bh->b_data, value, cp_len);
1379 value_len -= cp_len;
1381 if (cp_len < blocksize)
1382 memset(bh->b_data + cp_len, 0,
1383 blocksize - cp_len);
1385 ocfs2_journal_dirty(handle, bh);
1390 * XXX: do we need to empty all the following
1391 * blocks in this cluster?
1396 cpos += num_clusters;
1404 static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1412 free_space = free_start -
1413 sizeof(struct ocfs2_xattr_header) -
1414 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1415 OCFS2_XATTR_HEADER_GAP;
1418 if (free_space < needed_space)
1424 static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1427 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1430 static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1432 loc->xl_ops->xlo_journal_dirty(handle, loc);
1435 /* Give a pointer into the storage for the given offset */
1436 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1438 BUG_ON(offset >= loc->xl_size);
1439 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1443 * Wipe the name+value pair and allow the storage to reclaim it. This
1444 * must be followed by either removal of the entry or a call to
1445 * ocfs2_xa_add_namevalue().
1447 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1449 loc->xl_ops->xlo_wipe_namevalue(loc);
1453 * Find lowest offset to a name+value pair. This is the start of our
1454 * downward-growing free space.
1456 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1458 return loc->xl_ops->xlo_get_free_start(loc);
1461 /* Can we reuse loc->xl_entry for xi? */
1462 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1463 struct ocfs2_xattr_info *xi)
1465 return loc->xl_ops->xlo_can_reuse(loc, xi);
1468 /* How much free space is needed to set the new value */
1469 static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1470 struct ocfs2_xattr_info *xi)
1472 return loc->xl_ops->xlo_check_space(loc, xi);
1475 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1477 loc->xl_ops->xlo_add_entry(loc, name_hash);
1478 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1480 * We can't leave the new entry's xe_name_offset at zero or
1481 * add_namevalue() will go nuts. We set it to the size of our
1482 * storage so that it can never be less than any other entry.
1484 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1487 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1488 struct ocfs2_xattr_info *xi)
1490 int size = namevalue_size_xi(xi);
1494 loc->xl_ops->xlo_add_namevalue(loc, size);
1495 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1496 loc->xl_entry->xe_name_len = xi->xi_name_len;
1497 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1498 ocfs2_xattr_set_local(loc->xl_entry,
1499 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1501 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1502 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1503 memset(nameval_buf, 0, size);
1504 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1507 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1508 struct ocfs2_xattr_value_buf *vb)
1510 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1511 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1513 /* Value bufs are for value trees */
1514 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
1515 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1516 (name_size + OCFS2_XATTR_ROOT_SIZE));
1518 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1520 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1525 static int ocfs2_xa_block_journal_access(handle_t *handle,
1526 struct ocfs2_xa_loc *loc, int type)
1528 struct buffer_head *bh = loc->xl_storage;
1529 ocfs2_journal_access_func access;
1531 if (loc->xl_size == (bh->b_size -
1532 offsetof(struct ocfs2_xattr_block,
1533 xb_attrs.xb_header)))
1534 access = ocfs2_journal_access_xb;
1536 access = ocfs2_journal_access_di;
1537 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1540 static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1541 struct ocfs2_xa_loc *loc)
1543 struct buffer_head *bh = loc->xl_storage;
1545 ocfs2_journal_dirty(handle, bh);
1548 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1551 return (char *)loc->xl_header + offset;
1554 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1555 struct ocfs2_xattr_info *xi)
1558 * Block storage is strict. If the sizes aren't exact, we will
1559 * remove the old one and reinsert the new.
1561 return namevalue_size_xe(loc->xl_entry) ==
1562 namevalue_size_xi(xi);
1565 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1567 struct ocfs2_xattr_header *xh = loc->xl_header;
1568 int i, count = le16_to_cpu(xh->xh_count);
1569 int offset, free_start = loc->xl_size;
1571 for (i = 0; i < count; i++) {
1572 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1573 if (offset < free_start)
1574 free_start = offset;
1580 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1581 struct ocfs2_xattr_info *xi)
1583 int count = le16_to_cpu(loc->xl_header->xh_count);
1584 int free_start = ocfs2_xa_get_free_start(loc);
1585 int needed_space = ocfs2_xi_entry_usage(xi);
1588 * Block storage will reclaim the original entry before inserting
1589 * the new value, so we only need the difference. If the new
1590 * entry is smaller than the old one, we don't need anything.
1592 if (loc->xl_entry) {
1593 /* Don't need space if we're reusing! */
1594 if (ocfs2_xa_can_reuse_entry(loc, xi))
1597 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1599 if (needed_space < 0)
1601 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1605 * Block storage for xattrs keeps the name+value pairs compacted. When
1606 * we remove one, we have to shift any that preceded it towards the end.
1608 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1611 int namevalue_offset, first_namevalue_offset, namevalue_size;
1612 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1613 struct ocfs2_xattr_header *xh = loc->xl_header;
1614 int count = le16_to_cpu(xh->xh_count);
1616 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1617 namevalue_size = namevalue_size_xe(entry);
1618 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1620 /* Shift the name+value pairs */
1621 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1622 (char *)xh + first_namevalue_offset,
1623 namevalue_offset - first_namevalue_offset);
1624 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1626 /* Now tell xh->xh_entries about it */
1627 for (i = 0; i < count; i++) {
1628 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1629 if (offset <= namevalue_offset)
1630 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1635 * Note that we don't update xh_free_start or xh_name_value_len
1636 * because they're not used in block-stored xattrs.
1640 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1642 int count = le16_to_cpu(loc->xl_header->xh_count);
1643 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1644 le16_add_cpu(&loc->xl_header->xh_count, 1);
1645 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1648 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1650 int free_start = ocfs2_xa_get_free_start(loc);
1652 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1655 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1656 struct ocfs2_xattr_value_buf *vb)
1658 struct buffer_head *bh = loc->xl_storage;
1660 if (loc->xl_size == (bh->b_size -
1661 offsetof(struct ocfs2_xattr_block,
1662 xb_attrs.xb_header)))
1663 vb->vb_access = ocfs2_journal_access_xb;
1665 vb->vb_access = ocfs2_journal_access_di;
1670 * Operations for xattrs stored in blocks. This includes inline inode
1671 * storage and unindexed ocfs2_xattr_blocks.
1673 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1674 .xlo_journal_access = ocfs2_xa_block_journal_access,
1675 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
1676 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
1677 .xlo_check_space = ocfs2_xa_block_check_space,
1678 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1679 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
1680 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
1681 .xlo_add_entry = ocfs2_xa_block_add_entry,
1682 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
1683 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
1686 static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1687 struct ocfs2_xa_loc *loc, int type)
1689 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1691 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1694 static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1695 struct ocfs2_xa_loc *loc)
1697 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1699 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1702 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1705 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1706 int block, block_offset;
1708 /* The header is at the front of the bucket */
1709 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1710 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
1712 return bucket_block(bucket, block) + block_offset;
1715 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1716 struct ocfs2_xattr_info *xi)
1718 return namevalue_size_xe(loc->xl_entry) >=
1719 namevalue_size_xi(xi);
1722 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1724 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1725 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1728 static int ocfs2_bucket_align_free_start(struct super_block *sb,
1729 int free_start, int size)
1732 * We need to make sure that the name+value pair fits within
1735 if (((free_start - size) >> sb->s_blocksize_bits) !=
1736 ((free_start - 1) >> sb->s_blocksize_bits))
1737 free_start -= free_start % sb->s_blocksize;
1742 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1743 struct ocfs2_xattr_info *xi)
1746 int count = le16_to_cpu(loc->xl_header->xh_count);
1747 int free_start = ocfs2_xa_get_free_start(loc);
1748 int needed_space = ocfs2_xi_entry_usage(xi);
1749 int size = namevalue_size_xi(xi);
1750 struct super_block *sb = loc->xl_inode->i_sb;
1753 * Bucket storage does not reclaim name+value pairs it cannot
1754 * reuse. They live as holes until the bucket fills, and then
1755 * the bucket is defragmented. However, the bucket can reclaim
1756 * the ocfs2_xattr_entry.
1758 if (loc->xl_entry) {
1759 /* Don't need space if we're reusing! */
1760 if (ocfs2_xa_can_reuse_entry(loc, xi))
1763 needed_space -= sizeof(struct ocfs2_xattr_entry);
1765 BUG_ON(needed_space < 0);
1767 if (free_start < size) {
1772 * First we check if it would fit in the first place.
1773 * Below, we align the free start to a block. This may
1774 * slide us below the minimum gap. By checking unaligned
1775 * first, we avoid that error.
1777 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1781 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1784 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1787 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1789 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1790 -namevalue_size_xe(loc->xl_entry));
1793 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1795 struct ocfs2_xattr_header *xh = loc->xl_header;
1796 int count = le16_to_cpu(xh->xh_count);
1797 int low = 0, high = count - 1, tmp;
1798 struct ocfs2_xattr_entry *tmp_xe;
1801 * We keep buckets sorted by name_hash, so we need to find
1804 while (low <= high && count) {
1805 tmp = (low + high) / 2;
1806 tmp_xe = &xh->xh_entries[tmp];
1808 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1810 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1819 memmove(&xh->xh_entries[low + 1],
1820 &xh->xh_entries[low],
1821 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1823 le16_add_cpu(&xh->xh_count, 1);
1824 loc->xl_entry = &xh->xh_entries[low];
1825 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1828 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1830 int free_start = ocfs2_xa_get_free_start(loc);
1831 struct ocfs2_xattr_header *xh = loc->xl_header;
1832 struct super_block *sb = loc->xl_inode->i_sb;
1835 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1836 nameval_offset = free_start - size;
1837 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1838 xh->xh_free_start = cpu_to_le16(nameval_offset);
1839 le16_add_cpu(&xh->xh_name_value_len, size);
1843 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1844 struct ocfs2_xattr_value_buf *vb)
1846 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1847 struct super_block *sb = loc->xl_inode->i_sb;
1848 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1849 int size = namevalue_size_xe(loc->xl_entry);
1850 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1852 /* Values are not allowed to straddle block boundaries */
1853 BUG_ON(block_offset !=
1854 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1855 /* We expect the bucket to be filled in */
1856 BUG_ON(!bucket->bu_bhs[block_offset]);
1858 vb->vb_access = ocfs2_journal_access;
1859 vb->vb_bh = bucket->bu_bhs[block_offset];
1862 /* Operations for xattrs stored in buckets. */
1863 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1864 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1865 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
1866 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
1867 .xlo_check_space = ocfs2_xa_bucket_check_space,
1868 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1869 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
1870 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
1871 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1872 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
1873 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
1876 static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1878 struct ocfs2_xattr_value_buf vb;
1880 if (ocfs2_xattr_is_local(loc->xl_entry))
1883 ocfs2_xa_fill_value_buf(loc, &vb);
1884 return le32_to_cpu(vb.vb_xv->xr_clusters);
1887 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1888 struct ocfs2_xattr_set_ctxt *ctxt)
1890 int trunc_rc, access_rc;
1891 struct ocfs2_xattr_value_buf vb;
1893 ocfs2_xa_fill_value_buf(loc, &vb);
1894 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1898 * The caller of ocfs2_xa_value_truncate() has already called
1899 * ocfs2_xa_journal_access on the loc. However, The truncate code
1900 * calls ocfs2_extend_trans(). This may commit the previous
1901 * transaction and open a new one. If this is a bucket, truncate
1902 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1903 * the caller is expecting to dirty the entire bucket. So we must
1904 * reset the journal work. We do this even if truncate has failed,
1905 * as it could have failed after committing the extend.
1907 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1908 OCFS2_JOURNAL_ACCESS_WRITE);
1910 /* Errors in truncate take precedence */
1911 return trunc_rc ? trunc_rc : access_rc;
1914 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1917 struct ocfs2_xattr_header *xh = loc->xl_header;
1918 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1920 ocfs2_xa_wipe_namevalue(loc);
1921 loc->xl_entry = NULL;
1923 le16_add_cpu(&xh->xh_count, -1);
1924 count = le16_to_cpu(xh->xh_count);
1927 * Only zero out the entry if there are more remaining. This is
1928 * important for an empty bucket, as it keeps track of the
1929 * bucket's hash value. It doesn't hurt empty block storage.
1932 index = ((char *)entry - (char *)&xh->xh_entries) /
1933 sizeof(struct ocfs2_xattr_entry);
1934 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1935 (count - index) * sizeof(struct ocfs2_xattr_entry));
1936 memset(&xh->xh_entries[count], 0,
1937 sizeof(struct ocfs2_xattr_entry));
1942 * If we have a problem adjusting the size of an external value during
1943 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1944 * in an intermediate state. For example, the value may be partially
1947 * If the value tree hasn't changed, the extend/truncate went nowhere.
1948 * We have nothing to do. The caller can treat it as a straight error.
1950 * If the value tree got partially truncated, we now have a corrupted
1951 * extended attribute. We're going to wipe its entry and leak the
1952 * clusters. Better to leak some storage than leave a corrupt entry.
1954 * If the value tree grew, it obviously didn't grow enough for the
1955 * new entry. We're not going to try and reclaim those clusters either.
1956 * If there was already an external value there (orig_clusters != 0),
1957 * the new clusters are attached safely and we can just leave the old
1958 * value in place. If there was no external value there, we remove
1961 * This way, the xattr block we store in the journal will be consistent.
1962 * If the size change broke because of the journal, no changes will hit
1965 static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1967 unsigned int orig_clusters)
1969 unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1970 char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1971 le16_to_cpu(loc->xl_entry->xe_name_offset));
1973 if (new_clusters < orig_clusters) {
1975 "Partial truncate while %s xattr %.*s. Leaking "
1976 "%u clusters and removing the entry\n",
1977 what, loc->xl_entry->xe_name_len, nameval_buf,
1978 orig_clusters - new_clusters);
1979 ocfs2_xa_remove_entry(loc);
1980 } else if (!orig_clusters) {
1982 "Unable to allocate an external value for xattr "
1983 "%.*s safely. Leaking %u clusters and removing the "
1985 loc->xl_entry->xe_name_len, nameval_buf,
1986 new_clusters - orig_clusters);
1987 ocfs2_xa_remove_entry(loc);
1988 } else if (new_clusters > orig_clusters)
1990 "Unable to grow xattr %.*s safely. %u new clusters "
1991 "have been added, but the value will not be "
1993 loc->xl_entry->xe_name_len, nameval_buf,
1994 new_clusters - orig_clusters);
1997 static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1998 struct ocfs2_xattr_set_ctxt *ctxt)
2001 unsigned int orig_clusters;
2003 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2004 orig_clusters = ocfs2_xa_value_clusters(loc);
2005 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2009 * Since this is remove, we can return 0 if
2010 * ocfs2_xa_cleanup_value_truncate() is going to
2011 * wipe the entry anyway. So we check the
2012 * cluster count as well.
2014 if (orig_clusters != ocfs2_xa_value_clusters(loc))
2016 ocfs2_xa_cleanup_value_truncate(loc, "removing",
2023 ocfs2_xa_remove_entry(loc);
2029 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
2031 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
2034 nameval_buf = ocfs2_xa_offset_pointer(loc,
2035 le16_to_cpu(loc->xl_entry->xe_name_offset));
2036 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
2040 * Take an existing entry and make it ready for the new value. This
2041 * won't allocate space, but it may free space. It should be ready for
2042 * ocfs2_xa_prepare_entry() to finish the work.
2044 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
2045 struct ocfs2_xattr_info *xi,
2046 struct ocfs2_xattr_set_ctxt *ctxt)
2049 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2050 unsigned int orig_clusters;
2052 int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
2053 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
2055 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
2058 nameval_buf = ocfs2_xa_offset_pointer(loc,
2059 le16_to_cpu(loc->xl_entry->xe_name_offset));
2061 memset(nameval_buf + name_size, 0,
2062 namevalue_size_xe(loc->xl_entry) - name_size);
2064 ocfs2_xa_install_value_root(loc);
2066 orig_clusters = ocfs2_xa_value_clusters(loc);
2068 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2072 memset(nameval_buf + name_size, 0,
2073 namevalue_size_xe(loc->xl_entry) -
2075 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2077 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2084 ocfs2_xa_cleanup_value_truncate(loc, "reusing",
2090 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2091 ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2098 * Prepares loc->xl_entry to receive the new xattr. This includes
2099 * properly setting up the name+value pair region. If loc->xl_entry
2100 * already exists, it will take care of modifying it appropriately.
2102 * Note that this modifies the data. You did journal_access already,
2105 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2106 struct ocfs2_xattr_info *xi,
2108 struct ocfs2_xattr_set_ctxt *ctxt)
2111 unsigned int orig_clusters;
2112 __le64 orig_value_size = 0;
2114 rc = ocfs2_xa_check_space(loc, xi);
2118 if (loc->xl_entry) {
2119 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
2120 orig_value_size = loc->xl_entry->xe_value_size;
2121 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2127 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2128 orig_clusters = ocfs2_xa_value_clusters(loc);
2129 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2132 ocfs2_xa_cleanup_value_truncate(loc,
2138 ocfs2_xa_wipe_namevalue(loc);
2140 ocfs2_xa_add_entry(loc, name_hash);
2143 * If we get here, we have a blank entry. Fill it. We grow our
2144 * name+value pair back from the end.
2146 ocfs2_xa_add_namevalue(loc, xi);
2147 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2148 ocfs2_xa_install_value_root(loc);
2151 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2152 orig_clusters = ocfs2_xa_value_clusters(loc);
2153 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2155 ctxt->set_abort = 1;
2156 ocfs2_xa_cleanup_value_truncate(loc, "growing",
2159 * If we were growing an existing value,
2160 * ocfs2_xa_cleanup_value_truncate() won't remove
2161 * the entry. We need to restore the original value
2164 if (loc->xl_entry) {
2165 BUG_ON(!orig_value_size);
2166 loc->xl_entry->xe_value_size = orig_value_size;
2177 * Store the value portion of the name+value pair. This will skip
2178 * values that are stored externally. Their tree roots were set up
2179 * by ocfs2_xa_prepare_entry().
2181 static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2182 struct ocfs2_xattr_info *xi,
2183 struct ocfs2_xattr_set_ctxt *ctxt)
2186 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2187 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2189 struct ocfs2_xattr_value_buf vb;
2191 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
2192 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2193 ocfs2_xa_fill_value_buf(loc, &vb);
2194 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2199 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2204 static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2205 struct ocfs2_xattr_info *xi,
2206 struct ocfs2_xattr_set_ctxt *ctxt)
2209 u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2212 ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2213 OCFS2_JOURNAL_ACCESS_WRITE);
2220 * From here on out, everything is going to modify the buffer a
2221 * little. Errors are going to leave the xattr header in a
2222 * sane state. Thus, even with errors we dirty the sucker.
2225 /* Don't worry, we are never called with !xi_value and !xl_entry */
2226 if (!xi->xi_value) {
2227 ret = ocfs2_xa_remove(loc, ctxt);
2231 ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2238 ret = ocfs2_xa_store_value(loc, xi, ctxt);
2243 ocfs2_xa_journal_dirty(ctxt->handle, loc);
2249 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2250 struct inode *inode,
2251 struct buffer_head *bh,
2252 struct ocfs2_xattr_entry *entry)
2254 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2256 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2258 loc->xl_inode = inode;
2259 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2260 loc->xl_storage = bh;
2261 loc->xl_entry = entry;
2262 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2264 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2268 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2269 struct inode *inode,
2270 struct buffer_head *bh,
2271 struct ocfs2_xattr_entry *entry)
2273 struct ocfs2_xattr_block *xb =
2274 (struct ocfs2_xattr_block *)bh->b_data;
2276 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2278 loc->xl_inode = inode;
2279 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2280 loc->xl_storage = bh;
2281 loc->xl_header = &(xb->xb_attrs.xb_header);
2282 loc->xl_entry = entry;
2283 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2284 xb_attrs.xb_header);
2287 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2288 struct ocfs2_xattr_bucket *bucket,
2289 struct ocfs2_xattr_entry *entry)
2291 loc->xl_inode = bucket->bu_inode;
2292 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2293 loc->xl_storage = bucket;
2294 loc->xl_header = bucket_xh(bucket);
2295 loc->xl_entry = entry;
2296 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2300 * In xattr remove, if it is stored outside and refcounted, we may have
2301 * the chance to split the refcount tree. So need the allocators.
2303 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2304 struct ocfs2_xattr_value_root *xv,
2305 struct ocfs2_caching_info *ref_ci,
2306 struct buffer_head *ref_root_bh,
2307 struct ocfs2_alloc_context **meta_ac,
2310 int ret, meta_add = 0;
2311 u32 p_cluster, num_clusters;
2312 unsigned int ext_flags;
2315 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2324 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2327 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2329 &meta_add, ref_credits);
2335 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2344 static int ocfs2_remove_value_outside(struct inode*inode,
2345 struct ocfs2_xattr_value_buf *vb,
2346 struct ocfs2_xattr_header *header,
2347 struct ocfs2_caching_info *ref_ci,
2348 struct buffer_head *ref_root_bh)
2350 int ret = 0, i, ref_credits;
2351 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2352 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2355 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2357 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2358 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2360 if (ocfs2_xattr_is_local(entry))
2363 val = (void *)header +
2364 le16_to_cpu(entry->xe_name_offset);
2365 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2366 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2368 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2369 ref_ci, ref_root_bh,
2373 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2374 ocfs2_remove_extent_credits(osb->sb));
2375 if (IS_ERR(ctxt.handle)) {
2376 ret = PTR_ERR(ctxt.handle);
2381 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2387 ocfs2_commit_trans(osb, ctxt.handle);
2389 ocfs2_free_alloc_context(ctxt.meta_ac);
2390 ctxt.meta_ac = NULL;
2395 ocfs2_free_alloc_context(ctxt.meta_ac);
2396 ocfs2_schedule_truncate_log_flush(osb, 1);
2397 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2401 static int ocfs2_xattr_ibody_remove(struct inode *inode,
2402 struct buffer_head *di_bh,
2403 struct ocfs2_caching_info *ref_ci,
2404 struct buffer_head *ref_root_bh)
2407 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2408 struct ocfs2_xattr_header *header;
2410 struct ocfs2_xattr_value_buf vb = {
2412 .vb_access = ocfs2_journal_access_di,
2415 header = (struct ocfs2_xattr_header *)
2416 ((void *)di + inode->i_sb->s_blocksize -
2417 le16_to_cpu(di->i_xattr_inline_size));
2419 ret = ocfs2_remove_value_outside(inode, &vb, header,
2420 ref_ci, ref_root_bh);
2425 struct ocfs2_rm_xattr_bucket_para {
2426 struct ocfs2_caching_info *ref_ci;
2427 struct buffer_head *ref_root_bh;
2430 static int ocfs2_xattr_block_remove(struct inode *inode,
2431 struct buffer_head *blk_bh,
2432 struct ocfs2_caching_info *ref_ci,
2433 struct buffer_head *ref_root_bh)
2435 struct ocfs2_xattr_block *xb;
2437 struct ocfs2_xattr_value_buf vb = {
2439 .vb_access = ocfs2_journal_access_xb,
2441 struct ocfs2_rm_xattr_bucket_para args = {
2443 .ref_root_bh = ref_root_bh,
2446 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2447 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2448 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2449 ret = ocfs2_remove_value_outside(inode, &vb, header,
2450 ref_ci, ref_root_bh);
2452 ret = ocfs2_iterate_xattr_index_block(inode,
2454 ocfs2_rm_xattr_cluster,
2460 static int ocfs2_xattr_free_block(struct inode *inode,
2462 struct ocfs2_caching_info *ref_ci,
2463 struct buffer_head *ref_root_bh)
2465 struct inode *xb_alloc_inode;
2466 struct buffer_head *xb_alloc_bh = NULL;
2467 struct buffer_head *blk_bh = NULL;
2468 struct ocfs2_xattr_block *xb;
2469 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2475 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2481 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2487 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2488 blk = le64_to_cpu(xb->xb_blkno);
2489 bit = le16_to_cpu(xb->xb_suballoc_bit);
2490 if (xb->xb_suballoc_loc)
2491 bg_blkno = le64_to_cpu(xb->xb_suballoc_loc);
2493 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2495 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2496 EXTENT_ALLOC_SYSTEM_INODE,
2497 le16_to_cpu(xb->xb_suballoc_slot));
2498 if (!xb_alloc_inode) {
2503 mutex_lock(&xb_alloc_inode->i_mutex);
2505 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2511 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2512 if (IS_ERR(handle)) {
2513 ret = PTR_ERR(handle);
2518 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2523 ocfs2_commit_trans(osb, handle);
2525 ocfs2_inode_unlock(xb_alloc_inode, 1);
2526 brelse(xb_alloc_bh);
2528 mutex_unlock(&xb_alloc_inode->i_mutex);
2529 iput(xb_alloc_inode);
2536 * ocfs2_xattr_remove()
2538 * Free extended attribute resources associated with this inode.
2540 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2542 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2543 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2544 struct ocfs2_refcount_tree *ref_tree = NULL;
2545 struct buffer_head *ref_root_bh = NULL;
2546 struct ocfs2_caching_info *ref_ci = NULL;
2550 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2553 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2556 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2557 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2558 le64_to_cpu(di->i_refcount_loc),
2559 1, &ref_tree, &ref_root_bh);
2564 ref_ci = &ref_tree->rf_ci;
2568 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2569 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2570 ref_ci, ref_root_bh);
2577 if (di->i_xattr_loc) {
2578 ret = ocfs2_xattr_free_block(inode,
2579 le64_to_cpu(di->i_xattr_loc),
2580 ref_ci, ref_root_bh);
2587 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2588 OCFS2_INODE_UPDATE_CREDITS);
2589 if (IS_ERR(handle)) {
2590 ret = PTR_ERR(handle);
2594 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2595 OCFS2_JOURNAL_ACCESS_WRITE);
2601 di->i_xattr_loc = 0;
2603 spin_lock(&oi->ip_lock);
2604 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2605 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2606 spin_unlock(&oi->ip_lock);
2608 ocfs2_journal_dirty(handle, di_bh);
2610 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2613 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2614 brelse(ref_root_bh);
2618 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2619 struct ocfs2_dinode *di)
2621 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2622 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2625 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2628 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2629 struct ocfs2_inline_data *idata = &di->id2.i_data;
2630 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2631 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2632 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2633 le64_to_cpu(di->i_size);
2635 struct ocfs2_extent_list *el = &di->id2.i_list;
2636 free = (le16_to_cpu(el->l_count) -
2637 le16_to_cpu(el->l_next_free_rec)) *
2638 sizeof(struct ocfs2_extent_rec);
2640 if (free >= xattrsize)
2647 * ocfs2_xattr_ibody_find()
2649 * Find extended attribute in inode block and
2650 * fill search info into struct ocfs2_xattr_search.
2652 static int ocfs2_xattr_ibody_find(struct inode *inode,
2655 struct ocfs2_xattr_search *xs)
2657 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2658 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2662 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2665 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2666 down_read(&oi->ip_alloc_sem);
2667 has_space = ocfs2_xattr_has_space_inline(inode, di);
2668 up_read(&oi->ip_alloc_sem);
2673 xs->xattr_bh = xs->inode_bh;
2674 xs->end = (void *)di + inode->i_sb->s_blocksize;
2675 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2676 xs->header = (struct ocfs2_xattr_header *)
2677 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2679 xs->header = (struct ocfs2_xattr_header *)
2680 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2681 xs->base = (void *)xs->header;
2682 xs->here = xs->header->xh_entries;
2684 /* Find the named attribute. */
2685 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2686 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2687 if (ret && ret != -ENODATA)
2689 xs->not_found = ret;
2695 static int ocfs2_xattr_ibody_init(struct inode *inode,
2696 struct buffer_head *di_bh,
2697 struct ocfs2_xattr_set_ctxt *ctxt)
2700 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2701 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2702 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2703 unsigned int xattrsize = osb->s_xattr_inline_size;
2705 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2710 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2711 OCFS2_JOURNAL_ACCESS_WRITE);
2718 * Adjust extent record count or inline data size
2719 * to reserve space for extended attribute.
2721 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2722 struct ocfs2_inline_data *idata = &di->id2.i_data;
2723 le16_add_cpu(&idata->id_count, -xattrsize);
2724 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2725 struct ocfs2_extent_list *el = &di->id2.i_list;
2726 le16_add_cpu(&el->l_count, -(xattrsize /
2727 sizeof(struct ocfs2_extent_rec)));
2729 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2731 spin_lock(&oi->ip_lock);
2732 oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2733 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2734 spin_unlock(&oi->ip_lock);
2736 ocfs2_journal_dirty(ctxt->handle, di_bh);
2743 * ocfs2_xattr_ibody_set()
2745 * Set, replace or remove an extended attribute into inode block.
2748 static int ocfs2_xattr_ibody_set(struct inode *inode,
2749 struct ocfs2_xattr_info *xi,
2750 struct ocfs2_xattr_search *xs,
2751 struct ocfs2_xattr_set_ctxt *ctxt)
2754 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2755 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2756 struct ocfs2_xa_loc loc;
2758 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2761 down_write(&oi->ip_alloc_sem);
2762 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2763 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2769 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2770 ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2778 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2779 xs->not_found ? NULL : xs->here);
2780 ret = ocfs2_xa_set(&loc, xi, ctxt);
2786 xs->here = loc.xl_entry;
2789 up_write(&oi->ip_alloc_sem);
2795 * ocfs2_xattr_block_find()
2797 * Find extended attribute in external block and
2798 * fill search info into struct ocfs2_xattr_search.
2800 static int ocfs2_xattr_block_find(struct inode *inode,
2803 struct ocfs2_xattr_search *xs)
2805 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2806 struct buffer_head *blk_bh = NULL;
2807 struct ocfs2_xattr_block *xb;
2810 if (!di->i_xattr_loc)
2813 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2820 xs->xattr_bh = blk_bh;
2821 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2823 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2824 xs->header = &xb->xb_attrs.xb_header;
2825 xs->base = (void *)xs->header;
2826 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2827 xs->here = xs->header->xh_entries;
2829 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2831 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2835 if (ret && ret != -ENODATA) {
2836 xs->xattr_bh = NULL;
2839 xs->not_found = ret;
2847 static int ocfs2_create_xattr_block(struct inode *inode,
2848 struct buffer_head *inode_bh,
2849 struct ocfs2_xattr_set_ctxt *ctxt,
2851 struct buffer_head **ret_bh)
2854 u16 suballoc_bit_start;
2856 u64 suballoc_loc, first_blkno;
2857 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2858 struct buffer_head *new_bh = NULL;
2859 struct ocfs2_xattr_block *xblk;
2861 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2862 inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
2868 ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
2869 &suballoc_loc, &suballoc_bit_start,
2870 &num_got, &first_blkno);
2876 new_bh = sb_getblk(inode->i_sb, first_blkno);
2877 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2879 ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
2881 OCFS2_JOURNAL_ACCESS_CREATE);
2887 /* Initialize ocfs2_xattr_block */
2888 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2889 memset(xblk, 0, inode->i_sb->s_blocksize);
2890 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2891 xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
2892 xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc);
2893 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2894 xblk->xb_fs_generation =
2895 cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
2896 xblk->xb_blkno = cpu_to_le64(first_blkno);
2898 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2899 xr->xt_clusters = cpu_to_le32(1);
2900 xr->xt_last_eb_blk = 0;
2901 xr->xt_list.l_tree_depth = 0;
2902 xr->xt_list.l_count = cpu_to_le16(
2903 ocfs2_xattr_recs_per_xb(inode->i_sb));
2904 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2905 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2907 ocfs2_journal_dirty(ctxt->handle, new_bh);
2909 /* Add it to the inode */
2910 di->i_xattr_loc = cpu_to_le64(first_blkno);
2912 spin_lock(&OCFS2_I(inode)->ip_lock);
2913 OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2914 di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2915 spin_unlock(&OCFS2_I(inode)->ip_lock);
2917 ocfs2_journal_dirty(ctxt->handle, inode_bh);
2928 * ocfs2_xattr_block_set()
2930 * Set, replace or remove an extended attribute into external block.
2933 static int ocfs2_xattr_block_set(struct inode *inode,
2934 struct ocfs2_xattr_info *xi,
2935 struct ocfs2_xattr_search *xs,
2936 struct ocfs2_xattr_set_ctxt *ctxt)
2938 struct buffer_head *new_bh = NULL;
2939 struct ocfs2_xattr_block *xblk = NULL;
2941 struct ocfs2_xa_loc loc;
2943 if (!xs->xattr_bh) {
2944 ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2951 xs->xattr_bh = new_bh;
2952 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2953 xs->header = &xblk->xb_attrs.xb_header;
2954 xs->base = (void *)xs->header;
2955 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2956 xs->here = xs->header->xh_entries;
2958 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2960 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2961 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2962 xs->not_found ? NULL : xs->here);
2964 ret = ocfs2_xa_set(&loc, xi, ctxt);
2966 xs->here = loc.xl_entry;
2967 else if ((ret != -ENOSPC) || ctxt->set_abort)
2970 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2976 if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2977 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2983 /* Check whether the new xattr can be inserted into the inode. */
2984 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2985 struct ocfs2_xattr_info *xi,
2986 struct ocfs2_xattr_search *xs)
2988 struct ocfs2_xattr_entry *last;
2990 size_t min_offs = xs->end - xs->base;
2995 last = xs->header->xh_entries;
2997 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2998 size_t offs = le16_to_cpu(last->xe_name_offset);
2999 if (offs < min_offs)
3004 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
3008 BUG_ON(!xs->not_found);
3010 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
3016 static int ocfs2_calc_xattr_set_need(struct inode *inode,
3017 struct ocfs2_dinode *di,
3018 struct ocfs2_xattr_info *xi,
3019 struct ocfs2_xattr_search *xis,
3020 struct ocfs2_xattr_search *xbs,
3025 int ret = 0, old_in_xb = 0;
3026 int clusters_add = 0, meta_add = 0, credits = 0;
3027 struct buffer_head *bh = NULL;
3028 struct ocfs2_xattr_block *xb = NULL;
3029 struct ocfs2_xattr_entry *xe = NULL;
3030 struct ocfs2_xattr_value_root *xv = NULL;
3032 int name_offset, name_len = 0;
3033 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3038 * Calculate the clusters we need to write.
3039 * No matter whether we replace an old one or add a new one,
3040 * we need this for writing.
3042 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
3043 credits += new_clusters *
3044 ocfs2_clusters_to_blocks(inode->i_sb, 1);
3046 if (xis->not_found && xbs->not_found) {
3047 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3049 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3050 clusters_add += new_clusters;
3051 credits += ocfs2_calc_extend_credits(inode->i_sb,
3059 if (!xis->not_found) {
3061 name_offset = le16_to_cpu(xe->xe_name_offset);
3062 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3064 credits += OCFS2_INODE_UPDATE_CREDITS;
3066 int i, block_off = 0;
3067 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3069 name_offset = le16_to_cpu(xe->xe_name_offset);
3070 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3071 i = xbs->here - xbs->header->xh_entries;
3074 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3075 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3076 bucket_xh(xbs->bucket),
3079 base = bucket_block(xbs->bucket, block_off);
3080 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3083 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3088 * delete a xattr doesn't need metadata and cluster allocation.
3089 * so just calculate the credits and return.
3091 * The credits for removing the value tree will be extended
3092 * by ocfs2_remove_extent itself.
3094 if (!xi->xi_value) {
3095 if (!ocfs2_xattr_is_local(xe))
3096 credits += ocfs2_remove_extent_credits(inode->i_sb);
3101 /* do cluster allocation guess first. */
3102 value_size = le64_to_cpu(xe->xe_value_size);
3106 * In xattr set, we always try to set the xe in inode first,
3107 * so if it can be inserted into inode successfully, the old
3108 * one will be removed from the xattr block, and this xattr
3109 * will be inserted into inode as a new xattr in inode.
3111 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3112 clusters_add += new_clusters;
3113 credits += ocfs2_remove_extent_credits(inode->i_sb) +
3114 OCFS2_INODE_UPDATE_CREDITS;
3115 if (!ocfs2_xattr_is_local(xe))
3116 credits += ocfs2_calc_extend_credits(
3124 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3125 /* the new values will be stored outside. */
3126 u32 old_clusters = 0;
3128 if (!ocfs2_xattr_is_local(xe)) {
3129 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3131 xv = (struct ocfs2_xattr_value_root *)
3132 (base + name_offset + name_len);
3133 value_size = OCFS2_XATTR_ROOT_SIZE;
3137 if (old_clusters >= new_clusters) {
3138 credits += ocfs2_remove_extent_credits(inode->i_sb);
3141 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3142 clusters_add += new_clusters - old_clusters;
3143 credits += ocfs2_calc_extend_credits(inode->i_sb,
3147 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3152 * Now the new value will be stored inside. So if the new
3153 * value is smaller than the size of value root or the old
3154 * value, we don't need any allocation, otherwise we have
3155 * to guess metadata allocation.
3157 if ((ocfs2_xattr_is_local(xe) &&
3158 (value_size >= xi->xi_value_len)) ||
3159 (!ocfs2_xattr_is_local(xe) &&
3160 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
3165 /* calculate metadata allocation. */
3166 if (di->i_xattr_loc) {
3167 if (!xbs->xattr_bh) {
3168 ret = ocfs2_read_xattr_block(inode,
3169 le64_to_cpu(di->i_xattr_loc),
3176 xb = (struct ocfs2_xattr_block *)bh->b_data;
3178 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3181 * If there is already an xattr tree, good, we can calculate
3182 * like other b-trees. Otherwise we may have the chance of
3183 * create a tree, the credit calculation is borrowed from
3184 * ocfs2_calc_extend_credits with root_el = NULL. And the
3185 * new tree will be cluster based, so no meta is needed.
3187 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3188 struct ocfs2_extent_list *el =
3189 &xb->xb_attrs.xb_root.xt_list;
3190 meta_add += ocfs2_extend_meta_needed(el);
3191 credits += ocfs2_calc_extend_credits(inode->i_sb,
3194 credits += OCFS2_SUBALLOC_ALLOC + 1;
3197 * This cluster will be used either for new bucket or for
3199 * If the cluster size is the same as the bucket size, one
3200 * more is needed since we may need to extend the bucket
3204 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3205 if (OCFS2_XATTR_BUCKET_SIZE ==
3206 OCFS2_SB(inode->i_sb)->s_clustersize) {
3207 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3212 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3216 *clusters_need = clusters_add;
3218 *meta_need = meta_add;
3220 *credits_need = credits;
3225 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3226 struct ocfs2_dinode *di,
3227 struct ocfs2_xattr_info *xi,
3228 struct ocfs2_xattr_search *xis,
3229 struct ocfs2_xattr_search *xbs,
3230 struct ocfs2_xattr_set_ctxt *ctxt,
3234 int clusters_add, meta_add, ret;
3235 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3237 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3239 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3241 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
3242 &clusters_add, &meta_add, credits);
3248 meta_add += extra_meta;
3249 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
3250 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
3253 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3262 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3268 if (ctxt->meta_ac) {
3269 ocfs2_free_alloc_context(ctxt->meta_ac);
3270 ctxt->meta_ac = NULL;
3274 * We cannot have an error and a non null ctxt->data_ac.
3281 static int __ocfs2_xattr_set_handle(struct inode *inode,
3282 struct ocfs2_dinode *di,
3283 struct ocfs2_xattr_info *xi,
3284 struct ocfs2_xattr_search *xis,
3285 struct ocfs2_xattr_search *xbs,
3286 struct ocfs2_xattr_set_ctxt *ctxt)
3288 int ret = 0, credits, old_found;
3290 if (!xi->xi_value) {
3291 /* Remove existing extended attribute */
3292 if (!xis->not_found)
3293 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3294 else if (!xbs->not_found)
3295 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3297 /* We always try to set extended attribute into inode first*/
3298 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3299 if (!ret && !xbs->not_found) {
3301 * If succeed and that extended attribute existing in
3302 * external block, then we will remove it.
3304 xi->xi_value = NULL;
3305 xi->xi_value_len = 0;
3307 old_found = xis->not_found;
3308 xis->not_found = -ENODATA;
3309 ret = ocfs2_calc_xattr_set_need(inode,
3317 xis->not_found = old_found;
3323 ret = ocfs2_extend_trans(ctxt->handle, credits);
3328 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3329 } else if ((ret == -ENOSPC) && !ctxt->set_abort) {
3330 if (di->i_xattr_loc && !xbs->xattr_bh) {
3331 ret = ocfs2_xattr_block_find(inode,
3337 old_found = xis->not_found;
3338 xis->not_found = -ENODATA;
3339 ret = ocfs2_calc_xattr_set_need(inode,
3347 xis->not_found = old_found;
3353 ret = ocfs2_extend_trans(ctxt->handle, credits);
3360 * If no space in inode, we will set extended attribute
3361 * into external block.
3363 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3366 if (!xis->not_found) {
3368 * If succeed and that extended attribute
3369 * existing in inode, we will remove it.
3371 xi->xi_value = NULL;
3372 xi->xi_value_len = 0;
3373 xbs->not_found = -ENODATA;
3374 ret = ocfs2_calc_xattr_set_need(inode,
3387 ret = ocfs2_extend_trans(ctxt->handle, credits);
3392 ret = ocfs2_xattr_ibody_set(inode, xi,
3399 /* Update inode ctime. */
3400 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3402 OCFS2_JOURNAL_ACCESS_WRITE);
3408 inode->i_ctime = CURRENT_TIME;
3409 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3410 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3411 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3418 * This function only called duing creating inode
3419 * for init security/acl xattrs of the new inode.
3420 * All transanction credits have been reserved in mknod.
3422 int ocfs2_xattr_set_handle(handle_t *handle,
3423 struct inode *inode,
3424 struct buffer_head *di_bh,
3430 struct ocfs2_alloc_context *meta_ac,
3431 struct ocfs2_alloc_context *data_ac)
3433 struct ocfs2_dinode *di;
3436 struct ocfs2_xattr_info xi = {
3437 .xi_name_index = name_index,
3439 .xi_name_len = strlen(name),
3441 .xi_value_len = value_len,
3444 struct ocfs2_xattr_search xis = {
3445 .not_found = -ENODATA,
3448 struct ocfs2_xattr_search xbs = {
3449 .not_found = -ENODATA,
3452 struct ocfs2_xattr_set_ctxt ctxt = {
3458 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3462 * In extreme situation, may need xattr bucket when
3463 * block size is too small. And we have already reserved
3464 * the credits for bucket in mknod.
3466 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3467 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3469 mlog_errno(-ENOMEM);
3474 xis.inode_bh = xbs.inode_bh = di_bh;
3475 di = (struct ocfs2_dinode *)di_bh->b_data;
3477 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3479 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3482 if (xis.not_found) {
3483 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3488 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3491 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3492 brelse(xbs.xattr_bh);
3493 ocfs2_xattr_bucket_free(xbs.bucket);
3501 * Set, replace or remove an extended attribute for this inode.
3502 * value is NULL to remove an existing extended attribute, else either
3503 * create or replace an extended attribute.
3505 int ocfs2_xattr_set(struct inode *inode,
3512 struct buffer_head *di_bh = NULL;
3513 struct ocfs2_dinode *di;
3514 int ret, credits, ref_meta = 0, ref_credits = 0;
3515 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3516 struct inode *tl_inode = osb->osb_tl_inode;
3517 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
3518 struct ocfs2_refcount_tree *ref_tree = NULL;
3520 struct ocfs2_xattr_info xi = {
3521 .xi_name_index = name_index,
3523 .xi_name_len = strlen(name),
3525 .xi_value_len = value_len,
3528 struct ocfs2_xattr_search xis = {
3529 .not_found = -ENODATA,
3532 struct ocfs2_xattr_search xbs = {
3533 .not_found = -ENODATA,
3536 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3540 * Only xbs will be used on indexed trees. xis doesn't need a
3543 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3545 mlog_errno(-ENOMEM);
3549 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3552 goto cleanup_nolock;
3554 xis.inode_bh = xbs.inode_bh = di_bh;
3555 di = (struct ocfs2_dinode *)di_bh->b_data;
3557 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3559 * Scan inode and external block to find the same name
3560 * extended attribute and collect search infomation.
3562 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3565 if (xis.not_found) {
3566 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3571 if (xis.not_found && xbs.not_found) {
3573 if (flags & XATTR_REPLACE)
3580 if (flags & XATTR_CREATE)
3584 /* Check whether the value is refcounted and do some prepartion. */
3585 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3586 (!xis.not_found || !xbs.not_found)) {
3587 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3588 &xis, &xbs, &ref_tree,
3589 &ref_meta, &ref_credits);
3596 mutex_lock(&tl_inode->i_mutex);
3598 if (ocfs2_truncate_log_needs_flush(osb)) {
3599 ret = __ocfs2_flush_truncate_log(osb);
3601 mutex_unlock(&tl_inode->i_mutex);
3606 mutex_unlock(&tl_inode->i_mutex);
3608 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3609 &xbs, &ctxt, ref_meta, &credits);
3615 /* we need to update inode's ctime field, so add credit for it. */
3616 credits += OCFS2_INODE_UPDATE_CREDITS;
3617 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3618 if (IS_ERR(ctxt.handle)) {
3619 ret = PTR_ERR(ctxt.handle);
3624 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3626 ocfs2_commit_trans(osb, ctxt.handle);
3629 ocfs2_free_alloc_context(ctxt.data_ac);
3631 ocfs2_free_alloc_context(ctxt.meta_ac);
3632 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3633 ocfs2_schedule_truncate_log_flush(osb, 1);
3634 ocfs2_run_deallocs(osb, &ctxt.dealloc);
3638 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3639 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3640 if (!value && !ret) {
3641 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3645 ocfs2_inode_unlock(inode, 1);
3648 brelse(xbs.xattr_bh);
3649 ocfs2_xattr_bucket_free(xbs.bucket);
3655 * Find the xattr extent rec which may contains name_hash.
3656 * e_cpos will be the first name hash of the xattr rec.
3657 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3659 static int ocfs2_xattr_get_rec(struct inode *inode,
3664 struct ocfs2_extent_list *el)
3667 struct buffer_head *eb_bh = NULL;
3668 struct ocfs2_extent_block *eb;
3669 struct ocfs2_extent_rec *rec = NULL;
3672 if (el->l_tree_depth) {
3673 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3680 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3683 if (el->l_tree_depth) {
3684 ocfs2_error(inode->i_sb,
3685 "Inode %lu has non zero tree depth in "
3686 "xattr tree block %llu\n", inode->i_ino,
3687 (unsigned long long)eb_bh->b_blocknr);
3693 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3694 rec = &el->l_recs[i];
3696 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3697 e_blkno = le64_to_cpu(rec->e_blkno);
3703 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3704 "record (%u, %u, 0) in xattr", inode->i_ino,
3705 le32_to_cpu(rec->e_cpos),
3706 ocfs2_rec_clusters(el, rec));
3711 *p_blkno = le64_to_cpu(rec->e_blkno);
3712 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3714 *e_cpos = le32_to_cpu(rec->e_cpos);
3720 typedef int (xattr_bucket_func)(struct inode *inode,
3721 struct ocfs2_xattr_bucket *bucket,
3724 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3725 struct ocfs2_xattr_bucket *bucket,
3732 int i, ret = 0, cmp = 1, block_off, new_offset;
3733 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3734 size_t name_len = strlen(name);
3735 struct ocfs2_xattr_entry *xe = NULL;
3739 * We don't use binary search in the bucket because there
3740 * may be multiple entries with the same name hash.
3742 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3743 xe = &xh->xh_entries[i];
3745 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3747 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3750 cmp = name_index - ocfs2_xattr_get_type(xe);
3752 cmp = name_len - xe->xe_name_len;
3756 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3767 xe_name = bucket_block(bucket, block_off) + new_offset;
3768 if (!memcmp(name, xe_name, name_len)) {
3780 * Find the specified xattr entry in a series of buckets.
3781 * This series start from p_blkno and last for num_clusters.
3782 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3783 * the num of the valid buckets.
3785 * Return the buffer_head this xattr should reside in. And if the xattr's
3786 * hash is in the gap of 2 buckets, return the lower bucket.
3788 static int ocfs2_xattr_bucket_find(struct inode *inode,
3795 struct ocfs2_xattr_search *xs)
3798 struct ocfs2_xattr_header *xh = NULL;
3799 struct ocfs2_xattr_entry *xe = NULL;
3801 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3802 int low_bucket = 0, bucket, high_bucket;
3803 struct ocfs2_xattr_bucket *search;
3805 u64 blkno, lower_blkno = 0;
3807 search = ocfs2_xattr_bucket_new(inode);
3814 ret = ocfs2_read_xattr_bucket(search, p_blkno);
3820 xh = bucket_xh(search);
3821 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3822 while (low_bucket <= high_bucket) {
3823 ocfs2_xattr_bucket_relse(search);
3825 bucket = (low_bucket + high_bucket) / 2;
3826 blkno = p_blkno + bucket * blk_per_bucket;
3827 ret = ocfs2_read_xattr_bucket(search, blkno);
3833 xh = bucket_xh(search);
3834 xe = &xh->xh_entries[0];
3835 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3836 high_bucket = bucket - 1;
3841 * Check whether the hash of the last entry in our
3842 * bucket is larger than the search one. for an empty
3843 * bucket, the last one is also the first one.
3846 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3848 last_hash = le32_to_cpu(xe->xe_name_hash);
3850 /* record lower_blkno which may be the insert place. */
3851 lower_blkno = blkno;
3853 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3854 low_bucket = bucket + 1;
3858 /* the searched xattr should reside in this bucket if exists. */
3859 ret = ocfs2_find_xe_in_bucket(inode, search,
3860 name_index, name, name_hash,
3870 * Record the bucket we have found.
3871 * When the xattr's hash value is in the gap of 2 buckets, we will
3872 * always set it to the previous bucket.
3875 lower_blkno = p_blkno;
3877 /* This should be in cache - we just read it during the search */
3878 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3884 xs->header = bucket_xh(xs->bucket);
3885 xs->base = bucket_block(xs->bucket, 0);
3886 xs->end = xs->base + inode->i_sb->s_blocksize;
3889 xs->here = &xs->header->xh_entries[index];
3890 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3891 (unsigned long long)bucket_blkno(xs->bucket), index);
3896 ocfs2_xattr_bucket_free(search);
3900 static int ocfs2_xattr_index_block_find(struct inode *inode,
3901 struct buffer_head *root_bh,
3904 struct ocfs2_xattr_search *xs)
3907 struct ocfs2_xattr_block *xb =
3908 (struct ocfs2_xattr_block *)root_bh->b_data;
3909 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3910 struct ocfs2_extent_list *el = &xb_root->xt_list;
3912 u32 first_hash, num_clusters = 0;
3913 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3915 if (le16_to_cpu(el->l_next_free_rec) == 0)
3918 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3919 name, name_hash, name_index);
3921 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3928 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3930 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3931 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3934 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3935 p_blkno, first_hash, num_clusters, xs);
3941 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3944 xattr_bucket_func *func,
3948 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3949 u32 num_buckets = clusters * bpc;
3950 struct ocfs2_xattr_bucket *bucket;
3952 bucket = ocfs2_xattr_bucket_new(inode);
3954 mlog_errno(-ENOMEM);
3958 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3959 clusters, (unsigned long long)blkno);
3961 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3962 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3969 * The real bucket num in this series of blocks is stored
3970 * in the 1st bucket.
3973 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3975 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3976 (unsigned long long)blkno,
3977 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3979 ret = func(inode, bucket, para);
3980 if (ret && ret != -ERANGE)
3982 /* Fall through to bucket_relse() */
3985 ocfs2_xattr_bucket_relse(bucket);
3990 ocfs2_xattr_bucket_free(bucket);
3994 struct ocfs2_xattr_tree_list {
4000 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
4001 struct ocfs2_xattr_header *xh,
4008 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
4011 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
4013 *block_off = name_offset >> sb->s_blocksize_bits;
4014 *new_offset = name_offset % sb->s_blocksize;
4019 static int ocfs2_list_xattr_bucket(struct inode *inode,
4020 struct ocfs2_xattr_bucket *bucket,
4024 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
4025 int i, block_off, new_offset;
4026 const char *prefix, *name;
4028 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
4029 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
4030 type = ocfs2_xattr_get_type(entry);
4031 prefix = ocfs2_xattr_prefix(type);
4034 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
4042 name = (const char *)bucket_block(bucket, block_off) +
4044 ret = ocfs2_xattr_list_entry(xl->buffer,
4048 entry->xe_name_len);
4057 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4058 struct buffer_head *blk_bh,
4059 xattr_tree_rec_func *rec_func,
4062 struct ocfs2_xattr_block *xb =
4063 (struct ocfs2_xattr_block *)blk_bh->b_data;
4064 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4066 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4069 if (!el->l_next_free_rec || !rec_func)
4072 while (name_hash > 0) {
4073 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4074 &e_cpos, &num_clusters, el);
4080 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4081 num_clusters, para);
4091 name_hash = e_cpos - 1;
4098 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4099 struct buffer_head *root_bh,
4100 u64 blkno, u32 cpos, u32 len, void *para)
4102 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4103 ocfs2_list_xattr_bucket, para);
4106 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4107 struct buffer_head *blk_bh,
4112 struct ocfs2_xattr_tree_list xl = {
4114 .buffer_size = buffer_size,
4118 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4119 ocfs2_list_xattr_tree_rec, &xl);
4130 static int cmp_xe(const void *a, const void *b)
4132 const struct ocfs2_xattr_entry *l = a, *r = b;
4133 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4134 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4136 if (l_hash > r_hash)
4138 if (l_hash < r_hash)
4143 static void swap_xe(void *a, void *b, int size)
4145 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4148 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4149 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4153 * When the ocfs2_xattr_block is filled up, new bucket will be created
4154 * and all the xattr entries will be moved to the new bucket.
4155 * The header goes at the start of the bucket, and the names+values are
4156 * filled from the end. This is why *target starts as the last buffer.
4157 * Note: we need to sort the entries since they are not saved in order
4158 * in the ocfs2_xattr_block.
4160 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4161 struct buffer_head *xb_bh,
4162 struct ocfs2_xattr_bucket *bucket)
4164 int i, blocksize = inode->i_sb->s_blocksize;
4165 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4166 u16 offset, size, off_change;
4167 struct ocfs2_xattr_entry *xe;
4168 struct ocfs2_xattr_block *xb =
4169 (struct ocfs2_xattr_block *)xb_bh->b_data;
4170 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
4171 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4172 u16 count = le16_to_cpu(xb_xh->xh_count);
4173 char *src = xb_bh->b_data;
4174 char *target = bucket_block(bucket, blks - 1);
4176 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4177 (unsigned long long)xb_bh->b_blocknr,
4178 (unsigned long long)bucket_blkno(bucket));
4180 for (i = 0; i < blks; i++)
4181 memset(bucket_block(bucket, i), 0, blocksize);
4184 * Since the xe_name_offset is based on ocfs2_xattr_header,
4185 * there is a offset change corresponding to the change of
4186 * ocfs2_xattr_header's position.
4188 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4189 xe = &xb_xh->xh_entries[count - 1];
4190 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4191 size = blocksize - offset;
4193 /* copy all the names and values. */
4194 memcpy(target + offset, src + offset, size);
4196 /* Init new header now. */
4197 xh->xh_count = xb_xh->xh_count;
4198 xh->xh_num_buckets = cpu_to_le16(1);
4199 xh->xh_name_value_len = cpu_to_le16(size);
4200 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4202 /* copy all the entries. */
4203 target = bucket_block(bucket, 0);
4204 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4205 size = count * sizeof(struct ocfs2_xattr_entry);
4206 memcpy(target + offset, (char *)xb_xh + offset, size);
4208 /* Change the xe offset for all the xe because of the move. */
4209 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4210 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4211 for (i = 0; i < count; i++)
4212 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4214 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4215 offset, size, off_change);
4217 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4222 * After we move xattr from block to index btree, we have to
4223 * update ocfs2_xattr_search to the new xe and base.
4225 * When the entry is in xattr block, xattr_bh indicates the storage place.
4226 * While if the entry is in index b-tree, "bucket" indicates the
4227 * real place of the xattr.
4229 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4230 struct ocfs2_xattr_search *xs,
4231 struct buffer_head *old_bh)
4233 char *buf = old_bh->b_data;
4234 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4235 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
4238 xs->header = bucket_xh(xs->bucket);
4239 xs->base = bucket_block(xs->bucket, 0);
4240 xs->end = xs->base + inode->i_sb->s_blocksize;
4245 i = xs->here - old_xh->xh_entries;
4246 xs->here = &xs->header->xh_entries[i];
4249 static int ocfs2_xattr_create_index_block(struct inode *inode,
4250 struct ocfs2_xattr_search *xs,
4251 struct ocfs2_xattr_set_ctxt *ctxt)
4256 handle_t *handle = ctxt->handle;
4257 struct ocfs2_inode_info *oi = OCFS2_I(inode);
4258 struct buffer_head *xb_bh = xs->xattr_bh;
4259 struct ocfs2_xattr_block *xb =
4260 (struct ocfs2_xattr_block *)xb_bh->b_data;
4261 struct ocfs2_xattr_tree_root *xr;
4262 u16 xb_flags = le16_to_cpu(xb->xb_flags);
4264 mlog(0, "create xattr index block for %llu\n",
4265 (unsigned long long)xb_bh->b_blocknr);
4267 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
4268 BUG_ON(!xs->bucket);
4272 * We can use this lock for now, and maybe move to a dedicated mutex
4273 * if performance becomes a problem later.
4275 down_write(&oi->ip_alloc_sem);
4277 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
4278 OCFS2_JOURNAL_ACCESS_WRITE);
4284 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
4285 1, 1, &bit_off, &len);
4292 * The bucket may spread in many blocks, and
4293 * we will only touch the 1st block and the last block
4294 * in the whole bucket(one for entry and one for data).
4296 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4298 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4299 (unsigned long long)blkno);
4301 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
4307 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4308 OCFS2_JOURNAL_ACCESS_CREATE);
4314 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4315 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4317 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4319 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4320 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4321 offsetof(struct ocfs2_xattr_block, xb_attrs));
4323 xr = &xb->xb_attrs.xb_root;
4324 xr->xt_clusters = cpu_to_le32(1);
4325 xr->xt_last_eb_blk = 0;
4326 xr->xt_list.l_tree_depth = 0;
4327 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4328 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4330 xr->xt_list.l_recs[0].e_cpos = 0;
4331 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4332 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4334 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4336 ocfs2_journal_dirty(handle, xb_bh);
4339 up_write(&oi->ip_alloc_sem);
4344 static int cmp_xe_offset(const void *a, const void *b)
4346 const struct ocfs2_xattr_entry *l = a, *r = b;
4347 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4348 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4350 if (l_name_offset < r_name_offset)
4352 if (l_name_offset > r_name_offset)
4358 * defrag a xattr bucket if we find that the bucket has some
4359 * holes beteen name/value pairs.
4360 * We will move all the name/value pairs to the end of the bucket
4361 * so that we can spare some space for insertion.
4363 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
4365 struct ocfs2_xattr_bucket *bucket)
4368 size_t end, offset, len;
4369 struct ocfs2_xattr_header *xh;
4370 char *entries, *buf, *bucket_buf = NULL;
4371 u64 blkno = bucket_blkno(bucket);
4373 size_t blocksize = inode->i_sb->s_blocksize;
4374 struct ocfs2_xattr_entry *xe;
4377 * In order to make the operation more efficient and generic,
4378 * we copy all the blocks into a contiguous memory and do the
4379 * defragment there, so if anything is error, we will not touch
4382 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4389 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4390 memcpy(buf, bucket_block(bucket, i), blocksize);
4392 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4393 OCFS2_JOURNAL_ACCESS_WRITE);
4399 xh = (struct ocfs2_xattr_header *)bucket_buf;
4400 entries = (char *)xh->xh_entries;
4401 xh_free_start = le16_to_cpu(xh->xh_free_start);
4403 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4404 "xh_free_start = %u, xh_name_value_len = %u.\n",
4405 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4406 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4409 * sort all the entries by their offset.
4410 * the largest will be the first, so that we can
4411 * move them to the end one by one.
4413 sort(entries, le16_to_cpu(xh->xh_count),
4414 sizeof(struct ocfs2_xattr_entry),
4415 cmp_xe_offset, swap_xe);
4417 /* Move all name/values to the end of the bucket. */
4418 xe = xh->xh_entries;
4419 end = OCFS2_XATTR_BUCKET_SIZE;
4420 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4421 offset = le16_to_cpu(xe->xe_name_offset);
4422 len = namevalue_size_xe(xe);
4425 * We must make sure that the name/value pair
4426 * exist in the same block. So adjust end to
4427 * the previous block end if needed.
4429 if (((end - len) / blocksize !=
4430 (end - 1) / blocksize))
4431 end = end - end % blocksize;
4433 if (end > offset + len) {
4434 memmove(bucket_buf + end - len,
4435 bucket_buf + offset, len);
4436 xe->xe_name_offset = cpu_to_le16(end - len);
4439 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4440 "bucket %llu\n", (unsigned long long)blkno);
4445 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4446 "bucket %llu\n", (unsigned long long)blkno);
4448 if (xh_free_start == end)
4451 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4452 xh->xh_free_start = cpu_to_le16(end);
4454 /* sort the entries by their name_hash. */
4455 sort(entries, le16_to_cpu(xh->xh_count),
4456 sizeof(struct ocfs2_xattr_entry),
4460 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4461 memcpy(bucket_block(bucket, i), buf, blocksize);
4462 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4470 * prev_blkno points to the start of an existing extent. new_blkno
4471 * points to a newly allocated extent. Because we know each of our
4472 * clusters contains more than bucket, we can easily split one cluster
4473 * at a bucket boundary. So we take the last cluster of the existing
4474 * extent and split it down the middle. We move the last half of the
4475 * buckets in the last cluster of the existing extent over to the new
4478 * first_bh is the buffer at prev_blkno so we can update the existing
4479 * extent's bucket count. header_bh is the bucket were we were hoping
4480 * to insert our xattr. If the bucket move places the target in the new
4481 * extent, we'll update first_bh and header_bh after modifying the old
4484 * first_hash will be set as the 1st xe's name_hash in the new extent.
4486 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4488 struct ocfs2_xattr_bucket *first,
4489 struct ocfs2_xattr_bucket *target,
4495 struct super_block *sb = inode->i_sb;
4496 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4497 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4498 int to_move = num_buckets / 2;
4500 u64 last_cluster_blkno = bucket_blkno(first) +
4501 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4503 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4504 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4506 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4507 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
4509 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4510 last_cluster_blkno, new_blkno,
4511 to_move, first_hash);
4517 /* This is the first bucket that got moved */
4518 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4521 * If the target bucket was part of the moved buckets, we need to
4522 * update first and target.
4524 if (bucket_blkno(target) >= src_blkno) {
4525 /* Find the block for the new target bucket */
4526 src_blkno = new_blkno +
4527 (bucket_blkno(target) - src_blkno);
4529 ocfs2_xattr_bucket_relse(first);
4530 ocfs2_xattr_bucket_relse(target);
4533 * These shouldn't fail - the buffers are in the
4534 * journal from ocfs2_cp_xattr_bucket().
4536 ret = ocfs2_read_xattr_bucket(first, new_blkno);
4541 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4552 * Find the suitable pos when we divide a bucket into 2.
4553 * We have to make sure the xattrs with the same hash value exist
4554 * in the same bucket.
4556 * If this ocfs2_xattr_header covers more than one hash value, find a
4557 * place where the hash value changes. Try to find the most even split.
4558 * The most common case is that all entries have different hash values,
4559 * and the first check we make will find a place to split.
4561 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4563 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4564 int count = le16_to_cpu(xh->xh_count);
4565 int delta, middle = count / 2;
4568 * We start at the middle. Each step gets farther away in both
4569 * directions. We therefore hit the change in hash value
4570 * nearest to the middle. Note that this loop does not execute for
4573 for (delta = 0; delta < middle; delta++) {
4574 /* Let's check delta earlier than middle */
4575 if (cmp_xe(&entries[middle - delta - 1],
4576 &entries[middle - delta]))
4577 return middle - delta;
4579 /* For even counts, don't walk off the end */
4580 if ((middle + delta + 1) == count)
4583 /* Now try delta past middle */
4584 if (cmp_xe(&entries[middle + delta],
4585 &entries[middle + delta + 1]))
4586 return middle + delta + 1;
4589 /* Every entry had the same hash */
4594 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4595 * first_hash will record the 1st hash of the new bucket.
4597 * Normally half of the xattrs will be moved. But we have to make
4598 * sure that the xattrs with the same hash value are stored in the
4599 * same bucket. If all the xattrs in this bucket have the same hash
4600 * value, the new bucket will be initialized as an empty one and the
4601 * first_hash will be initialized as (hash_value+1).
4603 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4608 int new_bucket_head)
4611 int count, start, len, name_value_len = 0, name_offset = 0;
4612 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4613 struct ocfs2_xattr_header *xh;
4614 struct ocfs2_xattr_entry *xe;
4615 int blocksize = inode->i_sb->s_blocksize;
4617 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4618 (unsigned long long)blk, (unsigned long long)new_blk);
4620 s_bucket = ocfs2_xattr_bucket_new(inode);
4621 t_bucket = ocfs2_xattr_bucket_new(inode);
4622 if (!s_bucket || !t_bucket) {
4628 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4634 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4635 OCFS2_JOURNAL_ACCESS_WRITE);
4642 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4643 * there's no need to read it.
4645 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4652 * Hey, if we're overwriting t_bucket, what difference does
4653 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4654 * same part of ocfs2_cp_xattr_bucket().
4656 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4658 OCFS2_JOURNAL_ACCESS_CREATE :
4659 OCFS2_JOURNAL_ACCESS_WRITE);
4665 xh = bucket_xh(s_bucket);
4666 count = le16_to_cpu(xh->xh_count);
4667 start = ocfs2_xattr_find_divide_pos(xh);
4669 if (start == count) {
4670 xe = &xh->xh_entries[start-1];
4673 * initialized a new empty bucket here.
4674 * The hash value is set as one larger than
4675 * that of the last entry in the previous bucket.
4677 for (i = 0; i < t_bucket->bu_blocks; i++)
4678 memset(bucket_block(t_bucket, i), 0, blocksize);
4680 xh = bucket_xh(t_bucket);
4681 xh->xh_free_start = cpu_to_le16(blocksize);
4682 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4683 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4685 goto set_num_buckets;
4688 /* copy the whole bucket to the new first. */
4689 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4691 /* update the new bucket. */
4692 xh = bucket_xh(t_bucket);
4695 * Calculate the total name/value len and xh_free_start for
4696 * the old bucket first.
4698 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4700 for (i = 0; i < start; i++) {
4701 xe = &xh->xh_entries[i];
4702 name_value_len += namevalue_size_xe(xe);
4703 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4704 name_offset = le16_to_cpu(xe->xe_name_offset);
4708 * Now begin the modification to the new bucket.
4710 * In the new bucket, We just move the xattr entry to the beginning
4711 * and don't touch the name/value. So there will be some holes in the
4712 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4715 xe = &xh->xh_entries[start];
4716 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4717 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
4718 (int)((char *)xe - (char *)xh),
4719 (int)((char *)xh->xh_entries - (char *)xh));
4720 memmove((char *)xh->xh_entries, (char *)xe, len);
4721 xe = &xh->xh_entries[count - start];
4722 len = sizeof(struct ocfs2_xattr_entry) * start;
4723 memset((char *)xe, 0, len);
4725 le16_add_cpu(&xh->xh_count, -start);
4726 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4728 /* Calculate xh_free_start for the new bucket. */
4729 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4730 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4731 xe = &xh->xh_entries[i];
4732 if (le16_to_cpu(xe->xe_name_offset) <
4733 le16_to_cpu(xh->xh_free_start))
4734 xh->xh_free_start = xe->xe_name_offset;
4738 /* set xh->xh_num_buckets for the new xh. */
4739 if (new_bucket_head)
4740 xh->xh_num_buckets = cpu_to_le16(1);
4742 xh->xh_num_buckets = 0;
4744 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4746 /* store the first_hash of the new bucket. */
4748 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4751 * Now only update the 1st block of the old bucket. If we
4752 * just added a new empty bucket, there is no need to modify
4758 xh = bucket_xh(s_bucket);
4759 memset(&xh->xh_entries[start], 0,
4760 sizeof(struct ocfs2_xattr_entry) * (count - start));
4761 xh->xh_count = cpu_to_le16(start);
4762 xh->xh_free_start = cpu_to_le16(name_offset);
4763 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4765 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4768 ocfs2_xattr_bucket_free(s_bucket);
4769 ocfs2_xattr_bucket_free(t_bucket);
4775 * Copy xattr from one bucket to another bucket.
4777 * The caller must make sure that the journal transaction
4778 * has enough space for journaling.
4780 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4787 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4789 BUG_ON(s_blkno == t_blkno);
4791 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4792 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4795 s_bucket = ocfs2_xattr_bucket_new(inode);
4796 t_bucket = ocfs2_xattr_bucket_new(inode);
4797 if (!s_bucket || !t_bucket) {
4803 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4808 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4809 * there's no need to read it.
4811 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4816 * Hey, if we're overwriting t_bucket, what difference does
4817 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4818 * cluster to fill, we came here from
4819 * ocfs2_mv_xattr_buckets(), and it is really new -
4820 * ACCESS_CREATE is required. But we also might have moved data
4821 * out of t_bucket before extending back into it.
4822 * ocfs2_add_new_xattr_bucket() can do this - its call to
4823 * ocfs2_add_new_xattr_cluster() may have created a new extent
4824 * and copied out the end of the old extent. Then it re-extends
4825 * the old extent back to create space for new xattrs. That's
4826 * how we get here, and the bucket isn't really new.
4828 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4830 OCFS2_JOURNAL_ACCESS_CREATE :
4831 OCFS2_JOURNAL_ACCESS_WRITE);
4835 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4836 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4839 ocfs2_xattr_bucket_free(t_bucket);
4840 ocfs2_xattr_bucket_free(s_bucket);
4846 * src_blk points to the start of an existing extent. last_blk points to
4847 * last cluster in that extent. to_blk points to a newly allocated
4848 * extent. We copy the buckets from the cluster at last_blk to the new
4849 * extent. If start_bucket is non-zero, we skip that many buckets before
4850 * we start copying. The new extent's xh_num_buckets gets set to the
4851 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4852 * by the same amount.
4854 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4855 u64 src_blk, u64 last_blk, u64 to_blk,
4856 unsigned int start_bucket,
4859 int i, ret, credits;
4860 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4861 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4862 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4863 struct ocfs2_xattr_bucket *old_first, *new_first;
4865 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4866 (unsigned long long)last_blk, (unsigned long long)to_blk);
4868 BUG_ON(start_bucket >= num_buckets);
4870 num_buckets -= start_bucket;
4871 last_blk += (start_bucket * blks_per_bucket);
4874 /* The first bucket of the original extent */
4875 old_first = ocfs2_xattr_bucket_new(inode);
4876 /* The first bucket of the new extent */
4877 new_first = ocfs2_xattr_bucket_new(inode);
4878 if (!old_first || !new_first) {
4884 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4891 * We need to update the first bucket of the old extent and all
4892 * the buckets going to the new extent.
4894 credits = ((num_buckets + 1) * blks_per_bucket);
4895 ret = ocfs2_extend_trans(handle, credits);
4901 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4902 OCFS2_JOURNAL_ACCESS_WRITE);
4908 for (i = 0; i < num_buckets; i++) {
4909 ret = ocfs2_cp_xattr_bucket(inode, handle,
4910 last_blk + (i * blks_per_bucket),
4911 to_blk + (i * blks_per_bucket),
4920 * Get the new bucket ready before we dirty anything
4921 * (This actually shouldn't fail, because we already dirtied
4922 * it once in ocfs2_cp_xattr_bucket()).
4924 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4929 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4930 OCFS2_JOURNAL_ACCESS_WRITE);
4936 /* Now update the headers */
4937 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4938 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4940 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4941 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4944 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4947 ocfs2_xattr_bucket_free(new_first);
4948 ocfs2_xattr_bucket_free(old_first);
4953 * Move some xattrs in this cluster to the new cluster.
4954 * This function should only be called when bucket size == cluster size.
4955 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4957 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4963 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4964 int ret, credits = 2 * blk_per_bucket;
4966 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4968 ret = ocfs2_extend_trans(handle, credits);
4974 /* Move half of the xattr in start_blk to the next bucket. */
4975 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4976 new_blk, first_hash, 1);
4980 * Move some xattrs from the old cluster to the new one since they are not
4981 * contiguous in ocfs2 xattr tree.
4983 * new_blk starts a new separate cluster, and we will move some xattrs from
4984 * prev_blk to it. v_start will be set as the first name hash value in this
4985 * new cluster so that it can be used as e_cpos during tree insertion and
4986 * don't collide with our original b-tree operations. first_bh and header_bh
4987 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4988 * to extend the insert bucket.
4990 * The problem is how much xattr should we move to the new one and when should
4991 * we update first_bh and header_bh?
4992 * 1. If cluster size > bucket size, that means the previous cluster has more
4993 * than 1 bucket, so just move half nums of bucket into the new cluster and
4994 * update the first_bh and header_bh if the insert bucket has been moved
4995 * to the new cluster.
4996 * 2. If cluster_size == bucket_size:
4997 * a) If the previous extent rec has more than one cluster and the insert
4998 * place isn't in the last cluster, copy the entire last cluster to the
4999 * new one. This time, we don't need to upate the first_bh and header_bh
5000 * since they will not be moved into the new cluster.
5001 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
5002 * the new one. And we set the extend flag to zero if the insert place is
5003 * moved into the new allocated cluster since no extend is needed.
5005 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
5007 struct ocfs2_xattr_bucket *first,
5008 struct ocfs2_xattr_bucket *target,
5016 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
5017 (unsigned long long)bucket_blkno(first), prev_clusters,
5018 (unsigned long long)new_blk);
5020 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
5021 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
5030 /* The start of the last cluster in the first extent */
5031 u64 last_blk = bucket_blkno(first) +
5032 ((prev_clusters - 1) *
5033 ocfs2_clusters_to_blocks(inode->i_sb, 1));
5035 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
5036 ret = ocfs2_mv_xattr_buckets(inode, handle,
5037 bucket_blkno(first),
5038 last_blk, new_blk, 0,
5043 ret = ocfs2_divide_xattr_cluster(inode, handle,
5049 if ((bucket_blkno(target) == last_blk) && extend)
5058 * Add a new cluster for xattr storage.
5060 * If the new cluster is contiguous with the previous one, it will be
5061 * appended to the same extent record, and num_clusters will be updated.
5062 * If not, we will insert a new extent for it and move some xattrs in
5063 * the last cluster into the new allocated one.
5064 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5065 * lose the benefits of hashing because we'll have to search large leaves.
5066 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5069 * first_bh is the first block of the previous extent rec and header_bh
5070 * indicates the bucket we will insert the new xattrs. They will be updated
5071 * when the header_bh is moved into the new cluster.
5073 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5074 struct buffer_head *root_bh,
5075 struct ocfs2_xattr_bucket *first,
5076 struct ocfs2_xattr_bucket *target,
5080 struct ocfs2_xattr_set_ctxt *ctxt)
5083 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
5084 u32 prev_clusters = *num_clusters;
5085 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5087 handle_t *handle = ctxt->handle;
5088 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5089 struct ocfs2_extent_tree et;
5091 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5092 "previous xattr blkno = %llu\n",
5093 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5094 prev_cpos, (unsigned long long)bucket_blkno(first));
5096 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5098 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5099 OCFS2_JOURNAL_ACCESS_WRITE);
5105 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1,
5106 clusters_to_add, &bit_off, &num_bits);
5113 BUG_ON(num_bits > clusters_to_add);
5115 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5116 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5117 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5119 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
5120 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5121 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5123 * If this cluster is contiguous with the old one and
5124 * adding this new cluster, we don't surpass the limit of
5125 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5126 * initialized and used like other buckets in the previous
5128 * So add it as a contiguous one. The caller will handle
5131 v_start = prev_cpos + prev_clusters;
5132 *num_clusters = prev_clusters + num_bits;
5133 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5136 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5150 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
5151 num_bits, (unsigned long long)block, v_start);
5152 ret = ocfs2_insert_extent(handle, &et, v_start, block,
5153 num_bits, 0, ctxt->meta_ac);
5159 ocfs2_journal_dirty(handle, root_bh);
5166 * We are given an extent. 'first' is the bucket at the very front of
5167 * the extent. The extent has space for an additional bucket past
5168 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5169 * of the target bucket. We wish to shift every bucket past the target
5170 * down one, filling in that additional space. When we get back to the
5171 * target, we split the target between itself and the now-empty bucket
5172 * at target+1 (aka, target_blkno + blks_per_bucket).
5174 static int ocfs2_extend_xattr_bucket(struct inode *inode,
5176 struct ocfs2_xattr_bucket *first,
5181 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5182 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5184 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
5186 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
5187 "from %llu, len = %u\n", (unsigned long long)target_blk,
5188 (unsigned long long)bucket_blkno(first), num_clusters);
5190 /* The extent must have room for an additional bucket */
5191 BUG_ON(new_bucket >=
5192 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
5194 /* end_blk points to the last existing bucket */
5195 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
5198 * end_blk is the start of the last existing bucket.
5199 * Thus, (end_blk - target_blk) covers the target bucket and
5200 * every bucket after it up to, but not including, the last
5201 * existing bucket. Then we add the last existing bucket, the
5202 * new bucket, and the first bucket (3 * blk_per_bucket).
5204 credits = (end_blk - target_blk) + (3 * blk_per_bucket);
5205 ret = ocfs2_extend_trans(handle, credits);
5211 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5212 OCFS2_JOURNAL_ACCESS_WRITE);
5218 while (end_blk != target_blk) {
5219 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5220 end_blk + blk_per_bucket, 0);
5223 end_blk -= blk_per_bucket;
5226 /* Move half of the xattr in target_blkno to the next bucket. */
5227 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5228 target_blk + blk_per_bucket, NULL, 0);
5230 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5231 ocfs2_xattr_bucket_journal_dirty(handle, first);
5238 * Add new xattr bucket in an extent record and adjust the buckets
5239 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5240 * bucket we want to insert into.
5242 * In the easy case, we will move all the buckets after target down by
5243 * one. Half of target's xattrs will be moved to the next bucket.
5245 * If current cluster is full, we'll allocate a new one. This may not
5246 * be contiguous. The underlying calls will make sure that there is
5247 * space for the insert, shifting buckets around if necessary.
5248 * 'target' may be moved by those calls.
5250 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5251 struct buffer_head *xb_bh,
5252 struct ocfs2_xattr_bucket *target,
5253 struct ocfs2_xattr_set_ctxt *ctxt)
5255 struct ocfs2_xattr_block *xb =
5256 (struct ocfs2_xattr_block *)xb_bh->b_data;
5257 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5258 struct ocfs2_extent_list *el = &xb_root->xt_list;
5260 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
5261 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5262 int ret, num_buckets, extend = 1;
5264 u32 e_cpos, num_clusters;
5265 /* The bucket at the front of the extent */
5266 struct ocfs2_xattr_bucket *first;
5268 mlog(0, "Add new xattr bucket starting from %llu\n",
5269 (unsigned long long)bucket_blkno(target));
5271 /* The first bucket of the original extent */
5272 first = ocfs2_xattr_bucket_new(inode);
5279 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5286 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5292 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5293 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5295 * This can move first+target if the target bucket moves
5296 * to the new extent.
5298 ret = ocfs2_add_new_xattr_cluster(inode,
5313 ret = ocfs2_extend_xattr_bucket(inode,
5316 bucket_blkno(target),
5323 ocfs2_xattr_bucket_free(first);
5328 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5329 struct ocfs2_xattr_bucket *bucket,
5332 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5334 offs = offs % inode->i_sb->s_blocksize;
5335 return bucket_block(bucket, block_off) + offs;
5339 * Truncate the specified xe_off entry in xattr bucket.
5340 * bucket is indicated by header_bh and len is the new length.
5341 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5343 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5345 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5346 struct ocfs2_xattr_bucket *bucket,
5349 struct ocfs2_xattr_set_ctxt *ctxt)
5353 struct ocfs2_xattr_entry *xe;
5354 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5355 size_t blocksize = inode->i_sb->s_blocksize;
5356 struct ocfs2_xattr_value_buf vb = {
5357 .vb_access = ocfs2_journal_access,
5360 xe = &xh->xh_entries[xe_off];
5362 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5364 offset = le16_to_cpu(xe->xe_name_offset) +
5365 OCFS2_XATTR_SIZE(xe->xe_name_len);
5367 value_blk = offset / blocksize;
5369 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5370 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
5372 vb.vb_bh = bucket->bu_bhs[value_blk];
5375 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5376 (vb.vb_bh->b_data + offset % blocksize);
5379 * From here on out we have to dirty the bucket. The generic
5380 * value calls only modify one of the bucket's bhs, but we need
5381 * to send the bucket at once. So if they error, they *could* have
5382 * modified something. We have to assume they did, and dirty
5383 * the whole bucket. This leaves us in a consistent state.
5385 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5386 xe_off, (unsigned long long)bucket_blkno(bucket), len);
5387 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5393 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5394 OCFS2_JOURNAL_ACCESS_WRITE);
5400 xe->xe_value_size = cpu_to_le64(len);
5402 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5408 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5409 struct buffer_head *root_bh,
5416 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5417 struct inode *tl_inode = osb->osb_tl_inode;
5419 struct ocfs2_xattr_block *xb =
5420 (struct ocfs2_xattr_block *)root_bh->b_data;
5421 struct ocfs2_alloc_context *meta_ac = NULL;
5422 struct ocfs2_cached_dealloc_ctxt dealloc;
5423 struct ocfs2_extent_tree et;
5425 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5426 ocfs2_delete_xattr_in_bucket, para);
5432 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5434 ocfs2_init_dealloc_ctxt(&dealloc);
5436 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5437 cpos, len, (unsigned long long)blkno);
5439 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5442 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5448 mutex_lock(&tl_inode->i_mutex);
5450 if (ocfs2_truncate_log_needs_flush(osb)) {
5451 ret = __ocfs2_flush_truncate_log(osb);
5458 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5459 if (IS_ERR(handle)) {
5465 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5466 OCFS2_JOURNAL_ACCESS_WRITE);
5472 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5479 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5480 ocfs2_journal_dirty(handle, root_bh);
5482 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5487 ocfs2_commit_trans(osb, handle);
5489 ocfs2_schedule_truncate_log_flush(osb, 1);
5491 mutex_unlock(&tl_inode->i_mutex);
5494 ocfs2_free_alloc_context(meta_ac);
5496 ocfs2_run_deallocs(osb, &dealloc);
5502 * check whether the xattr bucket is filled up with the same hash value.
5503 * If we want to insert the xattr with the same hash, return -ENOSPC.
5504 * If we want to insert a xattr with different hash value, go ahead
5505 * and ocfs2_divide_xattr_bucket will handle this.
5507 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5508 struct ocfs2_xattr_bucket *bucket,
5511 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5512 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5514 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5517 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5518 xh->xh_entries[0].xe_name_hash) {
5519 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5521 (unsigned long long)bucket_blkno(bucket),
5522 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5530 * Try to set the entry in the current bucket. If we fail, the caller
5531 * will handle getting us another bucket.
5533 static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
5534 struct ocfs2_xattr_info *xi,
5535 struct ocfs2_xattr_search *xs,
5536 struct ocfs2_xattr_set_ctxt *ctxt)
5539 struct ocfs2_xa_loc loc;
5541 mlog_entry("Set xattr %s in xattr bucket\n", xi->xi_name);
5543 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5544 xs->not_found ? NULL : xs->here);
5545 ret = ocfs2_xa_set(&loc, xi, ctxt);
5547 xs->here = loc.xl_entry;
5550 if (ret != -ENOSPC) {
5555 /* Ok, we need space. Let's try defragmenting the bucket. */
5556 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5563 ret = ocfs2_xa_set(&loc, xi, ctxt);
5565 xs->here = loc.xl_entry;
5577 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5578 struct ocfs2_xattr_info *xi,
5579 struct ocfs2_xattr_search *xs,
5580 struct ocfs2_xattr_set_ctxt *ctxt)
5584 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
5586 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5589 if (ret != -ENOSPC) {
5594 /* Ack, need more space. Let's try to get another bucket! */
5597 * We do not allow for overlapping ranges between buckets. And
5598 * the maximum number of collisions we will allow for then is
5599 * one bucket's worth, so check it here whether we need to
5600 * add a new bucket for the insert.
5602 ret = ocfs2_check_xattr_bucket_collision(inode,
5610 ret = ocfs2_add_new_xattr_bucket(inode,
5620 * ocfs2_add_new_xattr_bucket() will have updated
5621 * xs->bucket if it moved, but it will not have updated
5622 * any of the other search fields. Thus, we drop it and
5623 * re-search. Everything should be cached, so it'll be
5626 ocfs2_xattr_bucket_relse(xs->bucket);
5627 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5630 if (ret && ret != -ENODATA)
5632 xs->not_found = ret;
5634 /* Ok, we have a new bucket, let's try again */
5635 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5636 if (ret && (ret != -ENOSPC))
5644 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5645 struct ocfs2_xattr_bucket *bucket,
5648 int ret = 0, ref_credits;
5649 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5651 struct ocfs2_xattr_entry *xe;
5652 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5653 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5654 int credits = ocfs2_remove_extent_credits(osb->sb) +
5655 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5656 struct ocfs2_xattr_value_root *xv;
5657 struct ocfs2_rm_xattr_bucket_para *args =
5658 (struct ocfs2_rm_xattr_bucket_para *)para;
5660 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5662 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5663 xe = &xh->xh_entries[i];
5664 if (ocfs2_xattr_is_local(xe))
5667 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5670 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5676 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5677 if (IS_ERR(ctxt.handle)) {
5678 ret = PTR_ERR(ctxt.handle);
5683 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5686 ocfs2_commit_trans(osb, ctxt.handle);
5688 ocfs2_free_alloc_context(ctxt.meta_ac);
5689 ctxt.meta_ac = NULL;
5698 ocfs2_free_alloc_context(ctxt.meta_ac);
5699 ocfs2_schedule_truncate_log_flush(osb, 1);
5700 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5705 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5706 * or change the extent record flag), we need to recalculate
5707 * the metaecc for the whole bucket. So it is done here.
5710 * We have to give the extra credits for the caller.
5712 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5717 struct ocfs2_xattr_bucket *bucket =
5718 (struct ocfs2_xattr_bucket *)para;
5720 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5721 OCFS2_JOURNAL_ACCESS_WRITE);
5727 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5733 * Special action we need if the xattr value is refcounted.
5735 * 1. If the xattr is refcounted, lock the tree.
5736 * 2. CoW the xattr if we are setting the new value and the value
5737 * will be stored outside.
5738 * 3. In other case, decrease_refcount will work for us, so just
5739 * lock the refcount tree, calculate the meta and credits is OK.
5741 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5742 * currently CoW is a completed transaction, while this function
5743 * will also lock the allocators and let us deadlock. So we will
5744 * CoW the whole xattr value.
5746 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5747 struct ocfs2_dinode *di,
5748 struct ocfs2_xattr_info *xi,
5749 struct ocfs2_xattr_search *xis,
5750 struct ocfs2_xattr_search *xbs,
5751 struct ocfs2_refcount_tree **ref_tree,
5756 struct ocfs2_xattr_block *xb;
5757 struct ocfs2_xattr_entry *xe;
5759 u32 p_cluster, num_clusters;
5760 unsigned int ext_flags;
5761 int name_offset, name_len;
5762 struct ocfs2_xattr_value_buf vb;
5763 struct ocfs2_xattr_bucket *bucket = NULL;
5764 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5765 struct ocfs2_post_refcount refcount;
5766 struct ocfs2_post_refcount *p = NULL;
5767 struct buffer_head *ref_root_bh = NULL;
5769 if (!xis->not_found) {
5771 name_offset = le16_to_cpu(xe->xe_name_offset);
5772 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5774 vb.vb_bh = xis->inode_bh;
5775 vb.vb_access = ocfs2_journal_access_di;
5777 int i, block_off = 0;
5778 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5780 name_offset = le16_to_cpu(xe->xe_name_offset);
5781 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5782 i = xbs->here - xbs->header->xh_entries;
5784 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5785 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5786 bucket_xh(xbs->bucket),
5793 base = bucket_block(xbs->bucket, block_off);
5794 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5795 vb.vb_access = ocfs2_journal_access;
5797 if (ocfs2_meta_ecc(osb)) {
5798 /*create parameters for ocfs2_post_refcount. */
5799 bucket = xbs->bucket;
5800 refcount.credits = bucket->bu_blocks;
5801 refcount.para = bucket;
5803 ocfs2_xattr_bucket_post_refcount;
5808 vb.vb_bh = xbs->xattr_bh;
5809 vb.vb_access = ocfs2_journal_access_xb;
5813 if (ocfs2_xattr_is_local(xe))
5816 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5817 (base + name_offset + name_len);
5819 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5820 &num_clusters, &vb.vb_xv->xr_list,
5828 * We just need to check the 1st extent record, since we always
5829 * CoW the whole xattr. So there shouldn't be a xattr with
5830 * some REFCOUNT extent recs after the 1st one.
5832 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5835 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5836 1, ref_tree, &ref_root_bh);
5843 * If we are deleting the xattr or the new size will be stored inside,
5844 * cool, leave it there, the xattr truncate process will remove them
5845 * for us(it still needs the refcount tree lock and the meta, credits).
5846 * And the worse case is that every cluster truncate will split the
5847 * refcount tree, and make the original extent become 3. So we will need
5848 * 2 * cluster more extent recs at most.
5850 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
5852 ret = ocfs2_refcounted_xattr_delete_need(inode,
5853 &(*ref_tree)->rf_ci,
5854 ref_root_bh, vb.vb_xv,
5861 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5862 *ref_tree, ref_root_bh, 0,
5863 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5868 brelse(ref_root_bh);
5873 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5874 * The physical clusters will be added to refcount tree.
5876 static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5877 struct ocfs2_xattr_value_root *xv,
5878 struct ocfs2_extent_tree *value_et,
5879 struct ocfs2_caching_info *ref_ci,
5880 struct buffer_head *ref_root_bh,
5881 struct ocfs2_cached_dealloc_ctxt *dealloc,
5882 struct ocfs2_post_refcount *refcount)
5885 u32 clusters = le32_to_cpu(xv->xr_clusters);
5886 u32 cpos, p_cluster, num_clusters;
5887 struct ocfs2_extent_list *el = &xv->xr_list;
5888 unsigned int ext_flags;
5891 while (cpos < clusters) {
5892 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5893 &num_clusters, el, &ext_flags);
5895 cpos += num_clusters;
5896 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5901 ret = ocfs2_add_refcount_flag(inode, value_et,
5902 ref_ci, ref_root_bh,
5903 cpos - num_clusters,
5904 p_cluster, num_clusters,
5916 * Given a normal ocfs2_xattr_header, refcount all the entries which
5917 * have value stored outside.
5918 * Used for xattrs stored in inode and ocfs2_xattr_block.
5920 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5921 struct ocfs2_xattr_value_buf *vb,
5922 struct ocfs2_xattr_header *header,
5923 struct ocfs2_caching_info *ref_ci,
5924 struct buffer_head *ref_root_bh,
5925 struct ocfs2_cached_dealloc_ctxt *dealloc)
5928 struct ocfs2_xattr_entry *xe;
5929 struct ocfs2_xattr_value_root *xv;
5930 struct ocfs2_extent_tree et;
5933 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5934 xe = &header->xh_entries[i];
5936 if (ocfs2_xattr_is_local(xe))
5939 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5940 le16_to_cpu(xe->xe_name_offset) +
5941 OCFS2_XATTR_SIZE(xe->xe_name_len));
5944 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5946 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5947 ref_ci, ref_root_bh,
5958 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5959 struct buffer_head *fe_bh,
5960 struct ocfs2_caching_info *ref_ci,
5961 struct buffer_head *ref_root_bh,
5962 struct ocfs2_cached_dealloc_ctxt *dealloc)
5964 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5965 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5966 (fe_bh->b_data + inode->i_sb->s_blocksize -
5967 le16_to_cpu(di->i_xattr_inline_size));
5968 struct ocfs2_xattr_value_buf vb = {
5970 .vb_access = ocfs2_journal_access_di,
5973 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5974 ref_ci, ref_root_bh, dealloc);
5977 struct ocfs2_xattr_tree_value_refcount_para {
5978 struct ocfs2_caching_info *ref_ci;
5979 struct buffer_head *ref_root_bh;
5980 struct ocfs2_cached_dealloc_ctxt *dealloc;
5983 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5984 struct ocfs2_xattr_bucket *bucket,
5986 struct ocfs2_xattr_value_root **xv,
5987 struct buffer_head **bh)
5989 int ret, block_off, name_offset;
5990 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5991 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5994 ret = ocfs2_xattr_bucket_get_name_value(sb,
6004 base = bucket_block(bucket, block_off);
6006 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6007 OCFS2_XATTR_SIZE(xe->xe_name_len));
6010 *bh = bucket->bu_bhs[block_off];
6016 * For a given xattr bucket, refcount all the entries which
6017 * have value stored outside.
6019 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6020 struct ocfs2_xattr_bucket *bucket,
6024 struct ocfs2_extent_tree et;
6025 struct ocfs2_xattr_tree_value_refcount_para *ref =
6026 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6027 struct ocfs2_xattr_header *xh =
6028 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6029 struct ocfs2_xattr_entry *xe;
6030 struct ocfs2_xattr_value_buf vb = {
6031 .vb_access = ocfs2_journal_access,
6033 struct ocfs2_post_refcount refcount = {
6034 .credits = bucket->bu_blocks,
6036 .func = ocfs2_xattr_bucket_post_refcount,
6038 struct ocfs2_post_refcount *p = NULL;
6040 /* We only need post_refcount if we support metaecc. */
6041 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6044 mlog(0, "refcount bucket %llu, count = %u\n",
6045 (unsigned long long)bucket_blkno(bucket),
6046 le16_to_cpu(xh->xh_count));
6047 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6048 xe = &xh->xh_entries[i];
6050 if (ocfs2_xattr_is_local(xe))
6053 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6054 &vb.vb_xv, &vb.vb_bh);
6060 ocfs2_init_xattr_value_extent_tree(&et,
6061 INODE_CACHE(inode), &vb);
6063 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6077 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6078 struct buffer_head *root_bh,
6079 u64 blkno, u32 cpos, u32 len, void *para)
6081 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6082 ocfs2_xattr_bucket_value_refcount,
6086 static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6087 struct buffer_head *blk_bh,
6088 struct ocfs2_caching_info *ref_ci,
6089 struct buffer_head *ref_root_bh,
6090 struct ocfs2_cached_dealloc_ctxt *dealloc)
6093 struct ocfs2_xattr_block *xb =
6094 (struct ocfs2_xattr_block *)blk_bh->b_data;
6096 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6097 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6098 struct ocfs2_xattr_value_buf vb = {
6100 .vb_access = ocfs2_journal_access_xb,
6103 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6104 ref_ci, ref_root_bh,
6107 struct ocfs2_xattr_tree_value_refcount_para para = {
6109 .ref_root_bh = ref_root_bh,
6113 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6114 ocfs2_refcount_xattr_tree_rec,
6121 int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6122 struct buffer_head *fe_bh,
6123 struct ocfs2_caching_info *ref_ci,
6124 struct buffer_head *ref_root_bh,
6125 struct ocfs2_cached_dealloc_ctxt *dealloc)
6128 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6129 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6130 struct buffer_head *blk_bh = NULL;
6132 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6133 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6134 ref_ci, ref_root_bh,
6142 if (!di->i_xattr_loc)
6145 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6152 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6153 ref_root_bh, dealloc);
6163 typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
6165 * Store the information we need in xattr reflink.
6166 * old_bh and new_bh are inode bh for the old and new inode.
6168 struct ocfs2_xattr_reflink {
6169 struct inode *old_inode;
6170 struct inode *new_inode;
6171 struct buffer_head *old_bh;
6172 struct buffer_head *new_bh;
6173 struct ocfs2_caching_info *ref_ci;
6174 struct buffer_head *ref_root_bh;
6175 struct ocfs2_cached_dealloc_ctxt *dealloc;
6176 should_xattr_reflinked *xattr_reflinked;
6180 * Given a xattr header and xe offset,
6181 * return the proper xv and the corresponding bh.
6182 * xattr in inode, block and xattr tree have different implementaions.
6184 typedef int (get_xattr_value_root)(struct super_block *sb,
6185 struct buffer_head *bh,
6186 struct ocfs2_xattr_header *xh,
6188 struct ocfs2_xattr_value_root **xv,
6189 struct buffer_head **ret_bh,
6193 * Calculate all the xattr value root metadata stored in this xattr header and
6194 * credits we need if we create them from the scratch.
6195 * We use get_xattr_value_root so that all types of xattr container can use it.
6197 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6198 struct buffer_head *bh,
6199 struct ocfs2_xattr_header *xh,
6200 int *metas, int *credits,
6202 get_xattr_value_root *func,
6206 struct ocfs2_xattr_value_root *xv;
6207 struct ocfs2_xattr_entry *xe;
6209 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6210 xe = &xh->xh_entries[i];
6211 if (ocfs2_xattr_is_local(xe))
6214 ret = func(sb, bh, xh, i, &xv, NULL, para);
6220 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6221 le16_to_cpu(xv->xr_list.l_next_free_rec);
6223 *credits += ocfs2_calc_extend_credits(sb,
6225 le32_to_cpu(xv->xr_clusters));
6228 * If the value is a tree with depth > 1, We don't go deep
6229 * to the extent block, so just calculate a maximum record num.
6231 if (!xv->xr_list.l_tree_depth)
6232 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
6234 *num_recs += ocfs2_clusters_for_bytes(sb,
6241 /* Used by xattr inode and block to return the right xv and buffer_head. */
6242 static int ocfs2_get_xattr_value_root(struct super_block *sb,
6243 struct buffer_head *bh,
6244 struct ocfs2_xattr_header *xh,
6246 struct ocfs2_xattr_value_root **xv,
6247 struct buffer_head **ret_bh,
6250 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6252 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6253 le16_to_cpu(xe->xe_name_offset) +
6254 OCFS2_XATTR_SIZE(xe->xe_name_len));
6263 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6264 * It is only used for inline xattr and xattr block.
6266 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6267 struct ocfs2_xattr_header *xh,
6268 struct buffer_head *ref_root_bh,
6270 struct ocfs2_alloc_context **meta_ac)
6272 int ret, meta_add = 0, num_recs = 0;
6273 struct ocfs2_refcount_block *rb =
6274 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6278 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6279 &meta_add, credits, &num_recs,
6280 ocfs2_get_xattr_value_root,
6288 * We need to add/modify num_recs in refcount tree, so just calculate
6289 * an approximate number we need for refcount tree change.
6290 * Sometimes we need to split the tree, and after split, half recs
6291 * will be moved to the new block, and a new block can only provide
6292 * half number of recs. So we multiple new blocks by 2.
6294 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6295 meta_add += num_recs;
6296 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6297 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6298 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6299 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6303 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6312 * Given a xattr header, reflink all the xattrs in this container.
6313 * It can be used for inode, block and bucket.
6316 * Before we call this function, the caller has memcpy the xattr in
6317 * old_xh to the new_xh.
6319 * If args.xattr_reflinked is set, call it to decide whether the xe should
6320 * be reflinked or not. If not, remove it from the new xattr header.
6322 static int ocfs2_reflink_xattr_header(handle_t *handle,
6323 struct ocfs2_xattr_reflink *args,
6324 struct buffer_head *old_bh,
6325 struct ocfs2_xattr_header *xh,
6326 struct buffer_head *new_bh,
6327 struct ocfs2_xattr_header *new_xh,
6328 struct ocfs2_xattr_value_buf *vb,
6329 struct ocfs2_alloc_context *meta_ac,
6330 get_xattr_value_root *func,
6334 struct super_block *sb = args->old_inode->i_sb;
6335 struct buffer_head *value_bh;
6336 struct ocfs2_xattr_entry *xe, *last;
6337 struct ocfs2_xattr_value_root *xv, *new_xv;
6338 struct ocfs2_extent_tree data_et;
6339 u32 clusters, cpos, p_cluster, num_clusters;
6340 unsigned int ext_flags = 0;
6342 mlog(0, "reflink xattr in container %llu, count = %u\n",
6343 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
6345 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6346 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
6347 xe = &xh->xh_entries[i];
6349 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6350 xe = &new_xh->xh_entries[j];
6352 le16_add_cpu(&new_xh->xh_count, -1);
6353 if (new_xh->xh_count) {
6355 (void *)last - (void *)xe);
6357 sizeof(struct ocfs2_xattr_entry));
6361 * We don't want j to increase in the next round since
6362 * it is already moved ahead.
6368 if (ocfs2_xattr_is_local(xe))
6371 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6377 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6384 * For the xattr which has l_tree_depth = 0, all the extent
6385 * recs have already be copied to the new xh with the
6386 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6387 * increase the refount count int the refcount tree.
6389 * For the xattr which has l_tree_depth > 0, we need
6390 * to initialize it to the empty default value root,
6391 * and then insert the extents one by one.
6393 if (xv->xr_list.l_tree_depth) {
6394 memcpy(new_xv, &def_xv, sizeof(def_xv));
6396 vb->vb_bh = value_bh;
6397 ocfs2_init_xattr_value_extent_tree(&data_et,
6398 INODE_CACHE(args->new_inode), vb);
6401 clusters = le32_to_cpu(xv->xr_clusters);
6403 while (cpos < clusters) {
6404 ret = ocfs2_xattr_get_clusters(args->old_inode,
6417 if (xv->xr_list.l_tree_depth) {
6418 ret = ocfs2_insert_extent(handle,
6420 ocfs2_clusters_to_blocks(
6421 args->old_inode->i_sb,
6423 num_clusters, ext_flags,
6431 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6433 p_cluster, num_clusters,
6434 meta_ac, args->dealloc);
6440 cpos += num_clusters;
6448 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6450 int ret = 0, credits = 0;
6452 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6453 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6454 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6455 int header_off = osb->sb->s_blocksize - inline_size;
6456 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6457 (args->old_bh->b_data + header_off);
6458 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6459 (args->new_bh->b_data + header_off);
6460 struct ocfs2_alloc_context *meta_ac = NULL;
6461 struct ocfs2_inode_info *new_oi;
6462 struct ocfs2_dinode *new_di;
6463 struct ocfs2_xattr_value_buf vb = {
6464 .vb_bh = args->new_bh,
6465 .vb_access = ocfs2_journal_access_di,
6468 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6469 &credits, &meta_ac);
6475 handle = ocfs2_start_trans(osb, credits);
6476 if (IS_ERR(handle)) {
6477 ret = PTR_ERR(handle);
6482 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6483 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6489 memcpy(args->new_bh->b_data + header_off,
6490 args->old_bh->b_data + header_off, inline_size);
6492 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6493 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6495 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6496 args->new_bh, new_xh, &vb, meta_ac,
6497 ocfs2_get_xattr_value_root, NULL);
6503 new_oi = OCFS2_I(args->new_inode);
6504 spin_lock(&new_oi->ip_lock);
6505 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6506 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6507 spin_unlock(&new_oi->ip_lock);
6509 ocfs2_journal_dirty(handle, args->new_bh);
6512 ocfs2_commit_trans(osb, handle);
6516 ocfs2_free_alloc_context(meta_ac);
6520 static int ocfs2_create_empty_xattr_block(struct inode *inode,
6521 struct buffer_head *fe_bh,
6522 struct buffer_head **ret_bh,
6526 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6527 struct ocfs2_xattr_set_ctxt ctxt;
6529 memset(&ctxt, 0, sizeof(ctxt));
6530 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
6536 ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6537 if (IS_ERR(ctxt.handle)) {
6538 ret = PTR_ERR(ctxt.handle);
6543 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6544 (unsigned long long)fe_bh->b_blocknr, indexed);
6545 ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6550 ocfs2_commit_trans(osb, ctxt.handle);
6552 ocfs2_free_alloc_context(ctxt.meta_ac);
6556 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6557 struct buffer_head *blk_bh,
6558 struct buffer_head *new_blk_bh)
6560 int ret = 0, credits = 0;
6562 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6563 struct ocfs2_dinode *new_di;
6564 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6565 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6566 struct ocfs2_xattr_block *xb =
6567 (struct ocfs2_xattr_block *)blk_bh->b_data;
6568 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6569 struct ocfs2_xattr_block *new_xb =
6570 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6571 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6572 struct ocfs2_alloc_context *meta_ac;
6573 struct ocfs2_xattr_value_buf vb = {
6574 .vb_bh = new_blk_bh,
6575 .vb_access = ocfs2_journal_access_xb,
6578 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6579 &credits, &meta_ac);
6585 /* One more credits in case we need to add xattr flags in new inode. */
6586 handle = ocfs2_start_trans(osb, credits + 1);
6587 if (IS_ERR(handle)) {
6588 ret = PTR_ERR(handle);
6593 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6594 ret = ocfs2_journal_access_di(handle,
6595 INODE_CACHE(args->new_inode),
6597 OCFS2_JOURNAL_ACCESS_WRITE);
6604 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6605 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6611 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6612 osb->sb->s_blocksize - header_off);
6614 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6615 new_blk_bh, new_xh, &vb, meta_ac,
6616 ocfs2_get_xattr_value_root, NULL);
6622 ocfs2_journal_dirty(handle, new_blk_bh);
6624 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6625 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6626 spin_lock(&new_oi->ip_lock);
6627 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6628 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6629 spin_unlock(&new_oi->ip_lock);
6631 ocfs2_journal_dirty(handle, args->new_bh);
6635 ocfs2_commit_trans(osb, handle);
6638 ocfs2_free_alloc_context(meta_ac);
6642 struct ocfs2_reflink_xattr_tree_args {
6643 struct ocfs2_xattr_reflink *reflink;
6644 struct buffer_head *old_blk_bh;
6645 struct buffer_head *new_blk_bh;
6646 struct ocfs2_xattr_bucket *old_bucket;
6647 struct ocfs2_xattr_bucket *new_bucket;
6652 * We have to handle the case that both old bucket and new bucket
6653 * will call this function to get the right ret_bh.
6654 * So The caller must give us the right bh.
6656 static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6657 struct buffer_head *bh,
6658 struct ocfs2_xattr_header *xh,
6660 struct ocfs2_xattr_value_root **xv,
6661 struct buffer_head **ret_bh,
6664 struct ocfs2_reflink_xattr_tree_args *args =
6665 (struct ocfs2_reflink_xattr_tree_args *)para;
6666 struct ocfs2_xattr_bucket *bucket;
6668 if (bh == args->old_bucket->bu_bhs[0])
6669 bucket = args->old_bucket;
6671 bucket = args->new_bucket;
6673 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6677 struct ocfs2_value_tree_metas {
6683 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6684 struct buffer_head *bh,
6685 struct ocfs2_xattr_header *xh,
6687 struct ocfs2_xattr_value_root **xv,
6688 struct buffer_head **ret_bh,
6691 struct ocfs2_xattr_bucket *bucket =
6692 (struct ocfs2_xattr_bucket *)para;
6694 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6698 static int ocfs2_calc_value_tree_metas(struct inode *inode,
6699 struct ocfs2_xattr_bucket *bucket,
6702 struct ocfs2_value_tree_metas *metas =
6703 (struct ocfs2_value_tree_metas *)para;
6704 struct ocfs2_xattr_header *xh =
6705 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6707 /* Add the credits for this bucket first. */
6708 metas->credits += bucket->bu_blocks;
6709 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6710 xh, &metas->num_metas,
6711 &metas->credits, &metas->num_recs,
6712 ocfs2_value_tree_metas_in_bucket,
6717 * Given a xattr extent rec starting from blkno and having len clusters,
6718 * iterate all the buckets calculate how much metadata we need for reflinking
6719 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6721 static int ocfs2_lock_reflink_xattr_rec_allocators(
6722 struct ocfs2_reflink_xattr_tree_args *args,
6723 struct ocfs2_extent_tree *xt_et,
6724 u64 blkno, u32 len, int *credits,
6725 struct ocfs2_alloc_context **meta_ac,
6726 struct ocfs2_alloc_context **data_ac)
6728 int ret, num_free_extents;
6729 struct ocfs2_value_tree_metas metas;
6730 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6731 struct ocfs2_refcount_block *rb;
6733 memset(&metas, 0, sizeof(metas));
6735 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6736 ocfs2_calc_value_tree_metas, &metas);
6742 *credits = metas.credits;
6745 * Calculate we need for refcount tree change.
6747 * We need to add/modify num_recs in refcount tree, so just calculate
6748 * an approximate number we need for refcount tree change.
6749 * Sometimes we need to split the tree, and after split, half recs
6750 * will be moved to the new block, and a new block can only provide
6751 * half number of recs. So we multiple new blocks by 2.
6752 * In the end, we have to add credits for modifying the already
6753 * existed refcount block.
6755 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6757 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6758 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6759 metas.num_metas += metas.num_recs;
6760 *credits += metas.num_recs +
6761 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6762 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6763 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6764 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6768 /* count in the xattr tree change. */
6769 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6770 if (num_free_extents < 0) {
6771 ret = num_free_extents;
6776 if (num_free_extents < len)
6777 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6779 *credits += ocfs2_calc_extend_credits(osb->sb,
6780 xt_et->et_root_el, len);
6782 if (metas.num_metas) {
6783 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6792 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6799 ocfs2_free_alloc_context(*meta_ac);
6807 static int ocfs2_reflink_xattr_bucket(handle_t *handle,
6808 u64 blkno, u64 new_blkno, u32 clusters,
6809 u32 *cpos, int num_buckets,
6810 struct ocfs2_alloc_context *meta_ac,
6811 struct ocfs2_alloc_context *data_ac,
6812 struct ocfs2_reflink_xattr_tree_args *args)
6815 struct super_block *sb = args->reflink->old_inode->i_sb;
6816 int bpb = args->old_bucket->bu_blocks;
6817 struct ocfs2_xattr_value_buf vb = {
6818 .vb_access = ocfs2_journal_access,
6821 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6822 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6828 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6834 ret = ocfs2_xattr_bucket_journal_access(handle,
6836 OCFS2_JOURNAL_ACCESS_CREATE);
6842 for (j = 0; j < bpb; j++)
6843 memcpy(bucket_block(args->new_bucket, j),
6844 bucket_block(args->old_bucket, j),
6848 * Record the start cpos so that we can use it to initialize
6849 * our xattr tree we also set the xh_num_bucket for the new
6853 *cpos = le32_to_cpu(bucket_xh(args->new_bucket)->
6854 xh_entries[0].xe_name_hash);
6855 bucket_xh(args->new_bucket)->xh_num_buckets =
6856 cpu_to_le16(num_buckets);
6859 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6861 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6862 args->old_bucket->bu_bhs[0],
6863 bucket_xh(args->old_bucket),
6864 args->new_bucket->bu_bhs[0],
6865 bucket_xh(args->new_bucket),
6867 ocfs2_get_reflink_xattr_value_root,
6875 * Re-access and dirty the bucket to calculate metaecc.
6876 * Because we may extend the transaction in reflink_xattr_header
6877 * which will let the already accessed block gone.
6879 ret = ocfs2_xattr_bucket_journal_access(handle,
6881 OCFS2_JOURNAL_ACCESS_WRITE);
6887 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6889 ocfs2_xattr_bucket_relse(args->old_bucket);
6890 ocfs2_xattr_bucket_relse(args->new_bucket);
6893 ocfs2_xattr_bucket_relse(args->old_bucket);
6894 ocfs2_xattr_bucket_relse(args->new_bucket);
6898 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6899 struct inode *inode,
6900 struct ocfs2_reflink_xattr_tree_args *args,
6901 struct ocfs2_extent_tree *et,
6902 struct ocfs2_alloc_context *meta_ac,
6903 struct ocfs2_alloc_context *data_ac,
6904 u64 blkno, u32 cpos, u32 len)
6906 int ret, first_inserted = 0;
6907 u32 p_cluster, num_clusters, reflink_cpos = 0;
6909 unsigned int num_buckets, reflink_buckets;
6911 ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
6913 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6918 num_buckets = le16_to_cpu(bucket_xh(args->old_bucket)->xh_num_buckets);
6919 ocfs2_xattr_bucket_relse(args->old_bucket);
6921 while (len && num_buckets) {
6922 ret = ocfs2_claim_clusters(handle, data_ac,
6923 1, &p_cluster, &num_clusters);
6929 new_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
6930 reflink_buckets = min(num_buckets, bpc * num_clusters);
6932 ret = ocfs2_reflink_xattr_bucket(handle, blkno,
6933 new_blkno, num_clusters,
6934 &reflink_cpos, reflink_buckets,
6935 meta_ac, data_ac, args);
6942 * For the 1st allocated cluster, we make it use the same cpos
6943 * so that the xattr tree looks the same as the original one
6946 if (!first_inserted) {
6947 reflink_cpos = cpos;
6950 ret = ocfs2_insert_extent(handle, et, reflink_cpos, new_blkno,
6951 num_clusters, 0, meta_ac);
6955 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6956 (unsigned long long)new_blkno, num_clusters, reflink_cpos);
6958 len -= num_clusters;
6959 blkno += ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
6960 num_buckets -= reflink_buckets;
6967 * Create the same xattr extent record in the new inode's xattr tree.
6969 static int ocfs2_reflink_xattr_rec(struct inode *inode,
6970 struct buffer_head *root_bh,
6976 int ret, credits = 0;
6978 struct ocfs2_reflink_xattr_tree_args *args =
6979 (struct ocfs2_reflink_xattr_tree_args *)para;
6980 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6981 struct ocfs2_alloc_context *meta_ac = NULL;
6982 struct ocfs2_alloc_context *data_ac = NULL;
6983 struct ocfs2_extent_tree et;
6985 mlog(0, "reflink xattr buckets %llu len %u\n",
6986 (unsigned long long)blkno, len);
6988 ocfs2_init_xattr_tree_extent_tree(&et,
6989 INODE_CACHE(args->reflink->new_inode),
6992 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6994 &meta_ac, &data_ac);
7000 handle = ocfs2_start_trans(osb, credits);
7001 if (IS_ERR(handle)) {
7002 ret = PTR_ERR(handle);
7007 ret = ocfs2_reflink_xattr_buckets(handle, inode, args, &et,
7013 ocfs2_commit_trans(osb, handle);
7017 ocfs2_free_alloc_context(meta_ac);
7019 ocfs2_free_alloc_context(data_ac);
7024 * Create reflinked xattr buckets.
7025 * We will add bucket one by one, and refcount all the xattrs in the bucket
7026 * if they are stored outside.
7028 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
7029 struct buffer_head *blk_bh,
7030 struct buffer_head *new_blk_bh)
7033 struct ocfs2_reflink_xattr_tree_args para;
7035 memset(¶, 0, sizeof(para));
7036 para.reflink = args;
7037 para.old_blk_bh = blk_bh;
7038 para.new_blk_bh = new_blk_bh;
7040 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
7041 if (!para.old_bucket) {
7042 mlog_errno(-ENOMEM);
7046 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
7047 if (!para.new_bucket) {
7053 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
7054 ocfs2_reflink_xattr_rec,
7060 ocfs2_xattr_bucket_free(para.old_bucket);
7061 ocfs2_xattr_bucket_free(para.new_bucket);
7065 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7066 struct buffer_head *blk_bh)
7068 int ret, indexed = 0;
7069 struct buffer_head *new_blk_bh = NULL;
7070 struct ocfs2_xattr_block *xb =
7071 (struct ocfs2_xattr_block *)blk_bh->b_data;
7074 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7077 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7078 &new_blk_bh, indexed);
7084 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7085 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7087 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7096 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7098 int type = ocfs2_xattr_get_type(xe);
7100 return type != OCFS2_XATTR_INDEX_SECURITY &&
7101 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7102 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7105 int ocfs2_reflink_xattrs(struct inode *old_inode,
7106 struct buffer_head *old_bh,
7107 struct inode *new_inode,
7108 struct buffer_head *new_bh,
7109 bool preserve_security)
7112 struct ocfs2_xattr_reflink args;
7113 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7114 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7115 struct buffer_head *blk_bh = NULL;
7116 struct ocfs2_cached_dealloc_ctxt dealloc;
7117 struct ocfs2_refcount_tree *ref_tree;
7118 struct buffer_head *ref_root_bh = NULL;
7120 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7121 le64_to_cpu(di->i_refcount_loc),
7122 1, &ref_tree, &ref_root_bh);
7128 ocfs2_init_dealloc_ctxt(&dealloc);
7130 args.old_inode = old_inode;
7131 args.new_inode = new_inode;
7132 args.old_bh = old_bh;
7133 args.new_bh = new_bh;
7134 args.ref_ci = &ref_tree->rf_ci;
7135 args.ref_root_bh = ref_root_bh;
7136 args.dealloc = &dealloc;
7137 if (preserve_security)
7138 args.xattr_reflinked = NULL;
7140 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
7142 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7143 ret = ocfs2_reflink_xattr_inline(&args);
7150 if (!di->i_xattr_loc)
7153 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7160 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7167 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7169 brelse(ref_root_bh);
7171 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7172 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7173 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7181 * Initialize security and acl for a already created inode.
7182 * Used for reflink a non-preserve-security file.
7184 * It uses common api like ocfs2_xattr_set, so the caller
7185 * must not hold any lock expect i_mutex.
7187 int ocfs2_init_security_and_acl(struct inode *dir,
7188 struct inode *inode)
7191 struct buffer_head *dir_bh = NULL;
7192 struct ocfs2_security_xattr_info si = {
7196 ret = ocfs2_init_security_get(inode, dir, &si);
7198 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7199 si.name, si.value, si.value_len,
7205 } else if (ret != -EOPNOTSUPP) {
7210 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7216 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7220 ocfs2_inode_unlock(dir, 0);
7226 * 'security' attributes support
7228 static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
7229 size_t list_size, const char *name,
7230 size_t name_len, int type)
7232 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7233 const size_t total_len = prefix_len + name_len + 1;
7235 if (list && total_len <= list_size) {
7236 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7237 memcpy(list + prefix_len, name, name_len);
7238 list[prefix_len + name_len] = '\0';
7243 static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7244 void *buffer, size_t size, int type)
7246 if (strcmp(name, "") == 0)
7248 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7249 name, buffer, size);
7252 static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7253 const void *value, size_t size, int flags, int type)
7255 if (strcmp(name, "") == 0)
7258 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7259 name, value, size, flags);
7262 int ocfs2_init_security_get(struct inode *inode,
7264 struct ocfs2_security_xattr_info *si)
7266 /* check whether ocfs2 support feature xattr */
7267 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7269 return security_inode_init_security(inode, dir, &si->name, &si->value,
7273 int ocfs2_init_security_set(handle_t *handle,
7274 struct inode *inode,
7275 struct buffer_head *di_bh,
7276 struct ocfs2_security_xattr_info *si,
7277 struct ocfs2_alloc_context *xattr_ac,
7278 struct ocfs2_alloc_context *data_ac)
7280 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7281 OCFS2_XATTR_INDEX_SECURITY,
7282 si->name, si->value, si->value_len, 0,
7286 const struct xattr_handler ocfs2_xattr_security_handler = {
7287 .prefix = XATTR_SECURITY_PREFIX,
7288 .list = ocfs2_xattr_security_list,
7289 .get = ocfs2_xattr_security_get,
7290 .set = ocfs2_xattr_security_set,
7294 * 'trusted' attributes support
7296 static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
7297 size_t list_size, const char *name,
7298 size_t name_len, int type)
7300 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
7301 const size_t total_len = prefix_len + name_len + 1;
7303 if (list && total_len <= list_size) {
7304 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7305 memcpy(list + prefix_len, name, name_len);
7306 list[prefix_len + name_len] = '\0';
7311 static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7312 void *buffer, size_t size, int type)
7314 if (strcmp(name, "") == 0)
7316 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7317 name, buffer, size);
7320 static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7321 const void *value, size_t size, int flags, int type)
7323 if (strcmp(name, "") == 0)
7326 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7327 name, value, size, flags);
7330 const struct xattr_handler ocfs2_xattr_trusted_handler = {
7331 .prefix = XATTR_TRUSTED_PREFIX,
7332 .list = ocfs2_xattr_trusted_list,
7333 .get = ocfs2_xattr_trusted_get,
7334 .set = ocfs2_xattr_trusted_set,
7338 * 'user' attributes support
7340 static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
7341 size_t list_size, const char *name,
7342 size_t name_len, int type)
7344 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7345 const size_t total_len = prefix_len + name_len + 1;
7346 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7348 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7351 if (list && total_len <= list_size) {
7352 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7353 memcpy(list + prefix_len, name, name_len);
7354 list[prefix_len + name_len] = '\0';
7359 static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7360 void *buffer, size_t size, int type)
7362 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7364 if (strcmp(name, "") == 0)
7366 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7368 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
7372 static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7373 const void *value, size_t size, int flags, int type)
7375 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7377 if (strcmp(name, "") == 0)
7379 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7382 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7383 name, value, size, flags);
7386 const struct xattr_handler ocfs2_xattr_user_handler = {
7387 .prefix = XATTR_USER_PREFIX,
7388 .list = ocfs2_xattr_user_list,
7389 .get = ocfs2_xattr_user_get,
7390 .set = ocfs2_xattr_user_set,