1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
9 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/security.h>
40 #define MLOG_MASK_PREFIX ML_XATTR
41 #include <cluster/masklog.h>
45 #include "blockcheck.h"
55 #include "buffer_head_io.h"
58 #include "refcounttree.h"
61 struct ocfs2_xattr_def_value_root {
62 struct ocfs2_xattr_value_root xv;
63 struct ocfs2_extent_rec er;
66 struct ocfs2_xattr_bucket {
67 /* The inode these xattrs are associated with */
68 struct inode *bu_inode;
70 /* The actual buffers that make up the bucket */
71 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
73 /* How many blocks make up one bucket for this filesystem */
77 struct ocfs2_xattr_set_ctxt {
79 struct ocfs2_alloc_context *meta_ac;
80 struct ocfs2_alloc_context *data_ac;
81 struct ocfs2_cached_dealloc_ctxt dealloc;
84 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
85 #define OCFS2_XATTR_INLINE_SIZE 80
86 #define OCFS2_XATTR_HEADER_GAP 4
87 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
88 - sizeof(struct ocfs2_xattr_header) \
89 - OCFS2_XATTR_HEADER_GAP)
90 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
91 - sizeof(struct ocfs2_xattr_block) \
92 - sizeof(struct ocfs2_xattr_header) \
93 - OCFS2_XATTR_HEADER_GAP)
95 static struct ocfs2_xattr_def_value_root def_xv = {
96 .xv.xr_list.l_count = cpu_to_le16(1),
99 struct xattr_handler *ocfs2_xattr_handlers[] = {
100 &ocfs2_xattr_user_handler,
101 #ifdef CONFIG_OCFS2_FS_POSIX_ACL
102 &ocfs2_xattr_acl_access_handler,
103 &ocfs2_xattr_acl_default_handler,
105 &ocfs2_xattr_trusted_handler,
106 &ocfs2_xattr_security_handler,
110 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
111 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
112 #ifdef CONFIG_OCFS2_FS_POSIX_ACL
113 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
114 = &ocfs2_xattr_acl_access_handler,
115 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
116 = &ocfs2_xattr_acl_default_handler,
118 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
119 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
122 struct ocfs2_xattr_info {
129 struct ocfs2_xattr_search {
130 struct buffer_head *inode_bh;
132 * xattr_bh point to the block buffer head which has extended attribute
133 * when extended attribute in inode, xattr_bh is equal to inode_bh.
135 struct buffer_head *xattr_bh;
136 struct ocfs2_xattr_header *header;
137 struct ocfs2_xattr_bucket *bucket;
140 struct ocfs2_xattr_entry *here;
144 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
145 struct ocfs2_xattr_header *xh,
150 static int ocfs2_xattr_block_find(struct inode *inode,
153 struct ocfs2_xattr_search *xs);
154 static int ocfs2_xattr_index_block_find(struct inode *inode,
155 struct buffer_head *root_bh,
158 struct ocfs2_xattr_search *xs);
160 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
161 struct buffer_head *blk_bh,
165 static int ocfs2_xattr_create_index_block(struct inode *inode,
166 struct ocfs2_xattr_search *xs,
167 struct ocfs2_xattr_set_ctxt *ctxt);
169 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
170 struct ocfs2_xattr_info *xi,
171 struct ocfs2_xattr_search *xs,
172 struct ocfs2_xattr_set_ctxt *ctxt);
174 typedef int (xattr_tree_rec_func)(struct inode *inode,
175 struct buffer_head *root_bh,
176 u64 blkno, u32 cpos, u32 len, void *para);
177 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
178 struct buffer_head *root_bh,
179 xattr_tree_rec_func *rec_func,
181 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
182 struct ocfs2_xattr_bucket *bucket,
184 static int ocfs2_rm_xattr_cluster(struct inode *inode,
185 struct buffer_head *root_bh,
191 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
192 u64 src_blk, u64 last_blk, u64 to_blk,
193 unsigned int start_bucket,
195 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
196 struct ocfs2_dinode *di,
197 struct ocfs2_xattr_info *xi,
198 struct ocfs2_xattr_search *xis,
199 struct ocfs2_xattr_search *xbs,
200 struct ocfs2_refcount_tree **ref_tree,
203 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
204 struct ocfs2_xattr_bucket *bucket,
206 struct ocfs2_xattr_value_root **xv,
207 struct buffer_head **bh);
209 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
211 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
214 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
216 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
219 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
221 u16 len = sb->s_blocksize -
222 offsetof(struct ocfs2_xattr_header, xh_entries);
224 return len / sizeof(struct ocfs2_xattr_entry);
227 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
228 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
229 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
231 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
233 struct ocfs2_xattr_bucket *bucket;
234 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
236 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
238 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
240 bucket->bu_inode = inode;
241 bucket->bu_blocks = blks;
247 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
251 for (i = 0; i < bucket->bu_blocks; i++) {
252 brelse(bucket->bu_bhs[i]);
253 bucket->bu_bhs[i] = NULL;
257 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
260 ocfs2_xattr_bucket_relse(bucket);
261 bucket->bu_inode = NULL;
267 * A bucket that has never been written to disk doesn't need to be
268 * read. We just need the buffer_heads. Don't call this for
269 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
272 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
277 for (i = 0; i < bucket->bu_blocks; i++) {
278 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
280 if (!bucket->bu_bhs[i]) {
286 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
288 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
293 ocfs2_xattr_bucket_relse(bucket);
297 /* Read the xattr bucket at xb_blkno */
298 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
303 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
304 bucket->bu_blocks, bucket->bu_bhs, 0,
307 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
308 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
311 &bucket_xh(bucket)->xh_check);
312 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
318 ocfs2_xattr_bucket_relse(bucket);
322 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
323 struct ocfs2_xattr_bucket *bucket,
328 for (i = 0; i < bucket->bu_blocks; i++) {
329 rc = ocfs2_journal_access(handle,
330 INODE_CACHE(bucket->bu_inode),
331 bucket->bu_bhs[i], type);
341 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
342 struct ocfs2_xattr_bucket *bucket)
346 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
347 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
348 bucket->bu_bhs, bucket->bu_blocks,
349 &bucket_xh(bucket)->xh_check);
350 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
352 for (i = 0; i < bucket->bu_blocks; i++)
353 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
356 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
357 struct ocfs2_xattr_bucket *src)
360 int blocksize = src->bu_inode->i_sb->s_blocksize;
362 BUG_ON(dest->bu_blocks != src->bu_blocks);
363 BUG_ON(dest->bu_inode != src->bu_inode);
365 for (i = 0; i < src->bu_blocks; i++) {
366 memcpy(bucket_block(dest, i), bucket_block(src, i),
371 static int ocfs2_validate_xattr_block(struct super_block *sb,
372 struct buffer_head *bh)
375 struct ocfs2_xattr_block *xb =
376 (struct ocfs2_xattr_block *)bh->b_data;
378 mlog(0, "Validating xattr block %llu\n",
379 (unsigned long long)bh->b_blocknr);
381 BUG_ON(!buffer_uptodate(bh));
384 * If the ecc fails, we return the error but otherwise
385 * leave the filesystem running. We know any error is
386 * local to this block.
388 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
393 * Errors after here are fatal
396 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
398 "Extended attribute block #%llu has bad "
400 (unsigned long long)bh->b_blocknr, 7,
405 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
407 "Extended attribute block #%llu has an "
408 "invalid xb_blkno of %llu",
409 (unsigned long long)bh->b_blocknr,
410 (unsigned long long)le64_to_cpu(xb->xb_blkno));
414 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
416 "Extended attribute block #%llu has an invalid "
417 "xb_fs_generation of #%u",
418 (unsigned long long)bh->b_blocknr,
419 le32_to_cpu(xb->xb_fs_generation));
426 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
427 struct buffer_head **bh)
430 struct buffer_head *tmp = *bh;
432 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
433 ocfs2_validate_xattr_block);
435 /* If ocfs2_read_block() got us a new bh, pass it up. */
442 static inline const char *ocfs2_xattr_prefix(int name_index)
444 struct xattr_handler *handler = NULL;
446 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
447 handler = ocfs2_xattr_handler_map[name_index];
449 return handler ? handler->prefix : NULL;
452 static u32 ocfs2_xattr_name_hash(struct inode *inode,
456 /* Get hash value of uuid from super block */
457 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
460 /* hash extended attribute name */
461 for (i = 0; i < name_len; i++) {
462 hash = (hash << OCFS2_HASH_SHIFT) ^
463 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
471 * ocfs2_xattr_hash_entry()
473 * Compute the hash of an extended attribute.
475 static void ocfs2_xattr_hash_entry(struct inode *inode,
476 struct ocfs2_xattr_header *header,
477 struct ocfs2_xattr_entry *entry)
480 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
482 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
483 entry->xe_name_hash = cpu_to_le32(hash);
488 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
492 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
493 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
495 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
496 size += sizeof(struct ocfs2_xattr_entry);
501 int ocfs2_calc_security_init(struct inode *dir,
502 struct ocfs2_security_xattr_info *si,
505 struct ocfs2_alloc_context **xattr_ac)
508 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
509 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
513 * The max space of security xattr taken inline is
514 * 256(name) + 80(value) + 16(entry) = 352 bytes,
515 * So reserve one metadata block for it is ok.
517 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
518 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
519 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
524 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
527 /* reserve clusters for xattr value which will be set in B tree*/
528 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
529 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
532 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
534 *want_clusters += new_clusters;
539 int ocfs2_calc_xattr_init(struct inode *dir,
540 struct buffer_head *dir_bh,
542 struct ocfs2_security_xattr_info *si,
548 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
549 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
552 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
555 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
556 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
557 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
560 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
563 } else if (acl_len != 0 && acl_len != -ENODATA) {
569 if (!(s_size + a_size))
573 * The max space of security xattr taken inline is
574 * 256(name) + 80(value) + 16(entry) = 352 bytes,
575 * The max space of acl xattr taken inline is
576 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
577 * when blocksize = 512, may reserve one more cluser for
578 * xattr bucket, otherwise reserve one metadata block
580 * If this is a new directory with inline data,
581 * we choose to reserve the entire inline area for
582 * directory contents and force an external xattr block.
584 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
585 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
586 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
587 *want_meta = *want_meta + 1;
588 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
591 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
592 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
594 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
598 * reserve credits and clusters for xattrs which has large value
599 * and have to be set outside
601 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
602 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
604 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
606 *want_clusters += new_clusters;
608 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
609 acl_len > OCFS2_XATTR_INLINE_SIZE) {
610 /* for directory, it has DEFAULT and ACCESS two types of acls */
611 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
612 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
613 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
615 *want_clusters += new_clusters;
621 static int ocfs2_xattr_extend_allocation(struct inode *inode,
623 struct ocfs2_xattr_value_buf *vb,
624 struct ocfs2_xattr_set_ctxt *ctxt)
627 handle_t *handle = ctxt->handle;
628 enum ocfs2_alloc_restarted why;
629 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
630 struct ocfs2_extent_tree et;
632 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
634 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
636 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
637 OCFS2_JOURNAL_ACCESS_WRITE);
643 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
644 status = ocfs2_add_clusters_in_btree(handle,
657 status = ocfs2_journal_dirty(handle, vb->vb_bh);
663 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
666 * We should have already allocated enough space before the transaction,
667 * so no need to restart.
669 BUG_ON(why != RESTART_NONE || clusters_to_add);
676 static int __ocfs2_remove_xattr_range(struct inode *inode,
677 struct ocfs2_xattr_value_buf *vb,
678 u32 cpos, u32 phys_cpos, u32 len,
679 unsigned int ext_flags,
680 struct ocfs2_xattr_set_ctxt *ctxt)
683 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
684 handle_t *handle = ctxt->handle;
685 struct ocfs2_extent_tree et;
687 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
689 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
690 OCFS2_JOURNAL_ACCESS_WRITE);
696 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
703 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
705 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
711 if (ext_flags & OCFS2_EXT_REFCOUNTED)
712 ret = ocfs2_decrease_refcount(inode, handle,
713 ocfs2_blocks_to_clusters(inode->i_sb,
715 len, ctxt->meta_ac, &ctxt->dealloc, 1);
717 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
726 static int ocfs2_xattr_shrink_size(struct inode *inode,
729 struct ocfs2_xattr_value_buf *vb,
730 struct ocfs2_xattr_set_ctxt *ctxt)
733 unsigned int ext_flags;
734 u32 trunc_len, cpos, phys_cpos, alloc_size;
737 if (old_clusters <= new_clusters)
741 trunc_len = old_clusters - new_clusters;
743 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
745 &vb->vb_xv->xr_list, &ext_flags);
751 if (alloc_size > trunc_len)
752 alloc_size = trunc_len;
754 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
755 phys_cpos, alloc_size,
762 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
763 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
766 trunc_len -= alloc_size;
773 static int ocfs2_xattr_value_truncate(struct inode *inode,
774 struct ocfs2_xattr_value_buf *vb,
776 struct ocfs2_xattr_set_ctxt *ctxt)
779 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
780 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
782 if (new_clusters == old_clusters)
785 if (new_clusters > old_clusters)
786 ret = ocfs2_xattr_extend_allocation(inode,
787 new_clusters - old_clusters,
790 ret = ocfs2_xattr_shrink_size(inode,
791 old_clusters, new_clusters,
797 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
798 size_t *result, const char *prefix,
799 const char *name, int name_len)
801 char *p = buffer + *result;
802 int prefix_len = strlen(prefix);
803 int total_len = prefix_len + name_len + 1;
805 *result += total_len;
807 /* we are just looking for how big our buffer needs to be */
814 memcpy(p, prefix, prefix_len);
815 memcpy(p + prefix_len, name, name_len);
816 p[prefix_len + name_len] = '\0';
821 static int ocfs2_xattr_list_entries(struct inode *inode,
822 struct ocfs2_xattr_header *header,
823 char *buffer, size_t buffer_size)
827 const char *prefix, *name;
829 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
830 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
831 type = ocfs2_xattr_get_type(entry);
832 prefix = ocfs2_xattr_prefix(type);
835 name = (const char *)header +
836 le16_to_cpu(entry->xe_name_offset);
838 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
839 &result, prefix, name,
849 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
850 struct ocfs2_dinode *di)
852 struct ocfs2_xattr_header *xh;
855 xh = (struct ocfs2_xattr_header *)
856 ((void *)di + inode->i_sb->s_blocksize -
857 le16_to_cpu(di->i_xattr_inline_size));
859 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
860 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
866 static int ocfs2_xattr_ibody_list(struct inode *inode,
867 struct ocfs2_dinode *di,
871 struct ocfs2_xattr_header *header = NULL;
872 struct ocfs2_inode_info *oi = OCFS2_I(inode);
875 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
878 header = (struct ocfs2_xattr_header *)
879 ((void *)di + inode->i_sb->s_blocksize -
880 le16_to_cpu(di->i_xattr_inline_size));
882 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
887 static int ocfs2_xattr_block_list(struct inode *inode,
888 struct ocfs2_dinode *di,
892 struct buffer_head *blk_bh = NULL;
893 struct ocfs2_xattr_block *xb;
896 if (!di->i_xattr_loc)
899 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
906 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
907 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
908 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
909 ret = ocfs2_xattr_list_entries(inode, header,
910 buffer, buffer_size);
912 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
913 buffer, buffer_size);
920 ssize_t ocfs2_listxattr(struct dentry *dentry,
924 int ret = 0, i_ret = 0, b_ret = 0;
925 struct buffer_head *di_bh = NULL;
926 struct ocfs2_dinode *di = NULL;
927 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
929 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
932 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
935 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
941 di = (struct ocfs2_dinode *)di_bh->b_data;
943 down_read(&oi->ip_xattr_sem);
944 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
952 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
957 up_read(&oi->ip_xattr_sem);
958 ocfs2_inode_unlock(dentry->d_inode, 0);
962 return i_ret + b_ret;
965 static int ocfs2_xattr_find_entry(int name_index,
967 struct ocfs2_xattr_search *xs)
969 struct ocfs2_xattr_entry *entry;
976 name_len = strlen(name);
978 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
979 cmp = name_index - ocfs2_xattr_get_type(entry);
981 cmp = name_len - entry->xe_name_len;
983 cmp = memcmp(name, (xs->base +
984 le16_to_cpu(entry->xe_name_offset)),
992 return cmp ? -ENODATA : 0;
995 static int ocfs2_xattr_get_value_outside(struct inode *inode,
996 struct ocfs2_xattr_value_root *xv,
1000 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1003 size_t cplen, blocksize;
1004 struct buffer_head *bh = NULL;
1005 struct ocfs2_extent_list *el;
1008 clusters = le32_to_cpu(xv->xr_clusters);
1009 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1010 blocksize = inode->i_sb->s_blocksize;
1013 while (cpos < clusters) {
1014 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1015 &num_clusters, el, NULL);
1021 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1022 /* Copy ocfs2_xattr_value */
1023 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1024 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1031 cplen = len >= blocksize ? blocksize : len;
1032 memcpy(buffer, bh->b_data, cplen);
1041 cpos += num_clusters;
1047 static int ocfs2_xattr_ibody_get(struct inode *inode,
1052 struct ocfs2_xattr_search *xs)
1054 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1055 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1056 struct ocfs2_xattr_value_root *xv;
1060 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1063 xs->end = (void *)di + inode->i_sb->s_blocksize;
1064 xs->header = (struct ocfs2_xattr_header *)
1065 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1066 xs->base = (void *)xs->header;
1067 xs->here = xs->header->xh_entries;
1069 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1072 size = le64_to_cpu(xs->here->xe_value_size);
1074 if (size > buffer_size)
1076 if (ocfs2_xattr_is_local(xs->here)) {
1077 memcpy(buffer, (void *)xs->base +
1078 le16_to_cpu(xs->here->xe_name_offset) +
1079 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1081 xv = (struct ocfs2_xattr_value_root *)
1082 (xs->base + le16_to_cpu(
1083 xs->here->xe_name_offset) +
1084 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1085 ret = ocfs2_xattr_get_value_outside(inode, xv,
1097 static int ocfs2_xattr_block_get(struct inode *inode,
1102 struct ocfs2_xattr_search *xs)
1104 struct ocfs2_xattr_block *xb;
1105 struct ocfs2_xattr_value_root *xv;
1107 int ret = -ENODATA, name_offset, name_len, i;
1108 int uninitialized_var(block_off);
1110 xs->bucket = ocfs2_xattr_bucket_new(inode);
1117 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1123 if (xs->not_found) {
1128 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1129 size = le64_to_cpu(xs->here->xe_value_size);
1132 if (size > buffer_size)
1135 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1136 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1137 i = xs->here - xs->header->xh_entries;
1139 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1140 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1141 bucket_xh(xs->bucket),
1145 xs->base = bucket_block(xs->bucket, block_off);
1147 if (ocfs2_xattr_is_local(xs->here)) {
1148 memcpy(buffer, (void *)xs->base +
1149 name_offset + name_len, size);
1151 xv = (struct ocfs2_xattr_value_root *)
1152 (xs->base + name_offset + name_len);
1153 ret = ocfs2_xattr_get_value_outside(inode, xv,
1163 ocfs2_xattr_bucket_free(xs->bucket);
1165 brelse(xs->xattr_bh);
1166 xs->xattr_bh = NULL;
1170 int ocfs2_xattr_get_nolock(struct inode *inode,
1171 struct buffer_head *di_bh,
1178 struct ocfs2_dinode *di = NULL;
1179 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1180 struct ocfs2_xattr_search xis = {
1181 .not_found = -ENODATA,
1183 struct ocfs2_xattr_search xbs = {
1184 .not_found = -ENODATA,
1187 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1190 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1193 xis.inode_bh = xbs.inode_bh = di_bh;
1194 di = (struct ocfs2_dinode *)di_bh->b_data;
1196 down_read(&oi->ip_xattr_sem);
1197 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1199 if (ret == -ENODATA && di->i_xattr_loc)
1200 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1202 up_read(&oi->ip_xattr_sem);
1207 /* ocfs2_xattr_get()
1209 * Copy an extended attribute into the buffer provided.
1210 * Buffer is NULL to compute the size of buffer required.
1212 static int ocfs2_xattr_get(struct inode *inode,
1219 struct buffer_head *di_bh = NULL;
1221 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1226 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1227 name, buffer, buffer_size);
1229 ocfs2_inode_unlock(inode, 0);
1236 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1238 struct ocfs2_xattr_value_buf *vb,
1242 int ret = 0, i, cp_len;
1243 u16 blocksize = inode->i_sb->s_blocksize;
1244 u32 p_cluster, num_clusters;
1245 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1246 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1248 struct buffer_head *bh = NULL;
1249 unsigned int ext_flags;
1250 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1252 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1254 while (cpos < clusters) {
1255 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1256 &num_clusters, &xv->xr_list,
1263 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1265 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1267 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1268 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1275 ret = ocfs2_journal_access(handle,
1278 OCFS2_JOURNAL_ACCESS_WRITE);
1284 cp_len = value_len > blocksize ? blocksize : value_len;
1285 memcpy(bh->b_data, value, cp_len);
1286 value_len -= cp_len;
1288 if (cp_len < blocksize)
1289 memset(bh->b_data + cp_len, 0,
1290 blocksize - cp_len);
1292 ret = ocfs2_journal_dirty(handle, bh);
1301 * XXX: do we need to empty all the following
1302 * blocks in this cluster?
1307 cpos += num_clusters;
1315 static int ocfs2_xattr_cleanup(struct inode *inode,
1317 struct ocfs2_xattr_info *xi,
1318 struct ocfs2_xattr_search *xs,
1319 struct ocfs2_xattr_value_buf *vb,
1323 size_t name_len = strlen(xi->name);
1324 void *val = xs->base + offs;
1325 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1327 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1328 OCFS2_JOURNAL_ACCESS_WRITE);
1333 /* Decrease xattr count */
1334 le16_add_cpu(&xs->header->xh_count, -1);
1335 /* Remove the xattr entry and tree root which has already be set*/
1336 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1337 memset(val, 0, size);
1339 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1346 static int ocfs2_xattr_update_entry(struct inode *inode,
1348 struct ocfs2_xattr_info *xi,
1349 struct ocfs2_xattr_search *xs,
1350 struct ocfs2_xattr_value_buf *vb,
1355 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1356 OCFS2_JOURNAL_ACCESS_WRITE);
1362 xs->here->xe_name_offset = cpu_to_le16(offs);
1363 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1364 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1365 ocfs2_xattr_set_local(xs->here, 1);
1367 ocfs2_xattr_set_local(xs->here, 0);
1368 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1370 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1378 * ocfs2_xattr_set_value_outside()
1380 * Set large size value in B tree.
1382 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1383 struct ocfs2_xattr_info *xi,
1384 struct ocfs2_xattr_search *xs,
1385 struct ocfs2_xattr_set_ctxt *ctxt,
1386 struct ocfs2_xattr_value_buf *vb,
1389 size_t name_len = strlen(xi->name);
1390 void *val = xs->base + offs;
1391 struct ocfs2_xattr_value_root *xv = NULL;
1392 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1395 memset(val, 0, size);
1396 memcpy(val, xi->name, name_len);
1397 xv = (struct ocfs2_xattr_value_root *)
1398 (val + OCFS2_XATTR_SIZE(name_len));
1399 xv->xr_clusters = 0;
1400 xv->xr_last_eb_blk = 0;
1401 xv->xr_list.l_tree_depth = 0;
1402 xv->xr_list.l_count = cpu_to_le16(1);
1403 xv->xr_list.l_next_free_rec = 0;
1406 ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt);
1411 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
1416 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
1417 xi->value, xi->value_len);
1425 * ocfs2_xattr_set_entry_local()
1427 * Set, replace or remove extended attribute in local.
1429 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1430 struct ocfs2_xattr_info *xi,
1431 struct ocfs2_xattr_search *xs,
1432 struct ocfs2_xattr_entry *last,
1435 size_t name_len = strlen(xi->name);
1438 if (xi->value && xs->not_found) {
1439 /* Insert the new xattr entry. */
1440 le16_add_cpu(&xs->header->xh_count, 1);
1441 ocfs2_xattr_set_type(last, xi->name_index);
1442 ocfs2_xattr_set_local(last, 1);
1443 last->xe_name_len = name_len;
1449 first_val = xs->base + min_offs;
1450 offs = le16_to_cpu(xs->here->xe_name_offset);
1451 val = xs->base + offs;
1453 if (le64_to_cpu(xs->here->xe_value_size) >
1454 OCFS2_XATTR_INLINE_SIZE)
1455 size = OCFS2_XATTR_SIZE(name_len) +
1456 OCFS2_XATTR_ROOT_SIZE;
1458 size = OCFS2_XATTR_SIZE(name_len) +
1459 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1461 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1462 OCFS2_XATTR_SIZE(xi->value_len)) {
1463 /* The old and the new value have the
1464 same size. Just replace the value. */
1465 ocfs2_xattr_set_local(xs->here, 1);
1466 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1467 /* Clear value bytes. */
1468 memset(val + OCFS2_XATTR_SIZE(name_len),
1470 OCFS2_XATTR_SIZE(xi->value_len));
1471 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1476 /* Remove the old name+value. */
1477 memmove(first_val + size, first_val, val - first_val);
1478 memset(first_val, 0, size);
1479 xs->here->xe_name_hash = 0;
1480 xs->here->xe_name_offset = 0;
1481 ocfs2_xattr_set_local(xs->here, 1);
1482 xs->here->xe_value_size = 0;
1486 /* Adjust all value offsets. */
1487 last = xs->header->xh_entries;
1488 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1489 size_t o = le16_to_cpu(last->xe_name_offset);
1492 last->xe_name_offset = cpu_to_le16(o + size);
1497 /* Remove the old entry. */
1499 memmove(xs->here, xs->here + 1,
1500 (void *)last - (void *)xs->here);
1501 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1502 le16_add_cpu(&xs->header->xh_count, -1);
1506 /* Insert the new name+value. */
1507 size_t size = OCFS2_XATTR_SIZE(name_len) +
1508 OCFS2_XATTR_SIZE(xi->value_len);
1509 void *val = xs->base + min_offs - size;
1511 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1512 memset(val, 0, size);
1513 memcpy(val, xi->name, name_len);
1514 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1517 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1518 ocfs2_xattr_set_local(xs->here, 1);
1519 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1526 * ocfs2_xattr_set_entry()
1528 * Set extended attribute entry into inode or block.
1530 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1531 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1532 * then set value in B tree with set_value_outside().
1534 static int ocfs2_xattr_set_entry(struct inode *inode,
1535 struct ocfs2_xattr_info *xi,
1536 struct ocfs2_xattr_search *xs,
1537 struct ocfs2_xattr_set_ctxt *ctxt,
1540 struct ocfs2_xattr_entry *last;
1541 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1542 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1543 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1545 handle_t *handle = ctxt->handle;
1547 struct ocfs2_xattr_info xi_l = {
1548 .name_index = xi->name_index,
1551 .value_len = xi->value_len,
1553 struct ocfs2_xattr_value_buf vb = {
1554 .vb_bh = xs->xattr_bh,
1555 .vb_access = ocfs2_journal_access_di,
1558 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1559 BUG_ON(xs->xattr_bh == xs->inode_bh);
1560 vb.vb_access = ocfs2_journal_access_xb;
1562 BUG_ON(xs->xattr_bh != xs->inode_bh);
1564 /* Compute min_offs, last and free space. */
1565 last = xs->header->xh_entries;
1567 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1568 size_t offs = le16_to_cpu(last->xe_name_offset);
1569 if (offs < min_offs)
1574 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
1578 if (!xs->not_found) {
1580 if (ocfs2_xattr_is_local(xs->here))
1581 size = OCFS2_XATTR_SIZE(name_len) +
1582 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1584 size = OCFS2_XATTR_SIZE(name_len) +
1585 OCFS2_XATTR_ROOT_SIZE;
1586 free += (size + sizeof(struct ocfs2_xattr_entry));
1588 /* Check free space in inode or block */
1589 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1590 if (free < sizeof(struct ocfs2_xattr_entry) +
1591 OCFS2_XATTR_SIZE(name_len) +
1592 OCFS2_XATTR_ROOT_SIZE) {
1596 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1597 xi_l.value = (void *)&def_xv;
1598 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1599 } else if (xi->value) {
1600 if (free < sizeof(struct ocfs2_xattr_entry) +
1601 OCFS2_XATTR_SIZE(name_len) +
1602 OCFS2_XATTR_SIZE(xi->value_len)) {
1608 if (!xs->not_found) {
1609 /* For existing extended attribute */
1610 size_t size = OCFS2_XATTR_SIZE(name_len) +
1611 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1612 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1613 void *val = xs->base + offs;
1615 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1616 /* Replace existing local xattr with tree root */
1617 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1622 } else if (!ocfs2_xattr_is_local(xs->here)) {
1623 /* For existing xattr which has value outside */
1624 vb.vb_xv = (struct ocfs2_xattr_value_root *)
1625 (val + OCFS2_XATTR_SIZE(name_len));
1627 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1629 * If new value need set outside also,
1630 * first truncate old value to new value,
1631 * then set new value with set_value_outside().
1633 ret = ocfs2_xattr_value_truncate(inode,
1642 ret = ocfs2_xattr_update_entry(inode,
1653 ret = __ocfs2_xattr_set_value_outside(inode,
1663 * If new value need set in local,
1664 * just trucate old value to zero.
1666 ret = ocfs2_xattr_value_truncate(inode,
1676 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
1677 OCFS2_JOURNAL_ACCESS_WRITE);
1683 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1684 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
1685 OCFS2_JOURNAL_ACCESS_WRITE);
1693 * Set value in local, include set tree root in local.
1694 * This is the first step for value size >INLINE_SIZE.
1696 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1698 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1699 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1706 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1707 (flag & OCFS2_INLINE_XATTR_FL)) {
1708 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1709 unsigned int xattrsize = osb->s_xattr_inline_size;
1712 * Adjust extent record count or inline data size
1713 * to reserve space for extended attribute.
1715 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1716 struct ocfs2_inline_data *idata = &di->id2.i_data;
1717 le16_add_cpu(&idata->id_count, -xattrsize);
1718 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1719 struct ocfs2_extent_list *el = &di->id2.i_list;
1720 le16_add_cpu(&el->l_count, -(xattrsize /
1721 sizeof(struct ocfs2_extent_rec)));
1723 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1725 /* Update xattr flag */
1726 spin_lock(&oi->ip_lock);
1727 oi->ip_dyn_features |= flag;
1728 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1729 spin_unlock(&oi->ip_lock);
1731 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1735 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1737 * Set value outside in B tree.
1738 * This is the second step for value size > INLINE_SIZE.
1740 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1741 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
1748 * If set value outside failed, we have to clean
1749 * the junk tree root we have already set in local.
1751 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1762 * In xattr remove, if it is stored outside and refcounted, we may have
1763 * the chance to split the refcount tree. So need the allocators.
1765 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
1766 struct ocfs2_xattr_value_root *xv,
1767 struct ocfs2_caching_info *ref_ci,
1768 struct buffer_head *ref_root_bh,
1769 struct ocfs2_alloc_context **meta_ac,
1772 int ret, meta_add = 0;
1773 u32 p_cluster, num_clusters;
1774 unsigned int ext_flags;
1777 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
1786 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
1789 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
1791 &meta_add, ref_credits);
1797 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
1806 static int ocfs2_remove_value_outside(struct inode*inode,
1807 struct ocfs2_xattr_value_buf *vb,
1808 struct ocfs2_xattr_header *header,
1809 struct ocfs2_caching_info *ref_ci,
1810 struct buffer_head *ref_root_bh)
1812 int ret = 0, i, ref_credits;
1813 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1814 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1817 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
1819 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1820 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1822 if (ocfs2_xattr_is_local(entry))
1825 val = (void *)header +
1826 le16_to_cpu(entry->xe_name_offset);
1827 vb->vb_xv = (struct ocfs2_xattr_value_root *)
1828 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1830 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
1831 ref_ci, ref_root_bh,
1835 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
1836 ocfs2_remove_extent_credits(osb->sb));
1837 if (IS_ERR(ctxt.handle)) {
1838 ret = PTR_ERR(ctxt.handle);
1843 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
1849 ocfs2_commit_trans(osb, ctxt.handle);
1851 ocfs2_free_alloc_context(ctxt.meta_ac);
1852 ctxt.meta_ac = NULL;
1857 ocfs2_free_alloc_context(ctxt.meta_ac);
1858 ocfs2_schedule_truncate_log_flush(osb, 1);
1859 ocfs2_run_deallocs(osb, &ctxt.dealloc);
1863 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1864 struct buffer_head *di_bh,
1865 struct ocfs2_caching_info *ref_ci,
1866 struct buffer_head *ref_root_bh)
1869 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1870 struct ocfs2_xattr_header *header;
1872 struct ocfs2_xattr_value_buf vb = {
1874 .vb_access = ocfs2_journal_access_di,
1877 header = (struct ocfs2_xattr_header *)
1878 ((void *)di + inode->i_sb->s_blocksize -
1879 le16_to_cpu(di->i_xattr_inline_size));
1881 ret = ocfs2_remove_value_outside(inode, &vb, header,
1882 ref_ci, ref_root_bh);
1887 struct ocfs2_rm_xattr_bucket_para {
1888 struct ocfs2_caching_info *ref_ci;
1889 struct buffer_head *ref_root_bh;
1892 static int ocfs2_xattr_block_remove(struct inode *inode,
1893 struct buffer_head *blk_bh,
1894 struct ocfs2_caching_info *ref_ci,
1895 struct buffer_head *ref_root_bh)
1897 struct ocfs2_xattr_block *xb;
1899 struct ocfs2_xattr_value_buf vb = {
1901 .vb_access = ocfs2_journal_access_xb,
1903 struct ocfs2_rm_xattr_bucket_para args = {
1905 .ref_root_bh = ref_root_bh,
1908 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1909 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1910 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1911 ret = ocfs2_remove_value_outside(inode, &vb, header,
1912 ref_ci, ref_root_bh);
1914 ret = ocfs2_iterate_xattr_index_block(inode,
1916 ocfs2_rm_xattr_cluster,
1922 static int ocfs2_xattr_free_block(struct inode *inode,
1924 struct ocfs2_caching_info *ref_ci,
1925 struct buffer_head *ref_root_bh)
1927 struct inode *xb_alloc_inode;
1928 struct buffer_head *xb_alloc_bh = NULL;
1929 struct buffer_head *blk_bh = NULL;
1930 struct ocfs2_xattr_block *xb;
1931 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1937 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
1943 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
1949 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1950 blk = le64_to_cpu(xb->xb_blkno);
1951 bit = le16_to_cpu(xb->xb_suballoc_bit);
1952 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1954 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1955 EXTENT_ALLOC_SYSTEM_INODE,
1956 le16_to_cpu(xb->xb_suballoc_slot));
1957 if (!xb_alloc_inode) {
1962 mutex_lock(&xb_alloc_inode->i_mutex);
1964 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1970 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1971 if (IS_ERR(handle)) {
1972 ret = PTR_ERR(handle);
1977 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1982 ocfs2_commit_trans(osb, handle);
1984 ocfs2_inode_unlock(xb_alloc_inode, 1);
1985 brelse(xb_alloc_bh);
1987 mutex_unlock(&xb_alloc_inode->i_mutex);
1988 iput(xb_alloc_inode);
1995 * ocfs2_xattr_remove()
1997 * Free extended attribute resources associated with this inode.
1999 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2001 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2002 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2003 struct ocfs2_refcount_tree *ref_tree = NULL;
2004 struct buffer_head *ref_root_bh = NULL;
2005 struct ocfs2_caching_info *ref_ci = NULL;
2009 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2012 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2015 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2016 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2017 le64_to_cpu(di->i_refcount_loc),
2018 1, &ref_tree, &ref_root_bh);
2023 ref_ci = &ref_tree->rf_ci;
2027 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2028 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2029 ref_ci, ref_root_bh);
2036 if (di->i_xattr_loc) {
2037 ret = ocfs2_xattr_free_block(inode,
2038 le64_to_cpu(di->i_xattr_loc),
2039 ref_ci, ref_root_bh);
2046 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2047 OCFS2_INODE_UPDATE_CREDITS);
2048 if (IS_ERR(handle)) {
2049 ret = PTR_ERR(handle);
2053 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2054 OCFS2_JOURNAL_ACCESS_WRITE);
2060 di->i_xattr_loc = 0;
2062 spin_lock(&oi->ip_lock);
2063 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2064 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2065 spin_unlock(&oi->ip_lock);
2067 ret = ocfs2_journal_dirty(handle, di_bh);
2071 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2074 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2075 brelse(ref_root_bh);
2079 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2080 struct ocfs2_dinode *di)
2082 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2083 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2086 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2089 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2090 struct ocfs2_inline_data *idata = &di->id2.i_data;
2091 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2092 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2093 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2094 le64_to_cpu(di->i_size);
2096 struct ocfs2_extent_list *el = &di->id2.i_list;
2097 free = (le16_to_cpu(el->l_count) -
2098 le16_to_cpu(el->l_next_free_rec)) *
2099 sizeof(struct ocfs2_extent_rec);
2101 if (free >= xattrsize)
2108 * ocfs2_xattr_ibody_find()
2110 * Find extended attribute in inode block and
2111 * fill search info into struct ocfs2_xattr_search.
2113 static int ocfs2_xattr_ibody_find(struct inode *inode,
2116 struct ocfs2_xattr_search *xs)
2118 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2119 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2123 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2126 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2127 down_read(&oi->ip_alloc_sem);
2128 has_space = ocfs2_xattr_has_space_inline(inode, di);
2129 up_read(&oi->ip_alloc_sem);
2134 xs->xattr_bh = xs->inode_bh;
2135 xs->end = (void *)di + inode->i_sb->s_blocksize;
2136 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2137 xs->header = (struct ocfs2_xattr_header *)
2138 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2140 xs->header = (struct ocfs2_xattr_header *)
2141 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2142 xs->base = (void *)xs->header;
2143 xs->here = xs->header->xh_entries;
2145 /* Find the named attribute. */
2146 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2147 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2148 if (ret && ret != -ENODATA)
2150 xs->not_found = ret;
2157 * ocfs2_xattr_ibody_set()
2159 * Set, replace or remove an extended attribute into inode block.
2162 static int ocfs2_xattr_ibody_set(struct inode *inode,
2163 struct ocfs2_xattr_info *xi,
2164 struct ocfs2_xattr_search *xs,
2165 struct ocfs2_xattr_set_ctxt *ctxt)
2167 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2168 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2171 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2174 down_write(&oi->ip_alloc_sem);
2175 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2176 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2182 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2183 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2185 up_write(&oi->ip_alloc_sem);
2191 * ocfs2_xattr_block_find()
2193 * Find extended attribute in external block and
2194 * fill search info into struct ocfs2_xattr_search.
2196 static int ocfs2_xattr_block_find(struct inode *inode,
2199 struct ocfs2_xattr_search *xs)
2201 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2202 struct buffer_head *blk_bh = NULL;
2203 struct ocfs2_xattr_block *xb;
2206 if (!di->i_xattr_loc)
2209 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2216 xs->xattr_bh = blk_bh;
2217 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2219 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2220 xs->header = &xb->xb_attrs.xb_header;
2221 xs->base = (void *)xs->header;
2222 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2223 xs->here = xs->header->xh_entries;
2225 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2227 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2231 if (ret && ret != -ENODATA) {
2232 xs->xattr_bh = NULL;
2235 xs->not_found = ret;
2243 static int ocfs2_create_xattr_block(handle_t *handle,
2244 struct inode *inode,
2245 struct buffer_head *inode_bh,
2246 struct ocfs2_alloc_context *meta_ac,
2247 struct buffer_head **ret_bh,
2251 u16 suballoc_bit_start;
2254 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2255 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2256 struct buffer_head *new_bh = NULL;
2257 struct ocfs2_xattr_block *xblk;
2259 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2260 OCFS2_JOURNAL_ACCESS_CREATE);
2266 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2267 &suballoc_bit_start, &num_got,
2274 new_bh = sb_getblk(inode->i_sb, first_blkno);
2275 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2277 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2279 OCFS2_JOURNAL_ACCESS_CREATE);
2285 /* Initialize ocfs2_xattr_block */
2286 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2287 memset(xblk, 0, inode->i_sb->s_blocksize);
2288 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2289 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2290 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2291 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2292 xblk->xb_blkno = cpu_to_le64(first_blkno);
2295 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2296 xr->xt_clusters = cpu_to_le32(1);
2297 xr->xt_last_eb_blk = 0;
2298 xr->xt_list.l_tree_depth = 0;
2299 xr->xt_list.l_count = cpu_to_le16(
2300 ocfs2_xattr_recs_per_xb(inode->i_sb));
2301 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2302 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2305 ret = ocfs2_journal_dirty(handle, new_bh);
2310 di->i_xattr_loc = cpu_to_le64(first_blkno);
2311 ocfs2_journal_dirty(handle, inode_bh);
2322 * ocfs2_xattr_block_set()
2324 * Set, replace or remove an extended attribute into external block.
2327 static int ocfs2_xattr_block_set(struct inode *inode,
2328 struct ocfs2_xattr_info *xi,
2329 struct ocfs2_xattr_search *xs,
2330 struct ocfs2_xattr_set_ctxt *ctxt)
2332 struct buffer_head *new_bh = NULL;
2333 handle_t *handle = ctxt->handle;
2334 struct ocfs2_xattr_block *xblk = NULL;
2337 if (!xs->xattr_bh) {
2338 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
2339 ctxt->meta_ac, &new_bh, 0);
2345 xs->xattr_bh = new_bh;
2346 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2347 xs->header = &xblk->xb_attrs.xb_header;
2348 xs->base = (void *)xs->header;
2349 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2350 xs->here = xs->header->xh_entries;
2352 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2354 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2355 /* Set extended attribute into external block */
2356 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2357 OCFS2_HAS_XATTR_FL);
2358 if (!ret || ret != -ENOSPC)
2361 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2366 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2373 /* Check whether the new xattr can be inserted into the inode. */
2374 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2375 struct ocfs2_xattr_info *xi,
2376 struct ocfs2_xattr_search *xs)
2379 struct ocfs2_xattr_entry *last;
2381 size_t min_offs = xs->end - xs->base;
2386 last = xs->header->xh_entries;
2388 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2389 size_t offs = le16_to_cpu(last->xe_name_offset);
2390 if (offs < min_offs)
2395 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2399 BUG_ON(!xs->not_found);
2401 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2402 value_size = OCFS2_XATTR_ROOT_SIZE;
2404 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2406 if (free >= sizeof(struct ocfs2_xattr_entry) +
2407 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2413 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2414 struct ocfs2_dinode *di,
2415 struct ocfs2_xattr_info *xi,
2416 struct ocfs2_xattr_search *xis,
2417 struct ocfs2_xattr_search *xbs,
2422 int ret = 0, old_in_xb = 0;
2423 int clusters_add = 0, meta_add = 0, credits = 0;
2424 struct buffer_head *bh = NULL;
2425 struct ocfs2_xattr_block *xb = NULL;
2426 struct ocfs2_xattr_entry *xe = NULL;
2427 struct ocfs2_xattr_value_root *xv = NULL;
2429 int name_offset, name_len = 0;
2430 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2435 * Calculate the clusters we need to write.
2436 * No matter whether we replace an old one or add a new one,
2437 * we need this for writing.
2439 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2440 credits += new_clusters *
2441 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2443 if (xis->not_found && xbs->not_found) {
2444 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2446 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2447 clusters_add += new_clusters;
2448 credits += ocfs2_calc_extend_credits(inode->i_sb,
2456 if (!xis->not_found) {
2458 name_offset = le16_to_cpu(xe->xe_name_offset);
2459 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2461 credits += OCFS2_INODE_UPDATE_CREDITS;
2463 int i, block_off = 0;
2464 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2466 name_offset = le16_to_cpu(xe->xe_name_offset);
2467 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2468 i = xbs->here - xbs->header->xh_entries;
2471 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2472 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
2473 bucket_xh(xbs->bucket),
2476 base = bucket_block(xbs->bucket, block_off);
2477 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2480 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2485 * delete a xattr doesn't need metadata and cluster allocation.
2486 * so just calculate the credits and return.
2488 * The credits for removing the value tree will be extended
2489 * by ocfs2_remove_extent itself.
2492 if (!ocfs2_xattr_is_local(xe))
2493 credits += ocfs2_remove_extent_credits(inode->i_sb);
2498 /* do cluster allocation guess first. */
2499 value_size = le64_to_cpu(xe->xe_value_size);
2503 * In xattr set, we always try to set the xe in inode first,
2504 * so if it can be inserted into inode successfully, the old
2505 * one will be removed from the xattr block, and this xattr
2506 * will be inserted into inode as a new xattr in inode.
2508 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2509 clusters_add += new_clusters;
2510 credits += ocfs2_remove_extent_credits(inode->i_sb) +
2511 OCFS2_INODE_UPDATE_CREDITS;
2512 if (!ocfs2_xattr_is_local(xe))
2513 credits += ocfs2_calc_extend_credits(
2521 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2522 /* the new values will be stored outside. */
2523 u32 old_clusters = 0;
2525 if (!ocfs2_xattr_is_local(xe)) {
2526 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2528 xv = (struct ocfs2_xattr_value_root *)
2529 (base + name_offset + name_len);
2530 value_size = OCFS2_XATTR_ROOT_SIZE;
2534 if (old_clusters >= new_clusters) {
2535 credits += ocfs2_remove_extent_credits(inode->i_sb);
2538 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2539 clusters_add += new_clusters - old_clusters;
2540 credits += ocfs2_calc_extend_credits(inode->i_sb,
2544 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2549 * Now the new value will be stored inside. So if the new
2550 * value is smaller than the size of value root or the old
2551 * value, we don't need any allocation, otherwise we have
2552 * to guess metadata allocation.
2554 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2555 (!ocfs2_xattr_is_local(xe) &&
2556 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2561 /* calculate metadata allocation. */
2562 if (di->i_xattr_loc) {
2563 if (!xbs->xattr_bh) {
2564 ret = ocfs2_read_xattr_block(inode,
2565 le64_to_cpu(di->i_xattr_loc),
2572 xb = (struct ocfs2_xattr_block *)bh->b_data;
2574 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2577 * If there is already an xattr tree, good, we can calculate
2578 * like other b-trees. Otherwise we may have the chance of
2579 * create a tree, the credit calculation is borrowed from
2580 * ocfs2_calc_extend_credits with root_el = NULL. And the
2581 * new tree will be cluster based, so no meta is needed.
2583 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2584 struct ocfs2_extent_list *el =
2585 &xb->xb_attrs.xb_root.xt_list;
2586 meta_add += ocfs2_extend_meta_needed(el);
2587 credits += ocfs2_calc_extend_credits(inode->i_sb,
2590 credits += OCFS2_SUBALLOC_ALLOC + 1;
2593 * This cluster will be used either for new bucket or for
2595 * If the cluster size is the same as the bucket size, one
2596 * more is needed since we may need to extend the bucket
2600 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2601 if (OCFS2_XATTR_BUCKET_SIZE ==
2602 OCFS2_SB(inode->i_sb)->s_clustersize) {
2603 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2608 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2612 *clusters_need = clusters_add;
2614 *meta_need = meta_add;
2616 *credits_need = credits;
2621 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2622 struct ocfs2_dinode *di,
2623 struct ocfs2_xattr_info *xi,
2624 struct ocfs2_xattr_search *xis,
2625 struct ocfs2_xattr_search *xbs,
2626 struct ocfs2_xattr_set_ctxt *ctxt,
2630 int clusters_add, meta_add, ret;
2631 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2633 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2635 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2637 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
2638 &clusters_add, &meta_add, credits);
2644 meta_add += extra_meta;
2645 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2646 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
2649 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2658 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2664 if (ctxt->meta_ac) {
2665 ocfs2_free_alloc_context(ctxt->meta_ac);
2666 ctxt->meta_ac = NULL;
2670 * We cannot have an error and a non null ctxt->data_ac.
2677 static int __ocfs2_xattr_set_handle(struct inode *inode,
2678 struct ocfs2_dinode *di,
2679 struct ocfs2_xattr_info *xi,
2680 struct ocfs2_xattr_search *xis,
2681 struct ocfs2_xattr_search *xbs,
2682 struct ocfs2_xattr_set_ctxt *ctxt)
2684 int ret = 0, credits, old_found;
2687 /* Remove existing extended attribute */
2688 if (!xis->not_found)
2689 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2690 else if (!xbs->not_found)
2691 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2693 /* We always try to set extended attribute into inode first*/
2694 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2695 if (!ret && !xbs->not_found) {
2697 * If succeed and that extended attribute existing in
2698 * external block, then we will remove it.
2703 old_found = xis->not_found;
2704 xis->not_found = -ENODATA;
2705 ret = ocfs2_calc_xattr_set_need(inode,
2713 xis->not_found = old_found;
2719 ret = ocfs2_extend_trans(ctxt->handle, credits +
2720 ctxt->handle->h_buffer_credits);
2725 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2726 } else if (ret == -ENOSPC) {
2727 if (di->i_xattr_loc && !xbs->xattr_bh) {
2728 ret = ocfs2_xattr_block_find(inode,
2734 old_found = xis->not_found;
2735 xis->not_found = -ENODATA;
2736 ret = ocfs2_calc_xattr_set_need(inode,
2744 xis->not_found = old_found;
2750 ret = ocfs2_extend_trans(ctxt->handle, credits +
2751 ctxt->handle->h_buffer_credits);
2758 * If no space in inode, we will set extended attribute
2759 * into external block.
2761 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2764 if (!xis->not_found) {
2766 * If succeed and that extended attribute
2767 * existing in inode, we will remove it.
2771 xbs->not_found = -ENODATA;
2772 ret = ocfs2_calc_xattr_set_need(inode,
2785 ret = ocfs2_extend_trans(ctxt->handle, credits +
2786 ctxt->handle->h_buffer_credits);
2791 ret = ocfs2_xattr_ibody_set(inode, xi,
2798 /* Update inode ctime. */
2799 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2801 OCFS2_JOURNAL_ACCESS_WRITE);
2807 inode->i_ctime = CURRENT_TIME;
2808 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
2809 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
2810 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
2817 * This function only called duing creating inode
2818 * for init security/acl xattrs of the new inode.
2819 * All transanction credits have been reserved in mknod.
2821 int ocfs2_xattr_set_handle(handle_t *handle,
2822 struct inode *inode,
2823 struct buffer_head *di_bh,
2829 struct ocfs2_alloc_context *meta_ac,
2830 struct ocfs2_alloc_context *data_ac)
2832 struct ocfs2_dinode *di;
2835 struct ocfs2_xattr_info xi = {
2836 .name_index = name_index,
2839 .value_len = value_len,
2842 struct ocfs2_xattr_search xis = {
2843 .not_found = -ENODATA,
2846 struct ocfs2_xattr_search xbs = {
2847 .not_found = -ENODATA,
2850 struct ocfs2_xattr_set_ctxt ctxt = {
2856 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2860 * In extreme situation, may need xattr bucket when
2861 * block size is too small. And we have already reserved
2862 * the credits for bucket in mknod.
2864 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
2865 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2867 mlog_errno(-ENOMEM);
2872 xis.inode_bh = xbs.inode_bh = di_bh;
2873 di = (struct ocfs2_dinode *)di_bh->b_data;
2875 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2877 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2880 if (xis.not_found) {
2881 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2886 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2889 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2890 brelse(xbs.xattr_bh);
2891 ocfs2_xattr_bucket_free(xbs.bucket);
2899 * Set, replace or remove an extended attribute for this inode.
2900 * value is NULL to remove an existing extended attribute, else either
2901 * create or replace an extended attribute.
2903 int ocfs2_xattr_set(struct inode *inode,
2910 struct buffer_head *di_bh = NULL;
2911 struct ocfs2_dinode *di;
2912 int ret, credits, ref_meta = 0, ref_credits = 0;
2913 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2914 struct inode *tl_inode = osb->osb_tl_inode;
2915 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2916 struct ocfs2_refcount_tree *ref_tree = NULL;
2918 struct ocfs2_xattr_info xi = {
2919 .name_index = name_index,
2922 .value_len = value_len,
2925 struct ocfs2_xattr_search xis = {
2926 .not_found = -ENODATA,
2929 struct ocfs2_xattr_search xbs = {
2930 .not_found = -ENODATA,
2933 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2937 * Only xbs will be used on indexed trees. xis doesn't need a
2940 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2942 mlog_errno(-ENOMEM);
2946 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2949 goto cleanup_nolock;
2951 xis.inode_bh = xbs.inode_bh = di_bh;
2952 di = (struct ocfs2_dinode *)di_bh->b_data;
2954 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2956 * Scan inode and external block to find the same name
2957 * extended attribute and collect search infomation.
2959 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2962 if (xis.not_found) {
2963 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2968 if (xis.not_found && xbs.not_found) {
2970 if (flags & XATTR_REPLACE)
2977 if (flags & XATTR_CREATE)
2981 /* Check whether the value is refcounted and do some prepartion. */
2982 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
2983 (!xis.not_found || !xbs.not_found)) {
2984 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
2985 &xis, &xbs, &ref_tree,
2986 &ref_meta, &ref_credits);
2993 mutex_lock(&tl_inode->i_mutex);
2995 if (ocfs2_truncate_log_needs_flush(osb)) {
2996 ret = __ocfs2_flush_truncate_log(osb);
2998 mutex_unlock(&tl_inode->i_mutex);
3003 mutex_unlock(&tl_inode->i_mutex);
3005 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3006 &xbs, &ctxt, ref_meta, &credits);
3012 /* we need to update inode's ctime field, so add credit for it. */
3013 credits += OCFS2_INODE_UPDATE_CREDITS;
3014 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3015 if (IS_ERR(ctxt.handle)) {
3016 ret = PTR_ERR(ctxt.handle);
3021 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3023 ocfs2_commit_trans(osb, ctxt.handle);
3026 ocfs2_free_alloc_context(ctxt.data_ac);
3028 ocfs2_free_alloc_context(ctxt.meta_ac);
3029 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3030 ocfs2_schedule_truncate_log_flush(osb, 1);
3031 ocfs2_run_deallocs(osb, &ctxt.dealloc);
3035 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3036 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3037 if (!value && !ret) {
3038 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3042 ocfs2_inode_unlock(inode, 1);
3045 brelse(xbs.xattr_bh);
3046 ocfs2_xattr_bucket_free(xbs.bucket);
3052 * Find the xattr extent rec which may contains name_hash.
3053 * e_cpos will be the first name hash of the xattr rec.
3054 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3056 static int ocfs2_xattr_get_rec(struct inode *inode,
3061 struct ocfs2_extent_list *el)
3064 struct buffer_head *eb_bh = NULL;
3065 struct ocfs2_extent_block *eb;
3066 struct ocfs2_extent_rec *rec = NULL;
3069 if (el->l_tree_depth) {
3070 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3077 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3080 if (el->l_tree_depth) {
3081 ocfs2_error(inode->i_sb,
3082 "Inode %lu has non zero tree depth in "
3083 "xattr tree block %llu\n", inode->i_ino,
3084 (unsigned long long)eb_bh->b_blocknr);
3090 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3091 rec = &el->l_recs[i];
3093 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3094 e_blkno = le64_to_cpu(rec->e_blkno);
3100 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3101 "record (%u, %u, 0) in xattr", inode->i_ino,
3102 le32_to_cpu(rec->e_cpos),
3103 ocfs2_rec_clusters(el, rec));
3108 *p_blkno = le64_to_cpu(rec->e_blkno);
3109 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3111 *e_cpos = le32_to_cpu(rec->e_cpos);
3117 typedef int (xattr_bucket_func)(struct inode *inode,
3118 struct ocfs2_xattr_bucket *bucket,
3121 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3122 struct ocfs2_xattr_bucket *bucket,
3129 int i, ret = 0, cmp = 1, block_off, new_offset;
3130 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3131 size_t name_len = strlen(name);
3132 struct ocfs2_xattr_entry *xe = NULL;
3136 * We don't use binary search in the bucket because there
3137 * may be multiple entries with the same name hash.
3139 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3140 xe = &xh->xh_entries[i];
3142 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3144 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3147 cmp = name_index - ocfs2_xattr_get_type(xe);
3149 cmp = name_len - xe->xe_name_len;
3153 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3164 xe_name = bucket_block(bucket, block_off) + new_offset;
3165 if (!memcmp(name, xe_name, name_len)) {
3177 * Find the specified xattr entry in a series of buckets.
3178 * This series start from p_blkno and last for num_clusters.
3179 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3180 * the num of the valid buckets.
3182 * Return the buffer_head this xattr should reside in. And if the xattr's
3183 * hash is in the gap of 2 buckets, return the lower bucket.
3185 static int ocfs2_xattr_bucket_find(struct inode *inode,
3192 struct ocfs2_xattr_search *xs)
3195 struct ocfs2_xattr_header *xh = NULL;
3196 struct ocfs2_xattr_entry *xe = NULL;
3198 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3199 int low_bucket = 0, bucket, high_bucket;
3200 struct ocfs2_xattr_bucket *search;
3202 u64 blkno, lower_blkno = 0;
3204 search = ocfs2_xattr_bucket_new(inode);
3211 ret = ocfs2_read_xattr_bucket(search, p_blkno);
3217 xh = bucket_xh(search);
3218 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3219 while (low_bucket <= high_bucket) {
3220 ocfs2_xattr_bucket_relse(search);
3222 bucket = (low_bucket + high_bucket) / 2;
3223 blkno = p_blkno + bucket * blk_per_bucket;
3224 ret = ocfs2_read_xattr_bucket(search, blkno);
3230 xh = bucket_xh(search);
3231 xe = &xh->xh_entries[0];
3232 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3233 high_bucket = bucket - 1;
3238 * Check whether the hash of the last entry in our
3239 * bucket is larger than the search one. for an empty
3240 * bucket, the last one is also the first one.
3243 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3245 last_hash = le32_to_cpu(xe->xe_name_hash);
3247 /* record lower_blkno which may be the insert place. */
3248 lower_blkno = blkno;
3250 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3251 low_bucket = bucket + 1;
3255 /* the searched xattr should reside in this bucket if exists. */
3256 ret = ocfs2_find_xe_in_bucket(inode, search,
3257 name_index, name, name_hash,
3267 * Record the bucket we have found.
3268 * When the xattr's hash value is in the gap of 2 buckets, we will
3269 * always set it to the previous bucket.
3272 lower_blkno = p_blkno;
3274 /* This should be in cache - we just read it during the search */
3275 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3281 xs->header = bucket_xh(xs->bucket);
3282 xs->base = bucket_block(xs->bucket, 0);
3283 xs->end = xs->base + inode->i_sb->s_blocksize;
3286 xs->here = &xs->header->xh_entries[index];
3287 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3288 (unsigned long long)bucket_blkno(xs->bucket), index);
3293 ocfs2_xattr_bucket_free(search);
3297 static int ocfs2_xattr_index_block_find(struct inode *inode,
3298 struct buffer_head *root_bh,
3301 struct ocfs2_xattr_search *xs)
3304 struct ocfs2_xattr_block *xb =
3305 (struct ocfs2_xattr_block *)root_bh->b_data;
3306 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3307 struct ocfs2_extent_list *el = &xb_root->xt_list;
3309 u32 first_hash, num_clusters = 0;
3310 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3312 if (le16_to_cpu(el->l_next_free_rec) == 0)
3315 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3316 name, name_hash, name_index);
3318 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3325 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3327 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3328 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3331 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3332 p_blkno, first_hash, num_clusters, xs);
3338 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3341 xattr_bucket_func *func,
3345 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3346 u32 num_buckets = clusters * bpc;
3347 struct ocfs2_xattr_bucket *bucket;
3349 bucket = ocfs2_xattr_bucket_new(inode);
3351 mlog_errno(-ENOMEM);
3355 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3356 clusters, (unsigned long long)blkno);
3358 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3359 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3366 * The real bucket num in this series of blocks is stored
3367 * in the 1st bucket.
3370 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3372 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3373 (unsigned long long)blkno,
3374 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3376 ret = func(inode, bucket, para);
3377 if (ret && ret != -ERANGE)
3379 /* Fall through to bucket_relse() */
3382 ocfs2_xattr_bucket_relse(bucket);
3387 ocfs2_xattr_bucket_free(bucket);
3391 struct ocfs2_xattr_tree_list {
3397 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
3398 struct ocfs2_xattr_header *xh,
3405 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3408 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3410 *block_off = name_offset >> sb->s_blocksize_bits;
3411 *new_offset = name_offset % sb->s_blocksize;
3416 static int ocfs2_list_xattr_bucket(struct inode *inode,
3417 struct ocfs2_xattr_bucket *bucket,
3421 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
3422 int i, block_off, new_offset;
3423 const char *prefix, *name;
3425 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3426 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
3427 type = ocfs2_xattr_get_type(entry);
3428 prefix = ocfs2_xattr_prefix(type);
3431 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3439 name = (const char *)bucket_block(bucket, block_off) +
3441 ret = ocfs2_xattr_list_entry(xl->buffer,
3445 entry->xe_name_len);
3454 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
3455 struct buffer_head *blk_bh,
3456 xattr_tree_rec_func *rec_func,
3459 struct ocfs2_xattr_block *xb =
3460 (struct ocfs2_xattr_block *)blk_bh->b_data;
3461 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
3463 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3466 if (!el->l_next_free_rec || !rec_func)
3469 while (name_hash > 0) {
3470 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3471 &e_cpos, &num_clusters, el);
3477 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
3478 num_clusters, para);
3488 name_hash = e_cpos - 1;
3495 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
3496 struct buffer_head *root_bh,
3497 u64 blkno, u32 cpos, u32 len, void *para)
3499 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
3500 ocfs2_list_xattr_bucket, para);
3503 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3504 struct buffer_head *blk_bh,
3509 struct ocfs2_xattr_tree_list xl = {
3511 .buffer_size = buffer_size,
3515 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
3516 ocfs2_list_xattr_tree_rec, &xl);
3527 static int cmp_xe(const void *a, const void *b)
3529 const struct ocfs2_xattr_entry *l = a, *r = b;
3530 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3531 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3533 if (l_hash > r_hash)
3535 if (l_hash < r_hash)
3540 static void swap_xe(void *a, void *b, int size)
3542 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3545 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3546 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3550 * When the ocfs2_xattr_block is filled up, new bucket will be created
3551 * and all the xattr entries will be moved to the new bucket.
3552 * The header goes at the start of the bucket, and the names+values are
3553 * filled from the end. This is why *target starts as the last buffer.
3554 * Note: we need to sort the entries since they are not saved in order
3555 * in the ocfs2_xattr_block.
3557 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3558 struct buffer_head *xb_bh,
3559 struct ocfs2_xattr_bucket *bucket)
3561 int i, blocksize = inode->i_sb->s_blocksize;
3562 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3563 u16 offset, size, off_change;
3564 struct ocfs2_xattr_entry *xe;
3565 struct ocfs2_xattr_block *xb =
3566 (struct ocfs2_xattr_block *)xb_bh->b_data;
3567 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
3568 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3569 u16 count = le16_to_cpu(xb_xh->xh_count);
3570 char *src = xb_bh->b_data;
3571 char *target = bucket_block(bucket, blks - 1);
3573 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3574 (unsigned long long)xb_bh->b_blocknr,
3575 (unsigned long long)bucket_blkno(bucket));
3577 for (i = 0; i < blks; i++)
3578 memset(bucket_block(bucket, i), 0, blocksize);
3581 * Since the xe_name_offset is based on ocfs2_xattr_header,
3582 * there is a offset change corresponding to the change of
3583 * ocfs2_xattr_header's position.
3585 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3586 xe = &xb_xh->xh_entries[count - 1];
3587 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3588 size = blocksize - offset;
3590 /* copy all the names and values. */
3591 memcpy(target + offset, src + offset, size);
3593 /* Init new header now. */
3594 xh->xh_count = xb_xh->xh_count;
3595 xh->xh_num_buckets = cpu_to_le16(1);
3596 xh->xh_name_value_len = cpu_to_le16(size);
3597 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3599 /* copy all the entries. */
3600 target = bucket_block(bucket, 0);
3601 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3602 size = count * sizeof(struct ocfs2_xattr_entry);
3603 memcpy(target + offset, (char *)xb_xh + offset, size);
3605 /* Change the xe offset for all the xe because of the move. */
3606 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3607 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3608 for (i = 0; i < count; i++)
3609 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3611 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3612 offset, size, off_change);
3614 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3619 * After we move xattr from block to index btree, we have to
3620 * update ocfs2_xattr_search to the new xe and base.
3622 * When the entry is in xattr block, xattr_bh indicates the storage place.
3623 * While if the entry is in index b-tree, "bucket" indicates the
3624 * real place of the xattr.
3626 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3627 struct ocfs2_xattr_search *xs,
3628 struct buffer_head *old_bh)
3630 char *buf = old_bh->b_data;
3631 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3632 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
3635 xs->header = bucket_xh(xs->bucket);
3636 xs->base = bucket_block(xs->bucket, 0);
3637 xs->end = xs->base + inode->i_sb->s_blocksize;
3642 i = xs->here - old_xh->xh_entries;
3643 xs->here = &xs->header->xh_entries[i];
3646 static int ocfs2_xattr_create_index_block(struct inode *inode,
3647 struct ocfs2_xattr_search *xs,
3648 struct ocfs2_xattr_set_ctxt *ctxt)
3653 handle_t *handle = ctxt->handle;
3654 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3655 struct ocfs2_inode_info *oi = OCFS2_I(inode);
3656 struct buffer_head *xb_bh = xs->xattr_bh;
3657 struct ocfs2_xattr_block *xb =
3658 (struct ocfs2_xattr_block *)xb_bh->b_data;
3659 struct ocfs2_xattr_tree_root *xr;
3660 u16 xb_flags = le16_to_cpu(xb->xb_flags);
3662 mlog(0, "create xattr index block for %llu\n",
3663 (unsigned long long)xb_bh->b_blocknr);
3665 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
3666 BUG_ON(!xs->bucket);
3670 * We can use this lock for now, and maybe move to a dedicated mutex
3671 * if performance becomes a problem later.
3673 down_write(&oi->ip_alloc_sem);
3675 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
3676 OCFS2_JOURNAL_ACCESS_WRITE);
3682 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3683 1, 1, &bit_off, &len);
3690 * The bucket may spread in many blocks, and
3691 * we will only touch the 1st block and the last block
3692 * in the whole bucket(one for entry and one for data).
3694 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3696 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3697 (unsigned long long)blkno);
3699 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
3705 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3706 OCFS2_JOURNAL_ACCESS_CREATE);
3712 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3713 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3715 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3717 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3718 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3719 offsetof(struct ocfs2_xattr_block, xb_attrs));
3721 xr = &xb->xb_attrs.xb_root;
3722 xr->xt_clusters = cpu_to_le32(1);
3723 xr->xt_last_eb_blk = 0;
3724 xr->xt_list.l_tree_depth = 0;
3725 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3726 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3728 xr->xt_list.l_recs[0].e_cpos = 0;
3729 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3730 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3732 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3734 ocfs2_journal_dirty(handle, xb_bh);
3737 up_write(&oi->ip_alloc_sem);
3742 static int cmp_xe_offset(const void *a, const void *b)
3744 const struct ocfs2_xattr_entry *l = a, *r = b;
3745 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3746 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3748 if (l_name_offset < r_name_offset)
3750 if (l_name_offset > r_name_offset)
3756 * defrag a xattr bucket if we find that the bucket has some
3757 * holes beteen name/value pairs.
3758 * We will move all the name/value pairs to the end of the bucket
3759 * so that we can spare some space for insertion.
3761 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
3763 struct ocfs2_xattr_bucket *bucket)
3766 size_t end, offset, len, value_len;
3767 struct ocfs2_xattr_header *xh;
3768 char *entries, *buf, *bucket_buf = NULL;
3769 u64 blkno = bucket_blkno(bucket);
3771 size_t blocksize = inode->i_sb->s_blocksize;
3772 struct ocfs2_xattr_entry *xe;
3775 * In order to make the operation more efficient and generic,
3776 * we copy all the blocks into a contiguous memory and do the
3777 * defragment there, so if anything is error, we will not touch
3780 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3787 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3788 memcpy(buf, bucket_block(bucket, i), blocksize);
3790 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
3791 OCFS2_JOURNAL_ACCESS_WRITE);
3797 xh = (struct ocfs2_xattr_header *)bucket_buf;
3798 entries = (char *)xh->xh_entries;
3799 xh_free_start = le16_to_cpu(xh->xh_free_start);
3801 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3802 "xh_free_start = %u, xh_name_value_len = %u.\n",
3803 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3804 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
3807 * sort all the entries by their offset.
3808 * the largest will be the first, so that we can
3809 * move them to the end one by one.
3811 sort(entries, le16_to_cpu(xh->xh_count),
3812 sizeof(struct ocfs2_xattr_entry),
3813 cmp_xe_offset, swap_xe);
3815 /* Move all name/values to the end of the bucket. */
3816 xe = xh->xh_entries;
3817 end = OCFS2_XATTR_BUCKET_SIZE;
3818 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3819 offset = le16_to_cpu(xe->xe_name_offset);
3820 if (ocfs2_xattr_is_local(xe))
3821 value_len = OCFS2_XATTR_SIZE(
3822 le64_to_cpu(xe->xe_value_size));
3824 value_len = OCFS2_XATTR_ROOT_SIZE;
3825 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3828 * We must make sure that the name/value pair
3829 * exist in the same block. So adjust end to
3830 * the previous block end if needed.
3832 if (((end - len) / blocksize !=
3833 (end - 1) / blocksize))
3834 end = end - end % blocksize;
3836 if (end > offset + len) {
3837 memmove(bucket_buf + end - len,
3838 bucket_buf + offset, len);
3839 xe->xe_name_offset = cpu_to_le16(end - len);
3842 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3843 "bucket %llu\n", (unsigned long long)blkno);
3848 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3849 "bucket %llu\n", (unsigned long long)blkno);
3851 if (xh_free_start == end)
3854 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3855 xh->xh_free_start = cpu_to_le16(end);
3857 /* sort the entries by their name_hash. */
3858 sort(entries, le16_to_cpu(xh->xh_count),
3859 sizeof(struct ocfs2_xattr_entry),
3863 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3864 memcpy(bucket_block(bucket, i), buf, blocksize);
3865 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
3873 * prev_blkno points to the start of an existing extent. new_blkno
3874 * points to a newly allocated extent. Because we know each of our
3875 * clusters contains more than bucket, we can easily split one cluster
3876 * at a bucket boundary. So we take the last cluster of the existing
3877 * extent and split it down the middle. We move the last half of the
3878 * buckets in the last cluster of the existing extent over to the new
3881 * first_bh is the buffer at prev_blkno so we can update the existing
3882 * extent's bucket count. header_bh is the bucket were we were hoping
3883 * to insert our xattr. If the bucket move places the target in the new
3884 * extent, we'll update first_bh and header_bh after modifying the old
3887 * first_hash will be set as the 1st xe's name_hash in the new extent.
3889 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3891 struct ocfs2_xattr_bucket *first,
3892 struct ocfs2_xattr_bucket *target,
3898 struct super_block *sb = inode->i_sb;
3899 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
3900 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
3901 int to_move = num_buckets / 2;
3903 u64 last_cluster_blkno = bucket_blkno(first) +
3904 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
3906 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
3907 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
3909 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3910 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
3912 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
3913 last_cluster_blkno, new_blkno,
3914 to_move, first_hash);
3920 /* This is the first bucket that got moved */
3921 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
3924 * If the target bucket was part of the moved buckets, we need to
3925 * update first and target.
3927 if (bucket_blkno(target) >= src_blkno) {
3928 /* Find the block for the new target bucket */
3929 src_blkno = new_blkno +
3930 (bucket_blkno(target) - src_blkno);
3932 ocfs2_xattr_bucket_relse(first);
3933 ocfs2_xattr_bucket_relse(target);
3936 * These shouldn't fail - the buffers are in the
3937 * journal from ocfs2_cp_xattr_bucket().
3939 ret = ocfs2_read_xattr_bucket(first, new_blkno);
3944 ret = ocfs2_read_xattr_bucket(target, src_blkno);
3955 * Find the suitable pos when we divide a bucket into 2.
3956 * We have to make sure the xattrs with the same hash value exist
3957 * in the same bucket.
3959 * If this ocfs2_xattr_header covers more than one hash value, find a
3960 * place where the hash value changes. Try to find the most even split.
3961 * The most common case is that all entries have different hash values,
3962 * and the first check we make will find a place to split.
3964 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3966 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3967 int count = le16_to_cpu(xh->xh_count);
3968 int delta, middle = count / 2;
3971 * We start at the middle. Each step gets farther away in both
3972 * directions. We therefore hit the change in hash value
3973 * nearest to the middle. Note that this loop does not execute for
3976 for (delta = 0; delta < middle; delta++) {
3977 /* Let's check delta earlier than middle */
3978 if (cmp_xe(&entries[middle - delta - 1],
3979 &entries[middle - delta]))
3980 return middle - delta;
3982 /* For even counts, don't walk off the end */
3983 if ((middle + delta + 1) == count)
3986 /* Now try delta past middle */
3987 if (cmp_xe(&entries[middle + delta],
3988 &entries[middle + delta + 1]))
3989 return middle + delta + 1;
3992 /* Every entry had the same hash */
3997 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3998 * first_hash will record the 1st hash of the new bucket.
4000 * Normally half of the xattrs will be moved. But we have to make
4001 * sure that the xattrs with the same hash value are stored in the
4002 * same bucket. If all the xattrs in this bucket have the same hash
4003 * value, the new bucket will be initialized as an empty one and the
4004 * first_hash will be initialized as (hash_value+1).
4006 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4011 int new_bucket_head)
4014 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
4015 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4016 struct ocfs2_xattr_header *xh;
4017 struct ocfs2_xattr_entry *xe;
4018 int blocksize = inode->i_sb->s_blocksize;
4020 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4021 (unsigned long long)blk, (unsigned long long)new_blk);
4023 s_bucket = ocfs2_xattr_bucket_new(inode);
4024 t_bucket = ocfs2_xattr_bucket_new(inode);
4025 if (!s_bucket || !t_bucket) {
4031 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4037 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4038 OCFS2_JOURNAL_ACCESS_WRITE);
4045 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4046 * there's no need to read it.
4048 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4055 * Hey, if we're overwriting t_bucket, what difference does
4056 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4057 * same part of ocfs2_cp_xattr_bucket().
4059 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4061 OCFS2_JOURNAL_ACCESS_CREATE :
4062 OCFS2_JOURNAL_ACCESS_WRITE);
4068 xh = bucket_xh(s_bucket);
4069 count = le16_to_cpu(xh->xh_count);
4070 start = ocfs2_xattr_find_divide_pos(xh);
4072 if (start == count) {
4073 xe = &xh->xh_entries[start-1];
4076 * initialized a new empty bucket here.
4077 * The hash value is set as one larger than
4078 * that of the last entry in the previous bucket.
4080 for (i = 0; i < t_bucket->bu_blocks; i++)
4081 memset(bucket_block(t_bucket, i), 0, blocksize);
4083 xh = bucket_xh(t_bucket);
4084 xh->xh_free_start = cpu_to_le16(blocksize);
4085 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4086 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4088 goto set_num_buckets;
4091 /* copy the whole bucket to the new first. */
4092 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4094 /* update the new bucket. */
4095 xh = bucket_xh(t_bucket);
4098 * Calculate the total name/value len and xh_free_start for
4099 * the old bucket first.
4101 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4103 for (i = 0; i < start; i++) {
4104 xe = &xh->xh_entries[i];
4105 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
4106 if (ocfs2_xattr_is_local(xe))
4108 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4110 xe_len += OCFS2_XATTR_ROOT_SIZE;
4111 name_value_len += xe_len;
4112 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4113 name_offset = le16_to_cpu(xe->xe_name_offset);
4117 * Now begin the modification to the new bucket.
4119 * In the new bucket, We just move the xattr entry to the beginning
4120 * and don't touch the name/value. So there will be some holes in the
4121 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4124 xe = &xh->xh_entries[start];
4125 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4126 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
4127 (int)((char *)xe - (char *)xh),
4128 (int)((char *)xh->xh_entries - (char *)xh));
4129 memmove((char *)xh->xh_entries, (char *)xe, len);
4130 xe = &xh->xh_entries[count - start];
4131 len = sizeof(struct ocfs2_xattr_entry) * start;
4132 memset((char *)xe, 0, len);
4134 le16_add_cpu(&xh->xh_count, -start);
4135 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4137 /* Calculate xh_free_start for the new bucket. */
4138 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4139 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4140 xe = &xh->xh_entries[i];
4141 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
4142 if (ocfs2_xattr_is_local(xe))
4144 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4146 xe_len += OCFS2_XATTR_ROOT_SIZE;
4147 if (le16_to_cpu(xe->xe_name_offset) <
4148 le16_to_cpu(xh->xh_free_start))
4149 xh->xh_free_start = xe->xe_name_offset;
4153 /* set xh->xh_num_buckets for the new xh. */
4154 if (new_bucket_head)
4155 xh->xh_num_buckets = cpu_to_le16(1);
4157 xh->xh_num_buckets = 0;
4159 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4161 /* store the first_hash of the new bucket. */
4163 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4166 * Now only update the 1st block of the old bucket. If we
4167 * just added a new empty bucket, there is no need to modify
4173 xh = bucket_xh(s_bucket);
4174 memset(&xh->xh_entries[start], 0,
4175 sizeof(struct ocfs2_xattr_entry) * (count - start));
4176 xh->xh_count = cpu_to_le16(start);
4177 xh->xh_free_start = cpu_to_le16(name_offset);
4178 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4180 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4183 ocfs2_xattr_bucket_free(s_bucket);
4184 ocfs2_xattr_bucket_free(t_bucket);
4190 * Copy xattr from one bucket to another bucket.
4192 * The caller must make sure that the journal transaction
4193 * has enough space for journaling.
4195 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4202 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4204 BUG_ON(s_blkno == t_blkno);
4206 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4207 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4210 s_bucket = ocfs2_xattr_bucket_new(inode);
4211 t_bucket = ocfs2_xattr_bucket_new(inode);
4212 if (!s_bucket || !t_bucket) {
4218 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4223 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4224 * there's no need to read it.
4226 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4231 * Hey, if we're overwriting t_bucket, what difference does
4232 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4233 * cluster to fill, we came here from
4234 * ocfs2_mv_xattr_buckets(), and it is really new -
4235 * ACCESS_CREATE is required. But we also might have moved data
4236 * out of t_bucket before extending back into it.
4237 * ocfs2_add_new_xattr_bucket() can do this - its call to
4238 * ocfs2_add_new_xattr_cluster() may have created a new extent
4239 * and copied out the end of the old extent. Then it re-extends
4240 * the old extent back to create space for new xattrs. That's
4241 * how we get here, and the bucket isn't really new.
4243 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4245 OCFS2_JOURNAL_ACCESS_CREATE :
4246 OCFS2_JOURNAL_ACCESS_WRITE);
4250 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4251 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4254 ocfs2_xattr_bucket_free(t_bucket);
4255 ocfs2_xattr_bucket_free(s_bucket);
4261 * src_blk points to the start of an existing extent. last_blk points to
4262 * last cluster in that extent. to_blk points to a newly allocated
4263 * extent. We copy the buckets from the cluster at last_blk to the new
4264 * extent. If start_bucket is non-zero, we skip that many buckets before
4265 * we start copying. The new extent's xh_num_buckets gets set to the
4266 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4267 * by the same amount.
4269 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4270 u64 src_blk, u64 last_blk, u64 to_blk,
4271 unsigned int start_bucket,
4274 int i, ret, credits;
4275 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4276 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4277 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4278 struct ocfs2_xattr_bucket *old_first, *new_first;
4280 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4281 (unsigned long long)last_blk, (unsigned long long)to_blk);
4283 BUG_ON(start_bucket >= num_buckets);
4285 num_buckets -= start_bucket;
4286 last_blk += (start_bucket * blks_per_bucket);
4289 /* The first bucket of the original extent */
4290 old_first = ocfs2_xattr_bucket_new(inode);
4291 /* The first bucket of the new extent */
4292 new_first = ocfs2_xattr_bucket_new(inode);
4293 if (!old_first || !new_first) {
4299 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4306 * We need to update the first bucket of the old extent and all
4307 * the buckets going to the new extent.
4309 credits = ((num_buckets + 1) * blks_per_bucket) +
4310 handle->h_buffer_credits;
4311 ret = ocfs2_extend_trans(handle, credits);
4317 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4318 OCFS2_JOURNAL_ACCESS_WRITE);
4324 for (i = 0; i < num_buckets; i++) {
4325 ret = ocfs2_cp_xattr_bucket(inode, handle,
4326 last_blk + (i * blks_per_bucket),
4327 to_blk + (i * blks_per_bucket),
4336 * Get the new bucket ready before we dirty anything
4337 * (This actually shouldn't fail, because we already dirtied
4338 * it once in ocfs2_cp_xattr_bucket()).
4340 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4345 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4346 OCFS2_JOURNAL_ACCESS_WRITE);
4352 /* Now update the headers */
4353 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4354 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4356 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4357 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4360 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4363 ocfs2_xattr_bucket_free(new_first);
4364 ocfs2_xattr_bucket_free(old_first);
4369 * Move some xattrs in this cluster to the new cluster.
4370 * This function should only be called when bucket size == cluster size.
4371 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4373 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4379 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4380 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
4382 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4384 ret = ocfs2_extend_trans(handle, credits);
4390 /* Move half of the xattr in start_blk to the next bucket. */
4391 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4392 new_blk, first_hash, 1);
4396 * Move some xattrs from the old cluster to the new one since they are not
4397 * contiguous in ocfs2 xattr tree.
4399 * new_blk starts a new separate cluster, and we will move some xattrs from
4400 * prev_blk to it. v_start will be set as the first name hash value in this
4401 * new cluster so that it can be used as e_cpos during tree insertion and
4402 * don't collide with our original b-tree operations. first_bh and header_bh
4403 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4404 * to extend the insert bucket.
4406 * The problem is how much xattr should we move to the new one and when should
4407 * we update first_bh and header_bh?
4408 * 1. If cluster size > bucket size, that means the previous cluster has more
4409 * than 1 bucket, so just move half nums of bucket into the new cluster and
4410 * update the first_bh and header_bh if the insert bucket has been moved
4411 * to the new cluster.
4412 * 2. If cluster_size == bucket_size:
4413 * a) If the previous extent rec has more than one cluster and the insert
4414 * place isn't in the last cluster, copy the entire last cluster to the
4415 * new one. This time, we don't need to upate the first_bh and header_bh
4416 * since they will not be moved into the new cluster.
4417 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4418 * the new one. And we set the extend flag to zero if the insert place is
4419 * moved into the new allocated cluster since no extend is needed.
4421 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4423 struct ocfs2_xattr_bucket *first,
4424 struct ocfs2_xattr_bucket *target,
4432 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4433 (unsigned long long)bucket_blkno(first), prev_clusters,
4434 (unsigned long long)new_blk);
4436 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
4437 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4446 /* The start of the last cluster in the first extent */
4447 u64 last_blk = bucket_blkno(first) +
4448 ((prev_clusters - 1) *
4449 ocfs2_clusters_to_blocks(inode->i_sb, 1));
4451 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
4452 ret = ocfs2_mv_xattr_buckets(inode, handle,
4453 bucket_blkno(first),
4454 last_blk, new_blk, 0,
4459 ret = ocfs2_divide_xattr_cluster(inode, handle,
4465 if ((bucket_blkno(target) == last_blk) && extend)
4474 * Add a new cluster for xattr storage.
4476 * If the new cluster is contiguous with the previous one, it will be
4477 * appended to the same extent record, and num_clusters will be updated.
4478 * If not, we will insert a new extent for it and move some xattrs in
4479 * the last cluster into the new allocated one.
4480 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4481 * lose the benefits of hashing because we'll have to search large leaves.
4482 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4485 * first_bh is the first block of the previous extent rec and header_bh
4486 * indicates the bucket we will insert the new xattrs. They will be updated
4487 * when the header_bh is moved into the new cluster.
4489 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4490 struct buffer_head *root_bh,
4491 struct ocfs2_xattr_bucket *first,
4492 struct ocfs2_xattr_bucket *target,
4496 struct ocfs2_xattr_set_ctxt *ctxt)
4499 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4500 u32 prev_clusters = *num_clusters;
4501 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4503 handle_t *handle = ctxt->handle;
4504 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4505 struct ocfs2_extent_tree et;
4507 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4508 "previous xattr blkno = %llu\n",
4509 (unsigned long long)OCFS2_I(inode)->ip_blkno,
4510 prev_cpos, (unsigned long long)bucket_blkno(first));
4512 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
4514 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
4515 OCFS2_JOURNAL_ACCESS_WRITE);
4521 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
4522 clusters_to_add, &bit_off, &num_bits);
4529 BUG_ON(num_bits > clusters_to_add);
4531 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4532 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4533 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4535 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
4536 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4537 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4539 * If this cluster is contiguous with the old one and
4540 * adding this new cluster, we don't surpass the limit of
4541 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4542 * initialized and used like other buckets in the previous
4544 * So add it as a contiguous one. The caller will handle
4547 v_start = prev_cpos + prev_clusters;
4548 *num_clusters = prev_clusters + num_bits;
4549 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4552 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4566 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
4567 num_bits, (unsigned long long)block, v_start);
4568 ret = ocfs2_insert_extent(handle, &et, v_start, block,
4569 num_bits, 0, ctxt->meta_ac);
4575 ret = ocfs2_journal_dirty(handle, root_bh);
4584 * We are given an extent. 'first' is the bucket at the very front of
4585 * the extent. The extent has space for an additional bucket past
4586 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4587 * of the target bucket. We wish to shift every bucket past the target
4588 * down one, filling in that additional space. When we get back to the
4589 * target, we split the target between itself and the now-empty bucket
4590 * at target+1 (aka, target_blkno + blks_per_bucket).
4592 static int ocfs2_extend_xattr_bucket(struct inode *inode,
4594 struct ocfs2_xattr_bucket *first,
4599 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4600 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4602 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
4604 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
4605 "from %llu, len = %u\n", (unsigned long long)target_blk,
4606 (unsigned long long)bucket_blkno(first), num_clusters);
4608 /* The extent must have room for an additional bucket */
4609 BUG_ON(new_bucket >=
4610 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
4612 /* end_blk points to the last existing bucket */
4613 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
4616 * end_blk is the start of the last existing bucket.
4617 * Thus, (end_blk - target_blk) covers the target bucket and
4618 * every bucket after it up to, but not including, the last
4619 * existing bucket. Then we add the last existing bucket, the
4620 * new bucket, and the first bucket (3 * blk_per_bucket).
4622 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
4623 handle->h_buffer_credits;
4624 ret = ocfs2_extend_trans(handle, credits);
4630 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4631 OCFS2_JOURNAL_ACCESS_WRITE);
4637 while (end_blk != target_blk) {
4638 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4639 end_blk + blk_per_bucket, 0);
4642 end_blk -= blk_per_bucket;
4645 /* Move half of the xattr in target_blkno to the next bucket. */
4646 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4647 target_blk + blk_per_bucket, NULL, 0);
4649 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4650 ocfs2_xattr_bucket_journal_dirty(handle, first);
4657 * Add new xattr bucket in an extent record and adjust the buckets
4658 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
4659 * bucket we want to insert into.
4661 * In the easy case, we will move all the buckets after target down by
4662 * one. Half of target's xattrs will be moved to the next bucket.
4664 * If current cluster is full, we'll allocate a new one. This may not
4665 * be contiguous. The underlying calls will make sure that there is
4666 * space for the insert, shifting buckets around if necessary.
4667 * 'target' may be moved by those calls.
4669 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4670 struct buffer_head *xb_bh,
4671 struct ocfs2_xattr_bucket *target,
4672 struct ocfs2_xattr_set_ctxt *ctxt)
4674 struct ocfs2_xattr_block *xb =
4675 (struct ocfs2_xattr_block *)xb_bh->b_data;
4676 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4677 struct ocfs2_extent_list *el = &xb_root->xt_list;
4679 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
4680 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4681 int ret, num_buckets, extend = 1;
4683 u32 e_cpos, num_clusters;
4684 /* The bucket at the front of the extent */
4685 struct ocfs2_xattr_bucket *first;
4687 mlog(0, "Add new xattr bucket starting from %llu\n",
4688 (unsigned long long)bucket_blkno(target));
4690 /* The first bucket of the original extent */
4691 first = ocfs2_xattr_bucket_new(inode);
4698 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4705 ret = ocfs2_read_xattr_bucket(first, p_blkno);
4711 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
4712 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
4714 * This can move first+target if the target bucket moves
4715 * to the new extent.
4717 ret = ocfs2_add_new_xattr_cluster(inode,
4732 ret = ocfs2_extend_xattr_bucket(inode,
4735 bucket_blkno(target),
4742 ocfs2_xattr_bucket_free(first);
4747 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4748 struct ocfs2_xattr_bucket *bucket,
4751 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4753 offs = offs % inode->i_sb->s_blocksize;
4754 return bucket_block(bucket, block_off) + offs;
4758 * Handle the normal xattr set, including replace, delete and new.
4760 * Note: "local" indicates the real data's locality. So we can't
4761 * just its bucket locality by its length.
4763 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4764 struct ocfs2_xattr_info *xi,
4765 struct ocfs2_xattr_search *xs,
4769 struct ocfs2_xattr_entry *last, *xe;
4770 int name_len = strlen(xi->name);
4771 struct ocfs2_xattr_header *xh = xs->header;
4772 u16 count = le16_to_cpu(xh->xh_count), start;
4773 size_t blocksize = inode->i_sb->s_blocksize;
4775 size_t offs, size, new_size;
4777 last = &xh->xh_entries[count];
4778 if (!xs->not_found) {
4780 offs = le16_to_cpu(xe->xe_name_offset);
4781 if (ocfs2_xattr_is_local(xe))
4782 size = OCFS2_XATTR_SIZE(name_len) +
4783 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4785 size = OCFS2_XATTR_SIZE(name_len) +
4786 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4789 * If the new value will be stored outside, xi->value has been
4790 * initalized as an empty ocfs2_xattr_value_root, and the same
4791 * goes with xi->value_len, so we can set new_size safely here.
4792 * See ocfs2_xattr_set_in_bucket.
4794 new_size = OCFS2_XATTR_SIZE(name_len) +
4795 OCFS2_XATTR_SIZE(xi->value_len);
4797 le16_add_cpu(&xh->xh_name_value_len, -size);
4799 if (new_size > size)
4800 goto set_new_name_value;
4802 /* Now replace the old value with new one. */
4804 xe->xe_value_size = cpu_to_le64(xi->value_len);
4806 xe->xe_value_size = 0;
4808 val = ocfs2_xattr_bucket_get_val(inode,
4810 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4811 size - OCFS2_XATTR_SIZE(name_len));
4812 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4813 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4814 xi->value, xi->value_len);
4816 le16_add_cpu(&xh->xh_name_value_len, new_size);
4817 ocfs2_xattr_set_local(xe, local);
4821 * Remove the old entry if there is more than one.
4822 * We don't remove the last entry so that we can
4823 * use it to indicate the hash value of the empty
4827 le16_add_cpu(&xh->xh_count, -1);
4830 (void *)last - (void *)xe);
4832 sizeof(struct ocfs2_xattr_entry));
4835 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4840 /* find a new entry for insert. */
4841 int low = 0, high = count - 1, tmp;
4842 struct ocfs2_xattr_entry *tmp_xe;
4844 while (low <= high && count) {
4845 tmp = (low + high) / 2;
4846 tmp_xe = &xh->xh_entries[tmp];
4848 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4850 else if (name_hash <
4851 le32_to_cpu(tmp_xe->xe_name_hash))
4859 xe = &xh->xh_entries[low];
4861 memmove(xe + 1, xe, (void *)last - (void *)xe);
4863 le16_add_cpu(&xh->xh_count, 1);
4864 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4865 xe->xe_name_hash = cpu_to_le32(name_hash);
4866 xe->xe_name_len = name_len;
4867 ocfs2_xattr_set_type(xe, xi->name_index);
4871 /* Insert the new name+value. */
4872 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4875 * We must make sure that the name/value pair
4876 * exists in the same block.
4878 offs = le16_to_cpu(xh->xh_free_start);
4879 start = offs - size;
4881 if (start >> inode->i_sb->s_blocksize_bits !=
4882 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4883 offs = offs - offs % blocksize;
4884 xh->xh_free_start = cpu_to_le16(offs);
4887 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
4888 xe->xe_name_offset = cpu_to_le16(offs - size);
4890 memset(val, 0, size);
4891 memcpy(val, xi->name, name_len);
4892 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4894 xe->xe_value_size = cpu_to_le64(xi->value_len);
4895 ocfs2_xattr_set_local(xe, local);
4897 le16_add_cpu(&xh->xh_free_start, -size);
4898 le16_add_cpu(&xh->xh_name_value_len, size);
4904 * Set the xattr entry in the specified bucket.
4905 * The bucket is indicated by xs->bucket and it should have the enough
4906 * space for the xattr insertion.
4908 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4910 struct ocfs2_xattr_info *xi,
4911 struct ocfs2_xattr_search *xs,
4918 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4919 (unsigned long)xi->value_len, xi->name_index,
4920 (unsigned long long)bucket_blkno(xs->bucket));
4922 if (!xs->bucket->bu_bhs[1]) {
4923 blkno = bucket_blkno(xs->bucket);
4924 ocfs2_xattr_bucket_relse(xs->bucket);
4925 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
4932 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4933 OCFS2_JOURNAL_ACCESS_WRITE);
4939 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4940 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4947 * Truncate the specified xe_off entry in xattr bucket.
4948 * bucket is indicated by header_bh and len is the new length.
4949 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4951 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4953 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4954 struct ocfs2_xattr_bucket *bucket,
4957 struct ocfs2_xattr_set_ctxt *ctxt)
4961 struct ocfs2_xattr_entry *xe;
4962 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4963 size_t blocksize = inode->i_sb->s_blocksize;
4964 struct ocfs2_xattr_value_buf vb = {
4965 .vb_access = ocfs2_journal_access,
4968 xe = &xh->xh_entries[xe_off];
4970 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4972 offset = le16_to_cpu(xe->xe_name_offset) +
4973 OCFS2_XATTR_SIZE(xe->xe_name_len);
4975 value_blk = offset / blocksize;
4977 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4978 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4980 vb.vb_bh = bucket->bu_bhs[value_blk];
4983 vb.vb_xv = (struct ocfs2_xattr_value_root *)
4984 (vb.vb_bh->b_data + offset % blocksize);
4987 * From here on out we have to dirty the bucket. The generic
4988 * value calls only modify one of the bucket's bhs, but we need
4989 * to send the bucket at once. So if they error, they *could* have
4990 * modified something. We have to assume they did, and dirty
4991 * the whole bucket. This leaves us in a consistent state.
4993 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4994 xe_off, (unsigned long long)bucket_blkno(bucket), len);
4995 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5001 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5002 OCFS2_JOURNAL_ACCESS_WRITE);
5008 xe->xe_value_size = cpu_to_le64(len);
5010 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5016 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
5017 struct ocfs2_xattr_search *xs,
5019 struct ocfs2_xattr_set_ctxt *ctxt)
5022 struct ocfs2_xattr_entry *xe = xs->here;
5023 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
5025 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
5027 offset = xe - xh->xh_entries;
5028 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
5036 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
5038 struct ocfs2_xattr_search *xs,
5042 int ret, offset, block_off;
5043 struct ocfs2_xattr_value_root *xv;
5044 struct ocfs2_xattr_entry *xe = xs->here;
5045 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5047 struct ocfs2_xattr_value_buf vb = {
5048 .vb_access = ocfs2_journal_access,
5051 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
5053 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
5054 xe - xh->xh_entries,
5062 base = bucket_block(xs->bucket, block_off);
5063 xv = (struct ocfs2_xattr_value_root *)(base + offset +
5064 OCFS2_XATTR_SIZE(xe->xe_name_len));
5067 vb.vb_bh = xs->bucket->bu_bhs[block_off];
5068 ret = __ocfs2_xattr_set_value_outside(inode, handle,
5069 &vb, val, value_len);
5076 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5077 struct buffer_head *root_bh,
5084 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5085 struct inode *tl_inode = osb->osb_tl_inode;
5087 struct ocfs2_xattr_block *xb =
5088 (struct ocfs2_xattr_block *)root_bh->b_data;
5089 struct ocfs2_alloc_context *meta_ac = NULL;
5090 struct ocfs2_cached_dealloc_ctxt dealloc;
5091 struct ocfs2_extent_tree et;
5093 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5094 ocfs2_delete_xattr_in_bucket, para);
5100 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5102 ocfs2_init_dealloc_ctxt(&dealloc);
5104 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5105 cpos, len, (unsigned long long)blkno);
5107 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5110 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5116 mutex_lock(&tl_inode->i_mutex);
5118 if (ocfs2_truncate_log_needs_flush(osb)) {
5119 ret = __ocfs2_flush_truncate_log(osb);
5126 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5127 if (IS_ERR(handle)) {
5133 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5134 OCFS2_JOURNAL_ACCESS_WRITE);
5140 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5147 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5149 ret = ocfs2_journal_dirty(handle, root_bh);
5155 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5160 ocfs2_commit_trans(osb, handle);
5162 ocfs2_schedule_truncate_log_flush(osb, 1);
5164 mutex_unlock(&tl_inode->i_mutex);
5167 ocfs2_free_alloc_context(meta_ac);
5169 ocfs2_run_deallocs(osb, &dealloc);
5174 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
5176 struct ocfs2_xattr_search *xs)
5178 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5179 struct ocfs2_xattr_entry *last = &xh->xh_entries[
5180 le16_to_cpu(xh->xh_count) - 1];
5183 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
5184 OCFS2_JOURNAL_ACCESS_WRITE);
5190 /* Remove the old entry. */
5191 memmove(xs->here, xs->here + 1,
5192 (void *)last - (void *)xs->here);
5193 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
5194 le16_add_cpu(&xh->xh_count, -1);
5196 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
5200 * Set the xattr name/value in the bucket specified in xs.
5202 * As the new value in xi may be stored in the bucket or in an outside cluster,
5203 * we divide the whole process into 3 steps:
5204 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
5205 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
5206 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
5207 * 4. If the clusters for the new outside value can't be allocated, we need
5208 * to free the xattr we allocated in set.
5210 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5211 struct ocfs2_xattr_info *xi,
5212 struct ocfs2_xattr_search *xs,
5213 struct ocfs2_xattr_set_ctxt *ctxt)
5217 char *val = (char *)xi->value;
5218 struct ocfs2_xattr_entry *xe = xs->here;
5219 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
5222 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
5224 * We need to truncate the xattr storage first.
5226 * If both the old and new value are stored to
5227 * outside block, we only need to truncate
5228 * the storage and then set the value outside.
5230 * If the new value should be stored within block,
5231 * we should free all the outside block first and
5232 * the modification to the xattr block will be done
5233 * by following steps.
5235 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5236 value_len = xi->value_len;
5240 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5247 goto set_value_outside;
5250 value_len = xi->value_len;
5251 /* So we have to handle the inside block change now. */
5252 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
5254 * If the new value will be stored outside of block,
5255 * initalize a new empty value root and insert it first.
5258 xi->value = &def_xv;
5259 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
5262 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
5269 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
5272 /* allocate the space now for the outside block storage. */
5273 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5278 if (xs->not_found) {
5280 * We can't allocate enough clusters for outside
5281 * storage and we have allocated xattr already,
5282 * so need to remove it.
5284 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
5290 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
5291 xs, val, value_len);
5297 * check whether the xattr bucket is filled up with the same hash value.
5298 * If we want to insert the xattr with the same hash, return -ENOSPC.
5299 * If we want to insert a xattr with different hash value, go ahead
5300 * and ocfs2_divide_xattr_bucket will handle this.
5302 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5303 struct ocfs2_xattr_bucket *bucket,
5306 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5307 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5309 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5312 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5313 xh->xh_entries[0].xe_name_hash) {
5314 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5316 (unsigned long long)bucket_blkno(bucket),
5317 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5324 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5325 struct ocfs2_xattr_info *xi,
5326 struct ocfs2_xattr_search *xs,
5327 struct ocfs2_xattr_set_ctxt *ctxt)
5329 struct ocfs2_xattr_header *xh;
5330 struct ocfs2_xattr_entry *xe;
5331 u16 count, header_size, xh_free_start;
5332 int free, max_free, need, old;
5333 size_t value_size = 0, name_len = strlen(xi->name);
5334 size_t blocksize = inode->i_sb->s_blocksize;
5335 int ret, allocation = 0;
5337 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
5341 count = le16_to_cpu(xh->xh_count);
5342 xh_free_start = le16_to_cpu(xh->xh_free_start);
5343 header_size = sizeof(struct ocfs2_xattr_header) +
5344 count * sizeof(struct ocfs2_xattr_entry);
5345 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5346 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
5348 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5349 "of %u which exceed block size\n",
5350 (unsigned long long)bucket_blkno(xs->bucket),
5353 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5354 value_size = OCFS2_XATTR_ROOT_SIZE;
5356 value_size = OCFS2_XATTR_SIZE(xi->value_len);
5359 need = sizeof(struct ocfs2_xattr_entry) +
5360 OCFS2_XATTR_SIZE(name_len) + value_size;
5362 need = value_size + OCFS2_XATTR_SIZE(name_len);
5365 * We only replace the old value if the new length is smaller
5366 * than the old one. Otherwise we will allocate new space in the
5367 * bucket to store it.
5370 if (ocfs2_xattr_is_local(xe))
5371 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5373 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5375 if (old >= value_size)
5379 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
5381 * We need to make sure the new name/value pair
5382 * can exist in the same block.
5384 if (xh_free_start % blocksize < need)
5385 free -= xh_free_start % blocksize;
5387 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5388 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5389 " %u\n", xs->not_found,
5390 (unsigned long long)bucket_blkno(xs->bucket),
5391 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5392 le16_to_cpu(xh->xh_name_value_len));
5396 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
5397 if (need <= max_free &&
5398 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5400 * We can create the space by defragment. Since only the
5401 * name/value will be moved, the xe shouldn't be changed
5404 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5411 xh_free_start = le16_to_cpu(xh->xh_free_start);
5412 free = xh_free_start - header_size
5413 - OCFS2_XATTR_HEADER_GAP;
5414 if (xh_free_start % blocksize < need)
5415 free -= xh_free_start % blocksize;
5420 mlog(0, "Can't get enough space for xattr insert by "
5421 "defragment. Need %u bytes, but we have %d, so "
5422 "allocate new bucket for it.\n", need, free);
5426 * We have to add new buckets or clusters and one
5427 * allocation should leave us enough space for insert.
5432 * We do not allow for overlapping ranges between buckets. And
5433 * the maximum number of collisions we will allow for then is
5434 * one bucket's worth, so check it here whether we need to
5435 * add a new bucket for the insert.
5437 ret = ocfs2_check_xattr_bucket_collision(inode,
5445 ret = ocfs2_add_new_xattr_bucket(inode,
5455 * ocfs2_add_new_xattr_bucket() will have updated
5456 * xs->bucket if it moved, but it will not have updated
5457 * any of the other search fields. Thus, we drop it and
5458 * re-search. Everything should be cached, so it'll be
5461 ocfs2_xattr_bucket_relse(xs->bucket);
5462 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5465 if (ret && ret != -ENODATA)
5467 xs->not_found = ret;
5473 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
5479 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5480 struct ocfs2_xattr_bucket *bucket,
5483 int ret = 0, ref_credits;
5484 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5486 struct ocfs2_xattr_entry *xe;
5487 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5488 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5489 int credits = ocfs2_remove_extent_credits(osb->sb) +
5490 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5491 struct ocfs2_xattr_value_root *xv;
5492 struct ocfs2_rm_xattr_bucket_para *args =
5493 (struct ocfs2_rm_xattr_bucket_para *)para;
5495 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5497 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5498 xe = &xh->xh_entries[i];
5499 if (ocfs2_xattr_is_local(xe))
5502 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5505 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5511 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5512 if (IS_ERR(ctxt.handle)) {
5513 ret = PTR_ERR(ctxt.handle);
5518 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5521 ocfs2_commit_trans(osb, ctxt.handle);
5523 ocfs2_free_alloc_context(ctxt.meta_ac);
5524 ctxt.meta_ac = NULL;
5533 ocfs2_free_alloc_context(ctxt.meta_ac);
5534 ocfs2_schedule_truncate_log_flush(osb, 1);
5535 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5540 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5541 * or change the extent record flag), we need to recalculate
5542 * the metaecc for the whole bucket. So it is done here.
5545 * We have to give the extra credits for the caller.
5547 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5552 struct ocfs2_xattr_bucket *bucket =
5553 (struct ocfs2_xattr_bucket *)para;
5555 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5556 OCFS2_JOURNAL_ACCESS_WRITE);
5562 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5568 * Special action we need if the xattr value is refcounted.
5570 * 1. If the xattr is refcounted, lock the tree.
5571 * 2. CoW the xattr if we are setting the new value and the value
5572 * will be stored outside.
5573 * 3. In other case, decrease_refcount will work for us, so just
5574 * lock the refcount tree, calculate the meta and credits is OK.
5576 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5577 * currently CoW is a completed transaction, while this function
5578 * will also lock the allocators and let us deadlock. So we will
5579 * CoW the whole xattr value.
5581 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5582 struct ocfs2_dinode *di,
5583 struct ocfs2_xattr_info *xi,
5584 struct ocfs2_xattr_search *xis,
5585 struct ocfs2_xattr_search *xbs,
5586 struct ocfs2_refcount_tree **ref_tree,
5591 struct ocfs2_xattr_block *xb;
5592 struct ocfs2_xattr_entry *xe;
5594 u32 p_cluster, num_clusters;
5595 unsigned int ext_flags;
5596 int name_offset, name_len;
5597 struct ocfs2_xattr_value_buf vb;
5598 struct ocfs2_xattr_bucket *bucket = NULL;
5599 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5600 struct ocfs2_post_refcount refcount;
5601 struct ocfs2_post_refcount *p = NULL;
5602 struct buffer_head *ref_root_bh = NULL;
5604 if (!xis->not_found) {
5606 name_offset = le16_to_cpu(xe->xe_name_offset);
5607 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5609 vb.vb_bh = xis->inode_bh;
5610 vb.vb_access = ocfs2_journal_access_di;
5612 int i, block_off = 0;
5613 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5615 name_offset = le16_to_cpu(xe->xe_name_offset);
5616 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5617 i = xbs->here - xbs->header->xh_entries;
5619 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5620 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5621 bucket_xh(xbs->bucket),
5628 base = bucket_block(xbs->bucket, block_off);
5629 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5630 vb.vb_access = ocfs2_journal_access;
5632 if (ocfs2_meta_ecc(osb)) {
5633 /*create parameters for ocfs2_post_refcount. */
5634 bucket = xbs->bucket;
5635 refcount.credits = bucket->bu_blocks;
5636 refcount.para = bucket;
5638 ocfs2_xattr_bucket_post_refcount;
5643 vb.vb_bh = xbs->xattr_bh;
5644 vb.vb_access = ocfs2_journal_access_xb;
5648 if (ocfs2_xattr_is_local(xe))
5651 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5652 (base + name_offset + name_len);
5654 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5655 &num_clusters, &vb.vb_xv->xr_list,
5663 * We just need to check the 1st extent record, since we always
5664 * CoW the whole xattr. So there shouldn't be a xattr with
5665 * some REFCOUNT extent recs after the 1st one.
5667 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5670 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5671 1, ref_tree, &ref_root_bh);
5678 * If we are deleting the xattr or the new size will be stored inside,
5679 * cool, leave it there, the xattr truncate process will remove them
5680 * for us(it still needs the refcount tree lock and the meta, credits).
5681 * And the worse case is that every cluster truncate will split the
5682 * refcount tree, and make the original extent become 3. So we will need
5683 * 2 * cluster more extent recs at most.
5685 if (!xi->value || xi->value_len <= OCFS2_XATTR_INLINE_SIZE) {
5687 ret = ocfs2_refcounted_xattr_delete_need(inode,
5688 &(*ref_tree)->rf_ci,
5689 ref_root_bh, vb.vb_xv,
5696 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5697 *ref_tree, ref_root_bh, 0,
5698 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5703 brelse(ref_root_bh);
5708 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5709 * The physical clusters will be added to refcount tree.
5711 static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5712 struct ocfs2_xattr_value_root *xv,
5713 struct ocfs2_extent_tree *value_et,
5714 struct ocfs2_caching_info *ref_ci,
5715 struct buffer_head *ref_root_bh,
5716 struct ocfs2_cached_dealloc_ctxt *dealloc,
5717 struct ocfs2_post_refcount *refcount)
5720 u32 clusters = le32_to_cpu(xv->xr_clusters);
5721 u32 cpos, p_cluster, num_clusters;
5722 struct ocfs2_extent_list *el = &xv->xr_list;
5723 unsigned int ext_flags;
5726 while (cpos < clusters) {
5727 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5728 &num_clusters, el, &ext_flags);
5730 cpos += num_clusters;
5731 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5736 ret = ocfs2_add_refcount_flag(inode, value_et,
5737 ref_ci, ref_root_bh,
5738 cpos - num_clusters,
5739 p_cluster, num_clusters,
5751 * Given a normal ocfs2_xattr_header, refcount all the entries which
5752 * have value stored outside.
5753 * Used for xattrs stored in inode and ocfs2_xattr_block.
5755 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5756 struct ocfs2_xattr_value_buf *vb,
5757 struct ocfs2_xattr_header *header,
5758 struct ocfs2_caching_info *ref_ci,
5759 struct buffer_head *ref_root_bh,
5760 struct ocfs2_cached_dealloc_ctxt *dealloc)
5763 struct ocfs2_xattr_entry *xe;
5764 struct ocfs2_xattr_value_root *xv;
5765 struct ocfs2_extent_tree et;
5768 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5769 xe = &header->xh_entries[i];
5771 if (ocfs2_xattr_is_local(xe))
5774 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5775 le16_to_cpu(xe->xe_name_offset) +
5776 OCFS2_XATTR_SIZE(xe->xe_name_len));
5779 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5781 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5782 ref_ci, ref_root_bh,
5793 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5794 struct buffer_head *fe_bh,
5795 struct ocfs2_caching_info *ref_ci,
5796 struct buffer_head *ref_root_bh,
5797 struct ocfs2_cached_dealloc_ctxt *dealloc)
5799 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5800 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5801 (fe_bh->b_data + inode->i_sb->s_blocksize -
5802 le16_to_cpu(di->i_xattr_inline_size));
5803 struct ocfs2_xattr_value_buf vb = {
5805 .vb_access = ocfs2_journal_access_di,
5808 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5809 ref_ci, ref_root_bh, dealloc);
5812 struct ocfs2_xattr_tree_value_refcount_para {
5813 struct ocfs2_caching_info *ref_ci;
5814 struct buffer_head *ref_root_bh;
5815 struct ocfs2_cached_dealloc_ctxt *dealloc;
5818 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5819 struct ocfs2_xattr_bucket *bucket,
5821 struct ocfs2_xattr_value_root **xv,
5822 struct buffer_head **bh)
5824 int ret, block_off, name_offset;
5825 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5826 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5829 ret = ocfs2_xattr_bucket_get_name_value(sb,
5839 base = bucket_block(bucket, block_off);
5841 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
5842 OCFS2_XATTR_SIZE(xe->xe_name_len));
5845 *bh = bucket->bu_bhs[block_off];
5851 * For a given xattr bucket, refcount all the entries which
5852 * have value stored outside.
5854 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
5855 struct ocfs2_xattr_bucket *bucket,
5859 struct ocfs2_extent_tree et;
5860 struct ocfs2_xattr_tree_value_refcount_para *ref =
5861 (struct ocfs2_xattr_tree_value_refcount_para *)para;
5862 struct ocfs2_xattr_header *xh =
5863 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
5864 struct ocfs2_xattr_entry *xe;
5865 struct ocfs2_xattr_value_buf vb = {
5866 .vb_access = ocfs2_journal_access,
5868 struct ocfs2_post_refcount refcount = {
5869 .credits = bucket->bu_blocks,
5871 .func = ocfs2_xattr_bucket_post_refcount,
5873 struct ocfs2_post_refcount *p = NULL;
5875 /* We only need post_refcount if we support metaecc. */
5876 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
5879 mlog(0, "refcount bucket %llu, count = %u\n",
5880 (unsigned long long)bucket_blkno(bucket),
5881 le16_to_cpu(xh->xh_count));
5882 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5883 xe = &xh->xh_entries[i];
5885 if (ocfs2_xattr_is_local(xe))
5888 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
5889 &vb.vb_xv, &vb.vb_bh);
5895 ocfs2_init_xattr_value_extent_tree(&et,
5896 INODE_CACHE(inode), &vb);
5898 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
5912 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
5913 struct buffer_head *root_bh,
5914 u64 blkno, u32 cpos, u32 len, void *para)
5916 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
5917 ocfs2_xattr_bucket_value_refcount,
5921 static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
5922 struct buffer_head *blk_bh,
5923 struct ocfs2_caching_info *ref_ci,
5924 struct buffer_head *ref_root_bh,
5925 struct ocfs2_cached_dealloc_ctxt *dealloc)
5928 struct ocfs2_xattr_block *xb =
5929 (struct ocfs2_xattr_block *)blk_bh->b_data;
5931 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
5932 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
5933 struct ocfs2_xattr_value_buf vb = {
5935 .vb_access = ocfs2_journal_access_xb,
5938 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5939 ref_ci, ref_root_bh,
5942 struct ocfs2_xattr_tree_value_refcount_para para = {
5944 .ref_root_bh = ref_root_bh,
5948 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
5949 ocfs2_refcount_xattr_tree_rec,
5956 int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
5957 struct buffer_head *fe_bh,
5958 struct ocfs2_caching_info *ref_ci,
5959 struct buffer_head *ref_root_bh,
5960 struct ocfs2_cached_dealloc_ctxt *dealloc)
5963 struct ocfs2_inode_info *oi = OCFS2_I(inode);
5964 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5965 struct buffer_head *blk_bh = NULL;
5967 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
5968 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
5969 ref_ci, ref_root_bh,
5977 if (!di->i_xattr_loc)
5980 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
5987 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
5988 ref_root_bh, dealloc);
5998 typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
6000 * Store the information we need in xattr reflink.
6001 * old_bh and new_bh are inode bh for the old and new inode.
6003 struct ocfs2_xattr_reflink {
6004 struct inode *old_inode;
6005 struct inode *new_inode;
6006 struct buffer_head *old_bh;
6007 struct buffer_head *new_bh;
6008 struct ocfs2_caching_info *ref_ci;
6009 struct buffer_head *ref_root_bh;
6010 struct ocfs2_cached_dealloc_ctxt *dealloc;
6011 should_xattr_reflinked *xattr_reflinked;
6015 * Given a xattr header and xe offset,
6016 * return the proper xv and the corresponding bh.
6017 * xattr in inode, block and xattr tree have different implementaions.
6019 typedef int (get_xattr_value_root)(struct super_block *sb,
6020 struct buffer_head *bh,
6021 struct ocfs2_xattr_header *xh,
6023 struct ocfs2_xattr_value_root **xv,
6024 struct buffer_head **ret_bh,
6028 * Calculate all the xattr value root metadata stored in this xattr header and
6029 * credits we need if we create them from the scratch.
6030 * We use get_xattr_value_root so that all types of xattr container can use it.
6032 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6033 struct buffer_head *bh,
6034 struct ocfs2_xattr_header *xh,
6035 int *metas, int *credits,
6037 get_xattr_value_root *func,
6041 struct ocfs2_xattr_value_root *xv;
6042 struct ocfs2_xattr_entry *xe;
6044 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6045 xe = &xh->xh_entries[i];
6046 if (ocfs2_xattr_is_local(xe))
6049 ret = func(sb, bh, xh, i, &xv, NULL, para);
6055 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6056 le16_to_cpu(xv->xr_list.l_next_free_rec);
6058 *credits += ocfs2_calc_extend_credits(sb,
6060 le32_to_cpu(xv->xr_clusters));
6063 * If the value is a tree with depth > 1, We don't go deep
6064 * to the extent block, so just calculate a maximum record num.
6066 if (!xv->xr_list.l_tree_depth)
6067 *num_recs += xv->xr_list.l_next_free_rec;
6069 *num_recs += ocfs2_clusters_for_bytes(sb,
6076 /* Used by xattr inode and block to return the right xv and buffer_head. */
6077 static int ocfs2_get_xattr_value_root(struct super_block *sb,
6078 struct buffer_head *bh,
6079 struct ocfs2_xattr_header *xh,
6081 struct ocfs2_xattr_value_root **xv,
6082 struct buffer_head **ret_bh,
6085 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6087 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6088 le16_to_cpu(xe->xe_name_offset) +
6089 OCFS2_XATTR_SIZE(xe->xe_name_len));
6098 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6099 * It is only used for inline xattr and xattr block.
6101 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6102 struct ocfs2_xattr_header *xh,
6103 struct buffer_head *ref_root_bh,
6105 struct ocfs2_alloc_context **meta_ac)
6107 int ret, meta_add = 0, num_recs = 0;
6108 struct ocfs2_refcount_block *rb =
6109 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6113 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6114 &meta_add, credits, &num_recs,
6115 ocfs2_get_xattr_value_root,
6123 * We need to add/modify num_recs in refcount tree, so just calculate
6124 * an approximate number we need for refcount tree change.
6125 * Sometimes we need to split the tree, and after split, half recs
6126 * will be moved to the new block, and a new block can only provide
6127 * half number of recs. So we multiple new blocks by 2.
6129 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6130 meta_add += num_recs;
6131 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6132 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6133 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6134 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6138 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6147 * Given a xattr header, reflink all the xattrs in this container.
6148 * It can be used for inode, block and bucket.
6151 * Before we call this function, the caller has memcpy the xattr in
6152 * old_xh to the new_xh.
6154 * If args.xattr_reflinked is set, call it to decide whether the xe should
6155 * be reflinked or not. If not, remove it from the new xattr header.
6157 static int ocfs2_reflink_xattr_header(handle_t *handle,
6158 struct ocfs2_xattr_reflink *args,
6159 struct buffer_head *old_bh,
6160 struct ocfs2_xattr_header *xh,
6161 struct buffer_head *new_bh,
6162 struct ocfs2_xattr_header *new_xh,
6163 struct ocfs2_xattr_value_buf *vb,
6164 struct ocfs2_alloc_context *meta_ac,
6165 get_xattr_value_root *func,
6169 struct super_block *sb = args->old_inode->i_sb;
6170 struct buffer_head *value_bh;
6171 struct ocfs2_xattr_entry *xe, *last;
6172 struct ocfs2_xattr_value_root *xv, *new_xv;
6173 struct ocfs2_extent_tree data_et;
6174 u32 clusters, cpos, p_cluster, num_clusters;
6175 unsigned int ext_flags = 0;
6177 mlog(0, "reflink xattr in container %llu, count = %u\n",
6178 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
6180 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6181 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
6182 xe = &xh->xh_entries[i];
6184 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6185 xe = &new_xh->xh_entries[j];
6187 le16_add_cpu(&new_xh->xh_count, -1);
6188 if (new_xh->xh_count) {
6190 (void *)last - (void *)xe);
6192 sizeof(struct ocfs2_xattr_entry));
6196 * We don't want j to increase in the next round since
6197 * it is already moved ahead.
6203 if (ocfs2_xattr_is_local(xe))
6206 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6212 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6219 * For the xattr which has l_tree_depth = 0, all the extent
6220 * recs have already be copied to the new xh with the
6221 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6222 * increase the refount count int the refcount tree.
6224 * For the xattr which has l_tree_depth > 0, we need
6225 * to initialize it to the empty default value root,
6226 * and then insert the extents one by one.
6228 if (xv->xr_list.l_tree_depth) {
6229 memcpy(new_xv, &def_xv, sizeof(def_xv));
6231 vb->vb_bh = value_bh;
6232 ocfs2_init_xattr_value_extent_tree(&data_et,
6233 INODE_CACHE(args->new_inode), vb);
6236 clusters = le32_to_cpu(xv->xr_clusters);
6238 while (cpos < clusters) {
6239 ret = ocfs2_xattr_get_clusters(args->old_inode,
6252 if (xv->xr_list.l_tree_depth) {
6253 ret = ocfs2_insert_extent(handle,
6255 ocfs2_clusters_to_blocks(
6256 args->old_inode->i_sb,
6258 num_clusters, ext_flags,
6266 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6268 p_cluster, num_clusters,
6269 meta_ac, args->dealloc);
6275 cpos += num_clusters;
6283 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6285 int ret = 0, credits = 0;
6287 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6288 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6289 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6290 int header_off = osb->sb->s_blocksize - inline_size;
6291 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6292 (args->old_bh->b_data + header_off);
6293 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6294 (args->new_bh->b_data + header_off);
6295 struct ocfs2_alloc_context *meta_ac = NULL;
6296 struct ocfs2_inode_info *new_oi;
6297 struct ocfs2_dinode *new_di;
6298 struct ocfs2_xattr_value_buf vb = {
6299 .vb_bh = args->new_bh,
6300 .vb_access = ocfs2_journal_access_di,
6303 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6304 &credits, &meta_ac);
6310 handle = ocfs2_start_trans(osb, credits);
6311 if (IS_ERR(handle)) {
6312 ret = PTR_ERR(handle);
6317 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6318 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6324 memcpy(args->new_bh->b_data + header_off,
6325 args->old_bh->b_data + header_off, inline_size);
6327 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6328 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6330 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6331 args->new_bh, new_xh, &vb, meta_ac,
6332 ocfs2_get_xattr_value_root, NULL);
6338 new_oi = OCFS2_I(args->new_inode);
6339 spin_lock(&new_oi->ip_lock);
6340 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6341 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6342 spin_unlock(&new_oi->ip_lock);
6344 ocfs2_journal_dirty(handle, args->new_bh);
6347 ocfs2_commit_trans(osb, handle);
6351 ocfs2_free_alloc_context(meta_ac);
6355 static int ocfs2_create_empty_xattr_block(struct inode *inode,
6356 struct buffer_head *fe_bh,
6357 struct buffer_head **ret_bh,
6362 struct ocfs2_alloc_context *meta_ac;
6363 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6365 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
6371 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6372 if (IS_ERR(handle)) {
6373 ret = PTR_ERR(handle);
6378 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6379 (unsigned long long)fe_bh->b_blocknr, indexed);
6380 ret = ocfs2_create_xattr_block(handle, inode, fe_bh,
6381 meta_ac, ret_bh, indexed);
6385 ocfs2_commit_trans(osb, handle);
6387 ocfs2_free_alloc_context(meta_ac);
6391 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6392 struct buffer_head *blk_bh,
6393 struct buffer_head *new_blk_bh)
6395 int ret = 0, credits = 0;
6397 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6398 struct ocfs2_dinode *new_di;
6399 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6400 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6401 struct ocfs2_xattr_block *xb =
6402 (struct ocfs2_xattr_block *)blk_bh->b_data;
6403 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6404 struct ocfs2_xattr_block *new_xb =
6405 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6406 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6407 struct ocfs2_alloc_context *meta_ac;
6408 struct ocfs2_xattr_value_buf vb = {
6409 .vb_bh = new_blk_bh,
6410 .vb_access = ocfs2_journal_access_xb,
6413 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6414 &credits, &meta_ac);
6420 /* One more credits in case we need to add xattr flags in new inode. */
6421 handle = ocfs2_start_trans(osb, credits + 1);
6422 if (IS_ERR(handle)) {
6423 ret = PTR_ERR(handle);
6428 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6429 ret = ocfs2_journal_access_di(handle,
6430 INODE_CACHE(args->new_inode),
6432 OCFS2_JOURNAL_ACCESS_WRITE);
6439 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6440 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6446 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6447 osb->sb->s_blocksize - header_off);
6449 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6450 new_blk_bh, new_xh, &vb, meta_ac,
6451 ocfs2_get_xattr_value_root, NULL);
6457 ocfs2_journal_dirty(handle, new_blk_bh);
6459 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6460 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6461 spin_lock(&new_oi->ip_lock);
6462 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6463 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6464 spin_unlock(&new_oi->ip_lock);
6466 ocfs2_journal_dirty(handle, args->new_bh);
6470 ocfs2_commit_trans(osb, handle);
6473 ocfs2_free_alloc_context(meta_ac);
6477 struct ocfs2_reflink_xattr_tree_args {
6478 struct ocfs2_xattr_reflink *reflink;
6479 struct buffer_head *old_blk_bh;
6480 struct buffer_head *new_blk_bh;
6481 struct ocfs2_xattr_bucket *old_bucket;
6482 struct ocfs2_xattr_bucket *new_bucket;
6487 * We have to handle the case that both old bucket and new bucket
6488 * will call this function to get the right ret_bh.
6489 * So The caller must give us the right bh.
6491 static int ocfs2_get_reflink_xattr_value_root(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_reflink_xattr_tree_args *args =
6500 (struct ocfs2_reflink_xattr_tree_args *)para;
6501 struct ocfs2_xattr_bucket *bucket;
6503 if (bh == args->old_bucket->bu_bhs[0])
6504 bucket = args->old_bucket;
6506 bucket = args->new_bucket;
6508 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6512 struct ocfs2_value_tree_metas {
6518 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6519 struct buffer_head *bh,
6520 struct ocfs2_xattr_header *xh,
6522 struct ocfs2_xattr_value_root **xv,
6523 struct buffer_head **ret_bh,
6526 struct ocfs2_xattr_bucket *bucket =
6527 (struct ocfs2_xattr_bucket *)para;
6529 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6533 static int ocfs2_calc_value_tree_metas(struct inode *inode,
6534 struct ocfs2_xattr_bucket *bucket,
6537 struct ocfs2_value_tree_metas *metas =
6538 (struct ocfs2_value_tree_metas *)para;
6539 struct ocfs2_xattr_header *xh =
6540 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6542 /* Add the credits for this bucket first. */
6543 metas->credits += bucket->bu_blocks;
6544 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6545 xh, &metas->num_metas,
6546 &metas->credits, &metas->num_recs,
6547 ocfs2_value_tree_metas_in_bucket,
6552 * Given a xattr extent rec starting from blkno and having len clusters,
6553 * iterate all the buckets calculate how much metadata we need for reflinking
6554 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6556 static int ocfs2_lock_reflink_xattr_rec_allocators(
6557 struct ocfs2_reflink_xattr_tree_args *args,
6558 struct ocfs2_extent_tree *xt_et,
6559 u64 blkno, u32 len, int *credits,
6560 struct ocfs2_alloc_context **meta_ac,
6561 struct ocfs2_alloc_context **data_ac)
6563 int ret, num_free_extents;
6564 struct ocfs2_value_tree_metas metas;
6565 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6566 struct ocfs2_refcount_block *rb;
6568 memset(&metas, 0, sizeof(metas));
6570 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6571 ocfs2_calc_value_tree_metas, &metas);
6577 *credits = metas.credits;
6580 * Calculate we need for refcount tree change.
6582 * We need to add/modify num_recs in refcount tree, so just calculate
6583 * an approximate number we need for refcount tree change.
6584 * Sometimes we need to split the tree, and after split, half recs
6585 * will be moved to the new block, and a new block can only provide
6586 * half number of recs. So we multiple new blocks by 2.
6587 * In the end, we have to add credits for modifying the already
6588 * existed refcount block.
6590 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6592 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6593 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6594 metas.num_metas += metas.num_recs;
6595 *credits += metas.num_recs +
6596 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6597 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6598 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6599 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6603 /* count in the xattr tree change. */
6604 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6605 if (num_free_extents < 0) {
6606 ret = num_free_extents;
6611 if (num_free_extents < len)
6612 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6614 *credits += ocfs2_calc_extend_credits(osb->sb,
6615 xt_et->et_root_el, len);
6617 if (metas.num_metas) {
6618 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6627 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6634 ocfs2_free_alloc_context(*meta_ac);
6642 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6643 u64 blkno, u64 new_blkno, u32 clusters,
6644 struct ocfs2_alloc_context *meta_ac,
6645 struct ocfs2_alloc_context *data_ac,
6646 struct ocfs2_reflink_xattr_tree_args *args)
6649 struct super_block *sb = args->reflink->old_inode->i_sb;
6650 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6651 u32 num_buckets = clusters * bpc;
6652 int bpb = args->old_bucket->bu_blocks;
6653 struct ocfs2_xattr_value_buf vb = {
6654 .vb_access = ocfs2_journal_access,
6657 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6658 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6664 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6671 * The real bucket num in this series of blocks is stored
6672 * in the 1st bucket.
6675 num_buckets = le16_to_cpu(
6676 bucket_xh(args->old_bucket)->xh_num_buckets);
6678 ret = ocfs2_xattr_bucket_journal_access(handle,
6680 OCFS2_JOURNAL_ACCESS_CREATE);
6686 for (j = 0; j < bpb; j++)
6687 memcpy(bucket_block(args->new_bucket, j),
6688 bucket_block(args->old_bucket, j),
6691 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6693 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6694 args->old_bucket->bu_bhs[0],
6695 bucket_xh(args->old_bucket),
6696 args->new_bucket->bu_bhs[0],
6697 bucket_xh(args->new_bucket),
6699 ocfs2_get_reflink_xattr_value_root,
6707 * Re-access and dirty the bucket to calculate metaecc.
6708 * Because we may extend the transaction in reflink_xattr_header
6709 * which will let the already accessed block gone.
6711 ret = ocfs2_xattr_bucket_journal_access(handle,
6713 OCFS2_JOURNAL_ACCESS_WRITE);
6719 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6720 ocfs2_xattr_bucket_relse(args->old_bucket);
6721 ocfs2_xattr_bucket_relse(args->new_bucket);
6724 ocfs2_xattr_bucket_relse(args->old_bucket);
6725 ocfs2_xattr_bucket_relse(args->new_bucket);
6729 * Create the same xattr extent record in the new inode's xattr tree.
6731 static int ocfs2_reflink_xattr_rec(struct inode *inode,
6732 struct buffer_head *root_bh,
6738 int ret, credits = 0;
6739 u32 p_cluster, num_clusters;
6742 struct ocfs2_reflink_xattr_tree_args *args =
6743 (struct ocfs2_reflink_xattr_tree_args *)para;
6744 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6745 struct ocfs2_alloc_context *meta_ac = NULL;
6746 struct ocfs2_alloc_context *data_ac = NULL;
6747 struct ocfs2_extent_tree et;
6749 ocfs2_init_xattr_tree_extent_tree(&et,
6750 INODE_CACHE(args->reflink->new_inode),
6753 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6755 &meta_ac, &data_ac);
6761 handle = ocfs2_start_trans(osb, credits);
6762 if (IS_ERR(handle)) {
6763 ret = PTR_ERR(handle);
6768 ret = ocfs2_claim_clusters(osb, handle, data_ac,
6769 len, &p_cluster, &num_clusters);
6775 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6777 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6778 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6779 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6780 meta_ac, data_ac, args);
6786 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6787 (unsigned long long)new_blkno, len, cpos);
6788 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6794 ocfs2_commit_trans(osb, handle);
6798 ocfs2_free_alloc_context(meta_ac);
6800 ocfs2_free_alloc_context(data_ac);
6805 * Create reflinked xattr buckets.
6806 * We will add bucket one by one, and refcount all the xattrs in the bucket
6807 * if they are stored outside.
6809 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6810 struct buffer_head *blk_bh,
6811 struct buffer_head *new_blk_bh)
6814 struct ocfs2_reflink_xattr_tree_args para;
6816 memset(¶, 0, sizeof(para));
6817 para.reflink = args;
6818 para.old_blk_bh = blk_bh;
6819 para.new_blk_bh = new_blk_bh;
6821 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
6822 if (!para.old_bucket) {
6823 mlog_errno(-ENOMEM);
6827 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
6828 if (!para.new_bucket) {
6834 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
6835 ocfs2_reflink_xattr_rec,
6841 ocfs2_xattr_bucket_free(para.old_bucket);
6842 ocfs2_xattr_bucket_free(para.new_bucket);
6846 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
6847 struct buffer_head *blk_bh)
6849 int ret, indexed = 0;
6850 struct buffer_head *new_blk_bh = NULL;
6851 struct ocfs2_xattr_block *xb =
6852 (struct ocfs2_xattr_block *)blk_bh->b_data;
6855 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
6858 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
6859 &new_blk_bh, indexed);
6865 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
6866 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
6868 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
6877 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
6879 int type = ocfs2_xattr_get_type(xe);
6881 return type != OCFS2_XATTR_INDEX_SECURITY &&
6882 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
6883 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
6886 int ocfs2_reflink_xattrs(struct inode *old_inode,
6887 struct buffer_head *old_bh,
6888 struct inode *new_inode,
6889 struct buffer_head *new_bh,
6890 bool preserve_security)
6893 struct ocfs2_xattr_reflink args;
6894 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
6895 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
6896 struct buffer_head *blk_bh = NULL;
6897 struct ocfs2_cached_dealloc_ctxt dealloc;
6898 struct ocfs2_refcount_tree *ref_tree;
6899 struct buffer_head *ref_root_bh = NULL;
6901 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
6902 le64_to_cpu(di->i_refcount_loc),
6903 1, &ref_tree, &ref_root_bh);
6909 ocfs2_init_dealloc_ctxt(&dealloc);
6911 args.old_inode = old_inode;
6912 args.new_inode = new_inode;
6913 args.old_bh = old_bh;
6914 args.new_bh = new_bh;
6915 args.ref_ci = &ref_tree->rf_ci;
6916 args.ref_root_bh = ref_root_bh;
6917 args.dealloc = &dealloc;
6918 if (preserve_security)
6919 args.xattr_reflinked = NULL;
6921 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
6923 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6924 ret = ocfs2_reflink_xattr_inline(&args);
6931 if (!di->i_xattr_loc)
6934 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
6941 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
6948 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
6950 brelse(ref_root_bh);
6952 if (ocfs2_dealloc_has_cluster(&dealloc)) {
6953 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
6954 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
6962 * Initialize security and acl for a already created inode.
6963 * Used for reflink a non-preserve-security file.
6965 * It uses common api like ocfs2_xattr_set, so the caller
6966 * must not hold any lock expect i_mutex.
6968 int ocfs2_init_security_and_acl(struct inode *dir,
6969 struct inode *inode)
6972 struct buffer_head *dir_bh = NULL;
6973 struct ocfs2_security_xattr_info si = {
6977 ret = ocfs2_init_security_get(inode, dir, &si);
6979 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
6980 si.name, si.value, si.value_len,
6986 } else if (ret != -EOPNOTSUPP) {
6991 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
6997 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7001 ocfs2_inode_unlock(dir, 0);
7007 * 'security' attributes support
7009 static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
7010 size_t list_size, const char *name,
7011 size_t name_len, int type)
7013 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7014 const size_t total_len = prefix_len + name_len + 1;
7016 if (list && total_len <= list_size) {
7017 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7018 memcpy(list + prefix_len, name, name_len);
7019 list[prefix_len + name_len] = '\0';
7024 static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7025 void *buffer, size_t size, int type)
7027 if (strcmp(name, "") == 0)
7029 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7030 name, buffer, size);
7033 static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7034 const void *value, size_t size, int flags, int type)
7036 if (strcmp(name, "") == 0)
7039 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7040 name, value, size, flags);
7043 int ocfs2_init_security_get(struct inode *inode,
7045 struct ocfs2_security_xattr_info *si)
7047 /* check whether ocfs2 support feature xattr */
7048 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7050 return security_inode_init_security(inode, dir, &si->name, &si->value,
7054 int ocfs2_init_security_set(handle_t *handle,
7055 struct inode *inode,
7056 struct buffer_head *di_bh,
7057 struct ocfs2_security_xattr_info *si,
7058 struct ocfs2_alloc_context *xattr_ac,
7059 struct ocfs2_alloc_context *data_ac)
7061 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7062 OCFS2_XATTR_INDEX_SECURITY,
7063 si->name, si->value, si->value_len, 0,
7067 struct xattr_handler ocfs2_xattr_security_handler = {
7068 .prefix = XATTR_SECURITY_PREFIX,
7069 .list = ocfs2_xattr_security_list,
7070 .get = ocfs2_xattr_security_get,
7071 .set = ocfs2_xattr_security_set,
7075 * 'trusted' attributes support
7077 static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
7078 size_t list_size, const char *name,
7079 size_t name_len, int type)
7081 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
7082 const size_t total_len = prefix_len + name_len + 1;
7084 if (list && total_len <= list_size) {
7085 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7086 memcpy(list + prefix_len, name, name_len);
7087 list[prefix_len + name_len] = '\0';
7092 static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7093 void *buffer, size_t size, int type)
7095 if (strcmp(name, "") == 0)
7097 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7098 name, buffer, size);
7101 static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7102 const void *value, size_t size, int flags, int type)
7104 if (strcmp(name, "") == 0)
7107 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7108 name, value, size, flags);
7111 struct xattr_handler ocfs2_xattr_trusted_handler = {
7112 .prefix = XATTR_TRUSTED_PREFIX,
7113 .list = ocfs2_xattr_trusted_list,
7114 .get = ocfs2_xattr_trusted_get,
7115 .set = ocfs2_xattr_trusted_set,
7119 * 'user' attributes support
7121 static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
7122 size_t list_size, const char *name,
7123 size_t name_len, int type)
7125 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7126 const size_t total_len = prefix_len + name_len + 1;
7127 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7129 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7132 if (list && total_len <= list_size) {
7133 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7134 memcpy(list + prefix_len, name, name_len);
7135 list[prefix_len + name_len] = '\0';
7140 static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7141 void *buffer, size_t size, int type)
7143 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7145 if (strcmp(name, "") == 0)
7147 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7149 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
7153 static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7154 const void *value, size_t size, int flags, int type)
7156 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7158 if (strcmp(name, "") == 0)
7160 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7163 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7164 name, value, size, flags);
7167 struct xattr_handler ocfs2_xattr_user_handler = {
7168 .prefix = XATTR_USER_PREFIX,
7169 .list = ocfs2_xattr_user_list,
7170 .get = ocfs2_xattr_user_get,
7171 .set = ocfs2_xattr_user_set,