xattr.c 32.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
#include <linux/ceph/ceph_debug.h>
3
#include <linux/ceph/pagelist.h>
4

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

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

#include <linux/xattr.h>
Y
Yan, Zheng 已提交
11
#include <linux/security.h>
12
#include <linux/posix_acl_xattr.h>
13
#include <linux/slab.h>
S
Sage Weil 已提交
14

15 16 17
#define XATTR_CEPH_PREFIX "ceph."
#define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)

18 19 20
static int __remove_xattr(struct ceph_inode_info *ci,
			  struct ceph_inode_xattr *xattr);

S
Sage Weil 已提交
21 22
static bool ceph_is_valid_xattr(const char *name)
{
23
	return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
S
Sage Weil 已提交
24 25 26 27 28 29 30 31
	       !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.
 */
32
struct ceph_vxattr {
S
Sage Weil 已提交
33
	char *name;
34
	size_t name_size;	/* strlen(name) + 1 (for '\0') */
35 36
	ssize_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
			       size_t size);
S
Sage Weil 已提交
37
	bool (*exists_cb)(struct ceph_inode_info *ci);
38
	unsigned int flags;
S
Sage Weil 已提交
39 40
};

41 42
#define VXATTR_FLAG_READONLY		(1<<0)
#define VXATTR_FLAG_HIDDEN		(1<<1)
43
#define VXATTR_FLAG_RSTAT		(1<<2)
44

45 46 47 48
/* layouts */

static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
{
Y
Yan, Zheng 已提交
49 50 51 52
	struct ceph_file_layout *fl = &ci->i_layout;
	return (fl->stripe_unit > 0 || fl->stripe_count > 0 ||
		fl->object_size > 0 || fl->pool_id >= 0 ||
		rcu_dereference_raw(fl->pool_ns) != NULL);
53 54
}

55 56
static ssize_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
				    size_t size)
57 58 59
{
	struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
	struct ceph_osd_client *osdc = &fsc->client->osdc;
Y
Yan, Zheng 已提交
60
	struct ceph_string *pool_ns;
61
	s64 pool = ci->i_layout.pool_id;
62
	const char *pool_name;
Y
Yan, Zheng 已提交
63
	const char *ns_field = " pool_namespace=";
64
	char buf[128];
Y
Yan, Zheng 已提交
65 66 67 68
	size_t len, total_len = 0;
	int ret;

	pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
69 70

	dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
71
	down_read(&osdc->lock);
72
	pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
73
	if (pool_name) {
Y
Yan, Zheng 已提交
74
		len = snprintf(buf, sizeof(buf),
75 76 77
		"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);
Y
Yan, Zheng 已提交
78
		total_len = len + strlen(pool_name);
79
	} else {
Y
Yan, Zheng 已提交
80
		len = snprintf(buf, sizeof(buf),
81 82
		"stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
		ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
83
		ci->i_layout.object_size, pool);
Y
Yan, Zheng 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
		total_len = len;
	}

	if (pool_ns)
		total_len += strlen(ns_field) + pool_ns->len;

	if (!size) {
		ret = total_len;
	} else if (total_len > size) {
		ret = -ERANGE;
	} else {
		memcpy(val, buf, len);
		ret = len;
		if (pool_name) {
			len = strlen(pool_name);
			memcpy(val + ret, pool_name, len);
			ret += len;
		}
		if (pool_ns) {
			len = strlen(ns_field);
			memcpy(val + ret, ns_field, len);
			ret += len;
			memcpy(val + ret, pool_ns->str, pool_ns->len);
			ret += pool_ns->len;
108 109
		}
	}
110
	up_read(&osdc->lock);
Y
Yan, Zheng 已提交
111
	ceph_put_string(pool_ns);
112 113 114
	return ret;
}

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

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

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

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

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

152 153
static ssize_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
						   char *val, size_t size)
Y
Yan, Zheng 已提交
154 155 156 157
{
	int ret = 0;
	struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
	if (ns) {
158
		ret = snprintf(val, size, "%.*s", ns->len, ns->str);
Y
Yan, Zheng 已提交
159 160 161 162 163
		ceph_put_string(ns);
	}
	return ret;
}

S
Sage Weil 已提交
164 165
/* directories */

166 167
static ssize_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
					 size_t size)
