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