xattr.c 13.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>
25
#include <linux/posix_acl_xattr.h>
J
Josef Bacik 已提交
26 27 28 29 30
#include "ctree.h"
#include "btrfs_inode.h"
#include "transaction.h"
#include "xattr.h"
#include "disk-io.h"
31
#include "props.h"
32
#include "locking.h"
J
Josef Bacik 已提交
33

J
Josef Bacik 已提交
34

35 36
ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
				void *buffer, size_t size)
J
Josef Bacik 已提交
37 38 39 40 41 42 43 44 45
{
	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();
46
	if (!path)
J
Josef Bacik 已提交
47 48 49
		return -ENOMEM;

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

	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 已提交
72 73 74 75 76 77 78 79

	/*
	 * 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 已提交
80 81 82
	data_ptr = (unsigned long)((char *)(di + 1) +
				   btrfs_dir_name_len(leaf, di));
	read_extent_buffer(leaf, buffer, data_ptr,
J
Josef Bacik 已提交
83
			   btrfs_dir_data_len(leaf, di));
J
Josef Bacik 已提交
84 85 86 87 88 89 90
	ret = btrfs_dir_data_len(leaf, di);

out:
	btrfs_free_path(path);
	return ret;
}

91 92 93
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 已提交
94
{
95
	struct btrfs_dir_item *di = NULL;
J
Josef Bacik 已提交
96 97
	struct btrfs_root *root = BTRFS_I(inode)->root;
	struct btrfs_path *path;
98 99 100 101 102
	size_t name_len = strlen(name);
	int ret = 0;

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

	path = btrfs_alloc_path();
105
	if (!path)
J
Josef Bacik 已提交
106
		return -ENOMEM;
107 108 109 110 111 112 113
	path->skip_release_on_error = 1;

	if (!value) {
		di = btrfs_lookup_xattr(trans, root, path, btrfs_ino(inode),
					name, name_len, -1);
		if (!di && (flags & XATTR_REPLACE))
			ret = -ENODATA;
114 115
		else if (IS_ERR(di))
			ret = PTR_ERR(di);
116 117 118 119
		else if (di)
			ret = btrfs_delete_one_dir_name(trans, root, path, di);
		goto out;
	}
J
Josef Bacik 已提交
120

121 122 123 124 125 126 127
	/*
	 * For a replace we can't just do the insert blindly.
	 * Do a lookup first (read-only btrfs_search_slot), and return if xattr
	 * doesn't exist. If it exists, fall down below to the insert/replace
	 * path - we can't race with a concurrent xattr delete, because the VFS
	 * locks the inode's i_mutex before calling setxattr or removexattr.
	 */
128
	if (flags & XATTR_REPLACE) {
129 130 131
		ASSERT(mutex_is_locked(&inode->i_mutex));
		di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(inode),
					name, name_len, 0);
132
		if (!di)
133
			ret = -ENODATA;
134 135 136
		else if (IS_ERR(di))
			ret = PTR_ERR(di);
		if (ret)
J
Josef Bacik 已提交
137
			goto out;
138
		btrfs_release_path(path);
139 140
		di = NULL;
	}
141

142 143 144
	ret = btrfs_insert_xattr_item(trans, root, path, btrfs_ino(inode),
				      name, name_len, value, size);
	if (ret == -EOVERFLOW) {
145
		/*
146 147 148
		 * We have an existing item in a leaf, split_leaf couldn't
		 * expand it. That item might have or not a dir_item that
		 * matches our target xattr, so lets check.
149
		 */
150 151 152 153 154
		ret = 0;
		btrfs_assert_tree_locked(path->nodes[0]);
		di = btrfs_match_dir_item_name(root, path, name, name_len);
		if (!di && !(flags & XATTR_REPLACE)) {
			ret = -ENOSPC;
155 156
			goto out;
		}
157 158 159 160 161 162
	} else if (ret == -EEXIST) {
		ret = 0;
		di = btrfs_match_dir_item_name(root, path, name, name_len);
		ASSERT(di); /* logic error */
	} else if (ret) {
		goto out;
163
	}
