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