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 __le32 ext4_xattr_hash_entry_signed(char *name, size_t name_len, __le32 *value,
86 static void ext4_xattr_rehash(struct ext4_xattr_header *);
88 static const struct xattr_handler * const ext4_xattr_handler_map[] = {
89 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
90 #ifdef CONFIG_EXT4_FS_POSIX_ACL
91 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
92 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
94 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
95 #ifdef CONFIG_EXT4_FS_SECURITY
96 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
98 [EXT4_XATTR_INDEX_HURD] = &ext4_xattr_hurd_handler,
101 const struct xattr_handler *ext4_xattr_handlers[] = {
102 &ext4_xattr_user_handler,
103 &ext4_xattr_trusted_handler,
104 #ifdef CONFIG_EXT4_FS_POSIX_ACL
105 &posix_acl_access_xattr_handler,
106 &posix_acl_default_xattr_handler,
108 #ifdef CONFIG_EXT4_FS_SECURITY
109 &ext4_xattr_security_handler,
111 &ext4_xattr_hurd_handler,
115 #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
116 inode->i_sb->s_fs_info)->s_ea_block_cache)
118 #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
119 inode->i_sb->s_fs_info)->s_ea_inode_cache)
122 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
123 struct inode *inode);
125 #ifdef CONFIG_LOCKDEP
126 void ext4_xattr_inode_set_class(struct inode *ea_inode)
128 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
132 static __le32 ext4_xattr_block_csum(struct inode *inode,
134 struct ext4_xattr_header *hdr)
136 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
138 __le64 dsk_block_nr = cpu_to_le64(block_nr);
139 __u32 dummy_csum = 0;
140 int offset = offsetof(struct ext4_xattr_header, h_checksum);
142 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
143 sizeof(dsk_block_nr));
144 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
145 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
146 offset += sizeof(dummy_csum);
147 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
148 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
150 return cpu_to_le32(csum);
153 static int ext4_xattr_block_csum_verify(struct inode *inode,
154 struct buffer_head *bh)
156 struct ext4_xattr_header *hdr = BHDR(bh);
159 if (ext4_has_metadata_csum(inode->i_sb)) {
161 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
162 bh->b_blocknr, hdr));
168 static void ext4_xattr_block_csum_set(struct inode *inode,
169 struct buffer_head *bh)
171 if (ext4_has_metadata_csum(inode->i_sb))
172 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
173 bh->b_blocknr, BHDR(bh));
176 static inline const struct xattr_handler *
177 ext4_xattr_handler(int name_index)
179 const struct xattr_handler *handler = NULL;
181 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
182 handler = ext4_xattr_handler_map[name_index];
187 check_xattrs(struct inode *inode, struct buffer_head *bh,
188 struct ext4_xattr_entry *entry, void *end, void *value_start,
189 const char *function, unsigned int line)
191 struct ext4_xattr_entry *e = entry;
192 int err = -EFSCORRUPTED;
196 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
197 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
198 err_str = "invalid header";
201 if (buffer_verified(bh))
203 if (!ext4_xattr_block_csum_verify(inode, bh)) {
205 err_str = "invalid checksum";
209 struct ext4_xattr_ibody_header *header = value_start;
212 if (end - (void *)header < sizeof(*header) + sizeof(u32)) {
213 err_str = "in-inode xattr block too small";
216 if (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
217 err_str = "bad magic number in in-inode xattr";
222 /* Find the end of the names list */
223 while (!IS_LAST_ENTRY(e)) {
224 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
225 if ((void *)next >= end) {
226 err_str = "e_name out of bounds";
229 if (strnlen(e->e_name, e->e_name_len) != e->e_name_len) {
230 err_str = "bad e_name length";
236 /* Check the values */
237 while (!IS_LAST_ENTRY(entry)) {
238 u32 size = le32_to_cpu(entry->e_value_size);
239 unsigned long ea_ino = le32_to_cpu(entry->e_value_inum);
241 if (!ext4_has_feature_ea_inode(inode->i_sb) && ea_ino) {
242 err_str = "ea_inode specified without ea_inode feature enabled";
245 if (ea_ino && ((ea_ino == EXT4_ROOT_INO) ||
246 !ext4_valid_inum(inode->i_sb, ea_ino))) {
247 err_str = "invalid ea_ino";
250 if (size > EXT4_XATTR_SIZE_MAX) {
251 err_str = "e_value size too large";
255 if (size != 0 && entry->e_value_inum == 0) {
256 u16 offs = le16_to_cpu(entry->e_value_offs);
260 * The value cannot overlap the names, and the value
261 * with padding cannot extend beyond 'end'. Check both
262 * the padded and unpadded sizes, since the size may
263 * overflow to 0 when adding padding.
265 if (offs > end - value_start) {
266 err_str = "e_value out of bounds";
269 value = value_start + offs;
270 if (value < (void *)e + sizeof(u32) ||
271 size > end - value ||
272 EXT4_XATTR_SIZE(size) > end - value) {
273 err_str = "overlapping e_value ";
277 entry = EXT4_XATTR_NEXT(entry);
280 set_buffer_verified(bh);
285 __ext4_error_inode(inode, function, line, 0, -err,
286 "corrupted xattr block %llu: %s",
287 (unsigned long long) bh->b_blocknr,
290 __ext4_error_inode(inode, function, line, 0, -err,
291 "corrupted in-inode xattr: %s", err_str);
296 __ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh,
297 const char *function, unsigned int line)
299 return check_xattrs(inode, bh, BFIRST(bh), bh->b_data + bh->b_size,
300 bh->b_data, function, line);
303 #define ext4_xattr_check_block(inode, bh) \
304 __ext4_xattr_check_block((inode), (bh), __func__, __LINE__)
308 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
309 void *end, const char *function, unsigned int line)
311 return check_xattrs(inode, NULL, IFIRST(header), end, IFIRST(header),
315 #define xattr_check_inode(inode, header, end) \
316 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
319 xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry,
320 void *end, int name_index, const char *name, int sorted)
322 struct ext4_xattr_entry *entry, *next;
328 name_len = strlen(name);
329 for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) {
330 next = EXT4_XATTR_NEXT(entry);
331 if ((void *) next >= end) {
332 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
333 return -EFSCORRUPTED;
335 cmp = name_index - entry->e_name_index;
337 cmp = name_len - entry->e_name_len;
339 cmp = memcmp(name, entry->e_name, name_len);
340 if (cmp <= 0 && (sorted || cmp == 0))
344 return cmp ? -ENODATA : 0;
348 ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
350 return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
353 static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
355 return ((u64)ea_inode->i_ctime.tv_sec << 32) |
356 (u32) inode_peek_iversion_raw(ea_inode);
359 static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
361 ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
362 inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
365 static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
367 return (u32)ea_inode->i_atime.tv_sec;
370 static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
372 ea_inode->i_atime.tv_sec = hash;
376 * Read the EA value from an inode.
378 static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
380 int blocksize = 1 << ea_inode->i_blkbits;
381 int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
382 int tail_size = (size % blocksize) ?: blocksize;
383 struct buffer_head *bhs_inline[8];
384 struct buffer_head **bhs = bhs_inline;
387 if (bh_count > ARRAY_SIZE(bhs_inline)) {
388 bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
393 ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
394 true /* wait */, bhs);
398 for (i = 0; i < bh_count; i++) {
399 /* There shouldn't be any holes in ea_inode. */
404 memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
405 i < bh_count - 1 ? blocksize : tail_size);
409 for (i = 0; i < bh_count; i++)
412 if (bhs != bhs_inline)
417 #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
419 static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
420 u32 ea_inode_hash, struct inode **ea_inode)
426 * We have to check for this corruption early as otherwise
427 * iget_locked() could wait indefinitely for the state of our
430 if (parent->i_ino == ea_ino) {
431 ext4_error(parent->i_sb,
432 "Parent and EA inode have the same ino %lu", ea_ino);
433 return -EFSCORRUPTED;
436 inode = ext4_iget(parent->i_sb, ea_ino, EXT4_IGET_NORMAL);
438 err = PTR_ERR(inode);
439 ext4_error(parent->i_sb,
440 "error while reading EA inode %lu err=%d", ea_ino,
445 if (is_bad_inode(inode)) {
446 ext4_error(parent->i_sb,
447 "error while reading EA inode %lu is_bad_inode",
453 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
454 ext4_error(parent->i_sb,
455 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
461 ext4_xattr_inode_set_class(inode);
464 * Check whether this is an old Lustre-style xattr inode. Lustre
465 * implementation does not have hash validation, rather it has a
466 * backpointer from ea_inode to the parent inode.
468 if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
469 EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
470 inode->i_generation == parent->i_generation) {
471 ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
472 ext4_xattr_inode_set_ref(inode, 1);
475 inode->i_flags |= S_NOQUOTA;
486 /* Remove entry from mbcache when EA inode is getting evicted */
487 void ext4_evict_ea_inode(struct inode *inode)
489 struct mb_cache_entry *oe;
491 if (!EA_INODE_CACHE(inode))
493 /* Wait for entry to get unused so that we can remove it */
494 while ((oe = mb_cache_entry_delete_or_get(EA_INODE_CACHE(inode),
495 ext4_xattr_inode_get_hash(inode), inode->i_ino))) {
496 mb_cache_entry_wait_unused(oe);
497 mb_cache_entry_put(EA_INODE_CACHE(inode), oe);
502 ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
503 struct ext4_xattr_entry *entry, void *buffer,
508 /* Verify stored hash matches calculated hash. */
509 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
510 if (hash != ext4_xattr_inode_get_hash(ea_inode))
511 return -EFSCORRUPTED;
514 __le32 e_hash, tmp_data;
516 /* Verify entry hash. */
517 tmp_data = cpu_to_le32(hash);
518 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
521 if (e_hash == entry->e_hash)
525 * Not good. Maybe the entry hash was calculated
526 * using the buggy signed char version?
528 e_hash = ext4_xattr_hash_entry_signed(entry->e_name, entry->e_name_len,
530 /* Still no match - bad */
531 if (e_hash != entry->e_hash)
532 return -EFSCORRUPTED;
534 /* Let people know about old hash */
535 pr_warn_once("ext4: filesystem with signed xattr name hash");
541 * Read xattr value from the EA inode.
544 ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
545 void *buffer, size_t size)
547 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
548 struct inode *ea_inode;
551 err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
552 le32_to_cpu(entry->e_hash), &ea_inode);
558 if (i_size_read(ea_inode) != size) {
559 ext4_warning_inode(ea_inode,
560 "ea_inode file size=%llu entry size=%zu",
561 i_size_read(ea_inode), size);
566 err = ext4_xattr_inode_read(ea_inode, buffer, size);
570 if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
571 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
574 ext4_warning_inode(ea_inode,
575 "EA inode hash validation failed");
580 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
581 ext4_xattr_inode_get_hash(ea_inode),
582 ea_inode->i_ino, true /* reusable */);
590 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
591 void *buffer, size_t buffer_size)
593 struct buffer_head *bh = NULL;
594 struct ext4_xattr_entry *entry;
598 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
600 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
601 name_index, name, buffer, (long)buffer_size);
603 if (!EXT4_I(inode)->i_file_acl)
605 ea_idebug(inode, "reading block %llu",
606 (unsigned long long)EXT4_I(inode)->i_file_acl);
607 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
610 ea_bdebug(bh, "b_count=%d, refcount=%d",
611 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
612 error = ext4_xattr_check_block(inode, bh);
615 ext4_xattr_block_cache_insert(ea_block_cache, bh);
617 end = bh->b_data + bh->b_size;
618 error = xattr_find_entry(inode, &entry, end, name_index, name, 1);
621 size = le32_to_cpu(entry->e_value_size);
623 if (unlikely(size > EXT4_XATTR_SIZE_MAX))
626 if (size > buffer_size)
628 if (entry->e_value_inum) {
629 error = ext4_xattr_inode_get(inode, entry, buffer,
634 u16 offset = le16_to_cpu(entry->e_value_offs);
635 void *p = bh->b_data + offset;
637 if (unlikely(p + size > end))
639 memcpy(buffer, p, size);
650 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
651 void *buffer, size_t buffer_size)
653 struct ext4_xattr_ibody_header *header;
654 struct ext4_xattr_entry *entry;
655 struct ext4_inode *raw_inode;
656 struct ext4_iloc iloc;
661 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
663 error = ext4_get_inode_loc(inode, &iloc);
666 raw_inode = ext4_raw_inode(&iloc);
667 header = IHDR(inode, raw_inode);
668 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
669 error = xattr_check_inode(inode, header, end);
672 entry = IFIRST(header);
673 error = xattr_find_entry(inode, &entry, end, name_index, name, 0);
676 size = le32_to_cpu(entry->e_value_size);
678 if (unlikely(size > EXT4_XATTR_SIZE_MAX))
681 if (size > buffer_size)
683 if (entry->e_value_inum) {
684 error = ext4_xattr_inode_get(inode, entry, buffer,
689 u16 offset = le16_to_cpu(entry->e_value_offs);
690 void *p = (void *)IFIRST(header) + offset;
692 if (unlikely(p + size > end))
694 memcpy(buffer, p, size);
707 * Copy an extended attribute into the buffer
708 * provided, or compute the buffer size required.
709 * Buffer is NULL to compute the size of the buffer required.
711 * Returns a negative error number on failure, or the number of bytes
712 * used / required on success.
715 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
716 void *buffer, size_t buffer_size)
720 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
723 if (strlen(name) > 255)
726 down_read(&EXT4_I(inode)->xattr_sem);
727 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
729 if (error == -ENODATA)
730 error = ext4_xattr_block_get(inode, name_index, name, buffer,
732 up_read(&EXT4_I(inode)->xattr_sem);
737 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
738 char *buffer, size_t buffer_size)
740 size_t rest = buffer_size;
742 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
743 const struct xattr_handler *handler =
744 ext4_xattr_handler(entry->e_name_index);
746 if (handler && (!handler->list || handler->list(dentry))) {
747 const char *prefix = handler->prefix ?: handler->name;
748 size_t prefix_len = strlen(prefix);
749 size_t size = prefix_len + entry->e_name_len + 1;
754 memcpy(buffer, prefix, prefix_len);
755 buffer += prefix_len;
756 memcpy(buffer, entry->e_name, entry->e_name_len);
757 buffer += entry->e_name_len;
763 return buffer_size - rest; /* total size */
767 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
769 struct inode *inode = d_inode(dentry);
770 struct buffer_head *bh = NULL;
773 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
774 buffer, (long)buffer_size);
776 if (!EXT4_I(inode)->i_file_acl)
778 ea_idebug(inode, "reading block %llu",
779 (unsigned long long)EXT4_I(inode)->i_file_acl);
780 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
783 ea_bdebug(bh, "b_count=%d, refcount=%d",
784 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
785 error = ext4_xattr_check_block(inode, bh);
788 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
789 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer,
797 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
799 struct inode *inode = d_inode(dentry);
800 struct ext4_xattr_ibody_header *header;
801 struct ext4_inode *raw_inode;
802 struct ext4_iloc iloc;
806 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
808 error = ext4_get_inode_loc(inode, &iloc);
811 raw_inode = ext4_raw_inode(&iloc);
812 header = IHDR(inode, raw_inode);
813 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
814 error = xattr_check_inode(inode, header, end);
817 error = ext4_xattr_list_entries(dentry, IFIRST(header),
818 buffer, buffer_size);
826 * Inode operation listxattr()
828 * d_inode(dentry)->i_rwsem: don't care
830 * Copy a list of attribute names into the buffer
831 * provided, or compute the buffer size required.
832 * Buffer is NULL to compute the size of the buffer required.
834 * Returns a negative error number on failure, or the number of bytes
835 * used / required on success.
838 ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
842 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
843 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
850 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
855 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
860 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
863 static void ext4_xattr_update_super_block(handle_t *handle,
864 struct super_block *sb)
866 if (ext4_has_feature_xattr(sb))
869 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
870 if (ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh,
871 EXT4_JTR_NONE) == 0) {
872 lock_buffer(EXT4_SB(sb)->s_sbh);
873 ext4_set_feature_xattr(sb);
874 ext4_superblock_csum_set(sb);
875 unlock_buffer(EXT4_SB(sb)->s_sbh);
876 ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
880 int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
882 struct ext4_iloc iloc = { .bh = NULL };
883 struct buffer_head *bh = NULL;
884 struct ext4_inode *raw_inode;
885 struct ext4_xattr_ibody_header *header;
886 struct ext4_xattr_entry *entry;
887 qsize_t ea_inode_refs = 0;
891 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
893 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
894 ret = ext4_get_inode_loc(inode, &iloc);
897 raw_inode = ext4_raw_inode(&iloc);
898 header = IHDR(inode, raw_inode);
899 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
900 ret = xattr_check_inode(inode, header, end);
904 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
905 entry = EXT4_XATTR_NEXT(entry))
906 if (entry->e_value_inum)
910 if (EXT4_I(inode)->i_file_acl) {
911 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
918 ret = ext4_xattr_check_block(inode, bh);
922 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
923 entry = EXT4_XATTR_NEXT(entry))
924 if (entry->e_value_inum)
927 *usage = ea_inode_refs + 1;
935 static inline size_t round_up_cluster(struct inode *inode, size_t length)
937 struct super_block *sb = inode->i_sb;
938 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
940 size_t mask = ~(cluster_size - 1);
942 return (length + cluster_size - 1) & mask;
945 static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
949 err = dquot_alloc_inode(inode);
952 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
954 dquot_free_inode(inode);
958 static void ext4_xattr_inode_free_quota(struct inode *parent,
959 struct inode *ea_inode,
963 ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
965 dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
966 dquot_free_inode(parent);
969 int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
970 struct buffer_head *block_bh, size_t value_len,
977 * 1) Owner inode update
978 * 2) Ref count update on old xattr block
980 * 4) block bitmap update for new xattr block
981 * 5) group descriptor for new xattr block
982 * 6) block bitmap update for old xattr block
983 * 7) group descriptor for old block
985 * 6 & 7 can happen if we have two racing threads T_a and T_b
986 * which are each trying to set an xattr on inodes I_a and I_b
987 * which were both initially sharing an xattr block.
992 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
995 * In case of inline data, we may push out the data to a block,
996 * so we need to reserve credits for this eventuality
998 if (inode && ext4_has_inline_data(inode))
999 credits += ext4_writepage_trans_blocks(inode) + 1;
1001 /* We are done if ea_inode feature is not enabled. */
1002 if (!ext4_has_feature_ea_inode(sb))
1005 /* New ea_inode, inode map, block bitmap, group descriptor. */
1009 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1011 /* Indirection block or one level of extent tree. */
1014 /* Block bitmap and group descriptor updates for each block. */
1015 credits += blocks * 2;
1017 /* Blocks themselves. */
1021 /* Dereference ea_inode holding old xattr value.
1022 * Old ea_inode, inode map, block bitmap, group descriptor.
1026 /* Data blocks for old ea_inode. */
1027 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
1029 /* Indirection block or one level of extent tree for old
1034 /* Block bitmap and group descriptor updates for each block. */
1035 credits += blocks * 2;
1038 /* We may need to clone the existing xattr block in which case we need
1039 * to increment ref counts for existing ea_inodes referenced by it.
1042 struct ext4_xattr_entry *entry = BFIRST(block_bh);
1044 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
1045 if (entry->e_value_inum)
1046 /* Ref count update on ea_inode. */
1052 static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
1055 struct ext4_iloc iloc;
1059 inode_lock(ea_inode);
1061 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
1065 ref_count = ext4_xattr_inode_get_ref(ea_inode);
1066 ref_count += ref_change;
1067 ext4_xattr_inode_set_ref(ea_inode, ref_count);
1069 if (ref_change > 0) {
1070 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
1071 ea_inode->i_ino, ref_count);
1073 if (ref_count == 1) {
1074 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
1075 ea_inode->i_ino, ea_inode->i_nlink);
1077 set_nlink(ea_inode, 1);
1078 ext4_orphan_del(handle, ea_inode);
1081 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1082 ea_inode->i_ino, ref_count);
1084 if (ref_count == 0) {
1085 WARN_ONCE(ea_inode->i_nlink != 1,
1086 "EA inode %lu i_nlink=%u",
1087 ea_inode->i_ino, ea_inode->i_nlink);
1089 clear_nlink(ea_inode);
1090 ext4_orphan_add(handle, ea_inode);
1094 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1096 ext4_warning_inode(ea_inode,
1097 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1099 inode_unlock(ea_inode);
1103 static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1105 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1108 static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1110 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1113 static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1114 struct ext4_xattr_entry *first)
1116 struct inode *ea_inode;
1117 struct ext4_xattr_entry *entry;
1118 struct ext4_xattr_entry *failed_entry;
1119 unsigned int ea_ino;
1122 for (entry = first; !IS_LAST_ENTRY(entry);
1123 entry = EXT4_XATTR_NEXT(entry)) {
1124 if (!entry->e_value_inum)
1126 ea_ino = le32_to_cpu(entry->e_value_inum);
1127 err = ext4_xattr_inode_iget(parent, ea_ino,
1128 le32_to_cpu(entry->e_hash),
1132 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1134 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1144 failed_entry = entry;
1146 for (entry = first; entry != failed_entry;
1147 entry = EXT4_XATTR_NEXT(entry)) {
1148 if (!entry->e_value_inum)
1150 ea_ino = le32_to_cpu(entry->e_value_inum);
1151 err = ext4_xattr_inode_iget(parent, ea_ino,
1152 le32_to_cpu(entry->e_hash),
1155 ext4_warning(parent->i_sb,
1156 "cleanup ea_ino %u iget error %d", ea_ino,
1160 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1162 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1169 static int ext4_xattr_restart_fn(handle_t *handle, struct inode *inode,
1170 struct buffer_head *bh, bool block_csum, bool dirty)
1176 ext4_xattr_block_csum_set(inode, bh);
1177 error = ext4_handle_dirty_metadata(handle, NULL, bh);
1179 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
1188 ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1189 struct buffer_head *bh,
1190 struct ext4_xattr_entry *first, bool block_csum,
1191 struct ext4_xattr_inode_array **ea_inode_array,
1192 int extra_credits, bool skip_quota)
1194 struct inode *ea_inode;
1195 struct ext4_xattr_entry *entry;
1197 unsigned int ea_ino;
1201 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1202 credits = 2 + extra_credits;
1204 for (entry = first; !IS_LAST_ENTRY(entry);
1205 entry = EXT4_XATTR_NEXT(entry)) {
1206 if (!entry->e_value_inum)
1208 ea_ino = le32_to_cpu(entry->e_value_inum);
1209 err = ext4_xattr_inode_iget(parent, ea_ino,
1210 le32_to_cpu(entry->e_hash),
1215 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1217 ext4_warning_inode(ea_inode,
1218 "Expand inode array err=%d", err);
1223 err = ext4_journal_ensure_credits_fn(handle, credits, credits,
1224 ext4_free_metadata_revoke_credits(parent->i_sb, 1),
1225 ext4_xattr_restart_fn(handle, parent, bh, block_csum,
1228 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1233 err = ext4_journal_get_write_access(handle,
1234 parent->i_sb, bh, EXT4_JTR_NONE);
1236 ext4_warning_inode(ea_inode,
1237 "Re-get write access err=%d",
1243 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1245 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1251 ext4_xattr_inode_free_quota(parent, ea_inode,
1252 le32_to_cpu(entry->e_value_size));
1255 * Forget about ea_inode within the same transaction that
1256 * decrements the ref count. This avoids duplicate decrements in
1257 * case the rest of the work spills over to subsequent
1260 entry->e_value_inum = 0;
1261 entry->e_value_size = 0;
1268 * Note that we are deliberately skipping csum calculation for
1269 * the final update because we do not expect any journal
1270 * restarts until xattr block is freed.
1273 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1275 ext4_warning_inode(parent,
1276 "handle dirty metadata err=%d", err);
1281 * Release the xattr block BH: If the reference count is > 1, decrement it;
1282 * otherwise free the block.
1285 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
1286 struct buffer_head *bh,
1287 struct ext4_xattr_inode_array **ea_inode_array,
1290 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1294 BUFFER_TRACE(bh, "get_write_access");
1295 error = ext4_journal_get_write_access(handle, inode->i_sb, bh,
1302 hash = le32_to_cpu(BHDR(bh)->h_hash);
1303 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1305 ea_bdebug(bh, "refcount now=0; freeing");
1307 * This must happen under buffer lock for
1308 * ext4_xattr_block_set() to reliably detect freed block
1310 if (ea_block_cache) {
1311 struct mb_cache_entry *oe;
1313 oe = mb_cache_entry_delete_or_get(ea_block_cache, hash,
1317 mb_cache_entry_wait_unused(oe);
1318 mb_cache_entry_put(ea_block_cache, oe);
1325 if (ext4_has_feature_ea_inode(inode->i_sb))
1326 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1328 true /* block_csum */,
1331 true /* skip_quota */);
1332 ext4_free_blocks(handle, inode, bh, 0, 1,
1333 EXT4_FREE_BLOCKS_METADATA |
1334 EXT4_FREE_BLOCKS_FORGET);
1337 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1338 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1339 struct mb_cache_entry *ce;
1341 if (ea_block_cache) {
1342 ce = mb_cache_entry_get(ea_block_cache, hash,
1345 set_bit(MBE_REUSABLE_B, &ce->e_flags);
1346 mb_cache_entry_put(ea_block_cache, ce);
1351 ext4_xattr_block_csum_set(inode, bh);
1353 * Beware of this ugliness: Releasing of xattr block references
1354 * from different inodes can race and so we have to protect
1355 * from a race where someone else frees the block (and releases
1356 * its journal_head) before we are done dirtying the buffer. In
1357 * nojournal mode this race is harmless and we actually cannot
1358 * call ext4_handle_dirty_metadata() with locked buffer as
1359 * that function can call sync_dirty_buffer() so for that case
1360 * we handle the dirtying after unlocking the buffer.
1362 if (ext4_handle_valid(handle))
1363 error = ext4_handle_dirty_metadata(handle, inode, bh);
1365 if (!ext4_handle_valid(handle))
1366 error = ext4_handle_dirty_metadata(handle, inode, bh);
1368 ext4_handle_sync(handle);
1369 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
1370 ea_bdebug(bh, "refcount now=%d; releasing",
1371 le32_to_cpu(BHDR(bh)->h_refcount));
1374 ext4_std_error(inode->i_sb, error);
1379 * Find the available free space for EAs. This also returns the total number of
1380 * bytes used by EA entries.
1382 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1383 size_t *min_offs, void *base, int *total)
1385 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1386 if (!last->e_value_inum && last->e_value_size) {
1387 size_t offs = le16_to_cpu(last->e_value_offs);
1388 if (offs < *min_offs)
1392 *total += EXT4_XATTR_LEN(last->e_name_len);
1394 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1398 * Write the value of the EA in an inode.
1400 static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1401 const void *buf, int bufsize)
1403 struct buffer_head *bh = NULL;
1404 unsigned long block = 0;
1405 int blocksize = ea_inode->i_sb->s_blocksize;
1406 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
1407 int csize, wsize = 0;
1408 int ret = 0, ret2 = 0;
1412 while (ret >= 0 && ret < max_blocks) {
1413 struct ext4_map_blocks map;
1414 map.m_lblk = block += ret;
1415 map.m_len = max_blocks -= ret;
1417 ret = ext4_map_blocks(handle, ea_inode, &map,
1418 EXT4_GET_BLOCKS_CREATE);
1420 ext4_mark_inode_dirty(handle, ea_inode);
1421 if (ret == -ENOSPC &&
1422 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1434 while (wsize < bufsize) {
1436 csize = (bufsize - wsize) > blocksize ? blocksize :
1438 bh = ext4_getblk(handle, ea_inode, block, 0);
1443 EXT4_ERROR_INODE(ea_inode,
1444 "ext4_getblk() return bh = NULL");
1445 return -EFSCORRUPTED;
1447 ret = ext4_journal_get_write_access(handle, ea_inode->i_sb, bh,
1452 memcpy(bh->b_data, buf, csize);
1453 set_buffer_uptodate(bh);
1454 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1461 inode_lock(ea_inode);
1462 i_size_write(ea_inode, wsize);
1463 ext4_update_i_disksize(ea_inode, wsize);
1464 inode_unlock(ea_inode);
1466 ret2 = ext4_mark_inode_dirty(handle, ea_inode);
1467 if (unlikely(ret2 && !ret))
1477 * Create an inode to store the value of a large EA.
1479 static struct inode *ext4_xattr_inode_create(handle_t *handle,
1480 struct inode *inode, u32 hash)
1482 struct inode *ea_inode = NULL;
1483 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
1486 if (inode->i_sb->s_root == NULL) {
1487 ext4_warning(inode->i_sb,
1488 "refuse to create EA inode when umounting");
1490 return ERR_PTR(-EINVAL);
1494 * Let the next inode be the goal, so we try and allocate the EA inode
1495 * in the same group, or nearby one.
1497 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
1498 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
1500 if (!IS_ERR(ea_inode)) {
1501 ea_inode->i_op = &ext4_file_inode_operations;
1502 ea_inode->i_fop = &ext4_file_operations;
1503 ext4_set_aops(ea_inode);
1504 ext4_xattr_inode_set_class(ea_inode);
1505 unlock_new_inode(ea_inode);
1506 ext4_xattr_inode_set_ref(ea_inode, 1);
1507 ext4_xattr_inode_set_hash(ea_inode, hash);
1508 err = ext4_mark_inode_dirty(handle, ea_inode);
1510 err = ext4_inode_attach_jinode(ea_inode);
1512 if (ext4_xattr_inode_dec_ref(handle, ea_inode))
1513 ext4_warning_inode(ea_inode,
1514 "cleanup dec ref error %d", err);
1516 return ERR_PTR(err);
1520 * Xattr inodes are shared therefore quota charging is performed
1521 * at a higher level.
1523 dquot_free_inode(ea_inode);
1524 dquot_drop(ea_inode);
1525 inode_lock(ea_inode);
1526 ea_inode->i_flags |= S_NOQUOTA;
1527 inode_unlock(ea_inode);
1533 static struct inode *
1534 ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1535 size_t value_len, u32 hash)
1537 struct inode *ea_inode;
1538 struct mb_cache_entry *ce;
1539 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1542 if (!ea_inode_cache)
1545 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1549 WARN_ON_ONCE(ext4_handle_valid(journal_current_handle()) &&
1550 !(current->flags & PF_MEMALLOC_NOFS));
1552 ea_data = kvmalloc(value_len, GFP_KERNEL);
1554 mb_cache_entry_put(ea_inode_cache, ce);
1559 ea_inode = ext4_iget(inode->i_sb, ce->e_value,
1561 if (!IS_ERR(ea_inode) &&
1562 !is_bad_inode(ea_inode) &&
1563 (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
1564 i_size_read(ea_inode) == value_len &&
1565 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
1566 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1568 !memcmp(value, ea_data, value_len)) {
1569 mb_cache_entry_touch(ea_inode_cache, ce);
1570 mb_cache_entry_put(ea_inode_cache, ce);
1575 if (!IS_ERR(ea_inode))
1577 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1584 * Add value of the EA in an inode.
1586 static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1587 const void *value, size_t value_len,
1588 struct inode **ret_inode)
1590 struct inode *ea_inode;
1594 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1595 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1597 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1603 *ret_inode = ea_inode;
1607 /* Create an inode for the EA value */
1608 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
1609 if (IS_ERR(ea_inode))
1610 return PTR_ERR(ea_inode);
1612 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
1614 if (ext4_xattr_inode_dec_ref(handle, ea_inode))
1615 ext4_warning_inode(ea_inode, "cleanup dec ref error %d", err);
1620 if (EA_INODE_CACHE(inode))
1621 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1622 ea_inode->i_ino, true /* reusable */);
1624 *ret_inode = ea_inode;
1629 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1630 * feature is enabled.
1632 #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1634 static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1635 struct ext4_xattr_search *s,
1636 handle_t *handle, struct inode *inode,
1639 struct ext4_xattr_entry *last, *next;
1640 struct ext4_xattr_entry *here = s->here;
1641 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
1642 int in_inode = i->in_inode;
1643 struct inode *old_ea_inode = NULL;
1644 struct inode *new_ea_inode = NULL;
1645 size_t old_size, new_size;
1648 /* Space used by old and new values. */
1649 old_size = (!s->not_found && !here->e_value_inum) ?
1650 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1651 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1654 * Optimization for the simple case when old and new values have the
1655 * same padded sizes. Not applicable if external inodes are involved.
1657 if (new_size && new_size == old_size) {
1658 size_t offs = le16_to_cpu(here->e_value_offs);
1659 void *val = s->base + offs;
1661 here->e_value_size = cpu_to_le32(i->value_len);
1662 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1663 memset(val, 0, new_size);
1665 memcpy(val, i->value, i->value_len);
1666 /* Clear padding bytes. */
1667 memset(val + i->value_len, 0, new_size - i->value_len);
1672 /* Compute min_offs and last. */
1674 for (; !IS_LAST_ENTRY(last); last = next) {
1675 next = EXT4_XATTR_NEXT(last);
1676 if ((void *)next >= s->end) {
1677 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
1678 ret = -EFSCORRUPTED;
1681 if (!last->e_value_inum && last->e_value_size) {
1682 size_t offs = le16_to_cpu(last->e_value_offs);
1683 if (offs < min_offs)
1688 /* Check whether we have enough space. */
1692 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1694 free += EXT4_XATTR_LEN(name_len) + old_size;
1696 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1702 * If storing the value in an external inode is an option,
1703 * reserve space for xattr entries/names in the external
1704 * attribute block so that a long value does not occupy the
1705 * whole space and prevent further entries being added.
1707 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1708 new_size && is_block &&
1709 (min_offs + old_size - new_size) <
1710 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1717 * Getting access to old and new ea inodes is subject to failures.
1718 * Finish that work before doing any modifications to the xattr data.
1720 if (!s->not_found && here->e_value_inum) {
1721 ret = ext4_xattr_inode_iget(inode,
1722 le32_to_cpu(here->e_value_inum),
1723 le32_to_cpu(here->e_hash),
1726 old_ea_inode = NULL;
1730 if (i->value && in_inode) {
1731 WARN_ON_ONCE(!i->value_len);
1733 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1737 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1741 new_ea_inode = NULL;
1742 ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
1748 /* We are ready to release ref count on the old_ea_inode. */
1749 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1751 /* Release newly required ref count on new_ea_inode. */
1755 err = ext4_xattr_inode_dec_ref(handle,
1758 ext4_warning_inode(new_ea_inode,
1759 "dec ref new_ea_inode err=%d",
1761 ext4_xattr_inode_free_quota(inode, new_ea_inode,
1767 ext4_xattr_inode_free_quota(inode, old_ea_inode,
1768 le32_to_cpu(here->e_value_size));
1771 /* No failures allowed past this point. */
1773 if (!s->not_found && here->e_value_size && !here->e_value_inum) {
1774 /* Remove the old value. */
1775 void *first_val = s->base + min_offs;
1776 size_t offs = le16_to_cpu(here->e_value_offs);
1777 void *val = s->base + offs;
1779 memmove(first_val + old_size, first_val, val - first_val);
1780 memset(first_val, 0, old_size);
1781 min_offs += old_size;
1783 /* Adjust all value offsets. */
1785 while (!IS_LAST_ENTRY(last)) {
1786 size_t o = le16_to_cpu(last->e_value_offs);
1788 if (!last->e_value_inum &&
1789 last->e_value_size && o < offs)
1790 last->e_value_offs = cpu_to_le16(o + old_size);
1791 last = EXT4_XATTR_NEXT(last);
1796 /* Remove old name. */
1797 size_t size = EXT4_XATTR_LEN(name_len);
1799 last = ENTRY((void *)last - size);
1800 memmove(here, (void *)here + size,
1801 (void *)last - (void *)here + sizeof(__u32));
1802 memset(last, 0, size);
1803 } else if (s->not_found) {
1804 /* Insert new name. */
1805 size_t size = EXT4_XATTR_LEN(name_len);
1806 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1808 memmove((void *)here + size, here, rest);
1809 memset(here, 0, size);
1810 here->e_name_index = i->name_index;
1811 here->e_name_len = name_len;
1812 memcpy(here->e_name, i->name, name_len);
1814 /* This is an update, reset value info. */
1815 here->e_value_inum = 0;
1816 here->e_value_offs = 0;
1817 here->e_value_size = 0;
1821 /* Insert new value. */
1823 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
1824 } else if (i->value_len) {
1825 void *val = s->base + min_offs - new_size;
1827 here->e_value_offs = cpu_to_le16(min_offs - new_size);
1828 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1829 memset(val, 0, new_size);
1831 memcpy(val, i->value, i->value_len);
1832 /* Clear padding bytes. */
1833 memset(val + i->value_len, 0,
1834 new_size - i->value_len);
1837 here->e_value_size = cpu_to_le32(i->value_len);
1844 /* Entry hash calculation. */
1849 * Feed crc32c hash instead of the raw value for entry
1850 * hash calculation. This is to avoid walking
1851 * potentially long value buffer again.
1853 crc32c_hash = cpu_to_le32(
1854 ext4_xattr_inode_get_hash(new_ea_inode));
1855 hash = ext4_xattr_hash_entry(here->e_name,
1858 } else if (is_block) {
1859 __le32 *value = s->base + le16_to_cpu(
1860 here->e_value_offs);
1862 hash = ext4_xattr_hash_entry(here->e_name,
1863 here->e_name_len, value,
1866 here->e_hash = hash;
1870 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1879 struct ext4_xattr_block_find {
1880 struct ext4_xattr_search s;
1881 struct buffer_head *bh;
1885 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1886 struct ext4_xattr_block_find *bs)
1888 struct super_block *sb = inode->i_sb;
1891 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1892 i->name_index, i->name, i->value, (long)i->value_len);
1894 if (EXT4_I(inode)->i_file_acl) {
1895 /* The inode already has an extended attribute block. */
1896 bs->bh = ext4_sb_bread(sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
1897 if (IS_ERR(bs->bh)) {
1898 error = PTR_ERR(bs->bh);
1902 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1903 atomic_read(&(bs->bh->b_count)),
1904 le32_to_cpu(BHDR(bs->bh)->h_refcount));
1905 error = ext4_xattr_check_block(inode, bs->bh);
1908 /* Find the named attribute. */
1909 bs->s.base = BHDR(bs->bh);
1910 bs->s.first = BFIRST(bs->bh);
1911 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1912 bs->s.here = bs->s.first;
1913 error = xattr_find_entry(inode, &bs->s.here, bs->s.end,
1914 i->name_index, i->name, 1);
1915 if (error && error != -ENODATA)
1917 bs->s.not_found = error;
1923 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1924 struct ext4_xattr_info *i,
1925 struct ext4_xattr_block_find *bs)
1927 struct super_block *sb = inode->i_sb;
1928 struct buffer_head *new_bh = NULL;
1929 struct ext4_xattr_search s_copy = bs->s;
1930 struct ext4_xattr_search *s = &s_copy;
1931 struct mb_cache_entry *ce = NULL;
1933 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1934 struct inode *ea_inode = NULL, *tmp_inode;
1935 size_t old_ea_inode_quota = 0;
1936 unsigned int ea_ino;
1939 #define header(x) ((struct ext4_xattr_header *)(x))
1942 int offset = (char *)s->here - bs->bh->b_data;
1944 BUFFER_TRACE(bs->bh, "get_write_access");
1945 error = ext4_journal_get_write_access(handle, sb, bs->bh,
1949 lock_buffer(bs->bh);
1951 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
1952 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1955 * This must happen under buffer lock for
1956 * ext4_xattr_block_set() to reliably detect modified
1959 if (ea_block_cache) {
1960 struct mb_cache_entry *oe;
1962 oe = mb_cache_entry_delete_or_get(ea_block_cache,
1963 hash, bs->bh->b_blocknr);
1966 * Xattr block is getting reused. Leave
1969 mb_cache_entry_put(ea_block_cache, oe);
1973 ea_bdebug(bs->bh, "modifying in-place");
1974 error = ext4_xattr_set_entry(i, s, handle, inode,
1975 true /* is_block */);
1976 ext4_xattr_block_csum_set(inode, bs->bh);
1977 unlock_buffer(bs->bh);
1978 if (error == -EFSCORRUPTED)
1981 error = ext4_handle_dirty_metadata(handle,
1989 unlock_buffer(bs->bh);
1990 ea_bdebug(bs->bh, "cloning");
1991 s->base = kmemdup(BHDR(bs->bh), bs->bh->b_size, GFP_NOFS);
1993 if (s->base == NULL)
1995 s->first = ENTRY(header(s->base)+1);
1996 header(s->base)->h_refcount = cpu_to_le32(1);
1997 s->here = ENTRY(s->base + offset);
1998 s->end = s->base + bs->bh->b_size;
2001 * If existing entry points to an xattr inode, we need
2002 * to prevent ext4_xattr_set_entry() from decrementing
2003 * ref count on it because the reference belongs to the
2004 * original block. In this case, make the entry look
2005 * like it has an empty value.
2007 if (!s->not_found && s->here->e_value_inum) {
2008 ea_ino = le32_to_cpu(s->here->e_value_inum);
2009 error = ext4_xattr_inode_iget(inode, ea_ino,
2010 le32_to_cpu(s->here->e_hash),
2015 if (!ext4_test_inode_state(tmp_inode,
2016 EXT4_STATE_LUSTRE_EA_INODE)) {
2018 * Defer quota free call for previous
2019 * inode until success is guaranteed.
2021 old_ea_inode_quota = le32_to_cpu(
2022 s->here->e_value_size);
2026 s->here->e_value_inum = 0;
2027 s->here->e_value_size = 0;
2030 /* Allocate a buffer where we construct the new block. */
2031 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
2033 if (s->base == NULL)
2035 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2036 header(s->base)->h_blocks = cpu_to_le32(1);
2037 header(s->base)->h_refcount = cpu_to_le32(1);
2038 s->first = ENTRY(header(s->base)+1);
2039 s->here = ENTRY(header(s->base)+1);
2040 s->end = s->base + sb->s_blocksize;
2043 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
2044 if (error == -EFSCORRUPTED)
2049 if (i->value && s->here->e_value_inum) {
2051 * A ref count on ea_inode has been taken as part of the call to
2052 * ext4_xattr_set_entry() above. We would like to drop this
2053 * extra ref but we have to wait until the xattr block is
2054 * initialized and has its own ref count on the ea_inode.
2056 ea_ino = le32_to_cpu(s->here->e_value_inum);
2057 error = ext4_xattr_inode_iget(inode, ea_ino,
2058 le32_to_cpu(s->here->e_hash),
2067 if (!IS_LAST_ENTRY(s->first)) {
2068 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
2071 /* We found an identical block in the cache. */
2072 if (new_bh == bs->bh)
2073 ea_bdebug(new_bh, "keeping");
2077 WARN_ON_ONCE(dquot_initialize_needed(inode));
2079 /* The old block is released after updating
2081 error = dquot_alloc_block(inode,
2082 EXT4_C2B(EXT4_SB(sb), 1));
2085 BUFFER_TRACE(new_bh, "get_write_access");
2086 error = ext4_journal_get_write_access(
2091 lock_buffer(new_bh);
2093 * We have to be careful about races with
2094 * adding references to xattr block. Once we
2095 * hold buffer lock xattr block's state is
2096 * stable so we can check the additional
2099 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2100 if (ref > EXT4_XATTR_REFCOUNT_MAX) {
2102 * Undo everything and check mbcache
2105 unlock_buffer(new_bh);
2106 dquot_free_block(inode,
2107 EXT4_C2B(EXT4_SB(sb),
2110 mb_cache_entry_put(ea_block_cache, ce);
2115 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2116 if (ref == EXT4_XATTR_REFCOUNT_MAX)
2117 clear_bit(MBE_REUSABLE_B, &ce->e_flags);
2118 ea_bdebug(new_bh, "reusing; refcount now=%d",
2120 ext4_xattr_block_csum_set(inode, new_bh);
2121 unlock_buffer(new_bh);
2122 error = ext4_handle_dirty_metadata(handle,
2128 mb_cache_entry_touch(ea_block_cache, ce);
2129 mb_cache_entry_put(ea_block_cache, ce);
2131 } else if (bs->bh && s->base == bs->bh->b_data) {
2132 /* We were modifying this block in-place. */
2133 ea_bdebug(bs->bh, "keeping this block");
2134 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
2138 /* We need to allocate a new block */
2139 ext4_fsblk_t goal, block;
2141 WARN_ON_ONCE(dquot_initialize_needed(inode));
2143 goal = ext4_group_first_block_no(sb,
2144 EXT4_I(inode)->i_block_group);
2145 block = ext4_new_meta_blocks(handle, inode, goal, 0,
2150 ea_idebug(inode, "creating block %llu",
2151 (unsigned long long)block);
2153 new_bh = sb_getblk(sb, block);
2154 if (unlikely(!new_bh)) {
2157 ext4_free_blocks(handle, inode, NULL, block, 1,
2158 EXT4_FREE_BLOCKS_METADATA);
2161 error = ext4_xattr_inode_inc_ref_all(handle, inode,
2162 ENTRY(header(s->base)+1));
2166 /* Drop the extra ref on ea_inode. */
2167 error = ext4_xattr_inode_dec_ref(handle,
2170 ext4_warning_inode(ea_inode,
2177 lock_buffer(new_bh);
2178 error = ext4_journal_get_create_access(handle, sb,
2179 new_bh, EXT4_JTR_NONE);
2181 unlock_buffer(new_bh);
2185 memcpy(new_bh->b_data, s->base, new_bh->b_size);
2186 ext4_xattr_block_csum_set(inode, new_bh);
2187 set_buffer_uptodate(new_bh);
2188 unlock_buffer(new_bh);
2189 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
2190 error = ext4_handle_dirty_metadata(handle, inode,
2197 if (old_ea_inode_quota)
2198 ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
2200 /* Update the inode. */
2201 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
2203 /* Drop the previous xattr block. */
2204 if (bs->bh && bs->bh != new_bh) {
2205 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2207 ext4_xattr_release_block(handle, inode, bs->bh,
2209 0 /* extra_credits */);
2210 ext4_xattr_inode_array_free(ea_inode_array);
2218 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2220 ext4_warning_inode(ea_inode, "dec ref error=%d",
2223 /* If there was an error, revert the quota charge. */
2225 ext4_xattr_inode_free_quota(inode, ea_inode,
2226 i_size_read(ea_inode));
2230 mb_cache_entry_put(ea_block_cache, ce);
2232 if (!(bs->bh && s->base == bs->bh->b_data))
2238 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
2242 EXT4_ERROR_INODE(inode, "bad block %llu",
2243 EXT4_I(inode)->i_file_acl);
2249 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2250 struct ext4_xattr_ibody_find *is)
2252 struct ext4_xattr_ibody_header *header;
2253 struct ext4_inode *raw_inode;
2256 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2259 raw_inode = ext4_raw_inode(&is->iloc);
2260 header = IHDR(inode, raw_inode);
2261 is->s.base = is->s.first = IFIRST(header);
2262 is->s.here = is->s.first;
2263 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2264 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2265 error = xattr_check_inode(inode, header, is->s.end);
2268 /* Find the named attribute. */
2269 error = xattr_find_entry(inode, &is->s.here, is->s.end,
2270 i->name_index, i->name, 0);
2271 if (error && error != -ENODATA)
2273 is->s.not_found = error;
2278 int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
2279 struct ext4_xattr_info *i,
2280 struct ext4_xattr_ibody_find *is)
2282 struct ext4_xattr_ibody_header *header;
2283 struct ext4_xattr_search *s = &is->s;
2286 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2289 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
2292 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2293 if (!IS_LAST_ENTRY(s->first)) {
2294 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2295 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2297 header->h_magic = cpu_to_le32(0);
2298 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2303 static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2304 struct ext4_xattr_info *i)
2308 /* When e_value_inum is set the value is stored externally. */
2309 if (s->here->e_value_inum)
2311 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2313 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2314 return !memcmp(value, i->value, i->value_len);
2317 static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2319 struct buffer_head *bh;
2322 if (!EXT4_I(inode)->i_file_acl)
2324 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2327 error = ext4_xattr_check_block(inode, bh);
2330 return ERR_PTR(error);
2336 * ext4_xattr_set_handle()
2338 * Create, replace or remove an extended attribute for this inode. Value
2339 * is NULL to remove an existing extended attribute, and non-NULL to
2340 * either replace an existing extended attribute, or create a new extended
2341 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2342 * specify that an extended attribute must exist and must not exist
2343 * previous to the call, respectively.
2345 * Returns 0, or a negative error number on failure.
2348 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
2349 const char *name, const void *value, size_t value_len,
2352 struct ext4_xattr_info i = {
2353 .name_index = name_index,
2356 .value_len = value_len,
2359 struct ext4_xattr_ibody_find is = {
2360 .s = { .not_found = -ENODATA, },
2362 struct ext4_xattr_block_find bs = {
2363 .s = { .not_found = -ENODATA, },
2370 if (strlen(name) > 255)
2373 ext4_write_lock_xattr(inode, &no_expand);
2375 /* Check journal credits under write lock. */
2376 if (ext4_handle_valid(handle)) {
2377 struct buffer_head *bh;
2380 bh = ext4_xattr_get_block(inode);
2382 error = PTR_ERR(bh);
2386 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2388 flags & XATTR_CREATE);
2391 if (jbd2_handle_buffer_credits(handle) < credits) {
2395 WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
2398 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
2402 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
2403 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2404 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
2405 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
2408 error = ext4_xattr_ibody_find(inode, &i, &is);
2412 error = ext4_xattr_block_find(inode, &i, &bs);
2415 if (is.s.not_found && bs.s.not_found) {
2417 if (flags & XATTR_REPLACE)
2424 if (flags & XATTR_CREATE)
2429 if (!is.s.not_found)
2430 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2431 else if (!bs.s.not_found)
2432 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2435 /* Xattr value did not change? Save us some work and bail out */
2436 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2438 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2441 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2442 (EXT4_XATTR_SIZE(i.value_len) >
2443 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2446 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2447 if (!error && !bs.s.not_found) {
2449 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2450 } else if (error == -ENOSPC) {
2451 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2454 error = ext4_xattr_block_find(inode, &i, &bs);
2458 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2459 if (!error && !is.s.not_found) {
2461 error = ext4_xattr_ibody_set(handle, inode, &i,
2463 } else if (error == -ENOSPC) {
2465 * Xattr does not fit in the block, store at
2466 * external inode if possible.
2468 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2469 i.value_len && !i.in_inode) {
2477 ext4_xattr_update_super_block(handle, inode->i_sb);
2478 inode->i_ctime = current_time(inode);
2479 inode_inc_iversion(inode);
2482 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
2484 * The bh is consumed by ext4_mark_iloc_dirty, even with
2489 ext4_handle_sync(handle);
2491 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle);
2496 ext4_write_unlock_xattr(inode, &no_expand);
2500 int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2501 bool is_create, int *credits)
2503 struct buffer_head *bh;
2508 if (!EXT4_SB(inode->i_sb)->s_journal)
2511 down_read(&EXT4_I(inode)->xattr_sem);
2513 bh = ext4_xattr_get_block(inode);
2517 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2518 value_len, is_create);
2523 up_read(&EXT4_I(inode)->xattr_sem);
2530 * Like ext4_xattr_set_handle, but start from an inode. This extended
2531 * attribute modification is a filesystem transaction by itself.
2533 * Returns 0, or a negative error number on failure.
2536 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
2537 const void *value, size_t value_len, int flags)
2540 struct super_block *sb = inode->i_sb;
2541 int error, retries = 0;
2544 error = dquot_initialize(inode);
2549 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2554 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
2555 if (IS_ERR(handle)) {
2556 error = PTR_ERR(handle);
2560 error = ext4_xattr_set_handle(handle, inode, name_index, name,
2561 value, value_len, flags);
2562 error2 = ext4_journal_stop(handle);
2563 if (error == -ENOSPC &&
2564 ext4_should_retry_alloc(sb, &retries))
2569 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, NULL);
2575 * Shift the EA entries in the inode to create space for the increased
2578 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2579 int value_offs_shift, void *to,
2580 void *from, size_t n)
2582 struct ext4_xattr_entry *last = entry;
2585 /* We always shift xattr headers further thus offsets get lower */
2586 BUG_ON(value_offs_shift > 0);
2588 /* Adjust the value offsets of the entries */
2589 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2590 if (!last->e_value_inum && last->e_value_size) {
2591 new_offs = le16_to_cpu(last->e_value_offs) +
2593 last->e_value_offs = cpu_to_le16(new_offs);
2596 /* Shift the entries by n bytes */
2597 memmove(to, from, n);
2601 * Move xattr pointed to by 'entry' from inode into external xattr block
2603 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2604 struct ext4_inode *raw_inode,
2605 struct ext4_xattr_entry *entry)
2607 struct ext4_xattr_ibody_find *is = NULL;
2608 struct ext4_xattr_block_find *bs = NULL;
2609 char *buffer = NULL, *b_entry_name = NULL;
2610 size_t value_size = le32_to_cpu(entry->e_value_size);
2611 struct ext4_xattr_info i = {
2614 .name_index = entry->e_name_index,
2615 .in_inode = !!entry->e_value_inum,
2617 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2620 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2621 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2622 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2623 if (!is || !bs || !b_entry_name) {
2628 is->s.not_found = -ENODATA;
2629 bs->s.not_found = -ENODATA;
2633 /* Save the entry name and the entry value */
2634 if (entry->e_value_inum) {
2635 buffer = kvmalloc(value_size, GFP_NOFS);
2641 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
2645 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2646 buffer = (void *)IFIRST(header) + value_offs;
2649 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2650 b_entry_name[entry->e_name_len] = '\0';
2651 i.name = b_entry_name;
2653 error = ext4_get_inode_loc(inode, &is->iloc);
2657 error = ext4_xattr_ibody_find(inode, &i, is);
2662 i.value_len = value_size;
2663 error = ext4_xattr_block_find(inode, &i, bs);
2667 /* Move ea entry from the inode into the block */
2668 error = ext4_xattr_block_set(handle, inode, &i, bs);
2672 /* Remove the chosen entry from the inode */
2675 error = ext4_xattr_ibody_set(handle, inode, &i, is);
2678 kfree(b_entry_name);
2679 if (entry->e_value_inum && buffer)
2682 brelse(is->iloc.bh);
2691 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2692 struct ext4_inode *raw_inode,
2693 int isize_diff, size_t ifree,
2694 size_t bfree, int *total_ino)
2696 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2697 struct ext4_xattr_entry *small_entry;
2698 struct ext4_xattr_entry *entry;
2699 struct ext4_xattr_entry *last;
2700 unsigned int entry_size; /* EA entry size */
2701 unsigned int total_size; /* EA entry size + value size */
2702 unsigned int min_total_size;
2705 while (isize_diff > ifree) {
2708 min_total_size = ~0U;
2709 last = IFIRST(header);
2710 /* Find the entry best suited to be pushed into EA block */
2711 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2712 /* never move system.data out of the inode */
2713 if ((last->e_name_len == 4) &&
2714 (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
2715 !memcmp(last->e_name, "data", 4))
2717 total_size = EXT4_XATTR_LEN(last->e_name_len);
2718 if (!last->e_value_inum)
2719 total_size += EXT4_XATTR_SIZE(
2720 le32_to_cpu(last->e_value_size));
2721 if (total_size <= bfree &&
2722 total_size < min_total_size) {
2723 if (total_size + ifree < isize_diff) {
2727 min_total_size = total_size;
2732 if (entry == NULL) {
2733 if (small_entry == NULL)
2735 entry = small_entry;
2738 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
2739 total_size = entry_size;
2740 if (!entry->e_value_inum)
2741 total_size += EXT4_XATTR_SIZE(
2742 le32_to_cpu(entry->e_value_size));
2743 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2748 *total_ino -= entry_size;
2749 ifree += total_size;
2750 bfree -= total_size;
2757 * Expand an inode by new_extra_isize bytes when EAs are present.
2758 * Returns 0 on success or negative error number on failure.
2760 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2761 struct ext4_inode *raw_inode, handle_t *handle)
2763 struct ext4_xattr_ibody_header *header;
2764 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2765 static unsigned int mnt_count;
2767 size_t ifree, bfree;
2770 int error = 0, tried_min_extra_isize = 0;
2771 int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
2772 int isize_diff; /* How much do we need to grow i_extra_isize */
2775 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
2776 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
2779 header = IHDR(inode, raw_inode);
2782 * Check if enough free space is available in the inode to shift the
2783 * entries ahead by new_extra_isize.
2786 base = IFIRST(header);
2787 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2788 min_offs = end - base;
2789 total_ino = sizeof(struct ext4_xattr_ibody_header) + sizeof(u32);
2791 error = xattr_check_inode(inode, header, end);
2795 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
2796 if (ifree >= isize_diff)
2800 * Enough free space isn't available in the inode, check if
2801 * EA block can hold new_extra_isize bytes.
2803 if (EXT4_I(inode)->i_file_acl) {
2804 struct buffer_head *bh;
2806 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2808 error = PTR_ERR(bh);
2811 error = ext4_xattr_check_block(inode, bh);
2817 end = bh->b_data + bh->b_size;
2818 min_offs = end - base;
2819 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2822 if (bfree + ifree < isize_diff) {
2823 if (!tried_min_extra_isize && s_min_extra_isize) {
2824 tried_min_extra_isize++;
2825 new_extra_isize = s_min_extra_isize;
2832 bfree = inode->i_sb->s_blocksize;
2835 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2836 isize_diff, ifree, bfree,
2839 if (error == -ENOSPC && !tried_min_extra_isize &&
2840 s_min_extra_isize) {
2841 tried_min_extra_isize++;
2842 new_extra_isize = s_min_extra_isize;
2848 /* Adjust the offsets and shift the remaining entries ahead */
2849 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
2850 - new_extra_isize, (void *)raw_inode +
2851 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
2852 (void *)header, total_ino);
2853 EXT4_I(inode)->i_extra_isize = new_extra_isize;
2856 if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
2857 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2859 mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
2864 #define EIA_INCR 16 /* must be 2^n */
2865 #define EIA_MASK (EIA_INCR - 1)
2867 /* Add the large xattr @inode into @ea_inode_array for deferred iput().
2868 * If @ea_inode_array is new or full it will be grown and the old
2869 * contents copied over.
2872 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2873 struct inode *inode)
2875 if (*ea_inode_array == NULL) {
2877 * Start with 15 inodes, so it fits into a power-of-two size.
2878 * If *ea_inode_array is NULL, this is essentially offsetof()
2881 kmalloc(offsetof(struct ext4_xattr_inode_array,
2884 if (*ea_inode_array == NULL)
2886 (*ea_inode_array)->count = 0;
2887 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
2888 /* expand the array once all 15 + n * 16 slots are full */
2889 struct ext4_xattr_inode_array *new_array = NULL;
2890 int count = (*ea_inode_array)->count;
2892 /* if new_array is NULL, this is essentially offsetof() */
2893 new_array = kmalloc(
2894 offsetof(struct ext4_xattr_inode_array,
2895 inodes[count + EIA_INCR]),
2897 if (new_array == NULL)
2899 memcpy(new_array, *ea_inode_array,
2900 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2901 kfree(*ea_inode_array);
2902 *ea_inode_array = new_array;
2904 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
2909 * ext4_xattr_delete_inode()
2911 * Free extended attribute resources associated with this inode. Traverse
2912 * all entries and decrement reference on any xattr inodes associated with this
2913 * inode. This is called immediately before an inode is freed. We have exclusive
2914 * access to the inode. If an orphan inode is deleted it will also release its
2915 * references on xattr block and xattr inodes.
2917 int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2918 struct ext4_xattr_inode_array **ea_inode_array,
2921 struct buffer_head *bh = NULL;
2922 struct ext4_xattr_ibody_header *header;
2923 struct ext4_iloc iloc = { .bh = NULL };
2924 struct ext4_xattr_entry *entry;
2925 struct inode *ea_inode;
2928 error = ext4_journal_ensure_credits(handle, extra_credits,
2929 ext4_free_metadata_revoke_credits(inode->i_sb, 1));
2931 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2935 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2936 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2938 error = ext4_get_inode_loc(inode, &iloc);
2940 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
2944 error = ext4_journal_get_write_access(handle, inode->i_sb,
2945 iloc.bh, EXT4_JTR_NONE);
2947 EXT4_ERROR_INODE(inode, "write access (error %d)",
2952 header = IHDR(inode, ext4_raw_inode(&iloc));
2953 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2954 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2956 false /* block_csum */,
2959 false /* skip_quota */);
2962 if (EXT4_I(inode)->i_file_acl) {
2963 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2965 error = PTR_ERR(bh);
2966 if (error == -EIO) {
2967 EXT4_ERROR_INODE_ERR(inode, EIO,
2968 "block %llu read error",
2969 EXT4_I(inode)->i_file_acl);
2974 error = ext4_xattr_check_block(inode, bh);
2978 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2979 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2980 entry = EXT4_XATTR_NEXT(entry)) {
2981 if (!entry->e_value_inum)
2983 error = ext4_xattr_inode_iget(inode,
2984 le32_to_cpu(entry->e_value_inum),
2985 le32_to_cpu(entry->e_hash),
2989 ext4_xattr_inode_free_quota(inode, ea_inode,
2990 le32_to_cpu(entry->e_value_size));
2996 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2999 * Update i_file_acl value in the same transaction that releases
3002 EXT4_I(inode)->i_file_acl = 0;
3003 error = ext4_mark_inode_dirty(handle, inode);
3005 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
3009 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle);
3018 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
3022 if (ea_inode_array == NULL)
3025 for (idx = 0; idx < ea_inode_array->count; ++idx)
3026 iput(ea_inode_array->inodes[idx]);
3027 kfree(ea_inode_array);
3031 * ext4_xattr_block_cache_insert()
3033 * Create a new entry in the extended attribute block cache, and insert
3034 * it unless such an entry is already in the cache.
3036 * Returns 0, or a negative error number on failure.
3039 ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
3040 struct buffer_head *bh)
3042 struct ext4_xattr_header *header = BHDR(bh);
3043 __u32 hash = le32_to_cpu(header->h_hash);
3044 int reusable = le32_to_cpu(header->h_refcount) <
3045 EXT4_XATTR_REFCOUNT_MAX;
3048 if (!ea_block_cache)
3050 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
3051 bh->b_blocknr, reusable);
3053 if (error == -EBUSY)
3054 ea_bdebug(bh, "already in cache");
3056 ea_bdebug(bh, "inserting [%x]", (int)hash);
3062 * Compare two extended attribute blocks for equality.
3064 * Returns 0 if the blocks are equal, 1 if they differ, and
3065 * a negative error number on errors.
3068 ext4_xattr_cmp(struct ext4_xattr_header *header1,
3069 struct ext4_xattr_header *header2)
3071 struct ext4_xattr_entry *entry1, *entry2;
3073 entry1 = ENTRY(header1+1);
3074 entry2 = ENTRY(header2+1);
3075 while (!IS_LAST_ENTRY(entry1)) {
3076 if (IS_LAST_ENTRY(entry2))
3078 if (entry1->e_hash != entry2->e_hash ||
3079 entry1->e_name_index != entry2->e_name_index ||
3080 entry1->e_name_len != entry2->e_name_len ||
3081 entry1->e_value_size != entry2->e_value_size ||
3082 entry1->e_value_inum != entry2->e_value_inum ||
3083 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
3085 if (!entry1->e_value_inum &&
3086 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
3087 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
3088 le32_to_cpu(entry1->e_value_size)))
3091 entry1 = EXT4_XATTR_NEXT(entry1);
3092 entry2 = EXT4_XATTR_NEXT(entry2);
3094 if (!IS_LAST_ENTRY(entry2))
3100 * ext4_xattr_block_cache_find()
3102 * Find an identical extended attribute block.
3104 * Returns a pointer to the block found, or NULL if such a block was
3105 * not found or an error occurred.
3107 static struct buffer_head *
3108 ext4_xattr_block_cache_find(struct inode *inode,
3109 struct ext4_xattr_header *header,
3110 struct mb_cache_entry **pce)
3112 __u32 hash = le32_to_cpu(header->h_hash);
3113 struct mb_cache_entry *ce;
3114 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
3116 if (!ea_block_cache)
3118 if (!header->h_hash)
3119 return NULL; /* never share */
3120 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
3121 ce = mb_cache_entry_find_first(ea_block_cache, hash);
3123 struct buffer_head *bh;
3125 bh = ext4_sb_bread(inode->i_sb, ce->e_value, REQ_PRIO);
3127 if (PTR_ERR(bh) == -ENOMEM)
3130 EXT4_ERROR_INODE(inode, "block %lu read error",
3131 (unsigned long)ce->e_value);
3132 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
3137 ce = mb_cache_entry_find_next(ea_block_cache, ce);
3142 #define NAME_HASH_SHIFT 5
3143 #define VALUE_HASH_SHIFT 16
3146 * ext4_xattr_hash_entry()
3148 * Compute the hash of an extended attribute.
3150 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3155 while (name_len--) {
3156 hash = (hash << NAME_HASH_SHIFT) ^
3157 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3158 (unsigned char)*name++;
3160 while (value_count--) {
3161 hash = (hash << VALUE_HASH_SHIFT) ^
3162 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3163 le32_to_cpu(*value++);
3165 return cpu_to_le32(hash);
3169 * ext4_xattr_hash_entry_signed()
3171 * Compute the hash of an extended attribute incorrectly.
3173 static __le32 ext4_xattr_hash_entry_signed(char *name, size_t name_len, __le32 *value, size_t value_count)
3177 while (name_len--) {
3178 hash = (hash << NAME_HASH_SHIFT) ^
3179 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3180 (signed char)*name++;
3182 while (value_count--) {
3183 hash = (hash << VALUE_HASH_SHIFT) ^
3184 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3185 le32_to_cpu(*value++);
3187 return cpu_to_le32(hash);
3190 #undef NAME_HASH_SHIFT
3191 #undef VALUE_HASH_SHIFT
3193 #define BLOCK_HASH_SHIFT 16
3196 * ext4_xattr_rehash()
3198 * Re-compute the extended attribute hash value after an entry has changed.
3200 static void ext4_xattr_rehash(struct ext4_xattr_header *header)
3202 struct ext4_xattr_entry *here;
3205 here = ENTRY(header+1);
3206 while (!IS_LAST_ENTRY(here)) {
3207 if (!here->e_hash) {
3208 /* Block is not shared if an entry's hash value == 0 */
3212 hash = (hash << BLOCK_HASH_SHIFT) ^
3213 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3214 le32_to_cpu(here->e_hash);
3215 here = EXT4_XATTR_NEXT(here);
3217 header->h_hash = cpu_to_le32(hash);
3220 #undef BLOCK_HASH_SHIFT
3222 #define HASH_BUCKET_BITS 10
3225 ext4_xattr_create_cache(void)
3227 return mb_cache_create(HASH_BUCKET_BITS);
3230 void ext4_xattr_destroy_cache(struct mb_cache *cache)
3233 mb_cache_destroy(cache);