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 #include <cluster/masklog.h>
44 #include "blockcheck.h"
54 #include "buffer_head_io.h"
57 #include "refcounttree.h"
59 #include "ocfs2_trace.h"
61 struct ocfs2_xattr_def_value_root
{
62 struct ocfs2_xattr_value_root xv
;
63 struct ocfs2_extent_rec er
;
66 struct ocfs2_xattr_bucket
{
67 /* The inode these xattrs are associated with */
68 struct inode
*bu_inode
;
70 /* The actual buffers that make up the bucket */
71 struct buffer_head
*bu_bhs
[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
];
73 /* How many blocks make up one bucket for this filesystem */
77 struct ocfs2_xattr_set_ctxt
{
79 struct ocfs2_alloc_context
*meta_ac
;
80 struct ocfs2_alloc_context
*data_ac
;
81 struct ocfs2_cached_dealloc_ctxt dealloc
;
85 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
86 #define OCFS2_XATTR_INLINE_SIZE 80
87 #define OCFS2_XATTR_HEADER_GAP 4
88 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
89 - sizeof(struct ocfs2_xattr_header) \
90 - OCFS2_XATTR_HEADER_GAP)
91 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
92 - sizeof(struct ocfs2_xattr_block) \
93 - sizeof(struct ocfs2_xattr_header) \
94 - OCFS2_XATTR_HEADER_GAP)
96 static struct ocfs2_xattr_def_value_root def_xv
= {
97 .xv
.xr_list
.l_count
= cpu_to_le16(1),
100 const struct xattr_handler
*ocfs2_xattr_handlers
[] = {
101 &ocfs2_xattr_user_handler
,
102 &posix_acl_access_xattr_handler
,
103 &posix_acl_default_xattr_handler
,
104 &ocfs2_xattr_trusted_handler
,
105 &ocfs2_xattr_security_handler
,
109 static const struct xattr_handler
*ocfs2_xattr_handler_map
[OCFS2_XATTR_MAX
] = {
110 [OCFS2_XATTR_INDEX_USER
] = &ocfs2_xattr_user_handler
,
111 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
]
112 = &posix_acl_access_xattr_handler
,
113 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
]
114 = &posix_acl_default_xattr_handler
,
115 [OCFS2_XATTR_INDEX_TRUSTED
] = &ocfs2_xattr_trusted_handler
,
116 [OCFS2_XATTR_INDEX_SECURITY
] = &ocfs2_xattr_security_handler
,
119 struct ocfs2_xattr_info
{
123 const void *xi_value
;
127 struct ocfs2_xattr_search
{
128 struct buffer_head
*inode_bh
;
130 * xattr_bh point to the block buffer head which has extended attribute
131 * when extended attribute in inode, xattr_bh is equal to inode_bh.
133 struct buffer_head
*xattr_bh
;
134 struct ocfs2_xattr_header
*header
;
135 struct ocfs2_xattr_bucket
*bucket
;
138 struct ocfs2_xattr_entry
*here
;
142 /* Operations on struct ocfs2_xa_entry */
144 struct ocfs2_xa_loc_operations
{
148 int (*xlo_journal_access
)(handle_t
*handle
, struct ocfs2_xa_loc
*loc
,
150 void (*xlo_journal_dirty
)(handle_t
*handle
, struct ocfs2_xa_loc
*loc
);
153 * Return a pointer to the appropriate buffer in loc->xl_storage
154 * at the given offset from loc->xl_header.
156 void *(*xlo_offset_pointer
)(struct ocfs2_xa_loc
*loc
, int offset
);
158 /* Can we reuse the existing entry for the new value? */
159 int (*xlo_can_reuse
)(struct ocfs2_xa_loc
*loc
,
160 struct ocfs2_xattr_info
*xi
);
162 /* How much space is needed for the new value? */
163 int (*xlo_check_space
)(struct ocfs2_xa_loc
*loc
,
164 struct ocfs2_xattr_info
*xi
);
167 * Return the offset of the first name+value pair. This is
168 * the start of our downward-filling free space.
170 int (*xlo_get_free_start
)(struct ocfs2_xa_loc
*loc
);
173 * Remove the name+value at this location. Do whatever is
174 * appropriate with the remaining name+value pairs.
176 void (*xlo_wipe_namevalue
)(struct ocfs2_xa_loc
*loc
);
178 /* Fill xl_entry with a new entry */
179 void (*xlo_add_entry
)(struct ocfs2_xa_loc
*loc
, u32 name_hash
);
181 /* Add name+value storage to an entry */
182 void (*xlo_add_namevalue
)(struct ocfs2_xa_loc
*loc
, int size
);
185 * Initialize the value buf's access and bh fields for this entry.
186 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
188 void (*xlo_fill_value_buf
)(struct ocfs2_xa_loc
*loc
,
189 struct ocfs2_xattr_value_buf
*vb
);
193 * Describes an xattr entry location. This is a memory structure
194 * tracking the on-disk structure.
196 struct ocfs2_xa_loc
{
197 /* This xattr belongs to this inode */
198 struct inode
*xl_inode
;
200 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
201 struct ocfs2_xattr_header
*xl_header
;
203 /* Bytes from xl_header to the end of the storage */
207 * The ocfs2_xattr_entry this location describes. If this is
208 * NULL, this location describes the on-disk structure where it
211 struct ocfs2_xattr_entry
*xl_entry
;
214 * Internal housekeeping
217 /* Buffer(s) containing this entry */
220 /* Operations on the storage backing this location */
221 const struct ocfs2_xa_loc_operations
*xl_ops
;
225 * Convenience functions to calculate how much space is needed for a
226 * given name+value pair
228 static int namevalue_size(int name_len
, uint64_t value_len
)
230 if (value_len
> OCFS2_XATTR_INLINE_SIZE
)
231 return OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
233 return OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_SIZE(value_len
);
236 static int namevalue_size_xi(struct ocfs2_xattr_info
*xi
)
238 return namevalue_size(xi
->xi_name_len
, xi
->xi_value_len
);
241 static int namevalue_size_xe(struct ocfs2_xattr_entry
*xe
)
243 u64 value_len
= le64_to_cpu(xe
->xe_value_size
);
245 BUG_ON((value_len
> OCFS2_XATTR_INLINE_SIZE
) &&
246 ocfs2_xattr_is_local(xe
));
247 return namevalue_size(xe
->xe_name_len
, value_len
);
251 static int ocfs2_xattr_bucket_get_name_value(struct super_block
*sb
,
252 struct ocfs2_xattr_header
*xh
,
257 static int ocfs2_xattr_block_find(struct inode
*inode
,
260 struct ocfs2_xattr_search
*xs
);
261 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
262 struct buffer_head
*root_bh
,
265 struct ocfs2_xattr_search
*xs
);
267 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
268 struct buffer_head
*blk_bh
,
272 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
273 struct ocfs2_xattr_search
*xs
,
274 struct ocfs2_xattr_set_ctxt
*ctxt
);
276 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
277 struct ocfs2_xattr_info
*xi
,
278 struct ocfs2_xattr_search
*xs
,
279 struct ocfs2_xattr_set_ctxt
*ctxt
);
281 typedef int (xattr_tree_rec_func
)(struct inode
*inode
,
282 struct buffer_head
*root_bh
,
283 u64 blkno
, u32 cpos
, u32 len
, void *para
);
284 static int ocfs2_iterate_xattr_index_block(struct inode
*inode
,
285 struct buffer_head
*root_bh
,
286 xattr_tree_rec_func
*rec_func
,
288 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
289 struct ocfs2_xattr_bucket
*bucket
,
291 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
292 struct buffer_head
*root_bh
,
298 static int ocfs2_mv_xattr_buckets(struct inode
*inode
, handle_t
*handle
,
299 u64 src_blk
, u64 last_blk
, u64 to_blk
,
300 unsigned int start_bucket
,
302 static int ocfs2_prepare_refcount_xattr(struct inode
*inode
,
303 struct ocfs2_dinode
*di
,
304 struct ocfs2_xattr_info
*xi
,
305 struct ocfs2_xattr_search
*xis
,
306 struct ocfs2_xattr_search
*xbs
,
307 struct ocfs2_refcount_tree
**ref_tree
,
310 static int ocfs2_get_xattr_tree_value_root(struct super_block
*sb
,
311 struct ocfs2_xattr_bucket
*bucket
,
313 struct ocfs2_xattr_value_root
**xv
,
314 struct buffer_head
**bh
);
316 static inline u16
ocfs2_xattr_buckets_per_cluster(struct ocfs2_super
*osb
)
318 return (1 << osb
->s_clustersize_bits
) / OCFS2_XATTR_BUCKET_SIZE
;
321 static inline u16
ocfs2_blocks_per_xattr_bucket(struct super_block
*sb
)
323 return OCFS2_XATTR_BUCKET_SIZE
/ (1 << sb
->s_blocksize_bits
);
326 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
327 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
328 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
330 static struct ocfs2_xattr_bucket
*ocfs2_xattr_bucket_new(struct inode
*inode
)
332 struct ocfs2_xattr_bucket
*bucket
;
333 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
335 BUG_ON(blks
> OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
);
337 bucket
= kzalloc(sizeof(struct ocfs2_xattr_bucket
), GFP_NOFS
);
339 bucket
->bu_inode
= inode
;
340 bucket
->bu_blocks
= blks
;
346 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket
*bucket
)
350 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
351 brelse(bucket
->bu_bhs
[i
]);
352 bucket
->bu_bhs
[i
] = NULL
;
356 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket
*bucket
)
359 ocfs2_xattr_bucket_relse(bucket
);
360 bucket
->bu_inode
= NULL
;
366 * A bucket that has never been written to disk doesn't need to be
367 * read. We just need the buffer_heads. Don't call this for
368 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
371 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
372 u64 xb_blkno
, int new)
376 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
377 bucket
->bu_bhs
[i
] = sb_getblk(bucket
->bu_inode
->i_sb
,
379 if (!bucket
->bu_bhs
[i
]) {
385 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket
->bu_inode
),
386 bucket
->bu_bhs
[i
])) {
388 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket
->bu_inode
),
391 set_buffer_uptodate(bucket
->bu_bhs
[i
]);
392 ocfs2_set_buffer_uptodate(INODE_CACHE(bucket
->bu_inode
),
399 ocfs2_xattr_bucket_relse(bucket
);
403 /* Read the xattr bucket at xb_blkno */
404 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
409 rc
= ocfs2_read_blocks(INODE_CACHE(bucket
->bu_inode
), xb_blkno
,
410 bucket
->bu_blocks
, bucket
->bu_bhs
, 0,
413 spin_lock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
414 rc
= ocfs2_validate_meta_ecc_bhs(bucket
->bu_inode
->i_sb
,
417 &bucket_xh(bucket
)->xh_check
);
418 spin_unlock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
424 ocfs2_xattr_bucket_relse(bucket
);
428 static int ocfs2_xattr_bucket_journal_access(handle_t
*handle
,
429 struct ocfs2_xattr_bucket
*bucket
,
434 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
435 rc
= ocfs2_journal_access(handle
,
436 INODE_CACHE(bucket
->bu_inode
),
437 bucket
->bu_bhs
[i
], type
);
447 static void ocfs2_xattr_bucket_journal_dirty(handle_t
*handle
,
448 struct ocfs2_xattr_bucket
*bucket
)
452 spin_lock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
453 ocfs2_compute_meta_ecc_bhs(bucket
->bu_inode
->i_sb
,
454 bucket
->bu_bhs
, bucket
->bu_blocks
,
455 &bucket_xh(bucket
)->xh_check
);
456 spin_unlock(&OCFS2_SB(bucket
->bu_inode
->i_sb
)->osb_xattr_lock
);
458 for (i
= 0; i
< bucket
->bu_blocks
; i
++)
459 ocfs2_journal_dirty(handle
, bucket
->bu_bhs
[i
]);
462 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket
*dest
,
463 struct ocfs2_xattr_bucket
*src
)
466 int blocksize
= src
->bu_inode
->i_sb
->s_blocksize
;
468 BUG_ON(dest
->bu_blocks
!= src
->bu_blocks
);
469 BUG_ON(dest
->bu_inode
!= src
->bu_inode
);
471 for (i
= 0; i
< src
->bu_blocks
; i
++) {
472 memcpy(bucket_block(dest
, i
), bucket_block(src
, i
),
477 static int ocfs2_validate_xattr_block(struct super_block
*sb
,
478 struct buffer_head
*bh
)
481 struct ocfs2_xattr_block
*xb
=
482 (struct ocfs2_xattr_block
*)bh
->b_data
;
484 trace_ocfs2_validate_xattr_block((unsigned long long)bh
->b_blocknr
);
486 BUG_ON(!buffer_uptodate(bh
));
489 * If the ecc fails, we return the error but otherwise
490 * leave the filesystem running. We know any error is
491 * local to this block.
493 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &xb
->xb_check
);
498 * Errors after here are fatal
501 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
502 return ocfs2_error(sb
,
503 "Extended attribute block #%llu has bad signature %.*s\n",
504 (unsigned long long)bh
->b_blocknr
, 7,
508 if (le64_to_cpu(xb
->xb_blkno
) != bh
->b_blocknr
) {
509 return ocfs2_error(sb
,
510 "Extended attribute block #%llu has an invalid xb_blkno of %llu\n",
511 (unsigned long long)bh
->b_blocknr
,
512 (unsigned long long)le64_to_cpu(xb
->xb_blkno
));
515 if (le32_to_cpu(xb
->xb_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
516 return ocfs2_error(sb
,
517 "Extended attribute block #%llu has an invalid xb_fs_generation of #%u\n",
518 (unsigned long long)bh
->b_blocknr
,
519 le32_to_cpu(xb
->xb_fs_generation
));
525 static int ocfs2_read_xattr_block(struct inode
*inode
, u64 xb_blkno
,
526 struct buffer_head
**bh
)
529 struct buffer_head
*tmp
= *bh
;
531 rc
= ocfs2_read_block(INODE_CACHE(inode
), xb_blkno
, &tmp
,
532 ocfs2_validate_xattr_block
);
534 /* If ocfs2_read_block() got us a new bh, pass it up. */
541 static inline const char *ocfs2_xattr_prefix(int name_index
)
543 const struct xattr_handler
*handler
= NULL
;
545 if (name_index
> 0 && name_index
< OCFS2_XATTR_MAX
)
546 handler
= ocfs2_xattr_handler_map
[name_index
];
548 return handler
? handler
->prefix
: NULL
;
551 static u32
ocfs2_xattr_name_hash(struct inode
*inode
,
555 /* Get hash value of uuid from super block */
556 u32 hash
= OCFS2_SB(inode
->i_sb
)->uuid_hash
;
559 /* hash extended attribute name */
560 for (i
= 0; i
< name_len
; i
++) {
561 hash
= (hash
<< OCFS2_HASH_SHIFT
) ^
562 (hash
>> (8*sizeof(hash
) - OCFS2_HASH_SHIFT
)) ^
569 static int ocfs2_xattr_entry_real_size(int name_len
, size_t value_len
)
571 return namevalue_size(name_len
, value_len
) +
572 sizeof(struct ocfs2_xattr_entry
);
575 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info
*xi
)
577 return namevalue_size_xi(xi
) +
578 sizeof(struct ocfs2_xattr_entry
);
581 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry
*xe
)
583 return namevalue_size_xe(xe
) +
584 sizeof(struct ocfs2_xattr_entry
);
587 int ocfs2_calc_security_init(struct inode
*dir
,
588 struct ocfs2_security_xattr_info
*si
,
591 struct ocfs2_alloc_context
**xattr_ac
)
594 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
595 int s_size
= ocfs2_xattr_entry_real_size(strlen(si
->name
),
599 * The max space of security xattr taken inline is
600 * 256(name) + 80(value) + 16(entry) = 352 bytes,
601 * So reserve one metadata block for it is ok.
603 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
||
604 s_size
> OCFS2_XATTR_FREE_IN_IBODY
) {
605 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, xattr_ac
);
610 *xattr_credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
613 /* reserve clusters for xattr value which will be set in B tree*/
614 if (si
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
615 int new_clusters
= ocfs2_clusters_for_bytes(dir
->i_sb
,
618 *xattr_credits
+= ocfs2_clusters_to_blocks(dir
->i_sb
,
620 *want_clusters
+= new_clusters
;
625 int ocfs2_calc_xattr_init(struct inode
*dir
,
626 struct buffer_head
*dir_bh
,
628 struct ocfs2_security_xattr_info
*si
,
634 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
635 int s_size
= 0, a_size
= 0, acl_len
= 0, new_clusters
;
638 s_size
= ocfs2_xattr_entry_real_size(strlen(si
->name
),
641 if (osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
) {
642 acl_len
= ocfs2_xattr_get_nolock(dir
, dir_bh
,
643 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
,
646 a_size
= ocfs2_xattr_entry_real_size(0, acl_len
);
649 } else if (acl_len
!= 0 && acl_len
!= -ENODATA
) {
655 if (!(s_size
+ a_size
))
659 * The max space of security xattr taken inline is
660 * 256(name) + 80(value) + 16(entry) = 352 bytes,
661 * The max space of acl xattr taken inline is
662 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
663 * when blocksize = 512, may reserve one more cluser for
664 * xattr bucket, otherwise reserve one metadata block
666 * If this is a new directory with inline data,
667 * we choose to reserve the entire inline area for
668 * directory contents and force an external xattr block.
670 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
||
671 (S_ISDIR(mode
) && ocfs2_supports_inline_data(osb
)) ||
672 (s_size
+ a_size
) > OCFS2_XATTR_FREE_IN_IBODY
) {
673 *want_meta
= *want_meta
+ 1;
674 *xattr_credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
677 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
&&
678 (s_size
+ a_size
) > OCFS2_XATTR_FREE_IN_BLOCK(dir
)) {
680 *xattr_credits
+= ocfs2_blocks_per_xattr_bucket(dir
->i_sb
);
684 * reserve credits and clusters for xattrs which has large value
685 * and have to be set outside
687 if (si
->enable
&& si
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
688 new_clusters
= ocfs2_clusters_for_bytes(dir
->i_sb
,
690 *xattr_credits
+= ocfs2_clusters_to_blocks(dir
->i_sb
,
692 *want_clusters
+= new_clusters
;
694 if (osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
&&
695 acl_len
> OCFS2_XATTR_INLINE_SIZE
) {
696 /* for directory, it has DEFAULT and ACCESS two types of acls */
697 new_clusters
= (S_ISDIR(mode
) ? 2 : 1) *
698 ocfs2_clusters_for_bytes(dir
->i_sb
, acl_len
);
699 *xattr_credits
+= ocfs2_clusters_to_blocks(dir
->i_sb
,
701 *want_clusters
+= new_clusters
;
707 static int ocfs2_xattr_extend_allocation(struct inode
*inode
,
709 struct ocfs2_xattr_value_buf
*vb
,
710 struct ocfs2_xattr_set_ctxt
*ctxt
)
712 int status
= 0, credits
;
713 handle_t
*handle
= ctxt
->handle
;
714 enum ocfs2_alloc_restarted why
;
715 u32 prev_clusters
, logical_start
= le32_to_cpu(vb
->vb_xv
->xr_clusters
);
716 struct ocfs2_extent_tree et
;
718 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
720 while (clusters_to_add
) {
721 trace_ocfs2_xattr_extend_allocation(clusters_to_add
);
723 status
= vb
->vb_access(handle
, INODE_CACHE(inode
), vb
->vb_bh
,
724 OCFS2_JOURNAL_ACCESS_WRITE
);
730 prev_clusters
= le32_to_cpu(vb
->vb_xv
->xr_clusters
);
731 status
= ocfs2_add_clusters_in_btree(handle
,
739 if ((status
< 0) && (status
!= -EAGAIN
)) {
740 if (status
!= -ENOSPC
)
745 ocfs2_journal_dirty(handle
, vb
->vb_bh
);
747 clusters_to_add
-= le32_to_cpu(vb
->vb_xv
->xr_clusters
) -
750 if (why
!= RESTART_NONE
&& clusters_to_add
) {
752 * We can only fail in case the alloc file doesn't give
753 * up enough clusters.
755 BUG_ON(why
== RESTART_META
);
757 credits
= ocfs2_calc_extend_credits(inode
->i_sb
,
758 &vb
->vb_xv
->xr_list
);
759 status
= ocfs2_extend_trans(handle
, credits
);
771 static int __ocfs2_remove_xattr_range(struct inode
*inode
,
772 struct ocfs2_xattr_value_buf
*vb
,
773 u32 cpos
, u32 phys_cpos
, u32 len
,
774 unsigned int ext_flags
,
775 struct ocfs2_xattr_set_ctxt
*ctxt
)
778 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
779 handle_t
*handle
= ctxt
->handle
;
780 struct ocfs2_extent_tree et
;
782 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
784 ret
= vb
->vb_access(handle
, INODE_CACHE(inode
), vb
->vb_bh
,
785 OCFS2_JOURNAL_ACCESS_WRITE
);
791 ret
= ocfs2_remove_extent(handle
, &et
, cpos
, len
, ctxt
->meta_ac
,
798 le32_add_cpu(&vb
->vb_xv
->xr_clusters
, -len
);
799 ocfs2_journal_dirty(handle
, vb
->vb_bh
);
801 if (ext_flags
& OCFS2_EXT_REFCOUNTED
)
802 ret
= ocfs2_decrease_refcount(inode
, handle
,
803 ocfs2_blocks_to_clusters(inode
->i_sb
,
805 len
, ctxt
->meta_ac
, &ctxt
->dealloc
, 1);
807 ret
= ocfs2_cache_cluster_dealloc(&ctxt
->dealloc
,
816 static int ocfs2_xattr_shrink_size(struct inode
*inode
,
819 struct ocfs2_xattr_value_buf
*vb
,
820 struct ocfs2_xattr_set_ctxt
*ctxt
)
823 unsigned int ext_flags
;
824 u32 trunc_len
, cpos
, phys_cpos
, alloc_size
;
827 if (old_clusters
<= new_clusters
)
831 trunc_len
= old_clusters
- new_clusters
;
833 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &phys_cpos
,
835 &vb
->vb_xv
->xr_list
, &ext_flags
);
841 if (alloc_size
> trunc_len
)
842 alloc_size
= trunc_len
;
844 ret
= __ocfs2_remove_xattr_range(inode
, vb
, cpos
,
845 phys_cpos
, alloc_size
,
852 block
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
853 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode
),
856 trunc_len
-= alloc_size
;
863 static int ocfs2_xattr_value_truncate(struct inode
*inode
,
864 struct ocfs2_xattr_value_buf
*vb
,
866 struct ocfs2_xattr_set_ctxt
*ctxt
)
869 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, len
);
870 u32 old_clusters
= le32_to_cpu(vb
->vb_xv
->xr_clusters
);
872 if (new_clusters
== old_clusters
)
875 if (new_clusters
> old_clusters
)
876 ret
= ocfs2_xattr_extend_allocation(inode
,
877 new_clusters
- old_clusters
,
880 ret
= ocfs2_xattr_shrink_size(inode
,
881 old_clusters
, new_clusters
,
887 static int ocfs2_xattr_list_entry(char *buffer
, size_t size
,
888 size_t *result
, const char *prefix
,
889 const char *name
, int name_len
)
891 char *p
= buffer
+ *result
;
892 int prefix_len
= strlen(prefix
);
893 int total_len
= prefix_len
+ name_len
+ 1;
895 *result
+= total_len
;
897 /* we are just looking for how big our buffer needs to be */
904 memcpy(p
, prefix
, prefix_len
);
905 memcpy(p
+ prefix_len
, name
, name_len
);
906 p
[prefix_len
+ name_len
] = '\0';
911 static int ocfs2_xattr_list_entries(struct inode
*inode
,
912 struct ocfs2_xattr_header
*header
,
913 char *buffer
, size_t buffer_size
)
917 const char *prefix
, *name
;
919 for (i
= 0 ; i
< le16_to_cpu(header
->xh_count
); i
++) {
920 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
921 type
= ocfs2_xattr_get_type(entry
);
922 prefix
= ocfs2_xattr_prefix(type
);
925 name
= (const char *)header
+
926 le16_to_cpu(entry
->xe_name_offset
);
928 ret
= ocfs2_xattr_list_entry(buffer
, buffer_size
,
929 &result
, prefix
, name
,
939 int ocfs2_has_inline_xattr_value_outside(struct inode
*inode
,
940 struct ocfs2_dinode
*di
)
942 struct ocfs2_xattr_header
*xh
;
945 xh
= (struct ocfs2_xattr_header
*)
946 ((void *)di
+ inode
->i_sb
->s_blocksize
-
947 le16_to_cpu(di
->i_xattr_inline_size
));
949 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++)
950 if (!ocfs2_xattr_is_local(&xh
->xh_entries
[i
]))
956 static int ocfs2_xattr_ibody_list(struct inode
*inode
,
957 struct ocfs2_dinode
*di
,
961 struct ocfs2_xattr_header
*header
= NULL
;
962 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
965 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
968 header
= (struct ocfs2_xattr_header
*)
969 ((void *)di
+ inode
->i_sb
->s_blocksize
-
970 le16_to_cpu(di
->i_xattr_inline_size
));
972 ret
= ocfs2_xattr_list_entries(inode
, header
, buffer
, buffer_size
);
977 static int ocfs2_xattr_block_list(struct inode
*inode
,
978 struct ocfs2_dinode
*di
,
982 struct buffer_head
*blk_bh
= NULL
;
983 struct ocfs2_xattr_block
*xb
;
986 if (!di
->i_xattr_loc
)
989 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
996 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
997 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
998 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
999 ret
= ocfs2_xattr_list_entries(inode
, header
,
1000 buffer
, buffer_size
);
1002 ret
= ocfs2_xattr_tree_list_index_block(inode
, blk_bh
,
1003 buffer
, buffer_size
);
1010 ssize_t
ocfs2_listxattr(struct dentry
*dentry
,
1014 int ret
= 0, i_ret
= 0, b_ret
= 0;
1015 struct buffer_head
*di_bh
= NULL
;
1016 struct ocfs2_dinode
*di
= NULL
;
1017 struct ocfs2_inode_info
*oi
= OCFS2_I(d_inode(dentry
));
1019 if (!ocfs2_supports_xattr(OCFS2_SB(dentry
->d_sb
)))
1022 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1025 ret
= ocfs2_inode_lock(d_inode(dentry
), &di_bh
, 0);
1031 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1033 down_read(&oi
->ip_xattr_sem
);
1034 i_ret
= ocfs2_xattr_ibody_list(d_inode(dentry
), di
, buffer
, size
);
1042 b_ret
= ocfs2_xattr_block_list(d_inode(dentry
), di
,
1047 up_read(&oi
->ip_xattr_sem
);
1048 ocfs2_inode_unlock(d_inode(dentry
), 0);
1052 return i_ret
+ b_ret
;
1055 static int ocfs2_xattr_find_entry(int name_index
,
1057 struct ocfs2_xattr_search
*xs
)
1059 struct ocfs2_xattr_entry
*entry
;
1066 name_len
= strlen(name
);
1068 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1069 cmp
= name_index
- ocfs2_xattr_get_type(entry
);
1071 cmp
= name_len
- entry
->xe_name_len
;
1073 cmp
= memcmp(name
, (xs
->base
+
1074 le16_to_cpu(entry
->xe_name_offset
)),
1082 return cmp
? -ENODATA
: 0;
1085 static int ocfs2_xattr_get_value_outside(struct inode
*inode
,
1086 struct ocfs2_xattr_value_root
*xv
,
1090 u32 cpos
, p_cluster
, num_clusters
, bpc
, clusters
;
1093 size_t cplen
, blocksize
;
1094 struct buffer_head
*bh
= NULL
;
1095 struct ocfs2_extent_list
*el
;
1098 clusters
= le32_to_cpu(xv
->xr_clusters
);
1099 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
1100 blocksize
= inode
->i_sb
->s_blocksize
;
1103 while (cpos
< clusters
) {
1104 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
1105 &num_clusters
, el
, NULL
);
1111 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
1112 /* Copy ocfs2_xattr_value */
1113 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
1114 ret
= ocfs2_read_block(INODE_CACHE(inode
), blkno
,
1121 cplen
= len
>= blocksize
? blocksize
: len
;
1122 memcpy(buffer
, bh
->b_data
, cplen
);
1131 cpos
+= num_clusters
;
1137 static int ocfs2_xattr_ibody_get(struct inode
*inode
,
1142 struct ocfs2_xattr_search
*xs
)
1144 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1145 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1146 struct ocfs2_xattr_value_root
*xv
;
1150 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
1153 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
1154 xs
->header
= (struct ocfs2_xattr_header
*)
1155 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
1156 xs
->base
= (void *)xs
->header
;
1157 xs
->here
= xs
->header
->xh_entries
;
1159 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1162 size
= le64_to_cpu(xs
->here
->xe_value_size
);
1164 if (size
> buffer_size
)
1166 if (ocfs2_xattr_is_local(xs
->here
)) {
1167 memcpy(buffer
, (void *)xs
->base
+
1168 le16_to_cpu(xs
->here
->xe_name_offset
) +
1169 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
), size
);
1171 xv
= (struct ocfs2_xattr_value_root
*)
1172 (xs
->base
+ le16_to_cpu(
1173 xs
->here
->xe_name_offset
) +
1174 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
));
1175 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
1187 static int ocfs2_xattr_block_get(struct inode
*inode
,
1192 struct ocfs2_xattr_search
*xs
)
1194 struct ocfs2_xattr_block
*xb
;
1195 struct ocfs2_xattr_value_root
*xv
;
1197 int ret
= -ENODATA
, name_offset
, name_len
, i
;
1198 int uninitialized_var(block_off
);
1200 xs
->bucket
= ocfs2_xattr_bucket_new(inode
);
1207 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, xs
);
1213 if (xs
->not_found
) {
1218 xb
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
1219 size
= le64_to_cpu(xs
->here
->xe_value_size
);
1222 if (size
> buffer_size
)
1225 name_offset
= le16_to_cpu(xs
->here
->xe_name_offset
);
1226 name_len
= OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
);
1227 i
= xs
->here
- xs
->header
->xh_entries
;
1229 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
1230 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
1231 bucket_xh(xs
->bucket
),
1239 xs
->base
= bucket_block(xs
->bucket
, block_off
);
1241 if (ocfs2_xattr_is_local(xs
->here
)) {
1242 memcpy(buffer
, (void *)xs
->base
+
1243 name_offset
+ name_len
, size
);
1245 xv
= (struct ocfs2_xattr_value_root
*)
1246 (xs
->base
+ name_offset
+ name_len
);
1247 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
1257 ocfs2_xattr_bucket_free(xs
->bucket
);
1259 brelse(xs
->xattr_bh
);
1260 xs
->xattr_bh
= NULL
;
1264 int ocfs2_xattr_get_nolock(struct inode
*inode
,
1265 struct buffer_head
*di_bh
,
1272 struct ocfs2_dinode
*di
= NULL
;
1273 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1274 struct ocfs2_xattr_search xis
= {
1275 .not_found
= -ENODATA
,
1277 struct ocfs2_xattr_search xbs
= {
1278 .not_found
= -ENODATA
,
1281 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
1284 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1287 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
1288 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1290 ret
= ocfs2_xattr_ibody_get(inode
, name_index
, name
, buffer
,
1292 if (ret
== -ENODATA
&& di
->i_xattr_loc
)
1293 ret
= ocfs2_xattr_block_get(inode
, name_index
, name
, buffer
,
1299 /* ocfs2_xattr_get()
1301 * Copy an extended attribute into the buffer provided.
1302 * Buffer is NULL to compute the size of buffer required.
1304 static int ocfs2_xattr_get(struct inode
*inode
,
1311 struct buffer_head
*di_bh
= NULL
;
1313 ret
= ocfs2_inode_lock(inode
, &di_bh
, 0);
1318 down_read(&OCFS2_I(inode
)->ip_xattr_sem
);
1319 ret
= ocfs2_xattr_get_nolock(inode
, di_bh
, name_index
,
1320 name
, buffer
, buffer_size
);
1321 up_read(&OCFS2_I(inode
)->ip_xattr_sem
);
1323 ocfs2_inode_unlock(inode
, 0);
1330 static int __ocfs2_xattr_set_value_outside(struct inode
*inode
,
1332 struct ocfs2_xattr_value_buf
*vb
,
1336 int ret
= 0, i
, cp_len
;
1337 u16 blocksize
= inode
->i_sb
->s_blocksize
;
1338 u32 p_cluster
, num_clusters
;
1339 u32 cpos
= 0, bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
1340 u32 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, value_len
);
1342 struct buffer_head
*bh
= NULL
;
1343 unsigned int ext_flags
;
1344 struct ocfs2_xattr_value_root
*xv
= vb
->vb_xv
;
1346 BUG_ON(clusters
> le32_to_cpu(xv
->xr_clusters
));
1348 while (cpos
< clusters
) {
1349 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
1350 &num_clusters
, &xv
->xr_list
,
1357 BUG_ON(ext_flags
& OCFS2_EXT_REFCOUNTED
);
1359 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
1361 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
1362 ret
= ocfs2_read_block(INODE_CACHE(inode
), blkno
,
1369 ret
= ocfs2_journal_access(handle
,
1372 OCFS2_JOURNAL_ACCESS_WRITE
);
1378 cp_len
= value_len
> blocksize
? blocksize
: value_len
;
1379 memcpy(bh
->b_data
, value
, cp_len
);
1380 value_len
-= cp_len
;
1382 if (cp_len
< blocksize
)
1383 memset(bh
->b_data
+ cp_len
, 0,
1384 blocksize
- cp_len
);
1386 ocfs2_journal_dirty(handle
, bh
);
1391 * XXX: do we need to empty all the following
1392 * blocks in this cluster?
1397 cpos
+= num_clusters
;
1405 static int ocfs2_xa_check_space_helper(int needed_space
, int free_start
,
1413 free_space
= free_start
-
1414 sizeof(struct ocfs2_xattr_header
) -
1415 (num_entries
* sizeof(struct ocfs2_xattr_entry
)) -
1416 OCFS2_XATTR_HEADER_GAP
;
1419 if (free_space
< needed_space
)
1425 static int ocfs2_xa_journal_access(handle_t
*handle
, struct ocfs2_xa_loc
*loc
,
1428 return loc
->xl_ops
->xlo_journal_access(handle
, loc
, type
);
1431 static void ocfs2_xa_journal_dirty(handle_t
*handle
, struct ocfs2_xa_loc
*loc
)
1433 loc
->xl_ops
->xlo_journal_dirty(handle
, loc
);
1436 /* Give a pointer into the storage for the given offset */
1437 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc
*loc
, int offset
)
1439 BUG_ON(offset
>= loc
->xl_size
);
1440 return loc
->xl_ops
->xlo_offset_pointer(loc
, offset
);
1444 * Wipe the name+value pair and allow the storage to reclaim it. This
1445 * must be followed by either removal of the entry or a call to
1446 * ocfs2_xa_add_namevalue().
1448 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1450 loc
->xl_ops
->xlo_wipe_namevalue(loc
);
1454 * Find lowest offset to a name+value pair. This is the start of our
1455 * downward-growing free space.
1457 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc
*loc
)
1459 return loc
->xl_ops
->xlo_get_free_start(loc
);
1462 /* Can we reuse loc->xl_entry for xi? */
1463 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc
*loc
,
1464 struct ocfs2_xattr_info
*xi
)
1466 return loc
->xl_ops
->xlo_can_reuse(loc
, xi
);
1469 /* How much free space is needed to set the new value */
1470 static int ocfs2_xa_check_space(struct ocfs2_xa_loc
*loc
,
1471 struct ocfs2_xattr_info
*xi
)
1473 return loc
->xl_ops
->xlo_check_space(loc
, xi
);
1476 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1478 loc
->xl_ops
->xlo_add_entry(loc
, name_hash
);
1479 loc
->xl_entry
->xe_name_hash
= cpu_to_le32(name_hash
);
1481 * We can't leave the new entry's xe_name_offset at zero or
1482 * add_namevalue() will go nuts. We set it to the size of our
1483 * storage so that it can never be less than any other entry.
1485 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(loc
->xl_size
);
1488 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc
*loc
,
1489 struct ocfs2_xattr_info
*xi
)
1491 int size
= namevalue_size_xi(xi
);
1495 loc
->xl_ops
->xlo_add_namevalue(loc
, size
);
1496 loc
->xl_entry
->xe_value_size
= cpu_to_le64(xi
->xi_value_len
);
1497 loc
->xl_entry
->xe_name_len
= xi
->xi_name_len
;
1498 ocfs2_xattr_set_type(loc
->xl_entry
, xi
->xi_name_index
);
1499 ocfs2_xattr_set_local(loc
->xl_entry
,
1500 xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
);
1502 nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1503 nameval_buf
= ocfs2_xa_offset_pointer(loc
, nameval_offset
);
1504 memset(nameval_buf
, 0, size
);
1505 memcpy(nameval_buf
, xi
->xi_name
, xi
->xi_name_len
);
1508 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1509 struct ocfs2_xattr_value_buf
*vb
)
1511 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1512 int name_size
= OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
);
1514 /* Value bufs are for value trees */
1515 BUG_ON(ocfs2_xattr_is_local(loc
->xl_entry
));
1516 BUG_ON(namevalue_size_xe(loc
->xl_entry
) !=
1517 (name_size
+ OCFS2_XATTR_ROOT_SIZE
));
1519 loc
->xl_ops
->xlo_fill_value_buf(loc
, vb
);
1521 (struct ocfs2_xattr_value_root
*)ocfs2_xa_offset_pointer(loc
,
1526 static int ocfs2_xa_block_journal_access(handle_t
*handle
,
1527 struct ocfs2_xa_loc
*loc
, int type
)
1529 struct buffer_head
*bh
= loc
->xl_storage
;
1530 ocfs2_journal_access_func access
;
1532 if (loc
->xl_size
== (bh
->b_size
-
1533 offsetof(struct ocfs2_xattr_block
,
1534 xb_attrs
.xb_header
)))
1535 access
= ocfs2_journal_access_xb
;
1537 access
= ocfs2_journal_access_di
;
1538 return access(handle
, INODE_CACHE(loc
->xl_inode
), bh
, type
);
1541 static void ocfs2_xa_block_journal_dirty(handle_t
*handle
,
1542 struct ocfs2_xa_loc
*loc
)
1544 struct buffer_head
*bh
= loc
->xl_storage
;
1546 ocfs2_journal_dirty(handle
, bh
);
1549 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc
*loc
,
1552 return (char *)loc
->xl_header
+ offset
;
1555 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc
*loc
,
1556 struct ocfs2_xattr_info
*xi
)
1559 * Block storage is strict. If the sizes aren't exact, we will
1560 * remove the old one and reinsert the new.
1562 return namevalue_size_xe(loc
->xl_entry
) ==
1563 namevalue_size_xi(xi
);
1566 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc
*loc
)
1568 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1569 int i
, count
= le16_to_cpu(xh
->xh_count
);
1570 int offset
, free_start
= loc
->xl_size
;
1572 for (i
= 0; i
< count
; i
++) {
1573 offset
= le16_to_cpu(xh
->xh_entries
[i
].xe_name_offset
);
1574 if (offset
< free_start
)
1575 free_start
= offset
;
1581 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc
*loc
,
1582 struct ocfs2_xattr_info
*xi
)
1584 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1585 int free_start
= ocfs2_xa_get_free_start(loc
);
1586 int needed_space
= ocfs2_xi_entry_usage(xi
);
1589 * Block storage will reclaim the original entry before inserting
1590 * the new value, so we only need the difference. If the new
1591 * entry is smaller than the old one, we don't need anything.
1593 if (loc
->xl_entry
) {
1594 /* Don't need space if we're reusing! */
1595 if (ocfs2_xa_can_reuse_entry(loc
, xi
))
1598 needed_space
-= ocfs2_xe_entry_usage(loc
->xl_entry
);
1600 if (needed_space
< 0)
1602 return ocfs2_xa_check_space_helper(needed_space
, free_start
, count
);
1606 * Block storage for xattrs keeps the name+value pairs compacted. When
1607 * we remove one, we have to shift any that preceded it towards the end.
1609 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1612 int namevalue_offset
, first_namevalue_offset
, namevalue_size
;
1613 struct ocfs2_xattr_entry
*entry
= loc
->xl_entry
;
1614 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1615 int count
= le16_to_cpu(xh
->xh_count
);
1617 namevalue_offset
= le16_to_cpu(entry
->xe_name_offset
);
1618 namevalue_size
= namevalue_size_xe(entry
);
1619 first_namevalue_offset
= ocfs2_xa_get_free_start(loc
);
1621 /* Shift the name+value pairs */
1622 memmove((char *)xh
+ first_namevalue_offset
+ namevalue_size
,
1623 (char *)xh
+ first_namevalue_offset
,
1624 namevalue_offset
- first_namevalue_offset
);
1625 memset((char *)xh
+ first_namevalue_offset
, 0, namevalue_size
);
1627 /* Now tell xh->xh_entries about it */
1628 for (i
= 0; i
< count
; i
++) {
1629 offset
= le16_to_cpu(xh
->xh_entries
[i
].xe_name_offset
);
1630 if (offset
<= namevalue_offset
)
1631 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
,
1636 * Note that we don't update xh_free_start or xh_name_value_len
1637 * because they're not used in block-stored xattrs.
1641 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1643 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1644 loc
->xl_entry
= &(loc
->xl_header
->xh_entries
[count
]);
1645 le16_add_cpu(&loc
->xl_header
->xh_count
, 1);
1646 memset(loc
->xl_entry
, 0, sizeof(struct ocfs2_xattr_entry
));
1649 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc
*loc
, int size
)
1651 int free_start
= ocfs2_xa_get_free_start(loc
);
1653 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(free_start
- size
);
1656 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1657 struct ocfs2_xattr_value_buf
*vb
)
1659 struct buffer_head
*bh
= loc
->xl_storage
;
1661 if (loc
->xl_size
== (bh
->b_size
-
1662 offsetof(struct ocfs2_xattr_block
,
1663 xb_attrs
.xb_header
)))
1664 vb
->vb_access
= ocfs2_journal_access_xb
;
1666 vb
->vb_access
= ocfs2_journal_access_di
;
1671 * Operations for xattrs stored in blocks. This includes inline inode
1672 * storage and unindexed ocfs2_xattr_blocks.
1674 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops
= {
1675 .xlo_journal_access
= ocfs2_xa_block_journal_access
,
1676 .xlo_journal_dirty
= ocfs2_xa_block_journal_dirty
,
1677 .xlo_offset_pointer
= ocfs2_xa_block_offset_pointer
,
1678 .xlo_check_space
= ocfs2_xa_block_check_space
,
1679 .xlo_can_reuse
= ocfs2_xa_block_can_reuse
,
1680 .xlo_get_free_start
= ocfs2_xa_block_get_free_start
,
1681 .xlo_wipe_namevalue
= ocfs2_xa_block_wipe_namevalue
,
1682 .xlo_add_entry
= ocfs2_xa_block_add_entry
,
1683 .xlo_add_namevalue
= ocfs2_xa_block_add_namevalue
,
1684 .xlo_fill_value_buf
= ocfs2_xa_block_fill_value_buf
,
1687 static int ocfs2_xa_bucket_journal_access(handle_t
*handle
,
1688 struct ocfs2_xa_loc
*loc
, int type
)
1690 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1692 return ocfs2_xattr_bucket_journal_access(handle
, bucket
, type
);
1695 static void ocfs2_xa_bucket_journal_dirty(handle_t
*handle
,
1696 struct ocfs2_xa_loc
*loc
)
1698 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1700 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
1703 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc
*loc
,
1706 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1707 int block
, block_offset
;
1709 /* The header is at the front of the bucket */
1710 block
= offset
>> loc
->xl_inode
->i_sb
->s_blocksize_bits
;
1711 block_offset
= offset
% loc
->xl_inode
->i_sb
->s_blocksize
;
1713 return bucket_block(bucket
, block
) + block_offset
;
1716 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc
*loc
,
1717 struct ocfs2_xattr_info
*xi
)
1719 return namevalue_size_xe(loc
->xl_entry
) >=
1720 namevalue_size_xi(xi
);
1723 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc
*loc
)
1725 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1726 return le16_to_cpu(bucket_xh(bucket
)->xh_free_start
);
1729 static int ocfs2_bucket_align_free_start(struct super_block
*sb
,
1730 int free_start
, int size
)
1733 * We need to make sure that the name+value pair fits within
1736 if (((free_start
- size
) >> sb
->s_blocksize_bits
) !=
1737 ((free_start
- 1) >> sb
->s_blocksize_bits
))
1738 free_start
-= free_start
% sb
->s_blocksize
;
1743 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc
*loc
,
1744 struct ocfs2_xattr_info
*xi
)
1747 int count
= le16_to_cpu(loc
->xl_header
->xh_count
);
1748 int free_start
= ocfs2_xa_get_free_start(loc
);
1749 int needed_space
= ocfs2_xi_entry_usage(xi
);
1750 int size
= namevalue_size_xi(xi
);
1751 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1754 * Bucket storage does not reclaim name+value pairs it cannot
1755 * reuse. They live as holes until the bucket fills, and then
1756 * the bucket is defragmented. However, the bucket can reclaim
1757 * the ocfs2_xattr_entry.
1759 if (loc
->xl_entry
) {
1760 /* Don't need space if we're reusing! */
1761 if (ocfs2_xa_can_reuse_entry(loc
, xi
))
1764 needed_space
-= sizeof(struct ocfs2_xattr_entry
);
1766 BUG_ON(needed_space
< 0);
1768 if (free_start
< size
) {
1773 * First we check if it would fit in the first place.
1774 * Below, we align the free start to a block. This may
1775 * slide us below the minimum gap. By checking unaligned
1776 * first, we avoid that error.
1778 rc
= ocfs2_xa_check_space_helper(needed_space
, free_start
,
1782 free_start
= ocfs2_bucket_align_free_start(sb
, free_start
,
1785 return ocfs2_xa_check_space_helper(needed_space
, free_start
, count
);
1788 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc
*loc
)
1790 le16_add_cpu(&loc
->xl_header
->xh_name_value_len
,
1791 -namevalue_size_xe(loc
->xl_entry
));
1794 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc
*loc
, u32 name_hash
)
1796 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1797 int count
= le16_to_cpu(xh
->xh_count
);
1798 int low
= 0, high
= count
- 1, tmp
;
1799 struct ocfs2_xattr_entry
*tmp_xe
;
1802 * We keep buckets sorted by name_hash, so we need to find
1805 while (low
<= high
&& count
) {
1806 tmp
= (low
+ high
) / 2;
1807 tmp_xe
= &xh
->xh_entries
[tmp
];
1809 if (name_hash
> le32_to_cpu(tmp_xe
->xe_name_hash
))
1811 else if (name_hash
< le32_to_cpu(tmp_xe
->xe_name_hash
))
1820 memmove(&xh
->xh_entries
[low
+ 1],
1821 &xh
->xh_entries
[low
],
1822 ((count
- low
) * sizeof(struct ocfs2_xattr_entry
)));
1824 le16_add_cpu(&xh
->xh_count
, 1);
1825 loc
->xl_entry
= &xh
->xh_entries
[low
];
1826 memset(loc
->xl_entry
, 0, sizeof(struct ocfs2_xattr_entry
));
1829 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc
*loc
, int size
)
1831 int free_start
= ocfs2_xa_get_free_start(loc
);
1832 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1833 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1836 free_start
= ocfs2_bucket_align_free_start(sb
, free_start
, size
);
1837 nameval_offset
= free_start
- size
;
1838 loc
->xl_entry
->xe_name_offset
= cpu_to_le16(nameval_offset
);
1839 xh
->xh_free_start
= cpu_to_le16(nameval_offset
);
1840 le16_add_cpu(&xh
->xh_name_value_len
, size
);
1844 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc
*loc
,
1845 struct ocfs2_xattr_value_buf
*vb
)
1847 struct ocfs2_xattr_bucket
*bucket
= loc
->xl_storage
;
1848 struct super_block
*sb
= loc
->xl_inode
->i_sb
;
1849 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
1850 int size
= namevalue_size_xe(loc
->xl_entry
);
1851 int block_offset
= nameval_offset
>> sb
->s_blocksize_bits
;
1853 /* Values are not allowed to straddle block boundaries */
1854 BUG_ON(block_offset
!=
1855 ((nameval_offset
+ size
- 1) >> sb
->s_blocksize_bits
));
1856 /* We expect the bucket to be filled in */
1857 BUG_ON(!bucket
->bu_bhs
[block_offset
]);
1859 vb
->vb_access
= ocfs2_journal_access
;
1860 vb
->vb_bh
= bucket
->bu_bhs
[block_offset
];
1863 /* Operations for xattrs stored in buckets. */
1864 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops
= {
1865 .xlo_journal_access
= ocfs2_xa_bucket_journal_access
,
1866 .xlo_journal_dirty
= ocfs2_xa_bucket_journal_dirty
,
1867 .xlo_offset_pointer
= ocfs2_xa_bucket_offset_pointer
,
1868 .xlo_check_space
= ocfs2_xa_bucket_check_space
,
1869 .xlo_can_reuse
= ocfs2_xa_bucket_can_reuse
,
1870 .xlo_get_free_start
= ocfs2_xa_bucket_get_free_start
,
1871 .xlo_wipe_namevalue
= ocfs2_xa_bucket_wipe_namevalue
,
1872 .xlo_add_entry
= ocfs2_xa_bucket_add_entry
,
1873 .xlo_add_namevalue
= ocfs2_xa_bucket_add_namevalue
,
1874 .xlo_fill_value_buf
= ocfs2_xa_bucket_fill_value_buf
,
1877 static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc
*loc
)
1879 struct ocfs2_xattr_value_buf vb
;
1881 if (ocfs2_xattr_is_local(loc
->xl_entry
))
1884 ocfs2_xa_fill_value_buf(loc
, &vb
);
1885 return le32_to_cpu(vb
.vb_xv
->xr_clusters
);
1888 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc
*loc
, u64 bytes
,
1889 struct ocfs2_xattr_set_ctxt
*ctxt
)
1891 int trunc_rc
, access_rc
;
1892 struct ocfs2_xattr_value_buf vb
;
1894 ocfs2_xa_fill_value_buf(loc
, &vb
);
1895 trunc_rc
= ocfs2_xattr_value_truncate(loc
->xl_inode
, &vb
, bytes
,
1899 * The caller of ocfs2_xa_value_truncate() has already called
1900 * ocfs2_xa_journal_access on the loc. However, The truncate code
1901 * calls ocfs2_extend_trans(). This may commit the previous
1902 * transaction and open a new one. If this is a bucket, truncate
1903 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1904 * the caller is expecting to dirty the entire bucket. So we must
1905 * reset the journal work. We do this even if truncate has failed,
1906 * as it could have failed after committing the extend.
1908 access_rc
= ocfs2_xa_journal_access(ctxt
->handle
, loc
,
1909 OCFS2_JOURNAL_ACCESS_WRITE
);
1911 /* Errors in truncate take precedence */
1912 return trunc_rc
? trunc_rc
: access_rc
;
1915 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc
*loc
)
1918 struct ocfs2_xattr_header
*xh
= loc
->xl_header
;
1919 struct ocfs2_xattr_entry
*entry
= loc
->xl_entry
;
1921 ocfs2_xa_wipe_namevalue(loc
);
1922 loc
->xl_entry
= NULL
;
1924 le16_add_cpu(&xh
->xh_count
, -1);
1925 count
= le16_to_cpu(xh
->xh_count
);
1928 * Only zero out the entry if there are more remaining. This is
1929 * important for an empty bucket, as it keeps track of the
1930 * bucket's hash value. It doesn't hurt empty block storage.
1933 index
= ((char *)entry
- (char *)&xh
->xh_entries
) /
1934 sizeof(struct ocfs2_xattr_entry
);
1935 memmove(&xh
->xh_entries
[index
], &xh
->xh_entries
[index
+ 1],
1936 (count
- index
) * sizeof(struct ocfs2_xattr_entry
));
1937 memset(&xh
->xh_entries
[count
], 0,
1938 sizeof(struct ocfs2_xattr_entry
));
1943 * If we have a problem adjusting the size of an external value during
1944 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1945 * in an intermediate state. For example, the value may be partially
1948 * If the value tree hasn't changed, the extend/truncate went nowhere.
1949 * We have nothing to do. The caller can treat it as a straight error.
1951 * If the value tree got partially truncated, we now have a corrupted
1952 * extended attribute. We're going to wipe its entry and leak the
1953 * clusters. Better to leak some storage than leave a corrupt entry.
1955 * If the value tree grew, it obviously didn't grow enough for the
1956 * new entry. We're not going to try and reclaim those clusters either.
1957 * If there was already an external value there (orig_clusters != 0),
1958 * the new clusters are attached safely and we can just leave the old
1959 * value in place. If there was no external value there, we remove
1962 * This way, the xattr block we store in the journal will be consistent.
1963 * If the size change broke because of the journal, no changes will hit
1966 static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc
*loc
,
1968 unsigned int orig_clusters
)
1970 unsigned int new_clusters
= ocfs2_xa_value_clusters(loc
);
1971 char *nameval_buf
= ocfs2_xa_offset_pointer(loc
,
1972 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
1974 if (new_clusters
< orig_clusters
) {
1976 "Partial truncate while %s xattr %.*s. Leaking "
1977 "%u clusters and removing the entry\n",
1978 what
, loc
->xl_entry
->xe_name_len
, nameval_buf
,
1979 orig_clusters
- new_clusters
);
1980 ocfs2_xa_remove_entry(loc
);
1981 } else if (!orig_clusters
) {
1983 "Unable to allocate an external value for xattr "
1984 "%.*s safely. Leaking %u clusters and removing the "
1986 loc
->xl_entry
->xe_name_len
, nameval_buf
,
1987 new_clusters
- orig_clusters
);
1988 ocfs2_xa_remove_entry(loc
);
1989 } else if (new_clusters
> orig_clusters
)
1991 "Unable to grow xattr %.*s safely. %u new clusters "
1992 "have been added, but the value will not be "
1994 loc
->xl_entry
->xe_name_len
, nameval_buf
,
1995 new_clusters
- orig_clusters
);
1998 static int ocfs2_xa_remove(struct ocfs2_xa_loc
*loc
,
1999 struct ocfs2_xattr_set_ctxt
*ctxt
)
2002 unsigned int orig_clusters
;
2004 if (!ocfs2_xattr_is_local(loc
->xl_entry
)) {
2005 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2006 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2010 * Since this is remove, we can return 0 if
2011 * ocfs2_xa_cleanup_value_truncate() is going to
2012 * wipe the entry anyway. So we check the
2013 * cluster count as well.
2015 if (orig_clusters
!= ocfs2_xa_value_clusters(loc
))
2017 ocfs2_xa_cleanup_value_truncate(loc
, "removing",
2024 ocfs2_xa_remove_entry(loc
);
2030 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc
*loc
)
2032 int name_size
= OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
);
2035 nameval_buf
= ocfs2_xa_offset_pointer(loc
,
2036 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
2037 memcpy(nameval_buf
+ name_size
, &def_xv
, OCFS2_XATTR_ROOT_SIZE
);
2041 * Take an existing entry and make it ready for the new value. This
2042 * won't allocate space, but it may free space. It should be ready for
2043 * ocfs2_xa_prepare_entry() to finish the work.
2045 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc
*loc
,
2046 struct ocfs2_xattr_info
*xi
,
2047 struct ocfs2_xattr_set_ctxt
*ctxt
)
2050 int name_size
= OCFS2_XATTR_SIZE(xi
->xi_name_len
);
2051 unsigned int orig_clusters
;
2053 int xe_local
= ocfs2_xattr_is_local(loc
->xl_entry
);
2054 int xi_local
= xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
;
2056 BUG_ON(OCFS2_XATTR_SIZE(loc
->xl_entry
->xe_name_len
) !=
2059 nameval_buf
= ocfs2_xa_offset_pointer(loc
,
2060 le16_to_cpu(loc
->xl_entry
->xe_name_offset
));
2062 memset(nameval_buf
+ name_size
, 0,
2063 namevalue_size_xe(loc
->xl_entry
) - name_size
);
2065 ocfs2_xa_install_value_root(loc
);
2067 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2069 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2073 memset(nameval_buf
+ name_size
, 0,
2074 namevalue_size_xe(loc
->xl_entry
) -
2076 } else if (le64_to_cpu(loc
->xl_entry
->xe_value_size
) >
2078 rc
= ocfs2_xa_value_truncate(loc
, xi
->xi_value_len
,
2085 ocfs2_xa_cleanup_value_truncate(loc
, "reusing",
2091 loc
->xl_entry
->xe_value_size
= cpu_to_le64(xi
->xi_value_len
);
2092 ocfs2_xattr_set_local(loc
->xl_entry
, xi_local
);
2099 * Prepares loc->xl_entry to receive the new xattr. This includes
2100 * properly setting up the name+value pair region. If loc->xl_entry
2101 * already exists, it will take care of modifying it appropriately.
2103 * Note that this modifies the data. You did journal_access already,
2106 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc
*loc
,
2107 struct ocfs2_xattr_info
*xi
,
2109 struct ocfs2_xattr_set_ctxt
*ctxt
)
2112 unsigned int orig_clusters
;
2113 __le64 orig_value_size
= 0;
2115 rc
= ocfs2_xa_check_space(loc
, xi
);
2119 if (loc
->xl_entry
) {
2120 if (ocfs2_xa_can_reuse_entry(loc
, xi
)) {
2121 orig_value_size
= loc
->xl_entry
->xe_value_size
;
2122 rc
= ocfs2_xa_reuse_entry(loc
, xi
, ctxt
);
2128 if (!ocfs2_xattr_is_local(loc
->xl_entry
)) {
2129 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2130 rc
= ocfs2_xa_value_truncate(loc
, 0, ctxt
);
2133 ocfs2_xa_cleanup_value_truncate(loc
,
2139 ocfs2_xa_wipe_namevalue(loc
);
2141 ocfs2_xa_add_entry(loc
, name_hash
);
2144 * If we get here, we have a blank entry. Fill it. We grow our
2145 * name+value pair back from the end.
2147 ocfs2_xa_add_namevalue(loc
, xi
);
2148 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
)
2149 ocfs2_xa_install_value_root(loc
);
2152 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2153 orig_clusters
= ocfs2_xa_value_clusters(loc
);
2154 rc
= ocfs2_xa_value_truncate(loc
, xi
->xi_value_len
, ctxt
);
2156 ctxt
->set_abort
= 1;
2157 ocfs2_xa_cleanup_value_truncate(loc
, "growing",
2160 * If we were growing an existing value,
2161 * ocfs2_xa_cleanup_value_truncate() won't remove
2162 * the entry. We need to restore the original value
2165 if (loc
->xl_entry
) {
2166 BUG_ON(!orig_value_size
);
2167 loc
->xl_entry
->xe_value_size
= orig_value_size
;
2178 * Store the value portion of the name+value pair. This will skip
2179 * values that are stored externally. Their tree roots were set up
2180 * by ocfs2_xa_prepare_entry().
2182 static int ocfs2_xa_store_value(struct ocfs2_xa_loc
*loc
,
2183 struct ocfs2_xattr_info
*xi
,
2184 struct ocfs2_xattr_set_ctxt
*ctxt
)
2187 int nameval_offset
= le16_to_cpu(loc
->xl_entry
->xe_name_offset
);
2188 int name_size
= OCFS2_XATTR_SIZE(xi
->xi_name_len
);
2190 struct ocfs2_xattr_value_buf vb
;
2192 nameval_buf
= ocfs2_xa_offset_pointer(loc
, nameval_offset
);
2193 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2194 ocfs2_xa_fill_value_buf(loc
, &vb
);
2195 rc
= __ocfs2_xattr_set_value_outside(loc
->xl_inode
,
2200 memcpy(nameval_buf
+ name_size
, xi
->xi_value
, xi
->xi_value_len
);
2205 static int ocfs2_xa_set(struct ocfs2_xa_loc
*loc
,
2206 struct ocfs2_xattr_info
*xi
,
2207 struct ocfs2_xattr_set_ctxt
*ctxt
)
2210 u32 name_hash
= ocfs2_xattr_name_hash(loc
->xl_inode
, xi
->xi_name
,
2213 ret
= ocfs2_xa_journal_access(ctxt
->handle
, loc
,
2214 OCFS2_JOURNAL_ACCESS_WRITE
);
2221 * From here on out, everything is going to modify the buffer a
2222 * little. Errors are going to leave the xattr header in a
2223 * sane state. Thus, even with errors we dirty the sucker.
2226 /* Don't worry, we are never called with !xi_value and !xl_entry */
2227 if (!xi
->xi_value
) {
2228 ret
= ocfs2_xa_remove(loc
, ctxt
);
2232 ret
= ocfs2_xa_prepare_entry(loc
, xi
, name_hash
, ctxt
);
2239 ret
= ocfs2_xa_store_value(loc
, xi
, ctxt
);
2244 ocfs2_xa_journal_dirty(ctxt
->handle
, loc
);
2250 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc
*loc
,
2251 struct inode
*inode
,
2252 struct buffer_head
*bh
,
2253 struct ocfs2_xattr_entry
*entry
)
2255 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)bh
->b_data
;
2257 BUG_ON(!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
));
2259 loc
->xl_inode
= inode
;
2260 loc
->xl_ops
= &ocfs2_xa_block_loc_ops
;
2261 loc
->xl_storage
= bh
;
2262 loc
->xl_entry
= entry
;
2263 loc
->xl_size
= le16_to_cpu(di
->i_xattr_inline_size
);
2265 (struct ocfs2_xattr_header
*)(bh
->b_data
+ bh
->b_size
-
2269 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc
*loc
,
2270 struct inode
*inode
,
2271 struct buffer_head
*bh
,
2272 struct ocfs2_xattr_entry
*entry
)
2274 struct ocfs2_xattr_block
*xb
=
2275 (struct ocfs2_xattr_block
*)bh
->b_data
;
2277 BUG_ON(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
);
2279 loc
->xl_inode
= inode
;
2280 loc
->xl_ops
= &ocfs2_xa_block_loc_ops
;
2281 loc
->xl_storage
= bh
;
2282 loc
->xl_header
= &(xb
->xb_attrs
.xb_header
);
2283 loc
->xl_entry
= entry
;
2284 loc
->xl_size
= bh
->b_size
- offsetof(struct ocfs2_xattr_block
,
2285 xb_attrs
.xb_header
);
2288 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc
*loc
,
2289 struct ocfs2_xattr_bucket
*bucket
,
2290 struct ocfs2_xattr_entry
*entry
)
2292 loc
->xl_inode
= bucket
->bu_inode
;
2293 loc
->xl_ops
= &ocfs2_xa_bucket_loc_ops
;
2294 loc
->xl_storage
= bucket
;
2295 loc
->xl_header
= bucket_xh(bucket
);
2296 loc
->xl_entry
= entry
;
2297 loc
->xl_size
= OCFS2_XATTR_BUCKET_SIZE
;
2301 * In xattr remove, if it is stored outside and refcounted, we may have
2302 * the chance to split the refcount tree. So need the allocators.
2304 static int ocfs2_lock_xattr_remove_allocators(struct inode
*inode
,
2305 struct ocfs2_xattr_value_root
*xv
,
2306 struct ocfs2_caching_info
*ref_ci
,
2307 struct buffer_head
*ref_root_bh
,
2308 struct ocfs2_alloc_context
**meta_ac
,
2311 int ret
, meta_add
= 0;
2312 u32 p_cluster
, num_clusters
;
2313 unsigned int ext_flags
;
2316 ret
= ocfs2_xattr_get_clusters(inode
, 0, &p_cluster
,
2325 if (!(ext_flags
& OCFS2_EXT_REFCOUNTED
))
2328 ret
= ocfs2_refcounted_xattr_delete_need(inode
, ref_ci
,
2330 &meta_add
, ref_credits
);
2336 ret
= ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode
->i_sb
),
2345 static int ocfs2_remove_value_outside(struct inode
*inode
,
2346 struct ocfs2_xattr_value_buf
*vb
,
2347 struct ocfs2_xattr_header
*header
,
2348 struct ocfs2_caching_info
*ref_ci
,
2349 struct buffer_head
*ref_root_bh
)
2351 int ret
= 0, i
, ref_credits
;
2352 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2353 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, };
2356 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
2358 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
2359 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
2361 if (ocfs2_xattr_is_local(entry
))
2364 val
= (void *)header
+
2365 le16_to_cpu(entry
->xe_name_offset
);
2366 vb
->vb_xv
= (struct ocfs2_xattr_value_root
*)
2367 (val
+ OCFS2_XATTR_SIZE(entry
->xe_name_len
));
2369 ret
= ocfs2_lock_xattr_remove_allocators(inode
, vb
->vb_xv
,
2370 ref_ci
, ref_root_bh
,
2374 ctxt
.handle
= ocfs2_start_trans(osb
, ref_credits
+
2375 ocfs2_remove_extent_credits(osb
->sb
));
2376 if (IS_ERR(ctxt
.handle
)) {
2377 ret
= PTR_ERR(ctxt
.handle
);
2382 ret
= ocfs2_xattr_value_truncate(inode
, vb
, 0, &ctxt
);
2384 ocfs2_commit_trans(osb
, ctxt
.handle
);
2386 ocfs2_free_alloc_context(ctxt
.meta_ac
);
2387 ctxt
.meta_ac
= NULL
;
2398 ocfs2_free_alloc_context(ctxt
.meta_ac
);
2399 ocfs2_schedule_truncate_log_flush(osb
, 1);
2400 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
2404 static int ocfs2_xattr_ibody_remove(struct inode
*inode
,
2405 struct buffer_head
*di_bh
,
2406 struct ocfs2_caching_info
*ref_ci
,
2407 struct buffer_head
*ref_root_bh
)
2410 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2411 struct ocfs2_xattr_header
*header
;
2413 struct ocfs2_xattr_value_buf vb
= {
2415 .vb_access
= ocfs2_journal_access_di
,
2418 header
= (struct ocfs2_xattr_header
*)
2419 ((void *)di
+ inode
->i_sb
->s_blocksize
-
2420 le16_to_cpu(di
->i_xattr_inline_size
));
2422 ret
= ocfs2_remove_value_outside(inode
, &vb
, header
,
2423 ref_ci
, ref_root_bh
);
2428 struct ocfs2_rm_xattr_bucket_para
{
2429 struct ocfs2_caching_info
*ref_ci
;
2430 struct buffer_head
*ref_root_bh
;
2433 static int ocfs2_xattr_block_remove(struct inode
*inode
,
2434 struct buffer_head
*blk_bh
,
2435 struct ocfs2_caching_info
*ref_ci
,
2436 struct buffer_head
*ref_root_bh
)
2438 struct ocfs2_xattr_block
*xb
;
2440 struct ocfs2_xattr_value_buf vb
= {
2442 .vb_access
= ocfs2_journal_access_xb
,
2444 struct ocfs2_rm_xattr_bucket_para args
= {
2446 .ref_root_bh
= ref_root_bh
,
2449 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2450 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2451 struct ocfs2_xattr_header
*header
= &(xb
->xb_attrs
.xb_header
);
2452 ret
= ocfs2_remove_value_outside(inode
, &vb
, header
,
2453 ref_ci
, ref_root_bh
);
2455 ret
= ocfs2_iterate_xattr_index_block(inode
,
2457 ocfs2_rm_xattr_cluster
,
2463 static int ocfs2_xattr_free_block(struct inode
*inode
,
2465 struct ocfs2_caching_info
*ref_ci
,
2466 struct buffer_head
*ref_root_bh
)
2468 struct inode
*xb_alloc_inode
;
2469 struct buffer_head
*xb_alloc_bh
= NULL
;
2470 struct buffer_head
*blk_bh
= NULL
;
2471 struct ocfs2_xattr_block
*xb
;
2472 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2478 ret
= ocfs2_read_xattr_block(inode
, block
, &blk_bh
);
2484 ret
= ocfs2_xattr_block_remove(inode
, blk_bh
, ref_ci
, ref_root_bh
);
2490 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2491 blk
= le64_to_cpu(xb
->xb_blkno
);
2492 bit
= le16_to_cpu(xb
->xb_suballoc_bit
);
2493 if (xb
->xb_suballoc_loc
)
2494 bg_blkno
= le64_to_cpu(xb
->xb_suballoc_loc
);
2496 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
2498 xb_alloc_inode
= ocfs2_get_system_file_inode(osb
,
2499 EXTENT_ALLOC_SYSTEM_INODE
,
2500 le16_to_cpu(xb
->xb_suballoc_slot
));
2501 if (!xb_alloc_inode
) {
2506 mutex_lock(&xb_alloc_inode
->i_mutex
);
2508 ret
= ocfs2_inode_lock(xb_alloc_inode
, &xb_alloc_bh
, 1);
2514 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
2515 if (IS_ERR(handle
)) {
2516 ret
= PTR_ERR(handle
);
2521 ret
= ocfs2_free_suballoc_bits(handle
, xb_alloc_inode
, xb_alloc_bh
,
2526 ocfs2_commit_trans(osb
, handle
);
2528 ocfs2_inode_unlock(xb_alloc_inode
, 1);
2529 brelse(xb_alloc_bh
);
2531 mutex_unlock(&xb_alloc_inode
->i_mutex
);
2532 iput(xb_alloc_inode
);
2539 * ocfs2_xattr_remove()
2541 * Free extended attribute resources associated with this inode.
2543 int ocfs2_xattr_remove(struct inode
*inode
, struct buffer_head
*di_bh
)
2545 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2546 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2547 struct ocfs2_refcount_tree
*ref_tree
= NULL
;
2548 struct buffer_head
*ref_root_bh
= NULL
;
2549 struct ocfs2_caching_info
*ref_ci
= NULL
;
2553 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
2556 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
2559 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
) {
2560 ret
= ocfs2_lock_refcount_tree(OCFS2_SB(inode
->i_sb
),
2561 le64_to_cpu(di
->i_refcount_loc
),
2562 1, &ref_tree
, &ref_root_bh
);
2567 ref_ci
= &ref_tree
->rf_ci
;
2571 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
2572 ret
= ocfs2_xattr_ibody_remove(inode
, di_bh
,
2573 ref_ci
, ref_root_bh
);
2580 if (di
->i_xattr_loc
) {
2581 ret
= ocfs2_xattr_free_block(inode
,
2582 le64_to_cpu(di
->i_xattr_loc
),
2583 ref_ci
, ref_root_bh
);
2590 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
2591 OCFS2_INODE_UPDATE_CREDITS
);
2592 if (IS_ERR(handle
)) {
2593 ret
= PTR_ERR(handle
);
2597 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
2598 OCFS2_JOURNAL_ACCESS_WRITE
);
2604 di
->i_xattr_loc
= 0;
2606 spin_lock(&oi
->ip_lock
);
2607 oi
->ip_dyn_features
&= ~(OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
);
2608 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
2609 spin_unlock(&oi
->ip_lock
);
2610 ocfs2_update_inode_fsync_trans(handle
, inode
, 0);
2612 ocfs2_journal_dirty(handle
, di_bh
);
2614 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
2617 ocfs2_unlock_refcount_tree(OCFS2_SB(inode
->i_sb
), ref_tree
, 1);
2618 brelse(ref_root_bh
);
2622 static int ocfs2_xattr_has_space_inline(struct inode
*inode
,
2623 struct ocfs2_dinode
*di
)
2625 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2626 unsigned int xattrsize
= OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
;
2629 if (xattrsize
< OCFS2_MIN_XATTR_INLINE_SIZE
)
2632 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
2633 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
2634 free
= le16_to_cpu(idata
->id_count
) - le64_to_cpu(di
->i_size
);
2635 } else if (ocfs2_inode_is_fast_symlink(inode
)) {
2636 free
= ocfs2_fast_symlink_chars(inode
->i_sb
) -
2637 le64_to_cpu(di
->i_size
);
2639 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
2640 free
= (le16_to_cpu(el
->l_count
) -
2641 le16_to_cpu(el
->l_next_free_rec
)) *
2642 sizeof(struct ocfs2_extent_rec
);
2644 if (free
>= xattrsize
)
2651 * ocfs2_xattr_ibody_find()
2653 * Find extended attribute in inode block and
2654 * fill search info into struct ocfs2_xattr_search.
2656 static int ocfs2_xattr_ibody_find(struct inode
*inode
,
2659 struct ocfs2_xattr_search
*xs
)
2661 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2662 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2666 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
2669 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
2670 down_read(&oi
->ip_alloc_sem
);
2671 has_space
= ocfs2_xattr_has_space_inline(inode
, di
);
2672 up_read(&oi
->ip_alloc_sem
);
2677 xs
->xattr_bh
= xs
->inode_bh
;
2678 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
2679 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)
2680 xs
->header
= (struct ocfs2_xattr_header
*)
2681 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
2683 xs
->header
= (struct ocfs2_xattr_header
*)
2684 (xs
->end
- OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
);
2685 xs
->base
= (void *)xs
->header
;
2686 xs
->here
= xs
->header
->xh_entries
;
2688 /* Find the named attribute. */
2689 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
2690 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
2691 if (ret
&& ret
!= -ENODATA
)
2693 xs
->not_found
= ret
;
2699 static int ocfs2_xattr_ibody_init(struct inode
*inode
,
2700 struct buffer_head
*di_bh
,
2701 struct ocfs2_xattr_set_ctxt
*ctxt
)
2704 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2705 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2706 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2707 unsigned int xattrsize
= osb
->s_xattr_inline_size
;
2709 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
2714 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
), di_bh
,
2715 OCFS2_JOURNAL_ACCESS_WRITE
);
2722 * Adjust extent record count or inline data size
2723 * to reserve space for extended attribute.
2725 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
2726 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
2727 le16_add_cpu(&idata
->id_count
, -xattrsize
);
2728 } else if (!(ocfs2_inode_is_fast_symlink(inode
))) {
2729 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
2730 le16_add_cpu(&el
->l_count
, -(xattrsize
/
2731 sizeof(struct ocfs2_extent_rec
)));
2733 di
->i_xattr_inline_size
= cpu_to_le16(xattrsize
);
2735 spin_lock(&oi
->ip_lock
);
2736 oi
->ip_dyn_features
|= OCFS2_INLINE_XATTR_FL
|OCFS2_HAS_XATTR_FL
;
2737 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
2738 spin_unlock(&oi
->ip_lock
);
2740 ocfs2_journal_dirty(ctxt
->handle
, di_bh
);
2747 * ocfs2_xattr_ibody_set()
2749 * Set, replace or remove an extended attribute into inode block.
2752 static int ocfs2_xattr_ibody_set(struct inode
*inode
,
2753 struct ocfs2_xattr_info
*xi
,
2754 struct ocfs2_xattr_search
*xs
,
2755 struct ocfs2_xattr_set_ctxt
*ctxt
)
2758 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2759 struct ocfs2_xa_loc loc
;
2761 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
2764 down_write(&oi
->ip_alloc_sem
);
2765 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
2766 ret
= ocfs2_xattr_ibody_init(inode
, xs
->inode_bh
, ctxt
);
2774 ocfs2_init_dinode_xa_loc(&loc
, inode
, xs
->inode_bh
,
2775 xs
->not_found
? NULL
: xs
->here
);
2776 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
2782 xs
->here
= loc
.xl_entry
;
2785 up_write(&oi
->ip_alloc_sem
);
2791 * ocfs2_xattr_block_find()
2793 * Find extended attribute in external block and
2794 * fill search info into struct ocfs2_xattr_search.
2796 static int ocfs2_xattr_block_find(struct inode
*inode
,
2799 struct ocfs2_xattr_search
*xs
)
2801 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2802 struct buffer_head
*blk_bh
= NULL
;
2803 struct ocfs2_xattr_block
*xb
;
2806 if (!di
->i_xattr_loc
)
2809 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
2816 xs
->xattr_bh
= blk_bh
;
2817 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2819 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2820 xs
->header
= &xb
->xb_attrs
.xb_header
;
2821 xs
->base
= (void *)xs
->header
;
2822 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
2823 xs
->here
= xs
->header
->xh_entries
;
2825 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
2827 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
2831 if (ret
&& ret
!= -ENODATA
) {
2832 xs
->xattr_bh
= NULL
;
2835 xs
->not_found
= ret
;
2843 static int ocfs2_create_xattr_block(struct inode
*inode
,
2844 struct buffer_head
*inode_bh
,
2845 struct ocfs2_xattr_set_ctxt
*ctxt
,
2847 struct buffer_head
**ret_bh
)
2850 u16 suballoc_bit_start
;
2852 u64 suballoc_loc
, first_blkno
;
2853 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)inode_bh
->b_data
;
2854 struct buffer_head
*new_bh
= NULL
;
2855 struct ocfs2_xattr_block
*xblk
;
2857 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
),
2858 inode_bh
, OCFS2_JOURNAL_ACCESS_CREATE
);
2864 ret
= ocfs2_claim_metadata(ctxt
->handle
, ctxt
->meta_ac
, 1,
2865 &suballoc_loc
, &suballoc_bit_start
,
2866 &num_got
, &first_blkno
);
2872 new_bh
= sb_getblk(inode
->i_sb
, first_blkno
);
2879 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode
), new_bh
);
2881 ret
= ocfs2_journal_access_xb(ctxt
->handle
, INODE_CACHE(inode
),
2883 OCFS2_JOURNAL_ACCESS_CREATE
);
2889 /* Initialize ocfs2_xattr_block */
2890 xblk
= (struct ocfs2_xattr_block
*)new_bh
->b_data
;
2891 memset(xblk
, 0, inode
->i_sb
->s_blocksize
);
2892 strcpy((void *)xblk
, OCFS2_XATTR_BLOCK_SIGNATURE
);
2893 xblk
->xb_suballoc_slot
= cpu_to_le16(ctxt
->meta_ac
->ac_alloc_slot
);
2894 xblk
->xb_suballoc_loc
= cpu_to_le64(suballoc_loc
);
2895 xblk
->xb_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
2896 xblk
->xb_fs_generation
=
2897 cpu_to_le32(OCFS2_SB(inode
->i_sb
)->fs_generation
);
2898 xblk
->xb_blkno
= cpu_to_le64(first_blkno
);
2900 struct ocfs2_xattr_tree_root
*xr
= &xblk
->xb_attrs
.xb_root
;
2901 xr
->xt_clusters
= cpu_to_le32(1);
2902 xr
->xt_last_eb_blk
= 0;
2903 xr
->xt_list
.l_tree_depth
= 0;
2904 xr
->xt_list
.l_count
= cpu_to_le16(
2905 ocfs2_xattr_recs_per_xb(inode
->i_sb
));
2906 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
2907 xblk
->xb_flags
= cpu_to_le16(OCFS2_XATTR_INDEXED
);
2909 ocfs2_journal_dirty(ctxt
->handle
, new_bh
);
2911 /* Add it to the inode */
2912 di
->i_xattr_loc
= cpu_to_le64(first_blkno
);
2914 spin_lock(&OCFS2_I(inode
)->ip_lock
);
2915 OCFS2_I(inode
)->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
;
2916 di
->i_dyn_features
= cpu_to_le16(OCFS2_I(inode
)->ip_dyn_features
);
2917 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
2919 ocfs2_journal_dirty(ctxt
->handle
, inode_bh
);
2930 * ocfs2_xattr_block_set()
2932 * Set, replace or remove an extended attribute into external block.
2935 static int ocfs2_xattr_block_set(struct inode
*inode
,
2936 struct ocfs2_xattr_info
*xi
,
2937 struct ocfs2_xattr_search
*xs
,
2938 struct ocfs2_xattr_set_ctxt
*ctxt
)
2940 struct buffer_head
*new_bh
= NULL
;
2941 struct ocfs2_xattr_block
*xblk
= NULL
;
2943 struct ocfs2_xa_loc loc
;
2945 if (!xs
->xattr_bh
) {
2946 ret
= ocfs2_create_xattr_block(inode
, xs
->inode_bh
, ctxt
,
2953 xs
->xattr_bh
= new_bh
;
2954 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
2955 xs
->header
= &xblk
->xb_attrs
.xb_header
;
2956 xs
->base
= (void *)xs
->header
;
2957 xs
->end
= (void *)xblk
+ inode
->i_sb
->s_blocksize
;
2958 xs
->here
= xs
->header
->xh_entries
;
2960 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
2962 if (!(le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2963 ocfs2_init_xattr_block_xa_loc(&loc
, inode
, xs
->xattr_bh
,
2964 xs
->not_found
? NULL
: xs
->here
);
2966 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
2968 xs
->here
= loc
.xl_entry
;
2969 else if ((ret
!= -ENOSPC
) || ctxt
->set_abort
)
2972 ret
= ocfs2_xattr_create_index_block(inode
, xs
, ctxt
);
2978 if (le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)
2979 ret
= ocfs2_xattr_set_entry_index_block(inode
, xi
, xs
, ctxt
);
2985 /* Check whether the new xattr can be inserted into the inode. */
2986 static int ocfs2_xattr_can_be_in_inode(struct inode
*inode
,
2987 struct ocfs2_xattr_info
*xi
,
2988 struct ocfs2_xattr_search
*xs
)
2990 struct ocfs2_xattr_entry
*last
;
2992 size_t min_offs
= xs
->end
- xs
->base
;
2997 last
= xs
->header
->xh_entries
;
2999 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
3000 size_t offs
= le16_to_cpu(last
->xe_name_offset
);
3001 if (offs
< min_offs
)
3006 free
= min_offs
- ((void *)last
- xs
->base
) - OCFS2_XATTR_HEADER_GAP
;
3010 BUG_ON(!xs
->not_found
);
3012 if (free
>= (sizeof(struct ocfs2_xattr_entry
) + namevalue_size_xi(xi
)))
3018 static int ocfs2_calc_xattr_set_need(struct inode
*inode
,
3019 struct ocfs2_dinode
*di
,
3020 struct ocfs2_xattr_info
*xi
,
3021 struct ocfs2_xattr_search
*xis
,
3022 struct ocfs2_xattr_search
*xbs
,
3027 int ret
= 0, old_in_xb
= 0;
3028 int clusters_add
= 0, meta_add
= 0, credits
= 0;
3029 struct buffer_head
*bh
= NULL
;
3030 struct ocfs2_xattr_block
*xb
= NULL
;
3031 struct ocfs2_xattr_entry
*xe
= NULL
;
3032 struct ocfs2_xattr_value_root
*xv
= NULL
;
3034 int name_offset
, name_len
= 0;
3035 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
3040 * Calculate the clusters we need to write.
3041 * No matter whether we replace an old one or add a new one,
3042 * we need this for writing.
3044 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
)
3045 credits
+= new_clusters
*
3046 ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3048 if (xis
->not_found
&& xbs
->not_found
) {
3049 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3051 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
3052 clusters_add
+= new_clusters
;
3053 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3054 &def_xv
.xv
.xr_list
);
3060 if (!xis
->not_found
) {
3062 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3063 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3065 credits
+= OCFS2_INODE_UPDATE_CREDITS
;
3067 int i
, block_off
= 0;
3068 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
3070 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3071 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3072 i
= xbs
->here
- xbs
->header
->xh_entries
;
3075 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
3076 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
3077 bucket_xh(xbs
->bucket
),
3080 base
= bucket_block(xbs
->bucket
, block_off
);
3081 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3084 credits
+= OCFS2_XATTR_BLOCK_UPDATE_CREDITS
;
3089 * delete a xattr doesn't need metadata and cluster allocation.
3090 * so just calculate the credits and return.
3092 * The credits for removing the value tree will be extended
3093 * by ocfs2_remove_extent itself.
3095 if (!xi
->xi_value
) {
3096 if (!ocfs2_xattr_is_local(xe
))
3097 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
);
3102 /* do cluster allocation guess first. */
3103 value_size
= le64_to_cpu(xe
->xe_value_size
);
3107 * In xattr set, we always try to set the xe in inode first,
3108 * so if it can be inserted into inode successfully, the old
3109 * one will be removed from the xattr block, and this xattr
3110 * will be inserted into inode as a new xattr in inode.
3112 if (ocfs2_xattr_can_be_in_inode(inode
, xi
, xis
)) {
3113 clusters_add
+= new_clusters
;
3114 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
) +
3115 OCFS2_INODE_UPDATE_CREDITS
;
3116 if (!ocfs2_xattr_is_local(xe
))
3117 credits
+= ocfs2_calc_extend_credits(
3119 &def_xv
.xv
.xr_list
);
3124 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
3125 /* the new values will be stored outside. */
3126 u32 old_clusters
= 0;
3128 if (!ocfs2_xattr_is_local(xe
)) {
3129 old_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
3131 xv
= (struct ocfs2_xattr_value_root
*)
3132 (base
+ name_offset
+ name_len
);
3133 value_size
= OCFS2_XATTR_ROOT_SIZE
;
3137 if (old_clusters
>= new_clusters
) {
3138 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
);
3141 meta_add
+= ocfs2_extend_meta_needed(&xv
->xr_list
);
3142 clusters_add
+= new_clusters
- old_clusters
;
3143 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3145 if (value_size
>= OCFS2_XATTR_ROOT_SIZE
)
3150 * Now the new value will be stored inside. So if the new
3151 * value is smaller than the size of value root or the old
3152 * value, we don't need any allocation, otherwise we have
3153 * to guess metadata allocation.
3155 if ((ocfs2_xattr_is_local(xe
) &&
3156 (value_size
>= xi
->xi_value_len
)) ||
3157 (!ocfs2_xattr_is_local(xe
) &&
3158 OCFS2_XATTR_ROOT_SIZE
>= xi
->xi_value_len
))
3163 /* calculate metadata allocation. */
3164 if (di
->i_xattr_loc
) {
3165 if (!xbs
->xattr_bh
) {
3166 ret
= ocfs2_read_xattr_block(inode
,
3167 le64_to_cpu(di
->i_xattr_loc
),
3174 xb
= (struct ocfs2_xattr_block
*)bh
->b_data
;
3176 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
3179 * If there is already an xattr tree, good, we can calculate
3180 * like other b-trees. Otherwise we may have the chance of
3181 * create a tree, the credit calculation is borrowed from
3182 * ocfs2_calc_extend_credits with root_el = NULL. And the
3183 * new tree will be cluster based, so no meta is needed.
3185 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
3186 struct ocfs2_extent_list
*el
=
3187 &xb
->xb_attrs
.xb_root
.xt_list
;
3188 meta_add
+= ocfs2_extend_meta_needed(el
);
3189 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3192 credits
+= OCFS2_SUBALLOC_ALLOC
+ 1;
3195 * This cluster will be used either for new bucket or for
3197 * If the cluster size is the same as the bucket size, one
3198 * more is needed since we may need to extend the bucket
3202 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3203 if (OCFS2_XATTR_BUCKET_SIZE
==
3204 OCFS2_SB(inode
->i_sb
)->s_clustersize
) {
3205 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3209 credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
3210 if (xi
->xi_value_len
> OCFS2_XATTR_INLINE_SIZE
) {
3211 struct ocfs2_extent_list
*el
= &def_xv
.xv
.xr_list
;
3212 meta_add
+= ocfs2_extend_meta_needed(el
);
3213 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3221 *clusters_need
= clusters_add
;
3223 *meta_need
= meta_add
;
3225 *credits_need
= credits
;
3230 static int ocfs2_init_xattr_set_ctxt(struct inode
*inode
,
3231 struct ocfs2_dinode
*di
,
3232 struct ocfs2_xattr_info
*xi
,
3233 struct ocfs2_xattr_search
*xis
,
3234 struct ocfs2_xattr_search
*xbs
,
3235 struct ocfs2_xattr_set_ctxt
*ctxt
,
3239 int clusters_add
, meta_add
, ret
;
3240 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3242 memset(ctxt
, 0, sizeof(struct ocfs2_xattr_set_ctxt
));
3244 ocfs2_init_dealloc_ctxt(&ctxt
->dealloc
);
3246 ret
= ocfs2_calc_xattr_set_need(inode
, di
, xi
, xis
, xbs
,
3247 &clusters_add
, &meta_add
, credits
);
3253 meta_add
+= extra_meta
;
3254 trace_ocfs2_init_xattr_set_ctxt(xi
->xi_name
, meta_add
,
3255 clusters_add
, *credits
);
3258 ret
= ocfs2_reserve_new_metadata_blocks(osb
, meta_add
,
3267 ret
= ocfs2_reserve_clusters(osb
, clusters_add
, &ctxt
->data_ac
);
3273 if (ctxt
->meta_ac
) {
3274 ocfs2_free_alloc_context(ctxt
->meta_ac
);
3275 ctxt
->meta_ac
= NULL
;
3279 * We cannot have an error and a non null ctxt->data_ac.
3286 static int __ocfs2_xattr_set_handle(struct inode
*inode
,
3287 struct ocfs2_dinode
*di
,
3288 struct ocfs2_xattr_info
*xi
,
3289 struct ocfs2_xattr_search
*xis
,
3290 struct ocfs2_xattr_search
*xbs
,
3291 struct ocfs2_xattr_set_ctxt
*ctxt
)
3293 int ret
= 0, credits
, old_found
;
3295 if (!xi
->xi_value
) {
3296 /* Remove existing extended attribute */
3297 if (!xis
->not_found
)
3298 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
3299 else if (!xbs
->not_found
)
3300 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3302 /* We always try to set extended attribute into inode first*/
3303 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
3304 if (!ret
&& !xbs
->not_found
) {
3306 * If succeed and that extended attribute existing in
3307 * external block, then we will remove it.
3309 xi
->xi_value
= NULL
;
3310 xi
->xi_value_len
= 0;
3312 old_found
= xis
->not_found
;
3313 xis
->not_found
= -ENODATA
;
3314 ret
= ocfs2_calc_xattr_set_need(inode
,
3322 xis
->not_found
= old_found
;
3328 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
);
3333 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3334 } else if ((ret
== -ENOSPC
) && !ctxt
->set_abort
) {
3335 if (di
->i_xattr_loc
&& !xbs
->xattr_bh
) {
3336 ret
= ocfs2_xattr_block_find(inode
,
3342 old_found
= xis
->not_found
;
3343 xis
->not_found
= -ENODATA
;
3344 ret
= ocfs2_calc_xattr_set_need(inode
,
3352 xis
->not_found
= old_found
;
3358 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
);
3365 * If no space in inode, we will set extended attribute
3366 * into external block.
3368 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
3371 if (!xis
->not_found
) {
3373 * If succeed and that extended attribute
3374 * existing in inode, we will remove it.
3376 xi
->xi_value
= NULL
;
3377 xi
->xi_value_len
= 0;
3378 xbs
->not_found
= -ENODATA
;
3379 ret
= ocfs2_calc_xattr_set_need(inode
,
3392 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
);
3397 ret
= ocfs2_xattr_ibody_set(inode
, xi
,
3404 /* Update inode ctime. */
3405 ret
= ocfs2_journal_access_di(ctxt
->handle
, INODE_CACHE(inode
),
3407 OCFS2_JOURNAL_ACCESS_WRITE
);
3413 inode
->i_ctime
= CURRENT_TIME
;
3414 di
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
3415 di
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
3416 ocfs2_journal_dirty(ctxt
->handle
, xis
->inode_bh
);
3423 * This function only called duing creating inode
3424 * for init security/acl xattrs of the new inode.
3425 * All transanction credits have been reserved in mknod.
3427 int ocfs2_xattr_set_handle(handle_t
*handle
,
3428 struct inode
*inode
,
3429 struct buffer_head
*di_bh
,
3435 struct ocfs2_alloc_context
*meta_ac
,
3436 struct ocfs2_alloc_context
*data_ac
)
3438 struct ocfs2_dinode
*di
;
3441 struct ocfs2_xattr_info xi
= {
3442 .xi_name_index
= name_index
,
3444 .xi_name_len
= strlen(name
),
3446 .xi_value_len
= value_len
,
3449 struct ocfs2_xattr_search xis
= {
3450 .not_found
= -ENODATA
,
3453 struct ocfs2_xattr_search xbs
= {
3454 .not_found
= -ENODATA
,
3457 struct ocfs2_xattr_set_ctxt ctxt
= {
3463 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
3467 * In extreme situation, may need xattr bucket when
3468 * block size is too small. And we have already reserved
3469 * the credits for bucket in mknod.
3471 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
) {
3472 xbs
.bucket
= ocfs2_xattr_bucket_new(inode
);
3474 mlog_errno(-ENOMEM
);
3479 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
3480 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3482 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3484 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
3487 if (xis
.not_found
) {
3488 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
3493 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
3496 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3497 brelse(xbs
.xattr_bh
);
3498 ocfs2_xattr_bucket_free(xbs
.bucket
);
3506 * Set, replace or remove an extended attribute for this inode.
3507 * value is NULL to remove an existing extended attribute, else either
3508 * create or replace an extended attribute.
3510 int ocfs2_xattr_set(struct inode
*inode
,
3517 struct buffer_head
*di_bh
= NULL
;
3518 struct ocfs2_dinode
*di
;
3519 int ret
, credits
, ref_meta
= 0, ref_credits
= 0;
3520 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3521 struct inode
*tl_inode
= osb
->osb_tl_inode
;
3522 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, NULL
, };
3523 struct ocfs2_refcount_tree
*ref_tree
= NULL
;
3525 struct ocfs2_xattr_info xi
= {
3526 .xi_name_index
= name_index
,
3528 .xi_name_len
= strlen(name
),
3530 .xi_value_len
= value_len
,
3533 struct ocfs2_xattr_search xis
= {
3534 .not_found
= -ENODATA
,
3537 struct ocfs2_xattr_search xbs
= {
3538 .not_found
= -ENODATA
,
3541 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
3545 * Only xbs will be used on indexed trees. xis doesn't need a
3548 xbs
.bucket
= ocfs2_xattr_bucket_new(inode
);
3550 mlog_errno(-ENOMEM
);
3554 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
3557 goto cleanup_nolock
;
3559 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
3560 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3562 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3564 * Scan inode and external block to find the same name
3565 * extended attribute and collect search information.
3567 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
3570 if (xis
.not_found
) {
3571 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
3576 if (xis
.not_found
&& xbs
.not_found
) {
3578 if (flags
& XATTR_REPLACE
)
3585 if (flags
& XATTR_CREATE
)
3589 /* Check whether the value is refcounted and do some preparation. */
3590 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
&&
3591 (!xis
.not_found
|| !xbs
.not_found
)) {
3592 ret
= ocfs2_prepare_refcount_xattr(inode
, di
, &xi
,
3593 &xis
, &xbs
, &ref_tree
,
3594 &ref_meta
, &ref_credits
);
3601 mutex_lock(&tl_inode
->i_mutex
);
3603 if (ocfs2_truncate_log_needs_flush(osb
)) {
3604 ret
= __ocfs2_flush_truncate_log(osb
);
3606 mutex_unlock(&tl_inode
->i_mutex
);
3611 mutex_unlock(&tl_inode
->i_mutex
);
3613 ret
= ocfs2_init_xattr_set_ctxt(inode
, di
, &xi
, &xis
,
3614 &xbs
, &ctxt
, ref_meta
, &credits
);
3620 /* we need to update inode's ctime field, so add credit for it. */
3621 credits
+= OCFS2_INODE_UPDATE_CREDITS
;
3622 ctxt
.handle
= ocfs2_start_trans(osb
, credits
+ ref_credits
);
3623 if (IS_ERR(ctxt
.handle
)) {
3624 ret
= PTR_ERR(ctxt
.handle
);
3629 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
3630 ocfs2_update_inode_fsync_trans(ctxt
.handle
, inode
, 0);
3632 ocfs2_commit_trans(osb
, ctxt
.handle
);
3636 ocfs2_free_alloc_context(ctxt
.data_ac
);
3638 ocfs2_free_alloc_context(ctxt
.meta_ac
);
3639 if (ocfs2_dealloc_has_cluster(&ctxt
.dealloc
))
3640 ocfs2_schedule_truncate_log_flush(osb
, 1);
3641 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
3645 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
3646 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
3647 if (!value
&& !ret
) {
3648 ret
= ocfs2_try_remove_refcount_tree(inode
, di_bh
);
3652 ocfs2_inode_unlock(inode
, 1);
3655 brelse(xbs
.xattr_bh
);
3656 ocfs2_xattr_bucket_free(xbs
.bucket
);
3662 * Find the xattr extent rec which may contains name_hash.
3663 * e_cpos will be the first name hash of the xattr rec.
3664 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3666 static int ocfs2_xattr_get_rec(struct inode
*inode
,
3671 struct ocfs2_extent_list
*el
)
3674 struct buffer_head
*eb_bh
= NULL
;
3675 struct ocfs2_extent_block
*eb
;
3676 struct ocfs2_extent_rec
*rec
= NULL
;
3679 if (el
->l_tree_depth
) {
3680 ret
= ocfs2_find_leaf(INODE_CACHE(inode
), el
, name_hash
,
3687 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
3690 if (el
->l_tree_depth
) {
3691 ret
= ocfs2_error(inode
->i_sb
,
3692 "Inode %lu has non zero tree depth in xattr tree block %llu\n",
3694 (unsigned long long)eb_bh
->b_blocknr
);
3699 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
3700 rec
= &el
->l_recs
[i
];
3702 if (le32_to_cpu(rec
->e_cpos
) <= name_hash
) {
3703 e_blkno
= le64_to_cpu(rec
->e_blkno
);
3709 ret
= ocfs2_error(inode
->i_sb
, "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
3711 le32_to_cpu(rec
->e_cpos
),
3712 ocfs2_rec_clusters(el
, rec
));
3716 *p_blkno
= le64_to_cpu(rec
->e_blkno
);
3717 *num_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
3719 *e_cpos
= le32_to_cpu(rec
->e_cpos
);
3725 typedef int (xattr_bucket_func
)(struct inode
*inode
,
3726 struct ocfs2_xattr_bucket
*bucket
,
3729 static int ocfs2_find_xe_in_bucket(struct inode
*inode
,
3730 struct ocfs2_xattr_bucket
*bucket
,
3737 int i
, ret
= 0, cmp
= 1, block_off
, new_offset
;
3738 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
3739 size_t name_len
= strlen(name
);
3740 struct ocfs2_xattr_entry
*xe
= NULL
;
3744 * We don't use binary search in the bucket because there
3745 * may be multiple entries with the same name hash.
3747 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
3748 xe
= &xh
->xh_entries
[i
];
3750 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
))
3752 else if (name_hash
< le32_to_cpu(xe
->xe_name_hash
))
3755 cmp
= name_index
- ocfs2_xattr_get_type(xe
);
3757 cmp
= name_len
- xe
->xe_name_len
;
3761 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
3772 xe_name
= bucket_block(bucket
, block_off
) + new_offset
;
3773 if (!memcmp(name
, xe_name
, name_len
)) {
3785 * Find the specified xattr entry in a series of buckets.
3786 * This series start from p_blkno and last for num_clusters.
3787 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3788 * the num of the valid buckets.
3790 * Return the buffer_head this xattr should reside in. And if the xattr's
3791 * hash is in the gap of 2 buckets, return the lower bucket.
3793 static int ocfs2_xattr_bucket_find(struct inode
*inode
,
3800 struct ocfs2_xattr_search
*xs
)
3803 struct ocfs2_xattr_header
*xh
= NULL
;
3804 struct ocfs2_xattr_entry
*xe
= NULL
;
3806 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3807 int low_bucket
= 0, bucket
, high_bucket
;
3808 struct ocfs2_xattr_bucket
*search
;
3810 u64 blkno
, lower_blkno
= 0;
3812 search
= ocfs2_xattr_bucket_new(inode
);
3819 ret
= ocfs2_read_xattr_bucket(search
, p_blkno
);
3825 xh
= bucket_xh(search
);
3826 high_bucket
= le16_to_cpu(xh
->xh_num_buckets
) - 1;
3827 while (low_bucket
<= high_bucket
) {
3828 ocfs2_xattr_bucket_relse(search
);
3830 bucket
= (low_bucket
+ high_bucket
) / 2;
3831 blkno
= p_blkno
+ bucket
* blk_per_bucket
;
3832 ret
= ocfs2_read_xattr_bucket(search
, blkno
);
3838 xh
= bucket_xh(search
);
3839 xe
= &xh
->xh_entries
[0];
3840 if (name_hash
< le32_to_cpu(xe
->xe_name_hash
)) {
3841 high_bucket
= bucket
- 1;
3846 * Check whether the hash of the last entry in our
3847 * bucket is larger than the search one. for an empty
3848 * bucket, the last one is also the first one.
3851 xe
= &xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1];
3853 last_hash
= le32_to_cpu(xe
->xe_name_hash
);
3855 /* record lower_blkno which may be the insert place. */
3856 lower_blkno
= blkno
;
3858 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
)) {
3859 low_bucket
= bucket
+ 1;
3863 /* the searched xattr should reside in this bucket if exists. */
3864 ret
= ocfs2_find_xe_in_bucket(inode
, search
,
3865 name_index
, name
, name_hash
,
3875 * Record the bucket we have found.
3876 * When the xattr's hash value is in the gap of 2 buckets, we will
3877 * always set it to the previous bucket.
3880 lower_blkno
= p_blkno
;
3882 /* This should be in cache - we just read it during the search */
3883 ret
= ocfs2_read_xattr_bucket(xs
->bucket
, lower_blkno
);
3889 xs
->header
= bucket_xh(xs
->bucket
);
3890 xs
->base
= bucket_block(xs
->bucket
, 0);
3891 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
3894 xs
->here
= &xs
->header
->xh_entries
[index
];
3895 trace_ocfs2_xattr_bucket_find(OCFS2_I(inode
)->ip_blkno
,
3896 name
, name_index
, name_hash
,
3897 (unsigned long long)bucket_blkno(xs
->bucket
),
3903 ocfs2_xattr_bucket_free(search
);
3907 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
3908 struct buffer_head
*root_bh
,
3911 struct ocfs2_xattr_search
*xs
)
3914 struct ocfs2_xattr_block
*xb
=
3915 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
3916 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
3917 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
3919 u32 first_hash
, num_clusters
= 0;
3920 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
3922 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
3925 trace_ocfs2_xattr_index_block_find(OCFS2_I(inode
)->ip_blkno
,
3926 name
, name_index
, name_hash
,
3927 (unsigned long long)root_bh
->b_blocknr
,
3930 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &first_hash
,
3937 BUG_ON(p_blkno
== 0 || num_clusters
== 0 || first_hash
> name_hash
);
3939 trace_ocfs2_xattr_index_block_find_rec(OCFS2_I(inode
)->ip_blkno
,
3940 name
, name_index
, first_hash
,
3941 (unsigned long long)p_blkno
,
3944 ret
= ocfs2_xattr_bucket_find(inode
, name_index
, name
, name_hash
,
3945 p_blkno
, first_hash
, num_clusters
, xs
);
3951 static int ocfs2_iterate_xattr_buckets(struct inode
*inode
,
3954 xattr_bucket_func
*func
,
3958 u32 bpc
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
3959 u32 num_buckets
= clusters
* bpc
;
3960 struct ocfs2_xattr_bucket
*bucket
;
3962 bucket
= ocfs2_xattr_bucket_new(inode
);
3964 mlog_errno(-ENOMEM
);
3968 trace_ocfs2_iterate_xattr_buckets(
3969 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
3970 (unsigned long long)blkno
, clusters
);
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 trace_ocfs2_iterate_xattr_bucket((unsigned long long)blkno
,
3987 le32_to_cpu(bucket_xh(bucket
)->xh_entries
[0].xe_name_hash
));
3989 ret
= func(inode
, bucket
, para
);
3990 if (ret
&& ret
!= -ERANGE
)
3992 /* Fall through to bucket_relse() */
3995 ocfs2_xattr_bucket_relse(bucket
);
4000 ocfs2_xattr_bucket_free(bucket
);
4004 struct ocfs2_xattr_tree_list
{
4010 static int ocfs2_xattr_bucket_get_name_value(struct super_block
*sb
,
4011 struct ocfs2_xattr_header
*xh
,
4018 if (index
< 0 || index
>= le16_to_cpu(xh
->xh_count
))
4021 name_offset
= le16_to_cpu(xh
->xh_entries
[index
].xe_name_offset
);
4023 *block_off
= name_offset
>> sb
->s_blocksize_bits
;
4024 *new_offset
= name_offset
% sb
->s_blocksize
;
4029 static int ocfs2_list_xattr_bucket(struct inode
*inode
,
4030 struct ocfs2_xattr_bucket
*bucket
,
4034 struct ocfs2_xattr_tree_list
*xl
= (struct ocfs2_xattr_tree_list
*)para
;
4035 int i
, block_off
, new_offset
;
4036 const char *prefix
, *name
;
4038 for (i
= 0 ; i
< le16_to_cpu(bucket_xh(bucket
)->xh_count
); i
++) {
4039 struct ocfs2_xattr_entry
*entry
= &bucket_xh(bucket
)->xh_entries
[i
];
4040 type
= ocfs2_xattr_get_type(entry
);
4041 prefix
= ocfs2_xattr_prefix(type
);
4044 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
4052 name
= (const char *)bucket_block(bucket
, block_off
) +
4054 ret
= ocfs2_xattr_list_entry(xl
->buffer
,
4058 entry
->xe_name_len
);
4067 static int ocfs2_iterate_xattr_index_block(struct inode
*inode
,
4068 struct buffer_head
*blk_bh
,
4069 xattr_tree_rec_func
*rec_func
,
4072 struct ocfs2_xattr_block
*xb
=
4073 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
4074 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
4076 u32 name_hash
= UINT_MAX
, e_cpos
= 0, num_clusters
= 0;
4079 if (!el
->l_next_free_rec
|| !rec_func
)
4082 while (name_hash
> 0) {
4083 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
4084 &e_cpos
, &num_clusters
, el
);
4090 ret
= rec_func(inode
, blk_bh
, p_blkno
, e_cpos
,
4091 num_clusters
, para
);
4101 name_hash
= e_cpos
- 1;
4108 static int ocfs2_list_xattr_tree_rec(struct inode
*inode
,
4109 struct buffer_head
*root_bh
,
4110 u64 blkno
, u32 cpos
, u32 len
, void *para
)
4112 return ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
4113 ocfs2_list_xattr_bucket
, para
);
4116 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
4117 struct buffer_head
*blk_bh
,
4122 struct ocfs2_xattr_tree_list xl
= {
4124 .buffer_size
= buffer_size
,
4128 ret
= ocfs2_iterate_xattr_index_block(inode
, blk_bh
,
4129 ocfs2_list_xattr_tree_rec
, &xl
);
4140 static int cmp_xe(const void *a
, const void *b
)
4142 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
4143 u32 l_hash
= le32_to_cpu(l
->xe_name_hash
);
4144 u32 r_hash
= le32_to_cpu(r
->xe_name_hash
);
4146 if (l_hash
> r_hash
)
4148 if (l_hash
< r_hash
)
4153 static void swap_xe(void *a
, void *b
, int size
)
4155 struct ocfs2_xattr_entry
*l
= a
, *r
= b
, tmp
;
4158 memcpy(l
, r
, sizeof(struct ocfs2_xattr_entry
));
4159 memcpy(r
, &tmp
, sizeof(struct ocfs2_xattr_entry
));
4163 * When the ocfs2_xattr_block is filled up, new bucket will be created
4164 * and all the xattr entries will be moved to the new bucket.
4165 * The header goes at the start of the bucket, and the names+values are
4166 * filled from the end. This is why *target starts as the last buffer.
4167 * Note: we need to sort the entries since they are not saved in order
4168 * in the ocfs2_xattr_block.
4170 static void ocfs2_cp_xattr_block_to_bucket(struct inode
*inode
,
4171 struct buffer_head
*xb_bh
,
4172 struct ocfs2_xattr_bucket
*bucket
)
4174 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
4175 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4176 u16 offset
, size
, off_change
;
4177 struct ocfs2_xattr_entry
*xe
;
4178 struct ocfs2_xattr_block
*xb
=
4179 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4180 struct ocfs2_xattr_header
*xb_xh
= &xb
->xb_attrs
.xb_header
;
4181 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
4182 u16 count
= le16_to_cpu(xb_xh
->xh_count
);
4183 char *src
= xb_bh
->b_data
;
4184 char *target
= bucket_block(bucket
, blks
- 1);
4186 trace_ocfs2_cp_xattr_block_to_bucket_begin(
4187 (unsigned long long)xb_bh
->b_blocknr
,
4188 (unsigned long long)bucket_blkno(bucket
));
4190 for (i
= 0; i
< blks
; i
++)
4191 memset(bucket_block(bucket
, i
), 0, blocksize
);
4194 * Since the xe_name_offset is based on ocfs2_xattr_header,
4195 * there is a offset change corresponding to the change of
4196 * ocfs2_xattr_header's position.
4198 off_change
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
4199 xe
= &xb_xh
->xh_entries
[count
- 1];
4200 offset
= le16_to_cpu(xe
->xe_name_offset
) + off_change
;
4201 size
= blocksize
- offset
;
4203 /* copy all the names and values. */
4204 memcpy(target
+ offset
, src
+ offset
, size
);
4206 /* Init new header now. */
4207 xh
->xh_count
= xb_xh
->xh_count
;
4208 xh
->xh_num_buckets
= cpu_to_le16(1);
4209 xh
->xh_name_value_len
= cpu_to_le16(size
);
4210 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
- size
);
4212 /* copy all the entries. */
4213 target
= bucket_block(bucket
, 0);
4214 offset
= offsetof(struct ocfs2_xattr_header
, xh_entries
);
4215 size
= count
* sizeof(struct ocfs2_xattr_entry
);
4216 memcpy(target
+ offset
, (char *)xb_xh
+ offset
, size
);
4218 /* Change the xe offset for all the xe because of the move. */
4219 off_change
= OCFS2_XATTR_BUCKET_SIZE
- blocksize
+
4220 offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
4221 for (i
= 0; i
< count
; i
++)
4222 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
, off_change
);
4224 trace_ocfs2_cp_xattr_block_to_bucket_end(offset
, size
, off_change
);
4226 sort(target
+ offset
, count
, sizeof(struct ocfs2_xattr_entry
),
4231 * After we move xattr from block to index btree, we have to
4232 * update ocfs2_xattr_search to the new xe and base.
4234 * When the entry is in xattr block, xattr_bh indicates the storage place.
4235 * While if the entry is in index b-tree, "bucket" indicates the
4236 * real place of the xattr.
4238 static void ocfs2_xattr_update_xattr_search(struct inode
*inode
,
4239 struct ocfs2_xattr_search
*xs
,
4240 struct buffer_head
*old_bh
)
4242 char *buf
= old_bh
->b_data
;
4243 struct ocfs2_xattr_block
*old_xb
= (struct ocfs2_xattr_block
*)buf
;
4244 struct ocfs2_xattr_header
*old_xh
= &old_xb
->xb_attrs
.xb_header
;
4247 xs
->header
= bucket_xh(xs
->bucket
);
4248 xs
->base
= bucket_block(xs
->bucket
, 0);
4249 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
4254 i
= xs
->here
- old_xh
->xh_entries
;
4255 xs
->here
= &xs
->header
->xh_entries
[i
];
4258 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
4259 struct ocfs2_xattr_search
*xs
,
4260 struct ocfs2_xattr_set_ctxt
*ctxt
)
4265 handle_t
*handle
= ctxt
->handle
;
4266 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
4267 struct buffer_head
*xb_bh
= xs
->xattr_bh
;
4268 struct ocfs2_xattr_block
*xb
=
4269 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4270 struct ocfs2_xattr_tree_root
*xr
;
4271 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
4273 trace_ocfs2_xattr_create_index_block_begin(
4274 (unsigned long long)xb_bh
->b_blocknr
);
4276 BUG_ON(xb_flags
& OCFS2_XATTR_INDEXED
);
4277 BUG_ON(!xs
->bucket
);
4281 * We can use this lock for now, and maybe move to a dedicated mutex
4282 * if performance becomes a problem later.
4284 down_write(&oi
->ip_alloc_sem
);
4286 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), xb_bh
,
4287 OCFS2_JOURNAL_ACCESS_WRITE
);
4293 ret
= __ocfs2_claim_clusters(handle
, ctxt
->data_ac
,
4294 1, 1, &bit_off
, &len
);
4301 * The bucket may spread in many blocks, and
4302 * we will only touch the 1st block and the last block
4303 * in the whole bucket(one for entry and one for data).
4305 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
4307 trace_ocfs2_xattr_create_index_block((unsigned long long)blkno
);
4309 ret
= ocfs2_init_xattr_bucket(xs
->bucket
, blkno
, 1);
4315 ret
= ocfs2_xattr_bucket_journal_access(handle
, xs
->bucket
,
4316 OCFS2_JOURNAL_ACCESS_CREATE
);
4322 ocfs2_cp_xattr_block_to_bucket(inode
, xb_bh
, xs
->bucket
);
4323 ocfs2_xattr_bucket_journal_dirty(handle
, xs
->bucket
);
4325 ocfs2_xattr_update_xattr_search(inode
, xs
, xb_bh
);
4327 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4328 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
4329 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
4331 xr
= &xb
->xb_attrs
.xb_root
;
4332 xr
->xt_clusters
= cpu_to_le32(1);
4333 xr
->xt_last_eb_blk
= 0;
4334 xr
->xt_list
.l_tree_depth
= 0;
4335 xr
->xt_list
.l_count
= cpu_to_le16(ocfs2_xattr_recs_per_xb(inode
->i_sb
));
4336 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
4338 xr
->xt_list
.l_recs
[0].e_cpos
= 0;
4339 xr
->xt_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
4340 xr
->xt_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
4342 xb
->xb_flags
= cpu_to_le16(xb_flags
| OCFS2_XATTR_INDEXED
);
4344 ocfs2_journal_dirty(handle
, xb_bh
);
4347 up_write(&oi
->ip_alloc_sem
);
4352 static int cmp_xe_offset(const void *a
, const void *b
)
4354 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
4355 u32 l_name_offset
= le16_to_cpu(l
->xe_name_offset
);
4356 u32 r_name_offset
= le16_to_cpu(r
->xe_name_offset
);
4358 if (l_name_offset
< r_name_offset
)
4360 if (l_name_offset
> r_name_offset
)
4366 * defrag a xattr bucket if we find that the bucket has some
4367 * holes beteen name/value pairs.
4368 * We will move all the name/value pairs to the end of the bucket
4369 * so that we can spare some space for insertion.
4371 static int ocfs2_defrag_xattr_bucket(struct inode
*inode
,
4373 struct ocfs2_xattr_bucket
*bucket
)
4376 size_t end
, offset
, len
;
4377 struct ocfs2_xattr_header
*xh
;
4378 char *entries
, *buf
, *bucket_buf
= NULL
;
4379 u64 blkno
= bucket_blkno(bucket
);
4381 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4382 struct ocfs2_xattr_entry
*xe
;
4385 * In order to make the operation more efficient and generic,
4386 * we copy all the blocks into a contiguous memory and do the
4387 * defragment there, so if anything is error, we will not touch
4390 bucket_buf
= kmalloc(OCFS2_XATTR_BUCKET_SIZE
, GFP_NOFS
);
4397 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
4398 memcpy(buf
, bucket_block(bucket
, i
), blocksize
);
4400 ret
= ocfs2_xattr_bucket_journal_access(handle
, bucket
,
4401 OCFS2_JOURNAL_ACCESS_WRITE
);
4407 xh
= (struct ocfs2_xattr_header
*)bucket_buf
;
4408 entries
= (char *)xh
->xh_entries
;
4409 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4411 trace_ocfs2_defrag_xattr_bucket(
4412 (unsigned long long)blkno
, le16_to_cpu(xh
->xh_count
),
4413 xh_free_start
, le16_to_cpu(xh
->xh_name_value_len
));
4416 * sort all the entries by their offset.
4417 * the largest will be the first, so that we can
4418 * move them to the end one by one.
4420 sort(entries
, le16_to_cpu(xh
->xh_count
),
4421 sizeof(struct ocfs2_xattr_entry
),
4422 cmp_xe_offset
, swap_xe
);
4424 /* Move all name/values to the end of the bucket. */
4425 xe
= xh
->xh_entries
;
4426 end
= OCFS2_XATTR_BUCKET_SIZE
;
4427 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, xe
++) {
4428 offset
= le16_to_cpu(xe
->xe_name_offset
);
4429 len
= namevalue_size_xe(xe
);
4432 * We must make sure that the name/value pair
4433 * exist in the same block. So adjust end to
4434 * the previous block end if needed.
4436 if (((end
- len
) / blocksize
!=
4437 (end
- 1) / blocksize
))
4438 end
= end
- end
% blocksize
;
4440 if (end
> offset
+ len
) {
4441 memmove(bucket_buf
+ end
- len
,
4442 bucket_buf
+ offset
, len
);
4443 xe
->xe_name_offset
= cpu_to_le16(end
- len
);
4446 mlog_bug_on_msg(end
< offset
+ len
, "Defrag check failed for "
4447 "bucket %llu\n", (unsigned long long)blkno
);
4452 mlog_bug_on_msg(xh_free_start
> end
, "Defrag check failed for "
4453 "bucket %llu\n", (unsigned long long)blkno
);
4455 if (xh_free_start
== end
)
4458 memset(bucket_buf
+ xh_free_start
, 0, end
- xh_free_start
);
4459 xh
->xh_free_start
= cpu_to_le16(end
);
4461 /* sort the entries by their name_hash. */
4462 sort(entries
, le16_to_cpu(xh
->xh_count
),
4463 sizeof(struct ocfs2_xattr_entry
),
4467 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
4468 memcpy(bucket_block(bucket
, i
), buf
, blocksize
);
4469 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
4477 * prev_blkno points to the start of an existing extent. new_blkno
4478 * points to a newly allocated extent. Because we know each of our
4479 * clusters contains more than bucket, we can easily split one cluster
4480 * at a bucket boundary. So we take the last cluster of the existing
4481 * extent and split it down the middle. We move the last half of the
4482 * buckets in the last cluster of the existing extent over to the new
4485 * first_bh is the buffer at prev_blkno so we can update the existing
4486 * extent's bucket count. header_bh is the bucket were we were hoping
4487 * to insert our xattr. If the bucket move places the target in the new
4488 * extent, we'll update first_bh and header_bh after modifying the old
4491 * first_hash will be set as the 1st xe's name_hash in the new extent.
4493 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode
*inode
,
4495 struct ocfs2_xattr_bucket
*first
,
4496 struct ocfs2_xattr_bucket
*target
,
4502 struct super_block
*sb
= inode
->i_sb
;
4503 int blks_per_bucket
= ocfs2_blocks_per_xattr_bucket(sb
);
4504 int num_buckets
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb
));
4505 int to_move
= num_buckets
/ 2;
4507 u64 last_cluster_blkno
= bucket_blkno(first
) +
4508 ((num_clusters
- 1) * ocfs2_clusters_to_blocks(sb
, 1));
4510 BUG_ON(le16_to_cpu(bucket_xh(first
)->xh_num_buckets
) < num_buckets
);
4511 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
== OCFS2_SB(sb
)->s_clustersize
);
4513 trace_ocfs2_mv_xattr_bucket_cross_cluster(
4514 (unsigned long long)last_cluster_blkno
,
4515 (unsigned long long)new_blkno
);
4517 ret
= ocfs2_mv_xattr_buckets(inode
, handle
, bucket_blkno(first
),
4518 last_cluster_blkno
, new_blkno
,
4519 to_move
, first_hash
);
4525 /* This is the first bucket that got moved */
4526 src_blkno
= last_cluster_blkno
+ (to_move
* blks_per_bucket
);
4529 * If the target bucket was part of the moved buckets, we need to
4530 * update first and target.
4532 if (bucket_blkno(target
) >= src_blkno
) {
4533 /* Find the block for the new target bucket */
4534 src_blkno
= new_blkno
+
4535 (bucket_blkno(target
) - src_blkno
);
4537 ocfs2_xattr_bucket_relse(first
);
4538 ocfs2_xattr_bucket_relse(target
);
4541 * These shouldn't fail - the buffers are in the
4542 * journal from ocfs2_cp_xattr_bucket().
4544 ret
= ocfs2_read_xattr_bucket(first
, new_blkno
);
4549 ret
= ocfs2_read_xattr_bucket(target
, src_blkno
);
4560 * Find the suitable pos when we divide a bucket into 2.
4561 * We have to make sure the xattrs with the same hash value exist
4562 * in the same bucket.
4564 * If this ocfs2_xattr_header covers more than one hash value, find a
4565 * place where the hash value changes. Try to find the most even split.
4566 * The most common case is that all entries have different hash values,
4567 * and the first check we make will find a place to split.
4569 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header
*xh
)
4571 struct ocfs2_xattr_entry
*entries
= xh
->xh_entries
;
4572 int count
= le16_to_cpu(xh
->xh_count
);
4573 int delta
, middle
= count
/ 2;
4576 * We start at the middle. Each step gets farther away in both
4577 * directions. We therefore hit the change in hash value
4578 * nearest to the middle. Note that this loop does not execute for
4581 for (delta
= 0; delta
< middle
; delta
++) {
4582 /* Let's check delta earlier than middle */
4583 if (cmp_xe(&entries
[middle
- delta
- 1],
4584 &entries
[middle
- delta
]))
4585 return middle
- delta
;
4587 /* For even counts, don't walk off the end */
4588 if ((middle
+ delta
+ 1) == count
)
4591 /* Now try delta past middle */
4592 if (cmp_xe(&entries
[middle
+ delta
],
4593 &entries
[middle
+ delta
+ 1]))
4594 return middle
+ delta
+ 1;
4597 /* Every entry had the same hash */
4602 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4603 * first_hash will record the 1st hash of the new bucket.
4605 * Normally half of the xattrs will be moved. But we have to make
4606 * sure that the xattrs with the same hash value are stored in the
4607 * same bucket. If all the xattrs in this bucket have the same hash
4608 * value, the new bucket will be initialized as an empty one and the
4609 * first_hash will be initialized as (hash_value+1).
4611 static int ocfs2_divide_xattr_bucket(struct inode
*inode
,
4616 int new_bucket_head
)
4619 int count
, start
, len
, name_value_len
= 0, name_offset
= 0;
4620 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
4621 struct ocfs2_xattr_header
*xh
;
4622 struct ocfs2_xattr_entry
*xe
;
4623 int blocksize
= inode
->i_sb
->s_blocksize
;
4625 trace_ocfs2_divide_xattr_bucket_begin((unsigned long long)blk
,
4626 (unsigned long long)new_blk
);
4628 s_bucket
= ocfs2_xattr_bucket_new(inode
);
4629 t_bucket
= ocfs2_xattr_bucket_new(inode
);
4630 if (!s_bucket
|| !t_bucket
) {
4636 ret
= ocfs2_read_xattr_bucket(s_bucket
, blk
);
4642 ret
= ocfs2_xattr_bucket_journal_access(handle
, s_bucket
,
4643 OCFS2_JOURNAL_ACCESS_WRITE
);
4650 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4651 * there's no need to read it.
4653 ret
= ocfs2_init_xattr_bucket(t_bucket
, new_blk
, new_bucket_head
);
4660 * Hey, if we're overwriting t_bucket, what difference does
4661 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4662 * same part of ocfs2_cp_xattr_bucket().
4664 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
4666 OCFS2_JOURNAL_ACCESS_CREATE
:
4667 OCFS2_JOURNAL_ACCESS_WRITE
);
4673 xh
= bucket_xh(s_bucket
);
4674 count
= le16_to_cpu(xh
->xh_count
);
4675 start
= ocfs2_xattr_find_divide_pos(xh
);
4677 if (start
== count
) {
4678 xe
= &xh
->xh_entries
[start
-1];
4681 * initialized a new empty bucket here.
4682 * The hash value is set as one larger than
4683 * that of the last entry in the previous bucket.
4685 for (i
= 0; i
< t_bucket
->bu_blocks
; i
++)
4686 memset(bucket_block(t_bucket
, i
), 0, blocksize
);
4688 xh
= bucket_xh(t_bucket
);
4689 xh
->xh_free_start
= cpu_to_le16(blocksize
);
4690 xh
->xh_entries
[0].xe_name_hash
= xe
->xe_name_hash
;
4691 le32_add_cpu(&xh
->xh_entries
[0].xe_name_hash
, 1);
4693 goto set_num_buckets
;
4696 /* copy the whole bucket to the new first. */
4697 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
4699 /* update the new bucket. */
4700 xh
= bucket_xh(t_bucket
);
4703 * Calculate the total name/value len and xh_free_start for
4704 * the old bucket first.
4706 name_offset
= OCFS2_XATTR_BUCKET_SIZE
;
4708 for (i
= 0; i
< start
; i
++) {
4709 xe
= &xh
->xh_entries
[i
];
4710 name_value_len
+= namevalue_size_xe(xe
);
4711 if (le16_to_cpu(xe
->xe_name_offset
) < name_offset
)
4712 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
4716 * Now begin the modification to the new bucket.
4718 * In the new bucket, We just move the xattr entry to the beginning
4719 * and don't touch the name/value. So there will be some holes in the
4720 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4723 xe
= &xh
->xh_entries
[start
];
4724 len
= sizeof(struct ocfs2_xattr_entry
) * (count
- start
);
4725 trace_ocfs2_divide_xattr_bucket_move(len
,
4726 (int)((char *)xe
- (char *)xh
),
4727 (int)((char *)xh
->xh_entries
- (char *)xh
));
4728 memmove((char *)xh
->xh_entries
, (char *)xe
, len
);
4729 xe
= &xh
->xh_entries
[count
- start
];
4730 len
= sizeof(struct ocfs2_xattr_entry
) * start
;
4731 memset((char *)xe
, 0, len
);
4733 le16_add_cpu(&xh
->xh_count
, -start
);
4734 le16_add_cpu(&xh
->xh_name_value_len
, -name_value_len
);
4736 /* Calculate xh_free_start for the new bucket. */
4737 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
4738 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
4739 xe
= &xh
->xh_entries
[i
];
4740 if (le16_to_cpu(xe
->xe_name_offset
) <
4741 le16_to_cpu(xh
->xh_free_start
))
4742 xh
->xh_free_start
= xe
->xe_name_offset
;
4746 /* set xh->xh_num_buckets for the new xh. */
4747 if (new_bucket_head
)
4748 xh
->xh_num_buckets
= cpu_to_le16(1);
4750 xh
->xh_num_buckets
= 0;
4752 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
4754 /* store the first_hash of the new bucket. */
4756 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
4759 * Now only update the 1st block of the old bucket. If we
4760 * just added a new empty bucket, there is no need to modify
4766 xh
= bucket_xh(s_bucket
);
4767 memset(&xh
->xh_entries
[start
], 0,
4768 sizeof(struct ocfs2_xattr_entry
) * (count
- start
));
4769 xh
->xh_count
= cpu_to_le16(start
);
4770 xh
->xh_free_start
= cpu_to_le16(name_offset
);
4771 xh
->xh_name_value_len
= cpu_to_le16(name_value_len
);
4773 ocfs2_xattr_bucket_journal_dirty(handle
, s_bucket
);
4776 ocfs2_xattr_bucket_free(s_bucket
);
4777 ocfs2_xattr_bucket_free(t_bucket
);
4783 * Copy xattr from one bucket to another bucket.
4785 * The caller must make sure that the journal transaction
4786 * has enough space for journaling.
4788 static int ocfs2_cp_xattr_bucket(struct inode
*inode
,
4795 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
4797 BUG_ON(s_blkno
== t_blkno
);
4799 trace_ocfs2_cp_xattr_bucket((unsigned long long)s_blkno
,
4800 (unsigned long long)t_blkno
,
4803 s_bucket
= ocfs2_xattr_bucket_new(inode
);
4804 t_bucket
= ocfs2_xattr_bucket_new(inode
);
4805 if (!s_bucket
|| !t_bucket
) {
4811 ret
= ocfs2_read_xattr_bucket(s_bucket
, s_blkno
);
4816 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4817 * there's no need to read it.
4819 ret
= ocfs2_init_xattr_bucket(t_bucket
, t_blkno
, t_is_new
);
4824 * Hey, if we're overwriting t_bucket, what difference does
4825 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4826 * cluster to fill, we came here from
4827 * ocfs2_mv_xattr_buckets(), and it is really new -
4828 * ACCESS_CREATE is required. But we also might have moved data
4829 * out of t_bucket before extending back into it.
4830 * ocfs2_add_new_xattr_bucket() can do this - its call to
4831 * ocfs2_add_new_xattr_cluster() may have created a new extent
4832 * and copied out the end of the old extent. Then it re-extends
4833 * the old extent back to create space for new xattrs. That's
4834 * how we get here, and the bucket isn't really new.
4836 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
4838 OCFS2_JOURNAL_ACCESS_CREATE
:
4839 OCFS2_JOURNAL_ACCESS_WRITE
);
4843 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
4844 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
4847 ocfs2_xattr_bucket_free(t_bucket
);
4848 ocfs2_xattr_bucket_free(s_bucket
);
4854 * src_blk points to the start of an existing extent. last_blk points to
4855 * last cluster in that extent. to_blk points to a newly allocated
4856 * extent. We copy the buckets from the cluster at last_blk to the new
4857 * extent. If start_bucket is non-zero, we skip that many buckets before
4858 * we start copying. The new extent's xh_num_buckets gets set to the
4859 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4860 * by the same amount.
4862 static int ocfs2_mv_xattr_buckets(struct inode
*inode
, handle_t
*handle
,
4863 u64 src_blk
, u64 last_blk
, u64 to_blk
,
4864 unsigned int start_bucket
,
4867 int i
, ret
, credits
;
4868 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4869 int blks_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4870 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
4871 struct ocfs2_xattr_bucket
*old_first
, *new_first
;
4873 trace_ocfs2_mv_xattr_buckets((unsigned long long)last_blk
,
4874 (unsigned long long)to_blk
);
4876 BUG_ON(start_bucket
>= num_buckets
);
4878 num_buckets
-= start_bucket
;
4879 last_blk
+= (start_bucket
* blks_per_bucket
);
4882 /* The first bucket of the original extent */
4883 old_first
= ocfs2_xattr_bucket_new(inode
);
4884 /* The first bucket of the new extent */
4885 new_first
= ocfs2_xattr_bucket_new(inode
);
4886 if (!old_first
|| !new_first
) {
4892 ret
= ocfs2_read_xattr_bucket(old_first
, src_blk
);
4899 * We need to update the first bucket of the old extent and all
4900 * the buckets going to the new extent.
4902 credits
= ((num_buckets
+ 1) * blks_per_bucket
);
4903 ret
= ocfs2_extend_trans(handle
, credits
);
4909 ret
= ocfs2_xattr_bucket_journal_access(handle
, old_first
,
4910 OCFS2_JOURNAL_ACCESS_WRITE
);
4916 for (i
= 0; i
< num_buckets
; i
++) {
4917 ret
= ocfs2_cp_xattr_bucket(inode
, handle
,
4918 last_blk
+ (i
* blks_per_bucket
),
4919 to_blk
+ (i
* blks_per_bucket
),
4928 * Get the new bucket ready before we dirty anything
4929 * (This actually shouldn't fail, because we already dirtied
4930 * it once in ocfs2_cp_xattr_bucket()).
4932 ret
= ocfs2_read_xattr_bucket(new_first
, to_blk
);
4937 ret
= ocfs2_xattr_bucket_journal_access(handle
, new_first
,
4938 OCFS2_JOURNAL_ACCESS_WRITE
);
4944 /* Now update the headers */
4945 le16_add_cpu(&bucket_xh(old_first
)->xh_num_buckets
, -num_buckets
);
4946 ocfs2_xattr_bucket_journal_dirty(handle
, old_first
);
4948 bucket_xh(new_first
)->xh_num_buckets
= cpu_to_le16(num_buckets
);
4949 ocfs2_xattr_bucket_journal_dirty(handle
, new_first
);
4952 *first_hash
= le32_to_cpu(bucket_xh(new_first
)->xh_entries
[0].xe_name_hash
);
4955 ocfs2_xattr_bucket_free(new_first
);
4956 ocfs2_xattr_bucket_free(old_first
);
4961 * Move some xattrs in this cluster to the new cluster.
4962 * This function should only be called when bucket size == cluster size.
4963 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4965 static int ocfs2_divide_xattr_cluster(struct inode
*inode
,
4971 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4972 int ret
, credits
= 2 * blk_per_bucket
;
4974 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
< OCFS2_SB(inode
->i_sb
)->s_clustersize
);
4976 ret
= ocfs2_extend_trans(handle
, credits
);
4982 /* Move half of the xattr in start_blk to the next bucket. */
4983 return ocfs2_divide_xattr_bucket(inode
, handle
, prev_blk
,
4984 new_blk
, first_hash
, 1);
4988 * Move some xattrs from the old cluster to the new one since they are not
4989 * contiguous in ocfs2 xattr tree.
4991 * new_blk starts a new separate cluster, and we will move some xattrs from
4992 * prev_blk to it. v_start will be set as the first name hash value in this
4993 * new cluster so that it can be used as e_cpos during tree insertion and
4994 * don't collide with our original b-tree operations. first_bh and header_bh
4995 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4996 * to extend the insert bucket.
4998 * The problem is how much xattr should we move to the new one and when should
4999 * we update first_bh and header_bh?
5000 * 1. If cluster size > bucket size, that means the previous cluster has more
5001 * than 1 bucket, so just move half nums of bucket into the new cluster and
5002 * update the first_bh and header_bh if the insert bucket has been moved
5003 * to the new cluster.
5004 * 2. If cluster_size == bucket_size:
5005 * a) If the previous extent rec has more than one cluster and the insert
5006 * place isn't in the last cluster, copy the entire last cluster to the
5007 * new one. This time, we don't need to upate the first_bh and header_bh
5008 * since they will not be moved into the new cluster.
5009 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
5010 * the new one. And we set the extend flag to zero if the insert place is
5011 * moved into the new allocated cluster since no extend is needed.
5013 static int ocfs2_adjust_xattr_cross_cluster(struct inode
*inode
,
5015 struct ocfs2_xattr_bucket
*first
,
5016 struct ocfs2_xattr_bucket
*target
,
5024 trace_ocfs2_adjust_xattr_cross_cluster(
5025 (unsigned long long)bucket_blkno(first
),
5026 (unsigned long long)new_blk
, prev_clusters
);
5028 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
)) > 1) {
5029 ret
= ocfs2_mv_xattr_bucket_cross_cluster(inode
,
5038 /* The start of the last cluster in the first extent */
5039 u64 last_blk
= bucket_blkno(first
) +
5040 ((prev_clusters
- 1) *
5041 ocfs2_clusters_to_blocks(inode
->i_sb
, 1));
5043 if (prev_clusters
> 1 && bucket_blkno(target
) != last_blk
) {
5044 ret
= ocfs2_mv_xattr_buckets(inode
, handle
,
5045 bucket_blkno(first
),
5046 last_blk
, new_blk
, 0,
5051 ret
= ocfs2_divide_xattr_cluster(inode
, handle
,
5057 if ((bucket_blkno(target
) == last_blk
) && extend
)
5066 * Add a new cluster for xattr storage.
5068 * If the new cluster is contiguous with the previous one, it will be
5069 * appended to the same extent record, and num_clusters will be updated.
5070 * If not, we will insert a new extent for it and move some xattrs in
5071 * the last cluster into the new allocated one.
5072 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5073 * lose the benefits of hashing because we'll have to search large leaves.
5074 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5077 * first_bh is the first block of the previous extent rec and header_bh
5078 * indicates the bucket we will insert the new xattrs. They will be updated
5079 * when the header_bh is moved into the new cluster.
5081 static int ocfs2_add_new_xattr_cluster(struct inode
*inode
,
5082 struct buffer_head
*root_bh
,
5083 struct ocfs2_xattr_bucket
*first
,
5084 struct ocfs2_xattr_bucket
*target
,
5088 struct ocfs2_xattr_set_ctxt
*ctxt
)
5091 u16 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
5092 u32 prev_clusters
= *num_clusters
;
5093 u32 clusters_to_add
= 1, bit_off
, num_bits
, v_start
= 0;
5095 handle_t
*handle
= ctxt
->handle
;
5096 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5097 struct ocfs2_extent_tree et
;
5099 trace_ocfs2_add_new_xattr_cluster_begin(
5100 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5101 (unsigned long long)bucket_blkno(first
),
5102 prev_cpos
, prev_clusters
);
5104 ocfs2_init_xattr_tree_extent_tree(&et
, INODE_CACHE(inode
), root_bh
);
5106 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), root_bh
,
5107 OCFS2_JOURNAL_ACCESS_WRITE
);
5113 ret
= __ocfs2_claim_clusters(handle
, ctxt
->data_ac
, 1,
5114 clusters_to_add
, &bit_off
, &num_bits
);
5121 BUG_ON(num_bits
> clusters_to_add
);
5123 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
5124 trace_ocfs2_add_new_xattr_cluster((unsigned long long)block
, num_bits
);
5126 if (bucket_blkno(first
) + (prev_clusters
* bpc
) == block
&&
5127 (prev_clusters
+ num_bits
) << osb
->s_clustersize_bits
<=
5128 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
) {
5130 * If this cluster is contiguous with the old one and
5131 * adding this new cluster, we don't surpass the limit of
5132 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5133 * initialized and used like other buckets in the previous
5135 * So add it as a contiguous one. The caller will handle
5138 v_start
= prev_cpos
+ prev_clusters
;
5139 *num_clusters
= prev_clusters
+ num_bits
;
5141 ret
= ocfs2_adjust_xattr_cross_cluster(inode
,
5155 trace_ocfs2_add_new_xattr_cluster_insert((unsigned long long)block
,
5157 ret
= ocfs2_insert_extent(handle
, &et
, v_start
, block
,
5158 num_bits
, 0, ctxt
->meta_ac
);
5164 ocfs2_journal_dirty(handle
, root_bh
);
5171 * We are given an extent. 'first' is the bucket at the very front of
5172 * the extent. The extent has space for an additional bucket past
5173 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5174 * of the target bucket. We wish to shift every bucket past the target
5175 * down one, filling in that additional space. When we get back to the
5176 * target, we split the target between itself and the now-empty bucket
5177 * at target+1 (aka, target_blkno + blks_per_bucket).
5179 static int ocfs2_extend_xattr_bucket(struct inode
*inode
,
5181 struct ocfs2_xattr_bucket
*first
,
5186 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5187 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
5189 u16 new_bucket
= le16_to_cpu(bucket_xh(first
)->xh_num_buckets
);
5191 trace_ocfs2_extend_xattr_bucket((unsigned long long)target_blk
,
5192 (unsigned long long)bucket_blkno(first
),
5193 num_clusters
, new_bucket
);
5195 /* The extent must have room for an additional bucket */
5196 BUG_ON(new_bucket
>=
5197 (num_clusters
* ocfs2_xattr_buckets_per_cluster(osb
)));
5199 /* end_blk points to the last existing bucket */
5200 end_blk
= bucket_blkno(first
) + ((new_bucket
- 1) * blk_per_bucket
);
5203 * end_blk is the start of the last existing bucket.
5204 * Thus, (end_blk - target_blk) covers the target bucket and
5205 * every bucket after it up to, but not including, the last
5206 * existing bucket. Then we add the last existing bucket, the
5207 * new bucket, and the first bucket (3 * blk_per_bucket).
5209 credits
= (end_blk
- target_blk
) + (3 * blk_per_bucket
);
5210 ret
= ocfs2_extend_trans(handle
, credits
);
5216 ret
= ocfs2_xattr_bucket_journal_access(handle
, first
,
5217 OCFS2_JOURNAL_ACCESS_WRITE
);
5223 while (end_blk
!= target_blk
) {
5224 ret
= ocfs2_cp_xattr_bucket(inode
, handle
, end_blk
,
5225 end_blk
+ blk_per_bucket
, 0);
5228 end_blk
-= blk_per_bucket
;
5231 /* Move half of the xattr in target_blkno to the next bucket. */
5232 ret
= ocfs2_divide_xattr_bucket(inode
, handle
, target_blk
,
5233 target_blk
+ blk_per_bucket
, NULL
, 0);
5235 le16_add_cpu(&bucket_xh(first
)->xh_num_buckets
, 1);
5236 ocfs2_xattr_bucket_journal_dirty(handle
, first
);
5243 * Add new xattr bucket in an extent record and adjust the buckets
5244 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5245 * bucket we want to insert into.
5247 * In the easy case, we will move all the buckets after target down by
5248 * one. Half of target's xattrs will be moved to the next bucket.
5250 * If current cluster is full, we'll allocate a new one. This may not
5251 * be contiguous. The underlying calls will make sure that there is
5252 * space for the insert, shifting buckets around if necessary.
5253 * 'target' may be moved by those calls.
5255 static int ocfs2_add_new_xattr_bucket(struct inode
*inode
,
5256 struct buffer_head
*xb_bh
,
5257 struct ocfs2_xattr_bucket
*target
,
5258 struct ocfs2_xattr_set_ctxt
*ctxt
)
5260 struct ocfs2_xattr_block
*xb
=
5261 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
5262 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
5263 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
5265 le32_to_cpu(bucket_xh(target
)->xh_entries
[0].xe_name_hash
);
5266 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5267 int ret
, num_buckets
, extend
= 1;
5269 u32 e_cpos
, num_clusters
;
5270 /* The bucket at the front of the extent */
5271 struct ocfs2_xattr_bucket
*first
;
5273 trace_ocfs2_add_new_xattr_bucket(
5274 (unsigned long long)bucket_blkno(target
));
5276 /* The first bucket of the original extent */
5277 first
= ocfs2_xattr_bucket_new(inode
);
5284 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &e_cpos
,
5291 ret
= ocfs2_read_xattr_bucket(first
, p_blkno
);
5297 num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
) * num_clusters
;
5298 if (num_buckets
== le16_to_cpu(bucket_xh(first
)->xh_num_buckets
)) {
5300 * This can move first+target if the target bucket moves
5301 * to the new extent.
5303 ret
= ocfs2_add_new_xattr_cluster(inode
,
5318 ret
= ocfs2_extend_xattr_bucket(inode
,
5321 bucket_blkno(target
),
5328 ocfs2_xattr_bucket_free(first
);
5334 * Truncate the specified xe_off entry in xattr bucket.
5335 * bucket is indicated by header_bh and len is the new length.
5336 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5338 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5340 static int ocfs2_xattr_bucket_value_truncate(struct inode
*inode
,
5341 struct ocfs2_xattr_bucket
*bucket
,
5344 struct ocfs2_xattr_set_ctxt
*ctxt
)
5348 struct ocfs2_xattr_entry
*xe
;
5349 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5350 size_t blocksize
= inode
->i_sb
->s_blocksize
;
5351 struct ocfs2_xattr_value_buf vb
= {
5352 .vb_access
= ocfs2_journal_access
,
5355 xe
= &xh
->xh_entries
[xe_off
];
5357 BUG_ON(!xe
|| ocfs2_xattr_is_local(xe
));
5359 offset
= le16_to_cpu(xe
->xe_name_offset
) +
5360 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5362 value_blk
= offset
/ blocksize
;
5364 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5365 BUG_ON(value_blk
!= (offset
+ OCFS2_XATTR_ROOT_SIZE
- 1) / blocksize
);
5367 vb
.vb_bh
= bucket
->bu_bhs
[value_blk
];
5370 vb
.vb_xv
= (struct ocfs2_xattr_value_root
*)
5371 (vb
.vb_bh
->b_data
+ offset
% blocksize
);
5374 * From here on out we have to dirty the bucket. The generic
5375 * value calls only modify one of the bucket's bhs, but we need
5376 * to send the bucket at once. So if they error, they *could* have
5377 * modified something. We have to assume they did, and dirty
5378 * the whole bucket. This leaves us in a consistent state.
5380 trace_ocfs2_xattr_bucket_value_truncate(
5381 (unsigned long long)bucket_blkno(bucket
), xe_off
, len
);
5382 ret
= ocfs2_xattr_value_truncate(inode
, &vb
, len
, ctxt
);
5388 ret
= ocfs2_xattr_bucket_journal_access(ctxt
->handle
, bucket
,
5389 OCFS2_JOURNAL_ACCESS_WRITE
);
5395 xe
->xe_value_size
= cpu_to_le64(len
);
5397 ocfs2_xattr_bucket_journal_dirty(ctxt
->handle
, bucket
);
5403 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
5404 struct buffer_head
*root_bh
,
5411 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5412 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5414 struct ocfs2_xattr_block
*xb
=
5415 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
5416 struct ocfs2_alloc_context
*meta_ac
= NULL
;
5417 struct ocfs2_cached_dealloc_ctxt dealloc
;
5418 struct ocfs2_extent_tree et
;
5420 ret
= ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
5421 ocfs2_delete_xattr_in_bucket
, para
);
5427 ocfs2_init_xattr_tree_extent_tree(&et
, INODE_CACHE(inode
), root_bh
);
5429 ocfs2_init_dealloc_ctxt(&dealloc
);
5431 trace_ocfs2_rm_xattr_cluster(
5432 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5433 (unsigned long long)blkno
, cpos
, len
);
5435 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode
), blkno
,
5438 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
5444 mutex_lock(&tl_inode
->i_mutex
);
5446 if (ocfs2_truncate_log_needs_flush(osb
)) {
5447 ret
= __ocfs2_flush_truncate_log(osb
);
5454 handle
= ocfs2_start_trans(osb
, ocfs2_remove_extent_credits(osb
->sb
));
5455 if (IS_ERR(handle
)) {
5461 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(inode
), root_bh
,
5462 OCFS2_JOURNAL_ACCESS_WRITE
);
5468 ret
= ocfs2_remove_extent(handle
, &et
, cpos
, len
, meta_ac
,
5475 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, -len
);
5476 ocfs2_journal_dirty(handle
, root_bh
);
5478 ret
= ocfs2_truncate_log_append(osb
, handle
, blkno
, len
);
5481 ocfs2_update_inode_fsync_trans(handle
, inode
, 0);
5484 ocfs2_commit_trans(osb
, handle
);
5486 ocfs2_schedule_truncate_log_flush(osb
, 1);
5488 mutex_unlock(&tl_inode
->i_mutex
);
5491 ocfs2_free_alloc_context(meta_ac
);
5493 ocfs2_run_deallocs(osb
, &dealloc
);
5499 * check whether the xattr bucket is filled up with the same hash value.
5500 * If we want to insert the xattr with the same hash, return -ENOSPC.
5501 * If we want to insert a xattr with different hash value, go ahead
5502 * and ocfs2_divide_xattr_bucket will handle this.
5504 static int ocfs2_check_xattr_bucket_collision(struct inode
*inode
,
5505 struct ocfs2_xattr_bucket
*bucket
,
5508 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5509 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
5511 if (name_hash
!= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
))
5514 if (xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1].xe_name_hash
==
5515 xh
->xh_entries
[0].xe_name_hash
) {
5516 mlog(ML_ERROR
, "Too much hash collision in xattr bucket %llu, "
5518 (unsigned long long)bucket_blkno(bucket
),
5519 le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
));
5527 * Try to set the entry in the current bucket. If we fail, the caller
5528 * will handle getting us another bucket.
5530 static int ocfs2_xattr_set_entry_bucket(struct inode
*inode
,
5531 struct ocfs2_xattr_info
*xi
,
5532 struct ocfs2_xattr_search
*xs
,
5533 struct ocfs2_xattr_set_ctxt
*ctxt
)
5536 struct ocfs2_xa_loc loc
;
5538 trace_ocfs2_xattr_set_entry_bucket(xi
->xi_name
);
5540 ocfs2_init_xattr_bucket_xa_loc(&loc
, xs
->bucket
,
5541 xs
->not_found
? NULL
: xs
->here
);
5542 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
5544 xs
->here
= loc
.xl_entry
;
5547 if (ret
!= -ENOSPC
) {
5552 /* Ok, we need space. Let's try defragmenting the bucket. */
5553 ret
= ocfs2_defrag_xattr_bucket(inode
, ctxt
->handle
,
5560 ret
= ocfs2_xa_set(&loc
, xi
, ctxt
);
5562 xs
->here
= loc
.xl_entry
;
5573 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
5574 struct ocfs2_xattr_info
*xi
,
5575 struct ocfs2_xattr_search
*xs
,
5576 struct ocfs2_xattr_set_ctxt
*ctxt
)
5580 trace_ocfs2_xattr_set_entry_index_block(xi
->xi_name
);
5582 ret
= ocfs2_xattr_set_entry_bucket(inode
, xi
, xs
, ctxt
);
5585 if (ret
!= -ENOSPC
) {
5590 /* Ack, need more space. Let's try to get another bucket! */
5593 * We do not allow for overlapping ranges between buckets. And
5594 * the maximum number of collisions we will allow for then is
5595 * one bucket's worth, so check it here whether we need to
5596 * add a new bucket for the insert.
5598 ret
= ocfs2_check_xattr_bucket_collision(inode
,
5606 ret
= ocfs2_add_new_xattr_bucket(inode
,
5616 * ocfs2_add_new_xattr_bucket() will have updated
5617 * xs->bucket if it moved, but it will not have updated
5618 * any of the other search fields. Thus, we drop it and
5619 * re-search. Everything should be cached, so it'll be
5622 ocfs2_xattr_bucket_relse(xs
->bucket
);
5623 ret
= ocfs2_xattr_index_block_find(inode
, xs
->xattr_bh
,
5626 if (ret
&& ret
!= -ENODATA
)
5628 xs
->not_found
= ret
;
5630 /* Ok, we have a new bucket, let's try again */
5631 ret
= ocfs2_xattr_set_entry_bucket(inode
, xi
, xs
, ctxt
);
5632 if (ret
&& (ret
!= -ENOSPC
))
5639 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
5640 struct ocfs2_xattr_bucket
*bucket
,
5643 int ret
= 0, ref_credits
;
5644 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5646 struct ocfs2_xattr_entry
*xe
;
5647 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5648 struct ocfs2_xattr_set_ctxt ctxt
= {NULL
, NULL
,};
5649 int credits
= ocfs2_remove_extent_credits(osb
->sb
) +
5650 ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
5651 struct ocfs2_xattr_value_root
*xv
;
5652 struct ocfs2_rm_xattr_bucket_para
*args
=
5653 (struct ocfs2_rm_xattr_bucket_para
*)para
;
5655 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
5657 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
5658 xe
= &xh
->xh_entries
[i
];
5659 if (ocfs2_xattr_is_local(xe
))
5662 ret
= ocfs2_get_xattr_tree_value_root(inode
->i_sb
, bucket
,
5669 ret
= ocfs2_lock_xattr_remove_allocators(inode
, xv
,
5675 ctxt
.handle
= ocfs2_start_trans(osb
, credits
+ ref_credits
);
5676 if (IS_ERR(ctxt
.handle
)) {
5677 ret
= PTR_ERR(ctxt
.handle
);
5682 ret
= ocfs2_xattr_bucket_value_truncate(inode
, bucket
,
5685 ocfs2_commit_trans(osb
, ctxt
.handle
);
5687 ocfs2_free_alloc_context(ctxt
.meta_ac
);
5688 ctxt
.meta_ac
= NULL
;
5697 ocfs2_free_alloc_context(ctxt
.meta_ac
);
5698 ocfs2_schedule_truncate_log_flush(osb
, 1);
5699 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
5704 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5705 * or change the extent record flag), we need to recalculate
5706 * the metaecc for the whole bucket. So it is done here.
5709 * We have to give the extra credits for the caller.
5711 static int ocfs2_xattr_bucket_post_refcount(struct inode
*inode
,
5716 struct ocfs2_xattr_bucket
*bucket
=
5717 (struct ocfs2_xattr_bucket
*)para
;
5719 ret
= ocfs2_xattr_bucket_journal_access(handle
, bucket
,
5720 OCFS2_JOURNAL_ACCESS_WRITE
);
5726 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
5732 * Special action we need if the xattr value is refcounted.
5734 * 1. If the xattr is refcounted, lock the tree.
5735 * 2. CoW the xattr if we are setting the new value and the value
5736 * will be stored outside.
5737 * 3. In other case, decrease_refcount will work for us, so just
5738 * lock the refcount tree, calculate the meta and credits is OK.
5740 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5741 * currently CoW is a completed transaction, while this function
5742 * will also lock the allocators and let us deadlock. So we will
5743 * CoW the whole xattr value.
5745 static int ocfs2_prepare_refcount_xattr(struct inode
*inode
,
5746 struct ocfs2_dinode
*di
,
5747 struct ocfs2_xattr_info
*xi
,
5748 struct ocfs2_xattr_search
*xis
,
5749 struct ocfs2_xattr_search
*xbs
,
5750 struct ocfs2_refcount_tree
**ref_tree
,
5755 struct ocfs2_xattr_block
*xb
;
5756 struct ocfs2_xattr_entry
*xe
;
5758 u32 p_cluster
, num_clusters
;
5759 unsigned int ext_flags
;
5760 int name_offset
, name_len
;
5761 struct ocfs2_xattr_value_buf vb
;
5762 struct ocfs2_xattr_bucket
*bucket
= NULL
;
5763 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5764 struct ocfs2_post_refcount refcount
;
5765 struct ocfs2_post_refcount
*p
= NULL
;
5766 struct buffer_head
*ref_root_bh
= NULL
;
5768 if (!xis
->not_found
) {
5770 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
5771 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5773 vb
.vb_bh
= xis
->inode_bh
;
5774 vb
.vb_access
= ocfs2_journal_access_di
;
5776 int i
, block_off
= 0;
5777 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
5779 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
5780 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
5781 i
= xbs
->here
- xbs
->header
->xh_entries
;
5783 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
5784 ret
= ocfs2_xattr_bucket_get_name_value(inode
->i_sb
,
5785 bucket_xh(xbs
->bucket
),
5792 base
= bucket_block(xbs
->bucket
, block_off
);
5793 vb
.vb_bh
= xbs
->bucket
->bu_bhs
[block_off
];
5794 vb
.vb_access
= ocfs2_journal_access
;
5796 if (ocfs2_meta_ecc(osb
)) {
5797 /*create parameters for ocfs2_post_refcount. */
5798 bucket
= xbs
->bucket
;
5799 refcount
.credits
= bucket
->bu_blocks
;
5800 refcount
.para
= bucket
;
5802 ocfs2_xattr_bucket_post_refcount
;
5807 vb
.vb_bh
= xbs
->xattr_bh
;
5808 vb
.vb_access
= ocfs2_journal_access_xb
;
5812 if (ocfs2_xattr_is_local(xe
))
5815 vb
.vb_xv
= (struct ocfs2_xattr_value_root
*)
5816 (base
+ name_offset
+ name_len
);
5818 ret
= ocfs2_xattr_get_clusters(inode
, 0, &p_cluster
,
5819 &num_clusters
, &vb
.vb_xv
->xr_list
,
5827 * We just need to check the 1st extent record, since we always
5828 * CoW the whole xattr. So there shouldn't be a xattr with
5829 * some REFCOUNT extent recs after the 1st one.
5831 if (!(ext_flags
& OCFS2_EXT_REFCOUNTED
))
5834 ret
= ocfs2_lock_refcount_tree(osb
, le64_to_cpu(di
->i_refcount_loc
),
5835 1, ref_tree
, &ref_root_bh
);
5842 * If we are deleting the xattr or the new size will be stored inside,
5843 * cool, leave it there, the xattr truncate process will remove them
5844 * for us(it still needs the refcount tree lock and the meta, credits).
5845 * And the worse case is that every cluster truncate will split the
5846 * refcount tree, and make the original extent become 3. So we will need
5847 * 2 * cluster more extent recs at most.
5849 if (!xi
->xi_value
|| xi
->xi_value_len
<= OCFS2_XATTR_INLINE_SIZE
) {
5851 ret
= ocfs2_refcounted_xattr_delete_need(inode
,
5852 &(*ref_tree
)->rf_ci
,
5853 ref_root_bh
, vb
.vb_xv
,
5860 ret
= ocfs2_refcount_cow_xattr(inode
, di
, &vb
,
5861 *ref_tree
, ref_root_bh
, 0,
5862 le32_to_cpu(vb
.vb_xv
->xr_clusters
), p
);
5867 brelse(ref_root_bh
);
5872 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5873 * The physical clusters will be added to refcount tree.
5875 static int ocfs2_xattr_value_attach_refcount(struct inode
*inode
,
5876 struct ocfs2_xattr_value_root
*xv
,
5877 struct ocfs2_extent_tree
*value_et
,
5878 struct ocfs2_caching_info
*ref_ci
,
5879 struct buffer_head
*ref_root_bh
,
5880 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5881 struct ocfs2_post_refcount
*refcount
)
5884 u32 clusters
= le32_to_cpu(xv
->xr_clusters
);
5885 u32 cpos
, p_cluster
, num_clusters
;
5886 struct ocfs2_extent_list
*el
= &xv
->xr_list
;
5887 unsigned int ext_flags
;
5890 while (cpos
< clusters
) {
5891 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
5892 &num_clusters
, el
, &ext_flags
);
5898 cpos
+= num_clusters
;
5899 if ((ext_flags
& OCFS2_EXT_REFCOUNTED
))
5904 ret
= ocfs2_add_refcount_flag(inode
, value_et
,
5905 ref_ci
, ref_root_bh
,
5906 cpos
- num_clusters
,
5907 p_cluster
, num_clusters
,
5919 * Given a normal ocfs2_xattr_header, refcount all the entries which
5920 * have value stored outside.
5921 * Used for xattrs stored in inode and ocfs2_xattr_block.
5923 static int ocfs2_xattr_attach_refcount_normal(struct inode
*inode
,
5924 struct ocfs2_xattr_value_buf
*vb
,
5925 struct ocfs2_xattr_header
*header
,
5926 struct ocfs2_caching_info
*ref_ci
,
5927 struct buffer_head
*ref_root_bh
,
5928 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5931 struct ocfs2_xattr_entry
*xe
;
5932 struct ocfs2_xattr_value_root
*xv
;
5933 struct ocfs2_extent_tree et
;
5936 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
5937 xe
= &header
->xh_entries
[i
];
5939 if (ocfs2_xattr_is_local(xe
))
5942 xv
= (struct ocfs2_xattr_value_root
*)((void *)header
+
5943 le16_to_cpu(xe
->xe_name_offset
) +
5944 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
5947 ocfs2_init_xattr_value_extent_tree(&et
, INODE_CACHE(inode
), vb
);
5949 ret
= ocfs2_xattr_value_attach_refcount(inode
, xv
, &et
,
5950 ref_ci
, ref_root_bh
,
5961 static int ocfs2_xattr_inline_attach_refcount(struct inode
*inode
,
5962 struct buffer_head
*fe_bh
,
5963 struct ocfs2_caching_info
*ref_ci
,
5964 struct buffer_head
*ref_root_bh
,
5965 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5967 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
5968 struct ocfs2_xattr_header
*header
= (struct ocfs2_xattr_header
*)
5969 (fe_bh
->b_data
+ inode
->i_sb
->s_blocksize
-
5970 le16_to_cpu(di
->i_xattr_inline_size
));
5971 struct ocfs2_xattr_value_buf vb
= {
5973 .vb_access
= ocfs2_journal_access_di
,
5976 return ocfs2_xattr_attach_refcount_normal(inode
, &vb
, header
,
5977 ref_ci
, ref_root_bh
, dealloc
);
5980 struct ocfs2_xattr_tree_value_refcount_para
{
5981 struct ocfs2_caching_info
*ref_ci
;
5982 struct buffer_head
*ref_root_bh
;
5983 struct ocfs2_cached_dealloc_ctxt
*dealloc
;
5986 static int ocfs2_get_xattr_tree_value_root(struct super_block
*sb
,
5987 struct ocfs2_xattr_bucket
*bucket
,
5989 struct ocfs2_xattr_value_root
**xv
,
5990 struct buffer_head
**bh
)
5992 int ret
, block_off
, name_offset
;
5993 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5994 struct ocfs2_xattr_entry
*xe
= &xh
->xh_entries
[offset
];
5997 ret
= ocfs2_xattr_bucket_get_name_value(sb
,
6007 base
= bucket_block(bucket
, block_off
);
6009 *xv
= (struct ocfs2_xattr_value_root
*)(base
+ name_offset
+
6010 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
6013 *bh
= bucket
->bu_bhs
[block_off
];
6019 * For a given xattr bucket, refcount all the entries which
6020 * have value stored outside.
6022 static int ocfs2_xattr_bucket_value_refcount(struct inode
*inode
,
6023 struct ocfs2_xattr_bucket
*bucket
,
6027 struct ocfs2_extent_tree et
;
6028 struct ocfs2_xattr_tree_value_refcount_para
*ref
=
6029 (struct ocfs2_xattr_tree_value_refcount_para
*)para
;
6030 struct ocfs2_xattr_header
*xh
=
6031 (struct ocfs2_xattr_header
*)bucket
->bu_bhs
[0]->b_data
;
6032 struct ocfs2_xattr_entry
*xe
;
6033 struct ocfs2_xattr_value_buf vb
= {
6034 .vb_access
= ocfs2_journal_access
,
6036 struct ocfs2_post_refcount refcount
= {
6037 .credits
= bucket
->bu_blocks
,
6039 .func
= ocfs2_xattr_bucket_post_refcount
,
6041 struct ocfs2_post_refcount
*p
= NULL
;
6043 /* We only need post_refcount if we support metaecc. */
6044 if (ocfs2_meta_ecc(OCFS2_SB(inode
->i_sb
)))
6047 trace_ocfs2_xattr_bucket_value_refcount(
6048 (unsigned long long)bucket_blkno(bucket
),
6049 le16_to_cpu(xh
->xh_count
));
6050 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
6051 xe
= &xh
->xh_entries
[i
];
6053 if (ocfs2_xattr_is_local(xe
))
6056 ret
= ocfs2_get_xattr_tree_value_root(inode
->i_sb
, bucket
, i
,
6057 &vb
.vb_xv
, &vb
.vb_bh
);
6063 ocfs2_init_xattr_value_extent_tree(&et
,
6064 INODE_CACHE(inode
), &vb
);
6066 ret
= ocfs2_xattr_value_attach_refcount(inode
, vb
.vb_xv
,
6080 static int ocfs2_refcount_xattr_tree_rec(struct inode
*inode
,
6081 struct buffer_head
*root_bh
,
6082 u64 blkno
, u32 cpos
, u32 len
, void *para
)
6084 return ocfs2_iterate_xattr_buckets(inode
, blkno
, len
,
6085 ocfs2_xattr_bucket_value_refcount
,
6089 static int ocfs2_xattr_block_attach_refcount(struct inode
*inode
,
6090 struct buffer_head
*blk_bh
,
6091 struct ocfs2_caching_info
*ref_ci
,
6092 struct buffer_head
*ref_root_bh
,
6093 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
6096 struct ocfs2_xattr_block
*xb
=
6097 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
6099 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
6100 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
6101 struct ocfs2_xattr_value_buf vb
= {
6103 .vb_access
= ocfs2_journal_access_xb
,
6106 ret
= ocfs2_xattr_attach_refcount_normal(inode
, &vb
, header
,
6107 ref_ci
, ref_root_bh
,
6110 struct ocfs2_xattr_tree_value_refcount_para para
= {
6112 .ref_root_bh
= ref_root_bh
,
6116 ret
= ocfs2_iterate_xattr_index_block(inode
, blk_bh
,
6117 ocfs2_refcount_xattr_tree_rec
,
6124 int ocfs2_xattr_attach_refcount_tree(struct inode
*inode
,
6125 struct buffer_head
*fe_bh
,
6126 struct ocfs2_caching_info
*ref_ci
,
6127 struct buffer_head
*ref_root_bh
,
6128 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
6131 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6132 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
6133 struct buffer_head
*blk_bh
= NULL
;
6135 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
6136 ret
= ocfs2_xattr_inline_attach_refcount(inode
, fe_bh
,
6137 ref_ci
, ref_root_bh
,
6145 if (!di
->i_xattr_loc
)
6148 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
6155 ret
= ocfs2_xattr_block_attach_refcount(inode
, blk_bh
, ref_ci
,
6156 ref_root_bh
, dealloc
);
6166 typedef int (should_xattr_reflinked
)(struct ocfs2_xattr_entry
*xe
);
6168 * Store the information we need in xattr reflink.
6169 * old_bh and new_bh are inode bh for the old and new inode.
6171 struct ocfs2_xattr_reflink
{
6172 struct inode
*old_inode
;
6173 struct inode
*new_inode
;
6174 struct buffer_head
*old_bh
;
6175 struct buffer_head
*new_bh
;
6176 struct ocfs2_caching_info
*ref_ci
;
6177 struct buffer_head
*ref_root_bh
;
6178 struct ocfs2_cached_dealloc_ctxt
*dealloc
;
6179 should_xattr_reflinked
*xattr_reflinked
;
6183 * Given a xattr header and xe offset,
6184 * return the proper xv and the corresponding bh.
6185 * xattr in inode, block and xattr tree have different implementaions.
6187 typedef int (get_xattr_value_root
)(struct super_block
*sb
,
6188 struct buffer_head
*bh
,
6189 struct ocfs2_xattr_header
*xh
,
6191 struct ocfs2_xattr_value_root
**xv
,
6192 struct buffer_head
**ret_bh
,
6196 * Calculate all the xattr value root metadata stored in this xattr header and
6197 * credits we need if we create them from the scratch.
6198 * We use get_xattr_value_root so that all types of xattr container can use it.
6200 static int ocfs2_value_metas_in_xattr_header(struct super_block
*sb
,
6201 struct buffer_head
*bh
,
6202 struct ocfs2_xattr_header
*xh
,
6203 int *metas
, int *credits
,
6205 get_xattr_value_root
*func
,
6209 struct ocfs2_xattr_value_root
*xv
;
6210 struct ocfs2_xattr_entry
*xe
;
6212 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
6213 xe
= &xh
->xh_entries
[i
];
6214 if (ocfs2_xattr_is_local(xe
))
6217 ret
= func(sb
, bh
, xh
, i
, &xv
, NULL
, para
);
6223 *metas
+= le16_to_cpu(xv
->xr_list
.l_tree_depth
) *
6224 le16_to_cpu(xv
->xr_list
.l_next_free_rec
);
6226 *credits
+= ocfs2_calc_extend_credits(sb
,
6227 &def_xv
.xv
.xr_list
);
6230 * If the value is a tree with depth > 1, We don't go deep
6231 * to the extent block, so just calculate a maximum record num.
6233 if (!xv
->xr_list
.l_tree_depth
)
6234 *num_recs
+= le16_to_cpu(xv
->xr_list
.l_next_free_rec
);
6236 *num_recs
+= ocfs2_clusters_for_bytes(sb
,
6243 /* Used by xattr inode and block to return the right xv and buffer_head. */
6244 static int ocfs2_get_xattr_value_root(struct super_block
*sb
,
6245 struct buffer_head
*bh
,
6246 struct ocfs2_xattr_header
*xh
,
6248 struct ocfs2_xattr_value_root
**xv
,
6249 struct buffer_head
**ret_bh
,
6252 struct ocfs2_xattr_entry
*xe
= &xh
->xh_entries
[offset
];
6254 *xv
= (struct ocfs2_xattr_value_root
*)((void *)xh
+
6255 le16_to_cpu(xe
->xe_name_offset
) +
6256 OCFS2_XATTR_SIZE(xe
->xe_name_len
));
6265 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6266 * It is only used for inline xattr and xattr block.
6268 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super
*osb
,
6269 struct ocfs2_xattr_header
*xh
,
6270 struct buffer_head
*ref_root_bh
,
6272 struct ocfs2_alloc_context
**meta_ac
)
6274 int ret
, meta_add
= 0, num_recs
= 0;
6275 struct ocfs2_refcount_block
*rb
=
6276 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
6280 ret
= ocfs2_value_metas_in_xattr_header(osb
->sb
, NULL
, xh
,
6281 &meta_add
, credits
, &num_recs
,
6282 ocfs2_get_xattr_value_root
,
6290 * We need to add/modify num_recs in refcount tree, so just calculate
6291 * an approximate number we need for refcount tree change.
6292 * Sometimes we need to split the tree, and after split, half recs
6293 * will be moved to the new block, and a new block can only provide
6294 * half number of recs. So we multiple new blocks by 2.
6296 num_recs
= num_recs
/ ocfs2_refcount_recs_per_rb(osb
->sb
) * 2;
6297 meta_add
+= num_recs
;
6298 *credits
+= num_recs
+ num_recs
* OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
6299 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)
6300 *credits
+= le16_to_cpu(rb
->rf_list
.l_tree_depth
) *
6301 le16_to_cpu(rb
->rf_list
.l_next_free_rec
) + 1;
6305 ret
= ocfs2_reserve_new_metadata_blocks(osb
, meta_add
, meta_ac
);
6314 * Given a xattr header, reflink all the xattrs in this container.
6315 * It can be used for inode, block and bucket.
6318 * Before we call this function, the caller has memcpy the xattr in
6319 * old_xh to the new_xh.
6321 * If args.xattr_reflinked is set, call it to decide whether the xe should
6322 * be reflinked or not. If not, remove it from the new xattr header.
6324 static int ocfs2_reflink_xattr_header(handle_t
*handle
,
6325 struct ocfs2_xattr_reflink
*args
,
6326 struct buffer_head
*old_bh
,
6327 struct ocfs2_xattr_header
*xh
,
6328 struct buffer_head
*new_bh
,
6329 struct ocfs2_xattr_header
*new_xh
,
6330 struct ocfs2_xattr_value_buf
*vb
,
6331 struct ocfs2_alloc_context
*meta_ac
,
6332 get_xattr_value_root
*func
,
6336 struct super_block
*sb
= args
->old_inode
->i_sb
;
6337 struct buffer_head
*value_bh
;
6338 struct ocfs2_xattr_entry
*xe
, *last
;
6339 struct ocfs2_xattr_value_root
*xv
, *new_xv
;
6340 struct ocfs2_extent_tree data_et
;
6341 u32 clusters
, cpos
, p_cluster
, num_clusters
;
6342 unsigned int ext_flags
= 0;
6344 trace_ocfs2_reflink_xattr_header((unsigned long long)old_bh
->b_blocknr
,
6345 le16_to_cpu(xh
->xh_count
));
6347 last
= &new_xh
->xh_entries
[le16_to_cpu(new_xh
->xh_count
)];
6348 for (i
= 0, j
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, j
++) {
6349 xe
= &xh
->xh_entries
[i
];
6351 if (args
->xattr_reflinked
&& !args
->xattr_reflinked(xe
)) {
6352 xe
= &new_xh
->xh_entries
[j
];
6354 le16_add_cpu(&new_xh
->xh_count
, -1);
6355 if (new_xh
->xh_count
) {
6357 (void *)last
- (void *)xe
);
6359 sizeof(struct ocfs2_xattr_entry
));
6363 * We don't want j to increase in the next round since
6364 * it is already moved ahead.
6370 if (ocfs2_xattr_is_local(xe
))
6373 ret
= func(sb
, old_bh
, xh
, i
, &xv
, NULL
, para
);
6379 ret
= func(sb
, new_bh
, new_xh
, j
, &new_xv
, &value_bh
, para
);
6386 * For the xattr which has l_tree_depth = 0, all the extent
6387 * recs have already be copied to the new xh with the
6388 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6389 * increase the refount count int the refcount tree.
6391 * For the xattr which has l_tree_depth > 0, we need
6392 * to initialize it to the empty default value root,
6393 * and then insert the extents one by one.
6395 if (xv
->xr_list
.l_tree_depth
) {
6396 memcpy(new_xv
, &def_xv
, sizeof(def_xv
));
6398 vb
->vb_bh
= value_bh
;
6399 ocfs2_init_xattr_value_extent_tree(&data_et
,
6400 INODE_CACHE(args
->new_inode
), vb
);
6403 clusters
= le32_to_cpu(xv
->xr_clusters
);
6405 while (cpos
< clusters
) {
6406 ret
= ocfs2_xattr_get_clusters(args
->old_inode
,
6419 if (xv
->xr_list
.l_tree_depth
) {
6420 ret
= ocfs2_insert_extent(handle
,
6422 ocfs2_clusters_to_blocks(
6423 args
->old_inode
->i_sb
,
6425 num_clusters
, ext_flags
,
6433 ret
= ocfs2_increase_refcount(handle
, args
->ref_ci
,
6435 p_cluster
, num_clusters
,
6436 meta_ac
, args
->dealloc
);
6442 cpos
+= num_clusters
;
6450 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink
*args
)
6452 int ret
= 0, credits
= 0;
6454 struct ocfs2_super
*osb
= OCFS2_SB(args
->old_inode
->i_sb
);
6455 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)args
->old_bh
->b_data
;
6456 int inline_size
= le16_to_cpu(di
->i_xattr_inline_size
);
6457 int header_off
= osb
->sb
->s_blocksize
- inline_size
;
6458 struct ocfs2_xattr_header
*xh
= (struct ocfs2_xattr_header
*)
6459 (args
->old_bh
->b_data
+ header_off
);
6460 struct ocfs2_xattr_header
*new_xh
= (struct ocfs2_xattr_header
*)
6461 (args
->new_bh
->b_data
+ header_off
);
6462 struct ocfs2_alloc_context
*meta_ac
= NULL
;
6463 struct ocfs2_inode_info
*new_oi
;
6464 struct ocfs2_dinode
*new_di
;
6465 struct ocfs2_xattr_value_buf vb
= {
6466 .vb_bh
= args
->new_bh
,
6467 .vb_access
= ocfs2_journal_access_di
,
6470 ret
= ocfs2_reflink_lock_xattr_allocators(osb
, xh
, args
->ref_root_bh
,
6471 &credits
, &meta_ac
);
6477 handle
= ocfs2_start_trans(osb
, credits
);
6478 if (IS_ERR(handle
)) {
6479 ret
= PTR_ERR(handle
);
6484 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(args
->new_inode
),
6485 args
->new_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
6491 memcpy(args
->new_bh
->b_data
+ header_off
,
6492 args
->old_bh
->b_data
+ header_off
, inline_size
);
6494 new_di
= (struct ocfs2_dinode
*)args
->new_bh
->b_data
;
6495 new_di
->i_xattr_inline_size
= cpu_to_le16(inline_size
);
6497 ret
= ocfs2_reflink_xattr_header(handle
, args
, args
->old_bh
, xh
,
6498 args
->new_bh
, new_xh
, &vb
, meta_ac
,
6499 ocfs2_get_xattr_value_root
, NULL
);
6505 new_oi
= OCFS2_I(args
->new_inode
);
6507 * Adjust extent record count to reserve space for extended attribute.
6508 * Inline data count had been adjusted in ocfs2_duplicate_inline_data().
6510 if (!(new_oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) &&
6511 !(ocfs2_inode_is_fast_symlink(args
->new_inode
))) {
6512 struct ocfs2_extent_list
*el
= &new_di
->id2
.i_list
;
6513 le16_add_cpu(&el
->l_count
, -(inline_size
/
6514 sizeof(struct ocfs2_extent_rec
)));
6516 spin_lock(&new_oi
->ip_lock
);
6517 new_oi
->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
| OCFS2_INLINE_XATTR_FL
;
6518 new_di
->i_dyn_features
= cpu_to_le16(new_oi
->ip_dyn_features
);
6519 spin_unlock(&new_oi
->ip_lock
);
6521 ocfs2_journal_dirty(handle
, args
->new_bh
);
6524 ocfs2_commit_trans(osb
, handle
);
6528 ocfs2_free_alloc_context(meta_ac
);
6532 static int ocfs2_create_empty_xattr_block(struct inode
*inode
,
6533 struct buffer_head
*fe_bh
,
6534 struct buffer_head
**ret_bh
,
6538 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6539 struct ocfs2_xattr_set_ctxt ctxt
;
6541 memset(&ctxt
, 0, sizeof(ctxt
));
6542 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, &ctxt
.meta_ac
);
6548 ctxt
.handle
= ocfs2_start_trans(osb
, OCFS2_XATTR_BLOCK_CREATE_CREDITS
);
6549 if (IS_ERR(ctxt
.handle
)) {
6550 ret
= PTR_ERR(ctxt
.handle
);
6555 trace_ocfs2_create_empty_xattr_block(
6556 (unsigned long long)fe_bh
->b_blocknr
, indexed
);
6557 ret
= ocfs2_create_xattr_block(inode
, fe_bh
, &ctxt
, indexed
,
6562 ocfs2_commit_trans(osb
, ctxt
.handle
);
6564 ocfs2_free_alloc_context(ctxt
.meta_ac
);
6568 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink
*args
,
6569 struct buffer_head
*blk_bh
,
6570 struct buffer_head
*new_blk_bh
)
6572 int ret
= 0, credits
= 0;
6574 struct ocfs2_inode_info
*new_oi
= OCFS2_I(args
->new_inode
);
6575 struct ocfs2_dinode
*new_di
;
6576 struct ocfs2_super
*osb
= OCFS2_SB(args
->new_inode
->i_sb
);
6577 int header_off
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
6578 struct ocfs2_xattr_block
*xb
=
6579 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
6580 struct ocfs2_xattr_header
*xh
= &xb
->xb_attrs
.xb_header
;
6581 struct ocfs2_xattr_block
*new_xb
=
6582 (struct ocfs2_xattr_block
*)new_blk_bh
->b_data
;
6583 struct ocfs2_xattr_header
*new_xh
= &new_xb
->xb_attrs
.xb_header
;
6584 struct ocfs2_alloc_context
*meta_ac
;
6585 struct ocfs2_xattr_value_buf vb
= {
6586 .vb_bh
= new_blk_bh
,
6587 .vb_access
= ocfs2_journal_access_xb
,
6590 ret
= ocfs2_reflink_lock_xattr_allocators(osb
, xh
, args
->ref_root_bh
,
6591 &credits
, &meta_ac
);
6597 /* One more credits in case we need to add xattr flags in new inode. */
6598 handle
= ocfs2_start_trans(osb
, credits
+ 1);
6599 if (IS_ERR(handle
)) {
6600 ret
= PTR_ERR(handle
);
6605 if (!(new_oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
)) {
6606 ret
= ocfs2_journal_access_di(handle
,
6607 INODE_CACHE(args
->new_inode
),
6609 OCFS2_JOURNAL_ACCESS_WRITE
);
6616 ret
= ocfs2_journal_access_xb(handle
, INODE_CACHE(args
->new_inode
),
6617 new_blk_bh
, OCFS2_JOURNAL_ACCESS_WRITE
);
6623 memcpy(new_blk_bh
->b_data
+ header_off
, blk_bh
->b_data
+ header_off
,
6624 osb
->sb
->s_blocksize
- header_off
);
6626 ret
= ocfs2_reflink_xattr_header(handle
, args
, blk_bh
, xh
,
6627 new_blk_bh
, new_xh
, &vb
, meta_ac
,
6628 ocfs2_get_xattr_value_root
, NULL
);
6634 ocfs2_journal_dirty(handle
, new_blk_bh
);
6636 if (!(new_oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
)) {
6637 new_di
= (struct ocfs2_dinode
*)args
->new_bh
->b_data
;
6638 spin_lock(&new_oi
->ip_lock
);
6639 new_oi
->ip_dyn_features
|= OCFS2_HAS_XATTR_FL
;
6640 new_di
->i_dyn_features
= cpu_to_le16(new_oi
->ip_dyn_features
);
6641 spin_unlock(&new_oi
->ip_lock
);
6643 ocfs2_journal_dirty(handle
, args
->new_bh
);
6647 ocfs2_commit_trans(osb
, handle
);
6650 ocfs2_free_alloc_context(meta_ac
);
6654 struct ocfs2_reflink_xattr_tree_args
{
6655 struct ocfs2_xattr_reflink
*reflink
;
6656 struct buffer_head
*old_blk_bh
;
6657 struct buffer_head
*new_blk_bh
;
6658 struct ocfs2_xattr_bucket
*old_bucket
;
6659 struct ocfs2_xattr_bucket
*new_bucket
;
6664 * We have to handle the case that both old bucket and new bucket
6665 * will call this function to get the right ret_bh.
6666 * So The caller must give us the right bh.
6668 static int ocfs2_get_reflink_xattr_value_root(struct super_block
*sb
,
6669 struct buffer_head
*bh
,
6670 struct ocfs2_xattr_header
*xh
,
6672 struct ocfs2_xattr_value_root
**xv
,
6673 struct buffer_head
**ret_bh
,
6676 struct ocfs2_reflink_xattr_tree_args
*args
=
6677 (struct ocfs2_reflink_xattr_tree_args
*)para
;
6678 struct ocfs2_xattr_bucket
*bucket
;
6680 if (bh
== args
->old_bucket
->bu_bhs
[0])
6681 bucket
= args
->old_bucket
;
6683 bucket
= args
->new_bucket
;
6685 return ocfs2_get_xattr_tree_value_root(sb
, bucket
, offset
,
6689 struct ocfs2_value_tree_metas
{
6695 static int ocfs2_value_tree_metas_in_bucket(struct super_block
*sb
,
6696 struct buffer_head
*bh
,
6697 struct ocfs2_xattr_header
*xh
,
6699 struct ocfs2_xattr_value_root
**xv
,
6700 struct buffer_head
**ret_bh
,
6703 struct ocfs2_xattr_bucket
*bucket
=
6704 (struct ocfs2_xattr_bucket
*)para
;
6706 return ocfs2_get_xattr_tree_value_root(sb
, bucket
, offset
,
6710 static int ocfs2_calc_value_tree_metas(struct inode
*inode
,
6711 struct ocfs2_xattr_bucket
*bucket
,
6714 struct ocfs2_value_tree_metas
*metas
=
6715 (struct ocfs2_value_tree_metas
*)para
;
6716 struct ocfs2_xattr_header
*xh
=
6717 (struct ocfs2_xattr_header
*)bucket
->bu_bhs
[0]->b_data
;
6719 /* Add the credits for this bucket first. */
6720 metas
->credits
+= bucket
->bu_blocks
;
6721 return ocfs2_value_metas_in_xattr_header(inode
->i_sb
, bucket
->bu_bhs
[0],
6722 xh
, &metas
->num_metas
,
6723 &metas
->credits
, &metas
->num_recs
,
6724 ocfs2_value_tree_metas_in_bucket
,
6729 * Given a xattr extent rec starting from blkno and having len clusters,
6730 * iterate all the buckets calculate how much metadata we need for reflinking
6731 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6733 static int ocfs2_lock_reflink_xattr_rec_allocators(
6734 struct ocfs2_reflink_xattr_tree_args
*args
,
6735 struct ocfs2_extent_tree
*xt_et
,
6736 u64 blkno
, u32 len
, int *credits
,
6737 struct ocfs2_alloc_context
**meta_ac
,
6738 struct ocfs2_alloc_context
**data_ac
)
6740 int ret
, num_free_extents
;
6741 struct ocfs2_value_tree_metas metas
;
6742 struct ocfs2_super
*osb
= OCFS2_SB(args
->reflink
->old_inode
->i_sb
);
6743 struct ocfs2_refcount_block
*rb
;
6745 memset(&metas
, 0, sizeof(metas
));
6747 ret
= ocfs2_iterate_xattr_buckets(args
->reflink
->old_inode
, blkno
, len
,
6748 ocfs2_calc_value_tree_metas
, &metas
);
6754 *credits
= metas
.credits
;
6757 * Calculate we need for refcount tree change.
6759 * We need to add/modify num_recs in refcount tree, so just calculate
6760 * an approximate number we need for refcount tree change.
6761 * Sometimes we need to split the tree, and after split, half recs
6762 * will be moved to the new block, and a new block can only provide
6763 * half number of recs. So we multiple new blocks by 2.
6764 * In the end, we have to add credits for modifying the already
6765 * existed refcount block.
6767 rb
= (struct ocfs2_refcount_block
*)args
->reflink
->ref_root_bh
->b_data
;
6769 (metas
.num_recs
+ ocfs2_refcount_recs_per_rb(osb
->sb
) - 1) /
6770 ocfs2_refcount_recs_per_rb(osb
->sb
) * 2;
6771 metas
.num_metas
+= metas
.num_recs
;
6772 *credits
+= metas
.num_recs
+
6773 metas
.num_recs
* OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
6774 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)
6775 *credits
+= le16_to_cpu(rb
->rf_list
.l_tree_depth
) *
6776 le16_to_cpu(rb
->rf_list
.l_next_free_rec
) + 1;
6780 /* count in the xattr tree change. */
6781 num_free_extents
= ocfs2_num_free_extents(osb
, xt_et
);
6782 if (num_free_extents
< 0) {
6783 ret
= num_free_extents
;
6788 if (num_free_extents
< len
)
6789 metas
.num_metas
+= ocfs2_extend_meta_needed(xt_et
->et_root_el
);
6791 *credits
+= ocfs2_calc_extend_credits(osb
->sb
,
6794 if (metas
.num_metas
) {
6795 ret
= ocfs2_reserve_new_metadata_blocks(osb
, metas
.num_metas
,
6804 ret
= ocfs2_reserve_clusters(osb
, len
, data_ac
);
6811 ocfs2_free_alloc_context(*meta_ac
);
6819 static int ocfs2_reflink_xattr_bucket(handle_t
*handle
,
6820 u64 blkno
, u64 new_blkno
, u32 clusters
,
6821 u32
*cpos
, int num_buckets
,
6822 struct ocfs2_alloc_context
*meta_ac
,
6823 struct ocfs2_alloc_context
*data_ac
,
6824 struct ocfs2_reflink_xattr_tree_args
*args
)
6827 struct super_block
*sb
= args
->reflink
->old_inode
->i_sb
;
6828 int bpb
= args
->old_bucket
->bu_blocks
;
6829 struct ocfs2_xattr_value_buf vb
= {
6830 .vb_access
= ocfs2_journal_access
,
6833 for (i
= 0; i
< num_buckets
; i
++, blkno
+= bpb
, new_blkno
+= bpb
) {
6834 ret
= ocfs2_read_xattr_bucket(args
->old_bucket
, blkno
);
6840 ret
= ocfs2_init_xattr_bucket(args
->new_bucket
, new_blkno
, 1);
6846 ret
= ocfs2_xattr_bucket_journal_access(handle
,
6848 OCFS2_JOURNAL_ACCESS_CREATE
);
6854 for (j
= 0; j
< bpb
; j
++)
6855 memcpy(bucket_block(args
->new_bucket
, j
),
6856 bucket_block(args
->old_bucket
, j
),
6860 * Record the start cpos so that we can use it to initialize
6861 * our xattr tree we also set the xh_num_bucket for the new
6865 *cpos
= le32_to_cpu(bucket_xh(args
->new_bucket
)->
6866 xh_entries
[0].xe_name_hash
);
6867 bucket_xh(args
->new_bucket
)->xh_num_buckets
=
6868 cpu_to_le16(num_buckets
);
6871 ocfs2_xattr_bucket_journal_dirty(handle
, args
->new_bucket
);
6873 ret
= ocfs2_reflink_xattr_header(handle
, args
->reflink
,
6874 args
->old_bucket
->bu_bhs
[0],
6875 bucket_xh(args
->old_bucket
),
6876 args
->new_bucket
->bu_bhs
[0],
6877 bucket_xh(args
->new_bucket
),
6879 ocfs2_get_reflink_xattr_value_root
,
6887 * Re-access and dirty the bucket to calculate metaecc.
6888 * Because we may extend the transaction in reflink_xattr_header
6889 * which will let the already accessed block gone.
6891 ret
= ocfs2_xattr_bucket_journal_access(handle
,
6893 OCFS2_JOURNAL_ACCESS_WRITE
);
6899 ocfs2_xattr_bucket_journal_dirty(handle
, args
->new_bucket
);
6901 ocfs2_xattr_bucket_relse(args
->old_bucket
);
6902 ocfs2_xattr_bucket_relse(args
->new_bucket
);
6905 ocfs2_xattr_bucket_relse(args
->old_bucket
);
6906 ocfs2_xattr_bucket_relse(args
->new_bucket
);
6910 static int ocfs2_reflink_xattr_buckets(handle_t
*handle
,
6911 struct inode
*inode
,
6912 struct ocfs2_reflink_xattr_tree_args
*args
,
6913 struct ocfs2_extent_tree
*et
,
6914 struct ocfs2_alloc_context
*meta_ac
,
6915 struct ocfs2_alloc_context
*data_ac
,
6916 u64 blkno
, u32 cpos
, u32 len
)
6918 int ret
, first_inserted
= 0;
6919 u32 p_cluster
, num_clusters
, reflink_cpos
= 0;
6921 unsigned int num_buckets
, reflink_buckets
;
6923 ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
6925 ret
= ocfs2_read_xattr_bucket(args
->old_bucket
, blkno
);
6930 num_buckets
= le16_to_cpu(bucket_xh(args
->old_bucket
)->xh_num_buckets
);
6931 ocfs2_xattr_bucket_relse(args
->old_bucket
);
6933 while (len
&& num_buckets
) {
6934 ret
= ocfs2_claim_clusters(handle
, data_ac
,
6935 1, &p_cluster
, &num_clusters
);
6941 new_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
6942 reflink_buckets
= min(num_buckets
, bpc
* num_clusters
);
6944 ret
= ocfs2_reflink_xattr_bucket(handle
, blkno
,
6945 new_blkno
, num_clusters
,
6946 &reflink_cpos
, reflink_buckets
,
6947 meta_ac
, data_ac
, args
);
6954 * For the 1st allocated cluster, we make it use the same cpos
6955 * so that the xattr tree looks the same as the original one
6958 if (!first_inserted
) {
6959 reflink_cpos
= cpos
;
6962 ret
= ocfs2_insert_extent(handle
, et
, reflink_cpos
, new_blkno
,
6963 num_clusters
, 0, meta_ac
);
6967 trace_ocfs2_reflink_xattr_buckets((unsigned long long)new_blkno
,
6968 num_clusters
, reflink_cpos
);
6970 len
-= num_clusters
;
6971 blkno
+= ocfs2_clusters_to_blocks(inode
->i_sb
, num_clusters
);
6972 num_buckets
-= reflink_buckets
;
6979 * Create the same xattr extent record in the new inode's xattr tree.
6981 static int ocfs2_reflink_xattr_rec(struct inode
*inode
,
6982 struct buffer_head
*root_bh
,
6988 int ret
, credits
= 0;
6990 struct ocfs2_reflink_xattr_tree_args
*args
=
6991 (struct ocfs2_reflink_xattr_tree_args
*)para
;
6992 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6993 struct ocfs2_alloc_context
*meta_ac
= NULL
;
6994 struct ocfs2_alloc_context
*data_ac
= NULL
;
6995 struct ocfs2_extent_tree et
;
6997 trace_ocfs2_reflink_xattr_rec((unsigned long long)blkno
, len
);
6999 ocfs2_init_xattr_tree_extent_tree(&et
,
7000 INODE_CACHE(args
->reflink
->new_inode
),
7003 ret
= ocfs2_lock_reflink_xattr_rec_allocators(args
, &et
, blkno
,
7005 &meta_ac
, &data_ac
);
7011 handle
= ocfs2_start_trans(osb
, credits
);
7012 if (IS_ERR(handle
)) {
7013 ret
= PTR_ERR(handle
);
7018 ret
= ocfs2_reflink_xattr_buckets(handle
, inode
, args
, &et
,
7024 ocfs2_commit_trans(osb
, handle
);
7028 ocfs2_free_alloc_context(meta_ac
);
7030 ocfs2_free_alloc_context(data_ac
);
7035 * Create reflinked xattr buckets.
7036 * We will add bucket one by one, and refcount all the xattrs in the bucket
7037 * if they are stored outside.
7039 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink
*args
,
7040 struct buffer_head
*blk_bh
,
7041 struct buffer_head
*new_blk_bh
)
7044 struct ocfs2_reflink_xattr_tree_args para
;
7046 memset(¶
, 0, sizeof(para
));
7047 para
.reflink
= args
;
7048 para
.old_blk_bh
= blk_bh
;
7049 para
.new_blk_bh
= new_blk_bh
;
7051 para
.old_bucket
= ocfs2_xattr_bucket_new(args
->old_inode
);
7052 if (!para
.old_bucket
) {
7053 mlog_errno(-ENOMEM
);
7057 para
.new_bucket
= ocfs2_xattr_bucket_new(args
->new_inode
);
7058 if (!para
.new_bucket
) {
7064 ret
= ocfs2_iterate_xattr_index_block(args
->old_inode
, blk_bh
,
7065 ocfs2_reflink_xattr_rec
,
7071 ocfs2_xattr_bucket_free(para
.old_bucket
);
7072 ocfs2_xattr_bucket_free(para
.new_bucket
);
7076 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink
*args
,
7077 struct buffer_head
*blk_bh
)
7079 int ret
, indexed
= 0;
7080 struct buffer_head
*new_blk_bh
= NULL
;
7081 struct ocfs2_xattr_block
*xb
=
7082 (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
7085 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)
7088 ret
= ocfs2_create_empty_xattr_block(args
->new_inode
, args
->new_bh
,
7089 &new_blk_bh
, indexed
);
7096 ret
= ocfs2_reflink_xattr_block(args
, blk_bh
, new_blk_bh
);
7098 ret
= ocfs2_reflink_xattr_tree(args
, blk_bh
, new_blk_bh
);
7107 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry
*xe
)
7109 int type
= ocfs2_xattr_get_type(xe
);
7111 return type
!= OCFS2_XATTR_INDEX_SECURITY
&&
7112 type
!= OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
&&
7113 type
!= OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
;
7116 int ocfs2_reflink_xattrs(struct inode
*old_inode
,
7117 struct buffer_head
*old_bh
,
7118 struct inode
*new_inode
,
7119 struct buffer_head
*new_bh
,
7120 bool preserve_security
)
7123 struct ocfs2_xattr_reflink args
;
7124 struct ocfs2_inode_info
*oi
= OCFS2_I(old_inode
);
7125 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)old_bh
->b_data
;
7126 struct buffer_head
*blk_bh
= NULL
;
7127 struct ocfs2_cached_dealloc_ctxt dealloc
;
7128 struct ocfs2_refcount_tree
*ref_tree
;
7129 struct buffer_head
*ref_root_bh
= NULL
;
7131 ret
= ocfs2_lock_refcount_tree(OCFS2_SB(old_inode
->i_sb
),
7132 le64_to_cpu(di
->i_refcount_loc
),
7133 1, &ref_tree
, &ref_root_bh
);
7139 ocfs2_init_dealloc_ctxt(&dealloc
);
7141 args
.old_inode
= old_inode
;
7142 args
.new_inode
= new_inode
;
7143 args
.old_bh
= old_bh
;
7144 args
.new_bh
= new_bh
;
7145 args
.ref_ci
= &ref_tree
->rf_ci
;
7146 args
.ref_root_bh
= ref_root_bh
;
7147 args
.dealloc
= &dealloc
;
7148 if (preserve_security
)
7149 args
.xattr_reflinked
= NULL
;
7151 args
.xattr_reflinked
= ocfs2_reflink_xattr_no_security
;
7153 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
7154 ret
= ocfs2_reflink_xattr_inline(&args
);
7161 if (!di
->i_xattr_loc
)
7164 ret
= ocfs2_read_xattr_block(old_inode
, le64_to_cpu(di
->i_xattr_loc
),
7171 ret
= ocfs2_reflink_xattr_in_block(&args
, blk_bh
);
7178 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode
->i_sb
),
7180 brelse(ref_root_bh
);
7182 if (ocfs2_dealloc_has_cluster(&dealloc
)) {
7183 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode
->i_sb
), 1);
7184 ocfs2_run_deallocs(OCFS2_SB(old_inode
->i_sb
), &dealloc
);
7192 * Initialize security and acl for a already created inode.
7193 * Used for reflink a non-preserve-security file.
7195 * It uses common api like ocfs2_xattr_set, so the caller
7196 * must not hold any lock expect i_mutex.
7198 int ocfs2_init_security_and_acl(struct inode
*dir
,
7199 struct inode
*inode
,
7200 const struct qstr
*qstr
,
7201 struct posix_acl
*default_acl
,
7202 struct posix_acl
*acl
)
7204 struct buffer_head
*dir_bh
= NULL
;
7207 ret
= ocfs2_init_security_get(inode
, dir
, qstr
, NULL
);
7213 ret
= ocfs2_inode_lock(dir
, &dir_bh
, 0);
7219 if (!ret
&& default_acl
)
7220 ret
= ocfs2_iop_set_acl(inode
, default_acl
, ACL_TYPE_DEFAULT
);
7222 ret
= ocfs2_iop_set_acl(inode
, acl
, ACL_TYPE_ACCESS
);
7224 ocfs2_inode_unlock(dir
, 0);
7230 * 'security' attributes support
7232 static size_t ocfs2_xattr_security_list(struct dentry
*dentry
, char *list
,
7233 size_t list_size
, const char *name
,
7234 size_t name_len
, int type
)
7236 const size_t prefix_len
= XATTR_SECURITY_PREFIX_LEN
;
7237 const size_t total_len
= prefix_len
+ name_len
+ 1;
7239 if (list
&& total_len
<= list_size
) {
7240 memcpy(list
, XATTR_SECURITY_PREFIX
, prefix_len
);
7241 memcpy(list
+ prefix_len
, name
, name_len
);
7242 list
[prefix_len
+ name_len
] = '\0';
7247 static int ocfs2_xattr_security_get(struct dentry
*dentry
, const char *name
,
7248 void *buffer
, size_t size
, int type
)
7250 if (strcmp(name
, "") == 0)
7252 return ocfs2_xattr_get(d_inode(dentry
), OCFS2_XATTR_INDEX_SECURITY
,
7253 name
, buffer
, size
);
7256 static int ocfs2_xattr_security_set(struct dentry
*dentry
, const char *name
,
7257 const void *value
, size_t size
, int flags
, int type
)
7259 if (strcmp(name
, "") == 0)
7262 return ocfs2_xattr_set(d_inode(dentry
), OCFS2_XATTR_INDEX_SECURITY
,
7263 name
, value
, size
, flags
);
7266 static int ocfs2_initxattrs(struct inode
*inode
, const struct xattr
*xattr_array
,
7269 const struct xattr
*xattr
;
7272 for (xattr
= xattr_array
; xattr
->name
!= NULL
; xattr
++) {
7273 err
= ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_SECURITY
,
7274 xattr
->name
, xattr
->value
,
7275 xattr
->value_len
, XATTR_CREATE
);
7282 int ocfs2_init_security_get(struct inode
*inode
,
7284 const struct qstr
*qstr
,
7285 struct ocfs2_security_xattr_info
*si
)
7287 /* check whether ocfs2 support feature xattr */
7288 if (!ocfs2_supports_xattr(OCFS2_SB(dir
->i_sb
)))
7291 return security_old_inode_init_security(inode
, dir
, qstr
,
7292 &si
->name
, &si
->value
,
7295 return security_inode_init_security(inode
, dir
, qstr
,
7296 &ocfs2_initxattrs
, NULL
);
7299 int ocfs2_init_security_set(handle_t
*handle
,
7300 struct inode
*inode
,
7301 struct buffer_head
*di_bh
,
7302 struct ocfs2_security_xattr_info
*si
,
7303 struct ocfs2_alloc_context
*xattr_ac
,
7304 struct ocfs2_alloc_context
*data_ac
)
7306 return ocfs2_xattr_set_handle(handle
, inode
, di_bh
,
7307 OCFS2_XATTR_INDEX_SECURITY
,
7308 si
->name
, si
->value
, si
->value_len
, 0,
7312 const struct xattr_handler ocfs2_xattr_security_handler
= {
7313 .prefix
= XATTR_SECURITY_PREFIX
,
7314 .list
= ocfs2_xattr_security_list
,
7315 .get
= ocfs2_xattr_security_get
,
7316 .set
= ocfs2_xattr_security_set
,
7320 * 'trusted' attributes support
7322 static size_t ocfs2_xattr_trusted_list(struct dentry
*dentry
, char *list
,
7323 size_t list_size
, const char *name
,
7324 size_t name_len
, int type
)
7326 const size_t prefix_len
= XATTR_TRUSTED_PREFIX_LEN
;
7327 const size_t total_len
= prefix_len
+ name_len
+ 1;
7329 if (!capable(CAP_SYS_ADMIN
))
7332 if (list
&& total_len
<= list_size
) {
7333 memcpy(list
, XATTR_TRUSTED_PREFIX
, prefix_len
);
7334 memcpy(list
+ prefix_len
, name
, name_len
);
7335 list
[prefix_len
+ name_len
] = '\0';
7340 static int ocfs2_xattr_trusted_get(struct dentry
*dentry
, const char *name
,
7341 void *buffer
, size_t size
, int type
)
7343 if (strcmp(name
, "") == 0)
7345 return ocfs2_xattr_get(d_inode(dentry
), OCFS2_XATTR_INDEX_TRUSTED
,
7346 name
, buffer
, size
);
7349 static int ocfs2_xattr_trusted_set(struct dentry
*dentry
, const char *name
,
7350 const void *value
, size_t size
, int flags
, int type
)
7352 if (strcmp(name
, "") == 0)
7355 return ocfs2_xattr_set(d_inode(dentry
), OCFS2_XATTR_INDEX_TRUSTED
,
7356 name
, value
, size
, flags
);
7359 const struct xattr_handler ocfs2_xattr_trusted_handler
= {
7360 .prefix
= XATTR_TRUSTED_PREFIX
,
7361 .list
= ocfs2_xattr_trusted_list
,
7362 .get
= ocfs2_xattr_trusted_get
,
7363 .set
= ocfs2_xattr_trusted_set
,
7367 * 'user' attributes support
7369 static size_t ocfs2_xattr_user_list(struct dentry
*dentry
, char *list
,
7370 size_t list_size
, const char *name
,
7371 size_t name_len
, int type
)
7373 const size_t prefix_len
= XATTR_USER_PREFIX_LEN
;
7374 const size_t total_len
= prefix_len
+ name_len
+ 1;
7375 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
7377 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7380 if (list
&& total_len
<= list_size
) {
7381 memcpy(list
, XATTR_USER_PREFIX
, prefix_len
);
7382 memcpy(list
+ prefix_len
, name
, name_len
);
7383 list
[prefix_len
+ name_len
] = '\0';
7388 static int ocfs2_xattr_user_get(struct dentry
*dentry
, const char *name
,
7389 void *buffer
, size_t size
, int type
)
7391 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
7393 if (strcmp(name
, "") == 0)
7395 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7397 return ocfs2_xattr_get(d_inode(dentry
), OCFS2_XATTR_INDEX_USER
, name
,
7401 static int ocfs2_xattr_user_set(struct dentry
*dentry
, const char *name
,
7402 const void *value
, size_t size
, int flags
, int type
)
7404 struct ocfs2_super
*osb
= OCFS2_SB(dentry
->d_sb
);
7406 if (strcmp(name
, "") == 0)
7408 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
7411 return ocfs2_xattr_set(d_inode(dentry
), OCFS2_XATTR_INDEX_USER
,
7412 name
, value
, size
, flags
);
7415 const struct xattr_handler ocfs2_xattr_user_handler
= {
7416 .prefix
= XATTR_USER_PREFIX
,
7417 .list
= ocfs2_xattr_user_list
,
7418 .get
= ocfs2_xattr_user_get
,
7419 .set
= ocfs2_xattr_user_set
,