1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/xattr.c
9 * Extended attributes for symlinks and special files added per
18 * Extended attributes are stored directly in inodes (on file systems with
19 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
20 * field contains the block number if an inode uses an additional block. All
21 * attributes must fit in the inode and one additional block. Blocks that
22 * contain the identical set of attributes may be shared among several inodes.
23 * Identical blocks are detected by keeping a cache of blocks that have
24 * recently been accessed.
26 * The attributes in inodes and on blocks have a different header; the entries
27 * are stored in the same format:
29 * +------------------+
32 * | entry 2 | | growing downwards
37 * | value 3 | | growing upwards
39 * +------------------+
41 * The header is followed by multiple entry descriptors. In disk blocks, the
42 * entry descriptors are kept sorted. In inodes, they are unsorted. The
43 * attribute values are aligned to the end of the block in no specific order.
47 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
48 * EA blocks are only changed if they are exclusive to an inode, so
49 * holding xattr_sem also means that nothing but the EA block's reference
50 * count can change. Multiple writers to the same block are synchronized
54 #include <linux/init.h>
56 #include <linux/slab.h>
57 #include <linux/mbcache.h>
58 #include <linux/quotaops.h>
59 #include <linux/iversion.h>
60 #include "ext4_jbd2.h"
65 #ifdef EXT4_XATTR_DEBUG
66 # define ea_idebug(inode, fmt, ...) \
67 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
68 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
69 # define ea_bdebug(bh, fmt, ...) \
70 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
71 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
73 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
74 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
77 static void ext4_xattr_block_cache_insert(struct mb_cache *,
78 struct buffer_head *);
79 static struct buffer_head *
80 ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
81 struct mb_cache_entry **);
82 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
84 static __le32 ext4_xattr_hash_entry_signed(char *name, size_t name_len, __le32 *value,
86 static void ext4_xattr_rehash(struct ext4_xattr_header *);
88 static const struct xattr_handler * const ext4_xattr_handler_map[] = {
89 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
90 #ifdef CONFIG_EXT4_FS_POSIX_ACL
91 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &nop_posix_acl_access,
92 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &nop_posix_acl_default,
94 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
95 #ifdef CONFIG_EXT4_FS_SECURITY
96 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
98 [EXT4_XATTR_INDEX_HURD] = &ext4_xattr_hurd_handler,
101 const struct xattr_handler *ext4_xattr_handlers[] = {
102 &ext4_xattr_user_handler,
103 &ext4_xattr_trusted_handler,
104 #ifdef CONFIG_EXT4_FS_SECURITY
105 &ext4_xattr_security_handler,
107 &ext4_xattr_hurd_handler,
111 #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
112 inode->i_sb->s_fs_info)->s_ea_block_cache)
114 #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
115 inode->i_sb->s_fs_info)->s_ea_inode_cache)
118 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
119 struct inode *inode);
121 #ifdef CONFIG_LOCKDEP
122 void ext4_xattr_inode_set_class(struct inode *ea_inode)
124 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
128 static __le32 ext4_xattr_block_csum(struct inode *inode,
130 struct ext4_xattr_header *hdr)
132 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
134 __le64 dsk_block_nr = cpu_to_le64(block_nr);
135 __u32 dummy_csum = 0;
136 int offset = offsetof(struct ext4_xattr_header, h_checksum);
138 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
139 sizeof(dsk_block_nr));
140 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
141 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
142 offset += sizeof(dummy_csum);
143 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
144 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
146 return cpu_to_le32(csum);
149 static int ext4_xattr_block_csum_verify(struct inode *inode,
150 struct buffer_head *bh)
152 struct ext4_xattr_header *hdr = BHDR(bh);
155 if (ext4_has_metadata_csum(inode->i_sb)) {
157 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
158 bh->b_blocknr, hdr));
164 static void ext4_xattr_block_csum_set(struct inode *inode,
165 struct buffer_head *bh)
167 if (ext4_has_metadata_csum(inode->i_sb))
168 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
169 bh->b_blocknr, BHDR(bh));
172 static inline const char *ext4_xattr_prefix(int name_index,
173 struct dentry *dentry)
175 const struct xattr_handler *handler = NULL;
177 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
178 handler = ext4_xattr_handler_map[name_index];
180 if (!xattr_handler_can_list(handler, dentry))
183 return xattr_prefix(handler);
187 check_xattrs(struct inode *inode, struct buffer_head *bh,
188 struct ext4_xattr_entry *entry, void *end, void *value_start,
189 const char *function, unsigned int line)
191 struct ext4_xattr_entry *e = entry;
192 int err = -EFSCORRUPTED;
196 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
197 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
198 err_str = "invalid header";
201 if (buffer_verified(bh))
203 if (!ext4_xattr_block_csum_verify(inode, bh)) {
205 err_str = "invalid checksum";
209 struct ext4_xattr_ibody_header *header = value_start;
212 if (end - (void *)header < sizeof(*header) + sizeof(u32)) {
213 err_str = "in-inode xattr block too small";
216 if (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
217 err_str = "bad magic number in in-inode xattr";
222 /* Find the end of the names list */
223 while (!IS_LAST_ENTRY(e)) {
224 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
225 if ((void *)next >= end) {
226 err_str = "e_name out of bounds";
229 if (strnlen(e->e_name, e->e_name_len) != e->e_name_len) {
230 err_str = "bad e_name length";
236 /* Check the values */
237 while (!IS_LAST_ENTRY(entry)) {
238 u32 size = le32_to_cpu(entry->e_value_size);
239 unsigned long ea_ino = le32_to_cpu(entry->e_value_inum);
241 if (!ext4_has_feature_ea_inode(inode->i_sb) && ea_ino) {
242 err_str = "ea_inode specified without ea_inode feature enabled";
245 if (ea_ino && ((ea_ino == EXT4_ROOT_INO) ||
246 !ext4_valid_inum(inode->i_sb, ea_ino))) {
247 err_str = "invalid ea_ino";
250 if (size > EXT4_XATTR_SIZE_MAX) {
251 err_str = "e_value size too large";
255 if (size != 0 && entry->e_value_inum == 0) {
256 u16 offs = le16_to_cpu(entry->e_value_offs);
260 * The value cannot overlap the names, and the value
261 * with padding cannot extend beyond 'end'. Check both
262 * the padded and unpadded sizes, since the size may
263 * overflow to 0 when adding padding.
265 if (offs > end - value_start) {
266 err_str = "e_value out of bounds";
269 value = value_start + offs;
270 if (value < (void *)e + sizeof(u32) ||
271 size > end - value ||
272 EXT4_XATTR_SIZE(size) > end - value) {
273 err_str = "overlapping e_value ";
277 entry = EXT4_XATTR_NEXT(entry);
280 set_buffer_verified(bh);
285 __ext4_error_inode(inode, function, line, 0, -err,
286 "corrupted xattr block %llu: %s",
287 (unsigned long long) bh->b_blocknr,
290 __ext4_error_inode(inode, function, line, 0, -err,
291 "corrupted in-inode xattr: %s", err_str);
296 __ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh,
297 const char *function, unsigned int line)
299 return check_xattrs(inode, bh, BFIRST(bh), bh->b_data + bh->b_size,
300 bh->b_data, function, line);
303 #define ext4_xattr_check_block(inode, bh) \
304 __ext4_xattr_check_block((inode), (bh), __func__, __LINE__)
308 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
309 void *end, const char *function, unsigned int line)
311 return check_xattrs(inode, NULL, IFIRST(header), end, IFIRST(header),
315 #define xattr_check_inode(inode, header, end) \
316 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
319 xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry,
320 void *end, int name_index, const char *name, int sorted)
322 struct ext4_xattr_entry *entry, *next;
328 name_len = strlen(name);
329 for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) {
330 next = EXT4_XATTR_NEXT(entry);
331 if ((void *) next >= end) {
332 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
333 return -EFSCORRUPTED;
335 cmp = name_index - entry->e_name_index;
337 cmp = name_len - entry->e_name_len;
339 cmp = memcmp(name, entry->e_name, name_len);
340 if (cmp <= 0 && (sorted || cmp == 0))
344 return cmp ? -ENODATA : 0;
348 ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
350 return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
353 static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
355 return ((u64)ea_inode->i_ctime.tv_sec << 32) |
356 (u32) inode_peek_iversion_raw(ea_inode);
359 static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
361 ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
362 inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
365 static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
367 return (u32)ea_inode->i_atime.tv_sec;
370 static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
372 ea_inode->i_atime.tv_sec = hash;
376 * Read the EA value from an inode.
378 static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
380 int blocksize = 1 << ea_inode->i_blkbits;
381 int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
382 int tail_size = (size % blocksize) ?: blocksize;
383 struct buffer_head *bhs_inline[8];
384 struct buffer_head **bhs = bhs_inline;
387 if (bh_count > ARRAY_SIZE(bhs_inline)) {
388 bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
393 ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
394 true /* wait */, bhs);
398 for (i = 0; i < bh_count; i++) {
399 /* There shouldn't be any holes in ea_inode. */
404 memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
405 i < bh_count - 1 ? blocksize : tail_size);
409 for (i = 0; i < bh_count; i++)
412 if (bhs != bhs_inline)
417 #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
419 static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
420 u32 ea_inode_hash, struct inode **ea_inode)
426 * We have to check for this corruption early as otherwise
427 * iget_locked() could wait indefinitely for the state of our
430 if (parent->i_ino == ea_ino) {
431 ext4_error(parent->i_sb,
432 "Parent and EA inode have the same ino %lu", ea_ino);
433 return -EFSCORRUPTED;
436 inode = ext4_iget(parent->i_sb, ea_ino, EXT4_IGET_NORMAL);
438 err = PTR_ERR(inode);
439 ext4_error(parent->i_sb,
440 "error while reading EA inode %lu err=%d", ea_ino,
445 if (is_bad_inode(inode)) {
446 ext4_error(parent->i_sb,
447 "error while reading EA inode %lu is_bad_inode",
453 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
454 ext4_error(parent->i_sb,
455 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
461 ext4_xattr_inode_set_class(inode);
464 * Check whether this is an old Lustre-style xattr inode. Lustre
465 * implementation does not have hash validation, rather it has a
466 * backpointer from ea_inode to the parent inode.
468 if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
469 EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
470 inode->i_generation == parent->i_generation) {
471 ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
472 ext4_xattr_inode_set_ref(inode, 1);
475 inode->i_flags |= S_NOQUOTA;
486 /* Remove entry from mbcache when EA inode is getting evicted */
487 void ext4_evict_ea_inode(struct inode *inode)
489 struct mb_cache_entry *oe;
491 if (!EA_INODE_CACHE(inode))
493 /* Wait for entry to get unused so that we can remove it */
494 while ((oe = mb_cache_entry_delete_or_get(EA_INODE_CACHE(inode),
495 ext4_xattr_inode_get_hash(inode), inode->i_ino))) {
496 mb_cache_entry_wait_unused(oe);
497 mb_cache_entry_put(EA_INODE_CACHE(inode), oe);
502 ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
503 struct ext4_xattr_entry *entry, void *buffer,
508 /* Verify stored hash matches calculated hash. */
509 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
510 if (hash != ext4_xattr_inode_get_hash(ea_inode))
511 return -EFSCORRUPTED;
514 __le32 e_hash, tmp_data;
516 /* Verify entry hash. */
517 tmp_data = cpu_to_le32(hash);
518 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
521 if (e_hash == entry->e_hash)
525 * Not good. Maybe the entry hash was calculated
526 * using the buggy signed char version?
528 e_hash = ext4_xattr_hash_entry_signed(entry->e_name, entry->e_name_len,
530 /* Still no match - bad */
531 if (e_hash != entry->e_hash)
532 return -EFSCORRUPTED;
534 /* Let people know about old hash */
535 pr_warn_once("ext4: filesystem with signed xattr name hash");
541 * Read xattr value from the EA inode.
544 ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
545 void *buffer, size_t size)
547 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
548 struct inode *ea_inode;
551 err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
552 le32_to_cpu(entry->e_hash), &ea_inode);
558 if (i_size_read(ea_inode) != size) {
559 ext4_warning_inode(ea_inode,
560 "ea_inode file size=%llu entry size=%zu",
561 i_size_read(ea_inode), size);
566 err = ext4_xattr_inode_read(ea_inode, buffer, size);
570 if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
571 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
574 ext4_warning_inode(ea_inode,
575 "EA inode hash validation failed");
580 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
581 ext4_xattr_inode_get_hash(ea_inode),
582 ea_inode->i_ino, true /* reusable */);
590 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
591 void *buffer, size_t buffer_size)
593 struct buffer_head *bh = NULL;
594 struct ext4_xattr_entry *entry;
598 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
600 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
601 name_index, name, buffer, (long)buffer_size);
603 if (!EXT4_I(inode)->i_file_acl)
605 ea_idebug(inode, "reading block %llu",
606 (unsigned long long)EXT4_I(inode)->i_file_acl);
607 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
610 ea_bdebug(bh, "b_count=%d, refcount=%d",
611 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
612 error = ext4_xattr_check_block(inode, bh);
615 ext4_xattr_block_cache_insert(ea_block_cache, bh);
617 end = bh->b_data + bh->b_size;
618 error = xattr_find_entry(inode, &entry, end, name_index, name, 1);
621 size = le32_to_cpu(entry->e_value_size);
623 if (unlikely(size > EXT4_XATTR_SIZE_MAX))
626 if (size > buffer_size)
628 if (entry->e_value_inum) {
629 error = ext4_xattr_inode_get(inode, entry, buffer,
634 u16 offset = le16_to_cpu(entry->e_value_offs);
635 void *p = bh->b_data + offset;
637 if (unlikely(p + size > end))
639 memcpy(buffer, p, size);
650 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
651 void *buffer, size_t buffer_size)
653 struct ext4_xattr_ibody_header *header;
654 struct ext4_xattr_entry *entry;
655 struct ext4_inode *raw_inode;
656 struct ext4_iloc iloc;
661 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
663 error = ext4_get_inode_loc(inode, &iloc);
666 raw_inode = ext4_raw_inode(&iloc);
667 header = IHDR(inode, raw_inode);
668 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
669 error = xattr_check_inode(inode, header, end);
672 entry = IFIRST(header);
673 error = xattr_find_entry(inode, &entry, end, name_index, name, 0);
676 size = le32_to_cpu(entry->e_value_size);
678 if (unlikely(size > EXT4_XATTR_SIZE_MAX))
681 if (size > buffer_size)
683 if (entry->e_value_inum) {
684 error = ext4_xattr_inode_get(inode, entry, buffer,
689 u16 offset = le16_to_cpu(entry->e_value_offs);
690 void *p = (void *)IFIRST(header) + offset;
692 if (unlikely(p + size > end))
694 memcpy(buffer, p, size);
707 * Copy an extended attribute into the buffer
708 * provided, or compute the buffer size required.
709 * Buffer is NULL to compute the size of the buffer required.
711 * Returns a negative error number on failure, or the number of bytes
712 * used / required on success.
715 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
716 void *buffer, size_t buffer_size)
720 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
723 if (strlen(name) > 255)
726 down_read(&EXT4_I(inode)->xattr_sem);
727 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
729 if (error == -ENODATA)
730 error = ext4_xattr_block_get(inode, name_index, name, buffer,
732 up_read(&EXT4_I(inode)->xattr_sem);
737 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
738 char *buffer, size_t buffer_size)
740 size_t rest = buffer_size;
742 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
745 prefix = ext4_xattr_prefix(entry->e_name_index, dentry);
747 size_t prefix_len = strlen(prefix);
748 size_t size = prefix_len + entry->e_name_len + 1;
753 memcpy(buffer, prefix, prefix_len);
754 buffer += prefix_len;
755 memcpy(buffer, entry->e_name, entry->e_name_len);
756 buffer += entry->e_name_len;
762 return buffer_size - rest; /* total size */
766 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
768 struct inode *inode = d_inode(dentry);
769 struct buffer_head *bh = NULL;
772 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
773 buffer, (long)buffer_size);
775 if (!EXT4_I(inode)->i_file_acl)
777 ea_idebug(inode, "reading block %llu",
778 (unsigned long long)EXT4_I(inode)->i_file_acl);
779 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
782 ea_bdebug(bh, "b_count=%d, refcount=%d",
783 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
784 error = ext4_xattr_check_block(inode, bh);
787 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
788 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer,
796 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
798 struct inode *inode = d_inode(dentry);
799 struct ext4_xattr_ibody_header *header;
800 struct ext4_inode *raw_inode;
801 struct ext4_iloc iloc;
805 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
807 error = ext4_get_inode_loc(inode, &iloc);
810 raw_inode = ext4_raw_inode(&iloc);
811 header = IHDR(inode, raw_inode);
812 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
813 error = xattr_check_inode(inode, header, end);
816 error = ext4_xattr_list_entries(dentry, IFIRST(header),
817 buffer, buffer_size);
825 * Inode operation listxattr()
827 * d_inode(dentry)->i_rwsem: don't care
829 * Copy a list of attribute names into the buffer
830 * provided, or compute the buffer size required.
831 * Buffer is NULL to compute the size of the buffer required.
833 * Returns a negative error number on failure, or the number of bytes
834 * used / required on success.
837 ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
841 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
842 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
849 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
854 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
859 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
862 static void ext4_xattr_update_super_block(handle_t *handle,
863 struct super_block *sb)
865 if (ext4_has_feature_xattr(sb))
868 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
869 if (ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh,
870 EXT4_JTR_NONE) == 0) {
871 lock_buffer(EXT4_SB(sb)->s_sbh);
872 ext4_set_feature_xattr(sb);
873 ext4_superblock_csum_set(sb);
874 unlock_buffer(EXT4_SB(sb)->s_sbh);
875 ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
879 int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
881 struct ext4_iloc iloc = { .bh = NULL };
882 struct buffer_head *bh = NULL;
883 struct ext4_inode *raw_inode;
884 struct ext4_xattr_ibody_header *header;
885 struct ext4_xattr_entry *entry;
886 qsize_t ea_inode_refs = 0;
890 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
892 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
893 ret = ext4_get_inode_loc(inode, &iloc);
896 raw_inode = ext4_raw_inode(&iloc);
897 header = IHDR(inode, raw_inode);
898 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
899 ret = xattr_check_inode(inode, header, end);
903 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
904 entry = EXT4_XATTR_NEXT(entry))
905 if (entry->e_value_inum)
909 if (EXT4_I(inode)->i_file_acl) {
910 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
917 ret = ext4_xattr_check_block(inode, bh);
921 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
922 entry = EXT4_XATTR_NEXT(entry))
923 if (entry->e_value_inum)
926 *usage = ea_inode_refs + 1;
934 static inline size_t round_up_cluster(struct inode *inode, size_t length)
936 struct super_block *sb = inode->i_sb;
937 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
939 size_t mask = ~(cluster_size - 1);
941 return (length + cluster_size - 1) & mask;
944 static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
948 err = dquot_alloc_inode(inode);
951 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
953 dquot_free_inode(inode);
957 static void ext4_xattr_inode_free_quota(struct inode *parent,
958 struct inode *ea_inode,
962 ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
964 dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
965 dquot_free_inode(parent);
968 int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
969 struct buffer_head *block_bh, size_t value_len,
976 * 1) Owner inode update
977 * 2) Ref count update on old xattr block
979 * 4) block bitmap update for new xattr block
980 * 5) group descriptor for new xattr block
981 * 6) block bitmap update for old xattr block
982 * 7) group descriptor for old block
984 * 6 & 7 can happen if we have two racing threads T_a and T_b
985 * which are each trying to set an xattr on inodes I_a and I_b
986 * which were both initially sharing an xattr block.
991 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
994 * In case of inline data, we may push out the data to a block,
995 * so we need to reserve credits for this eventuality
997 if (inode && ext4_has_inline_data(inode))
998 credits += ext4_writepage_trans_blocks(inode) + 1;
1000 /* We are done if ea_inode feature is not enabled. */
1001 if (!ext4_has_feature_ea_inode(sb))
1004 /* New ea_inode, inode map, block bitmap, group descriptor. */
1008 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1010 /* Indirection block or one level of extent tree. */
1013 /* Block bitmap and group descriptor updates for each block. */
1014 credits += blocks * 2;
1016 /* Blocks themselves. */
1020 /* Dereference ea_inode holding old xattr value.
1021 * Old ea_inode, inode map, block bitmap, group descriptor.
1025 /* Data blocks for old ea_inode. */
1026 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
1028 /* Indirection block or one level of extent tree for old
1033 /* Block bitmap and group descriptor updates for each block. */
1034 credits += blocks * 2;
1037 /* We may need to clone the existing xattr block in which case we need
1038 * to increment ref counts for existing ea_inodes referenced by it.
1041 struct ext4_xattr_entry *entry = BFIRST(block_bh);
1043 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
1044 if (entry->e_value_inum)
1045 /* Ref count update on ea_inode. */
1051 static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
1054 struct ext4_iloc iloc;
1058 inode_lock(ea_inode);
1060 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
1064 ref_count = ext4_xattr_inode_get_ref(ea_inode);
1065 ref_count += ref_change;
1066 ext4_xattr_inode_set_ref(ea_inode, ref_count);
1068 if (ref_change > 0) {
1069 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
1070 ea_inode->i_ino, ref_count);
1072 if (ref_count == 1) {
1073 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
1074 ea_inode->i_ino, ea_inode->i_nlink);
1076 set_nlink(ea_inode, 1);
1077 ext4_orphan_del(handle, ea_inode);
1080 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1081 ea_inode->i_ino, ref_count);
1083 if (ref_count == 0) {
1084 WARN_ONCE(ea_inode->i_nlink != 1,
1085 "EA inode %lu i_nlink=%u",
1086 ea_inode->i_ino, ea_inode->i_nlink);
1088 clear_nlink(ea_inode);
1089 ext4_orphan_add(handle, ea_inode);
1093 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1095 ext4_warning_inode(ea_inode,
1096 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1098 inode_unlock(ea_inode);
1102 static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1104 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1107 static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1109 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1112 static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1113 struct ext4_xattr_entry *first)
1115 struct inode *ea_inode;
1116 struct ext4_xattr_entry *entry;
1117 struct ext4_xattr_entry *failed_entry;
1118 unsigned int ea_ino;
1121 for (entry = first; !IS_LAST_ENTRY(entry);
1122 entry = EXT4_XATTR_NEXT(entry)) {
1123 if (!entry->e_value_inum)
1125 ea_ino = le32_to_cpu(entry->e_value_inum);
1126 err = ext4_xattr_inode_iget(parent, ea_ino,
1127 le32_to_cpu(entry->e_hash),
1131 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1133 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1143 failed_entry = entry;
1145 for (entry = first; entry != failed_entry;
1146 entry = EXT4_XATTR_NEXT(entry)) {
1147 if (!entry->e_value_inum)
1149 ea_ino = le32_to_cpu(entry->e_value_inum);
1150 err = ext4_xattr_inode_iget(parent, ea_ino,
1151 le32_to_cpu(entry->e_hash),
1154 ext4_warning(parent->i_sb,
1155 "cleanup ea_ino %u iget error %d", ea_ino,
1159 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1161 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1168 static int ext4_xattr_restart_fn(handle_t *handle, struct inode *inode,
1169 struct buffer_head *bh, bool block_csum, bool dirty)
1175 ext4_xattr_block_csum_set(inode, bh);
1176 error = ext4_handle_dirty_metadata(handle, NULL, bh);
1178 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
1187 ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1188 struct buffer_head *bh,
1189 struct ext4_xattr_entry *first, bool block_csum,
1190 struct ext4_xattr_inode_array **ea_inode_array,
1191 int extra_credits, bool skip_quota)
1193 struct inode *ea_inode;
1194 struct ext4_xattr_entry *entry;
1196 unsigned int ea_ino;
1200 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1201 credits = 2 + extra_credits;
1203 for (entry = first; !IS_LAST_ENTRY(entry);
1204 entry = EXT4_XATTR_NEXT(entry)) {
1205 if (!entry->e_value_inum)
1207 ea_ino = le32_to_cpu(entry->e_value_inum);
1208 err = ext4_xattr_inode_iget(parent, ea_ino,
1209 le32_to_cpu(entry->e_hash),
1214 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1216 ext4_warning_inode(ea_inode,
1217 "Expand inode array err=%d", err);
1222 err = ext4_journal_ensure_credits_fn(handle, credits, credits,
1223 ext4_free_metadata_revoke_credits(parent->i_sb, 1),
1224 ext4_xattr_restart_fn(handle, parent, bh, block_csum,
1227 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1232 err = ext4_journal_get_write_access(handle,
1233 parent->i_sb, bh, EXT4_JTR_NONE);
1235 ext4_warning_inode(ea_inode,
1236 "Re-get write access err=%d",
1242 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1244 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1250 ext4_xattr_inode_free_quota(parent, ea_inode,
1251 le32_to_cpu(entry->e_value_size));
1254 * Forget about ea_inode within the same transaction that
1255 * decrements the ref count. This avoids duplicate decrements in
1256 * case the rest of the work spills over to subsequent
1259 entry->e_value_inum = 0;
1260 entry->e_value_size = 0;
1267 * Note that we are deliberately skipping csum calculation for
1268 * the final update because we do not expect any journal
1269 * restarts until xattr block is freed.
1272 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1274 ext4_warning_inode(parent,
1275 "handle dirty metadata err=%d", err);
1280 * Release the xattr block BH: If the reference count is > 1, decrement it;
1281 * otherwise free the block.
1284 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
1285 struct buffer_head *bh,
1286 struct ext4_xattr_inode_array **ea_inode_array,
1289 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1293 BUFFER_TRACE(bh, "get_write_access");
1294 error = ext4_journal_get_write_access(handle, inode->i_sb, bh,
1301 hash = le32_to_cpu(BHDR(bh)->h_hash);
1302 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1304 ea_bdebug(bh, "refcount now=0; freeing");
1306 * This must happen under buffer lock for
1307 * ext4_xattr_block_set() to reliably detect freed block
1309 if (ea_block_cache) {
1310 struct mb_cache_entry *oe;
1312 oe = mb_cache_entry_delete_or_get(ea_block_cache, hash,
1316 mb_cache_entry_wait_unused(oe);
1317 mb_cache_entry_put(ea_block_cache, oe);
1324 if (ext4_has_feature_ea_inode(inode->i_sb))
1325 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1327 true /* block_csum */,
1330 true /* skip_quota */);
1331 ext4_free_blocks(handle, inode, bh, 0, 1,
1332 EXT4_FREE_BLOCKS_METADATA |
1333 EXT4_FREE_BLOCKS_FORGET);
1336 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1337 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1338 struct mb_cache_entry *ce;
1340 if (ea_block_cache) {
1341 ce = mb_cache_entry_get(ea_block_cache, hash,
1344 set_bit(MBE_REUSABLE_B, &ce->e_flags);
1345 mb_cache_entry_put(ea_block_cache, ce);
1350 ext4_xattr_block_csum_set(inode, bh);
1352 * Beware of this ugliness: Releasing of xattr block references
1353 * from different inodes can race and so we have to protect
1354 * from a race where someone else frees the block (and releases
1355 * its journal_head) before we are done dirtying the buffer. In
1356 * nojournal mode this race is harmless and we actually cannot
1357 * call ext4_handle_dirty_metadata() with locked buffer as
1358 * that function can call sync_dirty_buffer() so for that case
1359 * we handle the dirtying after unlocking the buffer.
1361 if (ext4_handle_valid(handle))
1362 error = ext4_handle_dirty_metadata(handle, inode, bh);
1364 if (!ext4_handle_valid(handle))
1365 error = ext4_handle_dirty_metadata(handle, inode, bh);
1367 ext4_handle_sync(handle);
1368 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
1369 ea_bdebug(bh, "refcount now=%d; releasing",
1370 le32_to_cpu(BHDR(bh)->h_refcount));
1373 ext4_std_error(inode->i_sb, error);
1378 * Find the available free space for EAs. This also returns the total number of
1379 * bytes used by EA entries.
1381 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1382 size_t *min_offs, void *base, int *total)
1384 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1385 if (!last->e_value_inum && last->e_value_size) {
1386 size_t offs = le16_to_cpu(last->e_value_offs);
1387 if (offs < *min_offs)
1391 *total += EXT4_XATTR_LEN(last->e_name_len);
1393 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1397 * Write the value of the EA in an inode.
1399 static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1400 const void *buf, int bufsize)
1402 struct buffer_head *bh = NULL;
1403 unsigned long block = 0;
1404 int blocksize = ea_inode->i_sb->s_blocksize;
1405 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
1406 int csize, wsize = 0;
1407 int ret = 0, ret2 = 0;
1411 while (ret >= 0 && ret < max_blocks) {
1412 struct ext4_map_blocks map;
1413 map.m_lblk = block += ret;
1414 map.m_len = max_blocks -= ret;
1416 ret = ext4_map_blocks(handle, ea_inode, &map,
1417 EXT4_GET_BLOCKS_CREATE);
1419 ext4_mark_inode_dirty(handle, ea_inode);
1420 if (ret == -ENOSPC &&
1421 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1433 while (wsize < bufsize) {
1435 csize = (bufsize - wsize) > blocksize ? blocksize :
1437 bh = ext4_getblk(handle, ea_inode, block, 0);
1442 EXT4_ERROR_INODE(ea_inode,
1443 "ext4_getblk() return bh = NULL");
1444 return -EFSCORRUPTED;
1446 ret = ext4_journal_get_write_access(handle, ea_inode->i_sb, bh,
1451 memcpy(bh->b_data, buf, csize);
1452 set_buffer_uptodate(bh);
1453 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1460 inode_lock(ea_inode);
1461 i_size_write(ea_inode, wsize);
1462 ext4_update_i_disksize(ea_inode, wsize);
1463 inode_unlock(ea_inode);
1465 ret2 = ext4_mark_inode_dirty(handle, ea_inode);
1466 if (unlikely(ret2 && !ret))
1476 * Create an inode to store the value of a large EA.
1478 static struct inode *ext4_xattr_inode_create(handle_t *handle,
1479 struct inode *inode, u32 hash)
1481 struct inode *ea_inode = NULL;
1482 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
1485 if (inode->i_sb->s_root == NULL) {
1486 ext4_warning(inode->i_sb,
1487 "refuse to create EA inode when umounting");
1489 return ERR_PTR(-EINVAL);
1493 * Let the next inode be the goal, so we try and allocate the EA inode
1494 * in the same group, or nearby one.
1496 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
1497 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
1499 if (!IS_ERR(ea_inode)) {
1500 ea_inode->i_op = &ext4_file_inode_operations;
1501 ea_inode->i_fop = &ext4_file_operations;
1502 ext4_set_aops(ea_inode);
1503 ext4_xattr_inode_set_class(ea_inode);
1504 unlock_new_inode(ea_inode);
1505 ext4_xattr_inode_set_ref(ea_inode, 1);
1506 ext4_xattr_inode_set_hash(ea_inode, hash);
1507 err = ext4_mark_inode_dirty(handle, ea_inode);
1509 err = ext4_inode_attach_jinode(ea_inode);
1511 if (ext4_xattr_inode_dec_ref(handle, ea_inode))
1512 ext4_warning_inode(ea_inode,
1513 "cleanup dec ref error %d", err);
1515 return ERR_PTR(err);
1519 * Xattr inodes are shared therefore quota charging is performed
1520 * at a higher level.
1522 dquot_free_inode(ea_inode);
1523 dquot_drop(ea_inode);
1524 inode_lock(ea_inode);
1525 ea_inode->i_flags |= S_NOQUOTA;
1526 inode_unlock(ea_inode);
1532 static struct inode *
1533 ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1534 size_t value_len, u32 hash)
1536 struct inode *ea_inode;
1537 struct mb_cache_entry *ce;
1538 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1541 if (!ea_inode_cache)
1544 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1548 WARN_ON_ONCE(ext4_handle_valid(journal_current_handle()) &&
1549 !(current->flags & PF_MEMALLOC_NOFS));
1551 ea_data = kvmalloc(value_len, GFP_KERNEL);
1553 mb_cache_entry_put(ea_inode_cache, ce);
1558 ea_inode = ext4_iget(inode->i_sb, ce->e_value,
1560 if (!IS_ERR(ea_inode) &&
1561 !is_bad_inode(ea_inode) &&
1562 (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
1563 i_size_read(ea_inode) == value_len &&
1564 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
1565 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1567 !memcmp(value, ea_data, value_len)) {
1568 mb_cache_entry_touch(ea_inode_cache, ce);
1569 mb_cache_entry_put(ea_inode_cache, ce);
1574 if (!IS_ERR(ea_inode))
1576 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1583 * Add value of the EA in an inode.
1585 static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1586 const void *value, size_t value_len,
1587 struct inode **ret_inode)
1589 struct inode *ea_inode;
1593 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1594 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1596 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1602 *ret_inode = ea_inode;
1606 /* Create an inode for the EA value */
1607 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
1608 if (IS_ERR(ea_inode))
1609 return PTR_ERR(ea_inode);
1611 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
1613 if (ext4_xattr_inode_dec_ref(handle, ea_inode))
1614 ext4_warning_inode(ea_inode, "cleanup dec ref error %d", err);
1619 if (EA_INODE_CACHE(inode))
1620 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1621 ea_inode->i_ino, true /* reusable */);
1623 *ret_inode = ea_inode;
1628 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1629 * feature is enabled.
1631 #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1633 static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1634 struct ext4_xattr_search *s,
1635 handle_t *handle, struct inode *inode,
1638 struct ext4_xattr_entry *last, *next;
1639 struct ext4_xattr_entry *here = s->here;
1640 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
1641 int in_inode = i->in_inode;
1642 struct inode *old_ea_inode = NULL;
1643 struct inode *new_ea_inode = NULL;
1644 size_t old_size, new_size;
1647 /* Space used by old and new values. */
1648 old_size = (!s->not_found && !here->e_value_inum) ?
1649 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1650 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1653 * Optimization for the simple case when old and new values have the
1654 * same padded sizes. Not applicable if external inodes are involved.
1656 if (new_size && new_size == old_size) {
1657 size_t offs = le16_to_cpu(here->e_value_offs);
1658 void *val = s->base + offs;
1660 here->e_value_size = cpu_to_le32(i->value_len);
1661 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1662 memset(val, 0, new_size);
1664 memcpy(val, i->value, i->value_len);
1665 /* Clear padding bytes. */
1666 memset(val + i->value_len, 0, new_size - i->value_len);
1671 /* Compute min_offs and last. */
1673 for (; !IS_LAST_ENTRY(last); last = next) {
1674 next = EXT4_XATTR_NEXT(last);
1675 if ((void *)next >= s->end) {
1676 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
1677 ret = -EFSCORRUPTED;
1680 if (!last->e_value_inum && last->e_value_size) {
1681 size_t offs = le16_to_cpu(last->e_value_offs);
1682 if (offs < min_offs)
1687 /* Check whether we have enough space. */
1691 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1693 free += EXT4_XATTR_LEN(name_len) + old_size;
1695 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1701 * If storing the value in an external inode is an option,
1702 * reserve space for xattr entries/names in the external
1703 * attribute block so that a long value does not occupy the
1704 * whole space and prevent further entries being added.
1706 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1707 new_size && is_block &&
1708 (min_offs + old_size - new_size) <
1709 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1716 * Getting access to old and new ea inodes is subject to failures.
1717 * Finish that work before doing any modifications to the xattr data.
1719 if (!s->not_found && here->e_value_inum) {
1720 ret = ext4_xattr_inode_iget(inode,
1721 le32_to_cpu(here->e_value_inum),
1722 le32_to_cpu(here->e_hash),
1725 old_ea_inode = NULL;
1729 if (i->value && in_inode) {
1730 WARN_ON_ONCE(!i->value_len);
1732 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1736 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1740 new_ea_inode = NULL;
1741 ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
1747 /* We are ready to release ref count on the old_ea_inode. */
1748 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1750 /* Release newly required ref count on new_ea_inode. */
1754 err = ext4_xattr_inode_dec_ref(handle,
1757 ext4_warning_inode(new_ea_inode,
1758 "dec ref new_ea_inode err=%d",
1760 ext4_xattr_inode_free_quota(inode, new_ea_inode,
1766 ext4_xattr_inode_free_quota(inode, old_ea_inode,
1767 le32_to_cpu(here->e_value_size));
1770 /* No failures allowed past this point. */
1772 if (!s->not_found && here->e_value_size && !here->e_value_inum) {
1773 /* Remove the old value. */
1774 void *first_val = s->base + min_offs;
1775 size_t offs = le16_to_cpu(here->e_value_offs);
1776 void *val = s->base + offs;
1778 memmove(first_val + old_size, first_val, val - first_val);
1779 memset(first_val, 0, old_size);
1780 min_offs += old_size;
1782 /* Adjust all value offsets. */
1784 while (!IS_LAST_ENTRY(last)) {
1785 size_t o = le16_to_cpu(last->e_value_offs);
1787 if (!last->e_value_inum &&
1788 last->e_value_size && o < offs)
1789 last->e_value_offs = cpu_to_le16(o + old_size);
1790 last = EXT4_XATTR_NEXT(last);
1795 /* Remove old name. */
1796 size_t size = EXT4_XATTR_LEN(name_len);
1798 last = ENTRY((void *)last - size);
1799 memmove(here, (void *)here + size,
1800 (void *)last - (void *)here + sizeof(__u32));
1801 memset(last, 0, size);
1802 } else if (s->not_found) {
1803 /* Insert new name. */
1804 size_t size = EXT4_XATTR_LEN(name_len);
1805 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1807 memmove((void *)here + size, here, rest);
1808 memset(here, 0, size);
1809 here->e_name_index = i->name_index;
1810 here->e_name_len = name_len;
1811 memcpy(here->e_name, i->name, name_len);
1813 /* This is an update, reset value info. */
1814 here->e_value_inum = 0;
1815 here->e_value_offs = 0;
1816 here->e_value_size = 0;
1820 /* Insert new value. */
1822 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
1823 } else if (i->value_len) {
1824 void *val = s->base + min_offs - new_size;
1826 here->e_value_offs = cpu_to_le16(min_offs - new_size);
1827 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1828 memset(val, 0, new_size);
1830 memcpy(val, i->value, i->value_len);
1831 /* Clear padding bytes. */
1832 memset(val + i->value_len, 0,
1833 new_size - i->value_len);
1836 here->e_value_size = cpu_to_le32(i->value_len);
1843 /* Entry hash calculation. */
1848 * Feed crc32c hash instead of the raw value for entry
1849 * hash calculation. This is to avoid walking
1850 * potentially long value buffer again.
1852 crc32c_hash = cpu_to_le32(
1853 ext4_xattr_inode_get_hash(new_ea_inode));
1854 hash = ext4_xattr_hash_entry(here->e_name,
1857 } else if (is_block) {
1858 __le32 *value = s->base + le16_to_cpu(
1859 here->e_value_offs);
1861 hash = ext4_xattr_hash_entry(here->e_name,
1862 here->e_name_len, value,
1865 here->e_hash = hash;
1869 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1878 struct ext4_xattr_block_find {
1879 struct ext4_xattr_search s;
1880 struct buffer_head *bh;
1884 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1885 struct ext4_xattr_block_find *bs)
1887 struct super_block *sb = inode->i_sb;
1890 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1891 i->name_index, i->name, i->value, (long)i->value_len);
1893 if (EXT4_I(inode)->i_file_acl) {
1894 /* The inode already has an extended attribute block. */
1895 bs->bh = ext4_sb_bread(sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
1896 if (IS_ERR(bs->bh)) {
1897 error = PTR_ERR(bs->bh);
1901 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1902 atomic_read(&(bs->bh->b_count)),
1903 le32_to_cpu(BHDR(bs->bh)->h_refcount));
1904 error = ext4_xattr_check_block(inode, bs->bh);
1907 /* Find the named attribute. */
1908 bs->s.base = BHDR(bs->bh);
1909 bs->s.first = BFIRST(bs->bh);
1910 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1911 bs->s.here = bs->s.first;
1912 error = xattr_find_entry(inode, &bs->s.here, bs->s.end,
1913 i->name_index, i->name, 1);
1914 if (error && error != -ENODATA)
1916 bs->s.not_found = error;
1922 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1923 struct ext4_xattr_info *i,
1924 struct ext4_xattr_block_find *bs)
1926 struct super_block *sb = inode->i_sb;
1927 struct buffer_head *new_bh = NULL;
1928 struct ext4_xattr_search s_copy = bs->s;
1929 struct ext4_xattr_search *s = &s_copy;
1930 struct mb_cache_entry *ce = NULL;
1932 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1933 struct inode *ea_inode = NULL, *tmp_inode;
1934 size_t old_ea_inode_quota = 0;
1935 unsigned int ea_ino;
1938 #define header(x) ((struct ext4_xattr_header *)(x))
1941 int offset = (char *)s->here - bs->bh->b_data;
1943 BUFFER_TRACE(bs->bh, "get_write_access");
1944 error = ext4_journal_get_write_access(handle, sb, bs->bh,
1948 lock_buffer(bs->bh);
1950 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
1951 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1954 * This must happen under buffer lock for
1955 * ext4_xattr_block_set() to reliably detect modified
1958 if (ea_block_cache) {
1959 struct mb_cache_entry *oe;
1961 oe = mb_cache_entry_delete_or_get(ea_block_cache,
1962 hash, bs->bh->b_blocknr);
1965 * Xattr block is getting reused. Leave
1968 mb_cache_entry_put(ea_block_cache, oe);
1972 ea_bdebug(bs->bh, "modifying in-place");
1973 error = ext4_xattr_set_entry(i, s, handle, inode,
1974 true /* is_block */);
1975 ext4_xattr_block_csum_set(inode, bs->bh);
1976 unlock_buffer(bs->bh);
1977 if (error == -EFSCORRUPTED)
1980 error = ext4_handle_dirty_metadata(handle,
1988 unlock_buffer(bs->bh);
1989 ea_bdebug(bs->bh, "cloning");
1990 s->base = kmemdup(BHDR(bs->bh), bs->bh->b_size, GFP_NOFS);
1992 if (s->base == NULL)
1994 s->first = ENTRY(header(s->base)+1);
1995 header(s->base)->h_refcount = cpu_to_le32(1);
1996 s->here = ENTRY(s->base + offset);
1997 s->end = s->base + bs->bh->b_size;
2000 * If existing entry points to an xattr inode, we need
2001 * to prevent ext4_xattr_set_entry() from decrementing
2002 * ref count on it because the reference belongs to the
2003 * original block. In this case, make the entry look
2004 * like it has an empty value.
2006 if (!s->not_found && s->here->e_value_inum) {
2007 ea_ino = le32_to_cpu(s->here->e_value_inum);
2008 error = ext4_xattr_inode_iget(inode, ea_ino,
2009 le32_to_cpu(s->here->e_hash),
2014 if (!ext4_test_inode_state(tmp_inode,
2015 EXT4_STATE_LUSTRE_EA_INODE)) {
2017 * Defer quota free call for previous
2018 * inode until success is guaranteed.
2020 old_ea_inode_quota = le32_to_cpu(
2021 s->here->e_value_size);
2025 s->here->e_value_inum = 0;
2026 s->here->e_value_size = 0;
2029 /* Allocate a buffer where we construct the new block. */
2030 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
2032 if (s->base == NULL)
2034 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2035 header(s->base)->h_blocks = cpu_to_le32(1);
2036 header(s->base)->h_refcount = cpu_to_le32(1);
2037 s->first = ENTRY(header(s->base)+1);
2038 s->here = ENTRY(header(s->base)+1);
2039 s->end = s->base + sb->s_blocksize;
2042 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
2043 if (error == -EFSCORRUPTED)
2048 if (i->value && s->here->e_value_inum) {
2050 * A ref count on ea_inode has been taken as part of the call to
2051 * ext4_xattr_set_entry() above. We would like to drop this
2052 * extra ref but we have to wait until the xattr block is
2053 * initialized and has its own ref count on the ea_inode.
2055 ea_ino = le32_to_cpu(s->here->e_value_inum);
2056 error = ext4_xattr_inode_iget(inode, ea_ino,
2057 le32_to_cpu(s->here->e_hash),
2066 if (!IS_LAST_ENTRY(s->first)) {
2067 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
2070 /* We found an identical block in the cache. */
2071 if (new_bh == bs->bh)
2072 ea_bdebug(new_bh, "keeping");
2076 WARN_ON_ONCE(dquot_initialize_needed(inode));
2078 /* The old block is released after updating
2080 error = dquot_alloc_block(inode,
2081 EXT4_C2B(EXT4_SB(sb), 1));
2084 BUFFER_TRACE(new_bh, "get_write_access");
2085 error = ext4_journal_get_write_access(
2090 lock_buffer(new_bh);
2092 * We have to be careful about races with
2093 * adding references to xattr block. Once we
2094 * hold buffer lock xattr block's state is
2095 * stable so we can check the additional
2098 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2099 if (ref > EXT4_XATTR_REFCOUNT_MAX) {
2101 * Undo everything and check mbcache
2104 unlock_buffer(new_bh);
2105 dquot_free_block(inode,
2106 EXT4_C2B(EXT4_SB(sb),
2109 mb_cache_entry_put(ea_block_cache, ce);
2114 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2115 if (ref == EXT4_XATTR_REFCOUNT_MAX)
2116 clear_bit(MBE_REUSABLE_B, &ce->e_flags);
2117 ea_bdebug(new_bh, "reusing; refcount now=%d",
2119 ext4_xattr_block_csum_set(inode, new_bh);
2120 unlock_buffer(new_bh);
2121 error = ext4_handle_dirty_metadata(handle,
2127 mb_cache_entry_touch(ea_block_cache, ce);
2128 mb_cache_entry_put(ea_block_cache, ce);
2130 } else if (bs->bh && s->base == bs->bh->b_data) {
2131 /* We were modifying this block in-place. */
2132 ea_bdebug(bs->bh, "keeping this block");
2133 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
2137 /* We need to allocate a new block */
2138 ext4_fsblk_t goal, block;
2140 WARN_ON_ONCE(dquot_initialize_needed(inode));
2142 goal = ext4_group_first_block_no(sb,
2143 EXT4_I(inode)->i_block_group);
2144 block = ext4_new_meta_blocks(handle, inode, goal, 0,
2149 ea_idebug(inode, "creating block %llu",
2150 (unsigned long long)block);
2152 new_bh = sb_getblk(sb, block);
2153 if (unlikely(!new_bh)) {
2156 ext4_free_blocks(handle, inode, NULL, block, 1,
2157 EXT4_FREE_BLOCKS_METADATA);
2160 error = ext4_xattr_inode_inc_ref_all(handle, inode,
2161 ENTRY(header(s->base)+1));
2165 /* Drop the extra ref on ea_inode. */
2166 error = ext4_xattr_inode_dec_ref(handle,
2169 ext4_warning_inode(ea_inode,
2176 lock_buffer(new_bh);
2177 error = ext4_journal_get_create_access(handle, sb,
2178 new_bh, EXT4_JTR_NONE);
2180 unlock_buffer(new_bh);
2184 memcpy(new_bh->b_data, s->base, new_bh->b_size);
2185 ext4_xattr_block_csum_set(inode, new_bh);
2186 set_buffer_uptodate(new_bh);
2187 unlock_buffer(new_bh);
2188 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
2189 error = ext4_handle_dirty_metadata(handle, inode,
2196 if (old_ea_inode_quota)
2197 ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
2199 /* Update the inode. */
2200 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
2202 /* Drop the previous xattr block. */
2203 if (bs->bh && bs->bh != new_bh) {
2204 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2206 ext4_xattr_release_block(handle, inode, bs->bh,
2208 0 /* extra_credits */);
2209 ext4_xattr_inode_array_free(ea_inode_array);
2217 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2219 ext4_warning_inode(ea_inode, "dec ref error=%d",
2222 /* If there was an error, revert the quota charge. */
2224 ext4_xattr_inode_free_quota(inode, ea_inode,
2225 i_size_read(ea_inode));
2229 mb_cache_entry_put(ea_block_cache, ce);
2231 if (!(bs->bh && s->base == bs->bh->b_data))
2237 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
2241 EXT4_ERROR_INODE(inode, "bad block %llu",
2242 EXT4_I(inode)->i_file_acl);
2248 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2249 struct ext4_xattr_ibody_find *is)
2251 struct ext4_xattr_ibody_header *header;
2252 struct ext4_inode *raw_inode;
2255 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2258 raw_inode = ext4_raw_inode(&is->iloc);
2259 header = IHDR(inode, raw_inode);
2260 is->s.base = is->s.first = IFIRST(header);
2261 is->s.here = is->s.first;
2262 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2263 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2264 error = xattr_check_inode(inode, header, is->s.end);
2267 /* Find the named attribute. */
2268 error = xattr_find_entry(inode, &is->s.here, is->s.end,
2269 i->name_index, i->name, 0);
2270 if (error && error != -ENODATA)
2272 is->s.not_found = error;
2277 int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
2278 struct ext4_xattr_info *i,
2279 struct ext4_xattr_ibody_find *is)
2281 struct ext4_xattr_ibody_header *header;
2282 struct ext4_xattr_search *s = &is->s;
2285 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2288 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
2291 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2292 if (!IS_LAST_ENTRY(s->first)) {
2293 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2294 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2296 header->h_magic = cpu_to_le32(0);
2297 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2302 static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2303 struct ext4_xattr_info *i)
2307 /* When e_value_inum is set the value is stored externally. */
2308 if (s->here->e_value_inum)
2310 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2312 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2313 return !memcmp(value, i->value, i->value_len);
2316 static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2318 struct buffer_head *bh;
2321 if (!EXT4_I(inode)->i_file_acl)
2323 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2326 error = ext4_xattr_check_block(inode, bh);
2329 return ERR_PTR(error);
2335 * ext4_xattr_set_handle()
2337 * Create, replace or remove an extended attribute for this inode. Value
2338 * is NULL to remove an existing extended attribute, and non-NULL to
2339 * either replace an existing extended attribute, or create a new extended
2340 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2341 * specify that an extended attribute must exist and must not exist
2342 * previous to the call, respectively.
2344 * Returns 0, or a negative error number on failure.
2347 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
2348 const char *name, const void *value, size_t value_len,
2351 struct ext4_xattr_info i = {
2352 .name_index = name_index,
2355 .value_len = value_len,
2358 struct ext4_xattr_ibody_find is = {
2359 .s = { .not_found = -ENODATA, },
2361 struct ext4_xattr_block_find bs = {
2362 .s = { .not_found = -ENODATA, },
2369 if (strlen(name) > 255)
2372 ext4_write_lock_xattr(inode, &no_expand);
2374 /* Check journal credits under write lock. */
2375 if (ext4_handle_valid(handle)) {
2376 struct buffer_head *bh;
2379 bh = ext4_xattr_get_block(inode);
2381 error = PTR_ERR(bh);
2385 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2387 flags & XATTR_CREATE);
2390 if (jbd2_handle_buffer_credits(handle) < credits) {
2394 WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
2397 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
2401 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
2402 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2403 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
2404 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
2407 error = ext4_xattr_ibody_find(inode, &i, &is);
2411 error = ext4_xattr_block_find(inode, &i, &bs);
2414 if (is.s.not_found && bs.s.not_found) {
2416 if (flags & XATTR_REPLACE)
2423 if (flags & XATTR_CREATE)
2428 if (!is.s.not_found)
2429 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2430 else if (!bs.s.not_found)
2431 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2434 /* Xattr value did not change? Save us some work and bail out */
2435 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2437 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2440 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2441 (EXT4_XATTR_SIZE(i.value_len) >
2442 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2445 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2446 if (!error && !bs.s.not_found) {
2448 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2449 } else if (error == -ENOSPC) {
2450 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2453 error = ext4_xattr_block_find(inode, &i, &bs);
2457 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2458 if (!error && !is.s.not_found) {
2460 error = ext4_xattr_ibody_set(handle, inode, &i,
2462 } else if (error == -ENOSPC) {
2464 * Xattr does not fit in the block, store at
2465 * external inode if possible.
2467 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2468 i.value_len && !i.in_inode) {
2476 ext4_xattr_update_super_block(handle, inode->i_sb);
2477 inode->i_ctime = current_time(inode);
2478 inode_inc_iversion(inode);
2481 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
2483 * The bh is consumed by ext4_mark_iloc_dirty, even with
2488 ext4_handle_sync(handle);
2490 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle);
2495 ext4_write_unlock_xattr(inode, &no_expand);
2499 int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2500 bool is_create, int *credits)
2502 struct buffer_head *bh;
2507 if (!EXT4_SB(inode->i_sb)->s_journal)
2510 down_read(&EXT4_I(inode)->xattr_sem);
2512 bh = ext4_xattr_get_block(inode);
2516 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2517 value_len, is_create);
2522 up_read(&EXT4_I(inode)->xattr_sem);
2529 * Like ext4_xattr_set_handle, but start from an inode. This extended
2530 * attribute modification is a filesystem transaction by itself.
2532 * Returns 0, or a negative error number on failure.
2535 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
2536 const void *value, size_t value_len, int flags)
2539 struct super_block *sb = inode->i_sb;
2540 int error, retries = 0;
2543 error = dquot_initialize(inode);
2548 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2553 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
2554 if (IS_ERR(handle)) {
2555 error = PTR_ERR(handle);
2559 error = ext4_xattr_set_handle(handle, inode, name_index, name,
2560 value, value_len, flags);
2561 error2 = ext4_journal_stop(handle);
2562 if (error == -ENOSPC &&
2563 ext4_should_retry_alloc(sb, &retries))
2568 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, NULL);
2574 * Shift the EA entries in the inode to create space for the increased
2577 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2578 int value_offs_shift, void *to,
2579 void *from, size_t n)
2581 struct ext4_xattr_entry *last = entry;
2584 /* We always shift xattr headers further thus offsets get lower */
2585 BUG_ON(value_offs_shift > 0);
2587 /* Adjust the value offsets of the entries */
2588 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2589 if (!last->e_value_inum && last->e_value_size) {
2590 new_offs = le16_to_cpu(last->e_value_offs) +
2592 last->e_value_offs = cpu_to_le16(new_offs);
2595 /* Shift the entries by n bytes */
2596 memmove(to, from, n);
2600 * Move xattr pointed to by 'entry' from inode into external xattr block
2602 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2603 struct ext4_inode *raw_inode,
2604 struct ext4_xattr_entry *entry)
2606 struct ext4_xattr_ibody_find *is = NULL;
2607 struct ext4_xattr_block_find *bs = NULL;
2608 char *buffer = NULL, *b_entry_name = NULL;
2609 size_t value_size = le32_to_cpu(entry->e_value_size);
2610 struct ext4_xattr_info i = {
2613 .name_index = entry->e_name_index,
2614 .in_inode = !!entry->e_value_inum,
2616 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2617 int needs_kvfree = 0;
2620 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2621 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2622 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2623 if (!is || !bs || !b_entry_name) {
2628 is->s.not_found = -ENODATA;
2629 bs->s.not_found = -ENODATA;
2633 /* Save the entry name and the entry value */
2634 if (entry->e_value_inum) {
2635 buffer = kvmalloc(value_size, GFP_NOFS);
2641 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
2645 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2646 buffer = (void *)IFIRST(header) + value_offs;
2649 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2650 b_entry_name[entry->e_name_len] = '\0';
2651 i.name = b_entry_name;
2653 error = ext4_get_inode_loc(inode, &is->iloc);
2657 error = ext4_xattr_ibody_find(inode, &i, is);
2662 i.value_len = value_size;
2663 error = ext4_xattr_block_find(inode, &i, bs);
2667 /* Move ea entry from the inode into the block */
2668 error = ext4_xattr_block_set(handle, inode, &i, bs);
2672 /* Remove the chosen entry from the inode */
2675 error = ext4_xattr_ibody_set(handle, inode, &i, is);
2678 kfree(b_entry_name);
2679 if (needs_kvfree && buffer)
2682 brelse(is->iloc.bh);
2691 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2692 struct ext4_inode *raw_inode,
2693 int isize_diff, size_t ifree,
2694 size_t bfree, int *total_ino)
2696 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2697 struct ext4_xattr_entry *small_entry;
2698 struct ext4_xattr_entry *entry;
2699 struct ext4_xattr_entry *last;
2700 unsigned int entry_size; /* EA entry size */
2701 unsigned int total_size; /* EA entry size + value size */
2702 unsigned int min_total_size;
2705 while (isize_diff > ifree) {
2708 min_total_size = ~0U;
2709 last = IFIRST(header);
2710 /* Find the entry best suited to be pushed into EA block */
2711 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2712 /* never move system.data out of the inode */
2713 if ((last->e_name_len == 4) &&
2714 (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
2715 !memcmp(last->e_name, "data", 4))
2717 total_size = EXT4_XATTR_LEN(last->e_name_len);
2718 if (!last->e_value_inum)
2719 total_size += EXT4_XATTR_SIZE(
2720 le32_to_cpu(last->e_value_size));
2721 if (total_size <= bfree &&
2722 total_size < min_total_size) {
2723 if (total_size + ifree < isize_diff) {
2727 min_total_size = total_size;
2732 if (entry == NULL) {
2733 if (small_entry == NULL)
2735 entry = small_entry;
2738 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
2739 total_size = entry_size;
2740 if (!entry->e_value_inum)
2741 total_size += EXT4_XATTR_SIZE(
2742 le32_to_cpu(entry->e_value_size));
2743 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2748 *total_ino -= entry_size;
2749 ifree += total_size;
2750 bfree -= total_size;
2757 * Expand an inode by new_extra_isize bytes when EAs are present.
2758 * Returns 0 on success or negative error number on failure.
2760 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2761 struct ext4_inode *raw_inode, handle_t *handle)
2763 struct ext4_xattr_ibody_header *header;
2764 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2765 static unsigned int mnt_count;
2767 size_t ifree, bfree;
2770 int error = 0, tried_min_extra_isize = 0;
2771 int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
2772 int isize_diff; /* How much do we need to grow i_extra_isize */
2775 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
2776 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
2779 header = IHDR(inode, raw_inode);
2782 * Check if enough free space is available in the inode to shift the
2783 * entries ahead by new_extra_isize.
2786 base = IFIRST(header);
2787 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2788 min_offs = end - base;
2789 total_ino = sizeof(struct ext4_xattr_ibody_header) + sizeof(u32);
2791 error = xattr_check_inode(inode, header, end);
2795 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
2796 if (ifree >= isize_diff)
2800 * Enough free space isn't available in the inode, check if
2801 * EA block can hold new_extra_isize bytes.
2803 if (EXT4_I(inode)->i_file_acl) {
2804 struct buffer_head *bh;
2806 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2808 error = PTR_ERR(bh);
2811 error = ext4_xattr_check_block(inode, bh);
2817 end = bh->b_data + bh->b_size;
2818 min_offs = end - base;
2819 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2822 if (bfree + ifree < isize_diff) {
2823 if (!tried_min_extra_isize && s_min_extra_isize) {
2824 tried_min_extra_isize++;
2825 new_extra_isize = s_min_extra_isize;
2832 bfree = inode->i_sb->s_blocksize;
2835 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2836 isize_diff, ifree, bfree,
2839 if (error == -ENOSPC && !tried_min_extra_isize &&
2840 s_min_extra_isize) {
2841 tried_min_extra_isize++;
2842 new_extra_isize = s_min_extra_isize;
2848 /* Adjust the offsets and shift the remaining entries ahead */
2849 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
2850 - new_extra_isize, (void *)raw_inode +
2851 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
2852 (void *)header, total_ino);
2853 EXT4_I(inode)->i_extra_isize = new_extra_isize;
2855 if (ext4_has_inline_data(inode))
2856 error = ext4_find_inline_data_nolock(inode);
2859 if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
2860 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2862 mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
2867 #define EIA_INCR 16 /* must be 2^n */
2868 #define EIA_MASK (EIA_INCR - 1)
2870 /* Add the large xattr @inode into @ea_inode_array for deferred iput().
2871 * If @ea_inode_array is new or full it will be grown and the old
2872 * contents copied over.
2875 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2876 struct inode *inode)
2878 if (*ea_inode_array == NULL) {
2880 * Start with 15 inodes, so it fits into a power-of-two size.
2881 * If *ea_inode_array is NULL, this is essentially offsetof()
2884 kmalloc(offsetof(struct ext4_xattr_inode_array,
2887 if (*ea_inode_array == NULL)
2889 (*ea_inode_array)->count = 0;
2890 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
2891 /* expand the array once all 15 + n * 16 slots are full */
2892 struct ext4_xattr_inode_array *new_array = NULL;
2893 int count = (*ea_inode_array)->count;
2895 /* if new_array is NULL, this is essentially offsetof() */
2896 new_array = kmalloc(
2897 offsetof(struct ext4_xattr_inode_array,
2898 inodes[count + EIA_INCR]),
2900 if (new_array == NULL)
2902 memcpy(new_array, *ea_inode_array,
2903 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2904 kfree(*ea_inode_array);
2905 *ea_inode_array = new_array;
2907 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
2912 * ext4_xattr_delete_inode()
2914 * Free extended attribute resources associated with this inode. Traverse
2915 * all entries and decrement reference on any xattr inodes associated with this
2916 * inode. This is called immediately before an inode is freed. We have exclusive
2917 * access to the inode. If an orphan inode is deleted it will also release its
2918 * references on xattr block and xattr inodes.
2920 int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2921 struct ext4_xattr_inode_array **ea_inode_array,
2924 struct buffer_head *bh = NULL;
2925 struct ext4_xattr_ibody_header *header;
2926 struct ext4_iloc iloc = { .bh = NULL };
2927 struct ext4_xattr_entry *entry;
2928 struct inode *ea_inode;
2931 error = ext4_journal_ensure_credits(handle, extra_credits,
2932 ext4_free_metadata_revoke_credits(inode->i_sb, 1));
2934 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2938 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2939 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2941 error = ext4_get_inode_loc(inode, &iloc);
2943 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
2947 error = ext4_journal_get_write_access(handle, inode->i_sb,
2948 iloc.bh, EXT4_JTR_NONE);
2950 EXT4_ERROR_INODE(inode, "write access (error %d)",
2955 header = IHDR(inode, ext4_raw_inode(&iloc));
2956 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2957 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2959 false /* block_csum */,
2962 false /* skip_quota */);
2965 if (EXT4_I(inode)->i_file_acl) {
2966 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2968 error = PTR_ERR(bh);
2969 if (error == -EIO) {
2970 EXT4_ERROR_INODE_ERR(inode, EIO,
2971 "block %llu read error",
2972 EXT4_I(inode)->i_file_acl);
2977 error = ext4_xattr_check_block(inode, bh);
2981 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2982 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2983 entry = EXT4_XATTR_NEXT(entry)) {
2984 if (!entry->e_value_inum)
2986 error = ext4_xattr_inode_iget(inode,
2987 le32_to_cpu(entry->e_value_inum),
2988 le32_to_cpu(entry->e_hash),
2992 ext4_xattr_inode_free_quota(inode, ea_inode,
2993 le32_to_cpu(entry->e_value_size));
2999 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
3002 * Update i_file_acl value in the same transaction that releases
3005 EXT4_I(inode)->i_file_acl = 0;
3006 error = ext4_mark_inode_dirty(handle, inode);
3008 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
3012 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle);
3021 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
3025 if (ea_inode_array == NULL)
3028 for (idx = 0; idx < ea_inode_array->count; ++idx)
3029 iput(ea_inode_array->inodes[idx]);
3030 kfree(ea_inode_array);
3034 * ext4_xattr_block_cache_insert()
3036 * Create a new entry in the extended attribute block cache, and insert
3037 * it unless such an entry is already in the cache.
3039 * Returns 0, or a negative error number on failure.
3042 ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
3043 struct buffer_head *bh)
3045 struct ext4_xattr_header *header = BHDR(bh);
3046 __u32 hash = le32_to_cpu(header->h_hash);
3047 int reusable = le32_to_cpu(header->h_refcount) <
3048 EXT4_XATTR_REFCOUNT_MAX;
3051 if (!ea_block_cache)
3053 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
3054 bh->b_blocknr, reusable);
3056 if (error == -EBUSY)
3057 ea_bdebug(bh, "already in cache");
3059 ea_bdebug(bh, "inserting [%x]", (int)hash);
3065 * Compare two extended attribute blocks for equality.
3067 * Returns 0 if the blocks are equal, 1 if they differ, and
3068 * a negative error number on errors.
3071 ext4_xattr_cmp(struct ext4_xattr_header *header1,
3072 struct ext4_xattr_header *header2)
3074 struct ext4_xattr_entry *entry1, *entry2;
3076 entry1 = ENTRY(header1+1);
3077 entry2 = ENTRY(header2+1);
3078 while (!IS_LAST_ENTRY(entry1)) {
3079 if (IS_LAST_ENTRY(entry2))
3081 if (entry1->e_hash != entry2->e_hash ||
3082 entry1->e_name_index != entry2->e_name_index ||
3083 entry1->e_name_len != entry2->e_name_len ||
3084 entry1->e_value_size != entry2->e_value_size ||
3085 entry1->e_value_inum != entry2->e_value_inum ||
3086 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
3088 if (!entry1->e_value_inum &&
3089 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
3090 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
3091 le32_to_cpu(entry1->e_value_size)))
3094 entry1 = EXT4_XATTR_NEXT(entry1);
3095 entry2 = EXT4_XATTR_NEXT(entry2);
3097 if (!IS_LAST_ENTRY(entry2))
3103 * ext4_xattr_block_cache_find()
3105 * Find an identical extended attribute block.
3107 * Returns a pointer to the block found, or NULL if such a block was
3108 * not found or an error occurred.
3110 static struct buffer_head *
3111 ext4_xattr_block_cache_find(struct inode *inode,
3112 struct ext4_xattr_header *header,
3113 struct mb_cache_entry **pce)
3115 __u32 hash = le32_to_cpu(header->h_hash);
3116 struct mb_cache_entry *ce;
3117 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
3119 if (!ea_block_cache)
3121 if (!header->h_hash)
3122 return NULL; /* never share */
3123 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
3124 ce = mb_cache_entry_find_first(ea_block_cache, hash);
3126 struct buffer_head *bh;
3128 bh = ext4_sb_bread(inode->i_sb, ce->e_value, REQ_PRIO);
3130 if (PTR_ERR(bh) == -ENOMEM)
3133 EXT4_ERROR_INODE(inode, "block %lu read error",
3134 (unsigned long)ce->e_value);
3135 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
3140 ce = mb_cache_entry_find_next(ea_block_cache, ce);
3145 #define NAME_HASH_SHIFT 5
3146 #define VALUE_HASH_SHIFT 16
3149 * ext4_xattr_hash_entry()
3151 * Compute the hash of an extended attribute.
3153 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3158 while (name_len--) {
3159 hash = (hash << NAME_HASH_SHIFT) ^
3160 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3161 (unsigned char)*name++;
3163 while (value_count--) {
3164 hash = (hash << VALUE_HASH_SHIFT) ^
3165 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3166 le32_to_cpu(*value++);
3168 return cpu_to_le32(hash);
3172 * ext4_xattr_hash_entry_signed()
3174 * Compute the hash of an extended attribute incorrectly.
3176 static __le32 ext4_xattr_hash_entry_signed(char *name, size_t name_len, __le32 *value, size_t value_count)
3180 while (name_len--) {
3181 hash = (hash << NAME_HASH_SHIFT) ^
3182 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3183 (signed char)*name++;
3185 while (value_count--) {
3186 hash = (hash << VALUE_HASH_SHIFT) ^
3187 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3188 le32_to_cpu(*value++);
3190 return cpu_to_le32(hash);
3193 #undef NAME_HASH_SHIFT
3194 #undef VALUE_HASH_SHIFT
3196 #define BLOCK_HASH_SHIFT 16
3199 * ext4_xattr_rehash()
3201 * Re-compute the extended attribute hash value after an entry has changed.
3203 static void ext4_xattr_rehash(struct ext4_xattr_header *header)
3205 struct ext4_xattr_entry *here;
3208 here = ENTRY(header+1);
3209 while (!IS_LAST_ENTRY(here)) {
3210 if (!here->e_hash) {
3211 /* Block is not shared if an entry's hash value == 0 */
3215 hash = (hash << BLOCK_HASH_SHIFT) ^
3216 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3217 le32_to_cpu(here->e_hash);
3218 here = EXT4_XATTR_NEXT(here);
3220 header->h_hash = cpu_to_le32(hash);
3223 #undef BLOCK_HASH_SHIFT
3225 #define HASH_BUCKET_BITS 10
3228 ext4_xattr_create_cache(void)
3230 return mb_cache_create(HASH_BUCKET_BITS);
3233 void ext4_xattr_destroy_cache(struct mb_cache *cache)
3236 mb_cache_destroy(cache);