1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
9 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/security.h>
40 #define MLOG_MASK_PREFIX ML_XATTR
41 #include <cluster/masklog.h>
45 #include "blockcheck.h"
55 #include "buffer_head_io.h"
58 #include "refcounttree.h"
61 struct ocfs2_xattr_def_value_root {
62 struct ocfs2_xattr_value_root xv;
63 struct ocfs2_extent_rec er;
66 struct ocfs2_xattr_bucket {
67 /* The inode these xattrs are associated with */
68 struct inode *bu_inode;
70 /* The actual buffers that make up the bucket */
71 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
73 /* How many blocks make up one bucket for this filesystem */
77 struct ocfs2_xattr_set_ctxt {
79 struct ocfs2_alloc_context *meta_ac;
80 struct ocfs2_alloc_context *data_ac;
81 struct ocfs2_cached_dealloc_ctxt dealloc;
84 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
85 #define OCFS2_XATTR_INLINE_SIZE 80
86 #define OCFS2_XATTR_HEADER_GAP 4
87 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
88 - sizeof(struct ocfs2_xattr_header) \
89 - OCFS2_XATTR_HEADER_GAP)
90 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
91 - sizeof(struct ocfs2_xattr_block) \
92 - sizeof(struct ocfs2_xattr_header) \
93 - OCFS2_XATTR_HEADER_GAP)
95 static struct ocfs2_xattr_def_value_root def_xv = {
96 .xv.xr_list.l_count = cpu_to_le16(1),
99 struct xattr_handler *ocfs2_xattr_handlers[] = {
100 &ocfs2_xattr_user_handler,
101 &ocfs2_xattr_acl_access_handler,
102 &ocfs2_xattr_acl_default_handler,
103 &ocfs2_xattr_trusted_handler,
104 &ocfs2_xattr_security_handler,
108 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
109 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
110 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
111 = &ocfs2_xattr_acl_access_handler,
112 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
113 = &ocfs2_xattr_acl_default_handler,
114 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
115 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
118 struct ocfs2_xattr_info {
122 const void *xi_value;
126 struct ocfs2_xattr_search {
127 struct buffer_head *inode_bh;
129 * xattr_bh point to the block buffer head which has extended attribute
130 * when extended attribute in inode, xattr_bh is equal to inode_bh.
132 struct buffer_head *xattr_bh;
133 struct ocfs2_xattr_header *header;
134 struct ocfs2_xattr_bucket *bucket;
137 struct ocfs2_xattr_entry *here;
141 /* Operations on struct ocfs2_xa_entry */
143 struct ocfs2_xa_loc_operations {
145 * Return a pointer to the appropriate buffer in loc->xl_storage
146 * at the given offset from loc->xl_header.
148 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
151 * Remove the name+value at this location. Do whatever is
152 * appropriate with the remaining name+value pairs.
154 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
158 * Describes an xattr entry location. This is a memory structure
159 * tracking the on-disk structure.
161 struct ocfs2_xa_loc {
162 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
163 struct ocfs2_xattr_header *xl_header;
165 /* Bytes from xl_header to the end of the storage */
169 * The ocfs2_xattr_entry this location describes. If this is
170 * NULL, this location describes the on-disk structure where it
173 struct ocfs2_xattr_entry *xl_entry;
176 * Internal housekeeping
179 /* Buffer(s) containing this entry */
182 /* Operations on the storage backing this location */
183 const struct ocfs2_xa_loc_operations *xl_ops;
187 * Convenience functions to calculate how much space is needed for a
188 * given name+value pair
190 static int namevalue_size(int name_len, uint64_t value_len)
192 if (value_len > OCFS2_XATTR_INLINE_SIZE)
193 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
195 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
198 static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
200 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
203 static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
205 u64 value_len = le64_to_cpu(xe->xe_value_size);
207 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
208 ocfs2_xattr_is_local(xe));
209 return namevalue_size(xe->xe_name_len, value_len);
213 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
214 struct ocfs2_xattr_header *xh,
219 static int ocfs2_xattr_block_find(struct inode *inode,
222 struct ocfs2_xattr_search *xs);
223 static int ocfs2_xattr_index_block_find(struct inode *inode,
224 struct buffer_head *root_bh,
227 struct ocfs2_xattr_search *xs);
229 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
230 struct buffer_head *blk_bh,
234 static int ocfs2_xattr_create_index_block(struct inode *inode,
235 struct ocfs2_xattr_search *xs,
236 struct ocfs2_xattr_set_ctxt *ctxt);
238 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
239 struct ocfs2_xattr_info *xi,
240 struct ocfs2_xattr_search *xs,
241 struct ocfs2_xattr_set_ctxt *ctxt);
243 typedef int (xattr_tree_rec_func)(struct inode *inode,
244 struct buffer_head *root_bh,
245 u64 blkno, u32 cpos, u32 len, void *para);
246 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
247 struct buffer_head *root_bh,
248 xattr_tree_rec_func *rec_func,
250 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
251 struct ocfs2_xattr_bucket *bucket,
253 static int ocfs2_rm_xattr_cluster(struct inode *inode,
254 struct buffer_head *root_bh,
260 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
261 u64 src_blk, u64 last_blk, u64 to_blk,
262 unsigned int start_bucket,
264 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
265 struct ocfs2_dinode *di,
266 struct ocfs2_xattr_info *xi,
267 struct ocfs2_xattr_search *xis,
268 struct ocfs2_xattr_search *xbs,
269 struct ocfs2_refcount_tree **ref_tree,
272 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
273 struct ocfs2_xattr_bucket *bucket,
275 struct ocfs2_xattr_value_root **xv,
276 struct buffer_head **bh);
278 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
280 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
283 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
285 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
288 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
290 u16 len = sb->s_blocksize -
291 offsetof(struct ocfs2_xattr_header, xh_entries);
293 return len / sizeof(struct ocfs2_xattr_entry);
296 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
297 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
298 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
300 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
302 struct ocfs2_xattr_bucket *bucket;
303 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
305 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
307 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
309 bucket->bu_inode = inode;
310 bucket->bu_blocks = blks;
316 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
320 for (i = 0; i < bucket->bu_blocks; i++) {
321 brelse(bucket->bu_bhs[i]);
322 bucket->bu_bhs[i] = NULL;
326 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
329 ocfs2_xattr_bucket_relse(bucket);
330 bucket->bu_inode = NULL;
336 * A bucket that has never been written to disk doesn't need to be
337 * read. We just need the buffer_heads. Don't call this for
338 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
341 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
346 for (i = 0; i < bucket->bu_blocks; i++) {
347 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
349 if (!bucket->bu_bhs[i]) {
355 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
357 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
362 ocfs2_xattr_bucket_relse(bucket);
366 /* Read the xattr bucket at xb_blkno */
367 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
372 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
373 bucket->bu_blocks, bucket->bu_bhs, 0,
376 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
377 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
380 &bucket_xh(bucket)->xh_check);
381 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
387 ocfs2_xattr_bucket_relse(bucket);
391 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
392 struct ocfs2_xattr_bucket *bucket,
397 for (i = 0; i < bucket->bu_blocks; i++) {
398 rc = ocfs2_journal_access(handle,
399 INODE_CACHE(bucket->bu_inode),
400 bucket->bu_bhs[i], type);
410 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
411 struct ocfs2_xattr_bucket *bucket)
415 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
416 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
417 bucket->bu_bhs, bucket->bu_blocks,
418 &bucket_xh(bucket)->xh_check);
419 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
421 for (i = 0; i < bucket->bu_blocks; i++)
422 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
425 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
426 struct ocfs2_xattr_bucket *src)
429 int blocksize = src->bu_inode->i_sb->s_blocksize;
431 BUG_ON(dest->bu_blocks != src->bu_blocks);
432 BUG_ON(dest->bu_inode != src->bu_inode);
434 for (i = 0; i < src->bu_blocks; i++) {
435 memcpy(bucket_block(dest, i), bucket_block(src, i),
440 static int ocfs2_validate_xattr_block(struct super_block *sb,
441 struct buffer_head *bh)
444 struct ocfs2_xattr_block *xb =
445 (struct ocfs2_xattr_block *)bh->b_data;
447 mlog(0, "Validating xattr block %llu\n",
448 (unsigned long long)bh->b_blocknr);
450 BUG_ON(!buffer_uptodate(bh));
453 * If the ecc fails, we return the error but otherwise
454 * leave the filesystem running. We know any error is
455 * local to this block.
457 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
462 * Errors after here are fatal
465 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
467 "Extended attribute block #%llu has bad "
469 (unsigned long long)bh->b_blocknr, 7,
474 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
476 "Extended attribute block #%llu has an "
477 "invalid xb_blkno of %llu",
478 (unsigned long long)bh->b_blocknr,
479 (unsigned long long)le64_to_cpu(xb->xb_blkno));
483 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
485 "Extended attribute block #%llu has an invalid "
486 "xb_fs_generation of #%u",
487 (unsigned long long)bh->b_blocknr,
488 le32_to_cpu(xb->xb_fs_generation));
495 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
496 struct buffer_head **bh)
499 struct buffer_head *tmp = *bh;
501 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
502 ocfs2_validate_xattr_block);
504 /* If ocfs2_read_block() got us a new bh, pass it up. */
511 static inline const char *ocfs2_xattr_prefix(int name_index)
513 struct xattr_handler *handler = NULL;
515 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
516 handler = ocfs2_xattr_handler_map[name_index];
518 return handler ? handler->prefix : NULL;
521 static u32 ocfs2_xattr_name_hash(struct inode *inode,
525 /* Get hash value of uuid from super block */
526 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
529 /* hash extended attribute name */
530 for (i = 0; i < name_len; i++) {
531 hash = (hash << OCFS2_HASH_SHIFT) ^
532 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
540 * ocfs2_xattr_hash_entry()
542 * Compute the hash of an extended attribute.
544 static void ocfs2_xattr_hash_entry(struct inode *inode,
545 struct ocfs2_xattr_header *header,
546 struct ocfs2_xattr_entry *entry)
549 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
551 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
552 entry->xe_name_hash = cpu_to_le32(hash);
557 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
559 return namevalue_size(name_len, value_len) +
560 sizeof(struct ocfs2_xattr_entry);
563 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
565 return namevalue_size_xi(xi) +
566 sizeof(struct ocfs2_xattr_entry);
569 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
571 return namevalue_size_xe(xe) +
572 sizeof(struct ocfs2_xattr_entry);
575 int ocfs2_calc_security_init(struct inode *dir,
576 struct ocfs2_security_xattr_info *si,
579 struct ocfs2_alloc_context **xattr_ac)
582 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
583 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
587 * The max space of security xattr taken inline is
588 * 256(name) + 80(value) + 16(entry) = 352 bytes,
589 * So reserve one metadata block for it is ok.
591 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
592 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
593 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
598 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
601 /* reserve clusters for xattr value which will be set in B tree*/
602 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
603 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
606 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
608 *want_clusters += new_clusters;
613 int ocfs2_calc_xattr_init(struct inode *dir,
614 struct buffer_head *dir_bh,
616 struct ocfs2_security_xattr_info *si,
622 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
623 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
626 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
629 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
630 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
631 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
634 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
637 } else if (acl_len != 0 && acl_len != -ENODATA) {
643 if (!(s_size + a_size))
647 * The max space of security xattr taken inline is
648 * 256(name) + 80(value) + 16(entry) = 352 bytes,
649 * The max space of acl xattr taken inline is
650 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
651 * when blocksize = 512, may reserve one more cluser for
652 * xattr bucket, otherwise reserve one metadata block
654 * If this is a new directory with inline data,
655 * we choose to reserve the entire inline area for
656 * directory contents and force an external xattr block.
658 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
659 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
660 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
661 *want_meta = *want_meta + 1;
662 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
665 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
666 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
668 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
672 * reserve credits and clusters for xattrs which has large value
673 * and have to be set outside
675 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
676 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
678 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
680 *want_clusters += new_clusters;
682 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
683 acl_len > OCFS2_XATTR_INLINE_SIZE) {
684 /* for directory, it has DEFAULT and ACCESS two types of acls */
685 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
686 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
687 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
689 *want_clusters += new_clusters;
695 static int ocfs2_xattr_extend_allocation(struct inode *inode,
697 struct ocfs2_xattr_value_buf *vb,
698 struct ocfs2_xattr_set_ctxt *ctxt)
701 handle_t *handle = ctxt->handle;
702 enum ocfs2_alloc_restarted why;
703 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
704 struct ocfs2_extent_tree et;
706 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
708 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
710 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
711 OCFS2_JOURNAL_ACCESS_WRITE);
717 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
718 status = ocfs2_add_clusters_in_btree(handle,
731 status = ocfs2_journal_dirty(handle, vb->vb_bh);
737 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
740 * We should have already allocated enough space before the transaction,
741 * so no need to restart.
743 BUG_ON(why != RESTART_NONE || clusters_to_add);
750 static int __ocfs2_remove_xattr_range(struct inode *inode,
751 struct ocfs2_xattr_value_buf *vb,
752 u32 cpos, u32 phys_cpos, u32 len,
753 unsigned int ext_flags,
754 struct ocfs2_xattr_set_ctxt *ctxt)
757 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
758 handle_t *handle = ctxt->handle;
759 struct ocfs2_extent_tree et;
761 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
763 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
764 OCFS2_JOURNAL_ACCESS_WRITE);
770 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
777 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
779 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
785 if (ext_flags & OCFS2_EXT_REFCOUNTED)
786 ret = ocfs2_decrease_refcount(inode, handle,
787 ocfs2_blocks_to_clusters(inode->i_sb,
789 len, ctxt->meta_ac, &ctxt->dealloc, 1);
791 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
800 static int ocfs2_xattr_shrink_size(struct inode *inode,
803 struct ocfs2_xattr_value_buf *vb,
804 struct ocfs2_xattr_set_ctxt *ctxt)
807 unsigned int ext_flags;
808 u32 trunc_len, cpos, phys_cpos, alloc_size;
811 if (old_clusters <= new_clusters)
815 trunc_len = old_clusters - new_clusters;
817 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
819 &vb->vb_xv->xr_list, &ext_flags);
825 if (alloc_size > trunc_len)
826 alloc_size = trunc_len;
828 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
829 phys_cpos, alloc_size,
836 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
837 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
840 trunc_len -= alloc_size;
847 static int ocfs2_xattr_value_truncate(struct inode *inode,
848 struct ocfs2_xattr_value_buf *vb,
850 struct ocfs2_xattr_set_ctxt *ctxt)
853 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
854 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
856 if (new_clusters == old_clusters)
859 if (new_clusters > old_clusters)
860 ret = ocfs2_xattr_extend_allocation(inode,
861 new_clusters - old_clusters,
864 ret = ocfs2_xattr_shrink_size(inode,
865 old_clusters, new_clusters,
871 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
872 size_t *result, const char *prefix,
873 const char *name, int name_len)
875 char *p = buffer + *result;
876 int prefix_len = strlen(prefix);
877 int total_len = prefix_len + name_len + 1;
879 *result += total_len;
881 /* we are just looking for how big our buffer needs to be */
888 memcpy(p, prefix, prefix_len);
889 memcpy(p + prefix_len, name, name_len);
890 p[prefix_len + name_len] = '\0';
895 static int ocfs2_xattr_list_entries(struct inode *inode,
896 struct ocfs2_xattr_header *header,
897 char *buffer, size_t buffer_size)
901 const char *prefix, *name;
903 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
904 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
905 type = ocfs2_xattr_get_type(entry);
906 prefix = ocfs2_xattr_prefix(type);
909 name = (const char *)header +
910 le16_to_cpu(entry->xe_name_offset);
912 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
913 &result, prefix, name,
923 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
924 struct ocfs2_dinode *di)
926 struct ocfs2_xattr_header *xh;
929 xh = (struct ocfs2_xattr_header *)
930 ((void *)di + inode->i_sb->s_blocksize -
931 le16_to_cpu(di->i_xattr_inline_size));
933 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
934 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
940 static int ocfs2_xattr_ibody_list(struct inode *inode,
941 struct ocfs2_dinode *di,
945 struct ocfs2_xattr_header *header = NULL;
946 struct ocfs2_inode_info *oi = OCFS2_I(inode);
949 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
952 header = (struct ocfs2_xattr_header *)
953 ((void *)di + inode->i_sb->s_blocksize -
954 le16_to_cpu(di->i_xattr_inline_size));
956 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
961 static int ocfs2_xattr_block_list(struct inode *inode,
962 struct ocfs2_dinode *di,
966 struct buffer_head *blk_bh = NULL;
967 struct ocfs2_xattr_block *xb;
970 if (!di->i_xattr_loc)
973 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
980 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
981 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
982 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
983 ret = ocfs2_xattr_list_entries(inode, header,
984 buffer, buffer_size);
986 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
987 buffer, buffer_size);
994 ssize_t ocfs2_listxattr(struct dentry *dentry,
998 int ret = 0, i_ret = 0, b_ret = 0;
999 struct buffer_head *di_bh = NULL;
1000 struct ocfs2_dinode *di = NULL;
1001 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1003 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1006 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1009 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1015 di = (struct ocfs2_dinode *)di_bh->b_data;
1017 down_read(&oi->ip_xattr_sem);
1018 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1026 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1031 up_read(&oi->ip_xattr_sem);
1032 ocfs2_inode_unlock(dentry->d_inode, 0);
1036 return i_ret + b_ret;
1039 static int ocfs2_xattr_find_entry(int name_index,
1041 struct ocfs2_xattr_search *xs)
1043 struct ocfs2_xattr_entry *entry;
1050 name_len = strlen(name);
1052 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1053 cmp = name_index - ocfs2_xattr_get_type(entry);
1055 cmp = name_len - entry->xe_name_len;
1057 cmp = memcmp(name, (xs->base +
1058 le16_to_cpu(entry->xe_name_offset)),
1066 return cmp ? -ENODATA : 0;
1069 static int ocfs2_xattr_get_value_outside(struct inode *inode,
1070 struct ocfs2_xattr_value_root *xv,
1074 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1077 size_t cplen, blocksize;
1078 struct buffer_head *bh = NULL;
1079 struct ocfs2_extent_list *el;
1082 clusters = le32_to_cpu(xv->xr_clusters);
1083 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1084 blocksize = inode->i_sb->s_blocksize;
1087 while (cpos < clusters) {
1088 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1089 &num_clusters, el, NULL);
1095 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1096 /* Copy ocfs2_xattr_value */
1097 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1098 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1105 cplen = len >= blocksize ? blocksize : len;
1106 memcpy(buffer, bh->b_data, cplen);
1115 cpos += num_clusters;
1121 static int ocfs2_xattr_ibody_get(struct inode *inode,
1126 struct ocfs2_xattr_search *xs)
1128 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1129 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1130 struct ocfs2_xattr_value_root *xv;
1134 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1137 xs->end = (void *)di + inode->i_sb->s_blocksize;
1138 xs->header = (struct ocfs2_xattr_header *)
1139 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1140 xs->base = (void *)xs->header;
1141 xs->here = xs->header->xh_entries;
1143 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1146 size = le64_to_cpu(xs->here->xe_value_size);
1148 if (size > buffer_size)
1150 if (ocfs2_xattr_is_local(xs->here)) {
1151 memcpy(buffer, (void *)xs->base +
1152 le16_to_cpu(xs->here->xe_name_offset) +
1153 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1155 xv = (struct ocfs2_xattr_value_root *)
1156 (xs->base + le16_to_cpu(
1157 xs->here->xe_name_offset) +
1158 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1159 ret = ocfs2_xattr_get_value_outside(inode, xv,
1171 static int ocfs2_xattr_block_get(struct inode *inode,
1176 struct ocfs2_xattr_search *xs)
1178 struct ocfs2_xattr_block *xb;
1179 struct ocfs2_xattr_value_root *xv;
1181 int ret = -ENODATA, name_offset, name_len, i;
1182 int uninitialized_var(block_off);
1184 xs->bucket = ocfs2_xattr_bucket_new(inode);
1191 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1197 if (xs->not_found) {
1202 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1203 size = le64_to_cpu(xs->here->xe_value_size);
1206 if (size > buffer_size)
1209 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1210 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1211 i = xs->here - xs->header->xh_entries;
1213 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1214 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1215 bucket_xh(xs->bucket),
1219 xs->base = bucket_block(xs->bucket, block_off);
1221 if (ocfs2_xattr_is_local(xs->here)) {
1222 memcpy(buffer, (void *)xs->base +
1223 name_offset + name_len, size);
1225 xv = (struct ocfs2_xattr_value_root *)
1226 (xs->base + name_offset + name_len);
1227 ret = ocfs2_xattr_get_value_outside(inode, xv,
1237 ocfs2_xattr_bucket_free(xs->bucket);
1239 brelse(xs->xattr_bh);
1240 xs->xattr_bh = NULL;
1244 int ocfs2_xattr_get_nolock(struct inode *inode,
1245 struct buffer_head *di_bh,
1252 struct ocfs2_dinode *di = NULL;
1253 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1254 struct ocfs2_xattr_search xis = {
1255 .not_found = -ENODATA,
1257 struct ocfs2_xattr_search xbs = {
1258 .not_found = -ENODATA,
1261 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1264 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1267 xis.inode_bh = xbs.inode_bh = di_bh;
1268 di = (struct ocfs2_dinode *)di_bh->b_data;
1270 down_read(&oi->ip_xattr_sem);
1271 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1273 if (ret == -ENODATA && di->i_xattr_loc)
1274 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1276 up_read(&oi->ip_xattr_sem);
1281 /* ocfs2_xattr_get()
1283 * Copy an extended attribute into the buffer provided.
1284 * Buffer is NULL to compute the size of buffer required.
1286 static int ocfs2_xattr_get(struct inode *inode,
1293 struct buffer_head *di_bh = NULL;
1295 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1300 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1301 name, buffer, buffer_size);
1303 ocfs2_inode_unlock(inode, 0);
1310 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1312 struct ocfs2_xattr_value_buf *vb,
1316 int ret = 0, i, cp_len;
1317 u16 blocksize = inode->i_sb->s_blocksize;
1318 u32 p_cluster, num_clusters;
1319 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1320 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1322 struct buffer_head *bh = NULL;
1323 unsigned int ext_flags;
1324 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1326 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1328 while (cpos < clusters) {
1329 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1330 &num_clusters, &xv->xr_list,
1337 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1339 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1341 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1342 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1349 ret = ocfs2_journal_access(handle,
1352 OCFS2_JOURNAL_ACCESS_WRITE);
1358 cp_len = value_len > blocksize ? blocksize : value_len;
1359 memcpy(bh->b_data, value, cp_len);
1360 value_len -= cp_len;
1362 if (cp_len < blocksize)
1363 memset(bh->b_data + cp_len, 0,
1364 blocksize - cp_len);
1366 ret = ocfs2_journal_dirty(handle, bh);
1375 * XXX: do we need to empty all the following
1376 * blocks in this cluster?
1381 cpos += num_clusters;
1389 static int ocfs2_xattr_cleanup(struct inode *inode,
1391 struct ocfs2_xattr_info *xi,
1392 struct ocfs2_xattr_search *xs,
1393 struct ocfs2_xattr_value_buf *vb,
1397 void *val = xs->base + offs;
1398 size_t size = namevalue_size_xi(xi);
1400 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1401 OCFS2_JOURNAL_ACCESS_WRITE);
1406 /* Decrease xattr count */
1407 le16_add_cpu(&xs->header->xh_count, -1);
1408 /* Remove the xattr entry and tree root which has already be set*/
1409 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1410 memset(val, 0, size);
1412 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1419 static int ocfs2_xattr_update_entry(struct inode *inode,
1421 struct ocfs2_xattr_info *xi,
1422 struct ocfs2_xattr_search *xs,
1423 struct ocfs2_xattr_value_buf *vb,
1428 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1429 OCFS2_JOURNAL_ACCESS_WRITE);
1435 xs->here->xe_name_offset = cpu_to_le16(offs);
1436 xs->here->xe_value_size = cpu_to_le64(xi->xi_value_len);
1437 if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
1438 ocfs2_xattr_set_local(xs->here, 1);
1440 ocfs2_xattr_set_local(xs->here, 0);
1441 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1443 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1451 * ocfs2_xattr_set_value_outside()
1453 * Set large size value in B tree.
1455 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1456 struct ocfs2_xattr_info *xi,
1457 struct ocfs2_xattr_search *xs,
1458 struct ocfs2_xattr_set_ctxt *ctxt,
1459 struct ocfs2_xattr_value_buf *vb,
1462 void *val = xs->base + offs;
1463 struct ocfs2_xattr_value_root *xv = NULL;
1464 size_t size = namevalue_size_xi(xi);
1467 memset(val, 0, size);
1468 memcpy(val, xi->xi_name, xi->xi_name_len);
1469 xv = (struct ocfs2_xattr_value_root *)
1470 (val + OCFS2_XATTR_SIZE(xi->xi_name_len));
1471 xv->xr_clusters = 0;
1472 xv->xr_last_eb_blk = 0;
1473 xv->xr_list.l_tree_depth = 0;
1474 xv->xr_list.l_count = cpu_to_le16(1);
1475 xv->xr_list.l_next_free_rec = 0;
1478 ret = ocfs2_xattr_value_truncate(inode, vb, xi->xi_value_len, ctxt);
1483 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
1488 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
1489 xi->xi_value, xi->xi_value_len);
1497 * Wipe the name+value pair and allow the storage to reclaim it. This
1498 * must be followed by either removal of the entry or a call to
1499 * ocfs2_xa_add_namevalue().
1501 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1503 loc->xl_ops->xlo_wipe_namevalue(loc);
1506 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1509 BUG_ON(offset >= loc->xl_size);
1510 return (char *)loc->xl_header + offset;
1514 * Block storage for xattrs keeps the name+value pairs compacted. When
1515 * we remove one, we have to shift any that preceded it towards the end.
1517 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1520 int namevalue_offset, first_namevalue_offset, namevalue_size;
1521 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1522 struct ocfs2_xattr_header *xh = loc->xl_header;
1523 int count = le16_to_cpu(xh->xh_count);
1525 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1526 namevalue_size = namevalue_size_xe(entry);
1528 for (i = 0, first_namevalue_offset = loc->xl_size;
1530 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1531 if (offset < first_namevalue_offset)
1532 first_namevalue_offset = offset;
1535 /* Shift the name+value pairs */
1536 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1537 (char *)xh + first_namevalue_offset,
1538 namevalue_offset - first_namevalue_offset);
1539 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1541 /* Now tell xh->xh_entries about it */
1542 for (i = 0; i < count; i++) {
1543 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1544 if (offset < namevalue_offset)
1545 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1550 * Note that we don't update xh_free_start or xh_name_value_len
1551 * because they're not used in block-stored xattrs.
1556 * Operations for xattrs stored in blocks. This includes inline inode
1557 * storage and unindexed ocfs2_xattr_blocks.
1559 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1560 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
1561 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
1564 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1567 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1568 int block, block_offset;
1570 BUG_ON(offset >= OCFS2_XATTR_BUCKET_SIZE);
1572 /* The header is at the front of the bucket */
1573 block = offset >> bucket->bu_inode->i_sb->s_blocksize_bits;
1574 block_offset = offset % bucket->bu_inode->i_sb->s_blocksize;
1576 return bucket_block(bucket, block) + block_offset;
1579 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1581 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1582 -namevalue_size_xe(loc->xl_entry));
1585 /* Operations for xattrs stored in buckets. */
1586 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1587 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
1588 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
1591 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1594 struct ocfs2_xattr_header *xh = loc->xl_header;
1595 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1597 ocfs2_xa_wipe_namevalue(loc);
1598 loc->xl_entry = NULL;
1600 le16_add_cpu(&xh->xh_count, -1);
1601 count = le16_to_cpu(xh->xh_count);
1604 * Only zero out the entry if there are more remaining. This is
1605 * important for an empty bucket, as it keeps track of the
1606 * bucket's hash value. It doesn't hurt empty block storage.
1609 index = ((char *)entry - (char *)&xh->xh_entries) /
1610 sizeof(struct ocfs2_xattr_entry);
1611 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1612 (count - index) * sizeof(struct ocfs2_xattr_entry));
1613 memset(&xh->xh_entries[count], 0,
1614 sizeof(struct ocfs2_xattr_entry));
1618 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
1619 struct inode *inode,
1620 struct buffer_head *bh,
1621 struct ocfs2_xattr_entry *entry)
1623 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1625 loc->xl_ops = &ocfs2_xa_block_loc_ops;
1626 loc->xl_storage = bh;
1627 loc->xl_entry = entry;
1629 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1630 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
1633 loc->xl_size = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1636 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
1640 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
1641 struct buffer_head *bh,
1642 struct ocfs2_xattr_entry *entry)
1644 struct ocfs2_xattr_block *xb =
1645 (struct ocfs2_xattr_block *)bh->b_data;
1647 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
1649 loc->xl_ops = &ocfs2_xa_block_loc_ops;
1650 loc->xl_storage = bh;
1651 loc->xl_header = &(xb->xb_attrs.xb_header);
1652 loc->xl_entry = entry;
1653 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
1654 xb_attrs.xb_header);
1657 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
1658 struct ocfs2_xattr_bucket *bucket,
1659 struct ocfs2_xattr_entry *entry)
1661 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
1662 loc->xl_storage = bucket;
1663 loc->xl_header = bucket_xh(bucket);
1664 loc->xl_entry = entry;
1665 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
1669 * ocfs2_xattr_set_entry_local()
1671 * Set, replace or remove extended attribute in local.
1673 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1674 struct ocfs2_xattr_info *xi,
1675 struct ocfs2_xattr_search *xs,
1676 struct ocfs2_xattr_entry *last,
1679 struct ocfs2_xa_loc loc;
1681 if (xs->xattr_bh == xs->inode_bh)
1682 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
1683 xs->not_found ? NULL : xs->here);
1685 ocfs2_init_xattr_block_xa_loc(&loc, xs->xattr_bh,
1686 xs->not_found ? NULL : xs->here);
1687 if (xi->xi_value && xs->not_found) {
1688 /* Insert the new xattr entry. */
1689 le16_add_cpu(&xs->header->xh_count, 1);
1690 ocfs2_xattr_set_type(last, xi->xi_name_index);
1691 ocfs2_xattr_set_local(last, 1);
1692 last->xe_name_len = xi->xi_name_len;
1698 first_val = xs->base + min_offs;
1699 offs = le16_to_cpu(xs->here->xe_name_offset);
1700 val = xs->base + offs;
1702 size = namevalue_size_xe(xs->here);
1703 if (xi->xi_value && (size == namevalue_size_xi(xi))) {
1704 /* The old and the new value have the
1705 same size. Just replace the value. */
1706 ocfs2_xattr_set_local(xs->here, 1);
1707 xs->here->xe_value_size = cpu_to_le64(xi->xi_value_len);
1708 /* Clear value bytes. */
1709 memset(val + OCFS2_XATTR_SIZE(xi->xi_name_len),
1711 OCFS2_XATTR_SIZE(xi->xi_value_len));
1712 memcpy(val + OCFS2_XATTR_SIZE(xi->xi_name_len),
1719 ocfs2_xa_remove_entry(&loc);
1721 ocfs2_xa_wipe_namevalue(&loc);
1726 /* Insert the new name+value. */
1727 size_t size = namevalue_size_xi(xi);
1728 void *val = xs->base + min_offs - size;
1730 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1731 memset(val, 0, size);
1732 memcpy(val, xi->xi_name, xi->xi_name_len);
1733 memcpy(val + OCFS2_XATTR_SIZE(xi->xi_name_len),
1736 xs->here->xe_value_size = cpu_to_le64(xi->xi_value_len);
1737 ocfs2_xattr_set_local(xs->here, 1);
1738 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1745 * ocfs2_xattr_set_entry()
1747 * Set extended attribute entry into inode or block.
1749 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1750 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1751 * then set value in B tree with set_value_outside().
1753 static int ocfs2_xattr_set_entry(struct inode *inode,
1754 struct ocfs2_xattr_info *xi,
1755 struct ocfs2_xattr_search *xs,
1756 struct ocfs2_xattr_set_ctxt *ctxt,
1759 struct ocfs2_xattr_entry *last;
1760 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1761 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1762 size_t min_offs = xs->end - xs->base;
1764 handle_t *handle = ctxt->handle;
1766 struct ocfs2_xattr_info xi_l = {
1767 .xi_name_index = xi->xi_name_index,
1768 .xi_name = xi->xi_name,
1769 .xi_name_len = xi->xi_name_len,
1770 .xi_value = xi->xi_value,
1771 .xi_value_len = xi->xi_value_len,
1773 struct ocfs2_xattr_value_buf vb = {
1774 .vb_bh = xs->xattr_bh,
1775 .vb_access = ocfs2_journal_access_di,
1778 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1779 BUG_ON(xs->xattr_bh == xs->inode_bh);
1780 vb.vb_access = ocfs2_journal_access_xb;
1782 BUG_ON(xs->xattr_bh != xs->inode_bh);
1784 /* Compute min_offs, last and free space. */
1785 last = xs->header->xh_entries;
1787 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1788 size_t offs = le16_to_cpu(last->xe_name_offset);
1789 if (offs < min_offs)
1794 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
1799 free += ocfs2_xe_entry_usage(xs->here);
1801 /* Check free space in inode or block */
1803 if (free < ocfs2_xi_entry_usage(xi)) {
1807 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
1808 size_l = namevalue_size_xi(xi);
1809 xi_l.xi_value = (void *)&def_xv;
1810 xi_l.xi_value_len = OCFS2_XATTR_ROOT_SIZE;
1814 if (!xs->not_found) {
1815 /* For existing extended attribute */
1816 size_t size = namevalue_size_xe(xs->here);
1817 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1818 void *val = xs->base + offs;
1820 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1821 /* Replace existing local xattr with tree root */
1822 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1827 } else if (!ocfs2_xattr_is_local(xs->here)) {
1828 /* For existing xattr which has value outside */
1829 vb.vb_xv = (struct ocfs2_xattr_value_root *)
1830 (val + OCFS2_XATTR_SIZE(xi->xi_name_len));
1832 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
1834 * If new value need set outside also,
1835 * first truncate old value to new value,
1836 * then set new value with set_value_outside().
1838 ret = ocfs2_xattr_value_truncate(inode,
1847 ret = ocfs2_xattr_update_entry(inode,
1858 ret = __ocfs2_xattr_set_value_outside(inode,
1868 * If new value need set in local,
1869 * just trucate old value to zero.
1871 ret = ocfs2_xattr_value_truncate(inode,
1881 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
1882 OCFS2_JOURNAL_ACCESS_WRITE);
1888 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1889 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
1890 OCFS2_JOURNAL_ACCESS_WRITE);
1898 * Set value in local, include set tree root in local.
1899 * This is the first step for value size >INLINE_SIZE.
1901 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1903 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1904 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1911 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1912 (flag & OCFS2_INLINE_XATTR_FL)) {
1913 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1914 unsigned int xattrsize = osb->s_xattr_inline_size;
1917 * Adjust extent record count or inline data size
1918 * to reserve space for extended attribute.
1920 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1921 struct ocfs2_inline_data *idata = &di->id2.i_data;
1922 le16_add_cpu(&idata->id_count, -xattrsize);
1923 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1924 struct ocfs2_extent_list *el = &di->id2.i_list;
1925 le16_add_cpu(&el->l_count, -(xattrsize /
1926 sizeof(struct ocfs2_extent_rec)));
1928 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1930 /* Update xattr flag */
1931 spin_lock(&oi->ip_lock);
1932 oi->ip_dyn_features |= flag;
1933 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1934 spin_unlock(&oi->ip_lock);
1936 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1940 if (!ret && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
1942 * Set value outside in B tree.
1943 * This is the second step for value size > INLINE_SIZE.
1945 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1946 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
1953 * If set value outside failed, we have to clean
1954 * the junk tree root we have already set in local.
1956 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1967 * In xattr remove, if it is stored outside and refcounted, we may have
1968 * the chance to split the refcount tree. So need the allocators.
1970 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
1971 struct ocfs2_xattr_value_root *xv,
1972 struct ocfs2_caching_info *ref_ci,
1973 struct buffer_head *ref_root_bh,
1974 struct ocfs2_alloc_context **meta_ac,
1977 int ret, meta_add = 0;
1978 u32 p_cluster, num_clusters;
1979 unsigned int ext_flags;
1982 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
1991 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
1994 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
1996 &meta_add, ref_credits);
2002 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2011 static int ocfs2_remove_value_outside(struct inode*inode,
2012 struct ocfs2_xattr_value_buf *vb,
2013 struct ocfs2_xattr_header *header,
2014 struct ocfs2_caching_info *ref_ci,
2015 struct buffer_head *ref_root_bh)
2017 int ret = 0, i, ref_credits;
2018 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2019 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2022 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2024 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2025 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2027 if (ocfs2_xattr_is_local(entry))
2030 val = (void *)header +
2031 le16_to_cpu(entry->xe_name_offset);
2032 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2033 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2035 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2036 ref_ci, ref_root_bh,
2040 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2041 ocfs2_remove_extent_credits(osb->sb));
2042 if (IS_ERR(ctxt.handle)) {
2043 ret = PTR_ERR(ctxt.handle);
2048 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2054 ocfs2_commit_trans(osb, ctxt.handle);
2056 ocfs2_free_alloc_context(ctxt.meta_ac);
2057 ctxt.meta_ac = NULL;
2062 ocfs2_free_alloc_context(ctxt.meta_ac);
2063 ocfs2_schedule_truncate_log_flush(osb, 1);
2064 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2068 static int ocfs2_xattr_ibody_remove(struct inode *inode,
2069 struct buffer_head *di_bh,
2070 struct ocfs2_caching_info *ref_ci,
2071 struct buffer_head *ref_root_bh)
2074 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2075 struct ocfs2_xattr_header *header;
2077 struct ocfs2_xattr_value_buf vb = {
2079 .vb_access = ocfs2_journal_access_di,
2082 header = (struct ocfs2_xattr_header *)
2083 ((void *)di + inode->i_sb->s_blocksize -
2084 le16_to_cpu(di->i_xattr_inline_size));
2086 ret = ocfs2_remove_value_outside(inode, &vb, header,
2087 ref_ci, ref_root_bh);
2092 struct ocfs2_rm_xattr_bucket_para {
2093 struct ocfs2_caching_info *ref_ci;
2094 struct buffer_head *ref_root_bh;
2097 static int ocfs2_xattr_block_remove(struct inode *inode,
2098 struct buffer_head *blk_bh,
2099 struct ocfs2_caching_info *ref_ci,
2100 struct buffer_head *ref_root_bh)
2102 struct ocfs2_xattr_block *xb;
2104 struct ocfs2_xattr_value_buf vb = {
2106 .vb_access = ocfs2_journal_access_xb,
2108 struct ocfs2_rm_xattr_bucket_para args = {
2110 .ref_root_bh = ref_root_bh,
2113 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2114 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2115 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2116 ret = ocfs2_remove_value_outside(inode, &vb, header,
2117 ref_ci, ref_root_bh);
2119 ret = ocfs2_iterate_xattr_index_block(inode,
2121 ocfs2_rm_xattr_cluster,
2127 static int ocfs2_xattr_free_block(struct inode *inode,
2129 struct ocfs2_caching_info *ref_ci,
2130 struct buffer_head *ref_root_bh)
2132 struct inode *xb_alloc_inode;
2133 struct buffer_head *xb_alloc_bh = NULL;
2134 struct buffer_head *blk_bh = NULL;
2135 struct ocfs2_xattr_block *xb;
2136 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2142 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2148 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2154 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2155 blk = le64_to_cpu(xb->xb_blkno);
2156 bit = le16_to_cpu(xb->xb_suballoc_bit);
2157 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2159 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2160 EXTENT_ALLOC_SYSTEM_INODE,
2161 le16_to_cpu(xb->xb_suballoc_slot));
2162 if (!xb_alloc_inode) {
2167 mutex_lock(&xb_alloc_inode->i_mutex);
2169 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2175 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2176 if (IS_ERR(handle)) {
2177 ret = PTR_ERR(handle);
2182 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2187 ocfs2_commit_trans(osb, handle);
2189 ocfs2_inode_unlock(xb_alloc_inode, 1);
2190 brelse(xb_alloc_bh);
2192 mutex_unlock(&xb_alloc_inode->i_mutex);
2193 iput(xb_alloc_inode);
2200 * ocfs2_xattr_remove()
2202 * Free extended attribute resources associated with this inode.
2204 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2206 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2207 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2208 struct ocfs2_refcount_tree *ref_tree = NULL;
2209 struct buffer_head *ref_root_bh = NULL;
2210 struct ocfs2_caching_info *ref_ci = NULL;
2214 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2217 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2220 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2221 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2222 le64_to_cpu(di->i_refcount_loc),
2223 1, &ref_tree, &ref_root_bh);
2228 ref_ci = &ref_tree->rf_ci;
2232 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2233 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2234 ref_ci, ref_root_bh);
2241 if (di->i_xattr_loc) {
2242 ret = ocfs2_xattr_free_block(inode,
2243 le64_to_cpu(di->i_xattr_loc),
2244 ref_ci, ref_root_bh);
2251 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2252 OCFS2_INODE_UPDATE_CREDITS);
2253 if (IS_ERR(handle)) {
2254 ret = PTR_ERR(handle);
2258 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2259 OCFS2_JOURNAL_ACCESS_WRITE);
2265 di->i_xattr_loc = 0;
2267 spin_lock(&oi->ip_lock);
2268 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2269 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2270 spin_unlock(&oi->ip_lock);
2272 ret = ocfs2_journal_dirty(handle, di_bh);
2276 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2279 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2280 brelse(ref_root_bh);
2284 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2285 struct ocfs2_dinode *di)
2287 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2288 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2291 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2294 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2295 struct ocfs2_inline_data *idata = &di->id2.i_data;
2296 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2297 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2298 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2299 le64_to_cpu(di->i_size);
2301 struct ocfs2_extent_list *el = &di->id2.i_list;
2302 free = (le16_to_cpu(el->l_count) -
2303 le16_to_cpu(el->l_next_free_rec)) *
2304 sizeof(struct ocfs2_extent_rec);
2306 if (free >= xattrsize)
2313 * ocfs2_xattr_ibody_find()
2315 * Find extended attribute in inode block and
2316 * fill search info into struct ocfs2_xattr_search.
2318 static int ocfs2_xattr_ibody_find(struct inode *inode,
2321 struct ocfs2_xattr_search *xs)
2323 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2324 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2328 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2331 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2332 down_read(&oi->ip_alloc_sem);
2333 has_space = ocfs2_xattr_has_space_inline(inode, di);
2334 up_read(&oi->ip_alloc_sem);
2339 xs->xattr_bh = xs->inode_bh;
2340 xs->end = (void *)di + inode->i_sb->s_blocksize;
2341 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2342 xs->header = (struct ocfs2_xattr_header *)
2343 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2345 xs->header = (struct ocfs2_xattr_header *)
2346 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2347 xs->base = (void *)xs->header;
2348 xs->here = xs->header->xh_entries;
2350 /* Find the named attribute. */
2351 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2352 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2353 if (ret && ret != -ENODATA)
2355 xs->not_found = ret;
2362 * ocfs2_xattr_ibody_set()
2364 * Set, replace or remove an extended attribute into inode block.
2367 static int ocfs2_xattr_ibody_set(struct inode *inode,
2368 struct ocfs2_xattr_info *xi,
2369 struct ocfs2_xattr_search *xs,
2370 struct ocfs2_xattr_set_ctxt *ctxt)
2372 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2373 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2376 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2379 down_write(&oi->ip_alloc_sem);
2380 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2381 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2387 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2388 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2390 up_write(&oi->ip_alloc_sem);
2396 * ocfs2_xattr_block_find()
2398 * Find extended attribute in external block and
2399 * fill search info into struct ocfs2_xattr_search.
2401 static int ocfs2_xattr_block_find(struct inode *inode,
2404 struct ocfs2_xattr_search *xs)
2406 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2407 struct buffer_head *blk_bh = NULL;
2408 struct ocfs2_xattr_block *xb;
2411 if (!di->i_xattr_loc)
2414 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2421 xs->xattr_bh = blk_bh;
2422 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2424 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2425 xs->header = &xb->xb_attrs.xb_header;
2426 xs->base = (void *)xs->header;
2427 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2428 xs->here = xs->header->xh_entries;
2430 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2432 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2436 if (ret && ret != -ENODATA) {
2437 xs->xattr_bh = NULL;
2440 xs->not_found = ret;
2448 static int ocfs2_create_xattr_block(handle_t *handle,
2449 struct inode *inode,
2450 struct buffer_head *inode_bh,
2451 struct ocfs2_alloc_context *meta_ac,
2452 struct buffer_head **ret_bh,
2456 u16 suballoc_bit_start;
2459 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2460 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2461 struct buffer_head *new_bh = NULL;
2462 struct ocfs2_xattr_block *xblk;
2464 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2465 OCFS2_JOURNAL_ACCESS_CREATE);
2471 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2472 &suballoc_bit_start, &num_got,
2479 new_bh = sb_getblk(inode->i_sb, first_blkno);
2480 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2482 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2484 OCFS2_JOURNAL_ACCESS_CREATE);
2490 /* Initialize ocfs2_xattr_block */
2491 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2492 memset(xblk, 0, inode->i_sb->s_blocksize);
2493 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2494 xblk->xb_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
2495 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2496 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2497 xblk->xb_blkno = cpu_to_le64(first_blkno);
2500 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2501 xr->xt_clusters = cpu_to_le32(1);
2502 xr->xt_last_eb_blk = 0;
2503 xr->xt_list.l_tree_depth = 0;
2504 xr->xt_list.l_count = cpu_to_le16(
2505 ocfs2_xattr_recs_per_xb(inode->i_sb));
2506 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2507 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2510 ret = ocfs2_journal_dirty(handle, new_bh);
2515 di->i_xattr_loc = cpu_to_le64(first_blkno);
2516 ocfs2_journal_dirty(handle, inode_bh);
2527 * ocfs2_xattr_block_set()
2529 * Set, replace or remove an extended attribute into external block.
2532 static int ocfs2_xattr_block_set(struct inode *inode,
2533 struct ocfs2_xattr_info *xi,
2534 struct ocfs2_xattr_search *xs,
2535 struct ocfs2_xattr_set_ctxt *ctxt)
2537 struct buffer_head *new_bh = NULL;
2538 handle_t *handle = ctxt->handle;
2539 struct ocfs2_xattr_block *xblk = NULL;
2542 if (!xs->xattr_bh) {
2543 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
2544 ctxt->meta_ac, &new_bh, 0);
2550 xs->xattr_bh = new_bh;
2551 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2552 xs->header = &xblk->xb_attrs.xb_header;
2553 xs->base = (void *)xs->header;
2554 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2555 xs->here = xs->header->xh_entries;
2557 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2559 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2560 /* Set extended attribute into external block */
2561 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2562 OCFS2_HAS_XATTR_FL);
2563 if (!ret || ret != -ENOSPC)
2566 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2571 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2578 /* Check whether the new xattr can be inserted into the inode. */
2579 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2580 struct ocfs2_xattr_info *xi,
2581 struct ocfs2_xattr_search *xs)
2583 struct ocfs2_xattr_entry *last;
2585 size_t min_offs = xs->end - xs->base;
2590 last = xs->header->xh_entries;
2592 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2593 size_t offs = le16_to_cpu(last->xe_name_offset);
2594 if (offs < min_offs)
2599 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2603 BUG_ON(!xs->not_found);
2605 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
2611 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2612 struct ocfs2_dinode *di,
2613 struct ocfs2_xattr_info *xi,
2614 struct ocfs2_xattr_search *xis,
2615 struct ocfs2_xattr_search *xbs,
2620 int ret = 0, old_in_xb = 0;
2621 int clusters_add = 0, meta_add = 0, credits = 0;
2622 struct buffer_head *bh = NULL;
2623 struct ocfs2_xattr_block *xb = NULL;
2624 struct ocfs2_xattr_entry *xe = NULL;
2625 struct ocfs2_xattr_value_root *xv = NULL;
2627 int name_offset, name_len = 0;
2628 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2633 * Calculate the clusters we need to write.
2634 * No matter whether we replace an old one or add a new one,
2635 * we need this for writing.
2637 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2638 credits += new_clusters *
2639 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2641 if (xis->not_found && xbs->not_found) {
2642 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2644 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2645 clusters_add += new_clusters;
2646 credits += ocfs2_calc_extend_credits(inode->i_sb,
2654 if (!xis->not_found) {
2656 name_offset = le16_to_cpu(xe->xe_name_offset);
2657 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2659 credits += OCFS2_INODE_UPDATE_CREDITS;
2661 int i, block_off = 0;
2662 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2664 name_offset = le16_to_cpu(xe->xe_name_offset);
2665 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2666 i = xbs->here - xbs->header->xh_entries;
2669 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2670 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
2671 bucket_xh(xbs->bucket),
2674 base = bucket_block(xbs->bucket, block_off);
2675 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2678 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2683 * delete a xattr doesn't need metadata and cluster allocation.
2684 * so just calculate the credits and return.
2686 * The credits for removing the value tree will be extended
2687 * by ocfs2_remove_extent itself.
2689 if (!xi->xi_value) {
2690 if (!ocfs2_xattr_is_local(xe))
2691 credits += ocfs2_remove_extent_credits(inode->i_sb);
2696 /* do cluster allocation guess first. */
2697 value_size = le64_to_cpu(xe->xe_value_size);
2701 * In xattr set, we always try to set the xe in inode first,
2702 * so if it can be inserted into inode successfully, the old
2703 * one will be removed from the xattr block, and this xattr
2704 * will be inserted into inode as a new xattr in inode.
2706 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2707 clusters_add += new_clusters;
2708 credits += ocfs2_remove_extent_credits(inode->i_sb) +
2709 OCFS2_INODE_UPDATE_CREDITS;
2710 if (!ocfs2_xattr_is_local(xe))
2711 credits += ocfs2_calc_extend_credits(
2719 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2720 /* the new values will be stored outside. */
2721 u32 old_clusters = 0;
2723 if (!ocfs2_xattr_is_local(xe)) {
2724 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2726 xv = (struct ocfs2_xattr_value_root *)
2727 (base + name_offset + name_len);
2728 value_size = OCFS2_XATTR_ROOT_SIZE;
2732 if (old_clusters >= new_clusters) {
2733 credits += ocfs2_remove_extent_credits(inode->i_sb);
2736 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2737 clusters_add += new_clusters - old_clusters;
2738 credits += ocfs2_calc_extend_credits(inode->i_sb,
2742 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2747 * Now the new value will be stored inside. So if the new
2748 * value is smaller than the size of value root or the old
2749 * value, we don't need any allocation, otherwise we have
2750 * to guess metadata allocation.
2752 if ((ocfs2_xattr_is_local(xe) &&
2753 (value_size >= xi->xi_value_len)) ||
2754 (!ocfs2_xattr_is_local(xe) &&
2755 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
2760 /* calculate metadata allocation. */
2761 if (di->i_xattr_loc) {
2762 if (!xbs->xattr_bh) {
2763 ret = ocfs2_read_xattr_block(inode,
2764 le64_to_cpu(di->i_xattr_loc),
2771 xb = (struct ocfs2_xattr_block *)bh->b_data;
2773 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2776 * If there is already an xattr tree, good, we can calculate
2777 * like other b-trees. Otherwise we may have the chance of
2778 * create a tree, the credit calculation is borrowed from
2779 * ocfs2_calc_extend_credits with root_el = NULL. And the
2780 * new tree will be cluster based, so no meta is needed.
2782 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2783 struct ocfs2_extent_list *el =
2784 &xb->xb_attrs.xb_root.xt_list;
2785 meta_add += ocfs2_extend_meta_needed(el);
2786 credits += ocfs2_calc_extend_credits(inode->i_sb,
2789 credits += OCFS2_SUBALLOC_ALLOC + 1;
2792 * This cluster will be used either for new bucket or for
2794 * If the cluster size is the same as the bucket size, one
2795 * more is needed since we may need to extend the bucket
2799 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2800 if (OCFS2_XATTR_BUCKET_SIZE ==
2801 OCFS2_SB(inode->i_sb)->s_clustersize) {
2802 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2807 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2811 *clusters_need = clusters_add;
2813 *meta_need = meta_add;
2815 *credits_need = credits;
2820 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2821 struct ocfs2_dinode *di,
2822 struct ocfs2_xattr_info *xi,
2823 struct ocfs2_xattr_search *xis,
2824 struct ocfs2_xattr_search *xbs,
2825 struct ocfs2_xattr_set_ctxt *ctxt,
2829 int clusters_add, meta_add, ret;
2830 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2832 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2834 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2836 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
2837 &clusters_add, &meta_add, credits);
2843 meta_add += extra_meta;
2844 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2845 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
2848 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2857 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2863 if (ctxt->meta_ac) {
2864 ocfs2_free_alloc_context(ctxt->meta_ac);
2865 ctxt->meta_ac = NULL;
2869 * We cannot have an error and a non null ctxt->data_ac.
2876 static int __ocfs2_xattr_set_handle(struct inode *inode,
2877 struct ocfs2_dinode *di,
2878 struct ocfs2_xattr_info *xi,
2879 struct ocfs2_xattr_search *xis,
2880 struct ocfs2_xattr_search *xbs,
2881 struct ocfs2_xattr_set_ctxt *ctxt)
2883 int ret = 0, credits, old_found;
2885 if (!xi->xi_value) {
2886 /* Remove existing extended attribute */
2887 if (!xis->not_found)
2888 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2889 else if (!xbs->not_found)
2890 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2892 /* We always try to set extended attribute into inode first*/
2893 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2894 if (!ret && !xbs->not_found) {
2896 * If succeed and that extended attribute existing in
2897 * external block, then we will remove it.
2899 xi->xi_value = NULL;
2900 xi->xi_value_len = 0;
2902 old_found = xis->not_found;
2903 xis->not_found = -ENODATA;
2904 ret = ocfs2_calc_xattr_set_need(inode,
2912 xis->not_found = old_found;
2918 ret = ocfs2_extend_trans(ctxt->handle, credits +
2919 ctxt->handle->h_buffer_credits);
2924 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2925 } else if (ret == -ENOSPC) {
2926 if (di->i_xattr_loc && !xbs->xattr_bh) {
2927 ret = ocfs2_xattr_block_find(inode,
2933 old_found = xis->not_found;
2934 xis->not_found = -ENODATA;
2935 ret = ocfs2_calc_xattr_set_need(inode,
2943 xis->not_found = old_found;
2949 ret = ocfs2_extend_trans(ctxt->handle, credits +
2950 ctxt->handle->h_buffer_credits);
2957 * If no space in inode, we will set extended attribute
2958 * into external block.
2960 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2963 if (!xis->not_found) {
2965 * If succeed and that extended attribute
2966 * existing in inode, we will remove it.
2968 xi->xi_value = NULL;
2969 xi->xi_value_len = 0;
2970 xbs->not_found = -ENODATA;
2971 ret = ocfs2_calc_xattr_set_need(inode,
2984 ret = ocfs2_extend_trans(ctxt->handle, credits +
2985 ctxt->handle->h_buffer_credits);
2990 ret = ocfs2_xattr_ibody_set(inode, xi,
2997 /* Update inode ctime. */
2998 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3000 OCFS2_JOURNAL_ACCESS_WRITE);
3006 inode->i_ctime = CURRENT_TIME;
3007 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3008 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3009 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3016 * This function only called duing creating inode
3017 * for init security/acl xattrs of the new inode.
3018 * All transanction credits have been reserved in mknod.
3020 int ocfs2_xattr_set_handle(handle_t *handle,
3021 struct inode *inode,
3022 struct buffer_head *di_bh,
3028 struct ocfs2_alloc_context *meta_ac,
3029 struct ocfs2_alloc_context *data_ac)
3031 struct ocfs2_dinode *di;
3034 struct ocfs2_xattr_info xi = {
3035 .xi_name_index = name_index,
3037 .xi_name_len = strlen(name),
3039 .xi_value_len = value_len,
3042 struct ocfs2_xattr_search xis = {
3043 .not_found = -ENODATA,
3046 struct ocfs2_xattr_search xbs = {
3047 .not_found = -ENODATA,
3050 struct ocfs2_xattr_set_ctxt ctxt = {
3056 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3060 * In extreme situation, may need xattr bucket when
3061 * block size is too small. And we have already reserved
3062 * the credits for bucket in mknod.
3064 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3065 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3067 mlog_errno(-ENOMEM);
3072 xis.inode_bh = xbs.inode_bh = di_bh;
3073 di = (struct ocfs2_dinode *)di_bh->b_data;
3075 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3077 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3080 if (xis.not_found) {
3081 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3086 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3089 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3090 brelse(xbs.xattr_bh);
3091 ocfs2_xattr_bucket_free(xbs.bucket);
3099 * Set, replace or remove an extended attribute for this inode.
3100 * value is NULL to remove an existing extended attribute, else either
3101 * create or replace an extended attribute.
3103 int ocfs2_xattr_set(struct inode *inode,
3110 struct buffer_head *di_bh = NULL;
3111 struct ocfs2_dinode *di;
3112 int ret, credits, ref_meta = 0, ref_credits = 0;
3113 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3114 struct inode *tl_inode = osb->osb_tl_inode;
3115 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
3116 struct ocfs2_refcount_tree *ref_tree = NULL;
3118 struct ocfs2_xattr_info xi = {
3119 .xi_name_index = name_index,
3121 .xi_name_len = strlen(name),
3123 .xi_value_len = value_len,
3126 struct ocfs2_xattr_search xis = {
3127 .not_found = -ENODATA,
3130 struct ocfs2_xattr_search xbs = {
3131 .not_found = -ENODATA,
3134 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3138 * Only xbs will be used on indexed trees. xis doesn't need a
3141 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3143 mlog_errno(-ENOMEM);
3147 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3150 goto cleanup_nolock;
3152 xis.inode_bh = xbs.inode_bh = di_bh;
3153 di = (struct ocfs2_dinode *)di_bh->b_data;
3155 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3157 * Scan inode and external block to find the same name
3158 * extended attribute and collect search infomation.
3160 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3163 if (xis.not_found) {
3164 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3169 if (xis.not_found && xbs.not_found) {
3171 if (flags & XATTR_REPLACE)
3178 if (flags & XATTR_CREATE)
3182 /* Check whether the value is refcounted and do some prepartion. */
3183 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3184 (!xis.not_found || !xbs.not_found)) {
3185 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3186 &xis, &xbs, &ref_tree,
3187 &ref_meta, &ref_credits);
3194 mutex_lock(&tl_inode->i_mutex);
3196 if (ocfs2_truncate_log_needs_flush(osb)) {
3197 ret = __ocfs2_flush_truncate_log(osb);
3199 mutex_unlock(&tl_inode->i_mutex);
3204 mutex_unlock(&tl_inode->i_mutex);
3206 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3207 &xbs, &ctxt, ref_meta, &credits);
3213 /* we need to update inode's ctime field, so add credit for it. */
3214 credits += OCFS2_INODE_UPDATE_CREDITS;
3215 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3216 if (IS_ERR(ctxt.handle)) {
3217 ret = PTR_ERR(ctxt.handle);
3222 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3224 ocfs2_commit_trans(osb, ctxt.handle);
3227 ocfs2_free_alloc_context(ctxt.data_ac);
3229 ocfs2_free_alloc_context(ctxt.meta_ac);
3230 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3231 ocfs2_schedule_truncate_log_flush(osb, 1);
3232 ocfs2_run_deallocs(osb, &ctxt.dealloc);
3236 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3237 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3238 if (!value && !ret) {
3239 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3243 ocfs2_inode_unlock(inode, 1);
3246 brelse(xbs.xattr_bh);
3247 ocfs2_xattr_bucket_free(xbs.bucket);
3253 * Find the xattr extent rec which may contains name_hash.
3254 * e_cpos will be the first name hash of the xattr rec.
3255 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3257 static int ocfs2_xattr_get_rec(struct inode *inode,
3262 struct ocfs2_extent_list *el)
3265 struct buffer_head *eb_bh = NULL;
3266 struct ocfs2_extent_block *eb;
3267 struct ocfs2_extent_rec *rec = NULL;
3270 if (el->l_tree_depth) {
3271 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3278 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3281 if (el->l_tree_depth) {
3282 ocfs2_error(inode->i_sb,
3283 "Inode %lu has non zero tree depth in "
3284 "xattr tree block %llu\n", inode->i_ino,
3285 (unsigned long long)eb_bh->b_blocknr);
3291 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3292 rec = &el->l_recs[i];
3294 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3295 e_blkno = le64_to_cpu(rec->e_blkno);
3301 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3302 "record (%u, %u, 0) in xattr", inode->i_ino,
3303 le32_to_cpu(rec->e_cpos),
3304 ocfs2_rec_clusters(el, rec));
3309 *p_blkno = le64_to_cpu(rec->e_blkno);
3310 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3312 *e_cpos = le32_to_cpu(rec->e_cpos);
3318 typedef int (xattr_bucket_func)(struct inode *inode,
3319 struct ocfs2_xattr_bucket *bucket,
3322 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3323 struct ocfs2_xattr_bucket *bucket,
3330 int i, ret = 0, cmp = 1, block_off, new_offset;
3331 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3332 size_t name_len = strlen(name);
3333 struct ocfs2_xattr_entry *xe = NULL;
3337 * We don't use binary search in the bucket because there
3338 * may be multiple entries with the same name hash.
3340 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3341 xe = &xh->xh_entries[i];
3343 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3345 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3348 cmp = name_index - ocfs2_xattr_get_type(xe);
3350 cmp = name_len - xe->xe_name_len;
3354 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3365 xe_name = bucket_block(bucket, block_off) + new_offset;
3366 if (!memcmp(name, xe_name, name_len)) {
3378 * Find the specified xattr entry in a series of buckets.
3379 * This series start from p_blkno and last for num_clusters.
3380 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3381 * the num of the valid buckets.
3383 * Return the buffer_head this xattr should reside in. And if the xattr's
3384 * hash is in the gap of 2 buckets, return the lower bucket.
3386 static int ocfs2_xattr_bucket_find(struct inode *inode,
3393 struct ocfs2_xattr_search *xs)
3396 struct ocfs2_xattr_header *xh = NULL;
3397 struct ocfs2_xattr_entry *xe = NULL;
3399 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3400 int low_bucket = 0, bucket, high_bucket;
3401 struct ocfs2_xattr_bucket *search;
3403 u64 blkno, lower_blkno = 0;
3405 search = ocfs2_xattr_bucket_new(inode);
3412 ret = ocfs2_read_xattr_bucket(search, p_blkno);
3418 xh = bucket_xh(search);
3419 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3420 while (low_bucket <= high_bucket) {
3421 ocfs2_xattr_bucket_relse(search);
3423 bucket = (low_bucket + high_bucket) / 2;
3424 blkno = p_blkno + bucket * blk_per_bucket;
3425 ret = ocfs2_read_xattr_bucket(search, blkno);
3431 xh = bucket_xh(search);
3432 xe = &xh->xh_entries[0];
3433 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3434 high_bucket = bucket - 1;
3439 * Check whether the hash of the last entry in our
3440 * bucket is larger than the search one. for an empty
3441 * bucket, the last one is also the first one.
3444 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3446 last_hash = le32_to_cpu(xe->xe_name_hash);
3448 /* record lower_blkno which may be the insert place. */
3449 lower_blkno = blkno;
3451 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3452 low_bucket = bucket + 1;
3456 /* the searched xattr should reside in this bucket if exists. */
3457 ret = ocfs2_find_xe_in_bucket(inode, search,
3458 name_index, name, name_hash,
3468 * Record the bucket we have found.
3469 * When the xattr's hash value is in the gap of 2 buckets, we will
3470 * always set it to the previous bucket.
3473 lower_blkno = p_blkno;
3475 /* This should be in cache - we just read it during the search */
3476 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3482 xs->header = bucket_xh(xs->bucket);
3483 xs->base = bucket_block(xs->bucket, 0);
3484 xs->end = xs->base + inode->i_sb->s_blocksize;
3487 xs->here = &xs->header->xh_entries[index];
3488 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3489 (unsigned long long)bucket_blkno(xs->bucket), index);
3494 ocfs2_xattr_bucket_free(search);
3498 static int ocfs2_xattr_index_block_find(struct inode *inode,
3499 struct buffer_head *root_bh,
3502 struct ocfs2_xattr_search *xs)
3505 struct ocfs2_xattr_block *xb =
3506 (struct ocfs2_xattr_block *)root_bh->b_data;
3507 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3508 struct ocfs2_extent_list *el = &xb_root->xt_list;
3510 u32 first_hash, num_clusters = 0;
3511 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3513 if (le16_to_cpu(el->l_next_free_rec) == 0)
3516 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3517 name, name_hash, name_index);
3519 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3526 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3528 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3529 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3532 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3533 p_blkno, first_hash, num_clusters, xs);
3539 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3542 xattr_bucket_func *func,
3546 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3547 u32 num_buckets = clusters * bpc;
3548 struct ocfs2_xattr_bucket *bucket;
3550 bucket = ocfs2_xattr_bucket_new(inode);
3552 mlog_errno(-ENOMEM);
3556 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3557 clusters, (unsigned long long)blkno);
3559 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3560 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3567 * The real bucket num in this series of blocks is stored
3568 * in the 1st bucket.
3571 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3573 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3574 (unsigned long long)blkno,
3575 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3577 ret = func(inode, bucket, para);
3578 if (ret && ret != -ERANGE)
3580 /* Fall through to bucket_relse() */
3583 ocfs2_xattr_bucket_relse(bucket);
3588 ocfs2_xattr_bucket_free(bucket);
3592 struct ocfs2_xattr_tree_list {
3598 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
3599 struct ocfs2_xattr_header *xh,
3606 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3609 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3611 *block_off = name_offset >> sb->s_blocksize_bits;
3612 *new_offset = name_offset % sb->s_blocksize;
3617 static int ocfs2_list_xattr_bucket(struct inode *inode,
3618 struct ocfs2_xattr_bucket *bucket,
3622 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
3623 int i, block_off, new_offset;
3624 const char *prefix, *name;
3626 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3627 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
3628 type = ocfs2_xattr_get_type(entry);
3629 prefix = ocfs2_xattr_prefix(type);
3632 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3640 name = (const char *)bucket_block(bucket, block_off) +
3642 ret = ocfs2_xattr_list_entry(xl->buffer,
3646 entry->xe_name_len);
3655 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
3656 struct buffer_head *blk_bh,
3657 xattr_tree_rec_func *rec_func,
3660 struct ocfs2_xattr_block *xb =
3661 (struct ocfs2_xattr_block *)blk_bh->b_data;
3662 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
3664 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3667 if (!el->l_next_free_rec || !rec_func)
3670 while (name_hash > 0) {
3671 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3672 &e_cpos, &num_clusters, el);
3678 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
3679 num_clusters, para);
3689 name_hash = e_cpos - 1;
3696 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
3697 struct buffer_head *root_bh,
3698 u64 blkno, u32 cpos, u32 len, void *para)
3700 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
3701 ocfs2_list_xattr_bucket, para);
3704 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3705 struct buffer_head *blk_bh,
3710 struct ocfs2_xattr_tree_list xl = {
3712 .buffer_size = buffer_size,
3716 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
3717 ocfs2_list_xattr_tree_rec, &xl);
3728 static int cmp_xe(const void *a, const void *b)
3730 const struct ocfs2_xattr_entry *l = a, *r = b;
3731 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3732 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3734 if (l_hash > r_hash)
3736 if (l_hash < r_hash)
3741 static void swap_xe(void *a, void *b, int size)
3743 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3746 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3747 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3751 * When the ocfs2_xattr_block is filled up, new bucket will be created
3752 * and all the xattr entries will be moved to the new bucket.
3753 * The header goes at the start of the bucket, and the names+values are
3754 * filled from the end. This is why *target starts as the last buffer.
3755 * Note: we need to sort the entries since they are not saved in order
3756 * in the ocfs2_xattr_block.
3758 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3759 struct buffer_head *xb_bh,
3760 struct ocfs2_xattr_bucket *bucket)
3762 int i, blocksize = inode->i_sb->s_blocksize;
3763 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3764 u16 offset, size, off_change;
3765 struct ocfs2_xattr_entry *xe;
3766 struct ocfs2_xattr_block *xb =
3767 (struct ocfs2_xattr_block *)xb_bh->b_data;
3768 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
3769 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3770 u16 count = le16_to_cpu(xb_xh->xh_count);
3771 char *src = xb_bh->b_data;
3772 char *target = bucket_block(bucket, blks - 1);
3774 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3775 (unsigned long long)xb_bh->b_blocknr,
3776 (unsigned long long)bucket_blkno(bucket));
3778 for (i = 0; i < blks; i++)
3779 memset(bucket_block(bucket, i), 0, blocksize);
3782 * Since the xe_name_offset is based on ocfs2_xattr_header,
3783 * there is a offset change corresponding to the change of
3784 * ocfs2_xattr_header's position.
3786 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3787 xe = &xb_xh->xh_entries[count - 1];
3788 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3789 size = blocksize - offset;
3791 /* copy all the names and values. */
3792 memcpy(target + offset, src + offset, size);
3794 /* Init new header now. */
3795 xh->xh_count = xb_xh->xh_count;
3796 xh->xh_num_buckets = cpu_to_le16(1);
3797 xh->xh_name_value_len = cpu_to_le16(size);
3798 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3800 /* copy all the entries. */
3801 target = bucket_block(bucket, 0);
3802 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3803 size = count * sizeof(struct ocfs2_xattr_entry);
3804 memcpy(target + offset, (char *)xb_xh + offset, size);
3806 /* Change the xe offset for all the xe because of the move. */
3807 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3808 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3809 for (i = 0; i < count; i++)
3810 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3812 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3813 offset, size, off_change);
3815 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3820 * After we move xattr from block to index btree, we have to
3821 * update ocfs2_xattr_search to the new xe and base.
3823 * When the entry is in xattr block, xattr_bh indicates the storage place.
3824 * While if the entry is in index b-tree, "bucket" indicates the
3825 * real place of the xattr.
3827 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3828 struct ocfs2_xattr_search *xs,
3829 struct buffer_head *old_bh)
3831 char *buf = old_bh->b_data;
3832 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3833 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
3836 xs->header = bucket_xh(xs->bucket);
3837 xs->base = bucket_block(xs->bucket, 0);
3838 xs->end = xs->base + inode->i_sb->s_blocksize;
3843 i = xs->here - old_xh->xh_entries;
3844 xs->here = &xs->header->xh_entries[i];
3847 static int ocfs2_xattr_create_index_block(struct inode *inode,
3848 struct ocfs2_xattr_search *xs,
3849 struct ocfs2_xattr_set_ctxt *ctxt)
3854 handle_t *handle = ctxt->handle;
3855 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3856 struct ocfs2_inode_info *oi = OCFS2_I(inode);
3857 struct buffer_head *xb_bh = xs->xattr_bh;
3858 struct ocfs2_xattr_block *xb =
3859 (struct ocfs2_xattr_block *)xb_bh->b_data;
3860 struct ocfs2_xattr_tree_root *xr;
3861 u16 xb_flags = le16_to_cpu(xb->xb_flags);
3863 mlog(0, "create xattr index block for %llu\n",
3864 (unsigned long long)xb_bh->b_blocknr);
3866 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
3867 BUG_ON(!xs->bucket);
3871 * We can use this lock for now, and maybe move to a dedicated mutex
3872 * if performance becomes a problem later.
3874 down_write(&oi->ip_alloc_sem);
3876 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
3877 OCFS2_JOURNAL_ACCESS_WRITE);
3883 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3884 1, 1, &bit_off, &len);
3891 * The bucket may spread in many blocks, and
3892 * we will only touch the 1st block and the last block
3893 * in the whole bucket(one for entry and one for data).
3895 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3897 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3898 (unsigned long long)blkno);
3900 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
3906 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3907 OCFS2_JOURNAL_ACCESS_CREATE);
3913 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3914 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3916 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3918 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3919 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3920 offsetof(struct ocfs2_xattr_block, xb_attrs));
3922 xr = &xb->xb_attrs.xb_root;
3923 xr->xt_clusters = cpu_to_le32(1);
3924 xr->xt_last_eb_blk = 0;
3925 xr->xt_list.l_tree_depth = 0;
3926 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3927 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3929 xr->xt_list.l_recs[0].e_cpos = 0;
3930 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3931 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3933 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3935 ocfs2_journal_dirty(handle, xb_bh);
3938 up_write(&oi->ip_alloc_sem);
3943 static int cmp_xe_offset(const void *a, const void *b)
3945 const struct ocfs2_xattr_entry *l = a, *r = b;
3946 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3947 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3949 if (l_name_offset < r_name_offset)
3951 if (l_name_offset > r_name_offset)
3957 * defrag a xattr bucket if we find that the bucket has some
3958 * holes beteen name/value pairs.
3959 * We will move all the name/value pairs to the end of the bucket
3960 * so that we can spare some space for insertion.
3962 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
3964 struct ocfs2_xattr_bucket *bucket)
3967 size_t end, offset, len;
3968 struct ocfs2_xattr_header *xh;
3969 char *entries, *buf, *bucket_buf = NULL;
3970 u64 blkno = bucket_blkno(bucket);
3972 size_t blocksize = inode->i_sb->s_blocksize;
3973 struct ocfs2_xattr_entry *xe;
3976 * In order to make the operation more efficient and generic,
3977 * we copy all the blocks into a contiguous memory and do the
3978 * defragment there, so if anything is error, we will not touch
3981 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3988 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3989 memcpy(buf, bucket_block(bucket, i), blocksize);
3991 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
3992 OCFS2_JOURNAL_ACCESS_WRITE);
3998 xh = (struct ocfs2_xattr_header *)bucket_buf;
3999 entries = (char *)xh->xh_entries;
4000 xh_free_start = le16_to_cpu(xh->xh_free_start);
4002 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4003 "xh_free_start = %u, xh_name_value_len = %u.\n",
4004 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4005 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4008 * sort all the entries by their offset.
4009 * the largest will be the first, so that we can
4010 * move them to the end one by one.
4012 sort(entries, le16_to_cpu(xh->xh_count),
4013 sizeof(struct ocfs2_xattr_entry),
4014 cmp_xe_offset, swap_xe);
4016 /* Move all name/values to the end of the bucket. */
4017 xe = xh->xh_entries;
4018 end = OCFS2_XATTR_BUCKET_SIZE;
4019 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4020 offset = le16_to_cpu(xe->xe_name_offset);
4021 len = namevalue_size_xe(xe);
4024 * We must make sure that the name/value pair
4025 * exist in the same block. So adjust end to
4026 * the previous block end if needed.
4028 if (((end - len) / blocksize !=
4029 (end - 1) / blocksize))
4030 end = end - end % blocksize;
4032 if (end > offset + len) {
4033 memmove(bucket_buf + end - len,
4034 bucket_buf + offset, len);
4035 xe->xe_name_offset = cpu_to_le16(end - len);
4038 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4039 "bucket %llu\n", (unsigned long long)blkno);
4044 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4045 "bucket %llu\n", (unsigned long long)blkno);
4047 if (xh_free_start == end)
4050 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4051 xh->xh_free_start = cpu_to_le16(end);
4053 /* sort the entries by their name_hash. */
4054 sort(entries, le16_to_cpu(xh->xh_count),
4055 sizeof(struct ocfs2_xattr_entry),
4059 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4060 memcpy(bucket_block(bucket, i), buf, blocksize);
4061 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4069 * prev_blkno points to the start of an existing extent. new_blkno
4070 * points to a newly allocated extent. Because we know each of our
4071 * clusters contains more than bucket, we can easily split one cluster
4072 * at a bucket boundary. So we take the last cluster of the existing
4073 * extent and split it down the middle. We move the last half of the
4074 * buckets in the last cluster of the existing extent over to the new
4077 * first_bh is the buffer at prev_blkno so we can update the existing
4078 * extent's bucket count. header_bh is the bucket were we were hoping
4079 * to insert our xattr. If the bucket move places the target in the new
4080 * extent, we'll update first_bh and header_bh after modifying the old
4083 * first_hash will be set as the 1st xe's name_hash in the new extent.
4085 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4087 struct ocfs2_xattr_bucket *first,
4088 struct ocfs2_xattr_bucket *target,
4094 struct super_block *sb = inode->i_sb;
4095 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4096 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4097 int to_move = num_buckets / 2;
4099 u64 last_cluster_blkno = bucket_blkno(first) +
4100 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4102 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4103 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4105 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4106 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
4108 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4109 last_cluster_blkno, new_blkno,
4110 to_move, first_hash);
4116 /* This is the first bucket that got moved */
4117 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4120 * If the target bucket was part of the moved buckets, we need to
4121 * update first and target.
4123 if (bucket_blkno(target) >= src_blkno) {
4124 /* Find the block for the new target bucket */
4125 src_blkno = new_blkno +
4126 (bucket_blkno(target) - src_blkno);
4128 ocfs2_xattr_bucket_relse(first);
4129 ocfs2_xattr_bucket_relse(target);
4132 * These shouldn't fail - the buffers are in the
4133 * journal from ocfs2_cp_xattr_bucket().
4135 ret = ocfs2_read_xattr_bucket(first, new_blkno);
4140 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4151 * Find the suitable pos when we divide a bucket into 2.
4152 * We have to make sure the xattrs with the same hash value exist
4153 * in the same bucket.
4155 * If this ocfs2_xattr_header covers more than one hash value, find a
4156 * place where the hash value changes. Try to find the most even split.
4157 * The most common case is that all entries have different hash values,
4158 * and the first check we make will find a place to split.
4160 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4162 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4163 int count = le16_to_cpu(xh->xh_count);
4164 int delta, middle = count / 2;
4167 * We start at the middle. Each step gets farther away in both
4168 * directions. We therefore hit the change in hash value
4169 * nearest to the middle. Note that this loop does not execute for
4172 for (delta = 0; delta < middle; delta++) {
4173 /* Let's check delta earlier than middle */
4174 if (cmp_xe(&entries[middle - delta - 1],
4175 &entries[middle - delta]))
4176 return middle - delta;
4178 /* For even counts, don't walk off the end */
4179 if ((middle + delta + 1) == count)
4182 /* Now try delta past middle */
4183 if (cmp_xe(&entries[middle + delta],
4184 &entries[middle + delta + 1]))
4185 return middle + delta + 1;
4188 /* Every entry had the same hash */
4193 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4194 * first_hash will record the 1st hash of the new bucket.
4196 * Normally half of the xattrs will be moved. But we have to make
4197 * sure that the xattrs with the same hash value are stored in the
4198 * same bucket. If all the xattrs in this bucket have the same hash
4199 * value, the new bucket will be initialized as an empty one and the
4200 * first_hash will be initialized as (hash_value+1).
4202 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4207 int new_bucket_head)
4210 int count, start, len, name_value_len = 0, name_offset = 0;
4211 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4212 struct ocfs2_xattr_header *xh;
4213 struct ocfs2_xattr_entry *xe;
4214 int blocksize = inode->i_sb->s_blocksize;
4216 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4217 (unsigned long long)blk, (unsigned long long)new_blk);
4219 s_bucket = ocfs2_xattr_bucket_new(inode);
4220 t_bucket = ocfs2_xattr_bucket_new(inode);
4221 if (!s_bucket || !t_bucket) {
4227 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4233 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4234 OCFS2_JOURNAL_ACCESS_WRITE);
4241 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4242 * there's no need to read it.
4244 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4251 * Hey, if we're overwriting t_bucket, what difference does
4252 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4253 * same part of ocfs2_cp_xattr_bucket().
4255 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4257 OCFS2_JOURNAL_ACCESS_CREATE :
4258 OCFS2_JOURNAL_ACCESS_WRITE);
4264 xh = bucket_xh(s_bucket);
4265 count = le16_to_cpu(xh->xh_count);
4266 start = ocfs2_xattr_find_divide_pos(xh);
4268 if (start == count) {
4269 xe = &xh->xh_entries[start-1];
4272 * initialized a new empty bucket here.
4273 * The hash value is set as one larger than
4274 * that of the last entry in the previous bucket.
4276 for (i = 0; i < t_bucket->bu_blocks; i++)
4277 memset(bucket_block(t_bucket, i), 0, blocksize);
4279 xh = bucket_xh(t_bucket);
4280 xh->xh_free_start = cpu_to_le16(blocksize);
4281 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4282 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4284 goto set_num_buckets;
4287 /* copy the whole bucket to the new first. */
4288 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4290 /* update the new bucket. */
4291 xh = bucket_xh(t_bucket);
4294 * Calculate the total name/value len and xh_free_start for
4295 * the old bucket first.
4297 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4299 for (i = 0; i < start; i++) {
4300 xe = &xh->xh_entries[i];
4301 name_value_len += namevalue_size_xe(xe);
4302 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4303 name_offset = le16_to_cpu(xe->xe_name_offset);
4307 * Now begin the modification to the new bucket.
4309 * In the new bucket, We just move the xattr entry to the beginning
4310 * and don't touch the name/value. So there will be some holes in the
4311 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4314 xe = &xh->xh_entries[start];
4315 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4316 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
4317 (int)((char *)xe - (char *)xh),
4318 (int)((char *)xh->xh_entries - (char *)xh));
4319 memmove((char *)xh->xh_entries, (char *)xe, len);
4320 xe = &xh->xh_entries[count - start];
4321 len = sizeof(struct ocfs2_xattr_entry) * start;
4322 memset((char *)xe, 0, len);
4324 le16_add_cpu(&xh->xh_count, -start);
4325 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4327 /* Calculate xh_free_start for the new bucket. */
4328 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4329 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4330 xe = &xh->xh_entries[i];
4331 if (le16_to_cpu(xe->xe_name_offset) <
4332 le16_to_cpu(xh->xh_free_start))
4333 xh->xh_free_start = xe->xe_name_offset;
4337 /* set xh->xh_num_buckets for the new xh. */
4338 if (new_bucket_head)
4339 xh->xh_num_buckets = cpu_to_le16(1);
4341 xh->xh_num_buckets = 0;
4343 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4345 /* store the first_hash of the new bucket. */
4347 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4350 * Now only update the 1st block of the old bucket. If we
4351 * just added a new empty bucket, there is no need to modify
4357 xh = bucket_xh(s_bucket);
4358 memset(&xh->xh_entries[start], 0,
4359 sizeof(struct ocfs2_xattr_entry) * (count - start));
4360 xh->xh_count = cpu_to_le16(start);
4361 xh->xh_free_start = cpu_to_le16(name_offset);
4362 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4364 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4367 ocfs2_xattr_bucket_free(s_bucket);
4368 ocfs2_xattr_bucket_free(t_bucket);
4374 * Copy xattr from one bucket to another bucket.
4376 * The caller must make sure that the journal transaction
4377 * has enough space for journaling.
4379 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4386 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4388 BUG_ON(s_blkno == t_blkno);
4390 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4391 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4394 s_bucket = ocfs2_xattr_bucket_new(inode);
4395 t_bucket = ocfs2_xattr_bucket_new(inode);
4396 if (!s_bucket || !t_bucket) {
4402 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4407 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4408 * there's no need to read it.
4410 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4415 * Hey, if we're overwriting t_bucket, what difference does
4416 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4417 * cluster to fill, we came here from
4418 * ocfs2_mv_xattr_buckets(), and it is really new -
4419 * ACCESS_CREATE is required. But we also might have moved data
4420 * out of t_bucket before extending back into it.
4421 * ocfs2_add_new_xattr_bucket() can do this - its call to
4422 * ocfs2_add_new_xattr_cluster() may have created a new extent
4423 * and copied out the end of the old extent. Then it re-extends
4424 * the old extent back to create space for new xattrs. That's
4425 * how we get here, and the bucket isn't really new.
4427 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4429 OCFS2_JOURNAL_ACCESS_CREATE :
4430 OCFS2_JOURNAL_ACCESS_WRITE);
4434 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4435 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4438 ocfs2_xattr_bucket_free(t_bucket);
4439 ocfs2_xattr_bucket_free(s_bucket);
4445 * src_blk points to the start of an existing extent. last_blk points to
4446 * last cluster in that extent. to_blk points to a newly allocated
4447 * extent. We copy the buckets from the cluster at last_blk to the new
4448 * extent. If start_bucket is non-zero, we skip that many buckets before
4449 * we start copying. The new extent's xh_num_buckets gets set to the
4450 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4451 * by the same amount.
4453 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4454 u64 src_blk, u64 last_blk, u64 to_blk,
4455 unsigned int start_bucket,
4458 int i, ret, credits;
4459 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4460 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4461 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4462 struct ocfs2_xattr_bucket *old_first, *new_first;
4464 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4465 (unsigned long long)last_blk, (unsigned long long)to_blk);
4467 BUG_ON(start_bucket >= num_buckets);
4469 num_buckets -= start_bucket;
4470 last_blk += (start_bucket * blks_per_bucket);
4473 /* The first bucket of the original extent */
4474 old_first = ocfs2_xattr_bucket_new(inode);
4475 /* The first bucket of the new extent */
4476 new_first = ocfs2_xattr_bucket_new(inode);
4477 if (!old_first || !new_first) {
4483 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4490 * We need to update the first bucket of the old extent and all
4491 * the buckets going to the new extent.
4493 credits = ((num_buckets + 1) * blks_per_bucket) +
4494 handle->h_buffer_credits;
4495 ret = ocfs2_extend_trans(handle, credits);
4501 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4502 OCFS2_JOURNAL_ACCESS_WRITE);
4508 for (i = 0; i < num_buckets; i++) {
4509 ret = ocfs2_cp_xattr_bucket(inode, handle,
4510 last_blk + (i * blks_per_bucket),
4511 to_blk + (i * blks_per_bucket),
4520 * Get the new bucket ready before we dirty anything
4521 * (This actually shouldn't fail, because we already dirtied
4522 * it once in ocfs2_cp_xattr_bucket()).
4524 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4529 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4530 OCFS2_JOURNAL_ACCESS_WRITE);
4536 /* Now update the headers */
4537 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4538 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4540 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4541 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4544 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4547 ocfs2_xattr_bucket_free(new_first);
4548 ocfs2_xattr_bucket_free(old_first);
4553 * Move some xattrs in this cluster to the new cluster.
4554 * This function should only be called when bucket size == cluster size.
4555 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4557 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4563 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4564 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
4566 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4568 ret = ocfs2_extend_trans(handle, credits);
4574 /* Move half of the xattr in start_blk to the next bucket. */
4575 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4576 new_blk, first_hash, 1);
4580 * Move some xattrs from the old cluster to the new one since they are not
4581 * contiguous in ocfs2 xattr tree.
4583 * new_blk starts a new separate cluster, and we will move some xattrs from
4584 * prev_blk to it. v_start will be set as the first name hash value in this
4585 * new cluster so that it can be used as e_cpos during tree insertion and
4586 * don't collide with our original b-tree operations. first_bh and header_bh
4587 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4588 * to extend the insert bucket.
4590 * The problem is how much xattr should we move to the new one and when should
4591 * we update first_bh and header_bh?
4592 * 1. If cluster size > bucket size, that means the previous cluster has more
4593 * than 1 bucket, so just move half nums of bucket into the new cluster and
4594 * update the first_bh and header_bh if the insert bucket has been moved
4595 * to the new cluster.
4596 * 2. If cluster_size == bucket_size:
4597 * a) If the previous extent rec has more than one cluster and the insert
4598 * place isn't in the last cluster, copy the entire last cluster to the
4599 * new one. This time, we don't need to upate the first_bh and header_bh
4600 * since they will not be moved into the new cluster.
4601 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4602 * the new one. And we set the extend flag to zero if the insert place is
4603 * moved into the new allocated cluster since no extend is needed.
4605 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4607 struct ocfs2_xattr_bucket *first,
4608 struct ocfs2_xattr_bucket *target,
4616 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4617 (unsigned long long)bucket_blkno(first), prev_clusters,
4618 (unsigned long long)new_blk);
4620 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
4621 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4630 /* The start of the last cluster in the first extent */
4631 u64 last_blk = bucket_blkno(first) +
4632 ((prev_clusters - 1) *
4633 ocfs2_clusters_to_blocks(inode->i_sb, 1));
4635 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
4636 ret = ocfs2_mv_xattr_buckets(inode, handle,
4637 bucket_blkno(first),
4638 last_blk, new_blk, 0,
4643 ret = ocfs2_divide_xattr_cluster(inode, handle,
4649 if ((bucket_blkno(target) == last_blk) && extend)
4658 * Add a new cluster for xattr storage.
4660 * If the new cluster is contiguous with the previous one, it will be
4661 * appended to the same extent record, and num_clusters will be updated.
4662 * If not, we will insert a new extent for it and move some xattrs in
4663 * the last cluster into the new allocated one.
4664 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4665 * lose the benefits of hashing because we'll have to search large leaves.
4666 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4669 * first_bh is the first block of the previous extent rec and header_bh
4670 * indicates the bucket we will insert the new xattrs. They will be updated
4671 * when the header_bh is moved into the new cluster.
4673 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4674 struct buffer_head *root_bh,
4675 struct ocfs2_xattr_bucket *first,
4676 struct ocfs2_xattr_bucket *target,
4680 struct ocfs2_xattr_set_ctxt *ctxt)
4683 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4684 u32 prev_clusters = *num_clusters;
4685 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4687 handle_t *handle = ctxt->handle;
4688 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4689 struct ocfs2_extent_tree et;
4691 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4692 "previous xattr blkno = %llu\n",
4693 (unsigned long long)OCFS2_I(inode)->ip_blkno,
4694 prev_cpos, (unsigned long long)bucket_blkno(first));
4696 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
4698 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
4699 OCFS2_JOURNAL_ACCESS_WRITE);
4705 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
4706 clusters_to_add, &bit_off, &num_bits);
4713 BUG_ON(num_bits > clusters_to_add);
4715 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4716 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4717 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4719 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
4720 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4721 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4723 * If this cluster is contiguous with the old one and
4724 * adding this new cluster, we don't surpass the limit of
4725 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4726 * initialized and used like other buckets in the previous
4728 * So add it as a contiguous one. The caller will handle
4731 v_start = prev_cpos + prev_clusters;
4732 *num_clusters = prev_clusters + num_bits;
4733 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4736 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4750 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
4751 num_bits, (unsigned long long)block, v_start);
4752 ret = ocfs2_insert_extent(handle, &et, v_start, block,
4753 num_bits, 0, ctxt->meta_ac);
4759 ret = ocfs2_journal_dirty(handle, root_bh);
4768 * We are given an extent. 'first' is the bucket at the very front of
4769 * the extent. The extent has space for an additional bucket past
4770 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4771 * of the target bucket. We wish to shift every bucket past the target
4772 * down one, filling in that additional space. When we get back to the
4773 * target, we split the target between itself and the now-empty bucket
4774 * at target+1 (aka, target_blkno + blks_per_bucket).
4776 static int ocfs2_extend_xattr_bucket(struct inode *inode,
4778 struct ocfs2_xattr_bucket *first,
4783 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4784 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4786 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
4788 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
4789 "from %llu, len = %u\n", (unsigned long long)target_blk,
4790 (unsigned long long)bucket_blkno(first), num_clusters);
4792 /* The extent must have room for an additional bucket */
4793 BUG_ON(new_bucket >=
4794 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
4796 /* end_blk points to the last existing bucket */
4797 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
4800 * end_blk is the start of the last existing bucket.
4801 * Thus, (end_blk - target_blk) covers the target bucket and
4802 * every bucket after it up to, but not including, the last
4803 * existing bucket. Then we add the last existing bucket, the
4804 * new bucket, and the first bucket (3 * blk_per_bucket).
4806 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
4807 handle->h_buffer_credits;
4808 ret = ocfs2_extend_trans(handle, credits);
4814 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4815 OCFS2_JOURNAL_ACCESS_WRITE);
4821 while (end_blk != target_blk) {
4822 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4823 end_blk + blk_per_bucket, 0);
4826 end_blk -= blk_per_bucket;
4829 /* Move half of the xattr in target_blkno to the next bucket. */
4830 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4831 target_blk + blk_per_bucket, NULL, 0);
4833 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4834 ocfs2_xattr_bucket_journal_dirty(handle, first);
4841 * Add new xattr bucket in an extent record and adjust the buckets
4842 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
4843 * bucket we want to insert into.
4845 * In the easy case, we will move all the buckets after target down by
4846 * one. Half of target's xattrs will be moved to the next bucket.
4848 * If current cluster is full, we'll allocate a new one. This may not
4849 * be contiguous. The underlying calls will make sure that there is
4850 * space for the insert, shifting buckets around if necessary.
4851 * 'target' may be moved by those calls.
4853 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4854 struct buffer_head *xb_bh,
4855 struct ocfs2_xattr_bucket *target,
4856 struct ocfs2_xattr_set_ctxt *ctxt)
4858 struct ocfs2_xattr_block *xb =
4859 (struct ocfs2_xattr_block *)xb_bh->b_data;
4860 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4861 struct ocfs2_extent_list *el = &xb_root->xt_list;
4863 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
4864 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4865 int ret, num_buckets, extend = 1;
4867 u32 e_cpos, num_clusters;
4868 /* The bucket at the front of the extent */
4869 struct ocfs2_xattr_bucket *first;
4871 mlog(0, "Add new xattr bucket starting from %llu\n",
4872 (unsigned long long)bucket_blkno(target));
4874 /* The first bucket of the original extent */
4875 first = ocfs2_xattr_bucket_new(inode);
4882 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4889 ret = ocfs2_read_xattr_bucket(first, p_blkno);
4895 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
4896 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
4898 * This can move first+target if the target bucket moves
4899 * to the new extent.
4901 ret = ocfs2_add_new_xattr_cluster(inode,
4916 ret = ocfs2_extend_xattr_bucket(inode,
4919 bucket_blkno(target),
4926 ocfs2_xattr_bucket_free(first);
4931 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4932 struct ocfs2_xattr_bucket *bucket,
4935 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4937 offs = offs % inode->i_sb->s_blocksize;
4938 return bucket_block(bucket, block_off) + offs;
4942 * Handle the normal xattr set, including replace, delete and new.
4944 * Note: "local" indicates the real data's locality. So we can't
4945 * just its bucket locality by its length.
4947 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4948 struct ocfs2_xattr_info *xi,
4949 struct ocfs2_xattr_search *xs,
4953 struct ocfs2_xattr_entry *last, *xe;
4954 struct ocfs2_xattr_header *xh = xs->header;
4955 u16 count = le16_to_cpu(xh->xh_count), start;
4956 size_t blocksize = inode->i_sb->s_blocksize;
4958 size_t offs, size, new_size;
4959 struct ocfs2_xa_loc loc;
4961 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
4962 xs->not_found ? NULL : xs->here);
4963 last = &xh->xh_entries[count];
4964 if (!xs->not_found) {
4966 offs = le16_to_cpu(xe->xe_name_offset);
4967 size = namevalue_size_xe(xe);
4970 * If the new value will be stored outside, xi->xi_value has
4971 * been initalized as an empty ocfs2_xattr_value_root, and
4972 * the same goes with xi->xi_value_len, so we can set
4973 * new_size safely here.
4974 * See ocfs2_xattr_set_in_bucket.
4976 new_size = namevalue_size_xi(xi);
4979 ocfs2_xa_wipe_namevalue(&loc);
4980 if (new_size > size)
4981 goto set_new_name_value;
4983 /* Now replace the old value with new one. */
4986 cpu_to_le64(xi->xi_value_len);
4988 xe->xe_value_size = 0;
4990 val = ocfs2_xattr_bucket_get_val(inode,
4992 memset(val + OCFS2_XATTR_SIZE(xi->xi_name_len), 0,
4993 size - OCFS2_XATTR_SIZE(xi->xi_name_len));
4994 if (OCFS2_XATTR_SIZE(xi->xi_value_len) > 0)
4995 memcpy(val + OCFS2_XATTR_SIZE(xi->xi_name_len),
4996 xi->xi_value, xi->xi_value_len);
4998 le16_add_cpu(&xh->xh_name_value_len, new_size);
4999 ocfs2_xattr_set_local(xe, local);
5002 ocfs2_xa_remove_entry(&loc);
5005 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
5010 /* find a new entry for insert. */
5011 int low = 0, high = count - 1, tmp;
5012 struct ocfs2_xattr_entry *tmp_xe;
5014 while (low <= high && count) {
5015 tmp = (low + high) / 2;
5016 tmp_xe = &xh->xh_entries[tmp];
5018 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
5020 else if (name_hash <
5021 le32_to_cpu(tmp_xe->xe_name_hash))
5029 xe = &xh->xh_entries[low];
5031 memmove(xe + 1, xe, (void *)last - (void *)xe);
5033 le16_add_cpu(&xh->xh_count, 1);
5034 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
5035 xe->xe_name_hash = cpu_to_le32(name_hash);
5036 xe->xe_name_len = xi->xi_name_len;
5037 ocfs2_xattr_set_type(xe, xi->xi_name_index);
5041 /* Insert the new name+value. */
5042 size = namevalue_size_xi(xi);
5045 * We must make sure that the name/value pair
5046 * exists in the same block.
5048 offs = le16_to_cpu(xh->xh_free_start);
5049 start = offs - size;
5051 if (start >> inode->i_sb->s_blocksize_bits !=
5052 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
5053 offs = offs - offs % blocksize;
5054 xh->xh_free_start = cpu_to_le16(offs);
5057 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
5058 xe->xe_name_offset = cpu_to_le16(offs - size);
5060 memset(val, 0, size);
5061 memcpy(val, xi->xi_name, xi->xi_name_len);
5062 memcpy(val + OCFS2_XATTR_SIZE(xi->xi_name_len), xi->xi_value,
5065 xe->xe_value_size = cpu_to_le64(xi->xi_value_len);
5066 ocfs2_xattr_set_local(xe, local);
5068 le16_add_cpu(&xh->xh_free_start, -size);
5069 le16_add_cpu(&xh->xh_name_value_len, size);
5075 * Set the xattr entry in the specified bucket.
5076 * The bucket is indicated by xs->bucket and it should have the enough
5077 * space for the xattr insertion.
5079 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
5081 struct ocfs2_xattr_info *xi,
5082 struct ocfs2_xattr_search *xs,
5089 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
5090 (unsigned long)xi->xi_value_len, xi->xi_name_index,
5091 (unsigned long long)bucket_blkno(xs->bucket));
5093 if (!xs->bucket->bu_bhs[1]) {
5094 blkno = bucket_blkno(xs->bucket);
5095 ocfs2_xattr_bucket_relse(xs->bucket);
5096 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
5103 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
5104 OCFS2_JOURNAL_ACCESS_WRITE);
5110 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
5111 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
5118 * Truncate the specified xe_off entry in xattr bucket.
5119 * bucket is indicated by header_bh and len is the new length.
5120 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5122 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5124 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5125 struct ocfs2_xattr_bucket *bucket,
5128 struct ocfs2_xattr_set_ctxt *ctxt)
5132 struct ocfs2_xattr_entry *xe;
5133 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5134 size_t blocksize = inode->i_sb->s_blocksize;
5135 struct ocfs2_xattr_value_buf vb = {
5136 .vb_access = ocfs2_journal_access,
5139 xe = &xh->xh_entries[xe_off];
5141 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5143 offset = le16_to_cpu(xe->xe_name_offset) +
5144 OCFS2_XATTR_SIZE(xe->xe_name_len);
5146 value_blk = offset / blocksize;
5148 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5149 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
5151 vb.vb_bh = bucket->bu_bhs[value_blk];
5154 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5155 (vb.vb_bh->b_data + offset % blocksize);
5158 * From here on out we have to dirty the bucket. The generic
5159 * value calls only modify one of the bucket's bhs, but we need
5160 * to send the bucket at once. So if they error, they *could* have
5161 * modified something. We have to assume they did, and dirty
5162 * the whole bucket. This leaves us in a consistent state.
5164 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5165 xe_off, (unsigned long long)bucket_blkno(bucket), len);
5166 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5172 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5173 OCFS2_JOURNAL_ACCESS_WRITE);
5179 xe->xe_value_size = cpu_to_le64(len);
5181 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5187 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
5188 struct ocfs2_xattr_search *xs,
5190 struct ocfs2_xattr_set_ctxt *ctxt)
5193 struct ocfs2_xattr_entry *xe = xs->here;
5194 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
5196 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
5198 offset = xe - xh->xh_entries;
5199 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
5207 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
5209 struct ocfs2_xattr_search *xs,
5213 int ret, offset, block_off;
5214 struct ocfs2_xattr_value_root *xv;
5215 struct ocfs2_xattr_entry *xe = xs->here;
5216 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5218 struct ocfs2_xattr_value_buf vb = {
5219 .vb_access = ocfs2_journal_access,
5222 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
5224 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
5225 xe - xh->xh_entries,
5233 base = bucket_block(xs->bucket, block_off);
5234 xv = (struct ocfs2_xattr_value_root *)(base + offset +
5235 OCFS2_XATTR_SIZE(xe->xe_name_len));
5238 vb.vb_bh = xs->bucket->bu_bhs[block_off];
5239 ret = __ocfs2_xattr_set_value_outside(inode, handle,
5240 &vb, val, value_len);
5247 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5248 struct buffer_head *root_bh,
5255 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5256 struct inode *tl_inode = osb->osb_tl_inode;
5258 struct ocfs2_xattr_block *xb =
5259 (struct ocfs2_xattr_block *)root_bh->b_data;
5260 struct ocfs2_alloc_context *meta_ac = NULL;
5261 struct ocfs2_cached_dealloc_ctxt dealloc;
5262 struct ocfs2_extent_tree et;
5264 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5265 ocfs2_delete_xattr_in_bucket, para);
5271 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5273 ocfs2_init_dealloc_ctxt(&dealloc);
5275 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5276 cpos, len, (unsigned long long)blkno);
5278 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5281 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5287 mutex_lock(&tl_inode->i_mutex);
5289 if (ocfs2_truncate_log_needs_flush(osb)) {
5290 ret = __ocfs2_flush_truncate_log(osb);
5297 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5298 if (IS_ERR(handle)) {
5304 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5305 OCFS2_JOURNAL_ACCESS_WRITE);
5311 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5318 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5320 ret = ocfs2_journal_dirty(handle, root_bh);
5326 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5331 ocfs2_commit_trans(osb, handle);
5333 ocfs2_schedule_truncate_log_flush(osb, 1);
5335 mutex_unlock(&tl_inode->i_mutex);
5338 ocfs2_free_alloc_context(meta_ac);
5340 ocfs2_run_deallocs(osb, &dealloc);
5345 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
5347 struct ocfs2_xattr_search *xs)
5349 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5350 struct ocfs2_xattr_entry *last = &xh->xh_entries[
5351 le16_to_cpu(xh->xh_count) - 1];
5354 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
5355 OCFS2_JOURNAL_ACCESS_WRITE);
5361 /* Remove the old entry. */
5362 memmove(xs->here, xs->here + 1,
5363 (void *)last - (void *)xs->here);
5364 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
5365 le16_add_cpu(&xh->xh_count, -1);
5367 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
5371 * Set the xattr name/value in the bucket specified in xs.
5373 * As the new value in xi may be stored in the bucket or in an outside cluster,
5374 * we divide the whole process into 3 steps:
5375 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
5376 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
5377 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
5378 * 4. If the clusters for the new outside value can't be allocated, we need
5379 * to free the xattr we allocated in set.
5381 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5382 struct ocfs2_xattr_info *xi,
5383 struct ocfs2_xattr_search *xs,
5384 struct ocfs2_xattr_set_ctxt *ctxt)
5388 char *val = (char *)xi->xi_value;
5389 struct ocfs2_xattr_entry *xe = xs->here;
5390 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
5393 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
5395 * We need to truncate the xattr storage first.
5397 * If both the old and new value are stored to
5398 * outside block, we only need to truncate
5399 * the storage and then set the value outside.
5401 * If the new value should be stored within block,
5402 * we should free all the outside block first and
5403 * the modification to the xattr block will be done
5404 * by following steps.
5406 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
5407 value_len = xi->xi_value_len;
5411 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5418 goto set_value_outside;
5421 value_len = xi->xi_value_len;
5422 /* So we have to handle the inside block change now. */
5423 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
5425 * If the new value will be stored outside of block,
5426 * initalize a new empty value root and insert it first.
5429 xi->xi_value = &def_xv;
5430 xi->xi_value_len = OCFS2_XATTR_ROOT_SIZE;
5433 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
5440 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
5443 /* allocate the space now for the outside block storage. */
5444 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5449 if (xs->not_found) {
5451 * We can't allocate enough clusters for outside
5452 * storage and we have allocated xattr already,
5453 * so need to remove it.
5455 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
5461 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
5462 xs, val, value_len);
5468 * check whether the xattr bucket is filled up with the same hash value.
5469 * If we want to insert the xattr with the same hash, return -ENOSPC.
5470 * If we want to insert a xattr with different hash value, go ahead
5471 * and ocfs2_divide_xattr_bucket will handle this.
5473 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5474 struct ocfs2_xattr_bucket *bucket,
5477 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5478 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5480 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5483 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5484 xh->xh_entries[0].xe_name_hash) {
5485 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5487 (unsigned long long)bucket_blkno(bucket),
5488 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5495 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5496 struct ocfs2_xattr_info *xi,
5497 struct ocfs2_xattr_search *xs,
5498 struct ocfs2_xattr_set_ctxt *ctxt)
5500 struct ocfs2_xattr_header *xh;
5501 struct ocfs2_xattr_entry *xe;
5502 u16 count, header_size, xh_free_start;
5503 int free, max_free, need, old;
5504 size_t value_size = 0;
5505 size_t blocksize = inode->i_sb->s_blocksize;
5506 int ret, allocation = 0;
5508 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
5512 count = le16_to_cpu(xh->xh_count);
5513 xh_free_start = le16_to_cpu(xh->xh_free_start);
5514 header_size = sizeof(struct ocfs2_xattr_header) +
5515 count * sizeof(struct ocfs2_xattr_entry);
5516 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5517 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
5519 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5520 "of %u which exceed block size\n",
5521 (unsigned long long)bucket_blkno(xs->bucket),
5524 if (xi->xi_value && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
5525 value_size = OCFS2_XATTR_ROOT_SIZE;
5526 else if (xi->xi_value)
5527 value_size = OCFS2_XATTR_SIZE(xi->xi_value_len);
5530 need = sizeof(struct ocfs2_xattr_entry) +
5531 OCFS2_XATTR_SIZE(xi->xi_name_len) + value_size;
5533 need = value_size + OCFS2_XATTR_SIZE(xi->xi_name_len);
5536 * We only replace the old value if the new length is smaller
5537 * than the old one. Otherwise we will allocate new space in the
5538 * bucket to store it.
5541 if (ocfs2_xattr_is_local(xe))
5542 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5544 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5546 if (old >= value_size)
5550 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
5552 * We need to make sure the new name/value pair
5553 * can exist in the same block.
5555 if (xh_free_start % blocksize < need)
5556 free -= xh_free_start % blocksize;
5558 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5559 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5560 " %u\n", xs->not_found,
5561 (unsigned long long)bucket_blkno(xs->bucket),
5562 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5563 le16_to_cpu(xh->xh_name_value_len));
5567 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
5568 if (need <= max_free &&
5569 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5571 * We can create the space by defragment. Since only the
5572 * name/value will be moved, the xe shouldn't be changed
5575 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5582 xh_free_start = le16_to_cpu(xh->xh_free_start);
5583 free = xh_free_start - header_size
5584 - OCFS2_XATTR_HEADER_GAP;
5585 if (xh_free_start % blocksize < need)
5586 free -= xh_free_start % blocksize;
5591 mlog(0, "Can't get enough space for xattr insert by "
5592 "defragment. Need %u bytes, but we have %d, so "
5593 "allocate new bucket for it.\n", need, free);
5597 * We have to add new buckets or clusters and one
5598 * allocation should leave us enough space for insert.
5603 * We do not allow for overlapping ranges between buckets. And
5604 * the maximum number of collisions we will allow for then is
5605 * one bucket's worth, so check it here whether we need to
5606 * add a new bucket for the insert.
5608 ret = ocfs2_check_xattr_bucket_collision(inode,
5616 ret = ocfs2_add_new_xattr_bucket(inode,
5626 * ocfs2_add_new_xattr_bucket() will have updated
5627 * xs->bucket if it moved, but it will not have updated
5628 * any of the other search fields. Thus, we drop it and
5629 * re-search. Everything should be cached, so it'll be
5632 ocfs2_xattr_bucket_relse(xs->bucket);
5633 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5636 if (ret && ret != -ENODATA)
5638 xs->not_found = ret;
5644 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
5650 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5651 struct ocfs2_xattr_bucket *bucket,
5654 int ret = 0, ref_credits;
5655 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5657 struct ocfs2_xattr_entry *xe;
5658 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5659 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5660 int credits = ocfs2_remove_extent_credits(osb->sb) +
5661 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5662 struct ocfs2_xattr_value_root *xv;
5663 struct ocfs2_rm_xattr_bucket_para *args =
5664 (struct ocfs2_rm_xattr_bucket_para *)para;
5666 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5668 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5669 xe = &xh->xh_entries[i];
5670 if (ocfs2_xattr_is_local(xe))
5673 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5676 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5682 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5683 if (IS_ERR(ctxt.handle)) {
5684 ret = PTR_ERR(ctxt.handle);
5689 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5692 ocfs2_commit_trans(osb, ctxt.handle);
5694 ocfs2_free_alloc_context(ctxt.meta_ac);
5695 ctxt.meta_ac = NULL;
5704 ocfs2_free_alloc_context(ctxt.meta_ac);
5705 ocfs2_schedule_truncate_log_flush(osb, 1);
5706 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5711 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5712 * or change the extent record flag), we need to recalculate
5713 * the metaecc for the whole bucket. So it is done here.
5716 * We have to give the extra credits for the caller.
5718 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5723 struct ocfs2_xattr_bucket *bucket =
5724 (struct ocfs2_xattr_bucket *)para;
5726 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5727 OCFS2_JOURNAL_ACCESS_WRITE);
5733 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5739 * Special action we need if the xattr value is refcounted.
5741 * 1. If the xattr is refcounted, lock the tree.
5742 * 2. CoW the xattr if we are setting the new value and the value
5743 * will be stored outside.
5744 * 3. In other case, decrease_refcount will work for us, so just
5745 * lock the refcount tree, calculate the meta and credits is OK.
5747 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5748 * currently CoW is a completed transaction, while this function
5749 * will also lock the allocators and let us deadlock. So we will
5750 * CoW the whole xattr value.
5752 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5753 struct ocfs2_dinode *di,
5754 struct ocfs2_xattr_info *xi,
5755 struct ocfs2_xattr_search *xis,
5756 struct ocfs2_xattr_search *xbs,
5757 struct ocfs2_refcount_tree **ref_tree,
5762 struct ocfs2_xattr_block *xb;
5763 struct ocfs2_xattr_entry *xe;
5765 u32 p_cluster, num_clusters;
5766 unsigned int ext_flags;
5767 int name_offset, name_len;
5768 struct ocfs2_xattr_value_buf vb;
5769 struct ocfs2_xattr_bucket *bucket = NULL;
5770 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5771 struct ocfs2_post_refcount refcount;
5772 struct ocfs2_post_refcount *p = NULL;
5773 struct buffer_head *ref_root_bh = NULL;
5775 if (!xis->not_found) {
5777 name_offset = le16_to_cpu(xe->xe_name_offset);
5778 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5780 vb.vb_bh = xis->inode_bh;
5781 vb.vb_access = ocfs2_journal_access_di;
5783 int i, block_off = 0;
5784 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5786 name_offset = le16_to_cpu(xe->xe_name_offset);
5787 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5788 i = xbs->here - xbs->header->xh_entries;
5790 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5791 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5792 bucket_xh(xbs->bucket),
5799 base = bucket_block(xbs->bucket, block_off);
5800 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5801 vb.vb_access = ocfs2_journal_access;
5803 if (ocfs2_meta_ecc(osb)) {
5804 /*create parameters for ocfs2_post_refcount. */
5805 bucket = xbs->bucket;
5806 refcount.credits = bucket->bu_blocks;
5807 refcount.para = bucket;
5809 ocfs2_xattr_bucket_post_refcount;
5814 vb.vb_bh = xbs->xattr_bh;
5815 vb.vb_access = ocfs2_journal_access_xb;
5819 if (ocfs2_xattr_is_local(xe))
5822 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5823 (base + name_offset + name_len);
5825 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5826 &num_clusters, &vb.vb_xv->xr_list,
5834 * We just need to check the 1st extent record, since we always
5835 * CoW the whole xattr. So there shouldn't be a xattr with
5836 * some REFCOUNT extent recs after the 1st one.
5838 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5841 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5842 1, ref_tree, &ref_root_bh);
5849 * If we are deleting the xattr or the new size will be stored inside,
5850 * cool, leave it there, the xattr truncate process will remove them
5851 * for us(it still needs the refcount tree lock and the meta, credits).
5852 * And the worse case is that every cluster truncate will split the
5853 * refcount tree, and make the original extent become 3. So we will need
5854 * 2 * cluster more extent recs at most.
5856 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
5858 ret = ocfs2_refcounted_xattr_delete_need(inode,
5859 &(*ref_tree)->rf_ci,
5860 ref_root_bh, vb.vb_xv,
5867 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5868 *ref_tree, ref_root_bh, 0,
5869 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5874 brelse(ref_root_bh);
5879 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5880 * The physical clusters will be added to refcount tree.
5882 static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5883 struct ocfs2_xattr_value_root *xv,
5884 struct ocfs2_extent_tree *value_et,
5885 struct ocfs2_caching_info *ref_ci,
5886 struct buffer_head *ref_root_bh,
5887 struct ocfs2_cached_dealloc_ctxt *dealloc,
5888 struct ocfs2_post_refcount *refcount)
5891 u32 clusters = le32_to_cpu(xv->xr_clusters);
5892 u32 cpos, p_cluster, num_clusters;
5893 struct ocfs2_extent_list *el = &xv->xr_list;
5894 unsigned int ext_flags;
5897 while (cpos < clusters) {
5898 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5899 &num_clusters, el, &ext_flags);
5901 cpos += num_clusters;
5902 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5907 ret = ocfs2_add_refcount_flag(inode, value_et,
5908 ref_ci, ref_root_bh,
5909 cpos - num_clusters,
5910 p_cluster, num_clusters,
5922 * Given a normal ocfs2_xattr_header, refcount all the entries which
5923 * have value stored outside.
5924 * Used for xattrs stored in inode and ocfs2_xattr_block.
5926 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5927 struct ocfs2_xattr_value_buf *vb,
5928 struct ocfs2_xattr_header *header,
5929 struct ocfs2_caching_info *ref_ci,
5930 struct buffer_head *ref_root_bh,
5931 struct ocfs2_cached_dealloc_ctxt *dealloc)
5934 struct ocfs2_xattr_entry *xe;
5935 struct ocfs2_xattr_value_root *xv;
5936 struct ocfs2_extent_tree et;
5939 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5940 xe = &header->xh_entries[i];
5942 if (ocfs2_xattr_is_local(xe))
5945 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5946 le16_to_cpu(xe->xe_name_offset) +
5947 OCFS2_XATTR_SIZE(xe->xe_name_len));
5950 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5952 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5953 ref_ci, ref_root_bh,
5964 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5965 struct buffer_head *fe_bh,
5966 struct ocfs2_caching_info *ref_ci,
5967 struct buffer_head *ref_root_bh,
5968 struct ocfs2_cached_dealloc_ctxt *dealloc)
5970 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5971 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5972 (fe_bh->b_data + inode->i_sb->s_blocksize -
5973 le16_to_cpu(di->i_xattr_inline_size));
5974 struct ocfs2_xattr_value_buf vb = {
5976 .vb_access = ocfs2_journal_access_di,
5979 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5980 ref_ci, ref_root_bh, dealloc);
5983 struct ocfs2_xattr_tree_value_refcount_para {
5984 struct ocfs2_caching_info *ref_ci;
5985 struct buffer_head *ref_root_bh;
5986 struct ocfs2_cached_dealloc_ctxt *dealloc;
5989 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5990 struct ocfs2_xattr_bucket *bucket,
5992 struct ocfs2_xattr_value_root **xv,
5993 struct buffer_head **bh)
5995 int ret, block_off, name_offset;
5996 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5997 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6000 ret = ocfs2_xattr_bucket_get_name_value(sb,
6010 base = bucket_block(bucket, block_off);
6012 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6013 OCFS2_XATTR_SIZE(xe->xe_name_len));
6016 *bh = bucket->bu_bhs[block_off];
6022 * For a given xattr bucket, refcount all the entries which
6023 * have value stored outside.
6025 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6026 struct ocfs2_xattr_bucket *bucket,
6030 struct ocfs2_extent_tree et;
6031 struct ocfs2_xattr_tree_value_refcount_para *ref =
6032 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6033 struct ocfs2_xattr_header *xh =
6034 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6035 struct ocfs2_xattr_entry *xe;
6036 struct ocfs2_xattr_value_buf vb = {
6037 .vb_access = ocfs2_journal_access,
6039 struct ocfs2_post_refcount refcount = {
6040 .credits = bucket->bu_blocks,
6042 .func = ocfs2_xattr_bucket_post_refcount,
6044 struct ocfs2_post_refcount *p = NULL;
6046 /* We only need post_refcount if we support metaecc. */
6047 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6050 mlog(0, "refcount bucket %llu, count = %u\n",
6051 (unsigned long long)bucket_blkno(bucket),
6052 le16_to_cpu(xh->xh_count));
6053 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6054 xe = &xh->xh_entries[i];
6056 if (ocfs2_xattr_is_local(xe))
6059 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6060 &vb.vb_xv, &vb.vb_bh);
6066 ocfs2_init_xattr_value_extent_tree(&et,
6067 INODE_CACHE(inode), &vb);
6069 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6083 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6084 struct buffer_head *root_bh,
6085 u64 blkno, u32 cpos, u32 len, void *para)
6087 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6088 ocfs2_xattr_bucket_value_refcount,
6092 static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6093 struct buffer_head *blk_bh,
6094 struct ocfs2_caching_info *ref_ci,
6095 struct buffer_head *ref_root_bh,
6096 struct ocfs2_cached_dealloc_ctxt *dealloc)
6099 struct ocfs2_xattr_block *xb =
6100 (struct ocfs2_xattr_block *)blk_bh->b_data;
6102 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6103 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6104 struct ocfs2_xattr_value_buf vb = {
6106 .vb_access = ocfs2_journal_access_xb,
6109 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6110 ref_ci, ref_root_bh,
6113 struct ocfs2_xattr_tree_value_refcount_para para = {
6115 .ref_root_bh = ref_root_bh,
6119 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6120 ocfs2_refcount_xattr_tree_rec,
6127 int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6128 struct buffer_head *fe_bh,
6129 struct ocfs2_caching_info *ref_ci,
6130 struct buffer_head *ref_root_bh,
6131 struct ocfs2_cached_dealloc_ctxt *dealloc)
6134 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6135 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6136 struct buffer_head *blk_bh = NULL;
6138 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6139 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6140 ref_ci, ref_root_bh,
6148 if (!di->i_xattr_loc)
6151 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6158 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6159 ref_root_bh, dealloc);
6169 typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
6171 * Store the information we need in xattr reflink.
6172 * old_bh and new_bh are inode bh for the old and new inode.
6174 struct ocfs2_xattr_reflink {
6175 struct inode *old_inode;
6176 struct inode *new_inode;
6177 struct buffer_head *old_bh;
6178 struct buffer_head *new_bh;
6179 struct ocfs2_caching_info *ref_ci;
6180 struct buffer_head *ref_root_bh;
6181 struct ocfs2_cached_dealloc_ctxt *dealloc;
6182 should_xattr_reflinked *xattr_reflinked;
6186 * Given a xattr header and xe offset,
6187 * return the proper xv and the corresponding bh.
6188 * xattr in inode, block and xattr tree have different implementaions.
6190 typedef int (get_xattr_value_root)(struct super_block *sb,
6191 struct buffer_head *bh,
6192 struct ocfs2_xattr_header *xh,
6194 struct ocfs2_xattr_value_root **xv,
6195 struct buffer_head **ret_bh,
6199 * Calculate all the xattr value root metadata stored in this xattr header and
6200 * credits we need if we create them from the scratch.
6201 * We use get_xattr_value_root so that all types of xattr container can use it.
6203 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6204 struct buffer_head *bh,
6205 struct ocfs2_xattr_header *xh,
6206 int *metas, int *credits,
6208 get_xattr_value_root *func,
6212 struct ocfs2_xattr_value_root *xv;
6213 struct ocfs2_xattr_entry *xe;
6215 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6216 xe = &xh->xh_entries[i];
6217 if (ocfs2_xattr_is_local(xe))
6220 ret = func(sb, bh, xh, i, &xv, NULL, para);
6226 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6227 le16_to_cpu(xv->xr_list.l_next_free_rec);
6229 *credits += ocfs2_calc_extend_credits(sb,
6231 le32_to_cpu(xv->xr_clusters));
6234 * If the value is a tree with depth > 1, We don't go deep
6235 * to the extent block, so just calculate a maximum record num.
6237 if (!xv->xr_list.l_tree_depth)
6238 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
6240 *num_recs += ocfs2_clusters_for_bytes(sb,
6247 /* Used by xattr inode and block to return the right xv and buffer_head. */
6248 static int ocfs2_get_xattr_value_root(struct super_block *sb,
6249 struct buffer_head *bh,
6250 struct ocfs2_xattr_header *xh,
6252 struct ocfs2_xattr_value_root **xv,
6253 struct buffer_head **ret_bh,
6256 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6258 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6259 le16_to_cpu(xe->xe_name_offset) +
6260 OCFS2_XATTR_SIZE(xe->xe_name_len));
6269 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6270 * It is only used for inline xattr and xattr block.
6272 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6273 struct ocfs2_xattr_header *xh,
6274 struct buffer_head *ref_root_bh,
6276 struct ocfs2_alloc_context **meta_ac)
6278 int ret, meta_add = 0, num_recs = 0;
6279 struct ocfs2_refcount_block *rb =
6280 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6284 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6285 &meta_add, credits, &num_recs,
6286 ocfs2_get_xattr_value_root,
6294 * We need to add/modify num_recs in refcount tree, so just calculate
6295 * an approximate number we need for refcount tree change.
6296 * Sometimes we need to split the tree, and after split, half recs
6297 * will be moved to the new block, and a new block can only provide
6298 * half number of recs. So we multiple new blocks by 2.
6300 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6301 meta_add += num_recs;
6302 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6303 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6304 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6305 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6309 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6318 * Given a xattr header, reflink all the xattrs in this container.
6319 * It can be used for inode, block and bucket.
6322 * Before we call this function, the caller has memcpy the xattr in
6323 * old_xh to the new_xh.
6325 * If args.xattr_reflinked is set, call it to decide whether the xe should
6326 * be reflinked or not. If not, remove it from the new xattr header.
6328 static int ocfs2_reflink_xattr_header(handle_t *handle,
6329 struct ocfs2_xattr_reflink *args,
6330 struct buffer_head *old_bh,
6331 struct ocfs2_xattr_header *xh,
6332 struct buffer_head *new_bh,
6333 struct ocfs2_xattr_header *new_xh,
6334 struct ocfs2_xattr_value_buf *vb,
6335 struct ocfs2_alloc_context *meta_ac,
6336 get_xattr_value_root *func,
6340 struct super_block *sb = args->old_inode->i_sb;
6341 struct buffer_head *value_bh;
6342 struct ocfs2_xattr_entry *xe, *last;
6343 struct ocfs2_xattr_value_root *xv, *new_xv;
6344 struct ocfs2_extent_tree data_et;
6345 u32 clusters, cpos, p_cluster, num_clusters;
6346 unsigned int ext_flags = 0;
6348 mlog(0, "reflink xattr in container %llu, count = %u\n",
6349 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
6351 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6352 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
6353 xe = &xh->xh_entries[i];
6355 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6356 xe = &new_xh->xh_entries[j];
6358 le16_add_cpu(&new_xh->xh_count, -1);
6359 if (new_xh->xh_count) {
6361 (void *)last - (void *)xe);
6363 sizeof(struct ocfs2_xattr_entry));
6367 * We don't want j to increase in the next round since
6368 * it is already moved ahead.
6374 if (ocfs2_xattr_is_local(xe))
6377 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6383 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6390 * For the xattr which has l_tree_depth = 0, all the extent
6391 * recs have already be copied to the new xh with the
6392 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6393 * increase the refount count int the refcount tree.
6395 * For the xattr which has l_tree_depth > 0, we need
6396 * to initialize it to the empty default value root,
6397 * and then insert the extents one by one.
6399 if (xv->xr_list.l_tree_depth) {
6400 memcpy(new_xv, &def_xv, sizeof(def_xv));
6402 vb->vb_bh = value_bh;
6403 ocfs2_init_xattr_value_extent_tree(&data_et,
6404 INODE_CACHE(args->new_inode), vb);
6407 clusters = le32_to_cpu(xv->xr_clusters);
6409 while (cpos < clusters) {
6410 ret = ocfs2_xattr_get_clusters(args->old_inode,
6423 if (xv->xr_list.l_tree_depth) {
6424 ret = ocfs2_insert_extent(handle,
6426 ocfs2_clusters_to_blocks(
6427 args->old_inode->i_sb,
6429 num_clusters, ext_flags,
6437 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6439 p_cluster, num_clusters,
6440 meta_ac, args->dealloc);
6446 cpos += num_clusters;
6454 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6456 int ret = 0, credits = 0;
6458 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6459 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6460 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6461 int header_off = osb->sb->s_blocksize - inline_size;
6462 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6463 (args->old_bh->b_data + header_off);
6464 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6465 (args->new_bh->b_data + header_off);
6466 struct ocfs2_alloc_context *meta_ac = NULL;
6467 struct ocfs2_inode_info *new_oi;
6468 struct ocfs2_dinode *new_di;
6469 struct ocfs2_xattr_value_buf vb = {
6470 .vb_bh = args->new_bh,
6471 .vb_access = ocfs2_journal_access_di,
6474 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6475 &credits, &meta_ac);
6481 handle = ocfs2_start_trans(osb, credits);
6482 if (IS_ERR(handle)) {
6483 ret = PTR_ERR(handle);
6488 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6489 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6495 memcpy(args->new_bh->b_data + header_off,
6496 args->old_bh->b_data + header_off, inline_size);
6498 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6499 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6501 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6502 args->new_bh, new_xh, &vb, meta_ac,
6503 ocfs2_get_xattr_value_root, NULL);
6509 new_oi = OCFS2_I(args->new_inode);
6510 spin_lock(&new_oi->ip_lock);
6511 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6512 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6513 spin_unlock(&new_oi->ip_lock);
6515 ocfs2_journal_dirty(handle, args->new_bh);
6518 ocfs2_commit_trans(osb, handle);
6522 ocfs2_free_alloc_context(meta_ac);
6526 static int ocfs2_create_empty_xattr_block(struct inode *inode,
6527 struct buffer_head *fe_bh,
6528 struct buffer_head **ret_bh,
6533 struct ocfs2_alloc_context *meta_ac;
6534 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6536 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
6542 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6543 if (IS_ERR(handle)) {
6544 ret = PTR_ERR(handle);
6549 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6550 (unsigned long long)fe_bh->b_blocknr, indexed);
6551 ret = ocfs2_create_xattr_block(handle, inode, fe_bh,
6552 meta_ac, ret_bh, indexed);
6556 ocfs2_commit_trans(osb, handle);
6558 ocfs2_free_alloc_context(meta_ac);
6562 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6563 struct buffer_head *blk_bh,
6564 struct buffer_head *new_blk_bh)
6566 int ret = 0, credits = 0;
6568 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6569 struct ocfs2_dinode *new_di;
6570 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6571 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6572 struct ocfs2_xattr_block *xb =
6573 (struct ocfs2_xattr_block *)blk_bh->b_data;
6574 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6575 struct ocfs2_xattr_block *new_xb =
6576 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6577 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6578 struct ocfs2_alloc_context *meta_ac;
6579 struct ocfs2_xattr_value_buf vb = {
6580 .vb_bh = new_blk_bh,
6581 .vb_access = ocfs2_journal_access_xb,
6584 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6585 &credits, &meta_ac);
6591 /* One more credits in case we need to add xattr flags in new inode. */
6592 handle = ocfs2_start_trans(osb, credits + 1);
6593 if (IS_ERR(handle)) {
6594 ret = PTR_ERR(handle);
6599 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6600 ret = ocfs2_journal_access_di(handle,
6601 INODE_CACHE(args->new_inode),
6603 OCFS2_JOURNAL_ACCESS_WRITE);
6610 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6611 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6617 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6618 osb->sb->s_blocksize - header_off);
6620 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6621 new_blk_bh, new_xh, &vb, meta_ac,
6622 ocfs2_get_xattr_value_root, NULL);
6628 ocfs2_journal_dirty(handle, new_blk_bh);
6630 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6631 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6632 spin_lock(&new_oi->ip_lock);
6633 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6634 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6635 spin_unlock(&new_oi->ip_lock);
6637 ocfs2_journal_dirty(handle, args->new_bh);
6641 ocfs2_commit_trans(osb, handle);
6644 ocfs2_free_alloc_context(meta_ac);
6648 struct ocfs2_reflink_xattr_tree_args {
6649 struct ocfs2_xattr_reflink *reflink;
6650 struct buffer_head *old_blk_bh;
6651 struct buffer_head *new_blk_bh;
6652 struct ocfs2_xattr_bucket *old_bucket;
6653 struct ocfs2_xattr_bucket *new_bucket;
6658 * We have to handle the case that both old bucket and new bucket
6659 * will call this function to get the right ret_bh.
6660 * So The caller must give us the right bh.
6662 static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6663 struct buffer_head *bh,
6664 struct ocfs2_xattr_header *xh,
6666 struct ocfs2_xattr_value_root **xv,
6667 struct buffer_head **ret_bh,
6670 struct ocfs2_reflink_xattr_tree_args *args =
6671 (struct ocfs2_reflink_xattr_tree_args *)para;
6672 struct ocfs2_xattr_bucket *bucket;
6674 if (bh == args->old_bucket->bu_bhs[0])
6675 bucket = args->old_bucket;
6677 bucket = args->new_bucket;
6679 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6683 struct ocfs2_value_tree_metas {
6689 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6690 struct buffer_head *bh,
6691 struct ocfs2_xattr_header *xh,
6693 struct ocfs2_xattr_value_root **xv,
6694 struct buffer_head **ret_bh,
6697 struct ocfs2_xattr_bucket *bucket =
6698 (struct ocfs2_xattr_bucket *)para;
6700 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6704 static int ocfs2_calc_value_tree_metas(struct inode *inode,
6705 struct ocfs2_xattr_bucket *bucket,
6708 struct ocfs2_value_tree_metas *metas =
6709 (struct ocfs2_value_tree_metas *)para;
6710 struct ocfs2_xattr_header *xh =
6711 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6713 /* Add the credits for this bucket first. */
6714 metas->credits += bucket->bu_blocks;
6715 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6716 xh, &metas->num_metas,
6717 &metas->credits, &metas->num_recs,
6718 ocfs2_value_tree_metas_in_bucket,
6723 * Given a xattr extent rec starting from blkno and having len clusters,
6724 * iterate all the buckets calculate how much metadata we need for reflinking
6725 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6727 static int ocfs2_lock_reflink_xattr_rec_allocators(
6728 struct ocfs2_reflink_xattr_tree_args *args,
6729 struct ocfs2_extent_tree *xt_et,
6730 u64 blkno, u32 len, int *credits,
6731 struct ocfs2_alloc_context **meta_ac,
6732 struct ocfs2_alloc_context **data_ac)
6734 int ret, num_free_extents;
6735 struct ocfs2_value_tree_metas metas;
6736 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6737 struct ocfs2_refcount_block *rb;
6739 memset(&metas, 0, sizeof(metas));
6741 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6742 ocfs2_calc_value_tree_metas, &metas);
6748 *credits = metas.credits;
6751 * Calculate we need for refcount tree change.
6753 * We need to add/modify num_recs in refcount tree, so just calculate
6754 * an approximate number we need for refcount tree change.
6755 * Sometimes we need to split the tree, and after split, half recs
6756 * will be moved to the new block, and a new block can only provide
6757 * half number of recs. So we multiple new blocks by 2.
6758 * In the end, we have to add credits for modifying the already
6759 * existed refcount block.
6761 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6763 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6764 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6765 metas.num_metas += metas.num_recs;
6766 *credits += metas.num_recs +
6767 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6768 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6769 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6770 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6774 /* count in the xattr tree change. */
6775 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6776 if (num_free_extents < 0) {
6777 ret = num_free_extents;
6782 if (num_free_extents < len)
6783 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6785 *credits += ocfs2_calc_extend_credits(osb->sb,
6786 xt_et->et_root_el, len);
6788 if (metas.num_metas) {
6789 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6798 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6805 ocfs2_free_alloc_context(*meta_ac);
6813 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6814 u64 blkno, u64 new_blkno, u32 clusters,
6815 struct ocfs2_alloc_context *meta_ac,
6816 struct ocfs2_alloc_context *data_ac,
6817 struct ocfs2_reflink_xattr_tree_args *args)
6820 struct super_block *sb = args->reflink->old_inode->i_sb;
6821 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6822 u32 num_buckets = clusters * bpc;
6823 int bpb = args->old_bucket->bu_blocks;
6824 struct ocfs2_xattr_value_buf vb = {
6825 .vb_access = ocfs2_journal_access,
6828 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6829 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6835 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6842 * The real bucket num in this series of blocks is stored
6843 * in the 1st bucket.
6846 num_buckets = le16_to_cpu(
6847 bucket_xh(args->old_bucket)->xh_num_buckets);
6849 ret = ocfs2_xattr_bucket_journal_access(handle,
6851 OCFS2_JOURNAL_ACCESS_CREATE);
6857 for (j = 0; j < bpb; j++)
6858 memcpy(bucket_block(args->new_bucket, j),
6859 bucket_block(args->old_bucket, j),
6862 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6864 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6865 args->old_bucket->bu_bhs[0],
6866 bucket_xh(args->old_bucket),
6867 args->new_bucket->bu_bhs[0],
6868 bucket_xh(args->new_bucket),
6870 ocfs2_get_reflink_xattr_value_root,
6878 * Re-access and dirty the bucket to calculate metaecc.
6879 * Because we may extend the transaction in reflink_xattr_header
6880 * which will let the already accessed block gone.
6882 ret = ocfs2_xattr_bucket_journal_access(handle,
6884 OCFS2_JOURNAL_ACCESS_WRITE);
6890 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6891 ocfs2_xattr_bucket_relse(args->old_bucket);
6892 ocfs2_xattr_bucket_relse(args->new_bucket);
6895 ocfs2_xattr_bucket_relse(args->old_bucket);
6896 ocfs2_xattr_bucket_relse(args->new_bucket);
6900 * Create the same xattr extent record in the new inode's xattr tree.
6902 static int ocfs2_reflink_xattr_rec(struct inode *inode,
6903 struct buffer_head *root_bh,
6909 int ret, credits = 0;
6910 u32 p_cluster, num_clusters;
6913 struct ocfs2_reflink_xattr_tree_args *args =
6914 (struct ocfs2_reflink_xattr_tree_args *)para;
6915 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6916 struct ocfs2_alloc_context *meta_ac = NULL;
6917 struct ocfs2_alloc_context *data_ac = NULL;
6918 struct ocfs2_extent_tree et;
6920 ocfs2_init_xattr_tree_extent_tree(&et,
6921 INODE_CACHE(args->reflink->new_inode),
6924 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6926 &meta_ac, &data_ac);
6932 handle = ocfs2_start_trans(osb, credits);
6933 if (IS_ERR(handle)) {
6934 ret = PTR_ERR(handle);
6939 ret = ocfs2_claim_clusters(osb, handle, data_ac,
6940 len, &p_cluster, &num_clusters);
6946 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6948 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6949 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6950 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6951 meta_ac, data_ac, args);
6957 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6958 (unsigned long long)new_blkno, len, cpos);
6959 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6965 ocfs2_commit_trans(osb, handle);
6969 ocfs2_free_alloc_context(meta_ac);
6971 ocfs2_free_alloc_context(data_ac);
6976 * Create reflinked xattr buckets.
6977 * We will add bucket one by one, and refcount all the xattrs in the bucket
6978 * if they are stored outside.
6980 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6981 struct buffer_head *blk_bh,
6982 struct buffer_head *new_blk_bh)
6985 struct ocfs2_reflink_xattr_tree_args para;
6987 memset(¶, 0, sizeof(para));
6988 para.reflink = args;
6989 para.old_blk_bh = blk_bh;
6990 para.new_blk_bh = new_blk_bh;
6992 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
6993 if (!para.old_bucket) {
6994 mlog_errno(-ENOMEM);
6998 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
6999 if (!para.new_bucket) {
7005 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
7006 ocfs2_reflink_xattr_rec,
7012 ocfs2_xattr_bucket_free(para.old_bucket);
7013 ocfs2_xattr_bucket_free(para.new_bucket);
7017 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7018 struct buffer_head *blk_bh)
7020 int ret, indexed = 0;
7021 struct buffer_head *new_blk_bh = NULL;
7022 struct ocfs2_xattr_block *xb =
7023 (struct ocfs2_xattr_block *)blk_bh->b_data;
7026 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7029 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7030 &new_blk_bh, indexed);
7036 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7037 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7039 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7048 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7050 int type = ocfs2_xattr_get_type(xe);
7052 return type != OCFS2_XATTR_INDEX_SECURITY &&
7053 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7054 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7057 int ocfs2_reflink_xattrs(struct inode *old_inode,
7058 struct buffer_head *old_bh,
7059 struct inode *new_inode,
7060 struct buffer_head *new_bh,
7061 bool preserve_security)
7064 struct ocfs2_xattr_reflink args;
7065 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7066 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7067 struct buffer_head *blk_bh = NULL;
7068 struct ocfs2_cached_dealloc_ctxt dealloc;
7069 struct ocfs2_refcount_tree *ref_tree;
7070 struct buffer_head *ref_root_bh = NULL;
7072 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7073 le64_to_cpu(di->i_refcount_loc),
7074 1, &ref_tree, &ref_root_bh);
7080 ocfs2_init_dealloc_ctxt(&dealloc);
7082 args.old_inode = old_inode;
7083 args.new_inode = new_inode;
7084 args.old_bh = old_bh;
7085 args.new_bh = new_bh;
7086 args.ref_ci = &ref_tree->rf_ci;
7087 args.ref_root_bh = ref_root_bh;
7088 args.dealloc = &dealloc;
7089 if (preserve_security)
7090 args.xattr_reflinked = NULL;
7092 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
7094 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7095 ret = ocfs2_reflink_xattr_inline(&args);
7102 if (!di->i_xattr_loc)
7105 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7112 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7119 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7121 brelse(ref_root_bh);
7123 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7124 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7125 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7133 * Initialize security and acl for a already created inode.
7134 * Used for reflink a non-preserve-security file.
7136 * It uses common api like ocfs2_xattr_set, so the caller
7137 * must not hold any lock expect i_mutex.
7139 int ocfs2_init_security_and_acl(struct inode *dir,
7140 struct inode *inode)
7143 struct buffer_head *dir_bh = NULL;
7144 struct ocfs2_security_xattr_info si = {
7148 ret = ocfs2_init_security_get(inode, dir, &si);
7150 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7151 si.name, si.value, si.value_len,
7157 } else if (ret != -EOPNOTSUPP) {
7162 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7168 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7172 ocfs2_inode_unlock(dir, 0);
7178 * 'security' attributes support
7180 static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
7181 size_t list_size, const char *name,
7182 size_t name_len, int type)
7184 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7185 const size_t total_len = prefix_len + name_len + 1;
7187 if (list && total_len <= list_size) {
7188 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7189 memcpy(list + prefix_len, name, name_len);
7190 list[prefix_len + name_len] = '\0';
7195 static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7196 void *buffer, size_t size, int type)
7198 if (strcmp(name, "") == 0)
7200 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7201 name, buffer, size);
7204 static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7205 const void *value, size_t size, int flags, int type)
7207 if (strcmp(name, "") == 0)
7210 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7211 name, value, size, flags);
7214 int ocfs2_init_security_get(struct inode *inode,
7216 struct ocfs2_security_xattr_info *si)
7218 /* check whether ocfs2 support feature xattr */
7219 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7221 return security_inode_init_security(inode, dir, &si->name, &si->value,
7225 int ocfs2_init_security_set(handle_t *handle,
7226 struct inode *inode,
7227 struct buffer_head *di_bh,
7228 struct ocfs2_security_xattr_info *si,
7229 struct ocfs2_alloc_context *xattr_ac,
7230 struct ocfs2_alloc_context *data_ac)
7232 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7233 OCFS2_XATTR_INDEX_SECURITY,
7234 si->name, si->value, si->value_len, 0,
7238 struct xattr_handler ocfs2_xattr_security_handler = {
7239 .prefix = XATTR_SECURITY_PREFIX,
7240 .list = ocfs2_xattr_security_list,
7241 .get = ocfs2_xattr_security_get,
7242 .set = ocfs2_xattr_security_set,
7246 * 'trusted' attributes support
7248 static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
7249 size_t list_size, const char *name,
7250 size_t name_len, int type)
7252 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
7253 const size_t total_len = prefix_len + name_len + 1;
7255 if (list && total_len <= list_size) {
7256 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7257 memcpy(list + prefix_len, name, name_len);
7258 list[prefix_len + name_len] = '\0';
7263 static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7264 void *buffer, size_t size, int type)
7266 if (strcmp(name, "") == 0)
7268 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7269 name, buffer, size);
7272 static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7273 const void *value, size_t size, int flags, int type)
7275 if (strcmp(name, "") == 0)
7278 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7279 name, value, size, flags);
7282 struct xattr_handler ocfs2_xattr_trusted_handler = {
7283 .prefix = XATTR_TRUSTED_PREFIX,
7284 .list = ocfs2_xattr_trusted_list,
7285 .get = ocfs2_xattr_trusted_get,
7286 .set = ocfs2_xattr_trusted_set,
7290 * 'user' attributes support
7292 static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
7293 size_t list_size, const char *name,
7294 size_t name_len, int type)
7296 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7297 const size_t total_len = prefix_len + name_len + 1;
7298 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7300 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7303 if (list && total_len <= list_size) {
7304 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7305 memcpy(list + prefix_len, name, name_len);
7306 list[prefix_len + name_len] = '\0';
7311 static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7312 void *buffer, size_t size, int type)
7314 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7316 if (strcmp(name, "") == 0)
7318 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7320 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
7324 static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7325 const void *value, size_t size, int flags, int type)
7327 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7329 if (strcmp(name, "") == 0)
7331 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7334 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7335 name, value, size, flags);
7338 struct xattr_handler ocfs2_xattr_user_handler = {
7339 .prefix = XATTR_USER_PREFIX,
7340 .list = ocfs2_xattr_user_list,
7341 .get = ocfs2_xattr_user_get,
7342 .set = ocfs2_xattr_user_set,