xref: /openbmc/linux/fs/ceph/xattr.c (revision af958a38)
1 #include <linux/ceph/ceph_debug.h>
2 
3 #include "super.h"
4 #include "mds_client.h"
5 
6 #include <linux/ceph/decode.h>
7 
8 #include <linux/xattr.h>
9 #include <linux/posix_acl_xattr.h>
10 #include <linux/slab.h>
11 
12 #define XATTR_CEPH_PREFIX "ceph."
13 #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
14 
15 static int __remove_xattr(struct ceph_inode_info *ci,
16 			  struct ceph_inode_xattr *xattr);
17 
18 /*
19  * List of handlers for synthetic system.* attributes. Other
20  * attributes are handled directly.
21  */
22 const struct xattr_handler *ceph_xattr_handlers[] = {
23 #ifdef CONFIG_CEPH_FS_POSIX_ACL
24 	&posix_acl_access_xattr_handler,
25 	&posix_acl_default_xattr_handler,
26 #endif
27 	NULL,
28 };
29 
30 static bool ceph_is_valid_xattr(const char *name)
31 {
32 	return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
33 	       !strncmp(name, XATTR_SECURITY_PREFIX,
34 			XATTR_SECURITY_PREFIX_LEN) ||
35 	       !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
36 	       !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
37 	       !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
38 }
39 
40 /*
41  * These define virtual xattrs exposing the recursive directory
42  * statistics and layout metadata.
43  */
44 struct ceph_vxattr {
45 	char *name;
46 	size_t name_size;	/* strlen(name) + 1 (for '\0') */
47 	size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
48 			      size_t size);
49 	bool readonly, hidden;
50 	bool (*exists_cb)(struct ceph_inode_info *ci);
51 };
52 
53 /* layouts */
54 
55 static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
56 {
57 	size_t s;
58 	char *p = (char *)&ci->i_layout;
59 
60 	for (s = 0; s < sizeof(ci->i_layout); s++, p++)
61 		if (*p)
62 			return true;
63 	return false;
64 }
65 
66 static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
67 				   size_t size)
68 {
69 	int ret;
70 	struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
71 	struct ceph_osd_client *osdc = &fsc->client->osdc;
72 	s64 pool = ceph_file_layout_pg_pool(ci->i_layout);
73 	const char *pool_name;
74 	char buf[128];
75 
76 	dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
77 	down_read(&osdc->map_sem);
78 	pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
79 	if (pool_name) {
80 		size_t len = strlen(pool_name);
81 		ret = snprintf(buf, sizeof(buf),
82 		"stripe_unit=%lld stripe_count=%lld object_size=%lld pool=",
83 		(unsigned long long)ceph_file_layout_su(ci->i_layout),
84 		(unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
85 	        (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
86 		if (!size) {
87 			ret += len;
88 		} else if (ret + len > size) {
89 			ret = -ERANGE;
90 		} else {
91 			memcpy(val, buf, ret);
92 			memcpy(val + ret, pool_name, len);
93 			ret += len;
94 		}
95 	} else {
96 		ret = snprintf(buf, sizeof(buf),
97 		"stripe_unit=%lld stripe_count=%lld object_size=%lld pool=%lld",
98 		(unsigned long long)ceph_file_layout_su(ci->i_layout),
99 		(unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
100 	        (unsigned long long)ceph_file_layout_object_size(ci->i_layout),
101 		(unsigned long long)pool);
102 		if (size) {
103 			if (ret <= size)
104 				memcpy(val, buf, ret);
105 			else
106 				ret = -ERANGE;
107 		}
108 	}
109 	up_read(&osdc->map_sem);
110 	return ret;
111 }
112 
113 static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
114 					       char *val, size_t size)
115 {
116 	return snprintf(val, size, "%lld",
117 			(unsigned long long)ceph_file_layout_su(ci->i_layout));
118 }
119 
120 static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
121 						char *val, size_t size)
122 {
123 	return snprintf(val, size, "%lld",
124 	       (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout));
125 }
126 
127 static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
128 					       char *val, size_t size)
129 {
130 	return snprintf(val, size, "%lld",
131 	       (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
132 }
133 
134 static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
135 					char *val, size_t size)
136 {
137 	int ret;
138 	struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
139 	struct ceph_osd_client *osdc = &fsc->client->osdc;
140 	s64 pool = ceph_file_layout_pg_pool(ci->i_layout);
141 	const char *pool_name;
142 
143 	down_read(&osdc->map_sem);
144 	pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
145 	if (pool_name)
146 		ret = snprintf(val, size, "%s", pool_name);
147 	else
148 		ret = snprintf(val, size, "%lld", (unsigned long long)pool);
149 	up_read(&osdc->map_sem);
150 	return ret;
151 }
152 
153 /* directories */
154 
155 static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
156 					size_t size)
157 {
158 	return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
159 }
160 
161 static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
162 				      size_t size)
163 {
164 	return snprintf(val, size, "%lld", ci->i_files);
165 }
166 
167 static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
168 					size_t size)
169 {
170 	return snprintf(val, size, "%lld", ci->i_subdirs);
171 }
172 
173 static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
174 					 size_t size)
175 {
176 	return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
177 }
178 
179 static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
180 				       size_t size)
181 {
182 	return snprintf(val, size, "%lld", ci->i_rfiles);
183 }
184 
185 static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
186 					 size_t size)
187 {
188 	return snprintf(val, size, "%lld", ci->i_rsubdirs);
189 }
190 
191 static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
192 				       size_t size)
193 {
194 	return snprintf(val, size, "%lld", ci->i_rbytes);
195 }
196 
197 static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
198 				       size_t size)
199 {
200 	return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
201 			(long)ci->i_rctime.tv_nsec);
202 }
203 
204 
205 #define CEPH_XATTR_NAME(_type, _name)	XATTR_CEPH_PREFIX #_type "." #_name
206 #define CEPH_XATTR_NAME2(_type, _name, _name2)	\
207 	XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
208 
209 #define XATTR_NAME_CEPH(_type, _name)					\
210 	{								\
211 		.name = CEPH_XATTR_NAME(_type, _name),			\
212 		.name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
213 		.getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
214 		.readonly = true,				\
215 		.hidden = false,				\
216 		.exists_cb = NULL,			\
217 	}
218 #define XATTR_LAYOUT_FIELD(_type, _name, _field)			\
219 	{								\
220 		.name = CEPH_XATTR_NAME2(_type, _name, _field),	\
221 		.name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
222 		.getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
223 		.readonly = false,				\
224 		.hidden = true,			\
225 		.exists_cb = ceph_vxattrcb_layout_exists,	\
226 	}
227 
228 static struct ceph_vxattr ceph_dir_vxattrs[] = {
229 	{
230 		.name = "ceph.dir.layout",
231 		.name_size = sizeof("ceph.dir.layout"),
232 		.getxattr_cb = ceph_vxattrcb_layout,
233 		.readonly = false,
234 		.hidden = true,
235 		.exists_cb = ceph_vxattrcb_layout_exists,
236 	},
237 	XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
238 	XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
239 	XATTR_LAYOUT_FIELD(dir, layout, object_size),
240 	XATTR_LAYOUT_FIELD(dir, layout, pool),
241 	XATTR_NAME_CEPH(dir, entries),
242 	XATTR_NAME_CEPH(dir, files),
243 	XATTR_NAME_CEPH(dir, subdirs),
244 	XATTR_NAME_CEPH(dir, rentries),
245 	XATTR_NAME_CEPH(dir, rfiles),
246 	XATTR_NAME_CEPH(dir, rsubdirs),
247 	XATTR_NAME_CEPH(dir, rbytes),
248 	XATTR_NAME_CEPH(dir, rctime),
249 	{ .name = NULL, 0 }	/* Required table terminator */
250 };
251 static size_t ceph_dir_vxattrs_name_size;	/* total size of all names */
252 
253 /* files */
254 
255 static struct ceph_vxattr ceph_file_vxattrs[] = {
256 	{
257 		.name = "ceph.file.layout",
258 		.name_size = sizeof("ceph.file.layout"),
259 		.getxattr_cb = ceph_vxattrcb_layout,
260 		.readonly = false,
261 		.hidden = true,
262 		.exists_cb = ceph_vxattrcb_layout_exists,
263 	},
264 	XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
265 	XATTR_LAYOUT_FIELD(file, layout, stripe_count),
266 	XATTR_LAYOUT_FIELD(file, layout, object_size),
267 	XATTR_LAYOUT_FIELD(file, layout, pool),
268 	{ .name = NULL, 0 }	/* Required table terminator */
269 };
270 static size_t ceph_file_vxattrs_name_size;	/* total size of all names */
271 
272 static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
273 {
274 	if (S_ISDIR(inode->i_mode))
275 		return ceph_dir_vxattrs;
276 	else if (S_ISREG(inode->i_mode))
277 		return ceph_file_vxattrs;
278 	return NULL;
279 }
280 
281 static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
282 {
283 	if (vxattrs == ceph_dir_vxattrs)
284 		return ceph_dir_vxattrs_name_size;
285 	if (vxattrs == ceph_file_vxattrs)
286 		return ceph_file_vxattrs_name_size;
287 	BUG();
288 
289 	return 0;
290 }
291 
292 /*
293  * Compute the aggregate size (including terminating '\0') of all
294  * virtual extended attribute names in the given vxattr table.
295  */
296 static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
297 {
298 	struct ceph_vxattr *vxattr;
299 	size_t size = 0;
300 
301 	for (vxattr = vxattrs; vxattr->name; vxattr++)
302 		if (!vxattr->hidden)
303 			size += vxattr->name_size;
304 
305 	return size;
306 }
307 
308 /* Routines called at initialization and exit time */
309 
310 void __init ceph_xattr_init(void)
311 {
312 	ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
313 	ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
314 }
315 
316 void ceph_xattr_exit(void)
317 {
318 	ceph_dir_vxattrs_name_size = 0;
319 	ceph_file_vxattrs_name_size = 0;
320 }
321 
322 static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
323 						const char *name)
324 {
325 	struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
326 
327 	if (vxattr) {
328 		while (vxattr->name) {
329 			if (!strcmp(vxattr->name, name))
330 				return vxattr;
331 			vxattr++;
332 		}
333 	}
334 
335 	return NULL;
336 }
337 
338 static int __set_xattr(struct ceph_inode_info *ci,
339 			   const char *name, int name_len,
340 			   const char *val, int val_len,
341 			   int flags, int update_xattr,
342 			   struct ceph_inode_xattr **newxattr)
343 {
344 	struct rb_node **p;
345 	struct rb_node *parent = NULL;
346 	struct ceph_inode_xattr *xattr = NULL;
347 	int c;
348 	int new = 0;
349 
350 	p = &ci->i_xattrs.index.rb_node;
351 	while (*p) {
352 		parent = *p;
353 		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
354 		c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
355 		if (c < 0)
356 			p = &(*p)->rb_left;
357 		else if (c > 0)
358 			p = &(*p)->rb_right;
359 		else {
360 			if (name_len == xattr->name_len)
361 				break;
362 			else if (name_len < xattr->name_len)
363 				p = &(*p)->rb_left;
364 			else
365 				p = &(*p)->rb_right;
366 		}
367 		xattr = NULL;
368 	}
369 
370 	if (update_xattr) {
371 		int err = 0;
372 		if (xattr && (flags & XATTR_CREATE))
373 			err = -EEXIST;
374 		else if (!xattr && (flags & XATTR_REPLACE))
375 			err = -ENODATA;
376 		if (err) {
377 			kfree(name);
378 			kfree(val);
379 			return err;
380 		}
381 		if (update_xattr < 0) {
382 			if (xattr)
383 				__remove_xattr(ci, xattr);
384 			kfree(name);
385 			return 0;
386 		}
387 	}
388 
389 	if (!xattr) {
390 		new = 1;
391 		xattr = *newxattr;
392 		xattr->name = name;
393 		xattr->name_len = name_len;
394 		xattr->should_free_name = update_xattr;
395 
396 		ci->i_xattrs.count++;
397 		dout("__set_xattr count=%d\n", ci->i_xattrs.count);
398 	} else {
399 		kfree(*newxattr);
400 		*newxattr = NULL;
401 		if (xattr->should_free_val)
402 			kfree((void *)xattr->val);
403 
404 		if (update_xattr) {
405 			kfree((void *)name);
406 			name = xattr->name;
407 		}
408 		ci->i_xattrs.names_size -= xattr->name_len;
409 		ci->i_xattrs.vals_size -= xattr->val_len;
410 	}
411 	ci->i_xattrs.names_size += name_len;
412 	ci->i_xattrs.vals_size += val_len;
413 	if (val)
414 		xattr->val = val;
415 	else
416 		xattr->val = "";
417 
418 	xattr->val_len = val_len;
419 	xattr->dirty = update_xattr;
420 	xattr->should_free_val = (val && update_xattr);
421 
422 	if (new) {
423 		rb_link_node(&xattr->node, parent, p);
424 		rb_insert_color(&xattr->node, &ci->i_xattrs.index);
425 		dout("__set_xattr_val p=%p\n", p);
426 	}
427 
428 	dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
429 	     ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
430 
431 	return 0;
432 }
433 
434 static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
435 			   const char *name)
436 {
437 	struct rb_node **p;
438 	struct rb_node *parent = NULL;
439 	struct ceph_inode_xattr *xattr = NULL;
440 	int name_len = strlen(name);
441 	int c;
442 
443 	p = &ci->i_xattrs.index.rb_node;
444 	while (*p) {
445 		parent = *p;
446 		xattr = rb_entry(parent, struct ceph_inode_xattr, node);
447 		c = strncmp(name, xattr->name, xattr->name_len);
448 		if (c == 0 && name_len > xattr->name_len)
449 			c = 1;
450 		if (c < 0)
451 			p = &(*p)->rb_left;
452 		else if (c > 0)
453 			p = &(*p)->rb_right;
454 		else {
455 			dout("__get_xattr %s: found %.*s\n", name,
456 			     xattr->val_len, xattr->val);
457 			return xattr;
458 		}
459 	}
460 
461 	dout("__get_xattr %s: not found\n", name);
462 
463 	return NULL;
464 }
465 
466 static void __free_xattr(struct ceph_inode_xattr *xattr)
467 {
468 	BUG_ON(!xattr);
469 
470 	if (xattr->should_free_name)
471 		kfree((void *)xattr->name);
472 	if (xattr->should_free_val)
473 		kfree((void *)xattr->val);
474 
475 	kfree(xattr);
476 }
477 
478 static int __remove_xattr(struct ceph_inode_info *ci,
479 			  struct ceph_inode_xattr *xattr)
480 {
481 	if (!xattr)
482 		return -ENODATA;
483 
484 	rb_erase(&xattr->node, &ci->i_xattrs.index);
485 
486 	if (xattr->should_free_name)
487 		kfree((void *)xattr->name);
488 	if (xattr->should_free_val)
489 		kfree((void *)xattr->val);
490 
491 	ci->i_xattrs.names_size -= xattr->name_len;
492 	ci->i_xattrs.vals_size -= xattr->val_len;
493 	ci->i_xattrs.count--;
494 	kfree(xattr);
495 
496 	return 0;
497 }
498 
499 static int __remove_xattr_by_name(struct ceph_inode_info *ci,
500 			   const char *name)
501 {
502 	struct rb_node **p;
503 	struct ceph_inode_xattr *xattr;
504 	int err;
505 
506 	p = &ci->i_xattrs.index.rb_node;
507 	xattr = __get_xattr(ci, name);
508 	err = __remove_xattr(ci, xattr);
509 	return err;
510 }
511 
512 static char *__copy_xattr_names(struct ceph_inode_info *ci,
513 				char *dest)
514 {
515 	struct rb_node *p;
516 	struct ceph_inode_xattr *xattr = NULL;
517 
518 	p = rb_first(&ci->i_xattrs.index);
519 	dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
520 
521 	while (p) {
522 		xattr = rb_entry(p, struct ceph_inode_xattr, node);
523 		memcpy(dest, xattr->name, xattr->name_len);
524 		dest[xattr->name_len] = '\0';
525 
526 		dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
527 		     xattr->name_len, ci->i_xattrs.names_size);
528 
529 		dest += xattr->name_len + 1;
530 		p = rb_next(p);
531 	}
532 
533 	return dest;
534 }
535 
536 void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
537 {
538 	struct rb_node *p, *tmp;
539 	struct ceph_inode_xattr *xattr = NULL;
540 
541 	p = rb_first(&ci->i_xattrs.index);
542 
543 	dout("__ceph_destroy_xattrs p=%p\n", p);
544 
545 	while (p) {
546 		xattr = rb_entry(p, struct ceph_inode_xattr, node);
547 		tmp = p;
548 		p = rb_next(tmp);
549 		dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
550 		     xattr->name_len, xattr->name);
551 		rb_erase(tmp, &ci->i_xattrs.index);
552 
553 		__free_xattr(xattr);
554 	}
555 
556 	ci->i_xattrs.names_size = 0;
557 	ci->i_xattrs.vals_size = 0;
558 	ci->i_xattrs.index_version = 0;
559 	ci->i_xattrs.count = 0;
560 	ci->i_xattrs.index = RB_ROOT;
561 }
562 
563 static int __build_xattrs(struct inode *inode)
564 	__releases(ci->i_ceph_lock)
565 	__acquires(ci->i_ceph_lock)
566 {
567 	u32 namelen;
568 	u32 numattr = 0;
569 	void *p, *end;
570 	u32 len;
571 	const char *name, *val;
572 	struct ceph_inode_info *ci = ceph_inode(inode);
573 	int xattr_version;
574 	struct ceph_inode_xattr **xattrs = NULL;
575 	int err = 0;
576 	int i;
577 
578 	dout("__build_xattrs() len=%d\n",
579 	     ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
580 
581 	if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
582 		return 0; /* already built */
583 
584 	__ceph_destroy_xattrs(ci);
585 
586 start:
587 	/* updated internal xattr rb tree */
588 	if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
589 		p = ci->i_xattrs.blob->vec.iov_base;
590 		end = p + ci->i_xattrs.blob->vec.iov_len;
591 		ceph_decode_32_safe(&p, end, numattr, bad);
592 		xattr_version = ci->i_xattrs.version;
593 		spin_unlock(&ci->i_ceph_lock);
594 
595 		xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
596 				 GFP_NOFS);
597 		err = -ENOMEM;
598 		if (!xattrs)
599 			goto bad_lock;
600 
601 		for (i = 0; i < numattr; i++) {
602 			xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
603 					    GFP_NOFS);
604 			if (!xattrs[i])
605 				goto bad_lock;
606 		}
607 
608 		spin_lock(&ci->i_ceph_lock);
609 		if (ci->i_xattrs.version != xattr_version) {
610 			/* lost a race, retry */
611 			for (i = 0; i < numattr; i++)
612 				kfree(xattrs[i]);
613 			kfree(xattrs);
614 			xattrs = NULL;
615 			goto start;
616 		}
617 		err = -EIO;
618 		while (numattr--) {
619 			ceph_decode_32_safe(&p, end, len, bad);
620 			namelen = len;
621 			name = p;
622 			p += len;
623 			ceph_decode_32_safe(&p, end, len, bad);
624 			val = p;
625 			p += len;
626 
627 			err = __set_xattr(ci, name, namelen, val, len,
628 					  0, 0, &xattrs[numattr]);
629 
630 			if (err < 0)
631 				goto bad;
632 		}
633 		kfree(xattrs);
634 	}
635 	ci->i_xattrs.index_version = ci->i_xattrs.version;
636 	ci->i_xattrs.dirty = false;
637 
638 	return err;
639 bad_lock:
640 	spin_lock(&ci->i_ceph_lock);
641 bad:
642 	if (xattrs) {
643 		for (i = 0; i < numattr; i++)
644 			kfree(xattrs[i]);
645 		kfree(xattrs);
646 	}
647 	ci->i_xattrs.names_size = 0;
648 	return err;
649 }
650 
651 static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
652 				    int val_size)
653 {
654 	/*
655 	 * 4 bytes for the length, and additional 4 bytes per each xattr name,
656 	 * 4 bytes per each value
657 	 */
658 	int size = 4 + ci->i_xattrs.count*(4 + 4) +
659 			     ci->i_xattrs.names_size +
660 			     ci->i_xattrs.vals_size;
661 	dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
662 	     ci->i_xattrs.count, ci->i_xattrs.names_size,
663 	     ci->i_xattrs.vals_size);
664 
665 	if (name_size)
666 		size += 4 + 4 + name_size + val_size;
667 
668 	return size;
669 }
670 
671 /*
672  * If there are dirty xattrs, reencode xattrs into the prealloc_blob
673  * and swap into place.
674  */
675 void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
676 {
677 	struct rb_node *p;
678 	struct ceph_inode_xattr *xattr = NULL;
679 	void *dest;
680 
681 	dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
682 	if (ci->i_xattrs.dirty) {
683 		int need = __get_required_blob_size(ci, 0, 0);
684 
685 		BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
686 
687 		p = rb_first(&ci->i_xattrs.index);
688 		dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
689 
690 		ceph_encode_32(&dest, ci->i_xattrs.count);
691 		while (p) {
692 			xattr = rb_entry(p, struct ceph_inode_xattr, node);
693 
694 			ceph_encode_32(&dest, xattr->name_len);
695 			memcpy(dest, xattr->name, xattr->name_len);
696 			dest += xattr->name_len;
697 			ceph_encode_32(&dest, xattr->val_len);
698 			memcpy(dest, xattr->val, xattr->val_len);
699 			dest += xattr->val_len;
700 
701 			p = rb_next(p);
702 		}
703 
704 		/* adjust buffer len; it may be larger than we need */
705 		ci->i_xattrs.prealloc_blob->vec.iov_len =
706 			dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
707 
708 		if (ci->i_xattrs.blob)
709 			ceph_buffer_put(ci->i_xattrs.blob);
710 		ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
711 		ci->i_xattrs.prealloc_blob = NULL;
712 		ci->i_xattrs.dirty = false;
713 		ci->i_xattrs.version++;
714 	}
715 }
716 
717 ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
718 		      size_t size)
719 {
720 	struct ceph_inode_info *ci = ceph_inode(inode);
721 	int err;
722 	struct ceph_inode_xattr *xattr;
723 	struct ceph_vxattr *vxattr = NULL;
724 
725 	if (!ceph_is_valid_xattr(name))
726 		return -ENODATA;
727 
728 	/* let's see if a virtual xattr was requested */
729 	vxattr = ceph_match_vxattr(inode, name);
730 	if (vxattr && !(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
731 		err = vxattr->getxattr_cb(ci, value, size);
732 		return err;
733 	}
734 
735 	spin_lock(&ci->i_ceph_lock);
736 	dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
737 	     ci->i_xattrs.version, ci->i_xattrs.index_version);
738 
739 	if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
740 	    (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
741 		goto get_xattr;
742 	} else {
743 		spin_unlock(&ci->i_ceph_lock);
744 		/* get xattrs from mds (if we don't already have them) */
745 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
746 		if (err)
747 			return err;
748 	}
749 
750 	spin_lock(&ci->i_ceph_lock);
751 
752 	err = __build_xattrs(inode);
753 	if (err < 0)
754 		goto out;
755 
756 get_xattr:
757 	err = -ENODATA;  /* == ENOATTR */
758 	xattr = __get_xattr(ci, name);
759 	if (!xattr)
760 		goto out;
761 
762 	err = -ERANGE;
763 	if (size && size < xattr->val_len)
764 		goto out;
765 
766 	err = xattr->val_len;
767 	if (size == 0)
768 		goto out;
769 
770 	memcpy(value, xattr->val, xattr->val_len);
771 
772 out:
773 	spin_unlock(&ci->i_ceph_lock);
774 	return err;
775 }
776 
777 ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
778 		      size_t size)
779 {
780 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
781 		return generic_getxattr(dentry, name, value, size);
782 
783 	return __ceph_getxattr(dentry->d_inode, name, value, size);
784 }
785 
786 ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
787 {
788 	struct inode *inode = dentry->d_inode;
789 	struct ceph_inode_info *ci = ceph_inode(inode);
790 	struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
791 	u32 vir_namelen = 0;
792 	u32 namelen;
793 	int err;
794 	u32 len;
795 	int i;
796 
797 	spin_lock(&ci->i_ceph_lock);
798 	dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
799 	     ci->i_xattrs.version, ci->i_xattrs.index_version);
800 
801 	if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
802 	    (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
803 		goto list_xattr;
804 	} else {
805 		spin_unlock(&ci->i_ceph_lock);
806 		err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
807 		if (err)
808 			return err;
809 	}
810 
811 	spin_lock(&ci->i_ceph_lock);
812 
813 	err = __build_xattrs(inode);
814 	if (err < 0)
815 		goto out;
816 
817 list_xattr:
818 	/*
819 	 * Start with virtual dir xattr names (if any) (including
820 	 * terminating '\0' characters for each).
821 	 */
822 	vir_namelen = ceph_vxattrs_name_size(vxattrs);
823 
824 	/* adding 1 byte per each variable due to the null termination */
825 	namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
826 	err = -ERANGE;
827 	if (size && vir_namelen + namelen > size)
828 		goto out;
829 
830 	err = namelen + vir_namelen;
831 	if (size == 0)
832 		goto out;
833 
834 	names = __copy_xattr_names(ci, names);
835 
836 	/* virtual xattr names, too */
837 	err = namelen;
838 	if (vxattrs) {
839 		for (i = 0; vxattrs[i].name; i++) {
840 			if (!vxattrs[i].hidden &&
841 			    !(vxattrs[i].exists_cb &&
842 			      !vxattrs[i].exists_cb(ci))) {
843 				len = sprintf(names, "%s", vxattrs[i].name);
844 				names += len + 1;
845 				err += len + 1;
846 			}
847 		}
848 	}
849 
850 out:
851 	spin_unlock(&ci->i_ceph_lock);
852 	return err;
853 }
854 
855 static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
856 			      const char *value, size_t size, int flags)
857 {
858 	struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
859 	struct inode *inode = dentry->d_inode;
860 	struct ceph_inode_info *ci = ceph_inode(inode);
861 	struct ceph_mds_request *req;
862 	struct ceph_mds_client *mdsc = fsc->mdsc;
863 	int err;
864 	int i, nr_pages;
865 	struct page **pages = NULL;
866 	void *kaddr;
867 
868 	/* copy value into some pages */
869 	nr_pages = calc_pages_for(0, size);
870 	if (nr_pages) {
871 		pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
872 		if (!pages)
873 			return -ENOMEM;
874 		err = -ENOMEM;
875 		for (i = 0; i < nr_pages; i++) {
876 			pages[i] = __page_cache_alloc(GFP_NOFS);
877 			if (!pages[i]) {
878 				nr_pages = i;
879 				goto out;
880 			}
881 			kaddr = kmap(pages[i]);
882 			memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
883 			       min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
884 		}
885 	}
886 
887 	dout("setxattr value=%.*s\n", (int)size, value);
888 
889 	if (!value)
890 		flags |= CEPH_XATTR_REMOVE;
891 
892 	/* do request */
893 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
894 				       USE_AUTH_MDS);
895 	if (IS_ERR(req)) {
896 		err = PTR_ERR(req);
897 		goto out;
898 	}
899 	req->r_inode = inode;
900 	ihold(inode);
901 	req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
902 	req->r_num_caps = 1;
903 	req->r_args.setxattr.flags = cpu_to_le32(flags);
904 	req->r_path2 = kstrdup(name, GFP_NOFS);
905 
906 	req->r_pages = pages;
907 	req->r_num_pages = nr_pages;
908 	req->r_data_len = size;
909 
910 	dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
911 	err = ceph_mdsc_do_request(mdsc, NULL, req);
912 	ceph_mdsc_put_request(req);
913 	dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
914 
915 out:
916 	if (pages) {
917 		for (i = 0; i < nr_pages; i++)
918 			__free_page(pages[i]);
919 		kfree(pages);
920 	}
921 	return err;
922 }
923 
924 int __ceph_setxattr(struct dentry *dentry, const char *name,
925 			const void *value, size_t size, int flags)
926 {
927 	struct inode *inode = dentry->d_inode;
928 	struct ceph_vxattr *vxattr;
929 	struct ceph_inode_info *ci = ceph_inode(inode);
930 	int issued;
931 	int err;
932 	int dirty = 0;
933 	int name_len = strlen(name);
934 	int val_len = size;
935 	char *newname = NULL;
936 	char *newval = NULL;
937 	struct ceph_inode_xattr *xattr = NULL;
938 	int required_blob_size;
939 
940 	if (!ceph_is_valid_xattr(name))
941 		return -EOPNOTSUPP;
942 
943 	vxattr = ceph_match_vxattr(inode, name);
944 	if (vxattr && vxattr->readonly)
945 		return -EOPNOTSUPP;
946 
947 	/* pass any unhandled ceph.* xattrs through to the MDS */
948 	if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
949 		goto do_sync_unlocked;
950 
951 	/* preallocate memory for xattr name, value, index node */
952 	err = -ENOMEM;
953 	newname = kmemdup(name, name_len + 1, GFP_NOFS);
954 	if (!newname)
955 		goto out;
956 
957 	if (val_len) {
958 		newval = kmemdup(value, val_len, GFP_NOFS);
959 		if (!newval)
960 			goto out;
961 	}
962 
963 	xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
964 	if (!xattr)
965 		goto out;
966 
967 	spin_lock(&ci->i_ceph_lock);
968 retry:
969 	issued = __ceph_caps_issued(ci, NULL);
970 	dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
971 	if (!(issued & CEPH_CAP_XATTR_EXCL))
972 		goto do_sync;
973 	__build_xattrs(inode);
974 
975 	required_blob_size = __get_required_blob_size(ci, name_len, val_len);
976 
977 	if (!ci->i_xattrs.prealloc_blob ||
978 	    required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
979 		struct ceph_buffer *blob;
980 
981 		spin_unlock(&ci->i_ceph_lock);
982 		dout(" preaallocating new blob size=%d\n", required_blob_size);
983 		blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
984 		if (!blob)
985 			goto out;
986 		spin_lock(&ci->i_ceph_lock);
987 		if (ci->i_xattrs.prealloc_blob)
988 			ceph_buffer_put(ci->i_xattrs.prealloc_blob);
989 		ci->i_xattrs.prealloc_blob = blob;
990 		goto retry;
991 	}
992 
993 	err = __set_xattr(ci, newname, name_len, newval, val_len,
994 			  flags, value ? 1 : -1, &xattr);
995 
996 	if (!err) {
997 		dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
998 		ci->i_xattrs.dirty = true;
999 		inode->i_ctime = CURRENT_TIME;
1000 	}
1001 
1002 	spin_unlock(&ci->i_ceph_lock);
1003 	if (dirty)
1004 		__mark_inode_dirty(inode, dirty);
1005 	return err;
1006 
1007 do_sync:
1008 	spin_unlock(&ci->i_ceph_lock);
1009 do_sync_unlocked:
1010 	err = ceph_sync_setxattr(dentry, name, value, size, flags);
1011 out:
1012 	kfree(newname);
1013 	kfree(newval);
1014 	kfree(xattr);
1015 	return err;
1016 }
1017 
1018 int ceph_setxattr(struct dentry *dentry, const char *name,
1019 		  const void *value, size_t size, int flags)
1020 {
1021 	if (ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
1022 		return -EROFS;
1023 
1024 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1025 		return generic_setxattr(dentry, name, value, size, flags);
1026 
1027 	return __ceph_setxattr(dentry, name, value, size, flags);
1028 }
1029 
1030 static int ceph_send_removexattr(struct dentry *dentry, const char *name)
1031 {
1032 	struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
1033 	struct ceph_mds_client *mdsc = fsc->mdsc;
1034 	struct inode *inode = dentry->d_inode;
1035 	struct ceph_mds_request *req;
1036 	int err;
1037 
1038 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
1039 				       USE_AUTH_MDS);
1040 	if (IS_ERR(req))
1041 		return PTR_ERR(req);
1042 	req->r_inode = inode;
1043 	ihold(inode);
1044 	req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
1045 	req->r_num_caps = 1;
1046 	req->r_path2 = kstrdup(name, GFP_NOFS);
1047 
1048 	err = ceph_mdsc_do_request(mdsc, NULL, req);
1049 	ceph_mdsc_put_request(req);
1050 	return err;
1051 }
1052 
1053 int __ceph_removexattr(struct dentry *dentry, const char *name)
1054 {
1055 	struct inode *inode = dentry->d_inode;
1056 	struct ceph_vxattr *vxattr;
1057 	struct ceph_inode_info *ci = ceph_inode(inode);
1058 	int issued;
1059 	int err;
1060 	int required_blob_size;
1061 	int dirty;
1062 
1063 	if (!ceph_is_valid_xattr(name))
1064 		return -EOPNOTSUPP;
1065 
1066 	vxattr = ceph_match_vxattr(inode, name);
1067 	if (vxattr && vxattr->readonly)
1068 		return -EOPNOTSUPP;
1069 
1070 	/* pass any unhandled ceph.* xattrs through to the MDS */
1071 	if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
1072 		goto do_sync_unlocked;
1073 
1074 	err = -ENOMEM;
1075 	spin_lock(&ci->i_ceph_lock);
1076 retry:
1077 	issued = __ceph_caps_issued(ci, NULL);
1078 	dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
1079 
1080 	if (!(issued & CEPH_CAP_XATTR_EXCL))
1081 		goto do_sync;
1082 	__build_xattrs(inode);
1083 
1084 	required_blob_size = __get_required_blob_size(ci, 0, 0);
1085 
1086 	if (!ci->i_xattrs.prealloc_blob ||
1087 	    required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
1088 		struct ceph_buffer *blob;
1089 
1090 		spin_unlock(&ci->i_ceph_lock);
1091 		dout(" preaallocating new blob size=%d\n", required_blob_size);
1092 		blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
1093 		if (!blob)
1094 			goto out;
1095 		spin_lock(&ci->i_ceph_lock);
1096 		if (ci->i_xattrs.prealloc_blob)
1097 			ceph_buffer_put(ci->i_xattrs.prealloc_blob);
1098 		ci->i_xattrs.prealloc_blob = blob;
1099 		goto retry;
1100 	}
1101 
1102 	err = __remove_xattr_by_name(ceph_inode(inode), name);
1103 
1104 	dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
1105 	ci->i_xattrs.dirty = true;
1106 	inode->i_ctime = CURRENT_TIME;
1107 	spin_unlock(&ci->i_ceph_lock);
1108 	if (dirty)
1109 		__mark_inode_dirty(inode, dirty);
1110 	return err;
1111 do_sync:
1112 	spin_unlock(&ci->i_ceph_lock);
1113 do_sync_unlocked:
1114 	err = ceph_send_removexattr(dentry, name);
1115 out:
1116 	return err;
1117 }
1118 
1119 int ceph_removexattr(struct dentry *dentry, const char *name)
1120 {
1121 	if (ceph_snap(dentry->d_inode) != CEPH_NOSNAP)
1122 		return -EROFS;
1123 
1124 	if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1125 		return generic_removexattr(dentry, name);
1126 
1127 	return __ceph_removexattr(dentry, name);
1128 }
1129