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
);
408 size_t size
= handler
->list(handler
, dentry
, buffer
,
419 return buffer_size
- rest
;
423 ext4_xattr_block_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
425 struct inode
*inode
= d_inode(dentry
);
426 struct buffer_head
*bh
= NULL
;
428 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
430 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
431 buffer
, (long)buffer_size
);
434 if (!EXT4_I(inode
)->i_file_acl
)
436 ea_idebug(inode
, "reading block %llu",
437 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
438 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
442 ea_bdebug(bh
, "b_count=%d, refcount=%d",
443 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
444 if (ext4_xattr_check_block(inode
, bh
)) {
445 EXT4_ERROR_INODE(inode
, "bad block %llu",
446 EXT4_I(inode
)->i_file_acl
);
447 error
= -EFSCORRUPTED
;
450 ext4_xattr_cache_insert(ext4_mb_cache
, bh
);
451 error
= ext4_xattr_list_entries(dentry
, BFIRST(bh
), buffer
, buffer_size
);
460 ext4_xattr_ibody_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
462 struct inode
*inode
= d_inode(dentry
);
463 struct ext4_xattr_ibody_header
*header
;
464 struct ext4_inode
*raw_inode
;
465 struct ext4_iloc iloc
;
469 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
471 error
= ext4_get_inode_loc(inode
, &iloc
);
474 raw_inode
= ext4_raw_inode(&iloc
);
475 header
= IHDR(inode
, raw_inode
);
476 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
477 error
= ext4_xattr_check_names(IFIRST(header
), end
, IFIRST(header
));
480 error
= ext4_xattr_list_entries(dentry
, IFIRST(header
),
481 buffer
, buffer_size
);
491 * Copy a list of attribute names into the buffer
492 * provided, or compute the buffer size required.
493 * Buffer is NULL to compute the size of the buffer required.
495 * Returns a negative error number on failure, or the number of bytes
496 * used / required on success.
499 ext4_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
503 down_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
504 ret
= ret2
= ext4_xattr_ibody_list(dentry
, buffer
, buffer_size
);
511 ret
= ext4_xattr_block_list(dentry
, buffer
, buffer_size
);
516 up_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
521 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
524 static void ext4_xattr_update_super_block(handle_t
*handle
,
525 struct super_block
*sb
)
527 if (ext4_has_feature_xattr(sb
))
530 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
531 if (ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
) == 0) {
532 ext4_set_feature_xattr(sb
);
533 ext4_handle_dirty_super(handle
, sb
);
538 * Release the xattr block BH: If the reference count is > 1, decrement it;
539 * otherwise free the block.
542 ext4_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
543 struct buffer_head
*bh
)
545 struct mb_cache_entry
*ce
= NULL
;
547 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
549 ce
= mb_cache_entry_get(ext4_mb_cache
, bh
->b_bdev
, bh
->b_blocknr
);
550 BUFFER_TRACE(bh
, "get_write_access");
551 error
= ext4_journal_get_write_access(handle
, bh
);
556 if (BHDR(bh
)->h_refcount
== cpu_to_le32(1)) {
557 ea_bdebug(bh
, "refcount now=0; freeing");
559 mb_cache_entry_free(ce
);
562 ext4_free_blocks(handle
, inode
, bh
, 0, 1,
563 EXT4_FREE_BLOCKS_METADATA
|
564 EXT4_FREE_BLOCKS_FORGET
);
566 le32_add_cpu(&BHDR(bh
)->h_refcount
, -1);
568 mb_cache_entry_release(ce
);
570 * Beware of this ugliness: Releasing of xattr block references
571 * from different inodes can race and so we have to protect
572 * from a race where someone else frees the block (and releases
573 * its journal_head) before we are done dirtying the buffer. In
574 * nojournal mode this race is harmless and we actually cannot
575 * call ext4_handle_dirty_xattr_block() with locked buffer as
576 * that function can call sync_dirty_buffer() so for that case
577 * we handle the dirtying after unlocking the buffer.
579 if (ext4_handle_valid(handle
))
580 error
= ext4_handle_dirty_xattr_block(handle
, inode
,
583 if (!ext4_handle_valid(handle
))
584 error
= ext4_handle_dirty_xattr_block(handle
, inode
,
587 ext4_handle_sync(handle
);
588 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(inode
->i_sb
), 1));
589 ea_bdebug(bh
, "refcount now=%d; releasing",
590 le32_to_cpu(BHDR(bh
)->h_refcount
));
593 ext4_std_error(inode
->i_sb
, error
);
598 * Find the available free space for EAs. This also returns the total number of
599 * bytes used by EA entries.
601 static size_t ext4_xattr_free_space(struct ext4_xattr_entry
*last
,
602 size_t *min_offs
, void *base
, int *total
)
604 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
605 if (!last
->e_value_block
&& last
->e_value_size
) {
606 size_t offs
= le16_to_cpu(last
->e_value_offs
);
607 if (offs
< *min_offs
)
611 *total
+= EXT4_XATTR_LEN(last
->e_name_len
);
613 return (*min_offs
- ((void *)last
- base
) - sizeof(__u32
));
617 ext4_xattr_set_entry(struct ext4_xattr_info
*i
, struct ext4_xattr_search
*s
)
619 struct ext4_xattr_entry
*last
;
620 size_t free
, min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
622 /* Compute min_offs and last. */
624 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
625 if (!last
->e_value_block
&& last
->e_value_size
) {
626 size_t offs
= le16_to_cpu(last
->e_value_offs
);
631 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
633 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
634 size_t size
= le32_to_cpu(s
->here
->e_value_size
);
635 free
+= EXT4_XATTR_SIZE(size
);
637 free
+= EXT4_XATTR_LEN(name_len
);
640 if (free
< EXT4_XATTR_LEN(name_len
) +
641 EXT4_XATTR_SIZE(i
->value_len
))
645 if (i
->value
&& s
->not_found
) {
646 /* Insert the new name. */
647 size_t size
= EXT4_XATTR_LEN(name_len
);
648 size_t rest
= (void *)last
- (void *)s
->here
+ sizeof(__u32
);
649 memmove((void *)s
->here
+ size
, s
->here
, rest
);
650 memset(s
->here
, 0, size
);
651 s
->here
->e_name_index
= i
->name_index
;
652 s
->here
->e_name_len
= name_len
;
653 memcpy(s
->here
->e_name
, i
->name
, name_len
);
655 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
656 void *first_val
= s
->base
+ min_offs
;
657 size_t offs
= le16_to_cpu(s
->here
->e_value_offs
);
658 void *val
= s
->base
+ offs
;
659 size_t size
= EXT4_XATTR_SIZE(
660 le32_to_cpu(s
->here
->e_value_size
));
662 if (i
->value
&& size
== EXT4_XATTR_SIZE(i
->value_len
)) {
663 /* The old and the new value have the same
664 size. Just replace. */
665 s
->here
->e_value_size
=
666 cpu_to_le32(i
->value_len
);
667 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
668 memset(val
, 0, size
);
670 /* Clear pad bytes first. */
671 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
673 memcpy(val
, i
->value
, i
->value_len
);
678 /* Remove the old value. */
679 memmove(first_val
+ size
, first_val
, val
- first_val
);
680 memset(first_val
, 0, size
);
681 s
->here
->e_value_size
= 0;
682 s
->here
->e_value_offs
= 0;
685 /* Adjust all value offsets. */
687 while (!IS_LAST_ENTRY(last
)) {
688 size_t o
= le16_to_cpu(last
->e_value_offs
);
689 if (!last
->e_value_block
&&
690 last
->e_value_size
&& o
< offs
)
692 cpu_to_le16(o
+ size
);
693 last
= EXT4_XATTR_NEXT(last
);
697 /* Remove the old name. */
698 size_t size
= EXT4_XATTR_LEN(name_len
);
699 last
= ENTRY((void *)last
- size
);
700 memmove(s
->here
, (void *)s
->here
+ size
,
701 (void *)last
- (void *)s
->here
+ sizeof(__u32
));
702 memset(last
, 0, size
);
707 /* Insert the new value. */
708 s
->here
->e_value_size
= cpu_to_le32(i
->value_len
);
710 size_t size
= EXT4_XATTR_SIZE(i
->value_len
);
711 void *val
= s
->base
+ min_offs
- size
;
712 s
->here
->e_value_offs
= cpu_to_le16(min_offs
- size
);
713 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
714 memset(val
, 0, size
);
716 /* Clear the pad bytes first. */
717 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
719 memcpy(val
, i
->value
, i
->value_len
);
726 struct ext4_xattr_block_find
{
727 struct ext4_xattr_search s
;
728 struct buffer_head
*bh
;
732 ext4_xattr_block_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
733 struct ext4_xattr_block_find
*bs
)
735 struct super_block
*sb
= inode
->i_sb
;
738 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
739 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
741 if (EXT4_I(inode
)->i_file_acl
) {
742 /* The inode already has an extended attribute block. */
743 bs
->bh
= sb_bread(sb
, EXT4_I(inode
)->i_file_acl
);
747 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
748 atomic_read(&(bs
->bh
->b_count
)),
749 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
750 if (ext4_xattr_check_block(inode
, bs
->bh
)) {
751 EXT4_ERROR_INODE(inode
, "bad block %llu",
752 EXT4_I(inode
)->i_file_acl
);
753 error
= -EFSCORRUPTED
;
756 /* Find the named attribute. */
757 bs
->s
.base
= BHDR(bs
->bh
);
758 bs
->s
.first
= BFIRST(bs
->bh
);
759 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
760 bs
->s
.here
= bs
->s
.first
;
761 error
= ext4_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
762 i
->name
, bs
->bh
->b_size
, 1);
763 if (error
&& error
!= -ENODATA
)
765 bs
->s
.not_found
= error
;
774 ext4_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
775 struct ext4_xattr_info
*i
,
776 struct ext4_xattr_block_find
*bs
)
778 struct super_block
*sb
= inode
->i_sb
;
779 struct buffer_head
*new_bh
= NULL
;
780 struct ext4_xattr_search
*s
= &bs
->s
;
781 struct mb_cache_entry
*ce
= NULL
;
783 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
785 #define header(x) ((struct ext4_xattr_header *)(x))
787 if (i
->value
&& i
->value_len
> sb
->s_blocksize
)
790 ce
= mb_cache_entry_get(ext4_mb_cache
, bs
->bh
->b_bdev
,
792 BUFFER_TRACE(bs
->bh
, "get_write_access");
793 error
= ext4_journal_get_write_access(handle
, bs
->bh
);
798 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
800 mb_cache_entry_free(ce
);
803 ea_bdebug(bs
->bh
, "modifying in-place");
804 error
= ext4_xattr_set_entry(i
, s
);
806 if (!IS_LAST_ENTRY(s
->first
))
807 ext4_xattr_rehash(header(s
->base
),
809 ext4_xattr_cache_insert(ext4_mb_cache
,
812 unlock_buffer(bs
->bh
);
813 if (error
== -EFSCORRUPTED
)
816 error
= ext4_handle_dirty_xattr_block(handle
,
823 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
825 unlock_buffer(bs
->bh
);
827 mb_cache_entry_release(ce
);
830 ea_bdebug(bs
->bh
, "cloning");
831 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
835 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
836 s
->first
= ENTRY(header(s
->base
)+1);
837 header(s
->base
)->h_refcount
= cpu_to_le32(1);
838 s
->here
= ENTRY(s
->base
+ offset
);
839 s
->end
= s
->base
+ bs
->bh
->b_size
;
842 /* Allocate a buffer where we construct the new block. */
843 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
844 /* assert(header == s->base) */
848 header(s
->base
)->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
849 header(s
->base
)->h_blocks
= cpu_to_le32(1);
850 header(s
->base
)->h_refcount
= cpu_to_le32(1);
851 s
->first
= ENTRY(header(s
->base
)+1);
852 s
->here
= ENTRY(header(s
->base
)+1);
853 s
->end
= s
->base
+ sb
->s_blocksize
;
856 error
= ext4_xattr_set_entry(i
, s
);
857 if (error
== -EFSCORRUPTED
)
861 if (!IS_LAST_ENTRY(s
->first
))
862 ext4_xattr_rehash(header(s
->base
), s
->here
);
865 if (!IS_LAST_ENTRY(s
->first
)) {
866 new_bh
= ext4_xattr_cache_find(inode
, header(s
->base
), &ce
);
868 /* We found an identical block in the cache. */
869 if (new_bh
== bs
->bh
)
870 ea_bdebug(new_bh
, "keeping");
872 /* The old block is released after updating
874 error
= dquot_alloc_block(inode
,
875 EXT4_C2B(EXT4_SB(sb
), 1));
878 BUFFER_TRACE(new_bh
, "get_write_access");
879 error
= ext4_journal_get_write_access(handle
,
884 le32_add_cpu(&BHDR(new_bh
)->h_refcount
, 1);
885 ea_bdebug(new_bh
, "reusing; refcount now=%d",
886 le32_to_cpu(BHDR(new_bh
)->h_refcount
));
887 unlock_buffer(new_bh
);
888 error
= ext4_handle_dirty_xattr_block(handle
,
894 mb_cache_entry_release(ce
);
896 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
897 /* We were modifying this block in-place. */
898 ea_bdebug(bs
->bh
, "keeping this block");
902 /* We need to allocate a new block */
903 ext4_fsblk_t goal
, block
;
905 goal
= ext4_group_first_block_no(sb
,
906 EXT4_I(inode
)->i_block_group
);
908 /* non-extent files can't have physical blocks past 2^32 */
909 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
910 goal
= goal
& EXT4_MAX_BLOCK_FILE_PHYS
;
912 block
= ext4_new_meta_blocks(handle
, inode
, goal
, 0,
917 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
918 BUG_ON(block
> EXT4_MAX_BLOCK_FILE_PHYS
);
920 ea_idebug(inode
, "creating block %llu",
921 (unsigned long long)block
);
923 new_bh
= sb_getblk(sb
, block
);
924 if (unlikely(!new_bh
)) {
927 ext4_free_blocks(handle
, inode
, NULL
, block
, 1,
928 EXT4_FREE_BLOCKS_METADATA
);
932 error
= ext4_journal_get_create_access(handle
, new_bh
);
934 unlock_buffer(new_bh
);
938 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
939 set_buffer_uptodate(new_bh
);
940 unlock_buffer(new_bh
);
941 ext4_xattr_cache_insert(ext4_mb_cache
, new_bh
);
942 error
= ext4_handle_dirty_xattr_block(handle
,
949 /* Update the inode. */
950 EXT4_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
952 /* Drop the previous xattr block. */
953 if (bs
->bh
&& bs
->bh
!= new_bh
)
954 ext4_xattr_release_block(handle
, inode
, bs
->bh
);
959 mb_cache_entry_release(ce
);
961 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
967 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(sb
), 1));
971 EXT4_ERROR_INODE(inode
, "bad block %llu",
972 EXT4_I(inode
)->i_file_acl
);
978 int ext4_xattr_ibody_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
979 struct ext4_xattr_ibody_find
*is
)
981 struct ext4_xattr_ibody_header
*header
;
982 struct ext4_inode
*raw_inode
;
985 if (EXT4_I(inode
)->i_extra_isize
== 0)
987 raw_inode
= ext4_raw_inode(&is
->iloc
);
988 header
= IHDR(inode
, raw_inode
);
989 is
->s
.base
= is
->s
.first
= IFIRST(header
);
990 is
->s
.here
= is
->s
.first
;
991 is
->s
.end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
992 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
993 error
= ext4_xattr_check_names(IFIRST(header
), is
->s
.end
,
997 /* Find the named attribute. */
998 error
= ext4_xattr_find_entry(&is
->s
.here
, i
->name_index
,
1000 (void *)is
->s
.base
, 0);
1001 if (error
&& error
!= -ENODATA
)
1003 is
->s
.not_found
= error
;
1008 int ext4_xattr_ibody_inline_set(handle_t
*handle
, struct inode
*inode
,
1009 struct ext4_xattr_info
*i
,
1010 struct ext4_xattr_ibody_find
*is
)
1012 struct ext4_xattr_ibody_header
*header
;
1013 struct ext4_xattr_search
*s
= &is
->s
;
1016 if (EXT4_I(inode
)->i_extra_isize
== 0)
1018 error
= ext4_xattr_set_entry(i
, s
);
1020 if (error
== -ENOSPC
&&
1021 ext4_has_inline_data(inode
)) {
1022 error
= ext4_try_to_evict_inline_data(handle
, inode
,
1023 EXT4_XATTR_LEN(strlen(i
->name
) +
1024 EXT4_XATTR_SIZE(i
->value_len
)));
1027 error
= ext4_xattr_ibody_find(inode
, i
, is
);
1030 error
= ext4_xattr_set_entry(i
, s
);
1035 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1036 if (!IS_LAST_ENTRY(s
->first
)) {
1037 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1038 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1040 header
->h_magic
= cpu_to_le32(0);
1041 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1046 static int ext4_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
1047 struct ext4_xattr_info
*i
,
1048 struct ext4_xattr_ibody_find
*is
)
1050 struct ext4_xattr_ibody_header
*header
;
1051 struct ext4_xattr_search
*s
= &is
->s
;
1054 if (EXT4_I(inode
)->i_extra_isize
== 0)
1056 error
= ext4_xattr_set_entry(i
, s
);
1059 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1060 if (!IS_LAST_ENTRY(s
->first
)) {
1061 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1062 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1064 header
->h_magic
= cpu_to_le32(0);
1065 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1071 * ext4_xattr_set_handle()
1073 * Create, replace or remove an extended attribute for this inode. Value
1074 * is NULL to remove an existing extended attribute, and non-NULL to
1075 * either replace an existing extended attribute, or create a new extended
1076 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1077 * specify that an extended attribute must exist and must not exist
1078 * previous to the call, respectively.
1080 * Returns 0, or a negative error number on failure.
1083 ext4_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
1084 const char *name
, const void *value
, size_t value_len
,
1087 struct ext4_xattr_info i
= {
1088 .name_index
= name_index
,
1091 .value_len
= value_len
,
1094 struct ext4_xattr_ibody_find is
= {
1095 .s
= { .not_found
= -ENODATA
, },
1097 struct ext4_xattr_block_find bs
= {
1098 .s
= { .not_found
= -ENODATA
, },
1100 unsigned long no_expand
;
1105 if (strlen(name
) > 255)
1107 down_write(&EXT4_I(inode
)->xattr_sem
);
1108 no_expand
= ext4_test_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1109 ext4_set_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1111 error
= ext4_reserve_inode_write(handle
, inode
, &is
.iloc
);
1115 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
)) {
1116 struct ext4_inode
*raw_inode
= ext4_raw_inode(&is
.iloc
);
1117 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
1118 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
1121 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
1125 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1128 if (is
.s
.not_found
&& bs
.s
.not_found
) {
1130 if (flags
& XATTR_REPLACE
)
1137 if (flags
& XATTR_CREATE
)
1141 if (!is
.s
.not_found
)
1142 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1143 else if (!bs
.s
.not_found
)
1144 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1146 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1147 if (!error
&& !bs
.s
.not_found
) {
1149 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1150 } else if (error
== -ENOSPC
) {
1151 if (EXT4_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
1152 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1156 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1159 if (!is
.s
.not_found
) {
1161 error
= ext4_xattr_ibody_set(handle
, inode
, &i
,
1167 ext4_xattr_update_super_block(handle
, inode
->i_sb
);
1168 inode
->i_ctime
= ext4_current_time(inode
);
1170 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1171 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
1173 * The bh is consumed by ext4_mark_iloc_dirty, even with
1178 ext4_handle_sync(handle
);
1185 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1186 up_write(&EXT4_I(inode
)->xattr_sem
);
1193 * Like ext4_xattr_set_handle, but start from an inode. This extended
1194 * attribute modification is a filesystem transaction by itself.
1196 * Returns 0, or a negative error number on failure.
1199 ext4_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
1200 const void *value
, size_t value_len
, int flags
)
1203 int error
, retries
= 0;
1204 int credits
= ext4_jbd2_credits_xattr(inode
);
1207 handle
= ext4_journal_start(inode
, EXT4_HT_XATTR
, credits
);
1208 if (IS_ERR(handle
)) {
1209 error
= PTR_ERR(handle
);
1213 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, name
,
1214 value
, value_len
, flags
);
1215 error2
= ext4_journal_stop(handle
);
1216 if (error
== -ENOSPC
&&
1217 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1227 * Shift the EA entries in the inode to create space for the increased
1230 static void ext4_xattr_shift_entries(struct ext4_xattr_entry
*entry
,
1231 int value_offs_shift
, void *to
,
1232 void *from
, size_t n
, int blocksize
)
1234 struct ext4_xattr_entry
*last
= entry
;
1237 /* Adjust the value offsets of the entries */
1238 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1239 if (!last
->e_value_block
&& last
->e_value_size
) {
1240 new_offs
= le16_to_cpu(last
->e_value_offs
) +
1242 BUG_ON(new_offs
+ le32_to_cpu(last
->e_value_size
)
1244 last
->e_value_offs
= cpu_to_le16(new_offs
);
1247 /* Shift the entries by n bytes */
1248 memmove(to
, from
, n
);
1252 * Expand an inode by new_extra_isize bytes when EAs are present.
1253 * Returns 0 on success or negative error number on failure.
1255 int ext4_expand_extra_isize_ea(struct inode
*inode
, int new_extra_isize
,
1256 struct ext4_inode
*raw_inode
, handle_t
*handle
)
1258 struct ext4_xattr_ibody_header
*header
;
1259 struct ext4_xattr_entry
*entry
, *last
, *first
;
1260 struct buffer_head
*bh
= NULL
;
1261 struct ext4_xattr_ibody_find
*is
= NULL
;
1262 struct ext4_xattr_block_find
*bs
= NULL
;
1263 char *buffer
= NULL
, *b_entry_name
= NULL
;
1264 size_t min_offs
, free
;
1266 void *base
, *start
, *end
;
1267 int extra_isize
= 0, error
= 0, tried_min_extra_isize
= 0;
1268 int s_min_extra_isize
= le16_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_min_extra_isize
);
1270 down_write(&EXT4_I(inode
)->xattr_sem
);
1272 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
) {
1273 up_write(&EXT4_I(inode
)->xattr_sem
);
1277 header
= IHDR(inode
, raw_inode
);
1278 entry
= IFIRST(header
);
1281 * Check if enough free space is available in the inode to shift the
1282 * entries ahead by new_extra_isize.
1285 base
= start
= entry
;
1286 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1287 min_offs
= end
- base
;
1289 total_ino
= sizeof(struct ext4_xattr_ibody_header
);
1291 free
= ext4_xattr_free_space(last
, &min_offs
, base
, &total_ino
);
1292 if (free
>= new_extra_isize
) {
1293 entry
= IFIRST(header
);
1294 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
1295 - new_extra_isize
, (void *)raw_inode
+
1296 EXT4_GOOD_OLD_INODE_SIZE
+ new_extra_isize
,
1297 (void *)header
, total_ino
,
1298 inode
->i_sb
->s_blocksize
);
1299 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
1305 * Enough free space isn't available in the inode, check if
1306 * EA block can hold new_extra_isize bytes.
1308 if (EXT4_I(inode
)->i_file_acl
) {
1309 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1313 if (ext4_xattr_check_block(inode
, bh
)) {
1314 EXT4_ERROR_INODE(inode
, "bad block %llu",
1315 EXT4_I(inode
)->i_file_acl
);
1316 error
= -EFSCORRUPTED
;
1321 end
= bh
->b_data
+ bh
->b_size
;
1322 min_offs
= end
- base
;
1323 free
= ext4_xattr_free_space(first
, &min_offs
, base
, NULL
);
1324 if (free
< new_extra_isize
) {
1325 if (!tried_min_extra_isize
&& s_min_extra_isize
) {
1326 tried_min_extra_isize
++;
1327 new_extra_isize
= s_min_extra_isize
;
1335 free
= inode
->i_sb
->s_blocksize
;
1338 while (new_extra_isize
> 0) {
1339 size_t offs
, size
, entry_size
;
1340 struct ext4_xattr_entry
*small_entry
= NULL
;
1341 struct ext4_xattr_info i
= {
1345 unsigned int total_size
; /* EA entry size + value size */
1346 unsigned int shift_bytes
; /* No. of bytes to shift EAs by? */
1347 unsigned int min_total_size
= ~0U;
1349 is
= kzalloc(sizeof(struct ext4_xattr_ibody_find
), GFP_NOFS
);
1350 bs
= kzalloc(sizeof(struct ext4_xattr_block_find
), GFP_NOFS
);
1356 is
->s
.not_found
= -ENODATA
;
1357 bs
->s
.not_found
= -ENODATA
;
1361 last
= IFIRST(header
);
1362 /* Find the entry best suited to be pushed into EA block */
1364 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1366 EXT4_XATTR_SIZE(le32_to_cpu(last
->e_value_size
)) +
1367 EXT4_XATTR_LEN(last
->e_name_len
);
1368 if (total_size
<= free
&& total_size
< min_total_size
) {
1369 if (total_size
< new_extra_isize
) {
1373 min_total_size
= total_size
;
1378 if (entry
== NULL
) {
1380 entry
= small_entry
;
1382 if (!tried_min_extra_isize
&&
1383 s_min_extra_isize
) {
1384 tried_min_extra_isize
++;
1385 new_extra_isize
= s_min_extra_isize
;
1386 kfree(is
); is
= NULL
;
1387 kfree(bs
); bs
= NULL
;
1395 offs
= le16_to_cpu(entry
->e_value_offs
);
1396 size
= le32_to_cpu(entry
->e_value_size
);
1397 entry_size
= EXT4_XATTR_LEN(entry
->e_name_len
);
1398 i
.name_index
= entry
->e_name_index
,
1399 buffer
= kmalloc(EXT4_XATTR_SIZE(size
), GFP_NOFS
);
1400 b_entry_name
= kmalloc(entry
->e_name_len
+ 1, GFP_NOFS
);
1401 if (!buffer
|| !b_entry_name
) {
1405 /* Save the entry name and the entry value */
1406 memcpy(buffer
, (void *)IFIRST(header
) + offs
,
1407 EXT4_XATTR_SIZE(size
));
1408 memcpy(b_entry_name
, entry
->e_name
, entry
->e_name_len
);
1409 b_entry_name
[entry
->e_name_len
] = '\0';
1410 i
.name
= b_entry_name
;
1412 error
= ext4_get_inode_loc(inode
, &is
->iloc
);
1416 error
= ext4_xattr_ibody_find(inode
, &i
, is
);
1420 /* Remove the chosen entry from the inode */
1421 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, is
);
1425 entry
= IFIRST(header
);
1426 if (entry_size
+ EXT4_XATTR_SIZE(size
) >= new_extra_isize
)
1427 shift_bytes
= new_extra_isize
;
1429 shift_bytes
= entry_size
+ size
;
1430 /* Adjust the offsets and shift the remaining entries ahead */
1431 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
-
1432 shift_bytes
, (void *)raw_inode
+
1433 EXT4_GOOD_OLD_INODE_SIZE
+ extra_isize
+ shift_bytes
,
1434 (void *)header
, total_ino
- entry_size
,
1435 inode
->i_sb
->s_blocksize
);
1437 extra_isize
+= shift_bytes
;
1438 new_extra_isize
-= shift_bytes
;
1439 EXT4_I(inode
)->i_extra_isize
= extra_isize
;
1441 i
.name
= b_entry_name
;
1444 error
= ext4_xattr_block_find(inode
, &i
, bs
);
1448 /* Add entry which was removed from the inode into the block */
1449 error
= ext4_xattr_block_set(handle
, inode
, &i
, bs
);
1452 kfree(b_entry_name
);
1454 b_entry_name
= NULL
;
1456 brelse(is
->iloc
.bh
);
1461 up_write(&EXT4_I(inode
)->xattr_sem
);
1465 kfree(b_entry_name
);
1468 brelse(is
->iloc
.bh
);
1472 up_write(&EXT4_I(inode
)->xattr_sem
);
1479 * ext4_xattr_delete_inode()
1481 * Free extended attribute resources associated with this inode. This
1482 * is called immediately before an inode is freed. We have exclusive
1483 * access to the inode.
1486 ext4_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
)
1488 struct buffer_head
*bh
= NULL
;
1490 if (!EXT4_I(inode
)->i_file_acl
)
1492 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1494 EXT4_ERROR_INODE(inode
, "block %llu read error",
1495 EXT4_I(inode
)->i_file_acl
);
1498 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
1499 BHDR(bh
)->h_blocks
!= cpu_to_le32(1)) {
1500 EXT4_ERROR_INODE(inode
, "bad block %llu",
1501 EXT4_I(inode
)->i_file_acl
);
1504 ext4_xattr_release_block(handle
, inode
, bh
);
1505 EXT4_I(inode
)->i_file_acl
= 0;
1512 * ext4_xattr_put_super()
1514 * This is called when a file system is unmounted.
1517 ext4_xattr_put_super(struct super_block
*sb
)
1519 mb_cache_shrink(sb
->s_bdev
);
1523 * ext4_xattr_cache_insert()
1525 * Create a new entry in the extended attribute cache, and insert
1526 * it unless such an entry is already in the cache.
1528 * Returns 0, or a negative error number on failure.
1531 ext4_xattr_cache_insert(struct mb_cache
*ext4_mb_cache
, struct buffer_head
*bh
)
1533 __u32 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
1534 struct mb_cache_entry
*ce
;
1537 ce
= mb_cache_entry_alloc(ext4_mb_cache
, GFP_NOFS
);
1539 ea_bdebug(bh
, "out of memory");
1542 error
= mb_cache_entry_insert(ce
, bh
->b_bdev
, bh
->b_blocknr
, hash
);
1544 mb_cache_entry_free(ce
);
1545 if (error
== -EBUSY
) {
1546 ea_bdebug(bh
, "already in cache");
1550 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
1551 mb_cache_entry_release(ce
);
1558 * Compare two extended attribute blocks for equality.
1560 * Returns 0 if the blocks are equal, 1 if they differ, and
1561 * a negative error number on errors.
1564 ext4_xattr_cmp(struct ext4_xattr_header
*header1
,
1565 struct ext4_xattr_header
*header2
)
1567 struct ext4_xattr_entry
*entry1
, *entry2
;
1569 entry1
= ENTRY(header1
+1);
1570 entry2
= ENTRY(header2
+1);
1571 while (!IS_LAST_ENTRY(entry1
)) {
1572 if (IS_LAST_ENTRY(entry2
))
1574 if (entry1
->e_hash
!= entry2
->e_hash
||
1575 entry1
->e_name_index
!= entry2
->e_name_index
||
1576 entry1
->e_name_len
!= entry2
->e_name_len
||
1577 entry1
->e_value_size
!= entry2
->e_value_size
||
1578 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
1580 if (entry1
->e_value_block
!= 0 || entry2
->e_value_block
!= 0)
1581 return -EFSCORRUPTED
;
1582 if (memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
1583 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
1584 le32_to_cpu(entry1
->e_value_size
)))
1587 entry1
= EXT4_XATTR_NEXT(entry1
);
1588 entry2
= EXT4_XATTR_NEXT(entry2
);
1590 if (!IS_LAST_ENTRY(entry2
))
1596 * ext4_xattr_cache_find()
1598 * Find an identical extended attribute block.
1600 * Returns a pointer to the block found, or NULL if such a block was
1601 * not found or an error occurred.
1603 static struct buffer_head
*
1604 ext4_xattr_cache_find(struct inode
*inode
, struct ext4_xattr_header
*header
,
1605 struct mb_cache_entry
**pce
)
1607 __u32 hash
= le32_to_cpu(header
->h_hash
);
1608 struct mb_cache_entry
*ce
;
1609 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
1611 if (!header
->h_hash
)
1612 return NULL
; /* never share */
1613 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
1615 ce
= mb_cache_entry_find_first(ext4_mb_cache
, inode
->i_sb
->s_bdev
,
1618 struct buffer_head
*bh
;
1621 if (PTR_ERR(ce
) == -EAGAIN
)
1625 bh
= sb_bread(inode
->i_sb
, ce
->e_block
);
1627 EXT4_ERROR_INODE(inode
, "block %lu read error",
1628 (unsigned long) ce
->e_block
);
1629 } else if (le32_to_cpu(BHDR(bh
)->h_refcount
) >=
1630 EXT4_XATTR_REFCOUNT_MAX
) {
1631 ea_idebug(inode
, "block %lu refcount %d>=%d",
1632 (unsigned long) ce
->e_block
,
1633 le32_to_cpu(BHDR(bh
)->h_refcount
),
1634 EXT4_XATTR_REFCOUNT_MAX
);
1635 } else if (ext4_xattr_cmp(header
, BHDR(bh
)) == 0) {
1640 ce
= mb_cache_entry_find_next(ce
, inode
->i_sb
->s_bdev
, hash
);
1645 #define NAME_HASH_SHIFT 5
1646 #define VALUE_HASH_SHIFT 16
1649 * ext4_xattr_hash_entry()
1651 * Compute the hash of an extended attribute.
1653 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header
*header
,
1654 struct ext4_xattr_entry
*entry
)
1657 char *name
= entry
->e_name
;
1660 for (n
= 0; n
< entry
->e_name_len
; n
++) {
1661 hash
= (hash
<< NAME_HASH_SHIFT
) ^
1662 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
1666 if (entry
->e_value_block
== 0 && entry
->e_value_size
!= 0) {
1667 __le32
*value
= (__le32
*)((char *)header
+
1668 le16_to_cpu(entry
->e_value_offs
));
1669 for (n
= (le32_to_cpu(entry
->e_value_size
) +
1670 EXT4_XATTR_ROUND
) >> EXT4_XATTR_PAD_BITS
; n
; n
--) {
1671 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
1672 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
1673 le32_to_cpu(*value
++);
1676 entry
->e_hash
= cpu_to_le32(hash
);
1679 #undef NAME_HASH_SHIFT
1680 #undef VALUE_HASH_SHIFT
1682 #define BLOCK_HASH_SHIFT 16
1685 * ext4_xattr_rehash()
1687 * Re-compute the extended attribute hash value after an entry has changed.
1689 static void ext4_xattr_rehash(struct ext4_xattr_header
*header
,
1690 struct ext4_xattr_entry
*entry
)
1692 struct ext4_xattr_entry
*here
;
1695 ext4_xattr_hash_entry(header
, entry
);
1696 here
= ENTRY(header
+1);
1697 while (!IS_LAST_ENTRY(here
)) {
1698 if (!here
->e_hash
) {
1699 /* Block is not shared if an entry's hash value == 0 */
1703 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
1704 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
1705 le32_to_cpu(here
->e_hash
);
1706 here
= EXT4_XATTR_NEXT(here
);
1708 header
->h_hash
= cpu_to_le32(hash
);
1711 #undef BLOCK_HASH_SHIFT
1713 #define HASH_BUCKET_BITS 10
1716 ext4_xattr_create_cache(char *name
)
1718 return mb_cache_create(name
, HASH_BUCKET_BITS
);
1721 void ext4_xattr_destroy_cache(struct mb_cache
*cache
)
1724 mb_cache_destroy(cache
);