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
);
130 struct ext4_inode_info
*ei
= EXT4_I(inode
);
133 old
= hdr
->h_checksum
;
135 if (le32_to_cpu(hdr
->h_refcount
) != 1) {
136 block_nr
= cpu_to_le64(block_nr
);
137 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&block_nr
,
140 csum
= ei
->i_csum_seed
;
141 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
,
142 EXT4_BLOCK_SIZE(inode
->i_sb
));
143 hdr
->h_checksum
= old
;
144 return cpu_to_le32(csum
);
147 static int ext4_xattr_block_csum_verify(struct inode
*inode
,
149 struct ext4_xattr_header
*hdr
)
151 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
152 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
) &&
153 (hdr
->h_checksum
!= ext4_xattr_block_csum(inode
, block_nr
, hdr
)))
158 static void ext4_xattr_block_csum_set(struct inode
*inode
,
160 struct ext4_xattr_header
*hdr
)
162 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
163 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
166 hdr
->h_checksum
= ext4_xattr_block_csum(inode
, block_nr
, hdr
);
169 static inline int ext4_handle_dirty_xattr_block(handle_t
*handle
,
171 struct buffer_head
*bh
)
173 ext4_xattr_block_csum_set(inode
, bh
->b_blocknr
, BHDR(bh
));
174 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
177 static inline const struct xattr_handler
*
178 ext4_xattr_handler(int name_index
)
180 const struct xattr_handler
*handler
= NULL
;
182 if (name_index
> 0 && name_index
< ARRAY_SIZE(ext4_xattr_handler_map
))
183 handler
= ext4_xattr_handler_map
[name_index
];
188 * Inode operation listxattr()
190 * dentry->d_inode->i_mutex: don't care
193 ext4_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
195 return ext4_xattr_list(dentry
, buffer
, size
);
199 ext4_xattr_check_names(struct ext4_xattr_entry
*entry
, void *end
)
201 while (!IS_LAST_ENTRY(entry
)) {
202 struct ext4_xattr_entry
*next
= EXT4_XATTR_NEXT(entry
);
203 if ((void *)next
>= end
)
211 ext4_xattr_check_block(struct inode
*inode
, struct buffer_head
*bh
)
215 if (buffer_verified(bh
))
218 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
219 BHDR(bh
)->h_blocks
!= cpu_to_le32(1))
221 if (!ext4_xattr_block_csum_verify(inode
, bh
->b_blocknr
, BHDR(bh
)))
223 error
= ext4_xattr_check_names(BFIRST(bh
), bh
->b_data
+ bh
->b_size
);
225 set_buffer_verified(bh
);
230 ext4_xattr_check_entry(struct ext4_xattr_entry
*entry
, size_t size
)
232 size_t value_size
= le32_to_cpu(entry
->e_value_size
);
234 if (entry
->e_value_block
!= 0 || value_size
> size
||
235 le16_to_cpu(entry
->e_value_offs
) + value_size
> size
)
241 ext4_xattr_find_entry(struct ext4_xattr_entry
**pentry
, int name_index
,
242 const char *name
, size_t size
, int sorted
)
244 struct ext4_xattr_entry
*entry
;
250 name_len
= strlen(name
);
252 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
253 cmp
= name_index
- entry
->e_name_index
;
255 cmp
= name_len
- entry
->e_name_len
;
257 cmp
= memcmp(name
, entry
->e_name
, name_len
);
258 if (cmp
<= 0 && (sorted
|| cmp
== 0))
262 if (!cmp
&& ext4_xattr_check_entry(entry
, size
))
264 return cmp
? -ENODATA
: 0;
268 ext4_xattr_block_get(struct inode
*inode
, int name_index
, const char *name
,
269 void *buffer
, size_t buffer_size
)
271 struct buffer_head
*bh
= NULL
;
272 struct ext4_xattr_entry
*entry
;
276 ea_idebug(inode
, "name=%d.%s, buffer=%p, buffer_size=%ld",
277 name_index
, name
, buffer
, (long)buffer_size
);
280 if (!EXT4_I(inode
)->i_file_acl
)
282 ea_idebug(inode
, "reading block %llu",
283 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
284 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
287 ea_bdebug(bh
, "b_count=%d, refcount=%d",
288 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
289 if (ext4_xattr_check_block(inode
, bh
)) {
291 EXT4_ERROR_INODE(inode
, "bad block %llu",
292 EXT4_I(inode
)->i_file_acl
);
296 ext4_xattr_cache_insert(bh
);
298 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, bh
->b_size
, 1);
303 size
= le32_to_cpu(entry
->e_value_size
);
306 if (size
> buffer_size
)
308 memcpy(buffer
, bh
->b_data
+ le16_to_cpu(entry
->e_value_offs
),
319 ext4_xattr_ibody_get(struct inode
*inode
, int name_index
, const char *name
,
320 void *buffer
, size_t buffer_size
)
322 struct ext4_xattr_ibody_header
*header
;
323 struct ext4_xattr_entry
*entry
;
324 struct ext4_inode
*raw_inode
;
325 struct ext4_iloc iloc
;
330 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
332 error
= ext4_get_inode_loc(inode
, &iloc
);
335 raw_inode
= ext4_raw_inode(&iloc
);
336 header
= IHDR(inode
, raw_inode
);
337 entry
= IFIRST(header
);
338 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
339 error
= ext4_xattr_check_names(entry
, end
);
342 error
= ext4_xattr_find_entry(&entry
, name_index
, name
,
343 end
- (void *)entry
, 0);
346 size
= le32_to_cpu(entry
->e_value_size
);
349 if (size
> buffer_size
)
351 memcpy(buffer
, (void *)IFIRST(header
) +
352 le16_to_cpu(entry
->e_value_offs
), size
);
364 * Copy an extended attribute into the buffer
365 * provided, or compute the buffer size required.
366 * Buffer is NULL to compute the size of the buffer required.
368 * Returns a negative error number on failure, or the number of bytes
369 * used / required on success.
372 ext4_xattr_get(struct inode
*inode
, int name_index
, const char *name
,
373 void *buffer
, size_t buffer_size
)
377 down_read(&EXT4_I(inode
)->xattr_sem
);
378 error
= ext4_xattr_ibody_get(inode
, name_index
, name
, buffer
,
380 if (error
== -ENODATA
)
381 error
= ext4_xattr_block_get(inode
, name_index
, name
, buffer
,
383 up_read(&EXT4_I(inode
)->xattr_sem
);
388 ext4_xattr_list_entries(struct dentry
*dentry
, struct ext4_xattr_entry
*entry
,
389 char *buffer
, size_t buffer_size
)
391 size_t rest
= buffer_size
;
393 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
394 const struct xattr_handler
*handler
=
395 ext4_xattr_handler(entry
->e_name_index
);
398 size_t size
= handler
->list(dentry
, buffer
, rest
,
410 return buffer_size
- rest
;
414 ext4_xattr_block_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
416 struct inode
*inode
= dentry
->d_inode
;
417 struct buffer_head
*bh
= NULL
;
420 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
421 buffer
, (long)buffer_size
);
424 if (!EXT4_I(inode
)->i_file_acl
)
426 ea_idebug(inode
, "reading block %llu",
427 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
428 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
432 ea_bdebug(bh
, "b_count=%d, refcount=%d",
433 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
434 if (ext4_xattr_check_block(inode
, bh
)) {
435 EXT4_ERROR_INODE(inode
, "bad block %llu",
436 EXT4_I(inode
)->i_file_acl
);
440 ext4_xattr_cache_insert(bh
);
441 error
= ext4_xattr_list_entries(dentry
, BFIRST(bh
), buffer
, buffer_size
);
450 ext4_xattr_ibody_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
452 struct inode
*inode
= dentry
->d_inode
;
453 struct ext4_xattr_ibody_header
*header
;
454 struct ext4_inode
*raw_inode
;
455 struct ext4_iloc iloc
;
459 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
461 error
= ext4_get_inode_loc(inode
, &iloc
);
464 raw_inode
= ext4_raw_inode(&iloc
);
465 header
= IHDR(inode
, raw_inode
);
466 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
467 error
= ext4_xattr_check_names(IFIRST(header
), end
);
470 error
= ext4_xattr_list_entries(dentry
, IFIRST(header
),
471 buffer
, buffer_size
);
481 * Copy a list of attribute names into the buffer
482 * provided, or compute the buffer size required.
483 * Buffer is NULL to compute the size of the buffer required.
485 * Returns a negative error number on failure, or the number of bytes
486 * used / required on success.
489 ext4_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
493 down_read(&EXT4_I(dentry
->d_inode
)->xattr_sem
);
494 ret
= ret2
= ext4_xattr_ibody_list(dentry
, buffer
, buffer_size
);
501 ret
= ext4_xattr_block_list(dentry
, buffer
, buffer_size
);
506 up_read(&EXT4_I(dentry
->d_inode
)->xattr_sem
);
511 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
514 static void ext4_xattr_update_super_block(handle_t
*handle
,
515 struct super_block
*sb
)
517 if (EXT4_HAS_COMPAT_FEATURE(sb
, EXT4_FEATURE_COMPAT_EXT_ATTR
))
520 if (ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
) == 0) {
521 EXT4_SET_COMPAT_FEATURE(sb
, EXT4_FEATURE_COMPAT_EXT_ATTR
);
522 ext4_handle_dirty_super(handle
, sb
);
527 * Release the xattr block BH: If the reference count is > 1, decrement
528 * it; otherwise free the block.
531 ext4_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
532 struct buffer_head
*bh
)
534 struct mb_cache_entry
*ce
= NULL
;
537 ce
= mb_cache_entry_get(ext4_xattr_cache
, bh
->b_bdev
, bh
->b_blocknr
);
538 error
= ext4_journal_get_write_access(handle
, bh
);
543 if (BHDR(bh
)->h_refcount
== cpu_to_le32(1)) {
544 ea_bdebug(bh
, "refcount now=0; freeing");
546 mb_cache_entry_free(ce
);
548 ext4_free_blocks(handle
, inode
, bh
, 0, 1,
549 EXT4_FREE_BLOCKS_METADATA
|
550 EXT4_FREE_BLOCKS_FORGET
);
553 le32_add_cpu(&BHDR(bh
)->h_refcount
, -1);
555 mb_cache_entry_release(ce
);
557 error
= ext4_handle_dirty_xattr_block(handle
, inode
, bh
);
559 ext4_handle_sync(handle
);
560 dquot_free_block(inode
, 1);
561 ea_bdebug(bh
, "refcount now=%d; releasing",
562 le32_to_cpu(BHDR(bh
)->h_refcount
));
565 ext4_std_error(inode
->i_sb
, error
);
570 * Find the available free space for EAs. This also returns the total number of
571 * bytes used by EA entries.
573 static size_t ext4_xattr_free_space(struct ext4_xattr_entry
*last
,
574 size_t *min_offs
, void *base
, int *total
)
576 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
577 *total
+= EXT4_XATTR_LEN(last
->e_name_len
);
578 if (!last
->e_value_block
&& last
->e_value_size
) {
579 size_t offs
= le16_to_cpu(last
->e_value_offs
);
580 if (offs
< *min_offs
)
584 return (*min_offs
- ((void *)last
- base
) - sizeof(__u32
));
587 struct ext4_xattr_info
{
594 struct ext4_xattr_search
{
595 struct ext4_xattr_entry
*first
;
598 struct ext4_xattr_entry
*here
;
603 ext4_xattr_set_entry(struct ext4_xattr_info
*i
, struct ext4_xattr_search
*s
)
605 struct ext4_xattr_entry
*last
;
606 size_t free
, min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
608 /* Compute min_offs and last. */
610 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
611 if (!last
->e_value_block
&& last
->e_value_size
) {
612 size_t offs
= le16_to_cpu(last
->e_value_offs
);
617 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
619 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
620 size_t size
= le32_to_cpu(s
->here
->e_value_size
);
621 free
+= EXT4_XATTR_SIZE(size
);
623 free
+= EXT4_XATTR_LEN(name_len
);
626 if (free
< EXT4_XATTR_SIZE(i
->value_len
) ||
627 free
< EXT4_XATTR_LEN(name_len
) +
628 EXT4_XATTR_SIZE(i
->value_len
))
632 if (i
->value
&& s
->not_found
) {
633 /* Insert the new name. */
634 size_t size
= EXT4_XATTR_LEN(name_len
);
635 size_t rest
= (void *)last
- (void *)s
->here
+ sizeof(__u32
);
636 memmove((void *)s
->here
+ size
, s
->here
, rest
);
637 memset(s
->here
, 0, size
);
638 s
->here
->e_name_index
= i
->name_index
;
639 s
->here
->e_name_len
= name_len
;
640 memcpy(s
->here
->e_name
, i
->name
, name_len
);
642 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
643 void *first_val
= s
->base
+ min_offs
;
644 size_t offs
= le16_to_cpu(s
->here
->e_value_offs
);
645 void *val
= s
->base
+ offs
;
646 size_t size
= EXT4_XATTR_SIZE(
647 le32_to_cpu(s
->here
->e_value_size
));
649 if (i
->value
&& size
== EXT4_XATTR_SIZE(i
->value_len
)) {
650 /* The old and the new value have the same
651 size. Just replace. */
652 s
->here
->e_value_size
=
653 cpu_to_le32(i
->value_len
);
654 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
655 EXT4_XATTR_PAD
); /* Clear pad bytes. */
656 memcpy(val
, i
->value
, i
->value_len
);
660 /* Remove the old value. */
661 memmove(first_val
+ size
, first_val
, val
- first_val
);
662 memset(first_val
, 0, size
);
663 s
->here
->e_value_size
= 0;
664 s
->here
->e_value_offs
= 0;
667 /* Adjust all value offsets. */
669 while (!IS_LAST_ENTRY(last
)) {
670 size_t o
= le16_to_cpu(last
->e_value_offs
);
671 if (!last
->e_value_block
&&
672 last
->e_value_size
&& o
< offs
)
674 cpu_to_le16(o
+ size
);
675 last
= EXT4_XATTR_NEXT(last
);
679 /* Remove the old name. */
680 size_t size
= EXT4_XATTR_LEN(name_len
);
681 last
= ENTRY((void *)last
- size
);
682 memmove(s
->here
, (void *)s
->here
+ size
,
683 (void *)last
- (void *)s
->here
+ sizeof(__u32
));
684 memset(last
, 0, size
);
689 /* Insert the new value. */
690 s
->here
->e_value_size
= cpu_to_le32(i
->value_len
);
692 size_t size
= EXT4_XATTR_SIZE(i
->value_len
);
693 void *val
= s
->base
+ min_offs
- size
;
694 s
->here
->e_value_offs
= cpu_to_le16(min_offs
- size
);
695 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
696 EXT4_XATTR_PAD
); /* Clear the pad bytes. */
697 memcpy(val
, i
->value
, i
->value_len
);
703 struct ext4_xattr_block_find
{
704 struct ext4_xattr_search s
;
705 struct buffer_head
*bh
;
709 ext4_xattr_block_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
710 struct ext4_xattr_block_find
*bs
)
712 struct super_block
*sb
= inode
->i_sb
;
715 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
716 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
718 if (EXT4_I(inode
)->i_file_acl
) {
719 /* The inode already has an extended attribute block. */
720 bs
->bh
= sb_bread(sb
, EXT4_I(inode
)->i_file_acl
);
724 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
725 atomic_read(&(bs
->bh
->b_count
)),
726 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
727 if (ext4_xattr_check_block(inode
, bs
->bh
)) {
728 EXT4_ERROR_INODE(inode
, "bad block %llu",
729 EXT4_I(inode
)->i_file_acl
);
733 /* Find the named attribute. */
734 bs
->s
.base
= BHDR(bs
->bh
);
735 bs
->s
.first
= BFIRST(bs
->bh
);
736 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
737 bs
->s
.here
= bs
->s
.first
;
738 error
= ext4_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
739 i
->name
, bs
->bh
->b_size
, 1);
740 if (error
&& error
!= -ENODATA
)
742 bs
->s
.not_found
= error
;
751 ext4_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
752 struct ext4_xattr_info
*i
,
753 struct ext4_xattr_block_find
*bs
)
755 struct super_block
*sb
= inode
->i_sb
;
756 struct buffer_head
*new_bh
= NULL
;
757 struct ext4_xattr_search
*s
= &bs
->s
;
758 struct mb_cache_entry
*ce
= NULL
;
761 #define header(x) ((struct ext4_xattr_header *)(x))
763 if (i
->value
&& i
->value_len
> sb
->s_blocksize
)
766 ce
= mb_cache_entry_get(ext4_xattr_cache
, bs
->bh
->b_bdev
,
768 error
= ext4_journal_get_write_access(handle
, bs
->bh
);
773 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
775 mb_cache_entry_free(ce
);
778 ea_bdebug(bs
->bh
, "modifying in-place");
779 error
= ext4_xattr_set_entry(i
, s
);
781 if (!IS_LAST_ENTRY(s
->first
))
782 ext4_xattr_rehash(header(s
->base
),
784 ext4_xattr_cache_insert(bs
->bh
);
786 unlock_buffer(bs
->bh
);
790 error
= ext4_handle_dirty_xattr_block(handle
,
797 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
799 unlock_buffer(bs
->bh
);
800 ext4_handle_release_buffer(handle
, bs
->bh
);
802 mb_cache_entry_release(ce
);
805 ea_bdebug(bs
->bh
, "cloning");
806 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
810 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
811 s
->first
= ENTRY(header(s
->base
)+1);
812 header(s
->base
)->h_refcount
= cpu_to_le32(1);
813 s
->here
= ENTRY(s
->base
+ offset
);
814 s
->end
= s
->base
+ bs
->bh
->b_size
;
817 /* Allocate a buffer where we construct the new block. */
818 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
819 /* assert(header == s->base) */
823 header(s
->base
)->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
824 header(s
->base
)->h_blocks
= cpu_to_le32(1);
825 header(s
->base
)->h_refcount
= cpu_to_le32(1);
826 s
->first
= ENTRY(header(s
->base
)+1);
827 s
->here
= ENTRY(header(s
->base
)+1);
828 s
->end
= s
->base
+ sb
->s_blocksize
;
831 error
= ext4_xattr_set_entry(i
, s
);
836 if (!IS_LAST_ENTRY(s
->first
))
837 ext4_xattr_rehash(header(s
->base
), s
->here
);
840 if (!IS_LAST_ENTRY(s
->first
)) {
841 new_bh
= ext4_xattr_cache_find(inode
, header(s
->base
), &ce
);
843 /* We found an identical block in the cache. */
844 if (new_bh
== bs
->bh
)
845 ea_bdebug(new_bh
, "keeping");
847 /* The old block is released after updating
849 error
= dquot_alloc_block(inode
, 1);
852 error
= ext4_journal_get_write_access(handle
,
857 le32_add_cpu(&BHDR(new_bh
)->h_refcount
, 1);
858 ea_bdebug(new_bh
, "reusing; refcount now=%d",
859 le32_to_cpu(BHDR(new_bh
)->h_refcount
));
860 unlock_buffer(new_bh
);
861 error
= ext4_handle_dirty_xattr_block(handle
,
867 mb_cache_entry_release(ce
);
869 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
870 /* We were modifying this block in-place. */
871 ea_bdebug(bs
->bh
, "keeping this block");
875 /* We need to allocate a new block */
876 ext4_fsblk_t goal
, block
;
878 goal
= ext4_group_first_block_no(sb
,
879 EXT4_I(inode
)->i_block_group
);
881 /* non-extent files can't have physical blocks past 2^32 */
882 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
883 goal
= goal
& EXT4_MAX_BLOCK_FILE_PHYS
;
886 * take i_data_sem because we will test
887 * i_delalloc_reserved_flag in ext4_mb_new_blocks
889 down_read((&EXT4_I(inode
)->i_data_sem
));
890 block
= ext4_new_meta_blocks(handle
, inode
, goal
, 0,
892 up_read((&EXT4_I(inode
)->i_data_sem
));
896 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
897 BUG_ON(block
> EXT4_MAX_BLOCK_FILE_PHYS
);
899 ea_idebug(inode
, "creating block %llu",
900 (unsigned long long)block
);
902 new_bh
= sb_getblk(sb
, block
);
905 ext4_free_blocks(handle
, inode
, NULL
, block
, 1,
906 EXT4_FREE_BLOCKS_METADATA
);
911 error
= ext4_journal_get_create_access(handle
, new_bh
);
913 unlock_buffer(new_bh
);
916 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
917 set_buffer_uptodate(new_bh
);
918 unlock_buffer(new_bh
);
919 ext4_xattr_cache_insert(new_bh
);
920 error
= ext4_handle_dirty_xattr_block(handle
,
927 /* Update the inode. */
928 EXT4_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
930 /* Drop the previous xattr block. */
931 if (bs
->bh
&& bs
->bh
!= new_bh
)
932 ext4_xattr_release_block(handle
, inode
, bs
->bh
);
937 mb_cache_entry_release(ce
);
939 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
945 dquot_free_block(inode
, 1);
949 EXT4_ERROR_INODE(inode
, "bad block %llu",
950 EXT4_I(inode
)->i_file_acl
);
956 struct ext4_xattr_ibody_find
{
957 struct ext4_xattr_search s
;
958 struct ext4_iloc iloc
;
962 ext4_xattr_ibody_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
963 struct ext4_xattr_ibody_find
*is
)
965 struct ext4_xattr_ibody_header
*header
;
966 struct ext4_inode
*raw_inode
;
969 if (EXT4_I(inode
)->i_extra_isize
== 0)
971 raw_inode
= ext4_raw_inode(&is
->iloc
);
972 header
= IHDR(inode
, raw_inode
);
973 is
->s
.base
= is
->s
.first
= IFIRST(header
);
974 is
->s
.here
= is
->s
.first
;
975 is
->s
.end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
976 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
977 error
= ext4_xattr_check_names(IFIRST(header
), is
->s
.end
);
980 /* Find the named attribute. */
981 error
= ext4_xattr_find_entry(&is
->s
.here
, i
->name_index
,
983 (void *)is
->s
.base
, 0);
984 if (error
&& error
!= -ENODATA
)
986 is
->s
.not_found
= error
;
992 ext4_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
993 struct ext4_xattr_info
*i
,
994 struct ext4_xattr_ibody_find
*is
)
996 struct ext4_xattr_ibody_header
*header
;
997 struct ext4_xattr_search
*s
= &is
->s
;
1000 if (EXT4_I(inode
)->i_extra_isize
== 0)
1002 error
= ext4_xattr_set_entry(i
, s
);
1005 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1006 if (!IS_LAST_ENTRY(s
->first
)) {
1007 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1008 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1010 header
->h_magic
= cpu_to_le32(0);
1011 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1017 * ext4_xattr_set_handle()
1019 * Create, replace or remove an extended attribute for this inode. Value
1020 * is NULL to remove an existing extended attribute, and non-NULL to
1021 * either replace an existing extended attribute, or create a new extended
1022 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1023 * specify that an extended attribute must exist and must not exist
1024 * previous to the call, respectively.
1026 * Returns 0, or a negative error number on failure.
1029 ext4_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
1030 const char *name
, const void *value
, size_t value_len
,
1033 struct ext4_xattr_info i
= {
1034 .name_index
= name_index
,
1037 .value_len
= value_len
,
1040 struct ext4_xattr_ibody_find is
= {
1041 .s
= { .not_found
= -ENODATA
, },
1043 struct ext4_xattr_block_find bs
= {
1044 .s
= { .not_found
= -ENODATA
, },
1046 unsigned long no_expand
;
1051 if (strlen(name
) > 255)
1053 down_write(&EXT4_I(inode
)->xattr_sem
);
1054 no_expand
= ext4_test_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1055 ext4_set_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1057 error
= ext4_reserve_inode_write(handle
, inode
, &is
.iloc
);
1061 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
)) {
1062 struct ext4_inode
*raw_inode
= ext4_raw_inode(&is
.iloc
);
1063 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
1064 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
1067 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
1071 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1074 if (is
.s
.not_found
&& bs
.s
.not_found
) {
1076 if (flags
& XATTR_REPLACE
)
1083 if (flags
& XATTR_CREATE
)
1087 if (!is
.s
.not_found
)
1088 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1089 else if (!bs
.s
.not_found
)
1090 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1092 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1093 if (!error
&& !bs
.s
.not_found
) {
1095 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1096 } else if (error
== -ENOSPC
) {
1097 if (EXT4_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
1098 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1102 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1105 if (!is
.s
.not_found
) {
1107 error
= ext4_xattr_ibody_set(handle
, inode
, &i
,
1113 ext4_xattr_update_super_block(handle
, inode
->i_sb
);
1114 inode
->i_ctime
= ext4_current_time(inode
);
1116 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1117 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
1119 * The bh is consumed by ext4_mark_iloc_dirty, even with
1124 ext4_handle_sync(handle
);
1131 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1132 up_write(&EXT4_I(inode
)->xattr_sem
);
1139 * Like ext4_xattr_set_handle, but start from an inode. This extended
1140 * attribute modification is a filesystem transaction by itself.
1142 * Returns 0, or a negative error number on failure.
1145 ext4_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
1146 const void *value
, size_t value_len
, int flags
)
1149 int error
, retries
= 0;
1152 handle
= ext4_journal_start(inode
, EXT4_DATA_TRANS_BLOCKS(inode
->i_sb
));
1153 if (IS_ERR(handle
)) {
1154 error
= PTR_ERR(handle
);
1158 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, name
,
1159 value
, value_len
, flags
);
1160 error2
= ext4_journal_stop(handle
);
1161 if (error
== -ENOSPC
&&
1162 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1172 * Shift the EA entries in the inode to create space for the increased
1175 static void ext4_xattr_shift_entries(struct ext4_xattr_entry
*entry
,
1176 int value_offs_shift
, void *to
,
1177 void *from
, size_t n
, int blocksize
)
1179 struct ext4_xattr_entry
*last
= entry
;
1182 /* Adjust the value offsets of the entries */
1183 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1184 if (!last
->e_value_block
&& last
->e_value_size
) {
1185 new_offs
= le16_to_cpu(last
->e_value_offs
) +
1187 BUG_ON(new_offs
+ le32_to_cpu(last
->e_value_size
)
1189 last
->e_value_offs
= cpu_to_le16(new_offs
);
1192 /* Shift the entries by n bytes */
1193 memmove(to
, from
, n
);
1197 * Expand an inode by new_extra_isize bytes when EAs are present.
1198 * Returns 0 on success or negative error number on failure.
1200 int ext4_expand_extra_isize_ea(struct inode
*inode
, int new_extra_isize
,
1201 struct ext4_inode
*raw_inode
, handle_t
*handle
)
1203 struct ext4_xattr_ibody_header
*header
;
1204 struct ext4_xattr_entry
*entry
, *last
, *first
;
1205 struct buffer_head
*bh
= NULL
;
1206 struct ext4_xattr_ibody_find
*is
= NULL
;
1207 struct ext4_xattr_block_find
*bs
= NULL
;
1208 char *buffer
= NULL
, *b_entry_name
= NULL
;
1209 size_t min_offs
, free
;
1210 int total_ino
, total_blk
;
1211 void *base
, *start
, *end
;
1212 int extra_isize
= 0, error
= 0, tried_min_extra_isize
= 0;
1213 int s_min_extra_isize
= le16_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_min_extra_isize
);
1215 down_write(&EXT4_I(inode
)->xattr_sem
);
1217 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
) {
1218 up_write(&EXT4_I(inode
)->xattr_sem
);
1222 header
= IHDR(inode
, raw_inode
);
1223 entry
= IFIRST(header
);
1226 * Check if enough free space is available in the inode to shift the
1227 * entries ahead by new_extra_isize.
1230 base
= start
= entry
;
1231 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1232 min_offs
= end
- base
;
1234 total_ino
= sizeof(struct ext4_xattr_ibody_header
);
1236 free
= ext4_xattr_free_space(last
, &min_offs
, base
, &total_ino
);
1237 if (free
>= new_extra_isize
) {
1238 entry
= IFIRST(header
);
1239 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
1240 - new_extra_isize
, (void *)raw_inode
+
1241 EXT4_GOOD_OLD_INODE_SIZE
+ new_extra_isize
,
1242 (void *)header
, total_ino
,
1243 inode
->i_sb
->s_blocksize
);
1244 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
1250 * Enough free space isn't available in the inode, check if
1251 * EA block can hold new_extra_isize bytes.
1253 if (EXT4_I(inode
)->i_file_acl
) {
1254 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1258 if (ext4_xattr_check_block(inode
, bh
)) {
1259 EXT4_ERROR_INODE(inode
, "bad block %llu",
1260 EXT4_I(inode
)->i_file_acl
);
1266 end
= bh
->b_data
+ bh
->b_size
;
1267 min_offs
= end
- base
;
1268 free
= ext4_xattr_free_space(first
, &min_offs
, base
,
1270 if (free
< new_extra_isize
) {
1271 if (!tried_min_extra_isize
&& s_min_extra_isize
) {
1272 tried_min_extra_isize
++;
1273 new_extra_isize
= s_min_extra_isize
;
1281 free
= inode
->i_sb
->s_blocksize
;
1284 while (new_extra_isize
> 0) {
1285 size_t offs
, size
, entry_size
;
1286 struct ext4_xattr_entry
*small_entry
= NULL
;
1287 struct ext4_xattr_info i
= {
1291 unsigned int total_size
; /* EA entry size + value size */
1292 unsigned int shift_bytes
; /* No. of bytes to shift EAs by? */
1293 unsigned int min_total_size
= ~0U;
1295 is
= kzalloc(sizeof(struct ext4_xattr_ibody_find
), GFP_NOFS
);
1296 bs
= kzalloc(sizeof(struct ext4_xattr_block_find
), GFP_NOFS
);
1302 is
->s
.not_found
= -ENODATA
;
1303 bs
->s
.not_found
= -ENODATA
;
1307 last
= IFIRST(header
);
1308 /* Find the entry best suited to be pushed into EA block */
1310 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1312 EXT4_XATTR_SIZE(le32_to_cpu(last
->e_value_size
)) +
1313 EXT4_XATTR_LEN(last
->e_name_len
);
1314 if (total_size
<= free
&& total_size
< min_total_size
) {
1315 if (total_size
< new_extra_isize
) {
1319 min_total_size
= total_size
;
1324 if (entry
== NULL
) {
1326 entry
= small_entry
;
1328 if (!tried_min_extra_isize
&&
1329 s_min_extra_isize
) {
1330 tried_min_extra_isize
++;
1331 new_extra_isize
= s_min_extra_isize
;
1338 offs
= le16_to_cpu(entry
->e_value_offs
);
1339 size
= le32_to_cpu(entry
->e_value_size
);
1340 entry_size
= EXT4_XATTR_LEN(entry
->e_name_len
);
1341 i
.name_index
= entry
->e_name_index
,
1342 buffer
= kmalloc(EXT4_XATTR_SIZE(size
), GFP_NOFS
);
1343 b_entry_name
= kmalloc(entry
->e_name_len
+ 1, GFP_NOFS
);
1344 if (!buffer
|| !b_entry_name
) {
1348 /* Save the entry name and the entry value */
1349 memcpy(buffer
, (void *)IFIRST(header
) + offs
,
1350 EXT4_XATTR_SIZE(size
));
1351 memcpy(b_entry_name
, entry
->e_name
, entry
->e_name_len
);
1352 b_entry_name
[entry
->e_name_len
] = '\0';
1353 i
.name
= b_entry_name
;
1355 error
= ext4_get_inode_loc(inode
, &is
->iloc
);
1359 error
= ext4_xattr_ibody_find(inode
, &i
, is
);
1363 /* Remove the chosen entry from the inode */
1364 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, is
);
1368 entry
= IFIRST(header
);
1369 if (entry_size
+ EXT4_XATTR_SIZE(size
) >= new_extra_isize
)
1370 shift_bytes
= new_extra_isize
;
1372 shift_bytes
= entry_size
+ size
;
1373 /* Adjust the offsets and shift the remaining entries ahead */
1374 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
-
1375 shift_bytes
, (void *)raw_inode
+
1376 EXT4_GOOD_OLD_INODE_SIZE
+ extra_isize
+ shift_bytes
,
1377 (void *)header
, total_ino
- entry_size
,
1378 inode
->i_sb
->s_blocksize
);
1380 extra_isize
+= shift_bytes
;
1381 new_extra_isize
-= shift_bytes
;
1382 EXT4_I(inode
)->i_extra_isize
= extra_isize
;
1384 i
.name
= b_entry_name
;
1387 error
= ext4_xattr_block_find(inode
, &i
, bs
);
1391 /* Add entry which was removed from the inode into the block */
1392 error
= ext4_xattr_block_set(handle
, inode
, &i
, bs
);
1395 kfree(b_entry_name
);
1397 b_entry_name
= NULL
;
1399 brelse(is
->iloc
.bh
);
1404 up_write(&EXT4_I(inode
)->xattr_sem
);
1408 kfree(b_entry_name
);
1411 brelse(is
->iloc
.bh
);
1415 up_write(&EXT4_I(inode
)->xattr_sem
);
1422 * ext4_xattr_delete_inode()
1424 * Free extended attribute resources associated with this inode. This
1425 * is called immediately before an inode is freed. We have exclusive
1426 * access to the inode.
1429 ext4_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
)
1431 struct buffer_head
*bh
= NULL
;
1433 if (!EXT4_I(inode
)->i_file_acl
)
1435 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1437 EXT4_ERROR_INODE(inode
, "block %llu read error",
1438 EXT4_I(inode
)->i_file_acl
);
1441 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
1442 BHDR(bh
)->h_blocks
!= cpu_to_le32(1)) {
1443 EXT4_ERROR_INODE(inode
, "bad block %llu",
1444 EXT4_I(inode
)->i_file_acl
);
1447 ext4_xattr_release_block(handle
, inode
, bh
);
1448 EXT4_I(inode
)->i_file_acl
= 0;
1455 * ext4_xattr_put_super()
1457 * This is called when a file system is unmounted.
1460 ext4_xattr_put_super(struct super_block
*sb
)
1462 mb_cache_shrink(sb
->s_bdev
);
1466 * ext4_xattr_cache_insert()
1468 * Create a new entry in the extended attribute cache, and insert
1469 * it unless such an entry is already in the cache.
1471 * Returns 0, or a negative error number on failure.
1474 ext4_xattr_cache_insert(struct buffer_head
*bh
)
1476 __u32 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
1477 struct mb_cache_entry
*ce
;
1480 ce
= mb_cache_entry_alloc(ext4_xattr_cache
, GFP_NOFS
);
1482 ea_bdebug(bh
, "out of memory");
1485 error
= mb_cache_entry_insert(ce
, bh
->b_bdev
, bh
->b_blocknr
, hash
);
1487 mb_cache_entry_free(ce
);
1488 if (error
== -EBUSY
) {
1489 ea_bdebug(bh
, "already in cache");
1493 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
1494 mb_cache_entry_release(ce
);
1501 * Compare two extended attribute blocks for equality.
1503 * Returns 0 if the blocks are equal, 1 if they differ, and
1504 * a negative error number on errors.
1507 ext4_xattr_cmp(struct ext4_xattr_header
*header1
,
1508 struct ext4_xattr_header
*header2
)
1510 struct ext4_xattr_entry
*entry1
, *entry2
;
1512 entry1
= ENTRY(header1
+1);
1513 entry2
= ENTRY(header2
+1);
1514 while (!IS_LAST_ENTRY(entry1
)) {
1515 if (IS_LAST_ENTRY(entry2
))
1517 if (entry1
->e_hash
!= entry2
->e_hash
||
1518 entry1
->e_name_index
!= entry2
->e_name_index
||
1519 entry1
->e_name_len
!= entry2
->e_name_len
||
1520 entry1
->e_value_size
!= entry2
->e_value_size
||
1521 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
1523 if (entry1
->e_value_block
!= 0 || entry2
->e_value_block
!= 0)
1525 if (memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
1526 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
1527 le32_to_cpu(entry1
->e_value_size
)))
1530 entry1
= EXT4_XATTR_NEXT(entry1
);
1531 entry2
= EXT4_XATTR_NEXT(entry2
);
1533 if (!IS_LAST_ENTRY(entry2
))
1539 * ext4_xattr_cache_find()
1541 * Find an identical extended attribute block.
1543 * Returns a pointer to the block found, or NULL if such a block was
1544 * not found or an error occurred.
1546 static struct buffer_head
*
1547 ext4_xattr_cache_find(struct inode
*inode
, struct ext4_xattr_header
*header
,
1548 struct mb_cache_entry
**pce
)
1550 __u32 hash
= le32_to_cpu(header
->h_hash
);
1551 struct mb_cache_entry
*ce
;
1553 if (!header
->h_hash
)
1554 return NULL
; /* never share */
1555 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
1557 ce
= mb_cache_entry_find_first(ext4_xattr_cache
, inode
->i_sb
->s_bdev
,
1560 struct buffer_head
*bh
;
1563 if (PTR_ERR(ce
) == -EAGAIN
)
1567 bh
= sb_bread(inode
->i_sb
, ce
->e_block
);
1569 EXT4_ERROR_INODE(inode
, "block %lu read error",
1570 (unsigned long) ce
->e_block
);
1571 } else if (le32_to_cpu(BHDR(bh
)->h_refcount
) >=
1572 EXT4_XATTR_REFCOUNT_MAX
) {
1573 ea_idebug(inode
, "block %lu refcount %d>=%d",
1574 (unsigned long) ce
->e_block
,
1575 le32_to_cpu(BHDR(bh
)->h_refcount
),
1576 EXT4_XATTR_REFCOUNT_MAX
);
1577 } else if (ext4_xattr_cmp(header
, BHDR(bh
)) == 0) {
1582 ce
= mb_cache_entry_find_next(ce
, inode
->i_sb
->s_bdev
, hash
);
1587 #define NAME_HASH_SHIFT 5
1588 #define VALUE_HASH_SHIFT 16
1591 * ext4_xattr_hash_entry()
1593 * Compute the hash of an extended attribute.
1595 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header
*header
,
1596 struct ext4_xattr_entry
*entry
)
1599 char *name
= entry
->e_name
;
1602 for (n
= 0; n
< entry
->e_name_len
; n
++) {
1603 hash
= (hash
<< NAME_HASH_SHIFT
) ^
1604 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
1608 if (entry
->e_value_block
== 0 && entry
->e_value_size
!= 0) {
1609 __le32
*value
= (__le32
*)((char *)header
+
1610 le16_to_cpu(entry
->e_value_offs
));
1611 for (n
= (le32_to_cpu(entry
->e_value_size
) +
1612 EXT4_XATTR_ROUND
) >> EXT4_XATTR_PAD_BITS
; n
; n
--) {
1613 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
1614 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
1615 le32_to_cpu(*value
++);
1618 entry
->e_hash
= cpu_to_le32(hash
);
1621 #undef NAME_HASH_SHIFT
1622 #undef VALUE_HASH_SHIFT
1624 #define BLOCK_HASH_SHIFT 16
1627 * ext4_xattr_rehash()
1629 * Re-compute the extended attribute hash value after an entry has changed.
1631 static void ext4_xattr_rehash(struct ext4_xattr_header
*header
,
1632 struct ext4_xattr_entry
*entry
)
1634 struct ext4_xattr_entry
*here
;
1637 ext4_xattr_hash_entry(header
, entry
);
1638 here
= ENTRY(header
+1);
1639 while (!IS_LAST_ENTRY(here
)) {
1640 if (!here
->e_hash
) {
1641 /* Block is not shared if an entry's hash value == 0 */
1645 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
1646 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
1647 le32_to_cpu(here
->e_hash
);
1648 here
= EXT4_XATTR_NEXT(here
);
1650 header
->h_hash
= cpu_to_le32(hash
);
1653 #undef BLOCK_HASH_SHIFT
1656 ext4_init_xattr(void)
1658 ext4_xattr_cache
= mb_cache_create("ext4_xattr", 6);
1659 if (!ext4_xattr_cache
)
1665 ext4_exit_xattr(void)
1667 if (ext4_xattr_cache
)
1668 mb_cache_destroy(ext4_xattr_cache
);
1669 ext4_xattr_cache
= NULL
;