1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
9 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/security.h>
40 #define MLOG_MASK_PREFIX ML_XATTR
41 #include <cluster/masklog.h>
45 #include "blockcheck.h"
55 #include "buffer_head_io.h"
58 #include "refcounttree.h"
61 struct ocfs2_xattr_def_value_root {
62 struct ocfs2_xattr_value_root xv;
63 struct ocfs2_extent_rec er;
66 struct ocfs2_xattr_bucket {
67 /* The inode these xattrs are associated with */
68 struct inode *bu_inode;
70 /* The actual buffers that make up the bucket */
71 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
73 /* How many blocks make up one bucket for this filesystem */
77 struct ocfs2_xattr_set_ctxt {
79 struct ocfs2_alloc_context *meta_ac;
80 struct ocfs2_alloc_context *data_ac;
81 struct ocfs2_cached_dealloc_ctxt dealloc;
84 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
85 #define OCFS2_XATTR_INLINE_SIZE 80
86 #define OCFS2_XATTR_HEADER_GAP 4
87 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
88 - sizeof(struct ocfs2_xattr_header) \
89 - OCFS2_XATTR_HEADER_GAP)
90 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
91 - sizeof(struct ocfs2_xattr_block) \
92 - sizeof(struct ocfs2_xattr_header) \
93 - OCFS2_XATTR_HEADER_GAP)
95 static struct ocfs2_xattr_def_value_root def_xv = {
96 .xv.xr_list.l_count = cpu_to_le16(1),
99 struct xattr_handler *ocfs2_xattr_handlers[] = {
100 &ocfs2_xattr_user_handler,
101 &ocfs2_xattr_acl_access_handler,
102 &ocfs2_xattr_acl_default_handler,
103 &ocfs2_xattr_trusted_handler,
104 &ocfs2_xattr_security_handler,
108 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
109 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
110 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
111 = &ocfs2_xattr_acl_access_handler,
112 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
113 = &ocfs2_xattr_acl_default_handler,
114 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
115 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
118 struct ocfs2_xattr_info {
122 const void *xi_value;
126 struct ocfs2_xattr_search {
127 struct buffer_head *inode_bh;
129 * xattr_bh point to the block buffer head which has extended attribute
130 * when extended attribute in inode, xattr_bh is equal to inode_bh.
132 struct buffer_head *xattr_bh;
133 struct ocfs2_xattr_header *header;
134 struct ocfs2_xattr_bucket *bucket;
137 struct ocfs2_xattr_entry *here;
141 /* Operations on struct ocfs2_xa_entry */
143 struct ocfs2_xa_loc_operations {
145 * Return a pointer to the appropriate buffer in loc->xl_storage
146 * at the given offset from loc->xl_header.
148 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
150 /* Can we reuse the existing entry for the new value? */
151 int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
152 struct ocfs2_xattr_info *xi);
154 /* How much space is needed for the new value? */
155 int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
156 struct ocfs2_xattr_info *xi);
159 * Return the offset of the first name+value pair. This is
160 * the start of our downward-filling free space.
162 int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
165 * Remove the name+value at this location. Do whatever is
166 * appropriate with the remaining name+value pairs.
168 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
170 /* Fill xl_entry with a new entry */
171 void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
173 /* Add name+value storage to an entry */
174 void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
178 * Describes an xattr entry location. This is a memory structure
179 * tracking the on-disk structure.
181 struct ocfs2_xa_loc {
182 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
183 struct ocfs2_xattr_header *xl_header;
185 /* Bytes from xl_header to the end of the storage */
189 * The ocfs2_xattr_entry this location describes. If this is
190 * NULL, this location describes the on-disk structure where it
193 struct ocfs2_xattr_entry *xl_entry;
196 * Internal housekeeping
199 /* Buffer(s) containing this entry */
202 /* Operations on the storage backing this location */
203 const struct ocfs2_xa_loc_operations *xl_ops;
207 * Convenience functions to calculate how much space is needed for a
208 * given name+value pair
210 static int namevalue_size(int name_len, uint64_t value_len)
212 if (value_len > OCFS2_XATTR_INLINE_SIZE)
213 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
215 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
218 static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
220 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
223 static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
225 u64 value_len = le64_to_cpu(xe->xe_value_size);
227 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
228 ocfs2_xattr_is_local(xe));
229 return namevalue_size(xe->xe_name_len, value_len);
233 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
234 struct ocfs2_xattr_header *xh,
239 static int ocfs2_xattr_block_find(struct inode *inode,
242 struct ocfs2_xattr_search *xs);
243 static int ocfs2_xattr_index_block_find(struct inode *inode,
244 struct buffer_head *root_bh,
247 struct ocfs2_xattr_search *xs);
249 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
250 struct buffer_head *blk_bh,
254 static int ocfs2_xattr_create_index_block(struct inode *inode,
255 struct ocfs2_xattr_search *xs,
256 struct ocfs2_xattr_set_ctxt *ctxt);
258 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
259 struct ocfs2_xattr_info *xi,
260 struct ocfs2_xattr_search *xs,
261 struct ocfs2_xattr_set_ctxt *ctxt);
263 typedef int (xattr_tree_rec_func)(struct inode *inode,
264 struct buffer_head *root_bh,
265 u64 blkno, u32 cpos, u32 len, void *para);
266 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
267 struct buffer_head *root_bh,
268 xattr_tree_rec_func *rec_func,
270 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
271 struct ocfs2_xattr_bucket *bucket,
273 static int ocfs2_rm_xattr_cluster(struct inode *inode,
274 struct buffer_head *root_bh,
280 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
281 u64 src_blk, u64 last_blk, u64 to_blk,
282 unsigned int start_bucket,
284 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
285 struct ocfs2_dinode *di,
286 struct ocfs2_xattr_info *xi,
287 struct ocfs2_xattr_search *xis,
288 struct ocfs2_xattr_search *xbs,
289 struct ocfs2_refcount_tree **ref_tree,
292 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
293 struct ocfs2_xattr_bucket *bucket,
295 struct ocfs2_xattr_value_root **xv,
296 struct buffer_head **bh);
298 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
300 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
303 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
305 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
308 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
310 u16 len = sb->s_blocksize -
311 offsetof(struct ocfs2_xattr_header, xh_entries);
313 return len / sizeof(struct ocfs2_xattr_entry);
316 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
317 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
318 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
320 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
322 struct ocfs2_xattr_bucket *bucket;
323 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
325 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
327 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
329 bucket->bu_inode = inode;
330 bucket->bu_blocks = blks;
336 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
340 for (i = 0; i < bucket->bu_blocks; i++) {
341 brelse(bucket->bu_bhs[i]);
342 bucket->bu_bhs[i] = NULL;
346 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
349 ocfs2_xattr_bucket_relse(bucket);
350 bucket->bu_inode = NULL;
356 * A bucket that has never been written to disk doesn't need to be
357 * read. We just need the buffer_heads. Don't call this for
358 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
361 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
366 for (i = 0; i < bucket->bu_blocks; i++) {
367 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
369 if (!bucket->bu_bhs[i]) {
375 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
377 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
382 ocfs2_xattr_bucket_relse(bucket);
386 /* Read the xattr bucket at xb_blkno */
387 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
392 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
393 bucket->bu_blocks, bucket->bu_bhs, 0,
396 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
397 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
400 &bucket_xh(bucket)->xh_check);
401 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
407 ocfs2_xattr_bucket_relse(bucket);
411 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
412 struct ocfs2_xattr_bucket *bucket,
417 for (i = 0; i < bucket->bu_blocks; i++) {
418 rc = ocfs2_journal_access(handle,
419 INODE_CACHE(bucket->bu_inode),
420 bucket->bu_bhs[i], type);
430 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
431 struct ocfs2_xattr_bucket *bucket)
435 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
436 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
437 bucket->bu_bhs, bucket->bu_blocks,
438 &bucket_xh(bucket)->xh_check);
439 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
441 for (i = 0; i < bucket->bu_blocks; i++)
442 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
445 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
446 struct ocfs2_xattr_bucket *src)
449 int blocksize = src->bu_inode->i_sb->s_blocksize;
451 BUG_ON(dest->bu_blocks != src->bu_blocks);
452 BUG_ON(dest->bu_inode != src->bu_inode);
454 for (i = 0; i < src->bu_blocks; i++) {
455 memcpy(bucket_block(dest, i), bucket_block(src, i),
460 static int ocfs2_validate_xattr_block(struct super_block *sb,
461 struct buffer_head *bh)
464 struct ocfs2_xattr_block *xb =
465 (struct ocfs2_xattr_block *)bh->b_data;
467 mlog(0, "Validating xattr block %llu\n",
468 (unsigned long long)bh->b_blocknr);
470 BUG_ON(!buffer_uptodate(bh));
473 * If the ecc fails, we return the error but otherwise
474 * leave the filesystem running. We know any error is
475 * local to this block.
477 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
482 * Errors after here are fatal
485 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
487 "Extended attribute block #%llu has bad "
489 (unsigned long long)bh->b_blocknr, 7,
494 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
496 "Extended attribute block #%llu has an "
497 "invalid xb_blkno of %llu",
498 (unsigned long long)bh->b_blocknr,
499 (unsigned long long)le64_to_cpu(xb->xb_blkno));
503 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
505 "Extended attribute block #%llu has an invalid "
506 "xb_fs_generation of #%u",
507 (unsigned long long)bh->b_blocknr,
508 le32_to_cpu(xb->xb_fs_generation));
515 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
516 struct buffer_head **bh)
519 struct buffer_head *tmp = *bh;
521 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
522 ocfs2_validate_xattr_block);
524 /* If ocfs2_read_block() got us a new bh, pass it up. */
531 static inline const char *ocfs2_xattr_prefix(int name_index)
533 struct xattr_handler *handler = NULL;
535 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
536 handler = ocfs2_xattr_handler_map[name_index];
538 return handler ? handler->prefix : NULL;
541 static u32 ocfs2_xattr_name_hash(struct inode *inode,
545 /* Get hash value of uuid from super block */
546 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
549 /* hash extended attribute name */
550 for (i = 0; i < name_len; i++) {
551 hash = (hash << OCFS2_HASH_SHIFT) ^
552 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
560 * ocfs2_xattr_hash_entry()
562 * Compute the hash of an extended attribute.
564 static void ocfs2_xattr_hash_entry(struct inode *inode,
565 struct ocfs2_xattr_header *header,
566 struct ocfs2_xattr_entry *entry)
569 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
571 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
572 entry->xe_name_hash = cpu_to_le32(hash);
577 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
579 return namevalue_size(name_len, value_len) +
580 sizeof(struct ocfs2_xattr_entry);
583 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
585 return namevalue_size_xi(xi) +
586 sizeof(struct ocfs2_xattr_entry);
589 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
591 return namevalue_size_xe(xe) +
592 sizeof(struct ocfs2_xattr_entry);
595 int ocfs2_calc_security_init(struct inode *dir,
596 struct ocfs2_security_xattr_info *si,
599 struct ocfs2_alloc_context **xattr_ac)
602 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
603 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
607 * The max space of security xattr taken inline is
608 * 256(name) + 80(value) + 16(entry) = 352 bytes,
609 * So reserve one metadata block for it is ok.
611 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
612 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
613 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
618 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
621 /* reserve clusters for xattr value which will be set in B tree*/
622 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
623 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
626 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
628 *want_clusters += new_clusters;
633 int ocfs2_calc_xattr_init(struct inode *dir,
634 struct buffer_head *dir_bh,
636 struct ocfs2_security_xattr_info *si,
642 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
643 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
646 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
649 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
650 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
651 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
654 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
657 } else if (acl_len != 0 && acl_len != -ENODATA) {
663 if (!(s_size + a_size))
667 * The max space of security xattr taken inline is
668 * 256(name) + 80(value) + 16(entry) = 352 bytes,
669 * The max space of acl xattr taken inline is
670 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
671 * when blocksize = 512, may reserve one more cluser for
672 * xattr bucket, otherwise reserve one metadata block
674 * If this is a new directory with inline data,
675 * we choose to reserve the entire inline area for
676 * directory contents and force an external xattr block.
678 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
679 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
680 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
681 *want_meta = *want_meta + 1;
682 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
685 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
686 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
688 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
692 * reserve credits and clusters for xattrs which has large value
693 * and have to be set outside
695 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
696 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
698 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
700 *want_clusters += new_clusters;
702 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
703 acl_len > OCFS2_XATTR_INLINE_SIZE) {
704 /* for directory, it has DEFAULT and ACCESS two types of acls */
705 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
706 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
707 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
709 *want_clusters += new_clusters;
715 static int ocfs2_xattr_extend_allocation(struct inode *inode,
717 struct ocfs2_xattr_value_buf *vb,
718 struct ocfs2_xattr_set_ctxt *ctxt)
721 handle_t *handle = ctxt->handle;
722 enum ocfs2_alloc_restarted why;
723 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
724 struct ocfs2_extent_tree et;
726 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
728 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
730 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
731 OCFS2_JOURNAL_ACCESS_WRITE);
737 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
738 status = ocfs2_add_clusters_in_btree(handle,
751 status = ocfs2_journal_dirty(handle, vb->vb_bh);
757 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
760 * We should have already allocated enough space before the transaction,
761 * so no need to restart.
763 BUG_ON(why != RESTART_NONE || clusters_to_add);
770 static int __ocfs2_remove_xattr_range(struct inode *inode,
771 struct ocfs2_xattr_value_buf *vb,
772 u32 cpos, u32 phys_cpos, u32 len,
773 unsigned int ext_flags,
774 struct ocfs2_xattr_set_ctxt *ctxt)
777 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
778 handle_t *handle = ctxt->handle;
779 struct ocfs2_extent_tree et;
781 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
783 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
784 OCFS2_JOURNAL_ACCESS_WRITE);
790 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
797 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
799 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
805 if (ext_flags & OCFS2_EXT_REFCOUNTED)
806 ret = ocfs2_decrease_refcount(inode, handle,
807 ocfs2_blocks_to_clusters(inode->i_sb,
809 len, ctxt->meta_ac, &ctxt->dealloc, 1);
811 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
820 static int ocfs2_xattr_shrink_size(struct inode *inode,
823 struct ocfs2_xattr_value_buf *vb,
824 struct ocfs2_xattr_set_ctxt *ctxt)
827 unsigned int ext_flags;
828 u32 trunc_len, cpos, phys_cpos, alloc_size;
831 if (old_clusters <= new_clusters)
835 trunc_len = old_clusters - new_clusters;
837 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
839 &vb->vb_xv->xr_list, &ext_flags);
845 if (alloc_size > trunc_len)
846 alloc_size = trunc_len;
848 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
849 phys_cpos, alloc_size,
856 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
857 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
860 trunc_len -= alloc_size;
867 static int ocfs2_xattr_value_truncate(struct inode *inode,
868 struct ocfs2_xattr_value_buf *vb,
870 struct ocfs2_xattr_set_ctxt *ctxt)
873 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
874 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
876 if (new_clusters == old_clusters)
879 if (new_clusters > old_clusters)
880 ret = ocfs2_xattr_extend_allocation(inode,
881 new_clusters - old_clusters,
884 ret = ocfs2_xattr_shrink_size(inode,
885 old_clusters, new_clusters,
891 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
892 size_t *result, const char *prefix,
893 const char *name, int name_len)
895 char *p = buffer + *result;
896 int prefix_len = strlen(prefix);
897 int total_len = prefix_len + name_len + 1;
899 *result += total_len;
901 /* we are just looking for how big our buffer needs to be */
908 memcpy(p, prefix, prefix_len);
909 memcpy(p + prefix_len, name, name_len);
910 p[prefix_len + name_len] = '\0';
915 static int ocfs2_xattr_list_entries(struct inode *inode,
916 struct ocfs2_xattr_header *header,
917 char *buffer, size_t buffer_size)
921 const char *prefix, *name;
923 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
924 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
925 type = ocfs2_xattr_get_type(entry);
926 prefix = ocfs2_xattr_prefix(type);
929 name = (const char *)header +
930 le16_to_cpu(entry->xe_name_offset);
932 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
933 &result, prefix, name,
943 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
944 struct ocfs2_dinode *di)
946 struct ocfs2_xattr_header *xh;
949 xh = (struct ocfs2_xattr_header *)
950 ((void *)di + inode->i_sb->s_blocksize -
951 le16_to_cpu(di->i_xattr_inline_size));
953 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
954 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
960 static int ocfs2_xattr_ibody_list(struct inode *inode,
961 struct ocfs2_dinode *di,
965 struct ocfs2_xattr_header *header = NULL;
966 struct ocfs2_inode_info *oi = OCFS2_I(inode);
969 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
972 header = (struct ocfs2_xattr_header *)
973 ((void *)di + inode->i_sb->s_blocksize -
974 le16_to_cpu(di->i_xattr_inline_size));
976 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
981 static int ocfs2_xattr_block_list(struct inode *inode,
982 struct ocfs2_dinode *di,
986 struct buffer_head *blk_bh = NULL;
987 struct ocfs2_xattr_block *xb;
990 if (!di->i_xattr_loc)
993 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
1000 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1001 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1002 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
1003 ret = ocfs2_xattr_list_entries(inode, header,
1004 buffer, buffer_size);
1006 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
1007 buffer, buffer_size);
1014 ssize_t ocfs2_listxattr(struct dentry *dentry,
1018 int ret = 0, i_ret = 0, b_ret = 0;
1019 struct buffer_head *di_bh = NULL;
1020 struct ocfs2_dinode *di = NULL;
1021 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1023 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1026 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1029 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1035 di = (struct ocfs2_dinode *)di_bh->b_data;
1037 down_read(&oi->ip_xattr_sem);
1038 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1046 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1051 up_read(&oi->ip_xattr_sem);
1052 ocfs2_inode_unlock(dentry->d_inode, 0);
1056 return i_ret + b_ret;
1059 static int ocfs2_xattr_find_entry(int name_index,
1061 struct ocfs2_xattr_search *xs)
1063 struct ocfs2_xattr_entry *entry;
1070 name_len = strlen(name);
1072 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1073 cmp = name_index - ocfs2_xattr_get_type(entry);
1075 cmp = name_len - entry->xe_name_len;
1077 cmp = memcmp(name, (xs->base +
1078 le16_to_cpu(entry->xe_name_offset)),
1086 return cmp ? -ENODATA : 0;
1089 static int ocfs2_xattr_get_value_outside(struct inode *inode,
1090 struct ocfs2_xattr_value_root *xv,
1094 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1097 size_t cplen, blocksize;
1098 struct buffer_head *bh = NULL;
1099 struct ocfs2_extent_list *el;
1102 clusters = le32_to_cpu(xv->xr_clusters);
1103 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1104 blocksize = inode->i_sb->s_blocksize;
1107 while (cpos < clusters) {
1108 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1109 &num_clusters, el, NULL);
1115 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1116 /* Copy ocfs2_xattr_value */
1117 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1118 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1125 cplen = len >= blocksize ? blocksize : len;
1126 memcpy(buffer, bh->b_data, cplen);
1135 cpos += num_clusters;
1141 static int ocfs2_xattr_ibody_get(struct inode *inode,
1146 struct ocfs2_xattr_search *xs)
1148 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1149 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1150 struct ocfs2_xattr_value_root *xv;
1154 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1157 xs->end = (void *)di + inode->i_sb->s_blocksize;
1158 xs->header = (struct ocfs2_xattr_header *)
1159 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1160 xs->base = (void *)xs->header;
1161 xs->here = xs->header->xh_entries;
1163 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1166 size = le64_to_cpu(xs->here->xe_value_size);
1168 if (size > buffer_size)
1170 if (ocfs2_xattr_is_local(xs->here)) {
1171 memcpy(buffer, (void *)xs->base +
1172 le16_to_cpu(xs->here->xe_name_offset) +
1173 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1175 xv = (struct ocfs2_xattr_value_root *)
1176 (xs->base + le16_to_cpu(
1177 xs->here->xe_name_offset) +
1178 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1179 ret = ocfs2_xattr_get_value_outside(inode, xv,
1191 static int ocfs2_xattr_block_get(struct inode *inode,
1196 struct ocfs2_xattr_search *xs)
1198 struct ocfs2_xattr_block *xb;
1199 struct ocfs2_xattr_value_root *xv;
1201 int ret = -ENODATA, name_offset, name_len, i;
1202 int uninitialized_var(block_off);
1204 xs->bucket = ocfs2_xattr_bucket_new(inode);
1211 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1217 if (xs->not_found) {
1222 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1223 size = le64_to_cpu(xs->here->xe_value_size);
1226 if (size > buffer_size)
1229 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1230 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1231 i = xs->here - xs->header->xh_entries;
1233 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1234 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1235 bucket_xh(xs->bucket),
1239 xs->base = bucket_block(xs->bucket, block_off);
1241 if (ocfs2_xattr_is_local(xs->here)) {
1242 memcpy(buffer, (void *)xs->base +
1243 name_offset + name_len, size);
1245 xv = (struct ocfs2_xattr_value_root *)
1246 (xs->base + name_offset + name_len);
1247 ret = ocfs2_xattr_get_value_outside(inode, xv,
1257 ocfs2_xattr_bucket_free(xs->bucket);
1259 brelse(xs->xattr_bh);
1260 xs->xattr_bh = NULL;
1264 int ocfs2_xattr_get_nolock(struct inode *inode,
1265 struct buffer_head *di_bh,
1272 struct ocfs2_dinode *di = NULL;
1273 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1274 struct ocfs2_xattr_search xis = {
1275 .not_found = -ENODATA,
1277 struct ocfs2_xattr_search xbs = {
1278 .not_found = -ENODATA,
1281 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1284 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1287 xis.inode_bh = xbs.inode_bh = di_bh;
1288 di = (struct ocfs2_dinode *)di_bh->b_data;
1290 down_read(&oi->ip_xattr_sem);
1291 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1293 if (ret == -ENODATA && di->i_xattr_loc)
1294 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1296 up_read(&oi->ip_xattr_sem);
1301 /* ocfs2_xattr_get()
1303 * Copy an extended attribute into the buffer provided.
1304 * Buffer is NULL to compute the size of buffer required.
1306 static int ocfs2_xattr_get(struct inode *inode,
1313 struct buffer_head *di_bh = NULL;
1315 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1320 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1321 name, buffer, buffer_size);
1323 ocfs2_inode_unlock(inode, 0);
1330 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1332 struct ocfs2_xattr_value_buf *vb,
1336 int ret = 0, i, cp_len;
1337 u16 blocksize = inode->i_sb->s_blocksize;
1338 u32 p_cluster, num_clusters;
1339 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1340 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1342 struct buffer_head *bh = NULL;
1343 unsigned int ext_flags;
1344 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1346 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1348 while (cpos < clusters) {
1349 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1350 &num_clusters, &xv->xr_list,
1357 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1359 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1361 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1362 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1369 ret = ocfs2_journal_access(handle,
1372 OCFS2_JOURNAL_ACCESS_WRITE);
1378 cp_len = value_len > blocksize ? blocksize : value_len;
1379 memcpy(bh->b_data, value, cp_len);
1380 value_len -= cp_len;
1382 if (cp_len < blocksize)
1383 memset(bh->b_data + cp_len, 0,
1384 blocksize - cp_len);
1386 ret = ocfs2_journal_dirty(handle, bh);
1395 * XXX: do we need to empty all the following
1396 * blocks in this cluster?
1401 cpos += num_clusters;
1409 static int ocfs2_xattr_cleanup(struct inode *inode,
1411 struct ocfs2_xattr_info *xi,
1412 struct ocfs2_xattr_search *xs,
1413 struct ocfs2_xattr_value_buf *vb,
1417 void *val = xs->base + offs;
1418 size_t size = namevalue_size_xi(xi);
1420 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1421 OCFS2_JOURNAL_ACCESS_WRITE);
1426 /* Decrease xattr count */
1427 le16_add_cpu(&xs->header->xh_count, -1);
1428 /* Remove the xattr entry and tree root which has already be set*/
1429 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1430 memset(val, 0, size);
1432 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1439 static int ocfs2_xattr_update_entry(struct inode *inode,
1441 struct ocfs2_xattr_info *xi,
1442 struct ocfs2_xattr_search *xs,
1443 struct ocfs2_xattr_value_buf *vb,
1448 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1449 OCFS2_JOURNAL_ACCESS_WRITE);
1455 xs->here->xe_name_offset = cpu_to_le16(offs);
1456 xs->here->xe_value_size = cpu_to_le64(xi->xi_value_len);
1457 if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
1458 ocfs2_xattr_set_local(xs->here, 1);
1460 ocfs2_xattr_set_local(xs->here, 0);
1461 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1463 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1471 * ocfs2_xattr_set_value_outside()
1473 * Set large size value in B tree.
1475 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1476 struct ocfs2_xattr_info *xi,
1477 struct ocfs2_xattr_search *xs,
1478 struct ocfs2_xattr_set_ctxt *ctxt,
1479 struct ocfs2_xattr_value_buf *vb,
1482 void *val = xs->base + offs;
1483 struct ocfs2_xattr_value_root *xv = NULL;
1484 size_t size = namevalue_size_xi(xi);
1487 memset(val, 0, size);
1488 memcpy(val, xi->xi_name, xi->xi_name_len);
1489 xv = (struct ocfs2_xattr_value_root *)
1490 (val + OCFS2_XATTR_SIZE(xi->xi_name_len));
1491 xv->xr_clusters = 0;
1492 xv->xr_last_eb_blk = 0;
1493 xv->xr_list.l_tree_depth = 0;
1494 xv->xr_list.l_count = cpu_to_le16(1);
1495 xv->xr_list.l_next_free_rec = 0;
1498 ret = ocfs2_xattr_value_truncate(inode, vb, xi->xi_value_len, ctxt);
1503 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
1508 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
1509 xi->xi_value, xi->xi_value_len);
1516 static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1524 free_space = free_start -
1525 sizeof(struct ocfs2_xattr_header) -
1526 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1527 OCFS2_XATTR_HEADER_GAP;
1530 if (free_space < needed_space)
1536 /* Give a pointer into the storage for the given offset */
1537 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1539 BUG_ON(offset >= loc->xl_size);
1540 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1544 * Wipe the name+value pair and allow the storage to reclaim it. This
1545 * must be followed by either removal of the entry or a call to
1546 * ocfs2_xa_add_namevalue().
1548 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1550 loc->xl_ops->xlo_wipe_namevalue(loc);
1554 * Find lowest offset to a name+value pair. This is the start of our
1555 * downward-growing free space.
1557 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1559 return loc->xl_ops->xlo_get_free_start(loc);
1562 /* Can we reuse loc->xl_entry for xi? */
1563 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1564 struct ocfs2_xattr_info *xi)
1566 return loc->xl_ops->xlo_can_reuse(loc, xi);
1569 /* How much free space is needed to set the new value */
1570 static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1571 struct ocfs2_xattr_info *xi)
1573 return loc->xl_ops->xlo_check_space(loc, xi);
1576 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1578 loc->xl_ops->xlo_add_entry(loc, name_hash);
1579 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1581 * We can't leave the new entry's xe_name_offset at zero or
1582 * add_namevalue() will go nuts. We set it to the size of our
1583 * storage so that it can never be less than any other entry.
1585 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1588 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1589 struct ocfs2_xattr_info *xi)
1591 int size = namevalue_size_xi(xi);
1595 loc->xl_ops->xlo_add_namevalue(loc, size);
1596 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1597 loc->xl_entry->xe_name_len = xi->xi_name_len;
1598 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1599 ocfs2_xattr_set_local(loc->xl_entry,
1600 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1602 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1603 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1604 memset(nameval_buf, 0, size);
1605 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1608 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1611 return (char *)loc->xl_header + offset;
1614 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1615 struct ocfs2_xattr_info *xi)
1618 * Block storage is strict. If the sizes aren't exact, we will
1619 * remove the old one and reinsert the new.
1621 return namevalue_size_xe(loc->xl_entry) ==
1622 namevalue_size_xi(xi);
1625 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1627 struct ocfs2_xattr_header *xh = loc->xl_header;
1628 int i, count = le16_to_cpu(xh->xh_count);
1629 int offset, free_start = loc->xl_size;
1631 for (i = 0; i < count; i++) {
1632 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1633 if (offset < free_start)
1634 free_start = offset;
1640 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1641 struct ocfs2_xattr_info *xi)
1643 int count = le16_to_cpu(loc->xl_header->xh_count);
1644 int free_start = ocfs2_xa_get_free_start(loc);
1645 int needed_space = ocfs2_xi_entry_usage(xi);
1648 * Block storage will reclaim the original entry before inserting
1649 * the new value, so we only need the difference. If the new
1650 * entry is smaller than the old one, we don't need anything.
1652 if (loc->xl_entry) {
1653 /* Don't need space if we're reusing! */
1654 if (ocfs2_xa_can_reuse_entry(loc, xi))
1657 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1659 if (needed_space < 0)
1661 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1665 * Block storage for xattrs keeps the name+value pairs compacted. When
1666 * we remove one, we have to shift any that preceded it towards the end.
1668 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1671 int namevalue_offset, first_namevalue_offset, namevalue_size;
1672 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1673 struct ocfs2_xattr_header *xh = loc->xl_header;
1674 int count = le16_to_cpu(xh->xh_count);
1676 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1677 namevalue_size = namevalue_size_xe(entry);
1678 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1680 /* Shift the name+value pairs */
1681 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1682 (char *)xh + first_namevalue_offset,
1683 namevalue_offset - first_namevalue_offset);
1684 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1686 /* Now tell xh->xh_entries about it */
1687 for (i = 0; i < count; i++) {
1688 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1689 if (offset < namevalue_offset)
1690 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1695 * Note that we don't update xh_free_start or xh_name_value_len
1696 * because they're not used in block-stored xattrs.
1700 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1702 int count = le16_to_cpu(loc->xl_header->xh_count);
1703 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1704 le16_add_cpu(&loc->xl_header->xh_count, 1);
1705 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1708 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1710 int free_start = ocfs2_xa_get_free_start(loc);
1712 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1716 * Operations for xattrs stored in blocks. This includes inline inode
1717 * storage and unindexed ocfs2_xattr_blocks.
1719 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1720 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
1721 .xlo_check_space = ocfs2_xa_block_check_space,
1722 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1723 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
1724 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
1725 .xlo_add_entry = ocfs2_xa_block_add_entry,
1726 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
1729 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1732 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1733 int block, block_offset;
1735 /* The header is at the front of the bucket */
1736 block = offset >> bucket->bu_inode->i_sb->s_blocksize_bits;
1737 block_offset = offset % bucket->bu_inode->i_sb->s_blocksize;
1739 return bucket_block(bucket, block) + block_offset;
1742 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1743 struct ocfs2_xattr_info *xi)
1745 return namevalue_size_xe(loc->xl_entry) >=
1746 namevalue_size_xi(xi);
1749 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1751 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1752 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1755 static int ocfs2_bucket_align_free_start(struct super_block *sb,
1756 int free_start, int size)
1759 * We need to make sure that the name+value pair fits within
1762 if (((free_start - size) >> sb->s_blocksize_bits) !=
1763 ((free_start - 1) >> sb->s_blocksize_bits))
1764 free_start -= free_start % sb->s_blocksize;
1769 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1770 struct ocfs2_xattr_info *xi)
1773 int count = le16_to_cpu(loc->xl_header->xh_count);
1774 int free_start = ocfs2_xa_get_free_start(loc);
1775 int needed_space = ocfs2_xi_entry_usage(xi);
1776 int size = namevalue_size_xi(xi);
1777 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1778 struct super_block *sb = bucket->bu_inode->i_sb;
1781 * Bucket storage does not reclaim name+value pairs it cannot
1782 * reuse. They live as holes until the bucket fills, and then
1783 * the bucket is defragmented. However, the bucket can reclaim
1784 * the ocfs2_xattr_entry.
1786 if (loc->xl_entry) {
1787 /* Don't need space if we're reusing! */
1788 if (ocfs2_xa_can_reuse_entry(loc, xi))
1791 needed_space -= sizeof(struct ocfs2_xattr_entry);
1793 BUG_ON(needed_space < 0);
1795 if (free_start < size) {
1800 * First we check if it would fit in the first place.
1801 * Below, we align the free start to a block. This may
1802 * slide us below the minimum gap. By checking unaligned
1803 * first, we avoid that error.
1805 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1809 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1812 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1815 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1817 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1818 -namevalue_size_xe(loc->xl_entry));
1821 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1823 struct ocfs2_xattr_header *xh = loc->xl_header;
1824 int count = le16_to_cpu(xh->xh_count);
1825 int low = 0, high = count - 1, tmp;
1826 struct ocfs2_xattr_entry *tmp_xe;
1829 * We keep buckets sorted by name_hash, so we need to find
1832 while (low <= high && count) {
1833 tmp = (low + high) / 2;
1834 tmp_xe = &xh->xh_entries[tmp];
1836 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1838 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1847 memmove(&xh->xh_entries[low + 1],
1848 &xh->xh_entries[low],
1849 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1851 le16_add_cpu(&xh->xh_count, 1);
1852 loc->xl_entry = &xh->xh_entries[low];
1853 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1856 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1858 int free_start = ocfs2_xa_get_free_start(loc);
1859 struct ocfs2_xattr_header *xh = loc->xl_header;
1860 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1861 struct super_block *sb = bucket->bu_inode->i_sb;
1864 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1865 nameval_offset = free_start - size;
1866 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1867 xh->xh_free_start = cpu_to_le16(nameval_offset);
1868 le16_add_cpu(&xh->xh_name_value_len, size);
1872 /* Operations for xattrs stored in buckets. */
1873 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1874 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
1875 .xlo_check_space = ocfs2_xa_bucket_check_space,
1876 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1877 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
1878 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
1879 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1880 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
1883 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1886 struct ocfs2_xattr_header *xh = loc->xl_header;
1887 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1889 ocfs2_xa_wipe_namevalue(loc);
1890 loc->xl_entry = NULL;
1892 le16_add_cpu(&xh->xh_count, -1);
1893 count = le16_to_cpu(xh->xh_count);
1896 * Only zero out the entry if there are more remaining. This is
1897 * important for an empty bucket, as it keeps track of the
1898 * bucket's hash value. It doesn't hurt empty block storage.
1901 index = ((char *)entry - (char *)&xh->xh_entries) /
1902 sizeof(struct ocfs2_xattr_entry);
1903 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1904 (count - index) * sizeof(struct ocfs2_xattr_entry));
1905 memset(&xh->xh_entries[count], 0,
1906 sizeof(struct ocfs2_xattr_entry));
1911 * Prepares loc->xl_entry to receive the new xattr. This includes
1912 * properly setting up the name+value pair region. If loc->xl_entry
1913 * already exists, it will take care of modifying it appropriately.
1914 * This also includes deleting entries, but don't call this to remove
1915 * a non-existant entry. That's just a bug.
1917 * Note that this modifies the data. You did journal_access already,
1920 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
1921 struct ocfs2_xattr_info *xi,
1925 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
1928 if (!xi->xi_value) {
1929 ocfs2_xa_remove_entry(loc);
1933 rc = ocfs2_xa_check_space(loc, xi);
1937 if (loc->xl_entry) {
1938 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
1939 nameval_buf = ocfs2_xa_offset_pointer(loc,
1940 le16_to_cpu(loc->xl_entry->xe_name_offset));
1941 memset(nameval_buf + name_size, 0,
1942 namevalue_size_xe(loc->xl_entry) - name_size);
1943 loc->xl_entry->xe_value_size =
1944 cpu_to_le64(xi->xi_value_len);
1948 ocfs2_xa_wipe_namevalue(loc);
1950 ocfs2_xa_add_entry(loc, name_hash);
1953 * If we get here, we have a blank entry. Fill it. We grow our
1954 * name+value pair back from the end.
1956 ocfs2_xa_add_namevalue(loc, xi);
1963 * Store the value portion of the name+value pair. This is either an
1964 * inline value or the tree root of an external value.
1966 static void ocfs2_xa_store_inline_value(struct ocfs2_xa_loc *loc,
1967 struct ocfs2_xattr_info *xi)
1969 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1970 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
1971 int size = namevalue_size_xi(xi);
1977 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1978 memcpy(nameval_buf + name_size, xi->xi_value, size - name_size);
1981 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
1982 struct inode *inode,
1983 struct buffer_head *bh,
1984 struct ocfs2_xattr_entry *entry)
1986 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1988 loc->xl_ops = &ocfs2_xa_block_loc_ops;
1989 loc->xl_storage = bh;
1990 loc->xl_entry = entry;
1992 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1993 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
1996 loc->xl_size = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1999 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2003 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2004 struct buffer_head *bh,
2005 struct ocfs2_xattr_entry *entry)
2007 struct ocfs2_xattr_block *xb =
2008 (struct ocfs2_xattr_block *)bh->b_data;
2010 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2012 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2013 loc->xl_storage = bh;
2014 loc->xl_header = &(xb->xb_attrs.xb_header);
2015 loc->xl_entry = entry;
2016 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2017 xb_attrs.xb_header);
2020 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2021 struct ocfs2_xattr_bucket *bucket,
2022 struct ocfs2_xattr_entry *entry)
2024 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2025 loc->xl_storage = bucket;
2026 loc->xl_header = bucket_xh(bucket);
2027 loc->xl_entry = entry;
2028 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2033 * ocfs2_xattr_set_entry()
2035 * Set extended attribute entry into inode or block.
2037 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
2038 * We first insert tree root(ocfs2_xattr_value_root) like a normal value,
2039 * then set value in B tree with set_value_outside().
2041 static int ocfs2_xattr_set_entry(struct inode *inode,
2042 struct ocfs2_xattr_info *xi,
2043 struct ocfs2_xattr_search *xs,
2044 struct ocfs2_xattr_set_ctxt *ctxt,
2047 struct ocfs2_xattr_entry *last;
2048 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2049 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2050 size_t min_offs = xs->end - xs->base;
2052 handle_t *handle = ctxt->handle;
2054 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
2056 struct ocfs2_xa_loc loc;
2057 struct ocfs2_xattr_info xi_l = {
2058 .xi_name_index = xi->xi_name_index,
2059 .xi_name = xi->xi_name,
2060 .xi_name_len = xi->xi_name_len,
2061 .xi_value = xi->xi_value,
2062 .xi_value_len = xi->xi_value_len,
2064 struct ocfs2_xattr_value_buf vb = {
2065 .vb_bh = xs->xattr_bh,
2066 .vb_access = ocfs2_journal_access_di,
2069 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
2070 BUG_ON(xs->xattr_bh == xs->inode_bh);
2071 vb.vb_access = ocfs2_journal_access_xb;
2073 BUG_ON(xs->xattr_bh != xs->inode_bh);
2075 /* Compute min_offs, last and free space. */
2076 last = xs->header->xh_entries;
2078 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
2079 size_t offs = le16_to_cpu(last->xe_name_offset);
2080 if (offs < min_offs)
2085 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2090 free += ocfs2_xe_entry_usage(xs->here);
2092 /* Check free space in inode or block */
2094 if (free < ocfs2_xi_entry_usage(xi)) {
2098 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2099 size_l = namevalue_size_xi(xi);
2100 xi_l.xi_value = (void *)&def_xv;
2101 xi_l.xi_value_len = OCFS2_XATTR_ROOT_SIZE;
2105 if (!xs->not_found) {
2106 /* For existing extended attribute */
2107 size_t size = namevalue_size_xe(xs->here);
2108 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
2109 void *val = xs->base + offs;
2111 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
2112 /* Replace existing local xattr with tree root */
2113 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
2118 } else if (!ocfs2_xattr_is_local(xs->here)) {
2119 /* For existing xattr which has value outside */
2120 vb.vb_xv = (struct ocfs2_xattr_value_root *)
2121 (val + OCFS2_XATTR_SIZE(xi->xi_name_len));
2123 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2125 * If new value need set outside also,
2126 * first truncate old value to new value,
2127 * then set new value with set_value_outside().
2129 ret = ocfs2_xattr_value_truncate(inode,
2138 ret = ocfs2_xattr_update_entry(inode,
2149 ret = __ocfs2_xattr_set_value_outside(inode,
2159 * If new value need set in local,
2160 * just trucate old value to zero.
2162 ret = ocfs2_xattr_value_truncate(inode,
2172 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
2173 OCFS2_JOURNAL_ACCESS_WRITE);
2179 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
2180 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
2181 OCFS2_JOURNAL_ACCESS_WRITE);
2188 if (xs->xattr_bh == xs->inode_bh)
2189 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2190 xs->not_found ? NULL : xs->here);
2192 ocfs2_init_xattr_block_xa_loc(&loc, xs->xattr_bh,
2193 xs->not_found ? NULL : xs->here);
2196 * Prepare our entry and insert the inline value. This will
2197 * be a value tree root for values that are larger than
2198 * OCFS2_XATTR_INLINE_SIZE.
2200 ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash);
2206 /* XXX For now, until we make ocfs2_xa_prepare_entry() primary */
2207 BUG_ON(ret == -ENOSPC);
2208 ocfs2_xa_store_inline_value(&loc, xi);
2209 xs->here = loc.xl_entry;
2211 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
2212 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
2219 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
2220 (flag & OCFS2_INLINE_XATTR_FL)) {
2221 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2222 unsigned int xattrsize = osb->s_xattr_inline_size;
2225 * Adjust extent record count or inline data size
2226 * to reserve space for extended attribute.
2228 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2229 struct ocfs2_inline_data *idata = &di->id2.i_data;
2230 le16_add_cpu(&idata->id_count, -xattrsize);
2231 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2232 struct ocfs2_extent_list *el = &di->id2.i_list;
2233 le16_add_cpu(&el->l_count, -(xattrsize /
2234 sizeof(struct ocfs2_extent_rec)));
2236 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2238 /* Update xattr flag */
2239 spin_lock(&oi->ip_lock);
2240 oi->ip_dyn_features |= flag;
2241 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2242 spin_unlock(&oi->ip_lock);
2244 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
2248 if (!ret && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2250 * Set value outside in B tree.
2251 * This is the second step for value size > INLINE_SIZE.
2253 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
2254 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
2261 * If set value outside failed, we have to clean
2262 * the junk tree root we have already set in local.
2264 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
2275 * In xattr remove, if it is stored outside and refcounted, we may have
2276 * the chance to split the refcount tree. So need the allocators.
2278 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2279 struct ocfs2_xattr_value_root *xv,
2280 struct ocfs2_caching_info *ref_ci,
2281 struct buffer_head *ref_root_bh,
2282 struct ocfs2_alloc_context **meta_ac,
2285 int ret, meta_add = 0;
2286 u32 p_cluster, num_clusters;
2287 unsigned int ext_flags;
2290 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2299 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2302 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2304 &meta_add, ref_credits);
2310 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2319 static int ocfs2_remove_value_outside(struct inode*inode,
2320 struct ocfs2_xattr_value_buf *vb,
2321 struct ocfs2_xattr_header *header,
2322 struct ocfs2_caching_info *ref_ci,
2323 struct buffer_head *ref_root_bh)
2325 int ret = 0, i, ref_credits;
2326 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2327 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2330 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2332 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2333 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2335 if (ocfs2_xattr_is_local(entry))
2338 val = (void *)header +
2339 le16_to_cpu(entry->xe_name_offset);
2340 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2341 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2343 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2344 ref_ci, ref_root_bh,
2348 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2349 ocfs2_remove_extent_credits(osb->sb));
2350 if (IS_ERR(ctxt.handle)) {
2351 ret = PTR_ERR(ctxt.handle);
2356 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2362 ocfs2_commit_trans(osb, ctxt.handle);
2364 ocfs2_free_alloc_context(ctxt.meta_ac);
2365 ctxt.meta_ac = NULL;
2370 ocfs2_free_alloc_context(ctxt.meta_ac);
2371 ocfs2_schedule_truncate_log_flush(osb, 1);
2372 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2376 static int ocfs2_xattr_ibody_remove(struct inode *inode,
2377 struct buffer_head *di_bh,
2378 struct ocfs2_caching_info *ref_ci,
2379 struct buffer_head *ref_root_bh)
2382 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2383 struct ocfs2_xattr_header *header;
2385 struct ocfs2_xattr_value_buf vb = {
2387 .vb_access = ocfs2_journal_access_di,
2390 header = (struct ocfs2_xattr_header *)
2391 ((void *)di + inode->i_sb->s_blocksize -
2392 le16_to_cpu(di->i_xattr_inline_size));
2394 ret = ocfs2_remove_value_outside(inode, &vb, header,
2395 ref_ci, ref_root_bh);
2400 struct ocfs2_rm_xattr_bucket_para {
2401 struct ocfs2_caching_info *ref_ci;
2402 struct buffer_head *ref_root_bh;
2405 static int ocfs2_xattr_block_remove(struct inode *inode,
2406 struct buffer_head *blk_bh,
2407 struct ocfs2_caching_info *ref_ci,
2408 struct buffer_head *ref_root_bh)
2410 struct ocfs2_xattr_block *xb;
2412 struct ocfs2_xattr_value_buf vb = {
2414 .vb_access = ocfs2_journal_access_xb,
2416 struct ocfs2_rm_xattr_bucket_para args = {
2418 .ref_root_bh = ref_root_bh,
2421 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2422 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2423 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2424 ret = ocfs2_remove_value_outside(inode, &vb, header,
2425 ref_ci, ref_root_bh);
2427 ret = ocfs2_iterate_xattr_index_block(inode,
2429 ocfs2_rm_xattr_cluster,
2435 static int ocfs2_xattr_free_block(struct inode *inode,
2437 struct ocfs2_caching_info *ref_ci,
2438 struct buffer_head *ref_root_bh)
2440 struct inode *xb_alloc_inode;
2441 struct buffer_head *xb_alloc_bh = NULL;
2442 struct buffer_head *blk_bh = NULL;
2443 struct ocfs2_xattr_block *xb;
2444 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2450 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2456 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2462 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2463 blk = le64_to_cpu(xb->xb_blkno);
2464 bit = le16_to_cpu(xb->xb_suballoc_bit);
2465 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2467 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2468 EXTENT_ALLOC_SYSTEM_INODE,
2469 le16_to_cpu(xb->xb_suballoc_slot));
2470 if (!xb_alloc_inode) {
2475 mutex_lock(&xb_alloc_inode->i_mutex);
2477 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2483 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2484 if (IS_ERR(handle)) {
2485 ret = PTR_ERR(handle);
2490 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2495 ocfs2_commit_trans(osb, handle);
2497 ocfs2_inode_unlock(xb_alloc_inode, 1);
2498 brelse(xb_alloc_bh);
2500 mutex_unlock(&xb_alloc_inode->i_mutex);
2501 iput(xb_alloc_inode);
2508 * ocfs2_xattr_remove()
2510 * Free extended attribute resources associated with this inode.
2512 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2514 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2515 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2516 struct ocfs2_refcount_tree *ref_tree = NULL;
2517 struct buffer_head *ref_root_bh = NULL;
2518 struct ocfs2_caching_info *ref_ci = NULL;
2522 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2525 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2528 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2529 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2530 le64_to_cpu(di->i_refcount_loc),
2531 1, &ref_tree, &ref_root_bh);
2536 ref_ci = &ref_tree->rf_ci;
2540 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2541 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2542 ref_ci, ref_root_bh);
2549 if (di->i_xattr_loc) {
2550 ret = ocfs2_xattr_free_block(inode,
2551 le64_to_cpu(di->i_xattr_loc),
2552 ref_ci, ref_root_bh);
2559 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2560 OCFS2_INODE_UPDATE_CREDITS);
2561 if (IS_ERR(handle)) {
2562 ret = PTR_ERR(handle);
2566 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2567 OCFS2_JOURNAL_ACCESS_WRITE);
2573 di->i_xattr_loc = 0;
2575 spin_lock(&oi->ip_lock);
2576 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2577 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2578 spin_unlock(&oi->ip_lock);
2580 ret = ocfs2_journal_dirty(handle, di_bh);
2584 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2587 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2588 brelse(ref_root_bh);
2592 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2593 struct ocfs2_dinode *di)
2595 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2596 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2599 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2602 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2603 struct ocfs2_inline_data *idata = &di->id2.i_data;
2604 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2605 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2606 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2607 le64_to_cpu(di->i_size);
2609 struct ocfs2_extent_list *el = &di->id2.i_list;
2610 free = (le16_to_cpu(el->l_count) -
2611 le16_to_cpu(el->l_next_free_rec)) *
2612 sizeof(struct ocfs2_extent_rec);
2614 if (free >= xattrsize)
2621 * ocfs2_xattr_ibody_find()
2623 * Find extended attribute in inode block and
2624 * fill search info into struct ocfs2_xattr_search.
2626 static int ocfs2_xattr_ibody_find(struct inode *inode,
2629 struct ocfs2_xattr_search *xs)
2631 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2632 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2636 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2639 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2640 down_read(&oi->ip_alloc_sem);
2641 has_space = ocfs2_xattr_has_space_inline(inode, di);
2642 up_read(&oi->ip_alloc_sem);
2647 xs->xattr_bh = xs->inode_bh;
2648 xs->end = (void *)di + inode->i_sb->s_blocksize;
2649 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2650 xs->header = (struct ocfs2_xattr_header *)
2651 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2653 xs->header = (struct ocfs2_xattr_header *)
2654 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2655 xs->base = (void *)xs->header;
2656 xs->here = xs->header->xh_entries;
2658 /* Find the named attribute. */
2659 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2660 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2661 if (ret && ret != -ENODATA)
2663 xs->not_found = ret;
2670 * ocfs2_xattr_ibody_set()
2672 * Set, replace or remove an extended attribute into inode block.
2675 static int ocfs2_xattr_ibody_set(struct inode *inode,
2676 struct ocfs2_xattr_info *xi,
2677 struct ocfs2_xattr_search *xs,
2678 struct ocfs2_xattr_set_ctxt *ctxt)
2680 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2681 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2684 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2687 down_write(&oi->ip_alloc_sem);
2688 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2689 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2695 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2696 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2698 up_write(&oi->ip_alloc_sem);
2704 * ocfs2_xattr_block_find()
2706 * Find extended attribute in external block and
2707 * fill search info into struct ocfs2_xattr_search.
2709 static int ocfs2_xattr_block_find(struct inode *inode,
2712 struct ocfs2_xattr_search *xs)
2714 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2715 struct buffer_head *blk_bh = NULL;
2716 struct ocfs2_xattr_block *xb;
2719 if (!di->i_xattr_loc)
2722 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2729 xs->xattr_bh = blk_bh;
2730 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2732 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2733 xs->header = &xb->xb_attrs.xb_header;
2734 xs->base = (void *)xs->header;
2735 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2736 xs->here = xs->header->xh_entries;
2738 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2740 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2744 if (ret && ret != -ENODATA) {
2745 xs->xattr_bh = NULL;
2748 xs->not_found = ret;
2756 static int ocfs2_create_xattr_block(handle_t *handle,
2757 struct inode *inode,
2758 struct buffer_head *inode_bh,
2759 struct ocfs2_alloc_context *meta_ac,
2760 struct buffer_head **ret_bh,
2764 u16 suballoc_bit_start;
2767 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2768 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2769 struct buffer_head *new_bh = NULL;
2770 struct ocfs2_xattr_block *xblk;
2772 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2773 OCFS2_JOURNAL_ACCESS_CREATE);
2779 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2780 &suballoc_bit_start, &num_got,
2787 new_bh = sb_getblk(inode->i_sb, first_blkno);
2788 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2790 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2792 OCFS2_JOURNAL_ACCESS_CREATE);
2798 /* Initialize ocfs2_xattr_block */
2799 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2800 memset(xblk, 0, inode->i_sb->s_blocksize);
2801 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2802 xblk->xb_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
2803 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2804 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2805 xblk->xb_blkno = cpu_to_le64(first_blkno);
2808 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2809 xr->xt_clusters = cpu_to_le32(1);
2810 xr->xt_last_eb_blk = 0;
2811 xr->xt_list.l_tree_depth = 0;
2812 xr->xt_list.l_count = cpu_to_le16(
2813 ocfs2_xattr_recs_per_xb(inode->i_sb));
2814 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2815 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2818 ret = ocfs2_journal_dirty(handle, new_bh);
2823 di->i_xattr_loc = cpu_to_le64(first_blkno);
2824 ocfs2_journal_dirty(handle, inode_bh);
2835 * ocfs2_xattr_block_set()
2837 * Set, replace or remove an extended attribute into external block.
2840 static int ocfs2_xattr_block_set(struct inode *inode,
2841 struct ocfs2_xattr_info *xi,
2842 struct ocfs2_xattr_search *xs,
2843 struct ocfs2_xattr_set_ctxt *ctxt)
2845 struct buffer_head *new_bh = NULL;
2846 handle_t *handle = ctxt->handle;
2847 struct ocfs2_xattr_block *xblk = NULL;
2850 if (!xs->xattr_bh) {
2851 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
2852 ctxt->meta_ac, &new_bh, 0);
2858 xs->xattr_bh = new_bh;
2859 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2860 xs->header = &xblk->xb_attrs.xb_header;
2861 xs->base = (void *)xs->header;
2862 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2863 xs->here = xs->header->xh_entries;
2865 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2867 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2868 /* Set extended attribute into external block */
2869 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2870 OCFS2_HAS_XATTR_FL);
2871 if (!ret || ret != -ENOSPC)
2874 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2879 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2886 /* Check whether the new xattr can be inserted into the inode. */
2887 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2888 struct ocfs2_xattr_info *xi,
2889 struct ocfs2_xattr_search *xs)
2891 struct ocfs2_xattr_entry *last;
2893 size_t min_offs = xs->end - xs->base;
2898 last = xs->header->xh_entries;
2900 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2901 size_t offs = le16_to_cpu(last->xe_name_offset);
2902 if (offs < min_offs)
2907 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2911 BUG_ON(!xs->not_found);
2913 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
2919 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2920 struct ocfs2_dinode *di,
2921 struct ocfs2_xattr_info *xi,
2922 struct ocfs2_xattr_search *xis,
2923 struct ocfs2_xattr_search *xbs,
2928 int ret = 0, old_in_xb = 0;
2929 int clusters_add = 0, meta_add = 0, credits = 0;
2930 struct buffer_head *bh = NULL;
2931 struct ocfs2_xattr_block *xb = NULL;
2932 struct ocfs2_xattr_entry *xe = NULL;
2933 struct ocfs2_xattr_value_root *xv = NULL;
2935 int name_offset, name_len = 0;
2936 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2941 * Calculate the clusters we need to write.
2942 * No matter whether we replace an old one or add a new one,
2943 * we need this for writing.
2945 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2946 credits += new_clusters *
2947 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2949 if (xis->not_found && xbs->not_found) {
2950 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2952 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2953 clusters_add += new_clusters;
2954 credits += ocfs2_calc_extend_credits(inode->i_sb,
2962 if (!xis->not_found) {
2964 name_offset = le16_to_cpu(xe->xe_name_offset);
2965 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2967 credits += OCFS2_INODE_UPDATE_CREDITS;
2969 int i, block_off = 0;
2970 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2972 name_offset = le16_to_cpu(xe->xe_name_offset);
2973 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2974 i = xbs->here - xbs->header->xh_entries;
2977 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2978 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
2979 bucket_xh(xbs->bucket),
2982 base = bucket_block(xbs->bucket, block_off);
2983 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2986 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2991 * delete a xattr doesn't need metadata and cluster allocation.
2992 * so just calculate the credits and return.
2994 * The credits for removing the value tree will be extended
2995 * by ocfs2_remove_extent itself.
2997 if (!xi->xi_value) {
2998 if (!ocfs2_xattr_is_local(xe))
2999 credits += ocfs2_remove_extent_credits(inode->i_sb);
3004 /* do cluster allocation guess first. */
3005 value_size = le64_to_cpu(xe->xe_value_size);
3009 * In xattr set, we always try to set the xe in inode first,
3010 * so if it can be inserted into inode successfully, the old
3011 * one will be removed from the xattr block, and this xattr
3012 * will be inserted into inode as a new xattr in inode.
3014 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3015 clusters_add += new_clusters;
3016 credits += ocfs2_remove_extent_credits(inode->i_sb) +
3017 OCFS2_INODE_UPDATE_CREDITS;
3018 if (!ocfs2_xattr_is_local(xe))
3019 credits += ocfs2_calc_extend_credits(
3027 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3028 /* the new values will be stored outside. */
3029 u32 old_clusters = 0;
3031 if (!ocfs2_xattr_is_local(xe)) {
3032 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3034 xv = (struct ocfs2_xattr_value_root *)
3035 (base + name_offset + name_len);
3036 value_size = OCFS2_XATTR_ROOT_SIZE;
3040 if (old_clusters >= new_clusters) {
3041 credits += ocfs2_remove_extent_credits(inode->i_sb);
3044 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3045 clusters_add += new_clusters - old_clusters;
3046 credits += ocfs2_calc_extend_credits(inode->i_sb,
3050 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3055 * Now the new value will be stored inside. So if the new
3056 * value is smaller than the size of value root or the old
3057 * value, we don't need any allocation, otherwise we have
3058 * to guess metadata allocation.
3060 if ((ocfs2_xattr_is_local(xe) &&
3061 (value_size >= xi->xi_value_len)) ||
3062 (!ocfs2_xattr_is_local(xe) &&
3063 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
3068 /* calculate metadata allocation. */
3069 if (di->i_xattr_loc) {
3070 if (!xbs->xattr_bh) {
3071 ret = ocfs2_read_xattr_block(inode,
3072 le64_to_cpu(di->i_xattr_loc),
3079 xb = (struct ocfs2_xattr_block *)bh->b_data;
3081 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3084 * If there is already an xattr tree, good, we can calculate
3085 * like other b-trees. Otherwise we may have the chance of
3086 * create a tree, the credit calculation is borrowed from
3087 * ocfs2_calc_extend_credits with root_el = NULL. And the
3088 * new tree will be cluster based, so no meta is needed.
3090 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3091 struct ocfs2_extent_list *el =
3092 &xb->xb_attrs.xb_root.xt_list;
3093 meta_add += ocfs2_extend_meta_needed(el);
3094 credits += ocfs2_calc_extend_credits(inode->i_sb,
3097 credits += OCFS2_SUBALLOC_ALLOC + 1;
3100 * This cluster will be used either for new bucket or for
3102 * If the cluster size is the same as the bucket size, one
3103 * more is needed since we may need to extend the bucket
3107 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3108 if (OCFS2_XATTR_BUCKET_SIZE ==
3109 OCFS2_SB(inode->i_sb)->s_clustersize) {
3110 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3115 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3119 *clusters_need = clusters_add;
3121 *meta_need = meta_add;
3123 *credits_need = credits;
3128 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3129 struct ocfs2_dinode *di,
3130 struct ocfs2_xattr_info *xi,
3131 struct ocfs2_xattr_search *xis,
3132 struct ocfs2_xattr_search *xbs,
3133 struct ocfs2_xattr_set_ctxt *ctxt,
3137 int clusters_add, meta_add, ret;
3138 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3140 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3142 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3144 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
3145 &clusters_add, &meta_add, credits);
3151 meta_add += extra_meta;
3152 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
3153 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
3156 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3165 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3171 if (ctxt->meta_ac) {
3172 ocfs2_free_alloc_context(ctxt->meta_ac);
3173 ctxt->meta_ac = NULL;
3177 * We cannot have an error and a non null ctxt->data_ac.
3184 static int __ocfs2_xattr_set_handle(struct inode *inode,
3185 struct ocfs2_dinode *di,
3186 struct ocfs2_xattr_info *xi,
3187 struct ocfs2_xattr_search *xis,
3188 struct ocfs2_xattr_search *xbs,
3189 struct ocfs2_xattr_set_ctxt *ctxt)
3191 int ret = 0, credits, old_found;
3193 if (!xi->xi_value) {
3194 /* Remove existing extended attribute */
3195 if (!xis->not_found)
3196 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3197 else if (!xbs->not_found)
3198 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3200 /* We always try to set extended attribute into inode first*/
3201 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3202 if (!ret && !xbs->not_found) {
3204 * If succeed and that extended attribute existing in
3205 * external block, then we will remove it.
3207 xi->xi_value = NULL;
3208 xi->xi_value_len = 0;
3210 old_found = xis->not_found;
3211 xis->not_found = -ENODATA;
3212 ret = ocfs2_calc_xattr_set_need(inode,
3220 xis->not_found = old_found;
3226 ret = ocfs2_extend_trans(ctxt->handle, credits +
3227 ctxt->handle->h_buffer_credits);
3232 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3233 } else if (ret == -ENOSPC) {
3234 if (di->i_xattr_loc && !xbs->xattr_bh) {
3235 ret = ocfs2_xattr_block_find(inode,
3241 old_found = xis->not_found;
3242 xis->not_found = -ENODATA;
3243 ret = ocfs2_calc_xattr_set_need(inode,
3251 xis->not_found = old_found;
3257 ret = ocfs2_extend_trans(ctxt->handle, credits +
3258 ctxt->handle->h_buffer_credits);
3265 * If no space in inode, we will set extended attribute
3266 * into external block.
3268 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3271 if (!xis->not_found) {
3273 * If succeed and that extended attribute
3274 * existing in inode, we will remove it.
3276 xi->xi_value = NULL;
3277 xi->xi_value_len = 0;
3278 xbs->not_found = -ENODATA;
3279 ret = ocfs2_calc_xattr_set_need(inode,
3292 ret = ocfs2_extend_trans(ctxt->handle, credits +
3293 ctxt->handle->h_buffer_credits);
3298 ret = ocfs2_xattr_ibody_set(inode, xi,
3305 /* Update inode ctime. */
3306 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3308 OCFS2_JOURNAL_ACCESS_WRITE);
3314 inode->i_ctime = CURRENT_TIME;
3315 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3316 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3317 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3324 * This function only called duing creating inode
3325 * for init security/acl xattrs of the new inode.
3326 * All transanction credits have been reserved in mknod.
3328 int ocfs2_xattr_set_handle(handle_t *handle,
3329 struct inode *inode,
3330 struct buffer_head *di_bh,
3336 struct ocfs2_alloc_context *meta_ac,
3337 struct ocfs2_alloc_context *data_ac)
3339 struct ocfs2_dinode *di;
3342 struct ocfs2_xattr_info xi = {
3343 .xi_name_index = name_index,
3345 .xi_name_len = strlen(name),
3347 .xi_value_len = value_len,
3350 struct ocfs2_xattr_search xis = {
3351 .not_found = -ENODATA,
3354 struct ocfs2_xattr_search xbs = {
3355 .not_found = -ENODATA,
3358 struct ocfs2_xattr_set_ctxt ctxt = {
3364 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3368 * In extreme situation, may need xattr bucket when
3369 * block size is too small. And we have already reserved
3370 * the credits for bucket in mknod.
3372 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3373 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3375 mlog_errno(-ENOMEM);
3380 xis.inode_bh = xbs.inode_bh = di_bh;
3381 di = (struct ocfs2_dinode *)di_bh->b_data;
3383 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3385 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3388 if (xis.not_found) {
3389 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3394 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3397 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3398 brelse(xbs.xattr_bh);
3399 ocfs2_xattr_bucket_free(xbs.bucket);
3407 * Set, replace or remove an extended attribute for this inode.
3408 * value is NULL to remove an existing extended attribute, else either
3409 * create or replace an extended attribute.
3411 int ocfs2_xattr_set(struct inode *inode,
3418 struct buffer_head *di_bh = NULL;
3419 struct ocfs2_dinode *di;
3420 int ret, credits, ref_meta = 0, ref_credits = 0;
3421 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3422 struct inode *tl_inode = osb->osb_tl_inode;
3423 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
3424 struct ocfs2_refcount_tree *ref_tree = NULL;
3426 struct ocfs2_xattr_info xi = {
3427 .xi_name_index = name_index,
3429 .xi_name_len = strlen(name),
3431 .xi_value_len = value_len,
3434 struct ocfs2_xattr_search xis = {
3435 .not_found = -ENODATA,
3438 struct ocfs2_xattr_search xbs = {
3439 .not_found = -ENODATA,
3442 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3446 * Only xbs will be used on indexed trees. xis doesn't need a
3449 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3451 mlog_errno(-ENOMEM);
3455 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3458 goto cleanup_nolock;
3460 xis.inode_bh = xbs.inode_bh = di_bh;
3461 di = (struct ocfs2_dinode *)di_bh->b_data;
3463 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3465 * Scan inode and external block to find the same name
3466 * extended attribute and collect search infomation.
3468 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3471 if (xis.not_found) {
3472 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3477 if (xis.not_found && xbs.not_found) {
3479 if (flags & XATTR_REPLACE)
3486 if (flags & XATTR_CREATE)
3490 /* Check whether the value is refcounted and do some prepartion. */
3491 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3492 (!xis.not_found || !xbs.not_found)) {
3493 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3494 &xis, &xbs, &ref_tree,
3495 &ref_meta, &ref_credits);
3502 mutex_lock(&tl_inode->i_mutex);
3504 if (ocfs2_truncate_log_needs_flush(osb)) {
3505 ret = __ocfs2_flush_truncate_log(osb);
3507 mutex_unlock(&tl_inode->i_mutex);
3512 mutex_unlock(&tl_inode->i_mutex);
3514 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3515 &xbs, &ctxt, ref_meta, &credits);
3521 /* we need to update inode's ctime field, so add credit for it. */
3522 credits += OCFS2_INODE_UPDATE_CREDITS;
3523 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3524 if (IS_ERR(ctxt.handle)) {
3525 ret = PTR_ERR(ctxt.handle);
3530 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3532 ocfs2_commit_trans(osb, ctxt.handle);
3535 ocfs2_free_alloc_context(ctxt.data_ac);
3537 ocfs2_free_alloc_context(ctxt.meta_ac);
3538 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3539 ocfs2_schedule_truncate_log_flush(osb, 1);
3540 ocfs2_run_deallocs(osb, &ctxt.dealloc);
3544 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3545 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3546 if (!value && !ret) {
3547 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3551 ocfs2_inode_unlock(inode, 1);
3554 brelse(xbs.xattr_bh);
3555 ocfs2_xattr_bucket_free(xbs.bucket);
3561 * Find the xattr extent rec which may contains name_hash.
3562 * e_cpos will be the first name hash of the xattr rec.
3563 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3565 static int ocfs2_xattr_get_rec(struct inode *inode,
3570 struct ocfs2_extent_list *el)
3573 struct buffer_head *eb_bh = NULL;
3574 struct ocfs2_extent_block *eb;
3575 struct ocfs2_extent_rec *rec = NULL;
3578 if (el->l_tree_depth) {
3579 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3586 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3589 if (el->l_tree_depth) {
3590 ocfs2_error(inode->i_sb,
3591 "Inode %lu has non zero tree depth in "
3592 "xattr tree block %llu\n", inode->i_ino,
3593 (unsigned long long)eb_bh->b_blocknr);
3599 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3600 rec = &el->l_recs[i];
3602 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3603 e_blkno = le64_to_cpu(rec->e_blkno);
3609 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3610 "record (%u, %u, 0) in xattr", inode->i_ino,
3611 le32_to_cpu(rec->e_cpos),
3612 ocfs2_rec_clusters(el, rec));
3617 *p_blkno = le64_to_cpu(rec->e_blkno);
3618 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3620 *e_cpos = le32_to_cpu(rec->e_cpos);
3626 typedef int (xattr_bucket_func)(struct inode *inode,
3627 struct ocfs2_xattr_bucket *bucket,
3630 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3631 struct ocfs2_xattr_bucket *bucket,
3638 int i, ret = 0, cmp = 1, block_off, new_offset;
3639 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3640 size_t name_len = strlen(name);
3641 struct ocfs2_xattr_entry *xe = NULL;
3645 * We don't use binary search in the bucket because there
3646 * may be multiple entries with the same name hash.
3648 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3649 xe = &xh->xh_entries[i];
3651 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3653 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3656 cmp = name_index - ocfs2_xattr_get_type(xe);
3658 cmp = name_len - xe->xe_name_len;
3662 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3673 xe_name = bucket_block(bucket, block_off) + new_offset;
3674 if (!memcmp(name, xe_name, name_len)) {
3686 * Find the specified xattr entry in a series of buckets.
3687 * This series start from p_blkno and last for num_clusters.
3688 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3689 * the num of the valid buckets.
3691 * Return the buffer_head this xattr should reside in. And if the xattr's
3692 * hash is in the gap of 2 buckets, return the lower bucket.
3694 static int ocfs2_xattr_bucket_find(struct inode *inode,
3701 struct ocfs2_xattr_search *xs)
3704 struct ocfs2_xattr_header *xh = NULL;
3705 struct ocfs2_xattr_entry *xe = NULL;
3707 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3708 int low_bucket = 0, bucket, high_bucket;
3709 struct ocfs2_xattr_bucket *search;
3711 u64 blkno, lower_blkno = 0;
3713 search = ocfs2_xattr_bucket_new(inode);
3720 ret = ocfs2_read_xattr_bucket(search, p_blkno);
3726 xh = bucket_xh(search);
3727 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3728 while (low_bucket <= high_bucket) {
3729 ocfs2_xattr_bucket_relse(search);
3731 bucket = (low_bucket + high_bucket) / 2;
3732 blkno = p_blkno + bucket * blk_per_bucket;
3733 ret = ocfs2_read_xattr_bucket(search, blkno);
3739 xh = bucket_xh(search);
3740 xe = &xh->xh_entries[0];
3741 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3742 high_bucket = bucket - 1;
3747 * Check whether the hash of the last entry in our
3748 * bucket is larger than the search one. for an empty
3749 * bucket, the last one is also the first one.
3752 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3754 last_hash = le32_to_cpu(xe->xe_name_hash);
3756 /* record lower_blkno which may be the insert place. */
3757 lower_blkno = blkno;
3759 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3760 low_bucket = bucket + 1;
3764 /* the searched xattr should reside in this bucket if exists. */
3765 ret = ocfs2_find_xe_in_bucket(inode, search,
3766 name_index, name, name_hash,
3776 * Record the bucket we have found.
3777 * When the xattr's hash value is in the gap of 2 buckets, we will
3778 * always set it to the previous bucket.
3781 lower_blkno = p_blkno;
3783 /* This should be in cache - we just read it during the search */
3784 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3790 xs->header = bucket_xh(xs->bucket);
3791 xs->base = bucket_block(xs->bucket, 0);
3792 xs->end = xs->base + inode->i_sb->s_blocksize;
3795 xs->here = &xs->header->xh_entries[index];
3796 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3797 (unsigned long long)bucket_blkno(xs->bucket), index);
3802 ocfs2_xattr_bucket_free(search);
3806 static int ocfs2_xattr_index_block_find(struct inode *inode,
3807 struct buffer_head *root_bh,
3810 struct ocfs2_xattr_search *xs)
3813 struct ocfs2_xattr_block *xb =
3814 (struct ocfs2_xattr_block *)root_bh->b_data;
3815 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3816 struct ocfs2_extent_list *el = &xb_root->xt_list;
3818 u32 first_hash, num_clusters = 0;
3819 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3821 if (le16_to_cpu(el->l_next_free_rec) == 0)
3824 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3825 name, name_hash, name_index);
3827 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3834 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3836 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3837 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3840 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3841 p_blkno, first_hash, num_clusters, xs);
3847 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3850 xattr_bucket_func *func,
3854 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3855 u32 num_buckets = clusters * bpc;
3856 struct ocfs2_xattr_bucket *bucket;
3858 bucket = ocfs2_xattr_bucket_new(inode);
3860 mlog_errno(-ENOMEM);
3864 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3865 clusters, (unsigned long long)blkno);
3867 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3868 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3875 * The real bucket num in this series of blocks is stored
3876 * in the 1st bucket.
3879 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3881 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3882 (unsigned long long)blkno,
3883 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3885 ret = func(inode, bucket, para);
3886 if (ret && ret != -ERANGE)
3888 /* Fall through to bucket_relse() */
3891 ocfs2_xattr_bucket_relse(bucket);
3896 ocfs2_xattr_bucket_free(bucket);
3900 struct ocfs2_xattr_tree_list {
3906 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
3907 struct ocfs2_xattr_header *xh,
3914 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3917 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3919 *block_off = name_offset >> sb->s_blocksize_bits;
3920 *new_offset = name_offset % sb->s_blocksize;
3925 static int ocfs2_list_xattr_bucket(struct inode *inode,
3926 struct ocfs2_xattr_bucket *bucket,
3930 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
3931 int i, block_off, new_offset;
3932 const char *prefix, *name;
3934 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3935 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
3936 type = ocfs2_xattr_get_type(entry);
3937 prefix = ocfs2_xattr_prefix(type);
3940 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3948 name = (const char *)bucket_block(bucket, block_off) +
3950 ret = ocfs2_xattr_list_entry(xl->buffer,
3954 entry->xe_name_len);
3963 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
3964 struct buffer_head *blk_bh,
3965 xattr_tree_rec_func *rec_func,
3968 struct ocfs2_xattr_block *xb =
3969 (struct ocfs2_xattr_block *)blk_bh->b_data;
3970 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
3972 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3975 if (!el->l_next_free_rec || !rec_func)
3978 while (name_hash > 0) {
3979 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3980 &e_cpos, &num_clusters, el);
3986 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
3987 num_clusters, para);
3997 name_hash = e_cpos - 1;
4004 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4005 struct buffer_head *root_bh,
4006 u64 blkno, u32 cpos, u32 len, void *para)
4008 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4009 ocfs2_list_xattr_bucket, para);
4012 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4013 struct buffer_head *blk_bh,
4018 struct ocfs2_xattr_tree_list xl = {
4020 .buffer_size = buffer_size,
4024 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4025 ocfs2_list_xattr_tree_rec, &xl);
4036 static int cmp_xe(const void *a, const void *b)
4038 const struct ocfs2_xattr_entry *l = a, *r = b;
4039 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4040 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4042 if (l_hash > r_hash)
4044 if (l_hash < r_hash)
4049 static void swap_xe(void *a, void *b, int size)
4051 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4054 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4055 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4059 * When the ocfs2_xattr_block is filled up, new bucket will be created
4060 * and all the xattr entries will be moved to the new bucket.
4061 * The header goes at the start of the bucket, and the names+values are
4062 * filled from the end. This is why *target starts as the last buffer.
4063 * Note: we need to sort the entries since they are not saved in order
4064 * in the ocfs2_xattr_block.
4066 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4067 struct buffer_head *xb_bh,
4068 struct ocfs2_xattr_bucket *bucket)
4070 int i, blocksize = inode->i_sb->s_blocksize;
4071 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4072 u16 offset, size, off_change;
4073 struct ocfs2_xattr_entry *xe;
4074 struct ocfs2_xattr_block *xb =
4075 (struct ocfs2_xattr_block *)xb_bh->b_data;
4076 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
4077 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4078 u16 count = le16_to_cpu(xb_xh->xh_count);
4079 char *src = xb_bh->b_data;
4080 char *target = bucket_block(bucket, blks - 1);
4082 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4083 (unsigned long long)xb_bh->b_blocknr,
4084 (unsigned long long)bucket_blkno(bucket));
4086 for (i = 0; i < blks; i++)
4087 memset(bucket_block(bucket, i), 0, blocksize);
4090 * Since the xe_name_offset is based on ocfs2_xattr_header,
4091 * there is a offset change corresponding to the change of
4092 * ocfs2_xattr_header's position.
4094 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4095 xe = &xb_xh->xh_entries[count - 1];
4096 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4097 size = blocksize - offset;
4099 /* copy all the names and values. */
4100 memcpy(target + offset, src + offset, size);
4102 /* Init new header now. */
4103 xh->xh_count = xb_xh->xh_count;
4104 xh->xh_num_buckets = cpu_to_le16(1);
4105 xh->xh_name_value_len = cpu_to_le16(size);
4106 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4108 /* copy all the entries. */
4109 target = bucket_block(bucket, 0);
4110 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4111 size = count * sizeof(struct ocfs2_xattr_entry);
4112 memcpy(target + offset, (char *)xb_xh + offset, size);
4114 /* Change the xe offset for all the xe because of the move. */
4115 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4116 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4117 for (i = 0; i < count; i++)
4118 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4120 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4121 offset, size, off_change);
4123 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4128 * After we move xattr from block to index btree, we have to
4129 * update ocfs2_xattr_search to the new xe and base.
4131 * When the entry is in xattr block, xattr_bh indicates the storage place.
4132 * While if the entry is in index b-tree, "bucket" indicates the
4133 * real place of the xattr.
4135 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4136 struct ocfs2_xattr_search *xs,
4137 struct buffer_head *old_bh)
4139 char *buf = old_bh->b_data;
4140 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4141 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
4144 xs->header = bucket_xh(xs->bucket);
4145 xs->base = bucket_block(xs->bucket, 0);
4146 xs->end = xs->base + inode->i_sb->s_blocksize;
4151 i = xs->here - old_xh->xh_entries;
4152 xs->here = &xs->header->xh_entries[i];
4155 static int ocfs2_xattr_create_index_block(struct inode *inode,
4156 struct ocfs2_xattr_search *xs,
4157 struct ocfs2_xattr_set_ctxt *ctxt)
4162 handle_t *handle = ctxt->handle;
4163 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4164 struct ocfs2_inode_info *oi = OCFS2_I(inode);
4165 struct buffer_head *xb_bh = xs->xattr_bh;
4166 struct ocfs2_xattr_block *xb =
4167 (struct ocfs2_xattr_block *)xb_bh->b_data;
4168 struct ocfs2_xattr_tree_root *xr;
4169 u16 xb_flags = le16_to_cpu(xb->xb_flags);
4171 mlog(0, "create xattr index block for %llu\n",
4172 (unsigned long long)xb_bh->b_blocknr);
4174 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
4175 BUG_ON(!xs->bucket);
4179 * We can use this lock for now, and maybe move to a dedicated mutex
4180 * if performance becomes a problem later.
4182 down_write(&oi->ip_alloc_sem);
4184 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
4185 OCFS2_JOURNAL_ACCESS_WRITE);
4191 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
4192 1, 1, &bit_off, &len);
4199 * The bucket may spread in many blocks, and
4200 * we will only touch the 1st block and the last block
4201 * in the whole bucket(one for entry and one for data).
4203 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4205 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4206 (unsigned long long)blkno);
4208 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
4214 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4215 OCFS2_JOURNAL_ACCESS_CREATE);
4221 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4222 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4224 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4226 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4227 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4228 offsetof(struct ocfs2_xattr_block, xb_attrs));
4230 xr = &xb->xb_attrs.xb_root;
4231 xr->xt_clusters = cpu_to_le32(1);
4232 xr->xt_last_eb_blk = 0;
4233 xr->xt_list.l_tree_depth = 0;
4234 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4235 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4237 xr->xt_list.l_recs[0].e_cpos = 0;
4238 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4239 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4241 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4243 ocfs2_journal_dirty(handle, xb_bh);
4246 up_write(&oi->ip_alloc_sem);
4251 static int cmp_xe_offset(const void *a, const void *b)
4253 const struct ocfs2_xattr_entry *l = a, *r = b;
4254 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4255 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4257 if (l_name_offset < r_name_offset)
4259 if (l_name_offset > r_name_offset)
4265 * defrag a xattr bucket if we find that the bucket has some
4266 * holes beteen name/value pairs.
4267 * We will move all the name/value pairs to the end of the bucket
4268 * so that we can spare some space for insertion.
4270 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
4272 struct ocfs2_xattr_bucket *bucket)
4275 size_t end, offset, len;
4276 struct ocfs2_xattr_header *xh;
4277 char *entries, *buf, *bucket_buf = NULL;
4278 u64 blkno = bucket_blkno(bucket);
4280 size_t blocksize = inode->i_sb->s_blocksize;
4281 struct ocfs2_xattr_entry *xe;
4284 * In order to make the operation more efficient and generic,
4285 * we copy all the blocks into a contiguous memory and do the
4286 * defragment there, so if anything is error, we will not touch
4289 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4296 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4297 memcpy(buf, bucket_block(bucket, i), blocksize);
4299 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4300 OCFS2_JOURNAL_ACCESS_WRITE);
4306 xh = (struct ocfs2_xattr_header *)bucket_buf;
4307 entries = (char *)xh->xh_entries;
4308 xh_free_start = le16_to_cpu(xh->xh_free_start);
4310 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4311 "xh_free_start = %u, xh_name_value_len = %u.\n",
4312 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4313 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4316 * sort all the entries by their offset.
4317 * the largest will be the first, so that we can
4318 * move them to the end one by one.
4320 sort(entries, le16_to_cpu(xh->xh_count),
4321 sizeof(struct ocfs2_xattr_entry),
4322 cmp_xe_offset, swap_xe);
4324 /* Move all name/values to the end of the bucket. */
4325 xe = xh->xh_entries;
4326 end = OCFS2_XATTR_BUCKET_SIZE;
4327 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4328 offset = le16_to_cpu(xe->xe_name_offset);
4329 len = namevalue_size_xe(xe);
4332 * We must make sure that the name/value pair
4333 * exist in the same block. So adjust end to
4334 * the previous block end if needed.
4336 if (((end - len) / blocksize !=
4337 (end - 1) / blocksize))
4338 end = end - end % blocksize;
4340 if (end > offset + len) {
4341 memmove(bucket_buf + end - len,
4342 bucket_buf + offset, len);
4343 xe->xe_name_offset = cpu_to_le16(end - len);
4346 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4347 "bucket %llu\n", (unsigned long long)blkno);
4352 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4353 "bucket %llu\n", (unsigned long long)blkno);
4355 if (xh_free_start == end)
4358 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4359 xh->xh_free_start = cpu_to_le16(end);
4361 /* sort the entries by their name_hash. */
4362 sort(entries, le16_to_cpu(xh->xh_count),
4363 sizeof(struct ocfs2_xattr_entry),
4367 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4368 memcpy(bucket_block(bucket, i), buf, blocksize);
4369 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4377 * prev_blkno points to the start of an existing extent. new_blkno
4378 * points to a newly allocated extent. Because we know each of our
4379 * clusters contains more than bucket, we can easily split one cluster
4380 * at a bucket boundary. So we take the last cluster of the existing
4381 * extent and split it down the middle. We move the last half of the
4382 * buckets in the last cluster of the existing extent over to the new
4385 * first_bh is the buffer at prev_blkno so we can update the existing
4386 * extent's bucket count. header_bh is the bucket were we were hoping
4387 * to insert our xattr. If the bucket move places the target in the new
4388 * extent, we'll update first_bh and header_bh after modifying the old
4391 * first_hash will be set as the 1st xe's name_hash in the new extent.
4393 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4395 struct ocfs2_xattr_bucket *first,
4396 struct ocfs2_xattr_bucket *target,
4402 struct super_block *sb = inode->i_sb;
4403 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4404 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4405 int to_move = num_buckets / 2;
4407 u64 last_cluster_blkno = bucket_blkno(first) +
4408 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4410 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4411 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4413 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4414 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
4416 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4417 last_cluster_blkno, new_blkno,
4418 to_move, first_hash);
4424 /* This is the first bucket that got moved */
4425 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4428 * If the target bucket was part of the moved buckets, we need to
4429 * update first and target.
4431 if (bucket_blkno(target) >= src_blkno) {
4432 /* Find the block for the new target bucket */
4433 src_blkno = new_blkno +
4434 (bucket_blkno(target) - src_blkno);
4436 ocfs2_xattr_bucket_relse(first);
4437 ocfs2_xattr_bucket_relse(target);
4440 * These shouldn't fail - the buffers are in the
4441 * journal from ocfs2_cp_xattr_bucket().
4443 ret = ocfs2_read_xattr_bucket(first, new_blkno);
4448 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4459 * Find the suitable pos when we divide a bucket into 2.
4460 * We have to make sure the xattrs with the same hash value exist
4461 * in the same bucket.
4463 * If this ocfs2_xattr_header covers more than one hash value, find a
4464 * place where the hash value changes. Try to find the most even split.
4465 * The most common case is that all entries have different hash values,
4466 * and the first check we make will find a place to split.
4468 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4470 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4471 int count = le16_to_cpu(xh->xh_count);
4472 int delta, middle = count / 2;
4475 * We start at the middle. Each step gets farther away in both
4476 * directions. We therefore hit the change in hash value
4477 * nearest to the middle. Note that this loop does not execute for
4480 for (delta = 0; delta < middle; delta++) {
4481 /* Let's check delta earlier than middle */
4482 if (cmp_xe(&entries[middle - delta - 1],
4483 &entries[middle - delta]))
4484 return middle - delta;
4486 /* For even counts, don't walk off the end */
4487 if ((middle + delta + 1) == count)
4490 /* Now try delta past middle */
4491 if (cmp_xe(&entries[middle + delta],
4492 &entries[middle + delta + 1]))
4493 return middle + delta + 1;
4496 /* Every entry had the same hash */
4501 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4502 * first_hash will record the 1st hash of the new bucket.
4504 * Normally half of the xattrs will be moved. But we have to make
4505 * sure that the xattrs with the same hash value are stored in the
4506 * same bucket. If all the xattrs in this bucket have the same hash
4507 * value, the new bucket will be initialized as an empty one and the
4508 * first_hash will be initialized as (hash_value+1).
4510 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4515 int new_bucket_head)
4518 int count, start, len, name_value_len = 0, name_offset = 0;
4519 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4520 struct ocfs2_xattr_header *xh;
4521 struct ocfs2_xattr_entry *xe;
4522 int blocksize = inode->i_sb->s_blocksize;
4524 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4525 (unsigned long long)blk, (unsigned long long)new_blk);
4527 s_bucket = ocfs2_xattr_bucket_new(inode);
4528 t_bucket = ocfs2_xattr_bucket_new(inode);
4529 if (!s_bucket || !t_bucket) {
4535 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4541 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4542 OCFS2_JOURNAL_ACCESS_WRITE);
4549 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4550 * there's no need to read it.
4552 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4559 * Hey, if we're overwriting t_bucket, what difference does
4560 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4561 * same part of ocfs2_cp_xattr_bucket().
4563 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4565 OCFS2_JOURNAL_ACCESS_CREATE :
4566 OCFS2_JOURNAL_ACCESS_WRITE);
4572 xh = bucket_xh(s_bucket);
4573 count = le16_to_cpu(xh->xh_count);
4574 start = ocfs2_xattr_find_divide_pos(xh);
4576 if (start == count) {
4577 xe = &xh->xh_entries[start-1];
4580 * initialized a new empty bucket here.
4581 * The hash value is set as one larger than
4582 * that of the last entry in the previous bucket.
4584 for (i = 0; i < t_bucket->bu_blocks; i++)
4585 memset(bucket_block(t_bucket, i), 0, blocksize);
4587 xh = bucket_xh(t_bucket);
4588 xh->xh_free_start = cpu_to_le16(blocksize);
4589 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4590 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4592 goto set_num_buckets;
4595 /* copy the whole bucket to the new first. */
4596 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4598 /* update the new bucket. */
4599 xh = bucket_xh(t_bucket);
4602 * Calculate the total name/value len and xh_free_start for
4603 * the old bucket first.
4605 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4607 for (i = 0; i < start; i++) {
4608 xe = &xh->xh_entries[i];
4609 name_value_len += namevalue_size_xe(xe);
4610 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4611 name_offset = le16_to_cpu(xe->xe_name_offset);
4615 * Now begin the modification to the new bucket.
4617 * In the new bucket, We just move the xattr entry to the beginning
4618 * and don't touch the name/value. So there will be some holes in the
4619 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4622 xe = &xh->xh_entries[start];
4623 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4624 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
4625 (int)((char *)xe - (char *)xh),
4626 (int)((char *)xh->xh_entries - (char *)xh));
4627 memmove((char *)xh->xh_entries, (char *)xe, len);
4628 xe = &xh->xh_entries[count - start];
4629 len = sizeof(struct ocfs2_xattr_entry) * start;
4630 memset((char *)xe, 0, len);
4632 le16_add_cpu(&xh->xh_count, -start);
4633 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4635 /* Calculate xh_free_start for the new bucket. */
4636 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4637 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4638 xe = &xh->xh_entries[i];
4639 if (le16_to_cpu(xe->xe_name_offset) <
4640 le16_to_cpu(xh->xh_free_start))
4641 xh->xh_free_start = xe->xe_name_offset;
4645 /* set xh->xh_num_buckets for the new xh. */
4646 if (new_bucket_head)
4647 xh->xh_num_buckets = cpu_to_le16(1);
4649 xh->xh_num_buckets = 0;
4651 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4653 /* store the first_hash of the new bucket. */
4655 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4658 * Now only update the 1st block of the old bucket. If we
4659 * just added a new empty bucket, there is no need to modify
4665 xh = bucket_xh(s_bucket);
4666 memset(&xh->xh_entries[start], 0,
4667 sizeof(struct ocfs2_xattr_entry) * (count - start));
4668 xh->xh_count = cpu_to_le16(start);
4669 xh->xh_free_start = cpu_to_le16(name_offset);
4670 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4672 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4675 ocfs2_xattr_bucket_free(s_bucket);
4676 ocfs2_xattr_bucket_free(t_bucket);
4682 * Copy xattr from one bucket to another bucket.
4684 * The caller must make sure that the journal transaction
4685 * has enough space for journaling.
4687 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4694 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4696 BUG_ON(s_blkno == t_blkno);
4698 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4699 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4702 s_bucket = ocfs2_xattr_bucket_new(inode);
4703 t_bucket = ocfs2_xattr_bucket_new(inode);
4704 if (!s_bucket || !t_bucket) {
4710 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4715 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4716 * there's no need to read it.
4718 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4723 * Hey, if we're overwriting t_bucket, what difference does
4724 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4725 * cluster to fill, we came here from
4726 * ocfs2_mv_xattr_buckets(), and it is really new -
4727 * ACCESS_CREATE is required. But we also might have moved data
4728 * out of t_bucket before extending back into it.
4729 * ocfs2_add_new_xattr_bucket() can do this - its call to
4730 * ocfs2_add_new_xattr_cluster() may have created a new extent
4731 * and copied out the end of the old extent. Then it re-extends
4732 * the old extent back to create space for new xattrs. That's
4733 * how we get here, and the bucket isn't really new.
4735 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4737 OCFS2_JOURNAL_ACCESS_CREATE :
4738 OCFS2_JOURNAL_ACCESS_WRITE);
4742 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4743 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4746 ocfs2_xattr_bucket_free(t_bucket);
4747 ocfs2_xattr_bucket_free(s_bucket);
4753 * src_blk points to the start of an existing extent. last_blk points to
4754 * last cluster in that extent. to_blk points to a newly allocated
4755 * extent. We copy the buckets from the cluster at last_blk to the new
4756 * extent. If start_bucket is non-zero, we skip that many buckets before
4757 * we start copying. The new extent's xh_num_buckets gets set to the
4758 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4759 * by the same amount.
4761 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4762 u64 src_blk, u64 last_blk, u64 to_blk,
4763 unsigned int start_bucket,
4766 int i, ret, credits;
4767 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4768 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4769 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4770 struct ocfs2_xattr_bucket *old_first, *new_first;
4772 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4773 (unsigned long long)last_blk, (unsigned long long)to_blk);
4775 BUG_ON(start_bucket >= num_buckets);
4777 num_buckets -= start_bucket;
4778 last_blk += (start_bucket * blks_per_bucket);
4781 /* The first bucket of the original extent */
4782 old_first = ocfs2_xattr_bucket_new(inode);
4783 /* The first bucket of the new extent */
4784 new_first = ocfs2_xattr_bucket_new(inode);
4785 if (!old_first || !new_first) {
4791 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4798 * We need to update the first bucket of the old extent and all
4799 * the buckets going to the new extent.
4801 credits = ((num_buckets + 1) * blks_per_bucket) +
4802 handle->h_buffer_credits;
4803 ret = ocfs2_extend_trans(handle, credits);
4809 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4810 OCFS2_JOURNAL_ACCESS_WRITE);
4816 for (i = 0; i < num_buckets; i++) {
4817 ret = ocfs2_cp_xattr_bucket(inode, handle,
4818 last_blk + (i * blks_per_bucket),
4819 to_blk + (i * blks_per_bucket),
4828 * Get the new bucket ready before we dirty anything
4829 * (This actually shouldn't fail, because we already dirtied
4830 * it once in ocfs2_cp_xattr_bucket()).
4832 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4837 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4838 OCFS2_JOURNAL_ACCESS_WRITE);
4844 /* Now update the headers */
4845 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4846 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4848 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4849 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4852 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4855 ocfs2_xattr_bucket_free(new_first);
4856 ocfs2_xattr_bucket_free(old_first);
4861 * Move some xattrs in this cluster to the new cluster.
4862 * This function should only be called when bucket size == cluster size.
4863 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4865 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4871 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4872 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
4874 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4876 ret = ocfs2_extend_trans(handle, credits);
4882 /* Move half of the xattr in start_blk to the next bucket. */
4883 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4884 new_blk, first_hash, 1);
4888 * Move some xattrs from the old cluster to the new one since they are not
4889 * contiguous in ocfs2 xattr tree.
4891 * new_blk starts a new separate cluster, and we will move some xattrs from
4892 * prev_blk to it. v_start will be set as the first name hash value in this
4893 * new cluster so that it can be used as e_cpos during tree insertion and
4894 * don't collide with our original b-tree operations. first_bh and header_bh
4895 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4896 * to extend the insert bucket.
4898 * The problem is how much xattr should we move to the new one and when should
4899 * we update first_bh and header_bh?
4900 * 1. If cluster size > bucket size, that means the previous cluster has more
4901 * than 1 bucket, so just move half nums of bucket into the new cluster and
4902 * update the first_bh and header_bh if the insert bucket has been moved
4903 * to the new cluster.
4904 * 2. If cluster_size == bucket_size:
4905 * a) If the previous extent rec has more than one cluster and the insert
4906 * place isn't in the last cluster, copy the entire last cluster to the
4907 * new one. This time, we don't need to upate the first_bh and header_bh
4908 * since they will not be moved into the new cluster.
4909 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4910 * the new one. And we set the extend flag to zero if the insert place is
4911 * moved into the new allocated cluster since no extend is needed.
4913 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4915 struct ocfs2_xattr_bucket *first,
4916 struct ocfs2_xattr_bucket *target,
4924 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4925 (unsigned long long)bucket_blkno(first), prev_clusters,
4926 (unsigned long long)new_blk);
4928 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
4929 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4938 /* The start of the last cluster in the first extent */
4939 u64 last_blk = bucket_blkno(first) +
4940 ((prev_clusters - 1) *
4941 ocfs2_clusters_to_blocks(inode->i_sb, 1));
4943 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
4944 ret = ocfs2_mv_xattr_buckets(inode, handle,
4945 bucket_blkno(first),
4946 last_blk, new_blk, 0,
4951 ret = ocfs2_divide_xattr_cluster(inode, handle,
4957 if ((bucket_blkno(target) == last_blk) && extend)
4966 * Add a new cluster for xattr storage.
4968 * If the new cluster is contiguous with the previous one, it will be
4969 * appended to the same extent record, and num_clusters will be updated.
4970 * If not, we will insert a new extent for it and move some xattrs in
4971 * the last cluster into the new allocated one.
4972 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4973 * lose the benefits of hashing because we'll have to search large leaves.
4974 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4977 * first_bh is the first block of the previous extent rec and header_bh
4978 * indicates the bucket we will insert the new xattrs. They will be updated
4979 * when the header_bh is moved into the new cluster.
4981 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4982 struct buffer_head *root_bh,
4983 struct ocfs2_xattr_bucket *first,
4984 struct ocfs2_xattr_bucket *target,
4988 struct ocfs2_xattr_set_ctxt *ctxt)
4991 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4992 u32 prev_clusters = *num_clusters;
4993 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4995 handle_t *handle = ctxt->handle;
4996 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4997 struct ocfs2_extent_tree et;
4999 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5000 "previous xattr blkno = %llu\n",
5001 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5002 prev_cpos, (unsigned long long)bucket_blkno(first));
5004 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5006 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5007 OCFS2_JOURNAL_ACCESS_WRITE);
5013 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
5014 clusters_to_add, &bit_off, &num_bits);
5021 BUG_ON(num_bits > clusters_to_add);
5023 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5024 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5025 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5027 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
5028 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5029 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5031 * If this cluster is contiguous with the old one and
5032 * adding this new cluster, we don't surpass the limit of
5033 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5034 * initialized and used like other buckets in the previous
5036 * So add it as a contiguous one. The caller will handle
5039 v_start = prev_cpos + prev_clusters;
5040 *num_clusters = prev_clusters + num_bits;
5041 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5044 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5058 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
5059 num_bits, (unsigned long long)block, v_start);
5060 ret = ocfs2_insert_extent(handle, &et, v_start, block,
5061 num_bits, 0, ctxt->meta_ac);
5067 ret = ocfs2_journal_dirty(handle, root_bh);
5076 * We are given an extent. 'first' is the bucket at the very front of
5077 * the extent. The extent has space for an additional bucket past
5078 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5079 * of the target bucket. We wish to shift every bucket past the target
5080 * down one, filling in that additional space. When we get back to the
5081 * target, we split the target between itself and the now-empty bucket
5082 * at target+1 (aka, target_blkno + blks_per_bucket).
5084 static int ocfs2_extend_xattr_bucket(struct inode *inode,
5086 struct ocfs2_xattr_bucket *first,
5091 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5092 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5094 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
5096 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
5097 "from %llu, len = %u\n", (unsigned long long)target_blk,
5098 (unsigned long long)bucket_blkno(first), num_clusters);
5100 /* The extent must have room for an additional bucket */
5101 BUG_ON(new_bucket >=
5102 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
5104 /* end_blk points to the last existing bucket */
5105 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
5108 * end_blk is the start of the last existing bucket.
5109 * Thus, (end_blk - target_blk) covers the target bucket and
5110 * every bucket after it up to, but not including, the last
5111 * existing bucket. Then we add the last existing bucket, the
5112 * new bucket, and the first bucket (3 * blk_per_bucket).
5114 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
5115 handle->h_buffer_credits;
5116 ret = ocfs2_extend_trans(handle, credits);
5122 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5123 OCFS2_JOURNAL_ACCESS_WRITE);
5129 while (end_blk != target_blk) {
5130 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5131 end_blk + blk_per_bucket, 0);
5134 end_blk -= blk_per_bucket;
5137 /* Move half of the xattr in target_blkno to the next bucket. */
5138 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5139 target_blk + blk_per_bucket, NULL, 0);
5141 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5142 ocfs2_xattr_bucket_journal_dirty(handle, first);
5149 * Add new xattr bucket in an extent record and adjust the buckets
5150 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5151 * bucket we want to insert into.
5153 * In the easy case, we will move all the buckets after target down by
5154 * one. Half of target's xattrs will be moved to the next bucket.
5156 * If current cluster is full, we'll allocate a new one. This may not
5157 * be contiguous. The underlying calls will make sure that there is
5158 * space for the insert, shifting buckets around if necessary.
5159 * 'target' may be moved by those calls.
5161 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5162 struct buffer_head *xb_bh,
5163 struct ocfs2_xattr_bucket *target,
5164 struct ocfs2_xattr_set_ctxt *ctxt)
5166 struct ocfs2_xattr_block *xb =
5167 (struct ocfs2_xattr_block *)xb_bh->b_data;
5168 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5169 struct ocfs2_extent_list *el = &xb_root->xt_list;
5171 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
5172 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5173 int ret, num_buckets, extend = 1;
5175 u32 e_cpos, num_clusters;
5176 /* The bucket at the front of the extent */
5177 struct ocfs2_xattr_bucket *first;
5179 mlog(0, "Add new xattr bucket starting from %llu\n",
5180 (unsigned long long)bucket_blkno(target));
5182 /* The first bucket of the original extent */
5183 first = ocfs2_xattr_bucket_new(inode);
5190 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5197 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5203 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5204 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5206 * This can move first+target if the target bucket moves
5207 * to the new extent.
5209 ret = ocfs2_add_new_xattr_cluster(inode,
5224 ret = ocfs2_extend_xattr_bucket(inode,
5227 bucket_blkno(target),
5234 ocfs2_xattr_bucket_free(first);
5239 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5240 struct ocfs2_xattr_bucket *bucket,
5243 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5245 offs = offs % inode->i_sb->s_blocksize;
5246 return bucket_block(bucket, block_off) + offs;
5250 * Set the xattr entry in the specified bucket.
5251 * The bucket is indicated by xs->bucket and it should have the enough
5252 * space for the xattr insertion.
5254 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
5256 struct ocfs2_xattr_info *xi,
5257 struct ocfs2_xattr_search *xs,
5263 struct ocfs2_xa_loc loc;
5265 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
5266 (unsigned long)xi->xi_value_len, xi->xi_name_index,
5267 (unsigned long long)bucket_blkno(xs->bucket));
5269 if (!xs->bucket->bu_bhs[1]) {
5270 blkno = bucket_blkno(xs->bucket);
5271 ocfs2_xattr_bucket_relse(xs->bucket);
5272 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
5279 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
5280 OCFS2_JOURNAL_ACCESS_WRITE);
5286 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5287 xs->not_found ? NULL : xs->here);
5288 ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash);
5294 /* XXX For now, until we make ocfs2_xa_prepare_entry() primary */
5295 BUG_ON(ret == -ENOSPC);
5296 ocfs2_xa_store_inline_value(&loc, xi);
5297 xs->here = loc.xl_entry;
5299 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
5306 * Truncate the specified xe_off entry in xattr bucket.
5307 * bucket is indicated by header_bh and len is the new length.
5308 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5310 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5312 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5313 struct ocfs2_xattr_bucket *bucket,
5316 struct ocfs2_xattr_set_ctxt *ctxt)
5320 struct ocfs2_xattr_entry *xe;
5321 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5322 size_t blocksize = inode->i_sb->s_blocksize;
5323 struct ocfs2_xattr_value_buf vb = {
5324 .vb_access = ocfs2_journal_access,
5327 xe = &xh->xh_entries[xe_off];
5329 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5331 offset = le16_to_cpu(xe->xe_name_offset) +
5332 OCFS2_XATTR_SIZE(xe->xe_name_len);
5334 value_blk = offset / blocksize;
5336 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5337 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
5339 vb.vb_bh = bucket->bu_bhs[value_blk];
5342 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5343 (vb.vb_bh->b_data + offset % blocksize);
5346 * From here on out we have to dirty the bucket. The generic
5347 * value calls only modify one of the bucket's bhs, but we need
5348 * to send the bucket at once. So if they error, they *could* have
5349 * modified something. We have to assume they did, and dirty
5350 * the whole bucket. This leaves us in a consistent state.
5352 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5353 xe_off, (unsigned long long)bucket_blkno(bucket), len);
5354 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5360 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5361 OCFS2_JOURNAL_ACCESS_WRITE);
5367 xe->xe_value_size = cpu_to_le64(len);
5369 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5375 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
5376 struct ocfs2_xattr_search *xs,
5378 struct ocfs2_xattr_set_ctxt *ctxt)
5381 struct ocfs2_xattr_entry *xe = xs->here;
5382 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
5384 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
5386 offset = xe - xh->xh_entries;
5387 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
5395 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
5397 struct ocfs2_xattr_search *xs,
5401 int ret, offset, block_off;
5402 struct ocfs2_xattr_value_root *xv;
5403 struct ocfs2_xattr_entry *xe = xs->here;
5404 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5406 struct ocfs2_xattr_value_buf vb = {
5407 .vb_access = ocfs2_journal_access,
5410 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
5412 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
5413 xe - xh->xh_entries,
5421 base = bucket_block(xs->bucket, block_off);
5422 xv = (struct ocfs2_xattr_value_root *)(base + offset +
5423 OCFS2_XATTR_SIZE(xe->xe_name_len));
5426 vb.vb_bh = xs->bucket->bu_bhs[block_off];
5427 ret = __ocfs2_xattr_set_value_outside(inode, handle,
5428 &vb, val, value_len);
5435 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5436 struct buffer_head *root_bh,
5443 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5444 struct inode *tl_inode = osb->osb_tl_inode;
5446 struct ocfs2_xattr_block *xb =
5447 (struct ocfs2_xattr_block *)root_bh->b_data;
5448 struct ocfs2_alloc_context *meta_ac = NULL;
5449 struct ocfs2_cached_dealloc_ctxt dealloc;
5450 struct ocfs2_extent_tree et;
5452 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5453 ocfs2_delete_xattr_in_bucket, para);
5459 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5461 ocfs2_init_dealloc_ctxt(&dealloc);
5463 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5464 cpos, len, (unsigned long long)blkno);
5466 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5469 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5475 mutex_lock(&tl_inode->i_mutex);
5477 if (ocfs2_truncate_log_needs_flush(osb)) {
5478 ret = __ocfs2_flush_truncate_log(osb);
5485 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5486 if (IS_ERR(handle)) {
5492 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5493 OCFS2_JOURNAL_ACCESS_WRITE);
5499 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5506 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5508 ret = ocfs2_journal_dirty(handle, root_bh);
5514 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5519 ocfs2_commit_trans(osb, handle);
5521 ocfs2_schedule_truncate_log_flush(osb, 1);
5523 mutex_unlock(&tl_inode->i_mutex);
5526 ocfs2_free_alloc_context(meta_ac);
5528 ocfs2_run_deallocs(osb, &dealloc);
5533 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
5535 struct ocfs2_xattr_search *xs)
5537 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5538 struct ocfs2_xattr_entry *last = &xh->xh_entries[
5539 le16_to_cpu(xh->xh_count) - 1];
5542 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
5543 OCFS2_JOURNAL_ACCESS_WRITE);
5549 /* Remove the old entry. */
5550 memmove(xs->here, xs->here + 1,
5551 (void *)last - (void *)xs->here);
5552 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
5553 le16_add_cpu(&xh->xh_count, -1);
5555 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
5559 * Set the xattr name/value in the bucket specified in xs.
5561 * As the new value in xi may be stored in the bucket or in an outside cluster,
5562 * we divide the whole process into 3 steps:
5563 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
5564 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
5565 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
5566 * 4. If the clusters for the new outside value can't be allocated, we need
5567 * to free the xattr we allocated in set.
5569 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5570 struct ocfs2_xattr_info *xi,
5571 struct ocfs2_xattr_search *xs,
5572 struct ocfs2_xattr_set_ctxt *ctxt)
5576 char *val = (char *)xi->xi_value;
5577 struct ocfs2_xattr_entry *xe = xs->here;
5578 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
5581 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
5583 * We need to truncate the xattr storage first.
5585 * If both the old and new value are stored to
5586 * outside block, we only need to truncate
5587 * the storage and then set the value outside.
5589 * If the new value should be stored within block,
5590 * we should free all the outside block first and
5591 * the modification to the xattr block will be done
5592 * by following steps.
5594 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
5595 value_len = xi->xi_value_len;
5599 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5606 goto set_value_outside;
5609 value_len = xi->xi_value_len;
5610 /* So we have to handle the inside block change now. */
5611 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
5613 * If the new value will be stored outside of block,
5614 * initalize a new empty value root and insert it first.
5617 xi->xi_value = &def_xv;
5618 xi->xi_value_len = OCFS2_XATTR_ROOT_SIZE;
5621 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
5628 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
5631 /* allocate the space now for the outside block storage. */
5632 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5637 if (xs->not_found) {
5639 * We can't allocate enough clusters for outside
5640 * storage and we have allocated xattr already,
5641 * so need to remove it.
5643 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
5649 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
5650 xs, val, value_len);
5656 * check whether the xattr bucket is filled up with the same hash value.
5657 * If we want to insert the xattr with the same hash, return -ENOSPC.
5658 * If we want to insert a xattr with different hash value, go ahead
5659 * and ocfs2_divide_xattr_bucket will handle this.
5661 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5662 struct ocfs2_xattr_bucket *bucket,
5665 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5666 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5668 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5671 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5672 xh->xh_entries[0].xe_name_hash) {
5673 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5675 (unsigned long long)bucket_blkno(bucket),
5676 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5683 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5684 struct ocfs2_xattr_info *xi,
5685 struct ocfs2_xattr_search *xs,
5686 struct ocfs2_xattr_set_ctxt *ctxt)
5688 struct ocfs2_xattr_header *xh;
5689 struct ocfs2_xattr_entry *xe;
5690 u16 count, header_size, xh_free_start;
5691 int free, max_free, need, old;
5692 size_t value_size = 0;
5693 size_t blocksize = inode->i_sb->s_blocksize;
5694 int ret, allocation = 0;
5696 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
5700 count = le16_to_cpu(xh->xh_count);
5701 xh_free_start = le16_to_cpu(xh->xh_free_start);
5702 header_size = sizeof(struct ocfs2_xattr_header) +
5703 count * sizeof(struct ocfs2_xattr_entry);
5704 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5705 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
5707 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5708 "of %u which exceed block size\n",
5709 (unsigned long long)bucket_blkno(xs->bucket),
5712 if (xi->xi_value && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
5713 value_size = OCFS2_XATTR_ROOT_SIZE;
5714 else if (xi->xi_value)
5715 value_size = OCFS2_XATTR_SIZE(xi->xi_value_len);
5718 need = sizeof(struct ocfs2_xattr_entry) +
5719 OCFS2_XATTR_SIZE(xi->xi_name_len) + value_size;
5721 need = value_size + OCFS2_XATTR_SIZE(xi->xi_name_len);
5724 * We only replace the old value if the new length is smaller
5725 * than the old one. Otherwise we will allocate new space in the
5726 * bucket to store it.
5729 if (ocfs2_xattr_is_local(xe))
5730 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5732 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5734 if (old >= value_size)
5738 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
5740 * We need to make sure the new name/value pair
5741 * can exist in the same block.
5743 if (xh_free_start % blocksize < need)
5744 free -= xh_free_start % blocksize;
5746 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5747 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5748 " %u\n", xs->not_found,
5749 (unsigned long long)bucket_blkno(xs->bucket),
5750 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5751 le16_to_cpu(xh->xh_name_value_len));
5755 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
5756 if (need <= max_free &&
5757 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5759 * We can create the space by defragment. Since only the
5760 * name/value will be moved, the xe shouldn't be changed
5763 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5770 xh_free_start = le16_to_cpu(xh->xh_free_start);
5771 free = xh_free_start - header_size
5772 - OCFS2_XATTR_HEADER_GAP;
5773 if (xh_free_start % blocksize < need)
5774 free -= xh_free_start % blocksize;
5779 mlog(0, "Can't get enough space for xattr insert by "
5780 "defragment. Need %u bytes, but we have %d, so "
5781 "allocate new bucket for it.\n", need, free);
5785 * We have to add new buckets or clusters and one
5786 * allocation should leave us enough space for insert.
5791 * We do not allow for overlapping ranges between buckets. And
5792 * the maximum number of collisions we will allow for then is
5793 * one bucket's worth, so check it here whether we need to
5794 * add a new bucket for the insert.
5796 ret = ocfs2_check_xattr_bucket_collision(inode,
5804 ret = ocfs2_add_new_xattr_bucket(inode,
5814 * ocfs2_add_new_xattr_bucket() will have updated
5815 * xs->bucket if it moved, but it will not have updated
5816 * any of the other search fields. Thus, we drop it and
5817 * re-search. Everything should be cached, so it'll be
5820 ocfs2_xattr_bucket_relse(xs->bucket);
5821 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5824 if (ret && ret != -ENODATA)
5826 xs->not_found = ret;
5832 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
5838 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5839 struct ocfs2_xattr_bucket *bucket,
5842 int ret = 0, ref_credits;
5843 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5845 struct ocfs2_xattr_entry *xe;
5846 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5847 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5848 int credits = ocfs2_remove_extent_credits(osb->sb) +
5849 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5850 struct ocfs2_xattr_value_root *xv;
5851 struct ocfs2_rm_xattr_bucket_para *args =
5852 (struct ocfs2_rm_xattr_bucket_para *)para;
5854 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5856 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5857 xe = &xh->xh_entries[i];
5858 if (ocfs2_xattr_is_local(xe))
5861 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5864 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5870 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5871 if (IS_ERR(ctxt.handle)) {
5872 ret = PTR_ERR(ctxt.handle);
5877 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5880 ocfs2_commit_trans(osb, ctxt.handle);
5882 ocfs2_free_alloc_context(ctxt.meta_ac);
5883 ctxt.meta_ac = NULL;
5892 ocfs2_free_alloc_context(ctxt.meta_ac);
5893 ocfs2_schedule_truncate_log_flush(osb, 1);
5894 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5899 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5900 * or change the extent record flag), we need to recalculate
5901 * the metaecc for the whole bucket. So it is done here.
5904 * We have to give the extra credits for the caller.
5906 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5911 struct ocfs2_xattr_bucket *bucket =
5912 (struct ocfs2_xattr_bucket *)para;
5914 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5915 OCFS2_JOURNAL_ACCESS_WRITE);
5921 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5927 * Special action we need if the xattr value is refcounted.
5929 * 1. If the xattr is refcounted, lock the tree.
5930 * 2. CoW the xattr if we are setting the new value and the value
5931 * will be stored outside.
5932 * 3. In other case, decrease_refcount will work for us, so just
5933 * lock the refcount tree, calculate the meta and credits is OK.
5935 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5936 * currently CoW is a completed transaction, while this function
5937 * will also lock the allocators and let us deadlock. So we will
5938 * CoW the whole xattr value.
5940 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5941 struct ocfs2_dinode *di,
5942 struct ocfs2_xattr_info *xi,
5943 struct ocfs2_xattr_search *xis,
5944 struct ocfs2_xattr_search *xbs,
5945 struct ocfs2_refcount_tree **ref_tree,
5950 struct ocfs2_xattr_block *xb;
5951 struct ocfs2_xattr_entry *xe;
5953 u32 p_cluster, num_clusters;
5954 unsigned int ext_flags;
5955 int name_offset, name_len;
5956 struct ocfs2_xattr_value_buf vb;
5957 struct ocfs2_xattr_bucket *bucket = NULL;
5958 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5959 struct ocfs2_post_refcount refcount;
5960 struct ocfs2_post_refcount *p = NULL;
5961 struct buffer_head *ref_root_bh = NULL;
5963 if (!xis->not_found) {
5965 name_offset = le16_to_cpu(xe->xe_name_offset);
5966 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5968 vb.vb_bh = xis->inode_bh;
5969 vb.vb_access = ocfs2_journal_access_di;
5971 int i, block_off = 0;
5972 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5974 name_offset = le16_to_cpu(xe->xe_name_offset);
5975 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5976 i = xbs->here - xbs->header->xh_entries;
5978 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5979 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5980 bucket_xh(xbs->bucket),
5987 base = bucket_block(xbs->bucket, block_off);
5988 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5989 vb.vb_access = ocfs2_journal_access;
5991 if (ocfs2_meta_ecc(osb)) {
5992 /*create parameters for ocfs2_post_refcount. */
5993 bucket = xbs->bucket;
5994 refcount.credits = bucket->bu_blocks;
5995 refcount.para = bucket;
5997 ocfs2_xattr_bucket_post_refcount;
6002 vb.vb_bh = xbs->xattr_bh;
6003 vb.vb_access = ocfs2_journal_access_xb;
6007 if (ocfs2_xattr_is_local(xe))
6010 vb.vb_xv = (struct ocfs2_xattr_value_root *)
6011 (base + name_offset + name_len);
6013 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
6014 &num_clusters, &vb.vb_xv->xr_list,
6022 * We just need to check the 1st extent record, since we always
6023 * CoW the whole xattr. So there shouldn't be a xattr with
6024 * some REFCOUNT extent recs after the 1st one.
6026 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
6029 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
6030 1, ref_tree, &ref_root_bh);
6037 * If we are deleting the xattr or the new size will be stored inside,
6038 * cool, leave it there, the xattr truncate process will remove them
6039 * for us(it still needs the refcount tree lock and the meta, credits).
6040 * And the worse case is that every cluster truncate will split the
6041 * refcount tree, and make the original extent become 3. So we will need
6042 * 2 * cluster more extent recs at most.
6044 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
6046 ret = ocfs2_refcounted_xattr_delete_need(inode,
6047 &(*ref_tree)->rf_ci,
6048 ref_root_bh, vb.vb_xv,
6055 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
6056 *ref_tree, ref_root_bh, 0,
6057 le32_to_cpu(vb.vb_xv->xr_clusters), p);
6062 brelse(ref_root_bh);
6067 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
6068 * The physical clusters will be added to refcount tree.
6070 static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
6071 struct ocfs2_xattr_value_root *xv,
6072 struct ocfs2_extent_tree *value_et,
6073 struct ocfs2_caching_info *ref_ci,
6074 struct buffer_head *ref_root_bh,
6075 struct ocfs2_cached_dealloc_ctxt *dealloc,
6076 struct ocfs2_post_refcount *refcount)
6079 u32 clusters = le32_to_cpu(xv->xr_clusters);
6080 u32 cpos, p_cluster, num_clusters;
6081 struct ocfs2_extent_list *el = &xv->xr_list;
6082 unsigned int ext_flags;
6085 while (cpos < clusters) {
6086 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
6087 &num_clusters, el, &ext_flags);
6089 cpos += num_clusters;
6090 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
6095 ret = ocfs2_add_refcount_flag(inode, value_et,
6096 ref_ci, ref_root_bh,
6097 cpos - num_clusters,
6098 p_cluster, num_clusters,
6110 * Given a normal ocfs2_xattr_header, refcount all the entries which
6111 * have value stored outside.
6112 * Used for xattrs stored in inode and ocfs2_xattr_block.
6114 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
6115 struct ocfs2_xattr_value_buf *vb,
6116 struct ocfs2_xattr_header *header,
6117 struct ocfs2_caching_info *ref_ci,
6118 struct buffer_head *ref_root_bh,
6119 struct ocfs2_cached_dealloc_ctxt *dealloc)
6122 struct ocfs2_xattr_entry *xe;
6123 struct ocfs2_xattr_value_root *xv;
6124 struct ocfs2_extent_tree et;
6127 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
6128 xe = &header->xh_entries[i];
6130 if (ocfs2_xattr_is_local(xe))
6133 xv = (struct ocfs2_xattr_value_root *)((void *)header +
6134 le16_to_cpu(xe->xe_name_offset) +
6135 OCFS2_XATTR_SIZE(xe->xe_name_len));
6138 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
6140 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
6141 ref_ci, ref_root_bh,
6152 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
6153 struct buffer_head *fe_bh,
6154 struct ocfs2_caching_info *ref_ci,
6155 struct buffer_head *ref_root_bh,
6156 struct ocfs2_cached_dealloc_ctxt *dealloc)
6158 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6159 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
6160 (fe_bh->b_data + inode->i_sb->s_blocksize -
6161 le16_to_cpu(di->i_xattr_inline_size));
6162 struct ocfs2_xattr_value_buf vb = {
6164 .vb_access = ocfs2_journal_access_di,
6167 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6168 ref_ci, ref_root_bh, dealloc);
6171 struct ocfs2_xattr_tree_value_refcount_para {
6172 struct ocfs2_caching_info *ref_ci;
6173 struct buffer_head *ref_root_bh;
6174 struct ocfs2_cached_dealloc_ctxt *dealloc;
6177 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
6178 struct ocfs2_xattr_bucket *bucket,
6180 struct ocfs2_xattr_value_root **xv,
6181 struct buffer_head **bh)
6183 int ret, block_off, name_offset;
6184 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
6185 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6188 ret = ocfs2_xattr_bucket_get_name_value(sb,
6198 base = bucket_block(bucket, block_off);
6200 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6201 OCFS2_XATTR_SIZE(xe->xe_name_len));
6204 *bh = bucket->bu_bhs[block_off];
6210 * For a given xattr bucket, refcount all the entries which
6211 * have value stored outside.
6213 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6214 struct ocfs2_xattr_bucket *bucket,
6218 struct ocfs2_extent_tree et;
6219 struct ocfs2_xattr_tree_value_refcount_para *ref =
6220 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6221 struct ocfs2_xattr_header *xh =
6222 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6223 struct ocfs2_xattr_entry *xe;
6224 struct ocfs2_xattr_value_buf vb = {
6225 .vb_access = ocfs2_journal_access,
6227 struct ocfs2_post_refcount refcount = {
6228 .credits = bucket->bu_blocks,
6230 .func = ocfs2_xattr_bucket_post_refcount,
6232 struct ocfs2_post_refcount *p = NULL;
6234 /* We only need post_refcount if we support metaecc. */
6235 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6238 mlog(0, "refcount bucket %llu, count = %u\n",
6239 (unsigned long long)bucket_blkno(bucket),
6240 le16_to_cpu(xh->xh_count));
6241 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6242 xe = &xh->xh_entries[i];
6244 if (ocfs2_xattr_is_local(xe))
6247 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6248 &vb.vb_xv, &vb.vb_bh);
6254 ocfs2_init_xattr_value_extent_tree(&et,
6255 INODE_CACHE(inode), &vb);
6257 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6271 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6272 struct buffer_head *root_bh,
6273 u64 blkno, u32 cpos, u32 len, void *para)
6275 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6276 ocfs2_xattr_bucket_value_refcount,
6280 static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6281 struct buffer_head *blk_bh,
6282 struct ocfs2_caching_info *ref_ci,
6283 struct buffer_head *ref_root_bh,
6284 struct ocfs2_cached_dealloc_ctxt *dealloc)
6287 struct ocfs2_xattr_block *xb =
6288 (struct ocfs2_xattr_block *)blk_bh->b_data;
6290 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6291 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6292 struct ocfs2_xattr_value_buf vb = {
6294 .vb_access = ocfs2_journal_access_xb,
6297 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6298 ref_ci, ref_root_bh,
6301 struct ocfs2_xattr_tree_value_refcount_para para = {
6303 .ref_root_bh = ref_root_bh,
6307 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6308 ocfs2_refcount_xattr_tree_rec,
6315 int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6316 struct buffer_head *fe_bh,
6317 struct ocfs2_caching_info *ref_ci,
6318 struct buffer_head *ref_root_bh,
6319 struct ocfs2_cached_dealloc_ctxt *dealloc)
6322 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6323 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6324 struct buffer_head *blk_bh = NULL;
6326 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6327 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6328 ref_ci, ref_root_bh,
6336 if (!di->i_xattr_loc)
6339 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6346 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6347 ref_root_bh, dealloc);
6357 typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
6359 * Store the information we need in xattr reflink.
6360 * old_bh and new_bh are inode bh for the old and new inode.
6362 struct ocfs2_xattr_reflink {
6363 struct inode *old_inode;
6364 struct inode *new_inode;
6365 struct buffer_head *old_bh;
6366 struct buffer_head *new_bh;
6367 struct ocfs2_caching_info *ref_ci;
6368 struct buffer_head *ref_root_bh;
6369 struct ocfs2_cached_dealloc_ctxt *dealloc;
6370 should_xattr_reflinked *xattr_reflinked;
6374 * Given a xattr header and xe offset,
6375 * return the proper xv and the corresponding bh.
6376 * xattr in inode, block and xattr tree have different implementaions.
6378 typedef int (get_xattr_value_root)(struct super_block *sb,
6379 struct buffer_head *bh,
6380 struct ocfs2_xattr_header *xh,
6382 struct ocfs2_xattr_value_root **xv,
6383 struct buffer_head **ret_bh,
6387 * Calculate all the xattr value root metadata stored in this xattr header and
6388 * credits we need if we create them from the scratch.
6389 * We use get_xattr_value_root so that all types of xattr container can use it.
6391 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6392 struct buffer_head *bh,
6393 struct ocfs2_xattr_header *xh,
6394 int *metas, int *credits,
6396 get_xattr_value_root *func,
6400 struct ocfs2_xattr_value_root *xv;
6401 struct ocfs2_xattr_entry *xe;
6403 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6404 xe = &xh->xh_entries[i];
6405 if (ocfs2_xattr_is_local(xe))
6408 ret = func(sb, bh, xh, i, &xv, NULL, para);
6414 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6415 le16_to_cpu(xv->xr_list.l_next_free_rec);
6417 *credits += ocfs2_calc_extend_credits(sb,
6419 le32_to_cpu(xv->xr_clusters));
6422 * If the value is a tree with depth > 1, We don't go deep
6423 * to the extent block, so just calculate a maximum record num.
6425 if (!xv->xr_list.l_tree_depth)
6426 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
6428 *num_recs += ocfs2_clusters_for_bytes(sb,
6435 /* Used by xattr inode and block to return the right xv and buffer_head. */
6436 static int ocfs2_get_xattr_value_root(struct super_block *sb,
6437 struct buffer_head *bh,
6438 struct ocfs2_xattr_header *xh,
6440 struct ocfs2_xattr_value_root **xv,
6441 struct buffer_head **ret_bh,
6444 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6446 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6447 le16_to_cpu(xe->xe_name_offset) +
6448 OCFS2_XATTR_SIZE(xe->xe_name_len));
6457 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6458 * It is only used for inline xattr and xattr block.
6460 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6461 struct ocfs2_xattr_header *xh,
6462 struct buffer_head *ref_root_bh,
6464 struct ocfs2_alloc_context **meta_ac)
6466 int ret, meta_add = 0, num_recs = 0;
6467 struct ocfs2_refcount_block *rb =
6468 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6472 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6473 &meta_add, credits, &num_recs,
6474 ocfs2_get_xattr_value_root,
6482 * We need to add/modify num_recs in refcount tree, so just calculate
6483 * an approximate number we need for refcount tree change.
6484 * Sometimes we need to split the tree, and after split, half recs
6485 * will be moved to the new block, and a new block can only provide
6486 * half number of recs. So we multiple new blocks by 2.
6488 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6489 meta_add += num_recs;
6490 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6491 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6492 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6493 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6497 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6506 * Given a xattr header, reflink all the xattrs in this container.
6507 * It can be used for inode, block and bucket.
6510 * Before we call this function, the caller has memcpy the xattr in
6511 * old_xh to the new_xh.
6513 * If args.xattr_reflinked is set, call it to decide whether the xe should
6514 * be reflinked or not. If not, remove it from the new xattr header.
6516 static int ocfs2_reflink_xattr_header(handle_t *handle,
6517 struct ocfs2_xattr_reflink *args,
6518 struct buffer_head *old_bh,
6519 struct ocfs2_xattr_header *xh,
6520 struct buffer_head *new_bh,
6521 struct ocfs2_xattr_header *new_xh,
6522 struct ocfs2_xattr_value_buf *vb,
6523 struct ocfs2_alloc_context *meta_ac,
6524 get_xattr_value_root *func,
6528 struct super_block *sb = args->old_inode->i_sb;
6529 struct buffer_head *value_bh;
6530 struct ocfs2_xattr_entry *xe, *last;
6531 struct ocfs2_xattr_value_root *xv, *new_xv;
6532 struct ocfs2_extent_tree data_et;
6533 u32 clusters, cpos, p_cluster, num_clusters;
6534 unsigned int ext_flags = 0;
6536 mlog(0, "reflink xattr in container %llu, count = %u\n",
6537 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
6539 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6540 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
6541 xe = &xh->xh_entries[i];
6543 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6544 xe = &new_xh->xh_entries[j];
6546 le16_add_cpu(&new_xh->xh_count, -1);
6547 if (new_xh->xh_count) {
6549 (void *)last - (void *)xe);
6551 sizeof(struct ocfs2_xattr_entry));
6555 * We don't want j to increase in the next round since
6556 * it is already moved ahead.
6562 if (ocfs2_xattr_is_local(xe))
6565 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6571 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6578 * For the xattr which has l_tree_depth = 0, all the extent
6579 * recs have already be copied to the new xh with the
6580 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6581 * increase the refount count int the refcount tree.
6583 * For the xattr which has l_tree_depth > 0, we need
6584 * to initialize it to the empty default value root,
6585 * and then insert the extents one by one.
6587 if (xv->xr_list.l_tree_depth) {
6588 memcpy(new_xv, &def_xv, sizeof(def_xv));
6590 vb->vb_bh = value_bh;
6591 ocfs2_init_xattr_value_extent_tree(&data_et,
6592 INODE_CACHE(args->new_inode), vb);
6595 clusters = le32_to_cpu(xv->xr_clusters);
6597 while (cpos < clusters) {
6598 ret = ocfs2_xattr_get_clusters(args->old_inode,
6611 if (xv->xr_list.l_tree_depth) {
6612 ret = ocfs2_insert_extent(handle,
6614 ocfs2_clusters_to_blocks(
6615 args->old_inode->i_sb,
6617 num_clusters, ext_flags,
6625 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6627 p_cluster, num_clusters,
6628 meta_ac, args->dealloc);
6634 cpos += num_clusters;
6642 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6644 int ret = 0, credits = 0;
6646 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6647 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6648 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6649 int header_off = osb->sb->s_blocksize - inline_size;
6650 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6651 (args->old_bh->b_data + header_off);
6652 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6653 (args->new_bh->b_data + header_off);
6654 struct ocfs2_alloc_context *meta_ac = NULL;
6655 struct ocfs2_inode_info *new_oi;
6656 struct ocfs2_dinode *new_di;
6657 struct ocfs2_xattr_value_buf vb = {
6658 .vb_bh = args->new_bh,
6659 .vb_access = ocfs2_journal_access_di,
6662 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6663 &credits, &meta_ac);
6669 handle = ocfs2_start_trans(osb, credits);
6670 if (IS_ERR(handle)) {
6671 ret = PTR_ERR(handle);
6676 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6677 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6683 memcpy(args->new_bh->b_data + header_off,
6684 args->old_bh->b_data + header_off, inline_size);
6686 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6687 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6689 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6690 args->new_bh, new_xh, &vb, meta_ac,
6691 ocfs2_get_xattr_value_root, NULL);
6697 new_oi = OCFS2_I(args->new_inode);
6698 spin_lock(&new_oi->ip_lock);
6699 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6700 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6701 spin_unlock(&new_oi->ip_lock);
6703 ocfs2_journal_dirty(handle, args->new_bh);
6706 ocfs2_commit_trans(osb, handle);
6710 ocfs2_free_alloc_context(meta_ac);
6714 static int ocfs2_create_empty_xattr_block(struct inode *inode,
6715 struct buffer_head *fe_bh,
6716 struct buffer_head **ret_bh,
6721 struct ocfs2_alloc_context *meta_ac;
6722 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6724 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
6730 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6731 if (IS_ERR(handle)) {
6732 ret = PTR_ERR(handle);
6737 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6738 (unsigned long long)fe_bh->b_blocknr, indexed);
6739 ret = ocfs2_create_xattr_block(handle, inode, fe_bh,
6740 meta_ac, ret_bh, indexed);
6744 ocfs2_commit_trans(osb, handle);
6746 ocfs2_free_alloc_context(meta_ac);
6750 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6751 struct buffer_head *blk_bh,
6752 struct buffer_head *new_blk_bh)
6754 int ret = 0, credits = 0;
6756 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6757 struct ocfs2_dinode *new_di;
6758 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6759 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6760 struct ocfs2_xattr_block *xb =
6761 (struct ocfs2_xattr_block *)blk_bh->b_data;
6762 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6763 struct ocfs2_xattr_block *new_xb =
6764 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6765 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6766 struct ocfs2_alloc_context *meta_ac;
6767 struct ocfs2_xattr_value_buf vb = {
6768 .vb_bh = new_blk_bh,
6769 .vb_access = ocfs2_journal_access_xb,
6772 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6773 &credits, &meta_ac);
6779 /* One more credits in case we need to add xattr flags in new inode. */
6780 handle = ocfs2_start_trans(osb, credits + 1);
6781 if (IS_ERR(handle)) {
6782 ret = PTR_ERR(handle);
6787 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6788 ret = ocfs2_journal_access_di(handle,
6789 INODE_CACHE(args->new_inode),
6791 OCFS2_JOURNAL_ACCESS_WRITE);
6798 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6799 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6805 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6806 osb->sb->s_blocksize - header_off);
6808 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6809 new_blk_bh, new_xh, &vb, meta_ac,
6810 ocfs2_get_xattr_value_root, NULL);
6816 ocfs2_journal_dirty(handle, new_blk_bh);
6818 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6819 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6820 spin_lock(&new_oi->ip_lock);
6821 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6822 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6823 spin_unlock(&new_oi->ip_lock);
6825 ocfs2_journal_dirty(handle, args->new_bh);
6829 ocfs2_commit_trans(osb, handle);
6832 ocfs2_free_alloc_context(meta_ac);
6836 struct ocfs2_reflink_xattr_tree_args {
6837 struct ocfs2_xattr_reflink *reflink;
6838 struct buffer_head *old_blk_bh;
6839 struct buffer_head *new_blk_bh;
6840 struct ocfs2_xattr_bucket *old_bucket;
6841 struct ocfs2_xattr_bucket *new_bucket;
6846 * We have to handle the case that both old bucket and new bucket
6847 * will call this function to get the right ret_bh.
6848 * So The caller must give us the right bh.
6850 static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6851 struct buffer_head *bh,
6852 struct ocfs2_xattr_header *xh,
6854 struct ocfs2_xattr_value_root **xv,
6855 struct buffer_head **ret_bh,
6858 struct ocfs2_reflink_xattr_tree_args *args =
6859 (struct ocfs2_reflink_xattr_tree_args *)para;
6860 struct ocfs2_xattr_bucket *bucket;
6862 if (bh == args->old_bucket->bu_bhs[0])
6863 bucket = args->old_bucket;
6865 bucket = args->new_bucket;
6867 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6871 struct ocfs2_value_tree_metas {
6877 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6878 struct buffer_head *bh,
6879 struct ocfs2_xattr_header *xh,
6881 struct ocfs2_xattr_value_root **xv,
6882 struct buffer_head **ret_bh,
6885 struct ocfs2_xattr_bucket *bucket =
6886 (struct ocfs2_xattr_bucket *)para;
6888 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6892 static int ocfs2_calc_value_tree_metas(struct inode *inode,
6893 struct ocfs2_xattr_bucket *bucket,
6896 struct ocfs2_value_tree_metas *metas =
6897 (struct ocfs2_value_tree_metas *)para;
6898 struct ocfs2_xattr_header *xh =
6899 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6901 /* Add the credits for this bucket first. */
6902 metas->credits += bucket->bu_blocks;
6903 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6904 xh, &metas->num_metas,
6905 &metas->credits, &metas->num_recs,
6906 ocfs2_value_tree_metas_in_bucket,
6911 * Given a xattr extent rec starting from blkno and having len clusters,
6912 * iterate all the buckets calculate how much metadata we need for reflinking
6913 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6915 static int ocfs2_lock_reflink_xattr_rec_allocators(
6916 struct ocfs2_reflink_xattr_tree_args *args,
6917 struct ocfs2_extent_tree *xt_et,
6918 u64 blkno, u32 len, int *credits,
6919 struct ocfs2_alloc_context **meta_ac,
6920 struct ocfs2_alloc_context **data_ac)
6922 int ret, num_free_extents;
6923 struct ocfs2_value_tree_metas metas;
6924 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6925 struct ocfs2_refcount_block *rb;
6927 memset(&metas, 0, sizeof(metas));
6929 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6930 ocfs2_calc_value_tree_metas, &metas);
6936 *credits = metas.credits;
6939 * Calculate we need for refcount tree change.
6941 * We need to add/modify num_recs in refcount tree, so just calculate
6942 * an approximate number we need for refcount tree change.
6943 * Sometimes we need to split the tree, and after split, half recs
6944 * will be moved to the new block, and a new block can only provide
6945 * half number of recs. So we multiple new blocks by 2.
6946 * In the end, we have to add credits for modifying the already
6947 * existed refcount block.
6949 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6951 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6952 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6953 metas.num_metas += metas.num_recs;
6954 *credits += metas.num_recs +
6955 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6956 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6957 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6958 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6962 /* count in the xattr tree change. */
6963 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6964 if (num_free_extents < 0) {
6965 ret = num_free_extents;
6970 if (num_free_extents < len)
6971 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6973 *credits += ocfs2_calc_extend_credits(osb->sb,
6974 xt_et->et_root_el, len);
6976 if (metas.num_metas) {
6977 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6986 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6993 ocfs2_free_alloc_context(*meta_ac);
7001 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
7002 u64 blkno, u64 new_blkno, u32 clusters,
7003 struct ocfs2_alloc_context *meta_ac,
7004 struct ocfs2_alloc_context *data_ac,
7005 struct ocfs2_reflink_xattr_tree_args *args)
7008 struct super_block *sb = args->reflink->old_inode->i_sb;
7009 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
7010 u32 num_buckets = clusters * bpc;
7011 int bpb = args->old_bucket->bu_blocks;
7012 struct ocfs2_xattr_value_buf vb = {
7013 .vb_access = ocfs2_journal_access,
7016 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
7017 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
7023 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
7030 * The real bucket num in this series of blocks is stored
7031 * in the 1st bucket.
7034 num_buckets = le16_to_cpu(
7035 bucket_xh(args->old_bucket)->xh_num_buckets);
7037 ret = ocfs2_xattr_bucket_journal_access(handle,
7039 OCFS2_JOURNAL_ACCESS_CREATE);
7045 for (j = 0; j < bpb; j++)
7046 memcpy(bucket_block(args->new_bucket, j),
7047 bucket_block(args->old_bucket, j),
7050 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
7052 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
7053 args->old_bucket->bu_bhs[0],
7054 bucket_xh(args->old_bucket),
7055 args->new_bucket->bu_bhs[0],
7056 bucket_xh(args->new_bucket),
7058 ocfs2_get_reflink_xattr_value_root,
7066 * Re-access and dirty the bucket to calculate metaecc.
7067 * Because we may extend the transaction in reflink_xattr_header
7068 * which will let the already accessed block gone.
7070 ret = ocfs2_xattr_bucket_journal_access(handle,
7072 OCFS2_JOURNAL_ACCESS_WRITE);
7078 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
7079 ocfs2_xattr_bucket_relse(args->old_bucket);
7080 ocfs2_xattr_bucket_relse(args->new_bucket);
7083 ocfs2_xattr_bucket_relse(args->old_bucket);
7084 ocfs2_xattr_bucket_relse(args->new_bucket);
7088 * Create the same xattr extent record in the new inode's xattr tree.
7090 static int ocfs2_reflink_xattr_rec(struct inode *inode,
7091 struct buffer_head *root_bh,
7097 int ret, credits = 0;
7098 u32 p_cluster, num_clusters;
7101 struct ocfs2_reflink_xattr_tree_args *args =
7102 (struct ocfs2_reflink_xattr_tree_args *)para;
7103 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7104 struct ocfs2_alloc_context *meta_ac = NULL;
7105 struct ocfs2_alloc_context *data_ac = NULL;
7106 struct ocfs2_extent_tree et;
7108 ocfs2_init_xattr_tree_extent_tree(&et,
7109 INODE_CACHE(args->reflink->new_inode),
7112 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
7114 &meta_ac, &data_ac);
7120 handle = ocfs2_start_trans(osb, credits);
7121 if (IS_ERR(handle)) {
7122 ret = PTR_ERR(handle);
7127 ret = ocfs2_claim_clusters(osb, handle, data_ac,
7128 len, &p_cluster, &num_clusters);
7134 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
7136 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
7137 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
7138 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
7139 meta_ac, data_ac, args);
7145 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
7146 (unsigned long long)new_blkno, len, cpos);
7147 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
7153 ocfs2_commit_trans(osb, handle);
7157 ocfs2_free_alloc_context(meta_ac);
7159 ocfs2_free_alloc_context(data_ac);
7164 * Create reflinked xattr buckets.
7165 * We will add bucket one by one, and refcount all the xattrs in the bucket
7166 * if they are stored outside.
7168 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
7169 struct buffer_head *blk_bh,
7170 struct buffer_head *new_blk_bh)
7173 struct ocfs2_reflink_xattr_tree_args para;
7175 memset(¶, 0, sizeof(para));
7176 para.reflink = args;
7177 para.old_blk_bh = blk_bh;
7178 para.new_blk_bh = new_blk_bh;
7180 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
7181 if (!para.old_bucket) {
7182 mlog_errno(-ENOMEM);
7186 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
7187 if (!para.new_bucket) {
7193 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
7194 ocfs2_reflink_xattr_rec,
7200 ocfs2_xattr_bucket_free(para.old_bucket);
7201 ocfs2_xattr_bucket_free(para.new_bucket);
7205 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7206 struct buffer_head *blk_bh)
7208 int ret, indexed = 0;
7209 struct buffer_head *new_blk_bh = NULL;
7210 struct ocfs2_xattr_block *xb =
7211 (struct ocfs2_xattr_block *)blk_bh->b_data;
7214 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7217 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7218 &new_blk_bh, indexed);
7224 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7225 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7227 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7236 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7238 int type = ocfs2_xattr_get_type(xe);
7240 return type != OCFS2_XATTR_INDEX_SECURITY &&
7241 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7242 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7245 int ocfs2_reflink_xattrs(struct inode *old_inode,
7246 struct buffer_head *old_bh,
7247 struct inode *new_inode,
7248 struct buffer_head *new_bh,
7249 bool preserve_security)
7252 struct ocfs2_xattr_reflink args;
7253 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7254 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7255 struct buffer_head *blk_bh = NULL;
7256 struct ocfs2_cached_dealloc_ctxt dealloc;
7257 struct ocfs2_refcount_tree *ref_tree;
7258 struct buffer_head *ref_root_bh = NULL;
7260 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7261 le64_to_cpu(di->i_refcount_loc),
7262 1, &ref_tree, &ref_root_bh);
7268 ocfs2_init_dealloc_ctxt(&dealloc);
7270 args.old_inode = old_inode;
7271 args.new_inode = new_inode;
7272 args.old_bh = old_bh;
7273 args.new_bh = new_bh;
7274 args.ref_ci = &ref_tree->rf_ci;
7275 args.ref_root_bh = ref_root_bh;
7276 args.dealloc = &dealloc;
7277 if (preserve_security)
7278 args.xattr_reflinked = NULL;
7280 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
7282 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7283 ret = ocfs2_reflink_xattr_inline(&args);
7290 if (!di->i_xattr_loc)
7293 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7300 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7307 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7309 brelse(ref_root_bh);
7311 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7312 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7313 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7321 * Initialize security and acl for a already created inode.
7322 * Used for reflink a non-preserve-security file.
7324 * It uses common api like ocfs2_xattr_set, so the caller
7325 * must not hold any lock expect i_mutex.
7327 int ocfs2_init_security_and_acl(struct inode *dir,
7328 struct inode *inode)
7331 struct buffer_head *dir_bh = NULL;
7332 struct ocfs2_security_xattr_info si = {
7336 ret = ocfs2_init_security_get(inode, dir, &si);
7338 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7339 si.name, si.value, si.value_len,
7345 } else if (ret != -EOPNOTSUPP) {
7350 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7356 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7360 ocfs2_inode_unlock(dir, 0);
7366 * 'security' attributes support
7368 static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
7369 size_t list_size, const char *name,
7370 size_t name_len, int type)
7372 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7373 const size_t total_len = prefix_len + name_len + 1;
7375 if (list && total_len <= list_size) {
7376 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7377 memcpy(list + prefix_len, name, name_len);
7378 list[prefix_len + name_len] = '\0';
7383 static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7384 void *buffer, size_t size, int type)
7386 if (strcmp(name, "") == 0)
7388 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7389 name, buffer, size);
7392 static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7393 const void *value, size_t size, int flags, int type)
7395 if (strcmp(name, "") == 0)
7398 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7399 name, value, size, flags);
7402 int ocfs2_init_security_get(struct inode *inode,
7404 struct ocfs2_security_xattr_info *si)
7406 /* check whether ocfs2 support feature xattr */
7407 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7409 return security_inode_init_security(inode, dir, &si->name, &si->value,
7413 int ocfs2_init_security_set(handle_t *handle,
7414 struct inode *inode,
7415 struct buffer_head *di_bh,
7416 struct ocfs2_security_xattr_info *si,
7417 struct ocfs2_alloc_context *xattr_ac,
7418 struct ocfs2_alloc_context *data_ac)
7420 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7421 OCFS2_XATTR_INDEX_SECURITY,
7422 si->name, si->value, si->value_len, 0,
7426 struct xattr_handler ocfs2_xattr_security_handler = {
7427 .prefix = XATTR_SECURITY_PREFIX,
7428 .list = ocfs2_xattr_security_list,
7429 .get = ocfs2_xattr_security_get,
7430 .set = ocfs2_xattr_security_set,
7434 * 'trusted' attributes support
7436 static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
7437 size_t list_size, const char *name,
7438 size_t name_len, int type)
7440 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
7441 const size_t total_len = prefix_len + name_len + 1;
7443 if (list && total_len <= list_size) {
7444 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7445 memcpy(list + prefix_len, name, name_len);
7446 list[prefix_len + name_len] = '\0';
7451 static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7452 void *buffer, size_t size, int type)
7454 if (strcmp(name, "") == 0)
7456 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7457 name, buffer, size);
7460 static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7461 const void *value, size_t size, int flags, int type)
7463 if (strcmp(name, "") == 0)
7466 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7467 name, value, size, flags);
7470 struct xattr_handler ocfs2_xattr_trusted_handler = {
7471 .prefix = XATTR_TRUSTED_PREFIX,
7472 .list = ocfs2_xattr_trusted_list,
7473 .get = ocfs2_xattr_trusted_get,
7474 .set = ocfs2_xattr_trusted_set,
7478 * 'user' attributes support
7480 static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
7481 size_t list_size, const char *name,
7482 size_t name_len, int type)
7484 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7485 const size_t total_len = prefix_len + name_len + 1;
7486 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7488 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7491 if (list && total_len <= list_size) {
7492 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7493 memcpy(list + prefix_len, name, name_len);
7494 list[prefix_len + name_len] = '\0';
7499 static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7500 void *buffer, size_t size, int type)
7502 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7504 if (strcmp(name, "") == 0)
7506 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7508 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
7512 static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7513 const void *value, size_t size, int flags, int type)
7515 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7517 if (strcmp(name, "") == 0)
7519 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7522 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7523 name, value, size, flags);
7526 struct xattr_handler ocfs2_xattr_user_handler = {
7527 .prefix = XATTR_USER_PREFIX,
7528 .list = ocfs2_xattr_user_list,
7529 .get = ocfs2_xattr_user_get,
7530 .set = ocfs2_xattr_user_set,