Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * linux/fs/ext4/xattr.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Fix by Harrison Xing <harrison@mountainviewdata.com>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * Extended attributes for symlinks and special files added per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *  suggestion of Luka Renko <luka.renko@hermes.si>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *  Red Hat Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *  and Andreas Gruenbacher <agruen@suse.de>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  * Extended attributes are stored directly in inodes (on file systems with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  * field contains the block number if an inode uses an additional block. All
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  * attributes must fit in the inode and one additional block. Blocks that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  * contain the identical set of attributes may be shared among several inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * Identical blocks are detected by keeping a cache of blocks that have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * recently been accessed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * The attributes in inodes and on blocks have a different header; the entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * are stored in the same format:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  *   +------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  *   | header           |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  *   | entry 1          | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  *   | entry 2          | | growing downwards
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  *   | entry 3          | v
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  *   | four null bytes  |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  *   | . . .            |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  *   | value 1          | ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  *   | value 3          | | growing upwards
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  *   | value 2          | |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  *   +------------------+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * The header is followed by multiple entry descriptors. In disk blocks, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * entry descriptors are kept sorted. In inodes, they are unsorted. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * attribute values are aligned to the end of the block in no specific order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * Locking strategy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  * ----------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  * EA blocks are only changed if they are exclusive to an inode, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * holding xattr_sem also means that nothing but the EA block's reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * count can change. Multiple writers to the same block are synchronized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * by the buffer lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #include <linux/mbcache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) #include "ext4_jbd2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #include "ext4.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #include "acl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #ifdef EXT4_XATTR_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) # define ea_idebug(inode, fmt, ...)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	printk(KERN_DEBUG "inode %s:%lu: " fmt "\n",			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	       inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) # define ea_bdebug(bh, fmt, ...)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	printk(KERN_DEBUG "block %pg:%lu: " fmt "\n",			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	       bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) # define ea_idebug(inode, fmt, ...)	no_printk(fmt, ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) # define ea_bdebug(bh, fmt, ...)	no_printk(fmt, ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) static void ext4_xattr_block_cache_insert(struct mb_cache *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 					  struct buffer_head *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) static struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 			    struct mb_cache_entry **);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 				    size_t value_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) static void ext4_xattr_rehash(struct ext4_xattr_header *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) static const struct xattr_handler * const ext4_xattr_handler_map[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	[EXT4_XATTR_INDEX_USER]		     = &ext4_xattr_user_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #ifdef CONFIG_EXT4_FS_POSIX_ACL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	[EXT4_XATTR_INDEX_POSIX_ACL_ACCESS]  = &posix_acl_access_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	[EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	[EXT4_XATTR_INDEX_TRUSTED]	     = &ext4_xattr_trusted_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) #ifdef CONFIG_EXT4_FS_SECURITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	[EXT4_XATTR_INDEX_SECURITY]	     = &ext4_xattr_security_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	[EXT4_XATTR_INDEX_HURD]		     = &ext4_xattr_hurd_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) const struct xattr_handler *ext4_xattr_handlers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	&ext4_xattr_user_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	&ext4_xattr_trusted_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) #ifdef CONFIG_EXT4_FS_POSIX_ACL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	&posix_acl_access_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	&posix_acl_default_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) #ifdef CONFIG_EXT4_FS_SECURITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	&ext4_xattr_security_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	&ext4_xattr_hurd_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) #define EA_BLOCK_CACHE(inode)	(((struct ext4_sb_info *) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 				inode->i_sb->s_fs_info)->s_ea_block_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) #define EA_INODE_CACHE(inode)	(((struct ext4_sb_info *) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 				inode->i_sb->s_fs_info)->s_ea_inode_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 			struct inode *inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) #ifdef CONFIG_LOCKDEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) void ext4_xattr_inode_set_class(struct inode *ea_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	lockdep_set_subclass(&ea_inode->i_rwsem, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) static __le32 ext4_xattr_block_csum(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 				    sector_t block_nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 				    struct ext4_xattr_header *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	__u32 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	__le64 dsk_block_nr = cpu_to_le64(block_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	__u32 dummy_csum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	int offset = offsetof(struct ext4_xattr_header, h_checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 			   sizeof(dsk_block_nr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	offset += sizeof(dummy_csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 			   EXT4_BLOCK_SIZE(inode->i_sb) - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	return cpu_to_le32(csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) static int ext4_xattr_block_csum_verify(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 					struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	struct ext4_xattr_header *hdr = BHDR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	int ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	if (ext4_has_metadata_csum(inode->i_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 							bh->b_blocknr, hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) static void ext4_xattr_block_csum_set(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 				      struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	if (ext4_has_metadata_csum(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 						bh->b_blocknr, BHDR(bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) static inline const struct xattr_handler *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) ext4_xattr_handler(int name_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	const struct xattr_handler *handler = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		handler = ext4_xattr_handler_map[name_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	return handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 			 void *value_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	struct ext4_xattr_entry *e = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	/* Find the end of the names list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	while (!IS_LAST_ENTRY(e)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		if ((void *)next >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 			return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		if (strnlen(e->e_name, e->e_name_len) != e->e_name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 			return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		e = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	/* Check the values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	while (!IS_LAST_ENTRY(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		u32 size = le32_to_cpu(entry->e_value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		if (size > EXT4_XATTR_SIZE_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 			return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		if (size != 0 && entry->e_value_inum == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 			u16 offs = le16_to_cpu(entry->e_value_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 			void *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 			 * The value cannot overlap the names, and the value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 			 * with padding cannot extend beyond 'end'.  Check both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 			 * the padded and unpadded sizes, since the size may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 			 * overflow to 0 when adding padding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 			if (offs > end - value_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 				return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 			value = value_start + offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 			if (value < (void *)e + sizeof(u32) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 			    size > end - value ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 			    EXT4_XATTR_SIZE(size) > end - value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 				return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		entry = EXT4_XATTR_NEXT(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) __ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 			 const char *function, unsigned int line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	int error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	    BHDR(bh)->h_blocks != cpu_to_le32(1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	if (buffer_verified(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	error = -EFSBADCRC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	if (!ext4_xattr_block_csum_verify(inode, bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 					 bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		__ext4_error_inode(inode, function, line, 0, -error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 				   "corrupted xattr block %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 				   (unsigned long long) bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		set_buffer_verified(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) #define ext4_xattr_check_block(inode, bh) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	__ext4_xattr_check_block((inode), (bh),  __func__, __LINE__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 			 void *end, const char *function, unsigned int line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	int error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	    (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		__ext4_error_inode(inode, function, line, 0, -error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 				   "corrupted in-inode xattr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) #define xattr_check_inode(inode, header, end) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	__xattr_check_inode((inode), (header), (end), __func__, __LINE__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		 void *end, int name_index, const char *name, int sorted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	struct ext4_xattr_entry *entry, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	size_t name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	int cmp = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	if (name == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	name_len = strlen(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		next = EXT4_XATTR_NEXT(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		if ((void *) next >= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 			EXT4_ERROR_INODE(inode, "corrupted xattr entries");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 			return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		cmp = name_index - entry->e_name_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		if (!cmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 			cmp = name_len - entry->e_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		if (!cmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 			cmp = memcmp(name, entry->e_name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		if (cmp <= 0 && (sorted || cmp == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	*pentry = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	return cmp ? -ENODATA : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) static u32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	return ((u64)ea_inode->i_ctime.tv_sec << 32) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		(u32) inode_peek_iversion_raw(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	return (u32)ea_inode->i_atime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	ea_inode->i_atime.tv_sec = hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340)  * Read the EA value from an inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	int blocksize = 1 << ea_inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	int tail_size = (size % blocksize) ?: blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	struct buffer_head *bhs_inline[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	struct buffer_head **bhs = bhs_inline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	if (bh_count > ARRAY_SIZE(bhs_inline)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		if (!bhs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 			       true /* wait */, bhs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		goto free_bhs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	for (i = 0; i < bh_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		/* There shouldn't be any holes in ea_inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		if (!bhs[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 			ret = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 			goto put_bhs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		       i < bh_count - 1 ? blocksize : tail_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) put_bhs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	for (i = 0; i < bh_count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		brelse(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) free_bhs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	if (bhs != bhs_inline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		kfree(bhs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 				 u32 ea_inode_hash, struct inode **ea_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	inode = ext4_iget(parent->i_sb, ea_ino, EXT4_IGET_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		ext4_error(parent->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 			   "error while reading EA inode %lu err=%d", ea_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 			   err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	if (is_bad_inode(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		ext4_error(parent->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 			   "error while reading EA inode %lu is_bad_inode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 			   ea_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		ext4_error(parent->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 			   "EA inode %lu does not have EXT4_EA_INODE_FL flag",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 			    ea_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	ext4_xattr_inode_set_class(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	 * Check whether this is an old Lustre-style xattr inode. Lustre
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	 * implementation does not have hash validation, rather it has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	 * backpointer from ea_inode to the parent inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	    EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	    inode->i_generation == parent->i_generation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		ext4_xattr_inode_set_ref(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		inode->i_flags |= S_NOQUOTA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	*ea_inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 			       struct ext4_xattr_entry *entry, void *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 			       size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	/* Verify stored hash matches calculated hash. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	if (hash != ext4_xattr_inode_get_hash(ea_inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	if (entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		__le32 e_hash, tmp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		/* Verify entry hash. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		tmp_data = cpu_to_le32(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 					       &tmp_data, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		if (e_hash != entry->e_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 			return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465)  * Read xattr value from the EA inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		     void *buffer, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	struct inode *ea_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 				    le32_to_cpu(entry->e_hash), &ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		ea_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	if (i_size_read(ea_inode) != size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		ext4_warning_inode(ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 				   "ea_inode file size=%llu entry size=%zu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 				   i_size_read(ea_inode), size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	err = ext4_xattr_inode_read(ea_inode, buffer, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 						     size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 			ext4_warning_inode(ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 					   "EA inode hash validation failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		if (ea_inode_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 			mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 					ext4_xattr_inode_get_hash(ea_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 					ea_inode->i_ino, true /* reusable */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	iput(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		     void *buffer, size_t buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	struct ext4_xattr_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	void *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		  name_index, name, buffer, (long)buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	if (!EXT4_I(inode)->i_file_acl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 		return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	ea_idebug(inode, "reading block %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		  (unsigned long long)EXT4_I(inode)->i_file_acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		return PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	ea_bdebug(bh, "b_count=%d, refcount=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	error = ext4_xattr_check_block(inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	ext4_xattr_block_cache_insert(ea_block_cache, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	entry = BFIRST(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	end = bh->b_data + bh->b_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	error = xattr_find_entry(inode, &entry, end, name_index, name, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	size = le32_to_cpu(entry->e_value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	error = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	if (unlikely(size > EXT4_XATTR_SIZE_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	if (buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		if (size > buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		if (entry->e_value_inum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 			error = ext4_xattr_inode_get(inode, entry, buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 						     size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 				goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 			u16 offset = le16_to_cpu(entry->e_value_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 			void *p = bh->b_data + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 			if (unlikely(p + size > end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 				goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 			memcpy(buffer, p, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	error = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		     void *buffer, size_t buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	struct ext4_xattr_ibody_header *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	struct ext4_xattr_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	struct ext4_inode *raw_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	struct ext4_iloc iloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	void *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	error = ext4_get_inode_loc(inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	raw_inode = ext4_raw_inode(&iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	header = IHDR(inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	error = xattr_check_inode(inode, header, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	entry = IFIRST(header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	error = xattr_find_entry(inode, &entry, end, name_index, name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	size = le32_to_cpu(entry->e_value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	error = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	if (unlikely(size > EXT4_XATTR_SIZE_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	if (buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 		if (size > buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		if (entry->e_value_inum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 			error = ext4_xattr_inode_get(inode, entry, buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 						     size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 				goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			u16 offset = le16_to_cpu(entry->e_value_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 			void *p = (void *)IFIRST(header) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 			if (unlikely(p + size > end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 				goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 			memcpy(buffer, p, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	error = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	brelse(iloc.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629)  * ext4_xattr_get()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631)  * Copy an extended attribute into the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632)  * provided, or compute the buffer size required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633)  * Buffer is NULL to compute the size of the buffer required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635)  * Returns a negative error number on failure, or the number of bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636)  * used / required on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) ext4_xattr_get(struct inode *inode, int name_index, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	       void *buffer, size_t buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	if (strlen(name) > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	down_read(&EXT4_I(inode)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 				     buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	if (error == -ENODATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		error = ext4_xattr_block_get(inode, name_index, name, buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 					     buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	up_read(&EXT4_I(inode)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 			char *buffer, size_t buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	size_t rest = buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		const struct xattr_handler *handler =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 			ext4_xattr_handler(entry->e_name_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		if (handler && (!handler->list || handler->list(dentry))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 			const char *prefix = handler->prefix ?: handler->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 			size_t prefix_len = strlen(prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 			size_t size = prefix_len + entry->e_name_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 			if (buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 				if (size > rest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 					return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 				memcpy(buffer, prefix, prefix_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 				buffer += prefix_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 				memcpy(buffer, entry->e_name, entry->e_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 				buffer += entry->e_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 				*buffer++ = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 			rest -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	return buffer_size - rest;  /* total size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	ea_idebug(inode, "buffer=%p, buffer_size=%ld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		  buffer, (long)buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	if (!EXT4_I(inode)->i_file_acl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	ea_idebug(inode, "reading block %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		  (unsigned long long)EXT4_I(inode)->i_file_acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		return PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	ea_bdebug(bh, "b_count=%d, refcount=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	error = ext4_xattr_check_block(inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 					buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	struct ext4_xattr_ibody_header *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	struct ext4_inode *raw_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	struct ext4_iloc iloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	void *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	error = ext4_get_inode_loc(inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	raw_inode = ext4_raw_inode(&iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	header = IHDR(inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	error = xattr_check_inode(inode, header, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	error = ext4_xattr_list_entries(dentry, IFIRST(header),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 					buffer, buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	brelse(iloc.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750)  * Inode operation listxattr()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752)  * d_inode(dentry)->i_rwsem: don't care
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754)  * Copy a list of attribute names into the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755)  * provided, or compute the buffer size required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756)  * Buffer is NULL to compute the size of the buffer required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758)  * Returns a negative error number on failure, or the number of bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759)  * used / required on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	int ret, ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	if (buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		buffer += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		buffer_size -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	ret += ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784)  * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785)  * not set, set it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) static void ext4_xattr_update_super_block(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 					  struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	if (ext4_has_feature_xattr(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		ext4_set_feature_xattr(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		ext4_handle_dirty_super(handle, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	struct ext4_iloc iloc = { .bh = NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	struct ext4_inode *raw_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	struct ext4_xattr_ibody_header *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	struct ext4_xattr_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	qsize_t ea_inode_refs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	void *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		ret = ext4_get_inode_loc(inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		raw_inode = ext4_raw_inode(&iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		header = IHDR(inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		ret = xattr_check_inode(inode, header, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		     entry = EXT4_XATTR_NEXT(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 			if (entry->e_value_inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 				ea_inode_refs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (EXT4_I(inode)->i_file_acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 			ret = PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 			bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		ret = ext4_xattr_check_block(inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		     entry = EXT4_XATTR_NEXT(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 			if (entry->e_value_inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 				ea_inode_refs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	*usage = ea_inode_refs + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	brelse(iloc.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) static inline size_t round_up_cluster(struct inode *inode, size_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 				    inode->i_blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	size_t mask = ~(cluster_size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	return (length + cluster_size - 1) & mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	err = dquot_alloc_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		dquot_free_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) static void ext4_xattr_inode_free_quota(struct inode *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 					struct inode *ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 					size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	if (ea_inode &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	    ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	dquot_free_inode(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 			     struct buffer_head *block_bh, size_t value_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 			     bool is_create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	int credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	int blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	 * 1) Owner inode update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	 * 2) Ref count update on old xattr block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	 * 3) new xattr block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	 * 4) block bitmap update for new xattr block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	 * 5) group descriptor for new xattr block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	 * 6) block bitmap update for old xattr block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	 * 7) group descriptor for old block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	 * 6 & 7 can happen if we have two racing threads T_a and T_b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	 * which are each trying to set an xattr on inodes I_a and I_b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	 * which were both initially sharing an xattr block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	credits = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	/* Quota updates. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	 * In case of inline data, we may push out the data to a block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	 * so we need to reserve credits for this eventuality
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	if (inode && ext4_has_inline_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		credits += ext4_writepage_trans_blocks(inode) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	/* We are done if ea_inode feature is not enabled. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	if (!ext4_has_feature_ea_inode(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		return credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	/* New ea_inode, inode map, block bitmap, group descriptor. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	credits += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	/* Data blocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	/* Indirection block or one level of extent tree. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	blocks += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	/* Block bitmap and group descriptor updates for each block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	credits += blocks * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	/* Blocks themselves. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	credits += blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	if (!is_create) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		/* Dereference ea_inode holding old xattr value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		 * Old ea_inode, inode map, block bitmap, group descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		credits += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		/* Data blocks for old ea_inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		/* Indirection block or one level of extent tree for old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		 * ea_inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		blocks += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		/* Block bitmap and group descriptor updates for each block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		credits += blocks * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	/* We may need to clone the existing xattr block in which case we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	 * to increment ref counts for existing ea_inodes referenced by it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	if (block_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		struct ext4_xattr_entry *entry = BFIRST(block_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 			if (entry->e_value_inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 				/* Ref count update on ea_inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 				credits += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	return credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 				       int ref_change)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	struct ext4_iloc iloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	s64 ref_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	inode_lock(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	ref_count = ext4_xattr_inode_get_ref(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	ref_count += ref_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	ext4_xattr_inode_set_ref(ea_inode, ref_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	if (ref_change > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 			  ea_inode->i_ino, ref_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		if (ref_count == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 			WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 				  ea_inode->i_ino, ea_inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 			set_nlink(ea_inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			ext4_orphan_del(handle, ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			if (ea_inode_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 				hash = ext4_xattr_inode_get_hash(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 				mb_cache_entry_create(ea_inode_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 						      GFP_NOFS, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 						      ea_inode->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 						      true /* reusable */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 			  ea_inode->i_ino, ref_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		if (ref_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 			WARN_ONCE(ea_inode->i_nlink != 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 				  "EA inode %lu i_nlink=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 				  ea_inode->i_ino, ea_inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 			clear_nlink(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 			ext4_orphan_add(handle, ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 			if (ea_inode_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 				hash = ext4_xattr_inode_get_hash(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 				mb_cache_entry_delete(ea_inode_cache, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 						      ea_inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		ext4_warning_inode(ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 				   "ext4_mark_iloc_dirty() failed ret=%d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	inode_unlock(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 					struct ext4_xattr_entry *first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	struct inode *ea_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	struct ext4_xattr_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	struct ext4_xattr_entry *failed_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	unsigned int ea_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	int err, saved_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	for (entry = first; !IS_LAST_ENTRY(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	     entry = EXT4_XATTR_NEXT(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		if (!entry->e_value_inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		ea_ino = le32_to_cpu(entry->e_value_inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		err = ext4_xattr_inode_iget(parent, ea_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 					    le32_to_cpu(entry->e_hash),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 					    &ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		err = ext4_xattr_inode_inc_ref(handle, ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 			ext4_warning_inode(ea_inode, "inc ref error %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 			iput(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		iput(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	saved_err = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	failed_entry = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	for (entry = first; entry != failed_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	     entry = EXT4_XATTR_NEXT(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		if (!entry->e_value_inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		ea_ino = le32_to_cpu(entry->e_value_inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		err = ext4_xattr_inode_iget(parent, ea_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 					    le32_to_cpu(entry->e_hash),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 					    &ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 			ext4_warning(parent->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 				     "cleanup ea_ino %u iget error %d", ea_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 				     err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		err = ext4_xattr_inode_dec_ref(handle, ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 			ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 					   err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		iput(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	return saved_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) static int ext4_xattr_restart_fn(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			struct buffer_head *bh, bool block_csum, bool dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	if (bh && dirty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		if (block_csum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 			ext4_xattr_block_csum_set(inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		error = ext4_handle_dirty_metadata(handle, NULL, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 			ext4_warning(inode->i_sb, "Handle metadata (error %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 				     error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 			     struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 			     struct ext4_xattr_entry *first, bool block_csum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 			     struct ext4_xattr_inode_array **ea_inode_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 			     int extra_credits, bool skip_quota)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	struct inode *ea_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	struct ext4_xattr_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	bool dirty = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	unsigned int ea_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	int credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	/* One credit for dec ref on ea_inode, one for orphan list addition, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	credits = 2 + extra_credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	for (entry = first; !IS_LAST_ENTRY(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	     entry = EXT4_XATTR_NEXT(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		if (!entry->e_value_inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		ea_ino = le32_to_cpu(entry->e_value_inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		err = ext4_xattr_inode_iget(parent, ea_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 					    le32_to_cpu(entry->e_hash),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 					    &ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		err = ext4_expand_inode_array(ea_inode_array, ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 			ext4_warning_inode(ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 					   "Expand inode array err=%d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 			iput(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		err = ext4_journal_ensure_credits_fn(handle, credits, credits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 			ext4_free_metadata_revoke_credits(parent->i_sb, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 			ext4_xattr_restart_fn(handle, parent, bh, block_csum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 					      dirty));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 			ext4_warning_inode(ea_inode, "Ensure credits err=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 					   err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		if (err > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 			err = ext4_journal_get_write_access(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 				ext4_warning_inode(ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 						"Re-get write access err=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 						err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		err = ext4_xattr_inode_dec_ref(handle, ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 			ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 					   err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		if (!skip_quota)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 			ext4_xattr_inode_free_quota(parent, ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 					      le32_to_cpu(entry->e_value_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		 * Forget about ea_inode within the same transaction that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		 * decrements the ref count. This avoids duplicate decrements in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		 * case the rest of the work spills over to subsequent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		 * transactions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		entry->e_value_inum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		entry->e_value_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	if (dirty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		 * Note that we are deliberately skipping csum calculation for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		 * the final update because we do not expect any journal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		 * restarts until xattr block is freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		err = ext4_handle_dirty_metadata(handle, NULL, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 			ext4_warning_inode(parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 					   "handle dirty metadata err=%d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)  * Release the xattr block BH: If the reference count is > 1, decrement it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)  * otherwise free the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) ext4_xattr_release_block(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 			 struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 			 struct ext4_xattr_inode_array **ea_inode_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 			 int extra_credits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	u32 hash, ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	BUFFER_TRACE(bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	error = ext4_journal_get_write_access(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	hash = le32_to_cpu(BHDR(bh)->h_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	ref = le32_to_cpu(BHDR(bh)->h_refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	if (ref == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		ea_bdebug(bh, "refcount now=0; freeing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		 * This must happen under buffer lock for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		 * ext4_xattr_block_set() to reliably detect freed block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		if (ea_block_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 			mb_cache_entry_delete(ea_block_cache, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 					      bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		get_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		if (ext4_has_feature_ea_inode(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 			ext4_xattr_inode_dec_ref_all(handle, inode, bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 						     BFIRST(bh),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 						     true /* block_csum */,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 						     ea_inode_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 						     extra_credits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 						     true /* skip_quota */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		ext4_free_blocks(handle, inode, bh, 0, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 				 EXT4_FREE_BLOCKS_METADATA |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 				 EXT4_FREE_BLOCKS_FORGET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		ref--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		BHDR(bh)->h_refcount = cpu_to_le32(ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			struct mb_cache_entry *ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			if (ea_block_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 				ce = mb_cache_entry_get(ea_block_cache, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 							bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 				if (ce) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 					ce->e_reusable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 					mb_cache_entry_put(ea_block_cache, ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		ext4_xattr_block_csum_set(inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		 * Beware of this ugliness: Releasing of xattr block references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		 * from different inodes can race and so we have to protect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 		 * from a race where someone else frees the block (and releases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		 * its journal_head) before we are done dirtying the buffer. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		 * nojournal mode this race is harmless and we actually cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		 * call ext4_handle_dirty_metadata() with locked buffer as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		 * that function can call sync_dirty_buffer() so for that case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		 * we handle the dirtying after unlocking the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		if (ext4_handle_valid(handle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 			error = ext4_handle_dirty_metadata(handle, inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		if (!ext4_handle_valid(handle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 			error = ext4_handle_dirty_metadata(handle, inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		if (IS_SYNC(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 			ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		ea_bdebug(bh, "refcount now=%d; releasing",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 			  le32_to_cpu(BHDR(bh)->h_refcount));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	ext4_std_error(inode->i_sb, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303)  * Find the available free space for EAs. This also returns the total number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304)  * bytes used by EA entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 				    size_t *min_offs, void *base, int *total)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		if (!last->e_value_inum && last->e_value_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 			size_t offs = le16_to_cpu(last->e_value_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 			if (offs < *min_offs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 				*min_offs = offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 		if (total)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 			*total += EXT4_XATTR_LEN(last->e_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	return (*min_offs - ((void *)last - base) - sizeof(__u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)  * Write the value of the EA in an inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 				  const void *buf, int bufsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	unsigned long block = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	int blocksize = ea_inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	int csize, wsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	int ret = 0, ret2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	int retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	while (ret >= 0 && ret < max_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		struct ext4_map_blocks map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		map.m_lblk = block += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		map.m_len = max_blocks -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		ret = ext4_map_blocks(handle, ea_inode, &map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 				      EXT4_GET_BLOCKS_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		if (ret <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 			ext4_mark_inode_dirty(handle, ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 			if (ret == -ENOSPC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 			    ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 				ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 				goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	block = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	while (wsize < bufsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		csize = (bufsize - wsize) > blocksize ? blocksize :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 								bufsize - wsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		bh = ext4_getblk(handle, ea_inode, block, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 			return PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 			WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 			EXT4_ERROR_INODE(ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 					 "ext4_getblk() return bh = NULL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 		ret = ext4_journal_get_write_access(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		memcpy(bh->b_data, buf, csize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		ext4_handle_dirty_metadata(handle, ea_inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 		buf += csize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		wsize += csize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		block += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	inode_lock(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	i_size_write(ea_inode, wsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	ext4_update_i_disksize(ea_inode, wsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	inode_unlock(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	ret2 = ext4_mark_inode_dirty(handle, ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	if (unlikely(ret2 && !ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		ret = ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400)  * Create an inode to store the value of a large EA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) static struct inode *ext4_xattr_inode_create(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 					     struct inode *inode, u32 hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	struct inode *ea_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	 * Let the next inode be the goal, so we try and allocate the EA inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	 * in the same group, or nearby one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 				  S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 				  EXT4_EA_INODE_FL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	if (!IS_ERR(ea_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		ea_inode->i_op = &ext4_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		ea_inode->i_fop = &ext4_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		ext4_set_aops(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 		ext4_xattr_inode_set_class(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		unlock_new_inode(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		ext4_xattr_inode_set_ref(ea_inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		ext4_xattr_inode_set_hash(ea_inode, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		err = ext4_mark_inode_dirty(handle, ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 			err = ext4_inode_attach_jinode(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 			iput(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 			return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		 * Xattr inodes are shared therefore quota charging is performed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 		 * at a higher level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 		dquot_free_inode(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		dquot_drop(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		inode_lock(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 		ea_inode->i_flags |= S_NOQUOTA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 		inode_unlock(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	return ea_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) static struct inode *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 			    size_t value_len, u32 hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	struct inode *ea_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	struct mb_cache_entry *ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	void *ea_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	if (!ea_inode_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	ce = mb_cache_entry_find_first(ea_inode_cache, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	if (!ce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	WARN_ON_ONCE(ext4_handle_valid(journal_current_handle()) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		     !(current->flags & PF_MEMALLOC_NOFS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	ea_data = kvmalloc(value_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	if (!ea_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 		mb_cache_entry_put(ea_inode_cache, ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	while (ce) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		ea_inode = ext4_iget(inode->i_sb, ce->e_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 				     EXT4_IGET_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		if (!IS_ERR(ea_inode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		    !is_bad_inode(ea_inode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		    (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		    i_size_read(ea_inode) == value_len &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		    !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		    !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 						    value_len) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		    !memcmp(value, ea_data, value_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 			mb_cache_entry_touch(ea_inode_cache, ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 			mb_cache_entry_put(ea_inode_cache, ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 			kvfree(ea_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 			return ea_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		if (!IS_ERR(ea_inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 			iput(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 		ce = mb_cache_entry_find_next(ea_inode_cache, ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	kvfree(ea_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497)  * Add value of the EA in an inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 					  const void *value, size_t value_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 					  struct inode **ret_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	struct inode *ea_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	if (ea_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		err = ext4_xattr_inode_inc_ref(handle, ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 			iput(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		*ret_inode = ea_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	/* Create an inode for the EA value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	ea_inode = ext4_xattr_inode_create(handle, inode, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	if (IS_ERR(ea_inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		return PTR_ERR(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		ext4_xattr_inode_dec_ref(handle, ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 		iput(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	if (EA_INODE_CACHE(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 				      ea_inode->i_ino, true /* reusable */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	*ret_inode = ea_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541)  * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542)  * feature is enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) #define EXT4_XATTR_BLOCK_RESERVE(inode)	min(i_blocksize(inode)/8, 1024U)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 				struct ext4_xattr_search *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 				handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 				bool is_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	struct ext4_xattr_entry *last, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	struct ext4_xattr_entry *here = s->here;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	size_t min_offs = s->end - s->base, name_len = strlen(i->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	int in_inode = i->in_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	struct inode *old_ea_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	struct inode *new_ea_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	size_t old_size, new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	/* Space used by old and new values. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	old_size = (!s->not_found && !here->e_value_inum) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 			EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	 * Optimization for the simple case when old and new values have the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	 * same padded sizes. Not applicable if external inodes are involved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	if (new_size && new_size == old_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		size_t offs = le16_to_cpu(here->e_value_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		void *val = s->base + offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		here->e_value_size = cpu_to_le32(i->value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		if (i->value == EXT4_ZERO_XATTR_VALUE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 			memset(val, 0, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 			memcpy(val, i->value, i->value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 			/* Clear padding bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 			memset(val + i->value_len, 0, new_size - i->value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		goto update_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	/* Compute min_offs and last. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	last = s->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	for (; !IS_LAST_ENTRY(last); last = next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 		next = EXT4_XATTR_NEXT(last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 		if ((void *)next >= s->end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 			EXT4_ERROR_INODE(inode, "corrupted xattr entries");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 			ret = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		if (!last->e_value_inum && last->e_value_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 			size_t offs = le16_to_cpu(last->e_value_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 			if (offs < min_offs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 				min_offs = offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	/* Check whether we have enough space. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	if (i->value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		size_t free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		free = min_offs - ((void *)last - s->base) - sizeof(__u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		if (!s->not_found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 			free += EXT4_XATTR_LEN(name_len) + old_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		if (free < EXT4_XATTR_LEN(name_len) + new_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 			ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		 * If storing the value in an external inode is an option,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 		 * reserve space for xattr entries/names in the external
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		 * attribute block so that a long value does not occupy the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 		 * whole space and prevent futher entries being added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		if (ext4_has_feature_ea_inode(inode->i_sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 		    new_size && is_block &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		    (min_offs + old_size - new_size) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 					EXT4_XATTR_BLOCK_RESERVE(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 			ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	 * Getting access to old and new ea inodes is subject to failures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	 * Finish that work before doing any modifications to the xattr data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	if (!s->not_found && here->e_value_inum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		ret = ext4_xattr_inode_iget(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 					    le32_to_cpu(here->e_value_inum),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 					    le32_to_cpu(here->e_hash),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 					    &old_ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 			old_ea_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	if (i->value && in_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		WARN_ON_ONCE(!i->value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 		ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 						     i->value_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 						     &new_ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 			new_ea_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 			ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	if (old_ea_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 		/* We are ready to release ref count on the old_ea_inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 		ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 			/* Release newly required ref count on new_ea_inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 			if (new_ea_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 				int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 				err = ext4_xattr_inode_dec_ref(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 							       new_ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 				if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 					ext4_warning_inode(new_ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 						  "dec ref new_ea_inode err=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 						  err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 				ext4_xattr_inode_free_quota(inode, new_ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 							    i->value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		ext4_xattr_inode_free_quota(inode, old_ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 					    le32_to_cpu(here->e_value_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	/* No failures allowed past this point. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	if (!s->not_found && here->e_value_size && !here->e_value_inum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		/* Remove the old value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 		void *first_val = s->base + min_offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 		size_t offs = le16_to_cpu(here->e_value_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 		void *val = s->base + offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 		memmove(first_val + old_size, first_val, val - first_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 		memset(first_val, 0, old_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		min_offs += old_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 		/* Adjust all value offsets. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 		last = s->first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		while (!IS_LAST_ENTRY(last)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 			size_t o = le16_to_cpu(last->e_value_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 			if (!last->e_value_inum &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 			    last->e_value_size && o < offs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 				last->e_value_offs = cpu_to_le16(o + old_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 			last = EXT4_XATTR_NEXT(last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	if (!i->value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 		/* Remove old name. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 		size_t size = EXT4_XATTR_LEN(name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		last = ENTRY((void *)last - size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		memmove(here, (void *)here + size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 			(void *)last - (void *)here + sizeof(__u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 		memset(last, 0, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	} else if (s->not_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		/* Insert new name. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 		size_t size = EXT4_XATTR_LEN(name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 		size_t rest = (void *)last - (void *)here + sizeof(__u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		memmove((void *)here + size, here, rest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		memset(here, 0, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		here->e_name_index = i->name_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		here->e_name_len = name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 		memcpy(here->e_name, i->name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 		/* This is an update, reset value info. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		here->e_value_inum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 		here->e_value_offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		here->e_value_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	if (i->value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 		/* Insert new value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 		if (in_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 			here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		} else if (i->value_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 			void *val = s->base + min_offs - new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 			here->e_value_offs = cpu_to_le16(min_offs - new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 			if (i->value == EXT4_ZERO_XATTR_VALUE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 				memset(val, 0, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 				memcpy(val, i->value, i->value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 				/* Clear padding bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 				memset(val + i->value_len, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 				       new_size - i->value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		here->e_value_size = cpu_to_le32(i->value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) update_hash:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	if (i->value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 		__le32 hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 		/* Entry hash calculation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 		if (in_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 			__le32 crc32c_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 			 * Feed crc32c hash instead of the raw value for entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 			 * hash calculation. This is to avoid walking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 			 * potentially long value buffer again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 			crc32c_hash = cpu_to_le32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 				       ext4_xattr_inode_get_hash(new_ea_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 			hash = ext4_xattr_hash_entry(here->e_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 						     here->e_name_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 						     &crc32c_hash, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 		} else if (is_block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 			__le32 *value = s->base + le16_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 							here->e_value_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 			hash = ext4_xattr_hash_entry(here->e_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 						     here->e_name_len, value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 						     new_size >> 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 		here->e_hash = hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	if (is_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	iput(old_ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	iput(new_ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) struct ext4_xattr_block_find {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	struct ext4_xattr_search s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		      struct ext4_xattr_block_find *bs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		  i->name_index, i->name, i->value, (long)i->value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	if (EXT4_I(inode)->i_file_acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		/* The inode already has an extended attribute block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		bs->bh = ext4_sb_bread(sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 		if (IS_ERR(bs->bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 			error = PTR_ERR(bs->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 			bs->bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 			atomic_read(&(bs->bh->b_count)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 			le32_to_cpu(BHDR(bs->bh)->h_refcount));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		error = ext4_xattr_check_block(inode, bs->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		/* Find the named attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 		bs->s.base = BHDR(bs->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 		bs->s.first = BFIRST(bs->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 		bs->s.end = bs->bh->b_data + bs->bh->b_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 		bs->s.here = bs->s.first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		error = xattr_find_entry(inode, &bs->s.here, bs->s.end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 					 i->name_index, i->name, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 		if (error && error != -ENODATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		bs->s.not_found = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) ext4_xattr_block_set(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 		     struct ext4_xattr_info *i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		     struct ext4_xattr_block_find *bs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	struct buffer_head *new_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	struct ext4_xattr_search s_copy = bs->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	struct ext4_xattr_search *s = &s_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	struct mb_cache_entry *ce = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	struct inode *ea_inode = NULL, *tmp_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	size_t old_ea_inode_quota = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	unsigned int ea_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) #define header(x) ((struct ext4_xattr_header *)(x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	if (s->base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		BUFFER_TRACE(bs->bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 		error = ext4_journal_get_write_access(handle, bs->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 		lock_buffer(bs->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 		if (header(s->base)->h_refcount == cpu_to_le32(1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 			__u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 			 * This must happen under buffer lock for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 			 * ext4_xattr_block_set() to reliably detect modified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 			 * block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 			if (ea_block_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 				mb_cache_entry_delete(ea_block_cache, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 						      bs->bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 			ea_bdebug(bs->bh, "modifying in-place");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 			error = ext4_xattr_set_entry(i, s, handle, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 						     true /* is_block */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 			ext4_xattr_block_csum_set(inode, bs->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 			unlock_buffer(bs->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 			if (error == -EFSCORRUPTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 				goto bad_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 			if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 				error = ext4_handle_dirty_metadata(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 								   inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 								   bs->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 				goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 			goto inserted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 			int offset = (char *)s->here - bs->bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 			unlock_buffer(bs->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 			ea_bdebug(bs->bh, "cloning");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 			s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 			error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 			if (s->base == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 				goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 			memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 			s->first = ENTRY(header(s->base)+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 			header(s->base)->h_refcount = cpu_to_le32(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 			s->here = ENTRY(s->base + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 			s->end = s->base + bs->bh->b_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 			 * If existing entry points to an xattr inode, we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 			 * to prevent ext4_xattr_set_entry() from decrementing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 			 * ref count on it because the reference belongs to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 			 * original block. In this case, make the entry look
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 			 * like it has an empty value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 			if (!s->not_found && s->here->e_value_inum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 				ea_ino = le32_to_cpu(s->here->e_value_inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 				error = ext4_xattr_inode_iget(inode, ea_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 					      le32_to_cpu(s->here->e_hash),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 					      &tmp_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 				if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 					goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 				if (!ext4_test_inode_state(tmp_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 						EXT4_STATE_LUSTRE_EA_INODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 					/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 					 * Defer quota free call for previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 					 * inode until success is guaranteed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 					old_ea_inode_quota = le32_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 							s->here->e_value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 				iput(tmp_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 				s->here->e_value_inum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 				s->here->e_value_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 		/* Allocate a buffer where we construct the new block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 		s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 		/* assert(header == s->base) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 		error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 		if (s->base == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 		header(s->base)->h_blocks = cpu_to_le32(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 		header(s->base)->h_refcount = cpu_to_le32(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		s->first = ENTRY(header(s->base)+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 		s->here = ENTRY(header(s->base)+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		s->end = s->base + sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	if (error == -EFSCORRUPTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 		goto bad_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	if (i->value && s->here->e_value_inum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 		 * A ref count on ea_inode has been taken as part of the call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 		 * ext4_xattr_set_entry() above. We would like to drop this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 		 * extra ref but we have to wait until the xattr block is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 		 * initialized and has its own ref count on the ea_inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 		ea_ino = le32_to_cpu(s->here->e_value_inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 		error = ext4_xattr_inode_iget(inode, ea_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 					      le32_to_cpu(s->here->e_hash),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 					      &ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 			ea_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) inserted:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	if (!IS_LAST_ENTRY(s->first)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 						     &ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		if (new_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 			/* We found an identical block in the cache. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 			if (new_bh == bs->bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 				ea_bdebug(new_bh, "keeping");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 				u32 ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 				WARN_ON_ONCE(dquot_initialize_needed(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 				/* The old block is released after updating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 				   the inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 				error = dquot_alloc_block(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 						EXT4_C2B(EXT4_SB(sb), 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 				if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 					goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 				BUFFER_TRACE(new_bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 				error = ext4_journal_get_write_access(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 								      new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 				if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 					goto cleanup_dquot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 				lock_buffer(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 				 * We have to be careful about races with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 				 * freeing, rehashing or adding references to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 				 * xattr block. Once we hold buffer lock xattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 				 * block's state is stable so we can check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 				 * whether the block got freed / rehashed or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 				 * not.  Since we unhash mbcache entry under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 				 * buffer lock when freeing / rehashing xattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 				 * block, checking whether entry is still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 				 * hashed is reliable. Same rules hold for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 				 * e_reusable handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 				if (hlist_bl_unhashed(&ce->e_hash_list) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 				    !ce->e_reusable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 					/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 					 * Undo everything and check mbcache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 					 * again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 					unlock_buffer(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 					dquot_free_block(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 							 EXT4_C2B(EXT4_SB(sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 								  1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 					brelse(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 					mb_cache_entry_put(ea_block_cache, ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 					ce = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 					new_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 					goto inserted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 				ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 				BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 				if (ref >= EXT4_XATTR_REFCOUNT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 					ce->e_reusable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 				ea_bdebug(new_bh, "reusing; refcount now=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 					  ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 				ext4_xattr_block_csum_set(inode, new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 				unlock_buffer(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 				error = ext4_handle_dirty_metadata(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 								   inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 								   new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 				if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 					goto cleanup_dquot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 			mb_cache_entry_touch(ea_block_cache, ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 			mb_cache_entry_put(ea_block_cache, ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 			ce = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		} else if (bs->bh && s->base == bs->bh->b_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 			/* We were modifying this block in-place. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 			ea_bdebug(bs->bh, "keeping this block");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 			ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 			new_bh = bs->bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 			get_bh(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 			/* We need to allocate a new block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 			ext4_fsblk_t goal, block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 			WARN_ON_ONCE(dquot_initialize_needed(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 			goal = ext4_group_first_block_no(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 						EXT4_I(inode)->i_block_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 			/* non-extent files can't have physical blocks past 2^32 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 			if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 				goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 			block = ext4_new_meta_blocks(handle, inode, goal, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 						     NULL, &error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 				goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 			if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 				BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 			ea_idebug(inode, "creating block %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 				  (unsigned long long)block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 			new_bh = sb_getblk(sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 			if (unlikely(!new_bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 				error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) getblk_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 				ext4_free_blocks(handle, inode, NULL, block, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 						 EXT4_FREE_BLOCKS_METADATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 				goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 			error = ext4_xattr_inode_inc_ref_all(handle, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 						      ENTRY(header(s->base)+1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 				goto getblk_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 			if (ea_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 				/* Drop the extra ref on ea_inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 				error = ext4_xattr_inode_dec_ref(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 								 ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 				if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 					ext4_warning_inode(ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 							   "dec ref error=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 							   error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 				iput(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 				ea_inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 			lock_buffer(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 			error = ext4_journal_get_create_access(handle, new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 			if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 				unlock_buffer(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 				error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 				goto getblk_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 			memcpy(new_bh->b_data, s->base, new_bh->b_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 			ext4_xattr_block_csum_set(inode, new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 			set_buffer_uptodate(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 			unlock_buffer(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 			ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 			error = ext4_handle_dirty_metadata(handle, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 							   new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 				goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	if (old_ea_inode_quota)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 		ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	/* Update the inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	/* Drop the previous xattr block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	if (bs->bh && bs->bh != new_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 		struct ext4_xattr_inode_array *ea_inode_array = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 		ext4_xattr_release_block(handle, inode, bs->bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 					 &ea_inode_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 					 0 /* extra_credits */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 		ext4_xattr_inode_array_free(ea_inode_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	if (ea_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 		int error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 		error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 		if (error2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 			ext4_warning_inode(ea_inode, "dec ref error=%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 					   error2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 		/* If there was an error, revert the quota charge. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 			ext4_xattr_inode_free_quota(inode, ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 						    i_size_read(ea_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 		iput(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	if (ce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 		mb_cache_entry_put(ea_block_cache, ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	brelse(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 	if (!(bs->bh && s->base == bs->bh->b_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 		kfree(s->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) cleanup_dquot:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) bad_block:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	EXT4_ERROR_INODE(inode, "bad block %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 			 EXT4_I(inode)->i_file_acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) #undef header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 			  struct ext4_xattr_ibody_find *is)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	struct ext4_xattr_ibody_header *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	struct ext4_inode *raw_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	if (EXT4_I(inode)->i_extra_isize == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 	raw_inode = ext4_raw_inode(&is->iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 	header = IHDR(inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	is->s.base = is->s.first = IFIRST(header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	is->s.here = is->s.first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 	if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 		error = xattr_check_inode(inode, header, is->s.end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 		/* Find the named attribute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 		error = xattr_find_entry(inode, &is->s.here, is->s.end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 					 i->name_index, i->name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 		if (error && error != -ENODATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 		is->s.not_found = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 				struct ext4_xattr_info *i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 				struct ext4_xattr_ibody_find *is)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 	struct ext4_xattr_ibody_header *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 	struct ext4_xattr_search *s = &is->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 	if (EXT4_I(inode)->i_extra_isize == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 	error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 	header = IHDR(inode, ext4_raw_inode(&is->iloc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	if (!IS_LAST_ENTRY(s->first)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 		header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 		ext4_set_inode_state(inode, EXT4_STATE_XATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 		header->h_magic = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 		ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 				struct ext4_xattr_info *i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 				struct ext4_xattr_ibody_find *is)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	struct ext4_xattr_ibody_header *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	struct ext4_xattr_search *s = &is->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	if (EXT4_I(inode)->i_extra_isize == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	header = IHDR(inode, ext4_raw_inode(&is->iloc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	if (!IS_LAST_ENTRY(s->first)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 		header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 		ext4_set_inode_state(inode, EXT4_STATE_XATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 		header->h_magic = cpu_to_le32(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 		ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) static int ext4_xattr_value_same(struct ext4_xattr_search *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 				 struct ext4_xattr_info *i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 	void *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	/* When e_value_inum is set the value is stored externally. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 	if (s->here->e_value_inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 	if (le32_to_cpu(s->here->e_value_size) != i->value_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 	value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 	return !memcmp(value, i->value, i->value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 	if (!EXT4_I(inode)->i_file_acl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 	if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 		return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 	error = ext4_xattr_check_block(inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272)  * ext4_xattr_set_handle()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274)  * Create, replace or remove an extended attribute for this inode.  Value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275)  * is NULL to remove an existing extended attribute, and non-NULL to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276)  * either replace an existing extended attribute, or create a new extended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277)  * attribute. The flags XATTR_REPLACE and XATTR_CREATE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278)  * specify that an extended attribute must exist and must not exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279)  * previous to the call, respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281)  * Returns 0, or a negative error number on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 		      const char *name, const void *value, size_t value_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 		      int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 	struct ext4_xattr_info i = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 		.name_index = name_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 		.name = name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 		.value = value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 		.value_len = value_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 		.in_inode = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 	struct ext4_xattr_ibody_find is = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 		.s = { .not_found = -ENODATA, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	struct ext4_xattr_block_find bs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 		.s = { .not_found = -ENODATA, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 	int no_expand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	if (strlen(name) > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 		return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	ext4_write_lock_xattr(inode, &no_expand);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	/* Check journal credits under write lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 	if (ext4_handle_valid(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 		struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 		int credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 		bh = ext4_xattr_get_block(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 		if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 			error = PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 		credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 						   value_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 						   flags & XATTR_CREATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 		if (jbd2_handle_buffer_credits(handle) < credits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 			error = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 		WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	error = ext4_reserve_inode_write(handle, inode, &is.iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 		struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 		memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 		ext4_clear_inode_state(inode, EXT4_STATE_NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 	error = ext4_xattr_ibody_find(inode, &i, &is);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 	if (is.s.not_found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 		error = ext4_xattr_block_find(inode, &i, &bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	if (is.s.not_found && bs.s.not_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 		error = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 		if (flags & XATTR_REPLACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 		error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 		if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 		error = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 		if (flags & XATTR_CREATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	if (!value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 		if (!is.s.not_found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 			error = ext4_xattr_ibody_set(handle, inode, &i, &is);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 		else if (!bs.s.not_found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 			error = ext4_xattr_block_set(handle, inode, &i, &bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 		error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 		/* Xattr value did not change? Save us some work and bail out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 		if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 		if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 		if (ext4_has_feature_ea_inode(inode->i_sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 		    (EXT4_XATTR_SIZE(i.value_len) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 			EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 			i.in_inode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) retry_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 		error = ext4_xattr_ibody_set(handle, inode, &i, &is);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 		if (!error && !bs.s.not_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 			i.value = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 			error = ext4_xattr_block_set(handle, inode, &i, &bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 		} else if (error == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 			if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 				brelse(bs.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 				bs.bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 				error = ext4_xattr_block_find(inode, &i, &bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 				if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 					goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 			error = ext4_xattr_block_set(handle, inode, &i, &bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 			if (!error && !is.s.not_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 				i.value = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 				error = ext4_xattr_ibody_set(handle, inode, &i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 							     &is);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 			} else if (error == -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 				 * Xattr does not fit in the block, store at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 				 * external inode if possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 				if (ext4_has_feature_ea_inode(inode->i_sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 				    i.value_len && !i.in_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 					i.in_inode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 					goto retry_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 		ext4_xattr_update_super_block(handle, inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 		inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 		if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 			no_expand = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 		error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 		 * The bh is consumed by ext4_mark_iloc_dirty, even with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 		 * error != 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 		is.iloc.bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 		if (IS_SYNC(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 			ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 	ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 	brelse(is.iloc.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	brelse(bs.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 	ext4_write_unlock_xattr(inode, &no_expand);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 			   bool is_create, int *credits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 	*credits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 	if (!EXT4_SB(inode->i_sb)->s_journal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 	down_read(&EXT4_I(inode)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 	bh = ext4_xattr_get_block(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 	if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 		err = PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 		*credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 						    value_len, is_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 	up_read(&EXT4_I(inode)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463)  * ext4_xattr_set()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465)  * Like ext4_xattr_set_handle, but start from an inode. This extended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466)  * attribute modification is a filesystem transaction by itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468)  * Returns 0, or a negative error number on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) ext4_xattr_set(struct inode *inode, int name_index, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	       const void *value, size_t value_len, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	int error, retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	int credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	error = dquot_initialize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 				       &credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 	handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 		error = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 		int error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 		error = ext4_xattr_set_handle(handle, inode, name_index, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 					      value, value_len, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 		error2 = ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 		if (error == -ENOSPC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 		    ext4_should_retry_alloc(sb, &retries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 		if (error == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 			error = error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 	ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510)  * Shift the EA entries in the inode to create space for the increased
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511)  * i_extra_isize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 				     int value_offs_shift, void *to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 				     void *from, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	struct ext4_xattr_entry *last = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 	int new_offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 	/* We always shift xattr headers further thus offsets get lower */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 	BUG_ON(value_offs_shift > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	/* Adjust the value offsets of the entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 		if (!last->e_value_inum && last->e_value_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 			new_offs = le16_to_cpu(last->e_value_offs) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 							value_offs_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 			last->e_value_offs = cpu_to_le16(new_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	/* Shift the entries by n bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	memmove(to, from, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536)  * Move xattr pointed to by 'entry' from inode into external xattr block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 				    struct ext4_inode *raw_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 				    struct ext4_xattr_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 	struct ext4_xattr_ibody_find *is = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 	struct ext4_xattr_block_find *bs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 	char *buffer = NULL, *b_entry_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 	size_t value_size = le32_to_cpu(entry->e_value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 	struct ext4_xattr_info i = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 		.value = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 		.value_len = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 		.name_index = entry->e_name_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 		.in_inode = !!entry->e_value_inum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 	is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 	bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 	buffer = kmalloc(value_size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 	b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 	if (!is || !bs || !buffer || !b_entry_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 		error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 	is->s.not_found = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 	bs->s.not_found = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 	is->iloc.bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 	bs->bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 	/* Save the entry name and the entry value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 	if (entry->e_value_inum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 		error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 		size_t value_offs = le16_to_cpu(entry->e_value_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 		memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 	memcpy(b_entry_name, entry->e_name, entry->e_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 	b_entry_name[entry->e_name_len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 	i.name = b_entry_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 	error = ext4_get_inode_loc(inode, &is->iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 	error = ext4_xattr_ibody_find(inode, &i, is);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 	/* Remove the chosen entry from the inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 	error = ext4_xattr_ibody_set(handle, inode, &i, is);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	i.value = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	i.value_len = value_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 	error = ext4_xattr_block_find(inode, &i, bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	/* Add entry which was removed from the inode into the block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 	error = ext4_xattr_block_set(handle, inode, &i, bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 	error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	kfree(b_entry_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 	if (is)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 		brelse(is->iloc.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 	if (bs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 		brelse(bs->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 	kfree(is);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 	kfree(bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 				       struct ext4_inode *raw_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 				       int isize_diff, size_t ifree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 				       size_t bfree, int *total_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 	struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 	struct ext4_xattr_entry *small_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	struct ext4_xattr_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	struct ext4_xattr_entry *last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 	unsigned int entry_size;	/* EA entry size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 	unsigned int total_size;	/* EA entry size + value size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 	unsigned int min_total_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 	while (isize_diff > ifree) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 		entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 		small_entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 		min_total_size = ~0U;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 		last = IFIRST(header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 		/* Find the entry best suited to be pushed into EA block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 		for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 			/* never move system.data out of the inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 			if ((last->e_name_len == 4) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 			    (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 			    !memcmp(last->e_name, "data", 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 			total_size = EXT4_XATTR_LEN(last->e_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 			if (!last->e_value_inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 				total_size += EXT4_XATTR_SIZE(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 					       le32_to_cpu(last->e_value_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 			if (total_size <= bfree &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 			    total_size < min_total_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 				if (total_size + ifree < isize_diff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 					small_entry = last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 					entry = last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 					min_total_size = total_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 		if (entry == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 			if (small_entry == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 				return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 			entry = small_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 		entry_size = EXT4_XATTR_LEN(entry->e_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 		total_size = entry_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 		if (!entry->e_value_inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 			total_size += EXT4_XATTR_SIZE(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 					      le32_to_cpu(entry->e_value_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 		error = ext4_xattr_move_to_block(handle, inode, raw_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 						 entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 		*total_ino -= entry_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 		ifree += total_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 		bfree -= total_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686)  * Expand an inode by new_extra_isize bytes when EAs are present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687)  * Returns 0 on success or negative error number on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 			       struct ext4_inode *raw_inode, handle_t *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 	struct ext4_xattr_ibody_header *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 	static unsigned int mnt_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 	size_t min_offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 	size_t ifree, bfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	int total_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	void *base, *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	int error = 0, tried_min_extra_isize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 	int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 	int isize_diff;	/* How much do we need to grow i_extra_isize */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 	isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 	if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 	header = IHDR(inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 	 * Check if enough free space is available in the inode to shift the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 	 * entries ahead by new_extra_isize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 	base = IFIRST(header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 	end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 	min_offs = end - base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 	total_ino = sizeof(struct ext4_xattr_ibody_header) + sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 	error = xattr_check_inode(inode, header, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 	ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 	if (ifree >= isize_diff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 		goto shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 	 * Enough free space isn't available in the inode, check if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	 * EA block can hold new_extra_isize bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 	if (EXT4_I(inode)->i_file_acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 		struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 		bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 		if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 			error = PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 		error = ext4_xattr_check_block(inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 		base = BHDR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 		end = bh->b_data + bh->b_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 		min_offs = end - base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 		bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 					      NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 		if (bfree + ifree < isize_diff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 			if (!tried_min_extra_isize && s_min_extra_isize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 				tried_min_extra_isize++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 				new_extra_isize = s_min_extra_isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 				goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 			error = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 		bfree = inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 	error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 					    isize_diff, ifree, bfree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 					    &total_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 		if (error == -ENOSPC && !tried_min_extra_isize &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 		    s_min_extra_isize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 			tried_min_extra_isize++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 			new_extra_isize = s_min_extra_isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 			goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) shift:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 	/* Adjust the offsets and shift the remaining entries ahead */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 	ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 			- new_extra_isize, (void *)raw_inode +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 			EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 			(void *)header, total_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 	EXT4_I(inode)->i_extra_isize = new_extra_isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 	if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 		ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 			     inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 		mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) #define EIA_INCR 16 /* must be 2^n */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) #define EIA_MASK (EIA_INCR - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) /* Add the large xattr @inode into @ea_inode_array for deferred iput().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797)  * If @ea_inode_array is new or full it will be grown and the old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798)  * contents copied over.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 			struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 	if (*ea_inode_array == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 		 * Start with 15 inodes, so it fits into a power-of-two size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 		 * If *ea_inode_array is NULL, this is essentially offsetof()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 		(*ea_inode_array) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 			kmalloc(offsetof(struct ext4_xattr_inode_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 					 inodes[EIA_MASK]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 				GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 		if (*ea_inode_array == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 		(*ea_inode_array)->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 	} else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 		/* expand the array once all 15 + n * 16 slots are full */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 		struct ext4_xattr_inode_array *new_array = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 		int count = (*ea_inode_array)->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 		/* if new_array is NULL, this is essentially offsetof() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 		new_array = kmalloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 				offsetof(struct ext4_xattr_inode_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 					 inodes[count + EIA_INCR]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 				GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 		if (new_array == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 		memcpy(new_array, *ea_inode_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 		       offsetof(struct ext4_xattr_inode_array, inodes[count]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 		kfree(*ea_inode_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 		*ea_inode_array = new_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 	(*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838)  * ext4_xattr_delete_inode()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840)  * Free extended attribute resources associated with this inode. Traverse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841)  * all entries and decrement reference on any xattr inodes associated with this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842)  * inode. This is called immediately before an inode is freed. We have exclusive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843)  * access to the inode. If an orphan inode is deleted it will also release its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844)  * references on xattr block and xattr inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 			    struct ext4_xattr_inode_array **ea_inode_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 			    int extra_credits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 	struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 	struct ext4_xattr_ibody_header *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 	struct ext4_iloc iloc = { .bh = NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 	struct ext4_xattr_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 	struct inode *ea_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 	error = ext4_journal_ensure_credits(handle, extra_credits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 			ext4_free_metadata_revoke_credits(inode->i_sb, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 	if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 		EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 	if (ext4_has_feature_ea_inode(inode->i_sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 	    ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 		error = ext4_get_inode_loc(inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 			EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 		error = ext4_journal_get_write_access(handle, iloc.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 			EXT4_ERROR_INODE(inode, "write access (error %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 					 error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 		header = IHDR(inode, ext4_raw_inode(&iloc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 		if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 			ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 						     IFIRST(header),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 						     false /* block_csum */,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 						     ea_inode_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 						     extra_credits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 						     false /* skip_quota */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 	if (EXT4_I(inode)->i_file_acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 		bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 		if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 			error = PTR_ERR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 			if (error == -EIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 				EXT4_ERROR_INODE_ERR(inode, EIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 						     "block %llu read error",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 						     EXT4_I(inode)->i_file_acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 			bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 		error = ext4_xattr_check_block(inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 		if (ext4_has_feature_ea_inode(inode->i_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 			for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 			     entry = EXT4_XATTR_NEXT(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 				if (!entry->e_value_inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 				error = ext4_xattr_inode_iget(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 					      le32_to_cpu(entry->e_value_inum),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 					      le32_to_cpu(entry->e_hash),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 					      &ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 				if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 				ext4_xattr_inode_free_quota(inode, ea_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 					      le32_to_cpu(entry->e_value_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 				iput(ea_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 		ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 					 extra_credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 		 * Update i_file_acl value in the same transaction that releases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 		 * block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 		EXT4_I(inode)->i_file_acl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 		error = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 			EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 					 error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 		ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 	error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 	brelse(iloc.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 	int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 	if (ea_inode_array == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 	for (idx = 0; idx < ea_inode_array->count; ++idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 		iput(ea_inode_array->inodes[idx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 	kfree(ea_inode_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959)  * ext4_xattr_block_cache_insert()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961)  * Create a new entry in the extended attribute block cache, and insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962)  * it unless such an entry is already in the cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964)  * Returns 0, or a negative error number on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 			      struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 	struct ext4_xattr_header *header = BHDR(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 	__u32 hash = le32_to_cpu(header->h_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 	int reusable = le32_to_cpu(header->h_refcount) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 		       EXT4_XATTR_REFCOUNT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 	if (!ea_block_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 	error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 				      bh->b_blocknr, reusable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 		if (error == -EBUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 			ea_bdebug(bh, "already in cache");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 		ea_bdebug(bh, "inserting [%x]", (int)hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988)  * ext4_xattr_cmp()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990)  * Compare two extended attribute blocks for equality.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992)  * Returns 0 if the blocks are equal, 1 if they differ, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993)  * a negative error number on errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) ext4_xattr_cmp(struct ext4_xattr_header *header1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 	       struct ext4_xattr_header *header2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 	struct ext4_xattr_entry *entry1, *entry2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 	entry1 = ENTRY(header1+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 	entry2 = ENTRY(header2+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 	while (!IS_LAST_ENTRY(entry1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 		if (IS_LAST_ENTRY(entry2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 		if (entry1->e_hash != entry2->e_hash ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 		    entry1->e_name_index != entry2->e_name_index ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 		    entry1->e_name_len != entry2->e_name_len ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 		    entry1->e_value_size != entry2->e_value_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 		    entry1->e_value_inum != entry2->e_value_inum ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 		    memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 		if (!entry1->e_value_inum &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 		    memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 			   (char *)header2 + le16_to_cpu(entry2->e_value_offs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 			   le32_to_cpu(entry1->e_value_size)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 		entry1 = EXT4_XATTR_NEXT(entry1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 		entry2 = EXT4_XATTR_NEXT(entry2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 	if (!IS_LAST_ENTRY(entry2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028)  * ext4_xattr_block_cache_find()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030)  * Find an identical extended attribute block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032)  * Returns a pointer to the block found, or NULL if such a block was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033)  * not found or an error occurred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) static struct buffer_head *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) ext4_xattr_block_cache_find(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 			    struct ext4_xattr_header *header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 			    struct mb_cache_entry **pce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 	__u32 hash = le32_to_cpu(header->h_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 	struct mb_cache_entry *ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 	struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 	if (!ea_block_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 	if (!header->h_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 		return NULL;  /* never share */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 	ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 	ce = mb_cache_entry_find_first(ea_block_cache, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 	while (ce) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 		struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 		bh = ext4_sb_bread(inode->i_sb, ce->e_value, REQ_PRIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 		if (IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 			if (PTR_ERR(bh) == -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 				return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 			bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 			EXT4_ERROR_INODE(inode, "block %lu read error",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 					 (unsigned long)ce->e_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 		} else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 			*pce = ce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 			return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 		brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 		ce = mb_cache_entry_find_next(ea_block_cache, ce);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) #define NAME_HASH_SHIFT 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) #define VALUE_HASH_SHIFT 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074)  * ext4_xattr_hash_entry()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076)  * Compute the hash of an extended attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 				    size_t value_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 	__u32 hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 	while (name_len--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 		hash = (hash << NAME_HASH_SHIFT) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 		       (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 		       *name++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 	while (value_count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 		hash = (hash << VALUE_HASH_SHIFT) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 		       (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 		       le32_to_cpu(*value++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 	return cpu_to_le32(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) #undef NAME_HASH_SHIFT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) #undef VALUE_HASH_SHIFT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) #define BLOCK_HASH_SHIFT 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102)  * ext4_xattr_rehash()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104)  * Re-compute the extended attribute hash value after an entry has changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) static void ext4_xattr_rehash(struct ext4_xattr_header *header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 	struct ext4_xattr_entry *here;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 	__u32 hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 	here = ENTRY(header+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 	while (!IS_LAST_ENTRY(here)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 		if (!here->e_hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 			/* Block is not shared if an entry's hash value == 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 			hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 		hash = (hash << BLOCK_HASH_SHIFT) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 		       (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 		       le32_to_cpu(here->e_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 		here = EXT4_XATTR_NEXT(here);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 	header->h_hash = cpu_to_le32(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) #undef BLOCK_HASH_SHIFT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) #define	HASH_BUCKET_BITS	10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) struct mb_cache *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) ext4_xattr_create_cache(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 	return mb_cache_create(HASH_BUCKET_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) void ext4_xattr_destroy_cache(struct mb_cache *cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 	if (cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 		mb_cache_destroy(cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141)