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
)
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 status
= vb
->vb_access(handle
, INODE_CACHE(inode
), vb
->vb_bh
,
723 OCFS2_JOURNAL_ACCESS_WRITE
);
729 prev_clusters
= le32_to_cpu(vb
->vb_xv
->xr_clusters
);
730 status
= ocfs2_add_clusters_in_btree(handle
,
743 ocfs2_journal_dirty(handle
, vb
->vb_bh
);
745 clusters_to_add
-= le32_to_cpu(vb
->vb_xv
->xr_clusters
) - prev_clusters
;
748 * We should have already allocated enough space before the transaction,
749 * so no need to restart.
751 BUG_ON(why
!= RESTART_NONE
|| clusters_to_add
);
758 static int __ocfs2_remove_xattr_range(struct inode
*inode
,
759 struct ocfs2_xattr_value_buf
*vb
,
760 u32 cpos
, u32 phys_cpos
, u32 len
,
761 unsigned int ext_flags
,
762 struct ocfs2_xattr_set_ctxt
*ctxt
)
765 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
766 handle_t
*handle
= ctxt
->handle
;
767 struct ocfs2_extent_tree et
;
769 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
771 ret
= vb
->vb_access(handle
, INODE_CACHE(inode
), vb
->vb_bh
,
772 OCFS2_JOURNAL_ACCESS_WRITE
);
778 ret
= ocfs2_remove_extent(handle
, &et
, cpos
, len
, ctxt
->meta_ac
,
785 le32_add_cpu(&vb
->vb_xv
->xr_clusters
, -len
);
786 ocfs2_journal_dirty(handle
, vb
->vb_bh
);
788 if (ext_flags
& OCFS2_EXT_REFCOUNTED
)
789 ret
= ocfs2_decrease_refcount(inode
, handle
,
790 ocfs2_blocks_to_clusters(inode
->i_sb
,
792 len
, ctxt
->meta_ac
, &ctxt
->dealloc
, 1);
794 ret
= ocfs2_cache_cluster_dealloc(&ctxt
->dealloc
,
803 static int ocfs2_xattr_shrink_size(struct inode
*inode
,
806 struct ocfs2_xattr_value_buf
*vb
,
807 struct ocfs2_xattr_set_ctxt
*ctxt
)
810 unsigned int ext_flags
;
811 u32 trunc_len
, cpos
, phys_cpos
, alloc_size
;
814 if (old_clusters
<= new_clusters
)
818 trunc_len
= old_clusters
- new_clusters
;
820 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &phys_cpos
,
822 &vb
->vb_xv
->xr_list
, &ext_flags
);
828 if (alloc_size
> trunc_len
)
829 alloc_size
= trunc_len
;
831 ret
= __ocfs2_remove_xattr_range(inode
, vb
, cpos
,
832 phys_cpos
, alloc_size
,
839 block
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
840 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode
),
843 trunc_len
-= alloc_size
;
850 static int ocfs2_xattr_value_truncate(struct inode
*inode
,
851 struct ocfs2_xattr_value_buf
*vb
,
853 struct ocfs2_xattr_set_ctxt
*ctxt
)
856 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, len
);
857 u32 old_clusters
= le32_to_cpu(vb
->vb_xv
->xr_clusters
);
859 if (new_clusters
== old_clusters
)
862 if (new_clusters
> old_clusters
)
863 ret
= ocfs2_xattr_extend_allocation(inode
,
864 new_clusters
- old_clusters
,
867 ret
= ocfs2_xattr_shrink_size(inode
,
868 old_clusters
, new_clusters
,
874 static int ocfs2_xattr_list_entry(char *buffer
, size_t size
,
875 size_t *result
, const char *prefix
,
876 const char *name
, int name_len
)
878 char *p
= buffer
+ *result
;
879 int prefix_len
= strlen(prefix
);
880 int total_len
= prefix_len
+ name_len
+ 1;
882 *result
+= total_len
;
884 /* we are just looking for how big our buffer needs to be */
891 memcpy(p
, prefix
, prefix_len
);
892 memcpy(p
+ prefix_len
, name
, name_len
);
893 p
[prefix_len
+ name_len
] = '\0';
898 static int ocfs2_xattr_list_entries(struct inode
*inode
,
899 struct ocfs2_xattr_header
*header
,
900 char *buffer
, size_t buffer_size
)
904 const char *prefix
, *name
;
906 for (i
= 0 ; i
< le16_to_cpu(header
->xh_count
); i
++) {
907 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
908 type
= ocfs2_xattr_get_type(entry
);
909 prefix
= ocfs2_xattr_prefix(type
);
912 name
= (const char *)header
+
913 le16_to_cpu(entry
->xe_name_offset
);
915 ret
= ocfs2_xattr_list_entry(buffer
, buffer_size
,
916 &result
, prefix
, name
,
926 int ocfs2_has_inline_xattr_value_outside(struct inode
*inode
,
927 struct ocfs2_dinode
*di
)
929 struct ocfs2_xattr_header
*xh
;
932 xh
= (struct ocfs2_xattr_header
*)
933 ((void *)di
+ inode
->i_sb
->s_blocksize
-
934 le16_to_cpu(di
->i_xattr_inline_size
));
936 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++)
937 if (!ocfs2_xattr_is_local(&xh
->xh_entries
[i
]))
943 static int ocfs2_xattr_ibody_list(struct inode
*inode
,
944 struct ocfs2_dinode
*di
,
948 struct ocfs2_xattr_header
*header
= NULL
;
949 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
952 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
955 header
= (struct ocfs2_xattr_header
*)
956 ((void *)di
+ inode
->i_sb
->s_blocksize
-
957 le16_to_cpu(di
->i_xattr_inline_size
));
959 ret
= ocfs2_xattr_list_entries(inode
, header
, buffer
, buffer_size
);
964 static int ocfs2_xattr_block_list(struct inode
*inode
,
965 struct ocfs2_dinode
*di
,
969 struct buffer_head
*blk_bh
= NULL
;
970 struct ocfs2_xattr_block
*xb
;
973 if (!di
->i_xattr_loc
)
976 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
983 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
984 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
985 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
986 ret
= ocfs2_xattr_list_entries(inode
, header
,
987 buffer
, buffer_size
);
989 ret
= ocfs2_xattr_tree_list_index_block(inode
, blk_bh
,
990 buffer
, buffer_size
);
997 ssize_t
ocfs2_listxattr(struct dentry
*dentry
,
1001 int ret
= 0, i_ret
= 0, b_ret
= 0;
1002 struct buffer_head
*di_bh
= NULL
;
1003 struct ocfs2_dinode
*di
= NULL
;
1004 struct ocfs2_inode_info
*oi
= OCFS2_I(dentry
->d_inode
);
1006 if (!ocfs2_supports_xattr(OCFS2_SB(dentry
->d_sb
)))
1009 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1012 ret
= ocfs2_inode_lock(dentry
->d_inode
, &di_bh
, 0);
1018 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1020 down_read(&oi
->ip_xattr_sem
);
1021 i_ret
= ocfs2_xattr_ibody_list(dentry
->d_inode
, di
, buffer
, size
);
1029 b_ret
= ocfs2_xattr_block_list(dentry
->d_inode
, di
,
1034 up_read(&oi
->ip_xattr_sem
);
1035 ocfs2_inode_unlock(dentry
->d_inode
, 0);
1039 return i_ret
+ b_ret
;
1042 static int ocfs2_xattr_find_entry(int name_index
,
1044 struct ocfs2_xattr_search
*xs
)
1046 struct ocfs2_xattr_entry
*entry
;
1053 name_len
= strlen(name
);
1055 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1056 cmp
= name_index
- ocfs2_xattr_get_type(entry
);
1058 cmp
= name_len
- entry
->xe_name_len
;
1060 cmp
= memcmp(name
, (xs
->base
+
1061 le16_to_cpu(entry
->xe_name_offset
)),
1069 return cmp
? -ENODATA
: 0;
1072 static int ocfs2_xattr_get_value_outside(struct inode
*inode
,
1073 struct ocfs2_xattr_value_root
*xv
,
1077 u32 cpos
, p_cluster
, num_clusters
, bpc
, clusters
;
1080 size_t cplen
, blocksize
;
1081 struct buffer_head
*bh
= NULL
;
1082 struct ocfs2_extent_list
*el
;
1085 clusters
= le32_to_cpu(xv
->xr_clusters
);
1086 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
1087 blocksize
= inode
->i_sb
->s_blocksize
;
1090 while (cpos
< clusters
) {
1091 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
1092 &num_clusters
, el
, NULL
);
1098 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
1099 /* Copy ocfs2_xattr_value */
1100 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
1101 ret
= ocfs2_read_block(INODE_CACHE(inode
), blkno
,
1108 cplen
= len
>= blocksize
? blocksize
: len
;
1109 memcpy(buffer
, bh
->b_data
, cplen
);
1118 cpos
+= num_clusters
;
1124 static int ocfs2_xattr_ibody_get(struct inode
*inode
,
1129 struct ocfs2_xattr_search
*xs
)
1131 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1132 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1133 struct ocfs2_xattr_value_root
*xv
;
1137 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
1140 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
1141 xs
->header
= (struct ocfs2_xattr_header
*)
1142 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
1143 xs
->base
= (void *)xs
->header
;
1144 xs
->here
= xs
->header
->xh_entries
;
1146 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1149 size
= le64_to_cpu(xs
->here
->xe_value_size
);
1151 if (size
> buffer_size
)
1153 if (ocfs2_xattr_is_local(xs
->here
)) {
1154 memcpy(buffer
, (void *)xs
->base
+
1155 le16_to_cpu(xs
->here
->xe_name_offset
) +
1156 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
), size
);
1158 xv
= (struct ocfs2_xattr_value_root
*)
1159 (xs
->base
+ le16_to_cpu(
1160 xs
->here
->xe_name_offset
) +
1161 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
));
1162 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
1174 static int ocfs2_xattr_block_get(struct inode
*inode
,
1179 struct ocfs2_xattr_search
*xs
)
1181 struct ocfs2_xattr_block
*xb
;
1182 struct ocfs2_xattr_value_root
*xv
;
1184 int ret
= -ENODATA
, name_offset
, name_len
, i
;
1185 int uninitialized_var(block_off
);
1187 xs
->bucket
= ocfs2_xattr_bucket_new(inode
);
1194 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, xs
);
1200 if (xs
->not_found
) {
1205 xb
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
1206 size
= le64_to_cpu(xs
->here
->xe_value_size
);
1209 if (size
> buffer_size
)
1212 name_offset
= le16_to_cpu(xs
->here
->xe_name_offset
);
1213 name_len
= OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
);
1214 i
= xs
->here
- xs
->header
->xh_entries
;
1216 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
1217 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
1218 bucket_xh(xs
->bucket
),
1222 xs
->base
= bucket_block(xs
->bucket
, block_off
);
1224 if (ocfs2_xattr_is_local(xs
->here
)) {
1225 memcpy(buffer
, (void *)xs
->base
+
1226 name_offset
+ name_len
, size
);
1228 xv
= (struct ocfs2_xattr_value_root
*)
1229 (xs
->base
+ name_offset
+ name_len
);
1230 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
1240 ocfs2_xattr_bucket_free(xs
->bucket
);
1242 brelse(xs
->xattr_bh
);
1243 xs
->xattr_bh
= NULL
;
1247 int ocfs2_xattr_get_nolock(struct inode
*inode
,
1248 struct buffer_head
*di_bh
,
1255 struct ocfs2_dinode
*di
= NULL
;
1256 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1257 struct ocfs2_xattr_search xis
= {
1258 .not_found
= -ENODATA
,
1260 struct ocfs2_xattr_search xbs
= {
1261 .not_found
= -ENODATA
,
1264 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
1267 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1270 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
1271 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1273 down_read(&oi
->ip_xattr_sem
);
1274 ret
= ocfs2_xattr_ibody_get(inode
, name_index
, name
, buffer
,
1276 if (ret
== -ENODATA
&& di
->i_xattr_loc
)
1277 ret
= ocfs2_xattr_block_get(inode
, name_index
, name
, buffer
,
1279 up_read(&oi
->ip_xattr_sem
);
1284 /* ocfs2_xattr_get()
1286 * Copy an extended attribute into the buffer provided.
1287 * Buffer is NULL to compute the size of buffer required.
1289 static int ocfs2_xattr_get(struct inode
*inode
,
1296 struct buffer_head
*di_bh
= NULL
;
1298 ret
= ocfs2_inode_lock(inode
, &di_bh
, 0);
1303 ret
= ocfs2_xattr_get_nolock(inode
, di_bh
, name_index
,
1304 name
, buffer
, buffer_size
);
1306 ocfs2_inode_unlock(inode
, 0);
1313 static int __ocfs2_xattr_set_value_outside(struct inode
*inode
,
1315 struct ocfs2_xattr_value_buf
*vb
,
1319 int ret
= 0, i
, cp_len
;
1320 u16 blocksize
= inode
->i_sb
->s_blocksize
;
1321 u32 p_cluster
, num_clusters
;
1322 u32 cpos
= 0, bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
1323 u32 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, value_len
);
1325 struct buffer_head
*bh
= NULL
;
1326 unsigned int ext_flags
;
1327 struct ocfs2_xattr_value_root
*xv
= vb
->vb_xv
;
1329 BUG_ON(clusters
> le32_to_cpu(xv
->xr_clusters
));
1331 while (cpos
< clusters
) {
1332 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
1333 &num_clusters
, &xv
->xr_list
,
1340 BUG_ON(ext_flags
& OCFS2_EXT_REFCOUNTED
);
1342 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
1344 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
1345 ret
= ocfs2_read_block(INODE_CACHE(inode
), blkno
,
1352 ret
= ocfs2_journal_access(handle
,
1355 OCFS2_JOURNAL_ACCESS_WRITE
);
1361 cp_len
= value_len
> blocksize
? blocksize
: value_len
;
1362 memcpy(bh
->b_data
, value
, cp_len
);
1363 value_len
-= cp_len
;
1365 if (cp_len
< blocksize
)
1366 memset(bh
->b_data
+ cp_len
, 0,
1367 blocksize
- cp_len
);
1369 ocfs2_journal_dirty(handle
, bh
);
1374 * XXX: do we need to empty all the following
1375 * blocks in this cluster?
1380 cpos
+= num_clusters
;
1388 static int ocfs2_xa_check_space_helper(int needed_space
, int free_start
,
1396 free_space
= free_start
-
1397 sizeof(struct ocfs2_xattr_header
) -
1398 (num_entries
* sizeof(struct ocfs2_xattr_entry
)) -
1399 OCFS2_XATTR_HEADER_GAP
;
1402 if (free_space
< needed_space
)
1408 static int ocfs2_xa_journal_access(handle_t
*handle
, struct ocfs2_xa_loc
*loc
,
1411 return loc
->xl_ops
->xlo_journal_access(handle
, loc
, type
);
1414 static void ocfs2_xa_journal_dirty(handle_t
*handle
, struct ocfs2_xa_loc
*loc
)
1416 loc
->xl_ops
->xlo_journal_dirty(handle
, loc
);
1419 /* Give a pointer into the storage for the given offset */
1420 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc
*loc
, int offset
)
1422 BUG_ON(offset
>= loc
->xl_size
);
1423 return loc
->xl_ops
->xlo_offset_pointer(loc
, offset
);
1427 * Wipe the name+value pair and allow the storage to reclaim it. This
1428 * must be followed by either removal of the entry or a call to
1429 * ocfs2_xa_add_namevalue().
1431 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1433 loc
->xl_ops
->xlo_wipe_namevalue(loc
);
1437 * Find lowest offset to a name+value pair. This is the start of our
1438 * downward-growing free space.
1440 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc
*loc
)
1442 return loc
->xl_ops
->xlo_get_free_start(loc
);
1445 /* Can we reuse loc->xl_entry for xi? */
1446 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc
*loc
,
1447 struct ocfs2_xattr_info
*xi
)
1449 return loc
->xl_ops
->xlo_can_reuse(loc
, xi
);
1452 /* How much free space is needed to set the new value */
1453 static int ocfs2_xa_check_space(struct ocfs2_xa_loc
*loc
,
1454 struct ocfs2_xattr_info
*xi
)
1456 return loc
->xl_ops
->xlo_check_space(loc
, xi
);
1459 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1461 loc
->xl_ops
->xlo_add_entry(loc
, name_hash
);
1462 loc
->xl_entry
->xe_name_hash
= cpu_to_le32(name_hash
);
1464 * We can't leave the new entry's xe_name_offset at zero or
1465 * add_namevalue() will go nuts. We set it to the size of our
1466 * storage so that it can never be less than any other entry.
1468 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(loc
->xl_size
);
1471 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc
*loc
,
1472 struct ocfs2_xattr_info
*xi
)
1474 int size
= namevalue_size_xi(xi
);
1478 loc
->xl_ops
->xlo_add_namevalue(loc
, size
);
1479 loc
->xl_entry
->xe_value_size
= cpu_to_le64(xi
->xi_value_len
);
1480 loc
->xl_entry
->xe_name_len
= xi
->xi_name_len
;
1481 ocfs2_xattr_set_type(loc
->xl_entry
, xi
->xi_name_index
);
1482 ocfs2_xattr_set_local(loc
->xl_entry
,
1483 xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
);
1485 nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1486 nameval_buf
= ocfs2_xa_offset_pointer(loc
, nameval_offset
);
1487 memset(nameval_buf
, 0, size
);
1488 memcpy(nameval_buf
, xi
->xi_name
, xi
->xi_name_len
);
1491 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1492 struct ocfs2_xattr_value_buf
*vb
)
1494 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1495 int name_size
= OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
);
1497 /* Value bufs are for value trees */
1498 BUG_ON(ocfs2_xattr_is_local(loc
->xl_entry
));
1499 BUG_ON(namevalue_size_xe(loc
->xl_entry
) !=
1500 (name_size
+ OCFS2_XATTR_ROOT_SIZE
));
1502 loc
->xl_ops
->xlo_fill_value_buf(loc
, vb
);
1504 (struct ocfs2_xattr_value_root
*)ocfs2_xa_offset_pointer(loc
,
1509 static int ocfs2_xa_block_journal_access(handle_t
*handle
,
1510 struct ocfs2_xa_loc
*loc
, int type
)
1512 struct buffer_head
*bh
= loc
->xl_storage
;
1513 ocfs2_journal_access_func access
;
1515 if (loc
->xl_size
== (bh
->b_size
-
1516 offsetof(struct ocfs2_xattr_block
,
1517 xb_attrs
.xb_header
)))
1518 access
= ocfs2_journal_access_xb
;
1520 access
= ocfs2_journal_access_di
;
1521 return access(handle
, INODE_CACHE(loc
->xl_inode
), bh
, type
);
1524 static void ocfs2_xa_block_journal_dirty(handle_t
*handle
,
1525 struct ocfs2_xa_loc
*loc
)
1527 struct buffer_head
*bh
= loc
->xl_storage
;
1529 ocfs2_journal_dirty(handle
, bh
);
1532 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc
*loc
,
1535 return (char *)loc
->xl_header
+ offset
;
1538 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc
*loc
,
1539 struct ocfs2_xattr_info
*xi
)
1542 * Block storage is strict. If the sizes aren't exact, we will
1543 * remove the old one and reinsert the new.
1545 return namevalue_size_xe(loc
->xl_entry
) ==
1546 namevalue_size_xi(xi
);
1549 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc
*loc
)
1551 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1552 int i
, count
= le16_to_cpu(xh
->xh_count
);
1553 int offset
, free_start
= loc
->xl_size
;
1555 for (i
= 0; i
< count
; i
++) {
1556 offset
= le16_to_cpu(xh
->xh_entries
[i
].xe_name_offset
);
1557 if (offset
< free_start
)
1558 free_start
= offset
;
1564 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc
*loc
,
1565 struct ocfs2_xattr_info
*xi
)
1567 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1568 int free_start
= ocfs2_xa_get_free_start(loc
);
1569 int needed_space
= ocfs2_xi_entry_usage(xi
);
1572 * Block storage will reclaim the original entry before inserting
1573 * the new value, so we only need the difference. If the new
1574 * entry is smaller than the old one, we don't need anything.
1576 if (loc
->xl_entry
) {
1577 /* Don't need space if we're reusing! */
1578 if (ocfs2_xa_can_reuse_entry(loc
, xi
))
1581 needed_space
-= ocfs2_xe_entry_usage(loc
->xl_entry
);
1583 if (needed_space
< 0)
1585 return ocfs2_xa_check_space_helper(needed_space
, free_start
, count
);
1589 * Block storage for xattrs keeps the name+value pairs compacted. When
1590 * we remove one, we have to shift any that preceded it towards the end.
1592 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1595 int namevalue_offset
, first_namevalue_offset
, namevalue_size
;
1596 struct ocfs2_xattr_entry
*entry
= loc
->xl_entry
;
1597 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1598 int count
= le16_to_cpu(xh
->xh_count
);
1600 namevalue_offset
= le16_to_cpu(entry
->xe_name_offset
);
1601 namevalue_size
= namevalue_size_xe(entry
);
1602 first_namevalue_offset
= ocfs2_xa_get_free_start(loc
);
1604 /* Shift the name+value pairs */
1605 memmove((char *)xh
+ first_namevalue_offset
+ namevalue_size
,
1606 (char *)xh
+ first_namevalue_offset
,
1607 namevalue_offset
- first_namevalue_offset
);
1608 memset((char *)xh
+ first_namevalue_offset
, 0, namevalue_size
);
1610 /* Now tell xh->xh_entries about it */
1611 for (i
= 0; i
< count
; i
++) {
1612 offset
= le16_to_cpu(xh
->xh_entries
[i
].xe_name_offset
);
1613 if (offset
<= namevalue_offset
)
1614 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
,
1619 * Note that we don't update xh_free_start or xh_name_value_len
1620 * because they're not used in block-stored xattrs.
1624 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1626 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1627 loc
->xl_entry
= &(loc
->xl_header
->xh_entries
[count
]);
1628 le16_add_cpu(&loc
->xl_header
->xh_count
, 1);
1629 memset(loc
->xl_entry
, 0, sizeof(struct ocfs2_xattr_entry
));
1632 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc
*loc
, int size
)
1634 int free_start
= ocfs2_xa_get_free_start(loc
);
1636 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(free_start
- size
);
1639 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1640 struct ocfs2_xattr_value_buf
*vb
)
1642 struct buffer_head
*bh
= loc
->xl_storage
;
1644 if (loc
->xl_size
== (bh
->b_size
-
1645 offsetof(struct ocfs2_xattr_block
,
1646 xb_attrs
.xb_header
)))
1647 vb
->vb_access
= ocfs2_journal_access_xb
;
1649 vb
->vb_access
= ocfs2_journal_access_di
;
1654 * Operations for xattrs stored in blocks. This includes inline inode
1655 * storage and unindexed ocfs2_xattr_blocks.
1657 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops
= {
1658 .xlo_journal_access
= ocfs2_xa_block_journal_access
,
1659 .xlo_journal_dirty
= ocfs2_xa_block_journal_dirty
,
1660 .xlo_offset_pointer
= ocfs2_xa_block_offset_pointer
,
1661 .xlo_check_space
= ocfs2_xa_block_check_space
,
1662 .xlo_can_reuse
= ocfs2_xa_block_can_reuse
,
1663 .xlo_get_free_start
= ocfs2_xa_block_get_free_start
,
1664 .xlo_wipe_namevalue
= ocfs2_xa_block_wipe_namevalue
,
1665 .xlo_add_entry
= ocfs2_xa_block_add_entry
,
1666 .xlo_add_namevalue
= ocfs2_xa_block_add_namevalue
,
1667 .xlo_fill_value_buf
= ocfs2_xa_block_fill_value_buf
,
1670 static int ocfs2_xa_bucket_journal_access(handle_t
*handle
,
1671 struct ocfs2_xa_loc
*loc
, int type
)
1673 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1675 return ocfs2_xattr_bucket_journal_access(handle
, bucket
, type
);
1678 static void ocfs2_xa_bucket_journal_dirty(handle_t
*handle
,
1679 struct ocfs2_xa_loc
*loc
)
1681 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1683 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
1686 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc
*loc
,
1689 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1690 int block
, block_offset
;
1692 /* The header is at the front of the bucket */
1693 block
= offset
>> loc
->xl_inode
->i_sb
->s_blocksize_bits
;
1694 block_offset
= offset
% loc
->xl_inode
->i_sb
->s_blocksize
;
1696 return bucket_block(bucket
, block
) + block_offset
;
1699 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc
*loc
,
1700 struct ocfs2_xattr_info
*xi
)
1702 return namevalue_size_xe(loc
->xl_entry
) >=
1703 namevalue_size_xi(xi
);
1706 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc
*loc
)
1708 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1709 return le16_to_cpu(bucket_xh(bucket
)->xh_free_start
);
1712 static int ocfs2_bucket_align_free_start(struct super_block
*sb
,
1713 int free_start
, int size
)
1716 * We need to make sure that the name+value pair fits within
1719 if (((free_start
- size
) >> sb
->s_blocksize_bits
) !=
1720 ((free_start
- 1) >> sb
->s_blocksize_bits
))
1721 free_start
-= free_start
% sb
->s_blocksize
;
1726 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc
*loc
,
1727 struct ocfs2_xattr_info
*xi
)
1730 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1731 int free_start
= ocfs2_xa_get_free_start(loc
);
1732 int needed_space
= ocfs2_xi_entry_usage(xi
);
1733 int size
= namevalue_size_xi(xi
);
1734 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1737 * Bucket storage does not reclaim name+value pairs it cannot
1738 * reuse. They live as holes until the bucket fills, and then
1739 * the bucket is defragmented. However, the bucket can reclaim
1740 * the ocfs2_xattr_entry.
1742 if (loc
->xl_entry
) {
1743 /* Don't need space if we're reusing! */
1744 if (ocfs2_xa_can_reuse_entry(loc
, xi
))
1747 needed_space
-= sizeof(struct ocfs2_xattr_entry
);
1749 BUG_ON(needed_space
< 0);
1751 if (free_start
< size
) {
1756 * First we check if it would fit in the first place.
1757 * Below, we align the free start to a block. This may
1758 * slide us below the minimum gap. By checking unaligned
1759 * first, we avoid that error.
1761 rc
= ocfs2_xa_check_space_helper(needed_space
, free_start
,
1765 free_start
= ocfs2_bucket_align_free_start(sb
, free_start
,
1768 return ocfs2_xa_check_space_helper(needed_space
, free_start
, count
);
1771 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1773 le16_add_cpu(&loc
->xl_header
->xh_name_value_len
,
1774 -namevalue_size_xe(loc
->xl_entry
));
1777 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1779 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1780 int count
= le16_to_cpu(xh
->xh_count
);
1781 int low
= 0, high
= count
- 1, tmp
;
1782 struct ocfs2_xattr_entry
*tmp_xe
;
1785 * We keep buckets sorted by name_hash, so we need to find
1788 while (low
<= high
&& count
) {
1789 tmp
= (low
+ high
) / 2;
1790 tmp_xe
= &xh
->xh_entries
[tmp
];
1792 if (name_hash
> le32_to_cpu(tmp_xe
->xe_name_hash
))
1794 else if (name_hash
< le32_to_cpu(tmp_xe
->xe_name_hash
))
1803 memmove(&xh
->xh_entries
[low
+ 1],
1804 &xh
->xh_entries
[low
],
1805 ((count
- low
) * sizeof(struct ocfs2_xattr_entry
)));
1807 le16_add_cpu(&xh
->xh_count
, 1);
1808 loc
->xl_entry
= &xh
->xh_entries
[low
];
1809 memset(loc
->xl_entry
, 0, sizeof(struct ocfs2_xattr_entry
));
1812 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc
*loc
, int size
)
1814 int free_start
= ocfs2_xa_get_free_start(loc
);
1815 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1816 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1819 free_start
= ocfs2_bucket_align_free_start(sb
, free_start
, size
);
1820 nameval_offset
= free_start
- size
;
1821 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(nameval_offset
);
1822 xh
->xh_free_start
= cpu_to_le16(nameval_offset
);
1823 le16_add_cpu(&xh
->xh_name_value_len
, size
);
1827 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1828 struct ocfs2_xattr_value_buf
*vb
)
1830 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1831 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1832 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1833 int size
= namevalue_size_xe(loc
->xl_entry
);
1834 int block_offset
= nameval_offset
>> sb
->s_blocksize_bits
;
1836 /* Values are not allowed to straddle block boundaries */
1837 BUG_ON(block_offset
!=
1838 ((nameval_offset
+ size
- 1) >> sb
->s_blocksize_bits
));
1839 /* We expect the bucket to be filled in */
1840 BUG_ON(!bucket
->bu_bhs
[block_offset
]);
1842 vb
->vb_access
= ocfs2_journal_access
;
1843 vb
->vb_bh
= bucket
->bu_bhs
[block_offset
];
1846 /* Operations for xattrs stored in buckets. */
1847 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops
= {
1848 .xlo_journal_access
= ocfs2_xa_bucket_journal_access
,
1849 .xlo_journal_dirty
= ocfs2_xa_bucket_journal_dirty
,
1850 .xlo_offset_pointer
= ocfs2_xa_bucket_offset_pointer
,
1851 .xlo_check_space
= ocfs2_xa_bucket_check_space
,
1852 .xlo_can_reuse
= ocfs2_xa_bucket_can_reuse
,
1853 .xlo_get_free_start
= ocfs2_xa_bucket_get_free_start
,
1854 .xlo_wipe_namevalue
= ocfs2_xa_bucket_wipe_namevalue
,
1855 .xlo_add_entry
= ocfs2_xa_bucket_add_entry
,
1856 .xlo_add_namevalue
= ocfs2_xa_bucket_add_namevalue
,
1857 .xlo_fill_value_buf
= ocfs2_xa_bucket_fill_value_buf
,
1860 static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc
*loc
)
1862 struct ocfs2_xattr_value_buf vb
;
1864 if (ocfs2_xattr_is_local(loc
->xl_entry
))
1867 ocfs2_xa_fill_value_buf(loc
, &vb
);
1868 return le32_to_cpu(vb
.vb_xv
->xr_clusters
);
1871 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc
*loc
, u64 bytes
,
1872 struct ocfs2_xattr_set_ctxt
*ctxt
)
1874 int trunc_rc
, access_rc
;
1875 struct ocfs2_xattr_value_buf vb
;
1877 ocfs2_xa_fill_value_buf(loc
, &vb
);
1878 trunc_rc
= ocfs2_xattr_value_truncate(loc
->xl_inode
, &vb
, bytes
,
1882 * The caller of ocfs2_xa_value_truncate() has already called
1883 * ocfs2_xa_journal_access on the loc. However, The truncate code
1884 * calls ocfs2_extend_trans(). This may commit the previous
1885 * transaction and open a new one. If this is a bucket, truncate
1886 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1887 * the caller is expecting to dirty the entire bucket. So we must
1888 * reset the journal work. We do this even if truncate has failed,
1889 * as it could have failed after committing the extend.
1891 access_rc
= ocfs2_xa_journal_access(ctxt
->handle
, loc
,
1892 OCFS2_JOURNAL_ACCESS_WRITE
);
1894 /* Errors in truncate take precedence */
1895 return trunc_rc
? trunc_rc
: access_rc
;
1898 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc
*loc
)
1901 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1902 struct ocfs2_xattr_entry
*entry
= loc
->xl_entry
;
1904 ocfs2_xa_wipe_namevalue(loc
);
1905 loc
->xl_entry
= NULL
;
1907 le16_add_cpu(&xh
->xh_count
, -1);
1908 count
= le16_to_cpu(xh
->xh_count
);
1911 * Only zero out the entry if there are more remaining. This is
1912 * important for an empty bucket, as it keeps track of the
1913 * bucket's hash value. It doesn't hurt empty block storage.
1916 index
= ((char *)entry
- (char *)&xh
->xh_entries
) /
1917 sizeof(struct ocfs2_xattr_entry
);
1918 memmove(&xh
->xh_entries
[index
], &xh
->xh_entries
[index
+ 1],
1919 (count
- index
) * sizeof(struct ocfs2_xattr_entry
));
1920 memset(&xh
->xh_entries
[count
], 0,
1921 sizeof(struct ocfs2_xattr_entry
));
1926 * If we have a problem adjusting the size of an external value during
1927 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1928 * in an intermediate state. For example, the value may be partially
1931 * If the value tree hasn't changed, the extend/truncate went nowhere.
1932 * We have nothing to do. The caller can treat it as a straight error.
1934 * If the value tree got partially truncated, we now have a corrupted
1935 * extended attribute. We're going to wipe its entry and leak the
1936 * clusters. Better to leak some storage than leave a corrupt entry.
1938 * If the value tree grew, it obviously didn't grow enough for the
1939 * new entry. We're not going to try and reclaim those clusters either.
1940 * If there was already an external value there (orig_clusters != 0),
1941 * the new clusters are attached safely and we can just leave the old
1942 * value in place. If there was no external value there, we remove
1945 * This way, the xattr block we store in the journal will be consistent.
1946 * If the size change broke because of the journal, no changes will hit
1949 static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc
*loc
,
1951 unsigned int orig_clusters
)
1953 unsigned int new_clusters
= ocfs2_xa_value_clusters(loc
);
1954 char *nameval_buf
= ocfs2_xa_offset_pointer(loc
,
1955 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
1957 if (new_clusters
< orig_clusters
) {
1959 "Partial truncate while %s xattr %.*s. Leaking "
1960 "%u clusters and removing the entry\n",
1961 what
, loc
->xl_entry
->xe_name_len
, nameval_buf
,
1962 orig_clusters
- new_clusters
);
1963 ocfs2_xa_remove_entry(loc
);
1964 } else if (!orig_clusters
) {
1966 "Unable to allocate an external value for xattr "
1967 "%.*s safely. Leaking %u clusters and removing the "
1969 loc
->xl_entry
->xe_name_len
, nameval_buf
,
1970 new_clusters
- orig_clusters
);
1971 ocfs2_xa_remove_entry(loc
);
1972 } else if (new_clusters
> orig_clusters
)
1974 "Unable to grow xattr %.*s safely. %u new clusters "
1975 "have been added, but the value will not be "
1977 loc
->xl_entry
->xe_name_len
, nameval_buf
,
1978 new_clusters
- orig_clusters
);
1981 static int ocfs2_xa_remove(struct ocfs2_xa_loc
*loc
,
1982 struct ocfs2_xattr_set_ctxt
*ctxt
)
1985 unsigned int orig_clusters
;
1987 if (!ocfs2_xattr_is_local(loc
->xl_entry
)) {
1988 orig_clusters
= ocfs2_xa_value_clusters(loc
);
1989 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
1993 * Since this is remove, we can return 0 if
1994 * ocfs2_xa_cleanup_value_truncate() is going to
1995 * wipe the entry anyway. So we check the
1996 * cluster count as well.
1998 if (orig_clusters
!= ocfs2_xa_value_clusters(loc
))
2000 ocfs2_xa_cleanup_value_truncate(loc
, "removing",
2007 ocfs2_xa_remove_entry(loc
);
2013 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc
*loc
)
2015 int name_size
= OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
);
2018 nameval_buf
= ocfs2_xa_offset_pointer(loc
,
2019 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
2020 memcpy(nameval_buf
+ name_size
, &def_xv
, OCFS2_XATTR_ROOT_SIZE
);
2024 * Take an existing entry and make it ready for the new value. This
2025 * won't allocate space, but it may free space. It should be ready for
2026 * ocfs2_xa_prepare_entry() to finish the work.
2028 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc
*loc
,
2029 struct ocfs2_xattr_info
*xi
,
2030 struct ocfs2_xattr_set_ctxt
*ctxt
)
2033 int name_size
= OCFS2_XATTR_SIZE(xi
->xi_name_len
);
2034 unsigned int orig_clusters
;
2036 int xe_local
= ocfs2_xattr_is_local(loc
->xl_entry
);
2037 int xi_local
= xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
;
2039 BUG_ON(OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
) !=
2042 nameval_buf
= ocfs2_xa_offset_pointer(loc
,
2043 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
2045 memset(nameval_buf
+ name_size
, 0,
2046 namevalue_size_xe(loc
->xl_entry
) - name_size
);
2048 ocfs2_xa_install_value_root(loc
);
2050 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2052 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2056 memset(nameval_buf
+ name_size
, 0,
2057 namevalue_size_xe(loc
->xl_entry
) -
2059 } else if (le64_to_cpu(loc
->xl_entry
->xe_value_size
) >
2061 rc
= ocfs2_xa_value_truncate(loc
, xi
->xi_value_len
,
2068 ocfs2_xa_cleanup_value_truncate(loc
, "reusing",
2074 loc
->xl_entry
->xe_value_size
= cpu_to_le64(xi
->xi_value_len
);
2075 ocfs2_xattr_set_local(loc
->xl_entry
, xi_local
);
2082 * Prepares loc->xl_entry to receive the new xattr. This includes
2083 * properly setting up the name+value pair region. If loc->xl_entry
2084 * already exists, it will take care of modifying it appropriately.
2086 * Note that this modifies the data. You did journal_access already,
2089 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc
*loc
,
2090 struct ocfs2_xattr_info
*xi
,
2092 struct ocfs2_xattr_set_ctxt
*ctxt
)
2095 unsigned int orig_clusters
;
2096 __le64 orig_value_size
= 0;
2098 rc
= ocfs2_xa_check_space(loc
, xi
);
2102 if (loc
->xl_entry
) {
2103 if (ocfs2_xa_can_reuse_entry(loc
, xi
)) {
2104 orig_value_size
= loc
->xl_entry
->xe_value_size
;
2105 rc
= ocfs2_xa_reuse_entry(loc
, xi
, ctxt
);
2111 if (!ocfs2_xattr_is_local(loc
->xl_entry
)) {
2112 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2113 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2116 ocfs2_xa_cleanup_value_truncate(loc
,
2122 ocfs2_xa_wipe_namevalue(loc
);
2124 ocfs2_xa_add_entry(loc
, name_hash
);
2127 * If we get here, we have a blank entry. Fill it. We grow our
2128 * name+value pair back from the end.
2130 ocfs2_xa_add_namevalue(loc
, xi
);
2131 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
)
2132 ocfs2_xa_install_value_root(loc
);
2135 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2136 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2137 rc
= ocfs2_xa_value_truncate(loc
, xi
->xi_value_len
, ctxt
);
2139 ctxt
->set_abort
= 1;
2140 ocfs2_xa_cleanup_value_truncate(loc
, "growing",
2143 * If we were growing an existing value,
2144 * ocfs2_xa_cleanup_value_truncate() won't remove
2145 * the entry. We need to restore the original value
2148 if (loc
->xl_entry
) {
2149 BUG_ON(!orig_value_size
);
2150 loc
->xl_entry
->xe_value_size
= orig_value_size
;
2161 * Store the value portion of the name+value pair. This will skip
2162 * values that are stored externally. Their tree roots were set up
2163 * by ocfs2_xa_prepare_entry().
2165 static int ocfs2_xa_store_value(struct ocfs2_xa_loc
*loc
,
2166 struct ocfs2_xattr_info
*xi
,
2167 struct ocfs2_xattr_set_ctxt
*ctxt
)
2170 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
2171 int name_size
= OCFS2_XATTR_SIZE(xi
->xi_name_len
);
2173 struct ocfs2_xattr_value_buf vb
;
2175 nameval_buf
= ocfs2_xa_offset_pointer(loc
, nameval_offset
);
2176 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2177 ocfs2_xa_fill_value_buf(loc
, &vb
);
2178 rc
= __ocfs2_xattr_set_value_outside(loc
->xl_inode
,
2183 memcpy(nameval_buf
+ name_size
, xi
->xi_value
, xi
->xi_value_len
);
2188 static int ocfs2_xa_set(struct ocfs2_xa_loc
*loc
,
2189 struct ocfs2_xattr_info
*xi
,
2190 struct ocfs2_xattr_set_ctxt
*ctxt
)
2193 u32 name_hash
= ocfs2_xattr_name_hash(loc
->xl_inode
, xi
->xi_name
,
2196 ret
= ocfs2_xa_journal_access(ctxt
->handle
, loc
,
2197 OCFS2_JOURNAL_ACCESS_WRITE
);
2204 * From here on out, everything is going to modify the buffer a
2205 * little. Errors are going to leave the xattr header in a
2206 * sane state. Thus, even with errors we dirty the sucker.
2209 /* Don't worry, we are never called with !xi_value and !xl_entry */
2210 if (!xi
->xi_value
) {
2211 ret
= ocfs2_xa_remove(loc
, ctxt
);
2215 ret
= ocfs2_xa_prepare_entry(loc
, xi
, name_hash
, ctxt
);
2222 ret
= ocfs2_xa_store_value(loc
, xi
, ctxt
);
2227 ocfs2_xa_journal_dirty(ctxt
->handle
, loc
);
2233 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc
*loc
,
2234 struct inode
*inode
,
2235 struct buffer_head
*bh
,
2236 struct ocfs2_xattr_entry
*entry
)
2238 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)bh
->b_data
;
2240 BUG_ON(!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
));
2242 loc
->xl_inode
= inode
;
2243 loc
->xl_ops
= &ocfs2_xa_block_loc_ops
;
2244 loc
->xl_storage
= bh
;
2245 loc
->xl_entry
= entry
;
2246 loc
->xl_size
= le16_to_cpu(di
->i_xattr_inline_size
);
2248 (struct ocfs2_xattr_header
*)(bh
->b_data
+ bh
->b_size
-
2252 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc
*loc
,
2253 struct inode
*inode
,
2254 struct buffer_head
*bh
,
2255 struct ocfs2_xattr_entry
*entry
)
2257 struct ocfs2_xattr_block
*xb
=
2258 (struct ocfs2_xattr_block
*)bh
->b_data
;
2260 BUG_ON(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
);
2262 loc
->xl_inode
= inode
;
2263 loc
->xl_ops
= &ocfs2_xa_block_loc_ops
;
2264 loc
->xl_storage
= bh
;
2265 loc
->xl_header
= &(xb
->xb_attrs
.xb_header
);
2266 loc
->xl_entry
= entry
;
2267 loc
->xl_size
= bh
->b_size
- offsetof(struct ocfs2_xattr_block
,
2268 xb_attrs
.xb_header
);
2271 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc
*loc
,
2272 struct ocfs2_xattr_bucket
*bucket
,
2273 struct ocfs2_xattr_entry
*entry
)
2275 loc
->xl_inode
= bucket
->bu_inode
;
2276 loc
->xl_ops
= &ocfs2_xa_bucket_loc_ops
;
2277 loc
->xl_storage
= bucket
;
2278 loc
->xl_header
= bucket_xh(bucket
);
2279 loc
->xl_entry
= entry
;
2280 loc
->xl_size
= OCFS2_XATTR_BUCKET_SIZE
;
2284 * In xattr remove, if it is stored outside and refcounted, we may have
2285 * the chance to split the refcount tree. So need the allocators.
2287 static int ocfs2_lock_xattr_remove_allocators(struct inode
*inode
,
2288 struct ocfs2_xattr_value_root
*xv
,
2289 struct ocfs2_caching_info
*ref_ci
,
2290 struct buffer_head
*ref_root_bh
,
2291 struct ocfs2_alloc_context
**meta_ac
,
2294 int ret
, meta_add
= 0;
2295 u32 p_cluster
, num_clusters
;
2296 unsigned int ext_flags
;
2299 ret
= ocfs2_xattr_get_clusters(inode
, 0, &p_cluster
,
2308 if (!(ext_flags
& OCFS2_EXT_REFCOUNTED
))
2311 ret
= ocfs2_refcounted_xattr_delete_need(inode
, ref_ci
,
2313 &meta_add
, ref_credits
);
2319 ret
= ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode
->i_sb
),
2328 static int ocfs2_remove_value_outside(struct inode
*inode
,
2329 struct ocfs2_xattr_value_buf
*vb
,
2330 struct ocfs2_xattr_header
*header
,
2331 struct ocfs2_caching_info
*ref_ci
,
2332 struct buffer_head
*ref_root_bh
)
2334 int ret
= 0, i
, ref_credits
;
2335 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2336 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, };
2339 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
2341 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
2342 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
2344 if (ocfs2_xattr_is_local(entry
))
2347 val
= (void *)header
+
2348 le16_to_cpu(entry
->xe_name_offset
);
2349 vb
->vb_xv
= (struct ocfs2_xattr_value_root
*)
2350 (val
+ OCFS2_XATTR_SIZE(entry
->xe_name_len
));
2352 ret
= ocfs2_lock_xattr_remove_allocators(inode
, vb
->vb_xv
,
2353 ref_ci
, ref_root_bh
,
2357 ctxt
.handle
= ocfs2_start_trans(osb
, ref_credits
+
2358 ocfs2_remove_extent_credits(osb
->sb
));
2359 if (IS_ERR(ctxt
.handle
)) {
2360 ret
= PTR_ERR(ctxt
.handle
);
2365 ret
= ocfs2_xattr_value_truncate(inode
, vb
, 0, &ctxt
);
2371 ocfs2_commit_trans(osb
, ctxt
.handle
);
2373 ocfs2_free_alloc_context(ctxt
.meta_ac
);
2374 ctxt
.meta_ac
= NULL
;
2379 ocfs2_free_alloc_context(ctxt
.meta_ac
);
2380 ocfs2_schedule_truncate_log_flush(osb
, 1);
2381 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
2385 static int ocfs2_xattr_ibody_remove(struct inode
*inode
,
2386 struct buffer_head
*di_bh
,
2387 struct ocfs2_caching_info
*ref_ci
,
2388 struct buffer_head
*ref_root_bh
)
2391 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2392 struct ocfs2_xattr_header
*header
;
2394 struct ocfs2_xattr_value_buf vb
= {
2396 .vb_access
= ocfs2_journal_access_di
,
2399 header
= (struct ocfs2_xattr_header
*)
2400 ((void *)di
+ inode
->i_sb
->s_blocksize
-
2401 le16_to_cpu(di
->i_xattr_inline_size
));
2403 ret
= ocfs2_remove_value_outside(inode
, &vb
, header
,
2404 ref_ci
, ref_root_bh
);
2409 struct ocfs2_rm_xattr_bucket_para
{
2410 struct ocfs2_caching_info
*ref_ci
;
2411 struct buffer_head
*ref_root_bh
;
2414 static int ocfs2_xattr_block_remove(struct inode
*inode
,
2415 struct buffer_head
*blk_bh
,
2416 struct ocfs2_caching_info
*ref_ci
,
2417 struct buffer_head
*ref_root_bh
)
2419 struct ocfs2_xattr_block
*xb
;
2421 struct ocfs2_xattr_value_buf vb
= {
2423 .vb_access
= ocfs2_journal_access_xb
,
2425 struct ocfs2_rm_xattr_bucket_para args
= {
2427 .ref_root_bh
= ref_root_bh
,
2430 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2431 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2432 struct ocfs2_xattr_header
*header
= &(xb
->xb_attrs
.xb_header
);
2433 ret
= ocfs2_remove_value_outside(inode
, &vb
, header
,
2434 ref_ci
, ref_root_bh
);
2436 ret
= ocfs2_iterate_xattr_index_block(inode
,
2438 ocfs2_rm_xattr_cluster
,
2444 static int ocfs2_xattr_free_block(struct inode
*inode
,
2446 struct ocfs2_caching_info
*ref_ci
,
2447 struct buffer_head
*ref_root_bh
)
2449 struct inode
*xb_alloc_inode
;
2450 struct buffer_head
*xb_alloc_bh
= NULL
;
2451 struct buffer_head
*blk_bh
= NULL
;
2452 struct ocfs2_xattr_block
*xb
;
2453 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2459 ret
= ocfs2_read_xattr_block(inode
, block
, &blk_bh
);
2465 ret
= ocfs2_xattr_block_remove(inode
, blk_bh
, ref_ci
, ref_root_bh
);
2471 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2472 blk
= le64_to_cpu(xb
->xb_blkno
);
2473 bit
= le16_to_cpu(xb
->xb_suballoc_bit
);
2474 if (xb
->xb_suballoc_loc
)
2475 bg_blkno
= le64_to_cpu(xb
->xb_suballoc_loc
);
2477 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
2479 xb_alloc_inode
= ocfs2_get_system_file_inode(osb
,
2480 EXTENT_ALLOC_SYSTEM_INODE
,
2481 le16_to_cpu(xb
->xb_suballoc_slot
));
2482 if (!xb_alloc_inode
) {
2487 mutex_lock(&xb_alloc_inode
->i_mutex
);
2489 ret
= ocfs2_inode_lock(xb_alloc_inode
, &xb_alloc_bh
, 1);
2495 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
2496 if (IS_ERR(handle
)) {
2497 ret
= PTR_ERR(handle
);
2502 ret
= ocfs2_free_suballoc_bits(handle
, xb_alloc_inode
, xb_alloc_bh
,
2507 ocfs2_commit_trans(osb
, handle
);
2509 ocfs2_inode_unlock(xb_alloc_inode
, 1);
2510 brelse(xb_alloc_bh
);
2512 mutex_unlock(&xb_alloc_inode
->i_mutex
);
2513 iput(xb_alloc_inode
);
2520 * ocfs2_xattr_remove()
2522 * Free extended attribute resources associated with this inode.
2524 int ocfs2_xattr_remove(struct inode
*inode
, struct buffer_head
*di_bh
)
2526 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2527 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2528 struct ocfs2_refcount_tree
*ref_tree
= NULL
;
2529 struct buffer_head
*ref_root_bh
= NULL
;
2530 struct ocfs2_caching_info
*ref_ci
= NULL
;
2534 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
2537 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
2540 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
) {
2541 ret
= ocfs2_lock_refcount_tree(OCFS2_SB(inode
->i_sb
),
2542 le64_to_cpu(di
->i_refcount_loc
),
2543 1, &ref_tree
, &ref_root_bh
);
2548 ref_ci
= &ref_tree
->rf_ci
;
2552 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
2553 ret
= ocfs2_xattr_ibody_remove(inode
, di_bh
,
2554 ref_ci
, ref_root_bh
);
2561 if (di
->i_xattr_loc
) {
2562 ret
= ocfs2_xattr_free_block(inode
,
2563 le64_to_cpu(di
->i_xattr_loc
),
2564 ref_ci
, ref_root_bh
);
2571 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
2572 OCFS2_INODE_UPDATE_CREDITS
);
2573 if (IS_ERR(handle
)) {
2574 ret
= PTR_ERR(handle
);
2578 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
2579 OCFS2_JOURNAL_ACCESS_WRITE
);
2585 di
->i_xattr_loc
= 0;
2587 spin_lock(&oi
->ip_lock
);
2588 oi
->ip_dyn_features
&= ~(OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
);
2589 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
2590 spin_unlock(&oi
->ip_lock
);
2592 ocfs2_journal_dirty(handle
, di_bh
);
2594 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
2597 ocfs2_unlock_refcount_tree(OCFS2_SB(inode
->i_sb
), ref_tree
, 1);
2598 brelse(ref_root_bh
);
2602 static int ocfs2_xattr_has_space_inline(struct inode
*inode
,
2603 struct ocfs2_dinode
*di
)
2605 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2606 unsigned int xattrsize
= OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
;
2609 if (xattrsize
< OCFS2_MIN_XATTR_INLINE_SIZE
)
2612 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
2613 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
2614 free
= le16_to_cpu(idata
->id_count
) - le64_to_cpu(di
->i_size
);
2615 } else if (ocfs2_inode_is_fast_symlink(inode
)) {
2616 free
= ocfs2_fast_symlink_chars(inode
->i_sb
) -
2617 le64_to_cpu(di
->i_size
);
2619 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
2620 free
= (le16_to_cpu(el
->l_count
) -
2621 le16_to_cpu(el
->l_next_free_rec
)) *
2622 sizeof(struct ocfs2_extent_rec
);
2624 if (free
>= xattrsize
)
2631 * ocfs2_xattr_ibody_find()
2633 * Find extended attribute in inode block and
2634 * fill search info into struct ocfs2_xattr_search.
2636 static int ocfs2_xattr_ibody_find(struct inode
*inode
,
2639 struct ocfs2_xattr_search
*xs
)
2641 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2642 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2646 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
2649 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
2650 down_read(&oi
->ip_alloc_sem
);
2651 has_space
= ocfs2_xattr_has_space_inline(inode
, di
);
2652 up_read(&oi
->ip_alloc_sem
);
2657 xs
->xattr_bh
= xs
->inode_bh
;
2658 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
2659 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)
2660 xs
->header
= (struct ocfs2_xattr_header
*)
2661 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
2663 xs
->header
= (struct ocfs2_xattr_header
*)
2664 (xs
->end
- OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
);
2665 xs
->base
= (void *)xs
->header
;
2666 xs
->here
= xs
->header
->xh_entries
;
2668 /* Find the named attribute. */
2669 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
2670 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
2671 if (ret
&& ret
!= -ENODATA
)
2673 xs
->not_found
= ret
;
2679 static int ocfs2_xattr_ibody_init(struct inode
*inode
,
2680 struct buffer_head
*di_bh
,
2681 struct ocfs2_xattr_set_ctxt
*ctxt
)
2684 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2685 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2686 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2687 unsigned int xattrsize
= osb
->s_xattr_inline_size
;
2689 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
2694 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
), di_bh
,
2695 OCFS2_JOURNAL_ACCESS_WRITE
);
2702 * Adjust extent record count or inline data size
2703 * to reserve space for extended attribute.
2705 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
2706 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
2707 le16_add_cpu(&idata
->id_count
, -xattrsize
);
2708 } else if (!(ocfs2_inode_is_fast_symlink(inode
))) {
2709 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
2710 le16_add_cpu(&el
->l_count
, -(xattrsize
/
2711 sizeof(struct ocfs2_extent_rec
)));
2713 di
->i_xattr_inline_size
= cpu_to_le16(xattrsize
);
2715 spin_lock(&oi
->ip_lock
);
2716 oi
->ip_dyn_features
|= OCFS2_INLINE_XATTR_FL
|OCFS2_HAS_XATTR_FL
;
2717 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
2718 spin_unlock(&oi
->ip_lock
);
2720 ocfs2_journal_dirty(ctxt
->handle
, di_bh
);
2727 * ocfs2_xattr_ibody_set()
2729 * Set, replace or remove an extended attribute into inode block.
2732 static int ocfs2_xattr_ibody_set(struct inode
*inode
,
2733 struct ocfs2_xattr_info
*xi
,
2734 struct ocfs2_xattr_search
*xs
,
2735 struct ocfs2_xattr_set_ctxt
*ctxt
)
2738 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2739 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2740 struct ocfs2_xa_loc loc
;
2742 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
2745 down_write(&oi
->ip_alloc_sem
);
2746 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
2747 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
2753 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
2754 ret
= ocfs2_xattr_ibody_init(inode
, xs
->inode_bh
, ctxt
);
2762 ocfs2_init_dinode_xa_loc(&loc
, inode
, xs
->inode_bh
,
2763 xs
->not_found
? NULL
: xs
->here
);
2764 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
2770 xs
->here
= loc
.xl_entry
;
2773 up_write(&oi
->ip_alloc_sem
);
2779 * ocfs2_xattr_block_find()
2781 * Find extended attribute in external block and
2782 * fill search info into struct ocfs2_xattr_search.
2784 static int ocfs2_xattr_block_find(struct inode
*inode
,
2787 struct ocfs2_xattr_search
*xs
)
2789 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2790 struct buffer_head
*blk_bh
= NULL
;
2791 struct ocfs2_xattr_block
*xb
;
2794 if (!di
->i_xattr_loc
)
2797 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
2804 xs
->xattr_bh
= blk_bh
;
2805 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2807 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2808 xs
->header
= &xb
->xb_attrs
.xb_header
;
2809 xs
->base
= (void *)xs
->header
;
2810 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
2811 xs
->here
= xs
->header
->xh_entries
;
2813 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
2815 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
2819 if (ret
&& ret
!= -ENODATA
) {
2820 xs
->xattr_bh
= NULL
;
2823 xs
->not_found
= ret
;
2831 static int ocfs2_create_xattr_block(struct inode
*inode
,
2832 struct buffer_head
*inode_bh
,
2833 struct ocfs2_xattr_set_ctxt
*ctxt
,
2835 struct buffer_head
**ret_bh
)
2838 u16 suballoc_bit_start
;
2840 u64 suballoc_loc
, first_blkno
;
2841 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)inode_bh
->b_data
;
2842 struct buffer_head
*new_bh
= NULL
;
2843 struct ocfs2_xattr_block
*xblk
;
2845 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
),
2846 inode_bh
, OCFS2_JOURNAL_ACCESS_CREATE
);
2852 ret
= ocfs2_claim_metadata(ctxt
->handle
, ctxt
->meta_ac
, 1,
2853 &suballoc_loc
, &suballoc_bit_start
,
2854 &num_got
, &first_blkno
);
2860 new_bh
= sb_getblk(inode
->i_sb
, first_blkno
);
2861 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode
), new_bh
);
2863 ret
= ocfs2_journal_access_xb(ctxt
->handle
, INODE_CACHE(inode
),
2865 OCFS2_JOURNAL_ACCESS_CREATE
);
2871 /* Initialize ocfs2_xattr_block */
2872 xblk
= (struct ocfs2_xattr_block
*)new_bh
->b_data
;
2873 memset(xblk
, 0, inode
->i_sb
->s_blocksize
);
2874 strcpy((void *)xblk
, OCFS2_XATTR_BLOCK_SIGNATURE
);
2875 xblk
->xb_suballoc_slot
= cpu_to_le16(ctxt
->meta_ac
->ac_alloc_slot
);
2876 xblk
->xb_suballoc_loc
= cpu_to_le64(suballoc_loc
);
2877 xblk
->xb_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
2878 xblk
->xb_fs_generation
=
2879 cpu_to_le32(OCFS2_SB(inode
->i_sb
)->fs_generation
);
2880 xblk
->xb_blkno
= cpu_to_le64(first_blkno
);
2882 struct ocfs2_xattr_tree_root
*xr
= &xblk
->xb_attrs
.xb_root
;
2883 xr
->xt_clusters
= cpu_to_le32(1);
2884 xr
->xt_last_eb_blk
= 0;
2885 xr
->xt_list
.l_tree_depth
= 0;
2886 xr
->xt_list
.l_count
= cpu_to_le16(
2887 ocfs2_xattr_recs_per_xb(inode
->i_sb
));
2888 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
2889 xblk
->xb_flags
= cpu_to_le16(OCFS2_XATTR_INDEXED
);
2891 ocfs2_journal_dirty(ctxt
->handle
, new_bh
);
2893 /* Add it to the inode */
2894 di
->i_xattr_loc
= cpu_to_le64(first_blkno
);
2896 spin_lock(&OCFS2_I(inode
)->ip_lock
);
2897 OCFS2_I(inode
)->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
;
2898 di
->i_dyn_features
= cpu_to_le16(OCFS2_I(inode
)->ip_dyn_features
);
2899 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
2901 ocfs2_journal_dirty(ctxt
->handle
, inode_bh
);
2912 * ocfs2_xattr_block_set()
2914 * Set, replace or remove an extended attribute into external block.
2917 static int ocfs2_xattr_block_set(struct inode
*inode
,
2918 struct ocfs2_xattr_info
*xi
,
2919 struct ocfs2_xattr_search
*xs
,
2920 struct ocfs2_xattr_set_ctxt
*ctxt
)
2922 struct buffer_head
*new_bh
= NULL
;
2923 struct ocfs2_xattr_block
*xblk
= NULL
;
2925 struct ocfs2_xa_loc loc
;
2927 if (!xs
->xattr_bh
) {
2928 ret
= ocfs2_create_xattr_block(inode
, xs
->inode_bh
, ctxt
,
2935 xs
->xattr_bh
= new_bh
;
2936 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
2937 xs
->header
= &xblk
->xb_attrs
.xb_header
;
2938 xs
->base
= (void *)xs
->header
;
2939 xs
->end
= (void *)xblk
+ inode
->i_sb
->s_blocksize
;
2940 xs
->here
= xs
->header
->xh_entries
;
2942 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
2944 if (!(le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2945 ocfs2_init_xattr_block_xa_loc(&loc
, inode
, xs
->xattr_bh
,
2946 xs
->not_found
? NULL
: xs
->here
);
2948 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
2950 xs
->here
= loc
.xl_entry
;
2951 else if ((ret
!= -ENOSPC
) || ctxt
->set_abort
)
2954 ret
= ocfs2_xattr_create_index_block(inode
, xs
, ctxt
);
2960 if (le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)
2961 ret
= ocfs2_xattr_set_entry_index_block(inode
, xi
, xs
, ctxt
);
2967 /* Check whether the new xattr can be inserted into the inode. */
2968 static int ocfs2_xattr_can_be_in_inode(struct inode
*inode
,
2969 struct ocfs2_xattr_info
*xi
,
2970 struct ocfs2_xattr_search
*xs
)
2972 struct ocfs2_xattr_entry
*last
;
2974 size_t min_offs
= xs
->end
- xs
->base
;
2979 last
= xs
->header
->xh_entries
;
2981 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
2982 size_t offs
= le16_to_cpu(last
->xe_name_offset
);
2983 if (offs
< min_offs
)
2988 free
= min_offs
- ((void *)last
- xs
->base
) - OCFS2_XATTR_HEADER_GAP
;
2992 BUG_ON(!xs
->not_found
);
2994 if (free
>= (sizeof(struct ocfs2_xattr_entry
) + namevalue_size_xi(xi
)))
3000 static int ocfs2_calc_xattr_set_need(struct inode
*inode
,
3001 struct ocfs2_dinode
*di
,
3002 struct ocfs2_xattr_info
*xi
,
3003 struct ocfs2_xattr_search
*xis
,
3004 struct ocfs2_xattr_search
*xbs
,
3009 int ret
= 0, old_in_xb
= 0;
3010 int clusters_add
= 0, meta_add
= 0, credits
= 0;
3011 struct buffer_head
*bh
= NULL
;
3012 struct ocfs2_xattr_block
*xb
= NULL
;
3013 struct ocfs2_xattr_entry
*xe
= NULL
;
3014 struct ocfs2_xattr_value_root
*xv
= NULL
;
3016 int name_offset
, name_len
= 0;
3017 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
3022 * Calculate the clusters we need to write.
3023 * No matter whether we replace an old one or add a new one,
3024 * we need this for writing.
3026 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
)
3027 credits
+= new_clusters
*
3028 ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3030 if (xis
->not_found
&& xbs
->not_found
) {
3031 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3033 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
3034 clusters_add
+= new_clusters
;
3035 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3043 if (!xis
->not_found
) {
3045 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3046 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3048 credits
+= OCFS2_INODE_UPDATE_CREDITS
;
3050 int i
, block_off
= 0;
3051 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
3053 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3054 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3055 i
= xbs
->here
- xbs
->header
->xh_entries
;
3058 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
3059 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
3060 bucket_xh(xbs
->bucket
),
3063 base
= bucket_block(xbs
->bucket
, block_off
);
3064 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3067 credits
+= OCFS2_XATTR_BLOCK_UPDATE_CREDITS
;
3072 * delete a xattr doesn't need metadata and cluster allocation.
3073 * so just calculate the credits and return.
3075 * The credits for removing the value tree will be extended
3076 * by ocfs2_remove_extent itself.
3078 if (!xi
->xi_value
) {
3079 if (!ocfs2_xattr_is_local(xe
))
3080 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
);
3085 /* do cluster allocation guess first. */
3086 value_size
= le64_to_cpu(xe
->xe_value_size
);
3090 * In xattr set, we always try to set the xe in inode first,
3091 * so if it can be inserted into inode successfully, the old
3092 * one will be removed from the xattr block, and this xattr
3093 * will be inserted into inode as a new xattr in inode.
3095 if (ocfs2_xattr_can_be_in_inode(inode
, xi
, xis
)) {
3096 clusters_add
+= new_clusters
;
3097 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
) +
3098 OCFS2_INODE_UPDATE_CREDITS
;
3099 if (!ocfs2_xattr_is_local(xe
))
3100 credits
+= ocfs2_calc_extend_credits(
3108 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
3109 /* the new values will be stored outside. */
3110 u32 old_clusters
= 0;
3112 if (!ocfs2_xattr_is_local(xe
)) {
3113 old_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
3115 xv
= (struct ocfs2_xattr_value_root
*)
3116 (base
+ name_offset
+ name_len
);
3117 value_size
= OCFS2_XATTR_ROOT_SIZE
;
3121 if (old_clusters
>= new_clusters
) {
3122 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
);
3125 meta_add
+= ocfs2_extend_meta_needed(&xv
->xr_list
);
3126 clusters_add
+= new_clusters
- old_clusters
;
3127 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3131 if (value_size
>= OCFS2_XATTR_ROOT_SIZE
)
3136 * Now the new value will be stored inside. So if the new
3137 * value is smaller than the size of value root or the old
3138 * value, we don't need any allocation, otherwise we have
3139 * to guess metadata allocation.
3141 if ((ocfs2_xattr_is_local(xe
) &&
3142 (value_size
>= xi
->xi_value_len
)) ||
3143 (!ocfs2_xattr_is_local(xe
) &&
3144 OCFS2_XATTR_ROOT_SIZE
>= xi
->xi_value_len
))
3149 /* calculate metadata allocation. */
3150 if (di
->i_xattr_loc
) {
3151 if (!xbs
->xattr_bh
) {
3152 ret
= ocfs2_read_xattr_block(inode
,
3153 le64_to_cpu(di
->i_xattr_loc
),
3160 xb
= (struct ocfs2_xattr_block
*)bh
->b_data
;
3162 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
3165 * If there is already an xattr tree, good, we can calculate
3166 * like other b-trees. Otherwise we may have the chance of
3167 * create a tree, the credit calculation is borrowed from
3168 * ocfs2_calc_extend_credits with root_el = NULL. And the
3169 * new tree will be cluster based, so no meta is needed.
3171 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
3172 struct ocfs2_extent_list
*el
=
3173 &xb
->xb_attrs
.xb_root
.xt_list
;
3174 meta_add
+= ocfs2_extend_meta_needed(el
);
3175 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3178 credits
+= OCFS2_SUBALLOC_ALLOC
+ 1;
3181 * This cluster will be used either for new bucket or for
3183 * If the cluster size is the same as the bucket size, one
3184 * more is needed since we may need to extend the bucket
3188 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3189 if (OCFS2_XATTR_BUCKET_SIZE
==
3190 OCFS2_SB(inode
->i_sb
)->s_clustersize
) {
3191 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3196 credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
3200 *clusters_need
= clusters_add
;
3202 *meta_need
= meta_add
;
3204 *credits_need
= credits
;
3209 static int ocfs2_init_xattr_set_ctxt(struct inode
*inode
,
3210 struct ocfs2_dinode
*di
,
3211 struct ocfs2_xattr_info
*xi
,
3212 struct ocfs2_xattr_search
*xis
,
3213 struct ocfs2_xattr_search
*xbs
,
3214 struct ocfs2_xattr_set_ctxt
*ctxt
,
3218 int clusters_add
, meta_add
, ret
;
3219 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3221 memset(ctxt
, 0, sizeof(struct ocfs2_xattr_set_ctxt
));
3223 ocfs2_init_dealloc_ctxt(&ctxt
->dealloc
);
3225 ret
= ocfs2_calc_xattr_set_need(inode
, di
, xi
, xis
, xbs
,
3226 &clusters_add
, &meta_add
, credits
);
3232 meta_add
+= extra_meta
;
3233 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
3234 "credits = %d\n", xi
->xi_name
, meta_add
, clusters_add
, *credits
);
3237 ret
= ocfs2_reserve_new_metadata_blocks(osb
, meta_add
,
3246 ret
= ocfs2_reserve_clusters(osb
, clusters_add
, &ctxt
->data_ac
);
3252 if (ctxt
->meta_ac
) {
3253 ocfs2_free_alloc_context(ctxt
->meta_ac
);
3254 ctxt
->meta_ac
= NULL
;
3258 * We cannot have an error and a non null ctxt->data_ac.
3265 static int __ocfs2_xattr_set_handle(struct inode
*inode
,
3266 struct ocfs2_dinode
*di
,
3267 struct ocfs2_xattr_info
*xi
,
3268 struct ocfs2_xattr_search
*xis
,
3269 struct ocfs2_xattr_search
*xbs
,
3270 struct ocfs2_xattr_set_ctxt
*ctxt
)
3272 int ret
= 0, credits
, old_found
;
3274 if (!xi
->xi_value
) {
3275 /* Remove existing extended attribute */
3276 if (!xis
->not_found
)
3277 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
3278 else if (!xbs
->not_found
)
3279 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3281 /* We always try to set extended attribute into inode first*/
3282 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
3283 if (!ret
&& !xbs
->not_found
) {
3285 * If succeed and that extended attribute existing in
3286 * external block, then we will remove it.
3288 xi
->xi_value
= NULL
;
3289 xi
->xi_value_len
= 0;
3291 old_found
= xis
->not_found
;
3292 xis
->not_found
= -ENODATA
;
3293 ret
= ocfs2_calc_xattr_set_need(inode
,
3301 xis
->not_found
= old_found
;
3307 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
);
3312 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3313 } else if ((ret
== -ENOSPC
) && !ctxt
->set_abort
) {
3314 if (di
->i_xattr_loc
&& !xbs
->xattr_bh
) {
3315 ret
= ocfs2_xattr_block_find(inode
,
3321 old_found
= xis
->not_found
;
3322 xis
->not_found
= -ENODATA
;
3323 ret
= ocfs2_calc_xattr_set_need(inode
,
3331 xis
->not_found
= old_found
;
3337 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
);
3344 * If no space in inode, we will set extended attribute
3345 * into external block.
3347 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3350 if (!xis
->not_found
) {
3352 * If succeed and that extended attribute
3353 * existing in inode, we will remove it.
3355 xi
->xi_value
= NULL
;
3356 xi
->xi_value_len
= 0;
3357 xbs
->not_found
= -ENODATA
;
3358 ret
= ocfs2_calc_xattr_set_need(inode
,
3371 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
);
3376 ret
= ocfs2_xattr_ibody_set(inode
, xi
,
3383 /* Update inode ctime. */
3384 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
),
3386 OCFS2_JOURNAL_ACCESS_WRITE
);
3392 inode
->i_ctime
= CURRENT_TIME
;
3393 di
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
3394 di
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
3395 ocfs2_journal_dirty(ctxt
->handle
, xis
->inode_bh
);
3402 * This function only called duing creating inode
3403 * for init security/acl xattrs of the new inode.
3404 * All transanction credits have been reserved in mknod.
3406 int ocfs2_xattr_set_handle(handle_t
*handle
,
3407 struct inode
*inode
,
3408 struct buffer_head
*di_bh
,
3414 struct ocfs2_alloc_context
*meta_ac
,
3415 struct ocfs2_alloc_context
*data_ac
)
3417 struct ocfs2_dinode
*di
;
3420 struct ocfs2_xattr_info xi
= {
3421 .xi_name_index
= name_index
,
3423 .xi_name_len
= strlen(name
),
3425 .xi_value_len
= value_len
,
3428 struct ocfs2_xattr_search xis
= {
3429 .not_found
= -ENODATA
,
3432 struct ocfs2_xattr_search xbs
= {
3433 .not_found
= -ENODATA
,
3436 struct ocfs2_xattr_set_ctxt ctxt
= {
3442 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
3446 * In extreme situation, may need xattr bucket when
3447 * block size is too small. And we have already reserved
3448 * the credits for bucket in mknod.
3450 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
) {
3451 xbs
.bucket
= ocfs2_xattr_bucket_new(inode
);
3453 mlog_errno(-ENOMEM
);
3458 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
3459 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3461 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3463 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
3466 if (xis
.not_found
) {
3467 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
3472 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
3475 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3476 brelse(xbs
.xattr_bh
);
3477 ocfs2_xattr_bucket_free(xbs
.bucket
);
3485 * Set, replace or remove an extended attribute for this inode.
3486 * value is NULL to remove an existing extended attribute, else either
3487 * create or replace an extended attribute.
3489 int ocfs2_xattr_set(struct inode
*inode
,
3496 struct buffer_head
*di_bh
= NULL
;
3497 struct ocfs2_dinode
*di
;
3498 int ret
, credits
, ref_meta
= 0, ref_credits
= 0;
3499 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3500 struct inode
*tl_inode
= osb
->osb_tl_inode
;
3501 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, };
3502 struct ocfs2_refcount_tree
*ref_tree
= NULL
;
3504 struct ocfs2_xattr_info xi
= {
3505 .xi_name_index
= name_index
,
3507 .xi_name_len
= strlen(name
),
3509 .xi_value_len
= value_len
,
3512 struct ocfs2_xattr_search xis
= {
3513 .not_found
= -ENODATA
,
3516 struct ocfs2_xattr_search xbs
= {
3517 .not_found
= -ENODATA
,
3520 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
3524 * Only xbs will be used on indexed trees. xis doesn't need a
3527 xbs
.bucket
= ocfs2_xattr_bucket_new(inode
);
3529 mlog_errno(-ENOMEM
);
3533 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
3536 goto cleanup_nolock
;
3538 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
3539 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3541 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3543 * Scan inode and external block to find the same name
3544 * extended attribute and collect search infomation.
3546 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
3549 if (xis
.not_found
) {
3550 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
3555 if (xis
.not_found
&& xbs
.not_found
) {
3557 if (flags
& XATTR_REPLACE
)
3564 if (flags
& XATTR_CREATE
)
3568 /* Check whether the value is refcounted and do some prepartion. */
3569 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
&&
3570 (!xis
.not_found
|| !xbs
.not_found
)) {
3571 ret
= ocfs2_prepare_refcount_xattr(inode
, di
, &xi
,
3572 &xis
, &xbs
, &ref_tree
,
3573 &ref_meta
, &ref_credits
);
3580 mutex_lock(&tl_inode
->i_mutex
);
3582 if (ocfs2_truncate_log_needs_flush(osb
)) {
3583 ret
= __ocfs2_flush_truncate_log(osb
);
3585 mutex_unlock(&tl_inode
->i_mutex
);
3590 mutex_unlock(&tl_inode
->i_mutex
);
3592 ret
= ocfs2_init_xattr_set_ctxt(inode
, di
, &xi
, &xis
,
3593 &xbs
, &ctxt
, ref_meta
, &credits
);
3599 /* we need to update inode's ctime field, so add credit for it. */
3600 credits
+= OCFS2_INODE_UPDATE_CREDITS
;
3601 ctxt
.handle
= ocfs2_start_trans(osb
, credits
+ ref_credits
);
3602 if (IS_ERR(ctxt
.handle
)) {
3603 ret
= PTR_ERR(ctxt
.handle
);
3608 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
3610 ocfs2_commit_trans(osb
, ctxt
.handle
);
3613 ocfs2_free_alloc_context(ctxt
.data_ac
);
3615 ocfs2_free_alloc_context(ctxt
.meta_ac
);
3616 if (ocfs2_dealloc_has_cluster(&ctxt
.dealloc
))
3617 ocfs2_schedule_truncate_log_flush(osb
, 1);
3618 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
3622 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
3623 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3624 if (!value
&& !ret
) {
3625 ret
= ocfs2_try_remove_refcount_tree(inode
, di_bh
);
3629 ocfs2_inode_unlock(inode
, 1);
3632 brelse(xbs
.xattr_bh
);
3633 ocfs2_xattr_bucket_free(xbs
.bucket
);
3639 * Find the xattr extent rec which may contains name_hash.
3640 * e_cpos will be the first name hash of the xattr rec.
3641 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3643 static int ocfs2_xattr_get_rec(struct inode
*inode
,
3648 struct ocfs2_extent_list
*el
)
3651 struct buffer_head
*eb_bh
= NULL
;
3652 struct ocfs2_extent_block
*eb
;
3653 struct ocfs2_extent_rec
*rec
= NULL
;
3656 if (el
->l_tree_depth
) {
3657 ret
= ocfs2_find_leaf(INODE_CACHE(inode
), el
, name_hash
,
3664 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
3667 if (el
->l_tree_depth
) {
3668 ocfs2_error(inode
->i_sb
,
3669 "Inode %lu has non zero tree depth in "
3670 "xattr tree block %llu\n", inode
->i_ino
,
3671 (unsigned long long)eb_bh
->b_blocknr
);
3677 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
3678 rec
= &el
->l_recs
[i
];
3680 if (le32_to_cpu(rec
->e_cpos
) <= name_hash
) {
3681 e_blkno
= le64_to_cpu(rec
->e_blkno
);
3687 ocfs2_error(inode
->i_sb
, "Inode %lu has bad extent "
3688 "record (%u, %u, 0) in xattr", inode
->i_ino
,
3689 le32_to_cpu(rec
->e_cpos
),
3690 ocfs2_rec_clusters(el
, rec
));
3695 *p_blkno
= le64_to_cpu(rec
->e_blkno
);
3696 *num_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
3698 *e_cpos
= le32_to_cpu(rec
->e_cpos
);
3704 typedef int (xattr_bucket_func
)(struct inode
*inode
,
3705 struct ocfs2_xattr_bucket
*bucket
,
3708 static int ocfs2_find_xe_in_bucket(struct inode
*inode
,
3709 struct ocfs2_xattr_bucket
*bucket
,
3716 int i
, ret
= 0, cmp
= 1, block_off
, new_offset
;
3717 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
3718 size_t name_len
= strlen(name
);
3719 struct ocfs2_xattr_entry
*xe
= NULL
;
3723 * We don't use binary search in the bucket because there
3724 * may be multiple entries with the same name hash.
3726 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
3727 xe
= &xh
->xh_entries
[i
];
3729 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
))
3731 else if (name_hash
< le32_to_cpu(xe
->xe_name_hash
))
3734 cmp
= name_index
- ocfs2_xattr_get_type(xe
);
3736 cmp
= name_len
- xe
->xe_name_len
;
3740 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
3751 xe_name
= bucket_block(bucket
, block_off
) + new_offset
;
3752 if (!memcmp(name
, xe_name
, name_len
)) {
3764 * Find the specified xattr entry in a series of buckets.
3765 * This series start from p_blkno and last for num_clusters.
3766 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3767 * the num of the valid buckets.
3769 * Return the buffer_head this xattr should reside in. And if the xattr's
3770 * hash is in the gap of 2 buckets, return the lower bucket.
3772 static int ocfs2_xattr_bucket_find(struct inode
*inode
,
3779 struct ocfs2_xattr_search
*xs
)
3782 struct ocfs2_xattr_header
*xh
= NULL
;
3783 struct ocfs2_xattr_entry
*xe
= NULL
;
3785 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3786 int low_bucket
= 0, bucket
, high_bucket
;
3787 struct ocfs2_xattr_bucket
*search
;
3789 u64 blkno
, lower_blkno
= 0;
3791 search
= ocfs2_xattr_bucket_new(inode
);
3798 ret
= ocfs2_read_xattr_bucket(search
, p_blkno
);
3804 xh
= bucket_xh(search
);
3805 high_bucket
= le16_to_cpu(xh
->xh_num_buckets
) - 1;
3806 while (low_bucket
<= high_bucket
) {
3807 ocfs2_xattr_bucket_relse(search
);
3809 bucket
= (low_bucket
+ high_bucket
) / 2;
3810 blkno
= p_blkno
+ bucket
* blk_per_bucket
;
3811 ret
= ocfs2_read_xattr_bucket(search
, blkno
);
3817 xh
= bucket_xh(search
);
3818 xe
= &xh
->xh_entries
[0];
3819 if (name_hash
< le32_to_cpu(xe
->xe_name_hash
)) {
3820 high_bucket
= bucket
- 1;
3825 * Check whether the hash of the last entry in our
3826 * bucket is larger than the search one. for an empty
3827 * bucket, the last one is also the first one.
3830 xe
= &xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1];
3832 last_hash
= le32_to_cpu(xe
->xe_name_hash
);
3834 /* record lower_blkno which may be the insert place. */
3835 lower_blkno
= blkno
;
3837 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
)) {
3838 low_bucket
= bucket
+ 1;
3842 /* the searched xattr should reside in this bucket if exists. */
3843 ret
= ocfs2_find_xe_in_bucket(inode
, search
,
3844 name_index
, name
, name_hash
,
3854 * Record the bucket we have found.
3855 * When the xattr's hash value is in the gap of 2 buckets, we will
3856 * always set it to the previous bucket.
3859 lower_blkno
= p_blkno
;
3861 /* This should be in cache - we just read it during the search */
3862 ret
= ocfs2_read_xattr_bucket(xs
->bucket
, lower_blkno
);
3868 xs
->header
= bucket_xh(xs
->bucket
);
3869 xs
->base
= bucket_block(xs
->bucket
, 0);
3870 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
3873 xs
->here
= &xs
->header
->xh_entries
[index
];
3874 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name
,
3875 (unsigned long long)bucket_blkno(xs
->bucket
), index
);
3880 ocfs2_xattr_bucket_free(search
);
3884 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
3885 struct buffer_head
*root_bh
,
3888 struct ocfs2_xattr_search
*xs
)
3891 struct ocfs2_xattr_block
*xb
=
3892 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
3893 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
3894 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
3896 u32 first_hash
, num_clusters
= 0;
3897 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
3899 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
3902 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3903 name
, name_hash
, name_index
);
3905 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &first_hash
,
3912 BUG_ON(p_blkno
== 0 || num_clusters
== 0 || first_hash
> name_hash
);
3914 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3915 "in the rec is %u\n", num_clusters
, (unsigned long long)p_blkno
,
3918 ret
= ocfs2_xattr_bucket_find(inode
, name_index
, name
, name_hash
,
3919 p_blkno
, first_hash
, num_clusters
, xs
);
3925 static int ocfs2_iterate_xattr_buckets(struct inode
*inode
,
3928 xattr_bucket_func
*func
,
3932 u32 bpc
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
3933 u32 num_buckets
= clusters
* bpc
;
3934 struct ocfs2_xattr_bucket
*bucket
;
3936 bucket
= ocfs2_xattr_bucket_new(inode
);
3938 mlog_errno(-ENOMEM
);
3942 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3943 clusters
, (unsigned long long)blkno
);
3945 for (i
= 0; i
< num_buckets
; i
++, blkno
+= bucket
->bu_blocks
) {
3946 ret
= ocfs2_read_xattr_bucket(bucket
, blkno
);
3953 * The real bucket num in this series of blocks is stored
3954 * in the 1st bucket.
3957 num_buckets
= le16_to_cpu(bucket_xh(bucket
)->xh_num_buckets
);
3959 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3960 (unsigned long long)blkno
,
3961 le32_to_cpu(bucket_xh(bucket
)->xh_entries
[0].xe_name_hash
));
3963 ret
= func(inode
, bucket
, para
);
3964 if (ret
&& ret
!= -ERANGE
)
3966 /* Fall through to bucket_relse() */
3969 ocfs2_xattr_bucket_relse(bucket
);
3974 ocfs2_xattr_bucket_free(bucket
);
3978 struct ocfs2_xattr_tree_list
{
3984 static int ocfs2_xattr_bucket_get_name_value(struct super_block
*sb
,
3985 struct ocfs2_xattr_header
*xh
,
3992 if (index
< 0 || index
>= le16_to_cpu(xh
->xh_count
))
3995 name_offset
= le16_to_cpu(xh
->xh_entries
[index
].xe_name_offset
);
3997 *block_off
= name_offset
>> sb
->s_blocksize_bits
;
3998 *new_offset
= name_offset
% sb
->s_blocksize
;
4003 static int ocfs2_list_xattr_bucket(struct inode
*inode
,
4004 struct ocfs2_xattr_bucket
*bucket
,
4008 struct ocfs2_xattr_tree_list
*xl
= (struct ocfs2_xattr_tree_list
*)para
;
4009 int i
, block_off
, new_offset
;
4010 const char *prefix
, *name
;
4012 for (i
= 0 ; i
< le16_to_cpu(bucket_xh(bucket
)->xh_count
); i
++) {
4013 struct ocfs2_xattr_entry
*entry
= &bucket_xh(bucket
)->xh_entries
[i
];
4014 type
= ocfs2_xattr_get_type(entry
);
4015 prefix
= ocfs2_xattr_prefix(type
);
4018 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
4026 name
= (const char *)bucket_block(bucket
, block_off
) +
4028 ret
= ocfs2_xattr_list_entry(xl
->buffer
,
4032 entry
->xe_name_len
);
4041 static int ocfs2_iterate_xattr_index_block(struct inode
*inode
,
4042 struct buffer_head
*blk_bh
,
4043 xattr_tree_rec_func
*rec_func
,
4046 struct ocfs2_xattr_block
*xb
=
4047 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
4048 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
4050 u32 name_hash
= UINT_MAX
, e_cpos
= 0, num_clusters
= 0;
4053 if (!el
->l_next_free_rec
|| !rec_func
)
4056 while (name_hash
> 0) {
4057 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
4058 &e_cpos
, &num_clusters
, el
);
4064 ret
= rec_func(inode
, blk_bh
, p_blkno
, e_cpos
,
4065 num_clusters
, para
);
4075 name_hash
= e_cpos
- 1;
4082 static int ocfs2_list_xattr_tree_rec(struct inode
*inode
,
4083 struct buffer_head
*root_bh
,
4084 u64 blkno
, u32 cpos
, u32 len
, void *para
)
4086 return ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
4087 ocfs2_list_xattr_bucket
, para
);
4090 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
4091 struct buffer_head
*blk_bh
,
4096 struct ocfs2_xattr_tree_list xl
= {
4098 .buffer_size
= buffer_size
,
4102 ret
= ocfs2_iterate_xattr_index_block(inode
, blk_bh
,
4103 ocfs2_list_xattr_tree_rec
, &xl
);
4114 static int cmp_xe(const void *a
, const void *b
)
4116 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
4117 u32 l_hash
= le32_to_cpu(l
->xe_name_hash
);
4118 u32 r_hash
= le32_to_cpu(r
->xe_name_hash
);
4120 if (l_hash
> r_hash
)
4122 if (l_hash
< r_hash
)
4127 static void swap_xe(void *a
, void *b
, int size
)
4129 struct ocfs2_xattr_entry
*l
= a
, *r
= b
, tmp
;
4132 memcpy(l
, r
, sizeof(struct ocfs2_xattr_entry
));
4133 memcpy(r
, &tmp
, sizeof(struct ocfs2_xattr_entry
));
4137 * When the ocfs2_xattr_block is filled up, new bucket will be created
4138 * and all the xattr entries will be moved to the new bucket.
4139 * The header goes at the start of the bucket, and the names+values are
4140 * filled from the end. This is why *target starts as the last buffer.
4141 * Note: we need to sort the entries since they are not saved in order
4142 * in the ocfs2_xattr_block.
4144 static void ocfs2_cp_xattr_block_to_bucket(struct inode
*inode
,
4145 struct buffer_head
*xb_bh
,
4146 struct ocfs2_xattr_bucket
*bucket
)
4148 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
4149 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4150 u16 offset
, size
, off_change
;
4151 struct ocfs2_xattr_entry
*xe
;
4152 struct ocfs2_xattr_block
*xb
=
4153 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4154 struct ocfs2_xattr_header
*xb_xh
= &xb
->xb_attrs
.xb_header
;
4155 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
4156 u16 count
= le16_to_cpu(xb_xh
->xh_count
);
4157 char *src
= xb_bh
->b_data
;
4158 char *target
= bucket_block(bucket
, blks
- 1);
4160 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4161 (unsigned long long)xb_bh
->b_blocknr
,
4162 (unsigned long long)bucket_blkno(bucket
));
4164 for (i
= 0; i
< blks
; i
++)
4165 memset(bucket_block(bucket
, i
), 0, blocksize
);
4168 * Since the xe_name_offset is based on ocfs2_xattr_header,
4169 * there is a offset change corresponding to the change of
4170 * ocfs2_xattr_header's position.
4172 off_change
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
4173 xe
= &xb_xh
->xh_entries
[count
- 1];
4174 offset
= le16_to_cpu(xe
->xe_name_offset
) + off_change
;
4175 size
= blocksize
- offset
;
4177 /* copy all the names and values. */
4178 memcpy(target
+ offset
, src
+ offset
, size
);
4180 /* Init new header now. */
4181 xh
->xh_count
= xb_xh
->xh_count
;
4182 xh
->xh_num_buckets
= cpu_to_le16(1);
4183 xh
->xh_name_value_len
= cpu_to_le16(size
);
4184 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
- size
);
4186 /* copy all the entries. */
4187 target
= bucket_block(bucket
, 0);
4188 offset
= offsetof(struct ocfs2_xattr_header
, xh_entries
);
4189 size
= count
* sizeof(struct ocfs2_xattr_entry
);
4190 memcpy(target
+ offset
, (char *)xb_xh
+ offset
, size
);
4192 /* Change the xe offset for all the xe because of the move. */
4193 off_change
= OCFS2_XATTR_BUCKET_SIZE
- blocksize
+
4194 offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
4195 for (i
= 0; i
< count
; i
++)
4196 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
, off_change
);
4198 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4199 offset
, size
, off_change
);
4201 sort(target
+ offset
, count
, sizeof(struct ocfs2_xattr_entry
),
4206 * After we move xattr from block to index btree, we have to
4207 * update ocfs2_xattr_search to the new xe and base.
4209 * When the entry is in xattr block, xattr_bh indicates the storage place.
4210 * While if the entry is in index b-tree, "bucket" indicates the
4211 * real place of the xattr.
4213 static void ocfs2_xattr_update_xattr_search(struct inode
*inode
,
4214 struct ocfs2_xattr_search
*xs
,
4215 struct buffer_head
*old_bh
)
4217 char *buf
= old_bh
->b_data
;
4218 struct ocfs2_xattr_block
*old_xb
= (struct ocfs2_xattr_block
*)buf
;
4219 struct ocfs2_xattr_header
*old_xh
= &old_xb
->xb_attrs
.xb_header
;
4222 xs
->header
= bucket_xh(xs
->bucket
);
4223 xs
->base
= bucket_block(xs
->bucket
, 0);
4224 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
4229 i
= xs
->here
- old_xh
->xh_entries
;
4230 xs
->here
= &xs
->header
->xh_entries
[i
];
4233 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
4234 struct ocfs2_xattr_search
*xs
,
4235 struct ocfs2_xattr_set_ctxt
*ctxt
)
4240 handle_t
*handle
= ctxt
->handle
;
4241 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
4242 struct buffer_head
*xb_bh
= xs
->xattr_bh
;
4243 struct ocfs2_xattr_block
*xb
=
4244 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4245 struct ocfs2_xattr_tree_root
*xr
;
4246 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
4248 mlog(0, "create xattr index block for %llu\n",
4249 (unsigned long long)xb_bh
->b_blocknr
);
4251 BUG_ON(xb_flags
& OCFS2_XATTR_INDEXED
);
4252 BUG_ON(!xs
->bucket
);
4256 * We can use this lock for now, and maybe move to a dedicated mutex
4257 * if performance becomes a problem later.
4259 down_write(&oi
->ip_alloc_sem
);
4261 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), xb_bh
,
4262 OCFS2_JOURNAL_ACCESS_WRITE
);
4268 ret
= __ocfs2_claim_clusters(handle
, ctxt
->data_ac
,
4269 1, 1, &bit_off
, &len
);
4276 * The bucket may spread in many blocks, and
4277 * we will only touch the 1st block and the last block
4278 * in the whole bucket(one for entry and one for data).
4280 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
4282 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4283 (unsigned long long)blkno
);
4285 ret
= ocfs2_init_xattr_bucket(xs
->bucket
, blkno
);
4291 ret
= ocfs2_xattr_bucket_journal_access(handle
, xs
->bucket
,
4292 OCFS2_JOURNAL_ACCESS_CREATE
);
4298 ocfs2_cp_xattr_block_to_bucket(inode
, xb_bh
, xs
->bucket
);
4299 ocfs2_xattr_bucket_journal_dirty(handle
, xs
->bucket
);
4301 ocfs2_xattr_update_xattr_search(inode
, xs
, xb_bh
);
4303 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4304 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
4305 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
4307 xr
= &xb
->xb_attrs
.xb_root
;
4308 xr
->xt_clusters
= cpu_to_le32(1);
4309 xr
->xt_last_eb_blk
= 0;
4310 xr
->xt_list
.l_tree_depth
= 0;
4311 xr
->xt_list
.l_count
= cpu_to_le16(ocfs2_xattr_recs_per_xb(inode
->i_sb
));
4312 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
4314 xr
->xt_list
.l_recs
[0].e_cpos
= 0;
4315 xr
->xt_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
4316 xr
->xt_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
4318 xb
->xb_flags
= cpu_to_le16(xb_flags
| OCFS2_XATTR_INDEXED
);
4320 ocfs2_journal_dirty(handle
, xb_bh
);
4323 up_write(&oi
->ip_alloc_sem
);
4328 static int cmp_xe_offset(const void *a
, const void *b
)
4330 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
4331 u32 l_name_offset
= le16_to_cpu(l
->xe_name_offset
);
4332 u32 r_name_offset
= le16_to_cpu(r
->xe_name_offset
);
4334 if (l_name_offset
< r_name_offset
)
4336 if (l_name_offset
> r_name_offset
)
4342 * defrag a xattr bucket if we find that the bucket has some
4343 * holes beteen name/value pairs.
4344 * We will move all the name/value pairs to the end of the bucket
4345 * so that we can spare some space for insertion.
4347 static int ocfs2_defrag_xattr_bucket(struct inode
*inode
,
4349 struct ocfs2_xattr_bucket
*bucket
)
4352 size_t end
, offset
, len
;
4353 struct ocfs2_xattr_header
*xh
;
4354 char *entries
, *buf
, *bucket_buf
= NULL
;
4355 u64 blkno
= bucket_blkno(bucket
);
4357 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4358 struct ocfs2_xattr_entry
*xe
;
4361 * In order to make the operation more efficient and generic,
4362 * we copy all the blocks into a contiguous memory and do the
4363 * defragment there, so if anything is error, we will not touch
4366 bucket_buf
= kmalloc(OCFS2_XATTR_BUCKET_SIZE
, GFP_NOFS
);
4373 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
4374 memcpy(buf
, bucket_block(bucket
, i
), blocksize
);
4376 ret
= ocfs2_xattr_bucket_journal_access(handle
, bucket
,
4377 OCFS2_JOURNAL_ACCESS_WRITE
);
4383 xh
= (struct ocfs2_xattr_header
*)bucket_buf
;
4384 entries
= (char *)xh
->xh_entries
;
4385 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4387 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4388 "xh_free_start = %u, xh_name_value_len = %u.\n",
4389 (unsigned long long)blkno
, le16_to_cpu(xh
->xh_count
),
4390 xh_free_start
, le16_to_cpu(xh
->xh_name_value_len
));
4393 * sort all the entries by their offset.
4394 * the largest will be the first, so that we can
4395 * move them to the end one by one.
4397 sort(entries
, le16_to_cpu(xh
->xh_count
),
4398 sizeof(struct ocfs2_xattr_entry
),
4399 cmp_xe_offset
, swap_xe
);
4401 /* Move all name/values to the end of the bucket. */
4402 xe
= xh
->xh_entries
;
4403 end
= OCFS2_XATTR_BUCKET_SIZE
;
4404 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, xe
++) {
4405 offset
= le16_to_cpu(xe
->xe_name_offset
);
4406 len
= namevalue_size_xe(xe
);
4409 * We must make sure that the name/value pair
4410 * exist in the same block. So adjust end to
4411 * the previous block end if needed.
4413 if (((end
- len
) / blocksize
!=
4414 (end
- 1) / blocksize
))
4415 end
= end
- end
% blocksize
;
4417 if (end
> offset
+ len
) {
4418 memmove(bucket_buf
+ end
- len
,
4419 bucket_buf
+ offset
, len
);
4420 xe
->xe_name_offset
= cpu_to_le16(end
- len
);
4423 mlog_bug_on_msg(end
< offset
+ len
, "Defrag check failed for "
4424 "bucket %llu\n", (unsigned long long)blkno
);
4429 mlog_bug_on_msg(xh_free_start
> end
, "Defrag check failed for "
4430 "bucket %llu\n", (unsigned long long)blkno
);
4432 if (xh_free_start
== end
)
4435 memset(bucket_buf
+ xh_free_start
, 0, end
- xh_free_start
);
4436 xh
->xh_free_start
= cpu_to_le16(end
);
4438 /* sort the entries by their name_hash. */
4439 sort(entries
, le16_to_cpu(xh
->xh_count
),
4440 sizeof(struct ocfs2_xattr_entry
),
4444 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
4445 memcpy(bucket_block(bucket
, i
), buf
, blocksize
);
4446 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
4454 * prev_blkno points to the start of an existing extent. new_blkno
4455 * points to a newly allocated extent. Because we know each of our
4456 * clusters contains more than bucket, we can easily split one cluster
4457 * at a bucket boundary. So we take the last cluster of the existing
4458 * extent and split it down the middle. We move the last half of the
4459 * buckets in the last cluster of the existing extent over to the new
4462 * first_bh is the buffer at prev_blkno so we can update the existing
4463 * extent's bucket count. header_bh is the bucket were we were hoping
4464 * to insert our xattr. If the bucket move places the target in the new
4465 * extent, we'll update first_bh and header_bh after modifying the old
4468 * first_hash will be set as the 1st xe's name_hash in the new extent.
4470 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode
*inode
,
4472 struct ocfs2_xattr_bucket
*first
,
4473 struct ocfs2_xattr_bucket
*target
,
4479 struct super_block
*sb
= inode
->i_sb
;
4480 int blks_per_bucket
= ocfs2_blocks_per_xattr_bucket(sb
);
4481 int num_buckets
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb
));
4482 int to_move
= num_buckets
/ 2;
4484 u64 last_cluster_blkno
= bucket_blkno(first
) +
4485 ((num_clusters
- 1) * ocfs2_clusters_to_blocks(sb
, 1));
4487 BUG_ON(le16_to_cpu(bucket_xh(first
)->xh_num_buckets
) < num_buckets
);
4488 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
== OCFS2_SB(sb
)->s_clustersize
);
4490 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4491 (unsigned long long)last_cluster_blkno
, (unsigned long long)new_blkno
);
4493 ret
= ocfs2_mv_xattr_buckets(inode
, handle
, bucket_blkno(first
),
4494 last_cluster_blkno
, new_blkno
,
4495 to_move
, first_hash
);
4501 /* This is the first bucket that got moved */
4502 src_blkno
= last_cluster_blkno
+ (to_move
* blks_per_bucket
);
4505 * If the target bucket was part of the moved buckets, we need to
4506 * update first and target.
4508 if (bucket_blkno(target
) >= src_blkno
) {
4509 /* Find the block for the new target bucket */
4510 src_blkno
= new_blkno
+
4511 (bucket_blkno(target
) - src_blkno
);
4513 ocfs2_xattr_bucket_relse(first
);
4514 ocfs2_xattr_bucket_relse(target
);
4517 * These shouldn't fail - the buffers are in the
4518 * journal from ocfs2_cp_xattr_bucket().
4520 ret
= ocfs2_read_xattr_bucket(first
, new_blkno
);
4525 ret
= ocfs2_read_xattr_bucket(target
, src_blkno
);
4536 * Find the suitable pos when we divide a bucket into 2.
4537 * We have to make sure the xattrs with the same hash value exist
4538 * in the same bucket.
4540 * If this ocfs2_xattr_header covers more than one hash value, find a
4541 * place where the hash value changes. Try to find the most even split.
4542 * The most common case is that all entries have different hash values,
4543 * and the first check we make will find a place to split.
4545 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header
*xh
)
4547 struct ocfs2_xattr_entry
*entries
= xh
->xh_entries
;
4548 int count
= le16_to_cpu(xh
->xh_count
);
4549 int delta
, middle
= count
/ 2;
4552 * We start at the middle. Each step gets farther away in both
4553 * directions. We therefore hit the change in hash value
4554 * nearest to the middle. Note that this loop does not execute for
4557 for (delta
= 0; delta
< middle
; delta
++) {
4558 /* Let's check delta earlier than middle */
4559 if (cmp_xe(&entries
[middle
- delta
- 1],
4560 &entries
[middle
- delta
]))
4561 return middle
- delta
;
4563 /* For even counts, don't walk off the end */
4564 if ((middle
+ delta
+ 1) == count
)
4567 /* Now try delta past middle */
4568 if (cmp_xe(&entries
[middle
+ delta
],
4569 &entries
[middle
+ delta
+ 1]))
4570 return middle
+ delta
+ 1;
4573 /* Every entry had the same hash */
4578 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4579 * first_hash will record the 1st hash of the new bucket.
4581 * Normally half of the xattrs will be moved. But we have to make
4582 * sure that the xattrs with the same hash value are stored in the
4583 * same bucket. If all the xattrs in this bucket have the same hash
4584 * value, the new bucket will be initialized as an empty one and the
4585 * first_hash will be initialized as (hash_value+1).
4587 static int ocfs2_divide_xattr_bucket(struct inode
*inode
,
4592 int new_bucket_head
)
4595 int count
, start
, len
, name_value_len
= 0, name_offset
= 0;
4596 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
4597 struct ocfs2_xattr_header
*xh
;
4598 struct ocfs2_xattr_entry
*xe
;
4599 int blocksize
= inode
->i_sb
->s_blocksize
;
4601 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4602 (unsigned long long)blk
, (unsigned long long)new_blk
);
4604 s_bucket
= ocfs2_xattr_bucket_new(inode
);
4605 t_bucket
= ocfs2_xattr_bucket_new(inode
);
4606 if (!s_bucket
|| !t_bucket
) {
4612 ret
= ocfs2_read_xattr_bucket(s_bucket
, blk
);
4618 ret
= ocfs2_xattr_bucket_journal_access(handle
, s_bucket
,
4619 OCFS2_JOURNAL_ACCESS_WRITE
);
4626 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4627 * there's no need to read it.
4629 ret
= ocfs2_init_xattr_bucket(t_bucket
, new_blk
);
4636 * Hey, if we're overwriting t_bucket, what difference does
4637 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4638 * same part of ocfs2_cp_xattr_bucket().
4640 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
4642 OCFS2_JOURNAL_ACCESS_CREATE
:
4643 OCFS2_JOURNAL_ACCESS_WRITE
);
4649 xh
= bucket_xh(s_bucket
);
4650 count
= le16_to_cpu(xh
->xh_count
);
4651 start
= ocfs2_xattr_find_divide_pos(xh
);
4653 if (start
== count
) {
4654 xe
= &xh
->xh_entries
[start
-1];
4657 * initialized a new empty bucket here.
4658 * The hash value is set as one larger than
4659 * that of the last entry in the previous bucket.
4661 for (i
= 0; i
< t_bucket
->bu_blocks
; i
++)
4662 memset(bucket_block(t_bucket
, i
), 0, blocksize
);
4664 xh
= bucket_xh(t_bucket
);
4665 xh
->xh_free_start
= cpu_to_le16(blocksize
);
4666 xh
->xh_entries
[0].xe_name_hash
= xe
->xe_name_hash
;
4667 le32_add_cpu(&xh
->xh_entries
[0].xe_name_hash
, 1);
4669 goto set_num_buckets
;
4672 /* copy the whole bucket to the new first. */
4673 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
4675 /* update the new bucket. */
4676 xh
= bucket_xh(t_bucket
);
4679 * Calculate the total name/value len and xh_free_start for
4680 * the old bucket first.
4682 name_offset
= OCFS2_XATTR_BUCKET_SIZE
;
4684 for (i
= 0; i
< start
; i
++) {
4685 xe
= &xh
->xh_entries
[i
];
4686 name_value_len
+= namevalue_size_xe(xe
);
4687 if (le16_to_cpu(xe
->xe_name_offset
) < name_offset
)
4688 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
4692 * Now begin the modification to the new bucket.
4694 * In the new bucket, We just move the xattr entry to the beginning
4695 * and don't touch the name/value. So there will be some holes in the
4696 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4699 xe
= &xh
->xh_entries
[start
];
4700 len
= sizeof(struct ocfs2_xattr_entry
) * (count
- start
);
4701 mlog(0, "mv xattr entry len %d from %d to %d\n", len
,
4702 (int)((char *)xe
- (char *)xh
),
4703 (int)((char *)xh
->xh_entries
- (char *)xh
));
4704 memmove((char *)xh
->xh_entries
, (char *)xe
, len
);
4705 xe
= &xh
->xh_entries
[count
- start
];
4706 len
= sizeof(struct ocfs2_xattr_entry
) * start
;
4707 memset((char *)xe
, 0, len
);
4709 le16_add_cpu(&xh
->xh_count
, -start
);
4710 le16_add_cpu(&xh
->xh_name_value_len
, -name_value_len
);
4712 /* Calculate xh_free_start for the new bucket. */
4713 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
4714 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
4715 xe
= &xh
->xh_entries
[i
];
4716 if (le16_to_cpu(xe
->xe_name_offset
) <
4717 le16_to_cpu(xh
->xh_free_start
))
4718 xh
->xh_free_start
= xe
->xe_name_offset
;
4722 /* set xh->xh_num_buckets for the new xh. */
4723 if (new_bucket_head
)
4724 xh
->xh_num_buckets
= cpu_to_le16(1);
4726 xh
->xh_num_buckets
= 0;
4728 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
4730 /* store the first_hash of the new bucket. */
4732 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
4735 * Now only update the 1st block of the old bucket. If we
4736 * just added a new empty bucket, there is no need to modify
4742 xh
= bucket_xh(s_bucket
);
4743 memset(&xh
->xh_entries
[start
], 0,
4744 sizeof(struct ocfs2_xattr_entry
) * (count
- start
));
4745 xh
->xh_count
= cpu_to_le16(start
);
4746 xh
->xh_free_start
= cpu_to_le16(name_offset
);
4747 xh
->xh_name_value_len
= cpu_to_le16(name_value_len
);
4749 ocfs2_xattr_bucket_journal_dirty(handle
, s_bucket
);
4752 ocfs2_xattr_bucket_free(s_bucket
);
4753 ocfs2_xattr_bucket_free(t_bucket
);
4759 * Copy xattr from one bucket to another bucket.
4761 * The caller must make sure that the journal transaction
4762 * has enough space for journaling.
4764 static int ocfs2_cp_xattr_bucket(struct inode
*inode
,
4771 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
4773 BUG_ON(s_blkno
== t_blkno
);
4775 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4776 (unsigned long long)s_blkno
, (unsigned long long)t_blkno
,
4779 s_bucket
= ocfs2_xattr_bucket_new(inode
);
4780 t_bucket
= ocfs2_xattr_bucket_new(inode
);
4781 if (!s_bucket
|| !t_bucket
) {
4787 ret
= ocfs2_read_xattr_bucket(s_bucket
, s_blkno
);
4792 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4793 * there's no need to read it.
4795 ret
= ocfs2_init_xattr_bucket(t_bucket
, t_blkno
);
4800 * Hey, if we're overwriting t_bucket, what difference does
4801 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4802 * cluster to fill, we came here from
4803 * ocfs2_mv_xattr_buckets(), and it is really new -
4804 * ACCESS_CREATE is required. But we also might have moved data
4805 * out of t_bucket before extending back into it.
4806 * ocfs2_add_new_xattr_bucket() can do this - its call to
4807 * ocfs2_add_new_xattr_cluster() may have created a new extent
4808 * and copied out the end of the old extent. Then it re-extends
4809 * the old extent back to create space for new xattrs. That's
4810 * how we get here, and the bucket isn't really new.
4812 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
4814 OCFS2_JOURNAL_ACCESS_CREATE
:
4815 OCFS2_JOURNAL_ACCESS_WRITE
);
4819 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
4820 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
4823 ocfs2_xattr_bucket_free(t_bucket
);
4824 ocfs2_xattr_bucket_free(s_bucket
);
4830 * src_blk points to the start of an existing extent. last_blk points to
4831 * last cluster in that extent. to_blk points to a newly allocated
4832 * extent. We copy the buckets from the cluster at last_blk to the new
4833 * extent. If start_bucket is non-zero, we skip that many buckets before
4834 * we start copying. The new extent's xh_num_buckets gets set to the
4835 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4836 * by the same amount.
4838 static int ocfs2_mv_xattr_buckets(struct inode
*inode
, handle_t
*handle
,
4839 u64 src_blk
, u64 last_blk
, u64 to_blk
,
4840 unsigned int start_bucket
,
4843 int i
, ret
, credits
;
4844 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4845 int blks_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4846 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
4847 struct ocfs2_xattr_bucket
*old_first
, *new_first
;
4849 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4850 (unsigned long long)last_blk
, (unsigned long long)to_blk
);
4852 BUG_ON(start_bucket
>= num_buckets
);
4854 num_buckets
-= start_bucket
;
4855 last_blk
+= (start_bucket
* blks_per_bucket
);
4858 /* The first bucket of the original extent */
4859 old_first
= ocfs2_xattr_bucket_new(inode
);
4860 /* The first bucket of the new extent */
4861 new_first
= ocfs2_xattr_bucket_new(inode
);
4862 if (!old_first
|| !new_first
) {
4868 ret
= ocfs2_read_xattr_bucket(old_first
, src_blk
);
4875 * We need to update the first bucket of the old extent and all
4876 * the buckets going to the new extent.
4878 credits
= ((num_buckets
+ 1) * blks_per_bucket
);
4879 ret
= ocfs2_extend_trans(handle
, credits
);
4885 ret
= ocfs2_xattr_bucket_journal_access(handle
, old_first
,
4886 OCFS2_JOURNAL_ACCESS_WRITE
);
4892 for (i
= 0; i
< num_buckets
; i
++) {
4893 ret
= ocfs2_cp_xattr_bucket(inode
, handle
,
4894 last_blk
+ (i
* blks_per_bucket
),
4895 to_blk
+ (i
* blks_per_bucket
),
4904 * Get the new bucket ready before we dirty anything
4905 * (This actually shouldn't fail, because we already dirtied
4906 * it once in ocfs2_cp_xattr_bucket()).
4908 ret
= ocfs2_read_xattr_bucket(new_first
, to_blk
);
4913 ret
= ocfs2_xattr_bucket_journal_access(handle
, new_first
,
4914 OCFS2_JOURNAL_ACCESS_WRITE
);
4920 /* Now update the headers */
4921 le16_add_cpu(&bucket_xh(old_first
)->xh_num_buckets
, -num_buckets
);
4922 ocfs2_xattr_bucket_journal_dirty(handle
, old_first
);
4924 bucket_xh(new_first
)->xh_num_buckets
= cpu_to_le16(num_buckets
);
4925 ocfs2_xattr_bucket_journal_dirty(handle
, new_first
);
4928 *first_hash
= le32_to_cpu(bucket_xh(new_first
)->xh_entries
[0].xe_name_hash
);
4931 ocfs2_xattr_bucket_free(new_first
);
4932 ocfs2_xattr_bucket_free(old_first
);
4937 * Move some xattrs in this cluster to the new cluster.
4938 * This function should only be called when bucket size == cluster size.
4939 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4941 static int ocfs2_divide_xattr_cluster(struct inode
*inode
,
4947 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4948 int ret
, credits
= 2 * blk_per_bucket
;
4950 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
< OCFS2_SB(inode
->i_sb
)->s_clustersize
);
4952 ret
= ocfs2_extend_trans(handle
, credits
);
4958 /* Move half of the xattr in start_blk to the next bucket. */
4959 return ocfs2_divide_xattr_bucket(inode
, handle
, prev_blk
,
4960 new_blk
, first_hash
, 1);
4964 * Move some xattrs from the old cluster to the new one since they are not
4965 * contiguous in ocfs2 xattr tree.
4967 * new_blk starts a new separate cluster, and we will move some xattrs from
4968 * prev_blk to it. v_start will be set as the first name hash value in this
4969 * new cluster so that it can be used as e_cpos during tree insertion and
4970 * don't collide with our original b-tree operations. first_bh and header_bh
4971 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4972 * to extend the insert bucket.
4974 * The problem is how much xattr should we move to the new one and when should
4975 * we update first_bh and header_bh?
4976 * 1. If cluster size > bucket size, that means the previous cluster has more
4977 * than 1 bucket, so just move half nums of bucket into the new cluster and
4978 * update the first_bh and header_bh if the insert bucket has been moved
4979 * to the new cluster.
4980 * 2. If cluster_size == bucket_size:
4981 * a) If the previous extent rec has more than one cluster and the insert
4982 * place isn't in the last cluster, copy the entire last cluster to the
4983 * new one. This time, we don't need to upate the first_bh and header_bh
4984 * since they will not be moved into the new cluster.
4985 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4986 * the new one. And we set the extend flag to zero if the insert place is
4987 * moved into the new allocated cluster since no extend is needed.
4989 static int ocfs2_adjust_xattr_cross_cluster(struct inode
*inode
,
4991 struct ocfs2_xattr_bucket
*first
,
4992 struct ocfs2_xattr_bucket
*target
,
5000 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
5001 (unsigned long long)bucket_blkno(first
), prev_clusters
,
5002 (unsigned long long)new_blk
);
5004 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
)) > 1) {
5005 ret
= ocfs2_mv_xattr_bucket_cross_cluster(inode
,
5014 /* The start of the last cluster in the first extent */
5015 u64 last_blk
= bucket_blkno(first
) +
5016 ((prev_clusters
- 1) *
5017 ocfs2_clusters_to_blocks(inode
->i_sb
, 1));
5019 if (prev_clusters
> 1 && bucket_blkno(target
) != last_blk
) {
5020 ret
= ocfs2_mv_xattr_buckets(inode
, handle
,
5021 bucket_blkno(first
),
5022 last_blk
, new_blk
, 0,
5027 ret
= ocfs2_divide_xattr_cluster(inode
, handle
,
5033 if ((bucket_blkno(target
) == last_blk
) && extend
)
5042 * Add a new cluster for xattr storage.
5044 * If the new cluster is contiguous with the previous one, it will be
5045 * appended to the same extent record, and num_clusters will be updated.
5046 * If not, we will insert a new extent for it and move some xattrs in
5047 * the last cluster into the new allocated one.
5048 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5049 * lose the benefits of hashing because we'll have to search large leaves.
5050 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5053 * first_bh is the first block of the previous extent rec and header_bh
5054 * indicates the bucket we will insert the new xattrs. They will be updated
5055 * when the header_bh is moved into the new cluster.
5057 static int ocfs2_add_new_xattr_cluster(struct inode
*inode
,
5058 struct buffer_head
*root_bh
,
5059 struct ocfs2_xattr_bucket
*first
,
5060 struct ocfs2_xattr_bucket
*target
,
5064 struct ocfs2_xattr_set_ctxt
*ctxt
)
5067 u16 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
5068 u32 prev_clusters
= *num_clusters
;
5069 u32 clusters_to_add
= 1, bit_off
, num_bits
, v_start
= 0;
5071 handle_t
*handle
= ctxt
->handle
;
5072 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5073 struct ocfs2_extent_tree et
;
5075 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5076 "previous xattr blkno = %llu\n",
5077 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5078 prev_cpos
, (unsigned long long)bucket_blkno(first
));
5080 ocfs2_init_xattr_tree_extent_tree(&et
, INODE_CACHE(inode
), root_bh
);
5082 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), root_bh
,
5083 OCFS2_JOURNAL_ACCESS_WRITE
);
5089 ret
= __ocfs2_claim_clusters(handle
, ctxt
->data_ac
, 1,
5090 clusters_to_add
, &bit_off
, &num_bits
);
5097 BUG_ON(num_bits
> clusters_to_add
);
5099 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
5100 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5101 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
5103 if (bucket_blkno(first
) + (prev_clusters
* bpc
) == block
&&
5104 (prev_clusters
+ num_bits
) << osb
->s_clustersize_bits
<=
5105 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
) {
5107 * If this cluster is contiguous with the old one and
5108 * adding this new cluster, we don't surpass the limit of
5109 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5110 * initialized and used like other buckets in the previous
5112 * So add it as a contiguous one. The caller will handle
5115 v_start
= prev_cpos
+ prev_clusters
;
5116 *num_clusters
= prev_clusters
+ num_bits
;
5117 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5120 ret
= ocfs2_adjust_xattr_cross_cluster(inode
,
5134 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
5135 num_bits
, (unsigned long long)block
, v_start
);
5136 ret
= ocfs2_insert_extent(handle
, &et
, v_start
, block
,
5137 num_bits
, 0, ctxt
->meta_ac
);
5143 ocfs2_journal_dirty(handle
, root_bh
);
5150 * We are given an extent. 'first' is the bucket at the very front of
5151 * the extent. The extent has space for an additional bucket past
5152 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5153 * of the target bucket. We wish to shift every bucket past the target
5154 * down one, filling in that additional space. When we get back to the
5155 * target, we split the target between itself and the now-empty bucket
5156 * at target+1 (aka, target_blkno + blks_per_bucket).
5158 static int ocfs2_extend_xattr_bucket(struct inode
*inode
,
5160 struct ocfs2_xattr_bucket
*first
,
5165 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5166 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
5168 u16 new_bucket
= le16_to_cpu(bucket_xh(first
)->xh_num_buckets
);
5170 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
5171 "from %llu, len = %u\n", (unsigned long long)target_blk
,
5172 (unsigned long long)bucket_blkno(first
), num_clusters
);
5174 /* The extent must have room for an additional bucket */
5175 BUG_ON(new_bucket
>=
5176 (num_clusters
* ocfs2_xattr_buckets_per_cluster(osb
)));
5178 /* end_blk points to the last existing bucket */
5179 end_blk
= bucket_blkno(first
) + ((new_bucket
- 1) * blk_per_bucket
);
5182 * end_blk is the start of the last existing bucket.
5183 * Thus, (end_blk - target_blk) covers the target bucket and
5184 * every bucket after it up to, but not including, the last
5185 * existing bucket. Then we add the last existing bucket, the
5186 * new bucket, and the first bucket (3 * blk_per_bucket).
5188 credits
= (end_blk
- target_blk
) + (3 * blk_per_bucket
);
5189 ret
= ocfs2_extend_trans(handle
, credits
);
5195 ret
= ocfs2_xattr_bucket_journal_access(handle
, first
,
5196 OCFS2_JOURNAL_ACCESS_WRITE
);
5202 while (end_blk
!= target_blk
) {
5203 ret
= ocfs2_cp_xattr_bucket(inode
, handle
, end_blk
,
5204 end_blk
+ blk_per_bucket
, 0);
5207 end_blk
-= blk_per_bucket
;
5210 /* Move half of the xattr in target_blkno to the next bucket. */
5211 ret
= ocfs2_divide_xattr_bucket(inode
, handle
, target_blk
,
5212 target_blk
+ blk_per_bucket
, NULL
, 0);
5214 le16_add_cpu(&bucket_xh(first
)->xh_num_buckets
, 1);
5215 ocfs2_xattr_bucket_journal_dirty(handle
, first
);
5222 * Add new xattr bucket in an extent record and adjust the buckets
5223 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5224 * bucket we want to insert into.
5226 * In the easy case, we will move all the buckets after target down by
5227 * one. Half of target's xattrs will be moved to the next bucket.
5229 * If current cluster is full, we'll allocate a new one. This may not
5230 * be contiguous. The underlying calls will make sure that there is
5231 * space for the insert, shifting buckets around if necessary.
5232 * 'target' may be moved by those calls.
5234 static int ocfs2_add_new_xattr_bucket(struct inode
*inode
,
5235 struct buffer_head
*xb_bh
,
5236 struct ocfs2_xattr_bucket
*target
,
5237 struct ocfs2_xattr_set_ctxt
*ctxt
)
5239 struct ocfs2_xattr_block
*xb
=
5240 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
5241 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
5242 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
5244 le32_to_cpu(bucket_xh(target
)->xh_entries
[0].xe_name_hash
);
5245 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5246 int ret
, num_buckets
, extend
= 1;
5248 u32 e_cpos
, num_clusters
;
5249 /* The bucket at the front of the extent */
5250 struct ocfs2_xattr_bucket
*first
;
5252 mlog(0, "Add new xattr bucket starting from %llu\n",
5253 (unsigned long long)bucket_blkno(target
));
5255 /* The first bucket of the original extent */
5256 first
= ocfs2_xattr_bucket_new(inode
);
5263 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &e_cpos
,
5270 ret
= ocfs2_read_xattr_bucket(first
, p_blkno
);
5276 num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
) * num_clusters
;
5277 if (num_buckets
== le16_to_cpu(bucket_xh(first
)->xh_num_buckets
)) {
5279 * This can move first+target if the target bucket moves
5280 * to the new extent.
5282 ret
= ocfs2_add_new_xattr_cluster(inode
,
5297 ret
= ocfs2_extend_xattr_bucket(inode
,
5300 bucket_blkno(target
),
5307 ocfs2_xattr_bucket_free(first
);
5312 static inline char *ocfs2_xattr_bucket_get_val(struct inode
*inode
,
5313 struct ocfs2_xattr_bucket
*bucket
,
5316 int block_off
= offs
>> inode
->i_sb
->s_blocksize_bits
;
5318 offs
= offs
% inode
->i_sb
->s_blocksize
;
5319 return bucket_block(bucket
, block_off
) + offs
;
5323 * Truncate the specified xe_off entry in xattr bucket.
5324 * bucket is indicated by header_bh and len is the new length.
5325 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5327 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5329 static int ocfs2_xattr_bucket_value_truncate(struct inode
*inode
,
5330 struct ocfs2_xattr_bucket
*bucket
,
5333 struct ocfs2_xattr_set_ctxt
*ctxt
)
5337 struct ocfs2_xattr_entry
*xe
;
5338 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5339 size_t blocksize
= inode
->i_sb
->s_blocksize
;
5340 struct ocfs2_xattr_value_buf vb
= {
5341 .vb_access
= ocfs2_journal_access
,
5344 xe
= &xh
->xh_entries
[xe_off
];
5346 BUG_ON(!xe
|| ocfs2_xattr_is_local(xe
));
5348 offset
= le16_to_cpu(xe
->xe_name_offset
) +
5349 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5351 value_blk
= offset
/ blocksize
;
5353 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5354 BUG_ON(value_blk
!= (offset
+ OCFS2_XATTR_ROOT_SIZE
- 1) / blocksize
);
5356 vb
.vb_bh
= bucket
->bu_bhs
[value_blk
];
5359 vb
.vb_xv
= (struct ocfs2_xattr_value_root
*)
5360 (vb
.vb_bh
->b_data
+ offset
% blocksize
);
5363 * From here on out we have to dirty the bucket. The generic
5364 * value calls only modify one of the bucket's bhs, but we need
5365 * to send the bucket at once. So if they error, they *could* have
5366 * modified something. We have to assume they did, and dirty
5367 * the whole bucket. This leaves us in a consistent state.
5369 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5370 xe_off
, (unsigned long long)bucket_blkno(bucket
), len
);
5371 ret
= ocfs2_xattr_value_truncate(inode
, &vb
, len
, ctxt
);
5377 ret
= ocfs2_xattr_bucket_journal_access(ctxt
->handle
, bucket
,
5378 OCFS2_JOURNAL_ACCESS_WRITE
);
5384 xe
->xe_value_size
= cpu_to_le64(len
);
5386 ocfs2_xattr_bucket_journal_dirty(ctxt
->handle
, bucket
);
5392 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
5393 struct buffer_head
*root_bh
,
5400 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5401 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5403 struct ocfs2_xattr_block
*xb
=
5404 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
5405 struct ocfs2_alloc_context
*meta_ac
= NULL
;
5406 struct ocfs2_cached_dealloc_ctxt dealloc
;
5407 struct ocfs2_extent_tree et
;
5409 ret
= ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
5410 ocfs2_delete_xattr_in_bucket
, para
);
5416 ocfs2_init_xattr_tree_extent_tree(&et
, INODE_CACHE(inode
), root_bh
);
5418 ocfs2_init_dealloc_ctxt(&dealloc
);
5420 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5421 cpos
, len
, (unsigned long long)blkno
);
5423 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode
), blkno
,
5426 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
5432 mutex_lock(&tl_inode
->i_mutex
);
5434 if (ocfs2_truncate_log_needs_flush(osb
)) {
5435 ret
= __ocfs2_flush_truncate_log(osb
);
5442 handle
= ocfs2_start_trans(osb
, ocfs2_remove_extent_credits(osb
->sb
));
5443 if (IS_ERR(handle
)) {
5449 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), root_bh
,
5450 OCFS2_JOURNAL_ACCESS_WRITE
);
5456 ret
= ocfs2_remove_extent(handle
, &et
, cpos
, len
, meta_ac
,
5463 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, -len
);
5464 ocfs2_journal_dirty(handle
, root_bh
);
5466 ret
= ocfs2_truncate_log_append(osb
, handle
, blkno
, len
);
5471 ocfs2_commit_trans(osb
, handle
);
5473 ocfs2_schedule_truncate_log_flush(osb
, 1);
5475 mutex_unlock(&tl_inode
->i_mutex
);
5478 ocfs2_free_alloc_context(meta_ac
);
5480 ocfs2_run_deallocs(osb
, &dealloc
);
5486 * check whether the xattr bucket is filled up with the same hash value.
5487 * If we want to insert the xattr with the same hash, return -ENOSPC.
5488 * If we want to insert a xattr with different hash value, go ahead
5489 * and ocfs2_divide_xattr_bucket will handle this.
5491 static int ocfs2_check_xattr_bucket_collision(struct inode
*inode
,
5492 struct ocfs2_xattr_bucket
*bucket
,
5495 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5496 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
5498 if (name_hash
!= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
))
5501 if (xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1].xe_name_hash
==
5502 xh
->xh_entries
[0].xe_name_hash
) {
5503 mlog(ML_ERROR
, "Too much hash collision in xattr bucket %llu, "
5505 (unsigned long long)bucket_blkno(bucket
),
5506 le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
));
5514 * Try to set the entry in the current bucket. If we fail, the caller
5515 * will handle getting us another bucket.
5517 static int ocfs2_xattr_set_entry_bucket(struct inode
*inode
,
5518 struct ocfs2_xattr_info
*xi
,
5519 struct ocfs2_xattr_search
*xs
,
5520 struct ocfs2_xattr_set_ctxt
*ctxt
)
5523 struct ocfs2_xa_loc loc
;
5525 mlog_entry("Set xattr %s in xattr bucket\n", xi
->xi_name
);
5527 ocfs2_init_xattr_bucket_xa_loc(&loc
, xs
->bucket
,
5528 xs
->not_found
? NULL
: xs
->here
);
5529 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
5531 xs
->here
= loc
.xl_entry
;
5534 if (ret
!= -ENOSPC
) {
5539 /* Ok, we need space. Let's try defragmenting the bucket. */
5540 ret
= ocfs2_defrag_xattr_bucket(inode
, ctxt
->handle
,
5547 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
5549 xs
->here
= loc
.xl_entry
;
5561 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
5562 struct ocfs2_xattr_info
*xi
,
5563 struct ocfs2_xattr_search
*xs
,
5564 struct ocfs2_xattr_set_ctxt
*ctxt
)
5568 mlog_entry("Set xattr %s in xattr index block\n", xi
->xi_name
);
5570 ret
= ocfs2_xattr_set_entry_bucket(inode
, xi
, xs
, ctxt
);
5573 if (ret
!= -ENOSPC
) {
5578 /* Ack, need more space. Let's try to get another bucket! */
5581 * We do not allow for overlapping ranges between buckets. And
5582 * the maximum number of collisions we will allow for then is
5583 * one bucket's worth, so check it here whether we need to
5584 * add a new bucket for the insert.
5586 ret
= ocfs2_check_xattr_bucket_collision(inode
,
5594 ret
= ocfs2_add_new_xattr_bucket(inode
,
5604 * ocfs2_add_new_xattr_bucket() will have updated
5605 * xs->bucket if it moved, but it will not have updated
5606 * any of the other search fields. Thus, we drop it and
5607 * re-search. Everything should be cached, so it'll be
5610 ocfs2_xattr_bucket_relse(xs
->bucket
);
5611 ret
= ocfs2_xattr_index_block_find(inode
, xs
->xattr_bh
,
5614 if (ret
&& ret
!= -ENODATA
)
5616 xs
->not_found
= ret
;
5618 /* Ok, we have a new bucket, let's try again */
5619 ret
= ocfs2_xattr_set_entry_bucket(inode
, xi
, xs
, ctxt
);
5620 if (ret
&& (ret
!= -ENOSPC
))
5628 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
5629 struct ocfs2_xattr_bucket
*bucket
,
5632 int ret
= 0, ref_credits
;
5633 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5635 struct ocfs2_xattr_entry
*xe
;
5636 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5637 struct ocfs2_xattr_set_ctxt ctxt
= {NULL
, NULL
,};
5638 int credits
= ocfs2_remove_extent_credits(osb
->sb
) +
5639 ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
5640 struct ocfs2_xattr_value_root
*xv
;
5641 struct ocfs2_rm_xattr_bucket_para
*args
=
5642 (struct ocfs2_rm_xattr_bucket_para
*)para
;
5644 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
5646 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
5647 xe
= &xh
->xh_entries
[i
];
5648 if (ocfs2_xattr_is_local(xe
))
5651 ret
= ocfs2_get_xattr_tree_value_root(inode
->i_sb
, bucket
,
5654 ret
= ocfs2_lock_xattr_remove_allocators(inode
, xv
,
5660 ctxt
.handle
= ocfs2_start_trans(osb
, credits
+ ref_credits
);
5661 if (IS_ERR(ctxt
.handle
)) {
5662 ret
= PTR_ERR(ctxt
.handle
);
5667 ret
= ocfs2_xattr_bucket_value_truncate(inode
, bucket
,
5670 ocfs2_commit_trans(osb
, ctxt
.handle
);
5672 ocfs2_free_alloc_context(ctxt
.meta_ac
);
5673 ctxt
.meta_ac
= NULL
;
5682 ocfs2_free_alloc_context(ctxt
.meta_ac
);
5683 ocfs2_schedule_truncate_log_flush(osb
, 1);
5684 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
5689 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5690 * or change the extent record flag), we need to recalculate
5691 * the metaecc for the whole bucket. So it is done here.
5694 * We have to give the extra credits for the caller.
5696 static int ocfs2_xattr_bucket_post_refcount(struct inode
*inode
,
5701 struct ocfs2_xattr_bucket
*bucket
=
5702 (struct ocfs2_xattr_bucket
*)para
;
5704 ret
= ocfs2_xattr_bucket_journal_access(handle
, bucket
,
5705 OCFS2_JOURNAL_ACCESS_WRITE
);
5711 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
5717 * Special action we need if the xattr value is refcounted.
5719 * 1. If the xattr is refcounted, lock the tree.
5720 * 2. CoW the xattr if we are setting the new value and the value
5721 * will be stored outside.
5722 * 3. In other case, decrease_refcount will work for us, so just
5723 * lock the refcount tree, calculate the meta and credits is OK.
5725 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5726 * currently CoW is a completed transaction, while this function
5727 * will also lock the allocators and let us deadlock. So we will
5728 * CoW the whole xattr value.
5730 static int ocfs2_prepare_refcount_xattr(struct inode
*inode
,
5731 struct ocfs2_dinode
*di
,
5732 struct ocfs2_xattr_info
*xi
,
5733 struct ocfs2_xattr_search
*xis
,
5734 struct ocfs2_xattr_search
*xbs
,
5735 struct ocfs2_refcount_tree
**ref_tree
,
5740 struct ocfs2_xattr_block
*xb
;
5741 struct ocfs2_xattr_entry
*xe
;
5743 u32 p_cluster
, num_clusters
;
5744 unsigned int ext_flags
;
5745 int name_offset
, name_len
;
5746 struct ocfs2_xattr_value_buf vb
;
5747 struct ocfs2_xattr_bucket
*bucket
= NULL
;
5748 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5749 struct ocfs2_post_refcount refcount
;
5750 struct ocfs2_post_refcount
*p
= NULL
;
5751 struct buffer_head
*ref_root_bh
= NULL
;
5753 if (!xis
->not_found
) {
5755 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
5756 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5758 vb
.vb_bh
= xis
->inode_bh
;
5759 vb
.vb_access
= ocfs2_journal_access_di
;
5761 int i
, block_off
= 0;
5762 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
5764 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
5765 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5766 i
= xbs
->here
- xbs
->header
->xh_entries
;
5768 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
5769 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
5770 bucket_xh(xbs
->bucket
),
5777 base
= bucket_block(xbs
->bucket
, block_off
);
5778 vb
.vb_bh
= xbs
->bucket
->bu_bhs
[block_off
];
5779 vb
.vb_access
= ocfs2_journal_access
;
5781 if (ocfs2_meta_ecc(osb
)) {
5782 /*create parameters for ocfs2_post_refcount. */
5783 bucket
= xbs
->bucket
;
5784 refcount
.credits
= bucket
->bu_blocks
;
5785 refcount
.para
= bucket
;
5787 ocfs2_xattr_bucket_post_refcount
;
5792 vb
.vb_bh
= xbs
->xattr_bh
;
5793 vb
.vb_access
= ocfs2_journal_access_xb
;
5797 if (ocfs2_xattr_is_local(xe
))
5800 vb
.vb_xv
= (struct ocfs2_xattr_value_root
*)
5801 (base
+ name_offset
+ name_len
);
5803 ret
= ocfs2_xattr_get_clusters(inode
, 0, &p_cluster
,
5804 &num_clusters
, &vb
.vb_xv
->xr_list
,
5812 * We just need to check the 1st extent record, since we always
5813 * CoW the whole xattr. So there shouldn't be a xattr with
5814 * some REFCOUNT extent recs after the 1st one.
5816 if (!(ext_flags
& OCFS2_EXT_REFCOUNTED
))
5819 ret
= ocfs2_lock_refcount_tree(osb
, le64_to_cpu(di
->i_refcount_loc
),
5820 1, ref_tree
, &ref_root_bh
);
5827 * If we are deleting the xattr or the new size will be stored inside,
5828 * cool, leave it there, the xattr truncate process will remove them
5829 * for us(it still needs the refcount tree lock and the meta, credits).
5830 * And the worse case is that every cluster truncate will split the
5831 * refcount tree, and make the original extent become 3. So we will need
5832 * 2 * cluster more extent recs at most.
5834 if (!xi
->xi_value
|| xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
) {
5836 ret
= ocfs2_refcounted_xattr_delete_need(inode
,
5837 &(*ref_tree
)->rf_ci
,
5838 ref_root_bh
, vb
.vb_xv
,
5845 ret
= ocfs2_refcount_cow_xattr(inode
, di
, &vb
,
5846 *ref_tree
, ref_root_bh
, 0,
5847 le32_to_cpu(vb
.vb_xv
->xr_clusters
), p
);
5852 brelse(ref_root_bh
);
5857 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5858 * The physical clusters will be added to refcount tree.
5860 static int ocfs2_xattr_value_attach_refcount(struct inode
*inode
,
5861 struct ocfs2_xattr_value_root
*xv
,
5862 struct ocfs2_extent_tree
*value_et
,
5863 struct ocfs2_caching_info
*ref_ci
,
5864 struct buffer_head
*ref_root_bh
,
5865 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5866 struct ocfs2_post_refcount
*refcount
)
5869 u32 clusters
= le32_to_cpu(xv
->xr_clusters
);
5870 u32 cpos
, p_cluster
, num_clusters
;
5871 struct ocfs2_extent_list
*el
= &xv
->xr_list
;
5872 unsigned int ext_flags
;
5875 while (cpos
< clusters
) {
5876 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
5877 &num_clusters
, el
, &ext_flags
);
5879 cpos
+= num_clusters
;
5880 if ((ext_flags
& OCFS2_EXT_REFCOUNTED
))
5885 ret
= ocfs2_add_refcount_flag(inode
, value_et
,
5886 ref_ci
, ref_root_bh
,
5887 cpos
- num_clusters
,
5888 p_cluster
, num_clusters
,
5900 * Given a normal ocfs2_xattr_header, refcount all the entries which
5901 * have value stored outside.
5902 * Used for xattrs stored in inode and ocfs2_xattr_block.
5904 static int ocfs2_xattr_attach_refcount_normal(struct inode
*inode
,
5905 struct ocfs2_xattr_value_buf
*vb
,
5906 struct ocfs2_xattr_header
*header
,
5907 struct ocfs2_caching_info
*ref_ci
,
5908 struct buffer_head
*ref_root_bh
,
5909 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5912 struct ocfs2_xattr_entry
*xe
;
5913 struct ocfs2_xattr_value_root
*xv
;
5914 struct ocfs2_extent_tree et
;
5917 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
5918 xe
= &header
->xh_entries
[i
];
5920 if (ocfs2_xattr_is_local(xe
))
5923 xv
= (struct ocfs2_xattr_value_root
*)((void *)header
+
5924 le16_to_cpu(xe
->xe_name_offset
) +
5925 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
5928 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
5930 ret
= ocfs2_xattr_value_attach_refcount(inode
, xv
, &et
,
5931 ref_ci
, ref_root_bh
,
5942 static int ocfs2_xattr_inline_attach_refcount(struct inode
*inode
,
5943 struct buffer_head
*fe_bh
,
5944 struct ocfs2_caching_info
*ref_ci
,
5945 struct buffer_head
*ref_root_bh
,
5946 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5948 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
5949 struct ocfs2_xattr_header
*header
= (struct ocfs2_xattr_header
*)
5950 (fe_bh
->b_data
+ inode
->i_sb
->s_blocksize
-
5951 le16_to_cpu(di
->i_xattr_inline_size
));
5952 struct ocfs2_xattr_value_buf vb
= {
5954 .vb_access
= ocfs2_journal_access_di
,
5957 return ocfs2_xattr_attach_refcount_normal(inode
, &vb
, header
,
5958 ref_ci
, ref_root_bh
, dealloc
);
5961 struct ocfs2_xattr_tree_value_refcount_para
{
5962 struct ocfs2_caching_info
*ref_ci
;
5963 struct buffer_head
*ref_root_bh
;
5964 struct ocfs2_cached_dealloc_ctxt
*dealloc
;
5967 static int ocfs2_get_xattr_tree_value_root(struct super_block
*sb
,
5968 struct ocfs2_xattr_bucket
*bucket
,
5970 struct ocfs2_xattr_value_root
**xv
,
5971 struct buffer_head
**bh
)
5973 int ret
, block_off
, name_offset
;
5974 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5975 struct ocfs2_xattr_entry
*xe
= &xh
->xh_entries
[offset
];
5978 ret
= ocfs2_xattr_bucket_get_name_value(sb
,
5988 base
= bucket_block(bucket
, block_off
);
5990 *xv
= (struct ocfs2_xattr_value_root
*)(base
+ name_offset
+
5991 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
5994 *bh
= bucket
->bu_bhs
[block_off
];
6000 * For a given xattr bucket, refcount all the entries which
6001 * have value stored outside.
6003 static int ocfs2_xattr_bucket_value_refcount(struct inode
*inode
,
6004 struct ocfs2_xattr_bucket
*bucket
,
6008 struct ocfs2_extent_tree et
;
6009 struct ocfs2_xattr_tree_value_refcount_para
*ref
=
6010 (struct ocfs2_xattr_tree_value_refcount_para
*)para
;
6011 struct ocfs2_xattr_header
*xh
=
6012 (struct ocfs2_xattr_header
*)bucket
->bu_bhs
[0]->b_data
;
6013 struct ocfs2_xattr_entry
*xe
;
6014 struct ocfs2_xattr_value_buf vb
= {
6015 .vb_access
= ocfs2_journal_access
,
6017 struct ocfs2_post_refcount refcount
= {
6018 .credits
= bucket
->bu_blocks
,
6020 .func
= ocfs2_xattr_bucket_post_refcount
,
6022 struct ocfs2_post_refcount
*p
= NULL
;
6024 /* We only need post_refcount if we support metaecc. */
6025 if (ocfs2_meta_ecc(OCFS2_SB(inode
->i_sb
)))
6028 mlog(0, "refcount bucket %llu, count = %u\n",
6029 (unsigned long long)bucket_blkno(bucket
),
6030 le16_to_cpu(xh
->xh_count
));
6031 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
6032 xe
= &xh
->xh_entries
[i
];
6034 if (ocfs2_xattr_is_local(xe
))
6037 ret
= ocfs2_get_xattr_tree_value_root(inode
->i_sb
, bucket
, i
,
6038 &vb
.vb_xv
, &vb
.vb_bh
);
6044 ocfs2_init_xattr_value_extent_tree(&et
,
6045 INODE_CACHE(inode
), &vb
);
6047 ret
= ocfs2_xattr_value_attach_refcount(inode
, vb
.vb_xv
,
6061 static int ocfs2_refcount_xattr_tree_rec(struct inode
*inode
,
6062 struct buffer_head
*root_bh
,
6063 u64 blkno
, u32 cpos
, u32 len
, void *para
)
6065 return ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
6066 ocfs2_xattr_bucket_value_refcount
,
6070 static int ocfs2_xattr_block_attach_refcount(struct inode
*inode
,
6071 struct buffer_head
*blk_bh
,
6072 struct ocfs2_caching_info
*ref_ci
,
6073 struct buffer_head
*ref_root_bh
,
6074 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
6077 struct ocfs2_xattr_block
*xb
=
6078 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
6080 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
6081 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
6082 struct ocfs2_xattr_value_buf vb
= {
6084 .vb_access
= ocfs2_journal_access_xb
,
6087 ret
= ocfs2_xattr_attach_refcount_normal(inode
, &vb
, header
,
6088 ref_ci
, ref_root_bh
,
6091 struct ocfs2_xattr_tree_value_refcount_para para
= {
6093 .ref_root_bh
= ref_root_bh
,
6097 ret
= ocfs2_iterate_xattr_index_block(inode
, blk_bh
,
6098 ocfs2_refcount_xattr_tree_rec
,
6105 int ocfs2_xattr_attach_refcount_tree(struct inode
*inode
,
6106 struct buffer_head
*fe_bh
,
6107 struct ocfs2_caching_info
*ref_ci
,
6108 struct buffer_head
*ref_root_bh
,
6109 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
6112 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6113 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
6114 struct buffer_head
*blk_bh
= NULL
;
6116 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
6117 ret
= ocfs2_xattr_inline_attach_refcount(inode
, fe_bh
,
6118 ref_ci
, ref_root_bh
,
6126 if (!di
->i_xattr_loc
)
6129 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
6136 ret
= ocfs2_xattr_block_attach_refcount(inode
, blk_bh
, ref_ci
,
6137 ref_root_bh
, dealloc
);
6147 typedef int (should_xattr_reflinked
)(struct ocfs2_xattr_entry
*xe
);
6149 * Store the information we need in xattr reflink.
6150 * old_bh and new_bh are inode bh for the old and new inode.
6152 struct ocfs2_xattr_reflink
{
6153 struct inode
*old_inode
;
6154 struct inode
*new_inode
;
6155 struct buffer_head
*old_bh
;
6156 struct buffer_head
*new_bh
;
6157 struct ocfs2_caching_info
*ref_ci
;
6158 struct buffer_head
*ref_root_bh
;
6159 struct ocfs2_cached_dealloc_ctxt
*dealloc
;
6160 should_xattr_reflinked
*xattr_reflinked
;
6164 * Given a xattr header and xe offset,
6165 * return the proper xv and the corresponding bh.
6166 * xattr in inode, block and xattr tree have different implementaions.
6168 typedef int (get_xattr_value_root
)(struct super_block
*sb
,
6169 struct buffer_head
*bh
,
6170 struct ocfs2_xattr_header
*xh
,
6172 struct ocfs2_xattr_value_root
**xv
,
6173 struct buffer_head
**ret_bh
,
6177 * Calculate all the xattr value root metadata stored in this xattr header and
6178 * credits we need if we create them from the scratch.
6179 * We use get_xattr_value_root so that all types of xattr container can use it.
6181 static int ocfs2_value_metas_in_xattr_header(struct super_block
*sb
,
6182 struct buffer_head
*bh
,
6183 struct ocfs2_xattr_header
*xh
,
6184 int *metas
, int *credits
,
6186 get_xattr_value_root
*func
,
6190 struct ocfs2_xattr_value_root
*xv
;
6191 struct ocfs2_xattr_entry
*xe
;
6193 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
6194 xe
= &xh
->xh_entries
[i
];
6195 if (ocfs2_xattr_is_local(xe
))
6198 ret
= func(sb
, bh
, xh
, i
, &xv
, NULL
, para
);
6204 *metas
+= le16_to_cpu(xv
->xr_list
.l_tree_depth
) *
6205 le16_to_cpu(xv
->xr_list
.l_next_free_rec
);
6207 *credits
+= ocfs2_calc_extend_credits(sb
,
6209 le32_to_cpu(xv
->xr_clusters
));
6212 * If the value is a tree with depth > 1, We don't go deep
6213 * to the extent block, so just calculate a maximum record num.
6215 if (!xv
->xr_list
.l_tree_depth
)
6216 *num_recs
+= le16_to_cpu(xv
->xr_list
.l_next_free_rec
);
6218 *num_recs
+= ocfs2_clusters_for_bytes(sb
,
6225 /* Used by xattr inode and block to return the right xv and buffer_head. */
6226 static int ocfs2_get_xattr_value_root(struct super_block
*sb
,
6227 struct buffer_head
*bh
,
6228 struct ocfs2_xattr_header
*xh
,
6230 struct ocfs2_xattr_value_root
**xv
,
6231 struct buffer_head
**ret_bh
,
6234 struct ocfs2_xattr_entry
*xe
= &xh
->xh_entries
[offset
];
6236 *xv
= (struct ocfs2_xattr_value_root
*)((void *)xh
+
6237 le16_to_cpu(xe
->xe_name_offset
) +
6238 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
6247 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6248 * It is only used for inline xattr and xattr block.
6250 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super
*osb
,
6251 struct ocfs2_xattr_header
*xh
,
6252 struct buffer_head
*ref_root_bh
,
6254 struct ocfs2_alloc_context
**meta_ac
)
6256 int ret
, meta_add
= 0, num_recs
= 0;
6257 struct ocfs2_refcount_block
*rb
=
6258 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
6262 ret
= ocfs2_value_metas_in_xattr_header(osb
->sb
, NULL
, xh
,
6263 &meta_add
, credits
, &num_recs
,
6264 ocfs2_get_xattr_value_root
,
6272 * We need to add/modify num_recs in refcount tree, so just calculate
6273 * an approximate number we need for refcount tree change.
6274 * Sometimes we need to split the tree, and after split, half recs
6275 * will be moved to the new block, and a new block can only provide
6276 * half number of recs. So we multiple new blocks by 2.
6278 num_recs
= num_recs
/ ocfs2_refcount_recs_per_rb(osb
->sb
) * 2;
6279 meta_add
+= num_recs
;
6280 *credits
+= num_recs
+ num_recs
* OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
6281 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)
6282 *credits
+= le16_to_cpu(rb
->rf_list
.l_tree_depth
) *
6283 le16_to_cpu(rb
->rf_list
.l_next_free_rec
) + 1;
6287 ret
= ocfs2_reserve_new_metadata_blocks(osb
, meta_add
, meta_ac
);
6296 * Given a xattr header, reflink all the xattrs in this container.
6297 * It can be used for inode, block and bucket.
6300 * Before we call this function, the caller has memcpy the xattr in
6301 * old_xh to the new_xh.
6303 * If args.xattr_reflinked is set, call it to decide whether the xe should
6304 * be reflinked or not. If not, remove it from the new xattr header.
6306 static int ocfs2_reflink_xattr_header(handle_t
*handle
,
6307 struct ocfs2_xattr_reflink
*args
,
6308 struct buffer_head
*old_bh
,
6309 struct ocfs2_xattr_header
*xh
,
6310 struct buffer_head
*new_bh
,
6311 struct ocfs2_xattr_header
*new_xh
,
6312 struct ocfs2_xattr_value_buf
*vb
,
6313 struct ocfs2_alloc_context
*meta_ac
,
6314 get_xattr_value_root
*func
,
6318 struct super_block
*sb
= args
->old_inode
->i_sb
;
6319 struct buffer_head
*value_bh
;
6320 struct ocfs2_xattr_entry
*xe
, *last
;
6321 struct ocfs2_xattr_value_root
*xv
, *new_xv
;
6322 struct ocfs2_extent_tree data_et
;
6323 u32 clusters
, cpos
, p_cluster
, num_clusters
;
6324 unsigned int ext_flags
= 0;
6326 mlog(0, "reflink xattr in container %llu, count = %u\n",
6327 (unsigned long long)old_bh
->b_blocknr
, le16_to_cpu(xh
->xh_count
));
6329 last
= &new_xh
->xh_entries
[le16_to_cpu(new_xh
->xh_count
)];
6330 for (i
= 0, j
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, j
++) {
6331 xe
= &xh
->xh_entries
[i
];
6333 if (args
->xattr_reflinked
&& !args
->xattr_reflinked(xe
)) {
6334 xe
= &new_xh
->xh_entries
[j
];
6336 le16_add_cpu(&new_xh
->xh_count
, -1);
6337 if (new_xh
->xh_count
) {
6339 (void *)last
- (void *)xe
);
6341 sizeof(struct ocfs2_xattr_entry
));
6345 * We don't want j to increase in the next round since
6346 * it is already moved ahead.
6352 if (ocfs2_xattr_is_local(xe
))
6355 ret
= func(sb
, old_bh
, xh
, i
, &xv
, NULL
, para
);
6361 ret
= func(sb
, new_bh
, new_xh
, j
, &new_xv
, &value_bh
, para
);
6368 * For the xattr which has l_tree_depth = 0, all the extent
6369 * recs have already be copied to the new xh with the
6370 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6371 * increase the refount count int the refcount tree.
6373 * For the xattr which has l_tree_depth > 0, we need
6374 * to initialize it to the empty default value root,
6375 * and then insert the extents one by one.
6377 if (xv
->xr_list
.l_tree_depth
) {
6378 memcpy(new_xv
, &def_xv
, sizeof(def_xv
));
6380 vb
->vb_bh
= value_bh
;
6381 ocfs2_init_xattr_value_extent_tree(&data_et
,
6382 INODE_CACHE(args
->new_inode
), vb
);
6385 clusters
= le32_to_cpu(xv
->xr_clusters
);
6387 while (cpos
< clusters
) {
6388 ret
= ocfs2_xattr_get_clusters(args
->old_inode
,
6401 if (xv
->xr_list
.l_tree_depth
) {
6402 ret
= ocfs2_insert_extent(handle
,
6404 ocfs2_clusters_to_blocks(
6405 args
->old_inode
->i_sb
,
6407 num_clusters
, ext_flags
,
6415 ret
= ocfs2_increase_refcount(handle
, args
->ref_ci
,
6417 p_cluster
, num_clusters
,
6418 meta_ac
, args
->dealloc
);
6424 cpos
+= num_clusters
;
6432 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink
*args
)
6434 int ret
= 0, credits
= 0;
6436 struct ocfs2_super
*osb
= OCFS2_SB(args
->old_inode
->i_sb
);
6437 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)args
->old_bh
->b_data
;
6438 int inline_size
= le16_to_cpu(di
->i_xattr_inline_size
);
6439 int header_off
= osb
->sb
->s_blocksize
- inline_size
;
6440 struct ocfs2_xattr_header
*xh
= (struct ocfs2_xattr_header
*)
6441 (args
->old_bh
->b_data
+ header_off
);
6442 struct ocfs2_xattr_header
*new_xh
= (struct ocfs2_xattr_header
*)
6443 (args
->new_bh
->b_data
+ header_off
);
6444 struct ocfs2_alloc_context
*meta_ac
= NULL
;
6445 struct ocfs2_inode_info
*new_oi
;
6446 struct ocfs2_dinode
*new_di
;
6447 struct ocfs2_xattr_value_buf vb
= {
6448 .vb_bh
= args
->new_bh
,
6449 .vb_access
= ocfs2_journal_access_di
,
6452 ret
= ocfs2_reflink_lock_xattr_allocators(osb
, xh
, args
->ref_root_bh
,
6453 &credits
, &meta_ac
);
6459 handle
= ocfs2_start_trans(osb
, credits
);
6460 if (IS_ERR(handle
)) {
6461 ret
= PTR_ERR(handle
);
6466 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(args
->new_inode
),
6467 args
->new_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
6473 memcpy(args
->new_bh
->b_data
+ header_off
,
6474 args
->old_bh
->b_data
+ header_off
, inline_size
);
6476 new_di
= (struct ocfs2_dinode
*)args
->new_bh
->b_data
;
6477 new_di
->i_xattr_inline_size
= cpu_to_le16(inline_size
);
6479 ret
= ocfs2_reflink_xattr_header(handle
, args
, args
->old_bh
, xh
,
6480 args
->new_bh
, new_xh
, &vb
, meta_ac
,
6481 ocfs2_get_xattr_value_root
, NULL
);
6487 new_oi
= OCFS2_I(args
->new_inode
);
6488 spin_lock(&new_oi
->ip_lock
);
6489 new_oi
->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
| OCFS2_INLINE_XATTR_FL
;
6490 new_di
->i_dyn_features
= cpu_to_le16(new_oi
->ip_dyn_features
);
6491 spin_unlock(&new_oi
->ip_lock
);
6493 ocfs2_journal_dirty(handle
, args
->new_bh
);
6496 ocfs2_commit_trans(osb
, handle
);
6500 ocfs2_free_alloc_context(meta_ac
);
6504 static int ocfs2_create_empty_xattr_block(struct inode
*inode
,
6505 struct buffer_head
*fe_bh
,
6506 struct buffer_head
**ret_bh
,
6510 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6511 struct ocfs2_xattr_set_ctxt ctxt
;
6513 memset(&ctxt
, 0, sizeof(ctxt
));
6514 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, &ctxt
.meta_ac
);
6520 ctxt
.handle
= ocfs2_start_trans(osb
, OCFS2_XATTR_BLOCK_CREATE_CREDITS
);
6521 if (IS_ERR(ctxt
.handle
)) {
6522 ret
= PTR_ERR(ctxt
.handle
);
6527 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6528 (unsigned long long)fe_bh
->b_blocknr
, indexed
);
6529 ret
= ocfs2_create_xattr_block(inode
, fe_bh
, &ctxt
, indexed
,
6534 ocfs2_commit_trans(osb
, ctxt
.handle
);
6536 ocfs2_free_alloc_context(ctxt
.meta_ac
);
6540 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink
*args
,
6541 struct buffer_head
*blk_bh
,
6542 struct buffer_head
*new_blk_bh
)
6544 int ret
= 0, credits
= 0;
6546 struct ocfs2_inode_info
*new_oi
= OCFS2_I(args
->new_inode
);
6547 struct ocfs2_dinode
*new_di
;
6548 struct ocfs2_super
*osb
= OCFS2_SB(args
->new_inode
->i_sb
);
6549 int header_off
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
6550 struct ocfs2_xattr_block
*xb
=
6551 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
6552 struct ocfs2_xattr_header
*xh
= &xb
->xb_attrs
.xb_header
;
6553 struct ocfs2_xattr_block
*new_xb
=
6554 (struct ocfs2_xattr_block
*)new_blk_bh
->b_data
;
6555 struct ocfs2_xattr_header
*new_xh
= &new_xb
->xb_attrs
.xb_header
;
6556 struct ocfs2_alloc_context
*meta_ac
;
6557 struct ocfs2_xattr_value_buf vb
= {
6558 .vb_bh
= new_blk_bh
,
6559 .vb_access
= ocfs2_journal_access_xb
,
6562 ret
= ocfs2_reflink_lock_xattr_allocators(osb
, xh
, args
->ref_root_bh
,
6563 &credits
, &meta_ac
);
6569 /* One more credits in case we need to add xattr flags in new inode. */
6570 handle
= ocfs2_start_trans(osb
, credits
+ 1);
6571 if (IS_ERR(handle
)) {
6572 ret
= PTR_ERR(handle
);
6577 if (!(new_oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
)) {
6578 ret
= ocfs2_journal_access_di(handle
,
6579 INODE_CACHE(args
->new_inode
),
6581 OCFS2_JOURNAL_ACCESS_WRITE
);
6588 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(args
->new_inode
),
6589 new_blk_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
6595 memcpy(new_blk_bh
->b_data
+ header_off
, blk_bh
->b_data
+ header_off
,
6596 osb
->sb
->s_blocksize
- header_off
);
6598 ret
= ocfs2_reflink_xattr_header(handle
, args
, blk_bh
, xh
,
6599 new_blk_bh
, new_xh
, &vb
, meta_ac
,
6600 ocfs2_get_xattr_value_root
, NULL
);
6606 ocfs2_journal_dirty(handle
, new_blk_bh
);
6608 if (!(new_oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
)) {
6609 new_di
= (struct ocfs2_dinode
*)args
->new_bh
->b_data
;
6610 spin_lock(&new_oi
->ip_lock
);
6611 new_oi
->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
;
6612 new_di
->i_dyn_features
= cpu_to_le16(new_oi
->ip_dyn_features
);
6613 spin_unlock(&new_oi
->ip_lock
);
6615 ocfs2_journal_dirty(handle
, args
->new_bh
);
6619 ocfs2_commit_trans(osb
, handle
);
6622 ocfs2_free_alloc_context(meta_ac
);
6626 struct ocfs2_reflink_xattr_tree_args
{
6627 struct ocfs2_xattr_reflink
*reflink
;
6628 struct buffer_head
*old_blk_bh
;
6629 struct buffer_head
*new_blk_bh
;
6630 struct ocfs2_xattr_bucket
*old_bucket
;
6631 struct ocfs2_xattr_bucket
*new_bucket
;
6636 * We have to handle the case that both old bucket and new bucket
6637 * will call this function to get the right ret_bh.
6638 * So The caller must give us the right bh.
6640 static int ocfs2_get_reflink_xattr_value_root(struct super_block
*sb
,
6641 struct buffer_head
*bh
,
6642 struct ocfs2_xattr_header
*xh
,
6644 struct ocfs2_xattr_value_root
**xv
,
6645 struct buffer_head
**ret_bh
,
6648 struct ocfs2_reflink_xattr_tree_args
*args
=
6649 (struct ocfs2_reflink_xattr_tree_args
*)para
;
6650 struct ocfs2_xattr_bucket
*bucket
;
6652 if (bh
== args
->old_bucket
->bu_bhs
[0])
6653 bucket
= args
->old_bucket
;
6655 bucket
= args
->new_bucket
;
6657 return ocfs2_get_xattr_tree_value_root(sb
, bucket
, offset
,
6661 struct ocfs2_value_tree_metas
{
6667 static int ocfs2_value_tree_metas_in_bucket(struct super_block
*sb
,
6668 struct buffer_head
*bh
,
6669 struct ocfs2_xattr_header
*xh
,
6671 struct ocfs2_xattr_value_root
**xv
,
6672 struct buffer_head
**ret_bh
,
6675 struct ocfs2_xattr_bucket
*bucket
=
6676 (struct ocfs2_xattr_bucket
*)para
;
6678 return ocfs2_get_xattr_tree_value_root(sb
, bucket
, offset
,
6682 static int ocfs2_calc_value_tree_metas(struct inode
*inode
,
6683 struct ocfs2_xattr_bucket
*bucket
,
6686 struct ocfs2_value_tree_metas
*metas
=
6687 (struct ocfs2_value_tree_metas
*)para
;
6688 struct ocfs2_xattr_header
*xh
=
6689 (struct ocfs2_xattr_header
*)bucket
->bu_bhs
[0]->b_data
;
6691 /* Add the credits for this bucket first. */
6692 metas
->credits
+= bucket
->bu_blocks
;
6693 return ocfs2_value_metas_in_xattr_header(inode
->i_sb
, bucket
->bu_bhs
[0],
6694 xh
, &metas
->num_metas
,
6695 &metas
->credits
, &metas
->num_recs
,
6696 ocfs2_value_tree_metas_in_bucket
,
6701 * Given a xattr extent rec starting from blkno and having len clusters,
6702 * iterate all the buckets calculate how much metadata we need for reflinking
6703 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6705 static int ocfs2_lock_reflink_xattr_rec_allocators(
6706 struct ocfs2_reflink_xattr_tree_args
*args
,
6707 struct ocfs2_extent_tree
*xt_et
,
6708 u64 blkno
, u32 len
, int *credits
,
6709 struct ocfs2_alloc_context
**meta_ac
,
6710 struct ocfs2_alloc_context
**data_ac
)
6712 int ret
, num_free_extents
;
6713 struct ocfs2_value_tree_metas metas
;
6714 struct ocfs2_super
*osb
= OCFS2_SB(args
->reflink
->old_inode
->i_sb
);
6715 struct ocfs2_refcount_block
*rb
;
6717 memset(&metas
, 0, sizeof(metas
));
6719 ret
= ocfs2_iterate_xattr_buckets(args
->reflink
->old_inode
, blkno
, len
,
6720 ocfs2_calc_value_tree_metas
, &metas
);
6726 *credits
= metas
.credits
;
6729 * Calculate we need for refcount tree change.
6731 * We need to add/modify num_recs in refcount tree, so just calculate
6732 * an approximate number we need for refcount tree change.
6733 * Sometimes we need to split the tree, and after split, half recs
6734 * will be moved to the new block, and a new block can only provide
6735 * half number of recs. So we multiple new blocks by 2.
6736 * In the end, we have to add credits for modifying the already
6737 * existed refcount block.
6739 rb
= (struct ocfs2_refcount_block
*)args
->reflink
->ref_root_bh
->b_data
;
6741 (metas
.num_recs
+ ocfs2_refcount_recs_per_rb(osb
->sb
) - 1) /
6742 ocfs2_refcount_recs_per_rb(osb
->sb
) * 2;
6743 metas
.num_metas
+= metas
.num_recs
;
6744 *credits
+= metas
.num_recs
+
6745 metas
.num_recs
* OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
6746 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)
6747 *credits
+= le16_to_cpu(rb
->rf_list
.l_tree_depth
) *
6748 le16_to_cpu(rb
->rf_list
.l_next_free_rec
) + 1;
6752 /* count in the xattr tree change. */
6753 num_free_extents
= ocfs2_num_free_extents(osb
, xt_et
);
6754 if (num_free_extents
< 0) {
6755 ret
= num_free_extents
;
6760 if (num_free_extents
< len
)
6761 metas
.num_metas
+= ocfs2_extend_meta_needed(xt_et
->et_root_el
);
6763 *credits
+= ocfs2_calc_extend_credits(osb
->sb
,
6764 xt_et
->et_root_el
, len
);
6766 if (metas
.num_metas
) {
6767 ret
= ocfs2_reserve_new_metadata_blocks(osb
, metas
.num_metas
,
6776 ret
= ocfs2_reserve_clusters(osb
, len
, data_ac
);
6783 ocfs2_free_alloc_context(*meta_ac
);
6791 static int ocfs2_reflink_xattr_buckets(handle_t
*handle
,
6792 u64 blkno
, u64 new_blkno
, u32 clusters
,
6793 struct ocfs2_alloc_context
*meta_ac
,
6794 struct ocfs2_alloc_context
*data_ac
,
6795 struct ocfs2_reflink_xattr_tree_args
*args
)
6798 struct super_block
*sb
= args
->reflink
->old_inode
->i_sb
;
6799 u32 bpc
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb
));
6800 u32 num_buckets
= clusters
* bpc
;
6801 int bpb
= args
->old_bucket
->bu_blocks
;
6802 struct ocfs2_xattr_value_buf vb
= {
6803 .vb_access
= ocfs2_journal_access
,
6806 for (i
= 0; i
< num_buckets
; i
++, blkno
+= bpb
, new_blkno
+= bpb
) {
6807 ret
= ocfs2_read_xattr_bucket(args
->old_bucket
, blkno
);
6813 ret
= ocfs2_init_xattr_bucket(args
->new_bucket
, new_blkno
);
6820 * The real bucket num in this series of blocks is stored
6821 * in the 1st bucket.
6824 num_buckets
= le16_to_cpu(
6825 bucket_xh(args
->old_bucket
)->xh_num_buckets
);
6827 ret
= ocfs2_xattr_bucket_journal_access(handle
,
6829 OCFS2_JOURNAL_ACCESS_CREATE
);
6835 for (j
= 0; j
< bpb
; j
++)
6836 memcpy(bucket_block(args
->new_bucket
, j
),
6837 bucket_block(args
->old_bucket
, j
),
6840 ocfs2_xattr_bucket_journal_dirty(handle
, args
->new_bucket
);
6842 ret
= ocfs2_reflink_xattr_header(handle
, args
->reflink
,
6843 args
->old_bucket
->bu_bhs
[0],
6844 bucket_xh(args
->old_bucket
),
6845 args
->new_bucket
->bu_bhs
[0],
6846 bucket_xh(args
->new_bucket
),
6848 ocfs2_get_reflink_xattr_value_root
,
6856 * Re-access and dirty the bucket to calculate metaecc.
6857 * Because we may extend the transaction in reflink_xattr_header
6858 * which will let the already accessed block gone.
6860 ret
= ocfs2_xattr_bucket_journal_access(handle
,
6862 OCFS2_JOURNAL_ACCESS_WRITE
);
6868 ocfs2_xattr_bucket_journal_dirty(handle
, args
->new_bucket
);
6869 ocfs2_xattr_bucket_relse(args
->old_bucket
);
6870 ocfs2_xattr_bucket_relse(args
->new_bucket
);
6873 ocfs2_xattr_bucket_relse(args
->old_bucket
);
6874 ocfs2_xattr_bucket_relse(args
->new_bucket
);
6878 * Create the same xattr extent record in the new inode's xattr tree.
6880 static int ocfs2_reflink_xattr_rec(struct inode
*inode
,
6881 struct buffer_head
*root_bh
,
6887 int ret
, credits
= 0;
6888 u32 p_cluster
, num_clusters
;
6891 struct ocfs2_reflink_xattr_tree_args
*args
=
6892 (struct ocfs2_reflink_xattr_tree_args
*)para
;
6893 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6894 struct ocfs2_alloc_context
*meta_ac
= NULL
;
6895 struct ocfs2_alloc_context
*data_ac
= NULL
;
6896 struct ocfs2_extent_tree et
;
6898 ocfs2_init_xattr_tree_extent_tree(&et
,
6899 INODE_CACHE(args
->reflink
->new_inode
),
6902 ret
= ocfs2_lock_reflink_xattr_rec_allocators(args
, &et
, blkno
,
6904 &meta_ac
, &data_ac
);
6910 handle
= ocfs2_start_trans(osb
, credits
);
6911 if (IS_ERR(handle
)) {
6912 ret
= PTR_ERR(handle
);
6917 ret
= ocfs2_claim_clusters(handle
, data_ac
,
6918 len
, &p_cluster
, &num_clusters
);
6924 new_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, p_cluster
);
6926 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6927 (unsigned long long)blkno
, (unsigned long long)new_blkno
, len
);
6928 ret
= ocfs2_reflink_xattr_buckets(handle
, blkno
, new_blkno
, len
,
6929 meta_ac
, data_ac
, args
);
6935 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6936 (unsigned long long)new_blkno
, len
, cpos
);
6937 ret
= ocfs2_insert_extent(handle
, &et
, cpos
, new_blkno
,
6943 ocfs2_commit_trans(osb
, handle
);
6947 ocfs2_free_alloc_context(meta_ac
);
6949 ocfs2_free_alloc_context(data_ac
);
6954 * Create reflinked xattr buckets.
6955 * We will add bucket one by one, and refcount all the xattrs in the bucket
6956 * if they are stored outside.
6958 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink
*args
,
6959 struct buffer_head
*blk_bh
,
6960 struct buffer_head
*new_blk_bh
)
6963 struct ocfs2_reflink_xattr_tree_args para
;
6965 memset(¶
, 0, sizeof(para
));
6966 para
.reflink
= args
;
6967 para
.old_blk_bh
= blk_bh
;
6968 para
.new_blk_bh
= new_blk_bh
;
6970 para
.old_bucket
= ocfs2_xattr_bucket_new(args
->old_inode
);
6971 if (!para
.old_bucket
) {
6972 mlog_errno(-ENOMEM
);
6976 para
.new_bucket
= ocfs2_xattr_bucket_new(args
->new_inode
);
6977 if (!para
.new_bucket
) {
6983 ret
= ocfs2_iterate_xattr_index_block(args
->old_inode
, blk_bh
,
6984 ocfs2_reflink_xattr_rec
,
6990 ocfs2_xattr_bucket_free(para
.old_bucket
);
6991 ocfs2_xattr_bucket_free(para
.new_bucket
);
6995 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink
*args
,
6996 struct buffer_head
*blk_bh
)
6998 int ret
, indexed
= 0;
6999 struct buffer_head
*new_blk_bh
= NULL
;
7000 struct ocfs2_xattr_block
*xb
=
7001 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
7004 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)
7007 ret
= ocfs2_create_empty_xattr_block(args
->new_inode
, args
->new_bh
,
7008 &new_blk_bh
, indexed
);
7014 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
))
7015 ret
= ocfs2_reflink_xattr_block(args
, blk_bh
, new_blk_bh
);
7017 ret
= ocfs2_reflink_xattr_tree(args
, blk_bh
, new_blk_bh
);
7026 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry
*xe
)
7028 int type
= ocfs2_xattr_get_type(xe
);
7030 return type
!= OCFS2_XATTR_INDEX_SECURITY
&&
7031 type
!= OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
&&
7032 type
!= OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
;
7035 int ocfs2_reflink_xattrs(struct inode
*old_inode
,
7036 struct buffer_head
*old_bh
,
7037 struct inode
*new_inode
,
7038 struct buffer_head
*new_bh
,
7039 bool preserve_security
)
7042 struct ocfs2_xattr_reflink args
;
7043 struct ocfs2_inode_info
*oi
= OCFS2_I(old_inode
);
7044 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)old_bh
->b_data
;
7045 struct buffer_head
*blk_bh
= NULL
;
7046 struct ocfs2_cached_dealloc_ctxt dealloc
;
7047 struct ocfs2_refcount_tree
*ref_tree
;
7048 struct buffer_head
*ref_root_bh
= NULL
;
7050 ret
= ocfs2_lock_refcount_tree(OCFS2_SB(old_inode
->i_sb
),
7051 le64_to_cpu(di
->i_refcount_loc
),
7052 1, &ref_tree
, &ref_root_bh
);
7058 ocfs2_init_dealloc_ctxt(&dealloc
);
7060 args
.old_inode
= old_inode
;
7061 args
.new_inode
= new_inode
;
7062 args
.old_bh
= old_bh
;
7063 args
.new_bh
= new_bh
;
7064 args
.ref_ci
= &ref_tree
->rf_ci
;
7065 args
.ref_root_bh
= ref_root_bh
;
7066 args
.dealloc
= &dealloc
;
7067 if (preserve_security
)
7068 args
.xattr_reflinked
= NULL
;
7070 args
.xattr_reflinked
= ocfs2_reflink_xattr_no_security
;
7072 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
7073 ret
= ocfs2_reflink_xattr_inline(&args
);
7080 if (!di
->i_xattr_loc
)
7083 ret
= ocfs2_read_xattr_block(old_inode
, le64_to_cpu(di
->i_xattr_loc
),
7090 ret
= ocfs2_reflink_xattr_in_block(&args
, blk_bh
);
7097 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode
->i_sb
),
7099 brelse(ref_root_bh
);
7101 if (ocfs2_dealloc_has_cluster(&dealloc
)) {
7102 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode
->i_sb
), 1);
7103 ocfs2_run_deallocs(OCFS2_SB(old_inode
->i_sb
), &dealloc
);
7111 * Initialize security and acl for a already created inode.
7112 * Used for reflink a non-preserve-security file.
7114 * It uses common api like ocfs2_xattr_set, so the caller
7115 * must not hold any lock expect i_mutex.
7117 int ocfs2_init_security_and_acl(struct inode
*dir
,
7118 struct inode
*inode
)
7121 struct buffer_head
*dir_bh
= NULL
;
7122 struct ocfs2_security_xattr_info si
= {
7126 ret
= ocfs2_init_security_get(inode
, dir
, &si
);
7128 ret
= ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_SECURITY
,
7129 si
.name
, si
.value
, si
.value_len
,
7135 } else if (ret
!= -EOPNOTSUPP
) {
7140 ret
= ocfs2_inode_lock(dir
, &dir_bh
, 0);
7146 ret
= ocfs2_init_acl(NULL
, inode
, dir
, NULL
, dir_bh
, NULL
, NULL
);
7150 ocfs2_inode_unlock(dir
, 0);
7156 * 'security' attributes support
7158 static size_t ocfs2_xattr_security_list(struct dentry
*dentry
, char *list
,
7159 size_t list_size
, const char *name
,
7160 size_t name_len
, int type
)
7162 const size_t prefix_len
= XATTR_SECURITY_PREFIX_LEN
;
7163 const size_t total_len
= prefix_len
+ name_len
+ 1;
7165 if (list
&& total_len
<= list_size
) {
7166 memcpy(list
, XATTR_SECURITY_PREFIX
, prefix_len
);
7167 memcpy(list
+ prefix_len
, name
, name_len
);
7168 list
[prefix_len
+ name_len
] = '\0';
7173 static int ocfs2_xattr_security_get(struct dentry
*dentry
, const char *name
,
7174 void *buffer
, size_t size
, int type
)
7176 if (strcmp(name
, "") == 0)
7178 return ocfs2_xattr_get(dentry
->d_inode
, OCFS2_XATTR_INDEX_SECURITY
,
7179 name
, buffer
, size
);
7182 static int ocfs2_xattr_security_set(struct dentry
*dentry
, const char *name
,
7183 const void *value
, size_t size
, int flags
, int type
)
7185 if (strcmp(name
, "") == 0)
7188 return ocfs2_xattr_set(dentry
->d_inode
, OCFS2_XATTR_INDEX_SECURITY
,
7189 name
, value
, size
, flags
);
7192 int ocfs2_init_security_get(struct inode
*inode
,
7194 struct ocfs2_security_xattr_info
*si
)
7196 /* check whether ocfs2 support feature xattr */
7197 if (!ocfs2_supports_xattr(OCFS2_SB(dir
->i_sb
)))
7199 return security_inode_init_security(inode
, dir
, &si
->name
, &si
->value
,
7203 int ocfs2_init_security_set(handle_t
*handle
,
7204 struct inode
*inode
,
7205 struct buffer_head
*di_bh
,
7206 struct ocfs2_security_xattr_info
*si
,
7207 struct ocfs2_alloc_context
*xattr_ac
,
7208 struct ocfs2_alloc_context
*data_ac
)
7210 return ocfs2_xattr_set_handle(handle
, inode
, di_bh
,
7211 OCFS2_XATTR_INDEX_SECURITY
,
7212 si
->name
, si
->value
, si
->value_len
, 0,
7216 const struct xattr_handler ocfs2_xattr_security_handler
= {
7217 .prefix
= XATTR_SECURITY_PREFIX
,
7218 .list
= ocfs2_xattr_security_list
,
7219 .get
= ocfs2_xattr_security_get
,
7220 .set
= ocfs2_xattr_security_set
,
7224 * 'trusted' attributes support
7226 static size_t ocfs2_xattr_trusted_list(struct dentry
*dentry
, char *list
,
7227 size_t list_size
, const char *name
,
7228 size_t name_len
, int type
)
7230 const size_t prefix_len
= XATTR_TRUSTED_PREFIX_LEN
;
7231 const size_t total_len
= prefix_len
+ name_len
+ 1;
7233 if (list
&& total_len
<= list_size
) {
7234 memcpy(list
, XATTR_TRUSTED_PREFIX
, prefix_len
);
7235 memcpy(list
+ prefix_len
, name
, name_len
);
7236 list
[prefix_len
+ name_len
] = '\0';
7241 static int ocfs2_xattr_trusted_get(struct dentry
*dentry
, const char *name
,
7242 void *buffer
, size_t size
, int type
)
7244 if (strcmp(name
, "") == 0)
7246 return ocfs2_xattr_get(dentry
->d_inode
, OCFS2_XATTR_INDEX_TRUSTED
,
7247 name
, buffer
, size
);
7250 static int ocfs2_xattr_trusted_set(struct dentry
*dentry
, const char *name
,
7251 const void *value
, size_t size
, int flags
, int type
)
7253 if (strcmp(name
, "") == 0)
7256 return ocfs2_xattr_set(dentry
->d_inode
, OCFS2_XATTR_INDEX_TRUSTED
,
7257 name
, value
, size
, flags
);
7260 const struct xattr_handler ocfs2_xattr_trusted_handler
= {
7261 .prefix
= XATTR_TRUSTED_PREFIX
,
7262 .list
= ocfs2_xattr_trusted_list
,
7263 .get
= ocfs2_xattr_trusted_get
,
7264 .set
= ocfs2_xattr_trusted_set
,
7268 * 'user' attributes support
7270 static size_t ocfs2_xattr_user_list(struct dentry
*dentry
, char *list
,
7271 size_t list_size
, const char *name
,
7272 size_t name_len
, int type
)
7274 const size_t prefix_len
= XATTR_USER_PREFIX_LEN
;
7275 const size_t total_len
= prefix_len
+ name_len
+ 1;
7276 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
7278 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7281 if (list
&& total_len
<= list_size
) {
7282 memcpy(list
, XATTR_USER_PREFIX
, prefix_len
);
7283 memcpy(list
+ prefix_len
, name
, name_len
);
7284 list
[prefix_len
+ name_len
] = '\0';
7289 static int ocfs2_xattr_user_get(struct dentry
*dentry
, const char *name
,
7290 void *buffer
, size_t size
, int type
)
7292 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
7294 if (strcmp(name
, "") == 0)
7296 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7298 return ocfs2_xattr_get(dentry
->d_inode
, OCFS2_XATTR_INDEX_USER
, name
,
7302 static int ocfs2_xattr_user_set(struct dentry
*dentry
, const char *name
,
7303 const void *value
, size_t size
, int flags
, int type
)
7305 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
7307 if (strcmp(name
, "") == 0)
7309 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7312 return ocfs2_xattr_set(dentry
->d_inode
, OCFS2_XATTR_INDEX_USER
,
7313 name
, value
, size
, flags
);
7316 const struct xattr_handler ocfs2_xattr_user_handler
= {
7317 .prefix
= XATTR_USER_PREFIX
,
7318 .list
= ocfs2_xattr_user_list
,
7319 .get
= ocfs2_xattr_user_get
,
7320 .set
= ocfs2_xattr_user_set
,