J
Josef Bacik 已提交
164

165
	if (di && (flags & XATTR_CREATE)) {
166
		ret = -EEXIST;
167 168
		goto out;
	}
169

170
	if (di) {
171
		/*
172 173 174 175 176
		 * We're doing a replace, and it must be atomic, that is, at
		 * any point in time we have either the old or the new xattr
		 * value in the tree. We don't want readers (getxattr and
		 * listxattrs) to miss a value, this is specially important
		 * for ACLs.
177
		 */
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
		const int slot = path->slots[0];
		struct extent_buffer *leaf = path->nodes[0];
		const u16 old_data_len = btrfs_dir_data_len(leaf, di);
		const u32 item_size = btrfs_item_size_nr(leaf, slot);
		const u32 data_size = sizeof(*di) + name_len + size;
		struct btrfs_item *item;
		unsigned long data_ptr;
		char *ptr;

		if (size > old_data_len) {
			if (btrfs_leaf_free_space(root, leaf) <
			    (size - old_data_len)) {
				ret = -ENOSPC;
				goto out;
			}
193
		}
J
Josef Bacik 已提交
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208
		if (old_data_len + name_len + sizeof(*di) == item_size) {
			/* No other xattrs packed in the same leaf item. */
			if (size > old_data_len)
				btrfs_extend_item(root, path,
						  size - old_data_len);
			else if (size < old_data_len)
				btrfs_truncate_item(root, path, data_size, 1);
		} else {
			/* There are other xattrs packed in the same item. */
			ret = btrfs_delete_one_dir_name(trans, root, path, di);
			if (ret)
				goto out;
			btrfs_extend_item(root, path, data_size);
		}
209

210 211 212 213 214 215 216 217 218
		item = btrfs_item_nr(slot);
		ptr = btrfs_item_ptr(leaf, slot, char);
		ptr += btrfs_item_size(leaf, item) - data_size;
		di = (struct btrfs_dir_item *)ptr;
		btrfs_set_dir_data_len(leaf, di, size);
		data_ptr = ((unsigned long)(di + 1)) + name_len;
		write_extent_buffer(leaf, value, data_ptr, size);
		btrfs_mark_buffer_dirty(leaf);
	} else {
219
		/*
220 221 222
		 * Insert, and we had space for the xattr, so path->slots[0] is
		 * where our xattr dir_item is and btrfs_insert_xattr_item()
		 * filled it.
223
		 */
J
Josef Bacik 已提交
224
	}
225 226 227 228 229
out:
	btrfs_free_path(path);
	return ret;
}

230 231 232
/*
 * @value: "" makes the attribute to empty, NULL removes it
 */
233 234 235 236 237 238 239 240 241 242
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);

243 244 245
	trans = btrfs_start_transaction(root, 2);
	if (IS_ERR(trans))
		return PTR_ERR(trans);
J
Josef Bacik 已提交
246

247 248 249 250
	ret = do_setxattr(trans, inode, name, value, size, flags);
	if (ret)
		goto out;

251
	inode_inc_iversion(inode);
252
	inode->i_ctime = CURRENT_TIME;
253
	set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
254 255 256
	ret = btrfs_update_inode(trans, root, inode);
	BUG_ON(ret);
out:
257
	btrfs_end_transaction(trans, root);
