1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2008 Oracle. All rights reserved.
9 * Lots of code in this file is taken from ext3.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
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.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
27 #include <linux/capability.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/highmem.h>
32 #include <linux/pagemap.h>
33 #include <linux/uio.h>
34 #include <linux/sched.h>
35 #include <linux/splice.h>
36 #include <linux/mount.h>
37 #include <linux/writeback.h>
38 #include <linux/falloc.h>
39 #include <linux/sort.h>
40 #include <linux/init.h>
41 #include <linux/module.h>
42 #include <linux/string.h>
44 #define MLOG_MASK_PREFIX ML_XATTR
45 #include <cluster/masklog.h>
58 #include "buffer_head_io.h"
63 struct ocfs2_xattr_def_value_root
{
64 struct ocfs2_xattr_value_root xv
;
65 struct ocfs2_extent_rec er
;
68 struct ocfs2_xattr_bucket
{
69 struct buffer_head
*bhs
[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
];
70 struct ocfs2_xattr_header
*xh
;
73 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
74 #define OCFS2_XATTR_INLINE_SIZE 80
76 static struct ocfs2_xattr_def_value_root def_xv
= {
77 .xv
.xr_list
.l_count
= cpu_to_le16(1),
80 struct xattr_handler
*ocfs2_xattr_handlers
[] = {
81 &ocfs2_xattr_user_handler
,
82 &ocfs2_xattr_trusted_handler
,
86 static struct xattr_handler
*ocfs2_xattr_handler_map
[] = {
87 [OCFS2_XATTR_INDEX_USER
] = &ocfs2_xattr_user_handler
,
88 [OCFS2_XATTR_INDEX_TRUSTED
] = &ocfs2_xattr_trusted_handler
,
91 struct ocfs2_xattr_info
{
98 struct ocfs2_xattr_search
{
99 struct buffer_head
*inode_bh
;
101 * xattr_bh point to the block buffer head which has extended attribute
102 * when extended attribute in inode, xattr_bh is equal to inode_bh.
104 struct buffer_head
*xattr_bh
;
105 struct ocfs2_xattr_header
*header
;
106 struct ocfs2_xattr_bucket bucket
;
109 struct ocfs2_xattr_entry
*here
;
113 static int ocfs2_xattr_bucket_get_name_value(struct inode
*inode
,
114 struct ocfs2_xattr_header
*xh
,
119 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
120 struct buffer_head
*root_bh
,
123 struct ocfs2_xattr_search
*xs
);
125 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
126 struct ocfs2_xattr_tree_root
*xt
,
130 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
131 struct ocfs2_xattr_search
*xs
);
133 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
134 struct ocfs2_xattr_info
*xi
,
135 struct ocfs2_xattr_search
*xs
);
137 static int ocfs2_delete_xattr_index_block(struct inode
*inode
,
138 struct buffer_head
*xb_bh
);
140 static inline const char *ocfs2_xattr_prefix(int name_index
)
142 struct xattr_handler
*handler
= NULL
;
144 if (name_index
> 0 && name_index
< OCFS2_XATTR_MAX
)
145 handler
= ocfs2_xattr_handler_map
[name_index
];
147 return handler
? handler
->prefix
: NULL
;
150 static u32
ocfs2_xattr_name_hash(struct inode
*inode
,
154 /* Get hash value of uuid from super block */
155 u32 hash
= OCFS2_SB(inode
->i_sb
)->uuid_hash
;
158 /* hash extended attribute name */
159 for (i
= 0; i
< name_len
; i
++) {
160 hash
= (hash
<< OCFS2_HASH_SHIFT
) ^
161 (hash
>> (8*sizeof(hash
) - OCFS2_HASH_SHIFT
)) ^
169 * ocfs2_xattr_hash_entry()
171 * Compute the hash of an extended attribute.
173 static void ocfs2_xattr_hash_entry(struct inode
*inode
,
174 struct ocfs2_xattr_header
*header
,
175 struct ocfs2_xattr_entry
*entry
)
178 char *name
= (char *)header
+ le16_to_cpu(entry
->xe_name_offset
);
180 hash
= ocfs2_xattr_name_hash(inode
, name
, entry
->xe_name_len
);
181 entry
->xe_name_hash
= cpu_to_le32(hash
);
186 static int ocfs2_xattr_extend_allocation(struct inode
*inode
,
188 struct buffer_head
*xattr_bh
,
189 struct ocfs2_xattr_value_root
*xv
)
192 int restart_func
= 0;
194 handle_t
*handle
= NULL
;
195 struct ocfs2_alloc_context
*data_ac
= NULL
;
196 struct ocfs2_alloc_context
*meta_ac
= NULL
;
197 enum ocfs2_alloc_restarted why
;
198 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
199 u32 prev_clusters
, logical_start
= le32_to_cpu(xv
->xr_clusters
);
200 struct ocfs2_extent_tree et
;
202 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add
);
204 ocfs2_init_xattr_value_extent_tree(&et
, inode
, xattr_bh
, xv
);
208 status
= ocfs2_lock_allocators(inode
, &et
, clusters_to_add
, 0,
215 credits
= ocfs2_calc_extend_credits(osb
->sb
, et
.et_root_el
,
217 handle
= ocfs2_start_trans(osb
, credits
);
218 if (IS_ERR(handle
)) {
219 status
= PTR_ERR(handle
);
225 restarted_transaction
:
226 status
= ocfs2_journal_access(handle
, inode
, xattr_bh
,
227 OCFS2_JOURNAL_ACCESS_WRITE
);
233 prev_clusters
= le32_to_cpu(xv
->xr_clusters
);
234 status
= ocfs2_add_clusters_in_btree(osb
,
244 if ((status
< 0) && (status
!= -EAGAIN
)) {
245 if (status
!= -ENOSPC
)
250 status
= ocfs2_journal_dirty(handle
, xattr_bh
);
256 clusters_to_add
-= le32_to_cpu(xv
->xr_clusters
) - prev_clusters
;
258 if (why
!= RESTART_NONE
&& clusters_to_add
) {
259 if (why
== RESTART_META
) {
260 mlog(0, "restarting function.\n");
263 BUG_ON(why
!= RESTART_TRANS
);
265 mlog(0, "restarting transaction.\n");
266 /* TODO: This can be more intelligent. */
267 credits
= ocfs2_calc_extend_credits(osb
->sb
,
270 status
= ocfs2_extend_trans(handle
, credits
);
272 /* handle still has to be committed at
278 goto restarted_transaction
;
284 ocfs2_commit_trans(osb
, handle
);
288 ocfs2_free_alloc_context(data_ac
);
292 ocfs2_free_alloc_context(meta_ac
);
295 if ((!status
) && restart_func
) {
303 static int __ocfs2_remove_xattr_range(struct inode
*inode
,
304 struct buffer_head
*root_bh
,
305 struct ocfs2_xattr_value_root
*xv
,
306 u32 cpos
, u32 phys_cpos
, u32 len
,
307 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
310 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
311 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
312 struct inode
*tl_inode
= osb
->osb_tl_inode
;
314 struct ocfs2_alloc_context
*meta_ac
= NULL
;
315 struct ocfs2_extent_tree et
;
317 ocfs2_init_xattr_value_extent_tree(&et
, inode
, root_bh
, xv
);
319 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
325 mutex_lock(&tl_inode
->i_mutex
);
327 if (ocfs2_truncate_log_needs_flush(osb
)) {
328 ret
= __ocfs2_flush_truncate_log(osb
);
335 handle
= ocfs2_start_trans(osb
, OCFS2_REMOVE_EXTENT_CREDITS
);
336 if (IS_ERR(handle
)) {
337 ret
= PTR_ERR(handle
);
342 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
343 OCFS2_JOURNAL_ACCESS_WRITE
);
349 ret
= ocfs2_remove_extent(inode
, &et
, cpos
, len
, handle
, meta_ac
,
356 le32_add_cpu(&xv
->xr_clusters
, -len
);
358 ret
= ocfs2_journal_dirty(handle
, root_bh
);
364 ret
= ocfs2_truncate_log_append(osb
, handle
, phys_blkno
, len
);
369 ocfs2_commit_trans(osb
, handle
);
371 mutex_unlock(&tl_inode
->i_mutex
);
374 ocfs2_free_alloc_context(meta_ac
);
379 static int ocfs2_xattr_shrink_size(struct inode
*inode
,
382 struct buffer_head
*root_bh
,
383 struct ocfs2_xattr_value_root
*xv
)
386 u32 trunc_len
, cpos
, phys_cpos
, alloc_size
;
388 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
389 struct ocfs2_cached_dealloc_ctxt dealloc
;
391 ocfs2_init_dealloc_ctxt(&dealloc
);
393 if (old_clusters
<= new_clusters
)
397 trunc_len
= old_clusters
- new_clusters
;
399 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &phys_cpos
,
400 &alloc_size
, &xv
->xr_list
);
406 if (alloc_size
> trunc_len
)
407 alloc_size
= trunc_len
;
409 ret
= __ocfs2_remove_xattr_range(inode
, root_bh
, xv
, cpos
,
410 phys_cpos
, alloc_size
,
417 block
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
418 ocfs2_remove_xattr_clusters_from_cache(inode
, block
,
421 trunc_len
-= alloc_size
;
425 ocfs2_schedule_truncate_log_flush(osb
, 1);
426 ocfs2_run_deallocs(osb
, &dealloc
);
431 static int ocfs2_xattr_value_truncate(struct inode
*inode
,
432 struct buffer_head
*root_bh
,
433 struct ocfs2_xattr_value_root
*xv
,
437 u32 new_clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, len
);
438 u32 old_clusters
= le32_to_cpu(xv
->xr_clusters
);
440 if (new_clusters
== old_clusters
)
443 if (new_clusters
> old_clusters
)
444 ret
= ocfs2_xattr_extend_allocation(inode
,
445 new_clusters
- old_clusters
,
448 ret
= ocfs2_xattr_shrink_size(inode
,
449 old_clusters
, new_clusters
,
455 static int ocfs2_xattr_list_entry(char *buffer
, size_t size
,
456 size_t *result
, const char *prefix
,
457 const char *name
, int name_len
)
459 char *p
= buffer
+ *result
;
460 int prefix_len
= strlen(prefix
);
461 int total_len
= prefix_len
+ name_len
+ 1;
463 *result
+= total_len
;
465 /* we are just looking for how big our buffer needs to be */
472 memcpy(p
, prefix
, prefix_len
);
473 memcpy(p
+ prefix_len
, name
, name_len
);
474 p
[prefix_len
+ name_len
] = '\0';
479 static int ocfs2_xattr_list_entries(struct inode
*inode
,
480 struct ocfs2_xattr_header
*header
,
481 char *buffer
, size_t buffer_size
)
485 const char *prefix
, *name
;
487 for (i
= 0 ; i
< le16_to_cpu(header
->xh_count
); i
++) {
488 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
489 type
= ocfs2_xattr_get_type(entry
);
490 prefix
= ocfs2_xattr_prefix(type
);
493 name
= (const char *)header
+
494 le16_to_cpu(entry
->xe_name_offset
);
496 ret
= ocfs2_xattr_list_entry(buffer
, buffer_size
,
497 &result
, prefix
, name
,
507 static int ocfs2_xattr_ibody_list(struct inode
*inode
,
508 struct ocfs2_dinode
*di
,
512 struct ocfs2_xattr_header
*header
= NULL
;
513 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
516 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
519 header
= (struct ocfs2_xattr_header
*)
520 ((void *)di
+ inode
->i_sb
->s_blocksize
-
521 le16_to_cpu(di
->i_xattr_inline_size
));
523 ret
= ocfs2_xattr_list_entries(inode
, header
, buffer
, buffer_size
);
528 static int ocfs2_xattr_block_list(struct inode
*inode
,
529 struct ocfs2_dinode
*di
,
533 struct buffer_head
*blk_bh
= NULL
;
534 struct ocfs2_xattr_block
*xb
;
537 if (!di
->i_xattr_loc
)
540 ret
= ocfs2_read_block(inode
, le64_to_cpu(di
->i_xattr_loc
), &blk_bh
);
545 /*Verify the signature of xattr block*/
546 if (memcmp((void *)blk_bh
->b_data
, OCFS2_XATTR_BLOCK_SIGNATURE
,
547 strlen(OCFS2_XATTR_BLOCK_SIGNATURE
))) {
552 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
554 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
555 struct ocfs2_xattr_header
*header
= &xb
->xb_attrs
.xb_header
;
556 ret
= ocfs2_xattr_list_entries(inode
, header
,
557 buffer
, buffer_size
);
559 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
560 ret
= ocfs2_xattr_tree_list_index_block(inode
, xt
,
561 buffer
, buffer_size
);
569 ssize_t
ocfs2_listxattr(struct dentry
*dentry
,
573 int ret
= 0, i_ret
= 0, b_ret
= 0;
574 struct buffer_head
*di_bh
= NULL
;
575 struct ocfs2_dinode
*di
= NULL
;
576 struct ocfs2_inode_info
*oi
= OCFS2_I(dentry
->d_inode
);
578 if (!ocfs2_supports_xattr(OCFS2_SB(dentry
->d_sb
)))
581 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
584 ret
= ocfs2_inode_lock(dentry
->d_inode
, &di_bh
, 0);
590 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
592 down_read(&oi
->ip_xattr_sem
);
593 i_ret
= ocfs2_xattr_ibody_list(dentry
->d_inode
, di
, buffer
, size
);
601 b_ret
= ocfs2_xattr_block_list(dentry
->d_inode
, di
,
606 up_read(&oi
->ip_xattr_sem
);
607 ocfs2_inode_unlock(dentry
->d_inode
, 0);
611 return i_ret
+ b_ret
;
614 static int ocfs2_xattr_find_entry(int name_index
,
616 struct ocfs2_xattr_search
*xs
)
618 struct ocfs2_xattr_entry
*entry
;
625 name_len
= strlen(name
);
627 for (i
= 0; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
628 cmp
= name_index
- ocfs2_xattr_get_type(entry
);
630 cmp
= name_len
- entry
->xe_name_len
;
632 cmp
= memcmp(name
, (xs
->base
+
633 le16_to_cpu(entry
->xe_name_offset
)),
641 return cmp
? -ENODATA
: 0;
644 static int ocfs2_xattr_get_value_outside(struct inode
*inode
,
645 struct ocfs2_xattr_value_root
*xv
,
649 u32 cpos
, p_cluster
, num_clusters
, bpc
, clusters
;
652 size_t cplen
, blocksize
;
653 struct buffer_head
*bh
= NULL
;
654 struct ocfs2_extent_list
*el
;
657 clusters
= le32_to_cpu(xv
->xr_clusters
);
658 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
659 blocksize
= inode
->i_sb
->s_blocksize
;
662 while (cpos
< clusters
) {
663 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
670 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
671 /* Copy ocfs2_xattr_value */
672 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
673 ret
= ocfs2_read_block(inode
, blkno
, &bh
);
679 cplen
= len
>= blocksize
? blocksize
: len
;
680 memcpy(buffer
, bh
->b_data
, cplen
);
689 cpos
+= num_clusters
;
695 static int ocfs2_xattr_ibody_get(struct inode
*inode
,
700 struct ocfs2_xattr_search
*xs
)
702 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
703 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
704 struct ocfs2_xattr_value_root
*xv
;
708 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
))
711 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
712 xs
->header
= (struct ocfs2_xattr_header
*)
713 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
714 xs
->base
= (void *)xs
->header
;
715 xs
->here
= xs
->header
->xh_entries
;
717 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
720 size
= le64_to_cpu(xs
->here
->xe_value_size
);
722 if (size
> buffer_size
)
724 if (ocfs2_xattr_is_local(xs
->here
)) {
725 memcpy(buffer
, (void *)xs
->base
+
726 le16_to_cpu(xs
->here
->xe_name_offset
) +
727 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
), size
);
729 xv
= (struct ocfs2_xattr_value_root
*)
730 (xs
->base
+ le16_to_cpu(
731 xs
->here
->xe_name_offset
) +
732 OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
));
733 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
745 static int ocfs2_xattr_block_get(struct inode
*inode
,
750 struct ocfs2_xattr_search
*xs
)
752 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
753 struct buffer_head
*blk_bh
= NULL
;
754 struct ocfs2_xattr_block
*xb
;
755 struct ocfs2_xattr_value_root
*xv
;
757 int ret
= -ENODATA
, name_offset
, name_len
, block_off
, i
;
759 if (!di
->i_xattr_loc
)
762 memset(&xs
->bucket
, 0, sizeof(xs
->bucket
));
764 ret
= ocfs2_read_block(inode
, le64_to_cpu(di
->i_xattr_loc
), &blk_bh
);
769 /*Verify the signature of xattr block*/
770 if (memcmp((void *)blk_bh
->b_data
, OCFS2_XATTR_BLOCK_SIGNATURE
,
771 strlen(OCFS2_XATTR_BLOCK_SIGNATURE
))) {
776 xs
->xattr_bh
= blk_bh
;
777 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
779 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
780 xs
->header
= &xb
->xb_attrs
.xb_header
;
781 xs
->base
= (void *)xs
->header
;
782 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
783 xs
->here
= xs
->header
->xh_entries
;
785 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
787 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
793 size
= le64_to_cpu(xs
->here
->xe_value_size
);
796 if (size
> buffer_size
)
799 name_offset
= le16_to_cpu(xs
->here
->xe_name_offset
);
800 name_len
= OCFS2_XATTR_SIZE(xs
->here
->xe_name_len
);
801 i
= xs
->here
- xs
->header
->xh_entries
;
803 if (le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
) {
804 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
809 xs
->base
= xs
->bucket
.bhs
[block_off
]->b_data
;
811 if (ocfs2_xattr_is_local(xs
->here
)) {
812 memcpy(buffer
, (void *)xs
->base
+
813 name_offset
+ name_len
, size
);
815 xv
= (struct ocfs2_xattr_value_root
*)
816 (xs
->base
+ name_offset
+ name_len
);
817 ret
= ocfs2_xattr_get_value_outside(inode
, xv
,
827 for (i
= 0; i
< OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET
; i
++)
828 brelse(xs
->bucket
.bhs
[i
]);
829 memset(&xs
->bucket
, 0, sizeof(xs
->bucket
));
837 * Copy an extended attribute into the buffer provided.
838 * Buffer is NULL to compute the size of buffer required.
840 int ocfs2_xattr_get(struct inode
*inode
,
847 struct ocfs2_dinode
*di
= NULL
;
848 struct buffer_head
*di_bh
= NULL
;
849 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
850 struct ocfs2_xattr_search xis
= {
851 .not_found
= -ENODATA
,
853 struct ocfs2_xattr_search xbs
= {
854 .not_found
= -ENODATA
,
857 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
860 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
863 ret
= ocfs2_inode_lock(inode
, &di_bh
, 0);
868 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
869 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
871 down_read(&oi
->ip_xattr_sem
);
872 ret
= ocfs2_xattr_ibody_get(inode
, name_index
, name
, buffer
,
875 ret
= ocfs2_xattr_block_get(inode
, name_index
, name
, buffer
,
877 up_read(&oi
->ip_xattr_sem
);
878 ocfs2_inode_unlock(inode
, 0);
885 static int __ocfs2_xattr_set_value_outside(struct inode
*inode
,
886 struct ocfs2_xattr_value_root
*xv
,
890 int ret
= 0, i
, cp_len
, credits
;
891 u16 blocksize
= inode
->i_sb
->s_blocksize
;
892 u32 p_cluster
, num_clusters
;
893 u32 cpos
= 0, bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
894 u32 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, value_len
);
896 struct buffer_head
*bh
= NULL
;
899 BUG_ON(clusters
> le32_to_cpu(xv
->xr_clusters
));
901 credits
= clusters
* bpc
;
902 handle
= ocfs2_start_trans(OCFS2_SB(inode
->i_sb
), credits
);
903 if (IS_ERR(handle
)) {
904 ret
= PTR_ERR(handle
);
909 while (cpos
< clusters
) {
910 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
911 &num_clusters
, &xv
->xr_list
);
917 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
919 for (i
= 0; i
< num_clusters
* bpc
; i
++, blkno
++) {
920 ret
= ocfs2_read_block(inode
, blkno
, &bh
);
926 ret
= ocfs2_journal_access(handle
,
929 OCFS2_JOURNAL_ACCESS_WRITE
);
935 cp_len
= value_len
> blocksize
? blocksize
: value_len
;
936 memcpy(bh
->b_data
, value
, cp_len
);
939 if (cp_len
< blocksize
)
940 memset(bh
->b_data
+ cp_len
, 0,
943 ret
= ocfs2_journal_dirty(handle
, bh
);
952 * XXX: do we need to empty all the following
953 * blocks in this cluster?
958 cpos
+= num_clusters
;
961 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
968 static int ocfs2_xattr_cleanup(struct inode
*inode
,
969 struct ocfs2_xattr_info
*xi
,
970 struct ocfs2_xattr_search
*xs
,
973 handle_t
*handle
= NULL
;
975 size_t name_len
= strlen(xi
->name
);
976 void *val
= xs
->base
+ offs
;
977 size_t size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
979 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
980 OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
981 if (IS_ERR(handle
)) {
982 ret
= PTR_ERR(handle
);
986 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
987 OCFS2_JOURNAL_ACCESS_WRITE
);
992 /* Decrease xattr count */
993 le16_add_cpu(&xs
->header
->xh_count
, -1);
994 /* Remove the xattr entry and tree root which has already be set*/
995 memset((void *)xs
->here
, 0, sizeof(struct ocfs2_xattr_entry
));
996 memset(val
, 0, size
);
998 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1002 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1007 static int ocfs2_xattr_update_entry(struct inode
*inode
,
1008 struct ocfs2_xattr_info
*xi
,
1009 struct ocfs2_xattr_search
*xs
,
1012 handle_t
*handle
= NULL
;
1015 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1016 OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
1017 if (IS_ERR(handle
)) {
1018 ret
= PTR_ERR(handle
);
1022 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1023 OCFS2_JOURNAL_ACCESS_WRITE
);
1029 xs
->here
->xe_name_offset
= cpu_to_le16(offs
);
1030 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1031 if (xi
->value_len
<= OCFS2_XATTR_INLINE_SIZE
)
1032 ocfs2_xattr_set_local(xs
->here
, 1);
1034 ocfs2_xattr_set_local(xs
->here
, 0);
1035 ocfs2_xattr_hash_entry(inode
, xs
->header
, xs
->here
);
1037 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1041 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1047 * ocfs2_xattr_set_value_outside()
1049 * Set large size value in B tree.
1051 static int ocfs2_xattr_set_value_outside(struct inode
*inode
,
1052 struct ocfs2_xattr_info
*xi
,
1053 struct ocfs2_xattr_search
*xs
,
1056 size_t name_len
= strlen(xi
->name
);
1057 void *val
= xs
->base
+ offs
;
1058 struct ocfs2_xattr_value_root
*xv
= NULL
;
1059 size_t size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1062 memset(val
, 0, size
);
1063 memcpy(val
, xi
->name
, name_len
);
1064 xv
= (struct ocfs2_xattr_value_root
*)
1065 (val
+ OCFS2_XATTR_SIZE(name_len
));
1066 xv
->xr_clusters
= 0;
1067 xv
->xr_last_eb_blk
= 0;
1068 xv
->xr_list
.l_tree_depth
= 0;
1069 xv
->xr_list
.l_count
= cpu_to_le16(1);
1070 xv
->xr_list
.l_next_free_rec
= 0;
1072 ret
= ocfs2_xattr_value_truncate(inode
, xs
->xattr_bh
, xv
,
1078 ret
= __ocfs2_xattr_set_value_outside(inode
, xv
, xi
->value
,
1084 ret
= ocfs2_xattr_update_entry(inode
, xi
, xs
, offs
);
1092 * ocfs2_xattr_set_entry_local()
1094 * Set, replace or remove extended attribute in local.
1096 static void ocfs2_xattr_set_entry_local(struct inode
*inode
,
1097 struct ocfs2_xattr_info
*xi
,
1098 struct ocfs2_xattr_search
*xs
,
1099 struct ocfs2_xattr_entry
*last
,
1102 size_t name_len
= strlen(xi
->name
);
1105 if (xi
->value
&& xs
->not_found
) {
1106 /* Insert the new xattr entry. */
1107 le16_add_cpu(&xs
->header
->xh_count
, 1);
1108 ocfs2_xattr_set_type(last
, xi
->name_index
);
1109 ocfs2_xattr_set_local(last
, 1);
1110 last
->xe_name_len
= name_len
;
1116 first_val
= xs
->base
+ min_offs
;
1117 offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1118 val
= xs
->base
+ offs
;
1120 if (le64_to_cpu(xs
->here
->xe_value_size
) >
1121 OCFS2_XATTR_INLINE_SIZE
)
1122 size
= OCFS2_XATTR_SIZE(name_len
) +
1123 OCFS2_XATTR_ROOT_SIZE
;
1125 size
= OCFS2_XATTR_SIZE(name_len
) +
1126 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1128 if (xi
->value
&& size
== OCFS2_XATTR_SIZE(name_len
) +
1129 OCFS2_XATTR_SIZE(xi
->value_len
)) {
1130 /* The old and the new value have the
1131 same size. Just replace the value. */
1132 ocfs2_xattr_set_local(xs
->here
, 1);
1133 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1134 /* Clear value bytes. */
1135 memset(val
+ OCFS2_XATTR_SIZE(name_len
),
1137 OCFS2_XATTR_SIZE(xi
->value_len
));
1138 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
1143 /* Remove the old name+value. */
1144 memmove(first_val
+ size
, first_val
, val
- first_val
);
1145 memset(first_val
, 0, size
);
1146 xs
->here
->xe_name_hash
= 0;
1147 xs
->here
->xe_name_offset
= 0;
1148 ocfs2_xattr_set_local(xs
->here
, 1);
1149 xs
->here
->xe_value_size
= 0;
1153 /* Adjust all value offsets. */
1154 last
= xs
->header
->xh_entries
;
1155 for (i
= 0 ; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1156 size_t o
= le16_to_cpu(last
->xe_name_offset
);
1159 last
->xe_name_offset
= cpu_to_le16(o
+ size
);
1164 /* Remove the old entry. */
1166 memmove(xs
->here
, xs
->here
+ 1,
1167 (void *)last
- (void *)xs
->here
);
1168 memset(last
, 0, sizeof(struct ocfs2_xattr_entry
));
1169 le16_add_cpu(&xs
->header
->xh_count
, -1);
1173 /* Insert the new name+value. */
1174 size_t size
= OCFS2_XATTR_SIZE(name_len
) +
1175 OCFS2_XATTR_SIZE(xi
->value_len
);
1176 void *val
= xs
->base
+ min_offs
- size
;
1178 xs
->here
->xe_name_offset
= cpu_to_le16(min_offs
- size
);
1179 memset(val
, 0, size
);
1180 memcpy(val
, xi
->name
, name_len
);
1181 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
1184 xs
->here
->xe_value_size
= cpu_to_le64(xi
->value_len
);
1185 ocfs2_xattr_set_local(xs
->here
, 1);
1186 ocfs2_xattr_hash_entry(inode
, xs
->header
, xs
->here
);
1193 * ocfs2_xattr_set_entry()
1195 * Set extended attribute entry into inode or block.
1197 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1198 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1199 * then set value in B tree with set_value_outside().
1201 static int ocfs2_xattr_set_entry(struct inode
*inode
,
1202 struct ocfs2_xattr_info
*xi
,
1203 struct ocfs2_xattr_search
*xs
,
1206 struct ocfs2_xattr_entry
*last
;
1207 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1208 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1209 size_t min_offs
= xs
->end
- xs
->base
, name_len
= strlen(xi
->name
);
1211 handle_t
*handle
= NULL
;
1213 struct ocfs2_xattr_info xi_l
= {
1214 .name_index
= xi
->name_index
,
1217 .value_len
= xi
->value_len
,
1220 /* Compute min_offs, last and free space. */
1221 last
= xs
->header
->xh_entries
;
1223 for (i
= 0 ; i
< le16_to_cpu(xs
->header
->xh_count
); i
++) {
1224 size_t offs
= le16_to_cpu(last
->xe_name_offset
);
1225 if (offs
< min_offs
)
1230 free
= min_offs
- ((void *)last
- xs
->base
) - sizeof(__u32
);
1234 if (!xs
->not_found
) {
1236 if (ocfs2_xattr_is_local(xs
->here
))
1237 size
= OCFS2_XATTR_SIZE(name_len
) +
1238 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1240 size
= OCFS2_XATTR_SIZE(name_len
) +
1241 OCFS2_XATTR_ROOT_SIZE
;
1242 free
+= (size
+ sizeof(struct ocfs2_xattr_entry
));
1244 /* Check free space in inode or block */
1245 if (xi
->value
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1246 if (free
< sizeof(struct ocfs2_xattr_entry
) +
1247 OCFS2_XATTR_SIZE(name_len
) +
1248 OCFS2_XATTR_ROOT_SIZE
) {
1252 size_l
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_ROOT_SIZE
;
1253 xi_l
.value
= (void *)&def_xv
;
1254 xi_l
.value_len
= OCFS2_XATTR_ROOT_SIZE
;
1255 } else if (xi
->value
) {
1256 if (free
< sizeof(struct ocfs2_xattr_entry
) +
1257 OCFS2_XATTR_SIZE(name_len
) +
1258 OCFS2_XATTR_SIZE(xi
->value_len
)) {
1264 if (!xs
->not_found
) {
1265 /* For existing extended attribute */
1266 size_t size
= OCFS2_XATTR_SIZE(name_len
) +
1267 OCFS2_XATTR_SIZE(le64_to_cpu(xs
->here
->xe_value_size
));
1268 size_t offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1269 void *val
= xs
->base
+ offs
;
1271 if (ocfs2_xattr_is_local(xs
->here
) && size
== size_l
) {
1272 /* Replace existing local xattr with tree root */
1273 ret
= ocfs2_xattr_set_value_outside(inode
, xi
, xs
,
1278 } else if (!ocfs2_xattr_is_local(xs
->here
)) {
1279 /* For existing xattr which has value outside */
1280 struct ocfs2_xattr_value_root
*xv
= NULL
;
1281 xv
= (struct ocfs2_xattr_value_root
*)(val
+
1282 OCFS2_XATTR_SIZE(name_len
));
1284 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1286 * If new value need set outside also,
1287 * first truncate old value to new value,
1288 * then set new value with set_value_outside().
1290 ret
= ocfs2_xattr_value_truncate(inode
,
1299 ret
= __ocfs2_xattr_set_value_outside(inode
,
1308 ret
= ocfs2_xattr_update_entry(inode
,
1317 * If new value need set in local,
1318 * just trucate old value to zero.
1320 ret
= ocfs2_xattr_value_truncate(inode
,
1330 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1331 OCFS2_INODE_UPDATE_CREDITS
);
1332 if (IS_ERR(handle
)) {
1333 ret
= PTR_ERR(handle
);
1338 ret
= ocfs2_journal_access(handle
, inode
, xs
->inode_bh
,
1339 OCFS2_JOURNAL_ACCESS_WRITE
);
1345 if (!(flag
& OCFS2_INLINE_XATTR_FL
)) {
1346 /* set extended attribute in external block. */
1347 ret
= ocfs2_extend_trans(handle
,
1348 OCFS2_INODE_UPDATE_CREDITS
+
1349 OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
1354 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1355 OCFS2_JOURNAL_ACCESS_WRITE
);
1363 * Set value in local, include set tree root in local.
1364 * This is the first step for value size >INLINE_SIZE.
1366 ocfs2_xattr_set_entry_local(inode
, &xi_l
, xs
, last
, min_offs
);
1368 if (!(flag
& OCFS2_INLINE_XATTR_FL
)) {
1369 ret
= ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1376 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) &&
1377 (flag
& OCFS2_INLINE_XATTR_FL
)) {
1378 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1379 unsigned int xattrsize
= osb
->s_xattr_inline_size
;
1382 * Adjust extent record count or inline data size
1383 * to reserve space for extended attribute.
1385 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1386 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
1387 le16_add_cpu(&idata
->id_count
, -xattrsize
);
1388 } else if (!(ocfs2_inode_is_fast_symlink(inode
))) {
1389 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
1390 le16_add_cpu(&el
->l_count
, -(xattrsize
/
1391 sizeof(struct ocfs2_extent_rec
)));
1393 di
->i_xattr_inline_size
= cpu_to_le16(xattrsize
);
1395 /* Update xattr flag */
1396 spin_lock(&oi
->ip_lock
);
1397 oi
->ip_dyn_features
|= flag
;
1398 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1399 spin_unlock(&oi
->ip_lock
);
1400 /* Update inode ctime */
1401 inode
->i_ctime
= CURRENT_TIME
;
1402 di
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
1403 di
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
1405 ret
= ocfs2_journal_dirty(handle
, xs
->inode_bh
);
1410 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1412 if (!ret
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
) {
1414 * Set value outside in B tree.
1415 * This is the second step for value size > INLINE_SIZE.
1417 size_t offs
= le16_to_cpu(xs
->here
->xe_name_offset
);
1418 ret
= ocfs2_xattr_set_value_outside(inode
, xi
, xs
, offs
);
1424 * If set value outside failed, we have to clean
1425 * the junk tree root we have already set in local.
1427 ret2
= ocfs2_xattr_cleanup(inode
, xi
, xs
, offs
);
1437 static int ocfs2_remove_value_outside(struct inode
*inode
,
1438 struct buffer_head
*bh
,
1439 struct ocfs2_xattr_header
*header
)
1443 for (i
= 0; i
< le16_to_cpu(header
->xh_count
); i
++) {
1444 struct ocfs2_xattr_entry
*entry
= &header
->xh_entries
[i
];
1446 if (!ocfs2_xattr_is_local(entry
)) {
1447 struct ocfs2_xattr_value_root
*xv
;
1450 val
= (void *)header
+
1451 le16_to_cpu(entry
->xe_name_offset
);
1452 xv
= (struct ocfs2_xattr_value_root
*)
1453 (val
+ OCFS2_XATTR_SIZE(entry
->xe_name_len
));
1454 ret
= ocfs2_xattr_value_truncate(inode
, bh
, xv
, 0);
1465 static int ocfs2_xattr_ibody_remove(struct inode
*inode
,
1466 struct buffer_head
*di_bh
)
1469 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1470 struct ocfs2_xattr_header
*header
;
1473 header
= (struct ocfs2_xattr_header
*)
1474 ((void *)di
+ inode
->i_sb
->s_blocksize
-
1475 le16_to_cpu(di
->i_xattr_inline_size
));
1477 ret
= ocfs2_remove_value_outside(inode
, di_bh
, header
);
1482 static int ocfs2_xattr_block_remove(struct inode
*inode
,
1483 struct buffer_head
*blk_bh
)
1485 struct ocfs2_xattr_block
*xb
;
1488 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1489 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1490 struct ocfs2_xattr_header
*header
= &(xb
->xb_attrs
.xb_header
);
1491 ret
= ocfs2_remove_value_outside(inode
, blk_bh
, header
);
1493 ret
= ocfs2_delete_xattr_index_block(inode
, blk_bh
);
1498 static int ocfs2_xattr_free_block(struct inode
*inode
,
1501 struct inode
*xb_alloc_inode
;
1502 struct buffer_head
*xb_alloc_bh
= NULL
;
1503 struct buffer_head
*blk_bh
= NULL
;
1504 struct ocfs2_xattr_block
*xb
;
1505 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1511 ret
= ocfs2_read_block(inode
, block
, &blk_bh
);
1517 /*Verify the signature of xattr block*/
1518 if (memcmp((void *)blk_bh
->b_data
, OCFS2_XATTR_BLOCK_SIGNATURE
,
1519 strlen(OCFS2_XATTR_BLOCK_SIGNATURE
))) {
1524 ret
= ocfs2_xattr_block_remove(inode
, blk_bh
);
1530 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1531 blk
= le64_to_cpu(xb
->xb_blkno
);
1532 bit
= le16_to_cpu(xb
->xb_suballoc_bit
);
1533 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
1535 xb_alloc_inode
= ocfs2_get_system_file_inode(osb
,
1536 EXTENT_ALLOC_SYSTEM_INODE
,
1537 le16_to_cpu(xb
->xb_suballoc_slot
));
1538 if (!xb_alloc_inode
) {
1543 mutex_lock(&xb_alloc_inode
->i_mutex
);
1545 ret
= ocfs2_inode_lock(xb_alloc_inode
, &xb_alloc_bh
, 1);
1551 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
1552 if (IS_ERR(handle
)) {
1553 ret
= PTR_ERR(handle
);
1558 ret
= ocfs2_free_suballoc_bits(handle
, xb_alloc_inode
, xb_alloc_bh
,
1563 ocfs2_commit_trans(osb
, handle
);
1565 ocfs2_inode_unlock(xb_alloc_inode
, 1);
1566 brelse(xb_alloc_bh
);
1568 mutex_unlock(&xb_alloc_inode
->i_mutex
);
1569 iput(xb_alloc_inode
);
1576 * ocfs2_xattr_remove()
1578 * Free extended attribute resources associated with this inode.
1580 int ocfs2_xattr_remove(struct inode
*inode
, struct buffer_head
*di_bh
)
1582 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1583 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1587 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
1590 if (!(oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
))
1593 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
1594 ret
= ocfs2_xattr_ibody_remove(inode
, di_bh
);
1601 if (di
->i_xattr_loc
) {
1602 ret
= ocfs2_xattr_free_block(inode
,
1603 le64_to_cpu(di
->i_xattr_loc
));
1610 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)),
1611 OCFS2_INODE_UPDATE_CREDITS
);
1612 if (IS_ERR(handle
)) {
1613 ret
= PTR_ERR(handle
);
1617 ret
= ocfs2_journal_access(handle
, inode
, di_bh
,
1618 OCFS2_JOURNAL_ACCESS_WRITE
);
1624 di
->i_xattr_loc
= 0;
1626 spin_lock(&oi
->ip_lock
);
1627 oi
->ip_dyn_features
&= ~(OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
);
1628 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
1629 spin_unlock(&oi
->ip_lock
);
1631 ret
= ocfs2_journal_dirty(handle
, di_bh
);
1635 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
1640 static int ocfs2_xattr_has_space_inline(struct inode
*inode
,
1641 struct ocfs2_dinode
*di
)
1643 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1644 unsigned int xattrsize
= OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
;
1647 if (xattrsize
< OCFS2_MIN_XATTR_INLINE_SIZE
)
1650 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1651 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
1652 free
= le16_to_cpu(idata
->id_count
) - le64_to_cpu(di
->i_size
);
1653 } else if (ocfs2_inode_is_fast_symlink(inode
)) {
1654 free
= ocfs2_fast_symlink_chars(inode
->i_sb
) -
1655 le64_to_cpu(di
->i_size
);
1657 struct ocfs2_extent_list
*el
= &di
->id2
.i_list
;
1658 free
= (le16_to_cpu(el
->l_count
) -
1659 le16_to_cpu(el
->l_next_free_rec
)) *
1660 sizeof(struct ocfs2_extent_rec
);
1662 if (free
>= xattrsize
)
1669 * ocfs2_xattr_ibody_find()
1671 * Find extended attribute in inode block and
1672 * fill search info into struct ocfs2_xattr_search.
1674 static int ocfs2_xattr_ibody_find(struct inode
*inode
,
1677 struct ocfs2_xattr_search
*xs
)
1679 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1680 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1684 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
1687 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
1688 down_read(&oi
->ip_alloc_sem
);
1689 has_space
= ocfs2_xattr_has_space_inline(inode
, di
);
1690 up_read(&oi
->ip_alloc_sem
);
1695 xs
->xattr_bh
= xs
->inode_bh
;
1696 xs
->end
= (void *)di
+ inode
->i_sb
->s_blocksize
;
1697 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)
1698 xs
->header
= (struct ocfs2_xattr_header
*)
1699 (xs
->end
- le16_to_cpu(di
->i_xattr_inline_size
));
1701 xs
->header
= (struct ocfs2_xattr_header
*)
1702 (xs
->end
- OCFS2_SB(inode
->i_sb
)->s_xattr_inline_size
);
1703 xs
->base
= (void *)xs
->header
;
1704 xs
->here
= xs
->header
->xh_entries
;
1706 /* Find the named attribute. */
1707 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
) {
1708 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1709 if (ret
&& ret
!= -ENODATA
)
1711 xs
->not_found
= ret
;
1718 * ocfs2_xattr_ibody_set()
1720 * Set, replace or remove an extended attribute into inode block.
1723 static int ocfs2_xattr_ibody_set(struct inode
*inode
,
1724 struct ocfs2_xattr_info
*xi
,
1725 struct ocfs2_xattr_search
*xs
)
1727 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1728 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1731 if (inode
->i_sb
->s_blocksize
== OCFS2_MIN_BLOCKSIZE
)
1734 down_write(&oi
->ip_alloc_sem
);
1735 if (!(oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
)) {
1736 if (!ocfs2_xattr_has_space_inline(inode
, di
)) {
1742 ret
= ocfs2_xattr_set_entry(inode
, xi
, xs
,
1743 (OCFS2_INLINE_XATTR_FL
| OCFS2_HAS_XATTR_FL
));
1745 up_write(&oi
->ip_alloc_sem
);
1751 * ocfs2_xattr_block_find()
1753 * Find extended attribute in external block and
1754 * fill search info into struct ocfs2_xattr_search.
1756 static int ocfs2_xattr_block_find(struct inode
*inode
,
1759 struct ocfs2_xattr_search
*xs
)
1761 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1762 struct buffer_head
*blk_bh
= NULL
;
1763 struct ocfs2_xattr_block
*xb
;
1766 if (!di
->i_xattr_loc
)
1769 ret
= ocfs2_read_block(inode
, le64_to_cpu(di
->i_xattr_loc
), &blk_bh
);
1774 /*Verify the signature of xattr block*/
1775 if (memcmp((void *)blk_bh
->b_data
, OCFS2_XATTR_BLOCK_SIGNATURE
,
1776 strlen(OCFS2_XATTR_BLOCK_SIGNATURE
))) {
1781 xs
->xattr_bh
= blk_bh
;
1782 xb
= (struct ocfs2_xattr_block
*)blk_bh
->b_data
;
1784 if (!(le16_to_cpu(xb
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1785 xs
->header
= &xb
->xb_attrs
.xb_header
;
1786 xs
->base
= (void *)xs
->header
;
1787 xs
->end
= (void *)(blk_bh
->b_data
) + blk_bh
->b_size
;
1788 xs
->here
= xs
->header
->xh_entries
;
1790 ret
= ocfs2_xattr_find_entry(name_index
, name
, xs
);
1792 ret
= ocfs2_xattr_index_block_find(inode
, blk_bh
,
1796 if (ret
&& ret
!= -ENODATA
) {
1797 xs
->xattr_bh
= NULL
;
1800 xs
->not_found
= ret
;
1809 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1810 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1813 static int ocfs2_restore_xattr_block(struct inode
*inode
,
1814 struct ocfs2_xattr_search
*xs
)
1818 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1819 struct ocfs2_xattr_block
*xb
=
1820 (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
1821 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
1822 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
1824 BUG_ON(!(xb_flags
& OCFS2_XATTR_INDEXED
) ||
1825 le16_to_cpu(el
->l_next_free_rec
) != 0);
1827 handle
= ocfs2_start_trans(osb
, OCFS2_XATTR_BLOCK_UPDATE_CREDITS
);
1828 if (IS_ERR(handle
)) {
1829 ret
= PTR_ERR(handle
);
1834 ret
= ocfs2_journal_access(handle
, inode
, xs
->xattr_bh
,
1835 OCFS2_JOURNAL_ACCESS_WRITE
);
1841 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
1842 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
1844 xb
->xb_flags
= cpu_to_le16(xb_flags
& ~OCFS2_XATTR_INDEXED
);
1846 ocfs2_journal_dirty(handle
, xs
->xattr_bh
);
1849 ocfs2_commit_trans(osb
, handle
);
1855 * ocfs2_xattr_block_set()
1857 * Set, replace or remove an extended attribute into external block.
1860 static int ocfs2_xattr_block_set(struct inode
*inode
,
1861 struct ocfs2_xattr_info
*xi
,
1862 struct ocfs2_xattr_search
*xs
)
1864 struct buffer_head
*new_bh
= NULL
;
1865 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1866 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)xs
->inode_bh
->b_data
;
1867 struct ocfs2_alloc_context
*meta_ac
= NULL
;
1868 handle_t
*handle
= NULL
;
1869 struct ocfs2_xattr_block
*xblk
= NULL
;
1870 u16 suballoc_bit_start
;
1875 if (!xs
->xattr_bh
) {
1877 * Alloc one external block for extended attribute
1880 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, &meta_ac
);
1885 handle
= ocfs2_start_trans(osb
,
1886 OCFS2_XATTR_BLOCK_CREATE_CREDITS
);
1887 if (IS_ERR(handle
)) {
1888 ret
= PTR_ERR(handle
);
1892 ret
= ocfs2_journal_access(handle
, inode
, xs
->inode_bh
,
1893 OCFS2_JOURNAL_ACCESS_CREATE
);
1899 ret
= ocfs2_claim_metadata(osb
, handle
, meta_ac
, 1,
1900 &suballoc_bit_start
, &num_got
,
1907 new_bh
= sb_getblk(inode
->i_sb
, first_blkno
);
1908 ocfs2_set_new_buffer_uptodate(inode
, new_bh
);
1910 ret
= ocfs2_journal_access(handle
, inode
, new_bh
,
1911 OCFS2_JOURNAL_ACCESS_CREATE
);
1917 /* Initialize ocfs2_xattr_block */
1918 xs
->xattr_bh
= new_bh
;
1919 xblk
= (struct ocfs2_xattr_block
*)new_bh
->b_data
;
1920 memset(xblk
, 0, inode
->i_sb
->s_blocksize
);
1921 strcpy((void *)xblk
, OCFS2_XATTR_BLOCK_SIGNATURE
);
1922 xblk
->xb_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
1923 xblk
->xb_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
1924 xblk
->xb_fs_generation
= cpu_to_le32(osb
->fs_generation
);
1925 xblk
->xb_blkno
= cpu_to_le64(first_blkno
);
1927 xs
->header
= &xblk
->xb_attrs
.xb_header
;
1928 xs
->base
= (void *)xs
->header
;
1929 xs
->end
= (void *)xblk
+ inode
->i_sb
->s_blocksize
;
1930 xs
->here
= xs
->header
->xh_entries
;
1933 ret
= ocfs2_journal_dirty(handle
, new_bh
);
1938 di
->i_xattr_loc
= cpu_to_le64(first_blkno
);
1939 ret
= ocfs2_journal_dirty(handle
, xs
->inode_bh
);
1943 ocfs2_commit_trans(osb
, handle
);
1946 ocfs2_free_alloc_context(meta_ac
);
1950 xblk
= (struct ocfs2_xattr_block
*)xs
->xattr_bh
->b_data
;
1952 if (!(le16_to_cpu(xblk
->xb_flags
) & OCFS2_XATTR_INDEXED
)) {
1953 /* Set extended attribute into external block */
1954 ret
= ocfs2_xattr_set_entry(inode
, xi
, xs
, OCFS2_HAS_XATTR_FL
);
1955 if (!ret
|| ret
!= -ENOSPC
)
1958 ret
= ocfs2_xattr_create_index_block(inode
, xs
);
1963 ret
= ocfs2_xattr_set_entry_index_block(inode
, xi
, xs
);
1964 if (!ret
&& xblk
->xb_attrs
.xb_root
.xt_list
.l_next_free_rec
== 0)
1965 ret
= ocfs2_restore_xattr_block(inode
, xs
);
1975 * Set, replace or remove an extended attribute for this inode.
1976 * value is NULL to remove an existing extended attribute, else either
1977 * create or replace an extended attribute.
1979 int ocfs2_xattr_set(struct inode
*inode
,
1986 struct buffer_head
*di_bh
= NULL
;
1987 struct ocfs2_dinode
*di
;
1989 u16 i
, blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
1991 struct ocfs2_xattr_info xi
= {
1992 .name_index
= name_index
,
1995 .value_len
= value_len
,
1998 struct ocfs2_xattr_search xis
= {
1999 .not_found
= -ENODATA
,
2002 struct ocfs2_xattr_search xbs
= {
2003 .not_found
= -ENODATA
,
2006 if (!ocfs2_supports_xattr(OCFS2_SB(inode
->i_sb
)))
2009 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
2014 xis
.inode_bh
= xbs
.inode_bh
= di_bh
;
2015 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2017 down_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2019 * Scan inode and external block to find the same name
2020 * extended attribute and collect search infomation.
2022 ret
= ocfs2_xattr_ibody_find(inode
, name_index
, name
, &xis
);
2025 if (xis
.not_found
) {
2026 ret
= ocfs2_xattr_block_find(inode
, name_index
, name
, &xbs
);
2031 if (xis
.not_found
&& xbs
.not_found
) {
2033 if (flags
& XATTR_REPLACE
)
2040 if (flags
& XATTR_CREATE
)
2045 /* Remove existing extended attribute */
2047 ret
= ocfs2_xattr_ibody_set(inode
, &xi
, &xis
);
2048 else if (!xbs
.not_found
)
2049 ret
= ocfs2_xattr_block_set(inode
, &xi
, &xbs
);
2051 /* We always try to set extended attribute into inode first*/
2052 ret
= ocfs2_xattr_ibody_set(inode
, &xi
, &xis
);
2053 if (!ret
&& !xbs
.not_found
) {
2055 * If succeed and that extended attribute existing in
2056 * external block, then we will remove it.
2060 ret
= ocfs2_xattr_block_set(inode
, &xi
, &xbs
);
2061 } else if (ret
== -ENOSPC
) {
2062 if (di
->i_xattr_loc
&& !xbs
.xattr_bh
) {
2063 ret
= ocfs2_xattr_block_find(inode
, name_index
,
2069 * If no space in inode, we will set extended attribute
2070 * into external block.
2072 ret
= ocfs2_xattr_block_set(inode
, &xi
, &xbs
);
2075 if (!xis
.not_found
) {
2077 * If succeed and that extended attribute
2078 * existing in inode, we will remove it.
2082 ret
= ocfs2_xattr_ibody_set(inode
, &xi
, &xis
);
2087 up_write(&OCFS2_I(inode
)->ip_xattr_sem
);
2088 ocfs2_inode_unlock(inode
, 1);
2090 brelse(xbs
.xattr_bh
);
2091 for (i
= 0; i
< blk_per_bucket
; i
++)
2092 brelse(xbs
.bucket
.bhs
[i
]);
2098 * Find the xattr extent rec which may contains name_hash.
2099 * e_cpos will be the first name hash of the xattr rec.
2100 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2102 static int ocfs2_xattr_get_rec(struct inode
*inode
,
2107 struct ocfs2_extent_list
*el
)
2110 struct buffer_head
*eb_bh
= NULL
;
2111 struct ocfs2_extent_block
*eb
;
2112 struct ocfs2_extent_rec
*rec
= NULL
;
2115 if (el
->l_tree_depth
) {
2116 ret
= ocfs2_find_leaf(inode
, el
, name_hash
, &eb_bh
);
2122 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
2125 if (el
->l_tree_depth
) {
2126 ocfs2_error(inode
->i_sb
,
2127 "Inode %lu has non zero tree depth in "
2128 "xattr tree block %llu\n", inode
->i_ino
,
2129 (unsigned long long)eb_bh
->b_blocknr
);
2135 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
2136 rec
= &el
->l_recs
[i
];
2138 if (le32_to_cpu(rec
->e_cpos
) <= name_hash
) {
2139 e_blkno
= le64_to_cpu(rec
->e_blkno
);
2145 ocfs2_error(inode
->i_sb
, "Inode %lu has bad extent "
2146 "record (%u, %u, 0) in xattr", inode
->i_ino
,
2147 le32_to_cpu(rec
->e_cpos
),
2148 ocfs2_rec_clusters(el
, rec
));
2153 *p_blkno
= le64_to_cpu(rec
->e_blkno
);
2154 *num_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
2156 *e_cpos
= le32_to_cpu(rec
->e_cpos
);
2162 typedef int (xattr_bucket_func
)(struct inode
*inode
,
2163 struct ocfs2_xattr_bucket
*bucket
,
2166 static int ocfs2_find_xe_in_bucket(struct inode
*inode
,
2167 struct buffer_head
*header_bh
,
2174 int i
, ret
= 0, cmp
= 1, block_off
, new_offset
;
2175 struct ocfs2_xattr_header
*xh
=
2176 (struct ocfs2_xattr_header
*)header_bh
->b_data
;
2177 size_t name_len
= strlen(name
);
2178 struct ocfs2_xattr_entry
*xe
= NULL
;
2179 struct buffer_head
*name_bh
= NULL
;
2183 * We don't use binary search in the bucket because there
2184 * may be multiple entries with the same name hash.
2186 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
2187 xe
= &xh
->xh_entries
[i
];
2189 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
))
2191 else if (name_hash
< le32_to_cpu(xe
->xe_name_hash
))
2194 cmp
= name_index
- ocfs2_xattr_get_type(xe
);
2196 cmp
= name_len
- xe
->xe_name_len
;
2200 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
2210 ret
= ocfs2_read_block(inode
, header_bh
->b_blocknr
+ block_off
,
2216 xe_name
= name_bh
->b_data
+ new_offset
;
2218 cmp
= memcmp(name
, xe_name
, name_len
);
2234 * Find the specified xattr entry in a series of buckets.
2235 * This series start from p_blkno and last for num_clusters.
2236 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2237 * the num of the valid buckets.
2239 * Return the buffer_head this xattr should reside in. And if the xattr's
2240 * hash is in the gap of 2 buckets, return the lower bucket.
2242 static int ocfs2_xattr_bucket_find(struct inode
*inode
,
2249 struct ocfs2_xattr_search
*xs
)
2252 struct buffer_head
*bh
= NULL
;
2253 struct buffer_head
*lower_bh
= NULL
;
2254 struct ocfs2_xattr_header
*xh
= NULL
;
2255 struct ocfs2_xattr_entry
*xe
= NULL
;
2257 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2258 int low_bucket
= 0, bucket
, high_bucket
;
2262 ret
= ocfs2_read_block(inode
, p_blkno
, &bh
);
2268 xh
= (struct ocfs2_xattr_header
*)bh
->b_data
;
2269 high_bucket
= le16_to_cpu(xh
->xh_num_buckets
) - 1;
2271 while (low_bucket
<= high_bucket
) {
2274 bucket
= (low_bucket
+ high_bucket
) / 2;
2276 blkno
= p_blkno
+ bucket
* blk_per_bucket
;
2278 ret
= ocfs2_read_block(inode
, blkno
, &bh
);
2284 xh
= (struct ocfs2_xattr_header
*)bh
->b_data
;
2285 xe
= &xh
->xh_entries
[0];
2286 if (name_hash
< le32_to_cpu(xe
->xe_name_hash
)) {
2287 high_bucket
= bucket
- 1;
2292 * Check whether the hash of the last entry in our
2293 * bucket is larger than the search one. for an empty
2294 * bucket, the last one is also the first one.
2297 xe
= &xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1];
2299 last_hash
= le32_to_cpu(xe
->xe_name_hash
);
2301 /* record lower_bh which may be the insert place. */
2306 if (name_hash
> le32_to_cpu(xe
->xe_name_hash
)) {
2307 low_bucket
= bucket
+ 1;
2311 /* the searched xattr should reside in this bucket if exists. */
2312 ret
= ocfs2_find_xe_in_bucket(inode
, lower_bh
,
2313 name_index
, name
, name_hash
,
2323 * Record the bucket we have found.
2324 * When the xattr's hash value is in the gap of 2 buckets, we will
2325 * always set it to the previous bucket.
2329 * We can't find any bucket whose first name_hash is less
2330 * than the find name_hash.
2332 BUG_ON(bh
->b_blocknr
!= p_blkno
);
2336 xs
->bucket
.bhs
[0] = lower_bh
;
2337 xs
->bucket
.xh
= (struct ocfs2_xattr_header
*)
2338 xs
->bucket
.bhs
[0]->b_data
;
2341 xs
->header
= xs
->bucket
.xh
;
2342 xs
->base
= xs
->bucket
.bhs
[0]->b_data
;
2343 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
2347 * If we have found the xattr enty, read all the blocks in
2350 ret
= ocfs2_read_blocks(inode
, xs
->bucket
.bhs
[0]->b_blocknr
+ 1,
2351 blk_per_bucket
- 1, &xs
->bucket
.bhs
[1],
2358 xs
->here
= &xs
->header
->xh_entries
[index
];
2359 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name
,
2360 (unsigned long long)xs
->bucket
.bhs
[0]->b_blocknr
, index
);
2370 static int ocfs2_xattr_index_block_find(struct inode
*inode
,
2371 struct buffer_head
*root_bh
,
2374 struct ocfs2_xattr_search
*xs
)
2377 struct ocfs2_xattr_block
*xb
=
2378 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
2379 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
2380 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
2382 u32 first_hash
, num_clusters
= 0;
2383 u32 name_hash
= ocfs2_xattr_name_hash(inode
, name
, strlen(name
));
2385 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
2388 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2389 name
, name_hash
, name_index
);
2391 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &first_hash
,
2398 BUG_ON(p_blkno
== 0 || num_clusters
== 0 || first_hash
> name_hash
);
2400 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2401 "in the rec is %u\n", num_clusters
, p_blkno
, first_hash
);
2403 ret
= ocfs2_xattr_bucket_find(inode
, name_index
, name
, name_hash
,
2404 p_blkno
, first_hash
, num_clusters
, xs
);
2410 static int ocfs2_iterate_xattr_buckets(struct inode
*inode
,
2413 xattr_bucket_func
*func
,
2417 int blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2418 u32 bpc
= ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
));
2419 u32 num_buckets
= clusters
* bpc
;
2420 struct ocfs2_xattr_bucket bucket
;
2422 memset(&bucket
, 0, sizeof(bucket
));
2424 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2427 for (i
= 0; i
< num_buckets
; i
++, blkno
+= blk_per_bucket
) {
2428 ret
= ocfs2_read_blocks(inode
, blkno
, blk_per_bucket
,
2429 bucket
.bhs
, OCFS2_BH_CACHED
);
2435 bucket
.xh
= (struct ocfs2_xattr_header
*)bucket
.bhs
[0]->b_data
;
2437 * The real bucket num in this series of blocks is stored
2438 * in the 1st bucket.
2441 num_buckets
= le16_to_cpu(bucket
.xh
->xh_num_buckets
);
2443 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno
,
2444 le32_to_cpu(bucket
.xh
->xh_entries
[0].xe_name_hash
));
2446 ret
= func(inode
, &bucket
, para
);
2453 for (j
= 0; j
< blk_per_bucket
; j
++)
2454 brelse(bucket
.bhs
[j
]);
2455 memset(&bucket
, 0, sizeof(bucket
));
2459 for (j
= 0; j
< blk_per_bucket
; j
++)
2460 brelse(bucket
.bhs
[j
]);
2465 struct ocfs2_xattr_tree_list
{
2471 static int ocfs2_xattr_bucket_get_name_value(struct inode
*inode
,
2472 struct ocfs2_xattr_header
*xh
,
2479 if (index
< 0 || index
>= le16_to_cpu(xh
->xh_count
))
2482 name_offset
= le16_to_cpu(xh
->xh_entries
[index
].xe_name_offset
);
2484 *block_off
= name_offset
>> inode
->i_sb
->s_blocksize_bits
;
2485 *new_offset
= name_offset
% inode
->i_sb
->s_blocksize
;
2490 static int ocfs2_list_xattr_bucket(struct inode
*inode
,
2491 struct ocfs2_xattr_bucket
*bucket
,
2495 struct ocfs2_xattr_tree_list
*xl
= (struct ocfs2_xattr_tree_list
*)para
;
2496 int i
, block_off
, new_offset
;
2497 const char *prefix
, *name
;
2499 for (i
= 0 ; i
< le16_to_cpu(bucket
->xh
->xh_count
); i
++) {
2500 struct ocfs2_xattr_entry
*entry
= &bucket
->xh
->xh_entries
[i
];
2501 type
= ocfs2_xattr_get_type(entry
);
2502 prefix
= ocfs2_xattr_prefix(type
);
2505 ret
= ocfs2_xattr_bucket_get_name_value(inode
,
2513 name
= (const char *)bucket
->bhs
[block_off
]->b_data
+
2515 ret
= ocfs2_xattr_list_entry(xl
->buffer
,
2519 entry
->xe_name_len
);
2528 static int ocfs2_xattr_tree_list_index_block(struct inode
*inode
,
2529 struct ocfs2_xattr_tree_root
*xt
,
2533 struct ocfs2_extent_list
*el
= &xt
->xt_list
;
2535 u32 name_hash
= UINT_MAX
, e_cpos
= 0, num_clusters
= 0;
2537 struct ocfs2_xattr_tree_list xl
= {
2539 .buffer_size
= buffer_size
,
2543 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
2546 while (name_hash
> 0) {
2547 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
2548 &e_cpos
, &num_clusters
, el
);
2554 ret
= ocfs2_iterate_xattr_buckets(inode
, p_blkno
, num_clusters
,
2555 ocfs2_list_xattr_bucket
,
2565 name_hash
= e_cpos
- 1;
2573 static int cmp_xe(const void *a
, const void *b
)
2575 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
2576 u32 l_hash
= le32_to_cpu(l
->xe_name_hash
);
2577 u32 r_hash
= le32_to_cpu(r
->xe_name_hash
);
2579 if (l_hash
> r_hash
)
2581 if (l_hash
< r_hash
)
2586 static void swap_xe(void *a
, void *b
, int size
)
2588 struct ocfs2_xattr_entry
*l
= a
, *r
= b
, tmp
;
2591 memcpy(l
, r
, sizeof(struct ocfs2_xattr_entry
));
2592 memcpy(r
, &tmp
, sizeof(struct ocfs2_xattr_entry
));
2596 * When the ocfs2_xattr_block is filled up, new bucket will be created
2597 * and all the xattr entries will be moved to the new bucket.
2598 * Note: we need to sort the entries since they are not saved in order
2599 * in the ocfs2_xattr_block.
2601 static void ocfs2_cp_xattr_block_to_bucket(struct inode
*inode
,
2602 struct buffer_head
*xb_bh
,
2603 struct buffer_head
*xh_bh
,
2604 struct buffer_head
*data_bh
)
2606 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
2607 u16 offset
, size
, off_change
;
2608 struct ocfs2_xattr_entry
*xe
;
2609 struct ocfs2_xattr_block
*xb
=
2610 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
2611 struct ocfs2_xattr_header
*xb_xh
= &xb
->xb_attrs
.xb_header
;
2612 struct ocfs2_xattr_header
*xh
=
2613 (struct ocfs2_xattr_header
*)xh_bh
->b_data
;
2614 u16 count
= le16_to_cpu(xb_xh
->xh_count
);
2615 char *target
= xh_bh
->b_data
, *src
= xb_bh
->b_data
;
2617 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2618 (unsigned long long)xb_bh
->b_blocknr
,
2619 (unsigned long long)xh_bh
->b_blocknr
);
2621 memset(xh_bh
->b_data
, 0, blocksize
);
2623 memset(data_bh
->b_data
, 0, blocksize
);
2625 * Since the xe_name_offset is based on ocfs2_xattr_header,
2626 * there is a offset change corresponding to the change of
2627 * ocfs2_xattr_header's position.
2629 off_change
= offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
2630 xe
= &xb_xh
->xh_entries
[count
- 1];
2631 offset
= le16_to_cpu(xe
->xe_name_offset
) + off_change
;
2632 size
= blocksize
- offset
;
2634 /* copy all the names and values. */
2636 target
= data_bh
->b_data
;
2637 memcpy(target
+ offset
, src
+ offset
, size
);
2639 /* Init new header now. */
2640 xh
->xh_count
= xb_xh
->xh_count
;
2641 xh
->xh_num_buckets
= cpu_to_le16(1);
2642 xh
->xh_name_value_len
= cpu_to_le16(size
);
2643 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
- size
);
2645 /* copy all the entries. */
2646 target
= xh_bh
->b_data
;
2647 offset
= offsetof(struct ocfs2_xattr_header
, xh_entries
);
2648 size
= count
* sizeof(struct ocfs2_xattr_entry
);
2649 memcpy(target
+ offset
, (char *)xb_xh
+ offset
, size
);
2651 /* Change the xe offset for all the xe because of the move. */
2652 off_change
= OCFS2_XATTR_BUCKET_SIZE
- blocksize
+
2653 offsetof(struct ocfs2_xattr_block
, xb_attrs
.xb_header
);
2654 for (i
= 0; i
< count
; i
++)
2655 le16_add_cpu(&xh
->xh_entries
[i
].xe_name_offset
, off_change
);
2657 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2658 offset
, size
, off_change
);
2660 sort(target
+ offset
, count
, sizeof(struct ocfs2_xattr_entry
),
2665 * After we move xattr from block to index btree, we have to
2666 * update ocfs2_xattr_search to the new xe and base.
2668 * When the entry is in xattr block, xattr_bh indicates the storage place.
2669 * While if the entry is in index b-tree, "bucket" indicates the
2670 * real place of the xattr.
2672 static int ocfs2_xattr_update_xattr_search(struct inode
*inode
,
2673 struct ocfs2_xattr_search
*xs
,
2674 struct buffer_head
*old_bh
,
2675 struct buffer_head
*new_bh
)
2678 char *buf
= old_bh
->b_data
;
2679 struct ocfs2_xattr_block
*old_xb
= (struct ocfs2_xattr_block
*)buf
;
2680 struct ocfs2_xattr_header
*old_xh
= &old_xb
->xb_attrs
.xb_header
;
2681 int i
, blocksize
= inode
->i_sb
->s_blocksize
;
2682 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2684 xs
->bucket
.bhs
[0] = new_bh
;
2686 xs
->bucket
.xh
= (struct ocfs2_xattr_header
*)xs
->bucket
.bhs
[0]->b_data
;
2687 xs
->header
= xs
->bucket
.xh
;
2689 xs
->base
= new_bh
->b_data
;
2690 xs
->end
= xs
->base
+ inode
->i_sb
->s_blocksize
;
2692 if (!xs
->not_found
) {
2693 if (OCFS2_XATTR_BUCKET_SIZE
!= blocksize
) {
2694 ret
= ocfs2_read_blocks(inode
,
2695 xs
->bucket
.bhs
[0]->b_blocknr
+ 1,
2696 blk_per_bucket
- 1, &xs
->bucket
.bhs
[1],
2703 i
= xs
->here
- old_xh
->xh_entries
;
2704 xs
->here
= &xs
->header
->xh_entries
[i
];
2711 static int ocfs2_xattr_create_index_block(struct inode
*inode
,
2712 struct ocfs2_xattr_search
*xs
)
2714 int ret
, credits
= OCFS2_SUBALLOC_ALLOC
;
2718 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
2719 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2720 struct ocfs2_alloc_context
*data_ac
;
2721 struct buffer_head
*xh_bh
= NULL
, *data_bh
= NULL
;
2722 struct buffer_head
*xb_bh
= xs
->xattr_bh
;
2723 struct ocfs2_xattr_block
*xb
=
2724 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
2725 struct ocfs2_xattr_tree_root
*xr
;
2726 u16 xb_flags
= le16_to_cpu(xb
->xb_flags
);
2727 u16 bpb
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2729 mlog(0, "create xattr index block for %llu\n",
2730 (unsigned long long)xb_bh
->b_blocknr
);
2732 BUG_ON(xb_flags
& OCFS2_XATTR_INDEXED
);
2734 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
2742 * We can use this lock for now, and maybe move to a dedicated mutex
2743 * if performance becomes a problem later.
2745 down_write(&oi
->ip_alloc_sem
);
2748 * 3 more credits, one for xattr block update, one for the 1st block
2749 * of the new xattr bucket and one for the value/data.
2752 handle
= ocfs2_start_trans(osb
, credits
);
2753 if (IS_ERR(handle
)) {
2754 ret
= PTR_ERR(handle
);
2759 ret
= ocfs2_journal_access(handle
, inode
, xb_bh
,
2760 OCFS2_JOURNAL_ACCESS_WRITE
);
2766 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
, &len
);
2773 * The bucket may spread in many blocks, and
2774 * we will only touch the 1st block and the last block
2775 * in the whole bucket(one for entry and one for data).
2777 blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
2779 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno
);
2781 xh_bh
= sb_getblk(inode
->i_sb
, blkno
);
2788 ocfs2_set_new_buffer_uptodate(inode
, xh_bh
);
2790 ret
= ocfs2_journal_access(handle
, inode
, xh_bh
,
2791 OCFS2_JOURNAL_ACCESS_CREATE
);
2798 data_bh
= sb_getblk(inode
->i_sb
, blkno
+ bpb
- 1);
2805 ocfs2_set_new_buffer_uptodate(inode
, data_bh
);
2807 ret
= ocfs2_journal_access(handle
, inode
, data_bh
,
2808 OCFS2_JOURNAL_ACCESS_CREATE
);
2815 ocfs2_cp_xattr_block_to_bucket(inode
, xb_bh
, xh_bh
, data_bh
);
2817 ocfs2_journal_dirty(handle
, xh_bh
);
2819 ocfs2_journal_dirty(handle
, data_bh
);
2821 ocfs2_xattr_update_xattr_search(inode
, xs
, xb_bh
, xh_bh
);
2823 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2824 memset(&xb
->xb_attrs
, 0, inode
->i_sb
->s_blocksize
-
2825 offsetof(struct ocfs2_xattr_block
, xb_attrs
));
2827 xr
= &xb
->xb_attrs
.xb_root
;
2828 xr
->xt_clusters
= cpu_to_le32(1);
2829 xr
->xt_last_eb_blk
= 0;
2830 xr
->xt_list
.l_tree_depth
= 0;
2831 xr
->xt_list
.l_count
= cpu_to_le16(ocfs2_xattr_recs_per_xb(inode
->i_sb
));
2832 xr
->xt_list
.l_next_free_rec
= cpu_to_le16(1);
2834 xr
->xt_list
.l_recs
[0].e_cpos
= 0;
2835 xr
->xt_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
2836 xr
->xt_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
2838 xb
->xb_flags
= cpu_to_le16(xb_flags
| OCFS2_XATTR_INDEXED
);
2840 ret
= ocfs2_journal_dirty(handle
, xb_bh
);
2847 ocfs2_commit_trans(osb
, handle
);
2850 up_write(&oi
->ip_alloc_sem
);
2854 ocfs2_free_alloc_context(data_ac
);
2862 static int cmp_xe_offset(const void *a
, const void *b
)
2864 const struct ocfs2_xattr_entry
*l
= a
, *r
= b
;
2865 u32 l_name_offset
= le16_to_cpu(l
->xe_name_offset
);
2866 u32 r_name_offset
= le16_to_cpu(r
->xe_name_offset
);
2868 if (l_name_offset
< r_name_offset
)
2870 if (l_name_offset
> r_name_offset
)
2876 * defrag a xattr bucket if we find that the bucket has some
2877 * holes beteen name/value pairs.
2878 * We will move all the name/value pairs to the end of the bucket
2879 * so that we can spare some space for insertion.
2881 static int ocfs2_defrag_xattr_bucket(struct inode
*inode
,
2882 struct ocfs2_xattr_bucket
*bucket
)
2885 size_t end
, offset
, len
, value_len
;
2886 struct ocfs2_xattr_header
*xh
;
2887 char *entries
, *buf
, *bucket_buf
= NULL
;
2888 u64 blkno
= bucket
->bhs
[0]->b_blocknr
;
2889 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
2891 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2893 struct buffer_head
**bhs
;
2894 struct ocfs2_xattr_entry
*xe
;
2896 bhs
= kzalloc(sizeof(struct buffer_head
*) * blk_per_bucket
,
2901 ret
= ocfs2_read_blocks(inode
, blkno
, blk_per_bucket
, bhs
,
2907 * In order to make the operation more efficient and generic,
2908 * we copy all the blocks into a contiguous memory and do the
2909 * defragment there, so if anything is error, we will not touch
2912 bucket_buf
= kmalloc(OCFS2_XATTR_BUCKET_SIZE
, GFP_NOFS
);
2919 for (i
= 0; i
< blk_per_bucket
; i
++, buf
+= blocksize
)
2920 memcpy(buf
, bhs
[i
]->b_data
, blocksize
);
2922 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)), blk_per_bucket
);
2923 if (IS_ERR(handle
)) {
2924 ret
= PTR_ERR(handle
);
2930 for (i
= 0; i
< blk_per_bucket
; i
++) {
2931 ret
= ocfs2_journal_access(handle
, inode
, bhs
[i
],
2932 OCFS2_JOURNAL_ACCESS_WRITE
);
2939 xh
= (struct ocfs2_xattr_header
*)bucket_buf
;
2940 entries
= (char *)xh
->xh_entries
;
2941 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
2943 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2944 "xh_free_start = %u, xh_name_value_len = %u.\n",
2945 blkno
, le16_to_cpu(xh
->xh_count
), xh_free_start
,
2946 le16_to_cpu(xh
->xh_name_value_len
));
2949 * sort all the entries by their offset.
2950 * the largest will be the first, so that we can
2951 * move them to the end one by one.
2953 sort(entries
, le16_to_cpu(xh
->xh_count
),
2954 sizeof(struct ocfs2_xattr_entry
),
2955 cmp_xe_offset
, swap_xe
);
2957 /* Move all name/values to the end of the bucket. */
2958 xe
= xh
->xh_entries
;
2959 end
= OCFS2_XATTR_BUCKET_SIZE
;
2960 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++, xe
++) {
2961 offset
= le16_to_cpu(xe
->xe_name_offset
);
2962 if (ocfs2_xattr_is_local(xe
))
2963 value_len
= OCFS2_XATTR_SIZE(
2964 le64_to_cpu(xe
->xe_value_size
));
2966 value_len
= OCFS2_XATTR_ROOT_SIZE
;
2967 len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
) + value_len
;
2970 * We must make sure that the name/value pair
2971 * exist in the same block. So adjust end to
2972 * the previous block end if needed.
2974 if (((end
- len
) / blocksize
!=
2975 (end
- 1) / blocksize
))
2976 end
= end
- end
% blocksize
;
2978 if (end
> offset
+ len
) {
2979 memmove(bucket_buf
+ end
- len
,
2980 bucket_buf
+ offset
, len
);
2981 xe
->xe_name_offset
= cpu_to_le16(end
- len
);
2984 mlog_bug_on_msg(end
< offset
+ len
, "Defrag check failed for "
2985 "bucket %llu\n", (unsigned long long)blkno
);
2990 mlog_bug_on_msg(xh_free_start
> end
, "Defrag check failed for "
2991 "bucket %llu\n", (unsigned long long)blkno
);
2993 if (xh_free_start
== end
)
2996 memset(bucket_buf
+ xh_free_start
, 0, end
- xh_free_start
);
2997 xh
->xh_free_start
= cpu_to_le16(end
);
2999 /* sort the entries by their name_hash. */
3000 sort(entries
, le16_to_cpu(xh
->xh_count
),
3001 sizeof(struct ocfs2_xattr_entry
),
3005 for (i
= 0; i
< blk_per_bucket
; i
++, buf
+= blocksize
) {
3006 memcpy(bhs
[i
]->b_data
, buf
, blocksize
);
3007 ocfs2_journal_dirty(handle
, bhs
[i
]);
3011 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
3015 for (i
= 0; i
< blk_per_bucket
; i
++)
3025 * Move half nums of the xattr bucket in the previous cluster to this new
3026 * cluster. We only touch the last cluster of the previous extend record.
3028 * first_bh is the first buffer_head of a series of bucket in the same
3029 * extent rec and header_bh is the header of one bucket in this cluster.
3030 * They will be updated if we move the data header_bh contains to the new
3031 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3033 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode
*inode
,
3035 struct buffer_head
**first_bh
,
3036 struct buffer_head
**header_bh
,
3042 int i
, ret
, credits
;
3043 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3044 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3045 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
3046 int blocksize
= inode
->i_sb
->s_blocksize
;
3047 struct buffer_head
*old_bh
, *new_bh
, *prev_bh
, *new_first_bh
= NULL
;
3048 struct ocfs2_xattr_header
*new_xh
;
3049 struct ocfs2_xattr_header
*xh
=
3050 (struct ocfs2_xattr_header
*)((*first_bh
)->b_data
);
3052 BUG_ON(le16_to_cpu(xh
->xh_num_buckets
) < num_buckets
);
3053 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
== osb
->s_clustersize
);
3055 prev_bh
= *first_bh
;
3057 xh
= (struct ocfs2_xattr_header
*)prev_bh
->b_data
;
3059 prev_blkno
+= (num_clusters
- 1) * bpc
+ bpc
/ 2;
3061 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3062 prev_blkno
, new_blkno
);
3065 * We need to update the 1st half of the new cluster and
3066 * 1 more for the update of the 1st bucket of the previous
3069 credits
= bpc
/ 2 + 1;
3070 ret
= ocfs2_extend_trans(handle
, credits
);
3076 ret
= ocfs2_journal_access(handle
, inode
, prev_bh
,
3077 OCFS2_JOURNAL_ACCESS_WRITE
);
3083 for (i
= 0; i
< bpc
/ 2; i
++, prev_blkno
++, new_blkno
++) {
3084 old_bh
= new_bh
= NULL
;
3085 new_bh
= sb_getblk(inode
->i_sb
, new_blkno
);
3092 ocfs2_set_new_buffer_uptodate(inode
, new_bh
);
3094 ret
= ocfs2_journal_access(handle
, inode
, new_bh
,
3095 OCFS2_JOURNAL_ACCESS_CREATE
);
3102 ret
= ocfs2_read_block(inode
, prev_blkno
, &old_bh
);
3109 memcpy(new_bh
->b_data
, old_bh
->b_data
, blocksize
);
3112 new_xh
= (struct ocfs2_xattr_header
*)new_bh
->b_data
;
3113 new_xh
->xh_num_buckets
= cpu_to_le16(num_buckets
/ 2);
3116 *first_hash
= le32_to_cpu(
3117 new_xh
->xh_entries
[0].xe_name_hash
);
3118 new_first_bh
= new_bh
;
3119 get_bh(new_first_bh
);
3122 ocfs2_journal_dirty(handle
, new_bh
);
3124 if (*header_bh
== old_bh
) {
3126 *header_bh
= new_bh
;
3130 *first_bh
= new_first_bh
;
3137 le16_add_cpu(&xh
->xh_num_buckets
, -(num_buckets
/ 2));
3139 ocfs2_journal_dirty(handle
, prev_bh
);
3142 brelse(new_first_bh
);
3146 static int ocfs2_read_xattr_bucket(struct inode
*inode
,
3148 struct buffer_head
**bhs
,
3152 u16 i
, blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3155 return ocfs2_read_blocks(inode
, blkno
,
3156 blk_per_bucket
, bhs
,
3159 for (i
= 0; i
< blk_per_bucket
; i
++) {
3160 bhs
[i
] = sb_getblk(inode
->i_sb
, blkno
+ i
);
3161 if (bhs
[i
] == NULL
) {
3166 ocfs2_set_new_buffer_uptodate(inode
, bhs
[i
]);
3173 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3174 * first_hash will record the 1st hash of the new bucket.
3176 static int ocfs2_half_xattr_bucket(struct inode
*inode
,
3181 int new_bucket_head
)
3184 u16 count
, start
, len
, name_value_len
, xe_len
, name_offset
;
3185 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3186 struct buffer_head
**s_bhs
, **t_bhs
= NULL
;
3187 struct ocfs2_xattr_header
*xh
;
3188 struct ocfs2_xattr_entry
*xe
;
3189 int blocksize
= inode
->i_sb
->s_blocksize
;
3191 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3194 s_bhs
= kcalloc(blk_per_bucket
, sizeof(struct buffer_head
*), GFP_NOFS
);
3198 ret
= ocfs2_read_xattr_bucket(inode
, blk
, s_bhs
, 0);
3204 ret
= ocfs2_journal_access(handle
, inode
, s_bhs
[0],
3205 OCFS2_JOURNAL_ACCESS_WRITE
);
3211 t_bhs
= kcalloc(blk_per_bucket
, sizeof(struct buffer_head
*), GFP_NOFS
);
3217 ret
= ocfs2_read_xattr_bucket(inode
, new_blk
, t_bhs
, new_bucket_head
);
3223 for (i
= 0; i
< blk_per_bucket
; i
++) {
3224 ret
= ocfs2_journal_access(handle
, inode
, t_bhs
[i
],
3225 OCFS2_JOURNAL_ACCESS_CREATE
);
3232 /* copy the whole bucket to the new first. */
3233 for (i
= 0; i
< blk_per_bucket
; i
++)
3234 memcpy(t_bhs
[i
]->b_data
, s_bhs
[i
]->b_data
, blocksize
);
3236 /* update the new bucket. */
3237 xh
= (struct ocfs2_xattr_header
*)t_bhs
[0]->b_data
;
3238 count
= le16_to_cpu(xh
->xh_count
);
3242 * Calculate the total name/value len and xh_free_start for
3243 * the old bucket first.
3245 name_offset
= OCFS2_XATTR_BUCKET_SIZE
;
3247 for (i
= 0; i
< start
; i
++) {
3248 xe
= &xh
->xh_entries
[i
];
3249 xe_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3250 if (ocfs2_xattr_is_local(xe
))
3252 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3254 xe_len
+= OCFS2_XATTR_ROOT_SIZE
;
3255 name_value_len
+= xe_len
;
3256 if (le16_to_cpu(xe
->xe_name_offset
) < name_offset
)
3257 name_offset
= le16_to_cpu(xe
->xe_name_offset
);
3261 * Now begin the modification to the new bucket.
3263 * In the new bucket, We just move the xattr entry to the beginning
3264 * and don't touch the name/value. So there will be some holes in the
3265 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3268 xe
= &xh
->xh_entries
[start
];
3269 len
= sizeof(struct ocfs2_xattr_entry
) * (count
- start
);
3270 mlog(0, "mv xattr entry len %d from %d to %d\n", len
,
3271 (int)((char *)xe
- (char *)xh
),
3272 (int)((char *)xh
->xh_entries
- (char *)xh
));
3273 memmove((char *)xh
->xh_entries
, (char *)xe
, len
);
3274 xe
= &xh
->xh_entries
[count
- start
];
3275 len
= sizeof(struct ocfs2_xattr_entry
) * start
;
3276 memset((char *)xe
, 0, len
);
3278 le16_add_cpu(&xh
->xh_count
, -start
);
3279 le16_add_cpu(&xh
->xh_name_value_len
, -name_value_len
);
3281 /* Calculate xh_free_start for the new bucket. */
3282 xh
->xh_free_start
= cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
3283 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
3284 xe
= &xh
->xh_entries
[i
];
3285 xe_len
= OCFS2_XATTR_SIZE(xe
->xe_name_len
);
3286 if (ocfs2_xattr_is_local(xe
))
3288 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3290 xe_len
+= OCFS2_XATTR_ROOT_SIZE
;
3291 if (le16_to_cpu(xe
->xe_name_offset
) <
3292 le16_to_cpu(xh
->xh_free_start
))
3293 xh
->xh_free_start
= xe
->xe_name_offset
;
3296 /* set xh->xh_num_buckets for the new xh. */
3297 if (new_bucket_head
)
3298 xh
->xh_num_buckets
= cpu_to_le16(1);
3300 xh
->xh_num_buckets
= 0;
3302 for (i
= 0; i
< blk_per_bucket
; i
++) {
3303 ocfs2_journal_dirty(handle
, t_bhs
[i
]);
3308 /* store the first_hash of the new bucket. */
3310 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3313 * Now only update the 1st block of the old bucket.
3314 * Please note that the entry has been sorted already above.
3316 xh
= (struct ocfs2_xattr_header
*)s_bhs
[0]->b_data
;
3317 memset(&xh
->xh_entries
[start
], 0,
3318 sizeof(struct ocfs2_xattr_entry
) * (count
- start
));
3319 xh
->xh_count
= cpu_to_le16(start
);
3320 xh
->xh_free_start
= cpu_to_le16(name_offset
);
3321 xh
->xh_name_value_len
= cpu_to_le16(name_value_len
);
3323 ocfs2_journal_dirty(handle
, s_bhs
[0]);
3329 for (i
= 0; i
< blk_per_bucket
; i
++)
3335 for (i
= 0; i
< blk_per_bucket
; i
++)
3344 * Copy xattr from one bucket to another bucket.
3346 * The caller must make sure that the journal transaction
3347 * has enough space for journaling.
3349 static int ocfs2_cp_xattr_bucket(struct inode
*inode
,
3356 int blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3357 int blocksize
= inode
->i_sb
->s_blocksize
;
3358 struct buffer_head
**s_bhs
, **t_bhs
= NULL
;
3360 BUG_ON(s_blkno
== t_blkno
);
3362 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3363 s_blkno
, t_blkno
, t_is_new
);
3365 s_bhs
= kzalloc(sizeof(struct buffer_head
*) * blk_per_bucket
,
3370 ret
= ocfs2_read_xattr_bucket(inode
, s_blkno
, s_bhs
, 0);
3374 t_bhs
= kzalloc(sizeof(struct buffer_head
*) * blk_per_bucket
,
3381 ret
= ocfs2_read_xattr_bucket(inode
, t_blkno
, t_bhs
, t_is_new
);
3385 for (i
= 0; i
< blk_per_bucket
; i
++) {
3386 ret
= ocfs2_journal_access(handle
, inode
, t_bhs
[i
],
3387 OCFS2_JOURNAL_ACCESS_WRITE
);
3392 for (i
= 0; i
< blk_per_bucket
; i
++) {
3393 memcpy(t_bhs
[i
]->b_data
, s_bhs
[i
]->b_data
, blocksize
);
3394 ocfs2_journal_dirty(handle
, t_bhs
[i
]);
3399 for (i
= 0; i
< blk_per_bucket
; i
++)
3405 for (i
= 0; i
< blk_per_bucket
; i
++)
3414 * Copy one xattr cluster from src_blk to to_blk.
3415 * The to_blk will become the first bucket header of the cluster, so its
3416 * xh_num_buckets will be initialized as the bucket num in the cluster.
3418 static int ocfs2_cp_xattr_cluster(struct inode
*inode
,
3420 struct buffer_head
*first_bh
,
3425 int i
, ret
, credits
;
3426 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3427 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3428 int num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
);
3429 struct buffer_head
*bh
= NULL
;
3430 struct ocfs2_xattr_header
*xh
;
3431 u64 to_blk_start
= to_blk
;
3433 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk
, to_blk
);
3436 * We need to update the new cluster and 1 more for the update of
3437 * the 1st bucket of the previous extent rec.
3440 ret
= ocfs2_extend_trans(handle
, credits
);
3446 ret
= ocfs2_journal_access(handle
, inode
, first_bh
,
3447 OCFS2_JOURNAL_ACCESS_WRITE
);
3453 for (i
= 0; i
< num_buckets
; i
++) {
3454 ret
= ocfs2_cp_xattr_bucket(inode
, handle
,
3455 src_blk
, to_blk
, 1);
3461 src_blk
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3462 to_blk
+= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3465 /* update the old bucket header. */
3466 xh
= (struct ocfs2_xattr_header
*)first_bh
->b_data
;
3467 le16_add_cpu(&xh
->xh_num_buckets
, -num_buckets
);
3469 ocfs2_journal_dirty(handle
, first_bh
);
3471 /* update the new bucket header. */
3472 ret
= ocfs2_read_block(inode
, to_blk_start
, &bh
);
3478 ret
= ocfs2_journal_access(handle
, inode
, bh
,
3479 OCFS2_JOURNAL_ACCESS_WRITE
);
3485 xh
= (struct ocfs2_xattr_header
*)bh
->b_data
;
3486 xh
->xh_num_buckets
= cpu_to_le16(num_buckets
);
3488 ocfs2_journal_dirty(handle
, bh
);
3491 *first_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3498 * Move half of the xattrs in this cluster to the new cluster.
3499 * This function should only be called when bucket size == cluster size.
3500 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3502 static int ocfs2_half_xattr_cluster(struct inode
*inode
,
3508 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3509 int ret
, credits
= 2 * blk_per_bucket
;
3511 BUG_ON(OCFS2_XATTR_BUCKET_SIZE
< OCFS2_SB(inode
->i_sb
)->s_clustersize
);
3513 ret
= ocfs2_extend_trans(handle
, credits
);
3519 /* Move half of the xattr in start_blk to the next bucket. */
3520 return ocfs2_half_xattr_bucket(inode
, handle
, prev_blk
,
3521 new_blk
, first_hash
, 1);
3525 * Move some xattrs from the old cluster to the new one since they are not
3526 * contiguous in ocfs2 xattr tree.
3528 * new_blk starts a new separate cluster, and we will move some xattrs from
3529 * prev_blk to it. v_start will be set as the first name hash value in this
3530 * new cluster so that it can be used as e_cpos during tree insertion and
3531 * don't collide with our original b-tree operations. first_bh and header_bh
3532 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3533 * to extend the insert bucket.
3535 * The problem is how much xattr should we move to the new one and when should
3536 * we update first_bh and header_bh?
3537 * 1. If cluster size > bucket size, that means the previous cluster has more
3538 * than 1 bucket, so just move half nums of bucket into the new cluster and
3539 * update the first_bh and header_bh if the insert bucket has been moved
3540 * to the new cluster.
3541 * 2. If cluster_size == bucket_size:
3542 * a) If the previous extent rec has more than one cluster and the insert
3543 * place isn't in the last cluster, copy the entire last cluster to the
3544 * new one. This time, we don't need to upate the first_bh and header_bh
3545 * since they will not be moved into the new cluster.
3546 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3547 * the new one. And we set the extend flag to zero if the insert place is
3548 * moved into the new allocated cluster since no extend is needed.
3550 static int ocfs2_adjust_xattr_cross_cluster(struct inode
*inode
,
3552 struct buffer_head
**first_bh
,
3553 struct buffer_head
**header_bh
,
3561 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3563 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3564 prev_blk
, prev_clusters
, new_blk
);
3566 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode
->i_sb
)) > 1)
3567 ret
= ocfs2_mv_xattr_bucket_cross_cluster(inode
,
3576 u64 last_blk
= prev_blk
+ bpc
* (prev_clusters
- 1);
3578 if (prev_clusters
> 1 && (*header_bh
)->b_blocknr
!= last_blk
)
3579 ret
= ocfs2_cp_xattr_cluster(inode
, handle
, *first_bh
,
3583 ret
= ocfs2_half_xattr_cluster(inode
, handle
,
3587 if ((*header_bh
)->b_blocknr
== last_blk
&& extend
)
3596 * Add a new cluster for xattr storage.
3598 * If the new cluster is contiguous with the previous one, it will be
3599 * appended to the same extent record, and num_clusters will be updated.
3600 * If not, we will insert a new extent for it and move some xattrs in
3601 * the last cluster into the new allocated one.
3602 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3603 * lose the benefits of hashing because we'll have to search large leaves.
3604 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3607 * first_bh is the first block of the previous extent rec and header_bh
3608 * indicates the bucket we will insert the new xattrs. They will be updated
3609 * when the header_bh is moved into the new cluster.
3611 static int ocfs2_add_new_xattr_cluster(struct inode
*inode
,
3612 struct buffer_head
*root_bh
,
3613 struct buffer_head
**first_bh
,
3614 struct buffer_head
**header_bh
,
3621 u16 bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
3622 u32 prev_clusters
= *num_clusters
;
3623 u32 clusters_to_add
= 1, bit_off
, num_bits
, v_start
= 0;
3625 handle_t
*handle
= NULL
;
3626 struct ocfs2_alloc_context
*data_ac
= NULL
;
3627 struct ocfs2_alloc_context
*meta_ac
= NULL
;
3628 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3629 struct ocfs2_extent_tree et
;
3631 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3632 "previous xattr blkno = %llu\n",
3633 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
3634 prev_cpos
, prev_blkno
);
3636 ocfs2_init_xattr_tree_extent_tree(&et
, inode
, root_bh
);
3638 ret
= ocfs2_lock_allocators(inode
, &et
, clusters_to_add
, 0,
3639 &data_ac
, &meta_ac
);
3645 credits
= ocfs2_calc_extend_credits(osb
->sb
, et
.et_root_el
,
3647 handle
= ocfs2_start_trans(osb
, credits
);
3648 if (IS_ERR(handle
)) {
3649 ret
= PTR_ERR(handle
);
3655 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
3656 OCFS2_JOURNAL_ACCESS_WRITE
);
3662 ret
= __ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
3663 clusters_to_add
, &bit_off
, &num_bits
);
3670 BUG_ON(num_bits
> clusters_to_add
);
3672 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
3673 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3674 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3676 if (prev_blkno
+ prev_clusters
* bpc
== block
&&
3677 (prev_clusters
+ num_bits
) << osb
->s_clustersize_bits
<=
3678 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
) {
3680 * If this cluster is contiguous with the old one and
3681 * adding this new cluster, we don't surpass the limit of
3682 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3683 * initialized and used like other buckets in the previous
3685 * So add it as a contiguous one. The caller will handle
3688 v_start
= prev_cpos
+ prev_clusters
;
3689 *num_clusters
= prev_clusters
+ num_bits
;
3690 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3693 ret
= ocfs2_adjust_xattr_cross_cluster(inode
,
3708 if (handle
->h_buffer_credits
< credits
) {
3710 * The journal has been restarted before, and don't
3711 * have enough space for the insertion, so extend it
3714 ret
= ocfs2_extend_trans(handle
, credits
);
3720 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3721 num_bits
, block
, v_start
);
3722 ret
= ocfs2_insert_extent(osb
, handle
, inode
, &et
, v_start
, block
,
3723 num_bits
, 0, meta_ac
);
3729 ret
= ocfs2_journal_dirty(handle
, root_bh
);
3737 ocfs2_commit_trans(osb
, handle
);
3739 ocfs2_free_alloc_context(data_ac
);
3741 ocfs2_free_alloc_context(meta_ac
);
3747 * Extend a new xattr bucket and move xattrs to the end one by one until
3748 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3750 static int ocfs2_extend_xattr_bucket(struct inode
*inode
,
3751 struct buffer_head
*first_bh
,
3752 struct buffer_head
*start_bh
,
3756 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3757 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
3758 u64 start_blk
= start_bh
->b_blocknr
, end_blk
;
3759 u32 num_buckets
= num_clusters
* ocfs2_xattr_buckets_per_cluster(osb
);
3761 struct ocfs2_xattr_header
*first_xh
=
3762 (struct ocfs2_xattr_header
*)first_bh
->b_data
;
3763 u16 bucket
= le16_to_cpu(first_xh
->xh_num_buckets
);
3765 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3766 "from %llu, len = %u\n", start_blk
,
3767 (unsigned long long)first_bh
->b_blocknr
, num_clusters
);
3769 BUG_ON(bucket
>= num_buckets
);
3771 end_blk
= first_bh
->b_blocknr
+ (bucket
- 1) * blk_per_bucket
;
3774 * We will touch all the buckets after the start_bh(include it).
3775 * Add one more bucket and modify the first_bh.
3777 credits
= end_blk
- start_blk
+ 2 * blk_per_bucket
+ 1;
3778 handle
= ocfs2_start_trans(osb
, credits
);
3779 if (IS_ERR(handle
)) {
3780 ret
= PTR_ERR(handle
);
3786 ret
= ocfs2_journal_access(handle
, inode
, first_bh
,
3787 OCFS2_JOURNAL_ACCESS_WRITE
);
3793 while (end_blk
!= start_blk
) {
3794 ret
= ocfs2_cp_xattr_bucket(inode
, handle
, end_blk
,
3795 end_blk
+ blk_per_bucket
, 0);
3798 end_blk
-= blk_per_bucket
;
3801 /* Move half of the xattr in start_blk to the next bucket. */
3802 ret
= ocfs2_half_xattr_bucket(inode
, handle
, start_blk
,
3803 start_blk
+ blk_per_bucket
, NULL
, 0);
3805 le16_add_cpu(&first_xh
->xh_num_buckets
, 1);
3806 ocfs2_journal_dirty(handle
, first_bh
);
3809 ocfs2_commit_trans(osb
, handle
);
3815 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3816 * xb_bh is the ocfs2_xattr_block.
3817 * We will move all the buckets starting from header_bh to the next place. As
3818 * for this one, half num of its xattrs will be moved to the next one.
3820 * We will allocate a new cluster if current cluster is full and adjust
3821 * header_bh and first_bh if the insert place is moved to the new cluster.
3823 static int ocfs2_add_new_xattr_bucket(struct inode
*inode
,
3824 struct buffer_head
*xb_bh
,
3825 struct buffer_head
*header_bh
)
3827 struct ocfs2_xattr_header
*first_xh
= NULL
;
3828 struct buffer_head
*first_bh
= NULL
;
3829 struct ocfs2_xattr_block
*xb
=
3830 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
3831 struct ocfs2_xattr_tree_root
*xb_root
= &xb
->xb_attrs
.xb_root
;
3832 struct ocfs2_extent_list
*el
= &xb_root
->xt_list
;
3833 struct ocfs2_xattr_header
*xh
=
3834 (struct ocfs2_xattr_header
*)header_bh
->b_data
;
3835 u32 name_hash
= le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
);
3836 struct super_block
*sb
= inode
->i_sb
;
3837 struct ocfs2_super
*osb
= OCFS2_SB(sb
);
3838 int ret
, num_buckets
, extend
= 1;
3840 u32 e_cpos
, num_clusters
;
3842 mlog(0, "Add new xattr bucket starting form %llu\n",
3843 (unsigned long long)header_bh
->b_blocknr
);
3846 * Add refrence for header_bh here because it may be
3847 * changed in ocfs2_add_new_xattr_cluster and we need
3848 * to free it in the end.
3852 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
, &e_cpos
,
3859 ret
= ocfs2_read_block(inode
, p_blkno
, &first_bh
);
3865 num_buckets
= ocfs2_xattr_buckets_per_cluster(osb
) * num_clusters
;
3866 first_xh
= (struct ocfs2_xattr_header
*)first_bh
->b_data
;
3868 if (num_buckets
== le16_to_cpu(first_xh
->xh_num_buckets
)) {
3869 ret
= ocfs2_add_new_xattr_cluster(inode
,
3884 ret
= ocfs2_extend_xattr_bucket(inode
,
3896 static inline char *ocfs2_xattr_bucket_get_val(struct inode
*inode
,
3897 struct ocfs2_xattr_bucket
*bucket
,
3900 int block_off
= offs
>> inode
->i_sb
->s_blocksize_bits
;
3902 offs
= offs
% inode
->i_sb
->s_blocksize
;
3903 return bucket
->bhs
[block_off
]->b_data
+ offs
;
3907 * Handle the normal xattr set, including replace, delete and new.
3909 * Note: "local" indicates the real data's locality. So we can't
3910 * just its bucket locality by its length.
3912 static void ocfs2_xattr_set_entry_normal(struct inode
*inode
,
3913 struct ocfs2_xattr_info
*xi
,
3914 struct ocfs2_xattr_search
*xs
,
3918 struct ocfs2_xattr_entry
*last
, *xe
;
3919 int name_len
= strlen(xi
->name
);
3920 struct ocfs2_xattr_header
*xh
= xs
->header
;
3921 u16 count
= le16_to_cpu(xh
->xh_count
), start
;
3922 size_t blocksize
= inode
->i_sb
->s_blocksize
;
3924 size_t offs
, size
, new_size
;
3926 last
= &xh
->xh_entries
[count
];
3927 if (!xs
->not_found
) {
3929 offs
= le16_to_cpu(xe
->xe_name_offset
);
3930 if (ocfs2_xattr_is_local(xe
))
3931 size
= OCFS2_XATTR_SIZE(name_len
) +
3932 OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
3934 size
= OCFS2_XATTR_SIZE(name_len
) +
3935 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE
);
3938 * If the new value will be stored outside, xi->value has been
3939 * initalized as an empty ocfs2_xattr_value_root, and the same
3940 * goes with xi->value_len, so we can set new_size safely here.
3941 * See ocfs2_xattr_set_in_bucket.
3943 new_size
= OCFS2_XATTR_SIZE(name_len
) +
3944 OCFS2_XATTR_SIZE(xi
->value_len
);
3946 le16_add_cpu(&xh
->xh_name_value_len
, -size
);
3948 if (new_size
> size
)
3949 goto set_new_name_value
;
3951 /* Now replace the old value with new one. */
3953 xe
->xe_value_size
= cpu_to_le64(xi
->value_len
);
3955 xe
->xe_value_size
= 0;
3957 val
= ocfs2_xattr_bucket_get_val(inode
,
3959 memset(val
+ OCFS2_XATTR_SIZE(name_len
), 0,
3960 size
- OCFS2_XATTR_SIZE(name_len
));
3961 if (OCFS2_XATTR_SIZE(xi
->value_len
) > 0)
3962 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
),
3963 xi
->value
, xi
->value_len
);
3965 le16_add_cpu(&xh
->xh_name_value_len
, new_size
);
3966 ocfs2_xattr_set_local(xe
, local
);
3970 * Remove the old entry if there is more than one.
3971 * We don't remove the last entry so that we can
3972 * use it to indicate the hash value of the empty
3976 le16_add_cpu(&xh
->xh_count
, -1);
3979 (void *)last
- (void *)xe
);
3981 sizeof(struct ocfs2_xattr_entry
));
3984 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE
);
3989 /* find a new entry for insert. */
3990 int low
= 0, high
= count
- 1, tmp
;
3991 struct ocfs2_xattr_entry
*tmp_xe
;
3993 while (low
<= high
&& count
) {
3994 tmp
= (low
+ high
) / 2;
3995 tmp_xe
= &xh
->xh_entries
[tmp
];
3997 if (name_hash
> le32_to_cpu(tmp_xe
->xe_name_hash
))
3999 else if (name_hash
<
4000 le32_to_cpu(tmp_xe
->xe_name_hash
))
4008 xe
= &xh
->xh_entries
[low
];
4010 memmove(xe
+ 1, xe
, (void *)last
- (void *)xe
);
4012 le16_add_cpu(&xh
->xh_count
, 1);
4013 memset(xe
, 0, sizeof(struct ocfs2_xattr_entry
));
4014 xe
->xe_name_hash
= cpu_to_le32(name_hash
);
4015 xe
->xe_name_len
= name_len
;
4016 ocfs2_xattr_set_type(xe
, xi
->name_index
);
4020 /* Insert the new name+value. */
4021 size
= OCFS2_XATTR_SIZE(name_len
) + OCFS2_XATTR_SIZE(xi
->value_len
);
4024 * We must make sure that the name/value pair
4025 * exists in the same block.
4027 offs
= le16_to_cpu(xh
->xh_free_start
);
4028 start
= offs
- size
;
4030 if (start
>> inode
->i_sb
->s_blocksize_bits
!=
4031 (offs
- 1) >> inode
->i_sb
->s_blocksize_bits
) {
4032 offs
= offs
- offs
% blocksize
;
4033 xh
->xh_free_start
= cpu_to_le16(offs
);
4036 val
= ocfs2_xattr_bucket_get_val(inode
,
4037 &xs
->bucket
, offs
- size
);
4038 xe
->xe_name_offset
= cpu_to_le16(offs
- size
);
4040 memset(val
, 0, size
);
4041 memcpy(val
, xi
->name
, name_len
);
4042 memcpy(val
+ OCFS2_XATTR_SIZE(name_len
), xi
->value
, xi
->value_len
);
4044 xe
->xe_value_size
= cpu_to_le64(xi
->value_len
);
4045 ocfs2_xattr_set_local(xe
, local
);
4047 le16_add_cpu(&xh
->xh_free_start
, -size
);
4048 le16_add_cpu(&xh
->xh_name_value_len
, size
);
4053 static int ocfs2_xattr_bucket_handle_journal(struct inode
*inode
,
4055 struct ocfs2_xattr_search
*xs
,
4056 struct buffer_head
**bhs
,
4059 int ret
= 0, off
, block_off
;
4060 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4063 * First calculate all the blocks we should journal_access
4064 * and journal_dirty. The first block should always be touched.
4066 ret
= ocfs2_journal_dirty(handle
, bhs
[0]);
4070 /* calc the data. */
4071 off
= le16_to_cpu(xe
->xe_name_offset
);
4072 block_off
= off
>> inode
->i_sb
->s_blocksize_bits
;
4073 ret
= ocfs2_journal_dirty(handle
, bhs
[block_off
]);
4081 * Set the xattr entry in the specified bucket.
4082 * The bucket is indicated by xs->bucket and it should have the enough
4083 * space for the xattr insertion.
4085 static int ocfs2_xattr_set_entry_in_bucket(struct inode
*inode
,
4086 struct ocfs2_xattr_info
*xi
,
4087 struct ocfs2_xattr_search
*xs
,
4092 handle_t
*handle
= NULL
;
4093 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4094 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4096 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4097 (unsigned long)xi
->value_len
, xi
->name_index
,
4098 (unsigned long long)xs
->bucket
.bhs
[0]->b_blocknr
);
4100 if (!xs
->bucket
.bhs
[1]) {
4101 ret
= ocfs2_read_blocks(inode
,
4102 xs
->bucket
.bhs
[0]->b_blocknr
+ 1,
4103 blk_per_bucket
- 1, &xs
->bucket
.bhs
[1],
4111 handle
= ocfs2_start_trans(osb
, blk_per_bucket
);
4112 if (IS_ERR(handle
)) {
4113 ret
= PTR_ERR(handle
);
4119 for (i
= 0; i
< blk_per_bucket
; i
++) {
4120 ret
= ocfs2_journal_access(handle
, inode
, xs
->bucket
.bhs
[i
],
4121 OCFS2_JOURNAL_ACCESS_WRITE
);
4128 ocfs2_xattr_set_entry_normal(inode
, xi
, xs
, name_hash
, local
);
4130 /*Only dirty the blocks we have touched in set xattr. */
4131 ret
= ocfs2_xattr_bucket_handle_journal(inode
, handle
, xs
,
4132 xs
->bucket
.bhs
, blk_per_bucket
);
4136 ocfs2_commit_trans(osb
, handle
);
4141 static int ocfs2_xattr_value_update_size(struct inode
*inode
,
4142 struct buffer_head
*xe_bh
,
4143 struct ocfs2_xattr_entry
*xe
,
4147 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4148 handle_t
*handle
= NULL
;
4150 handle
= ocfs2_start_trans(osb
, 1);
4151 if (handle
== NULL
) {
4157 ret
= ocfs2_journal_access(handle
, inode
, xe_bh
,
4158 OCFS2_JOURNAL_ACCESS_WRITE
);
4164 xe
->xe_value_size
= cpu_to_le64(new_size
);
4166 ret
= ocfs2_journal_dirty(handle
, xe_bh
);
4171 ocfs2_commit_trans(osb
, handle
);
4177 * Truncate the specified xe_off entry in xattr bucket.
4178 * bucket is indicated by header_bh and len is the new length.
4179 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4181 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4183 static int ocfs2_xattr_bucket_value_truncate(struct inode
*inode
,
4184 struct buffer_head
*header_bh
,
4190 struct buffer_head
*value_bh
= NULL
;
4191 struct ocfs2_xattr_value_root
*xv
;
4192 struct ocfs2_xattr_entry
*xe
;
4193 struct ocfs2_xattr_header
*xh
=
4194 (struct ocfs2_xattr_header
*)header_bh
->b_data
;
4195 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4197 xe
= &xh
->xh_entries
[xe_off
];
4199 BUG_ON(!xe
|| ocfs2_xattr_is_local(xe
));
4201 offset
= le16_to_cpu(xe
->xe_name_offset
) +
4202 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
4204 value_blk
= offset
/ blocksize
;
4206 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4207 BUG_ON(value_blk
!= (offset
+ OCFS2_XATTR_ROOT_SIZE
- 1) / blocksize
);
4208 value_blk
+= header_bh
->b_blocknr
;
4210 ret
= ocfs2_read_block(inode
, value_blk
, &value_bh
);
4216 xv
= (struct ocfs2_xattr_value_root
*)
4217 (value_bh
->b_data
+ offset
% blocksize
);
4219 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4220 xe_off
, (unsigned long long)header_bh
->b_blocknr
, len
);
4221 ret
= ocfs2_xattr_value_truncate(inode
, value_bh
, xv
, len
);
4227 ret
= ocfs2_xattr_value_update_size(inode
, header_bh
, xe
, len
);
4238 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode
*inode
,
4239 struct ocfs2_xattr_search
*xs
,
4243 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4244 struct ocfs2_xattr_header
*xh
= (struct ocfs2_xattr_header
*)xs
->base
;
4246 BUG_ON(!xs
->bucket
.bhs
[0] || !xe
|| ocfs2_xattr_is_local(xe
));
4248 offset
= xe
- xh
->xh_entries
;
4249 ret
= ocfs2_xattr_bucket_value_truncate(inode
, xs
->bucket
.bhs
[0],
4257 static int ocfs2_xattr_bucket_set_value_outside(struct inode
*inode
,
4258 struct ocfs2_xattr_search
*xs
,
4263 struct ocfs2_xattr_value_root
*xv
;
4264 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4266 BUG_ON(!xs
->base
|| !xe
|| ocfs2_xattr_is_local(xe
));
4268 offset
= le16_to_cpu(xe
->xe_name_offset
) +
4269 OCFS2_XATTR_SIZE(xe
->xe_name_len
);
4271 xv
= (struct ocfs2_xattr_value_root
*)(xs
->base
+ offset
);
4273 return __ocfs2_xattr_set_value_outside(inode
, xv
, val
, value_len
);
4276 static int ocfs2_rm_xattr_cluster(struct inode
*inode
,
4277 struct buffer_head
*root_bh
,
4283 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4284 struct inode
*tl_inode
= osb
->osb_tl_inode
;
4286 struct ocfs2_xattr_block
*xb
=
4287 (struct ocfs2_xattr_block
*)root_bh
->b_data
;
4288 struct ocfs2_alloc_context
*meta_ac
= NULL
;
4289 struct ocfs2_cached_dealloc_ctxt dealloc
;
4290 struct ocfs2_extent_tree et
;
4292 ocfs2_init_xattr_tree_extent_tree(&et
, inode
, root_bh
);
4294 ocfs2_init_dealloc_ctxt(&dealloc
);
4296 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4297 cpos
, len
, (unsigned long long)blkno
);
4299 ocfs2_remove_xattr_clusters_from_cache(inode
, blkno
, len
);
4301 ret
= ocfs2_lock_allocators(inode
, &et
, 0, 1, NULL
, &meta_ac
);
4307 mutex_lock(&tl_inode
->i_mutex
);
4309 if (ocfs2_truncate_log_needs_flush(osb
)) {
4310 ret
= __ocfs2_flush_truncate_log(osb
);
4317 handle
= ocfs2_start_trans(osb
, OCFS2_REMOVE_EXTENT_CREDITS
);
4318 if (handle
== NULL
) {
4324 ret
= ocfs2_journal_access(handle
, inode
, root_bh
,
4325 OCFS2_JOURNAL_ACCESS_WRITE
);
4331 ret
= ocfs2_remove_extent(inode
, &et
, cpos
, len
, handle
, meta_ac
,
4338 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, -len
);
4340 ret
= ocfs2_journal_dirty(handle
, root_bh
);
4346 ret
= ocfs2_truncate_log_append(osb
, handle
, blkno
, len
);
4351 ocfs2_commit_trans(osb
, handle
);
4353 ocfs2_schedule_truncate_log_flush(osb
, 1);
4355 mutex_unlock(&tl_inode
->i_mutex
);
4358 ocfs2_free_alloc_context(meta_ac
);
4360 ocfs2_run_deallocs(osb
, &dealloc
);
4365 static void ocfs2_xattr_bucket_remove_xs(struct inode
*inode
,
4366 struct ocfs2_xattr_search
*xs
)
4368 handle_t
*handle
= NULL
;
4369 struct ocfs2_xattr_header
*xh
= xs
->bucket
.xh
;
4370 struct ocfs2_xattr_entry
*last
= &xh
->xh_entries
[
4371 le16_to_cpu(xh
->xh_count
) - 1];
4374 handle
= ocfs2_start_trans((OCFS2_SB(inode
->i_sb
)), 1);
4375 if (IS_ERR(handle
)) {
4376 ret
= PTR_ERR(handle
);
4381 ret
= ocfs2_journal_access(handle
, inode
, xs
->bucket
.bhs
[0],
4382 OCFS2_JOURNAL_ACCESS_WRITE
);
4388 /* Remove the old entry. */
4389 memmove(xs
->here
, xs
->here
+ 1,
4390 (void *)last
- (void *)xs
->here
);
4391 memset(last
, 0, sizeof(struct ocfs2_xattr_entry
));
4392 le16_add_cpu(&xh
->xh_count
, -1);
4394 ret
= ocfs2_journal_dirty(handle
, xs
->bucket
.bhs
[0]);
4398 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
4402 * Set the xattr name/value in the bucket specified in xs.
4404 * As the new value in xi may be stored in the bucket or in an outside cluster,
4405 * we divide the whole process into 3 steps:
4406 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4407 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4408 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4409 * 4. If the clusters for the new outside value can't be allocated, we need
4410 * to free the xattr we allocated in set.
4412 static int ocfs2_xattr_set_in_bucket(struct inode
*inode
,
4413 struct ocfs2_xattr_info
*xi
,
4414 struct ocfs2_xattr_search
*xs
)
4418 char *val
= (char *)xi
->value
;
4419 struct ocfs2_xattr_entry
*xe
= xs
->here
;
4420 u32 name_hash
= ocfs2_xattr_name_hash(inode
, xi
->name
,
4423 if (!xs
->not_found
&& !ocfs2_xattr_is_local(xe
)) {
4425 * We need to truncate the xattr storage first.
4427 * If both the old and new value are stored to
4428 * outside block, we only need to truncate
4429 * the storage and then set the value outside.
4431 * If the new value should be stored within block,
4432 * we should free all the outside block first and
4433 * the modification to the xattr block will be done
4434 * by following steps.
4436 if (xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
4437 value_len
= xi
->value_len
;
4441 ret
= ocfs2_xattr_bucket_value_truncate_xs(inode
, xs
,
4447 goto set_value_outside
;
4450 value_len
= xi
->value_len
;
4451 /* So we have to handle the inside block change now. */
4452 if (value_len
> OCFS2_XATTR_INLINE_SIZE
) {
4454 * If the new value will be stored outside of block,
4455 * initalize a new empty value root and insert it first.
4458 xi
->value
= &def_xv
;
4459 xi
->value_len
= OCFS2_XATTR_ROOT_SIZE
;
4462 ret
= ocfs2_xattr_set_entry_in_bucket(inode
, xi
, xs
, name_hash
, local
);
4468 if (value_len
<= OCFS2_XATTR_INLINE_SIZE
)
4471 /* allocate the space now for the outside block storage. */
4472 ret
= ocfs2_xattr_bucket_value_truncate_xs(inode
, xs
,
4477 if (xs
->not_found
) {
4479 * We can't allocate enough clusters for outside
4480 * storage and we have allocated xattr already,
4481 * so need to remove it.
4483 ocfs2_xattr_bucket_remove_xs(inode
, xs
);
4489 ret
= ocfs2_xattr_bucket_set_value_outside(inode
, xs
, val
, value_len
);
4494 /* check whether the xattr bucket is filled up with the same hash value. */
4495 static int ocfs2_check_xattr_bucket_collision(struct inode
*inode
,
4496 struct ocfs2_xattr_bucket
*bucket
)
4498 struct ocfs2_xattr_header
*xh
= bucket
->xh
;
4500 if (xh
->xh_entries
[le16_to_cpu(xh
->xh_count
) - 1].xe_name_hash
==
4501 xh
->xh_entries
[0].xe_name_hash
) {
4502 mlog(ML_ERROR
, "Too much hash collision in xattr bucket %llu, "
4504 (unsigned long long)bucket
->bhs
[0]->b_blocknr
,
4505 le32_to_cpu(xh
->xh_entries
[0].xe_name_hash
));
4512 static int ocfs2_xattr_set_entry_index_block(struct inode
*inode
,
4513 struct ocfs2_xattr_info
*xi
,
4514 struct ocfs2_xattr_search
*xs
)
4516 struct ocfs2_xattr_header
*xh
;
4517 struct ocfs2_xattr_entry
*xe
;
4518 u16 count
, header_size
, xh_free_start
;
4519 int i
, free
, max_free
, need
, old
;
4520 size_t value_size
= 0, name_len
= strlen(xi
->name
);
4521 size_t blocksize
= inode
->i_sb
->s_blocksize
;
4522 int ret
, allocation
= 0;
4523 u16 blk_per_bucket
= ocfs2_blocks_per_xattr_bucket(inode
->i_sb
);
4525 mlog_entry("Set xattr %s in xattr index block\n", xi
->name
);
4529 count
= le16_to_cpu(xh
->xh_count
);
4530 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4531 header_size
= sizeof(struct ocfs2_xattr_header
) +
4532 count
* sizeof(struct ocfs2_xattr_entry
);
4533 max_free
= OCFS2_XATTR_BUCKET_SIZE
-
4534 le16_to_cpu(xh
->xh_name_value_len
) - header_size
;
4536 mlog_bug_on_msg(header_size
> blocksize
, "bucket %llu has header size "
4537 "of %u which exceed block size\n",
4538 (unsigned long long)xs
->bucket
.bhs
[0]->b_blocknr
,
4541 if (xi
->value
&& xi
->value_len
> OCFS2_XATTR_INLINE_SIZE
)
4542 value_size
= OCFS2_XATTR_ROOT_SIZE
;
4544 value_size
= OCFS2_XATTR_SIZE(xi
->value_len
);
4547 need
= sizeof(struct ocfs2_xattr_entry
) +
4548 OCFS2_XATTR_SIZE(name_len
) + value_size
;
4550 need
= value_size
+ OCFS2_XATTR_SIZE(name_len
);
4553 * We only replace the old value if the new length is smaller
4554 * than the old one. Otherwise we will allocate new space in the
4555 * bucket to store it.
4558 if (ocfs2_xattr_is_local(xe
))
4559 old
= OCFS2_XATTR_SIZE(le64_to_cpu(xe
->xe_value_size
));
4561 old
= OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE
);
4563 if (old
>= value_size
)
4567 free
= xh_free_start
- header_size
;
4569 * We need to make sure the new name/value pair
4570 * can exist in the same block.
4572 if (xh_free_start
% blocksize
< need
)
4573 free
-= xh_free_start
% blocksize
;
4575 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4576 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4577 " %u\n", xs
->not_found
,
4578 (unsigned long long)xs
->bucket
.bhs
[0]->b_blocknr
,
4579 free
, need
, max_free
, le16_to_cpu(xh
->xh_free_start
),
4580 le16_to_cpu(xh
->xh_name_value_len
));
4582 if (free
< need
|| count
== ocfs2_xattr_max_xe_in_bucket(inode
->i_sb
)) {
4583 if (need
<= max_free
&&
4584 count
< ocfs2_xattr_max_xe_in_bucket(inode
->i_sb
)) {
4586 * We can create the space by defragment. Since only the
4587 * name/value will be moved, the xe shouldn't be changed
4590 ret
= ocfs2_defrag_xattr_bucket(inode
, &xs
->bucket
);
4596 xh_free_start
= le16_to_cpu(xh
->xh_free_start
);
4597 free
= xh_free_start
- header_size
;
4598 if (xh_free_start
% blocksize
< need
)
4599 free
-= xh_free_start
% blocksize
;
4604 mlog(0, "Can't get enough space for xattr insert by "
4605 "defragment. Need %u bytes, but we have %d, so "
4606 "allocate new bucket for it.\n", need
, free
);
4610 * We have to add new buckets or clusters and one
4611 * allocation should leave us enough space for insert.
4616 * We do not allow for overlapping ranges between buckets. And
4617 * the maximum number of collisions we will allow for then is
4618 * one bucket's worth, so check it here whether we need to
4619 * add a new bucket for the insert.
4621 ret
= ocfs2_check_xattr_bucket_collision(inode
, &xs
->bucket
);
4627 ret
= ocfs2_add_new_xattr_bucket(inode
,
4635 for (i
= 0; i
< blk_per_bucket
; i
++)
4636 brelse(xs
->bucket
.bhs
[i
]);
4638 memset(&xs
->bucket
, 0, sizeof(xs
->bucket
));
4640 ret
= ocfs2_xattr_index_block_find(inode
, xs
->xattr_bh
,
4643 if (ret
&& ret
!= -ENODATA
)
4645 xs
->not_found
= ret
;
4651 ret
= ocfs2_xattr_set_in_bucket(inode
, xi
, xs
);
4657 static int ocfs2_delete_xattr_in_bucket(struct inode
*inode
,
4658 struct ocfs2_xattr_bucket
*bucket
,
4662 struct ocfs2_xattr_header
*xh
= bucket
->xh
;
4664 struct ocfs2_xattr_entry
*xe
;
4666 for (i
= 0; i
< le16_to_cpu(xh
->xh_count
); i
++) {
4667 xe
= &xh
->xh_entries
[i
];
4668 if (ocfs2_xattr_is_local(xe
))
4671 ret
= ocfs2_xattr_bucket_value_truncate(inode
,
4683 static int ocfs2_delete_xattr_index_block(struct inode
*inode
,
4684 struct buffer_head
*xb_bh
)
4686 struct ocfs2_xattr_block
*xb
=
4687 (struct ocfs2_xattr_block
*)xb_bh
->b_data
;
4688 struct ocfs2_extent_list
*el
= &xb
->xb_attrs
.xb_root
.xt_list
;
4690 u32 name_hash
= UINT_MAX
, e_cpos
, num_clusters
;
4693 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
4696 while (name_hash
> 0) {
4697 ret
= ocfs2_xattr_get_rec(inode
, name_hash
, &p_blkno
,
4698 &e_cpos
, &num_clusters
, el
);
4704 ret
= ocfs2_iterate_xattr_buckets(inode
, p_blkno
, num_clusters
,
4705 ocfs2_delete_xattr_in_bucket
,
4712 ret
= ocfs2_rm_xattr_cluster(inode
, xb_bh
,
4713 p_blkno
, e_cpos
, num_clusters
);
4722 name_hash
= e_cpos
- 1;
4730 * 'trusted' attributes support
4733 #define XATTR_TRUSTED_PREFIX "trusted."
4735 static size_t ocfs2_xattr_trusted_list(struct inode
*inode
, char *list
,
4736 size_t list_size
, const char *name
,
4739 const size_t prefix_len
= sizeof(XATTR_TRUSTED_PREFIX
) - 1;
4740 const size_t total_len
= prefix_len
+ name_len
+ 1;
4742 if (list
&& total_len
<= list_size
) {
4743 memcpy(list
, XATTR_TRUSTED_PREFIX
, prefix_len
);
4744 memcpy(list
+ prefix_len
, name
, name_len
);
4745 list
[prefix_len
+ name_len
] = '\0';
4750 static int ocfs2_xattr_trusted_get(struct inode
*inode
, const char *name
,
4751 void *buffer
, size_t size
)
4753 if (strcmp(name
, "") == 0)
4755 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_TRUSTED
, name
,
4759 static int ocfs2_xattr_trusted_set(struct inode
*inode
, const char *name
,
4760 const void *value
, size_t size
, int flags
)
4762 if (strcmp(name
, "") == 0)
4765 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_TRUSTED
, name
, value
,
4769 struct xattr_handler ocfs2_xattr_trusted_handler
= {
4770 .prefix
= XATTR_TRUSTED_PREFIX
,
4771 .list
= ocfs2_xattr_trusted_list
,
4772 .get
= ocfs2_xattr_trusted_get
,
4773 .set
= ocfs2_xattr_trusted_set
,
4778 * 'user' attributes support
4781 #define XATTR_USER_PREFIX "user."
4783 static size_t ocfs2_xattr_user_list(struct inode
*inode
, char *list
,
4784 size_t list_size
, const char *name
,
4787 const size_t prefix_len
= sizeof(XATTR_USER_PREFIX
) - 1;
4788 const size_t total_len
= prefix_len
+ name_len
+ 1;
4789 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4791 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
4794 if (list
&& total_len
<= list_size
) {
4795 memcpy(list
, XATTR_USER_PREFIX
, prefix_len
);
4796 memcpy(list
+ prefix_len
, name
, name_len
);
4797 list
[prefix_len
+ name_len
] = '\0';
4802 static int ocfs2_xattr_user_get(struct inode
*inode
, const char *name
,
4803 void *buffer
, size_t size
)
4805 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4807 if (strcmp(name
, "") == 0)
4809 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
4811 return ocfs2_xattr_get(inode
, OCFS2_XATTR_INDEX_USER
, name
,
4815 static int ocfs2_xattr_user_set(struct inode
*inode
, const char *name
,
4816 const void *value
, size_t size
, int flags
)
4818 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
4820 if (strcmp(name
, "") == 0)
4822 if (osb
->s_mount_opt
& OCFS2_MOUNT_NOUSERXATTR
)
4825 return ocfs2_xattr_set(inode
, OCFS2_XATTR_INDEX_USER
, name
, value
,
4829 struct xattr_handler ocfs2_xattr_user_handler
= {
4830 .prefix
= XATTR_USER_PREFIX
,
4831 .list
= ocfs2_xattr_user_list
,
4832 .get
= ocfs2_xattr_user_get
,
4833 .set
= ocfs2_xattr_user_set
,