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) \
87 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
88 - sizeof(struct ocfs2_xattr_block) \
89 - sizeof(struct ocfs2_xattr_header) \
92 static struct ocfs2_xattr_def_value_root def_xv
= {
93 .xv
.xr_list
.l_count
= cpu_to_le16(1),
96 struct xattr_handler
*ocfs2_xattr_handlers
[] = {
97 &ocfs2_xattr_user_handler
,
98 #ifdef CONFIG_OCFS2_FS_POSIX_ACL
99 &ocfs2_xattr_acl_access_handler
,
100 &ocfs2_xattr_acl_default_handler
,
102 &ocfs2_xattr_trusted_handler
,
103 &ocfs2_xattr_security_handler
,
107 static struct xattr_handler
*ocfs2_xattr_handler_map
[OCFS2_XATTR_MAX
] = {
108 [OCFS2_XATTR_INDEX_USER
] = &ocfs2_xattr_user_handler
,
109 #ifdef CONFIG_OCFS2_FS_POSIX_ACL
110 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS
]
111 = &ocfs2_xattr_acl_access_handler
,
112 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
]
113 = &ocfs2_xattr_acl_default_handler
,
115 [OCFS2_XATTR_INDEX_TRUSTED
] = &ocfs2_xattr_trusted_handler
,
116 [OCFS2_XATTR_INDEX_SECURITY
] = &ocfs2_xattr_security_handler
,
119 struct ocfs2_xattr_info
{
126 struct ocfs2_xattr_search
{
127 struct buffer_head
*inode_bh
;
129 * xattr_bh point to the block buffer head which has extended attribute
130 * when extended attribute in inode, xattr_bh is equal to inode_bh.
132 struct buffer_head
*xattr_bh
;
133 struct ocfs2_xattr_header
*header
;
134 struct ocfs2_xattr_bucket
*bucket
;
137 struct ocfs2_xattr_entry
*here
;
141 static int ocfs2_xattr_bucket_get_name_value(struct inode
*inode
,
142 struct ocfs2_xattr_header
*xh
,
147 static int ocfs2_xattr_block_find(struct inode
*inode
,
150 struct ocfs2_xattr_search
*xs
);
151 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
152 struct buffer_head
*root_bh
,
155 struct ocfs2_xattr_search
*xs
);
157 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
158 struct ocfs2_xattr_tree_root
*xt
,
162 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
163 struct ocfs2_xattr_search
*xs
,
164 struct ocfs2_xattr_set_ctxt
*ctxt
);
166 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
167 struct ocfs2_xattr_info
*xi
,
168 struct ocfs2_xattr_search
*xs
,
169 struct ocfs2_xattr_set_ctxt
*ctxt
);
171 static int ocfs2_delete_xattr_index_block(struct inode
*inode
,
172 struct buffer_head
*xb_bh
);
173 static int ocfs2_mv_xattr_buckets(struct inode
*inode
, handle_t
*handle
,
174 u64 src_blk
, u64 last_blk
, u64 to_blk
,
175 unsigned int start_bucket
,
178 static inline u16
ocfs2_xattr_buckets_per_cluster(struct ocfs2_super
*osb
)
180 return (1 << osb
->s_clustersize_bits
) / OCFS2_XATTR_BUCKET_SIZE
;
183 static inline u16
ocfs2_blocks_per_xattr_bucket(struct super_block
*sb
)
185 return OCFS2_XATTR_BUCKET_SIZE
/ (1 << sb
->s_blocksize_bits
);
188 static inline u16
ocfs2_xattr_max_xe_in_bucket(struct super_block
*sb
)
190 u16 len
= sb
->s_blocksize
-
191 offsetof(struct ocfs2_xattr_header
, xh_entries
);
193 return len
/ sizeof(struct ocfs2_xattr_entry
);
196 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
197 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
198 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
200 static struct ocfs2_xattr_bucket
*ocfs2_xattr_bucket_new(struct inode
*inode
)
202 struct ocfs2_xattr_bucket
*bucket
;
203 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
205 BUG_ON(blks
> OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
);
207 bucket
= kzalloc(sizeof(struct ocfs2_xattr_bucket
), GFP_NOFS
);
209 bucket
->bu_inode
= inode
;
210 bucket
->bu_blocks
= blks
;
216 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket
*bucket
)
220 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
221 brelse(bucket
->bu_bhs
[i
]);
222 bucket
->bu_bhs
[i
] = NULL
;
226 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket
*bucket
)
229 ocfs2_xattr_bucket_relse(bucket
);
230 bucket
->bu_inode
= NULL
;
236 * A bucket that has never been written to disk doesn't need to be
237 * read. We just need the buffer_heads. Don't call this for
238 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
241 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
246 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
247 bucket
->bu_bhs
[i
] = sb_getblk(bucket
->bu_inode
->i_sb
,
249 if (!bucket
->bu_bhs
[i
]) {
255 if (!ocfs2_buffer_uptodate(bucket
->bu_inode
,
257 ocfs2_set_new_buffer_uptodate(bucket
->bu_inode
,
262 ocfs2_xattr_bucket_relse(bucket
);
266 /* Read the xattr bucket at xb_blkno */
267 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
272 rc
= ocfs2_read_blocks(bucket
->bu_inode
, xb_blkno
,
273 bucket
->bu_blocks
, bucket
->bu_bhs
, 0,
276 ocfs2_xattr_bucket_relse(bucket
);
280 static int ocfs2_xattr_bucket_journal_access(handle_t
*handle
,
281 struct ocfs2_xattr_bucket
*bucket
,
286 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
287 rc
= ocfs2_journal_access(handle
, bucket
->bu_inode
,
288 bucket
->bu_bhs
[i
], type
);
298 static void ocfs2_xattr_bucket_journal_dirty(handle_t
*handle
,
299 struct ocfs2_xattr_bucket
*bucket
)
303 for (i
= 0; i
< bucket
->bu_blocks
; i
++)
304 ocfs2_journal_dirty(handle
, bucket
->bu_bhs
[i
]);
307 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket
*dest
,
308 struct ocfs2_xattr_bucket
*src
)
311 int blocksize
= src
->bu_inode
->i_sb
->s_blocksize
;
313 BUG_ON(dest
->bu_blocks
!= src
->bu_blocks
);
314 BUG_ON(dest
->bu_inode
!= src
->bu_inode
);
316 for (i
= 0; i
< src
->bu_blocks
; i
++) {
317 memcpy(bucket_block(dest
, i
), bucket_block(src
, i
),
322 static int ocfs2_validate_xattr_block(struct super_block
*sb
,
323 struct buffer_head
*bh
)
325 struct ocfs2_xattr_block
*xb
=
326 (struct ocfs2_xattr_block
*)bh
->b_data
;
328 mlog(0, "Validating xattr block %llu\n",
329 (unsigned long long)bh
->b_blocknr
);
331 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
333 "Extended attribute block #%llu has bad "
335 (unsigned long long)bh
->b_blocknr
, 7,
340 if (le64_to_cpu(xb
->xb_blkno
) != bh
->b_blocknr
) {
342 "Extended attribute block #%llu has an "
343 "invalid xb_blkno of %llu",
344 (unsigned long long)bh
->b_blocknr
,
345 (unsigned long long)le64_to_cpu(xb
->xb_blkno
));
349 if (le32_to_cpu(xb
->xb_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
351 "Extended attribute block #%llu has an invalid "
352 "xb_fs_generation of #%u",
353 (unsigned long long)bh
->b_blocknr
,
354 le32_to_cpu(xb
->xb_fs_generation
));
361 static int ocfs2_read_xattr_block(struct inode
*inode
, u64 xb_blkno
,
362 struct buffer_head
**bh
)
365 struct buffer_head
*tmp
= *bh
;
367 rc
= ocfs2_read_block(inode
, xb_blkno
, &tmp
,
368 ocfs2_validate_xattr_block
);
370 /* If ocfs2_read_block() got us a new bh, pass it up. */
377 static inline const char *ocfs2_xattr_prefix(int name_index
)
379 struct xattr_handler
*handler
= NULL
;
381 if (name_index
> 0 && name_index
< OCFS2_XATTR_MAX
)
382 handler
= ocfs2_xattr_handler_map
[name_index
];
384 return handler
? handler
->prefix
: NULL
;
387 static u32
ocfs2_xattr_name_hash(struct inode
*inode
,
391 /* Get hash value of uuid from super block */
392 u32 hash
= OCFS2_SB(inode
->i_sb
)->uuid_hash
;
395 /* hash extended attribute name */
396 for (i
= 0; i
< name_len
; i
++) {
397 hash
= (hash
<< OCFS2_HASH_SHIFT
) ^
398 (hash
>> (8*sizeof(hash
) - OCFS2_HASH_SHIFT
)) ^
406 * ocfs2_xattr_hash_entry()
408 * Compute the hash of an extended attribute.
410 static void ocfs2_xattr_hash_entry(struct inode
*inode
,
411 struct ocfs2_xattr_header
*header
,
412 struct ocfs2_xattr_entry
*entry
)
415 char *name
= (char *)header
+ le16_to_cpu(entry
->xe_name_offset
);
417 hash
= ocfs2_xattr_name_hash(inode
, name
, entry
->xe_name_len
);
418 entry
->xe_name_hash
= cpu_to_le32(hash
);
423 static int ocfs2_xattr_entry_real_size(int name_len
, size_t value_len
)
427 if (value_len
<= OCFS2_XATTR_INLINE_SIZE
)
428 size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_SIZE(value_len
);
430 size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
431 size
+= sizeof(struct ocfs2_xattr_entry
);
436 int ocfs2_calc_security_init(struct inode
*dir
,
437 struct ocfs2_security_xattr_info
*si
,
440 struct ocfs2_alloc_context
**xattr_ac
)
443 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
444 int s_size
= ocfs2_xattr_entry_real_size(strlen(si
->name
),
448 * The max space of security xattr taken inline is
449 * 256(name) + 80(value) + 16(entry) = 352 bytes,
450 * So reserve one metadata block for it is ok.
452 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
||
453 s_size
> OCFS2_XATTR_FREE_IN_IBODY
) {
454 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, xattr_ac
);
459 *xattr_credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
462 /* reserve clusters for xattr value which will be set in B tree*/
463 if (si
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
464 *want_clusters
+= ocfs2_clusters_for_bytes(dir
->i_sb
,
469 int ocfs2_calc_xattr_init(struct inode
*dir
,
470 struct buffer_head
*dir_bh
,
472 struct ocfs2_security_xattr_info
*si
,
475 struct ocfs2_alloc_context
**xattr_ac
)
478 struct ocfs2_super
*osb
= OCFS2_SB(dir
->i_sb
);
484 s_size
= ocfs2_xattr_entry_real_size(strlen(si
->name
),
487 if (osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
) {
488 acl_len
= ocfs2_xattr_get_nolock(dir
, dir_bh
,
489 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT
,
492 a_size
= ocfs2_xattr_entry_real_size(0, acl_len
);
495 } else if (acl_len
!= 0 && acl_len
!= -ENODATA
) {
501 if (!(s_size
+ a_size
))
505 * The max space of security xattr taken inline is
506 * 256(name) + 80(value) + 16(entry) = 352 bytes,
507 * The max space of acl xattr taken inline is
508 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
509 * when blocksize = 512, may reserve one more cluser for
510 * xattr bucket, otherwise reserve one metadata block
513 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
||
514 (s_size
+ a_size
) > OCFS2_XATTR_FREE_IN_IBODY
) {
515 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, xattr_ac
);
520 *xattr_credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
523 if (dir
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
&&
524 (s_size
+ a_size
) > OCFS2_XATTR_FREE_IN_BLOCK(dir
)) {
526 *xattr_credits
+= ocfs2_blocks_per_xattr_bucket(dir
->i_sb
);
529 /* reserve clusters for xattr value which will be set in B tree*/
530 if (si
->enable
&& si
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
531 *want_clusters
+= ocfs2_clusters_for_bytes(dir
->i_sb
,
533 if (osb
->s_mount_opt
& OCFS2_MOUNT_POSIX_ACL
&&
534 acl_len
> OCFS2_XATTR_INLINE_SIZE
) {
535 *want_clusters
+= ocfs2_clusters_for_bytes(dir
->i_sb
, acl_len
);
537 *want_clusters
+= ocfs2_clusters_for_bytes(dir
->i_sb
,
544 static int ocfs2_xattr_extend_allocation(struct inode
*inode
,
546 struct buffer_head
*xattr_bh
,
547 struct ocfs2_xattr_value_root
*xv
,
548 struct ocfs2_xattr_set_ctxt
*ctxt
)
551 handle_t
*handle
= ctxt
->handle
;
552 enum ocfs2_alloc_restarted why
;
553 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
554 u32 prev_clusters
, logical_start
= le32_to_cpu(xv
->xr_clusters
);
555 struct ocfs2_extent_tree et
;
557 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add
);
559 ocfs2_init_xattr_value_extent_tree(&et
, inode
, xattr_bh
, xv
);
561 status
= ocfs2_journal_access(handle
, inode
, xattr_bh
,
562 OCFS2_JOURNAL_ACCESS_WRITE
);
568 prev_clusters
= le32_to_cpu(xv
->xr_clusters
);
569 status
= ocfs2_add_clusters_in_btree(osb
,
584 status
= ocfs2_journal_dirty(handle
, xattr_bh
);
590 clusters_to_add
-= le32_to_cpu(xv
->xr_clusters
) - prev_clusters
;
593 * We should have already allocated enough space before the transaction,
594 * so no need to restart.
596 BUG_ON(why
!= RESTART_NONE
|| clusters_to_add
);
603 static int __ocfs2_remove_xattr_range(struct inode
*inode
,
604 struct buffer_head
*root_bh
,
605 struct ocfs2_xattr_value_root
*xv
,
606 u32 cpos
, u32 phys_cpos
, u32 len
,
607 struct ocfs2_xattr_set_ctxt
*ctxt
)
610 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
611 handle_t
*handle
= ctxt
->handle
;
612 struct ocfs2_extent_tree et
;
614 ocfs2_init_xattr_value_extent_tree(&et
, inode
, root_bh
, xv
);
616 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
617 OCFS2_JOURNAL_ACCESS_WRITE
);
623 ret
= ocfs2_remove_extent(inode
, &et
, cpos
, len
, handle
, ctxt
->meta_ac
,
630 le32_add_cpu(&xv
->xr_clusters
, -len
);
632 ret
= ocfs2_journal_dirty(handle
, root_bh
);
638 ret
= ocfs2_cache_cluster_dealloc(&ctxt
->dealloc
, phys_blkno
, len
);
646 static int ocfs2_xattr_shrink_size(struct inode
*inode
,
649 struct buffer_head
*root_bh
,
650 struct ocfs2_xattr_value_root
*xv
,
651 struct ocfs2_xattr_set_ctxt
*ctxt
)
654 u32 trunc_len
, cpos
, phys_cpos
, alloc_size
;
657 if (old_clusters
<= new_clusters
)
661 trunc_len
= old_clusters
- new_clusters
;
663 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &phys_cpos
,
664 &alloc_size
, &xv
->xr_list
);
670 if (alloc_size
> trunc_len
)
671 alloc_size
= trunc_len
;
673 ret
= __ocfs2_remove_xattr_range(inode
, root_bh
, xv
, cpos
,
674 phys_cpos
, alloc_size
,
681 block
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
682 ocfs2_remove_xattr_clusters_from_cache(inode
, block
,
685 trunc_len
-= alloc_size
;
692 static int ocfs2_xattr_value_truncate(struct inode
*inode
,
693 struct buffer_head
*root_bh
,
694 struct ocfs2_xattr_value_root
*xv
,
696 struct ocfs2_xattr_set_ctxt
*ctxt
)
699 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, len
);
700 u32 old_clusters
= le32_to_cpu(xv
->xr_clusters
);
702 if (new_clusters
== old_clusters
)
705 if (new_clusters
> old_clusters
)
706 ret
= ocfs2_xattr_extend_allocation(inode
,
707 new_clusters
- old_clusters
,
710 ret
= ocfs2_xattr_shrink_size(inode
,
711 old_clusters
, new_clusters
,
717 static int ocfs2_xattr_list_entry(char *buffer
, size_t size
,
718 size_t *result
, const char *prefix
,
719 const char *name
, int name_len
)
721 char *p
= buffer
+ *result
;
722 int prefix_len
= strlen(prefix
);
723 int total_len
= prefix_len
+ name_len
+ 1;
725 *result
+= total_len
;
727 /* we are just looking for how big our buffer needs to be */
734 memcpy(p
, prefix
, prefix_len
);
735 memcpy(p
+ prefix_len
, name
, name_len
);
736 p
[prefix_len
+ name_len
] = '\0';
741 static int ocfs2_xattr_list_entries(struct inode
*inode
,
742 struct ocfs2_xattr_header
*header
,
743 char *buffer
, size_t buffer_size
)
747 const char *prefix
, *name
;
749 for (i
= 0 ; i
< le16_to_cpu(header
->xh_count
); i
++) {
750 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
751 type
= ocfs2_xattr_get_type(entry
);
752 prefix
= ocfs2_xattr_prefix(type
);
755 name
= (const char *)header
+
756 le16_to_cpu(entry
->xe_name_offset
);
758 ret
= ocfs2_xattr_list_entry(buffer
, buffer_size
,
759 &result
, prefix
, name
,
769 static int ocfs2_xattr_ibody_list(struct inode
*inode
,
770 struct ocfs2_dinode
*di
,
774 struct ocfs2_xattr_header
*header
= NULL
;
775 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
778 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
781 header
= (struct ocfs2_xattr_header
*)
782 ((void *)di
+ inode
->i_sb
->s_blocksize
-
783 le16_to_cpu(di
->i_xattr_inline_size
));
785 ret
= ocfs2_xattr_list_entries(inode
, header
, buffer
, buffer_size
);
790 static int ocfs2_xattr_block_list(struct inode
*inode
,
791 struct ocfs2_dinode
*di
,
795 struct buffer_head
*blk_bh
= NULL
;
796 struct ocfs2_xattr_block
*xb
;
799 if (!di
->i_xattr_loc
)
802 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
809 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
810 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
811 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
812 ret
= ocfs2_xattr_list_entries(inode
, header
,
813 buffer
, buffer_size
);
815 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
816 ret
= ocfs2_xattr_tree_list_index_block(inode
, xt
,
817 buffer
, buffer_size
);
825 ssize_t
ocfs2_listxattr(struct dentry
*dentry
,
829 int ret
= 0, i_ret
= 0, b_ret
= 0;
830 struct buffer_head
*di_bh
= NULL
;
831 struct ocfs2_dinode
*di
= NULL
;
832 struct ocfs2_inode_info
*oi
= OCFS2_I(dentry
->d_inode
);
834 if (!ocfs2_supports_xattr(OCFS2_SB(dentry
->d_sb
)))
837 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
840 ret
= ocfs2_inode_lock(dentry
->d_inode
, &di_bh
, 0);
846 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
848 down_read(&oi
->ip_xattr_sem
);
849 i_ret
= ocfs2_xattr_ibody_list(dentry
->d_inode
, di
, buffer
, size
);
857 b_ret
= ocfs2_xattr_block_list(dentry
->d_inode
, di
,
862 up_read(&oi
->ip_xattr_sem
);
863 ocfs2_inode_unlock(dentry
->d_inode
, 0);
867 return i_ret
+ b_ret
;
870 static int ocfs2_xattr_find_entry(int name_index
,
872 struct ocfs2_xattr_search
*xs
)
874 struct ocfs2_xattr_entry
*entry
;
881 name_len
= strlen(name
);
883 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
884 cmp
= name_index
- ocfs2_xattr_get_type(entry
);
886 cmp
= name_len
- entry
->xe_name_len
;
888 cmp
= memcmp(name
, (xs
->base
+
889 le16_to_cpu(entry
->xe_name_offset
)),
897 return cmp
? -ENODATA
: 0;
900 static int ocfs2_xattr_get_value_outside(struct inode
*inode
,
901 struct ocfs2_xattr_value_root
*xv
,
905 u32 cpos
, p_cluster
, num_clusters
, bpc
, clusters
;
908 size_t cplen
, blocksize
;
909 struct buffer_head
*bh
= NULL
;
910 struct ocfs2_extent_list
*el
;
913 clusters
= le32_to_cpu(xv
->xr_clusters
);
914 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
915 blocksize
= inode
->i_sb
->s_blocksize
;
918 while (cpos
< clusters
) {
919 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
926 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
927 /* Copy ocfs2_xattr_value */
928 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
929 ret
= ocfs2_read_block(inode
, blkno
, &bh
, NULL
);
935 cplen
= len
>= blocksize
? blocksize
: len
;
936 memcpy(buffer
, bh
->b_data
, cplen
);
945 cpos
+= num_clusters
;
951 static int ocfs2_xattr_ibody_get(struct inode
*inode
,
956 struct ocfs2_xattr_search
*xs
)
958 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
959 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
960 struct ocfs2_xattr_value_root
*xv
;
964 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
967 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
968 xs
->header
= (struct ocfs2_xattr_header
*)
969 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
970 xs
->base
= (void *)xs
->header
;
971 xs
->here
= xs
->header
->xh_entries
;
973 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
976 size
= le64_to_cpu(xs
->here
->xe_value_size
);
978 if (size
> buffer_size
)
980 if (ocfs2_xattr_is_local(xs
->here
)) {
981 memcpy(buffer
, (void *)xs
->base
+
982 le16_to_cpu(xs
->here
->xe_name_offset
) +
983 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
), size
);
985 xv
= (struct ocfs2_xattr_value_root
*)
986 (xs
->base
+ le16_to_cpu(
987 xs
->here
->xe_name_offset
) +
988 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
));
989 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
1001 static int ocfs2_xattr_block_get(struct inode
*inode
,
1006 struct ocfs2_xattr_search
*xs
)
1008 struct ocfs2_xattr_block
*xb
;
1009 struct ocfs2_xattr_value_root
*xv
;
1011 int ret
= -ENODATA
, name_offset
, name_len
, block_off
, i
;
1013 xs
->bucket
= ocfs2_xattr_bucket_new(inode
);
1020 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, xs
);
1026 if (xs
->not_found
) {
1031 xb
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
1032 size
= le64_to_cpu(xs
->here
->xe_value_size
);
1035 if (size
> buffer_size
)
1038 name_offset
= le16_to_cpu(xs
->here
->xe_name_offset
);
1039 name_len
= OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
);
1040 i
= xs
->here
- xs
->header
->xh_entries
;
1042 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
1043 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
1044 bucket_xh(xs
->bucket
),
1048 xs
->base
= bucket_block(xs
->bucket
, block_off
);
1050 if (ocfs2_xattr_is_local(xs
->here
)) {
1051 memcpy(buffer
, (void *)xs
->base
+
1052 name_offset
+ name_len
, size
);
1054 xv
= (struct ocfs2_xattr_value_root
*)
1055 (xs
->base
+ name_offset
+ name_len
);
1056 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
1066 ocfs2_xattr_bucket_free(xs
->bucket
);
1068 brelse(xs
->xattr_bh
);
1069 xs
->xattr_bh
= NULL
;
1073 int ocfs2_xattr_get_nolock(struct inode
*inode
,
1074 struct buffer_head
*di_bh
,
1081 struct ocfs2_dinode
*di
= NULL
;
1082 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1083 struct ocfs2_xattr_search xis
= {
1084 .not_found
= -ENODATA
,
1086 struct ocfs2_xattr_search xbs
= {
1087 .not_found
= -ENODATA
,
1090 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
1093 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1096 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
1097 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1099 down_read(&oi
->ip_xattr_sem
);
1100 ret
= ocfs2_xattr_ibody_get(inode
, name_index
, name
, buffer
,
1102 if (ret
== -ENODATA
&& di
->i_xattr_loc
)
1103 ret
= ocfs2_xattr_block_get(inode
, name_index
, name
, buffer
,
1105 up_read(&oi
->ip_xattr_sem
);
1110 /* ocfs2_xattr_get()
1112 * Copy an extended attribute into the buffer provided.
1113 * Buffer is NULL to compute the size of buffer required.
1115 static int ocfs2_xattr_get(struct inode
*inode
,
1122 struct buffer_head
*di_bh
= NULL
;
1124 ret
= ocfs2_inode_lock(inode
, &di_bh
, 0);
1129 ret
= ocfs2_xattr_get_nolock(inode
, di_bh
, name_index
,
1130 name
, buffer
, buffer_size
);
1132 ocfs2_inode_unlock(inode
, 0);
1139 static int __ocfs2_xattr_set_value_outside(struct inode
*inode
,
1141 struct ocfs2_xattr_value_root
*xv
,
1145 int ret
= 0, i
, cp_len
, credits
;
1146 u16 blocksize
= inode
->i_sb
->s_blocksize
;
1147 u32 p_cluster
, num_clusters
;
1148 u32 cpos
= 0, bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
1149 u32 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, value_len
);
1151 struct buffer_head
*bh
= NULL
;
1153 BUG_ON(clusters
> le32_to_cpu(xv
->xr_clusters
));
1156 * In __ocfs2_xattr_set_value_outside has already been dirtied,
1157 * so we don't need to worry about whether ocfs2_extend_trans
1158 * will create a new transactio for us or not.
1160 credits
= clusters
* bpc
;
1161 ret
= ocfs2_extend_trans(handle
, credits
);
1167 while (cpos
< clusters
) {
1168 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
1169 &num_clusters
, &xv
->xr_list
);
1175 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
1177 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
1178 ret
= ocfs2_read_block(inode
, blkno
, &bh
, NULL
);
1184 ret
= ocfs2_journal_access(handle
,
1187 OCFS2_JOURNAL_ACCESS_WRITE
);
1193 cp_len
= value_len
> blocksize
? blocksize
: value_len
;
1194 memcpy(bh
->b_data
, value
, cp_len
);
1195 value_len
-= cp_len
;
1197 if (cp_len
< blocksize
)
1198 memset(bh
->b_data
+ cp_len
, 0,
1199 blocksize
- cp_len
);
1201 ret
= ocfs2_journal_dirty(handle
, bh
);
1210 * XXX: do we need to empty all the following
1211 * blocks in this cluster?
1216 cpos
+= num_clusters
;
1224 static int ocfs2_xattr_cleanup(struct inode
*inode
,
1226 struct ocfs2_xattr_info
*xi
,
1227 struct ocfs2_xattr_search
*xs
,
1231 size_t name_len
= strlen(xi
->name
);
1232 void *val
= xs
->base
+ offs
;
1233 size_t size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1235 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1236 OCFS2_JOURNAL_ACCESS_WRITE
);
1241 /* Decrease xattr count */
1242 le16_add_cpu(&xs
->header
->xh_count
, -1);
1243 /* Remove the xattr entry and tree root which has already be set*/
1244 memset((void *)xs
->here
, 0, sizeof(struct ocfs2_xattr_entry
));
1245 memset(val
, 0, size
);
1247 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1254 static int ocfs2_xattr_update_entry(struct inode
*inode
,
1256 struct ocfs2_xattr_info
*xi
,
1257 struct ocfs2_xattr_search
*xs
,
1262 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1263 OCFS2_JOURNAL_ACCESS_WRITE
);
1269 xs
->here
->xe_name_offset
= cpu_to_le16(offs
);
1270 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1271 if (xi
->value_len
<= OCFS2_XATTR_INLINE_SIZE
)
1272 ocfs2_xattr_set_local(xs
->here
, 1);
1274 ocfs2_xattr_set_local(xs
->here
, 0);
1275 ocfs2_xattr_hash_entry(inode
, xs
->header
, xs
->here
);
1277 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1285 * ocfs2_xattr_set_value_outside()
1287 * Set large size value in B tree.
1289 static int ocfs2_xattr_set_value_outside(struct inode
*inode
,
1290 struct ocfs2_xattr_info
*xi
,
1291 struct ocfs2_xattr_search
*xs
,
1292 struct ocfs2_xattr_set_ctxt
*ctxt
,
1295 size_t name_len
= strlen(xi
->name
);
1296 void *val
= xs
->base
+ offs
;
1297 struct ocfs2_xattr_value_root
*xv
= NULL
;
1298 size_t size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1301 memset(val
, 0, size
);
1302 memcpy(val
, xi
->name
, name_len
);
1303 xv
= (struct ocfs2_xattr_value_root
*)
1304 (val
+ OCFS2_XATTR_SIZE(name_len
));
1305 xv
->xr_clusters
= 0;
1306 xv
->xr_last_eb_blk
= 0;
1307 xv
->xr_list
.l_tree_depth
= 0;
1308 xv
->xr_list
.l_count
= cpu_to_le16(1);
1309 xv
->xr_list
.l_next_free_rec
= 0;
1311 ret
= ocfs2_xattr_value_truncate(inode
, xs
->xattr_bh
, xv
,
1312 xi
->value_len
, ctxt
);
1317 ret
= ocfs2_xattr_update_entry(inode
, ctxt
->handle
, xi
, xs
, offs
);
1322 ret
= __ocfs2_xattr_set_value_outside(inode
, ctxt
->handle
, xv
,
1323 xi
->value
, xi
->value_len
);
1331 * ocfs2_xattr_set_entry_local()
1333 * Set, replace or remove extended attribute in local.
1335 static void ocfs2_xattr_set_entry_local(struct inode
*inode
,
1336 struct ocfs2_xattr_info
*xi
,
1337 struct ocfs2_xattr_search
*xs
,
1338 struct ocfs2_xattr_entry
*last
,
1341 size_t name_len
= strlen(xi
->name
);
1344 if (xi
->value
&& xs
->not_found
) {
1345 /* Insert the new xattr entry. */
1346 le16_add_cpu(&xs
->header
->xh_count
, 1);
1347 ocfs2_xattr_set_type(last
, xi
->name_index
);
1348 ocfs2_xattr_set_local(last
, 1);
1349 last
->xe_name_len
= name_len
;
1355 first_val
= xs
->base
+ min_offs
;
1356 offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1357 val
= xs
->base
+ offs
;
1359 if (le64_to_cpu(xs
->here
->xe_value_size
) >
1360 OCFS2_XATTR_INLINE_SIZE
)
1361 size
= OCFS2_XATTR_SIZE(name_len
) +
1362 OCFS2_XATTR_ROOT_SIZE
;
1364 size
= OCFS2_XATTR_SIZE(name_len
) +
1365 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1367 if (xi
->value
&& size
== OCFS2_XATTR_SIZE(name_len
) +
1368 OCFS2_XATTR_SIZE(xi
->value_len
)) {
1369 /* The old and the new value have the
1370 same size. Just replace the value. */
1371 ocfs2_xattr_set_local(xs
->here
, 1);
1372 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1373 /* Clear value bytes. */
1374 memset(val
+ OCFS2_XATTR_SIZE(name_len
),
1376 OCFS2_XATTR_SIZE(xi
->value_len
));
1377 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
1382 /* Remove the old name+value. */
1383 memmove(first_val
+ size
, first_val
, val
- first_val
);
1384 memset(first_val
, 0, size
);
1385 xs
->here
->xe_name_hash
= 0;
1386 xs
->here
->xe_name_offset
= 0;
1387 ocfs2_xattr_set_local(xs
->here
, 1);
1388 xs
->here
->xe_value_size
= 0;
1392 /* Adjust all value offsets. */
1393 last
= xs
->header
->xh_entries
;
1394 for (i
= 0 ; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1395 size_t o
= le16_to_cpu(last
->xe_name_offset
);
1398 last
->xe_name_offset
= cpu_to_le16(o
+ size
);
1403 /* Remove the old entry. */
1405 memmove(xs
->here
, xs
->here
+ 1,
1406 (void *)last
- (void *)xs
->here
);
1407 memset(last
, 0, sizeof(struct ocfs2_xattr_entry
));
1408 le16_add_cpu(&xs
->header
->xh_count
, -1);
1412 /* Insert the new name+value. */
1413 size_t size
= OCFS2_XATTR_SIZE(name_len
) +
1414 OCFS2_XATTR_SIZE(xi
->value_len
);
1415 void *val
= xs
->base
+ min_offs
- size
;
1417 xs
->here
->xe_name_offset
= cpu_to_le16(min_offs
- size
);
1418 memset(val
, 0, size
);
1419 memcpy(val
, xi
->name
, name_len
);
1420 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
1423 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1424 ocfs2_xattr_set_local(xs
->here
, 1);
1425 ocfs2_xattr_hash_entry(inode
, xs
->header
, xs
->here
);
1432 * ocfs2_xattr_set_entry()
1434 * Set extended attribute entry into inode or block.
1436 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1437 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1438 * then set value in B tree with set_value_outside().
1440 static int ocfs2_xattr_set_entry(struct inode
*inode
,
1441 struct ocfs2_xattr_info
*xi
,
1442 struct ocfs2_xattr_search
*xs
,
1443 struct ocfs2_xattr_set_ctxt
*ctxt
,
1446 struct ocfs2_xattr_entry
*last
;
1447 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1448 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1449 size_t min_offs
= xs
->end
- xs
->base
, name_len
= strlen(xi
->name
);
1451 handle_t
*handle
= ctxt
->handle
;
1453 struct ocfs2_xattr_info xi_l
= {
1454 .name_index
= xi
->name_index
,
1457 .value_len
= xi
->value_len
,
1460 /* Compute min_offs, last and free space. */
1461 last
= xs
->header
->xh_entries
;
1463 for (i
= 0 ; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1464 size_t offs
= le16_to_cpu(last
->xe_name_offset
);
1465 if (offs
< min_offs
)
1470 free
= min_offs
- ((void *)last
- xs
->base
) - sizeof(__u32
);
1474 if (!xs
->not_found
) {
1476 if (ocfs2_xattr_is_local(xs
->here
))
1477 size
= OCFS2_XATTR_SIZE(name_len
) +
1478 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1480 size
= OCFS2_XATTR_SIZE(name_len
) +
1481 OCFS2_XATTR_ROOT_SIZE
;
1482 free
+= (size
+ sizeof(struct ocfs2_xattr_entry
));
1484 /* Check free space in inode or block */
1485 if (xi
->value
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1486 if (free
< sizeof(struct ocfs2_xattr_entry
) +
1487 OCFS2_XATTR_SIZE(name_len
) +
1488 OCFS2_XATTR_ROOT_SIZE
) {
1492 size_l
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1493 xi_l
.value
= (void *)&def_xv
;
1494 xi_l
.value_len
= OCFS2_XATTR_ROOT_SIZE
;
1495 } else if (xi
->value
) {
1496 if (free
< sizeof(struct ocfs2_xattr_entry
) +
1497 OCFS2_XATTR_SIZE(name_len
) +
1498 OCFS2_XATTR_SIZE(xi
->value_len
)) {
1504 if (!xs
->not_found
) {
1505 /* For existing extended attribute */
1506 size_t size
= OCFS2_XATTR_SIZE(name_len
) +
1507 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1508 size_t offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1509 void *val
= xs
->base
+ offs
;
1511 if (ocfs2_xattr_is_local(xs
->here
) && size
== size_l
) {
1512 /* Replace existing local xattr with tree root */
1513 ret
= ocfs2_xattr_set_value_outside(inode
, xi
, xs
,
1518 } else if (!ocfs2_xattr_is_local(xs
->here
)) {
1519 /* For existing xattr which has value outside */
1520 struct ocfs2_xattr_value_root
*xv
= NULL
;
1521 xv
= (struct ocfs2_xattr_value_root
*)(val
+
1522 OCFS2_XATTR_SIZE(name_len
));
1524 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1526 * If new value need set outside also,
1527 * first truncate old value to new value,
1528 * then set new value with set_value_outside().
1530 ret
= ocfs2_xattr_value_truncate(inode
,
1540 ret
= ocfs2_xattr_update_entry(inode
,
1550 ret
= __ocfs2_xattr_set_value_outside(inode
,
1560 * If new value need set in local,
1561 * just trucate old value to zero.
1563 ret
= ocfs2_xattr_value_truncate(inode
,
1574 ret
= ocfs2_journal_access(handle
, inode
, xs
->inode_bh
,
1575 OCFS2_JOURNAL_ACCESS_WRITE
);
1581 if (!(flag
& OCFS2_INLINE_XATTR_FL
)) {
1582 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1583 OCFS2_JOURNAL_ACCESS_WRITE
);
1591 * Set value in local, include set tree root in local.
1592 * This is the first step for value size >INLINE_SIZE.
1594 ocfs2_xattr_set_entry_local(inode
, &xi_l
, xs
, last
, min_offs
);
1596 if (!(flag
& OCFS2_INLINE_XATTR_FL
)) {
1597 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1604 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) &&
1605 (flag
& OCFS2_INLINE_XATTR_FL
)) {
1606 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1607 unsigned int xattrsize
= osb
->s_xattr_inline_size
;
1610 * Adjust extent record count or inline data size
1611 * to reserve space for extended attribute.
1613 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1614 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
1615 le16_add_cpu(&idata
->id_count
, -xattrsize
);
1616 } else if (!(ocfs2_inode_is_fast_symlink(inode
))) {
1617 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
1618 le16_add_cpu(&el
->l_count
, -(xattrsize
/
1619 sizeof(struct ocfs2_extent_rec
)));
1621 di
->i_xattr_inline_size
= cpu_to_le16(xattrsize
);
1623 /* Update xattr flag */
1624 spin_lock(&oi
->ip_lock
);
1625 oi
->ip_dyn_features
|= flag
;
1626 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1627 spin_unlock(&oi
->ip_lock
);
1628 /* Update inode ctime */
1629 inode
->i_ctime
= CURRENT_TIME
;
1630 di
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
1631 di
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
1633 ret
= ocfs2_journal_dirty(handle
, xs
->inode_bh
);
1637 if (!ret
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1639 * Set value outside in B tree.
1640 * This is the second step for value size > INLINE_SIZE.
1642 size_t offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1643 ret
= ocfs2_xattr_set_value_outside(inode
, xi
, xs
, ctxt
, offs
);
1649 * If set value outside failed, we have to clean
1650 * the junk tree root we have already set in local.
1652 ret2
= ocfs2_xattr_cleanup(inode
, ctxt
->handle
,
1662 static int ocfs2_remove_value_outside(struct inode
*inode
,
1663 struct buffer_head
*bh
,
1664 struct ocfs2_xattr_header
*header
)
1667 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1668 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, };
1670 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
1672 ctxt
.handle
= ocfs2_start_trans(osb
,
1673 ocfs2_remove_extent_credits(osb
->sb
));
1674 if (IS_ERR(ctxt
.handle
)) {
1675 ret
= PTR_ERR(ctxt
.handle
);
1680 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
1681 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
1683 if (!ocfs2_xattr_is_local(entry
)) {
1684 struct ocfs2_xattr_value_root
*xv
;
1687 val
= (void *)header
+
1688 le16_to_cpu(entry
->xe_name_offset
);
1689 xv
= (struct ocfs2_xattr_value_root
*)
1690 (val
+ OCFS2_XATTR_SIZE(entry
->xe_name_len
));
1691 ret
= ocfs2_xattr_value_truncate(inode
, bh
, xv
,
1700 ocfs2_commit_trans(osb
, ctxt
.handle
);
1701 ocfs2_schedule_truncate_log_flush(osb
, 1);
1702 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
1707 static int ocfs2_xattr_ibody_remove(struct inode
*inode
,
1708 struct buffer_head
*di_bh
)
1711 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1712 struct ocfs2_xattr_header
*header
;
1715 header
= (struct ocfs2_xattr_header
*)
1716 ((void *)di
+ inode
->i_sb
->s_blocksize
-
1717 le16_to_cpu(di
->i_xattr_inline_size
));
1719 ret
= ocfs2_remove_value_outside(inode
, di_bh
, header
);
1724 static int ocfs2_xattr_block_remove(struct inode
*inode
,
1725 struct buffer_head
*blk_bh
)
1727 struct ocfs2_xattr_block
*xb
;
1730 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1731 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1732 struct ocfs2_xattr_header
*header
= &(xb
->xb_attrs
.xb_header
);
1733 ret
= ocfs2_remove_value_outside(inode
, blk_bh
, header
);
1735 ret
= ocfs2_delete_xattr_index_block(inode
, blk_bh
);
1740 static int ocfs2_xattr_free_block(struct inode
*inode
,
1743 struct inode
*xb_alloc_inode
;
1744 struct buffer_head
*xb_alloc_bh
= NULL
;
1745 struct buffer_head
*blk_bh
= NULL
;
1746 struct ocfs2_xattr_block
*xb
;
1747 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1753 ret
= ocfs2_read_xattr_block(inode
, block
, &blk_bh
);
1759 ret
= ocfs2_xattr_block_remove(inode
, blk_bh
);
1765 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1766 blk
= le64_to_cpu(xb
->xb_blkno
);
1767 bit
= le16_to_cpu(xb
->xb_suballoc_bit
);
1768 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1770 xb_alloc_inode
= ocfs2_get_system_file_inode(osb
,
1771 EXTENT_ALLOC_SYSTEM_INODE
,
1772 le16_to_cpu(xb
->xb_suballoc_slot
));
1773 if (!xb_alloc_inode
) {
1778 mutex_lock(&xb_alloc_inode
->i_mutex
);
1780 ret
= ocfs2_inode_lock(xb_alloc_inode
, &xb_alloc_bh
, 1);
1786 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
1787 if (IS_ERR(handle
)) {
1788 ret
= PTR_ERR(handle
);
1793 ret
= ocfs2_free_suballoc_bits(handle
, xb_alloc_inode
, xb_alloc_bh
,
1798 ocfs2_commit_trans(osb
, handle
);
1800 ocfs2_inode_unlock(xb_alloc_inode
, 1);
1801 brelse(xb_alloc_bh
);
1803 mutex_unlock(&xb_alloc_inode
->i_mutex
);
1804 iput(xb_alloc_inode
);
1811 * ocfs2_xattr_remove()
1813 * Free extended attribute resources associated with this inode.
1815 int ocfs2_xattr_remove(struct inode
*inode
, struct buffer_head
*di_bh
)
1817 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1818 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1822 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
1825 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1828 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
1829 ret
= ocfs2_xattr_ibody_remove(inode
, di_bh
);
1836 if (di
->i_xattr_loc
) {
1837 ret
= ocfs2_xattr_free_block(inode
,
1838 le64_to_cpu(di
->i_xattr_loc
));
1845 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1846 OCFS2_INODE_UPDATE_CREDITS
);
1847 if (IS_ERR(handle
)) {
1848 ret
= PTR_ERR(handle
);
1852 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
1853 OCFS2_JOURNAL_ACCESS_WRITE
);
1859 di
->i_xattr_loc
= 0;
1861 spin_lock(&oi
->ip_lock
);
1862 oi
->ip_dyn_features
&= ~(OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
);
1863 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1864 spin_unlock(&oi
->ip_lock
);
1866 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1870 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1875 static int ocfs2_xattr_has_space_inline(struct inode
*inode
,
1876 struct ocfs2_dinode
*di
)
1878 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1879 unsigned int xattrsize
= OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
;
1882 if (xattrsize
< OCFS2_MIN_XATTR_INLINE_SIZE
)
1885 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1886 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
1887 free
= le16_to_cpu(idata
->id_count
) - le64_to_cpu(di
->i_size
);
1888 } else if (ocfs2_inode_is_fast_symlink(inode
)) {
1889 free
= ocfs2_fast_symlink_chars(inode
->i_sb
) -
1890 le64_to_cpu(di
->i_size
);
1892 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
1893 free
= (le16_to_cpu(el
->l_count
) -
1894 le16_to_cpu(el
->l_next_free_rec
)) *
1895 sizeof(struct ocfs2_extent_rec
);
1897 if (free
>= xattrsize
)
1904 * ocfs2_xattr_ibody_find()
1906 * Find extended attribute in inode block and
1907 * fill search info into struct ocfs2_xattr_search.
1909 static int ocfs2_xattr_ibody_find(struct inode
*inode
,
1912 struct ocfs2_xattr_search
*xs
)
1914 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1915 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1919 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
1922 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
1923 down_read(&oi
->ip_alloc_sem
);
1924 has_space
= ocfs2_xattr_has_space_inline(inode
, di
);
1925 up_read(&oi
->ip_alloc_sem
);
1930 xs
->xattr_bh
= xs
->inode_bh
;
1931 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
1932 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)
1933 xs
->header
= (struct ocfs2_xattr_header
*)
1934 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
1936 xs
->header
= (struct ocfs2_xattr_header
*)
1937 (xs
->end
- OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
);
1938 xs
->base
= (void *)xs
->header
;
1939 xs
->here
= xs
->header
->xh_entries
;
1941 /* Find the named attribute. */
1942 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
1943 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1944 if (ret
&& ret
!= -ENODATA
)
1946 xs
->not_found
= ret
;
1953 * ocfs2_xattr_ibody_set()
1955 * Set, replace or remove an extended attribute into inode block.
1958 static int ocfs2_xattr_ibody_set(struct inode
*inode
,
1959 struct ocfs2_xattr_info
*xi
,
1960 struct ocfs2_xattr_search
*xs
,
1961 struct ocfs2_xattr_set_ctxt
*ctxt
)
1963 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1964 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1967 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
1970 down_write(&oi
->ip_alloc_sem
);
1971 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
1972 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
1978 ret
= ocfs2_xattr_set_entry(inode
, xi
, xs
, ctxt
,
1979 (OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
));
1981 up_write(&oi
->ip_alloc_sem
);
1987 * ocfs2_xattr_block_find()
1989 * Find extended attribute in external block and
1990 * fill search info into struct ocfs2_xattr_search.
1992 static int ocfs2_xattr_block_find(struct inode
*inode
,
1995 struct ocfs2_xattr_search
*xs
)
1997 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1998 struct buffer_head
*blk_bh
= NULL
;
1999 struct ocfs2_xattr_block
*xb
;
2002 if (!di
->i_xattr_loc
)
2005 ret
= ocfs2_read_xattr_block(inode
, le64_to_cpu(di
->i_xattr_loc
),
2012 xs
->xattr_bh
= blk_bh
;
2013 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
2015 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2016 xs
->header
= &xb
->xb_attrs
.xb_header
;
2017 xs
->base
= (void *)xs
->header
;
2018 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
2019 xs
->here
= xs
->header
->xh_entries
;
2021 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
2023 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
2027 if (ret
&& ret
!= -ENODATA
) {
2028 xs
->xattr_bh
= NULL
;
2031 xs
->not_found
= ret
;
2040 * ocfs2_xattr_block_set()
2042 * Set, replace or remove an extended attribute into external block.
2045 static int ocfs2_xattr_block_set(struct inode
*inode
,
2046 struct ocfs2_xattr_info
*xi
,
2047 struct ocfs2_xattr_search
*xs
,
2048 struct ocfs2_xattr_set_ctxt
*ctxt
)
2050 struct buffer_head
*new_bh
= NULL
;
2051 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2052 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
2053 handle_t
*handle
= ctxt
->handle
;
2054 struct ocfs2_xattr_block
*xblk
= NULL
;
2055 u16 suballoc_bit_start
;
2060 if (!xs
->xattr_bh
) {
2061 ret
= ocfs2_journal_access(handle
, inode
, xs
->inode_bh
,
2062 OCFS2_JOURNAL_ACCESS_CREATE
);
2068 ret
= ocfs2_claim_metadata(osb
, handle
, ctxt
->meta_ac
, 1,
2069 &suballoc_bit_start
, &num_got
,
2076 new_bh
= sb_getblk(inode
->i_sb
, first_blkno
);
2077 ocfs2_set_new_buffer_uptodate(inode
, new_bh
);
2079 ret
= ocfs2_journal_access(handle
, inode
, new_bh
,
2080 OCFS2_JOURNAL_ACCESS_CREATE
);
2086 /* Initialize ocfs2_xattr_block */
2087 xs
->xattr_bh
= new_bh
;
2088 xblk
= (struct ocfs2_xattr_block
*)new_bh
->b_data
;
2089 memset(xblk
, 0, inode
->i_sb
->s_blocksize
);
2090 strcpy((void *)xblk
, OCFS2_XATTR_BLOCK_SIGNATURE
);
2091 xblk
->xb_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
2092 xblk
->xb_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
2093 xblk
->xb_fs_generation
= cpu_to_le32(osb
->fs_generation
);
2094 xblk
->xb_blkno
= cpu_to_le64(first_blkno
);
2096 xs
->header
= &xblk
->xb_attrs
.xb_header
;
2097 xs
->base
= (void *)xs
->header
;
2098 xs
->end
= (void *)xblk
+ inode
->i_sb
->s_blocksize
;
2099 xs
->here
= xs
->header
->xh_entries
;
2101 ret
= ocfs2_journal_dirty(handle
, new_bh
);
2106 di
->i_xattr_loc
= cpu_to_le64(first_blkno
);
2107 ocfs2_journal_dirty(handle
, xs
->inode_bh
);
2109 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
2111 if (!(le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2112 /* Set extended attribute into external block */
2113 ret
= ocfs2_xattr_set_entry(inode
, xi
, xs
, ctxt
,
2114 OCFS2_HAS_XATTR_FL
);
2115 if (!ret
|| ret
!= -ENOSPC
)
2118 ret
= ocfs2_xattr_create_index_block(inode
, xs
, ctxt
);
2123 ret
= ocfs2_xattr_set_entry_index_block(inode
, xi
, xs
, ctxt
);
2130 /* Check whether the new xattr can be inserted into the inode. */
2131 static int ocfs2_xattr_can_be_in_inode(struct inode
*inode
,
2132 struct ocfs2_xattr_info
*xi
,
2133 struct ocfs2_xattr_search
*xs
)
2136 struct ocfs2_xattr_entry
*last
;
2138 size_t min_offs
= xs
->end
- xs
->base
;
2143 last
= xs
->header
->xh_entries
;
2145 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
2146 size_t offs
= le16_to_cpu(last
->xe_name_offset
);
2147 if (offs
< min_offs
)
2152 free
= min_offs
- ((void *)last
- xs
->base
) - sizeof(__u32
);
2156 BUG_ON(!xs
->not_found
);
2158 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
2159 value_size
= OCFS2_XATTR_ROOT_SIZE
;
2161 value_size
= OCFS2_XATTR_SIZE(xi
->value_len
);
2163 if (free
>= sizeof(struct ocfs2_xattr_entry
) +
2164 OCFS2_XATTR_SIZE(strlen(xi
->name
)) + value_size
)
2170 static int ocfs2_calc_xattr_set_need(struct inode
*inode
,
2171 struct ocfs2_dinode
*di
,
2172 struct ocfs2_xattr_info
*xi
,
2173 struct ocfs2_xattr_search
*xis
,
2174 struct ocfs2_xattr_search
*xbs
,
2179 int ret
= 0, old_in_xb
= 0;
2180 int clusters_add
= 0, meta_add
= 0, credits
= 0;
2181 struct buffer_head
*bh
= NULL
;
2182 struct ocfs2_xattr_block
*xb
= NULL
;
2183 struct ocfs2_xattr_entry
*xe
= NULL
;
2184 struct ocfs2_xattr_value_root
*xv
= NULL
;
2186 int name_offset
, name_len
= 0;
2187 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
2191 if (xis
->not_found
&& xbs
->not_found
) {
2192 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2194 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2195 clusters_add
+= new_clusters
;
2196 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
2204 if (!xis
->not_found
) {
2206 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
2207 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
2209 credits
+= OCFS2_INODE_UPDATE_CREDITS
;
2211 int i
, block_off
= 0;
2212 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
2214 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
2215 name_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
2216 i
= xbs
->here
- xbs
->header
->xh_entries
;
2219 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
2220 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
2221 bucket_xh(xbs
->bucket
),
2224 base
= bucket_block(xbs
->bucket
, block_off
);
2225 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2228 credits
+= OCFS2_XATTR_BLOCK_UPDATE_CREDITS
;
2233 * delete a xattr doesn't need metadata and cluster allocation.
2234 * so just calculate the credits and return.
2236 * The credits for removing the value tree will be extended
2237 * by ocfs2_remove_extent itself.
2240 if (!ocfs2_xattr_is_local(xe
))
2241 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
);
2246 /* do cluster allocation guess first. */
2247 value_size
= le64_to_cpu(xe
->xe_value_size
);
2251 * In xattr set, we always try to set the xe in inode first,
2252 * so if it can be inserted into inode successfully, the old
2253 * one will be removed from the xattr block, and this xattr
2254 * will be inserted into inode as a new xattr in inode.
2256 if (ocfs2_xattr_can_be_in_inode(inode
, xi
, xis
)) {
2257 clusters_add
+= new_clusters
;
2258 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
) +
2259 OCFS2_INODE_UPDATE_CREDITS
;
2260 if (!ocfs2_xattr_is_local(xe
))
2261 credits
+= ocfs2_calc_extend_credits(
2269 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
2270 /* the new values will be stored outside. */
2271 u32 old_clusters
= 0;
2273 if (!ocfs2_xattr_is_local(xe
)) {
2274 old_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
,
2276 xv
= (struct ocfs2_xattr_value_root
*)
2277 (base
+ name_offset
+ name_len
);
2278 value_size
= OCFS2_XATTR_ROOT_SIZE
;
2282 if (old_clusters
>= new_clusters
) {
2283 credits
+= ocfs2_remove_extent_credits(inode
->i_sb
);
2286 meta_add
+= ocfs2_extend_meta_needed(&xv
->xr_list
);
2287 clusters_add
+= new_clusters
- old_clusters
;
2288 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
2292 if (value_size
>= OCFS2_XATTR_ROOT_SIZE
)
2297 * Now the new value will be stored inside. So if the new
2298 * value is smaller than the size of value root or the old
2299 * value, we don't need any allocation, otherwise we have
2300 * to guess metadata allocation.
2302 if ((ocfs2_xattr_is_local(xe
) && value_size
>= xi
->value_len
) ||
2303 (!ocfs2_xattr_is_local(xe
) &&
2304 OCFS2_XATTR_ROOT_SIZE
>= xi
->value_len
))
2309 /* calculate metadata allocation. */
2310 if (di
->i_xattr_loc
) {
2311 if (!xbs
->xattr_bh
) {
2312 ret
= ocfs2_read_xattr_block(inode
,
2313 le64_to_cpu(di
->i_xattr_loc
),
2320 xb
= (struct ocfs2_xattr_block
*)bh
->b_data
;
2322 xb
= (struct ocfs2_xattr_block
*)xbs
->xattr_bh
->b_data
;
2324 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
2325 struct ocfs2_extent_list
*el
=
2326 &xb
->xb_attrs
.xb_root
.xt_list
;
2327 meta_add
+= ocfs2_extend_meta_needed(el
);
2328 credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
2333 * This cluster will be used either for new bucket or for
2335 * If the cluster size is the same as the bucket size, one
2336 * more is needed since we may need to extend the bucket
2340 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2341 if (OCFS2_XATTR_BUCKET_SIZE
==
2342 OCFS2_SB(inode
->i_sb
)->s_clustersize
) {
2343 credits
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2348 credits
+= OCFS2_XATTR_BLOCK_CREATE_CREDITS
;
2352 *clusters_need
= clusters_add
;
2354 *meta_need
= meta_add
;
2356 *credits_need
= credits
;
2361 static int ocfs2_init_xattr_set_ctxt(struct inode
*inode
,
2362 struct ocfs2_dinode
*di
,
2363 struct ocfs2_xattr_info
*xi
,
2364 struct ocfs2_xattr_search
*xis
,
2365 struct ocfs2_xattr_search
*xbs
,
2366 struct ocfs2_xattr_set_ctxt
*ctxt
,
2369 int clusters_add
, meta_add
, ret
;
2370 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2372 memset(ctxt
, 0, sizeof(struct ocfs2_xattr_set_ctxt
));
2374 ocfs2_init_dealloc_ctxt(&ctxt
->dealloc
);
2376 ret
= ocfs2_calc_xattr_set_need(inode
, di
, xi
, xis
, xbs
,
2377 &clusters_add
, &meta_add
, credits
);
2383 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2384 "credits = %d\n", xi
->name
, meta_add
, clusters_add
, *credits
);
2387 ret
= ocfs2_reserve_new_metadata_blocks(osb
, meta_add
,
2396 ret
= ocfs2_reserve_clusters(osb
, clusters_add
, &ctxt
->data_ac
);
2402 if (ctxt
->meta_ac
) {
2403 ocfs2_free_alloc_context(ctxt
->meta_ac
);
2404 ctxt
->meta_ac
= NULL
;
2408 * We cannot have an error and a non null ctxt->data_ac.
2415 static int __ocfs2_xattr_set_handle(struct inode
*inode
,
2416 struct ocfs2_dinode
*di
,
2417 struct ocfs2_xattr_info
*xi
,
2418 struct ocfs2_xattr_search
*xis
,
2419 struct ocfs2_xattr_search
*xbs
,
2420 struct ocfs2_xattr_set_ctxt
*ctxt
)
2422 int ret
= 0, credits
, old_found
;
2425 /* Remove existing extended attribute */
2426 if (!xis
->not_found
)
2427 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
2428 else if (!xbs
->not_found
)
2429 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
2431 /* We always try to set extended attribute into inode first*/
2432 ret
= ocfs2_xattr_ibody_set(inode
, xi
, xis
, ctxt
);
2433 if (!ret
&& !xbs
->not_found
) {
2435 * If succeed and that extended attribute existing in
2436 * external block, then we will remove it.
2441 old_found
= xis
->not_found
;
2442 xis
->not_found
= -ENODATA
;
2443 ret
= ocfs2_calc_xattr_set_need(inode
,
2451 xis
->not_found
= old_found
;
2457 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
+
2458 ctxt
->handle
->h_buffer_credits
);
2463 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
2464 } else if (ret
== -ENOSPC
) {
2465 if (di
->i_xattr_loc
&& !xbs
->xattr_bh
) {
2466 ret
= ocfs2_xattr_block_find(inode
,
2472 old_found
= xis
->not_found
;
2473 xis
->not_found
= -ENODATA
;
2474 ret
= ocfs2_calc_xattr_set_need(inode
,
2482 xis
->not_found
= old_found
;
2488 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
+
2489 ctxt
->handle
->h_buffer_credits
);
2496 * If no space in inode, we will set extended attribute
2497 * into external block.
2499 ret
= ocfs2_xattr_block_set(inode
, xi
, xbs
, ctxt
);
2502 if (!xis
->not_found
) {
2504 * If succeed and that extended attribute
2505 * existing in inode, we will remove it.
2509 xbs
->not_found
= -ENODATA
;
2510 ret
= ocfs2_calc_xattr_set_need(inode
,
2523 ret
= ocfs2_extend_trans(ctxt
->handle
, credits
+
2524 ctxt
->handle
->h_buffer_credits
);
2529 ret
= ocfs2_xattr_ibody_set(inode
, xi
,
2540 * This function only called duing creating inode
2541 * for init security/acl xattrs of the new inode.
2542 * The xattrs could be put into ibody or extent block,
2543 * xattr bucket would not be use in this case.
2544 * transanction credits also be reserved in here.
2546 int ocfs2_xattr_set_handle(handle_t
*handle
,
2547 struct inode
*inode
,
2548 struct buffer_head
*di_bh
,
2554 struct ocfs2_alloc_context
*meta_ac
,
2555 struct ocfs2_alloc_context
*data_ac
)
2557 struct ocfs2_dinode
*di
;
2560 struct ocfs2_xattr_info xi
= {
2561 .name_index
= name_index
,
2564 .value_len
= value_len
,
2567 struct ocfs2_xattr_search xis
= {
2568 .not_found
= -ENODATA
,
2571 struct ocfs2_xattr_search xbs
= {
2572 .not_found
= -ENODATA
,
2575 struct ocfs2_xattr_set_ctxt ctxt
= {
2581 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
2584 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
2585 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2587 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2589 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
2592 if (xis
.not_found
) {
2593 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
2598 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
2601 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2602 brelse(xbs
.xattr_bh
);
2610 * Set, replace or remove an extended attribute for this inode.
2611 * value is NULL to remove an existing extended attribute, else either
2612 * create or replace an extended attribute.
2614 int ocfs2_xattr_set(struct inode
*inode
,
2621 struct buffer_head
*di_bh
= NULL
;
2622 struct ocfs2_dinode
*di
;
2624 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2625 struct inode
*tl_inode
= osb
->osb_tl_inode
;
2626 struct ocfs2_xattr_set_ctxt ctxt
= { NULL
, NULL
, };
2628 struct ocfs2_xattr_info xi
= {
2629 .name_index
= name_index
,
2632 .value_len
= value_len
,
2635 struct ocfs2_xattr_search xis
= {
2636 .not_found
= -ENODATA
,
2639 struct ocfs2_xattr_search xbs
= {
2640 .not_found
= -ENODATA
,
2643 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
2647 * Only xbs will be used on indexed trees. xis doesn't need a
2650 xbs
.bucket
= ocfs2_xattr_bucket_new(inode
);
2652 mlog_errno(-ENOMEM
);
2656 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
2659 goto cleanup_nolock
;
2661 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
2662 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2664 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2666 * Scan inode and external block to find the same name
2667 * extended attribute and collect search infomation.
2669 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
2672 if (xis
.not_found
) {
2673 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
2678 if (xis
.not_found
&& xbs
.not_found
) {
2680 if (flags
& XATTR_REPLACE
)
2687 if (flags
& XATTR_CREATE
)
2692 mutex_lock(&tl_inode
->i_mutex
);
2694 if (ocfs2_truncate_log_needs_flush(osb
)) {
2695 ret
= __ocfs2_flush_truncate_log(osb
);
2697 mutex_unlock(&tl_inode
->i_mutex
);
2702 mutex_unlock(&tl_inode
->i_mutex
);
2704 ret
= ocfs2_init_xattr_set_ctxt(inode
, di
, &xi
, &xis
,
2705 &xbs
, &ctxt
, &credits
);
2711 ctxt
.handle
= ocfs2_start_trans(osb
, credits
);
2712 if (IS_ERR(ctxt
.handle
)) {
2713 ret
= PTR_ERR(ctxt
.handle
);
2718 ret
= __ocfs2_xattr_set_handle(inode
, di
, &xi
, &xis
, &xbs
, &ctxt
);
2720 ocfs2_commit_trans(osb
, ctxt
.handle
);
2723 ocfs2_free_alloc_context(ctxt
.data_ac
);
2725 ocfs2_free_alloc_context(ctxt
.meta_ac
);
2726 if (ocfs2_dealloc_has_cluster(&ctxt
.dealloc
))
2727 ocfs2_schedule_truncate_log_flush(osb
, 1);
2728 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
2730 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2731 ocfs2_inode_unlock(inode
, 1);
2734 brelse(xbs
.xattr_bh
);
2735 ocfs2_xattr_bucket_free(xbs
.bucket
);
2741 * Find the xattr extent rec which may contains name_hash.
2742 * e_cpos will be the first name hash of the xattr rec.
2743 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2745 static int ocfs2_xattr_get_rec(struct inode
*inode
,
2750 struct ocfs2_extent_list
*el
)
2753 struct buffer_head
*eb_bh
= NULL
;
2754 struct ocfs2_extent_block
*eb
;
2755 struct ocfs2_extent_rec
*rec
= NULL
;
2758 if (el
->l_tree_depth
) {
2759 ret
= ocfs2_find_leaf(inode
, el
, name_hash
, &eb_bh
);
2765 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
2768 if (el
->l_tree_depth
) {
2769 ocfs2_error(inode
->i_sb
,
2770 "Inode %lu has non zero tree depth in "
2771 "xattr tree block %llu\n", inode
->i_ino
,
2772 (unsigned long long)eb_bh
->b_blocknr
);
2778 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
2779 rec
= &el
->l_recs
[i
];
2781 if (le32_to_cpu(rec
->e_cpos
) <= name_hash
) {
2782 e_blkno
= le64_to_cpu(rec
->e_blkno
);
2788 ocfs2_error(inode
->i_sb
, "Inode %lu has bad extent "
2789 "record (%u, %u, 0) in xattr", inode
->i_ino
,
2790 le32_to_cpu(rec
->e_cpos
),
2791 ocfs2_rec_clusters(el
, rec
));
2796 *p_blkno
= le64_to_cpu(rec
->e_blkno
);
2797 *num_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
2799 *e_cpos
= le32_to_cpu(rec
->e_cpos
);
2805 typedef int (xattr_bucket_func
)(struct inode
*inode
,
2806 struct ocfs2_xattr_bucket
*bucket
,
2809 static int ocfs2_find_xe_in_bucket(struct inode
*inode
,
2810 struct ocfs2_xattr_bucket
*bucket
,
2817 int i
, ret
= 0, cmp
= 1, block_off
, new_offset
;
2818 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
2819 size_t name_len
= strlen(name
);
2820 struct ocfs2_xattr_entry
*xe
= NULL
;
2824 * We don't use binary search in the bucket because there
2825 * may be multiple entries with the same name hash.
2827 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
2828 xe
= &xh
->xh_entries
[i
];
2830 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
))
2832 else if (name_hash
< le32_to_cpu(xe
->xe_name_hash
))
2835 cmp
= name_index
- ocfs2_xattr_get_type(xe
);
2837 cmp
= name_len
- xe
->xe_name_len
;
2841 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
2852 xe_name
= bucket_block(bucket
, block_off
) + new_offset
;
2853 if (!memcmp(name
, xe_name
, name_len
)) {
2865 * Find the specified xattr entry in a series of buckets.
2866 * This series start from p_blkno and last for num_clusters.
2867 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2868 * the num of the valid buckets.
2870 * Return the buffer_head this xattr should reside in. And if the xattr's
2871 * hash is in the gap of 2 buckets, return the lower bucket.
2873 static int ocfs2_xattr_bucket_find(struct inode
*inode
,
2880 struct ocfs2_xattr_search
*xs
)
2883 struct ocfs2_xattr_header
*xh
= NULL
;
2884 struct ocfs2_xattr_entry
*xe
= NULL
;
2886 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2887 int low_bucket
= 0, bucket
, high_bucket
;
2888 struct ocfs2_xattr_bucket
*search
;
2890 u64 blkno
, lower_blkno
= 0;
2892 search
= ocfs2_xattr_bucket_new(inode
);
2899 ret
= ocfs2_read_xattr_bucket(search
, p_blkno
);
2905 xh
= bucket_xh(search
);
2906 high_bucket
= le16_to_cpu(xh
->xh_num_buckets
) - 1;
2907 while (low_bucket
<= high_bucket
) {
2908 ocfs2_xattr_bucket_relse(search
);
2910 bucket
= (low_bucket
+ high_bucket
) / 2;
2911 blkno
= p_blkno
+ bucket
* blk_per_bucket
;
2912 ret
= ocfs2_read_xattr_bucket(search
, blkno
);
2918 xh
= bucket_xh(search
);
2919 xe
= &xh
->xh_entries
[0];
2920 if (name_hash
< le32_to_cpu(xe
->xe_name_hash
)) {
2921 high_bucket
= bucket
- 1;
2926 * Check whether the hash of the last entry in our
2927 * bucket is larger than the search one. for an empty
2928 * bucket, the last one is also the first one.
2931 xe
= &xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1];
2933 last_hash
= le32_to_cpu(xe
->xe_name_hash
);
2935 /* record lower_blkno which may be the insert place. */
2936 lower_blkno
= blkno
;
2938 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
)) {
2939 low_bucket
= bucket
+ 1;
2943 /* the searched xattr should reside in this bucket if exists. */
2944 ret
= ocfs2_find_xe_in_bucket(inode
, search
,
2945 name_index
, name
, name_hash
,
2955 * Record the bucket we have found.
2956 * When the xattr's hash value is in the gap of 2 buckets, we will
2957 * always set it to the previous bucket.
2960 lower_blkno
= p_blkno
;
2962 /* This should be in cache - we just read it during the search */
2963 ret
= ocfs2_read_xattr_bucket(xs
->bucket
, lower_blkno
);
2969 xs
->header
= bucket_xh(xs
->bucket
);
2970 xs
->base
= bucket_block(xs
->bucket
, 0);
2971 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
2974 xs
->here
= &xs
->header
->xh_entries
[index
];
2975 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name
,
2976 (unsigned long long)bucket_blkno(xs
->bucket
), index
);
2981 ocfs2_xattr_bucket_free(search
);
2985 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
2986 struct buffer_head
*root_bh
,
2989 struct ocfs2_xattr_search
*xs
)
2992 struct ocfs2_xattr_block
*xb
=
2993 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
2994 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
2995 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
2997 u32 first_hash
, num_clusters
= 0;
2998 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
3000 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
3003 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3004 name
, name_hash
, name_index
);
3006 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &first_hash
,
3013 BUG_ON(p_blkno
== 0 || num_clusters
== 0 || first_hash
> name_hash
);
3015 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3016 "in the rec is %u\n", num_clusters
, (unsigned long long)p_blkno
,
3019 ret
= ocfs2_xattr_bucket_find(inode
, name_index
, name
, name_hash
,
3020 p_blkno
, first_hash
, num_clusters
, xs
);
3026 static int ocfs2_iterate_xattr_buckets(struct inode
*inode
,
3029 xattr_bucket_func
*func
,
3033 u32 bpc
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
3034 u32 num_buckets
= clusters
* bpc
;
3035 struct ocfs2_xattr_bucket
*bucket
;
3037 bucket
= ocfs2_xattr_bucket_new(inode
);
3039 mlog_errno(-ENOMEM
);
3043 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3044 clusters
, (unsigned long long)blkno
);
3046 for (i
= 0; i
< num_buckets
; i
++, blkno
+= bucket
->bu_blocks
) {
3047 ret
= ocfs2_read_xattr_bucket(bucket
, blkno
);
3054 * The real bucket num in this series of blocks is stored
3055 * in the 1st bucket.
3058 num_buckets
= le16_to_cpu(bucket_xh(bucket
)->xh_num_buckets
);
3060 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3061 (unsigned long long)blkno
,
3062 le32_to_cpu(bucket_xh(bucket
)->xh_entries
[0].xe_name_hash
));
3064 ret
= func(inode
, bucket
, para
);
3067 /* Fall through to bucket_relse() */
3070 ocfs2_xattr_bucket_relse(bucket
);
3075 ocfs2_xattr_bucket_free(bucket
);
3079 struct ocfs2_xattr_tree_list
{
3085 static int ocfs2_xattr_bucket_get_name_value(struct inode
*inode
,
3086 struct ocfs2_xattr_header
*xh
,
3093 if (index
< 0 || index
>= le16_to_cpu(xh
->xh_count
))
3096 name_offset
= le16_to_cpu(xh
->xh_entries
[index
].xe_name_offset
);
3098 *block_off
= name_offset
>> inode
->i_sb
->s_blocksize_bits
;
3099 *new_offset
= name_offset
% inode
->i_sb
->s_blocksize
;
3104 static int ocfs2_list_xattr_bucket(struct inode
*inode
,
3105 struct ocfs2_xattr_bucket
*bucket
,
3109 struct ocfs2_xattr_tree_list
*xl
= (struct ocfs2_xattr_tree_list
*)para
;
3110 int i
, block_off
, new_offset
;
3111 const char *prefix
, *name
;
3113 for (i
= 0 ; i
< le16_to_cpu(bucket_xh(bucket
)->xh_count
); i
++) {
3114 struct ocfs2_xattr_entry
*entry
= &bucket_xh(bucket
)->xh_entries
[i
];
3115 type
= ocfs2_xattr_get_type(entry
);
3116 prefix
= ocfs2_xattr_prefix(type
);
3119 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
3127 name
= (const char *)bucket_block(bucket
, block_off
) +
3129 ret
= ocfs2_xattr_list_entry(xl
->buffer
,
3133 entry
->xe_name_len
);
3142 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
3143 struct ocfs2_xattr_tree_root
*xt
,
3147 struct ocfs2_extent_list
*el
= &xt
->xt_list
;
3149 u32 name_hash
= UINT_MAX
, e_cpos
= 0, num_clusters
= 0;
3151 struct ocfs2_xattr_tree_list xl
= {
3153 .buffer_size
= buffer_size
,
3157 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
3160 while (name_hash
> 0) {
3161 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
3162 &e_cpos
, &num_clusters
, el
);
3168 ret
= ocfs2_iterate_xattr_buckets(inode
, p_blkno
, num_clusters
,
3169 ocfs2_list_xattr_bucket
,
3179 name_hash
= e_cpos
- 1;
3187 static int cmp_xe(const void *a
, const void *b
)
3189 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
3190 u32 l_hash
= le32_to_cpu(l
->xe_name_hash
);
3191 u32 r_hash
= le32_to_cpu(r
->xe_name_hash
);
3193 if (l_hash
> r_hash
)
3195 if (l_hash
< r_hash
)
3200 static void swap_xe(void *a
, void *b
, int size
)
3202 struct ocfs2_xattr_entry
*l
= a
, *r
= b
, tmp
;
3205 memcpy(l
, r
, sizeof(struct ocfs2_xattr_entry
));
3206 memcpy(r
, &tmp
, sizeof(struct ocfs2_xattr_entry
));
3210 * When the ocfs2_xattr_block is filled up, new bucket will be created
3211 * and all the xattr entries will be moved to the new bucket.
3212 * The header goes at the start of the bucket, and the names+values are
3213 * filled from the end. This is why *target starts as the last buffer.
3214 * Note: we need to sort the entries since they are not saved in order
3215 * in the ocfs2_xattr_block.
3217 static void ocfs2_cp_xattr_block_to_bucket(struct inode
*inode
,
3218 struct buffer_head
*xb_bh
,
3219 struct ocfs2_xattr_bucket
*bucket
)
3221 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
3222 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3223 u16 offset
, size
, off_change
;
3224 struct ocfs2_xattr_entry
*xe
;
3225 struct ocfs2_xattr_block
*xb
=
3226 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
3227 struct ocfs2_xattr_header
*xb_xh
= &xb
->xb_attrs
.xb_header
;
3228 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
3229 u16 count
= le16_to_cpu(xb_xh
->xh_count
);
3230 char *src
= xb_bh
->b_data
;
3231 char *target
= bucket_block(bucket
, blks
- 1);
3233 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3234 (unsigned long long)xb_bh
->b_blocknr
,
3235 (unsigned long long)bucket_blkno(bucket
));
3237 for (i
= 0; i
< blks
; i
++)
3238 memset(bucket_block(bucket
, i
), 0, blocksize
);
3241 * Since the xe_name_offset is based on ocfs2_xattr_header,
3242 * there is a offset change corresponding to the change of
3243 * ocfs2_xattr_header's position.
3245 off_change
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
3246 xe
= &xb_xh
->xh_entries
[count
- 1];
3247 offset
= le16_to_cpu(xe
->xe_name_offset
) + off_change
;
3248 size
= blocksize
- offset
;
3250 /* copy all the names and values. */
3251 memcpy(target
+ offset
, src
+ offset
, size
);
3253 /* Init new header now. */
3254 xh
->xh_count
= xb_xh
->xh_count
;
3255 xh
->xh_num_buckets
= cpu_to_le16(1);
3256 xh
->xh_name_value_len
= cpu_to_le16(size
);
3257 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
- size
);
3259 /* copy all the entries. */
3260 target
= bucket_block(bucket
, 0);
3261 offset
= offsetof(struct ocfs2_xattr_header
, xh_entries
);
3262 size
= count
* sizeof(struct ocfs2_xattr_entry
);
3263 memcpy(target
+ offset
, (char *)xb_xh
+ offset
, size
);
3265 /* Change the xe offset for all the xe because of the move. */
3266 off_change
= OCFS2_XATTR_BUCKET_SIZE
- blocksize
+
3267 offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
3268 for (i
= 0; i
< count
; i
++)
3269 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
, off_change
);
3271 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3272 offset
, size
, off_change
);
3274 sort(target
+ offset
, count
, sizeof(struct ocfs2_xattr_entry
),
3279 * After we move xattr from block to index btree, we have to
3280 * update ocfs2_xattr_search to the new xe and base.
3282 * When the entry is in xattr block, xattr_bh indicates the storage place.
3283 * While if the entry is in index b-tree, "bucket" indicates the
3284 * real place of the xattr.
3286 static void ocfs2_xattr_update_xattr_search(struct inode
*inode
,
3287 struct ocfs2_xattr_search
*xs
,
3288 struct buffer_head
*old_bh
)
3290 char *buf
= old_bh
->b_data
;
3291 struct ocfs2_xattr_block
*old_xb
= (struct ocfs2_xattr_block
*)buf
;
3292 struct ocfs2_xattr_header
*old_xh
= &old_xb
->xb_attrs
.xb_header
;
3295 xs
->header
= bucket_xh(xs
->bucket
);
3296 xs
->base
= bucket_block(xs
->bucket
, 0);
3297 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
3302 i
= xs
->here
- old_xh
->xh_entries
;
3303 xs
->here
= &xs
->header
->xh_entries
[i
];
3306 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
3307 struct ocfs2_xattr_search
*xs
,
3308 struct ocfs2_xattr_set_ctxt
*ctxt
)
3313 handle_t
*handle
= ctxt
->handle
;
3314 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3315 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
3316 struct buffer_head
*xb_bh
= xs
->xattr_bh
;
3317 struct ocfs2_xattr_block
*xb
=
3318 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
3319 struct ocfs2_xattr_tree_root
*xr
;
3320 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
3322 mlog(0, "create xattr index block for %llu\n",
3323 (unsigned long long)xb_bh
->b_blocknr
);
3325 BUG_ON(xb_flags
& OCFS2_XATTR_INDEXED
);
3326 BUG_ON(!xs
->bucket
);
3330 * We can use this lock for now, and maybe move to a dedicated mutex
3331 * if performance becomes a problem later.
3333 down_write(&oi
->ip_alloc_sem
);
3335 ret
= ocfs2_journal_access(handle
, inode
, xb_bh
,
3336 OCFS2_JOURNAL_ACCESS_WRITE
);
3342 ret
= __ocfs2_claim_clusters(osb
, handle
, ctxt
->data_ac
,
3343 1, 1, &bit_off
, &len
);
3350 * The bucket may spread in many blocks, and
3351 * we will only touch the 1st block and the last block
3352 * in the whole bucket(one for entry and one for data).
3354 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
3356 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3357 (unsigned long long)blkno
);
3359 ret
= ocfs2_init_xattr_bucket(xs
->bucket
, blkno
);
3365 ret
= ocfs2_xattr_bucket_journal_access(handle
, xs
->bucket
,
3366 OCFS2_JOURNAL_ACCESS_CREATE
);
3372 ocfs2_cp_xattr_block_to_bucket(inode
, xb_bh
, xs
->bucket
);
3373 ocfs2_xattr_bucket_journal_dirty(handle
, xs
->bucket
);
3375 ocfs2_xattr_update_xattr_search(inode
, xs
, xb_bh
);
3377 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3378 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
3379 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
3381 xr
= &xb
->xb_attrs
.xb_root
;
3382 xr
->xt_clusters
= cpu_to_le32(1);
3383 xr
->xt_last_eb_blk
= 0;
3384 xr
->xt_list
.l_tree_depth
= 0;
3385 xr
->xt_list
.l_count
= cpu_to_le16(ocfs2_xattr_recs_per_xb(inode
->i_sb
));
3386 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
3388 xr
->xt_list
.l_recs
[0].e_cpos
= 0;
3389 xr
->xt_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
3390 xr
->xt_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
3392 xb
->xb_flags
= cpu_to_le16(xb_flags
| OCFS2_XATTR_INDEXED
);
3394 ocfs2_journal_dirty(handle
, xb_bh
);
3397 up_write(&oi
->ip_alloc_sem
);
3402 static int cmp_xe_offset(const void *a
, const void *b
)
3404 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
3405 u32 l_name_offset
= le16_to_cpu(l
->xe_name_offset
);
3406 u32 r_name_offset
= le16_to_cpu(r
->xe_name_offset
);
3408 if (l_name_offset
< r_name_offset
)
3410 if (l_name_offset
> r_name_offset
)
3416 * defrag a xattr bucket if we find that the bucket has some
3417 * holes beteen name/value pairs.
3418 * We will move all the name/value pairs to the end of the bucket
3419 * so that we can spare some space for insertion.
3421 static int ocfs2_defrag_xattr_bucket(struct inode
*inode
,
3423 struct ocfs2_xattr_bucket
*bucket
)
3426 size_t end
, offset
, len
, value_len
;
3427 struct ocfs2_xattr_header
*xh
;
3428 char *entries
, *buf
, *bucket_buf
= NULL
;
3429 u64 blkno
= bucket_blkno(bucket
);
3431 size_t blocksize
= inode
->i_sb
->s_blocksize
;
3432 struct ocfs2_xattr_entry
*xe
;
3435 * In order to make the operation more efficient and generic,
3436 * we copy all the blocks into a contiguous memory and do the
3437 * defragment there, so if anything is error, we will not touch
3440 bucket_buf
= kmalloc(OCFS2_XATTR_BUCKET_SIZE
, GFP_NOFS
);
3447 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
3448 memcpy(buf
, bucket_block(bucket
, i
), blocksize
);
3450 ret
= ocfs2_xattr_bucket_journal_access(handle
, bucket
,
3451 OCFS2_JOURNAL_ACCESS_WRITE
);
3457 xh
= (struct ocfs2_xattr_header
*)bucket_buf
;
3458 entries
= (char *)xh
->xh_entries
;
3459 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
3461 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3462 "xh_free_start = %u, xh_name_value_len = %u.\n",
3463 (unsigned long long)blkno
, le16_to_cpu(xh
->xh_count
),
3464 xh_free_start
, le16_to_cpu(xh
->xh_name_value_len
));
3467 * sort all the entries by their offset.
3468 * the largest will be the first, so that we can
3469 * move them to the end one by one.
3471 sort(entries
, le16_to_cpu(xh
->xh_count
),
3472 sizeof(struct ocfs2_xattr_entry
),
3473 cmp_xe_offset
, swap_xe
);
3475 /* Move all name/values to the end of the bucket. */
3476 xe
= xh
->xh_entries
;
3477 end
= OCFS2_XATTR_BUCKET_SIZE
;
3478 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, xe
++) {
3479 offset
= le16_to_cpu(xe
->xe_name_offset
);
3480 if (ocfs2_xattr_is_local(xe
))
3481 value_len
= OCFS2_XATTR_SIZE(
3482 le64_to_cpu(xe
->xe_value_size
));
3484 value_len
= OCFS2_XATTR_ROOT_SIZE
;
3485 len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
) + value_len
;
3488 * We must make sure that the name/value pair
3489 * exist in the same block. So adjust end to
3490 * the previous block end if needed.
3492 if (((end
- len
) / blocksize
!=
3493 (end
- 1) / blocksize
))
3494 end
= end
- end
% blocksize
;
3496 if (end
> offset
+ len
) {
3497 memmove(bucket_buf
+ end
- len
,
3498 bucket_buf
+ offset
, len
);
3499 xe
->xe_name_offset
= cpu_to_le16(end
- len
);
3502 mlog_bug_on_msg(end
< offset
+ len
, "Defrag check failed for "
3503 "bucket %llu\n", (unsigned long long)blkno
);
3508 mlog_bug_on_msg(xh_free_start
> end
, "Defrag check failed for "
3509 "bucket %llu\n", (unsigned long long)blkno
);
3511 if (xh_free_start
== end
)
3514 memset(bucket_buf
+ xh_free_start
, 0, end
- xh_free_start
);
3515 xh
->xh_free_start
= cpu_to_le16(end
);
3517 /* sort the entries by their name_hash. */
3518 sort(entries
, le16_to_cpu(xh
->xh_count
),
3519 sizeof(struct ocfs2_xattr_entry
),
3523 for (i
= 0; i
< bucket
->bu_blocks
; i
++, buf
+= blocksize
)
3524 memcpy(bucket_block(bucket
, i
), buf
, blocksize
);
3525 ocfs2_xattr_bucket_journal_dirty(handle
, bucket
);
3533 * prev_blkno points to the start of an existing extent. new_blkno
3534 * points to a newly allocated extent. Because we know each of our
3535 * clusters contains more than bucket, we can easily split one cluster
3536 * at a bucket boundary. So we take the last cluster of the existing
3537 * extent and split it down the middle. We move the last half of the
3538 * buckets in the last cluster of the existing extent over to the new
3541 * first_bh is the buffer at prev_blkno so we can update the existing
3542 * extent's bucket count. header_bh is the bucket were we were hoping
3543 * to insert our xattr. If the bucket move places the target in the new
3544 * extent, we'll update first_bh and header_bh after modifying the old
3547 * first_hash will be set as the 1st xe's name_hash in the new extent.
3549 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode
*inode
,
3551 struct ocfs2_xattr_bucket
*first
,
3552 struct ocfs2_xattr_bucket
*target
,
3558 struct super_block
*sb
= inode
->i_sb
;
3559 int blks_per_bucket
= ocfs2_blocks_per_xattr_bucket(sb
);
3560 int num_buckets
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb
));
3561 int to_move
= num_buckets
/ 2;
3563 u64 last_cluster_blkno
= bucket_blkno(first
) +
3564 ((num_clusters
- 1) * ocfs2_clusters_to_blocks(sb
, 1));
3566 BUG_ON(le16_to_cpu(bucket_xh(first
)->xh_num_buckets
) < num_buckets
);
3567 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
== OCFS2_SB(sb
)->s_clustersize
);
3569 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3570 (unsigned long long)last_cluster_blkno
, (unsigned long long)new_blkno
);
3572 ret
= ocfs2_mv_xattr_buckets(inode
, handle
, bucket_blkno(first
),
3573 last_cluster_blkno
, new_blkno
,
3574 to_move
, first_hash
);
3580 /* This is the first bucket that got moved */
3581 src_blkno
= last_cluster_blkno
+ (to_move
* blks_per_bucket
);
3584 * If the target bucket was part of the moved buckets, we need to
3585 * update first and target.
3587 if (bucket_blkno(target
) >= src_blkno
) {
3588 /* Find the block for the new target bucket */
3589 src_blkno
= new_blkno
+
3590 (bucket_blkno(target
) - src_blkno
);
3592 ocfs2_xattr_bucket_relse(first
);
3593 ocfs2_xattr_bucket_relse(target
);
3596 * These shouldn't fail - the buffers are in the
3597 * journal from ocfs2_cp_xattr_bucket().
3599 ret
= ocfs2_read_xattr_bucket(first
, new_blkno
);
3604 ret
= ocfs2_read_xattr_bucket(target
, src_blkno
);
3615 * Find the suitable pos when we divide a bucket into 2.
3616 * We have to make sure the xattrs with the same hash value exist
3617 * in the same bucket.
3619 * If this ocfs2_xattr_header covers more than one hash value, find a
3620 * place where the hash value changes. Try to find the most even split.
3621 * The most common case is that all entries have different hash values,
3622 * and the first check we make will find a place to split.
3624 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header
*xh
)
3626 struct ocfs2_xattr_entry
*entries
= xh
->xh_entries
;
3627 int count
= le16_to_cpu(xh
->xh_count
);
3628 int delta
, middle
= count
/ 2;
3631 * We start at the middle. Each step gets farther away in both
3632 * directions. We therefore hit the change in hash value
3633 * nearest to the middle. Note that this loop does not execute for
3636 for (delta
= 0; delta
< middle
; delta
++) {
3637 /* Let's check delta earlier than middle */
3638 if (cmp_xe(&entries
[middle
- delta
- 1],
3639 &entries
[middle
- delta
]))
3640 return middle
- delta
;
3642 /* For even counts, don't walk off the end */
3643 if ((middle
+ delta
+ 1) == count
)
3646 /* Now try delta past middle */
3647 if (cmp_xe(&entries
[middle
+ delta
],
3648 &entries
[middle
+ delta
+ 1]))
3649 return middle
+ delta
+ 1;
3652 /* Every entry had the same hash */
3657 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3658 * first_hash will record the 1st hash of the new bucket.
3660 * Normally half of the xattrs will be moved. But we have to make
3661 * sure that the xattrs with the same hash value are stored in the
3662 * same bucket. If all the xattrs in this bucket have the same hash
3663 * value, the new bucket will be initialized as an empty one and the
3664 * first_hash will be initialized as (hash_value+1).
3666 static int ocfs2_divide_xattr_bucket(struct inode
*inode
,
3671 int new_bucket_head
)
3674 int count
, start
, len
, name_value_len
= 0, xe_len
, name_offset
= 0;
3675 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
3676 struct ocfs2_xattr_header
*xh
;
3677 struct ocfs2_xattr_entry
*xe
;
3678 int blocksize
= inode
->i_sb
->s_blocksize
;
3680 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
3681 (unsigned long long)blk
, (unsigned long long)new_blk
);
3683 s_bucket
= ocfs2_xattr_bucket_new(inode
);
3684 t_bucket
= ocfs2_xattr_bucket_new(inode
);
3685 if (!s_bucket
|| !t_bucket
) {
3691 ret
= ocfs2_read_xattr_bucket(s_bucket
, blk
);
3697 ret
= ocfs2_xattr_bucket_journal_access(handle
, s_bucket
,
3698 OCFS2_JOURNAL_ACCESS_WRITE
);
3705 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3706 * there's no need to read it.
3708 ret
= ocfs2_init_xattr_bucket(t_bucket
, new_blk
);
3715 * Hey, if we're overwriting t_bucket, what difference does
3716 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
3717 * same part of ocfs2_cp_xattr_bucket().
3719 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
3721 OCFS2_JOURNAL_ACCESS_CREATE
:
3722 OCFS2_JOURNAL_ACCESS_WRITE
);
3728 xh
= bucket_xh(s_bucket
);
3729 count
= le16_to_cpu(xh
->xh_count
);
3730 start
= ocfs2_xattr_find_divide_pos(xh
);
3732 if (start
== count
) {
3733 xe
= &xh
->xh_entries
[start
-1];
3736 * initialized a new empty bucket here.
3737 * The hash value is set as one larger than
3738 * that of the last entry in the previous bucket.
3740 for (i
= 0; i
< t_bucket
->bu_blocks
; i
++)
3741 memset(bucket_block(t_bucket
, i
), 0, blocksize
);
3743 xh
= bucket_xh(t_bucket
);
3744 xh
->xh_free_start
= cpu_to_le16(blocksize
);
3745 xh
->xh_entries
[0].xe_name_hash
= xe
->xe_name_hash
;
3746 le32_add_cpu(&xh
->xh_entries
[0].xe_name_hash
, 1);
3748 goto set_num_buckets
;
3751 /* copy the whole bucket to the new first. */
3752 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
3754 /* update the new bucket. */
3755 xh
= bucket_xh(t_bucket
);
3758 * Calculate the total name/value len and xh_free_start for
3759 * the old bucket first.
3761 name_offset
= OCFS2_XATTR_BUCKET_SIZE
;
3763 for (i
= 0; i
< start
; i
++) {
3764 xe
= &xh
->xh_entries
[i
];
3765 xe_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3766 if (ocfs2_xattr_is_local(xe
))
3768 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3770 xe_len
+= OCFS2_XATTR_ROOT_SIZE
;
3771 name_value_len
+= xe_len
;
3772 if (le16_to_cpu(xe
->xe_name_offset
) < name_offset
)
3773 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3777 * Now begin the modification to the new bucket.
3779 * In the new bucket, We just move the xattr entry to the beginning
3780 * and don't touch the name/value. So there will be some holes in the
3781 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3784 xe
= &xh
->xh_entries
[start
];
3785 len
= sizeof(struct ocfs2_xattr_entry
) * (count
- start
);
3786 mlog(0, "mv xattr entry len %d from %d to %d\n", len
,
3787 (int)((char *)xe
- (char *)xh
),
3788 (int)((char *)xh
->xh_entries
- (char *)xh
));
3789 memmove((char *)xh
->xh_entries
, (char *)xe
, len
);
3790 xe
= &xh
->xh_entries
[count
- start
];
3791 len
= sizeof(struct ocfs2_xattr_entry
) * start
;
3792 memset((char *)xe
, 0, len
);
3794 le16_add_cpu(&xh
->xh_count
, -start
);
3795 le16_add_cpu(&xh
->xh_name_value_len
, -name_value_len
);
3797 /* Calculate xh_free_start for the new bucket. */
3798 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
3799 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
3800 xe
= &xh
->xh_entries
[i
];
3801 xe_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3802 if (ocfs2_xattr_is_local(xe
))
3804 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3806 xe_len
+= OCFS2_XATTR_ROOT_SIZE
;
3807 if (le16_to_cpu(xe
->xe_name_offset
) <
3808 le16_to_cpu(xh
->xh_free_start
))
3809 xh
->xh_free_start
= xe
->xe_name_offset
;
3813 /* set xh->xh_num_buckets for the new xh. */
3814 if (new_bucket_head
)
3815 xh
->xh_num_buckets
= cpu_to_le16(1);
3817 xh
->xh_num_buckets
= 0;
3819 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
3821 /* store the first_hash of the new bucket. */
3823 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3826 * Now only update the 1st block of the old bucket. If we
3827 * just added a new empty bucket, there is no need to modify
3833 xh
= bucket_xh(s_bucket
);
3834 memset(&xh
->xh_entries
[start
], 0,
3835 sizeof(struct ocfs2_xattr_entry
) * (count
- start
));
3836 xh
->xh_count
= cpu_to_le16(start
);
3837 xh
->xh_free_start
= cpu_to_le16(name_offset
);
3838 xh
->xh_name_value_len
= cpu_to_le16(name_value_len
);
3840 ocfs2_xattr_bucket_journal_dirty(handle
, s_bucket
);
3843 ocfs2_xattr_bucket_free(s_bucket
);
3844 ocfs2_xattr_bucket_free(t_bucket
);
3850 * Copy xattr from one bucket to another bucket.
3852 * The caller must make sure that the journal transaction
3853 * has enough space for journaling.
3855 static int ocfs2_cp_xattr_bucket(struct inode
*inode
,
3862 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
3864 BUG_ON(s_blkno
== t_blkno
);
3866 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3867 (unsigned long long)s_blkno
, (unsigned long long)t_blkno
,
3870 s_bucket
= ocfs2_xattr_bucket_new(inode
);
3871 t_bucket
= ocfs2_xattr_bucket_new(inode
);
3872 if (!s_bucket
|| !t_bucket
) {
3878 ret
= ocfs2_read_xattr_bucket(s_bucket
, s_blkno
);
3883 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3884 * there's no need to read it.
3886 ret
= ocfs2_init_xattr_bucket(t_bucket
, t_blkno
);
3891 * Hey, if we're overwriting t_bucket, what difference does
3892 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
3893 * cluster to fill, we came here from
3894 * ocfs2_mv_xattr_buckets(), and it is really new -
3895 * ACCESS_CREATE is required. But we also might have moved data
3896 * out of t_bucket before extending back into it.
3897 * ocfs2_add_new_xattr_bucket() can do this - its call to
3898 * ocfs2_add_new_xattr_cluster() may have created a new extent
3899 * and copied out the end of the old extent. Then it re-extends
3900 * the old extent back to create space for new xattrs. That's
3901 * how we get here, and the bucket isn't really new.
3903 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
3905 OCFS2_JOURNAL_ACCESS_CREATE
:
3906 OCFS2_JOURNAL_ACCESS_WRITE
);
3910 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
3911 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
3914 ocfs2_xattr_bucket_free(t_bucket
);
3915 ocfs2_xattr_bucket_free(s_bucket
);
3921 * src_blk points to the start of an existing extent. last_blk points to
3922 * last cluster in that extent. to_blk points to a newly allocated
3923 * extent. We copy the buckets from the cluster at last_blk to the new
3924 * extent. If start_bucket is non-zero, we skip that many buckets before
3925 * we start copying. The new extent's xh_num_buckets gets set to the
3926 * number of buckets we copied. The old extent's xh_num_buckets shrinks
3927 * by the same amount.
3929 static int ocfs2_mv_xattr_buckets(struct inode
*inode
, handle_t
*handle
,
3930 u64 src_blk
, u64 last_blk
, u64 to_blk
,
3931 unsigned int start_bucket
,
3934 int i
, ret
, credits
;
3935 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3936 int blks_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3937 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
3938 struct ocfs2_xattr_bucket
*old_first
, *new_first
;
3940 mlog(0, "mv xattrs from cluster %llu to %llu\n",
3941 (unsigned long long)last_blk
, (unsigned long long)to_blk
);
3943 BUG_ON(start_bucket
>= num_buckets
);
3945 num_buckets
-= start_bucket
;
3946 last_blk
+= (start_bucket
* blks_per_bucket
);
3949 /* The first bucket of the original extent */
3950 old_first
= ocfs2_xattr_bucket_new(inode
);
3951 /* The first bucket of the new extent */
3952 new_first
= ocfs2_xattr_bucket_new(inode
);
3953 if (!old_first
|| !new_first
) {
3959 ret
= ocfs2_read_xattr_bucket(old_first
, src_blk
);
3966 * We need to update the first bucket of the old extent and all
3967 * the buckets going to the new extent.
3969 credits
= ((num_buckets
+ 1) * blks_per_bucket
) +
3970 handle
->h_buffer_credits
;
3971 ret
= ocfs2_extend_trans(handle
, credits
);
3977 ret
= ocfs2_xattr_bucket_journal_access(handle
, old_first
,
3978 OCFS2_JOURNAL_ACCESS_WRITE
);
3984 for (i
= 0; i
< num_buckets
; i
++) {
3985 ret
= ocfs2_cp_xattr_bucket(inode
, handle
,
3986 last_blk
+ (i
* blks_per_bucket
),
3987 to_blk
+ (i
* blks_per_bucket
),
3996 * Get the new bucket ready before we dirty anything
3997 * (This actually shouldn't fail, because we already dirtied
3998 * it once in ocfs2_cp_xattr_bucket()).
4000 ret
= ocfs2_read_xattr_bucket(new_first
, to_blk
);
4005 ret
= ocfs2_xattr_bucket_journal_access(handle
, new_first
,
4006 OCFS2_JOURNAL_ACCESS_WRITE
);
4012 /* Now update the headers */
4013 le16_add_cpu(&bucket_xh(old_first
)->xh_num_buckets
, -num_buckets
);
4014 ocfs2_xattr_bucket_journal_dirty(handle
, old_first
);
4016 bucket_xh(new_first
)->xh_num_buckets
= cpu_to_le16(num_buckets
);
4017 ocfs2_xattr_bucket_journal_dirty(handle
, new_first
);
4020 *first_hash
= le32_to_cpu(bucket_xh(new_first
)->xh_entries
[0].xe_name_hash
);
4023 ocfs2_xattr_bucket_free(new_first
);
4024 ocfs2_xattr_bucket_free(old_first
);
4029 * Move some xattrs in this cluster to the new cluster.
4030 * This function should only be called when bucket size == cluster size.
4031 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4033 static int ocfs2_divide_xattr_cluster(struct inode
*inode
,
4039 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4040 int ret
, credits
= 2 * blk_per_bucket
+ handle
->h_buffer_credits
;
4042 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
< OCFS2_SB(inode
->i_sb
)->s_clustersize
);
4044 ret
= ocfs2_extend_trans(handle
, credits
);
4050 /* Move half of the xattr in start_blk to the next bucket. */
4051 return ocfs2_divide_xattr_bucket(inode
, handle
, prev_blk
,
4052 new_blk
, first_hash
, 1);
4056 * Move some xattrs from the old cluster to the new one since they are not
4057 * contiguous in ocfs2 xattr tree.
4059 * new_blk starts a new separate cluster, and we will move some xattrs from
4060 * prev_blk to it. v_start will be set as the first name hash value in this
4061 * new cluster so that it can be used as e_cpos during tree insertion and
4062 * don't collide with our original b-tree operations. first_bh and header_bh
4063 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4064 * to extend the insert bucket.
4066 * The problem is how much xattr should we move to the new one and when should
4067 * we update first_bh and header_bh?
4068 * 1. If cluster size > bucket size, that means the previous cluster has more
4069 * than 1 bucket, so just move half nums of bucket into the new cluster and
4070 * update the first_bh and header_bh if the insert bucket has been moved
4071 * to the new cluster.
4072 * 2. If cluster_size == bucket_size:
4073 * a) If the previous extent rec has more than one cluster and the insert
4074 * place isn't in the last cluster, copy the entire last cluster to the
4075 * new one. This time, we don't need to upate the first_bh and header_bh
4076 * since they will not be moved into the new cluster.
4077 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4078 * the new one. And we set the extend flag to zero if the insert place is
4079 * moved into the new allocated cluster since no extend is needed.
4081 static int ocfs2_adjust_xattr_cross_cluster(struct inode
*inode
,
4083 struct ocfs2_xattr_bucket
*first
,
4084 struct ocfs2_xattr_bucket
*target
,
4092 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4093 (unsigned long long)bucket_blkno(first
), prev_clusters
,
4094 (unsigned long long)new_blk
);
4096 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
)) > 1) {
4097 ret
= ocfs2_mv_xattr_bucket_cross_cluster(inode
,
4106 /* The start of the last cluster in the first extent */
4107 u64 last_blk
= bucket_blkno(first
) +
4108 ((prev_clusters
- 1) *
4109 ocfs2_clusters_to_blocks(inode
->i_sb
, 1));
4111 if (prev_clusters
> 1 && bucket_blkno(target
) != last_blk
) {
4112 ret
= ocfs2_mv_xattr_buckets(inode
, handle
,
4113 bucket_blkno(first
),
4114 last_blk
, new_blk
, 0,
4119 ret
= ocfs2_divide_xattr_cluster(inode
, handle
,
4125 if ((bucket_blkno(target
) == last_blk
) && extend
)
4134 * Add a new cluster for xattr storage.
4136 * If the new cluster is contiguous with the previous one, it will be
4137 * appended to the same extent record, and num_clusters will be updated.
4138 * If not, we will insert a new extent for it and move some xattrs in
4139 * the last cluster into the new allocated one.
4140 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4141 * lose the benefits of hashing because we'll have to search large leaves.
4142 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4145 * first_bh is the first block of the previous extent rec and header_bh
4146 * indicates the bucket we will insert the new xattrs. They will be updated
4147 * when the header_bh is moved into the new cluster.
4149 static int ocfs2_add_new_xattr_cluster(struct inode
*inode
,
4150 struct buffer_head
*root_bh
,
4151 struct ocfs2_xattr_bucket
*first
,
4152 struct ocfs2_xattr_bucket
*target
,
4156 struct ocfs2_xattr_set_ctxt
*ctxt
)
4159 u16 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
4160 u32 prev_clusters
= *num_clusters
;
4161 u32 clusters_to_add
= 1, bit_off
, num_bits
, v_start
= 0;
4163 handle_t
*handle
= ctxt
->handle
;
4164 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4165 struct ocfs2_extent_tree et
;
4167 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4168 "previous xattr blkno = %llu\n",
4169 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
4170 prev_cpos
, (unsigned long long)bucket_blkno(first
));
4172 ocfs2_init_xattr_tree_extent_tree(&et
, inode
, root_bh
);
4174 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
4175 OCFS2_JOURNAL_ACCESS_WRITE
);
4181 ret
= __ocfs2_claim_clusters(osb
, handle
, ctxt
->data_ac
, 1,
4182 clusters_to_add
, &bit_off
, &num_bits
);
4189 BUG_ON(num_bits
> clusters_to_add
);
4191 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
4192 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4193 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4195 if (bucket_blkno(first
) + (prev_clusters
* bpc
) == block
&&
4196 (prev_clusters
+ num_bits
) << osb
->s_clustersize_bits
<=
4197 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
) {
4199 * If this cluster is contiguous with the old one and
4200 * adding this new cluster, we don't surpass the limit of
4201 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4202 * initialized and used like other buckets in the previous
4204 * So add it as a contiguous one. The caller will handle
4207 v_start
= prev_cpos
+ prev_clusters
;
4208 *num_clusters
= prev_clusters
+ num_bits
;
4209 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4212 ret
= ocfs2_adjust_xattr_cross_cluster(inode
,
4226 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
4227 num_bits
, (unsigned long long)block
, v_start
);
4228 ret
= ocfs2_insert_extent(osb
, handle
, inode
, &et
, v_start
, block
,
4229 num_bits
, 0, ctxt
->meta_ac
);
4235 ret
= ocfs2_journal_dirty(handle
, root_bh
);
4244 * We are given an extent. 'first' is the bucket at the very front of
4245 * the extent. The extent has space for an additional bucket past
4246 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4247 * of the target bucket. We wish to shift every bucket past the target
4248 * down one, filling in that additional space. When we get back to the
4249 * target, we split the target between itself and the now-empty bucket
4250 * at target+1 (aka, target_blkno + blks_per_bucket).
4252 static int ocfs2_extend_xattr_bucket(struct inode
*inode
,
4254 struct ocfs2_xattr_bucket
*first
,
4259 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4260 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4262 u16 new_bucket
= le16_to_cpu(bucket_xh(first
)->xh_num_buckets
);
4264 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
4265 "from %llu, len = %u\n", (unsigned long long)target_blk
,
4266 (unsigned long long)bucket_blkno(first
), num_clusters
);
4268 /* The extent must have room for an additional bucket */
4269 BUG_ON(new_bucket
>=
4270 (num_clusters
* ocfs2_xattr_buckets_per_cluster(osb
)));
4272 /* end_blk points to the last existing bucket */
4273 end_blk
= bucket_blkno(first
) + ((new_bucket
- 1) * blk_per_bucket
);
4276 * end_blk is the start of the last existing bucket.
4277 * Thus, (end_blk - target_blk) covers the target bucket and
4278 * every bucket after it up to, but not including, the last
4279 * existing bucket. Then we add the last existing bucket, the
4280 * new bucket, and the first bucket (3 * blk_per_bucket).
4282 credits
= (end_blk
- target_blk
) + (3 * blk_per_bucket
) +
4283 handle
->h_buffer_credits
;
4284 ret
= ocfs2_extend_trans(handle
, credits
);
4290 ret
= ocfs2_xattr_bucket_journal_access(handle
, first
,
4291 OCFS2_JOURNAL_ACCESS_WRITE
);
4297 while (end_blk
!= target_blk
) {
4298 ret
= ocfs2_cp_xattr_bucket(inode
, handle
, end_blk
,
4299 end_blk
+ blk_per_bucket
, 0);
4302 end_blk
-= blk_per_bucket
;
4305 /* Move half of the xattr in target_blkno to the next bucket. */
4306 ret
= ocfs2_divide_xattr_bucket(inode
, handle
, target_blk
,
4307 target_blk
+ blk_per_bucket
, NULL
, 0);
4309 le16_add_cpu(&bucket_xh(first
)->xh_num_buckets
, 1);
4310 ocfs2_xattr_bucket_journal_dirty(handle
, first
);
4317 * Add new xattr bucket in an extent record and adjust the buckets
4318 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
4319 * bucket we want to insert into.
4321 * In the easy case, we will move all the buckets after target down by
4322 * one. Half of target's xattrs will be moved to the next bucket.
4324 * If current cluster is full, we'll allocate a new one. This may not
4325 * be contiguous. The underlying calls will make sure that there is
4326 * space for the insert, shifting buckets around if necessary.
4327 * 'target' may be moved by those calls.
4329 static int ocfs2_add_new_xattr_bucket(struct inode
*inode
,
4330 struct buffer_head
*xb_bh
,
4331 struct ocfs2_xattr_bucket
*target
,
4332 struct ocfs2_xattr_set_ctxt
*ctxt
)
4334 struct ocfs2_xattr_block
*xb
=
4335 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4336 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
4337 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
4339 le32_to_cpu(bucket_xh(target
)->xh_entries
[0].xe_name_hash
);
4340 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4341 int ret
, num_buckets
, extend
= 1;
4343 u32 e_cpos
, num_clusters
;
4344 /* The bucket at the front of the extent */
4345 struct ocfs2_xattr_bucket
*first
;
4347 mlog(0, "Add new xattr bucket starting from %llu\n",
4348 (unsigned long long)bucket_blkno(target
));
4350 /* The first bucket of the original extent */
4351 first
= ocfs2_xattr_bucket_new(inode
);
4358 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &e_cpos
,
4365 ret
= ocfs2_read_xattr_bucket(first
, p_blkno
);
4371 num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
) * num_clusters
;
4372 if (num_buckets
== le16_to_cpu(bucket_xh(first
)->xh_num_buckets
)) {
4374 * This can move first+target if the target bucket moves
4375 * to the new extent.
4377 ret
= ocfs2_add_new_xattr_cluster(inode
,
4392 ret
= ocfs2_extend_xattr_bucket(inode
,
4395 bucket_blkno(target
),
4402 ocfs2_xattr_bucket_free(first
);
4407 static inline char *ocfs2_xattr_bucket_get_val(struct inode
*inode
,
4408 struct ocfs2_xattr_bucket
*bucket
,
4411 int block_off
= offs
>> inode
->i_sb
->s_blocksize_bits
;
4413 offs
= offs
% inode
->i_sb
->s_blocksize
;
4414 return bucket_block(bucket
, block_off
) + offs
;
4418 * Handle the normal xattr set, including replace, delete and new.
4420 * Note: "local" indicates the real data's locality. So we can't
4421 * just its bucket locality by its length.
4423 static void ocfs2_xattr_set_entry_normal(struct inode
*inode
,
4424 struct ocfs2_xattr_info
*xi
,
4425 struct ocfs2_xattr_search
*xs
,
4429 struct ocfs2_xattr_entry
*last
, *xe
;
4430 int name_len
= strlen(xi
->name
);
4431 struct ocfs2_xattr_header
*xh
= xs
->header
;
4432 u16 count
= le16_to_cpu(xh
->xh_count
), start
;
4433 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4435 size_t offs
, size
, new_size
;
4437 last
= &xh
->xh_entries
[count
];
4438 if (!xs
->not_found
) {
4440 offs
= le16_to_cpu(xe
->xe_name_offset
);
4441 if (ocfs2_xattr_is_local(xe
))
4442 size
= OCFS2_XATTR_SIZE(name_len
) +
4443 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
4445 size
= OCFS2_XATTR_SIZE(name_len
) +
4446 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE
);
4449 * If the new value will be stored outside, xi->value has been
4450 * initalized as an empty ocfs2_xattr_value_root, and the same
4451 * goes with xi->value_len, so we can set new_size safely here.
4452 * See ocfs2_xattr_set_in_bucket.
4454 new_size
= OCFS2_XATTR_SIZE(name_len
) +
4455 OCFS2_XATTR_SIZE(xi
->value_len
);
4457 le16_add_cpu(&xh
->xh_name_value_len
, -size
);
4459 if (new_size
> size
)
4460 goto set_new_name_value
;
4462 /* Now replace the old value with new one. */
4464 xe
->xe_value_size
= cpu_to_le64(xi
->value_len
);
4466 xe
->xe_value_size
= 0;
4468 val
= ocfs2_xattr_bucket_get_val(inode
,
4470 memset(val
+ OCFS2_XATTR_SIZE(name_len
), 0,
4471 size
- OCFS2_XATTR_SIZE(name_len
));
4472 if (OCFS2_XATTR_SIZE(xi
->value_len
) > 0)
4473 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
4474 xi
->value
, xi
->value_len
);
4476 le16_add_cpu(&xh
->xh_name_value_len
, new_size
);
4477 ocfs2_xattr_set_local(xe
, local
);
4481 * Remove the old entry if there is more than one.
4482 * We don't remove the last entry so that we can
4483 * use it to indicate the hash value of the empty
4487 le16_add_cpu(&xh
->xh_count
, -1);
4490 (void *)last
- (void *)xe
);
4492 sizeof(struct ocfs2_xattr_entry
));
4495 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
4500 /* find a new entry for insert. */
4501 int low
= 0, high
= count
- 1, tmp
;
4502 struct ocfs2_xattr_entry
*tmp_xe
;
4504 while (low
<= high
&& count
) {
4505 tmp
= (low
+ high
) / 2;
4506 tmp_xe
= &xh
->xh_entries
[tmp
];
4508 if (name_hash
> le32_to_cpu(tmp_xe
->xe_name_hash
))
4510 else if (name_hash
<
4511 le32_to_cpu(tmp_xe
->xe_name_hash
))
4519 xe
= &xh
->xh_entries
[low
];
4521 memmove(xe
+ 1, xe
, (void *)last
- (void *)xe
);
4523 le16_add_cpu(&xh
->xh_count
, 1);
4524 memset(xe
, 0, sizeof(struct ocfs2_xattr_entry
));
4525 xe
->xe_name_hash
= cpu_to_le32(name_hash
);
4526 xe
->xe_name_len
= name_len
;
4527 ocfs2_xattr_set_type(xe
, xi
->name_index
);
4531 /* Insert the new name+value. */
4532 size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_SIZE(xi
->value_len
);
4535 * We must make sure that the name/value pair
4536 * exists in the same block.
4538 offs
= le16_to_cpu(xh
->xh_free_start
);
4539 start
= offs
- size
;
4541 if (start
>> inode
->i_sb
->s_blocksize_bits
!=
4542 (offs
- 1) >> inode
->i_sb
->s_blocksize_bits
) {
4543 offs
= offs
- offs
% blocksize
;
4544 xh
->xh_free_start
= cpu_to_le16(offs
);
4547 val
= ocfs2_xattr_bucket_get_val(inode
, xs
->bucket
, offs
- size
);
4548 xe
->xe_name_offset
= cpu_to_le16(offs
- size
);
4550 memset(val
, 0, size
);
4551 memcpy(val
, xi
->name
, name_len
);
4552 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
), xi
->value
, xi
->value_len
);
4554 xe
->xe_value_size
= cpu_to_le64(xi
->value_len
);
4555 ocfs2_xattr_set_local(xe
, local
);
4557 le16_add_cpu(&xh
->xh_free_start
, -size
);
4558 le16_add_cpu(&xh
->xh_name_value_len
, size
);
4564 * Set the xattr entry in the specified bucket.
4565 * The bucket is indicated by xs->bucket and it should have the enough
4566 * space for the xattr insertion.
4568 static int ocfs2_xattr_set_entry_in_bucket(struct inode
*inode
,
4570 struct ocfs2_xattr_info
*xi
,
4571 struct ocfs2_xattr_search
*xs
,
4578 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4579 (unsigned long)xi
->value_len
, xi
->name_index
,
4580 (unsigned long long)bucket_blkno(xs
->bucket
));
4582 if (!xs
->bucket
->bu_bhs
[1]) {
4583 blkno
= bucket_blkno(xs
->bucket
);
4584 ocfs2_xattr_bucket_relse(xs
->bucket
);
4585 ret
= ocfs2_read_xattr_bucket(xs
->bucket
, blkno
);
4592 ret
= ocfs2_xattr_bucket_journal_access(handle
, xs
->bucket
,
4593 OCFS2_JOURNAL_ACCESS_WRITE
);
4599 ocfs2_xattr_set_entry_normal(inode
, xi
, xs
, name_hash
, local
);
4600 ocfs2_xattr_bucket_journal_dirty(handle
, xs
->bucket
);
4607 * Truncate the specified xe_off entry in xattr bucket.
4608 * bucket is indicated by header_bh and len is the new length.
4609 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4611 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4613 static int ocfs2_xattr_bucket_value_truncate(struct inode
*inode
,
4614 struct ocfs2_xattr_bucket
*bucket
,
4617 struct ocfs2_xattr_set_ctxt
*ctxt
)
4621 struct buffer_head
*value_bh
= NULL
;
4622 struct ocfs2_xattr_value_root
*xv
;
4623 struct ocfs2_xattr_entry
*xe
;
4624 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
4625 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4627 xe
= &xh
->xh_entries
[xe_off
];
4629 BUG_ON(!xe
|| ocfs2_xattr_is_local(xe
));
4631 offset
= le16_to_cpu(xe
->xe_name_offset
) +
4632 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
4634 value_blk
= offset
/ blocksize
;
4636 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4637 BUG_ON(value_blk
!= (offset
+ OCFS2_XATTR_ROOT_SIZE
- 1) / blocksize
);
4639 value_bh
= bucket
->bu_bhs
[value_blk
];
4642 xv
= (struct ocfs2_xattr_value_root
*)
4643 (value_bh
->b_data
+ offset
% blocksize
);
4645 ret
= ocfs2_xattr_bucket_journal_access(ctxt
->handle
, bucket
,
4646 OCFS2_JOURNAL_ACCESS_WRITE
);
4653 * From here on out we have to dirty the bucket. The generic
4654 * value calls only modify one of the bucket's bhs, but we need
4655 * to send the bucket at once. So if they error, they *could* have
4656 * modified something. We have to assume they did, and dirty
4657 * the whole bucket. This leaves us in a consistent state.
4659 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4660 xe_off
, (unsigned long long)bucket_blkno(bucket
), len
);
4661 ret
= ocfs2_xattr_value_truncate(inode
, value_bh
, xv
, len
, ctxt
);
4667 xe
->xe_value_size
= cpu_to_le64(len
);
4670 ocfs2_xattr_bucket_journal_dirty(ctxt
->handle
, bucket
);
4676 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode
*inode
,
4677 struct ocfs2_xattr_search
*xs
,
4679 struct ocfs2_xattr_set_ctxt
*ctxt
)
4682 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4683 struct ocfs2_xattr_header
*xh
= (struct ocfs2_xattr_header
*)xs
->base
;
4685 BUG_ON(!xs
->bucket
->bu_bhs
[0] || !xe
|| ocfs2_xattr_is_local(xe
));
4687 offset
= xe
- xh
->xh_entries
;
4688 ret
= ocfs2_xattr_bucket_value_truncate(inode
, xs
->bucket
,
4696 static int ocfs2_xattr_bucket_set_value_outside(struct inode
*inode
,
4698 struct ocfs2_xattr_search
*xs
,
4703 struct ocfs2_xattr_value_root
*xv
;
4704 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4706 BUG_ON(!xs
->base
|| !xe
|| ocfs2_xattr_is_local(xe
));
4708 offset
= le16_to_cpu(xe
->xe_name_offset
) +
4709 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
4711 xv
= (struct ocfs2_xattr_value_root
*)(xs
->base
+ offset
);
4713 return __ocfs2_xattr_set_value_outside(inode
, handle
,
4714 xv
, val
, value_len
);
4717 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
4718 struct buffer_head
*root_bh
,
4724 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4725 struct inode
*tl_inode
= osb
->osb_tl_inode
;
4727 struct ocfs2_xattr_block
*xb
=
4728 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
4729 struct ocfs2_alloc_context
*meta_ac
= NULL
;
4730 struct ocfs2_cached_dealloc_ctxt dealloc
;
4731 struct ocfs2_extent_tree et
;
4733 ocfs2_init_xattr_tree_extent_tree(&et
, inode
, root_bh
);
4735 ocfs2_init_dealloc_ctxt(&dealloc
);
4737 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4738 cpos
, len
, (unsigned long long)blkno
);
4740 ocfs2_remove_xattr_clusters_from_cache(inode
, blkno
, len
);
4742 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
4748 mutex_lock(&tl_inode
->i_mutex
);
4750 if (ocfs2_truncate_log_needs_flush(osb
)) {
4751 ret
= __ocfs2_flush_truncate_log(osb
);
4758 handle
= ocfs2_start_trans(osb
, ocfs2_remove_extent_credits(osb
->sb
));
4759 if (IS_ERR(handle
)) {
4765 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
4766 OCFS2_JOURNAL_ACCESS_WRITE
);
4772 ret
= ocfs2_remove_extent(inode
, &et
, cpos
, len
, handle
, meta_ac
,
4779 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, -len
);
4781 ret
= ocfs2_journal_dirty(handle
, root_bh
);
4787 ret
= ocfs2_truncate_log_append(osb
, handle
, blkno
, len
);
4792 ocfs2_commit_trans(osb
, handle
);
4794 ocfs2_schedule_truncate_log_flush(osb
, 1);
4796 mutex_unlock(&tl_inode
->i_mutex
);
4799 ocfs2_free_alloc_context(meta_ac
);
4801 ocfs2_run_deallocs(osb
, &dealloc
);
4806 static void ocfs2_xattr_bucket_remove_xs(struct inode
*inode
,
4808 struct ocfs2_xattr_search
*xs
)
4810 struct ocfs2_xattr_header
*xh
= bucket_xh(xs
->bucket
);
4811 struct ocfs2_xattr_entry
*last
= &xh
->xh_entries
[
4812 le16_to_cpu(xh
->xh_count
) - 1];
4815 ret
= ocfs2_xattr_bucket_journal_access(handle
, xs
->bucket
,
4816 OCFS2_JOURNAL_ACCESS_WRITE
);
4822 /* Remove the old entry. */
4823 memmove(xs
->here
, xs
->here
+ 1,
4824 (void *)last
- (void *)xs
->here
);
4825 memset(last
, 0, sizeof(struct ocfs2_xattr_entry
));
4826 le16_add_cpu(&xh
->xh_count
, -1);
4828 ocfs2_xattr_bucket_journal_dirty(handle
, xs
->bucket
);
4832 * Set the xattr name/value in the bucket specified in xs.
4834 * As the new value in xi may be stored in the bucket or in an outside cluster,
4835 * we divide the whole process into 3 steps:
4836 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4837 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4838 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4839 * 4. If the clusters for the new outside value can't be allocated, we need
4840 * to free the xattr we allocated in set.
4842 static int ocfs2_xattr_set_in_bucket(struct inode
*inode
,
4843 struct ocfs2_xattr_info
*xi
,
4844 struct ocfs2_xattr_search
*xs
,
4845 struct ocfs2_xattr_set_ctxt
*ctxt
)
4849 char *val
= (char *)xi
->value
;
4850 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4851 u32 name_hash
= ocfs2_xattr_name_hash(inode
, xi
->name
,
4854 if (!xs
->not_found
&& !ocfs2_xattr_is_local(xe
)) {
4856 * We need to truncate the xattr storage first.
4858 * If both the old and new value are stored to
4859 * outside block, we only need to truncate
4860 * the storage and then set the value outside.
4862 * If the new value should be stored within block,
4863 * we should free all the outside block first and
4864 * the modification to the xattr block will be done
4865 * by following steps.
4867 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
4868 value_len
= xi
->value_len
;
4872 ret
= ocfs2_xattr_bucket_value_truncate_xs(inode
, xs
,
4879 goto set_value_outside
;
4882 value_len
= xi
->value_len
;
4883 /* So we have to handle the inside block change now. */
4884 if (value_len
> OCFS2_XATTR_INLINE_SIZE
) {
4886 * If the new value will be stored outside of block,
4887 * initalize a new empty value root and insert it first.
4890 xi
->value
= &def_xv
;
4891 xi
->value_len
= OCFS2_XATTR_ROOT_SIZE
;
4894 ret
= ocfs2_xattr_set_entry_in_bucket(inode
, ctxt
->handle
, xi
, xs
,
4901 if (value_len
<= OCFS2_XATTR_INLINE_SIZE
)
4904 /* allocate the space now for the outside block storage. */
4905 ret
= ocfs2_xattr_bucket_value_truncate_xs(inode
, xs
,
4910 if (xs
->not_found
) {
4912 * We can't allocate enough clusters for outside
4913 * storage and we have allocated xattr already,
4914 * so need to remove it.
4916 ocfs2_xattr_bucket_remove_xs(inode
, ctxt
->handle
, xs
);
4922 ret
= ocfs2_xattr_bucket_set_value_outside(inode
, ctxt
->handle
,
4923 xs
, val
, value_len
);
4929 * check whether the xattr bucket is filled up with the same hash value.
4930 * If we want to insert the xattr with the same hash, return -ENOSPC.
4931 * If we want to insert a xattr with different hash value, go ahead
4932 * and ocfs2_divide_xattr_bucket will handle this.
4934 static int ocfs2_check_xattr_bucket_collision(struct inode
*inode
,
4935 struct ocfs2_xattr_bucket
*bucket
,
4938 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
4939 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
4941 if (name_hash
!= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
))
4944 if (xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1].xe_name_hash
==
4945 xh
->xh_entries
[0].xe_name_hash
) {
4946 mlog(ML_ERROR
, "Too much hash collision in xattr bucket %llu, "
4948 (unsigned long long)bucket_blkno(bucket
),
4949 le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
));
4956 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
4957 struct ocfs2_xattr_info
*xi
,
4958 struct ocfs2_xattr_search
*xs
,
4959 struct ocfs2_xattr_set_ctxt
*ctxt
)
4961 struct ocfs2_xattr_header
*xh
;
4962 struct ocfs2_xattr_entry
*xe
;
4963 u16 count
, header_size
, xh_free_start
;
4964 int free
, max_free
, need
, old
;
4965 size_t value_size
= 0, name_len
= strlen(xi
->name
);
4966 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4967 int ret
, allocation
= 0;
4969 mlog_entry("Set xattr %s in xattr index block\n", xi
->name
);
4973 count
= le16_to_cpu(xh
->xh_count
);
4974 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4975 header_size
= sizeof(struct ocfs2_xattr_header
) +
4976 count
* sizeof(struct ocfs2_xattr_entry
);
4977 max_free
= OCFS2_XATTR_BUCKET_SIZE
-
4978 le16_to_cpu(xh
->xh_name_value_len
) - header_size
;
4980 mlog_bug_on_msg(header_size
> blocksize
, "bucket %llu has header size "
4981 "of %u which exceed block size\n",
4982 (unsigned long long)bucket_blkno(xs
->bucket
),
4985 if (xi
->value
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
4986 value_size
= OCFS2_XATTR_ROOT_SIZE
;
4988 value_size
= OCFS2_XATTR_SIZE(xi
->value_len
);
4991 need
= sizeof(struct ocfs2_xattr_entry
) +
4992 OCFS2_XATTR_SIZE(name_len
) + value_size
;
4994 need
= value_size
+ OCFS2_XATTR_SIZE(name_len
);
4997 * We only replace the old value if the new length is smaller
4998 * than the old one. Otherwise we will allocate new space in the
4999 * bucket to store it.
5002 if (ocfs2_xattr_is_local(xe
))
5003 old
= OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
5005 old
= OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE
);
5007 if (old
>= value_size
)
5011 free
= xh_free_start
- header_size
;
5013 * We need to make sure the new name/value pair
5014 * can exist in the same block.
5016 if (xh_free_start
% blocksize
< need
)
5017 free
-= xh_free_start
% blocksize
;
5019 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5020 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5021 " %u\n", xs
->not_found
,
5022 (unsigned long long)bucket_blkno(xs
->bucket
),
5023 free
, need
, max_free
, le16_to_cpu(xh
->xh_free_start
),
5024 le16_to_cpu(xh
->xh_name_value_len
));
5028 count
== ocfs2_xattr_max_xe_in_bucket(inode
->i_sb
))) {
5029 if (need
<= max_free
&&
5030 count
< ocfs2_xattr_max_xe_in_bucket(inode
->i_sb
)) {
5032 * We can create the space by defragment. Since only the
5033 * name/value will be moved, the xe shouldn't be changed
5036 ret
= ocfs2_defrag_xattr_bucket(inode
, ctxt
->handle
,
5043 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
5044 free
= xh_free_start
- header_size
;
5045 if (xh_free_start
% blocksize
< need
)
5046 free
-= xh_free_start
% blocksize
;
5051 mlog(0, "Can't get enough space for xattr insert by "
5052 "defragment. Need %u bytes, but we have %d, so "
5053 "allocate new bucket for it.\n", need
, free
);
5057 * We have to add new buckets or clusters and one
5058 * allocation should leave us enough space for insert.
5063 * We do not allow for overlapping ranges between buckets. And
5064 * the maximum number of collisions we will allow for then is
5065 * one bucket's worth, so check it here whether we need to
5066 * add a new bucket for the insert.
5068 ret
= ocfs2_check_xattr_bucket_collision(inode
,
5076 ret
= ocfs2_add_new_xattr_bucket(inode
,
5086 * ocfs2_add_new_xattr_bucket() will have updated
5087 * xs->bucket if it moved, but it will not have updated
5088 * any of the other search fields. Thus, we drop it and
5089 * re-search. Everything should be cached, so it'll be
5092 ocfs2_xattr_bucket_relse(xs
->bucket
);
5093 ret
= ocfs2_xattr_index_block_find(inode
, xs
->xattr_bh
,
5096 if (ret
&& ret
!= -ENODATA
)
5098 xs
->not_found
= ret
;
5104 ret
= ocfs2_xattr_set_in_bucket(inode
, xi
, xs
, ctxt
);
5110 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
5111 struct ocfs2_xattr_bucket
*bucket
,
5115 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
5117 struct ocfs2_xattr_entry
*xe
;
5118 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5119 struct ocfs2_xattr_set_ctxt ctxt
= {NULL
, NULL
,};
5120 int credits
= ocfs2_remove_extent_credits(osb
->sb
) +
5121 ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
5124 ocfs2_init_dealloc_ctxt(&ctxt
.dealloc
);
5126 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
5127 xe
= &xh
->xh_entries
[i
];
5128 if (ocfs2_xattr_is_local(xe
))
5131 ctxt
.handle
= ocfs2_start_trans(osb
, credits
);
5132 if (IS_ERR(ctxt
.handle
)) {
5133 ret
= PTR_ERR(ctxt
.handle
);
5138 ret
= ocfs2_xattr_bucket_value_truncate(inode
, bucket
,
5141 ocfs2_commit_trans(osb
, ctxt
.handle
);
5148 ocfs2_schedule_truncate_log_flush(osb
, 1);
5149 ocfs2_run_deallocs(osb
, &ctxt
.dealloc
);
5153 static int ocfs2_delete_xattr_index_block(struct inode
*inode
,
5154 struct buffer_head
*xb_bh
)
5156 struct ocfs2_xattr_block
*xb
=
5157 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
5158 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
5160 u32 name_hash
= UINT_MAX
, e_cpos
, num_clusters
;
5163 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
5166 while (name_hash
> 0) {
5167 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
5168 &e_cpos
, &num_clusters
, el
);
5174 ret
= ocfs2_iterate_xattr_buckets(inode
, p_blkno
, num_clusters
,
5175 ocfs2_delete_xattr_in_bucket
,
5182 ret
= ocfs2_rm_xattr_cluster(inode
, xb_bh
,
5183 p_blkno
, e_cpos
, num_clusters
);
5192 name_hash
= e_cpos
- 1;
5200 * 'security' attributes support
5202 static size_t ocfs2_xattr_security_list(struct inode
*inode
, char *list
,
5203 size_t list_size
, const char *name
,
5206 const size_t prefix_len
= XATTR_SECURITY_PREFIX_LEN
;
5207 const size_t total_len
= prefix_len
+ name_len
+ 1;
5209 if (list
&& total_len
<= list_size
) {
5210 memcpy(list
, XATTR_SECURITY_PREFIX
, prefix_len
);
5211 memcpy(list
+ prefix_len
, name
, name_len
);
5212 list
[prefix_len
+ name_len
] = '\0';
5217 static int ocfs2_xattr_security_get(struct inode
*inode
, const char *name
,
5218 void *buffer
, size_t size
)
5220 if (strcmp(name
, "") == 0)
5222 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_SECURITY
, name
,
5226 static int ocfs2_xattr_security_set(struct inode
*inode
, const char *name
,
5227 const void *value
, size_t size
, int flags
)
5229 if (strcmp(name
, "") == 0)
5232 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_SECURITY
, name
, value
,
5236 int ocfs2_init_security_get(struct inode
*inode
,
5238 struct ocfs2_security_xattr_info
*si
)
5240 return security_inode_init_security(inode
, dir
, &si
->name
, &si
->value
,
5244 int ocfs2_init_security_set(handle_t
*handle
,
5245 struct inode
*inode
,
5246 struct buffer_head
*di_bh
,
5247 struct ocfs2_security_xattr_info
*si
,
5248 struct ocfs2_alloc_context
*xattr_ac
,
5249 struct ocfs2_alloc_context
*data_ac
)
5251 return ocfs2_xattr_set_handle(handle
, inode
, di_bh
,
5252 OCFS2_XATTR_INDEX_SECURITY
,
5253 si
->name
, si
->value
, si
->value_len
, 0,
5257 struct xattr_handler ocfs2_xattr_security_handler
= {
5258 .prefix
= XATTR_SECURITY_PREFIX
,
5259 .list
= ocfs2_xattr_security_list
,
5260 .get
= ocfs2_xattr_security_get
,
5261 .set
= ocfs2_xattr_security_set
,
5265 * 'trusted' attributes support
5267 static size_t ocfs2_xattr_trusted_list(struct inode
*inode
, char *list
,
5268 size_t list_size
, const char *name
,
5271 const size_t prefix_len
= XATTR_TRUSTED_PREFIX_LEN
;
5272 const size_t total_len
= prefix_len
+ name_len
+ 1;
5274 if (list
&& total_len
<= list_size
) {
5275 memcpy(list
, XATTR_TRUSTED_PREFIX
, prefix_len
);
5276 memcpy(list
+ prefix_len
, name
, name_len
);
5277 list
[prefix_len
+ name_len
] = '\0';
5282 static int ocfs2_xattr_trusted_get(struct inode
*inode
, const char *name
,
5283 void *buffer
, size_t size
)
5285 if (strcmp(name
, "") == 0)
5287 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_TRUSTED
, name
,
5291 static int ocfs2_xattr_trusted_set(struct inode
*inode
, const char *name
,
5292 const void *value
, size_t size
, int flags
)
5294 if (strcmp(name
, "") == 0)
5297 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_TRUSTED
, name
, value
,
5301 struct xattr_handler ocfs2_xattr_trusted_handler
= {
5302 .prefix
= XATTR_TRUSTED_PREFIX
,
5303 .list
= ocfs2_xattr_trusted_list
,
5304 .get
= ocfs2_xattr_trusted_get
,
5305 .set
= ocfs2_xattr_trusted_set
,
5309 * 'user' attributes support
5311 static size_t ocfs2_xattr_user_list(struct inode
*inode
, char *list
,
5312 size_t list_size
, const char *name
,
5315 const size_t prefix_len
= XATTR_USER_PREFIX_LEN
;
5316 const size_t total_len
= prefix_len
+ name_len
+ 1;
5317 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5319 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
5322 if (list
&& total_len
<= list_size
) {
5323 memcpy(list
, XATTR_USER_PREFIX
, prefix_len
);
5324 memcpy(list
+ prefix_len
, name
, name_len
);
5325 list
[prefix_len
+ name_len
] = '\0';
5330 static int ocfs2_xattr_user_get(struct inode
*inode
, const char *name
,
5331 void *buffer
, size_t size
)
5333 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5335 if (strcmp(name
, "") == 0)
5337 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
5339 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_USER
, name
,
5343 static int ocfs2_xattr_user_set(struct inode
*inode
, const char *name
,
5344 const void *value
, size_t size
, int flags
)
5346 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5348 if (strcmp(name
, "") == 0)
5350 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
5353 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_USER
, name
, value
,
5357 struct xattr_handler ocfs2_xattr_user_handler
= {
5358 .prefix
= XATTR_USER_PREFIX
,
5359 .list
= ocfs2_xattr_user_list
,
5360 .get
= ocfs2_xattr_user_get
,
5361 .set
= ocfs2_xattr_user_set
,