1 // SPDX-License-Identifier: GPL-2.0
5 #include "bkey_methods.h"
6 #include "btree_update.h"
13 #include <linux/dcache.h>
14 #include <linux/posix_acl_xattr.h>
15 #include <linux/xattr.h>
17 static const struct xattr_handler
*bch2_xattr_type_to_handler(unsigned);
19 static u64
bch2_xattr_hash(const struct bch_hash_info
*info
,
20 const struct xattr_search_key
*key
)
22 struct bch_str_hash_ctx ctx
;
24 bch2_str_hash_init(&ctx
, info
);
25 bch2_str_hash_update(&ctx
, info
, &key
->type
, sizeof(key
->type
));
26 bch2_str_hash_update(&ctx
, info
, key
->name
.name
, key
->name
.len
);
28 return bch2_str_hash_end(&ctx
, info
);
31 static u64
xattr_hash_key(const struct bch_hash_info
*info
, const void *key
)
33 return bch2_xattr_hash(info
, key
);
36 static u64
xattr_hash_bkey(const struct bch_hash_info
*info
, struct bkey_s_c k
)
38 struct bkey_s_c_xattr x
= bkey_s_c_to_xattr(k
);
40 return bch2_xattr_hash(info
,
41 &X_SEARCH(x
.v
->x_type
, x
.v
->x_name
, x
.v
->x_name_len
));
44 static bool xattr_cmp_key(struct bkey_s_c _l
, const void *_r
)
46 struct bkey_s_c_xattr l
= bkey_s_c_to_xattr(_l
);
47 const struct xattr_search_key
*r
= _r
;
49 return l
.v
->x_type
!= r
->type
||
50 l
.v
->x_name_len
!= r
->name
.len
||
51 memcmp(l
.v
->x_name
, r
->name
.name
, r
->name
.len
);
54 static bool xattr_cmp_bkey(struct bkey_s_c _l
, struct bkey_s_c _r
)
56 struct bkey_s_c_xattr l
= bkey_s_c_to_xattr(_l
);
57 struct bkey_s_c_xattr r
= bkey_s_c_to_xattr(_r
);
59 return l
.v
->x_type
!= r
.v
->x_type
||
60 l
.v
->x_name_len
!= r
.v
->x_name_len
||
61 memcmp(l
.v
->x_name
, r
.v
->x_name
, r
.v
->x_name_len
);
64 const struct bch_hash_desc bch2_xattr_hash_desc
= {
65 .btree_id
= BTREE_ID_xattrs
,
66 .key_type
= KEY_TYPE_xattr
,
67 .hash_key
= xattr_hash_key
,
68 .hash_bkey
= xattr_hash_bkey
,
69 .cmp_key
= xattr_cmp_key
,
70 .cmp_bkey
= xattr_cmp_bkey
,
73 int bch2_xattr_validate(struct bch_fs
*c
, struct bkey_s_c k
,
74 enum bch_validate_flags flags
)
76 struct bkey_s_c_xattr xattr
= bkey_s_c_to_xattr(k
);
77 unsigned val_u64s
= xattr_val_u64s(xattr
.v
->x_name_len
,
78 le16_to_cpu(xattr
.v
->x_val_len
));
81 bkey_fsck_err_on(bkey_val_u64s(k
.k
) < val_u64s
,
82 c
, xattr_val_size_too_small
,
83 "value too small (%zu < %u)",
84 bkey_val_u64s(k
.k
), val_u64s
);
87 val_u64s
= xattr_val_u64s(xattr
.v
->x_name_len
,
88 le16_to_cpu(xattr
.v
->x_val_len
) + 4);
90 bkey_fsck_err_on(bkey_val_u64s(k
.k
) > val_u64s
,
91 c
, xattr_val_size_too_big
,
92 "value too big (%zu > %u)",
93 bkey_val_u64s(k
.k
), val_u64s
);
95 bkey_fsck_err_on(!bch2_xattr_type_to_handler(xattr
.v
->x_type
),
96 c
, xattr_invalid_type
,
97 "invalid type (%u)", xattr
.v
->x_type
);
99 bkey_fsck_err_on(memchr(xattr
.v
->x_name
, '\0', xattr
.v
->x_name_len
),
100 c
, xattr_name_invalid_chars
,
101 "xattr name has invalid characters");
106 void bch2_xattr_to_text(struct printbuf
*out
, struct bch_fs
*c
,
109 const struct xattr_handler
*handler
;
110 struct bkey_s_c_xattr xattr
= bkey_s_c_to_xattr(k
);
112 handler
= bch2_xattr_type_to_handler(xattr
.v
->x_type
);
113 if (handler
&& handler
->prefix
)
114 prt_printf(out
, "%s", handler
->prefix
);
116 prt_printf(out
, "(type %u)", xattr
.v
->x_type
);
118 prt_printf(out
, "(unknown type %u)", xattr
.v
->x_type
);
120 unsigned name_len
= xattr
.v
->x_name_len
;
121 unsigned val_len
= le16_to_cpu(xattr
.v
->x_val_len
);
122 unsigned max_name_val_bytes
= bkey_val_bytes(xattr
.k
) -
123 offsetof(struct bch_xattr
, x_name
);
125 val_len
= min_t(int, val_len
, max_name_val_bytes
- name_len
);
126 name_len
= min(name_len
, max_name_val_bytes
);
128 prt_printf(out
, "%.*s:%.*s",
129 name_len
, xattr
.v
->x_name
,
130 val_len
, (char *) xattr_val(xattr
.v
));
132 if (xattr
.v
->x_type
== KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS
||
133 xattr
.v
->x_type
== KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT
) {
135 bch2_acl_to_text(out
, xattr_val(xattr
.v
),
136 le16_to_cpu(xattr
.v
->x_val_len
));
140 static int bch2_xattr_get_trans(struct btree_trans
*trans
, struct bch_inode_info
*inode
,
141 const char *name
, void *buffer
, size_t size
, int type
)
143 struct bch_hash_info hash
= bch2_hash_info_init(trans
->c
, &inode
->ei_inode
);
144 struct xattr_search_key search
= X_SEARCH(type
, name
, strlen(name
));
145 struct btree_iter iter
;
146 struct bkey_s_c k
= bch2_hash_lookup(trans
, &iter
, bch2_xattr_hash_desc
, &hash
,
147 inode_inum(inode
), &search
, 0);
148 int ret
= bkey_err(k
);
152 struct bkey_s_c_xattr xattr
= bkey_s_c_to_xattr(k
);
153 ret
= le16_to_cpu(xattr
.v
->x_val_len
);
158 memcpy(buffer
, xattr_val(xattr
.v
), ret
);
160 bch2_trans_iter_exit(trans
, &iter
);
164 int bch2_xattr_set(struct btree_trans
*trans
, subvol_inum inum
,
165 struct bch_inode_unpacked
*inode_u
,
166 const struct bch_hash_info
*hash_info
,
167 const char *name
, const void *value
, size_t size
,
170 struct bch_fs
*c
= trans
->c
;
171 struct btree_iter inode_iter
= { NULL
};
174 ret
= bch2_subvol_is_ro_trans(trans
, inum
.subvol
) ?:
175 bch2_inode_peek(trans
, &inode_iter
, inode_u
, inum
, BTREE_ITER_intent
);
179 inode_u
->bi_ctime
= bch2_current_time(c
);
181 ret
= bch2_inode_write(trans
, &inode_iter
, inode_u
);
182 bch2_trans_iter_exit(trans
, &inode_iter
);
188 struct bkey_i_xattr
*xattr
;
189 unsigned namelen
= strlen(name
);
190 unsigned u64s
= BKEY_U64s
+
191 xattr_val_u64s(namelen
, size
);
196 xattr
= bch2_trans_kmalloc(trans
, u64s
* sizeof(u64
));
198 return PTR_ERR(xattr
);
200 bkey_xattr_init(&xattr
->k_i
);
201 xattr
->k
.u64s
= u64s
;
202 xattr
->v
.x_type
= type
;
203 xattr
->v
.x_name_len
= namelen
;
204 xattr
->v
.x_val_len
= cpu_to_le16(size
);
205 memcpy(xattr
->v
.x_name
, name
, namelen
);
206 memcpy(xattr_val(&xattr
->v
), value
, size
);
208 ret
= bch2_hash_set(trans
, bch2_xattr_hash_desc
, hash_info
,
210 (flags
& XATTR_CREATE
? STR_HASH_must_create
: 0)|
211 (flags
& XATTR_REPLACE
? STR_HASH_must_replace
: 0));
213 struct xattr_search_key search
=
214 X_SEARCH(type
, name
, strlen(name
));
216 ret
= bch2_hash_delete(trans
, bch2_xattr_hash_desc
,
217 hash_info
, inum
, &search
);
220 if (bch2_err_matches(ret
, ENOENT
))
221 ret
= flags
& XATTR_REPLACE
? -ENODATA
: 0;
232 static int __bch2_xattr_emit(const char *prefix
,
233 const char *name
, size_t name_len
,
234 struct xattr_buf
*buf
)
236 const size_t prefix_len
= strlen(prefix
);
237 const size_t total_len
= prefix_len
+ name_len
+ 1;
240 if (buf
->used
+ total_len
> buf
->len
)
243 memcpy(buf
->buf
+ buf
->used
, prefix
, prefix_len
);
244 memcpy(buf
->buf
+ buf
->used
+ prefix_len
,
246 buf
->buf
[buf
->used
+ prefix_len
+ name_len
] = '\0';
249 buf
->used
+= total_len
;
253 static inline const char *bch2_xattr_prefix(unsigned type
, struct dentry
*dentry
)
255 const struct xattr_handler
*handler
= bch2_xattr_type_to_handler(type
);
257 if (!xattr_handler_can_list(handler
, dentry
))
260 return xattr_prefix(handler
);
263 static int bch2_xattr_emit(struct dentry
*dentry
,
264 const struct bch_xattr
*xattr
,
265 struct xattr_buf
*buf
)
269 prefix
= bch2_xattr_prefix(xattr
->x_type
, dentry
);
273 return __bch2_xattr_emit(prefix
, xattr
->x_name
, xattr
->x_name_len
, buf
);
276 static int bch2_xattr_list_bcachefs(struct bch_fs
*c
,
277 struct bch_inode_unpacked
*inode
,
278 struct xattr_buf
*buf
,
281 const char *prefix
= all
? "bcachefs_effective." : "bcachefs.";
286 for (id
= 0; id
< Inode_opt_nr
; id
++) {
287 v
= bch2_inode_opt_get(inode
, id
);
292 !(inode
->bi_fields_set
& (1 << id
)))
295 ret
= __bch2_xattr_emit(prefix
, bch2_inode_opts
[id
],
296 strlen(bch2_inode_opts
[id
]), buf
);
304 ssize_t
bch2_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
306 struct bch_fs
*c
= dentry
->d_sb
->s_fs_info
;
307 struct bch_inode_info
*inode
= to_bch_ei(dentry
->d_inode
);
308 struct xattr_buf buf
= { .buf
= buffer
, .len
= buffer_size
};
309 u64 offset
= 0, inum
= inode
->ei_inode
.bi_inum
;
311 int ret
= bch2_trans_run(c
,
312 for_each_btree_key_in_subvolume_upto(trans
, iter
, BTREE_ID_xattrs
,
315 inode
->ei_inum
.subvol
, 0, k
, ({
316 if (k
.k
->type
!= KEY_TYPE_xattr
)
319 bch2_xattr_emit(dentry
, bkey_s_c_to_xattr(k
).v
, &buf
);
321 bch2_xattr_list_bcachefs(c
, &inode
->ei_inode
, &buf
, false) ?:
322 bch2_xattr_list_bcachefs(c
, &inode
->ei_inode
, &buf
, true);
324 return ret
? bch2_err_class(ret
) : buf
.used
;
327 static int bch2_xattr_get_handler(const struct xattr_handler
*handler
,
328 struct dentry
*dentry
, struct inode
*vinode
,
329 const char *name
, void *buffer
, size_t size
)
331 struct bch_inode_info
*inode
= to_bch_ei(vinode
);
332 struct bch_fs
*c
= inode
->v
.i_sb
->s_fs_info
;
333 int ret
= bch2_trans_do(c
,
334 bch2_xattr_get_trans(trans
, inode
, name
, buffer
, size
, handler
->flags
));
336 if (ret
< 0 && bch2_err_matches(ret
, ENOENT
))
339 return bch2_err_class(ret
);
342 static int bch2_xattr_set_handler(const struct xattr_handler
*handler
,
343 struct mnt_idmap
*idmap
,
344 struct dentry
*dentry
, struct inode
*vinode
,
345 const char *name
, const void *value
,
346 size_t size
, int flags
)
348 struct bch_inode_info
*inode
= to_bch_ei(vinode
);
349 struct bch_fs
*c
= inode
->v
.i_sb
->s_fs_info
;
350 struct bch_hash_info hash
= bch2_hash_info_init(c
, &inode
->ei_inode
);
351 struct bch_inode_unpacked inode_u
;
354 ret
= bch2_trans_run(c
,
355 commit_do(trans
, NULL
, NULL
, 0,
356 bch2_xattr_set(trans
, inode_inum(inode
), &inode_u
,
357 &hash
, name
, value
, size
,
358 handler
->flags
, flags
)) ?:
359 (bch2_inode_update_after_write(trans
, inode
, &inode_u
, ATTR_CTIME
), 0));
361 return bch2_err_class(ret
);
364 static const struct xattr_handler bch_xattr_user_handler
= {
365 .prefix
= XATTR_USER_PREFIX
,
366 .get
= bch2_xattr_get_handler
,
367 .set
= bch2_xattr_set_handler
,
368 .flags
= KEY_TYPE_XATTR_INDEX_USER
,
371 static bool bch2_xattr_trusted_list(struct dentry
*dentry
)
373 return capable(CAP_SYS_ADMIN
);
376 static const struct xattr_handler bch_xattr_trusted_handler
= {
377 .prefix
= XATTR_TRUSTED_PREFIX
,
378 .list
= bch2_xattr_trusted_list
,
379 .get
= bch2_xattr_get_handler
,
380 .set
= bch2_xattr_set_handler
,
381 .flags
= KEY_TYPE_XATTR_INDEX_TRUSTED
,
384 static const struct xattr_handler bch_xattr_security_handler
= {
385 .prefix
= XATTR_SECURITY_PREFIX
,
386 .get
= bch2_xattr_get_handler
,
387 .set
= bch2_xattr_set_handler
,
388 .flags
= KEY_TYPE_XATTR_INDEX_SECURITY
,
391 #ifndef NO_BCACHEFS_FS
393 static int opt_to_inode_opt(int id
)
396 #define x(name, ...) \
397 case Opt_##name: return Inode_opt_##name;
405 static int __bch2_xattr_bcachefs_get(const struct xattr_handler
*handler
,
406 struct dentry
*dentry
, struct inode
*vinode
,
407 const char *name
, void *buffer
, size_t size
,
410 struct bch_inode_info
*inode
= to_bch_ei(vinode
);
411 struct bch_fs
*c
= inode
->v
.i_sb
->s_fs_info
;
412 struct bch_opts opts
=
413 bch2_inode_opts_to_opts(&inode
->ei_inode
);
414 const struct bch_option
*opt
;
415 int id
, inode_opt_id
;
416 struct printbuf out
= PRINTBUF
;
420 id
= bch2_opt_lookup(name
);
421 if (id
< 0 || !bch2_opt_is_inode_opt(id
))
424 inode_opt_id
= opt_to_inode_opt(id
);
425 if (inode_opt_id
< 0)
428 opt
= bch2_opt_table
+ id
;
430 if (!bch2_opt_defined_by_id(&opts
, id
))
434 !(inode
->ei_inode
.bi_fields_set
& (1 << inode_opt_id
)))
437 v
= bch2_opt_get_by_id(&opts
, id
);
438 bch2_opt_to_text(&out
, c
, c
->disk_sb
.sb
, opt
, v
, 0);
442 if (out
.allocation_failure
) {
448 memcpy(buffer
, out
.buf
, out
.pos
);
455 static int bch2_xattr_bcachefs_get(const struct xattr_handler
*handler
,
456 struct dentry
*dentry
, struct inode
*vinode
,
457 const char *name
, void *buffer
, size_t size
)
459 return __bch2_xattr_bcachefs_get(handler
, dentry
, vinode
,
460 name
, buffer
, size
, false);
463 struct inode_opt_set
{
469 static int inode_opt_set_fn(struct btree_trans
*trans
,
470 struct bch_inode_info
*inode
,
471 struct bch_inode_unpacked
*bi
,
474 struct inode_opt_set
*s
= p
;
477 bi
->bi_fields_set
|= 1U << s
->id
;
479 bi
->bi_fields_set
&= ~(1U << s
->id
);
481 bch2_inode_opt_set(bi
, s
->id
, s
->v
);
486 static int bch2_xattr_bcachefs_set(const struct xattr_handler
*handler
,
487 struct mnt_idmap
*idmap
,
488 struct dentry
*dentry
, struct inode
*vinode
,
489 const char *name
, const void *value
,
490 size_t size
, int flags
)
492 struct bch_inode_info
*inode
= to_bch_ei(vinode
);
493 struct bch_fs
*c
= inode
->v
.i_sb
->s_fs_info
;
494 const struct bch_option
*opt
;
496 struct inode_opt_set s
;
497 int opt_id
, inode_opt_id
, ret
;
499 opt_id
= bch2_opt_lookup(name
);
503 opt
= bch2_opt_table
+ opt_id
;
505 inode_opt_id
= opt_to_inode_opt(opt_id
);
506 if (inode_opt_id
< 0)
514 buf
= kmalloc(size
+ 1, GFP_KERNEL
);
517 memcpy(buf
, value
, size
);
520 ret
= bch2_opt_parse(c
, opt
, buf
, &v
, NULL
);
526 ret
= bch2_opt_check_may_set(c
, opt_id
, v
);
534 * Check if this option was set on the parent - if so, switched
535 * back to inheriting from the parent:
537 * rename() also has to deal with keeping inherited options up
538 * to date - see bch2_reinherit_attrs()
540 spin_lock(&dentry
->d_lock
);
541 if (!IS_ROOT(dentry
)) {
542 struct bch_inode_info
*dir
=
543 to_bch_ei(d_inode(dentry
->d_parent
));
545 s
.v
= bch2_inode_opt_get(&dir
->ei_inode
, inode_opt_id
);
549 spin_unlock(&dentry
->d_lock
);
554 mutex_lock(&inode
->ei_update_lock
);
555 if (inode_opt_id
== Inode_opt_project
) {
557 * inode fields accessible via the xattr interface are stored
558 * with a +1 bias, so that 0 means unset:
560 ret
= bch2_set_projid(c
, inode
, s
.v
? s
.v
- 1 : 0);
565 ret
= bch2_write_inode(c
, inode
, inode_opt_set_fn
, &s
, 0);
567 mutex_unlock(&inode
->ei_update_lock
);
570 (opt_id
== Opt_background_target
||
571 opt_id
== Opt_background_compression
||
572 (opt_id
== Opt_compression
&& !inode_opt_get(c
, &inode
->ei_inode
, background_compression
))))
573 bch2_set_rebalance_needs_scan(c
, inode
->ei_inode
.bi_inum
);
576 return bch2_err_class(ret
);
579 static const struct xattr_handler bch_xattr_bcachefs_handler
= {
580 .prefix
= "bcachefs.",
581 .get
= bch2_xattr_bcachefs_get
,
582 .set
= bch2_xattr_bcachefs_set
,
585 static int bch2_xattr_bcachefs_get_effective(
586 const struct xattr_handler
*handler
,
587 struct dentry
*dentry
, struct inode
*vinode
,
588 const char *name
, void *buffer
, size_t size
)
590 return __bch2_xattr_bcachefs_get(handler
, dentry
, vinode
,
591 name
, buffer
, size
, true);
594 /* Noop - xattrs in the bcachefs_effective namespace are inherited */
595 static int bch2_xattr_bcachefs_set_effective(const struct xattr_handler
*handler
,
596 struct mnt_idmap
*idmap
,
597 struct dentry
*dentry
, struct inode
*vinode
,
598 const char *name
, const void *value
,
599 size_t size
, int flags
)
604 static const struct xattr_handler bch_xattr_bcachefs_effective_handler
= {
605 .prefix
= "bcachefs_effective.",
606 .get
= bch2_xattr_bcachefs_get_effective
,
607 .set
= bch2_xattr_bcachefs_set_effective
,
610 #endif /* NO_BCACHEFS_FS */
612 const struct xattr_handler
*bch2_xattr_handlers
[] = {
613 &bch_xattr_user_handler
,
614 &bch_xattr_trusted_handler
,
615 &bch_xattr_security_handler
,
616 #ifndef NO_BCACHEFS_FS
617 &bch_xattr_bcachefs_handler
,
618 &bch_xattr_bcachefs_effective_handler
,
623 static const struct xattr_handler
*bch_xattr_handler_map
[] = {
624 [KEY_TYPE_XATTR_INDEX_USER
] = &bch_xattr_user_handler
,
625 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS
] =
626 &nop_posix_acl_access
,
627 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT
] =
628 &nop_posix_acl_default
,
629 [KEY_TYPE_XATTR_INDEX_TRUSTED
] = &bch_xattr_trusted_handler
,
630 [KEY_TYPE_XATTR_INDEX_SECURITY
] = &bch_xattr_security_handler
,
633 static const struct xattr_handler
*bch2_xattr_type_to_handler(unsigned type
)
635 return type
< ARRAY_SIZE(bch_xattr_handler_map
)
636 ? bch_xattr_handler_map
[type
]