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
,
142 struct buffer_head
*bh
)
144 struct ext4_xattr_header
*hdr
= BHDR(bh
);
147 if (ext4_has_metadata_csum(inode
->i_sb
)) {
149 ret
= (hdr
->h_checksum
== ext4_xattr_block_csum(inode
,
150 bh
->b_blocknr
, hdr
));
156 static void ext4_xattr_block_csum_set(struct inode
*inode
,
157 struct buffer_head
*bh
)
159 if (ext4_has_metadata_csum(inode
->i_sb
))
160 BHDR(bh
)->h_checksum
= ext4_xattr_block_csum(inode
,
161 bh
->b_blocknr
, BHDR(bh
));
164 static inline const struct xattr_handler
*
165 ext4_xattr_handler(int name_index
)
167 const struct xattr_handler
*handler
= NULL
;
169 if (name_index
> 0 && name_index
< ARRAY_SIZE(ext4_xattr_handler_map
))
170 handler
= ext4_xattr_handler_map
[name_index
];
175 * Inode operation listxattr()
177 * d_inode(dentry)->i_mutex: don't care
180 ext4_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
182 return ext4_xattr_list(dentry
, buffer
, size
);
186 ext4_xattr_check_names(struct ext4_xattr_entry
*entry
, void *end
,
189 struct ext4_xattr_entry
*e
= entry
;
191 while (!IS_LAST_ENTRY(e
)) {
192 struct ext4_xattr_entry
*next
= EXT4_XATTR_NEXT(e
);
193 if ((void *)next
>= end
)
194 return -EFSCORRUPTED
;
195 if (strnlen(e
->e_name
, e
->e_name_len
) != e
->e_name_len
)
196 return -EFSCORRUPTED
;
200 while (!IS_LAST_ENTRY(entry
)) {
201 if (entry
->e_value_size
!= 0 &&
202 (value_start
+ le16_to_cpu(entry
->e_value_offs
) <
203 (void *)e
+ sizeof(__u32
) ||
204 value_start
+ le16_to_cpu(entry
->e_value_offs
) +
205 le32_to_cpu(entry
->e_value_size
) > end
))
206 return -EFSCORRUPTED
;
207 entry
= EXT4_XATTR_NEXT(entry
);
214 ext4_xattr_check_block(struct inode
*inode
, struct buffer_head
*bh
)
218 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
219 BHDR(bh
)->h_blocks
!= cpu_to_le32(1))
220 return -EFSCORRUPTED
;
221 if (buffer_verified(bh
))
224 if (!ext4_xattr_block_csum_verify(inode
, bh
))
226 error
= ext4_xattr_check_names(BFIRST(bh
), bh
->b_data
+ bh
->b_size
,
229 set_buffer_verified(bh
);
234 __xattr_check_inode(struct inode
*inode
, struct ext4_xattr_ibody_header
*header
,
235 void *end
, const char *function
, unsigned int line
)
237 struct ext4_xattr_entry
*entry
= IFIRST(header
);
238 int error
= -EFSCORRUPTED
;
240 if (((void *) header
>= end
) ||
241 (header
->h_magic
!= le32_to_cpu(EXT4_XATTR_MAGIC
)))
243 error
= ext4_xattr_check_names(entry
, end
, entry
);
246 __ext4_error_inode(inode
, function
, line
, 0,
247 "corrupted in-inode xattr");
251 #define xattr_check_inode(inode, header, end) \
252 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
255 ext4_xattr_check_entry(struct ext4_xattr_entry
*entry
, size_t size
)
257 size_t value_size
= le32_to_cpu(entry
->e_value_size
);
259 if (entry
->e_value_block
!= 0 || value_size
> size
||
260 le16_to_cpu(entry
->e_value_offs
) + value_size
> size
)
261 return -EFSCORRUPTED
;
266 ext4_xattr_find_entry(struct ext4_xattr_entry
**pentry
, int name_index
,
267 const char *name
, size_t size
, int sorted
)
269 struct ext4_xattr_entry
*entry
;
275 name_len
= strlen(name
);
277 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
278 cmp
= name_index
- entry
->e_name_index
;
280 cmp
= name_len
- entry
->e_name_len
;
282 cmp
= memcmp(name
, entry
->e_name
, name_len
);
283 if (cmp
<= 0 && (sorted
|| cmp
== 0))
287 if (!cmp
&& ext4_xattr_check_entry(entry
, size
))
288 return -EFSCORRUPTED
;
289 return cmp
? -ENODATA
: 0;
293 ext4_xattr_block_get(struct inode
*inode
, int name_index
, const char *name
,
294 void *buffer
, size_t buffer_size
)
296 struct buffer_head
*bh
= NULL
;
297 struct ext4_xattr_entry
*entry
;
300 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
302 ea_idebug(inode
, "name=%d.%s, buffer=%p, buffer_size=%ld",
303 name_index
, name
, buffer
, (long)buffer_size
);
306 if (!EXT4_I(inode
)->i_file_acl
)
308 ea_idebug(inode
, "reading block %llu",
309 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
310 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
313 ea_bdebug(bh
, "b_count=%d, refcount=%d",
314 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
315 if (ext4_xattr_check_block(inode
, bh
)) {
317 EXT4_ERROR_INODE(inode
, "bad block %llu",
318 EXT4_I(inode
)->i_file_acl
);
319 error
= -EFSCORRUPTED
;
322 ext4_xattr_cache_insert(ext4_mb_cache
, bh
);
324 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, bh
->b_size
, 1);
325 if (error
== -EFSCORRUPTED
)
329 size
= le32_to_cpu(entry
->e_value_size
);
332 if (size
> buffer_size
)
334 memcpy(buffer
, bh
->b_data
+ le16_to_cpu(entry
->e_value_offs
),
345 ext4_xattr_ibody_get(struct inode
*inode
, int name_index
, const char *name
,
346 void *buffer
, size_t buffer_size
)
348 struct ext4_xattr_ibody_header
*header
;
349 struct ext4_xattr_entry
*entry
;
350 struct ext4_inode
*raw_inode
;
351 struct ext4_iloc iloc
;
356 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
358 error
= ext4_get_inode_loc(inode
, &iloc
);
361 raw_inode
= ext4_raw_inode(&iloc
);
362 header
= IHDR(inode
, raw_inode
);
363 entry
= IFIRST(header
);
364 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
365 error
= xattr_check_inode(inode
, header
, end
);
368 error
= ext4_xattr_find_entry(&entry
, name_index
, name
,
369 end
- (void *)entry
, 0);
372 size
= le32_to_cpu(entry
->e_value_size
);
375 if (size
> buffer_size
)
377 memcpy(buffer
, (void *)IFIRST(header
) +
378 le16_to_cpu(entry
->e_value_offs
), size
);
390 * Copy an extended attribute into the buffer
391 * provided, or compute the buffer size required.
392 * Buffer is NULL to compute the size of the buffer required.
394 * Returns a negative error number on failure, or the number of bytes
395 * used / required on success.
398 ext4_xattr_get(struct inode
*inode
, int name_index
, const char *name
,
399 void *buffer
, size_t buffer_size
)
403 if (strlen(name
) > 255)
406 down_read(&EXT4_I(inode
)->xattr_sem
);
407 error
= ext4_xattr_ibody_get(inode
, name_index
, name
, buffer
,
409 if (error
== -ENODATA
)
410 error
= ext4_xattr_block_get(inode
, name_index
, name
, buffer
,
412 up_read(&EXT4_I(inode
)->xattr_sem
);
417 ext4_xattr_list_entries(struct dentry
*dentry
, struct ext4_xattr_entry
*entry
,
418 char *buffer
, size_t buffer_size
)
420 size_t rest
= buffer_size
;
422 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
423 const struct xattr_handler
*handler
=
424 ext4_xattr_handler(entry
->e_name_index
);
427 size_t size
= handler
->list(handler
, dentry
, buffer
,
438 return buffer_size
- rest
;
442 ext4_xattr_block_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
444 struct inode
*inode
= d_inode(dentry
);
445 struct buffer_head
*bh
= NULL
;
447 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
449 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
450 buffer
, (long)buffer_size
);
453 if (!EXT4_I(inode
)->i_file_acl
)
455 ea_idebug(inode
, "reading block %llu",
456 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
457 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
461 ea_bdebug(bh
, "b_count=%d, refcount=%d",
462 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
463 if (ext4_xattr_check_block(inode
, bh
)) {
464 EXT4_ERROR_INODE(inode
, "bad block %llu",
465 EXT4_I(inode
)->i_file_acl
);
466 error
= -EFSCORRUPTED
;
469 ext4_xattr_cache_insert(ext4_mb_cache
, bh
);
470 error
= ext4_xattr_list_entries(dentry
, BFIRST(bh
), buffer
, buffer_size
);
479 ext4_xattr_ibody_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
481 struct inode
*inode
= d_inode(dentry
);
482 struct ext4_xattr_ibody_header
*header
;
483 struct ext4_inode
*raw_inode
;
484 struct ext4_iloc iloc
;
488 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
490 error
= ext4_get_inode_loc(inode
, &iloc
);
493 raw_inode
= ext4_raw_inode(&iloc
);
494 header
= IHDR(inode
, raw_inode
);
495 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
496 error
= xattr_check_inode(inode
, header
, end
);
499 error
= ext4_xattr_list_entries(dentry
, IFIRST(header
),
500 buffer
, buffer_size
);
510 * Copy a list of attribute names into the buffer
511 * provided, or compute the buffer size required.
512 * Buffer is NULL to compute the size of the buffer required.
514 * Returns a negative error number on failure, or the number of bytes
515 * used / required on success.
518 ext4_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
522 down_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
523 ret
= ret2
= ext4_xattr_ibody_list(dentry
, buffer
, buffer_size
);
530 ret
= ext4_xattr_block_list(dentry
, buffer
, buffer_size
);
535 up_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
540 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
543 static void ext4_xattr_update_super_block(handle_t
*handle
,
544 struct super_block
*sb
)
546 if (ext4_has_feature_xattr(sb
))
549 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
550 if (ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
) == 0) {
551 ext4_set_feature_xattr(sb
);
552 ext4_handle_dirty_super(handle
, sb
);
557 * Release the xattr block BH: If the reference count is > 1, decrement it;
558 * otherwise free the block.
561 ext4_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
562 struct buffer_head
*bh
)
564 struct mb_cache_entry
*ce
= NULL
;
566 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
568 ce
= mb_cache_entry_get(ext4_mb_cache
, bh
->b_bdev
, bh
->b_blocknr
);
569 BUFFER_TRACE(bh
, "get_write_access");
570 error
= ext4_journal_get_write_access(handle
, bh
);
575 if (BHDR(bh
)->h_refcount
== cpu_to_le32(1)) {
576 ea_bdebug(bh
, "refcount now=0; freeing");
578 mb_cache_entry_free(ce
);
581 ext4_free_blocks(handle
, inode
, bh
, 0, 1,
582 EXT4_FREE_BLOCKS_METADATA
|
583 EXT4_FREE_BLOCKS_FORGET
);
585 le32_add_cpu(&BHDR(bh
)->h_refcount
, -1);
587 mb_cache_entry_release(ce
);
589 ext4_xattr_block_csum_set(inode
, bh
);
591 * Beware of this ugliness: Releasing of xattr block references
592 * from different inodes can race and so we have to protect
593 * from a race where someone else frees the block (and releases
594 * its journal_head) before we are done dirtying the buffer. In
595 * nojournal mode this race is harmless and we actually cannot
596 * call ext4_handle_dirty_metadata() with locked buffer as
597 * that function can call sync_dirty_buffer() so for that case
598 * we handle the dirtying after unlocking the buffer.
600 if (ext4_handle_valid(handle
))
601 error
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
603 if (!ext4_handle_valid(handle
))
604 error
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
606 ext4_handle_sync(handle
);
607 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(inode
->i_sb
), 1));
608 ea_bdebug(bh
, "refcount now=%d; releasing",
609 le32_to_cpu(BHDR(bh
)->h_refcount
));
612 ext4_std_error(inode
->i_sb
, error
);
617 * Find the available free space for EAs. This also returns the total number of
618 * bytes used by EA entries.
620 static size_t ext4_xattr_free_space(struct ext4_xattr_entry
*last
,
621 size_t *min_offs
, void *base
, int *total
)
623 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
624 if (!last
->e_value_block
&& last
->e_value_size
) {
625 size_t offs
= le16_to_cpu(last
->e_value_offs
);
626 if (offs
< *min_offs
)
630 *total
+= EXT4_XATTR_LEN(last
->e_name_len
);
632 return (*min_offs
- ((void *)last
- base
) - sizeof(__u32
));
636 ext4_xattr_set_entry(struct ext4_xattr_info
*i
, struct ext4_xattr_search
*s
,
639 struct ext4_xattr_entry
*last
, *next
;
640 size_t free
, min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
642 /* Compute min_offs and last. */
644 for (; !IS_LAST_ENTRY(last
); last
= next
) {
645 next
= EXT4_XATTR_NEXT(last
);
646 if ((void *)next
>= s
->end
) {
647 EXT4_ERROR_INODE(inode
, "corrupted xattr entries");
648 return -EFSCORRUPTED
;
650 if (!last
->e_value_block
&& last
->e_value_size
) {
651 size_t offs
= le16_to_cpu(last
->e_value_offs
);
656 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
658 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
659 size_t size
= le32_to_cpu(s
->here
->e_value_size
);
660 free
+= EXT4_XATTR_SIZE(size
);
662 free
+= EXT4_XATTR_LEN(name_len
);
665 if (free
< EXT4_XATTR_LEN(name_len
) +
666 EXT4_XATTR_SIZE(i
->value_len
))
670 if (i
->value
&& s
->not_found
) {
671 /* Insert the new name. */
672 size_t size
= EXT4_XATTR_LEN(name_len
);
673 size_t rest
= (void *)last
- (void *)s
->here
+ sizeof(__u32
);
674 memmove((void *)s
->here
+ size
, s
->here
, rest
);
675 memset(s
->here
, 0, size
);
676 s
->here
->e_name_index
= i
->name_index
;
677 s
->here
->e_name_len
= name_len
;
678 memcpy(s
->here
->e_name
, i
->name
, name_len
);
680 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
681 void *first_val
= s
->base
+ min_offs
;
682 size_t offs
= le16_to_cpu(s
->here
->e_value_offs
);
683 void *val
= s
->base
+ offs
;
684 size_t size
= EXT4_XATTR_SIZE(
685 le32_to_cpu(s
->here
->e_value_size
));
687 if (i
->value
&& size
== EXT4_XATTR_SIZE(i
->value_len
)) {
688 /* The old and the new value have the same
689 size. Just replace. */
690 s
->here
->e_value_size
=
691 cpu_to_le32(i
->value_len
);
692 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
693 memset(val
, 0, size
);
695 /* Clear pad bytes first. */
696 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
698 memcpy(val
, i
->value
, i
->value_len
);
703 /* Remove the old value. */
704 memmove(first_val
+ size
, first_val
, val
- first_val
);
705 memset(first_val
, 0, size
);
706 s
->here
->e_value_size
= 0;
707 s
->here
->e_value_offs
= 0;
710 /* Adjust all value offsets. */
712 while (!IS_LAST_ENTRY(last
)) {
713 size_t o
= le16_to_cpu(last
->e_value_offs
);
714 if (!last
->e_value_block
&&
715 last
->e_value_size
&& o
< offs
)
717 cpu_to_le16(o
+ size
);
718 last
= EXT4_XATTR_NEXT(last
);
722 /* Remove the old name. */
723 size_t size
= EXT4_XATTR_LEN(name_len
);
724 last
= ENTRY((void *)last
- size
);
725 memmove(s
->here
, (void *)s
->here
+ size
,
726 (void *)last
- (void *)s
->here
+ sizeof(__u32
));
727 memset(last
, 0, size
);
732 /* Insert the new value. */
733 s
->here
->e_value_size
= cpu_to_le32(i
->value_len
);
735 size_t size
= EXT4_XATTR_SIZE(i
->value_len
);
736 void *val
= s
->base
+ min_offs
- size
;
737 s
->here
->e_value_offs
= cpu_to_le16(min_offs
- size
);
738 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
739 memset(val
, 0, size
);
741 /* Clear the pad bytes first. */
742 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
744 memcpy(val
, i
->value
, i
->value_len
);
751 struct ext4_xattr_block_find
{
752 struct ext4_xattr_search s
;
753 struct buffer_head
*bh
;
757 ext4_xattr_block_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
758 struct ext4_xattr_block_find
*bs
)
760 struct super_block
*sb
= inode
->i_sb
;
763 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
764 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
766 if (EXT4_I(inode
)->i_file_acl
) {
767 /* The inode already has an extended attribute block. */
768 bs
->bh
= sb_bread(sb
, EXT4_I(inode
)->i_file_acl
);
772 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
773 atomic_read(&(bs
->bh
->b_count
)),
774 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
775 if (ext4_xattr_check_block(inode
, bs
->bh
)) {
776 EXT4_ERROR_INODE(inode
, "bad block %llu",
777 EXT4_I(inode
)->i_file_acl
);
778 error
= -EFSCORRUPTED
;
781 /* Find the named attribute. */
782 bs
->s
.base
= BHDR(bs
->bh
);
783 bs
->s
.first
= BFIRST(bs
->bh
);
784 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
785 bs
->s
.here
= bs
->s
.first
;
786 error
= ext4_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
787 i
->name
, bs
->bh
->b_size
, 1);
788 if (error
&& error
!= -ENODATA
)
790 bs
->s
.not_found
= error
;
799 ext4_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
800 struct ext4_xattr_info
*i
,
801 struct ext4_xattr_block_find
*bs
)
803 struct super_block
*sb
= inode
->i_sb
;
804 struct buffer_head
*new_bh
= NULL
;
805 struct ext4_xattr_search
*s
= &bs
->s
;
806 struct mb_cache_entry
*ce
= NULL
;
808 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
810 #define header(x) ((struct ext4_xattr_header *)(x))
812 if (i
->value
&& i
->value_len
> sb
->s_blocksize
)
815 ce
= mb_cache_entry_get(ext4_mb_cache
, bs
->bh
->b_bdev
,
817 BUFFER_TRACE(bs
->bh
, "get_write_access");
818 error
= ext4_journal_get_write_access(handle
, bs
->bh
);
823 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
825 mb_cache_entry_free(ce
);
828 ea_bdebug(bs
->bh
, "modifying in-place");
829 error
= ext4_xattr_set_entry(i
, s
, inode
);
831 if (!IS_LAST_ENTRY(s
->first
))
832 ext4_xattr_rehash(header(s
->base
),
835 ext4_xattr_block_csum_set(inode
, bs
->bh
);
836 unlock_buffer(bs
->bh
);
837 if (error
== -EFSCORRUPTED
)
840 error
= ext4_handle_dirty_metadata(handle
,
847 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
849 unlock_buffer(bs
->bh
);
851 mb_cache_entry_release(ce
);
854 ea_bdebug(bs
->bh
, "cloning");
855 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
859 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
860 s
->first
= ENTRY(header(s
->base
)+1);
861 header(s
->base
)->h_refcount
= cpu_to_le32(1);
862 s
->here
= ENTRY(s
->base
+ offset
);
863 s
->end
= s
->base
+ bs
->bh
->b_size
;
866 /* Allocate a buffer where we construct the new block. */
867 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
868 /* assert(header == s->base) */
872 header(s
->base
)->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
873 header(s
->base
)->h_blocks
= cpu_to_le32(1);
874 header(s
->base
)->h_refcount
= cpu_to_le32(1);
875 s
->first
= ENTRY(header(s
->base
)+1);
876 s
->here
= ENTRY(header(s
->base
)+1);
877 s
->end
= s
->base
+ sb
->s_blocksize
;
880 error
= ext4_xattr_set_entry(i
, s
, inode
);
881 if (error
== -EFSCORRUPTED
)
885 if (!IS_LAST_ENTRY(s
->first
))
886 ext4_xattr_rehash(header(s
->base
), s
->here
);
889 if (!IS_LAST_ENTRY(s
->first
)) {
890 new_bh
= ext4_xattr_cache_find(inode
, header(s
->base
), &ce
);
892 /* We found an identical block in the cache. */
893 if (new_bh
== bs
->bh
)
894 ea_bdebug(new_bh
, "keeping");
896 /* The old block is released after updating
898 error
= dquot_alloc_block(inode
,
899 EXT4_C2B(EXT4_SB(sb
), 1));
902 BUFFER_TRACE(new_bh
, "get_write_access");
903 error
= ext4_journal_get_write_access(handle
,
908 le32_add_cpu(&BHDR(new_bh
)->h_refcount
, 1);
909 ea_bdebug(new_bh
, "reusing; refcount now=%d",
910 le32_to_cpu(BHDR(new_bh
)->h_refcount
));
911 ext4_xattr_block_csum_set(inode
, new_bh
);
912 unlock_buffer(new_bh
);
913 error
= ext4_handle_dirty_metadata(handle
,
919 mb_cache_entry_release(ce
);
921 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
922 /* We were modifying this block in-place. */
923 ea_bdebug(bs
->bh
, "keeping this block");
924 ext4_xattr_cache_insert(ext4_mb_cache
, bs
->bh
);
928 /* We need to allocate a new block */
929 ext4_fsblk_t goal
, block
;
931 goal
= ext4_group_first_block_no(sb
,
932 EXT4_I(inode
)->i_block_group
);
934 /* non-extent files can't have physical blocks past 2^32 */
935 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
936 goal
= goal
& EXT4_MAX_BLOCK_FILE_PHYS
;
938 block
= ext4_new_meta_blocks(handle
, inode
, goal
, 0,
943 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
944 BUG_ON(block
> EXT4_MAX_BLOCK_FILE_PHYS
);
946 ea_idebug(inode
, "creating block %llu",
947 (unsigned long long)block
);
949 new_bh
= sb_getblk(sb
, block
);
950 if (unlikely(!new_bh
)) {
953 ext4_free_blocks(handle
, inode
, NULL
, block
, 1,
954 EXT4_FREE_BLOCKS_METADATA
);
958 error
= ext4_journal_get_create_access(handle
, new_bh
);
960 unlock_buffer(new_bh
);
964 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
965 ext4_xattr_block_csum_set(inode
, new_bh
);
966 set_buffer_uptodate(new_bh
);
967 unlock_buffer(new_bh
);
968 ext4_xattr_cache_insert(ext4_mb_cache
, new_bh
);
969 error
= ext4_handle_dirty_metadata(handle
, inode
,
976 /* Update the inode. */
977 EXT4_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
979 /* Drop the previous xattr block. */
980 if (bs
->bh
&& bs
->bh
!= new_bh
)
981 ext4_xattr_release_block(handle
, inode
, bs
->bh
);
986 mb_cache_entry_release(ce
);
988 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
994 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(sb
), 1));
998 EXT4_ERROR_INODE(inode
, "bad block %llu",
999 EXT4_I(inode
)->i_file_acl
);
1005 int ext4_xattr_ibody_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
1006 struct ext4_xattr_ibody_find
*is
)
1008 struct ext4_xattr_ibody_header
*header
;
1009 struct ext4_inode
*raw_inode
;
1012 if (EXT4_I(inode
)->i_extra_isize
== 0)
1014 raw_inode
= ext4_raw_inode(&is
->iloc
);
1015 header
= IHDR(inode
, raw_inode
);
1016 is
->s
.base
= is
->s
.first
= IFIRST(header
);
1017 is
->s
.here
= is
->s
.first
;
1018 is
->s
.end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1019 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
1020 error
= xattr_check_inode(inode
, header
, is
->s
.end
);
1023 /* Find the named attribute. */
1024 error
= ext4_xattr_find_entry(&is
->s
.here
, i
->name_index
,
1025 i
->name
, is
->s
.end
-
1026 (void *)is
->s
.base
, 0);
1027 if (error
&& error
!= -ENODATA
)
1029 is
->s
.not_found
= error
;
1034 int ext4_xattr_ibody_inline_set(handle_t
*handle
, struct inode
*inode
,
1035 struct ext4_xattr_info
*i
,
1036 struct ext4_xattr_ibody_find
*is
)
1038 struct ext4_xattr_ibody_header
*header
;
1039 struct ext4_xattr_search
*s
= &is
->s
;
1042 if (EXT4_I(inode
)->i_extra_isize
== 0)
1044 error
= ext4_xattr_set_entry(i
, s
, inode
);
1047 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1048 if (!IS_LAST_ENTRY(s
->first
)) {
1049 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1050 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1052 header
->h_magic
= cpu_to_le32(0);
1053 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1058 static int ext4_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
1059 struct ext4_xattr_info
*i
,
1060 struct ext4_xattr_ibody_find
*is
)
1062 struct ext4_xattr_ibody_header
*header
;
1063 struct ext4_xattr_search
*s
= &is
->s
;
1066 if (EXT4_I(inode
)->i_extra_isize
== 0)
1068 error
= ext4_xattr_set_entry(i
, s
, inode
);
1071 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1072 if (!IS_LAST_ENTRY(s
->first
)) {
1073 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1074 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1076 header
->h_magic
= cpu_to_le32(0);
1077 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1083 * ext4_xattr_set_handle()
1085 * Create, replace or remove an extended attribute for this inode. Value
1086 * is NULL to remove an existing extended attribute, and non-NULL to
1087 * either replace an existing extended attribute, or create a new extended
1088 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1089 * specify that an extended attribute must exist and must not exist
1090 * previous to the call, respectively.
1092 * Returns 0, or a negative error number on failure.
1095 ext4_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
1096 const char *name
, const void *value
, size_t value_len
,
1099 struct ext4_xattr_info i
= {
1100 .name_index
= name_index
,
1103 .value_len
= value_len
,
1106 struct ext4_xattr_ibody_find is
= {
1107 .s
= { .not_found
= -ENODATA
, },
1109 struct ext4_xattr_block_find bs
= {
1110 .s
= { .not_found
= -ENODATA
, },
1117 if (strlen(name
) > 255)
1119 ext4_write_lock_xattr(inode
, &no_expand
);
1121 error
= ext4_reserve_inode_write(handle
, inode
, &is
.iloc
);
1125 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
)) {
1126 struct ext4_inode
*raw_inode
= ext4_raw_inode(&is
.iloc
);
1127 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
1128 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
1131 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
1135 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1138 if (is
.s
.not_found
&& bs
.s
.not_found
) {
1140 if (flags
& XATTR_REPLACE
)
1147 if (flags
& XATTR_CREATE
)
1151 if (!is
.s
.not_found
)
1152 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1153 else if (!bs
.s
.not_found
)
1154 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1156 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1157 if (!error
&& !bs
.s
.not_found
) {
1159 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1160 } else if (error
== -ENOSPC
) {
1161 if (EXT4_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
1164 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1168 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1171 if (!is
.s
.not_found
) {
1173 error
= ext4_xattr_ibody_set(handle
, inode
, &i
,
1179 ext4_xattr_update_super_block(handle
, inode
->i_sb
);
1180 inode
->i_ctime
= ext4_current_time(inode
);
1183 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
1185 * The bh is consumed by ext4_mark_iloc_dirty, even with
1190 ext4_handle_sync(handle
);
1196 ext4_write_unlock_xattr(inode
, &no_expand
);
1203 * Like ext4_xattr_set_handle, but start from an inode. This extended
1204 * attribute modification is a filesystem transaction by itself.
1206 * Returns 0, or a negative error number on failure.
1209 ext4_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
1210 const void *value
, size_t value_len
, int flags
)
1213 int error
, retries
= 0;
1214 int credits
= ext4_jbd2_credits_xattr(inode
);
1217 handle
= ext4_journal_start(inode
, EXT4_HT_XATTR
, credits
);
1218 if (IS_ERR(handle
)) {
1219 error
= PTR_ERR(handle
);
1223 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, name
,
1224 value
, value_len
, flags
);
1225 error2
= ext4_journal_stop(handle
);
1226 if (error
== -ENOSPC
&&
1227 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1237 * Shift the EA entries in the inode to create space for the increased
1240 static void ext4_xattr_shift_entries(struct ext4_xattr_entry
*entry
,
1241 int value_offs_shift
, void *to
,
1242 void *from
, size_t n
, int blocksize
)
1244 struct ext4_xattr_entry
*last
= entry
;
1247 /* Adjust the value offsets of the entries */
1248 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1249 if (!last
->e_value_block
&& last
->e_value_size
) {
1250 new_offs
= le16_to_cpu(last
->e_value_offs
) +
1252 BUG_ON(new_offs
+ le32_to_cpu(last
->e_value_size
)
1254 last
->e_value_offs
= cpu_to_le16(new_offs
);
1257 /* Shift the entries by n bytes */
1258 memmove(to
, from
, n
);
1262 * Expand an inode by new_extra_isize bytes when EAs are present.
1263 * Returns 0 on success or negative error number on failure.
1265 int ext4_expand_extra_isize_ea(struct inode
*inode
, int new_extra_isize
,
1266 struct ext4_inode
*raw_inode
, handle_t
*handle
)
1268 struct ext4_xattr_ibody_header
*header
;
1269 struct ext4_xattr_entry
*entry
, *last
, *first
;
1270 struct buffer_head
*bh
= NULL
;
1271 struct ext4_xattr_ibody_find
*is
= NULL
;
1272 struct ext4_xattr_block_find
*bs
= NULL
;
1273 char *buffer
= NULL
, *b_entry_name
= NULL
;
1274 size_t min_offs
, free
;
1276 void *base
, *start
, *end
;
1277 int error
= 0, tried_min_extra_isize
= 0;
1278 int s_min_extra_isize
= le16_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_min_extra_isize
);
1279 int isize_diff
; /* How much do we need to grow i_extra_isize */
1282 if (ext4_write_trylock_xattr(inode
, &no_expand
) == 0)
1286 isize_diff
= new_extra_isize
- EXT4_I(inode
)->i_extra_isize
;
1287 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
)
1290 header
= IHDR(inode
, raw_inode
);
1291 entry
= IFIRST(header
);
1294 * Check if enough free space is available in the inode to shift the
1295 * entries ahead by new_extra_isize.
1298 base
= start
= entry
;
1299 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1300 min_offs
= end
- base
;
1302 total_ino
= sizeof(struct ext4_xattr_ibody_header
);
1304 error
= xattr_check_inode(inode
, header
, end
);
1308 free
= ext4_xattr_free_space(last
, &min_offs
, base
, &total_ino
);
1309 if (free
>= isize_diff
) {
1310 entry
= IFIRST(header
);
1311 ext4_xattr_shift_entries(entry
, EXT4_I(inode
)->i_extra_isize
1312 - new_extra_isize
, (void *)raw_inode
+
1313 EXT4_GOOD_OLD_INODE_SIZE
+ new_extra_isize
,
1314 (void *)header
, total_ino
,
1315 inode
->i_sb
->s_blocksize
);
1316 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
1321 * Enough free space isn't available in the inode, check if
1322 * EA block can hold new_extra_isize bytes.
1324 if (EXT4_I(inode
)->i_file_acl
) {
1325 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1329 if (ext4_xattr_check_block(inode
, bh
)) {
1330 EXT4_ERROR_INODE(inode
, "bad block %llu",
1331 EXT4_I(inode
)->i_file_acl
);
1332 error
= -EFSCORRUPTED
;
1337 end
= bh
->b_data
+ bh
->b_size
;
1338 min_offs
= end
- base
;
1339 free
= ext4_xattr_free_space(first
, &min_offs
, base
, NULL
);
1340 if (free
< isize_diff
) {
1341 if (!tried_min_extra_isize
&& s_min_extra_isize
) {
1342 tried_min_extra_isize
++;
1343 new_extra_isize
= s_min_extra_isize
;
1351 free
= inode
->i_sb
->s_blocksize
;
1354 while (isize_diff
> 0) {
1355 size_t offs
, size
, entry_size
;
1356 struct ext4_xattr_entry
*small_entry
= NULL
;
1357 struct ext4_xattr_info i
= {
1361 unsigned int total_size
; /* EA entry size + value size */
1362 unsigned int shift_bytes
; /* No. of bytes to shift EAs by? */
1363 unsigned int min_total_size
= ~0U;
1365 is
= kzalloc(sizeof(struct ext4_xattr_ibody_find
), GFP_NOFS
);
1366 bs
= kzalloc(sizeof(struct ext4_xattr_block_find
), GFP_NOFS
);
1372 is
->s
.not_found
= -ENODATA
;
1373 bs
->s
.not_found
= -ENODATA
;
1377 last
= IFIRST(header
);
1378 /* Find the entry best suited to be pushed into EA block */
1380 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1381 /* never move system.data out of the inode */
1382 if ((last
->e_name_len
== 4) &&
1383 (last
->e_name_index
== EXT4_XATTR_INDEX_SYSTEM
) &&
1384 !memcmp(last
->e_name
, "data", 4))
1387 EXT4_XATTR_SIZE(le32_to_cpu(last
->e_value_size
)) +
1388 EXT4_XATTR_LEN(last
->e_name_len
);
1389 if (total_size
<= free
&& total_size
< min_total_size
) {
1390 if (total_size
< isize_diff
) {
1394 min_total_size
= total_size
;
1399 if (entry
== NULL
) {
1401 entry
= small_entry
;
1403 if (!tried_min_extra_isize
&&
1404 s_min_extra_isize
) {
1405 tried_min_extra_isize
++;
1406 new_extra_isize
= s_min_extra_isize
;
1407 kfree(is
); is
= NULL
;
1408 kfree(bs
); bs
= NULL
;
1416 offs
= le16_to_cpu(entry
->e_value_offs
);
1417 size
= le32_to_cpu(entry
->e_value_size
);
1418 entry_size
= EXT4_XATTR_LEN(entry
->e_name_len
);
1419 i
.name_index
= entry
->e_name_index
,
1420 buffer
= kmalloc(EXT4_XATTR_SIZE(size
), GFP_NOFS
);
1421 b_entry_name
= kmalloc(entry
->e_name_len
+ 1, GFP_NOFS
);
1422 if (!buffer
|| !b_entry_name
) {
1426 /* Save the entry name and the entry value */
1427 memcpy(buffer
, (void *)IFIRST(header
) + offs
,
1428 EXT4_XATTR_SIZE(size
));
1429 memcpy(b_entry_name
, entry
->e_name
, entry
->e_name_len
);
1430 b_entry_name
[entry
->e_name_len
] = '\0';
1431 i
.name
= b_entry_name
;
1433 error
= ext4_get_inode_loc(inode
, &is
->iloc
);
1437 error
= ext4_xattr_ibody_find(inode
, &i
, is
);
1441 /* Remove the chosen entry from the inode */
1442 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, is
);
1445 total_ino
-= entry_size
;
1447 entry
= IFIRST(header
);
1448 if (entry_size
+ EXT4_XATTR_SIZE(size
) >= isize_diff
)
1449 shift_bytes
= isize_diff
;
1451 shift_bytes
= entry_size
+ EXT4_XATTR_SIZE(size
);
1452 /* Adjust the offsets and shift the remaining entries ahead */
1453 ext4_xattr_shift_entries(entry
, -shift_bytes
,
1454 (void *)raw_inode
+ EXT4_GOOD_OLD_INODE_SIZE
+
1455 EXT4_I(inode
)->i_extra_isize
+ shift_bytes
,
1456 (void *)header
, total_ino
, inode
->i_sb
->s_blocksize
);
1458 isize_diff
-= shift_bytes
;
1459 EXT4_I(inode
)->i_extra_isize
+= shift_bytes
;
1460 header
= IHDR(inode
, raw_inode
);
1462 i
.name
= b_entry_name
;
1465 error
= ext4_xattr_block_find(inode
, &i
, bs
);
1469 /* Add entry which was removed from the inode into the block */
1470 error
= ext4_xattr_block_set(handle
, inode
, &i
, bs
);
1473 kfree(b_entry_name
);
1475 b_entry_name
= NULL
;
1477 brelse(is
->iloc
.bh
);
1483 ext4_write_unlock_xattr(inode
, &no_expand
);
1487 kfree(b_entry_name
);
1490 brelse(is
->iloc
.bh
);
1497 * Inode size expansion failed; don't try again
1500 ext4_write_unlock_xattr(inode
, &no_expand
);
1507 * ext4_xattr_delete_inode()
1509 * Free extended attribute resources associated with this inode. This
1510 * is called immediately before an inode is freed. We have exclusive
1511 * access to the inode.
1514 ext4_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
)
1516 struct buffer_head
*bh
= NULL
;
1518 if (!EXT4_I(inode
)->i_file_acl
)
1520 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1522 EXT4_ERROR_INODE(inode
, "block %llu read error",
1523 EXT4_I(inode
)->i_file_acl
);
1526 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
1527 BHDR(bh
)->h_blocks
!= cpu_to_le32(1)) {
1528 EXT4_ERROR_INODE(inode
, "bad block %llu",
1529 EXT4_I(inode
)->i_file_acl
);
1532 ext4_xattr_release_block(handle
, inode
, bh
);
1533 EXT4_I(inode
)->i_file_acl
= 0;
1540 * ext4_xattr_put_super()
1542 * This is called when a file system is unmounted.
1545 ext4_xattr_put_super(struct super_block
*sb
)
1547 mb_cache_shrink(sb
->s_bdev
);
1551 * ext4_xattr_cache_insert()
1553 * Create a new entry in the extended attribute cache, and insert
1554 * it unless such an entry is already in the cache.
1556 * Returns 0, or a negative error number on failure.
1559 ext4_xattr_cache_insert(struct mb_cache
*ext4_mb_cache
, struct buffer_head
*bh
)
1561 __u32 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
1562 struct mb_cache_entry
*ce
;
1565 ce
= mb_cache_entry_alloc(ext4_mb_cache
, GFP_NOFS
);
1567 ea_bdebug(bh
, "out of memory");
1570 error
= mb_cache_entry_insert(ce
, bh
->b_bdev
, bh
->b_blocknr
, hash
);
1572 mb_cache_entry_free(ce
);
1573 if (error
== -EBUSY
) {
1574 ea_bdebug(bh
, "already in cache");
1578 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
1579 mb_cache_entry_release(ce
);
1586 * Compare two extended attribute blocks for equality.
1588 * Returns 0 if the blocks are equal, 1 if they differ, and
1589 * a negative error number on errors.
1592 ext4_xattr_cmp(struct ext4_xattr_header
*header1
,
1593 struct ext4_xattr_header
*header2
)
1595 struct ext4_xattr_entry
*entry1
, *entry2
;
1597 entry1
= ENTRY(header1
+1);
1598 entry2
= ENTRY(header2
+1);
1599 while (!IS_LAST_ENTRY(entry1
)) {
1600 if (IS_LAST_ENTRY(entry2
))
1602 if (entry1
->e_hash
!= entry2
->e_hash
||
1603 entry1
->e_name_index
!= entry2
->e_name_index
||
1604 entry1
->e_name_len
!= entry2
->e_name_len
||
1605 entry1
->e_value_size
!= entry2
->e_value_size
||
1606 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
1608 if (entry1
->e_value_block
!= 0 || entry2
->e_value_block
!= 0)
1609 return -EFSCORRUPTED
;
1610 if (memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
1611 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
1612 le32_to_cpu(entry1
->e_value_size
)))
1615 entry1
= EXT4_XATTR_NEXT(entry1
);
1616 entry2
= EXT4_XATTR_NEXT(entry2
);
1618 if (!IS_LAST_ENTRY(entry2
))
1624 * ext4_xattr_cache_find()
1626 * Find an identical extended attribute block.
1628 * Returns a pointer to the block found, or NULL if such a block was
1629 * not found or an error occurred.
1631 static struct buffer_head
*
1632 ext4_xattr_cache_find(struct inode
*inode
, struct ext4_xattr_header
*header
,
1633 struct mb_cache_entry
**pce
)
1635 __u32 hash
= le32_to_cpu(header
->h_hash
);
1636 struct mb_cache_entry
*ce
;
1637 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
1639 if (!header
->h_hash
)
1640 return NULL
; /* never share */
1641 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
1643 ce
= mb_cache_entry_find_first(ext4_mb_cache
, inode
->i_sb
->s_bdev
,
1646 struct buffer_head
*bh
;
1649 if (PTR_ERR(ce
) == -EAGAIN
)
1653 bh
= sb_bread(inode
->i_sb
, ce
->e_block
);
1655 EXT4_ERROR_INODE(inode
, "block %lu read error",
1656 (unsigned long) ce
->e_block
);
1657 } else if (le32_to_cpu(BHDR(bh
)->h_refcount
) >=
1658 EXT4_XATTR_REFCOUNT_MAX
) {
1659 ea_idebug(inode
, "block %lu refcount %d>=%d",
1660 (unsigned long) ce
->e_block
,
1661 le32_to_cpu(BHDR(bh
)->h_refcount
),
1662 EXT4_XATTR_REFCOUNT_MAX
);
1663 } else if (ext4_xattr_cmp(header
, BHDR(bh
)) == 0) {
1668 ce
= mb_cache_entry_find_next(ce
, inode
->i_sb
->s_bdev
, hash
);
1673 #define NAME_HASH_SHIFT 5
1674 #define VALUE_HASH_SHIFT 16
1677 * ext4_xattr_hash_entry()
1679 * Compute the hash of an extended attribute.
1681 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header
*header
,
1682 struct ext4_xattr_entry
*entry
)
1685 char *name
= entry
->e_name
;
1688 for (n
= 0; n
< entry
->e_name_len
; n
++) {
1689 hash
= (hash
<< NAME_HASH_SHIFT
) ^
1690 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
1694 if (entry
->e_value_block
== 0 && entry
->e_value_size
!= 0) {
1695 __le32
*value
= (__le32
*)((char *)header
+
1696 le16_to_cpu(entry
->e_value_offs
));
1697 for (n
= (le32_to_cpu(entry
->e_value_size
) +
1698 EXT4_XATTR_ROUND
) >> EXT4_XATTR_PAD_BITS
; n
; n
--) {
1699 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
1700 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
1701 le32_to_cpu(*value
++);
1704 entry
->e_hash
= cpu_to_le32(hash
);
1707 #undef NAME_HASH_SHIFT
1708 #undef VALUE_HASH_SHIFT
1710 #define BLOCK_HASH_SHIFT 16
1713 * ext4_xattr_rehash()
1715 * Re-compute the extended attribute hash value after an entry has changed.
1717 static void ext4_xattr_rehash(struct ext4_xattr_header
*header
,
1718 struct ext4_xattr_entry
*entry
)
1720 struct ext4_xattr_entry
*here
;
1723 ext4_xattr_hash_entry(header
, entry
);
1724 here
= ENTRY(header
+1);
1725 while (!IS_LAST_ENTRY(here
)) {
1726 if (!here
->e_hash
) {
1727 /* Block is not shared if an entry's hash value == 0 */
1731 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
1732 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
1733 le32_to_cpu(here
->e_hash
);
1734 here
= EXT4_XATTR_NEXT(here
);
1736 header
->h_hash
= cpu_to_le32(hash
);
1739 #undef BLOCK_HASH_SHIFT
1741 #define HASH_BUCKET_BITS 10
1744 ext4_xattr_create_cache(char *name
)
1746 return mb_cache_create(name
, HASH_BUCKET_BITS
);
1749 void ext4_xattr_destroy_cache(struct mb_cache
*cache
)
1752 mb_cache_destroy(cache
);