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 "ext4_jbd2.h"
63 #ifdef EXT4_XATTR_DEBUG
64 # define ea_idebug(inode, f...) do { \
65 printk(KERN_DEBUG "inode %s:%lu: ", \
66 inode->i_sb->s_id, inode->i_ino); \
70 # define ea_bdebug(bh, f...) do { \
71 char b[BDEVNAME_SIZE]; \
72 printk(KERN_DEBUG "block %s:%lu: ", \
73 bdevname(bh->b_bdev, b), \
74 (unsigned long) bh->b_blocknr); \
79 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
80 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
83 static void ext4_xattr_cache_insert(struct mb_cache
*, struct buffer_head
*);
84 static struct buffer_head
*ext4_xattr_cache_find(struct inode
*,
85 struct ext4_xattr_header
*,
86 struct mb_cache_entry
**);
87 static void ext4_xattr_rehash(struct ext4_xattr_header
*,
88 struct ext4_xattr_entry
*);
89 static int ext4_xattr_list(struct dentry
*dentry
, char *buffer
,
92 static const struct xattr_handler
*ext4_xattr_handler_map
[] = {
93 [EXT4_XATTR_INDEX_USER
] = &ext4_xattr_user_handler
,
94 #ifdef CONFIG_EXT4_FS_POSIX_ACL
95 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS
] = &posix_acl_access_xattr_handler
,
96 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT
] = &posix_acl_default_xattr_handler
,
98 [EXT4_XATTR_INDEX_TRUSTED
] = &ext4_xattr_trusted_handler
,
99 #ifdef CONFIG_EXT4_FS_SECURITY
100 [EXT4_XATTR_INDEX_SECURITY
] = &ext4_xattr_security_handler
,
104 const struct xattr_handler
*ext4_xattr_handlers
[] = {
105 &ext4_xattr_user_handler
,
106 &ext4_xattr_trusted_handler
,
107 #ifdef CONFIG_EXT4_FS_POSIX_ACL
108 &posix_acl_access_xattr_handler
,
109 &posix_acl_default_xattr_handler
,
111 #ifdef CONFIG_EXT4_FS_SECURITY
112 &ext4_xattr_security_handler
,
117 #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
118 inode->i_sb->s_fs_info)->s_mb_cache)
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_metadata_csum(inode
->i_sb
) &&
145 (hdr
->h_checksum
!= ext4_xattr_block_csum(inode
, block_nr
, hdr
)))
150 static void ext4_xattr_block_csum_set(struct inode
*inode
,
152 struct ext4_xattr_header
*hdr
)
154 if (!ext4_has_metadata_csum(inode
->i_sb
))
157 hdr
->h_checksum
= ext4_xattr_block_csum(inode
, block_nr
, hdr
);
160 static inline int ext4_handle_dirty_xattr_block(handle_t
*handle
,
162 struct buffer_head
*bh
)
164 ext4_xattr_block_csum_set(inode
, bh
->b_blocknr
, BHDR(bh
));
165 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
168 static inline const struct xattr_handler
*
169 ext4_xattr_handler(int name_index
)
171 const struct xattr_handler
*handler
= NULL
;
173 if (name_index
> 0 && name_index
< ARRAY_SIZE(ext4_xattr_handler_map
))
174 handler
= ext4_xattr_handler_map
[name_index
];
179 * Inode operation listxattr()
181 * d_inode(dentry)->i_mutex: don't care
184 ext4_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
186 return ext4_xattr_list(dentry
, buffer
, size
);
190 ext4_xattr_check_names(struct ext4_xattr_entry
*entry
, void *end
,
193 struct ext4_xattr_entry
*e
= entry
;
195 while (!IS_LAST_ENTRY(e
)) {
196 struct ext4_xattr_entry
*next
= EXT4_XATTR_NEXT(e
);
197 if ((void *)next
>= end
)
198 return -EFSCORRUPTED
;
202 while (!IS_LAST_ENTRY(entry
)) {
203 if (entry
->e_value_size
!= 0 &&
204 (value_start
+ le16_to_cpu(entry
->e_value_offs
) <
205 (void *)e
+ sizeof(__u32
) ||
206 value_start
+ le16_to_cpu(entry
->e_value_offs
) +
207 le32_to_cpu(entry
->e_value_size
) > end
))
208 return -EFSCORRUPTED
;
209 entry
= EXT4_XATTR_NEXT(entry
);
216 ext4_xattr_check_block(struct inode
*inode
, struct buffer_head
*bh
)
220 if (buffer_verified(bh
))
223 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
224 BHDR(bh
)->h_blocks
!= cpu_to_le32(1))
225 return -EFSCORRUPTED
;
226 if (!ext4_xattr_block_csum_verify(inode
, bh
->b_blocknr
, BHDR(bh
)))
228 error
= ext4_xattr_check_names(BFIRST(bh
), bh
->b_data
+ bh
->b_size
,
231 set_buffer_verified(bh
);
236 ext4_xattr_check_entry(struct ext4_xattr_entry
*entry
, size_t size
)
238 size_t value_size
= le32_to_cpu(entry
->e_value_size
);
240 if (entry
->e_value_block
!= 0 || value_size
> size
||
241 le16_to_cpu(entry
->e_value_offs
) + value_size
> size
)
242 return -EFSCORRUPTED
;
247 ext4_xattr_find_entry(struct ext4_xattr_entry
**pentry
, int name_index
,
248 const char *name
, size_t size
, int sorted
)
250 struct ext4_xattr_entry
*entry
;
256 name_len
= strlen(name
);
258 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
259 cmp
= name_index
- entry
->e_name_index
;
261 cmp
= name_len
- entry
->e_name_len
;
263 cmp
= memcmp(name
, entry
->e_name
, name_len
);
264 if (cmp
<= 0 && (sorted
|| cmp
== 0))
268 if (!cmp
&& ext4_xattr_check_entry(entry
, size
))
269 return -EFSCORRUPTED
;
270 return cmp
? -ENODATA
: 0;
274 ext4_xattr_block_get(struct inode
*inode
, int name_index
, const char *name
,
275 void *buffer
, size_t buffer_size
)
277 struct buffer_head
*bh
= NULL
;
278 struct ext4_xattr_entry
*entry
;
281 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
283 ea_idebug(inode
, "name=%d.%s, buffer=%p, buffer_size=%ld",
284 name_index
, name
, buffer
, (long)buffer_size
);
287 if (!EXT4_I(inode
)->i_file_acl
)
289 ea_idebug(inode
, "reading block %llu",
290 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
291 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
294 ea_bdebug(bh
, "b_count=%d, refcount=%d",
295 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
296 if (ext4_xattr_check_block(inode
, bh
)) {
298 EXT4_ERROR_INODE(inode
, "bad block %llu",
299 EXT4_I(inode
)->i_file_acl
);
300 error
= -EFSCORRUPTED
;
303 ext4_xattr_cache_insert(ext4_mb_cache
, bh
);
305 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, bh
->b_size
, 1);
306 if (error
== -EFSCORRUPTED
)
310 size
= le32_to_cpu(entry
->e_value_size
);
313 if (size
> buffer_size
)
315 memcpy(buffer
, bh
->b_data
+ le16_to_cpu(entry
->e_value_offs
),
326 ext4_xattr_ibody_get(struct inode
*inode
, int name_index
, const char *name
,
327 void *buffer
, size_t buffer_size
)
329 struct ext4_xattr_ibody_header
*header
;
330 struct ext4_xattr_entry
*entry
;
331 struct ext4_inode
*raw_inode
;
332 struct ext4_iloc iloc
;
337 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
339 error
= ext4_get_inode_loc(inode
, &iloc
);
342 raw_inode
= ext4_raw_inode(&iloc
);
343 header
= IHDR(inode
, raw_inode
);
344 entry
= IFIRST(header
);
345 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
346 error
= ext4_xattr_check_names(entry
, end
, entry
);
349 error
= ext4_xattr_find_entry(&entry
, name_index
, name
,
350 end
- (void *)entry
, 0);
353 size
= le32_to_cpu(entry
->e_value_size
);
356 if (size
> buffer_size
)
358 memcpy(buffer
, (void *)IFIRST(header
) +
359 le16_to_cpu(entry
->e_value_offs
), size
);
371 * Copy an extended attribute into the buffer
372 * provided, or compute the buffer size required.
373 * Buffer is NULL to compute the size of the buffer required.
375 * Returns a negative error number on failure, or the number of bytes
376 * used / required on success.
379 ext4_xattr_get(struct inode
*inode
, int name_index
, const char *name
,
380 void *buffer
, size_t buffer_size
)
384 if (strlen(name
) > 255)
387 down_read(&EXT4_I(inode
)->xattr_sem
);
388 error
= ext4_xattr_ibody_get(inode
, name_index
, name
, buffer
,
390 if (error
== -ENODATA
)
391 error
= ext4_xattr_block_get(inode
, name_index
, name
, buffer
,
393 up_read(&EXT4_I(inode
)->xattr_sem
);
398 ext4_xattr_list_entries(struct dentry
*dentry
, struct ext4_xattr_entry
*entry
,
399 char *buffer
, size_t buffer_size
)
401 size_t rest
= buffer_size
;
403 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
404 const struct xattr_handler
*handler
=
405 ext4_xattr_handler(entry
->e_name_index
);
407 if (handler
&& (!handler
->list
|| handler
->list(dentry
))) {
408 const char *prefix
= handler
->prefix
?: handler
->name
;
409 size_t prefix_len
= strlen(prefix
);
410 size_t size
= prefix_len
+ entry
->e_name_len
+ 1;
415 memcpy(buffer
, prefix
, prefix_len
);
416 buffer
+= prefix_len
;
417 memcpy(buffer
, entry
->e_name
, entry
->e_name_len
);
418 buffer
+= entry
->e_name_len
;
424 return buffer_size
- rest
; /* total size */
428 ext4_xattr_block_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
430 struct inode
*inode
= d_inode(dentry
);
431 struct buffer_head
*bh
= NULL
;
433 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
435 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
436 buffer
, (long)buffer_size
);
439 if (!EXT4_I(inode
)->i_file_acl
)
441 ea_idebug(inode
, "reading block %llu",
442 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
443 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
447 ea_bdebug(bh
, "b_count=%d, refcount=%d",
448 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
449 if (ext4_xattr_check_block(inode
, bh
)) {
450 EXT4_ERROR_INODE(inode
, "bad block %llu",
451 EXT4_I(inode
)->i_file_acl
);
452 error
= -EFSCORRUPTED
;
455 ext4_xattr_cache_insert(ext4_mb_cache
, bh
);
456 error
= ext4_xattr_list_entries(dentry
, BFIRST(bh
), buffer
, buffer_size
);
465 ext4_xattr_ibody_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
467 struct inode
*inode
= d_inode(dentry
);
468 struct ext4_xattr_ibody_header
*header
;
469 struct ext4_inode
*raw_inode
;
470 struct ext4_iloc iloc
;
474 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
476 error
= ext4_get_inode_loc(inode
, &iloc
);
479 raw_inode
= ext4_raw_inode(&iloc
);
480 header
= IHDR(inode
, raw_inode
);
481 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
482 error
= ext4_xattr_check_names(IFIRST(header
), end
, IFIRST(header
));
485 error
= ext4_xattr_list_entries(dentry
, IFIRST(header
),
486 buffer
, buffer_size
);
496 * Copy a list of attribute names into the buffer
497 * provided, or compute the buffer size required.
498 * Buffer is NULL to compute the size of the buffer required.
500 * Returns a negative error number on failure, or the number of bytes
501 * used / required on success.
504 ext4_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
508 down_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
509 ret
= ret2
= ext4_xattr_ibody_list(dentry
, buffer
, buffer_size
);
516 ret
= ext4_xattr_block_list(dentry
, buffer
, buffer_size
);
521 up_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
526 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
529 static void ext4_xattr_update_super_block(handle_t
*handle
,
530 struct super_block
*sb
)
532 if (ext4_has_feature_xattr(sb
))
535 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
536 if (ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
) == 0) {
537 ext4_set_feature_xattr(sb
);
538 ext4_handle_dirty_super(handle
, sb
);
543 * Release the xattr block BH: If the reference count is > 1, decrement it;
544 * otherwise free the block.
547 ext4_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
548 struct buffer_head
*bh
)
550 struct mb_cache_entry
*ce
= NULL
;
552 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
554 ce
= mb_cache_entry_get(ext4_mb_cache
, bh
->b_bdev
, bh
->b_blocknr
);
555 BUFFER_TRACE(bh
, "get_write_access");
556 error
= ext4_journal_get_write_access(handle
, bh
);
561 if (BHDR(bh
)->h_refcount
== cpu_to_le32(1)) {
562 ea_bdebug(bh
, "refcount now=0; freeing");
564 mb_cache_entry_free(ce
);
567 ext4_free_blocks(handle
, inode
, bh
, 0, 1,
568 EXT4_FREE_BLOCKS_METADATA
|
569 EXT4_FREE_BLOCKS_FORGET
);
571 le32_add_cpu(&BHDR(bh
)->h_refcount
, -1);
573 mb_cache_entry_release(ce
);
575 * Beware of this ugliness: Releasing of xattr block references
576 * from different inodes can race and so we have to protect
577 * from a race where someone else frees the block (and releases
578 * its journal_head) before we are done dirtying the buffer. In
579 * nojournal mode this race is harmless and we actually cannot
580 * call ext4_handle_dirty_xattr_block() with locked buffer as
581 * that function can call sync_dirty_buffer() so for that case
582 * we handle the dirtying after unlocking the buffer.
584 if (ext4_handle_valid(handle
))
585 error
= ext4_handle_dirty_xattr_block(handle
, inode
,
588 if (!ext4_handle_valid(handle
))
589 error
= ext4_handle_dirty_xattr_block(handle
, inode
,
592 ext4_handle_sync(handle
);
593 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(inode
->i_sb
), 1));
594 ea_bdebug(bh
, "refcount now=%d; releasing",
595 le32_to_cpu(BHDR(bh
)->h_refcount
));
598 ext4_std_error(inode
->i_sb
, error
);
603 * Find the available free space for EAs. This also returns the total number of
604 * bytes used by EA entries.
606 static size_t ext4_xattr_free_space(struct ext4_xattr_entry
*last
,
607 size_t *min_offs
, void *base
, int *total
)
609 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
610 if (!last
->e_value_block
&& last
->e_value_size
) {
611 size_t offs
= le16_to_cpu(last
->e_value_offs
);
612 if (offs
< *min_offs
)
616 *total
+= EXT4_XATTR_LEN(last
->e_name_len
);
618 return (*min_offs
- ((void *)last
- base
) - sizeof(__u32
));
622 ext4_xattr_set_entry(struct ext4_xattr_info
*i
, struct ext4_xattr_search
*s
)
624 struct ext4_xattr_entry
*last
;
625 size_t free
, min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
627 /* Compute min_offs and last. */
629 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
630 if (!last
->e_value_block
&& last
->e_value_size
) {
631 size_t offs
= le16_to_cpu(last
->e_value_offs
);
636 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
638 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
639 size_t size
= le32_to_cpu(s
->here
->e_value_size
);
640 free
+= EXT4_XATTR_SIZE(size
);
642 free
+= EXT4_XATTR_LEN(name_len
);
645 if (free
< EXT4_XATTR_LEN(name_len
) +
646 EXT4_XATTR_SIZE(i
->value_len
))
650 if (i
->value
&& s
->not_found
) {
651 /* Insert the new name. */
652 size_t size
= EXT4_XATTR_LEN(name_len
);
653 size_t rest
= (void *)last
- (void *)s
->here
+ sizeof(__u32
);
654 memmove((void *)s
->here
+ size
, s
->here
, rest
);
655 memset(s
->here
, 0, size
);
656 s
->here
->e_name_index
= i
->name_index
;
657 s
->here
->e_name_len
= name_len
;
658 memcpy(s
->here
->e_name
, i
->name
, name_len
);
660 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
661 void *first_val
= s
->base
+ min_offs
;
662 size_t offs
= le16_to_cpu(s
->here
->e_value_offs
);
663 void *val
= s
->base
+ offs
;
664 size_t size
= EXT4_XATTR_SIZE(
665 le32_to_cpu(s
->here
->e_value_size
));
667 if (i
->value
&& size
== EXT4_XATTR_SIZE(i
->value_len
)) {
668 /* The old and the new value have the same
669 size. Just replace. */
670 s
->here
->e_value_size
=
671 cpu_to_le32(i
->value_len
);
672 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
673 memset(val
, 0, size
);
675 /* Clear pad bytes first. */
676 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
678 memcpy(val
, i
->value
, i
->value_len
);
683 /* Remove the old value. */
684 memmove(first_val
+ size
, first_val
, val
- first_val
);
685 memset(first_val
, 0, size
);
686 s
->here
->e_value_size
= 0;
687 s
->here
->e_value_offs
= 0;
690 /* Adjust all value offsets. */
692 while (!IS_LAST_ENTRY(last
)) {
693 size_t o
= le16_to_cpu(last
->e_value_offs
);
694 if (!last
->e_value_block
&&
695 last
->e_value_size
&& o
< offs
)
697 cpu_to_le16(o
+ size
);
698 last
= EXT4_XATTR_NEXT(last
);
702 /* Remove the old name. */
703 size_t size
= EXT4_XATTR_LEN(name_len
);
704 last
= ENTRY((void *)last
- size
);
705 memmove(s
->here
, (void *)s
->here
+ size
,
706 (void *)last
- (void *)s
->here
+ sizeof(__u32
));
707 memset(last
, 0, size
);
712 /* Insert the new value. */
713 s
->here
->e_value_size
= cpu_to_le32(i
->value_len
);
715 size_t size
= EXT4_XATTR_SIZE(i
->value_len
);
716 void *val
= s
->base
+ min_offs
- size
;
717 s
->here
->e_value_offs
= cpu_to_le16(min_offs
- size
);
718 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
719 memset(val
, 0, size
);
721 /* Clear the pad bytes first. */
722 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
724 memcpy(val
, i
->value
, i
->value_len
);
731 struct ext4_xattr_block_find
{
732 struct ext4_xattr_search s
;
733 struct buffer_head
*bh
;
737 ext4_xattr_block_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
738 struct ext4_xattr_block_find
*bs
)
740 struct super_block
*sb
= inode
->i_sb
;
743 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
744 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
746 if (EXT4_I(inode
)->i_file_acl
) {
747 /* The inode already has an extended attribute block. */
748 bs
->bh
= sb_bread(sb
, EXT4_I(inode
)->i_file_acl
);
752 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
753 atomic_read(&(bs
->bh
->b_count
)),
754 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
755 if (ext4_xattr_check_block(inode
, bs
->bh
)) {
756 EXT4_ERROR_INODE(inode
, "bad block %llu",
757 EXT4_I(inode
)->i_file_acl
);
758 error
= -EFSCORRUPTED
;
761 /* Find the named attribute. */
762 bs
->s
.base
= BHDR(bs
->bh
);
763 bs
->s
.first
= BFIRST(bs
->bh
);
764 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
765 bs
->s
.here
= bs
->s
.first
;
766 error
= ext4_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
767 i
->name
, bs
->bh
->b_size
, 1);
768 if (error
&& error
!= -ENODATA
)
770 bs
->s
.not_found
= error
;
779 ext4_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
780 struct ext4_xattr_info
*i
,
781 struct ext4_xattr_block_find
*bs
)
783 struct super_block
*sb
= inode
->i_sb
;
784 struct buffer_head
*new_bh
= NULL
;
785 struct ext4_xattr_search
*s
= &bs
->s
;
786 struct mb_cache_entry
*ce
= NULL
;
788 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
790 #define header(x) ((struct ext4_xattr_header *)(x))
792 if (i
->value
&& i
->value_len
> sb
->s_blocksize
)
795 ce
= mb_cache_entry_get(ext4_mb_cache
, bs
->bh
->b_bdev
,
797 BUFFER_TRACE(bs
->bh
, "get_write_access");
798 error
= ext4_journal_get_write_access(handle
, bs
->bh
);
803 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
805 mb_cache_entry_free(ce
);
808 ea_bdebug(bs
->bh
, "modifying in-place");
809 error
= ext4_xattr_set_entry(i
, s
);
811 if (!IS_LAST_ENTRY(s
->first
))
812 ext4_xattr_rehash(header(s
->base
),
814 ext4_xattr_cache_insert(ext4_mb_cache
,
817 unlock_buffer(bs
->bh
);
818 if (error
== -EFSCORRUPTED
)
821 error
= ext4_handle_dirty_xattr_block(handle
,
828 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
830 unlock_buffer(bs
->bh
);
832 mb_cache_entry_release(ce
);
835 ea_bdebug(bs
->bh
, "cloning");
836 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
840 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
841 s
->first
= ENTRY(header(s
->base
)+1);
842 header(s
->base
)->h_refcount
= cpu_to_le32(1);
843 s
->here
= ENTRY(s
->base
+ offset
);
844 s
->end
= s
->base
+ bs
->bh
->b_size
;
847 /* Allocate a buffer where we construct the new block. */
848 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
849 /* assert(header == s->base) */
853 header(s
->base
)->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
854 header(s
->base
)->h_blocks
= cpu_to_le32(1);
855 header(s
->base
)->h_refcount
= cpu_to_le32(1);
856 s
->first
= ENTRY(header(s
->base
)+1);
857 s
->here
= ENTRY(header(s
->base
)+1);
858 s
->end
= s
->base
+ sb
->s_blocksize
;
861 error
= ext4_xattr_set_entry(i
, s
);
862 if (error
== -EFSCORRUPTED
)
866 if (!IS_LAST_ENTRY(s
->first
))
867 ext4_xattr_rehash(header(s
->base
), s
->here
);
870 if (!IS_LAST_ENTRY(s
->first
)) {
871 new_bh
= ext4_xattr_cache_find(inode
, header(s
->base
), &ce
);
873 /* We found an identical block in the cache. */
874 if (new_bh
== bs
->bh
)
875 ea_bdebug(new_bh
, "keeping");
877 /* The old block is released after updating
879 error
= dquot_alloc_block(inode
,
880 EXT4_C2B(EXT4_SB(sb
), 1));
883 BUFFER_TRACE(new_bh
, "get_write_access");
884 error
= ext4_journal_get_write_access(handle
,
889 le32_add_cpu(&BHDR(new_bh
)->h_refcount
, 1);
890 ea_bdebug(new_bh
, "reusing; refcount now=%d",
891 le32_to_cpu(BHDR(new_bh
)->h_refcount
));
892 unlock_buffer(new_bh
);
893 error
= ext4_handle_dirty_xattr_block(handle
,
899 mb_cache_entry_release(ce
);
901 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
902 /* We were modifying this block in-place. */
903 ea_bdebug(bs
->bh
, "keeping this block");
907 /* We need to allocate a new block */
908 ext4_fsblk_t goal
, block
;
910 goal
= ext4_group_first_block_no(sb
,
911 EXT4_I(inode
)->i_block_group
);
913 /* non-extent files can't have physical blocks past 2^32 */
914 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
915 goal
= goal
& EXT4_MAX_BLOCK_FILE_PHYS
;
917 block
= ext4_new_meta_blocks(handle
, inode
, goal
, 0,
922 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
923 BUG_ON(block
> EXT4_MAX_BLOCK_FILE_PHYS
);
925 ea_idebug(inode
, "creating block %llu",
926 (unsigned long long)block
);
928 new_bh
= sb_getblk(sb
, block
);
929 if (unlikely(!new_bh
)) {
932 ext4_free_blocks(handle
, inode
, NULL
, block
, 1,
933 EXT4_FREE_BLOCKS_METADATA
);
937 error
= ext4_journal_get_create_access(handle
, new_bh
);
939 unlock_buffer(new_bh
);
943 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
944 set_buffer_uptodate(new_bh
);
945 unlock_buffer(new_bh
);
946 ext4_xattr_cache_insert(ext4_mb_cache
, new_bh
);
947 error
= ext4_handle_dirty_xattr_block(handle
,
954 /* Update the inode. */
955 EXT4_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
957 /* Drop the previous xattr block. */
958 if (bs
->bh
&& bs
->bh
!= new_bh
)
959 ext4_xattr_release_block(handle
, inode
, bs
->bh
);
964 mb_cache_entry_release(ce
);
966 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
972 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(sb
), 1));
976 EXT4_ERROR_INODE(inode
, "bad block %llu",
977 EXT4_I(inode
)->i_file_acl
);
983 int ext4_xattr_ibody_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
984 struct ext4_xattr_ibody_find
*is
)
986 struct ext4_xattr_ibody_header
*header
;
987 struct ext4_inode
*raw_inode
;
990 if (EXT4_I(inode
)->i_extra_isize
== 0)
992 raw_inode
= ext4_raw_inode(&is
->iloc
);
993 header
= IHDR(inode
, raw_inode
);
994 is
->s
.base
= is
->s
.first
= IFIRST(header
);
995 is
->s
.here
= is
->s
.first
;
996 is
->s
.end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
997 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
998 error
= ext4_xattr_check_names(IFIRST(header
), is
->s
.end
,
1002 /* Find the named attribute. */
1003 error
= ext4_xattr_find_entry(&is
->s
.here
, i
->name_index
,
1004 i
->name
, is
->s
.end
-
1005 (void *)is
->s
.base
, 0);
1006 if (error
&& error
!= -ENODATA
)
1008 is
->s
.not_found
= error
;
1013 int ext4_xattr_ibody_inline_set(handle_t
*handle
, struct inode
*inode
,
1014 struct ext4_xattr_info
*i
,
1015 struct ext4_xattr_ibody_find
*is
)
1017 struct ext4_xattr_ibody_header
*header
;
1018 struct ext4_xattr_search
*s
= &is
->s
;
1021 if (EXT4_I(inode
)->i_extra_isize
== 0)
1023 error
= ext4_xattr_set_entry(i
, s
);
1025 if (error
== -ENOSPC
&&
1026 ext4_has_inline_data(inode
)) {
1027 error
= ext4_try_to_evict_inline_data(handle
, inode
,
1028 EXT4_XATTR_LEN(strlen(i
->name
) +
1029 EXT4_XATTR_SIZE(i
->value_len
)));
1032 error
= ext4_xattr_ibody_find(inode
, i
, is
);
1035 error
= ext4_xattr_set_entry(i
, s
);
1040 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1041 if (!IS_LAST_ENTRY(s
->first
)) {
1042 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1043 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1045 header
->h_magic
= cpu_to_le32(0);
1046 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1051 static int ext4_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
1052 struct ext4_xattr_info
*i
,
1053 struct ext4_xattr_ibody_find
*is
)
1055 struct ext4_xattr_ibody_header
*header
;
1056 struct ext4_xattr_search
*s
= &is
->s
;
1059 if (EXT4_I(inode
)->i_extra_isize
== 0)
1061 error
= ext4_xattr_set_entry(i
, s
);
1064 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1065 if (!IS_LAST_ENTRY(s
->first
)) {
1066 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1067 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1069 header
->h_magic
= cpu_to_le32(0);
1070 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1076 * ext4_xattr_set_handle()
1078 * Create, replace or remove an extended attribute for this inode. Value
1079 * is NULL to remove an existing extended attribute, and non-NULL to
1080 * either replace an existing extended attribute, or create a new extended
1081 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1082 * specify that an extended attribute must exist and must not exist
1083 * previous to the call, respectively.
1085 * Returns 0, or a negative error number on failure.
1088 ext4_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
1089 const char *name
, const void *value
, size_t value_len
,
1092 struct ext4_xattr_info i
= {
1093 .name_index
= name_index
,
1096 .value_len
= value_len
,
1099 struct ext4_xattr_ibody_find is
= {
1100 .s
= { .not_found
= -ENODATA
, },
1102 struct ext4_xattr_block_find bs
= {
1103 .s
= { .not_found
= -ENODATA
, },
1105 unsigned long no_expand
;
1110 if (strlen(name
) > 255)
1112 down_write(&EXT4_I(inode
)->xattr_sem
);
1113 no_expand
= ext4_test_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1114 ext4_set_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1116 error
= ext4_reserve_inode_write(handle
, inode
, &is
.iloc
);
1120 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
)) {
1121 struct ext4_inode
*raw_inode
= ext4_raw_inode(&is
.iloc
);
1122 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
1123 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
1126 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
1130 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1133 if (is
.s
.not_found
&& bs
.s
.not_found
) {
1135 if (flags
& XATTR_REPLACE
)
1142 if (flags
& XATTR_CREATE
)
1146 if (!is
.s
.not_found
)
1147 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1148 else if (!bs
.s
.not_found
)
1149 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1151 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1152 if (!error
&& !bs
.s
.not_found
) {
1154 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1155 } else if (error
== -ENOSPC
) {
1156 if (EXT4_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
1157 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1161 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1164 if (!is
.s
.not_found
) {
1166 error
= ext4_xattr_ibody_set(handle
, inode
, &i
,
1172 ext4_xattr_update_super_block(handle
, inode
->i_sb
);
1173 inode
->i_ctime
= ext4_current_time(inode
);
1175 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1176 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
1178 * The bh is consumed by ext4_mark_iloc_dirty, even with
1183 ext4_handle_sync(handle
);
1190 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1191 up_write(&EXT4_I(inode
)->xattr_sem
);
1198 * Like ext4_xattr_set_handle, but start from an inode. This extended
1199 * attribute modification is a filesystem transaction by itself.
1201 * Returns 0, or a negative error number on failure.
1204 ext4_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
1205 const void *value
, size_t value_len
, int flags
)
1208 int error
, retries
= 0;
1209 int credits
= ext4_jbd2_credits_xattr(inode
);
1212 handle
= ext4_journal_start(inode
, EXT4_HT_XATTR
, credits
);
1213 if (IS_ERR(handle
)) {
1214 error
= PTR_ERR(handle
);
1218 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, name
,
1219 value
, value_len
, flags
);
1220 error2
= ext4_journal_stop(handle
);
1221 if (error
== -ENOSPC
&&
1222 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1232 * Shift the EA entries in the inode to create space for the increased
1235 static void ext4_xattr_shift_entries(struct ext4_xattr_entry
*entry
,
1236 int value_offs_shift
, void *to
,
1237 void *from
, size_t n
, int blocksize
)
1239 struct ext4_xattr_entry
*last
= entry
;
1242 /* Adjust the value offsets of the entries */
1243 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1244 if (!last
->e_value_block
&& last
->e_value_size
) {
1245 new_offs
= le16_to_cpu(last
->e_value_offs
) +
1247 BUG_ON(new_offs
+ le32_to_cpu(last
->e_value_size
)
1249 last
->e_value_offs
= cpu_to_le16(new_offs
);
1252 /* Shift the entries by n bytes */
1253 memmove(to
, from
, n
);
1257 * Expand an inode by new_extra_isize bytes when EAs are present.
1258 * Returns 0 on success or negative error number on failure.
1260 int ext4_expand_extra_isize_ea(struct inode
*inode
, int new_extra_isize
,
1261 struct ext4_inode
*raw_inode
, handle_t
*handle
)
1263 struct ext4_xattr_ibody_header
*header
;
1264 struct ext4_xattr_entry
*entry
, *last
, *first
;
1265 struct buffer_head
*bh
= NULL
;
1266 struct ext4_xattr_ibody_find
*is
= NULL
;
1267 struct ext4_xattr_block_find
*bs
= NULL
;
1268 char *buffer
= NULL
, *b_entry_name
= NULL
;
1269 size_t min_offs
, free
;
1271 void *base
, *start
, *end
;
1272 int extra_isize
= 0, error
= 0, tried_min_extra_isize
= 0;
1273 int s_min_extra_isize
= le16_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_min_extra_isize
);
1275 down_write(&EXT4_I(inode
)->xattr_sem
);
1277 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
) {
1278 up_write(&EXT4_I(inode
)->xattr_sem
);
1282 header
= IHDR(inode
, raw_inode
);
1283 entry
= IFIRST(header
);
1286 * Check if enough free space is available in the inode to shift the
1287 * entries ahead by new_extra_isize.
1290 base
= start
= entry
;
1291 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1292 min_offs
= end
- base
;
1294 total_ino
= sizeof(struct ext4_xattr_ibody_header
);
1296 free
= ext4_xattr_free_space(last
, &min_offs
, base
, &total_ino
);
1297 if (free
>= new_extra_isize
) {
1298 entry
= IFIRST(header
);
1299 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
1300 - new_extra_isize
, (void *)raw_inode
+
1301 EXT4_GOOD_OLD_INODE_SIZE
+ new_extra_isize
,
1302 (void *)header
, total_ino
,
1303 inode
->i_sb
->s_blocksize
);
1304 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
1310 * Enough free space isn't available in the inode, check if
1311 * EA block can hold new_extra_isize bytes.
1313 if (EXT4_I(inode
)->i_file_acl
) {
1314 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1318 if (ext4_xattr_check_block(inode
, bh
)) {
1319 EXT4_ERROR_INODE(inode
, "bad block %llu",
1320 EXT4_I(inode
)->i_file_acl
);
1321 error
= -EFSCORRUPTED
;
1326 end
= bh
->b_data
+ bh
->b_size
;
1327 min_offs
= end
- base
;
1328 free
= ext4_xattr_free_space(first
, &min_offs
, base
, NULL
);
1329 if (free
< new_extra_isize
) {
1330 if (!tried_min_extra_isize
&& s_min_extra_isize
) {
1331 tried_min_extra_isize
++;
1332 new_extra_isize
= s_min_extra_isize
;
1340 free
= inode
->i_sb
->s_blocksize
;
1343 while (new_extra_isize
> 0) {
1344 size_t offs
, size
, entry_size
;
1345 struct ext4_xattr_entry
*small_entry
= NULL
;
1346 struct ext4_xattr_info i
= {
1350 unsigned int total_size
; /* EA entry size + value size */
1351 unsigned int shift_bytes
; /* No. of bytes to shift EAs by? */
1352 unsigned int min_total_size
= ~0U;
1354 is
= kzalloc(sizeof(struct ext4_xattr_ibody_find
), GFP_NOFS
);
1355 bs
= kzalloc(sizeof(struct ext4_xattr_block_find
), GFP_NOFS
);
1361 is
->s
.not_found
= -ENODATA
;
1362 bs
->s
.not_found
= -ENODATA
;
1366 last
= IFIRST(header
);
1367 /* Find the entry best suited to be pushed into EA block */
1369 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1371 EXT4_XATTR_SIZE(le32_to_cpu(last
->e_value_size
)) +
1372 EXT4_XATTR_LEN(last
->e_name_len
);
1373 if (total_size
<= free
&& total_size
< min_total_size
) {
1374 if (total_size
< new_extra_isize
) {
1378 min_total_size
= total_size
;
1383 if (entry
== NULL
) {
1385 entry
= small_entry
;
1387 if (!tried_min_extra_isize
&&
1388 s_min_extra_isize
) {
1389 tried_min_extra_isize
++;
1390 new_extra_isize
= s_min_extra_isize
;
1391 kfree(is
); is
= NULL
;
1392 kfree(bs
); bs
= NULL
;
1400 offs
= le16_to_cpu(entry
->e_value_offs
);
1401 size
= le32_to_cpu(entry
->e_value_size
);
1402 entry_size
= EXT4_XATTR_LEN(entry
->e_name_len
);
1403 i
.name_index
= entry
->e_name_index
,
1404 buffer
= kmalloc(EXT4_XATTR_SIZE(size
), GFP_NOFS
);
1405 b_entry_name
= kmalloc(entry
->e_name_len
+ 1, GFP_NOFS
);
1406 if (!buffer
|| !b_entry_name
) {
1410 /* Save the entry name and the entry value */
1411 memcpy(buffer
, (void *)IFIRST(header
) + offs
,
1412 EXT4_XATTR_SIZE(size
));
1413 memcpy(b_entry_name
, entry
->e_name
, entry
->e_name_len
);
1414 b_entry_name
[entry
->e_name_len
] = '\0';
1415 i
.name
= b_entry_name
;
1417 error
= ext4_get_inode_loc(inode
, &is
->iloc
);
1421 error
= ext4_xattr_ibody_find(inode
, &i
, is
);
1425 /* Remove the chosen entry from the inode */
1426 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, is
);
1430 entry
= IFIRST(header
);
1431 if (entry_size
+ EXT4_XATTR_SIZE(size
) >= new_extra_isize
)
1432 shift_bytes
= new_extra_isize
;
1434 shift_bytes
= entry_size
+ size
;
1435 /* Adjust the offsets and shift the remaining entries ahead */
1436 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
-
1437 shift_bytes
, (void *)raw_inode
+
1438 EXT4_GOOD_OLD_INODE_SIZE
+ extra_isize
+ shift_bytes
,
1439 (void *)header
, total_ino
- entry_size
,
1440 inode
->i_sb
->s_blocksize
);
1442 extra_isize
+= shift_bytes
;
1443 new_extra_isize
-= shift_bytes
;
1444 EXT4_I(inode
)->i_extra_isize
= extra_isize
;
1446 i
.name
= b_entry_name
;
1449 error
= ext4_xattr_block_find(inode
, &i
, bs
);
1453 /* Add entry which was removed from the inode into the block */
1454 error
= ext4_xattr_block_set(handle
, inode
, &i
, bs
);
1457 kfree(b_entry_name
);
1459 b_entry_name
= NULL
;
1461 brelse(is
->iloc
.bh
);
1466 up_write(&EXT4_I(inode
)->xattr_sem
);
1470 kfree(b_entry_name
);
1473 brelse(is
->iloc
.bh
);
1477 up_write(&EXT4_I(inode
)->xattr_sem
);
1484 * ext4_xattr_delete_inode()
1486 * Free extended attribute resources associated with this inode. This
1487 * is called immediately before an inode is freed. We have exclusive
1488 * access to the inode.
1491 ext4_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
)
1493 struct buffer_head
*bh
= NULL
;
1495 if (!EXT4_I(inode
)->i_file_acl
)
1497 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1499 EXT4_ERROR_INODE(inode
, "block %llu read error",
1500 EXT4_I(inode
)->i_file_acl
);
1503 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
1504 BHDR(bh
)->h_blocks
!= cpu_to_le32(1)) {
1505 EXT4_ERROR_INODE(inode
, "bad block %llu",
1506 EXT4_I(inode
)->i_file_acl
);
1509 ext4_xattr_release_block(handle
, inode
, bh
);
1510 EXT4_I(inode
)->i_file_acl
= 0;
1517 * ext4_xattr_put_super()
1519 * This is called when a file system is unmounted.
1522 ext4_xattr_put_super(struct super_block
*sb
)
1524 mb_cache_shrink(sb
->s_bdev
);
1528 * ext4_xattr_cache_insert()
1530 * Create a new entry in the extended attribute cache, and insert
1531 * it unless such an entry is already in the cache.
1533 * Returns 0, or a negative error number on failure.
1536 ext4_xattr_cache_insert(struct mb_cache
*ext4_mb_cache
, struct buffer_head
*bh
)
1538 __u32 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
1539 struct mb_cache_entry
*ce
;
1542 ce
= mb_cache_entry_alloc(ext4_mb_cache
, GFP_NOFS
);
1544 ea_bdebug(bh
, "out of memory");
1547 error
= mb_cache_entry_insert(ce
, bh
->b_bdev
, bh
->b_blocknr
, hash
);
1549 mb_cache_entry_free(ce
);
1550 if (error
== -EBUSY
) {
1551 ea_bdebug(bh
, "already in cache");
1555 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
1556 mb_cache_entry_release(ce
);
1563 * Compare two extended attribute blocks for equality.
1565 * Returns 0 if the blocks are equal, 1 if they differ, and
1566 * a negative error number on errors.
1569 ext4_xattr_cmp(struct ext4_xattr_header
*header1
,
1570 struct ext4_xattr_header
*header2
)
1572 struct ext4_xattr_entry
*entry1
, *entry2
;
1574 entry1
= ENTRY(header1
+1);
1575 entry2
= ENTRY(header2
+1);
1576 while (!IS_LAST_ENTRY(entry1
)) {
1577 if (IS_LAST_ENTRY(entry2
))
1579 if (entry1
->e_hash
!= entry2
->e_hash
||
1580 entry1
->e_name_index
!= entry2
->e_name_index
||
1581 entry1
->e_name_len
!= entry2
->e_name_len
||
1582 entry1
->e_value_size
!= entry2
->e_value_size
||
1583 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
1585 if (entry1
->e_value_block
!= 0 || entry2
->e_value_block
!= 0)
1586 return -EFSCORRUPTED
;
1587 if (memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
1588 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
1589 le32_to_cpu(entry1
->e_value_size
)))
1592 entry1
= EXT4_XATTR_NEXT(entry1
);
1593 entry2
= EXT4_XATTR_NEXT(entry2
);
1595 if (!IS_LAST_ENTRY(entry2
))
1601 * ext4_xattr_cache_find()
1603 * Find an identical extended attribute block.
1605 * Returns a pointer to the block found, or NULL if such a block was
1606 * not found or an error occurred.
1608 static struct buffer_head
*
1609 ext4_xattr_cache_find(struct inode
*inode
, struct ext4_xattr_header
*header
,
1610 struct mb_cache_entry
**pce
)
1612 __u32 hash
= le32_to_cpu(header
->h_hash
);
1613 struct mb_cache_entry
*ce
;
1614 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
1616 if (!header
->h_hash
)
1617 return NULL
; /* never share */
1618 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
1620 ce
= mb_cache_entry_find_first(ext4_mb_cache
, inode
->i_sb
->s_bdev
,
1623 struct buffer_head
*bh
;
1626 if (PTR_ERR(ce
) == -EAGAIN
)
1630 bh
= sb_bread(inode
->i_sb
, ce
->e_block
);
1632 EXT4_ERROR_INODE(inode
, "block %lu read error",
1633 (unsigned long) ce
->e_block
);
1634 } else if (le32_to_cpu(BHDR(bh
)->h_refcount
) >=
1635 EXT4_XATTR_REFCOUNT_MAX
) {
1636 ea_idebug(inode
, "block %lu refcount %d>=%d",
1637 (unsigned long) ce
->e_block
,
1638 le32_to_cpu(BHDR(bh
)->h_refcount
),
1639 EXT4_XATTR_REFCOUNT_MAX
);
1640 } else if (ext4_xattr_cmp(header
, BHDR(bh
)) == 0) {
1645 ce
= mb_cache_entry_find_next(ce
, inode
->i_sb
->s_bdev
, hash
);
1650 #define NAME_HASH_SHIFT 5
1651 #define VALUE_HASH_SHIFT 16
1654 * ext4_xattr_hash_entry()
1656 * Compute the hash of an extended attribute.
1658 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header
*header
,
1659 struct ext4_xattr_entry
*entry
)
1662 char *name
= entry
->e_name
;
1665 for (n
= 0; n
< entry
->e_name_len
; n
++) {
1666 hash
= (hash
<< NAME_HASH_SHIFT
) ^
1667 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
1671 if (entry
->e_value_block
== 0 && entry
->e_value_size
!= 0) {
1672 __le32
*value
= (__le32
*)((char *)header
+
1673 le16_to_cpu(entry
->e_value_offs
));
1674 for (n
= (le32_to_cpu(entry
->e_value_size
) +
1675 EXT4_XATTR_ROUND
) >> EXT4_XATTR_PAD_BITS
; n
; n
--) {
1676 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
1677 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
1678 le32_to_cpu(*value
++);
1681 entry
->e_hash
= cpu_to_le32(hash
);
1684 #undef NAME_HASH_SHIFT
1685 #undef VALUE_HASH_SHIFT
1687 #define BLOCK_HASH_SHIFT 16
1690 * ext4_xattr_rehash()
1692 * Re-compute the extended attribute hash value after an entry has changed.
1694 static void ext4_xattr_rehash(struct ext4_xattr_header
*header
,
1695 struct ext4_xattr_entry
*entry
)
1697 struct ext4_xattr_entry
*here
;
1700 ext4_xattr_hash_entry(header
, entry
);
1701 here
= ENTRY(header
+1);
1702 while (!IS_LAST_ENTRY(here
)) {
1703 if (!here
->e_hash
) {
1704 /* Block is not shared if an entry's hash value == 0 */
1708 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
1709 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
1710 le32_to_cpu(here
->e_hash
);
1711 here
= EXT4_XATTR_NEXT(here
);
1713 header
->h_hash
= cpu_to_le32(hash
);
1716 #undef BLOCK_HASH_SHIFT
1718 #define HASH_BUCKET_BITS 10
1721 ext4_xattr_create_cache(char *name
)
1723 return mb_cache_create(name
, HASH_BUCKET_BITS
);
1726 void ext4_xattr_destroy_cache(struct mb_cache
*cache
)
1729 mb_cache_destroy(cache
);