xattr.c 9.5 KB
Newer Older
J
Josef Bacik 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright (C) 2007 Red Hat.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/rwsem.h>
#include <linux/xattr.h>
J
Jim Owens 已提交
24
#include <linux/security.h>
J
Josef Bacik 已提交
25 26 27 28 29
#include "ctree.h"
#include "btrfs_inode.h"
#include "transaction.h"
#include "xattr.h"
#include "disk-io.h"
J
Josef Bacik 已提交
30

J
Josef Bacik 已提交
31

32 33
ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
				void *buffer, size_t size)
J
Josef Bacik 已提交
34 35 36 37 38 39 40 41 42
{
	struct btrfs_dir_item *di;
	struct btrfs_root *root = BTRFS_I(inode)->root;
	struct btrfs_path *path;
	struct extent_buffer *leaf;
	int ret = 0;
	unsigned long data_ptr;

	path = btrfs_alloc_path();
43
	if (!path)
J
Josef Bacik 已提交
44 45 46 47 48
		return -ENOMEM;

	/* lookup the xattr by name */
	di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name,
				strlen(name), 0);
J
Josef Bacik 已提交
49
	if (!di) {
J
Josef Bacik 已提交
50 51
		ret = -ENODATA;
		goto out;
J
Josef Bacik 已提交
52 53 54
	} else if (IS_ERR(di)) {
		ret = PTR_ERR(di);
		goto out;
J
Josef Bacik 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68
	}

	leaf = path->nodes[0];
	/* if size is 0, that means we want the size of the attr */
	if (!size) {
		ret = btrfs_dir_data_len(leaf, di);
		goto out;
	}

	/* now get the data out of our dir_item */
	if (btrfs_dir_data_len(leaf, di) > size) {
		ret = -ERANGE;
		goto out;
	}
J
Josef Bacik 已提交
69 70 71 72 73 74 75 76

	/*
	 * The way things are packed into the leaf is like this
	 * |struct btrfs_dir_item|name|data|
	 * where name is the xattr name, so security.foo, and data is the
	 * content of the xattr.  data_ptr points to the location in memory
	 * where the data starts in the in memory leaf
	 */
J
Josef Bacik 已提交
77 78 79
	data_ptr = (unsigned long)((char *)(di + 1) +
				   btrfs_dir_name_len(leaf, di));
	read_extent_buffer(leaf, buffer, data_ptr,
J
Josef Bacik 已提交
80
			   btrfs_dir_data_len(leaf, di));
J
Josef Bacik 已提交
81 82 83 84 85 86 87
	ret = btrfs_dir_data_len(leaf, di);

out:
	btrfs_free_path(path);
	return ret;
}

88 89 90
static int do_setxattr(struct btrfs_trans_handle *trans,
		       struct inode *inode, const char *name,
		       const void *value, size_t size, int flags)
J
Josef Bacik 已提交
91 92 93 94
{
	struct btrfs_dir_item *di;
	struct btrfs_root *root = BTRFS_I(inode)->root;
	struct btrfs_path *path;
95 96 97 98 99
	size_t name_len = strlen(name);
	int ret = 0;

	if (name_len + size > BTRFS_MAX_XATTR_SIZE(root))
		return -ENOSPC;
J
Josef Bacik 已提交
100 101

	path = btrfs_alloc_path();
102
	if (!path)
J
Josef Bacik 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
		return -ENOMEM;

	/* first lets see if we already have this xattr */
	di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name,
				strlen(name), -1);
	if (IS_ERR(di)) {
		ret = PTR_ERR(di);
		goto out;
	}

	/* ok we already have this xattr, lets remove it */
	if (di) {
		/* if we want create only exit */
		if (flags & XATTR_CREATE) {
			ret = -EEXIST;
			goto out;
		}

		ret = btrfs_delete_one_dir_name(trans, root, path, di);
122
		BUG_ON(ret);
J
Josef Bacik 已提交
123 124 125
		btrfs_release_path(root, path);

		/* if we don't have a value then we are removing the xattr */
126
		if (!value)
J
Josef Bacik 已提交
127
			goto out;
J
Josef Bacik 已提交
128 129 130 131 132 133 134 135
	} else {
		btrfs_release_path(root, path);

		if (flags & XATTR_REPLACE) {
			/* we couldn't find the attr to replace */
			ret = -ENODATA;
			goto out;
		}
J
Josef Bacik 已提交
136 137 138
	}

	/* ok we have to create a completely new xattr */
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	ret = btrfs_insert_xattr_item(trans, root, path, inode->i_ino,
				      name, name_len, value, size);
	BUG_ON(ret);