S
Sage Weil 已提交
168 169 170 171
{
	return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
}

172 173
static ssize_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
				       size_t size)
S
Sage Weil 已提交
174 175 176 177
{
	return snprintf(val, size, "%lld", ci->i_files);
}

178 179
static ssize_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
					 size_t size)
S
Sage Weil 已提交
180 181 182 183
{
	return snprintf(val, size, "%lld", ci->i_subdirs);
}

184 185
static ssize_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
					  size_t size)
S
Sage Weil 已提交
186 187 188 189
{
	return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
}

190 191
static ssize_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
					size_t size)
S
Sage Weil 已提交
192 193 194 195
{
	return snprintf(val, size, "%lld", ci->i_rfiles);
}

196 197
static ssize_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
					  size_t size)
S
Sage Weil 已提交
198 199 200 201
{
	return snprintf(val, size, "%lld", ci->i_rsubdirs);
}

202 203
static ssize_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
					size_t size)
S
Sage Weil 已提交
204 205 206 207
{
	return snprintf(val, size, "%lld", ci->i_rbytes);
}

208 209
static ssize_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
					size_t size)
S
Sage Weil 已提交
210
{
211
	return snprintf(val, size, "%lld.%09ld", ci->i_rctime.tv_sec,
212
			ci->i_rctime.tv_nsec);
S
Sage Weil 已提交
213 214
}

215 216 217 218 219 220
/* dir pin */
static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info *ci)
{
	return ci->i_dir_pin != -ENODATA;
}

221 222
static ssize_t ceph_vxattrcb_dir_pin(struct ceph_inode_info *ci, char *val,
				     size_t size)
223 224 225
{
	return snprintf(val, size, "%d", (int)ci->i_dir_pin);
}
226

227
/* quotas */
228 229
static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
{
230 231 232 233 234 235 236 237 238
	bool ret = false;
	spin_lock(&ci->i_ceph_lock);
	if ((ci->i_max_files || ci->i_max_bytes) &&
	    ci->i_vino.snap == CEPH_NOSNAP &&
	    ci->i_snap_realm &&
	    ci->i_snap_realm->ino == ci->i_vino.ino)
		ret = true;
	spin_unlock(&ci->i_ceph_lock);
	return ret;
239 240
}

241 242
static ssize_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
				   size_t size)
243 244 245 246 247
{
	return snprintf(val, size, "max_bytes=%llu max_files=%llu",
			ci->i_max_bytes, ci->i_max_files);
}

248 249
static ssize_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
					     char *val, size_t size)
250 251 252 253
{
	return snprintf(val, size, "%llu", ci->i_max_bytes);
}

254 255
static ssize_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
					     char *val, size_t size)
256 257 258
{
	return snprintf(val, size, "%llu", ci->i_max_files);
}
259

260 261 262 263 264 265
/* snapshots */
static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info *ci)
{
	return (ci->i_snap_btime.tv_sec != 0 || ci->i_snap_btime.tv_nsec != 0);
}

266 267
static ssize_t ceph_vxattrcb_snap_btime(struct ceph_inode_info *ci, char *val,
					size_t size)
268 269 270 271 272
{
	return snprintf(val, size, "%lld.%09ld", ci->i_snap_btime.tv_sec,
			ci->i_snap_btime.tv_nsec);
}

273
#define CEPH_XATTR_NAME(_type, _name)	XATTR_CEPH_PREFIX #_type "." #_name
274 275
#define CEPH_XATTR_NAME2(_type, _name, _name2)	\
	XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
276

277
#define XATTR_NAME_CEPH(_type, _name, _flags)				\
S
Sage Weil 已提交
278 279 280 281
	{								\
		.name = CEPH_XATTR_NAME(_type, _name),			\
		.name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
		.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
282 283
		.exists_cb = NULL,					\
		.flags = (VXATTR_FLAG_READONLY | _flags),		\
S
Sage Weil 已提交
284
	}
285 286
#define XATTR_RSTAT_FIELD(_type, _name)			\
	XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
287 288 289 290 291 292
#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, \
		.exists_cb = ceph_vxattrcb_layout_exists,	\
293
		.flags = VXATTR_FLAG_HIDDEN,			\
294
	}
