xattr.c 26.7 KB
Newer Older
1
#include <linux/ceph/ceph_debug.h>
2
#include <linux/ceph/pagelist.h>
3

S
Sage Weil 已提交
4
#include "super.h"
5 6 7
#include "mds_client.h"

#include <linux/ceph/decode.h>
S
Sage Weil 已提交
8 9

#include <linux/xattr.h>
10
#include <linux/posix_acl_xattr.h>
11
#include <linux/slab.h>
S
Sage Weil 已提交
12

13 14 15
#define XATTR_CEPH_PREFIX "ceph."
#define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)

16 17 18
static int __remove_xattr(struct ceph_inode_info *ci,
			  struct ceph_inode_xattr *xattr);

19 20
const struct xattr_handler ceph_other_xattr_handler;

G
Guangliang Zhao 已提交
21 22 23 24 25 26
/*
 * List of handlers for synthetic system.* attributes. Other
 * attributes are handled directly.
 */
const struct xattr_handler *ceph_xattr_handlers[] = {
#ifdef CONFIG_CEPH_FS_POSIX_ACL
27 28
	&posix_acl_access_xattr_handler,
	&posix_acl_default_xattr_handler,
G
Guangliang Zhao 已提交
29
#endif
30
	&ceph_other_xattr_handler,
G
Guangliang Zhao 已提交
31 32 33
	NULL,
};

S
Sage Weil 已提交
34 35
static bool ceph_is_valid_xattr(const char *name)
{
36
	return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
37
	       !strncmp(name, XATTR_SECURITY_PREFIX,
S
Sage Weil 已提交
38 39 40 41 42 43 44 45 46
			XATTR_SECURITY_PREFIX_LEN) ||
	       !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
	       !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
}

/*
 * These define virtual xattrs exposing the recursive directory
 * statistics and layout metadata.
 */
47
struct ceph_vxattr {
S
Sage Weil 已提交
48
	char *name;
49
	size_t name_size;	/* strlen(name) + 1 (for '\0') */
S
Sage Weil 已提交
50 51
	size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
			      size_t size);
S
Sage Weil 已提交
52
	bool readonly, hidden;
S
Sage Weil 已提交
53
	bool (*exists_cb)(struct ceph_inode_info *ci);
S
Sage Weil 已提交
54 55
};

56 57 58 59 60 61 62 63 64 65 66 67 68 69
/* layouts */

static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
{
	size_t s;
	char *p = (char *)&ci->i_layout;

	for (s = 0; s < sizeof(ci->i_layout); s++, p++)
		if (*p)
			return true;
	return false;
}

static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
70
				   size_t size)
71 72 73 74
{
	int ret;
	struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
	struct ceph_osd_client *osdc = &fsc->client->osdc;
75
	s64 pool = ci->i_layout.pool_id;
76
	const char *pool_name;
77
	char buf[128];
78 79

	dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
80
	down_read(&osdc->lock);
81
	pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
82 83 84
	if (pool_name) {
		size_t len = strlen(pool_name);
		ret = snprintf(buf, sizeof(buf),
85 86 87
		"stripe_unit=%u stripe_count=%u object_size=%u pool=",
		ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
	        ci->i_layout.object_size);
88 89 90 91 92 93 94 95 96 97 98
		if (!size) {
			ret += len;
		} else if (ret + len > size) {
			ret = -ERANGE;
		} else {
			memcpy(val, buf, ret);
			memcpy(val + ret, pool_name, len);
			ret += len;
		}
	} else {
		ret = snprintf(buf, sizeof(buf),
99 100 101
		"stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
		ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
	        ci->i_layout.object_size, (unsigned long long)pool);
102 103 104 105 106 107 108
		if (size) {
			if (ret <= size)
				memcpy(val, buf, ret);
			else
				ret = -ERANGE;
		}
	}
109
	up_read(&osdc->lock);
110 111 112
	return ret;
}

113 114 115
static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
					       char *val, size_t size)
{
116
	return snprintf(val, size, "%u", ci->i_layout.stripe_unit);
117 118 119 120 121
}

static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
						char *val, size_t size)
{
122
	return snprintf(val, size, "%u", ci->i_layout.stripe_count);
123 124 125 126 127
}

static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
					       char *val, size_t size)
{
128
	return snprintf(val, size, "%u", ci->i_layout.object_size);
129 130 131 132 133 134 135 136
}

static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
					char *val, size_t size)
{
	int ret;
	struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
	struct ceph_osd_client *osdc = &fsc->client->osdc;
137
	s64 pool = ci->i_layout.pool_id;
138 139
	const char *pool_name;

140
	down_read(&osdc->lock);
141 142 143 144 145
	pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
	if (pool_name)
		ret = snprintf(val, size, "%s", pool_name);
	else
		ret = snprintf(val, size, "%lld", (unsigned long long)pool);
146
	up_read(&osdc->lock);
147 148 149
	return ret;
}