out:
	btrfs_free_path(path);
	return ret;
}

int __btrfs_setxattr(struct btrfs_trans_handle *trans,
		     struct inode *inode, const char *name,
		     const void *value, size_t size, int flags)
{
	struct btrfs_root *root = BTRFS_I(inode)->root;
	int ret;

	if (trans)
		return do_setxattr(trans, inode, name, value, size, flags);

157 158 159
	trans = btrfs_start_transaction(root, 2);
	if (IS_ERR(trans))
		return PTR_ERR(trans);
J
Josef Bacik 已提交
160

161
	btrfs_set_trans_block_group(trans, inode);
J
Josef Bacik 已提交
162

163 164 165 166 167 168 169 170 171
	ret = do_setxattr(trans, inode, name, value, size, flags);
	if (ret)
		goto out;

	inode->i_ctime = CURRENT_TIME;
	ret = btrfs_update_inode(trans, root, inode);
	BUG_ON(ret);
out:
	btrfs_end_transaction_throttle(trans, root);
J
Josef Bacik 已提交
172 173 174 175 176 177 178 179 180 181 182 183
	return ret;
}

ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
{
	struct btrfs_key key, found_key;
	struct inode *inode = dentry->d_inode;
	struct btrfs_root *root = BTRFS_I(inode)->root;
	struct btrfs_path *path;
	struct extent_buffer *leaf;
	struct btrfs_dir_item *di;
	int ret = 0, slot, advance;
C
Christoph Hellwig 已提交
184
	size_t total_size = 0, size_left = size;
J
Josef Bacik 已提交
185
	unsigned long name_ptr;
C
Christoph Hellwig 已提交
186
	size_t name_len;
J
Josef Bacik 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200
	u32 nritems;

	/*
	 * ok we want all objects associated with this id.
	 * NOTE: we set key.offset = 0; because we want to start with the
	 * first xattr that we find and walk forward
	 */
	key.objectid = inode->i_ino;
	btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
	key.offset = 0;

	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;
201
	path->reada = 2;
J
Josef Bacik 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

	/* search for our xattrs */
	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
	if (ret < 0)
		goto err;
	advance = 0;
	while (1) {
		leaf = path->nodes[0];
		nritems = btrfs_header_nritems(leaf);
		slot = path->slots[0];

		/* this is where we start walking through the path */
		if (advance || slot >= nritems) {
			/*
			 * if we've reached the last slot in this leaf we need
			 * to go to the next leaf and reset everything
			 */
			if (slot >= nritems-1) {
				ret = btrfs_next_leaf(root, path);
				if (ret)
					break;
				leaf = path->nodes[0];
				nritems = btrfs_header_nritems(leaf);
				slot = path->slots[0];
			} else {
				/*
				 * just walking through the slots on this leaf
				 */
				slot++;
				path->slots[0]++;
			}
		}
		advance = 1;

		btrfs_item_key_to_cpu(leaf, &found_key, slot);

		/* check to make sure this item is what we want */
		if (found_key.objectid != key.objectid)
			break;
		if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
			break;

		di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);

C
Christoph Hellwig 已提交
246 247
		name_len = btrfs_dir_name_len(leaf, di);
		total_size += name_len + 1;
J
Josef Bacik 已提交
248 249 250 251 252

		/* we are just looking for how big our buffer needs to be */
		if (!size)
			continue;

C
Christoph Hellwig 已提交
253
		if (!buffer || (name_len + 1) > size_left) {
J
Josef Bacik 已提交
254
			ret = -ERANGE;
255
			goto err;
J
Josef Bacik 已提交
256 257
		}

C
Christoph Hellwig 已提交
258 259 260 261 262 263
		name_ptr = (unsigned long)(di + 1);
		read_extent_buffer(leaf, buffer, name_ptr, name_len);
		buffer[name_len] = '\0';

		size_left -= name_len + 1;
		buffer += name_len + 1;
J
Josef Bacik 已提交
264 265 266 267 268 269 270 271 272 273
	}
	ret = total_size;

