1 #include <linux/ceph/ceph_debug.h>
4 #include "mds_client.h"
6 #include <linux/ceph/decode.h>
8 #include <linux/xattr.h>
9 #include <linux/posix_acl_xattr.h>
10 #include <linux/slab.h>
12 #define XATTR_CEPH_PREFIX "ceph."
13 #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
15 static int __remove_xattr(struct ceph_inode_info
*ci
,
16 struct ceph_inode_xattr
*xattr
);
19 * List of handlers for synthetic system.* attributes. Other
20 * attributes are handled directly.
22 const struct xattr_handler
*ceph_xattr_handlers
[] = {
23 #ifdef CONFIG_CEPH_FS_POSIX_ACL
24 &posix_acl_access_xattr_handler
,
25 &posix_acl_default_xattr_handler
,
30 static bool ceph_is_valid_xattr(const char *name
)
32 return !strncmp(name
, XATTR_CEPH_PREFIX
, XATTR_CEPH_PREFIX_LEN
) ||
33 !strncmp(name
, XATTR_SECURITY_PREFIX
,
34 XATTR_SECURITY_PREFIX_LEN
) ||
35 !strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
) ||
36 !strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
37 !strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
);
41 * These define virtual xattrs exposing the recursive directory
42 * statistics and layout metadata.
46 size_t name_size
; /* strlen(name) + 1 (for '\0') */
47 size_t (*getxattr_cb
)(struct ceph_inode_info
*ci
, char *val
,
49 bool readonly
, hidden
;
50 bool (*exists_cb
)(struct ceph_inode_info
*ci
);
55 static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info
*ci
)
58 char *p
= (char *)&ci
->i_layout
;
60 for (s
= 0; s
< sizeof(ci
->i_layout
); s
++, p
++)
66 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 s64 pool
= ceph_file_layout_pg_pool(ci
->i_layout
);
73 const char *pool_name
;
75 dout("ceph_vxattrcb_layout %p\n", &ci
->vfs_inode
);
76 down_read(&osdc
->map_sem
);
77 pool_name
= ceph_pg_pool_name_by_id(osdc
->osdmap
, pool
);
79 ret
= snprintf(val
, size
,
80 "stripe_unit=%lld stripe_count=%lld object_size=%lld pool=%s",
81 (unsigned long long)ceph_file_layout_su(ci
->i_layout
),
82 (unsigned long long)ceph_file_layout_stripe_count(ci
->i_layout
),
83 (unsigned long long)ceph_file_layout_object_size(ci
->i_layout
),
86 ret
= snprintf(val
, size
,
87 "stripe_unit=%lld stripe_count=%lld object_size=%lld pool=%lld",
88 (unsigned long long)ceph_file_layout_su(ci
->i_layout
),
89 (unsigned long long)ceph_file_layout_stripe_count(ci
->i_layout
),
90 (unsigned long long)ceph_file_layout_object_size(ci
->i_layout
),
91 (unsigned long long)pool
);
93 up_read(&osdc
->map_sem
);
97 static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info
*ci
,
98 char *val
, size_t size
)
100 return snprintf(val
, size
, "%lld",
101 (unsigned long long)ceph_file_layout_su(ci
->i_layout
));
104 static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info
*ci
,
105 char *val
, size_t size
)
107 return snprintf(val
, size
, "%lld",
108 (unsigned long long)ceph_file_layout_stripe_count(ci
->i_layout
));
111 static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info
*ci
,
112 char *val
, size_t size
)
114 return snprintf(val
, size
, "%lld",
115 (unsigned long long)ceph_file_layout_object_size(ci
->i_layout
));
118 static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info
*ci
,
119 char *val
, size_t size
)
122 struct ceph_fs_client
*fsc
= ceph_sb_to_client(ci
->vfs_inode
.i_sb
);
123 struct ceph_osd_client
*osdc
= &fsc
->client
->osdc
;
124 s64 pool
= ceph_file_layout_pg_pool(ci
->i_layout
);
125 const char *pool_name
;
127 down_read(&osdc
->map_sem
);
128 pool_name
= ceph_pg_pool_name_by_id(osdc
->osdmap
, pool
);
130 ret
= snprintf(val
, size
, "%s", pool_name
);
132 ret
= snprintf(val
, size
, "%lld", (unsigned long long)pool
);
133 up_read(&osdc
->map_sem
);
139 static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info
*ci
, char *val
,
142 return snprintf(val
, size
, "%lld", ci
->i_files
+ ci
->i_subdirs
);
145 static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info
*ci
, char *val
,
148 return snprintf(val
, size
, "%lld", ci
->i_files
);
151 static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info
*ci
, char *val
,
154 return snprintf(val
, size
, "%lld", ci
->i_subdirs
);
157 static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info
*ci
, char *val
,
160 return snprintf(val
, size
, "%lld", ci
->i_rfiles
+ ci
->i_rsubdirs
);
163 static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info
*ci
, char *val
,
166 return snprintf(val
, size
, "%lld", ci
->i_rfiles
);
169 static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info
*ci
, char *val
,
172 return snprintf(val
, size
, "%lld", ci
->i_rsubdirs
);
175 static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info
*ci
, char *val
,
178 return snprintf(val
, size
, "%lld", ci
->i_rbytes
);
181 static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info
*ci
, char *val
,
184 return snprintf(val
, size
, "%ld.09%ld", (long)ci
->i_rctime
.tv_sec
,
185 (long)ci
->i_rctime
.tv_nsec
);
189 #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
190 #define CEPH_XATTR_NAME2(_type, _name, _name2) \
191 XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
193 #define XATTR_NAME_CEPH(_type, _name) \
195 .name = CEPH_XATTR_NAME(_type, _name), \
196 .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
197 .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
202 #define XATTR_LAYOUT_FIELD(_type, _name, _field) \
204 .name = CEPH_XATTR_NAME2(_type, _name, _field), \
205 .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
206 .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
209 .exists_cb = ceph_vxattrcb_layout_exists, \
212 static struct ceph_vxattr ceph_dir_vxattrs
[] = {
214 .name
= "ceph.dir.layout",
215 .name_size
= sizeof("ceph.dir.layout"),
216 .getxattr_cb
= ceph_vxattrcb_layout
,
219 .exists_cb
= ceph_vxattrcb_layout_exists
,
221 XATTR_LAYOUT_FIELD(dir
, layout
, stripe_unit
),
222 XATTR_LAYOUT_FIELD(dir
, layout
, stripe_count
),
223 XATTR_LAYOUT_FIELD(dir
, layout
, object_size
),
224 XATTR_LAYOUT_FIELD(dir
, layout
, pool
),
225 XATTR_NAME_CEPH(dir
, entries
),
226 XATTR_NAME_CEPH(dir
, files
),
227 XATTR_NAME_CEPH(dir
, subdirs
),
228 XATTR_NAME_CEPH(dir
, rentries
),
229 XATTR_NAME_CEPH(dir
, rfiles
),
230 XATTR_NAME_CEPH(dir
, rsubdirs
),
231 XATTR_NAME_CEPH(dir
, rbytes
),
232 XATTR_NAME_CEPH(dir
, rctime
),
233 { .name
= NULL
, 0 } /* Required table terminator */
235 static size_t ceph_dir_vxattrs_name_size
; /* total size of all names */
239 static struct ceph_vxattr ceph_file_vxattrs
[] = {
241 .name
= "ceph.file.layout",
242 .name_size
= sizeof("ceph.file.layout"),
243 .getxattr_cb
= ceph_vxattrcb_layout
,
246 .exists_cb
= ceph_vxattrcb_layout_exists
,
248 XATTR_LAYOUT_FIELD(file
, layout
, stripe_unit
),
249 XATTR_LAYOUT_FIELD(file
, layout
, stripe_count
),
250 XATTR_LAYOUT_FIELD(file
, layout
, object_size
),
251 XATTR_LAYOUT_FIELD(file
, layout
, pool
),
252 { .name
= NULL
, 0 } /* Required table terminator */
254 static size_t ceph_file_vxattrs_name_size
; /* total size of all names */
256 static struct ceph_vxattr
*ceph_inode_vxattrs(struct inode
*inode
)
258 if (S_ISDIR(inode
->i_mode
))
259 return ceph_dir_vxattrs
;
260 else if (S_ISREG(inode
->i_mode
))
261 return ceph_file_vxattrs
;
265 static size_t ceph_vxattrs_name_size(struct ceph_vxattr
*vxattrs
)
267 if (vxattrs
== ceph_dir_vxattrs
)
268 return ceph_dir_vxattrs_name_size
;
269 if (vxattrs
== ceph_file_vxattrs
)
270 return ceph_file_vxattrs_name_size
;
277 * Compute the aggregate size (including terminating '\0') of all
278 * virtual extended attribute names in the given vxattr table.
280 static size_t __init
vxattrs_name_size(struct ceph_vxattr
*vxattrs
)
282 struct ceph_vxattr
*vxattr
;
285 for (vxattr
= vxattrs
; vxattr
->name
; vxattr
++)
287 size
+= vxattr
->name_size
;
292 /* Routines called at initialization and exit time */
294 void __init
ceph_xattr_init(void)
296 ceph_dir_vxattrs_name_size
= vxattrs_name_size(ceph_dir_vxattrs
);
297 ceph_file_vxattrs_name_size
= vxattrs_name_size(ceph_file_vxattrs
);
300 void ceph_xattr_exit(void)
302 ceph_dir_vxattrs_name_size
= 0;
303 ceph_file_vxattrs_name_size
= 0;
306 static struct ceph_vxattr
*ceph_match_vxattr(struct inode
*inode
,
309 struct ceph_vxattr
*vxattr
= ceph_inode_vxattrs(inode
);
312 while (vxattr
->name
) {
313 if (!strcmp(vxattr
->name
, name
))
322 static int __set_xattr(struct ceph_inode_info
*ci
,
323 const char *name
, int name_len
,
324 const char *val
, int val_len
,
325 int flags
, int update_xattr
,
326 struct ceph_inode_xattr
**newxattr
)
329 struct rb_node
*parent
= NULL
;
330 struct ceph_inode_xattr
*xattr
= NULL
;
334 p
= &ci
->i_xattrs
.index
.rb_node
;
337 xattr
= rb_entry(parent
, struct ceph_inode_xattr
, node
);
338 c
= strncmp(name
, xattr
->name
, min(name_len
, xattr
->name_len
));
344 if (name_len
== xattr
->name_len
)
346 else if (name_len
< xattr
->name_len
)
356 if (xattr
&& (flags
& XATTR_CREATE
))
358 else if (!xattr
&& (flags
& XATTR_REPLACE
))
365 if (update_xattr
< 0) {
367 __remove_xattr(ci
, xattr
);
377 xattr
->name_len
= name_len
;
378 xattr
->should_free_name
= update_xattr
;
380 ci
->i_xattrs
.count
++;
381 dout("__set_xattr count=%d\n", ci
->i_xattrs
.count
);
385 if (xattr
->should_free_val
)
386 kfree((void *)xattr
->val
);
392 ci
->i_xattrs
.names_size
-= xattr
->name_len
;
393 ci
->i_xattrs
.vals_size
-= xattr
->val_len
;
395 ci
->i_xattrs
.names_size
+= name_len
;
396 ci
->i_xattrs
.vals_size
+= val_len
;
402 xattr
->val_len
= val_len
;
403 xattr
->dirty
= update_xattr
;
404 xattr
->should_free_val
= (val
&& update_xattr
);
407 rb_link_node(&xattr
->node
, parent
, p
);
408 rb_insert_color(&xattr
->node
, &ci
->i_xattrs
.index
);
409 dout("__set_xattr_val p=%p\n", p
);
412 dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
413 ceph_vinop(&ci
->vfs_inode
), xattr
, name
, val_len
, val
);
418 static struct ceph_inode_xattr
*__get_xattr(struct ceph_inode_info
*ci
,
422 struct rb_node
*parent
= NULL
;
423 struct ceph_inode_xattr
*xattr
= NULL
;
424 int name_len
= strlen(name
);
427 p
= &ci
->i_xattrs
.index
.rb_node
;
430 xattr
= rb_entry(parent
, struct ceph_inode_xattr
, node
);
431 c
= strncmp(name
, xattr
->name
, xattr
->name_len
);
432 if (c
== 0 && name_len
> xattr
->name_len
)
439 dout("__get_xattr %s: found %.*s\n", name
,
440 xattr
->val_len
, xattr
->val
);
445 dout("__get_xattr %s: not found\n", name
);
450 static void __free_xattr(struct ceph_inode_xattr
*xattr
)
454 if (xattr
->should_free_name
)
455 kfree((void *)xattr
->name
);
456 if (xattr
->should_free_val
)
457 kfree((void *)xattr
->val
);
462 static int __remove_xattr(struct ceph_inode_info
*ci
,
463 struct ceph_inode_xattr
*xattr
)
468 rb_erase(&xattr
->node
, &ci
->i_xattrs
.index
);
470 if (xattr
->should_free_name
)
471 kfree((void *)xattr
->name
);
472 if (xattr
->should_free_val
)
473 kfree((void *)xattr
->val
);
475 ci
->i_xattrs
.names_size
-= xattr
->name_len
;
476 ci
->i_xattrs
.vals_size
-= xattr
->val_len
;
477 ci
->i_xattrs
.count
--;
483 static int __remove_xattr_by_name(struct ceph_inode_info
*ci
,
487 struct ceph_inode_xattr
*xattr
;
490 p
= &ci
->i_xattrs
.index
.rb_node
;
491 xattr
= __get_xattr(ci
, name
);
492 err
= __remove_xattr(ci
, xattr
);
496 static char *__copy_xattr_names(struct ceph_inode_info
*ci
,
500 struct ceph_inode_xattr
*xattr
= NULL
;
502 p
= rb_first(&ci
->i_xattrs
.index
);
503 dout("__copy_xattr_names count=%d\n", ci
->i_xattrs
.count
);
506 xattr
= rb_entry(p
, struct ceph_inode_xattr
, node
);
507 memcpy(dest
, xattr
->name
, xattr
->name_len
);
508 dest
[xattr
->name_len
] = '\0';
510 dout("dest=%s %p (%s) (%d/%d)\n", dest
, xattr
, xattr
->name
,
511 xattr
->name_len
, ci
->i_xattrs
.names_size
);
513 dest
+= xattr
->name_len
+ 1;
520 void __ceph_destroy_xattrs(struct ceph_inode_info
*ci
)
522 struct rb_node
*p
, *tmp
;
523 struct ceph_inode_xattr
*xattr
= NULL
;
525 p
= rb_first(&ci
->i_xattrs
.index
);
527 dout("__ceph_destroy_xattrs p=%p\n", p
);
530 xattr
= rb_entry(p
, struct ceph_inode_xattr
, node
);
533 dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p
,
534 xattr
->name_len
, xattr
->name
);
535 rb_erase(tmp
, &ci
->i_xattrs
.index
);
540 ci
->i_xattrs
.names_size
= 0;
541 ci
->i_xattrs
.vals_size
= 0;
542 ci
->i_xattrs
.index_version
= 0;
543 ci
->i_xattrs
.count
= 0;
544 ci
->i_xattrs
.index
= RB_ROOT
;
547 static int __build_xattrs(struct inode
*inode
)
548 __releases(ci
->i_ceph_lock
)
549 __acquires(ci
->i_ceph_lock
)
555 const char *name
, *val
;
556 struct ceph_inode_info
*ci
= ceph_inode(inode
);
558 struct ceph_inode_xattr
**xattrs
= NULL
;
562 dout("__build_xattrs() len=%d\n",
563 ci
->i_xattrs
.blob
? (int)ci
->i_xattrs
.blob
->vec
.iov_len
: 0);
565 if (ci
->i_xattrs
.index_version
>= ci
->i_xattrs
.version
)
566 return 0; /* already built */
568 __ceph_destroy_xattrs(ci
);
571 /* updated internal xattr rb tree */
572 if (ci
->i_xattrs
.blob
&& ci
->i_xattrs
.blob
->vec
.iov_len
> 4) {
573 p
= ci
->i_xattrs
.blob
->vec
.iov_base
;
574 end
= p
+ ci
->i_xattrs
.blob
->vec
.iov_len
;
575 ceph_decode_32_safe(&p
, end
, numattr
, bad
);
576 xattr_version
= ci
->i_xattrs
.version
;
577 spin_unlock(&ci
->i_ceph_lock
);
579 xattrs
= kcalloc(numattr
, sizeof(struct ceph_xattr
*),
584 memset(xattrs
, 0, numattr
*sizeof(struct ceph_xattr
*));
585 for (i
= 0; i
< numattr
; i
++) {
586 xattrs
[i
] = kmalloc(sizeof(struct ceph_inode_xattr
),
592 spin_lock(&ci
->i_ceph_lock
);
593 if (ci
->i_xattrs
.version
!= xattr_version
) {
594 /* lost a race, retry */
595 for (i
= 0; i
< numattr
; i
++)
603 ceph_decode_32_safe(&p
, end
, len
, bad
);
607 ceph_decode_32_safe(&p
, end
, len
, bad
);
611 err
= __set_xattr(ci
, name
, namelen
, val
, len
,
612 0, 0, &xattrs
[numattr
]);
619 ci
->i_xattrs
.index_version
= ci
->i_xattrs
.version
;
620 ci
->i_xattrs
.dirty
= false;
624 spin_lock(&ci
->i_ceph_lock
);
627 for (i
= 0; i
< numattr
; i
++)
631 ci
->i_xattrs
.names_size
= 0;
635 static int __get_required_blob_size(struct ceph_inode_info
*ci
, int name_size
,
639 * 4 bytes for the length, and additional 4 bytes per each xattr name,
640 * 4 bytes per each value
642 int size
= 4 + ci
->i_xattrs
.count
*(4 + 4) +
643 ci
->i_xattrs
.names_size
+
644 ci
->i_xattrs
.vals_size
;
645 dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
646 ci
->i_xattrs
.count
, ci
->i_xattrs
.names_size
,
647 ci
->i_xattrs
.vals_size
);
650 size
+= 4 + 4 + name_size
+ val_size
;
656 * If there are dirty xattrs, reencode xattrs into the prealloc_blob
657 * and swap into place.
659 void __ceph_build_xattrs_blob(struct ceph_inode_info
*ci
)
662 struct ceph_inode_xattr
*xattr
= NULL
;
665 dout("__build_xattrs_blob %p\n", &ci
->vfs_inode
);
666 if (ci
->i_xattrs
.dirty
) {
667 int need
= __get_required_blob_size(ci
, 0, 0);
669 BUG_ON(need
> ci
->i_xattrs
.prealloc_blob
->alloc_len
);
671 p
= rb_first(&ci
->i_xattrs
.index
);
672 dest
= ci
->i_xattrs
.prealloc_blob
->vec
.iov_base
;
674 ceph_encode_32(&dest
, ci
->i_xattrs
.count
);
676 xattr
= rb_entry(p
, struct ceph_inode_xattr
, node
);
678 ceph_encode_32(&dest
, xattr
->name_len
);
679 memcpy(dest
, xattr
->name
, xattr
->name_len
);
680 dest
+= xattr
->name_len
;
681 ceph_encode_32(&dest
, xattr
->val_len
);
682 memcpy(dest
, xattr
->val
, xattr
->val_len
);
683 dest
+= xattr
->val_len
;
688 /* adjust buffer len; it may be larger than we need */
689 ci
->i_xattrs
.prealloc_blob
->vec
.iov_len
=
690 dest
- ci
->i_xattrs
.prealloc_blob
->vec
.iov_base
;
692 if (ci
->i_xattrs
.blob
)
693 ceph_buffer_put(ci
->i_xattrs
.blob
);
694 ci
->i_xattrs
.blob
= ci
->i_xattrs
.prealloc_blob
;
695 ci
->i_xattrs
.prealloc_blob
= NULL
;
696 ci
->i_xattrs
.dirty
= false;
697 ci
->i_xattrs
.version
++;
701 ssize_t
__ceph_getxattr(struct inode
*inode
, const char *name
, void *value
,
704 struct ceph_inode_info
*ci
= ceph_inode(inode
);
706 struct ceph_inode_xattr
*xattr
;
707 struct ceph_vxattr
*vxattr
= NULL
;
709 if (!ceph_is_valid_xattr(name
))
712 /* let's see if a virtual xattr was requested */
713 vxattr
= ceph_match_vxattr(inode
, name
);
714 if (vxattr
&& !(vxattr
->exists_cb
&& !vxattr
->exists_cb(ci
))) {
715 err
= vxattr
->getxattr_cb(ci
, value
, size
);
719 spin_lock(&ci
->i_ceph_lock
);
720 dout("getxattr %p ver=%lld index_ver=%lld\n", inode
,
721 ci
->i_xattrs
.version
, ci
->i_xattrs
.index_version
);
723 if (__ceph_caps_issued_mask(ci
, CEPH_CAP_XATTR_SHARED
, 1) &&
724 (ci
->i_xattrs
.index_version
>= ci
->i_xattrs
.version
)) {
727 spin_unlock(&ci
->i_ceph_lock
);
728 /* get xattrs from mds (if we don't already have them) */
729 err
= ceph_do_getattr(inode
, CEPH_STAT_CAP_XATTR
);
734 spin_lock(&ci
->i_ceph_lock
);
736 err
= __build_xattrs(inode
);
741 err
= -ENODATA
; /* == ENOATTR */
742 xattr
= __get_xattr(ci
, name
);
747 if (size
&& size
< xattr
->val_len
)
750 err
= xattr
->val_len
;
754 memcpy(value
, xattr
->val
, xattr
->val_len
);
757 spin_unlock(&ci
->i_ceph_lock
);
761 ssize_t
ceph_getxattr(struct dentry
*dentry
, const char *name
, void *value
,
764 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
765 return generic_getxattr(dentry
, name
, value
, size
);
767 return __ceph_getxattr(dentry
->d_inode
, name
, value
, size
);
770 ssize_t
ceph_listxattr(struct dentry
*dentry
, char *names
, size_t size
)
772 struct inode
*inode
= dentry
->d_inode
;
773 struct ceph_inode_info
*ci
= ceph_inode(inode
);
774 struct ceph_vxattr
*vxattrs
= ceph_inode_vxattrs(inode
);
781 spin_lock(&ci
->i_ceph_lock
);
782 dout("listxattr %p ver=%lld index_ver=%lld\n", inode
,
783 ci
->i_xattrs
.version
, ci
->i_xattrs
.index_version
);
785 if (__ceph_caps_issued_mask(ci
, CEPH_CAP_XATTR_SHARED
, 1) &&
786 (ci
->i_xattrs
.index_version
>= ci
->i_xattrs
.version
)) {
789 spin_unlock(&ci
->i_ceph_lock
);
790 err
= ceph_do_getattr(inode
, CEPH_STAT_CAP_XATTR
);
795 spin_lock(&ci
->i_ceph_lock
);
797 err
= __build_xattrs(inode
);
803 * Start with virtual dir xattr names (if any) (including
804 * terminating '\0' characters for each).
806 vir_namelen
= ceph_vxattrs_name_size(vxattrs
);
808 /* adding 1 byte per each variable due to the null termination */
809 namelen
= ci
->i_xattrs
.names_size
+ ci
->i_xattrs
.count
;
811 if (size
&& vir_namelen
+ namelen
> size
)
814 err
= namelen
+ vir_namelen
;
818 names
= __copy_xattr_names(ci
, names
);
820 /* virtual xattr names, too */
823 for (i
= 0; vxattrs
[i
].name
; i
++) {
824 if (!vxattrs
[i
].hidden
&&
825 !(vxattrs
[i
].exists_cb
&&
826 !vxattrs
[i
].exists_cb(ci
))) {
827 len
= sprintf(names
, "%s", vxattrs
[i
].name
);
835 spin_unlock(&ci
->i_ceph_lock
);
839 static int ceph_sync_setxattr(struct dentry
*dentry
, const char *name
,
840 const char *value
, size_t size
, int flags
)
842 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dentry
->d_sb
);
843 struct inode
*inode
= dentry
->d_inode
;
844 struct ceph_inode_info
*ci
= ceph_inode(inode
);
845 struct inode
*parent_inode
;
846 struct ceph_mds_request
*req
;
847 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
850 struct page
**pages
= NULL
;
853 /* copy value into some pages */
854 nr_pages
= calc_pages_for(0, size
);
856 pages
= kmalloc(sizeof(pages
[0])*nr_pages
, GFP_NOFS
);
860 for (i
= 0; i
< nr_pages
; i
++) {
861 pages
[i
] = __page_cache_alloc(GFP_NOFS
);
866 kaddr
= kmap(pages
[i
]);
867 memcpy(kaddr
, value
+ i
*PAGE_CACHE_SIZE
,
868 min(PAGE_CACHE_SIZE
, size
-i
*PAGE_CACHE_SIZE
));
872 dout("setxattr value=%.*s\n", (int)size
, value
);
875 flags
|= CEPH_XATTR_REMOVE
;
878 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_SETXATTR
,
884 req
->r_inode
= inode
;
886 req
->r_inode_drop
= CEPH_CAP_XATTR_SHARED
;
888 req
->r_args
.setxattr
.flags
= cpu_to_le32(flags
);
889 req
->r_path2
= kstrdup(name
, GFP_NOFS
);
891 req
->r_pages
= pages
;
892 req
->r_num_pages
= nr_pages
;
893 req
->r_data_len
= size
;
895 dout("xattr.ver (before): %lld\n", ci
->i_xattrs
.version
);
896 parent_inode
= ceph_get_dentry_parent_inode(dentry
);
897 err
= ceph_mdsc_do_request(mdsc
, parent_inode
, req
);
899 ceph_mdsc_put_request(req
);
900 dout("xattr.ver (after): %lld\n", ci
->i_xattrs
.version
);
904 for (i
= 0; i
< nr_pages
; i
++)
905 __free_page(pages
[i
]);
911 int __ceph_setxattr(struct dentry
*dentry
, const char *name
,
912 const void *value
, size_t size
, int flags
)
914 struct inode
*inode
= dentry
->d_inode
;
915 struct ceph_vxattr
*vxattr
;
916 struct ceph_inode_info
*ci
= ceph_inode(inode
);
920 int name_len
= strlen(name
);
922 char *newname
= NULL
;
924 struct ceph_inode_xattr
*xattr
= NULL
;
925 int required_blob_size
;
927 if (!ceph_is_valid_xattr(name
))
930 vxattr
= ceph_match_vxattr(inode
, name
);
931 if (vxattr
&& vxattr
->readonly
)
934 /* pass any unhandled ceph.* xattrs through to the MDS */
935 if (!strncmp(name
, XATTR_CEPH_PREFIX
, XATTR_CEPH_PREFIX_LEN
))
936 goto do_sync_unlocked
;
938 /* preallocate memory for xattr name, value, index node */
940 newname
= kmemdup(name
, name_len
+ 1, GFP_NOFS
);
945 newval
= kmemdup(value
, val_len
, GFP_NOFS
);
950 xattr
= kmalloc(sizeof(struct ceph_inode_xattr
), GFP_NOFS
);
954 spin_lock(&ci
->i_ceph_lock
);
956 issued
= __ceph_caps_issued(ci
, NULL
);
957 dout("setxattr %p issued %s\n", inode
, ceph_cap_string(issued
));
958 if (!(issued
& CEPH_CAP_XATTR_EXCL
))
960 __build_xattrs(inode
);
962 required_blob_size
= __get_required_blob_size(ci
, name_len
, val_len
);
964 if (!ci
->i_xattrs
.prealloc_blob
||
965 required_blob_size
> ci
->i_xattrs
.prealloc_blob
->alloc_len
) {
966 struct ceph_buffer
*blob
;
968 spin_unlock(&ci
->i_ceph_lock
);
969 dout(" preaallocating new blob size=%d\n", required_blob_size
);
970 blob
= ceph_buffer_new(required_blob_size
, GFP_NOFS
);
973 spin_lock(&ci
->i_ceph_lock
);
974 if (ci
->i_xattrs
.prealloc_blob
)
975 ceph_buffer_put(ci
->i_xattrs
.prealloc_blob
);
976 ci
->i_xattrs
.prealloc_blob
= blob
;
980 err
= __set_xattr(ci
, newname
, name_len
, newval
, val_len
,
981 flags
, value
? 1 : -1, &xattr
);
984 dirty
= __ceph_mark_dirty_caps(ci
, CEPH_CAP_XATTR_EXCL
);
985 ci
->i_xattrs
.dirty
= true;
986 inode
->i_ctime
= CURRENT_TIME
;
989 spin_unlock(&ci
->i_ceph_lock
);
991 __mark_inode_dirty(inode
, dirty
);
995 spin_unlock(&ci
->i_ceph_lock
);
997 err
= ceph_sync_setxattr(dentry
, name
, value
, size
, flags
);
1005 int ceph_setxattr(struct dentry
*dentry
, const char *name
,
1006 const void *value
, size_t size
, int flags
)
1008 if (ceph_snap(dentry
->d_inode
) != CEPH_NOSNAP
)
1011 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
1012 return generic_setxattr(dentry
, name
, value
, size
, flags
);
1014 return __ceph_setxattr(dentry
, name
, value
, size
, flags
);
1017 static int ceph_send_removexattr(struct dentry
*dentry
, const char *name
)
1019 struct ceph_fs_client
*fsc
= ceph_sb_to_client(dentry
->d_sb
);
1020 struct ceph_mds_client
*mdsc
= fsc
->mdsc
;
1021 struct inode
*inode
= dentry
->d_inode
;
1022 struct inode
*parent_inode
;
1023 struct ceph_mds_request
*req
;
1026 req
= ceph_mdsc_create_request(mdsc
, CEPH_MDS_OP_RMXATTR
,
1029 return PTR_ERR(req
);
1030 req
->r_inode
= inode
;
1032 req
->r_inode_drop
= CEPH_CAP_XATTR_SHARED
;
1033 req
->r_num_caps
= 1;
1034 req
->r_path2
= kstrdup(name
, GFP_NOFS
);
1036 parent_inode
= ceph_get_dentry_parent_inode(dentry
);
1037 err
= ceph_mdsc_do_request(mdsc
, parent_inode
, req
);
1039 ceph_mdsc_put_request(req
);
1043 int __ceph_removexattr(struct dentry
*dentry
, const char *name
)
1045 struct inode
*inode
= dentry
->d_inode
;
1046 struct ceph_vxattr
*vxattr
;
1047 struct ceph_inode_info
*ci
= ceph_inode(inode
);
1050 int required_blob_size
;
1053 if (!ceph_is_valid_xattr(name
))
1056 vxattr
= ceph_match_vxattr(inode
, name
);
1057 if (vxattr
&& vxattr
->readonly
)
1060 /* pass any unhandled ceph.* xattrs through to the MDS */
1061 if (!strncmp(name
, XATTR_CEPH_PREFIX
, XATTR_CEPH_PREFIX_LEN
))
1062 goto do_sync_unlocked
;
1065 spin_lock(&ci
->i_ceph_lock
);
1067 issued
= __ceph_caps_issued(ci
, NULL
);
1068 dout("removexattr %p issued %s\n", inode
, ceph_cap_string(issued
));
1070 if (!(issued
& CEPH_CAP_XATTR_EXCL
))
1072 __build_xattrs(inode
);
1074 required_blob_size
= __get_required_blob_size(ci
, 0, 0);
1076 if (!ci
->i_xattrs
.prealloc_blob
||
1077 required_blob_size
> ci
->i_xattrs
.prealloc_blob
->alloc_len
) {
1078 struct ceph_buffer
*blob
;
1080 spin_unlock(&ci
->i_ceph_lock
);
1081 dout(" preaallocating new blob size=%d\n", required_blob_size
);
1082 blob
= ceph_buffer_new(required_blob_size
, GFP_NOFS
);
1085 spin_lock(&ci
->i_ceph_lock
);
1086 if (ci
->i_xattrs
.prealloc_blob
)
1087 ceph_buffer_put(ci
->i_xattrs
.prealloc_blob
);
1088 ci
->i_xattrs
.prealloc_blob
= blob
;
1092 err
= __remove_xattr_by_name(ceph_inode(inode
), name
);
1094 dirty
= __ceph_mark_dirty_caps(ci
, CEPH_CAP_XATTR_EXCL
);
1095 ci
->i_xattrs
.dirty
= true;
1096 inode
->i_ctime
= CURRENT_TIME
;
1097 spin_unlock(&ci
->i_ceph_lock
);
1099 __mark_inode_dirty(inode
, dirty
);
1102 spin_unlock(&ci
->i_ceph_lock
);
1104 err
= ceph_send_removexattr(dentry
, name
);
1109 int ceph_removexattr(struct dentry
*dentry
, const char *name
)
1111 if (ceph_snap(dentry
->d_inode
) != CEPH_NOSNAP
)
1114 if (!strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
1115 return generic_removexattr(dentry
, name
);
1117 return __ceph_removexattr(dentry
, name
);