1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
9 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/security.h>
40 #define MLOG_MASK_PREFIX ML_XATTR
41 #include <cluster/masklog.h>
45 #include "blockcheck.h"
55 #include "buffer_head_io.h"
58 #include "refcounttree.h"
61 struct ocfs2_xattr_def_value_root
{
62 struct ocfs2_xattr_value_root xv
;
63 struct ocfs2_extent_rec er
;
66 struct ocfs2_xattr_bucket
{
67 /* The inode these xattrs are associated with */
68 struct inode
*bu_inode
;
70 /* The actual buffers that make up the bucket */
71 struct buffer_head
*bu_bhs
[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
];
73 /* How many blocks make up one bucket for this filesystem */
77 struct ocfs2_xattr_set_ctxt
{
79 struct ocfs2_alloc_context
*meta_ac
;
80 struct ocfs2_alloc_context
*data_ac
;
81 struct ocfs2_cached_dealloc_ctxt dealloc
;
85 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
86 #define OCFS2_XATTR_INLINE_SIZE 80
87 #define OCFS2_XATTR_HEADER_GAP 4
88 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
89 - sizeof(struct ocfs2_xattr_header) \
90 - OCFS2_XATTR_HEADER_GAP)
91 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
92 - sizeof(struct ocfs2_xattr_block) \
93 - sizeof(struct ocfs2_xattr_header) \
94 - OCFS2_XATTR_HEADER_GAP)
96 static struct ocfs2_xattr_def_value_root def_xv
= {
97 .xv
.xr_list
.l_count
= cpu_to_le16(1),
100 const struct xattr_handler
*ocfs2_xattr_handlers
[] = {
101 &ocfs2_xattr_user_handler
,
102 &ocfs2_xattr_acl_access_handler
,
103 &ocfs2_xattr_acl_default_handler
,
104 &ocfs2_xattr_trusted_handler
,
105 &ocfs2_xattr_security_handler
,
109 static const struct xattr_handler
*ocfs2_xattr_handler_map
[OCFS2_XATTR_MAX
] = {
110 [OCFS2_XATTR_INDEX_USER
] = &ocfs2_xattr_user_handler
,
111 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
]
112 = &ocfs2_xattr_acl_access_handler
,
113 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
]
114 = &ocfs2_xattr_acl_default_handler
,
115 [OCFS2_XATTR_INDEX_TRUSTED
] = &ocfs2_xattr_trusted_handler
,
116 [OCFS2_XATTR_INDEX_SECURITY
] = &ocfs2_xattr_security_handler
,
119 struct ocfs2_xattr_info
{
123 const void *xi_value
;
127 struct ocfs2_xattr_search
{
128 struct buffer_head
*inode_bh
;
130 * xattr_bh point to the block buffer head which has extended attribute
131 * when extended attribute in inode, xattr_bh is equal to inode_bh.
133 struct buffer_head
*xattr_bh
;
134 struct ocfs2_xattr_header
*header
;
135 struct ocfs2_xattr_bucket
*bucket
;
138 struct ocfs2_xattr_entry
*here
;
142 /* Operations on struct ocfs2_xa_entry */
144 struct ocfs2_xa_loc_operations
{
148 int (*xlo_journal_access
)(handle_t
*handle
, struct ocfs2_xa_loc
*loc
,
150 void (*xlo_journal_dirty
)(handle_t
*handle
, struct ocfs2_xa_loc
*loc
);
153 * Return a pointer to the appropriate buffer in loc->xl_storage
154 * at the given offset from loc->xl_header.
156 void *(*xlo_offset_pointer
)(struct ocfs2_xa_loc
*loc
, int offset
);
158 /* Can we reuse the existing entry for the new value? */
159 int (*xlo_can_reuse
)(struct ocfs2_xa_loc
*loc
,
160 struct ocfs2_xattr_info
*xi
);
162 /* How much space is needed for the new value? */
163 int (*xlo_check_space
)(struct ocfs2_xa_loc
*loc
,
164 struct ocfs2_xattr_info
*xi
);
167 * Return the offset of the first name+value pair. This is
168 * the start of our downward-filling free space.
170 int (*xlo_get_free_start
)(struct ocfs2_xa_loc
*loc
);
173 * Remove the name+value at this location. Do whatever is
174 * appropriate with the remaining name+value pairs.
176 void (*xlo_wipe_namevalue
)(struct ocfs2_xa_loc
*loc
);
178 /* Fill xl_entry with a new entry */
179 void (*xlo_add_entry
)(struct ocfs2_xa_loc
*loc
, u32 name_hash
);
181 /* Add name+value storage to an entry */
182 void (*xlo_add_namevalue
)(struct ocfs2_xa_loc
*loc
, int size
);
185 * Initialize the value buf's access and bh fields for this entry.
186 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
188 void (*xlo_fill_value_buf
)(struct ocfs2_xa_loc
*loc
,
189 struct ocfs2_xattr_value_buf
*vb
);
193 * Describes an xattr entry location. This is a memory structure
194 * tracking the on-disk structure.
196 struct ocfs2_xa_loc
{
197 /* This xattr belongs to this inode */
198 struct inode
*xl_inode
;
200 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
201 struct ocfs2_xattr_header
*xl_header
;
203 /* Bytes from xl_header to the end of the storage */
207 * The ocfs2_xattr_entry this location describes. If this is
208 * NULL, this location describes the on-disk structure where it
211 struct ocfs2_xattr_entry
*xl_entry
;
214 * Internal housekeeping
217 /* Buffer(s) containing this entry */
220 /* Operations on the storage backing this location */
221 const struct ocfs2_xa_loc_operations
*xl_ops
;
225 * Convenience functions to calculate how much space is needed for a
226 * given name+value pair
228 static int namevalue_size(int name_len
, uint64_t value_len
)
230 if (value_len
> OCFS2_XATTR_INLINE_SIZE
)
231 return OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
233 return OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_SIZE(value_len
);
236 static int namevalue_size_xi(struct ocfs2_xattr_info
*xi
)
238 return namevalue_size(xi
->xi_name_len
, xi
->xi_value_len
);
241 static int namevalue_size_xe(struct ocfs2_xattr_entry
*xe
)
243 u64 value_len
= le64_to_cpu(xe
->xe_value_size
);
245 BUG_ON((value_len
> OCFS2_XATTR_INLINE_SIZE
) &&
246 ocfs2_xattr_is_local(xe
));
247 return namevalue_size(xe
->xe_name_len
, value_len
);
251 static int ocfs2_xattr_bucket_get_name_value(struct super_block
*sb
,
252 struct ocfs2_xattr_header
*xh
,
257 static int ocfs2_xattr_block_find(struct inode
*inode
,
260 struct ocfs2_xattr_search
*xs
);
261 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
262 struct buffer_head
*root_bh
,
265 struct ocfs2_xattr_search
*xs
);
267 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
268 struct buffer_head
*blk_bh
,
272 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
273 struct ocfs2_xattr_search
*xs
,
274 struct ocfs2_xattr_set_ctxt
*ctxt
);
276 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
277 struct ocfs2_xattr_info
*xi
,
278 struct ocfs2_xattr_search
*xs
,
279 struct ocfs2_xattr_set_ctxt
*ctxt
);
281 typedef int (xattr_tree_rec_func
)(struct inode
*inode
,
282 struct buffer_head
*root_bh
,
283 u64 blkno
, u32 cpos
, u32 len
, void *para
);
284 static int ocfs2_iterate_xattr_index_block(struct inode
*inode
,
285 struct buffer_head
*root_bh
,
286 xattr_tree_rec_func
*rec_func
,
288 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
289 struct ocfs2_xattr_bucket
*bucket
,
291 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
292 struct buffer_head
*root_bh
,
298 static int ocfs2_mv_xattr_buckets(struct inode
*inode
, handle_t
*handle
,
299 u64 src_blk
, u64 last_blk
, u64 to_blk
,
300 unsigned int start_bucket
,
302 static int ocfs2_prepare_refcount_xattr(struct inode
*inode
,
303 struct ocfs2_dinode
*di
,
304 struct ocfs2_xattr_info
*xi
,
305 struct ocfs2_xattr_search
*xis
,
306 struct ocfs2_xattr_search
*xbs
,
307 struct ocfs2_refcount_tree
**ref_tree
,
310 static int ocfs2_get_xattr_tree_value_root(struct super_block
*sb
,
311 struct ocfs2_xattr_bucket
*bucket
,
313 struct ocfs2_xattr_value_root
**xv
,
314 struct buffer_head
**bh
);
316 static inline u16
ocfs2_xattr_buckets_per_cluster(struct ocfs2_super
*osb
)
318 return (1 << osb
->s_clustersize_bits
) / OCFS2_XATTR_BUCKET_SIZE
;
321 static inline u16
ocfs2_blocks_per_xattr_bucket(struct super_block
*sb
)
323 return OCFS2_XATTR_BUCKET_SIZE
/ (1 << sb
->s_blocksize_bits
);
326 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
327 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
328 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
330 static struct ocfs2_xattr_bucket
*ocfs2_xattr_bucket_new(struct inode
*inode
)
332 struct ocfs2_xattr_bucket
*bucket
;
333 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
335 BUG_ON(blks
> OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
);
337 bucket
= kzalloc(sizeof(struct ocfs2_xattr_bucket
), GFP_NOFS
);
339 bucket
->bu_inode
= inode
;
340 bucket
->bu_blocks
= blks
;
346 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket
*bucket
)
350 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
351 brelse(bucket
->bu_bhs
[i
]);
352 bucket
->bu_bhs
[i
] = NULL
;
356 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket
*bucket
)
359 ocfs2_xattr_bucket_relse(bucket
);
360 bucket
->bu_inode
= NULL
;
366 * A bucket that has never been written to disk doesn't need to be
367 * read. We just need the buffer_heads. Don't call this for
368 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
371 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
376 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
377 bucket
->bu_bhs
[i
] = sb_getblk(bucket
->bu_inode
->i_sb
,
379 if (!bucket
->bu_bhs
[i
]) {
385 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket
->bu_inode
),
387 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket
->bu_inode
),
392 ocfs2_xattr_bucket_relse(bucket
);
396 /* Read the xattr bucket at xb_blkno */
397 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
402 rc
= ocfs2_read_blocks(INODE_CACHE(bucket
->bu_inode
), xb_blkno
,
403 bucket
->bu_blocks
, bucket
->bu_bhs
, 0,
406 spin_lock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
407 rc
= ocfs2_validate_meta_ecc_bhs(bucket
->bu_inode
->i_sb
,
410 &bucket_xh(bucket
)->xh_check
);
411 spin_unlock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
417 ocfs2_xattr_bucket_relse(bucket
);
421 static int ocfs2_xattr_bucket_journal_access(handle_t
*handle
,
422 struct ocfs2_xattr_bucket
*bucket
,
427 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
428 rc
= ocfs2_journal_access(handle
,
429 INODE_CACHE(bucket
->bu_inode
),
430 bucket
->bu_bhs
[i
], type
);
440 static void ocfs2_xattr_bucket_journal_dirty(handle_t
*handle
,
441 struct ocfs2_xattr_bucket
*bucket
)
445 spin_lock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
446 ocfs2_compute_meta_ecc_bhs(bucket
->bu_inode
->i_sb
,
447 bucket
->bu_bhs
, bucket
->bu_blocks
,
448 &bucket_xh(bucket
)->xh_check
);
449 spin_unlock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
451 for (i
= 0; i
< bucket
->bu_blocks
; i
++)
452 ocfs2_journal_dirty(handle
, bucket
->bu_bhs
[i
]);
455 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket
*dest
,
456 struct ocfs2_xattr_bucket
*src
)
459 int blocksize
= src
->bu_inode
->i_sb
->s_blocksize
;
461 BUG_ON(dest
->bu_blocks
!= src
->bu_blocks
);
462 BUG_ON(dest
->bu_inode
!= src
->bu_inode
);
464 for (i
= 0; i
< src
->bu_blocks
; i
++) {
465 memcpy(bucket_block(dest
, i
), bucket_block(src
, i
),
470 static int ocfs2_validate_xattr_block(struct super_block
*sb
,
471 struct buffer_head
*bh
)
474 struct ocfs2_xattr_block
*xb
=
475 (struct ocfs2_xattr_block
*)bh
->b_data
;
477 mlog(0, "Validating xattr block %llu\n",
478 (unsigned long long)bh
->b_blocknr
);
480 BUG_ON(!buffer_uptodate(bh
));
483 * If the ecc fails, we return the error but otherwise
484 * leave the filesystem running. We know any error is
485 * local to this block.
487 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &xb
->xb_check
);
492 * Errors after here are fatal
495 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
497 "Extended attribute block #%llu has bad "
499 (unsigned long long)bh
->b_blocknr
, 7,
504 if (le64_to_cpu(xb
->xb_blkno
) != bh
->b_blocknr
) {
506 "Extended attribute block #%llu has an "
507 "invalid xb_blkno of %llu",
508 (unsigned long long)bh
->b_blocknr
,
509 (unsigned long long)le64_to_cpu(xb
->xb_blkno
));
513 if (le32_to_cpu(xb
->xb_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
515 "Extended attribute block #%llu has an invalid "
516 "xb_fs_generation of #%u",
517 (unsigned long long)bh
->b_blocknr
,
518 le32_to_cpu(xb
->xb_fs_generation
));
525 static int ocfs2_read_xattr_block(struct inode
*inode
, u64 xb_blkno
,
526 struct buffer_head
**bh
)
529 struct buffer_head
*tmp
= *bh
;
531 rc
= ocfs2_read_block(INODE_CACHE(inode
), xb_blkno
, &tmp
,
532 ocfs2_validate_xattr_block
);
534 /* If ocfs2_read_block() got us a new bh, pass it up. */
541 static inline const char *ocfs2_xattr_prefix(int name_index
)
543 const struct xattr_handler
*handler
= NULL
;
545 if (name_index
> 0 && name_index
< OCFS2_XATTR_MAX
)
546 handler
= ocfs2_xattr_handler_map
[name_index
];
548 return handler
? handler
->prefix
: NULL
;
551 static u32
ocfs2_xattr_name_hash(struct inode
*inode
,
555 /* Get hash value of uuid from super block */
556 u32 hash
= OCFS2_SB(inode
->i_sb
)->uuid_hash
;
559 /* hash extended attribute name */
560 for (i
= 0; i
< name_len
; i
++) {
561 hash
= (hash
<< OCFS2_HASH_SHIFT
) ^
562 (hash
>> (8*sizeof(hash
) - OCFS2_HASH_SHIFT
)) ^
569 static int ocfs2_xattr_entry_real_size(int name_len
, size_t value_len
)
571 return namevalue_size(name_len
, value_len
) +
572 sizeof(struct ocfs2_xattr_entry
);
575 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info
*xi
)
577 return namevalue_size_xi(xi
) +
578 sizeof(struct ocfs2_xattr_entry
);
581 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry
*xe
)
583 return namevalue_size_xe(xe
) +
584 sizeof(struct ocfs2_xattr_entry
);
587 int ocfs2_calc_security_init(struct inode
*dir
,
588 struct ocfs2_security_xattr_info
*si
,
591 struct ocfs2_alloc_context
**xattr_ac
)
594 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
595 int s_size
= ocfs2_xattr_entry_real_size(strlen(si
->name
),
599 * The max space of security xattr taken inline is
600 * 256(name) + 80(value) + 16(entry) = 352 bytes,
601 * So reserve one metadata block for it is ok.
603 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
||
604 s_size
> OCFS2_XATTR_FREE_IN_IBODY
) {
605 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, xattr_ac
);
610 *xattr_credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
613 /* reserve clusters for xattr value which will be set in B tree*/
614 if (si
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
615 int new_clusters
= ocfs2_clusters_for_bytes(dir
->i_sb
,
618 *xattr_credits
+= ocfs2_clusters_to_blocks(dir
->i_sb
,
620 *want_clusters
+= new_clusters
;
625 int ocfs2_calc_xattr_init(struct inode
*dir
,
626 struct buffer_head
*dir_bh
,
628 struct ocfs2_security_xattr_info
*si
,
634 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
635 int s_size
= 0, a_size
= 0, acl_len
= 0, new_clusters
;
638 s_size
= ocfs2_xattr_entry_real_size(strlen(si
->name
),
641 if (osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
) {
642 acl_len
= ocfs2_xattr_get_nolock(dir
, dir_bh
,
643 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
,
646 a_size
= ocfs2_xattr_entry_real_size(0, acl_len
);
649 } else if (acl_len
!= 0 && acl_len
!= -ENODATA
) {
655 if (!(s_size
+ a_size
))
659 * The max space of security xattr taken inline is
660 * 256(name) + 80(value) + 16(entry) = 352 bytes,
661 * The max space of acl xattr taken inline is
662 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
663 * when blocksize = 512, may reserve one more cluser for
664 * xattr bucket, otherwise reserve one metadata block
666 * If this is a new directory with inline data,
667 * we choose to reserve the entire inline area for
668 * directory contents and force an external xattr block.
670 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
||
671 (S_ISDIR(mode
) && ocfs2_supports_inline_data(osb
)) ||
672 (s_size
+ a_size
) > OCFS2_XATTR_FREE_IN_IBODY
) {
673 *want_meta
= *want_meta
+ 1;
674 *xattr_credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
677 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
&&
678 (s_size
+ a_size
) > OCFS2_XATTR_FREE_IN_BLOCK(dir
)) {
680 *xattr_credits
+= ocfs2_blocks_per_xattr_bucket(dir
->i_sb
);
684 * reserve credits and clusters for xattrs which has large value
685 * and have to be set outside
687 if (si
->enable
&& si
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
688 new_clusters
= ocfs2_clusters_for_bytes(dir
->i_sb
,
690 *xattr_credits
+= ocfs2_clusters_to_blocks(dir
->i_sb
,
692 *want_clusters
+= new_clusters
;
694 if (osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
&&
695 acl_len
> OCFS2_XATTR_INLINE_SIZE
) {
696 /* for directory, it has DEFAULT and ACCESS two types of acls */
697 new_clusters
= (S_ISDIR(mode
) ? 2 : 1) *
698 ocfs2_clusters_for_bytes(dir
->i_sb
, acl_len
);
699 *xattr_credits
+= ocfs2_clusters_to_blocks(dir
->i_sb
,
701 *want_clusters
+= new_clusters
;
707 static int ocfs2_xattr_extend_allocation(struct inode
*inode
,
709 struct ocfs2_xattr_value_buf
*vb
,
710 struct ocfs2_xattr_set_ctxt
*ctxt
)
712 int status
= 0, credits
;
713 handle_t
*handle
= ctxt
->handle
;
714 enum ocfs2_alloc_restarted why
;
715 u32 prev_clusters
, logical_start
= le32_to_cpu(vb
->vb_xv
->xr_clusters
);
716 struct ocfs2_extent_tree et
;
718 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add
);
720 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
722 while (clusters_to_add
) {
723 status
= vb
->vb_access(handle
, INODE_CACHE(inode
), vb
->vb_bh
,
724 OCFS2_JOURNAL_ACCESS_WRITE
);
730 prev_clusters
= le32_to_cpu(vb
->vb_xv
->xr_clusters
);
731 status
= ocfs2_add_clusters_in_btree(handle
,
739 if ((status
< 0) && (status
!= -EAGAIN
)) {
740 if (status
!= -ENOSPC
)
745 ocfs2_journal_dirty(handle
, vb
->vb_bh
);
747 clusters_to_add
-= le32_to_cpu(vb
->vb_xv
->xr_clusters
) -
750 if (why
!= RESTART_NONE
&& clusters_to_add
) {
752 * We can only fail in case the alloc file doesn't give
753 * up enough clusters.
755 BUG_ON(why
== RESTART_META
);
757 mlog(0, "restarting xattr value extension for %u"
758 " clusters,.\n", clusters_to_add
);
759 credits
= ocfs2_calc_extend_credits(inode
->i_sb
,
762 status
= ocfs2_extend_trans(handle
, credits
);
774 static int __ocfs2_remove_xattr_range(struct inode
*inode
,
775 struct ocfs2_xattr_value_buf
*vb
,
776 u32 cpos
, u32 phys_cpos
, u32 len
,
777 unsigned int ext_flags
,
778 struct ocfs2_xattr_set_ctxt
*ctxt
)
781 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
782 handle_t
*handle
= ctxt
->handle
;
783 struct ocfs2_extent_tree et
;
785 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
787 ret
= vb
->vb_access(handle
, INODE_CACHE(inode
), vb
->vb_bh
,
788 OCFS2_JOURNAL_ACCESS_WRITE
);
794 ret
= ocfs2_remove_extent(handle
, &et
, cpos
, len
, ctxt
->meta_ac
,
801 le32_add_cpu(&vb
->vb_xv
->xr_clusters
, -len
);
802 ocfs2_journal_dirty(handle
, vb
->vb_bh
);
804 if (ext_flags
& OCFS2_EXT_REFCOUNTED
)
805 ret
= ocfs2_decrease_refcount(inode
, handle
,
806 ocfs2_blocks_to_clusters(inode
->i_sb
,
808 len
, ctxt
->meta_ac
, &ctxt
->dealloc
, 1);
810 ret
= ocfs2_cache_cluster_dealloc(&ctxt
->dealloc
,
819 static int ocfs2_xattr_shrink_size(struct inode
*inode
,
822 struct ocfs2_xattr_value_buf
*vb
,
823 struct ocfs2_xattr_set_ctxt
*ctxt
)
826 unsigned int ext_flags
;
827 u32 trunc_len
, cpos
, phys_cpos
, alloc_size
;
830 if (old_clusters
<= new_clusters
)
834 trunc_len
= old_clusters
- new_clusters
;
836 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &phys_cpos
,
838 &vb
->vb_xv
->xr_list
, &ext_flags
);
844 if (alloc_size
> trunc_len
)
845 alloc_size
= trunc_len
;
847 ret
= __ocfs2_remove_xattr_range(inode
, vb
, cpos
,
848 phys_cpos
, alloc_size
,
855 block
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
856 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode
),
859 trunc_len
-= alloc_size
;
866 static int ocfs2_xattr_value_truncate(struct inode
*inode
,
867 struct ocfs2_xattr_value_buf
*vb
,
869 struct ocfs2_xattr_set_ctxt
*ctxt
)
872 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, len
);
873 u32 old_clusters
= le32_to_cpu(vb
->vb_xv
->xr_clusters
);
875 if (new_clusters
== old_clusters
)
878 if (new_clusters
> old_clusters
)
879 ret
= ocfs2_xattr_extend_allocation(inode
,
880 new_clusters
- old_clusters
,
883 ret
= ocfs2_xattr_shrink_size(inode
,
884 old_clusters
, new_clusters
,
890 static int ocfs2_xattr_list_entry(char *buffer
, size_t size
,
891 size_t *result
, const char *prefix
,
892 const char *name
, int name_len
)
894 char *p
= buffer
+ *result
;
895 int prefix_len
= strlen(prefix
);
896 int total_len
= prefix_len
+ name_len
+ 1;
898 *result
+= total_len
;
900 /* we are just looking for how big our buffer needs to be */
907 memcpy(p
, prefix
, prefix_len
);
908 memcpy(p
+ prefix_len
, name
, name_len
);
909 p
[prefix_len
+ name_len
] = '\0';
914 static int ocfs2_xattr_list_entries(struct inode
*inode
,
915 struct ocfs2_xattr_header
*header
,
916 char *buffer
, size_t buffer_size
)
920 const char *prefix
, *name
;
922 for (i
= 0 ; i
< le16_to_cpu(header
->xh_count
); i
++) {
923 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
924 type
= ocfs2_xattr_get_type(entry
);
925 prefix
= ocfs2_xattr_prefix(type
);
928 name
= (const char *)header
+
929 le16_to_cpu(entry
->xe_name_offset
);
931 ret
= ocfs2_xattr_list_entry(buffer
, buffer_size
,
932 &result
, prefix
, name
,
942 int ocfs2_has_inline_xattr_value_outside(struct inode
*inode
,
943 struct ocfs2_dinode
*di
)
945 struct ocfs2_xattr_header
*xh
;
948 xh
= (struct ocfs2_xattr_header
*)
949 ((void *)di
+ inode
->i_sb
->s_blocksize
-
950 le16_to_cpu(di
->i_xattr_inline_size
));
952 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++)
953 if (!ocfs2_xattr_is_local(&xh
->xh_entries
[i
]))
959 static int ocfs2_xattr_ibody_list(struct inode
*inode
,
960 struct ocfs2_dinode
*di
,
964 struct ocfs2_xattr_header
*header
= NULL
;
965 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
968 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
971 header
= (struct ocfs2_xattr_header
*)
972 ((void *)di
+ inode
->i_sb
->s_blocksize
-
973 le16_to_cpu(di
->i_xattr_inline_size
));
975 ret
= ocfs2_xattr_list_entries(inode
, header
, buffer
, buffer_size
);
980 static int ocfs2_xattr_block_list(struct inode
*inode
,
981 struct ocfs2_dinode
*di
,
985 struct buffer_head
*blk_bh
= NULL
;
986 struct ocfs2_xattr_block
*xb
;
989 if (!di
->i_xattr_loc
)
992 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
999 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1000 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1001 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
1002 ret
= ocfs2_xattr_list_entries(inode
, header
,
1003 buffer
, buffer_size
);
1005 ret
= ocfs2_xattr_tree_list_index_block(inode
, blk_bh
,
1006 buffer
, buffer_size
);
1013 ssize_t
ocfs2_listxattr(struct dentry
*dentry
,
1017 int ret
= 0, i_ret
= 0, b_ret
= 0;
1018 struct buffer_head
*di_bh
= NULL
;
1019 struct ocfs2_dinode
*di
= NULL
;
1020 struct ocfs2_inode_info
*oi
= OCFS2_I(dentry
->d_inode
);
1022 if (!ocfs2_supports_xattr(OCFS2_SB(dentry
->d_sb
)))
1025 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1028 ret
= ocfs2_inode_lock(dentry
->d_inode
, &di_bh
, 0);
1034 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1036 down_read(&oi
->ip_xattr_sem
);
1037 i_ret
= ocfs2_xattr_ibody_list(dentry
->d_inode
, di
, buffer
, size
);
1045 b_ret
= ocfs2_xattr_block_list(dentry
->d_inode
, di
,
1050 up_read(&oi
->ip_xattr_sem
);
1051 ocfs2_inode_unlock(dentry
->d_inode
, 0);
1055 return i_ret
+ b_ret
;
1058 static int ocfs2_xattr_find_entry(int name_index
,
1060 struct ocfs2_xattr_search
*xs
)
1062 struct ocfs2_xattr_entry
*entry
;
1069 name_len
= strlen(name
);
1071 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1072 cmp
= name_index
- ocfs2_xattr_get_type(entry
);
1074 cmp
= name_len
- entry
->xe_name_len
;
1076 cmp
= memcmp(name
, (xs
->base
+
1077 le16_to_cpu(entry
->xe_name_offset
)),
1085 return cmp
? -ENODATA
: 0;
1088 static int ocfs2_xattr_get_value_outside(struct inode
*inode
,
1089 struct ocfs2_xattr_value_root
*xv
,
1093 u32 cpos
, p_cluster
, num_clusters
, bpc
, clusters
;
1096 size_t cplen
, blocksize
;
1097 struct buffer_head
*bh
= NULL
;
1098 struct ocfs2_extent_list
*el
;
1101 clusters
= le32_to_cpu(xv
->xr_clusters
);
1102 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
1103 blocksize
= inode
->i_sb
->s_blocksize
;
1106 while (cpos
< clusters
) {
1107 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
1108 &num_clusters
, el
, NULL
);
1114 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
1115 /* Copy ocfs2_xattr_value */
1116 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
1117 ret
= ocfs2_read_block(INODE_CACHE(inode
), blkno
,
1124 cplen
= len
>= blocksize
? blocksize
: len
;
1125 memcpy(buffer
, bh
->b_data
, cplen
);
1134 cpos
+= num_clusters
;
1140 static int ocfs2_xattr_ibody_get(struct inode
*inode
,
1145 struct ocfs2_xattr_search
*xs
)
1147 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1148 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1149 struct ocfs2_xattr_value_root
*xv
;
1153 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
1156 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
1157 xs
->header
= (struct ocfs2_xattr_header
*)
1158 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
1159 xs
->base
= (void *)xs
->header
;
1160 xs
->here
= xs
->header
->xh_entries
;
1162 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1165 size
= le64_to_cpu(xs
->here
->xe_value_size
);
1167 if (size
> buffer_size
)
1169 if (ocfs2_xattr_is_local(xs
->here
)) {
1170 memcpy(buffer
, (void *)xs
->base
+
1171 le16_to_cpu(xs
->here
->xe_name_offset
) +
1172 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
), size
);
1174 xv
= (struct ocfs2_xattr_value_root
*)
1175 (xs
->base
+ le16_to_cpu(
1176 xs
->here
->xe_name_offset
) +
1177 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
));
1178 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
1190 static int ocfs2_xattr_block_get(struct inode
*inode
,
1195 struct ocfs2_xattr_search
*xs
)
1197 struct ocfs2_xattr_block
*xb
;
1198 struct ocfs2_xattr_value_root
*xv
;
1200 int ret
= -ENODATA
, name_offset
, name_len
, i
;
1201 int uninitialized_var(block_off
);
1203 xs
->bucket
= ocfs2_xattr_bucket_new(inode
);
1210 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, xs
);
1216 if (xs
->not_found
) {
1221 xb
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
1222 size
= le64_to_cpu(xs
->here
->xe_value_size
);
1225 if (size
> buffer_size
)
1228 name_offset
= le16_to_cpu(xs
->here
->xe_name_offset
);
1229 name_len
= OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
);
1230 i
= xs
->here
- xs
->header
->xh_entries
;
1232 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
1233 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
1234 bucket_xh(xs
->bucket
),
1238 xs
->base
= bucket_block(xs
->bucket
, block_off
);
1240 if (ocfs2_xattr_is_local(xs
->here
)) {
1241 memcpy(buffer
, (void *)xs
->base
+
1242 name_offset
+ name_len
, size
);
1244 xv
= (struct ocfs2_xattr_value_root
*)
1245 (xs
->base
+ name_offset
+ name_len
);
1246 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
1256 ocfs2_xattr_bucket_free(xs
->bucket
);
1258 brelse(xs
->xattr_bh
);
1259 xs
->xattr_bh
= NULL
;
1263 int ocfs2_xattr_get_nolock(struct inode
*inode
,
1264 struct buffer_head
*di_bh
,
1271 struct ocfs2_dinode
*di
= NULL
;
1272 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1273 struct ocfs2_xattr_search xis
= {
1274 .not_found
= -ENODATA
,
1276 struct ocfs2_xattr_search xbs
= {
1277 .not_found
= -ENODATA
,
1280 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
1283 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1286 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
1287 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1289 ret
= ocfs2_xattr_ibody_get(inode
, name_index
, name
, buffer
,
1291 if (ret
== -ENODATA
&& di
->i_xattr_loc
)
1292 ret
= ocfs2_xattr_block_get(inode
, name_index
, name
, buffer
,
1298 /* ocfs2_xattr_get()
1300 * Copy an extended attribute into the buffer provided.
1301 * Buffer is NULL to compute the size of buffer required.
1303 static int ocfs2_xattr_get(struct inode
*inode
,
1310 struct buffer_head
*di_bh
= NULL
;
1312 ret
= ocfs2_inode_lock(inode
, &di_bh
, 0);
1317 down_read(&OCFS2_I(inode
)->ip_xattr_sem
);
1318 ret
= ocfs2_xattr_get_nolock(inode
, di_bh
, name_index
,
1319 name
, buffer
, buffer_size
);
1320 up_read(&OCFS2_I(inode
)->ip_xattr_sem
);
1322 ocfs2_inode_unlock(inode
, 0);
1329 static int __ocfs2_xattr_set_value_outside(struct inode
*inode
,
1331 struct ocfs2_xattr_value_buf
*vb
,
1335 int ret
= 0, i
, cp_len
;
1336 u16 blocksize
= inode
->i_sb
->s_blocksize
;
1337 u32 p_cluster
, num_clusters
;
1338 u32 cpos
= 0, bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
1339 u32 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, value_len
);
1341 struct buffer_head
*bh
= NULL
;
1342 unsigned int ext_flags
;
1343 struct ocfs2_xattr_value_root
*xv
= vb
->vb_xv
;
1345 BUG_ON(clusters
> le32_to_cpu(xv
->xr_clusters
));
1347 while (cpos
< clusters
) {
1348 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
1349 &num_clusters
, &xv
->xr_list
,
1356 BUG_ON(ext_flags
& OCFS2_EXT_REFCOUNTED
);
1358 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
1360 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
1361 ret
= ocfs2_read_block(INODE_CACHE(inode
), blkno
,
1368 ret
= ocfs2_journal_access(handle
,
1371 OCFS2_JOURNAL_ACCESS_WRITE
);
1377 cp_len
= value_len
> blocksize
? blocksize
: value_len
;
1378 memcpy(bh
->b_data
, value
, cp_len
);
1379 value_len
-= cp_len
;
1381 if (cp_len
< blocksize
)
1382 memset(bh
->b_data
+ cp_len
, 0,
1383 blocksize
- cp_len
);
1385 ocfs2_journal_dirty(handle
, bh
);
1392 cpos
+= num_clusters
;
1400 static int ocfs2_xa_check_space_helper(int needed_space
, int free_start
,
1408 free_space
= free_start
-
1409 sizeof(struct ocfs2_xattr_header
) -
1410 (num_entries
* sizeof(struct ocfs2_xattr_entry
)) -
1411 OCFS2_XATTR_HEADER_GAP
;
1414 if (free_space
< needed_space
)
1420 static int ocfs2_xa_journal_access(handle_t
*handle
, struct ocfs2_xa_loc
*loc
,
1423 return loc
->xl_ops
->xlo_journal_access(handle
, loc
, type
);
1426 static void ocfs2_xa_journal_dirty(handle_t
*handle
, struct ocfs2_xa_loc
*loc
)
1428 loc
->xl_ops
->xlo_journal_dirty(handle
, loc
);
1431 /* Give a pointer into the storage for the given offset */
1432 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc
*loc
, int offset
)
1434 BUG_ON(offset
>= loc
->xl_size
);
1435 return loc
->xl_ops
->xlo_offset_pointer(loc
, offset
);
1439 * Wipe the name+value pair and allow the storage to reclaim it. This
1440 * must be followed by either removal of the entry or a call to
1441 * ocfs2_xa_add_namevalue().
1443 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1445 loc
->xl_ops
->xlo_wipe_namevalue(loc
);
1449 * Find lowest offset to a name+value pair. This is the start of our
1450 * downward-growing free space.
1452 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc
*loc
)
1454 return loc
->xl_ops
->xlo_get_free_start(loc
);
1457 /* Can we reuse loc->xl_entry for xi? */
1458 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc
*loc
,
1459 struct ocfs2_xattr_info
*xi
)
1461 return loc
->xl_ops
->xlo_can_reuse(loc
, xi
);
1464 /* How much free space is needed to set the new value */
1465 static int ocfs2_xa_check_space(struct ocfs2_xa_loc
*loc
,
1466 struct ocfs2_xattr_info
*xi
)
1468 return loc
->xl_ops
->xlo_check_space(loc
, xi
);
1471 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1473 loc
->xl_ops
->xlo_add_entry(loc
, name_hash
);
1474 loc
->xl_entry
->xe_name_hash
= cpu_to_le32(name_hash
);
1476 * We can't leave the new entry's xe_name_offset at zero or
1477 * add_namevalue() will go nuts. We set it to the size of our
1478 * storage so that it can never be less than any other entry.
1480 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(loc
->xl_size
);
1483 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc
*loc
,
1484 struct ocfs2_xattr_info
*xi
)
1486 int size
= namevalue_size_xi(xi
);
1490 loc
->xl_ops
->xlo_add_namevalue(loc
, size
);
1491 loc
->xl_entry
->xe_value_size
= cpu_to_le64(xi
->xi_value_len
);
1492 loc
->xl_entry
->xe_name_len
= xi
->xi_name_len
;
1493 ocfs2_xattr_set_type(loc
->xl_entry
, xi
->xi_name_index
);
1494 ocfs2_xattr_set_local(loc
->xl_entry
,
1495 xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
);
1497 nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1498 nameval_buf
= ocfs2_xa_offset_pointer(loc
, nameval_offset
);
1499 memset(nameval_buf
, 0, size
);
1500 memcpy(nameval_buf
, xi
->xi_name
, xi
->xi_name_len
);
1503 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1504 struct ocfs2_xattr_value_buf
*vb
)
1506 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1507 int name_size
= OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
);
1509 /* Value bufs are for value trees */
1510 BUG_ON(ocfs2_xattr_is_local(loc
->xl_entry
));
1511 BUG_ON(namevalue_size_xe(loc
->xl_entry
) !=
1512 (name_size
+ OCFS2_XATTR_ROOT_SIZE
));
1514 loc
->xl_ops
->xlo_fill_value_buf(loc
, vb
);
1516 (struct ocfs2_xattr_value_root
*)ocfs2_xa_offset_pointer(loc
,
1521 static int ocfs2_xa_block_journal_access(handle_t
*handle
,
1522 struct ocfs2_xa_loc
*loc
, int type
)
1524 struct buffer_head
*bh
= loc
->xl_storage
;
1525 ocfs2_journal_access_func access
;
1527 if (loc
->xl_size
== (bh
->b_size
-
1528 offsetof(struct ocfs2_xattr_block
,
1529 xb_attrs
.xb_header
)))
1530 access
= ocfs2_journal_access_xb
;
1532 access
= ocfs2_journal_access_di
;
1533 return access(handle
, INODE_CACHE(loc
->xl_inode
), bh
, type
);
1536 static void ocfs2_xa_block_journal_dirty(handle_t
*handle
,
1537 struct ocfs2_xa_loc
*loc
)
1539 struct buffer_head
*bh
= loc
->xl_storage
;
1541 ocfs2_journal_dirty(handle
, bh
);
1544 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc
*loc
,
1547 return (char *)loc
->xl_header
+ offset
;
1550 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc
*loc
,
1551 struct ocfs2_xattr_info
*xi
)
1554 * Block storage is strict. If the sizes aren't exact, we will
1555 * remove the old one and reinsert the new.
1557 return namevalue_size_xe(loc
->xl_entry
) ==
1558 namevalue_size_xi(xi
);
1561 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc
*loc
)
1563 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1564 int i
, count
= le16_to_cpu(xh
->xh_count
);
1565 int offset
, free_start
= loc
->xl_size
;
1567 for (i
= 0; i
< count
; i
++) {
1568 offset
= le16_to_cpu(xh
->xh_entries
[i
].xe_name_offset
);
1569 if (offset
< free_start
)
1570 free_start
= offset
;
1576 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc
*loc
,
1577 struct ocfs2_xattr_info
*xi
)
1579 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1580 int free_start
= ocfs2_xa_get_free_start(loc
);
1581 int needed_space
= ocfs2_xi_entry_usage(xi
);
1584 * Block storage will reclaim the original entry before inserting
1585 * the new value, so we only need the difference. If the new
1586 * entry is smaller than the old one, we don't need anything.
1588 if (loc
->xl_entry
) {
1589 /* Don't need space if we're reusing! */
1590 if (ocfs2_xa_can_reuse_entry(loc
, xi
))
1593 needed_space
-= ocfs2_xe_entry_usage(loc
->xl_entry
);
1595 if (needed_space
< 0)
1597 return ocfs2_xa_check_space_helper(needed_space
, free_start
, count
);
1601 * Block storage for xattrs keeps the name+value pairs compacted. When
1602 * we remove one, we have to shift any that preceded it towards the end.
1604 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1607 int namevalue_offset
, first_namevalue_offset
, namevalue_size
;
1608 struct ocfs2_xattr_entry
*entry
= loc
->xl_entry
;
1609 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1610 int count
= le16_to_cpu(xh
->xh_count
);
1612 namevalue_offset
= le16_to_cpu(entry
->xe_name_offset
);
1613 namevalue_size
= namevalue_size_xe(entry
);
1614 first_namevalue_offset
= ocfs2_xa_get_free_start(loc
);
1616 /* Shift the name+value pairs */
1617 memmove((char *)xh
+ first_namevalue_offset
+ namevalue_size
,
1618 (char *)xh
+ first_namevalue_offset
,
1619 namevalue_offset
- first_namevalue_offset
);
1620 memset((char *)xh
+ first_namevalue_offset
, 0, namevalue_size
);
1622 /* Now tell xh->xh_entries about it */
1623 for (i
= 0; i
< count
; i
++) {
1624 offset
= le16_to_cpu(xh
->xh_entries
[i
].xe_name_offset
);
1625 if (offset
<= namevalue_offset
)
1626 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
,
1631 * Note that we don't update xh_free_start or xh_name_value_len
1632 * because they're not used in block-stored xattrs.
1636 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1638 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1639 loc
->xl_entry
= &(loc
->xl_header
->xh_entries
[count
]);
1640 le16_add_cpu(&loc
->xl_header
->xh_count
, 1);
1641 memset(loc
->xl_entry
, 0, sizeof(struct ocfs2_xattr_entry
));
1644 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc
*loc
, int size
)
1646 int free_start
= ocfs2_xa_get_free_start(loc
);
1648 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(free_start
- size
);
1651 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1652 struct ocfs2_xattr_value_buf
*vb
)
1654 struct buffer_head
*bh
= loc
->xl_storage
;
1656 if (loc
->xl_size
== (bh
->b_size
-
1657 offsetof(struct ocfs2_xattr_block
,
1658 xb_attrs
.xb_header
)))
1659 vb
->vb_access
= ocfs2_journal_access_xb
;
1661 vb
->vb_access
= ocfs2_journal_access_di
;
1666 * Operations for xattrs stored in blocks. This includes inline inode
1667 * storage and unindexed ocfs2_xattr_blocks.
1669 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops
= {
1670 .xlo_journal_access
= ocfs2_xa_block_journal_access
,
1671 .xlo_journal_dirty
= ocfs2_xa_block_journal_dirty
,
1672 .xlo_offset_pointer
= ocfs2_xa_block_offset_pointer
,
1673 .xlo_check_space
= ocfs2_xa_block_check_space
,
1674 .xlo_can_reuse
= ocfs2_xa_block_can_reuse
,
1675 .xlo_get_free_start
= ocfs2_xa_block_get_free_start
,
1676 .xlo_wipe_namevalue
= ocfs2_xa_block_wipe_namevalue
,
1677 .xlo_add_entry
= ocfs2_xa_block_add_entry
,
1678 .xlo_add_namevalue
= ocfs2_xa_block_add_namevalue
,
1679 .xlo_fill_value_buf
= ocfs2_xa_block_fill_value_buf
,
1682 static int ocfs2_xa_bucket_journal_access(handle_t
*handle
,
1683 struct ocfs2_xa_loc
*loc
, int type
)
1685 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1687 return ocfs2_xattr_bucket_journal_access(handle
, bucket
, type
);
1690 static void ocfs2_xa_bucket_journal_dirty(handle_t
*handle
,
1691 struct ocfs2_xa_loc
*loc
)
1693 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1695 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
1698 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc
*loc
,
1701 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1702 int block
, block_offset
;
1704 /* The header is at the front of the bucket */
1705 block
= offset
>> loc
->xl_inode
->i_sb
->s_blocksize_bits
;
1706 block_offset
= offset
% loc
->xl_inode
->i_sb
->s_blocksize
;
1708 return bucket_block(bucket
, block
) + block_offset
;
1711 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc
*loc
,
1712 struct ocfs2_xattr_info
*xi
)
1714 return namevalue_size_xe(loc
->xl_entry
) >=
1715 namevalue_size_xi(xi
);
1718 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc
*loc
)
1720 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1721 return le16_to_cpu(bucket_xh(bucket
)->xh_free_start
);
1724 static int ocfs2_bucket_align_free_start(struct super_block
*sb
,
1725 int free_start
, int size
)
1728 * We need to make sure that the name+value pair fits within
1731 if (((free_start
- size
) >> sb
->s_blocksize_bits
) !=
1732 ((free_start
- 1) >> sb
->s_blocksize_bits
))
1733 free_start
-= free_start
% sb
->s_blocksize
;
1738 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc
*loc
,
1739 struct ocfs2_xattr_info
*xi
)
1742 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1743 int free_start
= ocfs2_xa_get_free_start(loc
);
1744 int needed_space
= ocfs2_xi_entry_usage(xi
);
1745 int size
= namevalue_size_xi(xi
);
1746 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1749 * Bucket storage does not reclaim name+value pairs it cannot
1750 * reuse. They live as holes until the bucket fills, and then
1751 * the bucket is defragmented. However, the bucket can reclaim
1752 * the ocfs2_xattr_entry.
1754 if (loc
->xl_entry
) {
1755 /* Don't need space if we're reusing! */
1756 if (ocfs2_xa_can_reuse_entry(loc
, xi
))
1759 needed_space
-= sizeof(struct ocfs2_xattr_entry
);
1761 BUG_ON(needed_space
< 0);
1763 if (free_start
< size
) {
1768 * First we check if it would fit in the first place.
1769 * Below, we align the free start to a block. This may
1770 * slide us below the minimum gap. By checking unaligned
1771 * first, we avoid that error.
1773 rc
= ocfs2_xa_check_space_helper(needed_space
, free_start
,
1777 free_start
= ocfs2_bucket_align_free_start(sb
, free_start
,
1780 return ocfs2_xa_check_space_helper(needed_space
, free_start
, count
);
1783 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1785 le16_add_cpu(&loc
->xl_header
->xh_name_value_len
,
1786 -namevalue_size_xe(loc
->xl_entry
));
1789 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1791 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1792 int count
= le16_to_cpu(xh
->xh_count
);
1793 int low
= 0, high
= count
- 1, tmp
;
1794 struct ocfs2_xattr_entry
*tmp_xe
;
1797 * We keep buckets sorted by name_hash, so we need to find
1800 while (low
<= high
&& count
) {
1801 tmp
= (low
+ high
) / 2;
1802 tmp_xe
= &xh
->xh_entries
[tmp
];
1804 if (name_hash
> le32_to_cpu(tmp_xe
->xe_name_hash
))
1806 else if (name_hash
< le32_to_cpu(tmp_xe
->xe_name_hash
))
1815 memmove(&xh
->xh_entries
[low
+ 1],
1816 &xh
->xh_entries
[low
],
1817 ((count
- low
) * sizeof(struct ocfs2_xattr_entry
)));
1819 le16_add_cpu(&xh
->xh_count
, 1);
1820 loc
->xl_entry
= &xh
->xh_entries
[low
];
1821 memset(loc
->xl_entry
, 0, sizeof(struct ocfs2_xattr_entry
));
1824 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc
*loc
, int size
)
1826 int free_start
= ocfs2_xa_get_free_start(loc
);
1827 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1828 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1831 free_start
= ocfs2_bucket_align_free_start(sb
, free_start
, size
);
1832 nameval_offset
= free_start
- size
;
1833 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(nameval_offset
);
1834 xh
->xh_free_start
= cpu_to_le16(nameval_offset
);
1835 le16_add_cpu(&xh
->xh_name_value_len
, size
);
1839 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1840 struct ocfs2_xattr_value_buf
*vb
)
1842 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1843 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1844 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1845 int size
= namevalue_size_xe(loc
->xl_entry
);
1846 int block_offset
= nameval_offset
>> sb
->s_blocksize_bits
;
1848 /* Values are not allowed to straddle block boundaries */
1849 BUG_ON(block_offset
!=
1850 ((nameval_offset
+ size
- 1) >> sb
->s_blocksize_bits
));
1851 /* We expect the bucket to be filled in */
1852 BUG_ON(!bucket
->bu_bhs
[block_offset
]);
1854 vb
->vb_access
= ocfs2_journal_access
;
1855 vb
->vb_bh
= bucket
->bu_bhs
[block_offset
];
1858 /* Operations for xattrs stored in buckets. */
1859 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops
= {
1860 .xlo_journal_access
= ocfs2_xa_bucket_journal_access
,
1861 .xlo_journal_dirty
= ocfs2_xa_bucket_journal_dirty
,
1862 .xlo_offset_pointer
= ocfs2_xa_bucket_offset_pointer
,
1863 .xlo_check_space
= ocfs2_xa_bucket_check_space
,
1864 .xlo_can_reuse
= ocfs2_xa_bucket_can_reuse
,
1865 .xlo_get_free_start
= ocfs2_xa_bucket_get_free_start
,
1866 .xlo_wipe_namevalue
= ocfs2_xa_bucket_wipe_namevalue
,
1867 .xlo_add_entry
= ocfs2_xa_bucket_add_entry
,
1868 .xlo_add_namevalue
= ocfs2_xa_bucket_add_namevalue
,
1869 .xlo_fill_value_buf
= ocfs2_xa_bucket_fill_value_buf
,
1872 static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc
*loc
)
1874 struct ocfs2_xattr_value_buf vb
;
1876 if (ocfs2_xattr_is_local(loc
->xl_entry
))
1879 ocfs2_xa_fill_value_buf(loc
, &vb
);
1880 return le32_to_cpu(vb
.vb_xv
->xr_clusters
);
1883 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc
*loc
, u64 bytes
,
1884 struct ocfs2_xattr_set_ctxt
*ctxt
)
1886 int trunc_rc
, access_rc
;
1887 struct ocfs2_xattr_value_buf vb
;
1889 ocfs2_xa_fill_value_buf(loc
, &vb
);
1890 trunc_rc
= ocfs2_xattr_value_truncate(loc
->xl_inode
, &vb
, bytes
,
1894 * The caller of ocfs2_xa_value_truncate() has already called
1895 * ocfs2_xa_journal_access on the loc. However, The truncate code
1896 * calls ocfs2_extend_trans(). This may commit the previous
1897 * transaction and open a new one. If this is a bucket, truncate
1898 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1899 * the caller is expecting to dirty the entire bucket. So we must
1900 * reset the journal work. We do this even if truncate has failed,
1901 * as it could have failed after committing the extend.
1903 access_rc
= ocfs2_xa_journal_access(ctxt
->handle
, loc
,
1904 OCFS2_JOURNAL_ACCESS_WRITE
);
1906 /* Errors in truncate take precedence */
1907 return trunc_rc
? trunc_rc
: access_rc
;
1910 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc
*loc
)
1913 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1914 struct ocfs2_xattr_entry
*entry
= loc
->xl_entry
;
1916 ocfs2_xa_wipe_namevalue(loc
);
1917 loc
->xl_entry
= NULL
;
1919 le16_add_cpu(&xh
->xh_count
, -1);
1920 count
= le16_to_cpu(xh
->xh_count
);
1923 * Only zero out the entry if there are more remaining. This is
1924 * important for an empty bucket, as it keeps track of the
1925 * bucket's hash value. It doesn't hurt empty block storage.
1928 index
= ((char *)entry
- (char *)&xh
->xh_entries
) /
1929 sizeof(struct ocfs2_xattr_entry
);
1930 memmove(&xh
->xh_entries
[index
], &xh
->xh_entries
[index
+ 1],
1931 (count
- index
) * sizeof(struct ocfs2_xattr_entry
));
1932 memset(&xh
->xh_entries
[count
], 0,
1933 sizeof(struct ocfs2_xattr_entry
));
1938 * If we have a problem adjusting the size of an external value during
1939 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1940 * in an intermediate state. For example, the value may be partially
1943 * If the value tree hasn't changed, the extend/truncate went nowhere.
1944 * We have nothing to do. The caller can treat it as a straight error.
1946 * If the value tree got partially truncated, we now have a corrupted
1947 * extended attribute. We're going to wipe its entry and leak the
1948 * clusters. Better to leak some storage than leave a corrupt entry.
1950 * If the value tree grew, it obviously didn't grow enough for the
1951 * new entry. We're not going to try and reclaim those clusters either.
1952 * If there was already an external value there (orig_clusters != 0),
1953 * the new clusters are attached safely and we can just leave the old
1954 * value in place. If there was no external value there, we remove
1957 * This way, the xattr block we store in the journal will be consistent.
1958 * If the size change broke because of the journal, no changes will hit
1961 static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc
*loc
,
1963 unsigned int orig_clusters
)
1965 unsigned int new_clusters
= ocfs2_xa_value_clusters(loc
);
1966 char *nameval_buf
= ocfs2_xa_offset_pointer(loc
,
1967 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
1969 if (new_clusters
< orig_clusters
) {
1971 "Partial truncate while %s xattr %.*s. Leaking "
1972 "%u clusters and removing the entry\n",
1973 what
, loc
->xl_entry
->xe_name_len
, nameval_buf
,
1974 orig_clusters
- new_clusters
);
1975 ocfs2_xa_remove_entry(loc
);
1976 } else if (!orig_clusters
) {
1978 "Unable to allocate an external value for xattr "
1979 "%.*s safely. Leaking %u clusters and removing the "
1981 loc
->xl_entry
->xe_name_len
, nameval_buf
,
1982 new_clusters
- orig_clusters
);
1983 ocfs2_xa_remove_entry(loc
);
1984 } else if (new_clusters
> orig_clusters
)
1986 "Unable to grow xattr %.*s safely. %u new clusters "
1987 "have been added, but the value will not be "
1989 loc
->xl_entry
->xe_name_len
, nameval_buf
,
1990 new_clusters
- orig_clusters
);
1993 static int ocfs2_xa_remove(struct ocfs2_xa_loc
*loc
,
1994 struct ocfs2_xattr_set_ctxt
*ctxt
)
1997 unsigned int orig_clusters
;
1999 if (!ocfs2_xattr_is_local(loc
->xl_entry
)) {
2000 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2001 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2005 * Since this is remove, we can return 0 if
2006 * ocfs2_xa_cleanup_value_truncate() is going to
2007 * wipe the entry anyway. So we check the
2008 * cluster count as well.
2010 if (orig_clusters
!= ocfs2_xa_value_clusters(loc
))
2012 ocfs2_xa_cleanup_value_truncate(loc
, "removing",
2019 ocfs2_xa_remove_entry(loc
);
2025 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc
*loc
)
2027 int name_size
= OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
);
2030 nameval_buf
= ocfs2_xa_offset_pointer(loc
,
2031 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
2032 memcpy(nameval_buf
+ name_size
, &def_xv
, OCFS2_XATTR_ROOT_SIZE
);
2036 * Take an existing entry and make it ready for the new value. This
2037 * won't allocate space, but it may free space. It should be ready for
2038 * ocfs2_xa_prepare_entry() to finish the work.
2040 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc
*loc
,
2041 struct ocfs2_xattr_info
*xi
,
2042 struct ocfs2_xattr_set_ctxt
*ctxt
)
2045 int name_size
= OCFS2_XATTR_SIZE(xi
->xi_name_len
);
2046 unsigned int orig_clusters
;
2048 int xe_local
= ocfs2_xattr_is_local(loc
->xl_entry
);
2049 int xi_local
= xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
;
2051 BUG_ON(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
));
2057 memset(nameval_buf
+ name_size
, 0,
2058 namevalue_size_xe(loc
->xl_entry
) - name_size
);
2060 ocfs2_xa_install_value_root(loc
);
2062 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2064 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2068 memset(nameval_buf
+ name_size
, 0,
2069 namevalue_size_xe(loc
->xl_entry
) -
2071 } else if (le64_to_cpu(loc
->xl_entry
->xe_value_size
) >
2073 rc
= ocfs2_xa_value_truncate(loc
, xi
->xi_value_len
,
2080 ocfs2_xa_cleanup_value_truncate(loc
, "reusing",
2086 loc
->xl_entry
->xe_value_size
= cpu_to_le64(xi
->xi_value_len
);
2087 ocfs2_xattr_set_local(loc
->xl_entry
, xi_local
);
2094 * Prepares loc->xl_entry to receive the new xattr. This includes
2095 * properly setting up the name+value pair region. If loc->xl_entry
2096 * already exists, it will take care of modifying it appropriately.
2098 * Note that this modifies the data. You did journal_access already,
2101 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc
*loc
,
2102 struct ocfs2_xattr_info
*xi
,
2104 struct ocfs2_xattr_set_ctxt
*ctxt
)
2107 unsigned int orig_clusters
;
2108 __le64 orig_value_size
= 0;
2110 rc
= ocfs2_xa_check_space(loc
, xi
);
2114 if (loc
->xl_entry
) {
2115 if (ocfs2_xa_can_reuse_entry(loc
, xi
)) {
2116 orig_value_size
= loc
->xl_entry
->xe_value_size
;
2117 rc
= ocfs2_xa_reuse_entry(loc
, xi
, ctxt
);
2123 if (!ocfs2_xattr_is_local(loc
->xl_entry
)) {
2124 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2125 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2128 ocfs2_xa_cleanup_value_truncate(loc
,
2134 ocfs2_xa_wipe_namevalue(loc
);
2136 ocfs2_xa_add_entry(loc
, name_hash
);
2139 * If we get here, we have a blank entry. Fill it. We grow our
2140 * name+value pair back from the end.
2142 ocfs2_xa_add_namevalue(loc
, xi
);
2143 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
)
2144 ocfs2_xa_install_value_root(loc
);
2147 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2148 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2149 rc
= ocfs2_xa_value_truncate(loc
, xi
->xi_value_len
, ctxt
);
2151 ctxt
->set_abort
= 1;
2152 ocfs2_xa_cleanup_value_truncate(loc
, "growing",
2155 * If we were growing an existing value,
2156 * ocfs2_xa_cleanup_value_truncate() won't remove
2157 * the entry. We need to restore the original value
2160 if (loc
->xl_entry
) {
2161 BUG_ON(!orig_value_size
);
2162 loc
->xl_entry
->xe_value_size
= orig_value_size
;
2173 * Store the value portion of the name+value pair. This will skip
2174 * values that are stored externally. Their tree roots were set up
2175 * by ocfs2_xa_prepare_entry().
2177 static int ocfs2_xa_store_value(struct ocfs2_xa_loc
*loc
,
2178 struct ocfs2_xattr_info
*xi
,
2179 struct ocfs2_xattr_set_ctxt
*ctxt
)
2182 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
2183 int name_size
= OCFS2_XATTR_SIZE(xi
->xi_name_len
);
2185 struct ocfs2_xattr_value_buf vb
;
2187 nameval_buf
= ocfs2_xa_offset_pointer(loc
, nameval_offset
);
2188 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2189 ocfs2_xa_fill_value_buf(loc
, &vb
);
2190 rc
= __ocfs2_xattr_set_value_outside(loc
->xl_inode
,
2195 memcpy(nameval_buf
+ name_size
, xi
->xi_value
, xi
->xi_value_len
);
2200 static int ocfs2_xa_set(struct ocfs2_xa_loc
*loc
,
2201 struct ocfs2_xattr_info
*xi
,
2202 struct ocfs2_xattr_set_ctxt
*ctxt
)
2205 u32 name_hash
= ocfs2_xattr_name_hash(loc
->xl_inode
, xi
->xi_name
,
2208 ret
= ocfs2_xa_journal_access(ctxt
->handle
, loc
,
2209 OCFS2_JOURNAL_ACCESS_WRITE
);
2216 * From here on out, everything is going to modify the buffer a
2217 * little. Errors are going to leave the xattr header in a
2218 * sane state. Thus, even with errors we dirty the sucker.
2221 /* Don't worry, we are never called with !xi_value and !xl_entry */
2222 if (!xi
->xi_value
) {
2223 ret
= ocfs2_xa_remove(loc
, ctxt
);
2227 ret
= ocfs2_xa_prepare_entry(loc
, xi
, name_hash
, ctxt
);
2234 ret
= ocfs2_xa_store_value(loc
, xi
, ctxt
);
2239 ocfs2_xa_journal_dirty(ctxt
->handle
, loc
);
2245 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc
*loc
,
2246 struct inode
*inode
,
2247 struct buffer_head
*bh
,
2248 struct ocfs2_xattr_entry
*entry
)
2250 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)bh
->b_data
;
2252 BUG_ON(!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
));
2254 loc
->xl_inode
= inode
;
2255 loc
->xl_ops
= &ocfs2_xa_block_loc_ops
;
2256 loc
->xl_storage
= bh
;
2257 loc
->xl_entry
= entry
;
2258 loc
->xl_size
= le16_to_cpu(di
->i_xattr_inline_size
);
2260 (struct ocfs2_xattr_header
*)(bh
->b_data
+ bh
->b_size
-
2264 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc
*loc
,
2265 struct inode
*inode
,
2266 struct buffer_head
*bh
,
2267 struct ocfs2_xattr_entry
*entry
)
2269 struct ocfs2_xattr_block
*xb
=
2270 (struct ocfs2_xattr_block
*)bh
->b_data
;
2272 BUG_ON(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
);
2274 loc
->xl_inode
= inode
;
2275 loc
->xl_ops
= &ocfs2_xa_block_loc_ops
;
2276 loc
->xl_storage
= bh
;
2277 loc
->xl_header
= &(xb
->xb_attrs
.xb_header
);
2278 loc
->xl_entry
= entry
;
2279 loc
->xl_size
= bh
->b_size
- offsetof(struct ocfs2_xattr_block
,
2280 xb_attrs
.xb_header
);
2283 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc
*loc
,
2284 struct ocfs2_xattr_bucket
*bucket
,
2285 struct ocfs2_xattr_entry
*entry
)
2287 loc
->xl_inode
= bucket
->bu_inode
;
2288 loc
->xl_ops
= &ocfs2_xa_bucket_loc_ops
;
2289 loc
->xl_storage
= bucket
;
2290 loc
->xl_header
= bucket_xh(bucket
);
2291 loc
->xl_entry
= entry
;
2292 loc
->xl_size
= OCFS2_XATTR_BUCKET_SIZE
;
2296 * In xattr remove, if it is stored outside and refcounted, we may have
2297 * the chance to split the refcount tree. So need the allocators.
2299 static int ocfs2_lock_xattr_remove_allocators(struct inode
*inode
,
2300 struct ocfs2_xattr_value_root
*xv
,
2301 struct ocfs2_caching_info
*ref_ci
,
2302 struct buffer_head
*ref_root_bh
,
2303 struct ocfs2_alloc_context
**meta_ac
,
2306 int ret
, meta_add
= 0;
2307 u32 p_cluster
, num_clusters
;
2308 unsigned int ext_flags
;
2311 ret
= ocfs2_xattr_get_clusters(inode
, 0, &p_cluster
,
2320 if (!(ext_flags
& OCFS2_EXT_REFCOUNTED
))
2323 ret
= ocfs2_refcounted_xattr_delete_need(inode
, ref_ci
,
2325 &meta_add
, ref_credits
);
2331 ret
= ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode
->i_sb
),
2340 static int ocfs2_remove_value_outside(struct inode
*inode
,
2341 struct ocfs2_xattr_value_buf
*vb
,
2342 struct ocfs2_xattr_header
*header
,
2343 struct ocfs2_caching_info
*ref_ci
,
2344 struct buffer_head
*ref_root_bh
)
2346 int ret
= 0, i
, ref_credits
;
2347 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2348 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, };
2351 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
2353 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
2354 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
2356 if (ocfs2_xattr_is_local(entry
))
2359 val
= (void *)header
+
2360 le16_to_cpu(entry
->xe_name_offset
);
2361 vb
->vb_xv
= (struct ocfs2_xattr_value_root
*)
2362 (val
+ OCFS2_XATTR_SIZE(entry
->xe_name_len
));
2364 ret
= ocfs2_lock_xattr_remove_allocators(inode
, vb
->vb_xv
,
2365 ref_ci
, ref_root_bh
,
2369 ctxt
.handle
= ocfs2_start_trans(osb
, ref_credits
+
2370 ocfs2_remove_extent_credits(osb
->sb
));
2371 if (IS_ERR(ctxt
.handle
)) {
2372 ret
= PTR_ERR(ctxt
.handle
);
2377 ret
= ocfs2_xattr_value_truncate(inode
, vb
, 0, &ctxt
);
2383 ocfs2_commit_trans(osb
, ctxt
.handle
);
2385 ocfs2_free_alloc_context(ctxt
.meta_ac
);
2386 ctxt
.meta_ac
= NULL
;
2391 ocfs2_free_alloc_context(ctxt
.meta_ac
);
2392 ocfs2_schedule_truncate_log_flush(osb
, 1);
2393 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
2397 static int ocfs2_xattr_ibody_remove(struct inode
*inode
,
2398 struct buffer_head
*di_bh
,
2399 struct ocfs2_caching_info
*ref_ci
,
2400 struct buffer_head
*ref_root_bh
)
2403 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2404 struct ocfs2_xattr_header
*header
;
2406 struct ocfs2_xattr_value_buf vb
= {
2408 .vb_access
= ocfs2_journal_access_di
,
2411 header
= (struct ocfs2_xattr_header
*)
2412 ((void *)di
+ inode
->i_sb
->s_blocksize
-
2413 le16_to_cpu(di
->i_xattr_inline_size
));
2415 ret
= ocfs2_remove_value_outside(inode
, &vb
, header
,
2416 ref_ci
, ref_root_bh
);
2421 struct ocfs2_rm_xattr_bucket_para
{
2422 struct ocfs2_caching_info
*ref_ci
;
2423 struct buffer_head
*ref_root_bh
;
2426 static int ocfs2_xattr_block_remove(struct inode
*inode
,
2427 struct buffer_head
*blk_bh
,
2428 struct ocfs2_caching_info
*ref_ci
,
2429 struct buffer_head
*ref_root_bh
)
2431 struct ocfs2_xattr_block
*xb
;
2433 struct ocfs2_xattr_value_buf vb
= {
2435 .vb_access
= ocfs2_journal_access_xb
,
2437 struct ocfs2_rm_xattr_bucket_para args
= {
2439 .ref_root_bh
= ref_root_bh
,
2442 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2443 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2444 struct ocfs2_xattr_header
*header
= &(xb
->xb_attrs
.xb_header
);
2445 ret
= ocfs2_remove_value_outside(inode
, &vb
, header
,
2446 ref_ci
, ref_root_bh
);
2448 ret
= ocfs2_iterate_xattr_index_block(inode
,
2450 ocfs2_rm_xattr_cluster
,
2456 static int ocfs2_xattr_free_block(struct inode
*inode
,
2458 struct ocfs2_caching_info
*ref_ci
,
2459 struct buffer_head
*ref_root_bh
)
2461 struct inode
*xb_alloc_inode
;
2462 struct buffer_head
*xb_alloc_bh
= NULL
;
2463 struct buffer_head
*blk_bh
= NULL
;
2464 struct ocfs2_xattr_block
*xb
;
2465 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2471 ret
= ocfs2_read_xattr_block(inode
, block
, &blk_bh
);
2477 ret
= ocfs2_xattr_block_remove(inode
, blk_bh
, ref_ci
, ref_root_bh
);
2483 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2484 blk
= le64_to_cpu(xb
->xb_blkno
);
2485 bit
= le16_to_cpu(xb
->xb_suballoc_bit
);
2486 if (xb
->xb_suballoc_loc
)
2487 bg_blkno
= le64_to_cpu(xb
->xb_suballoc_loc
);
2489 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
2491 xb_alloc_inode
= ocfs2_get_system_file_inode(osb
,
2492 EXTENT_ALLOC_SYSTEM_INODE
,
2493 le16_to_cpu(xb
->xb_suballoc_slot
));
2494 if (!xb_alloc_inode
) {
2499 mutex_lock(&xb_alloc_inode
->i_mutex
);
2501 ret
= ocfs2_inode_lock(xb_alloc_inode
, &xb_alloc_bh
, 1);
2507 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
2508 if (IS_ERR(handle
)) {
2509 ret
= PTR_ERR(handle
);
2514 ret
= ocfs2_free_suballoc_bits(handle
, xb_alloc_inode
, xb_alloc_bh
,
2519 ocfs2_commit_trans(osb
, handle
);
2521 ocfs2_inode_unlock(xb_alloc_inode
, 1);
2522 brelse(xb_alloc_bh
);
2524 mutex_unlock(&xb_alloc_inode
->i_mutex
);
2525 iput(xb_alloc_inode
);
2532 * ocfs2_xattr_remove()
2534 * Free extended attribute resources associated with this inode.
2536 int ocfs2_xattr_remove(struct inode
*inode
, struct buffer_head
*di_bh
)
2538 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2539 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2540 struct ocfs2_refcount_tree
*ref_tree
= NULL
;
2541 struct buffer_head
*ref_root_bh
= NULL
;
2542 struct ocfs2_caching_info
*ref_ci
= NULL
;
2546 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
2549 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
2552 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
) {
2553 ret
= ocfs2_lock_refcount_tree(OCFS2_SB(inode
->i_sb
),
2554 le64_to_cpu(di
->i_refcount_loc
),
2555 1, &ref_tree
, &ref_root_bh
);
2560 ref_ci
= &ref_tree
->rf_ci
;
2564 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
2565 ret
= ocfs2_xattr_ibody_remove(inode
, di_bh
,
2566 ref_ci
, ref_root_bh
);
2573 if (di
->i_xattr_loc
) {
2574 ret
= ocfs2_xattr_free_block(inode
,
2575 le64_to_cpu(di
->i_xattr_loc
),
2576 ref_ci
, ref_root_bh
);
2583 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
2584 OCFS2_INODE_UPDATE_CREDITS
);
2585 if (IS_ERR(handle
)) {
2586 ret
= PTR_ERR(handle
);
2590 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
2591 OCFS2_JOURNAL_ACCESS_WRITE
);
2597 di
->i_xattr_loc
= 0;
2599 spin_lock(&oi
->ip_lock
);
2600 oi
->ip_dyn_features
&= ~(OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
);
2601 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
2602 spin_unlock(&oi
->ip_lock
);
2604 ocfs2_journal_dirty(handle
, di_bh
);
2606 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
2609 ocfs2_unlock_refcount_tree(OCFS2_SB(inode
->i_sb
), ref_tree
, 1);
2610 brelse(ref_root_bh
);
2614 static int ocfs2_xattr_has_space_inline(struct inode
*inode
,
2615 struct ocfs2_dinode
*di
)
2617 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2618 unsigned int xattrsize
= OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
;
2621 if (xattrsize
< OCFS2_MIN_XATTR_INLINE_SIZE
)
2624 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
2625 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
2626 free
= le16_to_cpu(idata
->id_count
) - le64_to_cpu(di
->i_size
);
2627 } else if (ocfs2_inode_is_fast_symlink(inode
)) {
2628 free
= ocfs2_fast_symlink_chars(inode
->i_sb
) -
2629 le64_to_cpu(di
->i_size
);
2631 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
2632 free
= (le16_to_cpu(el
->l_count
) -
2633 le16_to_cpu(el
->l_next_free_rec
)) *
2634 sizeof(struct ocfs2_extent_rec
);
2636 if (free
>= xattrsize
)
2643 * ocfs2_xattr_ibody_find()
2645 * Find extended attribute in inode block and
2646 * fill search info into struct ocfs2_xattr_search.
2648 static int ocfs2_xattr_ibody_find(struct inode
*inode
,
2651 struct ocfs2_xattr_search
*xs
)
2653 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2654 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2658 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
2661 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
2662 down_read(&oi
->ip_alloc_sem
);
2663 has_space
= ocfs2_xattr_has_space_inline(inode
, di
);
2664 up_read(&oi
->ip_alloc_sem
);
2669 xs
->xattr_bh
= xs
->inode_bh
;
2670 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
2671 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)
2672 xs
->header
= (struct ocfs2_xattr_header
*)
2673 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
2675 xs
->header
= (struct ocfs2_xattr_header
*)
2676 (xs
->end
- OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
);
2677 xs
->base
= (void *)xs
->header
;
2678 xs
->here
= xs
->header
->xh_entries
;
2680 /* Find the named attribute. */
2681 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
2682 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
2683 if (ret
&& ret
!= -ENODATA
)
2685 xs
->not_found
= ret
;
2691 static int ocfs2_xattr_ibody_init(struct inode
*inode
,
2692 struct buffer_head
*di_bh
,
2693 struct ocfs2_xattr_set_ctxt
*ctxt
)
2696 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2697 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2698 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2699 unsigned int xattrsize
= osb
->s_xattr_inline_size
;
2701 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
2706 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
), di_bh
,
2707 OCFS2_JOURNAL_ACCESS_WRITE
);
2714 * Adjust extent record count or inline data size
2715 * to reserve space for extended attribute.
2717 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
2718 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
2719 le16_add_cpu(&idata
->id_count
, -xattrsize
);
2720 } else if (!(ocfs2_inode_is_fast_symlink(inode
))) {
2721 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
2722 le16_add_cpu(&el
->l_count
, -(xattrsize
/
2723 sizeof(struct ocfs2_extent_rec
)));
2725 di
->i_xattr_inline_size
= cpu_to_le16(xattrsize
);
2727 spin_lock(&oi
->ip_lock
);
2728 oi
->ip_dyn_features
|= OCFS2_INLINE_XATTR_FL
|OCFS2_HAS_XATTR_FL
;
2729 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
2730 spin_unlock(&oi
->ip_lock
);
2732 ocfs2_journal_dirty(ctxt
->handle
, di_bh
);
2739 * ocfs2_xattr_ibody_set()
2741 * Set, replace or remove an extended attribute into inode block.
2744 static int ocfs2_xattr_ibody_set(struct inode
*inode
,
2745 struct ocfs2_xattr_info
*xi
,
2746 struct ocfs2_xattr_search
*xs
,
2747 struct ocfs2_xattr_set_ctxt
*ctxt
)
2750 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2751 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2752 struct ocfs2_xa_loc loc
;
2754 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
2757 down_write(&oi
->ip_alloc_sem
);
2758 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
2759 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
2765 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
2766 ret
= ocfs2_xattr_ibody_init(inode
, xs
->inode_bh
, ctxt
);
2774 ocfs2_init_dinode_xa_loc(&loc
, inode
, xs
->inode_bh
,
2775 xs
->not_found
? NULL
: xs
->here
);
2776 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
2782 xs
->here
= loc
.xl_entry
;
2785 up_write(&oi
->ip_alloc_sem
);
2791 * ocfs2_xattr_block_find()
2793 * Find extended attribute in external block and
2794 * fill search info into struct ocfs2_xattr_search.
2796 static int ocfs2_xattr_block_find(struct inode
*inode
,
2799 struct ocfs2_xattr_search
*xs
)
2801 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2802 struct buffer_head
*blk_bh
= NULL
;
2803 struct ocfs2_xattr_block
*xb
;
2806 if (!di
->i_xattr_loc
)
2809 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
2816 xs
->xattr_bh
= blk_bh
;
2817 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2819 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2820 xs
->header
= &xb
->xb_attrs
.xb_header
;
2821 xs
->base
= (void *)xs
->header
;
2822 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
2823 xs
->here
= xs
->header
->xh_entries
;
2825 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
2827 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
2831 if (ret
&& ret
!= -ENODATA
) {
2832 xs
->xattr_bh
= NULL
;
2835 xs
->not_found
= ret
;
2843 static int ocfs2_create_xattr_block(struct inode
*inode
,
2844 struct buffer_head
*inode_bh
,
2845 struct ocfs2_xattr_set_ctxt
*ctxt
,
2847 struct buffer_head
**ret_bh
)
2850 u16 suballoc_bit_start
;
2852 u64 suballoc_loc
, first_blkno
;
2853 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)inode_bh
->b_data
;
2854 struct buffer_head
*new_bh
= NULL
;
2855 struct ocfs2_xattr_block
*xblk
;
2857 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
),
2858 inode_bh
, OCFS2_JOURNAL_ACCESS_CREATE
);
2864 ret
= ocfs2_claim_metadata(ctxt
->handle
, ctxt
->meta_ac
, 1,
2865 &suballoc_loc
, &suballoc_bit_start
,
2866 &num_got
, &first_blkno
);
2872 new_bh
= sb_getblk(inode
->i_sb
, first_blkno
);
2873 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode
), new_bh
);
2875 ret
= ocfs2_journal_access_xb(ctxt
->handle
, INODE_CACHE(inode
),
2877 OCFS2_JOURNAL_ACCESS_CREATE
);
2883 /* Initialize ocfs2_xattr_block */
2884 xblk
= (struct ocfs2_xattr_block
*)new_bh
->b_data
;
2885 memset(xblk
, 0, inode
->i_sb
->s_blocksize
);
2886 strcpy((void *)xblk
, OCFS2_XATTR_BLOCK_SIGNATURE
);
2887 xblk
->xb_suballoc_slot
= cpu_to_le16(ctxt
->meta_ac
->ac_alloc_slot
);
2888 xblk
->xb_suballoc_loc
= cpu_to_le64(suballoc_loc
);
2889 xblk
->xb_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
2890 xblk
->xb_fs_generation
=
2891 cpu_to_le32(OCFS2_SB(inode
->i_sb
)->fs_generation
);
2892 xblk
->xb_blkno
= cpu_to_le64(first_blkno
);
2894 struct ocfs2_xattr_tree_root
*xr
= &xblk
->xb_attrs
.xb_root
;
2895 xr
->xt_clusters
= cpu_to_le32(1);
2896 xr
->xt_last_eb_blk
= 0;
2897 xr
->xt_list
.l_tree_depth
= 0;
2898 xr
->xt_list
.l_count
= cpu_to_le16(
2899 ocfs2_xattr_recs_per_xb(inode
->i_sb
));
2900 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
2901 xblk
->xb_flags
= cpu_to_le16(OCFS2_XATTR_INDEXED
);
2903 ocfs2_journal_dirty(ctxt
->handle
, new_bh
);
2905 /* Add it to the inode */
2906 di
->i_xattr_loc
= cpu_to_le64(first_blkno
);
2908 spin_lock(&OCFS2_I(inode
)->ip_lock
);
2909 OCFS2_I(inode
)->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
;
2910 di
->i_dyn_features
= cpu_to_le16(OCFS2_I(inode
)->ip_dyn_features
);
2911 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
2913 ocfs2_journal_dirty(ctxt
->handle
, inode_bh
);
2924 * ocfs2_xattr_block_set()
2926 * Set, replace or remove an extended attribute into external block.
2929 static int ocfs2_xattr_block_set(struct inode
*inode
,
2930 struct ocfs2_xattr_info
*xi
,
2931 struct ocfs2_xattr_search
*xs
,
2932 struct ocfs2_xattr_set_ctxt
*ctxt
)
2934 struct buffer_head
*new_bh
= NULL
;
2935 struct ocfs2_xattr_block
*xblk
= NULL
;
2937 struct ocfs2_xa_loc loc
;
2939 if (!xs
->xattr_bh
) {
2940 ret
= ocfs2_create_xattr_block(inode
, xs
->inode_bh
, ctxt
,
2947 xs
->xattr_bh
= new_bh
;
2948 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
2949 xs
->header
= &xblk
->xb_attrs
.xb_header
;
2950 xs
->base
= (void *)xs
->header
;
2951 xs
->end
= (void *)xblk
+ inode
->i_sb
->s_blocksize
;
2952 xs
->here
= xs
->header
->xh_entries
;
2954 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
2956 if (!(le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2957 ocfs2_init_xattr_block_xa_loc(&loc
, inode
, xs
->xattr_bh
,
2958 xs
->not_found
? NULL
: xs
->here
);
2960 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
2962 xs
->here
= loc
.xl_entry
;
2963 else if ((ret
!= -ENOSPC
) || ctxt
->set_abort
)
2966 ret
= ocfs2_xattr_create_index_block(inode
, xs
, ctxt
);
2972 if (le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)
2973 ret
= ocfs2_xattr_set_entry_index_block(inode
, xi
, xs
, ctxt
);
2979 /* Check whether the new xattr can be inserted into the inode. */
2980 static int ocfs2_xattr_can_be_in_inode(struct inode
*inode
,
2981 struct ocfs2_xattr_info
*xi
,
2982 struct ocfs2_xattr_search
*xs
)
2984 struct ocfs2_xattr_entry
*last
;
2986 size_t min_offs
= xs
->end
- xs
->base
;
2991 last
= xs
->header
->xh_entries
;
2993 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
2994 size_t offs
= le16_to_cpu(last
->xe_name_offset
);
2995 if (offs
< min_offs
)
3000 free
= min_offs
- ((void *)last
- xs
->base
) - OCFS2_XATTR_HEADER_GAP
;
3004 BUG_ON(!xs
->not_found
);
3006 if (free
>= (sizeof(struct ocfs2_xattr_entry
) + namevalue_size_xi(xi
)))
3012 static int ocfs2_calc_xattr_set_need(struct inode
*inode
,
3013 struct ocfs2_dinode
*di
,
3014 struct ocfs2_xattr_info
*xi
,
3015 struct ocfs2_xattr_search
*xis
,
3016 struct ocfs2_xattr_search
*xbs
,
3021 int ret
= 0, old_in_xb
= 0;
3022 int clusters_add
= 0, meta_add
= 0, credits
= 0;
3023 struct buffer_head
*bh
= NULL
;
3024 struct ocfs2_xattr_block
*xb
= NULL
;
3025 struct ocfs2_xattr_entry
*xe
= NULL
;
3026 struct ocfs2_xattr_value_root
*xv
= NULL
;
3028 int name_offset
, name_len
= 0;
3029 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
3034 * Calculate the clusters we need to write.
3035 * No matter whether we replace an old one or add a new one,
3036 * we need this for writing.
3038 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
)
3039 credits
+= new_clusters
*
3040 ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3042 if (xis
->not_found
&& xbs
->not_found
) {
3043 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3045 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
3046 clusters_add
+= new_clusters
;
3047 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3055 if (!xis
->not_found
) {
3057 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3058 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3060 credits
+= OCFS2_INODE_UPDATE_CREDITS
;
3062 int i
, block_off
= 0;
3063 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
3065 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3066 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3067 i
= xbs
->here
- xbs
->header
->xh_entries
;
3070 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
3071 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
3072 bucket_xh(xbs
->bucket
),
3075 base
= bucket_block(xbs
->bucket
, block_off
);
3076 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3079 credits
+= OCFS2_XATTR_BLOCK_UPDATE_CREDITS
;
3084 * delete a xattr doesn't need metadata and cluster allocation.
3085 * so just calculate the credits and return.
3087 * The credits for removing the value tree will be extended
3088 * by ocfs2_remove_extent itself.
3090 if (!xi
->xi_value
) {
3091 if (!ocfs2_xattr_is_local(xe
))
3092 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
);
3097 /* do cluster allocation guess first. */
3098 value_size
= le64_to_cpu(xe
->xe_value_size
);
3102 * In xattr set, we always try to set the xe in inode first,
3103 * so if it can be inserted into inode successfully, the old
3104 * one will be removed from the xattr block, and this xattr
3105 * will be inserted into inode as a new xattr in inode.
3107 if (ocfs2_xattr_can_be_in_inode(inode
, xi
, xis
)) {
3108 clusters_add
+= new_clusters
;
3109 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
) +
3110 OCFS2_INODE_UPDATE_CREDITS
;
3111 if (!ocfs2_xattr_is_local(xe
))
3112 credits
+= ocfs2_calc_extend_credits(
3120 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
3121 /* the new values will be stored outside. */
3122 u32 old_clusters
= 0;
3124 if (!ocfs2_xattr_is_local(xe
)) {
3125 old_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
3127 xv
= (struct ocfs2_xattr_value_root
*)
3128 (base
+ name_offset
+ name_len
);
3129 value_size
= OCFS2_XATTR_ROOT_SIZE
;
3133 if (old_clusters
>= new_clusters
) {
3134 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
);
3137 meta_add
+= ocfs2_extend_meta_needed(&xv
->xr_list
);
3138 clusters_add
+= new_clusters
- old_clusters
;
3139 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3143 if (value_size
>= OCFS2_XATTR_ROOT_SIZE
)
3148 * Now the new value will be stored inside. So if the new
3149 * value is smaller than the size of value root or the old
3150 * value, we don't need any allocation, otherwise we have
3151 * to guess metadata allocation.
3153 if ((ocfs2_xattr_is_local(xe
) &&
3154 (value_size
>= xi
->xi_value_len
)) ||
3155 (!ocfs2_xattr_is_local(xe
) &&
3156 OCFS2_XATTR_ROOT_SIZE
>= xi
->xi_value_len
))
3161 /* calculate metadata allocation. */
3162 if (di
->i_xattr_loc
) {
3163 if (!xbs
->xattr_bh
) {
3164 ret
= ocfs2_read_xattr_block(inode
,
3165 le64_to_cpu(di
->i_xattr_loc
),
3172 xb
= (struct ocfs2_xattr_block
*)bh
->b_data
;
3174 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
3177 * If there is already an xattr tree, good, we can calculate
3178 * like other b-trees. Otherwise we may have the chance of
3179 * create a tree, the credit calculation is borrowed from
3180 * ocfs2_calc_extend_credits with root_el = NULL. And the
3181 * new tree will be cluster based, so no meta is needed.
3183 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
3184 struct ocfs2_extent_list
*el
=
3185 &xb
->xb_attrs
.xb_root
.xt_list
;
3186 meta_add
+= ocfs2_extend_meta_needed(el
);
3187 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3190 credits
+= OCFS2_SUBALLOC_ALLOC
+ 1;
3193 * This cluster will be used either for new bucket or for
3195 * If the cluster size is the same as the bucket size, one
3196 * more is needed since we may need to extend the bucket
3200 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3201 if (OCFS2_XATTR_BUCKET_SIZE
==
3202 OCFS2_SB(inode
->i_sb
)->s_clustersize
) {
3203 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3208 credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
3212 *clusters_need
= clusters_add
;
3214 *meta_need
= meta_add
;
3216 *credits_need
= credits
;
3221 static int ocfs2_init_xattr_set_ctxt(struct inode
*inode
,
3222 struct ocfs2_dinode
*di
,
3223 struct ocfs2_xattr_info
*xi
,
3224 struct ocfs2_xattr_search
*xis
,
3225 struct ocfs2_xattr_search
*xbs
,
3226 struct ocfs2_xattr_set_ctxt
*ctxt
,
3230 int clusters_add
, meta_add
, ret
;
3231 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3233 memset(ctxt
, 0, sizeof(struct ocfs2_xattr_set_ctxt
));
3235 ocfs2_init_dealloc_ctxt(&ctxt
->dealloc
);
3237 ret
= ocfs2_calc_xattr_set_need(inode
, di
, xi
, xis
, xbs
,
3238 &clusters_add
, &meta_add
, credits
);
3244 meta_add
+= extra_meta
;
3245 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
3246 "credits = %d\n", xi
->xi_name
, meta_add
, clusters_add
, *credits
);
3249 ret
= ocfs2_reserve_new_metadata_blocks(osb
, meta_add
,
3258 ret
= ocfs2_reserve_clusters(osb
, clusters_add
, &ctxt
->data_ac
);
3264 if (ctxt
->meta_ac
) {
3265 ocfs2_free_alloc_context(ctxt
->meta_ac
);
3266 ctxt
->meta_ac
= NULL
;
3270 * We cannot have an error and a non null ctxt->data_ac.
3277 static int __ocfs2_xattr_set_handle(struct inode
*inode
,
3278 struct ocfs2_dinode
*di
,
3279 struct ocfs2_xattr_info
*xi
,
3280 struct ocfs2_xattr_search
*xis
,
3281 struct ocfs2_xattr_search
*xbs
,
3282 struct ocfs2_xattr_set_ctxt
*ctxt
)
3284 int ret
= 0, credits
, old_found
;
3286 if (!xi
->xi_value
) {
3287 /* Remove existing extended attribute */
3288 if (!xis
->not_found
)
3289 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
3290 else if (!xbs
->not_found
)
3291 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3293 /* We always try to set extended attribute into inode first*/
3294 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
3295 if (!ret
&& !xbs
->not_found
) {
3297 * If succeed and that extended attribute existing in
3298 * external block, then we will remove it.
3300 xi
->xi_value
= NULL
;
3301 xi
->xi_value_len
= 0;
3303 old_found
= xis
->not_found
;
3304 xis
->not_found
= -ENODATA
;
3305 ret
= ocfs2_calc_xattr_set_need(inode
,
3313 xis
->not_found
= old_found
;
3319 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
);
3324 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3325 } else if ((ret
== -ENOSPC
) && !ctxt
->set_abort
) {
3326 if (di
->i_xattr_loc
&& !xbs
->xattr_bh
) {
3327 ret
= ocfs2_xattr_block_find(inode
,
3333 old_found
= xis
->not_found
;
3334 xis
->not_found
= -ENODATA
;
3335 ret
= ocfs2_calc_xattr_set_need(inode
,
3343 xis
->not_found
= old_found
;
3349 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
);
3356 * If no space in inode, we will set extended attribute
3357 * into external block.
3359 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3362 if (!xis
->not_found
) {
3364 * If succeed and that extended attribute
3365 * existing in inode, we will remove it.
3367 xi
->xi_value
= NULL
;
3368 xi
->xi_value_len
= 0;
3369 xbs
->not_found
= -ENODATA
;
3370 ret
= ocfs2_calc_xattr_set_need(inode
,
3383 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
);
3388 ret
= ocfs2_xattr_ibody_set(inode
, xi
,
3395 /* Update inode ctime. */
3396 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
),
3398 OCFS2_JOURNAL_ACCESS_WRITE
);
3404 inode
->i_ctime
= CURRENT_TIME
;
3405 di
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
3406 di
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
3407 ocfs2_journal_dirty(ctxt
->handle
, xis
->inode_bh
);
3414 * This function only called duing creating inode
3415 * for init security/acl xattrs of the new inode.
3416 * All transanction credits have been reserved in mknod.
3418 int ocfs2_xattr_set_handle(handle_t
*handle
,
3419 struct inode
*inode
,
3420 struct buffer_head
*di_bh
,
3426 struct ocfs2_alloc_context
*meta_ac
,
3427 struct ocfs2_alloc_context
*data_ac
)
3429 struct ocfs2_dinode
*di
;
3432 struct ocfs2_xattr_info xi
= {
3433 .xi_name_index
= name_index
,
3435 .xi_name_len
= strlen(name
),
3437 .xi_value_len
= value_len
,
3440 struct ocfs2_xattr_search xis
= {
3441 .not_found
= -ENODATA
,
3444 struct ocfs2_xattr_search xbs
= {
3445 .not_found
= -ENODATA
,
3448 struct ocfs2_xattr_set_ctxt ctxt
= {
3454 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
3458 * In extreme situation, may need xattr bucket when
3459 * block size is too small. And we have already reserved
3460 * the credits for bucket in mknod.
3462 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
) {
3463 xbs
.bucket
= ocfs2_xattr_bucket_new(inode
);
3465 mlog_errno(-ENOMEM
);
3470 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
3471 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3473 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3475 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
3478 if (xis
.not_found
) {
3479 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
3484 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
3487 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3488 brelse(xbs
.xattr_bh
);
3489 ocfs2_xattr_bucket_free(xbs
.bucket
);
3497 * Set, replace or remove an extended attribute for this inode.
3498 * value is NULL to remove an existing extended attribute, else either
3499 * create or replace an extended attribute.
3501 int ocfs2_xattr_set(struct inode
*inode
,
3508 struct buffer_head
*di_bh
= NULL
;
3509 struct ocfs2_dinode
*di
;
3510 int ret
, credits
, ref_meta
= 0, ref_credits
= 0;
3511 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3512 struct inode
*tl_inode
= osb
->osb_tl_inode
;
3513 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, };
3514 struct ocfs2_refcount_tree
*ref_tree
= NULL
;
3516 struct ocfs2_xattr_info xi
= {
3517 .xi_name_index
= name_index
,
3519 .xi_name_len
= strlen(name
),
3521 .xi_value_len
= value_len
,
3524 struct ocfs2_xattr_search xis
= {
3525 .not_found
= -ENODATA
,
3528 struct ocfs2_xattr_search xbs
= {
3529 .not_found
= -ENODATA
,
3532 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
3536 * Only xbs will be used on indexed trees. xis doesn't need a
3539 xbs
.bucket
= ocfs2_xattr_bucket_new(inode
);
3541 mlog_errno(-ENOMEM
);
3545 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
3548 goto cleanup_nolock
;
3550 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
3551 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3553 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3555 * Scan inode and external block to find the same name
3556 * extended attribute and collect search infomation.
3558 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
3561 if (xis
.not_found
) {
3562 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
3567 if (xis
.not_found
&& xbs
.not_found
) {
3569 if (flags
& XATTR_REPLACE
)
3576 if (flags
& XATTR_CREATE
)
3580 /* Check whether the value is refcounted and do some prepartion. */
3581 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
&&
3582 (!xis
.not_found
|| !xbs
.not_found
)) {
3583 ret
= ocfs2_prepare_refcount_xattr(inode
, di
, &xi
,
3584 &xis
, &xbs
, &ref_tree
,
3585 &ref_meta
, &ref_credits
);
3592 mutex_lock(&tl_inode
->i_mutex
);
3594 if (ocfs2_truncate_log_needs_flush(osb
)) {
3595 ret
= __ocfs2_flush_truncate_log(osb
);
3597 mutex_unlock(&tl_inode
->i_mutex
);
3602 mutex_unlock(&tl_inode
->i_mutex
);
3604 ret
= ocfs2_init_xattr_set_ctxt(inode
, di
, &xi
, &xis
,
3605 &xbs
, &ctxt
, ref_meta
, &credits
);
3611 /* we need to update inode's ctime field, so add credit for it. */
3612 credits
+= OCFS2_INODE_UPDATE_CREDITS
;
3613 ctxt
.handle
= ocfs2_start_trans(osb
, credits
+ ref_credits
);
3614 if (IS_ERR(ctxt
.handle
)) {
3615 ret
= PTR_ERR(ctxt
.handle
);
3620 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
3622 ocfs2_commit_trans(osb
, ctxt
.handle
);
3625 ocfs2_free_alloc_context(ctxt
.data_ac
);
3627 ocfs2_free_alloc_context(ctxt
.meta_ac
);
3628 if (ocfs2_dealloc_has_cluster(&ctxt
.dealloc
))
3629 ocfs2_schedule_truncate_log_flush(osb
, 1);
3630 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
3634 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
3635 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3636 if (!value
&& !ret
) {
3637 ret
= ocfs2_try_remove_refcount_tree(inode
, di_bh
);
3641 ocfs2_inode_unlock(inode
, 1);
3644 brelse(xbs
.xattr_bh
);
3645 ocfs2_xattr_bucket_free(xbs
.bucket
);
3651 * Find the xattr extent rec which may contains name_hash.
3652 * e_cpos will be the first name hash of the xattr rec.
3653 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3655 static int ocfs2_xattr_get_rec(struct inode
*inode
,
3660 struct ocfs2_extent_list
*el
)
3663 struct buffer_head
*eb_bh
= NULL
;
3664 struct ocfs2_extent_block
*eb
;
3665 struct ocfs2_extent_rec
*rec
= NULL
;
3668 if (el
->l_tree_depth
) {
3669 ret
= ocfs2_find_leaf(INODE_CACHE(inode
), el
, name_hash
,
3676 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
3679 if (el
->l_tree_depth
) {
3680 ocfs2_error(inode
->i_sb
,
3681 "Inode %lu has non zero tree depth in "
3682 "xattr tree block %llu\n", inode
->i_ino
,
3683 (unsigned long long)eb_bh
->b_blocknr
);
3689 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
3690 rec
= &el
->l_recs
[i
];
3692 if (le32_to_cpu(rec
->e_cpos
) <= name_hash
) {
3693 e_blkno
= le64_to_cpu(rec
->e_blkno
);
3699 ocfs2_error(inode
->i_sb
, "Inode %lu has bad extent "
3700 "record (%u, %u, 0) in xattr", inode
->i_ino
,
3701 le32_to_cpu(rec
->e_cpos
),
3702 ocfs2_rec_clusters(el
, rec
));
3707 *p_blkno
= le64_to_cpu(rec
->e_blkno
);
3708 *num_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
3710 *e_cpos
= le32_to_cpu(rec
->e_cpos
);
3716 typedef int (xattr_bucket_func
)(struct inode
*inode
,
3717 struct ocfs2_xattr_bucket
*bucket
,
3720 static int ocfs2_find_xe_in_bucket(struct inode
*inode
,
3721 struct ocfs2_xattr_bucket
*bucket
,
3728 int i
, ret
= 0, cmp
= 1, block_off
, new_offset
;
3729 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
3730 size_t name_len
= strlen(name
);
3731 struct ocfs2_xattr_entry
*xe
= NULL
;
3735 * We don't use binary search in the bucket because there
3736 * may be multiple entries with the same name hash.
3738 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
3739 xe
= &xh
->xh_entries
[i
];
3741 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
))
3743 else if (name_hash
< le32_to_cpu(xe
->xe_name_hash
))
3746 cmp
= name_index
- ocfs2_xattr_get_type(xe
);
3748 cmp
= name_len
- xe
->xe_name_len
;
3752 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
3763 xe_name
= bucket_block(bucket
, block_off
) + new_offset
;
3764 if (!memcmp(name
, xe_name
, name_len
)) {
3776 * Find the specified xattr entry in a series of buckets.
3777 * This series start from p_blkno and last for num_clusters.
3778 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3779 * the num of the valid buckets.
3781 * Return the buffer_head this xattr should reside in. And if the xattr's
3782 * hash is in the gap of 2 buckets, return the lower bucket.
3784 static int ocfs2_xattr_bucket_find(struct inode
*inode
,
3791 struct ocfs2_xattr_search
*xs
)
3794 struct ocfs2_xattr_header
*xh
= NULL
;
3795 struct ocfs2_xattr_entry
*xe
= NULL
;
3797 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3798 int low_bucket
= 0, bucket
, high_bucket
;
3799 struct ocfs2_xattr_bucket
*search
;
3801 u64 blkno
, lower_blkno
= 0;
3803 search
= ocfs2_xattr_bucket_new(inode
);
3810 ret
= ocfs2_read_xattr_bucket(search
, p_blkno
);
3816 xh
= bucket_xh(search
);
3817 high_bucket
= le16_to_cpu(xh
->xh_num_buckets
) - 1;
3818 while (low_bucket
<= high_bucket
) {
3819 ocfs2_xattr_bucket_relse(search
);
3821 bucket
= (low_bucket
+ high_bucket
) / 2;
3822 blkno
= p_blkno
+ bucket
* blk_per_bucket
;
3823 ret
= ocfs2_read_xattr_bucket(search
, blkno
);
3829 xh
= bucket_xh(search
);
3830 xe
= &xh
->xh_entries
[0];
3831 if (name_hash
< le32_to_cpu(xe
->xe_name_hash
)) {
3832 high_bucket
= bucket
- 1;
3837 * Check whether the hash of the last entry in our
3838 * bucket is larger than the search one. for an empty
3839 * bucket, the last one is also the first one.
3842 xe
= &xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1];
3844 last_hash
= le32_to_cpu(xe
->xe_name_hash
);
3846 /* record lower_blkno which may be the insert place. */
3847 lower_blkno
= blkno
;
3849 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
)) {
3850 low_bucket
= bucket
+ 1;
3854 /* the searched xattr should reside in this bucket if exists. */
3855 ret
= ocfs2_find_xe_in_bucket(inode
, search
,
3856 name_index
, name
, name_hash
,
3866 * Record the bucket we have found.
3867 * When the xattr's hash value is in the gap of 2 buckets, we will
3868 * always set it to the previous bucket.
3871 lower_blkno
= p_blkno
;
3873 /* This should be in cache - we just read it during the search */
3874 ret
= ocfs2_read_xattr_bucket(xs
->bucket
, lower_blkno
);
3880 xs
->header
= bucket_xh(xs
->bucket
);
3881 xs
->base
= bucket_block(xs
->bucket
, 0);
3882 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
3885 xs
->here
= &xs
->header
->xh_entries
[index
];
3886 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name
,
3887 (unsigned long long)bucket_blkno(xs
->bucket
), index
);
3892 ocfs2_xattr_bucket_free(search
);
3896 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
3897 struct buffer_head
*root_bh
,
3900 struct ocfs2_xattr_search
*xs
)
3903 struct ocfs2_xattr_block
*xb
=
3904 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
3905 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
3906 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
3908 u32 first_hash
, num_clusters
= 0;
3909 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
3911 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
3914 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3915 name
, name_hash
, name_index
);
3917 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &first_hash
,
3924 BUG_ON(p_blkno
== 0 || num_clusters
== 0 || first_hash
> name_hash
);
3926 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3927 "in the rec is %u\n", num_clusters
, (unsigned long long)p_blkno
,
3930 ret
= ocfs2_xattr_bucket_find(inode
, name_index
, name
, name_hash
,
3931 p_blkno
, first_hash
, num_clusters
, xs
);
3937 static int ocfs2_iterate_xattr_buckets(struct inode
*inode
,
3940 xattr_bucket_func
*func
,
3944 u32 bpc
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
3945 u32 num_buckets
= clusters
* bpc
;
3946 struct ocfs2_xattr_bucket
*bucket
;
3948 bucket
= ocfs2_xattr_bucket_new(inode
);
3950 mlog_errno(-ENOMEM
);
3954 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3955 clusters
, (unsigned long long)blkno
);
3957 for (i
= 0; i
< num_buckets
; i
++, blkno
+= bucket
->bu_blocks
) {
3958 ret
= ocfs2_read_xattr_bucket(bucket
, blkno
);
3965 * The real bucket num in this series of blocks is stored
3966 * in the 1st bucket.
3969 num_buckets
= le16_to_cpu(bucket_xh(bucket
)->xh_num_buckets
);
3971 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3972 (unsigned long long)blkno
,
3973 le32_to_cpu(bucket_xh(bucket
)->xh_entries
[0].xe_name_hash
));
3975 ret
= func(inode
, bucket
, para
);
3976 if (ret
&& ret
!= -ERANGE
)
3978 /* Fall through to bucket_relse() */
3981 ocfs2_xattr_bucket_relse(bucket
);
3986 ocfs2_xattr_bucket_free(bucket
);
3990 struct ocfs2_xattr_tree_list
{
3996 static int ocfs2_xattr_bucket_get_name_value(struct super_block
*sb
,
3997 struct ocfs2_xattr_header
*xh
,
4004 if (index
< 0 || index
>= le16_to_cpu(xh
->xh_count
))
4007 name_offset
= le16_to_cpu(xh
->xh_entries
[index
].xe_name_offset
);
4009 *block_off
= name_offset
>> sb
->s_blocksize_bits
;
4010 *new_offset
= name_offset
% sb
->s_blocksize
;
4015 static int ocfs2_list_xattr_bucket(struct inode
*inode
,
4016 struct ocfs2_xattr_bucket
*bucket
,
4020 struct ocfs2_xattr_tree_list
*xl
= (struct ocfs2_xattr_tree_list
*)para
;
4021 int i
, block_off
, new_offset
;
4022 const char *prefix
, *name
;
4024 for (i
= 0 ; i
< le16_to_cpu(bucket_xh(bucket
)->xh_count
); i
++) {
4025 struct ocfs2_xattr_entry
*entry
= &bucket_xh(bucket
)->xh_entries
[i
];
4026 type
= ocfs2_xattr_get_type(entry
);
4027 prefix
= ocfs2_xattr_prefix(type
);
4030 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
4038 name
= (const char *)bucket_block(bucket
, block_off
) +
4040 ret
= ocfs2_xattr_list_entry(xl
->buffer
,
4044 entry
->xe_name_len
);
4053 static int ocfs2_iterate_xattr_index_block(struct inode
*inode
,
4054 struct buffer_head
*blk_bh
,
4055 xattr_tree_rec_func
*rec_func
,
4058 struct ocfs2_xattr_block
*xb
=
4059 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
4060 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
4062 u32 name_hash
= UINT_MAX
, e_cpos
= 0, num_clusters
= 0;
4065 if (!el
->l_next_free_rec
|| !rec_func
)
4068 while (name_hash
> 0) {
4069 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
4070 &e_cpos
, &num_clusters
, el
);
4076 ret
= rec_func(inode
, blk_bh
, p_blkno
, e_cpos
,
4077 num_clusters
, para
);
4087 name_hash
= e_cpos
- 1;
4094 static int ocfs2_list_xattr_tree_rec(struct inode
*inode
,
4095 struct buffer_head
*root_bh
,
4096 u64 blkno
, u32 cpos
, u32 len
, void *para
)
4098 return ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
4099 ocfs2_list_xattr_bucket
, para
);
4102 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
4103 struct buffer_head
*blk_bh
,
4108 struct ocfs2_xattr_tree_list xl
= {
4110 .buffer_size
= buffer_size
,
4114 ret
= ocfs2_iterate_xattr_index_block(inode
, blk_bh
,
4115 ocfs2_list_xattr_tree_rec
, &xl
);
4126 static int cmp_xe(const void *a
, const void *b
)
4128 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
4129 u32 l_hash
= le32_to_cpu(l
->xe_name_hash
);
4130 u32 r_hash
= le32_to_cpu(r
->xe_name_hash
);
4132 if (l_hash
> r_hash
)
4134 if (l_hash
< r_hash
)
4139 static void swap_xe(void *a
, void *b
, int size
)
4141 struct ocfs2_xattr_entry
*l
= a
, *r
= b
, tmp
;
4144 memcpy(l
, r
, sizeof(struct ocfs2_xattr_entry
));
4145 memcpy(r
, &tmp
, sizeof(struct ocfs2_xattr_entry
));
4149 * When the ocfs2_xattr_block is filled up, new bucket will be created
4150 * and all the xattr entries will be moved to the new bucket.
4151 * The header goes at the start of the bucket, and the names+values are
4152 * filled from the end. This is why *target starts as the last buffer.
4153 * Note: we need to sort the entries since they are not saved in order
4154 * in the ocfs2_xattr_block.
4156 static void ocfs2_cp_xattr_block_to_bucket(struct inode
*inode
,
4157 struct buffer_head
*xb_bh
,
4158 struct ocfs2_xattr_bucket
*bucket
)
4160 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
4161 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4162 u16 offset
, size
, off_change
;
4163 struct ocfs2_xattr_entry
*xe
;
4164 struct ocfs2_xattr_block
*xb
=
4165 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4166 struct ocfs2_xattr_header
*xb_xh
= &xb
->xb_attrs
.xb_header
;
4167 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
4168 u16 count
= le16_to_cpu(xb_xh
->xh_count
);
4169 char *src
= xb_bh
->b_data
;
4170 char *target
= bucket_block(bucket
, blks
- 1);
4172 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4173 (unsigned long long)xb_bh
->b_blocknr
,
4174 (unsigned long long)bucket_blkno(bucket
));
4176 for (i
= 0; i
< blks
; i
++)
4177 memset(bucket_block(bucket
, i
), 0, blocksize
);
4180 * Since the xe_name_offset is based on ocfs2_xattr_header,
4181 * there is a offset change corresponding to the change of
4182 * ocfs2_xattr_header's position.
4184 off_change
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
4185 xe
= &xb_xh
->xh_entries
[count
- 1];
4186 offset
= le16_to_cpu(xe
->xe_name_offset
) + off_change
;
4187 size
= blocksize
- offset
;
4189 /* copy all the names and values. */
4190 memcpy(target
+ offset
, src
+ offset
, size
);
4192 /* Init new header now. */
4193 xh
->xh_count
= xb_xh
->xh_count
;
4194 xh
->xh_num_buckets
= cpu_to_le16(1);
4195 xh
->xh_name_value_len
= cpu_to_le16(size
);
4196 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
- size
);
4198 /* copy all the entries. */
4199 target
= bucket_block(bucket
, 0);
4200 offset
= offsetof(struct ocfs2_xattr_header
, xh_entries
);
4201 size
= count
* sizeof(struct ocfs2_xattr_entry
);
4202 memcpy(target
+ offset
, (char *)xb_xh
+ offset
, size
);
4204 /* Change the xe offset for all the xe because of the move. */
4205 off_change
= OCFS2_XATTR_BUCKET_SIZE
- blocksize
+
4206 offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
4207 for (i
= 0; i
< count
; i
++)
4208 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
, off_change
);
4210 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4211 offset
, size
, off_change
);
4213 sort(target
+ offset
, count
, sizeof(struct ocfs2_xattr_entry
),
4218 * After we move xattr from block to index btree, we have to
4219 * update ocfs2_xattr_search to the new xe and base.
4221 * When the entry is in xattr block, xattr_bh indicates the storage place.
4222 * While if the entry is in index b-tree, "bucket" indicates the
4223 * real place of the xattr.
4225 static void ocfs2_xattr_update_xattr_search(struct inode
*inode
,
4226 struct ocfs2_xattr_search
*xs
,
4227 struct buffer_head
*old_bh
)
4229 char *buf
= old_bh
->b_data
;
4230 struct ocfs2_xattr_block
*old_xb
= (struct ocfs2_xattr_block
*)buf
;
4231 struct ocfs2_xattr_header
*old_xh
= &old_xb
->xb_attrs
.xb_header
;
4234 xs
->header
= bucket_xh(xs
->bucket
);
4235 xs
->base
= bucket_block(xs
->bucket
, 0);
4236 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
4241 i
= xs
->here
- old_xh
->xh_entries
;
4242 xs
->here
= &xs
->header
->xh_entries
[i
];
4245 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
4246 struct ocfs2_xattr_search
*xs
,
4247 struct ocfs2_xattr_set_ctxt
*ctxt
)
4252 handle_t
*handle
= ctxt
->handle
;
4253 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
4254 struct buffer_head
*xb_bh
= xs
->xattr_bh
;
4255 struct ocfs2_xattr_block
*xb
=
4256 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4257 struct ocfs2_xattr_tree_root
*xr
;
4258 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
4260 mlog(0, "create xattr index block for %llu\n",
4261 (unsigned long long)xb_bh
->b_blocknr
);
4263 BUG_ON(xb_flags
& OCFS2_XATTR_INDEXED
);
4264 BUG_ON(!xs
->bucket
);
4266 down_write(&oi
->ip_alloc_sem
);
4268 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), xb_bh
,
4269 OCFS2_JOURNAL_ACCESS_WRITE
);
4275 ret
= __ocfs2_claim_clusters(handle
, ctxt
->data_ac
,
4276 1, 1, &bit_off
, &len
);
4283 * The bucket may spread in many blocks, and
4284 * we will only touch the 1st block and the last block
4285 * in the whole bucket(one for entry and one for data).
4287 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
4289 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4290 (unsigned long long)blkno
);
4292 ret
= ocfs2_init_xattr_bucket(xs
->bucket
, blkno
);
4298 ret
= ocfs2_xattr_bucket_journal_access(handle
, xs
->bucket
,
4299 OCFS2_JOURNAL_ACCESS_CREATE
);
4305 ocfs2_cp_xattr_block_to_bucket(inode
, xb_bh
, xs
->bucket
);
4306 ocfs2_xattr_bucket_journal_dirty(handle
, xs
->bucket
);
4308 ocfs2_xattr_update_xattr_search(inode
, xs
, xb_bh
);
4310 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4311 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
4312 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
4314 xr
= &xb
->xb_attrs
.xb_root
;
4315 xr
->xt_clusters
= cpu_to_le32(1);
4316 xr
->xt_last_eb_blk
= 0;
4317 xr
->xt_list
.l_tree_depth
= 0;
4318 xr
->xt_list
.l_count
= cpu_to_le16(ocfs2_xattr_recs_per_xb(inode
->i_sb
));
4319 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
4321 xr
->xt_list
.l_recs
[0].e_cpos
= 0;
4322 xr
->xt_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
4323 xr
->xt_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
4325 xb
->xb_flags
= cpu_to_le16(xb_flags
| OCFS2_XATTR_INDEXED
);
4327 ocfs2_journal_dirty(handle
, xb_bh
);
4330 up_write(&oi
->ip_alloc_sem
);
4335 static int cmp_xe_offset(const void *a
, const void *b
)
4337 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
4338 u32 l_name_offset
= le16_to_cpu(l
->xe_name_offset
);
4339 u32 r_name_offset
= le16_to_cpu(r
->xe_name_offset
);
4341 if (l_name_offset
< r_name_offset
)
4343 if (l_name_offset
> r_name_offset
)
4349 * defrag a xattr bucket if we find that the bucket has some
4350 * holes beteen name/value pairs.
4351 * We will move all the name/value pairs to the end of the bucket
4352 * so that we can spare some space for insertion.
4354 static int ocfs2_defrag_xattr_bucket(struct inode
*inode
,
4356 struct ocfs2_xattr_bucket
*bucket
)
4359 size_t end
, offset
, len
;
4360 struct ocfs2_xattr_header
*xh
;
4361 char *entries
, *buf
, *bucket_buf
= NULL
;
4362 u64 blkno
= bucket_blkno(bucket
);
4364 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4365 struct ocfs2_xattr_entry
*xe
;
4368 * In order to make the operation more efficient and generic,
4369 * we copy all the blocks into a contiguous memory and do the
4370 * defragment there, so if anything is error, we will not touch
4373 bucket_buf
= kmalloc(OCFS2_XATTR_BUCKET_SIZE
, GFP_NOFS
);
4380 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
4381 memcpy(buf
, bucket_block(bucket
, i
), blocksize
);
4383 ret
= ocfs2_xattr_bucket_journal_access(handle
, bucket
,
4384 OCFS2_JOURNAL_ACCESS_WRITE
);
4390 xh
= (struct ocfs2_xattr_header
*)bucket_buf
;
4391 entries
= (char *)xh
->xh_entries
;
4392 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4394 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4395 "xh_free_start = %u, xh_name_value_len = %u.\n",
4396 (unsigned long long)blkno
, le16_to_cpu(xh
->xh_count
),
4397 xh_free_start
, le16_to_cpu(xh
->xh_name_value_len
));
4400 * sort all the entries by their offset.
4401 * the largest will be the first, so that we can
4402 * move them to the end one by one.
4404 sort(entries
, le16_to_cpu(xh
->xh_count
),
4405 sizeof(struct ocfs2_xattr_entry
),
4406 cmp_xe_offset
, swap_xe
);
4408 /* Move all name/values to the end of the bucket. */
4409 xe
= xh
->xh_entries
;
4410 end
= OCFS2_XATTR_BUCKET_SIZE
;
4411 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, xe
++) {
4412 offset
= le16_to_cpu(xe
->xe_name_offset
);
4413 len
= namevalue_size_xe(xe
);
4416 * We must make sure that the name/value pair
4417 * exist in the same block. So adjust end to
4418 * the previous block end if needed.
4420 if (((end
- len
) / blocksize
!=
4421 (end
- 1) / blocksize
))
4422 end
= end
- end
% blocksize
;
4424 if (end
> offset
+ len
) {
4425 memmove(bucket_buf
+ end
- len
,
4426 bucket_buf
+ offset
, len
);
4427 xe
->xe_name_offset
= cpu_to_le16(end
- len
);
4430 mlog_bug_on_msg(end
< offset
+ len
, "Defrag check failed for "
4431 "bucket %llu\n", (unsigned long long)blkno
);
4436 mlog_bug_on_msg(xh_free_start
> end
, "Defrag check failed for "
4437 "bucket %llu\n", (unsigned long long)blkno
);
4439 if (xh_free_start
== end
)
4442 memset(bucket_buf
+ xh_free_start
, 0, end
- xh_free_start
);
4443 xh
->xh_free_start
= cpu_to_le16(end
);
4445 /* sort the entries by their name_hash. */
4446 sort(entries
, le16_to_cpu(xh
->xh_count
),
4447 sizeof(struct ocfs2_xattr_entry
),
4451 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
4452 memcpy(bucket_block(bucket
, i
), buf
, blocksize
);
4453 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
4461 * prev_blkno points to the start of an existing extent. new_blkno
4462 * points to a newly allocated extent. Because we know each of our
4463 * clusters contains more than bucket, we can easily split one cluster
4464 * at a bucket boundary. So we take the last cluster of the existing
4465 * extent and split it down the middle. We move the last half of the
4466 * buckets in the last cluster of the existing extent over to the new
4469 * first_bh is the buffer at prev_blkno so we can update the existing
4470 * extent's bucket count. header_bh is the bucket were we were hoping
4471 * to insert our xattr. If the bucket move places the target in the new
4472 * extent, we'll update first_bh and header_bh after modifying the old
4475 * first_hash will be set as the 1st xe's name_hash in the new extent.
4477 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode
*inode
,
4479 struct ocfs2_xattr_bucket
*first
,
4480 struct ocfs2_xattr_bucket
*target
,
4486 struct super_block
*sb
= inode
->i_sb
;
4487 int blks_per_bucket
= ocfs2_blocks_per_xattr_bucket(sb
);
4488 int num_buckets
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb
));
4489 int to_move
= num_buckets
/ 2;
4491 u64 last_cluster_blkno
= bucket_blkno(first
) +
4492 ((num_clusters
- 1) * ocfs2_clusters_to_blocks(sb
, 1));
4494 BUG_ON(le16_to_cpu(bucket_xh(first
)->xh_num_buckets
) < num_buckets
);
4495 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
== OCFS2_SB(sb
)->s_clustersize
);
4497 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4498 (unsigned long long)last_cluster_blkno
, (unsigned long long)new_blkno
);
4500 ret
= ocfs2_mv_xattr_buckets(inode
, handle
, bucket_blkno(first
),
4501 last_cluster_blkno
, new_blkno
,
4502 to_move
, first_hash
);
4508 /* This is the first bucket that got moved */
4509 src_blkno
= last_cluster_blkno
+ (to_move
* blks_per_bucket
);
4512 * If the target bucket was part of the moved buckets, we need to
4513 * update first and target.
4515 if (bucket_blkno(target
) >= src_blkno
) {
4516 /* Find the block for the new target bucket */
4517 src_blkno
= new_blkno
+
4518 (bucket_blkno(target
) - src_blkno
);
4520 ocfs2_xattr_bucket_relse(first
);
4521 ocfs2_xattr_bucket_relse(target
);
4524 * These shouldn't fail - the buffers are in the
4525 * journal from ocfs2_cp_xattr_bucket().
4527 ret
= ocfs2_read_xattr_bucket(first
, new_blkno
);
4532 ret
= ocfs2_read_xattr_bucket(target
, src_blkno
);
4543 * Find the suitable pos when we divide a bucket into 2.
4544 * We have to make sure the xattrs with the same hash value exist
4545 * in the same bucket.
4547 * If this ocfs2_xattr_header covers more than one hash value, find a
4548 * place where the hash value changes. Try to find the most even split.
4549 * The most common case is that all entries have different hash values,
4550 * and the first check we make will find a place to split.
4552 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header
*xh
)
4554 struct ocfs2_xattr_entry
*entries
= xh
->xh_entries
;
4555 int count
= le16_to_cpu(xh
->xh_count
);
4556 int delta
, middle
= count
/ 2;
4559 * We start at the middle. Each step gets farther away in both
4560 * directions. We therefore hit the change in hash value
4561 * nearest to the middle. Note that this loop does not execute for
4564 for (delta
= 0; delta
< middle
; delta
++) {
4565 /* Let's check delta earlier than middle */
4566 if (cmp_xe(&entries
[middle
- delta
- 1],
4567 &entries
[middle
- delta
]))
4568 return middle
- delta
;
4570 /* For even counts, don't walk off the end */
4571 if ((middle
+ delta
+ 1) == count
)
4574 /* Now try delta past middle */
4575 if (cmp_xe(&entries
[middle
+ delta
],
4576 &entries
[middle
+ delta
+ 1]))
4577 return middle
+ delta
+ 1;
4580 /* Every entry had the same hash */
4585 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4586 * first_hash will record the 1st hash of the new bucket.
4588 * Normally half of the xattrs will be moved. But we have to make
4589 * sure that the xattrs with the same hash value are stored in the
4590 * same bucket. If all the xattrs in this bucket have the same hash
4591 * value, the new bucket will be initialized as an empty one and the
4592 * first_hash will be initialized as (hash_value+1).
4594 static int ocfs2_divide_xattr_bucket(struct inode
*inode
,
4599 int new_bucket_head
)
4602 int count
, start
, len
, name_value_len
= 0, name_offset
= 0;
4603 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
4604 struct ocfs2_xattr_header
*xh
;
4605 struct ocfs2_xattr_entry
*xe
;
4606 int blocksize
= inode
->i_sb
->s_blocksize
;
4608 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4609 (unsigned long long)blk
, (unsigned long long)new_blk
);
4611 s_bucket
= ocfs2_xattr_bucket_new(inode
);
4612 t_bucket
= ocfs2_xattr_bucket_new(inode
);
4613 if (!s_bucket
|| !t_bucket
) {
4619 ret
= ocfs2_read_xattr_bucket(s_bucket
, blk
);
4625 ret
= ocfs2_xattr_bucket_journal_access(handle
, s_bucket
,
4626 OCFS2_JOURNAL_ACCESS_WRITE
);
4633 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4634 * there's no need to read it.
4636 ret
= ocfs2_init_xattr_bucket(t_bucket
, new_blk
);
4643 * Hey, if we're overwriting t_bucket, what difference does
4644 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4645 * same part of ocfs2_cp_xattr_bucket().
4647 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
4649 OCFS2_JOURNAL_ACCESS_CREATE
:
4650 OCFS2_JOURNAL_ACCESS_WRITE
);
4656 xh
= bucket_xh(s_bucket
);
4657 count
= le16_to_cpu(xh
->xh_count
);
4658 start
= ocfs2_xattr_find_divide_pos(xh
);
4660 if (start
== count
) {
4661 xe
= &xh
->xh_entries
[start
-1];
4664 * initialized a new empty bucket here.
4665 * The hash value is set as one larger than
4666 * that of the last entry in the previous bucket.
4668 for (i
= 0; i
< t_bucket
->bu_blocks
; i
++)
4669 memset(bucket_block(t_bucket
, i
), 0, blocksize
);
4671 xh
= bucket_xh(t_bucket
);
4672 xh
->xh_free_start
= cpu_to_le16(blocksize
);
4673 xh
->xh_entries
[0].xe_name_hash
= xe
->xe_name_hash
;
4674 le32_add_cpu(&xh
->xh_entries
[0].xe_name_hash
, 1);
4676 goto set_num_buckets
;
4679 /* copy the whole bucket to the new first. */
4680 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
4682 /* update the new bucket. */
4683 xh
= bucket_xh(t_bucket
);
4686 * Calculate the total name/value len and xh_free_start for
4687 * the old bucket first.
4689 name_offset
= OCFS2_XATTR_BUCKET_SIZE
;
4691 for (i
= 0; i
< start
; i
++) {
4692 xe
= &xh
->xh_entries
[i
];
4693 name_value_len
+= namevalue_size_xe(xe
);
4694 if (le16_to_cpu(xe
->xe_name_offset
) < name_offset
)
4695 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
4699 * Now begin the modification to the new bucket.
4701 * In the new bucket, We just move the xattr entry to the beginning
4702 * and don't touch the name/value. So there will be some holes in the
4703 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4706 xe
= &xh
->xh_entries
[start
];
4707 len
= sizeof(struct ocfs2_xattr_entry
) * (count
- start
);
4708 mlog(0, "mv xattr entry len %d from %d to %d\n", len
,
4709 (int)((char *)xe
- (char *)xh
),
4710 (int)((char *)xh
->xh_entries
- (char *)xh
));
4711 memmove((char *)xh
->xh_entries
, (char *)xe
, len
);
4712 xe
= &xh
->xh_entries
[count
- start
];
4713 len
= sizeof(struct ocfs2_xattr_entry
) * start
;
4714 memset((char *)xe
, 0, len
);
4716 le16_add_cpu(&xh
->xh_count
, -start
);
4717 le16_add_cpu(&xh
->xh_name_value_len
, -name_value_len
);
4719 /* Calculate xh_free_start for the new bucket. */
4720 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
4721 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
4722 xe
= &xh
->xh_entries
[i
];
4723 if (le16_to_cpu(xe
->xe_name_offset
) <
4724 le16_to_cpu(xh
->xh_free_start
))
4725 xh
->xh_free_start
= xe
->xe_name_offset
;
4729 /* set xh->xh_num_buckets for the new xh. */
4730 if (new_bucket_head
)
4731 xh
->xh_num_buckets
= cpu_to_le16(1);
4733 xh
->xh_num_buckets
= 0;
4735 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
4737 /* store the first_hash of the new bucket. */
4739 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
4742 * Now only update the 1st block of the old bucket. If we
4743 * just added a new empty bucket, there is no need to modify
4749 xh
= bucket_xh(s_bucket
);
4750 memset(&xh
->xh_entries
[start
], 0,
4751 sizeof(struct ocfs2_xattr_entry
) * (count
- start
));
4752 xh
->xh_count
= cpu_to_le16(start
);
4753 xh
->xh_free_start
= cpu_to_le16(name_offset
);
4754 xh
->xh_name_value_len
= cpu_to_le16(name_value_len
);
4756 ocfs2_xattr_bucket_journal_dirty(handle
, s_bucket
);
4759 ocfs2_xattr_bucket_free(s_bucket
);
4760 ocfs2_xattr_bucket_free(t_bucket
);
4766 * Copy xattr from one bucket to another bucket.
4768 * The caller must make sure that the journal transaction
4769 * has enough space for journaling.
4771 static int ocfs2_cp_xattr_bucket(struct inode
*inode
,
4778 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
4780 BUG_ON(s_blkno
== t_blkno
);
4782 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4783 (unsigned long long)s_blkno
, (unsigned long long)t_blkno
,
4786 s_bucket
= ocfs2_xattr_bucket_new(inode
);
4787 t_bucket
= ocfs2_xattr_bucket_new(inode
);
4788 if (!s_bucket
|| !t_bucket
) {
4794 ret
= ocfs2_read_xattr_bucket(s_bucket
, s_blkno
);
4799 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4800 * there's no need to read it.
4802 ret
= ocfs2_init_xattr_bucket(t_bucket
, t_blkno
);
4807 * Hey, if we're overwriting t_bucket, what difference does
4808 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4809 * cluster to fill, we came here from
4810 * ocfs2_mv_xattr_buckets(), and it is really new -
4811 * ACCESS_CREATE is required. But we also might have moved data
4812 * out of t_bucket before extending back into it.
4813 * ocfs2_add_new_xattr_bucket() can do this - its call to
4814 * ocfs2_add_new_xattr_cluster() may have created a new extent
4815 * and copied out the end of the old extent. Then it re-extends
4816 * the old extent back to create space for new xattrs. That's
4817 * how we get here, and the bucket isn't really new.
4819 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
4821 OCFS2_JOURNAL_ACCESS_CREATE
:
4822 OCFS2_JOURNAL_ACCESS_WRITE
);
4826 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
4827 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
4830 ocfs2_xattr_bucket_free(t_bucket
);
4831 ocfs2_xattr_bucket_free(s_bucket
);
4837 * src_blk points to the start of an existing extent. last_blk points to
4838 * last cluster in that extent. to_blk points to a newly allocated
4839 * extent. We copy the buckets from the cluster at last_blk to the new
4840 * extent. If start_bucket is non-zero, we skip that many buckets before
4841 * we start copying. The new extent's xh_num_buckets gets set to the
4842 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4843 * by the same amount.
4845 static int ocfs2_mv_xattr_buckets(struct inode
*inode
, handle_t
*handle
,
4846 u64 src_blk
, u64 last_blk
, u64 to_blk
,
4847 unsigned int start_bucket
,
4850 int i
, ret
, credits
;
4851 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4852 int blks_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4853 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
4854 struct ocfs2_xattr_bucket
*old_first
, *new_first
;
4856 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4857 (unsigned long long)last_blk
, (unsigned long long)to_blk
);
4859 BUG_ON(start_bucket
>= num_buckets
);
4861 num_buckets
-= start_bucket
;
4862 last_blk
+= (start_bucket
* blks_per_bucket
);
4865 /* The first bucket of the original extent */
4866 old_first
= ocfs2_xattr_bucket_new(inode
);
4867 /* The first bucket of the new extent */
4868 new_first
= ocfs2_xattr_bucket_new(inode
);
4869 if (!old_first
|| !new_first
) {
4875 ret
= ocfs2_read_xattr_bucket(old_first
, src_blk
);
4882 * We need to update the first bucket of the old extent and all
4883 * the buckets going to the new extent.
4885 credits
= ((num_buckets
+ 1) * blks_per_bucket
);
4886 ret
= ocfs2_extend_trans(handle
, credits
);
4892 ret
= ocfs2_xattr_bucket_journal_access(handle
, old_first
,
4893 OCFS2_JOURNAL_ACCESS_WRITE
);
4899 for (i
= 0; i
< num_buckets
; i
++) {
4900 ret
= ocfs2_cp_xattr_bucket(inode
, handle
,
4901 last_blk
+ (i
* blks_per_bucket
),
4902 to_blk
+ (i
* blks_per_bucket
),
4911 * Get the new bucket ready before we dirty anything
4912 * (This actually shouldn't fail, because we already dirtied
4913 * it once in ocfs2_cp_xattr_bucket()).
4915 ret
= ocfs2_read_xattr_bucket(new_first
, to_blk
);
4920 ret
= ocfs2_xattr_bucket_journal_access(handle
, new_first
,
4921 OCFS2_JOURNAL_ACCESS_WRITE
);
4927 /* Now update the headers */
4928 le16_add_cpu(&bucket_xh(old_first
)->xh_num_buckets
, -num_buckets
);
4929 ocfs2_xattr_bucket_journal_dirty(handle
, old_first
);
4931 bucket_xh(new_first
)->xh_num_buckets
= cpu_to_le16(num_buckets
);
4932 ocfs2_xattr_bucket_journal_dirty(handle
, new_first
);
4935 *first_hash
= le32_to_cpu(bucket_xh(new_first
)->xh_entries
[0].xe_name_hash
);
4938 ocfs2_xattr_bucket_free(new_first
);
4939 ocfs2_xattr_bucket_free(old_first
);
4944 * Move some xattrs in this cluster to the new cluster.
4945 * This function should only be called when bucket size == cluster size.
4946 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4948 static int ocfs2_divide_xattr_cluster(struct inode
*inode
,
4954 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4955 int ret
, credits
= 2 * blk_per_bucket
;
4957 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
< OCFS2_SB(inode
->i_sb
)->s_clustersize
);
4959 ret
= ocfs2_extend_trans(handle
, credits
);
4965 /* Move half of the xattr in start_blk to the next bucket. */
4966 return ocfs2_divide_xattr_bucket(inode
, handle
, prev_blk
,
4967 new_blk
, first_hash
, 1);
4971 * Move some xattrs from the old cluster to the new one since they are not
4972 * contiguous in ocfs2 xattr tree.
4974 * new_blk starts a new separate cluster, and we will move some xattrs from
4975 * prev_blk to it. v_start will be set as the first name hash value in this
4976 * new cluster so that it can be used as e_cpos during tree insertion and
4977 * don't collide with our original b-tree operations. first_bh and header_bh
4978 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4979 * to extend the insert bucket.
4981 * The problem is how much xattr should we move to the new one and when should
4982 * we update first_bh and header_bh?
4983 * 1. If cluster size > bucket size, that means the previous cluster has more
4984 * than 1 bucket, so just move half nums of bucket into the new cluster and
4985 * update the first_bh and header_bh if the insert bucket has been moved
4986 * to the new cluster.
4987 * 2. If cluster_size == bucket_size:
4988 * a) If the previous extent rec has more than one cluster and the insert
4989 * place isn't in the last cluster, copy the entire last cluster to the
4990 * new one. This time, we don't need to upate the first_bh and header_bh
4991 * since they will not be moved into the new cluster.
4992 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4993 * the new one. And we set the extend flag to zero if the insert place is
4994 * moved into the new allocated cluster since no extend is needed.
4996 static int ocfs2_adjust_xattr_cross_cluster(struct inode
*inode
,
4998 struct ocfs2_xattr_bucket
*first
,
4999 struct ocfs2_xattr_bucket
*target
,
5007 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
5008 (unsigned long long)bucket_blkno(first
), prev_clusters
,
5009 (unsigned long long)new_blk
);
5011 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
)) > 1) {
5012 ret
= ocfs2_mv_xattr_bucket_cross_cluster(inode
,
5021 /* The start of the last cluster in the first extent */
5022 u64 last_blk
= bucket_blkno(first
) +
5023 ((prev_clusters
- 1) *
5024 ocfs2_clusters_to_blocks(inode
->i_sb
, 1));
5026 if (prev_clusters
> 1 && bucket_blkno(target
) != last_blk
) {
5027 ret
= ocfs2_mv_xattr_buckets(inode
, handle
,
5028 bucket_blkno(first
),
5029 last_blk
, new_blk
, 0,
5034 ret
= ocfs2_divide_xattr_cluster(inode
, handle
,
5040 if ((bucket_blkno(target
) == last_blk
) && extend
)
5049 * Add a new cluster for xattr storage.
5051 * If the new cluster is contiguous with the previous one, it will be
5052 * appended to the same extent record, and num_clusters will be updated.
5053 * If not, we will insert a new extent for it and move some xattrs in
5054 * the last cluster into the new allocated one.
5055 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5056 * lose the benefits of hashing because we'll have to search large leaves.
5057 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5060 * first_bh is the first block of the previous extent rec and header_bh
5061 * indicates the bucket we will insert the new xattrs. They will be updated
5062 * when the header_bh is moved into the new cluster.
5064 static int ocfs2_add_new_xattr_cluster(struct inode
*inode
,
5065 struct buffer_head
*root_bh
,
5066 struct ocfs2_xattr_bucket
*first
,
5067 struct ocfs2_xattr_bucket
*target
,
5071 struct ocfs2_xattr_set_ctxt
*ctxt
)
5074 u16 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
5075 u32 prev_clusters
= *num_clusters
;
5076 u32 clusters_to_add
= 1, bit_off
, num_bits
, v_start
= 0;
5078 handle_t
*handle
= ctxt
->handle
;
5079 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5080 struct ocfs2_extent_tree et
;
5082 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5083 "previous xattr blkno = %llu\n",
5084 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5085 prev_cpos
, (unsigned long long)bucket_blkno(first
));
5087 ocfs2_init_xattr_tree_extent_tree(&et
, INODE_CACHE(inode
), root_bh
);
5089 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), root_bh
,
5090 OCFS2_JOURNAL_ACCESS_WRITE
);
5096 ret
= __ocfs2_claim_clusters(handle
, ctxt
->data_ac
, 1,
5097 clusters_to_add
, &bit_off
, &num_bits
);
5104 BUG_ON(num_bits
> clusters_to_add
);
5106 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
5107 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5108 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
5110 if (bucket_blkno(first
) + (prev_clusters
* bpc
) == block
&&
5111 (prev_clusters
+ num_bits
) << osb
->s_clustersize_bits
<=
5112 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
) {
5114 * If this cluster is contiguous with the old one and
5115 * adding this new cluster, we don't surpass the limit of
5116 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5117 * initialized and used like other buckets in the previous
5119 * So add it as a contiguous one. The caller will handle
5122 v_start
= prev_cpos
+ prev_clusters
;
5123 *num_clusters
= prev_clusters
+ num_bits
;
5124 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5127 ret
= ocfs2_adjust_xattr_cross_cluster(inode
,
5141 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
5142 num_bits
, (unsigned long long)block
, v_start
);
5143 ret
= ocfs2_insert_extent(handle
, &et
, v_start
, block
,
5144 num_bits
, 0, ctxt
->meta_ac
);
5150 ocfs2_journal_dirty(handle
, root_bh
);
5157 * We are given an extent. 'first' is the bucket at the very front of
5158 * the extent. The extent has space for an additional bucket past
5159 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5160 * of the target bucket. We wish to shift every bucket past the target
5161 * down one, filling in that additional space. When we get back to the
5162 * target, we split the target between itself and the now-empty bucket
5163 * at target+1 (aka, target_blkno + blks_per_bucket).
5165 static int ocfs2_extend_xattr_bucket(struct inode
*inode
,
5167 struct ocfs2_xattr_bucket
*first
,
5172 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5173 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
5175 u16 new_bucket
= le16_to_cpu(bucket_xh(first
)->xh_num_buckets
);
5177 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
5178 "from %llu, len = %u\n", (unsigned long long)target_blk
,
5179 (unsigned long long)bucket_blkno(first
), num_clusters
);
5181 /* The extent must have room for an additional bucket */
5182 BUG_ON(new_bucket
>=
5183 (num_clusters
* ocfs2_xattr_buckets_per_cluster(osb
)));
5185 /* end_blk points to the last existing bucket */
5186 end_blk
= bucket_blkno(first
) + ((new_bucket
- 1) * blk_per_bucket
);
5189 * end_blk is the start of the last existing bucket.
5190 * Thus, (end_blk - target_blk) covers the target bucket and
5191 * every bucket after it up to, but not including, the last
5192 * existing bucket. Then we add the last existing bucket, the
5193 * new bucket, and the first bucket (3 * blk_per_bucket).
5195 credits
= (end_blk
- target_blk
) + (3 * blk_per_bucket
);
5196 ret
= ocfs2_extend_trans(handle
, credits
);
5202 ret
= ocfs2_xattr_bucket_journal_access(handle
, first
,
5203 OCFS2_JOURNAL_ACCESS_WRITE
);
5209 while (end_blk
!= target_blk
) {
5210 ret
= ocfs2_cp_xattr_bucket(inode
, handle
, end_blk
,
5211 end_blk
+ blk_per_bucket
, 0);
5214 end_blk
-= blk_per_bucket
;
5217 /* Move half of the xattr in target_blkno to the next bucket. */
5218 ret
= ocfs2_divide_xattr_bucket(inode
, handle
, target_blk
,
5219 target_blk
+ blk_per_bucket
, NULL
, 0);
5221 le16_add_cpu(&bucket_xh(first
)->xh_num_buckets
, 1);
5222 ocfs2_xattr_bucket_journal_dirty(handle
, first
);
5229 * Add new xattr bucket in an extent record and adjust the buckets
5230 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5231 * bucket we want to insert into.
5233 * In the easy case, we will move all the buckets after target down by
5234 * one. Half of target's xattrs will be moved to the next bucket.
5236 * If current cluster is full, we'll allocate a new one. This may not
5237 * be contiguous. The underlying calls will make sure that there is
5238 * space for the insert, shifting buckets around if necessary.
5239 * 'target' may be moved by those calls.
5241 static int ocfs2_add_new_xattr_bucket(struct inode
*inode
,
5242 struct buffer_head
*xb_bh
,
5243 struct ocfs2_xattr_bucket
*target
,
5244 struct ocfs2_xattr_set_ctxt
*ctxt
)
5246 struct ocfs2_xattr_block
*xb
=
5247 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
5248 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
5249 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
5251 le32_to_cpu(bucket_xh(target
)->xh_entries
[0].xe_name_hash
);
5252 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5253 int ret
, num_buckets
, extend
= 1;
5255 u32 e_cpos
, num_clusters
;
5256 /* The bucket at the front of the extent */
5257 struct ocfs2_xattr_bucket
*first
;
5259 mlog(0, "Add new xattr bucket starting from %llu\n",
5260 (unsigned long long)bucket_blkno(target
));
5262 /* The first bucket of the original extent */
5263 first
= ocfs2_xattr_bucket_new(inode
);
5270 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &e_cpos
,
5277 ret
= ocfs2_read_xattr_bucket(first
, p_blkno
);
5283 num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
) * num_clusters
;
5284 if (num_buckets
== le16_to_cpu(bucket_xh(first
)->xh_num_buckets
)) {
5286 * This can move first+target if the target bucket moves
5287 * to the new extent.
5289 ret
= ocfs2_add_new_xattr_cluster(inode
,
5304 ret
= ocfs2_extend_xattr_bucket(inode
,
5307 bucket_blkno(target
),
5314 ocfs2_xattr_bucket_free(first
);
5319 static inline char *ocfs2_xattr_bucket_get_val(struct inode
*inode
,
5320 struct ocfs2_xattr_bucket
*bucket
,
5323 int block_off
= offs
>> inode
->i_sb
->s_blocksize_bits
;
5325 offs
= offs
% inode
->i_sb
->s_blocksize
;
5326 return bucket_block(bucket
, block_off
) + offs
;
5330 * Truncate the specified xe_off entry in xattr bucket.
5331 * bucket is indicated by header_bh and len is the new length.
5332 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5334 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5336 static int ocfs2_xattr_bucket_value_truncate(struct inode
*inode
,
5337 struct ocfs2_xattr_bucket
*bucket
,
5340 struct ocfs2_xattr_set_ctxt
*ctxt
)
5344 struct ocfs2_xattr_entry
*xe
;
5345 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5346 size_t blocksize
= inode
->i_sb
->s_blocksize
;
5347 struct ocfs2_xattr_value_buf vb
= {
5348 .vb_access
= ocfs2_journal_access
,
5351 xe
= &xh
->xh_entries
[xe_off
];
5353 BUG_ON(!xe
|| ocfs2_xattr_is_local(xe
));
5355 offset
= le16_to_cpu(xe
->xe_name_offset
) +
5356 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5358 value_blk
= offset
/ blocksize
;
5360 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5361 BUG_ON(value_blk
!= (offset
+ OCFS2_XATTR_ROOT_SIZE
- 1) / blocksize
);
5363 vb
.vb_bh
= bucket
->bu_bhs
[value_blk
];
5366 vb
.vb_xv
= (struct ocfs2_xattr_value_root
*)
5367 (vb
.vb_bh
->b_data
+ offset
% blocksize
);
5370 * From here on out we have to dirty the bucket. The generic
5371 * value calls only modify one of the bucket's bhs, but we need
5372 * to send the bucket at once. So if they error, they *could* have
5373 * modified something. We have to assume they did, and dirty
5374 * the whole bucket. This leaves us in a consistent state.
5376 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5377 xe_off
, (unsigned long long)bucket_blkno(bucket
), len
);
5378 ret
= ocfs2_xattr_value_truncate(inode
, &vb
, len
, ctxt
);
5384 ret
= ocfs2_xattr_bucket_journal_access(ctxt
->handle
, bucket
,
5385 OCFS2_JOURNAL_ACCESS_WRITE
);
5391 xe
->xe_value_size
= cpu_to_le64(len
);
5393 ocfs2_xattr_bucket_journal_dirty(ctxt
->handle
, bucket
);
5399 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
5400 struct buffer_head
*root_bh
,
5407 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5408 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5410 struct ocfs2_xattr_block
*xb
=
5411 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
5412 struct ocfs2_alloc_context
*meta_ac
= NULL
;
5413 struct ocfs2_cached_dealloc_ctxt dealloc
;
5414 struct ocfs2_extent_tree et
;
5416 ret
= ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
5417 ocfs2_delete_xattr_in_bucket
, para
);
5423 ocfs2_init_xattr_tree_extent_tree(&et
, INODE_CACHE(inode
), root_bh
);
5425 ocfs2_init_dealloc_ctxt(&dealloc
);
5427 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5428 cpos
, len
, (unsigned long long)blkno
);
5430 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode
), blkno
,
5433 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
5439 mutex_lock(&tl_inode
->i_mutex
);
5441 if (ocfs2_truncate_log_needs_flush(osb
)) {
5442 ret
= __ocfs2_flush_truncate_log(osb
);
5449 handle
= ocfs2_start_trans(osb
, ocfs2_remove_extent_credits(osb
->sb
));
5450 if (IS_ERR(handle
)) {
5456 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), root_bh
,
5457 OCFS2_JOURNAL_ACCESS_WRITE
);
5463 ret
= ocfs2_remove_extent(handle
, &et
, cpos
, len
, meta_ac
,
5470 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, -len
);
5471 ocfs2_journal_dirty(handle
, root_bh
);
5473 ret
= ocfs2_truncate_log_append(osb
, handle
, blkno
, len
);
5478 ocfs2_commit_trans(osb
, handle
);
5480 ocfs2_schedule_truncate_log_flush(osb
, 1);
5482 mutex_unlock(&tl_inode
->i_mutex
);
5485 ocfs2_free_alloc_context(meta_ac
);
5487 ocfs2_run_deallocs(osb
, &dealloc
);
5493 * check whether the xattr bucket is filled up with the same hash value.
5494 * If we want to insert the xattr with the same hash, return -ENOSPC.
5495 * If we want to insert a xattr with different hash value, go ahead
5496 * and ocfs2_divide_xattr_bucket will handle this.
5498 static int ocfs2_check_xattr_bucket_collision(struct inode
*inode
,
5499 struct ocfs2_xattr_bucket
*bucket
,
5502 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5503 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
5505 if (name_hash
!= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
))
5508 if (xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1].xe_name_hash
==
5509 xh
->xh_entries
[0].xe_name_hash
) {
5510 mlog(ML_ERROR
, "Too much hash collision in xattr bucket %llu, "
5512 (unsigned long long)bucket_blkno(bucket
),
5513 le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
));
5521 * Try to set the entry in the current bucket. If we fail, the caller
5522 * will handle getting us another bucket.
5524 static int ocfs2_xattr_set_entry_bucket(struct inode
*inode
,
5525 struct ocfs2_xattr_info
*xi
,
5526 struct ocfs2_xattr_search
*xs
,
5527 struct ocfs2_xattr_set_ctxt
*ctxt
)
5530 struct ocfs2_xa_loc loc
;
5532 mlog_entry("Set xattr %s in xattr bucket\n", xi
->xi_name
);
5534 ocfs2_init_xattr_bucket_xa_loc(&loc
, xs
->bucket
,
5535 xs
->not_found
? NULL
: xs
->here
);
5536 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
5538 xs
->here
= loc
.xl_entry
;
5541 if (ret
!= -ENOSPC
) {
5546 /* Ok, we need space. Let's try defragmenting the bucket. */
5547 ret
= ocfs2_defrag_xattr_bucket(inode
, ctxt
->handle
,
5554 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
5556 xs
->here
= loc
.xl_entry
;
5568 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
5569 struct ocfs2_xattr_info
*xi
,
5570 struct ocfs2_xattr_search
*xs
,
5571 struct ocfs2_xattr_set_ctxt
*ctxt
)
5575 mlog_entry("Set xattr %s in xattr index block\n", xi
->xi_name
);
5577 ret
= ocfs2_xattr_set_entry_bucket(inode
, xi
, xs
, ctxt
);
5580 if (ret
!= -ENOSPC
) {
5585 /* Ack, need more space. Let's try to get another bucket! */
5588 * We do not allow for overlapping ranges between buckets. And
5589 * the maximum number of collisions we will allow for then is
5590 * one bucket's worth, so check it here whether we need to
5591 * add a new bucket for the insert.
5593 ret
= ocfs2_check_xattr_bucket_collision(inode
,
5601 ret
= ocfs2_add_new_xattr_bucket(inode
,
5611 * ocfs2_add_new_xattr_bucket() will have updated
5612 * xs->bucket if it moved, but it will not have updated
5613 * any of the other search fields. Thus, we drop it and
5614 * re-search. Everything should be cached, so it'll be
5617 ocfs2_xattr_bucket_relse(xs
->bucket
);
5618 ret
= ocfs2_xattr_index_block_find(inode
, xs
->xattr_bh
,
5621 if (ret
&& ret
!= -ENODATA
)
5623 xs
->not_found
= ret
;
5625 /* Ok, we have a new bucket, let's try again */
5626 ret
= ocfs2_xattr_set_entry_bucket(inode
, xi
, xs
, ctxt
);
5627 if (ret
&& (ret
!= -ENOSPC
))
5635 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
5636 struct ocfs2_xattr_bucket
*bucket
,
5639 int ret
= 0, ref_credits
;
5640 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5642 struct ocfs2_xattr_entry
*xe
;
5643 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5644 struct ocfs2_xattr_set_ctxt ctxt
= {NULL
, NULL
,};
5645 int credits
= ocfs2_remove_extent_credits(osb
->sb
) +
5646 ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
5647 struct ocfs2_xattr_value_root
*xv
;
5648 struct ocfs2_rm_xattr_bucket_para
*args
=
5649 (struct ocfs2_rm_xattr_bucket_para
*)para
;
5651 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
5653 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
5654 xe
= &xh
->xh_entries
[i
];
5655 if (ocfs2_xattr_is_local(xe
))
5658 ret
= ocfs2_get_xattr_tree_value_root(inode
->i_sb
, bucket
,
5661 ret
= ocfs2_lock_xattr_remove_allocators(inode
, xv
,
5667 ctxt
.handle
= ocfs2_start_trans(osb
, credits
+ ref_credits
);
5668 if (IS_ERR(ctxt
.handle
)) {
5669 ret
= PTR_ERR(ctxt
.handle
);
5674 ret
= ocfs2_xattr_bucket_value_truncate(inode
, bucket
,
5677 ocfs2_commit_trans(osb
, ctxt
.handle
);
5679 ocfs2_free_alloc_context(ctxt
.meta_ac
);
5680 ctxt
.meta_ac
= NULL
;
5689 ocfs2_free_alloc_context(ctxt
.meta_ac
);
5690 ocfs2_schedule_truncate_log_flush(osb
, 1);
5691 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
5696 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5697 * or change the extent record flag), we need to recalculate
5698 * the metaecc for the whole bucket. So it is done here.
5701 * We have to give the extra credits for the caller.
5703 static int ocfs2_xattr_bucket_post_refcount(struct inode
*inode
,
5708 struct ocfs2_xattr_bucket
*bucket
=
5709 (struct ocfs2_xattr_bucket
*)para
;
5711 ret
= ocfs2_xattr_bucket_journal_access(handle
, bucket
,
5712 OCFS2_JOURNAL_ACCESS_WRITE
);
5718 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
5724 * Special action we need if the xattr value is refcounted.
5726 * 1. If the xattr is refcounted, lock the tree.
5727 * 2. CoW the xattr if we are setting the new value and the value
5728 * will be stored outside.
5729 * 3. In other case, decrease_refcount will work for us, so just
5730 * lock the refcount tree, calculate the meta and credits is OK.
5732 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5733 * currently CoW is a completed transaction, while this function
5734 * will also lock the allocators and let us deadlock. So we will
5735 * CoW the whole xattr value.
5737 static int ocfs2_prepare_refcount_xattr(struct inode
*inode
,
5738 struct ocfs2_dinode
*di
,
5739 struct ocfs2_xattr_info
*xi
,
5740 struct ocfs2_xattr_search
*xis
,
5741 struct ocfs2_xattr_search
*xbs
,
5742 struct ocfs2_refcount_tree
**ref_tree
,
5747 struct ocfs2_xattr_block
*xb
;
5748 struct ocfs2_xattr_entry
*xe
;
5750 u32 p_cluster
, num_clusters
;
5751 unsigned int ext_flags
;
5752 int name_offset
, name_len
;
5753 struct ocfs2_xattr_value_buf vb
;
5754 struct ocfs2_xattr_bucket
*bucket
= NULL
;
5755 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5756 struct ocfs2_post_refcount refcount
;
5757 struct ocfs2_post_refcount
*p
= NULL
;
5758 struct buffer_head
*ref_root_bh
= NULL
;
5760 if (!xis
->not_found
) {
5762 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
5763 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5765 vb
.vb_bh
= xis
->inode_bh
;
5766 vb
.vb_access
= ocfs2_journal_access_di
;
5768 int i
, block_off
= 0;
5769 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
5771 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
5772 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5773 i
= xbs
->here
- xbs
->header
->xh_entries
;
5775 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
5776 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
5777 bucket_xh(xbs
->bucket
),
5784 base
= bucket_block(xbs
->bucket
, block_off
);
5785 vb
.vb_bh
= xbs
->bucket
->bu_bhs
[block_off
];
5786 vb
.vb_access
= ocfs2_journal_access
;
5788 if (ocfs2_meta_ecc(osb
)) {
5789 /*create parameters for ocfs2_post_refcount. */
5790 bucket
= xbs
->bucket
;
5791 refcount
.credits
= bucket
->bu_blocks
;
5792 refcount
.para
= bucket
;
5794 ocfs2_xattr_bucket_post_refcount
;
5799 vb
.vb_bh
= xbs
->xattr_bh
;
5800 vb
.vb_access
= ocfs2_journal_access_xb
;
5804 if (ocfs2_xattr_is_local(xe
))
5807 vb
.vb_xv
= (struct ocfs2_xattr_value_root
*)
5808 (base
+ name_offset
+ name_len
);
5810 ret
= ocfs2_xattr_get_clusters(inode
, 0, &p_cluster
,
5811 &num_clusters
, &vb
.vb_xv
->xr_list
,
5819 * We just need to check the 1st extent record, since we always
5820 * CoW the whole xattr. So there shouldn't be a xattr with
5821 * some REFCOUNT extent recs after the 1st one.
5823 if (!(ext_flags
& OCFS2_EXT_REFCOUNTED
))
5826 ret
= ocfs2_lock_refcount_tree(osb
, le64_to_cpu(di
->i_refcount_loc
),
5827 1, ref_tree
, &ref_root_bh
);
5834 * If we are deleting the xattr or the new size will be stored inside,
5835 * cool, leave it there, the xattr truncate process will remove them
5836 * for us(it still needs the refcount tree lock and the meta, credits).
5837 * And the worse case is that every cluster truncate will split the
5838 * refcount tree, and make the original extent become 3. So we will need
5839 * 2 * cluster more extent recs at most.
5841 if (!xi
->xi_value
|| xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
) {
5843 ret
= ocfs2_refcounted_xattr_delete_need(inode
,
5844 &(*ref_tree
)->rf_ci
,
5845 ref_root_bh
, vb
.vb_xv
,
5852 ret
= ocfs2_refcount_cow_xattr(inode
, di
, &vb
,
5853 *ref_tree
, ref_root_bh
, 0,
5854 le32_to_cpu(vb
.vb_xv
->xr_clusters
), p
);
5859 brelse(ref_root_bh
);
5864 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5865 * The physical clusters will be added to refcount tree.
5867 static int ocfs2_xattr_value_attach_refcount(struct inode
*inode
,
5868 struct ocfs2_xattr_value_root
*xv
,
5869 struct ocfs2_extent_tree
*value_et
,
5870 struct ocfs2_caching_info
*ref_ci
,
5871 struct buffer_head
*ref_root_bh
,
5872 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5873 struct ocfs2_post_refcount
*refcount
)
5876 u32 clusters
= le32_to_cpu(xv
->xr_clusters
);
5877 u32 cpos
, p_cluster
, num_clusters
;
5878 struct ocfs2_extent_list
*el
= &xv
->xr_list
;
5879 unsigned int ext_flags
;
5882 while (cpos
< clusters
) {
5883 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
5884 &num_clusters
, el
, &ext_flags
);
5886 cpos
+= num_clusters
;
5887 if ((ext_flags
& OCFS2_EXT_REFCOUNTED
))
5892 ret
= ocfs2_add_refcount_flag(inode
, value_et
,
5893 ref_ci
, ref_root_bh
,
5894 cpos
- num_clusters
,
5895 p_cluster
, num_clusters
,
5907 * Given a normal ocfs2_xattr_header, refcount all the entries which
5908 * have value stored outside.
5909 * Used for xattrs stored in inode and ocfs2_xattr_block.
5911 static int ocfs2_xattr_attach_refcount_normal(struct inode
*inode
,
5912 struct ocfs2_xattr_value_buf
*vb
,
5913 struct ocfs2_xattr_header
*header
,
5914 struct ocfs2_caching_info
*ref_ci
,
5915 struct buffer_head
*ref_root_bh
,
5916 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5919 struct ocfs2_xattr_entry
*xe
;
5920 struct ocfs2_xattr_value_root
*xv
;
5921 struct ocfs2_extent_tree et
;
5924 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
5925 xe
= &header
->xh_entries
[i
];
5927 if (ocfs2_xattr_is_local(xe
))
5930 xv
= (struct ocfs2_xattr_value_root
*)((void *)header
+
5931 le16_to_cpu(xe
->xe_name_offset
) +
5932 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
5935 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
5937 ret
= ocfs2_xattr_value_attach_refcount(inode
, xv
, &et
,
5938 ref_ci
, ref_root_bh
,
5949 static int ocfs2_xattr_inline_attach_refcount(struct inode
*inode
,
5950 struct buffer_head
*fe_bh
,
5951 struct ocfs2_caching_info
*ref_ci
,
5952 struct buffer_head
*ref_root_bh
,
5953 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5955 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
5956 struct ocfs2_xattr_header
*header
= (struct ocfs2_xattr_header
*)
5957 (fe_bh
->b_data
+ inode
->i_sb
->s_blocksize
-
5958 le16_to_cpu(di
->i_xattr_inline_size
));
5959 struct ocfs2_xattr_value_buf vb
= {
5961 .vb_access
= ocfs2_journal_access_di
,
5964 return ocfs2_xattr_attach_refcount_normal(inode
, &vb
, header
,
5965 ref_ci
, ref_root_bh
, dealloc
);
5968 struct ocfs2_xattr_tree_value_refcount_para
{
5969 struct ocfs2_caching_info
*ref_ci
;
5970 struct buffer_head
*ref_root_bh
;
5971 struct ocfs2_cached_dealloc_ctxt
*dealloc
;
5974 static int ocfs2_get_xattr_tree_value_root(struct super_block
*sb
,
5975 struct ocfs2_xattr_bucket
*bucket
,
5977 struct ocfs2_xattr_value_root
**xv
,
5978 struct buffer_head
**bh
)
5980 int ret
, block_off
, name_offset
;
5981 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5982 struct ocfs2_xattr_entry
*xe
= &xh
->xh_entries
[offset
];
5985 ret
= ocfs2_xattr_bucket_get_name_value(sb
,
5995 base
= bucket_block(bucket
, block_off
);
5997 *xv
= (struct ocfs2_xattr_value_root
*)(base
+ name_offset
+
5998 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
6001 *bh
= bucket
->bu_bhs
[block_off
];
6007 * For a given xattr bucket, refcount all the entries which
6008 * have value stored outside.
6010 static int ocfs2_xattr_bucket_value_refcount(struct inode
*inode
,
6011 struct ocfs2_xattr_bucket
*bucket
,
6015 struct ocfs2_extent_tree et
;
6016 struct ocfs2_xattr_tree_value_refcount_para
*ref
=
6017 (struct ocfs2_xattr_tree_value_refcount_para
*)para
;
6018 struct ocfs2_xattr_header
*xh
=
6019 (struct ocfs2_xattr_header
*)bucket
->bu_bhs
[0]->b_data
;
6020 struct ocfs2_xattr_entry
*xe
;
6021 struct ocfs2_xattr_value_buf vb
= {
6022 .vb_access
= ocfs2_journal_access
,
6024 struct ocfs2_post_refcount refcount
= {
6025 .credits
= bucket
->bu_blocks
,
6027 .func
= ocfs2_xattr_bucket_post_refcount
,
6029 struct ocfs2_post_refcount
*p
= NULL
;
6031 /* We only need post_refcount if we support metaecc. */
6032 if (ocfs2_meta_ecc(OCFS2_SB(inode
->i_sb
)))
6035 mlog(0, "refcount bucket %llu, count = %u\n",
6036 (unsigned long long)bucket_blkno(bucket
),
6037 le16_to_cpu(xh
->xh_count
));
6038 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
6039 xe
= &xh
->xh_entries
[i
];
6041 if (ocfs2_xattr_is_local(xe
))
6044 ret
= ocfs2_get_xattr_tree_value_root(inode
->i_sb
, bucket
, i
,
6045 &vb
.vb_xv
, &vb
.vb_bh
);
6051 ocfs2_init_xattr_value_extent_tree(&et
,
6052 INODE_CACHE(inode
), &vb
);
6054 ret
= ocfs2_xattr_value_attach_refcount(inode
, vb
.vb_xv
,
6068 static int ocfs2_refcount_xattr_tree_rec(struct inode
*inode
,
6069 struct buffer_head
*root_bh
,
6070 u64 blkno
, u32 cpos
, u32 len
, void *para
)
6072 return ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
6073 ocfs2_xattr_bucket_value_refcount
,
6077 static int ocfs2_xattr_block_attach_refcount(struct inode
*inode
,
6078 struct buffer_head
*blk_bh
,
6079 struct ocfs2_caching_info
*ref_ci
,
6080 struct buffer_head
*ref_root_bh
,
6081 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
6084 struct ocfs2_xattr_block
*xb
=
6085 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
6087 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
6088 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
6089 struct ocfs2_xattr_value_buf vb
= {
6091 .vb_access
= ocfs2_journal_access_xb
,
6094 ret
= ocfs2_xattr_attach_refcount_normal(inode
, &vb
, header
,
6095 ref_ci
, ref_root_bh
,
6098 struct ocfs2_xattr_tree_value_refcount_para para
= {
6100 .ref_root_bh
= ref_root_bh
,
6104 ret
= ocfs2_iterate_xattr_index_block(inode
, blk_bh
,
6105 ocfs2_refcount_xattr_tree_rec
,
6112 int ocfs2_xattr_attach_refcount_tree(struct inode
*inode
,
6113 struct buffer_head
*fe_bh
,
6114 struct ocfs2_caching_info
*ref_ci
,
6115 struct buffer_head
*ref_root_bh
,
6116 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
6119 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6120 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
6121 struct buffer_head
*blk_bh
= NULL
;
6123 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
6124 ret
= ocfs2_xattr_inline_attach_refcount(inode
, fe_bh
,
6125 ref_ci
, ref_root_bh
,
6133 if (!di
->i_xattr_loc
)
6136 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
6143 ret
= ocfs2_xattr_block_attach_refcount(inode
, blk_bh
, ref_ci
,
6144 ref_root_bh
, dealloc
);
6154 typedef int (should_xattr_reflinked
)(struct ocfs2_xattr_entry
*xe
);
6156 * Store the information we need in xattr reflink.
6157 * old_bh and new_bh are inode bh for the old and new inode.
6159 struct ocfs2_xattr_reflink
{
6160 struct inode
*old_inode
;
6161 struct inode
*new_inode
;
6162 struct buffer_head
*old_bh
;
6163 struct buffer_head
*new_bh
;
6164 struct ocfs2_caching_info
*ref_ci
;
6165 struct buffer_head
*ref_root_bh
;
6166 struct ocfs2_cached_dealloc_ctxt
*dealloc
;
6167 should_xattr_reflinked
*xattr_reflinked
;
6171 * Given a xattr header and xe offset,
6172 * return the proper xv and the corresponding bh.
6173 * xattr in inode, block and xattr tree have different implementaions.
6175 typedef int (get_xattr_value_root
)(struct super_block
*sb
,
6176 struct buffer_head
*bh
,
6177 struct ocfs2_xattr_header
*xh
,
6179 struct ocfs2_xattr_value_root
**xv
,
6180 struct buffer_head
**ret_bh
,
6184 * Calculate all the xattr value root metadata stored in this xattr header and
6185 * credits we need if we create them from the scratch.
6186 * We use get_xattr_value_root so that all types of xattr container can use it.
6188 static int ocfs2_value_metas_in_xattr_header(struct super_block
*sb
,
6189 struct buffer_head
*bh
,
6190 struct ocfs2_xattr_header
*xh
,
6191 int *metas
, int *credits
,
6193 get_xattr_value_root
*func
,
6197 struct ocfs2_xattr_value_root
*xv
;
6198 struct ocfs2_xattr_entry
*xe
;
6200 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
6201 xe
= &xh
->xh_entries
[i
];
6202 if (ocfs2_xattr_is_local(xe
))
6205 ret
= func(sb
, bh
, xh
, i
, &xv
, NULL
, para
);
6211 *metas
+= le16_to_cpu(xv
->xr_list
.l_tree_depth
) *
6212 le16_to_cpu(xv
->xr_list
.l_next_free_rec
);
6214 *credits
+= ocfs2_calc_extend_credits(sb
,
6216 le32_to_cpu(xv
->xr_clusters
));
6219 * If the value is a tree with depth > 1, We don't go deep
6220 * to the extent block, so just calculate a maximum record num.
6222 if (!xv
->xr_list
.l_tree_depth
)
6223 *num_recs
+= le16_to_cpu(xv
->xr_list
.l_next_free_rec
);
6225 *num_recs
+= ocfs2_clusters_for_bytes(sb
,
6232 /* Used by xattr inode and block to return the right xv and buffer_head. */
6233 static int ocfs2_get_xattr_value_root(struct super_block
*sb
,
6234 struct buffer_head
*bh
,
6235 struct ocfs2_xattr_header
*xh
,
6237 struct ocfs2_xattr_value_root
**xv
,
6238 struct buffer_head
**ret_bh
,
6241 struct ocfs2_xattr_entry
*xe
= &xh
->xh_entries
[offset
];
6243 *xv
= (struct ocfs2_xattr_value_root
*)((void *)xh
+
6244 le16_to_cpu(xe
->xe_name_offset
) +
6245 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
6254 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6255 * It is only used for inline xattr and xattr block.
6257 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super
*osb
,
6258 struct ocfs2_xattr_header
*xh
,
6259 struct buffer_head
*ref_root_bh
,
6261 struct ocfs2_alloc_context
**meta_ac
)
6263 int ret
, meta_add
= 0, num_recs
= 0;
6264 struct ocfs2_refcount_block
*rb
=
6265 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
6269 ret
= ocfs2_value_metas_in_xattr_header(osb
->sb
, NULL
, xh
,
6270 &meta_add
, credits
, &num_recs
,
6271 ocfs2_get_xattr_value_root
,
6279 * We need to add/modify num_recs in refcount tree, so just calculate
6280 * an approximate number we need for refcount tree change.
6281 * Sometimes we need to split the tree, and after split, half recs
6282 * will be moved to the new block, and a new block can only provide
6283 * half number of recs. So we multiple new blocks by 2.
6285 num_recs
= num_recs
/ ocfs2_refcount_recs_per_rb(osb
->sb
) * 2;
6286 meta_add
+= num_recs
;
6287 *credits
+= num_recs
+ num_recs
* OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
6288 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)
6289 *credits
+= le16_to_cpu(rb
->rf_list
.l_tree_depth
) *
6290 le16_to_cpu(rb
->rf_list
.l_next_free_rec
) + 1;
6294 ret
= ocfs2_reserve_new_metadata_blocks(osb
, meta_add
, meta_ac
);
6303 * Given a xattr header, reflink all the xattrs in this container.
6304 * It can be used for inode, block and bucket.
6307 * Before we call this function, the caller has memcpy the xattr in
6308 * old_xh to the new_xh.
6310 * If args.xattr_reflinked is set, call it to decide whether the xe should
6311 * be reflinked or not. If not, remove it from the new xattr header.
6313 static int ocfs2_reflink_xattr_header(handle_t
*handle
,
6314 struct ocfs2_xattr_reflink
*args
,
6315 struct buffer_head
*old_bh
,
6316 struct ocfs2_xattr_header
*xh
,
6317 struct buffer_head
*new_bh
,
6318 struct ocfs2_xattr_header
*new_xh
,
6319 struct ocfs2_xattr_value_buf
*vb
,
6320 struct ocfs2_alloc_context
*meta_ac
,
6321 get_xattr_value_root
*func
,
6325 struct super_block
*sb
= args
->old_inode
->i_sb
;
6326 struct buffer_head
*value_bh
;
6327 struct ocfs2_xattr_entry
*xe
, *last
;
6328 struct ocfs2_xattr_value_root
*xv
, *new_xv
;
6329 struct ocfs2_extent_tree data_et
;
6330 u32 clusters
, cpos
, p_cluster
, num_clusters
;
6331 unsigned int ext_flags
= 0;
6333 mlog(0, "reflink xattr in container %llu, count = %u\n",
6334 (unsigned long long)old_bh
->b_blocknr
, le16_to_cpu(xh
->xh_count
));
6336 last
= &new_xh
->xh_entries
[le16_to_cpu(new_xh
->xh_count
)];
6337 for (i
= 0, j
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, j
++) {
6338 xe
= &xh
->xh_entries
[i
];
6340 if (args
->xattr_reflinked
&& !args
->xattr_reflinked(xe
)) {
6341 xe
= &new_xh
->xh_entries
[j
];
6343 le16_add_cpu(&new_xh
->xh_count
, -1);
6344 if (new_xh
->xh_count
) {
6346 (void *)last
- (void *)xe
);
6348 sizeof(struct ocfs2_xattr_entry
));
6352 * We don't want j to increase in the next round since
6353 * it is already moved ahead.
6359 if (ocfs2_xattr_is_local(xe
))
6362 ret
= func(sb
, old_bh
, xh
, i
, &xv
, NULL
, para
);
6368 ret
= func(sb
, new_bh
, new_xh
, j
, &new_xv
, &value_bh
, para
);
6375 * For the xattr which has l_tree_depth = 0, all the extent
6376 * recs have already be copied to the new xh with the
6377 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6378 * increase the refount count int the refcount tree.
6380 * For the xattr which has l_tree_depth > 0, we need
6381 * to initialize it to the empty default value root,
6382 * and then insert the extents one by one.
6384 if (xv
->xr_list
.l_tree_depth
) {
6385 memcpy(new_xv
, &def_xv
, sizeof(def_xv
));
6387 vb
->vb_bh
= value_bh
;
6388 ocfs2_init_xattr_value_extent_tree(&data_et
,
6389 INODE_CACHE(args
->new_inode
), vb
);
6392 clusters
= le32_to_cpu(xv
->xr_clusters
);
6394 while (cpos
< clusters
) {
6395 ret
= ocfs2_xattr_get_clusters(args
->old_inode
,
6408 if (xv
->xr_list
.l_tree_depth
) {
6409 ret
= ocfs2_insert_extent(handle
,
6411 ocfs2_clusters_to_blocks(
6412 args
->old_inode
->i_sb
,
6414 num_clusters
, ext_flags
,
6422 ret
= ocfs2_increase_refcount(handle
, args
->ref_ci
,
6424 p_cluster
, num_clusters
,
6425 meta_ac
, args
->dealloc
);
6431 cpos
+= num_clusters
;
6439 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink
*args
)
6441 int ret
= 0, credits
= 0;
6443 struct ocfs2_super
*osb
= OCFS2_SB(args
->old_inode
->i_sb
);
6444 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)args
->old_bh
->b_data
;
6445 int inline_size
= le16_to_cpu(di
->i_xattr_inline_size
);
6446 int header_off
= osb
->sb
->s_blocksize
- inline_size
;
6447 struct ocfs2_xattr_header
*xh
= (struct ocfs2_xattr_header
*)
6448 (args
->old_bh
->b_data
+ header_off
);
6449 struct ocfs2_xattr_header
*new_xh
= (struct ocfs2_xattr_header
*)
6450 (args
->new_bh
->b_data
+ header_off
);
6451 struct ocfs2_alloc_context
*meta_ac
= NULL
;
6452 struct ocfs2_inode_info
*new_oi
;
6453 struct ocfs2_dinode
*new_di
;
6454 struct ocfs2_xattr_value_buf vb
= {
6455 .vb_bh
= args
->new_bh
,
6456 .vb_access
= ocfs2_journal_access_di
,
6459 ret
= ocfs2_reflink_lock_xattr_allocators(osb
, xh
, args
->ref_root_bh
,
6460 &credits
, &meta_ac
);
6466 handle
= ocfs2_start_trans(osb
, credits
);
6467 if (IS_ERR(handle
)) {
6468 ret
= PTR_ERR(handle
);
6473 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(args
->new_inode
),
6474 args
->new_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
6480 memcpy(args
->new_bh
->b_data
+ header_off
,
6481 args
->old_bh
->b_data
+ header_off
, inline_size
);
6483 new_di
= (struct ocfs2_dinode
*)args
->new_bh
->b_data
;
6484 new_di
->i_xattr_inline_size
= cpu_to_le16(inline_size
);
6486 ret
= ocfs2_reflink_xattr_header(handle
, args
, args
->old_bh
, xh
,
6487 args
->new_bh
, new_xh
, &vb
, meta_ac
,
6488 ocfs2_get_xattr_value_root
, NULL
);
6494 new_oi
= OCFS2_I(args
->new_inode
);
6495 spin_lock(&new_oi
->ip_lock
);
6496 new_oi
->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
| OCFS2_INLINE_XATTR_FL
;
6497 new_di
->i_dyn_features
= cpu_to_le16(new_oi
->ip_dyn_features
);
6498 spin_unlock(&new_oi
->ip_lock
);
6500 ocfs2_journal_dirty(handle
, args
->new_bh
);
6503 ocfs2_commit_trans(osb
, handle
);
6507 ocfs2_free_alloc_context(meta_ac
);
6511 static int ocfs2_create_empty_xattr_block(struct inode
*inode
,
6512 struct buffer_head
*fe_bh
,
6513 struct buffer_head
**ret_bh
,
6517 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6518 struct ocfs2_xattr_set_ctxt ctxt
;
6520 memset(&ctxt
, 0, sizeof(ctxt
));
6521 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, &ctxt
.meta_ac
);
6527 ctxt
.handle
= ocfs2_start_trans(osb
, OCFS2_XATTR_BLOCK_CREATE_CREDITS
);
6528 if (IS_ERR(ctxt
.handle
)) {
6529 ret
= PTR_ERR(ctxt
.handle
);
6534 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6535 (unsigned long long)fe_bh
->b_blocknr
, indexed
);
6536 ret
= ocfs2_create_xattr_block(inode
, fe_bh
, &ctxt
, indexed
,
6541 ocfs2_commit_trans(osb
, ctxt
.handle
);
6543 ocfs2_free_alloc_context(ctxt
.meta_ac
);
6547 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink
*args
,
6548 struct buffer_head
*blk_bh
,
6549 struct buffer_head
*new_blk_bh
)
6551 int ret
= 0, credits
= 0;
6553 struct ocfs2_inode_info
*new_oi
= OCFS2_I(args
->new_inode
);
6554 struct ocfs2_dinode
*new_di
;
6555 struct ocfs2_super
*osb
= OCFS2_SB(args
->new_inode
->i_sb
);
6556 int header_off
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
6557 struct ocfs2_xattr_block
*xb
=
6558 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
6559 struct ocfs2_xattr_header
*xh
= &xb
->xb_attrs
.xb_header
;
6560 struct ocfs2_xattr_block
*new_xb
=
6561 (struct ocfs2_xattr_block
*)new_blk_bh
->b_data
;
6562 struct ocfs2_xattr_header
*new_xh
= &new_xb
->xb_attrs
.xb_header
;
6563 struct ocfs2_alloc_context
*meta_ac
;
6564 struct ocfs2_xattr_value_buf vb
= {
6565 .vb_bh
= new_blk_bh
,
6566 .vb_access
= ocfs2_journal_access_xb
,
6569 ret
= ocfs2_reflink_lock_xattr_allocators(osb
, xh
, args
->ref_root_bh
,
6570 &credits
, &meta_ac
);
6576 /* One more credits in case we need to add xattr flags in new inode. */
6577 handle
= ocfs2_start_trans(osb
, credits
+ 1);
6578 if (IS_ERR(handle
)) {
6579 ret
= PTR_ERR(handle
);
6584 if (!(new_oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
)) {
6585 ret
= ocfs2_journal_access_di(handle
,
6586 INODE_CACHE(args
->new_inode
),
6588 OCFS2_JOURNAL_ACCESS_WRITE
);
6595 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(args
->new_inode
),
6596 new_blk_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
6602 memcpy(new_blk_bh
->b_data
+ header_off
, blk_bh
->b_data
+ header_off
,
6603 osb
->sb
->s_blocksize
- header_off
);
6605 ret
= ocfs2_reflink_xattr_header(handle
, args
, blk_bh
, xh
,
6606 new_blk_bh
, new_xh
, &vb
, meta_ac
,
6607 ocfs2_get_xattr_value_root
, NULL
);
6613 ocfs2_journal_dirty(handle
, new_blk_bh
);
6615 if (!(new_oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
)) {
6616 new_di
= (struct ocfs2_dinode
*)args
->new_bh
->b_data
;
6617 spin_lock(&new_oi
->ip_lock
);
6618 new_oi
->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
;
6619 new_di
->i_dyn_features
= cpu_to_le16(new_oi
->ip_dyn_features
);
6620 spin_unlock(&new_oi
->ip_lock
);
6622 ocfs2_journal_dirty(handle
, args
->new_bh
);
6626 ocfs2_commit_trans(osb
, handle
);
6629 ocfs2_free_alloc_context(meta_ac
);
6633 struct ocfs2_reflink_xattr_tree_args
{
6634 struct ocfs2_xattr_reflink
*reflink
;
6635 struct buffer_head
*old_blk_bh
;
6636 struct buffer_head
*new_blk_bh
;
6637 struct ocfs2_xattr_bucket
*old_bucket
;
6638 struct ocfs2_xattr_bucket
*new_bucket
;
6643 * We have to handle the case that both old bucket and new bucket
6644 * will call this function to get the right ret_bh.
6645 * So The caller must give us the right bh.
6647 static int ocfs2_get_reflink_xattr_value_root(struct super_block
*sb
,
6648 struct buffer_head
*bh
,
6649 struct ocfs2_xattr_header
*xh
,
6651 struct ocfs2_xattr_value_root
**xv
,
6652 struct buffer_head
**ret_bh
,
6655 struct ocfs2_reflink_xattr_tree_args
*args
=
6656 (struct ocfs2_reflink_xattr_tree_args
*)para
;
6657 struct ocfs2_xattr_bucket
*bucket
;
6659 if (bh
== args
->old_bucket
->bu_bhs
[0])
6660 bucket
= args
->old_bucket
;
6662 bucket
= args
->new_bucket
;
6664 return ocfs2_get_xattr_tree_value_root(sb
, bucket
, offset
,
6668 struct ocfs2_value_tree_metas
{
6674 static int ocfs2_value_tree_metas_in_bucket(struct super_block
*sb
,
6675 struct buffer_head
*bh
,
6676 struct ocfs2_xattr_header
*xh
,
6678 struct ocfs2_xattr_value_root
**xv
,
6679 struct buffer_head
**ret_bh
,
6682 struct ocfs2_xattr_bucket
*bucket
=
6683 (struct ocfs2_xattr_bucket
*)para
;
6685 return ocfs2_get_xattr_tree_value_root(sb
, bucket
, offset
,
6689 static int ocfs2_calc_value_tree_metas(struct inode
*inode
,
6690 struct ocfs2_xattr_bucket
*bucket
,
6693 struct ocfs2_value_tree_metas
*metas
=
6694 (struct ocfs2_value_tree_metas
*)para
;
6695 struct ocfs2_xattr_header
*xh
=
6696 (struct ocfs2_xattr_header
*)bucket
->bu_bhs
[0]->b_data
;
6698 /* Add the credits for this bucket first. */
6699 metas
->credits
+= bucket
->bu_blocks
;
6700 return ocfs2_value_metas_in_xattr_header(inode
->i_sb
, bucket
->bu_bhs
[0],
6701 xh
, &metas
->num_metas
,
6702 &metas
->credits
, &metas
->num_recs
,
6703 ocfs2_value_tree_metas_in_bucket
,
6708 * Given a xattr extent rec starting from blkno and having len clusters,
6709 * iterate all the buckets calculate how much metadata we need for reflinking
6710 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6712 static int ocfs2_lock_reflink_xattr_rec_allocators(
6713 struct ocfs2_reflink_xattr_tree_args
*args
,
6714 struct ocfs2_extent_tree
*xt_et
,
6715 u64 blkno
, u32 len
, int *credits
,
6716 struct ocfs2_alloc_context
**meta_ac
,
6717 struct ocfs2_alloc_context
**data_ac
)
6719 int ret
, num_free_extents
;
6720 struct ocfs2_value_tree_metas metas
;
6721 struct ocfs2_super
*osb
= OCFS2_SB(args
->reflink
->old_inode
->i_sb
);
6722 struct ocfs2_refcount_block
*rb
;
6724 memset(&metas
, 0, sizeof(metas
));
6726 ret
= ocfs2_iterate_xattr_buckets(args
->reflink
->old_inode
, blkno
, len
,
6727 ocfs2_calc_value_tree_metas
, &metas
);
6733 *credits
= metas
.credits
;
6736 * Calculate we need for refcount tree change.
6738 * We need to add/modify num_recs in refcount tree, so just calculate
6739 * an approximate number we need for refcount tree change.
6740 * Sometimes we need to split the tree, and after split, half recs
6741 * will be moved to the new block, and a new block can only provide
6742 * half number of recs. So we multiple new blocks by 2.
6743 * In the end, we have to add credits for modifying the already
6744 * existed refcount block.
6746 rb
= (struct ocfs2_refcount_block
*)args
->reflink
->ref_root_bh
->b_data
;
6748 (metas
.num_recs
+ ocfs2_refcount_recs_per_rb(osb
->sb
) - 1) /
6749 ocfs2_refcount_recs_per_rb(osb
->sb
) * 2;
6750 metas
.num_metas
+= metas
.num_recs
;
6751 *credits
+= metas
.num_recs
+
6752 metas
.num_recs
* OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
6753 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)
6754 *credits
+= le16_to_cpu(rb
->rf_list
.l_tree_depth
) *
6755 le16_to_cpu(rb
->rf_list
.l_next_free_rec
) + 1;
6759 /* count in the xattr tree change. */
6760 num_free_extents
= ocfs2_num_free_extents(osb
, xt_et
);
6761 if (num_free_extents
< 0) {
6762 ret
= num_free_extents
;
6767 if (num_free_extents
< len
)
6768 metas
.num_metas
+= ocfs2_extend_meta_needed(xt_et
->et_root_el
);
6770 *credits
+= ocfs2_calc_extend_credits(osb
->sb
,
6771 xt_et
->et_root_el
, len
);
6773 if (metas
.num_metas
) {
6774 ret
= ocfs2_reserve_new_metadata_blocks(osb
, metas
.num_metas
,
6783 ret
= ocfs2_reserve_clusters(osb
, len
, data_ac
);
6790 ocfs2_free_alloc_context(*meta_ac
);
6798 static int ocfs2_reflink_xattr_bucket(handle_t
*handle
,
6799 u64 blkno
, u64 new_blkno
, u32 clusters
,
6800 u32
*cpos
, int num_buckets
,
6801 struct ocfs2_alloc_context
*meta_ac
,
6802 struct ocfs2_alloc_context
*data_ac
,
6803 struct ocfs2_reflink_xattr_tree_args
*args
)
6806 struct super_block
*sb
= args
->reflink
->old_inode
->i_sb
;
6807 int bpb
= args
->old_bucket
->bu_blocks
;
6808 struct ocfs2_xattr_value_buf vb
= {
6809 .vb_access
= ocfs2_journal_access
,
6812 for (i
= 0; i
< num_buckets
; i
++, blkno
+= bpb
, new_blkno
+= bpb
) {
6813 ret
= ocfs2_read_xattr_bucket(args
->old_bucket
, blkno
);
6819 ret
= ocfs2_init_xattr_bucket(args
->new_bucket
, new_blkno
);
6825 ret
= ocfs2_xattr_bucket_journal_access(handle
,
6827 OCFS2_JOURNAL_ACCESS_CREATE
);
6833 for (j
= 0; j
< bpb
; j
++)
6834 memcpy(bucket_block(args
->new_bucket
, j
),
6835 bucket_block(args
->old_bucket
, j
),
6839 * Record the start cpos so that we can use it to initialize
6840 * our xattr tree we also set the xh_num_bucket for the new
6844 *cpos
= le32_to_cpu(bucket_xh(args
->new_bucket
)->
6845 xh_entries
[0].xe_name_hash
);
6846 bucket_xh(args
->new_bucket
)->xh_num_buckets
=
6847 cpu_to_le16(num_buckets
);
6850 ocfs2_xattr_bucket_journal_dirty(handle
, args
->new_bucket
);
6852 ret
= ocfs2_reflink_xattr_header(handle
, args
->reflink
,
6853 args
->old_bucket
->bu_bhs
[0],
6854 bucket_xh(args
->old_bucket
),
6855 args
->new_bucket
->bu_bhs
[0],
6856 bucket_xh(args
->new_bucket
),
6858 ocfs2_get_reflink_xattr_value_root
,
6866 * Re-access and dirty the bucket to calculate metaecc.
6867 * Because we may extend the transaction in reflink_xattr_header
6868 * which will let the already accessed block gone.
6870 ret
= ocfs2_xattr_bucket_journal_access(handle
,
6872 OCFS2_JOURNAL_ACCESS_WRITE
);
6878 ocfs2_xattr_bucket_journal_dirty(handle
, args
->new_bucket
);
6880 ocfs2_xattr_bucket_relse(args
->old_bucket
);
6881 ocfs2_xattr_bucket_relse(args
->new_bucket
);
6884 ocfs2_xattr_bucket_relse(args
->old_bucket
);
6885 ocfs2_xattr_bucket_relse(args
->new_bucket
);
6889 static int ocfs2_reflink_xattr_buckets(handle_t
*handle
,
6890 struct inode
*inode
,
6891 struct ocfs2_reflink_xattr_tree_args
*args
,
6892 struct ocfs2_extent_tree
*et
,
6893 struct ocfs2_alloc_context
*meta_ac
,
6894 struct ocfs2_alloc_context
*data_ac
,
6895 u64 blkno
, u32 cpos
, u32 len
)
6897 int ret
, first_inserted
= 0;
6898 u32 p_cluster
, num_clusters
, reflink_cpos
= 0;
6900 unsigned int num_buckets
, reflink_buckets
;
6902 ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
6904 ret
= ocfs2_read_xattr_bucket(args
->old_bucket
, blkno
);
6909 num_buckets
= le16_to_cpu(bucket_xh(args
->old_bucket
)->xh_num_buckets
);
6910 ocfs2_xattr_bucket_relse(args
->old_bucket
);
6912 while (len
&& num_buckets
) {
6913 ret
= ocfs2_claim_clusters(handle
, data_ac
,
6914 1, &p_cluster
, &num_clusters
);
6920 new_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
6921 reflink_buckets
= min(num_buckets
, bpc
* num_clusters
);
6923 ret
= ocfs2_reflink_xattr_bucket(handle
, blkno
,
6924 new_blkno
, num_clusters
,
6925 &reflink_cpos
, reflink_buckets
,
6926 meta_ac
, data_ac
, args
);
6933 * For the 1st allocated cluster, we make it use the same cpos
6934 * so that the xattr tree looks the same as the original one
6937 if (!first_inserted
) {
6938 reflink_cpos
= cpos
;
6941 ret
= ocfs2_insert_extent(handle
, et
, reflink_cpos
, new_blkno
,
6942 num_clusters
, 0, meta_ac
);
6946 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6947 (unsigned long long)new_blkno
, num_clusters
, reflink_cpos
);
6949 len
-= num_clusters
;
6950 blkno
+= ocfs2_clusters_to_blocks(inode
->i_sb
, num_clusters
);
6951 num_buckets
-= reflink_buckets
;
6958 * Create the same xattr extent record in the new inode's xattr tree.
6960 static int ocfs2_reflink_xattr_rec(struct inode
*inode
,
6961 struct buffer_head
*root_bh
,
6967 int ret
, credits
= 0;
6969 struct ocfs2_reflink_xattr_tree_args
*args
=
6970 (struct ocfs2_reflink_xattr_tree_args
*)para
;
6971 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6972 struct ocfs2_alloc_context
*meta_ac
= NULL
;
6973 struct ocfs2_alloc_context
*data_ac
= NULL
;
6974 struct ocfs2_extent_tree et
;
6976 mlog(0, "reflink xattr buckets %llu len %u\n",
6977 (unsigned long long)blkno
, len
);
6979 ocfs2_init_xattr_tree_extent_tree(&et
,
6980 INODE_CACHE(args
->reflink
->new_inode
),
6983 ret
= ocfs2_lock_reflink_xattr_rec_allocators(args
, &et
, blkno
,
6985 &meta_ac
, &data_ac
);
6991 handle
= ocfs2_start_trans(osb
, credits
);
6992 if (IS_ERR(handle
)) {
6993 ret
= PTR_ERR(handle
);
6998 ret
= ocfs2_reflink_xattr_buckets(handle
, inode
, args
, &et
,
7004 ocfs2_commit_trans(osb
, handle
);
7008 ocfs2_free_alloc_context(meta_ac
);
7010 ocfs2_free_alloc_context(data_ac
);
7015 * Create reflinked xattr buckets.
7016 * We will add bucket one by one, and refcount all the xattrs in the bucket
7017 * if they are stored outside.
7019 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink
*args
,
7020 struct buffer_head
*blk_bh
,
7021 struct buffer_head
*new_blk_bh
)
7024 struct ocfs2_reflink_xattr_tree_args para
;
7026 memset(¶
, 0, sizeof(para
));
7027 para
.reflink
= args
;
7028 para
.old_blk_bh
= blk_bh
;
7029 para
.new_blk_bh
= new_blk_bh
;
7031 para
.old_bucket
= ocfs2_xattr_bucket_new(args
->old_inode
);
7032 if (!para
.old_bucket
) {
7033 mlog_errno(-ENOMEM
);
7037 para
.new_bucket
= ocfs2_xattr_bucket_new(args
->new_inode
);
7038 if (!para
.new_bucket
) {
7044 ret
= ocfs2_iterate_xattr_index_block(args
->old_inode
, blk_bh
,
7045 ocfs2_reflink_xattr_rec
,
7051 ocfs2_xattr_bucket_free(para
.old_bucket
);
7052 ocfs2_xattr_bucket_free(para
.new_bucket
);
7056 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink
*args
,
7057 struct buffer_head
*blk_bh
)
7059 int ret
, indexed
= 0;
7060 struct buffer_head
*new_blk_bh
= NULL
;
7061 struct ocfs2_xattr_block
*xb
=
7062 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
7065 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)
7068 ret
= ocfs2_create_empty_xattr_block(args
->new_inode
, args
->new_bh
,
7069 &new_blk_bh
, indexed
);
7075 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
))
7076 ret
= ocfs2_reflink_xattr_block(args
, blk_bh
, new_blk_bh
);
7078 ret
= ocfs2_reflink_xattr_tree(args
, blk_bh
, new_blk_bh
);
7087 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry
*xe
)
7089 int type
= ocfs2_xattr_get_type(xe
);
7091 return type
!= OCFS2_XATTR_INDEX_SECURITY
&&
7092 type
!= OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
&&
7093 type
!= OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
;
7096 int ocfs2_reflink_xattrs(struct inode
*old_inode
,
7097 struct buffer_head
*old_bh
,
7098 struct inode
*new_inode
,
7099 struct buffer_head
*new_bh
,
7100 bool preserve_security
)
7103 struct ocfs2_xattr_reflink args
;
7104 struct ocfs2_inode_info
*oi
= OCFS2_I(old_inode
);
7105 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)old_bh
->b_data
;
7106 struct buffer_head
*blk_bh
= NULL
;
7107 struct ocfs2_cached_dealloc_ctxt dealloc
;
7108 struct ocfs2_refcount_tree
*ref_tree
;
7109 struct buffer_head
*ref_root_bh
= NULL
;
7111 ret
= ocfs2_lock_refcount_tree(OCFS2_SB(old_inode
->i_sb
),
7112 le64_to_cpu(di
->i_refcount_loc
),
7113 1, &ref_tree
, &ref_root_bh
);
7119 ocfs2_init_dealloc_ctxt(&dealloc
);
7121 args
.old_inode
= old_inode
;
7122 args
.new_inode
= new_inode
;
7123 args
.old_bh
= old_bh
;
7124 args
.new_bh
= new_bh
;
7125 args
.ref_ci
= &ref_tree
->rf_ci
;
7126 args
.ref_root_bh
= ref_root_bh
;
7127 args
.dealloc
= &dealloc
;
7128 if (preserve_security
)
7129 args
.xattr_reflinked
= NULL
;
7131 args
.xattr_reflinked
= ocfs2_reflink_xattr_no_security
;
7133 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
7134 ret
= ocfs2_reflink_xattr_inline(&args
);
7141 if (!di
->i_xattr_loc
)
7144 ret
= ocfs2_read_xattr_block(old_inode
, le64_to_cpu(di
->i_xattr_loc
),
7151 ret
= ocfs2_reflink_xattr_in_block(&args
, blk_bh
);
7158 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode
->i_sb
),
7160 brelse(ref_root_bh
);
7162 if (ocfs2_dealloc_has_cluster(&dealloc
)) {
7163 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode
->i_sb
), 1);
7164 ocfs2_run_deallocs(OCFS2_SB(old_inode
->i_sb
), &dealloc
);
7172 * Initialize security and acl for a already created inode.
7173 * Used for reflink a non-preserve-security file.
7175 * It uses common api like ocfs2_xattr_set, so the caller
7176 * must not hold any lock expect i_mutex.
7178 int ocfs2_init_security_and_acl(struct inode
*dir
,
7179 struct inode
*inode
)
7182 struct buffer_head
*dir_bh
= NULL
;
7183 struct ocfs2_security_xattr_info si
= {
7187 ret
= ocfs2_init_security_get(inode
, dir
, &si
);
7189 ret
= ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_SECURITY
,
7190 si
.name
, si
.value
, si
.value_len
,
7196 } else if (ret
!= -EOPNOTSUPP
) {
7201 ret
= ocfs2_inode_lock(dir
, &dir_bh
, 0);
7207 ret
= ocfs2_init_acl(NULL
, inode
, dir
, NULL
, dir_bh
, NULL
, NULL
);
7211 ocfs2_inode_unlock(dir
, 0);
7217 * 'security' attributes support
7219 static size_t ocfs2_xattr_security_list(struct dentry
*dentry
, char *list
,
7220 size_t list_size
, const char *name
,
7221 size_t name_len
, int type
)
7223 const size_t prefix_len
= XATTR_SECURITY_PREFIX_LEN
;
7224 const size_t total_len
= prefix_len
+ name_len
+ 1;
7226 if (list
&& total_len
<= list_size
) {
7227 memcpy(list
, XATTR_SECURITY_PREFIX
, prefix_len
);
7228 memcpy(list
+ prefix_len
, name
, name_len
);
7229 list
[prefix_len
+ name_len
] = '\0';
7234 static int ocfs2_xattr_security_get(struct dentry
*dentry
, const char *name
,
7235 void *buffer
, size_t size
, int type
)
7237 if (strcmp(name
, "") == 0)
7239 return ocfs2_xattr_get(dentry
->d_inode
, OCFS2_XATTR_INDEX_SECURITY
,
7240 name
, buffer
, size
);
7243 static int ocfs2_xattr_security_set(struct dentry
*dentry
, const char *name
,
7244 const void *value
, size_t size
, int flags
, int type
)
7246 if (strcmp(name
, "") == 0)
7249 return ocfs2_xattr_set(dentry
->d_inode
, OCFS2_XATTR_INDEX_SECURITY
,
7250 name
, value
, size
, flags
);
7253 int ocfs2_init_security_get(struct inode
*inode
,
7255 struct ocfs2_security_xattr_info
*si
)
7257 /* check whether ocfs2 support feature xattr */
7258 if (!ocfs2_supports_xattr(OCFS2_SB(dir
->i_sb
)))
7260 return security_inode_init_security(inode
, dir
, &si
->name
, &si
->value
,
7264 int ocfs2_init_security_set(handle_t
*handle
,
7265 struct inode
*inode
,
7266 struct buffer_head
*di_bh
,
7267 struct ocfs2_security_xattr_info
*si
,
7268 struct ocfs2_alloc_context
*xattr_ac
,
7269 struct ocfs2_alloc_context
*data_ac
)
7271 return ocfs2_xattr_set_handle(handle
, inode
, di_bh
,
7272 OCFS2_XATTR_INDEX_SECURITY
,
7273 si
->name
, si
->value
, si
->value_len
, 0,
7277 const struct xattr_handler ocfs2_xattr_security_handler
= {
7278 .prefix
= XATTR_SECURITY_PREFIX
,
7279 .list
= ocfs2_xattr_security_list
,
7280 .get
= ocfs2_xattr_security_get
,
7281 .set
= ocfs2_xattr_security_set
,
7285 * 'trusted' attributes support
7287 static size_t ocfs2_xattr_trusted_list(struct dentry
*dentry
, char *list
,
7288 size_t list_size
, const char *name
,
7289 size_t name_len
, int type
)
7291 const size_t prefix_len
= XATTR_TRUSTED_PREFIX_LEN
;
7292 const size_t total_len
= prefix_len
+ name_len
+ 1;
7294 if (list
&& total_len
<= list_size
) {
7295 memcpy(list
, XATTR_TRUSTED_PREFIX
, prefix_len
);
7296 memcpy(list
+ prefix_len
, name
, name_len
);
7297 list
[prefix_len
+ name_len
] = '\0';
7302 static int ocfs2_xattr_trusted_get(struct dentry
*dentry
, const char *name
,
7303 void *buffer
, size_t size
, int type
)
7305 if (strcmp(name
, "") == 0)
7307 return ocfs2_xattr_get(dentry
->d_inode
, OCFS2_XATTR_INDEX_TRUSTED
,
7308 name
, buffer
, size
);
7311 static int ocfs2_xattr_trusted_set(struct dentry
*dentry
, const char *name
,
7312 const void *value
, size_t size
, int flags
, int type
)
7314 if (strcmp(name
, "") == 0)
7317 return ocfs2_xattr_set(dentry
->d_inode
, OCFS2_XATTR_INDEX_TRUSTED
,
7318 name
, value
, size
, flags
);
7321 const struct xattr_handler ocfs2_xattr_trusted_handler
= {
7322 .prefix
= XATTR_TRUSTED_PREFIX
,
7323 .list
= ocfs2_xattr_trusted_list
,
7324 .get
= ocfs2_xattr_trusted_get
,
7325 .set
= ocfs2_xattr_trusted_set
,
7329 * 'user' attributes support
7331 static size_t ocfs2_xattr_user_list(struct dentry
*dentry
, char *list
,
7332 size_t list_size
, const char *name
,
7333 size_t name_len
, int type
)
7335 const size_t prefix_len
= XATTR_USER_PREFIX_LEN
;
7336 const size_t total_len
= prefix_len
+ name_len
+ 1;
7337 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
7339 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7342 if (list
&& total_len
<= list_size
) {
7343 memcpy(list
, XATTR_USER_PREFIX
, prefix_len
);
7344 memcpy(list
+ prefix_len
, name
, name_len
);
7345 list
[prefix_len
+ name_len
] = '\0';
7350 static int ocfs2_xattr_user_get(struct dentry
*dentry
, const char *name
,
7351 void *buffer
, size_t size
, int type
)
7353 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
7355 if (strcmp(name
, "") == 0)
7357 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7359 return ocfs2_xattr_get(dentry
->d_inode
, OCFS2_XATTR_INDEX_USER
, name
,
7363 static int ocfs2_xattr_user_set(struct dentry
*dentry
, const char *name
,
7364 const void *value
, size_t size
, int flags
, int type
)
7366 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
7368 if (strcmp(name
, "") == 0)
7370 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7373 return ocfs2_xattr_set(dentry
->d_inode
, OCFS2_XATTR_INDEX_USER
,
7374 name
, value
, size
, flags
);
7377 const struct xattr_handler ocfs2_xattr_user_handler
= {
7378 .prefix
= XATTR_USER_PREFIX
,
7379 .list
= ocfs2_xattr_user_list
,
7380 .get
= ocfs2_xattr_user_get
,
7381 .set
= ocfs2_xattr_user_set
,