S
Sage Weil 已提交
150 151
/* directories */

152
static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
S
Sage Weil 已提交
153 154 155 156 157
					size_t size)
{
	return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
}

158
static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
S
Sage Weil 已提交
159 160 161 162 163
				      size_t size)
{
	return snprintf(val, size, "%lld", ci->i_files);
}

164
static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
S
Sage Weil 已提交
165 166 167 168 169
					size_t size)
{
	return snprintf(val, size, "%lld", ci->i_subdirs);
}

170
static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
S
Sage Weil 已提交
171 172 173 174 175
					 size_t size)
{
	return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
}

176
static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
S
Sage Weil 已提交
177 178 179 180 181
				       size_t size)
{
	return snprintf(val, size, "%lld", ci->i_rfiles);
}

182
static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
S
Sage Weil 已提交
183 184 185 186 187
					 size_t size)
{
	return snprintf(val, size, "%lld", ci->i_rsubdirs);
}

188
static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
S
Sage Weil 已提交
189 190 191 192 193
				       size_t size)
{
	return snprintf(val, size, "%lld", ci->i_rbytes);
}

194
static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
S
Sage Weil 已提交
195 196
				       size_t size)
{
197
	return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
S
Sage Weil 已提交
198 199 200
			(long)ci->i_rctime.tv_nsec);
}

201

202
#define CEPH_XATTR_NAME(_type, _name)	XATTR_CEPH_PREFIX #_type "." #_name
203 204
#define CEPH_XATTR_NAME2(_type, _name, _name2)	\
	XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
205

S
Sage Weil 已提交
206 207 208 209 210 211 212
#define XATTR_NAME_CEPH(_type, _name)					\
	{								\
		.name = CEPH_XATTR_NAME(_type, _name),			\
		.name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
		.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
		.readonly = true,				\
		.hidden = false,				\
S
Sage Weil 已提交
213
		.exists_cb = NULL,			\
S
Sage Weil 已提交
214
	}
215 216 217 218 219 220 221 222 223
#define XATTR_LAYOUT_FIELD(_type, _name, _field)			\
	{								\
		.name = CEPH_XATTR_NAME2(_type, _name, _field),	\
		.name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
		.getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
		.readonly = false,				\
		.hidden = true,			\
		.exists_cb = ceph_vxattrcb_layout_exists,	\
	}
224

225
static struct ceph_vxattr ceph_dir_vxattrs[] = {
S
Sage Weil 已提交
226 227 228 229 230
	{
		.name = "ceph.dir.layout",
		.name_size = sizeof("ceph.dir.layout"),
		.getxattr_cb = ceph_vxattrcb_layout,
		.readonly = false,
231
		.hidden = true,
S
Sage Weil 已提交
232 233
		.exists_cb = ceph_vxattrcb_layout_exists,
	},
234 235 236 237
	XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
	XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
	XATTR_LAYOUT_FIELD(dir, layout, object_size),
	XATTR_LAYOUT_FIELD(dir, layout, pool),
238 239 240 241 242 243 244 245
	XATTR_NAME_CEPH(dir, entries),
	XATTR_NAME_CEPH(dir, files),
	XATTR_NAME_CEPH(dir, subdirs),
	XATTR_NAME_CEPH(dir, rentries),
	XATTR_NAME_CEPH(dir, rfiles),
	XATTR_NAME_CEPH(dir, rsubdirs),
	XATTR_NAME_CEPH(dir, rbytes),
	XATTR_NAME_CEPH(dir, rctime),
246
	{ .name = NULL, 0 }	/* Required table terminator */
S
Sage Weil 已提交
247
};
248
static size_t ceph_dir_vxattrs_name_size;	/* total size of all names */
S
Sage Weil 已提交
249 250 251

/* files */

252
static struct ceph_vxattr ceph_file_vxattrs[] = {
253 254 255 256 257
	{
		.name = "ceph.file.layout",
		.name_size = sizeof("ceph.file.layout"),
		.getxattr_cb = ceph_vxattrcb_layout,
		.readonly = false,
258
		.hidden = true,
259 260
		.exists_cb = ceph_vxattrcb_layout_exists,
	},
261 262 263 264
	XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
	XATTR_LAYOUT_FIELD(file, layout, stripe_count),
	XATTR_LAYOUT_FIELD(file, layout, object_size),
	XATTR_LAYOUT_FIELD(file, layout, pool),
265
	{ .name = NULL, 0 }	/* Required table terminator */
S
Sage Weil 已提交
266
};
267
static size_t ceph_file_vxattrs_name_size;	/* total size of all names */
S
Sage Weil 已提交
268

