2 * linux/fs/ext4/xattr.c
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
8 * Extended attributes for symlinks and special files added per
9 * suggestion of Luka Renko <luka.renko@hermes.si>.
10 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
12 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
13 * and Andreas Gruenbacher <agruen@suse.de>.
17 * Extended attributes are stored directly in inodes (on file systems with
18 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
19 * field contains the block number if an inode uses an additional block. All
20 * attributes must fit in the inode and one additional block. Blocks that
21 * contain the identical set of attributes may be shared among several inodes.
22 * Identical blocks are detected by keeping a cache of blocks that have
23 * recently been accessed.
25 * The attributes in inodes and on blocks have a different header; the entries
26 * are stored in the same format:
28 * +------------------+
31 * | entry 2 | | growing downwards
36 * | value 3 | | growing upwards
38 * +------------------+
40 * The header is followed by multiple entry descriptors. In disk blocks, the
41 * entry descriptors are kept sorted. In inodes, they are unsorted. The
42 * attribute values are aligned to the end of the block in no specific order.
46 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
47 * EA blocks are only changed if they are exclusive to an inode, so
48 * holding xattr_sem also means that nothing but the EA block's reference
49 * count can change. Multiple writers to the same block are synchronized
53 #include <linux/init.h>
55 #include <linux/slab.h>
56 #include <linux/mbcache.h>
57 #include <linux/quotaops.h>
58 #include <linux/rwsem.h>
59 #include "ext4_jbd2.h"
64 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
65 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
66 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
67 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
69 #ifdef EXT4_XATTR_DEBUG
70 # define ea_idebug(inode, f...) do { \
71 printk(KERN_DEBUG "inode %s:%lu: ", \
72 inode->i_sb->s_id, inode->i_ino); \
76 # define ea_bdebug(bh, f...) do { \
77 char b[BDEVNAME_SIZE]; \
78 printk(KERN_DEBUG "block %s:%lu: ", \
79 bdevname(bh->b_bdev, b), \
80 (unsigned long) bh->b_blocknr); \
85 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
86 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
89 static void ext4_xattr_cache_insert(struct buffer_head
*);
90 static struct buffer_head
*ext4_xattr_cache_find(struct inode
*,
91 struct ext4_xattr_header
*,
92 struct mb_cache_entry
**);
93 static void ext4_xattr_rehash(struct ext4_xattr_header
*,
94 struct ext4_xattr_entry
*);
95 static int ext4_xattr_list(struct dentry
*dentry
, char *buffer
,
98 static struct mb_cache
*ext4_xattr_cache
;
100 static const struct xattr_handler
*ext4_xattr_handler_map
[] = {
101 [EXT4_XATTR_INDEX_USER
] = &ext4_xattr_user_handler
,
102 #ifdef CONFIG_EXT4_FS_POSIX_ACL
103 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS
] = &ext4_xattr_acl_access_handler
,
104 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT
] = &ext4_xattr_acl_default_handler
,
106 [EXT4_XATTR_INDEX_TRUSTED
] = &ext4_xattr_trusted_handler
,
107 #ifdef CONFIG_EXT4_FS_SECURITY
108 [EXT4_XATTR_INDEX_SECURITY
] = &ext4_xattr_security_handler
,
112 const struct xattr_handler
*ext4_xattr_handlers
[] = {
113 &ext4_xattr_user_handler
,
114 &ext4_xattr_trusted_handler
,
115 #ifdef CONFIG_EXT4_FS_POSIX_ACL
116 &ext4_xattr_acl_access_handler
,
117 &ext4_xattr_acl_default_handler
,
119 #ifdef CONFIG_EXT4_FS_SECURITY
120 &ext4_xattr_security_handler
,
125 static __le32
ext4_xattr_block_csum(struct inode
*inode
,
127 struct ext4_xattr_header
*hdr
)
129 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
132 old
= hdr
->h_checksum
;
134 block_nr
= cpu_to_le64(block_nr
);
135 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&block_nr
,
137 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
,
138 EXT4_BLOCK_SIZE(inode
->i_sb
));
140 hdr
->h_checksum
= old
;
141 return cpu_to_le32(csum
);
144 static int ext4_xattr_block_csum_verify(struct inode
*inode
,
146 struct ext4_xattr_header
*hdr
)
148 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
149 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
) &&
150 (hdr
->h_checksum
!= ext4_xattr_block_csum(inode
, block_nr
, hdr
)))
155 static void ext4_xattr_block_csum_set(struct inode
*inode
,
157 struct ext4_xattr_header
*hdr
)
159 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
160 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
163 hdr
->h_checksum
= ext4_xattr_block_csum(inode
, block_nr
, hdr
);
166 static inline int ext4_handle_dirty_xattr_block(handle_t
*handle
,
168 struct buffer_head
*bh
)
170 ext4_xattr_block_csum_set(inode
, bh
->b_blocknr
, BHDR(bh
));
171 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
174 static inline const struct xattr_handler
*
175 ext4_xattr_handler(int name_index
)
177 const struct xattr_handler
*handler
= NULL
;
179 if (name_index
> 0 && name_index
< ARRAY_SIZE(ext4_xattr_handler_map
))
180 handler
= ext4_xattr_handler_map
[name_index
];
185 * Inode operation listxattr()
187 * dentry->d_inode->i_mutex: don't care
190 ext4_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
192 return ext4_xattr_list(dentry
, buffer
, size
);
196 ext4_xattr_check_names(struct ext4_xattr_entry
*entry
, void *end
)
198 while (!IS_LAST_ENTRY(entry
)) {
199 struct ext4_xattr_entry
*next
= EXT4_XATTR_NEXT(entry
);
200 if ((void *)next
>= end
)
208 ext4_xattr_check_block(struct inode
*inode
, struct buffer_head
*bh
)
212 if (buffer_verified(bh
))
215 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
216 BHDR(bh
)->h_blocks
!= cpu_to_le32(1))
218 if (!ext4_xattr_block_csum_verify(inode
, bh
->b_blocknr
, BHDR(bh
)))
220 error
= ext4_xattr_check_names(BFIRST(bh
), bh
->b_data
+ bh
->b_size
);
222 set_buffer_verified(bh
);
227 ext4_xattr_check_entry(struct ext4_xattr_entry
*entry
, size_t size
)
229 size_t value_size
= le32_to_cpu(entry
->e_value_size
);
231 if (entry
->e_value_block
!= 0 || value_size
> size
||
232 le16_to_cpu(entry
->e_value_offs
) + value_size
> size
)
238 ext4_xattr_find_entry(struct ext4_xattr_entry
**pentry
, int name_index
,
239 const char *name
, size_t size
, int sorted
)
241 struct ext4_xattr_entry
*entry
;
247 name_len
= strlen(name
);
249 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
250 cmp
= name_index
- entry
->e_name_index
;
252 cmp
= name_len
- entry
->e_name_len
;
254 cmp
= memcmp(name
, entry
->e_name
, name_len
);
255 if (cmp
<= 0 && (sorted
|| cmp
== 0))
259 if (!cmp
&& ext4_xattr_check_entry(entry
, size
))
261 return cmp
? -ENODATA
: 0;
265 ext4_xattr_block_get(struct inode
*inode
, int name_index
, const char *name
,
266 void *buffer
, size_t buffer_size
)
268 struct buffer_head
*bh
= NULL
;
269 struct ext4_xattr_entry
*entry
;
273 ea_idebug(inode
, "name=%d.%s, buffer=%p, buffer_size=%ld",
274 name_index
, name
, buffer
, (long)buffer_size
);
277 if (!EXT4_I(inode
)->i_file_acl
)
279 ea_idebug(inode
, "reading block %llu",
280 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
281 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
284 ea_bdebug(bh
, "b_count=%d, refcount=%d",
285 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
286 if (ext4_xattr_check_block(inode
, bh
)) {
288 EXT4_ERROR_INODE(inode
, "bad block %llu",
289 EXT4_I(inode
)->i_file_acl
);
293 ext4_xattr_cache_insert(bh
);
295 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, bh
->b_size
, 1);
300 size
= le32_to_cpu(entry
->e_value_size
);
303 if (size
> buffer_size
)
305 memcpy(buffer
, bh
->b_data
+ le16_to_cpu(entry
->e_value_offs
),
316 ext4_xattr_ibody_get(struct inode
*inode
, int name_index
, const char *name
,
317 void *buffer
, size_t buffer_size
)
319 struct ext4_xattr_ibody_header
*header
;
320 struct ext4_xattr_entry
*entry
;
321 struct ext4_inode
*raw_inode
;
322 struct ext4_iloc iloc
;
327 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
329 error
= ext4_get_inode_loc(inode
, &iloc
);
332 raw_inode
= ext4_raw_inode(&iloc
);
333 header
= IHDR(inode
, raw_inode
);
334 entry
= IFIRST(header
);
335 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
336 error
= ext4_xattr_check_names(entry
, end
);
339 error
= ext4_xattr_find_entry(&entry
, name_index
, name
,
340 end
- (void *)entry
, 0);
343 size
= le32_to_cpu(entry
->e_value_size
);
346 if (size
> buffer_size
)
348 memcpy(buffer
, (void *)IFIRST(header
) +
349 le16_to_cpu(entry
->e_value_offs
), size
);
361 * Copy an extended attribute into the buffer
362 * provided, or compute the buffer size required.
363 * Buffer is NULL to compute the size of the buffer required.
365 * Returns a negative error number on failure, or the number of bytes
366 * used / required on success.
369 ext4_xattr_get(struct inode
*inode
, int name_index
, const char *name
,
370 void *buffer
, size_t buffer_size
)
374 down_read(&EXT4_I(inode
)->xattr_sem
);
375 error
= ext4_xattr_ibody_get(inode
, name_index
, name
, buffer
,
377 if (error
== -ENODATA
)
378 error
= ext4_xattr_block_get(inode
, name_index
, name
, buffer
,
380 up_read(&EXT4_I(inode
)->xattr_sem
);
385 ext4_xattr_list_entries(struct dentry
*dentry
, struct ext4_xattr_entry
*entry
,
386 char *buffer
, size_t buffer_size
)
388 size_t rest
= buffer_size
;
390 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
391 const struct xattr_handler
*handler
=
392 ext4_xattr_handler(entry
->e_name_index
);
395 size_t size
= handler
->list(dentry
, buffer
, rest
,
407 return buffer_size
- rest
;
411 ext4_xattr_block_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
413 struct inode
*inode
= dentry
->d_inode
;
414 struct buffer_head
*bh
= NULL
;
417 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
418 buffer
, (long)buffer_size
);
421 if (!EXT4_I(inode
)->i_file_acl
)
423 ea_idebug(inode
, "reading block %llu",
424 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
425 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
429 ea_bdebug(bh
, "b_count=%d, refcount=%d",
430 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
431 if (ext4_xattr_check_block(inode
, bh
)) {
432 EXT4_ERROR_INODE(inode
, "bad block %llu",
433 EXT4_I(inode
)->i_file_acl
);
437 ext4_xattr_cache_insert(bh
);
438 error
= ext4_xattr_list_entries(dentry
, BFIRST(bh
), buffer
, buffer_size
);
447 ext4_xattr_ibody_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
449 struct inode
*inode
= dentry
->d_inode
;
450 struct ext4_xattr_ibody_header
*header
;
451 struct ext4_inode
*raw_inode
;
452 struct ext4_iloc iloc
;
456 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
458 error
= ext4_get_inode_loc(inode
, &iloc
);
461 raw_inode
= ext4_raw_inode(&iloc
);
462 header
= IHDR(inode
, raw_inode
);
463 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
464 error
= ext4_xattr_check_names(IFIRST(header
), end
);
467 error
= ext4_xattr_list_entries(dentry
, IFIRST(header
),
468 buffer
, buffer_size
);
478 * Copy a list of attribute names into the buffer
479 * provided, or compute the buffer size required.
480 * Buffer is NULL to compute the size of the buffer required.
482 * Returns a negative error number on failure, or the number of bytes
483 * used / required on success.
486 ext4_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
490 down_read(&EXT4_I(dentry
->d_inode
)->xattr_sem
);
491 ret
= ret2
= ext4_xattr_ibody_list(dentry
, buffer
, buffer_size
);
498 ret
= ext4_xattr_block_list(dentry
, buffer
, buffer_size
);
503 up_read(&EXT4_I(dentry
->d_inode
)->xattr_sem
);
508 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
511 static void ext4_xattr_update_super_block(handle_t
*handle
,
512 struct super_block
*sb
)
514 if (EXT4_HAS_COMPAT_FEATURE(sb
, EXT4_FEATURE_COMPAT_EXT_ATTR
))
517 if (ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
) == 0) {
518 EXT4_SET_COMPAT_FEATURE(sb
, EXT4_FEATURE_COMPAT_EXT_ATTR
);
519 ext4_handle_dirty_super(handle
, sb
);
524 * Release the xattr block BH: If the reference count is > 1, decrement
525 * it; otherwise free the block.
528 ext4_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
529 struct buffer_head
*bh
)
531 struct mb_cache_entry
*ce
= NULL
;
534 ce
= mb_cache_entry_get(ext4_xattr_cache
, bh
->b_bdev
, bh
->b_blocknr
);
535 error
= ext4_journal_get_write_access(handle
, bh
);
540 if (BHDR(bh
)->h_refcount
== cpu_to_le32(1)) {
541 ea_bdebug(bh
, "refcount now=0; freeing");
543 mb_cache_entry_free(ce
);
545 ext4_free_blocks(handle
, inode
, bh
, 0, 1,
546 EXT4_FREE_BLOCKS_METADATA
|
547 EXT4_FREE_BLOCKS_FORGET
);
550 le32_add_cpu(&BHDR(bh
)->h_refcount
, -1);
552 mb_cache_entry_release(ce
);
554 error
= ext4_handle_dirty_xattr_block(handle
, inode
, bh
);
556 ext4_handle_sync(handle
);
557 dquot_free_block(inode
, 1);
558 ea_bdebug(bh
, "refcount now=%d; releasing",
559 le32_to_cpu(BHDR(bh
)->h_refcount
));
562 ext4_std_error(inode
->i_sb
, error
);
567 * Find the available free space for EAs. This also returns the total number of
568 * bytes used by EA entries.
570 static size_t ext4_xattr_free_space(struct ext4_xattr_entry
*last
,
571 size_t *min_offs
, void *base
, int *total
)
573 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
574 *total
+= EXT4_XATTR_LEN(last
->e_name_len
);
575 if (!last
->e_value_block
&& last
->e_value_size
) {
576 size_t offs
= le16_to_cpu(last
->e_value_offs
);
577 if (offs
< *min_offs
)
581 return (*min_offs
- ((void *)last
- base
) - sizeof(__u32
));
584 struct ext4_xattr_info
{
591 struct ext4_xattr_search
{
592 struct ext4_xattr_entry
*first
;
595 struct ext4_xattr_entry
*here
;
600 ext4_xattr_set_entry(struct ext4_xattr_info
*i
, struct ext4_xattr_search
*s
)
602 struct ext4_xattr_entry
*last
;
603 size_t free
, min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
605 /* Compute min_offs and last. */
607 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
608 if (!last
->e_value_block
&& last
->e_value_size
) {
609 size_t offs
= le16_to_cpu(last
->e_value_offs
);
614 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
616 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
617 size_t size
= le32_to_cpu(s
->here
->e_value_size
);
618 free
+= EXT4_XATTR_SIZE(size
);
620 free
+= EXT4_XATTR_LEN(name_len
);
623 if (free
< EXT4_XATTR_SIZE(i
->value_len
) ||
624 free
< EXT4_XATTR_LEN(name_len
) +
625 EXT4_XATTR_SIZE(i
->value_len
))
629 if (i
->value
&& s
->not_found
) {
630 /* Insert the new name. */
631 size_t size
= EXT4_XATTR_LEN(name_len
);
632 size_t rest
= (void *)last
- (void *)s
->here
+ sizeof(__u32
);
633 memmove((void *)s
->here
+ size
, s
->here
, rest
);
634 memset(s
->here
, 0, size
);
635 s
->here
->e_name_index
= i
->name_index
;
636 s
->here
->e_name_len
= name_len
;
637 memcpy(s
->here
->e_name
, i
->name
, name_len
);
639 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
640 void *first_val
= s
->base
+ min_offs
;
641 size_t offs
= le16_to_cpu(s
->here
->e_value_offs
);
642 void *val
= s
->base
+ offs
;
643 size_t size
= EXT4_XATTR_SIZE(
644 le32_to_cpu(s
->here
->e_value_size
));
646 if (i
->value
&& size
== EXT4_XATTR_SIZE(i
->value_len
)) {
647 /* The old and the new value have the same
648 size. Just replace. */
649 s
->here
->e_value_size
=
650 cpu_to_le32(i
->value_len
);
651 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
652 EXT4_XATTR_PAD
); /* Clear pad bytes. */
653 memcpy(val
, i
->value
, i
->value_len
);
657 /* Remove the old value. */
658 memmove(first_val
+ size
, first_val
, val
- first_val
);
659 memset(first_val
, 0, size
);
660 s
->here
->e_value_size
= 0;
661 s
->here
->e_value_offs
= 0;
664 /* Adjust all value offsets. */
666 while (!IS_LAST_ENTRY(last
)) {
667 size_t o
= le16_to_cpu(last
->e_value_offs
);
668 if (!last
->e_value_block
&&
669 last
->e_value_size
&& o
< offs
)
671 cpu_to_le16(o
+ size
);
672 last
= EXT4_XATTR_NEXT(last
);
676 /* Remove the old name. */
677 size_t size
= EXT4_XATTR_LEN(name_len
);
678 last
= ENTRY((void *)last
- size
);
679 memmove(s
->here
, (void *)s
->here
+ size
,
680 (void *)last
- (void *)s
->here
+ sizeof(__u32
));
681 memset(last
, 0, size
);
686 /* Insert the new value. */
687 s
->here
->e_value_size
= cpu_to_le32(i
->value_len
);
689 size_t size
= EXT4_XATTR_SIZE(i
->value_len
);
690 void *val
= s
->base
+ min_offs
- size
;
691 s
->here
->e_value_offs
= cpu_to_le16(min_offs
- size
);
692 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
693 EXT4_XATTR_PAD
); /* Clear the pad bytes. */
694 memcpy(val
, i
->value
, i
->value_len
);
700 struct ext4_xattr_block_find
{
701 struct ext4_xattr_search s
;
702 struct buffer_head
*bh
;
706 ext4_xattr_block_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
707 struct ext4_xattr_block_find
*bs
)
709 struct super_block
*sb
= inode
->i_sb
;
712 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
713 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
715 if (EXT4_I(inode
)->i_file_acl
) {
716 /* The inode already has an extended attribute block. */
717 bs
->bh
= sb_bread(sb
, EXT4_I(inode
)->i_file_acl
);
721 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
722 atomic_read(&(bs
->bh
->b_count
)),
723 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
724 if (ext4_xattr_check_block(inode
, bs
->bh
)) {
725 EXT4_ERROR_INODE(inode
, "bad block %llu",
726 EXT4_I(inode
)->i_file_acl
);
730 /* Find the named attribute. */
731 bs
->s
.base
= BHDR(bs
->bh
);
732 bs
->s
.first
= BFIRST(bs
->bh
);
733 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
734 bs
->s
.here
= bs
->s
.first
;
735 error
= ext4_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
736 i
->name
, bs
->bh
->b_size
, 1);
737 if (error
&& error
!= -ENODATA
)
739 bs
->s
.not_found
= error
;
748 ext4_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
749 struct ext4_xattr_info
*i
,
750 struct ext4_xattr_block_find
*bs
)
752 struct super_block
*sb
= inode
->i_sb
;
753 struct buffer_head
*new_bh
= NULL
;
754 struct ext4_xattr_search
*s
= &bs
->s
;
755 struct mb_cache_entry
*ce
= NULL
;
758 #define header(x) ((struct ext4_xattr_header *)(x))
760 if (i
->value
&& i
->value_len
> sb
->s_blocksize
)
763 ce
= mb_cache_entry_get(ext4_xattr_cache
, bs
->bh
->b_bdev
,
765 error
= ext4_journal_get_write_access(handle
, bs
->bh
);
770 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
772 mb_cache_entry_free(ce
);
775 ea_bdebug(bs
->bh
, "modifying in-place");
776 error
= ext4_xattr_set_entry(i
, s
);
778 if (!IS_LAST_ENTRY(s
->first
))
779 ext4_xattr_rehash(header(s
->base
),
781 ext4_xattr_cache_insert(bs
->bh
);
783 unlock_buffer(bs
->bh
);
787 error
= ext4_handle_dirty_xattr_block(handle
,
794 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
796 unlock_buffer(bs
->bh
);
797 ext4_handle_release_buffer(handle
, bs
->bh
);
799 mb_cache_entry_release(ce
);
802 ea_bdebug(bs
->bh
, "cloning");
803 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
807 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
808 s
->first
= ENTRY(header(s
->base
)+1);
809 header(s
->base
)->h_refcount
= cpu_to_le32(1);
810 s
->here
= ENTRY(s
->base
+ offset
);
811 s
->end
= s
->base
+ bs
->bh
->b_size
;
814 /* Allocate a buffer where we construct the new block. */
815 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
816 /* assert(header == s->base) */
820 header(s
->base
)->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
821 header(s
->base
)->h_blocks
= cpu_to_le32(1);
822 header(s
->base
)->h_refcount
= cpu_to_le32(1);
823 s
->first
= ENTRY(header(s
->base
)+1);
824 s
->here
= ENTRY(header(s
->base
)+1);
825 s
->end
= s
->base
+ sb
->s_blocksize
;
828 error
= ext4_xattr_set_entry(i
, s
);
833 if (!IS_LAST_ENTRY(s
->first
))
834 ext4_xattr_rehash(header(s
->base
), s
->here
);
837 if (!IS_LAST_ENTRY(s
->first
)) {
838 new_bh
= ext4_xattr_cache_find(inode
, header(s
->base
), &ce
);
840 /* We found an identical block in the cache. */
841 if (new_bh
== bs
->bh
)
842 ea_bdebug(new_bh
, "keeping");
844 /* The old block is released after updating
846 error
= dquot_alloc_block(inode
, 1);
849 error
= ext4_journal_get_write_access(handle
,
854 le32_add_cpu(&BHDR(new_bh
)->h_refcount
, 1);
855 ea_bdebug(new_bh
, "reusing; refcount now=%d",
856 le32_to_cpu(BHDR(new_bh
)->h_refcount
));
857 unlock_buffer(new_bh
);
858 error
= ext4_handle_dirty_xattr_block(handle
,
864 mb_cache_entry_release(ce
);
866 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
867 /* We were modifying this block in-place. */
868 ea_bdebug(bs
->bh
, "keeping this block");
872 /* We need to allocate a new block */
873 ext4_fsblk_t goal
, block
;
875 goal
= ext4_group_first_block_no(sb
,
876 EXT4_I(inode
)->i_block_group
);
878 /* non-extent files can't have physical blocks past 2^32 */
879 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
880 goal
= goal
& EXT4_MAX_BLOCK_FILE_PHYS
;
883 * take i_data_sem because we will test
884 * i_delalloc_reserved_flag in ext4_mb_new_blocks
886 down_read((&EXT4_I(inode
)->i_data_sem
));
887 block
= ext4_new_meta_blocks(handle
, inode
, goal
, 0,
889 up_read((&EXT4_I(inode
)->i_data_sem
));
893 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
894 BUG_ON(block
> EXT4_MAX_BLOCK_FILE_PHYS
);
896 ea_idebug(inode
, "creating block %llu",
897 (unsigned long long)block
);
899 new_bh
= sb_getblk(sb
, block
);
902 ext4_free_blocks(handle
, inode
, NULL
, block
, 1,
903 EXT4_FREE_BLOCKS_METADATA
);
908 error
= ext4_journal_get_create_access(handle
, new_bh
);
910 unlock_buffer(new_bh
);
913 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
914 set_buffer_uptodate(new_bh
);
915 unlock_buffer(new_bh
);
916 ext4_xattr_cache_insert(new_bh
);
917 error
= ext4_handle_dirty_xattr_block(handle
,
924 /* Update the inode. */
925 EXT4_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
927 /* Drop the previous xattr block. */
928 if (bs
->bh
&& bs
->bh
!= new_bh
)
929 ext4_xattr_release_block(handle
, inode
, bs
->bh
);
934 mb_cache_entry_release(ce
);
936 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
942 dquot_free_block(inode
, 1);
946 EXT4_ERROR_INODE(inode
, "bad block %llu",
947 EXT4_I(inode
)->i_file_acl
);
953 struct ext4_xattr_ibody_find
{
954 struct ext4_xattr_search s
;
955 struct ext4_iloc iloc
;
959 ext4_xattr_ibody_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
960 struct ext4_xattr_ibody_find
*is
)
962 struct ext4_xattr_ibody_header
*header
;
963 struct ext4_inode
*raw_inode
;
966 if (EXT4_I(inode
)->i_extra_isize
== 0)
968 raw_inode
= ext4_raw_inode(&is
->iloc
);
969 header
= IHDR(inode
, raw_inode
);
970 is
->s
.base
= is
->s
.first
= IFIRST(header
);
971 is
->s
.here
= is
->s
.first
;
972 is
->s
.end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
973 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
974 error
= ext4_xattr_check_names(IFIRST(header
), is
->s
.end
);
977 /* Find the named attribute. */
978 error
= ext4_xattr_find_entry(&is
->s
.here
, i
->name_index
,
980 (void *)is
->s
.base
, 0);
981 if (error
&& error
!= -ENODATA
)
983 is
->s
.not_found
= error
;
989 ext4_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
990 struct ext4_xattr_info
*i
,
991 struct ext4_xattr_ibody_find
*is
)
993 struct ext4_xattr_ibody_header
*header
;
994 struct ext4_xattr_search
*s
= &is
->s
;
997 if (EXT4_I(inode
)->i_extra_isize
== 0)
999 error
= ext4_xattr_set_entry(i
, s
);
1002 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1003 if (!IS_LAST_ENTRY(s
->first
)) {
1004 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1005 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1007 header
->h_magic
= cpu_to_le32(0);
1008 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1014 * ext4_xattr_set_handle()
1016 * Create, replace or remove an extended attribute for this inode. Value
1017 * is NULL to remove an existing extended attribute, and non-NULL to
1018 * either replace an existing extended attribute, or create a new extended
1019 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1020 * specify that an extended attribute must exist and must not exist
1021 * previous to the call, respectively.
1023 * Returns 0, or a negative error number on failure.
1026 ext4_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
1027 const char *name
, const void *value
, size_t value_len
,
1030 struct ext4_xattr_info i
= {
1031 .name_index
= name_index
,
1034 .value_len
= value_len
,
1037 struct ext4_xattr_ibody_find is
= {
1038 .s
= { .not_found
= -ENODATA
, },
1040 struct ext4_xattr_block_find bs
= {
1041 .s
= { .not_found
= -ENODATA
, },
1043 unsigned long no_expand
;
1048 if (strlen(name
) > 255)
1050 down_write(&EXT4_I(inode
)->xattr_sem
);
1051 no_expand
= ext4_test_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1052 ext4_set_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1054 error
= ext4_reserve_inode_write(handle
, inode
, &is
.iloc
);
1058 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
)) {
1059 struct ext4_inode
*raw_inode
= ext4_raw_inode(&is
.iloc
);
1060 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
1061 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
1064 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
1068 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1071 if (is
.s
.not_found
&& bs
.s
.not_found
) {
1073 if (flags
& XATTR_REPLACE
)
1080 if (flags
& XATTR_CREATE
)
1084 if (!is
.s
.not_found
)
1085 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1086 else if (!bs
.s
.not_found
)
1087 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1089 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1090 if (!error
&& !bs
.s
.not_found
) {
1092 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1093 } else if (error
== -ENOSPC
) {
1094 if (EXT4_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
1095 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1099 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1102 if (!is
.s
.not_found
) {
1104 error
= ext4_xattr_ibody_set(handle
, inode
, &i
,
1110 ext4_xattr_update_super_block(handle
, inode
->i_sb
);
1111 inode
->i_ctime
= ext4_current_time(inode
);
1113 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1114 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
1116 * The bh is consumed by ext4_mark_iloc_dirty, even with
1121 ext4_handle_sync(handle
);
1128 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1129 up_write(&EXT4_I(inode
)->xattr_sem
);
1136 * Like ext4_xattr_set_handle, but start from an inode. This extended
1137 * attribute modification is a filesystem transaction by itself.
1139 * Returns 0, or a negative error number on failure.
1142 ext4_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
1143 const void *value
, size_t value_len
, int flags
)
1146 int error
, retries
= 0;
1149 handle
= ext4_journal_start(inode
, EXT4_DATA_TRANS_BLOCKS(inode
->i_sb
));
1150 if (IS_ERR(handle
)) {
1151 error
= PTR_ERR(handle
);
1155 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, name
,
1156 value
, value_len
, flags
);
1157 error2
= ext4_journal_stop(handle
);
1158 if (error
== -ENOSPC
&&
1159 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1169 * Shift the EA entries in the inode to create space for the increased
1172 static void ext4_xattr_shift_entries(struct ext4_xattr_entry
*entry
,
1173 int value_offs_shift
, void *to
,
1174 void *from
, size_t n
, int blocksize
)
1176 struct ext4_xattr_entry
*last
= entry
;
1179 /* Adjust the value offsets of the entries */
1180 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1181 if (!last
->e_value_block
&& last
->e_value_size
) {
1182 new_offs
= le16_to_cpu(last
->e_value_offs
) +
1184 BUG_ON(new_offs
+ le32_to_cpu(last
->e_value_size
)
1186 last
->e_value_offs
= cpu_to_le16(new_offs
);
1189 /* Shift the entries by n bytes */
1190 memmove(to
, from
, n
);
1194 * Expand an inode by new_extra_isize bytes when EAs are present.
1195 * Returns 0 on success or negative error number on failure.
1197 int ext4_expand_extra_isize_ea(struct inode
*inode
, int new_extra_isize
,
1198 struct ext4_inode
*raw_inode
, handle_t
*handle
)
1200 struct ext4_xattr_ibody_header
*header
;
1201 struct ext4_xattr_entry
*entry
, *last
, *first
;
1202 struct buffer_head
*bh
= NULL
;
1203 struct ext4_xattr_ibody_find
*is
= NULL
;
1204 struct ext4_xattr_block_find
*bs
= NULL
;
1205 char *buffer
= NULL
, *b_entry_name
= NULL
;
1206 size_t min_offs
, free
;
1207 int total_ino
, total_blk
;
1208 void *base
, *start
, *end
;
1209 int extra_isize
= 0, error
= 0, tried_min_extra_isize
= 0;
1210 int s_min_extra_isize
= le16_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_min_extra_isize
);
1212 down_write(&EXT4_I(inode
)->xattr_sem
);
1214 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
) {
1215 up_write(&EXT4_I(inode
)->xattr_sem
);
1219 header
= IHDR(inode
, raw_inode
);
1220 entry
= IFIRST(header
);
1223 * Check if enough free space is available in the inode to shift the
1224 * entries ahead by new_extra_isize.
1227 base
= start
= entry
;
1228 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1229 min_offs
= end
- base
;
1231 total_ino
= sizeof(struct ext4_xattr_ibody_header
);
1233 free
= ext4_xattr_free_space(last
, &min_offs
, base
, &total_ino
);
1234 if (free
>= new_extra_isize
) {
1235 entry
= IFIRST(header
);
1236 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
1237 - new_extra_isize
, (void *)raw_inode
+
1238 EXT4_GOOD_OLD_INODE_SIZE
+ new_extra_isize
,
1239 (void *)header
, total_ino
,
1240 inode
->i_sb
->s_blocksize
);
1241 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
1247 * Enough free space isn't available in the inode, check if
1248 * EA block can hold new_extra_isize bytes.
1250 if (EXT4_I(inode
)->i_file_acl
) {
1251 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1255 if (ext4_xattr_check_block(inode
, bh
)) {
1256 EXT4_ERROR_INODE(inode
, "bad block %llu",
1257 EXT4_I(inode
)->i_file_acl
);
1263 end
= bh
->b_data
+ bh
->b_size
;
1264 min_offs
= end
- base
;
1265 free
= ext4_xattr_free_space(first
, &min_offs
, base
,
1267 if (free
< new_extra_isize
) {
1268 if (!tried_min_extra_isize
&& s_min_extra_isize
) {
1269 tried_min_extra_isize
++;
1270 new_extra_isize
= s_min_extra_isize
;
1278 free
= inode
->i_sb
->s_blocksize
;
1281 while (new_extra_isize
> 0) {
1282 size_t offs
, size
, entry_size
;
1283 struct ext4_xattr_entry
*small_entry
= NULL
;
1284 struct ext4_xattr_info i
= {
1288 unsigned int total_size
; /* EA entry size + value size */
1289 unsigned int shift_bytes
; /* No. of bytes to shift EAs by? */
1290 unsigned int min_total_size
= ~0U;
1292 is
= kzalloc(sizeof(struct ext4_xattr_ibody_find
), GFP_NOFS
);
1293 bs
= kzalloc(sizeof(struct ext4_xattr_block_find
), GFP_NOFS
);
1299 is
->s
.not_found
= -ENODATA
;
1300 bs
->s
.not_found
= -ENODATA
;
1304 last
= IFIRST(header
);
1305 /* Find the entry best suited to be pushed into EA block */
1307 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1309 EXT4_XATTR_SIZE(le32_to_cpu(last
->e_value_size
)) +
1310 EXT4_XATTR_LEN(last
->e_name_len
);
1311 if (total_size
<= free
&& total_size
< min_total_size
) {
1312 if (total_size
< new_extra_isize
) {
1316 min_total_size
= total_size
;
1321 if (entry
== NULL
) {
1323 entry
= small_entry
;
1325 if (!tried_min_extra_isize
&&
1326 s_min_extra_isize
) {
1327 tried_min_extra_isize
++;
1328 new_extra_isize
= s_min_extra_isize
;
1335 offs
= le16_to_cpu(entry
->e_value_offs
);
1336 size
= le32_to_cpu(entry
->e_value_size
);
1337 entry_size
= EXT4_XATTR_LEN(entry
->e_name_len
);
1338 i
.name_index
= entry
->e_name_index
,
1339 buffer
= kmalloc(EXT4_XATTR_SIZE(size
), GFP_NOFS
);
1340 b_entry_name
= kmalloc(entry
->e_name_len
+ 1, GFP_NOFS
);
1341 if (!buffer
|| !b_entry_name
) {
1345 /* Save the entry name and the entry value */
1346 memcpy(buffer
, (void *)IFIRST(header
) + offs
,
1347 EXT4_XATTR_SIZE(size
));
1348 memcpy(b_entry_name
, entry
->e_name
, entry
->e_name_len
);
1349 b_entry_name
[entry
->e_name_len
] = '\0';
1350 i
.name
= b_entry_name
;
1352 error
= ext4_get_inode_loc(inode
, &is
->iloc
);
1356 error
= ext4_xattr_ibody_find(inode
, &i
, is
);
1360 /* Remove the chosen entry from the inode */
1361 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, is
);
1365 entry
= IFIRST(header
);
1366 if (entry_size
+ EXT4_XATTR_SIZE(size
) >= new_extra_isize
)
1367 shift_bytes
= new_extra_isize
;
1369 shift_bytes
= entry_size
+ size
;
1370 /* Adjust the offsets and shift the remaining entries ahead */
1371 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
-
1372 shift_bytes
, (void *)raw_inode
+
1373 EXT4_GOOD_OLD_INODE_SIZE
+ extra_isize
+ shift_bytes
,
1374 (void *)header
, total_ino
- entry_size
,
1375 inode
->i_sb
->s_blocksize
);
1377 extra_isize
+= shift_bytes
;
1378 new_extra_isize
-= shift_bytes
;
1379 EXT4_I(inode
)->i_extra_isize
= extra_isize
;
1381 i
.name
= b_entry_name
;
1384 error
= ext4_xattr_block_find(inode
, &i
, bs
);
1388 /* Add entry which was removed from the inode into the block */
1389 error
= ext4_xattr_block_set(handle
, inode
, &i
, bs
);
1392 kfree(b_entry_name
);
1394 b_entry_name
= NULL
;
1396 brelse(is
->iloc
.bh
);
1401 up_write(&EXT4_I(inode
)->xattr_sem
);
1405 kfree(b_entry_name
);
1408 brelse(is
->iloc
.bh
);
1412 up_write(&EXT4_I(inode
)->xattr_sem
);
1419 * ext4_xattr_delete_inode()
1421 * Free extended attribute resources associated with this inode. This
1422 * is called immediately before an inode is freed. We have exclusive
1423 * access to the inode.
1426 ext4_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
)
1428 struct buffer_head
*bh
= NULL
;
1430 if (!EXT4_I(inode
)->i_file_acl
)
1432 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1434 EXT4_ERROR_INODE(inode
, "block %llu read error",
1435 EXT4_I(inode
)->i_file_acl
);
1438 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
1439 BHDR(bh
)->h_blocks
!= cpu_to_le32(1)) {
1440 EXT4_ERROR_INODE(inode
, "bad block %llu",
1441 EXT4_I(inode
)->i_file_acl
);
1444 ext4_xattr_release_block(handle
, inode
, bh
);
1445 EXT4_I(inode
)->i_file_acl
= 0;
1452 * ext4_xattr_put_super()
1454 * This is called when a file system is unmounted.
1457 ext4_xattr_put_super(struct super_block
*sb
)
1459 mb_cache_shrink(sb
->s_bdev
);
1463 * ext4_xattr_cache_insert()
1465 * Create a new entry in the extended attribute cache, and insert
1466 * it unless such an entry is already in the cache.
1468 * Returns 0, or a negative error number on failure.
1471 ext4_xattr_cache_insert(struct buffer_head
*bh
)
1473 __u32 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
1474 struct mb_cache_entry
*ce
;
1477 ce
= mb_cache_entry_alloc(ext4_xattr_cache
, GFP_NOFS
);
1479 ea_bdebug(bh
, "out of memory");
1482 error
= mb_cache_entry_insert(ce
, bh
->b_bdev
, bh
->b_blocknr
, hash
);
1484 mb_cache_entry_free(ce
);
1485 if (error
== -EBUSY
) {
1486 ea_bdebug(bh
, "already in cache");
1490 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
1491 mb_cache_entry_release(ce
);
1498 * Compare two extended attribute blocks for equality.
1500 * Returns 0 if the blocks are equal, 1 if they differ, and
1501 * a negative error number on errors.
1504 ext4_xattr_cmp(struct ext4_xattr_header
*header1
,
1505 struct ext4_xattr_header
*header2
)
1507 struct ext4_xattr_entry
*entry1
, *entry2
;
1509 entry1
= ENTRY(header1
+1);
1510 entry2
= ENTRY(header2
+1);
1511 while (!IS_LAST_ENTRY(entry1
)) {
1512 if (IS_LAST_ENTRY(entry2
))
1514 if (entry1
->e_hash
!= entry2
->e_hash
||
1515 entry1
->e_name_index
!= entry2
->e_name_index
||
1516 entry1
->e_name_len
!= entry2
->e_name_len
||
1517 entry1
->e_value_size
!= entry2
->e_value_size
||
1518 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
1520 if (entry1
->e_value_block
!= 0 || entry2
->e_value_block
!= 0)
1522 if (memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
1523 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
1524 le32_to_cpu(entry1
->e_value_size
)))
1527 entry1
= EXT4_XATTR_NEXT(entry1
);
1528 entry2
= EXT4_XATTR_NEXT(entry2
);
1530 if (!IS_LAST_ENTRY(entry2
))
1536 * ext4_xattr_cache_find()
1538 * Find an identical extended attribute block.
1540 * Returns a pointer to the block found, or NULL if such a block was
1541 * not found or an error occurred.
1543 static struct buffer_head
*
1544 ext4_xattr_cache_find(struct inode
*inode
, struct ext4_xattr_header
*header
,
1545 struct mb_cache_entry
**pce
)
1547 __u32 hash
= le32_to_cpu(header
->h_hash
);
1548 struct mb_cache_entry
*ce
;
1550 if (!header
->h_hash
)
1551 return NULL
; /* never share */
1552 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
1554 ce
= mb_cache_entry_find_first(ext4_xattr_cache
, inode
->i_sb
->s_bdev
,
1557 struct buffer_head
*bh
;
1560 if (PTR_ERR(ce
) == -EAGAIN
)
1564 bh
= sb_bread(inode
->i_sb
, ce
->e_block
);
1566 EXT4_ERROR_INODE(inode
, "block %lu read error",
1567 (unsigned long) ce
->e_block
);
1568 } else if (le32_to_cpu(BHDR(bh
)->h_refcount
) >=
1569 EXT4_XATTR_REFCOUNT_MAX
) {
1570 ea_idebug(inode
, "block %lu refcount %d>=%d",
1571 (unsigned long) ce
->e_block
,
1572 le32_to_cpu(BHDR(bh
)->h_refcount
),
1573 EXT4_XATTR_REFCOUNT_MAX
);
1574 } else if (ext4_xattr_cmp(header
, BHDR(bh
)) == 0) {
1579 ce
= mb_cache_entry_find_next(ce
, inode
->i_sb
->s_bdev
, hash
);
1584 #define NAME_HASH_SHIFT 5
1585 #define VALUE_HASH_SHIFT 16
1588 * ext4_xattr_hash_entry()
1590 * Compute the hash of an extended attribute.
1592 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header
*header
,
1593 struct ext4_xattr_entry
*entry
)
1596 char *name
= entry
->e_name
;
1599 for (n
= 0; n
< entry
->e_name_len
; n
++) {
1600 hash
= (hash
<< NAME_HASH_SHIFT
) ^
1601 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
1605 if (entry
->e_value_block
== 0 && entry
->e_value_size
!= 0) {
1606 __le32
*value
= (__le32
*)((char *)header
+
1607 le16_to_cpu(entry
->e_value_offs
));
1608 for (n
= (le32_to_cpu(entry
->e_value_size
) +
1609 EXT4_XATTR_ROUND
) >> EXT4_XATTR_PAD_BITS
; n
; n
--) {
1610 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
1611 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
1612 le32_to_cpu(*value
++);
1615 entry
->e_hash
= cpu_to_le32(hash
);
1618 #undef NAME_HASH_SHIFT
1619 #undef VALUE_HASH_SHIFT
1621 #define BLOCK_HASH_SHIFT 16
1624 * ext4_xattr_rehash()
1626 * Re-compute the extended attribute hash value after an entry has changed.
1628 static void ext4_xattr_rehash(struct ext4_xattr_header
*header
,
1629 struct ext4_xattr_entry
*entry
)
1631 struct ext4_xattr_entry
*here
;
1634 ext4_xattr_hash_entry(header
, entry
);
1635 here
= ENTRY(header
+1);
1636 while (!IS_LAST_ENTRY(here
)) {
1637 if (!here
->e_hash
) {
1638 /* Block is not shared if an entry's hash value == 0 */
1642 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
1643 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
1644 le32_to_cpu(here
->e_hash
);
1645 here
= EXT4_XATTR_NEXT(here
);
1647 header
->h_hash
= cpu_to_le32(hash
);
1650 #undef BLOCK_HASH_SHIFT
1653 ext4_init_xattr(void)
1655 ext4_xattr_cache
= mb_cache_create("ext4_xattr", 6);
1656 if (!ext4_xattr_cache
)
1662 ext4_exit_xattr(void)
1664 if (ext4_xattr_cache
)
1665 mb_cache_destroy(ext4_xattr_cache
);
1666 ext4_xattr_cache
= NULL
;