1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3 #include <linux/ceph/pagelist.h>
6 #include "mds_client.h"
8 #include <linux/ceph/decode.h>
10 #include <linux/xattr.h>
11 #include <linux/security.h>
12 #include <linux/posix_acl_xattr.h>
13 #include <linux/slab.h>
15 #define XATTR_CEPH_PREFIX "ceph."
16 #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
18 static int __remove_xattr(struct ceph_inode_info
*ci
,
19 struct ceph_inode_xattr
*xattr
);
21 static bool ceph_is_valid_xattr(const char *name
)
23 return !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) ||
24 !strncmp(name
, XATTR_CEPH_PREFIX
, XATTR_CEPH_PREFIX_LEN
) ||
25 !strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
26 !strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
);
30 * These define virtual xattrs exposing the recursive directory
31 * statistics and layout metadata.
35 size_t name_size
; /* strlen(name) + 1 (for '\0') */
36 ssize_t (*getxattr_cb
)(struct ceph_inode_info
*ci
, char *val
,
38 bool (*exists_cb
)(struct ceph_inode_info
*ci
);
42 #define VXATTR_FLAG_READONLY (1<<0)
43 #define VXATTR_FLAG_HIDDEN (1<<1)
44 #define VXATTR_FLAG_RSTAT (1<<2)
45 #define VXATTR_FLAG_DIRSTAT (1<<3)
49 static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info
*ci
)
51 struct ceph_file_layout
*fl
= &ci
->i_layout
;
52 return (fl
->stripe_unit
> 0 || fl
->stripe_count
> 0 ||
53 fl
->object_size
> 0 || fl
->pool_id
>= 0 ||
54 rcu_dereference_raw(fl
->pool_ns
) != NULL
);
57 static ssize_t
ceph_vxattrcb_layout(struct ceph_inode_info
*ci
, char *val
,
60 struct ceph_fs_client
*fsc
= ceph_sb_to_client(ci
->vfs_inode
.i_sb
);
61 struct ceph_osd_client
*osdc
= &fsc
->client
->osdc
;
62 struct ceph_string
*pool_ns
;
63 s64 pool
= ci
->i_layout
.pool_id
;
64 const char *pool_name
;
65 const char *ns_field
= " pool_namespace=";
67 size_t len
, total_len
= 0;
70 pool_ns
= ceph_try_get_string(ci
->i_layout
.pool_ns
);
72 dout("ceph_vxattrcb_layout %p\n", &ci
->vfs_inode
);
73 down_read(&osdc
->lock
);
74 pool_name
= ceph_pg_pool_name_by_id(osdc
->osdmap
, pool
);
76 len
= snprintf(buf
, sizeof(buf
),
77 "stripe_unit=%u stripe_count=%u object_size=%u pool=",
78 ci
->i_layout
.stripe_unit
, ci
->i_layout
.stripe_count
,
79 ci
->i_layout
.object_size
);
80 total_len
= len
+ strlen(pool_name
);
82 len
= snprintf(buf
, sizeof(buf
),
83 "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
84 ci
->i_layout
.stripe_unit
, ci
->i_layout
.stripe_count
,
85 ci
->i_layout
.object_size
, pool
);
90 total_len
+= strlen(ns_field
) + pool_ns
->len
;
93 if (size
>= total_len
) {
94 memcpy(val
, buf
, len
);
97 len
= strlen(pool_name
);
98 memcpy(val
+ ret
, pool_name
, len
);
102 len
= strlen(ns_field
);
103 memcpy(val
+ ret
, ns_field
, len
);
105 memcpy(val
+ ret
, pool_ns
->str
, pool_ns
->len
);
109 up_read(&osdc
->lock
);
110 ceph_put_string(pool_ns
);
115 * The convention with strings in xattrs is that they should not be NULL
116 * terminated, since we're returning the length with them. snprintf always
117 * NULL terminates however, so call it on a temporary buffer and then memcpy
118 * the result into place.
120 static __printf(3, 4)
121 int ceph_fmt_xattr(char *val
, size_t size
, const char *fmt
, ...)
125 char buf
[96]; /* NB: reevaluate size if new vxattrs are added */
128 ret
= vsnprintf(buf
, size
? sizeof(buf
) : 0, fmt
, args
);
132 if (size
&& ret
+ 1 > sizeof(buf
)) {
133 WARN_ONCE(true, "Returned length too big (%d)", ret
);
138 memcpy(val
, buf
, ret
);
142 static ssize_t
ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info
*ci
,
143 char *val
, size_t size
)
145 return ceph_fmt_xattr(val
, size
, "%u", ci
->i_layout
.stripe_unit
);
148 static ssize_t
ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info
*ci
,
149 char *val
, size_t size
)
151 return ceph_fmt_xattr(val
, size
, "%u", ci
->i_layout
.stripe_count
);
154 static ssize_t
ceph_vxattrcb_layout_object_size(struct ceph_inode_info
*ci
,
155 char *val
, size_t size
)
157 return ceph_fmt_xattr(val
, size
, "%u", ci
->i_layout
.object_size
);
160 static ssize_t
ceph_vxattrcb_layout_pool(struct ceph_inode_info
*ci
,
161 char *val
, size_t size
)
164 struct ceph_fs_client
*fsc
= ceph_sb_to_client(ci
->vfs_inode
.i_sb
);
165 struct ceph_osd_client
*osdc
= &fsc
->client
->osdc
;
166 s64 pool
= ci
->i_layout
.pool_id
;
167 const char *pool_name
;
169 down_read(&osdc
->lock
);
170 pool_name
= ceph_pg_pool_name_by_id(osdc
->osdmap
, pool
);
172 ret
= strlen(pool_name
);
174 memcpy(val
, pool_name
, ret
);
176 ret
= ceph_fmt_xattr(val
, size
, "%lld", pool
);
178 up_read(&osdc
->lock
);
182 static ssize_t
ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info
*ci
,
183 char *val
, size_t size
)
186 struct ceph_string
*ns
= ceph_try_get_string(ci
->i_layout
.pool_ns
);
191 memcpy(val
, ns
->str
, ret
);
199 static ssize_t
ceph_vxattrcb_dir_entries(struct ceph_inode_info
*ci
, char *val
,
202 return ceph_fmt_xattr(val
, size
, "%lld", ci
->i_files
+ ci
->i_subdirs
);
205 static ssize_t
ceph_vxattrcb_dir_files(struct ceph_inode_info
*ci
, char *val
,
208 return ceph_fmt_xattr(val
, size
, "%lld", ci
->i_files
);
211 static ssize_t
ceph_vxattrcb_dir_subdirs(struct ceph_inode_info
*ci
, char *val
,
214 return ceph_fmt_xattr(val
, size
, "%lld", ci
->i_subdirs
);
217 static ssize_t
ceph_vxattrcb_dir_rentries(struct ceph_inode_info
*ci
, char *val
,
220 return ceph_fmt_xattr(val
, size
, "%lld",
221 ci
->i_rfiles
+ ci
->i_rsubdirs
);
224 static ssize_t
ceph_vxattrcb_dir_rfiles(struct ceph_inode_info
*ci
, char *val
,
227 return ceph_fmt_xattr(val
, size
, "%lld", ci
->i_rfiles
);
230 static ssize_t
ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info
*ci
, char *val
,
233 return ceph_fmt_xattr(val
, size
, "%lld", ci
->i_rsubdirs
);
236 static ssize_t
ceph_vxattrcb_dir_rsnaps(struct ceph_inode_info
*ci
, char *val
,
239 return ceph_fmt_xattr(val
, size
, "%lld", ci
->i_rsnaps
);
242 static ssize_t
ceph_vxattrcb_dir_rbytes(struct ceph_inode_info
*ci
, char *val
,
245 return ceph_fmt_xattr(val
, size
, "%lld", ci
->i_rbytes
);
248 static ssize_t
ceph_vxattrcb_dir_rctime(struct ceph_inode_info
*ci
, char *val
,
251 return ceph_fmt_xattr(val
, size
, "%lld.%09ld", ci
->i_rctime
.tv_sec
,
252 ci
->i_rctime
.tv_nsec
);
256 static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info
*ci
)
258 return ci
->i_dir_pin
!= -ENODATA
;
261 static ssize_t
ceph_vxattrcb_dir_pin(struct ceph_inode_info
*ci
, char *val
,
264 return ceph_fmt_xattr(val
, size
, "%d", (int)ci
->i_dir_pin
);
268 static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info
*ci
)
271 spin_lock(&ci
->i_ceph_lock
);
272 if ((ci
->i_max_files
|| ci
->i_max_bytes
) &&
273 ci
->i_vino
.snap
== CEPH_NOSNAP
&&
275 ci
->i_snap_realm
->ino
== ci
->i_vino
.ino
)
277 spin_unlock(&ci
->i_ceph_lock
);
281 static ssize_t
ceph_vxattrcb_quota(struct ceph_inode_info
*ci
, char *val
,
284 return ceph_fmt_xattr(val
, size
, "max_bytes=%llu max_files=%llu",
285 ci
->i_max_bytes
, ci
->i_max_files
);
288 static ssize_t
ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info
*ci
,
289 char *val
, size_t size
)
291 return ceph_fmt_xattr(val
, size
, "%llu", ci
->i_max_bytes
);
294 static ssize_t
ceph_vxattrcb_quota_max_files(struct ceph_inode_info
*ci
,
295 char *val
, size_t size
)
297 return ceph_fmt_xattr(val
, size
, "%llu", ci
->i_max_files
);
301 static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info
*ci
)
303 return (ci
->i_snap_btime
.tv_sec
!= 0 || ci
->i_snap_btime
.tv_nsec
!= 0);
306 static ssize_t
ceph_vxattrcb_snap_btime(struct ceph_inode_info
*ci
, char *val
,
309 return ceph_fmt_xattr(val
, size
, "%lld.%09ld", ci
->i_snap_btime
.tv_sec
,
310 ci
->i_snap_btime
.tv_nsec
);
313 static ssize_t
ceph_vxattrcb_cluster_fsid(struct ceph_inode_info
*ci
,
314 char *val
, size_t size
)
316 struct ceph_fs_client
*fsc
= ceph_sb_to_client(ci
->vfs_inode
.i_sb
);
318 return ceph_fmt_xattr(val
, size
, "%pU", &fsc
->client
->fsid
);
321 static ssize_t
ceph_vxattrcb_client_id(struct ceph_inode_info
*ci
,
322 char *val
, size_t size
)
324 struct ceph_fs_client
*fsc
= ceph_sb_to_client(ci
->vfs_inode
.i_sb
);
326 return ceph_fmt_xattr(val
, size
, "client%lld",
327 ceph_client_gid(fsc
->client
));
330 static ssize_t
ceph_vxattrcb_caps(struct ceph_inode_info
*ci
, char *val
,
335 spin_lock(&ci
->i_ceph_lock
);
336 issued
= __ceph_caps_issued(ci
, NULL
);
337 spin_unlock(&ci
->i_ceph_lock
);
339 return ceph_fmt_xattr(val
, size
, "%s/0x%x",
340 ceph_cap_string(issued
), issued
);
343 static ssize_t
ceph_vxattrcb_auth_mds(struct ceph_inode_info
*ci
,
344 char *val
, size_t size
)
348 spin_lock(&ci
->i_ceph_lock
);
349 ret
= ceph_fmt_xattr(val
, size
, "%d",
350 ci
->i_auth_cap
? ci
->i_auth_cap
->session
->s_mds
: -1);
351 spin_unlock(&ci
->i_ceph_lock
);
355 #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
356 #define CEPH_XATTR_NAME2(_type, _name, _name2) \
357 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
359 #define XATTR_NAME_CEPH(_type, _name, _flags) \
361 .name = CEPH_XATTR_NAME(_type, _name), \
362 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
363 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
365 .flags = (VXATTR_FLAG_READONLY | _flags), \
367 #define XATTR_RSTAT_FIELD(_type, _name) \
368 XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
369 #define XATTR_LAYOUT_FIELD(_type, _name, _field) \
371 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
372 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
373 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
374 .exists_cb = ceph_vxattrcb_layout_exists, \
375 .flags = VXATTR_FLAG_HIDDEN, \
377 #define XATTR_QUOTA_FIELD(_type, _name) \
379 .name = CEPH_XATTR_NAME(_type, _name), \
380 .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \
381 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
382 .exists_cb = ceph_vxattrcb_quota_exists, \
383 .flags = VXATTR_FLAG_HIDDEN, \
386 static struct ceph_vxattr ceph_dir_vxattrs
[] = {
388 .name
= "ceph.dir.layout",
389 .name_size
= sizeof("ceph.dir.layout"),
390 .getxattr_cb
= ceph_vxattrcb_layout
,
391 .exists_cb
= ceph_vxattrcb_layout_exists
,
392 .flags
= VXATTR_FLAG_HIDDEN
,
394 XATTR_LAYOUT_FIELD(dir
, layout
, stripe_unit
),
395 XATTR_LAYOUT_FIELD(dir
, layout
, stripe_count
),
396 XATTR_LAYOUT_FIELD(dir
, layout
, object_size
),
397 XATTR_LAYOUT_FIELD(dir
, layout
, pool
),
398 XATTR_LAYOUT_FIELD(dir
, layout
, pool_namespace
),
399 XATTR_NAME_CEPH(dir
, entries
, VXATTR_FLAG_DIRSTAT
),
400 XATTR_NAME_CEPH(dir
, files
, VXATTR_FLAG_DIRSTAT
),
401 XATTR_NAME_CEPH(dir
, subdirs
, VXATTR_FLAG_DIRSTAT
),
402 XATTR_RSTAT_FIELD(dir
, rentries
),
403 XATTR_RSTAT_FIELD(dir
, rfiles
),
404 XATTR_RSTAT_FIELD(dir
, rsubdirs
),
405 XATTR_RSTAT_FIELD(dir
, rsnaps
),
406 XATTR_RSTAT_FIELD(dir
, rbytes
),
407 XATTR_RSTAT_FIELD(dir
, rctime
),
409 .name
= "ceph.dir.pin",
410 .name_size
= sizeof("ceph.dir.pin"),
411 .getxattr_cb
= ceph_vxattrcb_dir_pin
,
412 .exists_cb
= ceph_vxattrcb_dir_pin_exists
,
413 .flags
= VXATTR_FLAG_HIDDEN
,
416 .name
= "ceph.quota",
417 .name_size
= sizeof("ceph.quota"),
418 .getxattr_cb
= ceph_vxattrcb_quota
,
419 .exists_cb
= ceph_vxattrcb_quota_exists
,
420 .flags
= VXATTR_FLAG_HIDDEN
,
422 XATTR_QUOTA_FIELD(quota
, max_bytes
),
423 XATTR_QUOTA_FIELD(quota
, max_files
),
425 .name
= "ceph.snap.btime",
426 .name_size
= sizeof("ceph.snap.btime"),
427 .getxattr_cb
= ceph_vxattrcb_snap_btime
,
428 .exists_cb
= ceph_vxattrcb_snap_btime_exists
,
429 .flags
= VXATTR_FLAG_READONLY
,
433 .name_size
= sizeof("ceph.caps"),
434 .getxattr_cb
= ceph_vxattrcb_caps
,
436 .flags
= VXATTR_FLAG_HIDDEN
,
438 { .name
= NULL
, 0 } /* Required table terminator */
443 static struct ceph_vxattr ceph_file_vxattrs
[] = {
445 .name
= "ceph.file.layout",
446 .name_size
= sizeof("ceph.file.layout"),
447 .getxattr_cb
= ceph_vxattrcb_layout
,
448 .exists_cb
= ceph_vxattrcb_layout_exists
,
449 .flags
= VXATTR_FLAG_HIDDEN
,
451 XATTR_LAYOUT_FIELD(file
, layout
, stripe_unit
),
452 XATTR_LAYOUT_FIELD(file
, layout
, stripe_count
),
453 XATTR_LAYOUT_FIELD(file
, layout
, object_size
),
454 XATTR_LAYOUT_FIELD(file
, layout
, pool
),
455 XATTR_LAYOUT_FIELD(file
, layout
, pool_namespace
),
457 .name
= "ceph.snap.btime",
458 .name_size
= sizeof("ceph.snap.btime"),
459 .getxattr_cb
= ceph_vxattrcb_snap_btime
,
460 .exists_cb
= ceph_vxattrcb_snap_btime_exists
,
461 .flags
= VXATTR_FLAG_READONLY
,
465 .name_size
= sizeof("ceph.caps"),
466 .getxattr_cb
= ceph_vxattrcb_caps
,
468 .flags
= VXATTR_FLAG_HIDDEN
,
470 { .name
= NULL
, 0 } /* Required table terminator */
473 static struct ceph_vxattr ceph_common_vxattrs
[] = {
475 .name
= "ceph.cluster_fsid",
476 .name_size
= sizeof("ceph.cluster_fsid"),
477 .getxattr_cb
= ceph_vxattrcb_cluster_fsid
,
479 .flags
= VXATTR_FLAG_READONLY
,
482 .name
= "ceph.client_id",
483 .name_size
= sizeof("ceph.client_id"),
484 .getxattr_cb
= ceph_vxattrcb_client_id
,
486 .flags
= VXATTR_FLAG_READONLY
,
489 .name
= "ceph.auth_mds",
490 .name_size
= sizeof("ceph.auth_mds"),
491 .getxattr_cb
= ceph_vxattrcb_auth_mds
,
493 .flags
= VXATTR_FLAG_READONLY
,
495 { .name
= NULL
, 0 } /* Required table terminator */
498 static struct ceph_vxattr
*ceph_inode_vxattrs(struct inode
*inode
)
500 if (S_ISDIR(inode
->i_mode
))
501 return ceph_dir_vxattrs
;
502 else if (S_ISREG(inode
->i_mode
))
503 return ceph_file_vxattrs
;
507 static struct ceph_vxattr
*ceph_match_vxattr(struct inode
*inode
,
510 struct ceph_vxattr
*vxattr
= ceph_inode_vxattrs(inode
);
513 while (vxattr
->name
) {
514 if (!strcmp(vxattr
->name
, name
))
520 vxattr
= ceph_common_vxattrs
;
521 while (vxattr
->name
) {
522 if (!strcmp(vxattr
->name
, name
))
530 static int __set_xattr(struct ceph_inode_info
*ci
,
531 const char *name
, int name_len
,
532 const char *val
, int val_len
,
533 int flags
, int update_xattr
,
534 struct ceph_inode_xattr
**newxattr
)
537 struct rb_node
*parent
= NULL
;
538 struct ceph_inode_xattr
*xattr
= NULL
;
542 p
= &ci
->i_xattrs
.index
.rb_node
;
545 xattr
= rb_entry(parent
, struct ceph_inode_xattr
, node
);
546 c
= strncmp(name
, xattr
->name
, min(name_len
, xattr
->name_len
));
552 if (name_len
== xattr
->name_len
)
554 else if (name_len
< xattr
->name_len
)
565 if (xattr
&& (flags
& XATTR_CREATE
))
567 else if (!xattr
&& (flags
& XATTR_REPLACE
))
575 if (update_xattr
< 0) {
577 __remove_xattr(ci
, xattr
);
588 xattr
->name_len
= name_len
;
589 xattr
->should_free_name
= update_xattr
;
591 ci
->i_xattrs
.count
++;
592 dout("__set_xattr count=%d\n", ci
->i_xattrs
.count
);
596 if (xattr
->should_free_val
)
603 ci
->i_xattrs
.names_size
-= xattr
->name_len
;
604 ci
->i_xattrs
.vals_size
-= xattr
->val_len
;
606 ci
->i_xattrs
.names_size
+= name_len
;
607 ci
->i_xattrs
.vals_size
+= val_len
;
613 xattr
->val_len
= val_len
;
614 xattr
->dirty
= update_xattr
;
615 xattr
->should_free_val
= (val
&& update_xattr
);
618 rb_link_node(&xattr
->node
, parent
, p
);
619 rb_insert_color(&xattr
->node
, &ci
->i_xattrs
.index
);
620 dout("__set_xattr_val p=%p\n", p
);
623 dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s\n",
624 ceph_vinop(&ci
->vfs_inode
), xattr
, name_len
, name
, val_len
, val
);
629 static struct ceph_inode_xattr
*__get_xattr(struct ceph_inode_info
*ci
,
633 struct rb_node
*parent
= NULL
;
634 struct ceph_inode_xattr
*xattr
= NULL
;
635 int name_len
= strlen(name
);
638 p
= &ci
->i_xattrs
.index
.rb_node
;
641 xattr
= rb_entry(parent
, struct ceph_inode_xattr
, node
);
642 c
= strncmp(name
, xattr
->name
, xattr
->name_len
);
643 if (c
== 0 && name_len
> xattr
->name_len
)
650 dout("__get_xattr %s: found %.*s\n", name
,
651 xattr
->val_len
, xattr
->val
);
656 dout("__get_xattr %s: not found\n", name
);
661 static void __free_xattr(struct ceph_inode_xattr
*xattr
)
665 if (xattr
->should_free_name
)
667 if (xattr
->should_free_val
)
673 static int __remove_xattr(struct ceph_inode_info
*ci
,
674 struct ceph_inode_xattr
*xattr
)
679 rb_erase(&xattr
->node
, &ci
->i_xattrs
.index
);
681 if (xattr
->should_free_name
)
683 if (xattr
->should_free_val
)
686 ci
->i_xattrs
.names_size
-= xattr
->name_len
;
687 ci
->i_xattrs
.vals_size
-= xattr
->val_len
;
688 ci
->i_xattrs
.count
--;
694 static char *__copy_xattr_names(struct ceph_inode_info
*ci
,
698 struct ceph_inode_xattr
*xattr
= NULL
;
700 p
= rb_first(&ci
->i_xattrs
.index
);
701 dout("__copy_xattr_names count=%d\n", ci
->i_xattrs
.count
);
704 xattr
= rb_entry(p
, struct ceph_inode_xattr
, node
);
705 memcpy(dest
, xattr
->name
, xattr
->name_len
);
706 dest
[xattr
->name_len
] = '\0';
708 dout("dest=%s %p (%s) (%d/%d)\n", dest
, xattr
, xattr
->name
,
709 xattr
->name_len
, ci
->i_xattrs
.names_size
);
711 dest
+= xattr
->name_len
+ 1;
718 void __ceph_destroy_xattrs(struct ceph_inode_info
*ci
)
720 struct rb_node
*p
, *tmp
;
721 struct ceph_inode_xattr
*xattr
= NULL
;
723 p
= rb_first(&ci
->i_xattrs
.index
);
725 dout("__ceph_destroy_xattrs p=%p\n", p
);
728 xattr
= rb_entry(p
, struct ceph_inode_xattr
, node
);
731 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p
,
732 xattr
->name_len
, xattr
->name
);
733 rb_erase(tmp
, &ci
->i_xattrs
.index
);
738 ci
->i_xattrs
.names_size
= 0;
739 ci
->i_xattrs
.vals_size
= 0;
740 ci
->i_xattrs
.index_version
= 0;
741 ci
->i_xattrs
.count
= 0;
742 ci
->i_xattrs
.index
= RB_ROOT
;
745 static int __build_xattrs(struct inode
*inode
)
746 __releases(ci
->i_ceph_lock
)
747 __acquires(ci
->i_ceph_lock
)
753 const char *name
, *val
;
754 struct ceph_inode_info
*ci
= ceph_inode(inode
);
756 struct ceph_inode_xattr
**xattrs
= NULL
;
760 dout("__build_xattrs() len=%d\n",
761 ci
->i_xattrs
.blob
? (int)ci
->i_xattrs
.blob
->vec
.iov_len
: 0);
763 if (ci
->i_xattrs
.index_version
>= ci
->i_xattrs
.version
)
764 return 0; /* already built */
766 __ceph_destroy_xattrs(ci
);
769 /* updated internal xattr rb tree */
770 if (ci
->i_xattrs
.blob
&& ci
->i_xattrs
.blob
->vec
.iov_len
> 4) {
771 p
= ci
->i_xattrs
.blob
->vec
.iov_base
;
772 end
= p
+ ci
->i_xattrs
.blob
->vec
.iov_len
;
773 ceph_decode_32_safe(&p
, end
, numattr
, bad
);
774 xattr_version
= ci
->i_xattrs
.version
;
775 spin_unlock(&ci
->i_ceph_lock
);
777 xattrs
= kcalloc(numattr
, sizeof(struct ceph_inode_xattr
*),
783 for (i
= 0; i
< numattr
; i
++) {
784 xattrs
[i
] = kmalloc(sizeof(struct ceph_inode_xattr
),
790 spin_lock(&ci
->i_ceph_lock
);
791 if (ci
->i_xattrs
.version
!= xattr_version
) {
792 /* lost a race, retry */
793 for (i
= 0; i
< numattr
; i
++)
801 ceph_decode_32_safe(&p
, end
, len
, bad
);
805 ceph_decode_32_safe(&p
, end
, len
, bad
);
809 err
= __set_xattr(ci
, name
, namelen
, val
, len
,
810 0, 0, &xattrs
[numattr
]);
817 ci
->i_xattrs
.index_version
= ci
->i_xattrs
.version
;
818 ci
->i_xattrs
.dirty
= false;
822 spin_lock(&ci
->i_ceph_lock
);
825 for (i
= 0; i
< numattr
; i
++)
829 ci
->i_xattrs
.names_size
= 0;
833 static int __get_required_blob_size(struct ceph_inode_info
*ci
, int name_size
,
837 * 4 bytes for the length, and additional 4 bytes per each xattr name,
838 * 4 bytes per each value
840 int size
= 4 + ci
->i_xattrs
.count
*(4 + 4) +
841 ci
->i_xattrs
.names_size
+
842 ci
->i_xattrs
.vals_size
;
843 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
844 ci
->i_xattrs
.count
, ci
->i_xattrs
.names_size
,
845 ci
->i_xattrs
.vals_size
);
848 size
+= 4 + 4 + name_size
+ val_size
;
854 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
855 * and swap into place. It returns the old i_xattrs.blob (or NULL) so
856 * that it can be freed by the caller as the i_ceph_lock is likely to be
859 struct ceph_buffer
*__ceph_build_xattrs_blob(struct ceph_inode_info
*ci
)
862 struct ceph_inode_xattr
*xattr
= NULL
;
863 struct ceph_buffer
*old_blob
= NULL
;
866 dout("__build_xattrs_blob %p\n", &ci
->vfs_inode
);
867 if (ci
->i_xattrs
.dirty
) {
868 int need
= __get_required_blob_size(ci
, 0, 0);
870 BUG_ON(need
> ci
->i_xattrs
.prealloc_blob
->alloc_len
);
872 p
= rb_first(&ci
->i_xattrs
.index
);
873 dest
= ci
->i_xattrs
.prealloc_blob
->vec
.iov_base
;
875 ceph_encode_32(&dest
, ci
->i_xattrs
.count
);
877 xattr
= rb_entry(p
, struct ceph_inode_xattr
, node
);
879 ceph_encode_32(&dest
, xattr
->name_len
);
880 memcpy(dest
, xattr
->name
, xattr
->name_len
);
881 dest
+= xattr
->name_len
;
882 ceph_encode_32(&dest
, xattr
->val_len
);
883 memcpy(dest
, xattr
->val
, xattr
->val_len
);
884 dest
+= xattr
->val_len
;
889 /* adjust buffer len; it may be larger than we need */
890 ci
->i_xattrs
.prealloc_blob
->vec
.iov_len
=
891 dest
- ci
->i_xattrs
.prealloc_blob
->vec
.iov_base
;
893 if (ci
->i_xattrs
.blob
)
894 old_blob
= ci
->i_xattrs
.blob
;
895 ci
->i_xattrs
.blob
= ci
->i_xattrs
.prealloc_blob
;
896 ci
->i_xattrs
.prealloc_blob
= NULL
;
897 ci
->i_xattrs
.dirty
= false;
898 ci
->i_xattrs
.version
++;
904 static inline int __get_request_mask(struct inode
*in
) {
905 struct ceph_mds_request
*req
= current
->journal_info
;
907 if (req
&& req
->r_target_inode
== in
) {
908 if (req
->r_op
== CEPH_MDS_OP_LOOKUP
||
909 req
->r_op
== CEPH_MDS_OP_LOOKUPINO
||
910 req
->r_op
== CEPH_MDS_OP_LOOKUPPARENT
||
911 req
->r_op
== CEPH_MDS_OP_GETATTR
) {
912 mask
= le32_to_cpu(req
->r_args
.getattr
.mask
);
913 } else if (req
->r_op
== CEPH_MDS_OP_OPEN
||
914 req
->r_op
== CEPH_MDS_OP_CREATE
) {
915 mask
= le32_to_cpu(req
->r_args
.open
.mask
);
921 ssize_t
__ceph_getxattr(struct inode
*inode
, const char *name
, void *value
,
924 struct ceph_inode_info
*ci
= ceph_inode(inode
);
925 struct ceph_inode_xattr
*xattr
;
926 struct ceph_vxattr
*vxattr
= NULL
;
930 /* let's see if a virtual xattr was requested */
931 vxattr
= ceph_match_vxattr(inode
, name
);
934 if (vxattr
->flags
& VXATTR_FLAG_RSTAT
)
935 mask
|= CEPH_STAT_RSTAT
;
936 if (vxattr
->flags
& VXATTR_FLAG_DIRSTAT
)
937 mask
|= CEPH_CAP_FILE_SHARED
;
938 err
= ceph_do_getattr(inode
, mask
, true);
942 if (!(vxattr
->exists_cb
&& !vxattr
->exists_cb(ci
))) {
943 err
= vxattr
->getxattr_cb(ci
, value
, size
);
944 if (size
&& size
< err
)
950 req_mask
= __get_request_mask(inode
);
952 spin_lock(&ci
->i_ceph_lock
);
953 dout("getxattr %p name '%s' ver=%lld index_ver=%lld\n", inode
, name
,
954 ci
->i_xattrs
.version
, ci
->i_xattrs
.index_version
);
956 if (ci
->i_xattrs
.version
== 0 ||
957 !((req_mask
& CEPH_CAP_XATTR_SHARED
) ||
958 __ceph_caps_issued_mask_metric(ci
, CEPH_CAP_XATTR_SHARED
, 1))) {
959 spin_unlock(&ci
->i_ceph_lock
);
961 /* security module gets xattr while filling trace */
962 if (current
->journal_info
) {
963 pr_warn_ratelimited("sync getxattr %p "
964 "during filling trace\n", inode
);
968 /* get xattrs from mds (if we don't already have them) */
969 err
= ceph_do_getattr(inode
, CEPH_STAT_CAP_XATTR
, true);
972 spin_lock(&ci
->i_ceph_lock
);
975 err
= __build_xattrs(inode
);
979 err
= -ENODATA
; /* == ENOATTR */
980 xattr
= __get_xattr(ci
, name
);
985 if (size
&& size
< xattr
->val_len
)
988 err
= xattr
->val_len
;
992 memcpy(value
, xattr
->val
, xattr
->val_len
);
994 if (current
->journal_info
&&
995 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) &&
996 security_ismaclabel(name
+ XATTR_SECURITY_PREFIX_LEN
))
997 ci
->i_ceph_flags
|= CEPH_I_SEC_INITED
;
999 spin_unlock(&ci
->i_ceph_lock
);
1003 ssize_t
ceph_listxattr(struct dentry
*dentry
, char *names
, size_t size
)
1005 struct inode
*inode
= d_inode(dentry
);
1006 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1007 bool len_only
= (size
== 0);
1011 spin_lock(&ci
->i_ceph_lock
);
1012 dout("listxattr %p ver=%lld index_ver=%lld\n", inode
,
1013 ci
->i_xattrs
.version
, ci
->i_xattrs
.index_version
);
1015 if (ci
->i_xattrs
.version
== 0 ||
1016 !__ceph_caps_issued_mask_metric(ci
, CEPH_CAP_XATTR_SHARED
, 1)) {
1017 spin_unlock(&ci
->i_ceph_lock
);
1018 err
= ceph_do_getattr(inode
, CEPH_STAT_CAP_XATTR
, true);
1021 spin_lock(&ci
->i_ceph_lock
);
1024 err
= __build_xattrs(inode
);
1028 /* add 1 byte for each xattr due to the null termination */
1029 namelen
= ci
->i_xattrs
.names_size
+ ci
->i_xattrs
.count
;
1031 if (namelen
> size
) {
1035 names
= __copy_xattr_names(ci
, names
);
1040 spin_unlock(&ci
->i_ceph_lock
);
1044 static int ceph_sync_setxattr(struct inode
*inode
, const char *name
,
1045 const char *value
, size_t size
, int flags
)
1047 struct ceph_fs_client
*fsc
= ceph_sb_to_client(inode
->i_sb
);
1048 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1049 struct ceph_mds_request
*req
;
1050 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
1051 struct ceph_osd_client
*osdc
= &fsc
->client
->osdc
;
1052 struct ceph_pagelist
*pagelist
= NULL
;
1053 int op
= CEPH_MDS_OP_SETXATTR
;
1057 /* copy value into pagelist */
1058 pagelist
= ceph_pagelist_alloc(GFP_NOFS
);
1062 err
= ceph_pagelist_append(pagelist
, value
, size
);
1065 } else if (!value
) {
1066 if (flags
& CEPH_XATTR_REPLACE
)
1067 op
= CEPH_MDS_OP_RMXATTR
;
1069 flags
|= CEPH_XATTR_REMOVE
;
1072 dout("setxattr value=%.*s\n", (int)size
, value
);
1075 req
= ceph_mdsc_create_request(mdsc
, op
, USE_AUTH_MDS
);
1081 req
->r_path2
= kstrdup(name
, GFP_NOFS
);
1082 if (!req
->r_path2
) {
1083 ceph_mdsc_put_request(req
);
1088 if (op
== CEPH_MDS_OP_SETXATTR
) {
1089 req
->r_args
.setxattr
.flags
= cpu_to_le32(flags
);
1090 req
->r_args
.setxattr
.osdmap_epoch
=
1091 cpu_to_le32(osdc
->osdmap
->epoch
);
1092 req
->r_pagelist
= pagelist
;
1096 req
->r_inode
= inode
;
1098 req
->r_num_caps
= 1;
1099 req
->r_inode_drop
= CEPH_CAP_XATTR_SHARED
;
1101 dout("xattr.ver (before): %lld\n", ci
->i_xattrs
.version
);
1102 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
1103 ceph_mdsc_put_request(req
);
1104 dout("xattr.ver (after): %lld\n", ci
->i_xattrs
.version
);
1108 ceph_pagelist_release(pagelist
);
1112 int __ceph_setxattr(struct inode
*inode
, const char *name
,
1113 const void *value
, size_t size
, int flags
)
1115 struct ceph_vxattr
*vxattr
;
1116 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1117 struct ceph_mds_client
*mdsc
= ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1118 struct ceph_cap_flush
*prealloc_cf
= NULL
;
1119 struct ceph_buffer
*old_blob
= NULL
;
1123 int name_len
= strlen(name
);
1125 char *newname
= NULL
;
1126 char *newval
= NULL
;
1127 struct ceph_inode_xattr
*xattr
= NULL
;
1128 int required_blob_size
;
1129 bool check_realm
= false;
1130 bool lock_snap_rwsem
= false;
1132 if (ceph_snap(inode
) != CEPH_NOSNAP
)
1135 vxattr
= ceph_match_vxattr(inode
, name
);
1137 if (vxattr
->flags
& VXATTR_FLAG_READONLY
)
1139 if (value
&& !strncmp(vxattr
->name
, "ceph.quota", 10))
1143 /* pass any unhandled ceph.* xattrs through to the MDS */
1144 if (!strncmp(name
, XATTR_CEPH_PREFIX
, XATTR_CEPH_PREFIX_LEN
))
1145 goto do_sync_unlocked
;
1147 /* preallocate memory for xattr name, value, index node */
1149 newname
= kmemdup(name
, name_len
+ 1, GFP_NOFS
);
1154 newval
= kmemdup(value
, val_len
, GFP_NOFS
);
1159 xattr
= kmalloc(sizeof(struct ceph_inode_xattr
), GFP_NOFS
);
1163 prealloc_cf
= ceph_alloc_cap_flush();
1167 spin_lock(&ci
->i_ceph_lock
);
1169 issued
= __ceph_caps_issued(ci
, NULL
);
1170 if (ci
->i_xattrs
.version
== 0 || !(issued
& CEPH_CAP_XATTR_EXCL
))
1173 if (!lock_snap_rwsem
&& !ci
->i_head_snapc
) {
1174 lock_snap_rwsem
= true;
1175 if (!down_read_trylock(&mdsc
->snap_rwsem
)) {
1176 spin_unlock(&ci
->i_ceph_lock
);
1177 down_read(&mdsc
->snap_rwsem
);
1178 spin_lock(&ci
->i_ceph_lock
);
1183 dout("setxattr %p name '%s' issued %s\n", inode
, name
,
1184 ceph_cap_string(issued
));
1185 __build_xattrs(inode
);
1187 required_blob_size
= __get_required_blob_size(ci
, name_len
, val_len
);
1189 if (!ci
->i_xattrs
.prealloc_blob
||
1190 required_blob_size
> ci
->i_xattrs
.prealloc_blob
->alloc_len
) {
1191 struct ceph_buffer
*blob
;
1193 spin_unlock(&ci
->i_ceph_lock
);
1194 ceph_buffer_put(old_blob
); /* Shouldn't be required */
1195 dout(" pre-allocating new blob size=%d\n", required_blob_size
);
1196 blob
= ceph_buffer_new(required_blob_size
, GFP_NOFS
);
1198 goto do_sync_unlocked
;
1199 spin_lock(&ci
->i_ceph_lock
);
1200 /* prealloc_blob can't be released while holding i_ceph_lock */
1201 if (ci
->i_xattrs
.prealloc_blob
)
1202 old_blob
= ci
->i_xattrs
.prealloc_blob
;
1203 ci
->i_xattrs
.prealloc_blob
= blob
;
1207 err
= __set_xattr(ci
, newname
, name_len
, newval
, val_len
,
1208 flags
, value
? 1 : -1, &xattr
);
1211 dirty
= __ceph_mark_dirty_caps(ci
, CEPH_CAP_XATTR_EXCL
,
1213 ci
->i_xattrs
.dirty
= true;
1214 inode
->i_ctime
= current_time(inode
);
1217 spin_unlock(&ci
->i_ceph_lock
);
1218 ceph_buffer_put(old_blob
);
1219 if (lock_snap_rwsem
)
1220 up_read(&mdsc
->snap_rwsem
);
1222 __mark_inode_dirty(inode
, dirty
);
1223 ceph_free_cap_flush(prealloc_cf
);
1227 spin_unlock(&ci
->i_ceph_lock
);
1229 if (lock_snap_rwsem
)
1230 up_read(&mdsc
->snap_rwsem
);
1232 /* security module set xattr while filling trace */
1233 if (current
->journal_info
) {
1234 pr_warn_ratelimited("sync setxattr %p "
1235 "during filling trace\n", inode
);
1238 err
= ceph_sync_setxattr(inode
, name
, value
, size
, flags
);
1239 if (err
>= 0 && check_realm
) {
1240 /* check if snaprealm was created for quota inode */
1241 spin_lock(&ci
->i_ceph_lock
);
1242 if ((ci
->i_max_files
|| ci
->i_max_bytes
) &&
1243 !(ci
->i_snap_realm
&&
1244 ci
->i_snap_realm
->ino
== ci
->i_vino
.ino
))
1246 spin_unlock(&ci
->i_ceph_lock
);
1250 ceph_free_cap_flush(prealloc_cf
);
1257 static int ceph_get_xattr_handler(const struct xattr_handler
*handler
,
1258 struct dentry
*dentry
, struct inode
*inode
,
1259 const char *name
, void *value
, size_t size
)
1261 if (!ceph_is_valid_xattr(name
))
1263 return __ceph_getxattr(inode
, name
, value
, size
);
1266 static int ceph_set_xattr_handler(const struct xattr_handler
*handler
,
1267 struct user_namespace
*mnt_userns
,
1268 struct dentry
*unused
, struct inode
*inode
,
1269 const char *name
, const void *value
,
1270 size_t size
, int flags
)
1272 if (!ceph_is_valid_xattr(name
))
1274 return __ceph_setxattr(inode
, name
, value
, size
, flags
);
1277 static const struct xattr_handler ceph_other_xattr_handler
= {
1278 .prefix
= "", /* match any name => handlers called with full name */
1279 .get
= ceph_get_xattr_handler
,
1280 .set
= ceph_set_xattr_handler
,
1283 #ifdef CONFIG_SECURITY
1284 bool ceph_security_xattr_wanted(struct inode
*in
)
1286 return in
->i_security
!= NULL
;
1289 bool ceph_security_xattr_deadlock(struct inode
*in
)
1291 struct ceph_inode_info
*ci
;
1293 if (!in
->i_security
)
1295 ci
= ceph_inode(in
);
1296 spin_lock(&ci
->i_ceph_lock
);
1297 ret
= !(ci
->i_ceph_flags
& CEPH_I_SEC_INITED
) &&
1298 !(ci
->i_xattrs
.version
> 0 &&
1299 __ceph_caps_issued_mask(ci
, CEPH_CAP_XATTR_SHARED
, 0));
1300 spin_unlock(&ci
->i_ceph_lock
);
1304 #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1305 int ceph_security_init_secctx(struct dentry
*dentry
, umode_t mode
,
1306 struct ceph_acl_sec_ctx
*as_ctx
)
1308 struct ceph_pagelist
*pagelist
= as_ctx
->pagelist
;
1313 err
= security_dentry_init_security(dentry
, mode
, &dentry
->d_name
,
1315 &as_ctx
->sec_ctxlen
);
1317 WARN_ON_ONCE(err
!= -EOPNOTSUPP
);
1318 err
= 0; /* do nothing */
1324 pagelist
= ceph_pagelist_alloc(GFP_KERNEL
);
1327 err
= ceph_pagelist_reserve(pagelist
, PAGE_SIZE
);
1330 ceph_pagelist_encode_32(pagelist
, 1);
1334 * FIXME: Make security_dentry_init_security() generic. Currently
1335 * It only supports single security module and only selinux has
1336 * dentry_init_security hook.
1338 name
= XATTR_NAME_SELINUX
;
1339 name_len
= strlen(name
);
1340 err
= ceph_pagelist_reserve(pagelist
,
1341 4 * 2 + name_len
+ as_ctx
->sec_ctxlen
);
1345 if (as_ctx
->pagelist
) {
1346 /* update count of KV pairs */
1347 BUG_ON(pagelist
->length
<= sizeof(__le32
));
1348 if (list_is_singular(&pagelist
->head
)) {
1349 le32_add_cpu((__le32
*)pagelist
->mapped_tail
, 1);
1351 struct page
*page
= list_first_entry(&pagelist
->head
,
1353 void *addr
= kmap_atomic(page
);
1354 le32_add_cpu((__le32
*)addr
, 1);
1355 kunmap_atomic(addr
);
1358 as_ctx
->pagelist
= pagelist
;
1361 ceph_pagelist_encode_32(pagelist
, name_len
);
1362 ceph_pagelist_append(pagelist
, name
, name_len
);
1364 ceph_pagelist_encode_32(pagelist
, as_ctx
->sec_ctxlen
);
1365 ceph_pagelist_append(pagelist
, as_ctx
->sec_ctx
, as_ctx
->sec_ctxlen
);
1369 if (pagelist
&& !as_ctx
->pagelist
)
1370 ceph_pagelist_release(pagelist
);
1373 #endif /* CONFIG_CEPH_FS_SECURITY_LABEL */
1374 #endif /* CONFIG_SECURITY */
1376 void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx
*as_ctx
)
1378 #ifdef CONFIG_CEPH_FS_POSIX_ACL
1379 posix_acl_release(as_ctx
->acl
);
1380 posix_acl_release(as_ctx
->default_acl
);
1382 #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1383 security_release_secctx(as_ctx
->sec_ctx
, as_ctx
->sec_ctxlen
);
1385 if (as_ctx
->pagelist
)
1386 ceph_pagelist_release(as_ctx
->pagelist
);
1390 * List of handlers for synthetic system.* attributes. Other
1391 * attributes are handled directly.
1393 const struct xattr_handler
*ceph_xattr_handlers
[] = {
1394 #ifdef CONFIG_CEPH_FS_POSIX_ACL
1395 &posix_acl_access_xattr_handler
,
1396 &posix_acl_default_xattr_handler
,
1398 &ceph_other_xattr_handler
,