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)  * fs/f2fs/xattr.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *             http://www.samsung.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Portions of this code from linux/fs/ext2/xattr.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Fix by Harrison Xing <harrison@mountainviewdata.com>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * Extended attributes for symlinks and special files added per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *  suggestion of Luka Renko <luka.renko@hermes.si>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *  Red Hat Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/rwsem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/f2fs_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/posix_acl_xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "f2fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "segment.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static void *xattr_alloc(struct f2fs_sb_info *sbi, int size, bool *is_inline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	if (likely(size == sbi->inline_xattr_slab_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		*is_inline = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		return kmem_cache_zalloc(sbi->inline_xattr_slab, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	*is_inline = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	return f2fs_kzalloc(sbi, size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static void xattr_free(struct f2fs_sb_info *sbi, void *xattr_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 							bool is_inline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	if (is_inline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		kmem_cache_free(sbi->inline_xattr_slab, xattr_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		kfree(xattr_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		struct dentry *unused, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		const char *name, void *buffer, size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	switch (handler->flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	case F2FS_XATTR_INDEX_USER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		if (!test_opt(sbi, XATTR_USER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	case F2FS_XATTR_INDEX_TRUSTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	case F2FS_XATTR_INDEX_SECURITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return f2fs_getxattr(inode, handler->flags, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			     buffer, size, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		struct dentry *unused, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		const char *name, const void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	switch (handler->flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	case F2FS_XATTR_INDEX_USER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		if (!test_opt(sbi, XATTR_USER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	case F2FS_XATTR_INDEX_TRUSTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	case F2FS_XATTR_INDEX_SECURITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	return f2fs_setxattr(inode, handler->flags, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 					value, size, NULL, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static bool f2fs_xattr_user_list(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	return test_opt(sbi, XATTR_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static bool f2fs_xattr_trusted_list(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	return capable(CAP_SYS_ADMIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		struct dentry *unused, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		const char *name, void *buffer, size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		*((char *)buffer) = F2FS_I(inode)->i_advise;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	return sizeof(char);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		struct dentry *unused, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		const char *name, const void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	unsigned char old_advise = F2FS_I(inode)->i_advise;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	unsigned char new_advise;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (!inode_owner_or_capable(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (value == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	new_advise = *(char *)value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (new_advise & ~FADVISE_MODIFIABLE_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	F2FS_I(inode)->i_advise = new_advise;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	f2fs_mark_inode_dirty_sync(inode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #ifdef CONFIG_F2FS_FS_SECURITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		void *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	const struct xattr *xattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 				xattr->name, xattr->value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 				xattr->value_len, (struct page *)page, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return err;
^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) int f2fs_init_security(struct inode *inode, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				const struct qstr *qstr, struct page *ipage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	return security_inode_init_security(inode, dir, qstr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 				&f2fs_initxattrs, ipage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) const struct xattr_handler f2fs_xattr_user_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	.prefix	= XATTR_USER_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	.flags	= F2FS_XATTR_INDEX_USER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	.list	= f2fs_xattr_user_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	.get	= f2fs_xattr_generic_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	.set	= f2fs_xattr_generic_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) const struct xattr_handler f2fs_xattr_trusted_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	.prefix	= XATTR_TRUSTED_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	.flags	= F2FS_XATTR_INDEX_TRUSTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	.list	= f2fs_xattr_trusted_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	.get	= f2fs_xattr_generic_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	.set	= f2fs_xattr_generic_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) const struct xattr_handler f2fs_xattr_advise_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	.name	= F2FS_SYSTEM_ADVISE_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	.flags	= F2FS_XATTR_INDEX_ADVISE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	.get	= f2fs_xattr_advise_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	.set	= f2fs_xattr_advise_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) const struct xattr_handler f2fs_xattr_security_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	.prefix	= XATTR_SECURITY_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	.flags	= F2FS_XATTR_INDEX_SECURITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	.get	= f2fs_xattr_generic_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	.set	= f2fs_xattr_generic_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static const struct xattr_handler *f2fs_xattr_handler_map[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	[F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) #ifdef CONFIG_F2FS_FS_POSIX_ACL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	[F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	[F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	[F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) #ifdef CONFIG_F2FS_FS_SECURITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	[F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	[F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) const struct xattr_handler *f2fs_xattr_handlers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	&f2fs_xattr_user_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) #ifdef CONFIG_F2FS_FS_POSIX_ACL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	&posix_acl_access_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	&posix_acl_default_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	&f2fs_xattr_trusted_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) #ifdef CONFIG_F2FS_FS_SECURITY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	&f2fs_xattr_security_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	&f2fs_xattr_advise_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static inline const struct xattr_handler *f2fs_xattr_handler(int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	const struct xattr_handler *handler = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		handler = f2fs_xattr_handler_map[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	return handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 				void *last_base_addr, int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 				size_t len, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	struct f2fs_xattr_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	list_for_each_xattr(entry, base_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			(void *)XATTR_NEXT_ENTRY(entry) > last_base_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		if (entry->e_name_index != index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		if (entry->e_name_len != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		if (!memcmp(entry->e_name, name, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 				void *base_addr, void **last_addr, int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 				size_t len, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	struct f2fs_xattr_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	unsigned int inline_size = inline_xattr_size(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	void *max_addr = base_addr + inline_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	list_for_each_xattr(entry, base_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		if ((void *)entry + sizeof(__u32) > max_addr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			(void *)XATTR_NEXT_ENTRY(entry) > max_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			*last_addr = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		if (entry->e_name_index != index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		if (entry->e_name_len != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		if (!memcmp(entry->e_name, name, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	/* inline xattr header or entry across max inline xattr size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (IS_XATTR_LAST_ENTRY(entry) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		(void *)entry + sizeof(__u32) > max_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		*last_addr = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static int read_inline_xattr(struct inode *inode, struct page *ipage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 							void *txattr_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	unsigned int inline_size = inline_xattr_size(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	void *inline_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (ipage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		inline_addr = inline_xattr_addr(inode, ipage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		page = f2fs_get_node_page(sbi, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		if (IS_ERR(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			return PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		inline_addr = inline_xattr_addr(inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	memcpy(txattr_addr, inline_addr, inline_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	f2fs_put_page(page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static int read_xattr_block(struct inode *inode, void *txattr_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	unsigned int inline_size = inline_xattr_size(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	struct page *xpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	void *xattr_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	/* The inode already has an extended attribute block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	xpage = f2fs_get_node_page(sbi, xnid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (IS_ERR(xpage))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		return PTR_ERR(xpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	xattr_addr = page_address(xpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	f2fs_put_page(xpage, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 				unsigned int index, unsigned int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 				const char *name, struct f2fs_xattr_entry **xe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 				void **base_addr, int *base_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 				bool *is_inline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	void *cur_addr, *txattr_addr, *last_txattr_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	void *last_addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	unsigned int inline_size = inline_xattr_size(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	if (!xnid && !inline_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	*base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	if (!txattr_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	/* read from inline xattr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (inline_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		err = read_inline_xattr(inode, ipage, txattr_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		*xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 						index, len, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		if (*xe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			*base_size = inline_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			goto check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	/* read from xattr node block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	if (xnid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		err = read_xattr_block(inode, txattr_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if (last_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		cur_addr = XATTR_HDR(last_addr) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		cur_addr = txattr_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	*xe = __find_xattr(cur_addr, last_txattr_addr, index, len, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	if (!*xe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 								inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		err = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) check:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	if (IS_XATTR_LAST_ENTRY(*xe)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		err = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	*base_addr = txattr_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	xattr_free(F2FS_I_SB(inode), txattr_addr, *is_inline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static int read_all_xattrs(struct inode *inode, struct page *ipage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 							void **base_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	struct f2fs_xattr_header *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	unsigned int size = VALID_XATTR_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	unsigned int inline_size = inline_xattr_size(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	void *txattr_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	if (!txattr_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	/* read from inline xattr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	if (inline_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		err = read_inline_xattr(inode, ipage, txattr_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	/* read from xattr node block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (xnid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		err = read_xattr_block(inode, txattr_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	header = XATTR_HDR(txattr_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	/* never been allocated xattrs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		header->h_refcount = cpu_to_le32(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	*base_addr = txattr_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	kfree(txattr_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	return err;
^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) static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 				void *txattr_addr, struct page *ipage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	size_t inline_size = inline_xattr_size(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	struct page *in_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	void *xattr_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	void *inline_addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	struct page *xpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	nid_t new_nid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		if (!f2fs_alloc_nid(sbi, &new_nid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	/* write to inline xattr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	if (inline_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		if (ipage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 			inline_addr = inline_xattr_addr(inode, ipage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			in_page = f2fs_get_node_page(sbi, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			if (IS_ERR(in_page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 				f2fs_alloc_nid_failed(sbi, new_nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 				return PTR_ERR(in_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			inline_addr = inline_xattr_addr(inode, in_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 							NODE, true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		/* no need to use xattr node block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		if (hsize <= inline_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			err = f2fs_truncate_xattr_node(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			f2fs_alloc_nid_failed(sbi, new_nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 				f2fs_put_page(in_page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			memcpy(inline_addr, txattr_addr, inline_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 			set_page_dirty(ipage ? ipage : in_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			goto in_page_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	/* write to xattr node block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if (F2FS_I(inode)->i_xattr_nid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		if (IS_ERR(xpage)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 			err = PTR_ERR(xpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			f2fs_alloc_nid_failed(sbi, new_nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			goto in_page_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		f2fs_bug_on(sbi, new_nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		f2fs_wait_on_page_writeback(xpage, NODE, true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		struct dnode_of_data dn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		set_new_dnode(&dn, inode, NULL, NULL, new_nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		if (IS_ERR(xpage)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 			err = PTR_ERR(xpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 			f2fs_alloc_nid_failed(sbi, new_nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			goto in_page_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		f2fs_alloc_nid_done(sbi, new_nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	xattr_addr = page_address(xpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	if (inline_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		memcpy(inline_addr, txattr_addr, inline_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if (inline_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		set_page_dirty(ipage ? ipage : in_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	set_page_dirty(xpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	f2fs_put_page(xpage, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) in_page_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	f2fs_put_page(in_page, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) int f2fs_getxattr(struct inode *inode, int index, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		void *buffer, size_t buffer_size, struct page *ipage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	struct f2fs_xattr_entry *entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	unsigned int size, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	void *base_addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	int base_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	bool is_inline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	if (name == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	len = strlen(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	if (len > F2FS_NAME_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	f2fs_down_read(&F2FS_I(inode)->i_xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	error = lookup_all_xattrs(inode, ipage, index, len, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 				&entry, &base_addr, &base_size, &is_inline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	f2fs_up_read(&F2FS_I(inode)->i_xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	size = le16_to_cpu(entry->e_value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	if (buffer && size > buffer_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		error = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	if (buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		char *pval = entry->e_name + entry->e_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		if (base_size - (pval - (char *)base_addr) < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 			error = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		memcpy(buffer, pval, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	error = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	xattr_free(F2FS_I_SB(inode), base_addr, is_inline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	struct f2fs_xattr_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	void *base_addr, *last_base_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	size_t rest = buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	f2fs_down_read(&F2FS_I(inode)->i_xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	error = read_all_xattrs(inode, NULL, &base_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	f2fs_up_read(&F2FS_I(inode)->i_xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	list_for_each_xattr(entry, base_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		const struct xattr_handler *handler =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 			f2fs_xattr_handler(entry->e_name_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		const char *prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		size_t prefix_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 			(void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 			f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 						inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 			set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		if (!handler || (handler->list && !handler->list(dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		prefix = xattr_prefix(handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		prefix_len = strlen(prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		size = prefix_len + entry->e_name_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		if (buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 			if (size > rest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 				error = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 				goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 			memcpy(buffer, prefix, prefix_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 			buffer += prefix_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 			memcpy(buffer, entry->e_name, entry->e_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 			buffer += entry->e_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 			*buffer++ = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		rest -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	error = buffer_size - rest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	kfree(base_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 					const void *value, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	void *pval = entry->e_name + entry->e_name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	return (le16_to_cpu(entry->e_value_size) == size) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 					!memcmp(pval, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) static int __f2fs_setxattr(struct inode *inode, int index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 			const char *name, const void *value, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 			struct page *ipage, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	struct f2fs_xattr_entry *here, *last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	void *base_addr, *last_base_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	int found, newsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	__u32 new_hsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	if (name == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	if (value == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	len = strlen(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	if (len > F2FS_NAME_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 		return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	if (size > MAX_VALUE_LEN(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 		return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	error = read_all_xattrs(inode, ipage, &base_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	/* find entry with wanted name. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	here = __find_xattr(base_addr, last_base_addr, index, len, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	if (!here) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 								inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	if (found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		if ((flags & XATTR_CREATE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 			error = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		if (value && f2fs_xattr_value_same(here, value, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 			goto same;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	} else if ((flags & XATTR_REPLACE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		error = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	last = here;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	while (!IS_XATTR_LAST_ENTRY(last)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		if ((void *)(last) + sizeof(__u32) > last_base_addr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 			(void *)XATTR_NEXT_ENTRY(last) > last_base_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 			f2fs_err(F2FS_I_SB(inode), "inode (%lu) has invalid last xattr entry, entry_size: %zu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 					inode->i_ino, ENTRY_SIZE(last));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 			set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 			error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		last = XATTR_NEXT_ENTRY(last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	/* 1. Check space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	if (value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		int free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		 * If value is NULL, it is remove operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		 * In case of update operation, we calculate free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 		if (found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 			free = free + ENTRY_SIZE(here);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 		if (unlikely(free < newsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 			error = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	/* 2. Remove old entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	if (found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 		 * If entry is found, remove old entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 		 * If not found, remove operation is not needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 		struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 		int oldsize = ENTRY_SIZE(here);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 		memmove(here, next, (char *)last - (char *)next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		memset(last, 0, oldsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	new_hsize = (char *)last - (char *)base_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	/* 3. Write new entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	if (value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		char *pval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		 * Before we come here, old entry is removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 		 * We just write new entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 		last->e_name_index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 		last->e_name_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 		memcpy(last->e_name, name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		pval = last->e_name + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		memcpy(pval, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 		last->e_value_size = cpu_to_le16(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 		new_hsize += newsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 			!strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 		f2fs_set_encrypted_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	f2fs_mark_inode_dirty_sync(inode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	if (!error && S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) same:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	if (is_inode_flag_set(inode, FI_ACL_MODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 		inode->i_mode = F2FS_I(inode)->i_acl_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 		inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 		clear_inode_flag(inode, FI_ACL_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	kfree(base_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) int f2fs_setxattr(struct inode *inode, int index, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 				const void *value, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 				struct page *ipage, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	if (unlikely(f2fs_cp_error(sbi)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	if (!f2fs_is_checkpoint_ready(sbi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 	err = dquot_initialize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	/* this case is only from f2fs_init_inode_metadata */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	if (ipage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 		return __f2fs_setxattr(inode, index, name, value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 						size, ipage, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	f2fs_balance_fs(sbi, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	f2fs_lock_op(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	f2fs_down_write(&F2FS_I(inode)->i_xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 	err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	f2fs_up_write(&F2FS_I(inode)->i_xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	f2fs_unlock_op(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	f2fs_update_time(sbi, REQ_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	dev_t dev = sbi->sb->s_bdev->bd_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	char slab_name[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	sprintf(slab_name, "f2fs_xattr_entry-%u:%u", MAJOR(dev), MINOR(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	sbi->inline_xattr_slab_size = F2FS_OPTION(sbi).inline_xattr_size *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 					sizeof(__le32) + XATTR_PADDING_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	sbi->inline_xattr_slab = f2fs_kmem_cache_create(slab_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 					sbi->inline_xattr_slab_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	if (!sbi->inline_xattr_slab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	kmem_cache_destroy(sbi->inline_xattr_slab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) }