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"
60 struct ocfs2_xattr_def_value_root {
61 struct ocfs2_xattr_value_root xv;
62 struct ocfs2_extent_rec er;
65 struct ocfs2_xattr_bucket {
66 /* The inode these xattrs are associated with */
67 struct inode *bu_inode;
69 /* The actual buffers that make up the bucket */
70 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
72 /* How many blocks make up one bucket for this filesystem */
76 struct ocfs2_xattr_set_ctxt {
78 struct ocfs2_alloc_context *meta_ac;
79 struct ocfs2_alloc_context *data_ac;
80 struct ocfs2_cached_dealloc_ctxt dealloc;
83 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
84 #define OCFS2_XATTR_INLINE_SIZE 80
85 #define OCFS2_XATTR_HEADER_GAP 4
86 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
87 - sizeof(struct ocfs2_xattr_header) \
88 - OCFS2_XATTR_HEADER_GAP)
89 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
90 - sizeof(struct ocfs2_xattr_block) \
91 - sizeof(struct ocfs2_xattr_header) \
92 - OCFS2_XATTR_HEADER_GAP)
94 static struct ocfs2_xattr_def_value_root def_xv = {
95 .xv.xr_list.l_count = cpu_to_le16(1),
98 struct xattr_handler *ocfs2_xattr_handlers[] = {
99 &ocfs2_xattr_user_handler,
100 #ifdef CONFIG_OCFS2_FS_POSIX_ACL
101 &ocfs2_xattr_acl_access_handler,
102 &ocfs2_xattr_acl_default_handler,
104 &ocfs2_xattr_trusted_handler,
105 &ocfs2_xattr_security_handler,
109 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
110 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
111 #ifdef CONFIG_OCFS2_FS_POSIX_ACL
112 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
113 = &ocfs2_xattr_acl_access_handler,
114 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
115 = &ocfs2_xattr_acl_default_handler,
117 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
118 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
121 struct ocfs2_xattr_info {
128 struct ocfs2_xattr_search {
129 struct buffer_head *inode_bh;
131 * xattr_bh point to the block buffer head which has extended attribute
132 * when extended attribute in inode, xattr_bh is equal to inode_bh.
134 struct buffer_head *xattr_bh;
135 struct ocfs2_xattr_header *header;
136 struct ocfs2_xattr_bucket *bucket;
139 struct ocfs2_xattr_entry *here;
143 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
144 struct ocfs2_xattr_header *xh,
149 static int ocfs2_xattr_block_find(struct inode *inode,
152 struct ocfs2_xattr_search *xs);
153 static int ocfs2_xattr_index_block_find(struct inode *inode,
154 struct buffer_head *root_bh,
157 struct ocfs2_xattr_search *xs);
159 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
160 struct buffer_head *blk_bh,
164 static int ocfs2_xattr_create_index_block(struct inode *inode,
165 struct ocfs2_xattr_search *xs,
166 struct ocfs2_xattr_set_ctxt *ctxt);
168 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
169 struct ocfs2_xattr_info *xi,
170 struct ocfs2_xattr_search *xs,
171 struct ocfs2_xattr_set_ctxt *ctxt);
173 typedef int (xattr_tree_rec_func)(struct inode *inode,
174 struct buffer_head *root_bh,
175 u64 blkno, u32 cpos, u32 len, void *para);
176 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
177 struct buffer_head *root_bh,
178 xattr_tree_rec_func *rec_func,
180 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
181 struct ocfs2_xattr_bucket *bucket,
183 static int ocfs2_rm_xattr_cluster(struct inode *inode,
184 struct buffer_head *root_bh,
190 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
191 u64 src_blk, u64 last_blk, u64 to_blk,
192 unsigned int start_bucket,
194 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
195 struct ocfs2_dinode *di,
196 struct ocfs2_xattr_info *xi,
197 struct ocfs2_xattr_search *xis,
198 struct ocfs2_xattr_search *xbs,
199 struct ocfs2_refcount_tree **ref_tree,
202 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
203 struct ocfs2_xattr_bucket *bucket,
205 struct ocfs2_xattr_value_root **xv,
206 struct buffer_head **bh);
208 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
210 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
213 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
215 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
218 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
220 u16 len = sb->s_blocksize -
221 offsetof(struct ocfs2_xattr_header, xh_entries);
223 return len / sizeof(struct ocfs2_xattr_entry);
226 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
227 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
228 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
230 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
232 struct ocfs2_xattr_bucket *bucket;
233 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
235 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
237 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
239 bucket->bu_inode = inode;
240 bucket->bu_blocks = blks;
246 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
250 for (i = 0; i < bucket->bu_blocks; i++) {
251 brelse(bucket->bu_bhs[i]);
252 bucket->bu_bhs[i] = NULL;
256 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
259 ocfs2_xattr_bucket_relse(bucket);
260 bucket->bu_inode = NULL;
266 * A bucket that has never been written to disk doesn't need to be
267 * read. We just need the buffer_heads. Don't call this for
268 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
271 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
276 for (i = 0; i < bucket->bu_blocks; i++) {
277 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
279 if (!bucket->bu_bhs[i]) {
285 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
287 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
292 ocfs2_xattr_bucket_relse(bucket);
296 /* Read the xattr bucket at xb_blkno */
297 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
302 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
303 bucket->bu_blocks, bucket->bu_bhs, 0,
306 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
307 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
310 &bucket_xh(bucket)->xh_check);
311 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
317 ocfs2_xattr_bucket_relse(bucket);
321 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
322 struct ocfs2_xattr_bucket *bucket,
327 for (i = 0; i < bucket->bu_blocks; i++) {
328 rc = ocfs2_journal_access(handle,
329 INODE_CACHE(bucket->bu_inode),
330 bucket->bu_bhs[i], type);
340 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
341 struct ocfs2_xattr_bucket *bucket)
345 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
346 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
347 bucket->bu_bhs, bucket->bu_blocks,
348 &bucket_xh(bucket)->xh_check);
349 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
351 for (i = 0; i < bucket->bu_blocks; i++)
352 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
355 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
356 struct ocfs2_xattr_bucket *src)
359 int blocksize = src->bu_inode->i_sb->s_blocksize;
361 BUG_ON(dest->bu_blocks != src->bu_blocks);
362 BUG_ON(dest->bu_inode != src->bu_inode);
364 for (i = 0; i < src->bu_blocks; i++) {
365 memcpy(bucket_block(dest, i), bucket_block(src, i),
370 static int ocfs2_validate_xattr_block(struct super_block *sb,
371 struct buffer_head *bh)
374 struct ocfs2_xattr_block *xb =
375 (struct ocfs2_xattr_block *)bh->b_data;
377 mlog(0, "Validating xattr block %llu\n",
378 (unsigned long long)bh->b_blocknr);
380 BUG_ON(!buffer_uptodate(bh));
383 * If the ecc fails, we return the error but otherwise
384 * leave the filesystem running. We know any error is
385 * local to this block.
387 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
392 * Errors after here are fatal
395 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
397 "Extended attribute block #%llu has bad "
399 (unsigned long long)bh->b_blocknr, 7,
404 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
406 "Extended attribute block #%llu has an "
407 "invalid xb_blkno of %llu",
408 (unsigned long long)bh->b_blocknr,
409 (unsigned long long)le64_to_cpu(xb->xb_blkno));
413 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
415 "Extended attribute block #%llu has an invalid "
416 "xb_fs_generation of #%u",
417 (unsigned long long)bh->b_blocknr,
418 le32_to_cpu(xb->xb_fs_generation));
425 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
426 struct buffer_head **bh)
429 struct buffer_head *tmp = *bh;
431 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
432 ocfs2_validate_xattr_block);
434 /* If ocfs2_read_block() got us a new bh, pass it up. */
441 static inline const char *ocfs2_xattr_prefix(int name_index)
443 struct xattr_handler *handler = NULL;
445 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
446 handler = ocfs2_xattr_handler_map[name_index];
448 return handler ? handler->prefix : NULL;
451 static u32 ocfs2_xattr_name_hash(struct inode *inode,
455 /* Get hash value of uuid from super block */
456 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
459 /* hash extended attribute name */
460 for (i = 0; i < name_len; i++) {
461 hash = (hash << OCFS2_HASH_SHIFT) ^
462 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
470 * ocfs2_xattr_hash_entry()
472 * Compute the hash of an extended attribute.
474 static void ocfs2_xattr_hash_entry(struct inode *inode,
475 struct ocfs2_xattr_header *header,
476 struct ocfs2_xattr_entry *entry)
479 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
481 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
482 entry->xe_name_hash = cpu_to_le32(hash);
487 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
491 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
492 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
494 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
495 size += sizeof(struct ocfs2_xattr_entry);
500 int ocfs2_calc_security_init(struct inode *dir,
501 struct ocfs2_security_xattr_info *si,
504 struct ocfs2_alloc_context **xattr_ac)
507 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
508 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
512 * The max space of security xattr taken inline is
513 * 256(name) + 80(value) + 16(entry) = 352 bytes,
514 * So reserve one metadata block for it is ok.
516 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
517 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
518 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
523 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
526 /* reserve clusters for xattr value which will be set in B tree*/
527 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
528 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
531 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
533 *want_clusters += new_clusters;
538 int ocfs2_calc_xattr_init(struct inode *dir,
539 struct buffer_head *dir_bh,
541 struct ocfs2_security_xattr_info *si,
547 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
548 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
551 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
554 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
555 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
556 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
559 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
562 } else if (acl_len != 0 && acl_len != -ENODATA) {
568 if (!(s_size + a_size))
572 * The max space of security xattr taken inline is
573 * 256(name) + 80(value) + 16(entry) = 352 bytes,
574 * The max space of acl xattr taken inline is
575 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
576 * when blocksize = 512, may reserve one more cluser for
577 * xattr bucket, otherwise reserve one metadata block
579 * If this is a new directory with inline data,
580 * we choose to reserve the entire inline area for
581 * directory contents and force an external xattr block.
583 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
584 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
585 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
586 *want_meta = *want_meta + 1;
587 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
590 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
591 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
593 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
597 * reserve credits and clusters for xattrs which has large value
598 * and have to be set outside
600 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
601 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
603 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
605 *want_clusters += new_clusters;
607 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
608 acl_len > OCFS2_XATTR_INLINE_SIZE) {
609 /* for directory, it has DEFAULT and ACCESS two types of acls */
610 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
611 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
612 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
614 *want_clusters += new_clusters;
620 static int ocfs2_xattr_extend_allocation(struct inode *inode,
622 struct ocfs2_xattr_value_buf *vb,
623 struct ocfs2_xattr_set_ctxt *ctxt)
626 handle_t *handle = ctxt->handle;
627 enum ocfs2_alloc_restarted why;
628 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
629 struct ocfs2_extent_tree et;
631 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
633 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
635 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
636 OCFS2_JOURNAL_ACCESS_WRITE);
642 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
643 status = ocfs2_add_clusters_in_btree(handle,
656 status = ocfs2_journal_dirty(handle, vb->vb_bh);
662 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
665 * We should have already allocated enough space before the transaction,
666 * so no need to restart.
668 BUG_ON(why != RESTART_NONE || clusters_to_add);
675 static int __ocfs2_remove_xattr_range(struct inode *inode,
676 struct ocfs2_xattr_value_buf *vb,
677 u32 cpos, u32 phys_cpos, u32 len,
678 unsigned int ext_flags,
679 struct ocfs2_xattr_set_ctxt *ctxt)
682 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
683 handle_t *handle = ctxt->handle;
684 struct ocfs2_extent_tree et;
686 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
688 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
689 OCFS2_JOURNAL_ACCESS_WRITE);
695 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
702 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
704 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
710 if (ext_flags & OCFS2_EXT_REFCOUNTED)
711 ret = ocfs2_decrease_refcount(inode, handle,
712 ocfs2_blocks_to_clusters(inode->i_sb,
714 len, ctxt->meta_ac, &ctxt->dealloc, 1);
716 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
725 static int ocfs2_xattr_shrink_size(struct inode *inode,
728 struct ocfs2_xattr_value_buf *vb,
729 struct ocfs2_xattr_set_ctxt *ctxt)
732 unsigned int ext_flags;
733 u32 trunc_len, cpos, phys_cpos, alloc_size;
736 if (old_clusters <= new_clusters)
740 trunc_len = old_clusters - new_clusters;
742 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
744 &vb->vb_xv->xr_list, &ext_flags);
750 if (alloc_size > trunc_len)
751 alloc_size = trunc_len;
753 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
754 phys_cpos, alloc_size,
761 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
762 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
765 trunc_len -= alloc_size;
772 static int ocfs2_xattr_value_truncate(struct inode *inode,
773 struct ocfs2_xattr_value_buf *vb,
775 struct ocfs2_xattr_set_ctxt *ctxt)
778 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
779 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
781 if (new_clusters == old_clusters)
784 if (new_clusters > old_clusters)
785 ret = ocfs2_xattr_extend_allocation(inode,
786 new_clusters - old_clusters,
789 ret = ocfs2_xattr_shrink_size(inode,
790 old_clusters, new_clusters,
796 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
797 size_t *result, const char *prefix,
798 const char *name, int name_len)
800 char *p = buffer + *result;
801 int prefix_len = strlen(prefix);
802 int total_len = prefix_len + name_len + 1;
804 *result += total_len;
806 /* we are just looking for how big our buffer needs to be */
813 memcpy(p, prefix, prefix_len);
814 memcpy(p + prefix_len, name, name_len);
815 p[prefix_len + name_len] = '\0';
820 static int ocfs2_xattr_list_entries(struct inode *inode,
821 struct ocfs2_xattr_header *header,
822 char *buffer, size_t buffer_size)
826 const char *prefix, *name;
828 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
829 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
830 type = ocfs2_xattr_get_type(entry);
831 prefix = ocfs2_xattr_prefix(type);
834 name = (const char *)header +
835 le16_to_cpu(entry->xe_name_offset);
837 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
838 &result, prefix, name,
848 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
849 struct ocfs2_dinode *di)
851 struct ocfs2_xattr_header *xh;
854 xh = (struct ocfs2_xattr_header *)
855 ((void *)di + inode->i_sb->s_blocksize -
856 le16_to_cpu(di->i_xattr_inline_size));
858 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
859 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
865 static int ocfs2_xattr_ibody_list(struct inode *inode,
866 struct ocfs2_dinode *di,
870 struct ocfs2_xattr_header *header = NULL;
871 struct ocfs2_inode_info *oi = OCFS2_I(inode);
874 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
877 header = (struct ocfs2_xattr_header *)
878 ((void *)di + inode->i_sb->s_blocksize -
879 le16_to_cpu(di->i_xattr_inline_size));
881 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
886 static int ocfs2_xattr_block_list(struct inode *inode,
887 struct ocfs2_dinode *di,
891 struct buffer_head *blk_bh = NULL;
892 struct ocfs2_xattr_block *xb;
895 if (!di->i_xattr_loc)
898 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
905 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
906 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
907 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
908 ret = ocfs2_xattr_list_entries(inode, header,
909 buffer, buffer_size);
911 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
912 buffer, buffer_size);
919 ssize_t ocfs2_listxattr(struct dentry *dentry,
923 int ret = 0, i_ret = 0, b_ret = 0;
924 struct buffer_head *di_bh = NULL;
925 struct ocfs2_dinode *di = NULL;
926 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
928 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
931 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
934 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
940 di = (struct ocfs2_dinode *)di_bh->b_data;
942 down_read(&oi->ip_xattr_sem);
943 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
951 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
956 up_read(&oi->ip_xattr_sem);
957 ocfs2_inode_unlock(dentry->d_inode, 0);
961 return i_ret + b_ret;
964 static int ocfs2_xattr_find_entry(int name_index,
966 struct ocfs2_xattr_search *xs)
968 struct ocfs2_xattr_entry *entry;
975 name_len = strlen(name);
977 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
978 cmp = name_index - ocfs2_xattr_get_type(entry);
980 cmp = name_len - entry->xe_name_len;
982 cmp = memcmp(name, (xs->base +
983 le16_to_cpu(entry->xe_name_offset)),
991 return cmp ? -ENODATA : 0;
994 static int ocfs2_xattr_get_value_outside(struct inode *inode,
995 struct ocfs2_xattr_value_root *xv,
999 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1002 size_t cplen, blocksize;
1003 struct buffer_head *bh = NULL;
1004 struct ocfs2_extent_list *el;
1007 clusters = le32_to_cpu(xv->xr_clusters);
1008 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1009 blocksize = inode->i_sb->s_blocksize;
1012 while (cpos < clusters) {
1013 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1014 &num_clusters, el, NULL);
1020 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1021 /* Copy ocfs2_xattr_value */
1022 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1023 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1030 cplen = len >= blocksize ? blocksize : len;
1031 memcpy(buffer, bh->b_data, cplen);
1040 cpos += num_clusters;
1046 static int ocfs2_xattr_ibody_get(struct inode *inode,
1051 struct ocfs2_xattr_search *xs)
1053 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1054 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1055 struct ocfs2_xattr_value_root *xv;
1059 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1062 xs->end = (void *)di + inode->i_sb->s_blocksize;
1063 xs->header = (struct ocfs2_xattr_header *)
1064 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1065 xs->base = (void *)xs->header;
1066 xs->here = xs->header->xh_entries;
1068 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1071 size = le64_to_cpu(xs->here->xe_value_size);
1073 if (size > buffer_size)
1075 if (ocfs2_xattr_is_local(xs->here)) {
1076 memcpy(buffer, (void *)xs->base +
1077 le16_to_cpu(xs->here->xe_name_offset) +
1078 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1080 xv = (struct ocfs2_xattr_value_root *)
1081 (xs->base + le16_to_cpu(
1082 xs->here->xe_name_offset) +
1083 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1084 ret = ocfs2_xattr_get_value_outside(inode, xv,
1096 static int ocfs2_xattr_block_get(struct inode *inode,
1101 struct ocfs2_xattr_search *xs)
1103 struct ocfs2_xattr_block *xb;
1104 struct ocfs2_xattr_value_root *xv;
1106 int ret = -ENODATA, name_offset, name_len, i;
1107 int uninitialized_var(block_off);
1109 xs->bucket = ocfs2_xattr_bucket_new(inode);
1116 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1122 if (xs->not_found) {
1127 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1128 size = le64_to_cpu(xs->here->xe_value_size);
1131 if (size > buffer_size)
1134 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1135 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1136 i = xs->here - xs->header->xh_entries;
1138 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1139 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1140 bucket_xh(xs->bucket),
1144 xs->base = bucket_block(xs->bucket, block_off);
1146 if (ocfs2_xattr_is_local(xs->here)) {
1147 memcpy(buffer, (void *)xs->base +
1148 name_offset + name_len, size);
1150 xv = (struct ocfs2_xattr_value_root *)
1151 (xs->base + name_offset + name_len);
1152 ret = ocfs2_xattr_get_value_outside(inode, xv,
1162 ocfs2_xattr_bucket_free(xs->bucket);
1164 brelse(xs->xattr_bh);
1165 xs->xattr_bh = NULL;
1169 int ocfs2_xattr_get_nolock(struct inode *inode,
1170 struct buffer_head *di_bh,
1177 struct ocfs2_dinode *di = NULL;
1178 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1179 struct ocfs2_xattr_search xis = {
1180 .not_found = -ENODATA,
1182 struct ocfs2_xattr_search xbs = {
1183 .not_found = -ENODATA,
1186 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1189 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1192 xis.inode_bh = xbs.inode_bh = di_bh;
1193 di = (struct ocfs2_dinode *)di_bh->b_data;
1195 down_read(&oi->ip_xattr_sem);
1196 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1198 if (ret == -ENODATA && di->i_xattr_loc)
1199 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1201 up_read(&oi->ip_xattr_sem);
1206 /* ocfs2_xattr_get()
1208 * Copy an extended attribute into the buffer provided.
1209 * Buffer is NULL to compute the size of buffer required.
1211 static int ocfs2_xattr_get(struct inode *inode,
1218 struct buffer_head *di_bh = NULL;
1220 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1225 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1226 name, buffer, buffer_size);
1228 ocfs2_inode_unlock(inode, 0);
1235 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1237 struct ocfs2_xattr_value_buf *vb,
1241 int ret = 0, i, cp_len;
1242 u16 blocksize = inode->i_sb->s_blocksize;
1243 u32 p_cluster, num_clusters;
1244 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1245 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1247 struct buffer_head *bh = NULL;
1248 unsigned int ext_flags;
1249 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1251 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1253 while (cpos < clusters) {
1254 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1255 &num_clusters, &xv->xr_list,
1262 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1264 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1266 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1267 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1274 ret = ocfs2_journal_access(handle,
1277 OCFS2_JOURNAL_ACCESS_WRITE);
1283 cp_len = value_len > blocksize ? blocksize : value_len;
1284 memcpy(bh->b_data, value, cp_len);
1285 value_len -= cp_len;
1287 if (cp_len < blocksize)
1288 memset(bh->b_data + cp_len, 0,
1289 blocksize - cp_len);
1291 ret = ocfs2_journal_dirty(handle, bh);
1300 * XXX: do we need to empty all the following
1301 * blocks in this cluster?
1306 cpos += num_clusters;
1314 static int ocfs2_xattr_cleanup(struct inode *inode,
1316 struct ocfs2_xattr_info *xi,
1317 struct ocfs2_xattr_search *xs,
1318 struct ocfs2_xattr_value_buf *vb,
1322 size_t name_len = strlen(xi->name);
1323 void *val = xs->base + offs;
1324 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1326 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1327 OCFS2_JOURNAL_ACCESS_WRITE);
1332 /* Decrease xattr count */
1333 le16_add_cpu(&xs->header->xh_count, -1);
1334 /* Remove the xattr entry and tree root which has already be set*/
1335 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1336 memset(val, 0, size);
1338 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1345 static int ocfs2_xattr_update_entry(struct inode *inode,
1347 struct ocfs2_xattr_info *xi,
1348 struct ocfs2_xattr_search *xs,
1349 struct ocfs2_xattr_value_buf *vb,
1354 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1355 OCFS2_JOURNAL_ACCESS_WRITE);
1361 xs->here->xe_name_offset = cpu_to_le16(offs);
1362 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1363 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1364 ocfs2_xattr_set_local(xs->here, 1);
1366 ocfs2_xattr_set_local(xs->here, 0);
1367 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1369 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1377 * ocfs2_xattr_set_value_outside()
1379 * Set large size value in B tree.
1381 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1382 struct ocfs2_xattr_info *xi,
1383 struct ocfs2_xattr_search *xs,
1384 struct ocfs2_xattr_set_ctxt *ctxt,
1385 struct ocfs2_xattr_value_buf *vb,
1388 size_t name_len = strlen(xi->name);
1389 void *val = xs->base + offs;
1390 struct ocfs2_xattr_value_root *xv = NULL;
1391 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1394 memset(val, 0, size);
1395 memcpy(val, xi->name, name_len);
1396 xv = (struct ocfs2_xattr_value_root *)
1397 (val + OCFS2_XATTR_SIZE(name_len));
1398 xv->xr_clusters = 0;
1399 xv->xr_last_eb_blk = 0;
1400 xv->xr_list.l_tree_depth = 0;
1401 xv->xr_list.l_count = cpu_to_le16(1);
1402 xv->xr_list.l_next_free_rec = 0;
1405 ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt);
1410 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
1415 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
1416 xi->value, xi->value_len);
1424 * ocfs2_xattr_set_entry_local()
1426 * Set, replace or remove extended attribute in local.
1428 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1429 struct ocfs2_xattr_info *xi,
1430 struct ocfs2_xattr_search *xs,
1431 struct ocfs2_xattr_entry *last,
1434 size_t name_len = strlen(xi->name);
1437 if (xi->value && xs->not_found) {
1438 /* Insert the new xattr entry. */
1439 le16_add_cpu(&xs->header->xh_count, 1);
1440 ocfs2_xattr_set_type(last, xi->name_index);
1441 ocfs2_xattr_set_local(last, 1);
1442 last->xe_name_len = name_len;
1448 first_val = xs->base + min_offs;
1449 offs = le16_to_cpu(xs->here->xe_name_offset);
1450 val = xs->base + offs;
1452 if (le64_to_cpu(xs->here->xe_value_size) >
1453 OCFS2_XATTR_INLINE_SIZE)
1454 size = OCFS2_XATTR_SIZE(name_len) +
1455 OCFS2_XATTR_ROOT_SIZE;
1457 size = OCFS2_XATTR_SIZE(name_len) +
1458 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1460 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1461 OCFS2_XATTR_SIZE(xi->value_len)) {
1462 /* The old and the new value have the
1463 same size. Just replace the value. */
1464 ocfs2_xattr_set_local(xs->here, 1);
1465 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1466 /* Clear value bytes. */
1467 memset(val + OCFS2_XATTR_SIZE(name_len),
1469 OCFS2_XATTR_SIZE(xi->value_len));
1470 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1475 /* Remove the old name+value. */
1476 memmove(first_val + size, first_val, val - first_val);
1477 memset(first_val, 0, size);
1478 xs->here->xe_name_hash = 0;
1479 xs->here->xe_name_offset = 0;
1480 ocfs2_xattr_set_local(xs->here, 1);
1481 xs->here->xe_value_size = 0;
1485 /* Adjust all value offsets. */
1486 last = xs->header->xh_entries;
1487 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1488 size_t o = le16_to_cpu(last->xe_name_offset);
1491 last->xe_name_offset = cpu_to_le16(o + size);
1496 /* Remove the old entry. */
1498 memmove(xs->here, xs->here + 1,
1499 (void *)last - (void *)xs->here);
1500 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1501 le16_add_cpu(&xs->header->xh_count, -1);
1505 /* Insert the new name+value. */
1506 size_t size = OCFS2_XATTR_SIZE(name_len) +
1507 OCFS2_XATTR_SIZE(xi->value_len);
1508 void *val = xs->base + min_offs - size;
1510 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1511 memset(val, 0, size);
1512 memcpy(val, xi->name, name_len);
1513 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1516 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1517 ocfs2_xattr_set_local(xs->here, 1);
1518 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1525 * ocfs2_xattr_set_entry()
1527 * Set extended attribute entry into inode or block.
1529 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1530 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1531 * then set value in B tree with set_value_outside().
1533 static int ocfs2_xattr_set_entry(struct inode *inode,
1534 struct ocfs2_xattr_info *xi,
1535 struct ocfs2_xattr_search *xs,
1536 struct ocfs2_xattr_set_ctxt *ctxt,
1539 struct ocfs2_xattr_entry *last;
1540 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1541 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1542 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1544 handle_t *handle = ctxt->handle;
1546 struct ocfs2_xattr_info xi_l = {
1547 .name_index = xi->name_index,
1550 .value_len = xi->value_len,
1552 struct ocfs2_xattr_value_buf vb = {
1553 .vb_bh = xs->xattr_bh,
1554 .vb_access = ocfs2_journal_access_di,
1557 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1558 BUG_ON(xs->xattr_bh == xs->inode_bh);
1559 vb.vb_access = ocfs2_journal_access_xb;
1561 BUG_ON(xs->xattr_bh != xs->inode_bh);
1563 /* Compute min_offs, last and free space. */
1564 last = xs->header->xh_entries;
1566 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1567 size_t offs = le16_to_cpu(last->xe_name_offset);
1568 if (offs < min_offs)
1573 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
1577 if (!xs->not_found) {
1579 if (ocfs2_xattr_is_local(xs->here))
1580 size = OCFS2_XATTR_SIZE(name_len) +
1581 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1583 size = OCFS2_XATTR_SIZE(name_len) +
1584 OCFS2_XATTR_ROOT_SIZE;
1585 free += (size + sizeof(struct ocfs2_xattr_entry));
1587 /* Check free space in inode or block */
1588 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1589 if (free < sizeof(struct ocfs2_xattr_entry) +
1590 OCFS2_XATTR_SIZE(name_len) +
1591 OCFS2_XATTR_ROOT_SIZE) {
1595 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1596 xi_l.value = (void *)&def_xv;
1597 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1598 } else if (xi->value) {
1599 if (free < sizeof(struct ocfs2_xattr_entry) +
1600 OCFS2_XATTR_SIZE(name_len) +
1601 OCFS2_XATTR_SIZE(xi->value_len)) {
1607 if (!xs->not_found) {
1608 /* For existing extended attribute */
1609 size_t size = OCFS2_XATTR_SIZE(name_len) +
1610 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1611 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1612 void *val = xs->base + offs;
1614 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1615 /* Replace existing local xattr with tree root */
1616 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1621 } else if (!ocfs2_xattr_is_local(xs->here)) {
1622 /* For existing xattr which has value outside */
1623 vb.vb_xv = (struct ocfs2_xattr_value_root *)
1624 (val + OCFS2_XATTR_SIZE(name_len));
1626 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1628 * If new value need set outside also,
1629 * first truncate old value to new value,
1630 * then set new value with set_value_outside().
1632 ret = ocfs2_xattr_value_truncate(inode,
1641 ret = ocfs2_xattr_update_entry(inode,
1652 ret = __ocfs2_xattr_set_value_outside(inode,
1662 * If new value need set in local,
1663 * just trucate old value to zero.
1665 ret = ocfs2_xattr_value_truncate(inode,
1675 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
1676 OCFS2_JOURNAL_ACCESS_WRITE);
1682 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1683 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
1684 OCFS2_JOURNAL_ACCESS_WRITE);
1692 * Set value in local, include set tree root in local.
1693 * This is the first step for value size >INLINE_SIZE.
1695 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1697 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1698 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1705 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1706 (flag & OCFS2_INLINE_XATTR_FL)) {
1707 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1708 unsigned int xattrsize = osb->s_xattr_inline_size;
1711 * Adjust extent record count or inline data size
1712 * to reserve space for extended attribute.
1714 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1715 struct ocfs2_inline_data *idata = &di->id2.i_data;
1716 le16_add_cpu(&idata->id_count, -xattrsize);
1717 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1718 struct ocfs2_extent_list *el = &di->id2.i_list;
1719 le16_add_cpu(&el->l_count, -(xattrsize /
1720 sizeof(struct ocfs2_extent_rec)));
1722 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1724 /* Update xattr flag */
1725 spin_lock(&oi->ip_lock);
1726 oi->ip_dyn_features |= flag;
1727 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1728 spin_unlock(&oi->ip_lock);
1730 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1734 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1736 * Set value outside in B tree.
1737 * This is the second step for value size > INLINE_SIZE.
1739 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1740 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
1747 * If set value outside failed, we have to clean
1748 * the junk tree root we have already set in local.
1750 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1761 * In xattr remove, if it is stored outside and refcounted, we may have
1762 * the chance to split the refcount tree. So need the allocators.
1764 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
1765 struct ocfs2_xattr_value_root *xv,
1766 struct ocfs2_caching_info *ref_ci,
1767 struct buffer_head *ref_root_bh,
1768 struct ocfs2_alloc_context **meta_ac,
1771 int ret, meta_add = 0;
1772 u32 p_cluster, num_clusters;
1773 unsigned int ext_flags;
1776 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
1785 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
1788 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
1790 &meta_add, ref_credits);
1796 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
1805 static int ocfs2_remove_value_outside(struct inode*inode,
1806 struct ocfs2_xattr_value_buf *vb,
1807 struct ocfs2_xattr_header *header,
1808 struct ocfs2_caching_info *ref_ci,
1809 struct buffer_head *ref_root_bh)
1811 int ret = 0, i, ref_credits;
1812 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1813 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1816 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
1818 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1819 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1821 if (ocfs2_xattr_is_local(entry))
1824 val = (void *)header +
1825 le16_to_cpu(entry->xe_name_offset);
1826 vb->vb_xv = (struct ocfs2_xattr_value_root *)
1827 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1829 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
1830 ref_ci, ref_root_bh,
1834 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
1835 ocfs2_remove_extent_credits(osb->sb));
1836 if (IS_ERR(ctxt.handle)) {
1837 ret = PTR_ERR(ctxt.handle);
1842 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
1848 ocfs2_commit_trans(osb, ctxt.handle);
1850 ocfs2_free_alloc_context(ctxt.meta_ac);
1851 ctxt.meta_ac = NULL;
1856 ocfs2_free_alloc_context(ctxt.meta_ac);
1857 ocfs2_schedule_truncate_log_flush(osb, 1);
1858 ocfs2_run_deallocs(osb, &ctxt.dealloc);
1862 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1863 struct buffer_head *di_bh,
1864 struct ocfs2_caching_info *ref_ci,
1865 struct buffer_head *ref_root_bh)
1868 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1869 struct ocfs2_xattr_header *header;
1871 struct ocfs2_xattr_value_buf vb = {
1873 .vb_access = ocfs2_journal_access_di,
1876 header = (struct ocfs2_xattr_header *)
1877 ((void *)di + inode->i_sb->s_blocksize -
1878 le16_to_cpu(di->i_xattr_inline_size));
1880 ret = ocfs2_remove_value_outside(inode, &vb, header,
1881 ref_ci, ref_root_bh);
1886 struct ocfs2_rm_xattr_bucket_para {
1887 struct ocfs2_caching_info *ref_ci;
1888 struct buffer_head *ref_root_bh;
1891 static int ocfs2_xattr_block_remove(struct inode *inode,
1892 struct buffer_head *blk_bh,
1893 struct ocfs2_caching_info *ref_ci,
1894 struct buffer_head *ref_root_bh)
1896 struct ocfs2_xattr_block *xb;
1898 struct ocfs2_xattr_value_buf vb = {
1900 .vb_access = ocfs2_journal_access_xb,
1902 struct ocfs2_rm_xattr_bucket_para args = {
1904 .ref_root_bh = ref_root_bh,
1907 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1908 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1909 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1910 ret = ocfs2_remove_value_outside(inode, &vb, header,
1911 ref_ci, ref_root_bh);
1913 ret = ocfs2_iterate_xattr_index_block(inode,
1915 ocfs2_rm_xattr_cluster,
1921 static int ocfs2_xattr_free_block(struct inode *inode,
1923 struct ocfs2_caching_info *ref_ci,
1924 struct buffer_head *ref_root_bh)
1926 struct inode *xb_alloc_inode;
1927 struct buffer_head *xb_alloc_bh = NULL;
1928 struct buffer_head *blk_bh = NULL;
1929 struct ocfs2_xattr_block *xb;
1930 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1936 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
1942 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
1948 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1949 blk = le64_to_cpu(xb->xb_blkno);
1950 bit = le16_to_cpu(xb->xb_suballoc_bit);
1951 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1953 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1954 EXTENT_ALLOC_SYSTEM_INODE,
1955 le16_to_cpu(xb->xb_suballoc_slot));
1956 if (!xb_alloc_inode) {
1961 mutex_lock(&xb_alloc_inode->i_mutex);
1963 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1969 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1970 if (IS_ERR(handle)) {
1971 ret = PTR_ERR(handle);
1976 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1981 ocfs2_commit_trans(osb, handle);
1983 ocfs2_inode_unlock(xb_alloc_inode, 1);
1984 brelse(xb_alloc_bh);
1986 mutex_unlock(&xb_alloc_inode->i_mutex);
1987 iput(xb_alloc_inode);
1994 * ocfs2_xattr_remove()
1996 * Free extended attribute resources associated with this inode.
1998 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2000 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2001 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2002 struct ocfs2_refcount_tree *ref_tree = NULL;
2003 struct buffer_head *ref_root_bh = NULL;
2004 struct ocfs2_caching_info *ref_ci = NULL;
2008 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2011 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2014 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2015 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2016 le64_to_cpu(di->i_refcount_loc),
2017 1, &ref_tree, &ref_root_bh);
2022 ref_ci = &ref_tree->rf_ci;
2026 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2027 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2028 ref_ci, ref_root_bh);
2035 if (di->i_xattr_loc) {
2036 ret = ocfs2_xattr_free_block(inode,
2037 le64_to_cpu(di->i_xattr_loc),
2038 ref_ci, ref_root_bh);
2045 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2046 OCFS2_INODE_UPDATE_CREDITS);
2047 if (IS_ERR(handle)) {
2048 ret = PTR_ERR(handle);
2052 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2053 OCFS2_JOURNAL_ACCESS_WRITE);
2059 di->i_xattr_loc = 0;
2061 spin_lock(&oi->ip_lock);
2062 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2063 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2064 spin_unlock(&oi->ip_lock);
2066 ret = ocfs2_journal_dirty(handle, di_bh);
2070 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2073 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2074 brelse(ref_root_bh);
2078 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2079 struct ocfs2_dinode *di)
2081 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2082 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2085 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2088 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2089 struct ocfs2_inline_data *idata = &di->id2.i_data;
2090 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2091 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2092 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2093 le64_to_cpu(di->i_size);
2095 struct ocfs2_extent_list *el = &di->id2.i_list;
2096 free = (le16_to_cpu(el->l_count) -
2097 le16_to_cpu(el->l_next_free_rec)) *
2098 sizeof(struct ocfs2_extent_rec);
2100 if (free >= xattrsize)
2107 * ocfs2_xattr_ibody_find()
2109 * Find extended attribute in inode block and
2110 * fill search info into struct ocfs2_xattr_search.
2112 static int ocfs2_xattr_ibody_find(struct inode *inode,
2115 struct ocfs2_xattr_search *xs)
2117 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2118 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2122 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2125 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2126 down_read(&oi->ip_alloc_sem);
2127 has_space = ocfs2_xattr_has_space_inline(inode, di);
2128 up_read(&oi->ip_alloc_sem);
2133 xs->xattr_bh = xs->inode_bh;
2134 xs->end = (void *)di + inode->i_sb->s_blocksize;
2135 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2136 xs->header = (struct ocfs2_xattr_header *)
2137 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2139 xs->header = (struct ocfs2_xattr_header *)
2140 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2141 xs->base = (void *)xs->header;
2142 xs->here = xs->header->xh_entries;
2144 /* Find the named attribute. */
2145 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2146 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2147 if (ret && ret != -ENODATA)
2149 xs->not_found = ret;
2156 * ocfs2_xattr_ibody_set()
2158 * Set, replace or remove an extended attribute into inode block.
2161 static int ocfs2_xattr_ibody_set(struct inode *inode,
2162 struct ocfs2_xattr_info *xi,
2163 struct ocfs2_xattr_search *xs,
2164 struct ocfs2_xattr_set_ctxt *ctxt)
2166 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2167 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2170 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2173 down_write(&oi->ip_alloc_sem);
2174 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2175 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2181 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2182 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2184 up_write(&oi->ip_alloc_sem);
2190 * ocfs2_xattr_block_find()
2192 * Find extended attribute in external block and
2193 * fill search info into struct ocfs2_xattr_search.
2195 static int ocfs2_xattr_block_find(struct inode *inode,
2198 struct ocfs2_xattr_search *xs)
2200 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2201 struct buffer_head *blk_bh = NULL;
2202 struct ocfs2_xattr_block *xb;
2205 if (!di->i_xattr_loc)
2208 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2215 xs->xattr_bh = blk_bh;
2216 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2218 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2219 xs->header = &xb->xb_attrs.xb_header;
2220 xs->base = (void *)xs->header;
2221 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2222 xs->here = xs->header->xh_entries;
2224 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2226 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2230 if (ret && ret != -ENODATA) {
2231 xs->xattr_bh = NULL;
2234 xs->not_found = ret;
2242 static int ocfs2_create_xattr_block(handle_t *handle,
2243 struct inode *inode,
2244 struct buffer_head *inode_bh,
2245 struct ocfs2_alloc_context *meta_ac,
2246 struct buffer_head **ret_bh,
2250 u16 suballoc_bit_start;
2253 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2254 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2255 struct buffer_head *new_bh = NULL;
2256 struct ocfs2_xattr_block *xblk;
2258 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2259 OCFS2_JOURNAL_ACCESS_CREATE);
2265 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2266 &suballoc_bit_start, &num_got,
2273 new_bh = sb_getblk(inode->i_sb, first_blkno);
2274 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2276 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2278 OCFS2_JOURNAL_ACCESS_CREATE);
2284 /* Initialize ocfs2_xattr_block */
2285 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2286 memset(xblk, 0, inode->i_sb->s_blocksize);
2287 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2288 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2289 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2290 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2291 xblk->xb_blkno = cpu_to_le64(first_blkno);
2294 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2295 xr->xt_clusters = cpu_to_le32(1);
2296 xr->xt_last_eb_blk = 0;
2297 xr->xt_list.l_tree_depth = 0;
2298 xr->xt_list.l_count = cpu_to_le16(
2299 ocfs2_xattr_recs_per_xb(inode->i_sb));
2300 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2301 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2304 ret = ocfs2_journal_dirty(handle, new_bh);
2309 di->i_xattr_loc = cpu_to_le64(first_blkno);
2310 ocfs2_journal_dirty(handle, inode_bh);
2321 * ocfs2_xattr_block_set()
2323 * Set, replace or remove an extended attribute into external block.
2326 static int ocfs2_xattr_block_set(struct inode *inode,
2327 struct ocfs2_xattr_info *xi,
2328 struct ocfs2_xattr_search *xs,
2329 struct ocfs2_xattr_set_ctxt *ctxt)
2331 struct buffer_head *new_bh = NULL;
2332 handle_t *handle = ctxt->handle;
2333 struct ocfs2_xattr_block *xblk = NULL;
2336 if (!xs->xattr_bh) {
2337 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
2338 ctxt->meta_ac, &new_bh, 0);
2344 xs->xattr_bh = new_bh;
2345 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2346 xs->header = &xblk->xb_attrs.xb_header;
2347 xs->base = (void *)xs->header;
2348 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2349 xs->here = xs->header->xh_entries;
2351 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2353 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2354 /* Set extended attribute into external block */
2355 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2356 OCFS2_HAS_XATTR_FL);
2357 if (!ret || ret != -ENOSPC)
2360 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2365 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2372 /* Check whether the new xattr can be inserted into the inode. */
2373 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2374 struct ocfs2_xattr_info *xi,
2375 struct ocfs2_xattr_search *xs)
2378 struct ocfs2_xattr_entry *last;
2380 size_t min_offs = xs->end - xs->base;
2385 last = xs->header->xh_entries;
2387 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2388 size_t offs = le16_to_cpu(last->xe_name_offset);
2389 if (offs < min_offs)
2394 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2398 BUG_ON(!xs->not_found);
2400 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2401 value_size = OCFS2_XATTR_ROOT_SIZE;
2403 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2405 if (free >= sizeof(struct ocfs2_xattr_entry) +
2406 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2412 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2413 struct ocfs2_dinode *di,
2414 struct ocfs2_xattr_info *xi,
2415 struct ocfs2_xattr_search *xis,
2416 struct ocfs2_xattr_search *xbs,
2421 int ret = 0, old_in_xb = 0;
2422 int clusters_add = 0, meta_add = 0, credits = 0;
2423 struct buffer_head *bh = NULL;
2424 struct ocfs2_xattr_block *xb = NULL;
2425 struct ocfs2_xattr_entry *xe = NULL;
2426 struct ocfs2_xattr_value_root *xv = NULL;
2428 int name_offset, name_len = 0;
2429 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2434 * Calculate the clusters we need to write.
2435 * No matter whether we replace an old one or add a new one,
2436 * we need this for writing.
2438 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2439 credits += new_clusters *
2440 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2442 if (xis->not_found && xbs->not_found) {
2443 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2445 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2446 clusters_add += new_clusters;
2447 credits += ocfs2_calc_extend_credits(inode->i_sb,
2455 if (!xis->not_found) {
2457 name_offset = le16_to_cpu(xe->xe_name_offset);
2458 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2460 credits += OCFS2_INODE_UPDATE_CREDITS;
2462 int i, block_off = 0;
2463 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2465 name_offset = le16_to_cpu(xe->xe_name_offset);
2466 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2467 i = xbs->here - xbs->header->xh_entries;
2470 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2471 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
2472 bucket_xh(xbs->bucket),
2475 base = bucket_block(xbs->bucket, block_off);
2476 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2479 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2484 * delete a xattr doesn't need metadata and cluster allocation.
2485 * so just calculate the credits and return.
2487 * The credits for removing the value tree will be extended
2488 * by ocfs2_remove_extent itself.
2491 if (!ocfs2_xattr_is_local(xe))
2492 credits += ocfs2_remove_extent_credits(inode->i_sb);
2497 /* do cluster allocation guess first. */
2498 value_size = le64_to_cpu(xe->xe_value_size);
2502 * In xattr set, we always try to set the xe in inode first,
2503 * so if it can be inserted into inode successfully, the old
2504 * one will be removed from the xattr block, and this xattr
2505 * will be inserted into inode as a new xattr in inode.
2507 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2508 clusters_add += new_clusters;
2509 credits += ocfs2_remove_extent_credits(inode->i_sb) +
2510 OCFS2_INODE_UPDATE_CREDITS;
2511 if (!ocfs2_xattr_is_local(xe))
2512 credits += ocfs2_calc_extend_credits(
2520 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2521 /* the new values will be stored outside. */
2522 u32 old_clusters = 0;
2524 if (!ocfs2_xattr_is_local(xe)) {
2525 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2527 xv = (struct ocfs2_xattr_value_root *)
2528 (base + name_offset + name_len);
2529 value_size = OCFS2_XATTR_ROOT_SIZE;
2533 if (old_clusters >= new_clusters) {
2534 credits += ocfs2_remove_extent_credits(inode->i_sb);
2537 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2538 clusters_add += new_clusters - old_clusters;
2539 credits += ocfs2_calc_extend_credits(inode->i_sb,
2543 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2548 * Now the new value will be stored inside. So if the new
2549 * value is smaller than the size of value root or the old
2550 * value, we don't need any allocation, otherwise we have
2551 * to guess metadata allocation.
2553 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2554 (!ocfs2_xattr_is_local(xe) &&
2555 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2560 /* calculate metadata allocation. */
2561 if (di->i_xattr_loc) {
2562 if (!xbs->xattr_bh) {
2563 ret = ocfs2_read_xattr_block(inode,
2564 le64_to_cpu(di->i_xattr_loc),
2571 xb = (struct ocfs2_xattr_block *)bh->b_data;
2573 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2576 * If there is already an xattr tree, good, we can calculate
2577 * like other b-trees. Otherwise we may have the chance of
2578 * create a tree, the credit calculation is borrowed from
2579 * ocfs2_calc_extend_credits with root_el = NULL. And the
2580 * new tree will be cluster based, so no meta is needed.
2582 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2583 struct ocfs2_extent_list *el =
2584 &xb->xb_attrs.xb_root.xt_list;
2585 meta_add += ocfs2_extend_meta_needed(el);
2586 credits += ocfs2_calc_extend_credits(inode->i_sb,
2589 credits += OCFS2_SUBALLOC_ALLOC + 1;
2592 * This cluster will be used either for new bucket or for
2594 * If the cluster size is the same as the bucket size, one
2595 * more is needed since we may need to extend the bucket
2599 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2600 if (OCFS2_XATTR_BUCKET_SIZE ==
2601 OCFS2_SB(inode->i_sb)->s_clustersize) {
2602 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2607 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2611 *clusters_need = clusters_add;
2613 *meta_need = meta_add;
2615 *credits_need = credits;
2620 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2621 struct ocfs2_dinode *di,
2622 struct ocfs2_xattr_info *xi,
2623 struct ocfs2_xattr_search *xis,
2624 struct ocfs2_xattr_search *xbs,
2625 struct ocfs2_xattr_set_ctxt *ctxt,
2629 int clusters_add, meta_add, ret;
2630 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2632 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2634 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2636 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
2637 &clusters_add, &meta_add, credits);
2643 meta_add += extra_meta;
2644 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2645 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
2648 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2657 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2663 if (ctxt->meta_ac) {
2664 ocfs2_free_alloc_context(ctxt->meta_ac);
2665 ctxt->meta_ac = NULL;
2669 * We cannot have an error and a non null ctxt->data_ac.
2676 static int __ocfs2_xattr_set_handle(struct inode *inode,
2677 struct ocfs2_dinode *di,
2678 struct ocfs2_xattr_info *xi,
2679 struct ocfs2_xattr_search *xis,
2680 struct ocfs2_xattr_search *xbs,
2681 struct ocfs2_xattr_set_ctxt *ctxt)
2683 int ret = 0, credits, old_found;
2686 /* Remove existing extended attribute */
2687 if (!xis->not_found)
2688 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2689 else if (!xbs->not_found)
2690 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2692 /* We always try to set extended attribute into inode first*/
2693 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2694 if (!ret && !xbs->not_found) {
2696 * If succeed and that extended attribute existing in
2697 * external block, then we will remove it.
2702 old_found = xis->not_found;
2703 xis->not_found = -ENODATA;
2704 ret = ocfs2_calc_xattr_set_need(inode,
2712 xis->not_found = old_found;
2718 ret = ocfs2_extend_trans(ctxt->handle, credits +
2719 ctxt->handle->h_buffer_credits);
2724 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2725 } else if (ret == -ENOSPC) {
2726 if (di->i_xattr_loc && !xbs->xattr_bh) {
2727 ret = ocfs2_xattr_block_find(inode,
2733 old_found = xis->not_found;
2734 xis->not_found = -ENODATA;
2735 ret = ocfs2_calc_xattr_set_need(inode,
2743 xis->not_found = old_found;
2749 ret = ocfs2_extend_trans(ctxt->handle, credits +
2750 ctxt->handle->h_buffer_credits);
2757 * If no space in inode, we will set extended attribute
2758 * into external block.
2760 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2763 if (!xis->not_found) {
2765 * If succeed and that extended attribute
2766 * existing in inode, we will remove it.
2770 xbs->not_found = -ENODATA;
2771 ret = ocfs2_calc_xattr_set_need(inode,
2784 ret = ocfs2_extend_trans(ctxt->handle, credits +
2785 ctxt->handle->h_buffer_credits);
2790 ret = ocfs2_xattr_ibody_set(inode, xi,
2797 /* Update inode ctime. */
2798 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2800 OCFS2_JOURNAL_ACCESS_WRITE);
2806 inode->i_ctime = CURRENT_TIME;
2807 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
2808 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
2809 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
2816 * This function only called duing creating inode
2817 * for init security/acl xattrs of the new inode.
2818 * All transanction credits have been reserved in mknod.
2820 int ocfs2_xattr_set_handle(handle_t *handle,
2821 struct inode *inode,
2822 struct buffer_head *di_bh,
2828 struct ocfs2_alloc_context *meta_ac,
2829 struct ocfs2_alloc_context *data_ac)
2831 struct ocfs2_dinode *di;
2834 struct ocfs2_xattr_info xi = {
2835 .name_index = name_index,
2838 .value_len = value_len,
2841 struct ocfs2_xattr_search xis = {
2842 .not_found = -ENODATA,
2845 struct ocfs2_xattr_search xbs = {
2846 .not_found = -ENODATA,
2849 struct ocfs2_xattr_set_ctxt ctxt = {
2855 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2859 * In extreme situation, may need xattr bucket when
2860 * block size is too small. And we have already reserved
2861 * the credits for bucket in mknod.
2863 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
2864 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2866 mlog_errno(-ENOMEM);
2871 xis.inode_bh = xbs.inode_bh = di_bh;
2872 di = (struct ocfs2_dinode *)di_bh->b_data;
2874 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2876 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2879 if (xis.not_found) {
2880 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2885 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2888 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2889 brelse(xbs.xattr_bh);
2890 ocfs2_xattr_bucket_free(xbs.bucket);
2898 * Set, replace or remove an extended attribute for this inode.
2899 * value is NULL to remove an existing extended attribute, else either
2900 * create or replace an extended attribute.
2902 int ocfs2_xattr_set(struct inode *inode,
2909 struct buffer_head *di_bh = NULL;
2910 struct ocfs2_dinode *di;
2911 int ret, credits, ref_meta = 0, ref_credits = 0;
2912 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2913 struct inode *tl_inode = osb->osb_tl_inode;
2914 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2915 struct ocfs2_refcount_tree *ref_tree = NULL;
2917 struct ocfs2_xattr_info xi = {
2918 .name_index = name_index,
2921 .value_len = value_len,
2924 struct ocfs2_xattr_search xis = {
2925 .not_found = -ENODATA,
2928 struct ocfs2_xattr_search xbs = {
2929 .not_found = -ENODATA,
2932 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2936 * Only xbs will be used on indexed trees. xis doesn't need a
2939 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2941 mlog_errno(-ENOMEM);
2945 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2948 goto cleanup_nolock;
2950 xis.inode_bh = xbs.inode_bh = di_bh;
2951 di = (struct ocfs2_dinode *)di_bh->b_data;
2953 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2955 * Scan inode and external block to find the same name
2956 * extended attribute and collect search infomation.
2958 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2961 if (xis.not_found) {
2962 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2967 if (xis.not_found && xbs.not_found) {
2969 if (flags & XATTR_REPLACE)
2976 if (flags & XATTR_CREATE)
2980 /* Check whether the value is refcounted and do some prepartion. */
2981 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
2982 (!xis.not_found || !xbs.not_found)) {
2983 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
2984 &xis, &xbs, &ref_tree,
2985 &ref_meta, &ref_credits);
2992 mutex_lock(&tl_inode->i_mutex);
2994 if (ocfs2_truncate_log_needs_flush(osb)) {
2995 ret = __ocfs2_flush_truncate_log(osb);
2997 mutex_unlock(&tl_inode->i_mutex);
3002 mutex_unlock(&tl_inode->i_mutex);
3004 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3005 &xbs, &ctxt, ref_meta, &credits);
3011 /* we need to update inode's ctime field, so add credit for it. */
3012 credits += OCFS2_INODE_UPDATE_CREDITS;
3013 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3014 if (IS_ERR(ctxt.handle)) {
3015 ret = PTR_ERR(ctxt.handle);
3020 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3022 ocfs2_commit_trans(osb, ctxt.handle);
3025 ocfs2_free_alloc_context(ctxt.data_ac);
3027 ocfs2_free_alloc_context(ctxt.meta_ac);
3028 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3029 ocfs2_schedule_truncate_log_flush(osb, 1);
3030 ocfs2_run_deallocs(osb, &ctxt.dealloc);
3034 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3035 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3036 if (!value && !ret) {
3037 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3041 ocfs2_inode_unlock(inode, 1);
3044 brelse(xbs.xattr_bh);
3045 ocfs2_xattr_bucket_free(xbs.bucket);
3051 * Find the xattr extent rec which may contains name_hash.
3052 * e_cpos will be the first name hash of the xattr rec.
3053 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3055 static int ocfs2_xattr_get_rec(struct inode *inode,
3060 struct ocfs2_extent_list *el)
3063 struct buffer_head *eb_bh = NULL;
3064 struct ocfs2_extent_block *eb;
3065 struct ocfs2_extent_rec *rec = NULL;
3068 if (el->l_tree_depth) {
3069 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3076 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3079 if (el->l_tree_depth) {
3080 ocfs2_error(inode->i_sb,
3081 "Inode %lu has non zero tree depth in "
3082 "xattr tree block %llu\n", inode->i_ino,
3083 (unsigned long long)eb_bh->b_blocknr);
3089 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3090 rec = &el->l_recs[i];
3092 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3093 e_blkno = le64_to_cpu(rec->e_blkno);
3099 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3100 "record (%u, %u, 0) in xattr", inode->i_ino,
3101 le32_to_cpu(rec->e_cpos),
3102 ocfs2_rec_clusters(el, rec));
3107 *p_blkno = le64_to_cpu(rec->e_blkno);
3108 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3110 *e_cpos = le32_to_cpu(rec->e_cpos);
3116 typedef int (xattr_bucket_func)(struct inode *inode,
3117 struct ocfs2_xattr_bucket *bucket,
3120 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3121 struct ocfs2_xattr_bucket *bucket,
3128 int i, ret = 0, cmp = 1, block_off, new_offset;
3129 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3130 size_t name_len = strlen(name);
3131 struct ocfs2_xattr_entry *xe = NULL;
3135 * We don't use binary search in the bucket because there
3136 * may be multiple entries with the same name hash.
3138 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3139 xe = &xh->xh_entries[i];
3141 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3143 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3146 cmp = name_index - ocfs2_xattr_get_type(xe);
3148 cmp = name_len - xe->xe_name_len;
3152 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3163 xe_name = bucket_block(bucket, block_off) + new_offset;
3164 if (!memcmp(name, xe_name, name_len)) {
3176 * Find the specified xattr entry in a series of buckets.
3177 * This series start from p_blkno and last for num_clusters.
3178 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3179 * the num of the valid buckets.
3181 * Return the buffer_head this xattr should reside in. And if the xattr's
3182 * hash is in the gap of 2 buckets, return the lower bucket.
3184 static int ocfs2_xattr_bucket_find(struct inode *inode,
3191 struct ocfs2_xattr_search *xs)
3194 struct ocfs2_xattr_header *xh = NULL;
3195 struct ocfs2_xattr_entry *xe = NULL;
3197 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3198 int low_bucket = 0, bucket, high_bucket;
3199 struct ocfs2_xattr_bucket *search;
3201 u64 blkno, lower_blkno = 0;
3203 search = ocfs2_xattr_bucket_new(inode);
3210 ret = ocfs2_read_xattr_bucket(search, p_blkno);
3216 xh = bucket_xh(search);
3217 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3218 while (low_bucket <= high_bucket) {
3219 ocfs2_xattr_bucket_relse(search);
3221 bucket = (low_bucket + high_bucket) / 2;
3222 blkno = p_blkno + bucket * blk_per_bucket;
3223 ret = ocfs2_read_xattr_bucket(search, blkno);
3229 xh = bucket_xh(search);
3230 xe = &xh->xh_entries[0];
3231 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3232 high_bucket = bucket - 1;
3237 * Check whether the hash of the last entry in our
3238 * bucket is larger than the search one. for an empty
3239 * bucket, the last one is also the first one.
3242 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3244 last_hash = le32_to_cpu(xe->xe_name_hash);
3246 /* record lower_blkno which may be the insert place. */
3247 lower_blkno = blkno;
3249 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3250 low_bucket = bucket + 1;
3254 /* the searched xattr should reside in this bucket if exists. */
3255 ret = ocfs2_find_xe_in_bucket(inode, search,
3256 name_index, name, name_hash,
3266 * Record the bucket we have found.
3267 * When the xattr's hash value is in the gap of 2 buckets, we will
3268 * always set it to the previous bucket.
3271 lower_blkno = p_blkno;
3273 /* This should be in cache - we just read it during the search */
3274 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3280 xs->header = bucket_xh(xs->bucket);
3281 xs->base = bucket_block(xs->bucket, 0);
3282 xs->end = xs->base + inode->i_sb->s_blocksize;
3285 xs->here = &xs->header->xh_entries[index];
3286 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3287 (unsigned long long)bucket_blkno(xs->bucket), index);
3292 ocfs2_xattr_bucket_free(search);
3296 static int ocfs2_xattr_index_block_find(struct inode *inode,
3297 struct buffer_head *root_bh,
3300 struct ocfs2_xattr_search *xs)
3303 struct ocfs2_xattr_block *xb =
3304 (struct ocfs2_xattr_block *)root_bh->b_data;
3305 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3306 struct ocfs2_extent_list *el = &xb_root->xt_list;
3308 u32 first_hash, num_clusters = 0;
3309 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3311 if (le16_to_cpu(el->l_next_free_rec) == 0)
3314 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3315 name, name_hash, name_index);
3317 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3324 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3326 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3327 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3330 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3331 p_blkno, first_hash, num_clusters, xs);
3337 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3340 xattr_bucket_func *func,
3344 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3345 u32 num_buckets = clusters * bpc;
3346 struct ocfs2_xattr_bucket *bucket;
3348 bucket = ocfs2_xattr_bucket_new(inode);
3350 mlog_errno(-ENOMEM);
3354 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3355 clusters, (unsigned long long)blkno);
3357 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3358 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3365 * The real bucket num in this series of blocks is stored
3366 * in the 1st bucket.
3369 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3371 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3372 (unsigned long long)blkno,
3373 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3375 ret = func(inode, bucket, para);
3376 if (ret && ret != -ERANGE)
3378 /* Fall through to bucket_relse() */
3381 ocfs2_xattr_bucket_relse(bucket);
3386 ocfs2_xattr_bucket_free(bucket);
3390 struct ocfs2_xattr_tree_list {
3396 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
3397 struct ocfs2_xattr_header *xh,
3404 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3407 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3409 *block_off = name_offset >> sb->s_blocksize_bits;
3410 *new_offset = name_offset % sb->s_blocksize;
3415 static int ocfs2_list_xattr_bucket(struct inode *inode,
3416 struct ocfs2_xattr_bucket *bucket,
3420 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
3421 int i, block_off, new_offset;
3422 const char *prefix, *name;
3424 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3425 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
3426 type = ocfs2_xattr_get_type(entry);
3427 prefix = ocfs2_xattr_prefix(type);
3430 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3438 name = (const char *)bucket_block(bucket, block_off) +
3440 ret = ocfs2_xattr_list_entry(xl->buffer,
3444 entry->xe_name_len);
3453 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
3454 struct buffer_head *blk_bh,
3455 xattr_tree_rec_func *rec_func,
3458 struct ocfs2_xattr_block *xb =
3459 (struct ocfs2_xattr_block *)blk_bh->b_data;
3460 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
3462 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3465 if (!el->l_next_free_rec || !rec_func)
3468 while (name_hash > 0) {
3469 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3470 &e_cpos, &num_clusters, el);
3476 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
3477 num_clusters, para);
3487 name_hash = e_cpos - 1;
3494 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
3495 struct buffer_head *root_bh,
3496 u64 blkno, u32 cpos, u32 len, void *para)
3498 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
3499 ocfs2_list_xattr_bucket, para);
3502 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3503 struct buffer_head *blk_bh,
3508 struct ocfs2_xattr_tree_list xl = {
3510 .buffer_size = buffer_size,
3514 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
3515 ocfs2_list_xattr_tree_rec, &xl);
3526 static int cmp_xe(const void *a, const void *b)
3528 const struct ocfs2_xattr_entry *l = a, *r = b;
3529 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3530 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3532 if (l_hash > r_hash)
3534 if (l_hash < r_hash)
3539 static void swap_xe(void *a, void *b, int size)
3541 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3544 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3545 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3549 * When the ocfs2_xattr_block is filled up, new bucket will be created
3550 * and all the xattr entries will be moved to the new bucket.
3551 * The header goes at the start of the bucket, and the names+values are
3552 * filled from the end. This is why *target starts as the last buffer.
3553 * Note: we need to sort the entries since they are not saved in order
3554 * in the ocfs2_xattr_block.
3556 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3557 struct buffer_head *xb_bh,
3558 struct ocfs2_xattr_bucket *bucket)
3560 int i, blocksize = inode->i_sb->s_blocksize;
3561 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3562 u16 offset, size, off_change;
3563 struct ocfs2_xattr_entry *xe;
3564 struct ocfs2_xattr_block *xb =
3565 (struct ocfs2_xattr_block *)xb_bh->b_data;
3566 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
3567 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3568 u16 count = le16_to_cpu(xb_xh->xh_count);
3569 char *src = xb_bh->b_data;
3570 char *target = bucket_block(bucket, blks - 1);
3572 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3573 (unsigned long long)xb_bh->b_blocknr,
3574 (unsigned long long)bucket_blkno(bucket));
3576 for (i = 0; i < blks; i++)
3577 memset(bucket_block(bucket, i), 0, blocksize);
3580 * Since the xe_name_offset is based on ocfs2_xattr_header,
3581 * there is a offset change corresponding to the change of
3582 * ocfs2_xattr_header's position.
3584 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3585 xe = &xb_xh->xh_entries[count - 1];
3586 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3587 size = blocksize - offset;
3589 /* copy all the names and values. */
3590 memcpy(target + offset, src + offset, size);
3592 /* Init new header now. */
3593 xh->xh_count = xb_xh->xh_count;
3594 xh->xh_num_buckets = cpu_to_le16(1);
3595 xh->xh_name_value_len = cpu_to_le16(size);
3596 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3598 /* copy all the entries. */
3599 target = bucket_block(bucket, 0);
3600 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3601 size = count * sizeof(struct ocfs2_xattr_entry);
3602 memcpy(target + offset, (char *)xb_xh + offset, size);
3604 /* Change the xe offset for all the xe because of the move. */
3605 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3606 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3607 for (i = 0; i < count; i++)
3608 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3610 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3611 offset, size, off_change);
3613 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3618 * After we move xattr from block to index btree, we have to
3619 * update ocfs2_xattr_search to the new xe and base.
3621 * When the entry is in xattr block, xattr_bh indicates the storage place.
3622 * While if the entry is in index b-tree, "bucket" indicates the
3623 * real place of the xattr.
3625 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3626 struct ocfs2_xattr_search *xs,
3627 struct buffer_head *old_bh)
3629 char *buf = old_bh->b_data;
3630 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3631 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
3634 xs->header = bucket_xh(xs->bucket);
3635 xs->base = bucket_block(xs->bucket, 0);
3636 xs->end = xs->base + inode->i_sb->s_blocksize;
3641 i = xs->here - old_xh->xh_entries;
3642 xs->here = &xs->header->xh_entries[i];
3645 static int ocfs2_xattr_create_index_block(struct inode *inode,
3646 struct ocfs2_xattr_search *xs,
3647 struct ocfs2_xattr_set_ctxt *ctxt)
3652 handle_t *handle = ctxt->handle;
3653 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3654 struct ocfs2_inode_info *oi = OCFS2_I(inode);
3655 struct buffer_head *xb_bh = xs->xattr_bh;
3656 struct ocfs2_xattr_block *xb =
3657 (struct ocfs2_xattr_block *)xb_bh->b_data;
3658 struct ocfs2_xattr_tree_root *xr;
3659 u16 xb_flags = le16_to_cpu(xb->xb_flags);
3661 mlog(0, "create xattr index block for %llu\n",
3662 (unsigned long long)xb_bh->b_blocknr);
3664 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
3665 BUG_ON(!xs->bucket);
3669 * We can use this lock for now, and maybe move to a dedicated mutex
3670 * if performance becomes a problem later.
3672 down_write(&oi->ip_alloc_sem);
3674 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
3675 OCFS2_JOURNAL_ACCESS_WRITE);
3681 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3682 1, 1, &bit_off, &len);
3689 * The bucket may spread in many blocks, and
3690 * we will only touch the 1st block and the last block
3691 * in the whole bucket(one for entry and one for data).
3693 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3695 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3696 (unsigned long long)blkno);
3698 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
3704 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3705 OCFS2_JOURNAL_ACCESS_CREATE);
3711 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3712 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3714 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3716 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3717 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3718 offsetof(struct ocfs2_xattr_block, xb_attrs));
3720 xr = &xb->xb_attrs.xb_root;
3721 xr->xt_clusters = cpu_to_le32(1);
3722 xr->xt_last_eb_blk = 0;
3723 xr->xt_list.l_tree_depth = 0;
3724 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3725 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3727 xr->xt_list.l_recs[0].e_cpos = 0;
3728 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3729 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3731 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3733 ocfs2_journal_dirty(handle, xb_bh);
3736 up_write(&oi->ip_alloc_sem);
3741 static int cmp_xe_offset(const void *a, const void *b)
3743 const struct ocfs2_xattr_entry *l = a, *r = b;
3744 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3745 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3747 if (l_name_offset < r_name_offset)
3749 if (l_name_offset > r_name_offset)
3755 * defrag a xattr bucket if we find that the bucket has some
3756 * holes beteen name/value pairs.
3757 * We will move all the name/value pairs to the end of the bucket
3758 * so that we can spare some space for insertion.
3760 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
3762 struct ocfs2_xattr_bucket *bucket)
3765 size_t end, offset, len, value_len;
3766 struct ocfs2_xattr_header *xh;
3767 char *entries, *buf, *bucket_buf = NULL;
3768 u64 blkno = bucket_blkno(bucket);
3770 size_t blocksize = inode->i_sb->s_blocksize;
3771 struct ocfs2_xattr_entry *xe;
3774 * In order to make the operation more efficient and generic,
3775 * we copy all the blocks into a contiguous memory and do the
3776 * defragment there, so if anything is error, we will not touch
3779 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3786 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3787 memcpy(buf, bucket_block(bucket, i), blocksize);
3789 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
3790 OCFS2_JOURNAL_ACCESS_WRITE);
3796 xh = (struct ocfs2_xattr_header *)bucket_buf;
3797 entries = (char *)xh->xh_entries;
3798 xh_free_start = le16_to_cpu(xh->xh_free_start);
3800 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3801 "xh_free_start = %u, xh_name_value_len = %u.\n",
3802 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3803 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
3806 * sort all the entries by their offset.
3807 * the largest will be the first, so that we can
3808 * move them to the end one by one.
3810 sort(entries, le16_to_cpu(xh->xh_count),
3811 sizeof(struct ocfs2_xattr_entry),
3812 cmp_xe_offset, swap_xe);
3814 /* Move all name/values to the end of the bucket. */
3815 xe = xh->xh_entries;
3816 end = OCFS2_XATTR_BUCKET_SIZE;
3817 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3818 offset = le16_to_cpu(xe->xe_name_offset);
3819 if (ocfs2_xattr_is_local(xe))
3820 value_len = OCFS2_XATTR_SIZE(
3821 le64_to_cpu(xe->xe_value_size));
3823 value_len = OCFS2_XATTR_ROOT_SIZE;
3824 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3827 * We must make sure that the name/value pair
3828 * exist in the same block. So adjust end to
3829 * the previous block end if needed.
3831 if (((end - len) / blocksize !=
3832 (end - 1) / blocksize))
3833 end = end - end % blocksize;
3835 if (end > offset + len) {
3836 memmove(bucket_buf + end - len,
3837 bucket_buf + offset, len);
3838 xe->xe_name_offset = cpu_to_le16(end - len);
3841 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3842 "bucket %llu\n", (unsigned long long)blkno);
3847 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3848 "bucket %llu\n", (unsigned long long)blkno);
3850 if (xh_free_start == end)
3853 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3854 xh->xh_free_start = cpu_to_le16(end);
3856 /* sort the entries by their name_hash. */
3857 sort(entries, le16_to_cpu(xh->xh_count),
3858 sizeof(struct ocfs2_xattr_entry),
3862 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3863 memcpy(bucket_block(bucket, i), buf, blocksize);
3864 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
3872 * prev_blkno points to the start of an existing extent. new_blkno
3873 * points to a newly allocated extent. Because we know each of our
3874 * clusters contains more than bucket, we can easily split one cluster
3875 * at a bucket boundary. So we take the last cluster of the existing
3876 * extent and split it down the middle. We move the last half of the
3877 * buckets in the last cluster of the existing extent over to the new
3880 * first_bh is the buffer at prev_blkno so we can update the existing
3881 * extent's bucket count. header_bh is the bucket were we were hoping
3882 * to insert our xattr. If the bucket move places the target in the new
3883 * extent, we'll update first_bh and header_bh after modifying the old
3886 * first_hash will be set as the 1st xe's name_hash in the new extent.
3888 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3890 struct ocfs2_xattr_bucket *first,
3891 struct ocfs2_xattr_bucket *target,
3897 struct super_block *sb = inode->i_sb;
3898 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
3899 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
3900 int to_move = num_buckets / 2;
3902 u64 last_cluster_blkno = bucket_blkno(first) +
3903 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
3905 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
3906 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
3908 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3909 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
3911 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
3912 last_cluster_blkno, new_blkno,
3913 to_move, first_hash);
3919 /* This is the first bucket that got moved */
3920 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
3923 * If the target bucket was part of the moved buckets, we need to
3924 * update first and target.
3926 if (bucket_blkno(target) >= src_blkno) {
3927 /* Find the block for the new target bucket */
3928 src_blkno = new_blkno +
3929 (bucket_blkno(target) - src_blkno);
3931 ocfs2_xattr_bucket_relse(first);
3932 ocfs2_xattr_bucket_relse(target);
3935 * These shouldn't fail - the buffers are in the
3936 * journal from ocfs2_cp_xattr_bucket().
3938 ret = ocfs2_read_xattr_bucket(first, new_blkno);
3943 ret = ocfs2_read_xattr_bucket(target, src_blkno);
3954 * Find the suitable pos when we divide a bucket into 2.
3955 * We have to make sure the xattrs with the same hash value exist
3956 * in the same bucket.
3958 * If this ocfs2_xattr_header covers more than one hash value, find a
3959 * place where the hash value changes. Try to find the most even split.
3960 * The most common case is that all entries have different hash values,
3961 * and the first check we make will find a place to split.
3963 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3965 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3966 int count = le16_to_cpu(xh->xh_count);
3967 int delta, middle = count / 2;
3970 * We start at the middle. Each step gets farther away in both
3971 * directions. We therefore hit the change in hash value
3972 * nearest to the middle. Note that this loop does not execute for
3975 for (delta = 0; delta < middle; delta++) {
3976 /* Let's check delta earlier than middle */
3977 if (cmp_xe(&entries[middle - delta - 1],
3978 &entries[middle - delta]))
3979 return middle - delta;
3981 /* For even counts, don't walk off the end */
3982 if ((middle + delta + 1) == count)
3985 /* Now try delta past middle */
3986 if (cmp_xe(&entries[middle + delta],
3987 &entries[middle + delta + 1]))
3988 return middle + delta + 1;
3991 /* Every entry had the same hash */
3996 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3997 * first_hash will record the 1st hash of the new bucket.
3999 * Normally half of the xattrs will be moved. But we have to make
4000 * sure that the xattrs with the same hash value are stored in the
4001 * same bucket. If all the xattrs in this bucket have the same hash
4002 * value, the new bucket will be initialized as an empty one and the
4003 * first_hash will be initialized as (hash_value+1).
4005 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4010 int new_bucket_head)
4013 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
4014 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4015 struct ocfs2_xattr_header *xh;
4016 struct ocfs2_xattr_entry *xe;
4017 int blocksize = inode->i_sb->s_blocksize;
4019 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4020 (unsigned long long)blk, (unsigned long long)new_blk);
4022 s_bucket = ocfs2_xattr_bucket_new(inode);
4023 t_bucket = ocfs2_xattr_bucket_new(inode);
4024 if (!s_bucket || !t_bucket) {
4030 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4036 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4037 OCFS2_JOURNAL_ACCESS_WRITE);
4044 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4045 * there's no need to read it.
4047 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4054 * Hey, if we're overwriting t_bucket, what difference does
4055 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4056 * same part of ocfs2_cp_xattr_bucket().
4058 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4060 OCFS2_JOURNAL_ACCESS_CREATE :
4061 OCFS2_JOURNAL_ACCESS_WRITE);
4067 xh = bucket_xh(s_bucket);
4068 count = le16_to_cpu(xh->xh_count);
4069 start = ocfs2_xattr_find_divide_pos(xh);
4071 if (start == count) {
4072 xe = &xh->xh_entries[start-1];
4075 * initialized a new empty bucket here.
4076 * The hash value is set as one larger than
4077 * that of the last entry in the previous bucket.
4079 for (i = 0; i < t_bucket->bu_blocks; i++)
4080 memset(bucket_block(t_bucket, i), 0, blocksize);
4082 xh = bucket_xh(t_bucket);
4083 xh->xh_free_start = cpu_to_le16(blocksize);
4084 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4085 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4087 goto set_num_buckets;
4090 /* copy the whole bucket to the new first. */
4091 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4093 /* update the new bucket. */
4094 xh = bucket_xh(t_bucket);
4097 * Calculate the total name/value len and xh_free_start for
4098 * the old bucket first.
4100 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4102 for (i = 0; i < start; i++) {
4103 xe = &xh->xh_entries[i];
4104 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
4105 if (ocfs2_xattr_is_local(xe))
4107 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4109 xe_len += OCFS2_XATTR_ROOT_SIZE;
4110 name_value_len += xe_len;
4111 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4112 name_offset = le16_to_cpu(xe->xe_name_offset);
4116 * Now begin the modification to the new bucket.
4118 * In the new bucket, We just move the xattr entry to the beginning
4119 * and don't touch the name/value. So there will be some holes in the
4120 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4123 xe = &xh->xh_entries[start];
4124 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4125 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
4126 (int)((char *)xe - (char *)xh),
4127 (int)((char *)xh->xh_entries - (char *)xh));
4128 memmove((char *)xh->xh_entries, (char *)xe, len);
4129 xe = &xh->xh_entries[count - start];
4130 len = sizeof(struct ocfs2_xattr_entry) * start;
4131 memset((char *)xe, 0, len);
4133 le16_add_cpu(&xh->xh_count, -start);
4134 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4136 /* Calculate xh_free_start for the new bucket. */
4137 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4138 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4139 xe = &xh->xh_entries[i];
4140 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
4141 if (ocfs2_xattr_is_local(xe))
4143 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4145 xe_len += OCFS2_XATTR_ROOT_SIZE;
4146 if (le16_to_cpu(xe->xe_name_offset) <
4147 le16_to_cpu(xh->xh_free_start))
4148 xh->xh_free_start = xe->xe_name_offset;
4152 /* set xh->xh_num_buckets for the new xh. */
4153 if (new_bucket_head)
4154 xh->xh_num_buckets = cpu_to_le16(1);
4156 xh->xh_num_buckets = 0;
4158 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4160 /* store the first_hash of the new bucket. */
4162 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4165 * Now only update the 1st block of the old bucket. If we
4166 * just added a new empty bucket, there is no need to modify
4172 xh = bucket_xh(s_bucket);
4173 memset(&xh->xh_entries[start], 0,
4174 sizeof(struct ocfs2_xattr_entry) * (count - start));
4175 xh->xh_count = cpu_to_le16(start);
4176 xh->xh_free_start = cpu_to_le16(name_offset);
4177 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4179 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4182 ocfs2_xattr_bucket_free(s_bucket);
4183 ocfs2_xattr_bucket_free(t_bucket);
4189 * Copy xattr from one bucket to another bucket.
4191 * The caller must make sure that the journal transaction
4192 * has enough space for journaling.
4194 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4201 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4203 BUG_ON(s_blkno == t_blkno);
4205 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4206 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4209 s_bucket = ocfs2_xattr_bucket_new(inode);
4210 t_bucket = ocfs2_xattr_bucket_new(inode);
4211 if (!s_bucket || !t_bucket) {
4217 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4222 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4223 * there's no need to read it.
4225 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4230 * Hey, if we're overwriting t_bucket, what difference does
4231 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4232 * cluster to fill, we came here from
4233 * ocfs2_mv_xattr_buckets(), and it is really new -
4234 * ACCESS_CREATE is required. But we also might have moved data
4235 * out of t_bucket before extending back into it.
4236 * ocfs2_add_new_xattr_bucket() can do this - its call to
4237 * ocfs2_add_new_xattr_cluster() may have created a new extent
4238 * and copied out the end of the old extent. Then it re-extends
4239 * the old extent back to create space for new xattrs. That's
4240 * how we get here, and the bucket isn't really new.
4242 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4244 OCFS2_JOURNAL_ACCESS_CREATE :
4245 OCFS2_JOURNAL_ACCESS_WRITE);
4249 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4250 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4253 ocfs2_xattr_bucket_free(t_bucket);
4254 ocfs2_xattr_bucket_free(s_bucket);
4260 * src_blk points to the start of an existing extent. last_blk points to
4261 * last cluster in that extent. to_blk points to a newly allocated
4262 * extent. We copy the buckets from the cluster at last_blk to the new
4263 * extent. If start_bucket is non-zero, we skip that many buckets before
4264 * we start copying. The new extent's xh_num_buckets gets set to the
4265 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4266 * by the same amount.
4268 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4269 u64 src_blk, u64 last_blk, u64 to_blk,
4270 unsigned int start_bucket,
4273 int i, ret, credits;
4274 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4275 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4276 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4277 struct ocfs2_xattr_bucket *old_first, *new_first;
4279 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4280 (unsigned long long)last_blk, (unsigned long long)to_blk);
4282 BUG_ON(start_bucket >= num_buckets);
4284 num_buckets -= start_bucket;
4285 last_blk += (start_bucket * blks_per_bucket);
4288 /* The first bucket of the original extent */
4289 old_first = ocfs2_xattr_bucket_new(inode);
4290 /* The first bucket of the new extent */
4291 new_first = ocfs2_xattr_bucket_new(inode);
4292 if (!old_first || !new_first) {
4298 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4305 * We need to update the first bucket of the old extent and all
4306 * the buckets going to the new extent.
4308 credits = ((num_buckets + 1) * blks_per_bucket) +
4309 handle->h_buffer_credits;
4310 ret = ocfs2_extend_trans(handle, credits);
4316 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4317 OCFS2_JOURNAL_ACCESS_WRITE);
4323 for (i = 0; i < num_buckets; i++) {
4324 ret = ocfs2_cp_xattr_bucket(inode, handle,
4325 last_blk + (i * blks_per_bucket),
4326 to_blk + (i * blks_per_bucket),
4335 * Get the new bucket ready before we dirty anything
4336 * (This actually shouldn't fail, because we already dirtied
4337 * it once in ocfs2_cp_xattr_bucket()).
4339 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4344 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4345 OCFS2_JOURNAL_ACCESS_WRITE);
4351 /* Now update the headers */
4352 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4353 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4355 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4356 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4359 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4362 ocfs2_xattr_bucket_free(new_first);
4363 ocfs2_xattr_bucket_free(old_first);
4368 * Move some xattrs in this cluster to the new cluster.
4369 * This function should only be called when bucket size == cluster size.
4370 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4372 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4378 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4379 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
4381 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4383 ret = ocfs2_extend_trans(handle, credits);
4389 /* Move half of the xattr in start_blk to the next bucket. */
4390 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4391 new_blk, first_hash, 1);
4395 * Move some xattrs from the old cluster to the new one since they are not
4396 * contiguous in ocfs2 xattr tree.
4398 * new_blk starts a new separate cluster, and we will move some xattrs from
4399 * prev_blk to it. v_start will be set as the first name hash value in this
4400 * new cluster so that it can be used as e_cpos during tree insertion and
4401 * don't collide with our original b-tree operations. first_bh and header_bh
4402 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4403 * to extend the insert bucket.
4405 * The problem is how much xattr should we move to the new one and when should
4406 * we update first_bh and header_bh?
4407 * 1. If cluster size > bucket size, that means the previous cluster has more
4408 * than 1 bucket, so just move half nums of bucket into the new cluster and
4409 * update the first_bh and header_bh if the insert bucket has been moved
4410 * to the new cluster.
4411 * 2. If cluster_size == bucket_size:
4412 * a) If the previous extent rec has more than one cluster and the insert
4413 * place isn't in the last cluster, copy the entire last cluster to the
4414 * new one. This time, we don't need to upate the first_bh and header_bh
4415 * since they will not be moved into the new cluster.
4416 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4417 * the new one. And we set the extend flag to zero if the insert place is
4418 * moved into the new allocated cluster since no extend is needed.
4420 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4422 struct ocfs2_xattr_bucket *first,
4423 struct ocfs2_xattr_bucket *target,
4431 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4432 (unsigned long long)bucket_blkno(first), prev_clusters,
4433 (unsigned long long)new_blk);
4435 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
4436 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4445 /* The start of the last cluster in the first extent */
4446 u64 last_blk = bucket_blkno(first) +
4447 ((prev_clusters - 1) *
4448 ocfs2_clusters_to_blocks(inode->i_sb, 1));
4450 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
4451 ret = ocfs2_mv_xattr_buckets(inode, handle,
4452 bucket_blkno(first),
4453 last_blk, new_blk, 0,
4458 ret = ocfs2_divide_xattr_cluster(inode, handle,
4464 if ((bucket_blkno(target) == last_blk) && extend)
4473 * Add a new cluster for xattr storage.
4475 * If the new cluster is contiguous with the previous one, it will be
4476 * appended to the same extent record, and num_clusters will be updated.
4477 * If not, we will insert a new extent for it and move some xattrs in
4478 * the last cluster into the new allocated one.
4479 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4480 * lose the benefits of hashing because we'll have to search large leaves.
4481 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4484 * first_bh is the first block of the previous extent rec and header_bh
4485 * indicates the bucket we will insert the new xattrs. They will be updated
4486 * when the header_bh is moved into the new cluster.
4488 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4489 struct buffer_head *root_bh,
4490 struct ocfs2_xattr_bucket *first,
4491 struct ocfs2_xattr_bucket *target,
4495 struct ocfs2_xattr_set_ctxt *ctxt)
4498 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4499 u32 prev_clusters = *num_clusters;
4500 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4502 handle_t *handle = ctxt->handle;
4503 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4504 struct ocfs2_extent_tree et;
4506 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4507 "previous xattr blkno = %llu\n",
4508 (unsigned long long)OCFS2_I(inode)->ip_blkno,
4509 prev_cpos, (unsigned long long)bucket_blkno(first));
4511 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
4513 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
4514 OCFS2_JOURNAL_ACCESS_WRITE);
4520 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
4521 clusters_to_add, &bit_off, &num_bits);
4528 BUG_ON(num_bits > clusters_to_add);
4530 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4531 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4532 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4534 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
4535 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4536 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4538 * If this cluster is contiguous with the old one and
4539 * adding this new cluster, we don't surpass the limit of
4540 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4541 * initialized and used like other buckets in the previous
4543 * So add it as a contiguous one. The caller will handle
4546 v_start = prev_cpos + prev_clusters;
4547 *num_clusters = prev_clusters + num_bits;
4548 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4551 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4565 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
4566 num_bits, (unsigned long long)block, v_start);
4567 ret = ocfs2_insert_extent(handle, &et, v_start, block,
4568 num_bits, 0, ctxt->meta_ac);
4574 ret = ocfs2_journal_dirty(handle, root_bh);
4583 * We are given an extent. 'first' is the bucket at the very front of
4584 * the extent. The extent has space for an additional bucket past
4585 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4586 * of the target bucket. We wish to shift every bucket past the target
4587 * down one, filling in that additional space. When we get back to the
4588 * target, we split the target between itself and the now-empty bucket
4589 * at target+1 (aka, target_blkno + blks_per_bucket).
4591 static int ocfs2_extend_xattr_bucket(struct inode *inode,
4593 struct ocfs2_xattr_bucket *first,
4598 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4599 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4601 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
4603 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
4604 "from %llu, len = %u\n", (unsigned long long)target_blk,
4605 (unsigned long long)bucket_blkno(first), num_clusters);
4607 /* The extent must have room for an additional bucket */
4608 BUG_ON(new_bucket >=
4609 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
4611 /* end_blk points to the last existing bucket */
4612 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
4615 * end_blk is the start of the last existing bucket.
4616 * Thus, (end_blk - target_blk) covers the target bucket and
4617 * every bucket after it up to, but not including, the last
4618 * existing bucket. Then we add the last existing bucket, the
4619 * new bucket, and the first bucket (3 * blk_per_bucket).
4621 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
4622 handle->h_buffer_credits;
4623 ret = ocfs2_extend_trans(handle, credits);
4629 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4630 OCFS2_JOURNAL_ACCESS_WRITE);
4636 while (end_blk != target_blk) {
4637 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4638 end_blk + blk_per_bucket, 0);
4641 end_blk -= blk_per_bucket;
4644 /* Move half of the xattr in target_blkno to the next bucket. */
4645 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4646 target_blk + blk_per_bucket, NULL, 0);
4648 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4649 ocfs2_xattr_bucket_journal_dirty(handle, first);
4656 * Add new xattr bucket in an extent record and adjust the buckets
4657 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
4658 * bucket we want to insert into.
4660 * In the easy case, we will move all the buckets after target down by
4661 * one. Half of target's xattrs will be moved to the next bucket.
4663 * If current cluster is full, we'll allocate a new one. This may not
4664 * be contiguous. The underlying calls will make sure that there is
4665 * space for the insert, shifting buckets around if necessary.
4666 * 'target' may be moved by those calls.
4668 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4669 struct buffer_head *xb_bh,
4670 struct ocfs2_xattr_bucket *target,
4671 struct ocfs2_xattr_set_ctxt *ctxt)
4673 struct ocfs2_xattr_block *xb =
4674 (struct ocfs2_xattr_block *)xb_bh->b_data;
4675 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4676 struct ocfs2_extent_list *el = &xb_root->xt_list;
4678 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
4679 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4680 int ret, num_buckets, extend = 1;
4682 u32 e_cpos, num_clusters;
4683 /* The bucket at the front of the extent */
4684 struct ocfs2_xattr_bucket *first;
4686 mlog(0, "Add new xattr bucket starting from %llu\n",
4687 (unsigned long long)bucket_blkno(target));
4689 /* The first bucket of the original extent */
4690 first = ocfs2_xattr_bucket_new(inode);
4697 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4704 ret = ocfs2_read_xattr_bucket(first, p_blkno);
4710 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
4711 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
4713 * This can move first+target if the target bucket moves
4714 * to the new extent.
4716 ret = ocfs2_add_new_xattr_cluster(inode,
4731 ret = ocfs2_extend_xattr_bucket(inode,
4734 bucket_blkno(target),
4741 ocfs2_xattr_bucket_free(first);
4746 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4747 struct ocfs2_xattr_bucket *bucket,
4750 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4752 offs = offs % inode->i_sb->s_blocksize;
4753 return bucket_block(bucket, block_off) + offs;
4757 * Handle the normal xattr set, including replace, delete and new.
4759 * Note: "local" indicates the real data's locality. So we can't
4760 * just its bucket locality by its length.
4762 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4763 struct ocfs2_xattr_info *xi,
4764 struct ocfs2_xattr_search *xs,
4768 struct ocfs2_xattr_entry *last, *xe;
4769 int name_len = strlen(xi->name);
4770 struct ocfs2_xattr_header *xh = xs->header;
4771 u16 count = le16_to_cpu(xh->xh_count), start;
4772 size_t blocksize = inode->i_sb->s_blocksize;
4774 size_t offs, size, new_size;
4776 last = &xh->xh_entries[count];
4777 if (!xs->not_found) {
4779 offs = le16_to_cpu(xe->xe_name_offset);
4780 if (ocfs2_xattr_is_local(xe))
4781 size = OCFS2_XATTR_SIZE(name_len) +
4782 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4784 size = OCFS2_XATTR_SIZE(name_len) +
4785 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4788 * If the new value will be stored outside, xi->value has been
4789 * initalized as an empty ocfs2_xattr_value_root, and the same
4790 * goes with xi->value_len, so we can set new_size safely here.
4791 * See ocfs2_xattr_set_in_bucket.
4793 new_size = OCFS2_XATTR_SIZE(name_len) +
4794 OCFS2_XATTR_SIZE(xi->value_len);
4796 le16_add_cpu(&xh->xh_name_value_len, -size);
4798 if (new_size > size)
4799 goto set_new_name_value;
4801 /* Now replace the old value with new one. */
4803 xe->xe_value_size = cpu_to_le64(xi->value_len);
4805 xe->xe_value_size = 0;
4807 val = ocfs2_xattr_bucket_get_val(inode,
4809 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4810 size - OCFS2_XATTR_SIZE(name_len));
4811 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4812 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4813 xi->value, xi->value_len);
4815 le16_add_cpu(&xh->xh_name_value_len, new_size);
4816 ocfs2_xattr_set_local(xe, local);
4820 * Remove the old entry if there is more than one.
4821 * We don't remove the last entry so that we can
4822 * use it to indicate the hash value of the empty
4826 le16_add_cpu(&xh->xh_count, -1);
4829 (void *)last - (void *)xe);
4831 sizeof(struct ocfs2_xattr_entry));
4834 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4839 /* find a new entry for insert. */
4840 int low = 0, high = count - 1, tmp;
4841 struct ocfs2_xattr_entry *tmp_xe;
4843 while (low <= high && count) {
4844 tmp = (low + high) / 2;
4845 tmp_xe = &xh->xh_entries[tmp];
4847 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4849 else if (name_hash <
4850 le32_to_cpu(tmp_xe->xe_name_hash))
4858 xe = &xh->xh_entries[low];
4860 memmove(xe + 1, xe, (void *)last - (void *)xe);
4862 le16_add_cpu(&xh->xh_count, 1);
4863 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4864 xe->xe_name_hash = cpu_to_le32(name_hash);
4865 xe->xe_name_len = name_len;
4866 ocfs2_xattr_set_type(xe, xi->name_index);
4870 /* Insert the new name+value. */
4871 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4874 * We must make sure that the name/value pair
4875 * exists in the same block.
4877 offs = le16_to_cpu(xh->xh_free_start);
4878 start = offs - size;
4880 if (start >> inode->i_sb->s_blocksize_bits !=
4881 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4882 offs = offs - offs % blocksize;
4883 xh->xh_free_start = cpu_to_le16(offs);
4886 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
4887 xe->xe_name_offset = cpu_to_le16(offs - size);
4889 memset(val, 0, size);
4890 memcpy(val, xi->name, name_len);
4891 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4893 xe->xe_value_size = cpu_to_le64(xi->value_len);
4894 ocfs2_xattr_set_local(xe, local);
4896 le16_add_cpu(&xh->xh_free_start, -size);
4897 le16_add_cpu(&xh->xh_name_value_len, size);
4903 * Set the xattr entry in the specified bucket.
4904 * The bucket is indicated by xs->bucket and it should have the enough
4905 * space for the xattr insertion.
4907 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4909 struct ocfs2_xattr_info *xi,
4910 struct ocfs2_xattr_search *xs,
4917 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4918 (unsigned long)xi->value_len, xi->name_index,
4919 (unsigned long long)bucket_blkno(xs->bucket));
4921 if (!xs->bucket->bu_bhs[1]) {
4922 blkno = bucket_blkno(xs->bucket);
4923 ocfs2_xattr_bucket_relse(xs->bucket);
4924 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
4931 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4932 OCFS2_JOURNAL_ACCESS_WRITE);
4938 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4939 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4946 * Truncate the specified xe_off entry in xattr bucket.
4947 * bucket is indicated by header_bh and len is the new length.
4948 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4950 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4952 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4953 struct ocfs2_xattr_bucket *bucket,
4956 struct ocfs2_xattr_set_ctxt *ctxt)
4960 struct ocfs2_xattr_entry *xe;
4961 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4962 size_t blocksize = inode->i_sb->s_blocksize;
4963 struct ocfs2_xattr_value_buf vb = {
4964 .vb_access = ocfs2_journal_access,
4967 xe = &xh->xh_entries[xe_off];
4969 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4971 offset = le16_to_cpu(xe->xe_name_offset) +
4972 OCFS2_XATTR_SIZE(xe->xe_name_len);
4974 value_blk = offset / blocksize;
4976 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4977 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4979 vb.vb_bh = bucket->bu_bhs[value_blk];
4982 vb.vb_xv = (struct ocfs2_xattr_value_root *)
4983 (vb.vb_bh->b_data + offset % blocksize);
4986 * From here on out we have to dirty the bucket. The generic
4987 * value calls only modify one of the bucket's bhs, but we need
4988 * to send the bucket at once. So if they error, they *could* have
4989 * modified something. We have to assume they did, and dirty
4990 * the whole bucket. This leaves us in a consistent state.
4992 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4993 xe_off, (unsigned long long)bucket_blkno(bucket), len);
4994 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5000 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5001 OCFS2_JOURNAL_ACCESS_WRITE);
5007 xe->xe_value_size = cpu_to_le64(len);
5009 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5015 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
5016 struct ocfs2_xattr_search *xs,
5018 struct ocfs2_xattr_set_ctxt *ctxt)
5021 struct ocfs2_xattr_entry *xe = xs->here;
5022 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
5024 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
5026 offset = xe - xh->xh_entries;
5027 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
5035 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
5037 struct ocfs2_xattr_search *xs,
5041 int ret, offset, block_off;
5042 struct ocfs2_xattr_value_root *xv;
5043 struct ocfs2_xattr_entry *xe = xs->here;
5044 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5046 struct ocfs2_xattr_value_buf vb = {
5047 .vb_access = ocfs2_journal_access,
5050 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
5052 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
5053 xe - xh->xh_entries,
5061 base = bucket_block(xs->bucket, block_off);
5062 xv = (struct ocfs2_xattr_value_root *)(base + offset +
5063 OCFS2_XATTR_SIZE(xe->xe_name_len));
5066 vb.vb_bh = xs->bucket->bu_bhs[block_off];
5067 ret = __ocfs2_xattr_set_value_outside(inode, handle,
5068 &vb, val, value_len);
5075 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5076 struct buffer_head *root_bh,
5083 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5084 struct inode *tl_inode = osb->osb_tl_inode;
5086 struct ocfs2_xattr_block *xb =
5087 (struct ocfs2_xattr_block *)root_bh->b_data;
5088 struct ocfs2_alloc_context *meta_ac = NULL;
5089 struct ocfs2_cached_dealloc_ctxt dealloc;
5090 struct ocfs2_extent_tree et;
5092 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5093 ocfs2_delete_xattr_in_bucket, para);
5099 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5101 ocfs2_init_dealloc_ctxt(&dealloc);
5103 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5104 cpos, len, (unsigned long long)blkno);
5106 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5109 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5115 mutex_lock(&tl_inode->i_mutex);
5117 if (ocfs2_truncate_log_needs_flush(osb)) {
5118 ret = __ocfs2_flush_truncate_log(osb);
5125 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5126 if (IS_ERR(handle)) {
5132 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5133 OCFS2_JOURNAL_ACCESS_WRITE);
5139 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5146 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5148 ret = ocfs2_journal_dirty(handle, root_bh);
5154 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5159 ocfs2_commit_trans(osb, handle);
5161 ocfs2_schedule_truncate_log_flush(osb, 1);
5163 mutex_unlock(&tl_inode->i_mutex);
5166 ocfs2_free_alloc_context(meta_ac);
5168 ocfs2_run_deallocs(osb, &dealloc);
5173 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
5175 struct ocfs2_xattr_search *xs)
5177 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5178 struct ocfs2_xattr_entry *last = &xh->xh_entries[
5179 le16_to_cpu(xh->xh_count) - 1];
5182 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
5183 OCFS2_JOURNAL_ACCESS_WRITE);
5189 /* Remove the old entry. */
5190 memmove(xs->here, xs->here + 1,
5191 (void *)last - (void *)xs->here);
5192 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
5193 le16_add_cpu(&xh->xh_count, -1);
5195 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
5199 * Set the xattr name/value in the bucket specified in xs.
5201 * As the new value in xi may be stored in the bucket or in an outside cluster,
5202 * we divide the whole process into 3 steps:
5203 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
5204 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
5205 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
5206 * 4. If the clusters for the new outside value can't be allocated, we need
5207 * to free the xattr we allocated in set.
5209 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5210 struct ocfs2_xattr_info *xi,
5211 struct ocfs2_xattr_search *xs,
5212 struct ocfs2_xattr_set_ctxt *ctxt)
5216 char *val = (char *)xi->value;
5217 struct ocfs2_xattr_entry *xe = xs->here;
5218 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
5221 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
5223 * We need to truncate the xattr storage first.
5225 * If both the old and new value are stored to
5226 * outside block, we only need to truncate
5227 * the storage and then set the value outside.
5229 * If the new value should be stored within block,
5230 * we should free all the outside block first and
5231 * the modification to the xattr block will be done
5232 * by following steps.
5234 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5235 value_len = xi->value_len;
5239 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5246 goto set_value_outside;
5249 value_len = xi->value_len;
5250 /* So we have to handle the inside block change now. */
5251 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
5253 * If the new value will be stored outside of block,
5254 * initalize a new empty value root and insert it first.
5257 xi->value = &def_xv;
5258 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
5261 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
5268 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
5271 /* allocate the space now for the outside block storage. */
5272 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5277 if (xs->not_found) {
5279 * We can't allocate enough clusters for outside
5280 * storage and we have allocated xattr already,
5281 * so need to remove it.
5283 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
5289 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
5290 xs, val, value_len);
5296 * check whether the xattr bucket is filled up with the same hash value.
5297 * If we want to insert the xattr with the same hash, return -ENOSPC.
5298 * If we want to insert a xattr with different hash value, go ahead
5299 * and ocfs2_divide_xattr_bucket will handle this.
5301 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5302 struct ocfs2_xattr_bucket *bucket,
5305 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5306 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5308 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5311 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5312 xh->xh_entries[0].xe_name_hash) {
5313 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5315 (unsigned long long)bucket_blkno(bucket),
5316 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5323 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5324 struct ocfs2_xattr_info *xi,
5325 struct ocfs2_xattr_search *xs,
5326 struct ocfs2_xattr_set_ctxt *ctxt)
5328 struct ocfs2_xattr_header *xh;
5329 struct ocfs2_xattr_entry *xe;
5330 u16 count, header_size, xh_free_start;
5331 int free, max_free, need, old;
5332 size_t value_size = 0, name_len = strlen(xi->name);
5333 size_t blocksize = inode->i_sb->s_blocksize;
5334 int ret, allocation = 0;
5336 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
5340 count = le16_to_cpu(xh->xh_count);
5341 xh_free_start = le16_to_cpu(xh->xh_free_start);
5342 header_size = sizeof(struct ocfs2_xattr_header) +
5343 count * sizeof(struct ocfs2_xattr_entry);
5344 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5345 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
5347 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5348 "of %u which exceed block size\n",
5349 (unsigned long long)bucket_blkno(xs->bucket),
5352 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5353 value_size = OCFS2_XATTR_ROOT_SIZE;
5355 value_size = OCFS2_XATTR_SIZE(xi->value_len);
5358 need = sizeof(struct ocfs2_xattr_entry) +
5359 OCFS2_XATTR_SIZE(name_len) + value_size;
5361 need = value_size + OCFS2_XATTR_SIZE(name_len);
5364 * We only replace the old value if the new length is smaller
5365 * than the old one. Otherwise we will allocate new space in the
5366 * bucket to store it.
5369 if (ocfs2_xattr_is_local(xe))
5370 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5372 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5374 if (old >= value_size)
5378 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
5380 * We need to make sure the new name/value pair
5381 * can exist in the same block.
5383 if (xh_free_start % blocksize < need)
5384 free -= xh_free_start % blocksize;
5386 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5387 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5388 " %u\n", xs->not_found,
5389 (unsigned long long)bucket_blkno(xs->bucket),
5390 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5391 le16_to_cpu(xh->xh_name_value_len));
5395 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
5396 if (need <= max_free &&
5397 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5399 * We can create the space by defragment. Since only the
5400 * name/value will be moved, the xe shouldn't be changed
5403 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5410 xh_free_start = le16_to_cpu(xh->xh_free_start);
5411 free = xh_free_start - header_size
5412 - OCFS2_XATTR_HEADER_GAP;
5413 if (xh_free_start % blocksize < need)
5414 free -= xh_free_start % blocksize;
5419 mlog(0, "Can't get enough space for xattr insert by "
5420 "defragment. Need %u bytes, but we have %d, so "
5421 "allocate new bucket for it.\n", need, free);
5425 * We have to add new buckets or clusters and one
5426 * allocation should leave us enough space for insert.
5431 * We do not allow for overlapping ranges between buckets. And
5432 * the maximum number of collisions we will allow for then is
5433 * one bucket's worth, so check it here whether we need to
5434 * add a new bucket for the insert.
5436 ret = ocfs2_check_xattr_bucket_collision(inode,
5444 ret = ocfs2_add_new_xattr_bucket(inode,
5454 * ocfs2_add_new_xattr_bucket() will have updated
5455 * xs->bucket if it moved, but it will not have updated
5456 * any of the other search fields. Thus, we drop it and
5457 * re-search. Everything should be cached, so it'll be
5460 ocfs2_xattr_bucket_relse(xs->bucket);
5461 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5464 if (ret && ret != -ENODATA)
5466 xs->not_found = ret;
5472 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
5478 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5479 struct ocfs2_xattr_bucket *bucket,
5482 int ret = 0, ref_credits;
5483 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5485 struct ocfs2_xattr_entry *xe;
5486 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5487 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5488 int credits = ocfs2_remove_extent_credits(osb->sb) +
5489 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5490 struct ocfs2_xattr_value_root *xv;
5491 struct ocfs2_rm_xattr_bucket_para *args =
5492 (struct ocfs2_rm_xattr_bucket_para *)para;
5494 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5496 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5497 xe = &xh->xh_entries[i];
5498 if (ocfs2_xattr_is_local(xe))
5501 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5504 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5510 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5511 if (IS_ERR(ctxt.handle)) {
5512 ret = PTR_ERR(ctxt.handle);
5517 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5520 ocfs2_commit_trans(osb, ctxt.handle);
5522 ocfs2_free_alloc_context(ctxt.meta_ac);
5523 ctxt.meta_ac = NULL;
5532 ocfs2_free_alloc_context(ctxt.meta_ac);
5533 ocfs2_schedule_truncate_log_flush(osb, 1);
5534 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5539 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5540 * or change the extent record flag), we need to recalculate
5541 * the metaecc for the whole bucket. So it is done here.
5544 * We have to give the extra credits for the caller.
5546 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5551 struct ocfs2_xattr_bucket *bucket =
5552 (struct ocfs2_xattr_bucket *)para;
5554 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5555 OCFS2_JOURNAL_ACCESS_WRITE);
5561 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5567 * Special action we need if the xattr value is refcounted.
5569 * 1. If the xattr is refcounted, lock the tree.
5570 * 2. CoW the xattr if we are setting the new value and the value
5571 * will be stored outside.
5572 * 3. In other case, decrease_refcount will work for us, so just
5573 * lock the refcount tree, calculate the meta and credits is OK.
5575 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5576 * currently CoW is a completed transaction, while this function
5577 * will also lock the allocators and let us deadlock. So we will
5578 * CoW the whole xattr value.
5580 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5581 struct ocfs2_dinode *di,
5582 struct ocfs2_xattr_info *xi,
5583 struct ocfs2_xattr_search *xis,
5584 struct ocfs2_xattr_search *xbs,
5585 struct ocfs2_refcount_tree **ref_tree,
5590 struct ocfs2_xattr_block *xb;
5591 struct ocfs2_xattr_entry *xe;
5593 u32 p_cluster, num_clusters;
5594 unsigned int ext_flags;
5595 int name_offset, name_len;
5596 struct ocfs2_xattr_value_buf vb;
5597 struct ocfs2_xattr_bucket *bucket = NULL;
5598 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5599 struct ocfs2_post_refcount refcount;
5600 struct ocfs2_post_refcount *p = NULL;
5601 struct buffer_head *ref_root_bh = NULL;
5603 if (!xis->not_found) {
5605 name_offset = le16_to_cpu(xe->xe_name_offset);
5606 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5608 vb.vb_bh = xis->inode_bh;
5609 vb.vb_access = ocfs2_journal_access_di;
5611 int i, block_off = 0;
5612 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5614 name_offset = le16_to_cpu(xe->xe_name_offset);
5615 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5616 i = xbs->here - xbs->header->xh_entries;
5618 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5619 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5620 bucket_xh(xbs->bucket),
5627 base = bucket_block(xbs->bucket, block_off);
5628 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5629 vb.vb_access = ocfs2_journal_access;
5631 if (ocfs2_meta_ecc(osb)) {
5632 /*create parameters for ocfs2_post_refcount. */
5633 bucket = xbs->bucket;
5634 refcount.credits = bucket->bu_blocks;
5635 refcount.para = bucket;
5637 ocfs2_xattr_bucket_post_refcount;
5642 vb.vb_bh = xbs->xattr_bh;
5643 vb.vb_access = ocfs2_journal_access_xb;
5647 if (ocfs2_xattr_is_local(xe))
5650 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5651 (base + name_offset + name_len);
5653 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5654 &num_clusters, &vb.vb_xv->xr_list,
5662 * We just need to check the 1st extent record, since we always
5663 * CoW the whole xattr. So there shouldn't be a xattr with
5664 * some REFCOUNT extent recs after the 1st one.
5666 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5669 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5670 1, ref_tree, &ref_root_bh);
5677 * If we are deleting the xattr or the new size will be stored inside,
5678 * cool, leave it there, the xattr truncate process will remove them
5679 * for us(it still needs the refcount tree lock and the meta, credits).
5680 * And the worse case is that every cluster truncate will split the
5681 * refcount tree, and make the original extent become 3. So we will need
5682 * 2 * cluster more extent recs at most.
5684 if (!xi->value || xi->value_len <= OCFS2_XATTR_INLINE_SIZE) {
5686 ret = ocfs2_refcounted_xattr_delete_need(inode,
5687 &(*ref_tree)->rf_ci,
5688 ref_root_bh, vb.vb_xv,
5695 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5696 *ref_tree, ref_root_bh, 0,
5697 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5702 brelse(ref_root_bh);
5707 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5708 * The physical clusters will be added to refcount tree.
5710 static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5711 struct ocfs2_xattr_value_root *xv,
5712 struct ocfs2_extent_tree *value_et,
5713 struct ocfs2_caching_info *ref_ci,
5714 struct buffer_head *ref_root_bh,
5715 struct ocfs2_cached_dealloc_ctxt *dealloc,
5716 struct ocfs2_post_refcount *refcount)
5719 u32 clusters = le32_to_cpu(xv->xr_clusters);
5720 u32 cpos, p_cluster, num_clusters;
5721 struct ocfs2_extent_list *el = &xv->xr_list;
5722 unsigned int ext_flags;
5725 while (cpos < clusters) {
5726 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5727 &num_clusters, el, &ext_flags);
5729 cpos += num_clusters;
5730 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5735 ret = ocfs2_add_refcount_flag(inode, value_et,
5736 ref_ci, ref_root_bh,
5737 cpos - num_clusters,
5738 p_cluster, num_clusters,
5750 * Given a normal ocfs2_xattr_header, refcount all the entries which
5751 * have value stored outside.
5752 * Used for xattrs stored in inode and ocfs2_xattr_block.
5754 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5755 struct ocfs2_xattr_value_buf *vb,
5756 struct ocfs2_xattr_header *header,
5757 struct ocfs2_caching_info *ref_ci,
5758 struct buffer_head *ref_root_bh,
5759 struct ocfs2_cached_dealloc_ctxt *dealloc)
5762 struct ocfs2_xattr_entry *xe;
5763 struct ocfs2_xattr_value_root *xv;
5764 struct ocfs2_extent_tree et;
5767 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5768 xe = &header->xh_entries[i];
5770 if (ocfs2_xattr_is_local(xe))
5773 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5774 le16_to_cpu(xe->xe_name_offset) +
5775 OCFS2_XATTR_SIZE(xe->xe_name_len));
5778 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5780 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5781 ref_ci, ref_root_bh,
5792 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5793 struct buffer_head *fe_bh,
5794 struct ocfs2_caching_info *ref_ci,
5795 struct buffer_head *ref_root_bh,
5796 struct ocfs2_cached_dealloc_ctxt *dealloc)
5798 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5799 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5800 (fe_bh->b_data + inode->i_sb->s_blocksize -
5801 le16_to_cpu(di->i_xattr_inline_size));
5802 struct ocfs2_xattr_value_buf vb = {
5804 .vb_access = ocfs2_journal_access_di,
5807 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5808 ref_ci, ref_root_bh, dealloc);
5811 struct ocfs2_xattr_tree_value_refcount_para {
5812 struct ocfs2_caching_info *ref_ci;
5813 struct buffer_head *ref_root_bh;
5814 struct ocfs2_cached_dealloc_ctxt *dealloc;
5817 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5818 struct ocfs2_xattr_bucket *bucket,
5820 struct ocfs2_xattr_value_root **xv,
5821 struct buffer_head **bh)
5823 int ret, block_off, name_offset;
5824 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5825 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5828 ret = ocfs2_xattr_bucket_get_name_value(sb,
5838 base = bucket_block(bucket, block_off);
5840 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
5841 OCFS2_XATTR_SIZE(xe->xe_name_len));
5844 *bh = bucket->bu_bhs[block_off];
5850 * For a given xattr bucket, refcount all the entries which
5851 * have value stored outside.
5853 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
5854 struct ocfs2_xattr_bucket *bucket,
5858 struct ocfs2_extent_tree et;
5859 struct ocfs2_xattr_tree_value_refcount_para *ref =
5860 (struct ocfs2_xattr_tree_value_refcount_para *)para;
5861 struct ocfs2_xattr_header *xh =
5862 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
5863 struct ocfs2_xattr_entry *xe;
5864 struct ocfs2_xattr_value_buf vb = {
5865 .vb_access = ocfs2_journal_access,
5867 struct ocfs2_post_refcount refcount = {
5868 .credits = bucket->bu_blocks,
5870 .func = ocfs2_xattr_bucket_post_refcount,
5872 struct ocfs2_post_refcount *p = NULL;
5874 /* We only need post_refcount if we support metaecc. */
5875 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
5878 mlog(0, "refcount bucket %llu, count = %u\n",
5879 (unsigned long long)bucket_blkno(bucket),
5880 le16_to_cpu(xh->xh_count));
5881 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5882 xe = &xh->xh_entries[i];
5884 if (ocfs2_xattr_is_local(xe))
5887 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
5888 &vb.vb_xv, &vb.vb_bh);
5894 ocfs2_init_xattr_value_extent_tree(&et,
5895 INODE_CACHE(inode), &vb);
5897 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
5911 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
5912 struct buffer_head *root_bh,
5913 u64 blkno, u32 cpos, u32 len, void *para)
5915 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
5916 ocfs2_xattr_bucket_value_refcount,
5920 static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
5921 struct buffer_head *blk_bh,
5922 struct ocfs2_caching_info *ref_ci,
5923 struct buffer_head *ref_root_bh,
5924 struct ocfs2_cached_dealloc_ctxt *dealloc)
5927 struct ocfs2_xattr_block *xb =
5928 (struct ocfs2_xattr_block *)blk_bh->b_data;
5930 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
5931 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
5932 struct ocfs2_xattr_value_buf vb = {
5934 .vb_access = ocfs2_journal_access_xb,
5937 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5938 ref_ci, ref_root_bh,
5941 struct ocfs2_xattr_tree_value_refcount_para para = {
5943 .ref_root_bh = ref_root_bh,
5947 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
5948 ocfs2_refcount_xattr_tree_rec,
5955 int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
5956 struct buffer_head *fe_bh,
5957 struct ocfs2_caching_info *ref_ci,
5958 struct buffer_head *ref_root_bh,
5959 struct ocfs2_cached_dealloc_ctxt *dealloc)
5962 struct ocfs2_inode_info *oi = OCFS2_I(inode);
5963 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5964 struct buffer_head *blk_bh = NULL;
5966 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
5967 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
5968 ref_ci, ref_root_bh,
5976 if (!di->i_xattr_loc)
5979 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
5986 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
5987 ref_root_bh, dealloc);
5998 * Store the information we need in xattr reflink.
5999 * old_bh and new_bh are inode bh for the old and new inode.
6001 struct ocfs2_xattr_reflink {
6002 struct inode *old_inode;
6003 struct inode *new_inode;
6004 struct buffer_head *old_bh;
6005 struct buffer_head *new_bh;
6006 struct ocfs2_caching_info *ref_ci;
6007 struct buffer_head *ref_root_bh;
6008 struct ocfs2_cached_dealloc_ctxt *dealloc;
6012 * Given a xattr header and xe offset,
6013 * return the proper xv and the corresponding bh.
6014 * xattr in inode, block and xattr tree have different implementaions.
6016 typedef int (get_xattr_value_root)(struct super_block *sb,
6017 struct buffer_head *bh,
6018 struct ocfs2_xattr_header *xh,
6020 struct ocfs2_xattr_value_root **xv,
6021 struct buffer_head **ret_bh,
6025 * Calculate all the xattr value root metadata stored in this xattr header and
6026 * credits we need if we create them from the scratch.
6027 * We use get_xattr_value_root so that all types of xattr container can use it.
6029 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6030 struct buffer_head *bh,
6031 struct ocfs2_xattr_header *xh,
6032 int *metas, int *credits,
6034 get_xattr_value_root *func,
6038 struct ocfs2_xattr_value_root *xv;
6039 struct ocfs2_xattr_entry *xe;
6041 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6042 xe = &xh->xh_entries[i];
6043 if (ocfs2_xattr_is_local(xe))
6046 ret = func(sb, bh, xh, i, &xv, NULL, para);
6052 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6053 le16_to_cpu(xv->xr_list.l_next_free_rec);
6055 *credits += ocfs2_calc_extend_credits(sb,
6057 le32_to_cpu(xv->xr_clusters));
6060 * If the value is a tree with depth > 1, We don't go deep
6061 * to the extent block, so just calculate a maximum record num.
6063 if (!xv->xr_list.l_tree_depth)
6064 *num_recs += xv->xr_list.l_next_free_rec;
6066 *num_recs += ocfs2_clusters_for_bytes(sb,
6073 /* Used by xattr inode and block to return the right xv and buffer_head. */
6074 static int ocfs2_get_xattr_value_root(struct super_block *sb,
6075 struct buffer_head *bh,
6076 struct ocfs2_xattr_header *xh,
6078 struct ocfs2_xattr_value_root **xv,
6079 struct buffer_head **ret_bh,
6082 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6084 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6085 le16_to_cpu(xe->xe_name_offset) +
6086 OCFS2_XATTR_SIZE(xe->xe_name_len));
6095 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6096 * It is only used for inline xattr and xattr block.
6098 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6099 struct ocfs2_xattr_header *xh,
6100 struct buffer_head *ref_root_bh,
6102 struct ocfs2_alloc_context **meta_ac)
6104 int ret, meta_add = 0, num_recs = 0;
6105 struct ocfs2_refcount_block *rb =
6106 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6110 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6111 &meta_add, credits, &num_recs,
6112 ocfs2_get_xattr_value_root,
6120 * We need to add/modify num_recs in refcount tree, so just calculate
6121 * an approximate number we need for refcount tree change.
6122 * Sometimes we need to split the tree, and after split, half recs
6123 * will be moved to the new block, and a new block can only provide
6124 * half number of recs. So we multiple new blocks by 2.
6126 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6127 meta_add += num_recs;
6128 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6129 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6130 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6131 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6135 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6144 * Given a xattr header, reflink all the xattrs in this container.
6145 * It can be used for inode, block and bucket.
6148 * Before we call this function, the caller has memcpy the xattr in
6149 * old_xh to the new_xh.
6151 static int ocfs2_reflink_xattr_header(handle_t *handle,
6152 struct ocfs2_xattr_reflink *args,
6153 struct buffer_head *old_bh,
6154 struct ocfs2_xattr_header *xh,
6155 struct buffer_head *new_bh,
6156 struct ocfs2_xattr_header *new_xh,
6157 struct ocfs2_xattr_value_buf *vb,
6158 struct ocfs2_alloc_context *meta_ac,
6159 get_xattr_value_root *func,
6163 struct super_block *sb = args->old_inode->i_sb;
6164 struct buffer_head *value_bh;
6165 struct ocfs2_xattr_entry *xe;
6166 struct ocfs2_xattr_value_root *xv, *new_xv;
6167 struct ocfs2_extent_tree data_et;
6168 u32 clusters, cpos, p_cluster, num_clusters;
6169 unsigned int ext_flags = 0;
6171 mlog(0, "reflink xattr in container %llu, count = %u\n",
6172 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
6173 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6174 xe = &xh->xh_entries[i];
6176 if (ocfs2_xattr_is_local(xe))
6179 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6185 ret = func(sb, new_bh, new_xh, i, &new_xv, &value_bh, para);
6192 * For the xattr which has l_tree_depth = 0, all the extent
6193 * recs have already be copied to the new xh with the
6194 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6195 * increase the refount count int the refcount tree.
6197 * For the xattr which has l_tree_depth > 0, we need
6198 * to initialize it to the empty default value root,
6199 * and then insert the extents one by one.
6201 if (xv->xr_list.l_tree_depth) {
6202 memcpy(new_xv, &def_xv, sizeof(def_xv));
6204 vb->vb_bh = value_bh;
6205 ocfs2_init_xattr_value_extent_tree(&data_et,
6206 INODE_CACHE(args->new_inode), vb);
6209 clusters = le32_to_cpu(xv->xr_clusters);
6211 while (cpos < clusters) {
6212 ret = ocfs2_xattr_get_clusters(args->old_inode,
6225 if (xv->xr_list.l_tree_depth) {
6226 ret = ocfs2_insert_extent(handle,
6228 ocfs2_clusters_to_blocks(
6229 args->old_inode->i_sb,
6231 num_clusters, ext_flags,
6239 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6241 p_cluster, num_clusters,
6242 meta_ac, args->dealloc);
6248 cpos += num_clusters;
6256 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6258 int ret = 0, credits = 0;
6260 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6261 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6262 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6263 int header_off = osb->sb->s_blocksize - inline_size;
6264 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6265 (args->old_bh->b_data + header_off);
6266 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6267 (args->new_bh->b_data + header_off);
6268 struct ocfs2_alloc_context *meta_ac = NULL;
6269 struct ocfs2_inode_info *new_oi;
6270 struct ocfs2_dinode *new_di;
6271 struct ocfs2_xattr_value_buf vb = {
6272 .vb_bh = args->new_bh,
6273 .vb_access = ocfs2_journal_access_di,
6276 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6277 &credits, &meta_ac);
6283 handle = ocfs2_start_trans(osb, credits);
6284 if (IS_ERR(handle)) {
6285 ret = PTR_ERR(handle);
6290 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6291 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6297 memcpy(args->new_bh->b_data + header_off,
6298 args->old_bh->b_data + header_off, inline_size);
6300 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6301 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6303 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6304 args->new_bh, new_xh, &vb, meta_ac,
6305 ocfs2_get_xattr_value_root, NULL);
6311 new_oi = OCFS2_I(args->new_inode);
6312 spin_lock(&new_oi->ip_lock);
6313 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6314 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6315 spin_unlock(&new_oi->ip_lock);
6317 ocfs2_journal_dirty(handle, args->new_bh);
6320 ocfs2_commit_trans(osb, handle);
6324 ocfs2_free_alloc_context(meta_ac);
6328 static int ocfs2_create_empty_xattr_block(struct inode *inode,
6329 struct buffer_head *fe_bh,
6330 struct buffer_head **ret_bh,
6335 struct ocfs2_alloc_context *meta_ac;
6336 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6338 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
6344 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6345 if (IS_ERR(handle)) {
6346 ret = PTR_ERR(handle);
6351 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6352 (unsigned long long)fe_bh->b_blocknr, indexed);
6353 ret = ocfs2_create_xattr_block(handle, inode, fe_bh,
6354 meta_ac, ret_bh, indexed);
6358 ocfs2_commit_trans(osb, handle);
6360 ocfs2_free_alloc_context(meta_ac);
6364 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6365 struct buffer_head *blk_bh,
6366 struct buffer_head *new_blk_bh)
6368 int ret = 0, credits = 0;
6370 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6371 struct ocfs2_dinode *new_di;
6372 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6373 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6374 struct ocfs2_xattr_block *xb =
6375 (struct ocfs2_xattr_block *)blk_bh->b_data;
6376 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6377 struct ocfs2_xattr_block *new_xb =
6378 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6379 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6380 struct ocfs2_alloc_context *meta_ac;
6381 struct ocfs2_xattr_value_buf vb = {
6382 .vb_bh = new_blk_bh,
6383 .vb_access = ocfs2_journal_access_xb,
6386 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6387 &credits, &meta_ac);
6393 /* One more credits in case we need to add xattr flags in new inode. */
6394 handle = ocfs2_start_trans(osb, credits + 1);
6395 if (IS_ERR(handle)) {
6396 ret = PTR_ERR(handle);
6401 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6402 ret = ocfs2_journal_access_di(handle,
6403 INODE_CACHE(args->new_inode),
6405 OCFS2_JOURNAL_ACCESS_WRITE);
6412 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6413 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6419 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6420 osb->sb->s_blocksize - header_off);
6422 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6423 new_blk_bh, new_xh, &vb, meta_ac,
6424 ocfs2_get_xattr_value_root, NULL);
6430 ocfs2_journal_dirty(handle, new_blk_bh);
6432 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6433 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6434 spin_lock(&new_oi->ip_lock);
6435 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6436 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6437 spin_unlock(&new_oi->ip_lock);
6439 ocfs2_journal_dirty(handle, args->new_bh);
6443 ocfs2_commit_trans(osb, handle);
6446 ocfs2_free_alloc_context(meta_ac);
6450 struct ocfs2_reflink_xattr_tree_args {
6451 struct ocfs2_xattr_reflink *reflink;
6452 struct buffer_head *old_blk_bh;
6453 struct buffer_head *new_blk_bh;
6454 struct ocfs2_xattr_bucket *old_bucket;
6455 struct ocfs2_xattr_bucket *new_bucket;
6460 * We have to handle the case that both old bucket and new bucket
6461 * will call this function to get the right ret_bh.
6462 * So The caller must give us the right bh.
6464 static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6465 struct buffer_head *bh,
6466 struct ocfs2_xattr_header *xh,
6468 struct ocfs2_xattr_value_root **xv,
6469 struct buffer_head **ret_bh,
6472 struct ocfs2_reflink_xattr_tree_args *args =
6473 (struct ocfs2_reflink_xattr_tree_args *)para;
6474 struct ocfs2_xattr_bucket *bucket;
6476 if (bh == args->old_bucket->bu_bhs[0])
6477 bucket = args->old_bucket;
6479 bucket = args->new_bucket;
6481 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6485 struct ocfs2_value_tree_metas {
6491 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6492 struct buffer_head *bh,
6493 struct ocfs2_xattr_header *xh,
6495 struct ocfs2_xattr_value_root **xv,
6496 struct buffer_head **ret_bh,
6499 struct ocfs2_xattr_bucket *bucket =
6500 (struct ocfs2_xattr_bucket *)para;
6502 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6506 static int ocfs2_calc_value_tree_metas(struct inode *inode,
6507 struct ocfs2_xattr_bucket *bucket,
6510 struct ocfs2_value_tree_metas *metas =
6511 (struct ocfs2_value_tree_metas *)para;
6512 struct ocfs2_xattr_header *xh =
6513 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6515 /* Add the credits for this bucket first. */
6516 metas->credits += bucket->bu_blocks;
6517 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6518 xh, &metas->num_metas,
6519 &metas->credits, &metas->num_recs,
6520 ocfs2_value_tree_metas_in_bucket,
6525 * Given a xattr extent rec starting from blkno and having len clusters,
6526 * iterate all the buckets calculate how much metadata we need for reflinking
6527 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6529 static int ocfs2_lock_reflink_xattr_rec_allocators(
6530 struct ocfs2_reflink_xattr_tree_args *args,
6531 struct ocfs2_extent_tree *xt_et,
6532 u64 blkno, u32 len, int *credits,
6533 struct ocfs2_alloc_context **meta_ac,
6534 struct ocfs2_alloc_context **data_ac)
6536 int ret, num_free_extents;
6537 struct ocfs2_value_tree_metas metas;
6538 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6539 struct ocfs2_refcount_block *rb;
6541 memset(&metas, 0, sizeof(metas));
6543 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6544 ocfs2_calc_value_tree_metas, &metas);
6550 *credits = metas.credits;
6553 * Calculate we need for refcount tree change.
6555 * We need to add/modify num_recs in refcount tree, so just calculate
6556 * an approximate number we need for refcount tree change.
6557 * Sometimes we need to split the tree, and after split, half recs
6558 * will be moved to the new block, and a new block can only provide
6559 * half number of recs. So we multiple new blocks by 2.
6560 * In the end, we have to add credits for modifying the already
6561 * existed refcount block.
6563 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6565 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6566 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6567 metas.num_metas += metas.num_recs;
6568 *credits += metas.num_recs +
6569 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6570 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6571 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6572 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6576 /* count in the xattr tree change. */
6577 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6578 if (num_free_extents < 0) {
6579 ret = num_free_extents;
6584 if (num_free_extents < len)
6585 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6587 *credits += ocfs2_calc_extend_credits(osb->sb,
6588 xt_et->et_root_el, len);
6590 if (metas.num_metas) {
6591 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6600 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6607 ocfs2_free_alloc_context(*meta_ac);
6615 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6616 u64 blkno, u64 new_blkno, u32 clusters,
6617 struct ocfs2_alloc_context *meta_ac,
6618 struct ocfs2_alloc_context *data_ac,
6619 struct ocfs2_reflink_xattr_tree_args *args)
6622 struct super_block *sb = args->reflink->old_inode->i_sb;
6623 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6624 u32 num_buckets = clusters * bpc;
6625 int bpb = args->old_bucket->bu_blocks;
6626 struct ocfs2_xattr_value_buf vb = {
6627 .vb_access = ocfs2_journal_access,
6630 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6631 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6637 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6644 * The real bucket num in this series of blocks is stored
6645 * in the 1st bucket.
6648 num_buckets = le16_to_cpu(
6649 bucket_xh(args->old_bucket)->xh_num_buckets);
6651 ret = ocfs2_xattr_bucket_journal_access(handle,
6653 OCFS2_JOURNAL_ACCESS_CREATE);
6659 for (j = 0; j < bpb; j++)
6660 memcpy(bucket_block(args->new_bucket, j),
6661 bucket_block(args->old_bucket, j),
6664 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6666 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6667 args->old_bucket->bu_bhs[0],
6668 bucket_xh(args->old_bucket),
6669 args->new_bucket->bu_bhs[0],
6670 bucket_xh(args->new_bucket),
6672 ocfs2_get_reflink_xattr_value_root,
6680 * Re-access and dirty the bucket to calculate metaecc.
6681 * Because we may extend the transaction in reflink_xattr_header
6682 * which will let the already accessed block gone.
6684 ret = ocfs2_xattr_bucket_journal_access(handle,
6686 OCFS2_JOURNAL_ACCESS_WRITE);
6692 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6693 ocfs2_xattr_bucket_relse(args->old_bucket);
6694 ocfs2_xattr_bucket_relse(args->new_bucket);
6697 ocfs2_xattr_bucket_relse(args->old_bucket);
6698 ocfs2_xattr_bucket_relse(args->new_bucket);
6702 * Create the same xattr extent record in the new inode's xattr tree.
6704 static int ocfs2_reflink_xattr_rec(struct inode *inode,
6705 struct buffer_head *root_bh,
6711 int ret, credits = 0;
6712 u32 p_cluster, num_clusters;
6715 struct ocfs2_reflink_xattr_tree_args *args =
6716 (struct ocfs2_reflink_xattr_tree_args *)para;
6717 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6718 struct ocfs2_alloc_context *meta_ac = NULL;
6719 struct ocfs2_alloc_context *data_ac = NULL;
6720 struct ocfs2_extent_tree et;
6722 ocfs2_init_xattr_tree_extent_tree(&et,
6723 INODE_CACHE(args->reflink->new_inode),
6726 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6728 &meta_ac, &data_ac);
6734 handle = ocfs2_start_trans(osb, credits);
6735 if (IS_ERR(handle)) {
6736 ret = PTR_ERR(handle);
6741 ret = ocfs2_claim_clusters(osb, handle, data_ac,
6742 len, &p_cluster, &num_clusters);
6748 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6750 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6751 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6752 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6753 meta_ac, data_ac, args);
6759 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6760 (unsigned long long)new_blkno, len, cpos);
6761 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6767 ocfs2_commit_trans(osb, handle);
6771 ocfs2_free_alloc_context(meta_ac);
6773 ocfs2_free_alloc_context(data_ac);
6778 * Create reflinked xattr buckets.
6779 * We will add bucket one by one, and refcount all the xattrs in the bucket
6780 * if they are stored outside.
6782 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6783 struct buffer_head *blk_bh,
6784 struct buffer_head *new_blk_bh)
6787 struct ocfs2_reflink_xattr_tree_args para;
6789 memset(¶, 0, sizeof(para));
6790 para.reflink = args;
6791 para.old_blk_bh = blk_bh;
6792 para.new_blk_bh = new_blk_bh;
6794 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
6795 if (!para.old_bucket) {
6796 mlog_errno(-ENOMEM);
6800 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
6801 if (!para.new_bucket) {
6807 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
6808 ocfs2_reflink_xattr_rec,
6814 ocfs2_xattr_bucket_free(para.old_bucket);
6815 ocfs2_xattr_bucket_free(para.new_bucket);
6819 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
6820 struct buffer_head *blk_bh)
6822 int ret, indexed = 0;
6823 struct buffer_head *new_blk_bh = NULL;
6824 struct ocfs2_xattr_block *xb =
6825 (struct ocfs2_xattr_block *)blk_bh->b_data;
6828 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
6831 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
6832 &new_blk_bh, indexed);
6838 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
6839 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
6841 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
6850 int ocfs2_reflink_xattrs(struct inode *old_inode,
6851 struct buffer_head *old_bh,
6852 struct inode *new_inode,
6853 struct buffer_head *new_bh)
6856 struct ocfs2_xattr_reflink args;
6857 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
6858 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
6859 struct buffer_head *blk_bh = NULL;
6860 struct ocfs2_cached_dealloc_ctxt dealloc;
6861 struct ocfs2_refcount_tree *ref_tree;
6862 struct buffer_head *ref_root_bh = NULL;
6864 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
6865 le64_to_cpu(di->i_refcount_loc),
6866 1, &ref_tree, &ref_root_bh);
6872 ocfs2_init_dealloc_ctxt(&dealloc);
6874 args.old_inode = old_inode;
6875 args.new_inode = new_inode;
6876 args.old_bh = old_bh;
6877 args.new_bh = new_bh;
6878 args.ref_ci = &ref_tree->rf_ci;
6879 args.ref_root_bh = ref_root_bh;
6880 args.dealloc = &dealloc;
6882 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6883 ret = ocfs2_reflink_xattr_inline(&args);
6890 if (!di->i_xattr_loc)
6893 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
6900 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
6907 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
6909 brelse(ref_root_bh);
6911 if (ocfs2_dealloc_has_cluster(&dealloc)) {
6912 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
6913 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
6921 * 'security' attributes support
6923 static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
6924 size_t list_size, const char *name,
6927 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
6928 const size_t total_len = prefix_len + name_len + 1;
6930 if (list && total_len <= list_size) {
6931 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
6932 memcpy(list + prefix_len, name, name_len);
6933 list[prefix_len + name_len] = '\0';
6938 static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
6939 void *buffer, size_t size)
6941 if (strcmp(name, "") == 0)
6943 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
6947 static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
6948 const void *value, size_t size, int flags)
6950 if (strcmp(name, "") == 0)
6953 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
6957 int ocfs2_init_security_get(struct inode *inode,
6959 struct ocfs2_security_xattr_info *si)
6961 /* check whether ocfs2 support feature xattr */
6962 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
6964 return security_inode_init_security(inode, dir, &si->name, &si->value,
6968 int ocfs2_init_security_set(handle_t *handle,
6969 struct inode *inode,
6970 struct buffer_head *di_bh,
6971 struct ocfs2_security_xattr_info *si,
6972 struct ocfs2_alloc_context *xattr_ac,
6973 struct ocfs2_alloc_context *data_ac)
6975 return ocfs2_xattr_set_handle(handle, inode, di_bh,
6976 OCFS2_XATTR_INDEX_SECURITY,
6977 si->name, si->value, si->value_len, 0,
6981 struct xattr_handler ocfs2_xattr_security_handler = {
6982 .prefix = XATTR_SECURITY_PREFIX,
6983 .list = ocfs2_xattr_security_list,
6984 .get = ocfs2_xattr_security_get,
6985 .set = ocfs2_xattr_security_set,
6989 * 'trusted' attributes support
6991 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
6992 size_t list_size, const char *name,
6995 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
6996 const size_t total_len = prefix_len + name_len + 1;
6998 if (list && total_len <= list_size) {
6999 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7000 memcpy(list + prefix_len, name, name_len);
7001 list[prefix_len + name_len] = '\0';
7006 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
7007 void *buffer, size_t size)
7009 if (strcmp(name, "") == 0)
7011 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
7015 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
7016 const void *value, size_t size, int flags)
7018 if (strcmp(name, "") == 0)
7021 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
7025 struct xattr_handler ocfs2_xattr_trusted_handler = {
7026 .prefix = XATTR_TRUSTED_PREFIX,
7027 .list = ocfs2_xattr_trusted_list,
7028 .get = ocfs2_xattr_trusted_get,
7029 .set = ocfs2_xattr_trusted_set,
7033 * 'user' attributes support
7035 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
7036 size_t list_size, const char *name,
7039 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7040 const size_t total_len = prefix_len + name_len + 1;
7041 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7043 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7046 if (list && total_len <= list_size) {
7047 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7048 memcpy(list + prefix_len, name, name_len);
7049 list[prefix_len + name_len] = '\0';
7054 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
7055 void *buffer, size_t size)
7057 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7059 if (strcmp(name, "") == 0)
7061 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7063 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
7067 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
7068 const void *value, size_t size, int flags)
7070 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7072 if (strcmp(name, "") == 0)
7074 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7077 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
7081 struct xattr_handler ocfs2_xattr_user_handler = {
7082 .prefix = XATTR_USER_PREFIX,
7083 .list = ocfs2_xattr_user_list,
7084 .get = ocfs2_xattr_user_get,
7085 .set = ocfs2_xattr_user_set,