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