295 296 297 298 299 300
#define XATTR_QUOTA_FIELD(_type, _name)					\
	{								\
		.name = CEPH_XATTR_NAME(_type, _name),			\
		.name_size = sizeof(CEPH_XATTR_NAME(_type, _name)),	\
		.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name,	\
		.exists_cb = ceph_vxattrcb_quota_exists,		\
301
		.flags = VXATTR_FLAG_HIDDEN,				\
302
	}
303

304
static struct ceph_vxattr ceph_dir_vxattrs[] = {
S
Sage Weil 已提交
305 306 307 308 309
	{
		.name = "ceph.dir.layout",
		.name_size = sizeof("ceph.dir.layout"),
		.getxattr_cb = ceph_vxattrcb_layout,
		.exists_cb = ceph_vxattrcb_layout_exists,
310
		.flags = VXATTR_FLAG_HIDDEN,
S
Sage Weil 已提交
311
	},
312 313 314 315
	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),
Y
Yan, Zheng 已提交
316
	XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
317 318 319 320 321 322 323 324
	XATTR_NAME_CEPH(dir, entries, 0),
	XATTR_NAME_CEPH(dir, files, 0),
	XATTR_NAME_CEPH(dir, subdirs, 0),
	XATTR_RSTAT_FIELD(dir, rentries),
	XATTR_RSTAT_FIELD(dir, rfiles),
	XATTR_RSTAT_FIELD(dir, rsubdirs),
	XATTR_RSTAT_FIELD(dir, rbytes),
	XATTR_RSTAT_FIELD(dir, rctime),
325 326
	{
		.name = "ceph.dir.pin",
327
		.name_size = sizeof("ceph.dir.pin"),
328 329 330 331
		.getxattr_cb = ceph_vxattrcb_dir_pin,
		.exists_cb = ceph_vxattrcb_dir_pin_exists,
		.flags = VXATTR_FLAG_HIDDEN,
	},
332 333 334 335 336
	{
		.name = "ceph.quota",
		.name_size = sizeof("ceph.quota"),
		.getxattr_cb = ceph_vxattrcb_quota,
		.exists_cb = ceph_vxattrcb_quota_exists,
337
		.flags = VXATTR_FLAG_HIDDEN,
338 339 340
	},
	XATTR_QUOTA_FIELD(quota, max_bytes),
	XATTR_QUOTA_FIELD(quota, max_files),
341 342 343 344 345 346 347
	{
		.name = "ceph.snap.btime",
		.name_size = sizeof("ceph.snap.btime"),
		.getxattr_cb = ceph_vxattrcb_snap_btime,
		.exists_cb = ceph_vxattrcb_snap_btime_exists,
		.flags = VXATTR_FLAG_READONLY,
	},
348
	{ .name = NULL, 0 }	/* Required table terminator */
S
Sage Weil 已提交
349 350 351 352
};

/* files */

353
static struct ceph_vxattr ceph_file_vxattrs[] = {
354 355 356 357 358
	{
		.name = "ceph.file.layout",
		.name_size = sizeof("ceph.file.layout"),
		.getxattr_cb = ceph_vxattrcb_layout,
		.exists_cb = ceph_vxattrcb_layout_exists,
359
		.flags = VXATTR_FLAG_HIDDEN,
360
	},
361 362 363 364
	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),
Y
Yan, Zheng 已提交
365
	XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
366 367 368 369 370 371 372
	{
		.name = "ceph.snap.btime",
		.name_size = sizeof("ceph.snap.btime"),
		.getxattr_cb = ceph_vxattrcb_snap_btime,
		.exists_cb = ceph_vxattrcb_snap_btime_exists,
		.flags = VXATTR_FLAG_READONLY,
	},
373
	{ .name = NULL, 0 }	/* Required table terminator */
S
Sage Weil 已提交
374 375
};

376
static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
S
Sage Weil 已提交
377 378 379 380 381 382 383 384
{
	if (S_ISDIR(inode->i_mode))
		return ceph_dir_vxattrs;
	else if (S_ISREG(inode->i_mode))
		return ceph_file_vxattrs;
	return NULL;
}

385
static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
S
Sage Weil 已提交
386 387
						const char *name)
{
388
	struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
389 390 391 392 393 394 395 396 397

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

S
Sage Weil 已提交
398 399 400 401 402 403
	return NULL;
}