269
static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
S
Sage Weil 已提交
270 271 272 273 274 275 276 277
{
	if (S_ISDIR(inode->i_mode))
		return ceph_dir_vxattrs;
	else if (S_ISREG(inode->i_mode))
		return ceph_file_vxattrs;
	return NULL;
}

278 279 280 281 282 283
static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
{
	if (vxattrs == ceph_dir_vxattrs)
		return ceph_dir_vxattrs_name_size;
	if (vxattrs == ceph_file_vxattrs)
		return ceph_file_vxattrs_name_size;
Y
Yan, Zheng 已提交
284
	BUG_ON(vxattrs);
285 286 287 288 289 290 291 292 293 294 295 296 297
	return 0;
}

/*
 * Compute the aggregate size (including terminating '\0') of all
 * virtual extended attribute names in the given vxattr table.
 */
static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
{
	struct ceph_vxattr *vxattr;
	size_t size = 0;

	for (vxattr = vxattrs; vxattr->name; vxattr++)
S
Sage Weil 已提交
298 299
		if (!vxattr->hidden)
			size += vxattr->name_size;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

	return size;
}

/* Routines called at initialization and exit time */

void __init ceph_xattr_init(void)
{
	ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
	ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
}

void ceph_xattr_exit(void)
{
	ceph_dir_vxattrs_name_size = 0;
	ceph_file_vxattrs_name_size = 0;
}

318
static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
S
Sage Weil 已提交
319 320
						const char *name)
{
321
	struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
322 323 324 325 326 327 328 329 330

	if (vxattr) {
		while (vxattr->name) {
			if (!strcmp(vxattr->name, name))
				return vxattr;
			vxattr++;
		}
	}

S
Sage Weil 已提交
331 332 333 334 335 336
	return NULL;
}

static int __set_xattr(struct ceph_inode_info *ci,
			   const char *name, int name_len,
			   const char *val, int val_len,
337
			   int flags, int update_xattr,
S
Sage Weil 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
			   struct ceph_inode_xattr **newxattr)
{
	struct rb_node **p;
	struct rb_node *parent = NULL;
	struct ceph_inode_xattr *xattr = NULL;
	int c;
	int new = 0;

	p = &ci->i_xattrs.index.rb_node;
	while (*p) {
		parent = *p;
		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
		c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
		if (c < 0)
			p = &(*p)->rb_left;
		else if (c > 0)
			p = &(*p)->rb_right;
		else {
			if (name_len == xattr->name_len)
				break;
			else if (name_len < xattr->name_len)
				p = &(*p)->rb_left;
			else
				p = &(*p)->rb_right;
		}
		xattr = NULL;
	}

366 367 368 369 370 371 372 373 374 375 376
	if (update_xattr) {
		int err = 0;
		if (xattr && (flags & XATTR_CREATE))
			err = -EEXIST;
		else if (!xattr && (flags & XATTR_REPLACE))
			err = -ENODATA;
		if (err) {
			kfree(name);
			kfree(val);
			return err;
		}
377 378 379 380 381 382
		if (update_xattr < 0) {
			if (xattr)
				__remove_xattr(ci, xattr);
			kfree(name);
			return 0;
		}
383 384
	}

S
Sage Weil 已提交
385 386 387 388 389
	if (!xattr) {
		new = 1;
		xattr = *newxattr;
		xattr->name = name;
		xattr->name_len = name_len;
390
		xattr->should_free_name = update_xattr;
S
Sage Weil 已提交
391 392 393 394 395 396 397 398 399

		ci->i_xattrs.count++;
		dout("__set_xattr count=%d\n", ci->i_xattrs.count);
	} else {
		kfree(*newxattr);
		*newxattr = NULL;
		if (xattr->should_free_val)
			kfree((void *)xattr->val);

400
		if (update_xattr) {
S
Sage Weil 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414
			kfree((void *)name);
			name = xattr->name;
		}
		ci->i_xattrs.names_size -= xattr->name_len;
		ci->i_xattrs.vals_size -= xattr->val_len;
	}
	ci->i_xattrs.names_size += name_len;
	ci->i_xattrs.vals_size += val_len;
	if (val)
		xattr->val = val;
	else
		xattr->val = "";

	xattr->val_len = val_len;
415 416
	xattr->dirty = update_xattr;
	xattr->should_free_val = (val && update_xattr);
S
Sage Weil 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

	if (new) {
		rb_link_node(&xattr->node, parent, p);
		rb_insert_color(&xattr->node, &ci->i_xattrs.index);
		dout("__set_xattr_val p=%p\n", p);
	}

	dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
	     ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);

	return 0;
}

static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
			   const char *name)
{
	struct rb_node **p;
	struct rb_node *parent = NULL;
	struct ceph_inode_xattr *xattr = NULL;
S
Sage Weil 已提交
436
	int name_len = strlen(name);
S
Sage Weil 已提交
437 438 439 440 441 442 443
	int c;

	p = &ci->i_xattrs.index.rb_node;
	while (*p) {
		parent = *p;
		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
		c = strncmp(name, xattr->name, xattr->name_len);
S
Sage Weil 已提交
444 445
		if (c == 0 && name_len > xattr->name_len)
			c = 1;
S
Sage Weil 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
		if (c < 0)
			p = &(*p)->rb_left;
		else if (c > 0)
			p = &(*p)->rb_right;
		else {
			dout("__get_xattr %s: found %.*s\n", name,
			     xattr->val_len, xattr->val);
			return xattr;
		}
	}

	dout("__get_xattr %s: not found\n", name);

	return NULL;
}

static void __free_xattr(struct ceph_inode_xattr *xattr)
{
	BUG_ON(!xattr);

	if (xattr->should_free_name)
		kfree((void *)xattr->name);
	if (xattr->should_free_val)
		kfree((void *)xattr->val);

	kfree(xattr);
}

static int __remove_xattr(struct ceph_inode_info *ci,
			  struct ceph_inode_xattr *xattr)
{
	if (!xattr)
Y
Yan, Zheng 已提交
478
		return -ENODATA;
S
Sage Weil 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546

	rb_erase(&xattr->node, &ci->i_xattrs.index);

	if (xattr->should_free_name)
		kfree((void *)xattr->name);
	if (xattr->should_free_val)
		kfree((void *)xattr->val);

	ci->i_xattrs.names_size -= xattr->name_len;
	ci->i_xattrs.vals_size -= xattr->val_len;
	ci->i_xattrs.count--;
	kfree(xattr);

	return 0;
}

static char *__copy_xattr_names(struct ceph_inode_info *ci,
				char *dest)
{
	struct rb_node *p;
	struct ceph_inode_xattr *xattr = NULL;

	p = rb_first(&ci->i_xattrs.index);
	dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);

	while (p) {
		xattr = rb_entry(p, struct ceph_inode_xattr, node);
		memcpy(dest, xattr->name, xattr->name_len);
		dest[xattr->name_len] = '\0';

		dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
		     xattr->name_len, ci->i_xattrs.names_size);

		dest += xattr->name_len + 1;
		p = rb_next(p);
	}

	return dest;
}

void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
{
	struct rb_node *p, *tmp;
	struct ceph_inode_xattr *xattr = NULL;

	p = rb_first(&ci->i_xattrs.index);

	dout("__ceph_destroy_xattrs p=%p\n", p);

	while (p) {
		xattr = rb_entry(p, struct ceph_inode_xattr, node);
		tmp = p;
		p = rb_next(tmp);
		dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
		     xattr->name_len, xattr->name);
		rb_erase(tmp, &ci->i_xattrs.index);

		__free_xattr(xattr);
	}

	ci->i_xattrs.names_size = 0;
	ci->i_xattrs.vals_size = 0;
	ci->i_xattrs.index_version = 0;
	ci->i_xattrs.count = 0;
	ci->i_xattrs.index = RB_ROOT;
}

static int __build_xattrs(struct inode *inode)
547 548
	__releases(ci->i_ceph_lock)
	__acquires(ci->i_ceph_lock)
S
Sage Weil 已提交
549 550 551 552 553 554 555 556 557
{
	u32 namelen;
	u32 numattr = 0;
	void *p, *end;
	u32 len;
	const char *name, *val;
	struct ceph_inode_info *ci = ceph_inode(inode);
	int xattr_version;
	struct ceph_inode_xattr **xattrs = NULL;
S
Sage Weil 已提交
558
	int err = 0;
S
Sage Weil 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
	int i;

	dout("__build_xattrs() len=%d\n",
	     ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);

	if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
		return 0; /* already built */

	__ceph_destroy_xattrs(ci);

start:
	/* updated internal xattr rb tree */
	if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
		p = ci->i_xattrs.blob->vec.iov_base;
		end = p + ci->i_xattrs.blob->vec.iov_len;
		ceph_decode_32_safe(&p, end, numattr, bad);
		xattr_version = ci->i_xattrs.version;
576
		spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
577

578
		xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
S
Sage Weil 已提交
579 580 581 582
				 GFP_NOFS);
		err = -ENOMEM;
		if (!xattrs)
			goto bad_lock;
I
Ilya Dryomov 已提交
583