J
Josef Bacik 已提交
258 259 260 261 262 263
	return ret;
}

ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
{
	struct btrfs_key key, found_key;
264
	struct inode *inode = d_inode(dentry);
J
Josef Bacik 已提交
265 266 267 268
	struct btrfs_root *root = BTRFS_I(inode)->root;
	struct btrfs_path *path;
	struct extent_buffer *leaf;
	struct btrfs_dir_item *di;
269
	int ret = 0, slot;
C
Christoph Hellwig 已提交
270
	size_t total_size = 0, size_left = size;
J
Josef Bacik 已提交
271
	unsigned long name_ptr;
C
Christoph Hellwig 已提交
272
	size_t name_len;
J
Josef Bacik 已提交
273 274 275 276 277 278

	/*
	 * 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
	 */
L
Li Zefan 已提交
279
	key.objectid = btrfs_ino(inode);
280
	key.type = BTRFS_XATTR_ITEM_KEY;
J
Josef Bacik 已提交
281 282 283 284 285
	key.offset = 0;

	path = btrfs_alloc_path();
	if (!path)
		return -ENOMEM;
286
	path->reada = READA_FORWARD;
J
Josef Bacik 已提交
287 288 289 290 291

	/* search for our xattrs */
	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
	if (ret < 0)
		goto err;
292

J
Josef Bacik 已提交
293 294 295 296 297
	while (1) {
		leaf = path->nodes[0];
		slot = path->slots[0];

		/* this is where we start walking through the path */
298
		if (slot >= btrfs_header_nritems(leaf)) {
J
Josef Bacik 已提交
299 300 301 302
			/*
			 * if we've reached the last slot in this leaf we need
			 * to go to the next leaf and reset everything
			 */
303 304 305 306 307 308
			ret = btrfs_next_leaf(root, path);
			if (ret < 0)
				goto err;
			else if (ret > 0)
				break;
			continue;
J
Josef Bacik 已提交
309 310 311 312 313 314 315
		}

		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;
316
		if (found_key.type > BTRFS_XATTR_ITEM_KEY)
J
Josef Bacik 已提交
317
			break;
318 319
		if (found_key.type < BTRFS_XATTR_ITEM_KEY)
			goto next;
J
Josef Bacik 已提交
320 321

		di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
322
		if (verify_dir_item(root, leaf, di))
L
Liu Bo 已提交
323
			goto next;
J
Josef Bacik 已提交
324

C
Christoph Hellwig 已提交
325 326
		name_len = btrfs_dir_name_len(leaf, di);
		total_size += name_len + 1;
J
Josef Bacik 已提交
327 328 329

		/* we are just looking for how big our buffer needs to be */
		if (!size)
330
			goto next;
J
Josef Bacik 已提交
331

C
Christoph Hellwig 已提交
332
		if (!buffer || (name_len + 1) > size_left) {
J
Josef Bacik 已提交
333
			ret = -ERANGE;
334
			goto err;
J
Josef Bacik 已提交
335 336
		}

C
Christoph Hellwig 已提交
337 338 339 340 341 342
		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;
343 344
next:
		path->slots[0]++;
J
Josef Bacik 已提交
345 346 347 348 349 350 351 352 353 354
	}
	ret = total_size;

err:
	btrfs_free_path(path);

	return ret;
}

/*
355 356 357
 * List of handlers for synthetic system.* attributes.  All real ondisk
 * attributes are handled directly.
 */
358
const struct xattr_handler *btrfs_xattr_handlers[] = {
C
Chris Mason 已提交
359
#ifdef CONFIG_BTRFS_FS_POSIX_ACL
360 361
	&posix_acl_access_xattr_handler,
	&posix_acl_default_xattr_handler,
362 363 364 365 366 367 368
#endif
	NULL,
};

/*
 * Check if the attribute is in a supported namespace.
 *
369
 * This is applied after the check for the synthetic attributes in the system
370
 * namespace.
J
Josef Bacik 已提交
371
 */
372
static int btrfs_is_valid_xattr(const char *name)
373
{
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
	int len = strlen(name);
	int prefixlen = 0;

	if (!strncmp(name, XATTR_SECURITY_PREFIX,
			XATTR_SECURITY_PREFIX_LEN))
		prefixlen = XATTR_SECURITY_PREFIX_LEN;
	else if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
		prefixlen = XATTR_SYSTEM_PREFIX_LEN;
	else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
		prefixlen = XATTR_TRUSTED_PREFIX_LEN;
	else if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
		prefixlen = XATTR_USER_PREFIX_LEN;
	else if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN))
		prefixlen = XATTR_BTRFS_PREFIX_LEN;
	else
		return -EOPNOTSUPP;

	/*
	 * The name cannot consist of just prefix
	 */
	if (len <= prefixlen)
		return -EINVAL;

	return 0;