static int __set_xattr(struct ceph_inode_info *ci,
			   const char *name, int name_len,
			   const char *val, int val_len,
404
			   int flags, int update_xattr,
S
Sage Weil 已提交
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
			   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;
	}

433 434
	if (update_xattr) {
		int err = 0;
435

436 437 438 439 440 441 442
		if (xattr && (flags & XATTR_CREATE))
			err = -EEXIST;
		else if (!xattr && (flags & XATTR_REPLACE))
			err = -ENODATA;
		if (err) {
			kfree(name);
			kfree(val);
443
			kfree(*newxattr);
444 445
			return err;
		}
446 447 448 449
		if (update_xattr < 0) {
			if (xattr)
				__remove_xattr(ci, xattr);
			kfree(name);
450
			kfree(*newxattr);
451 452
			return 0;
		}
453 454
	}

S
Sage Weil 已提交
455 456 457 458 459
	if (!xattr) {
		new = 1;
		xattr = *newxattr;
		xattr->name = name;
		xattr->name_len = name_len;
460
		xattr->should_free_name = update_xattr;
S
Sage Weil 已提交
461 462 463 464 465 466 467 468 469

		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);

470
		if (update_xattr) {
S
Sage Weil 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484
			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;
485 486
	xattr->dirty = update_xattr;
	xattr->should_free_val = (val && update_xattr);
S
Sage Weil 已提交
487 488 489 490 491 492 493

	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);
	}

494 495
	dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s\n",
	     ceph_vinop(&ci->vfs_inode), xattr, name_len, name, val_len, val);
S
Sage Weil 已提交
496 497 498 499 500 501 502 503 504 505

	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 已提交
506
	int name_len = strlen(name);
S
Sage Weil 已提交
507 508 509 510 511 512 513
	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 已提交
514 515
		if (c == 0 && name_len > xattr->name_len)
			c = 1;
S
Sage Weil 已提交
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 547
		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 已提交
548
		return -ENODATA;
S
Sage Weil 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

	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)
617 618
	__releases(ci->i_ceph_lock)
	__acquires(ci->i_ceph_lock)
S
Sage Weil 已提交
619 620 621 622 623 624 625 626 627
{
	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 已提交
628
	int err = 0;
S
Sage Weil 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
	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;
646
		spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
647

648
		xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
S
Sage Weil 已提交
649 650 651 652
				 GFP_NOFS);
		err = -ENOMEM;
		if (!xattrs)
			goto bad_lock;
I
Ilya Dryomov 已提交
653

S
Sage Weil 已提交
654 655 656 657 658 659 660
		for (i = 0; i < numattr; i++) {
			xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
					    GFP_NOFS);
			if (!xattrs[i])
				goto bad_lock;
		}

661
		spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
662 663 664 665 666
		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 已提交
667
			xattrs = NULL;
S
Sage Weil 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680
			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,
681
					  0, 0, &xattrs[numattr]);
S
Sage Weil 已提交
682 683 684 685 686 687 688 689 690 691 692

			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:
693
	spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
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 已提交
761 762
		if (ci->i_xattrs.blob)
			ceph_buffer_put(ci->i_xattrs.blob);
S
Sage Weil 已提交
763 764 765
		ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
		ci->i_xattrs.prealloc_blob = NULL;
		ci->i_xattrs.dirty = false;
766
		ci->i_xattrs.version++;
S
Sage Weil 已提交
767 768 769
	}
}

Y
Yan, Zheng 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
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 已提交
787
ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
S
Sage Weil 已提交
788 789 790 791
		      size_t size)
{
	struct ceph_inode_info *ci = ceph_inode(inode);
	struct ceph_inode_xattr *xattr;
792
	struct ceph_vxattr *vxattr = NULL;
Y
Yan, Zheng 已提交
793
	int req_mask;
794
	ssize_t err;
S
Sage Weil 已提交
795

S
Sage Weil 已提交
796 797
	/* let's see if a virtual xattr was requested */
	vxattr = ceph_match_vxattr(inode, name);
798
	if (vxattr) {
799 800 801 802
		int mask = 0;
		if (vxattr->flags & VXATTR_FLAG_RSTAT)
			mask |= CEPH_STAT_RSTAT;
		err = ceph_do_getattr(inode, mask, true);
803 804
		if (err)
			return err;
805 806 807
		err = -ENODATA;
		if (!(vxattr->exists_cb && !vxattr->exists_cb(ci)))
			err = vxattr->getxattr_cb(ci, value, size);
808
		return err;
S
Sage Weil 已提交
809 810
	}

Y
Yan, Zheng 已提交
811 812
	req_mask = __get_request_mask(inode);

813 814 815 816
	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);

