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)  * Copyright (C) 2007 Red Hat.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/rwsem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/posix_acl_xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "btrfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "transaction.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "props.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "locking.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) int btrfs_getxattr(struct inode *inode, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 				void *buffer, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	unsigned long data_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	/* lookup the xattr by name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(BTRFS_I(inode)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 			name, strlen(name), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	if (!di) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		ret = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	} else if (IS_ERR(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		ret = PTR_ERR(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	/* if size is 0, that means we want the size of the attr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (!size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		ret = btrfs_dir_data_len(leaf, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	/* now get the data out of our dir_item */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (btrfs_dir_data_len(leaf, di) > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		ret = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	 * The way things are packed into the leaf is like this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	 * |struct btrfs_dir_item|name|data|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	 * where name is the xattr name, so security.foo, and data is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	 * content of the xattr.  data_ptr points to the location in memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	 * where the data starts in the in memory leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	data_ptr = (unsigned long)((char *)(di + 1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 				   btrfs_dir_name_len(leaf, di));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	read_extent_buffer(leaf, buffer, data_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			   btrfs_dir_data_len(leaf, di));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	ret = btrfs_dir_data_len(leaf, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		   const char *name, const void *value, size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	struct btrfs_dir_item *di = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	size_t name_len = strlen(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	ASSERT(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (name_len + size > BTRFS_MAX_XATTR_SIZE(root->fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	path->skip_release_on_error = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (!value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		di = btrfs_lookup_xattr(trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 				btrfs_ino(BTRFS_I(inode)), name, name_len, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		if (!di && (flags & XATTR_REPLACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			ret = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		else if (IS_ERR(di))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			ret = PTR_ERR(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		else if (di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			ret = btrfs_delete_one_dir_name(trans, root, path, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	 * For a replace we can't just do the insert blindly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	 * Do a lookup first (read-only btrfs_search_slot), and return if xattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	 * doesn't exist. If it exists, fall down below to the insert/replace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	 * path - we can't race with a concurrent xattr delete, because the VFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	 * locks the inode's i_mutex before calling setxattr or removexattr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (flags & XATTR_REPLACE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		ASSERT(inode_is_locked(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		di = btrfs_lookup_xattr(NULL, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 				btrfs_ino(BTRFS_I(inode)), name, name_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		if (!di)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			ret = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		else if (IS_ERR(di))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			ret = PTR_ERR(di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		di = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(BTRFS_I(inode)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 				      name, name_len, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (ret == -EOVERFLOW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		 * We have an existing item in a leaf, split_leaf couldn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		 * expand it. That item might have or not a dir_item that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		 * matches our target xattr, so lets check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		btrfs_assert_tree_locked(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		if (!di && !(flags & XATTR_REPLACE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	} else if (ret == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		ASSERT(di); /* logic error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	} else if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (di && (flags & XATTR_CREATE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (di) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		 * We're doing a replace, and it must be atomic, that is, at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		 * any point in time we have either the old or the new xattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		 * value in the tree. We don't want readers (getxattr and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		 * listxattrs) to miss a value, this is specially important
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		 * for ACLs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		const int slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		struct extent_buffer *leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		const u16 old_data_len = btrfs_dir_data_len(leaf, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		const u32 item_size = btrfs_item_size_nr(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		const u32 data_size = sizeof(*di) + name_len + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		struct btrfs_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		unsigned long data_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		if (size > old_data_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			if (btrfs_leaf_free_space(leaf) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			    (size - old_data_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 				ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 				goto out;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		if (old_data_len + name_len + sizeof(*di) == item_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			/* No other xattrs packed in the same leaf item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			if (size > old_data_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				btrfs_extend_item(path, size - old_data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			else if (size < old_data_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 				btrfs_truncate_item(path, data_size, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			/* There are other xattrs packed in the same item. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			ret = btrfs_delete_one_dir_name(trans, root, path, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			btrfs_extend_item(path, data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		item = btrfs_item_nr(slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		ptr = btrfs_item_ptr(leaf, slot, char);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		ptr += btrfs_item_size(leaf, item) - data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		di = (struct btrfs_dir_item *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		btrfs_set_dir_data_len(leaf, di, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		data_ptr = ((unsigned long)(di + 1)) + name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		write_extent_buffer(leaf, value, data_ptr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		 * Insert, and we had space for the xattr, so path->slots[0] is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		 * where our xattr dir_item is and btrfs_insert_xattr_item()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		 * filled it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		set_bit(BTRFS_INODE_COPY_EVERYTHING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			&BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		clear_bit(BTRFS_INODE_NO_XATTRS, &BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^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)  * @value: "" makes the attribute to empty, NULL removes it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) int btrfs_setxattr_trans(struct inode *inode, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			 const void *value, size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	const bool start_trans = (current->journal_info == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (start_trans) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		 * 1 unit for inserting/updating/deleting the xattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		 * 1 unit for the inode item update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		trans = btrfs_start_transaction(root, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		 * This can happen when smack is enabled and a directory is being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		 * created. It happens through d_instantiate_new(), which calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		 * smack_d_instantiate(), which in turn calls __vfs_setxattr() to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		 * set the transmute xattr (XATTR_NAME_SMACKTRANSMUTE) on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		 * inode. We have already reserved space for the xattr and inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		 * update at btrfs_mkdir(), so just use the transaction handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		 * We don't join or start a transaction, as that will reset the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		 * block_rsv of the handle and trigger a warning for the start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		 * case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		ASSERT(strncmp(name, XATTR_SECURITY_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			       XATTR_SECURITY_PREFIX_LEN) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		trans = current->journal_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	ret = btrfs_setxattr(trans, inode, name, value, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	BUG_ON(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (start_trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	size_t total_size = 0, size_left = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	 * ok we want all objects associated with this id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 * NOTE: we set key.offset = 0; because we want to start with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	 * first xattr that we find and walk forward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	key.objectid = btrfs_ino(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	key.type = BTRFS_XATTR_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	path->reada = READA_FORWARD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	/* search for our xattrs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		struct btrfs_key found_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		u32 item_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		u32 cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		/* this is where we start walking through the path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		if (slot >= btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			 * if we've reached the last slot in this leaf we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			 * to go to the next leaf and reset everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		/* check to make sure this item is what we want */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		if (found_key.objectid != key.objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		if (found_key.type > BTRFS_XATTR_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		if (found_key.type < BTRFS_XATTR_ITEM_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			goto next_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		item_size = btrfs_item_size_nr(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		cur = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		while (cur < item_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			u16 name_len = btrfs_dir_name_len(leaf, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			u16 data_len = btrfs_dir_data_len(leaf, di);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			u32 this_len = sizeof(*di) + name_len + data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			unsigned long name_ptr = (unsigned long)(di + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			total_size += name_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			 * We are just looking for how big our buffer needs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			 * be.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 				goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			if (!buffer || (name_len + 1) > size_left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 				ret = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			read_extent_buffer(leaf, buffer, name_ptr, name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			buffer[name_len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			size_left -= name_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			buffer += name_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			cur += this_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			di = (struct btrfs_dir_item *)((char *)di + this_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) next_item:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	ret = total_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static int btrfs_xattr_handler_get(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 				   struct dentry *unused, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 				   const char *name, void *buffer, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 				   int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	name = xattr_full_name(handler, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	return btrfs_getxattr(inode, name, buffer, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static int btrfs_xattr_handler_set(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 				   struct dentry *unused, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 				   const char *name, const void *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 				   size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	name = xattr_full_name(handler, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	return btrfs_setxattr_trans(inode, name, buffer, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 					struct dentry *unused, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 					const char *name, const void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 					size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	name = xattr_full_name(handler, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	ret = btrfs_validate_prop(name, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	trans = btrfs_start_transaction(root, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	ret = btrfs_set_prop(trans, inode, name, value, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		BUG_ON(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static const struct xattr_handler btrfs_security_xattr_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	.prefix = XATTR_SECURITY_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	.get = btrfs_xattr_handler_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	.set = btrfs_xattr_handler_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) static const struct xattr_handler btrfs_trusted_xattr_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	.prefix = XATTR_TRUSTED_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	.get = btrfs_xattr_handler_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	.set = btrfs_xattr_handler_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static const struct xattr_handler btrfs_user_xattr_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	.prefix = XATTR_USER_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	.get = btrfs_xattr_handler_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	.set = btrfs_xattr_handler_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static const struct xattr_handler btrfs_btrfs_xattr_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	.prefix = XATTR_BTRFS_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	.get = btrfs_xattr_handler_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	.set = btrfs_xattr_handler_set_prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) const struct xattr_handler *btrfs_xattr_handlers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	&btrfs_security_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) #ifdef CONFIG_BTRFS_FS_POSIX_ACL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	&posix_acl_access_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	&posix_acl_default_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	&btrfs_trusted_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	&btrfs_user_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	&btrfs_btrfs_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	NULL,
^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) static int btrfs_initxattrs(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			    const struct xattr *xattr_array, void *fs_private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	struct btrfs_trans_handle *trans = fs_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	const struct xattr *xattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	unsigned int nofs_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	 * We're holding a transaction handle, so use a NOFS memory allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	 * context to avoid deadlock if reclaim happens.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	nofs_flag = memalloc_nofs_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 			       strlen(xattr->name) + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		strcpy(name, XATTR_SECURITY_PREFIX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		err = btrfs_setxattr(trans, inode, name, xattr->value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 				     xattr->value_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	memalloc_nofs_restore(nofs_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			      struct inode *inode, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			      const struct qstr *qstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	return security_inode_init_security(inode, dir, qstr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 					    &btrfs_initxattrs, trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }