blob: 8294f461ecd1f3ee6e867bbf5191277f95111a16 [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 */
Alex Elder881a5fa2012-01-23 15:49:28 -060027struct ceph_vxattr {
Sage Weil355da1e2009-10-06 11:31:08 -070028 char *name;
Alex Elder3ce6cd12012-01-23 15:49:28 -060029 size_t name_size; /* strlen(name) + 1 (for '\0') */
Sage Weil355da1e2009-10-06 11:31:08 -070030 size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
31 size_t size);
Alex Eldereb788082012-01-23 15:49:28 -060032 bool readonly;
Sage Weil355da1e2009-10-06 11:31:08 -070033};
34
35/* directories */
36
Alex Elderaa4066e2012-01-23 15:49:28 -060037static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -070038 size_t size)
39{
40 return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
41}
42
Alex Elderaa4066e2012-01-23 15:49:28 -060043static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -070044 size_t size)
45{
46 return snprintf(val, size, "%lld", ci->i_files);
47}
48
Alex Elderaa4066e2012-01-23 15:49:28 -060049static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -070050 size_t size)
51{
52 return snprintf(val, size, "%lld", ci->i_subdirs);
53}
54
Alex Elderaa4066e2012-01-23 15:49:28 -060055static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -070056 size_t size)
57{
58 return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
59}
60
Alex Elderaa4066e2012-01-23 15:49:28 -060061static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -070062 size_t size)
63{
64 return snprintf(val, size, "%lld", ci->i_rfiles);
65}
66
Alex Elderaa4066e2012-01-23 15:49:28 -060067static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -070068 size_t size)
69{
70 return snprintf(val, size, "%lld", ci->i_rsubdirs);
71}
72
Alex Elderaa4066e2012-01-23 15:49:28 -060073static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -070074 size_t size)
75{
76 return snprintf(val, size, "%lld", ci->i_rbytes);
77}
78
Alex Elderaa4066e2012-01-23 15:49:28 -060079static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -070080 size_t size)
81{
82 return snprintf(val, size, "%ld.%ld", (long)ci->i_rctime.tv_sec,
83 (long)ci->i_rctime.tv_nsec);
84}
85
Alex Eldereb788082012-01-23 15:49:28 -060086#define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
87
88#define XATTR_NAME_CEPH(_type, _name) \
89 { \
90 .name = CEPH_XATTR_NAME(_type, _name), \
Alex Elder3ce6cd12012-01-23 15:49:28 -060091 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
Alex Elderaa4066e2012-01-23 15:49:28 -060092 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
Alex Eldereb788082012-01-23 15:49:28 -060093 .readonly = true, \
94 }
95
Alex Elder881a5fa2012-01-23 15:49:28 -060096static struct ceph_vxattr ceph_dir_vxattrs[] = {
Alex Eldereb788082012-01-23 15:49:28 -060097 XATTR_NAME_CEPH(dir, entries),
98 XATTR_NAME_CEPH(dir, files),
99 XATTR_NAME_CEPH(dir, subdirs),
100 XATTR_NAME_CEPH(dir, rentries),
101 XATTR_NAME_CEPH(dir, rfiles),
102 XATTR_NAME_CEPH(dir, rsubdirs),
103 XATTR_NAME_CEPH(dir, rbytes),
104 XATTR_NAME_CEPH(dir, rctime),
105 { 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700106};
Alex Elder3ce6cd12012-01-23 15:49:28 -0600107static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
Sage Weil355da1e2009-10-06 11:31:08 -0700108
109/* files */
110
Alex Elderaa4066e2012-01-23 15:49:28 -0600111static size_t ceph_vxattrcb_file_layout(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700112 size_t size)
113{
Sage Weilb195bef2009-10-07 10:59:30 -0700114 int ret;
115
116 ret = snprintf(val, size,
Sage Weil355da1e2009-10-06 11:31:08 -0700117 "chunk_bytes=%lld\nstripe_count=%lld\nobject_size=%lld\n",
118 (unsigned long long)ceph_file_layout_su(ci->i_layout),
119 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
120 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
Sage Weilb195bef2009-10-07 10:59:30 -0700121 if (ceph_file_layout_pg_preferred(ci->i_layout))
122 ret += snprintf(val + ret, size, "preferred_osd=%lld\n",
123 (unsigned long long)ceph_file_layout_pg_preferred(
124 ci->i_layout));
125 return ret;
Sage Weil355da1e2009-10-06 11:31:08 -0700126}
127
Alex Elder881a5fa2012-01-23 15:49:28 -0600128static struct ceph_vxattr ceph_file_vxattrs[] = {
Alex Eldereb788082012-01-23 15:49:28 -0600129 XATTR_NAME_CEPH(file, layout),
Alex Elder114fc472012-01-11 17:41:01 -0800130 /* The following extended attribute name is deprecated */
Alex Eldereb788082012-01-23 15:49:28 -0600131 {
132 .name = XATTR_CEPH_PREFIX "layout",
Alex Elder3ce6cd12012-01-23 15:49:28 -0600133 .name_size = sizeof (XATTR_CEPH_PREFIX "layout"),
Alex Elderaa4066e2012-01-23 15:49:28 -0600134 .getxattr_cb = ceph_vxattrcb_file_layout,
Alex Eldereb788082012-01-23 15:49:28 -0600135 .readonly = true,
136 },
137 { 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700138};
Alex Elder3ce6cd12012-01-23 15:49:28 -0600139static size_t ceph_file_vxattrs_name_size; /* total size of all names */
Sage Weil355da1e2009-10-06 11:31:08 -0700140
Alex Elder881a5fa2012-01-23 15:49:28 -0600141static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
Sage Weil355da1e2009-10-06 11:31:08 -0700142{
143 if (S_ISDIR(inode->i_mode))
144 return ceph_dir_vxattrs;
145 else if (S_ISREG(inode->i_mode))
146 return ceph_file_vxattrs;
147 return NULL;
148}
149
Alex Elder3ce6cd12012-01-23 15:49:28 -0600150static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
151{
152 if (vxattrs == ceph_dir_vxattrs)
153 return ceph_dir_vxattrs_name_size;
154 if (vxattrs == ceph_file_vxattrs)
155 return ceph_file_vxattrs_name_size;
156 BUG();
157
158 return 0;
159}
160
161/*
162 * Compute the aggregate size (including terminating '\0') of all
163 * virtual extended attribute names in the given vxattr table.
164 */
165static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
166{
167 struct ceph_vxattr *vxattr;
168 size_t size = 0;
169
170 for (vxattr = vxattrs; vxattr->name; vxattr++)
171 size += vxattr->name_size;
172
173 return size;
174}
175
176/* Routines called at initialization and exit time */
177
178void __init ceph_xattr_init(void)
179{
180 ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
181 ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
182}
183
184void ceph_xattr_exit(void)
185{
186 ceph_dir_vxattrs_name_size = 0;
187 ceph_file_vxattrs_name_size = 0;
188}
189
Alex Elder881a5fa2012-01-23 15:49:28 -0600190static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
Sage Weil355da1e2009-10-06 11:31:08 -0700191 const char *name)
192{
Alex Elder881a5fa2012-01-23 15:49:28 -0600193 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
Alex Elder06476a62012-01-23 15:49:27 -0600194
195 if (vxattr) {
196 while (vxattr->name) {
197 if (!strcmp(vxattr->name, name))
198 return vxattr;
199 vxattr++;
200 }
201 }
202
Sage Weil355da1e2009-10-06 11:31:08 -0700203 return NULL;
204}
205
206static int __set_xattr(struct ceph_inode_info *ci,
207 const char *name, int name_len,
208 const char *val, int val_len,
209 int dirty,
210 int should_free_name, int should_free_val,
211 struct ceph_inode_xattr **newxattr)
212{
213 struct rb_node **p;
214 struct rb_node *parent = NULL;
215 struct ceph_inode_xattr *xattr = NULL;
216 int c;
217 int new = 0;
218
219 p = &ci->i_xattrs.index.rb_node;
220 while (*p) {
221 parent = *p;
222 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
223 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
224 if (c < 0)
225 p = &(*p)->rb_left;
226 else if (c > 0)
227 p = &(*p)->rb_right;
228 else {
229 if (name_len == xattr->name_len)
230 break;
231 else if (name_len < xattr->name_len)
232 p = &(*p)->rb_left;
233 else
234 p = &(*p)->rb_right;
235 }
236 xattr = NULL;
237 }
238
239 if (!xattr) {
240 new = 1;
241 xattr = *newxattr;
242 xattr->name = name;
243 xattr->name_len = name_len;
244 xattr->should_free_name = should_free_name;
245
246 ci->i_xattrs.count++;
247 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
248 } else {
249 kfree(*newxattr);
250 *newxattr = NULL;
251 if (xattr->should_free_val)
252 kfree((void *)xattr->val);
253
254 if (should_free_name) {
255 kfree((void *)name);
256 name = xattr->name;
257 }
258 ci->i_xattrs.names_size -= xattr->name_len;
259 ci->i_xattrs.vals_size -= xattr->val_len;
260 }
Sage Weil355da1e2009-10-06 11:31:08 -0700261 ci->i_xattrs.names_size += name_len;
262 ci->i_xattrs.vals_size += val_len;
263 if (val)
264 xattr->val = val;
265 else
266 xattr->val = "";
267
268 xattr->val_len = val_len;
269 xattr->dirty = dirty;
270 xattr->should_free_val = (val && should_free_val);
271
272 if (new) {
273 rb_link_node(&xattr->node, parent, p);
274 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
275 dout("__set_xattr_val p=%p\n", p);
276 }
277
278 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
279 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
280
281 return 0;
282}
283
284static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
285 const char *name)
286{
287 struct rb_node **p;
288 struct rb_node *parent = NULL;
289 struct ceph_inode_xattr *xattr = NULL;
Sage Weil17db1432011-01-13 15:27:29 -0800290 int name_len = strlen(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700291 int c;
292
293 p = &ci->i_xattrs.index.rb_node;
294 while (*p) {
295 parent = *p;
296 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
297 c = strncmp(name, xattr->name, xattr->name_len);
Sage Weil17db1432011-01-13 15:27:29 -0800298 if (c == 0 && name_len > xattr->name_len)
299 c = 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700300 if (c < 0)
301 p = &(*p)->rb_left;
302 else if (c > 0)
303 p = &(*p)->rb_right;
304 else {
305 dout("__get_xattr %s: found %.*s\n", name,
306 xattr->val_len, xattr->val);
307 return xattr;
308 }
309 }
310
311 dout("__get_xattr %s: not found\n", name);
312
313 return NULL;
314}
315
316static void __free_xattr(struct ceph_inode_xattr *xattr)
317{
318 BUG_ON(!xattr);
319
320 if (xattr->should_free_name)
321 kfree((void *)xattr->name);
322 if (xattr->should_free_val)
323 kfree((void *)xattr->val);
324
325 kfree(xattr);
326}
327
328static int __remove_xattr(struct ceph_inode_info *ci,
329 struct ceph_inode_xattr *xattr)
330{
331 if (!xattr)
332 return -EOPNOTSUPP;
333
334 rb_erase(&xattr->node, &ci->i_xattrs.index);
335
336 if (xattr->should_free_name)
337 kfree((void *)xattr->name);
338 if (xattr->should_free_val)
339 kfree((void *)xattr->val);
340
341 ci->i_xattrs.names_size -= xattr->name_len;
342 ci->i_xattrs.vals_size -= xattr->val_len;
343 ci->i_xattrs.count--;
344 kfree(xattr);
345
346 return 0;
347}
348
349static int __remove_xattr_by_name(struct ceph_inode_info *ci,
350 const char *name)
351{
352 struct rb_node **p;
353 struct ceph_inode_xattr *xattr;
354 int err;
355
356 p = &ci->i_xattrs.index.rb_node;
357 xattr = __get_xattr(ci, name);
358 err = __remove_xattr(ci, xattr);
359 return err;
360}
361
362static char *__copy_xattr_names(struct ceph_inode_info *ci,
363 char *dest)
364{
365 struct rb_node *p;
366 struct ceph_inode_xattr *xattr = NULL;
367
368 p = rb_first(&ci->i_xattrs.index);
369 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
370
371 while (p) {
372 xattr = rb_entry(p, struct ceph_inode_xattr, node);
373 memcpy(dest, xattr->name, xattr->name_len);
374 dest[xattr->name_len] = '\0';
375
376 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
377 xattr->name_len, ci->i_xattrs.names_size);
378
379 dest += xattr->name_len + 1;
380 p = rb_next(p);
381 }
382
383 return dest;
384}
385
386void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
387{
388 struct rb_node *p, *tmp;
389 struct ceph_inode_xattr *xattr = NULL;
390
391 p = rb_first(&ci->i_xattrs.index);
392
393 dout("__ceph_destroy_xattrs p=%p\n", p);
394
395 while (p) {
396 xattr = rb_entry(p, struct ceph_inode_xattr, node);
397 tmp = p;
398 p = rb_next(tmp);
399 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
400 xattr->name_len, xattr->name);
401 rb_erase(tmp, &ci->i_xattrs.index);
402
403 __free_xattr(xattr);
404 }
405
406 ci->i_xattrs.names_size = 0;
407 ci->i_xattrs.vals_size = 0;
408 ci->i_xattrs.index_version = 0;
409 ci->i_xattrs.count = 0;
410 ci->i_xattrs.index = RB_ROOT;
411}
412
413static int __build_xattrs(struct inode *inode)
Sage Weilbe655592011-11-30 09:47:09 -0800414 __releases(ci->i_ceph_lock)
415 __acquires(ci->i_ceph_lock)
Sage Weil355da1e2009-10-06 11:31:08 -0700416{
417 u32 namelen;
418 u32 numattr = 0;
419 void *p, *end;
420 u32 len;
421 const char *name, *val;
422 struct ceph_inode_info *ci = ceph_inode(inode);
423 int xattr_version;
424 struct ceph_inode_xattr **xattrs = NULL;
Sage Weil63ff78b2009-11-01 17:51:15 -0800425 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700426 int i;
427
428 dout("__build_xattrs() len=%d\n",
429 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
430
431 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
432 return 0; /* already built */
433
434 __ceph_destroy_xattrs(ci);
435
436start:
437 /* updated internal xattr rb tree */
438 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
439 p = ci->i_xattrs.blob->vec.iov_base;
440 end = p + ci->i_xattrs.blob->vec.iov_len;
441 ceph_decode_32_safe(&p, end, numattr, bad);
442 xattr_version = ci->i_xattrs.version;
Sage Weilbe655592011-11-30 09:47:09 -0800443 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700444
445 xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
446 GFP_NOFS);
447 err = -ENOMEM;
448 if (!xattrs)
449 goto bad_lock;
450 memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
451 for (i = 0; i < numattr; i++) {
452 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
453 GFP_NOFS);
454 if (!xattrs[i])
455 goto bad_lock;
456 }
457
Sage Weilbe655592011-11-30 09:47:09 -0800458 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700459 if (ci->i_xattrs.version != xattr_version) {
460 /* lost a race, retry */
461 for (i = 0; i < numattr; i++)
462 kfree(xattrs[i]);
463 kfree(xattrs);
464 goto start;
465 }
466 err = -EIO;
467 while (numattr--) {
468 ceph_decode_32_safe(&p, end, len, bad);
469 namelen = len;
470 name = p;
471 p += len;
472 ceph_decode_32_safe(&p, end, len, bad);
473 val = p;
474 p += len;
475
476 err = __set_xattr(ci, name, namelen, val, len,
477 0, 0, 0, &xattrs[numattr]);
478
479 if (err < 0)
480 goto bad;
481 }
482 kfree(xattrs);
483 }
484 ci->i_xattrs.index_version = ci->i_xattrs.version;
485 ci->i_xattrs.dirty = false;
486
487 return err;
488bad_lock:
Sage Weilbe655592011-11-30 09:47:09 -0800489 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700490bad:
491 if (xattrs) {
492 for (i = 0; i < numattr; i++)
493 kfree(xattrs[i]);
494 kfree(xattrs);
495 }
496 ci->i_xattrs.names_size = 0;
497 return err;
498}
499
500static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
501 int val_size)
502{
503 /*
504 * 4 bytes for the length, and additional 4 bytes per each xattr name,
505 * 4 bytes per each value
506 */
507 int size = 4 + ci->i_xattrs.count*(4 + 4) +
508 ci->i_xattrs.names_size +
509 ci->i_xattrs.vals_size;
510 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
511 ci->i_xattrs.count, ci->i_xattrs.names_size,
512 ci->i_xattrs.vals_size);
513
514 if (name_size)
515 size += 4 + 4 + name_size + val_size;
516
517 return size;
518}
519
520/*
521 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
522 * and swap into place.
523 */
524void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
525{
526 struct rb_node *p;
527 struct ceph_inode_xattr *xattr = NULL;
528 void *dest;
529
530 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
531 if (ci->i_xattrs.dirty) {
532 int need = __get_required_blob_size(ci, 0, 0);
533
534 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
535
536 p = rb_first(&ci->i_xattrs.index);
537 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
538
539 ceph_encode_32(&dest, ci->i_xattrs.count);
540 while (p) {
541 xattr = rb_entry(p, struct ceph_inode_xattr, node);
542
543 ceph_encode_32(&dest, xattr->name_len);
544 memcpy(dest, xattr->name, xattr->name_len);
545 dest += xattr->name_len;
546 ceph_encode_32(&dest, xattr->val_len);
547 memcpy(dest, xattr->val, xattr->val_len);
548 dest += xattr->val_len;
549
550 p = rb_next(p);
551 }
552
553 /* adjust buffer len; it may be larger than we need */
554 ci->i_xattrs.prealloc_blob->vec.iov_len =
555 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
556
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800557 if (ci->i_xattrs.blob)
558 ceph_buffer_put(ci->i_xattrs.blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700559 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
560 ci->i_xattrs.prealloc_blob = NULL;
561 ci->i_xattrs.dirty = false;
Sage Weil4a625be2010-08-22 15:03:56 -0700562 ci->i_xattrs.version++;
Sage Weil355da1e2009-10-06 11:31:08 -0700563 }
564}
565
566ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
567 size_t size)
568{
569 struct inode *inode = dentry->d_inode;
570 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700571 int err;
572 struct ceph_inode_xattr *xattr;
Alex Elder881a5fa2012-01-23 15:49:28 -0600573 struct ceph_vxattr *vxattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700574
575 if (!ceph_is_valid_xattr(name))
576 return -ENODATA;
577
578 /* let's see if a virtual xattr was requested */
Alex Elder06476a62012-01-23 15:49:27 -0600579 vxattr = ceph_match_vxattr(inode, name);
Sage Weil355da1e2009-10-06 11:31:08 -0700580
Sage Weilbe655592011-11-30 09:47:09 -0800581 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700582 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
583 ci->i_xattrs.version, ci->i_xattrs.index_version);
584
585 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
586 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
587 goto get_xattr;
588 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800589 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700590 /* get xattrs from mds (if we don't already have them) */
591 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
592 if (err)
593 return err;
594 }
595
Sage Weilbe655592011-11-30 09:47:09 -0800596 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700597
598 if (vxattr && vxattr->readonly) {
599 err = vxattr->getxattr_cb(ci, value, size);
600 goto out;
601 }
602
603 err = __build_xattrs(inode);
604 if (err < 0)
605 goto out;
606
607get_xattr:
608 err = -ENODATA; /* == ENOATTR */
609 xattr = __get_xattr(ci, name);
610 if (!xattr) {
611 if (vxattr)
612 err = vxattr->getxattr_cb(ci, value, size);
613 goto out;
614 }
615
616 err = -ERANGE;
617 if (size && size < xattr->val_len)
618 goto out;
619
620 err = xattr->val_len;
621 if (size == 0)
622 goto out;
623
624 memcpy(value, xattr->val, xattr->val_len);
625
626out:
Sage Weilbe655592011-11-30 09:47:09 -0800627 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700628 return err;
629}
630
631ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
632{
633 struct inode *inode = dentry->d_inode;
634 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder881a5fa2012-01-23 15:49:28 -0600635 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700636 u32 vir_namelen = 0;
637 u32 namelen;
638 int err;
639 u32 len;
640 int i;
641
Sage Weilbe655592011-11-30 09:47:09 -0800642 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700643 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
644 ci->i_xattrs.version, ci->i_xattrs.index_version);
645
646 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
Henry C Changbddfa3c2010-04-29 09:32:28 -0700647 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
Sage Weil355da1e2009-10-06 11:31:08 -0700648 goto list_xattr;
649 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800650 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700651 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
652 if (err)
653 return err;
654 }
655
Sage Weilbe655592011-11-30 09:47:09 -0800656 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700657
658 err = __build_xattrs(inode);
659 if (err < 0)
660 goto out;
661
662list_xattr:
Alex Elder3ce6cd12012-01-23 15:49:28 -0600663 /*
664 * Start with virtual dir xattr names (if any) (including
665 * terminating '\0' characters for each).
666 */
667 vir_namelen = ceph_vxattrs_name_size(vxattrs);
668
Sage Weil355da1e2009-10-06 11:31:08 -0700669 /* adding 1 byte per each variable due to the null termination */
670 namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
671 err = -ERANGE;
672 if (size && namelen > size)
673 goto out;
674
675 err = namelen;
676 if (size == 0)
677 goto out;
678
679 names = __copy_xattr_names(ci, names);
680
681 /* virtual xattr names, too */
682 if (vxattrs)
683 for (i = 0; vxattrs[i].name; i++) {
684 len = sprintf(names, "%s", vxattrs[i].name);
685 names += len + 1;
686 }
687
688out:
Sage Weilbe655592011-11-30 09:47:09 -0800689 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700690 return err;
691}
692
693static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
694 const char *value, size_t size, int flags)
695{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700696 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700697 struct inode *inode = dentry->d_inode;
698 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil5f21c962011-07-26 11:30:29 -0700699 struct inode *parent_inode;
Sage Weil355da1e2009-10-06 11:31:08 -0700700 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700701 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -0700702 int err;
703 int i, nr_pages;
704 struct page **pages = NULL;
705 void *kaddr;
706
707 /* copy value into some pages */
708 nr_pages = calc_pages_for(0, size);
709 if (nr_pages) {
710 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
711 if (!pages)
712 return -ENOMEM;
713 err = -ENOMEM;
714 for (i = 0; i < nr_pages; i++) {
Yehuda Sadeh31459fe2010-03-17 13:54:02 -0700715 pages[i] = __page_cache_alloc(GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700716 if (!pages[i]) {
717 nr_pages = i;
718 goto out;
719 }
720 kaddr = kmap(pages[i]);
721 memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
722 min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
723 }
724 }
725
726 dout("setxattr value=%.*s\n", (int)size, value);
727
728 /* do request */
729 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
730 USE_AUTH_MDS);
Julia Lawall60d87732009-11-21 12:53:08 +0100731 if (IS_ERR(req)) {
732 err = PTR_ERR(req);
733 goto out;
734 }
Sage Weil70b666c2011-05-27 09:24:26 -0700735 req->r_inode = inode;
736 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700737 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
738 req->r_num_caps = 1;
739 req->r_args.setxattr.flags = cpu_to_le32(flags);
740 req->r_path2 = kstrdup(name, GFP_NOFS);
741
742 req->r_pages = pages;
743 req->r_num_pages = nr_pages;
744 req->r_data_len = size;
745
746 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
Sage Weil5f21c962011-07-26 11:30:29 -0700747 parent_inode = ceph_get_dentry_parent_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700748 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
Sage Weil5f21c962011-07-26 11:30:29 -0700749 iput(parent_inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700750 ceph_mdsc_put_request(req);
751 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
752
753out:
754 if (pages) {
755 for (i = 0; i < nr_pages; i++)
756 __free_page(pages[i]);
757 kfree(pages);
758 }
759 return err;
760}
761
762int ceph_setxattr(struct dentry *dentry, const char *name,
763 const void *value, size_t size, int flags)
764{
765 struct inode *inode = dentry->d_inode;
Alex Elder881a5fa2012-01-23 15:49:28 -0600766 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700767 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder18fa8b32012-01-23 15:49:28 -0600768 int issued;
Sage Weil355da1e2009-10-06 11:31:08 -0700769 int err;
Alex Elder18fa8b32012-01-23 15:49:28 -0600770 int dirty;
Sage Weil355da1e2009-10-06 11:31:08 -0700771 int name_len = strlen(name);
772 int val_len = size;
773 char *newname = NULL;
774 char *newval = NULL;
775 struct ceph_inode_xattr *xattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700776 int required_blob_size;
777
778 if (ceph_snap(inode) != CEPH_NOSNAP)
779 return -EROFS;
780
781 if (!ceph_is_valid_xattr(name))
782 return -EOPNOTSUPP;
783
Alex Elder06476a62012-01-23 15:49:27 -0600784 vxattr = ceph_match_vxattr(inode, name);
785 if (vxattr && vxattr->readonly)
786 return -EOPNOTSUPP;
Sage Weil355da1e2009-10-06 11:31:08 -0700787
788 /* preallocate memory for xattr name, value, index node */
789 err = -ENOMEM;
Julia Lawall61413c22010-10-17 21:55:21 +0200790 newname = kmemdup(name, name_len + 1, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700791 if (!newname)
792 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700793
794 if (val_len) {
Alex Elderb829c192012-01-23 15:49:27 -0600795 newval = kmemdup(value, val_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700796 if (!newval)
797 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700798 }
799
800 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
801 if (!xattr)
802 goto out;
803
Sage Weilbe655592011-11-30 09:47:09 -0800804 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700805retry:
806 issued = __ceph_caps_issued(ci, NULL);
Alex Elder18fa8b32012-01-23 15:49:28 -0600807 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
Sage Weil355da1e2009-10-06 11:31:08 -0700808 if (!(issued & CEPH_CAP_XATTR_EXCL))
809 goto do_sync;
810 __build_xattrs(inode);
811
812 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
813
814 if (!ci->i_xattrs.prealloc_blob ||
815 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
Alex Elder18fa8b32012-01-23 15:49:28 -0600816 struct ceph_buffer *blob;
Sage Weil355da1e2009-10-06 11:31:08 -0700817
Sage Weilbe655592011-11-30 09:47:09 -0800818 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700819 dout(" preaallocating new blob size=%d\n", required_blob_size);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800820 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700821 if (!blob)
822 goto out;
Sage Weilbe655592011-11-30 09:47:09 -0800823 spin_lock(&ci->i_ceph_lock);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800824 if (ci->i_xattrs.prealloc_blob)
825 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700826 ci->i_xattrs.prealloc_blob = blob;
827 goto retry;
828 }
829
Sage Weil355da1e2009-10-06 11:31:08 -0700830 err = __set_xattr(ci, newname, name_len, newval,
831 val_len, 1, 1, 1, &xattr);
Alex Elder18fa8b32012-01-23 15:49:28 -0600832
Sage Weilfca65b42011-05-04 11:33:47 -0700833 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
Sage Weil355da1e2009-10-06 11:31:08 -0700834 ci->i_xattrs.dirty = true;
835 inode->i_ctime = CURRENT_TIME;
Alex Elder18fa8b32012-01-23 15:49:28 -0600836
Sage Weilbe655592011-11-30 09:47:09 -0800837 spin_unlock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -0700838 if (dirty)
839 __mark_inode_dirty(inode, dirty);
Sage Weil355da1e2009-10-06 11:31:08 -0700840 return err;
841
842do_sync:
Sage Weilbe655592011-11-30 09:47:09 -0800843 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700844 err = ceph_sync_setxattr(dentry, name, value, size, flags);
845out:
846 kfree(newname);
847 kfree(newval);
848 kfree(xattr);
849 return err;
850}
851
852static int ceph_send_removexattr(struct dentry *dentry, const char *name)
853{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700854 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
855 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -0700856 struct inode *inode = dentry->d_inode;
Sage Weil5f21c962011-07-26 11:30:29 -0700857 struct inode *parent_inode;
Sage Weil355da1e2009-10-06 11:31:08 -0700858 struct ceph_mds_request *req;
859 int err;
860
861 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
862 USE_AUTH_MDS);
863 if (IS_ERR(req))
864 return PTR_ERR(req);
Sage Weil70b666c2011-05-27 09:24:26 -0700865 req->r_inode = inode;
866 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700867 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
868 req->r_num_caps = 1;
869 req->r_path2 = kstrdup(name, GFP_NOFS);
870
Sage Weil5f21c962011-07-26 11:30:29 -0700871 parent_inode = ceph_get_dentry_parent_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700872 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
Sage Weil5f21c962011-07-26 11:30:29 -0700873 iput(parent_inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700874 ceph_mdsc_put_request(req);
875 return err;
876}
877
878int ceph_removexattr(struct dentry *dentry, const char *name)
879{
880 struct inode *inode = dentry->d_inode;
Alex Elder881a5fa2012-01-23 15:49:28 -0600881 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700882 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700883 int issued;
884 int err;
Alex Elder83eb26a2012-01-11 17:41:01 -0800885 int required_blob_size;
Sage Weilfca65b42011-05-04 11:33:47 -0700886 int dirty;
Sage Weil355da1e2009-10-06 11:31:08 -0700887
888 if (ceph_snap(inode) != CEPH_NOSNAP)
889 return -EROFS;
890
891 if (!ceph_is_valid_xattr(name))
892 return -EOPNOTSUPP;
893
Alex Elder06476a62012-01-23 15:49:27 -0600894 vxattr = ceph_match_vxattr(inode, name);
895 if (vxattr && vxattr->readonly)
896 return -EOPNOTSUPP;
Sage Weil355da1e2009-10-06 11:31:08 -0700897
Alex Elder83eb26a2012-01-11 17:41:01 -0800898 err = -ENOMEM;
Sage Weilbe655592011-11-30 09:47:09 -0800899 spin_lock(&ci->i_ceph_lock);
Alex Elder83eb26a2012-01-11 17:41:01 -0800900retry:
Sage Weil355da1e2009-10-06 11:31:08 -0700901 issued = __ceph_caps_issued(ci, NULL);
902 dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
903
904 if (!(issued & CEPH_CAP_XATTR_EXCL))
905 goto do_sync;
Alex Elder18fa8b32012-01-23 15:49:28 -0600906 __build_xattrs(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700907
Alex Elder83eb26a2012-01-11 17:41:01 -0800908 required_blob_size = __get_required_blob_size(ci, 0, 0);
909
910 if (!ci->i_xattrs.prealloc_blob ||
911 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
912 struct ceph_buffer *blob;
913
914 spin_unlock(&ci->i_ceph_lock);
915 dout(" preaallocating new blob size=%d\n", required_blob_size);
916 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
917 if (!blob)
918 goto out;
919 spin_lock(&ci->i_ceph_lock);
920 if (ci->i_xattrs.prealloc_blob)
921 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
922 ci->i_xattrs.prealloc_blob = blob;
923 goto retry;
924 }
925
Sage Weil355da1e2009-10-06 11:31:08 -0700926 err = __remove_xattr_by_name(ceph_inode(inode), name);
Alex Elder18fa8b32012-01-23 15:49:28 -0600927
Sage Weilfca65b42011-05-04 11:33:47 -0700928 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
Sage Weil355da1e2009-10-06 11:31:08 -0700929 ci->i_xattrs.dirty = true;
930 inode->i_ctime = CURRENT_TIME;
Sage Weilbe655592011-11-30 09:47:09 -0800931 spin_unlock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -0700932 if (dirty)
933 __mark_inode_dirty(inode, dirty);
Sage Weil355da1e2009-10-06 11:31:08 -0700934 return err;
935do_sync:
Sage Weilbe655592011-11-30 09:47:09 -0800936 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700937 err = ceph_send_removexattr(dentry, name);
Alex Elder83eb26a2012-01-11 17:41:01 -0800938out:
Sage Weil355da1e2009-10-06 11:31:08 -0700939 return err;
940}
941