817
	if (ci->i_xattrs.version == 0 ||
Y
Yan, Zheng 已提交
818 819
	    !((req_mask & CEPH_CAP_XATTR_SHARED) ||
	      __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) {
820
		spin_unlock(&ci->i_ceph_lock);
Y
Yan, Zheng 已提交
821 822

		/* security module gets xattr while filling trace */
823
		if (current->journal_info) {
Y
Yan, Zheng 已提交
824 825 826 827 828
			pr_warn_ratelimited("sync getxattr %p "
					    "during filling trace\n", inode);
			return -EBUSY;
		}

S
Sage Weil 已提交
829
		/* get xattrs from mds (if we don't already have them) */
830
		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
S
Sage Weil 已提交
831 832
		if (err)
			return err;
833
		spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
834 835 836 837 838 839 840 841
	}

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

	err = -ENODATA;  /* == ENOATTR */
	xattr = __get_xattr(ci, name);
S
Sage Weil 已提交
842
	if (!xattr)
S
Sage Weil 已提交
843 844 845 846 847 848 849 850 851 852 853 854
		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);

855
	if (current->journal_info &&
Y
Yan, Zheng 已提交
856 857
	    !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
		ci->i_ceph_flags |= CEPH_I_SEC_INITED;
S
Sage Weil 已提交
858
out:
859
	spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
860 861 862 863 864
	return err;
}

ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
{
865
	struct inode *inode = d_inode(dentry);
S
Sage Weil 已提交
866
	struct ceph_inode_info *ci = ceph_inode(inode);
867
	struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
868
	bool len_only = (size == 0);
S
Sage Weil 已提交
869 870 871 872
	u32 namelen;
	int err;
	int i;

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

877 878
	if (ci->i_xattrs.version == 0 ||
	    !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
879
		spin_unlock(&ci->i_ceph_lock);
880
		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
S
Sage Weil 已提交
881 882
		if (err)
			return err;
883
		spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
884 885 886 887 888
	}

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

890
	/* add 1 byte for each xattr due to the null termination */
891
	namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
892 893 894 895 896 897 898 899
	if (!len_only) {
		if (namelen > size) {
			err = -ERANGE;
			goto out;
		}
		names = __copy_xattr_names(ci, names);
		size -= namelen;
	}
S
Sage Weil 已提交
900 901 902


	/* virtual xattr names, too */
903
	if (vxattrs) {
S
Sage Weil 已提交
904
		for (i = 0; vxattrs[i].name; i++) {
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
			size_t this_len;

			if (vxattrs[i].flags & VXATTR_FLAG_HIDDEN)
				continue;
			if (vxattrs[i].exists_cb && !vxattrs[i].exists_cb(ci))
				continue;

			this_len = strlen(vxattrs[i].name) + 1;
			namelen += this_len;
			if (len_only)
				continue;

			if (this_len > size) {
				err = -ERANGE;
				goto out;
920
			}
921 922 923 924

			memcpy(names, vxattrs[i].name, this_len);
			names += this_len;
			size -= this_len;
S
Sage Weil 已提交
925
		}
926
	}
927
	err = namelen;
S
Sage Weil 已提交
928
out:
929
	spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
930 931 932
	return err;
}

