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>
39 #define MLOG_MASK_PREFIX ML_XATTR
40 #include <cluster/masklog.h>
53 #include "buffer_head_io.h"
58 struct ocfs2_xattr_def_value_root
{
59 struct ocfs2_xattr_value_root xv
;
60 struct ocfs2_extent_rec er
;
63 struct ocfs2_xattr_bucket
{
64 struct buffer_head
*bhs
[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
];
65 struct ocfs2_xattr_header
*xh
;
68 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
69 #define OCFS2_XATTR_INLINE_SIZE 80
71 static struct ocfs2_xattr_def_value_root def_xv
= {
72 .xv
.xr_list
.l_count
= cpu_to_le16(1),
75 struct xattr_handler
*ocfs2_xattr_handlers
[] = {
76 &ocfs2_xattr_user_handler
,
77 &ocfs2_xattr_trusted_handler
,
81 static struct xattr_handler
*ocfs2_xattr_handler_map
[] = {
82 [OCFS2_XATTR_INDEX_USER
] = &ocfs2_xattr_user_handler
,
83 [OCFS2_XATTR_INDEX_TRUSTED
] = &ocfs2_xattr_trusted_handler
,
86 struct ocfs2_xattr_info
{
93 struct ocfs2_xattr_search
{
94 struct buffer_head
*inode_bh
;
96 * xattr_bh point to the block buffer head which has extended attribute
97 * when extended attribute in inode, xattr_bh is equal to inode_bh.
99 struct buffer_head
*xattr_bh
;
100 struct ocfs2_xattr_header
*header
;
101 struct ocfs2_xattr_bucket bucket
;
104 struct ocfs2_xattr_entry
*here
;
108 static int ocfs2_xattr_bucket_get_name_value(struct inode
*inode
,
109 struct ocfs2_xattr_header
*xh
,
114 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
115 struct buffer_head
*root_bh
,
118 struct ocfs2_xattr_search
*xs
);
120 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
121 struct ocfs2_xattr_tree_root
*xt
,
125 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
126 struct ocfs2_xattr_search
*xs
);
128 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
129 struct ocfs2_xattr_info
*xi
,
130 struct ocfs2_xattr_search
*xs
);
132 static int ocfs2_delete_xattr_index_block(struct inode
*inode
,
133 struct buffer_head
*xb_bh
);
135 static inline u16
ocfs2_xattr_buckets_per_cluster(struct ocfs2_super
*osb
)
137 return (1 << osb
->s_clustersize_bits
) / OCFS2_XATTR_BUCKET_SIZE
;
140 static inline u16
ocfs2_blocks_per_xattr_bucket(struct super_block
*sb
)
142 return OCFS2_XATTR_BUCKET_SIZE
/ (1 << sb
->s_blocksize_bits
);
145 static inline u16
ocfs2_xattr_max_xe_in_bucket(struct super_block
*sb
)
147 u16 len
= sb
->s_blocksize
-
148 offsetof(struct ocfs2_xattr_header
, xh_entries
);
150 return len
/ sizeof(struct ocfs2_xattr_entry
);
153 static inline const char *ocfs2_xattr_prefix(int name_index
)
155 struct xattr_handler
*handler
= NULL
;
157 if (name_index
> 0 && name_index
< OCFS2_XATTR_MAX
)
158 handler
= ocfs2_xattr_handler_map
[name_index
];
160 return handler
? handler
->prefix
: NULL
;
163 static u32
ocfs2_xattr_name_hash(struct inode
*inode
,
167 /* Get hash value of uuid from super block */
168 u32 hash
= OCFS2_SB(inode
->i_sb
)->uuid_hash
;
171 /* hash extended attribute name */
172 for (i
= 0; i
< name_len
; i
++) {
173 hash
= (hash
<< OCFS2_HASH_SHIFT
) ^
174 (hash
>> (8*sizeof(hash
) - OCFS2_HASH_SHIFT
)) ^
182 * ocfs2_xattr_hash_entry()
184 * Compute the hash of an extended attribute.
186 static void ocfs2_xattr_hash_entry(struct inode
*inode
,
187 struct ocfs2_xattr_header
*header
,
188 struct ocfs2_xattr_entry
*entry
)
191 char *name
= (char *)header
+ le16_to_cpu(entry
->xe_name_offset
);
193 hash
= ocfs2_xattr_name_hash(inode
, name
, entry
->xe_name_len
);
194 entry
->xe_name_hash
= cpu_to_le32(hash
);
199 static int ocfs2_xattr_extend_allocation(struct inode
*inode
,
201 struct buffer_head
*xattr_bh
,
202 struct ocfs2_xattr_value_root
*xv
)
205 int restart_func
= 0;
207 handle_t
*handle
= NULL
;
208 struct ocfs2_alloc_context
*data_ac
= NULL
;
209 struct ocfs2_alloc_context
*meta_ac
= NULL
;
210 enum ocfs2_alloc_restarted why
;
211 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
212 u32 prev_clusters
, logical_start
= le32_to_cpu(xv
->xr_clusters
);
213 struct ocfs2_extent_tree et
;
215 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add
);
217 ocfs2_init_xattr_value_extent_tree(&et
, inode
, xattr_bh
, xv
);
221 status
= ocfs2_lock_allocators(inode
, &et
, clusters_to_add
, 0,
228 credits
= ocfs2_calc_extend_credits(osb
->sb
, et
.et_root_el
,
230 handle
= ocfs2_start_trans(osb
, credits
);
231 if (IS_ERR(handle
)) {
232 status
= PTR_ERR(handle
);
238 restarted_transaction
:
239 status
= ocfs2_journal_access(handle
, inode
, xattr_bh
,
240 OCFS2_JOURNAL_ACCESS_WRITE
);
246 prev_clusters
= le32_to_cpu(xv
->xr_clusters
);
247 status
= ocfs2_add_clusters_in_btree(osb
,
257 if ((status
< 0) && (status
!= -EAGAIN
)) {
258 if (status
!= -ENOSPC
)
263 status
= ocfs2_journal_dirty(handle
, xattr_bh
);
269 clusters_to_add
-= le32_to_cpu(xv
->xr_clusters
) - prev_clusters
;
271 if (why
!= RESTART_NONE
&& clusters_to_add
) {
272 if (why
== RESTART_META
) {
273 mlog(0, "restarting function.\n");
276 BUG_ON(why
!= RESTART_TRANS
);
278 mlog(0, "restarting transaction.\n");
279 /* TODO: This can be more intelligent. */
280 credits
= ocfs2_calc_extend_credits(osb
->sb
,
283 status
= ocfs2_extend_trans(handle
, credits
);
285 /* handle still has to be committed at
291 goto restarted_transaction
;
297 ocfs2_commit_trans(osb
, handle
);
301 ocfs2_free_alloc_context(data_ac
);
305 ocfs2_free_alloc_context(meta_ac
);
308 if ((!status
) && restart_func
) {
316 static int __ocfs2_remove_xattr_range(struct inode
*inode
,
317 struct buffer_head
*root_bh
,
318 struct ocfs2_xattr_value_root
*xv
,
319 u32 cpos
, u32 phys_cpos
, u32 len
,
320 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
323 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
324 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
325 struct inode
*tl_inode
= osb
->osb_tl_inode
;
327 struct ocfs2_alloc_context
*meta_ac
= NULL
;
328 struct ocfs2_extent_tree et
;
330 ocfs2_init_xattr_value_extent_tree(&et
, inode
, root_bh
, xv
);
332 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
338 mutex_lock(&tl_inode
->i_mutex
);
340 if (ocfs2_truncate_log_needs_flush(osb
)) {
341 ret
= __ocfs2_flush_truncate_log(osb
);
348 handle
= ocfs2_start_trans(osb
, OCFS2_REMOVE_EXTENT_CREDITS
);
349 if (IS_ERR(handle
)) {
350 ret
= PTR_ERR(handle
);
355 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
356 OCFS2_JOURNAL_ACCESS_WRITE
);
362 ret
= ocfs2_remove_extent(inode
, &et
, cpos
, len
, handle
, meta_ac
,
369 le32_add_cpu(&xv
->xr_clusters
, -len
);
371 ret
= ocfs2_journal_dirty(handle
, root_bh
);
377 ret
= ocfs2_truncate_log_append(osb
, handle
, phys_blkno
, len
);
382 ocfs2_commit_trans(osb
, handle
);
384 mutex_unlock(&tl_inode
->i_mutex
);
387 ocfs2_free_alloc_context(meta_ac
);
392 static int ocfs2_xattr_shrink_size(struct inode
*inode
,
395 struct buffer_head
*root_bh
,
396 struct ocfs2_xattr_value_root
*xv
)
399 u32 trunc_len
, cpos
, phys_cpos
, alloc_size
;
401 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
402 struct ocfs2_cached_dealloc_ctxt dealloc
;
404 ocfs2_init_dealloc_ctxt(&dealloc
);
406 if (old_clusters
<= new_clusters
)
410 trunc_len
= old_clusters
- new_clusters
;
412 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &phys_cpos
,
413 &alloc_size
, &xv
->xr_list
);
419 if (alloc_size
> trunc_len
)
420 alloc_size
= trunc_len
;
422 ret
= __ocfs2_remove_xattr_range(inode
, root_bh
, xv
, cpos
,
423 phys_cpos
, alloc_size
,
430 block
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
431 ocfs2_remove_xattr_clusters_from_cache(inode
, block
,
434 trunc_len
-= alloc_size
;
438 ocfs2_schedule_truncate_log_flush(osb
, 1);
439 ocfs2_run_deallocs(osb
, &dealloc
);
444 static int ocfs2_xattr_value_truncate(struct inode
*inode
,
445 struct buffer_head
*root_bh
,
446 struct ocfs2_xattr_value_root
*xv
,
450 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, len
);
451 u32 old_clusters
= le32_to_cpu(xv
->xr_clusters
);
453 if (new_clusters
== old_clusters
)
456 if (new_clusters
> old_clusters
)
457 ret
= ocfs2_xattr_extend_allocation(inode
,
458 new_clusters
- old_clusters
,
461 ret
= ocfs2_xattr_shrink_size(inode
,
462 old_clusters
, new_clusters
,
468 static int ocfs2_xattr_list_entry(char *buffer
, size_t size
,
469 size_t *result
, const char *prefix
,
470 const char *name
, int name_len
)
472 char *p
= buffer
+ *result
;
473 int prefix_len
= strlen(prefix
);
474 int total_len
= prefix_len
+ name_len
+ 1;
476 *result
+= total_len
;
478 /* we are just looking for how big our buffer needs to be */
485 memcpy(p
, prefix
, prefix_len
);
486 memcpy(p
+ prefix_len
, name
, name_len
);
487 p
[prefix_len
+ name_len
] = '\0';
492 static int ocfs2_xattr_list_entries(struct inode
*inode
,
493 struct ocfs2_xattr_header
*header
,
494 char *buffer
, size_t buffer_size
)
498 const char *prefix
, *name
;
500 for (i
= 0 ; i
< le16_to_cpu(header
->xh_count
); i
++) {
501 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
502 type
= ocfs2_xattr_get_type(entry
);
503 prefix
= ocfs2_xattr_prefix(type
);
506 name
= (const char *)header
+
507 le16_to_cpu(entry
->xe_name_offset
);
509 ret
= ocfs2_xattr_list_entry(buffer
, buffer_size
,
510 &result
, prefix
, name
,
520 static int ocfs2_xattr_ibody_list(struct inode
*inode
,
521 struct ocfs2_dinode
*di
,
525 struct ocfs2_xattr_header
*header
= NULL
;
526 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
529 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
532 header
= (struct ocfs2_xattr_header
*)
533 ((void *)di
+ inode
->i_sb
->s_blocksize
-
534 le16_to_cpu(di
->i_xattr_inline_size
));
536 ret
= ocfs2_xattr_list_entries(inode
, header
, buffer
, buffer_size
);
541 static int ocfs2_xattr_block_list(struct inode
*inode
,
542 struct ocfs2_dinode
*di
,
546 struct buffer_head
*blk_bh
= NULL
;
547 struct ocfs2_xattr_block
*xb
;
550 if (!di
->i_xattr_loc
)
553 ret
= ocfs2_read_block(inode
, le64_to_cpu(di
->i_xattr_loc
), &blk_bh
);
558 /*Verify the signature of xattr block*/
559 if (memcmp((void *)blk_bh
->b_data
, OCFS2_XATTR_BLOCK_SIGNATURE
,
560 strlen(OCFS2_XATTR_BLOCK_SIGNATURE
))) {
565 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
567 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
568 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
569 ret
= ocfs2_xattr_list_entries(inode
, header
,
570 buffer
, buffer_size
);
572 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
573 ret
= ocfs2_xattr_tree_list_index_block(inode
, xt
,
574 buffer
, buffer_size
);
582 ssize_t
ocfs2_listxattr(struct dentry
*dentry
,
586 int ret
= 0, i_ret
= 0, b_ret
= 0;
587 struct buffer_head
*di_bh
= NULL
;
588 struct ocfs2_dinode
*di
= NULL
;
589 struct ocfs2_inode_info
*oi
= OCFS2_I(dentry
->d_inode
);
591 if (!ocfs2_supports_xattr(OCFS2_SB(dentry
->d_sb
)))
594 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
597 ret
= ocfs2_inode_lock(dentry
->d_inode
, &di_bh
, 0);
603 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
605 down_read(&oi
->ip_xattr_sem
);
606 i_ret
= ocfs2_xattr_ibody_list(dentry
->d_inode
, di
, buffer
, size
);
614 b_ret
= ocfs2_xattr_block_list(dentry
->d_inode
, di
,
619 up_read(&oi
->ip_xattr_sem
);
620 ocfs2_inode_unlock(dentry
->d_inode
, 0);
624 return i_ret
+ b_ret
;
627 static int ocfs2_xattr_find_entry(int name_index
,
629 struct ocfs2_xattr_search
*xs
)
631 struct ocfs2_xattr_entry
*entry
;
638 name_len
= strlen(name
);
640 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
641 cmp
= name_index
- ocfs2_xattr_get_type(entry
);
643 cmp
= name_len
- entry
->xe_name_len
;
645 cmp
= memcmp(name
, (xs
->base
+
646 le16_to_cpu(entry
->xe_name_offset
)),
654 return cmp
? -ENODATA
: 0;
657 static int ocfs2_xattr_get_value_outside(struct inode
*inode
,
658 struct ocfs2_xattr_value_root
*xv
,
662 u32 cpos
, p_cluster
, num_clusters
, bpc
, clusters
;
665 size_t cplen
, blocksize
;
666 struct buffer_head
*bh
= NULL
;
667 struct ocfs2_extent_list
*el
;
670 clusters
= le32_to_cpu(xv
->xr_clusters
);
671 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
672 blocksize
= inode
->i_sb
->s_blocksize
;
675 while (cpos
< clusters
) {
676 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
683 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
684 /* Copy ocfs2_xattr_value */
685 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
686 ret
= ocfs2_read_block(inode
, blkno
, &bh
);
692 cplen
= len
>= blocksize
? blocksize
: len
;
693 memcpy(buffer
, bh
->b_data
, cplen
);
702 cpos
+= num_clusters
;
708 static int ocfs2_xattr_ibody_get(struct inode
*inode
,
713 struct ocfs2_xattr_search
*xs
)
715 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
716 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
717 struct ocfs2_xattr_value_root
*xv
;
721 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
724 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
725 xs
->header
= (struct ocfs2_xattr_header
*)
726 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
727 xs
->base
= (void *)xs
->header
;
728 xs
->here
= xs
->header
->xh_entries
;
730 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
733 size
= le64_to_cpu(xs
->here
->xe_value_size
);
735 if (size
> buffer_size
)
737 if (ocfs2_xattr_is_local(xs
->here
)) {
738 memcpy(buffer
, (void *)xs
->base
+
739 le16_to_cpu(xs
->here
->xe_name_offset
) +
740 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
), size
);
742 xv
= (struct ocfs2_xattr_value_root
*)
743 (xs
->base
+ le16_to_cpu(
744 xs
->here
->xe_name_offset
) +
745 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
));
746 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
758 static int ocfs2_xattr_block_get(struct inode
*inode
,
763 struct ocfs2_xattr_search
*xs
)
765 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
766 struct buffer_head
*blk_bh
= NULL
;
767 struct ocfs2_xattr_block
*xb
;
768 struct ocfs2_xattr_value_root
*xv
;
770 int ret
= -ENODATA
, name_offset
, name_len
, block_off
, i
;
772 if (!di
->i_xattr_loc
)
775 memset(&xs
->bucket
, 0, sizeof(xs
->bucket
));
777 ret
= ocfs2_read_block(inode
, le64_to_cpu(di
->i_xattr_loc
), &blk_bh
);
782 /*Verify the signature of xattr block*/
783 if (memcmp((void *)blk_bh
->b_data
, OCFS2_XATTR_BLOCK_SIGNATURE
,
784 strlen(OCFS2_XATTR_BLOCK_SIGNATURE
))) {
789 xs
->xattr_bh
= blk_bh
;
790 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
792 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
793 xs
->header
= &xb
->xb_attrs
.xb_header
;
794 xs
->base
= (void *)xs
->header
;
795 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
796 xs
->here
= xs
->header
->xh_entries
;
798 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
800 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
806 size
= le64_to_cpu(xs
->here
->xe_value_size
);
809 if (size
> buffer_size
)
812 name_offset
= le16_to_cpu(xs
->here
->xe_name_offset
);
813 name_len
= OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
);
814 i
= xs
->here
- xs
->header
->xh_entries
;
816 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
817 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
822 xs
->base
= xs
->bucket
.bhs
[block_off
]->b_data
;
824 if (ocfs2_xattr_is_local(xs
->here
)) {
825 memcpy(buffer
, (void *)xs
->base
+
826 name_offset
+ name_len
, size
);
828 xv
= (struct ocfs2_xattr_value_root
*)
829 (xs
->base
+ name_offset
+ name_len
);
830 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
840 for (i
= 0; i
< OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
; i
++)
841 brelse(xs
->bucket
.bhs
[i
]);
842 memset(&xs
->bucket
, 0, sizeof(xs
->bucket
));
850 * Copy an extended attribute into the buffer provided.
851 * Buffer is NULL to compute the size of buffer required.
853 static int ocfs2_xattr_get(struct inode
*inode
,
860 struct ocfs2_dinode
*di
= NULL
;
861 struct buffer_head
*di_bh
= NULL
;
862 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
863 struct ocfs2_xattr_search xis
= {
864 .not_found
= -ENODATA
,
866 struct ocfs2_xattr_search xbs
= {
867 .not_found
= -ENODATA
,
870 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
873 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
876 ret
= ocfs2_inode_lock(inode
, &di_bh
, 0);
881 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
882 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
884 down_read(&oi
->ip_xattr_sem
);
885 ret
= ocfs2_xattr_ibody_get(inode
, name_index
, name
, buffer
,
888 ret
= ocfs2_xattr_block_get(inode
, name_index
, name
, buffer
,
890 up_read(&oi
->ip_xattr_sem
);
891 ocfs2_inode_unlock(inode
, 0);
898 static int __ocfs2_xattr_set_value_outside(struct inode
*inode
,
899 struct ocfs2_xattr_value_root
*xv
,
903 int ret
= 0, i
, cp_len
, credits
;
904 u16 blocksize
= inode
->i_sb
->s_blocksize
;
905 u32 p_cluster
, num_clusters
;
906 u32 cpos
= 0, bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
907 u32 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, value_len
);
909 struct buffer_head
*bh
= NULL
;
912 BUG_ON(clusters
> le32_to_cpu(xv
->xr_clusters
));
914 credits
= clusters
* bpc
;
915 handle
= ocfs2_start_trans(OCFS2_SB(inode
->i_sb
), credits
);
916 if (IS_ERR(handle
)) {
917 ret
= PTR_ERR(handle
);
922 while (cpos
< clusters
) {
923 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
924 &num_clusters
, &xv
->xr_list
);
930 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
932 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
933 ret
= ocfs2_read_block(inode
, blkno
, &bh
);
939 ret
= ocfs2_journal_access(handle
,
942 OCFS2_JOURNAL_ACCESS_WRITE
);
948 cp_len
= value_len
> blocksize
? blocksize
: value_len
;
949 memcpy(bh
->b_data
, value
, cp_len
);
952 if (cp_len
< blocksize
)
953 memset(bh
->b_data
+ cp_len
, 0,
956 ret
= ocfs2_journal_dirty(handle
, bh
);
965 * XXX: do we need to empty all the following
966 * blocks in this cluster?
971 cpos
+= num_clusters
;
974 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
981 static int ocfs2_xattr_cleanup(struct inode
*inode
,
982 struct ocfs2_xattr_info
*xi
,
983 struct ocfs2_xattr_search
*xs
,
986 handle_t
*handle
= NULL
;
988 size_t name_len
= strlen(xi
->name
);
989 void *val
= xs
->base
+ offs
;
990 size_t size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
992 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
993 OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
994 if (IS_ERR(handle
)) {
995 ret
= PTR_ERR(handle
);
999 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1000 OCFS2_JOURNAL_ACCESS_WRITE
);
1005 /* Decrease xattr count */
1006 le16_add_cpu(&xs
->header
->xh_count
, -1);
1007 /* Remove the xattr entry and tree root which has already be set*/
1008 memset((void *)xs
->here
, 0, sizeof(struct ocfs2_xattr_entry
));
1009 memset(val
, 0, size
);
1011 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1015 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1020 static int ocfs2_xattr_update_entry(struct inode
*inode
,
1021 struct ocfs2_xattr_info
*xi
,
1022 struct ocfs2_xattr_search
*xs
,
1025 handle_t
*handle
= NULL
;
1028 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1029 OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
1030 if (IS_ERR(handle
)) {
1031 ret
= PTR_ERR(handle
);
1035 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1036 OCFS2_JOURNAL_ACCESS_WRITE
);
1042 xs
->here
->xe_name_offset
= cpu_to_le16(offs
);
1043 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1044 if (xi
->value_len
<= OCFS2_XATTR_INLINE_SIZE
)
1045 ocfs2_xattr_set_local(xs
->here
, 1);
1047 ocfs2_xattr_set_local(xs
->here
, 0);
1048 ocfs2_xattr_hash_entry(inode
, xs
->header
, xs
->here
);
1050 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1054 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1060 * ocfs2_xattr_set_value_outside()
1062 * Set large size value in B tree.
1064 static int ocfs2_xattr_set_value_outside(struct inode
*inode
,
1065 struct ocfs2_xattr_info
*xi
,
1066 struct ocfs2_xattr_search
*xs
,
1069 size_t name_len
= strlen(xi
->name
);
1070 void *val
= xs
->base
+ offs
;
1071 struct ocfs2_xattr_value_root
*xv
= NULL
;
1072 size_t size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1075 memset(val
, 0, size
);
1076 memcpy(val
, xi
->name
, name_len
);
1077 xv
= (struct ocfs2_xattr_value_root
*)
1078 (val
+ OCFS2_XATTR_SIZE(name_len
));
1079 xv
->xr_clusters
= 0;
1080 xv
->xr_last_eb_blk
= 0;
1081 xv
->xr_list
.l_tree_depth
= 0;
1082 xv
->xr_list
.l_count
= cpu_to_le16(1);
1083 xv
->xr_list
.l_next_free_rec
= 0;
1085 ret
= ocfs2_xattr_value_truncate(inode
, xs
->xattr_bh
, xv
,
1091 ret
= __ocfs2_xattr_set_value_outside(inode
, xv
, xi
->value
,
1097 ret
= ocfs2_xattr_update_entry(inode
, xi
, xs
, offs
);
1105 * ocfs2_xattr_set_entry_local()
1107 * Set, replace or remove extended attribute in local.
1109 static void ocfs2_xattr_set_entry_local(struct inode
*inode
,
1110 struct ocfs2_xattr_info
*xi
,
1111 struct ocfs2_xattr_search
*xs
,
1112 struct ocfs2_xattr_entry
*last
,
1115 size_t name_len
= strlen(xi
->name
);
1118 if (xi
->value
&& xs
->not_found
) {
1119 /* Insert the new xattr entry. */
1120 le16_add_cpu(&xs
->header
->xh_count
, 1);
1121 ocfs2_xattr_set_type(last
, xi
->name_index
);
1122 ocfs2_xattr_set_local(last
, 1);
1123 last
->xe_name_len
= name_len
;
1129 first_val
= xs
->base
+ min_offs
;
1130 offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1131 val
= xs
->base
+ offs
;
1133 if (le64_to_cpu(xs
->here
->xe_value_size
) >
1134 OCFS2_XATTR_INLINE_SIZE
)
1135 size
= OCFS2_XATTR_SIZE(name_len
) +
1136 OCFS2_XATTR_ROOT_SIZE
;
1138 size
= OCFS2_XATTR_SIZE(name_len
) +
1139 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1141 if (xi
->value
&& size
== OCFS2_XATTR_SIZE(name_len
) +
1142 OCFS2_XATTR_SIZE(xi
->value_len
)) {
1143 /* The old and the new value have the
1144 same size. Just replace the value. */
1145 ocfs2_xattr_set_local(xs
->here
, 1);
1146 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1147 /* Clear value bytes. */
1148 memset(val
+ OCFS2_XATTR_SIZE(name_len
),
1150 OCFS2_XATTR_SIZE(xi
->value_len
));
1151 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
1156 /* Remove the old name+value. */
1157 memmove(first_val
+ size
, first_val
, val
- first_val
);
1158 memset(first_val
, 0, size
);
1159 xs
->here
->xe_name_hash
= 0;
1160 xs
->here
->xe_name_offset
= 0;
1161 ocfs2_xattr_set_local(xs
->here
, 1);
1162 xs
->here
->xe_value_size
= 0;
1166 /* Adjust all value offsets. */
1167 last
= xs
->header
->xh_entries
;
1168 for (i
= 0 ; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1169 size_t o
= le16_to_cpu(last
->xe_name_offset
);
1172 last
->xe_name_offset
= cpu_to_le16(o
+ size
);
1177 /* Remove the old entry. */
1179 memmove(xs
->here
, xs
->here
+ 1,
1180 (void *)last
- (void *)xs
->here
);
1181 memset(last
, 0, sizeof(struct ocfs2_xattr_entry
));
1182 le16_add_cpu(&xs
->header
->xh_count
, -1);
1186 /* Insert the new name+value. */
1187 size_t size
= OCFS2_XATTR_SIZE(name_len
) +
1188 OCFS2_XATTR_SIZE(xi
->value_len
);
1189 void *val
= xs
->base
+ min_offs
- size
;
1191 xs
->here
->xe_name_offset
= cpu_to_le16(min_offs
- size
);
1192 memset(val
, 0, size
);
1193 memcpy(val
, xi
->name
, name_len
);
1194 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
1197 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1198 ocfs2_xattr_set_local(xs
->here
, 1);
1199 ocfs2_xattr_hash_entry(inode
, xs
->header
, xs
->here
);
1206 * ocfs2_xattr_set_entry()
1208 * Set extended attribute entry into inode or block.
1210 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1211 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1212 * then set value in B tree with set_value_outside().
1214 static int ocfs2_xattr_set_entry(struct inode
*inode
,
1215 struct ocfs2_xattr_info
*xi
,
1216 struct ocfs2_xattr_search
*xs
,
1219 struct ocfs2_xattr_entry
*last
;
1220 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1221 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1222 size_t min_offs
= xs
->end
- xs
->base
, name_len
= strlen(xi
->name
);
1224 handle_t
*handle
= NULL
;
1226 struct ocfs2_xattr_info xi_l
= {
1227 .name_index
= xi
->name_index
,
1230 .value_len
= xi
->value_len
,
1233 /* Compute min_offs, last and free space. */
1234 last
= xs
->header
->xh_entries
;
1236 for (i
= 0 ; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1237 size_t offs
= le16_to_cpu(last
->xe_name_offset
);
1238 if (offs
< min_offs
)
1243 free
= min_offs
- ((void *)last
- xs
->base
) - sizeof(__u32
);
1247 if (!xs
->not_found
) {
1249 if (ocfs2_xattr_is_local(xs
->here
))
1250 size
= OCFS2_XATTR_SIZE(name_len
) +
1251 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1253 size
= OCFS2_XATTR_SIZE(name_len
) +
1254 OCFS2_XATTR_ROOT_SIZE
;
1255 free
+= (size
+ sizeof(struct ocfs2_xattr_entry
));
1257 /* Check free space in inode or block */
1258 if (xi
->value
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1259 if (free
< sizeof(struct ocfs2_xattr_entry
) +
1260 OCFS2_XATTR_SIZE(name_len
) +
1261 OCFS2_XATTR_ROOT_SIZE
) {
1265 size_l
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1266 xi_l
.value
= (void *)&def_xv
;
1267 xi_l
.value_len
= OCFS2_XATTR_ROOT_SIZE
;
1268 } else if (xi
->value
) {
1269 if (free
< sizeof(struct ocfs2_xattr_entry
) +
1270 OCFS2_XATTR_SIZE(name_len
) +
1271 OCFS2_XATTR_SIZE(xi
->value_len
)) {
1277 if (!xs
->not_found
) {
1278 /* For existing extended attribute */
1279 size_t size
= OCFS2_XATTR_SIZE(name_len
) +
1280 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1281 size_t offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1282 void *val
= xs
->base
+ offs
;
1284 if (ocfs2_xattr_is_local(xs
->here
) && size
== size_l
) {
1285 /* Replace existing local xattr with tree root */
1286 ret
= ocfs2_xattr_set_value_outside(inode
, xi
, xs
,
1291 } else if (!ocfs2_xattr_is_local(xs
->here
)) {
1292 /* For existing xattr which has value outside */
1293 struct ocfs2_xattr_value_root
*xv
= NULL
;
1294 xv
= (struct ocfs2_xattr_value_root
*)(val
+
1295 OCFS2_XATTR_SIZE(name_len
));
1297 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1299 * If new value need set outside also,
1300 * first truncate old value to new value,
1301 * then set new value with set_value_outside().
1303 ret
= ocfs2_xattr_value_truncate(inode
,
1312 ret
= __ocfs2_xattr_set_value_outside(inode
,
1321 ret
= ocfs2_xattr_update_entry(inode
,
1330 * If new value need set in local,
1331 * just trucate old value to zero.
1333 ret
= ocfs2_xattr_value_truncate(inode
,
1343 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1344 OCFS2_INODE_UPDATE_CREDITS
);
1345 if (IS_ERR(handle
)) {
1346 ret
= PTR_ERR(handle
);
1351 ret
= ocfs2_journal_access(handle
, inode
, xs
->inode_bh
,
1352 OCFS2_JOURNAL_ACCESS_WRITE
);
1358 if (!(flag
& OCFS2_INLINE_XATTR_FL
)) {
1359 /* set extended attribute in external block. */
1360 ret
= ocfs2_extend_trans(handle
,
1361 OCFS2_INODE_UPDATE_CREDITS
+
1362 OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
1367 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1368 OCFS2_JOURNAL_ACCESS_WRITE
);
1376 * Set value in local, include set tree root in local.
1377 * This is the first step for value size >INLINE_SIZE.
1379 ocfs2_xattr_set_entry_local(inode
, &xi_l
, xs
, last
, min_offs
);
1381 if (!(flag
& OCFS2_INLINE_XATTR_FL
)) {
1382 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1389 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) &&
1390 (flag
& OCFS2_INLINE_XATTR_FL
)) {
1391 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1392 unsigned int xattrsize
= osb
->s_xattr_inline_size
;
1395 * Adjust extent record count or inline data size
1396 * to reserve space for extended attribute.
1398 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1399 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
1400 le16_add_cpu(&idata
->id_count
, -xattrsize
);
1401 } else if (!(ocfs2_inode_is_fast_symlink(inode
))) {
1402 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
1403 le16_add_cpu(&el
->l_count
, -(xattrsize
/
1404 sizeof(struct ocfs2_extent_rec
)));
1406 di
->i_xattr_inline_size
= cpu_to_le16(xattrsize
);
1408 /* Update xattr flag */
1409 spin_lock(&oi
->ip_lock
);
1410 oi
->ip_dyn_features
|= flag
;
1411 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1412 spin_unlock(&oi
->ip_lock
);
1413 /* Update inode ctime */
1414 inode
->i_ctime
= CURRENT_TIME
;
1415 di
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
1416 di
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
1418 ret
= ocfs2_journal_dirty(handle
, xs
->inode_bh
);
1423 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1425 if (!ret
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1427 * Set value outside in B tree.
1428 * This is the second step for value size > INLINE_SIZE.
1430 size_t offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1431 ret
= ocfs2_xattr_set_value_outside(inode
, xi
, xs
, offs
);
1437 * If set value outside failed, we have to clean
1438 * the junk tree root we have already set in local.
1440 ret2
= ocfs2_xattr_cleanup(inode
, xi
, xs
, offs
);
1450 static int ocfs2_remove_value_outside(struct inode
*inode
,
1451 struct buffer_head
*bh
,
1452 struct ocfs2_xattr_header
*header
)
1456 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
1457 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
1459 if (!ocfs2_xattr_is_local(entry
)) {
1460 struct ocfs2_xattr_value_root
*xv
;
1463 val
= (void *)header
+
1464 le16_to_cpu(entry
->xe_name_offset
);
1465 xv
= (struct ocfs2_xattr_value_root
*)
1466 (val
+ OCFS2_XATTR_SIZE(entry
->xe_name_len
));
1467 ret
= ocfs2_xattr_value_truncate(inode
, bh
, xv
, 0);
1478 static int ocfs2_xattr_ibody_remove(struct inode
*inode
,
1479 struct buffer_head
*di_bh
)
1482 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1483 struct ocfs2_xattr_header
*header
;
1486 header
= (struct ocfs2_xattr_header
*)
1487 ((void *)di
+ inode
->i_sb
->s_blocksize
-
1488 le16_to_cpu(di
->i_xattr_inline_size
));
1490 ret
= ocfs2_remove_value_outside(inode
, di_bh
, header
);
1495 static int ocfs2_xattr_block_remove(struct inode
*inode
,
1496 struct buffer_head
*blk_bh
)
1498 struct ocfs2_xattr_block
*xb
;
1501 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1502 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1503 struct ocfs2_xattr_header
*header
= &(xb
->xb_attrs
.xb_header
);
1504 ret
= ocfs2_remove_value_outside(inode
, blk_bh
, header
);
1506 ret
= ocfs2_delete_xattr_index_block(inode
, blk_bh
);
1511 static int ocfs2_xattr_free_block(struct inode
*inode
,
1514 struct inode
*xb_alloc_inode
;
1515 struct buffer_head
*xb_alloc_bh
= NULL
;
1516 struct buffer_head
*blk_bh
= NULL
;
1517 struct ocfs2_xattr_block
*xb
;
1518 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1524 ret
= ocfs2_read_block(inode
, block
, &blk_bh
);
1530 /*Verify the signature of xattr block*/
1531 if (memcmp((void *)blk_bh
->b_data
, OCFS2_XATTR_BLOCK_SIGNATURE
,
1532 strlen(OCFS2_XATTR_BLOCK_SIGNATURE
))) {
1537 ret
= ocfs2_xattr_block_remove(inode
, blk_bh
);
1543 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1544 blk
= le64_to_cpu(xb
->xb_blkno
);
1545 bit
= le16_to_cpu(xb
->xb_suballoc_bit
);
1546 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1548 xb_alloc_inode
= ocfs2_get_system_file_inode(osb
,
1549 EXTENT_ALLOC_SYSTEM_INODE
,
1550 le16_to_cpu(xb
->xb_suballoc_slot
));
1551 if (!xb_alloc_inode
) {
1556 mutex_lock(&xb_alloc_inode
->i_mutex
);
1558 ret
= ocfs2_inode_lock(xb_alloc_inode
, &xb_alloc_bh
, 1);
1564 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
1565 if (IS_ERR(handle
)) {
1566 ret
= PTR_ERR(handle
);
1571 ret
= ocfs2_free_suballoc_bits(handle
, xb_alloc_inode
, xb_alloc_bh
,
1576 ocfs2_commit_trans(osb
, handle
);
1578 ocfs2_inode_unlock(xb_alloc_inode
, 1);
1579 brelse(xb_alloc_bh
);
1581 mutex_unlock(&xb_alloc_inode
->i_mutex
);
1582 iput(xb_alloc_inode
);
1589 * ocfs2_xattr_remove()
1591 * Free extended attribute resources associated with this inode.
1593 int ocfs2_xattr_remove(struct inode
*inode
, struct buffer_head
*di_bh
)
1595 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1596 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1600 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
1603 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1606 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
1607 ret
= ocfs2_xattr_ibody_remove(inode
, di_bh
);
1614 if (di
->i_xattr_loc
) {
1615 ret
= ocfs2_xattr_free_block(inode
,
1616 le64_to_cpu(di
->i_xattr_loc
));
1623 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1624 OCFS2_INODE_UPDATE_CREDITS
);
1625 if (IS_ERR(handle
)) {
1626 ret
= PTR_ERR(handle
);
1630 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
1631 OCFS2_JOURNAL_ACCESS_WRITE
);
1637 di
->i_xattr_loc
= 0;
1639 spin_lock(&oi
->ip_lock
);
1640 oi
->ip_dyn_features
&= ~(OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
);
1641 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1642 spin_unlock(&oi
->ip_lock
);
1644 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1648 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1653 static int ocfs2_xattr_has_space_inline(struct inode
*inode
,
1654 struct ocfs2_dinode
*di
)
1656 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1657 unsigned int xattrsize
= OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
;
1660 if (xattrsize
< OCFS2_MIN_XATTR_INLINE_SIZE
)
1663 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1664 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
1665 free
= le16_to_cpu(idata
->id_count
) - le64_to_cpu(di
->i_size
);
1666 } else if (ocfs2_inode_is_fast_symlink(inode
)) {
1667 free
= ocfs2_fast_symlink_chars(inode
->i_sb
) -
1668 le64_to_cpu(di
->i_size
);
1670 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
1671 free
= (le16_to_cpu(el
->l_count
) -
1672 le16_to_cpu(el
->l_next_free_rec
)) *
1673 sizeof(struct ocfs2_extent_rec
);
1675 if (free
>= xattrsize
)
1682 * ocfs2_xattr_ibody_find()
1684 * Find extended attribute in inode block and
1685 * fill search info into struct ocfs2_xattr_search.
1687 static int ocfs2_xattr_ibody_find(struct inode
*inode
,
1690 struct ocfs2_xattr_search
*xs
)
1692 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1693 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1697 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
1700 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
1701 down_read(&oi
->ip_alloc_sem
);
1702 has_space
= ocfs2_xattr_has_space_inline(inode
, di
);
1703 up_read(&oi
->ip_alloc_sem
);
1708 xs
->xattr_bh
= xs
->inode_bh
;
1709 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
1710 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)
1711 xs
->header
= (struct ocfs2_xattr_header
*)
1712 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
1714 xs
->header
= (struct ocfs2_xattr_header
*)
1715 (xs
->end
- OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
);
1716 xs
->base
= (void *)xs
->header
;
1717 xs
->here
= xs
->header
->xh_entries
;
1719 /* Find the named attribute. */
1720 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
1721 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1722 if (ret
&& ret
!= -ENODATA
)
1724 xs
->not_found
= ret
;
1731 * ocfs2_xattr_ibody_set()
1733 * Set, replace or remove an extended attribute into inode block.
1736 static int ocfs2_xattr_ibody_set(struct inode
*inode
,
1737 struct ocfs2_xattr_info
*xi
,
1738 struct ocfs2_xattr_search
*xs
)
1740 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1741 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1744 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
1747 down_write(&oi
->ip_alloc_sem
);
1748 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
1749 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
1755 ret
= ocfs2_xattr_set_entry(inode
, xi
, xs
,
1756 (OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
));
1758 up_write(&oi
->ip_alloc_sem
);
1764 * ocfs2_xattr_block_find()
1766 * Find extended attribute in external block and
1767 * fill search info into struct ocfs2_xattr_search.
1769 static int ocfs2_xattr_block_find(struct inode
*inode
,
1772 struct ocfs2_xattr_search
*xs
)
1774 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1775 struct buffer_head
*blk_bh
= NULL
;
1776 struct ocfs2_xattr_block
*xb
;
1779 if (!di
->i_xattr_loc
)
1782 ret
= ocfs2_read_block(inode
, le64_to_cpu(di
->i_xattr_loc
), &blk_bh
);
1787 /*Verify the signature of xattr block*/
1788 if (memcmp((void *)blk_bh
->b_data
, OCFS2_XATTR_BLOCK_SIGNATURE
,
1789 strlen(OCFS2_XATTR_BLOCK_SIGNATURE
))) {
1794 xs
->xattr_bh
= blk_bh
;
1795 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1797 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1798 xs
->header
= &xb
->xb_attrs
.xb_header
;
1799 xs
->base
= (void *)xs
->header
;
1800 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
1801 xs
->here
= xs
->header
->xh_entries
;
1803 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1805 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
1809 if (ret
&& ret
!= -ENODATA
) {
1810 xs
->xattr_bh
= NULL
;
1813 xs
->not_found
= ret
;
1822 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1823 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1826 static int ocfs2_restore_xattr_block(struct inode
*inode
,
1827 struct ocfs2_xattr_search
*xs
)
1831 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1832 struct ocfs2_xattr_block
*xb
=
1833 (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
1834 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
1835 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
1837 BUG_ON(!(xb_flags
& OCFS2_XATTR_INDEXED
) ||
1838 le16_to_cpu(el
->l_next_free_rec
) != 0);
1840 handle
= ocfs2_start_trans(osb
, OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
1841 if (IS_ERR(handle
)) {
1842 ret
= PTR_ERR(handle
);
1847 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1848 OCFS2_JOURNAL_ACCESS_WRITE
);
1854 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
1855 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
1857 xb
->xb_flags
= cpu_to_le16(xb_flags
& ~OCFS2_XATTR_INDEXED
);
1859 ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1862 ocfs2_commit_trans(osb
, handle
);
1868 * ocfs2_xattr_block_set()
1870 * Set, replace or remove an extended attribute into external block.
1873 static int ocfs2_xattr_block_set(struct inode
*inode
,
1874 struct ocfs2_xattr_info
*xi
,
1875 struct ocfs2_xattr_search
*xs
)
1877 struct buffer_head
*new_bh
= NULL
;
1878 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1879 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1880 struct ocfs2_alloc_context
*meta_ac
= NULL
;
1881 handle_t
*handle
= NULL
;
1882 struct ocfs2_xattr_block
*xblk
= NULL
;
1883 u16 suballoc_bit_start
;
1888 if (!xs
->xattr_bh
) {
1890 * Alloc one external block for extended attribute
1893 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, &meta_ac
);
1898 handle
= ocfs2_start_trans(osb
,
1899 OCFS2_XATTR_BLOCK_CREATE_CREDITS
);
1900 if (IS_ERR(handle
)) {
1901 ret
= PTR_ERR(handle
);
1905 ret
= ocfs2_journal_access(handle
, inode
, xs
->inode_bh
,
1906 OCFS2_JOURNAL_ACCESS_CREATE
);
1912 ret
= ocfs2_claim_metadata(osb
, handle
, meta_ac
, 1,
1913 &suballoc_bit_start
, &num_got
,
1920 new_bh
= sb_getblk(inode
->i_sb
, first_blkno
);
1921 ocfs2_set_new_buffer_uptodate(inode
, new_bh
);
1923 ret
= ocfs2_journal_access(handle
, inode
, new_bh
,
1924 OCFS2_JOURNAL_ACCESS_CREATE
);
1930 /* Initialize ocfs2_xattr_block */
1931 xs
->xattr_bh
= new_bh
;
1932 xblk
= (struct ocfs2_xattr_block
*)new_bh
->b_data
;
1933 memset(xblk
, 0, inode
->i_sb
->s_blocksize
);
1934 strcpy((void *)xblk
, OCFS2_XATTR_BLOCK_SIGNATURE
);
1935 xblk
->xb_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
1936 xblk
->xb_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
1937 xblk
->xb_fs_generation
= cpu_to_le32(osb
->fs_generation
);
1938 xblk
->xb_blkno
= cpu_to_le64(first_blkno
);
1940 xs
->header
= &xblk
->xb_attrs
.xb_header
;
1941 xs
->base
= (void *)xs
->header
;
1942 xs
->end
= (void *)xblk
+ inode
->i_sb
->s_blocksize
;
1943 xs
->here
= xs
->header
->xh_entries
;
1946 ret
= ocfs2_journal_dirty(handle
, new_bh
);
1951 di
->i_xattr_loc
= cpu_to_le64(first_blkno
);
1952 ret
= ocfs2_journal_dirty(handle
, xs
->inode_bh
);
1956 ocfs2_commit_trans(osb
, handle
);
1959 ocfs2_free_alloc_context(meta_ac
);
1963 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
1965 if (!(le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1966 /* Set extended attribute into external block */
1967 ret
= ocfs2_xattr_set_entry(inode
, xi
, xs
, OCFS2_HAS_XATTR_FL
);
1968 if (!ret
|| ret
!= -ENOSPC
)
1971 ret
= ocfs2_xattr_create_index_block(inode
, xs
);
1976 ret
= ocfs2_xattr_set_entry_index_block(inode
, xi
, xs
);
1977 if (!ret
&& xblk
->xb_attrs
.xb_root
.xt_list
.l_next_free_rec
== 0)
1978 ret
= ocfs2_restore_xattr_block(inode
, xs
);
1988 * Set, replace or remove an extended attribute for this inode.
1989 * value is NULL to remove an existing extended attribute, else either
1990 * create or replace an extended attribute.
1992 int ocfs2_xattr_set(struct inode
*inode
,
1999 struct buffer_head
*di_bh
= NULL
;
2000 struct ocfs2_dinode
*di
;
2002 u16 i
, blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2004 struct ocfs2_xattr_info xi
= {
2005 .name_index
= name_index
,
2008 .value_len
= value_len
,
2011 struct ocfs2_xattr_search xis
= {
2012 .not_found
= -ENODATA
,
2015 struct ocfs2_xattr_search xbs
= {
2016 .not_found
= -ENODATA
,
2019 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
2022 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
2027 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
2028 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2030 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2032 * Scan inode and external block to find the same name
2033 * extended attribute and collect search infomation.
2035 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
2038 if (xis
.not_found
) {
2039 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
2044 if (xis
.not_found
&& xbs
.not_found
) {
2046 if (flags
& XATTR_REPLACE
)
2053 if (flags
& XATTR_CREATE
)
2058 /* Remove existing extended attribute */
2060 ret
= ocfs2_xattr_ibody_set(inode
, &xi
, &xis
);
2061 else if (!xbs
.not_found
)
2062 ret
= ocfs2_xattr_block_set(inode
, &xi
, &xbs
);
2064 /* We always try to set extended attribute into inode first*/
2065 ret
= ocfs2_xattr_ibody_set(inode
, &xi
, &xis
);
2066 if (!ret
&& !xbs
.not_found
) {
2068 * If succeed and that extended attribute existing in
2069 * external block, then we will remove it.
2073 ret
= ocfs2_xattr_block_set(inode
, &xi
, &xbs
);
2074 } else if (ret
== -ENOSPC
) {
2075 if (di
->i_xattr_loc
&& !xbs
.xattr_bh
) {
2076 ret
= ocfs2_xattr_block_find(inode
, name_index
,
2082 * If no space in inode, we will set extended attribute
2083 * into external block.
2085 ret
= ocfs2_xattr_block_set(inode
, &xi
, &xbs
);
2088 if (!xis
.not_found
) {
2090 * If succeed and that extended attribute
2091 * existing in inode, we will remove it.
2095 ret
= ocfs2_xattr_ibody_set(inode
, &xi
, &xis
);
2100 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2101 ocfs2_inode_unlock(inode
, 1);
2103 brelse(xbs
.xattr_bh
);
2104 for (i
= 0; i
< blk_per_bucket
; i
++)
2105 brelse(xbs
.bucket
.bhs
[i
]);
2111 * Find the xattr extent rec which may contains name_hash.
2112 * e_cpos will be the first name hash of the xattr rec.
2113 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2115 static int ocfs2_xattr_get_rec(struct inode
*inode
,
2120 struct ocfs2_extent_list
*el
)
2123 struct buffer_head
*eb_bh
= NULL
;
2124 struct ocfs2_extent_block
*eb
;
2125 struct ocfs2_extent_rec
*rec
= NULL
;
2128 if (el
->l_tree_depth
) {
2129 ret
= ocfs2_find_leaf(inode
, el
, name_hash
, &eb_bh
);
2135 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
2138 if (el
->l_tree_depth
) {
2139 ocfs2_error(inode
->i_sb
,
2140 "Inode %lu has non zero tree depth in "
2141 "xattr tree block %llu\n", inode
->i_ino
,
2142 (unsigned long long)eb_bh
->b_blocknr
);
2148 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
2149 rec
= &el
->l_recs
[i
];
2151 if (le32_to_cpu(rec
->e_cpos
) <= name_hash
) {
2152 e_blkno
= le64_to_cpu(rec
->e_blkno
);
2158 ocfs2_error(inode
->i_sb
, "Inode %lu has bad extent "
2159 "record (%u, %u, 0) in xattr", inode
->i_ino
,
2160 le32_to_cpu(rec
->e_cpos
),
2161 ocfs2_rec_clusters(el
, rec
));
2166 *p_blkno
= le64_to_cpu(rec
->e_blkno
);
2167 *num_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
2169 *e_cpos
= le32_to_cpu(rec
->e_cpos
);
2175 typedef int (xattr_bucket_func
)(struct inode
*inode
,
2176 struct ocfs2_xattr_bucket
*bucket
,
2179 static int ocfs2_find_xe_in_bucket(struct inode
*inode
,
2180 struct buffer_head
*header_bh
,
2187 int i
, ret
= 0, cmp
= 1, block_off
, new_offset
;
2188 struct ocfs2_xattr_header
*xh
=
2189 (struct ocfs2_xattr_header
*)header_bh
->b_data
;
2190 size_t name_len
= strlen(name
);
2191 struct ocfs2_xattr_entry
*xe
= NULL
;
2192 struct buffer_head
*name_bh
= NULL
;
2196 * We don't use binary search in the bucket because there
2197 * may be multiple entries with the same name hash.
2199 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
2200 xe
= &xh
->xh_entries
[i
];
2202 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
))
2204 else if (name_hash
< le32_to_cpu(xe
->xe_name_hash
))
2207 cmp
= name_index
- ocfs2_xattr_get_type(xe
);
2209 cmp
= name_len
- xe
->xe_name_len
;
2213 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
2223 ret
= ocfs2_read_block(inode
, header_bh
->b_blocknr
+ block_off
,
2229 xe_name
= name_bh
->b_data
+ new_offset
;
2231 cmp
= memcmp(name
, xe_name
, name_len
);
2247 * Find the specified xattr entry in a series of buckets.
2248 * This series start from p_blkno and last for num_clusters.
2249 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2250 * the num of the valid buckets.
2252 * Return the buffer_head this xattr should reside in. And if the xattr's
2253 * hash is in the gap of 2 buckets, return the lower bucket.
2255 static int ocfs2_xattr_bucket_find(struct inode
*inode
,
2262 struct ocfs2_xattr_search
*xs
)
2265 struct buffer_head
*bh
= NULL
;
2266 struct buffer_head
*lower_bh
= NULL
;
2267 struct ocfs2_xattr_header
*xh
= NULL
;
2268 struct ocfs2_xattr_entry
*xe
= NULL
;
2270 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2271 int low_bucket
= 0, bucket
, high_bucket
;
2275 ret
= ocfs2_read_block(inode
, p_blkno
, &bh
);
2281 xh
= (struct ocfs2_xattr_header
*)bh
->b_data
;
2282 high_bucket
= le16_to_cpu(xh
->xh_num_buckets
) - 1;
2284 while (low_bucket
<= high_bucket
) {
2287 bucket
= (low_bucket
+ high_bucket
) / 2;
2289 blkno
= p_blkno
+ bucket
* blk_per_bucket
;
2291 ret
= ocfs2_read_block(inode
, blkno
, &bh
);
2297 xh
= (struct ocfs2_xattr_header
*)bh
->b_data
;
2298 xe
= &xh
->xh_entries
[0];
2299 if (name_hash
< le32_to_cpu(xe
->xe_name_hash
)) {
2300 high_bucket
= bucket
- 1;
2305 * Check whether the hash of the last entry in our
2306 * bucket is larger than the search one. for an empty
2307 * bucket, the last one is also the first one.
2310 xe
= &xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1];
2312 last_hash
= le32_to_cpu(xe
->xe_name_hash
);
2314 /* record lower_bh which may be the insert place. */
2319 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
)) {
2320 low_bucket
= bucket
+ 1;
2324 /* the searched xattr should reside in this bucket if exists. */
2325 ret
= ocfs2_find_xe_in_bucket(inode
, lower_bh
,
2326 name_index
, name
, name_hash
,
2336 * Record the bucket we have found.
2337 * When the xattr's hash value is in the gap of 2 buckets, we will
2338 * always set it to the previous bucket.
2342 * We can't find any bucket whose first name_hash is less
2343 * than the find name_hash.
2345 BUG_ON(bh
->b_blocknr
!= p_blkno
);
2349 xs
->bucket
.bhs
[0] = lower_bh
;
2350 xs
->bucket
.xh
= (struct ocfs2_xattr_header
*)
2351 xs
->bucket
.bhs
[0]->b_data
;
2354 xs
->header
= xs
->bucket
.xh
;
2355 xs
->base
= xs
->bucket
.bhs
[0]->b_data
;
2356 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
2360 * If we have found the xattr enty, read all the blocks in
2363 ret
= ocfs2_read_blocks(inode
, xs
->bucket
.bhs
[0]->b_blocknr
+ 1,
2364 blk_per_bucket
- 1, &xs
->bucket
.bhs
[1],
2371 xs
->here
= &xs
->header
->xh_entries
[index
];
2372 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name
,
2373 (unsigned long long)xs
->bucket
.bhs
[0]->b_blocknr
, index
);
2383 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
2384 struct buffer_head
*root_bh
,
2387 struct ocfs2_xattr_search
*xs
)
2390 struct ocfs2_xattr_block
*xb
=
2391 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
2392 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
2393 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
2395 u32 first_hash
, num_clusters
= 0;
2396 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
2398 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
2401 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2402 name
, name_hash
, name_index
);
2404 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &first_hash
,
2411 BUG_ON(p_blkno
== 0 || num_clusters
== 0 || first_hash
> name_hash
);
2413 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2414 "in the rec is %u\n", num_clusters
, p_blkno
, first_hash
);
2416 ret
= ocfs2_xattr_bucket_find(inode
, name_index
, name
, name_hash
,
2417 p_blkno
, first_hash
, num_clusters
, xs
);
2423 static int ocfs2_iterate_xattr_buckets(struct inode
*inode
,
2426 xattr_bucket_func
*func
,
2430 int blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2431 u32 bpc
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
2432 u32 num_buckets
= clusters
* bpc
;
2433 struct ocfs2_xattr_bucket bucket
;
2435 memset(&bucket
, 0, sizeof(bucket
));
2437 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2440 for (i
= 0; i
< num_buckets
; i
++, blkno
+= blk_per_bucket
) {
2441 ret
= ocfs2_read_blocks(inode
, blkno
, blk_per_bucket
,
2448 bucket
.xh
= (struct ocfs2_xattr_header
*)bucket
.bhs
[0]->b_data
;
2450 * The real bucket num in this series of blocks is stored
2451 * in the 1st bucket.
2454 num_buckets
= le16_to_cpu(bucket
.xh
->xh_num_buckets
);
2456 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno
,
2457 le32_to_cpu(bucket
.xh
->xh_entries
[0].xe_name_hash
));
2459 ret
= func(inode
, &bucket
, para
);
2466 for (j
= 0; j
< blk_per_bucket
; j
++)
2467 brelse(bucket
.bhs
[j
]);
2468 memset(&bucket
, 0, sizeof(bucket
));
2472 for (j
= 0; j
< blk_per_bucket
; j
++)
2473 brelse(bucket
.bhs
[j
]);
2478 struct ocfs2_xattr_tree_list
{
2484 static int ocfs2_xattr_bucket_get_name_value(struct inode
*inode
,
2485 struct ocfs2_xattr_header
*xh
,
2492 if (index
< 0 || index
>= le16_to_cpu(xh
->xh_count
))
2495 name_offset
= le16_to_cpu(xh
->xh_entries
[index
].xe_name_offset
);
2497 *block_off
= name_offset
>> inode
->i_sb
->s_blocksize_bits
;
2498 *new_offset
= name_offset
% inode
->i_sb
->s_blocksize
;
2503 static int ocfs2_list_xattr_bucket(struct inode
*inode
,
2504 struct ocfs2_xattr_bucket
*bucket
,
2508 struct ocfs2_xattr_tree_list
*xl
= (struct ocfs2_xattr_tree_list
*)para
;
2509 int i
, block_off
, new_offset
;
2510 const char *prefix
, *name
;
2512 for (i
= 0 ; i
< le16_to_cpu(bucket
->xh
->xh_count
); i
++) {
2513 struct ocfs2_xattr_entry
*entry
= &bucket
->xh
->xh_entries
[i
];
2514 type
= ocfs2_xattr_get_type(entry
);
2515 prefix
= ocfs2_xattr_prefix(type
);
2518 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
2526 name
= (const char *)bucket
->bhs
[block_off
]->b_data
+
2528 ret
= ocfs2_xattr_list_entry(xl
->buffer
,
2532 entry
->xe_name_len
);
2541 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
2542 struct ocfs2_xattr_tree_root
*xt
,
2546 struct ocfs2_extent_list
*el
= &xt
->xt_list
;
2548 u32 name_hash
= UINT_MAX
, e_cpos
= 0, num_clusters
= 0;
2550 struct ocfs2_xattr_tree_list xl
= {
2552 .buffer_size
= buffer_size
,
2556 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
2559 while (name_hash
> 0) {
2560 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
2561 &e_cpos
, &num_clusters
, el
);
2567 ret
= ocfs2_iterate_xattr_buckets(inode
, p_blkno
, num_clusters
,
2568 ocfs2_list_xattr_bucket
,
2578 name_hash
= e_cpos
- 1;
2586 static int cmp_xe(const void *a
, const void *b
)
2588 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
2589 u32 l_hash
= le32_to_cpu(l
->xe_name_hash
);
2590 u32 r_hash
= le32_to_cpu(r
->xe_name_hash
);
2592 if (l_hash
> r_hash
)
2594 if (l_hash
< r_hash
)
2599 static void swap_xe(void *a
, void *b
, int size
)
2601 struct ocfs2_xattr_entry
*l
= a
, *r
= b
, tmp
;
2604 memcpy(l
, r
, sizeof(struct ocfs2_xattr_entry
));
2605 memcpy(r
, &tmp
, sizeof(struct ocfs2_xattr_entry
));
2609 * When the ocfs2_xattr_block is filled up, new bucket will be created
2610 * and all the xattr entries will be moved to the new bucket.
2611 * Note: we need to sort the entries since they are not saved in order
2612 * in the ocfs2_xattr_block.
2614 static void ocfs2_cp_xattr_block_to_bucket(struct inode
*inode
,
2615 struct buffer_head
*xb_bh
,
2616 struct buffer_head
*xh_bh
,
2617 struct buffer_head
*data_bh
)
2619 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
2620 u16 offset
, size
, off_change
;
2621 struct ocfs2_xattr_entry
*xe
;
2622 struct ocfs2_xattr_block
*xb
=
2623 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
2624 struct ocfs2_xattr_header
*xb_xh
= &xb
->xb_attrs
.xb_header
;
2625 struct ocfs2_xattr_header
*xh
=
2626 (struct ocfs2_xattr_header
*)xh_bh
->b_data
;
2627 u16 count
= le16_to_cpu(xb_xh
->xh_count
);
2628 char *target
= xh_bh
->b_data
, *src
= xb_bh
->b_data
;
2630 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2631 (unsigned long long)xb_bh
->b_blocknr
,
2632 (unsigned long long)xh_bh
->b_blocknr
);
2634 memset(xh_bh
->b_data
, 0, blocksize
);
2636 memset(data_bh
->b_data
, 0, blocksize
);
2638 * Since the xe_name_offset is based on ocfs2_xattr_header,
2639 * there is a offset change corresponding to the change of
2640 * ocfs2_xattr_header's position.
2642 off_change
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
2643 xe
= &xb_xh
->xh_entries
[count
- 1];
2644 offset
= le16_to_cpu(xe
->xe_name_offset
) + off_change
;
2645 size
= blocksize
- offset
;
2647 /* copy all the names and values. */
2649 target
= data_bh
->b_data
;
2650 memcpy(target
+ offset
, src
+ offset
, size
);
2652 /* Init new header now. */
2653 xh
->xh_count
= xb_xh
->xh_count
;
2654 xh
->xh_num_buckets
= cpu_to_le16(1);
2655 xh
->xh_name_value_len
= cpu_to_le16(size
);
2656 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
- size
);
2658 /* copy all the entries. */
2659 target
= xh_bh
->b_data
;
2660 offset
= offsetof(struct ocfs2_xattr_header
, xh_entries
);
2661 size
= count
* sizeof(struct ocfs2_xattr_entry
);
2662 memcpy(target
+ offset
, (char *)xb_xh
+ offset
, size
);
2664 /* Change the xe offset for all the xe because of the move. */
2665 off_change
= OCFS2_XATTR_BUCKET_SIZE
- blocksize
+
2666 offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
2667 for (i
= 0; i
< count
; i
++)
2668 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
, off_change
);
2670 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2671 offset
, size
, off_change
);
2673 sort(target
+ offset
, count
, sizeof(struct ocfs2_xattr_entry
),
2678 * After we move xattr from block to index btree, we have to
2679 * update ocfs2_xattr_search to the new xe and base.
2681 * When the entry is in xattr block, xattr_bh indicates the storage place.
2682 * While if the entry is in index b-tree, "bucket" indicates the
2683 * real place of the xattr.
2685 static int ocfs2_xattr_update_xattr_search(struct inode
*inode
,
2686 struct ocfs2_xattr_search
*xs
,
2687 struct buffer_head
*old_bh
,
2688 struct buffer_head
*new_bh
)
2691 char *buf
= old_bh
->b_data
;
2692 struct ocfs2_xattr_block
*old_xb
= (struct ocfs2_xattr_block
*)buf
;
2693 struct ocfs2_xattr_header
*old_xh
= &old_xb
->xb_attrs
.xb_header
;
2694 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
2695 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2697 xs
->bucket
.bhs
[0] = new_bh
;
2699 xs
->bucket
.xh
= (struct ocfs2_xattr_header
*)xs
->bucket
.bhs
[0]->b_data
;
2700 xs
->header
= xs
->bucket
.xh
;
2702 xs
->base
= new_bh
->b_data
;
2703 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
2705 if (!xs
->not_found
) {
2706 if (OCFS2_XATTR_BUCKET_SIZE
!= blocksize
) {
2707 ret
= ocfs2_read_blocks(inode
,
2708 xs
->bucket
.bhs
[0]->b_blocknr
+ 1,
2709 blk_per_bucket
- 1, &xs
->bucket
.bhs
[1],
2716 i
= xs
->here
- old_xh
->xh_entries
;
2717 xs
->here
= &xs
->header
->xh_entries
[i
];
2724 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
2725 struct ocfs2_xattr_search
*xs
)
2727 int ret
, credits
= OCFS2_SUBALLOC_ALLOC
;
2731 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2732 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2733 struct ocfs2_alloc_context
*data_ac
;
2734 struct buffer_head
*xh_bh
= NULL
, *data_bh
= NULL
;
2735 struct buffer_head
*xb_bh
= xs
->xattr_bh
;
2736 struct ocfs2_xattr_block
*xb
=
2737 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
2738 struct ocfs2_xattr_tree_root
*xr
;
2739 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
2740 u16 bpb
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2742 mlog(0, "create xattr index block for %llu\n",
2743 (unsigned long long)xb_bh
->b_blocknr
);
2745 BUG_ON(xb_flags
& OCFS2_XATTR_INDEXED
);
2747 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
2755 * We can use this lock for now, and maybe move to a dedicated mutex
2756 * if performance becomes a problem later.
2758 down_write(&oi
->ip_alloc_sem
);
2761 * 3 more credits, one for xattr block update, one for the 1st block
2762 * of the new xattr bucket and one for the value/data.
2765 handle
= ocfs2_start_trans(osb
, credits
);
2766 if (IS_ERR(handle
)) {
2767 ret
= PTR_ERR(handle
);
2772 ret
= ocfs2_journal_access(handle
, inode
, xb_bh
,
2773 OCFS2_JOURNAL_ACCESS_WRITE
);
2779 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
, &len
);
2786 * The bucket may spread in many blocks, and
2787 * we will only touch the 1st block and the last block
2788 * in the whole bucket(one for entry and one for data).
2790 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
2792 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno
);
2794 xh_bh
= sb_getblk(inode
->i_sb
, blkno
);
2801 ocfs2_set_new_buffer_uptodate(inode
, xh_bh
);
2803 ret
= ocfs2_journal_access(handle
, inode
, xh_bh
,
2804 OCFS2_JOURNAL_ACCESS_CREATE
);
2811 data_bh
= sb_getblk(inode
->i_sb
, blkno
+ bpb
- 1);
2818 ocfs2_set_new_buffer_uptodate(inode
, data_bh
);
2820 ret
= ocfs2_journal_access(handle
, inode
, data_bh
,
2821 OCFS2_JOURNAL_ACCESS_CREATE
);
2828 ocfs2_cp_xattr_block_to_bucket(inode
, xb_bh
, xh_bh
, data_bh
);
2830 ocfs2_journal_dirty(handle
, xh_bh
);
2832 ocfs2_journal_dirty(handle
, data_bh
);
2834 ocfs2_xattr_update_xattr_search(inode
, xs
, xb_bh
, xh_bh
);
2836 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2837 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
2838 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
2840 xr
= &xb
->xb_attrs
.xb_root
;
2841 xr
->xt_clusters
= cpu_to_le32(1);
2842 xr
->xt_last_eb_blk
= 0;
2843 xr
->xt_list
.l_tree_depth
= 0;
2844 xr
->xt_list
.l_count
= cpu_to_le16(ocfs2_xattr_recs_per_xb(inode
->i_sb
));
2845 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
2847 xr
->xt_list
.l_recs
[0].e_cpos
= 0;
2848 xr
->xt_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
2849 xr
->xt_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
2851 xb
->xb_flags
= cpu_to_le16(xb_flags
| OCFS2_XATTR_INDEXED
);
2853 ret
= ocfs2_journal_dirty(handle
, xb_bh
);
2860 ocfs2_commit_trans(osb
, handle
);
2863 up_write(&oi
->ip_alloc_sem
);
2867 ocfs2_free_alloc_context(data_ac
);
2875 static int cmp_xe_offset(const void *a
, const void *b
)
2877 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
2878 u32 l_name_offset
= le16_to_cpu(l
->xe_name_offset
);
2879 u32 r_name_offset
= le16_to_cpu(r
->xe_name_offset
);
2881 if (l_name_offset
< r_name_offset
)
2883 if (l_name_offset
> r_name_offset
)
2889 * defrag a xattr bucket if we find that the bucket has some
2890 * holes beteen name/value pairs.
2891 * We will move all the name/value pairs to the end of the bucket
2892 * so that we can spare some space for insertion.
2894 static int ocfs2_defrag_xattr_bucket(struct inode
*inode
,
2895 struct ocfs2_xattr_bucket
*bucket
)
2898 size_t end
, offset
, len
, value_len
;
2899 struct ocfs2_xattr_header
*xh
;
2900 char *entries
, *buf
, *bucket_buf
= NULL
;
2901 u64 blkno
= bucket
->bhs
[0]->b_blocknr
;
2902 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2904 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2906 struct buffer_head
**bhs
;
2907 struct ocfs2_xattr_entry
*xe
;
2909 bhs
= kzalloc(sizeof(struct buffer_head
*) * blk_per_bucket
,
2914 ret
= ocfs2_read_blocks(inode
, blkno
, blk_per_bucket
, bhs
, 0);
2919 * In order to make the operation more efficient and generic,
2920 * we copy all the blocks into a contiguous memory and do the
2921 * defragment there, so if anything is error, we will not touch
2924 bucket_buf
= kmalloc(OCFS2_XATTR_BUCKET_SIZE
, GFP_NOFS
);
2931 for (i
= 0; i
< blk_per_bucket
; i
++, buf
+= blocksize
)
2932 memcpy(buf
, bhs
[i
]->b_data
, blocksize
);
2934 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)), blk_per_bucket
);
2935 if (IS_ERR(handle
)) {
2936 ret
= PTR_ERR(handle
);
2942 for (i
= 0; i
< blk_per_bucket
; i
++) {
2943 ret
= ocfs2_journal_access(handle
, inode
, bhs
[i
],
2944 OCFS2_JOURNAL_ACCESS_WRITE
);
2951 xh
= (struct ocfs2_xattr_header
*)bucket_buf
;
2952 entries
= (char *)xh
->xh_entries
;
2953 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
2955 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2956 "xh_free_start = %u, xh_name_value_len = %u.\n",
2957 blkno
, le16_to_cpu(xh
->xh_count
), xh_free_start
,
2958 le16_to_cpu(xh
->xh_name_value_len
));
2961 * sort all the entries by their offset.
2962 * the largest will be the first, so that we can
2963 * move them to the end one by one.
2965 sort(entries
, le16_to_cpu(xh
->xh_count
),
2966 sizeof(struct ocfs2_xattr_entry
),
2967 cmp_xe_offset
, swap_xe
);
2969 /* Move all name/values to the end of the bucket. */
2970 xe
= xh
->xh_entries
;
2971 end
= OCFS2_XATTR_BUCKET_SIZE
;
2972 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, xe
++) {
2973 offset
= le16_to_cpu(xe
->xe_name_offset
);
2974 if (ocfs2_xattr_is_local(xe
))
2975 value_len
= OCFS2_XATTR_SIZE(
2976 le64_to_cpu(xe
->xe_value_size
));
2978 value_len
= OCFS2_XATTR_ROOT_SIZE
;
2979 len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
) + value_len
;
2982 * We must make sure that the name/value pair
2983 * exist in the same block. So adjust end to
2984 * the previous block end if needed.
2986 if (((end
- len
) / blocksize
!=
2987 (end
- 1) / blocksize
))
2988 end
= end
- end
% blocksize
;
2990 if (end
> offset
+ len
) {
2991 memmove(bucket_buf
+ end
- len
,
2992 bucket_buf
+ offset
, len
);
2993 xe
->xe_name_offset
= cpu_to_le16(end
- len
);
2996 mlog_bug_on_msg(end
< offset
+ len
, "Defrag check failed for "
2997 "bucket %llu\n", (unsigned long long)blkno
);
3002 mlog_bug_on_msg(xh_free_start
> end
, "Defrag check failed for "
3003 "bucket %llu\n", (unsigned long long)blkno
);
3005 if (xh_free_start
== end
)
3008 memset(bucket_buf
+ xh_free_start
, 0, end
- xh_free_start
);
3009 xh
->xh_free_start
= cpu_to_le16(end
);
3011 /* sort the entries by their name_hash. */
3012 sort(entries
, le16_to_cpu(xh
->xh_count
),
3013 sizeof(struct ocfs2_xattr_entry
),
3017 for (i
= 0; i
< blk_per_bucket
; i
++, buf
+= blocksize
) {
3018 memcpy(bhs
[i
]->b_data
, buf
, blocksize
);
3019 ocfs2_journal_dirty(handle
, bhs
[i
]);
3023 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
3027 for (i
= 0; i
< blk_per_bucket
; i
++)
3037 * Move half nums of the xattr bucket in the previous cluster to this new
3038 * cluster. We only touch the last cluster of the previous extend record.
3040 * first_bh is the first buffer_head of a series of bucket in the same
3041 * extent rec and header_bh is the header of one bucket in this cluster.
3042 * They will be updated if we move the data header_bh contains to the new
3043 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3045 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode
*inode
,
3047 struct buffer_head
**first_bh
,
3048 struct buffer_head
**header_bh
,
3054 int i
, ret
, credits
;
3055 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3056 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3057 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
3058 int blocksize
= inode
->i_sb
->s_blocksize
;
3059 struct buffer_head
*old_bh
, *new_bh
, *prev_bh
, *new_first_bh
= NULL
;
3060 struct ocfs2_xattr_header
*new_xh
;
3061 struct ocfs2_xattr_header
*xh
=
3062 (struct ocfs2_xattr_header
*)((*first_bh
)->b_data
);
3064 BUG_ON(le16_to_cpu(xh
->xh_num_buckets
) < num_buckets
);
3065 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
== osb
->s_clustersize
);
3067 prev_bh
= *first_bh
;
3069 xh
= (struct ocfs2_xattr_header
*)prev_bh
->b_data
;
3071 prev_blkno
+= (num_clusters
- 1) * bpc
+ bpc
/ 2;
3073 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3074 prev_blkno
, new_blkno
);
3077 * We need to update the 1st half of the new cluster and
3078 * 1 more for the update of the 1st bucket of the previous
3081 credits
= bpc
/ 2 + 1;
3082 ret
= ocfs2_extend_trans(handle
, credits
);
3088 ret
= ocfs2_journal_access(handle
, inode
, prev_bh
,
3089 OCFS2_JOURNAL_ACCESS_WRITE
);
3095 for (i
= 0; i
< bpc
/ 2; i
++, prev_blkno
++, new_blkno
++) {
3096 old_bh
= new_bh
= NULL
;
3097 new_bh
= sb_getblk(inode
->i_sb
, new_blkno
);
3104 ocfs2_set_new_buffer_uptodate(inode
, new_bh
);
3106 ret
= ocfs2_journal_access(handle
, inode
, new_bh
,
3107 OCFS2_JOURNAL_ACCESS_CREATE
);
3114 ret
= ocfs2_read_block(inode
, prev_blkno
, &old_bh
);
3121 memcpy(new_bh
->b_data
, old_bh
->b_data
, blocksize
);
3124 new_xh
= (struct ocfs2_xattr_header
*)new_bh
->b_data
;
3125 new_xh
->xh_num_buckets
= cpu_to_le16(num_buckets
/ 2);
3128 *first_hash
= le32_to_cpu(
3129 new_xh
->xh_entries
[0].xe_name_hash
);
3130 new_first_bh
= new_bh
;
3131 get_bh(new_first_bh
);
3134 ocfs2_journal_dirty(handle
, new_bh
);
3136 if (*header_bh
== old_bh
) {
3138 *header_bh
= new_bh
;
3142 *first_bh
= new_first_bh
;
3149 le16_add_cpu(&xh
->xh_num_buckets
, -(num_buckets
/ 2));
3151 ocfs2_journal_dirty(handle
, prev_bh
);
3154 brelse(new_first_bh
);
3158 static int ocfs2_read_xattr_bucket(struct inode
*inode
,
3160 struct buffer_head
**bhs
,
3164 u16 i
, blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3167 return ocfs2_read_blocks(inode
, blkno
,
3168 blk_per_bucket
, bhs
, 0);
3170 for (i
= 0; i
< blk_per_bucket
; i
++) {
3171 bhs
[i
] = sb_getblk(inode
->i_sb
, blkno
+ i
);
3172 if (bhs
[i
] == NULL
) {
3177 ocfs2_set_new_buffer_uptodate(inode
, bhs
[i
]);
3184 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3185 * first_hash will record the 1st hash of the new bucket.
3187 static int ocfs2_half_xattr_bucket(struct inode
*inode
,
3192 int new_bucket_head
)
3195 u16 count
, start
, len
, name_value_len
, xe_len
, name_offset
;
3196 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3197 struct buffer_head
**s_bhs
, **t_bhs
= NULL
;
3198 struct ocfs2_xattr_header
*xh
;
3199 struct ocfs2_xattr_entry
*xe
;
3200 int blocksize
= inode
->i_sb
->s_blocksize
;
3202 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3205 s_bhs
= kcalloc(blk_per_bucket
, sizeof(struct buffer_head
*), GFP_NOFS
);
3209 ret
= ocfs2_read_xattr_bucket(inode
, blk
, s_bhs
, 0);
3215 ret
= ocfs2_journal_access(handle
, inode
, s_bhs
[0],
3216 OCFS2_JOURNAL_ACCESS_WRITE
);
3222 t_bhs
= kcalloc(blk_per_bucket
, sizeof(struct buffer_head
*), GFP_NOFS
);
3228 ret
= ocfs2_read_xattr_bucket(inode
, new_blk
, t_bhs
, new_bucket_head
);
3234 for (i
= 0; i
< blk_per_bucket
; i
++) {
3235 ret
= ocfs2_journal_access(handle
, inode
, t_bhs
[i
],
3236 OCFS2_JOURNAL_ACCESS_CREATE
);
3243 /* copy the whole bucket to the new first. */
3244 for (i
= 0; i
< blk_per_bucket
; i
++)
3245 memcpy(t_bhs
[i
]->b_data
, s_bhs
[i
]->b_data
, blocksize
);
3247 /* update the new bucket. */
3248 xh
= (struct ocfs2_xattr_header
*)t_bhs
[0]->b_data
;
3249 count
= le16_to_cpu(xh
->xh_count
);
3253 * Calculate the total name/value len and xh_free_start for
3254 * the old bucket first.
3256 name_offset
= OCFS2_XATTR_BUCKET_SIZE
;
3258 for (i
= 0; i
< start
; i
++) {
3259 xe
= &xh
->xh_entries
[i
];
3260 xe_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3261 if (ocfs2_xattr_is_local(xe
))
3263 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3265 xe_len
+= OCFS2_XATTR_ROOT_SIZE
;
3266 name_value_len
+= xe_len
;
3267 if (le16_to_cpu(xe
->xe_name_offset
) < name_offset
)
3268 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3272 * Now begin the modification to the new bucket.
3274 * In the new bucket, We just move the xattr entry to the beginning
3275 * and don't touch the name/value. So there will be some holes in the
3276 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3279 xe
= &xh
->xh_entries
[start
];
3280 len
= sizeof(struct ocfs2_xattr_entry
) * (count
- start
);
3281 mlog(0, "mv xattr entry len %d from %d to %d\n", len
,
3282 (int)((char *)xe
- (char *)xh
),
3283 (int)((char *)xh
->xh_entries
- (char *)xh
));
3284 memmove((char *)xh
->xh_entries
, (char *)xe
, len
);
3285 xe
= &xh
->xh_entries
[count
- start
];
3286 len
= sizeof(struct ocfs2_xattr_entry
) * start
;
3287 memset((char *)xe
, 0, len
);
3289 le16_add_cpu(&xh
->xh_count
, -start
);
3290 le16_add_cpu(&xh
->xh_name_value_len
, -name_value_len
);
3292 /* Calculate xh_free_start for the new bucket. */
3293 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
3294 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
3295 xe
= &xh
->xh_entries
[i
];
3296 xe_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3297 if (ocfs2_xattr_is_local(xe
))
3299 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3301 xe_len
+= OCFS2_XATTR_ROOT_SIZE
;
3302 if (le16_to_cpu(xe
->xe_name_offset
) <
3303 le16_to_cpu(xh
->xh_free_start
))
3304 xh
->xh_free_start
= xe
->xe_name_offset
;
3307 /* set xh->xh_num_buckets for the new xh. */
3308 if (new_bucket_head
)
3309 xh
->xh_num_buckets
= cpu_to_le16(1);
3311 xh
->xh_num_buckets
= 0;
3313 for (i
= 0; i
< blk_per_bucket
; i
++) {
3314 ocfs2_journal_dirty(handle
, t_bhs
[i
]);
3319 /* store the first_hash of the new bucket. */
3321 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3324 * Now only update the 1st block of the old bucket.
3325 * Please note that the entry has been sorted already above.
3327 xh
= (struct ocfs2_xattr_header
*)s_bhs
[0]->b_data
;
3328 memset(&xh
->xh_entries
[start
], 0,
3329 sizeof(struct ocfs2_xattr_entry
) * (count
- start
));
3330 xh
->xh_count
= cpu_to_le16(start
);
3331 xh
->xh_free_start
= cpu_to_le16(name_offset
);
3332 xh
->xh_name_value_len
= cpu_to_le16(name_value_len
);
3334 ocfs2_journal_dirty(handle
, s_bhs
[0]);
3340 for (i
= 0; i
< blk_per_bucket
; i
++)
3346 for (i
= 0; i
< blk_per_bucket
; i
++)
3355 * Copy xattr from one bucket to another bucket.
3357 * The caller must make sure that the journal transaction
3358 * has enough space for journaling.
3360 static int ocfs2_cp_xattr_bucket(struct inode
*inode
,
3367 int blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3368 int blocksize
= inode
->i_sb
->s_blocksize
;
3369 struct buffer_head
**s_bhs
, **t_bhs
= NULL
;
3371 BUG_ON(s_blkno
== t_blkno
);
3373 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3374 s_blkno
, t_blkno
, t_is_new
);
3376 s_bhs
= kzalloc(sizeof(struct buffer_head
*) * blk_per_bucket
,
3381 ret
= ocfs2_read_xattr_bucket(inode
, s_blkno
, s_bhs
, 0);
3385 t_bhs
= kzalloc(sizeof(struct buffer_head
*) * blk_per_bucket
,
3392 ret
= ocfs2_read_xattr_bucket(inode
, t_blkno
, t_bhs
, t_is_new
);
3396 for (i
= 0; i
< blk_per_bucket
; i
++) {
3397 ret
= ocfs2_journal_access(handle
, inode
, t_bhs
[i
],
3398 OCFS2_JOURNAL_ACCESS_WRITE
);
3403 for (i
= 0; i
< blk_per_bucket
; i
++) {
3404 memcpy(t_bhs
[i
]->b_data
, s_bhs
[i
]->b_data
, blocksize
);
3405 ocfs2_journal_dirty(handle
, t_bhs
[i
]);
3410 for (i
= 0; i
< blk_per_bucket
; i
++)
3416 for (i
= 0; i
< blk_per_bucket
; i
++)
3425 * Copy one xattr cluster from src_blk to to_blk.
3426 * The to_blk will become the first bucket header of the cluster, so its
3427 * xh_num_buckets will be initialized as the bucket num in the cluster.
3429 static int ocfs2_cp_xattr_cluster(struct inode
*inode
,
3431 struct buffer_head
*first_bh
,
3436 int i
, ret
, credits
;
3437 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3438 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3439 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
3440 struct buffer_head
*bh
= NULL
;
3441 struct ocfs2_xattr_header
*xh
;
3442 u64 to_blk_start
= to_blk
;
3444 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk
, to_blk
);
3447 * We need to update the new cluster and 1 more for the update of
3448 * the 1st bucket of the previous extent rec.
3451 ret
= ocfs2_extend_trans(handle
, credits
);
3457 ret
= ocfs2_journal_access(handle
, inode
, first_bh
,
3458 OCFS2_JOURNAL_ACCESS_WRITE
);
3464 for (i
= 0; i
< num_buckets
; i
++) {
3465 ret
= ocfs2_cp_xattr_bucket(inode
, handle
,
3466 src_blk
, to_blk
, 1);
3472 src_blk
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3473 to_blk
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3476 /* update the old bucket header. */
3477 xh
= (struct ocfs2_xattr_header
*)first_bh
->b_data
;
3478 le16_add_cpu(&xh
->xh_num_buckets
, -num_buckets
);
3480 ocfs2_journal_dirty(handle
, first_bh
);
3482 /* update the new bucket header. */
3483 ret
= ocfs2_read_block(inode
, to_blk_start
, &bh
);
3489 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3490 OCFS2_JOURNAL_ACCESS_WRITE
);
3496 xh
= (struct ocfs2_xattr_header
*)bh
->b_data
;
3497 xh
->xh_num_buckets
= cpu_to_le16(num_buckets
);
3499 ocfs2_journal_dirty(handle
, bh
);
3502 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3509 * Move half of the xattrs in this cluster to the new cluster.
3510 * This function should only be called when bucket size == cluster size.
3511 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3513 static int ocfs2_half_xattr_cluster(struct inode
*inode
,
3519 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3520 int ret
, credits
= 2 * blk_per_bucket
;
3522 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
< OCFS2_SB(inode
->i_sb
)->s_clustersize
);
3524 ret
= ocfs2_extend_trans(handle
, credits
);
3530 /* Move half of the xattr in start_blk to the next bucket. */
3531 return ocfs2_half_xattr_bucket(inode
, handle
, prev_blk
,
3532 new_blk
, first_hash
, 1);
3536 * Move some xattrs from the old cluster to the new one since they are not
3537 * contiguous in ocfs2 xattr tree.
3539 * new_blk starts a new separate cluster, and we will move some xattrs from
3540 * prev_blk to it. v_start will be set as the first name hash value in this
3541 * new cluster so that it can be used as e_cpos during tree insertion and
3542 * don't collide with our original b-tree operations. first_bh and header_bh
3543 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3544 * to extend the insert bucket.
3546 * The problem is how much xattr should we move to the new one and when should
3547 * we update first_bh and header_bh?
3548 * 1. If cluster size > bucket size, that means the previous cluster has more
3549 * than 1 bucket, so just move half nums of bucket into the new cluster and
3550 * update the first_bh and header_bh if the insert bucket has been moved
3551 * to the new cluster.
3552 * 2. If cluster_size == bucket_size:
3553 * a) If the previous extent rec has more than one cluster and the insert
3554 * place isn't in the last cluster, copy the entire last cluster to the
3555 * new one. This time, we don't need to upate the first_bh and header_bh
3556 * since they will not be moved into the new cluster.
3557 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3558 * the new one. And we set the extend flag to zero if the insert place is
3559 * moved into the new allocated cluster since no extend is needed.
3561 static int ocfs2_adjust_xattr_cross_cluster(struct inode
*inode
,
3563 struct buffer_head
**first_bh
,
3564 struct buffer_head
**header_bh
,
3572 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3574 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3575 prev_blk
, prev_clusters
, new_blk
);
3577 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
)) > 1)
3578 ret
= ocfs2_mv_xattr_bucket_cross_cluster(inode
,
3587 u64 last_blk
= prev_blk
+ bpc
* (prev_clusters
- 1);
3589 if (prev_clusters
> 1 && (*header_bh
)->b_blocknr
!= last_blk
)
3590 ret
= ocfs2_cp_xattr_cluster(inode
, handle
, *first_bh
,
3594 ret
= ocfs2_half_xattr_cluster(inode
, handle
,
3598 if ((*header_bh
)->b_blocknr
== last_blk
&& extend
)
3607 * Add a new cluster for xattr storage.
3609 * If the new cluster is contiguous with the previous one, it will be
3610 * appended to the same extent record, and num_clusters will be updated.
3611 * If not, we will insert a new extent for it and move some xattrs in
3612 * the last cluster into the new allocated one.
3613 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3614 * lose the benefits of hashing because we'll have to search large leaves.
3615 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3618 * first_bh is the first block of the previous extent rec and header_bh
3619 * indicates the bucket we will insert the new xattrs. They will be updated
3620 * when the header_bh is moved into the new cluster.
3622 static int ocfs2_add_new_xattr_cluster(struct inode
*inode
,
3623 struct buffer_head
*root_bh
,
3624 struct buffer_head
**first_bh
,
3625 struct buffer_head
**header_bh
,
3632 u16 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3633 u32 prev_clusters
= *num_clusters
;
3634 u32 clusters_to_add
= 1, bit_off
, num_bits
, v_start
= 0;
3636 handle_t
*handle
= NULL
;
3637 struct ocfs2_alloc_context
*data_ac
= NULL
;
3638 struct ocfs2_alloc_context
*meta_ac
= NULL
;
3639 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3640 struct ocfs2_extent_tree et
;
3642 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3643 "previous xattr blkno = %llu\n",
3644 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
3645 prev_cpos
, prev_blkno
);
3647 ocfs2_init_xattr_tree_extent_tree(&et
, inode
, root_bh
);
3649 ret
= ocfs2_lock_allocators(inode
, &et
, clusters_to_add
, 0,
3650 &data_ac
, &meta_ac
);
3656 credits
= ocfs2_calc_extend_credits(osb
->sb
, et
.et_root_el
,
3658 handle
= ocfs2_start_trans(osb
, credits
);
3659 if (IS_ERR(handle
)) {
3660 ret
= PTR_ERR(handle
);
3666 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
3667 OCFS2_JOURNAL_ACCESS_WRITE
);
3673 ret
= __ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
3674 clusters_to_add
, &bit_off
, &num_bits
);
3681 BUG_ON(num_bits
> clusters_to_add
);
3683 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
3684 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3685 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3687 if (prev_blkno
+ prev_clusters
* bpc
== block
&&
3688 (prev_clusters
+ num_bits
) << osb
->s_clustersize_bits
<=
3689 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
) {
3691 * If this cluster is contiguous with the old one and
3692 * adding this new cluster, we don't surpass the limit of
3693 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3694 * initialized and used like other buckets in the previous
3696 * So add it as a contiguous one. The caller will handle
3699 v_start
= prev_cpos
+ prev_clusters
;
3700 *num_clusters
= prev_clusters
+ num_bits
;
3701 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3704 ret
= ocfs2_adjust_xattr_cross_cluster(inode
,
3719 if (handle
->h_buffer_credits
< credits
) {
3721 * The journal has been restarted before, and don't
3722 * have enough space for the insertion, so extend it
3725 ret
= ocfs2_extend_trans(handle
, credits
);
3731 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3732 num_bits
, block
, v_start
);
3733 ret
= ocfs2_insert_extent(osb
, handle
, inode
, &et
, v_start
, block
,
3734 num_bits
, 0, meta_ac
);
3740 ret
= ocfs2_journal_dirty(handle
, root_bh
);
3748 ocfs2_commit_trans(osb
, handle
);
3750 ocfs2_free_alloc_context(data_ac
);
3752 ocfs2_free_alloc_context(meta_ac
);
3758 * Extend a new xattr bucket and move xattrs to the end one by one until
3759 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3761 static int ocfs2_extend_xattr_bucket(struct inode
*inode
,
3762 struct buffer_head
*first_bh
,
3763 struct buffer_head
*start_bh
,
3767 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3768 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3769 u64 start_blk
= start_bh
->b_blocknr
, end_blk
;
3770 u32 num_buckets
= num_clusters
* ocfs2_xattr_buckets_per_cluster(osb
);
3772 struct ocfs2_xattr_header
*first_xh
=
3773 (struct ocfs2_xattr_header
*)first_bh
->b_data
;
3774 u16 bucket
= le16_to_cpu(first_xh
->xh_num_buckets
);
3776 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3777 "from %llu, len = %u\n", start_blk
,
3778 (unsigned long long)first_bh
->b_blocknr
, num_clusters
);
3780 BUG_ON(bucket
>= num_buckets
);
3782 end_blk
= first_bh
->b_blocknr
+ (bucket
- 1) * blk_per_bucket
;
3785 * We will touch all the buckets after the start_bh(include it).
3786 * Add one more bucket and modify the first_bh.
3788 credits
= end_blk
- start_blk
+ 2 * blk_per_bucket
+ 1;
3789 handle
= ocfs2_start_trans(osb
, credits
);
3790 if (IS_ERR(handle
)) {
3791 ret
= PTR_ERR(handle
);
3797 ret
= ocfs2_journal_access(handle
, inode
, first_bh
,
3798 OCFS2_JOURNAL_ACCESS_WRITE
);
3804 while (end_blk
!= start_blk
) {
3805 ret
= ocfs2_cp_xattr_bucket(inode
, handle
, end_blk
,
3806 end_blk
+ blk_per_bucket
, 0);
3809 end_blk
-= blk_per_bucket
;
3812 /* Move half of the xattr in start_blk to the next bucket. */
3813 ret
= ocfs2_half_xattr_bucket(inode
, handle
, start_blk
,
3814 start_blk
+ blk_per_bucket
, NULL
, 0);
3816 le16_add_cpu(&first_xh
->xh_num_buckets
, 1);
3817 ocfs2_journal_dirty(handle
, first_bh
);
3820 ocfs2_commit_trans(osb
, handle
);
3826 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3827 * xb_bh is the ocfs2_xattr_block.
3828 * We will move all the buckets starting from header_bh to the next place. As
3829 * for this one, half num of its xattrs will be moved to the next one.
3831 * We will allocate a new cluster if current cluster is full and adjust
3832 * header_bh and first_bh if the insert place is moved to the new cluster.
3834 static int ocfs2_add_new_xattr_bucket(struct inode
*inode
,
3835 struct buffer_head
*xb_bh
,
3836 struct buffer_head
*header_bh
)
3838 struct ocfs2_xattr_header
*first_xh
= NULL
;
3839 struct buffer_head
*first_bh
= NULL
;
3840 struct ocfs2_xattr_block
*xb
=
3841 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
3842 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
3843 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
3844 struct ocfs2_xattr_header
*xh
=
3845 (struct ocfs2_xattr_header
*)header_bh
->b_data
;
3846 u32 name_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3847 struct super_block
*sb
= inode
->i_sb
;
3848 struct ocfs2_super
*osb
= OCFS2_SB(sb
);
3849 int ret
, num_buckets
, extend
= 1;
3851 u32 e_cpos
, num_clusters
;
3853 mlog(0, "Add new xattr bucket starting form %llu\n",
3854 (unsigned long long)header_bh
->b_blocknr
);
3857 * Add refrence for header_bh here because it may be
3858 * changed in ocfs2_add_new_xattr_cluster and we need
3859 * to free it in the end.
3863 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &e_cpos
,
3870 ret
= ocfs2_read_block(inode
, p_blkno
, &first_bh
);
3876 num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
) * num_clusters
;
3877 first_xh
= (struct ocfs2_xattr_header
*)first_bh
->b_data
;
3879 if (num_buckets
== le16_to_cpu(first_xh
->xh_num_buckets
)) {
3880 ret
= ocfs2_add_new_xattr_cluster(inode
,
3895 ret
= ocfs2_extend_xattr_bucket(inode
,
3907 static inline char *ocfs2_xattr_bucket_get_val(struct inode
*inode
,
3908 struct ocfs2_xattr_bucket
*bucket
,
3911 int block_off
= offs
>> inode
->i_sb
->s_blocksize_bits
;
3913 offs
= offs
% inode
->i_sb
->s_blocksize
;
3914 return bucket
->bhs
[block_off
]->b_data
+ offs
;
3918 * Handle the normal xattr set, including replace, delete and new.
3920 * Note: "local" indicates the real data's locality. So we can't
3921 * just its bucket locality by its length.
3923 static void ocfs2_xattr_set_entry_normal(struct inode
*inode
,
3924 struct ocfs2_xattr_info
*xi
,
3925 struct ocfs2_xattr_search
*xs
,
3929 struct ocfs2_xattr_entry
*last
, *xe
;
3930 int name_len
= strlen(xi
->name
);
3931 struct ocfs2_xattr_header
*xh
= xs
->header
;
3932 u16 count
= le16_to_cpu(xh
->xh_count
), start
;
3933 size_t blocksize
= inode
->i_sb
->s_blocksize
;
3935 size_t offs
, size
, new_size
;
3937 last
= &xh
->xh_entries
[count
];
3938 if (!xs
->not_found
) {
3940 offs
= le16_to_cpu(xe
->xe_name_offset
);
3941 if (ocfs2_xattr_is_local(xe
))
3942 size
= OCFS2_XATTR_SIZE(name_len
) +
3943 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3945 size
= OCFS2_XATTR_SIZE(name_len
) +
3946 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE
);
3949 * If the new value will be stored outside, xi->value has been
3950 * initalized as an empty ocfs2_xattr_value_root, and the same
3951 * goes with xi->value_len, so we can set new_size safely here.
3952 * See ocfs2_xattr_set_in_bucket.
3954 new_size
= OCFS2_XATTR_SIZE(name_len
) +
3955 OCFS2_XATTR_SIZE(xi
->value_len
);
3957 le16_add_cpu(&xh
->xh_name_value_len
, -size
);
3959 if (new_size
> size
)
3960 goto set_new_name_value
;
3962 /* Now replace the old value with new one. */
3964 xe
->xe_value_size
= cpu_to_le64(xi
->value_len
);
3966 xe
->xe_value_size
= 0;
3968 val
= ocfs2_xattr_bucket_get_val(inode
,
3970 memset(val
+ OCFS2_XATTR_SIZE(name_len
), 0,
3971 size
- OCFS2_XATTR_SIZE(name_len
));
3972 if (OCFS2_XATTR_SIZE(xi
->value_len
) > 0)
3973 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
3974 xi
->value
, xi
->value_len
);
3976 le16_add_cpu(&xh
->xh_name_value_len
, new_size
);
3977 ocfs2_xattr_set_local(xe
, local
);
3981 * Remove the old entry if there is more than one.
3982 * We don't remove the last entry so that we can
3983 * use it to indicate the hash value of the empty
3987 le16_add_cpu(&xh
->xh_count
, -1);
3990 (void *)last
- (void *)xe
);
3992 sizeof(struct ocfs2_xattr_entry
));
3995 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
4000 /* find a new entry for insert. */
4001 int low
= 0, high
= count
- 1, tmp
;
4002 struct ocfs2_xattr_entry
*tmp_xe
;
4004 while (low
<= high
&& count
) {
4005 tmp
= (low
+ high
) / 2;
4006 tmp_xe
= &xh
->xh_entries
[tmp
];
4008 if (name_hash
> le32_to_cpu(tmp_xe
->xe_name_hash
))
4010 else if (name_hash
<
4011 le32_to_cpu(tmp_xe
->xe_name_hash
))
4019 xe
= &xh
->xh_entries
[low
];
4021 memmove(xe
+ 1, xe
, (void *)last
- (void *)xe
);
4023 le16_add_cpu(&xh
->xh_count
, 1);
4024 memset(xe
, 0, sizeof(struct ocfs2_xattr_entry
));
4025 xe
->xe_name_hash
= cpu_to_le32(name_hash
);
4026 xe
->xe_name_len
= name_len
;
4027 ocfs2_xattr_set_type(xe
, xi
->name_index
);
4031 /* Insert the new name+value. */
4032 size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_SIZE(xi
->value_len
);
4035 * We must make sure that the name/value pair
4036 * exists in the same block.
4038 offs
= le16_to_cpu(xh
->xh_free_start
);
4039 start
= offs
- size
;
4041 if (start
>> inode
->i_sb
->s_blocksize_bits
!=
4042 (offs
- 1) >> inode
->i_sb
->s_blocksize_bits
) {
4043 offs
= offs
- offs
% blocksize
;
4044 xh
->xh_free_start
= cpu_to_le16(offs
);
4047 val
= ocfs2_xattr_bucket_get_val(inode
,
4048 &xs
->bucket
, offs
- size
);
4049 xe
->xe_name_offset
= cpu_to_le16(offs
- size
);
4051 memset(val
, 0, size
);
4052 memcpy(val
, xi
->name
, name_len
);
4053 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
), xi
->value
, xi
->value_len
);
4055 xe
->xe_value_size
= cpu_to_le64(xi
->value_len
);
4056 ocfs2_xattr_set_local(xe
, local
);
4058 le16_add_cpu(&xh
->xh_free_start
, -size
);
4059 le16_add_cpu(&xh
->xh_name_value_len
, size
);
4064 static int ocfs2_xattr_bucket_handle_journal(struct inode
*inode
,
4066 struct ocfs2_xattr_search
*xs
,
4067 struct buffer_head
**bhs
,
4070 int ret
= 0, off
, block_off
;
4071 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4074 * First calculate all the blocks we should journal_access
4075 * and journal_dirty. The first block should always be touched.
4077 ret
= ocfs2_journal_dirty(handle
, bhs
[0]);
4081 /* calc the data. */
4082 off
= le16_to_cpu(xe
->xe_name_offset
);
4083 block_off
= off
>> inode
->i_sb
->s_blocksize_bits
;
4084 ret
= ocfs2_journal_dirty(handle
, bhs
[block_off
]);
4092 * Set the xattr entry in the specified bucket.
4093 * The bucket is indicated by xs->bucket and it should have the enough
4094 * space for the xattr insertion.
4096 static int ocfs2_xattr_set_entry_in_bucket(struct inode
*inode
,
4097 struct ocfs2_xattr_info
*xi
,
4098 struct ocfs2_xattr_search
*xs
,
4103 handle_t
*handle
= NULL
;
4104 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4105 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4107 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4108 (unsigned long)xi
->value_len
, xi
->name_index
,
4109 (unsigned long long)xs
->bucket
.bhs
[0]->b_blocknr
);
4111 if (!xs
->bucket
.bhs
[1]) {
4112 ret
= ocfs2_read_blocks(inode
,
4113 xs
->bucket
.bhs
[0]->b_blocknr
+ 1,
4114 blk_per_bucket
- 1, &xs
->bucket
.bhs
[1],
4122 handle
= ocfs2_start_trans(osb
, blk_per_bucket
);
4123 if (IS_ERR(handle
)) {
4124 ret
= PTR_ERR(handle
);
4130 for (i
= 0; i
< blk_per_bucket
; i
++) {
4131 ret
= ocfs2_journal_access(handle
, inode
, xs
->bucket
.bhs
[i
],
4132 OCFS2_JOURNAL_ACCESS_WRITE
);
4139 ocfs2_xattr_set_entry_normal(inode
, xi
, xs
, name_hash
, local
);
4141 /*Only dirty the blocks we have touched in set xattr. */
4142 ret
= ocfs2_xattr_bucket_handle_journal(inode
, handle
, xs
,
4143 xs
->bucket
.bhs
, blk_per_bucket
);
4147 ocfs2_commit_trans(osb
, handle
);
4152 static int ocfs2_xattr_value_update_size(struct inode
*inode
,
4153 struct buffer_head
*xe_bh
,
4154 struct ocfs2_xattr_entry
*xe
,
4158 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4159 handle_t
*handle
= NULL
;
4161 handle
= ocfs2_start_trans(osb
, 1);
4162 if (handle
== NULL
) {
4168 ret
= ocfs2_journal_access(handle
, inode
, xe_bh
,
4169 OCFS2_JOURNAL_ACCESS_WRITE
);
4175 xe
->xe_value_size
= cpu_to_le64(new_size
);
4177 ret
= ocfs2_journal_dirty(handle
, xe_bh
);
4182 ocfs2_commit_trans(osb
, handle
);
4188 * Truncate the specified xe_off entry in xattr bucket.
4189 * bucket is indicated by header_bh and len is the new length.
4190 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4192 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4194 static int ocfs2_xattr_bucket_value_truncate(struct inode
*inode
,
4195 struct buffer_head
*header_bh
,
4201 struct buffer_head
*value_bh
= NULL
;
4202 struct ocfs2_xattr_value_root
*xv
;
4203 struct ocfs2_xattr_entry
*xe
;
4204 struct ocfs2_xattr_header
*xh
=
4205 (struct ocfs2_xattr_header
*)header_bh
->b_data
;
4206 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4208 xe
= &xh
->xh_entries
[xe_off
];
4210 BUG_ON(!xe
|| ocfs2_xattr_is_local(xe
));
4212 offset
= le16_to_cpu(xe
->xe_name_offset
) +
4213 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
4215 value_blk
= offset
/ blocksize
;
4217 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4218 BUG_ON(value_blk
!= (offset
+ OCFS2_XATTR_ROOT_SIZE
- 1) / blocksize
);
4219 value_blk
+= header_bh
->b_blocknr
;
4221 ret
= ocfs2_read_block(inode
, value_blk
, &value_bh
);
4227 xv
= (struct ocfs2_xattr_value_root
*)
4228 (value_bh
->b_data
+ offset
% blocksize
);
4230 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4231 xe_off
, (unsigned long long)header_bh
->b_blocknr
, len
);
4232 ret
= ocfs2_xattr_value_truncate(inode
, value_bh
, xv
, len
);
4238 ret
= ocfs2_xattr_value_update_size(inode
, header_bh
, xe
, len
);
4249 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode
*inode
,
4250 struct ocfs2_xattr_search
*xs
,
4254 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4255 struct ocfs2_xattr_header
*xh
= (struct ocfs2_xattr_header
*)xs
->base
;
4257 BUG_ON(!xs
->bucket
.bhs
[0] || !xe
|| ocfs2_xattr_is_local(xe
));
4259 offset
= xe
- xh
->xh_entries
;
4260 ret
= ocfs2_xattr_bucket_value_truncate(inode
, xs
->bucket
.bhs
[0],
4268 static int ocfs2_xattr_bucket_set_value_outside(struct inode
*inode
,
4269 struct ocfs2_xattr_search
*xs
,
4274 struct ocfs2_xattr_value_root
*xv
;
4275 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4277 BUG_ON(!xs
->base
|| !xe
|| ocfs2_xattr_is_local(xe
));
4279 offset
= le16_to_cpu(xe
->xe_name_offset
) +
4280 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
4282 xv
= (struct ocfs2_xattr_value_root
*)(xs
->base
+ offset
);
4284 return __ocfs2_xattr_set_value_outside(inode
, xv
, val
, value_len
);
4287 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
4288 struct buffer_head
*root_bh
,
4294 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4295 struct inode
*tl_inode
= osb
->osb_tl_inode
;
4297 struct ocfs2_xattr_block
*xb
=
4298 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
4299 struct ocfs2_alloc_context
*meta_ac
= NULL
;
4300 struct ocfs2_cached_dealloc_ctxt dealloc
;
4301 struct ocfs2_extent_tree et
;
4303 ocfs2_init_xattr_tree_extent_tree(&et
, inode
, root_bh
);
4305 ocfs2_init_dealloc_ctxt(&dealloc
);
4307 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4308 cpos
, len
, (unsigned long long)blkno
);
4310 ocfs2_remove_xattr_clusters_from_cache(inode
, blkno
, len
);
4312 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
4318 mutex_lock(&tl_inode
->i_mutex
);
4320 if (ocfs2_truncate_log_needs_flush(osb
)) {
4321 ret
= __ocfs2_flush_truncate_log(osb
);
4328 handle
= ocfs2_start_trans(osb
, OCFS2_REMOVE_EXTENT_CREDITS
);
4329 if (handle
== NULL
) {
4335 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
4336 OCFS2_JOURNAL_ACCESS_WRITE
);
4342 ret
= ocfs2_remove_extent(inode
, &et
, cpos
, len
, handle
, meta_ac
,
4349 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, -len
);
4351 ret
= ocfs2_journal_dirty(handle
, root_bh
);
4357 ret
= ocfs2_truncate_log_append(osb
, handle
, blkno
, len
);
4362 ocfs2_commit_trans(osb
, handle
);
4364 ocfs2_schedule_truncate_log_flush(osb
, 1);
4366 mutex_unlock(&tl_inode
->i_mutex
);
4369 ocfs2_free_alloc_context(meta_ac
);
4371 ocfs2_run_deallocs(osb
, &dealloc
);
4376 static void ocfs2_xattr_bucket_remove_xs(struct inode
*inode
,
4377 struct ocfs2_xattr_search
*xs
)
4379 handle_t
*handle
= NULL
;
4380 struct ocfs2_xattr_header
*xh
= xs
->bucket
.xh
;
4381 struct ocfs2_xattr_entry
*last
= &xh
->xh_entries
[
4382 le16_to_cpu(xh
->xh_count
) - 1];
4385 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)), 1);
4386 if (IS_ERR(handle
)) {
4387 ret
= PTR_ERR(handle
);
4392 ret
= ocfs2_journal_access(handle
, inode
, xs
->bucket
.bhs
[0],
4393 OCFS2_JOURNAL_ACCESS_WRITE
);
4399 /* Remove the old entry. */
4400 memmove(xs
->here
, xs
->here
+ 1,
4401 (void *)last
- (void *)xs
->here
);
4402 memset(last
, 0, sizeof(struct ocfs2_xattr_entry
));
4403 le16_add_cpu(&xh
->xh_count
, -1);
4405 ret
= ocfs2_journal_dirty(handle
, xs
->bucket
.bhs
[0]);
4409 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
4413 * Set the xattr name/value in the bucket specified in xs.
4415 * As the new value in xi may be stored in the bucket or in an outside cluster,
4416 * we divide the whole process into 3 steps:
4417 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4418 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4419 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4420 * 4. If the clusters for the new outside value can't be allocated, we need
4421 * to free the xattr we allocated in set.
4423 static int ocfs2_xattr_set_in_bucket(struct inode
*inode
,
4424 struct ocfs2_xattr_info
*xi
,
4425 struct ocfs2_xattr_search
*xs
)
4429 char *val
= (char *)xi
->value
;
4430 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4431 u32 name_hash
= ocfs2_xattr_name_hash(inode
, xi
->name
,
4434 if (!xs
->not_found
&& !ocfs2_xattr_is_local(xe
)) {
4436 * We need to truncate the xattr storage first.
4438 * If both the old and new value are stored to
4439 * outside block, we only need to truncate
4440 * the storage and then set the value outside.
4442 * If the new value should be stored within block,
4443 * we should free all the outside block first and
4444 * the modification to the xattr block will be done
4445 * by following steps.
4447 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
4448 value_len
= xi
->value_len
;
4452 ret
= ocfs2_xattr_bucket_value_truncate_xs(inode
, xs
,
4458 goto set_value_outside
;
4461 value_len
= xi
->value_len
;
4462 /* So we have to handle the inside block change now. */
4463 if (value_len
> OCFS2_XATTR_INLINE_SIZE
) {
4465 * If the new value will be stored outside of block,
4466 * initalize a new empty value root and insert it first.
4469 xi
->value
= &def_xv
;
4470 xi
->value_len
= OCFS2_XATTR_ROOT_SIZE
;
4473 ret
= ocfs2_xattr_set_entry_in_bucket(inode
, xi
, xs
, name_hash
, local
);
4479 if (value_len
<= OCFS2_XATTR_INLINE_SIZE
)
4482 /* allocate the space now for the outside block storage. */
4483 ret
= ocfs2_xattr_bucket_value_truncate_xs(inode
, xs
,
4488 if (xs
->not_found
) {
4490 * We can't allocate enough clusters for outside
4491 * storage and we have allocated xattr already,
4492 * so need to remove it.
4494 ocfs2_xattr_bucket_remove_xs(inode
, xs
);
4500 ret
= ocfs2_xattr_bucket_set_value_outside(inode
, xs
, val
, value_len
);
4505 /* check whether the xattr bucket is filled up with the same hash value. */
4506 static int ocfs2_check_xattr_bucket_collision(struct inode
*inode
,
4507 struct ocfs2_xattr_bucket
*bucket
)
4509 struct ocfs2_xattr_header
*xh
= bucket
->xh
;
4511 if (xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1].xe_name_hash
==
4512 xh
->xh_entries
[0].xe_name_hash
) {
4513 mlog(ML_ERROR
, "Too much hash collision in xattr bucket %llu, "
4515 (unsigned long long)bucket
->bhs
[0]->b_blocknr
,
4516 le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
));
4523 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
4524 struct ocfs2_xattr_info
*xi
,
4525 struct ocfs2_xattr_search
*xs
)
4527 struct ocfs2_xattr_header
*xh
;
4528 struct ocfs2_xattr_entry
*xe
;
4529 u16 count
, header_size
, xh_free_start
;
4530 int i
, free
, max_free
, need
, old
;
4531 size_t value_size
= 0, name_len
= strlen(xi
->name
);
4532 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4533 int ret
, allocation
= 0;
4534 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4536 mlog_entry("Set xattr %s in xattr index block\n", xi
->name
);
4540 count
= le16_to_cpu(xh
->xh_count
);
4541 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4542 header_size
= sizeof(struct ocfs2_xattr_header
) +
4543 count
* sizeof(struct ocfs2_xattr_entry
);
4544 max_free
= OCFS2_XATTR_BUCKET_SIZE
-
4545 le16_to_cpu(xh
->xh_name_value_len
) - header_size
;
4547 mlog_bug_on_msg(header_size
> blocksize
, "bucket %llu has header size "
4548 "of %u which exceed block size\n",
4549 (unsigned long long)xs
->bucket
.bhs
[0]->b_blocknr
,
4552 if (xi
->value
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
4553 value_size
= OCFS2_XATTR_ROOT_SIZE
;
4555 value_size
= OCFS2_XATTR_SIZE(xi
->value_len
);
4558 need
= sizeof(struct ocfs2_xattr_entry
) +
4559 OCFS2_XATTR_SIZE(name_len
) + value_size
;
4561 need
= value_size
+ OCFS2_XATTR_SIZE(name_len
);
4564 * We only replace the old value if the new length is smaller
4565 * than the old one. Otherwise we will allocate new space in the
4566 * bucket to store it.
4569 if (ocfs2_xattr_is_local(xe
))
4570 old
= OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
4572 old
= OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE
);
4574 if (old
>= value_size
)
4578 free
= xh_free_start
- header_size
;
4580 * We need to make sure the new name/value pair
4581 * can exist in the same block.
4583 if (xh_free_start
% blocksize
< need
)
4584 free
-= xh_free_start
% blocksize
;
4586 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4587 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4588 " %u\n", xs
->not_found
,
4589 (unsigned long long)xs
->bucket
.bhs
[0]->b_blocknr
,
4590 free
, need
, max_free
, le16_to_cpu(xh
->xh_free_start
),
4591 le16_to_cpu(xh
->xh_name_value_len
));
4593 if (free
< need
|| count
== ocfs2_xattr_max_xe_in_bucket(inode
->i_sb
)) {
4594 if (need
<= max_free
&&
4595 count
< ocfs2_xattr_max_xe_in_bucket(inode
->i_sb
)) {
4597 * We can create the space by defragment. Since only the
4598 * name/value will be moved, the xe shouldn't be changed
4601 ret
= ocfs2_defrag_xattr_bucket(inode
, &xs
->bucket
);
4607 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4608 free
= xh_free_start
- header_size
;
4609 if (xh_free_start
% blocksize
< need
)
4610 free
-= xh_free_start
% blocksize
;
4615 mlog(0, "Can't get enough space for xattr insert by "
4616 "defragment. Need %u bytes, but we have %d, so "
4617 "allocate new bucket for it.\n", need
, free
);
4621 * We have to add new buckets or clusters and one
4622 * allocation should leave us enough space for insert.
4627 * We do not allow for overlapping ranges between buckets. And
4628 * the maximum number of collisions we will allow for then is
4629 * one bucket's worth, so check it here whether we need to
4630 * add a new bucket for the insert.
4632 ret
= ocfs2_check_xattr_bucket_collision(inode
, &xs
->bucket
);
4638 ret
= ocfs2_add_new_xattr_bucket(inode
,
4646 for (i
= 0; i
< blk_per_bucket
; i
++)
4647 brelse(xs
->bucket
.bhs
[i
]);
4649 memset(&xs
->bucket
, 0, sizeof(xs
->bucket
));
4651 ret
= ocfs2_xattr_index_block_find(inode
, xs
->xattr_bh
,
4654 if (ret
&& ret
!= -ENODATA
)
4656 xs
->not_found
= ret
;
4662 ret
= ocfs2_xattr_set_in_bucket(inode
, xi
, xs
);
4668 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
4669 struct ocfs2_xattr_bucket
*bucket
,
4673 struct ocfs2_xattr_header
*xh
= bucket
->xh
;
4675 struct ocfs2_xattr_entry
*xe
;
4677 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
4678 xe
= &xh
->xh_entries
[i
];
4679 if (ocfs2_xattr_is_local(xe
))
4682 ret
= ocfs2_xattr_bucket_value_truncate(inode
,
4694 static int ocfs2_delete_xattr_index_block(struct inode
*inode
,
4695 struct buffer_head
*xb_bh
)
4697 struct ocfs2_xattr_block
*xb
=
4698 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4699 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
4701 u32 name_hash
= UINT_MAX
, e_cpos
, num_clusters
;
4704 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
4707 while (name_hash
> 0) {
4708 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
4709 &e_cpos
, &num_clusters
, el
);
4715 ret
= ocfs2_iterate_xattr_buckets(inode
, p_blkno
, num_clusters
,
4716 ocfs2_delete_xattr_in_bucket
,
4723 ret
= ocfs2_rm_xattr_cluster(inode
, xb_bh
,
4724 p_blkno
, e_cpos
, num_clusters
);
4733 name_hash
= e_cpos
- 1;
4741 * 'trusted' attributes support
4744 #define XATTR_TRUSTED_PREFIX "trusted."
4746 static size_t ocfs2_xattr_trusted_list(struct inode
*inode
, char *list
,
4747 size_t list_size
, const char *name
,
4750 const size_t prefix_len
= sizeof(XATTR_TRUSTED_PREFIX
) - 1;
4751 const size_t total_len
= prefix_len
+ name_len
+ 1;
4753 if (list
&& total_len
<= list_size
) {
4754 memcpy(list
, XATTR_TRUSTED_PREFIX
, prefix_len
);
4755 memcpy(list
+ prefix_len
, name
, name_len
);
4756 list
[prefix_len
+ name_len
] = '\0';
4761 static int ocfs2_xattr_trusted_get(struct inode
*inode
, const char *name
,
4762 void *buffer
, size_t size
)
4764 if (strcmp(name
, "") == 0)
4766 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_TRUSTED
, name
,
4770 static int ocfs2_xattr_trusted_set(struct inode
*inode
, const char *name
,
4771 const void *value
, size_t size
, int flags
)
4773 if (strcmp(name
, "") == 0)
4776 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_TRUSTED
, name
, value
,
4780 struct xattr_handler ocfs2_xattr_trusted_handler
= {
4781 .prefix
= XATTR_TRUSTED_PREFIX
,
4782 .list
= ocfs2_xattr_trusted_list
,
4783 .get
= ocfs2_xattr_trusted_get
,
4784 .set
= ocfs2_xattr_trusted_set
,
4789 * 'user' attributes support
4792 #define XATTR_USER_PREFIX "user."
4794 static size_t ocfs2_xattr_user_list(struct inode
*inode
, char *list
,
4795 size_t list_size
, const char *name
,
4798 const size_t prefix_len
= sizeof(XATTR_USER_PREFIX
) - 1;
4799 const size_t total_len
= prefix_len
+ name_len
+ 1;
4800 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4802 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
4805 if (list
&& total_len
<= list_size
) {
4806 memcpy(list
, XATTR_USER_PREFIX
, prefix_len
);
4807 memcpy(list
+ prefix_len
, name
, name_len
);
4808 list
[prefix_len
+ name_len
] = '\0';
4813 static int ocfs2_xattr_user_get(struct inode
*inode
, const char *name
,
4814 void *buffer
, size_t size
)
4816 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4818 if (strcmp(name
, "") == 0)
4820 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
4822 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_USER
, name
,
4826 static int ocfs2_xattr_user_set(struct inode
*inode
, const char *name
,
4827 const void *value
, size_t size
, int flags
)
4829 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4831 if (strcmp(name
, "") == 0)
4833 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
4836 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_USER
, name
, value
,
4840 struct xattr_handler ocfs2_xattr_user_handler
= {
4841 .prefix
= XATTR_USER_PREFIX
,
4842 .list
= ocfs2_xattr_user_list
,
4843 .get
= ocfs2_xattr_user_get
,
4844 .set
= ocfs2_xattr_user_set
,