blob: c2048b1a53957f84504232e43ffa9666470e42e9 [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{
Alex Elder3489b422012-03-08 16:50:09 -060082 return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
Sage Weil355da1e2009-10-06 11:31:08 -070083 (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 return ret;
Sage Weil355da1e2009-10-06 11:31:08 -0700122}
123
Alex Elder881a5fa2012-01-23 15:49:28 -0600124static struct ceph_vxattr ceph_file_vxattrs[] = {
Alex Eldereb788082012-01-23 15:49:28 -0600125 XATTR_NAME_CEPH(file, layout),
Alex Eldereb788082012-01-23 15:49:28 -0600126 { 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700127};
Alex Elder3ce6cd12012-01-23 15:49:28 -0600128static size_t ceph_file_vxattrs_name_size; /* total size of all names */
Sage Weil355da1e2009-10-06 11:31:08 -0700129
Alex Elder881a5fa2012-01-23 15:49:28 -0600130static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
Sage Weil355da1e2009-10-06 11:31:08 -0700131{
132 if (S_ISDIR(inode->i_mode))
133 return ceph_dir_vxattrs;
134 else if (S_ISREG(inode->i_mode))
135 return ceph_file_vxattrs;
136 return NULL;
137}
138
Alex Elder3ce6cd12012-01-23 15:49:28 -0600139static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
140{
141 if (vxattrs == ceph_dir_vxattrs)
142 return ceph_dir_vxattrs_name_size;
143 if (vxattrs == ceph_file_vxattrs)
144 return ceph_file_vxattrs_name_size;
145 BUG();
146
147 return 0;
148}
149
150/*
151 * Compute the aggregate size (including terminating '\0') of all
152 * virtual extended attribute names in the given vxattr table.
153 */
154static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
155{
156 struct ceph_vxattr *vxattr;
157 size_t size = 0;
158
159 for (vxattr = vxattrs; vxattr->name; vxattr++)
160 size += vxattr->name_size;
161
162 return size;
163}
164
165/* Routines called at initialization and exit time */
166
167void __init ceph_xattr_init(void)
168{
169 ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
170 ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
171}
172
173void ceph_xattr_exit(void)
174{
175 ceph_dir_vxattrs_name_size = 0;
176 ceph_file_vxattrs_name_size = 0;
177}
178
Alex Elder881a5fa2012-01-23 15:49:28 -0600179static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
Sage Weil355da1e2009-10-06 11:31:08 -0700180 const char *name)
181{
Alex Elder881a5fa2012-01-23 15:49:28 -0600182 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
Alex Elder06476a62012-01-23 15:49:27 -0600183
184 if (vxattr) {
185 while (vxattr->name) {
186 if (!strcmp(vxattr->name, name))
187 return vxattr;
188 vxattr++;
189 }
190 }
191
Sage Weil355da1e2009-10-06 11:31:08 -0700192 return NULL;
193}
194
195static int __set_xattr(struct ceph_inode_info *ci,
196 const char *name, int name_len,
197 const char *val, int val_len,
198 int dirty,
199 int should_free_name, int should_free_val,
200 struct ceph_inode_xattr **newxattr)
201{
202 struct rb_node **p;
203 struct rb_node *parent = NULL;
204 struct ceph_inode_xattr *xattr = NULL;
205 int c;
206 int new = 0;
207
208 p = &ci->i_xattrs.index.rb_node;
209 while (*p) {
210 parent = *p;
211 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
212 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
213 if (c < 0)
214 p = &(*p)->rb_left;
215 else if (c > 0)
216 p = &(*p)->rb_right;
217 else {
218 if (name_len == xattr->name_len)
219 break;
220 else if (name_len < xattr->name_len)
221 p = &(*p)->rb_left;
222 else
223 p = &(*p)->rb_right;
224 }
225 xattr = NULL;
226 }
227
228 if (!xattr) {
229 new = 1;
230 xattr = *newxattr;
231 xattr->name = name;
232 xattr->name_len = name_len;
233 xattr->should_free_name = should_free_name;
234
235 ci->i_xattrs.count++;
236 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
237 } else {
238 kfree(*newxattr);
239 *newxattr = NULL;
240 if (xattr->should_free_val)
241 kfree((void *)xattr->val);
242
243 if (should_free_name) {
244 kfree((void *)name);
245 name = xattr->name;
246 }
247 ci->i_xattrs.names_size -= xattr->name_len;
248 ci->i_xattrs.vals_size -= xattr->val_len;
249 }
Sage Weil355da1e2009-10-06 11:31:08 -0700250 ci->i_xattrs.names_size += name_len;
251 ci->i_xattrs.vals_size += val_len;
252 if (val)
253 xattr->val = val;
254 else
255 xattr->val = "";
256
257 xattr->val_len = val_len;
258 xattr->dirty = dirty;
259 xattr->should_free_val = (val && should_free_val);
260
261 if (new) {
262 rb_link_node(&xattr->node, parent, p);
263 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
264 dout("__set_xattr_val p=%p\n", p);
265 }
266
267 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
268 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
269
270 return 0;
271}
272
273static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
274 const char *name)
275{
276 struct rb_node **p;
277 struct rb_node *parent = NULL;
278 struct ceph_inode_xattr *xattr = NULL;
Sage Weil17db1432011-01-13 15:27:29 -0800279 int name_len = strlen(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700280 int c;
281
282 p = &ci->i_xattrs.index.rb_node;
283 while (*p) {
284 parent = *p;
285 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
286 c = strncmp(name, xattr->name, xattr->name_len);
Sage Weil17db1432011-01-13 15:27:29 -0800287 if (c == 0 && name_len > xattr->name_len)
288 c = 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700289 if (c < 0)
290 p = &(*p)->rb_left;
291 else if (c > 0)
292 p = &(*p)->rb_right;
293 else {
294 dout("__get_xattr %s: found %.*s\n", name,
295 xattr->val_len, xattr->val);
296 return xattr;
297 }
298 }
299
300 dout("__get_xattr %s: not found\n", name);
301
302 return NULL;
303}
304
305static void __free_xattr(struct ceph_inode_xattr *xattr)
306{
307 BUG_ON(!xattr);
308
309 if (xattr->should_free_name)
310 kfree((void *)xattr->name);
311 if (xattr->should_free_val)
312 kfree((void *)xattr->val);
313
314 kfree(xattr);
315}
316
317static int __remove_xattr(struct ceph_inode_info *ci,
318 struct ceph_inode_xattr *xattr)
319{
320 if (!xattr)
321 return -EOPNOTSUPP;
322
323 rb_erase(&xattr->node, &ci->i_xattrs.index);
324
325 if (xattr->should_free_name)
326 kfree((void *)xattr->name);
327 if (xattr->should_free_val)
328 kfree((void *)xattr->val);
329
330 ci->i_xattrs.names_size -= xattr->name_len;
331 ci->i_xattrs.vals_size -= xattr->val_len;
332 ci->i_xattrs.count--;
333 kfree(xattr);
334
335 return 0;
336}
337
338static int __remove_xattr_by_name(struct ceph_inode_info *ci,
339 const char *name)
340{
341 struct rb_node **p;
342 struct ceph_inode_xattr *xattr;
343 int err;
344
345 p = &ci->i_xattrs.index.rb_node;
346 xattr = __get_xattr(ci, name);
347 err = __remove_xattr(ci, xattr);
348 return err;
349}
350
351static char *__copy_xattr_names(struct ceph_inode_info *ci,
352 char *dest)
353{
354 struct rb_node *p;
355 struct ceph_inode_xattr *xattr = NULL;
356
357 p = rb_first(&ci->i_xattrs.index);
358 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
359
360 while (p) {
361 xattr = rb_entry(p, struct ceph_inode_xattr, node);
362 memcpy(dest, xattr->name, xattr->name_len);
363 dest[xattr->name_len] = '\0';
364
365 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
366 xattr->name_len, ci->i_xattrs.names_size);
367
368 dest += xattr->name_len + 1;
369 p = rb_next(p);
370 }
371
372 return dest;
373}
374
375void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
376{
377 struct rb_node *p, *tmp;
378 struct ceph_inode_xattr *xattr = NULL;
379
380 p = rb_first(&ci->i_xattrs.index);
381
382 dout("__ceph_destroy_xattrs p=%p\n", p);
383
384 while (p) {
385 xattr = rb_entry(p, struct ceph_inode_xattr, node);
386 tmp = p;
387 p = rb_next(tmp);
388 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
389 xattr->name_len, xattr->name);
390 rb_erase(tmp, &ci->i_xattrs.index);
391
392 __free_xattr(xattr);
393 }
394
395 ci->i_xattrs.names_size = 0;
396 ci->i_xattrs.vals_size = 0;
397 ci->i_xattrs.index_version = 0;
398 ci->i_xattrs.count = 0;
399 ci->i_xattrs.index = RB_ROOT;
400}
401
402static int __build_xattrs(struct inode *inode)
Sage Weilbe655592011-11-30 09:47:09 -0800403 __releases(ci->i_ceph_lock)
404 __acquires(ci->i_ceph_lock)
Sage Weil355da1e2009-10-06 11:31:08 -0700405{
406 u32 namelen;
407 u32 numattr = 0;
408 void *p, *end;
409 u32 len;
410 const char *name, *val;
411 struct ceph_inode_info *ci = ceph_inode(inode);
412 int xattr_version;
413 struct ceph_inode_xattr **xattrs = NULL;
Sage Weil63ff78b2009-11-01 17:51:15 -0800414 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700415 int i;
416
417 dout("__build_xattrs() len=%d\n",
418 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
419
420 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
421 return 0; /* already built */
422
423 __ceph_destroy_xattrs(ci);
424
425start:
426 /* updated internal xattr rb tree */
427 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
428 p = ci->i_xattrs.blob->vec.iov_base;
429 end = p + ci->i_xattrs.blob->vec.iov_len;
430 ceph_decode_32_safe(&p, end, numattr, bad);
431 xattr_version = ci->i_xattrs.version;
Sage Weilbe655592011-11-30 09:47:09 -0800432 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700433
434 xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
435 GFP_NOFS);
436 err = -ENOMEM;
437 if (!xattrs)
438 goto bad_lock;
439 memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
440 for (i = 0; i < numattr; i++) {
441 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
442 GFP_NOFS);
443 if (!xattrs[i])
444 goto bad_lock;
445 }
446
Sage Weilbe655592011-11-30 09:47:09 -0800447 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700448 if (ci->i_xattrs.version != xattr_version) {
449 /* lost a race, retry */
450 for (i = 0; i < numattr; i++)
451 kfree(xattrs[i]);
452 kfree(xattrs);
Alan Cox21ec6ff2012-07-20 08:18:36 -0500453 xattrs = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700454 goto start;
455 }
456 err = -EIO;
457 while (numattr--) {
458 ceph_decode_32_safe(&p, end, len, bad);
459 namelen = len;
460 name = p;
461 p += len;
462 ceph_decode_32_safe(&p, end, len, bad);
463 val = p;
464 p += len;
465
466 err = __set_xattr(ci, name, namelen, val, len,
467 0, 0, 0, &xattrs[numattr]);
468
469 if (err < 0)
470 goto bad;
471 }
472 kfree(xattrs);
473 }
474 ci->i_xattrs.index_version = ci->i_xattrs.version;
475 ci->i_xattrs.dirty = false;
476
477 return err;
478bad_lock:
Sage Weilbe655592011-11-30 09:47:09 -0800479 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700480bad:
481 if (xattrs) {
482 for (i = 0; i < numattr; i++)
483 kfree(xattrs[i]);
484 kfree(xattrs);
485 }
486 ci->i_xattrs.names_size = 0;
487 return err;
488}
489
490static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
491 int val_size)
492{
493 /*
494 * 4 bytes for the length, and additional 4 bytes per each xattr name,
495 * 4 bytes per each value
496 */
497 int size = 4 + ci->i_xattrs.count*(4 + 4) +
498 ci->i_xattrs.names_size +
499 ci->i_xattrs.vals_size;
500 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
501 ci->i_xattrs.count, ci->i_xattrs.names_size,
502 ci->i_xattrs.vals_size);
503
504 if (name_size)
505 size += 4 + 4 + name_size + val_size;
506
507 return size;
508}
509
510/*
511 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
512 * and swap into place.
513 */
514void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
515{
516 struct rb_node *p;
517 struct ceph_inode_xattr *xattr = NULL;
518 void *dest;
519
520 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
521 if (ci->i_xattrs.dirty) {
522 int need = __get_required_blob_size(ci, 0, 0);
523
524 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
525
526 p = rb_first(&ci->i_xattrs.index);
527 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
528
529 ceph_encode_32(&dest, ci->i_xattrs.count);
530 while (p) {
531 xattr = rb_entry(p, struct ceph_inode_xattr, node);
532
533 ceph_encode_32(&dest, xattr->name_len);
534 memcpy(dest, xattr->name, xattr->name_len);
535 dest += xattr->name_len;
536 ceph_encode_32(&dest, xattr->val_len);
537 memcpy(dest, xattr->val, xattr->val_len);
538 dest += xattr->val_len;
539
540 p = rb_next(p);
541 }
542
543 /* adjust buffer len; it may be larger than we need */
544 ci->i_xattrs.prealloc_blob->vec.iov_len =
545 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
546
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800547 if (ci->i_xattrs.blob)
548 ceph_buffer_put(ci->i_xattrs.blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700549 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
550 ci->i_xattrs.prealloc_blob = NULL;
551 ci->i_xattrs.dirty = false;
Sage Weil4a625be2010-08-22 15:03:56 -0700552 ci->i_xattrs.version++;
Sage Weil355da1e2009-10-06 11:31:08 -0700553 }
554}
555
556ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
557 size_t size)
558{
559 struct inode *inode = dentry->d_inode;
560 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700561 int err;
562 struct ceph_inode_xattr *xattr;
Alex Elder881a5fa2012-01-23 15:49:28 -0600563 struct ceph_vxattr *vxattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700564
565 if (!ceph_is_valid_xattr(name))
566 return -ENODATA;
567
568 /* let's see if a virtual xattr was requested */
Alex Elder06476a62012-01-23 15:49:27 -0600569 vxattr = ceph_match_vxattr(inode, name);
Sage Weil355da1e2009-10-06 11:31:08 -0700570
Sage Weilbe655592011-11-30 09:47:09 -0800571 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700572 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
573 ci->i_xattrs.version, ci->i_xattrs.index_version);
574
575 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
576 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
577 goto get_xattr;
578 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800579 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700580 /* get xattrs from mds (if we don't already have them) */
581 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
582 if (err)
583 return err;
584 }
585
Sage Weilbe655592011-11-30 09:47:09 -0800586 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700587
588 if (vxattr && vxattr->readonly) {
589 err = vxattr->getxattr_cb(ci, value, size);
590 goto out;
591 }
592
593 err = __build_xattrs(inode);
594 if (err < 0)
595 goto out;
596
597get_xattr:
598 err = -ENODATA; /* == ENOATTR */
599 xattr = __get_xattr(ci, name);
600 if (!xattr) {
601 if (vxattr)
602 err = vxattr->getxattr_cb(ci, value, size);
603 goto out;
604 }
605
606 err = -ERANGE;
607 if (size && size < xattr->val_len)
608 goto out;
609
610 err = xattr->val_len;
611 if (size == 0)
612 goto out;
613
614 memcpy(value, xattr->val, xattr->val_len);
615
616out:
Sage Weilbe655592011-11-30 09:47:09 -0800617 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700618 return err;
619}
620
621ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
622{
623 struct inode *inode = dentry->d_inode;
624 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder881a5fa2012-01-23 15:49:28 -0600625 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700626 u32 vir_namelen = 0;
627 u32 namelen;
628 int err;
629 u32 len;
630 int i;
631
Sage Weilbe655592011-11-30 09:47:09 -0800632 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700633 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
634 ci->i_xattrs.version, ci->i_xattrs.index_version);
635
636 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
Henry C Changbddfa3c2010-04-29 09:32:28 -0700637 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
Sage Weil355da1e2009-10-06 11:31:08 -0700638 goto list_xattr;
639 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800640 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700641 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
642 if (err)
643 return err;
644 }
645
Sage Weilbe655592011-11-30 09:47:09 -0800646 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700647
648 err = __build_xattrs(inode);
649 if (err < 0)
650 goto out;
651
652list_xattr:
Alex Elder3ce6cd12012-01-23 15:49:28 -0600653 /*
654 * Start with virtual dir xattr names (if any) (including
655 * terminating '\0' characters for each).
656 */
657 vir_namelen = ceph_vxattrs_name_size(vxattrs);
658
Sage Weil355da1e2009-10-06 11:31:08 -0700659 /* adding 1 byte per each variable due to the null termination */
660 namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
661 err = -ERANGE;
662 if (size && namelen > size)
663 goto out;
664
665 err = namelen;
666 if (size == 0)
667 goto out;
668
669 names = __copy_xattr_names(ci, names);
670
671 /* virtual xattr names, too */
672 if (vxattrs)
673 for (i = 0; vxattrs[i].name; i++) {
674 len = sprintf(names, "%s", vxattrs[i].name);
675 names += len + 1;
676 }
677
678out:
Sage Weilbe655592011-11-30 09:47:09 -0800679 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700680 return err;
681}
682
683static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
684 const char *value, size_t size, int flags)
685{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700686 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700687 struct inode *inode = dentry->d_inode;
688 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil5f21c962011-07-26 11:30:29 -0700689 struct inode *parent_inode;
Sage Weil355da1e2009-10-06 11:31:08 -0700690 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700691 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -0700692 int err;
693 int i, nr_pages;
694 struct page **pages = NULL;
695 void *kaddr;
696
697 /* copy value into some pages */
698 nr_pages = calc_pages_for(0, size);
699 if (nr_pages) {
700 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
701 if (!pages)
702 return -ENOMEM;
703 err = -ENOMEM;
704 for (i = 0; i < nr_pages; i++) {
Yehuda Sadeh31459fe2010-03-17 13:54:02 -0700705 pages[i] = __page_cache_alloc(GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700706 if (!pages[i]) {
707 nr_pages = i;
708 goto out;
709 }
710 kaddr = kmap(pages[i]);
711 memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
712 min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
713 }
714 }
715
716 dout("setxattr value=%.*s\n", (int)size, value);
717
718 /* do request */
719 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
720 USE_AUTH_MDS);
Julia Lawall60d87732009-11-21 12:53:08 +0100721 if (IS_ERR(req)) {
722 err = PTR_ERR(req);
723 goto out;
724 }
Sage Weil70b666c2011-05-27 09:24:26 -0700725 req->r_inode = inode;
726 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700727 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
728 req->r_num_caps = 1;
729 req->r_args.setxattr.flags = cpu_to_le32(flags);
730 req->r_path2 = kstrdup(name, GFP_NOFS);
731
732 req->r_pages = pages;
733 req->r_num_pages = nr_pages;
734 req->r_data_len = size;
735
736 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
Sage Weil5f21c962011-07-26 11:30:29 -0700737 parent_inode = ceph_get_dentry_parent_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700738 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
Sage Weil5f21c962011-07-26 11:30:29 -0700739 iput(parent_inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700740 ceph_mdsc_put_request(req);
741 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
742
743out:
744 if (pages) {
745 for (i = 0; i < nr_pages; i++)
746 __free_page(pages[i]);
747 kfree(pages);
748 }
749 return err;
750}
751
752int ceph_setxattr(struct dentry *dentry, const char *name,
753 const void *value, size_t size, int flags)
754{
755 struct inode *inode = dentry->d_inode;
Alex Elder881a5fa2012-01-23 15:49:28 -0600756 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700757 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder18fa8b32012-01-23 15:49:28 -0600758 int issued;
Sage Weil355da1e2009-10-06 11:31:08 -0700759 int err;
Alex Elder18fa8b32012-01-23 15:49:28 -0600760 int dirty;
Sage Weil355da1e2009-10-06 11:31:08 -0700761 int name_len = strlen(name);
762 int val_len = size;
763 char *newname = NULL;
764 char *newval = NULL;
765 struct ceph_inode_xattr *xattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700766 int required_blob_size;
767
768 if (ceph_snap(inode) != CEPH_NOSNAP)
769 return -EROFS;
770
771 if (!ceph_is_valid_xattr(name))
772 return -EOPNOTSUPP;
773
Alex Elder06476a62012-01-23 15:49:27 -0600774 vxattr = ceph_match_vxattr(inode, name);
775 if (vxattr && vxattr->readonly)
776 return -EOPNOTSUPP;
Sage Weil355da1e2009-10-06 11:31:08 -0700777
778 /* preallocate memory for xattr name, value, index node */
779 err = -ENOMEM;
Julia Lawall61413c22010-10-17 21:55:21 +0200780 newname = kmemdup(name, name_len + 1, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700781 if (!newname)
782 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700783
784 if (val_len) {
Alex Elderb829c192012-01-23 15:49:27 -0600785 newval = kmemdup(value, val_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700786 if (!newval)
787 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700788 }
789
790 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
791 if (!xattr)
792 goto out;
793
Sage Weilbe655592011-11-30 09:47:09 -0800794 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700795retry:
796 issued = __ceph_caps_issued(ci, NULL);
Alex Elder18fa8b32012-01-23 15:49:28 -0600797 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
Sage Weil355da1e2009-10-06 11:31:08 -0700798 if (!(issued & CEPH_CAP_XATTR_EXCL))
799 goto do_sync;
800 __build_xattrs(inode);
801
802 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
803
804 if (!ci->i_xattrs.prealloc_blob ||
805 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
Alex Elder18fa8b32012-01-23 15:49:28 -0600806 struct ceph_buffer *blob;
Sage Weil355da1e2009-10-06 11:31:08 -0700807
Sage Weilbe655592011-11-30 09:47:09 -0800808 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700809 dout(" preaallocating new blob size=%d\n", required_blob_size);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800810 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700811 if (!blob)
812 goto out;
Sage Weilbe655592011-11-30 09:47:09 -0800813 spin_lock(&ci->i_ceph_lock);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800814 if (ci->i_xattrs.prealloc_blob)
815 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700816 ci->i_xattrs.prealloc_blob = blob;
817 goto retry;
818 }
819
Sage Weil355da1e2009-10-06 11:31:08 -0700820 err = __set_xattr(ci, newname, name_len, newval,
821 val_len, 1, 1, 1, &xattr);
Alex Elder18fa8b32012-01-23 15:49:28 -0600822
Sage Weilfca65b42011-05-04 11:33:47 -0700823 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
Sage Weil355da1e2009-10-06 11:31:08 -0700824 ci->i_xattrs.dirty = true;
825 inode->i_ctime = CURRENT_TIME;
Alex Elder18fa8b32012-01-23 15:49:28 -0600826
Sage Weilbe655592011-11-30 09:47:09 -0800827 spin_unlock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -0700828 if (dirty)
829 __mark_inode_dirty(inode, dirty);
Sage Weil355da1e2009-10-06 11:31:08 -0700830 return err;
831
832do_sync:
Sage Weilbe655592011-11-30 09:47:09 -0800833 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700834 err = ceph_sync_setxattr(dentry, name, value, size, flags);
835out:
836 kfree(newname);
837 kfree(newval);
838 kfree(xattr);
839 return err;
840}
841
842static int ceph_send_removexattr(struct dentry *dentry, const char *name)
843{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700844 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
845 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -0700846 struct inode *inode = dentry->d_inode;
Sage Weil5f21c962011-07-26 11:30:29 -0700847 struct inode *parent_inode;
Sage Weil355da1e2009-10-06 11:31:08 -0700848 struct ceph_mds_request *req;
849 int err;
850
851 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
852 USE_AUTH_MDS);
853 if (IS_ERR(req))
854 return PTR_ERR(req);
Sage Weil70b666c2011-05-27 09:24:26 -0700855 req->r_inode = inode;
856 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700857 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
858 req->r_num_caps = 1;
859 req->r_path2 = kstrdup(name, GFP_NOFS);
860
Sage Weil5f21c962011-07-26 11:30:29 -0700861 parent_inode = ceph_get_dentry_parent_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700862 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
Sage Weil5f21c962011-07-26 11:30:29 -0700863 iput(parent_inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700864 ceph_mdsc_put_request(req);
865 return err;
866}
867
868int ceph_removexattr(struct dentry *dentry, const char *name)
869{
870 struct inode *inode = dentry->d_inode;
Alex Elder881a5fa2012-01-23 15:49:28 -0600871 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700872 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700873 int issued;
874 int err;
Alex Elder83eb26a2012-01-11 17:41:01 -0800875 int required_blob_size;
Sage Weilfca65b42011-05-04 11:33:47 -0700876 int dirty;
Sage Weil355da1e2009-10-06 11:31:08 -0700877
878 if (ceph_snap(inode) != CEPH_NOSNAP)
879 return -EROFS;
880
881 if (!ceph_is_valid_xattr(name))
882 return -EOPNOTSUPP;
883
Alex Elder06476a62012-01-23 15:49:27 -0600884 vxattr = ceph_match_vxattr(inode, name);
885 if (vxattr && vxattr->readonly)
886 return -EOPNOTSUPP;
Sage Weil355da1e2009-10-06 11:31:08 -0700887
Alex Elder83eb26a2012-01-11 17:41:01 -0800888 err = -ENOMEM;
Sage Weilbe655592011-11-30 09:47:09 -0800889 spin_lock(&ci->i_ceph_lock);
Alex Elder83eb26a2012-01-11 17:41:01 -0800890retry:
Sage Weil355da1e2009-10-06 11:31:08 -0700891 issued = __ceph_caps_issued(ci, NULL);
892 dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
893
894 if (!(issued & CEPH_CAP_XATTR_EXCL))
895 goto do_sync;
Alex Elder18fa8b32012-01-23 15:49:28 -0600896 __build_xattrs(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700897
Alex Elder83eb26a2012-01-11 17:41:01 -0800898 required_blob_size = __get_required_blob_size(ci, 0, 0);
899
900 if (!ci->i_xattrs.prealloc_blob ||
901 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
902 struct ceph_buffer *blob;
903
904 spin_unlock(&ci->i_ceph_lock);
905 dout(" preaallocating new blob size=%d\n", required_blob_size);
906 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
907 if (!blob)
908 goto out;
909 spin_lock(&ci->i_ceph_lock);
910 if (ci->i_xattrs.prealloc_blob)
911 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
912 ci->i_xattrs.prealloc_blob = blob;
913 goto retry;
914 }
915
Sage Weil355da1e2009-10-06 11:31:08 -0700916 err = __remove_xattr_by_name(ceph_inode(inode), name);
Alex Elder18fa8b32012-01-23 15:49:28 -0600917
Sage Weilfca65b42011-05-04 11:33:47 -0700918 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
Sage Weil355da1e2009-10-06 11:31:08 -0700919 ci->i_xattrs.dirty = true;
920 inode->i_ctime = CURRENT_TIME;
Sage Weilbe655592011-11-30 09:47:09 -0800921 spin_unlock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -0700922 if (dirty)
923 __mark_inode_dirty(inode, dirty);
Sage Weil355da1e2009-10-06 11:31:08 -0700924 return err;
925do_sync:
Sage Weilbe655592011-11-30 09:47:09 -0800926 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700927 err = ceph_send_removexattr(dentry, name);
Alex Elder83eb26a2012-01-11 17:41:01 -0800928out:
Sage Weil355da1e2009-10-06 11:31:08 -0700929 return err;
930}
931