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 void ext4_xattr_rehash(struct ext4_xattr_header *,
81 struct ext4_xattr_entry *);
83 static const struct xattr_handler * const ext4_xattr_handler_map[] = {
84 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
85 #ifdef CONFIG_EXT4_FS_POSIX_ACL
86 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
87 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
89 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
90 #ifdef CONFIG_EXT4_FS_SECURITY
91 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
95 const struct xattr_handler *ext4_xattr_handlers[] = {
96 &ext4_xattr_user_handler,
97 &ext4_xattr_trusted_handler,
98 #ifdef CONFIG_EXT4_FS_POSIX_ACL
99 &posix_acl_access_xattr_handler,
100 &posix_acl_default_xattr_handler,
102 #ifdef CONFIG_EXT4_FS_SECURITY
103 &ext4_xattr_security_handler,
108 #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
109 inode->i_sb->s_fs_info)->s_ea_block_cache)
111 #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
112 inode->i_sb->s_fs_info)->s_ea_inode_cache)
115 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
116 struct inode *inode);
118 #ifdef CONFIG_LOCKDEP
119 void ext4_xattr_inode_set_class(struct inode *ea_inode)
121 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
125 static __le32 ext4_xattr_block_csum(struct inode *inode,
127 struct ext4_xattr_header *hdr)
129 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
131 __le64 dsk_block_nr = cpu_to_le64(block_nr);
132 __u32 dummy_csum = 0;
133 int offset = offsetof(struct ext4_xattr_header, h_checksum);
135 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
136 sizeof(dsk_block_nr));
137 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
138 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
139 offset += sizeof(dummy_csum);
140 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
141 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
143 return cpu_to_le32(csum);
146 static int ext4_xattr_block_csum_verify(struct inode *inode,
147 struct buffer_head *bh)
149 struct ext4_xattr_header *hdr = BHDR(bh);
152 if (ext4_has_metadata_csum(inode->i_sb)) {
154 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
155 bh->b_blocknr, hdr));
161 static void ext4_xattr_block_csum_set(struct inode *inode,
162 struct buffer_head *bh)
164 if (ext4_has_metadata_csum(inode->i_sb))
165 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
166 bh->b_blocknr, BHDR(bh));
169 static inline const struct xattr_handler *
170 ext4_xattr_handler(int name_index)
172 const struct xattr_handler *handler = NULL;
174 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
175 handler = ext4_xattr_handler_map[name_index];
180 ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
183 struct ext4_xattr_entry *e = entry;
185 /* Find the end of the names list */
186 while (!IS_LAST_ENTRY(e)) {
187 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
188 if ((void *)next >= end)
189 return -EFSCORRUPTED;
193 /* Check the values */
194 while (!IS_LAST_ENTRY(entry)) {
195 if (entry->e_value_size != 0 &&
196 entry->e_value_inum == 0) {
197 u16 offs = le16_to_cpu(entry->e_value_offs);
198 u32 size = le32_to_cpu(entry->e_value_size);
202 * The value cannot overlap the names, and the value
203 * with padding cannot extend beyond 'end'. Check both
204 * the padded and unpadded sizes, since the size may
205 * overflow to 0 when adding padding.
207 if (offs > end - value_start)
208 return -EFSCORRUPTED;
209 value = value_start + offs;
210 if (value < (void *)e + sizeof(u32) ||
211 size > end - value ||
212 EXT4_XATTR_SIZE(size) > end - value)
213 return -EFSCORRUPTED;
215 entry = EXT4_XATTR_NEXT(entry);
222 ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
226 if (buffer_verified(bh))
229 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
230 BHDR(bh)->h_blocks != cpu_to_le32(1))
231 return -EFSCORRUPTED;
232 if (!ext4_xattr_block_csum_verify(inode, bh))
234 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
237 set_buffer_verified(bh);
242 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
243 void *end, const char *function, unsigned int line)
245 int error = -EFSCORRUPTED;
247 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
248 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
250 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
253 __ext4_error_inode(inode, function, line, 0,
254 "corrupted in-inode xattr");
258 #define xattr_check_inode(inode, header, end) \
259 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
262 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
263 const char *name, int sorted)
265 struct ext4_xattr_entry *entry;
271 name_len = strlen(name);
273 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
274 cmp = name_index - entry->e_name_index;
276 cmp = name_len - entry->e_name_len;
278 cmp = memcmp(name, entry->e_name, name_len);
279 if (cmp <= 0 && (sorted || cmp == 0))
283 return cmp ? -ENODATA : 0;
287 ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
289 return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
292 static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
294 return ((u64)ea_inode->i_ctime.tv_sec << 32) |
295 ((u32)ea_inode->i_version);
298 static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
300 ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
301 ea_inode->i_version = (u32)ref_count;
304 static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
306 return (u32)ea_inode->i_atime.tv_sec;
309 static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
311 ea_inode->i_atime.tv_sec = hash;
315 * Read the EA value from an inode.
317 static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
319 unsigned long block = 0;
320 struct buffer_head *bh;
321 int blocksize = ea_inode->i_sb->s_blocksize;
322 size_t csize, copied = 0;
323 void *copy_pos = buf;
325 while (copied < size) {
326 csize = (size - copied) > blocksize ? blocksize : size - copied;
327 bh = ext4_bread(NULL, ea_inode, block, 0);
331 return -EFSCORRUPTED;
333 memcpy(copy_pos, bh->b_data, csize);
343 static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
344 struct inode **ea_inode)
349 inode = ext4_iget(parent->i_sb, ea_ino);
351 err = PTR_ERR(inode);
352 ext4_error(parent->i_sb,
353 "error while reading EA inode %lu err=%d", ea_ino,
358 if (is_bad_inode(inode)) {
359 ext4_error(parent->i_sb,
360 "error while reading EA inode %lu is_bad_inode",
366 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
367 ext4_error(parent->i_sb,
368 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
382 ext4_xattr_inode_verify_hash(struct inode *ea_inode, void *buffer, size_t size)
386 /* Verify stored hash matches calculated hash. */
387 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
388 if (hash != ext4_xattr_inode_get_hash(ea_inode))
389 return -EFSCORRUPTED;
393 #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
396 * Read the value from the EA inode.
399 ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
402 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
403 struct inode *ea_inode;
406 err = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode);
412 if (i_size_read(ea_inode) != size) {
413 ext4_warning_inode(ea_inode,
414 "ea_inode file size=%llu entry size=%zu",
415 i_size_read(ea_inode), size);
420 err = ext4_xattr_inode_read(ea_inode, buffer, size);
424 err = ext4_xattr_inode_verify_hash(ea_inode, buffer, size);
426 * Compatibility check for old Lustre ea_inode implementation. Old
427 * version does not have hash validation, but it has a backpointer
428 * from ea_inode to the parent inode.
430 if (err == -EFSCORRUPTED) {
431 if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != inode->i_ino ||
432 ea_inode->i_generation != inode->i_generation) {
433 ext4_warning_inode(ea_inode,
434 "EA inode hash validation failed");
437 /* Do not add ea_inode to the cache. */
438 ea_inode_cache = NULL;
443 mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
444 ext4_xattr_inode_get_hash(ea_inode),
445 ea_inode->i_ino, true /* reusable */);
452 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
453 void *buffer, size_t buffer_size)
455 struct buffer_head *bh = NULL;
456 struct ext4_xattr_entry *entry;
459 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
461 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
462 name_index, name, buffer, (long)buffer_size);
465 if (!EXT4_I(inode)->i_file_acl)
467 ea_idebug(inode, "reading block %llu",
468 (unsigned long long)EXT4_I(inode)->i_file_acl);
469 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
472 ea_bdebug(bh, "b_count=%d, refcount=%d",
473 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
474 if (ext4_xattr_check_block(inode, bh)) {
475 EXT4_ERROR_INODE(inode, "bad block %llu",
476 EXT4_I(inode)->i_file_acl);
477 error = -EFSCORRUPTED;
480 ext4_xattr_block_cache_insert(ea_block_cache, bh);
482 error = ext4_xattr_find_entry(&entry, name_index, name, 1);
485 size = le32_to_cpu(entry->e_value_size);
488 if (size > buffer_size)
490 if (entry->e_value_inum) {
491 error = ext4_xattr_inode_get(inode,
492 le32_to_cpu(entry->e_value_inum),
497 memcpy(buffer, bh->b_data +
498 le16_to_cpu(entry->e_value_offs), size);
509 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
510 void *buffer, size_t buffer_size)
512 struct ext4_xattr_ibody_header *header;
513 struct ext4_xattr_entry *entry;
514 struct ext4_inode *raw_inode;
515 struct ext4_iloc iloc;
520 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
522 error = ext4_get_inode_loc(inode, &iloc);
525 raw_inode = ext4_raw_inode(&iloc);
526 header = IHDR(inode, raw_inode);
527 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
528 error = xattr_check_inode(inode, header, end);
531 entry = IFIRST(header);
532 error = ext4_xattr_find_entry(&entry, name_index, name, 0);
535 size = le32_to_cpu(entry->e_value_size);
538 if (size > buffer_size)
540 if (entry->e_value_inum) {
541 error = ext4_xattr_inode_get(inode,
542 le32_to_cpu(entry->e_value_inum),
547 memcpy(buffer, (void *)IFIRST(header) +
548 le16_to_cpu(entry->e_value_offs), size);
561 * Copy an extended attribute into the buffer
562 * provided, or compute the buffer size required.
563 * Buffer is NULL to compute the size of the buffer required.
565 * Returns a negative error number on failure, or the number of bytes
566 * used / required on success.
569 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
570 void *buffer, size_t buffer_size)
574 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
577 if (strlen(name) > 255)
580 down_read(&EXT4_I(inode)->xattr_sem);
581 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
583 if (error == -ENODATA)
584 error = ext4_xattr_block_get(inode, name_index, name, buffer,
586 up_read(&EXT4_I(inode)->xattr_sem);
591 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
592 char *buffer, size_t buffer_size)
594 size_t rest = buffer_size;
596 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
597 const struct xattr_handler *handler =
598 ext4_xattr_handler(entry->e_name_index);
600 if (handler && (!handler->list || handler->list(dentry))) {
601 const char *prefix = handler->prefix ?: handler->name;
602 size_t prefix_len = strlen(prefix);
603 size_t size = prefix_len + entry->e_name_len + 1;
608 memcpy(buffer, prefix, prefix_len);
609 buffer += prefix_len;
610 memcpy(buffer, entry->e_name, entry->e_name_len);
611 buffer += entry->e_name_len;
617 return buffer_size - rest; /* total size */
621 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
623 struct inode *inode = d_inode(dentry);
624 struct buffer_head *bh = NULL;
627 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
628 buffer, (long)buffer_size);
631 if (!EXT4_I(inode)->i_file_acl)
633 ea_idebug(inode, "reading block %llu",
634 (unsigned long long)EXT4_I(inode)->i_file_acl);
635 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
639 ea_bdebug(bh, "b_count=%d, refcount=%d",
640 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
641 if (ext4_xattr_check_block(inode, bh)) {
642 EXT4_ERROR_INODE(inode, "bad block %llu",
643 EXT4_I(inode)->i_file_acl);
644 error = -EFSCORRUPTED;
647 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
648 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
657 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
659 struct inode *inode = d_inode(dentry);
660 struct ext4_xattr_ibody_header *header;
661 struct ext4_inode *raw_inode;
662 struct ext4_iloc iloc;
666 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
668 error = ext4_get_inode_loc(inode, &iloc);
671 raw_inode = ext4_raw_inode(&iloc);
672 header = IHDR(inode, raw_inode);
673 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
674 error = xattr_check_inode(inode, header, end);
677 error = ext4_xattr_list_entries(dentry, IFIRST(header),
678 buffer, buffer_size);
686 * Inode operation listxattr()
688 * d_inode(dentry)->i_rwsem: don't care
690 * Copy a list of attribute names into the buffer
691 * provided, or compute the buffer size required.
692 * Buffer is NULL to compute the size of the buffer required.
694 * Returns a negative error number on failure, or the number of bytes
695 * used / required on success.
698 ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
702 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
703 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
710 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
715 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
720 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
723 static void ext4_xattr_update_super_block(handle_t *handle,
724 struct super_block *sb)
726 if (ext4_has_feature_xattr(sb))
729 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
730 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
731 ext4_set_feature_xattr(sb);
732 ext4_handle_dirty_super(handle, sb);
736 static inline size_t round_up_cluster(struct inode *inode, size_t length)
738 struct super_block *sb = inode->i_sb;
739 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
741 size_t mask = ~(cluster_size - 1);
743 return (length + cluster_size - 1) & mask;
746 static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
750 err = dquot_alloc_inode(inode);
753 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
755 dquot_free_inode(inode);
759 static void ext4_xattr_inode_free_quota(struct inode *inode, size_t len)
761 dquot_free_space_nodirty(inode, round_up_cluster(inode, len));
762 dquot_free_inode(inode);
765 static int __ext4_xattr_set_credits(struct inode *inode,
766 struct buffer_head *block_bh,
769 struct super_block *sb = inode->i_sb;
774 * 1) Owner inode update
775 * 2) Ref count update on old xattr block
777 * 4) block bitmap update for new xattr block
778 * 5) group descriptor for new xattr block
779 * 6) block bitmap update for old xattr block
780 * 7) group descriptor for old block
782 * 6 & 7 can happen if we have two racing threads T_a and T_b
783 * which are each trying to set an xattr on inodes I_a and I_b
784 * which were both initially sharing an xattr block.
789 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
792 * In case of inline data, we may push out the data to a block,
793 * so we need to reserve credits for this eventuality
795 if (ext4_has_inline_data(inode))
796 credits += ext4_writepage_trans_blocks(inode) + 1;
798 /* We are done if ea_inode feature is not enabled. */
799 if (!ext4_has_feature_ea_inode(sb))
802 /* New ea_inode, inode map, block bitmap, group descriptor. */
806 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
808 /* Indirection block or one level of extent tree. */
811 /* Block bitmap and group descriptor updates for each block. */
812 credits += blocks * 2;
814 /* Blocks themselves. */
817 /* Dereference ea_inode holding old xattr value.
818 * Old ea_inode, inode map, block bitmap, group descriptor.
822 /* Data blocks for old ea_inode. */
823 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
825 /* Indirection block or one level of extent tree for old ea_inode. */
828 /* Block bitmap and group descriptor updates for each block. */
829 credits += blocks * 2;
831 /* We may need to clone the existing xattr block in which case we need
832 * to increment ref counts for existing ea_inodes referenced by it.
835 struct ext4_xattr_entry *entry = BFIRST(block_bh);
837 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
838 if (entry->e_value_inum)
839 /* Ref count update on ea_inode. */
845 static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
846 int credits, struct buffer_head *bh,
847 bool dirty, bool block_csum)
851 if (!ext4_handle_valid(handle))
854 if (handle->h_buffer_credits >= credits)
857 error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
861 ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
867 ext4_xattr_block_csum_set(inode, bh);
868 error = ext4_handle_dirty_metadata(handle, NULL, bh);
870 ext4_warning(inode->i_sb, "Handle metadata (error %d)",
876 error = ext4_journal_restart(handle, credits);
878 ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
883 error = ext4_journal_get_write_access(handle, bh);
885 ext4_warning(inode->i_sb,
886 "Get write access failed (error %d)",
894 static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
897 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
898 struct ext4_iloc iloc;
903 inode_lock(ea_inode);
905 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
911 ref_count = ext4_xattr_inode_get_ref(ea_inode);
912 ref_count += ref_change;
913 ext4_xattr_inode_set_ref(ea_inode, ref_count);
915 if (ref_change > 0) {
916 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
917 ea_inode->i_ino, ref_count);
919 if (ref_count == 1) {
920 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
921 ea_inode->i_ino, ea_inode->i_nlink);
923 set_nlink(ea_inode, 1);
924 ext4_orphan_del(handle, ea_inode);
926 hash = ext4_xattr_inode_get_hash(ea_inode);
927 mb_cache_entry_create(ea_inode_cache, GFP_NOFS, hash,
929 true /* reusable */);
932 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
933 ea_inode->i_ino, ref_count);
935 if (ref_count == 0) {
936 WARN_ONCE(ea_inode->i_nlink != 1,
937 "EA inode %lu i_nlink=%u",
938 ea_inode->i_ino, ea_inode->i_nlink);
940 clear_nlink(ea_inode);
941 ext4_orphan_add(handle, ea_inode);
943 hash = ext4_xattr_inode_get_hash(ea_inode);
944 mb_cache_entry_delete(ea_inode_cache, hash,
949 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
952 ext4_warning_inode(ea_inode,
953 "ext4_mark_iloc_dirty() failed ret=%d", ret);
956 inode_unlock(ea_inode);
960 static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
962 return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
965 static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
967 return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
970 static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
971 struct ext4_xattr_entry *first)
973 struct inode *ea_inode;
974 struct ext4_xattr_entry *entry;
975 struct ext4_xattr_entry *failed_entry;
979 for (entry = first; !IS_LAST_ENTRY(entry);
980 entry = EXT4_XATTR_NEXT(entry)) {
981 if (!entry->e_value_inum)
983 ea_ino = le32_to_cpu(entry->e_value_inum);
984 err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
987 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
989 ext4_warning_inode(ea_inode, "inc ref error %d", err);
999 failed_entry = entry;
1001 for (entry = first; entry != failed_entry;
1002 entry = EXT4_XATTR_NEXT(entry)) {
1003 if (!entry->e_value_inum)
1005 ea_ino = le32_to_cpu(entry->e_value_inum);
1006 err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
1008 ext4_warning(parent->i_sb,
1009 "cleanup ea_ino %u iget error %d", ea_ino,
1013 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1015 ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1023 ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1024 struct buffer_head *bh,
1025 struct ext4_xattr_entry *first, bool block_csum,
1026 struct ext4_xattr_inode_array **ea_inode_array,
1027 int extra_credits, bool skip_quota)
1029 struct inode *ea_inode;
1030 struct ext4_xattr_entry *entry;
1032 unsigned int ea_ino;
1036 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1037 credits = 2 + extra_credits;
1039 for (entry = first; !IS_LAST_ENTRY(entry);
1040 entry = EXT4_XATTR_NEXT(entry)) {
1041 if (!entry->e_value_inum)
1043 ea_ino = le32_to_cpu(entry->e_value_inum);
1044 err = ext4_xattr_inode_iget(parent, ea_ino, &ea_inode);
1048 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1050 ext4_warning_inode(ea_inode,
1051 "Expand inode array err=%d", err);
1056 err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
1059 ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1064 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1066 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1072 ext4_xattr_inode_free_quota(parent,
1073 le32_to_cpu(entry->e_value_size));
1076 * Forget about ea_inode within the same transaction that
1077 * decrements the ref count. This avoids duplicate decrements in
1078 * case the rest of the work spills over to subsequent
1081 entry->e_value_inum = 0;
1082 entry->e_value_size = 0;
1089 * Note that we are deliberately skipping csum calculation for
1090 * the final update because we do not expect any journal
1091 * restarts until xattr block is freed.
1094 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1096 ext4_warning_inode(parent,
1097 "handle dirty metadata err=%d", err);
1102 * Release the xattr block BH: If the reference count is > 1, decrement it;
1103 * otherwise free the block.
1106 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
1107 struct buffer_head *bh,
1108 struct ext4_xattr_inode_array **ea_inode_array,
1111 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1115 BUFFER_TRACE(bh, "get_write_access");
1116 error = ext4_journal_get_write_access(handle, bh);
1121 hash = le32_to_cpu(BHDR(bh)->h_hash);
1122 ref = le32_to_cpu(BHDR(bh)->h_refcount);
1124 ea_bdebug(bh, "refcount now=0; freeing");
1126 * This must happen under buffer lock for
1127 * ext4_xattr_block_set() to reliably detect freed block
1129 mb_cache_entry_delete(ea_block_cache, hash, bh->b_blocknr);
1133 if (ext4_has_feature_ea_inode(inode->i_sb))
1134 ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1136 true /* block_csum */,
1139 true /* skip_quota */);
1140 ext4_free_blocks(handle, inode, bh, 0, 1,
1141 EXT4_FREE_BLOCKS_METADATA |
1142 EXT4_FREE_BLOCKS_FORGET);
1145 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1146 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1147 struct mb_cache_entry *ce;
1149 ce = mb_cache_entry_get(ea_block_cache, hash,
1153 mb_cache_entry_put(ea_block_cache, ce);
1157 ext4_xattr_block_csum_set(inode, bh);
1159 * Beware of this ugliness: Releasing of xattr block references
1160 * from different inodes can race and so we have to protect
1161 * from a race where someone else frees the block (and releases
1162 * its journal_head) before we are done dirtying the buffer. In
1163 * nojournal mode this race is harmless and we actually cannot
1164 * call ext4_handle_dirty_metadata() with locked buffer as
1165 * that function can call sync_dirty_buffer() so for that case
1166 * we handle the dirtying after unlocking the buffer.
1168 if (ext4_handle_valid(handle))
1169 error = ext4_handle_dirty_metadata(handle, inode, bh);
1171 if (!ext4_handle_valid(handle))
1172 error = ext4_handle_dirty_metadata(handle, inode, bh);
1174 ext4_handle_sync(handle);
1175 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
1176 ea_bdebug(bh, "refcount now=%d; releasing",
1177 le32_to_cpu(BHDR(bh)->h_refcount));
1180 ext4_std_error(inode->i_sb, error);
1185 * Find the available free space for EAs. This also returns the total number of
1186 * bytes used by EA entries.
1188 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1189 size_t *min_offs, void *base, int *total)
1191 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1192 if (!last->e_value_inum && last->e_value_size) {
1193 size_t offs = le16_to_cpu(last->e_value_offs);
1194 if (offs < *min_offs)
1198 *total += EXT4_XATTR_LEN(last->e_name_len);
1200 return (*min_offs - ((void *)last - base) - sizeof(__u32));
1204 * Write the value of the EA in an inode.
1206 static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1207 const void *buf, int bufsize)
1209 struct buffer_head *bh = NULL;
1210 unsigned long block = 0;
1211 int blocksize = ea_inode->i_sb->s_blocksize;
1212 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
1213 int csize, wsize = 0;
1218 while (ret >= 0 && ret < max_blocks) {
1219 struct ext4_map_blocks map;
1220 map.m_lblk = block += ret;
1221 map.m_len = max_blocks -= ret;
1223 ret = ext4_map_blocks(handle, ea_inode, &map,
1224 EXT4_GET_BLOCKS_CREATE);
1226 ext4_mark_inode_dirty(handle, ea_inode);
1227 if (ret == -ENOSPC &&
1228 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1240 while (wsize < bufsize) {
1243 csize = (bufsize - wsize) > blocksize ? blocksize :
1245 bh = ext4_getblk(handle, ea_inode, block, 0);
1248 ret = ext4_journal_get_write_access(handle, bh);
1252 memcpy(bh->b_data, buf, csize);
1253 set_buffer_uptodate(bh);
1254 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1261 inode_lock(ea_inode);
1262 i_size_write(ea_inode, wsize);
1263 ext4_update_i_disksize(ea_inode, wsize);
1264 inode_unlock(ea_inode);
1266 ext4_mark_inode_dirty(handle, ea_inode);
1275 * Create an inode to store the value of a large EA.
1277 static struct inode *ext4_xattr_inode_create(handle_t *handle,
1278 struct inode *inode, u32 hash)
1280 struct inode *ea_inode = NULL;
1281 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
1285 * Let the next inode be the goal, so we try and allocate the EA inode
1286 * in the same group, or nearby one.
1288 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
1289 S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
1291 if (!IS_ERR(ea_inode)) {
1292 ea_inode->i_op = &ext4_file_inode_operations;
1293 ea_inode->i_fop = &ext4_file_operations;
1294 ext4_set_aops(ea_inode);
1295 ext4_xattr_inode_set_class(ea_inode);
1296 unlock_new_inode(ea_inode);
1297 ext4_xattr_inode_set_ref(ea_inode, 1);
1298 ext4_xattr_inode_set_hash(ea_inode, hash);
1299 err = ext4_mark_inode_dirty(handle, ea_inode);
1301 err = ext4_inode_attach_jinode(ea_inode);
1304 return ERR_PTR(err);
1308 * Xattr inodes are shared therefore quota charging is performed
1309 * at a higher level.
1311 dquot_free_inode(ea_inode);
1312 dquot_drop(ea_inode);
1313 inode_lock(ea_inode);
1314 ea_inode->i_flags |= S_NOQUOTA;
1315 inode_unlock(ea_inode);
1321 static struct inode *
1322 ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1323 size_t value_len, u32 hash)
1325 struct inode *ea_inode;
1326 struct mb_cache_entry *ce;
1327 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1330 ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1334 ea_data = ext4_kvmalloc(value_len, GFP_NOFS);
1336 mb_cache_entry_put(ea_inode_cache, ce);
1341 ea_inode = ext4_iget(inode->i_sb, ce->e_value);
1342 if (!IS_ERR(ea_inode) &&
1343 !is_bad_inode(ea_inode) &&
1344 (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
1345 i_size_read(ea_inode) == value_len &&
1346 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
1347 !ext4_xattr_inode_verify_hash(ea_inode, ea_data,
1349 !memcmp(value, ea_data, value_len)) {
1350 mb_cache_entry_touch(ea_inode_cache, ce);
1351 mb_cache_entry_put(ea_inode_cache, ce);
1356 if (!IS_ERR(ea_inode))
1358 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1365 * Add value of the EA in an inode.
1367 static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1368 const void *value, size_t value_len,
1369 struct inode **ret_inode)
1371 struct inode *ea_inode;
1375 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1376 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1378 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1384 *ret_inode = ea_inode;
1388 /* Create an inode for the EA value */
1389 ea_inode = ext4_xattr_inode_create(handle, inode, hash);
1390 if (IS_ERR(ea_inode))
1391 return PTR_ERR(ea_inode);
1393 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
1395 ext4_xattr_inode_dec_ref(handle, ea_inode);
1400 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1401 ea_inode->i_ino, true /* reusable */);
1403 *ret_inode = ea_inode;
1407 static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1408 struct ext4_xattr_search *s,
1409 handle_t *handle, struct inode *inode)
1411 struct ext4_xattr_entry *last;
1412 struct ext4_xattr_entry *here = s->here;
1413 size_t min_offs = s->end - s->base, name_len = strlen(i->name);
1414 int in_inode = i->in_inode;
1415 struct inode *old_ea_inode = NULL;
1416 struct inode *new_ea_inode = NULL;
1417 size_t old_size, new_size;
1420 /* Space used by old and new values. */
1421 old_size = (!s->not_found && !here->e_value_inum) ?
1422 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1423 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1426 * Optimization for the simple case when old and new values have the
1427 * same padded sizes. Not applicable if external inodes are involved.
1429 if (new_size && new_size == old_size) {
1430 size_t offs = le16_to_cpu(here->e_value_offs);
1431 void *val = s->base + offs;
1433 here->e_value_size = cpu_to_le32(i->value_len);
1434 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1435 memset(val, 0, new_size);
1437 memcpy(val, i->value, i->value_len);
1438 /* Clear padding bytes. */
1439 memset(val + i->value_len, 0, new_size - i->value_len);
1444 /* Compute min_offs and last. */
1446 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1447 if (!last->e_value_inum && last->e_value_size) {
1448 size_t offs = le16_to_cpu(last->e_value_offs);
1449 if (offs < min_offs)
1454 /* Check whether we have enough space. */
1458 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1460 free += EXT4_XATTR_LEN(name_len) + old_size;
1462 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1469 * Getting access to old and new ea inodes is subject to failures.
1470 * Finish that work before doing any modifications to the xattr data.
1472 if (!s->not_found && here->e_value_inum) {
1473 ret = ext4_xattr_inode_iget(inode,
1474 le32_to_cpu(here->e_value_inum),
1477 old_ea_inode = NULL;
1481 if (i->value && in_inode) {
1482 WARN_ON_ONCE(!i->value_len);
1484 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1488 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1492 new_ea_inode = NULL;
1493 ext4_xattr_inode_free_quota(inode, i->value_len);
1499 /* We are ready to release ref count on the old_ea_inode. */
1500 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1502 /* Release newly required ref count on new_ea_inode. */
1506 err = ext4_xattr_inode_dec_ref(handle,
1509 ext4_warning_inode(new_ea_inode,
1510 "dec ref new_ea_inode err=%d",
1512 ext4_xattr_inode_free_quota(inode,
1518 ext4_xattr_inode_free_quota(inode,
1519 le32_to_cpu(here->e_value_size));
1522 /* No failures allowed past this point. */
1524 if (!s->not_found && here->e_value_offs) {
1525 /* Remove the old value. */
1526 void *first_val = s->base + min_offs;
1527 size_t offs = le16_to_cpu(here->e_value_offs);
1528 void *val = s->base + offs;
1530 memmove(first_val + old_size, first_val, val - first_val);
1531 memset(first_val, 0, old_size);
1532 min_offs += old_size;
1534 /* Adjust all value offsets. */
1536 while (!IS_LAST_ENTRY(last)) {
1537 size_t o = le16_to_cpu(last->e_value_offs);
1539 if (!last->e_value_inum &&
1540 last->e_value_size && o < offs)
1541 last->e_value_offs = cpu_to_le16(o + old_size);
1542 last = EXT4_XATTR_NEXT(last);
1547 /* Remove old name. */
1548 size_t size = EXT4_XATTR_LEN(name_len);
1550 last = ENTRY((void *)last - size);
1551 memmove(here, (void *)here + size,
1552 (void *)last - (void *)here + sizeof(__u32));
1553 memset(last, 0, size);
1554 } else if (s->not_found) {
1555 /* Insert new name. */
1556 size_t size = EXT4_XATTR_LEN(name_len);
1557 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1559 memmove((void *)here + size, here, rest);
1560 memset(here, 0, size);
1561 here->e_name_index = i->name_index;
1562 here->e_name_len = name_len;
1563 memcpy(here->e_name, i->name, name_len);
1565 /* This is an update, reset value info. */
1566 here->e_value_inum = 0;
1567 here->e_value_offs = 0;
1568 here->e_value_size = 0;
1572 /* Insert new value. */
1574 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
1575 } else if (i->value_len) {
1576 void *val = s->base + min_offs - new_size;
1578 here->e_value_offs = cpu_to_le16(min_offs - new_size);
1579 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1580 memset(val, 0, new_size);
1582 memcpy(val, i->value, i->value_len);
1583 /* Clear padding bytes. */
1584 memset(val + i->value_len, 0,
1585 new_size - i->value_len);
1588 here->e_value_size = cpu_to_le32(i->value_len);
1597 struct ext4_xattr_block_find {
1598 struct ext4_xattr_search s;
1599 struct buffer_head *bh;
1603 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1604 struct ext4_xattr_block_find *bs)
1606 struct super_block *sb = inode->i_sb;
1609 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1610 i->name_index, i->name, i->value, (long)i->value_len);
1612 if (EXT4_I(inode)->i_file_acl) {
1613 /* The inode already has an extended attribute block. */
1614 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
1618 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1619 atomic_read(&(bs->bh->b_count)),
1620 le32_to_cpu(BHDR(bs->bh)->h_refcount));
1621 if (ext4_xattr_check_block(inode, bs->bh)) {
1622 EXT4_ERROR_INODE(inode, "bad block %llu",
1623 EXT4_I(inode)->i_file_acl);
1624 error = -EFSCORRUPTED;
1627 /* Find the named attribute. */
1628 bs->s.base = BHDR(bs->bh);
1629 bs->s.first = BFIRST(bs->bh);
1630 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1631 bs->s.here = bs->s.first;
1632 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
1634 if (error && error != -ENODATA)
1636 bs->s.not_found = error;
1645 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1646 struct ext4_xattr_info *i,
1647 struct ext4_xattr_block_find *bs)
1649 struct super_block *sb = inode->i_sb;
1650 struct buffer_head *new_bh = NULL;
1651 struct ext4_xattr_search s_copy = bs->s;
1652 struct ext4_xattr_search *s = &s_copy;
1653 struct mb_cache_entry *ce = NULL;
1655 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1656 struct inode *ea_inode = NULL;
1657 size_t old_ea_inode_size = 0;
1659 #define header(x) ((struct ext4_xattr_header *)(x))
1662 BUFFER_TRACE(bs->bh, "get_write_access");
1663 error = ext4_journal_get_write_access(handle, bs->bh);
1666 lock_buffer(bs->bh);
1668 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
1669 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1672 * This must happen under buffer lock for
1673 * ext4_xattr_block_set() to reliably detect modified
1676 mb_cache_entry_delete(ea_block_cache, hash,
1678 ea_bdebug(bs->bh, "modifying in-place");
1679 error = ext4_xattr_set_entry(i, s, handle, inode);
1681 if (!IS_LAST_ENTRY(s->first))
1682 ext4_xattr_rehash(header(s->base),
1684 ext4_xattr_block_cache_insert(ea_block_cache,
1687 ext4_xattr_block_csum_set(inode, bs->bh);
1688 unlock_buffer(bs->bh);
1689 if (error == -EFSCORRUPTED)
1692 error = ext4_handle_dirty_metadata(handle,
1699 int offset = (char *)s->here - bs->bh->b_data;
1701 unlock_buffer(bs->bh);
1702 ea_bdebug(bs->bh, "cloning");
1703 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
1705 if (s->base == NULL)
1707 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1708 s->first = ENTRY(header(s->base)+1);
1709 header(s->base)->h_refcount = cpu_to_le32(1);
1710 s->here = ENTRY(s->base + offset);
1711 s->end = s->base + bs->bh->b_size;
1714 * If existing entry points to an xattr inode, we need
1715 * to prevent ext4_xattr_set_entry() from decrementing
1716 * ref count on it because the reference belongs to the
1717 * original block. In this case, make the entry look
1718 * like it has an empty value.
1720 if (!s->not_found && s->here->e_value_inum) {
1722 * Defer quota free call for previous inode
1723 * until success is guaranteed.
1725 old_ea_inode_size = le32_to_cpu(
1726 s->here->e_value_size);
1727 s->here->e_value_inum = 0;
1728 s->here->e_value_size = 0;
1732 /* Allocate a buffer where we construct the new block. */
1733 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
1734 /* assert(header == s->base) */
1736 if (s->base == NULL)
1738 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1739 header(s->base)->h_blocks = cpu_to_le32(1);
1740 header(s->base)->h_refcount = cpu_to_le32(1);
1741 s->first = ENTRY(header(s->base)+1);
1742 s->here = ENTRY(header(s->base)+1);
1743 s->end = s->base + sb->s_blocksize;
1746 error = ext4_xattr_set_entry(i, s, handle, inode);
1747 if (error == -EFSCORRUPTED)
1752 if (i->value && s->here->e_value_inum) {
1753 unsigned int ea_ino;
1756 * A ref count on ea_inode has been taken as part of the call to
1757 * ext4_xattr_set_entry() above. We would like to drop this
1758 * extra ref but we have to wait until the xattr block is
1759 * initialized and has its own ref count on the ea_inode.
1761 ea_ino = le32_to_cpu(s->here->e_value_inum);
1762 error = ext4_xattr_inode_iget(inode, ea_ino, &ea_inode);
1769 if (!IS_LAST_ENTRY(s->first))
1770 ext4_xattr_rehash(header(s->base), s->here);
1773 if (!IS_LAST_ENTRY(s->first)) {
1774 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
1777 /* We found an identical block in the cache. */
1778 if (new_bh == bs->bh)
1779 ea_bdebug(new_bh, "keeping");
1783 WARN_ON_ONCE(dquot_initialize_needed(inode));
1785 /* The old block is released after updating
1787 error = dquot_alloc_block(inode,
1788 EXT4_C2B(EXT4_SB(sb), 1));
1791 BUFFER_TRACE(new_bh, "get_write_access");
1792 error = ext4_journal_get_write_access(handle,
1796 lock_buffer(new_bh);
1798 * We have to be careful about races with
1799 * freeing, rehashing or adding references to
1800 * xattr block. Once we hold buffer lock xattr
1801 * block's state is stable so we can check
1802 * whether the block got freed / rehashed or
1803 * not. Since we unhash mbcache entry under
1804 * buffer lock when freeing / rehashing xattr
1805 * block, checking whether entry is still
1806 * hashed is reliable. Same rules hold for
1807 * e_reusable handling.
1809 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1812 * Undo everything and check mbcache
1815 unlock_buffer(new_bh);
1816 dquot_free_block(inode,
1817 EXT4_C2B(EXT4_SB(sb),
1820 mb_cache_entry_put(ea_block_cache, ce);
1825 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
1826 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
1827 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
1829 ea_bdebug(new_bh, "reusing; refcount now=%d",
1831 ext4_xattr_block_csum_set(inode, new_bh);
1832 unlock_buffer(new_bh);
1833 error = ext4_handle_dirty_metadata(handle,
1839 mb_cache_entry_touch(ea_block_cache, ce);
1840 mb_cache_entry_put(ea_block_cache, ce);
1842 } else if (bs->bh && s->base == bs->bh->b_data) {
1843 /* We were modifying this block in-place. */
1844 ea_bdebug(bs->bh, "keeping this block");
1848 /* We need to allocate a new block */
1849 ext4_fsblk_t goal, block;
1851 WARN_ON_ONCE(dquot_initialize_needed(inode));
1853 goal = ext4_group_first_block_no(sb,
1854 EXT4_I(inode)->i_block_group);
1856 /* non-extent files can't have physical blocks past 2^32 */
1857 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
1858 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
1860 block = ext4_new_meta_blocks(handle, inode, goal, 0,
1865 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
1866 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
1868 ea_idebug(inode, "creating block %llu",
1869 (unsigned long long)block);
1871 new_bh = sb_getblk(sb, block);
1872 if (unlikely(!new_bh)) {
1875 ext4_free_blocks(handle, inode, NULL, block, 1,
1876 EXT4_FREE_BLOCKS_METADATA);
1879 error = ext4_xattr_inode_inc_ref_all(handle, inode,
1880 ENTRY(header(s->base)+1));
1884 /* Drop the extra ref on ea_inode. */
1885 error = ext4_xattr_inode_dec_ref(handle,
1888 ext4_warning_inode(ea_inode,
1895 lock_buffer(new_bh);
1896 error = ext4_journal_get_create_access(handle, new_bh);
1898 unlock_buffer(new_bh);
1902 memcpy(new_bh->b_data, s->base, new_bh->b_size);
1903 ext4_xattr_block_csum_set(inode, new_bh);
1904 set_buffer_uptodate(new_bh);
1905 unlock_buffer(new_bh);
1906 ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
1907 error = ext4_handle_dirty_metadata(handle, inode,
1914 if (old_ea_inode_size)
1915 ext4_xattr_inode_free_quota(inode, old_ea_inode_size);
1917 /* Update the inode. */
1918 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
1920 /* Drop the previous xattr block. */
1921 if (bs->bh && bs->bh != new_bh) {
1922 struct ext4_xattr_inode_array *ea_inode_array = NULL;
1924 ext4_xattr_release_block(handle, inode, bs->bh,
1926 0 /* extra_credits */);
1927 ext4_xattr_inode_array_free(ea_inode_array);
1935 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
1937 ext4_warning_inode(ea_inode, "dec ref error=%d",
1940 /* If there was an error, revert the quota charge. */
1942 ext4_xattr_inode_free_quota(inode,
1943 i_size_read(ea_inode));
1947 mb_cache_entry_put(ea_block_cache, ce);
1949 if (!(bs->bh && s->base == bs->bh->b_data))
1955 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
1959 EXT4_ERROR_INODE(inode, "bad block %llu",
1960 EXT4_I(inode)->i_file_acl);
1966 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1967 struct ext4_xattr_ibody_find *is)
1969 struct ext4_xattr_ibody_header *header;
1970 struct ext4_inode *raw_inode;
1973 if (EXT4_I(inode)->i_extra_isize == 0)
1975 raw_inode = ext4_raw_inode(&is->iloc);
1976 header = IHDR(inode, raw_inode);
1977 is->s.base = is->s.first = IFIRST(header);
1978 is->s.here = is->s.first;
1979 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1980 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
1981 error = xattr_check_inode(inode, header, is->s.end);
1984 /* Find the named attribute. */
1985 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
1987 if (error && error != -ENODATA)
1989 is->s.not_found = error;
1994 int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1995 struct ext4_xattr_info *i,
1996 struct ext4_xattr_ibody_find *is)
1998 struct ext4_xattr_ibody_header *header;
1999 struct ext4_xattr_search *s = &is->s;
2002 if (EXT4_I(inode)->i_extra_isize == 0)
2004 error = ext4_xattr_set_entry(i, s, handle, inode);
2006 if (error == -ENOSPC &&
2007 ext4_has_inline_data(inode)) {
2008 error = ext4_try_to_evict_inline_data(handle, inode,
2009 EXT4_XATTR_LEN(strlen(i->name) +
2010 EXT4_XATTR_SIZE(i->value_len)));
2013 error = ext4_xattr_ibody_find(inode, i, is);
2016 error = ext4_xattr_set_entry(i, s, handle, inode);
2021 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2022 if (!IS_LAST_ENTRY(s->first)) {
2023 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2024 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2026 header->h_magic = cpu_to_le32(0);
2027 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2032 static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
2033 struct ext4_xattr_info *i,
2034 struct ext4_xattr_ibody_find *is)
2036 struct ext4_xattr_ibody_header *header;
2037 struct ext4_xattr_search *s = &is->s;
2040 if (EXT4_I(inode)->i_extra_isize == 0)
2042 error = ext4_xattr_set_entry(i, s, handle, inode);
2045 header = IHDR(inode, ext4_raw_inode(&is->iloc));
2046 if (!IS_LAST_ENTRY(s->first)) {
2047 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2048 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2050 header->h_magic = cpu_to_le32(0);
2051 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2056 static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2057 struct ext4_xattr_info *i)
2061 /* When e_value_inum is set the value is stored externally. */
2062 if (s->here->e_value_inum)
2064 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2066 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2067 return !memcmp(value, i->value, i->value_len);
2070 static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2072 struct buffer_head *bh;
2075 if (!EXT4_I(inode)->i_file_acl)
2077 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2079 return ERR_PTR(-EIO);
2080 error = ext4_xattr_check_block(inode, bh);
2082 return ERR_PTR(error);
2087 * ext4_xattr_set_handle()
2089 * Create, replace or remove an extended attribute for this inode. Value
2090 * is NULL to remove an existing extended attribute, and non-NULL to
2091 * either replace an existing extended attribute, or create a new extended
2092 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2093 * specify that an extended attribute must exist and must not exist
2094 * previous to the call, respectively.
2096 * Returns 0, or a negative error number on failure.
2099 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
2100 const char *name, const void *value, size_t value_len,
2103 struct ext4_xattr_info i = {
2104 .name_index = name_index,
2107 .value_len = value_len,
2110 struct ext4_xattr_ibody_find is = {
2111 .s = { .not_found = -ENODATA, },
2113 struct ext4_xattr_block_find bs = {
2114 .s = { .not_found = -ENODATA, },
2121 if (strlen(name) > 255)
2124 ext4_write_lock_xattr(inode, &no_expand);
2126 /* Check journal credits under write lock. */
2127 if (ext4_handle_valid(handle)) {
2128 struct buffer_head *bh;
2131 bh = ext4_xattr_get_block(inode);
2133 error = PTR_ERR(bh);
2137 credits = __ext4_xattr_set_credits(inode, bh, value_len);
2140 if (!ext4_handle_has_enough_credits(handle, credits)) {
2146 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
2150 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
2151 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2152 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
2153 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
2156 error = ext4_xattr_ibody_find(inode, &i, &is);
2160 error = ext4_xattr_block_find(inode, &i, &bs);
2163 if (is.s.not_found && bs.s.not_found) {
2165 if (flags & XATTR_REPLACE)
2172 if (flags & XATTR_CREATE)
2177 if (!is.s.not_found)
2178 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2179 else if (!bs.s.not_found)
2180 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2183 /* Xattr value did not change? Save us some work and bail out */
2184 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2186 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2189 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2190 (EXT4_XATTR_SIZE(i.value_len) >
2191 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2194 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2195 if (!error && !bs.s.not_found) {
2197 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2198 } else if (error == -ENOSPC) {
2199 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2200 error = ext4_xattr_block_find(inode, &i, &bs);
2204 error = ext4_xattr_block_set(handle, inode, &i, &bs);
2205 if (!error && !is.s.not_found) {
2207 error = ext4_xattr_ibody_set(handle, inode, &i,
2209 } else if (error == -ENOSPC) {
2211 * Xattr does not fit in the block, store at
2212 * external inode if possible.
2214 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2223 ext4_xattr_update_super_block(handle, inode->i_sb);
2224 inode->i_ctime = current_time(inode);
2227 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
2229 * The bh is consumed by ext4_mark_iloc_dirty, even with
2234 ext4_handle_sync(handle);
2240 ext4_write_unlock_xattr(inode, &no_expand);
2244 int ext4_xattr_set_credits(struct inode *inode, size_t value_len, int *credits)
2246 struct buffer_head *bh;
2251 if (!EXT4_SB(inode->i_sb)->s_journal)
2254 down_read(&EXT4_I(inode)->xattr_sem);
2256 bh = ext4_xattr_get_block(inode);
2260 *credits = __ext4_xattr_set_credits(inode, bh, value_len);
2265 up_read(&EXT4_I(inode)->xattr_sem);
2272 * Like ext4_xattr_set_handle, but start from an inode. This extended
2273 * attribute modification is a filesystem transaction by itself.
2275 * Returns 0, or a negative error number on failure.
2278 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
2279 const void *value, size_t value_len, int flags)
2282 struct super_block *sb = inode->i_sb;
2283 int error, retries = 0;
2286 error = dquot_initialize(inode);
2291 error = ext4_xattr_set_credits(inode, value_len, &credits);
2295 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
2296 if (IS_ERR(handle)) {
2297 error = PTR_ERR(handle);
2301 error = ext4_xattr_set_handle(handle, inode, name_index, name,
2302 value, value_len, flags);
2303 error2 = ext4_journal_stop(handle);
2304 if (error == -ENOSPC &&
2305 ext4_should_retry_alloc(sb, &retries))
2315 * Shift the EA entries in the inode to create space for the increased
2318 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2319 int value_offs_shift, void *to,
2320 void *from, size_t n)
2322 struct ext4_xattr_entry *last = entry;
2325 /* We always shift xattr headers further thus offsets get lower */
2326 BUG_ON(value_offs_shift > 0);
2328 /* Adjust the value offsets of the entries */
2329 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2330 if (!last->e_value_inum && last->e_value_size) {
2331 new_offs = le16_to_cpu(last->e_value_offs) +
2333 last->e_value_offs = cpu_to_le16(new_offs);
2336 /* Shift the entries by n bytes */
2337 memmove(to, from, n);
2341 * Move xattr pointed to by 'entry' from inode into external xattr block
2343 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2344 struct ext4_inode *raw_inode,
2345 struct ext4_xattr_entry *entry)
2347 struct ext4_xattr_ibody_find *is = NULL;
2348 struct ext4_xattr_block_find *bs = NULL;
2349 char *buffer = NULL, *b_entry_name = NULL;
2350 size_t value_size = le32_to_cpu(entry->e_value_size);
2351 struct ext4_xattr_info i = {
2354 .name_index = entry->e_name_index,
2355 .in_inode = !!entry->e_value_inum,
2357 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2360 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2361 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2362 buffer = kmalloc(value_size, GFP_NOFS);
2363 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2364 if (!is || !bs || !buffer || !b_entry_name) {
2369 is->s.not_found = -ENODATA;
2370 bs->s.not_found = -ENODATA;
2374 /* Save the entry name and the entry value */
2375 if (entry->e_value_inum) {
2376 error = ext4_xattr_inode_get(inode,
2377 le32_to_cpu(entry->e_value_inum),
2378 buffer, value_size);
2382 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2383 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
2386 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2387 b_entry_name[entry->e_name_len] = '\0';
2388 i.name = b_entry_name;
2390 error = ext4_get_inode_loc(inode, &is->iloc);
2394 error = ext4_xattr_ibody_find(inode, &i, is);
2398 /* Remove the chosen entry from the inode */
2399 error = ext4_xattr_ibody_set(handle, inode, &i, is);
2404 i.value_len = value_size;
2405 error = ext4_xattr_block_find(inode, &i, bs);
2409 /* Add entry which was removed from the inode into the block */
2410 error = ext4_xattr_block_set(handle, inode, &i, bs);
2415 kfree(b_entry_name);
2418 brelse(is->iloc.bh);
2425 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2426 struct ext4_inode *raw_inode,
2427 int isize_diff, size_t ifree,
2428 size_t bfree, int *total_ino)
2430 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2431 struct ext4_xattr_entry *small_entry;
2432 struct ext4_xattr_entry *entry;
2433 struct ext4_xattr_entry *last;
2434 unsigned int entry_size; /* EA entry size */
2435 unsigned int total_size; /* EA entry size + value size */
2436 unsigned int min_total_size;
2439 while (isize_diff > ifree) {
2442 min_total_size = ~0U;
2443 last = IFIRST(header);
2444 /* Find the entry best suited to be pushed into EA block */
2445 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2446 total_size = EXT4_XATTR_LEN(last->e_name_len);
2447 if (!last->e_value_inum)
2448 total_size += EXT4_XATTR_SIZE(
2449 le32_to_cpu(last->e_value_size));
2450 if (total_size <= bfree &&
2451 total_size < min_total_size) {
2452 if (total_size + ifree < isize_diff) {
2456 min_total_size = total_size;
2461 if (entry == NULL) {
2462 if (small_entry == NULL)
2464 entry = small_entry;
2467 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
2468 total_size = entry_size;
2469 if (!entry->e_value_inum)
2470 total_size += EXT4_XATTR_SIZE(
2471 le32_to_cpu(entry->e_value_size));
2472 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2477 *total_ino -= entry_size;
2478 ifree += total_size;
2479 bfree -= total_size;
2486 * Expand an inode by new_extra_isize bytes when EAs are present.
2487 * Returns 0 on success or negative error number on failure.
2489 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2490 struct ext4_inode *raw_inode, handle_t *handle)
2492 struct ext4_xattr_ibody_header *header;
2493 struct buffer_head *bh = NULL;
2495 size_t ifree, bfree;
2498 int error = 0, tried_min_extra_isize = 0;
2499 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
2500 int isize_diff; /* How much do we need to grow i_extra_isize */
2503 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
2507 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
2508 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
2511 header = IHDR(inode, raw_inode);
2514 * Check if enough free space is available in the inode to shift the
2515 * entries ahead by new_extra_isize.
2518 base = IFIRST(header);
2519 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2520 min_offs = end - base;
2521 total_ino = sizeof(struct ext4_xattr_ibody_header);
2523 error = xattr_check_inode(inode, header, end);
2527 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
2528 if (ifree >= isize_diff)
2532 * Enough free space isn't available in the inode, check if
2533 * EA block can hold new_extra_isize bytes.
2535 if (EXT4_I(inode)->i_file_acl) {
2536 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2540 if (ext4_xattr_check_block(inode, bh)) {
2541 EXT4_ERROR_INODE(inode, "bad block %llu",
2542 EXT4_I(inode)->i_file_acl);
2543 error = -EFSCORRUPTED;
2547 end = bh->b_data + bh->b_size;
2548 min_offs = end - base;
2549 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2551 if (bfree + ifree < isize_diff) {
2552 if (!tried_min_extra_isize && s_min_extra_isize) {
2553 tried_min_extra_isize++;
2554 new_extra_isize = s_min_extra_isize;
2562 bfree = inode->i_sb->s_blocksize;
2565 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2566 isize_diff, ifree, bfree,
2569 if (error == -ENOSPC && !tried_min_extra_isize &&
2570 s_min_extra_isize) {
2571 tried_min_extra_isize++;
2572 new_extra_isize = s_min_extra_isize;
2579 /* Adjust the offsets and shift the remaining entries ahead */
2580 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
2581 - new_extra_isize, (void *)raw_inode +
2582 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
2583 (void *)header, total_ino);
2584 EXT4_I(inode)->i_extra_isize = new_extra_isize;
2587 ext4_write_unlock_xattr(inode, &no_expand);
2593 * Inode size expansion failed; don't try again
2596 ext4_write_unlock_xattr(inode, &no_expand);
2600 #define EIA_INCR 16 /* must be 2^n */
2601 #define EIA_MASK (EIA_INCR - 1)
2603 /* Add the large xattr @inode into @ea_inode_array for deferred iput().
2604 * If @ea_inode_array is new or full it will be grown and the old
2605 * contents copied over.
2608 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2609 struct inode *inode)
2611 if (*ea_inode_array == NULL) {
2613 * Start with 15 inodes, so it fits into a power-of-two size.
2614 * If *ea_inode_array is NULL, this is essentially offsetof()
2617 kmalloc(offsetof(struct ext4_xattr_inode_array,
2620 if (*ea_inode_array == NULL)
2622 (*ea_inode_array)->count = 0;
2623 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
2624 /* expand the array once all 15 + n * 16 slots are full */
2625 struct ext4_xattr_inode_array *new_array = NULL;
2626 int count = (*ea_inode_array)->count;
2628 /* if new_array is NULL, this is essentially offsetof() */
2629 new_array = kmalloc(
2630 offsetof(struct ext4_xattr_inode_array,
2631 inodes[count + EIA_INCR]),
2633 if (new_array == NULL)
2635 memcpy(new_array, *ea_inode_array,
2636 offsetof(struct ext4_xattr_inode_array, inodes[count]));
2637 kfree(*ea_inode_array);
2638 *ea_inode_array = new_array;
2640 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
2645 * ext4_xattr_delete_inode()
2647 * Free extended attribute resources associated with this inode. Traverse
2648 * all entries and decrement reference on any xattr inodes associated with this
2649 * inode. This is called immediately before an inode is freed. We have exclusive
2650 * access to the inode. If an orphan inode is deleted it will also release its
2651 * references on xattr block and xattr inodes.
2653 int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2654 struct ext4_xattr_inode_array **ea_inode_array,
2657 struct buffer_head *bh = NULL;
2658 struct ext4_xattr_ibody_header *header;
2659 struct ext4_iloc iloc = { .bh = NULL };
2660 struct ext4_xattr_entry *entry;
2663 error = ext4_xattr_ensure_credits(handle, inode, extra_credits,
2666 false /* block_csum */);
2668 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2672 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2673 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2675 error = ext4_get_inode_loc(inode, &iloc);
2677 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
2681 error = ext4_journal_get_write_access(handle, iloc.bh);
2683 EXT4_ERROR_INODE(inode, "write access (error %d)",
2688 header = IHDR(inode, ext4_raw_inode(&iloc));
2689 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2690 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2692 false /* block_csum */,
2695 false /* skip_quota */);
2698 if (EXT4_I(inode)->i_file_acl) {
2699 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
2701 EXT4_ERROR_INODE(inode, "block %llu read error",
2702 EXT4_I(inode)->i_file_acl);
2706 error = ext4_xattr_check_block(inode, bh);
2708 EXT4_ERROR_INODE(inode, "bad block %llu (error %d)",
2709 EXT4_I(inode)->i_file_acl, error);
2713 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2714 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2715 entry = EXT4_XATTR_NEXT(entry))
2716 if (entry->e_value_inum)
2717 ext4_xattr_inode_free_quota(inode,
2718 le32_to_cpu(entry->e_value_size));
2722 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2725 * Update i_file_acl value in the same transaction that releases
2728 EXT4_I(inode)->i_file_acl = 0;
2729 error = ext4_mark_inode_dirty(handle, inode);
2731 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2743 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
2747 if (ea_inode_array == NULL)
2750 for (idx = 0; idx < ea_inode_array->count; ++idx)
2751 iput(ea_inode_array->inodes[idx]);
2752 kfree(ea_inode_array);
2756 * ext4_xattr_block_cache_insert()
2758 * Create a new entry in the extended attribute block cache, and insert
2759 * it unless such an entry is already in the cache.
2761 * Returns 0, or a negative error number on failure.
2764 ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
2765 struct buffer_head *bh)
2767 struct ext4_xattr_header *header = BHDR(bh);
2768 __u32 hash = le32_to_cpu(header->h_hash);
2769 int reusable = le32_to_cpu(header->h_refcount) <
2770 EXT4_XATTR_REFCOUNT_MAX;
2773 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
2774 bh->b_blocknr, reusable);
2776 if (error == -EBUSY)
2777 ea_bdebug(bh, "already in cache");
2779 ea_bdebug(bh, "inserting [%x]", (int)hash);
2785 * Compare two extended attribute blocks for equality.
2787 * Returns 0 if the blocks are equal, 1 if they differ, and
2788 * a negative error number on errors.
2791 ext4_xattr_cmp(struct ext4_xattr_header *header1,
2792 struct ext4_xattr_header *header2)
2794 struct ext4_xattr_entry *entry1, *entry2;
2796 entry1 = ENTRY(header1+1);
2797 entry2 = ENTRY(header2+1);
2798 while (!IS_LAST_ENTRY(entry1)) {
2799 if (IS_LAST_ENTRY(entry2))
2801 if (entry1->e_hash != entry2->e_hash ||
2802 entry1->e_name_index != entry2->e_name_index ||
2803 entry1->e_name_len != entry2->e_name_len ||
2804 entry1->e_value_size != entry2->e_value_size ||
2805 entry1->e_value_inum != entry2->e_value_inum ||
2806 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
2808 if (!entry1->e_value_inum &&
2809 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
2810 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
2811 le32_to_cpu(entry1->e_value_size)))
2814 entry1 = EXT4_XATTR_NEXT(entry1);
2815 entry2 = EXT4_XATTR_NEXT(entry2);
2817 if (!IS_LAST_ENTRY(entry2))
2823 * ext4_xattr_block_cache_find()
2825 * Find an identical extended attribute block.
2827 * Returns a pointer to the block found, or NULL if such a block was
2828 * not found or an error occurred.
2830 static struct buffer_head *
2831 ext4_xattr_block_cache_find(struct inode *inode,
2832 struct ext4_xattr_header *header,
2833 struct mb_cache_entry **pce)
2835 __u32 hash = le32_to_cpu(header->h_hash);
2836 struct mb_cache_entry *ce;
2837 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
2839 if (!header->h_hash)
2840 return NULL; /* never share */
2841 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
2842 ce = mb_cache_entry_find_first(ea_block_cache, hash);
2844 struct buffer_head *bh;
2846 bh = sb_bread(inode->i_sb, ce->e_value);
2848 EXT4_ERROR_INODE(inode, "block %lu read error",
2849 (unsigned long)ce->e_value);
2850 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
2855 ce = mb_cache_entry_find_next(ea_block_cache, ce);
2860 #define NAME_HASH_SHIFT 5
2861 #define VALUE_HASH_SHIFT 16
2864 * ext4_xattr_hash_entry()
2866 * Compute the hash of an extended attribute.
2868 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
2869 struct ext4_xattr_entry *entry)
2872 char *name = entry->e_name;
2875 for (n = 0; n < entry->e_name_len; n++) {
2876 hash = (hash << NAME_HASH_SHIFT) ^
2877 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
2881 if (!entry->e_value_inum && entry->e_value_size) {
2882 __le32 *value = (__le32 *)((char *)header +
2883 le16_to_cpu(entry->e_value_offs));
2884 for (n = (le32_to_cpu(entry->e_value_size) +
2885 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
2886 hash = (hash << VALUE_HASH_SHIFT) ^
2887 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
2888 le32_to_cpu(*value++);
2891 entry->e_hash = cpu_to_le32(hash);
2894 #undef NAME_HASH_SHIFT
2895 #undef VALUE_HASH_SHIFT
2897 #define BLOCK_HASH_SHIFT 16
2900 * ext4_xattr_rehash()
2902 * Re-compute the extended attribute hash value after an entry has changed.
2904 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
2905 struct ext4_xattr_entry *entry)
2907 struct ext4_xattr_entry *here;
2910 ext4_xattr_hash_entry(header, entry);
2911 here = ENTRY(header+1);
2912 while (!IS_LAST_ENTRY(here)) {
2913 if (!here->e_hash) {
2914 /* Block is not shared if an entry's hash value == 0 */
2918 hash = (hash << BLOCK_HASH_SHIFT) ^
2919 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
2920 le32_to_cpu(here->e_hash);
2921 here = EXT4_XATTR_NEXT(here);
2923 header->h_hash = cpu_to_le32(hash);
2926 #undef BLOCK_HASH_SHIFT
2928 #define HASH_BUCKET_BITS 10
2931 ext4_xattr_create_cache(void)
2933 return mb_cache_create(HASH_BUCKET_BITS);
2936 void ext4_xattr_destroy_cache(struct mb_cache *cache)
2939 mb_cache_destroy(cache);