xattr.c 14.3 KB
Newer Older
M
Mike Marshall 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * (C) 2001 Clemson University and The University of Chicago
 *
 * See COPYING in top-level directory.
 */

/*
 *  Linux VFS extended attribute operations.
 */

#include "protocol.h"
12 13
#include "orangefs-kernel.h"
#include "orangefs-bufmap.h"
M
Mike Marshall 已提交
14 15 16 17
#include <linux/posix_acl_xattr.h>
#include <linux/xattr.h>


18 19
#define SYSTEM_ORANGEFS_KEY "system.pvfs2."
#define SYSTEM_ORANGEFS_KEY_LEN 13
M
Mike Marshall 已提交
20 21 22 23 24 25 26

/*
 * this function returns
 *   0 if the key corresponding to name is not meant to be printed as part
 *     of a listxattr.
 *   1 if the key corresponding to name is meant to be returned as part of
 *     a listxattr.
27
 * The ones that start SYSTEM_ORANGEFS_KEY are the ones to avoid printing.
M
Mike Marshall 已提交
28 29 30 31
 */
static int is_reserved_key(const char *key, size_t size)
{

32
	if (size < SYSTEM_ORANGEFS_KEY_LEN)
M
Mike Marshall 已提交
33 34
		return 1;

35
	return strncmp(key, SYSTEM_ORANGEFS_KEY, SYSTEM_ORANGEFS_KEY_LEN) ?  1 : 0;
M
Mike Marshall 已提交
36 37 38 39 40 41 42 43
}

static inline int convert_to_internal_xattr_flags(int setxattr_flags)
{
	int internal_flag = 0;

	if (setxattr_flags & XATTR_REPLACE) {
		/* Attribute must exist! */
44
		internal_flag = ORANGEFS_XATTR_REPLACE;
M
Mike Marshall 已提交
45 46
	} else if (setxattr_flags & XATTR_CREATE) {
		/* Attribute must not exist */
47
		internal_flag = ORANGEFS_XATTR_CREATE;
M
Mike Marshall 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61
	}
	return internal_flag;
}


/*
 * Tries to get a specified key's attributes of a given
 * file into a user-specified buffer. Note that the getxattr
 * interface allows for the users to probe the size of an
 * extended attribute by passing in a value of 0 to size.
 * Thus our return value is always the size of the attribute
 * unless the key does not exist for the file and/or if
 * there were errors in fetching the attribute value.
 */