398 399
}

400 401 402
ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
		       void *buffer, size_t size)
{
403 404
	int ret;

405 406 407 408 409 410 411
	/*
	 * 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 已提交
412

413 414 415
	ret = btrfs_is_valid_xattr(name);
	if (ret)
		return ret;
416
	return __btrfs_getxattr(d_inode(dentry), name, buffer, size);
417
}
J
Josef Bacik 已提交
418

419 420 421
int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
		   size_t size, int flags)
{
422
	struct btrfs_root *root = BTRFS_I(d_inode(dentry))->root;
423
	int ret;
L
Li Zefan 已提交
424 425 426 427 428 429 430 431

	/*
	 * The permission on security.* and system.* is not checked
	 * in permission().
	 */
	if (btrfs_root_readonly(root))
		return -EROFS;

432 433 434 435 436 437 438
	/*
	 * 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 已提交
439

440 441 442
	ret = btrfs_is_valid_xattr(name);
	if (ret)
		return ret;
J
Josef Bacik 已提交
443

444
	if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN))
445
		return btrfs_set_prop(d_inode(dentry), name,
446 447
				      value, size, flags);

448 449
	if (size == 0)
		value = "";  /* empty EA, do not remove */
450

451
	return __btrfs_setxattr(NULL, d_inode(dentry), name, value, size,
452
				flags);
453 454 455 456
}

int btrfs_removexattr(struct dentry *dentry, const char *name)
{
457
	struct btrfs_root *root = BTRFS_I(d_inode(dentry))->root;
458
	int ret;
L
Li Zefan 已提交
459 460 461 462 463 464 465 466

	/*
	 * The permission on security.* and system.* is not checked
	 * in permission().
	 */
	if (btrfs_root_readonly(root))
		return -EROFS;

467 468 469 470 471 472 473 474
	/*
	 * 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);

475 476 477
	ret = btrfs_is_valid_xattr(name);
	if (ret)
		return ret;
478

479
	if (!strncmp(name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN))
480
		return btrfs_set_prop(d_inode(dentry), name,
481 482
				      NULL, 0, XATTR_REPLACE);

483
	return __btrfs_setxattr(NULL, d_inode(dentry), name, NULL, 0,
484
				XATTR_REPLACE);
485
}
J
Jim Owens 已提交
486

487 488
static int btrfs_initxattrs(struct inode *inode,
			    const struct xattr *xattr_array, void *fs_info)
J
Jim Owens 已提交
489
{
490 491
	const struct xattr *xattr;
	struct btrfs_trans_handle *trans = fs_info;
J
Jim Owens 已提交
492
	char *name;
493
	int err = 0;
J
Jim Owens 已提交
494

495 496 497 498 499 500 501
	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
		name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
			       strlen(xattr->name) + 1, GFP_NOFS);
		if (!name) {
			err = -ENOMEM;
			break;
		}
J
Jim Owens 已提交
502
		strcpy(name, XATTR_SECURITY_PREFIX);
503 504 505
		strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
		err = __btrfs_setxattr(trans, inode, name,
				       xattr->value, xattr->value_len, 0);
J
Jim Owens 已提交
506
		kfree(name);
507 508
		if (err < 0)
			break;
J
Jim Owens 已提交
509 510 511
	}
	return err;
}
512 513 514 515 516 517 518 519

int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
			      struct inode *inode, struct inode *dir,
			      const struct qstr *qstr)
{
	return security_inode_init_security(inode, dir, qstr,
					    &btrfs_initxattrs, trans);
}