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_rbytes(struct ceph_inode_info
*ci
, char *val
,
239 return ceph_fmt_xattr(val
, size
, "%lld", ci
->i_rbytes
);
242 static ssize_t
ceph_vxattrcb_dir_rctime(struct ceph_inode_info
*ci
, char *val
,
245 return ceph_fmt_xattr(val
, size
, "%lld.%09ld", ci
->i_rctime
.tv_sec
,
246 ci
->i_rctime
.tv_nsec
);
250 static bool ceph_vxattrcb_dir_pin_exists(struct ceph_inode_info
*ci
)
252 return ci
->i_dir_pin
!= -ENODATA
;
255 static ssize_t
ceph_vxattrcb_dir_pin(struct ceph_inode_info
*ci
, char *val
,
258 return ceph_fmt_xattr(val
, size
, "%d", (int)ci
->i_dir_pin
);
262 static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info
*ci
)
265 spin_lock(&ci
->i_ceph_lock
);
266 if ((ci
->i_max_files
|| ci
->i_max_bytes
) &&
267 ci
->i_vino
.snap
== CEPH_NOSNAP
&&
269 ci
->i_snap_realm
->ino
== ci
->i_vino
.ino
)
271 spin_unlock(&ci
->i_ceph_lock
);
275 static ssize_t
ceph_vxattrcb_quota(struct ceph_inode_info
*ci
, char *val
,
278 return ceph_fmt_xattr(val
, size
, "max_bytes=%llu max_files=%llu",
279 ci
->i_max_bytes
, ci
->i_max_files
);
282 static ssize_t
ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info
*ci
,
283 char *val
, size_t size
)
285 return ceph_fmt_xattr(val
, size
, "%llu", ci
->i_max_bytes
);
288 static ssize_t
ceph_vxattrcb_quota_max_files(struct ceph_inode_info
*ci
,
289 char *val
, size_t size
)
291 return ceph_fmt_xattr(val
, size
, "%llu", ci
->i_max_files
);
295 static bool ceph_vxattrcb_snap_btime_exists(struct ceph_inode_info
*ci
)
297 return (ci
->i_snap_btime
.tv_sec
!= 0 || ci
->i_snap_btime
.tv_nsec
!= 0);
300 static ssize_t
ceph_vxattrcb_snap_btime(struct ceph_inode_info
*ci
, char *val
,
303 return ceph_fmt_xattr(val
, size
, "%lld.%09ld", ci
->i_snap_btime
.tv_sec
,
304 ci
->i_snap_btime
.tv_nsec
);
307 static ssize_t
ceph_vxattrcb_cluster_fsid(struct ceph_inode_info
*ci
,
308 char *val
, size_t size
)
310 struct ceph_fs_client
*fsc
= ceph_sb_to_client(ci
->vfs_inode
.i_sb
);
312 return ceph_fmt_xattr(val
, size
, "%pU", &fsc
->client
->fsid
);
315 static ssize_t
ceph_vxattrcb_client_id(struct ceph_inode_info
*ci
,
316 char *val
, size_t size
)
318 struct ceph_fs_client
*fsc
= ceph_sb_to_client(ci
->vfs_inode
.i_sb
);
320 return ceph_fmt_xattr(val
, size
, "client%lld",
321 ceph_client_gid(fsc
->client
));
324 static ssize_t
ceph_vxattrcb_caps(struct ceph_inode_info
*ci
, char *val
,
329 spin_lock(&ci
->i_ceph_lock
);
330 issued
= __ceph_caps_issued(ci
, NULL
);
331 spin_unlock(&ci
->i_ceph_lock
);
333 return ceph_fmt_xattr(val
, size
, "%s/0x%x",
334 ceph_cap_string(issued
), issued
);
337 #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
338 #define CEPH_XATTR_NAME2(_type, _name, _name2) \
339 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
341 #define XATTR_NAME_CEPH(_type, _name, _flags) \
343 .name = CEPH_XATTR_NAME(_type, _name), \
344 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
345 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
347 .flags = (VXATTR_FLAG_READONLY | _flags), \
349 #define XATTR_RSTAT_FIELD(_type, _name) \
350 XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
351 #define XATTR_LAYOUT_FIELD(_type, _name, _field) \
353 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
354 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
355 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
356 .exists_cb = ceph_vxattrcb_layout_exists, \
357 .flags = VXATTR_FLAG_HIDDEN, \
359 #define XATTR_QUOTA_FIELD(_type, _name) \
361 .name = CEPH_XATTR_NAME(_type, _name), \
362 .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \
363 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
364 .exists_cb = ceph_vxattrcb_quota_exists, \
365 .flags = VXATTR_FLAG_HIDDEN, \
368 static struct ceph_vxattr ceph_dir_vxattrs
[] = {
370 .name
= "ceph.dir.layout",
371 .name_size
= sizeof("ceph.dir.layout"),
372 .getxattr_cb
= ceph_vxattrcb_layout
,
373 .exists_cb
= ceph_vxattrcb_layout_exists
,
374 .flags
= VXATTR_FLAG_HIDDEN
,
376 XATTR_LAYOUT_FIELD(dir
, layout
, stripe_unit
),
377 XATTR_LAYOUT_FIELD(dir
, layout
, stripe_count
),
378 XATTR_LAYOUT_FIELD(dir
, layout
, object_size
),
379 XATTR_LAYOUT_FIELD(dir
, layout
, pool
),
380 XATTR_LAYOUT_FIELD(dir
, layout
, pool_namespace
),
381 XATTR_NAME_CEPH(dir
, entries
, VXATTR_FLAG_DIRSTAT
),
382 XATTR_NAME_CEPH(dir
, files
, VXATTR_FLAG_DIRSTAT
),
383 XATTR_NAME_CEPH(dir
, subdirs
, VXATTR_FLAG_DIRSTAT
),
384 XATTR_RSTAT_FIELD(dir
, rentries
),
385 XATTR_RSTAT_FIELD(dir
, rfiles
),
386 XATTR_RSTAT_FIELD(dir
, rsubdirs
),
387 XATTR_RSTAT_FIELD(dir
, rbytes
),
388 XATTR_RSTAT_FIELD(dir
, rctime
),
390 .name
= "ceph.dir.pin",
391 .name_size
= sizeof("ceph.dir.pin"),
392 .getxattr_cb
= ceph_vxattrcb_dir_pin
,
393 .exists_cb
= ceph_vxattrcb_dir_pin_exists
,
394 .flags
= VXATTR_FLAG_HIDDEN
,
397 .name
= "ceph.quota",
398 .name_size
= sizeof("ceph.quota"),
399 .getxattr_cb
= ceph_vxattrcb_quota
,
400 .exists_cb
= ceph_vxattrcb_quota_exists
,
401 .flags
= VXATTR_FLAG_HIDDEN
,
403 XATTR_QUOTA_FIELD(quota
, max_bytes
),
404 XATTR_QUOTA_FIELD(quota
, max_files
),
406 .name
= "ceph.snap.btime",
407 .name_size
= sizeof("ceph.snap.btime"),
408 .getxattr_cb
= ceph_vxattrcb_snap_btime
,
409 .exists_cb
= ceph_vxattrcb_snap_btime_exists
,
410 .flags
= VXATTR_FLAG_READONLY
,
414 .name_size
= sizeof("ceph.caps"),
415 .getxattr_cb
= ceph_vxattrcb_caps
,
417 .flags
= VXATTR_FLAG_HIDDEN
,
419 { .name
= NULL
, 0 } /* Required table terminator */
424 static struct ceph_vxattr ceph_file_vxattrs
[] = {
426 .name
= "ceph.file.layout",
427 .name_size
= sizeof("ceph.file.layout"),
428 .getxattr_cb
= ceph_vxattrcb_layout
,
429 .exists_cb
= ceph_vxattrcb_layout_exists
,
430 .flags
= VXATTR_FLAG_HIDDEN
,
432 XATTR_LAYOUT_FIELD(file
, layout
, stripe_unit
),
433 XATTR_LAYOUT_FIELD(file
, layout
, stripe_count
),
434 XATTR_LAYOUT_FIELD(file
, layout
, object_size
),
435 XATTR_LAYOUT_FIELD(file
, layout
, pool
),
436 XATTR_LAYOUT_FIELD(file
, layout
, pool_namespace
),
438 .name
= "ceph.snap.btime",
439 .name_size
= sizeof("ceph.snap.btime"),
440 .getxattr_cb
= ceph_vxattrcb_snap_btime
,
441 .exists_cb
= ceph_vxattrcb_snap_btime_exists
,
442 .flags
= VXATTR_FLAG_READONLY
,
446 .name_size
= sizeof("ceph.caps"),
447 .getxattr_cb
= ceph_vxattrcb_caps
,
449 .flags
= VXATTR_FLAG_HIDDEN
,
451 { .name
= NULL
, 0 } /* Required table terminator */
454 static struct ceph_vxattr ceph_common_vxattrs
[] = {
456 .name
= "ceph.cluster_fsid",
457 .name_size
= sizeof("ceph.cluster_fsid"),
458 .getxattr_cb
= ceph_vxattrcb_cluster_fsid
,
460 .flags
= VXATTR_FLAG_READONLY
,
463 .name
= "ceph.client_id",
464 .name_size
= sizeof("ceph.client_id"),
465 .getxattr_cb
= ceph_vxattrcb_client_id
,
467 .flags
= VXATTR_FLAG_READONLY
,
469 { .name
= NULL
, 0 } /* Required table terminator */
472 static struct ceph_vxattr
*ceph_inode_vxattrs(struct inode
*inode
)
474 if (S_ISDIR(inode
->i_mode
))
475 return ceph_dir_vxattrs
;
476 else if (S_ISREG(inode
->i_mode
))
477 return ceph_file_vxattrs
;
481 static struct ceph_vxattr
*ceph_match_vxattr(struct inode
*inode
,
484 struct ceph_vxattr
*vxattr
= ceph_inode_vxattrs(inode
);
487 while (vxattr
->name
) {
488 if (!strcmp(vxattr
->name
, name
))
494 vxattr
= ceph_common_vxattrs
;
495 while (vxattr
->name
) {
496 if (!strcmp(vxattr
->name
, name
))
504 static int __set_xattr(struct ceph_inode_info
*ci
,
505 const char *name
, int name_len
,
506 const char *val
, int val_len
,
507 int flags
, int update_xattr
,
508 struct ceph_inode_xattr
**newxattr
)
511 struct rb_node
*parent
= NULL
;
512 struct ceph_inode_xattr
*xattr
= NULL
;
516 p
= &ci
->i_xattrs
.index
.rb_node
;
519 xattr
= rb_entry(parent
, struct ceph_inode_xattr
, node
);
520 c
= strncmp(name
, xattr
->name
, min(name_len
, xattr
->name_len
));
526 if (name_len
== xattr
->name_len
)
528 else if (name_len
< xattr
->name_len
)
539 if (xattr
&& (flags
& XATTR_CREATE
))
541 else if (!xattr
&& (flags
& XATTR_REPLACE
))
549 if (update_xattr
< 0) {
551 __remove_xattr(ci
, xattr
);
562 xattr
->name_len
= name_len
;
563 xattr
->should_free_name
= update_xattr
;
565 ci
->i_xattrs
.count
++;
566 dout("__set_xattr count=%d\n", ci
->i_xattrs
.count
);
570 if (xattr
->should_free_val
)
577 ci
->i_xattrs
.names_size
-= xattr
->name_len
;
578 ci
->i_xattrs
.vals_size
-= xattr
->val_len
;
580 ci
->i_xattrs
.names_size
+= name_len
;
581 ci
->i_xattrs
.vals_size
+= val_len
;
587 xattr
->val_len
= val_len
;
588 xattr
->dirty
= update_xattr
;
589 xattr
->should_free_val
= (val
&& update_xattr
);
592 rb_link_node(&xattr
->node
, parent
, p
);
593 rb_insert_color(&xattr
->node
, &ci
->i_xattrs
.index
);
594 dout("__set_xattr_val p=%p\n", p
);
597 dout("__set_xattr_val added %llx.%llx xattr %p %.*s=%.*s\n",
598 ceph_vinop(&ci
->vfs_inode
), xattr
, name_len
, name
, val_len
, val
);
603 static struct ceph_inode_xattr
*__get_xattr(struct ceph_inode_info
*ci
,
607 struct rb_node
*parent
= NULL
;
608 struct ceph_inode_xattr
*xattr
= NULL
;
609 int name_len
= strlen(name
);
612 p
= &ci
->i_xattrs
.index
.rb_node
;
615 xattr
= rb_entry(parent
, struct ceph_inode_xattr
, node
);
616 c
= strncmp(name
, xattr
->name
, xattr
->name_len
);
617 if (c
== 0 && name_len
> xattr
->name_len
)
624 dout("__get_xattr %s: found %.*s\n", name
,
625 xattr
->val_len
, xattr
->val
);
630 dout("__get_xattr %s: not found\n", name
);
635 static void __free_xattr(struct ceph_inode_xattr
*xattr
)
639 if (xattr
->should_free_name
)
641 if (xattr
->should_free_val
)
647 static int __remove_xattr(struct ceph_inode_info
*ci
,
648 struct ceph_inode_xattr
*xattr
)
653 rb_erase(&xattr
->node
, &ci
->i_xattrs
.index
);
655 if (xattr
->should_free_name
)
657 if (xattr
->should_free_val
)
660 ci
->i_xattrs
.names_size
-= xattr
->name_len
;
661 ci
->i_xattrs
.vals_size
-= xattr
->val_len
;
662 ci
->i_xattrs
.count
--;
668 static char *__copy_xattr_names(struct ceph_inode_info
*ci
,
672 struct ceph_inode_xattr
*xattr
= NULL
;
674 p
= rb_first(&ci
->i_xattrs
.index
);
675 dout("__copy_xattr_names count=%d\n", ci
->i_xattrs
.count
);
678 xattr
= rb_entry(p
, struct ceph_inode_xattr
, node
);
679 memcpy(dest
, xattr
->name
, xattr
->name_len
);
680 dest
[xattr
->name_len
] = '\0';
682 dout("dest=%s %p (%s) (%d/%d)\n", dest
, xattr
, xattr
->name
,
683 xattr
->name_len
, ci
->i_xattrs
.names_size
);
685 dest
+= xattr
->name_len
+ 1;
692 void __ceph_destroy_xattrs(struct ceph_inode_info
*ci
)
694 struct rb_node
*p
, *tmp
;
695 struct ceph_inode_xattr
*xattr
= NULL
;
697 p
= rb_first(&ci
->i_xattrs
.index
);
699 dout("__ceph_destroy_xattrs p=%p\n", p
);
702 xattr
= rb_entry(p
, struct ceph_inode_xattr
, node
);
705 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p
,
706 xattr
->name_len
, xattr
->name
);
707 rb_erase(tmp
, &ci
->i_xattrs
.index
);
712 ci
->i_xattrs
.names_size
= 0;
713 ci
->i_xattrs
.vals_size
= 0;
714 ci
->i_xattrs
.index_version
= 0;
715 ci
->i_xattrs
.count
= 0;
716 ci
->i_xattrs
.index
= RB_ROOT
;
719 static int __build_xattrs(struct inode
*inode
)
720 __releases(ci
->i_ceph_lock
)
721 __acquires(ci
->i_ceph_lock
)
727 const char *name
, *val
;
728 struct ceph_inode_info
*ci
= ceph_inode(inode
);
730 struct ceph_inode_xattr
**xattrs
= NULL
;
734 dout("__build_xattrs() len=%d\n",
735 ci
->i_xattrs
.blob
? (int)ci
->i_xattrs
.blob
->vec
.iov_len
: 0);
737 if (ci
->i_xattrs
.index_version
>= ci
->i_xattrs
.version
)
738 return 0; /* already built */
740 __ceph_destroy_xattrs(ci
);
743 /* updated internal xattr rb tree */
744 if (ci
->i_xattrs
.blob
&& ci
->i_xattrs
.blob
->vec
.iov_len
> 4) {
745 p
= ci
->i_xattrs
.blob
->vec
.iov_base
;
746 end
= p
+ ci
->i_xattrs
.blob
->vec
.iov_len
;
747 ceph_decode_32_safe(&p
, end
, numattr
, bad
);
748 xattr_version
= ci
->i_xattrs
.version
;
749 spin_unlock(&ci
->i_ceph_lock
);
751 xattrs
= kcalloc(numattr
, sizeof(struct ceph_inode_xattr
*),
757 for (i
= 0; i
< numattr
; i
++) {
758 xattrs
[i
] = kmalloc(sizeof(struct ceph_inode_xattr
),
764 spin_lock(&ci
->i_ceph_lock
);
765 if (ci
->i_xattrs
.version
!= xattr_version
) {
766 /* lost a race, retry */
767 for (i
= 0; i
< numattr
; i
++)
775 ceph_decode_32_safe(&p
, end
, len
, bad
);
779 ceph_decode_32_safe(&p
, end
, len
, bad
);
783 err
= __set_xattr(ci
, name
, namelen
, val
, len
,
784 0, 0, &xattrs
[numattr
]);
791 ci
->i_xattrs
.index_version
= ci
->i_xattrs
.version
;
792 ci
->i_xattrs
.dirty
= false;
796 spin_lock(&ci
->i_ceph_lock
);
799 for (i
= 0; i
< numattr
; i
++)
803 ci
->i_xattrs
.names_size
= 0;
807 static int __get_required_blob_size(struct ceph_inode_info
*ci
, int name_size
,
811 * 4 bytes for the length, and additional 4 bytes per each xattr name,
812 * 4 bytes per each value
814 int size
= 4 + ci
->i_xattrs
.count
*(4 + 4) +
815 ci
->i_xattrs
.names_size
+
816 ci
->i_xattrs
.vals_size
;
817 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
818 ci
->i_xattrs
.count
, ci
->i_xattrs
.names_size
,
819 ci
->i_xattrs
.vals_size
);
822 size
+= 4 + 4 + name_size
+ val_size
;
828 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
829 * and swap into place. It returns the old i_xattrs.blob (or NULL) so
830 * that it can be freed by the caller as the i_ceph_lock is likely to be
833 struct ceph_buffer
*__ceph_build_xattrs_blob(struct ceph_inode_info
*ci
)
836 struct ceph_inode_xattr
*xattr
= NULL
;
837 struct ceph_buffer
*old_blob
= NULL
;
840 dout("__build_xattrs_blob %p\n", &ci
->vfs_inode
);
841 if (ci
->i_xattrs
.dirty
) {
842 int need
= __get_required_blob_size(ci
, 0, 0);
844 BUG_ON(need
> ci
->i_xattrs
.prealloc_blob
->alloc_len
);
846 p
= rb_first(&ci
->i_xattrs
.index
);
847 dest
= ci
->i_xattrs
.prealloc_blob
->vec
.iov_base
;
849 ceph_encode_32(&dest
, ci
->i_xattrs
.count
);
851 xattr
= rb_entry(p
, struct ceph_inode_xattr
, node
);
853 ceph_encode_32(&dest
, xattr
->name_len
);
854 memcpy(dest
, xattr
->name
, xattr
->name_len
);
855 dest
+= xattr
->name_len
;
856 ceph_encode_32(&dest
, xattr
->val_len
);
857 memcpy(dest
, xattr
->val
, xattr
->val_len
);
858 dest
+= xattr
->val_len
;
863 /* adjust buffer len; it may be larger than we need */
864 ci
->i_xattrs
.prealloc_blob
->vec
.iov_len
=
865 dest
- ci
->i_xattrs
.prealloc_blob
->vec
.iov_base
;
867 if (ci
->i_xattrs
.blob
)
868 old_blob
= ci
->i_xattrs
.blob
;
869 ci
->i_xattrs
.blob
= ci
->i_xattrs
.prealloc_blob
;
870 ci
->i_xattrs
.prealloc_blob
= NULL
;
871 ci
->i_xattrs
.dirty
= false;
872 ci
->i_xattrs
.version
++;
878 static inline int __get_request_mask(struct inode
*in
) {
879 struct ceph_mds_request
*req
= current
->journal_info
;
881 if (req
&& req
->r_target_inode
== in
) {
882 if (req
->r_op
== CEPH_MDS_OP_LOOKUP
||
883 req
->r_op
== CEPH_MDS_OP_LOOKUPINO
||
884 req
->r_op
== CEPH_MDS_OP_LOOKUPPARENT
||
885 req
->r_op
== CEPH_MDS_OP_GETATTR
) {
886 mask
= le32_to_cpu(req
->r_args
.getattr
.mask
);
887 } else if (req
->r_op
== CEPH_MDS_OP_OPEN
||
888 req
->r_op
== CEPH_MDS_OP_CREATE
) {
889 mask
= le32_to_cpu(req
->r_args
.open
.mask
);
895 ssize_t
__ceph_getxattr(struct inode
*inode
, const char *name
, void *value
,
898 struct ceph_inode_info
*ci
= ceph_inode(inode
);
899 struct ceph_inode_xattr
*xattr
;
900 struct ceph_vxattr
*vxattr
= NULL
;
904 /* let's see if a virtual xattr was requested */
905 vxattr
= ceph_match_vxattr(inode
, name
);
908 if (vxattr
->flags
& VXATTR_FLAG_RSTAT
)
909 mask
|= CEPH_STAT_RSTAT
;
910 if (vxattr
->flags
& VXATTR_FLAG_DIRSTAT
)
911 mask
|= CEPH_CAP_FILE_SHARED
;
912 err
= ceph_do_getattr(inode
, mask
, true);
916 if (!(vxattr
->exists_cb
&& !vxattr
->exists_cb(ci
))) {
917 err
= vxattr
->getxattr_cb(ci
, value
, size
);
918 if (size
&& size
< err
)
924 req_mask
= __get_request_mask(inode
);
926 spin_lock(&ci
->i_ceph_lock
);
927 dout("getxattr %p name '%s' ver=%lld index_ver=%lld\n", inode
, name
,
928 ci
->i_xattrs
.version
, ci
->i_xattrs
.index_version
);
930 if (ci
->i_xattrs
.version
== 0 ||
931 !((req_mask
& CEPH_CAP_XATTR_SHARED
) ||
932 __ceph_caps_issued_mask_metric(ci
, CEPH_CAP_XATTR_SHARED
, 1))) {
933 spin_unlock(&ci
->i_ceph_lock
);
935 /* security module gets xattr while filling trace */
936 if (current
->journal_info
) {
937 pr_warn_ratelimited("sync getxattr %p "
938 "during filling trace\n", inode
);
942 /* get xattrs from mds (if we don't already have them) */
943 err
= ceph_do_getattr(inode
, CEPH_STAT_CAP_XATTR
, true);
946 spin_lock(&ci
->i_ceph_lock
);
949 err
= __build_xattrs(inode
);
953 err
= -ENODATA
; /* == ENOATTR */
954 xattr
= __get_xattr(ci
, name
);
959 if (size
&& size
< xattr
->val_len
)
962 err
= xattr
->val_len
;
966 memcpy(value
, xattr
->val
, xattr
->val_len
);
968 if (current
->journal_info
&&
969 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) &&
970 security_ismaclabel(name
+ XATTR_SECURITY_PREFIX_LEN
))
971 ci
->i_ceph_flags
|= CEPH_I_SEC_INITED
;
973 spin_unlock(&ci
->i_ceph_lock
);
977 ssize_t
ceph_listxattr(struct dentry
*dentry
, char *names
, size_t size
)
979 struct inode
*inode
= d_inode(dentry
);
980 struct ceph_inode_info
*ci
= ceph_inode(inode
);
981 bool len_only
= (size
== 0);
985 spin_lock(&ci
->i_ceph_lock
);
986 dout("listxattr %p ver=%lld index_ver=%lld\n", inode
,
987 ci
->i_xattrs
.version
, ci
->i_xattrs
.index_version
);
989 if (ci
->i_xattrs
.version
== 0 ||
990 !__ceph_caps_issued_mask_metric(ci
, CEPH_CAP_XATTR_SHARED
, 1)) {
991 spin_unlock(&ci
->i_ceph_lock
);
992 err
= ceph_do_getattr(inode
, CEPH_STAT_CAP_XATTR
, true);
995 spin_lock(&ci
->i_ceph_lock
);
998 err
= __build_xattrs(inode
);
1002 /* add 1 byte for each xattr due to the null termination */
1003 namelen
= ci
->i_xattrs
.names_size
+ ci
->i_xattrs
.count
;
1005 if (namelen
> size
) {
1009 names
= __copy_xattr_names(ci
, names
);
1014 spin_unlock(&ci
->i_ceph_lock
);
1018 static int ceph_sync_setxattr(struct inode
*inode
, const char *name
,
1019 const char *value
, size_t size
, int flags
)
1021 struct ceph_fs_client
*fsc
= ceph_sb_to_client(inode
->i_sb
);
1022 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1023 struct ceph_mds_request
*req
;
1024 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
1025 struct ceph_osd_client
*osdc
= &fsc
->client
->osdc
;
1026 struct ceph_pagelist
*pagelist
= NULL
;
1027 int op
= CEPH_MDS_OP_SETXATTR
;
1031 /* copy value into pagelist */
1032 pagelist
= ceph_pagelist_alloc(GFP_NOFS
);
1036 err
= ceph_pagelist_append(pagelist
, value
, size
);
1039 } else if (!value
) {
1040 if (flags
& CEPH_XATTR_REPLACE
)
1041 op
= CEPH_MDS_OP_RMXATTR
;
1043 flags
|= CEPH_XATTR_REMOVE
;
1046 dout("setxattr value=%.*s\n", (int)size
, value
);
1049 req
= ceph_mdsc_create_request(mdsc
, op
, USE_AUTH_MDS
);
1055 req
->r_path2
= kstrdup(name
, GFP_NOFS
);
1056 if (!req
->r_path2
) {
1057 ceph_mdsc_put_request(req
);
1062 if (op
== CEPH_MDS_OP_SETXATTR
) {
1063 req
->r_args
.setxattr
.flags
= cpu_to_le32(flags
);
1064 req
->r_args
.setxattr
.osdmap_epoch
=
1065 cpu_to_le32(osdc
->osdmap
->epoch
);
1066 req
->r_pagelist
= pagelist
;
1070 req
->r_inode
= inode
;
1072 req
->r_num_caps
= 1;
1073 req
->r_inode_drop
= CEPH_CAP_XATTR_SHARED
;
1075 dout("xattr.ver (before): %lld\n", ci
->i_xattrs
.version
);
1076 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
1077 ceph_mdsc_put_request(req
);
1078 dout("xattr.ver (after): %lld\n", ci
->i_xattrs
.version
);
1082 ceph_pagelist_release(pagelist
);
1086 int __ceph_setxattr(struct inode
*inode
, const char *name
,
1087 const void *value
, size_t size
, int flags
)
1089 struct ceph_vxattr
*vxattr
;
1090 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1091 struct ceph_mds_client
*mdsc
= ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1092 struct ceph_cap_flush
*prealloc_cf
= NULL
;
1093 struct ceph_buffer
*old_blob
= NULL
;
1097 int name_len
= strlen(name
);
1099 char *newname
= NULL
;
1100 char *newval
= NULL
;
1101 struct ceph_inode_xattr
*xattr
= NULL
;
1102 int required_blob_size
;
1103 bool check_realm
= false;
1104 bool lock_snap_rwsem
= false;
1106 if (ceph_snap(inode
) != CEPH_NOSNAP
)
1109 vxattr
= ceph_match_vxattr(inode
, name
);
1111 if (vxattr
->flags
& VXATTR_FLAG_READONLY
)
1113 if (value
&& !strncmp(vxattr
->name
, "ceph.quota", 10))
1117 /* pass any unhandled ceph.* xattrs through to the MDS */
1118 if (!strncmp(name
, XATTR_CEPH_PREFIX
, XATTR_CEPH_PREFIX_LEN
))
1119 goto do_sync_unlocked
;
1121 /* preallocate memory for xattr name, value, index node */
1123 newname
= kmemdup(name
, name_len
+ 1, GFP_NOFS
);
1128 newval
= kmemdup(value
, val_len
, GFP_NOFS
);
1133 xattr
= kmalloc(sizeof(struct ceph_inode_xattr
), GFP_NOFS
);
1137 prealloc_cf
= ceph_alloc_cap_flush();
1141 spin_lock(&ci
->i_ceph_lock
);
1143 issued
= __ceph_caps_issued(ci
, NULL
);
1144 if (ci
->i_xattrs
.version
== 0 || !(issued
& CEPH_CAP_XATTR_EXCL
))
1147 if (!lock_snap_rwsem
&& !ci
->i_head_snapc
) {
1148 lock_snap_rwsem
= true;
1149 if (!down_read_trylock(&mdsc
->snap_rwsem
)) {
1150 spin_unlock(&ci
->i_ceph_lock
);
1151 down_read(&mdsc
->snap_rwsem
);
1152 spin_lock(&ci
->i_ceph_lock
);
1157 dout("setxattr %p name '%s' issued %s\n", inode
, name
,
1158 ceph_cap_string(issued
));
1159 __build_xattrs(inode
);
1161 required_blob_size
= __get_required_blob_size(ci
, name_len
, val_len
);
1163 if (!ci
->i_xattrs
.prealloc_blob
||
1164 required_blob_size
> ci
->i_xattrs
.prealloc_blob
->alloc_len
) {
1165 struct ceph_buffer
*blob
;
1167 spin_unlock(&ci
->i_ceph_lock
);
1168 ceph_buffer_put(old_blob
); /* Shouldn't be required */
1169 dout(" pre-allocating new blob size=%d\n", required_blob_size
);
1170 blob
= ceph_buffer_new(required_blob_size
, GFP_NOFS
);
1172 goto do_sync_unlocked
;
1173 spin_lock(&ci
->i_ceph_lock
);
1174 /* prealloc_blob can't be released while holding i_ceph_lock */
1175 if (ci
->i_xattrs
.prealloc_blob
)
1176 old_blob
= ci
->i_xattrs
.prealloc_blob
;
1177 ci
->i_xattrs
.prealloc_blob
= blob
;
1181 err
= __set_xattr(ci
, newname
, name_len
, newval
, val_len
,
1182 flags
, value
? 1 : -1, &xattr
);
1185 dirty
= __ceph_mark_dirty_caps(ci
, CEPH_CAP_XATTR_EXCL
,
1187 ci
->i_xattrs
.dirty
= true;
1188 inode
->i_ctime
= current_time(inode
);
1191 spin_unlock(&ci
->i_ceph_lock
);
1192 ceph_buffer_put(old_blob
);
1193 if (lock_snap_rwsem
)
1194 up_read(&mdsc
->snap_rwsem
);
1196 __mark_inode_dirty(inode
, dirty
);
1197 ceph_free_cap_flush(prealloc_cf
);
1201 spin_unlock(&ci
->i_ceph_lock
);
1203 if (lock_snap_rwsem
)
1204 up_read(&mdsc
->snap_rwsem
);
1206 /* security module set xattr while filling trace */
1207 if (current
->journal_info
) {
1208 pr_warn_ratelimited("sync setxattr %p "
1209 "during filling trace\n", inode
);
1212 err
= ceph_sync_setxattr(inode
, name
, value
, size
, flags
);
1213 if (err
>= 0 && check_realm
) {
1214 /* check if snaprealm was created for quota inode */
1215 spin_lock(&ci
->i_ceph_lock
);
1216 if ((ci
->i_max_files
|| ci
->i_max_bytes
) &&
1217 !(ci
->i_snap_realm
&&
1218 ci
->i_snap_realm
->ino
== ci
->i_vino
.ino
))
1220 spin_unlock(&ci
->i_ceph_lock
);
1224 ceph_free_cap_flush(prealloc_cf
);
1231 static int ceph_get_xattr_handler(const struct xattr_handler
*handler
,
1232 struct dentry
*dentry
, struct inode
*inode
,
1233 const char *name
, void *value
, size_t size
)
1235 if (!ceph_is_valid_xattr(name
))
1237 return __ceph_getxattr(inode
, name
, value
, size
);
1240 static int ceph_set_xattr_handler(const struct xattr_handler
*handler
,
1241 struct dentry
*unused
, struct inode
*inode
,
1242 const char *name
, const void *value
,
1243 size_t size
, int flags
)
1245 if (!ceph_is_valid_xattr(name
))
1247 return __ceph_setxattr(inode
, name
, value
, size
, flags
);
1250 static const struct xattr_handler ceph_other_xattr_handler
= {
1251 .prefix
= "", /* match any name => handlers called with full name */
1252 .get
= ceph_get_xattr_handler
,
1253 .set
= ceph_set_xattr_handler
,
1256 #ifdef CONFIG_SECURITY
1257 bool ceph_security_xattr_wanted(struct inode
*in
)
1259 return in
->i_security
!= NULL
;
1262 bool ceph_security_xattr_deadlock(struct inode
*in
)
1264 struct ceph_inode_info
*ci
;
1266 if (!in
->i_security
)
1268 ci
= ceph_inode(in
);
1269 spin_lock(&ci
->i_ceph_lock
);
1270 ret
= !(ci
->i_ceph_flags
& CEPH_I_SEC_INITED
) &&
1271 !(ci
->i_xattrs
.version
> 0 &&
1272 __ceph_caps_issued_mask(ci
, CEPH_CAP_XATTR_SHARED
, 0));
1273 spin_unlock(&ci
->i_ceph_lock
);
1277 #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1278 int ceph_security_init_secctx(struct dentry
*dentry
, umode_t mode
,
1279 struct ceph_acl_sec_ctx
*as_ctx
)
1281 struct ceph_pagelist
*pagelist
= as_ctx
->pagelist
;
1286 err
= security_dentry_init_security(dentry
, mode
, &dentry
->d_name
,
1288 &as_ctx
->sec_ctxlen
);
1290 WARN_ON_ONCE(err
!= -EOPNOTSUPP
);
1291 err
= 0; /* do nothing */
1297 pagelist
= ceph_pagelist_alloc(GFP_KERNEL
);
1300 err
= ceph_pagelist_reserve(pagelist
, PAGE_SIZE
);
1303 ceph_pagelist_encode_32(pagelist
, 1);
1307 * FIXME: Make security_dentry_init_security() generic. Currently
1308 * It only supports single security module and only selinux has
1309 * dentry_init_security hook.
1311 name
= XATTR_NAME_SELINUX
;
1312 name_len
= strlen(name
);
1313 err
= ceph_pagelist_reserve(pagelist
,
1314 4 * 2 + name_len
+ as_ctx
->sec_ctxlen
);
1318 if (as_ctx
->pagelist
) {
1319 /* update count of KV pairs */
1320 BUG_ON(pagelist
->length
<= sizeof(__le32
));
1321 if (list_is_singular(&pagelist
->head
)) {
1322 le32_add_cpu((__le32
*)pagelist
->mapped_tail
, 1);
1324 struct page
*page
= list_first_entry(&pagelist
->head
,
1326 void *addr
= kmap_atomic(page
);
1327 le32_add_cpu((__le32
*)addr
, 1);
1328 kunmap_atomic(addr
);
1331 as_ctx
->pagelist
= pagelist
;
1334 ceph_pagelist_encode_32(pagelist
, name_len
);
1335 ceph_pagelist_append(pagelist
, name
, name_len
);
1337 ceph_pagelist_encode_32(pagelist
, as_ctx
->sec_ctxlen
);
1338 ceph_pagelist_append(pagelist
, as_ctx
->sec_ctx
, as_ctx
->sec_ctxlen
);
1342 if (pagelist
&& !as_ctx
->pagelist
)
1343 ceph_pagelist_release(pagelist
);
1346 #endif /* CONFIG_CEPH_FS_SECURITY_LABEL */
1347 #endif /* CONFIG_SECURITY */
1349 void ceph_release_acl_sec_ctx(struct ceph_acl_sec_ctx
*as_ctx
)
1351 #ifdef CONFIG_CEPH_FS_POSIX_ACL
1352 posix_acl_release(as_ctx
->acl
);
1353 posix_acl_release(as_ctx
->default_acl
);
1355 #ifdef CONFIG_CEPH_FS_SECURITY_LABEL
1356 security_release_secctx(as_ctx
->sec_ctx
, as_ctx
->sec_ctxlen
);
1358 if (as_ctx
->pagelist
)
1359 ceph_pagelist_release(as_ctx
->pagelist
);
1363 * List of handlers for synthetic system.* attributes. Other
1364 * attributes are handled directly.
1366 const struct xattr_handler
*ceph_xattr_handlers
[] = {
1367 #ifdef CONFIG_CEPH_FS_POSIX_ACL
1368 &posix_acl_access_xattr_handler
,
1369 &posix_acl_default_xattr_handler
,
1371 &ceph_other_xattr_handler
,