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