1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
9 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/security.h>
40 #define MLOG_MASK_PREFIX ML_XATTR
41 #include <cluster/masklog.h>
54 #include "buffer_head_io.h"
59 struct ocfs2_xattr_def_value_root
{
60 struct ocfs2_xattr_value_root xv
;
61 struct ocfs2_extent_rec er
;
64 struct ocfs2_xattr_bucket
{
65 /* The inode these xattrs are associated with */
66 struct inode
*bu_inode
;
68 /* The actual buffers that make up the bucket */
69 struct buffer_head
*bu_bhs
[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
];
71 /* How many blocks make up one bucket for this filesystem */
75 struct ocfs2_xattr_set_ctxt
{
77 struct ocfs2_alloc_context
*meta_ac
;
78 struct ocfs2_alloc_context
*data_ac
;
79 struct ocfs2_cached_dealloc_ctxt dealloc
;
82 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
83 #define OCFS2_XATTR_INLINE_SIZE 80
84 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
85 - sizeof(struct ocfs2_xattr_header) \
88 static struct ocfs2_xattr_def_value_root def_xv
= {
89 .xv
.xr_list
.l_count
= cpu_to_le16(1),
92 struct xattr_handler
*ocfs2_xattr_handlers
[] = {
93 &ocfs2_xattr_user_handler
,
94 &ocfs2_xattr_trusted_handler
,
95 &ocfs2_xattr_security_handler
,
99 static struct xattr_handler
*ocfs2_xattr_handler_map
[OCFS2_XATTR_MAX
] = {
100 [OCFS2_XATTR_INDEX_USER
] = &ocfs2_xattr_user_handler
,
101 [OCFS2_XATTR_INDEX_TRUSTED
] = &ocfs2_xattr_trusted_handler
,
102 [OCFS2_XATTR_INDEX_SECURITY
] = &ocfs2_xattr_security_handler
,
105 struct ocfs2_xattr_info
{
112 struct ocfs2_xattr_search
{
113 struct buffer_head
*inode_bh
;
115 * xattr_bh point to the block buffer head which has extended attribute
116 * when extended attribute in inode, xattr_bh is equal to inode_bh.
118 struct buffer_head
*xattr_bh
;
119 struct ocfs2_xattr_header
*header
;
120 struct ocfs2_xattr_bucket
*bucket
;
123 struct ocfs2_xattr_entry
*here
;
127 static int ocfs2_xattr_bucket_get_name_value(struct inode
*inode
,
128 struct ocfs2_xattr_header
*xh
,
133 static int ocfs2_xattr_block_find(struct inode
*inode
,
136 struct ocfs2_xattr_search
*xs
);
137 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
138 struct buffer_head
*root_bh
,
141 struct ocfs2_xattr_search
*xs
);
143 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
144 struct ocfs2_xattr_tree_root
*xt
,
148 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
149 struct ocfs2_xattr_search
*xs
,
150 struct ocfs2_xattr_set_ctxt
*ctxt
);
152 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
153 struct ocfs2_xattr_info
*xi
,
154 struct ocfs2_xattr_search
*xs
,
155 struct ocfs2_xattr_set_ctxt
*ctxt
);
157 static int ocfs2_delete_xattr_index_block(struct inode
*inode
,
158 struct buffer_head
*xb_bh
);
160 static inline u16
ocfs2_xattr_buckets_per_cluster(struct ocfs2_super
*osb
)
162 return (1 << osb
->s_clustersize_bits
) / OCFS2_XATTR_BUCKET_SIZE
;
165 static inline u16
ocfs2_blocks_per_xattr_bucket(struct super_block
*sb
)
167 return OCFS2_XATTR_BUCKET_SIZE
/ (1 << sb
->s_blocksize_bits
);
170 static inline u16
ocfs2_xattr_max_xe_in_bucket(struct super_block
*sb
)
172 u16 len
= sb
->s_blocksize
-
173 offsetof(struct ocfs2_xattr_header
, xh_entries
);
175 return len
/ sizeof(struct ocfs2_xattr_entry
);
178 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
179 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
180 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
182 static struct ocfs2_xattr_bucket
*ocfs2_xattr_bucket_new(struct inode
*inode
)
184 struct ocfs2_xattr_bucket
*bucket
;
185 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
187 BUG_ON(blks
> OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
);
189 bucket
= kzalloc(sizeof(struct ocfs2_xattr_bucket
), GFP_NOFS
);
191 bucket
->bu_inode
= inode
;
192 bucket
->bu_blocks
= blks
;
198 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket
*bucket
)
202 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
203 brelse(bucket
->bu_bhs
[i
]);
204 bucket
->bu_bhs
[i
] = NULL
;
208 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket
*bucket
)
211 ocfs2_xattr_bucket_relse(bucket
);
212 bucket
->bu_inode
= NULL
;
218 * A bucket that has never been written to disk doesn't need to be
219 * read. We just need the buffer_heads. Don't call this for
220 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
223 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
228 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
229 bucket
->bu_bhs
[i
] = sb_getblk(bucket
->bu_inode
->i_sb
,
231 if (!bucket
->bu_bhs
[i
]) {
237 if (!ocfs2_buffer_uptodate(bucket
->bu_inode
,
239 ocfs2_set_new_buffer_uptodate(bucket
->bu_inode
,
244 ocfs2_xattr_bucket_relse(bucket
);
248 /* Read the xattr bucket at xb_blkno */
249 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
254 rc
= ocfs2_read_blocks(bucket
->bu_inode
, xb_blkno
,
255 bucket
->bu_blocks
, bucket
->bu_bhs
, 0);
257 ocfs2_xattr_bucket_relse(bucket
);
261 static int ocfs2_xattr_bucket_journal_access(handle_t
*handle
,
262 struct ocfs2_xattr_bucket
*bucket
,
267 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
268 rc
= ocfs2_journal_access(handle
, bucket
->bu_inode
,
269 bucket
->bu_bhs
[i
], type
);
279 static void ocfs2_xattr_bucket_journal_dirty(handle_t
*handle
,
280 struct ocfs2_xattr_bucket
*bucket
)
284 for (i
= 0; i
< bucket
->bu_blocks
; i
++)
285 ocfs2_journal_dirty(handle
, bucket
->bu_bhs
[i
]);
288 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket
*dest
,
289 struct ocfs2_xattr_bucket
*src
)
292 int blocksize
= src
->bu_inode
->i_sb
->s_blocksize
;
294 BUG_ON(dest
->bu_blocks
!= src
->bu_blocks
);
295 BUG_ON(dest
->bu_inode
!= src
->bu_inode
);
297 for (i
= 0; i
< src
->bu_blocks
; i
++) {
298 memcpy(bucket_block(dest
, i
), bucket_block(src
, i
),
303 static inline const char *ocfs2_xattr_prefix(int name_index
)
305 struct xattr_handler
*handler
= NULL
;
307 if (name_index
> 0 && name_index
< OCFS2_XATTR_MAX
)
308 handler
= ocfs2_xattr_handler_map
[name_index
];
310 return handler
? handler
->prefix
: NULL
;
313 static u32
ocfs2_xattr_name_hash(struct inode
*inode
,
317 /* Get hash value of uuid from super block */
318 u32 hash
= OCFS2_SB(inode
->i_sb
)->uuid_hash
;
321 /* hash extended attribute name */
322 for (i
= 0; i
< name_len
; i
++) {
323 hash
= (hash
<< OCFS2_HASH_SHIFT
) ^
324 (hash
>> (8*sizeof(hash
) - OCFS2_HASH_SHIFT
)) ^
332 * ocfs2_xattr_hash_entry()
334 * Compute the hash of an extended attribute.
336 static void ocfs2_xattr_hash_entry(struct inode
*inode
,
337 struct ocfs2_xattr_header
*header
,
338 struct ocfs2_xattr_entry
*entry
)
341 char *name
= (char *)header
+ le16_to_cpu(entry
->xe_name_offset
);
343 hash
= ocfs2_xattr_name_hash(inode
, name
, entry
->xe_name_len
);
344 entry
->xe_name_hash
= cpu_to_le32(hash
);
349 static int ocfs2_xattr_entry_real_size(int name_len
, size_t value_len
)
353 if (value_len
<= OCFS2_XATTR_INLINE_SIZE
)
354 size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_SIZE(value_len
);
356 size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
357 size
+= sizeof(struct ocfs2_xattr_entry
);
362 int ocfs2_calc_security_init(struct inode
*dir
,
363 struct ocfs2_security_xattr_info
*si
,
366 struct ocfs2_alloc_context
**xattr_ac
)
369 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
370 int s_size
= ocfs2_xattr_entry_real_size(strlen(si
->name
),
374 * The max space of security xattr taken inline is
375 * 256(name) + 80(value) + 16(entry) = 352 bytes,
376 * So reserve one metadata block for it is ok.
378 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
||
379 s_size
> OCFS2_XATTR_FREE_IN_IBODY
) {
380 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, xattr_ac
);
385 *xattr_credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
388 /* reserve clusters for xattr value which will be set in B tree*/
389 if (si
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
390 *want_clusters
+= ocfs2_clusters_for_bytes(dir
->i_sb
,
395 static int ocfs2_xattr_extend_allocation(struct inode
*inode
,
397 struct buffer_head
*xattr_bh
,
398 struct ocfs2_xattr_value_root
*xv
,
399 struct ocfs2_xattr_set_ctxt
*ctxt
)
402 handle_t
*handle
= ctxt
->handle
;
403 enum ocfs2_alloc_restarted why
;
404 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
405 u32 prev_clusters
, logical_start
= le32_to_cpu(xv
->xr_clusters
);
406 struct ocfs2_extent_tree et
;
408 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add
);
410 ocfs2_init_xattr_value_extent_tree(&et
, inode
, xattr_bh
, xv
);
412 status
= ocfs2_journal_access(handle
, inode
, xattr_bh
,
413 OCFS2_JOURNAL_ACCESS_WRITE
);
419 prev_clusters
= le32_to_cpu(xv
->xr_clusters
);
420 status
= ocfs2_add_clusters_in_btree(osb
,
435 status
= ocfs2_journal_dirty(handle
, xattr_bh
);
441 clusters_to_add
-= le32_to_cpu(xv
->xr_clusters
) - prev_clusters
;
444 * We should have already allocated enough space before the transaction,
445 * so no need to restart.
447 BUG_ON(why
!= RESTART_NONE
|| clusters_to_add
);
454 static int __ocfs2_remove_xattr_range(struct inode
*inode
,
455 struct buffer_head
*root_bh
,
456 struct ocfs2_xattr_value_root
*xv
,
457 u32 cpos
, u32 phys_cpos
, u32 len
,
458 struct ocfs2_xattr_set_ctxt
*ctxt
)
461 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
462 handle_t
*handle
= ctxt
->handle
;
463 struct ocfs2_extent_tree et
;
465 ocfs2_init_xattr_value_extent_tree(&et
, inode
, root_bh
, xv
);
467 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
468 OCFS2_JOURNAL_ACCESS_WRITE
);
474 ret
= ocfs2_remove_extent(inode
, &et
, cpos
, len
, handle
, ctxt
->meta_ac
,
481 le32_add_cpu(&xv
->xr_clusters
, -len
);
483 ret
= ocfs2_journal_dirty(handle
, root_bh
);
489 ret
= ocfs2_cache_cluster_dealloc(&ctxt
->dealloc
, phys_blkno
, len
);
497 static int ocfs2_xattr_shrink_size(struct inode
*inode
,
500 struct buffer_head
*root_bh
,
501 struct ocfs2_xattr_value_root
*xv
,
502 struct ocfs2_xattr_set_ctxt
*ctxt
)
505 u32 trunc_len
, cpos
, phys_cpos
, alloc_size
;
508 if (old_clusters
<= new_clusters
)
512 trunc_len
= old_clusters
- new_clusters
;
514 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &phys_cpos
,
515 &alloc_size
, &xv
->xr_list
);
521 if (alloc_size
> trunc_len
)
522 alloc_size
= trunc_len
;
524 ret
= __ocfs2_remove_xattr_range(inode
, root_bh
, xv
, cpos
,
525 phys_cpos
, alloc_size
,
532 block
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
533 ocfs2_remove_xattr_clusters_from_cache(inode
, block
,
536 trunc_len
-= alloc_size
;
543 static int ocfs2_xattr_value_truncate(struct inode
*inode
,
544 struct buffer_head
*root_bh
,
545 struct ocfs2_xattr_value_root
*xv
,
547 struct ocfs2_xattr_set_ctxt
*ctxt
)
550 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, len
);
551 u32 old_clusters
= le32_to_cpu(xv
->xr_clusters
);
553 if (new_clusters
== old_clusters
)
556 if (new_clusters
> old_clusters
)
557 ret
= ocfs2_xattr_extend_allocation(inode
,
558 new_clusters
- old_clusters
,
561 ret
= ocfs2_xattr_shrink_size(inode
,
562 old_clusters
, new_clusters
,
568 static int ocfs2_xattr_list_entry(char *buffer
, size_t size
,
569 size_t *result
, const char *prefix
,
570 const char *name
, int name_len
)
572 char *p
= buffer
+ *result
;
573 int prefix_len
= strlen(prefix
);
574 int total_len
= prefix_len
+ name_len
+ 1;
576 *result
+= total_len
;
578 /* we are just looking for how big our buffer needs to be */
585 memcpy(p
, prefix
, prefix_len
);
586 memcpy(p
+ prefix_len
, name
, name_len
);
587 p
[prefix_len
+ name_len
] = '\0';
592 static int ocfs2_xattr_list_entries(struct inode
*inode
,
593 struct ocfs2_xattr_header
*header
,
594 char *buffer
, size_t buffer_size
)
598 const char *prefix
, *name
;
600 for (i
= 0 ; i
< le16_to_cpu(header
->xh_count
); i
++) {
601 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
602 type
= ocfs2_xattr_get_type(entry
);
603 prefix
= ocfs2_xattr_prefix(type
);
606 name
= (const char *)header
+
607 le16_to_cpu(entry
->xe_name_offset
);
609 ret
= ocfs2_xattr_list_entry(buffer
, buffer_size
,
610 &result
, prefix
, name
,
620 static int ocfs2_xattr_ibody_list(struct inode
*inode
,
621 struct ocfs2_dinode
*di
,
625 struct ocfs2_xattr_header
*header
= NULL
;
626 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
629 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
632 header
= (struct ocfs2_xattr_header
*)
633 ((void *)di
+ inode
->i_sb
->s_blocksize
-
634 le16_to_cpu(di
->i_xattr_inline_size
));
636 ret
= ocfs2_xattr_list_entries(inode
, header
, buffer
, buffer_size
);
641 static int ocfs2_xattr_block_list(struct inode
*inode
,
642 struct ocfs2_dinode
*di
,
646 struct buffer_head
*blk_bh
= NULL
;
647 struct ocfs2_xattr_block
*xb
;
650 if (!di
->i_xattr_loc
)
653 ret
= ocfs2_read_block(inode
, le64_to_cpu(di
->i_xattr_loc
), &blk_bh
);
659 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
660 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
665 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
666 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
667 ret
= ocfs2_xattr_list_entries(inode
, header
,
668 buffer
, buffer_size
);
670 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
671 ret
= ocfs2_xattr_tree_list_index_block(inode
, xt
,
672 buffer
, buffer_size
);
680 ssize_t
ocfs2_listxattr(struct dentry
*dentry
,
684 int ret
= 0, i_ret
= 0, b_ret
= 0;
685 struct buffer_head
*di_bh
= NULL
;
686 struct ocfs2_dinode
*di
= NULL
;
687 struct ocfs2_inode_info
*oi
= OCFS2_I(dentry
->d_inode
);
689 if (!ocfs2_supports_xattr(OCFS2_SB(dentry
->d_sb
)))
692 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
695 ret
= ocfs2_inode_lock(dentry
->d_inode
, &di_bh
, 0);
701 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
703 down_read(&oi
->ip_xattr_sem
);
704 i_ret
= ocfs2_xattr_ibody_list(dentry
->d_inode
, di
, buffer
, size
);
712 b_ret
= ocfs2_xattr_block_list(dentry
->d_inode
, di
,
717 up_read(&oi
->ip_xattr_sem
);
718 ocfs2_inode_unlock(dentry
->d_inode
, 0);
722 return i_ret
+ b_ret
;
725 static int ocfs2_xattr_find_entry(int name_index
,
727 struct ocfs2_xattr_search
*xs
)
729 struct ocfs2_xattr_entry
*entry
;
736 name_len
= strlen(name
);
738 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
739 cmp
= name_index
- ocfs2_xattr_get_type(entry
);
741 cmp
= name_len
- entry
->xe_name_len
;
743 cmp
= memcmp(name
, (xs
->base
+
744 le16_to_cpu(entry
->xe_name_offset
)),
752 return cmp
? -ENODATA
: 0;
755 static int ocfs2_xattr_get_value_outside(struct inode
*inode
,
756 struct ocfs2_xattr_value_root
*xv
,
760 u32 cpos
, p_cluster
, num_clusters
, bpc
, clusters
;
763 size_t cplen
, blocksize
;
764 struct buffer_head
*bh
= NULL
;
765 struct ocfs2_extent_list
*el
;
768 clusters
= le32_to_cpu(xv
->xr_clusters
);
769 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
770 blocksize
= inode
->i_sb
->s_blocksize
;
773 while (cpos
< clusters
) {
774 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
781 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
782 /* Copy ocfs2_xattr_value */
783 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
784 ret
= ocfs2_read_block(inode
, blkno
, &bh
);
790 cplen
= len
>= blocksize
? blocksize
: len
;
791 memcpy(buffer
, bh
->b_data
, cplen
);
800 cpos
+= num_clusters
;
806 static int ocfs2_xattr_ibody_get(struct inode
*inode
,
811 struct ocfs2_xattr_search
*xs
)
813 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
814 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
815 struct ocfs2_xattr_value_root
*xv
;
819 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
822 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
823 xs
->header
= (struct ocfs2_xattr_header
*)
824 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
825 xs
->base
= (void *)xs
->header
;
826 xs
->here
= xs
->header
->xh_entries
;
828 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
831 size
= le64_to_cpu(xs
->here
->xe_value_size
);
833 if (size
> buffer_size
)
835 if (ocfs2_xattr_is_local(xs
->here
)) {
836 memcpy(buffer
, (void *)xs
->base
+
837 le16_to_cpu(xs
->here
->xe_name_offset
) +
838 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
), size
);
840 xv
= (struct ocfs2_xattr_value_root
*)
841 (xs
->base
+ le16_to_cpu(
842 xs
->here
->xe_name_offset
) +
843 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
));
844 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
856 static int ocfs2_xattr_block_get(struct inode
*inode
,
861 struct ocfs2_xattr_search
*xs
)
863 struct ocfs2_xattr_block
*xb
;
864 struct ocfs2_xattr_value_root
*xv
;
866 int ret
= -ENODATA
, name_offset
, name_len
, block_off
, i
;
868 xs
->bucket
= ocfs2_xattr_bucket_new(inode
);
875 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, xs
);
886 xb
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
887 size
= le64_to_cpu(xs
->here
->xe_value_size
);
890 if (size
> buffer_size
)
893 name_offset
= le16_to_cpu(xs
->here
->xe_name_offset
);
894 name_len
= OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
);
895 i
= xs
->here
- xs
->header
->xh_entries
;
897 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
898 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
899 bucket_xh(xs
->bucket
),
903 xs
->base
= bucket_block(xs
->bucket
, block_off
);
905 if (ocfs2_xattr_is_local(xs
->here
)) {
906 memcpy(buffer
, (void *)xs
->base
+
907 name_offset
+ name_len
, size
);
909 xv
= (struct ocfs2_xattr_value_root
*)
910 (xs
->base
+ name_offset
+ name_len
);
911 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
921 ocfs2_xattr_bucket_free(xs
->bucket
);
923 brelse(xs
->xattr_bh
);
928 int ocfs2_xattr_get_nolock(struct inode
*inode
,
929 struct buffer_head
*di_bh
,
936 struct ocfs2_dinode
*di
= NULL
;
937 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
938 struct ocfs2_xattr_search xis
= {
939 .not_found
= -ENODATA
,
941 struct ocfs2_xattr_search xbs
= {
942 .not_found
= -ENODATA
,
945 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
948 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
951 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
952 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
954 down_read(&oi
->ip_xattr_sem
);
955 ret
= ocfs2_xattr_ibody_get(inode
, name_index
, name
, buffer
,
957 if (ret
== -ENODATA
&& di
->i_xattr_loc
)
958 ret
= ocfs2_xattr_block_get(inode
, name_index
, name
, buffer
,
960 up_read(&oi
->ip_xattr_sem
);
967 * Copy an extended attribute into the buffer provided.
968 * Buffer is NULL to compute the size of buffer required.
970 static int ocfs2_xattr_get(struct inode
*inode
,
977 struct buffer_head
*di_bh
= NULL
;
979 ret
= ocfs2_inode_lock(inode
, &di_bh
, 0);
984 ret
= ocfs2_xattr_get_nolock(inode
, di_bh
, name_index
,
985 name
, buffer
, buffer_size
);
987 ocfs2_inode_unlock(inode
, 0);
994 static int __ocfs2_xattr_set_value_outside(struct inode
*inode
,
996 struct ocfs2_xattr_value_root
*xv
,
1000 int ret
= 0, i
, cp_len
, credits
;
1001 u16 blocksize
= inode
->i_sb
->s_blocksize
;
1002 u32 p_cluster
, num_clusters
;
1003 u32 cpos
= 0, bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
1004 u32 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, value_len
);
1006 struct buffer_head
*bh
= NULL
;
1008 BUG_ON(clusters
> le32_to_cpu(xv
->xr_clusters
));
1011 * In __ocfs2_xattr_set_value_outside has already been dirtied,
1012 * so we don't need to worry about whether ocfs2_extend_trans
1013 * will create a new transactio for us or not.
1015 credits
= clusters
* bpc
;
1016 ret
= ocfs2_extend_trans(handle
, credits
);
1022 while (cpos
< clusters
) {
1023 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
1024 &num_clusters
, &xv
->xr_list
);
1030 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
1032 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
1033 ret
= ocfs2_read_block(inode
, blkno
, &bh
);
1039 ret
= ocfs2_journal_access(handle
,
1042 OCFS2_JOURNAL_ACCESS_WRITE
);
1048 cp_len
= value_len
> blocksize
? blocksize
: value_len
;
1049 memcpy(bh
->b_data
, value
, cp_len
);
1050 value_len
-= cp_len
;
1052 if (cp_len
< blocksize
)
1053 memset(bh
->b_data
+ cp_len
, 0,
1054 blocksize
- cp_len
);
1056 ret
= ocfs2_journal_dirty(handle
, bh
);
1065 * XXX: do we need to empty all the following
1066 * blocks in this cluster?
1071 cpos
+= num_clusters
;
1079 static int ocfs2_xattr_cleanup(struct inode
*inode
,
1081 struct ocfs2_xattr_info
*xi
,
1082 struct ocfs2_xattr_search
*xs
,
1086 size_t name_len
= strlen(xi
->name
);
1087 void *val
= xs
->base
+ offs
;
1088 size_t size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1090 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1091 OCFS2_JOURNAL_ACCESS_WRITE
);
1096 /* Decrease xattr count */
1097 le16_add_cpu(&xs
->header
->xh_count
, -1);
1098 /* Remove the xattr entry and tree root which has already be set*/
1099 memset((void *)xs
->here
, 0, sizeof(struct ocfs2_xattr_entry
));
1100 memset(val
, 0, size
);
1102 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1109 static int ocfs2_xattr_update_entry(struct inode
*inode
,
1111 struct ocfs2_xattr_info
*xi
,
1112 struct ocfs2_xattr_search
*xs
,
1117 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1118 OCFS2_JOURNAL_ACCESS_WRITE
);
1124 xs
->here
->xe_name_offset
= cpu_to_le16(offs
);
1125 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1126 if (xi
->value_len
<= OCFS2_XATTR_INLINE_SIZE
)
1127 ocfs2_xattr_set_local(xs
->here
, 1);
1129 ocfs2_xattr_set_local(xs
->here
, 0);
1130 ocfs2_xattr_hash_entry(inode
, xs
->header
, xs
->here
);
1132 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1140 * ocfs2_xattr_set_value_outside()
1142 * Set large size value in B tree.
1144 static int ocfs2_xattr_set_value_outside(struct inode
*inode
,
1145 struct ocfs2_xattr_info
*xi
,
1146 struct ocfs2_xattr_search
*xs
,
1147 struct ocfs2_xattr_set_ctxt
*ctxt
,
1150 size_t name_len
= strlen(xi
->name
);
1151 void *val
= xs
->base
+ offs
;
1152 struct ocfs2_xattr_value_root
*xv
= NULL
;
1153 size_t size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1156 memset(val
, 0, size
);
1157 memcpy(val
, xi
->name
, name_len
);
1158 xv
= (struct ocfs2_xattr_value_root
*)
1159 (val
+ OCFS2_XATTR_SIZE(name_len
));
1160 xv
->xr_clusters
= 0;
1161 xv
->xr_last_eb_blk
= 0;
1162 xv
->xr_list
.l_tree_depth
= 0;
1163 xv
->xr_list
.l_count
= cpu_to_le16(1);
1164 xv
->xr_list
.l_next_free_rec
= 0;
1166 ret
= ocfs2_xattr_value_truncate(inode
, xs
->xattr_bh
, xv
,
1167 xi
->value_len
, ctxt
);
1172 ret
= ocfs2_xattr_update_entry(inode
, ctxt
->handle
, xi
, xs
, offs
);
1177 ret
= __ocfs2_xattr_set_value_outside(inode
, ctxt
->handle
, xv
,
1178 xi
->value
, xi
->value_len
);
1186 * ocfs2_xattr_set_entry_local()
1188 * Set, replace or remove extended attribute in local.
1190 static void ocfs2_xattr_set_entry_local(struct inode
*inode
,
1191 struct ocfs2_xattr_info
*xi
,
1192 struct ocfs2_xattr_search
*xs
,
1193 struct ocfs2_xattr_entry
*last
,
1196 size_t name_len
= strlen(xi
->name
);
1199 if (xi
->value
&& xs
->not_found
) {
1200 /* Insert the new xattr entry. */
1201 le16_add_cpu(&xs
->header
->xh_count
, 1);
1202 ocfs2_xattr_set_type(last
, xi
->name_index
);
1203 ocfs2_xattr_set_local(last
, 1);
1204 last
->xe_name_len
= name_len
;
1210 first_val
= xs
->base
+ min_offs
;
1211 offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1212 val
= xs
->base
+ offs
;
1214 if (le64_to_cpu(xs
->here
->xe_value_size
) >
1215 OCFS2_XATTR_INLINE_SIZE
)
1216 size
= OCFS2_XATTR_SIZE(name_len
) +
1217 OCFS2_XATTR_ROOT_SIZE
;
1219 size
= OCFS2_XATTR_SIZE(name_len
) +
1220 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1222 if (xi
->value
&& size
== OCFS2_XATTR_SIZE(name_len
) +
1223 OCFS2_XATTR_SIZE(xi
->value_len
)) {
1224 /* The old and the new value have the
1225 same size. Just replace the value. */
1226 ocfs2_xattr_set_local(xs
->here
, 1);
1227 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1228 /* Clear value bytes. */
1229 memset(val
+ OCFS2_XATTR_SIZE(name_len
),
1231 OCFS2_XATTR_SIZE(xi
->value_len
));
1232 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
1237 /* Remove the old name+value. */
1238 memmove(first_val
+ size
, first_val
, val
- first_val
);
1239 memset(first_val
, 0, size
);
1240 xs
->here
->xe_name_hash
= 0;
1241 xs
->here
->xe_name_offset
= 0;
1242 ocfs2_xattr_set_local(xs
->here
, 1);
1243 xs
->here
->xe_value_size
= 0;
1247 /* Adjust all value offsets. */
1248 last
= xs
->header
->xh_entries
;
1249 for (i
= 0 ; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1250 size_t o
= le16_to_cpu(last
->xe_name_offset
);
1253 last
->xe_name_offset
= cpu_to_le16(o
+ size
);
1258 /* Remove the old entry. */
1260 memmove(xs
->here
, xs
->here
+ 1,
1261 (void *)last
- (void *)xs
->here
);
1262 memset(last
, 0, sizeof(struct ocfs2_xattr_entry
));
1263 le16_add_cpu(&xs
->header
->xh_count
, -1);
1267 /* Insert the new name+value. */
1268 size_t size
= OCFS2_XATTR_SIZE(name_len
) +
1269 OCFS2_XATTR_SIZE(xi
->value_len
);
1270 void *val
= xs
->base
+ min_offs
- size
;
1272 xs
->here
->xe_name_offset
= cpu_to_le16(min_offs
- size
);
1273 memset(val
, 0, size
);
1274 memcpy(val
, xi
->name
, name_len
);
1275 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
1278 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1279 ocfs2_xattr_set_local(xs
->here
, 1);
1280 ocfs2_xattr_hash_entry(inode
, xs
->header
, xs
->here
);
1287 * ocfs2_xattr_set_entry()
1289 * Set extended attribute entry into inode or block.
1291 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1292 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1293 * then set value in B tree with set_value_outside().
1295 static int ocfs2_xattr_set_entry(struct inode
*inode
,
1296 struct ocfs2_xattr_info
*xi
,
1297 struct ocfs2_xattr_search
*xs
,
1298 struct ocfs2_xattr_set_ctxt
*ctxt
,
1301 struct ocfs2_xattr_entry
*last
;
1302 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1303 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1304 size_t min_offs
= xs
->end
- xs
->base
, name_len
= strlen(xi
->name
);
1306 handle_t
*handle
= ctxt
->handle
;
1308 struct ocfs2_xattr_info xi_l
= {
1309 .name_index
= xi
->name_index
,
1312 .value_len
= xi
->value_len
,
1315 /* Compute min_offs, last and free space. */
1316 last
= xs
->header
->xh_entries
;
1318 for (i
= 0 ; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1319 size_t offs
= le16_to_cpu(last
->xe_name_offset
);
1320 if (offs
< min_offs
)
1325 free
= min_offs
- ((void *)last
- xs
->base
) - sizeof(__u32
);
1329 if (!xs
->not_found
) {
1331 if (ocfs2_xattr_is_local(xs
->here
))
1332 size
= OCFS2_XATTR_SIZE(name_len
) +
1333 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1335 size
= OCFS2_XATTR_SIZE(name_len
) +
1336 OCFS2_XATTR_ROOT_SIZE
;
1337 free
+= (size
+ sizeof(struct ocfs2_xattr_entry
));
1339 /* Check free space in inode or block */
1340 if (xi
->value
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1341 if (free
< sizeof(struct ocfs2_xattr_entry
) +
1342 OCFS2_XATTR_SIZE(name_len
) +
1343 OCFS2_XATTR_ROOT_SIZE
) {
1347 size_l
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1348 xi_l
.value
= (void *)&def_xv
;
1349 xi_l
.value_len
= OCFS2_XATTR_ROOT_SIZE
;
1350 } else if (xi
->value
) {
1351 if (free
< sizeof(struct ocfs2_xattr_entry
) +
1352 OCFS2_XATTR_SIZE(name_len
) +
1353 OCFS2_XATTR_SIZE(xi
->value_len
)) {
1359 if (!xs
->not_found
) {
1360 /* For existing extended attribute */
1361 size_t size
= OCFS2_XATTR_SIZE(name_len
) +
1362 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1363 size_t offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1364 void *val
= xs
->base
+ offs
;
1366 if (ocfs2_xattr_is_local(xs
->here
) && size
== size_l
) {
1367 /* Replace existing local xattr with tree root */
1368 ret
= ocfs2_xattr_set_value_outside(inode
, xi
, xs
,
1373 } else if (!ocfs2_xattr_is_local(xs
->here
)) {
1374 /* For existing xattr which has value outside */
1375 struct ocfs2_xattr_value_root
*xv
= NULL
;
1376 xv
= (struct ocfs2_xattr_value_root
*)(val
+
1377 OCFS2_XATTR_SIZE(name_len
));
1379 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1381 * If new value need set outside also,
1382 * first truncate old value to new value,
1383 * then set new value with set_value_outside().
1385 ret
= ocfs2_xattr_value_truncate(inode
,
1395 ret
= ocfs2_xattr_update_entry(inode
,
1405 ret
= __ocfs2_xattr_set_value_outside(inode
,
1415 * If new value need set in local,
1416 * just trucate old value to zero.
1418 ret
= ocfs2_xattr_value_truncate(inode
,
1429 ret
= ocfs2_journal_access(handle
, inode
, xs
->inode_bh
,
1430 OCFS2_JOURNAL_ACCESS_WRITE
);
1436 if (!(flag
& OCFS2_INLINE_XATTR_FL
)) {
1437 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1438 OCFS2_JOURNAL_ACCESS_WRITE
);
1446 * Set value in local, include set tree root in local.
1447 * This is the first step for value size >INLINE_SIZE.
1449 ocfs2_xattr_set_entry_local(inode
, &xi_l
, xs
, last
, min_offs
);
1451 if (!(flag
& OCFS2_INLINE_XATTR_FL
)) {
1452 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1459 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) &&
1460 (flag
& OCFS2_INLINE_XATTR_FL
)) {
1461 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1462 unsigned int xattrsize
= osb
->s_xattr_inline_size
;
1465 * Adjust extent record count or inline data size
1466 * to reserve space for extended attribute.
1468 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1469 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
1470 le16_add_cpu(&idata
->id_count
, -xattrsize
);
1471 } else if (!(ocfs2_inode_is_fast_symlink(inode
))) {
1472 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
1473 le16_add_cpu(&el
->l_count
, -(xattrsize
/
1474 sizeof(struct ocfs2_extent_rec
)));
1476 di
->i_xattr_inline_size
= cpu_to_le16(xattrsize
);
1478 /* Update xattr flag */
1479 spin_lock(&oi
->ip_lock
);
1480 oi
->ip_dyn_features
|= flag
;
1481 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1482 spin_unlock(&oi
->ip_lock
);
1483 /* Update inode ctime */
1484 inode
->i_ctime
= CURRENT_TIME
;
1485 di
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
1486 di
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
1488 ret
= ocfs2_journal_dirty(handle
, xs
->inode_bh
);
1492 if (!ret
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1494 * Set value outside in B tree.
1495 * This is the second step for value size > INLINE_SIZE.
1497 size_t offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1498 ret
= ocfs2_xattr_set_value_outside(inode
, xi
, xs
, ctxt
, offs
);
1504 * If set value outside failed, we have to clean
1505 * the junk tree root we have already set in local.
1507 ret2
= ocfs2_xattr_cleanup(inode
, ctxt
->handle
,
1517 static int ocfs2_remove_value_outside(struct inode
*inode
,
1518 struct buffer_head
*bh
,
1519 struct ocfs2_xattr_header
*header
)
1522 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1523 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, };
1525 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
1527 ctxt
.handle
= ocfs2_start_trans(osb
, OCFS2_REMOVE_EXTENT_CREDITS
);
1528 if (IS_ERR(ctxt
.handle
)) {
1529 ret
= PTR_ERR(ctxt
.handle
);
1534 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
1535 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
1537 if (!ocfs2_xattr_is_local(entry
)) {
1538 struct ocfs2_xattr_value_root
*xv
;
1541 val
= (void *)header
+
1542 le16_to_cpu(entry
->xe_name_offset
);
1543 xv
= (struct ocfs2_xattr_value_root
*)
1544 (val
+ OCFS2_XATTR_SIZE(entry
->xe_name_len
));
1545 ret
= ocfs2_xattr_value_truncate(inode
, bh
, xv
,
1554 ocfs2_commit_trans(osb
, ctxt
.handle
);
1555 ocfs2_schedule_truncate_log_flush(osb
, 1);
1556 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
1561 static int ocfs2_xattr_ibody_remove(struct inode
*inode
,
1562 struct buffer_head
*di_bh
)
1565 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1566 struct ocfs2_xattr_header
*header
;
1569 header
= (struct ocfs2_xattr_header
*)
1570 ((void *)di
+ inode
->i_sb
->s_blocksize
-
1571 le16_to_cpu(di
->i_xattr_inline_size
));
1573 ret
= ocfs2_remove_value_outside(inode
, di_bh
, header
);
1578 static int ocfs2_xattr_block_remove(struct inode
*inode
,
1579 struct buffer_head
*blk_bh
)
1581 struct ocfs2_xattr_block
*xb
;
1584 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1585 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1586 struct ocfs2_xattr_header
*header
= &(xb
->xb_attrs
.xb_header
);
1587 ret
= ocfs2_remove_value_outside(inode
, blk_bh
, header
);
1589 ret
= ocfs2_delete_xattr_index_block(inode
, blk_bh
);
1594 static int ocfs2_xattr_free_block(struct inode
*inode
,
1597 struct inode
*xb_alloc_inode
;
1598 struct buffer_head
*xb_alloc_bh
= NULL
;
1599 struct buffer_head
*blk_bh
= NULL
;
1600 struct ocfs2_xattr_block
*xb
;
1601 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1607 ret
= ocfs2_read_block(inode
, block
, &blk_bh
);
1613 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1614 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
1619 ret
= ocfs2_xattr_block_remove(inode
, blk_bh
);
1625 blk
= le64_to_cpu(xb
->xb_blkno
);
1626 bit
= le16_to_cpu(xb
->xb_suballoc_bit
);
1627 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1629 xb_alloc_inode
= ocfs2_get_system_file_inode(osb
,
1630 EXTENT_ALLOC_SYSTEM_INODE
,
1631 le16_to_cpu(xb
->xb_suballoc_slot
));
1632 if (!xb_alloc_inode
) {
1637 mutex_lock(&xb_alloc_inode
->i_mutex
);
1639 ret
= ocfs2_inode_lock(xb_alloc_inode
, &xb_alloc_bh
, 1);
1645 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
1646 if (IS_ERR(handle
)) {
1647 ret
= PTR_ERR(handle
);
1652 ret
= ocfs2_free_suballoc_bits(handle
, xb_alloc_inode
, xb_alloc_bh
,
1657 ocfs2_commit_trans(osb
, handle
);
1659 ocfs2_inode_unlock(xb_alloc_inode
, 1);
1660 brelse(xb_alloc_bh
);
1662 mutex_unlock(&xb_alloc_inode
->i_mutex
);
1663 iput(xb_alloc_inode
);
1670 * ocfs2_xattr_remove()
1672 * Free extended attribute resources associated with this inode.
1674 int ocfs2_xattr_remove(struct inode
*inode
, struct buffer_head
*di_bh
)
1676 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1677 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1681 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
1684 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1687 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
1688 ret
= ocfs2_xattr_ibody_remove(inode
, di_bh
);
1695 if (di
->i_xattr_loc
) {
1696 ret
= ocfs2_xattr_free_block(inode
,
1697 le64_to_cpu(di
->i_xattr_loc
));
1704 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1705 OCFS2_INODE_UPDATE_CREDITS
);
1706 if (IS_ERR(handle
)) {
1707 ret
= PTR_ERR(handle
);
1711 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
1712 OCFS2_JOURNAL_ACCESS_WRITE
);
1718 di
->i_xattr_loc
= 0;
1720 spin_lock(&oi
->ip_lock
);
1721 oi
->ip_dyn_features
&= ~(OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
);
1722 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1723 spin_unlock(&oi
->ip_lock
);
1725 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1729 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1734 static int ocfs2_xattr_has_space_inline(struct inode
*inode
,
1735 struct ocfs2_dinode
*di
)
1737 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1738 unsigned int xattrsize
= OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
;
1741 if (xattrsize
< OCFS2_MIN_XATTR_INLINE_SIZE
)
1744 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1745 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
1746 free
= le16_to_cpu(idata
->id_count
) - le64_to_cpu(di
->i_size
);
1747 } else if (ocfs2_inode_is_fast_symlink(inode
)) {
1748 free
= ocfs2_fast_symlink_chars(inode
->i_sb
) -
1749 le64_to_cpu(di
->i_size
);
1751 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
1752 free
= (le16_to_cpu(el
->l_count
) -
1753 le16_to_cpu(el
->l_next_free_rec
)) *
1754 sizeof(struct ocfs2_extent_rec
);
1756 if (free
>= xattrsize
)
1763 * ocfs2_xattr_ibody_find()
1765 * Find extended attribute in inode block and
1766 * fill search info into struct ocfs2_xattr_search.
1768 static int ocfs2_xattr_ibody_find(struct inode
*inode
,
1771 struct ocfs2_xattr_search
*xs
)
1773 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1774 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1778 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
1781 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
1782 down_read(&oi
->ip_alloc_sem
);
1783 has_space
= ocfs2_xattr_has_space_inline(inode
, di
);
1784 up_read(&oi
->ip_alloc_sem
);
1789 xs
->xattr_bh
= xs
->inode_bh
;
1790 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
1791 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)
1792 xs
->header
= (struct ocfs2_xattr_header
*)
1793 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
1795 xs
->header
= (struct ocfs2_xattr_header
*)
1796 (xs
->end
- OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
);
1797 xs
->base
= (void *)xs
->header
;
1798 xs
->here
= xs
->header
->xh_entries
;
1800 /* Find the named attribute. */
1801 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
1802 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1803 if (ret
&& ret
!= -ENODATA
)
1805 xs
->not_found
= ret
;
1812 * ocfs2_xattr_ibody_set()
1814 * Set, replace or remove an extended attribute into inode block.
1817 static int ocfs2_xattr_ibody_set(struct inode
*inode
,
1818 struct ocfs2_xattr_info
*xi
,
1819 struct ocfs2_xattr_search
*xs
,
1820 struct ocfs2_xattr_set_ctxt
*ctxt
)
1822 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1823 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1826 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
1829 down_write(&oi
->ip_alloc_sem
);
1830 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
1831 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
1837 ret
= ocfs2_xattr_set_entry(inode
, xi
, xs
, ctxt
,
1838 (OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
));
1840 up_write(&oi
->ip_alloc_sem
);
1846 * ocfs2_xattr_block_find()
1848 * Find extended attribute in external block and
1849 * fill search info into struct ocfs2_xattr_search.
1851 static int ocfs2_xattr_block_find(struct inode
*inode
,
1854 struct ocfs2_xattr_search
*xs
)
1856 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1857 struct buffer_head
*blk_bh
= NULL
;
1858 struct ocfs2_xattr_block
*xb
;
1861 if (!di
->i_xattr_loc
)
1864 ret
= ocfs2_read_block(inode
, le64_to_cpu(di
->i_xattr_loc
), &blk_bh
);
1870 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1871 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
1876 xs
->xattr_bh
= blk_bh
;
1878 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1879 xs
->header
= &xb
->xb_attrs
.xb_header
;
1880 xs
->base
= (void *)xs
->header
;
1881 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
1882 xs
->here
= xs
->header
->xh_entries
;
1884 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1886 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
1890 if (ret
&& ret
!= -ENODATA
) {
1891 xs
->xattr_bh
= NULL
;
1894 xs
->not_found
= ret
;
1903 * ocfs2_xattr_block_set()
1905 * Set, replace or remove an extended attribute into external block.
1908 static int ocfs2_xattr_block_set(struct inode
*inode
,
1909 struct ocfs2_xattr_info
*xi
,
1910 struct ocfs2_xattr_search
*xs
,
1911 struct ocfs2_xattr_set_ctxt
*ctxt
)
1913 struct buffer_head
*new_bh
= NULL
;
1914 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1915 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1916 handle_t
*handle
= ctxt
->handle
;
1917 struct ocfs2_xattr_block
*xblk
= NULL
;
1918 u16 suballoc_bit_start
;
1923 if (!xs
->xattr_bh
) {
1924 ret
= ocfs2_journal_access(handle
, inode
, xs
->inode_bh
,
1925 OCFS2_JOURNAL_ACCESS_CREATE
);
1931 ret
= ocfs2_claim_metadata(osb
, handle
, ctxt
->meta_ac
, 1,
1932 &suballoc_bit_start
, &num_got
,
1939 new_bh
= sb_getblk(inode
->i_sb
, first_blkno
);
1940 ocfs2_set_new_buffer_uptodate(inode
, new_bh
);
1942 ret
= ocfs2_journal_access(handle
, inode
, new_bh
,
1943 OCFS2_JOURNAL_ACCESS_CREATE
);
1949 /* Initialize ocfs2_xattr_block */
1950 xs
->xattr_bh
= new_bh
;
1951 xblk
= (struct ocfs2_xattr_block
*)new_bh
->b_data
;
1952 memset(xblk
, 0, inode
->i_sb
->s_blocksize
);
1953 strcpy((void *)xblk
, OCFS2_XATTR_BLOCK_SIGNATURE
);
1954 xblk
->xb_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
1955 xblk
->xb_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
1956 xblk
->xb_fs_generation
= cpu_to_le32(osb
->fs_generation
);
1957 xblk
->xb_blkno
= cpu_to_le64(first_blkno
);
1959 xs
->header
= &xblk
->xb_attrs
.xb_header
;
1960 xs
->base
= (void *)xs
->header
;
1961 xs
->end
= (void *)xblk
+ inode
->i_sb
->s_blocksize
;
1962 xs
->here
= xs
->header
->xh_entries
;
1964 ret
= ocfs2_journal_dirty(handle
, new_bh
);
1969 di
->i_xattr_loc
= cpu_to_le64(first_blkno
);
1970 ocfs2_journal_dirty(handle
, xs
->inode_bh
);
1972 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
1974 if (!(le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1975 /* Set extended attribute into external block */
1976 ret
= ocfs2_xattr_set_entry(inode
, xi
, xs
, ctxt
,
1977 OCFS2_HAS_XATTR_FL
);
1978 if (!ret
|| ret
!= -ENOSPC
)
1981 ret
= ocfs2_xattr_create_index_block(inode
, xs
, ctxt
);
1986 ret
= ocfs2_xattr_set_entry_index_block(inode
, xi
, xs
, ctxt
);
1993 /* Check whether the new xattr can be inserted into the inode. */
1994 static int ocfs2_xattr_can_be_in_inode(struct inode
*inode
,
1995 struct ocfs2_xattr_info
*xi
,
1996 struct ocfs2_xattr_search
*xs
)
1999 struct ocfs2_xattr_entry
*last
;
2001 size_t min_offs
= xs
->end
- xs
->base
;
2006 last
= xs
->header
->xh_entries
;
2008 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
2009 size_t offs
= le16_to_cpu(last
->xe_name_offset
);
2010 if (offs
< min_offs
)
2015 free
= min_offs
- ((void *)last
- xs
->base
) - sizeof(__u32
);
2019 BUG_ON(!xs
->not_found
);
2021 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
2022 value_size
= OCFS2_XATTR_ROOT_SIZE
;
2024 value_size
= OCFS2_XATTR_SIZE(xi
->value_len
);
2026 if (free
>= sizeof(struct ocfs2_xattr_entry
) +
2027 OCFS2_XATTR_SIZE(strlen(xi
->name
)) + value_size
)
2033 static int ocfs2_calc_xattr_set_need(struct inode
*inode
,
2034 struct ocfs2_dinode
*di
,
2035 struct ocfs2_xattr_info
*xi
,
2036 struct ocfs2_xattr_search
*xis
,
2037 struct ocfs2_xattr_search
*xbs
,
2042 int ret
= 0, old_in_xb
= 0;
2043 int clusters_add
= 0, meta_add
= 0, credits
= 0;
2044 struct buffer_head
*bh
= NULL
;
2045 struct ocfs2_xattr_block
*xb
= NULL
;
2046 struct ocfs2_xattr_entry
*xe
= NULL
;
2047 struct ocfs2_xattr_value_root
*xv
= NULL
;
2049 int name_offset
, name_len
= 0;
2050 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
2054 if (xis
->not_found
&& xbs
->not_found
) {
2055 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2057 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2058 clusters_add
+= new_clusters
;
2059 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
2067 if (!xis
->not_found
) {
2069 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
2070 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
2072 credits
+= OCFS2_INODE_UPDATE_CREDITS
;
2075 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
2077 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
2078 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
2079 i
= xbs
->here
- xbs
->header
->xh_entries
;
2082 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
2083 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
2084 bucket_xh(xbs
->bucket
),
2087 base
= bucket_block(xbs
->bucket
, block_off
);
2088 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2091 credits
+= OCFS2_XATTR_BLOCK_UPDATE_CREDITS
;
2096 * delete a xattr doesn't need metadata and cluster allocation.
2097 * so just calculate the credits and return.
2099 * The credits for removing the value tree will be extended
2100 * by ocfs2_remove_extent itself.
2103 if (!ocfs2_xattr_is_local(xe
))
2104 credits
+= OCFS2_REMOVE_EXTENT_CREDITS
;
2109 /* do cluster allocation guess first. */
2110 value_size
= le64_to_cpu(xe
->xe_value_size
);
2114 * In xattr set, we always try to set the xe in inode first,
2115 * so if it can be inserted into inode successfully, the old
2116 * one will be removed from the xattr block, and this xattr
2117 * will be inserted into inode as a new xattr in inode.
2119 if (ocfs2_xattr_can_be_in_inode(inode
, xi
, xis
)) {
2120 clusters_add
+= new_clusters
;
2121 credits
+= OCFS2_REMOVE_EXTENT_CREDITS
+
2122 OCFS2_INODE_UPDATE_CREDITS
;
2123 if (!ocfs2_xattr_is_local(xe
))
2124 credits
+= ocfs2_calc_extend_credits(
2132 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2133 /* the new values will be stored outside. */
2134 u32 old_clusters
= 0;
2136 if (!ocfs2_xattr_is_local(xe
)) {
2137 old_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
2139 xv
= (struct ocfs2_xattr_value_root
*)
2140 (base
+ name_offset
+ name_len
);
2144 if (old_clusters
>= new_clusters
) {
2145 credits
+= OCFS2_REMOVE_EXTENT_CREDITS
;
2148 meta_add
+= ocfs2_extend_meta_needed(&xv
->xr_list
);
2149 clusters_add
+= new_clusters
- old_clusters
;
2150 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
2158 * Now the new value will be stored inside. So if the new
2159 * value is smaller than the size of value root or the old
2160 * value, we don't need any allocation, otherwise we have
2161 * to guess metadata allocation.
2163 if ((ocfs2_xattr_is_local(xe
) && value_size
>= xi
->value_len
) ||
2164 (!ocfs2_xattr_is_local(xe
) &&
2165 OCFS2_XATTR_ROOT_SIZE
>= xi
->value_len
))
2170 /* calculate metadata allocation. */
2171 if (di
->i_xattr_loc
) {
2172 if (!xbs
->xattr_bh
) {
2173 ret
= ocfs2_read_block(inode
,
2174 le64_to_cpu(di
->i_xattr_loc
),
2181 xb
= (struct ocfs2_xattr_block
*)bh
->b_data
;
2183 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
2185 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
2186 struct ocfs2_extent_list
*el
=
2187 &xb
->xb_attrs
.xb_root
.xt_list
;
2188 meta_add
+= ocfs2_extend_meta_needed(el
);
2189 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
2194 * This cluster will be used either for new bucket or for
2196 * If the cluster size is the same as the bucket size, one
2197 * more is needed since we may need to extend the bucket
2201 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2202 if (OCFS2_XATTR_BUCKET_SIZE
==
2203 OCFS2_SB(inode
->i_sb
)->s_clustersize
) {
2204 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2209 credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
2213 *clusters_need
= clusters_add
;
2215 *meta_need
= meta_add
;
2217 *credits_need
= credits
;
2222 static int ocfs2_init_xattr_set_ctxt(struct inode
*inode
,
2223 struct ocfs2_dinode
*di
,
2224 struct ocfs2_xattr_info
*xi
,
2225 struct ocfs2_xattr_search
*xis
,
2226 struct ocfs2_xattr_search
*xbs
,
2227 struct ocfs2_xattr_set_ctxt
*ctxt
,
2230 int clusters_add
, meta_add
, ret
;
2231 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2233 memset(ctxt
, 0, sizeof(struct ocfs2_xattr_set_ctxt
));
2235 ocfs2_init_dealloc_ctxt(&ctxt
->dealloc
);
2237 ret
= ocfs2_calc_xattr_set_need(inode
, di
, xi
, xis
, xbs
,
2238 &clusters_add
, &meta_add
, credits
);
2244 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2245 "credits = %d\n", xi
->name
, meta_add
, clusters_add
, *credits
);
2248 ret
= ocfs2_reserve_new_metadata_blocks(osb
, meta_add
,
2257 ret
= ocfs2_reserve_clusters(osb
, clusters_add
, &ctxt
->data_ac
);
2263 if (ctxt
->meta_ac
) {
2264 ocfs2_free_alloc_context(ctxt
->meta_ac
);
2265 ctxt
->meta_ac
= NULL
;
2269 * We cannot have an error and a non null ctxt->data_ac.
2276 static int __ocfs2_xattr_set_handle(struct inode
*inode
,
2277 struct ocfs2_dinode
*di
,
2278 struct ocfs2_xattr_info
*xi
,
2279 struct ocfs2_xattr_search
*xis
,
2280 struct ocfs2_xattr_search
*xbs
,
2281 struct ocfs2_xattr_set_ctxt
*ctxt
)
2283 int ret
= 0, credits
;
2286 /* Remove existing extended attribute */
2287 if (!xis
->not_found
)
2288 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
2289 else if (!xbs
->not_found
)
2290 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
2292 /* We always try to set extended attribute into inode first*/
2293 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
2294 if (!ret
&& !xbs
->not_found
) {
2296 * If succeed and that extended attribute existing in
2297 * external block, then we will remove it.
2302 xis
->not_found
= -ENODATA
;
2303 ret
= ocfs2_calc_xattr_set_need(inode
,
2316 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
+
2317 ctxt
->handle
->h_buffer_credits
);
2322 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
2323 } else if (ret
== -ENOSPC
) {
2324 if (di
->i_xattr_loc
&& !xbs
->xattr_bh
) {
2325 ret
= ocfs2_xattr_block_find(inode
,
2331 xis
->not_found
= -ENODATA
;
2332 ret
= ocfs2_calc_xattr_set_need(inode
,
2345 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
+
2346 ctxt
->handle
->h_buffer_credits
);
2353 * If no space in inode, we will set extended attribute
2354 * into external block.
2356 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
2359 if (!xis
->not_found
) {
2361 * If succeed and that extended attribute
2362 * existing in inode, we will remove it.
2366 xbs
->not_found
= -ENODATA
;
2367 ret
= ocfs2_calc_xattr_set_need(inode
,
2380 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
+
2381 ctxt
->handle
->h_buffer_credits
);
2386 ret
= ocfs2_xattr_ibody_set(inode
, xi
,
2397 * This function only called duing creating inode
2398 * for init security/acl xattrs of the new inode.
2399 * The xattrs could be put into ibody or extent block,
2400 * xattr bucket would not be use in this case.
2401 * transanction credits also be reserved in here.
2403 int ocfs2_xattr_set_handle(handle_t
*handle
,
2404 struct inode
*inode
,
2405 struct buffer_head
*di_bh
,
2411 struct ocfs2_alloc_context
*meta_ac
,
2412 struct ocfs2_alloc_context
*data_ac
)
2414 struct ocfs2_dinode
*di
;
2417 struct ocfs2_xattr_info xi
= {
2418 .name_index
= name_index
,
2421 .value_len
= value_len
,
2424 struct ocfs2_xattr_search xis
= {
2425 .not_found
= -ENODATA
,
2428 struct ocfs2_xattr_search xbs
= {
2429 .not_found
= -ENODATA
,
2432 struct ocfs2_xattr_set_ctxt ctxt
= {
2438 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
2441 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
2442 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2444 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2446 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
2449 if (xis
.not_found
) {
2450 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
2455 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
2458 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2459 brelse(xbs
.xattr_bh
);
2467 * Set, replace or remove an extended attribute for this inode.
2468 * value is NULL to remove an existing extended attribute, else either
2469 * create or replace an extended attribute.
2471 int ocfs2_xattr_set(struct inode
*inode
,
2478 struct buffer_head
*di_bh
= NULL
;
2479 struct ocfs2_dinode
*di
;
2481 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2482 struct inode
*tl_inode
= osb
->osb_tl_inode
;
2483 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, };
2485 struct ocfs2_xattr_info xi
= {
2486 .name_index
= name_index
,
2489 .value_len
= value_len
,
2492 struct ocfs2_xattr_search xis
= {
2493 .not_found
= -ENODATA
,
2496 struct ocfs2_xattr_search xbs
= {
2497 .not_found
= -ENODATA
,
2500 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
2504 * Only xbs will be used on indexed trees. xis doesn't need a
2507 xbs
.bucket
= ocfs2_xattr_bucket_new(inode
);
2509 mlog_errno(-ENOMEM
);
2513 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
2516 goto cleanup_nolock
;
2518 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
2519 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2521 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2523 * Scan inode and external block to find the same name
2524 * extended attribute and collect search infomation.
2526 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
2529 if (xis
.not_found
) {
2530 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
2535 if (xis
.not_found
&& xbs
.not_found
) {
2537 if (flags
& XATTR_REPLACE
)
2544 if (flags
& XATTR_CREATE
)
2549 mutex_lock(&tl_inode
->i_mutex
);
2551 if (ocfs2_truncate_log_needs_flush(osb
)) {
2552 ret
= __ocfs2_flush_truncate_log(osb
);
2554 mutex_unlock(&tl_inode
->i_mutex
);
2559 mutex_unlock(&tl_inode
->i_mutex
);
2561 ret
= ocfs2_init_xattr_set_ctxt(inode
, di
, &xi
, &xis
,
2562 &xbs
, &ctxt
, &credits
);
2568 ctxt
.handle
= ocfs2_start_trans(osb
, credits
);
2569 if (IS_ERR(ctxt
.handle
)) {
2570 ret
= PTR_ERR(ctxt
.handle
);
2575 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
2577 ocfs2_commit_trans(osb
, ctxt
.handle
);
2580 ocfs2_free_alloc_context(ctxt
.data_ac
);
2582 ocfs2_free_alloc_context(ctxt
.meta_ac
);
2583 if (ocfs2_dealloc_has_cluster(&ctxt
.dealloc
))
2584 ocfs2_schedule_truncate_log_flush(osb
, 1);
2585 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
2587 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2588 ocfs2_inode_unlock(inode
, 1);
2591 brelse(xbs
.xattr_bh
);
2592 ocfs2_xattr_bucket_free(xbs
.bucket
);
2598 * Find the xattr extent rec which may contains name_hash.
2599 * e_cpos will be the first name hash of the xattr rec.
2600 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2602 static int ocfs2_xattr_get_rec(struct inode
*inode
,
2607 struct ocfs2_extent_list
*el
)
2610 struct buffer_head
*eb_bh
= NULL
;
2611 struct ocfs2_extent_block
*eb
;
2612 struct ocfs2_extent_rec
*rec
= NULL
;
2615 if (el
->l_tree_depth
) {
2616 ret
= ocfs2_find_leaf(inode
, el
, name_hash
, &eb_bh
);
2622 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
2625 if (el
->l_tree_depth
) {
2626 ocfs2_error(inode
->i_sb
,
2627 "Inode %lu has non zero tree depth in "
2628 "xattr tree block %llu\n", inode
->i_ino
,
2629 (unsigned long long)eb_bh
->b_blocknr
);
2635 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
2636 rec
= &el
->l_recs
[i
];
2638 if (le32_to_cpu(rec
->e_cpos
) <= name_hash
) {
2639 e_blkno
= le64_to_cpu(rec
->e_blkno
);
2645 ocfs2_error(inode
->i_sb
, "Inode %lu has bad extent "
2646 "record (%u, %u, 0) in xattr", inode
->i_ino
,
2647 le32_to_cpu(rec
->e_cpos
),
2648 ocfs2_rec_clusters(el
, rec
));
2653 *p_blkno
= le64_to_cpu(rec
->e_blkno
);
2654 *num_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
2656 *e_cpos
= le32_to_cpu(rec
->e_cpos
);
2662 typedef int (xattr_bucket_func
)(struct inode
*inode
,
2663 struct ocfs2_xattr_bucket
*bucket
,
2666 static int ocfs2_find_xe_in_bucket(struct inode
*inode
,
2667 struct ocfs2_xattr_bucket
*bucket
,
2674 int i
, ret
= 0, cmp
= 1, block_off
, new_offset
;
2675 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
2676 size_t name_len
= strlen(name
);
2677 struct ocfs2_xattr_entry
*xe
= NULL
;
2681 * We don't use binary search in the bucket because there
2682 * may be multiple entries with the same name hash.
2684 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
2685 xe
= &xh
->xh_entries
[i
];
2687 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
))
2689 else if (name_hash
< le32_to_cpu(xe
->xe_name_hash
))
2692 cmp
= name_index
- ocfs2_xattr_get_type(xe
);
2694 cmp
= name_len
- xe
->xe_name_len
;
2698 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
2708 xe_name
= bucket_block(bucket
, block_off
) + new_offset
;
2709 if (!memcmp(name
, xe_name
, name_len
)) {
2721 * Find the specified xattr entry in a series of buckets.
2722 * This series start from p_blkno and last for num_clusters.
2723 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2724 * the num of the valid buckets.
2726 * Return the buffer_head this xattr should reside in. And if the xattr's
2727 * hash is in the gap of 2 buckets, return the lower bucket.
2729 static int ocfs2_xattr_bucket_find(struct inode
*inode
,
2736 struct ocfs2_xattr_search
*xs
)
2739 struct ocfs2_xattr_header
*xh
= NULL
;
2740 struct ocfs2_xattr_entry
*xe
= NULL
;
2742 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2743 int low_bucket
= 0, bucket
, high_bucket
;
2744 struct ocfs2_xattr_bucket
*search
;
2746 u64 blkno
, lower_blkno
= 0;
2748 search
= ocfs2_xattr_bucket_new(inode
);
2755 ret
= ocfs2_read_xattr_bucket(search
, p_blkno
);
2761 xh
= bucket_xh(search
);
2762 high_bucket
= le16_to_cpu(xh
->xh_num_buckets
) - 1;
2763 while (low_bucket
<= high_bucket
) {
2764 ocfs2_xattr_bucket_relse(search
);
2766 bucket
= (low_bucket
+ high_bucket
) / 2;
2767 blkno
= p_blkno
+ bucket
* blk_per_bucket
;
2768 ret
= ocfs2_read_xattr_bucket(search
, blkno
);
2774 xh
= bucket_xh(search
);
2775 xe
= &xh
->xh_entries
[0];
2776 if (name_hash
< le32_to_cpu(xe
->xe_name_hash
)) {
2777 high_bucket
= bucket
- 1;
2782 * Check whether the hash of the last entry in our
2783 * bucket is larger than the search one. for an empty
2784 * bucket, the last one is also the first one.
2787 xe
= &xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1];
2789 last_hash
= le32_to_cpu(xe
->xe_name_hash
);
2791 /* record lower_blkno which may be the insert place. */
2792 lower_blkno
= blkno
;
2794 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
)) {
2795 low_bucket
= bucket
+ 1;
2799 /* the searched xattr should reside in this bucket if exists. */
2800 ret
= ocfs2_find_xe_in_bucket(inode
, search
,
2801 name_index
, name
, name_hash
,
2811 * Record the bucket we have found.
2812 * When the xattr's hash value is in the gap of 2 buckets, we will
2813 * always set it to the previous bucket.
2816 lower_blkno
= p_blkno
;
2818 /* This should be in cache - we just read it during the search */
2819 ret
= ocfs2_read_xattr_bucket(xs
->bucket
, lower_blkno
);
2825 xs
->header
= bucket_xh(xs
->bucket
);
2826 xs
->base
= bucket_block(xs
->bucket
, 0);
2827 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
2830 xs
->here
= &xs
->header
->xh_entries
[index
];
2831 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name
,
2832 (unsigned long long)bucket_blkno(xs
->bucket
), index
);
2837 ocfs2_xattr_bucket_free(search
);
2841 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
2842 struct buffer_head
*root_bh
,
2845 struct ocfs2_xattr_search
*xs
)
2848 struct ocfs2_xattr_block
*xb
=
2849 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
2850 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
2851 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
2853 u32 first_hash
, num_clusters
= 0;
2854 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
2856 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
2859 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2860 name
, name_hash
, name_index
);
2862 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &first_hash
,
2869 BUG_ON(p_blkno
== 0 || num_clusters
== 0 || first_hash
> name_hash
);
2871 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2872 "in the rec is %u\n", num_clusters
, (unsigned long long)p_blkno
,
2875 ret
= ocfs2_xattr_bucket_find(inode
, name_index
, name
, name_hash
,
2876 p_blkno
, first_hash
, num_clusters
, xs
);
2882 static int ocfs2_iterate_xattr_buckets(struct inode
*inode
,
2885 xattr_bucket_func
*func
,
2889 u32 bpc
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
2890 u32 num_buckets
= clusters
* bpc
;
2891 struct ocfs2_xattr_bucket
*bucket
;
2893 bucket
= ocfs2_xattr_bucket_new(inode
);
2895 mlog_errno(-ENOMEM
);
2899 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2900 clusters
, (unsigned long long)blkno
);
2902 for (i
= 0; i
< num_buckets
; i
++, blkno
+= bucket
->bu_blocks
) {
2903 ret
= ocfs2_read_xattr_bucket(bucket
, blkno
);
2910 * The real bucket num in this series of blocks is stored
2911 * in the 1st bucket.
2914 num_buckets
= le16_to_cpu(bucket_xh(bucket
)->xh_num_buckets
);
2916 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
2917 (unsigned long long)blkno
,
2918 le32_to_cpu(bucket_xh(bucket
)->xh_entries
[0].xe_name_hash
));
2920 ret
= func(inode
, bucket
, para
);
2923 /* Fall through to bucket_relse() */
2926 ocfs2_xattr_bucket_relse(bucket
);
2931 ocfs2_xattr_bucket_free(bucket
);
2935 struct ocfs2_xattr_tree_list
{
2941 static int ocfs2_xattr_bucket_get_name_value(struct inode
*inode
,
2942 struct ocfs2_xattr_header
*xh
,
2949 if (index
< 0 || index
>= le16_to_cpu(xh
->xh_count
))
2952 name_offset
= le16_to_cpu(xh
->xh_entries
[index
].xe_name_offset
);
2954 *block_off
= name_offset
>> inode
->i_sb
->s_blocksize_bits
;
2955 *new_offset
= name_offset
% inode
->i_sb
->s_blocksize
;
2960 static int ocfs2_list_xattr_bucket(struct inode
*inode
,
2961 struct ocfs2_xattr_bucket
*bucket
,
2965 struct ocfs2_xattr_tree_list
*xl
= (struct ocfs2_xattr_tree_list
*)para
;
2966 int i
, block_off
, new_offset
;
2967 const char *prefix
, *name
;
2969 for (i
= 0 ; i
< le16_to_cpu(bucket_xh(bucket
)->xh_count
); i
++) {
2970 struct ocfs2_xattr_entry
*entry
= &bucket_xh(bucket
)->xh_entries
[i
];
2971 type
= ocfs2_xattr_get_type(entry
);
2972 prefix
= ocfs2_xattr_prefix(type
);
2975 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
2983 name
= (const char *)bucket_block(bucket
, block_off
) +
2985 ret
= ocfs2_xattr_list_entry(xl
->buffer
,
2989 entry
->xe_name_len
);
2998 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
2999 struct ocfs2_xattr_tree_root
*xt
,
3003 struct ocfs2_extent_list
*el
= &xt
->xt_list
;
3005 u32 name_hash
= UINT_MAX
, e_cpos
= 0, num_clusters
= 0;
3007 struct ocfs2_xattr_tree_list xl
= {
3009 .buffer_size
= buffer_size
,
3013 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
3016 while (name_hash
> 0) {
3017 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
3018 &e_cpos
, &num_clusters
, el
);
3024 ret
= ocfs2_iterate_xattr_buckets(inode
, p_blkno
, num_clusters
,
3025 ocfs2_list_xattr_bucket
,
3035 name_hash
= e_cpos
- 1;
3043 static int cmp_xe(const void *a
, const void *b
)
3045 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
3046 u32 l_hash
= le32_to_cpu(l
->xe_name_hash
);
3047 u32 r_hash
= le32_to_cpu(r
->xe_name_hash
);
3049 if (l_hash
> r_hash
)
3051 if (l_hash
< r_hash
)
3056 static void swap_xe(void *a
, void *b
, int size
)
3058 struct ocfs2_xattr_entry
*l
= a
, *r
= b
, tmp
;
3061 memcpy(l
, r
, sizeof(struct ocfs2_xattr_entry
));
3062 memcpy(r
, &tmp
, sizeof(struct ocfs2_xattr_entry
));
3066 * When the ocfs2_xattr_block is filled up, new bucket will be created
3067 * and all the xattr entries will be moved to the new bucket.
3068 * The header goes at the start of the bucket, and the names+values are
3069 * filled from the end. This is why *target starts as the last buffer.
3070 * Note: we need to sort the entries since they are not saved in order
3071 * in the ocfs2_xattr_block.
3073 static void ocfs2_cp_xattr_block_to_bucket(struct inode
*inode
,
3074 struct buffer_head
*xb_bh
,
3075 struct ocfs2_xattr_bucket
*bucket
)
3077 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
3078 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3079 u16 offset
, size
, off_change
;
3080 struct ocfs2_xattr_entry
*xe
;
3081 struct ocfs2_xattr_block
*xb
=
3082 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
3083 struct ocfs2_xattr_header
*xb_xh
= &xb
->xb_attrs
.xb_header
;
3084 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
3085 u16 count
= le16_to_cpu(xb_xh
->xh_count
);
3086 char *src
= xb_bh
->b_data
;
3087 char *target
= bucket_block(bucket
, blks
- 1);
3089 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3090 (unsigned long long)xb_bh
->b_blocknr
,
3091 (unsigned long long)bucket_blkno(bucket
));
3093 for (i
= 0; i
< blks
; i
++)
3094 memset(bucket_block(bucket
, i
), 0, blocksize
);
3097 * Since the xe_name_offset is based on ocfs2_xattr_header,
3098 * there is a offset change corresponding to the change of
3099 * ocfs2_xattr_header's position.
3101 off_change
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
3102 xe
= &xb_xh
->xh_entries
[count
- 1];
3103 offset
= le16_to_cpu(xe
->xe_name_offset
) + off_change
;
3104 size
= blocksize
- offset
;
3106 /* copy all the names and values. */
3107 memcpy(target
+ offset
, src
+ offset
, size
);
3109 /* Init new header now. */
3110 xh
->xh_count
= xb_xh
->xh_count
;
3111 xh
->xh_num_buckets
= cpu_to_le16(1);
3112 xh
->xh_name_value_len
= cpu_to_le16(size
);
3113 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
- size
);
3115 /* copy all the entries. */
3116 target
= bucket_block(bucket
, 0);
3117 offset
= offsetof(struct ocfs2_xattr_header
, xh_entries
);
3118 size
= count
* sizeof(struct ocfs2_xattr_entry
);
3119 memcpy(target
+ offset
, (char *)xb_xh
+ offset
, size
);
3121 /* Change the xe offset for all the xe because of the move. */
3122 off_change
= OCFS2_XATTR_BUCKET_SIZE
- blocksize
+
3123 offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
3124 for (i
= 0; i
< count
; i
++)
3125 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
, off_change
);
3127 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3128 offset
, size
, off_change
);
3130 sort(target
+ offset
, count
, sizeof(struct ocfs2_xattr_entry
),
3135 * After we move xattr from block to index btree, we have to
3136 * update ocfs2_xattr_search to the new xe and base.
3138 * When the entry is in xattr block, xattr_bh indicates the storage place.
3139 * While if the entry is in index b-tree, "bucket" indicates the
3140 * real place of the xattr.
3142 static void ocfs2_xattr_update_xattr_search(struct inode
*inode
,
3143 struct ocfs2_xattr_search
*xs
,
3144 struct buffer_head
*old_bh
)
3146 char *buf
= old_bh
->b_data
;
3147 struct ocfs2_xattr_block
*old_xb
= (struct ocfs2_xattr_block
*)buf
;
3148 struct ocfs2_xattr_header
*old_xh
= &old_xb
->xb_attrs
.xb_header
;
3151 xs
->header
= bucket_xh(xs
->bucket
);
3152 xs
->base
= bucket_block(xs
->bucket
, 0);
3153 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
3158 i
= xs
->here
- old_xh
->xh_entries
;
3159 xs
->here
= &xs
->header
->xh_entries
[i
];
3162 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
3163 struct ocfs2_xattr_search
*xs
,
3164 struct ocfs2_xattr_set_ctxt
*ctxt
)
3169 handle_t
*handle
= ctxt
->handle
;
3170 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3171 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
3172 struct buffer_head
*xb_bh
= xs
->xattr_bh
;
3173 struct ocfs2_xattr_block
*xb
=
3174 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
3175 struct ocfs2_xattr_tree_root
*xr
;
3176 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
3178 mlog(0, "create xattr index block for %llu\n",
3179 (unsigned long long)xb_bh
->b_blocknr
);
3181 BUG_ON(xb_flags
& OCFS2_XATTR_INDEXED
);
3182 BUG_ON(!xs
->bucket
);
3186 * We can use this lock for now, and maybe move to a dedicated mutex
3187 * if performance becomes a problem later.
3189 down_write(&oi
->ip_alloc_sem
);
3191 ret
= ocfs2_journal_access(handle
, inode
, xb_bh
,
3192 OCFS2_JOURNAL_ACCESS_WRITE
);
3198 ret
= __ocfs2_claim_clusters(osb
, handle
, ctxt
->data_ac
,
3199 1, 1, &bit_off
, &len
);
3206 * The bucket may spread in many blocks, and
3207 * we will only touch the 1st block and the last block
3208 * in the whole bucket(one for entry and one for data).
3210 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
3212 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3213 (unsigned long long)blkno
);
3215 ret
= ocfs2_init_xattr_bucket(xs
->bucket
, blkno
);
3221 ret
= ocfs2_xattr_bucket_journal_access(handle
, xs
->bucket
,
3222 OCFS2_JOURNAL_ACCESS_CREATE
);
3228 ocfs2_cp_xattr_block_to_bucket(inode
, xb_bh
, xs
->bucket
);
3229 ocfs2_xattr_bucket_journal_dirty(handle
, xs
->bucket
);
3231 ocfs2_xattr_update_xattr_search(inode
, xs
, xb_bh
);
3233 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3234 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
3235 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
3237 xr
= &xb
->xb_attrs
.xb_root
;
3238 xr
->xt_clusters
= cpu_to_le32(1);
3239 xr
->xt_last_eb_blk
= 0;
3240 xr
->xt_list
.l_tree_depth
= 0;
3241 xr
->xt_list
.l_count
= cpu_to_le16(ocfs2_xattr_recs_per_xb(inode
->i_sb
));
3242 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
3244 xr
->xt_list
.l_recs
[0].e_cpos
= 0;
3245 xr
->xt_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
3246 xr
->xt_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
3248 xb
->xb_flags
= cpu_to_le16(xb_flags
| OCFS2_XATTR_INDEXED
);
3250 ocfs2_journal_dirty(handle
, xb_bh
);
3253 up_write(&oi
->ip_alloc_sem
);
3258 static int cmp_xe_offset(const void *a
, const void *b
)
3260 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
3261 u32 l_name_offset
= le16_to_cpu(l
->xe_name_offset
);
3262 u32 r_name_offset
= le16_to_cpu(r
->xe_name_offset
);
3264 if (l_name_offset
< r_name_offset
)
3266 if (l_name_offset
> r_name_offset
)
3272 * defrag a xattr bucket if we find that the bucket has some
3273 * holes beteen name/value pairs.
3274 * We will move all the name/value pairs to the end of the bucket
3275 * so that we can spare some space for insertion.
3277 static int ocfs2_defrag_xattr_bucket(struct inode
*inode
,
3279 struct ocfs2_xattr_bucket
*bucket
)
3282 size_t end
, offset
, len
, value_len
;
3283 struct ocfs2_xattr_header
*xh
;
3284 char *entries
, *buf
, *bucket_buf
= NULL
;
3285 u64 blkno
= bucket_blkno(bucket
);
3287 size_t blocksize
= inode
->i_sb
->s_blocksize
;
3288 struct ocfs2_xattr_entry
*xe
;
3291 * In order to make the operation more efficient and generic,
3292 * we copy all the blocks into a contiguous memory and do the
3293 * defragment there, so if anything is error, we will not touch
3296 bucket_buf
= kmalloc(OCFS2_XATTR_BUCKET_SIZE
, GFP_NOFS
);
3303 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
3304 memcpy(buf
, bucket_block(bucket
, i
), blocksize
);
3306 ret
= ocfs2_xattr_bucket_journal_access(handle
, bucket
,
3307 OCFS2_JOURNAL_ACCESS_WRITE
);
3313 xh
= (struct ocfs2_xattr_header
*)bucket_buf
;
3314 entries
= (char *)xh
->xh_entries
;
3315 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
3317 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3318 "xh_free_start = %u, xh_name_value_len = %u.\n",
3319 (unsigned long long)blkno
, le16_to_cpu(xh
->xh_count
),
3320 xh_free_start
, le16_to_cpu(xh
->xh_name_value_len
));
3323 * sort all the entries by their offset.
3324 * the largest will be the first, so that we can
3325 * move them to the end one by one.
3327 sort(entries
, le16_to_cpu(xh
->xh_count
),
3328 sizeof(struct ocfs2_xattr_entry
),
3329 cmp_xe_offset
, swap_xe
);
3331 /* Move all name/values to the end of the bucket. */
3332 xe
= xh
->xh_entries
;
3333 end
= OCFS2_XATTR_BUCKET_SIZE
;
3334 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, xe
++) {
3335 offset
= le16_to_cpu(xe
->xe_name_offset
);
3336 if (ocfs2_xattr_is_local(xe
))
3337 value_len
= OCFS2_XATTR_SIZE(
3338 le64_to_cpu(xe
->xe_value_size
));
3340 value_len
= OCFS2_XATTR_ROOT_SIZE
;
3341 len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
) + value_len
;
3344 * We must make sure that the name/value pair
3345 * exist in the same block. So adjust end to
3346 * the previous block end if needed.
3348 if (((end
- len
) / blocksize
!=
3349 (end
- 1) / blocksize
))
3350 end
= end
- end
% blocksize
;
3352 if (end
> offset
+ len
) {
3353 memmove(bucket_buf
+ end
- len
,
3354 bucket_buf
+ offset
, len
);
3355 xe
->xe_name_offset
= cpu_to_le16(end
- len
);
3358 mlog_bug_on_msg(end
< offset
+ len
, "Defrag check failed for "
3359 "bucket %llu\n", (unsigned long long)blkno
);
3364 mlog_bug_on_msg(xh_free_start
> end
, "Defrag check failed for "
3365 "bucket %llu\n", (unsigned long long)blkno
);
3367 if (xh_free_start
== end
)
3370 memset(bucket_buf
+ xh_free_start
, 0, end
- xh_free_start
);
3371 xh
->xh_free_start
= cpu_to_le16(end
);
3373 /* sort the entries by their name_hash. */
3374 sort(entries
, le16_to_cpu(xh
->xh_count
),
3375 sizeof(struct ocfs2_xattr_entry
),
3379 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
3380 memcpy(bucket_block(bucket
, i
), buf
, blocksize
);
3381 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
3389 * Move half nums of the xattr bucket in the previous cluster to this new
3390 * cluster. We only touch the last cluster of the previous extend record.
3392 * first_bh is the first buffer_head of a series of bucket in the same
3393 * extent rec and header_bh is the header of one bucket in this cluster.
3394 * They will be updated if we move the data header_bh contains to the new
3395 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3397 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode
*inode
,
3399 struct buffer_head
**first_bh
,
3400 struct buffer_head
**header_bh
,
3406 int i
, ret
, credits
;
3407 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3408 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3409 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
3410 int blocksize
= inode
->i_sb
->s_blocksize
;
3411 struct buffer_head
*old_bh
, *new_bh
, *prev_bh
, *new_first_bh
= NULL
;
3412 struct ocfs2_xattr_header
*new_xh
;
3413 struct ocfs2_xattr_header
*xh
=
3414 (struct ocfs2_xattr_header
*)((*first_bh
)->b_data
);
3416 BUG_ON(le16_to_cpu(xh
->xh_num_buckets
) < num_buckets
);
3417 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
== osb
->s_clustersize
);
3419 prev_bh
= *first_bh
;
3421 xh
= (struct ocfs2_xattr_header
*)prev_bh
->b_data
;
3423 prev_blkno
+= (num_clusters
- 1) * bpc
+ bpc
/ 2;
3425 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3426 (unsigned long long)prev_blkno
, (unsigned long long)new_blkno
);
3429 * We need to update the 1st half of the new cluster and
3430 * 1 more for the update of the 1st bucket of the previous
3433 credits
= bpc
/ 2 + 1 + handle
->h_buffer_credits
;
3434 ret
= ocfs2_extend_trans(handle
, credits
);
3440 ret
= ocfs2_journal_access(handle
, inode
, prev_bh
,
3441 OCFS2_JOURNAL_ACCESS_WRITE
);
3447 for (i
= 0; i
< bpc
/ 2; i
++, prev_blkno
++, new_blkno
++) {
3448 old_bh
= new_bh
= NULL
;
3449 new_bh
= sb_getblk(inode
->i_sb
, new_blkno
);
3456 ocfs2_set_new_buffer_uptodate(inode
, new_bh
);
3458 ret
= ocfs2_journal_access(handle
, inode
, new_bh
,
3459 OCFS2_JOURNAL_ACCESS_CREATE
);
3466 ret
= ocfs2_read_block(inode
, prev_blkno
, &old_bh
);
3473 memcpy(new_bh
->b_data
, old_bh
->b_data
, blocksize
);
3476 new_xh
= (struct ocfs2_xattr_header
*)new_bh
->b_data
;
3477 new_xh
->xh_num_buckets
= cpu_to_le16(num_buckets
/ 2);
3480 *first_hash
= le32_to_cpu(
3481 new_xh
->xh_entries
[0].xe_name_hash
);
3482 new_first_bh
= new_bh
;
3483 get_bh(new_first_bh
);
3486 ocfs2_journal_dirty(handle
, new_bh
);
3488 if (*header_bh
== old_bh
) {
3490 *header_bh
= new_bh
;
3494 *first_bh
= new_first_bh
;
3501 le16_add_cpu(&xh
->xh_num_buckets
, -(num_buckets
/ 2));
3503 ocfs2_journal_dirty(handle
, prev_bh
);
3506 brelse(new_first_bh
);
3511 * Find the suitable pos when we divide a bucket into 2.
3512 * We have to make sure the xattrs with the same hash value exist
3513 * in the same bucket.
3515 * If this ocfs2_xattr_header covers more than one hash value, find a
3516 * place where the hash value changes. Try to find the most even split.
3517 * The most common case is that all entries have different hash values,
3518 * and the first check we make will find a place to split.
3520 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header
*xh
)
3522 struct ocfs2_xattr_entry
*entries
= xh
->xh_entries
;
3523 int count
= le16_to_cpu(xh
->xh_count
);
3524 int delta
, middle
= count
/ 2;
3527 * We start at the middle. Each step gets farther away in both
3528 * directions. We therefore hit the change in hash value
3529 * nearest to the middle. Note that this loop does not execute for
3532 for (delta
= 0; delta
< middle
; delta
++) {
3533 /* Let's check delta earlier than middle */
3534 if (cmp_xe(&entries
[middle
- delta
- 1],
3535 &entries
[middle
- delta
]))
3536 return middle
- delta
;
3538 /* For even counts, don't walk off the end */
3539 if ((middle
+ delta
+ 1) == count
)
3542 /* Now try delta past middle */
3543 if (cmp_xe(&entries
[middle
+ delta
],
3544 &entries
[middle
+ delta
+ 1]))
3545 return middle
+ delta
+ 1;
3548 /* Every entry had the same hash */
3553 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3554 * first_hash will record the 1st hash of the new bucket.
3556 * Normally half of the xattrs will be moved. But we have to make
3557 * sure that the xattrs with the same hash value are stored in the
3558 * same bucket. If all the xattrs in this bucket have the same hash
3559 * value, the new bucket will be initialized as an empty one and the
3560 * first_hash will be initialized as (hash_value+1).
3562 static int ocfs2_divide_xattr_bucket(struct inode
*inode
,
3567 int new_bucket_head
)
3570 int count
, start
, len
, name_value_len
= 0, xe_len
, name_offset
= 0;
3571 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
3572 struct ocfs2_xattr_header
*xh
;
3573 struct ocfs2_xattr_entry
*xe
;
3574 int blocksize
= inode
->i_sb
->s_blocksize
;
3576 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
3577 (unsigned long long)blk
, (unsigned long long)new_blk
);
3579 s_bucket
= ocfs2_xattr_bucket_new(inode
);
3580 t_bucket
= ocfs2_xattr_bucket_new(inode
);
3581 if (!s_bucket
|| !t_bucket
) {
3587 ret
= ocfs2_read_xattr_bucket(s_bucket
, blk
);
3593 ret
= ocfs2_xattr_bucket_journal_access(handle
, s_bucket
,
3594 OCFS2_JOURNAL_ACCESS_WRITE
);
3601 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3602 * there's no need to read it.
3604 ret
= ocfs2_init_xattr_bucket(t_bucket
, new_blk
);
3610 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
3612 OCFS2_JOURNAL_ACCESS_CREATE
:
3613 OCFS2_JOURNAL_ACCESS_WRITE
);
3619 xh
= bucket_xh(s_bucket
);
3620 count
= le16_to_cpu(xh
->xh_count
);
3621 start
= ocfs2_xattr_find_divide_pos(xh
);
3623 if (start
== count
) {
3624 xe
= &xh
->xh_entries
[start
-1];
3627 * initialized a new empty bucket here.
3628 * The hash value is set as one larger than
3629 * that of the last entry in the previous bucket.
3631 for (i
= 0; i
< t_bucket
->bu_blocks
; i
++)
3632 memset(bucket_block(t_bucket
, i
), 0, blocksize
);
3634 xh
= bucket_xh(t_bucket
);
3635 xh
->xh_free_start
= cpu_to_le16(blocksize
);
3636 xh
->xh_entries
[0].xe_name_hash
= xe
->xe_name_hash
;
3637 le32_add_cpu(&xh
->xh_entries
[0].xe_name_hash
, 1);
3639 goto set_num_buckets
;
3642 /* copy the whole bucket to the new first. */
3643 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
3645 /* update the new bucket. */
3646 xh
= bucket_xh(t_bucket
);
3649 * Calculate the total name/value len and xh_free_start for
3650 * the old bucket first.
3652 name_offset
= OCFS2_XATTR_BUCKET_SIZE
;
3654 for (i
= 0; i
< start
; i
++) {
3655 xe
= &xh
->xh_entries
[i
];
3656 xe_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3657 if (ocfs2_xattr_is_local(xe
))
3659 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3661 xe_len
+= OCFS2_XATTR_ROOT_SIZE
;
3662 name_value_len
+= xe_len
;
3663 if (le16_to_cpu(xe
->xe_name_offset
) < name_offset
)
3664 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3668 * Now begin the modification to the new bucket.
3670 * In the new bucket, We just move the xattr entry to the beginning
3671 * and don't touch the name/value. So there will be some holes in the
3672 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3675 xe
= &xh
->xh_entries
[start
];
3676 len
= sizeof(struct ocfs2_xattr_entry
) * (count
- start
);
3677 mlog(0, "mv xattr entry len %d from %d to %d\n", len
,
3678 (int)((char *)xe
- (char *)xh
),
3679 (int)((char *)xh
->xh_entries
- (char *)xh
));
3680 memmove((char *)xh
->xh_entries
, (char *)xe
, len
);
3681 xe
= &xh
->xh_entries
[count
- start
];
3682 len
= sizeof(struct ocfs2_xattr_entry
) * start
;
3683 memset((char *)xe
, 0, len
);
3685 le16_add_cpu(&xh
->xh_count
, -start
);
3686 le16_add_cpu(&xh
->xh_name_value_len
, -name_value_len
);
3688 /* Calculate xh_free_start for the new bucket. */
3689 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
3690 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
3691 xe
= &xh
->xh_entries
[i
];
3692 xe_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3693 if (ocfs2_xattr_is_local(xe
))
3695 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3697 xe_len
+= OCFS2_XATTR_ROOT_SIZE
;
3698 if (le16_to_cpu(xe
->xe_name_offset
) <
3699 le16_to_cpu(xh
->xh_free_start
))
3700 xh
->xh_free_start
= xe
->xe_name_offset
;
3704 /* set xh->xh_num_buckets for the new xh. */
3705 if (new_bucket_head
)
3706 xh
->xh_num_buckets
= cpu_to_le16(1);
3708 xh
->xh_num_buckets
= 0;
3710 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
3712 /* store the first_hash of the new bucket. */
3714 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3717 * Now only update the 1st block of the old bucket. If we
3718 * just added a new empty bucket, there is no need to modify
3724 xh
= bucket_xh(s_bucket
);
3725 memset(&xh
->xh_entries
[start
], 0,
3726 sizeof(struct ocfs2_xattr_entry
) * (count
- start
));
3727 xh
->xh_count
= cpu_to_le16(start
);
3728 xh
->xh_free_start
= cpu_to_le16(name_offset
);
3729 xh
->xh_name_value_len
= cpu_to_le16(name_value_len
);
3731 ocfs2_xattr_bucket_journal_dirty(handle
, s_bucket
);
3734 ocfs2_xattr_bucket_free(s_bucket
);
3735 ocfs2_xattr_bucket_free(t_bucket
);
3741 * Copy xattr from one bucket to another bucket.
3743 * The caller must make sure that the journal transaction
3744 * has enough space for journaling.
3746 static int ocfs2_cp_xattr_bucket(struct inode
*inode
,
3753 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
3755 BUG_ON(s_blkno
== t_blkno
);
3757 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3758 (unsigned long long)s_blkno
, (unsigned long long)t_blkno
,
3761 s_bucket
= ocfs2_xattr_bucket_new(inode
);
3762 t_bucket
= ocfs2_xattr_bucket_new(inode
);
3763 if (!s_bucket
|| !t_bucket
) {
3769 ret
= ocfs2_read_xattr_bucket(s_bucket
, s_blkno
);
3774 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3775 * there's no need to read it.
3777 ret
= ocfs2_init_xattr_bucket(t_bucket
, t_blkno
);
3781 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
3783 OCFS2_JOURNAL_ACCESS_CREATE
:
3784 OCFS2_JOURNAL_ACCESS_WRITE
);
3788 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
3789 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
3792 ocfs2_xattr_bucket_free(t_bucket
);
3793 ocfs2_xattr_bucket_free(s_bucket
);
3799 * Copy one xattr cluster from src_blk to to_blk.
3800 * The to_blk will become the first bucket header of the cluster, so its
3801 * xh_num_buckets will be initialized as the bucket num in the cluster.
3803 static int ocfs2_cp_xattr_cluster(struct inode
*inode
,
3805 struct buffer_head
*first_bh
,
3810 int i
, ret
, credits
;
3811 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3812 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3813 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
3814 struct buffer_head
*bh
= NULL
;
3815 struct ocfs2_xattr_header
*xh
;
3816 u64 to_blk_start
= to_blk
;
3818 mlog(0, "cp xattrs from cluster %llu to %llu\n",
3819 (unsigned long long)src_blk
, (unsigned long long)to_blk
);
3822 * We need to update the new cluster and 1 more for the update of
3823 * the 1st bucket of the previous extent rec.
3825 credits
= bpc
+ 1 + handle
->h_buffer_credits
;
3826 ret
= ocfs2_extend_trans(handle
, credits
);
3832 ret
= ocfs2_journal_access(handle
, inode
, first_bh
,
3833 OCFS2_JOURNAL_ACCESS_WRITE
);
3839 for (i
= 0; i
< num_buckets
; i
++) {
3840 ret
= ocfs2_cp_xattr_bucket(inode
, handle
,
3841 src_blk
, to_blk
, 1);
3847 src_blk
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3848 to_blk
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3851 /* update the old bucket header. */
3852 xh
= (struct ocfs2_xattr_header
*)first_bh
->b_data
;
3853 le16_add_cpu(&xh
->xh_num_buckets
, -num_buckets
);
3855 ocfs2_journal_dirty(handle
, first_bh
);
3857 /* update the new bucket header. */
3858 ret
= ocfs2_read_block(inode
, to_blk_start
, &bh
);
3864 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3865 OCFS2_JOURNAL_ACCESS_WRITE
);
3871 xh
= (struct ocfs2_xattr_header
*)bh
->b_data
;
3872 xh
->xh_num_buckets
= cpu_to_le16(num_buckets
);
3874 ocfs2_journal_dirty(handle
, bh
);
3877 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3884 * Move some xattrs in this cluster to the new cluster.
3885 * This function should only be called when bucket size == cluster size.
3886 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3888 static int ocfs2_divide_xattr_cluster(struct inode
*inode
,
3894 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3895 int ret
, credits
= 2 * blk_per_bucket
+ handle
->h_buffer_credits
;
3897 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
< OCFS2_SB(inode
->i_sb
)->s_clustersize
);
3899 ret
= ocfs2_extend_trans(handle
, credits
);
3905 /* Move half of the xattr in start_blk to the next bucket. */
3906 return ocfs2_divide_xattr_bucket(inode
, handle
, prev_blk
,
3907 new_blk
, first_hash
, 1);
3911 * Move some xattrs from the old cluster to the new one since they are not
3912 * contiguous in ocfs2 xattr tree.
3914 * new_blk starts a new separate cluster, and we will move some xattrs from
3915 * prev_blk to it. v_start will be set as the first name hash value in this
3916 * new cluster so that it can be used as e_cpos during tree insertion and
3917 * don't collide with our original b-tree operations. first_bh and header_bh
3918 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3919 * to extend the insert bucket.
3921 * The problem is how much xattr should we move to the new one and when should
3922 * we update first_bh and header_bh?
3923 * 1. If cluster size > bucket size, that means the previous cluster has more
3924 * than 1 bucket, so just move half nums of bucket into the new cluster and
3925 * update the first_bh and header_bh if the insert bucket has been moved
3926 * to the new cluster.
3927 * 2. If cluster_size == bucket_size:
3928 * a) If the previous extent rec has more than one cluster and the insert
3929 * place isn't in the last cluster, copy the entire last cluster to the
3930 * new one. This time, we don't need to upate the first_bh and header_bh
3931 * since they will not be moved into the new cluster.
3932 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3933 * the new one. And we set the extend flag to zero if the insert place is
3934 * moved into the new allocated cluster since no extend is needed.
3936 static int ocfs2_adjust_xattr_cross_cluster(struct inode
*inode
,
3938 struct buffer_head
**first_bh
,
3939 struct buffer_head
**header_bh
,
3947 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3949 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3950 (unsigned long long)prev_blk
, prev_clusters
,
3951 (unsigned long long)new_blk
);
3953 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
)) > 1)
3954 ret
= ocfs2_mv_xattr_bucket_cross_cluster(inode
,
3963 u64 last_blk
= prev_blk
+ bpc
* (prev_clusters
- 1);
3965 if (prev_clusters
> 1 && (*header_bh
)->b_blocknr
!= last_blk
)
3966 ret
= ocfs2_cp_xattr_cluster(inode
, handle
, *first_bh
,
3970 ret
= ocfs2_divide_xattr_cluster(inode
, handle
,
3974 if ((*header_bh
)->b_blocknr
== last_blk
&& extend
)
3983 * Add a new cluster for xattr storage.
3985 * If the new cluster is contiguous with the previous one, it will be
3986 * appended to the same extent record, and num_clusters will be updated.
3987 * If not, we will insert a new extent for it and move some xattrs in
3988 * the last cluster into the new allocated one.
3989 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3990 * lose the benefits of hashing because we'll have to search large leaves.
3991 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3994 * first_bh is the first block of the previous extent rec and header_bh
3995 * indicates the bucket we will insert the new xattrs. They will be updated
3996 * when the header_bh is moved into the new cluster.
3998 static int ocfs2_add_new_xattr_cluster(struct inode
*inode
,
3999 struct buffer_head
*root_bh
,
4000 struct buffer_head
**first_bh
,
4001 struct buffer_head
**header_bh
,
4006 struct ocfs2_xattr_set_ctxt
*ctxt
)
4009 u16 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
4010 u32 prev_clusters
= *num_clusters
;
4011 u32 clusters_to_add
= 1, bit_off
, num_bits
, v_start
= 0;
4013 handle_t
*handle
= ctxt
->handle
;
4014 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4015 struct ocfs2_extent_tree et
;
4017 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4018 "previous xattr blkno = %llu\n",
4019 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
4020 prev_cpos
, (unsigned long long)prev_blkno
);
4022 ocfs2_init_xattr_tree_extent_tree(&et
, inode
, root_bh
);
4024 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
4025 OCFS2_JOURNAL_ACCESS_WRITE
);
4031 ret
= __ocfs2_claim_clusters(osb
, handle
, ctxt
->data_ac
, 1,
4032 clusters_to_add
, &bit_off
, &num_bits
);
4039 BUG_ON(num_bits
> clusters_to_add
);
4041 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
4042 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4043 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4045 if (prev_blkno
+ prev_clusters
* bpc
== block
&&
4046 (prev_clusters
+ num_bits
) << osb
->s_clustersize_bits
<=
4047 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
) {
4049 * If this cluster is contiguous with the old one and
4050 * adding this new cluster, we don't surpass the limit of
4051 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4052 * initialized and used like other buckets in the previous
4054 * So add it as a contiguous one. The caller will handle
4057 v_start
= prev_cpos
+ prev_clusters
;
4058 *num_clusters
= prev_clusters
+ num_bits
;
4059 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4062 ret
= ocfs2_adjust_xattr_cross_cluster(inode
,
4077 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
4078 num_bits
, (unsigned long long)block
, v_start
);
4079 ret
= ocfs2_insert_extent(osb
, handle
, inode
, &et
, v_start
, block
,
4080 num_bits
, 0, ctxt
->meta_ac
);
4086 ret
= ocfs2_journal_dirty(handle
, root_bh
);
4095 * Extend a new xattr bucket and move xattrs to the end one by one until
4096 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
4098 static int ocfs2_extend_xattr_bucket(struct inode
*inode
,
4100 struct buffer_head
*first_bh
,
4101 struct buffer_head
*start_bh
,
4105 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4106 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4107 u64 start_blk
= start_bh
->b_blocknr
, end_blk
;
4108 u32 num_buckets
= num_clusters
* ocfs2_xattr_buckets_per_cluster(osb
);
4109 struct ocfs2_xattr_header
*first_xh
=
4110 (struct ocfs2_xattr_header
*)first_bh
->b_data
;
4111 u16 bucket
= le16_to_cpu(first_xh
->xh_num_buckets
);
4113 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
4114 "from %llu, len = %u\n", (unsigned long long)start_blk
,
4115 (unsigned long long)first_bh
->b_blocknr
, num_clusters
);
4117 BUG_ON(bucket
>= num_buckets
);
4119 end_blk
= first_bh
->b_blocknr
+ (bucket
- 1) * blk_per_bucket
;
4122 * We will touch all the buckets after the start_bh(include it).
4123 * Then we add one more bucket.
4125 credits
= end_blk
- start_blk
+ 3 * blk_per_bucket
+ 1 +
4126 handle
->h_buffer_credits
;
4127 ret
= ocfs2_extend_trans(handle
, credits
);
4133 ret
= ocfs2_journal_access(handle
, inode
, first_bh
,
4134 OCFS2_JOURNAL_ACCESS_WRITE
);
4140 while (end_blk
!= start_blk
) {
4141 ret
= ocfs2_cp_xattr_bucket(inode
, handle
, end_blk
,
4142 end_blk
+ blk_per_bucket
, 0);
4145 end_blk
-= blk_per_bucket
;
4148 /* Move half of the xattr in start_blk to the next bucket. */
4149 ret
= ocfs2_divide_xattr_bucket(inode
, handle
, start_blk
,
4150 start_blk
+ blk_per_bucket
, NULL
, 0);
4152 le16_add_cpu(&first_xh
->xh_num_buckets
, 1);
4153 ocfs2_journal_dirty(handle
, first_bh
);
4160 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
4161 * xb_bh is the ocfs2_xattr_block.
4162 * We will move all the buckets starting from header_bh to the next place. As
4163 * for this one, half num of its xattrs will be moved to the next one.
4165 * We will allocate a new cluster if current cluster is full and adjust
4166 * header_bh and first_bh if the insert place is moved to the new cluster.
4168 static int ocfs2_add_new_xattr_bucket(struct inode
*inode
,
4169 struct buffer_head
*xb_bh
,
4170 struct buffer_head
*header_bh
,
4171 struct ocfs2_xattr_set_ctxt
*ctxt
)
4173 struct ocfs2_xattr_header
*first_xh
= NULL
;
4174 struct buffer_head
*first_bh
= NULL
;
4175 struct ocfs2_xattr_block
*xb
=
4176 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4177 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
4178 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
4179 struct ocfs2_xattr_header
*xh
=
4180 (struct ocfs2_xattr_header
*)header_bh
->b_data
;
4181 u32 name_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
4182 struct super_block
*sb
= inode
->i_sb
;
4183 struct ocfs2_super
*osb
= OCFS2_SB(sb
);
4184 int ret
, num_buckets
, extend
= 1;
4186 u32 e_cpos
, num_clusters
;
4188 mlog(0, "Add new xattr bucket starting form %llu\n",
4189 (unsigned long long)header_bh
->b_blocknr
);
4192 * Add refrence for header_bh here because it may be
4193 * changed in ocfs2_add_new_xattr_cluster and we need
4194 * to free it in the end.
4198 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &e_cpos
,
4205 ret
= ocfs2_read_block(inode
, p_blkno
, &first_bh
);
4211 num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
) * num_clusters
;
4212 first_xh
= (struct ocfs2_xattr_header
*)first_bh
->b_data
;
4214 if (num_buckets
== le16_to_cpu(first_xh
->xh_num_buckets
)) {
4215 ret
= ocfs2_add_new_xattr_cluster(inode
,
4231 ret
= ocfs2_extend_xattr_bucket(inode
,
4244 static inline char *ocfs2_xattr_bucket_get_val(struct inode
*inode
,
4245 struct ocfs2_xattr_bucket
*bucket
,
4248 int block_off
= offs
>> inode
->i_sb
->s_blocksize_bits
;
4250 offs
= offs
% inode
->i_sb
->s_blocksize
;
4251 return bucket_block(bucket
, block_off
) + offs
;
4255 * Handle the normal xattr set, including replace, delete and new.
4257 * Note: "local" indicates the real data's locality. So we can't
4258 * just its bucket locality by its length.
4260 static void ocfs2_xattr_set_entry_normal(struct inode
*inode
,
4261 struct ocfs2_xattr_info
*xi
,
4262 struct ocfs2_xattr_search
*xs
,
4266 struct ocfs2_xattr_entry
*last
, *xe
;
4267 int name_len
= strlen(xi
->name
);
4268 struct ocfs2_xattr_header
*xh
= xs
->header
;
4269 u16 count
= le16_to_cpu(xh
->xh_count
), start
;
4270 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4272 size_t offs
, size
, new_size
;
4274 last
= &xh
->xh_entries
[count
];
4275 if (!xs
->not_found
) {
4277 offs
= le16_to_cpu(xe
->xe_name_offset
);
4278 if (ocfs2_xattr_is_local(xe
))
4279 size
= OCFS2_XATTR_SIZE(name_len
) +
4280 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
4282 size
= OCFS2_XATTR_SIZE(name_len
) +
4283 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE
);
4286 * If the new value will be stored outside, xi->value has been
4287 * initalized as an empty ocfs2_xattr_value_root, and the same
4288 * goes with xi->value_len, so we can set new_size safely here.
4289 * See ocfs2_xattr_set_in_bucket.
4291 new_size
= OCFS2_XATTR_SIZE(name_len
) +
4292 OCFS2_XATTR_SIZE(xi
->value_len
);
4294 le16_add_cpu(&xh
->xh_name_value_len
, -size
);
4296 if (new_size
> size
)
4297 goto set_new_name_value
;
4299 /* Now replace the old value with new one. */
4301 xe
->xe_value_size
= cpu_to_le64(xi
->value_len
);
4303 xe
->xe_value_size
= 0;
4305 val
= ocfs2_xattr_bucket_get_val(inode
,
4307 memset(val
+ OCFS2_XATTR_SIZE(name_len
), 0,
4308 size
- OCFS2_XATTR_SIZE(name_len
));
4309 if (OCFS2_XATTR_SIZE(xi
->value_len
) > 0)
4310 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
4311 xi
->value
, xi
->value_len
);
4313 le16_add_cpu(&xh
->xh_name_value_len
, new_size
);
4314 ocfs2_xattr_set_local(xe
, local
);
4318 * Remove the old entry if there is more than one.
4319 * We don't remove the last entry so that we can
4320 * use it to indicate the hash value of the empty
4324 le16_add_cpu(&xh
->xh_count
, -1);
4327 (void *)last
- (void *)xe
);
4329 sizeof(struct ocfs2_xattr_entry
));
4332 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
4337 /* find a new entry for insert. */
4338 int low
= 0, high
= count
- 1, tmp
;
4339 struct ocfs2_xattr_entry
*tmp_xe
;
4341 while (low
<= high
&& count
) {
4342 tmp
= (low
+ high
) / 2;
4343 tmp_xe
= &xh
->xh_entries
[tmp
];
4345 if (name_hash
> le32_to_cpu(tmp_xe
->xe_name_hash
))
4347 else if (name_hash
<
4348 le32_to_cpu(tmp_xe
->xe_name_hash
))
4356 xe
= &xh
->xh_entries
[low
];
4358 memmove(xe
+ 1, xe
, (void *)last
- (void *)xe
);
4360 le16_add_cpu(&xh
->xh_count
, 1);
4361 memset(xe
, 0, sizeof(struct ocfs2_xattr_entry
));
4362 xe
->xe_name_hash
= cpu_to_le32(name_hash
);
4363 xe
->xe_name_len
= name_len
;
4364 ocfs2_xattr_set_type(xe
, xi
->name_index
);
4368 /* Insert the new name+value. */
4369 size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_SIZE(xi
->value_len
);
4372 * We must make sure that the name/value pair
4373 * exists in the same block.
4375 offs
= le16_to_cpu(xh
->xh_free_start
);
4376 start
= offs
- size
;
4378 if (start
>> inode
->i_sb
->s_blocksize_bits
!=
4379 (offs
- 1) >> inode
->i_sb
->s_blocksize_bits
) {
4380 offs
= offs
- offs
% blocksize
;
4381 xh
->xh_free_start
= cpu_to_le16(offs
);
4384 val
= ocfs2_xattr_bucket_get_val(inode
, xs
->bucket
, offs
- size
);
4385 xe
->xe_name_offset
= cpu_to_le16(offs
- size
);
4387 memset(val
, 0, size
);
4388 memcpy(val
, xi
->name
, name_len
);
4389 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
), xi
->value
, xi
->value_len
);
4391 xe
->xe_value_size
= cpu_to_le64(xi
->value_len
);
4392 ocfs2_xattr_set_local(xe
, local
);
4394 le16_add_cpu(&xh
->xh_free_start
, -size
);
4395 le16_add_cpu(&xh
->xh_name_value_len
, size
);
4401 * Set the xattr entry in the specified bucket.
4402 * The bucket is indicated by xs->bucket and it should have the enough
4403 * space for the xattr insertion.
4405 static int ocfs2_xattr_set_entry_in_bucket(struct inode
*inode
,
4407 struct ocfs2_xattr_info
*xi
,
4408 struct ocfs2_xattr_search
*xs
,
4415 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4416 (unsigned long)xi
->value_len
, xi
->name_index
,
4417 (unsigned long long)bucket_blkno(xs
->bucket
));
4419 if (!xs
->bucket
->bu_bhs
[1]) {
4420 blkno
= bucket_blkno(xs
->bucket
);
4421 ocfs2_xattr_bucket_relse(xs
->bucket
);
4422 ret
= ocfs2_read_xattr_bucket(xs
->bucket
, blkno
);
4429 ret
= ocfs2_xattr_bucket_journal_access(handle
, xs
->bucket
,
4430 OCFS2_JOURNAL_ACCESS_WRITE
);
4436 ocfs2_xattr_set_entry_normal(inode
, xi
, xs
, name_hash
, local
);
4437 ocfs2_xattr_bucket_journal_dirty(handle
, xs
->bucket
);
4443 static int ocfs2_xattr_value_update_size(struct inode
*inode
,
4445 struct buffer_head
*xe_bh
,
4446 struct ocfs2_xattr_entry
*xe
,
4451 ret
= ocfs2_journal_access(handle
, inode
, xe_bh
,
4452 OCFS2_JOURNAL_ACCESS_WRITE
);
4458 xe
->xe_value_size
= cpu_to_le64(new_size
);
4460 ret
= ocfs2_journal_dirty(handle
, xe_bh
);
4469 * Truncate the specified xe_off entry in xattr bucket.
4470 * bucket is indicated by header_bh and len is the new length.
4471 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4473 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4475 static int ocfs2_xattr_bucket_value_truncate(struct inode
*inode
,
4476 struct buffer_head
*header_bh
,
4479 struct ocfs2_xattr_set_ctxt
*ctxt
)
4483 struct buffer_head
*value_bh
= NULL
;
4484 struct ocfs2_xattr_value_root
*xv
;
4485 struct ocfs2_xattr_entry
*xe
;
4486 struct ocfs2_xattr_header
*xh
=
4487 (struct ocfs2_xattr_header
*)header_bh
->b_data
;
4488 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4490 xe
= &xh
->xh_entries
[xe_off
];
4492 BUG_ON(!xe
|| ocfs2_xattr_is_local(xe
));
4494 offset
= le16_to_cpu(xe
->xe_name_offset
) +
4495 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
4497 value_blk
= offset
/ blocksize
;
4499 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4500 BUG_ON(value_blk
!= (offset
+ OCFS2_XATTR_ROOT_SIZE
- 1) / blocksize
);
4501 value_blk
+= header_bh
->b_blocknr
;
4503 ret
= ocfs2_read_block(inode
, value_blk
, &value_bh
);
4509 xv
= (struct ocfs2_xattr_value_root
*)
4510 (value_bh
->b_data
+ offset
% blocksize
);
4512 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4513 xe_off
, (unsigned long long)header_bh
->b_blocknr
, len
);
4514 ret
= ocfs2_xattr_value_truncate(inode
, value_bh
, xv
, len
, ctxt
);
4520 ret
= ocfs2_xattr_value_update_size(inode
, ctxt
->handle
,
4521 header_bh
, xe
, len
);
4532 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode
*inode
,
4533 struct ocfs2_xattr_search
*xs
,
4535 struct ocfs2_xattr_set_ctxt
*ctxt
)
4538 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4539 struct ocfs2_xattr_header
*xh
= (struct ocfs2_xattr_header
*)xs
->base
;
4541 BUG_ON(!xs
->bucket
->bu_bhs
[0] || !xe
|| ocfs2_xattr_is_local(xe
));
4543 offset
= xe
- xh
->xh_entries
;
4544 ret
= ocfs2_xattr_bucket_value_truncate(inode
, xs
->bucket
->bu_bhs
[0],
4552 static int ocfs2_xattr_bucket_set_value_outside(struct inode
*inode
,
4554 struct ocfs2_xattr_search
*xs
,
4559 struct ocfs2_xattr_value_root
*xv
;
4560 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4562 BUG_ON(!xs
->base
|| !xe
|| ocfs2_xattr_is_local(xe
));
4564 offset
= le16_to_cpu(xe
->xe_name_offset
) +
4565 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
4567 xv
= (struct ocfs2_xattr_value_root
*)(xs
->base
+ offset
);
4569 return __ocfs2_xattr_set_value_outside(inode
, handle
,
4570 xv
, val
, value_len
);
4573 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
4574 struct buffer_head
*root_bh
,
4580 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4581 struct inode
*tl_inode
= osb
->osb_tl_inode
;
4583 struct ocfs2_xattr_block
*xb
=
4584 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
4585 struct ocfs2_alloc_context
*meta_ac
= NULL
;
4586 struct ocfs2_cached_dealloc_ctxt dealloc
;
4587 struct ocfs2_extent_tree et
;
4589 ocfs2_init_xattr_tree_extent_tree(&et
, inode
, root_bh
);
4591 ocfs2_init_dealloc_ctxt(&dealloc
);
4593 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4594 cpos
, len
, (unsigned long long)blkno
);
4596 ocfs2_remove_xattr_clusters_from_cache(inode
, blkno
, len
);
4598 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
4604 mutex_lock(&tl_inode
->i_mutex
);
4606 if (ocfs2_truncate_log_needs_flush(osb
)) {
4607 ret
= __ocfs2_flush_truncate_log(osb
);
4614 handle
= ocfs2_start_trans(osb
, OCFS2_REMOVE_EXTENT_CREDITS
);
4615 if (IS_ERR(handle
)) {
4621 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
4622 OCFS2_JOURNAL_ACCESS_WRITE
);
4628 ret
= ocfs2_remove_extent(inode
, &et
, cpos
, len
, handle
, meta_ac
,
4635 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, -len
);
4637 ret
= ocfs2_journal_dirty(handle
, root_bh
);
4643 ret
= ocfs2_truncate_log_append(osb
, handle
, blkno
, len
);
4648 ocfs2_commit_trans(osb
, handle
);
4650 ocfs2_schedule_truncate_log_flush(osb
, 1);
4652 mutex_unlock(&tl_inode
->i_mutex
);
4655 ocfs2_free_alloc_context(meta_ac
);
4657 ocfs2_run_deallocs(osb
, &dealloc
);
4662 static void ocfs2_xattr_bucket_remove_xs(struct inode
*inode
,
4664 struct ocfs2_xattr_search
*xs
)
4666 struct ocfs2_xattr_header
*xh
= bucket_xh(xs
->bucket
);
4667 struct ocfs2_xattr_entry
*last
= &xh
->xh_entries
[
4668 le16_to_cpu(xh
->xh_count
) - 1];
4671 ret
= ocfs2_xattr_bucket_journal_access(handle
, xs
->bucket
,
4672 OCFS2_JOURNAL_ACCESS_WRITE
);
4678 /* Remove the old entry. */
4679 memmove(xs
->here
, xs
->here
+ 1,
4680 (void *)last
- (void *)xs
->here
);
4681 memset(last
, 0, sizeof(struct ocfs2_xattr_entry
));
4682 le16_add_cpu(&xh
->xh_count
, -1);
4684 ocfs2_xattr_bucket_journal_dirty(handle
, xs
->bucket
);
4688 * Set the xattr name/value in the bucket specified in xs.
4690 * As the new value in xi may be stored in the bucket or in an outside cluster,
4691 * we divide the whole process into 3 steps:
4692 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4693 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4694 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4695 * 4. If the clusters for the new outside value can't be allocated, we need
4696 * to free the xattr we allocated in set.
4698 static int ocfs2_xattr_set_in_bucket(struct inode
*inode
,
4699 struct ocfs2_xattr_info
*xi
,
4700 struct ocfs2_xattr_search
*xs
,
4701 struct ocfs2_xattr_set_ctxt
*ctxt
)
4705 char *val
= (char *)xi
->value
;
4706 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4707 u32 name_hash
= ocfs2_xattr_name_hash(inode
, xi
->name
,
4710 if (!xs
->not_found
&& !ocfs2_xattr_is_local(xe
)) {
4712 * We need to truncate the xattr storage first.
4714 * If both the old and new value are stored to
4715 * outside block, we only need to truncate
4716 * the storage and then set the value outside.
4718 * If the new value should be stored within block,
4719 * we should free all the outside block first and
4720 * the modification to the xattr block will be done
4721 * by following steps.
4723 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
4724 value_len
= xi
->value_len
;
4728 ret
= ocfs2_xattr_bucket_value_truncate_xs(inode
, xs
,
4735 goto set_value_outside
;
4738 value_len
= xi
->value_len
;
4739 /* So we have to handle the inside block change now. */
4740 if (value_len
> OCFS2_XATTR_INLINE_SIZE
) {
4742 * If the new value will be stored outside of block,
4743 * initalize a new empty value root and insert it first.
4746 xi
->value
= &def_xv
;
4747 xi
->value_len
= OCFS2_XATTR_ROOT_SIZE
;
4750 ret
= ocfs2_xattr_set_entry_in_bucket(inode
, ctxt
->handle
, xi
, xs
,
4757 if (value_len
<= OCFS2_XATTR_INLINE_SIZE
)
4760 /* allocate the space now for the outside block storage. */
4761 ret
= ocfs2_xattr_bucket_value_truncate_xs(inode
, xs
,
4766 if (xs
->not_found
) {
4768 * We can't allocate enough clusters for outside
4769 * storage and we have allocated xattr already,
4770 * so need to remove it.
4772 ocfs2_xattr_bucket_remove_xs(inode
, ctxt
->handle
, xs
);
4778 ret
= ocfs2_xattr_bucket_set_value_outside(inode
, ctxt
->handle
,
4779 xs
, val
, value_len
);
4785 * check whether the xattr bucket is filled up with the same hash value.
4786 * If we want to insert the xattr with the same hash, return -ENOSPC.
4787 * If we want to insert a xattr with different hash value, go ahead
4788 * and ocfs2_divide_xattr_bucket will handle this.
4790 static int ocfs2_check_xattr_bucket_collision(struct inode
*inode
,
4791 struct ocfs2_xattr_bucket
*bucket
,
4794 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
4795 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
4797 if (name_hash
!= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
))
4800 if (xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1].xe_name_hash
==
4801 xh
->xh_entries
[0].xe_name_hash
) {
4802 mlog(ML_ERROR
, "Too much hash collision in xattr bucket %llu, "
4804 (unsigned long long)bucket_blkno(bucket
),
4805 le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
));
4812 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
4813 struct ocfs2_xattr_info
*xi
,
4814 struct ocfs2_xattr_search
*xs
,
4815 struct ocfs2_xattr_set_ctxt
*ctxt
)
4817 struct ocfs2_xattr_header
*xh
;
4818 struct ocfs2_xattr_entry
*xe
;
4819 u16 count
, header_size
, xh_free_start
;
4820 int free
, max_free
, need
, old
;
4821 size_t value_size
= 0, name_len
= strlen(xi
->name
);
4822 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4823 int ret
, allocation
= 0;
4825 mlog_entry("Set xattr %s in xattr index block\n", xi
->name
);
4829 count
= le16_to_cpu(xh
->xh_count
);
4830 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4831 header_size
= sizeof(struct ocfs2_xattr_header
) +
4832 count
* sizeof(struct ocfs2_xattr_entry
);
4833 max_free
= OCFS2_XATTR_BUCKET_SIZE
-
4834 le16_to_cpu(xh
->xh_name_value_len
) - header_size
;
4836 mlog_bug_on_msg(header_size
> blocksize
, "bucket %llu has header size "
4837 "of %u which exceed block size\n",
4838 (unsigned long long)bucket_blkno(xs
->bucket
),
4841 if (xi
->value
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
4842 value_size
= OCFS2_XATTR_ROOT_SIZE
;
4844 value_size
= OCFS2_XATTR_SIZE(xi
->value_len
);
4847 need
= sizeof(struct ocfs2_xattr_entry
) +
4848 OCFS2_XATTR_SIZE(name_len
) + value_size
;
4850 need
= value_size
+ OCFS2_XATTR_SIZE(name_len
);
4853 * We only replace the old value if the new length is smaller
4854 * than the old one. Otherwise we will allocate new space in the
4855 * bucket to store it.
4858 if (ocfs2_xattr_is_local(xe
))
4859 old
= OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
4861 old
= OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE
);
4863 if (old
>= value_size
)
4867 free
= xh_free_start
- header_size
;
4869 * We need to make sure the new name/value pair
4870 * can exist in the same block.
4872 if (xh_free_start
% blocksize
< need
)
4873 free
-= xh_free_start
% blocksize
;
4875 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4876 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4877 " %u\n", xs
->not_found
,
4878 (unsigned long long)bucket_blkno(xs
->bucket
),
4879 free
, need
, max_free
, le16_to_cpu(xh
->xh_free_start
),
4880 le16_to_cpu(xh
->xh_name_value_len
));
4884 count
== ocfs2_xattr_max_xe_in_bucket(inode
->i_sb
))) {
4885 if (need
<= max_free
&&
4886 count
< ocfs2_xattr_max_xe_in_bucket(inode
->i_sb
)) {
4888 * We can create the space by defragment. Since only the
4889 * name/value will be moved, the xe shouldn't be changed
4892 ret
= ocfs2_defrag_xattr_bucket(inode
, ctxt
->handle
,
4899 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4900 free
= xh_free_start
- header_size
;
4901 if (xh_free_start
% blocksize
< need
)
4902 free
-= xh_free_start
% blocksize
;
4907 mlog(0, "Can't get enough space for xattr insert by "
4908 "defragment. Need %u bytes, but we have %d, so "
4909 "allocate new bucket for it.\n", need
, free
);
4913 * We have to add new buckets or clusters and one
4914 * allocation should leave us enough space for insert.
4919 * We do not allow for overlapping ranges between buckets. And
4920 * the maximum number of collisions we will allow for then is
4921 * one bucket's worth, so check it here whether we need to
4922 * add a new bucket for the insert.
4924 ret
= ocfs2_check_xattr_bucket_collision(inode
,
4932 ret
= ocfs2_add_new_xattr_bucket(inode
,
4934 xs
->bucket
->bu_bhs
[0],
4941 ocfs2_xattr_bucket_relse(xs
->bucket
);
4943 ret
= ocfs2_xattr_index_block_find(inode
, xs
->xattr_bh
,
4946 if (ret
&& ret
!= -ENODATA
)
4948 xs
->not_found
= ret
;
4954 ret
= ocfs2_xattr_set_in_bucket(inode
, xi
, xs
, ctxt
);
4960 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
4961 struct ocfs2_xattr_bucket
*bucket
,
4965 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
4967 struct ocfs2_xattr_entry
*xe
;
4968 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4969 struct ocfs2_xattr_set_ctxt ctxt
= {NULL
, NULL
,};
4971 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
4973 ctxt
.handle
= ocfs2_start_trans(osb
, OCFS2_REMOVE_EXTENT_CREDITS
);
4974 if (IS_ERR(ctxt
.handle
)) {
4975 ret
= PTR_ERR(ctxt
.handle
);
4980 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
4981 xe
= &xh
->xh_entries
[i
];
4982 if (ocfs2_xattr_is_local(xe
))
4985 ret
= ocfs2_xattr_bucket_value_truncate(inode
,
4994 ret
= ocfs2_commit_trans(osb
, ctxt
.handle
);
4995 ocfs2_schedule_truncate_log_flush(osb
, 1);
4996 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
5001 static int ocfs2_delete_xattr_index_block(struct inode
*inode
,
5002 struct buffer_head
*xb_bh
)
5004 struct ocfs2_xattr_block
*xb
=
5005 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
5006 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
5008 u32 name_hash
= UINT_MAX
, e_cpos
, num_clusters
;
5011 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
5014 while (name_hash
> 0) {
5015 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
5016 &e_cpos
, &num_clusters
, el
);
5022 ret
= ocfs2_iterate_xattr_buckets(inode
, p_blkno
, num_clusters
,
5023 ocfs2_delete_xattr_in_bucket
,
5030 ret
= ocfs2_rm_xattr_cluster(inode
, xb_bh
,
5031 p_blkno
, e_cpos
, num_clusters
);
5040 name_hash
= e_cpos
- 1;
5048 * 'security' attributes support
5050 static size_t ocfs2_xattr_security_list(struct inode
*inode
, char *list
,
5051 size_t list_size
, const char *name
,
5054 const size_t prefix_len
= XATTR_SECURITY_PREFIX_LEN
;
5055 const size_t total_len
= prefix_len
+ name_len
+ 1;
5057 if (list
&& total_len
<= list_size
) {
5058 memcpy(list
, XATTR_SECURITY_PREFIX
, prefix_len
);
5059 memcpy(list
+ prefix_len
, name
, name_len
);
5060 list
[prefix_len
+ name_len
] = '\0';
5065 static int ocfs2_xattr_security_get(struct inode
*inode
, const char *name
,
5066 void *buffer
, size_t size
)
5068 if (strcmp(name
, "") == 0)
5070 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_SECURITY
, name
,
5074 static int ocfs2_xattr_security_set(struct inode
*inode
, const char *name
,
5075 const void *value
, size_t size
, int flags
)
5077 if (strcmp(name
, "") == 0)
5080 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_SECURITY
, name
, value
,
5084 int ocfs2_init_security_get(struct inode
*inode
,
5086 struct ocfs2_security_xattr_info
*si
)
5088 return security_inode_init_security(inode
, dir
, &si
->name
, &si
->value
,
5092 int ocfs2_init_security_set(handle_t
*handle
,
5093 struct inode
*inode
,
5094 struct buffer_head
*di_bh
,
5095 struct ocfs2_security_xattr_info
*si
,
5096 struct ocfs2_alloc_context
*xattr_ac
,
5097 struct ocfs2_alloc_context
*data_ac
)
5099 return ocfs2_xattr_set_handle(handle
, inode
, di_bh
,
5100 OCFS2_XATTR_INDEX_SECURITY
,
5101 si
->name
, si
->value
, si
->value_len
, 0,
5105 struct xattr_handler ocfs2_xattr_security_handler
= {
5106 .prefix
= XATTR_SECURITY_PREFIX
,
5107 .list
= ocfs2_xattr_security_list
,
5108 .get
= ocfs2_xattr_security_get
,
5109 .set
= ocfs2_xattr_security_set
,
5113 * 'trusted' attributes support
5115 static size_t ocfs2_xattr_trusted_list(struct inode
*inode
, char *list
,
5116 size_t list_size
, const char *name
,
5119 const size_t prefix_len
= XATTR_TRUSTED_PREFIX_LEN
;
5120 const size_t total_len
= prefix_len
+ name_len
+ 1;
5122 if (list
&& total_len
<= list_size
) {
5123 memcpy(list
, XATTR_TRUSTED_PREFIX
, prefix_len
);
5124 memcpy(list
+ prefix_len
, name
, name_len
);
5125 list
[prefix_len
+ name_len
] = '\0';
5130 static int ocfs2_xattr_trusted_get(struct inode
*inode
, const char *name
,
5131 void *buffer
, size_t size
)
5133 if (strcmp(name
, "") == 0)
5135 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_TRUSTED
, name
,
5139 static int ocfs2_xattr_trusted_set(struct inode
*inode
, const char *name
,
5140 const void *value
, size_t size
, int flags
)
5142 if (strcmp(name
, "") == 0)
5145 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_TRUSTED
, name
, value
,
5149 struct xattr_handler ocfs2_xattr_trusted_handler
= {
5150 .prefix
= XATTR_TRUSTED_PREFIX
,
5151 .list
= ocfs2_xattr_trusted_list
,
5152 .get
= ocfs2_xattr_trusted_get
,
5153 .set
= ocfs2_xattr_trusted_set
,
5157 * 'user' attributes support
5159 static size_t ocfs2_xattr_user_list(struct inode
*inode
, char *list
,
5160 size_t list_size
, const char *name
,
5163 const size_t prefix_len
= XATTR_USER_PREFIX_LEN
;
5164 const size_t total_len
= prefix_len
+ name_len
+ 1;
5165 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5167 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
5170 if (list
&& total_len
<= list_size
) {
5171 memcpy(list
, XATTR_USER_PREFIX
, prefix_len
);
5172 memcpy(list
+ prefix_len
, name
, name_len
);
5173 list
[prefix_len
+ name_len
] = '\0';
5178 static int ocfs2_xattr_user_get(struct inode
*inode
, const char *name
,
5179 void *buffer
, size_t size
)
5181 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5183 if (strcmp(name
, "") == 0)
5185 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
5187 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_USER
, name
,
5191 static int ocfs2_xattr_user_set(struct inode
*inode
, const char *name
,
5192 const void *value
, size_t size
, int flags
)
5194 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5196 if (strcmp(name
, "") == 0)
5198 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
5201 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_USER
, name
, value
,
5205 struct xattr_handler ocfs2_xattr_user_handler
= {
5206 .prefix
= XATTR_USER_PREFIX
,
5207 .list
= ocfs2_xattr_user_list
,
5208 .get
= ocfs2_xattr_user_get
,
5209 .set
= ocfs2_xattr_user_set
,