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] = &nop_posix_acl_access,
92 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &nop_posix_acl_default,
94 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
95 #ifdef CONFIG_EXT4_FS_SECURITY
96 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
98 [EXT4_XATTR_INDEX_HURD] = &ext4_xattr_hurd_handler,
101 const struct xattr_handler * const ext4_xattr_handlers[] = {
102 &ext4_xattr_user_handler,
103 &ext4_xattr_trusted_handler,
104 #ifdef CONFIG_EXT4_FS_SECURITY
105 &ext4_xattr_security_handler,
107 &ext4_xattr_hurd_handler,
111 #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
112 inode->i_sb->s_fs_info)->s_ea_block_cache)
114 #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
115 inode->i_sb->s_fs_info)->s_ea_inode_cache)
118 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
119 struct inode *inode);
121 #ifdef CONFIG_LOCKDEP
122 void ext4_xattr_inode_set_class(struct inode *ea_inode)
124 struct ext4_inode_info *ei = EXT4_I(ea_inode);
126 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
127 (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */
128 lockdep_set_subclass(&ei->i_data_sem, I_DATA_SEM_EA);
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->s_csum_seed, (__u8 *)&dsk_block_nr,
143 sizeof(dsk_block_nr));
144 csum = ext4_chksum(csum, (__u8 *)hdr, offset);
145 csum = ext4_chksum(csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
146 offset += sizeof(dummy_csum);
147 csum = ext4_chksum(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_feature_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_feature_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 char *ext4_xattr_prefix(int name_index,
177 struct dentry *dentry)
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];
184 if (!xattr_handler_can_list(handler, dentry))
187 return xattr_prefix(handler);
191 check_xattrs(struct inode *inode, struct buffer_head *bh,
192 struct ext4_xattr_entry *entry, void *end, void *value_start,
193 const char *function, unsigned int line)
195 struct ext4_xattr_entry *e = entry;
196 int err = -EFSCORRUPTED;
200 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
201 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
202 err_str = "invalid header";
205 if (buffer_verified(bh))
207 if (!ext4_xattr_block_csum_verify(inode, bh)) {
209 err_str = "invalid checksum";
213 struct ext4_xattr_ibody_header *header = value_start;
216 if (end - (void *)header < sizeof(*header) + sizeof(u32)) {
217 err_str = "in-inode xattr block too small";
220 if (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
221 err_str = "bad magic number in in-inode xattr";
226 /* Find the end of the names list */
227 while (!IS_LAST_ENTRY(e)) {
228 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
229 if ((void *)next >= end) {
230 err_str = "e_name out of bounds";
233 if (strnlen(e->e_name, e->e_name_len) != e->e_name_len) {
234 err_str = "bad e_name length";
240 /* Check the values */
241 while (!IS_LAST_ENTRY(entry)) {
242 u32 size = le32_to_cpu(entry->e_value_size);
243 unsigned long ea_ino = le32_to_cpu(entry->e_value_inum);
245 if (!ext4_has_feature_ea_inode(inode->i_sb) && ea_ino) {
246 err_str = "ea_inode specified without ea_inode feature enabled";
249 if (ea_ino && ((ea_ino == EXT4_ROOT_INO) ||
250 !ext4_valid_inum(inode->i_sb, ea_ino))) {
251 err_str = "invalid ea_ino";
254 if (size > EXT4_XATTR_SIZE_MAX) {
255 err_str = "e_value size too large";
259 if (size != 0 && entry->e_value_inum == 0) {
260 u16 offs = le16_to_cpu(entry->e_value_offs);
264 * The value cannot overlap the names, and the value
265 * with padding cannot extend beyond 'end'. Check both
266 * the padded and unpadded sizes, since the size may
267 * overflow to 0 when adding padding.
269 if (offs > end - value_start) {
270 err_str = "e_value out of bounds";
273 value = value_start + offs;
274 if (value < (void *)e + sizeof(u32) ||
275 size > end - value ||
276 EXT4_XATTR_SIZE(size) > end - value) {
277 err_str = "overlapping e_value ";
281 entry = EXT4_XATTR_NEXT(entry);
284 set_buffer_verified(bh);
289 __ext4_error_inode(inode, function, line, 0, -err,
290 "corrupted xattr block %llu: %s",
291 (unsigned long long) bh->b_blocknr,
294 __ext4_error_inode(inode, function, line, 0, -err,
295 "corrupted in-inode xattr: %s", err_str);
300 __ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh,
301 const char *function, unsigned int line)
303 return check_xattrs(inode, bh, BFIRST(bh), bh->b_data + bh->b_size,
304 bh->b_data, function, line);
307 #define ext4_xattr_check_block(inode, bh) \
308 __ext4_xattr_check_block((inode), (bh), __func__, __LINE__)
312 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
313 void *end, const char *function, unsigned int line)
315 return check_xattrs(inode, NULL, IFIRST(header), end, IFIRST(header),
320 xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry,
321 void *end, int name_index, const char *name, int sorted)
323 struct ext4_xattr_entry *entry, *next;
329 name_len = strlen(name);
330 for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) {
331 next = EXT4_XATTR_NEXT(entry);
332 if ((void *) next >= end) {
333 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
334 return -EFSCORRUPTED;
336 cmp = name_index - entry->e_name_index;
338 cmp = name_len - entry->e_name_len;
340 cmp = memcmp(name, entry->e_name, name_len);
341 if (cmp <= 0 && (sorted || cmp == 0))
345 return cmp ? -ENODATA : 0;
349 ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
351 return ext4_chksum(sbi->s_csum_seed, buffer, size);
354 static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
356 return ((u64) inode_get_ctime_sec(ea_inode) << 32) |
357 (u32) inode_peek_iversion_raw(ea_inode);
360 static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
362 inode_set_ctime(ea_inode, (u32)(ref_count >> 32), 0);
363 inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
366 static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
368 return (u32) inode_get_atime_sec(ea_inode);
371 static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
373 inode_set_atime(ea_inode, hash, 0);
377 * Read the EA value from an inode.
379 static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
381 int blocksize = 1 << ea_inode->i_blkbits;
382 int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
383 int tail_size = (size % blocksize) ?: blocksize;
384 struct buffer_head *bhs_inline[8];
385 struct buffer_head **bhs = bhs_inline;
388 if (bh_count > ARRAY_SIZE(bhs_inline)) {
389 bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
394 ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
395 true /* wait */, bhs);
399 for (i = 0; i < bh_count; i++) {
400 /* There shouldn't be any holes in ea_inode. */
405 memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
406 i < bh_count - 1 ? blocksize : tail_size);
410 for (i = 0; i < bh_count; i++)
413 if (bhs != bhs_inline)
418 #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode_get_mtime_sec(inode)))
420 static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
421 u32 ea_inode_hash, struct inode **ea_inode)
427 * We have to check for this corruption early as otherwise
428 * iget_locked() could wait indefinitely for the state of our
431 if (parent->i_ino == ea_ino) {
432 ext4_error(parent->i_sb,
433 "Parent and EA inode have the same ino %lu", ea_ino);
434 return -EFSCORRUPTED;
437 inode = ext4_iget(parent->i_sb, ea_ino, EXT4_IGET_EA_INODE);
439 err = PTR_ERR(inode);
440 ext4_error(parent->i_sb,
441 "error while reading EA inode %lu err=%d", ea_ino,
445 ext4_xattr_inode_set_class(inode);
448 * Check whether this is an old Lustre-style xattr inode. Lustre
449 * implementation does not have hash validation, rather it has a
450 * backpointer from ea_inode to the parent inode.
452 if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
453 EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
454 inode->i_generation == parent->i_generation) {
455 ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
456 ext4_xattr_inode_set_ref(inode, 1);
458 inode_lock_nested(inode, I_MUTEX_XATTR);
459 inode->i_flags |= S_NOQUOTA;
467 /* Remove entry from mbcache when EA inode is getting evicted */
468 void ext4_evict_ea_inode(struct inode *inode)
470 struct mb_cache_entry *oe;
472 if (!EA_INODE_CACHE(inode))
474 /* Wait for entry to get unused so that we can remove it */
475 while ((oe = mb_cache_entry_delete_or_get(EA_INODE_CACHE(inode),
476 ext4_xattr_inode_get_hash(inode), inode->i_ino))) {
477 mb_cache_entry_wait_unused(oe);
478 mb_cache_entry_put(EA_INODE_CACHE(inode), oe);
483 ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
484 struct ext4_xattr_entry *entry, void *buffer,
489 /* Verify stored hash matches calculated hash. */
490 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
491 if (hash != ext4_xattr_inode_get_hash(ea_inode))
492 return -EFSCORRUPTED;
495 __le32 e_hash, tmp_data;
497 /* Verify entry hash. */
498 tmp_data = cpu_to_le32(hash);
499 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
502 if (e_hash == entry->e_hash)
506 * Not good. Maybe the entry hash was calculated
507 * using the buggy signed char version?
509 e_hash = ext4_xattr_hash_entry_signed(entry->e_name, entry->e_name_len,
511 /* Still no match - bad */
512 if (e_hash != entry->e_hash)
513 return -EFSCORRUPTED;
515 /* Let people know about old hash */
516 pr_warn_once("ext4: filesystem with signed xattr name hash");
522 * Read xattr value from the EA inode.
525 ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
526 void *buffer, size_t size)
528 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
529 struct inode *ea_inode;
532 err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
533 le32_to_cpu(entry->e_hash), &ea_inode);
539 if (i_size_read(ea_inode) != size) {
540 ext4_warning_inode(ea_inode,
541 "ea_inode file size=%llu entry size=%zu",
542 i_size_read(ea_inode), size);
547 err = ext4_xattr_inode_read(ea_inode, buffer, size);
551 if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
552 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
555 ext4_warning_inode(ea_inode,
556 "EA inode hash validation failed");
561 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
562 ext4_xattr_inode_get_hash(ea_inode),
563 ea_inode->i_ino, true /* reusable */);
571 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
572 void *buffer, size_t buffer_size)
574 struct buffer_head *bh = NULL;
575 struct ext4_xattr_entry *entry;
579 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
581 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
582 name_index, name, buffer, (long)buffer_size);
584 if (!EXT4_I(inode)->i_file_acl)
586 ea_idebug(inode, "reading block %llu",
587 (unsigned long long)EXT4_I(inode)->i_file_acl);
588 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
591 ea_bdebug(bh, "b_count=%d, refcount=%d",
592 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
593 error = ext4_xattr_check_block(inode, bh);
596 ext4_xattr_block_cache_insert(ea_block_cache, bh);
598 end = bh->b_data + bh->b_size;
599 error = xattr_find_entry(inode, &entry, end, name_index, name, 1);
602 size = le32_to_cpu(entry->e_value_size);
604 if (unlikely(size > EXT4_XATTR_SIZE_MAX))
607 if (size > buffer_size)
609 if (entry->e_value_inum) {
610 error = ext4_xattr_inode_get(inode, entry, buffer,
615 u16 offset = le16_to_cpu(entry->e_value_offs);
616 void *p = bh->b_data + offset;
618 if (unlikely(p + size > end))
620 memcpy(buffer, p, size);
631 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
632 void *buffer, size_t buffer_size)
634 struct ext4_xattr_ibody_header *header;
635 struct ext4_xattr_entry *entry;
636 struct ext4_inode *raw_inode;
637 struct ext4_iloc iloc;
642 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
644 error = ext4_get_inode_loc(inode, &iloc);
647 raw_inode = ext4_raw_inode(&iloc);
648 header = IHDR(inode, raw_inode);
649 end = ITAIL(inode, raw_inode);
650 entry = IFIRST(header);
651 error = xattr_find_entry(inode, &entry, end, name_index, name, 0);
654 size = le32_to_cpu(entry->e_value_size);
656 if (unlikely(size > EXT4_XATTR_SIZE_MAX))
659 if (size > buffer_size)
661 if (entry->e_value_inum) {
662 error = ext4_xattr_inode_get(inode, entry, buffer,
667 u16 offset = le16_to_cpu(entry->e_value_offs);
668 void *p = (void *)IFIRST(header) + offset;
670 if (unlikely(p + size > end))
672 memcpy(buffer, p, size);
685 * Copy an extended attribute into the buffer
686 * provided, or compute the buffer size required.
687 * Buffer is NULL to compute the size of the buffer required.
689 * Returns a negative error number on failure, or the number of bytes
690 * used / required on success.
693 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
694 void *buffer, size_t buffer_size)
698 if (unlikely(ext4_forced_shutdown(inode->i_sb)))
701 if (strlen(name) > 255)
704 down_read(&EXT4_I(inode)->xattr_sem);
705 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
707 if (error == -ENODATA)
708 error = ext4_xattr_block_get(inode, name_index, name, buffer,
710 up_read(&EXT4_I(inode)->xattr_sem);
715 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
716 char *buffer, size_t buffer_size)
718 size_t rest = buffer_size;
720 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
723 prefix = ext4_xattr_prefix(entry->e_name_index, dentry);
725 size_t prefix_len = strlen(prefix);
726 size_t size = prefix_len + entry->e_name_len + 1;
731 memcpy(buffer, prefix, prefix_len);
732 buffer += prefix_len;
733 memcpy(buffer, entry->e_name, entry->e_name_len);
734 buffer += entry->e_name_len;
740 return buffer_size - rest; /* total size */
744 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
746 struct inode *inode = d_inode(dentry);
747 struct buffer_head *bh = NULL;
750 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
751 buffer, (long)buffer_size);
753 if (!EXT4_I(inode)->i_file_acl)
755 ea_idebug(inode, "reading block %llu",
756 (unsigned long long)EXT4_I(inode)->i_file_acl);
757 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
760 ea_bdebug(bh, "b_count=%d, refcount=%d",
761 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
762 error = ext4_xattr_check_block(inode, bh);
765 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
766 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer,
774 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
776 struct inode *inode = d_inode(dentry);
777 struct ext4_xattr_ibody_header *header;
778 struct ext4_inode *raw_inode;
779 struct ext4_iloc iloc;
782 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
784 error = ext4_get_inode_loc(inode, &iloc);
787 raw_inode = ext4_raw_inode(&iloc);
788 header = IHDR(inode, raw_inode);
789 error = ext4_xattr_list_entries(dentry, IFIRST(header),
790 buffer, buffer_size);
797 * Inode operation listxattr()
799 * d_inode(dentry)->i_rwsem: don't care
801 * Copy a list of attribute names into the buffer
802 * provided, or compute the buffer size required.
803 * Buffer is NULL to compute the size of the buffer required.
805 * Returns a negative error number on failure, or the number of bytes
806 * used / required on success.
809 ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
813 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
814 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
821 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
826 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
831 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
834 static void ext4_xattr_update_super_block(handle_t *handle,
835 struct super_block *sb)
837 if (ext4_has_feature_xattr(sb))
840 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
841 if (ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh,
842 EXT4_JTR_NONE) == 0) {
843 lock_buffer(EXT4_SB(sb)->s_sbh);
844 ext4_set_feature_xattr(sb);
845 ext4_superblock_csum_set(sb);
846 unlock_buffer(EXT4_SB(sb)->s_sbh);
847 ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
851 int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
853 struct ext4_iloc iloc = { .bh = NULL };
854 struct buffer_head *bh = NULL;
855 struct ext4_inode *raw_inode;
856 struct ext4_xattr_ibody_header *header;
857 struct ext4_xattr_entry *entry;
858 qsize_t ea_inode_refs = 0;
861 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
863 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
864 ret = ext4_get_inode_loc(inode, &iloc);
867 raw_inode = ext4_raw_inode(&iloc);
868 header = IHDR(inode, raw_inode);
870 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
871 entry = EXT4_XATTR_NEXT(entry))
872 if (entry->e_value_inum)
876 if (EXT4_I(inode)->i_file_acl) {
877 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
884 ret = ext4_xattr_check_block(inode, bh);
888 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
889 entry = EXT4_XATTR_NEXT(entry))
890 if (entry->e_value_inum)
893 *usage = ea_inode_refs + 1;
901 static inline size_t round_up_cluster(struct inode *inode, size_t length)
903 struct super_block *sb = inode->i_sb;
904 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
906 size_t mask = ~(cluster_size - 1);
908 return (length + cluster_size - 1) & mask;
911 static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
915 err = dquot_alloc_inode(inode);
918 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
920 dquot_free_inode(inode);
924 static void ext4_xattr_inode_free_quota(struct inode *parent,
925 struct inode *ea_inode,
929 ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
931 dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
932 dquot_free_inode(parent);
935 int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
936 struct buffer_head *block_bh, size_t value_len,
943 * 1) Owner inode update
944 * 2) Ref count update on old xattr block
946 * 4) block bitmap update for new xattr block
947 * 5) group descriptor for new xattr block
948 * 6) block bitmap update for old xattr block
949 * 7) group descriptor for old block
951 * 6 & 7 can happen if we have two racing threads T_a and T_b
952 * which are each trying to set an xattr on inodes I_a and I_b
953 * which were both initially sharing an xattr block.
958 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
961 * In case of inline data, we may push out the data to a block,
962 * so we need to reserve credits for this eventuality
964 if (inode && ext4_has_inline_data(inode))
965 credits += ext4_writepage_trans_blocks(inode) + 1;
967 /* We are done if ea_inode feature is not enabled. */
968 if (!ext4_has_feature_ea_inode(sb))
971 /* New ea_inode, inode map, block bitmap, group descriptor. */
975 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
977 /* Indirection block or one level of extent tree. */
980 /* Block bitmap and group descriptor updates for each block. */
981 credits += blocks * 2;
983 /* Blocks themselves. */
987 /* Dereference ea_inode holding old xattr value.
988 * Old ea_inode, inode map, block bitmap, group descriptor.
992 /* Data blocks for old ea_inode. */
993 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
995 /* Indirection block or one level of extent tree for old
1000 /* Block bitmap and group descriptor updates for each block. */
1001 credits += blocks * 2;
1004 /* We may need to clone the existing xattr block in which case we need
1005 * to increment ref counts for existing ea_inodes referenced by it.
1008 struct ext4_xattr_entry *entry = BFIRST(block_bh);
1010 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
1011 if (entry->e_value_inum)
1012 /* Ref count update on ea_inode. */
1018 static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
1021 struct ext4_iloc iloc;
1025 inode_lock_nested(ea_inode, I_MUTEX_XATTR);
1027 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
1031 ref_count = ext4_xattr_inode_get_ref(ea_inode);
1032 ref_count += ref_change;
1033 ext4_xattr_inode_set_ref(ea_inode, ref_count);
1035 if (ref_change > 0) {
1036 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
1037 ea_inode->i_ino, ref_count);
1039 if (ref_count == 1) {
1040 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
1041 ea_inode->i_ino, ea_inode->i_nlink);
1043 set_nlink(ea_inode, 1);
1044 ext4_orphan_del(handle, ea_inode);
1047 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1048 ea_inode->i_ino, ref_count);
1050 if (ref_count == 0) {
1051 WARN_ONCE(ea_inode->i_nlink != 1,
1052 "EA inode %lu i_nlink=%u",
1053 ea_inode->i_ino, ea_inode->i_nlink);
1055 clear_nlink(ea_inode);
1056 ext4_orphan_add(handle, ea_inode);
1060 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1062 ext4_warning_inode(ea_inode,
1063 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1065 inode_unlock(ea_inode);
1069 static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1071 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1074 static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1076 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1079 static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1080 struct ext4_xattr_entry *first)
1082 struct inode *ea_inode;
1083 struct ext4_xattr_entry *entry;
1084 struct ext4_xattr_entry *failed_entry;
1085 unsigned int ea_ino;
1088 for (entry = first; !IS_LAST_ENTRY(entry);
1089 entry = EXT4_XATTR_NEXT(entry)) {
1090 if (!entry->e_value_inum)
1092 ea_ino = le32_to_cpu(entry->e_value_inum);
1093 err = ext4_xattr_inode_iget(parent, ea_ino,
1094 le32_to_cpu(entry->e_hash),
1098 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1100 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1110 failed_entry = entry;
1112 for (entry = first; entry != failed_entry;
1113 entry = EXT4_XATTR_NEXT(entry)) {
1114 if (!entry->e_value_inum)
1116 ea_ino = le32_to_cpu(entry->e_value_inum);
1117 err = ext4_xattr_inode_iget(parent, ea_ino,
1118 le32_to_cpu(entry->e_hash),
1121 ext4_warning(parent->i_sb,
1122 "cleanup ea_ino %u iget error %d", ea_ino,
1126 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1128 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1135 static int ext4_xattr_restart_fn(handle_t *handle, struct inode *inode,
1136 struct buffer_head *bh, bool block_csum, bool dirty)
1142 ext4_xattr_block_csum_set(inode, bh);
1143 error = ext4_handle_dirty_metadata(handle, NULL, bh);
1145 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
1154 ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1155 struct buffer_head *bh,
1156 struct ext4_xattr_entry *first, bool block_csum,
1157 struct ext4_xattr_inode_array **ea_inode_array,
1158 int extra_credits, bool skip_quota)
1160 struct inode *ea_inode;
1161 struct ext4_xattr_entry *entry;
1162 struct ext4_iloc iloc;
1164 unsigned int ea_ino;
1170 end = (void *)bh->b_data + bh->b_size;
1172 ext4_get_inode_loc(parent, &iloc);
1173 end = (void *)ext4_raw_inode(&iloc) + EXT4_SB(parent->i_sb)->s_inode_size;
1176 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1177 credits = 2 + extra_credits;
1179 for (entry = first; (void *)entry < end && !IS_LAST_ENTRY(entry);
1180 entry = EXT4_XATTR_NEXT(entry)) {
1181 if (!entry->e_value_inum)
1183 ea_ino = le32_to_cpu(entry->e_value_inum);
1184 err = ext4_xattr_inode_iget(parent, ea_ino,
1185 le32_to_cpu(entry->e_hash),
1190 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1192 ext4_warning_inode(ea_inode,
1193 "Expand inode array err=%d", err);
1198 err = ext4_journal_ensure_credits_fn(handle, credits, credits,
1199 ext4_free_metadata_revoke_credits(parent->i_sb, 1),
1200 ext4_xattr_restart_fn(handle, parent, bh, block_csum,
1203 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1208 err = ext4_journal_get_write_access(handle,
1209 parent->i_sb, bh, EXT4_JTR_NONE);
1211 ext4_warning_inode(ea_inode,
1212 "Re-get write access err=%d",
1218 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1220 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1226 ext4_xattr_inode_free_quota(parent, ea_inode,
1227 le32_to_cpu(entry->e_value_size));
1230 * Forget about ea_inode within the same transaction that
1231 * decrements the ref count. This avoids duplicate decrements in
1232 * case the rest of the work spills over to subsequent
1235 entry->e_value_inum = 0;
1236 entry->e_value_size = 0;
1243 * Note that we are deliberately skipping csum calculation for
1244 * the final update because we do not expect any journal
1245 * restarts until xattr block is freed.
1248 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1250 ext4_warning_inode(parent,
1251 "handle dirty metadata err=%d", err);
1256 * Release the xattr block BH: If the reference count is > 1, decrement it;
1257 * otherwise free the block.
1260 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
1261 struct buffer_head *bh,
1262 struct ext4_xattr_inode_array **ea_inode_array,
1265 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1269 BUFFER_TRACE(bh, "get_write_access");
1270 error = ext4_journal_get_write_access(handle, inode->i_sb, bh,
1277 hash = le32_to_cpu(BHDR(bh)->h_hash);
1278 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1280 ea_bdebug(bh, "refcount now=0; freeing");
1282 * This must happen under buffer lock for
1283 * ext4_xattr_block_set() to reliably detect freed block
1285 if (ea_block_cache) {
1286 struct mb_cache_entry *oe;
1288 oe = mb_cache_entry_delete_or_get(ea_block_cache, hash,
1292 mb_cache_entry_wait_unused(oe);
1293 mb_cache_entry_put(ea_block_cache, oe);
1300 if (ext4_has_feature_ea_inode(inode->i_sb))
1301 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1303 true /* block_csum */,
1306 true /* skip_quota */);
1307 ext4_free_blocks(handle, inode, bh, 0, 1,
1308 EXT4_FREE_BLOCKS_METADATA |
1309 EXT4_FREE_BLOCKS_FORGET);
1312 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1313 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1314 struct mb_cache_entry *ce;
1316 if (ea_block_cache) {
1317 ce = mb_cache_entry_get(ea_block_cache, hash,
1320 set_bit(MBE_REUSABLE_B, &ce->e_flags);
1321 mb_cache_entry_put(ea_block_cache, ce);
1326 ext4_xattr_block_csum_set(inode, bh);
1328 * Beware of this ugliness: Releasing of xattr block references
1329 * from different inodes can race and so we have to protect
1330 * from a race where someone else frees the block (and releases
1331 * its journal_head) before we are done dirtying the buffer. In
1332 * nojournal mode this race is harmless and we actually cannot
1333 * call ext4_handle_dirty_metadata() with locked buffer as
1334 * that function can call sync_dirty_buffer() so for that case
1335 * we handle the dirtying after unlocking the buffer.
1337 if (ext4_handle_valid(handle))
1338 error = ext4_handle_dirty_metadata(handle, inode, bh);
1340 if (!ext4_handle_valid(handle))
1341 error = ext4_handle_dirty_metadata(handle, inode, bh);
1343 ext4_handle_sync(handle);
1344 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
1345 ea_bdebug(bh, "refcount now=%d; releasing",
1346 le32_to_cpu(BHDR(bh)->h_refcount));
1349 ext4_std_error(inode->i_sb, error);
1354 * Find the available free space for EAs. This also returns the total number of
1355 * bytes used by EA entries.
1357 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1358 size_t *min_offs, void *base, int *total)
1360 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1361 if (!last->e_value_inum && last->e_value_size) {
1362 size_t offs = le16_to_cpu(last->e_value_offs);
1363 if (offs < *min_offs)
1367 *total += EXT4_XATTR_LEN(last->e_name_len);
1369 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1373 * Write the value of the EA in an inode.
1375 static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1376 const void *buf, int bufsize)
1378 struct buffer_head *bh = NULL;
1379 unsigned long block = 0;
1380 int blocksize = ea_inode->i_sb->s_blocksize;
1381 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
1382 int csize, wsize = 0;
1383 int ret = 0, ret2 = 0;
1387 while (ret >= 0 && ret < max_blocks) {
1388 struct ext4_map_blocks map;
1389 map.m_lblk = block += ret;
1390 map.m_len = max_blocks -= ret;
1392 ret = ext4_map_blocks(handle, ea_inode, &map,
1393 EXT4_GET_BLOCKS_CREATE);
1395 ext4_mark_inode_dirty(handle, ea_inode);
1396 if (ret == -ENOSPC &&
1397 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1409 while (wsize < bufsize) {
1411 csize = (bufsize - wsize) > blocksize ? blocksize :
1413 bh = ext4_getblk(handle, ea_inode, block, 0);
1418 EXT4_ERROR_INODE(ea_inode,
1419 "ext4_getblk() return bh = NULL");
1420 return -EFSCORRUPTED;
1422 ret = ext4_journal_get_write_access(handle, ea_inode->i_sb, bh,
1427 memcpy(bh->b_data, buf, csize);
1429 * Zero out block tail to avoid writing uninitialized memory
1432 if (csize < blocksize)
1433 memset(bh->b_data + csize, 0, blocksize - csize);
1434 set_buffer_uptodate(bh);
1435 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1442 inode_lock(ea_inode);
1443 i_size_write(ea_inode, wsize);
1444 ext4_update_i_disksize(ea_inode, wsize);
1445 inode_unlock(ea_inode);
1447 ret2 = ext4_mark_inode_dirty(handle, ea_inode);
1448 if (unlikely(ret2 && !ret))
1458 * Create an inode to store the value of a large EA.
1460 static struct inode *ext4_xattr_inode_create(handle_t *handle,
1461 struct inode *inode, u32 hash)
1463 struct inode *ea_inode = NULL;
1464 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
1467 if (inode->i_sb->s_root == NULL) {
1468 ext4_warning(inode->i_sb,
1469 "refuse to create EA inode when umounting");
1471 return ERR_PTR(-EINVAL);
1475 * Let the next inode be the goal, so we try and allocate the EA inode
1476 * in the same group, or nearby one.
1478 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
1479 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
1481 if (!IS_ERR(ea_inode)) {
1482 ea_inode->i_op = &ext4_file_inode_operations;
1483 ea_inode->i_fop = &ext4_file_operations;
1484 ext4_set_aops(ea_inode);
1485 ext4_xattr_inode_set_class(ea_inode);
1486 unlock_new_inode(ea_inode);
1487 ext4_xattr_inode_set_ref(ea_inode, 1);
1488 ext4_xattr_inode_set_hash(ea_inode, hash);
1489 err = ext4_mark_inode_dirty(handle, ea_inode);
1491 err = ext4_inode_attach_jinode(ea_inode);
1493 if (ext4_xattr_inode_dec_ref(handle, ea_inode))
1494 ext4_warning_inode(ea_inode,
1495 "cleanup dec ref error %d", err);
1497 return ERR_PTR(err);
1501 * Xattr inodes are shared therefore quota charging is performed
1502 * at a higher level.
1504 dquot_free_inode(ea_inode);
1505 dquot_drop(ea_inode);
1506 inode_lock(ea_inode);
1507 ea_inode->i_flags |= S_NOQUOTA;
1508 inode_unlock(ea_inode);
1514 static struct inode *
1515 ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1516 size_t value_len, u32 hash)
1518 struct inode *ea_inode;
1519 struct mb_cache_entry *ce;
1520 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1523 if (!ea_inode_cache)
1526 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1530 WARN_ON_ONCE(ext4_handle_valid(journal_current_handle()) &&
1531 !(current->flags & PF_MEMALLOC_NOFS));
1533 ea_data = kvmalloc(value_len, GFP_KERNEL);
1535 mb_cache_entry_put(ea_inode_cache, ce);
1540 ea_inode = ext4_iget(inode->i_sb, ce->e_value,
1541 EXT4_IGET_EA_INODE);
1542 if (IS_ERR(ea_inode))
1544 ext4_xattr_inode_set_class(ea_inode);
1545 if (i_size_read(ea_inode) == value_len &&
1546 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
1547 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1549 !memcmp(value, ea_data, value_len)) {
1550 mb_cache_entry_touch(ea_inode_cache, ce);
1551 mb_cache_entry_put(ea_inode_cache, ce);
1557 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1564 * Add value of the EA in an inode.
1566 static struct inode *ext4_xattr_inode_lookup_create(handle_t *handle,
1567 struct inode *inode, const void *value, size_t value_len)
1569 struct inode *ea_inode;
1573 /* Account inode & space to quota even if sharing... */
1574 err = ext4_xattr_inode_alloc_quota(inode, value_len);
1576 return ERR_PTR(err);
1578 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1579 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1581 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1587 /* Create an inode for the EA value */
1588 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
1589 if (IS_ERR(ea_inode)) {
1590 ext4_xattr_inode_free_quota(inode, NULL, value_len);
1594 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
1596 if (ext4_xattr_inode_dec_ref(handle, ea_inode))
1597 ext4_warning_inode(ea_inode, "cleanup dec ref error %d", err);
1601 if (EA_INODE_CACHE(inode))
1602 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1603 ea_inode->i_ino, true /* reusable */);
1607 ext4_xattr_inode_free_quota(inode, NULL, value_len);
1608 return ERR_PTR(err);
1612 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1613 * feature is enabled.
1615 #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1617 static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1618 struct ext4_xattr_search *s,
1619 handle_t *handle, struct inode *inode,
1620 struct inode *new_ea_inode,
1623 struct ext4_xattr_entry *last, *next;
1624 struct ext4_xattr_entry *here = s->here;
1625 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
1626 int in_inode = i->in_inode;
1627 struct inode *old_ea_inode = NULL;
1628 size_t old_size, new_size;
1631 /* Space used by old and new values. */
1632 old_size = (!s->not_found && !here->e_value_inum) ?
1633 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1634 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1637 * Optimization for the simple case when old and new values have the
1638 * same padded sizes. Not applicable if external inodes are involved.
1640 if (new_size && new_size == old_size) {
1641 size_t offs = le16_to_cpu(here->e_value_offs);
1642 void *val = s->base + offs;
1644 here->e_value_size = cpu_to_le32(i->value_len);
1645 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1646 memset(val, 0, new_size);
1648 memcpy(val, i->value, i->value_len);
1649 /* Clear padding bytes. */
1650 memset(val + i->value_len, 0, new_size - i->value_len);
1655 /* Compute min_offs and last. */
1657 for (; !IS_LAST_ENTRY(last); last = next) {
1658 next = EXT4_XATTR_NEXT(last);
1659 if ((void *)next >= s->end) {
1660 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
1661 ret = -EFSCORRUPTED;
1664 if (!last->e_value_inum && last->e_value_size) {
1665 size_t offs = le16_to_cpu(last->e_value_offs);
1666 if (offs < min_offs)
1671 /* Check whether we have enough space. */
1675 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1677 free += EXT4_XATTR_LEN(name_len) + old_size;
1679 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1685 * If storing the value in an external inode is an option,
1686 * reserve space for xattr entries/names in the external
1687 * attribute block so that a long value does not occupy the
1688 * whole space and prevent further entries being added.
1690 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1691 new_size && is_block &&
1692 (min_offs + old_size - new_size) <
1693 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1700 * Getting access to old and new ea inodes is subject to failures.
1701 * Finish that work before doing any modifications to the xattr data.
1703 if (!s->not_found && here->e_value_inum) {
1704 ret = ext4_xattr_inode_iget(inode,
1705 le32_to_cpu(here->e_value_inum),
1706 le32_to_cpu(here->e_hash),
1709 old_ea_inode = NULL;
1713 /* We are ready to release ref count on the old_ea_inode. */
1714 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1718 ext4_xattr_inode_free_quota(inode, old_ea_inode,
1719 le32_to_cpu(here->e_value_size));
1722 /* No failures allowed past this point. */
1724 if (!s->not_found && here->e_value_size && !here->e_value_inum) {
1725 /* Remove the old value. */
1726 void *first_val = s->base + min_offs;
1727 size_t offs = le16_to_cpu(here->e_value_offs);
1728 void *val = s->base + offs;
1730 memmove(first_val + old_size, first_val, val - first_val);
1731 memset(first_val, 0, old_size);
1732 min_offs += old_size;
1734 /* Adjust all value offsets. */
1736 while (!IS_LAST_ENTRY(last)) {
1737 size_t o = le16_to_cpu(last->e_value_offs);
1739 if (!last->e_value_inum &&
1740 last->e_value_size && o < offs)
1741 last->e_value_offs = cpu_to_le16(o + old_size);
1742 last = EXT4_XATTR_NEXT(last);
1747 /* Remove old name. */
1748 size_t size = EXT4_XATTR_LEN(name_len);
1750 last = ENTRY((void *)last - size);
1751 memmove(here, (void *)here + size,
1752 (void *)last - (void *)here + sizeof(__u32));
1753 memset(last, 0, size);
1756 * Update i_inline_off - moved ibody region might contain
1757 * system.data attribute. Handling a failure here won't
1758 * cause other complications for setting an xattr.
1760 if (!is_block && ext4_has_inline_data(inode)) {
1761 ret = ext4_find_inline_data_nolock(inode);
1763 ext4_warning_inode(inode,
1764 "unable to update i_inline_off");
1768 } else if (s->not_found) {
1769 /* Insert new name. */
1770 size_t size = EXT4_XATTR_LEN(name_len);
1771 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1773 memmove((void *)here + size, here, rest);
1774 memset(here, 0, size);
1775 here->e_name_index = i->name_index;
1776 here->e_name_len = name_len;
1777 memcpy(here->e_name, i->name, name_len);
1779 /* This is an update, reset value info. */
1780 here->e_value_inum = 0;
1781 here->e_value_offs = 0;
1782 here->e_value_size = 0;
1786 /* Insert new value. */
1788 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
1789 } else if (i->value_len) {
1790 void *val = s->base + min_offs - new_size;
1792 here->e_value_offs = cpu_to_le16(min_offs - new_size);
1793 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1794 memset(val, 0, new_size);
1796 memcpy(val, i->value, i->value_len);
1797 /* Clear padding bytes. */
1798 memset(val + i->value_len, 0,
1799 new_size - i->value_len);
1802 here->e_value_size = cpu_to_le32(i->value_len);
1809 /* Entry hash calculation. */
1814 * Feed crc32c hash instead of the raw value for entry
1815 * hash calculation. This is to avoid walking
1816 * potentially long value buffer again.
1818 crc32c_hash = cpu_to_le32(
1819 ext4_xattr_inode_get_hash(new_ea_inode));
1820 hash = ext4_xattr_hash_entry(here->e_name,
1823 } else if (is_block) {
1824 __le32 *value = s->base + le16_to_cpu(
1825 here->e_value_offs);
1827 hash = ext4_xattr_hash_entry(here->e_name,
1828 here->e_name_len, value,
1831 here->e_hash = hash;
1835 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1843 struct ext4_xattr_block_find {
1844 struct ext4_xattr_search s;
1845 struct buffer_head *bh;
1849 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1850 struct ext4_xattr_block_find *bs)
1852 struct super_block *sb = inode->i_sb;
1855 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1856 i->name_index, i->name, i->value, (long)i->value_len);
1858 if (EXT4_I(inode)->i_file_acl) {
1859 /* The inode already has an extended attribute block. */
1860 bs->bh = ext4_sb_bread(sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
1861 if (IS_ERR(bs->bh)) {
1862 error = PTR_ERR(bs->bh);
1866 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1867 atomic_read(&(bs->bh->b_count)),
1868 le32_to_cpu(BHDR(bs->bh)->h_refcount));
1869 error = ext4_xattr_check_block(inode, bs->bh);
1872 /* Find the named attribute. */
1873 bs->s.base = BHDR(bs->bh);
1874 bs->s.first = BFIRST(bs->bh);
1875 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1876 bs->s.here = bs->s.first;
1877 error = xattr_find_entry(inode, &bs->s.here, bs->s.end,
1878 i->name_index, i->name, 1);
1879 if (error && error != -ENODATA)
1881 bs->s.not_found = error;
1887 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1888 struct ext4_xattr_info *i,
1889 struct ext4_xattr_block_find *bs)
1891 struct super_block *sb = inode->i_sb;
1892 struct buffer_head *new_bh = NULL;
1893 struct ext4_xattr_search s_copy = bs->s;
1894 struct ext4_xattr_search *s = &s_copy;
1895 struct mb_cache_entry *ce = NULL;
1897 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1898 struct inode *ea_inode = NULL, *tmp_inode;
1899 size_t old_ea_inode_quota = 0;
1900 unsigned int ea_ino;
1902 #define header(x) ((struct ext4_xattr_header *)(x))
1904 /* If we need EA inode, prepare it before locking the buffer */
1905 if (i->value && i->in_inode) {
1906 WARN_ON_ONCE(!i->value_len);
1908 ea_inode = ext4_xattr_inode_lookup_create(handle, inode,
1909 i->value, i->value_len);
1910 if (IS_ERR(ea_inode)) {
1911 error = PTR_ERR(ea_inode);
1918 int offset = (char *)s->here - bs->bh->b_data;
1920 BUFFER_TRACE(bs->bh, "get_write_access");
1921 error = ext4_journal_get_write_access(handle, sb, bs->bh,
1926 lock_buffer(bs->bh);
1928 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
1929 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1932 * This must happen under buffer lock for
1933 * ext4_xattr_block_set() to reliably detect modified
1936 if (ea_block_cache) {
1937 struct mb_cache_entry *oe;
1939 oe = mb_cache_entry_delete_or_get(ea_block_cache,
1940 hash, bs->bh->b_blocknr);
1943 * Xattr block is getting reused. Leave
1946 mb_cache_entry_put(ea_block_cache, oe);
1950 ea_bdebug(bs->bh, "modifying in-place");
1951 error = ext4_xattr_set_entry(i, s, handle, inode,
1952 ea_inode, true /* is_block */);
1953 ext4_xattr_block_csum_set(inode, bs->bh);
1954 unlock_buffer(bs->bh);
1955 if (error == -EFSCORRUPTED)
1958 error = ext4_handle_dirty_metadata(handle,
1966 unlock_buffer(bs->bh);
1967 ea_bdebug(bs->bh, "cloning");
1968 s->base = kmemdup(BHDR(bs->bh), bs->bh->b_size, GFP_NOFS);
1970 if (s->base == NULL)
1972 s->first = ENTRY(header(s->base)+1);
1973 header(s->base)->h_refcount = cpu_to_le32(1);
1974 s->here = ENTRY(s->base + offset);
1975 s->end = s->base + bs->bh->b_size;
1978 * If existing entry points to an xattr inode, we need
1979 * to prevent ext4_xattr_set_entry() from decrementing
1980 * ref count on it because the reference belongs to the
1981 * original block. In this case, make the entry look
1982 * like it has an empty value.
1984 if (!s->not_found && s->here->e_value_inum) {
1985 ea_ino = le32_to_cpu(s->here->e_value_inum);
1986 error = ext4_xattr_inode_iget(inode, ea_ino,
1987 le32_to_cpu(s->here->e_hash),
1992 if (!ext4_test_inode_state(tmp_inode,
1993 EXT4_STATE_LUSTRE_EA_INODE)) {
1995 * Defer quota free call for previous
1996 * inode until success is guaranteed.
1998 old_ea_inode_quota = le32_to_cpu(
1999 s->here->e_value_size);
2003 s->here->e_value_inum = 0;
2004 s->here->e_value_size = 0;
2007 /* Allocate a buffer where we construct the new block. */
2008 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
2010 if (s->base == NULL)
2012 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2013 header(s->base)->h_blocks = cpu_to_le32(1);
2014 header(s->base)->h_refcount = cpu_to_le32(1);
2015 s->first = ENTRY(header(s->base)+1);
2016 s->here = ENTRY(header(s->base)+1);
2017 s->end = s->base + sb->s_blocksize;
2020 error = ext4_xattr_set_entry(i, s, handle, inode, ea_inode,
2021 true /* is_block */);
2022 if (error == -EFSCORRUPTED)
2028 if (!IS_LAST_ENTRY(s->first)) {
2029 new_bh = ext4_xattr_block_cache_find(inode, header(s->base), &ce);
2030 if (IS_ERR(new_bh)) {
2031 error = PTR_ERR(new_bh);
2037 /* We found an identical block in the cache. */
2038 if (new_bh == bs->bh)
2039 ea_bdebug(new_bh, "keeping");
2043 #ifdef EXT4_XATTR_DEBUG
2044 WARN_ON_ONCE(dquot_initialize_needed(inode));
2046 /* The old block is released after updating
2048 error = dquot_alloc_block(inode,
2049 EXT4_C2B(EXT4_SB(sb), 1));
2052 BUFFER_TRACE(new_bh, "get_write_access");
2053 error = ext4_journal_get_write_access(
2058 lock_buffer(new_bh);
2060 * We have to be careful about races with
2061 * adding references to xattr block. Once we
2062 * hold buffer lock xattr block's state is
2063 * stable so we can check the additional
2066 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2067 if (ref > EXT4_XATTR_REFCOUNT_MAX) {
2069 * Undo everything and check mbcache
2072 unlock_buffer(new_bh);
2073 dquot_free_block(inode,
2074 EXT4_C2B(EXT4_SB(sb),
2077 mb_cache_entry_put(ea_block_cache, ce);
2082 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2083 if (ref == EXT4_XATTR_REFCOUNT_MAX)
2084 clear_bit(MBE_REUSABLE_B, &ce->e_flags);
2085 ea_bdebug(new_bh, "reusing; refcount now=%d",
2087 ext4_xattr_block_csum_set(inode, new_bh);
2088 unlock_buffer(new_bh);
2089 error = ext4_handle_dirty_metadata(handle,
2095 mb_cache_entry_touch(ea_block_cache, ce);
2096 mb_cache_entry_put(ea_block_cache, ce);
2098 } else if (bs->bh && s->base == bs->bh->b_data) {
2099 /* We were modifying this block in-place. */
2100 ea_bdebug(bs->bh, "keeping this block");
2101 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
2105 /* We need to allocate a new block */
2106 ext4_fsblk_t goal, block;
2108 #ifdef EXT4_XATTR_DEBUG
2109 WARN_ON_ONCE(dquot_initialize_needed(inode));
2111 goal = ext4_group_first_block_no(sb,
2112 EXT4_I(inode)->i_block_group);
2113 block = ext4_new_meta_blocks(handle, inode, goal, 0,
2118 ea_idebug(inode, "creating block %llu",
2119 (unsigned long long)block);
2121 new_bh = sb_getblk(sb, block);
2122 if (unlikely(!new_bh)) {
2125 ext4_free_blocks(handle, inode, NULL, block, 1,
2126 EXT4_FREE_BLOCKS_METADATA);
2129 error = ext4_xattr_inode_inc_ref_all(handle, inode,
2130 ENTRY(header(s->base)+1));
2134 /* Drop the extra ref on ea_inode. */
2135 error = ext4_xattr_inode_dec_ref(handle,
2138 ext4_warning_inode(ea_inode,
2145 lock_buffer(new_bh);
2146 error = ext4_journal_get_create_access(handle, sb,
2147 new_bh, EXT4_JTR_NONE);
2149 unlock_buffer(new_bh);
2153 memcpy(new_bh->b_data, s->base, new_bh->b_size);
2154 ext4_xattr_block_csum_set(inode, new_bh);
2155 set_buffer_uptodate(new_bh);
2156 unlock_buffer(new_bh);
2157 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
2158 error = ext4_handle_dirty_metadata(handle, inode,
2165 if (old_ea_inode_quota)
2166 ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
2168 /* Update the inode. */
2169 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
2171 /* Drop the previous xattr block. */
2172 if (bs->bh && bs->bh != new_bh) {
2173 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2175 ext4_xattr_release_block(handle, inode, bs->bh,
2177 0 /* extra_credits */);
2178 ext4_xattr_inode_array_free(ea_inode_array);
2187 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2189 ext4_warning_inode(ea_inode, "dec ref error=%d",
2191 ext4_xattr_inode_free_quota(inode, ea_inode,
2192 i_size_read(ea_inode));
2197 mb_cache_entry_put(ea_block_cache, ce);
2199 if (!(bs->bh && s->base == bs->bh->b_data))
2205 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
2209 EXT4_ERROR_INODE(inode, "bad block %llu",
2210 EXT4_I(inode)->i_file_acl);
2216 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2217 struct ext4_xattr_ibody_find *is)
2219 struct ext4_xattr_ibody_header *header;
2220 struct ext4_inode *raw_inode;
2223 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2226 raw_inode = ext4_raw_inode(&is->iloc);
2227 header = IHDR(inode, raw_inode);
2228 is->s.base = is->s.first = IFIRST(header);
2229 is->s.here = is->s.first;
2230 is->s.end = ITAIL(inode, raw_inode);
2231 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2232 /* Find the named attribute. */
2233 error = xattr_find_entry(inode, &is->s.here, is->s.end,
2234 i->name_index, i->name, 0);
2235 if (error && error != -ENODATA)
2237 is->s.not_found = error;
2242 int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
2243 struct ext4_xattr_info *i,
2244 struct ext4_xattr_ibody_find *is)
2246 struct ext4_xattr_ibody_header *header;
2247 struct ext4_xattr_search *s = &is->s;
2248 struct inode *ea_inode = NULL;
2251 if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2254 /* If we need EA inode, prepare it before locking the buffer */
2255 if (i->value && i->in_inode) {
2256 WARN_ON_ONCE(!i->value_len);
2258 ea_inode = ext4_xattr_inode_lookup_create(handle, inode,
2259 i->value, i->value_len);
2260 if (IS_ERR(ea_inode))
2261 return PTR_ERR(ea_inode);
2263 error = ext4_xattr_set_entry(i, s, handle, inode, ea_inode,
2264 false /* is_block */);
2269 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2271 ext4_warning_inode(ea_inode, "dec ref error=%d",
2274 ext4_xattr_inode_free_quota(inode, ea_inode,
2275 i_size_read(ea_inode));
2280 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2281 if (!IS_LAST_ENTRY(s->first)) {
2282 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2283 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2285 header->h_magic = cpu_to_le32(0);
2286 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2292 static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2293 struct ext4_xattr_info *i)
2297 /* When e_value_inum is set the value is stored externally. */
2298 if (s->here->e_value_inum)
2300 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2302 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2303 return !memcmp(value, i->value, i->value_len);
2306 static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2308 struct buffer_head *bh;
2311 if (!EXT4_I(inode)->i_file_acl)
2313 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2316 error = ext4_xattr_check_block(inode, bh);
2319 return ERR_PTR(error);
2325 * ext4_xattr_set_handle()
2327 * Create, replace or remove an extended attribute for this inode. Value
2328 * is NULL to remove an existing extended attribute, and non-NULL to
2329 * either replace an existing extended attribute, or create a new extended
2330 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2331 * specify that an extended attribute must exist and must not exist
2332 * previous to the call, respectively.
2334 * Returns 0, or a negative error number on failure.
2337 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
2338 const char *name, const void *value, size_t value_len,
2341 struct ext4_xattr_info i = {
2342 .name_index = name_index,
2345 .value_len = value_len,
2348 struct ext4_xattr_ibody_find is = {
2349 .s = { .not_found = -ENODATA, },
2351 struct ext4_xattr_block_find bs = {
2352 .s = { .not_found = -ENODATA, },
2359 if (strlen(name) > 255)
2362 ext4_write_lock_xattr(inode, &no_expand);
2364 /* Check journal credits under write lock. */
2365 if (ext4_handle_valid(handle)) {
2366 struct buffer_head *bh;
2369 bh = ext4_xattr_get_block(inode);
2371 error = PTR_ERR(bh);
2375 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2377 flags & XATTR_CREATE);
2380 if (jbd2_handle_buffer_credits(handle) < credits) {
2384 WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
2387 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
2391 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
2392 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2393 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
2394 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
2397 error = ext4_xattr_ibody_find(inode, &i, &is);
2401 error = ext4_xattr_block_find(inode, &i, &bs);
2404 if (is.s.not_found && bs.s.not_found) {
2406 if (flags & XATTR_REPLACE)
2413 if (flags & XATTR_CREATE)
2418 if (!is.s.not_found)
2419 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2420 else if (!bs.s.not_found)
2421 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2424 /* Xattr value did not change? Save us some work and bail out */
2425 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2427 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2430 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2431 (EXT4_XATTR_SIZE(i.value_len) >
2432 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2435 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2436 if (!error && !bs.s.not_found) {
2438 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2439 } else if (error == -ENOSPC) {
2440 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2443 error = ext4_xattr_block_find(inode, &i, &bs);
2447 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2448 if (!error && !is.s.not_found) {
2450 error = ext4_xattr_ibody_set(handle, inode, &i,
2452 } else if (error == -ENOSPC) {
2454 * Xattr does not fit in the block, store at
2455 * external inode if possible.
2457 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2458 i.value_len && !i.in_inode) {
2466 ext4_xattr_update_super_block(handle, inode->i_sb);
2467 inode_set_ctime_current(inode);
2468 inode_inc_iversion(inode);
2471 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
2473 * The bh is consumed by ext4_mark_iloc_dirty, even with
2478 ext4_handle_sync(handle);
2480 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle);
2485 ext4_write_unlock_xattr(inode, &no_expand);
2489 int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2490 bool is_create, int *credits)
2492 struct buffer_head *bh;
2497 if (!EXT4_SB(inode->i_sb)->s_journal)
2500 down_read(&EXT4_I(inode)->xattr_sem);
2502 bh = ext4_xattr_get_block(inode);
2506 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2507 value_len, is_create);
2512 up_read(&EXT4_I(inode)->xattr_sem);
2519 * Like ext4_xattr_set_handle, but start from an inode. This extended
2520 * attribute modification is a filesystem transaction by itself.
2522 * Returns 0, or a negative error number on failure.
2525 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
2526 const void *value, size_t value_len, int flags)
2529 struct super_block *sb = inode->i_sb;
2530 int error, retries = 0;
2533 error = dquot_initialize(inode);
2538 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2543 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
2544 if (IS_ERR(handle)) {
2545 error = PTR_ERR(handle);
2549 error = ext4_xattr_set_handle(handle, inode, name_index, name,
2550 value, value_len, flags);
2551 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR,
2553 error2 = ext4_journal_stop(handle);
2554 if (error == -ENOSPC &&
2555 ext4_should_retry_alloc(sb, &retries))
2565 * Shift the EA entries in the inode to create space for the increased
2568 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2569 int value_offs_shift, void *to,
2570 void *from, size_t n)
2572 struct ext4_xattr_entry *last = entry;
2575 /* We always shift xattr headers further thus offsets get lower */
2576 BUG_ON(value_offs_shift > 0);
2578 /* Adjust the value offsets of the entries */
2579 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2580 if (!last->e_value_inum && last->e_value_size) {
2581 new_offs = le16_to_cpu(last->e_value_offs) +
2583 last->e_value_offs = cpu_to_le16(new_offs);
2586 /* Shift the entries by n bytes */
2587 memmove(to, from, n);
2591 * Move xattr pointed to by 'entry' from inode into external xattr block
2593 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2594 struct ext4_inode *raw_inode,
2595 struct ext4_xattr_entry *entry)
2597 struct ext4_xattr_ibody_find *is = NULL;
2598 struct ext4_xattr_block_find *bs = NULL;
2599 char *buffer = NULL, *b_entry_name = NULL;
2600 size_t value_size = le32_to_cpu(entry->e_value_size);
2601 struct ext4_xattr_info i = {
2604 .name_index = entry->e_name_index,
2605 .in_inode = !!entry->e_value_inum,
2607 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2608 int needs_kvfree = 0;
2611 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2612 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2613 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2614 if (!is || !bs || !b_entry_name) {
2619 is->s.not_found = -ENODATA;
2620 bs->s.not_found = -ENODATA;
2624 /* Save the entry name and the entry value */
2625 if (entry->e_value_inum) {
2626 buffer = kvmalloc(value_size, GFP_NOFS);
2632 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
2636 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2637 buffer = (void *)IFIRST(header) + value_offs;
2640 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2641 b_entry_name[entry->e_name_len] = '\0';
2642 i.name = b_entry_name;
2644 error = ext4_get_inode_loc(inode, &is->iloc);
2648 error = ext4_xattr_ibody_find(inode, &i, is);
2653 i.value_len = value_size;
2654 error = ext4_xattr_block_find(inode, &i, bs);
2658 /* Move ea entry from the inode into the block */
2659 error = ext4_xattr_block_set(handle, inode, &i, bs);
2663 /* Remove the chosen entry from the inode */
2666 error = ext4_xattr_ibody_set(handle, inode, &i, is);
2669 kfree(b_entry_name);
2670 if (needs_kvfree && buffer)
2673 brelse(is->iloc.bh);
2682 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2683 struct ext4_inode *raw_inode,
2684 int isize_diff, size_t ifree,
2685 size_t bfree, int *total_ino)
2687 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2688 struct ext4_xattr_entry *small_entry;
2689 struct ext4_xattr_entry *entry;
2690 struct ext4_xattr_entry *last;
2691 unsigned int entry_size; /* EA entry size */
2692 unsigned int total_size; /* EA entry size + value size */
2693 unsigned int min_total_size;
2696 while (isize_diff > ifree) {
2699 min_total_size = ~0U;
2700 last = IFIRST(header);
2701 /* Find the entry best suited to be pushed into EA block */
2702 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2703 /* never move system.data out of the inode */
2704 if ((last->e_name_len == 4) &&
2705 (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
2706 !memcmp(last->e_name, "data", 4))
2708 total_size = EXT4_XATTR_LEN(last->e_name_len);
2709 if (!last->e_value_inum)
2710 total_size += EXT4_XATTR_SIZE(
2711 le32_to_cpu(last->e_value_size));
2712 if (total_size <= bfree &&
2713 total_size < min_total_size) {
2714 if (total_size + ifree < isize_diff) {
2718 min_total_size = total_size;
2723 if (entry == NULL) {
2724 if (small_entry == NULL)
2726 entry = small_entry;
2729 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
2730 total_size = entry_size;
2731 if (!entry->e_value_inum)
2732 total_size += EXT4_XATTR_SIZE(
2733 le32_to_cpu(entry->e_value_size));
2734 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2739 *total_ino -= entry_size;
2740 ifree += total_size;
2741 bfree -= total_size;
2748 * Expand an inode by new_extra_isize bytes when EAs are present.
2749 * Returns 0 on success or negative error number on failure.
2751 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2752 struct ext4_inode *raw_inode, handle_t *handle)
2754 struct ext4_xattr_ibody_header *header;
2755 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2756 static unsigned int mnt_count;
2758 size_t ifree, bfree;
2761 int error = 0, tried_min_extra_isize = 0;
2762 int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
2763 int isize_diff; /* How much do we need to grow i_extra_isize */
2766 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
2767 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
2770 header = IHDR(inode, raw_inode);
2773 * Check if enough free space is available in the inode to shift the
2774 * entries ahead by new_extra_isize.
2777 base = IFIRST(header);
2778 end = ITAIL(inode, raw_inode);
2779 min_offs = end - base;
2780 total_ino = sizeof(struct ext4_xattr_ibody_header) + sizeof(u32);
2782 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
2783 if (ifree >= isize_diff)
2787 * Enough free space isn't available in the inode, check if
2788 * EA block can hold new_extra_isize bytes.
2790 if (EXT4_I(inode)->i_file_acl) {
2791 struct buffer_head *bh;
2793 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2795 error = PTR_ERR(bh);
2798 error = ext4_xattr_check_block(inode, bh);
2804 end = bh->b_data + bh->b_size;
2805 min_offs = end - base;
2806 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2809 if (bfree + ifree < isize_diff) {
2810 if (!tried_min_extra_isize && s_min_extra_isize) {
2811 tried_min_extra_isize++;
2812 new_extra_isize = s_min_extra_isize;
2819 bfree = inode->i_sb->s_blocksize;
2822 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2823 isize_diff, ifree, bfree,
2826 if (error == -ENOSPC && !tried_min_extra_isize &&
2827 s_min_extra_isize) {
2828 tried_min_extra_isize++;
2829 new_extra_isize = s_min_extra_isize;
2835 /* Adjust the offsets and shift the remaining entries ahead */
2836 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
2837 - new_extra_isize, (void *)raw_inode +
2838 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
2839 (void *)header, total_ino);
2840 EXT4_I(inode)->i_extra_isize = new_extra_isize;
2842 if (ext4_has_inline_data(inode))
2843 error = ext4_find_inline_data_nolock(inode);
2846 if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
2847 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2849 mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
2854 #define EIA_INCR 16 /* must be 2^n */
2855 #define EIA_MASK (EIA_INCR - 1)
2857 /* Add the large xattr @inode into @ea_inode_array for deferred iput().
2858 * If @ea_inode_array is new or full it will be grown and the old
2859 * contents copied over.
2862 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2863 struct inode *inode)
2865 if (*ea_inode_array == NULL) {
2867 * Start with 15 inodes, so it fits into a power-of-two size.
2869 (*ea_inode_array) = kmalloc(
2870 struct_size(*ea_inode_array, inodes, EIA_MASK),
2872 if (*ea_inode_array == NULL)
2874 (*ea_inode_array)->count = 0;
2875 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
2876 /* expand the array once all 15 + n * 16 slots are full */
2877 struct ext4_xattr_inode_array *new_array = NULL;
2879 new_array = kmalloc(
2880 struct_size(*ea_inode_array, inodes,
2881 (*ea_inode_array)->count + EIA_INCR),
2883 if (new_array == NULL)
2885 memcpy(new_array, *ea_inode_array,
2886 struct_size(*ea_inode_array, inodes,
2887 (*ea_inode_array)->count));
2888 kfree(*ea_inode_array);
2889 *ea_inode_array = new_array;
2891 (*ea_inode_array)->count++;
2892 (*ea_inode_array)->inodes[(*ea_inode_array)->count - 1] = inode;
2897 * ext4_xattr_delete_inode()
2899 * Free extended attribute resources associated with this inode. Traverse
2900 * all entries and decrement reference on any xattr inodes associated with this
2901 * inode. This is called immediately before an inode is freed. We have exclusive
2902 * access to the inode. If an orphan inode is deleted it will also release its
2903 * references on xattr block and xattr inodes.
2905 int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2906 struct ext4_xattr_inode_array **ea_inode_array,
2909 struct buffer_head *bh = NULL;
2910 struct ext4_xattr_ibody_header *header;
2911 struct ext4_iloc iloc = { .bh = NULL };
2912 struct ext4_xattr_entry *entry;
2913 struct inode *ea_inode;
2916 error = ext4_journal_ensure_credits(handle, extra_credits,
2917 ext4_free_metadata_revoke_credits(inode->i_sb, 1));
2919 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2923 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2924 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2926 error = ext4_get_inode_loc(inode, &iloc);
2928 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
2932 error = ext4_journal_get_write_access(handle, inode->i_sb,
2933 iloc.bh, EXT4_JTR_NONE);
2935 EXT4_ERROR_INODE(inode, "write access (error %d)",
2940 header = IHDR(inode, ext4_raw_inode(&iloc));
2941 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2942 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2944 false /* block_csum */,
2947 false /* skip_quota */);
2950 if (EXT4_I(inode)->i_file_acl) {
2951 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2953 error = PTR_ERR(bh);
2954 if (error == -EIO) {
2955 EXT4_ERROR_INODE_ERR(inode, EIO,
2956 "block %llu read error",
2957 EXT4_I(inode)->i_file_acl);
2962 error = ext4_xattr_check_block(inode, bh);
2966 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2967 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2968 entry = EXT4_XATTR_NEXT(entry)) {
2969 if (!entry->e_value_inum)
2971 error = ext4_xattr_inode_iget(inode,
2972 le32_to_cpu(entry->e_value_inum),
2973 le32_to_cpu(entry->e_hash),
2977 ext4_xattr_inode_free_quota(inode, ea_inode,
2978 le32_to_cpu(entry->e_value_size));
2984 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2987 * Update i_file_acl value in the same transaction that releases
2990 EXT4_I(inode)->i_file_acl = 0;
2991 error = ext4_mark_inode_dirty(handle, inode);
2993 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2997 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle);
3006 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
3010 if (ea_inode_array == NULL)
3013 for (idx = 0; idx < ea_inode_array->count; ++idx)
3014 iput(ea_inode_array->inodes[idx]);
3015 kfree(ea_inode_array);
3019 * ext4_xattr_block_cache_insert()
3021 * Create a new entry in the extended attribute block cache, and insert
3022 * it unless such an entry is already in the cache.
3025 ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
3026 struct buffer_head *bh)
3028 struct ext4_xattr_header *header = BHDR(bh);
3029 __u32 hash = le32_to_cpu(header->h_hash);
3030 int reusable = le32_to_cpu(header->h_refcount) <
3031 EXT4_XATTR_REFCOUNT_MAX;
3034 if (!ea_block_cache)
3036 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
3037 bh->b_blocknr, reusable);
3039 if (error == -EBUSY)
3040 ea_bdebug(bh, "already in cache");
3042 ea_bdebug(bh, "inserting [%x]", (int)hash);
3048 * Compare two extended attribute blocks for equality.
3050 * Returns 0 if the blocks are equal, 1 if they differ.
3053 ext4_xattr_cmp(struct ext4_xattr_header *header1,
3054 struct ext4_xattr_header *header2)
3056 struct ext4_xattr_entry *entry1, *entry2;
3058 entry1 = ENTRY(header1+1);
3059 entry2 = ENTRY(header2+1);
3060 while (!IS_LAST_ENTRY(entry1)) {
3061 if (IS_LAST_ENTRY(entry2))
3063 if (entry1->e_hash != entry2->e_hash ||
3064 entry1->e_name_index != entry2->e_name_index ||
3065 entry1->e_name_len != entry2->e_name_len ||
3066 entry1->e_value_size != entry2->e_value_size ||
3067 entry1->e_value_inum != entry2->e_value_inum ||
3068 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
3070 if (!entry1->e_value_inum &&
3071 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
3072 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
3073 le32_to_cpu(entry1->e_value_size)))
3076 entry1 = EXT4_XATTR_NEXT(entry1);
3077 entry2 = EXT4_XATTR_NEXT(entry2);
3079 if (!IS_LAST_ENTRY(entry2))
3085 * ext4_xattr_block_cache_find()
3087 * Find an identical extended attribute block.
3089 * Returns a pointer to the block found, or NULL if such a block was not
3090 * found, or an error pointer if an error occurred while reading ea block.
3092 static struct buffer_head *
3093 ext4_xattr_block_cache_find(struct inode *inode,
3094 struct ext4_xattr_header *header,
3095 struct mb_cache_entry **pce)
3097 __u32 hash = le32_to_cpu(header->h_hash);
3098 struct mb_cache_entry *ce;
3099 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
3101 if (!ea_block_cache)
3103 if (!header->h_hash)
3104 return NULL; /* never share */
3105 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
3106 ce = mb_cache_entry_find_first(ea_block_cache, hash);
3108 struct buffer_head *bh;
3110 bh = ext4_sb_bread(inode->i_sb, ce->e_value, REQ_PRIO);
3112 if (PTR_ERR(bh) != -ENOMEM)
3113 EXT4_ERROR_INODE(inode, "block %lu read error",
3114 (unsigned long)ce->e_value);
3115 mb_cache_entry_put(ea_block_cache, ce);
3117 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
3122 ce = mb_cache_entry_find_next(ea_block_cache, ce);
3127 #define NAME_HASH_SHIFT 5
3128 #define VALUE_HASH_SHIFT 16
3131 * ext4_xattr_hash_entry()
3133 * Compute the hash of an extended attribute.
3135 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3140 while (name_len--) {
3141 hash = (hash << NAME_HASH_SHIFT) ^
3142 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3143 (unsigned char)*name++;
3145 while (value_count--) {
3146 hash = (hash << VALUE_HASH_SHIFT) ^
3147 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3148 le32_to_cpu(*value++);
3150 return cpu_to_le32(hash);
3154 * ext4_xattr_hash_entry_signed()
3156 * Compute the hash of an extended attribute incorrectly.
3158 static __le32 ext4_xattr_hash_entry_signed(char *name, size_t name_len, __le32 *value, size_t value_count)
3162 while (name_len--) {
3163 hash = (hash << NAME_HASH_SHIFT) ^
3164 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3165 (signed char)*name++;
3167 while (value_count--) {
3168 hash = (hash << VALUE_HASH_SHIFT) ^
3169 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3170 le32_to_cpu(*value++);
3172 return cpu_to_le32(hash);
3175 #undef NAME_HASH_SHIFT
3176 #undef VALUE_HASH_SHIFT
3178 #define BLOCK_HASH_SHIFT 16
3181 * ext4_xattr_rehash()
3183 * Re-compute the extended attribute hash value after an entry has changed.
3185 static void ext4_xattr_rehash(struct ext4_xattr_header *header)
3187 struct ext4_xattr_entry *here;
3190 here = ENTRY(header+1);
3191 while (!IS_LAST_ENTRY(here)) {
3192 if (!here->e_hash) {
3193 /* Block is not shared if an entry's hash value == 0 */
3197 hash = (hash << BLOCK_HASH_SHIFT) ^
3198 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3199 le32_to_cpu(here->e_hash);
3200 here = EXT4_XATTR_NEXT(here);
3202 header->h_hash = cpu_to_le32(hash);
3205 #undef BLOCK_HASH_SHIFT
3207 #define HASH_BUCKET_BITS 10
3210 ext4_xattr_create_cache(void)
3212 return mb_cache_create(HASH_BUCKET_BITS);
3215 void ext4_xattr_destroy_cache(struct mb_cache *cache)
3218 mb_cache_destroy(cache);