err:
	btrfs_free_path(path);

	return ret;
}

/*
274 275 276
 * List of handlers for synthetic system.* attributes.  All real ondisk
 * attributes are handled directly.
 */
277
const struct xattr_handler *btrfs_xattr_handlers[] = {
C
Chris Mason 已提交
278
#ifdef CONFIG_BTRFS_FS_POSIX_ACL
279 280 281 282 283 284 285 286 287 288 289
	&btrfs_xattr_acl_access_handler,
	&btrfs_xattr_acl_default_handler,
#endif
	NULL,
};

/*
 * Check if the attribute is in a supported namespace.
 *
 * This applied after the check for the synthetic attributes in the system
 * namespace.
J
Josef Bacik 已提交
290
 */
291 292
static bool btrfs_is_valid_xattr(const char *name)
{
C
Chris Mason 已提交
293 294
	return !strncmp(name, XATTR_SECURITY_PREFIX,
			XATTR_SECURITY_PREFIX_LEN) ||
295 296 297
	       !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
	       !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
	       !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
298 299
}

300 301 302 303 304 305 306 307 308 309
ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
		       void *buffer, size_t size)
{
	/*
	 * If this is a request for a synthetic attribute in the system.*
	 * namespace use the generic infrastructure to resolve a handler
	 * for it via sb->s_xattr.
	 */
	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
		return generic_getxattr(dentry, name, buffer, size);
J
Josef Bacik 已提交
310

311 312 313 314
	if (!btrfs_is_valid_xattr(name))
		return -EOPNOTSUPP;
	return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
}
J
Josef Bacik 已提交
315

316 317 318 319 320 321 322 323 324 325
int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
		   size_t size, int flags)
{
	/*
	 * If this is a request for a synthetic attribute in the system.*
	 * namespace use the generic infrastructure to resolve a handler
	 * for it via sb->s_xattr.
	 */
	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
		return generic_setxattr(dentry, name, value, size, flags);
J
Josef Bacik 已提交
326

327 328
	if (!btrfs_is_valid_xattr(name))
		return -EOPNOTSUPP;
J
Josef Bacik 已提交
329

330 331
	if (size == 0)
		value = "";  /* empty EA, do not remove */
332 333 334

	return __btrfs_setxattr(NULL, dentry->d_inode, name, value, size,
				flags);
335 336 337 338 339 340 341 342 343 344 345 346 347 348
}

int btrfs_removexattr(struct dentry *dentry, const char *name)
{
	/*
	 * If this is a request for a synthetic attribute in the system.*
	 * namespace use the generic infrastructure to resolve a handler
	 * for it via sb->s_xattr.
	 */
	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
		return generic_removexattr(dentry, name);

	if (!btrfs_is_valid_xattr(name))
		return -EOPNOTSUPP;
349 350 351

	return __btrfs_setxattr(NULL, dentry->d_inode, name, NULL, 0,
				XATTR_REPLACE);
352
}
J
Jim Owens 已提交
353

354 355
int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
			      struct inode *inode, struct inode *dir)
J
Jim Owens 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
{
	int err;
	size_t len;
	void *value;
	char *suffix;
	char *name;

	err = security_inode_init_security(inode, dir, &suffix, &value, &len);
	if (err) {
		if (err == -EOPNOTSUPP)
			return 0;
		return err;
	}

	name = kmalloc(XATTR_SECURITY_PREFIX_LEN + strlen(suffix) + 1,
		       GFP_NOFS);
	if (!name) {
		err = -ENOMEM;
	} else {
		strcpy(name, XATTR_SECURITY_PREFIX);
		strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
377
		err = __btrfs_setxattr(trans, inode, name, value, len, 0);
J
Jim Owens 已提交
378 379 380 381 382 383 384
		kfree(name);
	}

	kfree(suffix);
	kfree(value);
	return err;
}