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);
177 * Initialize the value buf's access and bh fields for this entry.
178 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
180 void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
181 struct ocfs2_xattr_value_buf *vb);
185 * Describes an xattr entry location. This is a memory structure
186 * tracking the on-disk structure.
188 struct ocfs2_xa_loc {
189 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
190 struct ocfs2_xattr_header *xl_header;
192 /* Bytes from xl_header to the end of the storage */
196 * The ocfs2_xattr_entry this location describes. If this is
197 * NULL, this location describes the on-disk structure where it
200 struct ocfs2_xattr_entry *xl_entry;
203 * Internal housekeeping
206 /* Buffer(s) containing this entry */
209 /* Operations on the storage backing this location */
210 const struct ocfs2_xa_loc_operations *xl_ops;
214 * Convenience functions to calculate how much space is needed for a
215 * given name+value pair
217 static int namevalue_size(int name_len, uint64_t value_len)
219 if (value_len > OCFS2_XATTR_INLINE_SIZE)
220 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
222 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
225 static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
227 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
230 static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
232 u64 value_len = le64_to_cpu(xe->xe_value_size);
234 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
235 ocfs2_xattr_is_local(xe));
236 return namevalue_size(xe->xe_name_len, value_len);
240 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
241 struct ocfs2_xattr_header *xh,
246 static int ocfs2_xattr_block_find(struct inode *inode,
249 struct ocfs2_xattr_search *xs);
250 static int ocfs2_xattr_index_block_find(struct inode *inode,
251 struct buffer_head *root_bh,
254 struct ocfs2_xattr_search *xs);
256 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
257 struct buffer_head *blk_bh,
261 static int ocfs2_xattr_create_index_block(struct inode *inode,
262 struct ocfs2_xattr_search *xs,
263 struct ocfs2_xattr_set_ctxt *ctxt);
265 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
266 struct ocfs2_xattr_info *xi,
267 struct ocfs2_xattr_search *xs,
268 struct ocfs2_xattr_set_ctxt *ctxt);
270 typedef int (xattr_tree_rec_func)(struct inode *inode,
271 struct buffer_head *root_bh,
272 u64 blkno, u32 cpos, u32 len, void *para);
273 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
274 struct buffer_head *root_bh,
275 xattr_tree_rec_func *rec_func,
277 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
278 struct ocfs2_xattr_bucket *bucket,
280 static int ocfs2_rm_xattr_cluster(struct inode *inode,
281 struct buffer_head *root_bh,
287 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
288 u64 src_blk, u64 last_blk, u64 to_blk,
289 unsigned int start_bucket,
291 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
292 struct ocfs2_dinode *di,
293 struct ocfs2_xattr_info *xi,
294 struct ocfs2_xattr_search *xis,
295 struct ocfs2_xattr_search *xbs,
296 struct ocfs2_refcount_tree **ref_tree,
299 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
300 struct ocfs2_xattr_bucket *bucket,
302 struct ocfs2_xattr_value_root **xv,
303 struct buffer_head **bh);
305 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
307 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
310 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
312 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
315 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
317 u16 len = sb->s_blocksize -
318 offsetof(struct ocfs2_xattr_header, xh_entries);
320 return len / sizeof(struct ocfs2_xattr_entry);
323 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
324 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
325 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
327 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
329 struct ocfs2_xattr_bucket *bucket;
330 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
332 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
334 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
336 bucket->bu_inode = inode;
337 bucket->bu_blocks = blks;
343 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
347 for (i = 0; i < bucket->bu_blocks; i++) {
348 brelse(bucket->bu_bhs[i]);
349 bucket->bu_bhs[i] = NULL;
353 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
356 ocfs2_xattr_bucket_relse(bucket);
357 bucket->bu_inode = NULL;
363 * A bucket that has never been written to disk doesn't need to be
364 * read. We just need the buffer_heads. Don't call this for
365 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
368 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
373 for (i = 0; i < bucket->bu_blocks; i++) {
374 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
376 if (!bucket->bu_bhs[i]) {
382 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
384 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
389 ocfs2_xattr_bucket_relse(bucket);
393 /* Read the xattr bucket at xb_blkno */
394 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
399 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
400 bucket->bu_blocks, bucket->bu_bhs, 0,
403 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
404 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
407 &bucket_xh(bucket)->xh_check);
408 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
414 ocfs2_xattr_bucket_relse(bucket);
418 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
419 struct ocfs2_xattr_bucket *bucket,
424 for (i = 0; i < bucket->bu_blocks; i++) {
425 rc = ocfs2_journal_access(handle,
426 INODE_CACHE(bucket->bu_inode),
427 bucket->bu_bhs[i], type);
437 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
438 struct ocfs2_xattr_bucket *bucket)
442 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
443 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
444 bucket->bu_bhs, bucket->bu_blocks,
445 &bucket_xh(bucket)->xh_check);
446 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
448 for (i = 0; i < bucket->bu_blocks; i++)
449 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
452 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
453 struct ocfs2_xattr_bucket *src)
456 int blocksize = src->bu_inode->i_sb->s_blocksize;
458 BUG_ON(dest->bu_blocks != src->bu_blocks);
459 BUG_ON(dest->bu_inode != src->bu_inode);
461 for (i = 0; i < src->bu_blocks; i++) {
462 memcpy(bucket_block(dest, i), bucket_block(src, i),
467 static int ocfs2_validate_xattr_block(struct super_block *sb,
468 struct buffer_head *bh)
471 struct ocfs2_xattr_block *xb =
472 (struct ocfs2_xattr_block *)bh->b_data;
474 mlog(0, "Validating xattr block %llu\n",
475 (unsigned long long)bh->b_blocknr);
477 BUG_ON(!buffer_uptodate(bh));
480 * If the ecc fails, we return the error but otherwise
481 * leave the filesystem running. We know any error is
482 * local to this block.
484 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
489 * Errors after here are fatal
492 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
494 "Extended attribute block #%llu has bad "
496 (unsigned long long)bh->b_blocknr, 7,
501 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
503 "Extended attribute block #%llu has an "
504 "invalid xb_blkno of %llu",
505 (unsigned long long)bh->b_blocknr,
506 (unsigned long long)le64_to_cpu(xb->xb_blkno));
510 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
512 "Extended attribute block #%llu has an invalid "
513 "xb_fs_generation of #%u",
514 (unsigned long long)bh->b_blocknr,
515 le32_to_cpu(xb->xb_fs_generation));
522 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
523 struct buffer_head **bh)
526 struct buffer_head *tmp = *bh;
528 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
529 ocfs2_validate_xattr_block);
531 /* If ocfs2_read_block() got us a new bh, pass it up. */
538 static inline const char *ocfs2_xattr_prefix(int name_index)
540 struct xattr_handler *handler = NULL;
542 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
543 handler = ocfs2_xattr_handler_map[name_index];
545 return handler ? handler->prefix : NULL;
548 static u32 ocfs2_xattr_name_hash(struct inode *inode,
552 /* Get hash value of uuid from super block */
553 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
556 /* hash extended attribute name */
557 for (i = 0; i < name_len; i++) {
558 hash = (hash << OCFS2_HASH_SHIFT) ^
559 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
567 * ocfs2_xattr_hash_entry()
569 * Compute the hash of an extended attribute.
571 static void ocfs2_xattr_hash_entry(struct inode *inode,
572 struct ocfs2_xattr_header *header,
573 struct ocfs2_xattr_entry *entry)
576 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
578 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
579 entry->xe_name_hash = cpu_to_le32(hash);
584 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
586 return namevalue_size(name_len, value_len) +
587 sizeof(struct ocfs2_xattr_entry);
590 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
592 return namevalue_size_xi(xi) +
593 sizeof(struct ocfs2_xattr_entry);
596 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
598 return namevalue_size_xe(xe) +
599 sizeof(struct ocfs2_xattr_entry);
602 int ocfs2_calc_security_init(struct inode *dir,
603 struct ocfs2_security_xattr_info *si,
606 struct ocfs2_alloc_context **xattr_ac)
609 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
610 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
614 * The max space of security xattr taken inline is
615 * 256(name) + 80(value) + 16(entry) = 352 bytes,
616 * So reserve one metadata block for it is ok.
618 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
619 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
620 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
625 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
628 /* reserve clusters for xattr value which will be set in B tree*/
629 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
630 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
633 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
635 *want_clusters += new_clusters;
640 int ocfs2_calc_xattr_init(struct inode *dir,
641 struct buffer_head *dir_bh,
643 struct ocfs2_security_xattr_info *si,
649 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
650 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
653 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
656 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
657 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
658 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
661 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
664 } else if (acl_len != 0 && acl_len != -ENODATA) {
670 if (!(s_size + a_size))
674 * The max space of security xattr taken inline is
675 * 256(name) + 80(value) + 16(entry) = 352 bytes,
676 * The max space of acl xattr taken inline is
677 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
678 * when blocksize = 512, may reserve one more cluser for
679 * xattr bucket, otherwise reserve one metadata block
681 * If this is a new directory with inline data,
682 * we choose to reserve the entire inline area for
683 * directory contents and force an external xattr block.
685 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
686 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
687 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
688 *want_meta = *want_meta + 1;
689 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
692 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
693 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
695 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
699 * reserve credits and clusters for xattrs which has large value
700 * and have to be set outside
702 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
703 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
705 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
707 *want_clusters += new_clusters;
709 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
710 acl_len > OCFS2_XATTR_INLINE_SIZE) {
711 /* for directory, it has DEFAULT and ACCESS two types of acls */
712 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
713 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
714 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
716 *want_clusters += new_clusters;
722 static int ocfs2_xattr_extend_allocation(struct inode *inode,
724 struct ocfs2_xattr_value_buf *vb,
725 struct ocfs2_xattr_set_ctxt *ctxt)
728 handle_t *handle = ctxt->handle;
729 enum ocfs2_alloc_restarted why;
730 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
731 struct ocfs2_extent_tree et;
733 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
735 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
737 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
738 OCFS2_JOURNAL_ACCESS_WRITE);
744 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
745 status = ocfs2_add_clusters_in_btree(handle,
758 status = ocfs2_journal_dirty(handle, vb->vb_bh);
764 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
767 * We should have already allocated enough space before the transaction,
768 * so no need to restart.
770 BUG_ON(why != RESTART_NONE || clusters_to_add);
777 static int __ocfs2_remove_xattr_range(struct inode *inode,
778 struct ocfs2_xattr_value_buf *vb,
779 u32 cpos, u32 phys_cpos, u32 len,
780 unsigned int ext_flags,
781 struct ocfs2_xattr_set_ctxt *ctxt)
784 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
785 handle_t *handle = ctxt->handle;
786 struct ocfs2_extent_tree et;
788 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
790 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
791 OCFS2_JOURNAL_ACCESS_WRITE);
797 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
804 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
806 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
812 if (ext_flags & OCFS2_EXT_REFCOUNTED)
813 ret = ocfs2_decrease_refcount(inode, handle,
814 ocfs2_blocks_to_clusters(inode->i_sb,
816 len, ctxt->meta_ac, &ctxt->dealloc, 1);
818 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
827 static int ocfs2_xattr_shrink_size(struct inode *inode,
830 struct ocfs2_xattr_value_buf *vb,
831 struct ocfs2_xattr_set_ctxt *ctxt)
834 unsigned int ext_flags;
835 u32 trunc_len, cpos, phys_cpos, alloc_size;
838 if (old_clusters <= new_clusters)
842 trunc_len = old_clusters - new_clusters;
844 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
846 &vb->vb_xv->xr_list, &ext_flags);
852 if (alloc_size > trunc_len)
853 alloc_size = trunc_len;
855 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
856 phys_cpos, alloc_size,
863 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
864 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
867 trunc_len -= alloc_size;
874 static int ocfs2_xattr_value_truncate(struct inode *inode,
875 struct ocfs2_xattr_value_buf *vb,
877 struct ocfs2_xattr_set_ctxt *ctxt)
880 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
881 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
883 if (new_clusters == old_clusters)
886 if (new_clusters > old_clusters)
887 ret = ocfs2_xattr_extend_allocation(inode,
888 new_clusters - old_clusters,
891 ret = ocfs2_xattr_shrink_size(inode,
892 old_clusters, new_clusters,
898 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
899 size_t *result, const char *prefix,
900 const char *name, int name_len)
902 char *p = buffer + *result;
903 int prefix_len = strlen(prefix);
904 int total_len = prefix_len + name_len + 1;
906 *result += total_len;
908 /* we are just looking for how big our buffer needs to be */
915 memcpy(p, prefix, prefix_len);
916 memcpy(p + prefix_len, name, name_len);
917 p[prefix_len + name_len] = '\0';
922 static int ocfs2_xattr_list_entries(struct inode *inode,
923 struct ocfs2_xattr_header *header,
924 char *buffer, size_t buffer_size)
928 const char *prefix, *name;
930 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
931 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
932 type = ocfs2_xattr_get_type(entry);
933 prefix = ocfs2_xattr_prefix(type);
936 name = (const char *)header +
937 le16_to_cpu(entry->xe_name_offset);
939 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
940 &result, prefix, name,
950 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
951 struct ocfs2_dinode *di)
953 struct ocfs2_xattr_header *xh;
956 xh = (struct ocfs2_xattr_header *)
957 ((void *)di + inode->i_sb->s_blocksize -
958 le16_to_cpu(di->i_xattr_inline_size));
960 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
961 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
967 static int ocfs2_xattr_ibody_list(struct inode *inode,
968 struct ocfs2_dinode *di,
972 struct ocfs2_xattr_header *header = NULL;
973 struct ocfs2_inode_info *oi = OCFS2_I(inode);
976 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
979 header = (struct ocfs2_xattr_header *)
980 ((void *)di + inode->i_sb->s_blocksize -
981 le16_to_cpu(di->i_xattr_inline_size));
983 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
988 static int ocfs2_xattr_block_list(struct inode *inode,
989 struct ocfs2_dinode *di,
993 struct buffer_head *blk_bh = NULL;
994 struct ocfs2_xattr_block *xb;
997 if (!di->i_xattr_loc)
1000 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
1007 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1008 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1009 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
1010 ret = ocfs2_xattr_list_entries(inode, header,
1011 buffer, buffer_size);
1013 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
1014 buffer, buffer_size);
1021 ssize_t ocfs2_listxattr(struct dentry *dentry,
1025 int ret = 0, i_ret = 0, b_ret = 0;
1026 struct buffer_head *di_bh = NULL;
1027 struct ocfs2_dinode *di = NULL;
1028 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1030 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1033 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1036 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1042 di = (struct ocfs2_dinode *)di_bh->b_data;
1044 down_read(&oi->ip_xattr_sem);
1045 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1053 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1058 up_read(&oi->ip_xattr_sem);
1059 ocfs2_inode_unlock(dentry->d_inode, 0);
1063 return i_ret + b_ret;
1066 static int ocfs2_xattr_find_entry(int name_index,
1068 struct ocfs2_xattr_search *xs)
1070 struct ocfs2_xattr_entry *entry;
1077 name_len = strlen(name);
1079 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1080 cmp = name_index - ocfs2_xattr_get_type(entry);
1082 cmp = name_len - entry->xe_name_len;
1084 cmp = memcmp(name, (xs->base +
1085 le16_to_cpu(entry->xe_name_offset)),
1093 return cmp ? -ENODATA : 0;
1096 static int ocfs2_xattr_get_value_outside(struct inode *inode,
1097 struct ocfs2_xattr_value_root *xv,
1101 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1104 size_t cplen, blocksize;
1105 struct buffer_head *bh = NULL;
1106 struct ocfs2_extent_list *el;
1109 clusters = le32_to_cpu(xv->xr_clusters);
1110 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1111 blocksize = inode->i_sb->s_blocksize;
1114 while (cpos < clusters) {
1115 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1116 &num_clusters, el, NULL);
1122 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1123 /* Copy ocfs2_xattr_value */
1124 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1125 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1132 cplen = len >= blocksize ? blocksize : len;
1133 memcpy(buffer, bh->b_data, cplen);
1142 cpos += num_clusters;
1148 static int ocfs2_xattr_ibody_get(struct inode *inode,
1153 struct ocfs2_xattr_search *xs)
1155 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1156 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1157 struct ocfs2_xattr_value_root *xv;
1161 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1164 xs->end = (void *)di + inode->i_sb->s_blocksize;
1165 xs->header = (struct ocfs2_xattr_header *)
1166 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1167 xs->base = (void *)xs->header;
1168 xs->here = xs->header->xh_entries;
1170 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1173 size = le64_to_cpu(xs->here->xe_value_size);
1175 if (size > buffer_size)
1177 if (ocfs2_xattr_is_local(xs->here)) {
1178 memcpy(buffer, (void *)xs->base +
1179 le16_to_cpu(xs->here->xe_name_offset) +
1180 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1182 xv = (struct ocfs2_xattr_value_root *)
1183 (xs->base + le16_to_cpu(
1184 xs->here->xe_name_offset) +
1185 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1186 ret = ocfs2_xattr_get_value_outside(inode, xv,
1198 static int ocfs2_xattr_block_get(struct inode *inode,
1203 struct ocfs2_xattr_search *xs)
1205 struct ocfs2_xattr_block *xb;
1206 struct ocfs2_xattr_value_root *xv;
1208 int ret = -ENODATA, name_offset, name_len, i;
1209 int uninitialized_var(block_off);
1211 xs->bucket = ocfs2_xattr_bucket_new(inode);
1218 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1224 if (xs->not_found) {
1229 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1230 size = le64_to_cpu(xs->here->xe_value_size);
1233 if (size > buffer_size)
1236 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1237 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1238 i = xs->here - xs->header->xh_entries;
1240 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1241 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1242 bucket_xh(xs->bucket),
1246 xs->base = bucket_block(xs->bucket, block_off);
1248 if (ocfs2_xattr_is_local(xs->here)) {
1249 memcpy(buffer, (void *)xs->base +
1250 name_offset + name_len, size);
1252 xv = (struct ocfs2_xattr_value_root *)
1253 (xs->base + name_offset + name_len);
1254 ret = ocfs2_xattr_get_value_outside(inode, xv,
1264 ocfs2_xattr_bucket_free(xs->bucket);
1266 brelse(xs->xattr_bh);
1267 xs->xattr_bh = NULL;
1271 int ocfs2_xattr_get_nolock(struct inode *inode,
1272 struct buffer_head *di_bh,
1279 struct ocfs2_dinode *di = NULL;
1280 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1281 struct ocfs2_xattr_search xis = {
1282 .not_found = -ENODATA,
1284 struct ocfs2_xattr_search xbs = {
1285 .not_found = -ENODATA,
1288 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1291 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1294 xis.inode_bh = xbs.inode_bh = di_bh;
1295 di = (struct ocfs2_dinode *)di_bh->b_data;
1297 down_read(&oi->ip_xattr_sem);
1298 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1300 if (ret == -ENODATA && di->i_xattr_loc)
1301 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1303 up_read(&oi->ip_xattr_sem);
1308 /* ocfs2_xattr_get()
1310 * Copy an extended attribute into the buffer provided.
1311 * Buffer is NULL to compute the size of buffer required.
1313 static int ocfs2_xattr_get(struct inode *inode,
1320 struct buffer_head *di_bh = NULL;
1322 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1327 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1328 name, buffer, buffer_size);
1330 ocfs2_inode_unlock(inode, 0);
1337 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1339 struct ocfs2_xattr_value_buf *vb,
1343 int ret = 0, i, cp_len;
1344 u16 blocksize = inode->i_sb->s_blocksize;
1345 u32 p_cluster, num_clusters;
1346 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1347 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1349 struct buffer_head *bh = NULL;
1350 unsigned int ext_flags;
1351 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1353 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1355 while (cpos < clusters) {
1356 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1357 &num_clusters, &xv->xr_list,
1364 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1366 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1368 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1369 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1376 ret = ocfs2_journal_access(handle,
1379 OCFS2_JOURNAL_ACCESS_WRITE);
1385 cp_len = value_len > blocksize ? blocksize : value_len;
1386 memcpy(bh->b_data, value, cp_len);
1387 value_len -= cp_len;
1389 if (cp_len < blocksize)
1390 memset(bh->b_data + cp_len, 0,
1391 blocksize - cp_len);
1393 ret = ocfs2_journal_dirty(handle, bh);
1402 * XXX: do we need to empty all the following
1403 * blocks in this cluster?
1408 cpos += num_clusters;
1416 static int ocfs2_xattr_cleanup(struct inode *inode,
1418 struct ocfs2_xattr_info *xi,
1419 struct ocfs2_xattr_search *xs,
1420 struct ocfs2_xattr_value_buf *vb,
1424 void *val = xs->base + offs;
1425 size_t size = namevalue_size_xi(xi);
1427 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1428 OCFS2_JOURNAL_ACCESS_WRITE);
1433 /* Decrease xattr count */
1434 le16_add_cpu(&xs->header->xh_count, -1);
1435 /* Remove the xattr entry and tree root which has already be set*/
1436 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1437 memset(val, 0, size);
1439 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1446 static int ocfs2_xattr_update_entry(struct inode *inode,
1448 struct ocfs2_xattr_info *xi,
1449 struct ocfs2_xattr_search *xs,
1450 struct ocfs2_xattr_value_buf *vb,
1455 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1456 OCFS2_JOURNAL_ACCESS_WRITE);
1462 xs->here->xe_name_offset = cpu_to_le16(offs);
1463 xs->here->xe_value_size = cpu_to_le64(xi->xi_value_len);
1464 if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
1465 ocfs2_xattr_set_local(xs->here, 1);
1467 ocfs2_xattr_set_local(xs->here, 0);
1468 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1470 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1478 * ocfs2_xattr_set_value_outside()
1480 * Set large size value in B tree.
1482 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1483 struct ocfs2_xattr_info *xi,
1484 struct ocfs2_xattr_search *xs,
1485 struct ocfs2_xattr_set_ctxt *ctxt,
1486 struct ocfs2_xattr_value_buf *vb,
1489 void *val = xs->base + offs;
1490 struct ocfs2_xattr_value_root *xv = NULL;
1491 size_t size = namevalue_size_xi(xi);
1494 memset(val, 0, size);
1495 memcpy(val, xi->xi_name, xi->xi_name_len);
1496 xv = (struct ocfs2_xattr_value_root *)
1497 (val + OCFS2_XATTR_SIZE(xi->xi_name_len));
1498 xv->xr_clusters = 0;
1499 xv->xr_last_eb_blk = 0;
1500 xv->xr_list.l_tree_depth = 0;
1501 xv->xr_list.l_count = cpu_to_le16(1);
1502 xv->xr_list.l_next_free_rec = 0;
1505 ret = ocfs2_xattr_value_truncate(inode, vb, xi->xi_value_len, ctxt);
1510 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
1515 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
1516 xi->xi_value, xi->xi_value_len);
1523 static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1531 free_space = free_start -
1532 sizeof(struct ocfs2_xattr_header) -
1533 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1534 OCFS2_XATTR_HEADER_GAP;
1537 if (free_space < needed_space)
1543 /* Give a pointer into the storage for the given offset */
1544 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1546 BUG_ON(offset >= loc->xl_size);
1547 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1551 * Wipe the name+value pair and allow the storage to reclaim it. This
1552 * must be followed by either removal of the entry or a call to
1553 * ocfs2_xa_add_namevalue().
1555 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1557 loc->xl_ops->xlo_wipe_namevalue(loc);
1561 * Find lowest offset to a name+value pair. This is the start of our
1562 * downward-growing free space.
1564 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1566 return loc->xl_ops->xlo_get_free_start(loc);
1569 /* Can we reuse loc->xl_entry for xi? */
1570 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1571 struct ocfs2_xattr_info *xi)
1573 return loc->xl_ops->xlo_can_reuse(loc, xi);
1576 /* How much free space is needed to set the new value */
1577 static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1578 struct ocfs2_xattr_info *xi)
1580 return loc->xl_ops->xlo_check_space(loc, xi);
1583 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1585 loc->xl_ops->xlo_add_entry(loc, name_hash);
1586 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1588 * We can't leave the new entry's xe_name_offset at zero or
1589 * add_namevalue() will go nuts. We set it to the size of our
1590 * storage so that it can never be less than any other entry.
1592 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1595 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1596 struct ocfs2_xattr_info *xi)
1598 int size = namevalue_size_xi(xi);
1602 loc->xl_ops->xlo_add_namevalue(loc, size);
1603 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1604 loc->xl_entry->xe_name_len = xi->xi_name_len;
1605 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1606 ocfs2_xattr_set_local(loc->xl_entry,
1607 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1609 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1610 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1611 memset(nameval_buf, 0, size);
1612 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1615 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1616 struct ocfs2_xattr_value_buf *vb)
1618 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1619 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1621 /* Value bufs are for value trees */
1622 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1623 (name_size + OCFS2_XATTR_ROOT_SIZE));
1625 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1627 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1632 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1635 return (char *)loc->xl_header + offset;
1638 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1639 struct ocfs2_xattr_info *xi)
1642 * Block storage is strict. If the sizes aren't exact, we will
1643 * remove the old one and reinsert the new.
1645 return namevalue_size_xe(loc->xl_entry) ==
1646 namevalue_size_xi(xi);
1649 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1651 struct ocfs2_xattr_header *xh = loc->xl_header;
1652 int i, count = le16_to_cpu(xh->xh_count);
1653 int offset, free_start = loc->xl_size;
1655 for (i = 0; i < count; i++) {
1656 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1657 if (offset < free_start)
1658 free_start = offset;
1664 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1665 struct ocfs2_xattr_info *xi)
1667 int count = le16_to_cpu(loc->xl_header->xh_count);
1668 int free_start = ocfs2_xa_get_free_start(loc);
1669 int needed_space = ocfs2_xi_entry_usage(xi);
1672 * Block storage will reclaim the original entry before inserting
1673 * the new value, so we only need the difference. If the new
1674 * entry is smaller than the old one, we don't need anything.
1676 if (loc->xl_entry) {
1677 /* Don't need space if we're reusing! */
1678 if (ocfs2_xa_can_reuse_entry(loc, xi))
1681 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1683 if (needed_space < 0)
1685 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1689 * Block storage for xattrs keeps the name+value pairs compacted. When
1690 * we remove one, we have to shift any that preceded it towards the end.
1692 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1695 int namevalue_offset, first_namevalue_offset, namevalue_size;
1696 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1697 struct ocfs2_xattr_header *xh = loc->xl_header;
1698 int count = le16_to_cpu(xh->xh_count);
1700 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1701 namevalue_size = namevalue_size_xe(entry);
1702 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1704 /* Shift the name+value pairs */
1705 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1706 (char *)xh + first_namevalue_offset,
1707 namevalue_offset - first_namevalue_offset);
1708 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1710 /* Now tell xh->xh_entries about it */
1711 for (i = 0; i < count; i++) {
1712 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1713 if (offset < namevalue_offset)
1714 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1719 * Note that we don't update xh_free_start or xh_name_value_len
1720 * because they're not used in block-stored xattrs.
1724 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1726 int count = le16_to_cpu(loc->xl_header->xh_count);
1727 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1728 le16_add_cpu(&loc->xl_header->xh_count, 1);
1729 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1732 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1734 int free_start = ocfs2_xa_get_free_start(loc);
1736 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1739 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1740 struct ocfs2_xattr_value_buf *vb)
1742 struct buffer_head *bh = loc->xl_storage;
1744 if (loc->xl_size == (bh->b_size -
1745 offsetof(struct ocfs2_xattr_block,
1746 xb_attrs.xb_header)))
1747 vb->vb_access = ocfs2_journal_access_xb;
1749 vb->vb_access = ocfs2_journal_access_di;
1754 * Operations for xattrs stored in blocks. This includes inline inode
1755 * storage and unindexed ocfs2_xattr_blocks.
1757 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1758 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
1759 .xlo_check_space = ocfs2_xa_block_check_space,
1760 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1761 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
1762 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
1763 .xlo_add_entry = ocfs2_xa_block_add_entry,
1764 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
1765 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
1768 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1771 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1772 int block, block_offset;
1774 /* The header is at the front of the bucket */
1775 block = offset >> bucket->bu_inode->i_sb->s_blocksize_bits;
1776 block_offset = offset % bucket->bu_inode->i_sb->s_blocksize;
1778 return bucket_block(bucket, block) + block_offset;
1781 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1782 struct ocfs2_xattr_info *xi)
1784 return namevalue_size_xe(loc->xl_entry) >=
1785 namevalue_size_xi(xi);
1788 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1790 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1791 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1794 static int ocfs2_bucket_align_free_start(struct super_block *sb,
1795 int free_start, int size)
1798 * We need to make sure that the name+value pair fits within
1801 if (((free_start - size) >> sb->s_blocksize_bits) !=
1802 ((free_start - 1) >> sb->s_blocksize_bits))
1803 free_start -= free_start % sb->s_blocksize;
1808 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1809 struct ocfs2_xattr_info *xi)
1812 int count = le16_to_cpu(loc->xl_header->xh_count);
1813 int free_start = ocfs2_xa_get_free_start(loc);
1814 int needed_space = ocfs2_xi_entry_usage(xi);
1815 int size = namevalue_size_xi(xi);
1816 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1817 struct super_block *sb = bucket->bu_inode->i_sb;
1820 * Bucket storage does not reclaim name+value pairs it cannot
1821 * reuse. They live as holes until the bucket fills, and then
1822 * the bucket is defragmented. However, the bucket can reclaim
1823 * the ocfs2_xattr_entry.
1825 if (loc->xl_entry) {
1826 /* Don't need space if we're reusing! */
1827 if (ocfs2_xa_can_reuse_entry(loc, xi))
1830 needed_space -= sizeof(struct ocfs2_xattr_entry);
1832 BUG_ON(needed_space < 0);
1834 if (free_start < size) {
1839 * First we check if it would fit in the first place.
1840 * Below, we align the free start to a block. This may
1841 * slide us below the minimum gap. By checking unaligned
1842 * first, we avoid that error.
1844 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1848 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1851 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1854 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1856 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1857 -namevalue_size_xe(loc->xl_entry));
1860 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1862 struct ocfs2_xattr_header *xh = loc->xl_header;
1863 int count = le16_to_cpu(xh->xh_count);
1864 int low = 0, high = count - 1, tmp;
1865 struct ocfs2_xattr_entry *tmp_xe;
1868 * We keep buckets sorted by name_hash, so we need to find
1871 while (low <= high && count) {
1872 tmp = (low + high) / 2;
1873 tmp_xe = &xh->xh_entries[tmp];
1875 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1877 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1886 memmove(&xh->xh_entries[low + 1],
1887 &xh->xh_entries[low],
1888 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1890 le16_add_cpu(&xh->xh_count, 1);
1891 loc->xl_entry = &xh->xh_entries[low];
1892 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1895 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1897 int free_start = ocfs2_xa_get_free_start(loc);
1898 struct ocfs2_xattr_header *xh = loc->xl_header;
1899 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1900 struct super_block *sb = bucket->bu_inode->i_sb;
1903 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1904 nameval_offset = free_start - size;
1905 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1906 xh->xh_free_start = cpu_to_le16(nameval_offset);
1907 le16_add_cpu(&xh->xh_name_value_len, size);
1911 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1912 struct ocfs2_xattr_value_buf *vb)
1914 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1915 struct super_block *sb = bucket->bu_inode->i_sb;
1916 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1917 int size = namevalue_size_xe(loc->xl_entry);
1918 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1920 /* Values are not allowed to straddle block boundaries */
1921 BUG_ON(block_offset !=
1922 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1923 /* We expect the bucket to be filled in */
1924 BUG_ON(!bucket->bu_bhs[block_offset]);
1926 vb->vb_access = ocfs2_journal_access;
1927 vb->vb_bh = bucket->bu_bhs[block_offset];
1930 /* Operations for xattrs stored in buckets. */
1931 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1932 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
1933 .xlo_check_space = ocfs2_xa_bucket_check_space,
1934 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1935 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
1936 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
1937 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1938 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
1939 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
1942 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1945 struct ocfs2_xattr_header *xh = loc->xl_header;
1946 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1948 ocfs2_xa_wipe_namevalue(loc);
1949 loc->xl_entry = NULL;
1951 le16_add_cpu(&xh->xh_count, -1);
1952 count = le16_to_cpu(xh->xh_count);
1955 * Only zero out the entry if there are more remaining. This is
1956 * important for an empty bucket, as it keeps track of the
1957 * bucket's hash value. It doesn't hurt empty block storage.
1960 index = ((char *)entry - (char *)&xh->xh_entries) /
1961 sizeof(struct ocfs2_xattr_entry);
1962 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1963 (count - index) * sizeof(struct ocfs2_xattr_entry));
1964 memset(&xh->xh_entries[count], 0,
1965 sizeof(struct ocfs2_xattr_entry));
1970 * Prepares loc->xl_entry to receive the new xattr. This includes
1971 * properly setting up the name+value pair region. If loc->xl_entry
1972 * already exists, it will take care of modifying it appropriately.
1973 * This also includes deleting entries, but don't call this to remove
1974 * a non-existant entry. That's just a bug.
1976 * Note that this modifies the data. You did journal_access already,
1979 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
1980 struct ocfs2_xattr_info *xi,
1984 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
1987 if (!xi->xi_value) {
1988 ocfs2_xa_remove_entry(loc);
1992 rc = ocfs2_xa_check_space(loc, xi);
1996 if (loc->xl_entry) {
1997 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
1998 nameval_buf = ocfs2_xa_offset_pointer(loc,
1999 le16_to_cpu(loc->xl_entry->xe_name_offset));
2000 memset(nameval_buf + name_size, 0,
2001 namevalue_size_xe(loc->xl_entry) - name_size);
2002 loc->xl_entry->xe_value_size =
2003 cpu_to_le64(xi->xi_value_len);
2007 ocfs2_xa_wipe_namevalue(loc);
2009 ocfs2_xa_add_entry(loc, name_hash);
2012 * If we get here, we have a blank entry. Fill it. We grow our
2013 * name+value pair back from the end.
2015 ocfs2_xa_add_namevalue(loc, xi);
2022 * Store the value portion of the name+value pair. This is either an
2023 * inline value or the tree root of an external value.
2025 static void ocfs2_xa_store_inline_value(struct ocfs2_xa_loc *loc,
2026 struct ocfs2_xattr_info *xi)
2028 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2029 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2030 int inline_value_size = namevalue_size_xi(xi) - name_size;
2031 const void *value = xi->xi_value;
2037 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2039 inline_value_size = OCFS2_XATTR_ROOT_SIZE;
2041 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
2042 memcpy(nameval_buf + name_size, value, inline_value_size);
2045 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2046 struct inode *inode,
2047 struct buffer_head *bh,
2048 struct ocfs2_xattr_entry *entry)
2050 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2052 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2053 loc->xl_storage = bh;
2054 loc->xl_entry = entry;
2056 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2057 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2060 loc->xl_size = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2063 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2067 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2068 struct buffer_head *bh,
2069 struct ocfs2_xattr_entry *entry)
2071 struct ocfs2_xattr_block *xb =
2072 (struct ocfs2_xattr_block *)bh->b_data;
2074 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2076 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2077 loc->xl_storage = bh;
2078 loc->xl_header = &(xb->xb_attrs.xb_header);
2079 loc->xl_entry = entry;
2080 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2081 xb_attrs.xb_header);
2084 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2085 struct ocfs2_xattr_bucket *bucket,
2086 struct ocfs2_xattr_entry *entry)
2088 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2089 loc->xl_storage = bucket;
2090 loc->xl_header = bucket_xh(bucket);
2091 loc->xl_entry = entry;
2092 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2097 * ocfs2_xattr_set_entry()
2099 * Set extended attribute entry into inode or block.
2101 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
2102 * We first insert tree root(ocfs2_xattr_value_root) like a normal value,
2103 * then set value in B tree with set_value_outside().
2105 static int ocfs2_xattr_set_entry(struct inode *inode,
2106 struct ocfs2_xattr_info *xi,
2107 struct ocfs2_xattr_search *xs,
2108 struct ocfs2_xattr_set_ctxt *ctxt,
2111 struct ocfs2_xattr_entry *last;
2112 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2113 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2114 size_t min_offs = xs->end - xs->base;
2116 handle_t *handle = ctxt->handle;
2118 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
2120 struct ocfs2_xa_loc loc;
2121 struct ocfs2_xattr_value_buf vb = {
2122 .vb_bh = xs->xattr_bh,
2123 .vb_access = ocfs2_journal_access_di,
2126 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
2127 BUG_ON(xs->xattr_bh == xs->inode_bh);
2128 vb.vb_access = ocfs2_journal_access_xb;
2130 BUG_ON(xs->xattr_bh != xs->inode_bh);
2132 /* Compute min_offs, last and free space. */
2133 last = xs->header->xh_entries;
2135 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
2136 size_t offs = le16_to_cpu(last->xe_name_offset);
2137 if (offs < min_offs)
2142 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2147 free += ocfs2_xe_entry_usage(xs->here);
2149 /* Check free space in inode or block */
2150 if (xi->xi_value && (free < ocfs2_xi_entry_usage(xi))) {
2155 if (!xs->not_found) {
2156 /* For existing extended attribute */
2157 size_t size = namevalue_size_xe(xs->here);
2158 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
2159 void *val = xs->base + offs;
2161 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
2162 /* Replace existing local xattr with tree root */
2163 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
2168 } else if (!ocfs2_xattr_is_local(xs->here)) {
2169 /* For existing xattr which has value outside */
2170 vb.vb_xv = (struct ocfs2_xattr_value_root *)
2171 (val + OCFS2_XATTR_SIZE(xi->xi_name_len));
2173 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2175 * If new value need set outside also,
2176 * first truncate old value to new value,
2177 * then set new value with set_value_outside().
2179 ret = ocfs2_xattr_value_truncate(inode,
2188 ret = ocfs2_xattr_update_entry(inode,
2199 ret = __ocfs2_xattr_set_value_outside(inode,
2209 * If new value need set in local,
2210 * just trucate old value to zero.
2212 ret = ocfs2_xattr_value_truncate(inode,
2222 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
2223 OCFS2_JOURNAL_ACCESS_WRITE);
2229 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
2230 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
2231 OCFS2_JOURNAL_ACCESS_WRITE);
2238 if (xs->xattr_bh == xs->inode_bh)
2239 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2240 xs->not_found ? NULL : xs->here);
2242 ocfs2_init_xattr_block_xa_loc(&loc, xs->xattr_bh,
2243 xs->not_found ? NULL : xs->here);
2246 * Prepare our entry and insert the inline value. This will
2247 * be a value tree root for values that are larger than
2248 * OCFS2_XATTR_INLINE_SIZE.
2250 ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash);
2256 /* XXX For now, until we make ocfs2_xa_prepare_entry() primary */
2257 BUG_ON(ret == -ENOSPC);
2258 ocfs2_xa_store_inline_value(&loc, xi);
2259 xs->here = loc.xl_entry;
2261 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
2262 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
2269 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
2270 (flag & OCFS2_INLINE_XATTR_FL)) {
2271 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2272 unsigned int xattrsize = osb->s_xattr_inline_size;
2275 * Adjust extent record count or inline data size
2276 * to reserve space for extended attribute.
2278 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2279 struct ocfs2_inline_data *idata = &di->id2.i_data;
2280 le16_add_cpu(&idata->id_count, -xattrsize);
2281 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2282 struct ocfs2_extent_list *el = &di->id2.i_list;
2283 le16_add_cpu(&el->l_count, -(xattrsize /
2284 sizeof(struct ocfs2_extent_rec)));
2286 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2288 /* Update xattr flag */
2289 spin_lock(&oi->ip_lock);
2290 oi->ip_dyn_features |= flag;
2291 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2292 spin_unlock(&oi->ip_lock);
2294 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
2298 if (!ret && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2300 * Set value outside in B tree.
2301 * This is the second step for value size > INLINE_SIZE.
2303 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
2304 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
2311 * If set value outside failed, we have to clean
2312 * the junk tree root we have already set in local.
2314 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
2325 * In xattr remove, if it is stored outside and refcounted, we may have
2326 * the chance to split the refcount tree. So need the allocators.
2328 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2329 struct ocfs2_xattr_value_root *xv,
2330 struct ocfs2_caching_info *ref_ci,
2331 struct buffer_head *ref_root_bh,
2332 struct ocfs2_alloc_context **meta_ac,
2335 int ret, meta_add = 0;
2336 u32 p_cluster, num_clusters;
2337 unsigned int ext_flags;
2340 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2349 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2352 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2354 &meta_add, ref_credits);
2360 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2369 static int ocfs2_remove_value_outside(struct inode*inode,
2370 struct ocfs2_xattr_value_buf *vb,
2371 struct ocfs2_xattr_header *header,
2372 struct ocfs2_caching_info *ref_ci,
2373 struct buffer_head *ref_root_bh)
2375 int ret = 0, i, ref_credits;
2376 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2377 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2380 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2382 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2383 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2385 if (ocfs2_xattr_is_local(entry))
2388 val = (void *)header +
2389 le16_to_cpu(entry->xe_name_offset);
2390 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2391 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2393 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2394 ref_ci, ref_root_bh,
2398 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2399 ocfs2_remove_extent_credits(osb->sb));
2400 if (IS_ERR(ctxt.handle)) {
2401 ret = PTR_ERR(ctxt.handle);
2406 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2412 ocfs2_commit_trans(osb, ctxt.handle);
2414 ocfs2_free_alloc_context(ctxt.meta_ac);
2415 ctxt.meta_ac = NULL;
2420 ocfs2_free_alloc_context(ctxt.meta_ac);
2421 ocfs2_schedule_truncate_log_flush(osb, 1);
2422 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2426 static int ocfs2_xattr_ibody_remove(struct inode *inode,
2427 struct buffer_head *di_bh,
2428 struct ocfs2_caching_info *ref_ci,
2429 struct buffer_head *ref_root_bh)
2432 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2433 struct ocfs2_xattr_header *header;
2435 struct ocfs2_xattr_value_buf vb = {
2437 .vb_access = ocfs2_journal_access_di,
2440 header = (struct ocfs2_xattr_header *)
2441 ((void *)di + inode->i_sb->s_blocksize -
2442 le16_to_cpu(di->i_xattr_inline_size));
2444 ret = ocfs2_remove_value_outside(inode, &vb, header,
2445 ref_ci, ref_root_bh);
2450 struct ocfs2_rm_xattr_bucket_para {
2451 struct ocfs2_caching_info *ref_ci;
2452 struct buffer_head *ref_root_bh;
2455 static int ocfs2_xattr_block_remove(struct inode *inode,
2456 struct buffer_head *blk_bh,
2457 struct ocfs2_caching_info *ref_ci,
2458 struct buffer_head *ref_root_bh)
2460 struct ocfs2_xattr_block *xb;
2462 struct ocfs2_xattr_value_buf vb = {
2464 .vb_access = ocfs2_journal_access_xb,
2466 struct ocfs2_rm_xattr_bucket_para args = {
2468 .ref_root_bh = ref_root_bh,
2471 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2472 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2473 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2474 ret = ocfs2_remove_value_outside(inode, &vb, header,
2475 ref_ci, ref_root_bh);
2477 ret = ocfs2_iterate_xattr_index_block(inode,
2479 ocfs2_rm_xattr_cluster,
2485 static int ocfs2_xattr_free_block(struct inode *inode,
2487 struct ocfs2_caching_info *ref_ci,
2488 struct buffer_head *ref_root_bh)
2490 struct inode *xb_alloc_inode;
2491 struct buffer_head *xb_alloc_bh = NULL;
2492 struct buffer_head *blk_bh = NULL;
2493 struct ocfs2_xattr_block *xb;
2494 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2500 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2506 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2512 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2513 blk = le64_to_cpu(xb->xb_blkno);
2514 bit = le16_to_cpu(xb->xb_suballoc_bit);
2515 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2517 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2518 EXTENT_ALLOC_SYSTEM_INODE,
2519 le16_to_cpu(xb->xb_suballoc_slot));
2520 if (!xb_alloc_inode) {
2525 mutex_lock(&xb_alloc_inode->i_mutex);
2527 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2533 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2534 if (IS_ERR(handle)) {
2535 ret = PTR_ERR(handle);
2540 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2545 ocfs2_commit_trans(osb, handle);
2547 ocfs2_inode_unlock(xb_alloc_inode, 1);
2548 brelse(xb_alloc_bh);
2550 mutex_unlock(&xb_alloc_inode->i_mutex);
2551 iput(xb_alloc_inode);
2558 * ocfs2_xattr_remove()
2560 * Free extended attribute resources associated with this inode.
2562 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2564 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2565 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2566 struct ocfs2_refcount_tree *ref_tree = NULL;
2567 struct buffer_head *ref_root_bh = NULL;
2568 struct ocfs2_caching_info *ref_ci = NULL;
2572 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2575 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2578 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2579 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2580 le64_to_cpu(di->i_refcount_loc),
2581 1, &ref_tree, &ref_root_bh);
2586 ref_ci = &ref_tree->rf_ci;
2590 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2591 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2592 ref_ci, ref_root_bh);
2599 if (di->i_xattr_loc) {
2600 ret = ocfs2_xattr_free_block(inode,
2601 le64_to_cpu(di->i_xattr_loc),
2602 ref_ci, ref_root_bh);
2609 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2610 OCFS2_INODE_UPDATE_CREDITS);
2611 if (IS_ERR(handle)) {
2612 ret = PTR_ERR(handle);
2616 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2617 OCFS2_JOURNAL_ACCESS_WRITE);
2623 di->i_xattr_loc = 0;
2625 spin_lock(&oi->ip_lock);
2626 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2627 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2628 spin_unlock(&oi->ip_lock);
2630 ret = ocfs2_journal_dirty(handle, di_bh);
2634 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2637 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2638 brelse(ref_root_bh);
2642 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2643 struct ocfs2_dinode *di)
2645 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2646 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2649 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2652 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2653 struct ocfs2_inline_data *idata = &di->id2.i_data;
2654 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2655 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2656 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2657 le64_to_cpu(di->i_size);
2659 struct ocfs2_extent_list *el = &di->id2.i_list;
2660 free = (le16_to_cpu(el->l_count) -
2661 le16_to_cpu(el->l_next_free_rec)) *
2662 sizeof(struct ocfs2_extent_rec);
2664 if (free >= xattrsize)
2671 * ocfs2_xattr_ibody_find()
2673 * Find extended attribute in inode block and
2674 * fill search info into struct ocfs2_xattr_search.
2676 static int ocfs2_xattr_ibody_find(struct inode *inode,
2679 struct ocfs2_xattr_search *xs)
2681 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2682 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2686 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2689 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2690 down_read(&oi->ip_alloc_sem);
2691 has_space = ocfs2_xattr_has_space_inline(inode, di);
2692 up_read(&oi->ip_alloc_sem);
2697 xs->xattr_bh = xs->inode_bh;
2698 xs->end = (void *)di + inode->i_sb->s_blocksize;
2699 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2700 xs->header = (struct ocfs2_xattr_header *)
2701 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2703 xs->header = (struct ocfs2_xattr_header *)
2704 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2705 xs->base = (void *)xs->header;
2706 xs->here = xs->header->xh_entries;
2708 /* Find the named attribute. */
2709 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2710 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2711 if (ret && ret != -ENODATA)
2713 xs->not_found = ret;
2720 * ocfs2_xattr_ibody_set()
2722 * Set, replace or remove an extended attribute into inode block.
2725 static int ocfs2_xattr_ibody_set(struct inode *inode,
2726 struct ocfs2_xattr_info *xi,
2727 struct ocfs2_xattr_search *xs,
2728 struct ocfs2_xattr_set_ctxt *ctxt)
2730 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2731 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2734 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2737 down_write(&oi->ip_alloc_sem);
2738 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2739 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2745 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2746 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2748 up_write(&oi->ip_alloc_sem);
2754 * ocfs2_xattr_block_find()
2756 * Find extended attribute in external block and
2757 * fill search info into struct ocfs2_xattr_search.
2759 static int ocfs2_xattr_block_find(struct inode *inode,
2762 struct ocfs2_xattr_search *xs)
2764 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2765 struct buffer_head *blk_bh = NULL;
2766 struct ocfs2_xattr_block *xb;
2769 if (!di->i_xattr_loc)
2772 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2779 xs->xattr_bh = blk_bh;
2780 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2782 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2783 xs->header = &xb->xb_attrs.xb_header;
2784 xs->base = (void *)xs->header;
2785 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2786 xs->here = xs->header->xh_entries;
2788 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2790 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2794 if (ret && ret != -ENODATA) {
2795 xs->xattr_bh = NULL;
2798 xs->not_found = ret;
2806 static int ocfs2_create_xattr_block(handle_t *handle,
2807 struct inode *inode,
2808 struct buffer_head *inode_bh,
2809 struct ocfs2_alloc_context *meta_ac,
2810 struct buffer_head **ret_bh,
2814 u16 suballoc_bit_start;
2817 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2818 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2819 struct buffer_head *new_bh = NULL;
2820 struct ocfs2_xattr_block *xblk;
2822 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2823 OCFS2_JOURNAL_ACCESS_CREATE);
2829 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2830 &suballoc_bit_start, &num_got,
2837 new_bh = sb_getblk(inode->i_sb, first_blkno);
2838 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2840 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2842 OCFS2_JOURNAL_ACCESS_CREATE);
2848 /* Initialize ocfs2_xattr_block */
2849 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2850 memset(xblk, 0, inode->i_sb->s_blocksize);
2851 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2852 xblk->xb_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
2853 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2854 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2855 xblk->xb_blkno = cpu_to_le64(first_blkno);
2858 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2859 xr->xt_clusters = cpu_to_le32(1);
2860 xr->xt_last_eb_blk = 0;
2861 xr->xt_list.l_tree_depth = 0;
2862 xr->xt_list.l_count = cpu_to_le16(
2863 ocfs2_xattr_recs_per_xb(inode->i_sb));
2864 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2865 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2868 ret = ocfs2_journal_dirty(handle, new_bh);
2873 di->i_xattr_loc = cpu_to_le64(first_blkno);
2874 ocfs2_journal_dirty(handle, inode_bh);
2885 * ocfs2_xattr_block_set()
2887 * Set, replace or remove an extended attribute into external block.
2890 static int ocfs2_xattr_block_set(struct inode *inode,
2891 struct ocfs2_xattr_info *xi,
2892 struct ocfs2_xattr_search *xs,
2893 struct ocfs2_xattr_set_ctxt *ctxt)
2895 struct buffer_head *new_bh = NULL;
2896 handle_t *handle = ctxt->handle;
2897 struct ocfs2_xattr_block *xblk = NULL;
2900 if (!xs->xattr_bh) {
2901 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
2902 ctxt->meta_ac, &new_bh, 0);
2908 xs->xattr_bh = new_bh;
2909 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2910 xs->header = &xblk->xb_attrs.xb_header;
2911 xs->base = (void *)xs->header;
2912 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2913 xs->here = xs->header->xh_entries;
2915 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2917 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2918 /* Set extended attribute into external block */
2919 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2920 OCFS2_HAS_XATTR_FL);
2921 if (!ret || ret != -ENOSPC)
2924 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2929 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2936 /* Check whether the new xattr can be inserted into the inode. */
2937 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2938 struct ocfs2_xattr_info *xi,
2939 struct ocfs2_xattr_search *xs)
2941 struct ocfs2_xattr_entry *last;
2943 size_t min_offs = xs->end - xs->base;
2948 last = xs->header->xh_entries;
2950 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2951 size_t offs = le16_to_cpu(last->xe_name_offset);
2952 if (offs < min_offs)
2957 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2961 BUG_ON(!xs->not_found);
2963 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
2969 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2970 struct ocfs2_dinode *di,
2971 struct ocfs2_xattr_info *xi,
2972 struct ocfs2_xattr_search *xis,
2973 struct ocfs2_xattr_search *xbs,
2978 int ret = 0, old_in_xb = 0;
2979 int clusters_add = 0, meta_add = 0, credits = 0;
2980 struct buffer_head *bh = NULL;
2981 struct ocfs2_xattr_block *xb = NULL;
2982 struct ocfs2_xattr_entry *xe = NULL;
2983 struct ocfs2_xattr_value_root *xv = NULL;
2985 int name_offset, name_len = 0;
2986 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2991 * Calculate the clusters we need to write.
2992 * No matter whether we replace an old one or add a new one,
2993 * we need this for writing.
2995 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2996 credits += new_clusters *
2997 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2999 if (xis->not_found && xbs->not_found) {
3000 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3002 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3003 clusters_add += new_clusters;
3004 credits += ocfs2_calc_extend_credits(inode->i_sb,
3012 if (!xis->not_found) {
3014 name_offset = le16_to_cpu(xe->xe_name_offset);
3015 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3017 credits += OCFS2_INODE_UPDATE_CREDITS;
3019 int i, block_off = 0;
3020 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3022 name_offset = le16_to_cpu(xe->xe_name_offset);
3023 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3024 i = xbs->here - xbs->header->xh_entries;
3027 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3028 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3029 bucket_xh(xbs->bucket),
3032 base = bucket_block(xbs->bucket, block_off);
3033 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3036 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3041 * delete a xattr doesn't need metadata and cluster allocation.
3042 * so just calculate the credits and return.
3044 * The credits for removing the value tree will be extended
3045 * by ocfs2_remove_extent itself.
3047 if (!xi->xi_value) {
3048 if (!ocfs2_xattr_is_local(xe))
3049 credits += ocfs2_remove_extent_credits(inode->i_sb);
3054 /* do cluster allocation guess first. */
3055 value_size = le64_to_cpu(xe->xe_value_size);
3059 * In xattr set, we always try to set the xe in inode first,
3060 * so if it can be inserted into inode successfully, the old
3061 * one will be removed from the xattr block, and this xattr
3062 * will be inserted into inode as a new xattr in inode.
3064 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3065 clusters_add += new_clusters;
3066 credits += ocfs2_remove_extent_credits(inode->i_sb) +
3067 OCFS2_INODE_UPDATE_CREDITS;
3068 if (!ocfs2_xattr_is_local(xe))
3069 credits += ocfs2_calc_extend_credits(
3077 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3078 /* the new values will be stored outside. */
3079 u32 old_clusters = 0;
3081 if (!ocfs2_xattr_is_local(xe)) {
3082 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3084 xv = (struct ocfs2_xattr_value_root *)
3085 (base + name_offset + name_len);
3086 value_size = OCFS2_XATTR_ROOT_SIZE;
3090 if (old_clusters >= new_clusters) {
3091 credits += ocfs2_remove_extent_credits(inode->i_sb);
3094 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3095 clusters_add += new_clusters - old_clusters;
3096 credits += ocfs2_calc_extend_credits(inode->i_sb,
3100 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3105 * Now the new value will be stored inside. So if the new
3106 * value is smaller than the size of value root or the old
3107 * value, we don't need any allocation, otherwise we have
3108 * to guess metadata allocation.
3110 if ((ocfs2_xattr_is_local(xe) &&
3111 (value_size >= xi->xi_value_len)) ||
3112 (!ocfs2_xattr_is_local(xe) &&
3113 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
3118 /* calculate metadata allocation. */
3119 if (di->i_xattr_loc) {
3120 if (!xbs->xattr_bh) {
3121 ret = ocfs2_read_xattr_block(inode,
3122 le64_to_cpu(di->i_xattr_loc),
3129 xb = (struct ocfs2_xattr_block *)bh->b_data;
3131 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3134 * If there is already an xattr tree, good, we can calculate
3135 * like other b-trees. Otherwise we may have the chance of
3136 * create a tree, the credit calculation is borrowed from
3137 * ocfs2_calc_extend_credits with root_el = NULL. And the
3138 * new tree will be cluster based, so no meta is needed.
3140 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3141 struct ocfs2_extent_list *el =
3142 &xb->xb_attrs.xb_root.xt_list;
3143 meta_add += ocfs2_extend_meta_needed(el);
3144 credits += ocfs2_calc_extend_credits(inode->i_sb,
3147 credits += OCFS2_SUBALLOC_ALLOC + 1;
3150 * This cluster will be used either for new bucket or for
3152 * If the cluster size is the same as the bucket size, one
3153 * more is needed since we may need to extend the bucket
3157 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3158 if (OCFS2_XATTR_BUCKET_SIZE ==
3159 OCFS2_SB(inode->i_sb)->s_clustersize) {
3160 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3165 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3169 *clusters_need = clusters_add;
3171 *meta_need = meta_add;
3173 *credits_need = credits;
3178 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3179 struct ocfs2_dinode *di,
3180 struct ocfs2_xattr_info *xi,
3181 struct ocfs2_xattr_search *xis,
3182 struct ocfs2_xattr_search *xbs,
3183 struct ocfs2_xattr_set_ctxt *ctxt,
3187 int clusters_add, meta_add, ret;
3188 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3190 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3192 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3194 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
3195 &clusters_add, &meta_add, credits);
3201 meta_add += extra_meta;
3202 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
3203 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
3206 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3215 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3221 if (ctxt->meta_ac) {
3222 ocfs2_free_alloc_context(ctxt->meta_ac);
3223 ctxt->meta_ac = NULL;
3227 * We cannot have an error and a non null ctxt->data_ac.
3234 static int __ocfs2_xattr_set_handle(struct inode *inode,
3235 struct ocfs2_dinode *di,
3236 struct ocfs2_xattr_info *xi,
3237 struct ocfs2_xattr_search *xis,
3238 struct ocfs2_xattr_search *xbs,
3239 struct ocfs2_xattr_set_ctxt *ctxt)
3241 int ret = 0, credits, old_found;
3243 if (!xi->xi_value) {
3244 /* Remove existing extended attribute */
3245 if (!xis->not_found)
3246 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3247 else if (!xbs->not_found)
3248 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3250 /* We always try to set extended attribute into inode first*/
3251 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3252 if (!ret && !xbs->not_found) {
3254 * If succeed and that extended attribute existing in
3255 * external block, then we will remove it.
3257 xi->xi_value = NULL;
3258 xi->xi_value_len = 0;
3260 old_found = xis->not_found;
3261 xis->not_found = -ENODATA;
3262 ret = ocfs2_calc_xattr_set_need(inode,
3270 xis->not_found = old_found;
3276 ret = ocfs2_extend_trans(ctxt->handle, credits +
3277 ctxt->handle->h_buffer_credits);
3282 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3283 } else if (ret == -ENOSPC) {
3284 if (di->i_xattr_loc && !xbs->xattr_bh) {
3285 ret = ocfs2_xattr_block_find(inode,
3291 old_found = xis->not_found;
3292 xis->not_found = -ENODATA;
3293 ret = ocfs2_calc_xattr_set_need(inode,
3301 xis->not_found = old_found;
3307 ret = ocfs2_extend_trans(ctxt->handle, credits +
3308 ctxt->handle->h_buffer_credits);
3315 * If no space in inode, we will set extended attribute
3316 * into external block.
3318 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3321 if (!xis->not_found) {
3323 * If succeed and that extended attribute
3324 * existing in inode, we will remove it.
3326 xi->xi_value = NULL;
3327 xi->xi_value_len = 0;
3328 xbs->not_found = -ENODATA;
3329 ret = ocfs2_calc_xattr_set_need(inode,
3342 ret = ocfs2_extend_trans(ctxt->handle, credits +
3343 ctxt->handle->h_buffer_credits);
3348 ret = ocfs2_xattr_ibody_set(inode, xi,
3355 /* Update inode ctime. */
3356 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3358 OCFS2_JOURNAL_ACCESS_WRITE);
3364 inode->i_ctime = CURRENT_TIME;
3365 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3366 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3367 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3374 * This function only called duing creating inode
3375 * for init security/acl xattrs of the new inode.
3376 * All transanction credits have been reserved in mknod.
3378 int ocfs2_xattr_set_handle(handle_t *handle,
3379 struct inode *inode,
3380 struct buffer_head *di_bh,
3386 struct ocfs2_alloc_context *meta_ac,
3387 struct ocfs2_alloc_context *data_ac)
3389 struct ocfs2_dinode *di;
3392 struct ocfs2_xattr_info xi = {
3393 .xi_name_index = name_index,
3395 .xi_name_len = strlen(name),
3397 .xi_value_len = value_len,
3400 struct ocfs2_xattr_search xis = {
3401 .not_found = -ENODATA,
3404 struct ocfs2_xattr_search xbs = {
3405 .not_found = -ENODATA,
3408 struct ocfs2_xattr_set_ctxt ctxt = {
3414 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3418 * In extreme situation, may need xattr bucket when
3419 * block size is too small. And we have already reserved
3420 * the credits for bucket in mknod.
3422 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3423 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3425 mlog_errno(-ENOMEM);
3430 xis.inode_bh = xbs.inode_bh = di_bh;
3431 di = (struct ocfs2_dinode *)di_bh->b_data;
3433 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3435 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3438 if (xis.not_found) {
3439 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3444 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3447 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3448 brelse(xbs.xattr_bh);
3449 ocfs2_xattr_bucket_free(xbs.bucket);
3457 * Set, replace or remove an extended attribute for this inode.
3458 * value is NULL to remove an existing extended attribute, else either
3459 * create or replace an extended attribute.
3461 int ocfs2_xattr_set(struct inode *inode,
3468 struct buffer_head *di_bh = NULL;
3469 struct ocfs2_dinode *di;
3470 int ret, credits, ref_meta = 0, ref_credits = 0;
3471 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3472 struct inode *tl_inode = osb->osb_tl_inode;
3473 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
3474 struct ocfs2_refcount_tree *ref_tree = NULL;
3476 struct ocfs2_xattr_info xi = {
3477 .xi_name_index = name_index,
3479 .xi_name_len = strlen(name),
3481 .xi_value_len = value_len,
3484 struct ocfs2_xattr_search xis = {
3485 .not_found = -ENODATA,
3488 struct ocfs2_xattr_search xbs = {
3489 .not_found = -ENODATA,
3492 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3496 * Only xbs will be used on indexed trees. xis doesn't need a
3499 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3501 mlog_errno(-ENOMEM);
3505 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3508 goto cleanup_nolock;
3510 xis.inode_bh = xbs.inode_bh = di_bh;
3511 di = (struct ocfs2_dinode *)di_bh->b_data;
3513 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3515 * Scan inode and external block to find the same name
3516 * extended attribute and collect search infomation.
3518 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3521 if (xis.not_found) {
3522 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3527 if (xis.not_found && xbs.not_found) {
3529 if (flags & XATTR_REPLACE)
3536 if (flags & XATTR_CREATE)
3540 /* Check whether the value is refcounted and do some prepartion. */
3541 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3542 (!xis.not_found || !xbs.not_found)) {
3543 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3544 &xis, &xbs, &ref_tree,
3545 &ref_meta, &ref_credits);
3552 mutex_lock(&tl_inode->i_mutex);
3554 if (ocfs2_truncate_log_needs_flush(osb)) {
3555 ret = __ocfs2_flush_truncate_log(osb);
3557 mutex_unlock(&tl_inode->i_mutex);
3562 mutex_unlock(&tl_inode->i_mutex);
3564 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3565 &xbs, &ctxt, ref_meta, &credits);
3571 /* we need to update inode's ctime field, so add credit for it. */
3572 credits += OCFS2_INODE_UPDATE_CREDITS;
3573 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3574 if (IS_ERR(ctxt.handle)) {
3575 ret = PTR_ERR(ctxt.handle);
3580 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3582 ocfs2_commit_trans(osb, ctxt.handle);
3585 ocfs2_free_alloc_context(ctxt.data_ac);
3587 ocfs2_free_alloc_context(ctxt.meta_ac);
3588 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3589 ocfs2_schedule_truncate_log_flush(osb, 1);
3590 ocfs2_run_deallocs(osb, &ctxt.dealloc);
3594 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3595 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3596 if (!value && !ret) {
3597 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3601 ocfs2_inode_unlock(inode, 1);
3604 brelse(xbs.xattr_bh);
3605 ocfs2_xattr_bucket_free(xbs.bucket);
3611 * Find the xattr extent rec which may contains name_hash.
3612 * e_cpos will be the first name hash of the xattr rec.
3613 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3615 static int ocfs2_xattr_get_rec(struct inode *inode,
3620 struct ocfs2_extent_list *el)
3623 struct buffer_head *eb_bh = NULL;
3624 struct ocfs2_extent_block *eb;
3625 struct ocfs2_extent_rec *rec = NULL;
3628 if (el->l_tree_depth) {
3629 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3636 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3639 if (el->l_tree_depth) {
3640 ocfs2_error(inode->i_sb,
3641 "Inode %lu has non zero tree depth in "
3642 "xattr tree block %llu\n", inode->i_ino,
3643 (unsigned long long)eb_bh->b_blocknr);
3649 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3650 rec = &el->l_recs[i];
3652 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3653 e_blkno = le64_to_cpu(rec->e_blkno);
3659 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3660 "record (%u, %u, 0) in xattr", inode->i_ino,
3661 le32_to_cpu(rec->e_cpos),
3662 ocfs2_rec_clusters(el, rec));
3667 *p_blkno = le64_to_cpu(rec->e_blkno);
3668 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3670 *e_cpos = le32_to_cpu(rec->e_cpos);
3676 typedef int (xattr_bucket_func)(struct inode *inode,
3677 struct ocfs2_xattr_bucket *bucket,
3680 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3681 struct ocfs2_xattr_bucket *bucket,
3688 int i, ret = 0, cmp = 1, block_off, new_offset;
3689 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3690 size_t name_len = strlen(name);
3691 struct ocfs2_xattr_entry *xe = NULL;
3695 * We don't use binary search in the bucket because there
3696 * may be multiple entries with the same name hash.
3698 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3699 xe = &xh->xh_entries[i];
3701 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3703 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3706 cmp = name_index - ocfs2_xattr_get_type(xe);
3708 cmp = name_len - xe->xe_name_len;
3712 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3723 xe_name = bucket_block(bucket, block_off) + new_offset;
3724 if (!memcmp(name, xe_name, name_len)) {
3736 * Find the specified xattr entry in a series of buckets.
3737 * This series start from p_blkno and last for num_clusters.
3738 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3739 * the num of the valid buckets.
3741 * Return the buffer_head this xattr should reside in. And if the xattr's
3742 * hash is in the gap of 2 buckets, return the lower bucket.
3744 static int ocfs2_xattr_bucket_find(struct inode *inode,
3751 struct ocfs2_xattr_search *xs)
3754 struct ocfs2_xattr_header *xh = NULL;
3755 struct ocfs2_xattr_entry *xe = NULL;
3757 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3758 int low_bucket = 0, bucket, high_bucket;
3759 struct ocfs2_xattr_bucket *search;
3761 u64 blkno, lower_blkno = 0;
3763 search = ocfs2_xattr_bucket_new(inode);
3770 ret = ocfs2_read_xattr_bucket(search, p_blkno);
3776 xh = bucket_xh(search);
3777 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3778 while (low_bucket <= high_bucket) {
3779 ocfs2_xattr_bucket_relse(search);
3781 bucket = (low_bucket + high_bucket) / 2;
3782 blkno = p_blkno + bucket * blk_per_bucket;
3783 ret = ocfs2_read_xattr_bucket(search, blkno);
3789 xh = bucket_xh(search);
3790 xe = &xh->xh_entries[0];
3791 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3792 high_bucket = bucket - 1;
3797 * Check whether the hash of the last entry in our
3798 * bucket is larger than the search one. for an empty
3799 * bucket, the last one is also the first one.
3802 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3804 last_hash = le32_to_cpu(xe->xe_name_hash);
3806 /* record lower_blkno which may be the insert place. */
3807 lower_blkno = blkno;
3809 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3810 low_bucket = bucket + 1;
3814 /* the searched xattr should reside in this bucket if exists. */
3815 ret = ocfs2_find_xe_in_bucket(inode, search,
3816 name_index, name, name_hash,
3826 * Record the bucket we have found.
3827 * When the xattr's hash value is in the gap of 2 buckets, we will
3828 * always set it to the previous bucket.
3831 lower_blkno = p_blkno;
3833 /* This should be in cache - we just read it during the search */
3834 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3840 xs->header = bucket_xh(xs->bucket);
3841 xs->base = bucket_block(xs->bucket, 0);
3842 xs->end = xs->base + inode->i_sb->s_blocksize;
3845 xs->here = &xs->header->xh_entries[index];
3846 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3847 (unsigned long long)bucket_blkno(xs->bucket), index);
3852 ocfs2_xattr_bucket_free(search);
3856 static int ocfs2_xattr_index_block_find(struct inode *inode,
3857 struct buffer_head *root_bh,
3860 struct ocfs2_xattr_search *xs)
3863 struct ocfs2_xattr_block *xb =
3864 (struct ocfs2_xattr_block *)root_bh->b_data;
3865 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3866 struct ocfs2_extent_list *el = &xb_root->xt_list;
3868 u32 first_hash, num_clusters = 0;
3869 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3871 if (le16_to_cpu(el->l_next_free_rec) == 0)
3874 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3875 name, name_hash, name_index);
3877 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3884 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3886 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3887 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3890 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3891 p_blkno, first_hash, num_clusters, xs);
3897 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3900 xattr_bucket_func *func,
3904 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3905 u32 num_buckets = clusters * bpc;
3906 struct ocfs2_xattr_bucket *bucket;
3908 bucket = ocfs2_xattr_bucket_new(inode);
3910 mlog_errno(-ENOMEM);
3914 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3915 clusters, (unsigned long long)blkno);
3917 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3918 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3925 * The real bucket num in this series of blocks is stored
3926 * in the 1st bucket.
3929 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3931 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3932 (unsigned long long)blkno,
3933 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3935 ret = func(inode, bucket, para);
3936 if (ret && ret != -ERANGE)
3938 /* Fall through to bucket_relse() */
3941 ocfs2_xattr_bucket_relse(bucket);
3946 ocfs2_xattr_bucket_free(bucket);
3950 struct ocfs2_xattr_tree_list {
3956 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
3957 struct ocfs2_xattr_header *xh,
3964 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3967 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3969 *block_off = name_offset >> sb->s_blocksize_bits;
3970 *new_offset = name_offset % sb->s_blocksize;
3975 static int ocfs2_list_xattr_bucket(struct inode *inode,
3976 struct ocfs2_xattr_bucket *bucket,
3980 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
3981 int i, block_off, new_offset;
3982 const char *prefix, *name;
3984 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3985 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
3986 type = ocfs2_xattr_get_type(entry);
3987 prefix = ocfs2_xattr_prefix(type);
3990 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3998 name = (const char *)bucket_block(bucket, block_off) +
4000 ret = ocfs2_xattr_list_entry(xl->buffer,
4004 entry->xe_name_len);
4013 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4014 struct buffer_head *blk_bh,
4015 xattr_tree_rec_func *rec_func,
4018 struct ocfs2_xattr_block *xb =
4019 (struct ocfs2_xattr_block *)blk_bh->b_data;
4020 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4022 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4025 if (!el->l_next_free_rec || !rec_func)
4028 while (name_hash > 0) {
4029 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4030 &e_cpos, &num_clusters, el);
4036 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4037 num_clusters, para);
4047 name_hash = e_cpos - 1;
4054 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4055 struct buffer_head *root_bh,
4056 u64 blkno, u32 cpos, u32 len, void *para)
4058 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4059 ocfs2_list_xattr_bucket, para);
4062 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4063 struct buffer_head *blk_bh,
4068 struct ocfs2_xattr_tree_list xl = {
4070 .buffer_size = buffer_size,
4074 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4075 ocfs2_list_xattr_tree_rec, &xl);
4086 static int cmp_xe(const void *a, const void *b)
4088 const struct ocfs2_xattr_entry *l = a, *r = b;
4089 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4090 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4092 if (l_hash > r_hash)
4094 if (l_hash < r_hash)
4099 static void swap_xe(void *a, void *b, int size)
4101 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4104 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4105 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4109 * When the ocfs2_xattr_block is filled up, new bucket will be created
4110 * and all the xattr entries will be moved to the new bucket.
4111 * The header goes at the start of the bucket, and the names+values are
4112 * filled from the end. This is why *target starts as the last buffer.
4113 * Note: we need to sort the entries since they are not saved in order
4114 * in the ocfs2_xattr_block.
4116 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4117 struct buffer_head *xb_bh,
4118 struct ocfs2_xattr_bucket *bucket)
4120 int i, blocksize = inode->i_sb->s_blocksize;
4121 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4122 u16 offset, size, off_change;
4123 struct ocfs2_xattr_entry *xe;
4124 struct ocfs2_xattr_block *xb =
4125 (struct ocfs2_xattr_block *)xb_bh->b_data;
4126 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
4127 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4128 u16 count = le16_to_cpu(xb_xh->xh_count);
4129 char *src = xb_bh->b_data;
4130 char *target = bucket_block(bucket, blks - 1);
4132 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4133 (unsigned long long)xb_bh->b_blocknr,
4134 (unsigned long long)bucket_blkno(bucket));
4136 for (i = 0; i < blks; i++)
4137 memset(bucket_block(bucket, i), 0, blocksize);
4140 * Since the xe_name_offset is based on ocfs2_xattr_header,
4141 * there is a offset change corresponding to the change of
4142 * ocfs2_xattr_header's position.
4144 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4145 xe = &xb_xh->xh_entries[count - 1];
4146 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4147 size = blocksize - offset;
4149 /* copy all the names and values. */
4150 memcpy(target + offset, src + offset, size);
4152 /* Init new header now. */
4153 xh->xh_count = xb_xh->xh_count;
4154 xh->xh_num_buckets = cpu_to_le16(1);
4155 xh->xh_name_value_len = cpu_to_le16(size);
4156 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4158 /* copy all the entries. */
4159 target = bucket_block(bucket, 0);
4160 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4161 size = count * sizeof(struct ocfs2_xattr_entry);
4162 memcpy(target + offset, (char *)xb_xh + offset, size);
4164 /* Change the xe offset for all the xe because of the move. */
4165 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4166 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4167 for (i = 0; i < count; i++)
4168 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4170 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4171 offset, size, off_change);
4173 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4178 * After we move xattr from block to index btree, we have to
4179 * update ocfs2_xattr_search to the new xe and base.
4181 * When the entry is in xattr block, xattr_bh indicates the storage place.
4182 * While if the entry is in index b-tree, "bucket" indicates the
4183 * real place of the xattr.
4185 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4186 struct ocfs2_xattr_search *xs,
4187 struct buffer_head *old_bh)
4189 char *buf = old_bh->b_data;
4190 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4191 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
4194 xs->header = bucket_xh(xs->bucket);
4195 xs->base = bucket_block(xs->bucket, 0);
4196 xs->end = xs->base + inode->i_sb->s_blocksize;
4201 i = xs->here - old_xh->xh_entries;
4202 xs->here = &xs->header->xh_entries[i];
4205 static int ocfs2_xattr_create_index_block(struct inode *inode,
4206 struct ocfs2_xattr_search *xs,
4207 struct ocfs2_xattr_set_ctxt *ctxt)
4212 handle_t *handle = ctxt->handle;
4213 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4214 struct ocfs2_inode_info *oi = OCFS2_I(inode);
4215 struct buffer_head *xb_bh = xs->xattr_bh;
4216 struct ocfs2_xattr_block *xb =
4217 (struct ocfs2_xattr_block *)xb_bh->b_data;
4218 struct ocfs2_xattr_tree_root *xr;
4219 u16 xb_flags = le16_to_cpu(xb->xb_flags);
4221 mlog(0, "create xattr index block for %llu\n",
4222 (unsigned long long)xb_bh->b_blocknr);
4224 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
4225 BUG_ON(!xs->bucket);
4229 * We can use this lock for now, and maybe move to a dedicated mutex
4230 * if performance becomes a problem later.
4232 down_write(&oi->ip_alloc_sem);
4234 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
4235 OCFS2_JOURNAL_ACCESS_WRITE);
4241 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
4242 1, 1, &bit_off, &len);
4249 * The bucket may spread in many blocks, and
4250 * we will only touch the 1st block and the last block
4251 * in the whole bucket(one for entry and one for data).
4253 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4255 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4256 (unsigned long long)blkno);
4258 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
4264 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4265 OCFS2_JOURNAL_ACCESS_CREATE);
4271 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4272 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4274 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4276 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4277 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4278 offsetof(struct ocfs2_xattr_block, xb_attrs));
4280 xr = &xb->xb_attrs.xb_root;
4281 xr->xt_clusters = cpu_to_le32(1);
4282 xr->xt_last_eb_blk = 0;
4283 xr->xt_list.l_tree_depth = 0;
4284 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4285 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4287 xr->xt_list.l_recs[0].e_cpos = 0;
4288 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4289 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4291 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4293 ocfs2_journal_dirty(handle, xb_bh);
4296 up_write(&oi->ip_alloc_sem);
4301 static int cmp_xe_offset(const void *a, const void *b)
4303 const struct ocfs2_xattr_entry *l = a, *r = b;
4304 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4305 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4307 if (l_name_offset < r_name_offset)
4309 if (l_name_offset > r_name_offset)
4315 * defrag a xattr bucket if we find that the bucket has some
4316 * holes beteen name/value pairs.
4317 * We will move all the name/value pairs to the end of the bucket
4318 * so that we can spare some space for insertion.
4320 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
4322 struct ocfs2_xattr_bucket *bucket)
4325 size_t end, offset, len;
4326 struct ocfs2_xattr_header *xh;
4327 char *entries, *buf, *bucket_buf = NULL;
4328 u64 blkno = bucket_blkno(bucket);
4330 size_t blocksize = inode->i_sb->s_blocksize;
4331 struct ocfs2_xattr_entry *xe;
4334 * In order to make the operation more efficient and generic,
4335 * we copy all the blocks into a contiguous memory and do the
4336 * defragment there, so if anything is error, we will not touch
4339 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4346 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4347 memcpy(buf, bucket_block(bucket, i), blocksize);
4349 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4350 OCFS2_JOURNAL_ACCESS_WRITE);
4356 xh = (struct ocfs2_xattr_header *)bucket_buf;
4357 entries = (char *)xh->xh_entries;
4358 xh_free_start = le16_to_cpu(xh->xh_free_start);
4360 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4361 "xh_free_start = %u, xh_name_value_len = %u.\n",
4362 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4363 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4366 * sort all the entries by their offset.
4367 * the largest will be the first, so that we can
4368 * move them to the end one by one.
4370 sort(entries, le16_to_cpu(xh->xh_count),
4371 sizeof(struct ocfs2_xattr_entry),
4372 cmp_xe_offset, swap_xe);
4374 /* Move all name/values to the end of the bucket. */
4375 xe = xh->xh_entries;
4376 end = OCFS2_XATTR_BUCKET_SIZE;
4377 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4378 offset = le16_to_cpu(xe->xe_name_offset);
4379 len = namevalue_size_xe(xe);
4382 * We must make sure that the name/value pair
4383 * exist in the same block. So adjust end to
4384 * the previous block end if needed.
4386 if (((end - len) / blocksize !=
4387 (end - 1) / blocksize))
4388 end = end - end % blocksize;
4390 if (end > offset + len) {
4391 memmove(bucket_buf + end - len,
4392 bucket_buf + offset, len);
4393 xe->xe_name_offset = cpu_to_le16(end - len);
4396 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4397 "bucket %llu\n", (unsigned long long)blkno);
4402 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4403 "bucket %llu\n", (unsigned long long)blkno);
4405 if (xh_free_start == end)
4408 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4409 xh->xh_free_start = cpu_to_le16(end);
4411 /* sort the entries by their name_hash. */
4412 sort(entries, le16_to_cpu(xh->xh_count),
4413 sizeof(struct ocfs2_xattr_entry),
4417 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4418 memcpy(bucket_block(bucket, i), buf, blocksize);
4419 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4427 * prev_blkno points to the start of an existing extent. new_blkno
4428 * points to a newly allocated extent. Because we know each of our
4429 * clusters contains more than bucket, we can easily split one cluster
4430 * at a bucket boundary. So we take the last cluster of the existing
4431 * extent and split it down the middle. We move the last half of the
4432 * buckets in the last cluster of the existing extent over to the new
4435 * first_bh is the buffer at prev_blkno so we can update the existing
4436 * extent's bucket count. header_bh is the bucket were we were hoping
4437 * to insert our xattr. If the bucket move places the target in the new
4438 * extent, we'll update first_bh and header_bh after modifying the old
4441 * first_hash will be set as the 1st xe's name_hash in the new extent.
4443 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4445 struct ocfs2_xattr_bucket *first,
4446 struct ocfs2_xattr_bucket *target,
4452 struct super_block *sb = inode->i_sb;
4453 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4454 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4455 int to_move = num_buckets / 2;
4457 u64 last_cluster_blkno = bucket_blkno(first) +
4458 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4460 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4461 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4463 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4464 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
4466 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4467 last_cluster_blkno, new_blkno,
4468 to_move, first_hash);
4474 /* This is the first bucket that got moved */
4475 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4478 * If the target bucket was part of the moved buckets, we need to
4479 * update first and target.
4481 if (bucket_blkno(target) >= src_blkno) {
4482 /* Find the block for the new target bucket */
4483 src_blkno = new_blkno +
4484 (bucket_blkno(target) - src_blkno);
4486 ocfs2_xattr_bucket_relse(first);
4487 ocfs2_xattr_bucket_relse(target);
4490 * These shouldn't fail - the buffers are in the
4491 * journal from ocfs2_cp_xattr_bucket().
4493 ret = ocfs2_read_xattr_bucket(first, new_blkno);
4498 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4509 * Find the suitable pos when we divide a bucket into 2.
4510 * We have to make sure the xattrs with the same hash value exist
4511 * in the same bucket.
4513 * If this ocfs2_xattr_header covers more than one hash value, find a
4514 * place where the hash value changes. Try to find the most even split.
4515 * The most common case is that all entries have different hash values,
4516 * and the first check we make will find a place to split.
4518 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4520 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4521 int count = le16_to_cpu(xh->xh_count);
4522 int delta, middle = count / 2;
4525 * We start at the middle. Each step gets farther away in both
4526 * directions. We therefore hit the change in hash value
4527 * nearest to the middle. Note that this loop does not execute for
4530 for (delta = 0; delta < middle; delta++) {
4531 /* Let's check delta earlier than middle */
4532 if (cmp_xe(&entries[middle - delta - 1],
4533 &entries[middle - delta]))
4534 return middle - delta;
4536 /* For even counts, don't walk off the end */
4537 if ((middle + delta + 1) == count)
4540 /* Now try delta past middle */
4541 if (cmp_xe(&entries[middle + delta],
4542 &entries[middle + delta + 1]))
4543 return middle + delta + 1;
4546 /* Every entry had the same hash */
4551 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4552 * first_hash will record the 1st hash of the new bucket.
4554 * Normally half of the xattrs will be moved. But we have to make
4555 * sure that the xattrs with the same hash value are stored in the
4556 * same bucket. If all the xattrs in this bucket have the same hash
4557 * value, the new bucket will be initialized as an empty one and the
4558 * first_hash will be initialized as (hash_value+1).
4560 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4565 int new_bucket_head)
4568 int count, start, len, name_value_len = 0, name_offset = 0;
4569 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4570 struct ocfs2_xattr_header *xh;
4571 struct ocfs2_xattr_entry *xe;
4572 int blocksize = inode->i_sb->s_blocksize;
4574 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4575 (unsigned long long)blk, (unsigned long long)new_blk);
4577 s_bucket = ocfs2_xattr_bucket_new(inode);
4578 t_bucket = ocfs2_xattr_bucket_new(inode);
4579 if (!s_bucket || !t_bucket) {
4585 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4591 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4592 OCFS2_JOURNAL_ACCESS_WRITE);
4599 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4600 * there's no need to read it.
4602 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4609 * Hey, if we're overwriting t_bucket, what difference does
4610 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4611 * same part of ocfs2_cp_xattr_bucket().
4613 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4615 OCFS2_JOURNAL_ACCESS_CREATE :
4616 OCFS2_JOURNAL_ACCESS_WRITE);
4622 xh = bucket_xh(s_bucket);
4623 count = le16_to_cpu(xh->xh_count);
4624 start = ocfs2_xattr_find_divide_pos(xh);
4626 if (start == count) {
4627 xe = &xh->xh_entries[start-1];
4630 * initialized a new empty bucket here.
4631 * The hash value is set as one larger than
4632 * that of the last entry in the previous bucket.
4634 for (i = 0; i < t_bucket->bu_blocks; i++)
4635 memset(bucket_block(t_bucket, i), 0, blocksize);
4637 xh = bucket_xh(t_bucket);
4638 xh->xh_free_start = cpu_to_le16(blocksize);
4639 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4640 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4642 goto set_num_buckets;
4645 /* copy the whole bucket to the new first. */
4646 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4648 /* update the new bucket. */
4649 xh = bucket_xh(t_bucket);
4652 * Calculate the total name/value len and xh_free_start for
4653 * the old bucket first.
4655 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4657 for (i = 0; i < start; i++) {
4658 xe = &xh->xh_entries[i];
4659 name_value_len += namevalue_size_xe(xe);
4660 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4661 name_offset = le16_to_cpu(xe->xe_name_offset);
4665 * Now begin the modification to the new bucket.
4667 * In the new bucket, We just move the xattr entry to the beginning
4668 * and don't touch the name/value. So there will be some holes in the
4669 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4672 xe = &xh->xh_entries[start];
4673 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4674 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
4675 (int)((char *)xe - (char *)xh),
4676 (int)((char *)xh->xh_entries - (char *)xh));
4677 memmove((char *)xh->xh_entries, (char *)xe, len);
4678 xe = &xh->xh_entries[count - start];
4679 len = sizeof(struct ocfs2_xattr_entry) * start;
4680 memset((char *)xe, 0, len);
4682 le16_add_cpu(&xh->xh_count, -start);
4683 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4685 /* Calculate xh_free_start for the new bucket. */
4686 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4687 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4688 xe = &xh->xh_entries[i];
4689 if (le16_to_cpu(xe->xe_name_offset) <
4690 le16_to_cpu(xh->xh_free_start))
4691 xh->xh_free_start = xe->xe_name_offset;
4695 /* set xh->xh_num_buckets for the new xh. */
4696 if (new_bucket_head)
4697 xh->xh_num_buckets = cpu_to_le16(1);
4699 xh->xh_num_buckets = 0;
4701 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4703 /* store the first_hash of the new bucket. */
4705 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4708 * Now only update the 1st block of the old bucket. If we
4709 * just added a new empty bucket, there is no need to modify
4715 xh = bucket_xh(s_bucket);
4716 memset(&xh->xh_entries[start], 0,
4717 sizeof(struct ocfs2_xattr_entry) * (count - start));
4718 xh->xh_count = cpu_to_le16(start);
4719 xh->xh_free_start = cpu_to_le16(name_offset);
4720 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4722 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4725 ocfs2_xattr_bucket_free(s_bucket);
4726 ocfs2_xattr_bucket_free(t_bucket);
4732 * Copy xattr from one bucket to another bucket.
4734 * The caller must make sure that the journal transaction
4735 * has enough space for journaling.
4737 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4744 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4746 BUG_ON(s_blkno == t_blkno);
4748 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4749 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4752 s_bucket = ocfs2_xattr_bucket_new(inode);
4753 t_bucket = ocfs2_xattr_bucket_new(inode);
4754 if (!s_bucket || !t_bucket) {
4760 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4765 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4766 * there's no need to read it.
4768 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4773 * Hey, if we're overwriting t_bucket, what difference does
4774 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4775 * cluster to fill, we came here from
4776 * ocfs2_mv_xattr_buckets(), and it is really new -
4777 * ACCESS_CREATE is required. But we also might have moved data
4778 * out of t_bucket before extending back into it.
4779 * ocfs2_add_new_xattr_bucket() can do this - its call to
4780 * ocfs2_add_new_xattr_cluster() may have created a new extent
4781 * and copied out the end of the old extent. Then it re-extends
4782 * the old extent back to create space for new xattrs. That's
4783 * how we get here, and the bucket isn't really new.
4785 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4787 OCFS2_JOURNAL_ACCESS_CREATE :
4788 OCFS2_JOURNAL_ACCESS_WRITE);
4792 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4793 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4796 ocfs2_xattr_bucket_free(t_bucket);
4797 ocfs2_xattr_bucket_free(s_bucket);
4803 * src_blk points to the start of an existing extent. last_blk points to
4804 * last cluster in that extent. to_blk points to a newly allocated
4805 * extent. We copy the buckets from the cluster at last_blk to the new
4806 * extent. If start_bucket is non-zero, we skip that many buckets before
4807 * we start copying. The new extent's xh_num_buckets gets set to the
4808 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4809 * by the same amount.
4811 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4812 u64 src_blk, u64 last_blk, u64 to_blk,
4813 unsigned int start_bucket,
4816 int i, ret, credits;
4817 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4818 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4819 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4820 struct ocfs2_xattr_bucket *old_first, *new_first;
4822 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4823 (unsigned long long)last_blk, (unsigned long long)to_blk);
4825 BUG_ON(start_bucket >= num_buckets);
4827 num_buckets -= start_bucket;
4828 last_blk += (start_bucket * blks_per_bucket);
4831 /* The first bucket of the original extent */
4832 old_first = ocfs2_xattr_bucket_new(inode);
4833 /* The first bucket of the new extent */
4834 new_first = ocfs2_xattr_bucket_new(inode);
4835 if (!old_first || !new_first) {
4841 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4848 * We need to update the first bucket of the old extent and all
4849 * the buckets going to the new extent.
4851 credits = ((num_buckets + 1) * blks_per_bucket) +
4852 handle->h_buffer_credits;
4853 ret = ocfs2_extend_trans(handle, credits);
4859 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4860 OCFS2_JOURNAL_ACCESS_WRITE);
4866 for (i = 0; i < num_buckets; i++) {
4867 ret = ocfs2_cp_xattr_bucket(inode, handle,
4868 last_blk + (i * blks_per_bucket),
4869 to_blk + (i * blks_per_bucket),
4878 * Get the new bucket ready before we dirty anything
4879 * (This actually shouldn't fail, because we already dirtied
4880 * it once in ocfs2_cp_xattr_bucket()).
4882 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4887 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4888 OCFS2_JOURNAL_ACCESS_WRITE);
4894 /* Now update the headers */
4895 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4896 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4898 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4899 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4902 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4905 ocfs2_xattr_bucket_free(new_first);
4906 ocfs2_xattr_bucket_free(old_first);
4911 * Move some xattrs in this cluster to the new cluster.
4912 * This function should only be called when bucket size == cluster size.
4913 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4915 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4921 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4922 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
4924 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4926 ret = ocfs2_extend_trans(handle, credits);
4932 /* Move half of the xattr in start_blk to the next bucket. */
4933 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4934 new_blk, first_hash, 1);
4938 * Move some xattrs from the old cluster to the new one since they are not
4939 * contiguous in ocfs2 xattr tree.
4941 * new_blk starts a new separate cluster, and we will move some xattrs from
4942 * prev_blk to it. v_start will be set as the first name hash value in this
4943 * new cluster so that it can be used as e_cpos during tree insertion and
4944 * don't collide with our original b-tree operations. first_bh and header_bh
4945 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4946 * to extend the insert bucket.
4948 * The problem is how much xattr should we move to the new one and when should
4949 * we update first_bh and header_bh?
4950 * 1. If cluster size > bucket size, that means the previous cluster has more
4951 * than 1 bucket, so just move half nums of bucket into the new cluster and
4952 * update the first_bh and header_bh if the insert bucket has been moved
4953 * to the new cluster.
4954 * 2. If cluster_size == bucket_size:
4955 * a) If the previous extent rec has more than one cluster and the insert
4956 * place isn't in the last cluster, copy the entire last cluster to the
4957 * new one. This time, we don't need to upate the first_bh and header_bh
4958 * since they will not be moved into the new cluster.
4959 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4960 * the new one. And we set the extend flag to zero if the insert place is
4961 * moved into the new allocated cluster since no extend is needed.
4963 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4965 struct ocfs2_xattr_bucket *first,
4966 struct ocfs2_xattr_bucket *target,
4974 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4975 (unsigned long long)bucket_blkno(first), prev_clusters,
4976 (unsigned long long)new_blk);
4978 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
4979 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4988 /* The start of the last cluster in the first extent */
4989 u64 last_blk = bucket_blkno(first) +
4990 ((prev_clusters - 1) *
4991 ocfs2_clusters_to_blocks(inode->i_sb, 1));
4993 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
4994 ret = ocfs2_mv_xattr_buckets(inode, handle,
4995 bucket_blkno(first),
4996 last_blk, new_blk, 0,
5001 ret = ocfs2_divide_xattr_cluster(inode, handle,
5007 if ((bucket_blkno(target) == last_blk) && extend)
5016 * Add a new cluster for xattr storage.
5018 * If the new cluster is contiguous with the previous one, it will be
5019 * appended to the same extent record, and num_clusters will be updated.
5020 * If not, we will insert a new extent for it and move some xattrs in
5021 * the last cluster into the new allocated one.
5022 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5023 * lose the benefits of hashing because we'll have to search large leaves.
5024 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5027 * first_bh is the first block of the previous extent rec and header_bh
5028 * indicates the bucket we will insert the new xattrs. They will be updated
5029 * when the header_bh is moved into the new cluster.
5031 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5032 struct buffer_head *root_bh,
5033 struct ocfs2_xattr_bucket *first,
5034 struct ocfs2_xattr_bucket *target,
5038 struct ocfs2_xattr_set_ctxt *ctxt)
5041 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
5042 u32 prev_clusters = *num_clusters;
5043 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5045 handle_t *handle = ctxt->handle;
5046 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5047 struct ocfs2_extent_tree et;
5049 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5050 "previous xattr blkno = %llu\n",
5051 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5052 prev_cpos, (unsigned long long)bucket_blkno(first));
5054 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5056 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5057 OCFS2_JOURNAL_ACCESS_WRITE);
5063 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
5064 clusters_to_add, &bit_off, &num_bits);
5071 BUG_ON(num_bits > clusters_to_add);
5073 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5074 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5075 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5077 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
5078 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5079 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5081 * If this cluster is contiguous with the old one and
5082 * adding this new cluster, we don't surpass the limit of
5083 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5084 * initialized and used like other buckets in the previous
5086 * So add it as a contiguous one. The caller will handle
5089 v_start = prev_cpos + prev_clusters;
5090 *num_clusters = prev_clusters + num_bits;
5091 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5094 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5108 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
5109 num_bits, (unsigned long long)block, v_start);
5110 ret = ocfs2_insert_extent(handle, &et, v_start, block,
5111 num_bits, 0, ctxt->meta_ac);
5117 ret = ocfs2_journal_dirty(handle, root_bh);
5126 * We are given an extent. 'first' is the bucket at the very front of
5127 * the extent. The extent has space for an additional bucket past
5128 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5129 * of the target bucket. We wish to shift every bucket past the target
5130 * down one, filling in that additional space. When we get back to the
5131 * target, we split the target between itself and the now-empty bucket
5132 * at target+1 (aka, target_blkno + blks_per_bucket).
5134 static int ocfs2_extend_xattr_bucket(struct inode *inode,
5136 struct ocfs2_xattr_bucket *first,
5141 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5142 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5144 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
5146 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
5147 "from %llu, len = %u\n", (unsigned long long)target_blk,
5148 (unsigned long long)bucket_blkno(first), num_clusters);
5150 /* The extent must have room for an additional bucket */
5151 BUG_ON(new_bucket >=
5152 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
5154 /* end_blk points to the last existing bucket */
5155 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
5158 * end_blk is the start of the last existing bucket.
5159 * Thus, (end_blk - target_blk) covers the target bucket and
5160 * every bucket after it up to, but not including, the last
5161 * existing bucket. Then we add the last existing bucket, the
5162 * new bucket, and the first bucket (3 * blk_per_bucket).
5164 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
5165 handle->h_buffer_credits;
5166 ret = ocfs2_extend_trans(handle, credits);
5172 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5173 OCFS2_JOURNAL_ACCESS_WRITE);
5179 while (end_blk != target_blk) {
5180 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5181 end_blk + blk_per_bucket, 0);
5184 end_blk -= blk_per_bucket;
5187 /* Move half of the xattr in target_blkno to the next bucket. */
5188 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5189 target_blk + blk_per_bucket, NULL, 0);
5191 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5192 ocfs2_xattr_bucket_journal_dirty(handle, first);
5199 * Add new xattr bucket in an extent record and adjust the buckets
5200 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5201 * bucket we want to insert into.
5203 * In the easy case, we will move all the buckets after target down by
5204 * one. Half of target's xattrs will be moved to the next bucket.
5206 * If current cluster is full, we'll allocate a new one. This may not
5207 * be contiguous. The underlying calls will make sure that there is
5208 * space for the insert, shifting buckets around if necessary.
5209 * 'target' may be moved by those calls.
5211 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5212 struct buffer_head *xb_bh,
5213 struct ocfs2_xattr_bucket *target,
5214 struct ocfs2_xattr_set_ctxt *ctxt)
5216 struct ocfs2_xattr_block *xb =
5217 (struct ocfs2_xattr_block *)xb_bh->b_data;
5218 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5219 struct ocfs2_extent_list *el = &xb_root->xt_list;
5221 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
5222 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5223 int ret, num_buckets, extend = 1;
5225 u32 e_cpos, num_clusters;
5226 /* The bucket at the front of the extent */
5227 struct ocfs2_xattr_bucket *first;
5229 mlog(0, "Add new xattr bucket starting from %llu\n",
5230 (unsigned long long)bucket_blkno(target));
5232 /* The first bucket of the original extent */
5233 first = ocfs2_xattr_bucket_new(inode);
5240 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5247 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5253 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5254 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5256 * This can move first+target if the target bucket moves
5257 * to the new extent.
5259 ret = ocfs2_add_new_xattr_cluster(inode,
5274 ret = ocfs2_extend_xattr_bucket(inode,
5277 bucket_blkno(target),
5284 ocfs2_xattr_bucket_free(first);
5289 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5290 struct ocfs2_xattr_bucket *bucket,
5293 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5295 offs = offs % inode->i_sb->s_blocksize;
5296 return bucket_block(bucket, block_off) + offs;
5300 * Set the xattr entry in the specified bucket.
5301 * The bucket is indicated by xs->bucket and it should have the enough
5302 * space for the xattr insertion.
5304 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
5306 struct ocfs2_xattr_info *xi,
5307 struct ocfs2_xattr_search *xs,
5312 struct ocfs2_xa_loc loc;
5314 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
5315 (unsigned long)xi->xi_value_len, xi->xi_name_index,
5316 (unsigned long long)bucket_blkno(xs->bucket));
5318 if (!xs->bucket->bu_bhs[1]) {
5319 blkno = bucket_blkno(xs->bucket);
5320 ocfs2_xattr_bucket_relse(xs->bucket);
5321 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
5328 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
5329 OCFS2_JOURNAL_ACCESS_WRITE);
5335 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5336 xs->not_found ? NULL : xs->here);
5337 ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash);
5343 /* XXX For now, until we make ocfs2_xa_prepare_entry() primary */
5344 BUG_ON(ret == -ENOSPC);
5345 ocfs2_xa_store_inline_value(&loc, xi);
5346 xs->here = loc.xl_entry;
5348 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
5355 * Truncate the specified xe_off entry in xattr bucket.
5356 * bucket is indicated by header_bh and len is the new length.
5357 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5359 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5361 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5362 struct ocfs2_xattr_bucket *bucket,
5365 struct ocfs2_xattr_set_ctxt *ctxt)
5369 struct ocfs2_xattr_entry *xe;
5370 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5371 size_t blocksize = inode->i_sb->s_blocksize;
5372 struct ocfs2_xattr_value_buf vb = {
5373 .vb_access = ocfs2_journal_access,
5376 xe = &xh->xh_entries[xe_off];
5378 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5380 offset = le16_to_cpu(xe->xe_name_offset) +
5381 OCFS2_XATTR_SIZE(xe->xe_name_len);
5383 value_blk = offset / blocksize;
5385 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5386 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
5388 vb.vb_bh = bucket->bu_bhs[value_blk];
5391 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5392 (vb.vb_bh->b_data + offset % blocksize);
5395 * From here on out we have to dirty the bucket. The generic
5396 * value calls only modify one of the bucket's bhs, but we need
5397 * to send the bucket at once. So if they error, they *could* have
5398 * modified something. We have to assume they did, and dirty
5399 * the whole bucket. This leaves us in a consistent state.
5401 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5402 xe_off, (unsigned long long)bucket_blkno(bucket), len);
5403 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5409 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5410 OCFS2_JOURNAL_ACCESS_WRITE);
5416 xe->xe_value_size = cpu_to_le64(len);
5418 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5424 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
5425 struct ocfs2_xattr_search *xs,
5427 struct ocfs2_xattr_set_ctxt *ctxt)
5430 struct ocfs2_xattr_entry *xe = xs->here;
5431 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
5433 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
5435 offset = xe - xh->xh_entries;
5436 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
5444 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
5446 struct ocfs2_xattr_search *xs,
5450 int ret, offset, block_off;
5451 struct ocfs2_xattr_value_root *xv;
5452 struct ocfs2_xattr_entry *xe = xs->here;
5453 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5455 struct ocfs2_xattr_value_buf vb = {
5456 .vb_access = ocfs2_journal_access,
5459 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
5461 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
5462 xe - xh->xh_entries,
5470 base = bucket_block(xs->bucket, block_off);
5471 xv = (struct ocfs2_xattr_value_root *)(base + offset +
5472 OCFS2_XATTR_SIZE(xe->xe_name_len));
5475 vb.vb_bh = xs->bucket->bu_bhs[block_off];
5476 ret = __ocfs2_xattr_set_value_outside(inode, handle,
5477 &vb, val, value_len);
5484 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5485 struct buffer_head *root_bh,
5492 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5493 struct inode *tl_inode = osb->osb_tl_inode;
5495 struct ocfs2_xattr_block *xb =
5496 (struct ocfs2_xattr_block *)root_bh->b_data;
5497 struct ocfs2_alloc_context *meta_ac = NULL;
5498 struct ocfs2_cached_dealloc_ctxt dealloc;
5499 struct ocfs2_extent_tree et;
5501 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5502 ocfs2_delete_xattr_in_bucket, para);
5508 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5510 ocfs2_init_dealloc_ctxt(&dealloc);
5512 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5513 cpos, len, (unsigned long long)blkno);
5515 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5518 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5524 mutex_lock(&tl_inode->i_mutex);
5526 if (ocfs2_truncate_log_needs_flush(osb)) {
5527 ret = __ocfs2_flush_truncate_log(osb);
5534 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5535 if (IS_ERR(handle)) {
5541 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5542 OCFS2_JOURNAL_ACCESS_WRITE);
5548 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5555 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5557 ret = ocfs2_journal_dirty(handle, root_bh);
5563 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5568 ocfs2_commit_trans(osb, handle);
5570 ocfs2_schedule_truncate_log_flush(osb, 1);
5572 mutex_unlock(&tl_inode->i_mutex);
5575 ocfs2_free_alloc_context(meta_ac);
5577 ocfs2_run_deallocs(osb, &dealloc);
5582 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
5584 struct ocfs2_xattr_search *xs)
5586 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5587 struct ocfs2_xattr_entry *last = &xh->xh_entries[
5588 le16_to_cpu(xh->xh_count) - 1];
5591 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
5592 OCFS2_JOURNAL_ACCESS_WRITE);
5598 /* Remove the old entry. */
5599 memmove(xs->here, xs->here + 1,
5600 (void *)last - (void *)xs->here);
5601 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
5602 le16_add_cpu(&xh->xh_count, -1);
5604 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
5608 * Set the xattr name/value in the bucket specified in xs.
5610 * As the new value in xi may be stored in the bucket or in an outside cluster,
5611 * we divide the whole process into 3 steps:
5612 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
5613 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
5614 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
5615 * 4. If the clusters for the new outside value can't be allocated, we need
5616 * to free the xattr we allocated in set.
5618 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5619 struct ocfs2_xattr_info *xi,
5620 struct ocfs2_xattr_search *xs,
5621 struct ocfs2_xattr_set_ctxt *ctxt)
5625 char *val = (char *)xi->xi_value;
5626 struct ocfs2_xattr_entry *xe = xs->here;
5627 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
5630 value_len = xi->xi_value_len;
5631 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
5633 * We need to truncate the xattr storage first.
5635 * If both the old and new value are stored to
5636 * outside block, we only need to truncate
5637 * the storage and then set the value outside.
5639 * If the new value should be stored within block,
5640 * we should free all the outside block first and
5641 * the modification to the xattr block will be done
5642 * by following steps.
5644 if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
5647 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5654 goto set_value_outside;
5657 /* So we have to handle the inside block change now. */
5658 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
5665 if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
5668 /* allocate the space now for the outside block storage. */
5669 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5674 if (xs->not_found) {
5676 * We can't allocate enough clusters for outside
5677 * storage and we have allocated xattr already,
5678 * so need to remove it.
5680 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
5686 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
5687 xs, val, value_len);
5693 * check whether the xattr bucket is filled up with the same hash value.
5694 * If we want to insert the xattr with the same hash, return -ENOSPC.
5695 * If we want to insert a xattr with different hash value, go ahead
5696 * and ocfs2_divide_xattr_bucket will handle this.
5698 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5699 struct ocfs2_xattr_bucket *bucket,
5702 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5703 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5705 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5708 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5709 xh->xh_entries[0].xe_name_hash) {
5710 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5712 (unsigned long long)bucket_blkno(bucket),
5713 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5720 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5721 struct ocfs2_xattr_info *xi,
5722 struct ocfs2_xattr_search *xs,
5723 struct ocfs2_xattr_set_ctxt *ctxt)
5725 struct ocfs2_xattr_header *xh;
5726 struct ocfs2_xattr_entry *xe;
5727 u16 count, header_size, xh_free_start;
5728 int free, max_free, need, old;
5729 size_t value_size = 0;
5730 size_t blocksize = inode->i_sb->s_blocksize;
5731 int ret, allocation = 0;
5733 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
5737 count = le16_to_cpu(xh->xh_count);
5738 xh_free_start = le16_to_cpu(xh->xh_free_start);
5739 header_size = sizeof(struct ocfs2_xattr_header) +
5740 count * sizeof(struct ocfs2_xattr_entry);
5741 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5742 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
5744 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5745 "of %u which exceed block size\n",
5746 (unsigned long long)bucket_blkno(xs->bucket),
5749 if (xi->xi_value && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
5750 value_size = OCFS2_XATTR_ROOT_SIZE;
5751 else if (xi->xi_value)
5752 value_size = OCFS2_XATTR_SIZE(xi->xi_value_len);
5755 need = sizeof(struct ocfs2_xattr_entry) +
5756 OCFS2_XATTR_SIZE(xi->xi_name_len) + value_size;
5758 need = value_size + OCFS2_XATTR_SIZE(xi->xi_name_len);
5761 * We only replace the old value if the new length is smaller
5762 * than the old one. Otherwise we will allocate new space in the
5763 * bucket to store it.
5766 if (ocfs2_xattr_is_local(xe))
5767 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5769 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5771 if (old >= value_size)
5775 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
5777 * We need to make sure the new name/value pair
5778 * can exist in the same block.
5780 if (xh_free_start % blocksize < need)
5781 free -= xh_free_start % blocksize;
5783 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5784 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5785 " %u\n", xs->not_found,
5786 (unsigned long long)bucket_blkno(xs->bucket),
5787 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5788 le16_to_cpu(xh->xh_name_value_len));
5792 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
5793 if (need <= max_free &&
5794 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5796 * We can create the space by defragment. Since only the
5797 * name/value will be moved, the xe shouldn't be changed
5800 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5807 xh_free_start = le16_to_cpu(xh->xh_free_start);
5808 free = xh_free_start - header_size
5809 - OCFS2_XATTR_HEADER_GAP;
5810 if (xh_free_start % blocksize < need)
5811 free -= xh_free_start % blocksize;
5816 mlog(0, "Can't get enough space for xattr insert by "
5817 "defragment. Need %u bytes, but we have %d, so "
5818 "allocate new bucket for it.\n", need, free);
5822 * We have to add new buckets or clusters and one
5823 * allocation should leave us enough space for insert.
5828 * We do not allow for overlapping ranges between buckets. And
5829 * the maximum number of collisions we will allow for then is
5830 * one bucket's worth, so check it here whether we need to
5831 * add a new bucket for the insert.
5833 ret = ocfs2_check_xattr_bucket_collision(inode,
5841 ret = ocfs2_add_new_xattr_bucket(inode,
5851 * ocfs2_add_new_xattr_bucket() will have updated
5852 * xs->bucket if it moved, but it will not have updated
5853 * any of the other search fields. Thus, we drop it and
5854 * re-search. Everything should be cached, so it'll be
5857 ocfs2_xattr_bucket_relse(xs->bucket);
5858 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5861 if (ret && ret != -ENODATA)
5863 xs->not_found = ret;
5869 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
5875 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5876 struct ocfs2_xattr_bucket *bucket,
5879 int ret = 0, ref_credits;
5880 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5882 struct ocfs2_xattr_entry *xe;
5883 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5884 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5885 int credits = ocfs2_remove_extent_credits(osb->sb) +
5886 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5887 struct ocfs2_xattr_value_root *xv;
5888 struct ocfs2_rm_xattr_bucket_para *args =
5889 (struct ocfs2_rm_xattr_bucket_para *)para;
5891 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5893 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5894 xe = &xh->xh_entries[i];
5895 if (ocfs2_xattr_is_local(xe))
5898 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5901 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5907 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5908 if (IS_ERR(ctxt.handle)) {
5909 ret = PTR_ERR(ctxt.handle);
5914 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5917 ocfs2_commit_trans(osb, ctxt.handle);
5919 ocfs2_free_alloc_context(ctxt.meta_ac);
5920 ctxt.meta_ac = NULL;
5929 ocfs2_free_alloc_context(ctxt.meta_ac);
5930 ocfs2_schedule_truncate_log_flush(osb, 1);
5931 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5936 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5937 * or change the extent record flag), we need to recalculate
5938 * the metaecc for the whole bucket. So it is done here.
5941 * We have to give the extra credits for the caller.
5943 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5948 struct ocfs2_xattr_bucket *bucket =
5949 (struct ocfs2_xattr_bucket *)para;
5951 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5952 OCFS2_JOURNAL_ACCESS_WRITE);
5958 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5964 * Special action we need if the xattr value is refcounted.
5966 * 1. If the xattr is refcounted, lock the tree.
5967 * 2. CoW the xattr if we are setting the new value and the value
5968 * will be stored outside.
5969 * 3. In other case, decrease_refcount will work for us, so just
5970 * lock the refcount tree, calculate the meta and credits is OK.
5972 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5973 * currently CoW is a completed transaction, while this function
5974 * will also lock the allocators and let us deadlock. So we will
5975 * CoW the whole xattr value.
5977 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5978 struct ocfs2_dinode *di,
5979 struct ocfs2_xattr_info *xi,
5980 struct ocfs2_xattr_search *xis,
5981 struct ocfs2_xattr_search *xbs,
5982 struct ocfs2_refcount_tree **ref_tree,
5987 struct ocfs2_xattr_block *xb;
5988 struct ocfs2_xattr_entry *xe;
5990 u32 p_cluster, num_clusters;
5991 unsigned int ext_flags;
5992 int name_offset, name_len;
5993 struct ocfs2_xattr_value_buf vb;
5994 struct ocfs2_xattr_bucket *bucket = NULL;
5995 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5996 struct ocfs2_post_refcount refcount;
5997 struct ocfs2_post_refcount *p = NULL;
5998 struct buffer_head *ref_root_bh = NULL;
6000 if (!xis->not_found) {
6002 name_offset = le16_to_cpu(xe->xe_name_offset);
6003 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
6005 vb.vb_bh = xis->inode_bh;
6006 vb.vb_access = ocfs2_journal_access_di;
6008 int i, block_off = 0;
6009 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
6011 name_offset = le16_to_cpu(xe->xe_name_offset);
6012 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
6013 i = xbs->here - xbs->header->xh_entries;
6015 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
6016 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
6017 bucket_xh(xbs->bucket),
6024 base = bucket_block(xbs->bucket, block_off);
6025 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
6026 vb.vb_access = ocfs2_journal_access;
6028 if (ocfs2_meta_ecc(osb)) {
6029 /*create parameters for ocfs2_post_refcount. */
6030 bucket = xbs->bucket;
6031 refcount.credits = bucket->bu_blocks;
6032 refcount.para = bucket;
6034 ocfs2_xattr_bucket_post_refcount;
6039 vb.vb_bh = xbs->xattr_bh;
6040 vb.vb_access = ocfs2_journal_access_xb;
6044 if (ocfs2_xattr_is_local(xe))
6047 vb.vb_xv = (struct ocfs2_xattr_value_root *)
6048 (base + name_offset + name_len);
6050 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
6051 &num_clusters, &vb.vb_xv->xr_list,
6059 * We just need to check the 1st extent record, since we always
6060 * CoW the whole xattr. So there shouldn't be a xattr with
6061 * some REFCOUNT extent recs after the 1st one.
6063 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
6066 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
6067 1, ref_tree, &ref_root_bh);
6074 * If we are deleting the xattr or the new size will be stored inside,
6075 * cool, leave it there, the xattr truncate process will remove them
6076 * for us(it still needs the refcount tree lock and the meta, credits).
6077 * And the worse case is that every cluster truncate will split the
6078 * refcount tree, and make the original extent become 3. So we will need
6079 * 2 * cluster more extent recs at most.
6081 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
6083 ret = ocfs2_refcounted_xattr_delete_need(inode,
6084 &(*ref_tree)->rf_ci,
6085 ref_root_bh, vb.vb_xv,
6092 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
6093 *ref_tree, ref_root_bh, 0,
6094 le32_to_cpu(vb.vb_xv->xr_clusters), p);
6099 brelse(ref_root_bh);
6104 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
6105 * The physical clusters will be added to refcount tree.
6107 static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
6108 struct ocfs2_xattr_value_root *xv,
6109 struct ocfs2_extent_tree *value_et,
6110 struct ocfs2_caching_info *ref_ci,
6111 struct buffer_head *ref_root_bh,
6112 struct ocfs2_cached_dealloc_ctxt *dealloc,
6113 struct ocfs2_post_refcount *refcount)
6116 u32 clusters = le32_to_cpu(xv->xr_clusters);
6117 u32 cpos, p_cluster, num_clusters;
6118 struct ocfs2_extent_list *el = &xv->xr_list;
6119 unsigned int ext_flags;
6122 while (cpos < clusters) {
6123 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
6124 &num_clusters, el, &ext_flags);
6126 cpos += num_clusters;
6127 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
6132 ret = ocfs2_add_refcount_flag(inode, value_et,
6133 ref_ci, ref_root_bh,
6134 cpos - num_clusters,
6135 p_cluster, num_clusters,
6147 * Given a normal ocfs2_xattr_header, refcount all the entries which
6148 * have value stored outside.
6149 * Used for xattrs stored in inode and ocfs2_xattr_block.
6151 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
6152 struct ocfs2_xattr_value_buf *vb,
6153 struct ocfs2_xattr_header *header,
6154 struct ocfs2_caching_info *ref_ci,
6155 struct buffer_head *ref_root_bh,
6156 struct ocfs2_cached_dealloc_ctxt *dealloc)
6159 struct ocfs2_xattr_entry *xe;
6160 struct ocfs2_xattr_value_root *xv;
6161 struct ocfs2_extent_tree et;
6164 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
6165 xe = &header->xh_entries[i];
6167 if (ocfs2_xattr_is_local(xe))
6170 xv = (struct ocfs2_xattr_value_root *)((void *)header +
6171 le16_to_cpu(xe->xe_name_offset) +
6172 OCFS2_XATTR_SIZE(xe->xe_name_len));
6175 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
6177 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
6178 ref_ci, ref_root_bh,
6189 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
6190 struct buffer_head *fe_bh,
6191 struct ocfs2_caching_info *ref_ci,
6192 struct buffer_head *ref_root_bh,
6193 struct ocfs2_cached_dealloc_ctxt *dealloc)
6195 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6196 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
6197 (fe_bh->b_data + inode->i_sb->s_blocksize -
6198 le16_to_cpu(di->i_xattr_inline_size));
6199 struct ocfs2_xattr_value_buf vb = {
6201 .vb_access = ocfs2_journal_access_di,
6204 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6205 ref_ci, ref_root_bh, dealloc);
6208 struct ocfs2_xattr_tree_value_refcount_para {
6209 struct ocfs2_caching_info *ref_ci;
6210 struct buffer_head *ref_root_bh;
6211 struct ocfs2_cached_dealloc_ctxt *dealloc;
6214 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
6215 struct ocfs2_xattr_bucket *bucket,
6217 struct ocfs2_xattr_value_root **xv,
6218 struct buffer_head **bh)
6220 int ret, block_off, name_offset;
6221 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
6222 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6225 ret = ocfs2_xattr_bucket_get_name_value(sb,
6235 base = bucket_block(bucket, block_off);
6237 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6238 OCFS2_XATTR_SIZE(xe->xe_name_len));
6241 *bh = bucket->bu_bhs[block_off];
6247 * For a given xattr bucket, refcount all the entries which
6248 * have value stored outside.
6250 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6251 struct ocfs2_xattr_bucket *bucket,
6255 struct ocfs2_extent_tree et;
6256 struct ocfs2_xattr_tree_value_refcount_para *ref =
6257 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6258 struct ocfs2_xattr_header *xh =
6259 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6260 struct ocfs2_xattr_entry *xe;
6261 struct ocfs2_xattr_value_buf vb = {
6262 .vb_access = ocfs2_journal_access,
6264 struct ocfs2_post_refcount refcount = {
6265 .credits = bucket->bu_blocks,
6267 .func = ocfs2_xattr_bucket_post_refcount,
6269 struct ocfs2_post_refcount *p = NULL;
6271 /* We only need post_refcount if we support metaecc. */
6272 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6275 mlog(0, "refcount bucket %llu, count = %u\n",
6276 (unsigned long long)bucket_blkno(bucket),
6277 le16_to_cpu(xh->xh_count));
6278 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6279 xe = &xh->xh_entries[i];
6281 if (ocfs2_xattr_is_local(xe))
6284 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6285 &vb.vb_xv, &vb.vb_bh);
6291 ocfs2_init_xattr_value_extent_tree(&et,
6292 INODE_CACHE(inode), &vb);
6294 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6308 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6309 struct buffer_head *root_bh,
6310 u64 blkno, u32 cpos, u32 len, void *para)
6312 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6313 ocfs2_xattr_bucket_value_refcount,
6317 static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6318 struct buffer_head *blk_bh,
6319 struct ocfs2_caching_info *ref_ci,
6320 struct buffer_head *ref_root_bh,
6321 struct ocfs2_cached_dealloc_ctxt *dealloc)
6324 struct ocfs2_xattr_block *xb =
6325 (struct ocfs2_xattr_block *)blk_bh->b_data;
6327 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6328 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6329 struct ocfs2_xattr_value_buf vb = {
6331 .vb_access = ocfs2_journal_access_xb,
6334 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6335 ref_ci, ref_root_bh,
6338 struct ocfs2_xattr_tree_value_refcount_para para = {
6340 .ref_root_bh = ref_root_bh,
6344 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6345 ocfs2_refcount_xattr_tree_rec,
6352 int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6353 struct buffer_head *fe_bh,
6354 struct ocfs2_caching_info *ref_ci,
6355 struct buffer_head *ref_root_bh,
6356 struct ocfs2_cached_dealloc_ctxt *dealloc)
6359 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6360 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6361 struct buffer_head *blk_bh = NULL;
6363 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6364 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6365 ref_ci, ref_root_bh,
6373 if (!di->i_xattr_loc)
6376 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6383 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6384 ref_root_bh, dealloc);
6394 typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
6396 * Store the information we need in xattr reflink.
6397 * old_bh and new_bh are inode bh for the old and new inode.
6399 struct ocfs2_xattr_reflink {
6400 struct inode *old_inode;
6401 struct inode *new_inode;
6402 struct buffer_head *old_bh;
6403 struct buffer_head *new_bh;
6404 struct ocfs2_caching_info *ref_ci;
6405 struct buffer_head *ref_root_bh;
6406 struct ocfs2_cached_dealloc_ctxt *dealloc;
6407 should_xattr_reflinked *xattr_reflinked;
6411 * Given a xattr header and xe offset,
6412 * return the proper xv and the corresponding bh.
6413 * xattr in inode, block and xattr tree have different implementaions.
6415 typedef int (get_xattr_value_root)(struct super_block *sb,
6416 struct buffer_head *bh,
6417 struct ocfs2_xattr_header *xh,
6419 struct ocfs2_xattr_value_root **xv,
6420 struct buffer_head **ret_bh,
6424 * Calculate all the xattr value root metadata stored in this xattr header and
6425 * credits we need if we create them from the scratch.
6426 * We use get_xattr_value_root so that all types of xattr container can use it.
6428 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6429 struct buffer_head *bh,
6430 struct ocfs2_xattr_header *xh,
6431 int *metas, int *credits,
6433 get_xattr_value_root *func,
6437 struct ocfs2_xattr_value_root *xv;
6438 struct ocfs2_xattr_entry *xe;
6440 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6441 xe = &xh->xh_entries[i];
6442 if (ocfs2_xattr_is_local(xe))
6445 ret = func(sb, bh, xh, i, &xv, NULL, para);
6451 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6452 le16_to_cpu(xv->xr_list.l_next_free_rec);
6454 *credits += ocfs2_calc_extend_credits(sb,
6456 le32_to_cpu(xv->xr_clusters));
6459 * If the value is a tree with depth > 1, We don't go deep
6460 * to the extent block, so just calculate a maximum record num.
6462 if (!xv->xr_list.l_tree_depth)
6463 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
6465 *num_recs += ocfs2_clusters_for_bytes(sb,
6472 /* Used by xattr inode and block to return the right xv and buffer_head. */
6473 static int ocfs2_get_xattr_value_root(struct super_block *sb,
6474 struct buffer_head *bh,
6475 struct ocfs2_xattr_header *xh,
6477 struct ocfs2_xattr_value_root **xv,
6478 struct buffer_head **ret_bh,
6481 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6483 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6484 le16_to_cpu(xe->xe_name_offset) +
6485 OCFS2_XATTR_SIZE(xe->xe_name_len));
6494 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6495 * It is only used for inline xattr and xattr block.
6497 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6498 struct ocfs2_xattr_header *xh,
6499 struct buffer_head *ref_root_bh,
6501 struct ocfs2_alloc_context **meta_ac)
6503 int ret, meta_add = 0, num_recs = 0;
6504 struct ocfs2_refcount_block *rb =
6505 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6509 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6510 &meta_add, credits, &num_recs,
6511 ocfs2_get_xattr_value_root,
6519 * We need to add/modify num_recs in refcount tree, so just calculate
6520 * an approximate number we need for refcount tree change.
6521 * Sometimes we need to split the tree, and after split, half recs
6522 * will be moved to the new block, and a new block can only provide
6523 * half number of recs. So we multiple new blocks by 2.
6525 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6526 meta_add += num_recs;
6527 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6528 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6529 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6530 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6534 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6543 * Given a xattr header, reflink all the xattrs in this container.
6544 * It can be used for inode, block and bucket.
6547 * Before we call this function, the caller has memcpy the xattr in
6548 * old_xh to the new_xh.
6550 * If args.xattr_reflinked is set, call it to decide whether the xe should
6551 * be reflinked or not. If not, remove it from the new xattr header.
6553 static int ocfs2_reflink_xattr_header(handle_t *handle,
6554 struct ocfs2_xattr_reflink *args,
6555 struct buffer_head *old_bh,
6556 struct ocfs2_xattr_header *xh,
6557 struct buffer_head *new_bh,
6558 struct ocfs2_xattr_header *new_xh,
6559 struct ocfs2_xattr_value_buf *vb,
6560 struct ocfs2_alloc_context *meta_ac,
6561 get_xattr_value_root *func,
6565 struct super_block *sb = args->old_inode->i_sb;
6566 struct buffer_head *value_bh;
6567 struct ocfs2_xattr_entry *xe, *last;
6568 struct ocfs2_xattr_value_root *xv, *new_xv;
6569 struct ocfs2_extent_tree data_et;
6570 u32 clusters, cpos, p_cluster, num_clusters;
6571 unsigned int ext_flags = 0;
6573 mlog(0, "reflink xattr in container %llu, count = %u\n",
6574 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
6576 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6577 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
6578 xe = &xh->xh_entries[i];
6580 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6581 xe = &new_xh->xh_entries[j];
6583 le16_add_cpu(&new_xh->xh_count, -1);
6584 if (new_xh->xh_count) {
6586 (void *)last - (void *)xe);
6588 sizeof(struct ocfs2_xattr_entry));
6592 * We don't want j to increase in the next round since
6593 * it is already moved ahead.
6599 if (ocfs2_xattr_is_local(xe))
6602 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6608 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6615 * For the xattr which has l_tree_depth = 0, all the extent
6616 * recs have already be copied to the new xh with the
6617 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6618 * increase the refount count int the refcount tree.
6620 * For the xattr which has l_tree_depth > 0, we need
6621 * to initialize it to the empty default value root,
6622 * and then insert the extents one by one.
6624 if (xv->xr_list.l_tree_depth) {
6625 memcpy(new_xv, &def_xv, sizeof(def_xv));
6627 vb->vb_bh = value_bh;
6628 ocfs2_init_xattr_value_extent_tree(&data_et,
6629 INODE_CACHE(args->new_inode), vb);
6632 clusters = le32_to_cpu(xv->xr_clusters);
6634 while (cpos < clusters) {
6635 ret = ocfs2_xattr_get_clusters(args->old_inode,
6648 if (xv->xr_list.l_tree_depth) {
6649 ret = ocfs2_insert_extent(handle,
6651 ocfs2_clusters_to_blocks(
6652 args->old_inode->i_sb,
6654 num_clusters, ext_flags,
6662 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6664 p_cluster, num_clusters,
6665 meta_ac, args->dealloc);
6671 cpos += num_clusters;
6679 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6681 int ret = 0, credits = 0;
6683 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6684 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6685 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6686 int header_off = osb->sb->s_blocksize - inline_size;
6687 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6688 (args->old_bh->b_data + header_off);
6689 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6690 (args->new_bh->b_data + header_off);
6691 struct ocfs2_alloc_context *meta_ac = NULL;
6692 struct ocfs2_inode_info *new_oi;
6693 struct ocfs2_dinode *new_di;
6694 struct ocfs2_xattr_value_buf vb = {
6695 .vb_bh = args->new_bh,
6696 .vb_access = ocfs2_journal_access_di,
6699 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6700 &credits, &meta_ac);
6706 handle = ocfs2_start_trans(osb, credits);
6707 if (IS_ERR(handle)) {
6708 ret = PTR_ERR(handle);
6713 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6714 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6720 memcpy(args->new_bh->b_data + header_off,
6721 args->old_bh->b_data + header_off, inline_size);
6723 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6724 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6726 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6727 args->new_bh, new_xh, &vb, meta_ac,
6728 ocfs2_get_xattr_value_root, NULL);
6734 new_oi = OCFS2_I(args->new_inode);
6735 spin_lock(&new_oi->ip_lock);
6736 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6737 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6738 spin_unlock(&new_oi->ip_lock);
6740 ocfs2_journal_dirty(handle, args->new_bh);
6743 ocfs2_commit_trans(osb, handle);
6747 ocfs2_free_alloc_context(meta_ac);
6751 static int ocfs2_create_empty_xattr_block(struct inode *inode,
6752 struct buffer_head *fe_bh,
6753 struct buffer_head **ret_bh,
6758 struct ocfs2_alloc_context *meta_ac;
6759 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6761 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
6767 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6768 if (IS_ERR(handle)) {
6769 ret = PTR_ERR(handle);
6774 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6775 (unsigned long long)fe_bh->b_blocknr, indexed);
6776 ret = ocfs2_create_xattr_block(handle, inode, fe_bh,
6777 meta_ac, ret_bh, indexed);
6781 ocfs2_commit_trans(osb, handle);
6783 ocfs2_free_alloc_context(meta_ac);
6787 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6788 struct buffer_head *blk_bh,
6789 struct buffer_head *new_blk_bh)
6791 int ret = 0, credits = 0;
6793 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6794 struct ocfs2_dinode *new_di;
6795 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6796 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6797 struct ocfs2_xattr_block *xb =
6798 (struct ocfs2_xattr_block *)blk_bh->b_data;
6799 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6800 struct ocfs2_xattr_block *new_xb =
6801 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6802 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6803 struct ocfs2_alloc_context *meta_ac;
6804 struct ocfs2_xattr_value_buf vb = {
6805 .vb_bh = new_blk_bh,
6806 .vb_access = ocfs2_journal_access_xb,
6809 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6810 &credits, &meta_ac);
6816 /* One more credits in case we need to add xattr flags in new inode. */
6817 handle = ocfs2_start_trans(osb, credits + 1);
6818 if (IS_ERR(handle)) {
6819 ret = PTR_ERR(handle);
6824 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6825 ret = ocfs2_journal_access_di(handle,
6826 INODE_CACHE(args->new_inode),
6828 OCFS2_JOURNAL_ACCESS_WRITE);
6835 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6836 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6842 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6843 osb->sb->s_blocksize - header_off);
6845 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6846 new_blk_bh, new_xh, &vb, meta_ac,
6847 ocfs2_get_xattr_value_root, NULL);
6853 ocfs2_journal_dirty(handle, new_blk_bh);
6855 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6856 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6857 spin_lock(&new_oi->ip_lock);
6858 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6859 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6860 spin_unlock(&new_oi->ip_lock);
6862 ocfs2_journal_dirty(handle, args->new_bh);
6866 ocfs2_commit_trans(osb, handle);
6869 ocfs2_free_alloc_context(meta_ac);
6873 struct ocfs2_reflink_xattr_tree_args {
6874 struct ocfs2_xattr_reflink *reflink;
6875 struct buffer_head *old_blk_bh;
6876 struct buffer_head *new_blk_bh;
6877 struct ocfs2_xattr_bucket *old_bucket;
6878 struct ocfs2_xattr_bucket *new_bucket;
6883 * We have to handle the case that both old bucket and new bucket
6884 * will call this function to get the right ret_bh.
6885 * So The caller must give us the right bh.
6887 static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6888 struct buffer_head *bh,
6889 struct ocfs2_xattr_header *xh,
6891 struct ocfs2_xattr_value_root **xv,
6892 struct buffer_head **ret_bh,
6895 struct ocfs2_reflink_xattr_tree_args *args =
6896 (struct ocfs2_reflink_xattr_tree_args *)para;
6897 struct ocfs2_xattr_bucket *bucket;
6899 if (bh == args->old_bucket->bu_bhs[0])
6900 bucket = args->old_bucket;
6902 bucket = args->new_bucket;
6904 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6908 struct ocfs2_value_tree_metas {
6914 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6915 struct buffer_head *bh,
6916 struct ocfs2_xattr_header *xh,
6918 struct ocfs2_xattr_value_root **xv,
6919 struct buffer_head **ret_bh,
6922 struct ocfs2_xattr_bucket *bucket =
6923 (struct ocfs2_xattr_bucket *)para;
6925 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6929 static int ocfs2_calc_value_tree_metas(struct inode *inode,
6930 struct ocfs2_xattr_bucket *bucket,
6933 struct ocfs2_value_tree_metas *metas =
6934 (struct ocfs2_value_tree_metas *)para;
6935 struct ocfs2_xattr_header *xh =
6936 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6938 /* Add the credits for this bucket first. */
6939 metas->credits += bucket->bu_blocks;
6940 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6941 xh, &metas->num_metas,
6942 &metas->credits, &metas->num_recs,
6943 ocfs2_value_tree_metas_in_bucket,
6948 * Given a xattr extent rec starting from blkno and having len clusters,
6949 * iterate all the buckets calculate how much metadata we need for reflinking
6950 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6952 static int ocfs2_lock_reflink_xattr_rec_allocators(
6953 struct ocfs2_reflink_xattr_tree_args *args,
6954 struct ocfs2_extent_tree *xt_et,
6955 u64 blkno, u32 len, int *credits,
6956 struct ocfs2_alloc_context **meta_ac,
6957 struct ocfs2_alloc_context **data_ac)
6959 int ret, num_free_extents;
6960 struct ocfs2_value_tree_metas metas;
6961 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6962 struct ocfs2_refcount_block *rb;
6964 memset(&metas, 0, sizeof(metas));
6966 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6967 ocfs2_calc_value_tree_metas, &metas);
6973 *credits = metas.credits;
6976 * Calculate we need for refcount tree change.
6978 * We need to add/modify num_recs in refcount tree, so just calculate
6979 * an approximate number we need for refcount tree change.
6980 * Sometimes we need to split the tree, and after split, half recs
6981 * will be moved to the new block, and a new block can only provide
6982 * half number of recs. So we multiple new blocks by 2.
6983 * In the end, we have to add credits for modifying the already
6984 * existed refcount block.
6986 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6988 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6989 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6990 metas.num_metas += metas.num_recs;
6991 *credits += metas.num_recs +
6992 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6993 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6994 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6995 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6999 /* count in the xattr tree change. */
7000 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
7001 if (num_free_extents < 0) {
7002 ret = num_free_extents;
7007 if (num_free_extents < len)
7008 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
7010 *credits += ocfs2_calc_extend_credits(osb->sb,
7011 xt_et->et_root_el, len);
7013 if (metas.num_metas) {
7014 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
7023 ret = ocfs2_reserve_clusters(osb, len, data_ac);
7030 ocfs2_free_alloc_context(*meta_ac);
7038 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
7039 u64 blkno, u64 new_blkno, u32 clusters,
7040 struct ocfs2_alloc_context *meta_ac,
7041 struct ocfs2_alloc_context *data_ac,
7042 struct ocfs2_reflink_xattr_tree_args *args)
7045 struct super_block *sb = args->reflink->old_inode->i_sb;
7046 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
7047 u32 num_buckets = clusters * bpc;
7048 int bpb = args->old_bucket->bu_blocks;
7049 struct ocfs2_xattr_value_buf vb = {
7050 .vb_access = ocfs2_journal_access,
7053 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
7054 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
7060 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
7067 * The real bucket num in this series of blocks is stored
7068 * in the 1st bucket.
7071 num_buckets = le16_to_cpu(
7072 bucket_xh(args->old_bucket)->xh_num_buckets);
7074 ret = ocfs2_xattr_bucket_journal_access(handle,
7076 OCFS2_JOURNAL_ACCESS_CREATE);
7082 for (j = 0; j < bpb; j++)
7083 memcpy(bucket_block(args->new_bucket, j),
7084 bucket_block(args->old_bucket, j),
7087 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
7089 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
7090 args->old_bucket->bu_bhs[0],
7091 bucket_xh(args->old_bucket),
7092 args->new_bucket->bu_bhs[0],
7093 bucket_xh(args->new_bucket),
7095 ocfs2_get_reflink_xattr_value_root,
7103 * Re-access and dirty the bucket to calculate metaecc.
7104 * Because we may extend the transaction in reflink_xattr_header
7105 * which will let the already accessed block gone.
7107 ret = ocfs2_xattr_bucket_journal_access(handle,
7109 OCFS2_JOURNAL_ACCESS_WRITE);
7115 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
7116 ocfs2_xattr_bucket_relse(args->old_bucket);
7117 ocfs2_xattr_bucket_relse(args->new_bucket);
7120 ocfs2_xattr_bucket_relse(args->old_bucket);
7121 ocfs2_xattr_bucket_relse(args->new_bucket);
7125 * Create the same xattr extent record in the new inode's xattr tree.
7127 static int ocfs2_reflink_xattr_rec(struct inode *inode,
7128 struct buffer_head *root_bh,
7134 int ret, credits = 0;
7135 u32 p_cluster, num_clusters;
7138 struct ocfs2_reflink_xattr_tree_args *args =
7139 (struct ocfs2_reflink_xattr_tree_args *)para;
7140 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7141 struct ocfs2_alloc_context *meta_ac = NULL;
7142 struct ocfs2_alloc_context *data_ac = NULL;
7143 struct ocfs2_extent_tree et;
7145 ocfs2_init_xattr_tree_extent_tree(&et,
7146 INODE_CACHE(args->reflink->new_inode),
7149 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
7151 &meta_ac, &data_ac);
7157 handle = ocfs2_start_trans(osb, credits);
7158 if (IS_ERR(handle)) {
7159 ret = PTR_ERR(handle);
7164 ret = ocfs2_claim_clusters(osb, handle, data_ac,
7165 len, &p_cluster, &num_clusters);
7171 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
7173 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
7174 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
7175 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
7176 meta_ac, data_ac, args);
7182 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
7183 (unsigned long long)new_blkno, len, cpos);
7184 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
7190 ocfs2_commit_trans(osb, handle);
7194 ocfs2_free_alloc_context(meta_ac);
7196 ocfs2_free_alloc_context(data_ac);
7201 * Create reflinked xattr buckets.
7202 * We will add bucket one by one, and refcount all the xattrs in the bucket
7203 * if they are stored outside.
7205 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
7206 struct buffer_head *blk_bh,
7207 struct buffer_head *new_blk_bh)
7210 struct ocfs2_reflink_xattr_tree_args para;
7212 memset(¶, 0, sizeof(para));
7213 para.reflink = args;
7214 para.old_blk_bh = blk_bh;
7215 para.new_blk_bh = new_blk_bh;
7217 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
7218 if (!para.old_bucket) {
7219 mlog_errno(-ENOMEM);
7223 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
7224 if (!para.new_bucket) {
7230 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
7231 ocfs2_reflink_xattr_rec,
7237 ocfs2_xattr_bucket_free(para.old_bucket);
7238 ocfs2_xattr_bucket_free(para.new_bucket);
7242 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7243 struct buffer_head *blk_bh)
7245 int ret, indexed = 0;
7246 struct buffer_head *new_blk_bh = NULL;
7247 struct ocfs2_xattr_block *xb =
7248 (struct ocfs2_xattr_block *)blk_bh->b_data;
7251 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7254 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7255 &new_blk_bh, indexed);
7261 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7262 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7264 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7273 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7275 int type = ocfs2_xattr_get_type(xe);
7277 return type != OCFS2_XATTR_INDEX_SECURITY &&
7278 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7279 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7282 int ocfs2_reflink_xattrs(struct inode *old_inode,
7283 struct buffer_head *old_bh,
7284 struct inode *new_inode,
7285 struct buffer_head *new_bh,
7286 bool preserve_security)
7289 struct ocfs2_xattr_reflink args;
7290 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7291 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7292 struct buffer_head *blk_bh = NULL;
7293 struct ocfs2_cached_dealloc_ctxt dealloc;
7294 struct ocfs2_refcount_tree *ref_tree;
7295 struct buffer_head *ref_root_bh = NULL;
7297 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7298 le64_to_cpu(di->i_refcount_loc),
7299 1, &ref_tree, &ref_root_bh);
7305 ocfs2_init_dealloc_ctxt(&dealloc);
7307 args.old_inode = old_inode;
7308 args.new_inode = new_inode;
7309 args.old_bh = old_bh;
7310 args.new_bh = new_bh;
7311 args.ref_ci = &ref_tree->rf_ci;
7312 args.ref_root_bh = ref_root_bh;
7313 args.dealloc = &dealloc;
7314 if (preserve_security)
7315 args.xattr_reflinked = NULL;
7317 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
7319 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7320 ret = ocfs2_reflink_xattr_inline(&args);
7327 if (!di->i_xattr_loc)
7330 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7337 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7344 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7346 brelse(ref_root_bh);
7348 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7349 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7350 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7358 * Initialize security and acl for a already created inode.
7359 * Used for reflink a non-preserve-security file.
7361 * It uses common api like ocfs2_xattr_set, so the caller
7362 * must not hold any lock expect i_mutex.
7364 int ocfs2_init_security_and_acl(struct inode *dir,
7365 struct inode *inode)
7368 struct buffer_head *dir_bh = NULL;
7369 struct ocfs2_security_xattr_info si = {
7373 ret = ocfs2_init_security_get(inode, dir, &si);
7375 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7376 si.name, si.value, si.value_len,
7382 } else if (ret != -EOPNOTSUPP) {
7387 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7393 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7397 ocfs2_inode_unlock(dir, 0);
7403 * 'security' attributes support
7405 static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
7406 size_t list_size, const char *name,
7407 size_t name_len, int type)
7409 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7410 const size_t total_len = prefix_len + name_len + 1;
7412 if (list && total_len <= list_size) {
7413 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7414 memcpy(list + prefix_len, name, name_len);
7415 list[prefix_len + name_len] = '\0';
7420 static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7421 void *buffer, size_t size, int type)
7423 if (strcmp(name, "") == 0)
7425 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7426 name, buffer, size);
7429 static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7430 const void *value, size_t size, int flags, int type)
7432 if (strcmp(name, "") == 0)
7435 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7436 name, value, size, flags);
7439 int ocfs2_init_security_get(struct inode *inode,
7441 struct ocfs2_security_xattr_info *si)
7443 /* check whether ocfs2 support feature xattr */
7444 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7446 return security_inode_init_security(inode, dir, &si->name, &si->value,
7450 int ocfs2_init_security_set(handle_t *handle,
7451 struct inode *inode,
7452 struct buffer_head *di_bh,
7453 struct ocfs2_security_xattr_info *si,
7454 struct ocfs2_alloc_context *xattr_ac,
7455 struct ocfs2_alloc_context *data_ac)
7457 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7458 OCFS2_XATTR_INDEX_SECURITY,
7459 si->name, si->value, si->value_len, 0,
7463 struct xattr_handler ocfs2_xattr_security_handler = {
7464 .prefix = XATTR_SECURITY_PREFIX,
7465 .list = ocfs2_xattr_security_list,
7466 .get = ocfs2_xattr_security_get,
7467 .set = ocfs2_xattr_security_set,
7471 * 'trusted' attributes support
7473 static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
7474 size_t list_size, const char *name,
7475 size_t name_len, int type)
7477 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
7478 const size_t total_len = prefix_len + name_len + 1;
7480 if (list && total_len <= list_size) {
7481 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7482 memcpy(list + prefix_len, name, name_len);
7483 list[prefix_len + name_len] = '\0';
7488 static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7489 void *buffer, size_t size, int type)
7491 if (strcmp(name, "") == 0)
7493 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7494 name, buffer, size);
7497 static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7498 const void *value, size_t size, int flags, int type)
7500 if (strcmp(name, "") == 0)
7503 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7504 name, value, size, flags);
7507 struct xattr_handler ocfs2_xattr_trusted_handler = {
7508 .prefix = XATTR_TRUSTED_PREFIX,
7509 .list = ocfs2_xattr_trusted_list,
7510 .get = ocfs2_xattr_trusted_get,
7511 .set = ocfs2_xattr_trusted_set,
7515 * 'user' attributes support
7517 static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
7518 size_t list_size, const char *name,
7519 size_t name_len, int type)
7521 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7522 const size_t total_len = prefix_len + name_len + 1;
7523 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7525 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7528 if (list && total_len <= list_size) {
7529 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7530 memcpy(list + prefix_len, name, name_len);
7531 list[prefix_len + name_len] = '\0';
7536 static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7537 void *buffer, size_t size, int type)
7539 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7541 if (strcmp(name, "") == 0)
7543 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7545 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
7549 static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7550 const void *value, size_t size, int flags, int type)
7552 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7554 if (strcmp(name, "") == 0)
7556 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7559 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7560 name, value, size, flags);
7563 struct xattr_handler ocfs2_xattr_user_handler = {
7564 .prefix = XATTR_USER_PREFIX,
7565 .list = ocfs2_xattr_user_list,
7566 .get = ocfs2_xattr_user_get,
7567 .set = ocfs2_xattr_user_set,