xref: /openbmc/linux/fs/btrfs/xattr.c (revision 070604040b86511cc2df0f25f98e26c5529bd928)
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>
245103e947SJosef Bacik #include "ctree.h"
255103e947SJosef Bacik #include "btrfs_inode.h"
265103e947SJosef Bacik #include "transaction.h"
275103e947SJosef Bacik #include "xattr.h"
285103e947SJosef Bacik #include "disk-io.h"
2933268eafSJosef Bacik 
3033268eafSJosef Bacik 
3195819c05SChristoph Hellwig ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
3295819c05SChristoph Hellwig 				void *buffer, size_t size)
335103e947SJosef Bacik {
345103e947SJosef Bacik 	struct btrfs_dir_item *di;
355103e947SJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
365103e947SJosef Bacik 	struct btrfs_path *path;
375103e947SJosef Bacik 	struct extent_buffer *leaf;
385103e947SJosef Bacik 	int ret = 0;
395103e947SJosef Bacik 	unsigned long data_ptr;
405103e947SJosef Bacik 
415103e947SJosef Bacik 	path = btrfs_alloc_path();
4295819c05SChristoph Hellwig 	if (!path)
435103e947SJosef Bacik 		return -ENOMEM;
445103e947SJosef Bacik 
455103e947SJosef Bacik 	/* lookup the xattr by name */
465103e947SJosef Bacik 	di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name,
475103e947SJosef Bacik 				strlen(name), 0);
48*07060404SJosef Bacik 	if (!di) {
495103e947SJosef Bacik 		ret = -ENODATA;
505103e947SJosef Bacik 		goto out;
51*07060404SJosef Bacik 	} else if (IS_ERR(di)) {
52*07060404SJosef Bacik 		ret = PTR_ERR(di);
53*07060404SJosef Bacik 		goto out;
545103e947SJosef Bacik 	}
555103e947SJosef Bacik 
565103e947SJosef Bacik 	leaf = path->nodes[0];
575103e947SJosef Bacik 	/* if size is 0, that means we want the size of the attr */
585103e947SJosef Bacik 	if (!size) {
595103e947SJosef Bacik 		ret = btrfs_dir_data_len(leaf, di);
605103e947SJosef Bacik 		goto out;
615103e947SJosef Bacik 	}
625103e947SJosef Bacik 
635103e947SJosef Bacik 	/* now get the data out of our dir_item */
645103e947SJosef Bacik 	if (btrfs_dir_data_len(leaf, di) > size) {
655103e947SJosef Bacik 		ret = -ERANGE;
665103e947SJosef Bacik 		goto out;
675103e947SJosef Bacik 	}
68*07060404SJosef Bacik 
69*07060404SJosef Bacik 	/*
70*07060404SJosef Bacik 	 * The way things are packed into the leaf is like this
71*07060404SJosef Bacik 	 * |struct btrfs_dir_item|name|data|
72*07060404SJosef Bacik 	 * where name is the xattr name, so security.foo, and data is the
73*07060404SJosef Bacik 	 * content of the xattr.  data_ptr points to the location in memory
74*07060404SJosef Bacik 	 * where the data starts in the in memory leaf
75*07060404SJosef Bacik 	 */
765103e947SJosef Bacik 	data_ptr = (unsigned long)((char *)(di + 1) +
775103e947SJosef Bacik 				   btrfs_dir_name_len(leaf, di));
785103e947SJosef Bacik 	read_extent_buffer(leaf, buffer, data_ptr,
793acd7ee8SJosef Bacik 			   btrfs_dir_data_len(leaf, di));
805103e947SJosef Bacik 	ret = btrfs_dir_data_len(leaf, di);
815103e947SJosef Bacik 
825103e947SJosef Bacik out:
835103e947SJosef Bacik 	btrfs_free_path(path);
845103e947SJosef Bacik 	return ret;
855103e947SJosef Bacik }
865103e947SJosef Bacik 
8795819c05SChristoph Hellwig int __btrfs_setxattr(struct inode *inode, const char *name,
8895819c05SChristoph Hellwig 			    const void *value, size_t size, int flags)
895103e947SJosef Bacik {
905103e947SJosef Bacik 	struct btrfs_dir_item *di;
915103e947SJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
925103e947SJosef Bacik 	struct btrfs_trans_handle *trans;
935103e947SJosef Bacik 	struct btrfs_path *path;
945103e947SJosef Bacik 	int ret = 0, mod = 0;
955103e947SJosef Bacik 
965103e947SJosef Bacik 	path = btrfs_alloc_path();
9795819c05SChristoph Hellwig 	if (!path)
985103e947SJosef Bacik 		return -ENOMEM;
995103e947SJosef Bacik 
1005103e947SJosef Bacik 	trans = btrfs_start_transaction(root, 1);
1015103e947SJosef Bacik 	btrfs_set_trans_block_group(trans, inode);
1025103e947SJosef Bacik 
1035103e947SJosef Bacik 	/* first lets see if we already have this xattr */
1045103e947SJosef Bacik 	di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name,
1055103e947SJosef Bacik 				strlen(name), -1);
1065103e947SJosef Bacik 	if (IS_ERR(di)) {
1075103e947SJosef Bacik 		ret = PTR_ERR(di);
1085103e947SJosef Bacik 		goto out;
1095103e947SJosef Bacik 	}
1105103e947SJosef Bacik 
1115103e947SJosef Bacik 	/* ok we already have this xattr, lets remove it */
1125103e947SJosef Bacik 	if (di) {
1135103e947SJosef Bacik 		/* if we want create only exit */
1145103e947SJosef Bacik 		if (flags & XATTR_CREATE) {
1155103e947SJosef Bacik 			ret = -EEXIST;
1165103e947SJosef Bacik 			goto out;
1175103e947SJosef Bacik 		}
1185103e947SJosef Bacik 
1195103e947SJosef Bacik 		ret = btrfs_delete_one_dir_name(trans, root, path, di);
1205103e947SJosef Bacik 		if (ret)
1215103e947SJosef Bacik 			goto out;
1225103e947SJosef Bacik 		btrfs_release_path(root, path);
1235103e947SJosef Bacik 
1245103e947SJosef Bacik 		/* if we don't have a value then we are removing the xattr */
1255103e947SJosef Bacik 		if (!value) {
1265103e947SJosef Bacik 			mod = 1;
1275103e947SJosef Bacik 			goto out;
1285103e947SJosef Bacik 		}
12933268eafSJosef Bacik 	} else {
13033268eafSJosef Bacik 		btrfs_release_path(root, path);
13133268eafSJosef Bacik 
13233268eafSJosef Bacik 		if (flags & XATTR_REPLACE) {
13333268eafSJosef Bacik 			/* we couldn't find the attr to replace */
1345103e947SJosef Bacik 			ret = -ENODATA;
1355103e947SJosef Bacik 			goto out;
1365103e947SJosef Bacik 		}
13733268eafSJosef Bacik 	}
1385103e947SJosef Bacik 
1395103e947SJosef Bacik 	/* ok we have to create a completely new xattr */
1405103e947SJosef Bacik 	ret = btrfs_insert_xattr_item(trans, root, name, strlen(name),
1415103e947SJosef Bacik 				      value, size, inode->i_ino);
1425103e947SJosef Bacik 	if (ret)
1435103e947SJosef Bacik 		goto out;
1445103e947SJosef Bacik 	mod = 1;
1455103e947SJosef Bacik 
1465103e947SJosef Bacik out:
1475103e947SJosef Bacik 	if (mod) {
1485103e947SJosef Bacik 		inode->i_ctime = CURRENT_TIME;
1495103e947SJosef Bacik 		ret = btrfs_update_inode(trans, root, inode);
1505103e947SJosef Bacik 	}
1515103e947SJosef Bacik 
1525103e947SJosef Bacik 	btrfs_end_transaction(trans, root);
1535103e947SJosef Bacik 	btrfs_free_path(path);
1545103e947SJosef Bacik 	return ret;
1555103e947SJosef Bacik }
1565103e947SJosef Bacik 
1575103e947SJosef Bacik ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
1585103e947SJosef Bacik {
1595103e947SJosef Bacik 	struct btrfs_key key, found_key;
1605103e947SJosef Bacik 	struct inode *inode = dentry->d_inode;
1615103e947SJosef Bacik 	struct btrfs_root *root = BTRFS_I(inode)->root;
1625103e947SJosef Bacik 	struct btrfs_path *path;
1635103e947SJosef Bacik 	struct btrfs_item *item;
1645103e947SJosef Bacik 	struct extent_buffer *leaf;
1655103e947SJosef Bacik 	struct btrfs_dir_item *di;
1665103e947SJosef Bacik 	int ret = 0, slot, advance;
167eaa47d86SChristoph Hellwig 	size_t total_size = 0, size_left = size;
1685103e947SJosef Bacik 	unsigned long name_ptr;
169eaa47d86SChristoph Hellwig 	size_t name_len;
1705103e947SJosef Bacik 	u32 nritems;
1715103e947SJosef Bacik 
1725103e947SJosef Bacik 	/*
1735103e947SJosef Bacik 	 * ok we want all objects associated with this id.
1745103e947SJosef Bacik 	 * NOTE: we set key.offset = 0; because we want to start with the
1755103e947SJosef Bacik 	 * first xattr that we find and walk forward
1765103e947SJosef Bacik 	 */
1775103e947SJosef Bacik 	key.objectid = inode->i_ino;
1785103e947SJosef Bacik 	btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
1795103e947SJosef Bacik 	key.offset = 0;
1805103e947SJosef Bacik 
1815103e947SJosef Bacik 	path = btrfs_alloc_path();
1825103e947SJosef Bacik 	if (!path)
1835103e947SJosef Bacik 		return -ENOMEM;
1841caf9342SJosef Bacik 	path->reada = 2;
1855103e947SJosef Bacik 
1865103e947SJosef Bacik 	/* search for our xattrs */
1875103e947SJosef Bacik 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1885103e947SJosef Bacik 	if (ret < 0)
1895103e947SJosef Bacik 		goto err;
1905103e947SJosef Bacik 	advance = 0;
1915103e947SJosef Bacik 	while (1) {
1925103e947SJosef Bacik 		leaf = path->nodes[0];
1935103e947SJosef Bacik 		nritems = btrfs_header_nritems(leaf);
1945103e947SJosef Bacik 		slot = path->slots[0];
1955103e947SJosef Bacik 
1965103e947SJosef Bacik 		/* this is where we start walking through the path */
1975103e947SJosef Bacik 		if (advance || slot >= nritems) {
1985103e947SJosef Bacik 			/*
1995103e947SJosef Bacik 			 * if we've reached the last slot in this leaf we need
2005103e947SJosef Bacik 			 * to go to the next leaf and reset everything
2015103e947SJosef Bacik 			 */
2025103e947SJosef Bacik 			if (slot >= nritems-1) {
2035103e947SJosef Bacik 				ret = btrfs_next_leaf(root, path);
2045103e947SJosef Bacik 				if (ret)
2055103e947SJosef Bacik 					break;
2065103e947SJosef Bacik 				leaf = path->nodes[0];
2075103e947SJosef Bacik 				nritems = btrfs_header_nritems(leaf);
2085103e947SJosef Bacik 				slot = path->slots[0];
2095103e947SJosef Bacik 			} else {
2105103e947SJosef Bacik 				/*
2115103e947SJosef Bacik 				 * just walking through the slots on this leaf
2125103e947SJosef Bacik 				 */
2135103e947SJosef Bacik 				slot++;
2145103e947SJosef Bacik 				path->slots[0]++;
2155103e947SJosef Bacik 			}
2165103e947SJosef Bacik 		}
2175103e947SJosef Bacik 		advance = 1;
2185103e947SJosef Bacik 
2195103e947SJosef Bacik 		item = btrfs_item_nr(leaf, slot);
2205103e947SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
2215103e947SJosef Bacik 
2225103e947SJosef Bacik 		/* check to make sure this item is what we want */
2235103e947SJosef Bacik 		if (found_key.objectid != key.objectid)
2245103e947SJosef Bacik 			break;
2255103e947SJosef Bacik 		if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
2265103e947SJosef Bacik 			break;
2275103e947SJosef Bacik 
2285103e947SJosef Bacik 		di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2295103e947SJosef Bacik 
230eaa47d86SChristoph Hellwig 		name_len = btrfs_dir_name_len(leaf, di);
231eaa47d86SChristoph Hellwig 		total_size += name_len + 1;
2325103e947SJosef Bacik 
2335103e947SJosef Bacik 		/* we are just looking for how big our buffer needs to be */
2345103e947SJosef Bacik 		if (!size)
2355103e947SJosef Bacik 			continue;
2365103e947SJosef Bacik 
237eaa47d86SChristoph Hellwig 		if (!buffer || (name_len + 1) > size_left) {
2385103e947SJosef Bacik 			ret = -ERANGE;
239b16281c3SYehuda Sadeh Weinraub 			goto err;
2405103e947SJosef Bacik 		}
2415103e947SJosef Bacik 
242eaa47d86SChristoph Hellwig 		name_ptr = (unsigned long)(di + 1);
243eaa47d86SChristoph Hellwig 		read_extent_buffer(leaf, buffer, name_ptr, name_len);
244eaa47d86SChristoph Hellwig 		buffer[name_len] = '\0';
245eaa47d86SChristoph Hellwig 
246eaa47d86SChristoph Hellwig 		size_left -= name_len + 1;
247eaa47d86SChristoph Hellwig 		buffer += name_len + 1;
2485103e947SJosef Bacik 	}
2495103e947SJosef Bacik 	ret = total_size;
2505103e947SJosef Bacik 
2515103e947SJosef Bacik err:
2525103e947SJosef Bacik 	btrfs_free_path(path);
2535103e947SJosef Bacik 
2545103e947SJosef Bacik 	return ret;
2555103e947SJosef Bacik }
2565103e947SJosef Bacik 
2575103e947SJosef Bacik /*
25895819c05SChristoph Hellwig  * List of handlers for synthetic system.* attributes.  All real ondisk
25995819c05SChristoph Hellwig  * attributes are handled directly.
2605103e947SJosef Bacik  */
26195819c05SChristoph Hellwig struct xattr_handler *btrfs_xattr_handlers[] = {
26295819c05SChristoph Hellwig #ifdef CONFIG_FS_POSIX_ACL
26395819c05SChristoph Hellwig 	&btrfs_xattr_acl_access_handler,
26495819c05SChristoph Hellwig 	&btrfs_xattr_acl_default_handler,
26595819c05SChristoph Hellwig #endif
26695819c05SChristoph Hellwig 	NULL,
26795819c05SChristoph Hellwig };
26895819c05SChristoph Hellwig 
26995819c05SChristoph Hellwig /*
27095819c05SChristoph Hellwig  * Check if the attribute is in a supported namespace.
27195819c05SChristoph Hellwig  *
27295819c05SChristoph Hellwig  * This applied after the check for the synthetic attributes in the system
27395819c05SChristoph Hellwig  * namespace.
27495819c05SChristoph Hellwig  */
27595819c05SChristoph Hellwig static bool btrfs_is_valid_xattr(const char *name)
27695819c05SChristoph Hellwig {
277d397712bSChris Mason 	return !strncmp(name, XATTR_SECURITY_PREFIX,
278d397712bSChris Mason 			XATTR_SECURITY_PREFIX_LEN) ||
27995819c05SChristoph Hellwig 	       !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
28095819c05SChristoph Hellwig 	       !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
28195819c05SChristoph Hellwig 	       !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
28269a32ac5SChris Mason }
28369a32ac5SChris Mason 
28495819c05SChristoph Hellwig ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
28595819c05SChristoph Hellwig 		       void *buffer, size_t size)
28695819c05SChristoph Hellwig {
28795819c05SChristoph Hellwig 	/*
28895819c05SChristoph Hellwig 	 * If this is a request for a synthetic attribute in the system.*
28995819c05SChristoph Hellwig 	 * namespace use the generic infrastructure to resolve a handler
29095819c05SChristoph Hellwig 	 * for it via sb->s_xattr.
29195819c05SChristoph Hellwig 	 */
29295819c05SChristoph Hellwig 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
29395819c05SChristoph Hellwig 		return generic_getxattr(dentry, name, buffer, size);
2945103e947SJosef Bacik 
29595819c05SChristoph Hellwig 	if (!btrfs_is_valid_xattr(name))
29695819c05SChristoph Hellwig 		return -EOPNOTSUPP;
29795819c05SChristoph Hellwig 	return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
29895819c05SChristoph Hellwig }
2995103e947SJosef Bacik 
30095819c05SChristoph Hellwig int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
30195819c05SChristoph Hellwig 		   size_t size, int flags)
30295819c05SChristoph Hellwig {
30395819c05SChristoph Hellwig 	/*
30495819c05SChristoph Hellwig 	 * If this is a request for a synthetic attribute in the system.*
30595819c05SChristoph Hellwig 	 * namespace use the generic infrastructure to resolve a handler
30695819c05SChristoph Hellwig 	 * for it via sb->s_xattr.
30795819c05SChristoph Hellwig 	 */
30895819c05SChristoph Hellwig 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
30995819c05SChristoph Hellwig 		return generic_setxattr(dentry, name, value, size, flags);
3105103e947SJosef Bacik 
31195819c05SChristoph Hellwig 	if (!btrfs_is_valid_xattr(name))
31295819c05SChristoph Hellwig 		return -EOPNOTSUPP;
3135103e947SJosef Bacik 
31495819c05SChristoph Hellwig 	if (size == 0)
31595819c05SChristoph Hellwig 		value = "";  /* empty EA, do not remove */
31695819c05SChristoph Hellwig 	return __btrfs_setxattr(dentry->d_inode, name, value, size, flags);
31795819c05SChristoph Hellwig }
31895819c05SChristoph Hellwig 
31995819c05SChristoph Hellwig int btrfs_removexattr(struct dentry *dentry, const char *name)
32095819c05SChristoph Hellwig {
32195819c05SChristoph Hellwig 	/*
32295819c05SChristoph Hellwig 	 * If this is a request for a synthetic attribute in the system.*
32395819c05SChristoph Hellwig 	 * namespace use the generic infrastructure to resolve a handler
32495819c05SChristoph Hellwig 	 * for it via sb->s_xattr.
32595819c05SChristoph Hellwig 	 */
32695819c05SChristoph Hellwig 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
32795819c05SChristoph Hellwig 		return generic_removexattr(dentry, name);
32895819c05SChristoph Hellwig 
32995819c05SChristoph Hellwig 	if (!btrfs_is_valid_xattr(name))
33095819c05SChristoph Hellwig 		return -EOPNOTSUPP;
33195819c05SChristoph Hellwig 	return __btrfs_setxattr(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
33295819c05SChristoph Hellwig }
333