2 * linux/fs/ext4/xattr.c
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
8 * Extended attributes for symlinks and special files added per
9 * suggestion of Luka Renko <luka.renko@hermes.si>.
10 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
12 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
13 * and Andreas Gruenbacher <agruen@suse.de>.
17 * Extended attributes are stored directly in inodes (on file systems with
18 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
19 * field contains the block number if an inode uses an additional block. All
20 * attributes must fit in the inode and one additional block. Blocks that
21 * contain the identical set of attributes may be shared among several inodes.
22 * Identical blocks are detected by keeping a cache of blocks that have
23 * recently been accessed.
25 * The attributes in inodes and on blocks have a different header; the entries
26 * are stored in the same format:
28 * +------------------+
31 * | entry 2 | | growing downwards
36 * | value 3 | | growing upwards
38 * +------------------+
40 * The header is followed by multiple entry descriptors. In disk blocks, the
41 * entry descriptors are kept sorted. In inodes, they are unsorted. The
42 * attribute values are aligned to the end of the block in no specific order.
46 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
47 * EA blocks are only changed if they are exclusive to an inode, so
48 * holding xattr_sem also means that nothing but the EA block's reference
49 * count can change. Multiple writers to the same block are synchronized
53 #include <linux/init.h>
55 #include <linux/slab.h>
56 #include <linux/mbcache.h>
57 #include <linux/quotaops.h>
58 #include "ext4_jbd2.h"
63 #ifdef EXT4_XATTR_DEBUG
64 # define ea_idebug(inode, fmt, ...) \
65 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
66 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
67 # define ea_bdebug(bh, fmt, ...) \
68 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
69 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
71 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
72 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
75 static void ext4_xattr_block_cache_insert(struct mb_cache *,
76 struct buffer_head *);
77 static struct buffer_head *
78 ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
79 struct mb_cache_entry **);
80 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
82 static void ext4_xattr_rehash(struct ext4_xattr_header *);
84 static const struct xattr_handler * const ext4_xattr_handler_map[] = {
85 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
86 #ifdef CONFIG_EXT4_FS_POSIX_ACL
87 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
88 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
90 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
91 #ifdef CONFIG_EXT4_FS_SECURITY
92 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
96 const struct xattr_handler *ext4_xattr_handlers[] = {
97 &ext4_xattr_user_handler,
98 &ext4_xattr_trusted_handler,
99 #ifdef CONFIG_EXT4_FS_POSIX_ACL
100 &posix_acl_access_xattr_handler,
101 &posix_acl_default_xattr_handler,
103 #ifdef CONFIG_EXT4_FS_SECURITY
104 &ext4_xattr_security_handler,
109 #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
110 inode->i_sb->s_fs_info)->s_ea_block_cache)
112 #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
113 inode->i_sb->s_fs_info)->s_ea_inode_cache)
116 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
117 struct inode *inode);
119 #ifdef CONFIG_LOCKDEP
120 void ext4_xattr_inode_set_class(struct inode *ea_inode)
122 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
126 static __le32 ext4_xattr_block_csum(struct inode *inode,
128 struct ext4_xattr_header *hdr)
130 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
132 __le64 dsk_block_nr = cpu_to_le64(block_nr);
133 __u32 dummy_csum = 0;
134 int offset = offsetof(struct ext4_xattr_header, h_checksum);
136 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
137 sizeof(dsk_block_nr));
138 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
139 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
140 offset += sizeof(dummy_csum);
141 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
142 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
144 return cpu_to_le32(csum);
147 static int ext4_xattr_block_csum_verify(struct inode *inode,
148 struct buffer_head *bh)
150 struct ext4_xattr_header *hdr = BHDR(bh);
153 if (ext4_has_metadata_csum(inode->i_sb)) {
155 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
156 bh->b_blocknr, hdr));
162 static void ext4_xattr_block_csum_set(struct inode *inode,
163 struct buffer_head *bh)
165 if (ext4_has_metadata_csum(inode->i_sb))
166 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
167 bh->b_blocknr, BHDR(bh));
170 static inline const struct xattr_handler *
171 ext4_xattr_handler(int name_index)
173 const struct xattr_handler *handler = NULL;
175 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
176 handler = ext4_xattr_handler_map[name_index];
181 ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
184 struct ext4_xattr_entry *e = entry;
186 /* Find the end of the names list */
187 while (!IS_LAST_ENTRY(e)) {
188 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
189 if ((void *)next >= end)
190 return -EFSCORRUPTED;
194 /* Check the values */
195 while (!IS_LAST_ENTRY(entry)) {
196 if (entry->e_value_size != 0 &&
197 entry->e_value_inum == 0) {
198 u16 offs = le16_to_cpu(entry->e_value_offs);
199 u32 size = le32_to_cpu(entry->e_value_size);
203 * The value cannot overlap the names, and the value
204 * with padding cannot extend beyond 'end'. Check both
205 * the padded and unpadded sizes, since the size may
206 * overflow to 0 when adding padding.
208 if (offs > end - value_start)
209 return -EFSCORRUPTED;
210 value = value_start + offs;
211 if (value < (void *)e + sizeof(u32) ||
212 size > end - value ||
213 EXT4_XATTR_SIZE(size) > end - value)
214 return -EFSCORRUPTED;
216 entry = EXT4_XATTR_NEXT(entry);
223 ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
227 if (buffer_verified(bh))
230 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
231 BHDR(bh)->h_blocks != cpu_to_le32(1))
232 return -EFSCORRUPTED;
233 if (!ext4_xattr_block_csum_verify(inode, bh))
235 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
238 set_buffer_verified(bh);
243 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
244 void *end, const char *function, unsigned int line)
246 int error = -EFSCORRUPTED;
248 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
249 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
251 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
254 __ext4_error_inode(inode, function, line, 0,
255 "corrupted in-inode xattr");
259 #define xattr_check_inode(inode, header, end) \
260 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
263 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
264 const char *name, int sorted)
266 struct ext4_xattr_entry *entry;
272 name_len = strlen(name);
274 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
275 cmp = name_index - entry->e_name_index;
277 cmp = name_len - entry->e_name_len;
279 cmp = memcmp(name, entry->e_name, name_len);
280 if (cmp <= 0 && (sorted || cmp == 0))
284 return cmp ? -ENODATA : 0;
288 ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
290 return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
293 static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
295 return ((u64)ea_inode->i_ctime.tv_sec << 32) |
296 ((u32)ea_inode->i_version);
299 static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
301 ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
302 ea_inode->i_version = (u32)ref_count;
305 static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
307 return (u32)ea_inode->i_atime.tv_sec;
310 static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
312 ea_inode->i_atime.tv_sec = hash;
316 * Read the EA value from an inode.
318 static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
320 unsigned long block = 0;
321 struct buffer_head *bh;
322 int blocksize = ea_inode->i_sb->s_blocksize;
323 size_t csize, copied = 0;
324 void *copy_pos = buf;
326 while (copied < size) {
327 csize = (size - copied) > blocksize ? blocksize : size - copied;
328 bh = ext4_bread(NULL, ea_inode, block, 0);
332 return -EFSCORRUPTED;
334 memcpy(copy_pos, bh->b_data, csize);
344 static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
345 struct inode **ea_inode)
350 inode = ext4_iget(parent->i_sb, ea_ino);
352 err = PTR_ERR(inode);
353 ext4_error(parent->i_sb,
354 "error while reading EA inode %lu err=%d", ea_ino,
359 if (is_bad_inode(inode)) {
360 ext4_error(parent->i_sb,
361 "error while reading EA inode %lu is_bad_inode",
367 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
368 ext4_error(parent->i_sb,
369 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
383 ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
384 struct ext4_xattr_entry *entry, void *buffer,
389 /* Verify stored hash matches calculated hash. */
390 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
391 if (hash != ext4_xattr_inode_get_hash(ea_inode))
392 return -EFSCORRUPTED;
395 __le32 e_hash, tmp_data;
397 /* Verify entry hash. */
398 tmp_data = cpu_to_le32(hash);
399 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
401 if (e_hash != entry->e_hash)
402 return -EFSCORRUPTED;
407 #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
410 * Read xattr value from the EA inode.
413 ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
414 void *buffer, size_t size)
416 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
417 struct inode *ea_inode;
420 err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
427 if (i_size_read(ea_inode) != size) {
428 ext4_warning_inode(ea_inode,
429 "ea_inode file size=%llu entry size=%zu",
430 i_size_read(ea_inode), size);
435 err = ext4_xattr_inode_read(ea_inode, buffer, size);
439 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer, size);
441 * Compatibility check for old Lustre ea_inode implementation. Old
442 * version does not have hash validation, but it has a backpointer
443 * from ea_inode to the parent inode.
445 if (err == -EFSCORRUPTED) {
446 if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != inode->i_ino ||
447 ea_inode->i_generation != inode->i_generation) {
448 ext4_warning_inode(ea_inode,
449 "EA inode hash validation failed");
452 /* Do not add ea_inode to the cache. */
453 ea_inode_cache = NULL;
459 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
460 ext4_xattr_inode_get_hash(ea_inode),
461 ea_inode->i_ino, true /* reusable */);
468 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
469 void *buffer, size_t buffer_size)
471 struct buffer_head *bh = NULL;
472 struct ext4_xattr_entry *entry;
475 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
477 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
478 name_index, name, buffer, (long)buffer_size);
481 if (!EXT4_I(inode)->i_file_acl)
483 ea_idebug(inode, "reading block %llu",
484 (unsigned long long)EXT4_I(inode)->i_file_acl);
485 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
488 ea_bdebug(bh, "b_count=%d, refcount=%d",
489 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
490 if (ext4_xattr_check_block(inode, bh)) {
491 EXT4_ERROR_INODE(inode, "bad block %llu",
492 EXT4_I(inode)->i_file_acl);
493 error = -EFSCORRUPTED;
496 ext4_xattr_block_cache_insert(ea_block_cache, bh);
498 error = ext4_xattr_find_entry(&entry, name_index, name, 1);
501 size = le32_to_cpu(entry->e_value_size);
504 if (size > buffer_size)
506 if (entry->e_value_inum) {
507 error = ext4_xattr_inode_get(inode, entry, buffer,
512 memcpy(buffer, bh->b_data +
513 le16_to_cpu(entry->e_value_offs), size);
524 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
525 void *buffer, size_t buffer_size)
527 struct ext4_xattr_ibody_header *header;
528 struct ext4_xattr_entry *entry;
529 struct ext4_inode *raw_inode;
530 struct ext4_iloc iloc;
535 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
537 error = ext4_get_inode_loc(inode, &iloc);
540 raw_inode = ext4_raw_inode(&iloc);
541 header = IHDR(inode, raw_inode);
542 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
543 error = xattr_check_inode(inode, header, end);
546 entry = IFIRST(header);
547 error = ext4_xattr_find_entry(&entry, name_index, name, 0);
550 size = le32_to_cpu(entry->e_value_size);
553 if (size > buffer_size)
555 if (entry->e_value_inum) {
556 error = ext4_xattr_inode_get(inode, entry, buffer,
561 memcpy(buffer, (void *)IFIRST(header) +
562 le16_to_cpu(entry->e_value_offs), size);
575 * Copy an extended attribute into the buffer
576 * provided, or compute the buffer size required.
577 * Buffer is NULL to compute the size of the buffer required.
579 * Returns a negative error number on failure, or the number of bytes
580 * used / required on success.
583 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
584 void *buffer, size_t buffer_size)
588 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
591 if (strlen(name) > 255)
594 down_read(&EXT4_I(inode)->xattr_sem);
595 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
597 if (error == -ENODATA)
598 error = ext4_xattr_block_get(inode, name_index, name, buffer,
600 up_read(&EXT4_I(inode)->xattr_sem);
605 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
606 char *buffer, size_t buffer_size)
608 size_t rest = buffer_size;
610 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
611 const struct xattr_handler *handler =
612 ext4_xattr_handler(entry->e_name_index);
614 if (handler && (!handler->list || handler->list(dentry))) {
615 const char *prefix = handler->prefix ?: handler->name;
616 size_t prefix_len = strlen(prefix);
617 size_t size = prefix_len + entry->e_name_len + 1;
622 memcpy(buffer, prefix, prefix_len);
623 buffer += prefix_len;
624 memcpy(buffer, entry->e_name, entry->e_name_len);
625 buffer += entry->e_name_len;
631 return buffer_size - rest; /* total size */
635 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
637 struct inode *inode = d_inode(dentry);
638 struct buffer_head *bh = NULL;
641 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
642 buffer, (long)buffer_size);
645 if (!EXT4_I(inode)->i_file_acl)
647 ea_idebug(inode, "reading block %llu",
648 (unsigned long long)EXT4_I(inode)->i_file_acl);
649 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
653 ea_bdebug(bh, "b_count=%d, refcount=%d",
654 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
655 if (ext4_xattr_check_block(inode, bh)) {
656 EXT4_ERROR_INODE(inode, "bad block %llu",
657 EXT4_I(inode)->i_file_acl);
658 error = -EFSCORRUPTED;
661 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
662 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
671 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
673 struct inode *inode = d_inode(dentry);
674 struct ext4_xattr_ibody_header *header;
675 struct ext4_inode *raw_inode;
676 struct ext4_iloc iloc;
680 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
682 error = ext4_get_inode_loc(inode, &iloc);
685 raw_inode = ext4_raw_inode(&iloc);
686 header = IHDR(inode, raw_inode);
687 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
688 error = xattr_check_inode(inode, header, end);
691 error = ext4_xattr_list_entries(dentry, IFIRST(header),
692 buffer, buffer_size);
700 * Inode operation listxattr()
702 * d_inode(dentry)->i_rwsem: don't care
704 * Copy a list of attribute names into the buffer
705 * provided, or compute the buffer size required.
706 * Buffer is NULL to compute the size of the buffer required.
708 * Returns a negative error number on failure, or the number of bytes
709 * used / required on success.
712 ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
716 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
717 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
724 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
729 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
734 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
737 static void ext4_xattr_update_super_block(handle_t *handle,
738 struct super_block *sb)
740 if (ext4_has_feature_xattr(sb))
743 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
744 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
745 ext4_set_feature_xattr(sb);
746 ext4_handle_dirty_super(handle, sb);
750 int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
752 struct ext4_iloc iloc = { .bh = NULL };
753 struct buffer_head *bh = NULL;
754 struct ext4_inode *raw_inode;
755 struct ext4_xattr_ibody_header *header;
756 struct ext4_xattr_entry *entry;
757 qsize_t ea_inode_refs = 0;
761 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
763 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
764 ret = ext4_get_inode_loc(inode, &iloc);
767 raw_inode = ext4_raw_inode(&iloc);
768 header = IHDR(inode, raw_inode);
769 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
770 ret = xattr_check_inode(inode, header, end);
774 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
775 entry = EXT4_XATTR_NEXT(entry))
776 if (entry->e_value_inum)
780 if (EXT4_I(inode)->i_file_acl) {
781 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
787 if (ext4_xattr_check_block(inode, bh)) {
792 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
793 entry = EXT4_XATTR_NEXT(entry))
794 if (entry->e_value_inum)
797 *usage = ea_inode_refs + 1;
805 static inline size_t round_up_cluster(struct inode *inode, size_t length)
807 struct super_block *sb = inode->i_sb;
808 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
810 size_t mask = ~(cluster_size - 1);
812 return (length + cluster_size - 1) & mask;
815 static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
819 err = dquot_alloc_inode(inode);
822 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
824 dquot_free_inode(inode);
828 static void ext4_xattr_inode_free_quota(struct inode *inode, size_t len)
830 dquot_free_space_nodirty(inode, round_up_cluster(inode, len));
831 dquot_free_inode(inode);
834 int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
835 struct buffer_head *block_bh, size_t value_len,
842 * 1) Owner inode update
843 * 2) Ref count update on old xattr block
845 * 4) block bitmap update for new xattr block
846 * 5) group descriptor for new xattr block
847 * 6) block bitmap update for old xattr block
848 * 7) group descriptor for old block
850 * 6 & 7 can happen if we have two racing threads T_a and T_b
851 * which are each trying to set an xattr on inodes I_a and I_b
852 * which were both initially sharing an xattr block.
857 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
860 * In case of inline data, we may push out the data to a block,
861 * so we need to reserve credits for this eventuality
863 if (inode && ext4_has_inline_data(inode))
864 credits += ext4_writepage_trans_blocks(inode) + 1;
866 /* We are done if ea_inode feature is not enabled. */
867 if (!ext4_has_feature_ea_inode(sb))
870 /* New ea_inode, inode map, block bitmap, group descriptor. */
874 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
876 /* Indirection block or one level of extent tree. */
879 /* Block bitmap and group descriptor updates for each block. */
880 credits += blocks * 2;
882 /* Blocks themselves. */
886 /* Dereference ea_inode holding old xattr value.
887 * Old ea_inode, inode map, block bitmap, group descriptor.
891 /* Data blocks for old ea_inode. */
892 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
894 /* Indirection block or one level of extent tree for old
899 /* Block bitmap and group descriptor updates for each block. */
900 credits += blocks * 2;
903 /* We may need to clone the existing xattr block in which case we need
904 * to increment ref counts for existing ea_inodes referenced by it.
907 struct ext4_xattr_entry *entry = BFIRST(block_bh);
909 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
910 if (entry->e_value_inum)
911 /* Ref count update on ea_inode. */
917 static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
918 int credits, struct buffer_head *bh,
919 bool dirty, bool block_csum)
923 if (!ext4_handle_valid(handle))
926 if (handle->h_buffer_credits >= credits)
929 error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
933 ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
939 ext4_xattr_block_csum_set(inode, bh);
940 error = ext4_handle_dirty_metadata(handle, NULL, bh);
942 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
948 error = ext4_journal_restart(handle, credits);
950 ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
955 error = ext4_journal_get_write_access(handle, bh);
957 ext4_warning(inode->i_sb,
958 "Get write access failed (error %d)",
966 static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
969 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
970 struct ext4_iloc iloc;
975 inode_lock(ea_inode);
977 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
983 ref_count = ext4_xattr_inode_get_ref(ea_inode);
984 ref_count += ref_change;
985 ext4_xattr_inode_set_ref(ea_inode, ref_count);
987 if (ref_change > 0) {
988 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
989 ea_inode->i_ino, ref_count);
991 if (ref_count == 1) {
992 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
993 ea_inode->i_ino, ea_inode->i_nlink);
995 set_nlink(ea_inode, 1);
996 ext4_orphan_del(handle, ea_inode);
998 if (ea_inode_cache) {
999 hash = ext4_xattr_inode_get_hash(ea_inode);
1000 mb_cache_entry_create(ea_inode_cache,
1003 true /* reusable */);
1007 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1008 ea_inode->i_ino, ref_count);
1010 if (ref_count == 0) {
1011 WARN_ONCE(ea_inode->i_nlink != 1,
1012 "EA inode %lu i_nlink=%u",
1013 ea_inode->i_ino, ea_inode->i_nlink);
1015 clear_nlink(ea_inode);
1016 ext4_orphan_add(handle, ea_inode);
1018 if (ea_inode_cache) {
1019 hash = ext4_xattr_inode_get_hash(ea_inode);
1020 mb_cache_entry_delete(ea_inode_cache, hash,
1026 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1029 ext4_warning_inode(ea_inode,
1030 "ext4_mark_iloc_dirty() failed ret=%d", ret);
1033 inode_unlock(ea_inode);
1037 static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1039 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1042 static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1044 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1047 static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1048 struct ext4_xattr_entry *first)
1050 struct inode *ea_inode;
1051 struct ext4_xattr_entry *entry;
1052 struct ext4_xattr_entry *failed_entry;
1053 unsigned int ea_ino;
1056 for (entry = first; !IS_LAST_ENTRY(entry);
1057 entry = EXT4_XATTR_NEXT(entry)) {
1058 if (!entry->e_value_inum)
1060 ea_ino = le32_to_cpu(entry->e_value_inum);
1061 err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
1064 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1066 ext4_warning_inode(ea_inode, "inc ref error %d", err);
1076 failed_entry = entry;
1078 for (entry = first; entry != failed_entry;
1079 entry = EXT4_XATTR_NEXT(entry)) {
1080 if (!entry->e_value_inum)
1082 ea_ino = le32_to_cpu(entry->e_value_inum);
1083 err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
1085 ext4_warning(parent->i_sb,
1086 "cleanup ea_ino %u iget error %d", ea_ino,
1090 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1092 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1100 ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1101 struct buffer_head *bh,
1102 struct ext4_xattr_entry *first, bool block_csum,
1103 struct ext4_xattr_inode_array **ea_inode_array,
1104 int extra_credits, bool skip_quota)
1106 struct inode *ea_inode;
1107 struct ext4_xattr_entry *entry;
1109 unsigned int ea_ino;
1113 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1114 credits = 2 + extra_credits;
1116 for (entry = first; !IS_LAST_ENTRY(entry);
1117 entry = EXT4_XATTR_NEXT(entry)) {
1118 if (!entry->e_value_inum)
1120 ea_ino = le32_to_cpu(entry->e_value_inum);
1121 err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
1125 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1127 ext4_warning_inode(ea_inode,
1128 "Expand inode array err=%d", err);
1133 err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
1136 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1141 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1143 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1149 ext4_xattr_inode_free_quota(parent,
1150 le32_to_cpu(entry->e_value_size));
1153 * Forget about ea_inode within the same transaction that
1154 * decrements the ref count. This avoids duplicate decrements in
1155 * case the rest of the work spills over to subsequent
1158 entry->e_value_inum = 0;
1159 entry->e_value_size = 0;
1166 * Note that we are deliberately skipping csum calculation for
1167 * the final update because we do not expect any journal
1168 * restarts until xattr block is freed.
1171 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1173 ext4_warning_inode(parent,
1174 "handle dirty metadata err=%d", err);
1179 * Release the xattr block BH: If the reference count is > 1, decrement it;
1180 * otherwise free the block.
1183 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
1184 struct buffer_head *bh,
1185 struct ext4_xattr_inode_array **ea_inode_array,
1188 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1192 BUFFER_TRACE(bh, "get_write_access");
1193 error = ext4_journal_get_write_access(handle, bh);
1198 hash = le32_to_cpu(BHDR(bh)->h_hash);
1199 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1201 ea_bdebug(bh, "refcount now=0; freeing");
1203 * This must happen under buffer lock for
1204 * ext4_xattr_block_set() to reliably detect freed block
1207 mb_cache_entry_delete(ea_block_cache, hash,
1212 if (ext4_has_feature_ea_inode(inode->i_sb))
1213 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1215 true /* block_csum */,
1218 true /* skip_quota */);
1219 ext4_free_blocks(handle, inode, bh, 0, 1,
1220 EXT4_FREE_BLOCKS_METADATA |
1221 EXT4_FREE_BLOCKS_FORGET);
1224 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1225 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1226 struct mb_cache_entry *ce;
1228 if (ea_block_cache) {
1229 ce = mb_cache_entry_get(ea_block_cache, hash,
1233 mb_cache_entry_put(ea_block_cache, ce);
1238 ext4_xattr_block_csum_set(inode, bh);
1240 * Beware of this ugliness: Releasing of xattr block references
1241 * from different inodes can race and so we have to protect
1242 * from a race where someone else frees the block (and releases
1243 * its journal_head) before we are done dirtying the buffer. In
1244 * nojournal mode this race is harmless and we actually cannot
1245 * call ext4_handle_dirty_metadata() with locked buffer as
1246 * that function can call sync_dirty_buffer() so for that case
1247 * we handle the dirtying after unlocking the buffer.
1249 if (ext4_handle_valid(handle))
1250 error = ext4_handle_dirty_metadata(handle, inode, bh);
1252 if (!ext4_handle_valid(handle))
1253 error = ext4_handle_dirty_metadata(handle, inode, bh);
1255 ext4_handle_sync(handle);
1256 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
1257 ea_bdebug(bh, "refcount now=%d; releasing",
1258 le32_to_cpu(BHDR(bh)->h_refcount));
1261 ext4_std_error(inode->i_sb, error);
1266 * Find the available free space for EAs. This also returns the total number of
1267 * bytes used by EA entries.
1269 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1270 size_t *min_offs, void *base, int *total)
1272 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1273 if (!last->e_value_inum && last->e_value_size) {
1274 size_t offs = le16_to_cpu(last->e_value_offs);
1275 if (offs < *min_offs)
1279 *total += EXT4_XATTR_LEN(last->e_name_len);
1281 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1285 * Write the value of the EA in an inode.
1287 static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1288 const void *buf, int bufsize)
1290 struct buffer_head *bh = NULL;
1291 unsigned long block = 0;
1292 int blocksize = ea_inode->i_sb->s_blocksize;
1293 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
1294 int csize, wsize = 0;
1299 while (ret >= 0 && ret < max_blocks) {
1300 struct ext4_map_blocks map;
1301 map.m_lblk = block += ret;
1302 map.m_len = max_blocks -= ret;
1304 ret = ext4_map_blocks(handle, ea_inode, &map,
1305 EXT4_GET_BLOCKS_CREATE);
1307 ext4_mark_inode_dirty(handle, ea_inode);
1308 if (ret == -ENOSPC &&
1309 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1321 while (wsize < bufsize) {
1324 csize = (bufsize - wsize) > blocksize ? blocksize :
1326 bh = ext4_getblk(handle, ea_inode, block, 0);
1329 ret = ext4_journal_get_write_access(handle, bh);
1333 memcpy(bh->b_data, buf, csize);
1334 set_buffer_uptodate(bh);
1335 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1342 inode_lock(ea_inode);
1343 i_size_write(ea_inode, wsize);
1344 ext4_update_i_disksize(ea_inode, wsize);
1345 inode_unlock(ea_inode);
1347 ext4_mark_inode_dirty(handle, ea_inode);
1356 * Create an inode to store the value of a large EA.
1358 static struct inode *ext4_xattr_inode_create(handle_t *handle,
1359 struct inode *inode, u32 hash)
1361 struct inode *ea_inode = NULL;
1362 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
1366 * Let the next inode be the goal, so we try and allocate the EA inode
1367 * in the same group, or nearby one.
1369 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
1370 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
1372 if (!IS_ERR(ea_inode)) {
1373 ea_inode->i_op = &ext4_file_inode_operations;
1374 ea_inode->i_fop = &ext4_file_operations;
1375 ext4_set_aops(ea_inode);
1376 ext4_xattr_inode_set_class(ea_inode);
1377 unlock_new_inode(ea_inode);
1378 ext4_xattr_inode_set_ref(ea_inode, 1);
1379 ext4_xattr_inode_set_hash(ea_inode, hash);
1380 err = ext4_mark_inode_dirty(handle, ea_inode);
1382 err = ext4_inode_attach_jinode(ea_inode);
1385 return ERR_PTR(err);
1389 * Xattr inodes are shared therefore quota charging is performed
1390 * at a higher level.
1392 dquot_free_inode(ea_inode);
1393 dquot_drop(ea_inode);
1394 inode_lock(ea_inode);
1395 ea_inode->i_flags |= S_NOQUOTA;
1396 inode_unlock(ea_inode);
1402 static struct inode *
1403 ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1404 size_t value_len, u32 hash)
1406 struct inode *ea_inode;
1407 struct mb_cache_entry *ce;
1408 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1411 if (!ea_inode_cache)
1414 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1418 ea_data = ext4_kvmalloc(value_len, GFP_NOFS);
1420 mb_cache_entry_put(ea_inode_cache, ce);
1425 ea_inode = ext4_iget(inode->i_sb, ce->e_value);
1426 if (!IS_ERR(ea_inode) &&
1427 !is_bad_inode(ea_inode) &&
1428 (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
1429 i_size_read(ea_inode) == value_len &&
1430 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
1431 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1433 !memcmp(value, ea_data, value_len)) {
1434 mb_cache_entry_touch(ea_inode_cache, ce);
1435 mb_cache_entry_put(ea_inode_cache, ce);
1440 if (!IS_ERR(ea_inode))
1442 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1449 * Add value of the EA in an inode.
1451 static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1452 const void *value, size_t value_len,
1453 struct inode **ret_inode)
1455 struct inode *ea_inode;
1459 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1460 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1462 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1468 *ret_inode = ea_inode;
1472 /* Create an inode for the EA value */
1473 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
1474 if (IS_ERR(ea_inode))
1475 return PTR_ERR(ea_inode);
1477 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
1479 ext4_xattr_inode_dec_ref(handle, ea_inode);
1484 if (EA_INODE_CACHE(inode))
1485 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1486 ea_inode->i_ino, true /* reusable */);
1488 *ret_inode = ea_inode;
1493 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1494 * feature is enabled.
1496 #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1498 static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1499 struct ext4_xattr_search *s,
1500 handle_t *handle, struct inode *inode,
1503 struct ext4_xattr_entry *last;
1504 struct ext4_xattr_entry *here = s->here;
1505 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
1506 int in_inode = i->in_inode;
1507 struct inode *old_ea_inode = NULL;
1508 struct inode *new_ea_inode = NULL;
1509 size_t old_size, new_size;
1512 /* Space used by old and new values. */
1513 old_size = (!s->not_found && !here->e_value_inum) ?
1514 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1515 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1518 * Optimization for the simple case when old and new values have the
1519 * same padded sizes. Not applicable if external inodes are involved.
1521 if (new_size && new_size == old_size) {
1522 size_t offs = le16_to_cpu(here->e_value_offs);
1523 void *val = s->base + offs;
1525 here->e_value_size = cpu_to_le32(i->value_len);
1526 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1527 memset(val, 0, new_size);
1529 memcpy(val, i->value, i->value_len);
1530 /* Clear padding bytes. */
1531 memset(val + i->value_len, 0, new_size - i->value_len);
1536 /* Compute min_offs and last. */
1538 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1539 if (!last->e_value_inum && last->e_value_size) {
1540 size_t offs = le16_to_cpu(last->e_value_offs);
1541 if (offs < min_offs)
1546 /* Check whether we have enough space. */
1550 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1552 free += EXT4_XATTR_LEN(name_len) + old_size;
1554 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1560 * If storing the value in an external inode is an option,
1561 * reserve space for xattr entries/names in the external
1562 * attribute block so that a long value does not occupy the
1563 * whole space and prevent futher entries being added.
1565 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1566 new_size && is_block &&
1567 (min_offs + old_size - new_size) <
1568 EXT4_XATTR_BLOCK_RESERVE(inode)) {
1575 * Getting access to old and new ea inodes is subject to failures.
1576 * Finish that work before doing any modifications to the xattr data.
1578 if (!s->not_found && here->e_value_inum) {
1579 ret = ext4_xattr_inode_iget(inode,
1580 le32_to_cpu(here->e_value_inum),
1583 old_ea_inode = NULL;
1587 if (i->value && in_inode) {
1588 WARN_ON_ONCE(!i->value_len);
1590 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1594 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1598 new_ea_inode = NULL;
1599 ext4_xattr_inode_free_quota(inode, i->value_len);
1605 /* We are ready to release ref count on the old_ea_inode. */
1606 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1608 /* Release newly required ref count on new_ea_inode. */
1612 err = ext4_xattr_inode_dec_ref(handle,
1615 ext4_warning_inode(new_ea_inode,
1616 "dec ref new_ea_inode err=%d",
1618 ext4_xattr_inode_free_quota(inode,
1624 ext4_xattr_inode_free_quota(inode,
1625 le32_to_cpu(here->e_value_size));
1628 /* No failures allowed past this point. */
1630 if (!s->not_found && here->e_value_offs) {
1631 /* Remove the old value. */
1632 void *first_val = s->base + min_offs;
1633 size_t offs = le16_to_cpu(here->e_value_offs);
1634 void *val = s->base + offs;
1636 memmove(first_val + old_size, first_val, val - first_val);
1637 memset(first_val, 0, old_size);
1638 min_offs += old_size;
1640 /* Adjust all value offsets. */
1642 while (!IS_LAST_ENTRY(last)) {
1643 size_t o = le16_to_cpu(last->e_value_offs);
1645 if (!last->e_value_inum &&
1646 last->e_value_size && o < offs)
1647 last->e_value_offs = cpu_to_le16(o + old_size);
1648 last = EXT4_XATTR_NEXT(last);
1653 /* Remove old name. */
1654 size_t size = EXT4_XATTR_LEN(name_len);
1656 last = ENTRY((void *)last - size);
1657 memmove(here, (void *)here + size,
1658 (void *)last - (void *)here + sizeof(__u32));
1659 memset(last, 0, size);
1660 } else if (s->not_found) {
1661 /* Insert new name. */
1662 size_t size = EXT4_XATTR_LEN(name_len);
1663 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1665 memmove((void *)here + size, here, rest);
1666 memset(here, 0, size);
1667 here->e_name_index = i->name_index;
1668 here->e_name_len = name_len;
1669 memcpy(here->e_name, i->name, name_len);
1671 /* This is an update, reset value info. */
1672 here->e_value_inum = 0;
1673 here->e_value_offs = 0;
1674 here->e_value_size = 0;
1678 /* Insert new value. */
1680 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
1681 } else if (i->value_len) {
1682 void *val = s->base + min_offs - new_size;
1684 here->e_value_offs = cpu_to_le16(min_offs - new_size);
1685 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1686 memset(val, 0, new_size);
1688 memcpy(val, i->value, i->value_len);
1689 /* Clear padding bytes. */
1690 memset(val + i->value_len, 0,
1691 new_size - i->value_len);
1694 here->e_value_size = cpu_to_le32(i->value_len);
1700 /* Entry hash calculation. */
1705 * Feed crc32c hash instead of the raw value for entry
1706 * hash calculation. This is to avoid walking
1707 * potentially long value buffer again.
1709 crc32c_hash = cpu_to_le32(
1710 ext4_xattr_inode_get_hash(new_ea_inode));
1711 hash = ext4_xattr_hash_entry(here->e_name,
1714 } else if (is_block) {
1715 __le32 *value = s->base + min_offs - new_size;
1717 hash = ext4_xattr_hash_entry(here->e_name,
1718 here->e_name_len, value,
1721 here->e_hash = hash;
1725 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1734 struct ext4_xattr_block_find {
1735 struct ext4_xattr_search s;
1736 struct buffer_head *bh;
1740 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1741 struct ext4_xattr_block_find *bs)
1743 struct super_block *sb = inode->i_sb;
1746 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1747 i->name_index, i->name, i->value, (long)i->value_len);
1749 if (EXT4_I(inode)->i_file_acl) {
1750 /* The inode already has an extended attribute block. */
1751 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
1755 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1756 atomic_read(&(bs->bh->b_count)),
1757 le32_to_cpu(BHDR(bs->bh)->h_refcount));
1758 if (ext4_xattr_check_block(inode, bs->bh)) {
1759 EXT4_ERROR_INODE(inode, "bad block %llu",
1760 EXT4_I(inode)->i_file_acl);
1761 error = -EFSCORRUPTED;
1764 /* Find the named attribute. */
1765 bs->s.base = BHDR(bs->bh);
1766 bs->s.first = BFIRST(bs->bh);
1767 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1768 bs->s.here = bs->s.first;
1769 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
1771 if (error && error != -ENODATA)
1773 bs->s.not_found = error;
1782 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1783 struct ext4_xattr_info *i,
1784 struct ext4_xattr_block_find *bs)
1786 struct super_block *sb = inode->i_sb;
1787 struct buffer_head *new_bh = NULL;
1788 struct ext4_xattr_search s_copy = bs->s;
1789 struct ext4_xattr_search *s = &s_copy;
1790 struct mb_cache_entry *ce = NULL;
1792 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1793 struct inode *ea_inode = NULL;
1794 size_t old_ea_inode_size = 0;
1796 #define header(x) ((struct ext4_xattr_header *)(x))
1799 BUFFER_TRACE(bs->bh, "get_write_access");
1800 error = ext4_journal_get_write_access(handle, bs->bh);
1803 lock_buffer(bs->bh);
1805 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
1806 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1809 * This must happen under buffer lock for
1810 * ext4_xattr_block_set() to reliably detect modified
1814 mb_cache_entry_delete(ea_block_cache, hash,
1816 ea_bdebug(bs->bh, "modifying in-place");
1817 error = ext4_xattr_set_entry(i, s, handle, inode,
1818 true /* is_block */);
1819 ext4_xattr_block_csum_set(inode, bs->bh);
1820 unlock_buffer(bs->bh);
1821 if (error == -EFSCORRUPTED)
1824 error = ext4_handle_dirty_metadata(handle,
1831 int offset = (char *)s->here - bs->bh->b_data;
1833 unlock_buffer(bs->bh);
1834 ea_bdebug(bs->bh, "cloning");
1835 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
1837 if (s->base == NULL)
1839 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1840 s->first = ENTRY(header(s->base)+1);
1841 header(s->base)->h_refcount = cpu_to_le32(1);
1842 s->here = ENTRY(s->base + offset);
1843 s->end = s->base + bs->bh->b_size;
1846 * If existing entry points to an xattr inode, we need
1847 * to prevent ext4_xattr_set_entry() from decrementing
1848 * ref count on it because the reference belongs to the
1849 * original block. In this case, make the entry look
1850 * like it has an empty value.
1852 if (!s->not_found && s->here->e_value_inum) {
1854 * Defer quota free call for previous inode
1855 * until success is guaranteed.
1857 old_ea_inode_size = le32_to_cpu(
1858 s->here->e_value_size);
1859 s->here->e_value_inum = 0;
1860 s->here->e_value_size = 0;
1864 /* Allocate a buffer where we construct the new block. */
1865 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
1866 /* assert(header == s->base) */
1868 if (s->base == NULL)
1870 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1871 header(s->base)->h_blocks = cpu_to_le32(1);
1872 header(s->base)->h_refcount = cpu_to_le32(1);
1873 s->first = ENTRY(header(s->base)+1);
1874 s->here = ENTRY(header(s->base)+1);
1875 s->end = s->base + sb->s_blocksize;
1878 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
1879 if (error == -EFSCORRUPTED)
1884 if (i->value && s->here->e_value_inum) {
1885 unsigned int ea_ino;
1888 * A ref count on ea_inode has been taken as part of the call to
1889 * ext4_xattr_set_entry() above. We would like to drop this
1890 * extra ref but we have to wait until the xattr block is
1891 * initialized and has its own ref count on the ea_inode.
1893 ea_ino = le32_to_cpu(s->here->e_value_inum);
1894 error = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode);
1902 if (!IS_LAST_ENTRY(s->first)) {
1903 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
1906 /* We found an identical block in the cache. */
1907 if (new_bh == bs->bh)
1908 ea_bdebug(new_bh, "keeping");
1912 WARN_ON_ONCE(dquot_initialize_needed(inode));
1914 /* The old block is released after updating
1916 error = dquot_alloc_block(inode,
1917 EXT4_C2B(EXT4_SB(sb), 1));
1920 BUFFER_TRACE(new_bh, "get_write_access");
1921 error = ext4_journal_get_write_access(handle,
1925 lock_buffer(new_bh);
1927 * We have to be careful about races with
1928 * freeing, rehashing or adding references to
1929 * xattr block. Once we hold buffer lock xattr
1930 * block's state is stable so we can check
1931 * whether the block got freed / rehashed or
1932 * not. Since we unhash mbcache entry under
1933 * buffer lock when freeing / rehashing xattr
1934 * block, checking whether entry is still
1935 * hashed is reliable. Same rules hold for
1936 * e_reusable handling.
1938 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1941 * Undo everything and check mbcache
1944 unlock_buffer(new_bh);
1945 dquot_free_block(inode,
1946 EXT4_C2B(EXT4_SB(sb),
1949 mb_cache_entry_put(ea_block_cache, ce);
1954 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
1955 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
1956 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
1958 ea_bdebug(new_bh, "reusing; refcount now=%d",
1960 ext4_xattr_block_csum_set(inode, new_bh);
1961 unlock_buffer(new_bh);
1962 error = ext4_handle_dirty_metadata(handle,
1968 mb_cache_entry_touch(ea_block_cache, ce);
1969 mb_cache_entry_put(ea_block_cache, ce);
1971 } else if (bs->bh && s->base == bs->bh->b_data) {
1972 /* We were modifying this block in-place. */
1973 ea_bdebug(bs->bh, "keeping this block");
1974 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
1978 /* We need to allocate a new block */
1979 ext4_fsblk_t goal, block;
1981 WARN_ON_ONCE(dquot_initialize_needed(inode));
1983 goal = ext4_group_first_block_no(sb,
1984 EXT4_I(inode)->i_block_group);
1986 /* non-extent files can't have physical blocks past 2^32 */
1987 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
1988 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
1990 block = ext4_new_meta_blocks(handle, inode, goal, 0,
1995 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
1996 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
1998 ea_idebug(inode, "creating block %llu",
1999 (unsigned long long)block);
2001 new_bh = sb_getblk(sb, block);
2002 if (unlikely(!new_bh)) {
2005 ext4_free_blocks(handle, inode, NULL, block, 1,
2006 EXT4_FREE_BLOCKS_METADATA);
2009 error = ext4_xattr_inode_inc_ref_all(handle, inode,
2010 ENTRY(header(s->base)+1));
2014 /* Drop the extra ref on ea_inode. */
2015 error = ext4_xattr_inode_dec_ref(handle,
2018 ext4_warning_inode(ea_inode,
2025 lock_buffer(new_bh);
2026 error = ext4_journal_get_create_access(handle, new_bh);
2028 unlock_buffer(new_bh);
2032 memcpy(new_bh->b_data, s->base, new_bh->b_size);
2033 ext4_xattr_block_csum_set(inode, new_bh);
2034 set_buffer_uptodate(new_bh);
2035 unlock_buffer(new_bh);
2036 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
2037 error = ext4_handle_dirty_metadata(handle, inode,
2044 if (old_ea_inode_size)
2045 ext4_xattr_inode_free_quota(inode, old_ea_inode_size);
2047 /* Update the inode. */
2048 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
2050 /* Drop the previous xattr block. */
2051 if (bs->bh && bs->bh != new_bh) {
2052 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2054 ext4_xattr_release_block(handle, inode, bs->bh,
2056 0 /* extra_credits */);
2057 ext4_xattr_inode_array_free(ea_inode_array);
2065 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2067 ext4_warning_inode(ea_inode, "dec ref error=%d",
2070 /* If there was an error, revert the quota charge. */
2072 ext4_xattr_inode_free_quota(inode,
2073 i_size_read(ea_inode));
2077 mb_cache_entry_put(ea_block_cache, ce);
2079 if (!(bs->bh && s->base == bs->bh->b_data))
2085 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
2089 EXT4_ERROR_INODE(inode, "bad block %llu",
2090 EXT4_I(inode)->i_file_acl);
2096 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2097 struct ext4_xattr_ibody_find *is)
2099 struct ext4_xattr_ibody_header *header;
2100 struct ext4_inode *raw_inode;
2103 if (EXT4_I(inode)->i_extra_isize == 0)
2105 raw_inode = ext4_raw_inode(&is->iloc);
2106 header = IHDR(inode, raw_inode);
2107 is->s.base = is->s.first = IFIRST(header);
2108 is->s.here = is->s.first;
2109 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2110 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2111 error = xattr_check_inode(inode, header, is->s.end);
2114 /* Find the named attribute. */
2115 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
2117 if (error && error != -ENODATA)
2119 is->s.not_found = error;
2124 int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
2125 struct ext4_xattr_info *i,
2126 struct ext4_xattr_ibody_find *is)
2128 struct ext4_xattr_ibody_header *header;
2129 struct ext4_xattr_search *s = &is->s;
2132 if (EXT4_I(inode)->i_extra_isize == 0)
2134 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
2136 if (error == -ENOSPC &&
2137 ext4_has_inline_data(inode)) {
2138 error = ext4_try_to_evict_inline_data(handle, inode,
2139 EXT4_XATTR_LEN(strlen(i->name) +
2140 EXT4_XATTR_SIZE(i->value_len)));
2143 error = ext4_xattr_ibody_find(inode, i, is);
2146 error = ext4_xattr_set_entry(i, s, handle, inode,
2147 false /* is_block */);
2152 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2153 if (!IS_LAST_ENTRY(s->first)) {
2154 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2155 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2157 header->h_magic = cpu_to_le32(0);
2158 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2163 static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
2164 struct ext4_xattr_info *i,
2165 struct ext4_xattr_ibody_find *is)
2167 struct ext4_xattr_ibody_header *header;
2168 struct ext4_xattr_search *s = &is->s;
2171 if (EXT4_I(inode)->i_extra_isize == 0)
2173 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
2176 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2177 if (!IS_LAST_ENTRY(s->first)) {
2178 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2179 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2181 header->h_magic = cpu_to_le32(0);
2182 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2187 static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2188 struct ext4_xattr_info *i)
2192 /* When e_value_inum is set the value is stored externally. */
2193 if (s->here->e_value_inum)
2195 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2197 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2198 return !memcmp(value, i->value, i->value_len);
2201 static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2203 struct buffer_head *bh;
2206 if (!EXT4_I(inode)->i_file_acl)
2208 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2210 return ERR_PTR(-EIO);
2211 error = ext4_xattr_check_block(inode, bh);
2213 return ERR_PTR(error);
2218 * ext4_xattr_set_handle()
2220 * Create, replace or remove an extended attribute for this inode. Value
2221 * is NULL to remove an existing extended attribute, and non-NULL to
2222 * either replace an existing extended attribute, or create a new extended
2223 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2224 * specify that an extended attribute must exist and must not exist
2225 * previous to the call, respectively.
2227 * Returns 0, or a negative error number on failure.
2230 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
2231 const char *name, const void *value, size_t value_len,
2234 struct ext4_xattr_info i = {
2235 .name_index = name_index,
2238 .value_len = value_len,
2241 struct ext4_xattr_ibody_find is = {
2242 .s = { .not_found = -ENODATA, },
2244 struct ext4_xattr_block_find bs = {
2245 .s = { .not_found = -ENODATA, },
2252 if (strlen(name) > 255)
2255 ext4_write_lock_xattr(inode, &no_expand);
2257 /* Check journal credits under write lock. */
2258 if (ext4_handle_valid(handle)) {
2259 struct buffer_head *bh;
2262 bh = ext4_xattr_get_block(inode);
2264 error = PTR_ERR(bh);
2268 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2270 flags & XATTR_CREATE);
2273 if (!ext4_handle_has_enough_credits(handle, credits)) {
2279 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
2283 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
2284 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2285 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
2286 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
2289 error = ext4_xattr_ibody_find(inode, &i, &is);
2293 error = ext4_xattr_block_find(inode, &i, &bs);
2296 if (is.s.not_found && bs.s.not_found) {
2298 if (flags & XATTR_REPLACE)
2305 if (flags & XATTR_CREATE)
2310 if (!is.s.not_found)
2311 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2312 else if (!bs.s.not_found)
2313 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2316 /* Xattr value did not change? Save us some work and bail out */
2317 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2319 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2322 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2323 (EXT4_XATTR_SIZE(i.value_len) >
2324 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2327 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2328 if (!error && !bs.s.not_found) {
2330 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2331 } else if (error == -ENOSPC) {
2332 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2333 error = ext4_xattr_block_find(inode, &i, &bs);
2337 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2338 if (!error && !is.s.not_found) {
2340 error = ext4_xattr_ibody_set(handle, inode, &i,
2342 } else if (error == -ENOSPC) {
2344 * Xattr does not fit in the block, store at
2345 * external inode if possible.
2347 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2356 ext4_xattr_update_super_block(handle, inode->i_sb);
2357 inode->i_ctime = current_time(inode);
2360 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
2362 * The bh is consumed by ext4_mark_iloc_dirty, even with
2367 ext4_handle_sync(handle);
2373 ext4_write_unlock_xattr(inode, &no_expand);
2377 int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2378 bool is_create, int *credits)
2380 struct buffer_head *bh;
2385 if (!EXT4_SB(inode->i_sb)->s_journal)
2388 down_read(&EXT4_I(inode)->xattr_sem);
2390 bh = ext4_xattr_get_block(inode);
2394 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2395 value_len, is_create);
2400 up_read(&EXT4_I(inode)->xattr_sem);
2407 * Like ext4_xattr_set_handle, but start from an inode. This extended
2408 * attribute modification is a filesystem transaction by itself.
2410 * Returns 0, or a negative error number on failure.
2413 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
2414 const void *value, size_t value_len, int flags)
2417 struct super_block *sb = inode->i_sb;
2418 int error, retries = 0;
2421 error = dquot_initialize(inode);
2426 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2431 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
2432 if (IS_ERR(handle)) {
2433 error = PTR_ERR(handle);
2437 error = ext4_xattr_set_handle(handle, inode, name_index, name,
2438 value, value_len, flags);
2439 error2 = ext4_journal_stop(handle);
2440 if (error == -ENOSPC &&
2441 ext4_should_retry_alloc(sb, &retries))
2451 * Shift the EA entries in the inode to create space for the increased
2454 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2455 int value_offs_shift, void *to,
2456 void *from, size_t n)
2458 struct ext4_xattr_entry *last = entry;
2461 /* We always shift xattr headers further thus offsets get lower */
2462 BUG_ON(value_offs_shift > 0);
2464 /* Adjust the value offsets of the entries */
2465 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2466 if (!last->e_value_inum && last->e_value_size) {
2467 new_offs = le16_to_cpu(last->e_value_offs) +
2469 last->e_value_offs = cpu_to_le16(new_offs);
2472 /* Shift the entries by n bytes */
2473 memmove(to, from, n);
2477 * Move xattr pointed to by 'entry' from inode into external xattr block
2479 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2480 struct ext4_inode *raw_inode,
2481 struct ext4_xattr_entry *entry)
2483 struct ext4_xattr_ibody_find *is = NULL;
2484 struct ext4_xattr_block_find *bs = NULL;
2485 char *buffer = NULL, *b_entry_name = NULL;
2486 size_t value_size = le32_to_cpu(entry->e_value_size);
2487 struct ext4_xattr_info i = {
2490 .name_index = entry->e_name_index,
2491 .in_inode = !!entry->e_value_inum,
2493 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2496 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2497 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2498 buffer = kmalloc(value_size, GFP_NOFS);
2499 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2500 if (!is || !bs || !buffer || !b_entry_name) {
2505 is->s.not_found = -ENODATA;
2506 bs->s.not_found = -ENODATA;
2510 /* Save the entry name and the entry value */
2511 if (entry->e_value_inum) {
2512 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
2516 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2517 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
2520 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2521 b_entry_name[entry->e_name_len] = '\0';
2522 i.name = b_entry_name;
2524 error = ext4_get_inode_loc(inode, &is->iloc);
2528 error = ext4_xattr_ibody_find(inode, &i, is);
2532 /* Remove the chosen entry from the inode */
2533 error = ext4_xattr_ibody_set(handle, inode, &i, is);
2538 i.value_len = value_size;
2539 error = ext4_xattr_block_find(inode, &i, bs);
2543 /* Add entry which was removed from the inode into the block */
2544 error = ext4_xattr_block_set(handle, inode, &i, bs);
2549 kfree(b_entry_name);
2552 brelse(is->iloc.bh);
2559 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2560 struct ext4_inode *raw_inode,
2561 int isize_diff, size_t ifree,
2562 size_t bfree, int *total_ino)
2564 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2565 struct ext4_xattr_entry *small_entry;
2566 struct ext4_xattr_entry *entry;
2567 struct ext4_xattr_entry *last;
2568 unsigned int entry_size; /* EA entry size */
2569 unsigned int total_size; /* EA entry size + value size */
2570 unsigned int min_total_size;
2573 while (isize_diff > ifree) {
2576 min_total_size = ~0U;
2577 last = IFIRST(header);
2578 /* Find the entry best suited to be pushed into EA block */
2579 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2580 total_size = EXT4_XATTR_LEN(last->e_name_len);
2581 if (!last->e_value_inum)
2582 total_size += EXT4_XATTR_SIZE(
2583 le32_to_cpu(last->e_value_size));
2584 if (total_size <= bfree &&
2585 total_size < min_total_size) {
2586 if (total_size + ifree < isize_diff) {
2590 min_total_size = total_size;
2595 if (entry == NULL) {
2596 if (small_entry == NULL)
2598 entry = small_entry;
2601 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
2602 total_size = entry_size;
2603 if (!entry->e_value_inum)
2604 total_size += EXT4_XATTR_SIZE(
2605 le32_to_cpu(entry->e_value_size));
2606 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2611 *total_ino -= entry_size;
2612 ifree += total_size;
2613 bfree -= total_size;
2620 * Expand an inode by new_extra_isize bytes when EAs are present.
2621 * Returns 0 on success or negative error number on failure.
2623 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2624 struct ext4_inode *raw_inode, handle_t *handle)
2626 struct ext4_xattr_ibody_header *header;
2627 struct buffer_head *bh = NULL;
2629 size_t ifree, bfree;
2632 int error = 0, tried_min_extra_isize = 0;
2633 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
2634 int isize_diff; /* How much do we need to grow i_extra_isize */
2637 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
2641 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
2642 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
2645 header = IHDR(inode, raw_inode);
2648 * Check if enough free space is available in the inode to shift the
2649 * entries ahead by new_extra_isize.
2652 base = IFIRST(header);
2653 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2654 min_offs = end - base;
2655 total_ino = sizeof(struct ext4_xattr_ibody_header);
2657 error = xattr_check_inode(inode, header, end);
2661 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
2662 if (ifree >= isize_diff)
2666 * Enough free space isn't available in the inode, check if
2667 * EA block can hold new_extra_isize bytes.
2669 if (EXT4_I(inode)->i_file_acl) {
2670 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2674 if (ext4_xattr_check_block(inode, bh)) {
2675 EXT4_ERROR_INODE(inode, "bad block %llu",
2676 EXT4_I(inode)->i_file_acl);
2677 error = -EFSCORRUPTED;
2681 end = bh->b_data + bh->b_size;
2682 min_offs = end - base;
2683 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2685 if (bfree + ifree < isize_diff) {
2686 if (!tried_min_extra_isize && s_min_extra_isize) {
2687 tried_min_extra_isize++;
2688 new_extra_isize = s_min_extra_isize;
2696 bfree = inode->i_sb->s_blocksize;
2699 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2700 isize_diff, ifree, bfree,
2703 if (error == -ENOSPC && !tried_min_extra_isize &&
2704 s_min_extra_isize) {
2705 tried_min_extra_isize++;
2706 new_extra_isize = s_min_extra_isize;
2713 /* Adjust the offsets and shift the remaining entries ahead */
2714 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
2715 - new_extra_isize, (void *)raw_inode +
2716 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
2717 (void *)header, total_ino);
2718 EXT4_I(inode)->i_extra_isize = new_extra_isize;
2721 ext4_write_unlock_xattr(inode, &no_expand);
2727 * Inode size expansion failed; don't try again
2730 ext4_write_unlock_xattr(inode, &no_expand);
2734 #define EIA_INCR 16 /* must be 2^n */
2735 #define EIA_MASK (EIA_INCR - 1)
2737 /* Add the large xattr @inode into @ea_inode_array for deferred iput().
2738 * If @ea_inode_array is new or full it will be grown and the old
2739 * contents copied over.
2742 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2743 struct inode *inode)
2745 if (*ea_inode_array == NULL) {
2747 * Start with 15 inodes, so it fits into a power-of-two size.
2748 * If *ea_inode_array is NULL, this is essentially offsetof()
2751 kmalloc(offsetof(struct ext4_xattr_inode_array,
2754 if (*ea_inode_array == NULL)
2756 (*ea_inode_array)->count = 0;
2757 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
2758 /* expand the array once all 15 + n * 16 slots are full */
2759 struct ext4_xattr_inode_array *new_array = NULL;
2760 int count = (*ea_inode_array)->count;
2762 /* if new_array is NULL, this is essentially offsetof() */
2763 new_array = kmalloc(
2764 offsetof(struct ext4_xattr_inode_array,
2765 inodes[count + EIA_INCR]),
2767 if (new_array == NULL)
2769 memcpy(new_array, *ea_inode_array,
2770 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2771 kfree(*ea_inode_array);
2772 *ea_inode_array = new_array;
2774 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
2779 * ext4_xattr_delete_inode()
2781 * Free extended attribute resources associated with this inode. Traverse
2782 * all entries and decrement reference on any xattr inodes associated with this
2783 * inode. This is called immediately before an inode is freed. We have exclusive
2784 * access to the inode. If an orphan inode is deleted it will also release its
2785 * references on xattr block and xattr inodes.
2787 int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2788 struct ext4_xattr_inode_array **ea_inode_array,
2791 struct buffer_head *bh = NULL;
2792 struct ext4_xattr_ibody_header *header;
2793 struct ext4_iloc iloc = { .bh = NULL };
2794 struct ext4_xattr_entry *entry;
2797 error = ext4_xattr_ensure_credits(handle, inode, extra_credits,
2800 false /* block_csum */);
2802 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2806 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2807 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2809 error = ext4_get_inode_loc(inode, &iloc);
2811 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
2815 error = ext4_journal_get_write_access(handle, iloc.bh);
2817 EXT4_ERROR_INODE(inode, "write access (error %d)",
2822 header = IHDR(inode, ext4_raw_inode(&iloc));
2823 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2824 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2826 false /* block_csum */,
2829 false /* skip_quota */);
2832 if (EXT4_I(inode)->i_file_acl) {
2833 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2835 EXT4_ERROR_INODE(inode, "block %llu read error",
2836 EXT4_I(inode)->i_file_acl);
2840 error = ext4_xattr_check_block(inode, bh);
2842 EXT4_ERROR_INODE(inode, "bad block %llu (error %d)",
2843 EXT4_I(inode)->i_file_acl, error);
2847 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2848 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2849 entry = EXT4_XATTR_NEXT(entry))
2850 if (entry->e_value_inum)
2851 ext4_xattr_inode_free_quota(inode,
2852 le32_to_cpu(entry->e_value_size));
2856 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2859 * Update i_file_acl value in the same transaction that releases
2862 EXT4_I(inode)->i_file_acl = 0;
2863 error = ext4_mark_inode_dirty(handle, inode);
2865 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2877 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
2881 if (ea_inode_array == NULL)
2884 for (idx = 0; idx < ea_inode_array->count; ++idx)
2885 iput(ea_inode_array->inodes[idx]);
2886 kfree(ea_inode_array);
2890 * ext4_xattr_block_cache_insert()
2892 * Create a new entry in the extended attribute block cache, and insert
2893 * it unless such an entry is already in the cache.
2895 * Returns 0, or a negative error number on failure.
2898 ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
2899 struct buffer_head *bh)
2901 struct ext4_xattr_header *header = BHDR(bh);
2902 __u32 hash = le32_to_cpu(header->h_hash);
2903 int reusable = le32_to_cpu(header->h_refcount) <
2904 EXT4_XATTR_REFCOUNT_MAX;
2907 if (!ea_block_cache)
2909 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
2910 bh->b_blocknr, reusable);
2912 if (error == -EBUSY)
2913 ea_bdebug(bh, "already in cache");
2915 ea_bdebug(bh, "inserting [%x]", (int)hash);
2921 * Compare two extended attribute blocks for equality.
2923 * Returns 0 if the blocks are equal, 1 if they differ, and
2924 * a negative error number on errors.
2927 ext4_xattr_cmp(struct ext4_xattr_header *header1,
2928 struct ext4_xattr_header *header2)
2930 struct ext4_xattr_entry *entry1, *entry2;
2932 entry1 = ENTRY(header1+1);
2933 entry2 = ENTRY(header2+1);
2934 while (!IS_LAST_ENTRY(entry1)) {
2935 if (IS_LAST_ENTRY(entry2))
2937 if (entry1->e_hash != entry2->e_hash ||
2938 entry1->e_name_index != entry2->e_name_index ||
2939 entry1->e_name_len != entry2->e_name_len ||
2940 entry1->e_value_size != entry2->e_value_size ||
2941 entry1->e_value_inum != entry2->e_value_inum ||
2942 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
2944 if (!entry1->e_value_inum &&
2945 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
2946 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
2947 le32_to_cpu(entry1->e_value_size)))
2950 entry1 = EXT4_XATTR_NEXT(entry1);
2951 entry2 = EXT4_XATTR_NEXT(entry2);
2953 if (!IS_LAST_ENTRY(entry2))
2959 * ext4_xattr_block_cache_find()
2961 * Find an identical extended attribute block.
2963 * Returns a pointer to the block found, or NULL if such a block was
2964 * not found or an error occurred.
2966 static struct buffer_head *
2967 ext4_xattr_block_cache_find(struct inode *inode,
2968 struct ext4_xattr_header *header,
2969 struct mb_cache_entry **pce)
2971 __u32 hash = le32_to_cpu(header->h_hash);
2972 struct mb_cache_entry *ce;
2973 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
2975 if (!ea_block_cache)
2977 if (!header->h_hash)
2978 return NULL; /* never share */
2979 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
2980 ce = mb_cache_entry_find_first(ea_block_cache, hash);
2982 struct buffer_head *bh;
2984 bh = sb_bread(inode->i_sb, ce->e_value);
2986 EXT4_ERROR_INODE(inode, "block %lu read error",
2987 (unsigned long)ce->e_value);
2988 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
2993 ce = mb_cache_entry_find_next(ea_block_cache, ce);
2998 #define NAME_HASH_SHIFT 5
2999 #define VALUE_HASH_SHIFT 16
3002 * ext4_xattr_hash_entry()
3004 * Compute the hash of an extended attribute.
3006 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3011 while (name_len--) {
3012 hash = (hash << NAME_HASH_SHIFT) ^
3013 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3016 while (value_count--) {
3017 hash = (hash << VALUE_HASH_SHIFT) ^
3018 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3019 le32_to_cpu(*value++);
3021 return cpu_to_le32(hash);
3024 #undef NAME_HASH_SHIFT
3025 #undef VALUE_HASH_SHIFT
3027 #define BLOCK_HASH_SHIFT 16
3030 * ext4_xattr_rehash()
3032 * Re-compute the extended attribute hash value after an entry has changed.
3034 static void ext4_xattr_rehash(struct ext4_xattr_header *header)
3036 struct ext4_xattr_entry *here;
3039 here = ENTRY(header+1);
3040 while (!IS_LAST_ENTRY(here)) {
3041 if (!here->e_hash) {
3042 /* Block is not shared if an entry's hash value == 0 */
3046 hash = (hash << BLOCK_HASH_SHIFT) ^
3047 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3048 le32_to_cpu(here->e_hash);
3049 here = EXT4_XATTR_NEXT(here);
3051 header->h_hash = cpu_to_le32(hash);
3054 #undef BLOCK_HASH_SHIFT
3056 #define HASH_BUCKET_BITS 10
3059 ext4_xattr_create_cache(void)
3061 return mb_cache_create(HASH_BUCKET_BITS);
3064 void ext4_xattr_destroy_cache(struct mb_cache *cache)
3067 mb_cache_destroy(cache);