blob: bfff735091f5f63905e4893f52de89e571b2848b [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
2
Sage Weil355da1e2009-10-06 11:31:08 -07003#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include "mds_client.h"
5
6#include <linux/ceph/decode.h>
Sage Weil355da1e2009-10-06 11:31:08 -07007
8#include <linux/xattr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Sage Weil355da1e2009-10-06 11:31:08 -070010
11static bool ceph_is_valid_xattr(const char *name)
12{
Sage Weil1a756272010-05-11 11:40:25 -070013 return !strncmp(name, "ceph.", 5) ||
14 !strncmp(name, XATTR_SECURITY_PREFIX,
Sage Weil355da1e2009-10-06 11:31:08 -070015 XATTR_SECURITY_PREFIX_LEN) ||
16 !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
17 !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
18}
19
20/*
21 * These define virtual xattrs exposing the recursive directory
22 * statistics and layout metadata.
23 */
24struct ceph_vxattr_cb {
25 bool readonly;
26 char *name;
27 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
28 size_t size);
29};
30
31/* directories */
32
33static size_t ceph_vxattrcb_entries(struct ceph_inode_info *ci, char *val,
34 size_t size)
35{
36 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
37}
38
39static size_t ceph_vxattrcb_files(struct ceph_inode_info *ci, char *val,
40 size_t size)
41{
42 return snprintf(val, size, "%lld", ci->i_files);
43}
44
45static size_t ceph_vxattrcb_subdirs(struct ceph_inode_info *ci, char *val,
46 size_t size)
47{
48 return snprintf(val, size, "%lld", ci->i_subdirs);
49}
50
51static size_t ceph_vxattrcb_rentries(struct ceph_inode_info *ci, char *val,
52 size_t size)
53{
54 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
55}
56
57static size_t ceph_vxattrcb_rfiles(struct ceph_inode_info *ci, char *val,
58 size_t size)
59{
60 return snprintf(val, size, "%lld", ci->i_rfiles);
61}
62
63static size_t ceph_vxattrcb_rsubdirs(struct ceph_inode_info *ci, char *val,
64 size_t size)
65{
66 return snprintf(val, size, "%lld", ci->i_rsubdirs);
67}
68
69static size_t ceph_vxattrcb_rbytes(struct ceph_inode_info *ci, char *val,
70 size_t size)
71{
72 return snprintf(val, size, "%lld", ci->i_rbytes);
73}
74
75static size_t ceph_vxattrcb_rctime(struct ceph_inode_info *ci, char *val,
76 size_t size)
77{
78 return snprintf(val, size, "%ld.%ld", (long)ci->i_rctime.tv_sec,
79 (long)ci->i_rctime.tv_nsec);
80}
81
82static struct ceph_vxattr_cb ceph_dir_vxattrs[] = {
Sage Weil1a756272010-05-11 11:40:25 -070083 { true, "ceph.dir.entries", ceph_vxattrcb_entries},
84 { true, "ceph.dir.files", ceph_vxattrcb_files},
85 { true, "ceph.dir.subdirs", ceph_vxattrcb_subdirs},
86 { true, "ceph.dir.rentries", ceph_vxattrcb_rentries},
87 { true, "ceph.dir.rfiles", ceph_vxattrcb_rfiles},
88 { true, "ceph.dir.rsubdirs", ceph_vxattrcb_rsubdirs},
89 { true, "ceph.dir.rbytes", ceph_vxattrcb_rbytes},
90 { true, "ceph.dir.rctime", ceph_vxattrcb_rctime},
Sage Weil355da1e2009-10-06 11:31:08 -070091 { true, NULL, NULL }
92};
93
94/* files */
95
96static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
97 size_t size)
98{
Sage Weilb195bef2009-10-07 10:59:30 -070099 int ret;
100
101 ret = snprintf(val, size,
Sage Weil355da1e2009-10-06 11:31:08 -0700102 "chunk_bytes=%lld\nstripe_count=%lld\nobject_size=%lld\n",
103 (unsigned long long)ceph_file_layout_su(ci->i_layout),
104 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
105 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
Sage Weilb195bef2009-10-07 10:59:30 -0700106 if (ceph_file_layout_pg_preferred(ci->i_layout))
107 ret += snprintf(val + ret, size, "preferred_osd=%lld\n",
108 (unsigned long long)ceph_file_layout_pg_preferred(
109 ci->i_layout));
110 return ret;
Sage Weil355da1e2009-10-06 11:31:08 -0700111}
112
113static struct ceph_vxattr_cb ceph_file_vxattrs[] = {
Alex Elder114fc472012-01-11 17:41:01 -0800114 { true, "ceph.file.layout", ceph_vxattrcb_layout},
115 /* The following extended attribute name is deprecated */
Sage Weil1a756272010-05-11 11:40:25 -0700116 { true, "ceph.layout", ceph_vxattrcb_layout},
Alex Elder114fc472012-01-11 17:41:01 -0800117 { true, NULL, NULL }
Sage Weil355da1e2009-10-06 11:31:08 -0700118};
119
120static struct ceph_vxattr_cb *ceph_inode_vxattrs(struct inode *inode)
121{
122 if (S_ISDIR(inode->i_mode))
123 return ceph_dir_vxattrs;
124 else if (S_ISREG(inode->i_mode))
125 return ceph_file_vxattrs;
126 return NULL;
127}
128
129static struct ceph_vxattr_cb *ceph_match_vxattr(struct ceph_vxattr_cb *vxattr,
130 const char *name)
131{
132 do {
133 if (strcmp(vxattr->name, name) == 0)
134 return vxattr;
135 vxattr++;
136 } while (vxattr->name);
137 return NULL;
138}
139
140static int __set_xattr(struct ceph_inode_info *ci,
141 const char *name, int name_len,
142 const char *val, int val_len,
143 int dirty,
144 int should_free_name, int should_free_val,
145 struct ceph_inode_xattr **newxattr)
146{
147 struct rb_node **p;
148 struct rb_node *parent = NULL;
149 struct ceph_inode_xattr *xattr = NULL;
150 int c;
151 int new = 0;
152
153 p = &ci->i_xattrs.index.rb_node;
154 while (*p) {
155 parent = *p;
156 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
157 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
158 if (c < 0)
159 p = &(*p)->rb_left;
160 else if (c > 0)
161 p = &(*p)->rb_right;
162 else {
163 if (name_len == xattr->name_len)
164 break;
165 else if (name_len < xattr->name_len)
166 p = &(*p)->rb_left;
167 else
168 p = &(*p)->rb_right;
169 }
170 xattr = NULL;
171 }
172
173 if (!xattr) {
174 new = 1;
175 xattr = *newxattr;
176 xattr->name = name;
177 xattr->name_len = name_len;
178 xattr->should_free_name = should_free_name;
179
180 ci->i_xattrs.count++;
181 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
182 } else {
183 kfree(*newxattr);
184 *newxattr = NULL;
185 if (xattr->should_free_val)
186 kfree((void *)xattr->val);
187
188 if (should_free_name) {
189 kfree((void *)name);
190 name = xattr->name;
191 }
192 ci->i_xattrs.names_size -= xattr->name_len;
193 ci->i_xattrs.vals_size -= xattr->val_len;
194 }
Sage Weil355da1e2009-10-06 11:31:08 -0700195 ci->i_xattrs.names_size += name_len;
196 ci->i_xattrs.vals_size += val_len;
197 if (val)
198 xattr->val = val;
199 else
200 xattr->val = "";
201
202 xattr->val_len = val_len;
203 xattr->dirty = dirty;
204 xattr->should_free_val = (val && should_free_val);
205
206 if (new) {
207 rb_link_node(&xattr->node, parent, p);
208 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
209 dout("__set_xattr_val p=%p\n", p);
210 }
211
212 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
213 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
214
215 return 0;
216}
217
218static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
219 const char *name)
220{
221 struct rb_node **p;
222 struct rb_node *parent = NULL;
223 struct ceph_inode_xattr *xattr = NULL;
Sage Weil17db1432011-01-13 15:27:29 -0800224 int name_len = strlen(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700225 int c;
226
227 p = &ci->i_xattrs.index.rb_node;
228 while (*p) {
229 parent = *p;
230 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
231 c = strncmp(name, xattr->name, xattr->name_len);
Sage Weil17db1432011-01-13 15:27:29 -0800232 if (c == 0 && name_len > xattr->name_len)
233 c = 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700234 if (c < 0)
235 p = &(*p)->rb_left;
236 else if (c > 0)
237 p = &(*p)->rb_right;
238 else {
239 dout("__get_xattr %s: found %.*s\n", name,
240 xattr->val_len, xattr->val);
241 return xattr;
242 }
243 }
244
245 dout("__get_xattr %s: not found\n", name);
246
247 return NULL;
248}
249
250static void __free_xattr(struct ceph_inode_xattr *xattr)
251{
252 BUG_ON(!xattr);
253
254 if (xattr->should_free_name)
255 kfree((void *)xattr->name);
256 if (xattr->should_free_val)
257 kfree((void *)xattr->val);
258
259 kfree(xattr);
260}
261
262static int __remove_xattr(struct ceph_inode_info *ci,
263 struct ceph_inode_xattr *xattr)
264{
265 if (!xattr)
266 return -EOPNOTSUPP;
267
268 rb_erase(&xattr->node, &ci->i_xattrs.index);
269
270 if (xattr->should_free_name)
271 kfree((void *)xattr->name);
272 if (xattr->should_free_val)
273 kfree((void *)xattr->val);
274
275 ci->i_xattrs.names_size -= xattr->name_len;
276 ci->i_xattrs.vals_size -= xattr->val_len;
277 ci->i_xattrs.count--;
278 kfree(xattr);
279
280 return 0;
281}
282
283static int __remove_xattr_by_name(struct ceph_inode_info *ci,
284 const char *name)
285{
286 struct rb_node **p;
287 struct ceph_inode_xattr *xattr;
288 int err;
289
290 p = &ci->i_xattrs.index.rb_node;
291 xattr = __get_xattr(ci, name);
292 err = __remove_xattr(ci, xattr);
293 return err;
294}
295
296static char *__copy_xattr_names(struct ceph_inode_info *ci,
297 char *dest)
298{
299 struct rb_node *p;
300 struct ceph_inode_xattr *xattr = NULL;
301
302 p = rb_first(&ci->i_xattrs.index);
303 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
304
305 while (p) {
306 xattr = rb_entry(p, struct ceph_inode_xattr, node);
307 memcpy(dest, xattr->name, xattr->name_len);
308 dest[xattr->name_len] = '\0';
309
310 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
311 xattr->name_len, ci->i_xattrs.names_size);
312
313 dest += xattr->name_len + 1;
314 p = rb_next(p);
315 }
316
317 return dest;
318}
319
320void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
321{
322 struct rb_node *p, *tmp;
323 struct ceph_inode_xattr *xattr = NULL;
324
325 p = rb_first(&ci->i_xattrs.index);
326
327 dout("__ceph_destroy_xattrs p=%p\n", p);
328
329 while (p) {
330 xattr = rb_entry(p, struct ceph_inode_xattr, node);
331 tmp = p;
332 p = rb_next(tmp);
333 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
334 xattr->name_len, xattr->name);
335 rb_erase(tmp, &ci->i_xattrs.index);
336
337 __free_xattr(xattr);
338 }
339
340 ci->i_xattrs.names_size = 0;
341 ci->i_xattrs.vals_size = 0;
342 ci->i_xattrs.index_version = 0;
343 ci->i_xattrs.count = 0;
344 ci->i_xattrs.index = RB_ROOT;
345}
346
347static int __build_xattrs(struct inode *inode)
Sage Weilbe655592011-11-30 09:47:09 -0800348 __releases(ci->i_ceph_lock)
349 __acquires(ci->i_ceph_lock)
Sage Weil355da1e2009-10-06 11:31:08 -0700350{
351 u32 namelen;
352 u32 numattr = 0;
353 void *p, *end;
354 u32 len;
355 const char *name, *val;
356 struct ceph_inode_info *ci = ceph_inode(inode);
357 int xattr_version;
358 struct ceph_inode_xattr **xattrs = NULL;
Sage Weil63ff78b2009-11-01 17:51:15 -0800359 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700360 int i;
361
362 dout("__build_xattrs() len=%d\n",
363 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
364
365 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
366 return 0; /* already built */
367
368 __ceph_destroy_xattrs(ci);
369
370start:
371 /* updated internal xattr rb tree */
372 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
373 p = ci->i_xattrs.blob->vec.iov_base;
374 end = p + ci->i_xattrs.blob->vec.iov_len;
375 ceph_decode_32_safe(&p, end, numattr, bad);
376 xattr_version = ci->i_xattrs.version;
Sage Weilbe655592011-11-30 09:47:09 -0800377 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700378
379 xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
380 GFP_NOFS);
381 err = -ENOMEM;
382 if (!xattrs)
383 goto bad_lock;
384 memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
385 for (i = 0; i < numattr; i++) {
386 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
387 GFP_NOFS);
388 if (!xattrs[i])
389 goto bad_lock;
390 }
391
Sage Weilbe655592011-11-30 09:47:09 -0800392 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700393 if (ci->i_xattrs.version != xattr_version) {
394 /* lost a race, retry */
395 for (i = 0; i < numattr; i++)
396 kfree(xattrs[i]);
397 kfree(xattrs);
398 goto start;
399 }
400 err = -EIO;
401 while (numattr--) {
402 ceph_decode_32_safe(&p, end, len, bad);
403 namelen = len;
404 name = p;
405 p += len;
406 ceph_decode_32_safe(&p, end, len, bad);
407 val = p;
408 p += len;
409
410 err = __set_xattr(ci, name, namelen, val, len,
411 0, 0, 0, &xattrs[numattr]);
412
413 if (err < 0)
414 goto bad;
415 }
416 kfree(xattrs);
417 }
418 ci->i_xattrs.index_version = ci->i_xattrs.version;
419 ci->i_xattrs.dirty = false;
420
421 return err;
422bad_lock:
Sage Weilbe655592011-11-30 09:47:09 -0800423 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700424bad:
425 if (xattrs) {
426 for (i = 0; i < numattr; i++)
427 kfree(xattrs[i]);
428 kfree(xattrs);
429 }
430 ci->i_xattrs.names_size = 0;
431 return err;
432}
433
434static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
435 int val_size)
436{
437 /*
438 * 4 bytes for the length, and additional 4 bytes per each xattr name,
439 * 4 bytes per each value
440 */
441 int size = 4 + ci->i_xattrs.count*(4 + 4) +
442 ci->i_xattrs.names_size +
443 ci->i_xattrs.vals_size;
444 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
445 ci->i_xattrs.count, ci->i_xattrs.names_size,
446 ci->i_xattrs.vals_size);
447
448 if (name_size)
449 size += 4 + 4 + name_size + val_size;
450
451 return size;
452}
453
454/*
455 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
456 * and swap into place.
457 */
458void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
459{
460 struct rb_node *p;
461 struct ceph_inode_xattr *xattr = NULL;
462 void *dest;
463
464 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
465 if (ci->i_xattrs.dirty) {
466 int need = __get_required_blob_size(ci, 0, 0);
467
468 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
469
470 p = rb_first(&ci->i_xattrs.index);
471 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
472
473 ceph_encode_32(&dest, ci->i_xattrs.count);
474 while (p) {
475 xattr = rb_entry(p, struct ceph_inode_xattr, node);
476
477 ceph_encode_32(&dest, xattr->name_len);
478 memcpy(dest, xattr->name, xattr->name_len);
479 dest += xattr->name_len;
480 ceph_encode_32(&dest, xattr->val_len);
481 memcpy(dest, xattr->val, xattr->val_len);
482 dest += xattr->val_len;
483
484 p = rb_next(p);
485 }
486
487 /* adjust buffer len; it may be larger than we need */
488 ci->i_xattrs.prealloc_blob->vec.iov_len =
489 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
490
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800491 if (ci->i_xattrs.blob)
492 ceph_buffer_put(ci->i_xattrs.blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700493 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
494 ci->i_xattrs.prealloc_blob = NULL;
495 ci->i_xattrs.dirty = false;
Sage Weil4a625be2010-08-22 15:03:56 -0700496 ci->i_xattrs.version++;
Sage Weil355da1e2009-10-06 11:31:08 -0700497 }
498}
499
500ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
501 size_t size)
502{
503 struct inode *inode = dentry->d_inode;
504 struct ceph_inode_info *ci = ceph_inode(inode);
505 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
506 int err;
507 struct ceph_inode_xattr *xattr;
508 struct ceph_vxattr_cb *vxattr = NULL;
509
510 if (!ceph_is_valid_xattr(name))
511 return -ENODATA;
512
513 /* let's see if a virtual xattr was requested */
514 if (vxattrs)
515 vxattr = ceph_match_vxattr(vxattrs, name);
516
Sage Weilbe655592011-11-30 09:47:09 -0800517 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700518 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
519 ci->i_xattrs.version, ci->i_xattrs.index_version);
520
521 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
522 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
523 goto get_xattr;
524 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800525 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700526 /* get xattrs from mds (if we don't already have them) */
527 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
528 if (err)
529 return err;
530 }
531
Sage Weilbe655592011-11-30 09:47:09 -0800532 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700533
534 if (vxattr && vxattr->readonly) {
535 err = vxattr->getxattr_cb(ci, value, size);
536 goto out;
537 }
538
539 err = __build_xattrs(inode);
540 if (err < 0)
541 goto out;
542
543get_xattr:
544 err = -ENODATA; /* == ENOATTR */
545 xattr = __get_xattr(ci, name);
546 if (!xattr) {
547 if (vxattr)
548 err = vxattr->getxattr_cb(ci, value, size);
549 goto out;
550 }
551
552 err = -ERANGE;
553 if (size && size < xattr->val_len)
554 goto out;
555
556 err = xattr->val_len;
557 if (size == 0)
558 goto out;
559
560 memcpy(value, xattr->val, xattr->val_len);
561
562out:
Sage Weilbe655592011-11-30 09:47:09 -0800563 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700564 return err;
565}
566
567ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
568{
569 struct inode *inode = dentry->d_inode;
570 struct ceph_inode_info *ci = ceph_inode(inode);
571 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
572 u32 vir_namelen = 0;
573 u32 namelen;
574 int err;
575 u32 len;
576 int i;
577
Sage Weilbe655592011-11-30 09:47:09 -0800578 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700579 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
580 ci->i_xattrs.version, ci->i_xattrs.index_version);
581
582 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
Henry C Changbddfa3c2010-04-29 09:32:28 -0700583 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
Sage Weil355da1e2009-10-06 11:31:08 -0700584 goto list_xattr;
585 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800586 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700587 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
588 if (err)
589 return err;
590 }
591
Sage Weilbe655592011-11-30 09:47:09 -0800592 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700593
594 err = __build_xattrs(inode);
595 if (err < 0)
596 goto out;
597
598list_xattr:
599 vir_namelen = 0;
600 /* include virtual dir xattrs */
601 if (vxattrs)
602 for (i = 0; vxattrs[i].name; i++)
603 vir_namelen += strlen(vxattrs[i].name) + 1;
604 /* adding 1 byte per each variable due to the null termination */
605 namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
606 err = -ERANGE;
607 if (size && namelen > size)
608 goto out;
609
610 err = namelen;
611 if (size == 0)
612 goto out;
613
614 names = __copy_xattr_names(ci, names);
615
616 /* virtual xattr names, too */
617 if (vxattrs)
618 for (i = 0; vxattrs[i].name; i++) {
619 len = sprintf(names, "%s", vxattrs[i].name);
620 names += len + 1;
621 }
622
623out:
Sage Weilbe655592011-11-30 09:47:09 -0800624 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700625 return err;
626}
627
628static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
629 const char *value, size_t size, int flags)
630{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700631 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700632 struct inode *inode = dentry->d_inode;
633 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil5f21c962011-07-26 11:30:29 -0700634 struct inode *parent_inode;
Sage Weil355da1e2009-10-06 11:31:08 -0700635 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700636 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -0700637 int err;
638 int i, nr_pages;
639 struct page **pages = NULL;
640 void *kaddr;
641
642 /* copy value into some pages */
643 nr_pages = calc_pages_for(0, size);
644 if (nr_pages) {
645 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
646 if (!pages)
647 return -ENOMEM;
648 err = -ENOMEM;
649 for (i = 0; i < nr_pages; i++) {
Yehuda Sadeh31459fe2010-03-17 13:54:02 -0700650 pages[i] = __page_cache_alloc(GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700651 if (!pages[i]) {
652 nr_pages = i;
653 goto out;
654 }
655 kaddr = kmap(pages[i]);
656 memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
657 min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
658 }
659 }
660
661 dout("setxattr value=%.*s\n", (int)size, value);
662
663 /* do request */
664 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
665 USE_AUTH_MDS);
Julia Lawall60d87732009-11-21 12:53:08 +0100666 if (IS_ERR(req)) {
667 err = PTR_ERR(req);
668 goto out;
669 }
Sage Weil70b666c2011-05-27 09:24:26 -0700670 req->r_inode = inode;
671 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700672 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
673 req->r_num_caps = 1;
674 req->r_args.setxattr.flags = cpu_to_le32(flags);
675 req->r_path2 = kstrdup(name, GFP_NOFS);
676
677 req->r_pages = pages;
678 req->r_num_pages = nr_pages;
679 req->r_data_len = size;
680
681 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
Sage Weil5f21c962011-07-26 11:30:29 -0700682 parent_inode = ceph_get_dentry_parent_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700683 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
Sage Weil5f21c962011-07-26 11:30:29 -0700684 iput(parent_inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700685 ceph_mdsc_put_request(req);
686 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
687
688out:
689 if (pages) {
690 for (i = 0; i < nr_pages; i++)
691 __free_page(pages[i]);
692 kfree(pages);
693 }
694 return err;
695}
696
697int ceph_setxattr(struct dentry *dentry, const char *name,
698 const void *value, size_t size, int flags)
699{
700 struct inode *inode = dentry->d_inode;
701 struct ceph_inode_info *ci = ceph_inode(inode);
702 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
703 int err;
704 int name_len = strlen(name);
705 int val_len = size;
706 char *newname = NULL;
707 char *newval = NULL;
708 struct ceph_inode_xattr *xattr = NULL;
709 int issued;
710 int required_blob_size;
Sage Weilfca65b42011-05-04 11:33:47 -0700711 int dirty;
Sage Weil355da1e2009-10-06 11:31:08 -0700712
713 if (ceph_snap(inode) != CEPH_NOSNAP)
714 return -EROFS;
715
716 if (!ceph_is_valid_xattr(name))
717 return -EOPNOTSUPP;
718
719 if (vxattrs) {
720 struct ceph_vxattr_cb *vxattr =
721 ceph_match_vxattr(vxattrs, name);
722 if (vxattr && vxattr->readonly)
723 return -EOPNOTSUPP;
724 }
725
726 /* preallocate memory for xattr name, value, index node */
727 err = -ENOMEM;
Julia Lawall61413c22010-10-17 21:55:21 +0200728 newname = kmemdup(name, name_len + 1, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700729 if (!newname)
730 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700731
732 if (val_len) {
Alex Elderb829c192012-01-23 15:49:27 -0600733 newval = kmemdup(value, val_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700734 if (!newval)
735 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700736 }
737
738 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
739 if (!xattr)
740 goto out;
741
Sage Weilbe655592011-11-30 09:47:09 -0800742 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700743retry:
744 issued = __ceph_caps_issued(ci, NULL);
745 if (!(issued & CEPH_CAP_XATTR_EXCL))
746 goto do_sync;
747 __build_xattrs(inode);
748
749 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
750
751 if (!ci->i_xattrs.prealloc_blob ||
752 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
753 struct ceph_buffer *blob = NULL;
754
Sage Weilbe655592011-11-30 09:47:09 -0800755 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700756 dout(" preaallocating new blob size=%d\n", required_blob_size);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800757 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700758 if (!blob)
759 goto out;
Sage Weilbe655592011-11-30 09:47:09 -0800760 spin_lock(&ci->i_ceph_lock);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800761 if (ci->i_xattrs.prealloc_blob)
762 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700763 ci->i_xattrs.prealloc_blob = blob;
764 goto retry;
765 }
766
767 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
768 err = __set_xattr(ci, newname, name_len, newval,
769 val_len, 1, 1, 1, &xattr);
Sage Weilfca65b42011-05-04 11:33:47 -0700770 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
Sage Weil355da1e2009-10-06 11:31:08 -0700771 ci->i_xattrs.dirty = true;
772 inode->i_ctime = CURRENT_TIME;
Sage Weilbe655592011-11-30 09:47:09 -0800773 spin_unlock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -0700774 if (dirty)
775 __mark_inode_dirty(inode, dirty);
Sage Weil355da1e2009-10-06 11:31:08 -0700776 return err;
777
778do_sync:
Sage Weilbe655592011-11-30 09:47:09 -0800779 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700780 err = ceph_sync_setxattr(dentry, name, value, size, flags);
781out:
782 kfree(newname);
783 kfree(newval);
784 kfree(xattr);
785 return err;
786}
787
788static int ceph_send_removexattr(struct dentry *dentry, const char *name)
789{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700790 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
791 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -0700792 struct inode *inode = dentry->d_inode;
Sage Weil5f21c962011-07-26 11:30:29 -0700793 struct inode *parent_inode;
Sage Weil355da1e2009-10-06 11:31:08 -0700794 struct ceph_mds_request *req;
795 int err;
796
797 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
798 USE_AUTH_MDS);
799 if (IS_ERR(req))
800 return PTR_ERR(req);
Sage Weil70b666c2011-05-27 09:24:26 -0700801 req->r_inode = inode;
802 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700803 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
804 req->r_num_caps = 1;
805 req->r_path2 = kstrdup(name, GFP_NOFS);
806
Sage Weil5f21c962011-07-26 11:30:29 -0700807 parent_inode = ceph_get_dentry_parent_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700808 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
Sage Weil5f21c962011-07-26 11:30:29 -0700809 iput(parent_inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700810 ceph_mdsc_put_request(req);
811 return err;
812}
813
814int ceph_removexattr(struct dentry *dentry, const char *name)
815{
816 struct inode *inode = dentry->d_inode;
817 struct ceph_inode_info *ci = ceph_inode(inode);
818 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
819 int issued;
820 int err;
Alex Elder83eb26a2012-01-11 17:41:01 -0800821 int required_blob_size;
Sage Weilfca65b42011-05-04 11:33:47 -0700822 int dirty;
Sage Weil355da1e2009-10-06 11:31:08 -0700823
824 if (ceph_snap(inode) != CEPH_NOSNAP)
825 return -EROFS;
826
827 if (!ceph_is_valid_xattr(name))
828 return -EOPNOTSUPP;
829
830 if (vxattrs) {
831 struct ceph_vxattr_cb *vxattr =
832 ceph_match_vxattr(vxattrs, name);
833 if (vxattr && vxattr->readonly)
834 return -EOPNOTSUPP;
835 }
836
Alex Elder83eb26a2012-01-11 17:41:01 -0800837 err = -ENOMEM;
Sage Weilbe655592011-11-30 09:47:09 -0800838 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700839 __build_xattrs(inode);
Alex Elder83eb26a2012-01-11 17:41:01 -0800840retry:
Sage Weil355da1e2009-10-06 11:31:08 -0700841 issued = __ceph_caps_issued(ci, NULL);
842 dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
843
844 if (!(issued & CEPH_CAP_XATTR_EXCL))
845 goto do_sync;
846
Alex Elder83eb26a2012-01-11 17:41:01 -0800847 required_blob_size = __get_required_blob_size(ci, 0, 0);
848
849 if (!ci->i_xattrs.prealloc_blob ||
850 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
851 struct ceph_buffer *blob;
852
853 spin_unlock(&ci->i_ceph_lock);
854 dout(" preaallocating new blob size=%d\n", required_blob_size);
855 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
856 if (!blob)
857 goto out;
858 spin_lock(&ci->i_ceph_lock);
859 if (ci->i_xattrs.prealloc_blob)
860 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
861 ci->i_xattrs.prealloc_blob = blob;
862 goto retry;
863 }
864
Sage Weil355da1e2009-10-06 11:31:08 -0700865 err = __remove_xattr_by_name(ceph_inode(inode), name);
Sage Weilfca65b42011-05-04 11:33:47 -0700866 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
Sage Weil355da1e2009-10-06 11:31:08 -0700867 ci->i_xattrs.dirty = true;
868 inode->i_ctime = CURRENT_TIME;
869
Sage Weilbe655592011-11-30 09:47:09 -0800870 spin_unlock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -0700871 if (dirty)
872 __mark_inode_dirty(inode, dirty);
Sage Weil355da1e2009-10-06 11:31:08 -0700873 return err;
874do_sync:
Sage Weilbe655592011-11-30 09:47:09 -0800875 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700876 err = ceph_send_removexattr(dentry, name);
Alex Elder83eb26a2012-01-11 17:41:01 -0800877out:
Sage Weil355da1e2009-10-06 11:31:08 -0700878 return err;
879}
880