933
static int ceph_sync_setxattr(struct inode *inode, const char *name,
S
Sage Weil 已提交
934 935
			      const char *value, size_t size, int flags)
{
936
	struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
S
Sage Weil 已提交
937 938
	struct ceph_inode_info *ci = ceph_inode(inode);
	struct ceph_mds_request *req;
939
	struct ceph_mds_client *mdsc = fsc->mdsc;
940
	struct ceph_pagelist *pagelist = NULL;
941
	int op = CEPH_MDS_OP_SETXATTR;
S
Sage Weil 已提交
942
	int err;
943

944
	if (size > 0) {
945
		/* copy value into pagelist */
946
		pagelist = ceph_pagelist_alloc(GFP_NOFS);
947
		if (!pagelist)
S
Sage Weil 已提交
948
			return -ENOMEM;
949 950 951 952

		err = ceph_pagelist_append(pagelist, value, size);
		if (err)
			goto out;
953
	} else if (!value) {
954 955 956 957
		if (flags & CEPH_XATTR_REPLACE)
			op = CEPH_MDS_OP_RMXATTR;
		else
			flags |= CEPH_XATTR_REMOVE;
S
Sage Weil 已提交
958 959 960 961 962
	}

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

	/* do request */
963
	req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
J
Julia Lawall 已提交
964 965 966 967
	if (IS_ERR(req)) {
		err = PTR_ERR(req);
		goto out;
	}
968

S
Sage Weil 已提交
969
	req->r_path2 = kstrdup(name, GFP_NOFS);
970 971 972 973 974
	if (!req->r_path2) {
		ceph_mdsc_put_request(req);
		err = -ENOMEM;
		goto out;
	}
S
Sage Weil 已提交
975

976 977 978 979 980
	if (op == CEPH_MDS_OP_SETXATTR) {
		req->r_args.setxattr.flags = cpu_to_le32(flags);
		req->r_pagelist = pagelist;
		pagelist = NULL;
	}
S
Sage Weil 已提交
981

982 983 984 985 986
	req->r_inode = inode;
	ihold(inode);
	req->r_num_caps = 1;
	req->r_inode_drop = CEPH_CAP_XATTR_SHARED;

S
Sage Weil 已提交
987
	dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
988
	err = ceph_mdsc_do_request(mdsc, NULL, req);
S
Sage Weil 已提交
989 990 991 992
	ceph_mdsc_put_request(req);
	dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);

out:
993 994
	if (pagelist)
		ceph_pagelist_release(pagelist);
S
Sage Weil 已提交
995 996 997
	return err;
}

998
int __ceph_setxattr(struct inode *inode, const char *name,
G
Guangliang Zhao 已提交
999
			const void *value, size_t size, int flags)
S
Sage Weil 已提交
1000
{
1001
	struct ceph_vxattr *vxattr;
S
Sage Weil 已提交
1002
	struct ceph_inode_info *ci = ceph_inode(inode);
1003
	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1004
	struct ceph_cap_flush *prealloc_cf = NULL;
1005
	int issued;
S
Sage Weil 已提交
1006
	int err;
1007
	int dirty = 0;
S
Sage Weil 已提交
1008 1009 1010 1011 1012 1013
	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;
1014
	bool check_realm = false;
1015
	bool lock_snap_rwsem = false;
S
Sage Weil 已提交
1016

1017 1018
	if (ceph_snap(inode) != CEPH_NOSNAP)
		return -EROFS;
S
Sage Weil 已提交
1019

1020
	vxattr = ceph_match_vxattr(inode, name);
1021
	if (vxattr) {
1022
		if (vxattr->flags & VXATTR_FLAG_READONLY)
1023 1024 1025 1026
			return -EOPNOTSUPP;
		if (value && !strncmp(vxattr->name, "ceph.quota", 10))
			check_realm = true;
	}
S
Sage Weil 已提交
1027

1028 1029 1030 1031
	/* 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 已提交
1032 1033
	/* preallocate memory for xattr name, value, index node */
	err = -ENOMEM;
J
Julia Lawall 已提交
1034
	newname = kmemdup(name, name_len + 1, GFP_NOFS);
S
Sage Weil 已提交
1035 1036 1037 1038
	if (!newname)
		goto out;

	if (val_len) {
1039
		newval = kmemdup(value, val_len, GFP_NOFS);
S
Sage Weil 已提交
1040 1041 1042 1043 1044 1045 1046 1047
		if (!newval)
			goto out;
	}

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

1048 1049 1050 1051
	prealloc_cf = ceph_alloc_cap_flush();
	if (!prealloc_cf)
		goto out;

1052
	spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
1053 1054
retry:
	issued = __ceph_caps_issued(ci, NULL);
1055
	if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
S
Sage Weil 已提交
1056
		goto do_sync;
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068

	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 已提交
1069 1070 1071 1072 1073 1074
	__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) {
1075
		struct ceph_buffer *blob;
S
Sage Weil 已提交
1076

1077
		spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
1078
		dout(" preaallocating new blob size=%d\n", required_blob_size);
S
Sage Weil 已提交
1079
		blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
S
Sage Weil 已提交
1080
		if (!blob)
1081
			goto do_sync_unlocked;
1082
		spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
1083 1084
		if (ci->i_xattrs.prealloc_blob)
			ceph_buffer_put(ci->i_xattrs.prealloc_blob);
S
Sage Weil 已提交
1085 1086 1087 1088
		ci->i_xattrs.prealloc_blob = blob;
		goto retry;
	}

