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
[OCFS2_XATTR_MAX
] = {
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
);
559 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
560 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
565 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
566 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
567 ret
= ocfs2_xattr_list_entries(inode
, header
,
568 buffer
, buffer_size
);
570 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
571 ret
= ocfs2_xattr_tree_list_index_block(inode
, xt
,
572 buffer
, buffer_size
);
580 ssize_t
ocfs2_listxattr(struct dentry
*dentry
,
584 int ret
= 0, i_ret
= 0, b_ret
= 0;
585 struct buffer_head
*di_bh
= NULL
;
586 struct ocfs2_dinode
*di
= NULL
;
587 struct ocfs2_inode_info
*oi
= OCFS2_I(dentry
->d_inode
);
589 if (!ocfs2_supports_xattr(OCFS2_SB(dentry
->d_sb
)))
592 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
595 ret
= ocfs2_inode_lock(dentry
->d_inode
, &di_bh
, 0);
601 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
603 down_read(&oi
->ip_xattr_sem
);
604 i_ret
= ocfs2_xattr_ibody_list(dentry
->d_inode
, di
, buffer
, size
);
612 b_ret
= ocfs2_xattr_block_list(dentry
->d_inode
, di
,
617 up_read(&oi
->ip_xattr_sem
);
618 ocfs2_inode_unlock(dentry
->d_inode
, 0);
622 return i_ret
+ b_ret
;
625 static int ocfs2_xattr_find_entry(int name_index
,
627 struct ocfs2_xattr_search
*xs
)
629 struct ocfs2_xattr_entry
*entry
;
636 name_len
= strlen(name
);
638 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
639 cmp
= name_index
- ocfs2_xattr_get_type(entry
);
641 cmp
= name_len
- entry
->xe_name_len
;
643 cmp
= memcmp(name
, (xs
->base
+
644 le16_to_cpu(entry
->xe_name_offset
)),
652 return cmp
? -ENODATA
: 0;
655 static int ocfs2_xattr_get_value_outside(struct inode
*inode
,
656 struct ocfs2_xattr_value_root
*xv
,
660 u32 cpos
, p_cluster
, num_clusters
, bpc
, clusters
;
663 size_t cplen
, blocksize
;
664 struct buffer_head
*bh
= NULL
;
665 struct ocfs2_extent_list
*el
;
668 clusters
= le32_to_cpu(xv
->xr_clusters
);
669 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
670 blocksize
= inode
->i_sb
->s_blocksize
;
673 while (cpos
< clusters
) {
674 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
681 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
682 /* Copy ocfs2_xattr_value */
683 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
684 ret
= ocfs2_read_block(inode
, blkno
, &bh
);
690 cplen
= len
>= blocksize
? blocksize
: len
;
691 memcpy(buffer
, bh
->b_data
, cplen
);
700 cpos
+= num_clusters
;
706 static int ocfs2_xattr_ibody_get(struct inode
*inode
,
711 struct ocfs2_xattr_search
*xs
)
713 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
714 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
715 struct ocfs2_xattr_value_root
*xv
;
719 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
722 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
723 xs
->header
= (struct ocfs2_xattr_header
*)
724 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
725 xs
->base
= (void *)xs
->header
;
726 xs
->here
= xs
->header
->xh_entries
;
728 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
731 size
= le64_to_cpu(xs
->here
->xe_value_size
);
733 if (size
> buffer_size
)
735 if (ocfs2_xattr_is_local(xs
->here
)) {
736 memcpy(buffer
, (void *)xs
->base
+
737 le16_to_cpu(xs
->here
->xe_name_offset
) +
738 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
), size
);
740 xv
= (struct ocfs2_xattr_value_root
*)
741 (xs
->base
+ le16_to_cpu(
742 xs
->here
->xe_name_offset
) +
743 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
));
744 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
756 static int ocfs2_xattr_block_get(struct inode
*inode
,
761 struct ocfs2_xattr_search
*xs
)
763 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
764 struct buffer_head
*blk_bh
= NULL
;
765 struct ocfs2_xattr_block
*xb
;
766 struct ocfs2_xattr_value_root
*xv
;
768 int ret
= -ENODATA
, name_offset
, name_len
, block_off
, i
;
770 if (!di
->i_xattr_loc
)
773 memset(&xs
->bucket
, 0, sizeof(xs
->bucket
));
775 ret
= ocfs2_read_block(inode
, le64_to_cpu(di
->i_xattr_loc
), &blk_bh
);
781 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
782 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
787 xs
->xattr_bh
= blk_bh
;
789 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
790 xs
->header
= &xb
->xb_attrs
.xb_header
;
791 xs
->base
= (void *)xs
->header
;
792 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
793 xs
->here
= xs
->header
->xh_entries
;
795 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
797 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
803 size
= le64_to_cpu(xs
->here
->xe_value_size
);
806 if (size
> buffer_size
)
809 name_offset
= le16_to_cpu(xs
->here
->xe_name_offset
);
810 name_len
= OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
);
811 i
= xs
->here
- xs
->header
->xh_entries
;
813 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
814 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
819 xs
->base
= xs
->bucket
.bhs
[block_off
]->b_data
;
821 if (ocfs2_xattr_is_local(xs
->here
)) {
822 memcpy(buffer
, (void *)xs
->base
+
823 name_offset
+ name_len
, size
);
825 xv
= (struct ocfs2_xattr_value_root
*)
826 (xs
->base
+ name_offset
+ name_len
);
827 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
837 for (i
= 0; i
< OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
; i
++)
838 brelse(xs
->bucket
.bhs
[i
]);
839 memset(&xs
->bucket
, 0, sizeof(xs
->bucket
));
847 * Copy an extended attribute into the buffer provided.
848 * Buffer is NULL to compute the size of buffer required.
850 static int ocfs2_xattr_get(struct inode
*inode
,
857 struct ocfs2_dinode
*di
= NULL
;
858 struct buffer_head
*di_bh
= NULL
;
859 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
860 struct ocfs2_xattr_search xis
= {
861 .not_found
= -ENODATA
,
863 struct ocfs2_xattr_search xbs
= {
864 .not_found
= -ENODATA
,
867 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
870 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
873 ret
= ocfs2_inode_lock(inode
, &di_bh
, 0);
878 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
879 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
881 down_read(&oi
->ip_xattr_sem
);
882 ret
= ocfs2_xattr_ibody_get(inode
, name_index
, name
, buffer
,
885 ret
= ocfs2_xattr_block_get(inode
, name_index
, name
, buffer
,
887 up_read(&oi
->ip_xattr_sem
);
888 ocfs2_inode_unlock(inode
, 0);
895 static int __ocfs2_xattr_set_value_outside(struct inode
*inode
,
896 struct ocfs2_xattr_value_root
*xv
,
900 int ret
= 0, i
, cp_len
, credits
;
901 u16 blocksize
= inode
->i_sb
->s_blocksize
;
902 u32 p_cluster
, num_clusters
;
903 u32 cpos
= 0, bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
904 u32 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, value_len
);
906 struct buffer_head
*bh
= NULL
;
909 BUG_ON(clusters
> le32_to_cpu(xv
->xr_clusters
));
911 credits
= clusters
* bpc
;
912 handle
= ocfs2_start_trans(OCFS2_SB(inode
->i_sb
), credits
);
913 if (IS_ERR(handle
)) {
914 ret
= PTR_ERR(handle
);
919 while (cpos
< clusters
) {
920 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
921 &num_clusters
, &xv
->xr_list
);
927 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
929 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
930 ret
= ocfs2_read_block(inode
, blkno
, &bh
);
936 ret
= ocfs2_journal_access(handle
,
939 OCFS2_JOURNAL_ACCESS_WRITE
);
945 cp_len
= value_len
> blocksize
? blocksize
: value_len
;
946 memcpy(bh
->b_data
, value
, cp_len
);
949 if (cp_len
< blocksize
)
950 memset(bh
->b_data
+ cp_len
, 0,
953 ret
= ocfs2_journal_dirty(handle
, bh
);
962 * XXX: do we need to empty all the following
963 * blocks in this cluster?
968 cpos
+= num_clusters
;
971 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
978 static int ocfs2_xattr_cleanup(struct inode
*inode
,
979 struct ocfs2_xattr_info
*xi
,
980 struct ocfs2_xattr_search
*xs
,
983 handle_t
*handle
= NULL
;
985 size_t name_len
= strlen(xi
->name
);
986 void *val
= xs
->base
+ offs
;
987 size_t size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
989 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
990 OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
991 if (IS_ERR(handle
)) {
992 ret
= PTR_ERR(handle
);
996 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
997 OCFS2_JOURNAL_ACCESS_WRITE
);
1002 /* Decrease xattr count */
1003 le16_add_cpu(&xs
->header
->xh_count
, -1);
1004 /* Remove the xattr entry and tree root which has already be set*/
1005 memset((void *)xs
->here
, 0, sizeof(struct ocfs2_xattr_entry
));
1006 memset(val
, 0, size
);
1008 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1012 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1017 static int ocfs2_xattr_update_entry(struct inode
*inode
,
1018 struct ocfs2_xattr_info
*xi
,
1019 struct ocfs2_xattr_search
*xs
,
1022 handle_t
*handle
= NULL
;
1025 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1026 OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
1027 if (IS_ERR(handle
)) {
1028 ret
= PTR_ERR(handle
);
1032 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1033 OCFS2_JOURNAL_ACCESS_WRITE
);
1039 xs
->here
->xe_name_offset
= cpu_to_le16(offs
);
1040 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1041 if (xi
->value_len
<= OCFS2_XATTR_INLINE_SIZE
)
1042 ocfs2_xattr_set_local(xs
->here
, 1);
1044 ocfs2_xattr_set_local(xs
->here
, 0);
1045 ocfs2_xattr_hash_entry(inode
, xs
->header
, xs
->here
);
1047 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1051 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1057 * ocfs2_xattr_set_value_outside()
1059 * Set large size value in B tree.
1061 static int ocfs2_xattr_set_value_outside(struct inode
*inode
,
1062 struct ocfs2_xattr_info
*xi
,
1063 struct ocfs2_xattr_search
*xs
,
1066 size_t name_len
= strlen(xi
->name
);
1067 void *val
= xs
->base
+ offs
;
1068 struct ocfs2_xattr_value_root
*xv
= NULL
;
1069 size_t size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1072 memset(val
, 0, size
);
1073 memcpy(val
, xi
->name
, name_len
);
1074 xv
= (struct ocfs2_xattr_value_root
*)
1075 (val
+ OCFS2_XATTR_SIZE(name_len
));
1076 xv
->xr_clusters
= 0;
1077 xv
->xr_last_eb_blk
= 0;
1078 xv
->xr_list
.l_tree_depth
= 0;
1079 xv
->xr_list
.l_count
= cpu_to_le16(1);
1080 xv
->xr_list
.l_next_free_rec
= 0;
1082 ret
= ocfs2_xattr_value_truncate(inode
, xs
->xattr_bh
, xv
,
1088 ret
= __ocfs2_xattr_set_value_outside(inode
, xv
, xi
->value
,
1094 ret
= ocfs2_xattr_update_entry(inode
, xi
, xs
, offs
);
1102 * ocfs2_xattr_set_entry_local()
1104 * Set, replace or remove extended attribute in local.
1106 static void ocfs2_xattr_set_entry_local(struct inode
*inode
,
1107 struct ocfs2_xattr_info
*xi
,
1108 struct ocfs2_xattr_search
*xs
,
1109 struct ocfs2_xattr_entry
*last
,
1112 size_t name_len
= strlen(xi
->name
);
1115 if (xi
->value
&& xs
->not_found
) {
1116 /* Insert the new xattr entry. */
1117 le16_add_cpu(&xs
->header
->xh_count
, 1);
1118 ocfs2_xattr_set_type(last
, xi
->name_index
);
1119 ocfs2_xattr_set_local(last
, 1);
1120 last
->xe_name_len
= name_len
;
1126 first_val
= xs
->base
+ min_offs
;
1127 offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1128 val
= xs
->base
+ offs
;
1130 if (le64_to_cpu(xs
->here
->xe_value_size
) >
1131 OCFS2_XATTR_INLINE_SIZE
)
1132 size
= OCFS2_XATTR_SIZE(name_len
) +
1133 OCFS2_XATTR_ROOT_SIZE
;
1135 size
= OCFS2_XATTR_SIZE(name_len
) +
1136 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1138 if (xi
->value
&& size
== OCFS2_XATTR_SIZE(name_len
) +
1139 OCFS2_XATTR_SIZE(xi
->value_len
)) {
1140 /* The old and the new value have the
1141 same size. Just replace the value. */
1142 ocfs2_xattr_set_local(xs
->here
, 1);
1143 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1144 /* Clear value bytes. */
1145 memset(val
+ OCFS2_XATTR_SIZE(name_len
),
1147 OCFS2_XATTR_SIZE(xi
->value_len
));
1148 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
1153 /* Remove the old name+value. */
1154 memmove(first_val
+ size
, first_val
, val
- first_val
);
1155 memset(first_val
, 0, size
);
1156 xs
->here
->xe_name_hash
= 0;
1157 xs
->here
->xe_name_offset
= 0;
1158 ocfs2_xattr_set_local(xs
->here
, 1);
1159 xs
->here
->xe_value_size
= 0;
1163 /* Adjust all value offsets. */
1164 last
= xs
->header
->xh_entries
;
1165 for (i
= 0 ; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1166 size_t o
= le16_to_cpu(last
->xe_name_offset
);
1169 last
->xe_name_offset
= cpu_to_le16(o
+ size
);
1174 /* Remove the old entry. */
1176 memmove(xs
->here
, xs
->here
+ 1,
1177 (void *)last
- (void *)xs
->here
);
1178 memset(last
, 0, sizeof(struct ocfs2_xattr_entry
));
1179 le16_add_cpu(&xs
->header
->xh_count
, -1);
1183 /* Insert the new name+value. */
1184 size_t size
= OCFS2_XATTR_SIZE(name_len
) +
1185 OCFS2_XATTR_SIZE(xi
->value_len
);
1186 void *val
= xs
->base
+ min_offs
- size
;
1188 xs
->here
->xe_name_offset
= cpu_to_le16(min_offs
- size
);
1189 memset(val
, 0, size
);
1190 memcpy(val
, xi
->name
, name_len
);
1191 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
1194 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1195 ocfs2_xattr_set_local(xs
->here
, 1);
1196 ocfs2_xattr_hash_entry(inode
, xs
->header
, xs
->here
);
1203 * ocfs2_xattr_set_entry()
1205 * Set extended attribute entry into inode or block.
1207 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1208 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1209 * then set value in B tree with set_value_outside().
1211 static int ocfs2_xattr_set_entry(struct inode
*inode
,
1212 struct ocfs2_xattr_info
*xi
,
1213 struct ocfs2_xattr_search
*xs
,
1216 struct ocfs2_xattr_entry
*last
;
1217 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1218 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1219 size_t min_offs
= xs
->end
- xs
->base
, name_len
= strlen(xi
->name
);
1221 handle_t
*handle
= NULL
;
1223 struct ocfs2_xattr_info xi_l
= {
1224 .name_index
= xi
->name_index
,
1227 .value_len
= xi
->value_len
,
1230 /* Compute min_offs, last and free space. */
1231 last
= xs
->header
->xh_entries
;
1233 for (i
= 0 ; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1234 size_t offs
= le16_to_cpu(last
->xe_name_offset
);
1235 if (offs
< min_offs
)
1240 free
= min_offs
- ((void *)last
- xs
->base
) - sizeof(__u32
);
1244 if (!xs
->not_found
) {
1246 if (ocfs2_xattr_is_local(xs
->here
))
1247 size
= OCFS2_XATTR_SIZE(name_len
) +
1248 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1250 size
= OCFS2_XATTR_SIZE(name_len
) +
1251 OCFS2_XATTR_ROOT_SIZE
;
1252 free
+= (size
+ sizeof(struct ocfs2_xattr_entry
));
1254 /* Check free space in inode or block */
1255 if (xi
->value
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1256 if (free
< sizeof(struct ocfs2_xattr_entry
) +
1257 OCFS2_XATTR_SIZE(name_len
) +
1258 OCFS2_XATTR_ROOT_SIZE
) {
1262 size_l
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1263 xi_l
.value
= (void *)&def_xv
;
1264 xi_l
.value_len
= OCFS2_XATTR_ROOT_SIZE
;
1265 } else if (xi
->value
) {
1266 if (free
< sizeof(struct ocfs2_xattr_entry
) +
1267 OCFS2_XATTR_SIZE(name_len
) +
1268 OCFS2_XATTR_SIZE(xi
->value_len
)) {
1274 if (!xs
->not_found
) {
1275 /* For existing extended attribute */
1276 size_t size
= OCFS2_XATTR_SIZE(name_len
) +
1277 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1278 size_t offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1279 void *val
= xs
->base
+ offs
;
1281 if (ocfs2_xattr_is_local(xs
->here
) && size
== size_l
) {
1282 /* Replace existing local xattr with tree root */
1283 ret
= ocfs2_xattr_set_value_outside(inode
, xi
, xs
,
1288 } else if (!ocfs2_xattr_is_local(xs
->here
)) {
1289 /* For existing xattr which has value outside */
1290 struct ocfs2_xattr_value_root
*xv
= NULL
;
1291 xv
= (struct ocfs2_xattr_value_root
*)(val
+
1292 OCFS2_XATTR_SIZE(name_len
));
1294 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1296 * If new value need set outside also,
1297 * first truncate old value to new value,
1298 * then set new value with set_value_outside().
1300 ret
= ocfs2_xattr_value_truncate(inode
,
1309 ret
= __ocfs2_xattr_set_value_outside(inode
,
1318 ret
= ocfs2_xattr_update_entry(inode
,
1327 * If new value need set in local,
1328 * just trucate old value to zero.
1330 ret
= ocfs2_xattr_value_truncate(inode
,
1340 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1341 OCFS2_INODE_UPDATE_CREDITS
);
1342 if (IS_ERR(handle
)) {
1343 ret
= PTR_ERR(handle
);
1348 ret
= ocfs2_journal_access(handle
, inode
, xs
->inode_bh
,
1349 OCFS2_JOURNAL_ACCESS_WRITE
);
1355 if (!(flag
& OCFS2_INLINE_XATTR_FL
)) {
1356 /* set extended attribute in external block. */
1357 ret
= ocfs2_extend_trans(handle
,
1358 OCFS2_INODE_UPDATE_CREDITS
+
1359 OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
1364 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1365 OCFS2_JOURNAL_ACCESS_WRITE
);
1373 * Set value in local, include set tree root in local.
1374 * This is the first step for value size >INLINE_SIZE.
1376 ocfs2_xattr_set_entry_local(inode
, &xi_l
, xs
, last
, min_offs
);
1378 if (!(flag
& OCFS2_INLINE_XATTR_FL
)) {
1379 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1386 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) &&
1387 (flag
& OCFS2_INLINE_XATTR_FL
)) {
1388 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1389 unsigned int xattrsize
= osb
->s_xattr_inline_size
;
1392 * Adjust extent record count or inline data size
1393 * to reserve space for extended attribute.
1395 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1396 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
1397 le16_add_cpu(&idata
->id_count
, -xattrsize
);
1398 } else if (!(ocfs2_inode_is_fast_symlink(inode
))) {
1399 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
1400 le16_add_cpu(&el
->l_count
, -(xattrsize
/
1401 sizeof(struct ocfs2_extent_rec
)));
1403 di
->i_xattr_inline_size
= cpu_to_le16(xattrsize
);
1405 /* Update xattr flag */
1406 spin_lock(&oi
->ip_lock
);
1407 oi
->ip_dyn_features
|= flag
;
1408 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1409 spin_unlock(&oi
->ip_lock
);
1410 /* Update inode ctime */
1411 inode
->i_ctime
= CURRENT_TIME
;
1412 di
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
1413 di
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
1415 ret
= ocfs2_journal_dirty(handle
, xs
->inode_bh
);
1420 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1422 if (!ret
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1424 * Set value outside in B tree.
1425 * This is the second step for value size > INLINE_SIZE.
1427 size_t offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1428 ret
= ocfs2_xattr_set_value_outside(inode
, xi
, xs
, offs
);
1434 * If set value outside failed, we have to clean
1435 * the junk tree root we have already set in local.
1437 ret2
= ocfs2_xattr_cleanup(inode
, xi
, xs
, offs
);
1447 static int ocfs2_remove_value_outside(struct inode
*inode
,
1448 struct buffer_head
*bh
,
1449 struct ocfs2_xattr_header
*header
)
1453 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
1454 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
1456 if (!ocfs2_xattr_is_local(entry
)) {
1457 struct ocfs2_xattr_value_root
*xv
;
1460 val
= (void *)header
+
1461 le16_to_cpu(entry
->xe_name_offset
);
1462 xv
= (struct ocfs2_xattr_value_root
*)
1463 (val
+ OCFS2_XATTR_SIZE(entry
->xe_name_len
));
1464 ret
= ocfs2_xattr_value_truncate(inode
, bh
, xv
, 0);
1475 static int ocfs2_xattr_ibody_remove(struct inode
*inode
,
1476 struct buffer_head
*di_bh
)
1479 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1480 struct ocfs2_xattr_header
*header
;
1483 header
= (struct ocfs2_xattr_header
*)
1484 ((void *)di
+ inode
->i_sb
->s_blocksize
-
1485 le16_to_cpu(di
->i_xattr_inline_size
));
1487 ret
= ocfs2_remove_value_outside(inode
, di_bh
, header
);
1492 static int ocfs2_xattr_block_remove(struct inode
*inode
,
1493 struct buffer_head
*blk_bh
)
1495 struct ocfs2_xattr_block
*xb
;
1498 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1499 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1500 struct ocfs2_xattr_header
*header
= &(xb
->xb_attrs
.xb_header
);
1501 ret
= ocfs2_remove_value_outside(inode
, blk_bh
, header
);
1503 ret
= ocfs2_delete_xattr_index_block(inode
, blk_bh
);
1508 static int ocfs2_xattr_free_block(struct inode
*inode
,
1511 struct inode
*xb_alloc_inode
;
1512 struct buffer_head
*xb_alloc_bh
= NULL
;
1513 struct buffer_head
*blk_bh
= NULL
;
1514 struct ocfs2_xattr_block
*xb
;
1515 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1521 ret
= ocfs2_read_block(inode
, block
, &blk_bh
);
1527 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1528 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
1533 ret
= ocfs2_xattr_block_remove(inode
, blk_bh
);
1539 blk
= le64_to_cpu(xb
->xb_blkno
);
1540 bit
= le16_to_cpu(xb
->xb_suballoc_bit
);
1541 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1543 xb_alloc_inode
= ocfs2_get_system_file_inode(osb
,
1544 EXTENT_ALLOC_SYSTEM_INODE
,
1545 le16_to_cpu(xb
->xb_suballoc_slot
));
1546 if (!xb_alloc_inode
) {
1551 mutex_lock(&xb_alloc_inode
->i_mutex
);
1553 ret
= ocfs2_inode_lock(xb_alloc_inode
, &xb_alloc_bh
, 1);
1559 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
1560 if (IS_ERR(handle
)) {
1561 ret
= PTR_ERR(handle
);
1566 ret
= ocfs2_free_suballoc_bits(handle
, xb_alloc_inode
, xb_alloc_bh
,
1571 ocfs2_commit_trans(osb
, handle
);
1573 ocfs2_inode_unlock(xb_alloc_inode
, 1);
1574 brelse(xb_alloc_bh
);
1576 mutex_unlock(&xb_alloc_inode
->i_mutex
);
1577 iput(xb_alloc_inode
);
1584 * ocfs2_xattr_remove()
1586 * Free extended attribute resources associated with this inode.
1588 int ocfs2_xattr_remove(struct inode
*inode
, struct buffer_head
*di_bh
)
1590 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1591 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1595 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
1598 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1601 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
1602 ret
= ocfs2_xattr_ibody_remove(inode
, di_bh
);
1609 if (di
->i_xattr_loc
) {
1610 ret
= ocfs2_xattr_free_block(inode
,
1611 le64_to_cpu(di
->i_xattr_loc
));
1618 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1619 OCFS2_INODE_UPDATE_CREDITS
);
1620 if (IS_ERR(handle
)) {
1621 ret
= PTR_ERR(handle
);
1625 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
1626 OCFS2_JOURNAL_ACCESS_WRITE
);
1632 di
->i_xattr_loc
= 0;
1634 spin_lock(&oi
->ip_lock
);
1635 oi
->ip_dyn_features
&= ~(OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
);
1636 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1637 spin_unlock(&oi
->ip_lock
);
1639 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1643 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1648 static int ocfs2_xattr_has_space_inline(struct inode
*inode
,
1649 struct ocfs2_dinode
*di
)
1651 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1652 unsigned int xattrsize
= OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
;
1655 if (xattrsize
< OCFS2_MIN_XATTR_INLINE_SIZE
)
1658 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1659 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
1660 free
= le16_to_cpu(idata
->id_count
) - le64_to_cpu(di
->i_size
);
1661 } else if (ocfs2_inode_is_fast_symlink(inode
)) {
1662 free
= ocfs2_fast_symlink_chars(inode
->i_sb
) -
1663 le64_to_cpu(di
->i_size
);
1665 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
1666 free
= (le16_to_cpu(el
->l_count
) -
1667 le16_to_cpu(el
->l_next_free_rec
)) *
1668 sizeof(struct ocfs2_extent_rec
);
1670 if (free
>= xattrsize
)
1677 * ocfs2_xattr_ibody_find()
1679 * Find extended attribute in inode block and
1680 * fill search info into struct ocfs2_xattr_search.
1682 static int ocfs2_xattr_ibody_find(struct inode
*inode
,
1685 struct ocfs2_xattr_search
*xs
)
1687 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1688 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1692 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
1695 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
1696 down_read(&oi
->ip_alloc_sem
);
1697 has_space
= ocfs2_xattr_has_space_inline(inode
, di
);
1698 up_read(&oi
->ip_alloc_sem
);
1703 xs
->xattr_bh
= xs
->inode_bh
;
1704 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
1705 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)
1706 xs
->header
= (struct ocfs2_xattr_header
*)
1707 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
1709 xs
->header
= (struct ocfs2_xattr_header
*)
1710 (xs
->end
- OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
);
1711 xs
->base
= (void *)xs
->header
;
1712 xs
->here
= xs
->header
->xh_entries
;
1714 /* Find the named attribute. */
1715 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
1716 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1717 if (ret
&& ret
!= -ENODATA
)
1719 xs
->not_found
= ret
;
1726 * ocfs2_xattr_ibody_set()
1728 * Set, replace or remove an extended attribute into inode block.
1731 static int ocfs2_xattr_ibody_set(struct inode
*inode
,
1732 struct ocfs2_xattr_info
*xi
,
1733 struct ocfs2_xattr_search
*xs
)
1735 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1736 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1739 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
1742 down_write(&oi
->ip_alloc_sem
);
1743 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
1744 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
1750 ret
= ocfs2_xattr_set_entry(inode
, xi
, xs
,
1751 (OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
));
1753 up_write(&oi
->ip_alloc_sem
);
1759 * ocfs2_xattr_block_find()
1761 * Find extended attribute in external block and
1762 * fill search info into struct ocfs2_xattr_search.
1764 static int ocfs2_xattr_block_find(struct inode
*inode
,
1767 struct ocfs2_xattr_search
*xs
)
1769 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1770 struct buffer_head
*blk_bh
= NULL
;
1771 struct ocfs2_xattr_block
*xb
;
1774 if (!di
->i_xattr_loc
)
1777 ret
= ocfs2_read_block(inode
, le64_to_cpu(di
->i_xattr_loc
), &blk_bh
);
1783 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1784 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
1789 xs
->xattr_bh
= blk_bh
;
1791 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1792 xs
->header
= &xb
->xb_attrs
.xb_header
;
1793 xs
->base
= (void *)xs
->header
;
1794 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
1795 xs
->here
= xs
->header
->xh_entries
;
1797 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1799 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
1803 if (ret
&& ret
!= -ENODATA
) {
1804 xs
->xattr_bh
= NULL
;
1807 xs
->not_found
= ret
;
1816 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1817 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1820 static int ocfs2_restore_xattr_block(struct inode
*inode
,
1821 struct ocfs2_xattr_search
*xs
)
1825 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1826 struct ocfs2_xattr_block
*xb
=
1827 (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
1828 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
1829 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
1831 BUG_ON(!(xb_flags
& OCFS2_XATTR_INDEXED
) ||
1832 le16_to_cpu(el
->l_next_free_rec
) != 0);
1834 handle
= ocfs2_start_trans(osb
, OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
1835 if (IS_ERR(handle
)) {
1836 ret
= PTR_ERR(handle
);
1841 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1842 OCFS2_JOURNAL_ACCESS_WRITE
);
1848 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
1849 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
1851 xb
->xb_flags
= cpu_to_le16(xb_flags
& ~OCFS2_XATTR_INDEXED
);
1853 ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1856 ocfs2_commit_trans(osb
, handle
);
1862 * ocfs2_xattr_block_set()
1864 * Set, replace or remove an extended attribute into external block.
1867 static int ocfs2_xattr_block_set(struct inode
*inode
,
1868 struct ocfs2_xattr_info
*xi
,
1869 struct ocfs2_xattr_search
*xs
)
1871 struct buffer_head
*new_bh
= NULL
;
1872 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1873 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1874 struct ocfs2_alloc_context
*meta_ac
= NULL
;
1875 handle_t
*handle
= NULL
;
1876 struct ocfs2_xattr_block
*xblk
= NULL
;
1877 u16 suballoc_bit_start
;
1882 if (!xs
->xattr_bh
) {
1884 * Alloc one external block for extended attribute
1887 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, &meta_ac
);
1892 handle
= ocfs2_start_trans(osb
,
1893 OCFS2_XATTR_BLOCK_CREATE_CREDITS
);
1894 if (IS_ERR(handle
)) {
1895 ret
= PTR_ERR(handle
);
1899 ret
= ocfs2_journal_access(handle
, inode
, xs
->inode_bh
,
1900 OCFS2_JOURNAL_ACCESS_CREATE
);
1906 ret
= ocfs2_claim_metadata(osb
, handle
, meta_ac
, 1,
1907 &suballoc_bit_start
, &num_got
,
1914 new_bh
= sb_getblk(inode
->i_sb
, first_blkno
);
1915 ocfs2_set_new_buffer_uptodate(inode
, new_bh
);
1917 ret
= ocfs2_journal_access(handle
, inode
, new_bh
,
1918 OCFS2_JOURNAL_ACCESS_CREATE
);
1924 /* Initialize ocfs2_xattr_block */
1925 xs
->xattr_bh
= new_bh
;
1926 xblk
= (struct ocfs2_xattr_block
*)new_bh
->b_data
;
1927 memset(xblk
, 0, inode
->i_sb
->s_blocksize
);
1928 strcpy((void *)xblk
, OCFS2_XATTR_BLOCK_SIGNATURE
);
1929 xblk
->xb_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
1930 xblk
->xb_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
1931 xblk
->xb_fs_generation
= cpu_to_le32(osb
->fs_generation
);
1932 xblk
->xb_blkno
= cpu_to_le64(first_blkno
);
1934 xs
->header
= &xblk
->xb_attrs
.xb_header
;
1935 xs
->base
= (void *)xs
->header
;
1936 xs
->end
= (void *)xblk
+ inode
->i_sb
->s_blocksize
;
1937 xs
->here
= xs
->header
->xh_entries
;
1940 ret
= ocfs2_journal_dirty(handle
, new_bh
);
1945 di
->i_xattr_loc
= cpu_to_le64(first_blkno
);
1946 ret
= ocfs2_journal_dirty(handle
, xs
->inode_bh
);
1950 ocfs2_commit_trans(osb
, handle
);
1953 ocfs2_free_alloc_context(meta_ac
);
1957 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
1959 if (!(le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1960 /* Set extended attribute into external block */
1961 ret
= ocfs2_xattr_set_entry(inode
, xi
, xs
, OCFS2_HAS_XATTR_FL
);
1962 if (!ret
|| ret
!= -ENOSPC
)
1965 ret
= ocfs2_xattr_create_index_block(inode
, xs
);
1970 ret
= ocfs2_xattr_set_entry_index_block(inode
, xi
, xs
);
1971 if (!ret
&& xblk
->xb_attrs
.xb_root
.xt_list
.l_next_free_rec
== 0)
1972 ret
= ocfs2_restore_xattr_block(inode
, xs
);
1982 * Set, replace or remove an extended attribute for this inode.
1983 * value is NULL to remove an existing extended attribute, else either
1984 * create or replace an extended attribute.
1986 int ocfs2_xattr_set(struct inode
*inode
,
1993 struct buffer_head
*di_bh
= NULL
;
1994 struct ocfs2_dinode
*di
;
1996 u16 i
, blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
1998 struct ocfs2_xattr_info xi
= {
1999 .name_index
= name_index
,
2002 .value_len
= value_len
,
2005 struct ocfs2_xattr_search xis
= {
2006 .not_found
= -ENODATA
,
2009 struct ocfs2_xattr_search xbs
= {
2010 .not_found
= -ENODATA
,
2013 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
2016 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
2021 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
2022 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2024 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2026 * Scan inode and external block to find the same name
2027 * extended attribute and collect search infomation.
2029 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
2032 if (xis
.not_found
) {
2033 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
2038 if (xis
.not_found
&& xbs
.not_found
) {
2040 if (flags
& XATTR_REPLACE
)
2047 if (flags
& XATTR_CREATE
)
2052 /* Remove existing extended attribute */
2054 ret
= ocfs2_xattr_ibody_set(inode
, &xi
, &xis
);
2055 else if (!xbs
.not_found
)
2056 ret
= ocfs2_xattr_block_set(inode
, &xi
, &xbs
);
2058 /* We always try to set extended attribute into inode first*/
2059 ret
= ocfs2_xattr_ibody_set(inode
, &xi
, &xis
);
2060 if (!ret
&& !xbs
.not_found
) {
2062 * If succeed and that extended attribute existing in
2063 * external block, then we will remove it.
2067 ret
= ocfs2_xattr_block_set(inode
, &xi
, &xbs
);
2068 } else if (ret
== -ENOSPC
) {
2069 if (di
->i_xattr_loc
&& !xbs
.xattr_bh
) {
2070 ret
= ocfs2_xattr_block_find(inode
, name_index
,
2076 * If no space in inode, we will set extended attribute
2077 * into external block.
2079 ret
= ocfs2_xattr_block_set(inode
, &xi
, &xbs
);
2082 if (!xis
.not_found
) {
2084 * If succeed and that extended attribute
2085 * existing in inode, we will remove it.
2089 ret
= ocfs2_xattr_ibody_set(inode
, &xi
, &xis
);
2094 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2095 ocfs2_inode_unlock(inode
, 1);
2097 brelse(xbs
.xattr_bh
);
2098 for (i
= 0; i
< blk_per_bucket
; i
++)
2099 brelse(xbs
.bucket
.bhs
[i
]);
2105 * Find the xattr extent rec which may contains name_hash.
2106 * e_cpos will be the first name hash of the xattr rec.
2107 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2109 static int ocfs2_xattr_get_rec(struct inode
*inode
,
2114 struct ocfs2_extent_list
*el
)
2117 struct buffer_head
*eb_bh
= NULL
;
2118 struct ocfs2_extent_block
*eb
;
2119 struct ocfs2_extent_rec
*rec
= NULL
;
2122 if (el
->l_tree_depth
) {
2123 ret
= ocfs2_find_leaf(inode
, el
, name_hash
, &eb_bh
);
2129 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
2132 if (el
->l_tree_depth
) {
2133 ocfs2_error(inode
->i_sb
,
2134 "Inode %lu has non zero tree depth in "
2135 "xattr tree block %llu\n", inode
->i_ino
,
2136 (unsigned long long)eb_bh
->b_blocknr
);
2142 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
2143 rec
= &el
->l_recs
[i
];
2145 if (le32_to_cpu(rec
->e_cpos
) <= name_hash
) {
2146 e_blkno
= le64_to_cpu(rec
->e_blkno
);
2152 ocfs2_error(inode
->i_sb
, "Inode %lu has bad extent "
2153 "record (%u, %u, 0) in xattr", inode
->i_ino
,
2154 le32_to_cpu(rec
->e_cpos
),
2155 ocfs2_rec_clusters(el
, rec
));
2160 *p_blkno
= le64_to_cpu(rec
->e_blkno
);
2161 *num_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
2163 *e_cpos
= le32_to_cpu(rec
->e_cpos
);
2169 typedef int (xattr_bucket_func
)(struct inode
*inode
,
2170 struct ocfs2_xattr_bucket
*bucket
,
2173 static int ocfs2_find_xe_in_bucket(struct inode
*inode
,
2174 struct buffer_head
*header_bh
,
2181 int i
, ret
= 0, cmp
= 1, block_off
, new_offset
;
2182 struct ocfs2_xattr_header
*xh
=
2183 (struct ocfs2_xattr_header
*)header_bh
->b_data
;
2184 size_t name_len
= strlen(name
);
2185 struct ocfs2_xattr_entry
*xe
= NULL
;
2186 struct buffer_head
*name_bh
= NULL
;
2190 * We don't use binary search in the bucket because there
2191 * may be multiple entries with the same name hash.
2193 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
2194 xe
= &xh
->xh_entries
[i
];
2196 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
))
2198 else if (name_hash
< le32_to_cpu(xe
->xe_name_hash
))
2201 cmp
= name_index
- ocfs2_xattr_get_type(xe
);
2203 cmp
= name_len
- xe
->xe_name_len
;
2207 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
2217 ret
= ocfs2_read_block(inode
, header_bh
->b_blocknr
+ block_off
,
2223 xe_name
= name_bh
->b_data
+ new_offset
;
2225 cmp
= memcmp(name
, xe_name
, name_len
);
2241 * Find the specified xattr entry in a series of buckets.
2242 * This series start from p_blkno and last for num_clusters.
2243 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2244 * the num of the valid buckets.
2246 * Return the buffer_head this xattr should reside in. And if the xattr's
2247 * hash is in the gap of 2 buckets, return the lower bucket.
2249 static int ocfs2_xattr_bucket_find(struct inode
*inode
,
2256 struct ocfs2_xattr_search
*xs
)
2259 struct buffer_head
*bh
= NULL
;
2260 struct buffer_head
*lower_bh
= NULL
;
2261 struct ocfs2_xattr_header
*xh
= NULL
;
2262 struct ocfs2_xattr_entry
*xe
= NULL
;
2264 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2265 int low_bucket
= 0, bucket
, high_bucket
;
2269 ret
= ocfs2_read_block(inode
, p_blkno
, &bh
);
2275 xh
= (struct ocfs2_xattr_header
*)bh
->b_data
;
2276 high_bucket
= le16_to_cpu(xh
->xh_num_buckets
) - 1;
2278 while (low_bucket
<= high_bucket
) {
2281 bucket
= (low_bucket
+ high_bucket
) / 2;
2283 blkno
= p_blkno
+ bucket
* blk_per_bucket
;
2285 ret
= ocfs2_read_block(inode
, blkno
, &bh
);
2291 xh
= (struct ocfs2_xattr_header
*)bh
->b_data
;
2292 xe
= &xh
->xh_entries
[0];
2293 if (name_hash
< le32_to_cpu(xe
->xe_name_hash
)) {
2294 high_bucket
= bucket
- 1;
2299 * Check whether the hash of the last entry in our
2300 * bucket is larger than the search one. for an empty
2301 * bucket, the last one is also the first one.
2304 xe
= &xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1];
2306 last_hash
= le32_to_cpu(xe
->xe_name_hash
);
2308 /* record lower_bh which may be the insert place. */
2313 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
)) {
2314 low_bucket
= bucket
+ 1;
2318 /* the searched xattr should reside in this bucket if exists. */
2319 ret
= ocfs2_find_xe_in_bucket(inode
, lower_bh
,
2320 name_index
, name
, name_hash
,
2330 * Record the bucket we have found.
2331 * When the xattr's hash value is in the gap of 2 buckets, we will
2332 * always set it to the previous bucket.
2336 * We can't find any bucket whose first name_hash is less
2337 * than the find name_hash.
2339 BUG_ON(bh
->b_blocknr
!= p_blkno
);
2343 xs
->bucket
.bhs
[0] = lower_bh
;
2344 xs
->bucket
.xh
= (struct ocfs2_xattr_header
*)
2345 xs
->bucket
.bhs
[0]->b_data
;
2348 xs
->header
= xs
->bucket
.xh
;
2349 xs
->base
= xs
->bucket
.bhs
[0]->b_data
;
2350 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
2354 * If we have found the xattr enty, read all the blocks in
2357 ret
= ocfs2_read_blocks(inode
, xs
->bucket
.bhs
[0]->b_blocknr
+ 1,
2358 blk_per_bucket
- 1, &xs
->bucket
.bhs
[1],
2365 xs
->here
= &xs
->header
->xh_entries
[index
];
2366 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name
,
2367 (unsigned long long)xs
->bucket
.bhs
[0]->b_blocknr
, index
);
2377 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
2378 struct buffer_head
*root_bh
,
2381 struct ocfs2_xattr_search
*xs
)
2384 struct ocfs2_xattr_block
*xb
=
2385 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
2386 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
2387 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
2389 u32 first_hash
, num_clusters
= 0;
2390 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
2392 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
2395 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2396 name
, name_hash
, name_index
);
2398 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &first_hash
,
2405 BUG_ON(p_blkno
== 0 || num_clusters
== 0 || first_hash
> name_hash
);
2407 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2408 "in the rec is %u\n", num_clusters
, p_blkno
, first_hash
);
2410 ret
= ocfs2_xattr_bucket_find(inode
, name_index
, name
, name_hash
,
2411 p_blkno
, first_hash
, num_clusters
, xs
);
2417 static int ocfs2_iterate_xattr_buckets(struct inode
*inode
,
2420 xattr_bucket_func
*func
,
2424 int blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2425 u32 bpc
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
2426 u32 num_buckets
= clusters
* bpc
;
2427 struct ocfs2_xattr_bucket bucket
;
2429 memset(&bucket
, 0, sizeof(bucket
));
2431 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2434 for (i
= 0; i
< num_buckets
; i
++, blkno
+= blk_per_bucket
) {
2435 ret
= ocfs2_read_blocks(inode
, blkno
, blk_per_bucket
,
2442 bucket
.xh
= (struct ocfs2_xattr_header
*)bucket
.bhs
[0]->b_data
;
2444 * The real bucket num in this series of blocks is stored
2445 * in the 1st bucket.
2448 num_buckets
= le16_to_cpu(bucket
.xh
->xh_num_buckets
);
2450 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno
,
2451 le32_to_cpu(bucket
.xh
->xh_entries
[0].xe_name_hash
));
2453 ret
= func(inode
, &bucket
, para
);
2460 for (j
= 0; j
< blk_per_bucket
; j
++)
2461 brelse(bucket
.bhs
[j
]);
2462 memset(&bucket
, 0, sizeof(bucket
));
2466 for (j
= 0; j
< blk_per_bucket
; j
++)
2467 brelse(bucket
.bhs
[j
]);
2472 struct ocfs2_xattr_tree_list
{
2478 static int ocfs2_xattr_bucket_get_name_value(struct inode
*inode
,
2479 struct ocfs2_xattr_header
*xh
,
2486 if (index
< 0 || index
>= le16_to_cpu(xh
->xh_count
))
2489 name_offset
= le16_to_cpu(xh
->xh_entries
[index
].xe_name_offset
);
2491 *block_off
= name_offset
>> inode
->i_sb
->s_blocksize_bits
;
2492 *new_offset
= name_offset
% inode
->i_sb
->s_blocksize
;
2497 static int ocfs2_list_xattr_bucket(struct inode
*inode
,
2498 struct ocfs2_xattr_bucket
*bucket
,
2502 struct ocfs2_xattr_tree_list
*xl
= (struct ocfs2_xattr_tree_list
*)para
;
2503 int i
, block_off
, new_offset
;
2504 const char *prefix
, *name
;
2506 for (i
= 0 ; i
< le16_to_cpu(bucket
->xh
->xh_count
); i
++) {
2507 struct ocfs2_xattr_entry
*entry
= &bucket
->xh
->xh_entries
[i
];
2508 type
= ocfs2_xattr_get_type(entry
);
2509 prefix
= ocfs2_xattr_prefix(type
);
2512 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
2520 name
= (const char *)bucket
->bhs
[block_off
]->b_data
+
2522 ret
= ocfs2_xattr_list_entry(xl
->buffer
,
2526 entry
->xe_name_len
);
2535 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
2536 struct ocfs2_xattr_tree_root
*xt
,
2540 struct ocfs2_extent_list
*el
= &xt
->xt_list
;
2542 u32 name_hash
= UINT_MAX
, e_cpos
= 0, num_clusters
= 0;
2544 struct ocfs2_xattr_tree_list xl
= {
2546 .buffer_size
= buffer_size
,
2550 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
2553 while (name_hash
> 0) {
2554 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
2555 &e_cpos
, &num_clusters
, el
);
2561 ret
= ocfs2_iterate_xattr_buckets(inode
, p_blkno
, num_clusters
,
2562 ocfs2_list_xattr_bucket
,
2572 name_hash
= e_cpos
- 1;
2580 static int cmp_xe(const void *a
, const void *b
)
2582 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
2583 u32 l_hash
= le32_to_cpu(l
->xe_name_hash
);
2584 u32 r_hash
= le32_to_cpu(r
->xe_name_hash
);
2586 if (l_hash
> r_hash
)
2588 if (l_hash
< r_hash
)
2593 static void swap_xe(void *a
, void *b
, int size
)
2595 struct ocfs2_xattr_entry
*l
= a
, *r
= b
, tmp
;
2598 memcpy(l
, r
, sizeof(struct ocfs2_xattr_entry
));
2599 memcpy(r
, &tmp
, sizeof(struct ocfs2_xattr_entry
));
2603 * When the ocfs2_xattr_block is filled up, new bucket will be created
2604 * and all the xattr entries will be moved to the new bucket.
2605 * Note: we need to sort the entries since they are not saved in order
2606 * in the ocfs2_xattr_block.
2608 static void ocfs2_cp_xattr_block_to_bucket(struct inode
*inode
,
2609 struct buffer_head
*xb_bh
,
2610 struct buffer_head
*xh_bh
,
2611 struct buffer_head
*data_bh
)
2613 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
2614 u16 offset
, size
, off_change
;
2615 struct ocfs2_xattr_entry
*xe
;
2616 struct ocfs2_xattr_block
*xb
=
2617 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
2618 struct ocfs2_xattr_header
*xb_xh
= &xb
->xb_attrs
.xb_header
;
2619 struct ocfs2_xattr_header
*xh
=
2620 (struct ocfs2_xattr_header
*)xh_bh
->b_data
;
2621 u16 count
= le16_to_cpu(xb_xh
->xh_count
);
2622 char *target
= xh_bh
->b_data
, *src
= xb_bh
->b_data
;
2624 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2625 (unsigned long long)xb_bh
->b_blocknr
,
2626 (unsigned long long)xh_bh
->b_blocknr
);
2628 memset(xh_bh
->b_data
, 0, blocksize
);
2630 memset(data_bh
->b_data
, 0, blocksize
);
2632 * Since the xe_name_offset is based on ocfs2_xattr_header,
2633 * there is a offset change corresponding to the change of
2634 * ocfs2_xattr_header's position.
2636 off_change
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
2637 xe
= &xb_xh
->xh_entries
[count
- 1];
2638 offset
= le16_to_cpu(xe
->xe_name_offset
) + off_change
;
2639 size
= blocksize
- offset
;
2641 /* copy all the names and values. */
2643 target
= data_bh
->b_data
;
2644 memcpy(target
+ offset
, src
+ offset
, size
);
2646 /* Init new header now. */
2647 xh
->xh_count
= xb_xh
->xh_count
;
2648 xh
->xh_num_buckets
= cpu_to_le16(1);
2649 xh
->xh_name_value_len
= cpu_to_le16(size
);
2650 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
- size
);
2652 /* copy all the entries. */
2653 target
= xh_bh
->b_data
;
2654 offset
= offsetof(struct ocfs2_xattr_header
, xh_entries
);
2655 size
= count
* sizeof(struct ocfs2_xattr_entry
);
2656 memcpy(target
+ offset
, (char *)xb_xh
+ offset
, size
);
2658 /* Change the xe offset for all the xe because of the move. */
2659 off_change
= OCFS2_XATTR_BUCKET_SIZE
- blocksize
+
2660 offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
2661 for (i
= 0; i
< count
; i
++)
2662 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
, off_change
);
2664 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2665 offset
, size
, off_change
);
2667 sort(target
+ offset
, count
, sizeof(struct ocfs2_xattr_entry
),
2672 * After we move xattr from block to index btree, we have to
2673 * update ocfs2_xattr_search to the new xe and base.
2675 * When the entry is in xattr block, xattr_bh indicates the storage place.
2676 * While if the entry is in index b-tree, "bucket" indicates the
2677 * real place of the xattr.
2679 static int ocfs2_xattr_update_xattr_search(struct inode
*inode
,
2680 struct ocfs2_xattr_search
*xs
,
2681 struct buffer_head
*old_bh
,
2682 struct buffer_head
*new_bh
)
2685 char *buf
= old_bh
->b_data
;
2686 struct ocfs2_xattr_block
*old_xb
= (struct ocfs2_xattr_block
*)buf
;
2687 struct ocfs2_xattr_header
*old_xh
= &old_xb
->xb_attrs
.xb_header
;
2688 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
2689 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2691 xs
->bucket
.bhs
[0] = new_bh
;
2693 xs
->bucket
.xh
= (struct ocfs2_xattr_header
*)xs
->bucket
.bhs
[0]->b_data
;
2694 xs
->header
= xs
->bucket
.xh
;
2696 xs
->base
= new_bh
->b_data
;
2697 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
2699 if (!xs
->not_found
) {
2700 if (OCFS2_XATTR_BUCKET_SIZE
!= blocksize
) {
2701 ret
= ocfs2_read_blocks(inode
,
2702 xs
->bucket
.bhs
[0]->b_blocknr
+ 1,
2703 blk_per_bucket
- 1, &xs
->bucket
.bhs
[1],
2710 i
= xs
->here
- old_xh
->xh_entries
;
2711 xs
->here
= &xs
->header
->xh_entries
[i
];
2718 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
2719 struct ocfs2_xattr_search
*xs
)
2721 int ret
, credits
= OCFS2_SUBALLOC_ALLOC
;
2725 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2726 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2727 struct ocfs2_alloc_context
*data_ac
;
2728 struct buffer_head
*xh_bh
= NULL
, *data_bh
= NULL
;
2729 struct buffer_head
*xb_bh
= xs
->xattr_bh
;
2730 struct ocfs2_xattr_block
*xb
=
2731 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
2732 struct ocfs2_xattr_tree_root
*xr
;
2733 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
2734 u16 bpb
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2736 mlog(0, "create xattr index block for %llu\n",
2737 (unsigned long long)xb_bh
->b_blocknr
);
2739 BUG_ON(xb_flags
& OCFS2_XATTR_INDEXED
);
2741 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
2749 * We can use this lock for now, and maybe move to a dedicated mutex
2750 * if performance becomes a problem later.
2752 down_write(&oi
->ip_alloc_sem
);
2755 * 3 more credits, one for xattr block update, one for the 1st block
2756 * of the new xattr bucket and one for the value/data.
2759 handle
= ocfs2_start_trans(osb
, credits
);
2760 if (IS_ERR(handle
)) {
2761 ret
= PTR_ERR(handle
);
2766 ret
= ocfs2_journal_access(handle
, inode
, xb_bh
,
2767 OCFS2_JOURNAL_ACCESS_WRITE
);
2773 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
, &len
);
2780 * The bucket may spread in many blocks, and
2781 * we will only touch the 1st block and the last block
2782 * in the whole bucket(one for entry and one for data).
2784 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
2786 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno
);
2788 xh_bh
= sb_getblk(inode
->i_sb
, blkno
);
2795 ocfs2_set_new_buffer_uptodate(inode
, xh_bh
);
2797 ret
= ocfs2_journal_access(handle
, inode
, xh_bh
,
2798 OCFS2_JOURNAL_ACCESS_CREATE
);
2805 data_bh
= sb_getblk(inode
->i_sb
, blkno
+ bpb
- 1);
2812 ocfs2_set_new_buffer_uptodate(inode
, data_bh
);
2814 ret
= ocfs2_journal_access(handle
, inode
, data_bh
,
2815 OCFS2_JOURNAL_ACCESS_CREATE
);
2822 ocfs2_cp_xattr_block_to_bucket(inode
, xb_bh
, xh_bh
, data_bh
);
2824 ocfs2_journal_dirty(handle
, xh_bh
);
2826 ocfs2_journal_dirty(handle
, data_bh
);
2828 ocfs2_xattr_update_xattr_search(inode
, xs
, xb_bh
, xh_bh
);
2830 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2831 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
2832 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
2834 xr
= &xb
->xb_attrs
.xb_root
;
2835 xr
->xt_clusters
= cpu_to_le32(1);
2836 xr
->xt_last_eb_blk
= 0;
2837 xr
->xt_list
.l_tree_depth
= 0;
2838 xr
->xt_list
.l_count
= cpu_to_le16(ocfs2_xattr_recs_per_xb(inode
->i_sb
));
2839 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
2841 xr
->xt_list
.l_recs
[0].e_cpos
= 0;
2842 xr
->xt_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
2843 xr
->xt_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
2845 xb
->xb_flags
= cpu_to_le16(xb_flags
| OCFS2_XATTR_INDEXED
);
2847 ret
= ocfs2_journal_dirty(handle
, xb_bh
);
2854 ocfs2_commit_trans(osb
, handle
);
2857 up_write(&oi
->ip_alloc_sem
);
2861 ocfs2_free_alloc_context(data_ac
);
2869 static int cmp_xe_offset(const void *a
, const void *b
)
2871 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
2872 u32 l_name_offset
= le16_to_cpu(l
->xe_name_offset
);
2873 u32 r_name_offset
= le16_to_cpu(r
->xe_name_offset
);
2875 if (l_name_offset
< r_name_offset
)
2877 if (l_name_offset
> r_name_offset
)
2883 * defrag a xattr bucket if we find that the bucket has some
2884 * holes beteen name/value pairs.
2885 * We will move all the name/value pairs to the end of the bucket
2886 * so that we can spare some space for insertion.
2888 static int ocfs2_defrag_xattr_bucket(struct inode
*inode
,
2889 struct ocfs2_xattr_bucket
*bucket
)
2892 size_t end
, offset
, len
, value_len
;
2893 struct ocfs2_xattr_header
*xh
;
2894 char *entries
, *buf
, *bucket_buf
= NULL
;
2895 u64 blkno
= bucket
->bhs
[0]->b_blocknr
;
2896 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2898 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2900 struct buffer_head
**bhs
;
2901 struct ocfs2_xattr_entry
*xe
;
2903 bhs
= kzalloc(sizeof(struct buffer_head
*) * blk_per_bucket
,
2908 ret
= ocfs2_read_blocks(inode
, blkno
, blk_per_bucket
, bhs
, 0);
2913 * In order to make the operation more efficient and generic,
2914 * we copy all the blocks into a contiguous memory and do the
2915 * defragment there, so if anything is error, we will not touch
2918 bucket_buf
= kmalloc(OCFS2_XATTR_BUCKET_SIZE
, GFP_NOFS
);
2925 for (i
= 0; i
< blk_per_bucket
; i
++, buf
+= blocksize
)
2926 memcpy(buf
, bhs
[i
]->b_data
, blocksize
);
2928 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)), blk_per_bucket
);
2929 if (IS_ERR(handle
)) {
2930 ret
= PTR_ERR(handle
);
2936 for (i
= 0; i
< blk_per_bucket
; i
++) {
2937 ret
= ocfs2_journal_access(handle
, inode
, bhs
[i
],
2938 OCFS2_JOURNAL_ACCESS_WRITE
);
2945 xh
= (struct ocfs2_xattr_header
*)bucket_buf
;
2946 entries
= (char *)xh
->xh_entries
;
2947 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
2949 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2950 "xh_free_start = %u, xh_name_value_len = %u.\n",
2951 blkno
, le16_to_cpu(xh
->xh_count
), xh_free_start
,
2952 le16_to_cpu(xh
->xh_name_value_len
));
2955 * sort all the entries by their offset.
2956 * the largest will be the first, so that we can
2957 * move them to the end one by one.
2959 sort(entries
, le16_to_cpu(xh
->xh_count
),
2960 sizeof(struct ocfs2_xattr_entry
),
2961 cmp_xe_offset
, swap_xe
);
2963 /* Move all name/values to the end of the bucket. */
2964 xe
= xh
->xh_entries
;
2965 end
= OCFS2_XATTR_BUCKET_SIZE
;
2966 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, xe
++) {
2967 offset
= le16_to_cpu(xe
->xe_name_offset
);
2968 if (ocfs2_xattr_is_local(xe
))
2969 value_len
= OCFS2_XATTR_SIZE(
2970 le64_to_cpu(xe
->xe_value_size
));
2972 value_len
= OCFS2_XATTR_ROOT_SIZE
;
2973 len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
) + value_len
;
2976 * We must make sure that the name/value pair
2977 * exist in the same block. So adjust end to
2978 * the previous block end if needed.
2980 if (((end
- len
) / blocksize
!=
2981 (end
- 1) / blocksize
))
2982 end
= end
- end
% blocksize
;
2984 if (end
> offset
+ len
) {
2985 memmove(bucket_buf
+ end
- len
,
2986 bucket_buf
+ offset
, len
);
2987 xe
->xe_name_offset
= cpu_to_le16(end
- len
);
2990 mlog_bug_on_msg(end
< offset
+ len
, "Defrag check failed for "
2991 "bucket %llu\n", (unsigned long long)blkno
);
2996 mlog_bug_on_msg(xh_free_start
> end
, "Defrag check failed for "
2997 "bucket %llu\n", (unsigned long long)blkno
);
2999 if (xh_free_start
== end
)
3002 memset(bucket_buf
+ xh_free_start
, 0, end
- xh_free_start
);
3003 xh
->xh_free_start
= cpu_to_le16(end
);
3005 /* sort the entries by their name_hash. */
3006 sort(entries
, le16_to_cpu(xh
->xh_count
),
3007 sizeof(struct ocfs2_xattr_entry
),
3011 for (i
= 0; i
< blk_per_bucket
; i
++, buf
+= blocksize
) {
3012 memcpy(bhs
[i
]->b_data
, buf
, blocksize
);
3013 ocfs2_journal_dirty(handle
, bhs
[i
]);
3017 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
3021 for (i
= 0; i
< blk_per_bucket
; i
++)
3031 * Move half nums of the xattr bucket in the previous cluster to this new
3032 * cluster. We only touch the last cluster of the previous extend record.
3034 * first_bh is the first buffer_head of a series of bucket in the same
3035 * extent rec and header_bh is the header of one bucket in this cluster.
3036 * They will be updated if we move the data header_bh contains to the new
3037 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3039 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode
*inode
,
3041 struct buffer_head
**first_bh
,
3042 struct buffer_head
**header_bh
,
3048 int i
, ret
, credits
;
3049 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3050 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3051 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
3052 int blocksize
= inode
->i_sb
->s_blocksize
;
3053 struct buffer_head
*old_bh
, *new_bh
, *prev_bh
, *new_first_bh
= NULL
;
3054 struct ocfs2_xattr_header
*new_xh
;
3055 struct ocfs2_xattr_header
*xh
=
3056 (struct ocfs2_xattr_header
*)((*first_bh
)->b_data
);
3058 BUG_ON(le16_to_cpu(xh
->xh_num_buckets
) < num_buckets
);
3059 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
== osb
->s_clustersize
);
3061 prev_bh
= *first_bh
;
3063 xh
= (struct ocfs2_xattr_header
*)prev_bh
->b_data
;
3065 prev_blkno
+= (num_clusters
- 1) * bpc
+ bpc
/ 2;
3067 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3068 prev_blkno
, new_blkno
);
3071 * We need to update the 1st half of the new cluster and
3072 * 1 more for the update of the 1st bucket of the previous
3075 credits
= bpc
/ 2 + 1;
3076 ret
= ocfs2_extend_trans(handle
, credits
);
3082 ret
= ocfs2_journal_access(handle
, inode
, prev_bh
,
3083 OCFS2_JOURNAL_ACCESS_WRITE
);
3089 for (i
= 0; i
< bpc
/ 2; i
++, prev_blkno
++, new_blkno
++) {
3090 old_bh
= new_bh
= NULL
;
3091 new_bh
= sb_getblk(inode
->i_sb
, new_blkno
);
3098 ocfs2_set_new_buffer_uptodate(inode
, new_bh
);
3100 ret
= ocfs2_journal_access(handle
, inode
, new_bh
,
3101 OCFS2_JOURNAL_ACCESS_CREATE
);
3108 ret
= ocfs2_read_block(inode
, prev_blkno
, &old_bh
);
3115 memcpy(new_bh
->b_data
, old_bh
->b_data
, blocksize
);
3118 new_xh
= (struct ocfs2_xattr_header
*)new_bh
->b_data
;
3119 new_xh
->xh_num_buckets
= cpu_to_le16(num_buckets
/ 2);
3122 *first_hash
= le32_to_cpu(
3123 new_xh
->xh_entries
[0].xe_name_hash
);
3124 new_first_bh
= new_bh
;
3125 get_bh(new_first_bh
);
3128 ocfs2_journal_dirty(handle
, new_bh
);
3130 if (*header_bh
== old_bh
) {
3132 *header_bh
= new_bh
;
3136 *first_bh
= new_first_bh
;
3143 le16_add_cpu(&xh
->xh_num_buckets
, -(num_buckets
/ 2));
3145 ocfs2_journal_dirty(handle
, prev_bh
);
3148 brelse(new_first_bh
);
3152 static int ocfs2_read_xattr_bucket(struct inode
*inode
,
3154 struct buffer_head
**bhs
,
3158 u16 i
, blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3161 return ocfs2_read_blocks(inode
, blkno
,
3162 blk_per_bucket
, bhs
, 0);
3164 for (i
= 0; i
< blk_per_bucket
; i
++) {
3165 bhs
[i
] = sb_getblk(inode
->i_sb
, blkno
+ i
);
3166 if (bhs
[i
] == NULL
) {
3171 ocfs2_set_new_buffer_uptodate(inode
, bhs
[i
]);
3178 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3179 * first_hash will record the 1st hash of the new bucket.
3181 static int ocfs2_half_xattr_bucket(struct inode
*inode
,
3186 int new_bucket_head
)
3189 u16 count
, start
, len
, name_value_len
, xe_len
, name_offset
;
3190 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3191 struct buffer_head
**s_bhs
, **t_bhs
= NULL
;
3192 struct ocfs2_xattr_header
*xh
;
3193 struct ocfs2_xattr_entry
*xe
;
3194 int blocksize
= inode
->i_sb
->s_blocksize
;
3196 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3199 s_bhs
= kcalloc(blk_per_bucket
, sizeof(struct buffer_head
*), GFP_NOFS
);
3203 ret
= ocfs2_read_xattr_bucket(inode
, blk
, s_bhs
, 0);
3209 ret
= ocfs2_journal_access(handle
, inode
, s_bhs
[0],
3210 OCFS2_JOURNAL_ACCESS_WRITE
);
3216 t_bhs
= kcalloc(blk_per_bucket
, sizeof(struct buffer_head
*), GFP_NOFS
);
3222 ret
= ocfs2_read_xattr_bucket(inode
, new_blk
, t_bhs
, new_bucket_head
);
3228 for (i
= 0; i
< blk_per_bucket
; i
++) {
3229 ret
= ocfs2_journal_access(handle
, inode
, t_bhs
[i
],
3230 OCFS2_JOURNAL_ACCESS_CREATE
);
3237 /* copy the whole bucket to the new first. */
3238 for (i
= 0; i
< blk_per_bucket
; i
++)
3239 memcpy(t_bhs
[i
]->b_data
, s_bhs
[i
]->b_data
, blocksize
);
3241 /* update the new bucket. */
3242 xh
= (struct ocfs2_xattr_header
*)t_bhs
[0]->b_data
;
3243 count
= le16_to_cpu(xh
->xh_count
);
3247 * Calculate the total name/value len and xh_free_start for
3248 * the old bucket first.
3250 name_offset
= OCFS2_XATTR_BUCKET_SIZE
;
3252 for (i
= 0; i
< start
; i
++) {
3253 xe
= &xh
->xh_entries
[i
];
3254 xe_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3255 if (ocfs2_xattr_is_local(xe
))
3257 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3259 xe_len
+= OCFS2_XATTR_ROOT_SIZE
;
3260 name_value_len
+= xe_len
;
3261 if (le16_to_cpu(xe
->xe_name_offset
) < name_offset
)
3262 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3266 * Now begin the modification to the new bucket.
3268 * In the new bucket, We just move the xattr entry to the beginning
3269 * and don't touch the name/value. So there will be some holes in the
3270 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3273 xe
= &xh
->xh_entries
[start
];
3274 len
= sizeof(struct ocfs2_xattr_entry
) * (count
- start
);
3275 mlog(0, "mv xattr entry len %d from %d to %d\n", len
,
3276 (int)((char *)xe
- (char *)xh
),
3277 (int)((char *)xh
->xh_entries
- (char *)xh
));
3278 memmove((char *)xh
->xh_entries
, (char *)xe
, len
);
3279 xe
= &xh
->xh_entries
[count
- start
];
3280 len
= sizeof(struct ocfs2_xattr_entry
) * start
;
3281 memset((char *)xe
, 0, len
);
3283 le16_add_cpu(&xh
->xh_count
, -start
);
3284 le16_add_cpu(&xh
->xh_name_value_len
, -name_value_len
);
3286 /* Calculate xh_free_start for the new bucket. */
3287 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
3288 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
3289 xe
= &xh
->xh_entries
[i
];
3290 xe_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3291 if (ocfs2_xattr_is_local(xe
))
3293 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3295 xe_len
+= OCFS2_XATTR_ROOT_SIZE
;
3296 if (le16_to_cpu(xe
->xe_name_offset
) <
3297 le16_to_cpu(xh
->xh_free_start
))
3298 xh
->xh_free_start
= xe
->xe_name_offset
;
3301 /* set xh->xh_num_buckets for the new xh. */
3302 if (new_bucket_head
)
3303 xh
->xh_num_buckets
= cpu_to_le16(1);
3305 xh
->xh_num_buckets
= 0;
3307 for (i
= 0; i
< blk_per_bucket
; i
++) {
3308 ocfs2_journal_dirty(handle
, t_bhs
[i
]);
3313 /* store the first_hash of the new bucket. */
3315 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3318 * Now only update the 1st block of the old bucket.
3319 * Please note that the entry has been sorted already above.
3321 xh
= (struct ocfs2_xattr_header
*)s_bhs
[0]->b_data
;
3322 memset(&xh
->xh_entries
[start
], 0,
3323 sizeof(struct ocfs2_xattr_entry
) * (count
- start
));
3324 xh
->xh_count
= cpu_to_le16(start
);
3325 xh
->xh_free_start
= cpu_to_le16(name_offset
);
3326 xh
->xh_name_value_len
= cpu_to_le16(name_value_len
);
3328 ocfs2_journal_dirty(handle
, s_bhs
[0]);
3334 for (i
= 0; i
< blk_per_bucket
; i
++)
3340 for (i
= 0; i
< blk_per_bucket
; i
++)
3349 * Copy xattr from one bucket to another bucket.
3351 * The caller must make sure that the journal transaction
3352 * has enough space for journaling.
3354 static int ocfs2_cp_xattr_bucket(struct inode
*inode
,
3361 int blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3362 int blocksize
= inode
->i_sb
->s_blocksize
;
3363 struct buffer_head
**s_bhs
, **t_bhs
= NULL
;
3365 BUG_ON(s_blkno
== t_blkno
);
3367 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3368 s_blkno
, t_blkno
, t_is_new
);
3370 s_bhs
= kzalloc(sizeof(struct buffer_head
*) * blk_per_bucket
,
3375 ret
= ocfs2_read_xattr_bucket(inode
, s_blkno
, s_bhs
, 0);
3379 t_bhs
= kzalloc(sizeof(struct buffer_head
*) * blk_per_bucket
,
3386 ret
= ocfs2_read_xattr_bucket(inode
, t_blkno
, t_bhs
, t_is_new
);
3390 for (i
= 0; i
< blk_per_bucket
; i
++) {
3391 ret
= ocfs2_journal_access(handle
, inode
, t_bhs
[i
],
3392 OCFS2_JOURNAL_ACCESS_WRITE
);
3397 for (i
= 0; i
< blk_per_bucket
; i
++) {
3398 memcpy(t_bhs
[i
]->b_data
, s_bhs
[i
]->b_data
, blocksize
);
3399 ocfs2_journal_dirty(handle
, t_bhs
[i
]);
3404 for (i
= 0; i
< blk_per_bucket
; i
++)
3410 for (i
= 0; i
< blk_per_bucket
; i
++)
3419 * Copy one xattr cluster from src_blk to to_blk.
3420 * The to_blk will become the first bucket header of the cluster, so its
3421 * xh_num_buckets will be initialized as the bucket num in the cluster.
3423 static int ocfs2_cp_xattr_cluster(struct inode
*inode
,
3425 struct buffer_head
*first_bh
,
3430 int i
, ret
, credits
;
3431 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3432 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3433 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
3434 struct buffer_head
*bh
= NULL
;
3435 struct ocfs2_xattr_header
*xh
;
3436 u64 to_blk_start
= to_blk
;
3438 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk
, to_blk
);
3441 * We need to update the new cluster and 1 more for the update of
3442 * the 1st bucket of the previous extent rec.
3445 ret
= ocfs2_extend_trans(handle
, credits
);
3451 ret
= ocfs2_journal_access(handle
, inode
, first_bh
,
3452 OCFS2_JOURNAL_ACCESS_WRITE
);
3458 for (i
= 0; i
< num_buckets
; i
++) {
3459 ret
= ocfs2_cp_xattr_bucket(inode
, handle
,
3460 src_blk
, to_blk
, 1);
3466 src_blk
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3467 to_blk
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3470 /* update the old bucket header. */
3471 xh
= (struct ocfs2_xattr_header
*)first_bh
->b_data
;
3472 le16_add_cpu(&xh
->xh_num_buckets
, -num_buckets
);
3474 ocfs2_journal_dirty(handle
, first_bh
);
3476 /* update the new bucket header. */
3477 ret
= ocfs2_read_block(inode
, to_blk_start
, &bh
);
3483 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3484 OCFS2_JOURNAL_ACCESS_WRITE
);
3490 xh
= (struct ocfs2_xattr_header
*)bh
->b_data
;
3491 xh
->xh_num_buckets
= cpu_to_le16(num_buckets
);
3493 ocfs2_journal_dirty(handle
, bh
);
3496 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3503 * Move half of the xattrs in this cluster to the new cluster.
3504 * This function should only be called when bucket size == cluster size.
3505 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3507 static int ocfs2_half_xattr_cluster(struct inode
*inode
,
3513 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3514 int ret
, credits
= 2 * blk_per_bucket
;
3516 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
< OCFS2_SB(inode
->i_sb
)->s_clustersize
);
3518 ret
= ocfs2_extend_trans(handle
, credits
);
3524 /* Move half of the xattr in start_blk to the next bucket. */
3525 return ocfs2_half_xattr_bucket(inode
, handle
, prev_blk
,
3526 new_blk
, first_hash
, 1);
3530 * Move some xattrs from the old cluster to the new one since they are not
3531 * contiguous in ocfs2 xattr tree.
3533 * new_blk starts a new separate cluster, and we will move some xattrs from
3534 * prev_blk to it. v_start will be set as the first name hash value in this
3535 * new cluster so that it can be used as e_cpos during tree insertion and
3536 * don't collide with our original b-tree operations. first_bh and header_bh
3537 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3538 * to extend the insert bucket.
3540 * The problem is how much xattr should we move to the new one and when should
3541 * we update first_bh and header_bh?
3542 * 1. If cluster size > bucket size, that means the previous cluster has more
3543 * than 1 bucket, so just move half nums of bucket into the new cluster and
3544 * update the first_bh and header_bh if the insert bucket has been moved
3545 * to the new cluster.
3546 * 2. If cluster_size == bucket_size:
3547 * a) If the previous extent rec has more than one cluster and the insert
3548 * place isn't in the last cluster, copy the entire last cluster to the
3549 * new one. This time, we don't need to upate the first_bh and header_bh
3550 * since they will not be moved into the new cluster.
3551 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3552 * the new one. And we set the extend flag to zero if the insert place is
3553 * moved into the new allocated cluster since no extend is needed.
3555 static int ocfs2_adjust_xattr_cross_cluster(struct inode
*inode
,
3557 struct buffer_head
**first_bh
,
3558 struct buffer_head
**header_bh
,
3566 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3568 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3569 prev_blk
, prev_clusters
, new_blk
);
3571 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
)) > 1)
3572 ret
= ocfs2_mv_xattr_bucket_cross_cluster(inode
,
3581 u64 last_blk
= prev_blk
+ bpc
* (prev_clusters
- 1);
3583 if (prev_clusters
> 1 && (*header_bh
)->b_blocknr
!= last_blk
)
3584 ret
= ocfs2_cp_xattr_cluster(inode
, handle
, *first_bh
,
3588 ret
= ocfs2_half_xattr_cluster(inode
, handle
,
3592 if ((*header_bh
)->b_blocknr
== last_blk
&& extend
)
3601 * Add a new cluster for xattr storage.
3603 * If the new cluster is contiguous with the previous one, it will be
3604 * appended to the same extent record, and num_clusters will be updated.
3605 * If not, we will insert a new extent for it and move some xattrs in
3606 * the last cluster into the new allocated one.
3607 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3608 * lose the benefits of hashing because we'll have to search large leaves.
3609 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3612 * first_bh is the first block of the previous extent rec and header_bh
3613 * indicates the bucket we will insert the new xattrs. They will be updated
3614 * when the header_bh is moved into the new cluster.
3616 static int ocfs2_add_new_xattr_cluster(struct inode
*inode
,
3617 struct buffer_head
*root_bh
,
3618 struct buffer_head
**first_bh
,
3619 struct buffer_head
**header_bh
,
3626 u16 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3627 u32 prev_clusters
= *num_clusters
;
3628 u32 clusters_to_add
= 1, bit_off
, num_bits
, v_start
= 0;
3630 handle_t
*handle
= NULL
;
3631 struct ocfs2_alloc_context
*data_ac
= NULL
;
3632 struct ocfs2_alloc_context
*meta_ac
= NULL
;
3633 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3634 struct ocfs2_extent_tree et
;
3636 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3637 "previous xattr blkno = %llu\n",
3638 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
3639 prev_cpos
, prev_blkno
);
3641 ocfs2_init_xattr_tree_extent_tree(&et
, inode
, root_bh
);
3643 ret
= ocfs2_lock_allocators(inode
, &et
, clusters_to_add
, 0,
3644 &data_ac
, &meta_ac
);
3650 credits
= ocfs2_calc_extend_credits(osb
->sb
, et
.et_root_el
,
3652 handle
= ocfs2_start_trans(osb
, credits
);
3653 if (IS_ERR(handle
)) {
3654 ret
= PTR_ERR(handle
);
3660 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
3661 OCFS2_JOURNAL_ACCESS_WRITE
);
3667 ret
= __ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
3668 clusters_to_add
, &bit_off
, &num_bits
);
3675 BUG_ON(num_bits
> clusters_to_add
);
3677 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
3678 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3679 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3681 if (prev_blkno
+ prev_clusters
* bpc
== block
&&
3682 (prev_clusters
+ num_bits
) << osb
->s_clustersize_bits
<=
3683 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
) {
3685 * If this cluster is contiguous with the old one and
3686 * adding this new cluster, we don't surpass the limit of
3687 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3688 * initialized and used like other buckets in the previous
3690 * So add it as a contiguous one. The caller will handle
3693 v_start
= prev_cpos
+ prev_clusters
;
3694 *num_clusters
= prev_clusters
+ num_bits
;
3695 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3698 ret
= ocfs2_adjust_xattr_cross_cluster(inode
,
3713 if (handle
->h_buffer_credits
< credits
) {
3715 * The journal has been restarted before, and don't
3716 * have enough space for the insertion, so extend it
3719 ret
= ocfs2_extend_trans(handle
, credits
);
3725 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3726 num_bits
, block
, v_start
);
3727 ret
= ocfs2_insert_extent(osb
, handle
, inode
, &et
, v_start
, block
,
3728 num_bits
, 0, meta_ac
);
3734 ret
= ocfs2_journal_dirty(handle
, root_bh
);
3742 ocfs2_commit_trans(osb
, handle
);
3744 ocfs2_free_alloc_context(data_ac
);
3746 ocfs2_free_alloc_context(meta_ac
);
3752 * Extend a new xattr bucket and move xattrs to the end one by one until
3753 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3755 static int ocfs2_extend_xattr_bucket(struct inode
*inode
,
3756 struct buffer_head
*first_bh
,
3757 struct buffer_head
*start_bh
,
3761 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3762 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3763 u64 start_blk
= start_bh
->b_blocknr
, end_blk
;
3764 u32 num_buckets
= num_clusters
* ocfs2_xattr_buckets_per_cluster(osb
);
3766 struct ocfs2_xattr_header
*first_xh
=
3767 (struct ocfs2_xattr_header
*)first_bh
->b_data
;
3768 u16 bucket
= le16_to_cpu(first_xh
->xh_num_buckets
);
3770 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3771 "from %llu, len = %u\n", start_blk
,
3772 (unsigned long long)first_bh
->b_blocknr
, num_clusters
);
3774 BUG_ON(bucket
>= num_buckets
);
3776 end_blk
= first_bh
->b_blocknr
+ (bucket
- 1) * blk_per_bucket
;
3779 * We will touch all the buckets after the start_bh(include it).
3780 * Add one more bucket and modify the first_bh.
3782 credits
= end_blk
- start_blk
+ 2 * blk_per_bucket
+ 1;
3783 handle
= ocfs2_start_trans(osb
, credits
);
3784 if (IS_ERR(handle
)) {
3785 ret
= PTR_ERR(handle
);
3791 ret
= ocfs2_journal_access(handle
, inode
, first_bh
,
3792 OCFS2_JOURNAL_ACCESS_WRITE
);
3798 while (end_blk
!= start_blk
) {
3799 ret
= ocfs2_cp_xattr_bucket(inode
, handle
, end_blk
,
3800 end_blk
+ blk_per_bucket
, 0);
3803 end_blk
-= blk_per_bucket
;
3806 /* Move half of the xattr in start_blk to the next bucket. */
3807 ret
= ocfs2_half_xattr_bucket(inode
, handle
, start_blk
,
3808 start_blk
+ blk_per_bucket
, NULL
, 0);
3810 le16_add_cpu(&first_xh
->xh_num_buckets
, 1);
3811 ocfs2_journal_dirty(handle
, first_bh
);
3814 ocfs2_commit_trans(osb
, handle
);
3820 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3821 * xb_bh is the ocfs2_xattr_block.
3822 * We will move all the buckets starting from header_bh to the next place. As
3823 * for this one, half num of its xattrs will be moved to the next one.
3825 * We will allocate a new cluster if current cluster is full and adjust
3826 * header_bh and first_bh if the insert place is moved to the new cluster.
3828 static int ocfs2_add_new_xattr_bucket(struct inode
*inode
,
3829 struct buffer_head
*xb_bh
,
3830 struct buffer_head
*header_bh
)
3832 struct ocfs2_xattr_header
*first_xh
= NULL
;
3833 struct buffer_head
*first_bh
= NULL
;
3834 struct ocfs2_xattr_block
*xb
=
3835 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
3836 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
3837 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
3838 struct ocfs2_xattr_header
*xh
=
3839 (struct ocfs2_xattr_header
*)header_bh
->b_data
;
3840 u32 name_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3841 struct super_block
*sb
= inode
->i_sb
;
3842 struct ocfs2_super
*osb
= OCFS2_SB(sb
);
3843 int ret
, num_buckets
, extend
= 1;
3845 u32 e_cpos
, num_clusters
;
3847 mlog(0, "Add new xattr bucket starting form %llu\n",
3848 (unsigned long long)header_bh
->b_blocknr
);
3851 * Add refrence for header_bh here because it may be
3852 * changed in ocfs2_add_new_xattr_cluster and we need
3853 * to free it in the end.
3857 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &e_cpos
,
3864 ret
= ocfs2_read_block(inode
, p_blkno
, &first_bh
);
3870 num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
) * num_clusters
;
3871 first_xh
= (struct ocfs2_xattr_header
*)first_bh
->b_data
;
3873 if (num_buckets
== le16_to_cpu(first_xh
->xh_num_buckets
)) {
3874 ret
= ocfs2_add_new_xattr_cluster(inode
,
3889 ret
= ocfs2_extend_xattr_bucket(inode
,
3901 static inline char *ocfs2_xattr_bucket_get_val(struct inode
*inode
,
3902 struct ocfs2_xattr_bucket
*bucket
,
3905 int block_off
= offs
>> inode
->i_sb
->s_blocksize_bits
;
3907 offs
= offs
% inode
->i_sb
->s_blocksize
;
3908 return bucket
->bhs
[block_off
]->b_data
+ offs
;
3912 * Handle the normal xattr set, including replace, delete and new.
3914 * Note: "local" indicates the real data's locality. So we can't
3915 * just its bucket locality by its length.
3917 static void ocfs2_xattr_set_entry_normal(struct inode
*inode
,
3918 struct ocfs2_xattr_info
*xi
,
3919 struct ocfs2_xattr_search
*xs
,
3923 struct ocfs2_xattr_entry
*last
, *xe
;
3924 int name_len
= strlen(xi
->name
);
3925 struct ocfs2_xattr_header
*xh
= xs
->header
;
3926 u16 count
= le16_to_cpu(xh
->xh_count
), start
;
3927 size_t blocksize
= inode
->i_sb
->s_blocksize
;
3929 size_t offs
, size
, new_size
;
3931 last
= &xh
->xh_entries
[count
];
3932 if (!xs
->not_found
) {
3934 offs
= le16_to_cpu(xe
->xe_name_offset
);
3935 if (ocfs2_xattr_is_local(xe
))
3936 size
= OCFS2_XATTR_SIZE(name_len
) +
3937 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3939 size
= OCFS2_XATTR_SIZE(name_len
) +
3940 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE
);
3943 * If the new value will be stored outside, xi->value has been
3944 * initalized as an empty ocfs2_xattr_value_root, and the same
3945 * goes with xi->value_len, so we can set new_size safely here.
3946 * See ocfs2_xattr_set_in_bucket.
3948 new_size
= OCFS2_XATTR_SIZE(name_len
) +
3949 OCFS2_XATTR_SIZE(xi
->value_len
);
3951 le16_add_cpu(&xh
->xh_name_value_len
, -size
);
3953 if (new_size
> size
)
3954 goto set_new_name_value
;
3956 /* Now replace the old value with new one. */
3958 xe
->xe_value_size
= cpu_to_le64(xi
->value_len
);
3960 xe
->xe_value_size
= 0;
3962 val
= ocfs2_xattr_bucket_get_val(inode
,
3964 memset(val
+ OCFS2_XATTR_SIZE(name_len
), 0,
3965 size
- OCFS2_XATTR_SIZE(name_len
));
3966 if (OCFS2_XATTR_SIZE(xi
->value_len
) > 0)
3967 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
3968 xi
->value
, xi
->value_len
);
3970 le16_add_cpu(&xh
->xh_name_value_len
, new_size
);
3971 ocfs2_xattr_set_local(xe
, local
);
3975 * Remove the old entry if there is more than one.
3976 * We don't remove the last entry so that we can
3977 * use it to indicate the hash value of the empty
3981 le16_add_cpu(&xh
->xh_count
, -1);
3984 (void *)last
- (void *)xe
);
3986 sizeof(struct ocfs2_xattr_entry
));
3989 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
3994 /* find a new entry for insert. */
3995 int low
= 0, high
= count
- 1, tmp
;
3996 struct ocfs2_xattr_entry
*tmp_xe
;
3998 while (low
<= high
&& count
) {
3999 tmp
= (low
+ high
) / 2;
4000 tmp_xe
= &xh
->xh_entries
[tmp
];
4002 if (name_hash
> le32_to_cpu(tmp_xe
->xe_name_hash
))
4004 else if (name_hash
<
4005 le32_to_cpu(tmp_xe
->xe_name_hash
))
4013 xe
= &xh
->xh_entries
[low
];
4015 memmove(xe
+ 1, xe
, (void *)last
- (void *)xe
);
4017 le16_add_cpu(&xh
->xh_count
, 1);
4018 memset(xe
, 0, sizeof(struct ocfs2_xattr_entry
));
4019 xe
->xe_name_hash
= cpu_to_le32(name_hash
);
4020 xe
->xe_name_len
= name_len
;
4021 ocfs2_xattr_set_type(xe
, xi
->name_index
);
4025 /* Insert the new name+value. */
4026 size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_SIZE(xi
->value_len
);
4029 * We must make sure that the name/value pair
4030 * exists in the same block.
4032 offs
= le16_to_cpu(xh
->xh_free_start
);
4033 start
= offs
- size
;
4035 if (start
>> inode
->i_sb
->s_blocksize_bits
!=
4036 (offs
- 1) >> inode
->i_sb
->s_blocksize_bits
) {
4037 offs
= offs
- offs
% blocksize
;
4038 xh
->xh_free_start
= cpu_to_le16(offs
);
4041 val
= ocfs2_xattr_bucket_get_val(inode
,
4042 &xs
->bucket
, offs
- size
);
4043 xe
->xe_name_offset
= cpu_to_le16(offs
- size
);
4045 memset(val
, 0, size
);
4046 memcpy(val
, xi
->name
, name_len
);
4047 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
), xi
->value
, xi
->value_len
);
4049 xe
->xe_value_size
= cpu_to_le64(xi
->value_len
);
4050 ocfs2_xattr_set_local(xe
, local
);
4052 le16_add_cpu(&xh
->xh_free_start
, -size
);
4053 le16_add_cpu(&xh
->xh_name_value_len
, size
);
4058 static int ocfs2_xattr_bucket_handle_journal(struct inode
*inode
,
4060 struct ocfs2_xattr_search
*xs
,
4061 struct buffer_head
**bhs
,
4064 int ret
= 0, off
, block_off
;
4065 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4068 * First calculate all the blocks we should journal_access
4069 * and journal_dirty. The first block should always be touched.
4071 ret
= ocfs2_journal_dirty(handle
, bhs
[0]);
4075 /* calc the data. */
4076 off
= le16_to_cpu(xe
->xe_name_offset
);
4077 block_off
= off
>> inode
->i_sb
->s_blocksize_bits
;
4078 ret
= ocfs2_journal_dirty(handle
, bhs
[block_off
]);
4086 * Set the xattr entry in the specified bucket.
4087 * The bucket is indicated by xs->bucket and it should have the enough
4088 * space for the xattr insertion.
4090 static int ocfs2_xattr_set_entry_in_bucket(struct inode
*inode
,
4091 struct ocfs2_xattr_info
*xi
,
4092 struct ocfs2_xattr_search
*xs
,
4097 handle_t
*handle
= NULL
;
4098 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4099 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4101 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4102 (unsigned long)xi
->value_len
, xi
->name_index
,
4103 (unsigned long long)xs
->bucket
.bhs
[0]->b_blocknr
);
4105 if (!xs
->bucket
.bhs
[1]) {
4106 ret
= ocfs2_read_blocks(inode
,
4107 xs
->bucket
.bhs
[0]->b_blocknr
+ 1,
4108 blk_per_bucket
- 1, &xs
->bucket
.bhs
[1],
4116 handle
= ocfs2_start_trans(osb
, blk_per_bucket
);
4117 if (IS_ERR(handle
)) {
4118 ret
= PTR_ERR(handle
);
4124 for (i
= 0; i
< blk_per_bucket
; i
++) {
4125 ret
= ocfs2_journal_access(handle
, inode
, xs
->bucket
.bhs
[i
],
4126 OCFS2_JOURNAL_ACCESS_WRITE
);
4133 ocfs2_xattr_set_entry_normal(inode
, xi
, xs
, name_hash
, local
);
4135 /*Only dirty the blocks we have touched in set xattr. */
4136 ret
= ocfs2_xattr_bucket_handle_journal(inode
, handle
, xs
,
4137 xs
->bucket
.bhs
, blk_per_bucket
);
4141 ocfs2_commit_trans(osb
, handle
);
4146 static int ocfs2_xattr_value_update_size(struct inode
*inode
,
4147 struct buffer_head
*xe_bh
,
4148 struct ocfs2_xattr_entry
*xe
,
4152 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4153 handle_t
*handle
= NULL
;
4155 handle
= ocfs2_start_trans(osb
, 1);
4156 if (handle
== NULL
) {
4162 ret
= ocfs2_journal_access(handle
, inode
, xe_bh
,
4163 OCFS2_JOURNAL_ACCESS_WRITE
);
4169 xe
->xe_value_size
= cpu_to_le64(new_size
);
4171 ret
= ocfs2_journal_dirty(handle
, xe_bh
);
4176 ocfs2_commit_trans(osb
, handle
);
4182 * Truncate the specified xe_off entry in xattr bucket.
4183 * bucket is indicated by header_bh and len is the new length.
4184 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4186 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4188 static int ocfs2_xattr_bucket_value_truncate(struct inode
*inode
,
4189 struct buffer_head
*header_bh
,
4195 struct buffer_head
*value_bh
= NULL
;
4196 struct ocfs2_xattr_value_root
*xv
;
4197 struct ocfs2_xattr_entry
*xe
;
4198 struct ocfs2_xattr_header
*xh
=
4199 (struct ocfs2_xattr_header
*)header_bh
->b_data
;
4200 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4202 xe
= &xh
->xh_entries
[xe_off
];
4204 BUG_ON(!xe
|| ocfs2_xattr_is_local(xe
));
4206 offset
= le16_to_cpu(xe
->xe_name_offset
) +
4207 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
4209 value_blk
= offset
/ blocksize
;
4211 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4212 BUG_ON(value_blk
!= (offset
+ OCFS2_XATTR_ROOT_SIZE
- 1) / blocksize
);
4213 value_blk
+= header_bh
->b_blocknr
;
4215 ret
= ocfs2_read_block(inode
, value_blk
, &value_bh
);
4221 xv
= (struct ocfs2_xattr_value_root
*)
4222 (value_bh
->b_data
+ offset
% blocksize
);
4224 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4225 xe_off
, (unsigned long long)header_bh
->b_blocknr
, len
);
4226 ret
= ocfs2_xattr_value_truncate(inode
, value_bh
, xv
, len
);
4232 ret
= ocfs2_xattr_value_update_size(inode
, header_bh
, xe
, len
);
4243 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode
*inode
,
4244 struct ocfs2_xattr_search
*xs
,
4248 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4249 struct ocfs2_xattr_header
*xh
= (struct ocfs2_xattr_header
*)xs
->base
;
4251 BUG_ON(!xs
->bucket
.bhs
[0] || !xe
|| ocfs2_xattr_is_local(xe
));
4253 offset
= xe
- xh
->xh_entries
;
4254 ret
= ocfs2_xattr_bucket_value_truncate(inode
, xs
->bucket
.bhs
[0],
4262 static int ocfs2_xattr_bucket_set_value_outside(struct inode
*inode
,
4263 struct ocfs2_xattr_search
*xs
,
4268 struct ocfs2_xattr_value_root
*xv
;
4269 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4271 BUG_ON(!xs
->base
|| !xe
|| ocfs2_xattr_is_local(xe
));
4273 offset
= le16_to_cpu(xe
->xe_name_offset
) +
4274 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
4276 xv
= (struct ocfs2_xattr_value_root
*)(xs
->base
+ offset
);
4278 return __ocfs2_xattr_set_value_outside(inode
, xv
, val
, value_len
);
4281 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
4282 struct buffer_head
*root_bh
,
4288 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4289 struct inode
*tl_inode
= osb
->osb_tl_inode
;
4291 struct ocfs2_xattr_block
*xb
=
4292 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
4293 struct ocfs2_alloc_context
*meta_ac
= NULL
;
4294 struct ocfs2_cached_dealloc_ctxt dealloc
;
4295 struct ocfs2_extent_tree et
;
4297 ocfs2_init_xattr_tree_extent_tree(&et
, inode
, root_bh
);
4299 ocfs2_init_dealloc_ctxt(&dealloc
);
4301 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4302 cpos
, len
, (unsigned long long)blkno
);
4304 ocfs2_remove_xattr_clusters_from_cache(inode
, blkno
, len
);
4306 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
4312 mutex_lock(&tl_inode
->i_mutex
);
4314 if (ocfs2_truncate_log_needs_flush(osb
)) {
4315 ret
= __ocfs2_flush_truncate_log(osb
);
4322 handle
= ocfs2_start_trans(osb
, OCFS2_REMOVE_EXTENT_CREDITS
);
4323 if (handle
== NULL
) {
4329 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
4330 OCFS2_JOURNAL_ACCESS_WRITE
);
4336 ret
= ocfs2_remove_extent(inode
, &et
, cpos
, len
, handle
, meta_ac
,
4343 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, -len
);
4345 ret
= ocfs2_journal_dirty(handle
, root_bh
);
4351 ret
= ocfs2_truncate_log_append(osb
, handle
, blkno
, len
);
4356 ocfs2_commit_trans(osb
, handle
);
4358 ocfs2_schedule_truncate_log_flush(osb
, 1);
4360 mutex_unlock(&tl_inode
->i_mutex
);
4363 ocfs2_free_alloc_context(meta_ac
);
4365 ocfs2_run_deallocs(osb
, &dealloc
);
4370 static void ocfs2_xattr_bucket_remove_xs(struct inode
*inode
,
4371 struct ocfs2_xattr_search
*xs
)
4373 handle_t
*handle
= NULL
;
4374 struct ocfs2_xattr_header
*xh
= xs
->bucket
.xh
;
4375 struct ocfs2_xattr_entry
*last
= &xh
->xh_entries
[
4376 le16_to_cpu(xh
->xh_count
) - 1];
4379 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)), 1);
4380 if (IS_ERR(handle
)) {
4381 ret
= PTR_ERR(handle
);
4386 ret
= ocfs2_journal_access(handle
, inode
, xs
->bucket
.bhs
[0],
4387 OCFS2_JOURNAL_ACCESS_WRITE
);
4393 /* Remove the old entry. */
4394 memmove(xs
->here
, xs
->here
+ 1,
4395 (void *)last
- (void *)xs
->here
);
4396 memset(last
, 0, sizeof(struct ocfs2_xattr_entry
));
4397 le16_add_cpu(&xh
->xh_count
, -1);
4399 ret
= ocfs2_journal_dirty(handle
, xs
->bucket
.bhs
[0]);
4403 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
4407 * Set the xattr name/value in the bucket specified in xs.
4409 * As the new value in xi may be stored in the bucket or in an outside cluster,
4410 * we divide the whole process into 3 steps:
4411 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4412 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4413 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4414 * 4. If the clusters for the new outside value can't be allocated, we need
4415 * to free the xattr we allocated in set.
4417 static int ocfs2_xattr_set_in_bucket(struct inode
*inode
,
4418 struct ocfs2_xattr_info
*xi
,
4419 struct ocfs2_xattr_search
*xs
)
4423 char *val
= (char *)xi
->value
;
4424 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4425 u32 name_hash
= ocfs2_xattr_name_hash(inode
, xi
->name
,
4428 if (!xs
->not_found
&& !ocfs2_xattr_is_local(xe
)) {
4430 * We need to truncate the xattr storage first.
4432 * If both the old and new value are stored to
4433 * outside block, we only need to truncate
4434 * the storage and then set the value outside.
4436 * If the new value should be stored within block,
4437 * we should free all the outside block first and
4438 * the modification to the xattr block will be done
4439 * by following steps.
4441 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
4442 value_len
= xi
->value_len
;
4446 ret
= ocfs2_xattr_bucket_value_truncate_xs(inode
, xs
,
4452 goto set_value_outside
;
4455 value_len
= xi
->value_len
;
4456 /* So we have to handle the inside block change now. */
4457 if (value_len
> OCFS2_XATTR_INLINE_SIZE
) {
4459 * If the new value will be stored outside of block,
4460 * initalize a new empty value root and insert it first.
4463 xi
->value
= &def_xv
;
4464 xi
->value_len
= OCFS2_XATTR_ROOT_SIZE
;
4467 ret
= ocfs2_xattr_set_entry_in_bucket(inode
, xi
, xs
, name_hash
, local
);
4473 if (value_len
<= OCFS2_XATTR_INLINE_SIZE
)
4476 /* allocate the space now for the outside block storage. */
4477 ret
= ocfs2_xattr_bucket_value_truncate_xs(inode
, xs
,
4482 if (xs
->not_found
) {
4484 * We can't allocate enough clusters for outside
4485 * storage and we have allocated xattr already,
4486 * so need to remove it.
4488 ocfs2_xattr_bucket_remove_xs(inode
, xs
);
4494 ret
= ocfs2_xattr_bucket_set_value_outside(inode
, xs
, val
, value_len
);
4499 /* check whether the xattr bucket is filled up with the same hash value. */
4500 static int ocfs2_check_xattr_bucket_collision(struct inode
*inode
,
4501 struct ocfs2_xattr_bucket
*bucket
)
4503 struct ocfs2_xattr_header
*xh
= bucket
->xh
;
4505 if (xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1].xe_name_hash
==
4506 xh
->xh_entries
[0].xe_name_hash
) {
4507 mlog(ML_ERROR
, "Too much hash collision in xattr bucket %llu, "
4509 (unsigned long long)bucket
->bhs
[0]->b_blocknr
,
4510 le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
));
4517 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
4518 struct ocfs2_xattr_info
*xi
,
4519 struct ocfs2_xattr_search
*xs
)
4521 struct ocfs2_xattr_header
*xh
;
4522 struct ocfs2_xattr_entry
*xe
;
4523 u16 count
, header_size
, xh_free_start
;
4524 int i
, free
, max_free
, need
, old
;
4525 size_t value_size
= 0, name_len
= strlen(xi
->name
);
4526 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4527 int ret
, allocation
= 0;
4528 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4530 mlog_entry("Set xattr %s in xattr index block\n", xi
->name
);
4534 count
= le16_to_cpu(xh
->xh_count
);
4535 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4536 header_size
= sizeof(struct ocfs2_xattr_header
) +
4537 count
* sizeof(struct ocfs2_xattr_entry
);
4538 max_free
= OCFS2_XATTR_BUCKET_SIZE
-
4539 le16_to_cpu(xh
->xh_name_value_len
) - header_size
;
4541 mlog_bug_on_msg(header_size
> blocksize
, "bucket %llu has header size "
4542 "of %u which exceed block size\n",
4543 (unsigned long long)xs
->bucket
.bhs
[0]->b_blocknr
,
4546 if (xi
->value
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
4547 value_size
= OCFS2_XATTR_ROOT_SIZE
;
4549 value_size
= OCFS2_XATTR_SIZE(xi
->value_len
);
4552 need
= sizeof(struct ocfs2_xattr_entry
) +
4553 OCFS2_XATTR_SIZE(name_len
) + value_size
;
4555 need
= value_size
+ OCFS2_XATTR_SIZE(name_len
);
4558 * We only replace the old value if the new length is smaller
4559 * than the old one. Otherwise we will allocate new space in the
4560 * bucket to store it.
4563 if (ocfs2_xattr_is_local(xe
))
4564 old
= OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
4566 old
= OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE
);
4568 if (old
>= value_size
)
4572 free
= xh_free_start
- header_size
;
4574 * We need to make sure the new name/value pair
4575 * can exist in the same block.
4577 if (xh_free_start
% blocksize
< need
)
4578 free
-= xh_free_start
% blocksize
;
4580 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4581 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4582 " %u\n", xs
->not_found
,
4583 (unsigned long long)xs
->bucket
.bhs
[0]->b_blocknr
,
4584 free
, need
, max_free
, le16_to_cpu(xh
->xh_free_start
),
4585 le16_to_cpu(xh
->xh_name_value_len
));
4587 if (free
< need
|| count
== ocfs2_xattr_max_xe_in_bucket(inode
->i_sb
)) {
4588 if (need
<= max_free
&&
4589 count
< ocfs2_xattr_max_xe_in_bucket(inode
->i_sb
)) {
4591 * We can create the space by defragment. Since only the
4592 * name/value will be moved, the xe shouldn't be changed
4595 ret
= ocfs2_defrag_xattr_bucket(inode
, &xs
->bucket
);
4601 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4602 free
= xh_free_start
- header_size
;
4603 if (xh_free_start
% blocksize
< need
)
4604 free
-= xh_free_start
% blocksize
;
4609 mlog(0, "Can't get enough space for xattr insert by "
4610 "defragment. Need %u bytes, but we have %d, so "
4611 "allocate new bucket for it.\n", need
, free
);
4615 * We have to add new buckets or clusters and one
4616 * allocation should leave us enough space for insert.
4621 * We do not allow for overlapping ranges between buckets. And
4622 * the maximum number of collisions we will allow for then is
4623 * one bucket's worth, so check it here whether we need to
4624 * add a new bucket for the insert.
4626 ret
= ocfs2_check_xattr_bucket_collision(inode
, &xs
->bucket
);
4632 ret
= ocfs2_add_new_xattr_bucket(inode
,
4640 for (i
= 0; i
< blk_per_bucket
; i
++)
4641 brelse(xs
->bucket
.bhs
[i
]);
4643 memset(&xs
->bucket
, 0, sizeof(xs
->bucket
));
4645 ret
= ocfs2_xattr_index_block_find(inode
, xs
->xattr_bh
,
4648 if (ret
&& ret
!= -ENODATA
)
4650 xs
->not_found
= ret
;
4656 ret
= ocfs2_xattr_set_in_bucket(inode
, xi
, xs
);
4662 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
4663 struct ocfs2_xattr_bucket
*bucket
,
4667 struct ocfs2_xattr_header
*xh
= bucket
->xh
;
4669 struct ocfs2_xattr_entry
*xe
;
4671 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
4672 xe
= &xh
->xh_entries
[i
];
4673 if (ocfs2_xattr_is_local(xe
))
4676 ret
= ocfs2_xattr_bucket_value_truncate(inode
,
4688 static int ocfs2_delete_xattr_index_block(struct inode
*inode
,
4689 struct buffer_head
*xb_bh
)
4691 struct ocfs2_xattr_block
*xb
=
4692 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4693 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
4695 u32 name_hash
= UINT_MAX
, e_cpos
, num_clusters
;
4698 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
4701 while (name_hash
> 0) {
4702 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
4703 &e_cpos
, &num_clusters
, el
);
4709 ret
= ocfs2_iterate_xattr_buckets(inode
, p_blkno
, num_clusters
,
4710 ocfs2_delete_xattr_in_bucket
,
4717 ret
= ocfs2_rm_xattr_cluster(inode
, xb_bh
,
4718 p_blkno
, e_cpos
, num_clusters
);
4727 name_hash
= e_cpos
- 1;
4735 * 'trusted' attributes support
4737 static size_t ocfs2_xattr_trusted_list(struct inode
*inode
, char *list
,
4738 size_t list_size
, const char *name
,
4741 const size_t prefix_len
= XATTR_TRUSTED_PREFIX_LEN
;
4742 const size_t total_len
= prefix_len
+ name_len
+ 1;
4744 if (list
&& total_len
<= list_size
) {
4745 memcpy(list
, XATTR_TRUSTED_PREFIX
, prefix_len
);
4746 memcpy(list
+ prefix_len
, name
, name_len
);
4747 list
[prefix_len
+ name_len
] = '\0';
4752 static int ocfs2_xattr_trusted_get(struct inode
*inode
, const char *name
,
4753 void *buffer
, size_t size
)
4755 if (strcmp(name
, "") == 0)
4757 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_TRUSTED
, name
,
4761 static int ocfs2_xattr_trusted_set(struct inode
*inode
, const char *name
,
4762 const void *value
, size_t size
, int flags
)
4764 if (strcmp(name
, "") == 0)
4767 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_TRUSTED
, name
, value
,
4771 struct xattr_handler ocfs2_xattr_trusted_handler
= {
4772 .prefix
= XATTR_TRUSTED_PREFIX
,
4773 .list
= ocfs2_xattr_trusted_list
,
4774 .get
= ocfs2_xattr_trusted_get
,
4775 .set
= ocfs2_xattr_trusted_set
,
4779 * 'user' attributes support
4781 static size_t ocfs2_xattr_user_list(struct inode
*inode
, char *list
,
4782 size_t list_size
, const char *name
,
4785 const size_t prefix_len
= XATTR_USER_PREFIX_LEN
;
4786 const size_t total_len
= prefix_len
+ name_len
+ 1;
4787 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4789 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
4792 if (list
&& total_len
<= list_size
) {
4793 memcpy(list
, XATTR_USER_PREFIX
, prefix_len
);
4794 memcpy(list
+ prefix_len
, name
, name_len
);
4795 list
[prefix_len
+ name_len
] = '\0';
4800 static int ocfs2_xattr_user_get(struct inode
*inode
, const char *name
,
4801 void *buffer
, size_t size
)
4803 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4805 if (strcmp(name
, "") == 0)
4807 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
4809 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_USER
, name
,
4813 static int ocfs2_xattr_user_set(struct inode
*inode
, const char *name
,
4814 const void *value
, size_t size
, int flags
)
4816 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4818 if (strcmp(name
, "") == 0)
4820 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
4823 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_USER
, name
, value
,
4827 struct xattr_handler ocfs2_xattr_user_handler
= {
4828 .prefix
= XATTR_USER_PREFIX
,
4829 .list
= ocfs2_xattr_user_list
,
4830 .get
= ocfs2_xattr_user_get
,
4831 .set
= ocfs2_xattr_user_set
,