62
ssize_t orangefs_inode_getxattr(struct inode *inode, const char *prefix,
M
Mike Marshall 已提交
63 64
		const char *name, void *buffer, size_t size)
{
65 66
	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
	struct orangefs_kernel_op_s *new_op = NULL;
M
Mike Marshall 已提交
67 68 69 70 71 72 73 74 75 76
	ssize_t ret = -ENOMEM;
	ssize_t length = 0;
	int fsuid;
	int fsgid;

	gossip_debug(GOSSIP_XATTR_DEBUG,
		     "%s: prefix %s name %s, buffer_size %zd\n",
		     __func__, prefix, name, size);

	if (name == NULL || (size > 0 && buffer == NULL)) {
77
		gossip_err("orangefs_inode_getxattr: bogus NULL pointers\n");
M
Mike Marshall 已提交
78 79
		return -EINVAL;
	}
80
	if ((strlen(name) + strlen(prefix)) >= ORANGEFS_MAX_XATTR_NAMELEN) {
81
		gossip_err("Invalid key length (%d)\n",
M
Mike Marshall 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
			   (int)(strlen(name) + strlen(prefix)));
		return -EINVAL;
	}

	fsuid = from_kuid(current_user_ns(), current_fsuid());
	fsgid = from_kgid(current_user_ns(), current_fsgid());

	gossip_debug(GOSSIP_XATTR_DEBUG,
		     "getxattr on inode %pU, name %s "
		     "(uid %o, gid %o)\n",
		     get_khandle_from_ino(inode),
		     name,
		     fsuid,
		     fsgid);

97
	down_read(&orangefs_inode->xattr_sem);
M
Mike Marshall 已提交
98

99
	new_op = op_alloc(ORANGEFS_VFS_OP_GETXATTR);
M
Mike Marshall 已提交
100 101 102
	if (!new_op)
		goto out_unlock;

103
	new_op->upcall.req.getxattr.refn = orangefs_inode->refn;
M
Mike Marshall 已提交
104
	ret = snprintf((char *)new_op->upcall.req.getxattr.key,
105
		       ORANGEFS_MAX_XATTR_NAMELEN, "%s%s", prefix, name);
M
Mike Marshall 已提交
106 107 108 109 110 111 112 113

	/*
	 * NOTE: Although keys are meant to be NULL terminated textual
	 * strings, I am going to explicitly pass the length just in case
	 * we change this later on...
	 */
	new_op->upcall.req.getxattr.key_sz = ret + 1;

114
	ret = service_operation(new_op, "orangefs_inode_getxattr",
M
Mike Marshall 已提交
115 116 117 118 119
				get_interruptible_flag(inode));
	if (ret != 0) {
		if (ret == -ENOENT) {
			ret = -ENODATA;
			gossip_debug(GOSSIP_XATTR_DEBUG,
120
				     "orangefs_inode_getxattr: inode %pU key %s"
M
Mike Marshall 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
				     " does not exist!\n",
				     get_khandle_from_ino(inode),
				     (char *)new_op->upcall.req.getxattr.key);
		}
		goto out_release_op;
	}

	/*
	 * Length returned includes null terminator.
	 */
	length = new_op->downcall.resp.getxattr.val_sz;

	/*
	 * Just return the length of the queried attribute.
	 */
	if (size == 0) {
		ret = length;
		goto out_release_op;
	}

	/*
	 * Check to see if key length is > provided buffer size.
	 */
	if (length > size) {
		ret = -ERANGE;
		goto out_release_op;
	}

	memset(buffer, 0, size);
	memcpy(buffer, new_op->downcall.resp.getxattr.val, length);
	gossip_debug(GOSSIP_XATTR_DEBUG,
152
	     "orangefs_inode_getxattr: inode %pU "
M
Mike Marshall 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165
	     "key %s key_sz %d, val_len %d\n",
	     get_khandle_from_ino(inode),
	     (char *)new_op->
		upcall.req.getxattr.key,
		     (int)new_op->
		upcall.req.getxattr.key_sz,
	     (int)ret);

	ret = length;

out_release_op:
	op_release(new_op);
out_unlock:
166
	up_read(&orangefs_inode->xattr_sem);
M
Mike Marshall 已提交
167 168 169
	return ret;
}

170
static int orangefs_inode_removexattr(struct inode *inode,
M
Mike Marshall 已提交
171 172 173 174
			    const char *prefix,
			    const char *name,
			    int flags)
{
175 176
	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
	struct orangefs_kernel_op_s *new_op = NULL;
M
Mike Marshall 已提交
177 178
	int ret = -ENOMEM;

179 180
	down_write(&orangefs_inode->xattr_sem);
	new_op = op_alloc(ORANGEFS_VFS_OP_REMOVEXATTR);
M
Mike Marshall 已提交
181 182 183
	if (!new_op)
		goto out_unlock;

184
	new_op->upcall.req.removexattr.refn = orangefs_inode->refn;
M
Mike Marshall 已提交
185 186 187 188 189 190
	/*
	 * NOTE: Although keys are meant to be NULL terminated
	 * textual strings, I am going to explicitly pass the
	 * length just in case we change this later on...
	 */
	ret = snprintf((char *)new_op->upcall.req.removexattr.key,
191
		       ORANGEFS_MAX_XATTR_NAMELEN,
M
Mike Marshall 已提交
192 193 194 195 196 197
		       "%s%s",
		       (prefix ? prefix : ""),
		       name);
	new_op->upcall.req.removexattr.key_sz = ret + 1;

	gossip_debug(GOSSIP_XATTR_DEBUG,
198
		     "orangefs_inode_removexattr: key %s, key_sz %d\n",
M
Mike Marshall 已提交
199 200 201 202
		     (char *)new_op->upcall.req.removexattr.key,
		     (int)new_op->upcall.req.removexattr.key_sz);

	ret = service_operation(new_op,
203
				"orangefs_inode_removexattr",
M
Mike Marshall 已提交
204 205 206 207 208 209 210 211 212 213 214 215
				get_interruptible_flag(inode));
	if (ret == -ENOENT) {
		/*
		 * Request to replace a non-existent attribute is an error.
		 */
		if (flags & XATTR_REPLACE)
			ret = -ENODATA;
		else
			ret = 0;
	}

	gossip_debug(GOSSIP_XATTR_DEBUG,
216
		     "orangefs_inode_removexattr: returning %d\n", ret);
M
Mike Marshall 已提交
217 218 219

	op_release(new_op);
out_unlock:
220
	up_write(&orangefs_inode->xattr_sem);
M
Mike Marshall 已提交
221 222 223 224 225 226 227 228 229
	return ret;
}