S
Sage Weil 已提交
584 585 586 587 588 589 590
		for (i = 0; i < numattr; i++) {
			xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
					    GFP_NOFS);
			if (!xattrs[i])
				goto bad_lock;
		}

591
		spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
592 593 594 595 596
		if (ci->i_xattrs.version != xattr_version) {
			/* lost a race, retry */
			for (i = 0; i < numattr; i++)
				kfree(xattrs[i]);
			kfree(xattrs);
A
Alan Cox 已提交
597
			xattrs = NULL;
S
Sage Weil 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610
			goto start;
		}
		err = -EIO;
		while (numattr--) {
			ceph_decode_32_safe(&p, end, len, bad);
			namelen = len;
			name = p;
			p += len;
			ceph_decode_32_safe(&p, end, len, bad);
			val = p;
			p += len;

			err = __set_xattr(ci, name, namelen, val, len,
611
					  0, 0, &xattrs[numattr]);
S
Sage Weil 已提交
612 613 614 615 616 617 618 619 620 621 622

			if (err < 0)
				goto bad;
		}
		kfree(xattrs);
	}
	ci->i_xattrs.index_version = ci->i_xattrs.version;
	ci->i_xattrs.dirty = false;

	return err;
bad_lock:
623
	spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
bad:
	if (xattrs) {
		for (i = 0; i < numattr; i++)
			kfree(xattrs[i]);
		kfree(xattrs);
	}
	ci->i_xattrs.names_size = 0;
	return err;
}

static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
				    int val_size)
{
	/*
	 * 4 bytes for the length, and additional 4 bytes per each xattr name,
	 * 4 bytes per each value
	 */
	int size = 4 + ci->i_xattrs.count*(4 + 4) +
			     ci->i_xattrs.names_size +
			     ci->i_xattrs.vals_size;
	dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
	     ci->i_xattrs.count, ci->i_xattrs.names_size,
	     ci->i_xattrs.vals_size);

	if (name_size)
		size += 4 + 4 + name_size + val_size;

	return size;
}

/*
 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
 * and swap into place.
 */
void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
{
	struct rb_node *p;
	struct ceph_inode_xattr *xattr = NULL;
	void *dest;

	dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
	if (ci->i_xattrs.dirty) {
		int need = __get_required_blob_size(ci, 0, 0);

		BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);

		p = rb_first(&ci->i_xattrs.index);
		dest = ci->i_xattrs.prealloc_blob->vec.iov_base;

		ceph_encode_32(&dest, ci->i_xattrs.count);
		while (p) {
			xattr = rb_entry(p, struct ceph_inode_xattr, node);

			ceph_encode_32(&dest, xattr->name_len);
			memcpy(dest, xattr->name, xattr->name_len);
			dest += xattr->name_len;
			ceph_encode_32(&dest, xattr->val_len);
			memcpy(dest, xattr->val, xattr->val_len);
			dest += xattr->val_len;

			p = rb_next(p);
		}

		/* adjust buffer len; it may be larger than we need */
		ci->i_xattrs.prealloc_blob->vec.iov_len =
			dest - ci->i_xattrs.prealloc_blob->vec.iov_base;

S
Sage Weil 已提交
691 692
		if (ci->i_xattrs.blob)
			ceph_buffer_put(ci->i_xattrs.blob);
S
Sage Weil 已提交
693 694 695
		ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
		ci->i_xattrs.prealloc_blob = NULL;
		ci->i_xattrs.dirty = false;
696
		ci->i_xattrs.version++;
S
Sage Weil 已提交
697 698 699
	}
}

Y
Yan, Zheng 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
static inline int __get_request_mask(struct inode *in) {
	struct ceph_mds_request *req = current->journal_info;
	int mask = 0;
	if (req && req->r_target_inode == in) {
		if (req->r_op == CEPH_MDS_OP_LOOKUP ||
		    req->r_op == CEPH_MDS_OP_LOOKUPINO ||
		    req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
		    req->r_op == CEPH_MDS_OP_GETATTR) {
			mask = le32_to_cpu(req->r_args.getattr.mask);
		} else if (req->r_op == CEPH_MDS_OP_OPEN ||
			   req->r_op == CEPH_MDS_OP_CREATE) {
			mask = le32_to_cpu(req->r_args.open.mask);
		}
	}
	return mask;
}

