1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
8 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
9 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 #include <linux/capability.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/highmem.h>
17 #include <linux/pagemap.h>
18 #include <linux/uio.h>
19 #include <linux/sched.h>
20 #include <linux/splice.h>
21 #include <linux/mount.h>
22 #include <linux/writeback.h>
23 #include <linux/falloc.h>
24 #include <linux/sort.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/string.h>
28 #include <linux/security.h>
30 #include <cluster/masklog.h>
34 #include "blockcheck.h"
44 #include "buffer_head_io.h"
47 #include "refcounttree.h"
49 #include "ocfs2_trace.h"
51 struct ocfs2_xattr_def_value_root
{
52 struct ocfs2_xattr_value_root xv
;
53 struct ocfs2_extent_rec er
;
56 struct ocfs2_xattr_bucket
{
57 /* The inode these xattrs are associated with */
58 struct inode
*bu_inode
;
60 /* The actual buffers that make up the bucket */
61 struct buffer_head
*bu_bhs
[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
];
63 /* How many blocks make up one bucket for this filesystem */
67 struct ocfs2_xattr_set_ctxt
{
69 struct ocfs2_alloc_context
*meta_ac
;
70 struct ocfs2_alloc_context
*data_ac
;
71 struct ocfs2_cached_dealloc_ctxt dealloc
;
75 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
76 #define OCFS2_XATTR_INLINE_SIZE 80
77 #define OCFS2_XATTR_HEADER_GAP 4
78 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
79 - sizeof(struct ocfs2_xattr_header) \
80 - OCFS2_XATTR_HEADER_GAP)
81 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
82 - sizeof(struct ocfs2_xattr_block) \
83 - sizeof(struct ocfs2_xattr_header) \
84 - OCFS2_XATTR_HEADER_GAP)
86 static struct ocfs2_xattr_def_value_root def_xv
= {
87 .xv
.xr_list
.l_count
= cpu_to_le16(1),
90 const struct xattr_handler
* const ocfs2_xattr_handlers
[] = {
91 &ocfs2_xattr_user_handler
,
92 &ocfs2_xattr_trusted_handler
,
93 &ocfs2_xattr_security_handler
,
97 static const struct xattr_handler
* const ocfs2_xattr_handler_map
[OCFS2_XATTR_MAX
] = {
98 [OCFS2_XATTR_INDEX_USER
] = &ocfs2_xattr_user_handler
,
99 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
] = &nop_posix_acl_access
,
100 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
] = &nop_posix_acl_default
,
101 [OCFS2_XATTR_INDEX_TRUSTED
] = &ocfs2_xattr_trusted_handler
,
102 [OCFS2_XATTR_INDEX_SECURITY
] = &ocfs2_xattr_security_handler
,
105 struct ocfs2_xattr_info
{
109 const void *xi_value
;
113 struct ocfs2_xattr_search
{
114 struct buffer_head
*inode_bh
;
116 * xattr_bh point to the block buffer head which has extended attribute
117 * when extended attribute in inode, xattr_bh is equal to inode_bh.
119 struct buffer_head
*xattr_bh
;
120 struct ocfs2_xattr_header
*header
;
121 struct ocfs2_xattr_bucket
*bucket
;
124 struct ocfs2_xattr_entry
*here
;
128 /* Operations on struct ocfs2_xa_entry */
130 struct ocfs2_xa_loc_operations
{
134 int (*xlo_journal_access
)(handle_t
*handle
, struct ocfs2_xa_loc
*loc
,
136 void (*xlo_journal_dirty
)(handle_t
*handle
, struct ocfs2_xa_loc
*loc
);
139 * Return a pointer to the appropriate buffer in loc->xl_storage
140 * at the given offset from loc->xl_header.
142 void *(*xlo_offset_pointer
)(struct ocfs2_xa_loc
*loc
, int offset
);
144 /* Can we reuse the existing entry for the new value? */
145 int (*xlo_can_reuse
)(struct ocfs2_xa_loc
*loc
,
146 struct ocfs2_xattr_info
*xi
);
148 /* How much space is needed for the new value? */
149 int (*xlo_check_space
)(struct ocfs2_xa_loc
*loc
,
150 struct ocfs2_xattr_info
*xi
);
153 * Return the offset of the first name+value pair. This is
154 * the start of our downward-filling free space.
156 int (*xlo_get_free_start
)(struct ocfs2_xa_loc
*loc
);
159 * Remove the name+value at this location. Do whatever is
160 * appropriate with the remaining name+value pairs.
162 void (*xlo_wipe_namevalue
)(struct ocfs2_xa_loc
*loc
);
164 /* Fill xl_entry with a new entry */
165 void (*xlo_add_entry
)(struct ocfs2_xa_loc
*loc
, u32 name_hash
);
167 /* Add name+value storage to an entry */
168 void (*xlo_add_namevalue
)(struct ocfs2_xa_loc
*loc
, int size
);
171 * Initialize the value buf's access and bh fields for this entry.
172 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
174 void (*xlo_fill_value_buf
)(struct ocfs2_xa_loc
*loc
,
175 struct ocfs2_xattr_value_buf
*vb
);
179 * Describes an xattr entry location. This is a memory structure
180 * tracking the on-disk structure.
182 struct ocfs2_xa_loc
{
183 /* This xattr belongs to this inode */
184 struct inode
*xl_inode
;
186 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
187 struct ocfs2_xattr_header
*xl_header
;
189 /* Bytes from xl_header to the end of the storage */
193 * The ocfs2_xattr_entry this location describes. If this is
194 * NULL, this location describes the on-disk structure where it
197 struct ocfs2_xattr_entry
*xl_entry
;
200 * Internal housekeeping
203 /* Buffer(s) containing this entry */
206 /* Operations on the storage backing this location */
207 const struct ocfs2_xa_loc_operations
*xl_ops
;
211 * Convenience functions to calculate how much space is needed for a
212 * given name+value pair
214 static int namevalue_size(int name_len
, uint64_t value_len
)
216 if (value_len
> OCFS2_XATTR_INLINE_SIZE
)
217 return OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
219 return OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_SIZE(value_len
);
222 static int namevalue_size_xi(struct ocfs2_xattr_info
*xi
)
224 return namevalue_size(xi
->xi_name_len
, xi
->xi_value_len
);
227 static int namevalue_size_xe(struct ocfs2_xattr_entry
*xe
)
229 u64 value_len
= le64_to_cpu(xe
->xe_value_size
);
231 BUG_ON((value_len
> OCFS2_XATTR_INLINE_SIZE
) &&
232 ocfs2_xattr_is_local(xe
));
233 return namevalue_size(xe
->xe_name_len
, value_len
);
237 static int ocfs2_xattr_bucket_get_name_value(struct super_block
*sb
,
238 struct ocfs2_xattr_header
*xh
,
243 static int ocfs2_xattr_block_find(struct inode
*inode
,
246 struct ocfs2_xattr_search
*xs
);
247 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
248 struct buffer_head
*root_bh
,
251 struct ocfs2_xattr_search
*xs
);
253 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
254 struct buffer_head
*blk_bh
,
258 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
259 struct ocfs2_xattr_search
*xs
,
260 struct ocfs2_xattr_set_ctxt
*ctxt
);
262 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
263 struct ocfs2_xattr_info
*xi
,
264 struct ocfs2_xattr_search
*xs
,
265 struct ocfs2_xattr_set_ctxt
*ctxt
);
267 typedef int (xattr_tree_rec_func
)(struct inode
*inode
,
268 struct buffer_head
*root_bh
,
269 u64 blkno
, u32 cpos
, u32 len
, void *para
);
270 static int ocfs2_iterate_xattr_index_block(struct inode
*inode
,
271 struct buffer_head
*root_bh
,
272 xattr_tree_rec_func
*rec_func
,
274 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
275 struct ocfs2_xattr_bucket
*bucket
,
277 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
278 struct buffer_head
*root_bh
,
284 static int ocfs2_mv_xattr_buckets(struct inode
*inode
, handle_t
*handle
,
285 u64 src_blk
, u64 last_blk
, u64 to_blk
,
286 unsigned int start_bucket
,
288 static int ocfs2_prepare_refcount_xattr(struct inode
*inode
,
289 struct ocfs2_dinode
*di
,
290 struct ocfs2_xattr_info
*xi
,
291 struct ocfs2_xattr_search
*xis
,
292 struct ocfs2_xattr_search
*xbs
,
293 struct ocfs2_refcount_tree
**ref_tree
,
296 static int ocfs2_get_xattr_tree_value_root(struct super_block
*sb
,
297 struct ocfs2_xattr_bucket
*bucket
,
299 struct ocfs2_xattr_value_root
**xv
,
300 struct buffer_head
**bh
);
302 static inline u16
ocfs2_xattr_buckets_per_cluster(struct ocfs2_super
*osb
)
304 return (1 << osb
->s_clustersize_bits
) / OCFS2_XATTR_BUCKET_SIZE
;
307 static inline u16
ocfs2_blocks_per_xattr_bucket(struct super_block
*sb
)
309 return OCFS2_XATTR_BUCKET_SIZE
/ (1 << sb
->s_blocksize_bits
);
312 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
313 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
314 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
316 static struct ocfs2_xattr_bucket
*ocfs2_xattr_bucket_new(struct inode
*inode
)
318 struct ocfs2_xattr_bucket
*bucket
;
319 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
321 BUG_ON(blks
> OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
);
323 bucket
= kzalloc(sizeof(struct ocfs2_xattr_bucket
), GFP_NOFS
);
325 bucket
->bu_inode
= inode
;
326 bucket
->bu_blocks
= blks
;
332 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket
*bucket
)
336 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
337 brelse(bucket
->bu_bhs
[i
]);
338 bucket
->bu_bhs
[i
] = NULL
;
342 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket
*bucket
)
345 ocfs2_xattr_bucket_relse(bucket
);
346 bucket
->bu_inode
= NULL
;
352 * A bucket that has never been written to disk doesn't need to be
353 * read. We just need the buffer_heads. Don't call this for
354 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
357 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
358 u64 xb_blkno
, int new)
362 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
363 bucket
->bu_bhs
[i
] = sb_getblk(bucket
->bu_inode
->i_sb
,
365 if (!bucket
->bu_bhs
[i
]) {
371 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket
->bu_inode
),
372 bucket
->bu_bhs
[i
])) {
374 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket
->bu_inode
),
377 set_buffer_uptodate(bucket
->bu_bhs
[i
]);
378 ocfs2_set_buffer_uptodate(INODE_CACHE(bucket
->bu_inode
),
385 ocfs2_xattr_bucket_relse(bucket
);
389 /* Read the xattr bucket at xb_blkno */
390 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
395 rc
= ocfs2_read_blocks(INODE_CACHE(bucket
->bu_inode
), xb_blkno
,
396 bucket
->bu_blocks
, bucket
->bu_bhs
, 0,
399 spin_lock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
400 rc
= ocfs2_validate_meta_ecc_bhs(bucket
->bu_inode
->i_sb
,
403 &bucket_xh(bucket
)->xh_check
);
404 spin_unlock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
410 ocfs2_xattr_bucket_relse(bucket
);
414 static int ocfs2_xattr_bucket_journal_access(handle_t
*handle
,
415 struct ocfs2_xattr_bucket
*bucket
,
420 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
421 rc
= ocfs2_journal_access(handle
,
422 INODE_CACHE(bucket
->bu_inode
),
423 bucket
->bu_bhs
[i
], type
);
433 static void ocfs2_xattr_bucket_journal_dirty(handle_t
*handle
,
434 struct ocfs2_xattr_bucket
*bucket
)
438 spin_lock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
439 ocfs2_compute_meta_ecc_bhs(bucket
->bu_inode
->i_sb
,
440 bucket
->bu_bhs
, bucket
->bu_blocks
,
441 &bucket_xh(bucket
)->xh_check
);
442 spin_unlock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
444 for (i
= 0; i
< bucket
->bu_blocks
; i
++)
445 ocfs2_journal_dirty(handle
, bucket
->bu_bhs
[i
]);
448 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket
*dest
,
449 struct ocfs2_xattr_bucket
*src
)
452 int blocksize
= src
->bu_inode
->i_sb
->s_blocksize
;
454 BUG_ON(dest
->bu_blocks
!= src
->bu_blocks
);
455 BUG_ON(dest
->bu_inode
!= src
->bu_inode
);
457 for (i
= 0; i
< src
->bu_blocks
; i
++) {
458 memcpy(bucket_block(dest
, i
), bucket_block(src
, i
),
463 static int ocfs2_validate_xattr_block(struct super_block
*sb
,
464 struct buffer_head
*bh
)
467 struct ocfs2_xattr_block
*xb
=
468 (struct ocfs2_xattr_block
*)bh
->b_data
;
470 trace_ocfs2_validate_xattr_block((unsigned long long)bh
->b_blocknr
);
472 BUG_ON(!buffer_uptodate(bh
));
475 * If the ecc fails, we return the error but otherwise
476 * leave the filesystem running. We know any error is
477 * local to this block.
479 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &xb
->xb_check
);
484 * Errors after here are fatal
487 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
488 return ocfs2_error(sb
,
489 "Extended attribute block #%llu has bad signature %.*s\n",
490 (unsigned long long)bh
->b_blocknr
, 7,
494 if (le64_to_cpu(xb
->xb_blkno
) != bh
->b_blocknr
) {
495 return ocfs2_error(sb
,
496 "Extended attribute block #%llu has an invalid xb_blkno of %llu\n",
497 (unsigned long long)bh
->b_blocknr
,
498 (unsigned long long)le64_to_cpu(xb
->xb_blkno
));
501 if (le32_to_cpu(xb
->xb_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
502 return ocfs2_error(sb
,
503 "Extended attribute block #%llu has an invalid xb_fs_generation of #%u\n",
504 (unsigned long long)bh
->b_blocknr
,
505 le32_to_cpu(xb
->xb_fs_generation
));
511 static int ocfs2_read_xattr_block(struct inode
*inode
, u64 xb_blkno
,
512 struct buffer_head
**bh
)
515 struct buffer_head
*tmp
= *bh
;
517 rc
= ocfs2_read_block(INODE_CACHE(inode
), xb_blkno
, &tmp
,
518 ocfs2_validate_xattr_block
);
520 /* If ocfs2_read_block() got us a new bh, pass it up. */
527 static inline const char *ocfs2_xattr_prefix(int name_index
)
529 const struct xattr_handler
*handler
= NULL
;
531 if (name_index
> 0 && name_index
< OCFS2_XATTR_MAX
)
532 handler
= ocfs2_xattr_handler_map
[name_index
];
533 return handler
? xattr_prefix(handler
) : NULL
;
536 static u32
ocfs2_xattr_name_hash(struct inode
*inode
,
540 /* Get hash value of uuid from super block */
541 u32 hash
= OCFS2_SB(inode
->i_sb
)->uuid_hash
;
544 /* hash extended attribute name */
545 for (i
= 0; i
< name_len
; i
++) {
546 hash
= (hash
<< OCFS2_HASH_SHIFT
) ^
547 (hash
>> (8*sizeof(hash
) - OCFS2_HASH_SHIFT
)) ^
554 static int ocfs2_xattr_entry_real_size(int name_len
, size_t value_len
)
556 return namevalue_size(name_len
, value_len
) +
557 sizeof(struct ocfs2_xattr_entry
);
560 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info
*xi
)
562 return namevalue_size_xi(xi
) +
563 sizeof(struct ocfs2_xattr_entry
);
566 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry
*xe
)
568 return namevalue_size_xe(xe
) +
569 sizeof(struct ocfs2_xattr_entry
);
572 int ocfs2_calc_security_init(struct inode
*dir
,
573 struct ocfs2_security_xattr_info
*si
,
576 struct ocfs2_alloc_context
**xattr_ac
)
579 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
580 int s_size
= ocfs2_xattr_entry_real_size(strlen(si
->name
),
584 * The max space of security xattr taken inline is
585 * 256(name) + 80(value) + 16(entry) = 352 bytes,
586 * So reserve one metadata block for it is ok.
588 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
||
589 s_size
> OCFS2_XATTR_FREE_IN_IBODY
) {
590 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, xattr_ac
);
595 *xattr_credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
598 /* reserve clusters for xattr value which will be set in B tree*/
599 if (si
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
600 int new_clusters
= ocfs2_clusters_for_bytes(dir
->i_sb
,
603 *xattr_credits
+= ocfs2_clusters_to_blocks(dir
->i_sb
,
605 *want_clusters
+= new_clusters
;
610 int ocfs2_calc_xattr_init(struct inode
*dir
,
611 struct buffer_head
*dir_bh
,
613 struct ocfs2_security_xattr_info
*si
,
619 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
620 int s_size
= 0, a_size
= 0, acl_len
= 0, new_clusters
;
623 s_size
= ocfs2_xattr_entry_real_size(strlen(si
->name
),
626 if (osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
) {
627 down_read(&OCFS2_I(dir
)->ip_xattr_sem
);
628 acl_len
= ocfs2_xattr_get_nolock(dir
, dir_bh
,
629 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
,
631 up_read(&OCFS2_I(dir
)->ip_xattr_sem
);
633 a_size
= ocfs2_xattr_entry_real_size(0, acl_len
);
636 } else if (acl_len
!= 0 && acl_len
!= -ENODATA
) {
643 if (!(s_size
+ a_size
))
647 * The max space of security xattr taken inline is
648 * 256(name) + 80(value) + 16(entry) = 352 bytes,
649 * The max space of acl xattr taken inline is
650 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
651 * when blocksize = 512, may reserve one more cluser for
652 * xattr bucket, otherwise reserve one metadata block
654 * If this is a new directory with inline data,
655 * we choose to reserve the entire inline area for
656 * directory contents and force an external xattr block.
658 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
||
659 (S_ISDIR(mode
) && ocfs2_supports_inline_data(osb
)) ||
660 (s_size
+ a_size
) > OCFS2_XATTR_FREE_IN_IBODY
) {
661 *want_meta
= *want_meta
+ 1;
662 *xattr_credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
665 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
&&
666 (s_size
+ a_size
) > OCFS2_XATTR_FREE_IN_BLOCK(dir
)) {
668 *xattr_credits
+= ocfs2_blocks_per_xattr_bucket(dir
->i_sb
);
672 * reserve credits and clusters for xattrs which has large value
673 * and have to be set outside
675 if (si
->enable
&& si
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
676 new_clusters
= ocfs2_clusters_for_bytes(dir
->i_sb
,
678 *xattr_credits
+= ocfs2_clusters_to_blocks(dir
->i_sb
,
680 *want_clusters
+= new_clusters
;
682 if (osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
&&
683 acl_len
> OCFS2_XATTR_INLINE_SIZE
) {
684 /* for directory, it has DEFAULT and ACCESS two types of acls */
685 new_clusters
= (S_ISDIR(mode
) ? 2 : 1) *
686 ocfs2_clusters_for_bytes(dir
->i_sb
, acl_len
);
687 *xattr_credits
+= ocfs2_clusters_to_blocks(dir
->i_sb
,
689 *want_clusters
+= new_clusters
;
695 static int ocfs2_xattr_extend_allocation(struct inode
*inode
,
697 struct ocfs2_xattr_value_buf
*vb
,
698 struct ocfs2_xattr_set_ctxt
*ctxt
)
700 int status
= 0, credits
;
701 handle_t
*handle
= ctxt
->handle
;
702 enum ocfs2_alloc_restarted why
;
703 u32 prev_clusters
, logical_start
= le32_to_cpu(vb
->vb_xv
->xr_clusters
);
704 struct ocfs2_extent_tree et
;
706 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
708 while (clusters_to_add
) {
709 trace_ocfs2_xattr_extend_allocation(clusters_to_add
);
711 status
= vb
->vb_access(handle
, INODE_CACHE(inode
), vb
->vb_bh
,
712 OCFS2_JOURNAL_ACCESS_WRITE
);
718 prev_clusters
= le32_to_cpu(vb
->vb_xv
->xr_clusters
);
719 status
= ocfs2_add_clusters_in_btree(handle
,
727 if ((status
< 0) && (status
!= -EAGAIN
)) {
728 if (status
!= -ENOSPC
)
733 ocfs2_journal_dirty(handle
, vb
->vb_bh
);
735 clusters_to_add
-= le32_to_cpu(vb
->vb_xv
->xr_clusters
) -
738 if (why
!= RESTART_NONE
&& clusters_to_add
) {
740 * We can only fail in case the alloc file doesn't give
741 * up enough clusters.
743 BUG_ON(why
== RESTART_META
);
745 credits
= ocfs2_calc_extend_credits(inode
->i_sb
,
746 &vb
->vb_xv
->xr_list
);
747 status
= ocfs2_extend_trans(handle
, credits
);
759 static int __ocfs2_remove_xattr_range(struct inode
*inode
,
760 struct ocfs2_xattr_value_buf
*vb
,
761 u32 cpos
, u32 phys_cpos
, u32 len
,
762 unsigned int ext_flags
,
763 struct ocfs2_xattr_set_ctxt
*ctxt
)
766 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
767 handle_t
*handle
= ctxt
->handle
;
768 struct ocfs2_extent_tree et
;
770 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
772 ret
= vb
->vb_access(handle
, INODE_CACHE(inode
), vb
->vb_bh
,
773 OCFS2_JOURNAL_ACCESS_WRITE
);
779 ret
= ocfs2_remove_extent(handle
, &et
, cpos
, len
, ctxt
->meta_ac
,
786 le32_add_cpu(&vb
->vb_xv
->xr_clusters
, -len
);
787 ocfs2_journal_dirty(handle
, vb
->vb_bh
);
789 if (ext_flags
& OCFS2_EXT_REFCOUNTED
)
790 ret
= ocfs2_decrease_refcount(inode
, handle
,
791 ocfs2_blocks_to_clusters(inode
->i_sb
,
793 len
, ctxt
->meta_ac
, &ctxt
->dealloc
, 1);
795 ret
= ocfs2_cache_cluster_dealloc(&ctxt
->dealloc
,
804 static int ocfs2_xattr_shrink_size(struct inode
*inode
,
807 struct ocfs2_xattr_value_buf
*vb
,
808 struct ocfs2_xattr_set_ctxt
*ctxt
)
811 unsigned int ext_flags
;
812 u32 trunc_len
, cpos
, phys_cpos
, alloc_size
;
815 if (old_clusters
<= new_clusters
)
819 trunc_len
= old_clusters
- new_clusters
;
821 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &phys_cpos
,
823 &vb
->vb_xv
->xr_list
, &ext_flags
);
829 if (alloc_size
> trunc_len
)
830 alloc_size
= trunc_len
;
832 ret
= __ocfs2_remove_xattr_range(inode
, vb
, cpos
,
833 phys_cpos
, alloc_size
,
840 block
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
841 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode
),
844 trunc_len
-= alloc_size
;
851 static int ocfs2_xattr_value_truncate(struct inode
*inode
,
852 struct ocfs2_xattr_value_buf
*vb
,
854 struct ocfs2_xattr_set_ctxt
*ctxt
)
857 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, len
);
858 u32 old_clusters
= le32_to_cpu(vb
->vb_xv
->xr_clusters
);
860 if (new_clusters
== old_clusters
)
863 if (new_clusters
> old_clusters
)
864 ret
= ocfs2_xattr_extend_allocation(inode
,
865 new_clusters
- old_clusters
,
868 ret
= ocfs2_xattr_shrink_size(inode
,
869 old_clusters
, new_clusters
,
875 static int ocfs2_xattr_list_entry(struct super_block
*sb
,
876 char *buffer
, size_t size
,
877 size_t *result
, int type
,
878 const char *name
, int name_len
)
880 char *p
= buffer
+ *result
;
886 case OCFS2_XATTR_INDEX_USER
:
887 if (OCFS2_SB(sb
)->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
891 case OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
:
892 case OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
:
893 if (!(sb
->s_flags
& SB_POSIXACL
))
897 case OCFS2_XATTR_INDEX_TRUSTED
:
898 if (!capable(CAP_SYS_ADMIN
))
903 prefix
= ocfs2_xattr_prefix(type
);
906 prefix_len
= strlen(prefix
);
907 total_len
= prefix_len
+ name_len
+ 1;
908 *result
+= total_len
;
910 /* we are just looking for how big our buffer needs to be */
917 memcpy(p
, prefix
, prefix_len
);
918 memcpy(p
+ prefix_len
, name
, name_len
);
919 p
[prefix_len
+ name_len
] = '\0';
924 static int ocfs2_xattr_list_entries(struct inode
*inode
,
925 struct ocfs2_xattr_header
*header
,
926 char *buffer
, size_t buffer_size
)
932 for (i
= 0 ; i
< le16_to_cpu(header
->xh_count
); i
++) {
933 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
934 type
= ocfs2_xattr_get_type(entry
);
935 name
= (const char *)header
+
936 le16_to_cpu(entry
->xe_name_offset
);
938 ret
= ocfs2_xattr_list_entry(inode
->i_sb
,
949 int ocfs2_has_inline_xattr_value_outside(struct inode
*inode
,
950 struct ocfs2_dinode
*di
)
952 struct ocfs2_xattr_header
*xh
;
955 xh
= (struct ocfs2_xattr_header
*)
956 ((void *)di
+ inode
->i_sb
->s_blocksize
-
957 le16_to_cpu(di
->i_xattr_inline_size
));
959 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++)
960 if (!ocfs2_xattr_is_local(&xh
->xh_entries
[i
]))
966 static int ocfs2_xattr_ibody_list(struct inode
*inode
,
967 struct ocfs2_dinode
*di
,
971 struct ocfs2_xattr_header
*header
= NULL
;
972 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
975 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
978 header
= (struct ocfs2_xattr_header
*)
979 ((void *)di
+ inode
->i_sb
->s_blocksize
-
980 le16_to_cpu(di
->i_xattr_inline_size
));
982 ret
= ocfs2_xattr_list_entries(inode
, header
, buffer
, buffer_size
);
987 static int ocfs2_xattr_block_list(struct inode
*inode
,
988 struct ocfs2_dinode
*di
,
992 struct buffer_head
*blk_bh
= NULL
;
993 struct ocfs2_xattr_block
*xb
;
996 if (!di
->i_xattr_loc
)
999 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
1006 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1007 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1008 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
1009 ret
= ocfs2_xattr_list_entries(inode
, header
,
1010 buffer
, buffer_size
);
1012 ret
= ocfs2_xattr_tree_list_index_block(inode
, blk_bh
,
1013 buffer
, buffer_size
);
1020 ssize_t
ocfs2_listxattr(struct dentry
*dentry
,
1024 int ret
= 0, i_ret
= 0, b_ret
= 0;
1025 struct buffer_head
*di_bh
= NULL
;
1026 struct ocfs2_dinode
*di
= NULL
;
1027 struct ocfs2_inode_info
*oi
= OCFS2_I(d_inode(dentry
));
1029 if (!ocfs2_supports_xattr(OCFS2_SB(dentry
->d_sb
)))
1032 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1035 ret
= ocfs2_inode_lock(d_inode(dentry
), &di_bh
, 0);
1041 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1043 down_read(&oi
->ip_xattr_sem
);
1044 i_ret
= ocfs2_xattr_ibody_list(d_inode(dentry
), di
, buffer
, size
);
1052 b_ret
= ocfs2_xattr_block_list(d_inode(dentry
), di
,
1057 up_read(&oi
->ip_xattr_sem
);
1058 ocfs2_inode_unlock(d_inode(dentry
), 0);
1062 return i_ret
+ b_ret
;
1065 static int ocfs2_xattr_find_entry(struct inode
*inode
, int name_index
,
1067 struct ocfs2_xattr_search
*xs
)
1069 struct ocfs2_xattr_entry
*entry
;
1071 int i
, name_offset
, cmp
= 1;
1076 name_len
= strlen(name
);
1078 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1079 if ((void *)entry
>= xs
->end
) {
1080 ocfs2_error(inode
->i_sb
, "corrupted xattr entries");
1081 return -EFSCORRUPTED
;
1083 cmp
= name_index
- ocfs2_xattr_get_type(entry
);
1085 cmp
= name_len
- entry
->xe_name_len
;
1087 name_offset
= le16_to_cpu(entry
->xe_name_offset
);
1088 if ((xs
->base
+ name_offset
+ name_len
) > xs
->end
) {
1089 ocfs2_error(inode
->i_sb
,
1090 "corrupted xattr entries");
1091 return -EFSCORRUPTED
;
1093 cmp
= memcmp(name
, (xs
->base
+ name_offset
), name_len
);
1101 return cmp
? -ENODATA
: 0;
1104 static int ocfs2_xattr_get_value_outside(struct inode
*inode
,
1105 struct ocfs2_xattr_value_root
*xv
,
1109 u32 cpos
, p_cluster
, num_clusters
, bpc
, clusters
;
1112 size_t cplen
, blocksize
;
1113 struct buffer_head
*bh
= NULL
;
1114 struct ocfs2_extent_list
*el
;
1117 clusters
= le32_to_cpu(xv
->xr_clusters
);
1118 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
1119 blocksize
= inode
->i_sb
->s_blocksize
;
1122 while (cpos
< clusters
) {
1123 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
1124 &num_clusters
, el
, NULL
);
1130 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
1131 /* Copy ocfs2_xattr_value */
1132 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
1133 ret
= ocfs2_read_block(INODE_CACHE(inode
), blkno
,
1140 cplen
= len
>= blocksize
? blocksize
: len
;
1141 memcpy(buffer
, bh
->b_data
, cplen
);
1150 cpos
+= num_clusters
;
1156 static int ocfs2_xattr_ibody_get(struct inode
*inode
,
1161 struct ocfs2_xattr_search
*xs
)
1163 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1164 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1165 struct ocfs2_xattr_value_root
*xv
;
1169 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
1172 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
1173 xs
->header
= (struct ocfs2_xattr_header
*)
1174 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
1175 xs
->base
= (void *)xs
->header
;
1176 xs
->here
= xs
->header
->xh_entries
;
1178 ret
= ocfs2_xattr_find_entry(inode
, name_index
, name
, xs
);
1181 size
= le64_to_cpu(xs
->here
->xe_value_size
);
1183 if (size
> buffer_size
)
1185 if (ocfs2_xattr_is_local(xs
->here
)) {
1186 memcpy(buffer
, (void *)xs
->base
+
1187 le16_to_cpu(xs
->here
->xe_name_offset
) +
1188 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
), size
);
1190 xv
= (struct ocfs2_xattr_value_root
*)
1191 (xs
->base
+ le16_to_cpu(
1192 xs
->here
->xe_name_offset
) +
1193 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
));
1194 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
1206 static int ocfs2_xattr_block_get(struct inode
*inode
,
1211 struct ocfs2_xattr_search
*xs
)
1213 struct ocfs2_xattr_block
*xb
;
1214 struct ocfs2_xattr_value_root
*xv
;
1216 int ret
= -ENODATA
, name_offset
, name_len
, i
;
1219 xs
->bucket
= ocfs2_xattr_bucket_new(inode
);
1226 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, xs
);
1232 if (xs
->not_found
) {
1237 xb
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
1238 size
= le64_to_cpu(xs
->here
->xe_value_size
);
1241 if (size
> buffer_size
)
1244 name_offset
= le16_to_cpu(xs
->here
->xe_name_offset
);
1245 name_len
= OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
);
1246 i
= xs
->here
- xs
->header
->xh_entries
;
1248 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
1249 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
1250 bucket_xh(xs
->bucket
),
1258 xs
->base
= bucket_block(xs
->bucket
, block_off
);
1260 if (ocfs2_xattr_is_local(xs
->here
)) {
1261 memcpy(buffer
, (void *)xs
->base
+
1262 name_offset
+ name_len
, size
);
1264 xv
= (struct ocfs2_xattr_value_root
*)
1265 (xs
->base
+ name_offset
+ name_len
);
1266 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
1276 ocfs2_xattr_bucket_free(xs
->bucket
);
1278 brelse(xs
->xattr_bh
);
1279 xs
->xattr_bh
= NULL
;
1283 int ocfs2_xattr_get_nolock(struct inode
*inode
,
1284 struct buffer_head
*di_bh
,
1291 struct ocfs2_dinode
*di
= NULL
;
1292 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1293 struct ocfs2_xattr_search xis
= {
1294 .not_found
= -ENODATA
,
1296 struct ocfs2_xattr_search xbs
= {
1297 .not_found
= -ENODATA
,
1300 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
1303 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1306 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
1307 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1309 ret
= ocfs2_xattr_ibody_get(inode
, name_index
, name
, buffer
,
1311 if (ret
== -ENODATA
&& di
->i_xattr_loc
)
1312 ret
= ocfs2_xattr_block_get(inode
, name_index
, name
, buffer
,
1318 /* ocfs2_xattr_get()
1320 * Copy an extended attribute into the buffer provided.
1321 * Buffer is NULL to compute the size of buffer required.
1323 static int ocfs2_xattr_get(struct inode
*inode
,
1330 struct buffer_head
*di_bh
= NULL
;
1331 struct ocfs2_lock_holder oh
;
1333 had_lock
= ocfs2_inode_lock_tracker(inode
, &di_bh
, 0, &oh
);
1335 mlog_errno(had_lock
);
1338 down_read(&OCFS2_I(inode
)->ip_xattr_sem
);
1339 ret
= ocfs2_xattr_get_nolock(inode
, di_bh
, name_index
,
1340 name
, buffer
, buffer_size
);
1341 up_read(&OCFS2_I(inode
)->ip_xattr_sem
);
1343 ocfs2_inode_unlock_tracker(inode
, 0, &oh
, had_lock
);
1350 static int __ocfs2_xattr_set_value_outside(struct inode
*inode
,
1352 struct ocfs2_xattr_value_buf
*vb
,
1356 int ret
= 0, i
, cp_len
;
1357 u16 blocksize
= inode
->i_sb
->s_blocksize
;
1358 u32 p_cluster
, num_clusters
;
1359 u32 cpos
= 0, bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
1360 u32 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, value_len
);
1362 struct buffer_head
*bh
= NULL
;
1363 unsigned int ext_flags
;
1364 struct ocfs2_xattr_value_root
*xv
= vb
->vb_xv
;
1366 BUG_ON(clusters
> le32_to_cpu(xv
->xr_clusters
));
1368 while (cpos
< clusters
) {
1369 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
1370 &num_clusters
, &xv
->xr_list
,
1377 BUG_ON(ext_flags
& OCFS2_EXT_REFCOUNTED
);
1379 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
1381 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
1382 ret
= ocfs2_read_block(INODE_CACHE(inode
), blkno
,
1389 ret
= ocfs2_journal_access(handle
,
1392 OCFS2_JOURNAL_ACCESS_WRITE
);
1398 cp_len
= value_len
> blocksize
? blocksize
: value_len
;
1399 memcpy(bh
->b_data
, value
, cp_len
);
1400 value_len
-= cp_len
;
1402 if (cp_len
< blocksize
)
1403 memset(bh
->b_data
+ cp_len
, 0,
1404 blocksize
- cp_len
);
1406 ocfs2_journal_dirty(handle
, bh
);
1411 * XXX: do we need to empty all the following
1412 * blocks in this cluster?
1417 cpos
+= num_clusters
;
1425 static int ocfs2_xa_check_space_helper(int needed_space
, int free_start
,
1433 free_space
= free_start
-
1434 sizeof(struct ocfs2_xattr_header
) -
1435 (num_entries
* sizeof(struct ocfs2_xattr_entry
)) -
1436 OCFS2_XATTR_HEADER_GAP
;
1439 if (free_space
< needed_space
)
1445 static int ocfs2_xa_journal_access(handle_t
*handle
, struct ocfs2_xa_loc
*loc
,
1448 return loc
->xl_ops
->xlo_journal_access(handle
, loc
, type
);
1451 static void ocfs2_xa_journal_dirty(handle_t
*handle
, struct ocfs2_xa_loc
*loc
)
1453 loc
->xl_ops
->xlo_journal_dirty(handle
, loc
);
1456 /* Give a pointer into the storage for the given offset */
1457 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc
*loc
, int offset
)
1459 BUG_ON(offset
>= loc
->xl_size
);
1460 return loc
->xl_ops
->xlo_offset_pointer(loc
, offset
);
1464 * Wipe the name+value pair and allow the storage to reclaim it. This
1465 * must be followed by either removal of the entry or a call to
1466 * ocfs2_xa_add_namevalue().
1468 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1470 loc
->xl_ops
->xlo_wipe_namevalue(loc
);
1474 * Find lowest offset to a name+value pair. This is the start of our
1475 * downward-growing free space.
1477 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc
*loc
)
1479 return loc
->xl_ops
->xlo_get_free_start(loc
);
1482 /* Can we reuse loc->xl_entry for xi? */
1483 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc
*loc
,
1484 struct ocfs2_xattr_info
*xi
)
1486 return loc
->xl_ops
->xlo_can_reuse(loc
, xi
);
1489 /* How much free space is needed to set the new value */
1490 static int ocfs2_xa_check_space(struct ocfs2_xa_loc
*loc
,
1491 struct ocfs2_xattr_info
*xi
)
1493 return loc
->xl_ops
->xlo_check_space(loc
, xi
);
1496 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1498 loc
->xl_ops
->xlo_add_entry(loc
, name_hash
);
1499 loc
->xl_entry
->xe_name_hash
= cpu_to_le32(name_hash
);
1501 * We can't leave the new entry's xe_name_offset at zero or
1502 * add_namevalue() will go nuts. We set it to the size of our
1503 * storage so that it can never be less than any other entry.
1505 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(loc
->xl_size
);
1508 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc
*loc
,
1509 struct ocfs2_xattr_info
*xi
)
1511 int size
= namevalue_size_xi(xi
);
1515 loc
->xl_ops
->xlo_add_namevalue(loc
, size
);
1516 loc
->xl_entry
->xe_value_size
= cpu_to_le64(xi
->xi_value_len
);
1517 loc
->xl_entry
->xe_name_len
= xi
->xi_name_len
;
1518 ocfs2_xattr_set_type(loc
->xl_entry
, xi
->xi_name_index
);
1519 ocfs2_xattr_set_local(loc
->xl_entry
,
1520 xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
);
1522 nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1523 nameval_buf
= ocfs2_xa_offset_pointer(loc
, nameval_offset
);
1524 memset(nameval_buf
, 0, size
);
1525 memcpy(nameval_buf
, xi
->xi_name
, xi
->xi_name_len
);
1528 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1529 struct ocfs2_xattr_value_buf
*vb
)
1531 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1532 int name_size
= OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
);
1534 /* Value bufs are for value trees */
1535 BUG_ON(ocfs2_xattr_is_local(loc
->xl_entry
));
1536 BUG_ON(namevalue_size_xe(loc
->xl_entry
) !=
1537 (name_size
+ OCFS2_XATTR_ROOT_SIZE
));
1539 loc
->xl_ops
->xlo_fill_value_buf(loc
, vb
);
1541 (struct ocfs2_xattr_value_root
*)ocfs2_xa_offset_pointer(loc
,
1546 static int ocfs2_xa_block_journal_access(handle_t
*handle
,
1547 struct ocfs2_xa_loc
*loc
, int type
)
1549 struct buffer_head
*bh
= loc
->xl_storage
;
1550 ocfs2_journal_access_func access
;
1552 if (loc
->xl_size
== (bh
->b_size
-
1553 offsetof(struct ocfs2_xattr_block
,
1554 xb_attrs
.xb_header
)))
1555 access
= ocfs2_journal_access_xb
;
1557 access
= ocfs2_journal_access_di
;
1558 return access(handle
, INODE_CACHE(loc
->xl_inode
), bh
, type
);
1561 static void ocfs2_xa_block_journal_dirty(handle_t
*handle
,
1562 struct ocfs2_xa_loc
*loc
)
1564 struct buffer_head
*bh
= loc
->xl_storage
;
1566 ocfs2_journal_dirty(handle
, bh
);
1569 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc
*loc
,
1572 return (char *)loc
->xl_header
+ offset
;
1575 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc
*loc
,
1576 struct ocfs2_xattr_info
*xi
)
1579 * Block storage is strict. If the sizes aren't exact, we will
1580 * remove the old one and reinsert the new.
1582 return namevalue_size_xe(loc
->xl_entry
) ==
1583 namevalue_size_xi(xi
);
1586 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc
*loc
)
1588 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1589 int i
, count
= le16_to_cpu(xh
->xh_count
);
1590 int offset
, free_start
= loc
->xl_size
;
1592 for (i
= 0; i
< count
; i
++) {
1593 offset
= le16_to_cpu(xh
->xh_entries
[i
].xe_name_offset
);
1594 if (offset
< free_start
)
1595 free_start
= offset
;
1601 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc
*loc
,
1602 struct ocfs2_xattr_info
*xi
)
1604 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1605 int free_start
= ocfs2_xa_get_free_start(loc
);
1606 int needed_space
= ocfs2_xi_entry_usage(xi
);
1609 * Block storage will reclaim the original entry before inserting
1610 * the new value, so we only need the difference. If the new
1611 * entry is smaller than the old one, we don't need anything.
1613 if (loc
->xl_entry
) {
1614 /* Don't need space if we're reusing! */
1615 if (ocfs2_xa_can_reuse_entry(loc
, xi
))
1618 needed_space
-= ocfs2_xe_entry_usage(loc
->xl_entry
);
1620 if (needed_space
< 0)
1622 return ocfs2_xa_check_space_helper(needed_space
, free_start
, count
);
1626 * Block storage for xattrs keeps the name+value pairs compacted. When
1627 * we remove one, we have to shift any that preceded it towards the end.
1629 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1632 int namevalue_offset
, first_namevalue_offset
, namevalue_size
;
1633 struct ocfs2_xattr_entry
*entry
= loc
->xl_entry
;
1634 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1635 int count
= le16_to_cpu(xh
->xh_count
);
1637 namevalue_offset
= le16_to_cpu(entry
->xe_name_offset
);
1638 namevalue_size
= namevalue_size_xe(entry
);
1639 first_namevalue_offset
= ocfs2_xa_get_free_start(loc
);
1641 /* Shift the name+value pairs */
1642 memmove((char *)xh
+ first_namevalue_offset
+ namevalue_size
,
1643 (char *)xh
+ first_namevalue_offset
,
1644 namevalue_offset
- first_namevalue_offset
);
1645 memset((char *)xh
+ first_namevalue_offset
, 0, namevalue_size
);
1647 /* Now tell xh->xh_entries about it */
1648 for (i
= 0; i
< count
; i
++) {
1649 offset
= le16_to_cpu(xh
->xh_entries
[i
].xe_name_offset
);
1650 if (offset
<= namevalue_offset
)
1651 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
,
1656 * Note that we don't update xh_free_start or xh_name_value_len
1657 * because they're not used in block-stored xattrs.
1661 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1663 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1664 loc
->xl_entry
= &(loc
->xl_header
->xh_entries
[count
]);
1665 le16_add_cpu(&loc
->xl_header
->xh_count
, 1);
1666 memset(loc
->xl_entry
, 0, sizeof(struct ocfs2_xattr_entry
));
1669 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc
*loc
, int size
)
1671 int free_start
= ocfs2_xa_get_free_start(loc
);
1673 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(free_start
- size
);
1676 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1677 struct ocfs2_xattr_value_buf
*vb
)
1679 struct buffer_head
*bh
= loc
->xl_storage
;
1681 if (loc
->xl_size
== (bh
->b_size
-
1682 offsetof(struct ocfs2_xattr_block
,
1683 xb_attrs
.xb_header
)))
1684 vb
->vb_access
= ocfs2_journal_access_xb
;
1686 vb
->vb_access
= ocfs2_journal_access_di
;
1691 * Operations for xattrs stored in blocks. This includes inline inode
1692 * storage and unindexed ocfs2_xattr_blocks.
1694 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops
= {
1695 .xlo_journal_access
= ocfs2_xa_block_journal_access
,
1696 .xlo_journal_dirty
= ocfs2_xa_block_journal_dirty
,
1697 .xlo_offset_pointer
= ocfs2_xa_block_offset_pointer
,
1698 .xlo_check_space
= ocfs2_xa_block_check_space
,
1699 .xlo_can_reuse
= ocfs2_xa_block_can_reuse
,
1700 .xlo_get_free_start
= ocfs2_xa_block_get_free_start
,
1701 .xlo_wipe_namevalue
= ocfs2_xa_block_wipe_namevalue
,
1702 .xlo_add_entry
= ocfs2_xa_block_add_entry
,
1703 .xlo_add_namevalue
= ocfs2_xa_block_add_namevalue
,
1704 .xlo_fill_value_buf
= ocfs2_xa_block_fill_value_buf
,
1707 static int ocfs2_xa_bucket_journal_access(handle_t
*handle
,
1708 struct ocfs2_xa_loc
*loc
, int type
)
1710 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1712 return ocfs2_xattr_bucket_journal_access(handle
, bucket
, type
);
1715 static void ocfs2_xa_bucket_journal_dirty(handle_t
*handle
,
1716 struct ocfs2_xa_loc
*loc
)
1718 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1720 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
1723 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc
*loc
,
1726 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1727 int block
, block_offset
;
1729 /* The header is at the front of the bucket */
1730 block
= offset
>> loc
->xl_inode
->i_sb
->s_blocksize_bits
;
1731 block_offset
= offset
% loc
->xl_inode
->i_sb
->s_blocksize
;
1733 return bucket_block(bucket
, block
) + block_offset
;
1736 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc
*loc
,
1737 struct ocfs2_xattr_info
*xi
)
1739 return namevalue_size_xe(loc
->xl_entry
) >=
1740 namevalue_size_xi(xi
);
1743 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc
*loc
)
1745 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1746 return le16_to_cpu(bucket_xh(bucket
)->xh_free_start
);
1749 static int ocfs2_bucket_align_free_start(struct super_block
*sb
,
1750 int free_start
, int size
)
1753 * We need to make sure that the name+value pair fits within
1756 if (((free_start
- size
) >> sb
->s_blocksize_bits
) !=
1757 ((free_start
- 1) >> sb
->s_blocksize_bits
))
1758 free_start
-= free_start
% sb
->s_blocksize
;
1763 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc
*loc
,
1764 struct ocfs2_xattr_info
*xi
)
1767 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1768 int free_start
= ocfs2_xa_get_free_start(loc
);
1769 int needed_space
= ocfs2_xi_entry_usage(xi
);
1770 int size
= namevalue_size_xi(xi
);
1771 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1774 * Bucket storage does not reclaim name+value pairs it cannot
1775 * reuse. They live as holes until the bucket fills, and then
1776 * the bucket is defragmented. However, the bucket can reclaim
1777 * the ocfs2_xattr_entry.
1779 if (loc
->xl_entry
) {
1780 /* Don't need space if we're reusing! */
1781 if (ocfs2_xa_can_reuse_entry(loc
, xi
))
1784 needed_space
-= sizeof(struct ocfs2_xattr_entry
);
1786 BUG_ON(needed_space
< 0);
1788 if (free_start
< size
) {
1793 * First we check if it would fit in the first place.
1794 * Below, we align the free start to a block. This may
1795 * slide us below the minimum gap. By checking unaligned
1796 * first, we avoid that error.
1798 rc
= ocfs2_xa_check_space_helper(needed_space
, free_start
,
1802 free_start
= ocfs2_bucket_align_free_start(sb
, free_start
,
1805 return ocfs2_xa_check_space_helper(needed_space
, free_start
, count
);
1808 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1810 le16_add_cpu(&loc
->xl_header
->xh_name_value_len
,
1811 -namevalue_size_xe(loc
->xl_entry
));
1814 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1816 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1817 int count
= le16_to_cpu(xh
->xh_count
);
1818 int low
= 0, high
= count
- 1, tmp
;
1819 struct ocfs2_xattr_entry
*tmp_xe
;
1822 * We keep buckets sorted by name_hash, so we need to find
1825 while (low
<= high
&& count
) {
1826 tmp
= (low
+ high
) / 2;
1827 tmp_xe
= &xh
->xh_entries
[tmp
];
1829 if (name_hash
> le32_to_cpu(tmp_xe
->xe_name_hash
))
1831 else if (name_hash
< le32_to_cpu(tmp_xe
->xe_name_hash
))
1840 memmove(&xh
->xh_entries
[low
+ 1],
1841 &xh
->xh_entries
[low
],
1842 ((count
- low
) * sizeof(struct ocfs2_xattr_entry
)));
1844 le16_add_cpu(&xh
->xh_count
, 1);
1845 loc
->xl_entry
= &xh
->xh_entries
[low
];
1846 memset(loc
->xl_entry
, 0, sizeof(struct ocfs2_xattr_entry
));
1849 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc
*loc
, int size
)
1851 int free_start
= ocfs2_xa_get_free_start(loc
);
1852 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1853 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1856 free_start
= ocfs2_bucket_align_free_start(sb
, free_start
, size
);
1857 nameval_offset
= free_start
- size
;
1858 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(nameval_offset
);
1859 xh
->xh_free_start
= cpu_to_le16(nameval_offset
);
1860 le16_add_cpu(&xh
->xh_name_value_len
, size
);
1864 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1865 struct ocfs2_xattr_value_buf
*vb
)
1867 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1868 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1869 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1870 int size
= namevalue_size_xe(loc
->xl_entry
);
1871 int block_offset
= nameval_offset
>> sb
->s_blocksize_bits
;
1873 /* Values are not allowed to straddle block boundaries */
1874 BUG_ON(block_offset
!=
1875 ((nameval_offset
+ size
- 1) >> sb
->s_blocksize_bits
));
1876 /* We expect the bucket to be filled in */
1877 BUG_ON(!bucket
->bu_bhs
[block_offset
]);
1879 vb
->vb_access
= ocfs2_journal_access
;
1880 vb
->vb_bh
= bucket
->bu_bhs
[block_offset
];
1883 /* Operations for xattrs stored in buckets. */
1884 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops
= {
1885 .xlo_journal_access
= ocfs2_xa_bucket_journal_access
,
1886 .xlo_journal_dirty
= ocfs2_xa_bucket_journal_dirty
,
1887 .xlo_offset_pointer
= ocfs2_xa_bucket_offset_pointer
,
1888 .xlo_check_space
= ocfs2_xa_bucket_check_space
,
1889 .xlo_can_reuse
= ocfs2_xa_bucket_can_reuse
,
1890 .xlo_get_free_start
= ocfs2_xa_bucket_get_free_start
,
1891 .xlo_wipe_namevalue
= ocfs2_xa_bucket_wipe_namevalue
,
1892 .xlo_add_entry
= ocfs2_xa_bucket_add_entry
,
1893 .xlo_add_namevalue
= ocfs2_xa_bucket_add_namevalue
,
1894 .xlo_fill_value_buf
= ocfs2_xa_bucket_fill_value_buf
,
1897 static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc
*loc
)
1899 struct ocfs2_xattr_value_buf vb
;
1901 if (ocfs2_xattr_is_local(loc
->xl_entry
))
1904 ocfs2_xa_fill_value_buf(loc
, &vb
);
1905 return le32_to_cpu(vb
.vb_xv
->xr_clusters
);
1908 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc
*loc
, u64 bytes
,
1909 struct ocfs2_xattr_set_ctxt
*ctxt
)
1911 int trunc_rc
, access_rc
;
1912 struct ocfs2_xattr_value_buf vb
;
1914 ocfs2_xa_fill_value_buf(loc
, &vb
);
1915 trunc_rc
= ocfs2_xattr_value_truncate(loc
->xl_inode
, &vb
, bytes
,
1919 * The caller of ocfs2_xa_value_truncate() has already called
1920 * ocfs2_xa_journal_access on the loc. However, The truncate code
1921 * calls ocfs2_extend_trans(). This may commit the previous
1922 * transaction and open a new one. If this is a bucket, truncate
1923 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1924 * the caller is expecting to dirty the entire bucket. So we must
1925 * reset the journal work. We do this even if truncate has failed,
1926 * as it could have failed after committing the extend.
1928 access_rc
= ocfs2_xa_journal_access(ctxt
->handle
, loc
,
1929 OCFS2_JOURNAL_ACCESS_WRITE
);
1931 /* Errors in truncate take precedence */
1932 return trunc_rc
? trunc_rc
: access_rc
;
1935 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc
*loc
)
1938 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1939 struct ocfs2_xattr_entry
*entry
= loc
->xl_entry
;
1941 ocfs2_xa_wipe_namevalue(loc
);
1942 loc
->xl_entry
= NULL
;
1944 le16_add_cpu(&xh
->xh_count
, -1);
1945 count
= le16_to_cpu(xh
->xh_count
);
1948 * Only zero out the entry if there are more remaining. This is
1949 * important for an empty bucket, as it keeps track of the
1950 * bucket's hash value. It doesn't hurt empty block storage.
1953 index
= ((char *)entry
- (char *)&xh
->xh_entries
) /
1954 sizeof(struct ocfs2_xattr_entry
);
1955 memmove(&xh
->xh_entries
[index
], &xh
->xh_entries
[index
+ 1],
1956 (count
- index
) * sizeof(struct ocfs2_xattr_entry
));
1957 memset(&xh
->xh_entries
[count
], 0,
1958 sizeof(struct ocfs2_xattr_entry
));
1963 * If we have a problem adjusting the size of an external value during
1964 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1965 * in an intermediate state. For example, the value may be partially
1968 * If the value tree hasn't changed, the extend/truncate went nowhere.
1969 * We have nothing to do. The caller can treat it as a straight error.
1971 * If the value tree got partially truncated, we now have a corrupted
1972 * extended attribute. We're going to wipe its entry and leak the
1973 * clusters. Better to leak some storage than leave a corrupt entry.
1975 * If the value tree grew, it obviously didn't grow enough for the
1976 * new entry. We're not going to try and reclaim those clusters either.
1977 * If there was already an external value there (orig_clusters != 0),
1978 * the new clusters are attached safely and we can just leave the old
1979 * value in place. If there was no external value there, we remove
1982 * This way, the xattr block we store in the journal will be consistent.
1983 * If the size change broke because of the journal, no changes will hit
1986 static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc
*loc
,
1988 unsigned int orig_clusters
)
1990 unsigned int new_clusters
= ocfs2_xa_value_clusters(loc
);
1991 char *nameval_buf
= ocfs2_xa_offset_pointer(loc
,
1992 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
1994 if (new_clusters
< orig_clusters
) {
1996 "Partial truncate while %s xattr %.*s. Leaking "
1997 "%u clusters and removing the entry\n",
1998 what
, loc
->xl_entry
->xe_name_len
, nameval_buf
,
1999 orig_clusters
- new_clusters
);
2000 ocfs2_xa_remove_entry(loc
);
2001 } else if (!orig_clusters
) {
2003 "Unable to allocate an external value for xattr "
2004 "%.*s safely. Leaking %u clusters and removing the "
2006 loc
->xl_entry
->xe_name_len
, nameval_buf
,
2007 new_clusters
- orig_clusters
);
2008 ocfs2_xa_remove_entry(loc
);
2009 } else if (new_clusters
> orig_clusters
)
2011 "Unable to grow xattr %.*s safely. %u new clusters "
2012 "have been added, but the value will not be "
2014 loc
->xl_entry
->xe_name_len
, nameval_buf
,
2015 new_clusters
- orig_clusters
);
2018 static int ocfs2_xa_remove(struct ocfs2_xa_loc
*loc
,
2019 struct ocfs2_xattr_set_ctxt
*ctxt
)
2022 unsigned int orig_clusters
;
2024 if (!ocfs2_xattr_is_local(loc
->xl_entry
)) {
2025 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2026 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2030 * Since this is remove, we can return 0 if
2031 * ocfs2_xa_cleanup_value_truncate() is going to
2032 * wipe the entry anyway. So we check the
2033 * cluster count as well.
2035 if (orig_clusters
!= ocfs2_xa_value_clusters(loc
))
2037 ocfs2_xa_cleanup_value_truncate(loc
, "removing",
2043 ocfs2_xa_remove_entry(loc
);
2049 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc
*loc
)
2051 int name_size
= OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
);
2054 nameval_buf
= ocfs2_xa_offset_pointer(loc
,
2055 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
2056 memcpy(nameval_buf
+ name_size
, &def_xv
, OCFS2_XATTR_ROOT_SIZE
);
2060 * Take an existing entry and make it ready for the new value. This
2061 * won't allocate space, but it may free space. It should be ready for
2062 * ocfs2_xa_prepare_entry() to finish the work.
2064 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc
*loc
,
2065 struct ocfs2_xattr_info
*xi
,
2066 struct ocfs2_xattr_set_ctxt
*ctxt
)
2069 int name_size
= OCFS2_XATTR_SIZE(xi
->xi_name_len
);
2070 unsigned int orig_clusters
;
2072 int xe_local
= ocfs2_xattr_is_local(loc
->xl_entry
);
2073 int xi_local
= xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
;
2075 BUG_ON(OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
) !=
2078 nameval_buf
= ocfs2_xa_offset_pointer(loc
,
2079 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
2081 memset(nameval_buf
+ name_size
, 0,
2082 namevalue_size_xe(loc
->xl_entry
) - name_size
);
2084 ocfs2_xa_install_value_root(loc
);
2086 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2088 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2092 memset(nameval_buf
+ name_size
, 0,
2093 namevalue_size_xe(loc
->xl_entry
) -
2095 } else if (le64_to_cpu(loc
->xl_entry
->xe_value_size
) >
2097 rc
= ocfs2_xa_value_truncate(loc
, xi
->xi_value_len
,
2104 ocfs2_xa_cleanup_value_truncate(loc
, "reusing",
2110 loc
->xl_entry
->xe_value_size
= cpu_to_le64(xi
->xi_value_len
);
2111 ocfs2_xattr_set_local(loc
->xl_entry
, xi_local
);
2118 * Prepares loc->xl_entry to receive the new xattr. This includes
2119 * properly setting up the name+value pair region. If loc->xl_entry
2120 * already exists, it will take care of modifying it appropriately.
2122 * Note that this modifies the data. You did journal_access already,
2125 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc
*loc
,
2126 struct ocfs2_xattr_info
*xi
,
2128 struct ocfs2_xattr_set_ctxt
*ctxt
)
2131 unsigned int orig_clusters
;
2132 __le64 orig_value_size
= 0;
2134 rc
= ocfs2_xa_check_space(loc
, xi
);
2138 if (loc
->xl_entry
) {
2139 if (ocfs2_xa_can_reuse_entry(loc
, xi
)) {
2140 orig_value_size
= loc
->xl_entry
->xe_value_size
;
2141 rc
= ocfs2_xa_reuse_entry(loc
, xi
, ctxt
);
2147 if (!ocfs2_xattr_is_local(loc
->xl_entry
)) {
2148 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2149 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2152 ocfs2_xa_cleanup_value_truncate(loc
,
2158 ocfs2_xa_wipe_namevalue(loc
);
2160 ocfs2_xa_add_entry(loc
, name_hash
);
2163 * If we get here, we have a blank entry. Fill it. We grow our
2164 * name+value pair back from the end.
2166 ocfs2_xa_add_namevalue(loc
, xi
);
2167 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
)
2168 ocfs2_xa_install_value_root(loc
);
2171 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2172 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2173 rc
= ocfs2_xa_value_truncate(loc
, xi
->xi_value_len
, ctxt
);
2175 ctxt
->set_abort
= 1;
2176 ocfs2_xa_cleanup_value_truncate(loc
, "growing",
2179 * If we were growing an existing value,
2180 * ocfs2_xa_cleanup_value_truncate() won't remove
2181 * the entry. We need to restore the original value
2184 if (loc
->xl_entry
) {
2185 BUG_ON(!orig_value_size
);
2186 loc
->xl_entry
->xe_value_size
= orig_value_size
;
2197 * Store the value portion of the name+value pair. This will skip
2198 * values that are stored externally. Their tree roots were set up
2199 * by ocfs2_xa_prepare_entry().
2201 static int ocfs2_xa_store_value(struct ocfs2_xa_loc
*loc
,
2202 struct ocfs2_xattr_info
*xi
,
2203 struct ocfs2_xattr_set_ctxt
*ctxt
)
2206 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
2207 int name_size
= OCFS2_XATTR_SIZE(xi
->xi_name_len
);
2209 struct ocfs2_xattr_value_buf vb
;
2211 nameval_buf
= ocfs2_xa_offset_pointer(loc
, nameval_offset
);
2212 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2213 ocfs2_xa_fill_value_buf(loc
, &vb
);
2214 rc
= __ocfs2_xattr_set_value_outside(loc
->xl_inode
,
2219 memcpy(nameval_buf
+ name_size
, xi
->xi_value
, xi
->xi_value_len
);
2224 static int ocfs2_xa_set(struct ocfs2_xa_loc
*loc
,
2225 struct ocfs2_xattr_info
*xi
,
2226 struct ocfs2_xattr_set_ctxt
*ctxt
)
2229 u32 name_hash
= ocfs2_xattr_name_hash(loc
->xl_inode
, xi
->xi_name
,
2232 ret
= ocfs2_xa_journal_access(ctxt
->handle
, loc
,
2233 OCFS2_JOURNAL_ACCESS_WRITE
);
2240 * From here on out, everything is going to modify the buffer a
2241 * little. Errors are going to leave the xattr header in a
2242 * sane state. Thus, even with errors we dirty the sucker.
2245 /* Don't worry, we are never called with !xi_value and !xl_entry */
2246 if (!xi
->xi_value
) {
2247 ret
= ocfs2_xa_remove(loc
, ctxt
);
2251 ret
= ocfs2_xa_prepare_entry(loc
, xi
, name_hash
, ctxt
);
2258 ret
= ocfs2_xa_store_value(loc
, xi
, ctxt
);
2263 ocfs2_xa_journal_dirty(ctxt
->handle
, loc
);
2269 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc
*loc
,
2270 struct inode
*inode
,
2271 struct buffer_head
*bh
,
2272 struct ocfs2_xattr_entry
*entry
)
2274 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)bh
->b_data
;
2276 BUG_ON(!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
));
2278 loc
->xl_inode
= inode
;
2279 loc
->xl_ops
= &ocfs2_xa_block_loc_ops
;
2280 loc
->xl_storage
= bh
;
2281 loc
->xl_entry
= entry
;
2282 loc
->xl_size
= le16_to_cpu(di
->i_xattr_inline_size
);
2284 (struct ocfs2_xattr_header
*)(bh
->b_data
+ bh
->b_size
-
2288 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc
*loc
,
2289 struct inode
*inode
,
2290 struct buffer_head
*bh
,
2291 struct ocfs2_xattr_entry
*entry
)
2293 struct ocfs2_xattr_block
*xb
=
2294 (struct ocfs2_xattr_block
*)bh
->b_data
;
2296 BUG_ON(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
);
2298 loc
->xl_inode
= inode
;
2299 loc
->xl_ops
= &ocfs2_xa_block_loc_ops
;
2300 loc
->xl_storage
= bh
;
2301 loc
->xl_header
= &(xb
->xb_attrs
.xb_header
);
2302 loc
->xl_entry
= entry
;
2303 loc
->xl_size
= bh
->b_size
- offsetof(struct ocfs2_xattr_block
,
2304 xb_attrs
.xb_header
);
2307 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc
*loc
,
2308 struct ocfs2_xattr_bucket
*bucket
,
2309 struct ocfs2_xattr_entry
*entry
)
2311 loc
->xl_inode
= bucket
->bu_inode
;
2312 loc
->xl_ops
= &ocfs2_xa_bucket_loc_ops
;
2313 loc
->xl_storage
= bucket
;
2314 loc
->xl_header
= bucket_xh(bucket
);
2315 loc
->xl_entry
= entry
;
2316 loc
->xl_size
= OCFS2_XATTR_BUCKET_SIZE
;
2320 * In xattr remove, if it is stored outside and refcounted, we may have
2321 * the chance to split the refcount tree. So need the allocators.
2323 static int ocfs2_lock_xattr_remove_allocators(struct inode
*inode
,
2324 struct ocfs2_xattr_value_root
*xv
,
2325 struct ocfs2_caching_info
*ref_ci
,
2326 struct buffer_head
*ref_root_bh
,
2327 struct ocfs2_alloc_context
**meta_ac
,
2330 int ret
, meta_add
= 0;
2331 u32 p_cluster
, num_clusters
;
2332 unsigned int ext_flags
;
2335 ret
= ocfs2_xattr_get_clusters(inode
, 0, &p_cluster
,
2344 if (!(ext_flags
& OCFS2_EXT_REFCOUNTED
))
2347 ret
= ocfs2_refcounted_xattr_delete_need(inode
, ref_ci
,
2349 &meta_add
, ref_credits
);
2355 ret
= ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode
->i_sb
),
2364 static int ocfs2_remove_value_outside(struct inode
*inode
,
2365 struct ocfs2_xattr_value_buf
*vb
,
2366 struct ocfs2_xattr_header
*header
,
2367 struct ocfs2_caching_info
*ref_ci
,
2368 struct buffer_head
*ref_root_bh
)
2370 int ret
= 0, i
, ref_credits
;
2371 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2372 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, };
2375 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
2377 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
2378 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
2380 if (ocfs2_xattr_is_local(entry
))
2383 val
= (void *)header
+
2384 le16_to_cpu(entry
->xe_name_offset
);
2385 vb
->vb_xv
= (struct ocfs2_xattr_value_root
*)
2386 (val
+ OCFS2_XATTR_SIZE(entry
->xe_name_len
));
2388 ret
= ocfs2_lock_xattr_remove_allocators(inode
, vb
->vb_xv
,
2389 ref_ci
, ref_root_bh
,
2393 ctxt
.handle
= ocfs2_start_trans(osb
, ref_credits
+
2394 ocfs2_remove_extent_credits(osb
->sb
));
2395 if (IS_ERR(ctxt
.handle
)) {
2396 ret
= PTR_ERR(ctxt
.handle
);
2401 ret
= ocfs2_xattr_value_truncate(inode
, vb
, 0, &ctxt
);
2403 ocfs2_commit_trans(osb
, ctxt
.handle
);
2405 ocfs2_free_alloc_context(ctxt
.meta_ac
);
2406 ctxt
.meta_ac
= NULL
;
2417 ocfs2_free_alloc_context(ctxt
.meta_ac
);
2418 ocfs2_schedule_truncate_log_flush(osb
, 1);
2419 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
2423 static int ocfs2_xattr_ibody_remove(struct inode
*inode
,
2424 struct buffer_head
*di_bh
,
2425 struct ocfs2_caching_info
*ref_ci
,
2426 struct buffer_head
*ref_root_bh
)
2429 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2430 struct ocfs2_xattr_header
*header
;
2432 struct ocfs2_xattr_value_buf vb
= {
2434 .vb_access
= ocfs2_journal_access_di
,
2437 header
= (struct ocfs2_xattr_header
*)
2438 ((void *)di
+ inode
->i_sb
->s_blocksize
-
2439 le16_to_cpu(di
->i_xattr_inline_size
));
2441 ret
= ocfs2_remove_value_outside(inode
, &vb
, header
,
2442 ref_ci
, ref_root_bh
);
2447 struct ocfs2_rm_xattr_bucket_para
{
2448 struct ocfs2_caching_info
*ref_ci
;
2449 struct buffer_head
*ref_root_bh
;
2452 static int ocfs2_xattr_block_remove(struct inode
*inode
,
2453 struct buffer_head
*blk_bh
,
2454 struct ocfs2_caching_info
*ref_ci
,
2455 struct buffer_head
*ref_root_bh
)
2457 struct ocfs2_xattr_block
*xb
;
2459 struct ocfs2_xattr_value_buf vb
= {
2461 .vb_access
= ocfs2_journal_access_xb
,
2463 struct ocfs2_rm_xattr_bucket_para args
= {
2465 .ref_root_bh
= ref_root_bh
,
2468 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2469 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2470 struct ocfs2_xattr_header
*header
= &(xb
->xb_attrs
.xb_header
);
2471 ret
= ocfs2_remove_value_outside(inode
, &vb
, header
,
2472 ref_ci
, ref_root_bh
);
2474 ret
= ocfs2_iterate_xattr_index_block(inode
,
2476 ocfs2_rm_xattr_cluster
,
2482 static int ocfs2_xattr_free_block(struct inode
*inode
,
2484 struct ocfs2_caching_info
*ref_ci
,
2485 struct buffer_head
*ref_root_bh
)
2487 struct inode
*xb_alloc_inode
;
2488 struct buffer_head
*xb_alloc_bh
= NULL
;
2489 struct buffer_head
*blk_bh
= NULL
;
2490 struct ocfs2_xattr_block
*xb
;
2491 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2497 ret
= ocfs2_read_xattr_block(inode
, block
, &blk_bh
);
2503 ret
= ocfs2_xattr_block_remove(inode
, blk_bh
, ref_ci
, ref_root_bh
);
2509 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2510 blk
= le64_to_cpu(xb
->xb_blkno
);
2511 bit
= le16_to_cpu(xb
->xb_suballoc_bit
);
2512 if (xb
->xb_suballoc_loc
)
2513 bg_blkno
= le64_to_cpu(xb
->xb_suballoc_loc
);
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 inode_lock(xb_alloc_inode
);
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 inode_unlock(xb_alloc_inode
);
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_is_refcount_inode(inode
)) {
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
);
2629 ocfs2_update_inode_fsync_trans(handle
, inode
, 0);
2631 ocfs2_journal_dirty(handle
, di_bh
);
2633 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
2636 ocfs2_unlock_refcount_tree(OCFS2_SB(inode
->i_sb
), ref_tree
, 1);
2637 brelse(ref_root_bh
);
2641 static int ocfs2_xattr_has_space_inline(struct inode
*inode
,
2642 struct ocfs2_dinode
*di
)
2644 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2645 unsigned int xattrsize
= OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
;
2648 if (xattrsize
< OCFS2_MIN_XATTR_INLINE_SIZE
)
2651 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
2652 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
2653 free
= le16_to_cpu(idata
->id_count
) - le64_to_cpu(di
->i_size
);
2654 } else if (ocfs2_inode_is_fast_symlink(inode
)) {
2655 free
= ocfs2_fast_symlink_chars(inode
->i_sb
) -
2656 le64_to_cpu(di
->i_size
);
2658 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
2659 free
= (le16_to_cpu(el
->l_count
) -
2660 le16_to_cpu(el
->l_next_free_rec
)) *
2661 sizeof(struct ocfs2_extent_rec
);
2663 if (free
>= xattrsize
)
2670 * ocfs2_xattr_ibody_find()
2672 * Find extended attribute in inode block and
2673 * fill search info into struct ocfs2_xattr_search.
2675 static int ocfs2_xattr_ibody_find(struct inode
*inode
,
2678 struct ocfs2_xattr_search
*xs
)
2680 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2681 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2685 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
2688 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
2689 down_read(&oi
->ip_alloc_sem
);
2690 has_space
= ocfs2_xattr_has_space_inline(inode
, di
);
2691 up_read(&oi
->ip_alloc_sem
);
2696 xs
->xattr_bh
= xs
->inode_bh
;
2697 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
2698 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)
2699 xs
->header
= (struct ocfs2_xattr_header
*)
2700 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
2702 xs
->header
= (struct ocfs2_xattr_header
*)
2703 (xs
->end
- OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
);
2704 xs
->base
= (void *)xs
->header
;
2705 xs
->here
= xs
->header
->xh_entries
;
2707 /* Find the named attribute. */
2708 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
2709 ret
= ocfs2_xattr_find_entry(inode
, name_index
, name
, xs
);
2710 if (ret
&& ret
!= -ENODATA
)
2712 xs
->not_found
= ret
;
2718 static int ocfs2_xattr_ibody_init(struct inode
*inode
,
2719 struct buffer_head
*di_bh
,
2720 struct ocfs2_xattr_set_ctxt
*ctxt
)
2723 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2724 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2725 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2726 unsigned int xattrsize
= osb
->s_xattr_inline_size
;
2728 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
2733 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
), di_bh
,
2734 OCFS2_JOURNAL_ACCESS_WRITE
);
2741 * Adjust extent record count or inline data size
2742 * to reserve space for extended attribute.
2744 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
2745 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
2746 le16_add_cpu(&idata
->id_count
, -xattrsize
);
2747 } else if (!(ocfs2_inode_is_fast_symlink(inode
))) {
2748 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
2749 le16_add_cpu(&el
->l_count
, -(xattrsize
/
2750 sizeof(struct ocfs2_extent_rec
)));
2752 di
->i_xattr_inline_size
= cpu_to_le16(xattrsize
);
2754 spin_lock(&oi
->ip_lock
);
2755 oi
->ip_dyn_features
|= OCFS2_INLINE_XATTR_FL
|OCFS2_HAS_XATTR_FL
;
2756 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
2757 spin_unlock(&oi
->ip_lock
);
2759 ocfs2_journal_dirty(ctxt
->handle
, di_bh
);
2766 * ocfs2_xattr_ibody_set()
2768 * Set, replace or remove an extended attribute into inode block.
2771 static int ocfs2_xattr_ibody_set(struct inode
*inode
,
2772 struct ocfs2_xattr_info
*xi
,
2773 struct ocfs2_xattr_search
*xs
,
2774 struct ocfs2_xattr_set_ctxt
*ctxt
)
2777 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2778 struct ocfs2_xa_loc loc
;
2780 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
2783 down_write(&oi
->ip_alloc_sem
);
2784 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
2785 ret
= ocfs2_xattr_ibody_init(inode
, xs
->inode_bh
, ctxt
);
2793 ocfs2_init_dinode_xa_loc(&loc
, inode
, xs
->inode_bh
,
2794 xs
->not_found
? NULL
: xs
->here
);
2795 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
2801 xs
->here
= loc
.xl_entry
;
2804 up_write(&oi
->ip_alloc_sem
);
2810 * ocfs2_xattr_block_find()
2812 * Find extended attribute in external block and
2813 * fill search info into struct ocfs2_xattr_search.
2815 static int ocfs2_xattr_block_find(struct inode
*inode
,
2818 struct ocfs2_xattr_search
*xs
)
2820 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2821 struct buffer_head
*blk_bh
= NULL
;
2822 struct ocfs2_xattr_block
*xb
;
2825 if (!di
->i_xattr_loc
)
2828 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
2835 xs
->xattr_bh
= blk_bh
;
2836 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2838 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2839 xs
->header
= &xb
->xb_attrs
.xb_header
;
2840 xs
->base
= (void *)xs
->header
;
2841 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
2842 xs
->here
= xs
->header
->xh_entries
;
2844 ret
= ocfs2_xattr_find_entry(inode
, name_index
, name
, xs
);
2846 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
2850 if (ret
&& ret
!= -ENODATA
) {
2851 xs
->xattr_bh
= NULL
;
2854 xs
->not_found
= ret
;
2862 static int ocfs2_create_xattr_block(struct inode
*inode
,
2863 struct buffer_head
*inode_bh
,
2864 struct ocfs2_xattr_set_ctxt
*ctxt
,
2866 struct buffer_head
**ret_bh
)
2869 u16 suballoc_bit_start
;
2871 u64 suballoc_loc
, first_blkno
;
2872 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)inode_bh
->b_data
;
2873 struct buffer_head
*new_bh
= NULL
;
2874 struct ocfs2_xattr_block
*xblk
;
2876 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
),
2877 inode_bh
, OCFS2_JOURNAL_ACCESS_CREATE
);
2883 ret
= ocfs2_claim_metadata(ctxt
->handle
, ctxt
->meta_ac
, 1,
2884 &suballoc_loc
, &suballoc_bit_start
,
2885 &num_got
, &first_blkno
);
2891 new_bh
= sb_getblk(inode
->i_sb
, first_blkno
);
2898 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode
), new_bh
);
2900 ret
= ocfs2_journal_access_xb(ctxt
->handle
, INODE_CACHE(inode
),
2902 OCFS2_JOURNAL_ACCESS_CREATE
);
2908 /* Initialize ocfs2_xattr_block */
2909 xblk
= (struct ocfs2_xattr_block
*)new_bh
->b_data
;
2910 memset(xblk
, 0, inode
->i_sb
->s_blocksize
);
2911 strcpy((void *)xblk
, OCFS2_XATTR_BLOCK_SIGNATURE
);
2912 xblk
->xb_suballoc_slot
= cpu_to_le16(ctxt
->meta_ac
->ac_alloc_slot
);
2913 xblk
->xb_suballoc_loc
= cpu_to_le64(suballoc_loc
);
2914 xblk
->xb_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
2915 xblk
->xb_fs_generation
=
2916 cpu_to_le32(OCFS2_SB(inode
->i_sb
)->fs_generation
);
2917 xblk
->xb_blkno
= cpu_to_le64(first_blkno
);
2919 struct ocfs2_xattr_tree_root
*xr
= &xblk
->xb_attrs
.xb_root
;
2920 xr
->xt_clusters
= cpu_to_le32(1);
2921 xr
->xt_last_eb_blk
= 0;
2922 xr
->xt_list
.l_tree_depth
= 0;
2923 xr
->xt_list
.l_count
= cpu_to_le16(
2924 ocfs2_xattr_recs_per_xb(inode
->i_sb
));
2925 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
2926 xblk
->xb_flags
= cpu_to_le16(OCFS2_XATTR_INDEXED
);
2928 ocfs2_journal_dirty(ctxt
->handle
, new_bh
);
2930 /* Add it to the inode */
2931 di
->i_xattr_loc
= cpu_to_le64(first_blkno
);
2933 spin_lock(&OCFS2_I(inode
)->ip_lock
);
2934 OCFS2_I(inode
)->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
;
2935 di
->i_dyn_features
= cpu_to_le16(OCFS2_I(inode
)->ip_dyn_features
);
2936 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
2938 ocfs2_journal_dirty(ctxt
->handle
, inode_bh
);
2949 * ocfs2_xattr_block_set()
2951 * Set, replace or remove an extended attribute into external block.
2954 static int ocfs2_xattr_block_set(struct inode
*inode
,
2955 struct ocfs2_xattr_info
*xi
,
2956 struct ocfs2_xattr_search
*xs
,
2957 struct ocfs2_xattr_set_ctxt
*ctxt
)
2959 struct buffer_head
*new_bh
= NULL
;
2960 struct ocfs2_xattr_block
*xblk
= NULL
;
2962 struct ocfs2_xa_loc loc
;
2964 if (!xs
->xattr_bh
) {
2965 ret
= ocfs2_create_xattr_block(inode
, xs
->inode_bh
, ctxt
,
2972 xs
->xattr_bh
= new_bh
;
2973 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
2974 xs
->header
= &xblk
->xb_attrs
.xb_header
;
2975 xs
->base
= (void *)xs
->header
;
2976 xs
->end
= (void *)xblk
+ inode
->i_sb
->s_blocksize
;
2977 xs
->here
= xs
->header
->xh_entries
;
2979 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
2981 if (!(le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2982 ocfs2_init_xattr_block_xa_loc(&loc
, inode
, xs
->xattr_bh
,
2983 xs
->not_found
? NULL
: xs
->here
);
2985 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
2987 xs
->here
= loc
.xl_entry
;
2988 else if ((ret
!= -ENOSPC
) || ctxt
->set_abort
)
2991 ret
= ocfs2_xattr_create_index_block(inode
, xs
, ctxt
);
2997 if (le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)
2998 ret
= ocfs2_xattr_set_entry_index_block(inode
, xi
, xs
, ctxt
);
3004 /* Check whether the new xattr can be inserted into the inode. */
3005 static int ocfs2_xattr_can_be_in_inode(struct inode
*inode
,
3006 struct ocfs2_xattr_info
*xi
,
3007 struct ocfs2_xattr_search
*xs
)
3009 struct ocfs2_xattr_entry
*last
;
3011 size_t min_offs
= xs
->end
- xs
->base
;
3016 last
= xs
->header
->xh_entries
;
3018 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
3019 size_t offs
= le16_to_cpu(last
->xe_name_offset
);
3020 if (offs
< min_offs
)
3025 free
= min_offs
- ((void *)last
- xs
->base
) - OCFS2_XATTR_HEADER_GAP
;
3029 BUG_ON(!xs
->not_found
);
3031 if (free
>= (sizeof(struct ocfs2_xattr_entry
) + namevalue_size_xi(xi
)))
3037 static int ocfs2_calc_xattr_set_need(struct inode
*inode
,
3038 struct ocfs2_dinode
*di
,
3039 struct ocfs2_xattr_info
*xi
,
3040 struct ocfs2_xattr_search
*xis
,
3041 struct ocfs2_xattr_search
*xbs
,
3046 int ret
= 0, old_in_xb
= 0;
3047 int clusters_add
= 0, meta_add
= 0, credits
= 0;
3048 struct buffer_head
*bh
= NULL
;
3049 struct ocfs2_xattr_block
*xb
= NULL
;
3050 struct ocfs2_xattr_entry
*xe
= NULL
;
3051 struct ocfs2_xattr_value_root
*xv
= NULL
;
3053 int name_offset
, name_len
= 0;
3054 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
3059 * Calculate the clusters we need to write.
3060 * No matter whether we replace an old one or add a new one,
3061 * we need this for writing.
3063 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
)
3064 credits
+= new_clusters
*
3065 ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3067 if (xis
->not_found
&& xbs
->not_found
) {
3068 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3070 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
3071 clusters_add
+= new_clusters
;
3072 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3073 &def_xv
.xv
.xr_list
);
3079 if (!xis
->not_found
) {
3081 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3082 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3084 credits
+= OCFS2_INODE_UPDATE_CREDITS
;
3086 int i
, block_off
= 0;
3087 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
3089 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3090 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3091 i
= xbs
->here
- xbs
->header
->xh_entries
;
3094 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
3095 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
3096 bucket_xh(xbs
->bucket
),
3099 base
= bucket_block(xbs
->bucket
, block_off
);
3100 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3103 credits
+= OCFS2_XATTR_BLOCK_UPDATE_CREDITS
;
3108 * delete a xattr doesn't need metadata and cluster allocation.
3109 * so just calculate the credits and return.
3111 * The credits for removing the value tree will be extended
3112 * by ocfs2_remove_extent itself.
3114 if (!xi
->xi_value
) {
3115 if (!ocfs2_xattr_is_local(xe
))
3116 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
);
3121 /* do cluster allocation guess first. */
3122 value_size
= le64_to_cpu(xe
->xe_value_size
);
3126 * In xattr set, we always try to set the xe in inode first,
3127 * so if it can be inserted into inode successfully, the old
3128 * one will be removed from the xattr block, and this xattr
3129 * will be inserted into inode as a new xattr in inode.
3131 if (ocfs2_xattr_can_be_in_inode(inode
, xi
, xis
)) {
3132 clusters_add
+= new_clusters
;
3133 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
) +
3134 OCFS2_INODE_UPDATE_CREDITS
;
3135 if (!ocfs2_xattr_is_local(xe
))
3136 credits
+= ocfs2_calc_extend_credits(
3138 &def_xv
.xv
.xr_list
);
3143 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
3144 /* the new values will be stored outside. */
3145 u32 old_clusters
= 0;
3147 if (!ocfs2_xattr_is_local(xe
)) {
3148 old_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
3150 xv
= (struct ocfs2_xattr_value_root
*)
3151 (base
+ name_offset
+ name_len
);
3152 value_size
= OCFS2_XATTR_ROOT_SIZE
;
3156 if (old_clusters
>= new_clusters
) {
3157 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
);
3160 meta_add
+= ocfs2_extend_meta_needed(&xv
->xr_list
);
3161 clusters_add
+= new_clusters
- old_clusters
;
3162 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3164 if (value_size
>= OCFS2_XATTR_ROOT_SIZE
)
3169 * Now the new value will be stored inside. So if the new
3170 * value is smaller than the size of value root or the old
3171 * value, we don't need any allocation, otherwise we have
3172 * to guess metadata allocation.
3174 if ((ocfs2_xattr_is_local(xe
) &&
3175 (value_size
>= xi
->xi_value_len
)) ||
3176 (!ocfs2_xattr_is_local(xe
) &&
3177 OCFS2_XATTR_ROOT_SIZE
>= xi
->xi_value_len
))
3182 /* calculate metadata allocation. */
3183 if (di
->i_xattr_loc
) {
3184 if (!xbs
->xattr_bh
) {
3185 ret
= ocfs2_read_xattr_block(inode
,
3186 le64_to_cpu(di
->i_xattr_loc
),
3193 xb
= (struct ocfs2_xattr_block
*)bh
->b_data
;
3195 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
3198 * If there is already an xattr tree, good, we can calculate
3199 * like other b-trees. Otherwise we may have the chance of
3200 * create a tree, the credit calculation is borrowed from
3201 * ocfs2_calc_extend_credits with root_el = NULL. And the
3202 * new tree will be cluster based, so no meta is needed.
3204 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
3205 struct ocfs2_extent_list
*el
=
3206 &xb
->xb_attrs
.xb_root
.xt_list
;
3207 meta_add
+= ocfs2_extend_meta_needed(el
);
3208 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3211 credits
+= OCFS2_SUBALLOC_ALLOC
+ 1;
3214 * This cluster will be used either for new bucket or for
3216 * If the cluster size is the same as the bucket size, one
3217 * more is needed since we may need to extend the bucket
3221 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3222 if (OCFS2_XATTR_BUCKET_SIZE
==
3223 OCFS2_SB(inode
->i_sb
)->s_clustersize
) {
3224 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3228 credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
3229 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
3230 struct ocfs2_extent_list
*el
= &def_xv
.xv
.xr_list
;
3231 meta_add
+= ocfs2_extend_meta_needed(el
);
3232 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3240 *clusters_need
= clusters_add
;
3242 *meta_need
= meta_add
;
3244 *credits_need
= credits
;
3249 static int ocfs2_init_xattr_set_ctxt(struct inode
*inode
,
3250 struct ocfs2_dinode
*di
,
3251 struct ocfs2_xattr_info
*xi
,
3252 struct ocfs2_xattr_search
*xis
,
3253 struct ocfs2_xattr_search
*xbs
,
3254 struct ocfs2_xattr_set_ctxt
*ctxt
,
3258 int clusters_add
, meta_add
, ret
;
3259 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3261 memset(ctxt
, 0, sizeof(struct ocfs2_xattr_set_ctxt
));
3263 ocfs2_init_dealloc_ctxt(&ctxt
->dealloc
);
3265 ret
= ocfs2_calc_xattr_set_need(inode
, di
, xi
, xis
, xbs
,
3266 &clusters_add
, &meta_add
, credits
);
3272 meta_add
+= extra_meta
;
3273 trace_ocfs2_init_xattr_set_ctxt(xi
->xi_name
, meta_add
,
3274 clusters_add
, *credits
);
3277 ret
= ocfs2_reserve_new_metadata_blocks(osb
, meta_add
,
3286 ret
= ocfs2_reserve_clusters(osb
, clusters_add
, &ctxt
->data_ac
);
3292 if (ctxt
->meta_ac
) {
3293 ocfs2_free_alloc_context(ctxt
->meta_ac
);
3294 ctxt
->meta_ac
= NULL
;
3298 * We cannot have an error and a non null ctxt->data_ac.
3305 static int __ocfs2_xattr_set_handle(struct inode
*inode
,
3306 struct ocfs2_dinode
*di
,
3307 struct ocfs2_xattr_info
*xi
,
3308 struct ocfs2_xattr_search
*xis
,
3309 struct ocfs2_xattr_search
*xbs
,
3310 struct ocfs2_xattr_set_ctxt
*ctxt
)
3312 int ret
= 0, credits
, old_found
;
3314 if (!xi
->xi_value
) {
3315 /* Remove existing extended attribute */
3316 if (!xis
->not_found
)
3317 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
3318 else if (!xbs
->not_found
)
3319 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3321 /* We always try to set extended attribute into inode first*/
3322 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
3323 if (!ret
&& !xbs
->not_found
) {
3325 * If succeed and that extended attribute existing in
3326 * external block, then we will remove it.
3328 xi
->xi_value
= NULL
;
3329 xi
->xi_value_len
= 0;
3331 old_found
= xis
->not_found
;
3332 xis
->not_found
= -ENODATA
;
3333 ret
= ocfs2_calc_xattr_set_need(inode
,
3341 xis
->not_found
= old_found
;
3347 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
);
3352 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3353 } else if ((ret
== -ENOSPC
) && !ctxt
->set_abort
) {
3354 if (di
->i_xattr_loc
&& !xbs
->xattr_bh
) {
3355 ret
= ocfs2_xattr_block_find(inode
,
3361 old_found
= xis
->not_found
;
3362 xis
->not_found
= -ENODATA
;
3363 ret
= ocfs2_calc_xattr_set_need(inode
,
3371 xis
->not_found
= old_found
;
3377 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
);
3384 * If no space in inode, we will set extended attribute
3385 * into external block.
3387 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3390 if (!xis
->not_found
) {
3392 * If succeed and that extended attribute
3393 * existing in inode, we will remove it.
3395 xi
->xi_value
= NULL
;
3396 xi
->xi_value_len
= 0;
3397 xbs
->not_found
= -ENODATA
;
3398 ret
= ocfs2_calc_xattr_set_need(inode
,
3411 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
);
3416 ret
= ocfs2_xattr_ibody_set(inode
, xi
,
3423 /* Update inode ctime. */
3424 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
),
3426 OCFS2_JOURNAL_ACCESS_WRITE
);
3432 inode_set_ctime_current(inode
);
3433 di
->i_ctime
= cpu_to_le64(inode_get_ctime_sec(inode
));
3434 di
->i_ctime_nsec
= cpu_to_le32(inode_get_ctime_nsec(inode
));
3435 ocfs2_journal_dirty(ctxt
->handle
, xis
->inode_bh
);
3442 * This function only called duing creating inode
3443 * for init security/acl xattrs of the new inode.
3444 * All transanction credits have been reserved in mknod.
3446 int ocfs2_xattr_set_handle(handle_t
*handle
,
3447 struct inode
*inode
,
3448 struct buffer_head
*di_bh
,
3454 struct ocfs2_alloc_context
*meta_ac
,
3455 struct ocfs2_alloc_context
*data_ac
)
3457 struct ocfs2_dinode
*di
;
3460 struct ocfs2_xattr_info xi
= {
3461 .xi_name_index
= name_index
,
3463 .xi_name_len
= strlen(name
),
3465 .xi_value_len
= value_len
,
3468 struct ocfs2_xattr_search xis
= {
3469 .not_found
= -ENODATA
,
3472 struct ocfs2_xattr_search xbs
= {
3473 .not_found
= -ENODATA
,
3476 struct ocfs2_xattr_set_ctxt ctxt
= {
3482 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
3486 * In extreme situation, may need xattr bucket when
3487 * block size is too small. And we have already reserved
3488 * the credits for bucket in mknod.
3490 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
) {
3491 xbs
.bucket
= ocfs2_xattr_bucket_new(inode
);
3493 mlog_errno(-ENOMEM
);
3498 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
3499 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3501 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3503 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
3506 if (xis
.not_found
) {
3507 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
3512 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
3515 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3516 brelse(xbs
.xattr_bh
);
3517 ocfs2_xattr_bucket_free(xbs
.bucket
);
3525 * Set, replace or remove an extended attribute for this inode.
3526 * value is NULL to remove an existing extended attribute, else either
3527 * create or replace an extended attribute.
3529 int ocfs2_xattr_set(struct inode
*inode
,
3536 struct buffer_head
*di_bh
= NULL
;
3537 struct ocfs2_dinode
*di
;
3538 int ret
, credits
, had_lock
, ref_meta
= 0, ref_credits
= 0;
3539 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3540 struct inode
*tl_inode
= osb
->osb_tl_inode
;
3541 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, NULL
, };
3542 struct ocfs2_refcount_tree
*ref_tree
= NULL
;
3543 struct ocfs2_lock_holder oh
;
3545 struct ocfs2_xattr_info xi
= {
3546 .xi_name_index
= name_index
,
3548 .xi_name_len
= strlen(name
),
3550 .xi_value_len
= value_len
,
3553 struct ocfs2_xattr_search xis
= {
3554 .not_found
= -ENODATA
,
3557 struct ocfs2_xattr_search xbs
= {
3558 .not_found
= -ENODATA
,
3561 if (!ocfs2_supports_xattr(osb
))
3565 * Only xbs will be used on indexed trees. xis doesn't need a
3568 xbs
.bucket
= ocfs2_xattr_bucket_new(inode
);
3570 mlog_errno(-ENOMEM
);
3574 had_lock
= ocfs2_inode_lock_tracker(inode
, &di_bh
, 1, &oh
);
3578 goto cleanup_nolock
;
3580 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
3581 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3583 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3585 * Scan inode and external block to find the same name
3586 * extended attribute and collect search information.
3588 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
3591 if (xis
.not_found
) {
3592 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
3597 if (xis
.not_found
&& xbs
.not_found
) {
3599 if (flags
& XATTR_REPLACE
)
3606 if (flags
& XATTR_CREATE
)
3610 /* Check whether the value is refcounted and do some preparation. */
3611 if (ocfs2_is_refcount_inode(inode
) &&
3612 (!xis
.not_found
|| !xbs
.not_found
)) {
3613 ret
= ocfs2_prepare_refcount_xattr(inode
, di
, &xi
,
3614 &xis
, &xbs
, &ref_tree
,
3615 &ref_meta
, &ref_credits
);
3622 inode_lock(tl_inode
);
3624 if (ocfs2_truncate_log_needs_flush(osb
)) {
3625 ret
= __ocfs2_flush_truncate_log(osb
);
3627 inode_unlock(tl_inode
);
3632 inode_unlock(tl_inode
);
3634 ret
= ocfs2_init_xattr_set_ctxt(inode
, di
, &xi
, &xis
,
3635 &xbs
, &ctxt
, ref_meta
, &credits
);
3641 /* we need to update inode's ctime field, so add credit for it. */
3642 credits
+= OCFS2_INODE_UPDATE_CREDITS
;
3643 ctxt
.handle
= ocfs2_start_trans(osb
, credits
+ ref_credits
);
3644 if (IS_ERR(ctxt
.handle
)) {
3645 ret
= PTR_ERR(ctxt
.handle
);
3650 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
3651 ocfs2_update_inode_fsync_trans(ctxt
.handle
, inode
, 0);
3653 ocfs2_commit_trans(osb
, ctxt
.handle
);
3657 ocfs2_free_alloc_context(ctxt
.data_ac
);
3659 ocfs2_free_alloc_context(ctxt
.meta_ac
);
3660 if (ocfs2_dealloc_has_cluster(&ctxt
.dealloc
))
3661 ocfs2_schedule_truncate_log_flush(osb
, 1);
3662 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
3666 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
3667 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3668 if (!value
&& !ret
) {
3669 ret
= ocfs2_try_remove_refcount_tree(inode
, di_bh
);
3673 ocfs2_inode_unlock_tracker(inode
, 1, &oh
, had_lock
);
3676 brelse(xbs
.xattr_bh
);
3677 ocfs2_xattr_bucket_free(xbs
.bucket
);
3683 * Find the xattr extent rec which may contains name_hash.
3684 * e_cpos will be the first name hash of the xattr rec.
3685 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3687 static int ocfs2_xattr_get_rec(struct inode
*inode
,
3692 struct ocfs2_extent_list
*el
)
3695 struct buffer_head
*eb_bh
= NULL
;
3696 struct ocfs2_extent_block
*eb
;
3697 struct ocfs2_extent_rec
*rec
= NULL
;
3700 if (el
->l_tree_depth
) {
3701 ret
= ocfs2_find_leaf(INODE_CACHE(inode
), el
, name_hash
,
3708 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
3711 if (el
->l_tree_depth
) {
3712 ret
= ocfs2_error(inode
->i_sb
,
3713 "Inode %lu has non zero tree depth in xattr tree block %llu\n",
3715 (unsigned long long)eb_bh
->b_blocknr
);
3720 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
3721 rec
= &el
->l_recs
[i
];
3723 if (le32_to_cpu(rec
->e_cpos
) <= name_hash
) {
3724 e_blkno
= le64_to_cpu(rec
->e_blkno
);
3730 ret
= ocfs2_error(inode
->i_sb
, "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
3732 le32_to_cpu(rec
->e_cpos
),
3733 ocfs2_rec_clusters(el
, rec
));
3737 *p_blkno
= le64_to_cpu(rec
->e_blkno
);
3738 *num_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
3740 *e_cpos
= le32_to_cpu(rec
->e_cpos
);
3746 typedef int (xattr_bucket_func
)(struct inode
*inode
,
3747 struct ocfs2_xattr_bucket
*bucket
,
3750 static int ocfs2_find_xe_in_bucket(struct inode
*inode
,
3751 struct ocfs2_xattr_bucket
*bucket
,
3758 int i
, ret
= 0, cmp
= 1, block_off
, new_offset
;
3759 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
3760 size_t name_len
= strlen(name
);
3761 struct ocfs2_xattr_entry
*xe
= NULL
;
3765 * We don't use binary search in the bucket because there
3766 * may be multiple entries with the same name hash.
3768 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
3769 xe
= &xh
->xh_entries
[i
];
3771 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
))
3773 else if (name_hash
< le32_to_cpu(xe
->xe_name_hash
))
3776 cmp
= name_index
- ocfs2_xattr_get_type(xe
);
3778 cmp
= name_len
- xe
->xe_name_len
;
3782 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
3793 xe_name
= bucket_block(bucket
, block_off
) + new_offset
;
3794 if (!memcmp(name
, xe_name
, name_len
)) {
3806 * Find the specified xattr entry in a series of buckets.
3807 * This series start from p_blkno and last for num_clusters.
3808 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3809 * the num of the valid buckets.
3811 * Return the buffer_head this xattr should reside in. And if the xattr's
3812 * hash is in the gap of 2 buckets, return the lower bucket.
3814 static int ocfs2_xattr_bucket_find(struct inode
*inode
,
3821 struct ocfs2_xattr_search
*xs
)
3824 struct ocfs2_xattr_header
*xh
= NULL
;
3825 struct ocfs2_xattr_entry
*xe
= NULL
;
3827 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3828 int low_bucket
= 0, bucket
, high_bucket
;
3829 struct ocfs2_xattr_bucket
*search
;
3830 u64 blkno
, lower_blkno
= 0;
3832 search
= ocfs2_xattr_bucket_new(inode
);
3839 ret
= ocfs2_read_xattr_bucket(search
, p_blkno
);
3845 xh
= bucket_xh(search
);
3846 high_bucket
= le16_to_cpu(xh
->xh_num_buckets
) - 1;
3847 while (low_bucket
<= high_bucket
) {
3848 ocfs2_xattr_bucket_relse(search
);
3850 bucket
= (low_bucket
+ high_bucket
) / 2;
3851 blkno
= p_blkno
+ bucket
* blk_per_bucket
;
3852 ret
= ocfs2_read_xattr_bucket(search
, blkno
);
3858 xh
= bucket_xh(search
);
3859 xe
= &xh
->xh_entries
[0];
3860 if (name_hash
< le32_to_cpu(xe
->xe_name_hash
)) {
3861 high_bucket
= bucket
- 1;
3866 * Check whether the hash of the last entry in our
3867 * bucket is larger than the search one. for an empty
3868 * bucket, the last one is also the first one.
3871 xe
= &xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1];
3873 /* record lower_blkno which may be the insert place. */
3874 lower_blkno
= blkno
;
3876 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
)) {
3877 low_bucket
= bucket
+ 1;
3881 /* the searched xattr should reside in this bucket if exists. */
3882 ret
= ocfs2_find_xe_in_bucket(inode
, search
,
3883 name_index
, name
, name_hash
,
3893 * Record the bucket we have found.
3894 * When the xattr's hash value is in the gap of 2 buckets, we will
3895 * always set it to the previous bucket.
3898 lower_blkno
= p_blkno
;
3900 /* This should be in cache - we just read it during the search */
3901 ret
= ocfs2_read_xattr_bucket(xs
->bucket
, lower_blkno
);
3907 xs
->header
= bucket_xh(xs
->bucket
);
3908 xs
->base
= bucket_block(xs
->bucket
, 0);
3909 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
3912 xs
->here
= &xs
->header
->xh_entries
[index
];
3913 trace_ocfs2_xattr_bucket_find(OCFS2_I(inode
)->ip_blkno
,
3914 name
, name_index
, name_hash
,
3915 (unsigned long long)bucket_blkno(xs
->bucket
),
3921 ocfs2_xattr_bucket_free(search
);
3925 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
3926 struct buffer_head
*root_bh
,
3929 struct ocfs2_xattr_search
*xs
)
3932 struct ocfs2_xattr_block
*xb
=
3933 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
3934 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
3935 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
3937 u32 first_hash
, num_clusters
= 0;
3938 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
3940 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
3943 trace_ocfs2_xattr_index_block_find(OCFS2_I(inode
)->ip_blkno
,
3944 name
, name_index
, name_hash
,
3945 (unsigned long long)root_bh
->b_blocknr
,
3948 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &first_hash
,
3955 BUG_ON(p_blkno
== 0 || num_clusters
== 0 || first_hash
> name_hash
);
3957 trace_ocfs2_xattr_index_block_find_rec(OCFS2_I(inode
)->ip_blkno
,
3958 name
, name_index
, first_hash
,
3959 (unsigned long long)p_blkno
,
3962 ret
= ocfs2_xattr_bucket_find(inode
, name_index
, name
, name_hash
,
3963 p_blkno
, first_hash
, num_clusters
, xs
);
3969 static int ocfs2_iterate_xattr_buckets(struct inode
*inode
,
3972 xattr_bucket_func
*func
,
3976 u32 bpc
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
3977 u32 num_buckets
= clusters
* bpc
;
3978 struct ocfs2_xattr_bucket
*bucket
;
3980 bucket
= ocfs2_xattr_bucket_new(inode
);
3982 mlog_errno(-ENOMEM
);
3986 trace_ocfs2_iterate_xattr_buckets(
3987 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
3988 (unsigned long long)blkno
, clusters
);
3990 for (i
= 0; i
< num_buckets
; i
++, blkno
+= bucket
->bu_blocks
) {
3991 ret
= ocfs2_read_xattr_bucket(bucket
, blkno
);
3998 * The real bucket num in this series of blocks is stored
3999 * in the 1st bucket.
4002 num_buckets
= le16_to_cpu(bucket_xh(bucket
)->xh_num_buckets
);
4004 trace_ocfs2_iterate_xattr_bucket((unsigned long long)blkno
,
4005 le32_to_cpu(bucket_xh(bucket
)->xh_entries
[0].xe_name_hash
));
4007 ret
= func(inode
, bucket
, para
);
4008 if (ret
&& ret
!= -ERANGE
)
4010 /* Fall through to bucket_relse() */
4013 ocfs2_xattr_bucket_relse(bucket
);
4018 ocfs2_xattr_bucket_free(bucket
);
4022 struct ocfs2_xattr_tree_list
{
4028 static int ocfs2_xattr_bucket_get_name_value(struct super_block
*sb
,
4029 struct ocfs2_xattr_header
*xh
,
4036 if (index
< 0 || index
>= le16_to_cpu(xh
->xh_count
))
4039 name_offset
= le16_to_cpu(xh
->xh_entries
[index
].xe_name_offset
);
4041 *block_off
= name_offset
>> sb
->s_blocksize_bits
;
4042 *new_offset
= name_offset
% sb
->s_blocksize
;
4047 static int ocfs2_list_xattr_bucket(struct inode
*inode
,
4048 struct ocfs2_xattr_bucket
*bucket
,
4052 struct ocfs2_xattr_tree_list
*xl
= (struct ocfs2_xattr_tree_list
*)para
;
4053 int i
, block_off
, new_offset
;
4056 for (i
= 0 ; i
< le16_to_cpu(bucket_xh(bucket
)->xh_count
); i
++) {
4057 struct ocfs2_xattr_entry
*entry
= &bucket_xh(bucket
)->xh_entries
[i
];
4058 type
= ocfs2_xattr_get_type(entry
);
4060 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
4068 name
= (const char *)bucket_block(bucket
, block_off
) +
4070 ret
= ocfs2_xattr_list_entry(inode
->i_sb
,
4075 entry
->xe_name_len
);
4083 static int ocfs2_iterate_xattr_index_block(struct inode
*inode
,
4084 struct buffer_head
*blk_bh
,
4085 xattr_tree_rec_func
*rec_func
,
4088 struct ocfs2_xattr_block
*xb
=
4089 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
4090 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
4092 u32 name_hash
= UINT_MAX
, e_cpos
= 0, num_clusters
= 0;
4095 if (!el
->l_next_free_rec
|| !rec_func
)
4098 while (name_hash
> 0) {
4099 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
4100 &e_cpos
, &num_clusters
, el
);
4106 ret
= rec_func(inode
, blk_bh
, p_blkno
, e_cpos
,
4107 num_clusters
, para
);
4117 name_hash
= e_cpos
- 1;
4124 static int ocfs2_list_xattr_tree_rec(struct inode
*inode
,
4125 struct buffer_head
*root_bh
,
4126 u64 blkno
, u32 cpos
, u32 len
, void *para
)
4128 return ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
4129 ocfs2_list_xattr_bucket
, para
);
4132 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
4133 struct buffer_head
*blk_bh
,
4138 struct ocfs2_xattr_tree_list xl
= {
4140 .buffer_size
= buffer_size
,
4144 ret
= ocfs2_iterate_xattr_index_block(inode
, blk_bh
,
4145 ocfs2_list_xattr_tree_rec
, &xl
);
4156 static int cmp_xe(const void *a
, const void *b
)
4158 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
4159 u32 l_hash
= le32_to_cpu(l
->xe_name_hash
);
4160 u32 r_hash
= le32_to_cpu(r
->xe_name_hash
);
4162 if (l_hash
> r_hash
)
4164 if (l_hash
< r_hash
)
4170 * When the ocfs2_xattr_block is filled up, new bucket will be created
4171 * and all the xattr entries will be moved to the new bucket.
4172 * The header goes at the start of the bucket, and the names+values are
4173 * filled from the end. This is why *target starts as the last buffer.
4174 * Note: we need to sort the entries since they are not saved in order
4175 * in the ocfs2_xattr_block.
4177 static void ocfs2_cp_xattr_block_to_bucket(struct inode
*inode
,
4178 struct buffer_head
*xb_bh
,
4179 struct ocfs2_xattr_bucket
*bucket
)
4181 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
4182 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4183 u16 offset
, size
, off_change
;
4184 struct ocfs2_xattr_entry
*xe
;
4185 struct ocfs2_xattr_block
*xb
=
4186 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4187 struct ocfs2_xattr_header
*xb_xh
= &xb
->xb_attrs
.xb_header
;
4188 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
4189 u16 count
= le16_to_cpu(xb_xh
->xh_count
);
4190 char *src
= xb_bh
->b_data
;
4191 char *target
= bucket_block(bucket
, blks
- 1);
4193 trace_ocfs2_cp_xattr_block_to_bucket_begin(
4194 (unsigned long long)xb_bh
->b_blocknr
,
4195 (unsigned long long)bucket_blkno(bucket
));
4197 for (i
= 0; i
< blks
; i
++)
4198 memset(bucket_block(bucket
, i
), 0, blocksize
);
4201 * Since the xe_name_offset is based on ocfs2_xattr_header,
4202 * there is a offset change corresponding to the change of
4203 * ocfs2_xattr_header's position.
4205 off_change
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
4206 xe
= &xb_xh
->xh_entries
[count
- 1];
4207 offset
= le16_to_cpu(xe
->xe_name_offset
) + off_change
;
4208 size
= blocksize
- offset
;
4210 /* copy all the names and values. */
4211 memcpy(target
+ offset
, src
+ offset
, size
);
4213 /* Init new header now. */
4214 xh
->xh_count
= xb_xh
->xh_count
;
4215 xh
->xh_num_buckets
= cpu_to_le16(1);
4216 xh
->xh_name_value_len
= cpu_to_le16(size
);
4217 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
- size
);
4219 /* copy all the entries. */
4220 target
= bucket_block(bucket
, 0);
4221 offset
= offsetof(struct ocfs2_xattr_header
, xh_entries
);
4222 size
= count
* sizeof(struct ocfs2_xattr_entry
);
4223 memcpy(target
+ offset
, (char *)xb_xh
+ offset
, size
);
4225 /* Change the xe offset for all the xe because of the move. */
4226 off_change
= OCFS2_XATTR_BUCKET_SIZE
- blocksize
+
4227 offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
4228 for (i
= 0; i
< count
; i
++)
4229 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
, off_change
);
4231 trace_ocfs2_cp_xattr_block_to_bucket_end(offset
, size
, off_change
);
4233 sort(target
+ offset
, count
, sizeof(struct ocfs2_xattr_entry
),
4238 * After we move xattr from block to index btree, we have to
4239 * update ocfs2_xattr_search to the new xe and base.
4241 * When the entry is in xattr block, xattr_bh indicates the storage place.
4242 * While if the entry is in index b-tree, "bucket" indicates the
4243 * real place of the xattr.
4245 static void ocfs2_xattr_update_xattr_search(struct inode
*inode
,
4246 struct ocfs2_xattr_search
*xs
,
4247 struct buffer_head
*old_bh
)
4249 char *buf
= old_bh
->b_data
;
4250 struct ocfs2_xattr_block
*old_xb
= (struct ocfs2_xattr_block
*)buf
;
4251 struct ocfs2_xattr_header
*old_xh
= &old_xb
->xb_attrs
.xb_header
;
4254 xs
->header
= bucket_xh(xs
->bucket
);
4255 xs
->base
= bucket_block(xs
->bucket
, 0);
4256 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
4261 i
= xs
->here
- old_xh
->xh_entries
;
4262 xs
->here
= &xs
->header
->xh_entries
[i
];
4265 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
4266 struct ocfs2_xattr_search
*xs
,
4267 struct ocfs2_xattr_set_ctxt
*ctxt
)
4272 handle_t
*handle
= ctxt
->handle
;
4273 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
4274 struct buffer_head
*xb_bh
= xs
->xattr_bh
;
4275 struct ocfs2_xattr_block
*xb
=
4276 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4277 struct ocfs2_xattr_tree_root
*xr
;
4278 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
4280 trace_ocfs2_xattr_create_index_block_begin(
4281 (unsigned long long)xb_bh
->b_blocknr
);
4283 BUG_ON(xb_flags
& OCFS2_XATTR_INDEXED
);
4284 BUG_ON(!xs
->bucket
);
4288 * We can use this lock for now, and maybe move to a dedicated mutex
4289 * if performance becomes a problem later.
4291 down_write(&oi
->ip_alloc_sem
);
4293 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), xb_bh
,
4294 OCFS2_JOURNAL_ACCESS_WRITE
);
4300 ret
= __ocfs2_claim_clusters(handle
, ctxt
->data_ac
,
4301 1, 1, &bit_off
, &len
);
4308 * The bucket may spread in many blocks, and
4309 * we will only touch the 1st block and the last block
4310 * in the whole bucket(one for entry and one for data).
4312 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
4314 trace_ocfs2_xattr_create_index_block((unsigned long long)blkno
);
4316 ret
= ocfs2_init_xattr_bucket(xs
->bucket
, blkno
, 1);
4322 ret
= ocfs2_xattr_bucket_journal_access(handle
, xs
->bucket
,
4323 OCFS2_JOURNAL_ACCESS_CREATE
);
4329 ocfs2_cp_xattr_block_to_bucket(inode
, xb_bh
, xs
->bucket
);
4330 ocfs2_xattr_bucket_journal_dirty(handle
, xs
->bucket
);
4332 ocfs2_xattr_update_xattr_search(inode
, xs
, xb_bh
);
4334 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4335 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
4336 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
4338 xr
= &xb
->xb_attrs
.xb_root
;
4339 xr
->xt_clusters
= cpu_to_le32(1);
4340 xr
->xt_last_eb_blk
= 0;
4341 xr
->xt_list
.l_tree_depth
= 0;
4342 xr
->xt_list
.l_count
= cpu_to_le16(ocfs2_xattr_recs_per_xb(inode
->i_sb
));
4343 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
4345 xr
->xt_list
.l_recs
[0].e_cpos
= 0;
4346 xr
->xt_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
4347 xr
->xt_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
4349 xb
->xb_flags
= cpu_to_le16(xb_flags
| OCFS2_XATTR_INDEXED
);
4351 ocfs2_journal_dirty(handle
, xb_bh
);
4354 up_write(&oi
->ip_alloc_sem
);
4359 static int cmp_xe_offset(const void *a
, const void *b
)
4361 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
4362 u32 l_name_offset
= le16_to_cpu(l
->xe_name_offset
);
4363 u32 r_name_offset
= le16_to_cpu(r
->xe_name_offset
);
4365 if (l_name_offset
< r_name_offset
)
4367 if (l_name_offset
> r_name_offset
)
4373 * defrag a xattr bucket if we find that the bucket has some
4374 * holes beteen name/value pairs.
4375 * We will move all the name/value pairs to the end of the bucket
4376 * so that we can spare some space for insertion.
4378 static int ocfs2_defrag_xattr_bucket(struct inode
*inode
,
4380 struct ocfs2_xattr_bucket
*bucket
)
4383 size_t end
, offset
, len
;
4384 struct ocfs2_xattr_header
*xh
;
4385 char *entries
, *buf
, *bucket_buf
= NULL
;
4386 u64 blkno
= bucket_blkno(bucket
);
4388 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4389 struct ocfs2_xattr_entry
*xe
;
4392 * In order to make the operation more efficient and generic,
4393 * we copy all the blocks into a contiguous memory and do the
4394 * defragment there, so if anything is error, we will not touch
4397 bucket_buf
= kmalloc(OCFS2_XATTR_BUCKET_SIZE
, GFP_NOFS
);
4404 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
4405 memcpy(buf
, bucket_block(bucket
, i
), blocksize
);
4407 ret
= ocfs2_xattr_bucket_journal_access(handle
, bucket
,
4408 OCFS2_JOURNAL_ACCESS_WRITE
);
4414 xh
= (struct ocfs2_xattr_header
*)bucket_buf
;
4415 entries
= (char *)xh
->xh_entries
;
4416 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4418 trace_ocfs2_defrag_xattr_bucket(
4419 (unsigned long long)blkno
, le16_to_cpu(xh
->xh_count
),
4420 xh_free_start
, le16_to_cpu(xh
->xh_name_value_len
));
4423 * sort all the entries by their offset.
4424 * the largest will be the first, so that we can
4425 * move them to the end one by one.
4427 sort(entries
, le16_to_cpu(xh
->xh_count
),
4428 sizeof(struct ocfs2_xattr_entry
),
4429 cmp_xe_offset
, NULL
);
4431 /* Move all name/values to the end of the bucket. */
4432 xe
= xh
->xh_entries
;
4433 end
= OCFS2_XATTR_BUCKET_SIZE
;
4434 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, xe
++) {
4435 offset
= le16_to_cpu(xe
->xe_name_offset
);
4436 len
= namevalue_size_xe(xe
);
4439 * We must make sure that the name/value pair
4440 * exist in the same block. So adjust end to
4441 * the previous block end if needed.
4443 if (((end
- len
) / blocksize
!=
4444 (end
- 1) / blocksize
))
4445 end
= end
- end
% blocksize
;
4447 if (end
> offset
+ len
) {
4448 memmove(bucket_buf
+ end
- len
,
4449 bucket_buf
+ offset
, len
);
4450 xe
->xe_name_offset
= cpu_to_le16(end
- len
);
4453 mlog_bug_on_msg(end
< offset
+ len
, "Defrag check failed for "
4454 "bucket %llu\n", (unsigned long long)blkno
);
4459 mlog_bug_on_msg(xh_free_start
> end
, "Defrag check failed for "
4460 "bucket %llu\n", (unsigned long long)blkno
);
4462 if (xh_free_start
== end
)
4465 memset(bucket_buf
+ xh_free_start
, 0, end
- xh_free_start
);
4466 xh
->xh_free_start
= cpu_to_le16(end
);
4468 /* sort the entries by their name_hash. */
4469 sort(entries
, le16_to_cpu(xh
->xh_count
),
4470 sizeof(struct ocfs2_xattr_entry
),
4474 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
4475 memcpy(bucket_block(bucket
, i
), buf
, blocksize
);
4476 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
4484 * prev_blkno points to the start of an existing extent. new_blkno
4485 * points to a newly allocated extent. Because we know each of our
4486 * clusters contains more than bucket, we can easily split one cluster
4487 * at a bucket boundary. So we take the last cluster of the existing
4488 * extent and split it down the middle. We move the last half of the
4489 * buckets in the last cluster of the existing extent over to the new
4492 * first_bh is the buffer at prev_blkno so we can update the existing
4493 * extent's bucket count. header_bh is the bucket were we were hoping
4494 * to insert our xattr. If the bucket move places the target in the new
4495 * extent, we'll update first_bh and header_bh after modifying the old
4498 * first_hash will be set as the 1st xe's name_hash in the new extent.
4500 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode
*inode
,
4502 struct ocfs2_xattr_bucket
*first
,
4503 struct ocfs2_xattr_bucket
*target
,
4509 struct super_block
*sb
= inode
->i_sb
;
4510 int blks_per_bucket
= ocfs2_blocks_per_xattr_bucket(sb
);
4511 int num_buckets
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb
));
4512 int to_move
= num_buckets
/ 2;
4514 u64 last_cluster_blkno
= bucket_blkno(first
) +
4515 ((num_clusters
- 1) * ocfs2_clusters_to_blocks(sb
, 1));
4517 BUG_ON(le16_to_cpu(bucket_xh(first
)->xh_num_buckets
) < num_buckets
);
4518 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
== OCFS2_SB(sb
)->s_clustersize
);
4520 trace_ocfs2_mv_xattr_bucket_cross_cluster(
4521 (unsigned long long)last_cluster_blkno
,
4522 (unsigned long long)new_blkno
);
4524 ret
= ocfs2_mv_xattr_buckets(inode
, handle
, bucket_blkno(first
),
4525 last_cluster_blkno
, new_blkno
,
4526 to_move
, first_hash
);
4532 /* This is the first bucket that got moved */
4533 src_blkno
= last_cluster_blkno
+ (to_move
* blks_per_bucket
);
4536 * If the target bucket was part of the moved buckets, we need to
4537 * update first and target.
4539 if (bucket_blkno(target
) >= src_blkno
) {
4540 /* Find the block for the new target bucket */
4541 src_blkno
= new_blkno
+
4542 (bucket_blkno(target
) - src_blkno
);
4544 ocfs2_xattr_bucket_relse(first
);
4545 ocfs2_xattr_bucket_relse(target
);
4548 * These shouldn't fail - the buffers are in the
4549 * journal from ocfs2_cp_xattr_bucket().
4551 ret
= ocfs2_read_xattr_bucket(first
, new_blkno
);
4556 ret
= ocfs2_read_xattr_bucket(target
, src_blkno
);
4567 * Find the suitable pos when we divide a bucket into 2.
4568 * We have to make sure the xattrs with the same hash value exist
4569 * in the same bucket.
4571 * If this ocfs2_xattr_header covers more than one hash value, find a
4572 * place where the hash value changes. Try to find the most even split.
4573 * The most common case is that all entries have different hash values,
4574 * and the first check we make will find a place to split.
4576 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header
*xh
)
4578 struct ocfs2_xattr_entry
*entries
= xh
->xh_entries
;
4579 int count
= le16_to_cpu(xh
->xh_count
);
4580 int delta
, middle
= count
/ 2;
4583 * We start at the middle. Each step gets farther away in both
4584 * directions. We therefore hit the change in hash value
4585 * nearest to the middle. Note that this loop does not execute for
4588 for (delta
= 0; delta
< middle
; delta
++) {
4589 /* Let's check delta earlier than middle */
4590 if (cmp_xe(&entries
[middle
- delta
- 1],
4591 &entries
[middle
- delta
]))
4592 return middle
- delta
;
4594 /* For even counts, don't walk off the end */
4595 if ((middle
+ delta
+ 1) == count
)
4598 /* Now try delta past middle */
4599 if (cmp_xe(&entries
[middle
+ delta
],
4600 &entries
[middle
+ delta
+ 1]))
4601 return middle
+ delta
+ 1;
4604 /* Every entry had the same hash */
4609 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4610 * first_hash will record the 1st hash of the new bucket.
4612 * Normally half of the xattrs will be moved. But we have to make
4613 * sure that the xattrs with the same hash value are stored in the
4614 * same bucket. If all the xattrs in this bucket have the same hash
4615 * value, the new bucket will be initialized as an empty one and the
4616 * first_hash will be initialized as (hash_value+1).
4618 static int ocfs2_divide_xattr_bucket(struct inode
*inode
,
4623 int new_bucket_head
)
4626 int count
, start
, len
, name_value_len
= 0, name_offset
= 0;
4627 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
4628 struct ocfs2_xattr_header
*xh
;
4629 struct ocfs2_xattr_entry
*xe
;
4630 int blocksize
= inode
->i_sb
->s_blocksize
;
4632 trace_ocfs2_divide_xattr_bucket_begin((unsigned long long)blk
,
4633 (unsigned long long)new_blk
);
4635 s_bucket
= ocfs2_xattr_bucket_new(inode
);
4636 t_bucket
= ocfs2_xattr_bucket_new(inode
);
4637 if (!s_bucket
|| !t_bucket
) {
4643 ret
= ocfs2_read_xattr_bucket(s_bucket
, blk
);
4649 ret
= ocfs2_xattr_bucket_journal_access(handle
, s_bucket
,
4650 OCFS2_JOURNAL_ACCESS_WRITE
);
4657 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4658 * there's no need to read it.
4660 ret
= ocfs2_init_xattr_bucket(t_bucket
, new_blk
, new_bucket_head
);
4667 * Hey, if we're overwriting t_bucket, what difference does
4668 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4669 * same part of ocfs2_cp_xattr_bucket().
4671 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
4673 OCFS2_JOURNAL_ACCESS_CREATE
:
4674 OCFS2_JOURNAL_ACCESS_WRITE
);
4680 xh
= bucket_xh(s_bucket
);
4681 count
= le16_to_cpu(xh
->xh_count
);
4682 start
= ocfs2_xattr_find_divide_pos(xh
);
4684 if (start
== count
) {
4685 xe
= &xh
->xh_entries
[start
-1];
4688 * initialized a new empty bucket here.
4689 * The hash value is set as one larger than
4690 * that of the last entry in the previous bucket.
4692 for (i
= 0; i
< t_bucket
->bu_blocks
; i
++)
4693 memset(bucket_block(t_bucket
, i
), 0, blocksize
);
4695 xh
= bucket_xh(t_bucket
);
4696 xh
->xh_free_start
= cpu_to_le16(blocksize
);
4697 xh
->xh_entries
[0].xe_name_hash
= xe
->xe_name_hash
;
4698 le32_add_cpu(&xh
->xh_entries
[0].xe_name_hash
, 1);
4700 goto set_num_buckets
;
4703 /* copy the whole bucket to the new first. */
4704 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
4706 /* update the new bucket. */
4707 xh
= bucket_xh(t_bucket
);
4710 * Calculate the total name/value len and xh_free_start for
4711 * the old bucket first.
4713 name_offset
= OCFS2_XATTR_BUCKET_SIZE
;
4715 for (i
= 0; i
< start
; i
++) {
4716 xe
= &xh
->xh_entries
[i
];
4717 name_value_len
+= namevalue_size_xe(xe
);
4718 if (le16_to_cpu(xe
->xe_name_offset
) < name_offset
)
4719 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
4723 * Now begin the modification to the new bucket.
4725 * In the new bucket, We just move the xattr entry to the beginning
4726 * and don't touch the name/value. So there will be some holes in the
4727 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4730 xe
= &xh
->xh_entries
[start
];
4731 len
= sizeof(struct ocfs2_xattr_entry
) * (count
- start
);
4732 trace_ocfs2_divide_xattr_bucket_move(len
,
4733 (int)((char *)xe
- (char *)xh
),
4734 (int)((char *)xh
->xh_entries
- (char *)xh
));
4735 memmove((char *)xh
->xh_entries
, (char *)xe
, len
);
4736 xe
= &xh
->xh_entries
[count
- start
];
4737 len
= sizeof(struct ocfs2_xattr_entry
) * start
;
4738 memset((char *)xe
, 0, len
);
4740 le16_add_cpu(&xh
->xh_count
, -start
);
4741 le16_add_cpu(&xh
->xh_name_value_len
, -name_value_len
);
4743 /* Calculate xh_free_start for the new bucket. */
4744 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
4745 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
4746 xe
= &xh
->xh_entries
[i
];
4747 if (le16_to_cpu(xe
->xe_name_offset
) <
4748 le16_to_cpu(xh
->xh_free_start
))
4749 xh
->xh_free_start
= xe
->xe_name_offset
;
4753 /* set xh->xh_num_buckets for the new xh. */
4754 if (new_bucket_head
)
4755 xh
->xh_num_buckets
= cpu_to_le16(1);
4757 xh
->xh_num_buckets
= 0;
4759 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
4761 /* store the first_hash of the new bucket. */
4763 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
4766 * Now only update the 1st block of the old bucket. If we
4767 * just added a new empty bucket, there is no need to modify
4773 xh
= bucket_xh(s_bucket
);
4774 memset(&xh
->xh_entries
[start
], 0,
4775 sizeof(struct ocfs2_xattr_entry
) * (count
- start
));
4776 xh
->xh_count
= cpu_to_le16(start
);
4777 xh
->xh_free_start
= cpu_to_le16(name_offset
);
4778 xh
->xh_name_value_len
= cpu_to_le16(name_value_len
);
4780 ocfs2_xattr_bucket_journal_dirty(handle
, s_bucket
);
4783 ocfs2_xattr_bucket_free(s_bucket
);
4784 ocfs2_xattr_bucket_free(t_bucket
);
4790 * Copy xattr from one bucket to another bucket.
4792 * The caller must make sure that the journal transaction
4793 * has enough space for journaling.
4795 static int ocfs2_cp_xattr_bucket(struct inode
*inode
,
4802 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
4804 BUG_ON(s_blkno
== t_blkno
);
4806 trace_ocfs2_cp_xattr_bucket((unsigned long long)s_blkno
,
4807 (unsigned long long)t_blkno
,
4810 s_bucket
= ocfs2_xattr_bucket_new(inode
);
4811 t_bucket
= ocfs2_xattr_bucket_new(inode
);
4812 if (!s_bucket
|| !t_bucket
) {
4818 ret
= ocfs2_read_xattr_bucket(s_bucket
, s_blkno
);
4823 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4824 * there's no need to read it.
4826 ret
= ocfs2_init_xattr_bucket(t_bucket
, t_blkno
, t_is_new
);
4831 * Hey, if we're overwriting t_bucket, what difference does
4832 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4833 * cluster to fill, we came here from
4834 * ocfs2_mv_xattr_buckets(), and it is really new -
4835 * ACCESS_CREATE is required. But we also might have moved data
4836 * out of t_bucket before extending back into it.
4837 * ocfs2_add_new_xattr_bucket() can do this - its call to
4838 * ocfs2_add_new_xattr_cluster() may have created a new extent
4839 * and copied out the end of the old extent. Then it re-extends
4840 * the old extent back to create space for new xattrs. That's
4841 * how we get here, and the bucket isn't really new.
4843 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
4845 OCFS2_JOURNAL_ACCESS_CREATE
:
4846 OCFS2_JOURNAL_ACCESS_WRITE
);
4850 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
4851 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
4854 ocfs2_xattr_bucket_free(t_bucket
);
4855 ocfs2_xattr_bucket_free(s_bucket
);
4861 * src_blk points to the start of an existing extent. last_blk points to
4862 * last cluster in that extent. to_blk points to a newly allocated
4863 * extent. We copy the buckets from the cluster at last_blk to the new
4864 * extent. If start_bucket is non-zero, we skip that many buckets before
4865 * we start copying. The new extent's xh_num_buckets gets set to the
4866 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4867 * by the same amount.
4869 static int ocfs2_mv_xattr_buckets(struct inode
*inode
, handle_t
*handle
,
4870 u64 src_blk
, u64 last_blk
, u64 to_blk
,
4871 unsigned int start_bucket
,
4874 int i
, ret
, credits
;
4875 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4876 int blks_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4877 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
4878 struct ocfs2_xattr_bucket
*old_first
, *new_first
;
4880 trace_ocfs2_mv_xattr_buckets((unsigned long long)last_blk
,
4881 (unsigned long long)to_blk
);
4883 BUG_ON(start_bucket
>= num_buckets
);
4885 num_buckets
-= start_bucket
;
4886 last_blk
+= (start_bucket
* blks_per_bucket
);
4889 /* The first bucket of the original extent */
4890 old_first
= ocfs2_xattr_bucket_new(inode
);
4891 /* The first bucket of the new extent */
4892 new_first
= ocfs2_xattr_bucket_new(inode
);
4893 if (!old_first
|| !new_first
) {
4899 ret
= ocfs2_read_xattr_bucket(old_first
, src_blk
);
4906 * We need to update the first bucket of the old extent and all
4907 * the buckets going to the new extent.
4909 credits
= ((num_buckets
+ 1) * blks_per_bucket
);
4910 ret
= ocfs2_extend_trans(handle
, credits
);
4916 ret
= ocfs2_xattr_bucket_journal_access(handle
, old_first
,
4917 OCFS2_JOURNAL_ACCESS_WRITE
);
4923 for (i
= 0; i
< num_buckets
; i
++) {
4924 ret
= ocfs2_cp_xattr_bucket(inode
, handle
,
4925 last_blk
+ (i
* blks_per_bucket
),
4926 to_blk
+ (i
* blks_per_bucket
),
4935 * Get the new bucket ready before we dirty anything
4936 * (This actually shouldn't fail, because we already dirtied
4937 * it once in ocfs2_cp_xattr_bucket()).
4939 ret
= ocfs2_read_xattr_bucket(new_first
, to_blk
);
4944 ret
= ocfs2_xattr_bucket_journal_access(handle
, new_first
,
4945 OCFS2_JOURNAL_ACCESS_WRITE
);
4951 /* Now update the headers */
4952 le16_add_cpu(&bucket_xh(old_first
)->xh_num_buckets
, -num_buckets
);
4953 ocfs2_xattr_bucket_journal_dirty(handle
, old_first
);
4955 bucket_xh(new_first
)->xh_num_buckets
= cpu_to_le16(num_buckets
);
4956 ocfs2_xattr_bucket_journal_dirty(handle
, new_first
);
4959 *first_hash
= le32_to_cpu(bucket_xh(new_first
)->xh_entries
[0].xe_name_hash
);
4962 ocfs2_xattr_bucket_free(new_first
);
4963 ocfs2_xattr_bucket_free(old_first
);
4968 * Move some xattrs in this cluster to the new cluster.
4969 * This function should only be called when bucket size == cluster size.
4970 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4972 static int ocfs2_divide_xattr_cluster(struct inode
*inode
,
4978 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4979 int ret
, credits
= 2 * blk_per_bucket
;
4981 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
< OCFS2_SB(inode
->i_sb
)->s_clustersize
);
4983 ret
= ocfs2_extend_trans(handle
, credits
);
4989 /* Move half of the xattr in start_blk to the next bucket. */
4990 return ocfs2_divide_xattr_bucket(inode
, handle
, prev_blk
,
4991 new_blk
, first_hash
, 1);
4995 * Move some xattrs from the old cluster to the new one since they are not
4996 * contiguous in ocfs2 xattr tree.
4998 * new_blk starts a new separate cluster, and we will move some xattrs from
4999 * prev_blk to it. v_start will be set as the first name hash value in this
5000 * new cluster so that it can be used as e_cpos during tree insertion and
5001 * don't collide with our original b-tree operations. first_bh and header_bh
5002 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
5003 * to extend the insert bucket.
5005 * The problem is how much xattr should we move to the new one and when should
5006 * we update first_bh and header_bh?
5007 * 1. If cluster size > bucket size, that means the previous cluster has more
5008 * than 1 bucket, so just move half nums of bucket into the new cluster and
5009 * update the first_bh and header_bh if the insert bucket has been moved
5010 * to the new cluster.
5011 * 2. If cluster_size == bucket_size:
5012 * a) If the previous extent rec has more than one cluster and the insert
5013 * place isn't in the last cluster, copy the entire last cluster to the
5014 * new one. This time, we don't need to upate the first_bh and header_bh
5015 * since they will not be moved into the new cluster.
5016 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
5017 * the new one. And we set the extend flag to zero if the insert place is
5018 * moved into the new allocated cluster since no extend is needed.
5020 static int ocfs2_adjust_xattr_cross_cluster(struct inode
*inode
,
5022 struct ocfs2_xattr_bucket
*first
,
5023 struct ocfs2_xattr_bucket
*target
,
5031 trace_ocfs2_adjust_xattr_cross_cluster(
5032 (unsigned long long)bucket_blkno(first
),
5033 (unsigned long long)new_blk
, prev_clusters
);
5035 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
)) > 1) {
5036 ret
= ocfs2_mv_xattr_bucket_cross_cluster(inode
,
5045 /* The start of the last cluster in the first extent */
5046 u64 last_blk
= bucket_blkno(first
) +
5047 ((prev_clusters
- 1) *
5048 ocfs2_clusters_to_blocks(inode
->i_sb
, 1));
5050 if (prev_clusters
> 1 && bucket_blkno(target
) != last_blk
) {
5051 ret
= ocfs2_mv_xattr_buckets(inode
, handle
,
5052 bucket_blkno(first
),
5053 last_blk
, new_blk
, 0,
5058 ret
= ocfs2_divide_xattr_cluster(inode
, handle
,
5064 if ((bucket_blkno(target
) == last_blk
) && extend
)
5073 * Add a new cluster for xattr storage.
5075 * If the new cluster is contiguous with the previous one, it will be
5076 * appended to the same extent record, and num_clusters will be updated.
5077 * If not, we will insert a new extent for it and move some xattrs in
5078 * the last cluster into the new allocated one.
5079 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5080 * lose the benefits of hashing because we'll have to search large leaves.
5081 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5084 * first_bh is the first block of the previous extent rec and header_bh
5085 * indicates the bucket we will insert the new xattrs. They will be updated
5086 * when the header_bh is moved into the new cluster.
5088 static int ocfs2_add_new_xattr_cluster(struct inode
*inode
,
5089 struct buffer_head
*root_bh
,
5090 struct ocfs2_xattr_bucket
*first
,
5091 struct ocfs2_xattr_bucket
*target
,
5095 struct ocfs2_xattr_set_ctxt
*ctxt
)
5098 u16 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
5099 u32 prev_clusters
= *num_clusters
;
5100 u32 clusters_to_add
= 1, bit_off
, num_bits
, v_start
= 0;
5102 handle_t
*handle
= ctxt
->handle
;
5103 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5104 struct ocfs2_extent_tree et
;
5106 trace_ocfs2_add_new_xattr_cluster_begin(
5107 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5108 (unsigned long long)bucket_blkno(first
),
5109 prev_cpos
, prev_clusters
);
5111 ocfs2_init_xattr_tree_extent_tree(&et
, INODE_CACHE(inode
), root_bh
);
5113 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), root_bh
,
5114 OCFS2_JOURNAL_ACCESS_WRITE
);
5120 ret
= __ocfs2_claim_clusters(handle
, ctxt
->data_ac
, 1,
5121 clusters_to_add
, &bit_off
, &num_bits
);
5128 BUG_ON(num_bits
> clusters_to_add
);
5130 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
5131 trace_ocfs2_add_new_xattr_cluster((unsigned long long)block
, num_bits
);
5133 if (bucket_blkno(first
) + (prev_clusters
* bpc
) == block
&&
5134 (prev_clusters
+ num_bits
) << osb
->s_clustersize_bits
<=
5135 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
) {
5137 * If this cluster is contiguous with the old one and
5138 * adding this new cluster, we don't surpass the limit of
5139 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5140 * initialized and used like other buckets in the previous
5142 * So add it as a contiguous one. The caller will handle
5145 v_start
= prev_cpos
+ prev_clusters
;
5146 *num_clusters
= prev_clusters
+ num_bits
;
5148 ret
= ocfs2_adjust_xattr_cross_cluster(inode
,
5162 trace_ocfs2_add_new_xattr_cluster_insert((unsigned long long)block
,
5164 ret
= ocfs2_insert_extent(handle
, &et
, v_start
, block
,
5165 num_bits
, 0, ctxt
->meta_ac
);
5171 ocfs2_journal_dirty(handle
, root_bh
);
5178 * We are given an extent. 'first' is the bucket at the very front of
5179 * the extent. The extent has space for an additional bucket past
5180 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5181 * of the target bucket. We wish to shift every bucket past the target
5182 * down one, filling in that additional space. When we get back to the
5183 * target, we split the target between itself and the now-empty bucket
5184 * at target+1 (aka, target_blkno + blks_per_bucket).
5186 static int ocfs2_extend_xattr_bucket(struct inode
*inode
,
5188 struct ocfs2_xattr_bucket
*first
,
5193 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5194 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
5196 u16 new_bucket
= le16_to_cpu(bucket_xh(first
)->xh_num_buckets
);
5198 trace_ocfs2_extend_xattr_bucket((unsigned long long)target_blk
,
5199 (unsigned long long)bucket_blkno(first
),
5200 num_clusters
, new_bucket
);
5202 /* The extent must have room for an additional bucket */
5203 BUG_ON(new_bucket
>=
5204 (num_clusters
* ocfs2_xattr_buckets_per_cluster(osb
)));
5206 /* end_blk points to the last existing bucket */
5207 end_blk
= bucket_blkno(first
) + ((new_bucket
- 1) * blk_per_bucket
);
5210 * end_blk is the start of the last existing bucket.
5211 * Thus, (end_blk - target_blk) covers the target bucket and
5212 * every bucket after it up to, but not including, the last
5213 * existing bucket. Then we add the last existing bucket, the
5214 * new bucket, and the first bucket (3 * blk_per_bucket).
5216 credits
= (end_blk
- target_blk
) + (3 * blk_per_bucket
);
5217 ret
= ocfs2_extend_trans(handle
, credits
);
5223 ret
= ocfs2_xattr_bucket_journal_access(handle
, first
,
5224 OCFS2_JOURNAL_ACCESS_WRITE
);
5230 while (end_blk
!= target_blk
) {
5231 ret
= ocfs2_cp_xattr_bucket(inode
, handle
, end_blk
,
5232 end_blk
+ blk_per_bucket
, 0);
5235 end_blk
-= blk_per_bucket
;
5238 /* Move half of the xattr in target_blkno to the next bucket. */
5239 ret
= ocfs2_divide_xattr_bucket(inode
, handle
, target_blk
,
5240 target_blk
+ blk_per_bucket
, NULL
, 0);
5242 le16_add_cpu(&bucket_xh(first
)->xh_num_buckets
, 1);
5243 ocfs2_xattr_bucket_journal_dirty(handle
, first
);
5250 * Add new xattr bucket in an extent record and adjust the buckets
5251 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5252 * bucket we want to insert into.
5254 * In the easy case, we will move all the buckets after target down by
5255 * one. Half of target's xattrs will be moved to the next bucket.
5257 * If current cluster is full, we'll allocate a new one. This may not
5258 * be contiguous. The underlying calls will make sure that there is
5259 * space for the insert, shifting buckets around if necessary.
5260 * 'target' may be moved by those calls.
5262 static int ocfs2_add_new_xattr_bucket(struct inode
*inode
,
5263 struct buffer_head
*xb_bh
,
5264 struct ocfs2_xattr_bucket
*target
,
5265 struct ocfs2_xattr_set_ctxt
*ctxt
)
5267 struct ocfs2_xattr_block
*xb
=
5268 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
5269 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
5270 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
5272 le32_to_cpu(bucket_xh(target
)->xh_entries
[0].xe_name_hash
);
5273 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5274 int ret
, num_buckets
, extend
= 1;
5276 u32 e_cpos
, num_clusters
;
5277 /* The bucket at the front of the extent */
5278 struct ocfs2_xattr_bucket
*first
;
5280 trace_ocfs2_add_new_xattr_bucket(
5281 (unsigned long long)bucket_blkno(target
));
5283 /* The first bucket of the original extent */
5284 first
= ocfs2_xattr_bucket_new(inode
);
5291 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &e_cpos
,
5298 ret
= ocfs2_read_xattr_bucket(first
, p_blkno
);
5304 num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
) * num_clusters
;
5305 if (num_buckets
== le16_to_cpu(bucket_xh(first
)->xh_num_buckets
)) {
5307 * This can move first+target if the target bucket moves
5308 * to the new extent.
5310 ret
= ocfs2_add_new_xattr_cluster(inode
,
5325 ret
= ocfs2_extend_xattr_bucket(inode
,
5328 bucket_blkno(target
),
5335 ocfs2_xattr_bucket_free(first
);
5341 * Truncate the specified xe_off entry in xattr bucket.
5342 * bucket is indicated by header_bh and len is the new length.
5343 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5345 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5347 static int ocfs2_xattr_bucket_value_truncate(struct inode
*inode
,
5348 struct ocfs2_xattr_bucket
*bucket
,
5351 struct ocfs2_xattr_set_ctxt
*ctxt
)
5355 struct ocfs2_xattr_entry
*xe
;
5356 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5357 size_t blocksize
= inode
->i_sb
->s_blocksize
;
5358 struct ocfs2_xattr_value_buf vb
= {
5359 .vb_access
= ocfs2_journal_access
,
5362 xe
= &xh
->xh_entries
[xe_off
];
5364 BUG_ON(!xe
|| ocfs2_xattr_is_local(xe
));
5366 offset
= le16_to_cpu(xe
->xe_name_offset
) +
5367 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5369 value_blk
= offset
/ blocksize
;
5371 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5372 BUG_ON(value_blk
!= (offset
+ OCFS2_XATTR_ROOT_SIZE
- 1) / blocksize
);
5374 vb
.vb_bh
= bucket
->bu_bhs
[value_blk
];
5377 vb
.vb_xv
= (struct ocfs2_xattr_value_root
*)
5378 (vb
.vb_bh
->b_data
+ offset
% blocksize
);
5381 * From here on out we have to dirty the bucket. The generic
5382 * value calls only modify one of the bucket's bhs, but we need
5383 * to send the bucket at once. So if they error, they *could* have
5384 * modified something. We have to assume they did, and dirty
5385 * the whole bucket. This leaves us in a consistent state.
5387 trace_ocfs2_xattr_bucket_value_truncate(
5388 (unsigned long long)bucket_blkno(bucket
), xe_off
, len
);
5389 ret
= ocfs2_xattr_value_truncate(inode
, &vb
, len
, ctxt
);
5395 ret
= ocfs2_xattr_bucket_journal_access(ctxt
->handle
, bucket
,
5396 OCFS2_JOURNAL_ACCESS_WRITE
);
5402 xe
->xe_value_size
= cpu_to_le64(len
);
5404 ocfs2_xattr_bucket_journal_dirty(ctxt
->handle
, bucket
);
5410 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
5411 struct buffer_head
*root_bh
,
5418 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5419 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5421 struct ocfs2_xattr_block
*xb
=
5422 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
5423 struct ocfs2_alloc_context
*meta_ac
= NULL
;
5424 struct ocfs2_cached_dealloc_ctxt dealloc
;
5425 struct ocfs2_extent_tree et
;
5427 ret
= ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
5428 ocfs2_delete_xattr_in_bucket
, para
);
5434 ocfs2_init_xattr_tree_extent_tree(&et
, INODE_CACHE(inode
), root_bh
);
5436 ocfs2_init_dealloc_ctxt(&dealloc
);
5438 trace_ocfs2_rm_xattr_cluster(
5439 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5440 (unsigned long long)blkno
, cpos
, len
);
5442 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode
), blkno
,
5445 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
5451 inode_lock(tl_inode
);
5453 if (ocfs2_truncate_log_needs_flush(osb
)) {
5454 ret
= __ocfs2_flush_truncate_log(osb
);
5461 handle
= ocfs2_start_trans(osb
, ocfs2_remove_extent_credits(osb
->sb
));
5462 if (IS_ERR(handle
)) {
5468 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), root_bh
,
5469 OCFS2_JOURNAL_ACCESS_WRITE
);
5475 ret
= ocfs2_remove_extent(handle
, &et
, cpos
, len
, meta_ac
,
5482 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, -len
);
5483 ocfs2_journal_dirty(handle
, root_bh
);
5485 ret
= ocfs2_truncate_log_append(osb
, handle
, blkno
, len
);
5488 ocfs2_update_inode_fsync_trans(handle
, inode
, 0);
5491 ocfs2_commit_trans(osb
, handle
);
5493 ocfs2_schedule_truncate_log_flush(osb
, 1);
5495 inode_unlock(tl_inode
);
5498 ocfs2_free_alloc_context(meta_ac
);
5500 ocfs2_run_deallocs(osb
, &dealloc
);
5506 * check whether the xattr bucket is filled up with the same hash value.
5507 * If we want to insert the xattr with the same hash, return -ENOSPC.
5508 * If we want to insert a xattr with different hash value, go ahead
5509 * and ocfs2_divide_xattr_bucket will handle this.
5511 static int ocfs2_check_xattr_bucket_collision(struct inode
*inode
,
5512 struct ocfs2_xattr_bucket
*bucket
,
5515 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5516 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
5518 if (name_hash
!= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
))
5521 if (xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1].xe_name_hash
==
5522 xh
->xh_entries
[0].xe_name_hash
) {
5523 mlog(ML_ERROR
, "Too much hash collision in xattr bucket %llu, "
5525 (unsigned long long)bucket_blkno(bucket
),
5526 le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
));
5534 * Try to set the entry in the current bucket. If we fail, the caller
5535 * will handle getting us another bucket.
5537 static int ocfs2_xattr_set_entry_bucket(struct inode
*inode
,
5538 struct ocfs2_xattr_info
*xi
,
5539 struct ocfs2_xattr_search
*xs
,
5540 struct ocfs2_xattr_set_ctxt
*ctxt
)
5543 struct ocfs2_xa_loc loc
;
5545 trace_ocfs2_xattr_set_entry_bucket(xi
->xi_name
);
5547 ocfs2_init_xattr_bucket_xa_loc(&loc
, xs
->bucket
,
5548 xs
->not_found
? NULL
: xs
->here
);
5549 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
5551 xs
->here
= loc
.xl_entry
;
5554 if (ret
!= -ENOSPC
) {
5559 /* Ok, we need space. Let's try defragmenting the bucket. */
5560 ret
= ocfs2_defrag_xattr_bucket(inode
, ctxt
->handle
,
5567 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
5569 xs
->here
= loc
.xl_entry
;
5580 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
5581 struct ocfs2_xattr_info
*xi
,
5582 struct ocfs2_xattr_search
*xs
,
5583 struct ocfs2_xattr_set_ctxt
*ctxt
)
5587 trace_ocfs2_xattr_set_entry_index_block(xi
->xi_name
);
5589 ret
= ocfs2_xattr_set_entry_bucket(inode
, xi
, xs
, ctxt
);
5592 if (ret
!= -ENOSPC
) {
5597 /* Ack, need more space. Let's try to get another bucket! */
5600 * We do not allow for overlapping ranges between buckets. And
5601 * the maximum number of collisions we will allow for then is
5602 * one bucket's worth, so check it here whether we need to
5603 * add a new bucket for the insert.
5605 ret
= ocfs2_check_xattr_bucket_collision(inode
,
5613 ret
= ocfs2_add_new_xattr_bucket(inode
,
5623 * ocfs2_add_new_xattr_bucket() will have updated
5624 * xs->bucket if it moved, but it will not have updated
5625 * any of the other search fields. Thus, we drop it and
5626 * re-search. Everything should be cached, so it'll be
5629 ocfs2_xattr_bucket_relse(xs
->bucket
);
5630 ret
= ocfs2_xattr_index_block_find(inode
, xs
->xattr_bh
,
5633 if (ret
&& ret
!= -ENODATA
)
5635 xs
->not_found
= ret
;
5637 /* Ok, we have a new bucket, let's try again */
5638 ret
= ocfs2_xattr_set_entry_bucket(inode
, xi
, xs
, ctxt
);
5639 if (ret
&& (ret
!= -ENOSPC
))
5646 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
5647 struct ocfs2_xattr_bucket
*bucket
,
5650 int ret
= 0, ref_credits
;
5651 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5653 struct ocfs2_xattr_entry
*xe
;
5654 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5655 struct ocfs2_xattr_set_ctxt ctxt
= {NULL
, NULL
,};
5656 int credits
= ocfs2_remove_extent_credits(osb
->sb
) +
5657 ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
5658 struct ocfs2_xattr_value_root
*xv
;
5659 struct ocfs2_rm_xattr_bucket_para
*args
=
5660 (struct ocfs2_rm_xattr_bucket_para
*)para
;
5662 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
5664 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
5665 xe
= &xh
->xh_entries
[i
];
5666 if (ocfs2_xattr_is_local(xe
))
5669 ret
= ocfs2_get_xattr_tree_value_root(inode
->i_sb
, bucket
,
5676 ret
= ocfs2_lock_xattr_remove_allocators(inode
, xv
,
5682 ctxt
.handle
= ocfs2_start_trans(osb
, credits
+ ref_credits
);
5683 if (IS_ERR(ctxt
.handle
)) {
5684 ret
= PTR_ERR(ctxt
.handle
);
5689 ret
= ocfs2_xattr_bucket_value_truncate(inode
, bucket
,
5692 ocfs2_commit_trans(osb
, ctxt
.handle
);
5694 ocfs2_free_alloc_context(ctxt
.meta_ac
);
5695 ctxt
.meta_ac
= NULL
;
5704 ocfs2_free_alloc_context(ctxt
.meta_ac
);
5705 ocfs2_schedule_truncate_log_flush(osb
, 1);
5706 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
5711 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5712 * or change the extent record flag), we need to recalculate
5713 * the metaecc for the whole bucket. So it is done here.
5716 * We have to give the extra credits for the caller.
5718 static int ocfs2_xattr_bucket_post_refcount(struct inode
*inode
,
5723 struct ocfs2_xattr_bucket
*bucket
=
5724 (struct ocfs2_xattr_bucket
*)para
;
5726 ret
= ocfs2_xattr_bucket_journal_access(handle
, bucket
,
5727 OCFS2_JOURNAL_ACCESS_WRITE
);
5733 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
5739 * Special action we need if the xattr value is refcounted.
5741 * 1. If the xattr is refcounted, lock the tree.
5742 * 2. CoW the xattr if we are setting the new value and the value
5743 * will be stored outside.
5744 * 3. In other case, decrease_refcount will work for us, so just
5745 * lock the refcount tree, calculate the meta and credits is OK.
5747 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5748 * currently CoW is a completed transaction, while this function
5749 * will also lock the allocators and let us deadlock. So we will
5750 * CoW the whole xattr value.
5752 static int ocfs2_prepare_refcount_xattr(struct inode
*inode
,
5753 struct ocfs2_dinode
*di
,
5754 struct ocfs2_xattr_info
*xi
,
5755 struct ocfs2_xattr_search
*xis
,
5756 struct ocfs2_xattr_search
*xbs
,
5757 struct ocfs2_refcount_tree
**ref_tree
,
5762 struct ocfs2_xattr_block
*xb
;
5763 struct ocfs2_xattr_entry
*xe
;
5765 u32 p_cluster
, num_clusters
;
5766 unsigned int ext_flags
;
5767 int name_offset
, name_len
;
5768 struct ocfs2_xattr_value_buf vb
;
5769 struct ocfs2_xattr_bucket
*bucket
= NULL
;
5770 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5771 struct ocfs2_post_refcount refcount
;
5772 struct ocfs2_post_refcount
*p
= NULL
;
5773 struct buffer_head
*ref_root_bh
= NULL
;
5775 if (!xis
->not_found
) {
5777 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
5778 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5780 vb
.vb_bh
= xis
->inode_bh
;
5781 vb
.vb_access
= ocfs2_journal_access_di
;
5783 int i
, block_off
= 0;
5784 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
5786 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
5787 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5788 i
= xbs
->here
- xbs
->header
->xh_entries
;
5790 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
5791 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
5792 bucket_xh(xbs
->bucket
),
5799 base
= bucket_block(xbs
->bucket
, block_off
);
5800 vb
.vb_bh
= xbs
->bucket
->bu_bhs
[block_off
];
5801 vb
.vb_access
= ocfs2_journal_access
;
5803 if (ocfs2_meta_ecc(osb
)) {
5804 /*create parameters for ocfs2_post_refcount. */
5805 bucket
= xbs
->bucket
;
5806 refcount
.credits
= bucket
->bu_blocks
;
5807 refcount
.para
= bucket
;
5809 ocfs2_xattr_bucket_post_refcount
;
5814 vb
.vb_bh
= xbs
->xattr_bh
;
5815 vb
.vb_access
= ocfs2_journal_access_xb
;
5819 if (ocfs2_xattr_is_local(xe
))
5822 vb
.vb_xv
= (struct ocfs2_xattr_value_root
*)
5823 (base
+ name_offset
+ name_len
);
5825 ret
= ocfs2_xattr_get_clusters(inode
, 0, &p_cluster
,
5826 &num_clusters
, &vb
.vb_xv
->xr_list
,
5834 * We just need to check the 1st extent record, since we always
5835 * CoW the whole xattr. So there shouldn't be a xattr with
5836 * some REFCOUNT extent recs after the 1st one.
5838 if (!(ext_flags
& OCFS2_EXT_REFCOUNTED
))
5841 ret
= ocfs2_lock_refcount_tree(osb
, le64_to_cpu(di
->i_refcount_loc
),
5842 1, ref_tree
, &ref_root_bh
);
5849 * If we are deleting the xattr or the new size will be stored inside,
5850 * cool, leave it there, the xattr truncate process will remove them
5851 * for us(it still needs the refcount tree lock and the meta, credits).
5852 * And the worse case is that every cluster truncate will split the
5853 * refcount tree, and make the original extent become 3. So we will need
5854 * 2 * cluster more extent recs at most.
5856 if (!xi
->xi_value
|| xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
) {
5858 ret
= ocfs2_refcounted_xattr_delete_need(inode
,
5859 &(*ref_tree
)->rf_ci
,
5860 ref_root_bh
, vb
.vb_xv
,
5867 ret
= ocfs2_refcount_cow_xattr(inode
, di
, &vb
,
5868 *ref_tree
, ref_root_bh
, 0,
5869 le32_to_cpu(vb
.vb_xv
->xr_clusters
), p
);
5874 brelse(ref_root_bh
);
5879 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5880 * The physical clusters will be added to refcount tree.
5882 static int ocfs2_xattr_value_attach_refcount(struct inode
*inode
,
5883 struct ocfs2_xattr_value_root
*xv
,
5884 struct ocfs2_extent_tree
*value_et
,
5885 struct ocfs2_caching_info
*ref_ci
,
5886 struct buffer_head
*ref_root_bh
,
5887 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5888 struct ocfs2_post_refcount
*refcount
)
5891 u32 clusters
= le32_to_cpu(xv
->xr_clusters
);
5892 u32 cpos
, p_cluster
, num_clusters
;
5893 struct ocfs2_extent_list
*el
= &xv
->xr_list
;
5894 unsigned int ext_flags
;
5897 while (cpos
< clusters
) {
5898 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
5899 &num_clusters
, el
, &ext_flags
);
5905 cpos
+= num_clusters
;
5906 if ((ext_flags
& OCFS2_EXT_REFCOUNTED
))
5911 ret
= ocfs2_add_refcount_flag(inode
, value_et
,
5912 ref_ci
, ref_root_bh
,
5913 cpos
- num_clusters
,
5914 p_cluster
, num_clusters
,
5926 * Given a normal ocfs2_xattr_header, refcount all the entries which
5927 * have value stored outside.
5928 * Used for xattrs stored in inode and ocfs2_xattr_block.
5930 static int ocfs2_xattr_attach_refcount_normal(struct inode
*inode
,
5931 struct ocfs2_xattr_value_buf
*vb
,
5932 struct ocfs2_xattr_header
*header
,
5933 struct ocfs2_caching_info
*ref_ci
,
5934 struct buffer_head
*ref_root_bh
,
5935 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5938 struct ocfs2_xattr_entry
*xe
;
5939 struct ocfs2_xattr_value_root
*xv
;
5940 struct ocfs2_extent_tree et
;
5943 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
5944 xe
= &header
->xh_entries
[i
];
5946 if (ocfs2_xattr_is_local(xe
))
5949 xv
= (struct ocfs2_xattr_value_root
*)((void *)header
+
5950 le16_to_cpu(xe
->xe_name_offset
) +
5951 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
5954 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
5956 ret
= ocfs2_xattr_value_attach_refcount(inode
, xv
, &et
,
5957 ref_ci
, ref_root_bh
,
5968 static int ocfs2_xattr_inline_attach_refcount(struct inode
*inode
,
5969 struct buffer_head
*fe_bh
,
5970 struct ocfs2_caching_info
*ref_ci
,
5971 struct buffer_head
*ref_root_bh
,
5972 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5974 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
5975 struct ocfs2_xattr_header
*header
= (struct ocfs2_xattr_header
*)
5976 (fe_bh
->b_data
+ inode
->i_sb
->s_blocksize
-
5977 le16_to_cpu(di
->i_xattr_inline_size
));
5978 struct ocfs2_xattr_value_buf vb
= {
5980 .vb_access
= ocfs2_journal_access_di
,
5983 return ocfs2_xattr_attach_refcount_normal(inode
, &vb
, header
,
5984 ref_ci
, ref_root_bh
, dealloc
);
5987 struct ocfs2_xattr_tree_value_refcount_para
{
5988 struct ocfs2_caching_info
*ref_ci
;
5989 struct buffer_head
*ref_root_bh
;
5990 struct ocfs2_cached_dealloc_ctxt
*dealloc
;
5993 static int ocfs2_get_xattr_tree_value_root(struct super_block
*sb
,
5994 struct ocfs2_xattr_bucket
*bucket
,
5996 struct ocfs2_xattr_value_root
**xv
,
5997 struct buffer_head
**bh
)
5999 int ret
, block_off
, name_offset
;
6000 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
6001 struct ocfs2_xattr_entry
*xe
= &xh
->xh_entries
[offset
];
6004 ret
= ocfs2_xattr_bucket_get_name_value(sb
,
6014 base
= bucket_block(bucket
, block_off
);
6016 *xv
= (struct ocfs2_xattr_value_root
*)(base
+ name_offset
+
6017 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
6020 *bh
= bucket
->bu_bhs
[block_off
];
6026 * For a given xattr bucket, refcount all the entries which
6027 * have value stored outside.
6029 static int ocfs2_xattr_bucket_value_refcount(struct inode
*inode
,
6030 struct ocfs2_xattr_bucket
*bucket
,
6034 struct ocfs2_extent_tree et
;
6035 struct ocfs2_xattr_tree_value_refcount_para
*ref
=
6036 (struct ocfs2_xattr_tree_value_refcount_para
*)para
;
6037 struct ocfs2_xattr_header
*xh
=
6038 (struct ocfs2_xattr_header
*)bucket
->bu_bhs
[0]->b_data
;
6039 struct ocfs2_xattr_entry
*xe
;
6040 struct ocfs2_xattr_value_buf vb
= {
6041 .vb_access
= ocfs2_journal_access
,
6043 struct ocfs2_post_refcount refcount
= {
6044 .credits
= bucket
->bu_blocks
,
6046 .func
= ocfs2_xattr_bucket_post_refcount
,
6048 struct ocfs2_post_refcount
*p
= NULL
;
6050 /* We only need post_refcount if we support metaecc. */
6051 if (ocfs2_meta_ecc(OCFS2_SB(inode
->i_sb
)))
6054 trace_ocfs2_xattr_bucket_value_refcount(
6055 (unsigned long long)bucket_blkno(bucket
),
6056 le16_to_cpu(xh
->xh_count
));
6057 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
6058 xe
= &xh
->xh_entries
[i
];
6060 if (ocfs2_xattr_is_local(xe
))
6063 ret
= ocfs2_get_xattr_tree_value_root(inode
->i_sb
, bucket
, i
,
6064 &vb
.vb_xv
, &vb
.vb_bh
);
6070 ocfs2_init_xattr_value_extent_tree(&et
,
6071 INODE_CACHE(inode
), &vb
);
6073 ret
= ocfs2_xattr_value_attach_refcount(inode
, vb
.vb_xv
,
6087 static int ocfs2_refcount_xattr_tree_rec(struct inode
*inode
,
6088 struct buffer_head
*root_bh
,
6089 u64 blkno
, u32 cpos
, u32 len
, void *para
)
6091 return ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
6092 ocfs2_xattr_bucket_value_refcount
,
6096 static int ocfs2_xattr_block_attach_refcount(struct inode
*inode
,
6097 struct buffer_head
*blk_bh
,
6098 struct ocfs2_caching_info
*ref_ci
,
6099 struct buffer_head
*ref_root_bh
,
6100 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
6103 struct ocfs2_xattr_block
*xb
=
6104 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
6106 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
6107 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
6108 struct ocfs2_xattr_value_buf vb
= {
6110 .vb_access
= ocfs2_journal_access_xb
,
6113 ret
= ocfs2_xattr_attach_refcount_normal(inode
, &vb
, header
,
6114 ref_ci
, ref_root_bh
,
6117 struct ocfs2_xattr_tree_value_refcount_para para
= {
6119 .ref_root_bh
= ref_root_bh
,
6123 ret
= ocfs2_iterate_xattr_index_block(inode
, blk_bh
,
6124 ocfs2_refcount_xattr_tree_rec
,
6131 int ocfs2_xattr_attach_refcount_tree(struct inode
*inode
,
6132 struct buffer_head
*fe_bh
,
6133 struct ocfs2_caching_info
*ref_ci
,
6134 struct buffer_head
*ref_root_bh
,
6135 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
6138 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6139 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
6140 struct buffer_head
*blk_bh
= NULL
;
6142 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
6143 ret
= ocfs2_xattr_inline_attach_refcount(inode
, fe_bh
,
6144 ref_ci
, ref_root_bh
,
6152 if (!di
->i_xattr_loc
)
6155 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
6162 ret
= ocfs2_xattr_block_attach_refcount(inode
, blk_bh
, ref_ci
,
6163 ref_root_bh
, dealloc
);
6173 typedef int (should_xattr_reflinked
)(struct ocfs2_xattr_entry
*xe
);
6175 * Store the information we need in xattr reflink.
6176 * old_bh and new_bh are inode bh for the old and new inode.
6178 struct ocfs2_xattr_reflink
{
6179 struct inode
*old_inode
;
6180 struct inode
*new_inode
;
6181 struct buffer_head
*old_bh
;
6182 struct buffer_head
*new_bh
;
6183 struct ocfs2_caching_info
*ref_ci
;
6184 struct buffer_head
*ref_root_bh
;
6185 struct ocfs2_cached_dealloc_ctxt
*dealloc
;
6186 should_xattr_reflinked
*xattr_reflinked
;
6190 * Given a xattr header and xe offset,
6191 * return the proper xv and the corresponding bh.
6192 * xattr in inode, block and xattr tree have different implementaions.
6194 typedef int (get_xattr_value_root
)(struct super_block
*sb
,
6195 struct buffer_head
*bh
,
6196 struct ocfs2_xattr_header
*xh
,
6198 struct ocfs2_xattr_value_root
**xv
,
6199 struct buffer_head
**ret_bh
,
6203 * Calculate all the xattr value root metadata stored in this xattr header and
6204 * credits we need if we create them from the scratch.
6205 * We use get_xattr_value_root so that all types of xattr container can use it.
6207 static int ocfs2_value_metas_in_xattr_header(struct super_block
*sb
,
6208 struct buffer_head
*bh
,
6209 struct ocfs2_xattr_header
*xh
,
6210 int *metas
, int *credits
,
6212 get_xattr_value_root
*func
,
6216 struct ocfs2_xattr_value_root
*xv
;
6217 struct ocfs2_xattr_entry
*xe
;
6219 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
6220 xe
= &xh
->xh_entries
[i
];
6221 if (ocfs2_xattr_is_local(xe
))
6224 ret
= func(sb
, bh
, xh
, i
, &xv
, NULL
, para
);
6230 *metas
+= le16_to_cpu(xv
->xr_list
.l_tree_depth
) *
6231 le16_to_cpu(xv
->xr_list
.l_next_free_rec
);
6233 *credits
+= ocfs2_calc_extend_credits(sb
,
6234 &def_xv
.xv
.xr_list
);
6237 * If the value is a tree with depth > 1, We don't go deep
6238 * to the extent block, so just calculate a maximum record num.
6240 if (!xv
->xr_list
.l_tree_depth
)
6241 *num_recs
+= le16_to_cpu(xv
->xr_list
.l_next_free_rec
);
6243 *num_recs
+= ocfs2_clusters_for_bytes(sb
,
6250 /* Used by xattr inode and block to return the right xv and buffer_head. */
6251 static int ocfs2_get_xattr_value_root(struct super_block
*sb
,
6252 struct buffer_head
*bh
,
6253 struct ocfs2_xattr_header
*xh
,
6255 struct ocfs2_xattr_value_root
**xv
,
6256 struct buffer_head
**ret_bh
,
6259 struct ocfs2_xattr_entry
*xe
= &xh
->xh_entries
[offset
];
6261 *xv
= (struct ocfs2_xattr_value_root
*)((void *)xh
+
6262 le16_to_cpu(xe
->xe_name_offset
) +
6263 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
6272 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6273 * It is only used for inline xattr and xattr block.
6275 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super
*osb
,
6276 struct ocfs2_xattr_header
*xh
,
6277 struct buffer_head
*ref_root_bh
,
6279 struct ocfs2_alloc_context
**meta_ac
)
6281 int ret
, meta_add
= 0, num_recs
= 0;
6282 struct ocfs2_refcount_block
*rb
=
6283 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
6287 ret
= ocfs2_value_metas_in_xattr_header(osb
->sb
, NULL
, xh
,
6288 &meta_add
, credits
, &num_recs
,
6289 ocfs2_get_xattr_value_root
,
6297 * We need to add/modify num_recs in refcount tree, so just calculate
6298 * an approximate number we need for refcount tree change.
6299 * Sometimes we need to split the tree, and after split, half recs
6300 * will be moved to the new block, and a new block can only provide
6301 * half number of recs. So we multiple new blocks by 2.
6303 num_recs
= num_recs
/ ocfs2_refcount_recs_per_rb(osb
->sb
) * 2;
6304 meta_add
+= num_recs
;
6305 *credits
+= num_recs
+ num_recs
* OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
6306 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)
6307 *credits
+= le16_to_cpu(rb
->rf_list
.l_tree_depth
) *
6308 le16_to_cpu(rb
->rf_list
.l_next_free_rec
) + 1;
6312 ret
= ocfs2_reserve_new_metadata_blocks(osb
, meta_add
, meta_ac
);
6321 * Given a xattr header, reflink all the xattrs in this container.
6322 * It can be used for inode, block and bucket.
6325 * Before we call this function, the caller has memcpy the xattr in
6326 * old_xh to the new_xh.
6328 * If args.xattr_reflinked is set, call it to decide whether the xe should
6329 * be reflinked or not. If not, remove it from the new xattr header.
6331 static int ocfs2_reflink_xattr_header(handle_t
*handle
,
6332 struct ocfs2_xattr_reflink
*args
,
6333 struct buffer_head
*old_bh
,
6334 struct ocfs2_xattr_header
*xh
,
6335 struct buffer_head
*new_bh
,
6336 struct ocfs2_xattr_header
*new_xh
,
6337 struct ocfs2_xattr_value_buf
*vb
,
6338 struct ocfs2_alloc_context
*meta_ac
,
6339 get_xattr_value_root
*func
,
6343 struct super_block
*sb
= args
->old_inode
->i_sb
;
6344 struct buffer_head
*value_bh
;
6345 struct ocfs2_xattr_entry
*xe
, *last
;
6346 struct ocfs2_xattr_value_root
*xv
, *new_xv
;
6347 struct ocfs2_extent_tree data_et
;
6348 u32 clusters
, cpos
, p_cluster
, num_clusters
;
6349 unsigned int ext_flags
= 0;
6351 trace_ocfs2_reflink_xattr_header((unsigned long long)old_bh
->b_blocknr
,
6352 le16_to_cpu(xh
->xh_count
));
6354 last
= &new_xh
->xh_entries
[le16_to_cpu(new_xh
->xh_count
)];
6355 for (i
= 0, j
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, j
++) {
6356 xe
= &xh
->xh_entries
[i
];
6358 if (args
->xattr_reflinked
&& !args
->xattr_reflinked(xe
)) {
6359 xe
= &new_xh
->xh_entries
[j
];
6361 le16_add_cpu(&new_xh
->xh_count
, -1);
6362 if (new_xh
->xh_count
) {
6364 (void *)last
- (void *)xe
);
6366 sizeof(struct ocfs2_xattr_entry
));
6370 * We don't want j to increase in the next round since
6371 * it is already moved ahead.
6377 if (ocfs2_xattr_is_local(xe
))
6380 ret
= func(sb
, old_bh
, xh
, i
, &xv
, NULL
, para
);
6386 ret
= func(sb
, new_bh
, new_xh
, j
, &new_xv
, &value_bh
, para
);
6393 * For the xattr which has l_tree_depth = 0, all the extent
6394 * recs have already be copied to the new xh with the
6395 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6396 * increase the refount count int the refcount tree.
6398 * For the xattr which has l_tree_depth > 0, we need
6399 * to initialize it to the empty default value root,
6400 * and then insert the extents one by one.
6402 if (xv
->xr_list
.l_tree_depth
) {
6403 memcpy(new_xv
, &def_xv
, OCFS2_XATTR_ROOT_SIZE
);
6405 vb
->vb_bh
= value_bh
;
6406 ocfs2_init_xattr_value_extent_tree(&data_et
,
6407 INODE_CACHE(args
->new_inode
), vb
);
6410 clusters
= le32_to_cpu(xv
->xr_clusters
);
6412 while (cpos
< clusters
) {
6413 ret
= ocfs2_xattr_get_clusters(args
->old_inode
,
6426 if (xv
->xr_list
.l_tree_depth
) {
6427 ret
= ocfs2_insert_extent(handle
,
6429 ocfs2_clusters_to_blocks(
6430 args
->old_inode
->i_sb
,
6432 num_clusters
, ext_flags
,
6440 ret
= ocfs2_increase_refcount(handle
, args
->ref_ci
,
6442 p_cluster
, num_clusters
,
6443 meta_ac
, args
->dealloc
);
6449 cpos
+= num_clusters
;
6457 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink
*args
)
6459 int ret
= 0, credits
= 0;
6461 struct ocfs2_super
*osb
= OCFS2_SB(args
->old_inode
->i_sb
);
6462 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)args
->old_bh
->b_data
;
6463 int inline_size
= le16_to_cpu(di
->i_xattr_inline_size
);
6464 int header_off
= osb
->sb
->s_blocksize
- inline_size
;
6465 struct ocfs2_xattr_header
*xh
= (struct ocfs2_xattr_header
*)
6466 (args
->old_bh
->b_data
+ header_off
);
6467 struct ocfs2_xattr_header
*new_xh
= (struct ocfs2_xattr_header
*)
6468 (args
->new_bh
->b_data
+ header_off
);
6469 struct ocfs2_alloc_context
*meta_ac
= NULL
;
6470 struct ocfs2_inode_info
*new_oi
;
6471 struct ocfs2_dinode
*new_di
;
6472 struct ocfs2_xattr_value_buf vb
= {
6473 .vb_bh
= args
->new_bh
,
6474 .vb_access
= ocfs2_journal_access_di
,
6477 ret
= ocfs2_reflink_lock_xattr_allocators(osb
, xh
, args
->ref_root_bh
,
6478 &credits
, &meta_ac
);
6484 handle
= ocfs2_start_trans(osb
, credits
);
6485 if (IS_ERR(handle
)) {
6486 ret
= PTR_ERR(handle
);
6491 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(args
->new_inode
),
6492 args
->new_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
6498 memcpy(args
->new_bh
->b_data
+ header_off
,
6499 args
->old_bh
->b_data
+ header_off
, inline_size
);
6501 new_di
= (struct ocfs2_dinode
*)args
->new_bh
->b_data
;
6502 new_di
->i_xattr_inline_size
= cpu_to_le16(inline_size
);
6504 ret
= ocfs2_reflink_xattr_header(handle
, args
, args
->old_bh
, xh
,
6505 args
->new_bh
, new_xh
, &vb
, meta_ac
,
6506 ocfs2_get_xattr_value_root
, NULL
);
6512 new_oi
= OCFS2_I(args
->new_inode
);
6514 spin_lock(&new_oi
->ip_lock
);
6515 new_oi
->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
| OCFS2_INLINE_XATTR_FL
;
6516 new_di
->i_dyn_features
= cpu_to_le16(new_oi
->ip_dyn_features
);
6517 spin_unlock(&new_oi
->ip_lock
);
6519 ocfs2_journal_dirty(handle
, args
->new_bh
);
6522 ocfs2_commit_trans(osb
, handle
);
6526 ocfs2_free_alloc_context(meta_ac
);
6530 static int ocfs2_create_empty_xattr_block(struct inode
*inode
,
6531 struct buffer_head
*fe_bh
,
6532 struct buffer_head
**ret_bh
,
6536 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6537 struct ocfs2_xattr_set_ctxt ctxt
;
6539 memset(&ctxt
, 0, sizeof(ctxt
));
6540 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, &ctxt
.meta_ac
);
6546 ctxt
.handle
= ocfs2_start_trans(osb
, OCFS2_XATTR_BLOCK_CREATE_CREDITS
);
6547 if (IS_ERR(ctxt
.handle
)) {
6548 ret
= PTR_ERR(ctxt
.handle
);
6553 trace_ocfs2_create_empty_xattr_block(
6554 (unsigned long long)fe_bh
->b_blocknr
, indexed
);
6555 ret
= ocfs2_create_xattr_block(inode
, fe_bh
, &ctxt
, indexed
,
6560 ocfs2_commit_trans(osb
, ctxt
.handle
);
6562 ocfs2_free_alloc_context(ctxt
.meta_ac
);
6566 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink
*args
,
6567 struct buffer_head
*blk_bh
,
6568 struct buffer_head
*new_blk_bh
)
6570 int ret
= 0, credits
= 0;
6572 struct ocfs2_inode_info
*new_oi
= OCFS2_I(args
->new_inode
);
6573 struct ocfs2_dinode
*new_di
;
6574 struct ocfs2_super
*osb
= OCFS2_SB(args
->new_inode
->i_sb
);
6575 int header_off
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
6576 struct ocfs2_xattr_block
*xb
=
6577 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
6578 struct ocfs2_xattr_header
*xh
= &xb
->xb_attrs
.xb_header
;
6579 struct ocfs2_xattr_block
*new_xb
=
6580 (struct ocfs2_xattr_block
*)new_blk_bh
->b_data
;
6581 struct ocfs2_xattr_header
*new_xh
= &new_xb
->xb_attrs
.xb_header
;
6582 struct ocfs2_alloc_context
*meta_ac
;
6583 struct ocfs2_xattr_value_buf vb
= {
6584 .vb_bh
= new_blk_bh
,
6585 .vb_access
= ocfs2_journal_access_xb
,
6588 ret
= ocfs2_reflink_lock_xattr_allocators(osb
, xh
, args
->ref_root_bh
,
6589 &credits
, &meta_ac
);
6595 /* One more credits in case we need to add xattr flags in new inode. */
6596 handle
= ocfs2_start_trans(osb
, credits
+ 1);
6597 if (IS_ERR(handle
)) {
6598 ret
= PTR_ERR(handle
);
6603 if (!(new_oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
)) {
6604 ret
= ocfs2_journal_access_di(handle
,
6605 INODE_CACHE(args
->new_inode
),
6607 OCFS2_JOURNAL_ACCESS_WRITE
);
6614 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(args
->new_inode
),
6615 new_blk_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
6621 memcpy(new_blk_bh
->b_data
+ header_off
, blk_bh
->b_data
+ header_off
,
6622 osb
->sb
->s_blocksize
- header_off
);
6624 ret
= ocfs2_reflink_xattr_header(handle
, args
, blk_bh
, xh
,
6625 new_blk_bh
, new_xh
, &vb
, meta_ac
,
6626 ocfs2_get_xattr_value_root
, NULL
);
6632 ocfs2_journal_dirty(handle
, new_blk_bh
);
6634 if (!(new_oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
)) {
6635 new_di
= (struct ocfs2_dinode
*)args
->new_bh
->b_data
;
6636 spin_lock(&new_oi
->ip_lock
);
6637 new_oi
->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
;
6638 new_di
->i_dyn_features
= cpu_to_le16(new_oi
->ip_dyn_features
);
6639 spin_unlock(&new_oi
->ip_lock
);
6641 ocfs2_journal_dirty(handle
, args
->new_bh
);
6645 ocfs2_commit_trans(osb
, handle
);
6648 ocfs2_free_alloc_context(meta_ac
);
6652 struct ocfs2_reflink_xattr_tree_args
{
6653 struct ocfs2_xattr_reflink
*reflink
;
6654 struct buffer_head
*old_blk_bh
;
6655 struct buffer_head
*new_blk_bh
;
6656 struct ocfs2_xattr_bucket
*old_bucket
;
6657 struct ocfs2_xattr_bucket
*new_bucket
;
6662 * We have to handle the case that both old bucket and new bucket
6663 * will call this function to get the right ret_bh.
6664 * So The caller must give us the right bh.
6666 static int ocfs2_get_reflink_xattr_value_root(struct super_block
*sb
,
6667 struct buffer_head
*bh
,
6668 struct ocfs2_xattr_header
*xh
,
6670 struct ocfs2_xattr_value_root
**xv
,
6671 struct buffer_head
**ret_bh
,
6674 struct ocfs2_reflink_xattr_tree_args
*args
=
6675 (struct ocfs2_reflink_xattr_tree_args
*)para
;
6676 struct ocfs2_xattr_bucket
*bucket
;
6678 if (bh
== args
->old_bucket
->bu_bhs
[0])
6679 bucket
= args
->old_bucket
;
6681 bucket
= args
->new_bucket
;
6683 return ocfs2_get_xattr_tree_value_root(sb
, bucket
, offset
,
6687 struct ocfs2_value_tree_metas
{
6693 static int ocfs2_value_tree_metas_in_bucket(struct super_block
*sb
,
6694 struct buffer_head
*bh
,
6695 struct ocfs2_xattr_header
*xh
,
6697 struct ocfs2_xattr_value_root
**xv
,
6698 struct buffer_head
**ret_bh
,
6701 struct ocfs2_xattr_bucket
*bucket
=
6702 (struct ocfs2_xattr_bucket
*)para
;
6704 return ocfs2_get_xattr_tree_value_root(sb
, bucket
, offset
,
6708 static int ocfs2_calc_value_tree_metas(struct inode
*inode
,
6709 struct ocfs2_xattr_bucket
*bucket
,
6712 struct ocfs2_value_tree_metas
*metas
=
6713 (struct ocfs2_value_tree_metas
*)para
;
6714 struct ocfs2_xattr_header
*xh
=
6715 (struct ocfs2_xattr_header
*)bucket
->bu_bhs
[0]->b_data
;
6717 /* Add the credits for this bucket first. */
6718 metas
->credits
+= bucket
->bu_blocks
;
6719 return ocfs2_value_metas_in_xattr_header(inode
->i_sb
, bucket
->bu_bhs
[0],
6720 xh
, &metas
->num_metas
,
6721 &metas
->credits
, &metas
->num_recs
,
6722 ocfs2_value_tree_metas_in_bucket
,
6727 * Given a xattr extent rec starting from blkno and having len clusters,
6728 * iterate all the buckets calculate how much metadata we need for reflinking
6729 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6731 static int ocfs2_lock_reflink_xattr_rec_allocators(
6732 struct ocfs2_reflink_xattr_tree_args
*args
,
6733 struct ocfs2_extent_tree
*xt_et
,
6734 u64 blkno
, u32 len
, int *credits
,
6735 struct ocfs2_alloc_context
**meta_ac
,
6736 struct ocfs2_alloc_context
**data_ac
)
6738 int ret
, num_free_extents
;
6739 struct ocfs2_value_tree_metas metas
;
6740 struct ocfs2_super
*osb
= OCFS2_SB(args
->reflink
->old_inode
->i_sb
);
6741 struct ocfs2_refcount_block
*rb
;
6743 memset(&metas
, 0, sizeof(metas
));
6745 ret
= ocfs2_iterate_xattr_buckets(args
->reflink
->old_inode
, blkno
, len
,
6746 ocfs2_calc_value_tree_metas
, &metas
);
6752 *credits
= metas
.credits
;
6755 * Calculate we need for refcount tree change.
6757 * We need to add/modify num_recs in refcount tree, so just calculate
6758 * an approximate number we need for refcount tree change.
6759 * Sometimes we need to split the tree, and after split, half recs
6760 * will be moved to the new block, and a new block can only provide
6761 * half number of recs. So we multiple new blocks by 2.
6762 * In the end, we have to add credits for modifying the already
6763 * existed refcount block.
6765 rb
= (struct ocfs2_refcount_block
*)args
->reflink
->ref_root_bh
->b_data
;
6767 (metas
.num_recs
+ ocfs2_refcount_recs_per_rb(osb
->sb
) - 1) /
6768 ocfs2_refcount_recs_per_rb(osb
->sb
) * 2;
6769 metas
.num_metas
+= metas
.num_recs
;
6770 *credits
+= metas
.num_recs
+
6771 metas
.num_recs
* OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
6772 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)
6773 *credits
+= le16_to_cpu(rb
->rf_list
.l_tree_depth
) *
6774 le16_to_cpu(rb
->rf_list
.l_next_free_rec
) + 1;
6778 /* count in the xattr tree change. */
6779 num_free_extents
= ocfs2_num_free_extents(xt_et
);
6780 if (num_free_extents
< 0) {
6781 ret
= num_free_extents
;
6786 if (num_free_extents
< len
)
6787 metas
.num_metas
+= ocfs2_extend_meta_needed(xt_et
->et_root_el
);
6789 *credits
+= ocfs2_calc_extend_credits(osb
->sb
,
6792 if (metas
.num_metas
) {
6793 ret
= ocfs2_reserve_new_metadata_blocks(osb
, metas
.num_metas
,
6802 ret
= ocfs2_reserve_clusters(osb
, len
, data_ac
);
6809 ocfs2_free_alloc_context(*meta_ac
);
6817 static int ocfs2_reflink_xattr_bucket(handle_t
*handle
,
6818 u64 blkno
, u64 new_blkno
, u32 clusters
,
6819 u32
*cpos
, int num_buckets
,
6820 struct ocfs2_alloc_context
*meta_ac
,
6821 struct ocfs2_alloc_context
*data_ac
,
6822 struct ocfs2_reflink_xattr_tree_args
*args
)
6825 struct super_block
*sb
= args
->reflink
->old_inode
->i_sb
;
6826 int bpb
= args
->old_bucket
->bu_blocks
;
6827 struct ocfs2_xattr_value_buf vb
= {
6828 .vb_access
= ocfs2_journal_access
,
6831 for (i
= 0; i
< num_buckets
; i
++, blkno
+= bpb
, new_blkno
+= bpb
) {
6832 ret
= ocfs2_read_xattr_bucket(args
->old_bucket
, blkno
);
6838 ret
= ocfs2_init_xattr_bucket(args
->new_bucket
, new_blkno
, 1);
6844 ret
= ocfs2_xattr_bucket_journal_access(handle
,
6846 OCFS2_JOURNAL_ACCESS_CREATE
);
6852 for (j
= 0; j
< bpb
; j
++)
6853 memcpy(bucket_block(args
->new_bucket
, j
),
6854 bucket_block(args
->old_bucket
, j
),
6858 * Record the start cpos so that we can use it to initialize
6859 * our xattr tree we also set the xh_num_bucket for the new
6863 *cpos
= le32_to_cpu(bucket_xh(args
->new_bucket
)->
6864 xh_entries
[0].xe_name_hash
);
6865 bucket_xh(args
->new_bucket
)->xh_num_buckets
=
6866 cpu_to_le16(num_buckets
);
6869 ocfs2_xattr_bucket_journal_dirty(handle
, args
->new_bucket
);
6871 ret
= ocfs2_reflink_xattr_header(handle
, args
->reflink
,
6872 args
->old_bucket
->bu_bhs
[0],
6873 bucket_xh(args
->old_bucket
),
6874 args
->new_bucket
->bu_bhs
[0],
6875 bucket_xh(args
->new_bucket
),
6877 ocfs2_get_reflink_xattr_value_root
,
6885 * Re-access and dirty the bucket to calculate metaecc.
6886 * Because we may extend the transaction in reflink_xattr_header
6887 * which will let the already accessed block gone.
6889 ret
= ocfs2_xattr_bucket_journal_access(handle
,
6891 OCFS2_JOURNAL_ACCESS_WRITE
);
6897 ocfs2_xattr_bucket_journal_dirty(handle
, args
->new_bucket
);
6899 ocfs2_xattr_bucket_relse(args
->old_bucket
);
6900 ocfs2_xattr_bucket_relse(args
->new_bucket
);
6903 ocfs2_xattr_bucket_relse(args
->old_bucket
);
6904 ocfs2_xattr_bucket_relse(args
->new_bucket
);
6908 static int ocfs2_reflink_xattr_buckets(handle_t
*handle
,
6909 struct inode
*inode
,
6910 struct ocfs2_reflink_xattr_tree_args
*args
,
6911 struct ocfs2_extent_tree
*et
,
6912 struct ocfs2_alloc_context
*meta_ac
,
6913 struct ocfs2_alloc_context
*data_ac
,
6914 u64 blkno
, u32 cpos
, u32 len
)
6916 int ret
, first_inserted
= 0;
6917 u32 p_cluster
, num_clusters
, reflink_cpos
= 0;
6919 unsigned int num_buckets
, reflink_buckets
;
6921 ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
6923 ret
= ocfs2_read_xattr_bucket(args
->old_bucket
, blkno
);
6928 num_buckets
= le16_to_cpu(bucket_xh(args
->old_bucket
)->xh_num_buckets
);
6929 ocfs2_xattr_bucket_relse(args
->old_bucket
);
6931 while (len
&& num_buckets
) {
6932 ret
= ocfs2_claim_clusters(handle
, data_ac
,
6933 1, &p_cluster
, &num_clusters
);
6939 new_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
6940 reflink_buckets
= min(num_buckets
, bpc
* num_clusters
);
6942 ret
= ocfs2_reflink_xattr_bucket(handle
, blkno
,
6943 new_blkno
, num_clusters
,
6944 &reflink_cpos
, reflink_buckets
,
6945 meta_ac
, data_ac
, args
);
6952 * For the 1st allocated cluster, we make it use the same cpos
6953 * so that the xattr tree looks the same as the original one
6956 if (!first_inserted
) {
6957 reflink_cpos
= cpos
;
6960 ret
= ocfs2_insert_extent(handle
, et
, reflink_cpos
, new_blkno
,
6961 num_clusters
, 0, meta_ac
);
6965 trace_ocfs2_reflink_xattr_buckets((unsigned long long)new_blkno
,
6966 num_clusters
, reflink_cpos
);
6968 len
-= num_clusters
;
6969 blkno
+= ocfs2_clusters_to_blocks(inode
->i_sb
, num_clusters
);
6970 num_buckets
-= reflink_buckets
;
6977 * Create the same xattr extent record in the new inode's xattr tree.
6979 static int ocfs2_reflink_xattr_rec(struct inode
*inode
,
6980 struct buffer_head
*root_bh
,
6986 int ret
, credits
= 0;
6988 struct ocfs2_reflink_xattr_tree_args
*args
=
6989 (struct ocfs2_reflink_xattr_tree_args
*)para
;
6990 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6991 struct ocfs2_alloc_context
*meta_ac
= NULL
;
6992 struct ocfs2_alloc_context
*data_ac
= NULL
;
6993 struct ocfs2_extent_tree et
;
6995 trace_ocfs2_reflink_xattr_rec((unsigned long long)blkno
, len
);
6997 ocfs2_init_xattr_tree_extent_tree(&et
,
6998 INODE_CACHE(args
->reflink
->new_inode
),
7001 ret
= ocfs2_lock_reflink_xattr_rec_allocators(args
, &et
, blkno
,
7003 &meta_ac
, &data_ac
);
7009 handle
= ocfs2_start_trans(osb
, credits
);
7010 if (IS_ERR(handle
)) {
7011 ret
= PTR_ERR(handle
);
7016 ret
= ocfs2_reflink_xattr_buckets(handle
, inode
, args
, &et
,
7022 ocfs2_commit_trans(osb
, handle
);
7026 ocfs2_free_alloc_context(meta_ac
);
7028 ocfs2_free_alloc_context(data_ac
);
7033 * Create reflinked xattr buckets.
7034 * We will add bucket one by one, and refcount all the xattrs in the bucket
7035 * if they are stored outside.
7037 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink
*args
,
7038 struct buffer_head
*blk_bh
,
7039 struct buffer_head
*new_blk_bh
)
7042 struct ocfs2_reflink_xattr_tree_args para
;
7044 memset(¶
, 0, sizeof(para
));
7045 para
.reflink
= args
;
7046 para
.old_blk_bh
= blk_bh
;
7047 para
.new_blk_bh
= new_blk_bh
;
7049 para
.old_bucket
= ocfs2_xattr_bucket_new(args
->old_inode
);
7050 if (!para
.old_bucket
) {
7051 mlog_errno(-ENOMEM
);
7055 para
.new_bucket
= ocfs2_xattr_bucket_new(args
->new_inode
);
7056 if (!para
.new_bucket
) {
7062 ret
= ocfs2_iterate_xattr_index_block(args
->old_inode
, blk_bh
,
7063 ocfs2_reflink_xattr_rec
,
7069 ocfs2_xattr_bucket_free(para
.old_bucket
);
7070 ocfs2_xattr_bucket_free(para
.new_bucket
);
7074 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink
*args
,
7075 struct buffer_head
*blk_bh
)
7077 int ret
, indexed
= 0;
7078 struct buffer_head
*new_blk_bh
= NULL
;
7079 struct ocfs2_xattr_block
*xb
=
7080 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
7083 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)
7086 ret
= ocfs2_create_empty_xattr_block(args
->new_inode
, args
->new_bh
,
7087 &new_blk_bh
, indexed
);
7094 ret
= ocfs2_reflink_xattr_block(args
, blk_bh
, new_blk_bh
);
7096 ret
= ocfs2_reflink_xattr_tree(args
, blk_bh
, new_blk_bh
);
7105 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry
*xe
)
7107 int type
= ocfs2_xattr_get_type(xe
);
7109 return type
!= OCFS2_XATTR_INDEX_SECURITY
&&
7110 type
!= OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
&&
7111 type
!= OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
;
7114 int ocfs2_reflink_xattrs(struct inode
*old_inode
,
7115 struct buffer_head
*old_bh
,
7116 struct inode
*new_inode
,
7117 struct buffer_head
*new_bh
,
7118 bool preserve_security
)
7121 struct ocfs2_xattr_reflink args
;
7122 struct ocfs2_inode_info
*oi
= OCFS2_I(old_inode
);
7123 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)old_bh
->b_data
;
7124 struct buffer_head
*blk_bh
= NULL
;
7125 struct ocfs2_cached_dealloc_ctxt dealloc
;
7126 struct ocfs2_refcount_tree
*ref_tree
;
7127 struct buffer_head
*ref_root_bh
= NULL
;
7129 ret
= ocfs2_lock_refcount_tree(OCFS2_SB(old_inode
->i_sb
),
7130 le64_to_cpu(di
->i_refcount_loc
),
7131 1, &ref_tree
, &ref_root_bh
);
7137 ocfs2_init_dealloc_ctxt(&dealloc
);
7139 args
.old_inode
= old_inode
;
7140 args
.new_inode
= new_inode
;
7141 args
.old_bh
= old_bh
;
7142 args
.new_bh
= new_bh
;
7143 args
.ref_ci
= &ref_tree
->rf_ci
;
7144 args
.ref_root_bh
= ref_root_bh
;
7145 args
.dealloc
= &dealloc
;
7146 if (preserve_security
)
7147 args
.xattr_reflinked
= NULL
;
7149 args
.xattr_reflinked
= ocfs2_reflink_xattr_no_security
;
7151 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
7152 ret
= ocfs2_reflink_xattr_inline(&args
);
7159 if (!di
->i_xattr_loc
)
7162 ret
= ocfs2_read_xattr_block(old_inode
, le64_to_cpu(di
->i_xattr_loc
),
7169 ret
= ocfs2_reflink_xattr_in_block(&args
, blk_bh
);
7176 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode
->i_sb
),
7178 brelse(ref_root_bh
);
7180 if (ocfs2_dealloc_has_cluster(&dealloc
)) {
7181 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode
->i_sb
), 1);
7182 ocfs2_run_deallocs(OCFS2_SB(old_inode
->i_sb
), &dealloc
);
7190 * Initialize security and acl for a already created inode.
7191 * Used for reflink a non-preserve-security file.
7193 * It uses common api like ocfs2_xattr_set, so the caller
7194 * must not hold any lock expect i_rwsem.
7196 int ocfs2_init_security_and_acl(struct inode
*dir
,
7197 struct inode
*inode
,
7198 const struct qstr
*qstr
)
7201 struct buffer_head
*dir_bh
= NULL
;
7203 ret
= ocfs2_init_security_get(inode
, dir
, qstr
, NULL
);
7209 ret
= ocfs2_inode_lock(dir
, &dir_bh
, 0);
7214 ret
= ocfs2_init_acl(NULL
, inode
, dir
, NULL
, dir_bh
, NULL
, NULL
);
7218 ocfs2_inode_unlock(dir
, 0);
7225 * 'security' attributes support
7227 static int ocfs2_xattr_security_get(const struct xattr_handler
*handler
,
7228 struct dentry
*unused
, struct inode
*inode
,
7229 const char *name
, void *buffer
, size_t size
)
7231 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_SECURITY
,
7232 name
, buffer
, size
);
7235 static int ocfs2_xattr_security_set(const struct xattr_handler
*handler
,
7236 struct mnt_idmap
*idmap
,
7237 struct dentry
*unused
, struct inode
*inode
,
7238 const char *name
, const void *value
,
7239 size_t size
, int flags
)
7241 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_SECURITY
,
7242 name
, value
, size
, flags
);
7245 static int ocfs2_initxattrs(struct inode
*inode
, const struct xattr
*xattr_array
,
7248 struct ocfs2_security_xattr_info
*si
= fs_info
;
7249 const struct xattr
*xattr
;
7253 si
->value
= kmemdup(xattr_array
->value
, xattr_array
->value_len
,
7258 si
->name
= xattr_array
->name
;
7259 si
->value_len
= xattr_array
->value_len
;
7263 for (xattr
= xattr_array
; xattr
->name
!= NULL
; xattr
++) {
7264 err
= ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_SECURITY
,
7265 xattr
->name
, xattr
->value
,
7266 xattr
->value_len
, XATTR_CREATE
);
7273 int ocfs2_init_security_get(struct inode
*inode
,
7275 const struct qstr
*qstr
,
7276 struct ocfs2_security_xattr_info
*si
)
7280 /* check whether ocfs2 support feature xattr */
7281 if (!ocfs2_supports_xattr(OCFS2_SB(dir
->i_sb
)))
7284 ret
= security_inode_init_security(inode
, dir
, qstr
,
7285 &ocfs2_initxattrs
, si
);
7287 * security_inode_init_security() does not return -EOPNOTSUPP,
7288 * we have to check the xattr ourselves.
7290 if (!ret
&& !si
->name
)
7296 return security_inode_init_security(inode
, dir
, qstr
,
7297 &ocfs2_initxattrs
, NULL
);
7300 int ocfs2_init_security_set(handle_t
*handle
,
7301 struct inode
*inode
,
7302 struct buffer_head
*di_bh
,
7303 struct ocfs2_security_xattr_info
*si
,
7304 struct ocfs2_alloc_context
*xattr_ac
,
7305 struct ocfs2_alloc_context
*data_ac
)
7307 return ocfs2_xattr_set_handle(handle
, inode
, di_bh
,
7308 OCFS2_XATTR_INDEX_SECURITY
,
7309 si
->name
, si
->value
, si
->value_len
, 0,
7313 const struct xattr_handler ocfs2_xattr_security_handler
= {
7314 .prefix
= XATTR_SECURITY_PREFIX
,
7315 .get
= ocfs2_xattr_security_get
,
7316 .set
= ocfs2_xattr_security_set
,
7320 * 'trusted' attributes support
7322 static int ocfs2_xattr_trusted_get(const struct xattr_handler
*handler
,
7323 struct dentry
*unused
, struct inode
*inode
,
7324 const char *name
, void *buffer
, size_t size
)
7326 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_TRUSTED
,
7327 name
, buffer
, size
);
7330 static int ocfs2_xattr_trusted_set(const struct xattr_handler
*handler
,
7331 struct mnt_idmap
*idmap
,
7332 struct dentry
*unused
, struct inode
*inode
,
7333 const char *name
, const void *value
,
7334 size_t size
, int flags
)
7336 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_TRUSTED
,
7337 name
, value
, size
, flags
);
7340 const struct xattr_handler ocfs2_xattr_trusted_handler
= {
7341 .prefix
= XATTR_TRUSTED_PREFIX
,
7342 .get
= ocfs2_xattr_trusted_get
,
7343 .set
= ocfs2_xattr_trusted_set
,
7347 * 'user' attributes support
7349 static int ocfs2_xattr_user_get(const struct xattr_handler
*handler
,
7350 struct dentry
*unused
, struct inode
*inode
,
7351 const char *name
, void *buffer
, size_t size
)
7353 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7355 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7357 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_USER
, name
,
7361 static int ocfs2_xattr_user_set(const struct xattr_handler
*handler
,
7362 struct mnt_idmap
*idmap
,
7363 struct dentry
*unused
, struct inode
*inode
,
7364 const char *name
, const void *value
,
7365 size_t size
, int flags
)
7367 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7369 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7372 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_USER
,
7373 name
, value
, size
, flags
);
7376 const struct xattr_handler ocfs2_xattr_user_handler
= {
7377 .prefix
= XATTR_USER_PREFIX
,
7378 .get
= ocfs2_xattr_user_get
,
7379 .set
= ocfs2_xattr_user_set
,