/*
 * Tries to set an attribute for a given key on a file.
 *
 * Returns a -ve number on error and 0 on success.  Key is text, but value
 * can be binary!
 */
230
int orangefs_inode_setxattr(struct inode *inode, const char *prefix,
M
Mike Marshall 已提交
231 232
		const char *name, const void *value, size_t size, int flags)
{
233 234
	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
	struct orangefs_kernel_op_s *new_op;
M
Mike Marshall 已提交
235 236 237 238 239 240 241 242
	int internal_flag = 0;
	int ret = -ENOMEM;

	gossip_debug(GOSSIP_XATTR_DEBUG,
		     "%s: prefix %s, name %s, buffer_size %zd\n",
		     __func__, prefix, name, size);

	if (size < 0 ||
243
	    size >= ORANGEFS_MAX_XATTR_VALUELEN ||
M
Mike Marshall 已提交
244
	    flags < 0) {
245
		gossip_err("orangefs_inode_setxattr: bogus values of size(%d), flags(%d)\n",
M
Mike Marshall 已提交
246 247 248 249 250 251 252
			   (int)size,
			   flags);
		return -EINVAL;
	}

	if (name == NULL ||
	    (size > 0 && value == NULL)) {
253
		gossip_err("orangefs_inode_setxattr: bogus NULL pointers!\n");
M
Mike Marshall 已提交
254 255 256 257 258 259
		return -EINVAL;
	}

	internal_flag = convert_to_internal_xattr_flags(flags);

	if (prefix) {
260
		if (strlen(name) + strlen(prefix) >= ORANGEFS_MAX_XATTR_NAMELEN) {
M
Mike Marshall 已提交
261
			gossip_err
262
			    ("orangefs_inode_setxattr: bogus key size (%d)\n",
M
Mike Marshall 已提交
263 264 265 266
			     (int)(strlen(name) + strlen(prefix)));
			return -EINVAL;
		}
	} else {
267
		if (strlen(name) >= ORANGEFS_MAX_XATTR_NAMELEN) {
M
Mike Marshall 已提交
268
			gossip_err
269
			    ("orangefs_inode_setxattr: bogus key size (%d)\n",
M
Mike Marshall 已提交
270 271 272 273 274 275 276 277 278 279 280
			     (int)(strlen(name)));
			return -EINVAL;
		}
	}

	/* This is equivalent to a removexattr */
	if (size == 0 && value == NULL) {
		gossip_debug(GOSSIP_XATTR_DEBUG,
			     "removing xattr (%s%s)\n",
			     prefix,
			     name);
281
		return orangefs_inode_removexattr(inode, prefix, name, flags);
M
Mike Marshall 已提交
282 283 284 285 286 287 288
	}

	gossip_debug(GOSSIP_XATTR_DEBUG,
		     "setxattr on inode %pU, name %s\n",
		     get_khandle_from_ino(inode),
		     name);

289 290
	down_write(&orangefs_inode->xattr_sem);
	new_op = op_alloc(ORANGEFS_VFS_OP_SETXATTR);
M
Mike Marshall 已提交
291 292 293 294
	if (!new_op)
		goto out_unlock;


295
	new_op->upcall.req.setxattr.refn = orangefs_inode->refn;
M
Mike Marshall 已提交
296 297 298 299 300 301 302
	new_op->upcall.req.setxattr.flags = internal_flag;
	/*
	 * NOTE: Although keys are meant to be NULL terminated textual
	 * strings, I am going to explicitly pass the length just in
	 * case we change this later on...
	 */
	ret = snprintf((char *)new_op->upcall.req.setxattr.keyval.key,
303
		       ORANGEFS_MAX_XATTR_NAMELEN,
M
Mike Marshall 已提交
304 305 306 307 308 309 310
		       "%s%s",
		       prefix, name);
	new_op->upcall.req.setxattr.keyval.key_sz = ret + 1;
	memcpy(new_op->upcall.req.setxattr.keyval.val, value, size);
	new_op->upcall.req.setxattr.keyval.val_sz = size;

	gossip_debug(GOSSIP_XATTR_DEBUG,
311
		     "orangefs_inode_setxattr: key %s, key_sz %d "
M
Mike Marshall 已提交
312 313 314 315 316 317
		     " value size %zd\n",
		     (char *)new_op->upcall.req.setxattr.keyval.key,
		     (int)new_op->upcall.req.setxattr.keyval.key_sz,
		     size);

	ret = service_operation(new_op,
318
				"orangefs_inode_setxattr",
M
Mike Marshall 已提交
319 320 321
				get_interruptible_flag(inode));

	gossip_debug(GOSSIP_XATTR_DEBUG,
322
		     "orangefs_inode_setxattr: returning %d\n",
M
Mike Marshall 已提交
323 324 325 326 327
		     ret);

	/* when request is serviced properly, free req op struct */
	op_release(new_op);
out_unlock:
328
	up_write(&orangefs_inode->xattr_sem);
M
Mike Marshall 已提交
329 330 331 332 333 334 335 336 337 338
	return ret;
}

