]>
Commit | Line | Data |
---|---|---|
3d14c5d2 YS |
1 | #include <linux/ceph/ceph_debug.h> |
2 | ||
355da1eb | 3 | #include "super.h" |
3d14c5d2 YS |
4 | #include "mds_client.h" |
5 | ||
6 | #include <linux/ceph/decode.h> | |
355da1eb SW |
7 | |
8 | #include <linux/xattr.h> | |
5a0e3ad6 | 9 | #include <linux/slab.h> |
355da1eb | 10 | |
22891907 AE |
11 | #define XATTR_CEPH_PREFIX "ceph." |
12 | #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1) | |
13 | ||
355da1eb SW |
14 | static bool ceph_is_valid_xattr(const char *name) |
15 | { | |
22891907 | 16 | return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) || |
1a756278 | 17 | !strncmp(name, XATTR_SECURITY_PREFIX, |
355da1eb SW |
18 | 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 | */ | |
881a5fa2 | 27 | struct ceph_vxattr { |
355da1eb | 28 | char *name; |
3ce6cd12 | 29 | size_t name_size; /* strlen(name) + 1 (for '\0') */ |
355da1eb SW |
30 | size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val, |
31 | size_t size); | |
8860147a | 32 | bool readonly, hidden; |
355da1eb SW |
33 | }; |
34 | ||
35 | /* directories */ | |
36 | ||
aa4066ed | 37 | static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
38 | size_t size) |
39 | { | |
40 | return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs); | |
41 | } | |
42 | ||
aa4066ed | 43 | static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
44 | size_t size) |
45 | { | |
46 | return snprintf(val, size, "%lld", ci->i_files); | |
47 | } | |
48 | ||
aa4066ed | 49 | static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
50 | size_t size) |
51 | { | |
52 | return snprintf(val, size, "%lld", ci->i_subdirs); | |
53 | } | |
54 | ||
aa4066ed | 55 | static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
56 | size_t size) |
57 | { | |
58 | return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs); | |
59 | } | |
60 | ||
aa4066ed | 61 | static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
62 | size_t size) |
63 | { | |
64 | return snprintf(val, size, "%lld", ci->i_rfiles); | |
65 | } | |
66 | ||
aa4066ed | 67 | static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
68 | size_t size) |
69 | { | |
70 | return snprintf(val, size, "%lld", ci->i_rsubdirs); | |
71 | } | |
72 | ||
aa4066ed | 73 | static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
74 | size_t size) |
75 | { | |
76 | return snprintf(val, size, "%lld", ci->i_rbytes); | |
77 | } | |
78 | ||
aa4066ed | 79 | static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
80 | size_t size) |
81 | { | |
3489b42a | 82 | return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec, |
355da1eb SW |
83 | (long)ci->i_rctime.tv_nsec); |
84 | } | |
85 | ||
eb788084 AE |
86 | #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name |
87 | ||
8860147a SW |
88 | #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 | } | |
eb788084 | 96 | |
881a5fa2 | 97 | static struct ceph_vxattr ceph_dir_vxattrs[] = { |
eb788084 AE |
98 | 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 */ | |
355da1eb | 107 | }; |
3ce6cd12 | 108 | static size_t ceph_dir_vxattrs_name_size; /* total size of all names */ |
355da1eb SW |
109 | |
110 | /* files */ | |
111 | ||
aa4066ed | 112 | static size_t ceph_vxattrcb_file_layout(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
113 | size_t size) |
114 | { | |
b195befd SW |
115 | int ret; |
116 | ||
117 | ret = snprintf(val, size, | |
355da1eb SW |
118 | "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)); | |
b195befd | 122 | return ret; |
355da1eb SW |
123 | } |
124 | ||
881a5fa2 | 125 | static struct ceph_vxattr ceph_file_vxattrs[] = { |
eb788084 | 126 | XATTR_NAME_CEPH(file, layout), |
eb788084 | 127 | { 0 } /* Required table terminator */ |
355da1eb | 128 | }; |
3ce6cd12 | 129 | static size_t ceph_file_vxattrs_name_size; /* total size of all names */ |
355da1eb | 130 | |
881a5fa2 | 131 | static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode) |
355da1eb SW |
132 | { |
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 | ||
3ce6cd12 AE |
140 | static 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 | */ | |
155 | static 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++) | |
8860147a SW |
161 | if (!vxattr->hidden) |
162 | size += vxattr->name_size; | |
3ce6cd12 AE |
163 | |
164 | return size; | |
165 | } | |
166 | ||
167 | /* Routines called at initialization and exit time */ | |
168 | ||
169 | void __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 | ||
175 | void ceph_xattr_exit(void) | |
176 | { | |
177 | ceph_dir_vxattrs_name_size = 0; | |
178 | ceph_file_vxattrs_name_size = 0; | |
179 | } | |
180 | ||
881a5fa2 | 181 | static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode, |
355da1eb SW |
182 | const char *name) |
183 | { | |
881a5fa2 | 184 | struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode); |
06476a69 AE |
185 | |
186 | if (vxattr) { | |
187 | while (vxattr->name) { | |
188 | if (!strcmp(vxattr->name, name)) | |
189 | return vxattr; | |
190 | vxattr++; | |
191 | } | |
192 | } | |
193 | ||
355da1eb SW |
194 | return NULL; |
195 | } | |
196 | ||
197 | static 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 | } | |
355da1eb SW |
252 | 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 | ||
275 | static 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; | |
17db143f | 281 | int name_len = strlen(name); |
355da1eb SW |
282 | 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); | |
17db143f SW |
289 | if (c == 0 && name_len > xattr->name_len) |
290 | c = 1; | |
355da1eb SW |
291 | 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 | ||
307 | static 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 | ||
319 | static 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 | ||
340 | static 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 | ||
353 | static 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 | ||
377 | void __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 | ||
404 | static int __build_xattrs(struct inode *inode) | |
be655596 SW |
405 | __releases(ci->i_ceph_lock) |
406 | __acquires(ci->i_ceph_lock) | |
355da1eb SW |
407 | { |
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; | |
63ff78b2 | 416 | int err = 0; |
355da1eb SW |
417 | 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 | ||
427 | start: | |
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; | |
be655596 | 434 | spin_unlock(&ci->i_ceph_lock); |
355da1eb SW |
435 | |
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 | ||
be655596 | 449 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
450 | 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); | |
21ec6ffa | 455 | xattrs = NULL; |
355da1eb SW |
456 | 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; | |
480 | bad_lock: | |
be655596 | 481 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
482 | bad: |
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 | ||
492 | static 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 | */ | |
516 | void __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 | ||
b6c1d5b8 SW |
549 | if (ci->i_xattrs.blob) |
550 | ceph_buffer_put(ci->i_xattrs.blob); | |
355da1eb SW |
551 | ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob; |
552 | ci->i_xattrs.prealloc_blob = NULL; | |
553 | ci->i_xattrs.dirty = false; | |
4a625be4 | 554 | ci->i_xattrs.version++; |
355da1eb SW |
555 | } |
556 | } | |
557 | ||
558 | ssize_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); | |
355da1eb SW |
563 | int err; |
564 | struct ceph_inode_xattr *xattr; | |
881a5fa2 | 565 | struct ceph_vxattr *vxattr = NULL; |
355da1eb SW |
566 | |
567 | if (!ceph_is_valid_xattr(name)) | |
568 | return -ENODATA; | |
569 | ||
570 | /* let's see if a virtual xattr was requested */ | |
06476a69 | 571 | vxattr = ceph_match_vxattr(inode, name); |
355da1eb | 572 | |
be655596 | 573 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
574 | 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 { | |
be655596 | 581 | spin_unlock(&ci->i_ceph_lock); |
355da1eb SW |
582 | /* 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 | ||
be655596 | 588 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
589 | |
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 | ||
599 | get_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 | ||
618 | out: | |
be655596 | 619 | spin_unlock(&ci->i_ceph_lock); |
355da1eb SW |
620 | return err; |
621 | } | |
622 | ||
623 | ssize_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); | |
881a5fa2 | 627 | struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode); |
355da1eb SW |
628 | u32 vir_namelen = 0; |
629 | u32 namelen; | |
630 | int err; | |
631 | u32 len; | |
632 | int i; | |
633 | ||
be655596 | 634 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
635 | 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) && | |
bddfa3cc | 639 | (ci->i_xattrs.index_version >= ci->i_xattrs.version)) { |
355da1eb SW |
640 | goto list_xattr; |
641 | } else { | |
be655596 | 642 | spin_unlock(&ci->i_ceph_lock); |
355da1eb SW |
643 | err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR); |
644 | if (err) | |
645 | return err; | |
646 | } | |
647 | ||
be655596 | 648 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
649 | |
650 | err = __build_xattrs(inode); | |
651 | if (err < 0) | |
652 | goto out; | |
653 | ||
654 | list_xattr: | |
3ce6cd12 AE |
655 | /* |
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 | ||
355da1eb SW |
661 | /* 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 | ||
680 | out: | |
be655596 | 681 | spin_unlock(&ci->i_ceph_lock); |
355da1eb SW |
682 | return err; |
683 | } | |
684 | ||
685 | static int ceph_sync_setxattr(struct dentry *dentry, const char *name, | |
686 | const char *value, size_t size, int flags) | |
687 | { | |
3d14c5d2 | 688 | struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb); |
355da1eb SW |
689 | struct inode *inode = dentry->d_inode; |
690 | struct ceph_inode_info *ci = ceph_inode(inode); | |
5f21c96d | 691 | struct inode *parent_inode; |
355da1eb | 692 | struct ceph_mds_request *req; |
3d14c5d2 | 693 | struct ceph_mds_client *mdsc = fsc->mdsc; |
355da1eb SW |
694 | 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++) { | |
31459fe4 | 707 | pages[i] = __page_cache_alloc(GFP_NOFS); |
355da1eb SW |
708 | 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); | |
60d87733 JL |
723 | if (IS_ERR(req)) { |
724 | err = PTR_ERR(req); | |
725 | goto out; | |
726 | } | |
70b666c3 SW |
727 | req->r_inode = inode; |
728 | ihold(inode); | |
355da1eb SW |
729 | 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); | |
5f21c96d | 739 | parent_inode = ceph_get_dentry_parent_inode(dentry); |
355da1eb | 740 | err = ceph_mdsc_do_request(mdsc, parent_inode, req); |
5f21c96d | 741 | iput(parent_inode); |
355da1eb SW |
742 | ceph_mdsc_put_request(req); |
743 | dout("xattr.ver (after): %lld\n", ci->i_xattrs.version); | |
744 | ||
745 | out: | |
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 | ||
754 | int 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; | |
881a5fa2 | 758 | struct ceph_vxattr *vxattr; |
355da1eb | 759 | struct ceph_inode_info *ci = ceph_inode(inode); |
18fa8b3f | 760 | int issued; |
355da1eb | 761 | int err; |
18fa8b3f | 762 | int dirty; |
355da1eb SW |
763 | 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; | |
355da1eb SW |
768 | 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 | ||
06476a69 AE |
776 | vxattr = ceph_match_vxattr(inode, name); |
777 | if (vxattr && vxattr->readonly) | |
778 | return -EOPNOTSUPP; | |
355da1eb | 779 | |
3adf654d SW |
780 | /* 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 | ||
355da1eb SW |
784 | /* preallocate memory for xattr name, value, index node */ |
785 | err = -ENOMEM; | |
61413c2f | 786 | newname = kmemdup(name, name_len + 1, GFP_NOFS); |
355da1eb SW |
787 | if (!newname) |
788 | goto out; | |
355da1eb SW |
789 | |
790 | if (val_len) { | |
b829c195 | 791 | newval = kmemdup(value, val_len, GFP_NOFS); |
355da1eb SW |
792 | if (!newval) |
793 | goto out; | |
355da1eb SW |
794 | } |
795 | ||
796 | xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS); | |
797 | if (!xattr) | |
798 | goto out; | |
799 | ||
be655596 | 800 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
801 | retry: |
802 | issued = __ceph_caps_issued(ci, NULL); | |
18fa8b3f | 803 | dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued)); |
355da1eb SW |
804 | 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) { | |
18fa8b3f | 812 | struct ceph_buffer *blob; |
355da1eb | 813 | |
be655596 | 814 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 815 | dout(" preaallocating new blob size=%d\n", required_blob_size); |
b6c1d5b8 | 816 | blob = ceph_buffer_new(required_blob_size, GFP_NOFS); |
355da1eb SW |
817 | if (!blob) |
818 | goto out; | |
be655596 | 819 | spin_lock(&ci->i_ceph_lock); |
b6c1d5b8 SW |
820 | if (ci->i_xattrs.prealloc_blob) |
821 | ceph_buffer_put(ci->i_xattrs.prealloc_blob); | |
355da1eb SW |
822 | ci->i_xattrs.prealloc_blob = blob; |
823 | goto retry; | |
824 | } | |
825 | ||
355da1eb SW |
826 | err = __set_xattr(ci, newname, name_len, newval, |
827 | val_len, 1, 1, 1, &xattr); | |
18fa8b3f | 828 | |
fca65b4a | 829 | dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL); |
355da1eb SW |
830 | ci->i_xattrs.dirty = true; |
831 | inode->i_ctime = CURRENT_TIME; | |
18fa8b3f | 832 | |
be655596 | 833 | spin_unlock(&ci->i_ceph_lock); |
fca65b4a SW |
834 | if (dirty) |
835 | __mark_inode_dirty(inode, dirty); | |
355da1eb SW |
836 | return err; |
837 | ||
838 | do_sync: | |
be655596 | 839 | spin_unlock(&ci->i_ceph_lock); |
3adf654d | 840 | do_sync_unlocked: |
355da1eb SW |
841 | err = ceph_sync_setxattr(dentry, name, value, size, flags); |
842 | out: | |
843 | kfree(newname); | |
844 | kfree(newval); | |
845 | kfree(xattr); | |
846 | return err; | |
847 | } | |
848 | ||
849 | static int ceph_send_removexattr(struct dentry *dentry, const char *name) | |
850 | { | |
3d14c5d2 YS |
851 | struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb); |
852 | struct ceph_mds_client *mdsc = fsc->mdsc; | |
355da1eb | 853 | struct inode *inode = dentry->d_inode; |
5f21c96d | 854 | struct inode *parent_inode; |
355da1eb SW |
855 | 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); | |
70b666c3 SW |
862 | req->r_inode = inode; |
863 | ihold(inode); | |
355da1eb SW |
864 | req->r_inode_drop = CEPH_CAP_XATTR_SHARED; |
865 | req->r_num_caps = 1; | |
866 | req->r_path2 = kstrdup(name, GFP_NOFS); | |
867 | ||
5f21c96d | 868 | parent_inode = ceph_get_dentry_parent_inode(dentry); |
355da1eb | 869 | err = ceph_mdsc_do_request(mdsc, parent_inode, req); |
5f21c96d | 870 | iput(parent_inode); |
355da1eb SW |
871 | ceph_mdsc_put_request(req); |
872 | return err; | |
873 | } | |
874 | ||
875 | int ceph_removexattr(struct dentry *dentry, const char *name) | |
876 | { | |
877 | struct inode *inode = dentry->d_inode; | |
881a5fa2 | 878 | struct ceph_vxattr *vxattr; |
355da1eb | 879 | struct ceph_inode_info *ci = ceph_inode(inode); |
355da1eb SW |
880 | int issued; |
881 | int err; | |
83eb26af | 882 | int required_blob_size; |
fca65b4a | 883 | int dirty; |
355da1eb SW |
884 | |
885 | if (ceph_snap(inode) != CEPH_NOSNAP) | |
886 | return -EROFS; | |
887 | ||
888 | if (!ceph_is_valid_xattr(name)) | |
889 | return -EOPNOTSUPP; | |
890 | ||
06476a69 AE |
891 | vxattr = ceph_match_vxattr(inode, name); |
892 | if (vxattr && vxattr->readonly) | |
893 | return -EOPNOTSUPP; | |
355da1eb | 894 | |
83eb26af | 895 | err = -ENOMEM; |
be655596 | 896 | spin_lock(&ci->i_ceph_lock); |
83eb26af | 897 | retry: |
355da1eb SW |
898 | issued = __ceph_caps_issued(ci, NULL); |
899 | dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued)); | |
900 | ||
901 | if (!(issued & CEPH_CAP_XATTR_EXCL)) | |
902 | goto do_sync; | |
18fa8b3f | 903 | __build_xattrs(inode); |
355da1eb | 904 | |
83eb26af AE |
905 | required_blob_size = __get_required_blob_size(ci, 0, 0); |
906 | ||
907 | if (!ci->i_xattrs.prealloc_blob || | |
908 | required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) { | |
909 | struct ceph_buffer *blob; | |
910 | ||
911 | spin_unlock(&ci->i_ceph_lock); | |
912 | dout(" preaallocating new blob size=%d\n", required_blob_size); | |
913 | blob = ceph_buffer_new(required_blob_size, GFP_NOFS); | |
914 | if (!blob) | |
915 | goto out; | |
916 | spin_lock(&ci->i_ceph_lock); | |
917 | if (ci->i_xattrs.prealloc_blob) | |
918 | ceph_buffer_put(ci->i_xattrs.prealloc_blob); | |
919 | ci->i_xattrs.prealloc_blob = blob; | |
920 | goto retry; | |
921 | } | |
922 | ||
355da1eb | 923 | err = __remove_xattr_by_name(ceph_inode(inode), name); |
18fa8b3f | 924 | |
fca65b4a | 925 | dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL); |
355da1eb SW |
926 | ci->i_xattrs.dirty = true; |
927 | inode->i_ctime = CURRENT_TIME; | |
be655596 | 928 | spin_unlock(&ci->i_ceph_lock); |
fca65b4a SW |
929 | if (dirty) |
930 | __mark_inode_dirty(inode, dirty); | |
355da1eb SW |
931 | return err; |
932 | do_sync: | |
be655596 | 933 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 934 | err = ceph_send_removexattr(dentry, name); |
83eb26af | 935 | out: |
355da1eb SW |
936 | return err; |
937 | } | |
938 |