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/posix_acl_xattr.h>
12 #include <linux/slab.h>
14 #define XATTR_CEPH_PREFIX "ceph."
15 #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
17 static int __remove_xattr(struct ceph_inode_info
*ci
,
18 struct ceph_inode_xattr
*xattr
);
20 static const struct xattr_handler ceph_other_xattr_handler
;
23 * List of handlers for synthetic system.* attributes. Other
24 * attributes are handled directly.
26 const struct xattr_handler
*ceph_xattr_handlers
[] = {
27 #ifdef CONFIG_CEPH_FS_POSIX_ACL
28 &posix_acl_access_xattr_handler
,
29 &posix_acl_default_xattr_handler
,
31 &ceph_other_xattr_handler
,
35 static bool ceph_is_valid_xattr(const char *name
)
37 return !strncmp(name
, XATTR_CEPH_PREFIX
, XATTR_CEPH_PREFIX_LEN
) ||
38 !strncmp(name
, XATTR_SECURITY_PREFIX
,
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
);
45 * These define virtual xattrs exposing the recursive directory
46 * statistics and layout metadata.
50 size_t name_size
; /* strlen(name) + 1 (for '\0') */
51 size_t (*getxattr_cb
)(struct ceph_inode_info
*ci
, char *val
,
53 bool readonly
, hidden
;
54 bool (*exists_cb
)(struct ceph_inode_info
*ci
);
59 static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info
*ci
)
61 struct ceph_file_layout
*fl
= &ci
->i_layout
;
62 return (fl
->stripe_unit
> 0 || fl
->stripe_count
> 0 ||
63 fl
->object_size
> 0 || fl
->pool_id
>= 0 ||
64 rcu_dereference_raw(fl
->pool_ns
) != NULL
);
67 static size_t ceph_vxattrcb_layout(struct ceph_inode_info
*ci
, char *val
,
70 struct ceph_fs_client
*fsc
= ceph_sb_to_client(ci
->vfs_inode
.i_sb
);
71 struct ceph_osd_client
*osdc
= &fsc
->client
->osdc
;
72 struct ceph_string
*pool_ns
;
73 s64 pool
= ci
->i_layout
.pool_id
;
74 const char *pool_name
;
75 const char *ns_field
= " pool_namespace=";
77 size_t len
, total_len
= 0;
80 pool_ns
= ceph_try_get_string(ci
->i_layout
.pool_ns
);
82 dout("ceph_vxattrcb_layout %p\n", &ci
->vfs_inode
);
83 down_read(&osdc
->lock
);
84 pool_name
= ceph_pg_pool_name_by_id(osdc
->osdmap
, pool
);
86 len
= snprintf(buf
, sizeof(buf
),
87 "stripe_unit=%u stripe_count=%u object_size=%u pool=",
88 ci
->i_layout
.stripe_unit
, ci
->i_layout
.stripe_count
,
89 ci
->i_layout
.object_size
);
90 total_len
= len
+ strlen(pool_name
);
92 len
= snprintf(buf
, sizeof(buf
),
93 "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
94 ci
->i_layout
.stripe_unit
, ci
->i_layout
.stripe_count
,
95 ci
->i_layout
.object_size
, (unsigned long long)pool
);
100 total_len
+= strlen(ns_field
) + pool_ns
->len
;
103 if (size
>= total_len
) {
104 memcpy(val
, buf
, len
);
107 len
= strlen(pool_name
);
108 memcpy(val
+ ret
, pool_name
, len
);
112 len
= strlen(ns_field
);
113 memcpy(val
+ ret
, ns_field
, len
);
115 memcpy(val
+ ret
, pool_ns
->str
, pool_ns
->len
);
119 up_read(&osdc
->lock
);
120 ceph_put_string(pool_ns
);
124 static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info
*ci
,
125 char *val
, size_t size
)
127 return snprintf(val
, size
, "%u", ci
->i_layout
.stripe_unit
);
130 static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info
*ci
,
131 char *val
, size_t size
)
133 return snprintf(val
, size
, "%u", ci
->i_layout
.stripe_count
);
136 static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info
*ci
,
137 char *val
, size_t size
)
139 return snprintf(val
, size
, "%u", ci
->i_layout
.object_size
);
142 static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info
*ci
,
143 char *val
, size_t size
)
146 struct ceph_fs_client
*fsc
= ceph_sb_to_client(ci
->vfs_inode
.i_sb
);
147 struct ceph_osd_client
*osdc
= &fsc
->client
->osdc
;
148 s64 pool
= ci
->i_layout
.pool_id
;
149 const char *pool_name
;
151 down_read(&osdc
->lock
);
152 pool_name
= ceph_pg_pool_name_by_id(osdc
->osdmap
, pool
);
154 ret
= snprintf(val
, size
, "%s", pool_name
);
156 ret
= snprintf(val
, size
, "%lld", (unsigned long long)pool
);
157 up_read(&osdc
->lock
);
161 static size_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info
*ci
,
162 char *val
, size_t size
)
165 struct ceph_string
*ns
= ceph_try_get_string(ci
->i_layout
.pool_ns
);
167 ret
= snprintf(val
, size
, "%.*s", (int)ns
->len
, ns
->str
);
175 static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info
*ci
, char *val
,
178 return snprintf(val
, size
, "%lld", ci
->i_files
+ ci
->i_subdirs
);
181 static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info
*ci
, char *val
,
184 return snprintf(val
, size
, "%lld", ci
->i_files
);
187 static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info
*ci
, char *val
,
190 return snprintf(val
, size
, "%lld", ci
->i_subdirs
);
193 static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info
*ci
, char *val
,
196 return snprintf(val
, size
, "%lld", ci
->i_rfiles
+ ci
->i_rsubdirs
);
199 static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info
*ci
, char *val
,
202 return snprintf(val
, size
, "%lld", ci
->i_rfiles
);
205 static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info
*ci
, char *val
,
208 return snprintf(val
, size
, "%lld", ci
->i_rsubdirs
);
211 static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info
*ci
, char *val
,
214 return snprintf(val
, size
, "%lld", ci
->i_rbytes
);
217 static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info
*ci
, char *val
,
220 return snprintf(val
, size
, "%ld.09%ld", (long)ci
->i_rctime
.tv_sec
,
221 (long)ci
->i_rctime
.tv_nsec
);
226 static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info
*ci
)
228 return (ci
->i_max_files
|| ci
->i_max_bytes
);
231 static size_t ceph_vxattrcb_quota(struct ceph_inode_info
*ci
, char *val
,
234 return snprintf(val
, size
, "max_bytes=%llu max_files=%llu",
235 ci
->i_max_bytes
, ci
->i_max_files
);
238 static size_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info
*ci
,
239 char *val
, size_t size
)
241 return snprintf(val
, size
, "%llu", ci
->i_max_bytes
);
244 static size_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info
*ci
,
245 char *val
, size_t size
)
247 return snprintf(val
, size
, "%llu", ci
->i_max_files
);
250 #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
251 #define CEPH_XATTR_NAME2(_type, _name, _name2) \
252 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
254 #define XATTR_NAME_CEPH(_type, _name) \
256 .name = CEPH_XATTR_NAME(_type, _name), \
257 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
258 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
263 #define XATTR_LAYOUT_FIELD(_type, _name, _field) \
265 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
266 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
267 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
270 .exists_cb = ceph_vxattrcb_layout_exists, \
272 #define XATTR_QUOTA_FIELD(_type, _name) \
274 .name = CEPH_XATTR_NAME(_type, _name), \
275 .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \
276 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
279 .exists_cb = ceph_vxattrcb_quota_exists, \
282 static struct ceph_vxattr ceph_dir_vxattrs
[] = {
284 .name
= "ceph.dir.layout",
285 .name_size
= sizeof("ceph.dir.layout"),
286 .getxattr_cb
= ceph_vxattrcb_layout
,
289 .exists_cb
= ceph_vxattrcb_layout_exists
,
291 XATTR_LAYOUT_FIELD(dir
, layout
, stripe_unit
),
292 XATTR_LAYOUT_FIELD(dir
, layout
, stripe_count
),
293 XATTR_LAYOUT_FIELD(dir
, layout
, object_size
),
294 XATTR_LAYOUT_FIELD(dir
, layout
, pool
),
295 XATTR_LAYOUT_FIELD(dir
, layout
, pool_namespace
),
296 XATTR_NAME_CEPH(dir
, entries
),
297 XATTR_NAME_CEPH(dir
, files
),
298 XATTR_NAME_CEPH(dir
, subdirs
),
299 XATTR_NAME_CEPH(dir
, rentries
),
300 XATTR_NAME_CEPH(dir
, rfiles
),
301 XATTR_NAME_CEPH(dir
, rsubdirs
),
302 XATTR_NAME_CEPH(dir
, rbytes
),
303 XATTR_NAME_CEPH(dir
, rctime
),
305 .name
= "ceph.quota",
306 .name_size
= sizeof("ceph.quota"),
307 .getxattr_cb
= ceph_vxattrcb_quota
,
310 .exists_cb
= ceph_vxattrcb_quota_exists
,
312 XATTR_QUOTA_FIELD(quota
, max_bytes
),
313 XATTR_QUOTA_FIELD(quota
, max_files
),
314 { .name
= NULL
, 0 } /* Required table terminator */
316 static size_t ceph_dir_vxattrs_name_size
; /* total size of all names */
320 static struct ceph_vxattr ceph_file_vxattrs
[] = {
322 .name
= "ceph.file.layout",
323 .name_size
= sizeof("ceph.file.layout"),
324 .getxattr_cb
= ceph_vxattrcb_layout
,
327 .exists_cb
= ceph_vxattrcb_layout_exists
,
329 XATTR_LAYOUT_FIELD(file
, layout
, stripe_unit
),
330 XATTR_LAYOUT_FIELD(file
, layout
, stripe_count
),
331 XATTR_LAYOUT_FIELD(file
, layout
, object_size
),
332 XATTR_LAYOUT_FIELD(file
, layout
, pool
),
333 XATTR_LAYOUT_FIELD(file
, layout
, pool_namespace
),
334 { .name
= NULL
, 0 } /* Required table terminator */
336 static size_t ceph_file_vxattrs_name_size
; /* total size of all names */
338 static struct ceph_vxattr
*ceph_inode_vxattrs(struct inode
*inode
)
340 if (S_ISDIR(inode
->i_mode
))
341 return ceph_dir_vxattrs
;
342 else if (S_ISREG(inode
->i_mode
))
343 return ceph_file_vxattrs
;
347 static size_t ceph_vxattrs_name_size(struct ceph_vxattr
*vxattrs
)
349 if (vxattrs
== ceph_dir_vxattrs
)
350 return ceph_dir_vxattrs_name_size
;
351 if (vxattrs
== ceph_file_vxattrs
)
352 return ceph_file_vxattrs_name_size
;
358 * Compute the aggregate size (including terminating '\0') of all
359 * virtual extended attribute names in the given vxattr table.
361 static size_t __init
vxattrs_name_size(struct ceph_vxattr
*vxattrs
)
363 struct ceph_vxattr
*vxattr
;
366 for (vxattr
= vxattrs
; vxattr
->name
; vxattr
++)
368 size
+= vxattr
->name_size
;
373 /* Routines called at initialization and exit time */
375 void __init
ceph_xattr_init(void)
377 ceph_dir_vxattrs_name_size
= vxattrs_name_size(ceph_dir_vxattrs
);
378 ceph_file_vxattrs_name_size
= vxattrs_name_size(ceph_file_vxattrs
);
381 void ceph_xattr_exit(void)
383 ceph_dir_vxattrs_name_size
= 0;
384 ceph_file_vxattrs_name_size
= 0;
387 static struct ceph_vxattr
*ceph_match_vxattr(struct inode
*inode
,
390 struct ceph_vxattr
*vxattr
= ceph_inode_vxattrs(inode
);
393 while (vxattr
->name
) {
394 if (!strcmp(vxattr
->name
, name
))
403 static int __set_xattr(struct ceph_inode_info
*ci
,
404 const char *name
, int name_len
,
405 const char *val
, int val_len
,
406 int flags
, int update_xattr
,
407 struct ceph_inode_xattr
**newxattr
)
410 struct rb_node
*parent
= NULL
;
411 struct ceph_inode_xattr
*xattr
= NULL
;
415 p
= &ci
->i_xattrs
.index
.rb_node
;
418 xattr
= rb_entry(parent
, struct ceph_inode_xattr
, node
);
419 c
= strncmp(name
, xattr
->name
, min(name_len
, xattr
->name_len
));
425 if (name_len
== xattr
->name_len
)
427 else if (name_len
< xattr
->name_len
)
438 if (xattr
&& (flags
& XATTR_CREATE
))
440 else if (!xattr
&& (flags
& XATTR_REPLACE
))
448 if (update_xattr
< 0) {
450 __remove_xattr(ci
, xattr
);
461 xattr
->name_len
= name_len
;
462 xattr
->should_free_name
= update_xattr
;
464 ci
->i_xattrs
.count
++;
465 dout("__set_xattr count=%d\n", ci
->i_xattrs
.count
);
469 if (xattr
->should_free_val
)
470 kfree((void *)xattr
->val
);
476 ci
->i_xattrs
.names_size
-= xattr
->name_len
;
477 ci
->i_xattrs
.vals_size
-= xattr
->val_len
;
479 ci
->i_xattrs
.names_size
+= name_len
;
480 ci
->i_xattrs
.vals_size
+= val_len
;
486 xattr
->val_len
= val_len
;
487 xattr
->dirty
= update_xattr
;
488 xattr
->should_free_val
= (val
&& update_xattr
);
491 rb_link_node(&xattr
->node
, parent
, p
);
492 rb_insert_color(&xattr
->node
, &ci
->i_xattrs
.index
);
493 dout("__set_xattr_val p=%p\n", p
);
496 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
497 ceph_vinop(&ci
->vfs_inode
), xattr
, name
, val_len
, val
);
502 static struct ceph_inode_xattr
*__get_xattr(struct ceph_inode_info
*ci
,
506 struct rb_node
*parent
= NULL
;
507 struct ceph_inode_xattr
*xattr
= NULL
;
508 int name_len
= strlen(name
);
511 p
= &ci
->i_xattrs
.index
.rb_node
;
514 xattr
= rb_entry(parent
, struct ceph_inode_xattr
, node
);
515 c
= strncmp(name
, xattr
->name
, xattr
->name_len
);
516 if (c
== 0 && name_len
> xattr
->name_len
)
523 dout("__get_xattr %s: found %.*s\n", name
,
524 xattr
->val_len
, xattr
->val
);
529 dout("__get_xattr %s: not found\n", name
);
534 static void __free_xattr(struct ceph_inode_xattr
*xattr
)
538 if (xattr
->should_free_name
)
539 kfree((void *)xattr
->name
);
540 if (xattr
->should_free_val
)
541 kfree((void *)xattr
->val
);
546 static int __remove_xattr(struct ceph_inode_info
*ci
,
547 struct ceph_inode_xattr
*xattr
)
552 rb_erase(&xattr
->node
, &ci
->i_xattrs
.index
);
554 if (xattr
->should_free_name
)
555 kfree((void *)xattr
->name
);
556 if (xattr
->should_free_val
)
557 kfree((void *)xattr
->val
);
559 ci
->i_xattrs
.names_size
-= xattr
->name_len
;
560 ci
->i_xattrs
.vals_size
-= xattr
->val_len
;
561 ci
->i_xattrs
.count
--;
567 static char *__copy_xattr_names(struct ceph_inode_info
*ci
,
571 struct ceph_inode_xattr
*xattr
= NULL
;
573 p
= rb_first(&ci
->i_xattrs
.index
);
574 dout("__copy_xattr_names count=%d\n", ci
->i_xattrs
.count
);
577 xattr
= rb_entry(p
, struct ceph_inode_xattr
, node
);
578 memcpy(dest
, xattr
->name
, xattr
->name_len
);
579 dest
[xattr
->name_len
] = '\0';
581 dout("dest=%s %p (%s) (%d/%d)\n", dest
, xattr
, xattr
->name
,
582 xattr
->name_len
, ci
->i_xattrs
.names_size
);
584 dest
+= xattr
->name_len
+ 1;
591 void __ceph_destroy_xattrs(struct ceph_inode_info
*ci
)
593 struct rb_node
*p
, *tmp
;
594 struct ceph_inode_xattr
*xattr
= NULL
;
596 p
= rb_first(&ci
->i_xattrs
.index
);
598 dout("__ceph_destroy_xattrs p=%p\n", p
);
601 xattr
= rb_entry(p
, struct ceph_inode_xattr
, node
);
604 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p
,
605 xattr
->name_len
, xattr
->name
);
606 rb_erase(tmp
, &ci
->i_xattrs
.index
);
611 ci
->i_xattrs
.names_size
= 0;
612 ci
->i_xattrs
.vals_size
= 0;
613 ci
->i_xattrs
.index_version
= 0;
614 ci
->i_xattrs
.count
= 0;
615 ci
->i_xattrs
.index
= RB_ROOT
;
618 static int __build_xattrs(struct inode
*inode
)
619 __releases(ci
->i_ceph_lock
)
620 __acquires(ci
->i_ceph_lock
)
626 const char *name
, *val
;
627 struct ceph_inode_info
*ci
= ceph_inode(inode
);
629 struct ceph_inode_xattr
**xattrs
= NULL
;
633 dout("__build_xattrs() len=%d\n",
634 ci
->i_xattrs
.blob
? (int)ci
->i_xattrs
.blob
->vec
.iov_len
: 0);
636 if (ci
->i_xattrs
.index_version
>= ci
->i_xattrs
.version
)
637 return 0; /* already built */
639 __ceph_destroy_xattrs(ci
);
642 /* updated internal xattr rb tree */
643 if (ci
->i_xattrs
.blob
&& ci
->i_xattrs
.blob
->vec
.iov_len
> 4) {
644 p
= ci
->i_xattrs
.blob
->vec
.iov_base
;
645 end
= p
+ ci
->i_xattrs
.blob
->vec
.iov_len
;
646 ceph_decode_32_safe(&p
, end
, numattr
, bad
);
647 xattr_version
= ci
->i_xattrs
.version
;
648 spin_unlock(&ci
->i_ceph_lock
);
650 xattrs
= kcalloc(numattr
, sizeof(struct ceph_inode_xattr
*),
656 for (i
= 0; i
< numattr
; i
++) {
657 xattrs
[i
] = kmalloc(sizeof(struct ceph_inode_xattr
),
663 spin_lock(&ci
->i_ceph_lock
);
664 if (ci
->i_xattrs
.version
!= xattr_version
) {
665 /* lost a race, retry */
666 for (i
= 0; i
< numattr
; i
++)
674 ceph_decode_32_safe(&p
, end
, len
, bad
);
678 ceph_decode_32_safe(&p
, end
, len
, bad
);
682 err
= __set_xattr(ci
, name
, namelen
, val
, len
,
683 0, 0, &xattrs
[numattr
]);
690 ci
->i_xattrs
.index_version
= ci
->i_xattrs
.version
;
691 ci
->i_xattrs
.dirty
= false;
695 spin_lock(&ci
->i_ceph_lock
);
698 for (i
= 0; i
< numattr
; i
++)
702 ci
->i_xattrs
.names_size
= 0;
706 static int __get_required_blob_size(struct ceph_inode_info
*ci
, int name_size
,
710 * 4 bytes for the length, and additional 4 bytes per each xattr name,
711 * 4 bytes per each value
713 int size
= 4 + ci
->i_xattrs
.count
*(4 + 4) +
714 ci
->i_xattrs
.names_size
+
715 ci
->i_xattrs
.vals_size
;
716 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
717 ci
->i_xattrs
.count
, ci
->i_xattrs
.names_size
,
718 ci
->i_xattrs
.vals_size
);
721 size
+= 4 + 4 + name_size
+ val_size
;
727 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
728 * and swap into place. It returns the old i_xattrs.blob (or NULL) so
729 * that it can be freed by the caller as the i_ceph_lock is likely to be
732 struct ceph_buffer
*__ceph_build_xattrs_blob(struct ceph_inode_info
*ci
)
735 struct ceph_inode_xattr
*xattr
= NULL
;
736 struct ceph_buffer
*old_blob
= NULL
;
739 dout("__build_xattrs_blob %p\n", &ci
->vfs_inode
);
740 if (ci
->i_xattrs
.dirty
) {
741 int need
= __get_required_blob_size(ci
, 0, 0);
743 BUG_ON(need
> ci
->i_xattrs
.prealloc_blob
->alloc_len
);
745 p
= rb_first(&ci
->i_xattrs
.index
);
746 dest
= ci
->i_xattrs
.prealloc_blob
->vec
.iov_base
;
748 ceph_encode_32(&dest
, ci
->i_xattrs
.count
);
750 xattr
= rb_entry(p
, struct ceph_inode_xattr
, node
);
752 ceph_encode_32(&dest
, xattr
->name_len
);
753 memcpy(dest
, xattr
->name
, xattr
->name_len
);
754 dest
+= xattr
->name_len
;
755 ceph_encode_32(&dest
, xattr
->val_len
);
756 memcpy(dest
, xattr
->val
, xattr
->val_len
);
757 dest
+= xattr
->val_len
;
762 /* adjust buffer len; it may be larger than we need */
763 ci
->i_xattrs
.prealloc_blob
->vec
.iov_len
=
764 dest
- ci
->i_xattrs
.prealloc_blob
->vec
.iov_base
;
766 if (ci
->i_xattrs
.blob
)
767 old_blob
= ci
->i_xattrs
.blob
;
768 ci
->i_xattrs
.blob
= ci
->i_xattrs
.prealloc_blob
;
769 ci
->i_xattrs
.prealloc_blob
= NULL
;
770 ci
->i_xattrs
.dirty
= false;
771 ci
->i_xattrs
.version
++;
777 static inline int __get_request_mask(struct inode
*in
) {
778 struct ceph_mds_request
*req
= current
->journal_info
;
780 if (req
&& req
->r_target_inode
== in
) {
781 if (req
->r_op
== CEPH_MDS_OP_LOOKUP
||
782 req
->r_op
== CEPH_MDS_OP_LOOKUPINO
||
783 req
->r_op
== CEPH_MDS_OP_LOOKUPPARENT
||
784 req
->r_op
== CEPH_MDS_OP_GETATTR
) {
785 mask
= le32_to_cpu(req
->r_args
.getattr
.mask
);
786 } else if (req
->r_op
== CEPH_MDS_OP_OPEN
||
787 req
->r_op
== CEPH_MDS_OP_CREATE
) {
788 mask
= le32_to_cpu(req
->r_args
.open
.mask
);
794 ssize_t
__ceph_getxattr(struct inode
*inode
, const char *name
, void *value
,
797 struct ceph_inode_info
*ci
= ceph_inode(inode
);
798 struct ceph_inode_xattr
*xattr
;
799 struct ceph_vxattr
*vxattr
= NULL
;
803 /* let's see if a virtual xattr was requested */
804 vxattr
= ceph_match_vxattr(inode
, name
);
806 err
= ceph_do_getattr(inode
, 0, true);
810 if (!(vxattr
->exists_cb
&& !vxattr
->exists_cb(ci
))) {
811 err
= vxattr
->getxattr_cb(ci
, value
, size
);
812 if (size
&& size
< err
)
818 req_mask
= __get_request_mask(inode
);
820 spin_lock(&ci
->i_ceph_lock
);
821 dout("getxattr %p ver=%lld index_ver=%lld\n", inode
,
822 ci
->i_xattrs
.version
, ci
->i_xattrs
.index_version
);
824 if (ci
->i_xattrs
.version
== 0 ||
825 !((req_mask
& CEPH_CAP_XATTR_SHARED
) ||
826 __ceph_caps_issued_mask(ci
, CEPH_CAP_XATTR_SHARED
, 1))) {
827 spin_unlock(&ci
->i_ceph_lock
);
829 /* security module gets xattr while filling trace */
830 if (current
->journal_info
) {
831 pr_warn_ratelimited("sync getxattr %p "
832 "during filling trace\n", inode
);
836 /* get xattrs from mds (if we don't already have them) */
837 err
= ceph_do_getattr(inode
, CEPH_STAT_CAP_XATTR
, true);
840 spin_lock(&ci
->i_ceph_lock
);
843 err
= __build_xattrs(inode
);
847 err
= -ENODATA
; /* == ENOATTR */
848 xattr
= __get_xattr(ci
, name
);
853 if (size
&& size
< xattr
->val_len
)
856 err
= xattr
->val_len
;
860 memcpy(value
, xattr
->val
, xattr
->val_len
);
862 if (current
->journal_info
&&
863 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
))
864 ci
->i_ceph_flags
|= CEPH_I_SEC_INITED
;
866 spin_unlock(&ci
->i_ceph_lock
);
870 ssize_t
ceph_listxattr(struct dentry
*dentry
, char *names
, size_t size
)
872 struct inode
*inode
= d_inode(dentry
);
873 struct ceph_inode_info
*ci
= ceph_inode(inode
);
874 struct ceph_vxattr
*vxattrs
= ceph_inode_vxattrs(inode
);
881 spin_lock(&ci
->i_ceph_lock
);
882 dout("listxattr %p ver=%lld index_ver=%lld\n", inode
,
883 ci
->i_xattrs
.version
, ci
->i_xattrs
.index_version
);
885 if (ci
->i_xattrs
.version
== 0 ||
886 !__ceph_caps_issued_mask(ci
, CEPH_CAP_XATTR_SHARED
, 1)) {
887 spin_unlock(&ci
->i_ceph_lock
);
888 err
= ceph_do_getattr(inode
, CEPH_STAT_CAP_XATTR
, true);
891 spin_lock(&ci
->i_ceph_lock
);
894 err
= __build_xattrs(inode
);
898 * Start with virtual dir xattr names (if any) (including
899 * terminating '\0' characters for each).
901 vir_namelen
= ceph_vxattrs_name_size(vxattrs
);
903 /* adding 1 byte per each variable due to the null termination */
904 namelen
= ci
->i_xattrs
.names_size
+ ci
->i_xattrs
.count
;
906 if (size
&& vir_namelen
+ namelen
> size
)
909 err
= namelen
+ vir_namelen
;
913 names
= __copy_xattr_names(ci
, names
);
915 /* virtual xattr names, too */
918 for (i
= 0; vxattrs
[i
].name
; i
++) {
919 if (!vxattrs
[i
].hidden
&&
920 !(vxattrs
[i
].exists_cb
&&
921 !vxattrs
[i
].exists_cb(ci
))) {
922 len
= sprintf(names
, "%s", vxattrs
[i
].name
);
930 spin_unlock(&ci
->i_ceph_lock
);
934 static int ceph_sync_setxattr(struct inode
*inode
, const char *name
,
935 const char *value
, size_t size
, int flags
)
937 struct ceph_fs_client
*fsc
= ceph_sb_to_client(inode
->i_sb
);
938 struct ceph_inode_info
*ci
= ceph_inode(inode
);
939 struct ceph_mds_request
*req
;
940 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
941 struct ceph_pagelist
*pagelist
= NULL
;
942 int op
= CEPH_MDS_OP_SETXATTR
;
946 /* copy value into pagelist */
947 pagelist
= kmalloc(sizeof(*pagelist
), GFP_NOFS
);
951 ceph_pagelist_init(pagelist
);
952 err
= ceph_pagelist_append(pagelist
, value
, size
);
956 if (flags
& CEPH_XATTR_REPLACE
)
957 op
= CEPH_MDS_OP_RMXATTR
;
959 flags
|= CEPH_XATTR_REMOVE
;
962 dout("setxattr value=%.*s\n", (int)size
, value
);
965 req
= ceph_mdsc_create_request(mdsc
, op
, USE_AUTH_MDS
);
971 req
->r_path2
= kstrdup(name
, GFP_NOFS
);
973 ceph_mdsc_put_request(req
);
978 if (op
== CEPH_MDS_OP_SETXATTR
) {
979 req
->r_args
.setxattr
.flags
= cpu_to_le32(flags
);
980 req
->r_pagelist
= pagelist
;
984 req
->r_inode
= inode
;
987 req
->r_inode_drop
= CEPH_CAP_XATTR_SHARED
;
989 dout("xattr.ver (before): %lld\n", ci
->i_xattrs
.version
);
990 err
= ceph_mdsc_do_request(mdsc
, NULL
, req
);
991 ceph_mdsc_put_request(req
);
992 dout("xattr.ver (after): %lld\n", ci
->i_xattrs
.version
);
996 ceph_pagelist_release(pagelist
);
1000 int __ceph_setxattr(struct inode
*inode
, const char *name
,
1001 const void *value
, size_t size
, int flags
)
1003 struct ceph_vxattr
*vxattr
;
1004 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1005 struct ceph_mds_client
*mdsc
= ceph_sb_to_client(inode
->i_sb
)->mdsc
;
1006 struct ceph_cap_flush
*prealloc_cf
= NULL
;
1007 struct ceph_buffer
*old_blob
= NULL
;
1011 int name_len
= strlen(name
);
1013 char *newname
= NULL
;
1014 char *newval
= NULL
;
1015 struct ceph_inode_xattr
*xattr
= NULL
;
1016 int required_blob_size
;
1017 bool lock_snap_rwsem
= false;
1019 if (ceph_snap(inode
) != CEPH_NOSNAP
)
1022 vxattr
= ceph_match_vxattr(inode
, name
);
1023 if (vxattr
&& vxattr
->readonly
)
1026 /* pass any unhandled ceph.* xattrs through to the MDS */
1027 if (!strncmp(name
, XATTR_CEPH_PREFIX
, XATTR_CEPH_PREFIX_LEN
))
1028 goto do_sync_unlocked
;
1030 /* preallocate memory for xattr name, value, index node */
1032 newname
= kmemdup(name
, name_len
+ 1, GFP_NOFS
);
1037 newval
= kmemdup(value
, val_len
, GFP_NOFS
);
1042 xattr
= kmalloc(sizeof(struct ceph_inode_xattr
), GFP_NOFS
);
1046 prealloc_cf
= ceph_alloc_cap_flush();
1050 spin_lock(&ci
->i_ceph_lock
);
1052 issued
= __ceph_caps_issued(ci
, NULL
);
1053 if (ci
->i_xattrs
.version
== 0 || !(issued
& CEPH_CAP_XATTR_EXCL
))
1056 if (!lock_snap_rwsem
&& !ci
->i_head_snapc
) {
1057 lock_snap_rwsem
= true;
1058 if (!down_read_trylock(&mdsc
->snap_rwsem
)) {
1059 spin_unlock(&ci
->i_ceph_lock
);
1060 down_read(&mdsc
->snap_rwsem
);
1061 spin_lock(&ci
->i_ceph_lock
);
1066 dout("setxattr %p issued %s\n", inode
, ceph_cap_string(issued
));
1067 __build_xattrs(inode
);
1069 required_blob_size
= __get_required_blob_size(ci
, name_len
, val_len
);
1071 if (!ci
->i_xattrs
.prealloc_blob
||
1072 required_blob_size
> ci
->i_xattrs
.prealloc_blob
->alloc_len
) {
1073 struct ceph_buffer
*blob
;
1075 spin_unlock(&ci
->i_ceph_lock
);
1076 ceph_buffer_put(old_blob
); /* Shouldn't be required */
1077 dout(" pre-allocating new blob size=%d\n", required_blob_size
);
1078 blob
= ceph_buffer_new(required_blob_size
, GFP_NOFS
);
1080 goto do_sync_unlocked
;
1081 spin_lock(&ci
->i_ceph_lock
);
1082 /* prealloc_blob can't be released while holding i_ceph_lock */
1083 if (ci
->i_xattrs
.prealloc_blob
)
1084 old_blob
= ci
->i_xattrs
.prealloc_blob
;
1085 ci
->i_xattrs
.prealloc_blob
= blob
;
1089 err
= __set_xattr(ci
, newname
, name_len
, newval
, val_len
,
1090 flags
, value
? 1 : -1, &xattr
);
1093 dirty
= __ceph_mark_dirty_caps(ci
, CEPH_CAP_XATTR_EXCL
,
1095 ci
->i_xattrs
.dirty
= true;
1096 inode
->i_ctime
= current_time(inode
);
1099 spin_unlock(&ci
->i_ceph_lock
);
1100 ceph_buffer_put(old_blob
);
1101 if (lock_snap_rwsem
)
1102 up_read(&mdsc
->snap_rwsem
);
1104 __mark_inode_dirty(inode
, dirty
);
1105 ceph_free_cap_flush(prealloc_cf
);
1109 spin_unlock(&ci
->i_ceph_lock
);
1111 if (lock_snap_rwsem
)
1112 up_read(&mdsc
->snap_rwsem
);
1114 /* security module set xattr while filling trace */
1115 if (current
->journal_info
) {
1116 pr_warn_ratelimited("sync setxattr %p "
1117 "during filling trace\n", inode
);
1120 err
= ceph_sync_setxattr(inode
, name
, value
, size
, flags
);
1123 ceph_free_cap_flush(prealloc_cf
);
1130 static int ceph_get_xattr_handler(const struct xattr_handler
*handler
,
1131 struct dentry
*dentry
, struct inode
*inode
,
1132 const char *name
, void *value
, size_t size
)
1134 if (!ceph_is_valid_xattr(name
))
1136 return __ceph_getxattr(inode
, name
, value
, size
);
1139 static int ceph_set_xattr_handler(const struct xattr_handler
*handler
,
1140 struct dentry
*unused
, struct inode
*inode
,
1141 const char *name
, const void *value
,
1142 size_t size
, int flags
)
1144 if (!ceph_is_valid_xattr(name
))
1146 return __ceph_setxattr(inode
, name
, value
, size
, flags
);
1149 static const struct xattr_handler ceph_other_xattr_handler
= {
1150 .prefix
= "", /* match any name => handlers called with full name */
1151 .get
= ceph_get_xattr_handler
,
1152 .set
= ceph_set_xattr_handler
,
1155 #ifdef CONFIG_SECURITY
1156 bool ceph_security_xattr_wanted(struct inode
*in
)
1158 return in
->i_security
!= NULL
;
1161 bool ceph_security_xattr_deadlock(struct inode
*in
)
1163 struct ceph_inode_info
*ci
;
1165 if (!in
->i_security
)
1167 ci
= ceph_inode(in
);
1168 spin_lock(&ci
->i_ceph_lock
);
1169 ret
= !(ci
->i_ceph_flags
& CEPH_I_SEC_INITED
) &&
1170 !(ci
->i_xattrs
.version
> 0 &&
1171 __ceph_caps_issued_mask(ci
, CEPH_CAP_XATTR_SHARED
, 0));
1172 spin_unlock(&ci
->i_ceph_lock
);