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 #ifdef EXT4_XATTR_DEBUG
65 # define ea_idebug(inode, f...) do { \
66 printk(KERN_DEBUG "inode %s:%lu: ", \
67 inode->i_sb->s_id, inode->i_ino); \
71 # define ea_bdebug(bh, f...) do { \
72 char b[BDEVNAME_SIZE]; \
73 printk(KERN_DEBUG "block %s:%lu: ", \
74 bdevname(bh->b_bdev, b), \
75 (unsigned long) bh->b_blocknr); \
80 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
81 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
84 static void ext4_xattr_cache_insert(struct buffer_head
*);
85 static struct buffer_head
*ext4_xattr_cache_find(struct inode
*,
86 struct ext4_xattr_header
*,
87 struct mb_cache_entry
**);
88 static void ext4_xattr_rehash(struct ext4_xattr_header
*,
89 struct ext4_xattr_entry
*);
90 static int ext4_xattr_list(struct dentry
*dentry
, char *buffer
,
93 static struct mb_cache
*ext4_xattr_cache
;
95 static const struct xattr_handler
*ext4_xattr_handler_map
[] = {
96 [EXT4_XATTR_INDEX_USER
] = &ext4_xattr_user_handler
,
97 #ifdef CONFIG_EXT4_FS_POSIX_ACL
98 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS
] = &ext4_xattr_acl_access_handler
,
99 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT
] = &ext4_xattr_acl_default_handler
,
101 [EXT4_XATTR_INDEX_TRUSTED
] = &ext4_xattr_trusted_handler
,
102 #ifdef CONFIG_EXT4_FS_SECURITY
103 [EXT4_XATTR_INDEX_SECURITY
] = &ext4_xattr_security_handler
,
107 const struct xattr_handler
*ext4_xattr_handlers
[] = {
108 &ext4_xattr_user_handler
,
109 &ext4_xattr_trusted_handler
,
110 #ifdef CONFIG_EXT4_FS_POSIX_ACL
111 &ext4_xattr_acl_access_handler
,
112 &ext4_xattr_acl_default_handler
,
114 #ifdef CONFIG_EXT4_FS_SECURITY
115 &ext4_xattr_security_handler
,
120 static __le32
ext4_xattr_block_csum(struct inode
*inode
,
122 struct ext4_xattr_header
*hdr
)
124 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
127 __le64 dsk_block_nr
= cpu_to_le64(block_nr
);
129 save_csum
= hdr
->h_checksum
;
131 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&dsk_block_nr
,
132 sizeof(dsk_block_nr
));
133 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
,
134 EXT4_BLOCK_SIZE(inode
->i_sb
));
136 hdr
->h_checksum
= save_csum
;
137 return cpu_to_le32(csum
);
140 static int ext4_xattr_block_csum_verify(struct inode
*inode
,
142 struct ext4_xattr_header
*hdr
)
144 if (EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
145 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
) &&
146 (hdr
->h_checksum
!= ext4_xattr_block_csum(inode
, block_nr
, hdr
)))
151 static void ext4_xattr_block_csum_set(struct inode
*inode
,
153 struct ext4_xattr_header
*hdr
)
155 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode
->i_sb
,
156 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
))
159 hdr
->h_checksum
= ext4_xattr_block_csum(inode
, block_nr
, hdr
);
162 static inline int ext4_handle_dirty_xattr_block(handle_t
*handle
,
164 struct buffer_head
*bh
)
166 ext4_xattr_block_csum_set(inode
, bh
->b_blocknr
, BHDR(bh
));
167 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
170 static inline const struct xattr_handler
*
171 ext4_xattr_handler(int name_index
)
173 const struct xattr_handler
*handler
= NULL
;
175 if (name_index
> 0 && name_index
< ARRAY_SIZE(ext4_xattr_handler_map
))
176 handler
= ext4_xattr_handler_map
[name_index
];
181 * Inode operation listxattr()
183 * dentry->d_inode->i_mutex: don't care
186 ext4_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
188 return ext4_xattr_list(dentry
, buffer
, size
);
192 ext4_xattr_check_names(struct ext4_xattr_entry
*entry
, void *end
)
194 while (!IS_LAST_ENTRY(entry
)) {
195 struct ext4_xattr_entry
*next
= EXT4_XATTR_NEXT(entry
);
196 if ((void *)next
>= end
)
204 ext4_xattr_check_block(struct inode
*inode
, struct buffer_head
*bh
)
208 if (buffer_verified(bh
))
211 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
212 BHDR(bh
)->h_blocks
!= cpu_to_le32(1))
214 if (!ext4_xattr_block_csum_verify(inode
, bh
->b_blocknr
, BHDR(bh
)))
216 error
= ext4_xattr_check_names(BFIRST(bh
), bh
->b_data
+ bh
->b_size
);
218 set_buffer_verified(bh
);
223 ext4_xattr_check_entry(struct ext4_xattr_entry
*entry
, size_t size
)
225 size_t value_size
= le32_to_cpu(entry
->e_value_size
);
227 if (entry
->e_value_block
!= 0 || value_size
> size
||
228 le16_to_cpu(entry
->e_value_offs
) + value_size
> size
)
234 ext4_xattr_find_entry(struct ext4_xattr_entry
**pentry
, int name_index
,
235 const char *name
, size_t size
, int sorted
)
237 struct ext4_xattr_entry
*entry
;
243 name_len
= strlen(name
);
245 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
246 cmp
= name_index
- entry
->e_name_index
;
248 cmp
= name_len
- entry
->e_name_len
;
250 cmp
= memcmp(name
, entry
->e_name
, name_len
);
251 if (cmp
<= 0 && (sorted
|| cmp
== 0))
255 if (!cmp
&& ext4_xattr_check_entry(entry
, size
))
257 return cmp
? -ENODATA
: 0;
261 ext4_xattr_block_get(struct inode
*inode
, int name_index
, const char *name
,
262 void *buffer
, size_t buffer_size
)
264 struct buffer_head
*bh
= NULL
;
265 struct ext4_xattr_entry
*entry
;
269 ea_idebug(inode
, "name=%d.%s, buffer=%p, buffer_size=%ld",
270 name_index
, name
, buffer
, (long)buffer_size
);
273 if (!EXT4_I(inode
)->i_file_acl
)
275 ea_idebug(inode
, "reading block %llu",
276 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
277 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
280 ea_bdebug(bh
, "b_count=%d, refcount=%d",
281 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
282 if (ext4_xattr_check_block(inode
, bh
)) {
284 EXT4_ERROR_INODE(inode
, "bad block %llu",
285 EXT4_I(inode
)->i_file_acl
);
289 ext4_xattr_cache_insert(bh
);
291 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, bh
->b_size
, 1);
296 size
= le32_to_cpu(entry
->e_value_size
);
299 if (size
> buffer_size
)
301 memcpy(buffer
, bh
->b_data
+ le16_to_cpu(entry
->e_value_offs
),
312 ext4_xattr_ibody_get(struct inode
*inode
, int name_index
, const char *name
,
313 void *buffer
, size_t buffer_size
)
315 struct ext4_xattr_ibody_header
*header
;
316 struct ext4_xattr_entry
*entry
;
317 struct ext4_inode
*raw_inode
;
318 struct ext4_iloc iloc
;
323 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
325 error
= ext4_get_inode_loc(inode
, &iloc
);
328 raw_inode
= ext4_raw_inode(&iloc
);
329 header
= IHDR(inode
, raw_inode
);
330 entry
= IFIRST(header
);
331 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
332 error
= ext4_xattr_check_names(entry
, end
);
335 error
= ext4_xattr_find_entry(&entry
, name_index
, name
,
336 end
- (void *)entry
, 0);
339 size
= le32_to_cpu(entry
->e_value_size
);
342 if (size
> buffer_size
)
344 memcpy(buffer
, (void *)IFIRST(header
) +
345 le16_to_cpu(entry
->e_value_offs
), size
);
357 * Copy an extended attribute into the buffer
358 * provided, or compute the buffer size required.
359 * Buffer is NULL to compute the size of the buffer required.
361 * Returns a negative error number on failure, or the number of bytes
362 * used / required on success.
365 ext4_xattr_get(struct inode
*inode
, int name_index
, const char *name
,
366 void *buffer
, size_t buffer_size
)
370 down_read(&EXT4_I(inode
)->xattr_sem
);
371 error
= ext4_xattr_ibody_get(inode
, name_index
, name
, buffer
,
373 if (error
== -ENODATA
)
374 error
= ext4_xattr_block_get(inode
, name_index
, name
, buffer
,
376 up_read(&EXT4_I(inode
)->xattr_sem
);
381 ext4_xattr_list_entries(struct dentry
*dentry
, struct ext4_xattr_entry
*entry
,
382 char *buffer
, size_t buffer_size
)
384 size_t rest
= buffer_size
;
386 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
387 const struct xattr_handler
*handler
=
388 ext4_xattr_handler(entry
->e_name_index
);
391 size_t size
= handler
->list(dentry
, buffer
, rest
,
403 return buffer_size
- rest
;
407 ext4_xattr_block_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
409 struct inode
*inode
= dentry
->d_inode
;
410 struct buffer_head
*bh
= NULL
;
413 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
414 buffer
, (long)buffer_size
);
417 if (!EXT4_I(inode
)->i_file_acl
)
419 ea_idebug(inode
, "reading block %llu",
420 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
421 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
425 ea_bdebug(bh
, "b_count=%d, refcount=%d",
426 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
427 if (ext4_xattr_check_block(inode
, bh
)) {
428 EXT4_ERROR_INODE(inode
, "bad block %llu",
429 EXT4_I(inode
)->i_file_acl
);
433 ext4_xattr_cache_insert(bh
);
434 error
= ext4_xattr_list_entries(dentry
, BFIRST(bh
), buffer
, buffer_size
);
443 ext4_xattr_ibody_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
445 struct inode
*inode
= dentry
->d_inode
;
446 struct ext4_xattr_ibody_header
*header
;
447 struct ext4_inode
*raw_inode
;
448 struct ext4_iloc iloc
;
452 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
454 error
= ext4_get_inode_loc(inode
, &iloc
);
457 raw_inode
= ext4_raw_inode(&iloc
);
458 header
= IHDR(inode
, raw_inode
);
459 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
460 error
= ext4_xattr_check_names(IFIRST(header
), end
);
463 error
= ext4_xattr_list_entries(dentry
, IFIRST(header
),
464 buffer
, buffer_size
);
474 * Copy a list of attribute names into the buffer
475 * provided, or compute the buffer size required.
476 * Buffer is NULL to compute the size of the buffer required.
478 * Returns a negative error number on failure, or the number of bytes
479 * used / required on success.
482 ext4_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
486 down_read(&EXT4_I(dentry
->d_inode
)->xattr_sem
);
487 ret
= ret2
= ext4_xattr_ibody_list(dentry
, buffer
, buffer_size
);
494 ret
= ext4_xattr_block_list(dentry
, buffer
, buffer_size
);
499 up_read(&EXT4_I(dentry
->d_inode
)->xattr_sem
);
504 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
507 static void ext4_xattr_update_super_block(handle_t
*handle
,
508 struct super_block
*sb
)
510 if (EXT4_HAS_COMPAT_FEATURE(sb
, EXT4_FEATURE_COMPAT_EXT_ATTR
))
513 if (ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
) == 0) {
514 EXT4_SET_COMPAT_FEATURE(sb
, EXT4_FEATURE_COMPAT_EXT_ATTR
);
515 ext4_handle_dirty_super(handle
, sb
);
520 * Release the xattr block BH: If the reference count is > 1, decrement
521 * it; otherwise free the block.
524 ext4_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
525 struct buffer_head
*bh
)
527 struct mb_cache_entry
*ce
= NULL
;
530 ce
= mb_cache_entry_get(ext4_xattr_cache
, bh
->b_bdev
, bh
->b_blocknr
);
531 error
= ext4_journal_get_write_access(handle
, bh
);
536 if (BHDR(bh
)->h_refcount
== cpu_to_le32(1)) {
537 ea_bdebug(bh
, "refcount now=0; freeing");
539 mb_cache_entry_free(ce
);
541 ext4_free_blocks(handle
, inode
, bh
, 0, 1,
542 EXT4_FREE_BLOCKS_METADATA
|
543 EXT4_FREE_BLOCKS_FORGET
);
546 le32_add_cpu(&BHDR(bh
)->h_refcount
, -1);
548 mb_cache_entry_release(ce
);
550 error
= ext4_handle_dirty_xattr_block(handle
, inode
, bh
);
552 ext4_handle_sync(handle
);
553 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(inode
->i_sb
), 1));
554 ea_bdebug(bh
, "refcount now=%d; releasing",
555 le32_to_cpu(BHDR(bh
)->h_refcount
));
558 ext4_std_error(inode
->i_sb
, error
);
563 * Find the available free space for EAs. This also returns the total number of
564 * bytes used by EA entries.
566 static size_t ext4_xattr_free_space(struct ext4_xattr_entry
*last
,
567 size_t *min_offs
, void *base
, int *total
)
569 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
570 *total
+= EXT4_XATTR_LEN(last
->e_name_len
);
571 if (!last
->e_value_block
&& last
->e_value_size
) {
572 size_t offs
= le16_to_cpu(last
->e_value_offs
);
573 if (offs
< *min_offs
)
577 return (*min_offs
- ((void *)last
- base
) - sizeof(__u32
));
581 ext4_xattr_set_entry(struct ext4_xattr_info
*i
, struct ext4_xattr_search
*s
)
583 struct ext4_xattr_entry
*last
;
584 size_t free
, min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
586 /* Compute min_offs and last. */
588 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
589 if (!last
->e_value_block
&& last
->e_value_size
) {
590 size_t offs
= le16_to_cpu(last
->e_value_offs
);
595 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
597 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
598 size_t size
= le32_to_cpu(s
->here
->e_value_size
);
599 free
+= EXT4_XATTR_SIZE(size
);
601 free
+= EXT4_XATTR_LEN(name_len
);
604 if (free
< EXT4_XATTR_SIZE(i
->value_len
) ||
605 free
< EXT4_XATTR_LEN(name_len
) +
606 EXT4_XATTR_SIZE(i
->value_len
))
610 if (i
->value
&& s
->not_found
) {
611 /* Insert the new name. */
612 size_t size
= EXT4_XATTR_LEN(name_len
);
613 size_t rest
= (void *)last
- (void *)s
->here
+ sizeof(__u32
);
614 memmove((void *)s
->here
+ size
, s
->here
, rest
);
615 memset(s
->here
, 0, size
);
616 s
->here
->e_name_index
= i
->name_index
;
617 s
->here
->e_name_len
= name_len
;
618 memcpy(s
->here
->e_name
, i
->name
, name_len
);
620 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
621 void *first_val
= s
->base
+ min_offs
;
622 size_t offs
= le16_to_cpu(s
->here
->e_value_offs
);
623 void *val
= s
->base
+ offs
;
624 size_t size
= EXT4_XATTR_SIZE(
625 le32_to_cpu(s
->here
->e_value_size
));
627 if (i
->value
&& size
== EXT4_XATTR_SIZE(i
->value_len
)) {
628 /* The old and the new value have the same
629 size. Just replace. */
630 s
->here
->e_value_size
=
631 cpu_to_le32(i
->value_len
);
632 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
633 memset(val
, 0, size
);
635 /* Clear pad bytes first. */
636 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
638 memcpy(val
, i
->value
, i
->value_len
);
643 /* Remove the old value. */
644 memmove(first_val
+ size
, first_val
, val
- first_val
);
645 memset(first_val
, 0, size
);
646 s
->here
->e_value_size
= 0;
647 s
->here
->e_value_offs
= 0;
650 /* Adjust all value offsets. */
652 while (!IS_LAST_ENTRY(last
)) {
653 size_t o
= le16_to_cpu(last
->e_value_offs
);
654 if (!last
->e_value_block
&&
655 last
->e_value_size
&& o
< offs
)
657 cpu_to_le16(o
+ size
);
658 last
= EXT4_XATTR_NEXT(last
);
662 /* Remove the old name. */
663 size_t size
= EXT4_XATTR_LEN(name_len
);
664 last
= ENTRY((void *)last
- size
);
665 memmove(s
->here
, (void *)s
->here
+ size
,
666 (void *)last
- (void *)s
->here
+ sizeof(__u32
));
667 memset(last
, 0, size
);
672 /* Insert the new value. */
673 s
->here
->e_value_size
= cpu_to_le32(i
->value_len
);
675 size_t size
= EXT4_XATTR_SIZE(i
->value_len
);
676 void *val
= s
->base
+ min_offs
- size
;
677 s
->here
->e_value_offs
= cpu_to_le16(min_offs
- size
);
678 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
679 memset(val
, 0, size
);
681 /* Clear the pad bytes first. */
682 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
684 memcpy(val
, i
->value
, i
->value_len
);
691 struct ext4_xattr_block_find
{
692 struct ext4_xattr_search s
;
693 struct buffer_head
*bh
;
697 ext4_xattr_block_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
698 struct ext4_xattr_block_find
*bs
)
700 struct super_block
*sb
= inode
->i_sb
;
703 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
704 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
706 if (EXT4_I(inode
)->i_file_acl
) {
707 /* The inode already has an extended attribute block. */
708 bs
->bh
= sb_bread(sb
, EXT4_I(inode
)->i_file_acl
);
712 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
713 atomic_read(&(bs
->bh
->b_count
)),
714 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
715 if (ext4_xattr_check_block(inode
, bs
->bh
)) {
716 EXT4_ERROR_INODE(inode
, "bad block %llu",
717 EXT4_I(inode
)->i_file_acl
);
721 /* Find the named attribute. */
722 bs
->s
.base
= BHDR(bs
->bh
);
723 bs
->s
.first
= BFIRST(bs
->bh
);
724 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
725 bs
->s
.here
= bs
->s
.first
;
726 error
= ext4_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
727 i
->name
, bs
->bh
->b_size
, 1);
728 if (error
&& error
!= -ENODATA
)
730 bs
->s
.not_found
= error
;
739 ext4_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
740 struct ext4_xattr_info
*i
,
741 struct ext4_xattr_block_find
*bs
)
743 struct super_block
*sb
= inode
->i_sb
;
744 struct buffer_head
*new_bh
= NULL
;
745 struct ext4_xattr_search
*s
= &bs
->s
;
746 struct mb_cache_entry
*ce
= NULL
;
749 #define header(x) ((struct ext4_xattr_header *)(x))
751 if (i
->value
&& i
->value_len
> sb
->s_blocksize
)
754 ce
= mb_cache_entry_get(ext4_xattr_cache
, bs
->bh
->b_bdev
,
756 error
= ext4_journal_get_write_access(handle
, bs
->bh
);
761 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
763 mb_cache_entry_free(ce
);
766 ea_bdebug(bs
->bh
, "modifying in-place");
767 error
= ext4_xattr_set_entry(i
, s
);
769 if (!IS_LAST_ENTRY(s
->first
))
770 ext4_xattr_rehash(header(s
->base
),
772 ext4_xattr_cache_insert(bs
->bh
);
774 unlock_buffer(bs
->bh
);
778 error
= ext4_handle_dirty_xattr_block(handle
,
785 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
787 unlock_buffer(bs
->bh
);
789 mb_cache_entry_release(ce
);
792 ea_bdebug(bs
->bh
, "cloning");
793 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
797 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
798 s
->first
= ENTRY(header(s
->base
)+1);
799 header(s
->base
)->h_refcount
= cpu_to_le32(1);
800 s
->here
= ENTRY(s
->base
+ offset
);
801 s
->end
= s
->base
+ bs
->bh
->b_size
;
804 /* Allocate a buffer where we construct the new block. */
805 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
806 /* assert(header == s->base) */
810 header(s
->base
)->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
811 header(s
->base
)->h_blocks
= cpu_to_le32(1);
812 header(s
->base
)->h_refcount
= cpu_to_le32(1);
813 s
->first
= ENTRY(header(s
->base
)+1);
814 s
->here
= ENTRY(header(s
->base
)+1);
815 s
->end
= s
->base
+ sb
->s_blocksize
;
818 error
= ext4_xattr_set_entry(i
, s
);
823 if (!IS_LAST_ENTRY(s
->first
))
824 ext4_xattr_rehash(header(s
->base
), s
->here
);
827 if (!IS_LAST_ENTRY(s
->first
)) {
828 new_bh
= ext4_xattr_cache_find(inode
, header(s
->base
), &ce
);
830 /* We found an identical block in the cache. */
831 if (new_bh
== bs
->bh
)
832 ea_bdebug(new_bh
, "keeping");
834 /* The old block is released after updating
836 error
= dquot_alloc_block(inode
,
837 EXT4_C2B(EXT4_SB(sb
), 1));
840 error
= ext4_journal_get_write_access(handle
,
845 le32_add_cpu(&BHDR(new_bh
)->h_refcount
, 1);
846 ea_bdebug(new_bh
, "reusing; refcount now=%d",
847 le32_to_cpu(BHDR(new_bh
)->h_refcount
));
848 unlock_buffer(new_bh
);
849 error
= ext4_handle_dirty_xattr_block(handle
,
855 mb_cache_entry_release(ce
);
857 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
858 /* We were modifying this block in-place. */
859 ea_bdebug(bs
->bh
, "keeping this block");
863 /* We need to allocate a new block */
864 ext4_fsblk_t goal
, block
;
866 goal
= ext4_group_first_block_no(sb
,
867 EXT4_I(inode
)->i_block_group
);
869 /* non-extent files can't have physical blocks past 2^32 */
870 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
871 goal
= goal
& EXT4_MAX_BLOCK_FILE_PHYS
;
874 * take i_data_sem because we will test
875 * i_delalloc_reserved_flag in ext4_mb_new_blocks
877 down_read((&EXT4_I(inode
)->i_data_sem
));
878 block
= ext4_new_meta_blocks(handle
, inode
, goal
, 0,
880 up_read((&EXT4_I(inode
)->i_data_sem
));
884 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
885 BUG_ON(block
> EXT4_MAX_BLOCK_FILE_PHYS
);
887 ea_idebug(inode
, "creating block %llu",
888 (unsigned long long)block
);
890 new_bh
= sb_getblk(sb
, block
);
891 if (unlikely(!new_bh
)) {
894 ext4_free_blocks(handle
, inode
, NULL
, block
, 1,
895 EXT4_FREE_BLOCKS_METADATA
);
899 error
= ext4_journal_get_create_access(handle
, new_bh
);
901 unlock_buffer(new_bh
);
905 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
906 set_buffer_uptodate(new_bh
);
907 unlock_buffer(new_bh
);
908 ext4_xattr_cache_insert(new_bh
);
909 error
= ext4_handle_dirty_xattr_block(handle
,
916 /* Update the inode. */
917 EXT4_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
919 /* Drop the previous xattr block. */
920 if (bs
->bh
&& bs
->bh
!= new_bh
)
921 ext4_xattr_release_block(handle
, inode
, bs
->bh
);
926 mb_cache_entry_release(ce
);
928 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
934 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(sb
), 1));
938 EXT4_ERROR_INODE(inode
, "bad block %llu",
939 EXT4_I(inode
)->i_file_acl
);
945 int ext4_xattr_ibody_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
946 struct ext4_xattr_ibody_find
*is
)
948 struct ext4_xattr_ibody_header
*header
;
949 struct ext4_inode
*raw_inode
;
952 if (EXT4_I(inode
)->i_extra_isize
== 0)
954 raw_inode
= ext4_raw_inode(&is
->iloc
);
955 header
= IHDR(inode
, raw_inode
);
956 is
->s
.base
= is
->s
.first
= IFIRST(header
);
957 is
->s
.here
= is
->s
.first
;
958 is
->s
.end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
959 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
960 error
= ext4_xattr_check_names(IFIRST(header
), is
->s
.end
);
963 /* Find the named attribute. */
964 error
= ext4_xattr_find_entry(&is
->s
.here
, i
->name_index
,
966 (void *)is
->s
.base
, 0);
967 if (error
&& error
!= -ENODATA
)
969 is
->s
.not_found
= error
;
974 int ext4_xattr_ibody_inline_set(handle_t
*handle
, struct inode
*inode
,
975 struct ext4_xattr_info
*i
,
976 struct ext4_xattr_ibody_find
*is
)
978 struct ext4_xattr_ibody_header
*header
;
979 struct ext4_xattr_search
*s
= &is
->s
;
982 if (EXT4_I(inode
)->i_extra_isize
== 0)
984 error
= ext4_xattr_set_entry(i
, s
);
986 if (error
== -ENOSPC
&&
987 ext4_has_inline_data(inode
)) {
988 error
= ext4_try_to_evict_inline_data(handle
, inode
,
989 EXT4_XATTR_LEN(strlen(i
->name
) +
990 EXT4_XATTR_SIZE(i
->value_len
)));
993 error
= ext4_xattr_ibody_find(inode
, i
, is
);
996 error
= ext4_xattr_set_entry(i
, s
);
1001 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1002 if (!IS_LAST_ENTRY(s
->first
)) {
1003 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1004 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1006 header
->h_magic
= cpu_to_le32(0);
1007 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1012 static int ext4_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
1013 struct ext4_xattr_info
*i
,
1014 struct ext4_xattr_ibody_find
*is
)
1016 struct ext4_xattr_ibody_header
*header
;
1017 struct ext4_xattr_search
*s
= &is
->s
;
1020 if (EXT4_I(inode
)->i_extra_isize
== 0)
1022 error
= ext4_xattr_set_entry(i
, s
);
1025 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1026 if (!IS_LAST_ENTRY(s
->first
)) {
1027 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1028 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1030 header
->h_magic
= cpu_to_le32(0);
1031 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1037 * ext4_xattr_set_handle()
1039 * Create, replace or remove an extended attribute for this inode. Value
1040 * is NULL to remove an existing extended attribute, and non-NULL to
1041 * either replace an existing extended attribute, or create a new extended
1042 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1043 * specify that an extended attribute must exist and must not exist
1044 * previous to the call, respectively.
1046 * Returns 0, or a negative error number on failure.
1049 ext4_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
1050 const char *name
, const void *value
, size_t value_len
,
1053 struct ext4_xattr_info i
= {
1054 .name_index
= name_index
,
1057 .value_len
= value_len
,
1060 struct ext4_xattr_ibody_find is
= {
1061 .s
= { .not_found
= -ENODATA
, },
1063 struct ext4_xattr_block_find bs
= {
1064 .s
= { .not_found
= -ENODATA
, },
1066 unsigned long no_expand
;
1071 if (strlen(name
) > 255)
1073 down_write(&EXT4_I(inode
)->xattr_sem
);
1074 no_expand
= ext4_test_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1075 ext4_set_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1077 error
= ext4_reserve_inode_write(handle
, inode
, &is
.iloc
);
1081 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
)) {
1082 struct ext4_inode
*raw_inode
= ext4_raw_inode(&is
.iloc
);
1083 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
1084 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
1087 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
1091 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1094 if (is
.s
.not_found
&& bs
.s
.not_found
) {
1096 if (flags
& XATTR_REPLACE
)
1103 if (flags
& XATTR_CREATE
)
1107 if (!is
.s
.not_found
)
1108 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1109 else if (!bs
.s
.not_found
)
1110 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1112 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1113 if (!error
&& !bs
.s
.not_found
) {
1115 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1116 } else if (error
== -ENOSPC
) {
1117 if (EXT4_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
1118 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1122 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1125 if (!is
.s
.not_found
) {
1127 error
= ext4_xattr_ibody_set(handle
, inode
, &i
,
1133 ext4_xattr_update_super_block(handle
, inode
->i_sb
);
1134 inode
->i_ctime
= ext4_current_time(inode
);
1136 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1137 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
1139 * The bh is consumed by ext4_mark_iloc_dirty, even with
1144 ext4_handle_sync(handle
);
1151 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1152 up_write(&EXT4_I(inode
)->xattr_sem
);
1159 * Like ext4_xattr_set_handle, but start from an inode. This extended
1160 * attribute modification is a filesystem transaction by itself.
1162 * Returns 0, or a negative error number on failure.
1165 ext4_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
1166 const void *value
, size_t value_len
, int flags
)
1169 int error
, retries
= 0;
1170 int credits
= ext4_jbd2_credits_xattr(inode
);
1173 handle
= ext4_journal_start(inode
, EXT4_HT_XATTR
, credits
);
1174 if (IS_ERR(handle
)) {
1175 error
= PTR_ERR(handle
);
1179 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, name
,
1180 value
, value_len
, flags
);
1181 error2
= ext4_journal_stop(handle
);
1182 if (error
== -ENOSPC
&&
1183 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1193 * Shift the EA entries in the inode to create space for the increased
1196 static void ext4_xattr_shift_entries(struct ext4_xattr_entry
*entry
,
1197 int value_offs_shift
, void *to
,
1198 void *from
, size_t n
, int blocksize
)
1200 struct ext4_xattr_entry
*last
= entry
;
1203 /* Adjust the value offsets of the entries */
1204 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1205 if (!last
->e_value_block
&& last
->e_value_size
) {
1206 new_offs
= le16_to_cpu(last
->e_value_offs
) +
1208 BUG_ON(new_offs
+ le32_to_cpu(last
->e_value_size
)
1210 last
->e_value_offs
= cpu_to_le16(new_offs
);
1213 /* Shift the entries by n bytes */
1214 memmove(to
, from
, n
);
1218 * Expand an inode by new_extra_isize bytes when EAs are present.
1219 * Returns 0 on success or negative error number on failure.
1221 int ext4_expand_extra_isize_ea(struct inode
*inode
, int new_extra_isize
,
1222 struct ext4_inode
*raw_inode
, handle_t
*handle
)
1224 struct ext4_xattr_ibody_header
*header
;
1225 struct ext4_xattr_entry
*entry
, *last
, *first
;
1226 struct buffer_head
*bh
= NULL
;
1227 struct ext4_xattr_ibody_find
*is
= NULL
;
1228 struct ext4_xattr_block_find
*bs
= NULL
;
1229 char *buffer
= NULL
, *b_entry_name
= NULL
;
1230 size_t min_offs
, free
;
1231 int total_ino
, total_blk
;
1232 void *base
, *start
, *end
;
1233 int extra_isize
= 0, error
= 0, tried_min_extra_isize
= 0;
1234 int s_min_extra_isize
= le16_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_min_extra_isize
);
1236 down_write(&EXT4_I(inode
)->xattr_sem
);
1238 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
) {
1239 up_write(&EXT4_I(inode
)->xattr_sem
);
1243 header
= IHDR(inode
, raw_inode
);
1244 entry
= IFIRST(header
);
1247 * Check if enough free space is available in the inode to shift the
1248 * entries ahead by new_extra_isize.
1251 base
= start
= entry
;
1252 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1253 min_offs
= end
- base
;
1255 total_ino
= sizeof(struct ext4_xattr_ibody_header
);
1257 free
= ext4_xattr_free_space(last
, &min_offs
, base
, &total_ino
);
1258 if (free
>= new_extra_isize
) {
1259 entry
= IFIRST(header
);
1260 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
1261 - new_extra_isize
, (void *)raw_inode
+
1262 EXT4_GOOD_OLD_INODE_SIZE
+ new_extra_isize
,
1263 (void *)header
, total_ino
,
1264 inode
->i_sb
->s_blocksize
);
1265 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
1271 * Enough free space isn't available in the inode, check if
1272 * EA block can hold new_extra_isize bytes.
1274 if (EXT4_I(inode
)->i_file_acl
) {
1275 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1279 if (ext4_xattr_check_block(inode
, bh
)) {
1280 EXT4_ERROR_INODE(inode
, "bad block %llu",
1281 EXT4_I(inode
)->i_file_acl
);
1287 end
= bh
->b_data
+ bh
->b_size
;
1288 min_offs
= end
- base
;
1289 free
= ext4_xattr_free_space(first
, &min_offs
, base
,
1291 if (free
< new_extra_isize
) {
1292 if (!tried_min_extra_isize
&& s_min_extra_isize
) {
1293 tried_min_extra_isize
++;
1294 new_extra_isize
= s_min_extra_isize
;
1302 free
= inode
->i_sb
->s_blocksize
;
1305 while (new_extra_isize
> 0) {
1306 size_t offs
, size
, entry_size
;
1307 struct ext4_xattr_entry
*small_entry
= NULL
;
1308 struct ext4_xattr_info i
= {
1312 unsigned int total_size
; /* EA entry size + value size */
1313 unsigned int shift_bytes
; /* No. of bytes to shift EAs by? */
1314 unsigned int min_total_size
= ~0U;
1316 is
= kzalloc(sizeof(struct ext4_xattr_ibody_find
), GFP_NOFS
);
1317 bs
= kzalloc(sizeof(struct ext4_xattr_block_find
), GFP_NOFS
);
1323 is
->s
.not_found
= -ENODATA
;
1324 bs
->s
.not_found
= -ENODATA
;
1328 last
= IFIRST(header
);
1329 /* Find the entry best suited to be pushed into EA block */
1331 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1333 EXT4_XATTR_SIZE(le32_to_cpu(last
->e_value_size
)) +
1334 EXT4_XATTR_LEN(last
->e_name_len
);
1335 if (total_size
<= free
&& total_size
< min_total_size
) {
1336 if (total_size
< new_extra_isize
) {
1340 min_total_size
= total_size
;
1345 if (entry
== NULL
) {
1347 entry
= small_entry
;
1349 if (!tried_min_extra_isize
&&
1350 s_min_extra_isize
) {
1351 tried_min_extra_isize
++;
1352 new_extra_isize
= s_min_extra_isize
;
1353 kfree(is
); is
= NULL
;
1354 kfree(bs
); bs
= NULL
;
1362 offs
= le16_to_cpu(entry
->e_value_offs
);
1363 size
= le32_to_cpu(entry
->e_value_size
);
1364 entry_size
= EXT4_XATTR_LEN(entry
->e_name_len
);
1365 i
.name_index
= entry
->e_name_index
,
1366 buffer
= kmalloc(EXT4_XATTR_SIZE(size
), GFP_NOFS
);
1367 b_entry_name
= kmalloc(entry
->e_name_len
+ 1, GFP_NOFS
);
1368 if (!buffer
|| !b_entry_name
) {
1372 /* Save the entry name and the entry value */
1373 memcpy(buffer
, (void *)IFIRST(header
) + offs
,
1374 EXT4_XATTR_SIZE(size
));
1375 memcpy(b_entry_name
, entry
->e_name
, entry
->e_name_len
);
1376 b_entry_name
[entry
->e_name_len
] = '\0';
1377 i
.name
= b_entry_name
;
1379 error
= ext4_get_inode_loc(inode
, &is
->iloc
);
1383 error
= ext4_xattr_ibody_find(inode
, &i
, is
);
1387 /* Remove the chosen entry from the inode */
1388 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, is
);
1392 entry
= IFIRST(header
);
1393 if (entry_size
+ EXT4_XATTR_SIZE(size
) >= new_extra_isize
)
1394 shift_bytes
= new_extra_isize
;
1396 shift_bytes
= entry_size
+ size
;
1397 /* Adjust the offsets and shift the remaining entries ahead */
1398 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
-
1399 shift_bytes
, (void *)raw_inode
+
1400 EXT4_GOOD_OLD_INODE_SIZE
+ extra_isize
+ shift_bytes
,
1401 (void *)header
, total_ino
- entry_size
,
1402 inode
->i_sb
->s_blocksize
);
1404 extra_isize
+= shift_bytes
;
1405 new_extra_isize
-= shift_bytes
;
1406 EXT4_I(inode
)->i_extra_isize
= extra_isize
;
1408 i
.name
= b_entry_name
;
1411 error
= ext4_xattr_block_find(inode
, &i
, bs
);
1415 /* Add entry which was removed from the inode into the block */
1416 error
= ext4_xattr_block_set(handle
, inode
, &i
, bs
);
1419 kfree(b_entry_name
);
1421 b_entry_name
= NULL
;
1423 brelse(is
->iloc
.bh
);
1428 up_write(&EXT4_I(inode
)->xattr_sem
);
1432 kfree(b_entry_name
);
1435 brelse(is
->iloc
.bh
);
1439 up_write(&EXT4_I(inode
)->xattr_sem
);
1446 * ext4_xattr_delete_inode()
1448 * Free extended attribute resources associated with this inode. This
1449 * is called immediately before an inode is freed. We have exclusive
1450 * access to the inode.
1453 ext4_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
)
1455 struct buffer_head
*bh
= NULL
;
1457 if (!EXT4_I(inode
)->i_file_acl
)
1459 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1461 EXT4_ERROR_INODE(inode
, "block %llu read error",
1462 EXT4_I(inode
)->i_file_acl
);
1465 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
1466 BHDR(bh
)->h_blocks
!= cpu_to_le32(1)) {
1467 EXT4_ERROR_INODE(inode
, "bad block %llu",
1468 EXT4_I(inode
)->i_file_acl
);
1471 ext4_xattr_release_block(handle
, inode
, bh
);
1472 EXT4_I(inode
)->i_file_acl
= 0;
1479 * ext4_xattr_put_super()
1481 * This is called when a file system is unmounted.
1484 ext4_xattr_put_super(struct super_block
*sb
)
1486 mb_cache_shrink(sb
->s_bdev
);
1490 * ext4_xattr_cache_insert()
1492 * Create a new entry in the extended attribute cache, and insert
1493 * it unless such an entry is already in the cache.
1495 * Returns 0, or a negative error number on failure.
1498 ext4_xattr_cache_insert(struct buffer_head
*bh
)
1500 __u32 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
1501 struct mb_cache_entry
*ce
;
1504 ce
= mb_cache_entry_alloc(ext4_xattr_cache
, GFP_NOFS
);
1506 ea_bdebug(bh
, "out of memory");
1509 error
= mb_cache_entry_insert(ce
, bh
->b_bdev
, bh
->b_blocknr
, hash
);
1511 mb_cache_entry_free(ce
);
1512 if (error
== -EBUSY
) {
1513 ea_bdebug(bh
, "already in cache");
1517 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
1518 mb_cache_entry_release(ce
);
1525 * Compare two extended attribute blocks for equality.
1527 * Returns 0 if the blocks are equal, 1 if they differ, and
1528 * a negative error number on errors.
1531 ext4_xattr_cmp(struct ext4_xattr_header
*header1
,
1532 struct ext4_xattr_header
*header2
)
1534 struct ext4_xattr_entry
*entry1
, *entry2
;
1536 entry1
= ENTRY(header1
+1);
1537 entry2
= ENTRY(header2
+1);
1538 while (!IS_LAST_ENTRY(entry1
)) {
1539 if (IS_LAST_ENTRY(entry2
))
1541 if (entry1
->e_hash
!= entry2
->e_hash
||
1542 entry1
->e_name_index
!= entry2
->e_name_index
||
1543 entry1
->e_name_len
!= entry2
->e_name_len
||
1544 entry1
->e_value_size
!= entry2
->e_value_size
||
1545 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
1547 if (entry1
->e_value_block
!= 0 || entry2
->e_value_block
!= 0)
1549 if (memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
1550 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
1551 le32_to_cpu(entry1
->e_value_size
)))
1554 entry1
= EXT4_XATTR_NEXT(entry1
);
1555 entry2
= EXT4_XATTR_NEXT(entry2
);
1557 if (!IS_LAST_ENTRY(entry2
))
1563 * ext4_xattr_cache_find()
1565 * Find an identical extended attribute block.
1567 * Returns a pointer to the block found, or NULL if such a block was
1568 * not found or an error occurred.
1570 static struct buffer_head
*
1571 ext4_xattr_cache_find(struct inode
*inode
, struct ext4_xattr_header
*header
,
1572 struct mb_cache_entry
**pce
)
1574 __u32 hash
= le32_to_cpu(header
->h_hash
);
1575 struct mb_cache_entry
*ce
;
1577 if (!header
->h_hash
)
1578 return NULL
; /* never share */
1579 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
1581 ce
= mb_cache_entry_find_first(ext4_xattr_cache
, inode
->i_sb
->s_bdev
,
1584 struct buffer_head
*bh
;
1587 if (PTR_ERR(ce
) == -EAGAIN
)
1591 bh
= sb_bread(inode
->i_sb
, ce
->e_block
);
1593 EXT4_ERROR_INODE(inode
, "block %lu read error",
1594 (unsigned long) ce
->e_block
);
1595 } else if (le32_to_cpu(BHDR(bh
)->h_refcount
) >=
1596 EXT4_XATTR_REFCOUNT_MAX
) {
1597 ea_idebug(inode
, "block %lu refcount %d>=%d",
1598 (unsigned long) ce
->e_block
,
1599 le32_to_cpu(BHDR(bh
)->h_refcount
),
1600 EXT4_XATTR_REFCOUNT_MAX
);
1601 } else if (ext4_xattr_cmp(header
, BHDR(bh
)) == 0) {
1606 ce
= mb_cache_entry_find_next(ce
, inode
->i_sb
->s_bdev
, hash
);
1611 #define NAME_HASH_SHIFT 5
1612 #define VALUE_HASH_SHIFT 16
1615 * ext4_xattr_hash_entry()
1617 * Compute the hash of an extended attribute.
1619 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header
*header
,
1620 struct ext4_xattr_entry
*entry
)
1623 char *name
= entry
->e_name
;
1626 for (n
= 0; n
< entry
->e_name_len
; n
++) {
1627 hash
= (hash
<< NAME_HASH_SHIFT
) ^
1628 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
1632 if (entry
->e_value_block
== 0 && entry
->e_value_size
!= 0) {
1633 __le32
*value
= (__le32
*)((char *)header
+
1634 le16_to_cpu(entry
->e_value_offs
));
1635 for (n
= (le32_to_cpu(entry
->e_value_size
) +
1636 EXT4_XATTR_ROUND
) >> EXT4_XATTR_PAD_BITS
; n
; n
--) {
1637 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
1638 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
1639 le32_to_cpu(*value
++);
1642 entry
->e_hash
= cpu_to_le32(hash
);
1645 #undef NAME_HASH_SHIFT
1646 #undef VALUE_HASH_SHIFT
1648 #define BLOCK_HASH_SHIFT 16
1651 * ext4_xattr_rehash()
1653 * Re-compute the extended attribute hash value after an entry has changed.
1655 static void ext4_xattr_rehash(struct ext4_xattr_header
*header
,
1656 struct ext4_xattr_entry
*entry
)
1658 struct ext4_xattr_entry
*here
;
1661 ext4_xattr_hash_entry(header
, entry
);
1662 here
= ENTRY(header
+1);
1663 while (!IS_LAST_ENTRY(here
)) {
1664 if (!here
->e_hash
) {
1665 /* Block is not shared if an entry's hash value == 0 */
1669 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
1670 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
1671 le32_to_cpu(here
->e_hash
);
1672 here
= EXT4_XATTR_NEXT(here
);
1674 header
->h_hash
= cpu_to_le32(hash
);
1677 #undef BLOCK_HASH_SHIFT
1680 ext4_init_xattr(void)
1682 ext4_xattr_cache
= mb_cache_create("ext4_xattr", 6);
1683 if (!ext4_xattr_cache
)
1689 ext4_exit_xattr(void)
1691 if (ext4_xattr_cache
)
1692 mb_cache_destroy(ext4_xattr_cache
);
1693 ext4_xattr_cache
= NULL
;