blob: 234270f00c2a0f9e38ec088db9100c6fe6d5d384 [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);
Sage Weil88601472013-01-31 11:53:27 -080032 bool readonly, hidden;
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
Sage Weil88601472013-01-31 11:53:27 -080088#define XATTR_NAME_CEPH(_type, _name) \
89 { \
90 .name = CEPH_XATTR_NAME(_type, _name), \
91 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
92 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
93 .readonly = true, \
94 .hidden = false, \
95 }
Alex Eldereb788082012-01-23 15:49:28 -060096
Alex Elder881a5fa2012-01-23 15:49:28 -060097static struct ceph_vxattr ceph_dir_vxattrs[] = {
Alex Eldereb788082012-01-23 15:49:28 -060098 XATTR_NAME_CEPH(dir, entries),
99 XATTR_NAME_CEPH(dir, files),
100 XATTR_NAME_CEPH(dir, subdirs),
101 XATTR_NAME_CEPH(dir, rentries),
102 XATTR_NAME_CEPH(dir, rfiles),
103 XATTR_NAME_CEPH(dir, rsubdirs),
104 XATTR_NAME_CEPH(dir, rbytes),
105 XATTR_NAME_CEPH(dir, rctime),
106 { 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700107};
Alex Elder3ce6cd12012-01-23 15:49:28 -0600108static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
Sage Weil355da1e2009-10-06 11:31:08 -0700109
110/* files */
111
Alex Elderaa4066e2012-01-23 15:49:28 -0600112static size_t ceph_vxattrcb_file_layout(struct ceph_inode_info *ci, char *val,
Sage Weil355da1e2009-10-06 11:31:08 -0700113 size_t size)
114{
Sage Weilb195bef2009-10-07 10:59:30 -0700115 int ret;
116
117 ret = snprintf(val, size,
Sage Weil355da1e2009-10-06 11:31:08 -0700118 "chunk_bytes=%lld\nstripe_count=%lld\nobject_size=%lld\n",
119 (unsigned long long)ceph_file_layout_su(ci->i_layout),
120 (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
121 (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
Sage Weilb195bef2009-10-07 10:59:30 -0700122 return ret;
Sage Weil355da1e2009-10-06 11:31:08 -0700123}
124
Alex Elder881a5fa2012-01-23 15:49:28 -0600125static struct ceph_vxattr ceph_file_vxattrs[] = {
Alex Eldereb788082012-01-23 15:49:28 -0600126 XATTR_NAME_CEPH(file, layout),
Alex Eldereb788082012-01-23 15:49:28 -0600127 { 0 } /* Required table terminator */
Sage Weil355da1e2009-10-06 11:31:08 -0700128};
Alex Elder3ce6cd12012-01-23 15:49:28 -0600129static size_t ceph_file_vxattrs_name_size; /* total size of all names */
Sage Weil355da1e2009-10-06 11:31:08 -0700130
Alex Elder881a5fa2012-01-23 15:49:28 -0600131static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
Sage Weil355da1e2009-10-06 11:31:08 -0700132{
133 if (S_ISDIR(inode->i_mode))
134 return ceph_dir_vxattrs;
135 else if (S_ISREG(inode->i_mode))
136 return ceph_file_vxattrs;
137 return NULL;
138}
139
Alex Elder3ce6cd12012-01-23 15:49:28 -0600140static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
141{
142 if (vxattrs == ceph_dir_vxattrs)
143 return ceph_dir_vxattrs_name_size;
144 if (vxattrs == ceph_file_vxattrs)
145 return ceph_file_vxattrs_name_size;
146 BUG();
147
148 return 0;
149}
150
151/*
152 * Compute the aggregate size (including terminating '\0') of all
153 * virtual extended attribute names in the given vxattr table.
154 */
155static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
156{
157 struct ceph_vxattr *vxattr;
158 size_t size = 0;
159
160 for (vxattr = vxattrs; vxattr->name; vxattr++)
Sage Weil88601472013-01-31 11:53:27 -0800161 if (!vxattr->hidden)
162 size += vxattr->name_size;
Alex Elder3ce6cd12012-01-23 15:49:28 -0600163
164 return size;
165}
166
167/* Routines called at initialization and exit time */
168
169void __init ceph_xattr_init(void)
170{
171 ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
172 ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
173}
174
175void ceph_xattr_exit(void)
176{
177 ceph_dir_vxattrs_name_size = 0;
178 ceph_file_vxattrs_name_size = 0;
179}
180
Alex Elder881a5fa2012-01-23 15:49:28 -0600181static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
Sage Weil355da1e2009-10-06 11:31:08 -0700182 const char *name)
183{
Alex Elder881a5fa2012-01-23 15:49:28 -0600184 struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
Alex Elder06476a62012-01-23 15:49:27 -0600185
186 if (vxattr) {
187 while (vxattr->name) {
188 if (!strcmp(vxattr->name, name))
189 return vxattr;
190 vxattr++;
191 }
192 }
193
Sage Weil355da1e2009-10-06 11:31:08 -0700194 return NULL;
195}
196
197static int __set_xattr(struct ceph_inode_info *ci,
198 const char *name, int name_len,
199 const char *val, int val_len,
200 int dirty,
201 int should_free_name, int should_free_val,
202 struct ceph_inode_xattr **newxattr)
203{
204 struct rb_node **p;
205 struct rb_node *parent = NULL;
206 struct ceph_inode_xattr *xattr = NULL;
207 int c;
208 int new = 0;
209
210 p = &ci->i_xattrs.index.rb_node;
211 while (*p) {
212 parent = *p;
213 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
214 c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
215 if (c < 0)
216 p = &(*p)->rb_left;
217 else if (c > 0)
218 p = &(*p)->rb_right;
219 else {
220 if (name_len == xattr->name_len)
221 break;
222 else if (name_len < xattr->name_len)
223 p = &(*p)->rb_left;
224 else
225 p = &(*p)->rb_right;
226 }
227 xattr = NULL;
228 }
229
230 if (!xattr) {
231 new = 1;
232 xattr = *newxattr;
233 xattr->name = name;
234 xattr->name_len = name_len;
235 xattr->should_free_name = should_free_name;
236
237 ci->i_xattrs.count++;
238 dout("__set_xattr count=%d\n", ci->i_xattrs.count);
239 } else {
240 kfree(*newxattr);
241 *newxattr = NULL;
242 if (xattr->should_free_val)
243 kfree((void *)xattr->val);
244
245 if (should_free_name) {
246 kfree((void *)name);
247 name = xattr->name;
248 }
249 ci->i_xattrs.names_size -= xattr->name_len;
250 ci->i_xattrs.vals_size -= xattr->val_len;
251 }
Sage Weil355da1e2009-10-06 11:31:08 -0700252 ci->i_xattrs.names_size += name_len;
253 ci->i_xattrs.vals_size += val_len;
254 if (val)
255 xattr->val = val;
256 else
257 xattr->val = "";
258
259 xattr->val_len = val_len;
260 xattr->dirty = dirty;
261 xattr->should_free_val = (val && should_free_val);
262
263 if (new) {
264 rb_link_node(&xattr->node, parent, p);
265 rb_insert_color(&xattr->node, &ci->i_xattrs.index);
266 dout("__set_xattr_val p=%p\n", p);
267 }
268
269 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
270 ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
271
272 return 0;
273}
274
275static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
276 const char *name)
277{
278 struct rb_node **p;
279 struct rb_node *parent = NULL;
280 struct ceph_inode_xattr *xattr = NULL;
Sage Weil17db1432011-01-13 15:27:29 -0800281 int name_len = strlen(name);
Sage Weil355da1e2009-10-06 11:31:08 -0700282 int c;
283
284 p = &ci->i_xattrs.index.rb_node;
285 while (*p) {
286 parent = *p;
287 xattr = rb_entry(parent, struct ceph_inode_xattr, node);
288 c = strncmp(name, xattr->name, xattr->name_len);
Sage Weil17db1432011-01-13 15:27:29 -0800289 if (c == 0 && name_len > xattr->name_len)
290 c = 1;
Sage Weil355da1e2009-10-06 11:31:08 -0700291 if (c < 0)
292 p = &(*p)->rb_left;
293 else if (c > 0)
294 p = &(*p)->rb_right;
295 else {
296 dout("__get_xattr %s: found %.*s\n", name,
297 xattr->val_len, xattr->val);
298 return xattr;
299 }
300 }
301
302 dout("__get_xattr %s: not found\n", name);
303
304 return NULL;
305}
306
307static void __free_xattr(struct ceph_inode_xattr *xattr)
308{
309 BUG_ON(!xattr);
310
311 if (xattr->should_free_name)
312 kfree((void *)xattr->name);
313 if (xattr->should_free_val)
314 kfree((void *)xattr->val);
315
316 kfree(xattr);
317}
318
319static int __remove_xattr(struct ceph_inode_info *ci,
320 struct ceph_inode_xattr *xattr)
321{
322 if (!xattr)
323 return -EOPNOTSUPP;
324
325 rb_erase(&xattr->node, &ci->i_xattrs.index);
326
327 if (xattr->should_free_name)
328 kfree((void *)xattr->name);
329 if (xattr->should_free_val)
330 kfree((void *)xattr->val);
331
332 ci->i_xattrs.names_size -= xattr->name_len;
333 ci->i_xattrs.vals_size -= xattr->val_len;
334 ci->i_xattrs.count--;
335 kfree(xattr);
336
337 return 0;
338}
339
340static int __remove_xattr_by_name(struct ceph_inode_info *ci,
341 const char *name)
342{
343 struct rb_node **p;
344 struct ceph_inode_xattr *xattr;
345 int err;
346
347 p = &ci->i_xattrs.index.rb_node;
348 xattr = __get_xattr(ci, name);
349 err = __remove_xattr(ci, xattr);
350 return err;
351}
352
353static char *__copy_xattr_names(struct ceph_inode_info *ci,
354 char *dest)
355{
356 struct rb_node *p;
357 struct ceph_inode_xattr *xattr = NULL;
358
359 p = rb_first(&ci->i_xattrs.index);
360 dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
361
362 while (p) {
363 xattr = rb_entry(p, struct ceph_inode_xattr, node);
364 memcpy(dest, xattr->name, xattr->name_len);
365 dest[xattr->name_len] = '\0';
366
367 dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
368 xattr->name_len, ci->i_xattrs.names_size);
369
370 dest += xattr->name_len + 1;
371 p = rb_next(p);
372 }
373
374 return dest;
375}
376
377void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
378{
379 struct rb_node *p, *tmp;
380 struct ceph_inode_xattr *xattr = NULL;
381
382 p = rb_first(&ci->i_xattrs.index);
383
384 dout("__ceph_destroy_xattrs p=%p\n", p);
385
386 while (p) {
387 xattr = rb_entry(p, struct ceph_inode_xattr, node);
388 tmp = p;
389 p = rb_next(tmp);
390 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
391 xattr->name_len, xattr->name);
392 rb_erase(tmp, &ci->i_xattrs.index);
393
394 __free_xattr(xattr);
395 }
396
397 ci->i_xattrs.names_size = 0;
398 ci->i_xattrs.vals_size = 0;
399 ci->i_xattrs.index_version = 0;
400 ci->i_xattrs.count = 0;
401 ci->i_xattrs.index = RB_ROOT;
402}
403
404static int __build_xattrs(struct inode *inode)
Sage Weilbe655592011-11-30 09:47:09 -0800405 __releases(ci->i_ceph_lock)
406 __acquires(ci->i_ceph_lock)
Sage Weil355da1e2009-10-06 11:31:08 -0700407{
408 u32 namelen;
409 u32 numattr = 0;
410 void *p, *end;
411 u32 len;
412 const char *name, *val;
413 struct ceph_inode_info *ci = ceph_inode(inode);
414 int xattr_version;
415 struct ceph_inode_xattr **xattrs = NULL;
Sage Weil63ff78b2009-11-01 17:51:15 -0800416 int err = 0;
Sage Weil355da1e2009-10-06 11:31:08 -0700417 int i;
418
419 dout("__build_xattrs() len=%d\n",
420 ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
421
422 if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
423 return 0; /* already built */
424
425 __ceph_destroy_xattrs(ci);
426
427start:
428 /* updated internal xattr rb tree */
429 if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
430 p = ci->i_xattrs.blob->vec.iov_base;
431 end = p + ci->i_xattrs.blob->vec.iov_len;
432 ceph_decode_32_safe(&p, end, numattr, bad);
433 xattr_version = ci->i_xattrs.version;
Sage Weilbe655592011-11-30 09:47:09 -0800434 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700435
436 xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
437 GFP_NOFS);
438 err = -ENOMEM;
439 if (!xattrs)
440 goto bad_lock;
441 memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
442 for (i = 0; i < numattr; i++) {
443 xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
444 GFP_NOFS);
445 if (!xattrs[i])
446 goto bad_lock;
447 }
448
Sage Weilbe655592011-11-30 09:47:09 -0800449 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700450 if (ci->i_xattrs.version != xattr_version) {
451 /* lost a race, retry */
452 for (i = 0; i < numattr; i++)
453 kfree(xattrs[i]);
454 kfree(xattrs);
Alan Cox21ec6ff2012-07-20 08:18:36 -0500455 xattrs = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700456 goto start;
457 }
458 err = -EIO;
459 while (numattr--) {
460 ceph_decode_32_safe(&p, end, len, bad);
461 namelen = len;
462 name = p;
463 p += len;
464 ceph_decode_32_safe(&p, end, len, bad);
465 val = p;
466 p += len;
467
468 err = __set_xattr(ci, name, namelen, val, len,
469 0, 0, 0, &xattrs[numattr]);
470
471 if (err < 0)
472 goto bad;
473 }
474 kfree(xattrs);
475 }
476 ci->i_xattrs.index_version = ci->i_xattrs.version;
477 ci->i_xattrs.dirty = false;
478
479 return err;
480bad_lock:
Sage Weilbe655592011-11-30 09:47:09 -0800481 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700482bad:
483 if (xattrs) {
484 for (i = 0; i < numattr; i++)
485 kfree(xattrs[i]);
486 kfree(xattrs);
487 }
488 ci->i_xattrs.names_size = 0;
489 return err;
490}
491
492static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
493 int val_size)
494{
495 /*
496 * 4 bytes for the length, and additional 4 bytes per each xattr name,
497 * 4 bytes per each value
498 */
499 int size = 4 + ci->i_xattrs.count*(4 + 4) +
500 ci->i_xattrs.names_size +
501 ci->i_xattrs.vals_size;
502 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
503 ci->i_xattrs.count, ci->i_xattrs.names_size,
504 ci->i_xattrs.vals_size);
505
506 if (name_size)
507 size += 4 + 4 + name_size + val_size;
508
509 return size;
510}
511
512/*
513 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
514 * and swap into place.
515 */
516void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
517{
518 struct rb_node *p;
519 struct ceph_inode_xattr *xattr = NULL;
520 void *dest;
521
522 dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
523 if (ci->i_xattrs.dirty) {
524 int need = __get_required_blob_size(ci, 0, 0);
525
526 BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
527
528 p = rb_first(&ci->i_xattrs.index);
529 dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
530
531 ceph_encode_32(&dest, ci->i_xattrs.count);
532 while (p) {
533 xattr = rb_entry(p, struct ceph_inode_xattr, node);
534
535 ceph_encode_32(&dest, xattr->name_len);
536 memcpy(dest, xattr->name, xattr->name_len);
537 dest += xattr->name_len;
538 ceph_encode_32(&dest, xattr->val_len);
539 memcpy(dest, xattr->val, xattr->val_len);
540 dest += xattr->val_len;
541
542 p = rb_next(p);
543 }
544
545 /* adjust buffer len; it may be larger than we need */
546 ci->i_xattrs.prealloc_blob->vec.iov_len =
547 dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
548
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800549 if (ci->i_xattrs.blob)
550 ceph_buffer_put(ci->i_xattrs.blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700551 ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
552 ci->i_xattrs.prealloc_blob = NULL;
553 ci->i_xattrs.dirty = false;
Sage Weil4a625be2010-08-22 15:03:56 -0700554 ci->i_xattrs.version++;
Sage Weil355da1e2009-10-06 11:31:08 -0700555 }
556}
557
558ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
559 size_t size)
560{
561 struct inode *inode = dentry->d_inode;
562 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700563 int err;
564 struct ceph_inode_xattr *xattr;
Alex Elder881a5fa2012-01-23 15:49:28 -0600565 struct ceph_vxattr *vxattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700566
567 if (!ceph_is_valid_xattr(name))
568 return -ENODATA;
569
570 /* let's see if a virtual xattr was requested */
Alex Elder06476a62012-01-23 15:49:27 -0600571 vxattr = ceph_match_vxattr(inode, name);
Sage Weil355da1e2009-10-06 11:31:08 -0700572
Sage Weilbe655592011-11-30 09:47:09 -0800573 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700574 dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
575 ci->i_xattrs.version, ci->i_xattrs.index_version);
576
577 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
578 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
579 goto get_xattr;
580 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800581 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700582 /* get xattrs from mds (if we don't already have them) */
583 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
584 if (err)
585 return err;
586 }
587
Sage Weilbe655592011-11-30 09:47:09 -0800588 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700589
590 if (vxattr && vxattr->readonly) {
591 err = vxattr->getxattr_cb(ci, value, size);
592 goto out;
593 }
594
595 err = __build_xattrs(inode);
596 if (err < 0)
597 goto out;
598
599get_xattr:
600 err = -ENODATA; /* == ENOATTR */
601 xattr = __get_xattr(ci, name);
602 if (!xattr) {
603 if (vxattr)
604 err = vxattr->getxattr_cb(ci, value, size);
605 goto out;
606 }
607
608 err = -ERANGE;
609 if (size && size < xattr->val_len)
610 goto out;
611
612 err = xattr->val_len;
613 if (size == 0)
614 goto out;
615
616 memcpy(value, xattr->val, xattr->val_len);
617
618out:
Sage Weilbe655592011-11-30 09:47:09 -0800619 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700620 return err;
621}
622
623ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
624{
625 struct inode *inode = dentry->d_inode;
626 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder881a5fa2012-01-23 15:49:28 -0600627 struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700628 u32 vir_namelen = 0;
629 u32 namelen;
630 int err;
631 u32 len;
632 int i;
633
Sage Weilbe655592011-11-30 09:47:09 -0800634 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700635 dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
636 ci->i_xattrs.version, ci->i_xattrs.index_version);
637
638 if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
Henry C Changbddfa3c2010-04-29 09:32:28 -0700639 (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
Sage Weil355da1e2009-10-06 11:31:08 -0700640 goto list_xattr;
641 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800642 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700643 err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
644 if (err)
645 return err;
646 }
647
Sage Weilbe655592011-11-30 09:47:09 -0800648 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700649
650 err = __build_xattrs(inode);
651 if (err < 0)
652 goto out;
653
654list_xattr:
Alex Elder3ce6cd12012-01-23 15:49:28 -0600655 /*
656 * Start with virtual dir xattr names (if any) (including
657 * terminating '\0' characters for each).
658 */
659 vir_namelen = ceph_vxattrs_name_size(vxattrs);
660
Sage Weil355da1e2009-10-06 11:31:08 -0700661 /* adding 1 byte per each variable due to the null termination */
662 namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
663 err = -ERANGE;
664 if (size && namelen > size)
665 goto out;
666
667 err = namelen;
668 if (size == 0)
669 goto out;
670
671 names = __copy_xattr_names(ci, names);
672
673 /* virtual xattr names, too */
674 if (vxattrs)
675 for (i = 0; vxattrs[i].name; i++) {
676 len = sprintf(names, "%s", vxattrs[i].name);
677 names += len + 1;
678 }
679
680out:
Sage Weilbe655592011-11-30 09:47:09 -0800681 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700682 return err;
683}
684
685static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
686 const char *value, size_t size, int flags)
687{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700688 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
Sage Weil355da1e2009-10-06 11:31:08 -0700689 struct inode *inode = dentry->d_inode;
690 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil5f21c962011-07-26 11:30:29 -0700691 struct inode *parent_inode;
Sage Weil355da1e2009-10-06 11:31:08 -0700692 struct ceph_mds_request *req;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700693 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -0700694 int err;
695 int i, nr_pages;
696 struct page **pages = NULL;
697 void *kaddr;
698
699 /* copy value into some pages */
700 nr_pages = calc_pages_for(0, size);
701 if (nr_pages) {
702 pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
703 if (!pages)
704 return -ENOMEM;
705 err = -ENOMEM;
706 for (i = 0; i < nr_pages; i++) {
Yehuda Sadeh31459fe2010-03-17 13:54:02 -0700707 pages[i] = __page_cache_alloc(GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700708 if (!pages[i]) {
709 nr_pages = i;
710 goto out;
711 }
712 kaddr = kmap(pages[i]);
713 memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
714 min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
715 }
716 }
717
718 dout("setxattr value=%.*s\n", (int)size, value);
719
720 /* do request */
721 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
722 USE_AUTH_MDS);
Julia Lawall60d87732009-11-21 12:53:08 +0100723 if (IS_ERR(req)) {
724 err = PTR_ERR(req);
725 goto out;
726 }
Sage Weil70b666c2011-05-27 09:24:26 -0700727 req->r_inode = inode;
728 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700729 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
730 req->r_num_caps = 1;
731 req->r_args.setxattr.flags = cpu_to_le32(flags);
732 req->r_path2 = kstrdup(name, GFP_NOFS);
733
734 req->r_pages = pages;
735 req->r_num_pages = nr_pages;
736 req->r_data_len = size;
737
738 dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
Sage Weil5f21c962011-07-26 11:30:29 -0700739 parent_inode = ceph_get_dentry_parent_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700740 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
Sage Weil5f21c962011-07-26 11:30:29 -0700741 iput(parent_inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700742 ceph_mdsc_put_request(req);
743 dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
744
745out:
746 if (pages) {
747 for (i = 0; i < nr_pages; i++)
748 __free_page(pages[i]);
749 kfree(pages);
750 }
751 return err;
752}
753
754int ceph_setxattr(struct dentry *dentry, const char *name,
755 const void *value, size_t size, int flags)
756{
757 struct inode *inode = dentry->d_inode;
Alex Elder881a5fa2012-01-23 15:49:28 -0600758 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700759 struct ceph_inode_info *ci = ceph_inode(inode);
Alex Elder18fa8b32012-01-23 15:49:28 -0600760 int issued;
Sage Weil355da1e2009-10-06 11:31:08 -0700761 int err;
Alex Elder18fa8b32012-01-23 15:49:28 -0600762 int dirty;
Sage Weil355da1e2009-10-06 11:31:08 -0700763 int name_len = strlen(name);
764 int val_len = size;
765 char *newname = NULL;
766 char *newval = NULL;
767 struct ceph_inode_xattr *xattr = NULL;
Sage Weil355da1e2009-10-06 11:31:08 -0700768 int required_blob_size;
769
770 if (ceph_snap(inode) != CEPH_NOSNAP)
771 return -EROFS;
772
773 if (!ceph_is_valid_xattr(name))
774 return -EOPNOTSUPP;
775
Alex Elder06476a62012-01-23 15:49:27 -0600776 vxattr = ceph_match_vxattr(inode, name);
777 if (vxattr && vxattr->readonly)
778 return -EOPNOTSUPP;
Sage Weil355da1e2009-10-06 11:31:08 -0700779
Sage Weil3adf6542013-01-31 11:53:41 -0800780 /* pass any unhandled ceph.* xattrs through to the MDS */
781 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
782 goto do_sync_unlocked;
783
Sage Weil355da1e2009-10-06 11:31:08 -0700784 /* preallocate memory for xattr name, value, index node */
785 err = -ENOMEM;
Julia Lawall61413c22010-10-17 21:55:21 +0200786 newname = kmemdup(name, name_len + 1, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700787 if (!newname)
788 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700789
790 if (val_len) {
Alex Elderb829c192012-01-23 15:49:27 -0600791 newval = kmemdup(value, val_len, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700792 if (!newval)
793 goto out;
Sage Weil355da1e2009-10-06 11:31:08 -0700794 }
795
796 xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
797 if (!xattr)
798 goto out;
799
Sage Weilbe655592011-11-30 09:47:09 -0800800 spin_lock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700801retry:
802 issued = __ceph_caps_issued(ci, NULL);
Alex Elder18fa8b32012-01-23 15:49:28 -0600803 dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
Sage Weil355da1e2009-10-06 11:31:08 -0700804 if (!(issued & CEPH_CAP_XATTR_EXCL))
805 goto do_sync;
806 __build_xattrs(inode);
807
808 required_blob_size = __get_required_blob_size(ci, name_len, val_len);
809
810 if (!ci->i_xattrs.prealloc_blob ||
811 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
Alex Elder18fa8b32012-01-23 15:49:28 -0600812 struct ceph_buffer *blob;
Sage Weil355da1e2009-10-06 11:31:08 -0700813
Sage Weilbe655592011-11-30 09:47:09 -0800814 spin_unlock(&ci->i_ceph_lock);
Sage Weil355da1e2009-10-06 11:31:08 -0700815 dout(" preaallocating new blob size=%d\n", required_blob_size);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800816 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
Sage Weil355da1e2009-10-06 11:31:08 -0700817 if (!blob)
818 goto out;
Sage Weilbe655592011-11-30 09:47:09 -0800819 spin_lock(&ci->i_ceph_lock);
Sage Weilb6c1d5b2009-12-07 12:17:17 -0800820 if (ci->i_xattrs.prealloc_blob)
821 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
Sage Weil355da1e2009-10-06 11:31:08 -0700822 ci->i_xattrs.prealloc_blob = blob;
823 goto retry;
824 }
825
Sage Weil355da1e2009-10-06 11:31:08 -0700826 err = __set_xattr(ci, newname, name_len, newval,
827 val_len, 1, 1, 1, &xattr);
Alex Elder18fa8b32012-01-23 15:49:28 -0600828
Sage Weilfca65b42011-05-04 11:33:47 -0700829 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
Sage Weil355da1e2009-10-06 11:31:08 -0700830 ci->i_xattrs.dirty = true;
831 inode->i_ctime = CURRENT_TIME;
Alex Elder18fa8b32012-01-23 15:49:28 -0600832
Sage Weilbe655592011-11-30 09:47:09 -0800833 spin_unlock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -0700834 if (dirty)
835 __mark_inode_dirty(inode, dirty);
Sage Weil355da1e2009-10-06 11:31:08 -0700836 return err;
837
838do_sync:
Sage Weilbe655592011-11-30 09:47:09 -0800839 spin_unlock(&ci->i_ceph_lock);
Sage Weil3adf6542013-01-31 11:53:41 -0800840do_sync_unlocked:
Sage Weil355da1e2009-10-06 11:31:08 -0700841 err = ceph_sync_setxattr(dentry, name, value, size, flags);
842out:
843 kfree(newname);
844 kfree(newval);
845 kfree(xattr);
846 return err;
847}
848
849static int ceph_send_removexattr(struct dentry *dentry, const char *name)
850{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700851 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
852 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil355da1e2009-10-06 11:31:08 -0700853 struct inode *inode = dentry->d_inode;
Sage Weil5f21c962011-07-26 11:30:29 -0700854 struct inode *parent_inode;
Sage Weil355da1e2009-10-06 11:31:08 -0700855 struct ceph_mds_request *req;
856 int err;
857
858 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
859 USE_AUTH_MDS);
860 if (IS_ERR(req))
861 return PTR_ERR(req);
Sage Weil70b666c2011-05-27 09:24:26 -0700862 req->r_inode = inode;
863 ihold(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700864 req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
865 req->r_num_caps = 1;
866 req->r_path2 = kstrdup(name, GFP_NOFS);
867
Sage Weil5f21c962011-07-26 11:30:29 -0700868 parent_inode = ceph_get_dentry_parent_inode(dentry);
Sage Weil355da1e2009-10-06 11:31:08 -0700869 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
Sage Weil5f21c962011-07-26 11:30:29 -0700870 iput(parent_inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700871 ceph_mdsc_put_request(req);
872 return err;
873}
874
875int ceph_removexattr(struct dentry *dentry, const char *name)
876{
877 struct inode *inode = dentry->d_inode;
Alex Elder881a5fa2012-01-23 15:49:28 -0600878 struct ceph_vxattr *vxattr;
Sage Weil355da1e2009-10-06 11:31:08 -0700879 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700880 int issued;
881 int err;
Alex Elder83eb26a2012-01-11 17:41:01 -0800882 int required_blob_size;
Sage Weilfca65b42011-05-04 11:33:47 -0700883 int dirty;
Sage Weil355da1e2009-10-06 11:31:08 -0700884
885 if (ceph_snap(inode) != CEPH_NOSNAP)
886 return -EROFS;
887
888 if (!ceph_is_valid_xattr(name))
889 return -EOPNOTSUPP;
890
Alex Elder06476a62012-01-23 15:49:27 -0600891 vxattr = ceph_match_vxattr(inode, name);
892 if (vxattr && vxattr->readonly)
893 return -EOPNOTSUPP;
Sage Weil355da1e2009-10-06 11:31:08 -0700894
Sage Weild421acb2013-01-20 21:55:30 -0800895 /* pass any unhandled ceph.* xattrs through to the MDS */
896 if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
897 goto do_sync_unlocked;
898
Alex Elder83eb26a2012-01-11 17:41:01 -0800899 err = -ENOMEM;
Sage Weilbe655592011-11-30 09:47:09 -0800900 spin_lock(&ci->i_ceph_lock);
Alex Elder83eb26a2012-01-11 17:41:01 -0800901retry:
Sage Weil355da1e2009-10-06 11:31:08 -0700902 issued = __ceph_caps_issued(ci, NULL);
903 dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
904
905 if (!(issued & CEPH_CAP_XATTR_EXCL))
906 goto do_sync;
Alex Elder18fa8b32012-01-23 15:49:28 -0600907 __build_xattrs(inode);
Sage Weil355da1e2009-10-06 11:31:08 -0700908
Alex Elder83eb26a2012-01-11 17:41:01 -0800909 required_blob_size = __get_required_blob_size(ci, 0, 0);
910
911 if (!ci->i_xattrs.prealloc_blob ||
912 required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
913 struct ceph_buffer *blob;
914
915 spin_unlock(&ci->i_ceph_lock);
916 dout(" preaallocating new blob size=%d\n", required_blob_size);
917 blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
918 if (!blob)
919 goto out;
920 spin_lock(&ci->i_ceph_lock);
921 if (ci->i_xattrs.prealloc_blob)
922 ceph_buffer_put(ci->i_xattrs.prealloc_blob);
923 ci->i_xattrs.prealloc_blob = blob;
924 goto retry;
925 }
926
Sage Weil355da1e2009-10-06 11:31:08 -0700927 err = __remove_xattr_by_name(ceph_inode(inode), name);
Alex Elder18fa8b32012-01-23 15:49:28 -0600928
Sage Weilfca65b42011-05-04 11:33:47 -0700929 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
Sage Weil355da1e2009-10-06 11:31:08 -0700930 ci->i_xattrs.dirty = true;
931 inode->i_ctime = CURRENT_TIME;
Sage Weilbe655592011-11-30 09:47:09 -0800932 spin_unlock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -0700933 if (dirty)
934 __mark_inode_dirty(inode, dirty);
Sage Weil355da1e2009-10-06 11:31:08 -0700935 return err;
936do_sync:
Sage Weilbe655592011-11-30 09:47:09 -0800937 spin_unlock(&ci->i_ceph_lock);
Sage Weild421acb2013-01-20 21:55:30 -0800938do_sync_unlocked:
Sage Weil355da1e2009-10-06 11:31:08 -0700939 err = ceph_send_removexattr(dentry, name);
Alex Elder83eb26a2012-01-11 17:41:01 -0800940out:
Sage Weil355da1e2009-10-06 11:31:08 -0700941 return err;
942}
943