1089 1090
	err = __set_xattr(ci, newname, name_len, newval, val_len,
			  flags, value ? 1 : -1, &xattr);
1091

1092
	if (!err) {
1093 1094
		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
					       &prealloc_cf);
1095
		ci->i_xattrs.dirty = true;
1096
		inode->i_ctime = current_time(inode);
1097
	}
1098

1099
	spin_unlock(&ci->i_ceph_lock);
1100 1101
	if (lock_snap_rwsem)
		up_read(&mdsc->snap_rwsem);
1102 1103
	if (dirty)
		__mark_inode_dirty(inode, dirty);
1104
	ceph_free_cap_flush(prealloc_cf);
S
Sage Weil 已提交
1105 1106 1107
	return err;

do_sync:
1108
	spin_unlock(&ci->i_ceph_lock);
1109
do_sync_unlocked:
1110 1111
	if (lock_snap_rwsem)
		up_read(&mdsc->snap_rwsem);
Y
Yan, Zheng 已提交
1112 1113

	/* security module set xattr while filling trace */
1114
	if (current->journal_info) {
Y
Yan, Zheng 已提交
1115 1116 1117 1118
		pr_warn_ratelimited("sync setxattr %p "
				    "during filling trace\n", inode);
		err = -EBUSY;
	} else {
1119
		err = ceph_sync_setxattr(inode, name, value, size, flags);
1120 1121 1122 1123 1124 1125 1126 1127 1128
		if (err >= 0 && check_realm) {
			/* check if snaprealm was created for quota inode */
			spin_lock(&ci->i_ceph_lock);
			if ((ci->i_max_files || ci->i_max_bytes) &&
			    !(ci->i_snap_realm &&
			      ci->i_snap_realm->ino == ci->i_vino.ino))
				err = -EOPNOTSUPP;
			spin_unlock(&ci->i_ceph_lock);
		}
Y
Yan, Zheng 已提交
1129
	}
S
Sage Weil 已提交
1130
out:
1131
	ceph_free_cap_flush(prealloc_cf);
S
Sage Weil 已提交
1132 1133 1134 1135 1136 1137
	kfree(newname);
	kfree(newval);
	kfree(xattr);
	return err;
}

1138 1139 1140
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 已提交
1141
{
1142 1143 1144 1145
	if (!ceph_is_valid_xattr(name))
		return -EOPNOTSUPP;
	return __ceph_getxattr(inode, name, value, size);
}
G
Guangliang Zhao 已提交
1146

1147
static int ceph_set_xattr_handler(const struct xattr_handler *handler,
1148 1149 1150
				  struct dentry *unused, struct inode *inode,
				  const char *name, const void *value,
				  size_t size, int flags)
1151 1152 1153
{
	if (!ceph_is_valid_xattr(name))
		return -EOPNOTSUPP;
1154
	return __ceph_setxattr(inode, name, value, size, flags);
G
Guangliang Zhao 已提交
1155
}
Y
Yan, Zheng 已提交
1156

W
Wei Yongjun 已提交
1157
static const struct xattr_handler ceph_other_xattr_handler = {
1158 1159 1160 1161 1162
	.prefix = "",  /* match any name => handlers called with full name */
	.get = ceph_get_xattr_handler,
	.set = ceph_set_xattr_handler,
};

Y
Yan, Zheng 已提交
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
#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;
1173
	if (!in->i_security)
Y
Yan, Zheng 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182
		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;
}
Y
Yan, Zheng 已提交
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

#ifdef CONFIG_CEPH_FS_SECURITY_LABEL
int ceph_security_init_secctx(struct dentry *dentry, umode_t mode,
			   struct ceph_acl_sec_ctx *as_ctx)
{
	struct ceph_pagelist *pagelist = as_ctx->pagelist;
	const char *name;
	size_t name_len;
	int err;