G
Guangliang Zhao 已提交
717
ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
S
Sage Weil 已提交
718 719 720 721
		      size_t size)
{
	struct ceph_inode_info *ci = ceph_inode(inode);
	struct ceph_inode_xattr *xattr;
722
	struct ceph_vxattr *vxattr = NULL;
Y
Yan, Zheng 已提交
723 724
	int req_mask;
	int err;
S
Sage Weil 已提交
725

S
Sage Weil 已提交
726 727
	/* let's see if a virtual xattr was requested */
	vxattr = ceph_match_vxattr(inode, name);
728 729 730 731
	if (vxattr) {
		err = -ENODATA;
		if (!(vxattr->exists_cb && !vxattr->exists_cb(ci)))
			err = vxattr->getxattr_cb(ci, value, size);
732
		return err;
S
Sage Weil 已提交
733 734
	}

Y
Yan, Zheng 已提交
735 736
	req_mask = __get_request_mask(inode);

737 738 739 740
	spin_lock(&ci->i_ceph_lock);
	dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
	     ci->i_xattrs.version, ci->i_xattrs.index_version);

741
	if (ci->i_xattrs.version == 0 ||
Y
Yan, Zheng 已提交
742 743
	    !((req_mask & CEPH_CAP_XATTR_SHARED) ||
	      __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) {
744
		spin_unlock(&ci->i_ceph_lock);
Y
Yan, Zheng 已提交
745 746 747 748 749 750 751 752

		/* security module gets xattr while filling trace */
		if (current->journal_info != NULL) {
			pr_warn_ratelimited("sync getxattr %p "
					    "during filling trace\n", inode);
			return -EBUSY;
		}

S
Sage Weil 已提交
753
		/* get xattrs from mds (if we don't already have them) */
754
		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
S
Sage Weil 已提交
755 756
		if (err)
			return err;
757
		spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
758 759 760 761 762 763 764 765
	}

	err = __build_xattrs(inode);
	if (err < 0)
		goto out;

	err = -ENODATA;  /* == ENOATTR */
	xattr = __get_xattr(ci, name);
S
Sage Weil 已提交
766
	if (!xattr)
S
Sage Weil 已提交
767 768 769 770 771 772 773 774 775 776 777 778
		goto out;

	err = -ERANGE;
	if (size && size < xattr->val_len)
		goto out;

	err = xattr->val_len;
	if (size == 0)
		goto out;

	memcpy(value, xattr->val, xattr->val_len);

Y
Yan, Zheng 已提交
779 780 781
	if (current->journal_info != NULL &&
	    !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
		ci->i_ceph_flags |= CEPH_I_SEC_INITED;
S
Sage Weil 已提交
782
out:
783
	spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
784 785 786 787 788
	return err;
}

ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
{
789
	struct inode *inode = d_inode(dentry);
S
Sage Weil 已提交
790
	struct ceph_inode_info *ci = ceph_inode(inode);
791
	struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
S
Sage Weil 已提交
792 793 794 795 796 797
	u32 vir_namelen = 0;
	u32 namelen;
	int err;
	u32 len;
	int i;

798
	spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
799 800 801
	dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
	     ci->i_xattrs.version, ci->i_xattrs.index_version);

802 803
	if (ci->i_xattrs.version == 0 ||
	    !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
804
		spin_unlock(&ci->i_ceph_lock);
805
		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
S
Sage Weil 已提交
806 807
		if (err)
			return err;
808
		spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
809 810 811 812 813
	}

	err = __build_xattrs(inode);
	if (err < 0)
		goto out;
814 815 816 817 818 819
	/*
	 * Start with virtual dir xattr names (if any) (including
	 * terminating '\0' characters for each).
	 */
	vir_namelen = ceph_vxattrs_name_size(vxattrs);

S
Sage Weil 已提交
820
	/* adding 1 byte per each variable due to the null termination */
821
	namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
S
Sage Weil 已提交
822
	err = -ERANGE;
823
	if (size && vir_namelen + namelen > size)
S
Sage Weil 已提交
824 825
		goto out;

826
	err = namelen + vir_namelen;
S
Sage Weil 已提交
827 828 829 830 831 832
	if (size == 0)
		goto out;

	names = __copy_xattr_names(ci, names);

	/* virtual xattr names, too */
833 834
	err = namelen;
	if (vxattrs) {
S
Sage Weil 已提交
835
		for (i = 0; vxattrs[i].name; i++) {
836 837 838 839 840 841 842
			if (!vxattrs[i].hidden &&
			    !(vxattrs[i].exists_cb &&
			      !vxattrs[i].exists_cb(ci))) {
				len = sprintf(names, "%s", vxattrs[i].name);
				names += len + 1;
				err += len + 1;
			}
S
Sage Weil 已提交
843
		}
844
	}
S
Sage Weil 已提交
845 846

out:
847
	spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
848 849 850
	return err;
}

