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
);
126 __le64 dsk_block_nr
= cpu_to_le64(block_nr
);
127 __u32 dummy_csum
= 0;
128 int offset
= offsetof(struct ext4_xattr_header
, h_checksum
);
130 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&dsk_block_nr
,
131 sizeof(dsk_block_nr
));
132 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
, offset
);
133 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
, sizeof(dummy_csum
));
134 offset
+= sizeof(dummy_csum
);
135 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
+ offset
,
136 EXT4_BLOCK_SIZE(inode
->i_sb
) - offset
);
138 return cpu_to_le32(csum
);
141 static int ext4_xattr_block_csum_verify(struct inode
*inode
,
143 struct ext4_xattr_header
*hdr
)
145 if (ext4_has_metadata_csum(inode
->i_sb
) &&
146 (hdr
->h_checksum
!= ext4_xattr_block_csum(inode
, block_nr
, hdr
)))
151 static void ext4_xattr_block_csum_set(struct inode
*inode
,
153 struct ext4_xattr_header
*hdr
)
155 if (!ext4_has_metadata_csum(inode
->i_sb
))
158 hdr
->h_checksum
= ext4_xattr_block_csum(inode
, block_nr
, hdr
);
161 static inline int ext4_handle_dirty_xattr_block(handle_t
*handle
,
163 struct buffer_head
*bh
)
165 ext4_xattr_block_csum_set(inode
, bh
->b_blocknr
, BHDR(bh
));
166 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
169 static inline const struct xattr_handler
*
170 ext4_xattr_handler(int name_index
)
172 const struct xattr_handler
*handler
= NULL
;
174 if (name_index
> 0 && name_index
< ARRAY_SIZE(ext4_xattr_handler_map
))
175 handler
= ext4_xattr_handler_map
[name_index
];
180 * Inode operation listxattr()
182 * d_inode(dentry)->i_mutex: don't care
185 ext4_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
187 return ext4_xattr_list(dentry
, buffer
, size
);
191 ext4_xattr_check_names(struct ext4_xattr_entry
*entry
, void *end
,
194 struct ext4_xattr_entry
*e
= entry
;
196 while (!IS_LAST_ENTRY(e
)) {
197 struct ext4_xattr_entry
*next
= EXT4_XATTR_NEXT(e
);
198 if ((void *)next
>= end
)
199 return -EFSCORRUPTED
;
203 while (!IS_LAST_ENTRY(entry
)) {
204 if (entry
->e_value_size
!= 0 &&
205 (value_start
+ le16_to_cpu(entry
->e_value_offs
) <
206 (void *)e
+ sizeof(__u32
) ||
207 value_start
+ le16_to_cpu(entry
->e_value_offs
) +
208 le32_to_cpu(entry
->e_value_size
) > end
))
209 return -EFSCORRUPTED
;
210 entry
= EXT4_XATTR_NEXT(entry
);
217 ext4_xattr_check_block(struct inode
*inode
, struct buffer_head
*bh
)
221 if (buffer_verified(bh
))
224 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
225 BHDR(bh
)->h_blocks
!= cpu_to_le32(1))
226 return -EFSCORRUPTED
;
227 if (!ext4_xattr_block_csum_verify(inode
, bh
->b_blocknr
, BHDR(bh
)))
229 error
= ext4_xattr_check_names(BFIRST(bh
), bh
->b_data
+ bh
->b_size
,
232 set_buffer_verified(bh
);
237 __xattr_check_inode(struct inode
*inode
, struct ext4_xattr_ibody_header
*header
,
238 void *end
, const char *function
, unsigned int line
)
240 struct ext4_xattr_entry
*entry
= IFIRST(header
);
241 int error
= -EFSCORRUPTED
;
243 if (((void *) header
>= end
) ||
244 (header
->h_magic
!= le32_to_cpu(EXT4_XATTR_MAGIC
)))
246 error
= ext4_xattr_check_names(entry
, end
, entry
);
249 __ext4_error_inode(inode
, function
, line
, 0,
250 "corrupted in-inode xattr");
254 #define xattr_check_inode(inode, header, end) \
255 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
258 ext4_xattr_check_entry(struct ext4_xattr_entry
*entry
, size_t size
)
260 size_t value_size
= le32_to_cpu(entry
->e_value_size
);
262 if (entry
->e_value_block
!= 0 || value_size
> size
||
263 le16_to_cpu(entry
->e_value_offs
) + value_size
> size
)
264 return -EFSCORRUPTED
;
269 ext4_xattr_find_entry(struct ext4_xattr_entry
**pentry
, int name_index
,
270 const char *name
, size_t size
, int sorted
)
272 struct ext4_xattr_entry
*entry
;
278 name_len
= strlen(name
);
280 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
281 cmp
= name_index
- entry
->e_name_index
;
283 cmp
= name_len
- entry
->e_name_len
;
285 cmp
= memcmp(name
, entry
->e_name
, name_len
);
286 if (cmp
<= 0 && (sorted
|| cmp
== 0))
290 if (!cmp
&& ext4_xattr_check_entry(entry
, size
))
291 return -EFSCORRUPTED
;
292 return cmp
? -ENODATA
: 0;
296 ext4_xattr_block_get(struct inode
*inode
, int name_index
, const char *name
,
297 void *buffer
, size_t buffer_size
)
299 struct buffer_head
*bh
= NULL
;
300 struct ext4_xattr_entry
*entry
;
303 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
305 ea_idebug(inode
, "name=%d.%s, buffer=%p, buffer_size=%ld",
306 name_index
, name
, buffer
, (long)buffer_size
);
309 if (!EXT4_I(inode
)->i_file_acl
)
311 ea_idebug(inode
, "reading block %llu",
312 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
313 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
316 ea_bdebug(bh
, "b_count=%d, refcount=%d",
317 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
318 if (ext4_xattr_check_block(inode
, bh
)) {
320 EXT4_ERROR_INODE(inode
, "bad block %llu",
321 EXT4_I(inode
)->i_file_acl
);
322 error
= -EFSCORRUPTED
;
325 ext4_xattr_cache_insert(ext4_mb_cache
, bh
);
327 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, bh
->b_size
, 1);
328 if (error
== -EFSCORRUPTED
)
332 size
= le32_to_cpu(entry
->e_value_size
);
335 if (size
> buffer_size
)
337 memcpy(buffer
, bh
->b_data
+ le16_to_cpu(entry
->e_value_offs
),
348 ext4_xattr_ibody_get(struct inode
*inode
, int name_index
, const char *name
,
349 void *buffer
, size_t buffer_size
)
351 struct ext4_xattr_ibody_header
*header
;
352 struct ext4_xattr_entry
*entry
;
353 struct ext4_inode
*raw_inode
;
354 struct ext4_iloc iloc
;
359 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
361 error
= ext4_get_inode_loc(inode
, &iloc
);
364 raw_inode
= ext4_raw_inode(&iloc
);
365 header
= IHDR(inode
, raw_inode
);
366 entry
= IFIRST(header
);
367 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
368 error
= xattr_check_inode(inode
, header
, end
);
371 error
= ext4_xattr_find_entry(&entry
, name_index
, name
,
372 end
- (void *)entry
, 0);
375 size
= le32_to_cpu(entry
->e_value_size
);
378 if (size
> buffer_size
)
380 memcpy(buffer
, (void *)IFIRST(header
) +
381 le16_to_cpu(entry
->e_value_offs
), size
);
393 * Copy an extended attribute into the buffer
394 * provided, or compute the buffer size required.
395 * Buffer is NULL to compute the size of the buffer required.
397 * Returns a negative error number on failure, or the number of bytes
398 * used / required on success.
401 ext4_xattr_get(struct inode
*inode
, int name_index
, const char *name
,
402 void *buffer
, size_t buffer_size
)
406 if (strlen(name
) > 255)
409 down_read(&EXT4_I(inode
)->xattr_sem
);
410 error
= ext4_xattr_ibody_get(inode
, name_index
, name
, buffer
,
412 if (error
== -ENODATA
)
413 error
= ext4_xattr_block_get(inode
, name_index
, name
, buffer
,
415 up_read(&EXT4_I(inode
)->xattr_sem
);
420 ext4_xattr_list_entries(struct dentry
*dentry
, struct ext4_xattr_entry
*entry
,
421 char *buffer
, size_t buffer_size
)
423 size_t rest
= buffer_size
;
425 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
426 const struct xattr_handler
*handler
=
427 ext4_xattr_handler(entry
->e_name_index
);
430 size_t size
= handler
->list(handler
, dentry
, buffer
,
441 return buffer_size
- rest
;
445 ext4_xattr_block_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
447 struct inode
*inode
= d_inode(dentry
);
448 struct buffer_head
*bh
= NULL
;
450 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
452 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
453 buffer
, (long)buffer_size
);
456 if (!EXT4_I(inode
)->i_file_acl
)
458 ea_idebug(inode
, "reading block %llu",
459 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
460 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
464 ea_bdebug(bh
, "b_count=%d, refcount=%d",
465 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
466 if (ext4_xattr_check_block(inode
, bh
)) {
467 EXT4_ERROR_INODE(inode
, "bad block %llu",
468 EXT4_I(inode
)->i_file_acl
);
469 error
= -EFSCORRUPTED
;
472 ext4_xattr_cache_insert(ext4_mb_cache
, bh
);
473 error
= ext4_xattr_list_entries(dentry
, BFIRST(bh
), buffer
, buffer_size
);
482 ext4_xattr_ibody_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
484 struct inode
*inode
= d_inode(dentry
);
485 struct ext4_xattr_ibody_header
*header
;
486 struct ext4_inode
*raw_inode
;
487 struct ext4_iloc iloc
;
491 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
493 error
= ext4_get_inode_loc(inode
, &iloc
);
496 raw_inode
= ext4_raw_inode(&iloc
);
497 header
= IHDR(inode
, raw_inode
);
498 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
499 error
= xattr_check_inode(inode
, header
, end
);
502 error
= ext4_xattr_list_entries(dentry
, IFIRST(header
),
503 buffer
, buffer_size
);
513 * Copy a list of attribute names into the buffer
514 * provided, or compute the buffer size required.
515 * Buffer is NULL to compute the size of the buffer required.
517 * Returns a negative error number on failure, or the number of bytes
518 * used / required on success.
521 ext4_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
525 down_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
526 ret
= ret2
= ext4_xattr_ibody_list(dentry
, buffer
, buffer_size
);
533 ret
= ext4_xattr_block_list(dentry
, buffer
, buffer_size
);
538 up_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
543 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
546 static void ext4_xattr_update_super_block(handle_t
*handle
,
547 struct super_block
*sb
)
549 if (ext4_has_feature_xattr(sb
))
552 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
553 if (ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
) == 0) {
554 ext4_set_feature_xattr(sb
);
555 ext4_handle_dirty_super(handle
, sb
);
560 * Release the xattr block BH: If the reference count is > 1, decrement it;
561 * otherwise free the block.
564 ext4_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
565 struct buffer_head
*bh
)
567 struct mb_cache_entry
*ce
= NULL
;
569 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
571 ce
= mb_cache_entry_get(ext4_mb_cache
, bh
->b_bdev
, bh
->b_blocknr
);
572 BUFFER_TRACE(bh
, "get_write_access");
573 error
= ext4_journal_get_write_access(handle
, bh
);
578 if (BHDR(bh
)->h_refcount
== cpu_to_le32(1)) {
579 ea_bdebug(bh
, "refcount now=0; freeing");
581 mb_cache_entry_free(ce
);
584 ext4_free_blocks(handle
, inode
, bh
, 0, 1,
585 EXT4_FREE_BLOCKS_METADATA
|
586 EXT4_FREE_BLOCKS_FORGET
);
588 le32_add_cpu(&BHDR(bh
)->h_refcount
, -1);
590 mb_cache_entry_release(ce
);
592 * Beware of this ugliness: Releasing of xattr block references
593 * from different inodes can race and so we have to protect
594 * from a race where someone else frees the block (and releases
595 * its journal_head) before we are done dirtying the buffer. In
596 * nojournal mode this race is harmless and we actually cannot
597 * call ext4_handle_dirty_xattr_block() with locked buffer as
598 * that function can call sync_dirty_buffer() so for that case
599 * we handle the dirtying after unlocking the buffer.
601 if (ext4_handle_valid(handle
))
602 error
= ext4_handle_dirty_xattr_block(handle
, inode
,
605 if (!ext4_handle_valid(handle
))
606 error
= ext4_handle_dirty_xattr_block(handle
, inode
,
609 ext4_handle_sync(handle
);
610 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(inode
->i_sb
), 1));
611 ea_bdebug(bh
, "refcount now=%d; releasing",
612 le32_to_cpu(BHDR(bh
)->h_refcount
));
615 ext4_std_error(inode
->i_sb
, error
);
620 * Find the available free space for EAs. This also returns the total number of
621 * bytes used by EA entries.
623 static size_t ext4_xattr_free_space(struct ext4_xattr_entry
*last
,
624 size_t *min_offs
, void *base
, int *total
)
626 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
627 if (!last
->e_value_block
&& last
->e_value_size
) {
628 size_t offs
= le16_to_cpu(last
->e_value_offs
);
629 if (offs
< *min_offs
)
633 *total
+= EXT4_XATTR_LEN(last
->e_name_len
);
635 return (*min_offs
- ((void *)last
- base
) - sizeof(__u32
));
639 ext4_xattr_set_entry(struct ext4_xattr_info
*i
, struct ext4_xattr_search
*s
)
641 struct ext4_xattr_entry
*last
;
642 size_t free
, min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
644 /* Compute min_offs and last. */
646 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
647 if (!last
->e_value_block
&& last
->e_value_size
) {
648 size_t offs
= le16_to_cpu(last
->e_value_offs
);
653 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
655 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
656 size_t size
= le32_to_cpu(s
->here
->e_value_size
);
657 free
+= EXT4_XATTR_SIZE(size
);
659 free
+= EXT4_XATTR_LEN(name_len
);
662 if (free
< EXT4_XATTR_LEN(name_len
) +
663 EXT4_XATTR_SIZE(i
->value_len
))
667 if (i
->value
&& s
->not_found
) {
668 /* Insert the new name. */
669 size_t size
= EXT4_XATTR_LEN(name_len
);
670 size_t rest
= (void *)last
- (void *)s
->here
+ sizeof(__u32
);
671 memmove((void *)s
->here
+ size
, s
->here
, rest
);
672 memset(s
->here
, 0, size
);
673 s
->here
->e_name_index
= i
->name_index
;
674 s
->here
->e_name_len
= name_len
;
675 memcpy(s
->here
->e_name
, i
->name
, name_len
);
677 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
678 void *first_val
= s
->base
+ min_offs
;
679 size_t offs
= le16_to_cpu(s
->here
->e_value_offs
);
680 void *val
= s
->base
+ offs
;
681 size_t size
= EXT4_XATTR_SIZE(
682 le32_to_cpu(s
->here
->e_value_size
));
684 if (i
->value
&& size
== EXT4_XATTR_SIZE(i
->value_len
)) {
685 /* The old and the new value have the same
686 size. Just replace. */
687 s
->here
->e_value_size
=
688 cpu_to_le32(i
->value_len
);
689 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
690 memset(val
, 0, size
);
692 /* Clear pad bytes first. */
693 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
695 memcpy(val
, i
->value
, i
->value_len
);
700 /* Remove the old value. */
701 memmove(first_val
+ size
, first_val
, val
- first_val
);
702 memset(first_val
, 0, size
);
703 s
->here
->e_value_size
= 0;
704 s
->here
->e_value_offs
= 0;
707 /* Adjust all value offsets. */
709 while (!IS_LAST_ENTRY(last
)) {
710 size_t o
= le16_to_cpu(last
->e_value_offs
);
711 if (!last
->e_value_block
&&
712 last
->e_value_size
&& o
< offs
)
714 cpu_to_le16(o
+ size
);
715 last
= EXT4_XATTR_NEXT(last
);
719 /* Remove the old name. */
720 size_t size
= EXT4_XATTR_LEN(name_len
);
721 last
= ENTRY((void *)last
- size
);
722 memmove(s
->here
, (void *)s
->here
+ size
,
723 (void *)last
- (void *)s
->here
+ sizeof(__u32
));
724 memset(last
, 0, size
);
729 /* Insert the new value. */
730 s
->here
->e_value_size
= cpu_to_le32(i
->value_len
);
732 size_t size
= EXT4_XATTR_SIZE(i
->value_len
);
733 void *val
= s
->base
+ min_offs
- size
;
734 s
->here
->e_value_offs
= cpu_to_le16(min_offs
- size
);
735 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
736 memset(val
, 0, size
);
738 /* Clear the pad bytes first. */
739 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
741 memcpy(val
, i
->value
, i
->value_len
);
748 struct ext4_xattr_block_find
{
749 struct ext4_xattr_search s
;
750 struct buffer_head
*bh
;
754 ext4_xattr_block_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
755 struct ext4_xattr_block_find
*bs
)
757 struct super_block
*sb
= inode
->i_sb
;
760 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
761 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
763 if (EXT4_I(inode
)->i_file_acl
) {
764 /* The inode already has an extended attribute block. */
765 bs
->bh
= sb_bread(sb
, EXT4_I(inode
)->i_file_acl
);
769 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
770 atomic_read(&(bs
->bh
->b_count
)),
771 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
772 if (ext4_xattr_check_block(inode
, bs
->bh
)) {
773 EXT4_ERROR_INODE(inode
, "bad block %llu",
774 EXT4_I(inode
)->i_file_acl
);
775 error
= -EFSCORRUPTED
;
778 /* Find the named attribute. */
779 bs
->s
.base
= BHDR(bs
->bh
);
780 bs
->s
.first
= BFIRST(bs
->bh
);
781 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
782 bs
->s
.here
= bs
->s
.first
;
783 error
= ext4_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
784 i
->name
, bs
->bh
->b_size
, 1);
785 if (error
&& error
!= -ENODATA
)
787 bs
->s
.not_found
= error
;
796 ext4_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
797 struct ext4_xattr_info
*i
,
798 struct ext4_xattr_block_find
*bs
)
800 struct super_block
*sb
= inode
->i_sb
;
801 struct buffer_head
*new_bh
= NULL
;
802 struct ext4_xattr_search
*s
= &bs
->s
;
803 struct mb_cache_entry
*ce
= NULL
;
805 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
807 #define header(x) ((struct ext4_xattr_header *)(x))
809 if (i
->value
&& i
->value_len
> sb
->s_blocksize
)
812 ce
= mb_cache_entry_get(ext4_mb_cache
, bs
->bh
->b_bdev
,
814 BUFFER_TRACE(bs
->bh
, "get_write_access");
815 error
= ext4_journal_get_write_access(handle
, bs
->bh
);
820 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
822 mb_cache_entry_free(ce
);
825 ea_bdebug(bs
->bh
, "modifying in-place");
826 error
= ext4_xattr_set_entry(i
, s
);
828 if (!IS_LAST_ENTRY(s
->first
))
829 ext4_xattr_rehash(header(s
->base
),
831 ext4_xattr_cache_insert(ext4_mb_cache
,
834 unlock_buffer(bs
->bh
);
835 if (error
== -EFSCORRUPTED
)
838 error
= ext4_handle_dirty_xattr_block(handle
,
845 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
847 unlock_buffer(bs
->bh
);
849 mb_cache_entry_release(ce
);
852 ea_bdebug(bs
->bh
, "cloning");
853 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
857 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
858 s
->first
= ENTRY(header(s
->base
)+1);
859 header(s
->base
)->h_refcount
= cpu_to_le32(1);
860 s
->here
= ENTRY(s
->base
+ offset
);
861 s
->end
= s
->base
+ bs
->bh
->b_size
;
864 /* Allocate a buffer where we construct the new block. */
865 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
866 /* assert(header == s->base) */
870 header(s
->base
)->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
871 header(s
->base
)->h_blocks
= cpu_to_le32(1);
872 header(s
->base
)->h_refcount
= cpu_to_le32(1);
873 s
->first
= ENTRY(header(s
->base
)+1);
874 s
->here
= ENTRY(header(s
->base
)+1);
875 s
->end
= s
->base
+ sb
->s_blocksize
;
878 error
= ext4_xattr_set_entry(i
, s
);
879 if (error
== -EFSCORRUPTED
)
883 if (!IS_LAST_ENTRY(s
->first
))
884 ext4_xattr_rehash(header(s
->base
), s
->here
);
887 if (!IS_LAST_ENTRY(s
->first
)) {
888 new_bh
= ext4_xattr_cache_find(inode
, header(s
->base
), &ce
);
890 /* We found an identical block in the cache. */
891 if (new_bh
== bs
->bh
)
892 ea_bdebug(new_bh
, "keeping");
894 /* The old block is released after updating
896 error
= dquot_alloc_block(inode
,
897 EXT4_C2B(EXT4_SB(sb
), 1));
900 BUFFER_TRACE(new_bh
, "get_write_access");
901 error
= ext4_journal_get_write_access(handle
,
906 le32_add_cpu(&BHDR(new_bh
)->h_refcount
, 1);
907 ea_bdebug(new_bh
, "reusing; refcount now=%d",
908 le32_to_cpu(BHDR(new_bh
)->h_refcount
));
909 unlock_buffer(new_bh
);
910 error
= ext4_handle_dirty_xattr_block(handle
,
916 mb_cache_entry_release(ce
);
918 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
919 /* We were modifying this block in-place. */
920 ea_bdebug(bs
->bh
, "keeping this block");
924 /* We need to allocate a new block */
925 ext4_fsblk_t goal
, block
;
927 goal
= ext4_group_first_block_no(sb
,
928 EXT4_I(inode
)->i_block_group
);
930 /* non-extent files can't have physical blocks past 2^32 */
931 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
932 goal
= goal
& EXT4_MAX_BLOCK_FILE_PHYS
;
934 block
= ext4_new_meta_blocks(handle
, inode
, goal
, 0,
939 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
940 BUG_ON(block
> EXT4_MAX_BLOCK_FILE_PHYS
);
942 ea_idebug(inode
, "creating block %llu",
943 (unsigned long long)block
);
945 new_bh
= sb_getblk(sb
, block
);
946 if (unlikely(!new_bh
)) {
949 ext4_free_blocks(handle
, inode
, NULL
, block
, 1,
950 EXT4_FREE_BLOCKS_METADATA
);
954 error
= ext4_journal_get_create_access(handle
, new_bh
);
956 unlock_buffer(new_bh
);
960 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
961 set_buffer_uptodate(new_bh
);
962 unlock_buffer(new_bh
);
963 ext4_xattr_cache_insert(ext4_mb_cache
, new_bh
);
964 error
= ext4_handle_dirty_xattr_block(handle
,
971 /* Update the inode. */
972 EXT4_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
974 /* Drop the previous xattr block. */
975 if (bs
->bh
&& bs
->bh
!= new_bh
)
976 ext4_xattr_release_block(handle
, inode
, bs
->bh
);
981 mb_cache_entry_release(ce
);
983 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
989 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(sb
), 1));
993 EXT4_ERROR_INODE(inode
, "bad block %llu",
994 EXT4_I(inode
)->i_file_acl
);
1000 int ext4_xattr_ibody_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
1001 struct ext4_xattr_ibody_find
*is
)
1003 struct ext4_xattr_ibody_header
*header
;
1004 struct ext4_inode
*raw_inode
;
1007 if (EXT4_I(inode
)->i_extra_isize
== 0)
1009 raw_inode
= ext4_raw_inode(&is
->iloc
);
1010 header
= IHDR(inode
, raw_inode
);
1011 is
->s
.base
= is
->s
.first
= IFIRST(header
);
1012 is
->s
.here
= is
->s
.first
;
1013 is
->s
.end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1014 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
1015 error
= xattr_check_inode(inode
, header
, is
->s
.end
);
1018 /* Find the named attribute. */
1019 error
= ext4_xattr_find_entry(&is
->s
.here
, i
->name_index
,
1020 i
->name
, is
->s
.end
-
1021 (void *)is
->s
.base
, 0);
1022 if (error
&& error
!= -ENODATA
)
1024 is
->s
.not_found
= error
;
1029 int ext4_xattr_ibody_inline_set(handle_t
*handle
, struct inode
*inode
,
1030 struct ext4_xattr_info
*i
,
1031 struct ext4_xattr_ibody_find
*is
)
1033 struct ext4_xattr_ibody_header
*header
;
1034 struct ext4_xattr_search
*s
= &is
->s
;
1037 if (EXT4_I(inode
)->i_extra_isize
== 0)
1039 error
= ext4_xattr_set_entry(i
, s
);
1041 if (error
== -ENOSPC
&&
1042 ext4_has_inline_data(inode
)) {
1043 error
= ext4_try_to_evict_inline_data(handle
, inode
,
1044 EXT4_XATTR_LEN(strlen(i
->name
) +
1045 EXT4_XATTR_SIZE(i
->value_len
)));
1048 error
= ext4_xattr_ibody_find(inode
, i
, is
);
1051 error
= ext4_xattr_set_entry(i
, s
);
1056 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1057 if (!IS_LAST_ENTRY(s
->first
)) {
1058 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1059 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1061 header
->h_magic
= cpu_to_le32(0);
1062 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1067 static int ext4_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
1068 struct ext4_xattr_info
*i
,
1069 struct ext4_xattr_ibody_find
*is
)
1071 struct ext4_xattr_ibody_header
*header
;
1072 struct ext4_xattr_search
*s
= &is
->s
;
1075 if (EXT4_I(inode
)->i_extra_isize
== 0)
1077 error
= ext4_xattr_set_entry(i
, s
);
1080 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1081 if (!IS_LAST_ENTRY(s
->first
)) {
1082 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1083 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1085 header
->h_magic
= cpu_to_le32(0);
1086 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1092 * ext4_xattr_set_handle()
1094 * Create, replace or remove an extended attribute for this inode. Value
1095 * is NULL to remove an existing extended attribute, and non-NULL to
1096 * either replace an existing extended attribute, or create a new extended
1097 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1098 * specify that an extended attribute must exist and must not exist
1099 * previous to the call, respectively.
1101 * Returns 0, or a negative error number on failure.
1104 ext4_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
1105 const char *name
, const void *value
, size_t value_len
,
1108 struct ext4_xattr_info i
= {
1109 .name_index
= name_index
,
1112 .value_len
= value_len
,
1115 struct ext4_xattr_ibody_find is
= {
1116 .s
= { .not_found
= -ENODATA
, },
1118 struct ext4_xattr_block_find bs
= {
1119 .s
= { .not_found
= -ENODATA
, },
1121 unsigned long no_expand
;
1126 if (strlen(name
) > 255)
1128 down_write(&EXT4_I(inode
)->xattr_sem
);
1129 no_expand
= ext4_test_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1130 ext4_set_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1132 error
= ext4_reserve_inode_write(handle
, inode
, &is
.iloc
);
1136 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
)) {
1137 struct ext4_inode
*raw_inode
= ext4_raw_inode(&is
.iloc
);
1138 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
1139 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
1142 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
1146 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1149 if (is
.s
.not_found
&& bs
.s
.not_found
) {
1151 if (flags
& XATTR_REPLACE
)
1158 if (flags
& XATTR_CREATE
)
1162 if (!is
.s
.not_found
)
1163 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1164 else if (!bs
.s
.not_found
)
1165 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1167 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1168 if (!error
&& !bs
.s
.not_found
) {
1170 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1171 } else if (error
== -ENOSPC
) {
1172 if (EXT4_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
1173 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1177 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1180 if (!is
.s
.not_found
) {
1182 error
= ext4_xattr_ibody_set(handle
, inode
, &i
,
1188 ext4_xattr_update_super_block(handle
, inode
->i_sb
);
1189 inode
->i_ctime
= ext4_current_time(inode
);
1191 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1192 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
1194 * The bh is consumed by ext4_mark_iloc_dirty, even with
1199 ext4_handle_sync(handle
);
1206 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1207 up_write(&EXT4_I(inode
)->xattr_sem
);
1214 * Like ext4_xattr_set_handle, but start from an inode. This extended
1215 * attribute modification is a filesystem transaction by itself.
1217 * Returns 0, or a negative error number on failure.
1220 ext4_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
1221 const void *value
, size_t value_len
, int flags
)
1224 int error
, retries
= 0;
1225 int credits
= ext4_jbd2_credits_xattr(inode
);
1228 handle
= ext4_journal_start(inode
, EXT4_HT_XATTR
, credits
);
1229 if (IS_ERR(handle
)) {
1230 error
= PTR_ERR(handle
);
1234 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, name
,
1235 value
, value_len
, flags
);
1236 error2
= ext4_journal_stop(handle
);
1237 if (error
== -ENOSPC
&&
1238 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1248 * Shift the EA entries in the inode to create space for the increased
1251 static void ext4_xattr_shift_entries(struct ext4_xattr_entry
*entry
,
1252 int value_offs_shift
, void *to
,
1253 void *from
, size_t n
, int blocksize
)
1255 struct ext4_xattr_entry
*last
= entry
;
1258 /* Adjust the value offsets of the entries */
1259 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1260 if (!last
->e_value_block
&& last
->e_value_size
) {
1261 new_offs
= le16_to_cpu(last
->e_value_offs
) +
1263 BUG_ON(new_offs
+ le32_to_cpu(last
->e_value_size
)
1265 last
->e_value_offs
= cpu_to_le16(new_offs
);
1268 /* Shift the entries by n bytes */
1269 memmove(to
, from
, n
);
1273 * Expand an inode by new_extra_isize bytes when EAs are present.
1274 * Returns 0 on success or negative error number on failure.
1276 int ext4_expand_extra_isize_ea(struct inode
*inode
, int new_extra_isize
,
1277 struct ext4_inode
*raw_inode
, handle_t
*handle
)
1279 struct ext4_xattr_ibody_header
*header
;
1280 struct ext4_xattr_entry
*entry
, *last
, *first
;
1281 struct buffer_head
*bh
= NULL
;
1282 struct ext4_xattr_ibody_find
*is
= NULL
;
1283 struct ext4_xattr_block_find
*bs
= NULL
;
1284 char *buffer
= NULL
, *b_entry_name
= NULL
;
1285 size_t min_offs
, free
;
1287 void *base
, *start
, *end
;
1288 int error
= 0, tried_min_extra_isize
= 0;
1289 int s_min_extra_isize
= le16_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_min_extra_isize
);
1290 int isize_diff
; /* How much do we need to grow i_extra_isize */
1292 down_write(&EXT4_I(inode
)->xattr_sem
);
1294 * Set EXT4_STATE_NO_EXPAND to avoid recursion when marking inode dirty
1296 ext4_set_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1298 isize_diff
= new_extra_isize
- EXT4_I(inode
)->i_extra_isize
;
1299 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
)
1302 header
= IHDR(inode
, raw_inode
);
1303 entry
= IFIRST(header
);
1306 * Check if enough free space is available in the inode to shift the
1307 * entries ahead by new_extra_isize.
1310 base
= start
= entry
;
1311 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1312 min_offs
= end
- base
;
1314 total_ino
= sizeof(struct ext4_xattr_ibody_header
);
1316 error
= xattr_check_inode(inode
, header
, end
);
1320 free
= ext4_xattr_free_space(last
, &min_offs
, base
, &total_ino
);
1321 if (free
>= isize_diff
) {
1322 entry
= IFIRST(header
);
1323 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
1324 - new_extra_isize
, (void *)raw_inode
+
1325 EXT4_GOOD_OLD_INODE_SIZE
+ new_extra_isize
,
1326 (void *)header
, total_ino
,
1327 inode
->i_sb
->s_blocksize
);
1328 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
1333 * Enough free space isn't available in the inode, check if
1334 * EA block can hold new_extra_isize bytes.
1336 if (EXT4_I(inode
)->i_file_acl
) {
1337 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1341 if (ext4_xattr_check_block(inode
, bh
)) {
1342 EXT4_ERROR_INODE(inode
, "bad block %llu",
1343 EXT4_I(inode
)->i_file_acl
);
1344 error
= -EFSCORRUPTED
;
1349 end
= bh
->b_data
+ bh
->b_size
;
1350 min_offs
= end
- base
;
1351 free
= ext4_xattr_free_space(first
, &min_offs
, base
, NULL
);
1352 if (free
< isize_diff
) {
1353 if (!tried_min_extra_isize
&& s_min_extra_isize
) {
1354 tried_min_extra_isize
++;
1355 new_extra_isize
= s_min_extra_isize
;
1363 free
= inode
->i_sb
->s_blocksize
;
1366 while (isize_diff
> 0) {
1367 size_t offs
, size
, entry_size
;
1368 struct ext4_xattr_entry
*small_entry
= NULL
;
1369 struct ext4_xattr_info i
= {
1373 unsigned int total_size
; /* EA entry size + value size */
1374 unsigned int shift_bytes
; /* No. of bytes to shift EAs by? */
1375 unsigned int min_total_size
= ~0U;
1377 is
= kzalloc(sizeof(struct ext4_xattr_ibody_find
), GFP_NOFS
);
1378 bs
= kzalloc(sizeof(struct ext4_xattr_block_find
), GFP_NOFS
);
1384 is
->s
.not_found
= -ENODATA
;
1385 bs
->s
.not_found
= -ENODATA
;
1389 last
= IFIRST(header
);
1390 /* Find the entry best suited to be pushed into EA block */
1392 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1394 EXT4_XATTR_SIZE(le32_to_cpu(last
->e_value_size
)) +
1395 EXT4_XATTR_LEN(last
->e_name_len
);
1396 if (total_size
<= free
&& total_size
< min_total_size
) {
1397 if (total_size
< isize_diff
) {
1401 min_total_size
= total_size
;
1406 if (entry
== NULL
) {
1408 entry
= small_entry
;
1410 if (!tried_min_extra_isize
&&
1411 s_min_extra_isize
) {
1412 tried_min_extra_isize
++;
1413 new_extra_isize
= s_min_extra_isize
;
1414 kfree(is
); is
= NULL
;
1415 kfree(bs
); bs
= NULL
;
1423 offs
= le16_to_cpu(entry
->e_value_offs
);
1424 size
= le32_to_cpu(entry
->e_value_size
);
1425 entry_size
= EXT4_XATTR_LEN(entry
->e_name_len
);
1426 i
.name_index
= entry
->e_name_index
,
1427 buffer
= kmalloc(EXT4_XATTR_SIZE(size
), GFP_NOFS
);
1428 b_entry_name
= kmalloc(entry
->e_name_len
+ 1, GFP_NOFS
);
1429 if (!buffer
|| !b_entry_name
) {
1433 /* Save the entry name and the entry value */
1434 memcpy(buffer
, (void *)IFIRST(header
) + offs
,
1435 EXT4_XATTR_SIZE(size
));
1436 memcpy(b_entry_name
, entry
->e_name
, entry
->e_name_len
);
1437 b_entry_name
[entry
->e_name_len
] = '\0';
1438 i
.name
= b_entry_name
;
1440 error
= ext4_get_inode_loc(inode
, &is
->iloc
);
1444 error
= ext4_xattr_ibody_find(inode
, &i
, is
);
1448 /* Remove the chosen entry from the inode */
1449 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, is
);
1452 total_ino
-= entry_size
;
1454 entry
= IFIRST(header
);
1455 if (entry_size
+ EXT4_XATTR_SIZE(size
) >= isize_diff
)
1456 shift_bytes
= isize_diff
;
1458 shift_bytes
= entry_size
+ EXT4_XATTR_SIZE(size
);
1459 /* Adjust the offsets and shift the remaining entries ahead */
1460 ext4_xattr_shift_entries(entry
, -shift_bytes
,
1461 (void *)raw_inode
+ EXT4_GOOD_OLD_INODE_SIZE
+
1462 EXT4_I(inode
)->i_extra_isize
+ shift_bytes
,
1463 (void *)header
, total_ino
, inode
->i_sb
->s_blocksize
);
1465 isize_diff
-= shift_bytes
;
1466 EXT4_I(inode
)->i_extra_isize
+= shift_bytes
;
1467 header
= IHDR(inode
, raw_inode
);
1469 i
.name
= b_entry_name
;
1472 error
= ext4_xattr_block_find(inode
, &i
, bs
);
1476 /* Add entry which was removed from the inode into the block */
1477 error
= ext4_xattr_block_set(handle
, inode
, &i
, bs
);
1480 kfree(b_entry_name
);
1482 b_entry_name
= NULL
;
1484 brelse(is
->iloc
.bh
);
1490 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1491 up_write(&EXT4_I(inode
)->xattr_sem
);
1495 kfree(b_entry_name
);
1498 brelse(is
->iloc
.bh
);
1503 * We deliberately leave EXT4_STATE_NO_EXPAND set here since inode
1504 * size expansion failed.
1506 up_write(&EXT4_I(inode
)->xattr_sem
);
1513 * ext4_xattr_delete_inode()
1515 * Free extended attribute resources associated with this inode. This
1516 * is called immediately before an inode is freed. We have exclusive
1517 * access to the inode.
1520 ext4_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
)
1522 struct buffer_head
*bh
= NULL
;
1524 if (!EXT4_I(inode
)->i_file_acl
)
1526 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1528 EXT4_ERROR_INODE(inode
, "block %llu read error",
1529 EXT4_I(inode
)->i_file_acl
);
1532 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
1533 BHDR(bh
)->h_blocks
!= cpu_to_le32(1)) {
1534 EXT4_ERROR_INODE(inode
, "bad block %llu",
1535 EXT4_I(inode
)->i_file_acl
);
1538 ext4_xattr_release_block(handle
, inode
, bh
);
1539 EXT4_I(inode
)->i_file_acl
= 0;
1546 * ext4_xattr_put_super()
1548 * This is called when a file system is unmounted.
1551 ext4_xattr_put_super(struct super_block
*sb
)
1553 mb_cache_shrink(sb
->s_bdev
);
1557 * ext4_xattr_cache_insert()
1559 * Create a new entry in the extended attribute cache, and insert
1560 * it unless such an entry is already in the cache.
1562 * Returns 0, or a negative error number on failure.
1565 ext4_xattr_cache_insert(struct mb_cache
*ext4_mb_cache
, struct buffer_head
*bh
)
1567 __u32 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
1568 struct mb_cache_entry
*ce
;
1571 ce
= mb_cache_entry_alloc(ext4_mb_cache
, GFP_NOFS
);
1573 ea_bdebug(bh
, "out of memory");
1576 error
= mb_cache_entry_insert(ce
, bh
->b_bdev
, bh
->b_blocknr
, hash
);
1578 mb_cache_entry_free(ce
);
1579 if (error
== -EBUSY
) {
1580 ea_bdebug(bh
, "already in cache");
1584 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
1585 mb_cache_entry_release(ce
);
1592 * Compare two extended attribute blocks for equality.
1594 * Returns 0 if the blocks are equal, 1 if they differ, and
1595 * a negative error number on errors.
1598 ext4_xattr_cmp(struct ext4_xattr_header
*header1
,
1599 struct ext4_xattr_header
*header2
)
1601 struct ext4_xattr_entry
*entry1
, *entry2
;
1603 entry1
= ENTRY(header1
+1);
1604 entry2
= ENTRY(header2
+1);
1605 while (!IS_LAST_ENTRY(entry1
)) {
1606 if (IS_LAST_ENTRY(entry2
))
1608 if (entry1
->e_hash
!= entry2
->e_hash
||
1609 entry1
->e_name_index
!= entry2
->e_name_index
||
1610 entry1
->e_name_len
!= entry2
->e_name_len
||
1611 entry1
->e_value_size
!= entry2
->e_value_size
||
1612 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
1614 if (entry1
->e_value_block
!= 0 || entry2
->e_value_block
!= 0)
1615 return -EFSCORRUPTED
;
1616 if (memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
1617 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
1618 le32_to_cpu(entry1
->e_value_size
)))
1621 entry1
= EXT4_XATTR_NEXT(entry1
);
1622 entry2
= EXT4_XATTR_NEXT(entry2
);
1624 if (!IS_LAST_ENTRY(entry2
))
1630 * ext4_xattr_cache_find()
1632 * Find an identical extended attribute block.
1634 * Returns a pointer to the block found, or NULL if such a block was
1635 * not found or an error occurred.
1637 static struct buffer_head
*
1638 ext4_xattr_cache_find(struct inode
*inode
, struct ext4_xattr_header
*header
,
1639 struct mb_cache_entry
**pce
)
1641 __u32 hash
= le32_to_cpu(header
->h_hash
);
1642 struct mb_cache_entry
*ce
;
1643 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
1645 if (!header
->h_hash
)
1646 return NULL
; /* never share */
1647 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
1649 ce
= mb_cache_entry_find_first(ext4_mb_cache
, inode
->i_sb
->s_bdev
,
1652 struct buffer_head
*bh
;
1655 if (PTR_ERR(ce
) == -EAGAIN
)
1659 bh
= sb_bread(inode
->i_sb
, ce
->e_block
);
1661 EXT4_ERROR_INODE(inode
, "block %lu read error",
1662 (unsigned long) ce
->e_block
);
1663 } else if (le32_to_cpu(BHDR(bh
)->h_refcount
) >=
1664 EXT4_XATTR_REFCOUNT_MAX
) {
1665 ea_idebug(inode
, "block %lu refcount %d>=%d",
1666 (unsigned long) ce
->e_block
,
1667 le32_to_cpu(BHDR(bh
)->h_refcount
),
1668 EXT4_XATTR_REFCOUNT_MAX
);
1669 } else if (ext4_xattr_cmp(header
, BHDR(bh
)) == 0) {
1674 ce
= mb_cache_entry_find_next(ce
, inode
->i_sb
->s_bdev
, hash
);
1679 #define NAME_HASH_SHIFT 5
1680 #define VALUE_HASH_SHIFT 16
1683 * ext4_xattr_hash_entry()
1685 * Compute the hash of an extended attribute.
1687 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header
*header
,
1688 struct ext4_xattr_entry
*entry
)
1691 char *name
= entry
->e_name
;
1694 for (n
= 0; n
< entry
->e_name_len
; n
++) {
1695 hash
= (hash
<< NAME_HASH_SHIFT
) ^
1696 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
1700 if (entry
->e_value_block
== 0 && entry
->e_value_size
!= 0) {
1701 __le32
*value
= (__le32
*)((char *)header
+
1702 le16_to_cpu(entry
->e_value_offs
));
1703 for (n
= (le32_to_cpu(entry
->e_value_size
) +
1704 EXT4_XATTR_ROUND
) >> EXT4_XATTR_PAD_BITS
; n
; n
--) {
1705 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
1706 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
1707 le32_to_cpu(*value
++);
1710 entry
->e_hash
= cpu_to_le32(hash
);
1713 #undef NAME_HASH_SHIFT
1714 #undef VALUE_HASH_SHIFT
1716 #define BLOCK_HASH_SHIFT 16
1719 * ext4_xattr_rehash()
1721 * Re-compute the extended attribute hash value after an entry has changed.
1723 static void ext4_xattr_rehash(struct ext4_xattr_header
*header
,
1724 struct ext4_xattr_entry
*entry
)
1726 struct ext4_xattr_entry
*here
;
1729 ext4_xattr_hash_entry(header
, entry
);
1730 here
= ENTRY(header
+1);
1731 while (!IS_LAST_ENTRY(here
)) {
1732 if (!here
->e_hash
) {
1733 /* Block is not shared if an entry's hash value == 0 */
1737 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
1738 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
1739 le32_to_cpu(here
->e_hash
);
1740 here
= EXT4_XATTR_NEXT(here
);
1742 header
->h_hash
= cpu_to_le32(hash
);
1745 #undef BLOCK_HASH_SHIFT
1747 #define HASH_BUCKET_BITS 10
1750 ext4_xattr_create_cache(char *name
)
1752 return mb_cache_create(name
, HASH_BUCKET_BITS
);
1755 void ext4_xattr_destroy_cache(struct mb_cache
*cache
)
1758 mb_cache_destroy(cache
);