xref: /openbmc/linux/fs/btrfs/xattr.c (revision 996a710d46418cacb5b4a519ab9341a74066551d)
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>
25*996a710dSChristoph Hellwig #include <linux/posix_acl_xattr.h>
265103e947SJosef Bacik #include "ctree.h"
275103e947SJosef Bacik #include "btrfs_inode.h"
285103e947SJosef Bacik #include "transaction.h"
295103e947SJosef Bacik #include "xattr.h"
305103e947SJosef Bacik #include "disk-io.h"
3133268eafSJosef Bacik 
3233268eafSJosef Bacik 
3395819c05SChristoph Hellwig ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
3495819c05SChristoph Hellwig 				void *buffer, size_t size)
355103e947SJosef Bacik {
365103e947SJosef Bacik 	struct btrfs_dir_item *di;
375103e947SJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
385103e947SJosef Bacik 	struct btrfs_path *path;
395103e947SJosef Bacik 	struct extent_buffer *leaf;
405103e947SJosef Bacik 	int ret = 0;
415103e947SJosef Bacik 	unsigned long data_ptr;
425103e947SJosef Bacik 
435103e947SJosef Bacik 	path = btrfs_alloc_path();
4495819c05SChristoph Hellwig 	if (!path)
455103e947SJosef Bacik 		return -ENOMEM;
465103e947SJosef Bacik 
475103e947SJosef Bacik 	/* lookup the xattr by name */
4833345d01SLi Zefan 	di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode), name,
495103e947SJosef Bacik 				strlen(name), 0);
5007060404SJosef Bacik 	if (!di) {
515103e947SJosef Bacik 		ret = -ENODATA;
525103e947SJosef Bacik 		goto out;
5307060404SJosef Bacik 	} else if (IS_ERR(di)) {
5407060404SJosef Bacik 		ret = PTR_ERR(di);
5507060404SJosef Bacik 		goto out;
565103e947SJosef Bacik 	}
575103e947SJosef Bacik 
585103e947SJosef Bacik 	leaf = path->nodes[0];
595103e947SJosef Bacik 	/* if size is 0, that means we want the size of the attr */
605103e947SJosef Bacik 	if (!size) {
615103e947SJosef Bacik 		ret = btrfs_dir_data_len(leaf, di);
625103e947SJosef Bacik 		goto out;
635103e947SJosef Bacik 	}
645103e947SJosef Bacik 
655103e947SJosef Bacik 	/* now get the data out of our dir_item */
665103e947SJosef Bacik 	if (btrfs_dir_data_len(leaf, di) > size) {
675103e947SJosef Bacik 		ret = -ERANGE;
685103e947SJosef Bacik 		goto out;
695103e947SJosef Bacik 	}
7007060404SJosef Bacik 
7107060404SJosef Bacik 	/*
7207060404SJosef Bacik 	 * The way things are packed into the leaf is like this
7307060404SJosef Bacik 	 * |struct btrfs_dir_item|name|data|
7407060404SJosef Bacik 	 * where name is the xattr name, so security.foo, and data is the
7507060404SJosef Bacik 	 * content of the xattr.  data_ptr points to the location in memory
7607060404SJosef Bacik 	 * where the data starts in the in memory leaf
7707060404SJosef Bacik 	 */
785103e947SJosef Bacik 	data_ptr = (unsigned long)((char *)(di + 1) +
795103e947SJosef Bacik 				   btrfs_dir_name_len(leaf, di));
805103e947SJosef Bacik 	read_extent_buffer(leaf, buffer, data_ptr,
813acd7ee8SJosef Bacik 			   btrfs_dir_data_len(leaf, di));
825103e947SJosef Bacik 	ret = btrfs_dir_data_len(leaf, di);
835103e947SJosef Bacik 
845103e947SJosef Bacik out:
855103e947SJosef Bacik 	btrfs_free_path(path);
865103e947SJosef Bacik 	return ret;
875103e947SJosef Bacik }
885103e947SJosef Bacik 
89f34f57a3SYan, Zheng static int do_setxattr(struct btrfs_trans_handle *trans,
90f34f57a3SYan, Zheng 		       struct inode *inode, const char *name,
9195819c05SChristoph Hellwig 		       const void *value, size_t size, int flags)
925103e947SJosef Bacik {
935103e947SJosef Bacik 	struct btrfs_dir_item *di;
945103e947SJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
955103e947SJosef Bacik 	struct btrfs_path *path;
96f34f57a3SYan, Zheng 	size_t name_len = strlen(name);
97f34f57a3SYan, Zheng 	int ret = 0;
98f34f57a3SYan, Zheng 
99f34f57a3SYan, Zheng 	if (name_len + size > BTRFS_MAX_XATTR_SIZE(root))
100f34f57a3SYan, Zheng 		return -ENOSPC;
1015103e947SJosef Bacik 
1025103e947SJosef Bacik 	path = btrfs_alloc_path();
10395819c05SChristoph Hellwig 	if (!path)
1045103e947SJosef Bacik 		return -ENOMEM;
1055103e947SJosef Bacik 
106fa09200bSJosef Bacik 	if (flags & XATTR_REPLACE) {
10733345d01SLi Zefan 		di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode), name,
108fa09200bSJosef Bacik 					name_len, -1);
1095103e947SJosef Bacik 		if (IS_ERR(di)) {
1105103e947SJosef Bacik 			ret = PTR_ERR(di);
1115103e947SJosef Bacik 			goto out;
112fa09200bSJosef Bacik 		} else if (!di) {
1135103e947SJosef Bacik 			ret = -ENODATA;
1145103e947SJosef Bacik 			goto out;
1155103e947SJosef Bacik 		}
116fa09200bSJosef Bacik 		ret = btrfs_delete_one_dir_name(trans, root, path, di);
117fa09200bSJosef Bacik 		if (ret)
118fa09200bSJosef Bacik 			goto out;
119fa09200bSJosef Bacik 		btrfs_release_path(path);
1204815053aSDavid Sterba 
1214815053aSDavid Sterba 		/*
1224815053aSDavid Sterba 		 * remove the attribute
1234815053aSDavid Sterba 		 */
1244815053aSDavid Sterba 		if (!value)
1254815053aSDavid Sterba 			goto out;
12601e6deb2SLiu Bo 	} else {
12701e6deb2SLiu Bo 		di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode),
12801e6deb2SLiu Bo 					name, name_len, 0);
12901e6deb2SLiu Bo 		if (IS_ERR(di)) {
13001e6deb2SLiu Bo 			ret = PTR_ERR(di);
13101e6deb2SLiu Bo 			goto out;
13201e6deb2SLiu Bo 		}
13301e6deb2SLiu Bo 		if (!di && !value)
13401e6deb2SLiu Bo 			goto out;
13501e6deb2SLiu Bo 		btrfs_release_path(path);
13633268eafSJosef Bacik 	}
1375103e947SJosef Bacik 
138fa09200bSJosef Bacik again:
13933345d01SLi Zefan 	ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode),
140f34f57a3SYan, Zheng 				      name, name_len, value, size);
141ed3ee9f4SJosef Bacik 	/*
142ed3ee9f4SJosef Bacik 	 * If we're setting an xattr to a new value but the new value is say
143ed3ee9f4SJosef Bacik 	 * exactly BTRFS_MAX_XATTR_SIZE, we could end up with EOVERFLOW getting
144ed3ee9f4SJosef Bacik 	 * back from split_leaf.  This is because it thinks we'll be extending
145ed3ee9f4SJosef Bacik 	 * the existing item size, but we're asking for enough space to add the
146ed3ee9f4SJosef Bacik 	 * item itself.  So if we get EOVERFLOW just set ret to EEXIST and let
147ed3ee9f4SJosef Bacik 	 * the rest of the function figure it out.
148ed3ee9f4SJosef Bacik 	 */
149ed3ee9f4SJosef Bacik 	if (ret == -EOVERFLOW)
150ed3ee9f4SJosef Bacik 		ret = -EEXIST;
151ed3ee9f4SJosef Bacik 
152fa09200bSJosef Bacik 	if (ret == -EEXIST) {
153fa09200bSJosef Bacik 		if (flags & XATTR_CREATE)
154fa09200bSJosef Bacik 			goto out;
155fa09200bSJosef Bacik 		/*
156fa09200bSJosef Bacik 		 * We can't use the path we already have since we won't have the
157fa09200bSJosef Bacik 		 * proper locking for a delete, so release the path and
158fa09200bSJosef Bacik 		 * re-lookup to delete the thing.
159fa09200bSJosef Bacik 		 */
160fa09200bSJosef Bacik 		btrfs_release_path(path);
161fa09200bSJosef Bacik 		di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode),
162fa09200bSJosef Bacik 					name, name_len, -1);
163fa09200bSJosef Bacik 		if (IS_ERR(di)) {
164fa09200bSJosef Bacik 			ret = PTR_ERR(di);
165fa09200bSJosef Bacik 			goto out;
166fa09200bSJosef Bacik 		} else if (!di) {
167fa09200bSJosef Bacik 			/* Shouldn't happen but just in case... */
168fa09200bSJosef Bacik 			btrfs_release_path(path);
169fa09200bSJosef Bacik 			goto again;
170fa09200bSJosef Bacik 		}
171fa09200bSJosef Bacik 
172fa09200bSJosef Bacik 		ret = btrfs_delete_one_dir_name(trans, root, path, di);
173fa09200bSJosef Bacik 		if (ret)
174fa09200bSJosef Bacik 			goto out;
175fa09200bSJosef Bacik 
176fa09200bSJosef Bacik 		/*
177fa09200bSJosef Bacik 		 * We have a value to set, so go back and try to insert it now.
178fa09200bSJosef Bacik 		 */
179fa09200bSJosef Bacik 		if (value) {
180fa09200bSJosef Bacik 			btrfs_release_path(path);
181fa09200bSJosef Bacik 			goto again;
182fa09200bSJosef Bacik 		}
183fa09200bSJosef Bacik 	}
1845103e947SJosef Bacik out:
185f34f57a3SYan, Zheng 	btrfs_free_path(path);
186f34f57a3SYan, Zheng 	return ret;
1875103e947SJosef Bacik }
1885103e947SJosef Bacik 
1894815053aSDavid Sterba /*
1904815053aSDavid Sterba  * @value: "" makes the attribute to empty, NULL removes it
1914815053aSDavid Sterba  */
192f34f57a3SYan, Zheng int __btrfs_setxattr(struct btrfs_trans_handle *trans,
193f34f57a3SYan, Zheng 		     struct inode *inode, const char *name,
194f34f57a3SYan, Zheng 		     const void *value, size_t size, int flags)
195f34f57a3SYan, Zheng {
196f34f57a3SYan, Zheng 	struct btrfs_root *root = BTRFS_I(inode)->root;
197f34f57a3SYan, Zheng 	int ret;
198f34f57a3SYan, Zheng 
199f34f57a3SYan, Zheng 	if (trans)
200f34f57a3SYan, Zheng 		return do_setxattr(trans, inode, name, value, size, flags);
201f34f57a3SYan, Zheng 
202a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 2);
203a22285a6SYan, Zheng 	if (IS_ERR(trans))
204a22285a6SYan, Zheng 		return PTR_ERR(trans);
205f34f57a3SYan, Zheng 
206f34f57a3SYan, Zheng 	ret = do_setxattr(trans, inode, name, value, size, flags);
207f34f57a3SYan, Zheng 	if (ret)
208f34f57a3SYan, Zheng 		goto out;
209f34f57a3SYan, Zheng 
2100c4d2d95SJosef Bacik 	inode_inc_iversion(inode);
211f34f57a3SYan, Zheng 	inode->i_ctime = CURRENT_TIME;
212e9976151SJosef Bacik 	set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
213f34f57a3SYan, Zheng 	ret = btrfs_update_inode(trans, root, inode);
214f34f57a3SYan, Zheng 	BUG_ON(ret);
215f34f57a3SYan, Zheng out:
2167ad85bb7SJosef Bacik 	btrfs_end_transaction(trans, root);
2175103e947SJosef Bacik 	return ret;
2185103e947SJosef Bacik }
2195103e947SJosef Bacik 
2205103e947SJosef Bacik ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
2215103e947SJosef Bacik {
2225103e947SJosef Bacik 	struct btrfs_key key, found_key;
2235103e947SJosef Bacik 	struct inode *inode = dentry->d_inode;
2245103e947SJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
2255103e947SJosef Bacik 	struct btrfs_path *path;
2265103e947SJosef Bacik 	struct extent_buffer *leaf;
2275103e947SJosef Bacik 	struct btrfs_dir_item *di;
2282e6a0035SLi Zefan 	int ret = 0, slot;
229eaa47d86SChristoph Hellwig 	size_t total_size = 0, size_left = size;
2305103e947SJosef Bacik 	unsigned long name_ptr;
231eaa47d86SChristoph Hellwig 	size_t name_len;
2325103e947SJosef Bacik 
2335103e947SJosef Bacik 	/*
2345103e947SJosef Bacik 	 * ok we want all objects associated with this id.
2355103e947SJosef Bacik 	 * NOTE: we set key.offset = 0; because we want to start with the
2365103e947SJosef Bacik 	 * first xattr that we find and walk forward
2375103e947SJosef Bacik 	 */
23833345d01SLi Zefan 	key.objectid = btrfs_ino(inode);
2395103e947SJosef Bacik 	btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
2405103e947SJosef Bacik 	key.offset = 0;
2415103e947SJosef Bacik 
2425103e947SJosef Bacik 	path = btrfs_alloc_path();
2435103e947SJosef Bacik 	if (!path)
2445103e947SJosef Bacik 		return -ENOMEM;
2451caf9342SJosef Bacik 	path->reada = 2;
2465103e947SJosef Bacik 
2475103e947SJosef Bacik 	/* search for our xattrs */
2485103e947SJosef Bacik 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2495103e947SJosef Bacik 	if (ret < 0)
2505103e947SJosef Bacik 		goto err;
2512e6a0035SLi Zefan 
2525103e947SJosef Bacik 	while (1) {
2535103e947SJosef Bacik 		leaf = path->nodes[0];
2545103e947SJosef Bacik 		slot = path->slots[0];
2555103e947SJosef Bacik 
2565103e947SJosef Bacik 		/* this is where we start walking through the path */
2572e6a0035SLi Zefan 		if (slot >= btrfs_header_nritems(leaf)) {
2585103e947SJosef Bacik 			/*
2595103e947SJosef Bacik 			 * if we've reached the last slot in this leaf we need
2605103e947SJosef Bacik 			 * to go to the next leaf and reset everything
2615103e947SJosef Bacik 			 */
2625103e947SJosef Bacik 			ret = btrfs_next_leaf(root, path);
2632e6a0035SLi Zefan 			if (ret < 0)
2642e6a0035SLi Zefan 				goto err;
2652e6a0035SLi Zefan 			else if (ret > 0)
2665103e947SJosef Bacik 				break;
2672e6a0035SLi Zefan 			continue;
2685103e947SJosef Bacik 		}
2695103e947SJosef Bacik 
2705103e947SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
2715103e947SJosef Bacik 
2725103e947SJosef Bacik 		/* check to make sure this item is what we want */
2735103e947SJosef Bacik 		if (found_key.objectid != key.objectid)
2745103e947SJosef Bacik 			break;
2755103e947SJosef Bacik 		if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
2765103e947SJosef Bacik 			break;
2775103e947SJosef Bacik 
2785103e947SJosef Bacik 		di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
27922a94d44SJosef Bacik 		if (verify_dir_item(root, leaf, di))
280db2254bcSLiu Bo 			goto next;
2815103e947SJosef Bacik 
282eaa47d86SChristoph Hellwig 		name_len = btrfs_dir_name_len(leaf, di);
283eaa47d86SChristoph Hellwig 		total_size += name_len + 1;
2845103e947SJosef Bacik 
2855103e947SJosef Bacik 		/* we are just looking for how big our buffer needs to be */
2865103e947SJosef Bacik 		if (!size)
2872e6a0035SLi Zefan 			goto next;
2885103e947SJosef Bacik 
289eaa47d86SChristoph Hellwig 		if (!buffer || (name_len + 1) > size_left) {
2905103e947SJosef Bacik 			ret = -ERANGE;
291b16281c3SYehuda Sadeh Weinraub 			goto err;
2925103e947SJosef Bacik 		}
2935103e947SJosef Bacik 
294eaa47d86SChristoph Hellwig 		name_ptr = (unsigned long)(di + 1);
295eaa47d86SChristoph Hellwig 		read_extent_buffer(leaf, buffer, name_ptr, name_len);
296eaa47d86SChristoph Hellwig 		buffer[name_len] = '\0';
297eaa47d86SChristoph Hellwig 
298eaa47d86SChristoph Hellwig 		size_left -= name_len + 1;
299eaa47d86SChristoph Hellwig 		buffer += name_len + 1;
3002e6a0035SLi Zefan next:
3012e6a0035SLi Zefan 		path->slots[0]++;
3025103e947SJosef Bacik 	}
3035103e947SJosef Bacik 	ret = total_size;
3045103e947SJosef Bacik 
3055103e947SJosef Bacik err:
3065103e947SJosef Bacik 	btrfs_free_path(path);
3075103e947SJosef Bacik 
3085103e947SJosef Bacik 	return ret;
3095103e947SJosef Bacik }
3105103e947SJosef Bacik 
3115103e947SJosef Bacik /*
31295819c05SChristoph Hellwig  * List of handlers for synthetic system.* attributes.  All real ondisk
31395819c05SChristoph Hellwig  * attributes are handled directly.
3145103e947SJosef Bacik  */
315f01cbd3fSStephen Hemminger const struct xattr_handler *btrfs_xattr_handlers[] = {
3160eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
317*996a710dSChristoph Hellwig 	&posix_acl_access_xattr_handler,
318*996a710dSChristoph Hellwig 	&posix_acl_default_xattr_handler,
31995819c05SChristoph Hellwig #endif
32095819c05SChristoph Hellwig 	NULL,
32195819c05SChristoph Hellwig };
32295819c05SChristoph Hellwig 
32395819c05SChristoph Hellwig /*
32495819c05SChristoph Hellwig  * Check if the attribute is in a supported namespace.
32595819c05SChristoph Hellwig  *
32695819c05SChristoph Hellwig  * This applied after the check for the synthetic attributes in the system
32795819c05SChristoph Hellwig  * namespace.
32895819c05SChristoph Hellwig  */
32995819c05SChristoph Hellwig static bool btrfs_is_valid_xattr(const char *name)
33095819c05SChristoph Hellwig {
331d397712bSChris Mason 	return !strncmp(name, XATTR_SECURITY_PREFIX,
332d397712bSChris Mason 			XATTR_SECURITY_PREFIX_LEN) ||
33395819c05SChristoph Hellwig 	       !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
33495819c05SChristoph Hellwig 	       !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
33595819c05SChristoph Hellwig 	       !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
33669a32ac5SChris Mason }
33769a32ac5SChris Mason 
33895819c05SChristoph Hellwig ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
33995819c05SChristoph Hellwig 		       void *buffer, size_t size)
34095819c05SChristoph Hellwig {
34195819c05SChristoph Hellwig 	/*
34295819c05SChristoph Hellwig 	 * If this is a request for a synthetic attribute in the system.*
34395819c05SChristoph Hellwig 	 * namespace use the generic infrastructure to resolve a handler
34495819c05SChristoph Hellwig 	 * for it via sb->s_xattr.
34595819c05SChristoph Hellwig 	 */
34695819c05SChristoph Hellwig 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
34795819c05SChristoph Hellwig 		return generic_getxattr(dentry, name, buffer, size);
3485103e947SJosef Bacik 
34995819c05SChristoph Hellwig 	if (!btrfs_is_valid_xattr(name))
35095819c05SChristoph Hellwig 		return -EOPNOTSUPP;
35195819c05SChristoph Hellwig 	return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
35295819c05SChristoph Hellwig }
3535103e947SJosef Bacik 
35495819c05SChristoph Hellwig int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
35595819c05SChristoph Hellwig 		   size_t size, int flags)
35695819c05SChristoph Hellwig {
357b83cc969SLi Zefan 	struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root;
358b83cc969SLi Zefan 
359b83cc969SLi Zefan 	/*
360b83cc969SLi Zefan 	 * The permission on security.* and system.* is not checked
361b83cc969SLi Zefan 	 * in permission().
362b83cc969SLi Zefan 	 */
363b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
364b83cc969SLi Zefan 		return -EROFS;
365b83cc969SLi Zefan 
36695819c05SChristoph Hellwig 	/*
36795819c05SChristoph Hellwig 	 * If this is a request for a synthetic attribute in the system.*
36895819c05SChristoph Hellwig 	 * namespace use the generic infrastructure to resolve a handler
36995819c05SChristoph Hellwig 	 * for it via sb->s_xattr.
37095819c05SChristoph Hellwig 	 */
37195819c05SChristoph Hellwig 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
37295819c05SChristoph Hellwig 		return generic_setxattr(dentry, name, value, size, flags);
3735103e947SJosef Bacik 
37495819c05SChristoph Hellwig 	if (!btrfs_is_valid_xattr(name))
37595819c05SChristoph Hellwig 		return -EOPNOTSUPP;
3765103e947SJosef Bacik 
37795819c05SChristoph Hellwig 	if (size == 0)
37895819c05SChristoph Hellwig 		value = "";  /* empty EA, do not remove */
379f34f57a3SYan, Zheng 
380f34f57a3SYan, Zheng 	return __btrfs_setxattr(NULL, dentry->d_inode, name, value, size,
381f34f57a3SYan, Zheng 				flags);
38295819c05SChristoph Hellwig }
38395819c05SChristoph Hellwig 
38495819c05SChristoph Hellwig int btrfs_removexattr(struct dentry *dentry, const char *name)
38595819c05SChristoph Hellwig {
386b83cc969SLi Zefan 	struct btrfs_root *root = BTRFS_I(dentry->d_inode)->root;
387b83cc969SLi Zefan 
388b83cc969SLi Zefan 	/*
389b83cc969SLi Zefan 	 * The permission on security.* and system.* is not checked
390b83cc969SLi Zefan 	 * in permission().
391b83cc969SLi Zefan 	 */
392b83cc969SLi Zefan 	if (btrfs_root_readonly(root))
393b83cc969SLi Zefan 		return -EROFS;
394b83cc969SLi Zefan 
39595819c05SChristoph Hellwig 	/*
39695819c05SChristoph Hellwig 	 * If this is a request for a synthetic attribute in the system.*
39795819c05SChristoph Hellwig 	 * namespace use the generic infrastructure to resolve a handler
39895819c05SChristoph Hellwig 	 * for it via sb->s_xattr.
39995819c05SChristoph Hellwig 	 */
40095819c05SChristoph Hellwig 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
40195819c05SChristoph Hellwig 		return generic_removexattr(dentry, name);
40295819c05SChristoph Hellwig 
40395819c05SChristoph Hellwig 	if (!btrfs_is_valid_xattr(name))
40495819c05SChristoph Hellwig 		return -EOPNOTSUPP;
405f34f57a3SYan, Zheng 
406f34f57a3SYan, Zheng 	return __btrfs_setxattr(NULL, dentry->d_inode, name, NULL, 0,
407f34f57a3SYan, Zheng 				XATTR_REPLACE);
40895819c05SChristoph Hellwig }
4090279b4cdSJim Owens 
41048a3b636SEric Sandeen static int btrfs_initxattrs(struct inode *inode,
41148a3b636SEric Sandeen 			    const struct xattr *xattr_array, void *fs_info)
4129d8f13baSMimi Zohar {
4139d8f13baSMimi Zohar 	const struct xattr *xattr;
4149d8f13baSMimi Zohar 	struct btrfs_trans_handle *trans = fs_info;
4159d8f13baSMimi Zohar 	char *name;
4169d8f13baSMimi Zohar 	int err = 0;
4179d8f13baSMimi Zohar 
4189d8f13baSMimi Zohar 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
4199d8f13baSMimi Zohar 		name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
4209d8f13baSMimi Zohar 			       strlen(xattr->name) + 1, GFP_NOFS);
4219d8f13baSMimi Zohar 		if (!name) {
4229d8f13baSMimi Zohar 			err = -ENOMEM;
4239d8f13baSMimi Zohar 			break;
4249d8f13baSMimi Zohar 		}
4259d8f13baSMimi Zohar 		strcpy(name, XATTR_SECURITY_PREFIX);
4269d8f13baSMimi Zohar 		strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
4279d8f13baSMimi Zohar 		err = __btrfs_setxattr(trans, inode, name,
4289d8f13baSMimi Zohar 				       xattr->value, xattr->value_len, 0);
4299d8f13baSMimi Zohar 		kfree(name);
4309d8f13baSMimi Zohar 		if (err < 0)
4319d8f13baSMimi Zohar 			break;
4329d8f13baSMimi Zohar 	}
4339d8f13baSMimi Zohar 	return err;
4349d8f13baSMimi Zohar }
4359d8f13baSMimi Zohar 
436f34f57a3SYan, Zheng int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
4372a7dba39SEric Paris 			      struct inode *inode, struct inode *dir,
4382a7dba39SEric Paris 			      const struct qstr *qstr)
4390279b4cdSJim Owens {
4409d8f13baSMimi Zohar 	return security_inode_init_security(inode, dir, qstr,
4419d8f13baSMimi Zohar 					    &btrfs_initxattrs, trans);
4420279b4cdSJim Owens }
443