xref: /openbmc/linux/fs/btrfs/xattr.c (revision 4815053a)
15103e947SJosef Bacik /*
25103e947SJosef Bacik  * Copyright (C) 2007 Red Hat.  All rights reserved.
35103e947SJosef Bacik  *
45103e947SJosef Bacik  * This program is free software; you can redistribute it and/or
55103e947SJosef Bacik  * modify it under the terms of the GNU General Public
65103e947SJosef Bacik  * License v2 as published by the Free Software Foundation.
75103e947SJosef Bacik  *
85103e947SJosef Bacik  * This program is distributed in the hope that it will be useful,
95103e947SJosef Bacik  * but WITHOUT ANY WARRANTY; without even the implied warranty of
105103e947SJosef Bacik  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
115103e947SJosef Bacik  * General Public License for more details.
125103e947SJosef Bacik  *
135103e947SJosef Bacik  * You should have received a copy of the GNU General Public
145103e947SJosef Bacik  * License along with this program; if not, write to the
155103e947SJosef Bacik  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
165103e947SJosef Bacik  * Boston, MA 021110-1307, USA.
175103e947SJosef Bacik  */
185103e947SJosef Bacik 
195103e947SJosef Bacik #include <linux/init.h>
205103e947SJosef Bacik #include <linux/fs.h>
215103e947SJosef Bacik #include <linux/slab.h>
225103e947SJosef Bacik #include <linux/rwsem.h>
235103e947SJosef Bacik #include <linux/xattr.h>
240279b4cdSJim Owens #include <linux/security.h>
255103e947SJosef Bacik #include "ctree.h"
265103e947SJosef Bacik #include "btrfs_inode.h"
275103e947SJosef Bacik #include "transaction.h"
285103e947SJosef Bacik #include "xattr.h"
295103e947SJosef Bacik #include "disk-io.h"
3033268eafSJosef Bacik 
3133268eafSJosef Bacik 
3295819c05SChristoph Hellwig ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
3395819c05SChristoph Hellwig 				void *buffer, size_t size)
345103e947SJosef Bacik {
355103e947SJosef Bacik 	struct btrfs_dir_item *di;
365103e947SJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
375103e947SJosef Bacik 	struct btrfs_path *path;
385103e947SJosef Bacik 	struct extent_buffer *leaf;
395103e947SJosef Bacik 	int ret = 0;
405103e947SJosef Bacik 	unsigned long data_ptr;
415103e947SJosef Bacik 
425103e947SJosef Bacik 	path = btrfs_alloc_path();
4395819c05SChristoph Hellwig 	if (!path)
445103e947SJosef Bacik 		return -ENOMEM;
455103e947SJosef Bacik 
465103e947SJosef Bacik 	/* lookup the xattr by name */
4733345d01SLi Zefan 	di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode), name,
485103e947SJosef Bacik 				strlen(name), 0);
4907060404SJosef Bacik 	if (!di) {
505103e947SJosef Bacik 		ret = -ENODATA;
515103e947SJosef Bacik 		goto out;
5207060404SJosef Bacik 	} else if (IS_ERR(di)) {
5307060404SJosef Bacik 		ret = PTR_ERR(di);
5407060404SJosef Bacik 		goto out;
555103e947SJosef Bacik 	}
565103e947SJosef Bacik 
575103e947SJosef Bacik 	leaf = path->nodes[0];
585103e947SJosef Bacik 	/* if size is 0, that means we want the size of the attr */
595103e947SJosef Bacik 	if (!size) {
605103e947SJosef Bacik 		ret = btrfs_dir_data_len(leaf, di);
615103e947SJosef Bacik 		goto out;
625103e947SJosef Bacik 	}
635103e947SJosef Bacik 
645103e947SJosef Bacik 	/* now get the data out of our dir_item */
655103e947SJosef Bacik 	if (btrfs_dir_data_len(leaf, di) > size) {
665103e947SJosef Bacik 		ret = -ERANGE;
675103e947SJosef Bacik 		goto out;
685103e947SJosef Bacik 	}
6907060404SJosef Bacik 
7007060404SJosef Bacik 	/*
7107060404SJosef Bacik 	 * The way things are packed into the leaf is like this
7207060404SJosef Bacik 	 * |struct btrfs_dir_item|name|data|
7307060404SJosef Bacik 	 * where name is the xattr name, so security.foo, and data is the
7407060404SJosef Bacik 	 * content of the xattr.  data_ptr points to the location in memory
7507060404SJosef Bacik 	 * where the data starts in the in memory leaf
7607060404SJosef Bacik 	 */
775103e947SJosef Bacik 	data_ptr = (unsigned long)((char *)(di + 1) +
785103e947SJosef Bacik 				   btrfs_dir_name_len(leaf, di));
795103e947SJosef Bacik 	read_extent_buffer(leaf, buffer, data_ptr,
803acd7ee8SJosef Bacik 			   btrfs_dir_data_len(leaf, di));
815103e947SJosef Bacik 	ret = btrfs_dir_data_len(leaf, di);
825103e947SJosef Bacik 
835103e947SJosef Bacik out:
845103e947SJosef Bacik 	btrfs_free_path(path);
855103e947SJosef Bacik 	return ret;
865103e947SJosef Bacik }
875103e947SJosef Bacik 
88f34f57a3SYan, Zheng static int do_setxattr(struct btrfs_trans_handle *trans,
89f34f57a3SYan, Zheng 		       struct inode *inode, const char *name,
9095819c05SChristoph Hellwig 		       const void *value, size_t size, int flags)
915103e947SJosef Bacik {
925103e947SJosef Bacik 	struct btrfs_dir_item *di;
935103e947SJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
945103e947SJosef Bacik 	struct btrfs_path *path;
95f34f57a3SYan, Zheng 	size_t name_len = strlen(name);
96f34f57a3SYan, Zheng 	int ret = 0;
97f34f57a3SYan, Zheng 
98f34f57a3SYan, Zheng 	if (name_len + size > BTRFS_MAX_XATTR_SIZE(root))
99f34f57a3SYan, Zheng 		return -ENOSPC;
1005103e947SJosef Bacik 
1015103e947SJosef Bacik 	path = btrfs_alloc_path();
10295819c05SChristoph Hellwig 	if (!path)
1035103e947SJosef Bacik 		return -ENOMEM;
1045103e947SJosef Bacik 
105fa09200bSJosef Bacik 	if (flags & XATTR_REPLACE) {
10633345d01SLi Zefan 		di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode), name,
107fa09200bSJosef Bacik 					name_len, -1);
1085103e947SJosef Bacik 		if (IS_ERR(di)) {
1095103e947SJosef Bacik 			ret = PTR_ERR(di);
1105103e947SJosef Bacik 			goto out;
111fa09200bSJosef Bacik 		} else if (!di) {
1125103e947SJosef Bacik 			ret = -ENODATA;
1135103e947SJosef Bacik 			goto out;
1145103e947SJosef Bacik 		}
115fa09200bSJosef Bacik 		ret = btrfs_delete_one_dir_name(trans, root, path, di);
116fa09200bSJosef Bacik 		if (ret)
117fa09200bSJosef Bacik 			goto out;
118fa09200bSJosef Bacik 		btrfs_release_path(path);
1194815053aSDavid Sterba 
1204815053aSDavid Sterba 		/*
1214815053aSDavid Sterba 		 * remove the attribute
1224815053aSDavid Sterba 		 */
1234815053aSDavid Sterba 		if (!value)
1244815053aSDavid Sterba 			goto out;
12533268eafSJosef Bacik 	}
1265103e947SJosef Bacik 
127fa09200bSJosef Bacik again:
12833345d01SLi Zefan 	ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode),
129f34f57a3SYan, Zheng 				      name, name_len, value, size);
130fa09200bSJosef Bacik 	if (ret == -EEXIST) {
131fa09200bSJosef Bacik 		if (flags & XATTR_CREATE)
132fa09200bSJosef Bacik 			goto out;
133fa09200bSJosef Bacik 		/*
134fa09200bSJosef Bacik 		 * We can't use the path we already have since we won't have the
135fa09200bSJosef Bacik 		 * proper locking for a delete, so release the path and
136fa09200bSJosef Bacik 		 * re-lookup to delete the thing.
137fa09200bSJosef Bacik 		 */
138fa09200bSJosef Bacik 		btrfs_release_path(path);
139fa09200bSJosef Bacik 		di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode),
140fa09200bSJosef Bacik 					name, name_len, -1);
141fa09200bSJosef Bacik 		if (IS_ERR(di)) {
142fa09200bSJosef Bacik 			ret = PTR_ERR(di);
143fa09200bSJosef Bacik 			goto out;
144fa09200bSJosef Bacik 		} else if (!di) {
145fa09200bSJosef Bacik 			/* Shouldn't happen but just in case... */
146fa09200bSJosef Bacik 			btrfs_release_path(path);
147fa09200bSJosef Bacik 			goto again;
148fa09200bSJosef Bacik 		}
149fa09200bSJosef Bacik 
150fa09200bSJosef Bacik 		ret = btrfs_delete_one_dir_name(trans, root, path, di);
151fa09200bSJosef Bacik 		if (ret)
152fa09200bSJosef Bacik 			goto out;
153fa09200bSJosef Bacik 
154fa09200bSJosef Bacik 		/*
155fa09200bSJosef Bacik 		 * We have a value to set, so go back and try to insert it now.
156fa09200bSJosef Bacik 		 */
157fa09200bSJosef Bacik 		if (value) {
158fa09200bSJosef Bacik 			btrfs_release_path(path);
159fa09200bSJosef Bacik 			goto again;
160fa09200bSJosef Bacik 		}
161fa09200bSJosef Bacik 	}
1625103e947SJosef Bacik out:
163f34f57a3SYan, Zheng 	btrfs_free_path(path);
164f34f57a3SYan, Zheng 	return ret;
1655103e947SJosef Bacik }
1665103e947SJosef Bacik 
1674815053aSDavid Sterba /*
1684815053aSDavid Sterba  * @value: "" makes the attribute to empty, NULL removes it
1694815053aSDavid Sterba  */
170f34f57a3SYan, Zheng int __btrfs_setxattr(struct btrfs_trans_handle *trans,
171f34f57a3SYan, Zheng 		     struct inode *inode, const char *name,
172f34f57a3SYan, Zheng 		     const void *value, size_t size, int flags)
173f34f57a3SYan, Zheng {
174f34f57a3SYan, Zheng 	struct btrfs_root *root = BTRFS_I(inode)->root;
175f34f57a3SYan, Zheng 	int ret;
176f34f57a3SYan, Zheng 
177f34f57a3SYan, Zheng 	if (trans)
178f34f57a3SYan, Zheng 		return do_setxattr(trans, inode, name, value, size, flags);
179f34f57a3SYan, Zheng 
180a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 2);
181a22285a6SYan, Zheng 	if (IS_ERR(trans))
182a22285a6SYan, Zheng 		return PTR_ERR(trans);
183f34f57a3SYan, Zheng 
184f34f57a3SYan, Zheng 	ret = do_setxattr(trans, inode, name, value, size, flags);
185f34f57a3SYan, Zheng 	if (ret)
186f34f57a3SYan, Zheng 		goto out;
187f34f57a3SYan, Zheng 
188f34f57a3SYan, Zheng 	inode->i_ctime = CURRENT_TIME;
189f34f57a3SYan, Zheng 	ret = btrfs_update_inode(trans, root, inode);
190f34f57a3SYan, Zheng 	BUG_ON(ret);
191f34f57a3SYan, Zheng out:
192f34f57a3SYan, Zheng 	btrfs_end_transaction_throttle(trans, root);
1935103e947SJosef Bacik 	return ret;
1945103e947SJosef Bacik }
1955103e947SJosef Bacik 
1965103e947SJosef Bacik ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
1975103e947SJosef Bacik {
1985103e947SJosef Bacik 	struct btrfs_key key, found_key;
1995103e947SJosef Bacik 	struct inode *inode = dentry->d_inode;
2005103e947SJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
2015103e947SJosef Bacik 	struct btrfs_path *path;
2025103e947SJosef Bacik 	struct extent_buffer *leaf;
2035103e947SJosef Bacik 	struct btrfs_dir_item *di;
2042e6a0035SLi Zefan 	int ret = 0, slot;
205eaa47d86SChristoph Hellwig 	size_t total_size = 0, size_left = size;
2065103e947SJosef Bacik 	unsigned long name_ptr;
207eaa47d86SChristoph Hellwig 	size_t name_len;
2085103e947SJosef Bacik 
2095103e947SJosef Bacik 	/*
2105103e947SJosef Bacik 	 * ok we want all objects associated with this id.
2115103e947SJosef Bacik 	 * NOTE: we set key.offset = 0; because we want to start with the
2125103e947SJosef Bacik 	 * first xattr that we find and walk forward
2135103e947SJosef Bacik 	 */
21433345d01SLi Zefan 	key.objectid = btrfs_ino(inode);
2155103e947SJosef Bacik 	btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
2165103e947SJosef Bacik 	key.offset = 0;
2175103e947SJosef Bacik 
2185103e947SJosef Bacik 	path = btrfs_alloc_path();
2195103e947SJosef Bacik 	if (!path)
2205103e947SJosef Bacik 		return -ENOMEM;
2211caf9342SJosef Bacik 	path->reada = 2;
2225103e947SJosef Bacik 
2235103e947SJosef Bacik 	/* search for our xattrs */
2245103e947SJosef Bacik 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2255103e947SJosef Bacik 	if (ret < 0)
2265103e947SJosef Bacik 		goto err;
2272e6a0035SLi Zefan 
2285103e947SJosef Bacik 	while (1) {
2295103e947SJosef Bacik 		leaf = path->nodes[0];
2305103e947SJosef Bacik 		slot = path->slots[0];
2315103e947SJosef Bacik 
2325103e947SJosef Bacik 		/* this is where we start walking through the path */
2332e6a0035SLi Zefan 		if (slot >= btrfs_header_nritems(leaf)) {
2345103e947SJosef Bacik 			/*
2355103e947SJosef Bacik 			 * if we've reached the last slot in this leaf we need
2365103e947SJosef Bacik 			 * to go to the next leaf and reset everything
2375103e947SJosef Bacik 			 */
2385103e947SJosef Bacik 			ret = btrfs_next_leaf(root, path);
2392e6a0035SLi Zefan 			if (ret < 0)
2402e6a0035SLi Zefan 				goto err;
2412e6a0035SLi Zefan 			else if (ret > 0)
2425103e947SJosef Bacik 				break;
2432e6a0035SLi Zefan 			continue;
2445103e947SJosef Bacik 		}
2455103e947SJosef Bacik 
2465103e947SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
2475103e947SJosef Bacik 
2485103e947SJosef Bacik 		/* check to make sure this item is what we want */
2495103e947SJosef Bacik 		if (found_key.objectid != key.objectid)
2505103e947SJosef Bacik 			break;
2515103e947SJosef Bacik 		if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
2525103e947SJosef Bacik 			break;
2535103e947SJosef Bacik 
2545103e947SJosef Bacik 		di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
25522a94d44SJosef Bacik 		if (verify_dir_item(root, leaf, di))
25622a94d44SJosef Bacik 			continue;
2575103e947SJosef Bacik 
258eaa47d86SChristoph Hellwig 		name_len = btrfs_dir_name_len(leaf, di);
259eaa47d86SChristoph Hellwig 		total_size += name_len + 1;
2605103e947SJosef Bacik 
2615103e947SJosef Bacik 		/* we are just looking for how big our buffer needs to be */
2625103e947SJosef Bacik 		if (!size)
2632e6a0035SLi Zefan 			goto next;
2645103e947SJosef Bacik 
265eaa47d86SChristoph Hellwig 		if (!buffer || (name_len + 1) > size_left) {
2665103e947SJosef Bacik 			ret = -ERANGE;
267b16281c3SYehuda Sadeh Weinraub 			goto err;
2685103e947SJosef Bacik 		}
2695103e947SJosef Bacik 
270eaa47d86SChristoph Hellwig 		name_ptr = (unsigned long)(di + 1);
271eaa47d86SChristoph Hellwig 		read_extent_buffer(leaf, buffer, name_ptr, name_len);
272eaa47d86SChristoph Hellwig 		buffer[name_len] = '\0';
273eaa47d86SChristoph Hellwig 
274eaa47d86SChristoph Hellwig 		size_left -= name_len + 1;
275eaa47d86SChristoph Hellwig 		buffer += name_len + 1;
2762e6a0035SLi Zefan next:
2772e6a0035SLi Zefan 		path->slots[0]++;
2785103e947SJosef Bacik 	}
2795103e947SJosef Bacik 	ret = total_size;
2805103e947SJosef Bacik 
2815103e947SJosef Bacik err:
2825103e947SJosef Bacik 	btrfs_free_path(path);
2835103e947SJosef Bacik 
2845103e947SJosef Bacik 	return ret;
2855103e947SJosef Bacik }
2865103e947SJosef Bacik 
2875103e947SJosef Bacik /*
28895819c05SChristoph Hellwig  * List of handlers for synthetic system.* attributes.  All real ondisk
28995819c05SChristoph Hellwig  * attributes are handled directly.
2905103e947SJosef Bacik  */
291f01cbd3fSStephen Hemminger const struct xattr_handler *btrfs_xattr_handlers[] = {
2920eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
29395819c05SChristoph Hellwig 	&btrfs_xattr_acl_access_handler,
29495819c05SChristoph Hellwig 	&btrfs_xattr_acl_default_handler,
29595819c05SChristoph Hellwig #endif
29695819c05SChristoph Hellwig 	NULL,
29795819c05SChristoph Hellwig };
29895819c05SChristoph Hellwig 
29995819c05SChristoph Hellwig /*
30095819c05SChristoph Hellwig  * Check if the attribute is in a supported namespace.
30195819c05SChristoph Hellwig  *
30295819c05SChristoph Hellwig  * This applied after the check for the synthetic attributes in the system
30395819c05SChristoph Hellwig  * namespace.
30495819c05SChristoph Hellwig  */
30595819c05SChristoph Hellwig static bool btrfs_is_valid_xattr(const char *name)
30695819c05SChristoph Hellwig {
307d397712bSChris Mason 	return !strncmp(name, XATTR_SECURITY_PREFIX,
308d397712bSChris Mason 			XATTR_SECURITY_PREFIX_LEN) ||
30995819c05SChristoph Hellwig 	       !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
31095819c05SChristoph Hellwig 	       !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
31195819c05SChristoph Hellwig 	       !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
31269a32ac5SChris Mason }
31369a32ac5SChris Mason 
31495819c05SChristoph Hellwig ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
31595819c05SChristoph Hellwig 		       void *buffer, size_t size)
31695819c05SChristoph Hellwig {
31795819c05SChristoph Hellwig 	/*
31895819c05SChristoph Hellwig 	 * If this is a request for a synthetic attribute in the system.*
31995819c05SChristoph Hellwig 	 * namespace use the generic infrastructure to resolve a handler
32095819c05SChristoph Hellwig 	 * for it via sb->s_xattr.
32195819c05SChristoph Hellwig 	 */
32295819c05SChristoph Hellwig 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
32395819c05SChristoph Hellwig 		return generic_getxattr(dentry, name, buffer, size);
3245103e947SJosef Bacik 
32595819c05SChristoph Hellwig 	if (!btrfs_is_valid_xattr(name))
32695819c05SChristoph Hellwig 		return -EOPNOTSUPP;
32795819c05SChristoph Hellwig 	return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
32895819c05SChristoph Hellwig }
3295103e947SJosef Bacik 
33095819c05SChristoph Hellwig int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
33195819c05SChristoph Hellwig 		   size_t size, int flags)
33295819c05SChristoph Hellwig {
333b83cc969SLi Zefan 	struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root;
334b83cc969SLi Zefan 
335b83cc969SLi Zefan 	/*
336b83cc969SLi Zefan 	 * The permission on security.* and system.* is not checked
337b83cc969SLi Zefan 	 * in permission().
338b83cc969SLi Zefan 	 */
339b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
340b83cc969SLi Zefan 		return -EROFS;
341b83cc969SLi Zefan 
34295819c05SChristoph Hellwig 	/*
34395819c05SChristoph Hellwig 	 * If this is a request for a synthetic attribute in the system.*
34495819c05SChristoph Hellwig 	 * namespace use the generic infrastructure to resolve a handler
34595819c05SChristoph Hellwig 	 * for it via sb->s_xattr.
34695819c05SChristoph Hellwig 	 */
34795819c05SChristoph Hellwig 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
34895819c05SChristoph Hellwig 		return generic_setxattr(dentry, name, value, size, flags);
3495103e947SJosef Bacik 
35095819c05SChristoph Hellwig 	if (!btrfs_is_valid_xattr(name))
35195819c05SChristoph Hellwig 		return -EOPNOTSUPP;
3525103e947SJosef Bacik 
35395819c05SChristoph Hellwig 	if (size == 0)
35495819c05SChristoph Hellwig 		value = "";  /* empty EA, do not remove */
355f34f57a3SYan, Zheng 
356f34f57a3SYan, Zheng 	return __btrfs_setxattr(NULL, dentry->d_inode, name, value, size,
357f34f57a3SYan, Zheng 				flags);
35895819c05SChristoph Hellwig }
35995819c05SChristoph Hellwig 
36095819c05SChristoph Hellwig int btrfs_removexattr(struct dentry *dentry, const char *name)
36195819c05SChristoph Hellwig {
362b83cc969SLi Zefan 	struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root;
363b83cc969SLi Zefan 
364b83cc969SLi Zefan 	/*
365b83cc969SLi Zefan 	 * The permission on security.* and system.* is not checked
366b83cc969SLi Zefan 	 * in permission().
367b83cc969SLi Zefan 	 */
368b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
369b83cc969SLi Zefan 		return -EROFS;
370b83cc969SLi Zefan 
37195819c05SChristoph Hellwig 	/*
37295819c05SChristoph Hellwig 	 * If this is a request for a synthetic attribute in the system.*
37395819c05SChristoph Hellwig 	 * namespace use the generic infrastructure to resolve a handler
37495819c05SChristoph Hellwig 	 * for it via sb->s_xattr.
37595819c05SChristoph Hellwig 	 */
37695819c05SChristoph Hellwig 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
37795819c05SChristoph Hellwig 		return generic_removexattr(dentry, name);
37895819c05SChristoph Hellwig 
37995819c05SChristoph Hellwig 	if (!btrfs_is_valid_xattr(name))
38095819c05SChristoph Hellwig 		return -EOPNOTSUPP;
381f34f57a3SYan, Zheng 
382f34f57a3SYan, Zheng 	return __btrfs_setxattr(NULL, dentry->d_inode, name, NULL, 0,
383f34f57a3SYan, Zheng 				XATTR_REPLACE);
38495819c05SChristoph Hellwig }
3850279b4cdSJim Owens 
386f34f57a3SYan, Zheng int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
3872a7dba39SEric Paris 			      struct inode *inode, struct inode *dir,
3882a7dba39SEric Paris 			      const struct qstr *qstr)
3890279b4cdSJim Owens {
3900279b4cdSJim Owens 	int err;
3910279b4cdSJim Owens 	size_t len;
3920279b4cdSJim Owens 	void *value;
3930279b4cdSJim Owens 	char *suffix;
3940279b4cdSJim Owens 	char *name;
3950279b4cdSJim Owens 
3962a7dba39SEric Paris 	err = security_inode_init_security(inode, dir, qstr, &suffix, &value,
3972a7dba39SEric Paris 					   &len);
3980279b4cdSJim Owens 	if (err) {
3990279b4cdSJim Owens 		if (err == -EOPNOTSUPP)
4000279b4cdSJim Owens 			return 0;
4010279b4cdSJim Owens 		return err;
4020279b4cdSJim Owens 	}
4030279b4cdSJim Owens 
4040279b4cdSJim Owens 	name = kmalloc(XATTR_SECURITY_PREFIX_LEN + strlen(suffix) + 1,
4050279b4cdSJim Owens 		       GFP_NOFS);
4060279b4cdSJim Owens 	if (!name) {
4070279b4cdSJim Owens 		err = -ENOMEM;
4080279b4cdSJim Owens 	} else {
4090279b4cdSJim Owens 		strcpy(name, XATTR_SECURITY_PREFIX);
4100279b4cdSJim Owens 		strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
411f34f57a3SYan, Zheng 		err = __btrfs_setxattr(trans, inode, name, value, len, 0);
4120279b4cdSJim Owens 		kfree(name);
4130279b4cdSJim Owens 	}
4140279b4cdSJim Owens 
4150279b4cdSJim Owens 	kfree(suffix);
4160279b4cdSJim Owens 	kfree(value);
4170279b4cdSJim Owens 	return err;
4180279b4cdSJim Owens }
419