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
)
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
))
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))
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
)
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
))
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
);
303 ext4_xattr_cache_insert(ext4_mb_cache
, bh
);
305 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, bh
->b_size
, 1);
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(dentry
, buffer
, rest
,
420 return buffer_size
- rest
;
424 ext4_xattr_block_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
426 struct inode
*inode
= d_inode(dentry
);
427 struct buffer_head
*bh
= NULL
;
429 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
431 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
432 buffer
, (long)buffer_size
);
435 if (!EXT4_I(inode
)->i_file_acl
)
437 ea_idebug(inode
, "reading block %llu",
438 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
439 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
443 ea_bdebug(bh
, "b_count=%d, refcount=%d",
444 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
445 if (ext4_xattr_check_block(inode
, bh
)) {
446 EXT4_ERROR_INODE(inode
, "bad block %llu",
447 EXT4_I(inode
)->i_file_acl
);
451 ext4_xattr_cache_insert(ext4_mb_cache
, bh
);
452 error
= ext4_xattr_list_entries(dentry
, BFIRST(bh
), buffer
, buffer_size
);
461 ext4_xattr_ibody_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
463 struct inode
*inode
= d_inode(dentry
);
464 struct ext4_xattr_ibody_header
*header
;
465 struct ext4_inode
*raw_inode
;
466 struct ext4_iloc iloc
;
470 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
472 error
= ext4_get_inode_loc(inode
, &iloc
);
475 raw_inode
= ext4_raw_inode(&iloc
);
476 header
= IHDR(inode
, raw_inode
);
477 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
478 error
= ext4_xattr_check_names(IFIRST(header
), end
, IFIRST(header
));
481 error
= ext4_xattr_list_entries(dentry
, IFIRST(header
),
482 buffer
, buffer_size
);
492 * Copy a list of attribute names into the buffer
493 * provided, or compute the buffer size required.
494 * Buffer is NULL to compute the size of the buffer required.
496 * Returns a negative error number on failure, or the number of bytes
497 * used / required on success.
500 ext4_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
504 down_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
505 ret
= ret2
= ext4_xattr_ibody_list(dentry
, buffer
, buffer_size
);
512 ret
= ext4_xattr_block_list(dentry
, buffer
, buffer_size
);
517 up_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
522 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
525 static void ext4_xattr_update_super_block(handle_t
*handle
,
526 struct super_block
*sb
)
528 if (EXT4_HAS_COMPAT_FEATURE(sb
, EXT4_FEATURE_COMPAT_EXT_ATTR
))
531 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
532 if (ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
) == 0) {
533 EXT4_SET_COMPAT_FEATURE(sb
, EXT4_FEATURE_COMPAT_EXT_ATTR
);
534 ext4_handle_dirty_super(handle
, sb
);
539 * Release the xattr block BH: If the reference count is > 1, decrement it;
540 * otherwise free the block.
543 ext4_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
544 struct buffer_head
*bh
)
546 struct mb_cache_entry
*ce
= NULL
;
548 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
550 ce
= mb_cache_entry_get(ext4_mb_cache
, bh
->b_bdev
, bh
->b_blocknr
);
551 BUFFER_TRACE(bh
, "get_write_access");
552 error
= ext4_journal_get_write_access(handle
, bh
);
557 if (BHDR(bh
)->h_refcount
== cpu_to_le32(1)) {
558 ea_bdebug(bh
, "refcount now=0; freeing");
560 mb_cache_entry_free(ce
);
563 ext4_free_blocks(handle
, inode
, bh
, 0, 1,
564 EXT4_FREE_BLOCKS_METADATA
|
565 EXT4_FREE_BLOCKS_FORGET
);
567 le32_add_cpu(&BHDR(bh
)->h_refcount
, -1);
569 mb_cache_entry_release(ce
);
571 * Beware of this ugliness: Releasing of xattr block references
572 * from different inodes can race and so we have to protect
573 * from a race where someone else frees the block (and releases
574 * its journal_head) before we are done dirtying the buffer. In
575 * nojournal mode this race is harmless and we actually cannot
576 * call ext4_handle_dirty_xattr_block() with locked buffer as
577 * that function can call sync_dirty_buffer() so for that case
578 * we handle the dirtying after unlocking the buffer.
580 if (ext4_handle_valid(handle
))
581 error
= ext4_handle_dirty_xattr_block(handle
, inode
,
584 if (!ext4_handle_valid(handle
))
585 error
= ext4_handle_dirty_xattr_block(handle
, inode
,
588 ext4_handle_sync(handle
);
589 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(inode
->i_sb
), 1));
590 ea_bdebug(bh
, "refcount now=%d; releasing",
591 le32_to_cpu(BHDR(bh
)->h_refcount
));
594 ext4_std_error(inode
->i_sb
, error
);
599 * Find the available free space for EAs. This also returns the total number of
600 * bytes used by EA entries.
602 static size_t ext4_xattr_free_space(struct ext4_xattr_entry
*last
,
603 size_t *min_offs
, void *base
, int *total
)
605 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
606 if (!last
->e_value_block
&& last
->e_value_size
) {
607 size_t offs
= le16_to_cpu(last
->e_value_offs
);
608 if (offs
< *min_offs
)
612 *total
+= EXT4_XATTR_LEN(last
->e_name_len
);
614 return (*min_offs
- ((void *)last
- base
) - sizeof(__u32
));
618 ext4_xattr_set_entry(struct ext4_xattr_info
*i
, struct ext4_xattr_search
*s
)
620 struct ext4_xattr_entry
*last
;
621 size_t free
, min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
623 /* Compute min_offs and last. */
625 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
626 if (!last
->e_value_block
&& last
->e_value_size
) {
627 size_t offs
= le16_to_cpu(last
->e_value_offs
);
632 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
634 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
635 size_t size
= le32_to_cpu(s
->here
->e_value_size
);
636 free
+= EXT4_XATTR_SIZE(size
);
638 free
+= EXT4_XATTR_LEN(name_len
);
641 if (free
< EXT4_XATTR_LEN(name_len
) +
642 EXT4_XATTR_SIZE(i
->value_len
))
646 if (i
->value
&& s
->not_found
) {
647 /* Insert the new name. */
648 size_t size
= EXT4_XATTR_LEN(name_len
);
649 size_t rest
= (void *)last
- (void *)s
->here
+ sizeof(__u32
);
650 memmove((void *)s
->here
+ size
, s
->here
, rest
);
651 memset(s
->here
, 0, size
);
652 s
->here
->e_name_index
= i
->name_index
;
653 s
->here
->e_name_len
= name_len
;
654 memcpy(s
->here
->e_name
, i
->name
, name_len
);
656 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
657 void *first_val
= s
->base
+ min_offs
;
658 size_t offs
= le16_to_cpu(s
->here
->e_value_offs
);
659 void *val
= s
->base
+ offs
;
660 size_t size
= EXT4_XATTR_SIZE(
661 le32_to_cpu(s
->here
->e_value_size
));
663 if (i
->value
&& size
== EXT4_XATTR_SIZE(i
->value_len
)) {
664 /* The old and the new value have the same
665 size. Just replace. */
666 s
->here
->e_value_size
=
667 cpu_to_le32(i
->value_len
);
668 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
669 memset(val
, 0, size
);
671 /* Clear pad bytes first. */
672 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
674 memcpy(val
, i
->value
, i
->value_len
);
679 /* Remove the old value. */
680 memmove(first_val
+ size
, first_val
, val
- first_val
);
681 memset(first_val
, 0, size
);
682 s
->here
->e_value_size
= 0;
683 s
->here
->e_value_offs
= 0;
686 /* Adjust all value offsets. */
688 while (!IS_LAST_ENTRY(last
)) {
689 size_t o
= le16_to_cpu(last
->e_value_offs
);
690 if (!last
->e_value_block
&&
691 last
->e_value_size
&& o
< offs
)
693 cpu_to_le16(o
+ size
);
694 last
= EXT4_XATTR_NEXT(last
);
698 /* Remove the old name. */
699 size_t size
= EXT4_XATTR_LEN(name_len
);
700 last
= ENTRY((void *)last
- size
);
701 memmove(s
->here
, (void *)s
->here
+ size
,
702 (void *)last
- (void *)s
->here
+ sizeof(__u32
));
703 memset(last
, 0, size
);
708 /* Insert the new value. */
709 s
->here
->e_value_size
= cpu_to_le32(i
->value_len
);
711 size_t size
= EXT4_XATTR_SIZE(i
->value_len
);
712 void *val
= s
->base
+ min_offs
- size
;
713 s
->here
->e_value_offs
= cpu_to_le16(min_offs
- size
);
714 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
715 memset(val
, 0, size
);
717 /* Clear the pad bytes first. */
718 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
720 memcpy(val
, i
->value
, i
->value_len
);
727 struct ext4_xattr_block_find
{
728 struct ext4_xattr_search s
;
729 struct buffer_head
*bh
;
733 ext4_xattr_block_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
734 struct ext4_xattr_block_find
*bs
)
736 struct super_block
*sb
= inode
->i_sb
;
739 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
740 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
742 if (EXT4_I(inode
)->i_file_acl
) {
743 /* The inode already has an extended attribute block. */
744 bs
->bh
= sb_bread(sb
, EXT4_I(inode
)->i_file_acl
);
748 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
749 atomic_read(&(bs
->bh
->b_count
)),
750 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
751 if (ext4_xattr_check_block(inode
, bs
->bh
)) {
752 EXT4_ERROR_INODE(inode
, "bad block %llu",
753 EXT4_I(inode
)->i_file_acl
);
757 /* Find the named attribute. */
758 bs
->s
.base
= BHDR(bs
->bh
);
759 bs
->s
.first
= BFIRST(bs
->bh
);
760 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
761 bs
->s
.here
= bs
->s
.first
;
762 error
= ext4_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
763 i
->name
, bs
->bh
->b_size
, 1);
764 if (error
&& error
!= -ENODATA
)
766 bs
->s
.not_found
= error
;
775 ext4_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
776 struct ext4_xattr_info
*i
,
777 struct ext4_xattr_block_find
*bs
)
779 struct super_block
*sb
= inode
->i_sb
;
780 struct buffer_head
*new_bh
= NULL
;
781 struct ext4_xattr_search
*s
= &bs
->s
;
782 struct mb_cache_entry
*ce
= NULL
;
784 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
786 #define header(x) ((struct ext4_xattr_header *)(x))
788 if (i
->value
&& i
->value_len
> sb
->s_blocksize
)
791 ce
= mb_cache_entry_get(ext4_mb_cache
, bs
->bh
->b_bdev
,
793 BUFFER_TRACE(bs
->bh
, "get_write_access");
794 error
= ext4_journal_get_write_access(handle
, bs
->bh
);
799 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
801 mb_cache_entry_free(ce
);
804 ea_bdebug(bs
->bh
, "modifying in-place");
805 error
= ext4_xattr_set_entry(i
, s
);
807 if (!IS_LAST_ENTRY(s
->first
))
808 ext4_xattr_rehash(header(s
->base
),
810 ext4_xattr_cache_insert(ext4_mb_cache
,
813 unlock_buffer(bs
->bh
);
817 error
= ext4_handle_dirty_xattr_block(handle
,
824 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
826 unlock_buffer(bs
->bh
);
828 mb_cache_entry_release(ce
);
831 ea_bdebug(bs
->bh
, "cloning");
832 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
836 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
837 s
->first
= ENTRY(header(s
->base
)+1);
838 header(s
->base
)->h_refcount
= cpu_to_le32(1);
839 s
->here
= ENTRY(s
->base
+ offset
);
840 s
->end
= s
->base
+ bs
->bh
->b_size
;
843 /* Allocate a buffer where we construct the new block. */
844 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
845 /* assert(header == s->base) */
849 header(s
->base
)->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
850 header(s
->base
)->h_blocks
= cpu_to_le32(1);
851 header(s
->base
)->h_refcount
= cpu_to_le32(1);
852 s
->first
= ENTRY(header(s
->base
)+1);
853 s
->here
= ENTRY(header(s
->base
)+1);
854 s
->end
= s
->base
+ sb
->s_blocksize
;
857 error
= ext4_xattr_set_entry(i
, s
);
862 if (!IS_LAST_ENTRY(s
->first
))
863 ext4_xattr_rehash(header(s
->base
), s
->here
);
866 if (!IS_LAST_ENTRY(s
->first
)) {
867 new_bh
= ext4_xattr_cache_find(inode
, header(s
->base
), &ce
);
869 /* We found an identical block in the cache. */
870 if (new_bh
== bs
->bh
)
871 ea_bdebug(new_bh
, "keeping");
873 /* The old block is released after updating
875 error
= dquot_alloc_block(inode
,
876 EXT4_C2B(EXT4_SB(sb
), 1));
879 BUFFER_TRACE(new_bh
, "get_write_access");
880 error
= ext4_journal_get_write_access(handle
,
885 le32_add_cpu(&BHDR(new_bh
)->h_refcount
, 1);
886 ea_bdebug(new_bh
, "reusing; refcount now=%d",
887 le32_to_cpu(BHDR(new_bh
)->h_refcount
));
888 unlock_buffer(new_bh
);
889 error
= ext4_handle_dirty_xattr_block(handle
,
895 mb_cache_entry_release(ce
);
897 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
898 /* We were modifying this block in-place. */
899 ea_bdebug(bs
->bh
, "keeping this block");
903 /* We need to allocate a new block */
904 ext4_fsblk_t goal
, block
;
906 goal
= ext4_group_first_block_no(sb
,
907 EXT4_I(inode
)->i_block_group
);
909 /* non-extent files can't have physical blocks past 2^32 */
910 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
911 goal
= goal
& EXT4_MAX_BLOCK_FILE_PHYS
;
913 block
= ext4_new_meta_blocks(handle
, inode
, goal
, 0,
918 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
919 BUG_ON(block
> EXT4_MAX_BLOCK_FILE_PHYS
);
921 ea_idebug(inode
, "creating block %llu",
922 (unsigned long long)block
);
924 new_bh
= sb_getblk(sb
, block
);
925 if (unlikely(!new_bh
)) {
928 ext4_free_blocks(handle
, inode
, NULL
, block
, 1,
929 EXT4_FREE_BLOCKS_METADATA
);
933 error
= ext4_journal_get_create_access(handle
, new_bh
);
935 unlock_buffer(new_bh
);
939 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
940 set_buffer_uptodate(new_bh
);
941 unlock_buffer(new_bh
);
942 ext4_xattr_cache_insert(ext4_mb_cache
, new_bh
);
943 error
= ext4_handle_dirty_xattr_block(handle
,
950 /* Update the inode. */
951 EXT4_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
953 /* Drop the previous xattr block. */
954 if (bs
->bh
&& bs
->bh
!= new_bh
)
955 ext4_xattr_release_block(handle
, inode
, bs
->bh
);
960 mb_cache_entry_release(ce
);
962 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
968 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(sb
), 1));
972 EXT4_ERROR_INODE(inode
, "bad block %llu",
973 EXT4_I(inode
)->i_file_acl
);
979 int ext4_xattr_ibody_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
980 struct ext4_xattr_ibody_find
*is
)
982 struct ext4_xattr_ibody_header
*header
;
983 struct ext4_inode
*raw_inode
;
986 if (EXT4_I(inode
)->i_extra_isize
== 0)
988 raw_inode
= ext4_raw_inode(&is
->iloc
);
989 header
= IHDR(inode
, raw_inode
);
990 is
->s
.base
= is
->s
.first
= IFIRST(header
);
991 is
->s
.here
= is
->s
.first
;
992 is
->s
.end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
993 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
994 error
= ext4_xattr_check_names(IFIRST(header
), is
->s
.end
,
998 /* Find the named attribute. */
999 error
= ext4_xattr_find_entry(&is
->s
.here
, i
->name_index
,
1000 i
->name
, is
->s
.end
-
1001 (void *)is
->s
.base
, 0);
1002 if (error
&& error
!= -ENODATA
)
1004 is
->s
.not_found
= error
;
1009 int ext4_xattr_ibody_inline_set(handle_t
*handle
, struct inode
*inode
,
1010 struct ext4_xattr_info
*i
,
1011 struct ext4_xattr_ibody_find
*is
)
1013 struct ext4_xattr_ibody_header
*header
;
1014 struct ext4_xattr_search
*s
= &is
->s
;
1017 if (EXT4_I(inode
)->i_extra_isize
== 0)
1019 error
= ext4_xattr_set_entry(i
, s
);
1021 if (error
== -ENOSPC
&&
1022 ext4_has_inline_data(inode
)) {
1023 error
= ext4_try_to_evict_inline_data(handle
, inode
,
1024 EXT4_XATTR_LEN(strlen(i
->name
) +
1025 EXT4_XATTR_SIZE(i
->value_len
)));
1028 error
= ext4_xattr_ibody_find(inode
, i
, is
);
1031 error
= ext4_xattr_set_entry(i
, s
);
1036 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1037 if (!IS_LAST_ENTRY(s
->first
)) {
1038 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1039 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1041 header
->h_magic
= cpu_to_le32(0);
1042 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1047 static int ext4_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
1048 struct ext4_xattr_info
*i
,
1049 struct ext4_xattr_ibody_find
*is
)
1051 struct ext4_xattr_ibody_header
*header
;
1052 struct ext4_xattr_search
*s
= &is
->s
;
1055 if (EXT4_I(inode
)->i_extra_isize
== 0)
1057 error
= ext4_xattr_set_entry(i
, s
);
1060 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1061 if (!IS_LAST_ENTRY(s
->first
)) {
1062 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1063 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1065 header
->h_magic
= cpu_to_le32(0);
1066 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1072 * ext4_xattr_set_handle()
1074 * Create, replace or remove an extended attribute for this inode. Value
1075 * is NULL to remove an existing extended attribute, and non-NULL to
1076 * either replace an existing extended attribute, or create a new extended
1077 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1078 * specify that an extended attribute must exist and must not exist
1079 * previous to the call, respectively.
1081 * Returns 0, or a negative error number on failure.
1084 ext4_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
1085 const char *name
, const void *value
, size_t value_len
,
1088 struct ext4_xattr_info i
= {
1089 .name_index
= name_index
,
1092 .value_len
= value_len
,
1095 struct ext4_xattr_ibody_find is
= {
1096 .s
= { .not_found
= -ENODATA
, },
1098 struct ext4_xattr_block_find bs
= {
1099 .s
= { .not_found
= -ENODATA
, },
1101 unsigned long no_expand
;
1106 if (strlen(name
) > 255)
1108 down_write(&EXT4_I(inode
)->xattr_sem
);
1109 no_expand
= ext4_test_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1110 ext4_set_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1112 error
= ext4_reserve_inode_write(handle
, inode
, &is
.iloc
);
1116 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
)) {
1117 struct ext4_inode
*raw_inode
= ext4_raw_inode(&is
.iloc
);
1118 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
1119 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
1122 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
1126 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1129 if (is
.s
.not_found
&& bs
.s
.not_found
) {
1131 if (flags
& XATTR_REPLACE
)
1138 if (flags
& XATTR_CREATE
)
1142 if (!is
.s
.not_found
)
1143 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1144 else if (!bs
.s
.not_found
)
1145 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1147 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1148 if (!error
&& !bs
.s
.not_found
) {
1150 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1151 } else if (error
== -ENOSPC
) {
1152 if (EXT4_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
1153 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1157 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1160 if (!is
.s
.not_found
) {
1162 error
= ext4_xattr_ibody_set(handle
, inode
, &i
,
1168 ext4_xattr_update_super_block(handle
, inode
->i_sb
);
1169 inode
->i_ctime
= ext4_current_time(inode
);
1171 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1172 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
1174 * The bh is consumed by ext4_mark_iloc_dirty, even with
1179 ext4_handle_sync(handle
);
1186 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1187 up_write(&EXT4_I(inode
)->xattr_sem
);
1194 * Like ext4_xattr_set_handle, but start from an inode. This extended
1195 * attribute modification is a filesystem transaction by itself.
1197 * Returns 0, or a negative error number on failure.
1200 ext4_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
1201 const void *value
, size_t value_len
, int flags
)
1204 int error
, retries
= 0;
1205 int credits
= ext4_jbd2_credits_xattr(inode
);
1208 handle
= ext4_journal_start(inode
, EXT4_HT_XATTR
, credits
);
1209 if (IS_ERR(handle
)) {
1210 error
= PTR_ERR(handle
);
1214 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, name
,
1215 value
, value_len
, flags
);
1216 error2
= ext4_journal_stop(handle
);
1217 if (error
== -ENOSPC
&&
1218 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1228 * Shift the EA entries in the inode to create space for the increased
1231 static void ext4_xattr_shift_entries(struct ext4_xattr_entry
*entry
,
1232 int value_offs_shift
, void *to
,
1233 void *from
, size_t n
, int blocksize
)
1235 struct ext4_xattr_entry
*last
= entry
;
1238 /* Adjust the value offsets of the entries */
1239 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1240 if (!last
->e_value_block
&& last
->e_value_size
) {
1241 new_offs
= le16_to_cpu(last
->e_value_offs
) +
1243 BUG_ON(new_offs
+ le32_to_cpu(last
->e_value_size
)
1245 last
->e_value_offs
= cpu_to_le16(new_offs
);
1248 /* Shift the entries by n bytes */
1249 memmove(to
, from
, n
);
1253 * Expand an inode by new_extra_isize bytes when EAs are present.
1254 * Returns 0 on success or negative error number on failure.
1256 int ext4_expand_extra_isize_ea(struct inode
*inode
, int new_extra_isize
,
1257 struct ext4_inode
*raw_inode
, handle_t
*handle
)
1259 struct ext4_xattr_ibody_header
*header
;
1260 struct ext4_xattr_entry
*entry
, *last
, *first
;
1261 struct buffer_head
*bh
= NULL
;
1262 struct ext4_xattr_ibody_find
*is
= NULL
;
1263 struct ext4_xattr_block_find
*bs
= NULL
;
1264 char *buffer
= NULL
, *b_entry_name
= NULL
;
1265 size_t min_offs
, free
;
1267 void *base
, *start
, *end
;
1268 int extra_isize
= 0, error
= 0, tried_min_extra_isize
= 0;
1269 int s_min_extra_isize
= le16_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_min_extra_isize
);
1271 down_write(&EXT4_I(inode
)->xattr_sem
);
1273 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
) {
1274 up_write(&EXT4_I(inode
)->xattr_sem
);
1278 header
= IHDR(inode
, raw_inode
);
1279 entry
= IFIRST(header
);
1282 * Check if enough free space is available in the inode to shift the
1283 * entries ahead by new_extra_isize.
1286 base
= start
= entry
;
1287 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1288 min_offs
= end
- base
;
1290 total_ino
= sizeof(struct ext4_xattr_ibody_header
);
1292 free
= ext4_xattr_free_space(last
, &min_offs
, base
, &total_ino
);
1293 if (free
>= new_extra_isize
) {
1294 entry
= IFIRST(header
);
1295 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
1296 - new_extra_isize
, (void *)raw_inode
+
1297 EXT4_GOOD_OLD_INODE_SIZE
+ new_extra_isize
,
1298 (void *)header
, total_ino
,
1299 inode
->i_sb
->s_blocksize
);
1300 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
1306 * Enough free space isn't available in the inode, check if
1307 * EA block can hold new_extra_isize bytes.
1309 if (EXT4_I(inode
)->i_file_acl
) {
1310 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1314 if (ext4_xattr_check_block(inode
, bh
)) {
1315 EXT4_ERROR_INODE(inode
, "bad block %llu",
1316 EXT4_I(inode
)->i_file_acl
);
1322 end
= bh
->b_data
+ bh
->b_size
;
1323 min_offs
= end
- base
;
1324 free
= ext4_xattr_free_space(first
, &min_offs
, base
, NULL
);
1325 if (free
< new_extra_isize
) {
1326 if (!tried_min_extra_isize
&& s_min_extra_isize
) {
1327 tried_min_extra_isize
++;
1328 new_extra_isize
= s_min_extra_isize
;
1336 free
= inode
->i_sb
->s_blocksize
;
1339 while (new_extra_isize
> 0) {
1340 size_t offs
, size
, entry_size
;
1341 struct ext4_xattr_entry
*small_entry
= NULL
;
1342 struct ext4_xattr_info i
= {
1346 unsigned int total_size
; /* EA entry size + value size */
1347 unsigned int shift_bytes
; /* No. of bytes to shift EAs by? */
1348 unsigned int min_total_size
= ~0U;
1350 is
= kzalloc(sizeof(struct ext4_xattr_ibody_find
), GFP_NOFS
);
1351 bs
= kzalloc(sizeof(struct ext4_xattr_block_find
), GFP_NOFS
);
1357 is
->s
.not_found
= -ENODATA
;
1358 bs
->s
.not_found
= -ENODATA
;
1362 last
= IFIRST(header
);
1363 /* Find the entry best suited to be pushed into EA block */
1365 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1367 EXT4_XATTR_SIZE(le32_to_cpu(last
->e_value_size
)) +
1368 EXT4_XATTR_LEN(last
->e_name_len
);
1369 if (total_size
<= free
&& total_size
< min_total_size
) {
1370 if (total_size
< new_extra_isize
) {
1374 min_total_size
= total_size
;
1379 if (entry
== NULL
) {
1381 entry
= small_entry
;
1383 if (!tried_min_extra_isize
&&
1384 s_min_extra_isize
) {
1385 tried_min_extra_isize
++;
1386 new_extra_isize
= s_min_extra_isize
;
1387 kfree(is
); is
= NULL
;
1388 kfree(bs
); bs
= NULL
;
1396 offs
= le16_to_cpu(entry
->e_value_offs
);
1397 size
= le32_to_cpu(entry
->e_value_size
);
1398 entry_size
= EXT4_XATTR_LEN(entry
->e_name_len
);
1399 i
.name_index
= entry
->e_name_index
,
1400 buffer
= kmalloc(EXT4_XATTR_SIZE(size
), GFP_NOFS
);
1401 b_entry_name
= kmalloc(entry
->e_name_len
+ 1, GFP_NOFS
);
1402 if (!buffer
|| !b_entry_name
) {
1406 /* Save the entry name and the entry value */
1407 memcpy(buffer
, (void *)IFIRST(header
) + offs
,
1408 EXT4_XATTR_SIZE(size
));
1409 memcpy(b_entry_name
, entry
->e_name
, entry
->e_name_len
);
1410 b_entry_name
[entry
->e_name_len
] = '\0';
1411 i
.name
= b_entry_name
;
1413 error
= ext4_get_inode_loc(inode
, &is
->iloc
);
1417 error
= ext4_xattr_ibody_find(inode
, &i
, is
);
1421 /* Remove the chosen entry from the inode */
1422 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, is
);
1426 entry
= IFIRST(header
);
1427 if (entry_size
+ EXT4_XATTR_SIZE(size
) >= new_extra_isize
)
1428 shift_bytes
= new_extra_isize
;
1430 shift_bytes
= entry_size
+ size
;
1431 /* Adjust the offsets and shift the remaining entries ahead */
1432 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
-
1433 shift_bytes
, (void *)raw_inode
+
1434 EXT4_GOOD_OLD_INODE_SIZE
+ extra_isize
+ shift_bytes
,
1435 (void *)header
, total_ino
- entry_size
,
1436 inode
->i_sb
->s_blocksize
);
1438 extra_isize
+= shift_bytes
;
1439 new_extra_isize
-= shift_bytes
;
1440 EXT4_I(inode
)->i_extra_isize
= extra_isize
;
1442 i
.name
= b_entry_name
;
1445 error
= ext4_xattr_block_find(inode
, &i
, bs
);
1449 /* Add entry which was removed from the inode into the block */
1450 error
= ext4_xattr_block_set(handle
, inode
, &i
, bs
);
1453 kfree(b_entry_name
);
1455 b_entry_name
= NULL
;
1457 brelse(is
->iloc
.bh
);
1462 up_write(&EXT4_I(inode
)->xattr_sem
);
1466 kfree(b_entry_name
);
1469 brelse(is
->iloc
.bh
);
1473 up_write(&EXT4_I(inode
)->xattr_sem
);
1480 * ext4_xattr_delete_inode()
1482 * Free extended attribute resources associated with this inode. This
1483 * is called immediately before an inode is freed. We have exclusive
1484 * access to the inode.
1487 ext4_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
)
1489 struct buffer_head
*bh
= NULL
;
1491 if (!EXT4_I(inode
)->i_file_acl
)
1493 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1495 EXT4_ERROR_INODE(inode
, "block %llu read error",
1496 EXT4_I(inode
)->i_file_acl
);
1499 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
1500 BHDR(bh
)->h_blocks
!= cpu_to_le32(1)) {
1501 EXT4_ERROR_INODE(inode
, "bad block %llu",
1502 EXT4_I(inode
)->i_file_acl
);
1505 ext4_xattr_release_block(handle
, inode
, bh
);
1506 EXT4_I(inode
)->i_file_acl
= 0;
1513 * ext4_xattr_put_super()
1515 * This is called when a file system is unmounted.
1518 ext4_xattr_put_super(struct super_block
*sb
)
1520 mb_cache_shrink(sb
->s_bdev
);
1524 * ext4_xattr_cache_insert()
1526 * Create a new entry in the extended attribute cache, and insert
1527 * it unless such an entry is already in the cache.
1529 * Returns 0, or a negative error number on failure.
1532 ext4_xattr_cache_insert(struct mb_cache
*ext4_mb_cache
, struct buffer_head
*bh
)
1534 __u32 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
1535 struct mb_cache_entry
*ce
;
1538 ce
= mb_cache_entry_alloc(ext4_mb_cache
, GFP_NOFS
);
1540 ea_bdebug(bh
, "out of memory");
1543 error
= mb_cache_entry_insert(ce
, bh
->b_bdev
, bh
->b_blocknr
, hash
);
1545 mb_cache_entry_free(ce
);
1546 if (error
== -EBUSY
) {
1547 ea_bdebug(bh
, "already in cache");
1551 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
1552 mb_cache_entry_release(ce
);
1559 * Compare two extended attribute blocks for equality.
1561 * Returns 0 if the blocks are equal, 1 if they differ, and
1562 * a negative error number on errors.
1565 ext4_xattr_cmp(struct ext4_xattr_header
*header1
,
1566 struct ext4_xattr_header
*header2
)
1568 struct ext4_xattr_entry
*entry1
, *entry2
;
1570 entry1
= ENTRY(header1
+1);
1571 entry2
= ENTRY(header2
+1);
1572 while (!IS_LAST_ENTRY(entry1
)) {
1573 if (IS_LAST_ENTRY(entry2
))
1575 if (entry1
->e_hash
!= entry2
->e_hash
||
1576 entry1
->e_name_index
!= entry2
->e_name_index
||
1577 entry1
->e_name_len
!= entry2
->e_name_len
||
1578 entry1
->e_value_size
!= entry2
->e_value_size
||
1579 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
1581 if (entry1
->e_value_block
!= 0 || entry2
->e_value_block
!= 0)
1583 if (memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
1584 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
1585 le32_to_cpu(entry1
->e_value_size
)))
1588 entry1
= EXT4_XATTR_NEXT(entry1
);
1589 entry2
= EXT4_XATTR_NEXT(entry2
);
1591 if (!IS_LAST_ENTRY(entry2
))
1597 * ext4_xattr_cache_find()
1599 * Find an identical extended attribute block.
1601 * Returns a pointer to the block found, or NULL if such a block was
1602 * not found or an error occurred.
1604 static struct buffer_head
*
1605 ext4_xattr_cache_find(struct inode
*inode
, struct ext4_xattr_header
*header
,
1606 struct mb_cache_entry
**pce
)
1608 __u32 hash
= le32_to_cpu(header
->h_hash
);
1609 struct mb_cache_entry
*ce
;
1610 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
1612 if (!header
->h_hash
)
1613 return NULL
; /* never share */
1614 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
1616 ce
= mb_cache_entry_find_first(ext4_mb_cache
, inode
->i_sb
->s_bdev
,
1619 struct buffer_head
*bh
;
1622 if (PTR_ERR(ce
) == -EAGAIN
)
1626 bh
= sb_bread(inode
->i_sb
, ce
->e_block
);
1628 EXT4_ERROR_INODE(inode
, "block %lu read error",
1629 (unsigned long) ce
->e_block
);
1630 } else if (le32_to_cpu(BHDR(bh
)->h_refcount
) >=
1631 EXT4_XATTR_REFCOUNT_MAX
) {
1632 ea_idebug(inode
, "block %lu refcount %d>=%d",
1633 (unsigned long) ce
->e_block
,
1634 le32_to_cpu(BHDR(bh
)->h_refcount
),
1635 EXT4_XATTR_REFCOUNT_MAX
);
1636 } else if (ext4_xattr_cmp(header
, BHDR(bh
)) == 0) {
1641 ce
= mb_cache_entry_find_next(ce
, inode
->i_sb
->s_bdev
, hash
);
1646 #define NAME_HASH_SHIFT 5
1647 #define VALUE_HASH_SHIFT 16
1650 * ext4_xattr_hash_entry()
1652 * Compute the hash of an extended attribute.
1654 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header
*header
,
1655 struct ext4_xattr_entry
*entry
)
1658 char *name
= entry
->e_name
;
1661 for (n
= 0; n
< entry
->e_name_len
; n
++) {
1662 hash
= (hash
<< NAME_HASH_SHIFT
) ^
1663 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
1667 if (entry
->e_value_block
== 0 && entry
->e_value_size
!= 0) {
1668 __le32
*value
= (__le32
*)((char *)header
+
1669 le16_to_cpu(entry
->e_value_offs
));
1670 for (n
= (le32_to_cpu(entry
->e_value_size
) +
1671 EXT4_XATTR_ROUND
) >> EXT4_XATTR_PAD_BITS
; n
; n
--) {
1672 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
1673 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
1674 le32_to_cpu(*value
++);
1677 entry
->e_hash
= cpu_to_le32(hash
);
1680 #undef NAME_HASH_SHIFT
1681 #undef VALUE_HASH_SHIFT
1683 #define BLOCK_HASH_SHIFT 16
1686 * ext4_xattr_rehash()
1688 * Re-compute the extended attribute hash value after an entry has changed.
1690 static void ext4_xattr_rehash(struct ext4_xattr_header
*header
,
1691 struct ext4_xattr_entry
*entry
)
1693 struct ext4_xattr_entry
*here
;
1696 ext4_xattr_hash_entry(header
, entry
);
1697 here
= ENTRY(header
+1);
1698 while (!IS_LAST_ENTRY(here
)) {
1699 if (!here
->e_hash
) {
1700 /* Block is not shared if an entry's hash value == 0 */
1704 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
1705 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
1706 le32_to_cpu(here
->e_hash
);
1707 here
= EXT4_XATTR_NEXT(here
);
1709 header
->h_hash
= cpu_to_le32(hash
);
1712 #undef BLOCK_HASH_SHIFT
1714 #define HASH_BUCKET_BITS 10
1717 ext4_xattr_create_cache(char *name
)
1719 return mb_cache_create(name
, HASH_BUCKET_BITS
);
1722 void ext4_xattr_destroy_cache(struct mb_cache
*cache
)
1725 mb_cache_destroy(cache
);