851
static int ceph_sync_setxattr(struct inode *inode, const char *name,
S
Sage Weil 已提交
852 853
			      const char *value, size_t size, int flags)
{
854
	struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
S
Sage Weil 已提交
855 856
	struct ceph_inode_info *ci = ceph_inode(inode);
	struct ceph_mds_request *req;
857
	struct ceph_mds_client *mdsc = fsc->mdsc;
858
	struct ceph_pagelist *pagelist = NULL;
859
	int op = CEPH_MDS_OP_SETXATTR;
S
Sage Weil 已提交
860
	int err;
861

862
	if (size > 0) {
863 864 865
		/* copy value into pagelist */
		pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
		if (!pagelist)
S
Sage Weil 已提交
866
			return -ENOMEM;
867 868 869 870 871

		ceph_pagelist_init(pagelist);
		err = ceph_pagelist_append(pagelist, value, size);
		if (err)
			goto out;
872
	} else if (!value) {
873 874 875 876
		if (flags & CEPH_XATTR_REPLACE)
			op = CEPH_MDS_OP_RMXATTR;
		else
			flags |= CEPH_XATTR_REMOVE;
S
Sage Weil 已提交
877 878 879 880 881
	}

	dout("setxattr value=%.*s\n", (int)size, value);

	/* do request */
882
	req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
J
Julia Lawall 已提交
883 884 885 886
	if (IS_ERR(req)) {
		err = PTR_ERR(req);
		goto out;
	}
887

S
Sage Weil 已提交
888
	req->r_path2 = kstrdup(name, GFP_NOFS);
889 890 891 892 893
	if (!req->r_path2) {
		ceph_mdsc_put_request(req);
		err = -ENOMEM;
		goto out;
	}
S
Sage Weil 已提交
894

895 896 897 898 899
	if (op == CEPH_MDS_OP_SETXATTR) {
		req->r_args.setxattr.flags = cpu_to_le32(flags);
		req->r_pagelist = pagelist;
		pagelist = NULL;
	}
S
Sage Weil 已提交
900

901 902 903 904 905
	req->r_inode = inode;
	ihold(inode);
	req->r_num_caps = 1;
	req->r_inode_drop = CEPH_CAP_XATTR_SHARED;

S
Sage Weil 已提交
906
	dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
907
	err = ceph_mdsc_do_request(mdsc, NULL, req);
S
Sage Weil 已提交
908 909 910 911
	ceph_mdsc_put_request(req);
	dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);

out:
912 913
	if (pagelist)
		ceph_pagelist_release(pagelist);
S
Sage Weil 已提交
914 915 916
	return err;
}

917
int __ceph_setxattr(struct inode *inode, const char *name,
G
Guangliang Zhao 已提交
918
			const void *value, size_t size, int flags)
S
Sage Weil 已提交
919
{
920
	struct ceph_vxattr *vxattr;
S
Sage Weil 已提交
921
	struct ceph_inode_info *ci = ceph_inode(inode);
922
	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
923
	struct ceph_cap_flush *prealloc_cf = NULL;
924
	int issued;
S
Sage Weil 已提交
925
	int err;
926
	int dirty = 0;
S
Sage Weil 已提交
927 928 929 930 931 932
	int name_len = strlen(name);
	int val_len = size;
	char *newname = NULL;
	char *newval = NULL;
	struct ceph_inode_xattr *xattr = NULL;
	int required_blob_size;
933
	bool lock_snap_rwsem = false;
S
Sage Weil 已提交
934

935 936
	if (ceph_snap(inode) != CEPH_NOSNAP)
		return -EROFS;
S
Sage Weil 已提交
937

938 939 940
	vxattr = ceph_match_vxattr(inode, name);
	if (vxattr && vxattr->readonly)
		return -EOPNOTSUPP;
S
Sage Weil 已提交
941

942 943 944 945
	/* pass any unhandled ceph.* xattrs through to the MDS */
	if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
		goto do_sync_unlocked;

S
Sage Weil 已提交
946 947
	/* preallocate memory for xattr name, value, index node */
	err = -ENOMEM;
J
Julia Lawall 已提交
948
	newname = kmemdup(name, name_len + 1, GFP_NOFS);
S
Sage Weil 已提交
949 950 951 952
	if (!newname)
		goto out;

	if (val_len) {
953
		newval = kmemdup(value, val_len, GFP_NOFS);
S
Sage Weil 已提交
954 955 956 957 958 959 960 961
		if (!newval)
			goto out;
	}

	xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
	if (!xattr)
		goto out;

962 963 964 965
	prealloc_cf = ceph_alloc_cap_flush();
	if (!prealloc_cf)
		goto out;

966
	spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
967 968
retry:
	issued = __ceph_caps_issued(ci, NULL);
969
	if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
S
Sage Weil 已提交
970
		goto do_sync;
971 972 973 974 975 976 977 978 979 980 981 982

	if (!lock_snap_rwsem && !ci->i_head_snapc) {
		lock_snap_rwsem = true;
		if (!down_read_trylock(&mdsc->snap_rwsem)) {
			spin_unlock(&ci->i_ceph_lock);
			down_read(&mdsc->snap_rwsem);
			spin_lock(&ci->i_ceph_lock);
			goto retry;
		}
	}

	dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
S
Sage Weil 已提交
983 984 985 986 987 988
	__build_xattrs(inode);

	required_blob_size = __get_required_blob_size(ci, name_len, val_len);

	if (!ci->i_xattrs.prealloc_blob ||
	    required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
989
		struct ceph_buffer *blob;
S
Sage Weil 已提交
990

991
		spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
992
		dout(" preaallocating new blob size=%d\n", required_blob_size);
S
Sage Weil 已提交
993
		blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
S
Sage Weil 已提交
994
		if (!blob)
995
			goto do_sync_unlocked;
996
		spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
997 998
		if (ci->i_xattrs.prealloc_blob)
			ceph_buffer_put(ci->i_xattrs.prealloc_blob);
S
Sage Weil 已提交
999 1000 1001 1002
		ci->i_xattrs.prealloc_blob = blob;
		goto retry;
	}

1003 1004
	err = __set_xattr(ci, newname, name_len, newval, val_len,
			  flags, value ? 1 : -1, &xattr);
1005

1006
	if (!err) {
1007 1008
		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
					       &prealloc_cf);
1009
		ci->i_xattrs.dirty = true;
1010
		inode->i_ctime = current_fs_time(inode->i_sb);
1011
	}
