1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/xattr.c
5 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
7 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
8 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
9 * Extended attributes for symlinks and special files added per
10 * suggestion of Luka Renko <luka.renko@hermes.si>.
11 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
13 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
14 * and Andreas Gruenbacher <agruen@suse.de>.
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 void ext4_xattr_rehash(struct ext4_xattr_header *);
86 static const struct xattr_handler * const ext4_xattr_handler_map[] = {
87 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
88 #ifdef CONFIG_EXT4_FS_POSIX_ACL
89 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
90 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
92 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
93 #ifdef CONFIG_EXT4_FS_SECURITY
94 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
96 [EXT4_XATTR_INDEX_HURD] = &ext4_xattr_hurd_handler,
99 const struct xattr_handler *ext4_xattr_handlers[] = {
100 &ext4_xattr_user_handler,
101 &ext4_xattr_trusted_handler,
102 #ifdef CONFIG_EXT4_FS_POSIX_ACL
103 &posix_acl_access_xattr_handler,
104 &posix_acl_default_xattr_handler,
106 #ifdef CONFIG_EXT4_FS_SECURITY
107 &ext4_xattr_security_handler,
109 &ext4_xattr_hurd_handler,
113 #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
114 inode->i_sb->s_fs_info)->s_ea_block_cache)
116 #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
117 inode->i_sb->s_fs_info)->s_ea_inode_cache)
120 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
121 struct inode *inode);
123 #ifdef CONFIG_LOCKDEP
124 void ext4_xattr_inode_set_class(struct inode *ea_inode)
126 struct ext4_inode_info *ei = EXT4_I(ea_inode);
128 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
129 (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */
130 lockdep_set_subclass(&ei->i_data_sem, I_DATA_SEM_EA);
134 static __le32 ext4_xattr_block_csum(struct inode *inode,
136 struct ext4_xattr_header *hdr)
138 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
140 __le64 dsk_block_nr = cpu_to_le64(block_nr);
141 __u32 dummy_csum = 0;
142 int offset = offsetof(struct ext4_xattr_header, h_checksum);
144 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
145 sizeof(dsk_block_nr));
146 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
147 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
148 offset += sizeof(dummy_csum);
149 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
150 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
152 return cpu_to_le32(csum);
155 static int ext4_xattr_block_csum_verify(struct inode *inode,
156 struct buffer_head *bh)
158 struct ext4_xattr_header *hdr = BHDR(bh);
161 if (ext4_has_metadata_csum(inode->i_sb)) {
163 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
164 bh->b_blocknr, hdr));
170 static void ext4_xattr_block_csum_set(struct inode *inode,
171 struct buffer_head *bh)
173 if (ext4_has_metadata_csum(inode->i_sb))
174 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
175 bh->b_blocknr, BHDR(bh));
178 static inline const struct xattr_handler *
179 ext4_xattr_handler(int name_index)
181 const struct xattr_handler *handler = NULL;
183 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
184 handler = ext4_xattr_handler_map[name_index];
189 ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
192 struct ext4_xattr_entry *e = entry;
194 /* Find the end of the names list */
195 while (!IS_LAST_ENTRY(e)) {
196 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
197 if ((void *)next >= end)
198 return -EFSCORRUPTED;
199 if (strnlen(e->e_name, e->e_name_len) != e->e_name_len)
200 return -EFSCORRUPTED;
204 /* Check the values */
205 while (!IS_LAST_ENTRY(entry)) {
206 u32 size = le32_to_cpu(entry->e_value_size);
208 if (size > EXT4_XATTR_SIZE_MAX)
209 return -EFSCORRUPTED;
211 if (size != 0 && entry->e_value_inum == 0) {
212 u16 offs = le16_to_cpu(entry->e_value_offs);
216 * The value cannot overlap the names, and the value
217 * with padding cannot extend beyond 'end'. Check both
218 * the padded and unpadded sizes, since the size may
219 * overflow to 0 when adding padding.
221 if (offs > end - value_start)
222 return -EFSCORRUPTED;
223 value = value_start + offs;
224 if (value < (void *)e + sizeof(u32) ||
225 size > end - value ||
226 EXT4_XATTR_SIZE(size) > end - value)
227 return -EFSCORRUPTED;
229 entry = EXT4_XATTR_NEXT(entry);
236 __ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh,
237 const char *function, unsigned int line)
239 int error = -EFSCORRUPTED;
241 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
242 BHDR(bh)->h_blocks != cpu_to_le32(1))
244 if (buffer_verified(bh))
248 if (!ext4_xattr_block_csum_verify(inode, bh))
250 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
254 __ext4_error_inode(inode, function, line, 0, -error,
255 "corrupted xattr block %llu",
256 (unsigned long long) bh->b_blocknr);
258 set_buffer_verified(bh);
262 #define ext4_xattr_check_block(inode, bh) \
263 __ext4_xattr_check_block((inode), (bh), __func__, __LINE__)
267 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
268 void *end, const char *function, unsigned int line)
270 int error = -EFSCORRUPTED;
272 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
273 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
275 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
278 __ext4_error_inode(inode, function, line, 0, -error,
279 "corrupted in-inode xattr");
283 #define xattr_check_inode(inode, header, end) \
284 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
287 xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry,
288 void *end, int name_index, const char *name, int sorted)
290 struct ext4_xattr_entry *entry, *next;
296 name_len = strlen(name);
297 for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) {
298 next = EXT4_XATTR_NEXT(entry);
299 if ((void *) next >= end) {
300 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
301 return -EFSCORRUPTED;
303 cmp = name_index - entry->e_name_index;
305 cmp = name_len - entry->e_name_len;
307 cmp = memcmp(name, entry->e_name, name_len);
308 if (cmp <= 0 && (sorted || cmp == 0))
312 return cmp ? -ENODATA : 0;
316 ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
318 return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
321 static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
323 return ((u64)ea_inode->i_ctime.tv_sec << 32) |
324 (u32) inode_peek_iversion_raw(ea_inode);
327 static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
329 ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
330 inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
333 static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
335 return (u32)ea_inode->i_atime.tv_sec;
338 static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
340 ea_inode->i_atime.tv_sec = hash;
344 * Read the EA value from an inode.
346 static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
348 int blocksize = 1 << ea_inode->i_blkbits;
349 int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
350 int tail_size = (size % blocksize) ?: blocksize;
351 struct buffer_head *bhs_inline[8];
352 struct buffer_head **bhs = bhs_inline;
355 if (bh_count > ARRAY_SIZE(bhs_inline)) {
356 bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
361 ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
362 true /* wait */, bhs);
366 for (i = 0; i < bh_count; i++) {
367 /* There shouldn't be any holes in ea_inode. */
372 memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
373 i < bh_count - 1 ? blocksize : tail_size);
377 for (i = 0; i < bh_count; i++)
380 if (bhs != bhs_inline)
385 #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
387 static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
388 u32 ea_inode_hash, struct inode **ea_inode)
394 * We have to check for this corruption early as otherwise
395 * iget_locked() could wait indefinitely for the state of our
398 if (parent->i_ino == ea_ino) {
399 ext4_error(parent->i_sb,
400 "Parent and EA inode have the same ino %lu", ea_ino);
401 return -EFSCORRUPTED;
404 inode = ext4_iget(parent->i_sb, ea_ino, EXT4_IGET_EA_INODE);
406 err = PTR_ERR(inode);
407 ext4_error(parent->i_sb,
408 "error while reading EA inode %lu err=%d", ea_ino,
412 ext4_xattr_inode_set_class(inode);
415 * Check whether this is an old Lustre-style xattr inode. Lustre
416 * implementation does not have hash validation, rather it has a
417 * backpointer from ea_inode to the parent inode.
419 if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
420 EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
421 inode->i_generation == parent->i_generation) {
422 ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
423 ext4_xattr_inode_set_ref(inode, 1);
426 inode->i_flags |= S_NOQUOTA;
434 /* Remove entry from mbcache when EA inode is getting evicted */
435 void ext4_evict_ea_inode(struct inode *inode)
437 struct mb_cache_entry *oe;
439 if (!EA_INODE_CACHE(inode))
441 /* Wait for entry to get unused so that we can remove it */
442 while ((oe = mb_cache_entry_delete_or_get(EA_INODE_CACHE(inode),
443 ext4_xattr_inode_get_hash(inode), inode->i_ino))) {
444 mb_cache_entry_wait_unused(oe);
445 mb_cache_entry_put(EA_INODE_CACHE(inode), oe);
450 ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
451 struct ext4_xattr_entry *entry, void *buffer,
456 /* Verify stored hash matches calculated hash. */
457 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
458 if (hash != ext4_xattr_inode_get_hash(ea_inode))
459 return -EFSCORRUPTED;
462 __le32 e_hash, tmp_data;
464 /* Verify entry hash. */
465 tmp_data = cpu_to_le32(hash);
466 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
468 if (e_hash != entry->e_hash)
469 return -EFSCORRUPTED;
475 * Read xattr value from the EA inode.
478 ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
479 void *buffer, size_t size)
481 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
482 struct inode *ea_inode;
485 err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
486 le32_to_cpu(entry->e_hash), &ea_inode);
492 if (i_size_read(ea_inode) != size) {
493 ext4_warning_inode(ea_inode,
494 "ea_inode file size=%llu entry size=%zu",
495 i_size_read(ea_inode), size);
500 err = ext4_xattr_inode_read(ea_inode, buffer, size);
504 if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
505 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
508 ext4_warning_inode(ea_inode,
509 "EA inode hash validation failed");
514 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
515 ext4_xattr_inode_get_hash(ea_inode),
516 ea_inode->i_ino, true /* reusable */);
524 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
525 void *buffer, size_t buffer_size)
527 struct buffer_head *bh = NULL;
528 struct ext4_xattr_entry *entry;
532 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
534 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
535 name_index, name, buffer, (long)buffer_size);
537 if (!EXT4_I(inode)->i_file_acl)
539 ea_idebug(inode, "reading block %llu",
540 (unsigned long long)EXT4_I(inode)->i_file_acl);
541 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
544 ea_bdebug(bh, "b_count=%d, refcount=%d",
545 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
546 error = ext4_xattr_check_block(inode, bh);
549 ext4_xattr_block_cache_insert(ea_block_cache, bh);
551 end = bh->b_data + bh->b_size;
552 error = xattr_find_entry(inode, &entry, end, name_index, name, 1);
555 size = le32_to_cpu(entry->e_value_size);
557 if (unlikely(size > EXT4_XATTR_SIZE_MAX))
560 if (size > buffer_size)
562 if (entry->e_value_inum) {
563 error = ext4_xattr_inode_get(inode, entry, buffer,
568 u16 offset = le16_to_cpu(entry->e_value_offs);
569 void *p = bh->b_data + offset;
571 if (unlikely(p + size > end))
573 memcpy(buffer, p, size);
584 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
585 void *buffer, size_t buffer_size)
587 struct ext4_xattr_ibody_header *header;
588 struct ext4_xattr_entry *entry;
589 struct ext4_inode *raw_inode;
590 struct ext4_iloc iloc;
595 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
597 error = ext4_get_inode_loc(inode, &iloc);
600 raw_inode = ext4_raw_inode(&iloc);
601 header = IHDR(inode, raw_inode);
602 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
603 error = xattr_check_inode(inode, header, end);
606 entry = IFIRST(header);
607 error = xattr_find_entry(inode, &entry, end, name_index, name, 0);
610 size = le32_to_cpu(entry->e_value_size);
612 if (unlikely(size > EXT4_XATTR_SIZE_MAX))
615 if (size > buffer_size)
617 if (entry->e_value_inum) {
618 error = ext4_xattr_inode_get(inode, entry, buffer,
623 u16 offset = le16_to_cpu(entry->e_value_offs);
624 void *p = (void *)IFIRST(header) + offset;
626 if (unlikely(p + size > end))
628 memcpy(buffer, p, size);
641 * Copy an extended attribute into the buffer
642 * provided, or compute the buffer size required.
643 * Buffer is NULL to compute the size of the buffer required.
645 * Returns a negative error number on failure, or the number of bytes
646 * used / required on success.
649 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
650 void *buffer, size_t buffer_size)
654 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
657 if (strlen(name) > 255)
660 down_read(&EXT4_I(inode)->xattr_sem);
661 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
663 if (error == -ENODATA)
664 error = ext4_xattr_block_get(inode, name_index, name, buffer,
666 up_read(&EXT4_I(inode)->xattr_sem);
671 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
672 char *buffer, size_t buffer_size)
674 size_t rest = buffer_size;
676 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
677 const struct xattr_handler *handler =
678 ext4_xattr_handler(entry->e_name_index);
680 if (handler && (!handler->list || handler->list(dentry))) {
681 const char *prefix = handler->prefix ?: handler->name;
682 size_t prefix_len = strlen(prefix);
683 size_t size = prefix_len + entry->e_name_len + 1;
688 memcpy(buffer, prefix, prefix_len);
689 buffer += prefix_len;
690 memcpy(buffer, entry->e_name, entry->e_name_len);
691 buffer += entry->e_name_len;
697 return buffer_size - rest; /* total size */
701 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
703 struct inode *inode = d_inode(dentry);
704 struct buffer_head *bh = NULL;
707 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
708 buffer, (long)buffer_size);
710 if (!EXT4_I(inode)->i_file_acl)
712 ea_idebug(inode, "reading block %llu",
713 (unsigned long long)EXT4_I(inode)->i_file_acl);
714 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
717 ea_bdebug(bh, "b_count=%d, refcount=%d",
718 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
719 error = ext4_xattr_check_block(inode, bh);
722 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
723 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer,
731 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
733 struct inode *inode = d_inode(dentry);
734 struct ext4_xattr_ibody_header *header;
735 struct ext4_inode *raw_inode;
736 struct ext4_iloc iloc;
740 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
742 error = ext4_get_inode_loc(inode, &iloc);
745 raw_inode = ext4_raw_inode(&iloc);
746 header = IHDR(inode, raw_inode);
747 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
748 error = xattr_check_inode(inode, header, end);
751 error = ext4_xattr_list_entries(dentry, IFIRST(header),
752 buffer, buffer_size);
760 * Inode operation listxattr()
762 * d_inode(dentry)->i_rwsem: don't care
764 * Copy a list of attribute names into the buffer
765 * provided, or compute the buffer size required.
766 * Buffer is NULL to compute the size of the buffer required.
768 * Returns a negative error number on failure, or the number of bytes
769 * used / required on success.
772 ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
776 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
777 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
784 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
789 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
794 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
797 static void ext4_xattr_update_super_block(handle_t *handle,
798 struct super_block *sb)
800 if (ext4_has_feature_xattr(sb))
803 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
804 if (ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh,
805 EXT4_JTR_NONE) == 0) {
806 lock_buffer(EXT4_SB(sb)->s_sbh);
807 ext4_set_feature_xattr(sb);
808 ext4_superblock_csum_set(sb);
809 unlock_buffer(EXT4_SB(sb)->s_sbh);
810 ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
814 int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
816 struct ext4_iloc iloc = { .bh = NULL };
817 struct buffer_head *bh = NULL;
818 struct ext4_inode *raw_inode;
819 struct ext4_xattr_ibody_header *header;
820 struct ext4_xattr_entry *entry;
821 qsize_t ea_inode_refs = 0;
825 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
827 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
828 ret = ext4_get_inode_loc(inode, &iloc);
831 raw_inode = ext4_raw_inode(&iloc);
832 header = IHDR(inode, raw_inode);
833 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
834 ret = xattr_check_inode(inode, header, end);
838 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
839 entry = EXT4_XATTR_NEXT(entry))
840 if (entry->e_value_inum)
844 if (EXT4_I(inode)->i_file_acl) {
845 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
852 ret = ext4_xattr_check_block(inode, bh);
856 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
857 entry = EXT4_XATTR_NEXT(entry))
858 if (entry->e_value_inum)
861 *usage = ea_inode_refs + 1;
869 static inline size_t round_up_cluster(struct inode *inode, size_t length)
871 struct super_block *sb = inode->i_sb;
872 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
874 size_t mask = ~(cluster_size - 1);
876 return (length + cluster_size - 1) & mask;
879 static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
883 err = dquot_alloc_inode(inode);
886 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
888 dquot_free_inode(inode);
892 static void ext4_xattr_inode_free_quota(struct inode *parent,
893 struct inode *ea_inode,
897 ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
899 dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
900 dquot_free_inode(parent);
903 int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
904 struct buffer_head *block_bh, size_t value_len,
911 * 1) Owner inode update
912 * 2) Ref count update on old xattr block
914 * 4) block bitmap update for new xattr block
915 * 5) group descriptor for new xattr block
916 * 6) block bitmap update for old xattr block
917 * 7) group descriptor for old block
919 * 6 & 7 can happen if we have two racing threads T_a and T_b
920 * which are each trying to set an xattr on inodes I_a and I_b
921 * which were both initially sharing an xattr block.
926 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
929 * In case of inline data, we may push out the data to a block,
930 * so we need to reserve credits for this eventuality
932 if (inode && ext4_has_inline_data(inode))
933 credits += ext4_writepage_trans_blocks(inode) + 1;
935 /* We are done if ea_inode feature is not enabled. */
936 if (!ext4_has_feature_ea_inode(sb))
939 /* New ea_inode, inode map, block bitmap, group descriptor. */
943 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
945 /* Indirection block or one level of extent tree. */
948 /* Block bitmap and group descriptor updates for each block. */
949 credits += blocks * 2;
951 /* Blocks themselves. */
955 /* Dereference ea_inode holding old xattr value.
956 * Old ea_inode, inode map, block bitmap, group descriptor.
960 /* Data blocks for old ea_inode. */
961 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
963 /* Indirection block or one level of extent tree for old
968 /* Block bitmap and group descriptor updates for each block. */
969 credits += blocks * 2;
972 /* We may need to clone the existing xattr block in which case we need
973 * to increment ref counts for existing ea_inodes referenced by it.
976 struct ext4_xattr_entry *entry = BFIRST(block_bh);
978 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
979 if (entry->e_value_inum)
980 /* Ref count update on ea_inode. */
986 static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
989 struct ext4_iloc iloc;
993 inode_lock(ea_inode);
995 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
999 ref_count = ext4_xattr_inode_get_ref(ea_inode);
1000 ref_count += ref_change;
1001 ext4_xattr_inode_set_ref(ea_inode, ref_count);
1003 if (ref_change > 0) {
1004 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
1005 ea_inode->i_ino, ref_count);
1007 if (ref_count == 1) {
1008 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
1009 ea_inode->i_ino, ea_inode->i_nlink);
1011 set_nlink(ea_inode, 1);
1012 ext4_orphan_del(handle, ea_inode);
1015 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1016 ea_inode->i_ino, ref_count);
1018 if (ref_count == 0) {
1019 WARN_ONCE(ea_inode->i_nlink != 1,
1020 "EA inode %lu i_nlink=%u",
1021 ea_inode->i_ino, ea_inode->i_nlink);
1023 clear_nlink(ea_inode);
1024 ext4_orphan_add(handle, ea_inode);
1028 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1030 ext4_warning_inode(ea_inode,
1031 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1033 inode_unlock(ea_inode);
1037 static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1039 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1042 static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1044 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1047 static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1048 struct ext4_xattr_entry *first)
1050 struct inode *ea_inode;
1051 struct ext4_xattr_entry *entry;
1052 struct ext4_xattr_entry *failed_entry;
1053 unsigned int ea_ino;
1056 for (entry = first; !IS_LAST_ENTRY(entry);
1057 entry = EXT4_XATTR_NEXT(entry)) {
1058 if (!entry->e_value_inum)
1060 ea_ino = le32_to_cpu(entry->e_value_inum);
1061 err = ext4_xattr_inode_iget(parent, ea_ino,
1062 le32_to_cpu(entry->e_hash),
1066 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1068 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1078 failed_entry = entry;
1080 for (entry = first; entry != failed_entry;
1081 entry = EXT4_XATTR_NEXT(entry)) {
1082 if (!entry->e_value_inum)
1084 ea_ino = le32_to_cpu(entry->e_value_inum);
1085 err = ext4_xattr_inode_iget(parent, ea_ino,
1086 le32_to_cpu(entry->e_hash),
1089 ext4_warning(parent->i_sb,
1090 "cleanup ea_ino %u iget error %d", ea_ino,
1094 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1096 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1103 static int ext4_xattr_restart_fn(handle_t *handle, struct inode *inode,
1104 struct buffer_head *bh, bool block_csum, bool dirty)
1110 ext4_xattr_block_csum_set(inode, bh);
1111 error = ext4_handle_dirty_metadata(handle, NULL, bh);
1113 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
1122 ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1123 struct buffer_head *bh,
1124 struct ext4_xattr_entry *first, bool block_csum,
1125 struct ext4_xattr_inode_array **ea_inode_array,
1126 int extra_credits, bool skip_quota)
1128 struct inode *ea_inode;
1129 struct ext4_xattr_entry *entry;
1131 unsigned int ea_ino;
1135 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1136 credits = 2 + extra_credits;
1138 for (entry = first; !IS_LAST_ENTRY(entry);
1139 entry = EXT4_XATTR_NEXT(entry)) {
1140 if (!entry->e_value_inum)
1142 ea_ino = le32_to_cpu(entry->e_value_inum);
1143 err = ext4_xattr_inode_iget(parent, ea_ino,
1144 le32_to_cpu(entry->e_hash),
1149 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1151 ext4_warning_inode(ea_inode,
1152 "Expand inode array err=%d", err);
1157 err = ext4_journal_ensure_credits_fn(handle, credits, credits,
1158 ext4_free_metadata_revoke_credits(parent->i_sb, 1),
1159 ext4_xattr_restart_fn(handle, parent, bh, block_csum,
1162 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1167 err = ext4_journal_get_write_access(handle,
1168 parent->i_sb, bh, EXT4_JTR_NONE);
1170 ext4_warning_inode(ea_inode,
1171 "Re-get write access err=%d",
1177 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1179 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1185 ext4_xattr_inode_free_quota(parent, ea_inode,
1186 le32_to_cpu(entry->e_value_size));
1189 * Forget about ea_inode within the same transaction that
1190 * decrements the ref count. This avoids duplicate decrements in
1191 * case the rest of the work spills over to subsequent
1194 entry->e_value_inum = 0;
1195 entry->e_value_size = 0;
1202 * Note that we are deliberately skipping csum calculation for
1203 * the final update because we do not expect any journal
1204 * restarts until xattr block is freed.
1207 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1209 ext4_warning_inode(parent,
1210 "handle dirty metadata err=%d", err);
1215 * Release the xattr block BH: If the reference count is > 1, decrement it;
1216 * otherwise free the block.
1219 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
1220 struct buffer_head *bh,
1221 struct ext4_xattr_inode_array **ea_inode_array,
1224 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1228 BUFFER_TRACE(bh, "get_write_access");
1229 error = ext4_journal_get_write_access(handle, inode->i_sb, bh,
1236 hash = le32_to_cpu(BHDR(bh)->h_hash);
1237 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1239 ea_bdebug(bh, "refcount now=0; freeing");
1241 * This must happen under buffer lock for
1242 * ext4_xattr_block_set() to reliably detect freed block
1244 if (ea_block_cache) {
1245 struct mb_cache_entry *oe;
1247 oe = mb_cache_entry_delete_or_get(ea_block_cache, hash,
1251 mb_cache_entry_wait_unused(oe);
1252 mb_cache_entry_put(ea_block_cache, oe);
1259 if (ext4_has_feature_ea_inode(inode->i_sb))
1260 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1262 true /* block_csum */,
1265 true /* skip_quota */);
1266 ext4_free_blocks(handle, inode, bh, 0, 1,
1267 EXT4_FREE_BLOCKS_METADATA |
1268 EXT4_FREE_BLOCKS_FORGET);
1271 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1272 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1273 struct mb_cache_entry *ce;
1275 if (ea_block_cache) {
1276 ce = mb_cache_entry_get(ea_block_cache, hash,
1279 set_bit(MBE_REUSABLE_B, &ce->e_flags);
1280 mb_cache_entry_put(ea_block_cache, ce);
1285 ext4_xattr_block_csum_set(inode, bh);
1287 * Beware of this ugliness: Releasing of xattr block references
1288 * from different inodes can race and so we have to protect
1289 * from a race where someone else frees the block (and releases
1290 * its journal_head) before we are done dirtying the buffer. In
1291 * nojournal mode this race is harmless and we actually cannot
1292 * call ext4_handle_dirty_metadata() with locked buffer as
1293 * that function can call sync_dirty_buffer() so for that case
1294 * we handle the dirtying after unlocking the buffer.
1296 if (ext4_handle_valid(handle))
1297 error = ext4_handle_dirty_metadata(handle, inode, bh);
1299 if (!ext4_handle_valid(handle))
1300 error = ext4_handle_dirty_metadata(handle, inode, bh);
1302 ext4_handle_sync(handle);
1303 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
1304 ea_bdebug(bh, "refcount now=%d; releasing",
1305 le32_to_cpu(BHDR(bh)->h_refcount));
1308 ext4_std_error(inode->i_sb, error);
1313 * Find the available free space for EAs. This also returns the total number of
1314 * bytes used by EA entries.
1316 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1317 size_t *min_offs, void *base, int *total)
1319 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1320 if (!last->e_value_inum && last->e_value_size) {
1321 size_t offs = le16_to_cpu(last->e_value_offs);
1322 if (offs < *min_offs)
1326 *total += EXT4_XATTR_LEN(last->e_name_len);
1328 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1332 * Write the value of the EA in an inode.
1334 static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1335 const void *buf, int bufsize)
1337 struct buffer_head *bh = NULL;
1338 unsigned long block = 0;
1339 int blocksize = ea_inode->i_sb->s_blocksize;
1340 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
1341 int csize, wsize = 0;
1342 int ret = 0, ret2 = 0;
1346 while (ret >= 0 && ret < max_blocks) {
1347 struct ext4_map_blocks map;
1348 map.m_lblk = block += ret;
1349 map.m_len = max_blocks -= ret;
1351 ret = ext4_map_blocks(handle, ea_inode, &map,
1352 EXT4_GET_BLOCKS_CREATE);
1354 ext4_mark_inode_dirty(handle, ea_inode);
1355 if (ret == -ENOSPC &&
1356 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1368 while (wsize < bufsize) {
1370 csize = (bufsize - wsize) > blocksize ? blocksize :
1372 bh = ext4_getblk(handle, ea_inode, block, 0);
1377 EXT4_ERROR_INODE(ea_inode,
1378 "ext4_getblk() return bh = NULL");
1379 return -EFSCORRUPTED;
1381 ret = ext4_journal_get_write_access(handle, ea_inode->i_sb, bh,
1386 memcpy(bh->b_data, buf, csize);
1387 set_buffer_uptodate(bh);
1388 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1395 inode_lock(ea_inode);
1396 i_size_write(ea_inode, wsize);
1397 ext4_update_i_disksize(ea_inode, wsize);
1398 inode_unlock(ea_inode);
1400 ret2 = ext4_mark_inode_dirty(handle, ea_inode);
1401 if (unlikely(ret2 && !ret))
1411 * Create an inode to store the value of a large EA.
1413 static struct inode *ext4_xattr_inode_create(handle_t *handle,
1414 struct inode *inode, u32 hash)
1416 struct inode *ea_inode = NULL;
1417 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
1420 if (inode->i_sb->s_root == NULL) {
1421 ext4_warning(inode->i_sb,
1422 "refuse to create EA inode when umounting");
1424 return ERR_PTR(-EINVAL);
1428 * Let the next inode be the goal, so we try and allocate the EA inode
1429 * in the same group, or nearby one.
1431 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
1432 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
1434 if (!IS_ERR(ea_inode)) {
1435 ea_inode->i_op = &ext4_file_inode_operations;
1436 ea_inode->i_fop = &ext4_file_operations;
1437 ext4_set_aops(ea_inode);
1438 ext4_xattr_inode_set_class(ea_inode);
1439 unlock_new_inode(ea_inode);
1440 ext4_xattr_inode_set_ref(ea_inode, 1);
1441 ext4_xattr_inode_set_hash(ea_inode, hash);
1442 err = ext4_mark_inode_dirty(handle, ea_inode);
1444 err = ext4_inode_attach_jinode(ea_inode);
1446 if (ext4_xattr_inode_dec_ref(handle, ea_inode))
1447 ext4_warning_inode(ea_inode,
1448 "cleanup dec ref error %d", err);
1450 return ERR_PTR(err);
1454 * Xattr inodes are shared therefore quota charging is performed
1455 * at a higher level.
1457 dquot_free_inode(ea_inode);
1458 dquot_drop(ea_inode);
1459 inode_lock(ea_inode);
1460 ea_inode->i_flags |= S_NOQUOTA;
1461 inode_unlock(ea_inode);
1467 static struct inode *
1468 ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1469 size_t value_len, u32 hash)
1471 struct inode *ea_inode;
1472 struct mb_cache_entry *ce;
1473 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1476 if (!ea_inode_cache)
1479 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1483 WARN_ON_ONCE(ext4_handle_valid(journal_current_handle()) &&
1484 !(current->flags & PF_MEMALLOC_NOFS));
1486 ea_data = kvmalloc(value_len, GFP_KERNEL);
1488 mb_cache_entry_put(ea_inode_cache, ce);
1493 ea_inode = ext4_iget(inode->i_sb, ce->e_value,
1494 EXT4_IGET_EA_INODE);
1495 if (IS_ERR(ea_inode))
1497 ext4_xattr_inode_set_class(ea_inode);
1498 if (i_size_read(ea_inode) == value_len &&
1499 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
1500 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1502 !memcmp(value, ea_data, value_len)) {
1503 mb_cache_entry_touch(ea_inode_cache, ce);
1504 mb_cache_entry_put(ea_inode_cache, ce);
1510 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1517 * Add value of the EA in an inode.
1519 static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1520 const void *value, size_t value_len,
1521 struct inode **ret_inode)
1523 struct inode *ea_inode;
1527 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1528 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1530 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1536 *ret_inode = ea_inode;
1540 /* Create an inode for the EA value */
1541 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
1542 if (IS_ERR(ea_inode))
1543 return PTR_ERR(ea_inode);
1545 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
1547 ext4_xattr_inode_dec_ref(handle, ea_inode);
1552 if (EA_INODE_CACHE(inode))
1553 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1554 ea_inode->i_ino, true /* reusable */);
1556 *ret_inode = ea_inode;
1561 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1562 * feature is enabled.
1564 #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1566 static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1567 struct ext4_xattr_search *s,
1568 handle_t *handle, struct inode *inode,
1571 struct ext4_xattr_entry *last, *next;
1572 struct ext4_xattr_entry *here = s->here;
1573 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
1574 int in_inode = i->in_inode;
1575 struct inode *old_ea_inode = NULL;
1576 struct inode *new_ea_inode = NULL;
1577 size_t old_size, new_size;
1580 /* Space used by old and new values. */
1581 old_size = (!s->not_found && !here->e_value_inum) ?
1582 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1583 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1586 * Optimization for the simple case when old and new values have the
1587 * same padded sizes. Not applicable if external inodes are involved.
1589 if (new_size && new_size == old_size) {
1590 size_t offs = le16_to_cpu(here->e_value_offs);
1591 void *val = s->base + offs;
1593 here->e_value_size = cpu_to_le32(i->value_len);
1594 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1595 memset(val, 0, new_size);
1597 memcpy(val, i->value, i->value_len);
1598 /* Clear padding bytes. */
1599 memset(val + i->value_len, 0, new_size - i->value_len);
1604 /* Compute min_offs and last. */
1606 for (; !IS_LAST_ENTRY(last); last = next) {
1607 next = EXT4_XATTR_NEXT(last);
1608 if ((void *)next >= s->end) {
1609 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
1610 ret = -EFSCORRUPTED;
1613 if (!last->e_value_inum && last->e_value_size) {
1614 size_t offs = le16_to_cpu(last->e_value_offs);
1615 if (offs < min_offs)
1620 /* Check whether we have enough space. */
1624 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1626 free += EXT4_XATTR_LEN(name_len) + old_size;
1628 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1634 * If storing the value in an external inode is an option,
1635 * reserve space for xattr entries/names in the external
1636 * attribute block so that a long value does not occupy the
1637 * whole space and prevent further entries being added.
1639 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1640 new_size && is_block &&
1641 (min_offs + old_size - new_size) <
1642 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1649 * Getting access to old and new ea inodes is subject to failures.
1650 * Finish that work before doing any modifications to the xattr data.
1652 if (!s->not_found && here->e_value_inum) {
1653 ret = ext4_xattr_inode_iget(inode,
1654 le32_to_cpu(here->e_value_inum),
1655 le32_to_cpu(here->e_hash),
1658 old_ea_inode = NULL;
1662 if (i->value && in_inode) {
1663 WARN_ON_ONCE(!i->value_len);
1665 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1669 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1673 new_ea_inode = NULL;
1674 ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
1680 /* We are ready to release ref count on the old_ea_inode. */
1681 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1683 /* Release newly required ref count on new_ea_inode. */
1687 err = ext4_xattr_inode_dec_ref(handle,
1690 ext4_warning_inode(new_ea_inode,
1691 "dec ref new_ea_inode err=%d",
1693 ext4_xattr_inode_free_quota(inode, new_ea_inode,
1699 ext4_xattr_inode_free_quota(inode, old_ea_inode,
1700 le32_to_cpu(here->e_value_size));
1703 /* No failures allowed past this point. */
1705 if (!s->not_found && here->e_value_size && !here->e_value_inum) {
1706 /* Remove the old value. */
1707 void *first_val = s->base + min_offs;
1708 size_t offs = le16_to_cpu(here->e_value_offs);
1709 void *val = s->base + offs;
1711 memmove(first_val + old_size, first_val, val - first_val);
1712 memset(first_val, 0, old_size);
1713 min_offs += old_size;
1715 /* Adjust all value offsets. */
1717 while (!IS_LAST_ENTRY(last)) {
1718 size_t o = le16_to_cpu(last->e_value_offs);
1720 if (!last->e_value_inum &&
1721 last->e_value_size && o < offs)
1722 last->e_value_offs = cpu_to_le16(o + old_size);
1723 last = EXT4_XATTR_NEXT(last);
1728 /* Remove old name. */
1729 size_t size = EXT4_XATTR_LEN(name_len);
1731 last = ENTRY((void *)last - size);
1732 memmove(here, (void *)here + size,
1733 (void *)last - (void *)here + sizeof(__u32));
1734 memset(last, 0, size);
1737 * Update i_inline_off - moved ibody region might contain
1738 * system.data attribute. Handling a failure here won't
1739 * cause other complications for setting an xattr.
1741 if (!is_block && ext4_has_inline_data(inode)) {
1742 ret = ext4_find_inline_data_nolock(inode);
1744 ext4_warning_inode(inode,
1745 "unable to update i_inline_off");
1749 } else if (s->not_found) {
1750 /* Insert new name. */
1751 size_t size = EXT4_XATTR_LEN(name_len);
1752 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1754 memmove((void *)here + size, here, rest);
1755 memset(here, 0, size);
1756 here->e_name_index = i->name_index;
1757 here->e_name_len = name_len;
1758 memcpy(here->e_name, i->name, name_len);
1760 /* This is an update, reset value info. */
1761 here->e_value_inum = 0;
1762 here->e_value_offs = 0;
1763 here->e_value_size = 0;
1767 /* Insert new value. */
1769 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
1770 } else if (i->value_len) {
1771 void *val = s->base + min_offs - new_size;
1773 here->e_value_offs = cpu_to_le16(min_offs - new_size);
1774 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1775 memset(val, 0, new_size);
1777 memcpy(val, i->value, i->value_len);
1778 /* Clear padding bytes. */
1779 memset(val + i->value_len, 0,
1780 new_size - i->value_len);
1783 here->e_value_size = cpu_to_le32(i->value_len);
1790 /* Entry hash calculation. */
1795 * Feed crc32c hash instead of the raw value for entry
1796 * hash calculation. This is to avoid walking
1797 * potentially long value buffer again.
1799 crc32c_hash = cpu_to_le32(
1800 ext4_xattr_inode_get_hash(new_ea_inode));
1801 hash = ext4_xattr_hash_entry(here->e_name,
1804 } else if (is_block) {
1805 __le32 *value = s->base + le16_to_cpu(
1806 here->e_value_offs);
1808 hash = ext4_xattr_hash_entry(here->e_name,
1809 here->e_name_len, value,
1812 here->e_hash = hash;
1816 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1825 struct ext4_xattr_block_find {
1826 struct ext4_xattr_search s;
1827 struct buffer_head *bh;
1831 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1832 struct ext4_xattr_block_find *bs)
1834 struct super_block *sb = inode->i_sb;
1837 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1838 i->name_index, i->name, i->value, (long)i->value_len);
1840 if (EXT4_I(inode)->i_file_acl) {
1841 /* The inode already has an extended attribute block. */
1842 bs->bh = ext4_sb_bread(sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
1843 if (IS_ERR(bs->bh)) {
1844 error = PTR_ERR(bs->bh);
1848 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1849 atomic_read(&(bs->bh->b_count)),
1850 le32_to_cpu(BHDR(bs->bh)->h_refcount));
1851 error = ext4_xattr_check_block(inode, bs->bh);
1854 /* Find the named attribute. */
1855 bs->s.base = BHDR(bs->bh);
1856 bs->s.first = BFIRST(bs->bh);
1857 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1858 bs->s.here = bs->s.first;
1859 error = xattr_find_entry(inode, &bs->s.here, bs->s.end,
1860 i->name_index, i->name, 1);
1861 if (error && error != -ENODATA)
1863 bs->s.not_found = error;
1869 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1870 struct ext4_xattr_info *i,
1871 struct ext4_xattr_block_find *bs)
1873 struct super_block *sb = inode->i_sb;
1874 struct buffer_head *new_bh = NULL;
1875 struct ext4_xattr_search s_copy = bs->s;
1876 struct ext4_xattr_search *s = &s_copy;
1877 struct mb_cache_entry *ce = NULL;
1879 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1880 struct inode *ea_inode = NULL, *tmp_inode;
1881 size_t old_ea_inode_quota = 0;
1882 unsigned int ea_ino;
1885 #define header(x) ((struct ext4_xattr_header *)(x))
1888 int offset = (char *)s->here - bs->bh->b_data;
1890 BUFFER_TRACE(bs->bh, "get_write_access");
1891 error = ext4_journal_get_write_access(handle, sb, bs->bh,
1895 lock_buffer(bs->bh);
1897 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
1898 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1901 * This must happen under buffer lock for
1902 * ext4_xattr_block_set() to reliably detect modified
1905 if (ea_block_cache) {
1906 struct mb_cache_entry *oe;
1908 oe = mb_cache_entry_delete_or_get(ea_block_cache,
1909 hash, bs->bh->b_blocknr);
1912 * Xattr block is getting reused. Leave
1915 mb_cache_entry_put(ea_block_cache, oe);
1919 ea_bdebug(bs->bh, "modifying in-place");
1920 error = ext4_xattr_set_entry(i, s, handle, inode,
1921 true /* is_block */);
1922 ext4_xattr_block_csum_set(inode, bs->bh);
1923 unlock_buffer(bs->bh);
1924 if (error == -EFSCORRUPTED)
1927 error = ext4_handle_dirty_metadata(handle,
1935 unlock_buffer(bs->bh);
1936 ea_bdebug(bs->bh, "cloning");
1937 s->base = kmemdup(BHDR(bs->bh), bs->bh->b_size, GFP_NOFS);
1939 if (s->base == NULL)
1941 s->first = ENTRY(header(s->base)+1);
1942 header(s->base)->h_refcount = cpu_to_le32(1);
1943 s->here = ENTRY(s->base + offset);
1944 s->end = s->base + bs->bh->b_size;
1947 * If existing entry points to an xattr inode, we need
1948 * to prevent ext4_xattr_set_entry() from decrementing
1949 * ref count on it because the reference belongs to the
1950 * original block. In this case, make the entry look
1951 * like it has an empty value.
1953 if (!s->not_found && s->here->e_value_inum) {
1954 ea_ino = le32_to_cpu(s->here->e_value_inum);
1955 error = ext4_xattr_inode_iget(inode, ea_ino,
1956 le32_to_cpu(s->here->e_hash),
1961 if (!ext4_test_inode_state(tmp_inode,
1962 EXT4_STATE_LUSTRE_EA_INODE)) {
1964 * Defer quota free call for previous
1965 * inode until success is guaranteed.
1967 old_ea_inode_quota = le32_to_cpu(
1968 s->here->e_value_size);
1972 s->here->e_value_inum = 0;
1973 s->here->e_value_size = 0;
1976 /* Allocate a buffer where we construct the new block. */
1977 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
1979 if (s->base == NULL)
1981 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1982 header(s->base)->h_blocks = cpu_to_le32(1);
1983 header(s->base)->h_refcount = cpu_to_le32(1);
1984 s->first = ENTRY(header(s->base)+1);
1985 s->here = ENTRY(header(s->base)+1);
1986 s->end = s->base + sb->s_blocksize;
1989 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
1990 if (error == -EFSCORRUPTED)
1995 if (i->value && s->here->e_value_inum) {
1997 * A ref count on ea_inode has been taken as part of the call to
1998 * ext4_xattr_set_entry() above. We would like to drop this
1999 * extra ref but we have to wait until the xattr block is
2000 * initialized and has its own ref count on the ea_inode.
2002 ea_ino = le32_to_cpu(s->here->e_value_inum);
2003 error = ext4_xattr_inode_iget(inode, ea_ino,
2004 le32_to_cpu(s->here->e_hash),
2013 if (!IS_LAST_ENTRY(s->first)) {
2014 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
2017 /* We found an identical block in the cache. */
2018 if (new_bh == bs->bh)
2019 ea_bdebug(new_bh, "keeping");
2023 #ifdef EXT4_XATTR_DEBUG
2024 WARN_ON_ONCE(dquot_initialize_needed(inode));
2026 /* The old block is released after updating
2028 error = dquot_alloc_block(inode,
2029 EXT4_C2B(EXT4_SB(sb), 1));
2032 BUFFER_TRACE(new_bh, "get_write_access");
2033 error = ext4_journal_get_write_access(
2038 lock_buffer(new_bh);
2040 * We have to be careful about races with
2041 * adding references to xattr block. Once we
2042 * hold buffer lock xattr block's state is
2043 * stable so we can check the additional
2046 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2047 if (ref > EXT4_XATTR_REFCOUNT_MAX) {
2049 * Undo everything and check mbcache
2052 unlock_buffer(new_bh);
2053 dquot_free_block(inode,
2054 EXT4_C2B(EXT4_SB(sb),
2057 mb_cache_entry_put(ea_block_cache, ce);
2062 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2063 if (ref == EXT4_XATTR_REFCOUNT_MAX)
2064 clear_bit(MBE_REUSABLE_B, &ce->e_flags);
2065 ea_bdebug(new_bh, "reusing; refcount now=%d",
2067 ext4_xattr_block_csum_set(inode, new_bh);
2068 unlock_buffer(new_bh);
2069 error = ext4_handle_dirty_metadata(handle,
2075 mb_cache_entry_touch(ea_block_cache, ce);
2076 mb_cache_entry_put(ea_block_cache, ce);
2078 } else if (bs->bh && s->base == bs->bh->b_data) {
2079 /* We were modifying this block in-place. */
2080 ea_bdebug(bs->bh, "keeping this block");
2081 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
2085 /* We need to allocate a new block */
2086 ext4_fsblk_t goal, block;
2088 #ifdef EXT4_XATTR_DEBUG
2089 WARN_ON_ONCE(dquot_initialize_needed(inode));
2091 goal = ext4_group_first_block_no(sb,
2092 EXT4_I(inode)->i_block_group);
2093 block = ext4_new_meta_blocks(handle, inode, goal, 0,
2098 ea_idebug(inode, "creating block %llu",
2099 (unsigned long long)block);
2101 new_bh = sb_getblk(sb, block);
2102 if (unlikely(!new_bh)) {
2105 ext4_free_blocks(handle, inode, NULL, block, 1,
2106 EXT4_FREE_BLOCKS_METADATA);
2109 error = ext4_xattr_inode_inc_ref_all(handle, inode,
2110 ENTRY(header(s->base)+1));
2114 /* Drop the extra ref on ea_inode. */
2115 error = ext4_xattr_inode_dec_ref(handle,
2118 ext4_warning_inode(ea_inode,
2125 lock_buffer(new_bh);
2126 error = ext4_journal_get_create_access(handle, sb,
2127 new_bh, EXT4_JTR_NONE);
2129 unlock_buffer(new_bh);
2133 memcpy(new_bh->b_data, s->base, new_bh->b_size);
2134 ext4_xattr_block_csum_set(inode, new_bh);
2135 set_buffer_uptodate(new_bh);
2136 unlock_buffer(new_bh);
2137 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
2138 error = ext4_handle_dirty_metadata(handle, inode,
2145 if (old_ea_inode_quota)
2146 ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
2148 /* Update the inode. */
2149 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
2151 /* Drop the previous xattr block. */
2152 if (bs->bh && bs->bh != new_bh) {
2153 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2155 ext4_xattr_release_block(handle, inode, bs->bh,
2157 0 /* extra_credits */);
2158 ext4_xattr_inode_array_free(ea_inode_array);
2166 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2168 ext4_warning_inode(ea_inode, "dec ref error=%d",
2171 /* If there was an error, revert the quota charge. */
2173 ext4_xattr_inode_free_quota(inode, ea_inode,
2174 i_size_read(ea_inode));
2178 mb_cache_entry_put(ea_block_cache, ce);
2180 if (!(bs->bh && s->base == bs->bh->b_data))
2186 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
2190 EXT4_ERROR_INODE(inode, "bad block %llu",
2191 EXT4_I(inode)->i_file_acl);
2197 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2198 struct ext4_xattr_ibody_find *is)
2200 struct ext4_xattr_ibody_header *header;
2201 struct ext4_inode *raw_inode;
2204 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2207 raw_inode = ext4_raw_inode(&is->iloc);
2208 header = IHDR(inode, raw_inode);
2209 is->s.base = is->s.first = IFIRST(header);
2210 is->s.here = is->s.first;
2211 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2212 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2213 error = xattr_check_inode(inode, header, is->s.end);
2216 /* Find the named attribute. */
2217 error = xattr_find_entry(inode, &is->s.here, is->s.end,
2218 i->name_index, i->name, 0);
2219 if (error && error != -ENODATA)
2221 is->s.not_found = error;
2226 int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
2227 struct ext4_xattr_info *i,
2228 struct ext4_xattr_ibody_find *is)
2230 struct ext4_xattr_ibody_header *header;
2231 struct ext4_xattr_search *s = &is->s;
2234 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2237 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
2240 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2241 if (!IS_LAST_ENTRY(s->first)) {
2242 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2243 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2245 header->h_magic = cpu_to_le32(0);
2246 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2251 static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2252 struct ext4_xattr_info *i)
2256 /* When e_value_inum is set the value is stored externally. */
2257 if (s->here->e_value_inum)
2259 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2261 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2262 return !memcmp(value, i->value, i->value_len);
2265 static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2267 struct buffer_head *bh;
2270 if (!EXT4_I(inode)->i_file_acl)
2272 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2275 error = ext4_xattr_check_block(inode, bh);
2278 return ERR_PTR(error);
2284 * ext4_xattr_set_handle()
2286 * Create, replace or remove an extended attribute for this inode. Value
2287 * is NULL to remove an existing extended attribute, and non-NULL to
2288 * either replace an existing extended attribute, or create a new extended
2289 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2290 * specify that an extended attribute must exist and must not exist
2291 * previous to the call, respectively.
2293 * Returns 0, or a negative error number on failure.
2296 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
2297 const char *name, const void *value, size_t value_len,
2300 struct ext4_xattr_info i = {
2301 .name_index = name_index,
2304 .value_len = value_len,
2307 struct ext4_xattr_ibody_find is = {
2308 .s = { .not_found = -ENODATA, },
2310 struct ext4_xattr_block_find bs = {
2311 .s = { .not_found = -ENODATA, },
2318 if (strlen(name) > 255)
2321 ext4_write_lock_xattr(inode, &no_expand);
2323 /* Check journal credits under write lock. */
2324 if (ext4_handle_valid(handle)) {
2325 struct buffer_head *bh;
2328 bh = ext4_xattr_get_block(inode);
2330 error = PTR_ERR(bh);
2334 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2336 flags & XATTR_CREATE);
2339 if (jbd2_handle_buffer_credits(handle) < credits) {
2343 WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
2346 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
2350 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
2351 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2352 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
2353 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
2356 error = ext4_xattr_ibody_find(inode, &i, &is);
2360 error = ext4_xattr_block_find(inode, &i, &bs);
2363 if (is.s.not_found && bs.s.not_found) {
2365 if (flags & XATTR_REPLACE)
2372 if (flags & XATTR_CREATE)
2377 if (!is.s.not_found)
2378 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2379 else if (!bs.s.not_found)
2380 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2383 /* Xattr value did not change? Save us some work and bail out */
2384 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2386 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2389 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2390 (EXT4_XATTR_SIZE(i.value_len) >
2391 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2394 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2395 if (!error && !bs.s.not_found) {
2397 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2398 } else if (error == -ENOSPC) {
2399 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2402 error = ext4_xattr_block_find(inode, &i, &bs);
2406 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2407 if (!error && !is.s.not_found) {
2409 error = ext4_xattr_ibody_set(handle, inode, &i,
2411 } else if (error == -ENOSPC) {
2413 * Xattr does not fit in the block, store at
2414 * external inode if possible.
2416 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2417 i.value_len && !i.in_inode) {
2425 ext4_xattr_update_super_block(handle, inode->i_sb);
2426 inode->i_ctime = current_time(inode);
2427 inode_inc_iversion(inode);
2430 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
2432 * The bh is consumed by ext4_mark_iloc_dirty, even with
2437 ext4_handle_sync(handle);
2439 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle);
2444 ext4_write_unlock_xattr(inode, &no_expand);
2448 int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2449 bool is_create, int *credits)
2451 struct buffer_head *bh;
2456 if (!EXT4_SB(inode->i_sb)->s_journal)
2459 down_read(&EXT4_I(inode)->xattr_sem);
2461 bh = ext4_xattr_get_block(inode);
2465 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2466 value_len, is_create);
2471 up_read(&EXT4_I(inode)->xattr_sem);
2478 * Like ext4_xattr_set_handle, but start from an inode. This extended
2479 * attribute modification is a filesystem transaction by itself.
2481 * Returns 0, or a negative error number on failure.
2484 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
2485 const void *value, size_t value_len, int flags)
2488 struct super_block *sb = inode->i_sb;
2489 int error, retries = 0;
2492 error = dquot_initialize(inode);
2497 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2502 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
2503 if (IS_ERR(handle)) {
2504 error = PTR_ERR(handle);
2508 error = ext4_xattr_set_handle(handle, inode, name_index, name,
2509 value, value_len, flags);
2510 error2 = ext4_journal_stop(handle);
2511 if (error == -ENOSPC &&
2512 ext4_should_retry_alloc(sb, &retries))
2517 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, NULL);
2523 * Shift the EA entries in the inode to create space for the increased
2526 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2527 int value_offs_shift, void *to,
2528 void *from, size_t n)
2530 struct ext4_xattr_entry *last = entry;
2533 /* We always shift xattr headers further thus offsets get lower */
2534 BUG_ON(value_offs_shift > 0);
2536 /* Adjust the value offsets of the entries */
2537 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2538 if (!last->e_value_inum && last->e_value_size) {
2539 new_offs = le16_to_cpu(last->e_value_offs) +
2541 last->e_value_offs = cpu_to_le16(new_offs);
2544 /* Shift the entries by n bytes */
2545 memmove(to, from, n);
2549 * Move xattr pointed to by 'entry' from inode into external xattr block
2551 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2552 struct ext4_inode *raw_inode,
2553 struct ext4_xattr_entry *entry)
2555 struct ext4_xattr_ibody_find *is = NULL;
2556 struct ext4_xattr_block_find *bs = NULL;
2557 char *buffer = NULL, *b_entry_name = NULL;
2558 size_t value_size = le32_to_cpu(entry->e_value_size);
2559 struct ext4_xattr_info i = {
2562 .name_index = entry->e_name_index,
2563 .in_inode = !!entry->e_value_inum,
2565 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2566 int needs_kvfree = 0;
2569 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2570 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2571 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2572 if (!is || !bs || !b_entry_name) {
2577 is->s.not_found = -ENODATA;
2578 bs->s.not_found = -ENODATA;
2582 /* Save the entry name and the entry value */
2583 if (entry->e_value_inum) {
2584 buffer = kvmalloc(value_size, GFP_NOFS);
2590 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
2594 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2595 buffer = (void *)IFIRST(header) + value_offs;
2598 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2599 b_entry_name[entry->e_name_len] = '\0';
2600 i.name = b_entry_name;
2602 error = ext4_get_inode_loc(inode, &is->iloc);
2606 error = ext4_xattr_ibody_find(inode, &i, is);
2611 i.value_len = value_size;
2612 error = ext4_xattr_block_find(inode, &i, bs);
2616 /* Move ea entry from the inode into the block */
2617 error = ext4_xattr_block_set(handle, inode, &i, bs);
2621 /* Remove the chosen entry from the inode */
2624 error = ext4_xattr_ibody_set(handle, inode, &i, is);
2627 kfree(b_entry_name);
2628 if (needs_kvfree && buffer)
2631 brelse(is->iloc.bh);
2640 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2641 struct ext4_inode *raw_inode,
2642 int isize_diff, size_t ifree,
2643 size_t bfree, int *total_ino)
2645 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2646 struct ext4_xattr_entry *small_entry;
2647 struct ext4_xattr_entry *entry;
2648 struct ext4_xattr_entry *last;
2649 unsigned int entry_size; /* EA entry size */
2650 unsigned int total_size; /* EA entry size + value size */
2651 unsigned int min_total_size;
2654 while (isize_diff > ifree) {
2657 min_total_size = ~0U;
2658 last = IFIRST(header);
2659 /* Find the entry best suited to be pushed into EA block */
2660 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2661 /* never move system.data out of the inode */
2662 if ((last->e_name_len == 4) &&
2663 (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
2664 !memcmp(last->e_name, "data", 4))
2666 total_size = EXT4_XATTR_LEN(last->e_name_len);
2667 if (!last->e_value_inum)
2668 total_size += EXT4_XATTR_SIZE(
2669 le32_to_cpu(last->e_value_size));
2670 if (total_size <= bfree &&
2671 total_size < min_total_size) {
2672 if (total_size + ifree < isize_diff) {
2676 min_total_size = total_size;
2681 if (entry == NULL) {
2682 if (small_entry == NULL)
2684 entry = small_entry;
2687 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
2688 total_size = entry_size;
2689 if (!entry->e_value_inum)
2690 total_size += EXT4_XATTR_SIZE(
2691 le32_to_cpu(entry->e_value_size));
2692 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2697 *total_ino -= entry_size;
2698 ifree += total_size;
2699 bfree -= total_size;
2706 * Expand an inode by new_extra_isize bytes when EAs are present.
2707 * Returns 0 on success or negative error number on failure.
2709 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2710 struct ext4_inode *raw_inode, handle_t *handle)
2712 struct ext4_xattr_ibody_header *header;
2713 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2714 static unsigned int mnt_count;
2716 size_t ifree, bfree;
2719 int error = 0, tried_min_extra_isize = 0;
2720 int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
2721 int isize_diff; /* How much do we need to grow i_extra_isize */
2724 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
2725 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
2728 header = IHDR(inode, raw_inode);
2731 * Check if enough free space is available in the inode to shift the
2732 * entries ahead by new_extra_isize.
2735 base = IFIRST(header);
2736 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2737 min_offs = end - base;
2738 total_ino = sizeof(struct ext4_xattr_ibody_header) + sizeof(u32);
2740 error = xattr_check_inode(inode, header, end);
2744 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
2745 if (ifree >= isize_diff)
2749 * Enough free space isn't available in the inode, check if
2750 * EA block can hold new_extra_isize bytes.
2752 if (EXT4_I(inode)->i_file_acl) {
2753 struct buffer_head *bh;
2755 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2757 error = PTR_ERR(bh);
2760 error = ext4_xattr_check_block(inode, bh);
2766 end = bh->b_data + bh->b_size;
2767 min_offs = end - base;
2768 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2771 if (bfree + ifree < isize_diff) {
2772 if (!tried_min_extra_isize && s_min_extra_isize) {
2773 tried_min_extra_isize++;
2774 new_extra_isize = s_min_extra_isize;
2781 bfree = inode->i_sb->s_blocksize;
2784 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2785 isize_diff, ifree, bfree,
2788 if (error == -ENOSPC && !tried_min_extra_isize &&
2789 s_min_extra_isize) {
2790 tried_min_extra_isize++;
2791 new_extra_isize = s_min_extra_isize;
2797 /* Adjust the offsets and shift the remaining entries ahead */
2798 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
2799 - new_extra_isize, (void *)raw_inode +
2800 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
2801 (void *)header, total_ino);
2802 EXT4_I(inode)->i_extra_isize = new_extra_isize;
2804 if (ext4_has_inline_data(inode))
2805 error = ext4_find_inline_data_nolock(inode);
2808 if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
2809 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2811 mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
2816 #define EIA_INCR 16 /* must be 2^n */
2817 #define EIA_MASK (EIA_INCR - 1)
2819 /* Add the large xattr @inode into @ea_inode_array for deferred iput().
2820 * If @ea_inode_array is new or full it will be grown and the old
2821 * contents copied over.
2824 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2825 struct inode *inode)
2827 if (*ea_inode_array == NULL) {
2829 * Start with 15 inodes, so it fits into a power-of-two size.
2830 * If *ea_inode_array is NULL, this is essentially offsetof()
2833 kmalloc(offsetof(struct ext4_xattr_inode_array,
2836 if (*ea_inode_array == NULL)
2838 (*ea_inode_array)->count = 0;
2839 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
2840 /* expand the array once all 15 + n * 16 slots are full */
2841 struct ext4_xattr_inode_array *new_array = NULL;
2842 int count = (*ea_inode_array)->count;
2844 /* if new_array is NULL, this is essentially offsetof() */
2845 new_array = kmalloc(
2846 offsetof(struct ext4_xattr_inode_array,
2847 inodes[count + EIA_INCR]),
2849 if (new_array == NULL)
2851 memcpy(new_array, *ea_inode_array,
2852 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2853 kfree(*ea_inode_array);
2854 *ea_inode_array = new_array;
2856 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
2861 * ext4_xattr_delete_inode()
2863 * Free extended attribute resources associated with this inode. Traverse
2864 * all entries and decrement reference on any xattr inodes associated with this
2865 * inode. This is called immediately before an inode is freed. We have exclusive
2866 * access to the inode. If an orphan inode is deleted it will also release its
2867 * references on xattr block and xattr inodes.
2869 int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2870 struct ext4_xattr_inode_array **ea_inode_array,
2873 struct buffer_head *bh = NULL;
2874 struct ext4_xattr_ibody_header *header;
2875 struct ext4_iloc iloc = { .bh = NULL };
2876 struct ext4_xattr_entry *entry;
2877 struct inode *ea_inode;
2880 error = ext4_journal_ensure_credits(handle, extra_credits,
2881 ext4_free_metadata_revoke_credits(inode->i_sb, 1));
2883 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2887 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2888 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2890 error = ext4_get_inode_loc(inode, &iloc);
2892 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
2896 error = ext4_journal_get_write_access(handle, inode->i_sb,
2897 iloc.bh, EXT4_JTR_NONE);
2899 EXT4_ERROR_INODE(inode, "write access (error %d)",
2904 header = IHDR(inode, ext4_raw_inode(&iloc));
2905 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2906 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2908 false /* block_csum */,
2911 false /* skip_quota */);
2914 if (EXT4_I(inode)->i_file_acl) {
2915 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2917 error = PTR_ERR(bh);
2918 if (error == -EIO) {
2919 EXT4_ERROR_INODE_ERR(inode, EIO,
2920 "block %llu read error",
2921 EXT4_I(inode)->i_file_acl);
2926 error = ext4_xattr_check_block(inode, bh);
2930 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2931 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2932 entry = EXT4_XATTR_NEXT(entry)) {
2933 if (!entry->e_value_inum)
2935 error = ext4_xattr_inode_iget(inode,
2936 le32_to_cpu(entry->e_value_inum),
2937 le32_to_cpu(entry->e_hash),
2941 ext4_xattr_inode_free_quota(inode, ea_inode,
2942 le32_to_cpu(entry->e_value_size));
2948 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2951 * Update i_file_acl value in the same transaction that releases
2954 EXT4_I(inode)->i_file_acl = 0;
2955 error = ext4_mark_inode_dirty(handle, inode);
2957 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2961 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle);
2970 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
2974 if (ea_inode_array == NULL)
2977 for (idx = 0; idx < ea_inode_array->count; ++idx)
2978 iput(ea_inode_array->inodes[idx]);
2979 kfree(ea_inode_array);
2983 * ext4_xattr_block_cache_insert()
2985 * Create a new entry in the extended attribute block cache, and insert
2986 * it unless such an entry is already in the cache.
2988 * Returns 0, or a negative error number on failure.
2991 ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
2992 struct buffer_head *bh)
2994 struct ext4_xattr_header *header = BHDR(bh);
2995 __u32 hash = le32_to_cpu(header->h_hash);
2996 int reusable = le32_to_cpu(header->h_refcount) <
2997 EXT4_XATTR_REFCOUNT_MAX;
3000 if (!ea_block_cache)
3002 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
3003 bh->b_blocknr, reusable);
3005 if (error == -EBUSY)
3006 ea_bdebug(bh, "already in cache");
3008 ea_bdebug(bh, "inserting [%x]", (int)hash);
3014 * Compare two extended attribute blocks for equality.
3016 * Returns 0 if the blocks are equal, 1 if they differ, and
3017 * a negative error number on errors.
3020 ext4_xattr_cmp(struct ext4_xattr_header *header1,
3021 struct ext4_xattr_header *header2)
3023 struct ext4_xattr_entry *entry1, *entry2;
3025 entry1 = ENTRY(header1+1);
3026 entry2 = ENTRY(header2+1);
3027 while (!IS_LAST_ENTRY(entry1)) {
3028 if (IS_LAST_ENTRY(entry2))
3030 if (entry1->e_hash != entry2->e_hash ||
3031 entry1->e_name_index != entry2->e_name_index ||
3032 entry1->e_name_len != entry2->e_name_len ||
3033 entry1->e_value_size != entry2->e_value_size ||
3034 entry1->e_value_inum != entry2->e_value_inum ||
3035 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
3037 if (!entry1->e_value_inum &&
3038 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
3039 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
3040 le32_to_cpu(entry1->e_value_size)))
3043 entry1 = EXT4_XATTR_NEXT(entry1);
3044 entry2 = EXT4_XATTR_NEXT(entry2);
3046 if (!IS_LAST_ENTRY(entry2))
3052 * ext4_xattr_block_cache_find()
3054 * Find an identical extended attribute block.
3056 * Returns a pointer to the block found, or NULL if such a block was
3057 * not found or an error occurred.
3059 static struct buffer_head *
3060 ext4_xattr_block_cache_find(struct inode *inode,
3061 struct ext4_xattr_header *header,
3062 struct mb_cache_entry **pce)
3064 __u32 hash = le32_to_cpu(header->h_hash);
3065 struct mb_cache_entry *ce;
3066 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
3068 if (!ea_block_cache)
3070 if (!header->h_hash)
3071 return NULL; /* never share */
3072 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
3073 ce = mb_cache_entry_find_first(ea_block_cache, hash);
3075 struct buffer_head *bh;
3077 bh = ext4_sb_bread(inode->i_sb, ce->e_value, REQ_PRIO);
3079 if (PTR_ERR(bh) == -ENOMEM)
3082 EXT4_ERROR_INODE(inode, "block %lu read error",
3083 (unsigned long)ce->e_value);
3084 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
3089 ce = mb_cache_entry_find_next(ea_block_cache, ce);
3094 #define NAME_HASH_SHIFT 5
3095 #define VALUE_HASH_SHIFT 16
3098 * ext4_xattr_hash_entry()
3100 * Compute the hash of an extended attribute.
3102 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3107 while (name_len--) {
3108 hash = (hash << NAME_HASH_SHIFT) ^
3109 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3112 while (value_count--) {
3113 hash = (hash << VALUE_HASH_SHIFT) ^
3114 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3115 le32_to_cpu(*value++);
3117 return cpu_to_le32(hash);
3120 #undef NAME_HASH_SHIFT
3121 #undef VALUE_HASH_SHIFT
3123 #define BLOCK_HASH_SHIFT 16
3126 * ext4_xattr_rehash()
3128 * Re-compute the extended attribute hash value after an entry has changed.
3130 static void ext4_xattr_rehash(struct ext4_xattr_header *header)
3132 struct ext4_xattr_entry *here;
3135 here = ENTRY(header+1);
3136 while (!IS_LAST_ENTRY(here)) {
3137 if (!here->e_hash) {
3138 /* Block is not shared if an entry's hash value == 0 */
3142 hash = (hash << BLOCK_HASH_SHIFT) ^
3143 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3144 le32_to_cpu(here->e_hash);
3145 here = EXT4_XATTR_NEXT(here);
3147 header->h_hash = cpu_to_le32(hash);
3150 #undef BLOCK_HASH_SHIFT
3152 #define HASH_BUCKET_BITS 10
3155 ext4_xattr_create_cache(void)
3157 return mb_cache_create(HASH_BUCKET_BITS);
3160 void ext4_xattr_destroy_cache(struct mb_cache *cache)
3163 mb_cache_destroy(cache);