/*
 * Tries to get a specified object's keys into a user-specified buffer of a
 * given size.  Note that like the previous instances of xattr routines, this
 * also allows you to pass in a NULL pointer and 0 size to probe the size for
 * subsequent memory allocations. Thus our return value is always the size of
 * all the keys unless there were errors in fetching the keys!
 */
339
ssize_t orangefs_listxattr(struct dentry *dentry, char *buffer, size_t size)
M
Mike Marshall 已提交
340 341
{
	struct inode *inode = dentry->d_inode;
342 343 344
	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
	struct orangefs_kernel_op_s *new_op;
	__u64 token = ORANGEFS_ITERATE_START;
M
Mike Marshall 已提交
345 346 347 348 349
	ssize_t ret = -ENOMEM;
	ssize_t total = 0;
	int count_keys = 0;
	int key_size;
	int i = 0;
350
	int returned_count = 0;
M
Mike Marshall 已提交
351 352 353 354 355 356 357 358 359 360

	if (size > 0 && buffer == NULL) {
		gossip_err("%s: bogus NULL pointers\n", __func__);
		return -EINVAL;
	}
	if (size < 0) {
		gossip_err("Invalid size (%d)\n", (int)size);
		return -EINVAL;
	}

361 362
	down_read(&orangefs_inode->xattr_sem);
	new_op = op_alloc(ORANGEFS_VFS_OP_LISTXATTR);
M
Mike Marshall 已提交
363 364 365 366 367 368 369 370
	if (!new_op)
		goto out_unlock;

	if (buffer && size > 0)
		memset(buffer, 0, size);

try_again:
	key_size = 0;
371
	new_op->upcall.req.listxattr.refn = orangefs_inode->refn;
M
Mike Marshall 已提交
372 373
	new_op->upcall.req.listxattr.token = token;
	new_op->upcall.req.listxattr.requested_count =
374
	    (size == 0) ? 0 : ORANGEFS_MAX_XATTR_LISTLEN;
M
Mike Marshall 已提交
375 376 377 378 379 380 381 382 383 384 385 386
	ret = service_operation(new_op, __func__,
				get_interruptible_flag(inode));
	if (ret != 0)
		goto done;

	if (size == 0) {
		/*
		 * This is a bit of a big upper limit, but I did not want to
		 * spend too much time getting this correct, since users end
		 * up allocating memory rather than us...
		 */
		total = new_op->downcall.resp.listxattr.returned_count *
387
			ORANGEFS_MAX_XATTR_NAMELEN;
M
Mike Marshall 已提交
388 389 390
		goto done;
	}

391 392 393 394 395 396 397 398 399
	returned_count = new_op->downcall.resp.listxattr.returned_count;
	if (returned_count < 0 ||
	    returned_count >= ORANGEFS_MAX_XATTR_LISTLEN) {
		gossip_err("%s: impossible value for returned_count:%d:\n",
		__func__,
		returned_count);
		goto done;
	}

M
Mike Marshall 已提交
400 401 402
	/*
	 * Check to see how much can be fit in the buffer. Fit only whole keys.
	 */
403
	for (i = 0; i < returned_count; i++) {
M
Mike Marshall 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
		if (total + new_op->downcall.resp.listxattr.lengths[i] > size)
			goto done;

		/*
		 * Since many dumb programs try to setxattr() on our reserved
		 * xattrs this is a feeble attempt at defeating those by not
		 * listing them in the output of listxattr.. sigh
		 */
		if (is_reserved_key(new_op->downcall.resp.listxattr.key +
				    key_size,
				    new_op->downcall.resp.
					listxattr.lengths[i])) {
			gossip_debug(GOSSIP_XATTR_DEBUG, "Copying key %d -> %s\n",
					i, new_op->downcall.resp.listxattr.key +
						key_size);
			memcpy(buffer + total,
				new_op->downcall.resp.listxattr.key + key_size,
				new_op->downcall.resp.listxattr.lengths[i]);
			total += new_op->downcall.resp.listxattr.lengths[i];
			count_keys++;
		} else {
			gossip_debug(GOSSIP_XATTR_DEBUG, "[RESERVED] key %d -> %s\n",
					i, new_op->downcall.resp.listxattr.key +
						key_size);
		}
		key_size += new_op->downcall.resp.listxattr.lengths[i];
	}

	/*
	 * Since the buffer was large enough, we might have to continue
	 * fetching more keys!
	 */
	token = new_op->downcall.resp.listxattr.token;
437
	if (token != ORANGEFS_ITERATE_END)
M
Mike Marshall 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450
		goto try_again;

done:
	gossip_debug(GOSSIP_XATTR_DEBUG, "%s: returning %d"
		     " [size of buffer %ld] (filled in %d keys)\n",
		     __func__,
		     ret ? (int)ret : (int)total,
		     (long)size,
		     count_keys);
	op_release(new_op);
	if (ret == 0)
		ret = total;
out_unlock:
451
	up_read(&orangefs_inode->xattr_sem);
M
Mike Marshall 已提交
452 453 454
	return ret;
}