1012

1013
	spin_unlock(&ci->i_ceph_lock);
1014 1015
	if (lock_snap_rwsem)
		up_read(&mdsc->snap_rwsem);
1016 1017
	if (dirty)
		__mark_inode_dirty(inode, dirty);
1018
	ceph_free_cap_flush(prealloc_cf);
S
Sage Weil 已提交
1019 1020 1021
	return err;

do_sync:
1022
	spin_unlock(&ci->i_ceph_lock);
1023
do_sync_unlocked:
1024 1025
	if (lock_snap_rwsem)
		up_read(&mdsc->snap_rwsem);
Y
Yan, Zheng 已提交
1026 1027 1028 1029 1030 1031 1032

	/* security module set xattr while filling trace */
	if (current->journal_info != NULL) {
		pr_warn_ratelimited("sync setxattr %p "
				    "during filling trace\n", inode);
		err = -EBUSY;
	} else {
1033
		err = ceph_sync_setxattr(inode, name, value, size, flags);
Y
Yan, Zheng 已提交
1034
	}
S
Sage Weil 已提交
1035
out:
1036
	ceph_free_cap_flush(prealloc_cf);
S
Sage Weil 已提交
1037 1038 1039 1040 1041 1042
	kfree(newname);
	kfree(newval);
	kfree(xattr);
	return err;
}

1043 1044 1045
static int ceph_get_xattr_handler(const struct xattr_handler *handler,
				  struct dentry *dentry, struct inode *inode,
				  const char *name, void *value, size_t size)
G
Guangliang Zhao 已提交
1046
{
1047 1048 1049 1050
	if (!ceph_is_valid_xattr(name))
		return -EOPNOTSUPP;
	return __ceph_getxattr(inode, name, value, size);
}
G
Guangliang Zhao 已提交
1051

1052
static int ceph_set_xattr_handler(const struct xattr_handler *handler,
1053 1054 1055
				  struct dentry *unused, struct inode *inode,
				  const char *name, const void *value,
				  size_t size, int flags)
1056 1057 1058
{
	if (!ceph_is_valid_xattr(name))
		return -EOPNOTSUPP;
1059
	return __ceph_setxattr(inode, name, value, size, flags);
G
Guangliang Zhao 已提交
1060
}
Y
Yan, Zheng 已提交
1061

1062 1063 1064 1065 1066 1067
const struct xattr_handler ceph_other_xattr_handler = {
	.prefix = "",  /* match any name => handlers called with full name */
	.get = ceph_get_xattr_handler,
	.set = ceph_set_xattr_handler,
};

Y
Yan, Zheng 已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
#ifdef CONFIG_SECURITY
bool ceph_security_xattr_wanted(struct inode *in)
{
	return in->i_security != NULL;
}

bool ceph_security_xattr_deadlock(struct inode *in)
{
	struct ceph_inode_info *ci;
	bool ret;
	if (in->i_security == NULL)
		return false;
	ci = ceph_inode(in);
	spin_lock(&ci->i_ceph_lock);
	ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
	      !(ci->i_xattrs.version > 0 &&
		__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
	spin_unlock(&ci->i_ceph_lock);
	return ret;
}
#endif