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 /* The inode these xattrs are associated with */
65 struct inode
*bu_inode
;
67 /* The actual buffers that make up the bucket */
68 struct buffer_head
*bu_bhs
[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
];
70 /* How many blocks make up one bucket for this filesystem */
74 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
75 #define OCFS2_XATTR_INLINE_SIZE 80
77 static struct ocfs2_xattr_def_value_root def_xv
= {
78 .xv
.xr_list
.l_count
= cpu_to_le16(1),
81 struct xattr_handler
*ocfs2_xattr_handlers
[] = {
82 &ocfs2_xattr_user_handler
,
83 &ocfs2_xattr_trusted_handler
,
87 static struct xattr_handler
*ocfs2_xattr_handler_map
[OCFS2_XATTR_MAX
] = {
88 [OCFS2_XATTR_INDEX_USER
] = &ocfs2_xattr_user_handler
,
89 [OCFS2_XATTR_INDEX_TRUSTED
] = &ocfs2_xattr_trusted_handler
,
92 struct ocfs2_xattr_info
{
99 struct ocfs2_xattr_search
{
100 struct buffer_head
*inode_bh
;
102 * xattr_bh point to the block buffer head which has extended attribute
103 * when extended attribute in inode, xattr_bh is equal to inode_bh.
105 struct buffer_head
*xattr_bh
;
106 struct ocfs2_xattr_header
*header
;
107 struct ocfs2_xattr_bucket
*bucket
;
110 struct ocfs2_xattr_entry
*here
;
114 static int ocfs2_xattr_bucket_get_name_value(struct inode
*inode
,
115 struct ocfs2_xattr_header
*xh
,
120 static int ocfs2_xattr_block_find(struct inode
*inode
,
123 struct ocfs2_xattr_search
*xs
);
124 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
125 struct buffer_head
*root_bh
,
128 struct ocfs2_xattr_search
*xs
);
130 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
131 struct ocfs2_xattr_tree_root
*xt
,
135 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
136 struct ocfs2_xattr_search
*xs
);
138 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
139 struct ocfs2_xattr_info
*xi
,
140 struct ocfs2_xattr_search
*xs
);
142 static int ocfs2_delete_xattr_index_block(struct inode
*inode
,
143 struct buffer_head
*xb_bh
);
145 static inline u16
ocfs2_xattr_buckets_per_cluster(struct ocfs2_super
*osb
)
147 return (1 << osb
->s_clustersize_bits
) / OCFS2_XATTR_BUCKET_SIZE
;
150 static inline u16
ocfs2_blocks_per_xattr_bucket(struct super_block
*sb
)
152 return OCFS2_XATTR_BUCKET_SIZE
/ (1 << sb
->s_blocksize_bits
);
155 static inline u16
ocfs2_xattr_max_xe_in_bucket(struct super_block
*sb
)
157 u16 len
= sb
->s_blocksize
-
158 offsetof(struct ocfs2_xattr_header
, xh_entries
);
160 return len
/ sizeof(struct ocfs2_xattr_entry
);
163 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
164 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
165 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
167 static struct ocfs2_xattr_bucket
*ocfs2_xattr_bucket_new(struct inode
*inode
)
169 struct ocfs2_xattr_bucket
*bucket
;
170 int blks
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
172 BUG_ON(blks
> OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
);
174 bucket
= kzalloc(sizeof(struct ocfs2_xattr_bucket
), GFP_NOFS
);
176 bucket
->bu_inode
= inode
;
177 bucket
->bu_blocks
= blks
;
183 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket
*bucket
)
187 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
188 brelse(bucket
->bu_bhs
[i
]);
189 bucket
->bu_bhs
[i
] = NULL
;
193 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket
*bucket
)
196 ocfs2_xattr_bucket_relse(bucket
);
197 bucket
->bu_inode
= NULL
;
203 * A bucket that has never been written to disk doesn't need to be
204 * read. We just need the buffer_heads. Don't call this for
205 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
208 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
213 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
214 bucket
->bu_bhs
[i
] = sb_getblk(bucket
->bu_inode
->i_sb
,
216 if (!bucket
->bu_bhs
[i
]) {
222 ocfs2_set_new_buffer_uptodate(bucket
->bu_inode
,
227 ocfs2_xattr_bucket_relse(bucket
);
231 /* Read the xattr bucket at xb_blkno */
232 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket
*bucket
,
237 rc
= ocfs2_read_blocks(bucket
->bu_inode
, xb_blkno
,
238 bucket
->bu_blocks
, bucket
->bu_bhs
, 0);
240 ocfs2_xattr_bucket_relse(bucket
);
244 static int ocfs2_xattr_bucket_journal_access(handle_t
*handle
,
245 struct ocfs2_xattr_bucket
*bucket
,
250 for (i
= 0; i
< bucket
->bu_blocks
; i
++) {
251 rc
= ocfs2_journal_access(handle
, bucket
->bu_inode
,
252 bucket
->bu_bhs
[i
], type
);
262 static void ocfs2_xattr_bucket_journal_dirty(handle_t
*handle
,
263 struct ocfs2_xattr_bucket
*bucket
)
267 for (i
= 0; i
< bucket
->bu_blocks
; i
++)
268 ocfs2_journal_dirty(handle
, bucket
->bu_bhs
[i
]);
271 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket
*dest
,
272 struct ocfs2_xattr_bucket
*src
)
275 int blocksize
= src
->bu_inode
->i_sb
->s_blocksize
;
277 BUG_ON(dest
->bu_blocks
!= src
->bu_blocks
);
278 BUG_ON(dest
->bu_inode
!= src
->bu_inode
);
280 for (i
= 0; i
< src
->bu_blocks
; i
++) {
281 memcpy(bucket_block(dest
, i
), bucket_block(src
, i
),
286 static inline const char *ocfs2_xattr_prefix(int name_index
)
288 struct xattr_handler
*handler
= NULL
;
290 if (name_index
> 0 && name_index
< OCFS2_XATTR_MAX
)
291 handler
= ocfs2_xattr_handler_map
[name_index
];
293 return handler
? handler
->prefix
: NULL
;
296 static u32
ocfs2_xattr_name_hash(struct inode
*inode
,
300 /* Get hash value of uuid from super block */
301 u32 hash
= OCFS2_SB(inode
->i_sb
)->uuid_hash
;
304 /* hash extended attribute name */
305 for (i
= 0; i
< name_len
; i
++) {
306 hash
= (hash
<< OCFS2_HASH_SHIFT
) ^
307 (hash
>> (8*sizeof(hash
) - OCFS2_HASH_SHIFT
)) ^
315 * ocfs2_xattr_hash_entry()
317 * Compute the hash of an extended attribute.
319 static void ocfs2_xattr_hash_entry(struct inode
*inode
,
320 struct ocfs2_xattr_header
*header
,
321 struct ocfs2_xattr_entry
*entry
)
324 char *name
= (char *)header
+ le16_to_cpu(entry
->xe_name_offset
);
326 hash
= ocfs2_xattr_name_hash(inode
, name
, entry
->xe_name_len
);
327 entry
->xe_name_hash
= cpu_to_le32(hash
);
332 static int ocfs2_xattr_extend_allocation(struct inode
*inode
,
334 struct buffer_head
*xattr_bh
,
335 struct ocfs2_xattr_value_root
*xv
)
338 int restart_func
= 0;
340 handle_t
*handle
= NULL
;
341 struct ocfs2_alloc_context
*data_ac
= NULL
;
342 struct ocfs2_alloc_context
*meta_ac
= NULL
;
343 enum ocfs2_alloc_restarted why
;
344 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
345 u32 prev_clusters
, logical_start
= le32_to_cpu(xv
->xr_clusters
);
346 struct ocfs2_extent_tree et
;
348 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add
);
350 ocfs2_init_xattr_value_extent_tree(&et
, inode
, xattr_bh
, xv
);
354 status
= ocfs2_lock_allocators(inode
, &et
, clusters_to_add
, 0,
361 credits
= ocfs2_calc_extend_credits(osb
->sb
, et
.et_root_el
,
363 handle
= ocfs2_start_trans(osb
, credits
);
364 if (IS_ERR(handle
)) {
365 status
= PTR_ERR(handle
);
371 restarted_transaction
:
372 status
= ocfs2_journal_access(handle
, inode
, xattr_bh
,
373 OCFS2_JOURNAL_ACCESS_WRITE
);
379 prev_clusters
= le32_to_cpu(xv
->xr_clusters
);
380 status
= ocfs2_add_clusters_in_btree(osb
,
390 if ((status
< 0) && (status
!= -EAGAIN
)) {
391 if (status
!= -ENOSPC
)
396 status
= ocfs2_journal_dirty(handle
, xattr_bh
);
402 clusters_to_add
-= le32_to_cpu(xv
->xr_clusters
) - prev_clusters
;
404 if (why
!= RESTART_NONE
&& clusters_to_add
) {
405 if (why
== RESTART_META
) {
406 mlog(0, "restarting function.\n");
409 BUG_ON(why
!= RESTART_TRANS
);
411 mlog(0, "restarting transaction.\n");
412 /* TODO: This can be more intelligent. */
413 credits
= ocfs2_calc_extend_credits(osb
->sb
,
416 status
= ocfs2_extend_trans(handle
, credits
);
418 /* handle still has to be committed at
424 goto restarted_transaction
;
430 ocfs2_commit_trans(osb
, handle
);
434 ocfs2_free_alloc_context(data_ac
);
438 ocfs2_free_alloc_context(meta_ac
);
441 if ((!status
) && restart_func
) {
449 static int __ocfs2_remove_xattr_range(struct inode
*inode
,
450 struct buffer_head
*root_bh
,
451 struct ocfs2_xattr_value_root
*xv
,
452 u32 cpos
, u32 phys_cpos
, u32 len
,
453 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
456 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
457 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
458 struct inode
*tl_inode
= osb
->osb_tl_inode
;
460 struct ocfs2_alloc_context
*meta_ac
= NULL
;
461 struct ocfs2_extent_tree et
;
463 ocfs2_init_xattr_value_extent_tree(&et
, inode
, root_bh
, xv
);
465 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
471 mutex_lock(&tl_inode
->i_mutex
);
473 if (ocfs2_truncate_log_needs_flush(osb
)) {
474 ret
= __ocfs2_flush_truncate_log(osb
);
481 handle
= ocfs2_start_trans(osb
, OCFS2_REMOVE_EXTENT_CREDITS
);
482 if (IS_ERR(handle
)) {
483 ret
= PTR_ERR(handle
);
488 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
489 OCFS2_JOURNAL_ACCESS_WRITE
);
495 ret
= ocfs2_remove_extent(inode
, &et
, cpos
, len
, handle
, meta_ac
,
502 le32_add_cpu(&xv
->xr_clusters
, -len
);
504 ret
= ocfs2_journal_dirty(handle
, root_bh
);
510 ret
= ocfs2_truncate_log_append(osb
, handle
, phys_blkno
, len
);
515 ocfs2_commit_trans(osb
, handle
);
517 mutex_unlock(&tl_inode
->i_mutex
);
520 ocfs2_free_alloc_context(meta_ac
);
525 static int ocfs2_xattr_shrink_size(struct inode
*inode
,
528 struct buffer_head
*root_bh
,
529 struct ocfs2_xattr_value_root
*xv
)
532 u32 trunc_len
, cpos
, phys_cpos
, alloc_size
;
534 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
535 struct ocfs2_cached_dealloc_ctxt dealloc
;
537 ocfs2_init_dealloc_ctxt(&dealloc
);
539 if (old_clusters
<= new_clusters
)
543 trunc_len
= old_clusters
- new_clusters
;
545 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &phys_cpos
,
546 &alloc_size
, &xv
->xr_list
);
552 if (alloc_size
> trunc_len
)
553 alloc_size
= trunc_len
;
555 ret
= __ocfs2_remove_xattr_range(inode
, root_bh
, xv
, cpos
,
556 phys_cpos
, alloc_size
,
563 block
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
564 ocfs2_remove_xattr_clusters_from_cache(inode
, block
,
567 trunc_len
-= alloc_size
;
571 ocfs2_schedule_truncate_log_flush(osb
, 1);
572 ocfs2_run_deallocs(osb
, &dealloc
);
577 static int ocfs2_xattr_value_truncate(struct inode
*inode
,
578 struct buffer_head
*root_bh
,
579 struct ocfs2_xattr_value_root
*xv
,
583 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, len
);
584 u32 old_clusters
= le32_to_cpu(xv
->xr_clusters
);
586 if (new_clusters
== old_clusters
)
589 if (new_clusters
> old_clusters
)
590 ret
= ocfs2_xattr_extend_allocation(inode
,
591 new_clusters
- old_clusters
,
594 ret
= ocfs2_xattr_shrink_size(inode
,
595 old_clusters
, new_clusters
,
601 static int ocfs2_xattr_list_entry(char *buffer
, size_t size
,
602 size_t *result
, const char *prefix
,
603 const char *name
, int name_len
)
605 char *p
= buffer
+ *result
;
606 int prefix_len
= strlen(prefix
);
607 int total_len
= prefix_len
+ name_len
+ 1;
609 *result
+= total_len
;
611 /* we are just looking for how big our buffer needs to be */
618 memcpy(p
, prefix
, prefix_len
);
619 memcpy(p
+ prefix_len
, name
, name_len
);
620 p
[prefix_len
+ name_len
] = '\0';
625 static int ocfs2_xattr_list_entries(struct inode
*inode
,
626 struct ocfs2_xattr_header
*header
,
627 char *buffer
, size_t buffer_size
)
631 const char *prefix
, *name
;
633 for (i
= 0 ; i
< le16_to_cpu(header
->xh_count
); i
++) {
634 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
635 type
= ocfs2_xattr_get_type(entry
);
636 prefix
= ocfs2_xattr_prefix(type
);
639 name
= (const char *)header
+
640 le16_to_cpu(entry
->xe_name_offset
);
642 ret
= ocfs2_xattr_list_entry(buffer
, buffer_size
,
643 &result
, prefix
, name
,
653 static int ocfs2_xattr_ibody_list(struct inode
*inode
,
654 struct ocfs2_dinode
*di
,
658 struct ocfs2_xattr_header
*header
= NULL
;
659 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
662 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
665 header
= (struct ocfs2_xattr_header
*)
666 ((void *)di
+ inode
->i_sb
->s_blocksize
-
667 le16_to_cpu(di
->i_xattr_inline_size
));
669 ret
= ocfs2_xattr_list_entries(inode
, header
, buffer
, buffer_size
);
674 static int ocfs2_xattr_block_list(struct inode
*inode
,
675 struct ocfs2_dinode
*di
,
679 struct buffer_head
*blk_bh
= NULL
;
680 struct ocfs2_xattr_block
*xb
;
683 if (!di
->i_xattr_loc
)
686 ret
= ocfs2_read_block(inode
, le64_to_cpu(di
->i_xattr_loc
), &blk_bh
);
692 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
693 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
698 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
699 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
700 ret
= ocfs2_xattr_list_entries(inode
, header
,
701 buffer
, buffer_size
);
703 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
704 ret
= ocfs2_xattr_tree_list_index_block(inode
, xt
,
705 buffer
, buffer_size
);
713 ssize_t
ocfs2_listxattr(struct dentry
*dentry
,
717 int ret
= 0, i_ret
= 0, b_ret
= 0;
718 struct buffer_head
*di_bh
= NULL
;
719 struct ocfs2_dinode
*di
= NULL
;
720 struct ocfs2_inode_info
*oi
= OCFS2_I(dentry
->d_inode
);
722 if (!ocfs2_supports_xattr(OCFS2_SB(dentry
->d_sb
)))
725 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
728 ret
= ocfs2_inode_lock(dentry
->d_inode
, &di_bh
, 0);
734 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
736 down_read(&oi
->ip_xattr_sem
);
737 i_ret
= ocfs2_xattr_ibody_list(dentry
->d_inode
, di
, buffer
, size
);
745 b_ret
= ocfs2_xattr_block_list(dentry
->d_inode
, di
,
750 up_read(&oi
->ip_xattr_sem
);
751 ocfs2_inode_unlock(dentry
->d_inode
, 0);
755 return i_ret
+ b_ret
;
758 static int ocfs2_xattr_find_entry(int name_index
,
760 struct ocfs2_xattr_search
*xs
)
762 struct ocfs2_xattr_entry
*entry
;
769 name_len
= strlen(name
);
771 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
772 cmp
= name_index
- ocfs2_xattr_get_type(entry
);
774 cmp
= name_len
- entry
->xe_name_len
;
776 cmp
= memcmp(name
, (xs
->base
+
777 le16_to_cpu(entry
->xe_name_offset
)),
785 return cmp
? -ENODATA
: 0;
788 static int ocfs2_xattr_get_value_outside(struct inode
*inode
,
789 struct ocfs2_xattr_value_root
*xv
,
793 u32 cpos
, p_cluster
, num_clusters
, bpc
, clusters
;
796 size_t cplen
, blocksize
;
797 struct buffer_head
*bh
= NULL
;
798 struct ocfs2_extent_list
*el
;
801 clusters
= le32_to_cpu(xv
->xr_clusters
);
802 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
803 blocksize
= inode
->i_sb
->s_blocksize
;
806 while (cpos
< clusters
) {
807 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
814 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
815 /* Copy ocfs2_xattr_value */
816 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
817 ret
= ocfs2_read_block(inode
, blkno
, &bh
);
823 cplen
= len
>= blocksize
? blocksize
: len
;
824 memcpy(buffer
, bh
->b_data
, cplen
);
833 cpos
+= num_clusters
;
839 static int ocfs2_xattr_ibody_get(struct inode
*inode
,
844 struct ocfs2_xattr_search
*xs
)
846 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
847 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
848 struct ocfs2_xattr_value_root
*xv
;
852 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
855 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
856 xs
->header
= (struct ocfs2_xattr_header
*)
857 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
858 xs
->base
= (void *)xs
->header
;
859 xs
->here
= xs
->header
->xh_entries
;
861 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
864 size
= le64_to_cpu(xs
->here
->xe_value_size
);
866 if (size
> buffer_size
)
868 if (ocfs2_xattr_is_local(xs
->here
)) {
869 memcpy(buffer
, (void *)xs
->base
+
870 le16_to_cpu(xs
->here
->xe_name_offset
) +
871 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
), size
);
873 xv
= (struct ocfs2_xattr_value_root
*)
874 (xs
->base
+ le16_to_cpu(
875 xs
->here
->xe_name_offset
) +
876 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
));
877 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
889 static int ocfs2_xattr_block_get(struct inode
*inode
,
894 struct ocfs2_xattr_search
*xs
)
896 struct ocfs2_xattr_block
*xb
;
897 struct ocfs2_xattr_value_root
*xv
;
899 int ret
= -ENODATA
, name_offset
, name_len
, block_off
, i
;
901 xs
->bucket
= ocfs2_xattr_bucket_new(inode
);
908 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, xs
);
919 xb
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
920 size
= le64_to_cpu(xs
->here
->xe_value_size
);
923 if (size
> buffer_size
)
926 name_offset
= le16_to_cpu(xs
->here
->xe_name_offset
);
927 name_len
= OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
);
928 i
= xs
->here
- xs
->header
->xh_entries
;
930 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
931 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
932 bucket_xh(xs
->bucket
),
936 xs
->base
= bucket_block(xs
->bucket
, block_off
);
938 if (ocfs2_xattr_is_local(xs
->here
)) {
939 memcpy(buffer
, (void *)xs
->base
+
940 name_offset
+ name_len
, size
);
942 xv
= (struct ocfs2_xattr_value_root
*)
943 (xs
->base
+ name_offset
+ name_len
);
944 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
954 ocfs2_xattr_bucket_free(xs
->bucket
);
956 brelse(xs
->xattr_bh
);
963 * Copy an extended attribute into the buffer provided.
964 * Buffer is NULL to compute the size of buffer required.
966 static int ocfs2_xattr_get(struct inode
*inode
,
973 struct ocfs2_dinode
*di
= NULL
;
974 struct buffer_head
*di_bh
= NULL
;
975 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
976 struct ocfs2_xattr_search xis
= {
977 .not_found
= -ENODATA
,
979 struct ocfs2_xattr_search xbs
= {
980 .not_found
= -ENODATA
,
983 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
986 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
989 ret
= ocfs2_inode_lock(inode
, &di_bh
, 0);
994 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
995 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
997 down_read(&oi
->ip_xattr_sem
);
998 ret
= ocfs2_xattr_ibody_get(inode
, name_index
, name
, buffer
,
1000 if (ret
== -ENODATA
&& di
->i_xattr_loc
)
1001 ret
= ocfs2_xattr_block_get(inode
, name_index
, name
, buffer
,
1003 up_read(&oi
->ip_xattr_sem
);
1004 ocfs2_inode_unlock(inode
, 0);
1011 static int __ocfs2_xattr_set_value_outside(struct inode
*inode
,
1012 struct ocfs2_xattr_value_root
*xv
,
1016 int ret
= 0, i
, cp_len
, credits
;
1017 u16 blocksize
= inode
->i_sb
->s_blocksize
;
1018 u32 p_cluster
, num_clusters
;
1019 u32 cpos
= 0, bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
1020 u32 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, value_len
);
1022 struct buffer_head
*bh
= NULL
;
1025 BUG_ON(clusters
> le32_to_cpu(xv
->xr_clusters
));
1027 credits
= clusters
* bpc
;
1028 handle
= ocfs2_start_trans(OCFS2_SB(inode
->i_sb
), credits
);
1029 if (IS_ERR(handle
)) {
1030 ret
= PTR_ERR(handle
);
1035 while (cpos
< clusters
) {
1036 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
1037 &num_clusters
, &xv
->xr_list
);
1043 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
1045 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
1046 ret
= ocfs2_read_block(inode
, blkno
, &bh
);
1052 ret
= ocfs2_journal_access(handle
,
1055 OCFS2_JOURNAL_ACCESS_WRITE
);
1061 cp_len
= value_len
> blocksize
? blocksize
: value_len
;
1062 memcpy(bh
->b_data
, value
, cp_len
);
1063 value_len
-= cp_len
;
1065 if (cp_len
< blocksize
)
1066 memset(bh
->b_data
+ cp_len
, 0,
1067 blocksize
- cp_len
);
1069 ret
= ocfs2_journal_dirty(handle
, bh
);
1078 * XXX: do we need to empty all the following
1079 * blocks in this cluster?
1084 cpos
+= num_clusters
;
1087 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1094 static int ocfs2_xattr_cleanup(struct inode
*inode
,
1095 struct ocfs2_xattr_info
*xi
,
1096 struct ocfs2_xattr_search
*xs
,
1099 handle_t
*handle
= NULL
;
1101 size_t name_len
= strlen(xi
->name
);
1102 void *val
= xs
->base
+ offs
;
1103 size_t size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1105 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1106 OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
1107 if (IS_ERR(handle
)) {
1108 ret
= PTR_ERR(handle
);
1112 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1113 OCFS2_JOURNAL_ACCESS_WRITE
);
1118 /* Decrease xattr count */
1119 le16_add_cpu(&xs
->header
->xh_count
, -1);
1120 /* Remove the xattr entry and tree root which has already be set*/
1121 memset((void *)xs
->here
, 0, sizeof(struct ocfs2_xattr_entry
));
1122 memset(val
, 0, size
);
1124 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1128 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1133 static int ocfs2_xattr_update_entry(struct inode
*inode
,
1134 struct ocfs2_xattr_info
*xi
,
1135 struct ocfs2_xattr_search
*xs
,
1138 handle_t
*handle
= NULL
;
1141 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1142 OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
1143 if (IS_ERR(handle
)) {
1144 ret
= PTR_ERR(handle
);
1148 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1149 OCFS2_JOURNAL_ACCESS_WRITE
);
1155 xs
->here
->xe_name_offset
= cpu_to_le16(offs
);
1156 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1157 if (xi
->value_len
<= OCFS2_XATTR_INLINE_SIZE
)
1158 ocfs2_xattr_set_local(xs
->here
, 1);
1160 ocfs2_xattr_set_local(xs
->here
, 0);
1161 ocfs2_xattr_hash_entry(inode
, xs
->header
, xs
->here
);
1163 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1167 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1173 * ocfs2_xattr_set_value_outside()
1175 * Set large size value in B tree.
1177 static int ocfs2_xattr_set_value_outside(struct inode
*inode
,
1178 struct ocfs2_xattr_info
*xi
,
1179 struct ocfs2_xattr_search
*xs
,
1182 size_t name_len
= strlen(xi
->name
);
1183 void *val
= xs
->base
+ offs
;
1184 struct ocfs2_xattr_value_root
*xv
= NULL
;
1185 size_t size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1188 memset(val
, 0, size
);
1189 memcpy(val
, xi
->name
, name_len
);
1190 xv
= (struct ocfs2_xattr_value_root
*)
1191 (val
+ OCFS2_XATTR_SIZE(name_len
));
1192 xv
->xr_clusters
= 0;
1193 xv
->xr_last_eb_blk
= 0;
1194 xv
->xr_list
.l_tree_depth
= 0;
1195 xv
->xr_list
.l_count
= cpu_to_le16(1);
1196 xv
->xr_list
.l_next_free_rec
= 0;
1198 ret
= ocfs2_xattr_value_truncate(inode
, xs
->xattr_bh
, xv
,
1204 ret
= __ocfs2_xattr_set_value_outside(inode
, xv
, xi
->value
,
1210 ret
= ocfs2_xattr_update_entry(inode
, xi
, xs
, offs
);
1218 * ocfs2_xattr_set_entry_local()
1220 * Set, replace or remove extended attribute in local.
1222 static void ocfs2_xattr_set_entry_local(struct inode
*inode
,
1223 struct ocfs2_xattr_info
*xi
,
1224 struct ocfs2_xattr_search
*xs
,
1225 struct ocfs2_xattr_entry
*last
,
1228 size_t name_len
= strlen(xi
->name
);
1231 if (xi
->value
&& xs
->not_found
) {
1232 /* Insert the new xattr entry. */
1233 le16_add_cpu(&xs
->header
->xh_count
, 1);
1234 ocfs2_xattr_set_type(last
, xi
->name_index
);
1235 ocfs2_xattr_set_local(last
, 1);
1236 last
->xe_name_len
= name_len
;
1242 first_val
= xs
->base
+ min_offs
;
1243 offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1244 val
= xs
->base
+ offs
;
1246 if (le64_to_cpu(xs
->here
->xe_value_size
) >
1247 OCFS2_XATTR_INLINE_SIZE
)
1248 size
= OCFS2_XATTR_SIZE(name_len
) +
1249 OCFS2_XATTR_ROOT_SIZE
;
1251 size
= OCFS2_XATTR_SIZE(name_len
) +
1252 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1254 if (xi
->value
&& size
== OCFS2_XATTR_SIZE(name_len
) +
1255 OCFS2_XATTR_SIZE(xi
->value_len
)) {
1256 /* The old and the new value have the
1257 same size. Just replace the value. */
1258 ocfs2_xattr_set_local(xs
->here
, 1);
1259 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1260 /* Clear value bytes. */
1261 memset(val
+ OCFS2_XATTR_SIZE(name_len
),
1263 OCFS2_XATTR_SIZE(xi
->value_len
));
1264 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
1269 /* Remove the old name+value. */
1270 memmove(first_val
+ size
, first_val
, val
- first_val
);
1271 memset(first_val
, 0, size
);
1272 xs
->here
->xe_name_hash
= 0;
1273 xs
->here
->xe_name_offset
= 0;
1274 ocfs2_xattr_set_local(xs
->here
, 1);
1275 xs
->here
->xe_value_size
= 0;
1279 /* Adjust all value offsets. */
1280 last
= xs
->header
->xh_entries
;
1281 for (i
= 0 ; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1282 size_t o
= le16_to_cpu(last
->xe_name_offset
);
1285 last
->xe_name_offset
= cpu_to_le16(o
+ size
);
1290 /* Remove the old entry. */
1292 memmove(xs
->here
, xs
->here
+ 1,
1293 (void *)last
- (void *)xs
->here
);
1294 memset(last
, 0, sizeof(struct ocfs2_xattr_entry
));
1295 le16_add_cpu(&xs
->header
->xh_count
, -1);
1299 /* Insert the new name+value. */
1300 size_t size
= OCFS2_XATTR_SIZE(name_len
) +
1301 OCFS2_XATTR_SIZE(xi
->value_len
);
1302 void *val
= xs
->base
+ min_offs
- size
;
1304 xs
->here
->xe_name_offset
= cpu_to_le16(min_offs
- size
);
1305 memset(val
, 0, size
);
1306 memcpy(val
, xi
->name
, name_len
);
1307 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
1310 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1311 ocfs2_xattr_set_local(xs
->here
, 1);
1312 ocfs2_xattr_hash_entry(inode
, xs
->header
, xs
->here
);
1319 * ocfs2_xattr_set_entry()
1321 * Set extended attribute entry into inode or block.
1323 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1324 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1325 * then set value in B tree with set_value_outside().
1327 static int ocfs2_xattr_set_entry(struct inode
*inode
,
1328 struct ocfs2_xattr_info
*xi
,
1329 struct ocfs2_xattr_search
*xs
,
1332 struct ocfs2_xattr_entry
*last
;
1333 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1334 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1335 size_t min_offs
= xs
->end
- xs
->base
, name_len
= strlen(xi
->name
);
1337 handle_t
*handle
= NULL
;
1339 struct ocfs2_xattr_info xi_l
= {
1340 .name_index
= xi
->name_index
,
1343 .value_len
= xi
->value_len
,
1346 /* Compute min_offs, last and free space. */
1347 last
= xs
->header
->xh_entries
;
1349 for (i
= 0 ; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1350 size_t offs
= le16_to_cpu(last
->xe_name_offset
);
1351 if (offs
< min_offs
)
1356 free
= min_offs
- ((void *)last
- xs
->base
) - sizeof(__u32
);
1360 if (!xs
->not_found
) {
1362 if (ocfs2_xattr_is_local(xs
->here
))
1363 size
= OCFS2_XATTR_SIZE(name_len
) +
1364 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1366 size
= OCFS2_XATTR_SIZE(name_len
) +
1367 OCFS2_XATTR_ROOT_SIZE
;
1368 free
+= (size
+ sizeof(struct ocfs2_xattr_entry
));
1370 /* Check free space in inode or block */
1371 if (xi
->value
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1372 if (free
< sizeof(struct ocfs2_xattr_entry
) +
1373 OCFS2_XATTR_SIZE(name_len
) +
1374 OCFS2_XATTR_ROOT_SIZE
) {
1378 size_l
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1379 xi_l
.value
= (void *)&def_xv
;
1380 xi_l
.value_len
= OCFS2_XATTR_ROOT_SIZE
;
1381 } else if (xi
->value
) {
1382 if (free
< sizeof(struct ocfs2_xattr_entry
) +
1383 OCFS2_XATTR_SIZE(name_len
) +
1384 OCFS2_XATTR_SIZE(xi
->value_len
)) {
1390 if (!xs
->not_found
) {
1391 /* For existing extended attribute */
1392 size_t size
= OCFS2_XATTR_SIZE(name_len
) +
1393 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1394 size_t offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1395 void *val
= xs
->base
+ offs
;
1397 if (ocfs2_xattr_is_local(xs
->here
) && size
== size_l
) {
1398 /* Replace existing local xattr with tree root */
1399 ret
= ocfs2_xattr_set_value_outside(inode
, xi
, xs
,
1404 } else if (!ocfs2_xattr_is_local(xs
->here
)) {
1405 /* For existing xattr which has value outside */
1406 struct ocfs2_xattr_value_root
*xv
= NULL
;
1407 xv
= (struct ocfs2_xattr_value_root
*)(val
+
1408 OCFS2_XATTR_SIZE(name_len
));
1410 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1412 * If new value need set outside also,
1413 * first truncate old value to new value,
1414 * then set new value with set_value_outside().
1416 ret
= ocfs2_xattr_value_truncate(inode
,
1425 ret
= __ocfs2_xattr_set_value_outside(inode
,
1434 ret
= ocfs2_xattr_update_entry(inode
,
1443 * If new value need set in local,
1444 * just trucate old value to zero.
1446 ret
= ocfs2_xattr_value_truncate(inode
,
1456 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1457 OCFS2_INODE_UPDATE_CREDITS
);
1458 if (IS_ERR(handle
)) {
1459 ret
= PTR_ERR(handle
);
1464 ret
= ocfs2_journal_access(handle
, inode
, xs
->inode_bh
,
1465 OCFS2_JOURNAL_ACCESS_WRITE
);
1471 if (!(flag
& OCFS2_INLINE_XATTR_FL
)) {
1472 /* set extended attribute in external block. */
1473 ret
= ocfs2_extend_trans(handle
,
1474 OCFS2_INODE_UPDATE_CREDITS
+
1475 OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
1480 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1481 OCFS2_JOURNAL_ACCESS_WRITE
);
1489 * Set value in local, include set tree root in local.
1490 * This is the first step for value size >INLINE_SIZE.
1492 ocfs2_xattr_set_entry_local(inode
, &xi_l
, xs
, last
, min_offs
);
1494 if (!(flag
& OCFS2_INLINE_XATTR_FL
)) {
1495 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1502 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) &&
1503 (flag
& OCFS2_INLINE_XATTR_FL
)) {
1504 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1505 unsigned int xattrsize
= osb
->s_xattr_inline_size
;
1508 * Adjust extent record count or inline data size
1509 * to reserve space for extended attribute.
1511 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1512 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
1513 le16_add_cpu(&idata
->id_count
, -xattrsize
);
1514 } else if (!(ocfs2_inode_is_fast_symlink(inode
))) {
1515 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
1516 le16_add_cpu(&el
->l_count
, -(xattrsize
/
1517 sizeof(struct ocfs2_extent_rec
)));
1519 di
->i_xattr_inline_size
= cpu_to_le16(xattrsize
);
1521 /* Update xattr flag */
1522 spin_lock(&oi
->ip_lock
);
1523 oi
->ip_dyn_features
|= flag
;
1524 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1525 spin_unlock(&oi
->ip_lock
);
1526 /* Update inode ctime */
1527 inode
->i_ctime
= CURRENT_TIME
;
1528 di
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
1529 di
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
1531 ret
= ocfs2_journal_dirty(handle
, xs
->inode_bh
);
1536 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1538 if (!ret
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1540 * Set value outside in B tree.
1541 * This is the second step for value size > INLINE_SIZE.
1543 size_t offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1544 ret
= ocfs2_xattr_set_value_outside(inode
, xi
, xs
, offs
);
1550 * If set value outside failed, we have to clean
1551 * the junk tree root we have already set in local.
1553 ret2
= ocfs2_xattr_cleanup(inode
, xi
, xs
, offs
);
1563 static int ocfs2_remove_value_outside(struct inode
*inode
,
1564 struct buffer_head
*bh
,
1565 struct ocfs2_xattr_header
*header
)
1569 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
1570 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
1572 if (!ocfs2_xattr_is_local(entry
)) {
1573 struct ocfs2_xattr_value_root
*xv
;
1576 val
= (void *)header
+
1577 le16_to_cpu(entry
->xe_name_offset
);
1578 xv
= (struct ocfs2_xattr_value_root
*)
1579 (val
+ OCFS2_XATTR_SIZE(entry
->xe_name_len
));
1580 ret
= ocfs2_xattr_value_truncate(inode
, bh
, xv
, 0);
1591 static int ocfs2_xattr_ibody_remove(struct inode
*inode
,
1592 struct buffer_head
*di_bh
)
1595 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1596 struct ocfs2_xattr_header
*header
;
1599 header
= (struct ocfs2_xattr_header
*)
1600 ((void *)di
+ inode
->i_sb
->s_blocksize
-
1601 le16_to_cpu(di
->i_xattr_inline_size
));
1603 ret
= ocfs2_remove_value_outside(inode
, di_bh
, header
);
1608 static int ocfs2_xattr_block_remove(struct inode
*inode
,
1609 struct buffer_head
*blk_bh
)
1611 struct ocfs2_xattr_block
*xb
;
1614 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1615 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1616 struct ocfs2_xattr_header
*header
= &(xb
->xb_attrs
.xb_header
);
1617 ret
= ocfs2_remove_value_outside(inode
, blk_bh
, header
);
1619 ret
= ocfs2_delete_xattr_index_block(inode
, blk_bh
);
1624 static int ocfs2_xattr_free_block(struct inode
*inode
,
1627 struct inode
*xb_alloc_inode
;
1628 struct buffer_head
*xb_alloc_bh
= NULL
;
1629 struct buffer_head
*blk_bh
= NULL
;
1630 struct ocfs2_xattr_block
*xb
;
1631 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1637 ret
= ocfs2_read_block(inode
, block
, &blk_bh
);
1643 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1644 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
1649 ret
= ocfs2_xattr_block_remove(inode
, blk_bh
);
1655 blk
= le64_to_cpu(xb
->xb_blkno
);
1656 bit
= le16_to_cpu(xb
->xb_suballoc_bit
);
1657 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1659 xb_alloc_inode
= ocfs2_get_system_file_inode(osb
,
1660 EXTENT_ALLOC_SYSTEM_INODE
,
1661 le16_to_cpu(xb
->xb_suballoc_slot
));
1662 if (!xb_alloc_inode
) {
1667 mutex_lock(&xb_alloc_inode
->i_mutex
);
1669 ret
= ocfs2_inode_lock(xb_alloc_inode
, &xb_alloc_bh
, 1);
1675 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
1676 if (IS_ERR(handle
)) {
1677 ret
= PTR_ERR(handle
);
1682 ret
= ocfs2_free_suballoc_bits(handle
, xb_alloc_inode
, xb_alloc_bh
,
1687 ocfs2_commit_trans(osb
, handle
);
1689 ocfs2_inode_unlock(xb_alloc_inode
, 1);
1690 brelse(xb_alloc_bh
);
1692 mutex_unlock(&xb_alloc_inode
->i_mutex
);
1693 iput(xb_alloc_inode
);
1700 * ocfs2_xattr_remove()
1702 * Free extended attribute resources associated with this inode.
1704 int ocfs2_xattr_remove(struct inode
*inode
, struct buffer_head
*di_bh
)
1706 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1707 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1711 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
1714 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1717 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
1718 ret
= ocfs2_xattr_ibody_remove(inode
, di_bh
);
1725 if (di
->i_xattr_loc
) {
1726 ret
= ocfs2_xattr_free_block(inode
,
1727 le64_to_cpu(di
->i_xattr_loc
));
1734 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1735 OCFS2_INODE_UPDATE_CREDITS
);
1736 if (IS_ERR(handle
)) {
1737 ret
= PTR_ERR(handle
);
1741 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
1742 OCFS2_JOURNAL_ACCESS_WRITE
);
1748 di
->i_xattr_loc
= 0;
1750 spin_lock(&oi
->ip_lock
);
1751 oi
->ip_dyn_features
&= ~(OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
);
1752 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1753 spin_unlock(&oi
->ip_lock
);
1755 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1759 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1764 static int ocfs2_xattr_has_space_inline(struct inode
*inode
,
1765 struct ocfs2_dinode
*di
)
1767 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1768 unsigned int xattrsize
= OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
;
1771 if (xattrsize
< OCFS2_MIN_XATTR_INLINE_SIZE
)
1774 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1775 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
1776 free
= le16_to_cpu(idata
->id_count
) - le64_to_cpu(di
->i_size
);
1777 } else if (ocfs2_inode_is_fast_symlink(inode
)) {
1778 free
= ocfs2_fast_symlink_chars(inode
->i_sb
) -
1779 le64_to_cpu(di
->i_size
);
1781 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
1782 free
= (le16_to_cpu(el
->l_count
) -
1783 le16_to_cpu(el
->l_next_free_rec
)) *
1784 sizeof(struct ocfs2_extent_rec
);
1786 if (free
>= xattrsize
)
1793 * ocfs2_xattr_ibody_find()
1795 * Find extended attribute in inode block and
1796 * fill search info into struct ocfs2_xattr_search.
1798 static int ocfs2_xattr_ibody_find(struct inode
*inode
,
1801 struct ocfs2_xattr_search
*xs
)
1803 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1804 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1808 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
1811 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
1812 down_read(&oi
->ip_alloc_sem
);
1813 has_space
= ocfs2_xattr_has_space_inline(inode
, di
);
1814 up_read(&oi
->ip_alloc_sem
);
1819 xs
->xattr_bh
= xs
->inode_bh
;
1820 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
1821 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)
1822 xs
->header
= (struct ocfs2_xattr_header
*)
1823 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
1825 xs
->header
= (struct ocfs2_xattr_header
*)
1826 (xs
->end
- OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
);
1827 xs
->base
= (void *)xs
->header
;
1828 xs
->here
= xs
->header
->xh_entries
;
1830 /* Find the named attribute. */
1831 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
1832 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1833 if (ret
&& ret
!= -ENODATA
)
1835 xs
->not_found
= ret
;
1842 * ocfs2_xattr_ibody_set()
1844 * Set, replace or remove an extended attribute into inode block.
1847 static int ocfs2_xattr_ibody_set(struct inode
*inode
,
1848 struct ocfs2_xattr_info
*xi
,
1849 struct ocfs2_xattr_search
*xs
)
1851 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1852 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1855 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
1858 down_write(&oi
->ip_alloc_sem
);
1859 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
1860 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
1866 ret
= ocfs2_xattr_set_entry(inode
, xi
, xs
,
1867 (OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
));
1869 up_write(&oi
->ip_alloc_sem
);
1875 * ocfs2_xattr_block_find()
1877 * Find extended attribute in external block and
1878 * fill search info into struct ocfs2_xattr_search.
1880 static int ocfs2_xattr_block_find(struct inode
*inode
,
1883 struct ocfs2_xattr_search
*xs
)
1885 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1886 struct buffer_head
*blk_bh
= NULL
;
1887 struct ocfs2_xattr_block
*xb
;
1890 if (!di
->i_xattr_loc
)
1893 ret
= ocfs2_read_block(inode
, le64_to_cpu(di
->i_xattr_loc
), &blk_bh
);
1899 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1900 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb
)) {
1905 xs
->xattr_bh
= blk_bh
;
1907 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1908 xs
->header
= &xb
->xb_attrs
.xb_header
;
1909 xs
->base
= (void *)xs
->header
;
1910 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
1911 xs
->here
= xs
->header
->xh_entries
;
1913 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1915 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
1919 if (ret
&& ret
!= -ENODATA
) {
1920 xs
->xattr_bh
= NULL
;
1923 xs
->not_found
= ret
;
1932 * ocfs2_xattr_block_set()
1934 * Set, replace or remove an extended attribute into external block.
1937 static int ocfs2_xattr_block_set(struct inode
*inode
,
1938 struct ocfs2_xattr_info
*xi
,
1939 struct ocfs2_xattr_search
*xs
)
1941 struct buffer_head
*new_bh
= NULL
;
1942 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1943 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1944 struct ocfs2_alloc_context
*meta_ac
= NULL
;
1945 handle_t
*handle
= NULL
;
1946 struct ocfs2_xattr_block
*xblk
= NULL
;
1947 u16 suballoc_bit_start
;
1952 if (!xs
->xattr_bh
) {
1954 * Alloc one external block for extended attribute
1957 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, &meta_ac
);
1962 handle
= ocfs2_start_trans(osb
,
1963 OCFS2_XATTR_BLOCK_CREATE_CREDITS
);
1964 if (IS_ERR(handle
)) {
1965 ret
= PTR_ERR(handle
);
1969 ret
= ocfs2_journal_access(handle
, inode
, xs
->inode_bh
,
1970 OCFS2_JOURNAL_ACCESS_CREATE
);
1976 ret
= ocfs2_claim_metadata(osb
, handle
, meta_ac
, 1,
1977 &suballoc_bit_start
, &num_got
,
1984 new_bh
= sb_getblk(inode
->i_sb
, first_blkno
);
1985 ocfs2_set_new_buffer_uptodate(inode
, new_bh
);
1987 ret
= ocfs2_journal_access(handle
, inode
, new_bh
,
1988 OCFS2_JOURNAL_ACCESS_CREATE
);
1994 /* Initialize ocfs2_xattr_block */
1995 xs
->xattr_bh
= new_bh
;
1996 xblk
= (struct ocfs2_xattr_block
*)new_bh
->b_data
;
1997 memset(xblk
, 0, inode
->i_sb
->s_blocksize
);
1998 strcpy((void *)xblk
, OCFS2_XATTR_BLOCK_SIGNATURE
);
1999 xblk
->xb_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
2000 xblk
->xb_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
2001 xblk
->xb_fs_generation
= cpu_to_le32(osb
->fs_generation
);
2002 xblk
->xb_blkno
= cpu_to_le64(first_blkno
);
2004 xs
->header
= &xblk
->xb_attrs
.xb_header
;
2005 xs
->base
= (void *)xs
->header
;
2006 xs
->end
= (void *)xblk
+ inode
->i_sb
->s_blocksize
;
2007 xs
->here
= xs
->header
->xh_entries
;
2010 ret
= ocfs2_journal_dirty(handle
, new_bh
);
2015 di
->i_xattr_loc
= cpu_to_le64(first_blkno
);
2016 ret
= ocfs2_journal_dirty(handle
, xs
->inode_bh
);
2020 ocfs2_commit_trans(osb
, handle
);
2023 ocfs2_free_alloc_context(meta_ac
);
2027 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
2029 if (!(le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
2030 /* Set extended attribute into external block */
2031 ret
= ocfs2_xattr_set_entry(inode
, xi
, xs
, OCFS2_HAS_XATTR_FL
);
2032 if (!ret
|| ret
!= -ENOSPC
)
2035 ret
= ocfs2_xattr_create_index_block(inode
, xs
);
2040 ret
= ocfs2_xattr_set_entry_index_block(inode
, xi
, xs
);
2050 * Set, replace or remove an extended attribute for this inode.
2051 * value is NULL to remove an existing extended attribute, else either
2052 * create or replace an extended attribute.
2054 int ocfs2_xattr_set(struct inode
*inode
,
2061 struct buffer_head
*di_bh
= NULL
;
2062 struct ocfs2_dinode
*di
;
2065 struct ocfs2_xattr_info xi
= {
2066 .name_index
= name_index
,
2069 .value_len
= value_len
,
2072 struct ocfs2_xattr_search xis
= {
2073 .not_found
= -ENODATA
,
2076 struct ocfs2_xattr_search xbs
= {
2077 .not_found
= -ENODATA
,
2080 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
2084 * Only xbs will be used on indexed trees. xis doesn't need a
2087 xbs
.bucket
= ocfs2_xattr_bucket_new(inode
);
2089 mlog_errno(-ENOMEM
);
2093 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
2096 goto cleanup_nolock
;
2098 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
2099 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2101 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2103 * Scan inode and external block to find the same name
2104 * extended attribute and collect search infomation.
2106 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
2109 if (xis
.not_found
) {
2110 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
2115 if (xis
.not_found
&& xbs
.not_found
) {
2117 if (flags
& XATTR_REPLACE
)
2124 if (flags
& XATTR_CREATE
)
2129 /* Remove existing extended attribute */
2131 ret
= ocfs2_xattr_ibody_set(inode
, &xi
, &xis
);
2132 else if (!xbs
.not_found
)
2133 ret
= ocfs2_xattr_block_set(inode
, &xi
, &xbs
);
2135 /* We always try to set extended attribute into inode first*/
2136 ret
= ocfs2_xattr_ibody_set(inode
, &xi
, &xis
);
2137 if (!ret
&& !xbs
.not_found
) {
2139 * If succeed and that extended attribute existing in
2140 * external block, then we will remove it.
2144 ret
= ocfs2_xattr_block_set(inode
, &xi
, &xbs
);
2145 } else if (ret
== -ENOSPC
) {
2146 if (di
->i_xattr_loc
&& !xbs
.xattr_bh
) {
2147 ret
= ocfs2_xattr_block_find(inode
, name_index
,
2153 * If no space in inode, we will set extended attribute
2154 * into external block.
2156 ret
= ocfs2_xattr_block_set(inode
, &xi
, &xbs
);
2159 if (!xis
.not_found
) {
2161 * If succeed and that extended attribute
2162 * existing in inode, we will remove it.
2166 ret
= ocfs2_xattr_ibody_set(inode
, &xi
, &xis
);
2171 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2172 ocfs2_inode_unlock(inode
, 1);
2175 brelse(xbs
.xattr_bh
);
2176 ocfs2_xattr_bucket_free(xbs
.bucket
);
2182 * Find the xattr extent rec which may contains name_hash.
2183 * e_cpos will be the first name hash of the xattr rec.
2184 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2186 static int ocfs2_xattr_get_rec(struct inode
*inode
,
2191 struct ocfs2_extent_list
*el
)
2194 struct buffer_head
*eb_bh
= NULL
;
2195 struct ocfs2_extent_block
*eb
;
2196 struct ocfs2_extent_rec
*rec
= NULL
;
2199 if (el
->l_tree_depth
) {
2200 ret
= ocfs2_find_leaf(inode
, el
, name_hash
, &eb_bh
);
2206 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
2209 if (el
->l_tree_depth
) {
2210 ocfs2_error(inode
->i_sb
,
2211 "Inode %lu has non zero tree depth in "
2212 "xattr tree block %llu\n", inode
->i_ino
,
2213 (unsigned long long)eb_bh
->b_blocknr
);
2219 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
2220 rec
= &el
->l_recs
[i
];
2222 if (le32_to_cpu(rec
->e_cpos
) <= name_hash
) {
2223 e_blkno
= le64_to_cpu(rec
->e_blkno
);
2229 ocfs2_error(inode
->i_sb
, "Inode %lu has bad extent "
2230 "record (%u, %u, 0) in xattr", inode
->i_ino
,
2231 le32_to_cpu(rec
->e_cpos
),
2232 ocfs2_rec_clusters(el
, rec
));
2237 *p_blkno
= le64_to_cpu(rec
->e_blkno
);
2238 *num_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
2240 *e_cpos
= le32_to_cpu(rec
->e_cpos
);
2246 typedef int (xattr_bucket_func
)(struct inode
*inode
,
2247 struct ocfs2_xattr_bucket
*bucket
,
2250 static int ocfs2_find_xe_in_bucket(struct inode
*inode
,
2251 struct ocfs2_xattr_bucket
*bucket
,
2258 int i
, ret
= 0, cmp
= 1, block_off
, new_offset
;
2259 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
2260 size_t name_len
= strlen(name
);
2261 struct ocfs2_xattr_entry
*xe
= NULL
;
2265 * We don't use binary search in the bucket because there
2266 * may be multiple entries with the same name hash.
2268 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
2269 xe
= &xh
->xh_entries
[i
];
2271 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
))
2273 else if (name_hash
< le32_to_cpu(xe
->xe_name_hash
))
2276 cmp
= name_index
- ocfs2_xattr_get_type(xe
);
2278 cmp
= name_len
- xe
->xe_name_len
;
2282 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
2292 xe_name
= bucket_block(bucket
, block_off
) + new_offset
;
2293 if (!memcmp(name
, xe_name
, name_len
)) {
2305 * Find the specified xattr entry in a series of buckets.
2306 * This series start from p_blkno and last for num_clusters.
2307 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2308 * the num of the valid buckets.
2310 * Return the buffer_head this xattr should reside in. And if the xattr's
2311 * hash is in the gap of 2 buckets, return the lower bucket.
2313 static int ocfs2_xattr_bucket_find(struct inode
*inode
,
2320 struct ocfs2_xattr_search
*xs
)
2323 struct ocfs2_xattr_header
*xh
= NULL
;
2324 struct ocfs2_xattr_entry
*xe
= NULL
;
2326 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2327 int low_bucket
= 0, bucket
, high_bucket
;
2328 struct ocfs2_xattr_bucket
*search
;
2330 u64 blkno
, lower_blkno
= 0;
2332 search
= ocfs2_xattr_bucket_new(inode
);
2339 ret
= ocfs2_read_xattr_bucket(search
, p_blkno
);
2345 xh
= bucket_xh(search
);
2346 high_bucket
= le16_to_cpu(xh
->xh_num_buckets
) - 1;
2347 while (low_bucket
<= high_bucket
) {
2348 ocfs2_xattr_bucket_relse(search
);
2350 bucket
= (low_bucket
+ high_bucket
) / 2;
2351 blkno
= p_blkno
+ bucket
* blk_per_bucket
;
2352 ret
= ocfs2_read_xattr_bucket(search
, blkno
);
2358 xh
= bucket_xh(search
);
2359 xe
= &xh
->xh_entries
[0];
2360 if (name_hash
< le32_to_cpu(xe
->xe_name_hash
)) {
2361 high_bucket
= bucket
- 1;
2366 * Check whether the hash of the last entry in our
2367 * bucket is larger than the search one. for an empty
2368 * bucket, the last one is also the first one.
2371 xe
= &xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1];
2373 last_hash
= le32_to_cpu(xe
->xe_name_hash
);
2375 /* record lower_blkno which may be the insert place. */
2376 lower_blkno
= blkno
;
2378 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
)) {
2379 low_bucket
= bucket
+ 1;
2383 /* the searched xattr should reside in this bucket if exists. */
2384 ret
= ocfs2_find_xe_in_bucket(inode
, search
,
2385 name_index
, name
, name_hash
,
2395 * Record the bucket we have found.
2396 * When the xattr's hash value is in the gap of 2 buckets, we will
2397 * always set it to the previous bucket.
2400 lower_blkno
= p_blkno
;
2402 /* This should be in cache - we just read it during the search */
2403 ret
= ocfs2_read_xattr_bucket(xs
->bucket
, lower_blkno
);
2409 xs
->header
= bucket_xh(xs
->bucket
);
2410 xs
->base
= bucket_block(xs
->bucket
, 0);
2411 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
2414 xs
->here
= &xs
->header
->xh_entries
[index
];
2415 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name
,
2416 (unsigned long long)bucket_blkno(xs
->bucket
), index
);
2421 ocfs2_xattr_bucket_free(search
);
2425 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
2426 struct buffer_head
*root_bh
,
2429 struct ocfs2_xattr_search
*xs
)
2432 struct ocfs2_xattr_block
*xb
=
2433 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
2434 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
2435 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
2437 u32 first_hash
, num_clusters
= 0;
2438 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
2440 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
2443 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2444 name
, name_hash
, name_index
);
2446 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &first_hash
,
2453 BUG_ON(p_blkno
== 0 || num_clusters
== 0 || first_hash
> name_hash
);
2455 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2456 "in the rec is %u\n", num_clusters
, (unsigned long long)p_blkno
,
2459 ret
= ocfs2_xattr_bucket_find(inode
, name_index
, name
, name_hash
,
2460 p_blkno
, first_hash
, num_clusters
, xs
);
2466 static int ocfs2_iterate_xattr_buckets(struct inode
*inode
,
2469 xattr_bucket_func
*func
,
2473 u32 bpc
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
2474 u32 num_buckets
= clusters
* bpc
;
2475 struct ocfs2_xattr_bucket
*bucket
;
2477 bucket
= ocfs2_xattr_bucket_new(inode
);
2479 mlog_errno(-ENOMEM
);
2483 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2484 clusters
, (unsigned long long)blkno
);
2486 for (i
= 0; i
< num_buckets
; i
++, blkno
+= bucket
->bu_blocks
) {
2487 ret
= ocfs2_read_xattr_bucket(bucket
, blkno
);
2494 * The real bucket num in this series of blocks is stored
2495 * in the 1st bucket.
2498 num_buckets
= le16_to_cpu(bucket_xh(bucket
)->xh_num_buckets
);
2500 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
2501 (unsigned long long)blkno
,
2502 le32_to_cpu(bucket_xh(bucket
)->xh_entries
[0].xe_name_hash
));
2504 ret
= func(inode
, bucket
, para
);
2507 /* Fall through to bucket_relse() */
2510 ocfs2_xattr_bucket_relse(bucket
);
2515 ocfs2_xattr_bucket_free(bucket
);
2519 struct ocfs2_xattr_tree_list
{
2525 static int ocfs2_xattr_bucket_get_name_value(struct inode
*inode
,
2526 struct ocfs2_xattr_header
*xh
,
2533 if (index
< 0 || index
>= le16_to_cpu(xh
->xh_count
))
2536 name_offset
= le16_to_cpu(xh
->xh_entries
[index
].xe_name_offset
);
2538 *block_off
= name_offset
>> inode
->i_sb
->s_blocksize_bits
;
2539 *new_offset
= name_offset
% inode
->i_sb
->s_blocksize
;
2544 static int ocfs2_list_xattr_bucket(struct inode
*inode
,
2545 struct ocfs2_xattr_bucket
*bucket
,
2549 struct ocfs2_xattr_tree_list
*xl
= (struct ocfs2_xattr_tree_list
*)para
;
2550 int i
, block_off
, new_offset
;
2551 const char *prefix
, *name
;
2553 for (i
= 0 ; i
< le16_to_cpu(bucket_xh(bucket
)->xh_count
); i
++) {
2554 struct ocfs2_xattr_entry
*entry
= &bucket_xh(bucket
)->xh_entries
[i
];
2555 type
= ocfs2_xattr_get_type(entry
);
2556 prefix
= ocfs2_xattr_prefix(type
);
2559 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
2567 name
= (const char *)bucket_block(bucket
, block_off
) +
2569 ret
= ocfs2_xattr_list_entry(xl
->buffer
,
2573 entry
->xe_name_len
);
2582 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
2583 struct ocfs2_xattr_tree_root
*xt
,
2587 struct ocfs2_extent_list
*el
= &xt
->xt_list
;
2589 u32 name_hash
= UINT_MAX
, e_cpos
= 0, num_clusters
= 0;
2591 struct ocfs2_xattr_tree_list xl
= {
2593 .buffer_size
= buffer_size
,
2597 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
2600 while (name_hash
> 0) {
2601 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
2602 &e_cpos
, &num_clusters
, el
);
2608 ret
= ocfs2_iterate_xattr_buckets(inode
, p_blkno
, num_clusters
,
2609 ocfs2_list_xattr_bucket
,
2619 name_hash
= e_cpos
- 1;
2627 static int cmp_xe(const void *a
, const void *b
)
2629 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
2630 u32 l_hash
= le32_to_cpu(l
->xe_name_hash
);
2631 u32 r_hash
= le32_to_cpu(r
->xe_name_hash
);
2633 if (l_hash
> r_hash
)
2635 if (l_hash
< r_hash
)
2640 static void swap_xe(void *a
, void *b
, int size
)
2642 struct ocfs2_xattr_entry
*l
= a
, *r
= b
, tmp
;
2645 memcpy(l
, r
, sizeof(struct ocfs2_xattr_entry
));
2646 memcpy(r
, &tmp
, sizeof(struct ocfs2_xattr_entry
));
2650 * When the ocfs2_xattr_block is filled up, new bucket will be created
2651 * and all the xattr entries will be moved to the new bucket.
2652 * Note: we need to sort the entries since they are not saved in order
2653 * in the ocfs2_xattr_block.
2655 static void ocfs2_cp_xattr_block_to_bucket(struct inode
*inode
,
2656 struct buffer_head
*xb_bh
,
2657 struct buffer_head
*xh_bh
,
2658 struct buffer_head
*data_bh
)
2660 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
2661 u16 offset
, size
, off_change
;
2662 struct ocfs2_xattr_entry
*xe
;
2663 struct ocfs2_xattr_block
*xb
=
2664 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
2665 struct ocfs2_xattr_header
*xb_xh
= &xb
->xb_attrs
.xb_header
;
2666 struct ocfs2_xattr_header
*xh
=
2667 (struct ocfs2_xattr_header
*)xh_bh
->b_data
;
2668 u16 count
= le16_to_cpu(xb_xh
->xh_count
);
2669 char *target
= xh_bh
->b_data
, *src
= xb_bh
->b_data
;
2671 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2672 (unsigned long long)xb_bh
->b_blocknr
,
2673 (unsigned long long)xh_bh
->b_blocknr
);
2675 memset(xh_bh
->b_data
, 0, blocksize
);
2677 memset(data_bh
->b_data
, 0, blocksize
);
2679 * Since the xe_name_offset is based on ocfs2_xattr_header,
2680 * there is a offset change corresponding to the change of
2681 * ocfs2_xattr_header's position.
2683 off_change
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
2684 xe
= &xb_xh
->xh_entries
[count
- 1];
2685 offset
= le16_to_cpu(xe
->xe_name_offset
) + off_change
;
2686 size
= blocksize
- offset
;
2688 /* copy all the names and values. */
2690 target
= data_bh
->b_data
;
2691 memcpy(target
+ offset
, src
+ offset
, size
);
2693 /* Init new header now. */
2694 xh
->xh_count
= xb_xh
->xh_count
;
2695 xh
->xh_num_buckets
= cpu_to_le16(1);
2696 xh
->xh_name_value_len
= cpu_to_le16(size
);
2697 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
- size
);
2699 /* copy all the entries. */
2700 target
= xh_bh
->b_data
;
2701 offset
= offsetof(struct ocfs2_xattr_header
, xh_entries
);
2702 size
= count
* sizeof(struct ocfs2_xattr_entry
);
2703 memcpy(target
+ offset
, (char *)xb_xh
+ offset
, size
);
2705 /* Change the xe offset for all the xe because of the move. */
2706 off_change
= OCFS2_XATTR_BUCKET_SIZE
- blocksize
+
2707 offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
2708 for (i
= 0; i
< count
; i
++)
2709 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
, off_change
);
2711 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2712 offset
, size
, off_change
);
2714 sort(target
+ offset
, count
, sizeof(struct ocfs2_xattr_entry
),
2719 * After we move xattr from block to index btree, we have to
2720 * update ocfs2_xattr_search to the new xe and base.
2722 * When the entry is in xattr block, xattr_bh indicates the storage place.
2723 * While if the entry is in index b-tree, "bucket" indicates the
2724 * real place of the xattr.
2726 static int ocfs2_xattr_update_xattr_search(struct inode
*inode
,
2727 struct ocfs2_xattr_search
*xs
,
2728 struct buffer_head
*old_bh
,
2729 struct buffer_head
*new_bh
)
2732 char *buf
= old_bh
->b_data
;
2733 struct ocfs2_xattr_block
*old_xb
= (struct ocfs2_xattr_block
*)buf
;
2734 struct ocfs2_xattr_header
*old_xh
= &old_xb
->xb_attrs
.xb_header
;
2735 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
2736 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2738 xs
->bucket
->bu_bhs
[0] = new_bh
;
2740 xs
->header
= bucket_xh(xs
->bucket
);
2742 xs
->base
= new_bh
->b_data
;
2743 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
2745 if (!xs
->not_found
) {
2746 if (OCFS2_XATTR_BUCKET_SIZE
!= blocksize
) {
2747 ret
= ocfs2_read_blocks(inode
,
2748 bucket_blkno(xs
->bucket
) + 1,
2749 blk_per_bucket
- 1, &xs
->bucket
->bu_bhs
[1],
2757 i
= xs
->here
- old_xh
->xh_entries
;
2758 xs
->here
= &xs
->header
->xh_entries
[i
];
2764 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
2765 struct ocfs2_xattr_search
*xs
)
2767 int ret
, credits
= OCFS2_SUBALLOC_ALLOC
;
2771 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2772 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2773 struct ocfs2_alloc_context
*data_ac
;
2774 struct buffer_head
*xh_bh
= NULL
, *data_bh
= NULL
;
2775 struct buffer_head
*xb_bh
= xs
->xattr_bh
;
2776 struct ocfs2_xattr_block
*xb
=
2777 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
2778 struct ocfs2_xattr_tree_root
*xr
;
2779 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
2780 u16 bpb
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2782 mlog(0, "create xattr index block for %llu\n",
2783 (unsigned long long)xb_bh
->b_blocknr
);
2785 BUG_ON(xb_flags
& OCFS2_XATTR_INDEXED
);
2787 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
2795 * We can use this lock for now, and maybe move to a dedicated mutex
2796 * if performance becomes a problem later.
2798 down_write(&oi
->ip_alloc_sem
);
2801 * 3 more credits, one for xattr block update, one for the 1st block
2802 * of the new xattr bucket and one for the value/data.
2805 handle
= ocfs2_start_trans(osb
, credits
);
2806 if (IS_ERR(handle
)) {
2807 ret
= PTR_ERR(handle
);
2812 ret
= ocfs2_journal_access(handle
, inode
, xb_bh
,
2813 OCFS2_JOURNAL_ACCESS_WRITE
);
2819 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
, &len
);
2826 * The bucket may spread in many blocks, and
2827 * we will only touch the 1st block and the last block
2828 * in the whole bucket(one for entry and one for data).
2830 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
2832 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
2833 (unsigned long long)blkno
);
2835 xh_bh
= sb_getblk(inode
->i_sb
, blkno
);
2842 ocfs2_set_new_buffer_uptodate(inode
, xh_bh
);
2844 ret
= ocfs2_journal_access(handle
, inode
, xh_bh
,
2845 OCFS2_JOURNAL_ACCESS_CREATE
);
2852 data_bh
= sb_getblk(inode
->i_sb
, blkno
+ bpb
- 1);
2859 ocfs2_set_new_buffer_uptodate(inode
, data_bh
);
2861 ret
= ocfs2_journal_access(handle
, inode
, data_bh
,
2862 OCFS2_JOURNAL_ACCESS_CREATE
);
2869 ocfs2_cp_xattr_block_to_bucket(inode
, xb_bh
, xh_bh
, data_bh
);
2871 ocfs2_journal_dirty(handle
, xh_bh
);
2873 ocfs2_journal_dirty(handle
, data_bh
);
2875 ret
= ocfs2_xattr_update_xattr_search(inode
, xs
, xb_bh
, xh_bh
);
2881 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2882 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
2883 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
2885 xr
= &xb
->xb_attrs
.xb_root
;
2886 xr
->xt_clusters
= cpu_to_le32(1);
2887 xr
->xt_last_eb_blk
= 0;
2888 xr
->xt_list
.l_tree_depth
= 0;
2889 xr
->xt_list
.l_count
= cpu_to_le16(ocfs2_xattr_recs_per_xb(inode
->i_sb
));
2890 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
2892 xr
->xt_list
.l_recs
[0].e_cpos
= 0;
2893 xr
->xt_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
2894 xr
->xt_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
2896 xb
->xb_flags
= cpu_to_le16(xb_flags
| OCFS2_XATTR_INDEXED
);
2898 ret
= ocfs2_journal_dirty(handle
, xb_bh
);
2905 ocfs2_commit_trans(osb
, handle
);
2908 up_write(&oi
->ip_alloc_sem
);
2912 ocfs2_free_alloc_context(data_ac
);
2920 static int cmp_xe_offset(const void *a
, const void *b
)
2922 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
2923 u32 l_name_offset
= le16_to_cpu(l
->xe_name_offset
);
2924 u32 r_name_offset
= le16_to_cpu(r
->xe_name_offset
);
2926 if (l_name_offset
< r_name_offset
)
2928 if (l_name_offset
> r_name_offset
)
2934 * defrag a xattr bucket if we find that the bucket has some
2935 * holes beteen name/value pairs.
2936 * We will move all the name/value pairs to the end of the bucket
2937 * so that we can spare some space for insertion.
2939 static int ocfs2_defrag_xattr_bucket(struct inode
*inode
,
2940 struct ocfs2_xattr_bucket
*bucket
)
2943 size_t end
, offset
, len
, value_len
;
2944 struct ocfs2_xattr_header
*xh
;
2945 char *entries
, *buf
, *bucket_buf
= NULL
;
2946 u64 blkno
= bucket_blkno(bucket
);
2947 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2949 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2951 struct buffer_head
**bhs
;
2952 struct ocfs2_xattr_entry
*xe
;
2954 bhs
= kzalloc(sizeof(struct buffer_head
*) * blk_per_bucket
,
2959 ret
= ocfs2_read_blocks(inode
, blkno
, blk_per_bucket
, bhs
, 0);
2964 * In order to make the operation more efficient and generic,
2965 * we copy all the blocks into a contiguous memory and do the
2966 * defragment there, so if anything is error, we will not touch
2969 bucket_buf
= kmalloc(OCFS2_XATTR_BUCKET_SIZE
, GFP_NOFS
);
2976 for (i
= 0; i
< blk_per_bucket
; i
++, buf
+= blocksize
)
2977 memcpy(buf
, bhs
[i
]->b_data
, blocksize
);
2979 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)), blk_per_bucket
);
2980 if (IS_ERR(handle
)) {
2981 ret
= PTR_ERR(handle
);
2987 for (i
= 0; i
< blk_per_bucket
; i
++) {
2988 ret
= ocfs2_journal_access(handle
, inode
, bhs
[i
],
2989 OCFS2_JOURNAL_ACCESS_WRITE
);
2996 xh
= (struct ocfs2_xattr_header
*)bucket_buf
;
2997 entries
= (char *)xh
->xh_entries
;
2998 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
3000 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3001 "xh_free_start = %u, xh_name_value_len = %u.\n",
3002 (unsigned long long)blkno
, le16_to_cpu(xh
->xh_count
),
3003 xh_free_start
, le16_to_cpu(xh
->xh_name_value_len
));
3006 * sort all the entries by their offset.
3007 * the largest will be the first, so that we can
3008 * move them to the end one by one.
3010 sort(entries
, le16_to_cpu(xh
->xh_count
),
3011 sizeof(struct ocfs2_xattr_entry
),
3012 cmp_xe_offset
, swap_xe
);
3014 /* Move all name/values to the end of the bucket. */
3015 xe
= xh
->xh_entries
;
3016 end
= OCFS2_XATTR_BUCKET_SIZE
;
3017 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, xe
++) {
3018 offset
= le16_to_cpu(xe
->xe_name_offset
);
3019 if (ocfs2_xattr_is_local(xe
))
3020 value_len
= OCFS2_XATTR_SIZE(
3021 le64_to_cpu(xe
->xe_value_size
));
3023 value_len
= OCFS2_XATTR_ROOT_SIZE
;
3024 len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
) + value_len
;
3027 * We must make sure that the name/value pair
3028 * exist in the same block. So adjust end to
3029 * the previous block end if needed.
3031 if (((end
- len
) / blocksize
!=
3032 (end
- 1) / blocksize
))
3033 end
= end
- end
% blocksize
;
3035 if (end
> offset
+ len
) {
3036 memmove(bucket_buf
+ end
- len
,
3037 bucket_buf
+ offset
, len
);
3038 xe
->xe_name_offset
= cpu_to_le16(end
- len
);
3041 mlog_bug_on_msg(end
< offset
+ len
, "Defrag check failed for "
3042 "bucket %llu\n", (unsigned long long)blkno
);
3047 mlog_bug_on_msg(xh_free_start
> end
, "Defrag check failed for "
3048 "bucket %llu\n", (unsigned long long)blkno
);
3050 if (xh_free_start
== end
)
3053 memset(bucket_buf
+ xh_free_start
, 0, end
- xh_free_start
);
3054 xh
->xh_free_start
= cpu_to_le16(end
);
3056 /* sort the entries by their name_hash. */
3057 sort(entries
, le16_to_cpu(xh
->xh_count
),
3058 sizeof(struct ocfs2_xattr_entry
),
3062 for (i
= 0; i
< blk_per_bucket
; i
++, buf
+= blocksize
) {
3063 memcpy(bhs
[i
]->b_data
, buf
, blocksize
);
3064 ocfs2_journal_dirty(handle
, bhs
[i
]);
3068 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
3072 for (i
= 0; i
< blk_per_bucket
; i
++)
3082 * Move half nums of the xattr bucket in the previous cluster to this new
3083 * cluster. We only touch the last cluster of the previous extend record.
3085 * first_bh is the first buffer_head of a series of bucket in the same
3086 * extent rec and header_bh is the header of one bucket in this cluster.
3087 * They will be updated if we move the data header_bh contains to the new
3088 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3090 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode
*inode
,
3092 struct buffer_head
**first_bh
,
3093 struct buffer_head
**header_bh
,
3099 int i
, ret
, credits
;
3100 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3101 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3102 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
3103 int blocksize
= inode
->i_sb
->s_blocksize
;
3104 struct buffer_head
*old_bh
, *new_bh
, *prev_bh
, *new_first_bh
= NULL
;
3105 struct ocfs2_xattr_header
*new_xh
;
3106 struct ocfs2_xattr_header
*xh
=
3107 (struct ocfs2_xattr_header
*)((*first_bh
)->b_data
);
3109 BUG_ON(le16_to_cpu(xh
->xh_num_buckets
) < num_buckets
);
3110 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
== osb
->s_clustersize
);
3112 prev_bh
= *first_bh
;
3114 xh
= (struct ocfs2_xattr_header
*)prev_bh
->b_data
;
3116 prev_blkno
+= (num_clusters
- 1) * bpc
+ bpc
/ 2;
3118 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3119 (unsigned long long)prev_blkno
, (unsigned long long)new_blkno
);
3122 * We need to update the 1st half of the new cluster and
3123 * 1 more for the update of the 1st bucket of the previous
3126 credits
= bpc
/ 2 + 1;
3127 ret
= ocfs2_extend_trans(handle
, credits
);
3133 ret
= ocfs2_journal_access(handle
, inode
, prev_bh
,
3134 OCFS2_JOURNAL_ACCESS_WRITE
);
3140 for (i
= 0; i
< bpc
/ 2; i
++, prev_blkno
++, new_blkno
++) {
3141 old_bh
= new_bh
= NULL
;
3142 new_bh
= sb_getblk(inode
->i_sb
, new_blkno
);
3149 ocfs2_set_new_buffer_uptodate(inode
, new_bh
);
3151 ret
= ocfs2_journal_access(handle
, inode
, new_bh
,
3152 OCFS2_JOURNAL_ACCESS_CREATE
);
3159 ret
= ocfs2_read_block(inode
, prev_blkno
, &old_bh
);
3166 memcpy(new_bh
->b_data
, old_bh
->b_data
, blocksize
);
3169 new_xh
= (struct ocfs2_xattr_header
*)new_bh
->b_data
;
3170 new_xh
->xh_num_buckets
= cpu_to_le16(num_buckets
/ 2);
3173 *first_hash
= le32_to_cpu(
3174 new_xh
->xh_entries
[0].xe_name_hash
);
3175 new_first_bh
= new_bh
;
3176 get_bh(new_first_bh
);
3179 ocfs2_journal_dirty(handle
, new_bh
);
3181 if (*header_bh
== old_bh
) {
3183 *header_bh
= new_bh
;
3187 *first_bh
= new_first_bh
;
3194 le16_add_cpu(&xh
->xh_num_buckets
, -(num_buckets
/ 2));
3196 ocfs2_journal_dirty(handle
, prev_bh
);
3199 brelse(new_first_bh
);
3204 * Find the suitable pos when we divide a bucket into 2.
3205 * We have to make sure the xattrs with the same hash value exist
3206 * in the same bucket.
3208 * If this ocfs2_xattr_header covers more than one hash value, find a
3209 * place where the hash value changes. Try to find the most even split.
3210 * The most common case is that all entries have different hash values,
3211 * and the first check we make will find a place to split.
3213 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header
*xh
)
3215 struct ocfs2_xattr_entry
*entries
= xh
->xh_entries
;
3216 int count
= le16_to_cpu(xh
->xh_count
);
3217 int delta
, middle
= count
/ 2;
3220 * We start at the middle. Each step gets farther away in both
3221 * directions. We therefore hit the change in hash value
3222 * nearest to the middle. Note that this loop does not execute for
3225 for (delta
= 0; delta
< middle
; delta
++) {
3226 /* Let's check delta earlier than middle */
3227 if (cmp_xe(&entries
[middle
- delta
- 1],
3228 &entries
[middle
- delta
]))
3229 return middle
- delta
;
3231 /* For even counts, don't walk off the end */
3232 if ((middle
+ delta
+ 1) == count
)
3235 /* Now try delta past middle */
3236 if (cmp_xe(&entries
[middle
+ delta
],
3237 &entries
[middle
+ delta
+ 1]))
3238 return middle
+ delta
+ 1;
3241 /* Every entry had the same hash */
3246 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3247 * first_hash will record the 1st hash of the new bucket.
3249 * Normally half of the xattrs will be moved. But we have to make
3250 * sure that the xattrs with the same hash value are stored in the
3251 * same bucket. If all the xattrs in this bucket have the same hash
3252 * value, the new bucket will be initialized as an empty one and the
3253 * first_hash will be initialized as (hash_value+1).
3255 static int ocfs2_divide_xattr_bucket(struct inode
*inode
,
3260 int new_bucket_head
)
3263 int count
, start
, len
, name_value_len
= 0, xe_len
, name_offset
= 0;
3264 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
3265 struct ocfs2_xattr_header
*xh
;
3266 struct ocfs2_xattr_entry
*xe
;
3267 int blocksize
= inode
->i_sb
->s_blocksize
;
3269 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
3270 (unsigned long long)blk
, (unsigned long long)new_blk
);
3272 s_bucket
= ocfs2_xattr_bucket_new(inode
);
3273 t_bucket
= ocfs2_xattr_bucket_new(inode
);
3274 if (!s_bucket
|| !t_bucket
) {
3280 ret
= ocfs2_read_xattr_bucket(s_bucket
, blk
);
3286 ret
= ocfs2_xattr_bucket_journal_access(handle
, s_bucket
,
3287 OCFS2_JOURNAL_ACCESS_WRITE
);
3294 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3295 * there's no need to read it.
3297 ret
= ocfs2_init_xattr_bucket(t_bucket
, new_blk
);
3303 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
3305 OCFS2_JOURNAL_ACCESS_CREATE
:
3306 OCFS2_JOURNAL_ACCESS_WRITE
);
3312 xh
= bucket_xh(s_bucket
);
3313 count
= le16_to_cpu(xh
->xh_count
);
3314 start
= ocfs2_xattr_find_divide_pos(xh
);
3316 if (start
== count
) {
3317 xe
= &xh
->xh_entries
[start
-1];
3320 * initialized a new empty bucket here.
3321 * The hash value is set as one larger than
3322 * that of the last entry in the previous bucket.
3324 for (i
= 0; i
< t_bucket
->bu_blocks
; i
++)
3325 memset(bucket_block(t_bucket
, i
), 0, blocksize
);
3327 xh
= bucket_xh(t_bucket
);
3328 xh
->xh_free_start
= cpu_to_le16(blocksize
);
3329 xh
->xh_entries
[0].xe_name_hash
= xe
->xe_name_hash
;
3330 le32_add_cpu(&xh
->xh_entries
[0].xe_name_hash
, 1);
3332 goto set_num_buckets
;
3335 /* copy the whole bucket to the new first. */
3336 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
3338 /* update the new bucket. */
3339 xh
= bucket_xh(t_bucket
);
3342 * Calculate the total name/value len and xh_free_start for
3343 * the old bucket first.
3345 name_offset
= OCFS2_XATTR_BUCKET_SIZE
;
3347 for (i
= 0; i
< start
; i
++) {
3348 xe
= &xh
->xh_entries
[i
];
3349 xe_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3350 if (ocfs2_xattr_is_local(xe
))
3352 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3354 xe_len
+= OCFS2_XATTR_ROOT_SIZE
;
3355 name_value_len
+= xe_len
;
3356 if (le16_to_cpu(xe
->xe_name_offset
) < name_offset
)
3357 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3361 * Now begin the modification to the new bucket.
3363 * In the new bucket, We just move the xattr entry to the beginning
3364 * and don't touch the name/value. So there will be some holes in the
3365 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3368 xe
= &xh
->xh_entries
[start
];
3369 len
= sizeof(struct ocfs2_xattr_entry
) * (count
- start
);
3370 mlog(0, "mv xattr entry len %d from %d to %d\n", len
,
3371 (int)((char *)xe
- (char *)xh
),
3372 (int)((char *)xh
->xh_entries
- (char *)xh
));
3373 memmove((char *)xh
->xh_entries
, (char *)xe
, len
);
3374 xe
= &xh
->xh_entries
[count
- start
];
3375 len
= sizeof(struct ocfs2_xattr_entry
) * start
;
3376 memset((char *)xe
, 0, len
);
3378 le16_add_cpu(&xh
->xh_count
, -start
);
3379 le16_add_cpu(&xh
->xh_name_value_len
, -name_value_len
);
3381 /* Calculate xh_free_start for the new bucket. */
3382 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
3383 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
3384 xe
= &xh
->xh_entries
[i
];
3385 xe_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3386 if (ocfs2_xattr_is_local(xe
))
3388 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3390 xe_len
+= OCFS2_XATTR_ROOT_SIZE
;
3391 if (le16_to_cpu(xe
->xe_name_offset
) <
3392 le16_to_cpu(xh
->xh_free_start
))
3393 xh
->xh_free_start
= xe
->xe_name_offset
;
3397 /* set xh->xh_num_buckets for the new xh. */
3398 if (new_bucket_head
)
3399 xh
->xh_num_buckets
= cpu_to_le16(1);
3401 xh
->xh_num_buckets
= 0;
3403 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
3405 /* store the first_hash of the new bucket. */
3407 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3410 * Now only update the 1st block of the old bucket. If we
3411 * just added a new empty bucket, there is no need to modify
3417 xh
= bucket_xh(s_bucket
);
3418 memset(&xh
->xh_entries
[start
], 0,
3419 sizeof(struct ocfs2_xattr_entry
) * (count
- start
));
3420 xh
->xh_count
= cpu_to_le16(start
);
3421 xh
->xh_free_start
= cpu_to_le16(name_offset
);
3422 xh
->xh_name_value_len
= cpu_to_le16(name_value_len
);
3424 ocfs2_xattr_bucket_journal_dirty(handle
, s_bucket
);
3427 ocfs2_xattr_bucket_free(s_bucket
);
3428 ocfs2_xattr_bucket_free(t_bucket
);
3434 * Copy xattr from one bucket to another bucket.
3436 * The caller must make sure that the journal transaction
3437 * has enough space for journaling.
3439 static int ocfs2_cp_xattr_bucket(struct inode
*inode
,
3446 struct ocfs2_xattr_bucket
*s_bucket
= NULL
, *t_bucket
= NULL
;
3448 BUG_ON(s_blkno
== t_blkno
);
3450 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3451 (unsigned long long)s_blkno
, (unsigned long long)t_blkno
,
3454 s_bucket
= ocfs2_xattr_bucket_new(inode
);
3455 t_bucket
= ocfs2_xattr_bucket_new(inode
);
3456 if (!s_bucket
|| !t_bucket
) {
3462 ret
= ocfs2_read_xattr_bucket(s_bucket
, s_blkno
);
3467 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3468 * there's no need to read it.
3470 ret
= ocfs2_init_xattr_bucket(t_bucket
, t_blkno
);
3474 ret
= ocfs2_xattr_bucket_journal_access(handle
, t_bucket
,
3476 OCFS2_JOURNAL_ACCESS_CREATE
:
3477 OCFS2_JOURNAL_ACCESS_WRITE
);
3481 ocfs2_xattr_bucket_copy_data(t_bucket
, s_bucket
);
3482 ocfs2_xattr_bucket_journal_dirty(handle
, t_bucket
);
3485 ocfs2_xattr_bucket_free(t_bucket
);
3486 ocfs2_xattr_bucket_free(s_bucket
);
3492 * Copy one xattr cluster from src_blk to to_blk.
3493 * The to_blk will become the first bucket header of the cluster, so its
3494 * xh_num_buckets will be initialized as the bucket num in the cluster.
3496 static int ocfs2_cp_xattr_cluster(struct inode
*inode
,
3498 struct buffer_head
*first_bh
,
3503 int i
, ret
, credits
;
3504 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3505 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3506 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
3507 struct buffer_head
*bh
= NULL
;
3508 struct ocfs2_xattr_header
*xh
;
3509 u64 to_blk_start
= to_blk
;
3511 mlog(0, "cp xattrs from cluster %llu to %llu\n",
3512 (unsigned long long)src_blk
, (unsigned long long)to_blk
);
3515 * We need to update the new cluster and 1 more for the update of
3516 * the 1st bucket of the previous extent rec.
3519 ret
= ocfs2_extend_trans(handle
, credits
);
3525 ret
= ocfs2_journal_access(handle
, inode
, first_bh
,
3526 OCFS2_JOURNAL_ACCESS_WRITE
);
3532 for (i
= 0; i
< num_buckets
; i
++) {
3533 ret
= ocfs2_cp_xattr_bucket(inode
, handle
,
3534 src_blk
, to_blk
, 1);
3540 src_blk
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3541 to_blk
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3544 /* update the old bucket header. */
3545 xh
= (struct ocfs2_xattr_header
*)first_bh
->b_data
;
3546 le16_add_cpu(&xh
->xh_num_buckets
, -num_buckets
);
3548 ocfs2_journal_dirty(handle
, first_bh
);
3550 /* update the new bucket header. */
3551 ret
= ocfs2_read_block(inode
, to_blk_start
, &bh
);
3557 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3558 OCFS2_JOURNAL_ACCESS_WRITE
);
3564 xh
= (struct ocfs2_xattr_header
*)bh
->b_data
;
3565 xh
->xh_num_buckets
= cpu_to_le16(num_buckets
);
3567 ocfs2_journal_dirty(handle
, bh
);
3570 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3577 * Move some xattrs in this cluster to the new cluster.
3578 * This function should only be called when bucket size == cluster size.
3579 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3581 static int ocfs2_divide_xattr_cluster(struct inode
*inode
,
3587 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3588 int ret
, credits
= 2 * blk_per_bucket
;
3590 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
< OCFS2_SB(inode
->i_sb
)->s_clustersize
);
3592 ret
= ocfs2_extend_trans(handle
, credits
);
3598 /* Move half of the xattr in start_blk to the next bucket. */
3599 return ocfs2_divide_xattr_bucket(inode
, handle
, prev_blk
,
3600 new_blk
, first_hash
, 1);
3604 * Move some xattrs from the old cluster to the new one since they are not
3605 * contiguous in ocfs2 xattr tree.
3607 * new_blk starts a new separate cluster, and we will move some xattrs from
3608 * prev_blk to it. v_start will be set as the first name hash value in this
3609 * new cluster so that it can be used as e_cpos during tree insertion and
3610 * don't collide with our original b-tree operations. first_bh and header_bh
3611 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3612 * to extend the insert bucket.
3614 * The problem is how much xattr should we move to the new one and when should
3615 * we update first_bh and header_bh?
3616 * 1. If cluster size > bucket size, that means the previous cluster has more
3617 * than 1 bucket, so just move half nums of bucket into the new cluster and
3618 * update the first_bh and header_bh if the insert bucket has been moved
3619 * to the new cluster.
3620 * 2. If cluster_size == bucket_size:
3621 * a) If the previous extent rec has more than one cluster and the insert
3622 * place isn't in the last cluster, copy the entire last cluster to the
3623 * new one. This time, we don't need to upate the first_bh and header_bh
3624 * since they will not be moved into the new cluster.
3625 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3626 * the new one. And we set the extend flag to zero if the insert place is
3627 * moved into the new allocated cluster since no extend is needed.
3629 static int ocfs2_adjust_xattr_cross_cluster(struct inode
*inode
,
3631 struct buffer_head
**first_bh
,
3632 struct buffer_head
**header_bh
,
3640 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3642 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3643 (unsigned long long)prev_blk
, prev_clusters
,
3644 (unsigned long long)new_blk
);
3646 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
)) > 1)
3647 ret
= ocfs2_mv_xattr_bucket_cross_cluster(inode
,
3656 u64 last_blk
= prev_blk
+ bpc
* (prev_clusters
- 1);
3658 if (prev_clusters
> 1 && (*header_bh
)->b_blocknr
!= last_blk
)
3659 ret
= ocfs2_cp_xattr_cluster(inode
, handle
, *first_bh
,
3663 ret
= ocfs2_divide_xattr_cluster(inode
, handle
,
3667 if ((*header_bh
)->b_blocknr
== last_blk
&& extend
)
3676 * Add a new cluster for xattr storage.
3678 * If the new cluster is contiguous with the previous one, it will be
3679 * appended to the same extent record, and num_clusters will be updated.
3680 * If not, we will insert a new extent for it and move some xattrs in
3681 * the last cluster into the new allocated one.
3682 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3683 * lose the benefits of hashing because we'll have to search large leaves.
3684 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3687 * first_bh is the first block of the previous extent rec and header_bh
3688 * indicates the bucket we will insert the new xattrs. They will be updated
3689 * when the header_bh is moved into the new cluster.
3691 static int ocfs2_add_new_xattr_cluster(struct inode
*inode
,
3692 struct buffer_head
*root_bh
,
3693 struct buffer_head
**first_bh
,
3694 struct buffer_head
**header_bh
,
3701 u16 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3702 u32 prev_clusters
= *num_clusters
;
3703 u32 clusters_to_add
= 1, bit_off
, num_bits
, v_start
= 0;
3705 handle_t
*handle
= NULL
;
3706 struct ocfs2_alloc_context
*data_ac
= NULL
;
3707 struct ocfs2_alloc_context
*meta_ac
= NULL
;
3708 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3709 struct ocfs2_extent_tree et
;
3711 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3712 "previous xattr blkno = %llu\n",
3713 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
3714 prev_cpos
, (unsigned long long)prev_blkno
);
3716 ocfs2_init_xattr_tree_extent_tree(&et
, inode
, root_bh
);
3718 ret
= ocfs2_lock_allocators(inode
, &et
, clusters_to_add
, 0,
3719 &data_ac
, &meta_ac
);
3725 credits
= ocfs2_calc_extend_credits(osb
->sb
, et
.et_root_el
,
3727 handle
= ocfs2_start_trans(osb
, credits
);
3728 if (IS_ERR(handle
)) {
3729 ret
= PTR_ERR(handle
);
3735 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
3736 OCFS2_JOURNAL_ACCESS_WRITE
);
3742 ret
= __ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
3743 clusters_to_add
, &bit_off
, &num_bits
);
3750 BUG_ON(num_bits
> clusters_to_add
);
3752 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
3753 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3754 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3756 if (prev_blkno
+ prev_clusters
* bpc
== block
&&
3757 (prev_clusters
+ num_bits
) << osb
->s_clustersize_bits
<=
3758 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
) {
3760 * If this cluster is contiguous with the old one and
3761 * adding this new cluster, we don't surpass the limit of
3762 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3763 * initialized and used like other buckets in the previous
3765 * So add it as a contiguous one. The caller will handle
3768 v_start
= prev_cpos
+ prev_clusters
;
3769 *num_clusters
= prev_clusters
+ num_bits
;
3770 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3773 ret
= ocfs2_adjust_xattr_cross_cluster(inode
,
3788 if (handle
->h_buffer_credits
< credits
) {
3790 * The journal has been restarted before, and don't
3791 * have enough space for the insertion, so extend it
3794 ret
= ocfs2_extend_trans(handle
, credits
);
3800 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3801 num_bits
, (unsigned long long)block
, v_start
);
3802 ret
= ocfs2_insert_extent(osb
, handle
, inode
, &et
, v_start
, block
,
3803 num_bits
, 0, meta_ac
);
3809 ret
= ocfs2_journal_dirty(handle
, root_bh
);
3817 ocfs2_commit_trans(osb
, handle
);
3819 ocfs2_free_alloc_context(data_ac
);
3821 ocfs2_free_alloc_context(meta_ac
);
3827 * Extend a new xattr bucket and move xattrs to the end one by one until
3828 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3830 static int ocfs2_extend_xattr_bucket(struct inode
*inode
,
3831 struct buffer_head
*first_bh
,
3832 struct buffer_head
*start_bh
,
3836 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3837 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3838 u64 start_blk
= start_bh
->b_blocknr
, end_blk
;
3839 u32 num_buckets
= num_clusters
* ocfs2_xattr_buckets_per_cluster(osb
);
3841 struct ocfs2_xattr_header
*first_xh
=
3842 (struct ocfs2_xattr_header
*)first_bh
->b_data
;
3843 u16 bucket
= le16_to_cpu(first_xh
->xh_num_buckets
);
3845 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3846 "from %llu, len = %u\n", (unsigned long long)start_blk
,
3847 (unsigned long long)first_bh
->b_blocknr
, num_clusters
);
3849 BUG_ON(bucket
>= num_buckets
);
3851 end_blk
= first_bh
->b_blocknr
+ (bucket
- 1) * blk_per_bucket
;
3854 * We will touch all the buckets after the start_bh(include it).
3855 * Then we add one more bucket.
3857 credits
= end_blk
- start_blk
+ 3 * blk_per_bucket
+ 1;
3858 handle
= ocfs2_start_trans(osb
, credits
);
3859 if (IS_ERR(handle
)) {
3860 ret
= PTR_ERR(handle
);
3866 ret
= ocfs2_journal_access(handle
, inode
, first_bh
,
3867 OCFS2_JOURNAL_ACCESS_WRITE
);
3873 while (end_blk
!= start_blk
) {
3874 ret
= ocfs2_cp_xattr_bucket(inode
, handle
, end_blk
,
3875 end_blk
+ blk_per_bucket
, 0);
3878 end_blk
-= blk_per_bucket
;
3881 /* Move half of the xattr in start_blk to the next bucket. */
3882 ret
= ocfs2_divide_xattr_bucket(inode
, handle
, start_blk
,
3883 start_blk
+ blk_per_bucket
, NULL
, 0);
3885 le16_add_cpu(&first_xh
->xh_num_buckets
, 1);
3886 ocfs2_journal_dirty(handle
, first_bh
);
3889 ocfs2_commit_trans(osb
, handle
);
3895 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3896 * xb_bh is the ocfs2_xattr_block.
3897 * We will move all the buckets starting from header_bh to the next place. As
3898 * for this one, half num of its xattrs will be moved to the next one.
3900 * We will allocate a new cluster if current cluster is full and adjust
3901 * header_bh and first_bh if the insert place is moved to the new cluster.
3903 static int ocfs2_add_new_xattr_bucket(struct inode
*inode
,
3904 struct buffer_head
*xb_bh
,
3905 struct buffer_head
*header_bh
)
3907 struct ocfs2_xattr_header
*first_xh
= NULL
;
3908 struct buffer_head
*first_bh
= NULL
;
3909 struct ocfs2_xattr_block
*xb
=
3910 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
3911 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
3912 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
3913 struct ocfs2_xattr_header
*xh
=
3914 (struct ocfs2_xattr_header
*)header_bh
->b_data
;
3915 u32 name_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3916 struct super_block
*sb
= inode
->i_sb
;
3917 struct ocfs2_super
*osb
= OCFS2_SB(sb
);
3918 int ret
, num_buckets
, extend
= 1;
3920 u32 e_cpos
, num_clusters
;
3922 mlog(0, "Add new xattr bucket starting form %llu\n",
3923 (unsigned long long)header_bh
->b_blocknr
);
3926 * Add refrence for header_bh here because it may be
3927 * changed in ocfs2_add_new_xattr_cluster and we need
3928 * to free it in the end.
3932 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &e_cpos
,
3939 ret
= ocfs2_read_block(inode
, p_blkno
, &first_bh
);
3945 num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
) * num_clusters
;
3946 first_xh
= (struct ocfs2_xattr_header
*)first_bh
->b_data
;
3948 if (num_buckets
== le16_to_cpu(first_xh
->xh_num_buckets
)) {
3949 ret
= ocfs2_add_new_xattr_cluster(inode
,
3964 ret
= ocfs2_extend_xattr_bucket(inode
,
3976 static inline char *ocfs2_xattr_bucket_get_val(struct inode
*inode
,
3977 struct ocfs2_xattr_bucket
*bucket
,
3980 int block_off
= offs
>> inode
->i_sb
->s_blocksize_bits
;
3982 offs
= offs
% inode
->i_sb
->s_blocksize
;
3983 return bucket_block(bucket
, block_off
) + offs
;
3987 * Handle the normal xattr set, including replace, delete and new.
3989 * Note: "local" indicates the real data's locality. So we can't
3990 * just its bucket locality by its length.
3992 static void ocfs2_xattr_set_entry_normal(struct inode
*inode
,
3993 struct ocfs2_xattr_info
*xi
,
3994 struct ocfs2_xattr_search
*xs
,
3998 struct ocfs2_xattr_entry
*last
, *xe
;
3999 int name_len
= strlen(xi
->name
);
4000 struct ocfs2_xattr_header
*xh
= xs
->header
;
4001 u16 count
= le16_to_cpu(xh
->xh_count
), start
;
4002 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4004 size_t offs
, size
, new_size
;
4006 last
= &xh
->xh_entries
[count
];
4007 if (!xs
->not_found
) {
4009 offs
= le16_to_cpu(xe
->xe_name_offset
);
4010 if (ocfs2_xattr_is_local(xe
))
4011 size
= OCFS2_XATTR_SIZE(name_len
) +
4012 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
4014 size
= OCFS2_XATTR_SIZE(name_len
) +
4015 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE
);
4018 * If the new value will be stored outside, xi->value has been
4019 * initalized as an empty ocfs2_xattr_value_root, and the same
4020 * goes with xi->value_len, so we can set new_size safely here.
4021 * See ocfs2_xattr_set_in_bucket.
4023 new_size
= OCFS2_XATTR_SIZE(name_len
) +
4024 OCFS2_XATTR_SIZE(xi
->value_len
);
4026 le16_add_cpu(&xh
->xh_name_value_len
, -size
);
4028 if (new_size
> size
)
4029 goto set_new_name_value
;
4031 /* Now replace the old value with new one. */
4033 xe
->xe_value_size
= cpu_to_le64(xi
->value_len
);
4035 xe
->xe_value_size
= 0;
4037 val
= ocfs2_xattr_bucket_get_val(inode
,
4039 memset(val
+ OCFS2_XATTR_SIZE(name_len
), 0,
4040 size
- OCFS2_XATTR_SIZE(name_len
));
4041 if (OCFS2_XATTR_SIZE(xi
->value_len
) > 0)
4042 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
4043 xi
->value
, xi
->value_len
);
4045 le16_add_cpu(&xh
->xh_name_value_len
, new_size
);
4046 ocfs2_xattr_set_local(xe
, local
);
4050 * Remove the old entry if there is more than one.
4051 * We don't remove the last entry so that we can
4052 * use it to indicate the hash value of the empty
4056 le16_add_cpu(&xh
->xh_count
, -1);
4059 (void *)last
- (void *)xe
);
4061 sizeof(struct ocfs2_xattr_entry
));
4064 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
4069 /* find a new entry for insert. */
4070 int low
= 0, high
= count
- 1, tmp
;
4071 struct ocfs2_xattr_entry
*tmp_xe
;
4073 while (low
<= high
&& count
) {
4074 tmp
= (low
+ high
) / 2;
4075 tmp_xe
= &xh
->xh_entries
[tmp
];
4077 if (name_hash
> le32_to_cpu(tmp_xe
->xe_name_hash
))
4079 else if (name_hash
<
4080 le32_to_cpu(tmp_xe
->xe_name_hash
))
4088 xe
= &xh
->xh_entries
[low
];
4090 memmove(xe
+ 1, xe
, (void *)last
- (void *)xe
);
4092 le16_add_cpu(&xh
->xh_count
, 1);
4093 memset(xe
, 0, sizeof(struct ocfs2_xattr_entry
));
4094 xe
->xe_name_hash
= cpu_to_le32(name_hash
);
4095 xe
->xe_name_len
= name_len
;
4096 ocfs2_xattr_set_type(xe
, xi
->name_index
);
4100 /* Insert the new name+value. */
4101 size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_SIZE(xi
->value_len
);
4104 * We must make sure that the name/value pair
4105 * exists in the same block.
4107 offs
= le16_to_cpu(xh
->xh_free_start
);
4108 start
= offs
- size
;
4110 if (start
>> inode
->i_sb
->s_blocksize_bits
!=
4111 (offs
- 1) >> inode
->i_sb
->s_blocksize_bits
) {
4112 offs
= offs
- offs
% blocksize
;
4113 xh
->xh_free_start
= cpu_to_le16(offs
);
4116 val
= ocfs2_xattr_bucket_get_val(inode
, xs
->bucket
, offs
- size
);
4117 xe
->xe_name_offset
= cpu_to_le16(offs
- size
);
4119 memset(val
, 0, size
);
4120 memcpy(val
, xi
->name
, name_len
);
4121 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
), xi
->value
, xi
->value_len
);
4123 xe
->xe_value_size
= cpu_to_le64(xi
->value_len
);
4124 ocfs2_xattr_set_local(xe
, local
);
4126 le16_add_cpu(&xh
->xh_free_start
, -size
);
4127 le16_add_cpu(&xh
->xh_name_value_len
, size
);
4133 * Set the xattr entry in the specified bucket.
4134 * The bucket is indicated by xs->bucket and it should have the enough
4135 * space for the xattr insertion.
4137 static int ocfs2_xattr_set_entry_in_bucket(struct inode
*inode
,
4138 struct ocfs2_xattr_info
*xi
,
4139 struct ocfs2_xattr_search
*xs
,
4144 handle_t
*handle
= NULL
;
4145 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4146 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4148 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4149 (unsigned long)xi
->value_len
, xi
->name_index
,
4150 (unsigned long long)bucket_blkno(xs
->bucket
));
4152 if (!xs
->bucket
->bu_bhs
[1]) {
4153 ret
= ocfs2_read_blocks(inode
,
4154 bucket_blkno(xs
->bucket
) + 1,
4155 blk_per_bucket
- 1, &xs
->bucket
->bu_bhs
[1],
4163 handle
= ocfs2_start_trans(osb
, blk_per_bucket
);
4164 if (IS_ERR(handle
)) {
4165 ret
= PTR_ERR(handle
);
4171 ret
= ocfs2_xattr_bucket_journal_access(handle
, xs
->bucket
,
4172 OCFS2_JOURNAL_ACCESS_WRITE
);
4178 ocfs2_xattr_set_entry_normal(inode
, xi
, xs
, name_hash
, local
);
4179 ocfs2_xattr_bucket_journal_dirty(handle
, xs
->bucket
);
4182 ocfs2_commit_trans(osb
, handle
);
4187 static int ocfs2_xattr_value_update_size(struct inode
*inode
,
4188 struct buffer_head
*xe_bh
,
4189 struct ocfs2_xattr_entry
*xe
,
4193 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4194 handle_t
*handle
= NULL
;
4196 handle
= ocfs2_start_trans(osb
, 1);
4197 if (IS_ERR(handle
)) {
4203 ret
= ocfs2_journal_access(handle
, inode
, xe_bh
,
4204 OCFS2_JOURNAL_ACCESS_WRITE
);
4210 xe
->xe_value_size
= cpu_to_le64(new_size
);
4212 ret
= ocfs2_journal_dirty(handle
, xe_bh
);
4217 ocfs2_commit_trans(osb
, handle
);
4223 * Truncate the specified xe_off entry in xattr bucket.
4224 * bucket is indicated by header_bh and len is the new length.
4225 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4227 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4229 static int ocfs2_xattr_bucket_value_truncate(struct inode
*inode
,
4230 struct buffer_head
*header_bh
,
4236 struct buffer_head
*value_bh
= NULL
;
4237 struct ocfs2_xattr_value_root
*xv
;
4238 struct ocfs2_xattr_entry
*xe
;
4239 struct ocfs2_xattr_header
*xh
=
4240 (struct ocfs2_xattr_header
*)header_bh
->b_data
;
4241 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4243 xe
= &xh
->xh_entries
[xe_off
];
4245 BUG_ON(!xe
|| ocfs2_xattr_is_local(xe
));
4247 offset
= le16_to_cpu(xe
->xe_name_offset
) +
4248 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
4250 value_blk
= offset
/ blocksize
;
4252 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4253 BUG_ON(value_blk
!= (offset
+ OCFS2_XATTR_ROOT_SIZE
- 1) / blocksize
);
4254 value_blk
+= header_bh
->b_blocknr
;
4256 ret
= ocfs2_read_block(inode
, value_blk
, &value_bh
);
4262 xv
= (struct ocfs2_xattr_value_root
*)
4263 (value_bh
->b_data
+ offset
% blocksize
);
4265 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4266 xe_off
, (unsigned long long)header_bh
->b_blocknr
, len
);
4267 ret
= ocfs2_xattr_value_truncate(inode
, value_bh
, xv
, len
);
4273 ret
= ocfs2_xattr_value_update_size(inode
, header_bh
, xe
, len
);
4284 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode
*inode
,
4285 struct ocfs2_xattr_search
*xs
,
4289 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4290 struct ocfs2_xattr_header
*xh
= (struct ocfs2_xattr_header
*)xs
->base
;
4292 BUG_ON(!xs
->bucket
->bu_bhs
[0] || !xe
|| ocfs2_xattr_is_local(xe
));
4294 offset
= xe
- xh
->xh_entries
;
4295 ret
= ocfs2_xattr_bucket_value_truncate(inode
, xs
->bucket
->bu_bhs
[0],
4303 static int ocfs2_xattr_bucket_set_value_outside(struct inode
*inode
,
4304 struct ocfs2_xattr_search
*xs
,
4309 struct ocfs2_xattr_value_root
*xv
;
4310 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4312 BUG_ON(!xs
->base
|| !xe
|| ocfs2_xattr_is_local(xe
));
4314 offset
= le16_to_cpu(xe
->xe_name_offset
) +
4315 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
4317 xv
= (struct ocfs2_xattr_value_root
*)(xs
->base
+ offset
);
4319 return __ocfs2_xattr_set_value_outside(inode
, xv
, val
, value_len
);
4322 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
4323 struct buffer_head
*root_bh
,
4329 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4330 struct inode
*tl_inode
= osb
->osb_tl_inode
;
4332 struct ocfs2_xattr_block
*xb
=
4333 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
4334 struct ocfs2_alloc_context
*meta_ac
= NULL
;
4335 struct ocfs2_cached_dealloc_ctxt dealloc
;
4336 struct ocfs2_extent_tree et
;
4338 ocfs2_init_xattr_tree_extent_tree(&et
, inode
, root_bh
);
4340 ocfs2_init_dealloc_ctxt(&dealloc
);
4342 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4343 cpos
, len
, (unsigned long long)blkno
);
4345 ocfs2_remove_xattr_clusters_from_cache(inode
, blkno
, len
);
4347 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
4353 mutex_lock(&tl_inode
->i_mutex
);
4355 if (ocfs2_truncate_log_needs_flush(osb
)) {
4356 ret
= __ocfs2_flush_truncate_log(osb
);
4363 handle
= ocfs2_start_trans(osb
, OCFS2_REMOVE_EXTENT_CREDITS
);
4364 if (IS_ERR(handle
)) {
4370 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
4371 OCFS2_JOURNAL_ACCESS_WRITE
);
4377 ret
= ocfs2_remove_extent(inode
, &et
, cpos
, len
, handle
, meta_ac
,
4384 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, -len
);
4386 ret
= ocfs2_journal_dirty(handle
, root_bh
);
4392 ret
= ocfs2_truncate_log_append(osb
, handle
, blkno
, len
);
4397 ocfs2_commit_trans(osb
, handle
);
4399 ocfs2_schedule_truncate_log_flush(osb
, 1);
4401 mutex_unlock(&tl_inode
->i_mutex
);
4404 ocfs2_free_alloc_context(meta_ac
);
4406 ocfs2_run_deallocs(osb
, &dealloc
);
4411 static void ocfs2_xattr_bucket_remove_xs(struct inode
*inode
,
4412 struct ocfs2_xattr_search
*xs
)
4414 handle_t
*handle
= NULL
;
4415 struct ocfs2_xattr_header
*xh
= bucket_xh(xs
->bucket
);
4416 struct ocfs2_xattr_entry
*last
= &xh
->xh_entries
[
4417 le16_to_cpu(xh
->xh_count
) - 1];
4420 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
4421 ocfs2_blocks_per_xattr_bucket(inode
->i_sb
));
4422 if (IS_ERR(handle
)) {
4423 ret
= PTR_ERR(handle
);
4428 ret
= ocfs2_xattr_bucket_journal_access(handle
, xs
->bucket
,
4429 OCFS2_JOURNAL_ACCESS_WRITE
);
4435 /* Remove the old entry. */
4436 memmove(xs
->here
, xs
->here
+ 1,
4437 (void *)last
- (void *)xs
->here
);
4438 memset(last
, 0, sizeof(struct ocfs2_xattr_entry
));
4439 le16_add_cpu(&xh
->xh_count
, -1);
4441 ocfs2_xattr_bucket_journal_dirty(handle
, xs
->bucket
);
4444 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
4448 * Set the xattr name/value in the bucket specified in xs.
4450 * As the new value in xi may be stored in the bucket or in an outside cluster,
4451 * we divide the whole process into 3 steps:
4452 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4453 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4454 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4455 * 4. If the clusters for the new outside value can't be allocated, we need
4456 * to free the xattr we allocated in set.
4458 static int ocfs2_xattr_set_in_bucket(struct inode
*inode
,
4459 struct ocfs2_xattr_info
*xi
,
4460 struct ocfs2_xattr_search
*xs
)
4464 char *val
= (char *)xi
->value
;
4465 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4466 u32 name_hash
= ocfs2_xattr_name_hash(inode
, xi
->name
,
4469 if (!xs
->not_found
&& !ocfs2_xattr_is_local(xe
)) {
4471 * We need to truncate the xattr storage first.
4473 * If both the old and new value are stored to
4474 * outside block, we only need to truncate
4475 * the storage and then set the value outside.
4477 * If the new value should be stored within block,
4478 * we should free all the outside block first and
4479 * the modification to the xattr block will be done
4480 * by following steps.
4482 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
4483 value_len
= xi
->value_len
;
4487 ret
= ocfs2_xattr_bucket_value_truncate_xs(inode
, xs
,
4493 goto set_value_outside
;
4496 value_len
= xi
->value_len
;
4497 /* So we have to handle the inside block change now. */
4498 if (value_len
> OCFS2_XATTR_INLINE_SIZE
) {
4500 * If the new value will be stored outside of block,
4501 * initalize a new empty value root and insert it first.
4504 xi
->value
= &def_xv
;
4505 xi
->value_len
= OCFS2_XATTR_ROOT_SIZE
;
4508 ret
= ocfs2_xattr_set_entry_in_bucket(inode
, xi
, xs
, name_hash
, local
);
4514 if (value_len
<= OCFS2_XATTR_INLINE_SIZE
)
4517 /* allocate the space now for the outside block storage. */
4518 ret
= ocfs2_xattr_bucket_value_truncate_xs(inode
, xs
,
4523 if (xs
->not_found
) {
4525 * We can't allocate enough clusters for outside
4526 * storage and we have allocated xattr already,
4527 * so need to remove it.
4529 ocfs2_xattr_bucket_remove_xs(inode
, xs
);
4535 ret
= ocfs2_xattr_bucket_set_value_outside(inode
, xs
, val
, value_len
);
4541 * check whether the xattr bucket is filled up with the same hash value.
4542 * If we want to insert the xattr with the same hash, return -ENOSPC.
4543 * If we want to insert a xattr with different hash value, go ahead
4544 * and ocfs2_divide_xattr_bucket will handle this.
4546 static int ocfs2_check_xattr_bucket_collision(struct inode
*inode
,
4547 struct ocfs2_xattr_bucket
*bucket
,
4550 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
4551 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
4553 if (name_hash
!= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
))
4556 if (xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1].xe_name_hash
==
4557 xh
->xh_entries
[0].xe_name_hash
) {
4558 mlog(ML_ERROR
, "Too much hash collision in xattr bucket %llu, "
4560 (unsigned long long)bucket_blkno(bucket
),
4561 le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
));
4568 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
4569 struct ocfs2_xattr_info
*xi
,
4570 struct ocfs2_xattr_search
*xs
)
4572 struct ocfs2_xattr_header
*xh
;
4573 struct ocfs2_xattr_entry
*xe
;
4574 u16 count
, header_size
, xh_free_start
;
4575 int free
, max_free
, need
, old
;
4576 size_t value_size
= 0, name_len
= strlen(xi
->name
);
4577 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4578 int ret
, allocation
= 0;
4580 mlog_entry("Set xattr %s in xattr index block\n", xi
->name
);
4584 count
= le16_to_cpu(xh
->xh_count
);
4585 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4586 header_size
= sizeof(struct ocfs2_xattr_header
) +
4587 count
* sizeof(struct ocfs2_xattr_entry
);
4588 max_free
= OCFS2_XATTR_BUCKET_SIZE
-
4589 le16_to_cpu(xh
->xh_name_value_len
) - header_size
;
4591 mlog_bug_on_msg(header_size
> blocksize
, "bucket %llu has header size "
4592 "of %u which exceed block size\n",
4593 (unsigned long long)bucket_blkno(xs
->bucket
),
4596 if (xi
->value
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
4597 value_size
= OCFS2_XATTR_ROOT_SIZE
;
4599 value_size
= OCFS2_XATTR_SIZE(xi
->value_len
);
4602 need
= sizeof(struct ocfs2_xattr_entry
) +
4603 OCFS2_XATTR_SIZE(name_len
) + value_size
;
4605 need
= value_size
+ OCFS2_XATTR_SIZE(name_len
);
4608 * We only replace the old value if the new length is smaller
4609 * than the old one. Otherwise we will allocate new space in the
4610 * bucket to store it.
4613 if (ocfs2_xattr_is_local(xe
))
4614 old
= OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
4616 old
= OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE
);
4618 if (old
>= value_size
)
4622 free
= xh_free_start
- header_size
;
4624 * We need to make sure the new name/value pair
4625 * can exist in the same block.
4627 if (xh_free_start
% blocksize
< need
)
4628 free
-= xh_free_start
% blocksize
;
4630 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4631 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4632 " %u\n", xs
->not_found
,
4633 (unsigned long long)bucket_blkno(xs
->bucket
),
4634 free
, need
, max_free
, le16_to_cpu(xh
->xh_free_start
),
4635 le16_to_cpu(xh
->xh_name_value_len
));
4637 if (free
< need
|| count
== ocfs2_xattr_max_xe_in_bucket(inode
->i_sb
)) {
4638 if (need
<= max_free
&&
4639 count
< ocfs2_xattr_max_xe_in_bucket(inode
->i_sb
)) {
4641 * We can create the space by defragment. Since only the
4642 * name/value will be moved, the xe shouldn't be changed
4645 ret
= ocfs2_defrag_xattr_bucket(inode
, xs
->bucket
);
4651 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4652 free
= xh_free_start
- header_size
;
4653 if (xh_free_start
% blocksize
< need
)
4654 free
-= xh_free_start
% blocksize
;
4659 mlog(0, "Can't get enough space for xattr insert by "
4660 "defragment. Need %u bytes, but we have %d, so "
4661 "allocate new bucket for it.\n", need
, free
);
4665 * We have to add new buckets or clusters and one
4666 * allocation should leave us enough space for insert.
4671 * We do not allow for overlapping ranges between buckets. And
4672 * the maximum number of collisions we will allow for then is
4673 * one bucket's worth, so check it here whether we need to
4674 * add a new bucket for the insert.
4676 ret
= ocfs2_check_xattr_bucket_collision(inode
,
4684 ret
= ocfs2_add_new_xattr_bucket(inode
,
4686 xs
->bucket
->bu_bhs
[0]);
4692 ocfs2_xattr_bucket_relse(xs
->bucket
);
4694 ret
= ocfs2_xattr_index_block_find(inode
, xs
->xattr_bh
,
4697 if (ret
&& ret
!= -ENODATA
)
4699 xs
->not_found
= ret
;
4705 ret
= ocfs2_xattr_set_in_bucket(inode
, xi
, xs
);
4711 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
4712 struct ocfs2_xattr_bucket
*bucket
,
4716 struct ocfs2_xattr_header
*xh
= bucket_xh(bucket
);
4718 struct ocfs2_xattr_entry
*xe
;
4720 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
4721 xe
= &xh
->xh_entries
[i
];
4722 if (ocfs2_xattr_is_local(xe
))
4725 ret
= ocfs2_xattr_bucket_value_truncate(inode
,
4737 static int ocfs2_delete_xattr_index_block(struct inode
*inode
,
4738 struct buffer_head
*xb_bh
)
4740 struct ocfs2_xattr_block
*xb
=
4741 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4742 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
4744 u32 name_hash
= UINT_MAX
, e_cpos
, num_clusters
;
4747 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
4750 while (name_hash
> 0) {
4751 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
4752 &e_cpos
, &num_clusters
, el
);
4758 ret
= ocfs2_iterate_xattr_buckets(inode
, p_blkno
, num_clusters
,
4759 ocfs2_delete_xattr_in_bucket
,
4766 ret
= ocfs2_rm_xattr_cluster(inode
, xb_bh
,
4767 p_blkno
, e_cpos
, num_clusters
);
4776 name_hash
= e_cpos
- 1;
4784 * 'trusted' attributes support
4786 static size_t ocfs2_xattr_trusted_list(struct inode
*inode
, char *list
,
4787 size_t list_size
, const char *name
,
4790 const size_t prefix_len
= XATTR_TRUSTED_PREFIX_LEN
;
4791 const size_t total_len
= prefix_len
+ name_len
+ 1;
4793 if (list
&& total_len
<= list_size
) {
4794 memcpy(list
, XATTR_TRUSTED_PREFIX
, prefix_len
);
4795 memcpy(list
+ prefix_len
, name
, name_len
);
4796 list
[prefix_len
+ name_len
] = '\0';
4801 static int ocfs2_xattr_trusted_get(struct inode
*inode
, const char *name
,
4802 void *buffer
, size_t size
)
4804 if (strcmp(name
, "") == 0)
4806 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_TRUSTED
, name
,
4810 static int ocfs2_xattr_trusted_set(struct inode
*inode
, const char *name
,
4811 const void *value
, size_t size
, int flags
)
4813 if (strcmp(name
, "") == 0)
4816 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_TRUSTED
, name
, value
,
4820 struct xattr_handler ocfs2_xattr_trusted_handler
= {
4821 .prefix
= XATTR_TRUSTED_PREFIX
,
4822 .list
= ocfs2_xattr_trusted_list
,
4823 .get
= ocfs2_xattr_trusted_get
,
4824 .set
= ocfs2_xattr_trusted_set
,
4828 * 'user' attributes support
4830 static size_t ocfs2_xattr_user_list(struct inode
*inode
, char *list
,
4831 size_t list_size
, const char *name
,
4834 const size_t prefix_len
= XATTR_USER_PREFIX_LEN
;
4835 const size_t total_len
= prefix_len
+ name_len
+ 1;
4836 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4838 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
4841 if (list
&& total_len
<= list_size
) {
4842 memcpy(list
, XATTR_USER_PREFIX
, prefix_len
);
4843 memcpy(list
+ prefix_len
, name
, name_len
);
4844 list
[prefix_len
+ name_len
] = '\0';
4849 static int ocfs2_xattr_user_get(struct inode
*inode
, const char *name
,
4850 void *buffer
, size_t size
)
4852 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4854 if (strcmp(name
, "") == 0)
4856 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
4858 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_USER
, name
,
4862 static int ocfs2_xattr_user_set(struct inode
*inode
, const char *name
,
4863 const void *value
, size_t size
, int flags
)
4865 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4867 if (strcmp(name
, "") == 0)
4869 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
4872 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_USER
, name
, value
,
4876 struct xattr_handler ocfs2_xattr_user_handler
= {
4877 .prefix
= XATTR_USER_PREFIX
,
4878 .list
= ocfs2_xattr_user_list
,
4879 .get
= ocfs2_xattr_user_get
,
4880 .set
= ocfs2_xattr_user_set
,