	err = security_dentry_init_security(dentry, mode, &dentry->d_name,
					    &as_ctx->sec_ctx,
					    &as_ctx->sec_ctxlen);
	if (err < 0) {
		WARN_ON_ONCE(err != -EOPNOTSUPP);
		err = 0; /* do nothing */
		goto out;
	}

	err = -ENOMEM;
	if (!pagelist) {
		pagelist = ceph_pagelist_alloc(GFP_KERNEL);
		if (!pagelist)
			goto out;
		err = ceph_pagelist_reserve(pagelist, PAGE_SIZE);
		if (err)
			goto out;
		ceph_pagelist_encode_32(pagelist, 1);
	}

	/*
	 * FIXME: Make security_dentry_init_security() generic. Currently
	 * It only supports single security module and only selinux has
	 * dentry_init_security hook.
	 */
	name = XATTR_NAME_SELINUX;
	name_len = strlen(name);
	err = ceph_pagelist_reserve(pagelist,
				    4 * 2 + name_len + as_ctx->sec_ctxlen);
	if (err)
		goto out;

	if (as_ctx->pagelist) {
		/* update count of KV pairs */
		BUG_ON(pagelist->length <= sizeof(__le32));
		if (list_is_singular(&pagelist->head)) {
			le32_add_cpu((__le32*)pagelist->mapped_tail, 1);
		} else {
			struct page *page = list_first_entry(&pagelist->head,
							     struct page, lru);
			void *addr = kmap_atomic(page);
			le32_add_cpu((__le32*)addr, 1);
			kunmap_atomic(addr);
		}
	} else {
		as_ctx->pagelist = pagelist;
	}

	ceph_pagelist_encode_32(pagelist, name_len);
	ceph_pagelist_append(pagelist, name, name_len);

	ceph_pagelist_encode_32(pagelist, as_ctx->sec_ctxlen);
	ceph_pagelist_append(pagelist, as_ctx->sec_ctx, as_ctx->sec_ctxlen);

	err = 0;
out:
	if (pagelist && !as_ctx->pagelist)
		ceph_pagelist_release(pagelist);
	return err;
}

void ceph_security_invalidate_secctx(struct inode *inode)
{
	security_inode_invalidate_secctx(inode);
}

static int ceph_xattr_set_security_label(const struct xattr_handler *handler,
				    struct dentry *unused, struct inode *inode,
				    const char *key, const void *buf,
				    size_t buflen, int flags)
{
	if (security_ismaclabel(key)) {
		const char *name = xattr_full_name(handler, key);
		return __ceph_setxattr(inode, name, buf, buflen, flags);
	}
	return  -EOPNOTSUPP;
}

static int ceph_xattr_get_security_label(const struct xattr_handler *handler,
				    struct dentry *unused, struct inode *inode,
				    const char *key, void *buf, size_t buflen)
{
	if (security_ismaclabel(key)) {
		const char *name = xattr_full_name(handler, key);
		return __ceph_getxattr(inode, name, buf, buflen);
	}
	return  -EOPNOTSUPP;
}

static const struct xattr_handler ceph_security_label_handler = {
	.prefix = XATTR_SECURITY_PREFIX,
	.get    = ceph_xattr_get_security_label,
	.set    = ceph_xattr_set_security_label,
};
#endif
Y
Yan, Zheng 已提交
1288
#endif
1289 1290 1291 1292 1293 1294

void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx *as_ctx)
{
#ifdef CONFIG_CEPH_FS_POSIX_ACL
	posix_acl_release(as_ctx->acl);
	posix_acl_release(as_ctx->default_acl);
Y
Yan, Zheng 已提交
1295 1296 1297
#endif
#ifdef CONFIG_CEPH_FS_SECURITY_LABEL
	security_release_secctx(as_ctx->sec_ctx, as_ctx->sec_ctxlen);
1298 1299 1300 1301
#endif
	if (as_ctx->pagelist)
		ceph_pagelist_release(as_ctx->pagelist);
}
Y
Yan, Zheng 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317

/*
 * 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
	&posix_acl_access_xattr_handler,
	&posix_acl_default_xattr_handler,
#endif
#ifdef CONFIG_CEPH_FS_SECURITY_LABEL
	&ceph_security_label_handler,
#endif
	&ceph_other_xattr_handler,
	NULL,
};