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, EXT4_SB(sb)->s_sbh) == 0) {
805 ext4_set_feature_xattr(sb);
806 ext4_handle_dirty_super(handle, sb);
810 int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
812 struct ext4_iloc iloc = { .bh = NULL };
813 struct buffer_head *bh = NULL;
814 struct ext4_inode *raw_inode;
815 struct ext4_xattr_ibody_header *header;
816 struct ext4_xattr_entry *entry;
817 qsize_t ea_inode_refs = 0;
821 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
823 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
824 ret = ext4_get_inode_loc(inode, &iloc);
827 raw_inode = ext4_raw_inode(&iloc);
828 header = IHDR(inode, raw_inode);
829 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
830 ret = xattr_check_inode(inode, header, end);
834 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
835 entry = EXT4_XATTR_NEXT(entry))
836 if (entry->e_value_inum)
840 if (EXT4_I(inode)->i_file_acl) {
841 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
848 ret = ext4_xattr_check_block(inode, bh);
852 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
853 entry = EXT4_XATTR_NEXT(entry))
854 if (entry->e_value_inum)
857 *usage = ea_inode_refs + 1;
865 static inline size_t round_up_cluster(struct inode *inode, size_t length)
867 struct super_block *sb = inode->i_sb;
868 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
870 size_t mask = ~(cluster_size - 1);
872 return (length + cluster_size - 1) & mask;
875 static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
879 err = dquot_alloc_inode(inode);
882 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
884 dquot_free_inode(inode);
888 static void ext4_xattr_inode_free_quota(struct inode *parent,
889 struct inode *ea_inode,
893 ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
895 dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
896 dquot_free_inode(parent);
899 int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
900 struct buffer_head *block_bh, size_t value_len,
907 * 1) Owner inode update
908 * 2) Ref count update on old xattr block
910 * 4) block bitmap update for new xattr block
911 * 5) group descriptor for new xattr block
912 * 6) block bitmap update for old xattr block
913 * 7) group descriptor for old block
915 * 6 & 7 can happen if we have two racing threads T_a and T_b
916 * which are each trying to set an xattr on inodes I_a and I_b
917 * which were both initially sharing an xattr block.
922 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
925 * In case of inline data, we may push out the data to a block,
926 * so we need to reserve credits for this eventuality
928 if (inode && ext4_has_inline_data(inode))
929 credits += ext4_writepage_trans_blocks(inode) + 1;
931 /* We are done if ea_inode feature is not enabled. */
932 if (!ext4_has_feature_ea_inode(sb))
935 /* New ea_inode, inode map, block bitmap, group descriptor. */
939 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
941 /* Indirection block or one level of extent tree. */
944 /* Block bitmap and group descriptor updates for each block. */
945 credits += blocks * 2;
947 /* Blocks themselves. */
951 /* Dereference ea_inode holding old xattr value.
952 * Old ea_inode, inode map, block bitmap, group descriptor.
956 /* Data blocks for old ea_inode. */
957 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
959 /* Indirection block or one level of extent tree for old
964 /* Block bitmap and group descriptor updates for each block. */
965 credits += blocks * 2;
968 /* We may need to clone the existing xattr block in which case we need
969 * to increment ref counts for existing ea_inodes referenced by it.
972 struct ext4_xattr_entry *entry = BFIRST(block_bh);
974 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
975 if (entry->e_value_inum)
976 /* Ref count update on ea_inode. */
982 static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
985 struct ext4_iloc iloc;
989 inode_lock(ea_inode);
991 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
995 ref_count = ext4_xattr_inode_get_ref(ea_inode);
996 ref_count += ref_change;
997 ext4_xattr_inode_set_ref(ea_inode, ref_count);
999 if (ref_change > 0) {
1000 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
1001 ea_inode->i_ino, ref_count);
1003 if (ref_count == 1) {
1004 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
1005 ea_inode->i_ino, ea_inode->i_nlink);
1007 set_nlink(ea_inode, 1);
1008 ext4_orphan_del(handle, ea_inode);
1011 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1012 ea_inode->i_ino, ref_count);
1014 if (ref_count == 0) {
1015 WARN_ONCE(ea_inode->i_nlink != 1,
1016 "EA inode %lu i_nlink=%u",
1017 ea_inode->i_ino, ea_inode->i_nlink);
1019 clear_nlink(ea_inode);
1020 ext4_orphan_add(handle, ea_inode);
1024 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1026 ext4_warning_inode(ea_inode,
1027 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1029 inode_unlock(ea_inode);
1033 static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1035 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1038 static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1040 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1043 static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1044 struct ext4_xattr_entry *first)
1046 struct inode *ea_inode;
1047 struct ext4_xattr_entry *entry;
1048 struct ext4_xattr_entry *failed_entry;
1049 unsigned int ea_ino;
1052 for (entry = first; !IS_LAST_ENTRY(entry);
1053 entry = EXT4_XATTR_NEXT(entry)) {
1054 if (!entry->e_value_inum)
1056 ea_ino = le32_to_cpu(entry->e_value_inum);
1057 err = ext4_xattr_inode_iget(parent, ea_ino,
1058 le32_to_cpu(entry->e_hash),
1062 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1064 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1074 failed_entry = entry;
1076 for (entry = first; entry != failed_entry;
1077 entry = EXT4_XATTR_NEXT(entry)) {
1078 if (!entry->e_value_inum)
1080 ea_ino = le32_to_cpu(entry->e_value_inum);
1081 err = ext4_xattr_inode_iget(parent, ea_ino,
1082 le32_to_cpu(entry->e_hash),
1085 ext4_warning(parent->i_sb,
1086 "cleanup ea_ino %u iget error %d", ea_ino,
1090 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1092 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1099 static int ext4_xattr_restart_fn(handle_t *handle, struct inode *inode,
1100 struct buffer_head *bh, bool block_csum, bool dirty)
1106 ext4_xattr_block_csum_set(inode, bh);
1107 error = ext4_handle_dirty_metadata(handle, NULL, bh);
1109 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
1118 ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1119 struct buffer_head *bh,
1120 struct ext4_xattr_entry *first, bool block_csum,
1121 struct ext4_xattr_inode_array **ea_inode_array,
1122 int extra_credits, bool skip_quota)
1124 struct inode *ea_inode;
1125 struct ext4_xattr_entry *entry;
1127 unsigned int ea_ino;
1131 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1132 credits = 2 + extra_credits;
1134 for (entry = first; !IS_LAST_ENTRY(entry);
1135 entry = EXT4_XATTR_NEXT(entry)) {
1136 if (!entry->e_value_inum)
1138 ea_ino = le32_to_cpu(entry->e_value_inum);
1139 err = ext4_xattr_inode_iget(parent, ea_ino,
1140 le32_to_cpu(entry->e_hash),
1145 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1147 ext4_warning_inode(ea_inode,
1148 "Expand inode array err=%d", err);
1153 err = ext4_journal_ensure_credits_fn(handle, credits, credits,
1154 ext4_free_metadata_revoke_credits(parent->i_sb, 1),
1155 ext4_xattr_restart_fn(handle, parent, bh, block_csum,
1158 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1163 err = ext4_journal_get_write_access(handle, bh);
1165 ext4_warning_inode(ea_inode,
1166 "Re-get write access err=%d",
1172 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1174 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1180 ext4_xattr_inode_free_quota(parent, ea_inode,
1181 le32_to_cpu(entry->e_value_size));
1184 * Forget about ea_inode within the same transaction that
1185 * decrements the ref count. This avoids duplicate decrements in
1186 * case the rest of the work spills over to subsequent
1189 entry->e_value_inum = 0;
1190 entry->e_value_size = 0;
1197 * Note that we are deliberately skipping csum calculation for
1198 * the final update because we do not expect any journal
1199 * restarts until xattr block is freed.
1202 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1204 ext4_warning_inode(parent,
1205 "handle dirty metadata err=%d", err);
1210 * Release the xattr block BH: If the reference count is > 1, decrement it;
1211 * otherwise free the block.
1214 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
1215 struct buffer_head *bh,
1216 struct ext4_xattr_inode_array **ea_inode_array,
1219 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1223 BUFFER_TRACE(bh, "get_write_access");
1224 error = ext4_journal_get_write_access(handle, bh);
1230 hash = le32_to_cpu(BHDR(bh)->h_hash);
1231 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1233 ea_bdebug(bh, "refcount now=0; freeing");
1235 * This must happen under buffer lock for
1236 * ext4_xattr_block_set() to reliably detect freed block
1238 if (ea_block_cache) {
1239 struct mb_cache_entry *oe;
1241 oe = mb_cache_entry_delete_or_get(ea_block_cache, hash,
1245 mb_cache_entry_wait_unused(oe);
1246 mb_cache_entry_put(ea_block_cache, oe);
1253 if (ext4_has_feature_ea_inode(inode->i_sb))
1254 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1256 true /* block_csum */,
1259 true /* skip_quota */);
1260 ext4_free_blocks(handle, inode, bh, 0, 1,
1261 EXT4_FREE_BLOCKS_METADATA |
1262 EXT4_FREE_BLOCKS_FORGET);
1265 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1266 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1267 struct mb_cache_entry *ce;
1269 if (ea_block_cache) {
1270 ce = mb_cache_entry_get(ea_block_cache, hash,
1273 set_bit(MBE_REUSABLE_B, &ce->e_flags);
1274 mb_cache_entry_put(ea_block_cache, ce);
1279 ext4_xattr_block_csum_set(inode, bh);
1281 * Beware of this ugliness: Releasing of xattr block references
1282 * from different inodes can race and so we have to protect
1283 * from a race where someone else frees the block (and releases
1284 * its journal_head) before we are done dirtying the buffer. In
1285 * nojournal mode this race is harmless and we actually cannot
1286 * call ext4_handle_dirty_metadata() with locked buffer as
1287 * that function can call sync_dirty_buffer() so for that case
1288 * we handle the dirtying after unlocking the buffer.
1290 if (ext4_handle_valid(handle))
1291 error = ext4_handle_dirty_metadata(handle, inode, bh);
1293 if (!ext4_handle_valid(handle))
1294 error = ext4_handle_dirty_metadata(handle, inode, bh);
1296 ext4_handle_sync(handle);
1297 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
1298 ea_bdebug(bh, "refcount now=%d; releasing",
1299 le32_to_cpu(BHDR(bh)->h_refcount));
1302 ext4_std_error(inode->i_sb, error);
1307 * Find the available free space for EAs. This also returns the total number of
1308 * bytes used by EA entries.
1310 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1311 size_t *min_offs, void *base, int *total)
1313 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1314 if (!last->e_value_inum && last->e_value_size) {
1315 size_t offs = le16_to_cpu(last->e_value_offs);
1316 if (offs < *min_offs)
1320 *total += EXT4_XATTR_LEN(last->e_name_len);
1322 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1326 * Write the value of the EA in an inode.
1328 static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1329 const void *buf, int bufsize)
1331 struct buffer_head *bh = NULL;
1332 unsigned long block = 0;
1333 int blocksize = ea_inode->i_sb->s_blocksize;
1334 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
1335 int csize, wsize = 0;
1336 int ret = 0, ret2 = 0;
1340 while (ret >= 0 && ret < max_blocks) {
1341 struct ext4_map_blocks map;
1342 map.m_lblk = block += ret;
1343 map.m_len = max_blocks -= ret;
1345 ret = ext4_map_blocks(handle, ea_inode, &map,
1346 EXT4_GET_BLOCKS_CREATE);
1348 ext4_mark_inode_dirty(handle, ea_inode);
1349 if (ret == -ENOSPC &&
1350 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1362 while (wsize < bufsize) {
1364 csize = (bufsize - wsize) > blocksize ? blocksize :
1366 bh = ext4_getblk(handle, ea_inode, block, 0);
1371 EXT4_ERROR_INODE(ea_inode,
1372 "ext4_getblk() return bh = NULL");
1373 return -EFSCORRUPTED;
1375 ret = ext4_journal_get_write_access(handle, bh);
1379 memcpy(bh->b_data, buf, csize);
1380 set_buffer_uptodate(bh);
1381 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1388 inode_lock(ea_inode);
1389 i_size_write(ea_inode, wsize);
1390 ext4_update_i_disksize(ea_inode, wsize);
1391 inode_unlock(ea_inode);
1393 ret2 = ext4_mark_inode_dirty(handle, ea_inode);
1394 if (unlikely(ret2 && !ret))
1404 * Create an inode to store the value of a large EA.
1406 static struct inode *ext4_xattr_inode_create(handle_t *handle,
1407 struct inode *inode, u32 hash)
1409 struct inode *ea_inode = NULL;
1410 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
1413 if (inode->i_sb->s_root == NULL) {
1414 ext4_warning(inode->i_sb,
1415 "refuse to create EA inode when umounting");
1417 return ERR_PTR(-EINVAL);
1421 * Let the next inode be the goal, so we try and allocate the EA inode
1422 * in the same group, or nearby one.
1424 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
1425 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
1427 if (!IS_ERR(ea_inode)) {
1428 ea_inode->i_op = &ext4_file_inode_operations;
1429 ea_inode->i_fop = &ext4_file_operations;
1430 ext4_set_aops(ea_inode);
1431 ext4_xattr_inode_set_class(ea_inode);
1432 unlock_new_inode(ea_inode);
1433 ext4_xattr_inode_set_ref(ea_inode, 1);
1434 ext4_xattr_inode_set_hash(ea_inode, hash);
1435 err = ext4_mark_inode_dirty(handle, ea_inode);
1437 err = ext4_inode_attach_jinode(ea_inode);
1439 if (ext4_xattr_inode_dec_ref(handle, ea_inode))
1440 ext4_warning_inode(ea_inode,
1441 "cleanup dec ref error %d", err);
1443 return ERR_PTR(err);
1447 * Xattr inodes are shared therefore quota charging is performed
1448 * at a higher level.
1450 dquot_free_inode(ea_inode);
1451 dquot_drop(ea_inode);
1452 inode_lock(ea_inode);
1453 ea_inode->i_flags |= S_NOQUOTA;
1454 inode_unlock(ea_inode);
1460 static struct inode *
1461 ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1462 size_t value_len, u32 hash)
1464 struct inode *ea_inode;
1465 struct mb_cache_entry *ce;
1466 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1469 if (!ea_inode_cache)
1472 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1476 WARN_ON_ONCE(ext4_handle_valid(journal_current_handle()) &&
1477 !(current->flags & PF_MEMALLOC_NOFS));
1479 ea_data = kvmalloc(value_len, GFP_KERNEL);
1481 mb_cache_entry_put(ea_inode_cache, ce);
1486 ea_inode = ext4_iget(inode->i_sb, ce->e_value,
1487 EXT4_IGET_EA_INODE);
1488 if (IS_ERR(ea_inode))
1490 ext4_xattr_inode_set_class(ea_inode);
1491 if (i_size_read(ea_inode) == value_len &&
1492 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
1493 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1495 !memcmp(value, ea_data, value_len)) {
1496 mb_cache_entry_touch(ea_inode_cache, ce);
1497 mb_cache_entry_put(ea_inode_cache, ce);
1503 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1510 * Add value of the EA in an inode.
1512 static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1513 const void *value, size_t value_len,
1514 struct inode **ret_inode)
1516 struct inode *ea_inode;
1520 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1521 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1523 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1529 *ret_inode = ea_inode;
1533 /* Create an inode for the EA value */
1534 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
1535 if (IS_ERR(ea_inode))
1536 return PTR_ERR(ea_inode);
1538 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
1540 ext4_xattr_inode_dec_ref(handle, ea_inode);
1545 if (EA_INODE_CACHE(inode))
1546 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1547 ea_inode->i_ino, true /* reusable */);
1549 *ret_inode = ea_inode;
1554 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1555 * feature is enabled.
1557 #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1559 static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1560 struct ext4_xattr_search *s,
1561 handle_t *handle, struct inode *inode,
1564 struct ext4_xattr_entry *last, *next;
1565 struct ext4_xattr_entry *here = s->here;
1566 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
1567 int in_inode = i->in_inode;
1568 struct inode *old_ea_inode = NULL;
1569 struct inode *new_ea_inode = NULL;
1570 size_t old_size, new_size;
1573 /* Space used by old and new values. */
1574 old_size = (!s->not_found && !here->e_value_inum) ?
1575 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1576 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1579 * Optimization for the simple case when old and new values have the
1580 * same padded sizes. Not applicable if external inodes are involved.
1582 if (new_size && new_size == old_size) {
1583 size_t offs = le16_to_cpu(here->e_value_offs);
1584 void *val = s->base + offs;
1586 here->e_value_size = cpu_to_le32(i->value_len);
1587 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1588 memset(val, 0, new_size);
1590 memcpy(val, i->value, i->value_len);
1591 /* Clear padding bytes. */
1592 memset(val + i->value_len, 0, new_size - i->value_len);
1597 /* Compute min_offs and last. */
1599 for (; !IS_LAST_ENTRY(last); last = next) {
1600 next = EXT4_XATTR_NEXT(last);
1601 if ((void *)next >= s->end) {
1602 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
1603 ret = -EFSCORRUPTED;
1606 if (!last->e_value_inum && last->e_value_size) {
1607 size_t offs = le16_to_cpu(last->e_value_offs);
1608 if (offs < min_offs)
1613 /* Check whether we have enough space. */
1617 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1619 free += EXT4_XATTR_LEN(name_len) + old_size;
1621 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1627 * If storing the value in an external inode is an option,
1628 * reserve space for xattr entries/names in the external
1629 * attribute block so that a long value does not occupy the
1630 * whole space and prevent further entries being added.
1632 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1633 new_size && is_block &&
1634 (min_offs + old_size - new_size) <
1635 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1642 * Getting access to old and new ea inodes is subject to failures.
1643 * Finish that work before doing any modifications to the xattr data.
1645 if (!s->not_found && here->e_value_inum) {
1646 ret = ext4_xattr_inode_iget(inode,
1647 le32_to_cpu(here->e_value_inum),
1648 le32_to_cpu(here->e_hash),
1651 old_ea_inode = NULL;
1655 if (i->value && in_inode) {
1656 WARN_ON_ONCE(!i->value_len);
1658 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1662 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1666 new_ea_inode = NULL;
1667 ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
1673 /* We are ready to release ref count on the old_ea_inode. */
1674 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1676 /* Release newly required ref count on new_ea_inode. */
1680 err = ext4_xattr_inode_dec_ref(handle,
1683 ext4_warning_inode(new_ea_inode,
1684 "dec ref new_ea_inode err=%d",
1686 ext4_xattr_inode_free_quota(inode, new_ea_inode,
1692 ext4_xattr_inode_free_quota(inode, old_ea_inode,
1693 le32_to_cpu(here->e_value_size));
1696 /* No failures allowed past this point. */
1698 if (!s->not_found && here->e_value_size && !here->e_value_inum) {
1699 /* Remove the old value. */
1700 void *first_val = s->base + min_offs;
1701 size_t offs = le16_to_cpu(here->e_value_offs);
1702 void *val = s->base + offs;
1704 memmove(first_val + old_size, first_val, val - first_val);
1705 memset(first_val, 0, old_size);
1706 min_offs += old_size;
1708 /* Adjust all value offsets. */
1710 while (!IS_LAST_ENTRY(last)) {
1711 size_t o = le16_to_cpu(last->e_value_offs);
1713 if (!last->e_value_inum &&
1714 last->e_value_size && o < offs)
1715 last->e_value_offs = cpu_to_le16(o + old_size);
1716 last = EXT4_XATTR_NEXT(last);
1721 /* Remove old name. */
1722 size_t size = EXT4_XATTR_LEN(name_len);
1724 last = ENTRY((void *)last - size);
1725 memmove(here, (void *)here + size,
1726 (void *)last - (void *)here + sizeof(__u32));
1727 memset(last, 0, size);
1730 * Update i_inline_off - moved ibody region might contain
1731 * system.data attribute. Handling a failure here won't
1732 * cause other complications for setting an xattr.
1734 if (!is_block && ext4_has_inline_data(inode)) {
1735 ret = ext4_find_inline_data_nolock(inode);
1737 ext4_warning_inode(inode,
1738 "unable to update i_inline_off");
1742 } else if (s->not_found) {
1743 /* Insert new name. */
1744 size_t size = EXT4_XATTR_LEN(name_len);
1745 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1747 memmove((void *)here + size, here, rest);
1748 memset(here, 0, size);
1749 here->e_name_index = i->name_index;
1750 here->e_name_len = name_len;
1751 memcpy(here->e_name, i->name, name_len);
1753 /* This is an update, reset value info. */
1754 here->e_value_inum = 0;
1755 here->e_value_offs = 0;
1756 here->e_value_size = 0;
1760 /* Insert new value. */
1762 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
1763 } else if (i->value_len) {
1764 void *val = s->base + min_offs - new_size;
1766 here->e_value_offs = cpu_to_le16(min_offs - new_size);
1767 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1768 memset(val, 0, new_size);
1770 memcpy(val, i->value, i->value_len);
1771 /* Clear padding bytes. */
1772 memset(val + i->value_len, 0,
1773 new_size - i->value_len);
1776 here->e_value_size = cpu_to_le32(i->value_len);
1783 /* Entry hash calculation. */
1788 * Feed crc32c hash instead of the raw value for entry
1789 * hash calculation. This is to avoid walking
1790 * potentially long value buffer again.
1792 crc32c_hash = cpu_to_le32(
1793 ext4_xattr_inode_get_hash(new_ea_inode));
1794 hash = ext4_xattr_hash_entry(here->e_name,
1797 } else if (is_block) {
1798 __le32 *value = s->base + le16_to_cpu(
1799 here->e_value_offs);
1801 hash = ext4_xattr_hash_entry(here->e_name,
1802 here->e_name_len, value,
1805 here->e_hash = hash;
1809 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1818 struct ext4_xattr_block_find {
1819 struct ext4_xattr_search s;
1820 struct buffer_head *bh;
1824 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1825 struct ext4_xattr_block_find *bs)
1827 struct super_block *sb = inode->i_sb;
1830 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1831 i->name_index, i->name, i->value, (long)i->value_len);
1833 if (EXT4_I(inode)->i_file_acl) {
1834 /* The inode already has an extended attribute block. */
1835 bs->bh = ext4_sb_bread(sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
1836 if (IS_ERR(bs->bh)) {
1837 error = PTR_ERR(bs->bh);
1841 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1842 atomic_read(&(bs->bh->b_count)),
1843 le32_to_cpu(BHDR(bs->bh)->h_refcount));
1844 error = ext4_xattr_check_block(inode, bs->bh);
1847 /* Find the named attribute. */
1848 bs->s.base = BHDR(bs->bh);
1849 bs->s.first = BFIRST(bs->bh);
1850 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1851 bs->s.here = bs->s.first;
1852 error = xattr_find_entry(inode, &bs->s.here, bs->s.end,
1853 i->name_index, i->name, 1);
1854 if (error && error != -ENODATA)
1856 bs->s.not_found = error;
1862 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1863 struct ext4_xattr_info *i,
1864 struct ext4_xattr_block_find *bs)
1866 struct super_block *sb = inode->i_sb;
1867 struct buffer_head *new_bh = NULL;
1868 struct ext4_xattr_search s_copy = bs->s;
1869 struct ext4_xattr_search *s = &s_copy;
1870 struct mb_cache_entry *ce = NULL;
1872 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1873 struct inode *ea_inode = NULL, *tmp_inode;
1874 size_t old_ea_inode_quota = 0;
1875 unsigned int ea_ino;
1878 #define header(x) ((struct ext4_xattr_header *)(x))
1881 int offset = (char *)s->here - bs->bh->b_data;
1883 BUFFER_TRACE(bs->bh, "get_write_access");
1884 error = ext4_journal_get_write_access(handle, bs->bh);
1887 lock_buffer(bs->bh);
1889 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
1890 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1893 * This must happen under buffer lock for
1894 * ext4_xattr_block_set() to reliably detect modified
1897 if (ea_block_cache) {
1898 struct mb_cache_entry *oe;
1900 oe = mb_cache_entry_delete_or_get(ea_block_cache,
1901 hash, bs->bh->b_blocknr);
1904 * Xattr block is getting reused. Leave
1907 mb_cache_entry_put(ea_block_cache, oe);
1911 ea_bdebug(bs->bh, "modifying in-place");
1912 error = ext4_xattr_set_entry(i, s, handle, inode,
1913 true /* is_block */);
1914 ext4_xattr_block_csum_set(inode, bs->bh);
1915 unlock_buffer(bs->bh);
1916 if (error == -EFSCORRUPTED)
1919 error = ext4_handle_dirty_metadata(handle,
1927 unlock_buffer(bs->bh);
1928 ea_bdebug(bs->bh, "cloning");
1929 s->base = kmemdup(BHDR(bs->bh), bs->bh->b_size, GFP_NOFS);
1931 if (s->base == NULL)
1933 s->first = ENTRY(header(s->base)+1);
1934 header(s->base)->h_refcount = cpu_to_le32(1);
1935 s->here = ENTRY(s->base + offset);
1936 s->end = s->base + bs->bh->b_size;
1939 * If existing entry points to an xattr inode, we need
1940 * to prevent ext4_xattr_set_entry() from decrementing
1941 * ref count on it because the reference belongs to the
1942 * original block. In this case, make the entry look
1943 * like it has an empty value.
1945 if (!s->not_found && s->here->e_value_inum) {
1946 ea_ino = le32_to_cpu(s->here->e_value_inum);
1947 error = ext4_xattr_inode_iget(inode, ea_ino,
1948 le32_to_cpu(s->here->e_hash),
1953 if (!ext4_test_inode_state(tmp_inode,
1954 EXT4_STATE_LUSTRE_EA_INODE)) {
1956 * Defer quota free call for previous
1957 * inode until success is guaranteed.
1959 old_ea_inode_quota = le32_to_cpu(
1960 s->here->e_value_size);
1964 s->here->e_value_inum = 0;
1965 s->here->e_value_size = 0;
1968 /* Allocate a buffer where we construct the new block. */
1969 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
1970 /* assert(header == s->base) */
1972 if (s->base == NULL)
1974 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1975 header(s->base)->h_blocks = cpu_to_le32(1);
1976 header(s->base)->h_refcount = cpu_to_le32(1);
1977 s->first = ENTRY(header(s->base)+1);
1978 s->here = ENTRY(header(s->base)+1);
1979 s->end = s->base + sb->s_blocksize;
1982 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
1983 if (error == -EFSCORRUPTED)
1988 if (i->value && s->here->e_value_inum) {
1990 * A ref count on ea_inode has been taken as part of the call to
1991 * ext4_xattr_set_entry() above. We would like to drop this
1992 * extra ref but we have to wait until the xattr block is
1993 * initialized and has its own ref count on the ea_inode.
1995 ea_ino = le32_to_cpu(s->here->e_value_inum);
1996 error = ext4_xattr_inode_iget(inode, ea_ino,
1997 le32_to_cpu(s->here->e_hash),
2006 if (!IS_LAST_ENTRY(s->first)) {
2007 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
2010 /* We found an identical block in the cache. */
2011 if (new_bh == bs->bh)
2012 ea_bdebug(new_bh, "keeping");
2016 #ifdef EXT4_XATTR_DEBUG
2017 WARN_ON_ONCE(dquot_initialize_needed(inode));
2019 /* The old block is released after updating
2021 error = dquot_alloc_block(inode,
2022 EXT4_C2B(EXT4_SB(sb), 1));
2025 BUFFER_TRACE(new_bh, "get_write_access");
2026 error = ext4_journal_get_write_access(handle,
2030 lock_buffer(new_bh);
2032 * We have to be careful about races with
2033 * adding references to xattr block. Once we
2034 * hold buffer lock xattr block's state is
2035 * stable so we can check the additional
2038 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2039 if (ref > EXT4_XATTR_REFCOUNT_MAX) {
2041 * Undo everything and check mbcache
2044 unlock_buffer(new_bh);
2045 dquot_free_block(inode,
2046 EXT4_C2B(EXT4_SB(sb),
2049 mb_cache_entry_put(ea_block_cache, ce);
2054 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2055 if (ref == EXT4_XATTR_REFCOUNT_MAX)
2056 clear_bit(MBE_REUSABLE_B, &ce->e_flags);
2057 ea_bdebug(new_bh, "reusing; refcount now=%d",
2059 ext4_xattr_block_csum_set(inode, new_bh);
2060 unlock_buffer(new_bh);
2061 error = ext4_handle_dirty_metadata(handle,
2067 mb_cache_entry_touch(ea_block_cache, ce);
2068 mb_cache_entry_put(ea_block_cache, ce);
2070 } else if (bs->bh && s->base == bs->bh->b_data) {
2071 /* We were modifying this block in-place. */
2072 ea_bdebug(bs->bh, "keeping this block");
2073 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
2077 /* We need to allocate a new block */
2078 ext4_fsblk_t goal, block;
2080 #ifdef EXT4_XATTR_DEBUG
2081 WARN_ON_ONCE(dquot_initialize_needed(inode));
2083 goal = ext4_group_first_block_no(sb,
2084 EXT4_I(inode)->i_block_group);
2085 block = ext4_new_meta_blocks(handle, inode, goal, 0,
2090 ea_idebug(inode, "creating block %llu",
2091 (unsigned long long)block);
2093 new_bh = sb_getblk(sb, block);
2094 if (unlikely(!new_bh)) {
2097 ext4_free_blocks(handle, inode, NULL, block, 1,
2098 EXT4_FREE_BLOCKS_METADATA);
2101 error = ext4_xattr_inode_inc_ref_all(handle, inode,
2102 ENTRY(header(s->base)+1));
2106 /* Drop the extra ref on ea_inode. */
2107 error = ext4_xattr_inode_dec_ref(handle,
2110 ext4_warning_inode(ea_inode,
2117 lock_buffer(new_bh);
2118 error = ext4_journal_get_create_access(handle, new_bh);
2120 unlock_buffer(new_bh);
2124 memcpy(new_bh->b_data, s->base, new_bh->b_size);
2125 ext4_xattr_block_csum_set(inode, new_bh);
2126 set_buffer_uptodate(new_bh);
2127 unlock_buffer(new_bh);
2128 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
2129 error = ext4_handle_dirty_metadata(handle, inode,
2136 if (old_ea_inode_quota)
2137 ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
2139 /* Update the inode. */
2140 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
2142 /* Drop the previous xattr block. */
2143 if (bs->bh && bs->bh != new_bh) {
2144 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2146 ext4_xattr_release_block(handle, inode, bs->bh,
2148 0 /* extra_credits */);
2149 ext4_xattr_inode_array_free(ea_inode_array);
2157 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2159 ext4_warning_inode(ea_inode, "dec ref error=%d",
2162 /* If there was an error, revert the quota charge. */
2164 ext4_xattr_inode_free_quota(inode, ea_inode,
2165 i_size_read(ea_inode));
2169 mb_cache_entry_put(ea_block_cache, ce);
2171 if (!(bs->bh && s->base == bs->bh->b_data))
2177 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
2181 EXT4_ERROR_INODE(inode, "bad block %llu",
2182 EXT4_I(inode)->i_file_acl);
2188 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2189 struct ext4_xattr_ibody_find *is)
2191 struct ext4_xattr_ibody_header *header;
2192 struct ext4_inode *raw_inode;
2195 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2198 raw_inode = ext4_raw_inode(&is->iloc);
2199 header = IHDR(inode, raw_inode);
2200 is->s.base = is->s.first = IFIRST(header);
2201 is->s.here = is->s.first;
2202 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2203 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2204 error = xattr_check_inode(inode, header, is->s.end);
2207 /* Find the named attribute. */
2208 error = xattr_find_entry(inode, &is->s.here, is->s.end,
2209 i->name_index, i->name, 0);
2210 if (error && error != -ENODATA)
2212 is->s.not_found = error;
2217 int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
2218 struct ext4_xattr_info *i,
2219 struct ext4_xattr_ibody_find *is)
2221 struct ext4_xattr_ibody_header *header;
2222 struct ext4_xattr_search *s = &is->s;
2225 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2228 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
2231 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2232 if (!IS_LAST_ENTRY(s->first)) {
2233 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2234 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2236 header->h_magic = cpu_to_le32(0);
2237 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2242 static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2243 struct ext4_xattr_info *i)
2247 /* When e_value_inum is set the value is stored externally. */
2248 if (s->here->e_value_inum)
2250 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2252 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2253 return !memcmp(value, i->value, i->value_len);
2256 static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2258 struct buffer_head *bh;
2261 if (!EXT4_I(inode)->i_file_acl)
2263 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2266 error = ext4_xattr_check_block(inode, bh);
2269 return ERR_PTR(error);
2275 * ext4_xattr_set_handle()
2277 * Create, replace or remove an extended attribute for this inode. Value
2278 * is NULL to remove an existing extended attribute, and non-NULL to
2279 * either replace an existing extended attribute, or create a new extended
2280 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2281 * specify that an extended attribute must exist and must not exist
2282 * previous to the call, respectively.
2284 * Returns 0, or a negative error number on failure.
2287 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
2288 const char *name, const void *value, size_t value_len,
2291 struct ext4_xattr_info i = {
2292 .name_index = name_index,
2295 .value_len = value_len,
2298 struct ext4_xattr_ibody_find is = {
2299 .s = { .not_found = -ENODATA, },
2301 struct ext4_xattr_block_find bs = {
2302 .s = { .not_found = -ENODATA, },
2309 if (strlen(name) > 255)
2312 ext4_write_lock_xattr(inode, &no_expand);
2314 /* Check journal credits under write lock. */
2315 if (ext4_handle_valid(handle)) {
2316 struct buffer_head *bh;
2319 bh = ext4_xattr_get_block(inode);
2321 error = PTR_ERR(bh);
2325 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2327 flags & XATTR_CREATE);
2330 if (jbd2_handle_buffer_credits(handle) < credits) {
2334 WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
2337 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
2341 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
2342 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2343 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
2344 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
2347 error = ext4_xattr_ibody_find(inode, &i, &is);
2351 error = ext4_xattr_block_find(inode, &i, &bs);
2354 if (is.s.not_found && bs.s.not_found) {
2356 if (flags & XATTR_REPLACE)
2363 if (flags & XATTR_CREATE)
2368 if (!is.s.not_found)
2369 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2370 else if (!bs.s.not_found)
2371 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2374 /* Xattr value did not change? Save us some work and bail out */
2375 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2377 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2380 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2381 (EXT4_XATTR_SIZE(i.value_len) >
2382 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2385 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2386 if (!error && !bs.s.not_found) {
2388 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2389 } else if (error == -ENOSPC) {
2390 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2393 error = ext4_xattr_block_find(inode, &i, &bs);
2397 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2398 if (!error && !is.s.not_found) {
2400 error = ext4_xattr_ibody_set(handle, inode, &i,
2402 } else if (error == -ENOSPC) {
2404 * Xattr does not fit in the block, store at
2405 * external inode if possible.
2407 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2408 i.value_len && !i.in_inode) {
2416 ext4_xattr_update_super_block(handle, inode->i_sb);
2417 inode->i_ctime = current_time(inode);
2420 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
2422 * The bh is consumed by ext4_mark_iloc_dirty, even with
2427 ext4_handle_sync(handle);
2429 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR);
2434 ext4_write_unlock_xattr(inode, &no_expand);
2438 int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2439 bool is_create, int *credits)
2441 struct buffer_head *bh;
2446 if (!EXT4_SB(inode->i_sb)->s_journal)
2449 down_read(&EXT4_I(inode)->xattr_sem);
2451 bh = ext4_xattr_get_block(inode);
2455 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2456 value_len, is_create);
2461 up_read(&EXT4_I(inode)->xattr_sem);
2468 * Like ext4_xattr_set_handle, but start from an inode. This extended
2469 * attribute modification is a filesystem transaction by itself.
2471 * Returns 0, or a negative error number on failure.
2474 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
2475 const void *value, size_t value_len, int flags)
2478 struct super_block *sb = inode->i_sb;
2479 int error, retries = 0;
2482 error = dquot_initialize(inode);
2487 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2492 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
2493 if (IS_ERR(handle)) {
2494 error = PTR_ERR(handle);
2498 error = ext4_xattr_set_handle(handle, inode, name_index, name,
2499 value, value_len, flags);
2500 error2 = ext4_journal_stop(handle);
2501 if (error == -ENOSPC &&
2502 ext4_should_retry_alloc(sb, &retries))
2507 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR);
2513 * Shift the EA entries in the inode to create space for the increased
2516 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2517 int value_offs_shift, void *to,
2518 void *from, size_t n)
2520 struct ext4_xattr_entry *last = entry;
2523 /* We always shift xattr headers further thus offsets get lower */
2524 BUG_ON(value_offs_shift > 0);
2526 /* Adjust the value offsets of the entries */
2527 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2528 if (!last->e_value_inum && last->e_value_size) {
2529 new_offs = le16_to_cpu(last->e_value_offs) +
2531 last->e_value_offs = cpu_to_le16(new_offs);
2534 /* Shift the entries by n bytes */
2535 memmove(to, from, n);
2539 * Move xattr pointed to by 'entry' from inode into external xattr block
2541 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2542 struct ext4_inode *raw_inode,
2543 struct ext4_xattr_entry *entry)
2545 struct ext4_xattr_ibody_find *is = NULL;
2546 struct ext4_xattr_block_find *bs = NULL;
2547 char *buffer = NULL, *b_entry_name = NULL;
2548 size_t value_size = le32_to_cpu(entry->e_value_size);
2549 struct ext4_xattr_info i = {
2552 .name_index = entry->e_name_index,
2553 .in_inode = !!entry->e_value_inum,
2555 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2556 int needs_kvfree = 0;
2559 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2560 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2561 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2562 if (!is || !bs || !b_entry_name) {
2567 is->s.not_found = -ENODATA;
2568 bs->s.not_found = -ENODATA;
2572 /* Save the entry name and the entry value */
2573 if (entry->e_value_inum) {
2574 buffer = kvmalloc(value_size, GFP_NOFS);
2580 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
2584 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2585 buffer = (void *)IFIRST(header) + value_offs;
2588 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2589 b_entry_name[entry->e_name_len] = '\0';
2590 i.name = b_entry_name;
2592 error = ext4_get_inode_loc(inode, &is->iloc);
2596 error = ext4_xattr_ibody_find(inode, &i, is);
2601 i.value_len = value_size;
2602 error = ext4_xattr_block_find(inode, &i, bs);
2606 /* Move ea entry from the inode into the block */
2607 error = ext4_xattr_block_set(handle, inode, &i, bs);
2611 /* Remove the chosen entry from the inode */
2614 error = ext4_xattr_ibody_set(handle, inode, &i, is);
2617 kfree(b_entry_name);
2618 if (needs_kvfree && buffer)
2621 brelse(is->iloc.bh);
2630 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2631 struct ext4_inode *raw_inode,
2632 int isize_diff, size_t ifree,
2633 size_t bfree, int *total_ino)
2635 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2636 struct ext4_xattr_entry *small_entry;
2637 struct ext4_xattr_entry *entry;
2638 struct ext4_xattr_entry *last;
2639 unsigned int entry_size; /* EA entry size */
2640 unsigned int total_size; /* EA entry size + value size */
2641 unsigned int min_total_size;
2644 while (isize_diff > ifree) {
2647 min_total_size = ~0U;
2648 last = IFIRST(header);
2649 /* Find the entry best suited to be pushed into EA block */
2650 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2651 /* never move system.data out of the inode */
2652 if ((last->e_name_len == 4) &&
2653 (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
2654 !memcmp(last->e_name, "data", 4))
2656 total_size = EXT4_XATTR_LEN(last->e_name_len);
2657 if (!last->e_value_inum)
2658 total_size += EXT4_XATTR_SIZE(
2659 le32_to_cpu(last->e_value_size));
2660 if (total_size <= bfree &&
2661 total_size < min_total_size) {
2662 if (total_size + ifree < isize_diff) {
2666 min_total_size = total_size;
2671 if (entry == NULL) {
2672 if (small_entry == NULL)
2674 entry = small_entry;
2677 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
2678 total_size = entry_size;
2679 if (!entry->e_value_inum)
2680 total_size += EXT4_XATTR_SIZE(
2681 le32_to_cpu(entry->e_value_size));
2682 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2687 *total_ino -= entry_size;
2688 ifree += total_size;
2689 bfree -= total_size;
2696 * Expand an inode by new_extra_isize bytes when EAs are present.
2697 * Returns 0 on success or negative error number on failure.
2699 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2700 struct ext4_inode *raw_inode, handle_t *handle)
2702 struct ext4_xattr_ibody_header *header;
2703 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2704 static unsigned int mnt_count;
2706 size_t ifree, bfree;
2709 int error = 0, tried_min_extra_isize = 0;
2710 int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
2711 int isize_diff; /* How much do we need to grow i_extra_isize */
2714 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
2715 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
2718 header = IHDR(inode, raw_inode);
2721 * Check if enough free space is available in the inode to shift the
2722 * entries ahead by new_extra_isize.
2725 base = IFIRST(header);
2726 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2727 min_offs = end - base;
2728 total_ino = sizeof(struct ext4_xattr_ibody_header) + sizeof(u32);
2730 error = xattr_check_inode(inode, header, end);
2734 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
2735 if (ifree >= isize_diff)
2739 * Enough free space isn't available in the inode, check if
2740 * EA block can hold new_extra_isize bytes.
2742 if (EXT4_I(inode)->i_file_acl) {
2743 struct buffer_head *bh;
2745 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2747 error = PTR_ERR(bh);
2750 error = ext4_xattr_check_block(inode, bh);
2756 end = bh->b_data + bh->b_size;
2757 min_offs = end - base;
2758 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2761 if (bfree + ifree < isize_diff) {
2762 if (!tried_min_extra_isize && s_min_extra_isize) {
2763 tried_min_extra_isize++;
2764 new_extra_isize = s_min_extra_isize;
2771 bfree = inode->i_sb->s_blocksize;
2774 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2775 isize_diff, ifree, bfree,
2778 if (error == -ENOSPC && !tried_min_extra_isize &&
2779 s_min_extra_isize) {
2780 tried_min_extra_isize++;
2781 new_extra_isize = s_min_extra_isize;
2787 /* Adjust the offsets and shift the remaining entries ahead */
2788 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
2789 - new_extra_isize, (void *)raw_inode +
2790 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
2791 (void *)header, total_ino);
2792 EXT4_I(inode)->i_extra_isize = new_extra_isize;
2794 if (ext4_has_inline_data(inode))
2795 error = ext4_find_inline_data_nolock(inode);
2798 if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
2799 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2801 mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
2806 #define EIA_INCR 16 /* must be 2^n */
2807 #define EIA_MASK (EIA_INCR - 1)
2809 /* Add the large xattr @inode into @ea_inode_array for deferred iput().
2810 * If @ea_inode_array is new or full it will be grown and the old
2811 * contents copied over.
2814 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2815 struct inode *inode)
2817 if (*ea_inode_array == NULL) {
2819 * Start with 15 inodes, so it fits into a power-of-two size.
2820 * If *ea_inode_array is NULL, this is essentially offsetof()
2823 kmalloc(offsetof(struct ext4_xattr_inode_array,
2826 if (*ea_inode_array == NULL)
2828 (*ea_inode_array)->count = 0;
2829 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
2830 /* expand the array once all 15 + n * 16 slots are full */
2831 struct ext4_xattr_inode_array *new_array = NULL;
2832 int count = (*ea_inode_array)->count;
2834 /* if new_array is NULL, this is essentially offsetof() */
2835 new_array = kmalloc(
2836 offsetof(struct ext4_xattr_inode_array,
2837 inodes[count + EIA_INCR]),
2839 if (new_array == NULL)
2841 memcpy(new_array, *ea_inode_array,
2842 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2843 kfree(*ea_inode_array);
2844 *ea_inode_array = new_array;
2846 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
2851 * ext4_xattr_delete_inode()
2853 * Free extended attribute resources associated with this inode. Traverse
2854 * all entries and decrement reference on any xattr inodes associated with this
2855 * inode. This is called immediately before an inode is freed. We have exclusive
2856 * access to the inode. If an orphan inode is deleted it will also release its
2857 * references on xattr block and xattr inodes.
2859 int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2860 struct ext4_xattr_inode_array **ea_inode_array,
2863 struct buffer_head *bh = NULL;
2864 struct ext4_xattr_ibody_header *header;
2865 struct ext4_iloc iloc = { .bh = NULL };
2866 struct ext4_xattr_entry *entry;
2867 struct inode *ea_inode;
2870 error = ext4_journal_ensure_credits(handle, extra_credits,
2871 ext4_free_metadata_revoke_credits(inode->i_sb, 1));
2873 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2877 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2878 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2880 error = ext4_get_inode_loc(inode, &iloc);
2882 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
2886 error = ext4_journal_get_write_access(handle, iloc.bh);
2888 EXT4_ERROR_INODE(inode, "write access (error %d)",
2893 header = IHDR(inode, ext4_raw_inode(&iloc));
2894 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2895 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2897 false /* block_csum */,
2900 false /* skip_quota */);
2903 if (EXT4_I(inode)->i_file_acl) {
2904 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2906 error = PTR_ERR(bh);
2907 if (error == -EIO) {
2908 EXT4_ERROR_INODE_ERR(inode, EIO,
2909 "block %llu read error",
2910 EXT4_I(inode)->i_file_acl);
2915 error = ext4_xattr_check_block(inode, bh);
2919 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2920 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2921 entry = EXT4_XATTR_NEXT(entry)) {
2922 if (!entry->e_value_inum)
2924 error = ext4_xattr_inode_iget(inode,
2925 le32_to_cpu(entry->e_value_inum),
2926 le32_to_cpu(entry->e_hash),
2930 ext4_xattr_inode_free_quota(inode, ea_inode,
2931 le32_to_cpu(entry->e_value_size));
2937 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2940 * Update i_file_acl value in the same transaction that releases
2943 EXT4_I(inode)->i_file_acl = 0;
2944 error = ext4_mark_inode_dirty(handle, inode);
2946 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2950 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR);
2959 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
2963 if (ea_inode_array == NULL)
2966 for (idx = 0; idx < ea_inode_array->count; ++idx)
2967 iput(ea_inode_array->inodes[idx]);
2968 kfree(ea_inode_array);
2972 * ext4_xattr_block_cache_insert()
2974 * Create a new entry in the extended attribute block cache, and insert
2975 * it unless such an entry is already in the cache.
2977 * Returns 0, or a negative error number on failure.
2980 ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
2981 struct buffer_head *bh)
2983 struct ext4_xattr_header *header = BHDR(bh);
2984 __u32 hash = le32_to_cpu(header->h_hash);
2985 int reusable = le32_to_cpu(header->h_refcount) <
2986 EXT4_XATTR_REFCOUNT_MAX;
2989 if (!ea_block_cache)
2991 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
2992 bh->b_blocknr, reusable);
2994 if (error == -EBUSY)
2995 ea_bdebug(bh, "already in cache");
2997 ea_bdebug(bh, "inserting [%x]", (int)hash);
3003 * Compare two extended attribute blocks for equality.
3005 * Returns 0 if the blocks are equal, 1 if they differ, and
3006 * a negative error number on errors.
3009 ext4_xattr_cmp(struct ext4_xattr_header *header1,
3010 struct ext4_xattr_header *header2)
3012 struct ext4_xattr_entry *entry1, *entry2;
3014 entry1 = ENTRY(header1+1);
3015 entry2 = ENTRY(header2+1);
3016 while (!IS_LAST_ENTRY(entry1)) {
3017 if (IS_LAST_ENTRY(entry2))
3019 if (entry1->e_hash != entry2->e_hash ||
3020 entry1->e_name_index != entry2->e_name_index ||
3021 entry1->e_name_len != entry2->e_name_len ||
3022 entry1->e_value_size != entry2->e_value_size ||
3023 entry1->e_value_inum != entry2->e_value_inum ||
3024 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
3026 if (!entry1->e_value_inum &&
3027 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
3028 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
3029 le32_to_cpu(entry1->e_value_size)))
3032 entry1 = EXT4_XATTR_NEXT(entry1);
3033 entry2 = EXT4_XATTR_NEXT(entry2);
3035 if (!IS_LAST_ENTRY(entry2))
3041 * ext4_xattr_block_cache_find()
3043 * Find an identical extended attribute block.
3045 * Returns a pointer to the block found, or NULL if such a block was
3046 * not found or an error occurred.
3048 static struct buffer_head *
3049 ext4_xattr_block_cache_find(struct inode *inode,
3050 struct ext4_xattr_header *header,
3051 struct mb_cache_entry **pce)
3053 __u32 hash = le32_to_cpu(header->h_hash);
3054 struct mb_cache_entry *ce;
3055 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
3057 if (!ea_block_cache)
3059 if (!header->h_hash)
3060 return NULL; /* never share */
3061 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
3062 ce = mb_cache_entry_find_first(ea_block_cache, hash);
3064 struct buffer_head *bh;
3066 bh = ext4_sb_bread(inode->i_sb, ce->e_value, REQ_PRIO);
3068 if (PTR_ERR(bh) == -ENOMEM)
3071 EXT4_ERROR_INODE(inode, "block %lu read error",
3072 (unsigned long)ce->e_value);
3073 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
3078 ce = mb_cache_entry_find_next(ea_block_cache, ce);
3083 #define NAME_HASH_SHIFT 5
3084 #define VALUE_HASH_SHIFT 16
3087 * ext4_xattr_hash_entry()
3089 * Compute the hash of an extended attribute.
3091 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3096 while (name_len--) {
3097 hash = (hash << NAME_HASH_SHIFT) ^
3098 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3101 while (value_count--) {
3102 hash = (hash << VALUE_HASH_SHIFT) ^
3103 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3104 le32_to_cpu(*value++);
3106 return cpu_to_le32(hash);
3109 #undef NAME_HASH_SHIFT
3110 #undef VALUE_HASH_SHIFT
3112 #define BLOCK_HASH_SHIFT 16
3115 * ext4_xattr_rehash()
3117 * Re-compute the extended attribute hash value after an entry has changed.
3119 static void ext4_xattr_rehash(struct ext4_xattr_header *header)
3121 struct ext4_xattr_entry *here;
3124 here = ENTRY(header+1);
3125 while (!IS_LAST_ENTRY(here)) {
3126 if (!here->e_hash) {
3127 /* Block is not shared if an entry's hash value == 0 */
3131 hash = (hash << BLOCK_HASH_SHIFT) ^
3132 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3133 le32_to_cpu(here->e_hash);
3134 here = EXT4_XATTR_NEXT(here);
3136 header->h_hash = cpu_to_le32(hash);
3139 #undef BLOCK_HASH_SHIFT
3141 #define HASH_BUCKET_BITS 10
3144 ext4_xattr_create_cache(void)
3146 return mb_cache_create(HASH_BUCKET_BITS);
3149 void ext4_xattr_destroy_cache(struct mb_cache *cache)
3152 mb_cache_destroy(cache);