455 456 457 458 459 460
static int orangefs_xattr_set_default(const struct xattr_handler *handler,
				      struct dentry *dentry,
				      const char *name,
				      const void *buffer,
				      size_t size,
				      int flags)
M
Mike Marshall 已提交
461
{
462 463
	return orangefs_inode_setxattr(dentry->d_inode,
				    ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
M
Mike Marshall 已提交
464 465 466 467 468 469
				    name,
				    buffer,
				    size,
				    flags);
}

470 471 472 473 474
static int orangefs_xattr_get_default(const struct xattr_handler *handler,
				      struct dentry *dentry,
				      const char *name,
				      void *buffer,
				      size_t size)
M
Mike Marshall 已提交
475
{
476 477
	return orangefs_inode_getxattr(dentry->d_inode,
				    ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
M
Mike Marshall 已提交
478 479 480 481 482 483
				    name,
				    buffer,
				    size);

}

484 485 486 487 488 489
static int orangefs_xattr_set_trusted(const struct xattr_handler *handler,
				     struct dentry *dentry,
				     const char *name,
				     const void *buffer,
				     size_t size,
				     int flags)
M
Mike Marshall 已提交
490
{
491 492
	return orangefs_inode_setxattr(dentry->d_inode,
				    ORANGEFS_XATTR_NAME_TRUSTED_PREFIX,
M
Mike Marshall 已提交
493 494 495 496 497 498
				    name,
				    buffer,
				    size,
				    flags);
}

499 500 501 502 503
static int orangefs_xattr_get_trusted(const struct xattr_handler *handler,
				      struct dentry *dentry,
				      const char *name,
				      void *buffer,
				      size_t size)
M
Mike Marshall 已提交
504
{
505 506
	return orangefs_inode_getxattr(dentry->d_inode,
				    ORANGEFS_XATTR_NAME_TRUSTED_PREFIX,
M
Mike Marshall 已提交
507 508 509 510 511
				    name,
				    buffer,
				    size);
}

512 513 514 515
static struct xattr_handler orangefs_xattr_trusted_handler = {
	.prefix = ORANGEFS_XATTR_NAME_TRUSTED_PREFIX,
	.get = orangefs_xattr_get_trusted,
	.set = orangefs_xattr_set_trusted,
M
Mike Marshall 已提交
516 517
};

518
static struct xattr_handler orangefs_xattr_default_handler = {
M
Mike Marshall 已提交
519 520 521 522 523
	/*
	 * NOTE: this is set to be the empty string.
	 * so that all un-prefixed xattrs keys get caught
	 * here!
	 */
524 525 526
	.prefix = ORANGEFS_XATTR_NAME_DEFAULT_PREFIX,
	.get = orangefs_xattr_get_default,
	.set = orangefs_xattr_set_default,
M
Mike Marshall 已提交
527 528
};

529
const struct xattr_handler *orangefs_xattr_handlers[] = {
M
Mike Marshall 已提交
530 531
	&posix_acl_access_xattr_handler,
	&posix_acl_default_xattr_handler,
532 533
	&orangefs_xattr_trusted_handler,
	&orangefs_xattr_default_handler,
M
Mike Marshall 已提交
534 535
	NULL
};