Commit | Line | Data |
---|---|---|
3d14c5d2 | 1 | #include <linux/ceph/ceph_debug.h> |
25e6bae3 | 2 | #include <linux/ceph/pagelist.h> |
3d14c5d2 | 3 | |
355da1eb | 4 | #include "super.h" |
3d14c5d2 YS |
5 | #include "mds_client.h" |
6 | ||
7 | #include <linux/ceph/decode.h> | |
355da1eb SW |
8 | |
9 | #include <linux/xattr.h> | |
4db658ea | 10 | #include <linux/posix_acl_xattr.h> |
5a0e3ad6 | 11 | #include <linux/slab.h> |
355da1eb | 12 | |
22891907 AE |
13 | #define XATTR_CEPH_PREFIX "ceph." |
14 | #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1) | |
15 | ||
bcdfeb2e YZ |
16 | static int __remove_xattr(struct ceph_inode_info *ci, |
17 | struct ceph_inode_xattr *xattr); | |
18 | ||
2cdeb1e4 AG |
19 | const struct xattr_handler ceph_other_xattr_handler; |
20 | ||
7221fe4c GZ |
21 | /* |
22 | * List of handlers for synthetic system.* attributes. Other | |
23 | * attributes are handled directly. | |
24 | */ | |
25 | const struct xattr_handler *ceph_xattr_handlers[] = { | |
26 | #ifdef CONFIG_CEPH_FS_POSIX_ACL | |
4db658ea LT |
27 | &posix_acl_access_xattr_handler, |
28 | &posix_acl_default_xattr_handler, | |
7221fe4c | 29 | #endif |
2cdeb1e4 | 30 | &ceph_other_xattr_handler, |
7221fe4c GZ |
31 | NULL, |
32 | }; | |
33 | ||
355da1eb SW |
34 | static bool ceph_is_valid_xattr(const char *name) |
35 | { | |
22891907 | 36 | return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) || |
1a756278 | 37 | !strncmp(name, XATTR_SECURITY_PREFIX, |
355da1eb SW |
38 | XATTR_SECURITY_PREFIX_LEN) || |
39 | !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || | |
40 | !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); | |
41 | } | |
42 | ||
43 | /* | |
44 | * These define virtual xattrs exposing the recursive directory | |
45 | * statistics and layout metadata. | |
46 | */ | |
881a5fa2 | 47 | struct ceph_vxattr { |
355da1eb | 48 | char *name; |
3ce6cd12 | 49 | size_t name_size; /* strlen(name) + 1 (for '\0') */ |
355da1eb SW |
50 | size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val, |
51 | size_t size); | |
8860147a | 52 | bool readonly, hidden; |
f36e4472 | 53 | bool (*exists_cb)(struct ceph_inode_info *ci); |
355da1eb SW |
54 | }; |
55 | ||
32ab0bd7 SW |
56 | /* layouts */ |
57 | ||
58 | static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci) | |
59 | { | |
60 | size_t s; | |
61 | char *p = (char *)&ci->i_layout; | |
62 | ||
63 | for (s = 0; s < sizeof(ci->i_layout); s++, p++) | |
64 | if (*p) | |
65 | return true; | |
66 | return false; | |
67 | } | |
68 | ||
69 | static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val, | |
1e5c6649 | 70 | size_t size) |
32ab0bd7 SW |
71 | { |
72 | int ret; | |
73 | struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb); | |
74 | struct ceph_osd_client *osdc = &fsc->client->osdc; | |
7627151e | 75 | s64 pool = ci->i_layout.pool_id; |
32ab0bd7 | 76 | const char *pool_name; |
1e5c6649 | 77 | char buf[128]; |
32ab0bd7 SW |
78 | |
79 | dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode); | |
5aea3dcd | 80 | down_read(&osdc->lock); |
32ab0bd7 | 81 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool); |
1e5c6649 YZ |
82 | if (pool_name) { |
83 | size_t len = strlen(pool_name); | |
84 | ret = snprintf(buf, sizeof(buf), | |
7627151e YZ |
85 | "stripe_unit=%u stripe_count=%u object_size=%u pool=", |
86 | ci->i_layout.stripe_unit, ci->i_layout.stripe_count, | |
87 | ci->i_layout.object_size); | |
1e5c6649 YZ |
88 | if (!size) { |
89 | ret += len; | |
90 | } else if (ret + len > size) { | |
91 | ret = -ERANGE; | |
92 | } else { | |
93 | memcpy(val, buf, ret); | |
94 | memcpy(val + ret, pool_name, len); | |
95 | ret += len; | |
96 | } | |
97 | } else { | |
98 | ret = snprintf(buf, sizeof(buf), | |
7627151e YZ |
99 | "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld", |
100 | ci->i_layout.stripe_unit, ci->i_layout.stripe_count, | |
101 | ci->i_layout.object_size, (unsigned long long)pool); | |
1e5c6649 YZ |
102 | if (size) { |
103 | if (ret <= size) | |
104 | memcpy(val, buf, ret); | |
105 | else | |
106 | ret = -ERANGE; | |
107 | } | |
108 | } | |
5aea3dcd | 109 | up_read(&osdc->lock); |
32ab0bd7 SW |
110 | return ret; |
111 | } | |
112 | ||
695b7119 SW |
113 | static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci, |
114 | char *val, size_t size) | |
115 | { | |
7627151e | 116 | return snprintf(val, size, "%u", ci->i_layout.stripe_unit); |
695b7119 SW |
117 | } |
118 | ||
119 | static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci, | |
120 | char *val, size_t size) | |
121 | { | |
7627151e | 122 | return snprintf(val, size, "%u", ci->i_layout.stripe_count); |
695b7119 SW |
123 | } |
124 | ||
125 | static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci, | |
126 | char *val, size_t size) | |
127 | { | |
7627151e | 128 | return snprintf(val, size, "%u", ci->i_layout.object_size); |
695b7119 SW |
129 | } |
130 | ||
131 | static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci, | |
132 | char *val, size_t size) | |
133 | { | |
134 | int ret; | |
135 | struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb); | |
136 | struct ceph_osd_client *osdc = &fsc->client->osdc; | |
7627151e | 137 | s64 pool = ci->i_layout.pool_id; |
695b7119 SW |
138 | const char *pool_name; |
139 | ||
5aea3dcd | 140 | down_read(&osdc->lock); |
695b7119 SW |
141 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool); |
142 | if (pool_name) | |
143 | ret = snprintf(val, size, "%s", pool_name); | |
144 | else | |
145 | ret = snprintf(val, size, "%lld", (unsigned long long)pool); | |
5aea3dcd | 146 | up_read(&osdc->lock); |
695b7119 SW |
147 | return ret; |
148 | } | |
149 | ||
355da1eb SW |
150 | /* directories */ |
151 | ||
aa4066ed | 152 | static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
153 | size_t size) |
154 | { | |
155 | return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs); | |
156 | } | |
157 | ||
aa4066ed | 158 | static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
159 | size_t size) |
160 | { | |
161 | return snprintf(val, size, "%lld", ci->i_files); | |
162 | } | |
163 | ||
aa4066ed | 164 | static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
165 | size_t size) |
166 | { | |
167 | return snprintf(val, size, "%lld", ci->i_subdirs); | |
168 | } | |
169 | ||
aa4066ed | 170 | static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
171 | size_t size) |
172 | { | |
173 | return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs); | |
174 | } | |
175 | ||
aa4066ed | 176 | static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
177 | size_t size) |
178 | { | |
179 | return snprintf(val, size, "%lld", ci->i_rfiles); | |
180 | } | |
181 | ||
aa4066ed | 182 | static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
183 | size_t size) |
184 | { | |
185 | return snprintf(val, size, "%lld", ci->i_rsubdirs); | |
186 | } | |
187 | ||
aa4066ed | 188 | static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
189 | size_t size) |
190 | { | |
191 | return snprintf(val, size, "%lld", ci->i_rbytes); | |
192 | } | |
193 | ||
aa4066ed | 194 | static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val, |
355da1eb SW |
195 | size_t size) |
196 | { | |
3489b42a | 197 | return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec, |
355da1eb SW |
198 | (long)ci->i_rctime.tv_nsec); |
199 | } | |
200 | ||
32ab0bd7 | 201 | |
eb788084 | 202 | #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name |
695b7119 SW |
203 | #define CEPH_XATTR_NAME2(_type, _name, _name2) \ |
204 | XATTR_CEPH_PREFIX #_type "." #_name "." #_name2 | |
eb788084 | 205 | |
8860147a SW |
206 | #define XATTR_NAME_CEPH(_type, _name) \ |
207 | { \ | |
208 | .name = CEPH_XATTR_NAME(_type, _name), \ | |
209 | .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \ | |
210 | .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \ | |
211 | .readonly = true, \ | |
212 | .hidden = false, \ | |
f36e4472 | 213 | .exists_cb = NULL, \ |
8860147a | 214 | } |
695b7119 SW |
215 | #define XATTR_LAYOUT_FIELD(_type, _name, _field) \ |
216 | { \ | |
217 | .name = CEPH_XATTR_NAME2(_type, _name, _field), \ | |
218 | .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \ | |
219 | .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \ | |
220 | .readonly = false, \ | |
221 | .hidden = true, \ | |
222 | .exists_cb = ceph_vxattrcb_layout_exists, \ | |
223 | } | |
eb788084 | 224 | |
881a5fa2 | 225 | static struct ceph_vxattr ceph_dir_vxattrs[] = { |
1f08f2b0 SW |
226 | { |
227 | .name = "ceph.dir.layout", | |
228 | .name_size = sizeof("ceph.dir.layout"), | |
229 | .getxattr_cb = ceph_vxattrcb_layout, | |
230 | .readonly = false, | |
cc48c3e8 | 231 | .hidden = true, |
1f08f2b0 SW |
232 | .exists_cb = ceph_vxattrcb_layout_exists, |
233 | }, | |
695b7119 SW |
234 | XATTR_LAYOUT_FIELD(dir, layout, stripe_unit), |
235 | XATTR_LAYOUT_FIELD(dir, layout, stripe_count), | |
236 | XATTR_LAYOUT_FIELD(dir, layout, object_size), | |
237 | XATTR_LAYOUT_FIELD(dir, layout, pool), | |
eb788084 AE |
238 | XATTR_NAME_CEPH(dir, entries), |
239 | XATTR_NAME_CEPH(dir, files), | |
240 | XATTR_NAME_CEPH(dir, subdirs), | |
241 | XATTR_NAME_CEPH(dir, rentries), | |
242 | XATTR_NAME_CEPH(dir, rfiles), | |
243 | XATTR_NAME_CEPH(dir, rsubdirs), | |
244 | XATTR_NAME_CEPH(dir, rbytes), | |
245 | XATTR_NAME_CEPH(dir, rctime), | |
2c3dd4ff | 246 | { .name = NULL, 0 } /* Required table terminator */ |
355da1eb | 247 | }; |
3ce6cd12 | 248 | static size_t ceph_dir_vxattrs_name_size; /* total size of all names */ |
355da1eb SW |
249 | |
250 | /* files */ | |
251 | ||
881a5fa2 | 252 | static struct ceph_vxattr ceph_file_vxattrs[] = { |
32ab0bd7 SW |
253 | { |
254 | .name = "ceph.file.layout", | |
255 | .name_size = sizeof("ceph.file.layout"), | |
256 | .getxattr_cb = ceph_vxattrcb_layout, | |
257 | .readonly = false, | |
cc48c3e8 | 258 | .hidden = true, |
32ab0bd7 SW |
259 | .exists_cb = ceph_vxattrcb_layout_exists, |
260 | }, | |
695b7119 SW |
261 | XATTR_LAYOUT_FIELD(file, layout, stripe_unit), |
262 | XATTR_LAYOUT_FIELD(file, layout, stripe_count), | |
263 | XATTR_LAYOUT_FIELD(file, layout, object_size), | |
264 | XATTR_LAYOUT_FIELD(file, layout, pool), | |
2c3dd4ff | 265 | { .name = NULL, 0 } /* Required table terminator */ |
355da1eb | 266 | }; |
3ce6cd12 | 267 | static size_t ceph_file_vxattrs_name_size; /* total size of all names */ |
355da1eb | 268 | |
881a5fa2 | 269 | static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode) |
355da1eb SW |
270 | { |
271 | if (S_ISDIR(inode->i_mode)) | |
272 | return ceph_dir_vxattrs; | |
273 | else if (S_ISREG(inode->i_mode)) | |
274 | return ceph_file_vxattrs; | |
275 | return NULL; | |
276 | } | |
277 | ||
3ce6cd12 AE |
278 | static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs) |
279 | { | |
280 | if (vxattrs == ceph_dir_vxattrs) | |
281 | return ceph_dir_vxattrs_name_size; | |
282 | if (vxattrs == ceph_file_vxattrs) | |
283 | return ceph_file_vxattrs_name_size; | |
0abb43dc | 284 | BUG_ON(vxattrs); |
3ce6cd12 AE |
285 | return 0; |
286 | } | |
287 | ||
288 | /* | |
289 | * Compute the aggregate size (including terminating '\0') of all | |
290 | * virtual extended attribute names in the given vxattr table. | |
291 | */ | |
292 | static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs) | |
293 | { | |
294 | struct ceph_vxattr *vxattr; | |
295 | size_t size = 0; | |
296 | ||
297 | for (vxattr = vxattrs; vxattr->name; vxattr++) | |
8860147a SW |
298 | if (!vxattr->hidden) |
299 | size += vxattr->name_size; | |
3ce6cd12 AE |
300 | |
301 | return size; | |
302 | } | |
303 | ||
304 | /* Routines called at initialization and exit time */ | |
305 | ||
306 | void __init ceph_xattr_init(void) | |
307 | { | |
308 | ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs); | |
309 | ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs); | |
310 | } | |
311 | ||
312 | void ceph_xattr_exit(void) | |
313 | { | |
314 | ceph_dir_vxattrs_name_size = 0; | |
315 | ceph_file_vxattrs_name_size = 0; | |
316 | } | |
317 | ||
881a5fa2 | 318 | static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode, |
355da1eb SW |
319 | const char *name) |
320 | { | |
881a5fa2 | 321 | struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode); |
06476a69 AE |
322 | |
323 | if (vxattr) { | |
324 | while (vxattr->name) { | |
325 | if (!strcmp(vxattr->name, name)) | |
326 | return vxattr; | |
327 | vxattr++; | |
328 | } | |
329 | } | |
330 | ||
355da1eb SW |
331 | return NULL; |
332 | } | |
333 | ||
334 | static int __set_xattr(struct ceph_inode_info *ci, | |
335 | const char *name, int name_len, | |
336 | const char *val, int val_len, | |
fbc0b970 | 337 | int flags, int update_xattr, |
355da1eb SW |
338 | struct ceph_inode_xattr **newxattr) |
339 | { | |
340 | struct rb_node **p; | |
341 | struct rb_node *parent = NULL; | |
342 | struct ceph_inode_xattr *xattr = NULL; | |
343 | int c; | |
344 | int new = 0; | |
345 | ||
346 | p = &ci->i_xattrs.index.rb_node; | |
347 | while (*p) { | |
348 | parent = *p; | |
349 | xattr = rb_entry(parent, struct ceph_inode_xattr, node); | |
350 | c = strncmp(name, xattr->name, min(name_len, xattr->name_len)); | |
351 | if (c < 0) | |
352 | p = &(*p)->rb_left; | |
353 | else if (c > 0) | |
354 | p = &(*p)->rb_right; | |
355 | else { | |
356 | if (name_len == xattr->name_len) | |
357 | break; | |
358 | else if (name_len < xattr->name_len) | |
359 | p = &(*p)->rb_left; | |
360 | else | |
361 | p = &(*p)->rb_right; | |
362 | } | |
363 | xattr = NULL; | |
364 | } | |
365 | ||
fbc0b970 YZ |
366 | if (update_xattr) { |
367 | int err = 0; | |
368 | if (xattr && (flags & XATTR_CREATE)) | |
369 | err = -EEXIST; | |
370 | else if (!xattr && (flags & XATTR_REPLACE)) | |
371 | err = -ENODATA; | |
372 | if (err) { | |
373 | kfree(name); | |
374 | kfree(val); | |
375 | return err; | |
376 | } | |
bcdfeb2e YZ |
377 | if (update_xattr < 0) { |
378 | if (xattr) | |
379 | __remove_xattr(ci, xattr); | |
380 | kfree(name); | |
381 | return 0; | |
382 | } | |
fbc0b970 YZ |
383 | } |
384 | ||
355da1eb SW |
385 | if (!xattr) { |
386 | new = 1; | |
387 | xattr = *newxattr; | |
388 | xattr->name = name; | |
389 | xattr->name_len = name_len; | |
fbc0b970 | 390 | xattr->should_free_name = update_xattr; |
355da1eb SW |
391 | |
392 | ci->i_xattrs.count++; | |
393 | dout("__set_xattr count=%d\n", ci->i_xattrs.count); | |
394 | } else { | |
395 | kfree(*newxattr); | |
396 | *newxattr = NULL; | |
397 | if (xattr->should_free_val) | |
398 | kfree((void *)xattr->val); | |
399 | ||
fbc0b970 | 400 | if (update_xattr) { |
355da1eb SW |
401 | kfree((void *)name); |
402 | name = xattr->name; | |
403 | } | |
404 | ci->i_xattrs.names_size -= xattr->name_len; | |
405 | ci->i_xattrs.vals_size -= xattr->val_len; | |
406 | } | |
355da1eb SW |
407 | ci->i_xattrs.names_size += name_len; |
408 | ci->i_xattrs.vals_size += val_len; | |
409 | if (val) | |
410 | xattr->val = val; | |
411 | else | |
412 | xattr->val = ""; | |
413 | ||
414 | xattr->val_len = val_len; | |
fbc0b970 YZ |
415 | xattr->dirty = update_xattr; |
416 | xattr->should_free_val = (val && update_xattr); | |
355da1eb SW |
417 | |
418 | if (new) { | |
419 | rb_link_node(&xattr->node, parent, p); | |
420 | rb_insert_color(&xattr->node, &ci->i_xattrs.index); | |
421 | dout("__set_xattr_val p=%p\n", p); | |
422 | } | |
423 | ||
424 | dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n", | |
425 | ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val); | |
426 | ||
427 | return 0; | |
428 | } | |
429 | ||
430 | static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci, | |
431 | const char *name) | |
432 | { | |
433 | struct rb_node **p; | |
434 | struct rb_node *parent = NULL; | |
435 | struct ceph_inode_xattr *xattr = NULL; | |
17db143f | 436 | int name_len = strlen(name); |
355da1eb SW |
437 | int c; |
438 | ||
439 | p = &ci->i_xattrs.index.rb_node; | |
440 | while (*p) { | |
441 | parent = *p; | |
442 | xattr = rb_entry(parent, struct ceph_inode_xattr, node); | |
443 | c = strncmp(name, xattr->name, xattr->name_len); | |
17db143f SW |
444 | if (c == 0 && name_len > xattr->name_len) |
445 | c = 1; | |
355da1eb SW |
446 | if (c < 0) |
447 | p = &(*p)->rb_left; | |
448 | else if (c > 0) | |
449 | p = &(*p)->rb_right; | |
450 | else { | |
451 | dout("__get_xattr %s: found %.*s\n", name, | |
452 | xattr->val_len, xattr->val); | |
453 | return xattr; | |
454 | } | |
455 | } | |
456 | ||
457 | dout("__get_xattr %s: not found\n", name); | |
458 | ||
459 | return NULL; | |
460 | } | |
461 | ||
462 | static void __free_xattr(struct ceph_inode_xattr *xattr) | |
463 | { | |
464 | BUG_ON(!xattr); | |
465 | ||
466 | if (xattr->should_free_name) | |
467 | kfree((void *)xattr->name); | |
468 | if (xattr->should_free_val) | |
469 | kfree((void *)xattr->val); | |
470 | ||
471 | kfree(xattr); | |
472 | } | |
473 | ||
474 | static int __remove_xattr(struct ceph_inode_info *ci, | |
475 | struct ceph_inode_xattr *xattr) | |
476 | { | |
477 | if (!xattr) | |
524186ac | 478 | return -ENODATA; |
355da1eb SW |
479 | |
480 | rb_erase(&xattr->node, &ci->i_xattrs.index); | |
481 | ||
482 | if (xattr->should_free_name) | |
483 | kfree((void *)xattr->name); | |
484 | if (xattr->should_free_val) | |
485 | kfree((void *)xattr->val); | |
486 | ||
487 | ci->i_xattrs.names_size -= xattr->name_len; | |
488 | ci->i_xattrs.vals_size -= xattr->val_len; | |
489 | ci->i_xattrs.count--; | |
490 | kfree(xattr); | |
491 | ||
492 | return 0; | |
493 | } | |
494 | ||
355da1eb SW |
495 | static char *__copy_xattr_names(struct ceph_inode_info *ci, |
496 | char *dest) | |
497 | { | |
498 | struct rb_node *p; | |
499 | struct ceph_inode_xattr *xattr = NULL; | |
500 | ||
501 | p = rb_first(&ci->i_xattrs.index); | |
502 | dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count); | |
503 | ||
504 | while (p) { | |
505 | xattr = rb_entry(p, struct ceph_inode_xattr, node); | |
506 | memcpy(dest, xattr->name, xattr->name_len); | |
507 | dest[xattr->name_len] = '\0'; | |
508 | ||
509 | dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name, | |
510 | xattr->name_len, ci->i_xattrs.names_size); | |
511 | ||
512 | dest += xattr->name_len + 1; | |
513 | p = rb_next(p); | |
514 | } | |
515 | ||
516 | return dest; | |
517 | } | |
518 | ||
519 | void __ceph_destroy_xattrs(struct ceph_inode_info *ci) | |
520 | { | |
521 | struct rb_node *p, *tmp; | |
522 | struct ceph_inode_xattr *xattr = NULL; | |
523 | ||
524 | p = rb_first(&ci->i_xattrs.index); | |
525 | ||
526 | dout("__ceph_destroy_xattrs p=%p\n", p); | |
527 | ||
528 | while (p) { | |
529 | xattr = rb_entry(p, struct ceph_inode_xattr, node); | |
530 | tmp = p; | |
531 | p = rb_next(tmp); | |
532 | dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p, | |
533 | xattr->name_len, xattr->name); | |
534 | rb_erase(tmp, &ci->i_xattrs.index); | |
535 | ||
536 | __free_xattr(xattr); | |
537 | } | |
538 | ||
539 | ci->i_xattrs.names_size = 0; | |
540 | ci->i_xattrs.vals_size = 0; | |
541 | ci->i_xattrs.index_version = 0; | |
542 | ci->i_xattrs.count = 0; | |
543 | ci->i_xattrs.index = RB_ROOT; | |
544 | } | |
545 | ||
546 | static int __build_xattrs(struct inode *inode) | |
be655596 SW |
547 | __releases(ci->i_ceph_lock) |
548 | __acquires(ci->i_ceph_lock) | |
355da1eb SW |
549 | { |
550 | u32 namelen; | |
551 | u32 numattr = 0; | |
552 | void *p, *end; | |
553 | u32 len; | |
554 | const char *name, *val; | |
555 | struct ceph_inode_info *ci = ceph_inode(inode); | |
556 | int xattr_version; | |
557 | struct ceph_inode_xattr **xattrs = NULL; | |
63ff78b2 | 558 | int err = 0; |
355da1eb SW |
559 | int i; |
560 | ||
561 | dout("__build_xattrs() len=%d\n", | |
562 | ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0); | |
563 | ||
564 | if (ci->i_xattrs.index_version >= ci->i_xattrs.version) | |
565 | return 0; /* already built */ | |
566 | ||
567 | __ceph_destroy_xattrs(ci); | |
568 | ||
569 | start: | |
570 | /* updated internal xattr rb tree */ | |
571 | if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) { | |
572 | p = ci->i_xattrs.blob->vec.iov_base; | |
573 | end = p + ci->i_xattrs.blob->vec.iov_len; | |
574 | ceph_decode_32_safe(&p, end, numattr, bad); | |
575 | xattr_version = ci->i_xattrs.version; | |
be655596 | 576 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 577 | |
7e8a2952 | 578 | xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *), |
355da1eb SW |
579 | GFP_NOFS); |
580 | err = -ENOMEM; | |
581 | if (!xattrs) | |
582 | goto bad_lock; | |
1a295bd8 | 583 | |
355da1eb SW |
584 | for (i = 0; i < numattr; i++) { |
585 | xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr), | |
586 | GFP_NOFS); | |
587 | if (!xattrs[i]) | |
588 | goto bad_lock; | |
589 | } | |
590 | ||
be655596 | 591 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
592 | if (ci->i_xattrs.version != xattr_version) { |
593 | /* lost a race, retry */ | |
594 | for (i = 0; i < numattr; i++) | |
595 | kfree(xattrs[i]); | |
596 | kfree(xattrs); | |
21ec6ffa | 597 | xattrs = NULL; |
355da1eb SW |
598 | goto start; |
599 | } | |
600 | err = -EIO; | |
601 | while (numattr--) { | |
602 | ceph_decode_32_safe(&p, end, len, bad); | |
603 | namelen = len; | |
604 | name = p; | |
605 | p += len; | |
606 | ceph_decode_32_safe(&p, end, len, bad); | |
607 | val = p; | |
608 | p += len; | |
609 | ||
610 | err = __set_xattr(ci, name, namelen, val, len, | |
fbc0b970 | 611 | 0, 0, &xattrs[numattr]); |
355da1eb SW |
612 | |
613 | if (err < 0) | |
614 | goto bad; | |
615 | } | |
616 | kfree(xattrs); | |
617 | } | |
618 | ci->i_xattrs.index_version = ci->i_xattrs.version; | |
619 | ci->i_xattrs.dirty = false; | |
620 | ||
621 | return err; | |
622 | bad_lock: | |
be655596 | 623 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
624 | bad: |
625 | if (xattrs) { | |
626 | for (i = 0; i < numattr; i++) | |
627 | kfree(xattrs[i]); | |
628 | kfree(xattrs); | |
629 | } | |
630 | ci->i_xattrs.names_size = 0; | |
631 | return err; | |
632 | } | |
633 | ||
634 | static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size, | |
635 | int val_size) | |
636 | { | |
637 | /* | |
638 | * 4 bytes for the length, and additional 4 bytes per each xattr name, | |
639 | * 4 bytes per each value | |
640 | */ | |
641 | int size = 4 + ci->i_xattrs.count*(4 + 4) + | |
642 | ci->i_xattrs.names_size + | |
643 | ci->i_xattrs.vals_size; | |
644 | dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n", | |
645 | ci->i_xattrs.count, ci->i_xattrs.names_size, | |
646 | ci->i_xattrs.vals_size); | |
647 | ||
648 | if (name_size) | |
649 | size += 4 + 4 + name_size + val_size; | |
650 | ||
651 | return size; | |
652 | } | |
653 | ||
654 | /* | |
655 | * If there are dirty xattrs, reencode xattrs into the prealloc_blob | |
656 | * and swap into place. | |
657 | */ | |
658 | void __ceph_build_xattrs_blob(struct ceph_inode_info *ci) | |
659 | { | |
660 | struct rb_node *p; | |
661 | struct ceph_inode_xattr *xattr = NULL; | |
662 | void *dest; | |
663 | ||
664 | dout("__build_xattrs_blob %p\n", &ci->vfs_inode); | |
665 | if (ci->i_xattrs.dirty) { | |
666 | int need = __get_required_blob_size(ci, 0, 0); | |
667 | ||
668 | BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len); | |
669 | ||
670 | p = rb_first(&ci->i_xattrs.index); | |
671 | dest = ci->i_xattrs.prealloc_blob->vec.iov_base; | |
672 | ||
673 | ceph_encode_32(&dest, ci->i_xattrs.count); | |
674 | while (p) { | |
675 | xattr = rb_entry(p, struct ceph_inode_xattr, node); | |
676 | ||
677 | ceph_encode_32(&dest, xattr->name_len); | |
678 | memcpy(dest, xattr->name, xattr->name_len); | |
679 | dest += xattr->name_len; | |
680 | ceph_encode_32(&dest, xattr->val_len); | |
681 | memcpy(dest, xattr->val, xattr->val_len); | |
682 | dest += xattr->val_len; | |
683 | ||
684 | p = rb_next(p); | |
685 | } | |
686 | ||
687 | /* adjust buffer len; it may be larger than we need */ | |
688 | ci->i_xattrs.prealloc_blob->vec.iov_len = | |
689 | dest - ci->i_xattrs.prealloc_blob->vec.iov_base; | |
690 | ||
b6c1d5b8 SW |
691 | if (ci->i_xattrs.blob) |
692 | ceph_buffer_put(ci->i_xattrs.blob); | |
355da1eb SW |
693 | ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob; |
694 | ci->i_xattrs.prealloc_blob = NULL; | |
695 | ci->i_xattrs.dirty = false; | |
4a625be4 | 696 | ci->i_xattrs.version++; |
355da1eb SW |
697 | } |
698 | } | |
699 | ||
315f2408 YZ |
700 | static inline int __get_request_mask(struct inode *in) { |
701 | struct ceph_mds_request *req = current->journal_info; | |
702 | int mask = 0; | |
703 | if (req && req->r_target_inode == in) { | |
704 | if (req->r_op == CEPH_MDS_OP_LOOKUP || | |
705 | req->r_op == CEPH_MDS_OP_LOOKUPINO || | |
706 | req->r_op == CEPH_MDS_OP_LOOKUPPARENT || | |
707 | req->r_op == CEPH_MDS_OP_GETATTR) { | |
708 | mask = le32_to_cpu(req->r_args.getattr.mask); | |
709 | } else if (req->r_op == CEPH_MDS_OP_OPEN || | |
710 | req->r_op == CEPH_MDS_OP_CREATE) { | |
711 | mask = le32_to_cpu(req->r_args.open.mask); | |
712 | } | |
713 | } | |
714 | return mask; | |
715 | } | |
716 | ||
7221fe4c | 717 | ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value, |
355da1eb SW |
718 | size_t size) |
719 | { | |
355da1eb | 720 | struct ceph_inode_info *ci = ceph_inode(inode); |
355da1eb | 721 | struct ceph_inode_xattr *xattr; |
881a5fa2 | 722 | struct ceph_vxattr *vxattr = NULL; |
315f2408 YZ |
723 | int req_mask; |
724 | int err; | |
355da1eb | 725 | |
0bee82fb SW |
726 | /* let's see if a virtual xattr was requested */ |
727 | vxattr = ceph_match_vxattr(inode, name); | |
29dccfa5 YZ |
728 | if (vxattr) { |
729 | err = -ENODATA; | |
730 | if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) | |
731 | err = vxattr->getxattr_cb(ci, value, size); | |
a1dc1937 | 732 | return err; |
0bee82fb SW |
733 | } |
734 | ||
315f2408 YZ |
735 | req_mask = __get_request_mask(inode); |
736 | ||
a1dc1937 | 737 | spin_lock(&ci->i_ceph_lock); |
738 | dout("getxattr %p ver=%lld index_ver=%lld\n", inode, | |
739 | ci->i_xattrs.version, ci->i_xattrs.index_version); | |
740 | ||
508b32d8 | 741 | if (ci->i_xattrs.version == 0 || |
315f2408 YZ |
742 | !((req_mask & CEPH_CAP_XATTR_SHARED) || |
743 | __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) { | |
be655596 | 744 | spin_unlock(&ci->i_ceph_lock); |
315f2408 YZ |
745 | |
746 | /* security module gets xattr while filling trace */ | |
747 | if (current->journal_info != NULL) { | |
748 | pr_warn_ratelimited("sync getxattr %p " | |
749 | "during filling trace\n", inode); | |
750 | return -EBUSY; | |
751 | } | |
752 | ||
355da1eb | 753 | /* get xattrs from mds (if we don't already have them) */ |
508b32d8 | 754 | err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true); |
355da1eb SW |
755 | if (err) |
756 | return err; | |
508b32d8 | 757 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
758 | } |
759 | ||
355da1eb SW |
760 | err = __build_xattrs(inode); |
761 | if (err < 0) | |
762 | goto out; | |
763 | ||
355da1eb SW |
764 | err = -ENODATA; /* == ENOATTR */ |
765 | xattr = __get_xattr(ci, name); | |
0bee82fb | 766 | if (!xattr) |
355da1eb | 767 | goto out; |
355da1eb SW |
768 | |
769 | err = -ERANGE; | |
770 | if (size && size < xattr->val_len) | |
771 | goto out; | |
772 | ||
773 | err = xattr->val_len; | |
774 | if (size == 0) | |
775 | goto out; | |
776 | ||
777 | memcpy(value, xattr->val, xattr->val_len); | |
778 | ||
315f2408 YZ |
779 | if (current->journal_info != NULL && |
780 | !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) | |
781 | ci->i_ceph_flags |= CEPH_I_SEC_INITED; | |
355da1eb | 782 | out: |
be655596 | 783 | spin_unlock(&ci->i_ceph_lock); |
355da1eb SW |
784 | return err; |
785 | } | |
786 | ||
787 | ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size) | |
788 | { | |
2b0143b5 | 789 | struct inode *inode = d_inode(dentry); |
355da1eb | 790 | struct ceph_inode_info *ci = ceph_inode(inode); |
881a5fa2 | 791 | struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode); |
355da1eb SW |
792 | u32 vir_namelen = 0; |
793 | u32 namelen; | |
794 | int err; | |
795 | u32 len; | |
796 | int i; | |
797 | ||
be655596 | 798 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
799 | dout("listxattr %p ver=%lld index_ver=%lld\n", inode, |
800 | ci->i_xattrs.version, ci->i_xattrs.index_version); | |
801 | ||
508b32d8 YZ |
802 | if (ci->i_xattrs.version == 0 || |
803 | !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) { | |
be655596 | 804 | spin_unlock(&ci->i_ceph_lock); |
508b32d8 | 805 | err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true); |
355da1eb SW |
806 | if (err) |
807 | return err; | |
508b32d8 | 808 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
809 | } |
810 | ||
355da1eb SW |
811 | err = __build_xattrs(inode); |
812 | if (err < 0) | |
813 | goto out; | |
3ce6cd12 AE |
814 | /* |
815 | * Start with virtual dir xattr names (if any) (including | |
816 | * terminating '\0' characters for each). | |
817 | */ | |
818 | vir_namelen = ceph_vxattrs_name_size(vxattrs); | |
819 | ||
355da1eb | 820 | /* adding 1 byte per each variable due to the null termination */ |
b65917dd | 821 | namelen = ci->i_xattrs.names_size + ci->i_xattrs.count; |
355da1eb | 822 | err = -ERANGE; |
b65917dd | 823 | if (size && vir_namelen + namelen > size) |
355da1eb SW |
824 | goto out; |
825 | ||
b65917dd | 826 | err = namelen + vir_namelen; |
355da1eb SW |
827 | if (size == 0) |
828 | goto out; | |
829 | ||
830 | names = __copy_xattr_names(ci, names); | |
831 | ||
832 | /* virtual xattr names, too */ | |
b65917dd SW |
833 | err = namelen; |
834 | if (vxattrs) { | |
355da1eb | 835 | for (i = 0; vxattrs[i].name; i++) { |
b65917dd SW |
836 | if (!vxattrs[i].hidden && |
837 | !(vxattrs[i].exists_cb && | |
838 | !vxattrs[i].exists_cb(ci))) { | |
839 | len = sprintf(names, "%s", vxattrs[i].name); | |
840 | names += len + 1; | |
841 | err += len + 1; | |
842 | } | |
355da1eb | 843 | } |
b65917dd | 844 | } |
355da1eb SW |
845 | |
846 | out: | |
be655596 | 847 | spin_unlock(&ci->i_ceph_lock); |
355da1eb SW |
848 | return err; |
849 | } | |
850 | ||
a26fecca | 851 | static int ceph_sync_setxattr(struct inode *inode, const char *name, |
355da1eb SW |
852 | const char *value, size_t size, int flags) |
853 | { | |
a26fecca | 854 | struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); |
355da1eb | 855 | struct ceph_inode_info *ci = ceph_inode(inode); |
355da1eb | 856 | struct ceph_mds_request *req; |
3d14c5d2 | 857 | struct ceph_mds_client *mdsc = fsc->mdsc; |
25e6bae3 | 858 | struct ceph_pagelist *pagelist = NULL; |
04303d8a | 859 | int op = CEPH_MDS_OP_SETXATTR; |
355da1eb | 860 | int err; |
25e6bae3 | 861 | |
0aeff37a | 862 | if (size > 0) { |
25e6bae3 YZ |
863 | /* copy value into pagelist */ |
864 | pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS); | |
865 | if (!pagelist) | |
355da1eb | 866 | return -ENOMEM; |
25e6bae3 YZ |
867 | |
868 | ceph_pagelist_init(pagelist); | |
869 | err = ceph_pagelist_append(pagelist, value, size); | |
870 | if (err) | |
871 | goto out; | |
0aeff37a | 872 | } else if (!value) { |
04303d8a YZ |
873 | if (flags & CEPH_XATTR_REPLACE) |
874 | op = CEPH_MDS_OP_RMXATTR; | |
875 | else | |
876 | flags |= CEPH_XATTR_REMOVE; | |
355da1eb SW |
877 | } |
878 | ||
879 | dout("setxattr value=%.*s\n", (int)size, value); | |
880 | ||
881 | /* do request */ | |
04303d8a | 882 | req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
60d87733 JL |
883 | if (IS_ERR(req)) { |
884 | err = PTR_ERR(req); | |
885 | goto out; | |
886 | } | |
a149bb9a | 887 | |
355da1eb | 888 | req->r_path2 = kstrdup(name, GFP_NOFS); |
a149bb9a SK |
889 | if (!req->r_path2) { |
890 | ceph_mdsc_put_request(req); | |
891 | err = -ENOMEM; | |
892 | goto out; | |
893 | } | |
355da1eb | 894 | |
04303d8a YZ |
895 | if (op == CEPH_MDS_OP_SETXATTR) { |
896 | req->r_args.setxattr.flags = cpu_to_le32(flags); | |
897 | req->r_pagelist = pagelist; | |
898 | pagelist = NULL; | |
899 | } | |
355da1eb | 900 | |
a149bb9a SK |
901 | req->r_inode = inode; |
902 | ihold(inode); | |
903 | req->r_num_caps = 1; | |
904 | req->r_inode_drop = CEPH_CAP_XATTR_SHARED; | |
905 | ||
355da1eb | 906 | dout("xattr.ver (before): %lld\n", ci->i_xattrs.version); |
752c8bdc | 907 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
355da1eb SW |
908 | ceph_mdsc_put_request(req); |
909 | dout("xattr.ver (after): %lld\n", ci->i_xattrs.version); | |
910 | ||
911 | out: | |
25e6bae3 YZ |
912 | if (pagelist) |
913 | ceph_pagelist_release(pagelist); | |
355da1eb SW |
914 | return err; |
915 | } | |
916 | ||
a26fecca | 917 | int __ceph_setxattr(struct inode *inode, const char *name, |
7221fe4c | 918 | const void *value, size_t size, int flags) |
355da1eb | 919 | { |
881a5fa2 | 920 | struct ceph_vxattr *vxattr; |
355da1eb | 921 | struct ceph_inode_info *ci = ceph_inode(inode); |
a26fecca | 922 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
f66fd9f0 | 923 | struct ceph_cap_flush *prealloc_cf = NULL; |
18fa8b3f | 924 | int issued; |
355da1eb | 925 | int err; |
fbc0b970 | 926 | int dirty = 0; |
355da1eb SW |
927 | int name_len = strlen(name); |
928 | int val_len = size; | |
929 | char *newname = NULL; | |
930 | char *newval = NULL; | |
931 | struct ceph_inode_xattr *xattr = NULL; | |
355da1eb | 932 | int required_blob_size; |
604d1b02 | 933 | bool lock_snap_rwsem = false; |
355da1eb | 934 | |
2cdeb1e4 AG |
935 | if (ceph_snap(inode) != CEPH_NOSNAP) |
936 | return -EROFS; | |
355da1eb | 937 | |
06476a69 AE |
938 | vxattr = ceph_match_vxattr(inode, name); |
939 | if (vxattr && vxattr->readonly) | |
940 | return -EOPNOTSUPP; | |
355da1eb | 941 | |
3adf654d SW |
942 | /* pass any unhandled ceph.* xattrs through to the MDS */ |
943 | if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN)) | |
944 | goto do_sync_unlocked; | |
945 | ||
355da1eb SW |
946 | /* preallocate memory for xattr name, value, index node */ |
947 | err = -ENOMEM; | |
61413c2f | 948 | newname = kmemdup(name, name_len + 1, GFP_NOFS); |
355da1eb SW |
949 | if (!newname) |
950 | goto out; | |
355da1eb SW |
951 | |
952 | if (val_len) { | |
b829c195 | 953 | newval = kmemdup(value, val_len, GFP_NOFS); |
355da1eb SW |
954 | if (!newval) |
955 | goto out; | |
355da1eb SW |
956 | } |
957 | ||
958 | xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS); | |
959 | if (!xattr) | |
960 | goto out; | |
961 | ||
f66fd9f0 YZ |
962 | prealloc_cf = ceph_alloc_cap_flush(); |
963 | if (!prealloc_cf) | |
964 | goto out; | |
965 | ||
be655596 | 966 | spin_lock(&ci->i_ceph_lock); |
355da1eb SW |
967 | retry: |
968 | issued = __ceph_caps_issued(ci, NULL); | |
508b32d8 | 969 | if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) |
355da1eb | 970 | goto do_sync; |
604d1b02 YZ |
971 | |
972 | if (!lock_snap_rwsem && !ci->i_head_snapc) { | |
973 | lock_snap_rwsem = true; | |
974 | if (!down_read_trylock(&mdsc->snap_rwsem)) { | |
975 | spin_unlock(&ci->i_ceph_lock); | |
976 | down_read(&mdsc->snap_rwsem); | |
977 | spin_lock(&ci->i_ceph_lock); | |
978 | goto retry; | |
979 | } | |
980 | } | |
981 | ||
982 | dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued)); | |
355da1eb SW |
983 | __build_xattrs(inode); |
984 | ||
985 | required_blob_size = __get_required_blob_size(ci, name_len, val_len); | |
986 | ||
987 | if (!ci->i_xattrs.prealloc_blob || | |
988 | required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) { | |
18fa8b3f | 989 | struct ceph_buffer *blob; |
355da1eb | 990 | |
be655596 | 991 | spin_unlock(&ci->i_ceph_lock); |
355da1eb | 992 | dout(" preaallocating new blob size=%d\n", required_blob_size); |
b6c1d5b8 | 993 | blob = ceph_buffer_new(required_blob_size, GFP_NOFS); |
355da1eb | 994 | if (!blob) |
604d1b02 | 995 | goto do_sync_unlocked; |
be655596 | 996 | spin_lock(&ci->i_ceph_lock); |
b6c1d5b8 SW |
997 | if (ci->i_xattrs.prealloc_blob) |
998 | ceph_buffer_put(ci->i_xattrs.prealloc_blob); | |
355da1eb SW |
999 | ci->i_xattrs.prealloc_blob = blob; |
1000 | goto retry; | |
1001 | } | |
1002 | ||
bcdfeb2e YZ |
1003 | err = __set_xattr(ci, newname, name_len, newval, val_len, |
1004 | flags, value ? 1 : -1, &xattr); | |
18fa8b3f | 1005 | |
fbc0b970 | 1006 | if (!err) { |
f66fd9f0 YZ |
1007 | dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL, |
1008 | &prealloc_cf); | |
fbc0b970 | 1009 | ci->i_xattrs.dirty = true; |
8bbd4714 | 1010 | inode->i_ctime = current_fs_time(inode->i_sb); |
fbc0b970 | 1011 | } |
18fa8b3f | 1012 | |
be655596 | 1013 | spin_unlock(&ci->i_ceph_lock); |
604d1b02 YZ |
1014 | if (lock_snap_rwsem) |
1015 | up_read(&mdsc->snap_rwsem); | |
fca65b4a SW |
1016 | if (dirty) |
1017 | __mark_inode_dirty(inode, dirty); | |
f66fd9f0 | 1018 | ceph_free_cap_flush(prealloc_cf); |
355da1eb SW |
1019 | return err; |
1020 | ||
1021 | do_sync: | |
be655596 | 1022 | spin_unlock(&ci->i_ceph_lock); |
3adf654d | 1023 | do_sync_unlocked: |
604d1b02 YZ |
1024 | if (lock_snap_rwsem) |
1025 | up_read(&mdsc->snap_rwsem); | |
315f2408 YZ |
1026 | |
1027 | /* security module set xattr while filling trace */ | |
1028 | if (current->journal_info != NULL) { | |
1029 | pr_warn_ratelimited("sync setxattr %p " | |
1030 | "during filling trace\n", inode); | |
1031 | err = -EBUSY; | |
1032 | } else { | |
a26fecca | 1033 | err = ceph_sync_setxattr(inode, name, value, size, flags); |
315f2408 | 1034 | } |
355da1eb | 1035 | out: |
f66fd9f0 | 1036 | ceph_free_cap_flush(prealloc_cf); |
355da1eb SW |
1037 | kfree(newname); |
1038 | kfree(newval); | |
1039 | kfree(xattr); | |
1040 | return err; | |
1041 | } | |
1042 | ||
2cdeb1e4 AG |
1043 | static int ceph_get_xattr_handler(const struct xattr_handler *handler, |
1044 | struct dentry *dentry, struct inode *inode, | |
1045 | const char *name, void *value, size_t size) | |
7221fe4c | 1046 | { |
2cdeb1e4 AG |
1047 | if (!ceph_is_valid_xattr(name)) |
1048 | return -EOPNOTSUPP; | |
1049 | return __ceph_getxattr(inode, name, value, size); | |
1050 | } | |
7221fe4c | 1051 | |
2cdeb1e4 | 1052 | static int ceph_set_xattr_handler(const struct xattr_handler *handler, |
59301226 AV |
1053 | struct dentry *unused, struct inode *inode, |
1054 | const char *name, const void *value, | |
1055 | size_t size, int flags) | |
2cdeb1e4 AG |
1056 | { |
1057 | if (!ceph_is_valid_xattr(name)) | |
1058 | return -EOPNOTSUPP; | |
59301226 | 1059 | return __ceph_setxattr(inode, name, value, size, flags); |
7221fe4c | 1060 | } |
315f2408 | 1061 | |
2cdeb1e4 AG |
1062 | const struct xattr_handler ceph_other_xattr_handler = { |
1063 | .prefix = "", /* match any name => handlers called with full name */ | |
1064 | .get = ceph_get_xattr_handler, | |
1065 | .set = ceph_set_xattr_handler, | |
1066 | }; | |
1067 | ||
315f2408 YZ |
1068 | #ifdef CONFIG_SECURITY |
1069 | bool ceph_security_xattr_wanted(struct inode *in) | |
1070 | { | |
1071 | return in->i_security != NULL; | |
1072 | } | |
1073 | ||
1074 | bool ceph_security_xattr_deadlock(struct inode *in) | |
1075 | { | |
1076 | struct ceph_inode_info *ci; | |
1077 | bool ret; | |
1078 | if (in->i_security == NULL) | |
1079 | return false; | |
1080 | ci = ceph_inode(in); | |
1081 | spin_lock(&ci->i_ceph_lock); | |
1082 | ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) && | |
1083 | !(ci->i_xattrs.version > 0 && | |
1084 | __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0)); | |
1085 | spin_unlock(&ci->i_ceph_lock); | |
1086 | return ret; | |
1087 | } | |
1088 | #endif |