1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
9 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/security.h>
40 #define MLOG_MASK_PREFIX ML_XATTR
41 #include <cluster/masklog.h>
45 #include "blockcheck.h"
55 #include "buffer_head_io.h"
58 #include "refcounttree.h"
61 struct ocfs2_xattr_def_value_root
{
62 struct ocfs2_xattr_value_root xv
;
63 struct ocfs2_extent_rec er
;
66 struct ocfs2_xattr_bucket
{
67 /* The inode these xattrs are associated with */
68 struct inode
*bu_inode
;
70 /* The actual buffers that make up the bucket */
71 struct buffer_head
*bu_bhs
[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
];
73 /* How many blocks make up one bucket for this filesystem */
77 struct ocfs2_xattr_set_ctxt
{
79 struct ocfs2_alloc_context
*meta_ac
;
80 struct ocfs2_alloc_context
*data_ac
;
81 struct ocfs2_cached_dealloc_ctxt dealloc
;
84 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
85 #define OCFS2_XATTR_INLINE_SIZE 80
86 #define OCFS2_XATTR_HEADER_GAP 4
87 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
88 - sizeof(struct ocfs2_xattr_header) \
89 - OCFS2_XATTR_HEADER_GAP)
90 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
91 - sizeof(struct ocfs2_xattr_block) \
92 - sizeof(struct ocfs2_xattr_header) \
93 - OCFS2_XATTR_HEADER_GAP)
95 static struct ocfs2_xattr_def_value_root def_xv
= {
96 .xv
.xr_list
.l_count
= cpu_to_le16(1),
99 struct xattr_handler
*ocfs2_xattr_handlers
[] = {
100 &ocfs2_xattr_user_handler
,
101 &ocfs2_xattr_acl_access_handler
,
102 &ocfs2_xattr_acl_default_handler
,
103 &ocfs2_xattr_trusted_handler
,
104 &ocfs2_xattr_security_handler
,
108 static struct xattr_handler
*ocfs2_xattr_handler_map
[OCFS2_XATTR_MAX
] = {
109 [OCFS2_XATTR_INDEX_USER
] = &ocfs2_xattr_user_handler
,
110 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
]
111 = &ocfs2_xattr_acl_access_handler
,
112 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
]
113 = &ocfs2_xattr_acl_default_handler
,
114 [OCFS2_XATTR_INDEX_TRUSTED
] = &ocfs2_xattr_trusted_handler
,
115 [OCFS2_XATTR_INDEX_SECURITY
] = &ocfs2_xattr_security_handler
,
118 struct ocfs2_xattr_info
{
122 const void *xi_value
;
126 struct ocfs2_xattr_search
{
127 struct buffer_head
*inode_bh
;
129 * xattr_bh point to the block buffer head which has extended attribute
130 * when extended attribute in inode, xattr_bh is equal to inode_bh.
132 struct buffer_head
*xattr_bh
;
133 struct ocfs2_xattr_header
*header
;
134 struct ocfs2_xattr_bucket
*bucket
;
137 struct ocfs2_xattr_entry
*here
;
141 /* Operations on struct ocfs2_xa_entry */
143 struct ocfs2_xa_loc_operations
{
147 int (*xlo_journal_access
)(handle_t
*handle
, struct ocfs2_xa_loc
*loc
,
149 void (*xlo_journal_dirty
)(handle_t
*handle
, struct ocfs2_xa_loc
*loc
);
152 * Return a pointer to the appropriate buffer in loc->xl_storage
153 * at the given offset from loc->xl_header.
155 void *(*xlo_offset_pointer
)(struct ocfs2_xa_loc
*loc
, int offset
);
157 /* Can we reuse the existing entry for the new value? */
158 int (*xlo_can_reuse
)(struct ocfs2_xa_loc
*loc
,
159 struct ocfs2_xattr_info
*xi
);
161 /* How much space is needed for the new value? */
162 int (*xlo_check_space
)(struct ocfs2_xa_loc
*loc
,
163 struct ocfs2_xattr_info
*xi
);
166 * Return the offset of the first name+value pair. This is
167 * the start of our downward-filling free space.
169 int (*xlo_get_free_start
)(struct ocfs2_xa_loc
*loc
);
172 * Remove the name+value at this location. Do whatever is
173 * appropriate with the remaining name+value pairs.
175 void (*xlo_wipe_namevalue
)(struct ocfs2_xa_loc
*loc
);
177 /* Fill xl_entry with a new entry */
178 void (*xlo_add_entry
)(struct ocfs2_xa_loc
*loc
, u32 name_hash
);
180 /* Add name+value storage to an entry */
181 void (*xlo_add_namevalue
)(struct ocfs2_xa_loc
*loc
, int size
);
184 * Initialize the value buf's access and bh fields for this entry.
185 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
187 void (*xlo_fill_value_buf
)(struct ocfs2_xa_loc
*loc
,
188 struct ocfs2_xattr_value_buf
*vb
);
192 * Describes an xattr entry location. This is a memory structure
193 * tracking the on-disk structure.
195 struct ocfs2_xa_loc
{
196 /* This xattr belongs to this inode */
197 struct inode
*xl_inode
;
199 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
200 struct ocfs2_xattr_header
*xl_header
;
202 /* Bytes from xl_header to the end of the storage */
206 * The ocfs2_xattr_entry this location describes. If this is
207 * NULL, this location describes the on-disk structure where it
210 struct ocfs2_xattr_entry
*xl_entry
;
213 * Internal housekeeping
216 /* Buffer(s) containing this entry */
219 /* Operations on the storage backing this location */
220 const struct ocfs2_xa_loc_operations
*xl_ops
;
224 * Convenience functions to calculate how much space is needed for a
225 * given name+value pair
227 static int namevalue_size(int name_len
, uint64_t value_len
)
229 if (value_len
> OCFS2_XATTR_INLINE_SIZE
)
230 return OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
232 return OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_SIZE(value_len
);
235 static int namevalue_size_xi(struct ocfs2_xattr_info
*xi
)
237 return namevalue_size(xi
->xi_name_len
, xi
->xi_value_len
);
240 static int namevalue_size_xe(struct ocfs2_xattr_entry
*xe
)
242 u64 value_len
= le64_to_cpu(xe
->xe_value_size
);
244 BUG_ON((value_len
> OCFS2_XATTR_INLINE_SIZE
) &&
245 ocfs2_xattr_is_local(xe
));
246 return namevalue_size(xe
->xe_name_len
, value_len
);
250 static int ocfs2_xattr_bucket_get_name_value(struct super_block
*sb
,
251 struct ocfs2_xattr_header
*xh
,
256 static int ocfs2_xattr_block_find(struct inode
*inode
,
259 struct ocfs2_xattr_search
*xs
);
260 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
261 struct buffer_head
*root_bh
,
264 struct ocfs2_xattr_search
*xs
);
266 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
267 struct buffer_head
*blk_bh
,
271 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
272 struct ocfs2_xattr_search
*xs
,
273 struct ocfs2_xattr_set_ctxt
*ctxt
);
275 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
276 struct ocfs2_xattr_info
*xi
,
277 struct ocfs2_xattr_search
*xs
,
278 struct ocfs2_xattr_set_ctxt
*ctxt
);
280 typedef int (xattr_tree_rec_func
)(struct inode
*inode
,
281 struct buffer_head
*root_bh
,
282 u64 blkno
, u32 cpos
, u32 len
, void *para
);
283 static int ocfs2_iterate_xattr_index_block(struct inode
*inode
,
284 struct buffer_head
*root_bh
,
285 xattr_tree_rec_func
*rec_func
,
287 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
288 struct ocfs2_xattr_bucket
*bucket
,
290 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
291 struct buffer_head
*root_bh
,
297 static int ocfs2_mv_xattr_buckets(struct inode
*inode
, handle_t
*handle
,
298 u64 src_blk
, u64 last_blk
, u64 to_blk
,
299 unsigned int start_bucket
,
301 static int ocfs2_prepare_refcount_xattr(struct inode
*inode
,
302 struct ocfs2_dinode
*di
,
303 struct ocfs2_xattr_info
*xi
,
304 struct ocfs2_xattr_search
*xis
,
305 struct ocfs2_xattr_search
*xbs
,
306 struct ocfs2_refcount_tree
**ref_tree
,
309 static int ocfs2_get_xattr_tree_value_root(struct super_block
*sb
,
310 struct ocfs2_xattr_bucket
*bucket
,
312 struct ocfs2_xattr_value_root
**xv
,
313 struct buffer_head
**bh
);
315 static inline u16
ocfs2_xattr_buckets_per_cluster(struct ocfs2_super
*osb
)
317 return (1 << osb
->s_clustersize_bits
) / OCFS2_XATTR_BUCKET_SIZE
;
320 static inline u16
ocfs2_blocks_per_xattr_bucket(struct super_block
*sb
)
322 return OCFS2_XATTR_BUCKET_SIZE
/ (1 << sb
->s_blocksize_bits
);
325 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
326 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
327 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
329 static struct ocfs2_xattr_bucket
*ocfs2_xattr_bucket_new(struct inode
*inode
)
331 struct ocfs2_xattr_bucket
*bucket
;
332 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
334 BUG_ON(blks
> OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
);
336 bucket
= kzalloc(sizeof(struct ocfs2_xattr_bucket
), GFP_NOFS
);
338 bucket
->bu_inode
= inode
;
339 bucket
->bu_blocks
= blks
;
345 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket
*bucket
)
349 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
350 brelse(bucket
->bu_bhs
[i
]);
351 bucket
->bu_bhs
[i
] = NULL
;
355 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket
*bucket
)
358 ocfs2_xattr_bucket_relse(bucket
);
359 bucket
->bu_inode
= NULL
;
365 * A bucket that has never been written to disk doesn't need to be
366 * read. We just need the buffer_heads. Don't call this for
367 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
370 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
375 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
376 bucket
->bu_bhs
[i
] = sb_getblk(bucket
->bu_inode
->i_sb
,
378 if (!bucket
->bu_bhs
[i
]) {
384 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket
->bu_inode
),
386 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket
->bu_inode
),
391 ocfs2_xattr_bucket_relse(bucket
);
395 /* Read the xattr bucket at xb_blkno */
396 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
401 rc
= ocfs2_read_blocks(INODE_CACHE(bucket
->bu_inode
), xb_blkno
,
402 bucket
->bu_blocks
, bucket
->bu_bhs
, 0,
405 spin_lock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
406 rc
= ocfs2_validate_meta_ecc_bhs(bucket
->bu_inode
->i_sb
,
409 &bucket_xh(bucket
)->xh_check
);
410 spin_unlock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
416 ocfs2_xattr_bucket_relse(bucket
);
420 static int ocfs2_xattr_bucket_journal_access(handle_t
*handle
,
421 struct ocfs2_xattr_bucket
*bucket
,
426 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
427 rc
= ocfs2_journal_access(handle
,
428 INODE_CACHE(bucket
->bu_inode
),
429 bucket
->bu_bhs
[i
], type
);
439 static void ocfs2_xattr_bucket_journal_dirty(handle_t
*handle
,
440 struct ocfs2_xattr_bucket
*bucket
)
444 spin_lock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
445 ocfs2_compute_meta_ecc_bhs(bucket
->bu_inode
->i_sb
,
446 bucket
->bu_bhs
, bucket
->bu_blocks
,
447 &bucket_xh(bucket
)->xh_check
);
448 spin_unlock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
450 for (i
= 0; i
< bucket
->bu_blocks
; i
++)
451 ocfs2_journal_dirty(handle
, bucket
->bu_bhs
[i
]);
454 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket
*dest
,
455 struct ocfs2_xattr_bucket
*src
)
458 int blocksize
= src
->bu_inode
->i_sb
->s_blocksize
;
460 BUG_ON(dest
->bu_blocks
!= src
->bu_blocks
);
461 BUG_ON(dest
->bu_inode
!= src
->bu_inode
);
463 for (i
= 0; i
< src
->bu_blocks
; i
++) {
464 memcpy(bucket_block(dest
, i
), bucket_block(src
, i
),
469 static int ocfs2_validate_xattr_block(struct super_block
*sb
,
470 struct buffer_head
*bh
)
473 struct ocfs2_xattr_block
*xb
=
474 (struct ocfs2_xattr_block
*)bh
->b_data
;
476 mlog(0, "Validating xattr block %llu\n",
477 (unsigned long long)bh
->b_blocknr
);
479 BUG_ON(!buffer_uptodate(bh
));
482 * If the ecc fails, we return the error but otherwise
483 * leave the filesystem running. We know any error is
484 * local to this block.
486 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &xb
->xb_check
);
491 * Errors after here are fatal
494 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
496 "Extended attribute block #%llu has bad "
498 (unsigned long long)bh
->b_blocknr
, 7,
503 if (le64_to_cpu(xb
->xb_blkno
) != bh
->b_blocknr
) {
505 "Extended attribute block #%llu has an "
506 "invalid xb_blkno of %llu",
507 (unsigned long long)bh
->b_blocknr
,
508 (unsigned long long)le64_to_cpu(xb
->xb_blkno
));
512 if (le32_to_cpu(xb
->xb_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
514 "Extended attribute block #%llu has an invalid "
515 "xb_fs_generation of #%u",
516 (unsigned long long)bh
->b_blocknr
,
517 le32_to_cpu(xb
->xb_fs_generation
));
524 static int ocfs2_read_xattr_block(struct inode
*inode
, u64 xb_blkno
,
525 struct buffer_head
**bh
)
528 struct buffer_head
*tmp
= *bh
;
530 rc
= ocfs2_read_block(INODE_CACHE(inode
), xb_blkno
, &tmp
,
531 ocfs2_validate_xattr_block
);
533 /* If ocfs2_read_block() got us a new bh, pass it up. */
540 static inline const char *ocfs2_xattr_prefix(int name_index
)
542 struct xattr_handler
*handler
= NULL
;
544 if (name_index
> 0 && name_index
< OCFS2_XATTR_MAX
)
545 handler
= ocfs2_xattr_handler_map
[name_index
];
547 return handler
? handler
->prefix
: NULL
;
550 static u32
ocfs2_xattr_name_hash(struct inode
*inode
,
554 /* Get hash value of uuid from super block */
555 u32 hash
= OCFS2_SB(inode
->i_sb
)->uuid_hash
;
558 /* hash extended attribute name */
559 for (i
= 0; i
< name_len
; i
++) {
560 hash
= (hash
<< OCFS2_HASH_SHIFT
) ^
561 (hash
>> (8*sizeof(hash
) - OCFS2_HASH_SHIFT
)) ^
568 static int ocfs2_xattr_entry_real_size(int name_len
, size_t value_len
)
570 return namevalue_size(name_len
, value_len
) +
571 sizeof(struct ocfs2_xattr_entry
);
574 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info
*xi
)
576 return namevalue_size_xi(xi
) +
577 sizeof(struct ocfs2_xattr_entry
);
580 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry
*xe
)
582 return namevalue_size_xe(xe
) +
583 sizeof(struct ocfs2_xattr_entry
);
586 int ocfs2_calc_security_init(struct inode
*dir
,
587 struct ocfs2_security_xattr_info
*si
,
590 struct ocfs2_alloc_context
**xattr_ac
)
593 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
594 int s_size
= ocfs2_xattr_entry_real_size(strlen(si
->name
),
598 * The max space of security xattr taken inline is
599 * 256(name) + 80(value) + 16(entry) = 352 bytes,
600 * So reserve one metadata block for it is ok.
602 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
||
603 s_size
> OCFS2_XATTR_FREE_IN_IBODY
) {
604 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, xattr_ac
);
609 *xattr_credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
612 /* reserve clusters for xattr value which will be set in B tree*/
613 if (si
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
614 int new_clusters
= ocfs2_clusters_for_bytes(dir
->i_sb
,
617 *xattr_credits
+= ocfs2_clusters_to_blocks(dir
->i_sb
,
619 *want_clusters
+= new_clusters
;
624 int ocfs2_calc_xattr_init(struct inode
*dir
,
625 struct buffer_head
*dir_bh
,
627 struct ocfs2_security_xattr_info
*si
,
633 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
634 int s_size
= 0, a_size
= 0, acl_len
= 0, new_clusters
;
637 s_size
= ocfs2_xattr_entry_real_size(strlen(si
->name
),
640 if (osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
) {
641 acl_len
= ocfs2_xattr_get_nolock(dir
, dir_bh
,
642 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
,
645 a_size
= ocfs2_xattr_entry_real_size(0, acl_len
);
648 } else if (acl_len
!= 0 && acl_len
!= -ENODATA
) {
654 if (!(s_size
+ a_size
))
658 * The max space of security xattr taken inline is
659 * 256(name) + 80(value) + 16(entry) = 352 bytes,
660 * The max space of acl xattr taken inline is
661 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
662 * when blocksize = 512, may reserve one more cluser for
663 * xattr bucket, otherwise reserve one metadata block
665 * If this is a new directory with inline data,
666 * we choose to reserve the entire inline area for
667 * directory contents and force an external xattr block.
669 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
||
670 (S_ISDIR(mode
) && ocfs2_supports_inline_data(osb
)) ||
671 (s_size
+ a_size
) > OCFS2_XATTR_FREE_IN_IBODY
) {
672 *want_meta
= *want_meta
+ 1;
673 *xattr_credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
676 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
&&
677 (s_size
+ a_size
) > OCFS2_XATTR_FREE_IN_BLOCK(dir
)) {
679 *xattr_credits
+= ocfs2_blocks_per_xattr_bucket(dir
->i_sb
);
683 * reserve credits and clusters for xattrs which has large value
684 * and have to be set outside
686 if (si
->enable
&& si
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
687 new_clusters
= ocfs2_clusters_for_bytes(dir
->i_sb
,
689 *xattr_credits
+= ocfs2_clusters_to_blocks(dir
->i_sb
,
691 *want_clusters
+= new_clusters
;
693 if (osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
&&
694 acl_len
> OCFS2_XATTR_INLINE_SIZE
) {
695 /* for directory, it has DEFAULT and ACCESS two types of acls */
696 new_clusters
= (S_ISDIR(mode
) ? 2 : 1) *
697 ocfs2_clusters_for_bytes(dir
->i_sb
, acl_len
);
698 *xattr_credits
+= ocfs2_clusters_to_blocks(dir
->i_sb
,
700 *want_clusters
+= new_clusters
;
706 static int ocfs2_xattr_extend_allocation(struct inode
*inode
,
708 struct ocfs2_xattr_value_buf
*vb
,
709 struct ocfs2_xattr_set_ctxt
*ctxt
)
711 int status
= 0, credits
;
712 handle_t
*handle
= ctxt
->handle
;
713 enum ocfs2_alloc_restarted why
;
714 u32 prev_clusters
, logical_start
= le32_to_cpu(vb
->vb_xv
->xr_clusters
);
715 struct ocfs2_extent_tree et
;
717 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add
);
719 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
721 while (clusters_to_add
) {
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
,
738 if ((status
< 0) && (status
!= -EAGAIN
)) {
739 if (status
!= -ENOSPC
)
744 status
= ocfs2_journal_dirty(handle
, vb
->vb_bh
);
750 clusters_to_add
-= le32_to_cpu(vb
->vb_xv
->xr_clusters
) -
753 if (why
!= RESTART_NONE
&& clusters_to_add
) {
755 * We can only fail in case the alloc file doesn't give
756 * up enough clusters.
758 BUG_ON(why
== RESTART_META
);
760 mlog(0, "restarting xattr value extension for %u"
761 " clusters,.\n", clusters_to_add
);
762 credits
= ocfs2_calc_extend_credits(inode
->i_sb
,
765 status
= ocfs2_extend_trans(handle
, credits
);
777 static int __ocfs2_remove_xattr_range(struct inode
*inode
,
778 struct ocfs2_xattr_value_buf
*vb
,
779 u32 cpos
, u32 phys_cpos
, u32 len
,
780 unsigned int ext_flags
,
781 struct ocfs2_xattr_set_ctxt
*ctxt
)
784 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
785 handle_t
*handle
= ctxt
->handle
;
786 struct ocfs2_extent_tree et
;
788 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
790 ret
= vb
->vb_access(handle
, INODE_CACHE(inode
), vb
->vb_bh
,
791 OCFS2_JOURNAL_ACCESS_WRITE
);
797 ret
= ocfs2_remove_extent(handle
, &et
, cpos
, len
, ctxt
->meta_ac
,
804 le32_add_cpu(&vb
->vb_xv
->xr_clusters
, -len
);
806 ret
= ocfs2_journal_dirty(handle
, vb
->vb_bh
);
812 if (ext_flags
& OCFS2_EXT_REFCOUNTED
)
813 ret
= ocfs2_decrease_refcount(inode
, handle
,
814 ocfs2_blocks_to_clusters(inode
->i_sb
,
816 len
, ctxt
->meta_ac
, &ctxt
->dealloc
, 1);
818 ret
= ocfs2_cache_cluster_dealloc(&ctxt
->dealloc
,
827 static int ocfs2_xattr_shrink_size(struct inode
*inode
,
830 struct ocfs2_xattr_value_buf
*vb
,
831 struct ocfs2_xattr_set_ctxt
*ctxt
)
834 unsigned int ext_flags
;
835 u32 trunc_len
, cpos
, phys_cpos
, alloc_size
;
838 if (old_clusters
<= new_clusters
)
842 trunc_len
= old_clusters
- new_clusters
;
844 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &phys_cpos
,
846 &vb
->vb_xv
->xr_list
, &ext_flags
);
852 if (alloc_size
> trunc_len
)
853 alloc_size
= trunc_len
;
855 ret
= __ocfs2_remove_xattr_range(inode
, vb
, cpos
,
856 phys_cpos
, alloc_size
,
863 block
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
864 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode
),
867 trunc_len
-= alloc_size
;
874 static int ocfs2_xattr_value_truncate(struct inode
*inode
,
875 struct ocfs2_xattr_value_buf
*vb
,
877 struct ocfs2_xattr_set_ctxt
*ctxt
)
880 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, len
);
881 u32 old_clusters
= le32_to_cpu(vb
->vb_xv
->xr_clusters
);
883 if (new_clusters
== old_clusters
)
886 if (new_clusters
> old_clusters
)
887 ret
= ocfs2_xattr_extend_allocation(inode
,
888 new_clusters
- old_clusters
,
891 ret
= ocfs2_xattr_shrink_size(inode
,
892 old_clusters
, new_clusters
,
898 static int ocfs2_xattr_list_entry(char *buffer
, size_t size
,
899 size_t *result
, const char *prefix
,
900 const char *name
, int name_len
)
902 char *p
= buffer
+ *result
;
903 int prefix_len
= strlen(prefix
);
904 int total_len
= prefix_len
+ name_len
+ 1;
906 *result
+= total_len
;
908 /* we are just looking for how big our buffer needs to be */
915 memcpy(p
, prefix
, prefix_len
);
916 memcpy(p
+ prefix_len
, name
, name_len
);
917 p
[prefix_len
+ name_len
] = '\0';
922 static int ocfs2_xattr_list_entries(struct inode
*inode
,
923 struct ocfs2_xattr_header
*header
,
924 char *buffer
, size_t buffer_size
)
928 const char *prefix
, *name
;
930 for (i
= 0 ; i
< le16_to_cpu(header
->xh_count
); i
++) {
931 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
932 type
= ocfs2_xattr_get_type(entry
);
933 prefix
= ocfs2_xattr_prefix(type
);
936 name
= (const char *)header
+
937 le16_to_cpu(entry
->xe_name_offset
);
939 ret
= ocfs2_xattr_list_entry(buffer
, buffer_size
,
940 &result
, prefix
, name
,
950 int ocfs2_has_inline_xattr_value_outside(struct inode
*inode
,
951 struct ocfs2_dinode
*di
)
953 struct ocfs2_xattr_header
*xh
;
956 xh
= (struct ocfs2_xattr_header
*)
957 ((void *)di
+ inode
->i_sb
->s_blocksize
-
958 le16_to_cpu(di
->i_xattr_inline_size
));
960 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++)
961 if (!ocfs2_xattr_is_local(&xh
->xh_entries
[i
]))
967 static int ocfs2_xattr_ibody_list(struct inode
*inode
,
968 struct ocfs2_dinode
*di
,
972 struct ocfs2_xattr_header
*header
= NULL
;
973 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
976 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
979 header
= (struct ocfs2_xattr_header
*)
980 ((void *)di
+ inode
->i_sb
->s_blocksize
-
981 le16_to_cpu(di
->i_xattr_inline_size
));
983 ret
= ocfs2_xattr_list_entries(inode
, header
, buffer
, buffer_size
);
988 static int ocfs2_xattr_block_list(struct inode
*inode
,
989 struct ocfs2_dinode
*di
,
993 struct buffer_head
*blk_bh
= NULL
;
994 struct ocfs2_xattr_block
*xb
;
997 if (!di
->i_xattr_loc
)
1000 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
1007 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1008 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1009 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
1010 ret
= ocfs2_xattr_list_entries(inode
, header
,
1011 buffer
, buffer_size
);
1013 ret
= ocfs2_xattr_tree_list_index_block(inode
, blk_bh
,
1014 buffer
, buffer_size
);
1021 ssize_t
ocfs2_listxattr(struct dentry
*dentry
,
1025 int ret
= 0, i_ret
= 0, b_ret
= 0;
1026 struct buffer_head
*di_bh
= NULL
;
1027 struct ocfs2_dinode
*di
= NULL
;
1028 struct ocfs2_inode_info
*oi
= OCFS2_I(dentry
->d_inode
);
1030 if (!ocfs2_supports_xattr(OCFS2_SB(dentry
->d_sb
)))
1033 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1036 ret
= ocfs2_inode_lock(dentry
->d_inode
, &di_bh
, 0);
1042 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1044 down_read(&oi
->ip_xattr_sem
);
1045 i_ret
= ocfs2_xattr_ibody_list(dentry
->d_inode
, di
, buffer
, size
);
1053 b_ret
= ocfs2_xattr_block_list(dentry
->d_inode
, di
,
1058 up_read(&oi
->ip_xattr_sem
);
1059 ocfs2_inode_unlock(dentry
->d_inode
, 0);
1063 return i_ret
+ b_ret
;
1066 static int ocfs2_xattr_find_entry(int name_index
,
1068 struct ocfs2_xattr_search
*xs
)
1070 struct ocfs2_xattr_entry
*entry
;
1077 name_len
= strlen(name
);
1079 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1080 cmp
= name_index
- ocfs2_xattr_get_type(entry
);
1082 cmp
= name_len
- entry
->xe_name_len
;
1084 cmp
= memcmp(name
, (xs
->base
+
1085 le16_to_cpu(entry
->xe_name_offset
)),
1093 return cmp
? -ENODATA
: 0;
1096 static int ocfs2_xattr_get_value_outside(struct inode
*inode
,
1097 struct ocfs2_xattr_value_root
*xv
,
1101 u32 cpos
, p_cluster
, num_clusters
, bpc
, clusters
;
1104 size_t cplen
, blocksize
;
1105 struct buffer_head
*bh
= NULL
;
1106 struct ocfs2_extent_list
*el
;
1109 clusters
= le32_to_cpu(xv
->xr_clusters
);
1110 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
1111 blocksize
= inode
->i_sb
->s_blocksize
;
1114 while (cpos
< clusters
) {
1115 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
1116 &num_clusters
, el
, NULL
);
1122 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
1123 /* Copy ocfs2_xattr_value */
1124 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
1125 ret
= ocfs2_read_block(INODE_CACHE(inode
), blkno
,
1132 cplen
= len
>= blocksize
? blocksize
: len
;
1133 memcpy(buffer
, bh
->b_data
, cplen
);
1142 cpos
+= num_clusters
;
1148 static int ocfs2_xattr_ibody_get(struct inode
*inode
,
1153 struct ocfs2_xattr_search
*xs
)
1155 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1156 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1157 struct ocfs2_xattr_value_root
*xv
;
1161 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
1164 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
1165 xs
->header
= (struct ocfs2_xattr_header
*)
1166 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
1167 xs
->base
= (void *)xs
->header
;
1168 xs
->here
= xs
->header
->xh_entries
;
1170 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1173 size
= le64_to_cpu(xs
->here
->xe_value_size
);
1175 if (size
> buffer_size
)
1177 if (ocfs2_xattr_is_local(xs
->here
)) {
1178 memcpy(buffer
, (void *)xs
->base
+
1179 le16_to_cpu(xs
->here
->xe_name_offset
) +
1180 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
), size
);
1182 xv
= (struct ocfs2_xattr_value_root
*)
1183 (xs
->base
+ le16_to_cpu(
1184 xs
->here
->xe_name_offset
) +
1185 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
));
1186 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
1198 static int ocfs2_xattr_block_get(struct inode
*inode
,
1203 struct ocfs2_xattr_search
*xs
)
1205 struct ocfs2_xattr_block
*xb
;
1206 struct ocfs2_xattr_value_root
*xv
;
1208 int ret
= -ENODATA
, name_offset
, name_len
, i
;
1209 int uninitialized_var(block_off
);
1211 xs
->bucket
= ocfs2_xattr_bucket_new(inode
);
1218 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, xs
);
1224 if (xs
->not_found
) {
1229 xb
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
1230 size
= le64_to_cpu(xs
->here
->xe_value_size
);
1233 if (size
> buffer_size
)
1236 name_offset
= le16_to_cpu(xs
->here
->xe_name_offset
);
1237 name_len
= OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
);
1238 i
= xs
->here
- xs
->header
->xh_entries
;
1240 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
1241 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
1242 bucket_xh(xs
->bucket
),
1246 xs
->base
= bucket_block(xs
->bucket
, block_off
);
1248 if (ocfs2_xattr_is_local(xs
->here
)) {
1249 memcpy(buffer
, (void *)xs
->base
+
1250 name_offset
+ name_len
, size
);
1252 xv
= (struct ocfs2_xattr_value_root
*)
1253 (xs
->base
+ name_offset
+ name_len
);
1254 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
1264 ocfs2_xattr_bucket_free(xs
->bucket
);
1266 brelse(xs
->xattr_bh
);
1267 xs
->xattr_bh
= NULL
;
1271 int ocfs2_xattr_get_nolock(struct inode
*inode
,
1272 struct buffer_head
*di_bh
,
1279 struct ocfs2_dinode
*di
= NULL
;
1280 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1281 struct ocfs2_xattr_search xis
= {
1282 .not_found
= -ENODATA
,
1284 struct ocfs2_xattr_search xbs
= {
1285 .not_found
= -ENODATA
,
1288 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
1291 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1294 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
1295 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1297 down_read(&oi
->ip_xattr_sem
);
1298 ret
= ocfs2_xattr_ibody_get(inode
, name_index
, name
, buffer
,
1300 if (ret
== -ENODATA
&& di
->i_xattr_loc
)
1301 ret
= ocfs2_xattr_block_get(inode
, name_index
, name
, buffer
,
1303 up_read(&oi
->ip_xattr_sem
);
1308 /* ocfs2_xattr_get()
1310 * Copy an extended attribute into the buffer provided.
1311 * Buffer is NULL to compute the size of buffer required.
1313 static int ocfs2_xattr_get(struct inode
*inode
,
1320 struct buffer_head
*di_bh
= NULL
;
1322 ret
= ocfs2_inode_lock(inode
, &di_bh
, 0);
1327 ret
= ocfs2_xattr_get_nolock(inode
, di_bh
, name_index
,
1328 name
, buffer
, buffer_size
);
1330 ocfs2_inode_unlock(inode
, 0);
1337 static int __ocfs2_xattr_set_value_outside(struct inode
*inode
,
1339 struct ocfs2_xattr_value_buf
*vb
,
1343 int ret
= 0, i
, cp_len
;
1344 u16 blocksize
= inode
->i_sb
->s_blocksize
;
1345 u32 p_cluster
, num_clusters
;
1346 u32 cpos
= 0, bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
1347 u32 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, value_len
);
1349 struct buffer_head
*bh
= NULL
;
1350 unsigned int ext_flags
;
1351 struct ocfs2_xattr_value_root
*xv
= vb
->vb_xv
;
1353 BUG_ON(clusters
> le32_to_cpu(xv
->xr_clusters
));
1355 while (cpos
< clusters
) {
1356 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
1357 &num_clusters
, &xv
->xr_list
,
1364 BUG_ON(ext_flags
& OCFS2_EXT_REFCOUNTED
);
1366 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
1368 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
1369 ret
= ocfs2_read_block(INODE_CACHE(inode
), blkno
,
1376 ret
= ocfs2_journal_access(handle
,
1379 OCFS2_JOURNAL_ACCESS_WRITE
);
1385 cp_len
= value_len
> blocksize
? blocksize
: value_len
;
1386 memcpy(bh
->b_data
, value
, cp_len
);
1387 value_len
-= cp_len
;
1389 if (cp_len
< blocksize
)
1390 memset(bh
->b_data
+ cp_len
, 0,
1391 blocksize
- cp_len
);
1393 ret
= ocfs2_journal_dirty(handle
, bh
);
1402 * XXX: do we need to empty all the following
1403 * blocks in this cluster?
1408 cpos
+= num_clusters
;
1416 static int ocfs2_xa_check_space_helper(int needed_space
, int free_start
,
1424 free_space
= free_start
-
1425 sizeof(struct ocfs2_xattr_header
) -
1426 (num_entries
* sizeof(struct ocfs2_xattr_entry
)) -
1427 OCFS2_XATTR_HEADER_GAP
;
1430 if (free_space
< needed_space
)
1436 static int ocfs2_xa_journal_access(handle_t
*handle
, struct ocfs2_xa_loc
*loc
,
1439 return loc
->xl_ops
->xlo_journal_access(handle
, loc
, type
);
1442 static void ocfs2_xa_journal_dirty(handle_t
*handle
, struct ocfs2_xa_loc
*loc
)
1444 loc
->xl_ops
->xlo_journal_dirty(handle
, loc
);
1447 /* Give a pointer into the storage for the given offset */
1448 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc
*loc
, int offset
)
1450 BUG_ON(offset
>= loc
->xl_size
);
1451 return loc
->xl_ops
->xlo_offset_pointer(loc
, offset
);
1455 * Wipe the name+value pair and allow the storage to reclaim it. This
1456 * must be followed by either removal of the entry or a call to
1457 * ocfs2_xa_add_namevalue().
1459 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1461 loc
->xl_ops
->xlo_wipe_namevalue(loc
);
1465 * Find lowest offset to a name+value pair. This is the start of our
1466 * downward-growing free space.
1468 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc
*loc
)
1470 return loc
->xl_ops
->xlo_get_free_start(loc
);
1473 /* Can we reuse loc->xl_entry for xi? */
1474 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc
*loc
,
1475 struct ocfs2_xattr_info
*xi
)
1477 return loc
->xl_ops
->xlo_can_reuse(loc
, xi
);
1480 /* How much free space is needed to set the new value */
1481 static int ocfs2_xa_check_space(struct ocfs2_xa_loc
*loc
,
1482 struct ocfs2_xattr_info
*xi
)
1484 return loc
->xl_ops
->xlo_check_space(loc
, xi
);
1487 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1489 loc
->xl_ops
->xlo_add_entry(loc
, name_hash
);
1490 loc
->xl_entry
->xe_name_hash
= cpu_to_le32(name_hash
);
1492 * We can't leave the new entry's xe_name_offset at zero or
1493 * add_namevalue() will go nuts. We set it to the size of our
1494 * storage so that it can never be less than any other entry.
1496 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(loc
->xl_size
);
1499 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc
*loc
,
1500 struct ocfs2_xattr_info
*xi
)
1502 int size
= namevalue_size_xi(xi
);
1506 loc
->xl_ops
->xlo_add_namevalue(loc
, size
);
1507 loc
->xl_entry
->xe_value_size
= cpu_to_le64(xi
->xi_value_len
);
1508 loc
->xl_entry
->xe_name_len
= xi
->xi_name_len
;
1509 ocfs2_xattr_set_type(loc
->xl_entry
, xi
->xi_name_index
);
1510 ocfs2_xattr_set_local(loc
->xl_entry
,
1511 xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
);
1513 nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1514 nameval_buf
= ocfs2_xa_offset_pointer(loc
, nameval_offset
);
1515 memset(nameval_buf
, 0, size
);
1516 memcpy(nameval_buf
, xi
->xi_name
, xi
->xi_name_len
);
1519 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1520 struct ocfs2_xattr_value_buf
*vb
)
1522 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1523 int name_size
= OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
);
1525 /* Value bufs are for value trees */
1526 BUG_ON(ocfs2_xattr_is_local(loc
->xl_entry
));
1527 BUG_ON(namevalue_size_xe(loc
->xl_entry
) !=
1528 (name_size
+ OCFS2_XATTR_ROOT_SIZE
));
1530 loc
->xl_ops
->xlo_fill_value_buf(loc
, vb
);
1532 (struct ocfs2_xattr_value_root
*)ocfs2_xa_offset_pointer(loc
,
1537 static int ocfs2_xa_block_journal_access(handle_t
*handle
,
1538 struct ocfs2_xa_loc
*loc
, int type
)
1540 struct buffer_head
*bh
= loc
->xl_storage
;
1541 ocfs2_journal_access_func access
;
1543 if (loc
->xl_size
== (bh
->b_size
-
1544 offsetof(struct ocfs2_xattr_block
,
1545 xb_attrs
.xb_header
)))
1546 access
= ocfs2_journal_access_xb
;
1548 access
= ocfs2_journal_access_di
;
1549 return access(handle
, INODE_CACHE(loc
->xl_inode
), bh
, type
);
1552 static void ocfs2_xa_block_journal_dirty(handle_t
*handle
,
1553 struct ocfs2_xa_loc
*loc
)
1555 struct buffer_head
*bh
= loc
->xl_storage
;
1557 ocfs2_journal_dirty(handle
, bh
);
1560 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc
*loc
,
1563 return (char *)loc
->xl_header
+ offset
;
1566 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc
*loc
,
1567 struct ocfs2_xattr_info
*xi
)
1570 * Block storage is strict. If the sizes aren't exact, we will
1571 * remove the old one and reinsert the new.
1573 return namevalue_size_xe(loc
->xl_entry
) ==
1574 namevalue_size_xi(xi
);
1577 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc
*loc
)
1579 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1580 int i
, count
= le16_to_cpu(xh
->xh_count
);
1581 int offset
, free_start
= loc
->xl_size
;
1583 for (i
= 0; i
< count
; i
++) {
1584 offset
= le16_to_cpu(xh
->xh_entries
[i
].xe_name_offset
);
1585 if (offset
< free_start
)
1586 free_start
= offset
;
1592 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc
*loc
,
1593 struct ocfs2_xattr_info
*xi
)
1595 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1596 int free_start
= ocfs2_xa_get_free_start(loc
);
1597 int needed_space
= ocfs2_xi_entry_usage(xi
);
1600 * Block storage will reclaim the original entry before inserting
1601 * the new value, so we only need the difference. If the new
1602 * entry is smaller than the old one, we don't need anything.
1604 if (loc
->xl_entry
) {
1605 /* Don't need space if we're reusing! */
1606 if (ocfs2_xa_can_reuse_entry(loc
, xi
))
1609 needed_space
-= ocfs2_xe_entry_usage(loc
->xl_entry
);
1611 if (needed_space
< 0)
1613 return ocfs2_xa_check_space_helper(needed_space
, free_start
, count
);
1617 * Block storage for xattrs keeps the name+value pairs compacted. When
1618 * we remove one, we have to shift any that preceded it towards the end.
1620 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1623 int namevalue_offset
, first_namevalue_offset
, namevalue_size
;
1624 struct ocfs2_xattr_entry
*entry
= loc
->xl_entry
;
1625 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1626 int count
= le16_to_cpu(xh
->xh_count
);
1628 namevalue_offset
= le16_to_cpu(entry
->xe_name_offset
);
1629 namevalue_size
= namevalue_size_xe(entry
);
1630 first_namevalue_offset
= ocfs2_xa_get_free_start(loc
);
1632 /* Shift the name+value pairs */
1633 memmove((char *)xh
+ first_namevalue_offset
+ namevalue_size
,
1634 (char *)xh
+ first_namevalue_offset
,
1635 namevalue_offset
- first_namevalue_offset
);
1636 memset((char *)xh
+ first_namevalue_offset
, 0, namevalue_size
);
1638 /* Now tell xh->xh_entries about it */
1639 for (i
= 0; i
< count
; i
++) {
1640 offset
= le16_to_cpu(xh
->xh_entries
[i
].xe_name_offset
);
1641 if (offset
<= namevalue_offset
)
1642 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
,
1647 * Note that we don't update xh_free_start or xh_name_value_len
1648 * because they're not used in block-stored xattrs.
1652 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1654 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1655 loc
->xl_entry
= &(loc
->xl_header
->xh_entries
[count
]);
1656 le16_add_cpu(&loc
->xl_header
->xh_count
, 1);
1657 memset(loc
->xl_entry
, 0, sizeof(struct ocfs2_xattr_entry
));
1660 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc
*loc
, int size
)
1662 int free_start
= ocfs2_xa_get_free_start(loc
);
1664 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(free_start
- size
);
1667 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1668 struct ocfs2_xattr_value_buf
*vb
)
1670 struct buffer_head
*bh
= loc
->xl_storage
;
1672 if (loc
->xl_size
== (bh
->b_size
-
1673 offsetof(struct ocfs2_xattr_block
,
1674 xb_attrs
.xb_header
)))
1675 vb
->vb_access
= ocfs2_journal_access_xb
;
1677 vb
->vb_access
= ocfs2_journal_access_di
;
1682 * Operations for xattrs stored in blocks. This includes inline inode
1683 * storage and unindexed ocfs2_xattr_blocks.
1685 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops
= {
1686 .xlo_journal_access
= ocfs2_xa_block_journal_access
,
1687 .xlo_journal_dirty
= ocfs2_xa_block_journal_dirty
,
1688 .xlo_offset_pointer
= ocfs2_xa_block_offset_pointer
,
1689 .xlo_check_space
= ocfs2_xa_block_check_space
,
1690 .xlo_can_reuse
= ocfs2_xa_block_can_reuse
,
1691 .xlo_get_free_start
= ocfs2_xa_block_get_free_start
,
1692 .xlo_wipe_namevalue
= ocfs2_xa_block_wipe_namevalue
,
1693 .xlo_add_entry
= ocfs2_xa_block_add_entry
,
1694 .xlo_add_namevalue
= ocfs2_xa_block_add_namevalue
,
1695 .xlo_fill_value_buf
= ocfs2_xa_block_fill_value_buf
,
1698 static int ocfs2_xa_bucket_journal_access(handle_t
*handle
,
1699 struct ocfs2_xa_loc
*loc
, int type
)
1701 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1703 return ocfs2_xattr_bucket_journal_access(handle
, bucket
, type
);
1706 static void ocfs2_xa_bucket_journal_dirty(handle_t
*handle
,
1707 struct ocfs2_xa_loc
*loc
)
1709 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1711 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
1714 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc
*loc
,
1717 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1718 int block
, block_offset
;
1720 /* The header is at the front of the bucket */
1721 block
= offset
>> loc
->xl_inode
->i_sb
->s_blocksize_bits
;
1722 block_offset
= offset
% loc
->xl_inode
->i_sb
->s_blocksize
;
1724 return bucket_block(bucket
, block
) + block_offset
;
1727 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc
*loc
,
1728 struct ocfs2_xattr_info
*xi
)
1730 return namevalue_size_xe(loc
->xl_entry
) >=
1731 namevalue_size_xi(xi
);
1734 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc
*loc
)
1736 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1737 return le16_to_cpu(bucket_xh(bucket
)->xh_free_start
);
1740 static int ocfs2_bucket_align_free_start(struct super_block
*sb
,
1741 int free_start
, int size
)
1744 * We need to make sure that the name+value pair fits within
1747 if (((free_start
- size
) >> sb
->s_blocksize_bits
) !=
1748 ((free_start
- 1) >> sb
->s_blocksize_bits
))
1749 free_start
-= free_start
% sb
->s_blocksize
;
1754 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc
*loc
,
1755 struct ocfs2_xattr_info
*xi
)
1758 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1759 int free_start
= ocfs2_xa_get_free_start(loc
);
1760 int needed_space
= ocfs2_xi_entry_usage(xi
);
1761 int size
= namevalue_size_xi(xi
);
1762 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1765 * Bucket storage does not reclaim name+value pairs it cannot
1766 * reuse. They live as holes until the bucket fills, and then
1767 * the bucket is defragmented. However, the bucket can reclaim
1768 * the ocfs2_xattr_entry.
1770 if (loc
->xl_entry
) {
1771 /* Don't need space if we're reusing! */
1772 if (ocfs2_xa_can_reuse_entry(loc
, xi
))
1775 needed_space
-= sizeof(struct ocfs2_xattr_entry
);
1777 BUG_ON(needed_space
< 0);
1779 if (free_start
< size
) {
1784 * First we check if it would fit in the first place.
1785 * Below, we align the free start to a block. This may
1786 * slide us below the minimum gap. By checking unaligned
1787 * first, we avoid that error.
1789 rc
= ocfs2_xa_check_space_helper(needed_space
, free_start
,
1793 free_start
= ocfs2_bucket_align_free_start(sb
, free_start
,
1796 return ocfs2_xa_check_space_helper(needed_space
, free_start
, count
);
1799 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1801 le16_add_cpu(&loc
->xl_header
->xh_name_value_len
,
1802 -namevalue_size_xe(loc
->xl_entry
));
1805 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1807 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1808 int count
= le16_to_cpu(xh
->xh_count
);
1809 int low
= 0, high
= count
- 1, tmp
;
1810 struct ocfs2_xattr_entry
*tmp_xe
;
1813 * We keep buckets sorted by name_hash, so we need to find
1816 while (low
<= high
&& count
) {
1817 tmp
= (low
+ high
) / 2;
1818 tmp_xe
= &xh
->xh_entries
[tmp
];
1820 if (name_hash
> le32_to_cpu(tmp_xe
->xe_name_hash
))
1822 else if (name_hash
< le32_to_cpu(tmp_xe
->xe_name_hash
))
1831 memmove(&xh
->xh_entries
[low
+ 1],
1832 &xh
->xh_entries
[low
],
1833 ((count
- low
) * sizeof(struct ocfs2_xattr_entry
)));
1835 le16_add_cpu(&xh
->xh_count
, 1);
1836 loc
->xl_entry
= &xh
->xh_entries
[low
];
1837 memset(loc
->xl_entry
, 0, sizeof(struct ocfs2_xattr_entry
));
1840 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc
*loc
, int size
)
1842 int free_start
= ocfs2_xa_get_free_start(loc
);
1843 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1844 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1847 free_start
= ocfs2_bucket_align_free_start(sb
, free_start
, size
);
1848 nameval_offset
= free_start
- size
;
1849 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(nameval_offset
);
1850 xh
->xh_free_start
= cpu_to_le16(nameval_offset
);
1851 le16_add_cpu(&xh
->xh_name_value_len
, size
);
1855 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1856 struct ocfs2_xattr_value_buf
*vb
)
1858 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1859 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1860 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1861 int size
= namevalue_size_xe(loc
->xl_entry
);
1862 int block_offset
= nameval_offset
>> sb
->s_blocksize_bits
;
1864 /* Values are not allowed to straddle block boundaries */
1865 BUG_ON(block_offset
!=
1866 ((nameval_offset
+ size
- 1) >> sb
->s_blocksize_bits
));
1867 /* We expect the bucket to be filled in */
1868 BUG_ON(!bucket
->bu_bhs
[block_offset
]);
1870 vb
->vb_access
= ocfs2_journal_access
;
1871 vb
->vb_bh
= bucket
->bu_bhs
[block_offset
];
1874 /* Operations for xattrs stored in buckets. */
1875 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops
= {
1876 .xlo_journal_access
= ocfs2_xa_bucket_journal_access
,
1877 .xlo_journal_dirty
= ocfs2_xa_bucket_journal_dirty
,
1878 .xlo_offset_pointer
= ocfs2_xa_bucket_offset_pointer
,
1879 .xlo_check_space
= ocfs2_xa_bucket_check_space
,
1880 .xlo_can_reuse
= ocfs2_xa_bucket_can_reuse
,
1881 .xlo_get_free_start
= ocfs2_xa_bucket_get_free_start
,
1882 .xlo_wipe_namevalue
= ocfs2_xa_bucket_wipe_namevalue
,
1883 .xlo_add_entry
= ocfs2_xa_bucket_add_entry
,
1884 .xlo_add_namevalue
= ocfs2_xa_bucket_add_namevalue
,
1885 .xlo_fill_value_buf
= ocfs2_xa_bucket_fill_value_buf
,
1888 static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc
*loc
)
1890 struct ocfs2_xattr_value_buf vb
;
1892 if (ocfs2_xattr_is_local(loc
->xl_entry
))
1895 ocfs2_xa_fill_value_buf(loc
, &vb
);
1896 return le32_to_cpu(vb
.vb_xv
->xr_clusters
);
1899 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc
*loc
, u64 bytes
,
1900 struct ocfs2_xattr_set_ctxt
*ctxt
)
1902 int trunc_rc
, access_rc
;
1903 struct ocfs2_xattr_value_buf vb
;
1905 ocfs2_xa_fill_value_buf(loc
, &vb
);
1906 trunc_rc
= ocfs2_xattr_value_truncate(loc
->xl_inode
, &vb
, bytes
,
1910 * The caller of ocfs2_xa_value_truncate() has already called
1911 * ocfs2_xa_journal_access on the loc. However, The truncate code
1912 * calls ocfs2_extend_trans(). This may commit the previous
1913 * transaction and open a new one. If this is a bucket, truncate
1914 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1915 * the caller is expecting to dirty the entire bucket. So we must
1916 * reset the journal work. We do this even if truncate has failed,
1917 * as it could have failed after committing the extend.
1919 access_rc
= ocfs2_xa_journal_access(ctxt
->handle
, loc
,
1920 OCFS2_JOURNAL_ACCESS_WRITE
);
1922 /* Errors in truncate take precedence */
1923 return trunc_rc
? trunc_rc
: access_rc
;
1926 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc
*loc
)
1929 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1930 struct ocfs2_xattr_entry
*entry
= loc
->xl_entry
;
1932 ocfs2_xa_wipe_namevalue(loc
);
1933 loc
->xl_entry
= NULL
;
1935 le16_add_cpu(&xh
->xh_count
, -1);
1936 count
= le16_to_cpu(xh
->xh_count
);
1939 * Only zero out the entry if there are more remaining. This is
1940 * important for an empty bucket, as it keeps track of the
1941 * bucket's hash value. It doesn't hurt empty block storage.
1944 index
= ((char *)entry
- (char *)&xh
->xh_entries
) /
1945 sizeof(struct ocfs2_xattr_entry
);
1946 memmove(&xh
->xh_entries
[index
], &xh
->xh_entries
[index
+ 1],
1947 (count
- index
) * sizeof(struct ocfs2_xattr_entry
));
1948 memset(&xh
->xh_entries
[count
], 0,
1949 sizeof(struct ocfs2_xattr_entry
));
1954 * If we have a problem adjusting the size of an external value during
1955 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1956 * in an intermediate state. For example, the value may be partially
1959 * If the value tree hasn't changed, the extend/truncate went nowhere.
1960 * We have nothing to do. The caller can treat it as a straight error.
1962 * If the value tree got partially truncated, we now have a corrupted
1963 * extended attribute. We're going to wipe its entry and leak the
1964 * clusters. Better to leak some storage than leave a corrupt entry.
1966 * If the value tree grew, it obviously didn't grow enough for the
1967 * new entry. We're not going to try and reclaim those clusters either.
1968 * If there was already an external value there (orig_clusters != 0),
1969 * the new clusters are attached safely and we can just leave the old
1970 * value in place. If there was no external value there, we remove
1973 * This way, the xattr block we store in the journal will be consistent.
1974 * If the size change broke because of the journal, no changes will hit
1977 static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc
*loc
,
1979 unsigned int orig_clusters
)
1981 unsigned int new_clusters
= ocfs2_xa_value_clusters(loc
);
1982 char *nameval_buf
= ocfs2_xa_offset_pointer(loc
,
1983 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
1985 if (new_clusters
< orig_clusters
) {
1987 "Partial truncate while %s xattr %.*s. Leaking "
1988 "%u clusters and removing the entry\n",
1989 what
, loc
->xl_entry
->xe_name_len
, nameval_buf
,
1990 orig_clusters
- new_clusters
);
1991 ocfs2_xa_remove_entry(loc
);
1992 } else if (!orig_clusters
) {
1994 "Unable to allocate an external value for xattr "
1995 "%.*s safely. Leaking %u clusters and removing the "
1997 loc
->xl_entry
->xe_name_len
, nameval_buf
,
1998 new_clusters
- orig_clusters
);
1999 ocfs2_xa_remove_entry(loc
);
2000 } else if (new_clusters
> orig_clusters
)
2002 "Unable to grow xattr %.*s safely. %u new clusters "
2003 "have been added, but the value will not be "
2005 loc
->xl_entry
->xe_name_len
, nameval_buf
,
2006 new_clusters
- orig_clusters
);
2009 static int ocfs2_xa_remove(struct ocfs2_xa_loc
*loc
,
2010 struct ocfs2_xattr_set_ctxt
*ctxt
)
2013 unsigned int orig_clusters
;
2015 if (!ocfs2_xattr_is_local(loc
->xl_entry
)) {
2016 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2017 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2021 * Since this is remove, we can return 0 if
2022 * ocfs2_xa_cleanup_value_truncate() is going to
2023 * wipe the entry anyway. So we check the
2024 * cluster count as well.
2026 if (orig_clusters
!= ocfs2_xa_value_clusters(loc
))
2028 ocfs2_xa_cleanup_value_truncate(loc
, "removing",
2035 ocfs2_xa_remove_entry(loc
);
2041 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc
*loc
)
2043 int name_size
= OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
);
2046 nameval_buf
= ocfs2_xa_offset_pointer(loc
,
2047 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
2048 memcpy(nameval_buf
+ name_size
, &def_xv
, OCFS2_XATTR_ROOT_SIZE
);
2052 * Take an existing entry and make it ready for the new value. This
2053 * won't allocate space, but it may free space. It should be ready for
2054 * ocfs2_xa_prepare_entry() to finish the work.
2056 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc
*loc
,
2057 struct ocfs2_xattr_info
*xi
,
2058 struct ocfs2_xattr_set_ctxt
*ctxt
)
2061 int name_size
= OCFS2_XATTR_SIZE(xi
->xi_name_len
);
2062 unsigned int orig_clusters
;
2064 int xe_local
= ocfs2_xattr_is_local(loc
->xl_entry
);
2065 int xi_local
= xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
;
2067 BUG_ON(OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
) !=
2070 nameval_buf
= ocfs2_xa_offset_pointer(loc
,
2071 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
2073 memset(nameval_buf
+ name_size
, 0,
2074 namevalue_size_xe(loc
->xl_entry
) - name_size
);
2076 ocfs2_xa_install_value_root(loc
);
2078 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2080 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2084 memset(nameval_buf
+ name_size
, 0,
2085 namevalue_size_xe(loc
->xl_entry
) -
2087 } else if (le64_to_cpu(loc
->xl_entry
->xe_value_size
) >
2089 rc
= ocfs2_xa_value_truncate(loc
, xi
->xi_value_len
,
2096 ocfs2_xa_cleanup_value_truncate(loc
, "reusing",
2102 loc
->xl_entry
->xe_value_size
= cpu_to_le64(xi
->xi_value_len
);
2103 ocfs2_xattr_set_local(loc
->xl_entry
, xi_local
);
2110 * Prepares loc->xl_entry to receive the new xattr. This includes
2111 * properly setting up the name+value pair region. If loc->xl_entry
2112 * already exists, it will take care of modifying it appropriately.
2114 * Note that this modifies the data. You did journal_access already,
2117 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc
*loc
,
2118 struct ocfs2_xattr_info
*xi
,
2120 struct ocfs2_xattr_set_ctxt
*ctxt
)
2123 unsigned int orig_clusters
;
2124 __le64 orig_value_size
= 0;
2126 rc
= ocfs2_xa_check_space(loc
, xi
);
2130 if (loc
->xl_entry
) {
2131 if (ocfs2_xa_can_reuse_entry(loc
, xi
)) {
2132 orig_value_size
= loc
->xl_entry
->xe_value_size
;
2133 rc
= ocfs2_xa_reuse_entry(loc
, xi
, ctxt
);
2139 if (!ocfs2_xattr_is_local(loc
->xl_entry
)) {
2140 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2141 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2144 ocfs2_xa_cleanup_value_truncate(loc
,
2150 ocfs2_xa_wipe_namevalue(loc
);
2152 ocfs2_xa_add_entry(loc
, name_hash
);
2155 * If we get here, we have a blank entry. Fill it. We grow our
2156 * name+value pair back from the end.
2158 ocfs2_xa_add_namevalue(loc
, xi
);
2159 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
)
2160 ocfs2_xa_install_value_root(loc
);
2163 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2164 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2165 rc
= ocfs2_xa_value_truncate(loc
, xi
->xi_value_len
, ctxt
);
2168 * If we tried to grow an existing external value,
2169 * ocfs2_xa_cleanuP-value_truncate() is going to
2170 * let it stand. We have to restore its original
2173 loc
->xl_entry
->xe_value_size
= orig_value_size
;
2174 ocfs2_xa_cleanup_value_truncate(loc
, "growing",
2185 * Store the value portion of the name+value pair. This will skip
2186 * values that are stored externally. Their tree roots were set up
2187 * by ocfs2_xa_prepare_entry().
2189 static int ocfs2_xa_store_value(struct ocfs2_xa_loc
*loc
,
2190 struct ocfs2_xattr_info
*xi
,
2191 struct ocfs2_xattr_set_ctxt
*ctxt
)
2194 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
2195 int name_size
= OCFS2_XATTR_SIZE(xi
->xi_name_len
);
2197 struct ocfs2_xattr_value_buf vb
;
2199 nameval_buf
= ocfs2_xa_offset_pointer(loc
, nameval_offset
);
2200 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2201 ocfs2_xa_fill_value_buf(loc
, &vb
);
2202 rc
= __ocfs2_xattr_set_value_outside(loc
->xl_inode
,
2207 memcpy(nameval_buf
+ name_size
, xi
->xi_value
, xi
->xi_value_len
);
2212 static int ocfs2_xa_set(struct ocfs2_xa_loc
*loc
,
2213 struct ocfs2_xattr_info
*xi
,
2214 struct ocfs2_xattr_set_ctxt
*ctxt
)
2217 u32 name_hash
= ocfs2_xattr_name_hash(loc
->xl_inode
, xi
->xi_name
,
2220 ret
= ocfs2_xa_journal_access(ctxt
->handle
, loc
,
2221 OCFS2_JOURNAL_ACCESS_WRITE
);
2228 * From here on out, everything is going to modify the buffer a
2229 * little. Errors are going to leave the xattr header in a
2230 * sane state. Thus, even with errors we dirty the sucker.
2233 /* Don't worry, we are never called with !xi_value and !xl_entry */
2234 if (!xi
->xi_value
) {
2235 ret
= ocfs2_xa_remove(loc
, ctxt
);
2239 ret
= ocfs2_xa_prepare_entry(loc
, xi
, name_hash
, ctxt
);
2246 ret
= ocfs2_xa_store_value(loc
, xi
, ctxt
);
2251 ocfs2_xa_journal_dirty(ctxt
->handle
, loc
);
2257 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc
*loc
,
2258 struct inode
*inode
,
2259 struct buffer_head
*bh
,
2260 struct ocfs2_xattr_entry
*entry
)
2262 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)bh
->b_data
;
2264 BUG_ON(!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
));
2266 loc
->xl_inode
= inode
;
2267 loc
->xl_ops
= &ocfs2_xa_block_loc_ops
;
2268 loc
->xl_storage
= bh
;
2269 loc
->xl_entry
= entry
;
2270 loc
->xl_size
= le16_to_cpu(di
->i_xattr_inline_size
);
2272 (struct ocfs2_xattr_header
*)(bh
->b_data
+ bh
->b_size
-
2276 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc
*loc
,
2277 struct inode
*inode
,
2278 struct buffer_head
*bh
,
2279 struct ocfs2_xattr_entry
*entry
)
2281 struct ocfs2_xattr_block
*xb
=
2282 (struct ocfs2_xattr_block
*)bh
->b_data
;
2284 BUG_ON(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
);
2286 loc
->xl_inode
= inode
;
2287 loc
->xl_ops
= &ocfs2_xa_block_loc_ops
;
2288 loc
->xl_storage
= bh
;
2289 loc
->xl_header
= &(xb
->xb_attrs
.xb_header
);
2290 loc
->xl_entry
= entry
;
2291 loc
->xl_size
= bh
->b_size
- offsetof(struct ocfs2_xattr_block
,
2292 xb_attrs
.xb_header
);
2295 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc
*loc
,
2296 struct ocfs2_xattr_bucket
*bucket
,
2297 struct ocfs2_xattr_entry
*entry
)
2299 loc
->xl_inode
= bucket
->bu_inode
;
2300 loc
->xl_ops
= &ocfs2_xa_bucket_loc_ops
;
2301 loc
->xl_storage
= bucket
;
2302 loc
->xl_header
= bucket_xh(bucket
);
2303 loc
->xl_entry
= entry
;
2304 loc
->xl_size
= OCFS2_XATTR_BUCKET_SIZE
;
2308 * In xattr remove, if it is stored outside and refcounted, we may have
2309 * the chance to split the refcount tree. So need the allocators.
2311 static int ocfs2_lock_xattr_remove_allocators(struct inode
*inode
,
2312 struct ocfs2_xattr_value_root
*xv
,
2313 struct ocfs2_caching_info
*ref_ci
,
2314 struct buffer_head
*ref_root_bh
,
2315 struct ocfs2_alloc_context
**meta_ac
,
2318 int ret
, meta_add
= 0;
2319 u32 p_cluster
, num_clusters
;
2320 unsigned int ext_flags
;
2323 ret
= ocfs2_xattr_get_clusters(inode
, 0, &p_cluster
,
2332 if (!(ext_flags
& OCFS2_EXT_REFCOUNTED
))
2335 ret
= ocfs2_refcounted_xattr_delete_need(inode
, ref_ci
,
2337 &meta_add
, ref_credits
);
2343 ret
= ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode
->i_sb
),
2352 static int ocfs2_remove_value_outside(struct inode
*inode
,
2353 struct ocfs2_xattr_value_buf
*vb
,
2354 struct ocfs2_xattr_header
*header
,
2355 struct ocfs2_caching_info
*ref_ci
,
2356 struct buffer_head
*ref_root_bh
)
2358 int ret
= 0, i
, ref_credits
;
2359 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2360 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, };
2363 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
2365 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
2366 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
2368 if (ocfs2_xattr_is_local(entry
))
2371 val
= (void *)header
+
2372 le16_to_cpu(entry
->xe_name_offset
);
2373 vb
->vb_xv
= (struct ocfs2_xattr_value_root
*)
2374 (val
+ OCFS2_XATTR_SIZE(entry
->xe_name_len
));
2376 ret
= ocfs2_lock_xattr_remove_allocators(inode
, vb
->vb_xv
,
2377 ref_ci
, ref_root_bh
,
2381 ctxt
.handle
= ocfs2_start_trans(osb
, ref_credits
+
2382 ocfs2_remove_extent_credits(osb
->sb
));
2383 if (IS_ERR(ctxt
.handle
)) {
2384 ret
= PTR_ERR(ctxt
.handle
);
2389 ret
= ocfs2_xattr_value_truncate(inode
, vb
, 0, &ctxt
);
2395 ocfs2_commit_trans(osb
, ctxt
.handle
);
2397 ocfs2_free_alloc_context(ctxt
.meta_ac
);
2398 ctxt
.meta_ac
= NULL
;
2403 ocfs2_free_alloc_context(ctxt
.meta_ac
);
2404 ocfs2_schedule_truncate_log_flush(osb
, 1);
2405 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
2409 static int ocfs2_xattr_ibody_remove(struct inode
*inode
,
2410 struct buffer_head
*di_bh
,
2411 struct ocfs2_caching_info
*ref_ci
,
2412 struct buffer_head
*ref_root_bh
)
2415 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2416 struct ocfs2_xattr_header
*header
;
2418 struct ocfs2_xattr_value_buf vb
= {
2420 .vb_access
= ocfs2_journal_access_di
,
2423 header
= (struct ocfs2_xattr_header
*)
2424 ((void *)di
+ inode
->i_sb
->s_blocksize
-
2425 le16_to_cpu(di
->i_xattr_inline_size
));
2427 ret
= ocfs2_remove_value_outside(inode
, &vb
, header
,
2428 ref_ci
, ref_root_bh
);
2433 struct ocfs2_rm_xattr_bucket_para
{
2434 struct ocfs2_caching_info
*ref_ci
;
2435 struct buffer_head
*ref_root_bh
;
2438 static int ocfs2_xattr_block_remove(struct inode
*inode
,
2439 struct buffer_head
*blk_bh
,
2440 struct ocfs2_caching_info
*ref_ci
,
2441 struct buffer_head
*ref_root_bh
)
2443 struct ocfs2_xattr_block
*xb
;
2445 struct ocfs2_xattr_value_buf vb
= {
2447 .vb_access
= ocfs2_journal_access_xb
,
2449 struct ocfs2_rm_xattr_bucket_para args
= {
2451 .ref_root_bh
= ref_root_bh
,
2454 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2455 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2456 struct ocfs2_xattr_header
*header
= &(xb
->xb_attrs
.xb_header
);
2457 ret
= ocfs2_remove_value_outside(inode
, &vb
, header
,
2458 ref_ci
, ref_root_bh
);
2460 ret
= ocfs2_iterate_xattr_index_block(inode
,
2462 ocfs2_rm_xattr_cluster
,
2468 static int ocfs2_xattr_free_block(struct inode
*inode
,
2470 struct ocfs2_caching_info
*ref_ci
,
2471 struct buffer_head
*ref_root_bh
)
2473 struct inode
*xb_alloc_inode
;
2474 struct buffer_head
*xb_alloc_bh
= NULL
;
2475 struct buffer_head
*blk_bh
= NULL
;
2476 struct ocfs2_xattr_block
*xb
;
2477 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2483 ret
= ocfs2_read_xattr_block(inode
, block
, &blk_bh
);
2489 ret
= ocfs2_xattr_block_remove(inode
, blk_bh
, ref_ci
, ref_root_bh
);
2495 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2496 blk
= le64_to_cpu(xb
->xb_blkno
);
2497 bit
= le16_to_cpu(xb
->xb_suballoc_bit
);
2498 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
2500 xb_alloc_inode
= ocfs2_get_system_file_inode(osb
,
2501 EXTENT_ALLOC_SYSTEM_INODE
,
2502 le16_to_cpu(xb
->xb_suballoc_slot
));
2503 if (!xb_alloc_inode
) {
2508 mutex_lock(&xb_alloc_inode
->i_mutex
);
2510 ret
= ocfs2_inode_lock(xb_alloc_inode
, &xb_alloc_bh
, 1);
2516 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
2517 if (IS_ERR(handle
)) {
2518 ret
= PTR_ERR(handle
);
2523 ret
= ocfs2_free_suballoc_bits(handle
, xb_alloc_inode
, xb_alloc_bh
,
2528 ocfs2_commit_trans(osb
, handle
);
2530 ocfs2_inode_unlock(xb_alloc_inode
, 1);
2531 brelse(xb_alloc_bh
);
2533 mutex_unlock(&xb_alloc_inode
->i_mutex
);
2534 iput(xb_alloc_inode
);
2541 * ocfs2_xattr_remove()
2543 * Free extended attribute resources associated with this inode.
2545 int ocfs2_xattr_remove(struct inode
*inode
, struct buffer_head
*di_bh
)
2547 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2548 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2549 struct ocfs2_refcount_tree
*ref_tree
= NULL
;
2550 struct buffer_head
*ref_root_bh
= NULL
;
2551 struct ocfs2_caching_info
*ref_ci
= NULL
;
2555 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
2558 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
2561 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
) {
2562 ret
= ocfs2_lock_refcount_tree(OCFS2_SB(inode
->i_sb
),
2563 le64_to_cpu(di
->i_refcount_loc
),
2564 1, &ref_tree
, &ref_root_bh
);
2569 ref_ci
= &ref_tree
->rf_ci
;
2573 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
2574 ret
= ocfs2_xattr_ibody_remove(inode
, di_bh
,
2575 ref_ci
, ref_root_bh
);
2582 if (di
->i_xattr_loc
) {
2583 ret
= ocfs2_xattr_free_block(inode
,
2584 le64_to_cpu(di
->i_xattr_loc
),
2585 ref_ci
, ref_root_bh
);
2592 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
2593 OCFS2_INODE_UPDATE_CREDITS
);
2594 if (IS_ERR(handle
)) {
2595 ret
= PTR_ERR(handle
);
2599 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
2600 OCFS2_JOURNAL_ACCESS_WRITE
);
2606 di
->i_xattr_loc
= 0;
2608 spin_lock(&oi
->ip_lock
);
2609 oi
->ip_dyn_features
&= ~(OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
);
2610 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
2611 spin_unlock(&oi
->ip_lock
);
2613 ret
= ocfs2_journal_dirty(handle
, di_bh
);
2617 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
2620 ocfs2_unlock_refcount_tree(OCFS2_SB(inode
->i_sb
), ref_tree
, 1);
2621 brelse(ref_root_bh
);
2625 static int ocfs2_xattr_has_space_inline(struct inode
*inode
,
2626 struct ocfs2_dinode
*di
)
2628 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2629 unsigned int xattrsize
= OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
;
2632 if (xattrsize
< OCFS2_MIN_XATTR_INLINE_SIZE
)
2635 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
2636 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
2637 free
= le16_to_cpu(idata
->id_count
) - le64_to_cpu(di
->i_size
);
2638 } else if (ocfs2_inode_is_fast_symlink(inode
)) {
2639 free
= ocfs2_fast_symlink_chars(inode
->i_sb
) -
2640 le64_to_cpu(di
->i_size
);
2642 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
2643 free
= (le16_to_cpu(el
->l_count
) -
2644 le16_to_cpu(el
->l_next_free_rec
)) *
2645 sizeof(struct ocfs2_extent_rec
);
2647 if (free
>= xattrsize
)
2654 * ocfs2_xattr_ibody_find()
2656 * Find extended attribute in inode block and
2657 * fill search info into struct ocfs2_xattr_search.
2659 static int ocfs2_xattr_ibody_find(struct inode
*inode
,
2662 struct ocfs2_xattr_search
*xs
)
2664 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2665 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2669 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
2672 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
2673 down_read(&oi
->ip_alloc_sem
);
2674 has_space
= ocfs2_xattr_has_space_inline(inode
, di
);
2675 up_read(&oi
->ip_alloc_sem
);
2680 xs
->xattr_bh
= xs
->inode_bh
;
2681 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
2682 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)
2683 xs
->header
= (struct ocfs2_xattr_header
*)
2684 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
2686 xs
->header
= (struct ocfs2_xattr_header
*)
2687 (xs
->end
- OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
);
2688 xs
->base
= (void *)xs
->header
;
2689 xs
->here
= xs
->header
->xh_entries
;
2691 /* Find the named attribute. */
2692 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
2693 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
2694 if (ret
&& ret
!= -ENODATA
)
2696 xs
->not_found
= ret
;
2702 static int ocfs2_xattr_ibody_init(struct inode
*inode
,
2703 struct buffer_head
*di_bh
,
2704 struct ocfs2_xattr_set_ctxt
*ctxt
)
2707 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2708 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2709 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2710 unsigned int xattrsize
= osb
->s_xattr_inline_size
;
2712 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
2717 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
), di_bh
,
2718 OCFS2_JOURNAL_ACCESS_WRITE
);
2725 * Adjust extent record count or inline data size
2726 * to reserve space for extended attribute.
2728 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
2729 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
2730 le16_add_cpu(&idata
->id_count
, -xattrsize
);
2731 } else if (!(ocfs2_inode_is_fast_symlink(inode
))) {
2732 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
2733 le16_add_cpu(&el
->l_count
, -(xattrsize
/
2734 sizeof(struct ocfs2_extent_rec
)));
2736 di
->i_xattr_inline_size
= cpu_to_le16(xattrsize
);
2738 spin_lock(&oi
->ip_lock
);
2739 oi
->ip_dyn_features
|= OCFS2_INLINE_XATTR_FL
|OCFS2_HAS_XATTR_FL
;
2740 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
2741 spin_unlock(&oi
->ip_lock
);
2743 ret
= ocfs2_journal_dirty(ctxt
->handle
, di_bh
);
2752 * ocfs2_xattr_ibody_set()
2754 * Set, replace or remove an extended attribute into inode block.
2757 static int ocfs2_xattr_ibody_set(struct inode
*inode
,
2758 struct ocfs2_xattr_info
*xi
,
2759 struct ocfs2_xattr_search
*xs
,
2760 struct ocfs2_xattr_set_ctxt
*ctxt
)
2763 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2764 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2765 struct ocfs2_xa_loc loc
;
2767 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
2770 down_write(&oi
->ip_alloc_sem
);
2771 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
2772 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
2778 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
2779 ret
= ocfs2_xattr_ibody_init(inode
, xs
->inode_bh
, ctxt
);
2787 ocfs2_init_dinode_xa_loc(&loc
, inode
, xs
->inode_bh
,
2788 xs
->not_found
? NULL
: xs
->here
);
2789 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
2795 xs
->here
= loc
.xl_entry
;
2798 up_write(&oi
->ip_alloc_sem
);
2804 * ocfs2_xattr_block_find()
2806 * Find extended attribute in external block and
2807 * fill search info into struct ocfs2_xattr_search.
2809 static int ocfs2_xattr_block_find(struct inode
*inode
,
2812 struct ocfs2_xattr_search
*xs
)
2814 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2815 struct buffer_head
*blk_bh
= NULL
;
2816 struct ocfs2_xattr_block
*xb
;
2819 if (!di
->i_xattr_loc
)
2822 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
2829 xs
->xattr_bh
= blk_bh
;
2830 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2832 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2833 xs
->header
= &xb
->xb_attrs
.xb_header
;
2834 xs
->base
= (void *)xs
->header
;
2835 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
2836 xs
->here
= xs
->header
->xh_entries
;
2838 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
2840 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
2844 if (ret
&& ret
!= -ENODATA
) {
2845 xs
->xattr_bh
= NULL
;
2848 xs
->not_found
= ret
;
2856 static int ocfs2_create_xattr_block(struct inode
*inode
,
2857 struct buffer_head
*inode_bh
,
2858 struct ocfs2_xattr_set_ctxt
*ctxt
,
2860 struct buffer_head
**ret_bh
)
2863 u16 suballoc_bit_start
;
2866 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)inode_bh
->b_data
;
2867 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2868 struct buffer_head
*new_bh
= NULL
;
2869 struct ocfs2_xattr_block
*xblk
;
2871 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
),
2872 inode_bh
, OCFS2_JOURNAL_ACCESS_CREATE
);
2878 ret
= ocfs2_claim_metadata(osb
, ctxt
->handle
, ctxt
->meta_ac
, 1,
2879 &suballoc_bit_start
, &num_got
,
2886 new_bh
= sb_getblk(inode
->i_sb
, first_blkno
);
2887 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode
), new_bh
);
2889 ret
= ocfs2_journal_access_xb(ctxt
->handle
, INODE_CACHE(inode
),
2891 OCFS2_JOURNAL_ACCESS_CREATE
);
2897 /* Initialize ocfs2_xattr_block */
2898 xblk
= (struct ocfs2_xattr_block
*)new_bh
->b_data
;
2899 memset(xblk
, 0, inode
->i_sb
->s_blocksize
);
2900 strcpy((void *)xblk
, OCFS2_XATTR_BLOCK_SIGNATURE
);
2901 xblk
->xb_suballoc_slot
= cpu_to_le16(ctxt
->meta_ac
->ac_alloc_slot
);
2902 xblk
->xb_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
2903 xblk
->xb_fs_generation
= cpu_to_le32(osb
->fs_generation
);
2904 xblk
->xb_blkno
= cpu_to_le64(first_blkno
);
2906 struct ocfs2_xattr_tree_root
*xr
= &xblk
->xb_attrs
.xb_root
;
2907 xr
->xt_clusters
= cpu_to_le32(1);
2908 xr
->xt_last_eb_blk
= 0;
2909 xr
->xt_list
.l_tree_depth
= 0;
2910 xr
->xt_list
.l_count
= cpu_to_le16(
2911 ocfs2_xattr_recs_per_xb(inode
->i_sb
));
2912 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
2913 xblk
->xb_flags
= cpu_to_le16(OCFS2_XATTR_INDEXED
);
2915 ocfs2_journal_dirty(ctxt
->handle
, new_bh
);
2917 /* Add it to the inode */
2918 di
->i_xattr_loc
= cpu_to_le64(first_blkno
);
2920 spin_lock(&OCFS2_I(inode
)->ip_lock
);
2921 OCFS2_I(inode
)->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
;
2922 di
->i_dyn_features
= cpu_to_le16(OCFS2_I(inode
)->ip_dyn_features
);
2923 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
2925 ocfs2_journal_dirty(ctxt
->handle
, inode_bh
);
2936 * ocfs2_xattr_block_set()
2938 * Set, replace or remove an extended attribute into external block.
2941 static int ocfs2_xattr_block_set(struct inode
*inode
,
2942 struct ocfs2_xattr_info
*xi
,
2943 struct ocfs2_xattr_search
*xs
,
2944 struct ocfs2_xattr_set_ctxt
*ctxt
)
2946 struct buffer_head
*new_bh
= NULL
;
2947 struct ocfs2_xattr_block
*xblk
= NULL
;
2949 struct ocfs2_xa_loc loc
;
2951 if (!xs
->xattr_bh
) {
2952 ret
= ocfs2_create_xattr_block(inode
, xs
->inode_bh
, ctxt
,
2959 xs
->xattr_bh
= new_bh
;
2960 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
2961 xs
->header
= &xblk
->xb_attrs
.xb_header
;
2962 xs
->base
= (void *)xs
->header
;
2963 xs
->end
= (void *)xblk
+ inode
->i_sb
->s_blocksize
;
2964 xs
->here
= xs
->header
->xh_entries
;
2966 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
2968 if (!(le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2969 ocfs2_init_xattr_block_xa_loc(&loc
, inode
, xs
->xattr_bh
,
2970 xs
->not_found
? NULL
: xs
->here
);
2972 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
2974 xs
->here
= loc
.xl_entry
;
2975 else if (ret
!= -ENOSPC
)
2978 ret
= ocfs2_xattr_create_index_block(inode
, xs
, ctxt
);
2984 if (le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)
2985 ret
= ocfs2_xattr_set_entry_index_block(inode
, xi
, xs
, ctxt
);
2991 /* Check whether the new xattr can be inserted into the inode. */
2992 static int ocfs2_xattr_can_be_in_inode(struct inode
*inode
,
2993 struct ocfs2_xattr_info
*xi
,
2994 struct ocfs2_xattr_search
*xs
)
2996 struct ocfs2_xattr_entry
*last
;
2998 size_t min_offs
= xs
->end
- xs
->base
;
3003 last
= xs
->header
->xh_entries
;
3005 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
3006 size_t offs
= le16_to_cpu(last
->xe_name_offset
);
3007 if (offs
< min_offs
)
3012 free
= min_offs
- ((void *)last
- xs
->base
) - OCFS2_XATTR_HEADER_GAP
;
3016 BUG_ON(!xs
->not_found
);
3018 if (free
>= (sizeof(struct ocfs2_xattr_entry
) + namevalue_size_xi(xi
)))
3024 static int ocfs2_calc_xattr_set_need(struct inode
*inode
,
3025 struct ocfs2_dinode
*di
,
3026 struct ocfs2_xattr_info
*xi
,
3027 struct ocfs2_xattr_search
*xis
,
3028 struct ocfs2_xattr_search
*xbs
,
3033 int ret
= 0, old_in_xb
= 0;
3034 int clusters_add
= 0, meta_add
= 0, credits
= 0;
3035 struct buffer_head
*bh
= NULL
;
3036 struct ocfs2_xattr_block
*xb
= NULL
;
3037 struct ocfs2_xattr_entry
*xe
= NULL
;
3038 struct ocfs2_xattr_value_root
*xv
= NULL
;
3040 int name_offset
, name_len
= 0;
3041 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
3046 * Calculate the clusters we need to write.
3047 * No matter whether we replace an old one or add a new one,
3048 * we need this for writing.
3050 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
)
3051 credits
+= new_clusters
*
3052 ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3054 if (xis
->not_found
&& xbs
->not_found
) {
3055 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3057 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
3058 clusters_add
+= new_clusters
;
3059 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3067 if (!xis
->not_found
) {
3069 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3070 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3072 credits
+= OCFS2_INODE_UPDATE_CREDITS
;
3074 int i
, block_off
= 0;
3075 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
3077 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3078 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3079 i
= xbs
->here
- xbs
->header
->xh_entries
;
3082 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
3083 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
3084 bucket_xh(xbs
->bucket
),
3087 base
= bucket_block(xbs
->bucket
, block_off
);
3088 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3091 credits
+= OCFS2_XATTR_BLOCK_UPDATE_CREDITS
;
3096 * delete a xattr doesn't need metadata and cluster allocation.
3097 * so just calculate the credits and return.
3099 * The credits for removing the value tree will be extended
3100 * by ocfs2_remove_extent itself.
3102 if (!xi
->xi_value
) {
3103 if (!ocfs2_xattr_is_local(xe
))
3104 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
);
3109 /* do cluster allocation guess first. */
3110 value_size
= le64_to_cpu(xe
->xe_value_size
);
3114 * In xattr set, we always try to set the xe in inode first,
3115 * so if it can be inserted into inode successfully, the old
3116 * one will be removed from the xattr block, and this xattr
3117 * will be inserted into inode as a new xattr in inode.
3119 if (ocfs2_xattr_can_be_in_inode(inode
, xi
, xis
)) {
3120 clusters_add
+= new_clusters
;
3121 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
) +
3122 OCFS2_INODE_UPDATE_CREDITS
;
3123 if (!ocfs2_xattr_is_local(xe
))
3124 credits
+= ocfs2_calc_extend_credits(
3132 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
3133 /* the new values will be stored outside. */
3134 u32 old_clusters
= 0;
3136 if (!ocfs2_xattr_is_local(xe
)) {
3137 old_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
3139 xv
= (struct ocfs2_xattr_value_root
*)
3140 (base
+ name_offset
+ name_len
);
3141 value_size
= OCFS2_XATTR_ROOT_SIZE
;
3145 if (old_clusters
>= new_clusters
) {
3146 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
);
3149 meta_add
+= ocfs2_extend_meta_needed(&xv
->xr_list
);
3150 clusters_add
+= new_clusters
- old_clusters
;
3151 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3155 if (value_size
>= OCFS2_XATTR_ROOT_SIZE
)
3160 * Now the new value will be stored inside. So if the new
3161 * value is smaller than the size of value root or the old
3162 * value, we don't need any allocation, otherwise we have
3163 * to guess metadata allocation.
3165 if ((ocfs2_xattr_is_local(xe
) &&
3166 (value_size
>= xi
->xi_value_len
)) ||
3167 (!ocfs2_xattr_is_local(xe
) &&
3168 OCFS2_XATTR_ROOT_SIZE
>= xi
->xi_value_len
))
3173 /* calculate metadata allocation. */
3174 if (di
->i_xattr_loc
) {
3175 if (!xbs
->xattr_bh
) {
3176 ret
= ocfs2_read_xattr_block(inode
,
3177 le64_to_cpu(di
->i_xattr_loc
),
3184 xb
= (struct ocfs2_xattr_block
*)bh
->b_data
;
3186 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
3189 * If there is already an xattr tree, good, we can calculate
3190 * like other b-trees. Otherwise we may have the chance of
3191 * create a tree, the credit calculation is borrowed from
3192 * ocfs2_calc_extend_credits with root_el = NULL. And the
3193 * new tree will be cluster based, so no meta is needed.
3195 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
3196 struct ocfs2_extent_list
*el
=
3197 &xb
->xb_attrs
.xb_root
.xt_list
;
3198 meta_add
+= ocfs2_extend_meta_needed(el
);
3199 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3202 credits
+= OCFS2_SUBALLOC_ALLOC
+ 1;
3205 * This cluster will be used either for new bucket or for
3207 * If the cluster size is the same as the bucket size, one
3208 * more is needed since we may need to extend the bucket
3212 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3213 if (OCFS2_XATTR_BUCKET_SIZE
==
3214 OCFS2_SB(inode
->i_sb
)->s_clustersize
) {
3215 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3220 credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
3224 *clusters_need
= clusters_add
;
3226 *meta_need
= meta_add
;
3228 *credits_need
= credits
;
3233 static int ocfs2_init_xattr_set_ctxt(struct inode
*inode
,
3234 struct ocfs2_dinode
*di
,
3235 struct ocfs2_xattr_info
*xi
,
3236 struct ocfs2_xattr_search
*xis
,
3237 struct ocfs2_xattr_search
*xbs
,
3238 struct ocfs2_xattr_set_ctxt
*ctxt
,
3242 int clusters_add
, meta_add
, ret
;
3243 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3245 memset(ctxt
, 0, sizeof(struct ocfs2_xattr_set_ctxt
));
3247 ocfs2_init_dealloc_ctxt(&ctxt
->dealloc
);
3249 ret
= ocfs2_calc_xattr_set_need(inode
, di
, xi
, xis
, xbs
,
3250 &clusters_add
, &meta_add
, credits
);
3256 meta_add
+= extra_meta
;
3257 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
3258 "credits = %d\n", xi
->xi_name
, meta_add
, clusters_add
, *credits
);
3261 ret
= ocfs2_reserve_new_metadata_blocks(osb
, meta_add
,
3270 ret
= ocfs2_reserve_clusters(osb
, clusters_add
, &ctxt
->data_ac
);
3276 if (ctxt
->meta_ac
) {
3277 ocfs2_free_alloc_context(ctxt
->meta_ac
);
3278 ctxt
->meta_ac
= NULL
;
3282 * We cannot have an error and a non null ctxt->data_ac.
3289 static int __ocfs2_xattr_set_handle(struct inode
*inode
,
3290 struct ocfs2_dinode
*di
,
3291 struct ocfs2_xattr_info
*xi
,
3292 struct ocfs2_xattr_search
*xis
,
3293 struct ocfs2_xattr_search
*xbs
,
3294 struct ocfs2_xattr_set_ctxt
*ctxt
)
3296 int ret
= 0, credits
, old_found
;
3298 if (!xi
->xi_value
) {
3299 /* Remove existing extended attribute */
3300 if (!xis
->not_found
)
3301 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
3302 else if (!xbs
->not_found
)
3303 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3305 /* We always try to set extended attribute into inode first*/
3306 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
3307 if (!ret
&& !xbs
->not_found
) {
3309 * If succeed and that extended attribute existing in
3310 * external block, then we will remove it.
3312 xi
->xi_value
= NULL
;
3313 xi
->xi_value_len
= 0;
3315 old_found
= xis
->not_found
;
3316 xis
->not_found
= -ENODATA
;
3317 ret
= ocfs2_calc_xattr_set_need(inode
,
3325 xis
->not_found
= old_found
;
3331 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
+
3332 ctxt
->handle
->h_buffer_credits
);
3337 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3338 } else if (ret
== -ENOSPC
) {
3339 if (di
->i_xattr_loc
&& !xbs
->xattr_bh
) {
3340 ret
= ocfs2_xattr_block_find(inode
,
3346 old_found
= xis
->not_found
;
3347 xis
->not_found
= -ENODATA
;
3348 ret
= ocfs2_calc_xattr_set_need(inode
,
3356 xis
->not_found
= old_found
;
3362 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
+
3363 ctxt
->handle
->h_buffer_credits
);
3370 * If no space in inode, we will set extended attribute
3371 * into external block.
3373 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3376 if (!xis
->not_found
) {
3378 * If succeed and that extended attribute
3379 * existing in inode, we will remove it.
3381 xi
->xi_value
= NULL
;
3382 xi
->xi_value_len
= 0;
3383 xbs
->not_found
= -ENODATA
;
3384 ret
= ocfs2_calc_xattr_set_need(inode
,
3397 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
+
3398 ctxt
->handle
->h_buffer_credits
);
3403 ret
= ocfs2_xattr_ibody_set(inode
, xi
,
3410 /* Update inode ctime. */
3411 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
),
3413 OCFS2_JOURNAL_ACCESS_WRITE
);
3419 inode
->i_ctime
= CURRENT_TIME
;
3420 di
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
3421 di
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
3422 ocfs2_journal_dirty(ctxt
->handle
, xis
->inode_bh
);
3429 * This function only called duing creating inode
3430 * for init security/acl xattrs of the new inode.
3431 * All transanction credits have been reserved in mknod.
3433 int ocfs2_xattr_set_handle(handle_t
*handle
,
3434 struct inode
*inode
,
3435 struct buffer_head
*di_bh
,
3441 struct ocfs2_alloc_context
*meta_ac
,
3442 struct ocfs2_alloc_context
*data_ac
)
3444 struct ocfs2_dinode
*di
;
3447 struct ocfs2_xattr_info xi
= {
3448 .xi_name_index
= name_index
,
3450 .xi_name_len
= strlen(name
),
3452 .xi_value_len
= value_len
,
3455 struct ocfs2_xattr_search xis
= {
3456 .not_found
= -ENODATA
,
3459 struct ocfs2_xattr_search xbs
= {
3460 .not_found
= -ENODATA
,
3463 struct ocfs2_xattr_set_ctxt ctxt
= {
3469 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
3473 * In extreme situation, may need xattr bucket when
3474 * block size is too small. And we have already reserved
3475 * the credits for bucket in mknod.
3477 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
) {
3478 xbs
.bucket
= ocfs2_xattr_bucket_new(inode
);
3480 mlog_errno(-ENOMEM
);
3485 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
3486 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3488 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3490 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
3493 if (xis
.not_found
) {
3494 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
3499 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
3502 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3503 brelse(xbs
.xattr_bh
);
3504 ocfs2_xattr_bucket_free(xbs
.bucket
);
3512 * Set, replace or remove an extended attribute for this inode.
3513 * value is NULL to remove an existing extended attribute, else either
3514 * create or replace an extended attribute.
3516 int ocfs2_xattr_set(struct inode
*inode
,
3523 struct buffer_head
*di_bh
= NULL
;
3524 struct ocfs2_dinode
*di
;
3525 int ret
, credits
, ref_meta
= 0, ref_credits
= 0;
3526 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3527 struct inode
*tl_inode
= osb
->osb_tl_inode
;
3528 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, };
3529 struct ocfs2_refcount_tree
*ref_tree
= NULL
;
3531 struct ocfs2_xattr_info xi
= {
3532 .xi_name_index
= name_index
,
3534 .xi_name_len
= strlen(name
),
3536 .xi_value_len
= value_len
,
3539 struct ocfs2_xattr_search xis
= {
3540 .not_found
= -ENODATA
,
3543 struct ocfs2_xattr_search xbs
= {
3544 .not_found
= -ENODATA
,
3547 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
3551 * Only xbs will be used on indexed trees. xis doesn't need a
3554 xbs
.bucket
= ocfs2_xattr_bucket_new(inode
);
3556 mlog_errno(-ENOMEM
);
3560 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
3563 goto cleanup_nolock
;
3565 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
3566 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3568 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3570 * Scan inode and external block to find the same name
3571 * extended attribute and collect search infomation.
3573 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
3576 if (xis
.not_found
) {
3577 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
3582 if (xis
.not_found
&& xbs
.not_found
) {
3584 if (flags
& XATTR_REPLACE
)
3591 if (flags
& XATTR_CREATE
)
3595 /* Check whether the value is refcounted and do some prepartion. */
3596 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
&&
3597 (!xis
.not_found
|| !xbs
.not_found
)) {
3598 ret
= ocfs2_prepare_refcount_xattr(inode
, di
, &xi
,
3599 &xis
, &xbs
, &ref_tree
,
3600 &ref_meta
, &ref_credits
);
3607 mutex_lock(&tl_inode
->i_mutex
);
3609 if (ocfs2_truncate_log_needs_flush(osb
)) {
3610 ret
= __ocfs2_flush_truncate_log(osb
);
3612 mutex_unlock(&tl_inode
->i_mutex
);
3617 mutex_unlock(&tl_inode
->i_mutex
);
3619 ret
= ocfs2_init_xattr_set_ctxt(inode
, di
, &xi
, &xis
,
3620 &xbs
, &ctxt
, ref_meta
, &credits
);
3626 /* we need to update inode's ctime field, so add credit for it. */
3627 credits
+= OCFS2_INODE_UPDATE_CREDITS
;
3628 ctxt
.handle
= ocfs2_start_trans(osb
, credits
+ ref_credits
);
3629 if (IS_ERR(ctxt
.handle
)) {
3630 ret
= PTR_ERR(ctxt
.handle
);
3635 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
3637 ocfs2_commit_trans(osb
, ctxt
.handle
);
3640 ocfs2_free_alloc_context(ctxt
.data_ac
);
3642 ocfs2_free_alloc_context(ctxt
.meta_ac
);
3643 if (ocfs2_dealloc_has_cluster(&ctxt
.dealloc
))
3644 ocfs2_schedule_truncate_log_flush(osb
, 1);
3645 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
3649 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
3650 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3651 if (!value
&& !ret
) {
3652 ret
= ocfs2_try_remove_refcount_tree(inode
, di_bh
);
3656 ocfs2_inode_unlock(inode
, 1);
3659 brelse(xbs
.xattr_bh
);
3660 ocfs2_xattr_bucket_free(xbs
.bucket
);
3666 * Find the xattr extent rec which may contains name_hash.
3667 * e_cpos will be the first name hash of the xattr rec.
3668 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3670 static int ocfs2_xattr_get_rec(struct inode
*inode
,
3675 struct ocfs2_extent_list
*el
)
3678 struct buffer_head
*eb_bh
= NULL
;
3679 struct ocfs2_extent_block
*eb
;
3680 struct ocfs2_extent_rec
*rec
= NULL
;
3683 if (el
->l_tree_depth
) {
3684 ret
= ocfs2_find_leaf(INODE_CACHE(inode
), el
, name_hash
,
3691 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
3694 if (el
->l_tree_depth
) {
3695 ocfs2_error(inode
->i_sb
,
3696 "Inode %lu has non zero tree depth in "
3697 "xattr tree block %llu\n", inode
->i_ino
,
3698 (unsigned long long)eb_bh
->b_blocknr
);
3704 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
3705 rec
= &el
->l_recs
[i
];
3707 if (le32_to_cpu(rec
->e_cpos
) <= name_hash
) {
3708 e_blkno
= le64_to_cpu(rec
->e_blkno
);
3714 ocfs2_error(inode
->i_sb
, "Inode %lu has bad extent "
3715 "record (%u, %u, 0) in xattr", inode
->i_ino
,
3716 le32_to_cpu(rec
->e_cpos
),
3717 ocfs2_rec_clusters(el
, rec
));
3722 *p_blkno
= le64_to_cpu(rec
->e_blkno
);
3723 *num_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
3725 *e_cpos
= le32_to_cpu(rec
->e_cpos
);
3731 typedef int (xattr_bucket_func
)(struct inode
*inode
,
3732 struct ocfs2_xattr_bucket
*bucket
,
3735 static int ocfs2_find_xe_in_bucket(struct inode
*inode
,
3736 struct ocfs2_xattr_bucket
*bucket
,
3743 int i
, ret
= 0, cmp
= 1, block_off
, new_offset
;
3744 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
3745 size_t name_len
= strlen(name
);
3746 struct ocfs2_xattr_entry
*xe
= NULL
;
3750 * We don't use binary search in the bucket because there
3751 * may be multiple entries with the same name hash.
3753 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
3754 xe
= &xh
->xh_entries
[i
];
3756 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
))
3758 else if (name_hash
< le32_to_cpu(xe
->xe_name_hash
))
3761 cmp
= name_index
- ocfs2_xattr_get_type(xe
);
3763 cmp
= name_len
- xe
->xe_name_len
;
3767 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
3778 xe_name
= bucket_block(bucket
, block_off
) + new_offset
;
3779 if (!memcmp(name
, xe_name
, name_len
)) {
3791 * Find the specified xattr entry in a series of buckets.
3792 * This series start from p_blkno and last for num_clusters.
3793 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3794 * the num of the valid buckets.
3796 * Return the buffer_head this xattr should reside in. And if the xattr's
3797 * hash is in the gap of 2 buckets, return the lower bucket.
3799 static int ocfs2_xattr_bucket_find(struct inode
*inode
,
3806 struct ocfs2_xattr_search
*xs
)
3809 struct ocfs2_xattr_header
*xh
= NULL
;
3810 struct ocfs2_xattr_entry
*xe
= NULL
;
3812 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3813 int low_bucket
= 0, bucket
, high_bucket
;
3814 struct ocfs2_xattr_bucket
*search
;
3816 u64 blkno
, lower_blkno
= 0;
3818 search
= ocfs2_xattr_bucket_new(inode
);
3825 ret
= ocfs2_read_xattr_bucket(search
, p_blkno
);
3831 xh
= bucket_xh(search
);
3832 high_bucket
= le16_to_cpu(xh
->xh_num_buckets
) - 1;
3833 while (low_bucket
<= high_bucket
) {
3834 ocfs2_xattr_bucket_relse(search
);
3836 bucket
= (low_bucket
+ high_bucket
) / 2;
3837 blkno
= p_blkno
+ bucket
* blk_per_bucket
;
3838 ret
= ocfs2_read_xattr_bucket(search
, blkno
);
3844 xh
= bucket_xh(search
);
3845 xe
= &xh
->xh_entries
[0];
3846 if (name_hash
< le32_to_cpu(xe
->xe_name_hash
)) {
3847 high_bucket
= bucket
- 1;
3852 * Check whether the hash of the last entry in our
3853 * bucket is larger than the search one. for an empty
3854 * bucket, the last one is also the first one.
3857 xe
= &xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1];
3859 last_hash
= le32_to_cpu(xe
->xe_name_hash
);
3861 /* record lower_blkno which may be the insert place. */
3862 lower_blkno
= blkno
;
3864 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
)) {
3865 low_bucket
= bucket
+ 1;
3869 /* the searched xattr should reside in this bucket if exists. */
3870 ret
= ocfs2_find_xe_in_bucket(inode
, search
,
3871 name_index
, name
, name_hash
,
3881 * Record the bucket we have found.
3882 * When the xattr's hash value is in the gap of 2 buckets, we will
3883 * always set it to the previous bucket.
3886 lower_blkno
= p_blkno
;
3888 /* This should be in cache - we just read it during the search */
3889 ret
= ocfs2_read_xattr_bucket(xs
->bucket
, lower_blkno
);
3895 xs
->header
= bucket_xh(xs
->bucket
);
3896 xs
->base
= bucket_block(xs
->bucket
, 0);
3897 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
3900 xs
->here
= &xs
->header
->xh_entries
[index
];
3901 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name
,
3902 (unsigned long long)bucket_blkno(xs
->bucket
), index
);
3907 ocfs2_xattr_bucket_free(search
);
3911 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
3912 struct buffer_head
*root_bh
,
3915 struct ocfs2_xattr_search
*xs
)
3918 struct ocfs2_xattr_block
*xb
=
3919 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
3920 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
3921 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
3923 u32 first_hash
, num_clusters
= 0;
3924 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
3926 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
3929 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3930 name
, name_hash
, name_index
);
3932 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &first_hash
,
3939 BUG_ON(p_blkno
== 0 || num_clusters
== 0 || first_hash
> name_hash
);
3941 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3942 "in the rec is %u\n", num_clusters
, (unsigned long long)p_blkno
,
3945 ret
= ocfs2_xattr_bucket_find(inode
, name_index
, name
, name_hash
,
3946 p_blkno
, first_hash
, num_clusters
, xs
);
3952 static int ocfs2_iterate_xattr_buckets(struct inode
*inode
,
3955 xattr_bucket_func
*func
,
3959 u32 bpc
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
3960 u32 num_buckets
= clusters
* bpc
;
3961 struct ocfs2_xattr_bucket
*bucket
;
3963 bucket
= ocfs2_xattr_bucket_new(inode
);
3965 mlog_errno(-ENOMEM
);
3969 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3970 clusters
, (unsigned long long)blkno
);
3972 for (i
= 0; i
< num_buckets
; i
++, blkno
+= bucket
->bu_blocks
) {
3973 ret
= ocfs2_read_xattr_bucket(bucket
, blkno
);
3980 * The real bucket num in this series of blocks is stored
3981 * in the 1st bucket.
3984 num_buckets
= le16_to_cpu(bucket_xh(bucket
)->xh_num_buckets
);
3986 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3987 (unsigned long long)blkno
,
3988 le32_to_cpu(bucket_xh(bucket
)->xh_entries
[0].xe_name_hash
));
3990 ret
= func(inode
, bucket
, para
);
3991 if (ret
&& ret
!= -ERANGE
)
3993 /* Fall through to bucket_relse() */
3996 ocfs2_xattr_bucket_relse(bucket
);
4001 ocfs2_xattr_bucket_free(bucket
);
4005 struct ocfs2_xattr_tree_list
{
4011 static int ocfs2_xattr_bucket_get_name_value(struct super_block
*sb
,
4012 struct ocfs2_xattr_header
*xh
,
4019 if (index
< 0 || index
>= le16_to_cpu(xh
->xh_count
))
4022 name_offset
= le16_to_cpu(xh
->xh_entries
[index
].xe_name_offset
);
4024 *block_off
= name_offset
>> sb
->s_blocksize_bits
;
4025 *new_offset
= name_offset
% sb
->s_blocksize
;
4030 static int ocfs2_list_xattr_bucket(struct inode
*inode
,
4031 struct ocfs2_xattr_bucket
*bucket
,
4035 struct ocfs2_xattr_tree_list
*xl
= (struct ocfs2_xattr_tree_list
*)para
;
4036 int i
, block_off
, new_offset
;
4037 const char *prefix
, *name
;
4039 for (i
= 0 ; i
< le16_to_cpu(bucket_xh(bucket
)->xh_count
); i
++) {
4040 struct ocfs2_xattr_entry
*entry
= &bucket_xh(bucket
)->xh_entries
[i
];
4041 type
= ocfs2_xattr_get_type(entry
);
4042 prefix
= ocfs2_xattr_prefix(type
);
4045 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
4053 name
= (const char *)bucket_block(bucket
, block_off
) +
4055 ret
= ocfs2_xattr_list_entry(xl
->buffer
,
4059 entry
->xe_name_len
);
4068 static int ocfs2_iterate_xattr_index_block(struct inode
*inode
,
4069 struct buffer_head
*blk_bh
,
4070 xattr_tree_rec_func
*rec_func
,
4073 struct ocfs2_xattr_block
*xb
=
4074 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
4075 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
4077 u32 name_hash
= UINT_MAX
, e_cpos
= 0, num_clusters
= 0;
4080 if (!el
->l_next_free_rec
|| !rec_func
)
4083 while (name_hash
> 0) {
4084 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
4085 &e_cpos
, &num_clusters
, el
);
4091 ret
= rec_func(inode
, blk_bh
, p_blkno
, e_cpos
,
4092 num_clusters
, para
);
4102 name_hash
= e_cpos
- 1;
4109 static int ocfs2_list_xattr_tree_rec(struct inode
*inode
,
4110 struct buffer_head
*root_bh
,
4111 u64 blkno
, u32 cpos
, u32 len
, void *para
)
4113 return ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
4114 ocfs2_list_xattr_bucket
, para
);
4117 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
4118 struct buffer_head
*blk_bh
,
4123 struct ocfs2_xattr_tree_list xl
= {
4125 .buffer_size
= buffer_size
,
4129 ret
= ocfs2_iterate_xattr_index_block(inode
, blk_bh
,
4130 ocfs2_list_xattr_tree_rec
, &xl
);
4141 static int cmp_xe(const void *a
, const void *b
)
4143 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
4144 u32 l_hash
= le32_to_cpu(l
->xe_name_hash
);
4145 u32 r_hash
= le32_to_cpu(r
->xe_name_hash
);
4147 if (l_hash
> r_hash
)
4149 if (l_hash
< r_hash
)
4154 static void swap_xe(void *a
, void *b
, int size
)
4156 struct ocfs2_xattr_entry
*l
= a
, *r
= b
, tmp
;
4159 memcpy(l
, r
, sizeof(struct ocfs2_xattr_entry
));
4160 memcpy(r
, &tmp
, sizeof(struct ocfs2_xattr_entry
));
4164 * When the ocfs2_xattr_block is filled up, new bucket will be created
4165 * and all the xattr entries will be moved to the new bucket.
4166 * The header goes at the start of the bucket, and the names+values are
4167 * filled from the end. This is why *target starts as the last buffer.
4168 * Note: we need to sort the entries since they are not saved in order
4169 * in the ocfs2_xattr_block.
4171 static void ocfs2_cp_xattr_block_to_bucket(struct inode
*inode
,
4172 struct buffer_head
*xb_bh
,
4173 struct ocfs2_xattr_bucket
*bucket
)
4175 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
4176 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4177 u16 offset
, size
, off_change
;
4178 struct ocfs2_xattr_entry
*xe
;
4179 struct ocfs2_xattr_block
*xb
=
4180 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4181 struct ocfs2_xattr_header
*xb_xh
= &xb
->xb_attrs
.xb_header
;
4182 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
4183 u16 count
= le16_to_cpu(xb_xh
->xh_count
);
4184 char *src
= xb_bh
->b_data
;
4185 char *target
= bucket_block(bucket
, blks
- 1);
4187 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4188 (unsigned long long)xb_bh
->b_blocknr
,
4189 (unsigned long long)bucket_blkno(bucket
));
4191 for (i
= 0; i
< blks
; i
++)
4192 memset(bucket_block(bucket
, i
), 0, blocksize
);
4195 * Since the xe_name_offset is based on ocfs2_xattr_header,
4196 * there is a offset change corresponding to the change of
4197 * ocfs2_xattr_header's position.
4199 off_change
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
4200 xe
= &xb_xh
->xh_entries
[count
- 1];
4201 offset
= le16_to_cpu(xe
->xe_name_offset
) + off_change
;
4202 size
= blocksize
- offset
;
4204 /* copy all the names and values. */
4205 memcpy(target
+ offset
, src
+ offset
, size
);
4207 /* Init new header now. */
4208 xh
->xh_count
= xb_xh
->xh_count
;
4209 xh
->xh_num_buckets
= cpu_to_le16(1);
4210 xh
->xh_name_value_len
= cpu_to_le16(size
);
4211 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
- size
);
4213 /* copy all the entries. */
4214 target
= bucket_block(bucket
, 0);
4215 offset
= offsetof(struct ocfs2_xattr_header
, xh_entries
);
4216 size
= count
* sizeof(struct ocfs2_xattr_entry
);
4217 memcpy(target
+ offset
, (char *)xb_xh
+ offset
, size
);
4219 /* Change the xe offset for all the xe because of the move. */
4220 off_change
= OCFS2_XATTR_BUCKET_SIZE
- blocksize
+
4221 offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
4222 for (i
= 0; i
< count
; i
++)
4223 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
, off_change
);
4225 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4226 offset
, size
, off_change
);
4228 sort(target
+ offset
, count
, sizeof(struct ocfs2_xattr_entry
),
4233 * After we move xattr from block to index btree, we have to
4234 * update ocfs2_xattr_search to the new xe and base.
4236 * When the entry is in xattr block, xattr_bh indicates the storage place.
4237 * While if the entry is in index b-tree, "bucket" indicates the
4238 * real place of the xattr.
4240 static void ocfs2_xattr_update_xattr_search(struct inode
*inode
,
4241 struct ocfs2_xattr_search
*xs
,
4242 struct buffer_head
*old_bh
)
4244 char *buf
= old_bh
->b_data
;
4245 struct ocfs2_xattr_block
*old_xb
= (struct ocfs2_xattr_block
*)buf
;
4246 struct ocfs2_xattr_header
*old_xh
= &old_xb
->xb_attrs
.xb_header
;
4249 xs
->header
= bucket_xh(xs
->bucket
);
4250 xs
->base
= bucket_block(xs
->bucket
, 0);
4251 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
4256 i
= xs
->here
- old_xh
->xh_entries
;
4257 xs
->here
= &xs
->header
->xh_entries
[i
];
4260 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
4261 struct ocfs2_xattr_search
*xs
,
4262 struct ocfs2_xattr_set_ctxt
*ctxt
)
4267 handle_t
*handle
= ctxt
->handle
;
4268 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4269 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
4270 struct buffer_head
*xb_bh
= xs
->xattr_bh
;
4271 struct ocfs2_xattr_block
*xb
=
4272 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4273 struct ocfs2_xattr_tree_root
*xr
;
4274 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
4276 mlog(0, "create xattr index block for %llu\n",
4277 (unsigned long long)xb_bh
->b_blocknr
);
4279 BUG_ON(xb_flags
& OCFS2_XATTR_INDEXED
);
4280 BUG_ON(!xs
->bucket
);
4284 * We can use this lock for now, and maybe move to a dedicated mutex
4285 * if performance becomes a problem later.
4287 down_write(&oi
->ip_alloc_sem
);
4289 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), xb_bh
,
4290 OCFS2_JOURNAL_ACCESS_WRITE
);
4296 ret
= __ocfs2_claim_clusters(osb
, handle
, ctxt
->data_ac
,
4297 1, 1, &bit_off
, &len
);
4304 * The bucket may spread in many blocks, and
4305 * we will only touch the 1st block and the last block
4306 * in the whole bucket(one for entry and one for data).
4308 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
4310 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4311 (unsigned long long)blkno
);
4313 ret
= ocfs2_init_xattr_bucket(xs
->bucket
, blkno
);
4319 ret
= ocfs2_xattr_bucket_journal_access(handle
, xs
->bucket
,
4320 OCFS2_JOURNAL_ACCESS_CREATE
);
4326 ocfs2_cp_xattr_block_to_bucket(inode
, xb_bh
, xs
->bucket
);
4327 ocfs2_xattr_bucket_journal_dirty(handle
, xs
->bucket
);
4329 ocfs2_xattr_update_xattr_search(inode
, xs
, xb_bh
);
4331 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4332 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
4333 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
4335 xr
= &xb
->xb_attrs
.xb_root
;
4336 xr
->xt_clusters
= cpu_to_le32(1);
4337 xr
->xt_last_eb_blk
= 0;
4338 xr
->xt_list
.l_tree_depth
= 0;
4339 xr
->xt_list
.l_count
= cpu_to_le16(ocfs2_xattr_recs_per_xb(inode
->i_sb
));
4340 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
4342 xr
->xt_list
.l_recs
[0].e_cpos
= 0;
4343 xr
->xt_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
4344 xr
->xt_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
4346 xb
->xb_flags
= cpu_to_le16(xb_flags
| OCFS2_XATTR_INDEXED
);
4348 ocfs2_journal_dirty(handle
, xb_bh
);
4351 up_write(&oi
->ip_alloc_sem
);
4356 static int cmp_xe_offset(const void *a
, const void *b
)
4358 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
4359 u32 l_name_offset
= le16_to_cpu(l
->xe_name_offset
);
4360 u32 r_name_offset
= le16_to_cpu(r
->xe_name_offset
);
4362 if (l_name_offset
< r_name_offset
)
4364 if (l_name_offset
> r_name_offset
)
4370 * defrag a xattr bucket if we find that the bucket has some
4371 * holes beteen name/value pairs.
4372 * We will move all the name/value pairs to the end of the bucket
4373 * so that we can spare some space for insertion.
4375 static int ocfs2_defrag_xattr_bucket(struct inode
*inode
,
4377 struct ocfs2_xattr_bucket
*bucket
)
4380 size_t end
, offset
, len
;
4381 struct ocfs2_xattr_header
*xh
;
4382 char *entries
, *buf
, *bucket_buf
= NULL
;
4383 u64 blkno
= bucket_blkno(bucket
);
4385 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4386 struct ocfs2_xattr_entry
*xe
;
4389 * In order to make the operation more efficient and generic,
4390 * we copy all the blocks into a contiguous memory and do the
4391 * defragment there, so if anything is error, we will not touch
4394 bucket_buf
= kmalloc(OCFS2_XATTR_BUCKET_SIZE
, GFP_NOFS
);
4401 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
4402 memcpy(buf
, bucket_block(bucket
, i
), blocksize
);
4404 ret
= ocfs2_xattr_bucket_journal_access(handle
, bucket
,
4405 OCFS2_JOURNAL_ACCESS_WRITE
);
4411 xh
= (struct ocfs2_xattr_header
*)bucket_buf
;
4412 entries
= (char *)xh
->xh_entries
;
4413 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4415 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4416 "xh_free_start = %u, xh_name_value_len = %u.\n",
4417 (unsigned long long)blkno
, le16_to_cpu(xh
->xh_count
),
4418 xh_free_start
, le16_to_cpu(xh
->xh_name_value_len
));
4421 * sort all the entries by their offset.
4422 * the largest will be the first, so that we can
4423 * move them to the end one by one.
4425 sort(entries
, le16_to_cpu(xh
->xh_count
),
4426 sizeof(struct ocfs2_xattr_entry
),
4427 cmp_xe_offset
, swap_xe
);
4429 /* Move all name/values to the end of the bucket. */
4430 xe
= xh
->xh_entries
;
4431 end
= OCFS2_XATTR_BUCKET_SIZE
;
4432 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, xe
++) {
4433 offset
= le16_to_cpu(xe
->xe_name_offset
);
4434 len
= namevalue_size_xe(xe
);
4437 * We must make sure that the name/value pair
4438 * exist in the same block. So adjust end to
4439 * the previous block end if needed.
4441 if (((end
- len
) / blocksize
!=
4442 (end
- 1) / blocksize
))
4443 end
= end
- end
% blocksize
;
4445 if (end
> offset
+ len
) {
4446 memmove(bucket_buf
+ end
- len
,
4447 bucket_buf
+ offset
, len
);
4448 xe
->xe_name_offset
= cpu_to_le16(end
- len
);
4451 mlog_bug_on_msg(end
< offset
+ len
, "Defrag check failed for "
4452 "bucket %llu\n", (unsigned long long)blkno
);
4457 mlog_bug_on_msg(xh_free_start
> end
, "Defrag check failed for "
4458 "bucket %llu\n", (unsigned long long)blkno
);
4460 if (xh_free_start
== end
)
4463 memset(bucket_buf
+ xh_free_start
, 0, end
- xh_free_start
);
4464 xh
->xh_free_start
= cpu_to_le16(end
);
4466 /* sort the entries by their name_hash. */
4467 sort(entries
, le16_to_cpu(xh
->xh_count
),
4468 sizeof(struct ocfs2_xattr_entry
),
4472 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
4473 memcpy(bucket_block(bucket
, i
), buf
, blocksize
);
4474 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
4482 * prev_blkno points to the start of an existing extent. new_blkno
4483 * points to a newly allocated extent. Because we know each of our
4484 * clusters contains more than bucket, we can easily split one cluster
4485 * at a bucket boundary. So we take the last cluster of the existing
4486 * extent and split it down the middle. We move the last half of the
4487 * buckets in the last cluster of the existing extent over to the new
4490 * first_bh is the buffer at prev_blkno so we can update the existing
4491 * extent's bucket count. header_bh is the bucket were we were hoping
4492 * to insert our xattr. If the bucket move places the target in the new
4493 * extent, we'll update first_bh and header_bh after modifying the old
4496 * first_hash will be set as the 1st xe's name_hash in the new extent.
4498 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode
*inode
,
4500 struct ocfs2_xattr_bucket
*first
,
4501 struct ocfs2_xattr_bucket
*target
,
4507 struct super_block
*sb
= inode
->i_sb
;
4508 int blks_per_bucket
= ocfs2_blocks_per_xattr_bucket(sb
);
4509 int num_buckets
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb
));
4510 int to_move
= num_buckets
/ 2;
4512 u64 last_cluster_blkno
= bucket_blkno(first
) +
4513 ((num_clusters
- 1) * ocfs2_clusters_to_blocks(sb
, 1));
4515 BUG_ON(le16_to_cpu(bucket_xh(first
)->xh_num_buckets
) < num_buckets
);
4516 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
== OCFS2_SB(sb
)->s_clustersize
);
4518 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4519 (unsigned long long)last_cluster_blkno
, (unsigned long long)new_blkno
);
4521 ret
= ocfs2_mv_xattr_buckets(inode
, handle
, bucket_blkno(first
),
4522 last_cluster_blkno
, new_blkno
,
4523 to_move
, first_hash
);
4529 /* This is the first bucket that got moved */
4530 src_blkno
= last_cluster_blkno
+ (to_move
* blks_per_bucket
);
4533 * If the target bucket was part of the moved buckets, we need to
4534 * update first and target.
4536 if (bucket_blkno(target
) >= src_blkno
) {
4537 /* Find the block for the new target bucket */
4538 src_blkno
= new_blkno
+
4539 (bucket_blkno(target
) - src_blkno
);
4541 ocfs2_xattr_bucket_relse(first
);
4542 ocfs2_xattr_bucket_relse(target
);
4545 * These shouldn't fail - the buffers are in the
4546 * journal from ocfs2_cp_xattr_bucket().
4548 ret
= ocfs2_read_xattr_bucket(first
, new_blkno
);
4553 ret
= ocfs2_read_xattr_bucket(target
, src_blkno
);
4564 * Find the suitable pos when we divide a bucket into 2.
4565 * We have to make sure the xattrs with the same hash value exist
4566 * in the same bucket.
4568 * If this ocfs2_xattr_header covers more than one hash value, find a
4569 * place where the hash value changes. Try to find the most even split.
4570 * The most common case is that all entries have different hash values,
4571 * and the first check we make will find a place to split.
4573 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header
*xh
)
4575 struct ocfs2_xattr_entry
*entries
= xh
->xh_entries
;
4576 int count
= le16_to_cpu(xh
->xh_count
);
4577 int delta
, middle
= count
/ 2;
4580 * We start at the middle. Each step gets farther away in both
4581 * directions. We therefore hit the change in hash value
4582 * nearest to the middle. Note that this loop does not execute for
4585 for (delta
= 0; delta
< middle
; delta
++) {
4586 /* Let's check delta earlier than middle */
4587 if (cmp_xe(&entries
[middle
- delta
- 1],
4588 &entries
[middle
- delta
]))
4589 return middle
- delta
;
4591 /* For even counts, don't walk off the end */
4592 if ((middle
+ delta
+ 1) == count
)
4595 /* Now try delta past middle */
4596 if (cmp_xe(&entries
[middle
+ delta
],
4597 &entries
[middle
+ delta
+ 1]))
4598 return middle
+ delta
+ 1;
4601 /* Every entry had the same hash */
4606 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4607 * first_hash will record the 1st hash of the new bucket.
4609 * Normally half of the xattrs will be moved. But we have to make
4610 * sure that the xattrs with the same hash value are stored in the
4611 * same bucket. If all the xattrs in this bucket have the same hash
4612 * value, the new bucket will be initialized as an empty one and the
4613 * first_hash will be initialized as (hash_value+1).
4615 static int ocfs2_divide_xattr_bucket(struct inode
*inode
,
4620 int new_bucket_head
)
4623 int count
, start
, len
, name_value_len
= 0, name_offset
= 0;
4624 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
4625 struct ocfs2_xattr_header
*xh
;
4626 struct ocfs2_xattr_entry
*xe
;
4627 int blocksize
= inode
->i_sb
->s_blocksize
;
4629 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4630 (unsigned long long)blk
, (unsigned long long)new_blk
);
4632 s_bucket
= ocfs2_xattr_bucket_new(inode
);
4633 t_bucket
= ocfs2_xattr_bucket_new(inode
);
4634 if (!s_bucket
|| !t_bucket
) {
4640 ret
= ocfs2_read_xattr_bucket(s_bucket
, blk
);
4646 ret
= ocfs2_xattr_bucket_journal_access(handle
, s_bucket
,
4647 OCFS2_JOURNAL_ACCESS_WRITE
);
4654 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4655 * there's no need to read it.
4657 ret
= ocfs2_init_xattr_bucket(t_bucket
, new_blk
);
4664 * Hey, if we're overwriting t_bucket, what difference does
4665 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4666 * same part of ocfs2_cp_xattr_bucket().
4668 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
4670 OCFS2_JOURNAL_ACCESS_CREATE
:
4671 OCFS2_JOURNAL_ACCESS_WRITE
);
4677 xh
= bucket_xh(s_bucket
);
4678 count
= le16_to_cpu(xh
->xh_count
);
4679 start
= ocfs2_xattr_find_divide_pos(xh
);
4681 if (start
== count
) {
4682 xe
= &xh
->xh_entries
[start
-1];
4685 * initialized a new empty bucket here.
4686 * The hash value is set as one larger than
4687 * that of the last entry in the previous bucket.
4689 for (i
= 0; i
< t_bucket
->bu_blocks
; i
++)
4690 memset(bucket_block(t_bucket
, i
), 0, blocksize
);
4692 xh
= bucket_xh(t_bucket
);
4693 xh
->xh_free_start
= cpu_to_le16(blocksize
);
4694 xh
->xh_entries
[0].xe_name_hash
= xe
->xe_name_hash
;
4695 le32_add_cpu(&xh
->xh_entries
[0].xe_name_hash
, 1);
4697 goto set_num_buckets
;
4700 /* copy the whole bucket to the new first. */
4701 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
4703 /* update the new bucket. */
4704 xh
= bucket_xh(t_bucket
);
4707 * Calculate the total name/value len and xh_free_start for
4708 * the old bucket first.
4710 name_offset
= OCFS2_XATTR_BUCKET_SIZE
;
4712 for (i
= 0; i
< start
; i
++) {
4713 xe
= &xh
->xh_entries
[i
];
4714 name_value_len
+= namevalue_size_xe(xe
);
4715 if (le16_to_cpu(xe
->xe_name_offset
) < name_offset
)
4716 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
4720 * Now begin the modification to the new bucket.
4722 * In the new bucket, We just move the xattr entry to the beginning
4723 * and don't touch the name/value. So there will be some holes in the
4724 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4727 xe
= &xh
->xh_entries
[start
];
4728 len
= sizeof(struct ocfs2_xattr_entry
) * (count
- start
);
4729 mlog(0, "mv xattr entry len %d from %d to %d\n", len
,
4730 (int)((char *)xe
- (char *)xh
),
4731 (int)((char *)xh
->xh_entries
- (char *)xh
));
4732 memmove((char *)xh
->xh_entries
, (char *)xe
, len
);
4733 xe
= &xh
->xh_entries
[count
- start
];
4734 len
= sizeof(struct ocfs2_xattr_entry
) * start
;
4735 memset((char *)xe
, 0, len
);
4737 le16_add_cpu(&xh
->xh_count
, -start
);
4738 le16_add_cpu(&xh
->xh_name_value_len
, -name_value_len
);
4740 /* Calculate xh_free_start for the new bucket. */
4741 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
4742 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
4743 xe
= &xh
->xh_entries
[i
];
4744 if (le16_to_cpu(xe
->xe_name_offset
) <
4745 le16_to_cpu(xh
->xh_free_start
))
4746 xh
->xh_free_start
= xe
->xe_name_offset
;
4750 /* set xh->xh_num_buckets for the new xh. */
4751 if (new_bucket_head
)
4752 xh
->xh_num_buckets
= cpu_to_le16(1);
4754 xh
->xh_num_buckets
= 0;
4756 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
4758 /* store the first_hash of the new bucket. */
4760 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
4763 * Now only update the 1st block of the old bucket. If we
4764 * just added a new empty bucket, there is no need to modify
4770 xh
= bucket_xh(s_bucket
);
4771 memset(&xh
->xh_entries
[start
], 0,
4772 sizeof(struct ocfs2_xattr_entry
) * (count
- start
));
4773 xh
->xh_count
= cpu_to_le16(start
);
4774 xh
->xh_free_start
= cpu_to_le16(name_offset
);
4775 xh
->xh_name_value_len
= cpu_to_le16(name_value_len
);
4777 ocfs2_xattr_bucket_journal_dirty(handle
, s_bucket
);
4780 ocfs2_xattr_bucket_free(s_bucket
);
4781 ocfs2_xattr_bucket_free(t_bucket
);
4787 * Copy xattr from one bucket to another bucket.
4789 * The caller must make sure that the journal transaction
4790 * has enough space for journaling.
4792 static int ocfs2_cp_xattr_bucket(struct inode
*inode
,
4799 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
4801 BUG_ON(s_blkno
== t_blkno
);
4803 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4804 (unsigned long long)s_blkno
, (unsigned long long)t_blkno
,
4807 s_bucket
= ocfs2_xattr_bucket_new(inode
);
4808 t_bucket
= ocfs2_xattr_bucket_new(inode
);
4809 if (!s_bucket
|| !t_bucket
) {
4815 ret
= ocfs2_read_xattr_bucket(s_bucket
, s_blkno
);
4820 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4821 * there's no need to read it.
4823 ret
= ocfs2_init_xattr_bucket(t_bucket
, t_blkno
);
4828 * Hey, if we're overwriting t_bucket, what difference does
4829 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4830 * cluster to fill, we came here from
4831 * ocfs2_mv_xattr_buckets(), and it is really new -
4832 * ACCESS_CREATE is required. But we also might have moved data
4833 * out of t_bucket before extending back into it.
4834 * ocfs2_add_new_xattr_bucket() can do this - its call to
4835 * ocfs2_add_new_xattr_cluster() may have created a new extent
4836 * and copied out the end of the old extent. Then it re-extends
4837 * the old extent back to create space for new xattrs. That's
4838 * how we get here, and the bucket isn't really new.
4840 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
4842 OCFS2_JOURNAL_ACCESS_CREATE
:
4843 OCFS2_JOURNAL_ACCESS_WRITE
);
4847 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
4848 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
4851 ocfs2_xattr_bucket_free(t_bucket
);
4852 ocfs2_xattr_bucket_free(s_bucket
);
4858 * src_blk points to the start of an existing extent. last_blk points to
4859 * last cluster in that extent. to_blk points to a newly allocated
4860 * extent. We copy the buckets from the cluster at last_blk to the new
4861 * extent. If start_bucket is non-zero, we skip that many buckets before
4862 * we start copying. The new extent's xh_num_buckets gets set to the
4863 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4864 * by the same amount.
4866 static int ocfs2_mv_xattr_buckets(struct inode
*inode
, handle_t
*handle
,
4867 u64 src_blk
, u64 last_blk
, u64 to_blk
,
4868 unsigned int start_bucket
,
4871 int i
, ret
, credits
;
4872 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4873 int blks_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4874 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
4875 struct ocfs2_xattr_bucket
*old_first
, *new_first
;
4877 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4878 (unsigned long long)last_blk
, (unsigned long long)to_blk
);
4880 BUG_ON(start_bucket
>= num_buckets
);
4882 num_buckets
-= start_bucket
;
4883 last_blk
+= (start_bucket
* blks_per_bucket
);
4886 /* The first bucket of the original extent */
4887 old_first
= ocfs2_xattr_bucket_new(inode
);
4888 /* The first bucket of the new extent */
4889 new_first
= ocfs2_xattr_bucket_new(inode
);
4890 if (!old_first
|| !new_first
) {
4896 ret
= ocfs2_read_xattr_bucket(old_first
, src_blk
);
4903 * We need to update the first bucket of the old extent and all
4904 * the buckets going to the new extent.
4906 credits
= ((num_buckets
+ 1) * blks_per_bucket
) +
4907 handle
->h_buffer_credits
;
4908 ret
= ocfs2_extend_trans(handle
, credits
);
4914 ret
= ocfs2_xattr_bucket_journal_access(handle
, old_first
,
4915 OCFS2_JOURNAL_ACCESS_WRITE
);
4921 for (i
= 0; i
< num_buckets
; i
++) {
4922 ret
= ocfs2_cp_xattr_bucket(inode
, handle
,
4923 last_blk
+ (i
* blks_per_bucket
),
4924 to_blk
+ (i
* blks_per_bucket
),
4933 * Get the new bucket ready before we dirty anything
4934 * (This actually shouldn't fail, because we already dirtied
4935 * it once in ocfs2_cp_xattr_bucket()).
4937 ret
= ocfs2_read_xattr_bucket(new_first
, to_blk
);
4942 ret
= ocfs2_xattr_bucket_journal_access(handle
, new_first
,
4943 OCFS2_JOURNAL_ACCESS_WRITE
);
4949 /* Now update the headers */
4950 le16_add_cpu(&bucket_xh(old_first
)->xh_num_buckets
, -num_buckets
);
4951 ocfs2_xattr_bucket_journal_dirty(handle
, old_first
);
4953 bucket_xh(new_first
)->xh_num_buckets
= cpu_to_le16(num_buckets
);
4954 ocfs2_xattr_bucket_journal_dirty(handle
, new_first
);
4957 *first_hash
= le32_to_cpu(bucket_xh(new_first
)->xh_entries
[0].xe_name_hash
);
4960 ocfs2_xattr_bucket_free(new_first
);
4961 ocfs2_xattr_bucket_free(old_first
);
4966 * Move some xattrs in this cluster to the new cluster.
4967 * This function should only be called when bucket size == cluster size.
4968 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4970 static int ocfs2_divide_xattr_cluster(struct inode
*inode
,
4976 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4977 int ret
, credits
= 2 * blk_per_bucket
+ handle
->h_buffer_credits
;
4979 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
< OCFS2_SB(inode
->i_sb
)->s_clustersize
);
4981 ret
= ocfs2_extend_trans(handle
, credits
);
4987 /* Move half of the xattr in start_blk to the next bucket. */
4988 return ocfs2_divide_xattr_bucket(inode
, handle
, prev_blk
,
4989 new_blk
, first_hash
, 1);
4993 * Move some xattrs from the old cluster to the new one since they are not
4994 * contiguous in ocfs2 xattr tree.
4996 * new_blk starts a new separate cluster, and we will move some xattrs from
4997 * prev_blk to it. v_start will be set as the first name hash value in this
4998 * new cluster so that it can be used as e_cpos during tree insertion and
4999 * don't collide with our original b-tree operations. first_bh and header_bh
5000 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
5001 * to extend the insert bucket.
5003 * The problem is how much xattr should we move to the new one and when should
5004 * we update first_bh and header_bh?
5005 * 1. If cluster size > bucket size, that means the previous cluster has more
5006 * than 1 bucket, so just move half nums of bucket into the new cluster and
5007 * update the first_bh and header_bh if the insert bucket has been moved
5008 * to the new cluster.
5009 * 2. If cluster_size == bucket_size:
5010 * a) If the previous extent rec has more than one cluster and the insert
5011 * place isn't in the last cluster, copy the entire last cluster to the
5012 * new one. This time, we don't need to upate the first_bh and header_bh
5013 * since they will not be moved into the new cluster.
5014 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
5015 * the new one. And we set the extend flag to zero if the insert place is
5016 * moved into the new allocated cluster since no extend is needed.
5018 static int ocfs2_adjust_xattr_cross_cluster(struct inode
*inode
,
5020 struct ocfs2_xattr_bucket
*first
,
5021 struct ocfs2_xattr_bucket
*target
,
5029 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
5030 (unsigned long long)bucket_blkno(first
), prev_clusters
,
5031 (unsigned long long)new_blk
);
5033 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
)) > 1) {
5034 ret
= ocfs2_mv_xattr_bucket_cross_cluster(inode
,
5043 /* The start of the last cluster in the first extent */
5044 u64 last_blk
= bucket_blkno(first
) +
5045 ((prev_clusters
- 1) *
5046 ocfs2_clusters_to_blocks(inode
->i_sb
, 1));
5048 if (prev_clusters
> 1 && bucket_blkno(target
) != last_blk
) {
5049 ret
= ocfs2_mv_xattr_buckets(inode
, handle
,
5050 bucket_blkno(first
),
5051 last_blk
, new_blk
, 0,
5056 ret
= ocfs2_divide_xattr_cluster(inode
, handle
,
5062 if ((bucket_blkno(target
) == last_blk
) && extend
)
5071 * Add a new cluster for xattr storage.
5073 * If the new cluster is contiguous with the previous one, it will be
5074 * appended to the same extent record, and num_clusters will be updated.
5075 * If not, we will insert a new extent for it and move some xattrs in
5076 * the last cluster into the new allocated one.
5077 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5078 * lose the benefits of hashing because we'll have to search large leaves.
5079 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5082 * first_bh is the first block of the previous extent rec and header_bh
5083 * indicates the bucket we will insert the new xattrs. They will be updated
5084 * when the header_bh is moved into the new cluster.
5086 static int ocfs2_add_new_xattr_cluster(struct inode
*inode
,
5087 struct buffer_head
*root_bh
,
5088 struct ocfs2_xattr_bucket
*first
,
5089 struct ocfs2_xattr_bucket
*target
,
5093 struct ocfs2_xattr_set_ctxt
*ctxt
)
5096 u16 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
5097 u32 prev_clusters
= *num_clusters
;
5098 u32 clusters_to_add
= 1, bit_off
, num_bits
, v_start
= 0;
5100 handle_t
*handle
= ctxt
->handle
;
5101 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5102 struct ocfs2_extent_tree et
;
5104 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5105 "previous xattr blkno = %llu\n",
5106 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5107 prev_cpos
, (unsigned long long)bucket_blkno(first
));
5109 ocfs2_init_xattr_tree_extent_tree(&et
, INODE_CACHE(inode
), root_bh
);
5111 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), root_bh
,
5112 OCFS2_JOURNAL_ACCESS_WRITE
);
5118 ret
= __ocfs2_claim_clusters(osb
, handle
, ctxt
->data_ac
, 1,
5119 clusters_to_add
, &bit_off
, &num_bits
);
5126 BUG_ON(num_bits
> clusters_to_add
);
5128 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
5129 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5130 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
5132 if (bucket_blkno(first
) + (prev_clusters
* bpc
) == block
&&
5133 (prev_clusters
+ num_bits
) << osb
->s_clustersize_bits
<=
5134 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
) {
5136 * If this cluster is contiguous with the old one and
5137 * adding this new cluster, we don't surpass the limit of
5138 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5139 * initialized and used like other buckets in the previous
5141 * So add it as a contiguous one. The caller will handle
5144 v_start
= prev_cpos
+ prev_clusters
;
5145 *num_clusters
= prev_clusters
+ num_bits
;
5146 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5149 ret
= ocfs2_adjust_xattr_cross_cluster(inode
,
5163 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
5164 num_bits
, (unsigned long long)block
, v_start
);
5165 ret
= ocfs2_insert_extent(handle
, &et
, v_start
, block
,
5166 num_bits
, 0, ctxt
->meta_ac
);
5172 ret
= ocfs2_journal_dirty(handle
, root_bh
);
5181 * We are given an extent. 'first' is the bucket at the very front of
5182 * the extent. The extent has space for an additional bucket past
5183 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5184 * of the target bucket. We wish to shift every bucket past the target
5185 * down one, filling in that additional space. When we get back to the
5186 * target, we split the target between itself and the now-empty bucket
5187 * at target+1 (aka, target_blkno + blks_per_bucket).
5189 static int ocfs2_extend_xattr_bucket(struct inode
*inode
,
5191 struct ocfs2_xattr_bucket
*first
,
5196 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5197 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
5199 u16 new_bucket
= le16_to_cpu(bucket_xh(first
)->xh_num_buckets
);
5201 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
5202 "from %llu, len = %u\n", (unsigned long long)target_blk
,
5203 (unsigned long long)bucket_blkno(first
), num_clusters
);
5205 /* The extent must have room for an additional bucket */
5206 BUG_ON(new_bucket
>=
5207 (num_clusters
* ocfs2_xattr_buckets_per_cluster(osb
)));
5209 /* end_blk points to the last existing bucket */
5210 end_blk
= bucket_blkno(first
) + ((new_bucket
- 1) * blk_per_bucket
);
5213 * end_blk is the start of the last existing bucket.
5214 * Thus, (end_blk - target_blk) covers the target bucket and
5215 * every bucket after it up to, but not including, the last
5216 * existing bucket. Then we add the last existing bucket, the
5217 * new bucket, and the first bucket (3 * blk_per_bucket).
5219 credits
= (end_blk
- target_blk
) + (3 * blk_per_bucket
) +
5220 handle
->h_buffer_credits
;
5221 ret
= ocfs2_extend_trans(handle
, credits
);
5227 ret
= ocfs2_xattr_bucket_journal_access(handle
, first
,
5228 OCFS2_JOURNAL_ACCESS_WRITE
);
5234 while (end_blk
!= target_blk
) {
5235 ret
= ocfs2_cp_xattr_bucket(inode
, handle
, end_blk
,
5236 end_blk
+ blk_per_bucket
, 0);
5239 end_blk
-= blk_per_bucket
;
5242 /* Move half of the xattr in target_blkno to the next bucket. */
5243 ret
= ocfs2_divide_xattr_bucket(inode
, handle
, target_blk
,
5244 target_blk
+ blk_per_bucket
, NULL
, 0);
5246 le16_add_cpu(&bucket_xh(first
)->xh_num_buckets
, 1);
5247 ocfs2_xattr_bucket_journal_dirty(handle
, first
);
5254 * Add new xattr bucket in an extent record and adjust the buckets
5255 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5256 * bucket we want to insert into.
5258 * In the easy case, we will move all the buckets after target down by
5259 * one. Half of target's xattrs will be moved to the next bucket.
5261 * If current cluster is full, we'll allocate a new one. This may not
5262 * be contiguous. The underlying calls will make sure that there is
5263 * space for the insert, shifting buckets around if necessary.
5264 * 'target' may be moved by those calls.
5266 static int ocfs2_add_new_xattr_bucket(struct inode
*inode
,
5267 struct buffer_head
*xb_bh
,
5268 struct ocfs2_xattr_bucket
*target
,
5269 struct ocfs2_xattr_set_ctxt
*ctxt
)
5271 struct ocfs2_xattr_block
*xb
=
5272 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
5273 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
5274 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
5276 le32_to_cpu(bucket_xh(target
)->xh_entries
[0].xe_name_hash
);
5277 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5278 int ret
, num_buckets
, extend
= 1;
5280 u32 e_cpos
, num_clusters
;
5281 /* The bucket at the front of the extent */
5282 struct ocfs2_xattr_bucket
*first
;
5284 mlog(0, "Add new xattr bucket starting from %llu\n",
5285 (unsigned long long)bucket_blkno(target
));
5287 /* The first bucket of the original extent */
5288 first
= ocfs2_xattr_bucket_new(inode
);
5295 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &e_cpos
,
5302 ret
= ocfs2_read_xattr_bucket(first
, p_blkno
);
5308 num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
) * num_clusters
;
5309 if (num_buckets
== le16_to_cpu(bucket_xh(first
)->xh_num_buckets
)) {
5311 * This can move first+target if the target bucket moves
5312 * to the new extent.
5314 ret
= ocfs2_add_new_xattr_cluster(inode
,
5329 ret
= ocfs2_extend_xattr_bucket(inode
,
5332 bucket_blkno(target
),
5339 ocfs2_xattr_bucket_free(first
);
5344 static inline char *ocfs2_xattr_bucket_get_val(struct inode
*inode
,
5345 struct ocfs2_xattr_bucket
*bucket
,
5348 int block_off
= offs
>> inode
->i_sb
->s_blocksize_bits
;
5350 offs
= offs
% inode
->i_sb
->s_blocksize
;
5351 return bucket_block(bucket
, block_off
) + offs
;
5355 * Truncate the specified xe_off entry in xattr bucket.
5356 * bucket is indicated by header_bh and len is the new length.
5357 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5359 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5361 static int ocfs2_xattr_bucket_value_truncate(struct inode
*inode
,
5362 struct ocfs2_xattr_bucket
*bucket
,
5365 struct ocfs2_xattr_set_ctxt
*ctxt
)
5369 struct ocfs2_xattr_entry
*xe
;
5370 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5371 size_t blocksize
= inode
->i_sb
->s_blocksize
;
5372 struct ocfs2_xattr_value_buf vb
= {
5373 .vb_access
= ocfs2_journal_access
,
5376 xe
= &xh
->xh_entries
[xe_off
];
5378 BUG_ON(!xe
|| ocfs2_xattr_is_local(xe
));
5380 offset
= le16_to_cpu(xe
->xe_name_offset
) +
5381 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5383 value_blk
= offset
/ blocksize
;
5385 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5386 BUG_ON(value_blk
!= (offset
+ OCFS2_XATTR_ROOT_SIZE
- 1) / blocksize
);
5388 vb
.vb_bh
= bucket
->bu_bhs
[value_blk
];
5391 vb
.vb_xv
= (struct ocfs2_xattr_value_root
*)
5392 (vb
.vb_bh
->b_data
+ offset
% blocksize
);
5395 * From here on out we have to dirty the bucket. The generic
5396 * value calls only modify one of the bucket's bhs, but we need
5397 * to send the bucket at once. So if they error, they *could* have
5398 * modified something. We have to assume they did, and dirty
5399 * the whole bucket. This leaves us in a consistent state.
5401 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5402 xe_off
, (unsigned long long)bucket_blkno(bucket
), len
);
5403 ret
= ocfs2_xattr_value_truncate(inode
, &vb
, len
, ctxt
);
5409 ret
= ocfs2_xattr_bucket_journal_access(ctxt
->handle
, bucket
,
5410 OCFS2_JOURNAL_ACCESS_WRITE
);
5416 xe
->xe_value_size
= cpu_to_le64(len
);
5418 ocfs2_xattr_bucket_journal_dirty(ctxt
->handle
, bucket
);
5424 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
5425 struct buffer_head
*root_bh
,
5432 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5433 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5435 struct ocfs2_xattr_block
*xb
=
5436 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
5437 struct ocfs2_alloc_context
*meta_ac
= NULL
;
5438 struct ocfs2_cached_dealloc_ctxt dealloc
;
5439 struct ocfs2_extent_tree et
;
5441 ret
= ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
5442 ocfs2_delete_xattr_in_bucket
, para
);
5448 ocfs2_init_xattr_tree_extent_tree(&et
, INODE_CACHE(inode
), root_bh
);
5450 ocfs2_init_dealloc_ctxt(&dealloc
);
5452 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5453 cpos
, len
, (unsigned long long)blkno
);
5455 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode
), blkno
,
5458 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
5464 mutex_lock(&tl_inode
->i_mutex
);
5466 if (ocfs2_truncate_log_needs_flush(osb
)) {
5467 ret
= __ocfs2_flush_truncate_log(osb
);
5474 handle
= ocfs2_start_trans(osb
, ocfs2_remove_extent_credits(osb
->sb
));
5475 if (IS_ERR(handle
)) {
5481 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), root_bh
,
5482 OCFS2_JOURNAL_ACCESS_WRITE
);
5488 ret
= ocfs2_remove_extent(handle
, &et
, cpos
, len
, meta_ac
,
5495 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, -len
);
5497 ret
= ocfs2_journal_dirty(handle
, root_bh
);
5503 ret
= ocfs2_truncate_log_append(osb
, handle
, blkno
, len
);
5508 ocfs2_commit_trans(osb
, handle
);
5510 ocfs2_schedule_truncate_log_flush(osb
, 1);
5512 mutex_unlock(&tl_inode
->i_mutex
);
5515 ocfs2_free_alloc_context(meta_ac
);
5517 ocfs2_run_deallocs(osb
, &dealloc
);
5523 * check whether the xattr bucket is filled up with the same hash value.
5524 * If we want to insert the xattr with the same hash, return -ENOSPC.
5525 * If we want to insert a xattr with different hash value, go ahead
5526 * and ocfs2_divide_xattr_bucket will handle this.
5528 static int ocfs2_check_xattr_bucket_collision(struct inode
*inode
,
5529 struct ocfs2_xattr_bucket
*bucket
,
5532 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5533 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
5535 if (name_hash
!= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
))
5538 if (xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1].xe_name_hash
==
5539 xh
->xh_entries
[0].xe_name_hash
) {
5540 mlog(ML_ERROR
, "Too much hash collision in xattr bucket %llu, "
5542 (unsigned long long)bucket_blkno(bucket
),
5543 le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
));
5551 * Try to set the entry in the current bucket. If we fail, the caller
5552 * will handle getting us another bucket.
5554 static int ocfs2_xattr_set_entry_bucket(struct inode
*inode
,
5555 struct ocfs2_xattr_info
*xi
,
5556 struct ocfs2_xattr_search
*xs
,
5557 struct ocfs2_xattr_set_ctxt
*ctxt
)
5560 struct ocfs2_xa_loc loc
;
5562 mlog_entry("Set xattr %s in xattr bucket\n", xi
->xi_name
);
5564 ocfs2_init_xattr_bucket_xa_loc(&loc
, xs
->bucket
,
5565 xs
->not_found
? NULL
: xs
->here
);
5566 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
5568 xs
->here
= loc
.xl_entry
;
5571 if (ret
!= -ENOSPC
) {
5576 /* Ok, we need space. Let's try defragmenting the bucket. */
5577 ret
= ocfs2_defrag_xattr_bucket(inode
, ctxt
->handle
,
5584 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
5586 xs
->here
= loc
.xl_entry
;
5598 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
5599 struct ocfs2_xattr_info
*xi
,
5600 struct ocfs2_xattr_search
*xs
,
5601 struct ocfs2_xattr_set_ctxt
*ctxt
)
5605 mlog_entry("Set xattr %s in xattr index block\n", xi
->xi_name
);
5607 ret
= ocfs2_xattr_set_entry_bucket(inode
, xi
, xs
, ctxt
);
5610 if (ret
!= -ENOSPC
) {
5615 /* Ack, need more space. Let's try to get another bucket! */
5618 * We do not allow for overlapping ranges between buckets. And
5619 * the maximum number of collisions we will allow for then is
5620 * one bucket's worth, so check it here whether we need to
5621 * add a new bucket for the insert.
5623 ret
= ocfs2_check_xattr_bucket_collision(inode
,
5631 ret
= ocfs2_add_new_xattr_bucket(inode
,
5641 * ocfs2_add_new_xattr_bucket() will have updated
5642 * xs->bucket if it moved, but it will not have updated
5643 * any of the other search fields. Thus, we drop it and
5644 * re-search. Everything should be cached, so it'll be
5647 ocfs2_xattr_bucket_relse(xs
->bucket
);
5648 ret
= ocfs2_xattr_index_block_find(inode
, xs
->xattr_bh
,
5651 if (ret
&& ret
!= -ENODATA
)
5653 xs
->not_found
= ret
;
5655 /* Ok, we have a new bucket, let's try again */
5656 ret
= ocfs2_xattr_set_entry_bucket(inode
, xi
, xs
, ctxt
);
5657 if (ret
&& (ret
!= -ENOSPC
))
5665 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
5666 struct ocfs2_xattr_bucket
*bucket
,
5669 int ret
= 0, ref_credits
;
5670 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5672 struct ocfs2_xattr_entry
*xe
;
5673 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5674 struct ocfs2_xattr_set_ctxt ctxt
= {NULL
, NULL
,};
5675 int credits
= ocfs2_remove_extent_credits(osb
->sb
) +
5676 ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
5677 struct ocfs2_xattr_value_root
*xv
;
5678 struct ocfs2_rm_xattr_bucket_para
*args
=
5679 (struct ocfs2_rm_xattr_bucket_para
*)para
;
5681 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
5683 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
5684 xe
= &xh
->xh_entries
[i
];
5685 if (ocfs2_xattr_is_local(xe
))
5688 ret
= ocfs2_get_xattr_tree_value_root(inode
->i_sb
, bucket
,
5691 ret
= ocfs2_lock_xattr_remove_allocators(inode
, xv
,
5697 ctxt
.handle
= ocfs2_start_trans(osb
, credits
+ ref_credits
);
5698 if (IS_ERR(ctxt
.handle
)) {
5699 ret
= PTR_ERR(ctxt
.handle
);
5704 ret
= ocfs2_xattr_bucket_value_truncate(inode
, bucket
,
5707 ocfs2_commit_trans(osb
, ctxt
.handle
);
5709 ocfs2_free_alloc_context(ctxt
.meta_ac
);
5710 ctxt
.meta_ac
= NULL
;
5719 ocfs2_free_alloc_context(ctxt
.meta_ac
);
5720 ocfs2_schedule_truncate_log_flush(osb
, 1);
5721 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
5726 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5727 * or change the extent record flag), we need to recalculate
5728 * the metaecc for the whole bucket. So it is done here.
5731 * We have to give the extra credits for the caller.
5733 static int ocfs2_xattr_bucket_post_refcount(struct inode
*inode
,
5738 struct ocfs2_xattr_bucket
*bucket
=
5739 (struct ocfs2_xattr_bucket
*)para
;
5741 ret
= ocfs2_xattr_bucket_journal_access(handle
, bucket
,
5742 OCFS2_JOURNAL_ACCESS_WRITE
);
5748 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
5754 * Special action we need if the xattr value is refcounted.
5756 * 1. If the xattr is refcounted, lock the tree.
5757 * 2. CoW the xattr if we are setting the new value and the value
5758 * will be stored outside.
5759 * 3. In other case, decrease_refcount will work for us, so just
5760 * lock the refcount tree, calculate the meta and credits is OK.
5762 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5763 * currently CoW is a completed transaction, while this function
5764 * will also lock the allocators and let us deadlock. So we will
5765 * CoW the whole xattr value.
5767 static int ocfs2_prepare_refcount_xattr(struct inode
*inode
,
5768 struct ocfs2_dinode
*di
,
5769 struct ocfs2_xattr_info
*xi
,
5770 struct ocfs2_xattr_search
*xis
,
5771 struct ocfs2_xattr_search
*xbs
,
5772 struct ocfs2_refcount_tree
**ref_tree
,
5777 struct ocfs2_xattr_block
*xb
;
5778 struct ocfs2_xattr_entry
*xe
;
5780 u32 p_cluster
, num_clusters
;
5781 unsigned int ext_flags
;
5782 int name_offset
, name_len
;
5783 struct ocfs2_xattr_value_buf vb
;
5784 struct ocfs2_xattr_bucket
*bucket
= NULL
;
5785 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5786 struct ocfs2_post_refcount refcount
;
5787 struct ocfs2_post_refcount
*p
= NULL
;
5788 struct buffer_head
*ref_root_bh
= NULL
;
5790 if (!xis
->not_found
) {
5792 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
5793 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5795 vb
.vb_bh
= xis
->inode_bh
;
5796 vb
.vb_access
= ocfs2_journal_access_di
;
5798 int i
, block_off
= 0;
5799 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
5801 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
5802 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5803 i
= xbs
->here
- xbs
->header
->xh_entries
;
5805 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
5806 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
5807 bucket_xh(xbs
->bucket
),
5814 base
= bucket_block(xbs
->bucket
, block_off
);
5815 vb
.vb_bh
= xbs
->bucket
->bu_bhs
[block_off
];
5816 vb
.vb_access
= ocfs2_journal_access
;
5818 if (ocfs2_meta_ecc(osb
)) {
5819 /*create parameters for ocfs2_post_refcount. */
5820 bucket
= xbs
->bucket
;
5821 refcount
.credits
= bucket
->bu_blocks
;
5822 refcount
.para
= bucket
;
5824 ocfs2_xattr_bucket_post_refcount
;
5829 vb
.vb_bh
= xbs
->xattr_bh
;
5830 vb
.vb_access
= ocfs2_journal_access_xb
;
5834 if (ocfs2_xattr_is_local(xe
))
5837 vb
.vb_xv
= (struct ocfs2_xattr_value_root
*)
5838 (base
+ name_offset
+ name_len
);
5840 ret
= ocfs2_xattr_get_clusters(inode
, 0, &p_cluster
,
5841 &num_clusters
, &vb
.vb_xv
->xr_list
,
5849 * We just need to check the 1st extent record, since we always
5850 * CoW the whole xattr. So there shouldn't be a xattr with
5851 * some REFCOUNT extent recs after the 1st one.
5853 if (!(ext_flags
& OCFS2_EXT_REFCOUNTED
))
5856 ret
= ocfs2_lock_refcount_tree(osb
, le64_to_cpu(di
->i_refcount_loc
),
5857 1, ref_tree
, &ref_root_bh
);
5864 * If we are deleting the xattr or the new size will be stored inside,
5865 * cool, leave it there, the xattr truncate process will remove them
5866 * for us(it still needs the refcount tree lock and the meta, credits).
5867 * And the worse case is that every cluster truncate will split the
5868 * refcount tree, and make the original extent become 3. So we will need
5869 * 2 * cluster more extent recs at most.
5871 if (!xi
->xi_value
|| xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
) {
5873 ret
= ocfs2_refcounted_xattr_delete_need(inode
,
5874 &(*ref_tree
)->rf_ci
,
5875 ref_root_bh
, vb
.vb_xv
,
5882 ret
= ocfs2_refcount_cow_xattr(inode
, di
, &vb
,
5883 *ref_tree
, ref_root_bh
, 0,
5884 le32_to_cpu(vb
.vb_xv
->xr_clusters
), p
);
5889 brelse(ref_root_bh
);
5894 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5895 * The physical clusters will be added to refcount tree.
5897 static int ocfs2_xattr_value_attach_refcount(struct inode
*inode
,
5898 struct ocfs2_xattr_value_root
*xv
,
5899 struct ocfs2_extent_tree
*value_et
,
5900 struct ocfs2_caching_info
*ref_ci
,
5901 struct buffer_head
*ref_root_bh
,
5902 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5903 struct ocfs2_post_refcount
*refcount
)
5906 u32 clusters
= le32_to_cpu(xv
->xr_clusters
);
5907 u32 cpos
, p_cluster
, num_clusters
;
5908 struct ocfs2_extent_list
*el
= &xv
->xr_list
;
5909 unsigned int ext_flags
;
5912 while (cpos
< clusters
) {
5913 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
5914 &num_clusters
, el
, &ext_flags
);
5916 cpos
+= num_clusters
;
5917 if ((ext_flags
& OCFS2_EXT_REFCOUNTED
))
5922 ret
= ocfs2_add_refcount_flag(inode
, value_et
,
5923 ref_ci
, ref_root_bh
,
5924 cpos
- num_clusters
,
5925 p_cluster
, num_clusters
,
5937 * Given a normal ocfs2_xattr_header, refcount all the entries which
5938 * have value stored outside.
5939 * Used for xattrs stored in inode and ocfs2_xattr_block.
5941 static int ocfs2_xattr_attach_refcount_normal(struct inode
*inode
,
5942 struct ocfs2_xattr_value_buf
*vb
,
5943 struct ocfs2_xattr_header
*header
,
5944 struct ocfs2_caching_info
*ref_ci
,
5945 struct buffer_head
*ref_root_bh
,
5946 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5949 struct ocfs2_xattr_entry
*xe
;
5950 struct ocfs2_xattr_value_root
*xv
;
5951 struct ocfs2_extent_tree et
;
5954 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
5955 xe
= &header
->xh_entries
[i
];
5957 if (ocfs2_xattr_is_local(xe
))
5960 xv
= (struct ocfs2_xattr_value_root
*)((void *)header
+
5961 le16_to_cpu(xe
->xe_name_offset
) +
5962 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
5965 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
5967 ret
= ocfs2_xattr_value_attach_refcount(inode
, xv
, &et
,
5968 ref_ci
, ref_root_bh
,
5979 static int ocfs2_xattr_inline_attach_refcount(struct inode
*inode
,
5980 struct buffer_head
*fe_bh
,
5981 struct ocfs2_caching_info
*ref_ci
,
5982 struct buffer_head
*ref_root_bh
,
5983 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5985 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
5986 struct ocfs2_xattr_header
*header
= (struct ocfs2_xattr_header
*)
5987 (fe_bh
->b_data
+ inode
->i_sb
->s_blocksize
-
5988 le16_to_cpu(di
->i_xattr_inline_size
));
5989 struct ocfs2_xattr_value_buf vb
= {
5991 .vb_access
= ocfs2_journal_access_di
,
5994 return ocfs2_xattr_attach_refcount_normal(inode
, &vb
, header
,
5995 ref_ci
, ref_root_bh
, dealloc
);
5998 struct ocfs2_xattr_tree_value_refcount_para
{
5999 struct ocfs2_caching_info
*ref_ci
;
6000 struct buffer_head
*ref_root_bh
;
6001 struct ocfs2_cached_dealloc_ctxt
*dealloc
;
6004 static int ocfs2_get_xattr_tree_value_root(struct super_block
*sb
,
6005 struct ocfs2_xattr_bucket
*bucket
,
6007 struct ocfs2_xattr_value_root
**xv
,
6008 struct buffer_head
**bh
)
6010 int ret
, block_off
, name_offset
;
6011 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
6012 struct ocfs2_xattr_entry
*xe
= &xh
->xh_entries
[offset
];
6015 ret
= ocfs2_xattr_bucket_get_name_value(sb
,
6025 base
= bucket_block(bucket
, block_off
);
6027 *xv
= (struct ocfs2_xattr_value_root
*)(base
+ name_offset
+
6028 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
6031 *bh
= bucket
->bu_bhs
[block_off
];
6037 * For a given xattr bucket, refcount all the entries which
6038 * have value stored outside.
6040 static int ocfs2_xattr_bucket_value_refcount(struct inode
*inode
,
6041 struct ocfs2_xattr_bucket
*bucket
,
6045 struct ocfs2_extent_tree et
;
6046 struct ocfs2_xattr_tree_value_refcount_para
*ref
=
6047 (struct ocfs2_xattr_tree_value_refcount_para
*)para
;
6048 struct ocfs2_xattr_header
*xh
=
6049 (struct ocfs2_xattr_header
*)bucket
->bu_bhs
[0]->b_data
;
6050 struct ocfs2_xattr_entry
*xe
;
6051 struct ocfs2_xattr_value_buf vb
= {
6052 .vb_access
= ocfs2_journal_access
,
6054 struct ocfs2_post_refcount refcount
= {
6055 .credits
= bucket
->bu_blocks
,
6057 .func
= ocfs2_xattr_bucket_post_refcount
,
6059 struct ocfs2_post_refcount
*p
= NULL
;
6061 /* We only need post_refcount if we support metaecc. */
6062 if (ocfs2_meta_ecc(OCFS2_SB(inode
->i_sb
)))
6065 mlog(0, "refcount bucket %llu, count = %u\n",
6066 (unsigned long long)bucket_blkno(bucket
),
6067 le16_to_cpu(xh
->xh_count
));
6068 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
6069 xe
= &xh
->xh_entries
[i
];
6071 if (ocfs2_xattr_is_local(xe
))
6074 ret
= ocfs2_get_xattr_tree_value_root(inode
->i_sb
, bucket
, i
,
6075 &vb
.vb_xv
, &vb
.vb_bh
);
6081 ocfs2_init_xattr_value_extent_tree(&et
,
6082 INODE_CACHE(inode
), &vb
);
6084 ret
= ocfs2_xattr_value_attach_refcount(inode
, vb
.vb_xv
,
6098 static int ocfs2_refcount_xattr_tree_rec(struct inode
*inode
,
6099 struct buffer_head
*root_bh
,
6100 u64 blkno
, u32 cpos
, u32 len
, void *para
)
6102 return ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
6103 ocfs2_xattr_bucket_value_refcount
,
6107 static int ocfs2_xattr_block_attach_refcount(struct inode
*inode
,
6108 struct buffer_head
*blk_bh
,
6109 struct ocfs2_caching_info
*ref_ci
,
6110 struct buffer_head
*ref_root_bh
,
6111 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
6114 struct ocfs2_xattr_block
*xb
=
6115 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
6117 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
6118 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
6119 struct ocfs2_xattr_value_buf vb
= {
6121 .vb_access
= ocfs2_journal_access_xb
,
6124 ret
= ocfs2_xattr_attach_refcount_normal(inode
, &vb
, header
,
6125 ref_ci
, ref_root_bh
,
6128 struct ocfs2_xattr_tree_value_refcount_para para
= {
6130 .ref_root_bh
= ref_root_bh
,
6134 ret
= ocfs2_iterate_xattr_index_block(inode
, blk_bh
,
6135 ocfs2_refcount_xattr_tree_rec
,
6142 int ocfs2_xattr_attach_refcount_tree(struct inode
*inode
,
6143 struct buffer_head
*fe_bh
,
6144 struct ocfs2_caching_info
*ref_ci
,
6145 struct buffer_head
*ref_root_bh
,
6146 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
6149 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6150 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
6151 struct buffer_head
*blk_bh
= NULL
;
6153 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
6154 ret
= ocfs2_xattr_inline_attach_refcount(inode
, fe_bh
,
6155 ref_ci
, ref_root_bh
,
6163 if (!di
->i_xattr_loc
)
6166 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
6173 ret
= ocfs2_xattr_block_attach_refcount(inode
, blk_bh
, ref_ci
,
6174 ref_root_bh
, dealloc
);
6184 typedef int (should_xattr_reflinked
)(struct ocfs2_xattr_entry
*xe
);
6186 * Store the information we need in xattr reflink.
6187 * old_bh and new_bh are inode bh for the old and new inode.
6189 struct ocfs2_xattr_reflink
{
6190 struct inode
*old_inode
;
6191 struct inode
*new_inode
;
6192 struct buffer_head
*old_bh
;
6193 struct buffer_head
*new_bh
;
6194 struct ocfs2_caching_info
*ref_ci
;
6195 struct buffer_head
*ref_root_bh
;
6196 struct ocfs2_cached_dealloc_ctxt
*dealloc
;
6197 should_xattr_reflinked
*xattr_reflinked
;
6201 * Given a xattr header and xe offset,
6202 * return the proper xv and the corresponding bh.
6203 * xattr in inode, block and xattr tree have different implementaions.
6205 typedef int (get_xattr_value_root
)(struct super_block
*sb
,
6206 struct buffer_head
*bh
,
6207 struct ocfs2_xattr_header
*xh
,
6209 struct ocfs2_xattr_value_root
**xv
,
6210 struct buffer_head
**ret_bh
,
6214 * Calculate all the xattr value root metadata stored in this xattr header and
6215 * credits we need if we create them from the scratch.
6216 * We use get_xattr_value_root so that all types of xattr container can use it.
6218 static int ocfs2_value_metas_in_xattr_header(struct super_block
*sb
,
6219 struct buffer_head
*bh
,
6220 struct ocfs2_xattr_header
*xh
,
6221 int *metas
, int *credits
,
6223 get_xattr_value_root
*func
,
6227 struct ocfs2_xattr_value_root
*xv
;
6228 struct ocfs2_xattr_entry
*xe
;
6230 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
6231 xe
= &xh
->xh_entries
[i
];
6232 if (ocfs2_xattr_is_local(xe
))
6235 ret
= func(sb
, bh
, xh
, i
, &xv
, NULL
, para
);
6241 *metas
+= le16_to_cpu(xv
->xr_list
.l_tree_depth
) *
6242 le16_to_cpu(xv
->xr_list
.l_next_free_rec
);
6244 *credits
+= ocfs2_calc_extend_credits(sb
,
6246 le32_to_cpu(xv
->xr_clusters
));
6249 * If the value is a tree with depth > 1, We don't go deep
6250 * to the extent block, so just calculate a maximum record num.
6252 if (!xv
->xr_list
.l_tree_depth
)
6253 *num_recs
+= le16_to_cpu(xv
->xr_list
.l_next_free_rec
);
6255 *num_recs
+= ocfs2_clusters_for_bytes(sb
,
6262 /* Used by xattr inode and block to return the right xv and buffer_head. */
6263 static int ocfs2_get_xattr_value_root(struct super_block
*sb
,
6264 struct buffer_head
*bh
,
6265 struct ocfs2_xattr_header
*xh
,
6267 struct ocfs2_xattr_value_root
**xv
,
6268 struct buffer_head
**ret_bh
,
6271 struct ocfs2_xattr_entry
*xe
= &xh
->xh_entries
[offset
];
6273 *xv
= (struct ocfs2_xattr_value_root
*)((void *)xh
+
6274 le16_to_cpu(xe
->xe_name_offset
) +
6275 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
6284 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6285 * It is only used for inline xattr and xattr block.
6287 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super
*osb
,
6288 struct ocfs2_xattr_header
*xh
,
6289 struct buffer_head
*ref_root_bh
,
6291 struct ocfs2_alloc_context
**meta_ac
)
6293 int ret
, meta_add
= 0, num_recs
= 0;
6294 struct ocfs2_refcount_block
*rb
=
6295 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
6299 ret
= ocfs2_value_metas_in_xattr_header(osb
->sb
, NULL
, xh
,
6300 &meta_add
, credits
, &num_recs
,
6301 ocfs2_get_xattr_value_root
,
6309 * We need to add/modify num_recs in refcount tree, so just calculate
6310 * an approximate number we need for refcount tree change.
6311 * Sometimes we need to split the tree, and after split, half recs
6312 * will be moved to the new block, and a new block can only provide
6313 * half number of recs. So we multiple new blocks by 2.
6315 num_recs
= num_recs
/ ocfs2_refcount_recs_per_rb(osb
->sb
) * 2;
6316 meta_add
+= num_recs
;
6317 *credits
+= num_recs
+ num_recs
* OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
6318 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)
6319 *credits
+= le16_to_cpu(rb
->rf_list
.l_tree_depth
) *
6320 le16_to_cpu(rb
->rf_list
.l_next_free_rec
) + 1;
6324 ret
= ocfs2_reserve_new_metadata_blocks(osb
, meta_add
, meta_ac
);
6333 * Given a xattr header, reflink all the xattrs in this container.
6334 * It can be used for inode, block and bucket.
6337 * Before we call this function, the caller has memcpy the xattr in
6338 * old_xh to the new_xh.
6340 * If args.xattr_reflinked is set, call it to decide whether the xe should
6341 * be reflinked or not. If not, remove it from the new xattr header.
6343 static int ocfs2_reflink_xattr_header(handle_t
*handle
,
6344 struct ocfs2_xattr_reflink
*args
,
6345 struct buffer_head
*old_bh
,
6346 struct ocfs2_xattr_header
*xh
,
6347 struct buffer_head
*new_bh
,
6348 struct ocfs2_xattr_header
*new_xh
,
6349 struct ocfs2_xattr_value_buf
*vb
,
6350 struct ocfs2_alloc_context
*meta_ac
,
6351 get_xattr_value_root
*func
,
6355 struct super_block
*sb
= args
->old_inode
->i_sb
;
6356 struct buffer_head
*value_bh
;
6357 struct ocfs2_xattr_entry
*xe
, *last
;
6358 struct ocfs2_xattr_value_root
*xv
, *new_xv
;
6359 struct ocfs2_extent_tree data_et
;
6360 u32 clusters
, cpos
, p_cluster
, num_clusters
;
6361 unsigned int ext_flags
= 0;
6363 mlog(0, "reflink xattr in container %llu, count = %u\n",
6364 (unsigned long long)old_bh
->b_blocknr
, le16_to_cpu(xh
->xh_count
));
6366 last
= &new_xh
->xh_entries
[le16_to_cpu(new_xh
->xh_count
)];
6367 for (i
= 0, j
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, j
++) {
6368 xe
= &xh
->xh_entries
[i
];
6370 if (args
->xattr_reflinked
&& !args
->xattr_reflinked(xe
)) {
6371 xe
= &new_xh
->xh_entries
[j
];
6373 le16_add_cpu(&new_xh
->xh_count
, -1);
6374 if (new_xh
->xh_count
) {
6376 (void *)last
- (void *)xe
);
6378 sizeof(struct ocfs2_xattr_entry
));
6382 * We don't want j to increase in the next round since
6383 * it is already moved ahead.
6389 if (ocfs2_xattr_is_local(xe
))
6392 ret
= func(sb
, old_bh
, xh
, i
, &xv
, NULL
, para
);
6398 ret
= func(sb
, new_bh
, new_xh
, j
, &new_xv
, &value_bh
, para
);
6405 * For the xattr which has l_tree_depth = 0, all the extent
6406 * recs have already be copied to the new xh with the
6407 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6408 * increase the refount count int the refcount tree.
6410 * For the xattr which has l_tree_depth > 0, we need
6411 * to initialize it to the empty default value root,
6412 * and then insert the extents one by one.
6414 if (xv
->xr_list
.l_tree_depth
) {
6415 memcpy(new_xv
, &def_xv
, sizeof(def_xv
));
6417 vb
->vb_bh
= value_bh
;
6418 ocfs2_init_xattr_value_extent_tree(&data_et
,
6419 INODE_CACHE(args
->new_inode
), vb
);
6422 clusters
= le32_to_cpu(xv
->xr_clusters
);
6424 while (cpos
< clusters
) {
6425 ret
= ocfs2_xattr_get_clusters(args
->old_inode
,
6438 if (xv
->xr_list
.l_tree_depth
) {
6439 ret
= ocfs2_insert_extent(handle
,
6441 ocfs2_clusters_to_blocks(
6442 args
->old_inode
->i_sb
,
6444 num_clusters
, ext_flags
,
6452 ret
= ocfs2_increase_refcount(handle
, args
->ref_ci
,
6454 p_cluster
, num_clusters
,
6455 meta_ac
, args
->dealloc
);
6461 cpos
+= num_clusters
;
6469 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink
*args
)
6471 int ret
= 0, credits
= 0;
6473 struct ocfs2_super
*osb
= OCFS2_SB(args
->old_inode
->i_sb
);
6474 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)args
->old_bh
->b_data
;
6475 int inline_size
= le16_to_cpu(di
->i_xattr_inline_size
);
6476 int header_off
= osb
->sb
->s_blocksize
- inline_size
;
6477 struct ocfs2_xattr_header
*xh
= (struct ocfs2_xattr_header
*)
6478 (args
->old_bh
->b_data
+ header_off
);
6479 struct ocfs2_xattr_header
*new_xh
= (struct ocfs2_xattr_header
*)
6480 (args
->new_bh
->b_data
+ header_off
);
6481 struct ocfs2_alloc_context
*meta_ac
= NULL
;
6482 struct ocfs2_inode_info
*new_oi
;
6483 struct ocfs2_dinode
*new_di
;
6484 struct ocfs2_xattr_value_buf vb
= {
6485 .vb_bh
= args
->new_bh
,
6486 .vb_access
= ocfs2_journal_access_di
,
6489 ret
= ocfs2_reflink_lock_xattr_allocators(osb
, xh
, args
->ref_root_bh
,
6490 &credits
, &meta_ac
);
6496 handle
= ocfs2_start_trans(osb
, credits
);
6497 if (IS_ERR(handle
)) {
6498 ret
= PTR_ERR(handle
);
6503 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(args
->new_inode
),
6504 args
->new_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
6510 memcpy(args
->new_bh
->b_data
+ header_off
,
6511 args
->old_bh
->b_data
+ header_off
, inline_size
);
6513 new_di
= (struct ocfs2_dinode
*)args
->new_bh
->b_data
;
6514 new_di
->i_xattr_inline_size
= cpu_to_le16(inline_size
);
6516 ret
= ocfs2_reflink_xattr_header(handle
, args
, args
->old_bh
, xh
,
6517 args
->new_bh
, new_xh
, &vb
, meta_ac
,
6518 ocfs2_get_xattr_value_root
, NULL
);
6524 new_oi
= OCFS2_I(args
->new_inode
);
6525 spin_lock(&new_oi
->ip_lock
);
6526 new_oi
->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
| OCFS2_INLINE_XATTR_FL
;
6527 new_di
->i_dyn_features
= cpu_to_le16(new_oi
->ip_dyn_features
);
6528 spin_unlock(&new_oi
->ip_lock
);
6530 ocfs2_journal_dirty(handle
, args
->new_bh
);
6533 ocfs2_commit_trans(osb
, handle
);
6537 ocfs2_free_alloc_context(meta_ac
);
6541 static int ocfs2_create_empty_xattr_block(struct inode
*inode
,
6542 struct buffer_head
*fe_bh
,
6543 struct buffer_head
**ret_bh
,
6547 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6548 struct ocfs2_xattr_set_ctxt ctxt
;
6550 memset(&ctxt
, 0, sizeof(ctxt
));
6551 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, &ctxt
.meta_ac
);
6557 ctxt
.handle
= ocfs2_start_trans(osb
, OCFS2_XATTR_BLOCK_CREATE_CREDITS
);
6558 if (IS_ERR(ctxt
.handle
)) {
6559 ret
= PTR_ERR(ctxt
.handle
);
6564 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6565 (unsigned long long)fe_bh
->b_blocknr
, indexed
);
6566 ret
= ocfs2_create_xattr_block(inode
, fe_bh
, &ctxt
, indexed
,
6571 ocfs2_commit_trans(osb
, ctxt
.handle
);
6573 ocfs2_free_alloc_context(ctxt
.meta_ac
);
6577 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink
*args
,
6578 struct buffer_head
*blk_bh
,
6579 struct buffer_head
*new_blk_bh
)
6581 int ret
= 0, credits
= 0;
6583 struct ocfs2_inode_info
*new_oi
= OCFS2_I(args
->new_inode
);
6584 struct ocfs2_dinode
*new_di
;
6585 struct ocfs2_super
*osb
= OCFS2_SB(args
->new_inode
->i_sb
);
6586 int header_off
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
6587 struct ocfs2_xattr_block
*xb
=
6588 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
6589 struct ocfs2_xattr_header
*xh
= &xb
->xb_attrs
.xb_header
;
6590 struct ocfs2_xattr_block
*new_xb
=
6591 (struct ocfs2_xattr_block
*)new_blk_bh
->b_data
;
6592 struct ocfs2_xattr_header
*new_xh
= &new_xb
->xb_attrs
.xb_header
;
6593 struct ocfs2_alloc_context
*meta_ac
;
6594 struct ocfs2_xattr_value_buf vb
= {
6595 .vb_bh
= new_blk_bh
,
6596 .vb_access
= ocfs2_journal_access_xb
,
6599 ret
= ocfs2_reflink_lock_xattr_allocators(osb
, xh
, args
->ref_root_bh
,
6600 &credits
, &meta_ac
);
6606 /* One more credits in case we need to add xattr flags in new inode. */
6607 handle
= ocfs2_start_trans(osb
, credits
+ 1);
6608 if (IS_ERR(handle
)) {
6609 ret
= PTR_ERR(handle
);
6614 if (!(new_oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
)) {
6615 ret
= ocfs2_journal_access_di(handle
,
6616 INODE_CACHE(args
->new_inode
),
6618 OCFS2_JOURNAL_ACCESS_WRITE
);
6625 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(args
->new_inode
),
6626 new_blk_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
6632 memcpy(new_blk_bh
->b_data
+ header_off
, blk_bh
->b_data
+ header_off
,
6633 osb
->sb
->s_blocksize
- header_off
);
6635 ret
= ocfs2_reflink_xattr_header(handle
, args
, blk_bh
, xh
,
6636 new_blk_bh
, new_xh
, &vb
, meta_ac
,
6637 ocfs2_get_xattr_value_root
, NULL
);
6643 ocfs2_journal_dirty(handle
, new_blk_bh
);
6645 if (!(new_oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
)) {
6646 new_di
= (struct ocfs2_dinode
*)args
->new_bh
->b_data
;
6647 spin_lock(&new_oi
->ip_lock
);
6648 new_oi
->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
;
6649 new_di
->i_dyn_features
= cpu_to_le16(new_oi
->ip_dyn_features
);
6650 spin_unlock(&new_oi
->ip_lock
);
6652 ocfs2_journal_dirty(handle
, args
->new_bh
);
6656 ocfs2_commit_trans(osb
, handle
);
6659 ocfs2_free_alloc_context(meta_ac
);
6663 struct ocfs2_reflink_xattr_tree_args
{
6664 struct ocfs2_xattr_reflink
*reflink
;
6665 struct buffer_head
*old_blk_bh
;
6666 struct buffer_head
*new_blk_bh
;
6667 struct ocfs2_xattr_bucket
*old_bucket
;
6668 struct ocfs2_xattr_bucket
*new_bucket
;
6673 * We have to handle the case that both old bucket and new bucket
6674 * will call this function to get the right ret_bh.
6675 * So The caller must give us the right bh.
6677 static int ocfs2_get_reflink_xattr_value_root(struct super_block
*sb
,
6678 struct buffer_head
*bh
,
6679 struct ocfs2_xattr_header
*xh
,
6681 struct ocfs2_xattr_value_root
**xv
,
6682 struct buffer_head
**ret_bh
,
6685 struct ocfs2_reflink_xattr_tree_args
*args
=
6686 (struct ocfs2_reflink_xattr_tree_args
*)para
;
6687 struct ocfs2_xattr_bucket
*bucket
;
6689 if (bh
== args
->old_bucket
->bu_bhs
[0])
6690 bucket
= args
->old_bucket
;
6692 bucket
= args
->new_bucket
;
6694 return ocfs2_get_xattr_tree_value_root(sb
, bucket
, offset
,
6698 struct ocfs2_value_tree_metas
{
6704 static int ocfs2_value_tree_metas_in_bucket(struct super_block
*sb
,
6705 struct buffer_head
*bh
,
6706 struct ocfs2_xattr_header
*xh
,
6708 struct ocfs2_xattr_value_root
**xv
,
6709 struct buffer_head
**ret_bh
,
6712 struct ocfs2_xattr_bucket
*bucket
=
6713 (struct ocfs2_xattr_bucket
*)para
;
6715 return ocfs2_get_xattr_tree_value_root(sb
, bucket
, offset
,
6719 static int ocfs2_calc_value_tree_metas(struct inode
*inode
,
6720 struct ocfs2_xattr_bucket
*bucket
,
6723 struct ocfs2_value_tree_metas
*metas
=
6724 (struct ocfs2_value_tree_metas
*)para
;
6725 struct ocfs2_xattr_header
*xh
=
6726 (struct ocfs2_xattr_header
*)bucket
->bu_bhs
[0]->b_data
;
6728 /* Add the credits for this bucket first. */
6729 metas
->credits
+= bucket
->bu_blocks
;
6730 return ocfs2_value_metas_in_xattr_header(inode
->i_sb
, bucket
->bu_bhs
[0],
6731 xh
, &metas
->num_metas
,
6732 &metas
->credits
, &metas
->num_recs
,
6733 ocfs2_value_tree_metas_in_bucket
,
6738 * Given a xattr extent rec starting from blkno and having len clusters,
6739 * iterate all the buckets calculate how much metadata we need for reflinking
6740 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6742 static int ocfs2_lock_reflink_xattr_rec_allocators(
6743 struct ocfs2_reflink_xattr_tree_args
*args
,
6744 struct ocfs2_extent_tree
*xt_et
,
6745 u64 blkno
, u32 len
, int *credits
,
6746 struct ocfs2_alloc_context
**meta_ac
,
6747 struct ocfs2_alloc_context
**data_ac
)
6749 int ret
, num_free_extents
;
6750 struct ocfs2_value_tree_metas metas
;
6751 struct ocfs2_super
*osb
= OCFS2_SB(args
->reflink
->old_inode
->i_sb
);
6752 struct ocfs2_refcount_block
*rb
;
6754 memset(&metas
, 0, sizeof(metas
));
6756 ret
= ocfs2_iterate_xattr_buckets(args
->reflink
->old_inode
, blkno
, len
,
6757 ocfs2_calc_value_tree_metas
, &metas
);
6763 *credits
= metas
.credits
;
6766 * Calculate we need for refcount tree change.
6768 * We need to add/modify num_recs in refcount tree, so just calculate
6769 * an approximate number we need for refcount tree change.
6770 * Sometimes we need to split the tree, and after split, half recs
6771 * will be moved to the new block, and a new block can only provide
6772 * half number of recs. So we multiple new blocks by 2.
6773 * In the end, we have to add credits for modifying the already
6774 * existed refcount block.
6776 rb
= (struct ocfs2_refcount_block
*)args
->reflink
->ref_root_bh
->b_data
;
6778 (metas
.num_recs
+ ocfs2_refcount_recs_per_rb(osb
->sb
) - 1) /
6779 ocfs2_refcount_recs_per_rb(osb
->sb
) * 2;
6780 metas
.num_metas
+= metas
.num_recs
;
6781 *credits
+= metas
.num_recs
+
6782 metas
.num_recs
* OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
6783 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)
6784 *credits
+= le16_to_cpu(rb
->rf_list
.l_tree_depth
) *
6785 le16_to_cpu(rb
->rf_list
.l_next_free_rec
) + 1;
6789 /* count in the xattr tree change. */
6790 num_free_extents
= ocfs2_num_free_extents(osb
, xt_et
);
6791 if (num_free_extents
< 0) {
6792 ret
= num_free_extents
;
6797 if (num_free_extents
< len
)
6798 metas
.num_metas
+= ocfs2_extend_meta_needed(xt_et
->et_root_el
);
6800 *credits
+= ocfs2_calc_extend_credits(osb
->sb
,
6801 xt_et
->et_root_el
, len
);
6803 if (metas
.num_metas
) {
6804 ret
= ocfs2_reserve_new_metadata_blocks(osb
, metas
.num_metas
,
6813 ret
= ocfs2_reserve_clusters(osb
, len
, data_ac
);
6820 ocfs2_free_alloc_context(*meta_ac
);
6828 static int ocfs2_reflink_xattr_buckets(handle_t
*handle
,
6829 u64 blkno
, u64 new_blkno
, u32 clusters
,
6830 struct ocfs2_alloc_context
*meta_ac
,
6831 struct ocfs2_alloc_context
*data_ac
,
6832 struct ocfs2_reflink_xattr_tree_args
*args
)
6835 struct super_block
*sb
= args
->reflink
->old_inode
->i_sb
;
6836 u32 bpc
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb
));
6837 u32 num_buckets
= clusters
* bpc
;
6838 int bpb
= args
->old_bucket
->bu_blocks
;
6839 struct ocfs2_xattr_value_buf vb
= {
6840 .vb_access
= ocfs2_journal_access
,
6843 for (i
= 0; i
< num_buckets
; i
++, blkno
+= bpb
, new_blkno
+= bpb
) {
6844 ret
= ocfs2_read_xattr_bucket(args
->old_bucket
, blkno
);
6850 ret
= ocfs2_init_xattr_bucket(args
->new_bucket
, new_blkno
);
6857 * The real bucket num in this series of blocks is stored
6858 * in the 1st bucket.
6861 num_buckets
= le16_to_cpu(
6862 bucket_xh(args
->old_bucket
)->xh_num_buckets
);
6864 ret
= ocfs2_xattr_bucket_journal_access(handle
,
6866 OCFS2_JOURNAL_ACCESS_CREATE
);
6872 for (j
= 0; j
< bpb
; j
++)
6873 memcpy(bucket_block(args
->new_bucket
, j
),
6874 bucket_block(args
->old_bucket
, j
),
6877 ocfs2_xattr_bucket_journal_dirty(handle
, args
->new_bucket
);
6879 ret
= ocfs2_reflink_xattr_header(handle
, args
->reflink
,
6880 args
->old_bucket
->bu_bhs
[0],
6881 bucket_xh(args
->old_bucket
),
6882 args
->new_bucket
->bu_bhs
[0],
6883 bucket_xh(args
->new_bucket
),
6885 ocfs2_get_reflink_xattr_value_root
,
6893 * Re-access and dirty the bucket to calculate metaecc.
6894 * Because we may extend the transaction in reflink_xattr_header
6895 * which will let the already accessed block gone.
6897 ret
= ocfs2_xattr_bucket_journal_access(handle
,
6899 OCFS2_JOURNAL_ACCESS_WRITE
);
6905 ocfs2_xattr_bucket_journal_dirty(handle
, args
->new_bucket
);
6906 ocfs2_xattr_bucket_relse(args
->old_bucket
);
6907 ocfs2_xattr_bucket_relse(args
->new_bucket
);
6910 ocfs2_xattr_bucket_relse(args
->old_bucket
);
6911 ocfs2_xattr_bucket_relse(args
->new_bucket
);
6915 * Create the same xattr extent record in the new inode's xattr tree.
6917 static int ocfs2_reflink_xattr_rec(struct inode
*inode
,
6918 struct buffer_head
*root_bh
,
6924 int ret
, credits
= 0;
6925 u32 p_cluster
, num_clusters
;
6928 struct ocfs2_reflink_xattr_tree_args
*args
=
6929 (struct ocfs2_reflink_xattr_tree_args
*)para
;
6930 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6931 struct ocfs2_alloc_context
*meta_ac
= NULL
;
6932 struct ocfs2_alloc_context
*data_ac
= NULL
;
6933 struct ocfs2_extent_tree et
;
6935 ocfs2_init_xattr_tree_extent_tree(&et
,
6936 INODE_CACHE(args
->reflink
->new_inode
),
6939 ret
= ocfs2_lock_reflink_xattr_rec_allocators(args
, &et
, blkno
,
6941 &meta_ac
, &data_ac
);
6947 handle
= ocfs2_start_trans(osb
, credits
);
6948 if (IS_ERR(handle
)) {
6949 ret
= PTR_ERR(handle
);
6954 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
,
6955 len
, &p_cluster
, &num_clusters
);
6961 new_blkno
= ocfs2_clusters_to_blocks(osb
->sb
, p_cluster
);
6963 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6964 (unsigned long long)blkno
, (unsigned long long)new_blkno
, len
);
6965 ret
= ocfs2_reflink_xattr_buckets(handle
, blkno
, new_blkno
, len
,
6966 meta_ac
, data_ac
, args
);
6972 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6973 (unsigned long long)new_blkno
, len
, cpos
);
6974 ret
= ocfs2_insert_extent(handle
, &et
, cpos
, new_blkno
,
6980 ocfs2_commit_trans(osb
, handle
);
6984 ocfs2_free_alloc_context(meta_ac
);
6986 ocfs2_free_alloc_context(data_ac
);
6991 * Create reflinked xattr buckets.
6992 * We will add bucket one by one, and refcount all the xattrs in the bucket
6993 * if they are stored outside.
6995 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink
*args
,
6996 struct buffer_head
*blk_bh
,
6997 struct buffer_head
*new_blk_bh
)
7000 struct ocfs2_reflink_xattr_tree_args para
;
7002 memset(¶
, 0, sizeof(para
));
7003 para
.reflink
= args
;
7004 para
.old_blk_bh
= blk_bh
;
7005 para
.new_blk_bh
= new_blk_bh
;
7007 para
.old_bucket
= ocfs2_xattr_bucket_new(args
->old_inode
);
7008 if (!para
.old_bucket
) {
7009 mlog_errno(-ENOMEM
);
7013 para
.new_bucket
= ocfs2_xattr_bucket_new(args
->new_inode
);
7014 if (!para
.new_bucket
) {
7020 ret
= ocfs2_iterate_xattr_index_block(args
->old_inode
, blk_bh
,
7021 ocfs2_reflink_xattr_rec
,
7027 ocfs2_xattr_bucket_free(para
.old_bucket
);
7028 ocfs2_xattr_bucket_free(para
.new_bucket
);
7032 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink
*args
,
7033 struct buffer_head
*blk_bh
)
7035 int ret
, indexed
= 0;
7036 struct buffer_head
*new_blk_bh
= NULL
;
7037 struct ocfs2_xattr_block
*xb
=
7038 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
7041 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)
7044 ret
= ocfs2_create_empty_xattr_block(args
->new_inode
, args
->new_bh
,
7045 &new_blk_bh
, indexed
);
7051 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
))
7052 ret
= ocfs2_reflink_xattr_block(args
, blk_bh
, new_blk_bh
);
7054 ret
= ocfs2_reflink_xattr_tree(args
, blk_bh
, new_blk_bh
);
7063 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry
*xe
)
7065 int type
= ocfs2_xattr_get_type(xe
);
7067 return type
!= OCFS2_XATTR_INDEX_SECURITY
&&
7068 type
!= OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
&&
7069 type
!= OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
;
7072 int ocfs2_reflink_xattrs(struct inode
*old_inode
,
7073 struct buffer_head
*old_bh
,
7074 struct inode
*new_inode
,
7075 struct buffer_head
*new_bh
,
7076 bool preserve_security
)
7079 struct ocfs2_xattr_reflink args
;
7080 struct ocfs2_inode_info
*oi
= OCFS2_I(old_inode
);
7081 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)old_bh
->b_data
;
7082 struct buffer_head
*blk_bh
= NULL
;
7083 struct ocfs2_cached_dealloc_ctxt dealloc
;
7084 struct ocfs2_refcount_tree
*ref_tree
;
7085 struct buffer_head
*ref_root_bh
= NULL
;
7087 ret
= ocfs2_lock_refcount_tree(OCFS2_SB(old_inode
->i_sb
),
7088 le64_to_cpu(di
->i_refcount_loc
),
7089 1, &ref_tree
, &ref_root_bh
);
7095 ocfs2_init_dealloc_ctxt(&dealloc
);
7097 args
.old_inode
= old_inode
;
7098 args
.new_inode
= new_inode
;
7099 args
.old_bh
= old_bh
;
7100 args
.new_bh
= new_bh
;
7101 args
.ref_ci
= &ref_tree
->rf_ci
;
7102 args
.ref_root_bh
= ref_root_bh
;
7103 args
.dealloc
= &dealloc
;
7104 if (preserve_security
)
7105 args
.xattr_reflinked
= NULL
;
7107 args
.xattr_reflinked
= ocfs2_reflink_xattr_no_security
;
7109 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
7110 ret
= ocfs2_reflink_xattr_inline(&args
);
7117 if (!di
->i_xattr_loc
)
7120 ret
= ocfs2_read_xattr_block(old_inode
, le64_to_cpu(di
->i_xattr_loc
),
7127 ret
= ocfs2_reflink_xattr_in_block(&args
, blk_bh
);
7134 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode
->i_sb
),
7136 brelse(ref_root_bh
);
7138 if (ocfs2_dealloc_has_cluster(&dealloc
)) {
7139 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode
->i_sb
), 1);
7140 ocfs2_run_deallocs(OCFS2_SB(old_inode
->i_sb
), &dealloc
);
7148 * Initialize security and acl for a already created inode.
7149 * Used for reflink a non-preserve-security file.
7151 * It uses common api like ocfs2_xattr_set, so the caller
7152 * must not hold any lock expect i_mutex.
7154 int ocfs2_init_security_and_acl(struct inode
*dir
,
7155 struct inode
*inode
)
7158 struct buffer_head
*dir_bh
= NULL
;
7159 struct ocfs2_security_xattr_info si
= {
7163 ret
= ocfs2_init_security_get(inode
, dir
, &si
);
7165 ret
= ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_SECURITY
,
7166 si
.name
, si
.value
, si
.value_len
,
7172 } else if (ret
!= -EOPNOTSUPP
) {
7177 ret
= ocfs2_inode_lock(dir
, &dir_bh
, 0);
7183 ret
= ocfs2_init_acl(NULL
, inode
, dir
, NULL
, dir_bh
, NULL
, NULL
);
7187 ocfs2_inode_unlock(dir
, 0);
7193 * 'security' attributes support
7195 static size_t ocfs2_xattr_security_list(struct dentry
*dentry
, char *list
,
7196 size_t list_size
, const char *name
,
7197 size_t name_len
, int type
)
7199 const size_t prefix_len
= XATTR_SECURITY_PREFIX_LEN
;
7200 const size_t total_len
= prefix_len
+ name_len
+ 1;
7202 if (list
&& total_len
<= list_size
) {
7203 memcpy(list
, XATTR_SECURITY_PREFIX
, prefix_len
);
7204 memcpy(list
+ prefix_len
, name
, name_len
);
7205 list
[prefix_len
+ name_len
] = '\0';
7210 static int ocfs2_xattr_security_get(struct dentry
*dentry
, const char *name
,
7211 void *buffer
, size_t size
, int type
)
7213 if (strcmp(name
, "") == 0)
7215 return ocfs2_xattr_get(dentry
->d_inode
, OCFS2_XATTR_INDEX_SECURITY
,
7216 name
, buffer
, size
);
7219 static int ocfs2_xattr_security_set(struct dentry
*dentry
, const char *name
,
7220 const void *value
, size_t size
, int flags
, int type
)
7222 if (strcmp(name
, "") == 0)
7225 return ocfs2_xattr_set(dentry
->d_inode
, OCFS2_XATTR_INDEX_SECURITY
,
7226 name
, value
, size
, flags
);
7229 int ocfs2_init_security_get(struct inode
*inode
,
7231 struct ocfs2_security_xattr_info
*si
)
7233 /* check whether ocfs2 support feature xattr */
7234 if (!ocfs2_supports_xattr(OCFS2_SB(dir
->i_sb
)))
7236 return security_inode_init_security(inode
, dir
, &si
->name
, &si
->value
,
7240 int ocfs2_init_security_set(handle_t
*handle
,
7241 struct inode
*inode
,
7242 struct buffer_head
*di_bh
,
7243 struct ocfs2_security_xattr_info
*si
,
7244 struct ocfs2_alloc_context
*xattr_ac
,
7245 struct ocfs2_alloc_context
*data_ac
)
7247 return ocfs2_xattr_set_handle(handle
, inode
, di_bh
,
7248 OCFS2_XATTR_INDEX_SECURITY
,
7249 si
->name
, si
->value
, si
->value_len
, 0,
7253 struct xattr_handler ocfs2_xattr_security_handler
= {
7254 .prefix
= XATTR_SECURITY_PREFIX
,
7255 .list
= ocfs2_xattr_security_list
,
7256 .get
= ocfs2_xattr_security_get
,
7257 .set
= ocfs2_xattr_security_set
,
7261 * 'trusted' attributes support
7263 static size_t ocfs2_xattr_trusted_list(struct dentry
*dentry
, char *list
,
7264 size_t list_size
, const char *name
,
7265 size_t name_len
, int type
)
7267 const size_t prefix_len
= XATTR_TRUSTED_PREFIX_LEN
;
7268 const size_t total_len
= prefix_len
+ name_len
+ 1;
7270 if (list
&& total_len
<= list_size
) {
7271 memcpy(list
, XATTR_TRUSTED_PREFIX
, prefix_len
);
7272 memcpy(list
+ prefix_len
, name
, name_len
);
7273 list
[prefix_len
+ name_len
] = '\0';
7278 static int ocfs2_xattr_trusted_get(struct dentry
*dentry
, const char *name
,
7279 void *buffer
, size_t size
, int type
)
7281 if (strcmp(name
, "") == 0)
7283 return ocfs2_xattr_get(dentry
->d_inode
, OCFS2_XATTR_INDEX_TRUSTED
,
7284 name
, buffer
, size
);
7287 static int ocfs2_xattr_trusted_set(struct dentry
*dentry
, const char *name
,
7288 const void *value
, size_t size
, int flags
, int type
)
7290 if (strcmp(name
, "") == 0)
7293 return ocfs2_xattr_set(dentry
->d_inode
, OCFS2_XATTR_INDEX_TRUSTED
,
7294 name
, value
, size
, flags
);
7297 struct xattr_handler ocfs2_xattr_trusted_handler
= {
7298 .prefix
= XATTR_TRUSTED_PREFIX
,
7299 .list
= ocfs2_xattr_trusted_list
,
7300 .get
= ocfs2_xattr_trusted_get
,
7301 .set
= ocfs2_xattr_trusted_set
,
7305 * 'user' attributes support
7307 static size_t ocfs2_xattr_user_list(struct dentry
*dentry
, char *list
,
7308 size_t list_size
, const char *name
,
7309 size_t name_len
, int type
)
7311 const size_t prefix_len
= XATTR_USER_PREFIX_LEN
;
7312 const size_t total_len
= prefix_len
+ name_len
+ 1;
7313 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
7315 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7318 if (list
&& total_len
<= list_size
) {
7319 memcpy(list
, XATTR_USER_PREFIX
, prefix_len
);
7320 memcpy(list
+ prefix_len
, name
, name_len
);
7321 list
[prefix_len
+ name_len
] = '\0';
7326 static int ocfs2_xattr_user_get(struct dentry
*dentry
, const char *name
,
7327 void *buffer
, size_t size
, int type
)
7329 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
7331 if (strcmp(name
, "") == 0)
7333 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7335 return ocfs2_xattr_get(dentry
->d_inode
, OCFS2_XATTR_INDEX_USER
, name
,
7339 static int ocfs2_xattr_user_set(struct dentry
*dentry
, const char *name
,
7340 const void *value
, size_t size
, int flags
, int type
)
7342 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
7344 if (strcmp(name
, "") == 0)
7346 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7349 return ocfs2_xattr_set(dentry
->d_inode
, OCFS2_XATTR_INDEX_USER
,
7350 name
, value
, size
, flags
);
7353 struct xattr_handler ocfs2_xattr_user_handler
= {
7354 .prefix
= XATTR_USER_PREFIX
,
7355 .list
= ocfs2_xattr_user_list
,
7356 .get
= ocfs2_xattr_user_get
,
7357 .set
= ocfs2_xattr_user_set
,