blob: 05bb56f402a457036921e3bd353a9cc5d2218076 [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
Alex Elder22891902012-01-23 15:49:28 -060011#define XATTR_CEPH_PREFIX "ceph."
12#define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
13
Sage Weil355da1e2009-10-06 11:31:08 -070014static bool ceph_is_valid_xattr(const char *name)
15{
Alex Elder22891902012-01-23 15:49:28 -060016 return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
Sage Weil1a756272010-05-11 11:40:25 -070017 !strncmp(name, XATTR_SECURITY_PREFIX,
Sage Weil355da1e2009-10-06 11:31:08 -070018 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 */
27struct ceph_vxattr_cb {
28 bool readonly;
29 char *name;
30 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
31 size_t size);
32};
33
34/* directories */
35
36static size_t ceph_vxattrcb_entries(struct ceph_inode_info *ci, char *val,
37 size_t size)
38{
39 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
40}
41
42static size_t ceph_vxattrcb_files(struct ceph_inode_info *ci, char *val,
43 size_t size)
44{
45 return snprintf(val, size, "%lld", ci->i_files);
46}
47
48static size_t ceph_vxattrcb_subdirs(struct ceph_inode_info *ci, char *val,
49 size_t size)
50{
51 return snprintf(val, size, "%lld", ci->i_subdirs);
52}
53
54static size_t ceph_vxattrcb_rentries(struct ceph_inode_info *ci, char *val,
55 size_t size)
56{
57 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
58}
59
60static size_t ceph_vxattrcb_rfiles(struct ceph_inode_info *ci, char *val,
61 size_t size)
62{
63 return snprintf(val, size, "%lld", ci->i_rfiles);
64}
65
66static size_t ceph_vxattrcb_rsubdirs(struct ceph_inode_info *ci, char *val,
67 size_t size)
68{
69 return snprintf(val, size, "%lld", ci->i_rsubdirs);
70}
71
72static size_t ceph_vxattrcb_rbytes(struct ceph_inode_info *ci, char *val,
73 size_t size)
74{
75 return snprintf(val, size, "%lld", ci->i_rbytes);
76}
77
78static size_t ceph_vxattrcb_rctime(struct ceph_inode_info *ci, char *val,
79 size_t size)
80{
81 return snprintf(val, size, "%ld.%ld", (long)ci->i_rctime.tv_sec,
82 (long)ci->i_rctime.tv_nsec);
83}
84
85static struct ceph_vxattr_cb ceph_dir_vxattrs[] = {
Alex Elder22891902012-01-23 15:49:28 -060086 { true, XATTR_CEPH_PREFIX "dir.entries", ceph_vxattrcb_entries},
87 { true, XATTR_CEPH_PREFIX "dir.files", ceph_vxattrcb_files},
88 { true, XATTR_CEPH_PREFIX "dir.subdirs", ceph_vxattrcb_subdirs},
89 { true, XATTR_CEPH_PREFIX "dir.rentries", ceph_vxattrcb_rentries},
90 { true, XATTR_CEPH_PREFIX "dir.rfiles", ceph_vxattrcb_rfiles},
91 { true, XATTR_CEPH_PREFIX "dir.rsubdirs", ceph_vxattrcb_rsubdirs},
92 { true, XATTR_CEPH_PREFIX "dir.rbytes", ceph_vxattrcb_rbytes},
93 { true, XATTR_CEPH_PREFIX "dir.rctime", ceph_vxattrcb_rctime},
Sage Weil355da1e2009-10-06 11:31:08 -070094 { true, NULL, NULL }
95};
96
97/* files */
98
99static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
100 size_t size)
101{
Sage Weilb195bef2009-10-07 10:59:30 -0700102 int ret;
103
104 ret = snprintf(val, size,
Sage Weil355da1e2009-10-06 11:31:08 -0700105 "chunk_bytes=%lld\nstripe_count=%lld\nobject_size=%lld\n",
106 (unsigned long long)ceph_file_layout_su(ci->i_layout),
107 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
108 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
Sage Weilb195bef2009-10-07 10:59:30 -0700109 if (ceph_file_layout_pg_preferred(ci->i_layout))
110 ret += snprintf(val + ret, size, "preferred_osd=%lld\n",
111 (unsigned long long)ceph_file_layout_pg_preferred(
112 ci->i_layout));
113 return ret;
Sage Weil355da1e2009-10-06 11:31:08 -0700114}
115
116static struct ceph_vxattr_cb ceph_file_vxattrs[] = {
Alex Elder22891902012-01-23 15:49:28 -0600117 { true, XATTR_CEPH_PREFIX "file.layout", ceph_vxattrcb_layout},
Alex Elder114fc472012-01-11 17:41:01 -0800118 /* The following extended attribute name is deprecated */
Alex Elder22891902012-01-23 15:49:28 -0600119 { true, XATTR_CEPH_PREFIX "layout", ceph_vxattrcb_layout},
Alex Elder114fc472012-01-11 17:41:01 -0800120 { true, NULL, NULL }
Sage Weil355da1e2009-10-06 11:31:08 -0700121};
122
123static struct ceph_vxattr_cb *ceph_inode_vxattrs(struct inode *inode)
124{
125 if (S_ISDIR(inode->i_mode))
126 return ceph_dir_vxattrs;
127 else if (S_ISREG(inode->i_mode))
128 return ceph_file_vxattrs;
129 return NULL;
130}
131
Alex Elder06476a62012-01-23 15:49:27 -0600132static struct ceph_vxattr_cb *ceph_match_vxattr(struct inode *inode,
Sage Weil355da1e2009-10-06 11:31:08 -0700133 const char *name)
134{
Alex Elder06476a62012-01-23 15:49:27 -0600135 struct ceph_vxattr_cb *vxattr = ceph_inode_vxattrs(inode);
136
137 if (vxattr) {
138 while (vxattr->name) {
139 if (!strcmp(vxattr->name, name))
140 return vxattr;
141 vxattr++;
142 }
143 }
144
Sage Weil355da1e2009-10-06 11:31:08 -0700145 return NULL;
146}
147
148static int __set_xattr(struct ceph_inode_info *ci,
149 const char *name, int name_len,
150 const char *val, int val_len,
151 int dirty,
152 int should_free_name, int should_free_val,
153 struct ceph_inode_xattr **newxattr)
154{
155 struct rb_node **p;
156 struct rb_node *parent = NULL;
157 struct ceph_inode_xattr *xattr = NULL;
158 int c;
159 int new = 0;
160
161 p = &ci->i_xattrs.index.rb_node;
162 while (*p) {
163 parent = *p;
164 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
165 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
166 if (c < 0)
167 p = &(*p)->rb_left;
168 else if (c > 0)
169 p = &(*p)->rb_right;
170 else {
171 if (name_len == xattr->name_len)
172 break;
173 else if (name_len < xattr->name_len)
174 p = &(*p)->rb_left;
175 else
176 p = &(*p)->rb_right;
177 }
178 xattr = NULL;
179 }
180
181 if (!xattr) {
182 new = 1;
183 xattr = *newxattr;
184 xattr->name = name;
185 xattr->name_len = name_len;
186 xattr->should_free_name = should_free_name;
187
188 ci->i_xattrs.count++;
189 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
190 } else {
191 kfree(*newxattr);
192 *newxattr = NULL;
193 if (xattr->should_free_val)
194 kfree((void *)xattr->val);
195
196 if (should_free_name) {
197 kfree((void *)name);
198 name = xattr->name;
199 }
200 ci->i_xattrs.names_size -= xattr->name_len;
201 ci->i_xattrs.vals_size -= xattr->val_len;
202 }
Sage Weil355da1e2009-10-06 11:31:08 -0700203 ci->i_xattrs.names_size += name_len;
204 ci->i_xattrs.vals_size += val_len;
205 if (val)
206 xattr->val = val;
207 else
208 xattr->val = "";
209
210 xattr->val_len = val_len;
211 xattr->dirty = dirty;
212 xattr->should_free_val = (val && should_free_val);
213
214 if (new) {
215 rb_link_node(&xattr->node, parent, p);
216 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
217 dout("__set_xattr_val p=%p\n", p);
218 }
219
220 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
221 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
222
223 return 0;
224}
225
226static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
227 const char *name)
228{
229 struct rb_node **p;
230 struct rb_node *parent = NULL;
231 struct ceph_inode_xattr *xattr = NULL;
Sage Weil17db1432011-01-13 15:27:29 -0800232 int name_len = strlen(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700233 int c;
234
235 p = &ci->i_xattrs.index.rb_node;
236 while (*p) {
237 parent = *p;
238 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
239 c = strncmp(name, xattr->name, xattr->name_len);
Sage Weil17db1432011-01-13 15:27:29 -0800240 if (c == 0 && name_len > xattr->name_len)
241 c = 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700242 if (c < 0)
243 p = &(*p)->rb_left;
244 else if (c > 0)
245 p = &(*p)->rb_right;
246 else {
247 dout("__get_xattr %s: found %.*s\n", name,
248 xattr->val_len, xattr->val);
249 return xattr;
250 }
251 }
252
253 dout("__get_xattr %s: not found\n", name);
254
255 return NULL;
256}
257
258static void __free_xattr(struct ceph_inode_xattr *xattr)
259{
260 BUG_ON(!xattr);
261
262 if (xattr->should_free_name)
263 kfree((void *)xattr->name);
264 if (xattr->should_free_val)
265 kfree((void *)xattr->val);
266
267 kfree(xattr);
268}
269
270static int __remove_xattr(struct ceph_inode_info *ci,
271 struct ceph_inode_xattr *xattr)
272{
273 if (!xattr)
274 return -EOPNOTSUPP;
275
276 rb_erase(&xattr->node, &ci->i_xattrs.index);
277
278 if (xattr->should_free_name)
279 kfree((void *)xattr->name);
280 if (xattr->should_free_val)
281 kfree((void *)xattr->val);
282
283 ci->i_xattrs.names_size -= xattr->name_len;
284 ci->i_xattrs.vals_size -= xattr->val_len;
285 ci->i_xattrs.count--;
286 kfree(xattr);
287
288 return 0;
289}
290
291static int __remove_xattr_by_name(struct ceph_inode_info *ci,
292 const char *name)
293{
294 struct rb_node **p;
295 struct ceph_inode_xattr *xattr;
296 int err;
297
298 p = &ci->i_xattrs.index.rb_node;
299 xattr = __get_xattr(ci, name);
300 err = __remove_xattr(ci, xattr);
301 return err;
302}
303
304static char *__copy_xattr_names(struct ceph_inode_info *ci,
305 char *dest)
306{
307 struct rb_node *p;
308 struct ceph_inode_xattr *xattr = NULL;
309
310 p = rb_first(&ci->i_xattrs.index);
311 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
312
313 while (p) {
314 xattr = rb_entry(p, struct ceph_inode_xattr, node);
315 memcpy(dest, xattr->name, xattr->name_len);
316 dest[xattr->name_len] = '\0';
317
318 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
319 xattr->name_len, ci->i_xattrs.names_size);
320
321 dest += xattr->name_len + 1;
322 p = rb_next(p);
323 }
324
325 return dest;
326}
327
328void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
329{
330 struct rb_node *p, *tmp;
331 struct ceph_inode_xattr *xattr = NULL;
332
333 p = rb_first(&ci->i_xattrs.index);
334
335 dout("__ceph_destroy_xattrs p=%p\n", p);
336
337 while (p) {
338 xattr = rb_entry(p, struct ceph_inode_xattr, node);
339 tmp = p;
340 p = rb_next(tmp);
341 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
342 xattr->name_len, xattr->name);
343 rb_erase(tmp, &ci->i_xattrs.index);
344
345 __free_xattr(xattr);
346 }
347
348 ci->i_xattrs.names_size = 0;
349 ci->i_xattrs.vals_size = 0;
350 ci->i_xattrs.index_version = 0;
351 ci->i_xattrs.count = 0;
352 ci->i_xattrs.index = RB_ROOT;
353}
354
355static int __build_xattrs(struct inode *inode)
Sage Weilbe655592011-11-30 09:47:09 -0800356 __releases(ci->i_ceph_lock)
357 __acquires(ci->i_ceph_lock)
Sage Weil355da1e2009-10-06 11:31:08 -0700358{
359 u32 namelen;
360 u32 numattr = 0;
361 void *p, *end;
362 u32 len;
363 const char *name, *val;
364 struct ceph_inode_info *ci = ceph_inode(inode);
365 int xattr_version;
366 struct ceph_inode_xattr **xattrs = NULL;
Sage Weil63ff78b2009-11-01 17:51:15 -0800367 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700368 int i;
369
370 dout("__build_xattrs() len=%d\n",
371 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
372
373 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
374 return 0; /* already built */
375
376 __ceph_destroy_xattrs(ci);
377
378start:
379 /* updated internal xattr rb tree */
380 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
381 p = ci->i_xattrs.blob->vec.iov_base;
382 end = p + ci->i_xattrs.blob->vec.iov_len;
383 ceph_decode_32_safe(&p, end, numattr, bad);
384 xattr_version = ci->i_xattrs.version;
Sage Weilbe655592011-11-30 09:47:09 -0800385 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700386
387 xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
388 GFP_NOFS);
389 err = -ENOMEM;
390 if (!xattrs)
391 goto bad_lock;
392 memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
393 for (i = 0; i < numattr; i++) {
394 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
395 GFP_NOFS);
396 if (!xattrs[i])
397 goto bad_lock;
398 }
399
Sage Weilbe655592011-11-30 09:47:09 -0800400 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700401 if (ci->i_xattrs.version != xattr_version) {
402 /* lost a race, retry */
403 for (i = 0; i < numattr; i++)
404 kfree(xattrs[i]);
405 kfree(xattrs);
406 goto start;
407 }
408 err = -EIO;
409 while (numattr--) {
410 ceph_decode_32_safe(&p, end, len, bad);
411 namelen = len;
412 name = p;
413 p += len;
414 ceph_decode_32_safe(&p, end, len, bad);
415 val = p;
416 p += len;
417
418 err = __set_xattr(ci, name, namelen, val, len,
419 0, 0, 0, &xattrs[numattr]);
420
421 if (err < 0)
422 goto bad;
423 }
424 kfree(xattrs);
425 }
426 ci->i_xattrs.index_version = ci->i_xattrs.version;
427 ci->i_xattrs.dirty = false;
428
429 return err;
430bad_lock:
Sage Weilbe655592011-11-30 09:47:09 -0800431 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700432bad:
433 if (xattrs) {
434 for (i = 0; i < numattr; i++)
435 kfree(xattrs[i]);
436 kfree(xattrs);
437 }
438 ci->i_xattrs.names_size = 0;
439 return err;
440}
441
442static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
443 int val_size)
444{
445 /*
446 * 4 bytes for the length, and additional 4 bytes per each xattr name,
447 * 4 bytes per each value
448 */
449 int size = 4 + ci->i_xattrs.count*(4 + 4) +
450 ci->i_xattrs.names_size +
451 ci->i_xattrs.vals_size;
452 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
453 ci->i_xattrs.count, ci->i_xattrs.names_size,
454 ci->i_xattrs.vals_size);
455
456 if (name_size)
457 size += 4 + 4 + name_size + val_size;
458
459 return size;
460}
461
462/*
463 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
464 * and swap into place.
465 */
466void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
467{
468 struct rb_node *p;
469 struct ceph_inode_xattr *xattr = NULL;
470 void *dest;
471
472 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
473 if (ci->i_xattrs.dirty) {
474 int need = __get_required_blob_size(ci, 0, 0);
475
476 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
477
478 p = rb_first(&ci->i_xattrs.index);
479 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
480
481 ceph_encode_32(&dest, ci->i_xattrs.count);
482 while (p) {
483 xattr = rb_entry(p, struct ceph_inode_xattr, node);
484
485 ceph_encode_32(&dest, xattr->name_len);
486 memcpy(dest, xattr->name, xattr->name_len);
487 dest += xattr->name_len;
488 ceph_encode_32(&dest, xattr->val_len);
489 memcpy(dest, xattr->val, xattr->val_len);
490 dest += xattr->val_len;
491
492 p = rb_next(p);
493 }
494
495 /* adjust buffer len; it may be larger than we need */
496 ci->i_xattrs.prealloc_blob->vec.iov_len =
497 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
498
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800499 if (ci->i_xattrs.blob)
500 ceph_buffer_put(ci->i_xattrs.blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700501 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
502 ci->i_xattrs.prealloc_blob = NULL;
503 ci->i_xattrs.dirty = false;
Sage Weil4a625be2010-08-22 15:03:56 -0700504 ci->i_xattrs.version++;
Sage Weil355da1e2009-10-06 11:31:08 -0700505 }
506}
507
508ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
509 size_t size)
510{
511 struct inode *inode = dentry->d_inode;
512 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700513 int err;
514 struct ceph_inode_xattr *xattr;
515 struct ceph_vxattr_cb *vxattr = NULL;
516
517 if (!ceph_is_valid_xattr(name))
518 return -ENODATA;
519
520 /* let's see if a virtual xattr was requested */
Alex Elder06476a62012-01-23 15:49:27 -0600521 vxattr = ceph_match_vxattr(inode, name);
Sage Weil355da1e2009-10-06 11:31:08 -0700522
Sage Weilbe655592011-11-30 09:47:09 -0800523 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700524 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
525 ci->i_xattrs.version, ci->i_xattrs.index_version);
526
527 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
528 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
529 goto get_xattr;
530 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800531 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700532 /* get xattrs from mds (if we don't already have them) */
533 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
534 if (err)
535 return err;
536 }
537
Sage Weilbe655592011-11-30 09:47:09 -0800538 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700539
540 if (vxattr && vxattr->readonly) {
541 err = vxattr->getxattr_cb(ci, value, size);
542 goto out;
543 }
544
545 err = __build_xattrs(inode);
546 if (err < 0)
547 goto out;
548
549get_xattr:
550 err = -ENODATA; /* == ENOATTR */
551 xattr = __get_xattr(ci, name);
552 if (!xattr) {
553 if (vxattr)
554 err = vxattr->getxattr_cb(ci, value, size);
555 goto out;
556 }
557
558 err = -ERANGE;
559 if (size && size < xattr->val_len)
560 goto out;
561
562 err = xattr->val_len;
563 if (size == 0)
564 goto out;
565
566 memcpy(value, xattr->val, xattr->val_len);
567
568out:
Sage Weilbe655592011-11-30 09:47:09 -0800569 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700570 return err;
571}
572
573ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
574{
575 struct inode *inode = dentry->d_inode;
576 struct ceph_inode_info *ci = ceph_inode(inode);
577 struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
578 u32 vir_namelen = 0;
579 u32 namelen;
580 int err;
581 u32 len;
582 int i;
583
Sage Weilbe655592011-11-30 09:47:09 -0800584 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700585 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
586 ci->i_xattrs.version, ci->i_xattrs.index_version);
587
588 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
Henry C Changbddfa3c2010-04-29 09:32:28 -0700589 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
Sage Weil355da1e2009-10-06 11:31:08 -0700590 goto list_xattr;
591 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800592 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700593 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
594 if (err)
595 return err;
596 }
597
Sage Weilbe655592011-11-30 09:47:09 -0800598 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700599
600 err = __build_xattrs(inode);
601 if (err < 0)
602 goto out;
603
604list_xattr:
605 vir_namelen = 0;
606 /* include virtual dir xattrs */
607 if (vxattrs)
608 for (i = 0; vxattrs[i].name; i++)
609 vir_namelen += strlen(vxattrs[i].name) + 1;
610 /* adding 1 byte per each variable due to the null termination */
611 namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
612 err = -ERANGE;
613 if (size && namelen > size)
614 goto out;
615
616 err = namelen;
617 if (size == 0)
618 goto out;
619
620 names = __copy_xattr_names(ci, names);
621
622 /* virtual xattr names, too */
623 if (vxattrs)
624 for (i = 0; vxattrs[i].name; i++) {
625 len = sprintf(names, "%s", vxattrs[i].name);
626 names += len + 1;
627 }
628
629out:
Sage Weilbe655592011-11-30 09:47:09 -0800630 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700631 return err;
632}
633
634static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
635 const char *value, size_t size, int flags)
636{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700637 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700638 struct inode *inode = dentry->d_inode;
639 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil5f21c962011-07-26 11:30:29 -0700640 struct inode *parent_inode;
Sage Weil355da1e2009-10-06 11:31:08 -0700641 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700642 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -0700643 int err;
644 int i, nr_pages;
645 struct page **pages = NULL;
646 void *kaddr;
647
648 /* copy value into some pages */
649 nr_pages = calc_pages_for(0, size);
650 if (nr_pages) {
651 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
652 if (!pages)
653 return -ENOMEM;
654 err = -ENOMEM;
655 for (i = 0; i < nr_pages; i++) {
Yehuda Sadeh31459fe2010-03-17 13:54:02 -0700656 pages[i] = __page_cache_alloc(GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700657 if (!pages[i]) {
658 nr_pages = i;
659 goto out;
660 }
661 kaddr = kmap(pages[i]);
662 memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
663 min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
664 }
665 }
666
667 dout("setxattr value=%.*s\n", (int)size, value);
668
669 /* do request */
670 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
671 USE_AUTH_MDS);
Julia Lawall60d87732009-11-21 12:53:08 +0100672 if (IS_ERR(req)) {
673 err = PTR_ERR(req);
674 goto out;
675 }
Sage Weil70b666c2011-05-27 09:24:26 -0700676 req->r_inode = inode;
677 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700678 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
679 req->r_num_caps = 1;
680 req->r_args.setxattr.flags = cpu_to_le32(flags);
681 req->r_path2 = kstrdup(name, GFP_NOFS);
682
683 req->r_pages = pages;
684 req->r_num_pages = nr_pages;
685 req->r_data_len = size;
686
687 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
Sage Weil5f21c962011-07-26 11:30:29 -0700688 parent_inode = ceph_get_dentry_parent_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700689 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
Sage Weil5f21c962011-07-26 11:30:29 -0700690 iput(parent_inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700691 ceph_mdsc_put_request(req);
692 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
693
694out:
695 if (pages) {
696 for (i = 0; i < nr_pages; i++)
697 __free_page(pages[i]);
698 kfree(pages);
699 }
700 return err;
701}
702
703int ceph_setxattr(struct dentry *dentry, const char *name,
704 const void *value, size_t size, int flags)
705{
706 struct inode *inode = dentry->d_inode;
Alex Elder06476a62012-01-23 15:49:27 -0600707 struct ceph_vxattr_cb *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700708 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700709 int err;
710 int name_len = strlen(name);
711 int val_len = size;
712 char *newname = NULL;
713 char *newval = NULL;
714 struct ceph_inode_xattr *xattr = NULL;
715 int issued;
716 int required_blob_size;
Sage Weilfca65b42011-05-04 11:33:47 -0700717 int dirty;
Sage Weil355da1e2009-10-06 11:31:08 -0700718
719 if (ceph_snap(inode) != CEPH_NOSNAP)
720 return -EROFS;
721
722 if (!ceph_is_valid_xattr(name))
723 return -EOPNOTSUPP;
724
Alex Elder06476a62012-01-23 15:49:27 -0600725 vxattr = ceph_match_vxattr(inode, name);
726 if (vxattr && vxattr->readonly)
727 return -EOPNOTSUPP;
Sage Weil355da1e2009-10-06 11:31:08 -0700728
729 /* preallocate memory for xattr name, value, index node */
730 err = -ENOMEM;
Julia Lawall61413c22010-10-17 21:55:21 +0200731 newname = kmemdup(name, name_len + 1, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700732 if (!newname)
733 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700734
735 if (val_len) {
Alex Elderb829c192012-01-23 15:49:27 -0600736 newval = kmemdup(value, val_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700737 if (!newval)
738 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700739 }
740
741 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
742 if (!xattr)
743 goto out;
744
Sage Weilbe655592011-11-30 09:47:09 -0800745 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700746retry:
747 issued = __ceph_caps_issued(ci, NULL);
748 if (!(issued & CEPH_CAP_XATTR_EXCL))
749 goto do_sync;
750 __build_xattrs(inode);
751
752 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
753
754 if (!ci->i_xattrs.prealloc_blob ||
755 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
756 struct ceph_buffer *blob = NULL;
757
Sage Weilbe655592011-11-30 09:47:09 -0800758 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700759 dout(" preaallocating new blob size=%d\n", required_blob_size);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800760 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700761 if (!blob)
762 goto out;
Sage Weilbe655592011-11-30 09:47:09 -0800763 spin_lock(&ci->i_ceph_lock);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800764 if (ci->i_xattrs.prealloc_blob)
765 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700766 ci->i_xattrs.prealloc_blob = blob;
767 goto retry;
768 }
769
770 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
771 err = __set_xattr(ci, newname, name_len, newval,
772 val_len, 1, 1, 1, &xattr);
Sage Weilfca65b42011-05-04 11:33:47 -0700773 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
Sage Weil355da1e2009-10-06 11:31:08 -0700774 ci->i_xattrs.dirty = true;
775 inode->i_ctime = CURRENT_TIME;
Sage Weilbe655592011-11-30 09:47:09 -0800776 spin_unlock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -0700777 if (dirty)
778 __mark_inode_dirty(inode, dirty);
Sage Weil355da1e2009-10-06 11:31:08 -0700779 return err;
780
781do_sync:
Sage Weilbe655592011-11-30 09:47:09 -0800782 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700783 err = ceph_sync_setxattr(dentry, name, value, size, flags);
784out:
785 kfree(newname);
786 kfree(newval);
787 kfree(xattr);
788 return err;
789}
790
791static int ceph_send_removexattr(struct dentry *dentry, const char *name)
792{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700793 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
794 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -0700795 struct inode *inode = dentry->d_inode;
Sage Weil5f21c962011-07-26 11:30:29 -0700796 struct inode *parent_inode;
Sage Weil355da1e2009-10-06 11:31:08 -0700797 struct ceph_mds_request *req;
798 int err;
799
800 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
801 USE_AUTH_MDS);
802 if (IS_ERR(req))
803 return PTR_ERR(req);
Sage Weil70b666c2011-05-27 09:24:26 -0700804 req->r_inode = inode;
805 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700806 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
807 req->r_num_caps = 1;
808 req->r_path2 = kstrdup(name, GFP_NOFS);
809
Sage Weil5f21c962011-07-26 11:30:29 -0700810 parent_inode = ceph_get_dentry_parent_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700811 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
Sage Weil5f21c962011-07-26 11:30:29 -0700812 iput(parent_inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700813 ceph_mdsc_put_request(req);
814 return err;
815}
816
817int ceph_removexattr(struct dentry *dentry, const char *name)
818{
819 struct inode *inode = dentry->d_inode;
Alex Elder06476a62012-01-23 15:49:27 -0600820 struct ceph_vxattr_cb *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700821 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700822 int issued;
823 int err;
Alex Elder83eb26a2012-01-11 17:41:01 -0800824 int required_blob_size;
Sage Weilfca65b42011-05-04 11:33:47 -0700825 int dirty;
Sage Weil355da1e2009-10-06 11:31:08 -0700826
827 if (ceph_snap(inode) != CEPH_NOSNAP)
828 return -EROFS;
829
830 if (!ceph_is_valid_xattr(name))
831 return -EOPNOTSUPP;
832
Alex Elder06476a62012-01-23 15:49:27 -0600833 vxattr = ceph_match_vxattr(inode, name);
834 if (vxattr && vxattr->readonly)
835 return -EOPNOTSUPP;
Sage Weil355da1e2009-10-06 11:31:08 -0700836
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