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_invalid(struct bch_fs
*c
, struct bkey_s_c k
,
74 enum bkey_invalid_flags flags
,
77 struct bkey_s_c_xattr xattr
= bkey_s_c_to_xattr(k
);
78 unsigned val_u64s
= xattr_val_u64s(xattr
.v
->x_name_len
,
79 le16_to_cpu(xattr
.v
->x_val_len
));
82 bkey_fsck_err_on(bkey_val_u64s(k
.k
) < val_u64s
, c
, err
,
83 xattr_val_size_too_small
,
84 "value too small (%zu < %u)",
85 bkey_val_u64s(k
.k
), val_u64s
);
88 val_u64s
= xattr_val_u64s(xattr
.v
->x_name_len
,
89 le16_to_cpu(xattr
.v
->x_val_len
) + 4);
91 bkey_fsck_err_on(bkey_val_u64s(k
.k
) > val_u64s
, c
, err
,
92 xattr_val_size_too_big
,
93 "value too big (%zu > %u)",
94 bkey_val_u64s(k
.k
), val_u64s
);
96 bkey_fsck_err_on(!bch2_xattr_type_to_handler(xattr
.v
->x_type
), c
, err
,
98 "invalid type (%u)", xattr
.v
->x_type
);
100 bkey_fsck_err_on(memchr(xattr
.v
->x_name
, '\0', xattr
.v
->x_name_len
), c
, err
,
101 xattr_name_invalid_chars
,
102 "xattr name has invalid characters");
107 void bch2_xattr_to_text(struct printbuf
*out
, struct bch_fs
*c
,
110 const struct xattr_handler
*handler
;
111 struct bkey_s_c_xattr xattr
= bkey_s_c_to_xattr(k
);
113 handler
= bch2_xattr_type_to_handler(xattr
.v
->x_type
);
114 if (handler
&& handler
->prefix
)
115 prt_printf(out
, "%s", handler
->prefix
);
117 prt_printf(out
, "(type %u)", xattr
.v
->x_type
);
119 prt_printf(out
, "(unknown type %u)", xattr
.v
->x_type
);
121 prt_printf(out
, "%.*s:%.*s",
124 le16_to_cpu(xattr
.v
->x_val_len
),
125 (char *) xattr_val(xattr
.v
));
127 if (xattr
.v
->x_type
== KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS
||
128 xattr
.v
->x_type
== KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT
) {
130 bch2_acl_to_text(out
, xattr_val(xattr
.v
),
131 le16_to_cpu(xattr
.v
->x_val_len
));
135 static int bch2_xattr_get_trans(struct btree_trans
*trans
, struct bch_inode_info
*inode
,
136 const char *name
, void *buffer
, size_t size
, int type
)
138 struct bch_hash_info hash
= bch2_hash_info_init(trans
->c
, &inode
->ei_inode
);
139 struct xattr_search_key search
= X_SEARCH(type
, name
, strlen(name
));
140 struct btree_iter iter
;
141 struct bkey_s_c_xattr xattr
;
145 ret
= bch2_hash_lookup(trans
, &iter
, bch2_xattr_hash_desc
, &hash
,
146 inode_inum(inode
), &search
, 0);
150 k
= bch2_btree_iter_peek_slot(&iter
);
155 xattr
= bkey_s_c_to_xattr(k
);
156 ret
= le16_to_cpu(xattr
.v
->x_val_len
);
161 memcpy(buffer
, xattr_val(xattr
.v
), ret
);
164 bch2_trans_iter_exit(trans
, &iter
);
166 return ret
< 0 && bch2_err_matches(ret
, ENOENT
) ? -ENODATA
: ret
;
169 int bch2_xattr_set(struct btree_trans
*trans
, subvol_inum inum
,
170 struct bch_inode_unpacked
*inode_u
,
171 const struct bch_hash_info
*hash_info
,
172 const char *name
, const void *value
, size_t size
,
175 struct bch_fs
*c
= trans
->c
;
176 struct btree_iter inode_iter
= { NULL
};
179 ret
= bch2_inode_peek(trans
, &inode_iter
, inode_u
, inum
, BTREE_ITER_INTENT
);
183 inode_u
->bi_ctime
= bch2_current_time(c
);
185 ret
= bch2_inode_write(trans
, &inode_iter
, inode_u
);
186 bch2_trans_iter_exit(trans
, &inode_iter
);
192 struct bkey_i_xattr
*xattr
;
193 unsigned namelen
= strlen(name
);
194 unsigned u64s
= BKEY_U64s
+
195 xattr_val_u64s(namelen
, size
);
200 xattr
= bch2_trans_kmalloc(trans
, u64s
* sizeof(u64
));
202 return PTR_ERR(xattr
);
204 bkey_xattr_init(&xattr
->k_i
);
205 xattr
->k
.u64s
= u64s
;
206 xattr
->v
.x_type
= type
;
207 xattr
->v
.x_name_len
= namelen
;
208 xattr
->v
.x_val_len
= cpu_to_le16(size
);
209 memcpy(xattr
->v
.x_name
, name
, namelen
);
210 memcpy(xattr_val(&xattr
->v
), value
, size
);
212 ret
= bch2_hash_set(trans
, bch2_xattr_hash_desc
, hash_info
,
214 (flags
& XATTR_CREATE
? BCH_HASH_SET_MUST_CREATE
: 0)|
215 (flags
& XATTR_REPLACE
? BCH_HASH_SET_MUST_REPLACE
: 0));
217 struct xattr_search_key search
=
218 X_SEARCH(type
, name
, strlen(name
));
220 ret
= bch2_hash_delete(trans
, bch2_xattr_hash_desc
,
221 hash_info
, inum
, &search
);
224 if (bch2_err_matches(ret
, ENOENT
))
225 ret
= flags
& XATTR_REPLACE
? -ENODATA
: 0;
236 static int __bch2_xattr_emit(const char *prefix
,
237 const char *name
, size_t name_len
,
238 struct xattr_buf
*buf
)
240 const size_t prefix_len
= strlen(prefix
);
241 const size_t total_len
= prefix_len
+ name_len
+ 1;
244 if (buf
->used
+ total_len
> buf
->len
)
247 memcpy(buf
->buf
+ buf
->used
, prefix
, prefix_len
);
248 memcpy(buf
->buf
+ buf
->used
+ prefix_len
,
250 buf
->buf
[buf
->used
+ prefix_len
+ name_len
] = '\0';
253 buf
->used
+= total_len
;
257 static int bch2_xattr_emit(struct dentry
*dentry
,
258 const struct bch_xattr
*xattr
,
259 struct xattr_buf
*buf
)
261 const struct xattr_handler
*handler
=
262 bch2_xattr_type_to_handler(xattr
->x_type
);
264 return handler
&& (!handler
->list
|| handler
->list(dentry
))
265 ? __bch2_xattr_emit(handler
->prefix
?: handler
->name
,
266 xattr
->x_name
, xattr
->x_name_len
, buf
)
270 static int bch2_xattr_list_bcachefs(struct bch_fs
*c
,
271 struct bch_inode_unpacked
*inode
,
272 struct xattr_buf
*buf
,
275 const char *prefix
= all
? "bcachefs_effective." : "bcachefs.";
280 for (id
= 0; id
< Inode_opt_nr
; id
++) {
281 v
= bch2_inode_opt_get(inode
, id
);
286 !(inode
->bi_fields_set
& (1 << id
)))
289 ret
= __bch2_xattr_emit(prefix
, bch2_inode_opts
[id
],
290 strlen(bch2_inode_opts
[id
]), buf
);
298 ssize_t
bch2_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
300 struct bch_fs
*c
= dentry
->d_sb
->s_fs_info
;
301 struct bch_inode_info
*inode
= to_bch_ei(dentry
->d_inode
);
302 struct btree_trans
*trans
= bch2_trans_get(c
);
303 struct btree_iter iter
;
305 struct xattr_buf buf
= { .buf
= buffer
, .len
= buffer_size
};
306 u64 offset
= 0, inum
= inode
->ei_inode
.bi_inum
;
310 bch2_trans_begin(trans
);
311 iter
= (struct btree_iter
) { NULL
};
313 ret
= bch2_subvolume_get_snapshot(trans
, inode
->ei_subvol
, &snapshot
);
317 for_each_btree_key_upto_norestart(trans
, iter
, BTREE_ID_xattrs
,
318 SPOS(inum
, offset
, snapshot
),
319 POS(inum
, U64_MAX
), 0, k
, ret
) {
320 if (k
.k
->type
!= KEY_TYPE_xattr
)
323 ret
= bch2_xattr_emit(dentry
, bkey_s_c_to_xattr(k
).v
, &buf
);
328 offset
= iter
.pos
.offset
;
329 bch2_trans_iter_exit(trans
, &iter
);
331 if (bch2_err_matches(ret
, BCH_ERR_transaction_restart
))
334 bch2_trans_put(trans
);
339 ret
= bch2_xattr_list_bcachefs(c
, &inode
->ei_inode
, &buf
, false);
343 ret
= bch2_xattr_list_bcachefs(c
, &inode
->ei_inode
, &buf
, true);
349 return bch2_err_class(ret
);
352 static int bch2_xattr_get_handler(const struct xattr_handler
*handler
,
353 struct dentry
*dentry
, struct inode
*vinode
,
354 const char *name
, void *buffer
, size_t size
)
356 struct bch_inode_info
*inode
= to_bch_ei(vinode
);
357 struct bch_fs
*c
= inode
->v
.i_sb
->s_fs_info
;
358 int ret
= bch2_trans_do(c
, NULL
, NULL
, 0,
359 bch2_xattr_get_trans(trans
, inode
, name
, buffer
, size
, handler
->flags
));
361 return bch2_err_class(ret
);
364 static int bch2_xattr_set_handler(const struct xattr_handler
*handler
,
365 struct mnt_idmap
*idmap
,
366 struct dentry
*dentry
, struct inode
*vinode
,
367 const char *name
, const void *value
,
368 size_t size
, int flags
)
370 struct bch_inode_info
*inode
= to_bch_ei(vinode
);
371 struct bch_fs
*c
= inode
->v
.i_sb
->s_fs_info
;
372 struct bch_hash_info hash
= bch2_hash_info_init(c
, &inode
->ei_inode
);
373 struct bch_inode_unpacked inode_u
;
376 ret
= bch2_trans_run(c
,
377 commit_do(trans
, NULL
, NULL
, 0,
378 bch2_xattr_set(trans
, inode_inum(inode
), &inode_u
,
379 &hash
, name
, value
, size
,
380 handler
->flags
, flags
)) ?:
381 (bch2_inode_update_after_write(trans
, inode
, &inode_u
, ATTR_CTIME
), 0));
383 return bch2_err_class(ret
);
386 static const struct xattr_handler bch_xattr_user_handler
= {
387 .prefix
= XATTR_USER_PREFIX
,
388 .get
= bch2_xattr_get_handler
,
389 .set
= bch2_xattr_set_handler
,
390 .flags
= KEY_TYPE_XATTR_INDEX_USER
,
393 static bool bch2_xattr_trusted_list(struct dentry
*dentry
)
395 return capable(CAP_SYS_ADMIN
);
398 static const struct xattr_handler bch_xattr_trusted_handler
= {
399 .prefix
= XATTR_TRUSTED_PREFIX
,
400 .list
= bch2_xattr_trusted_list
,
401 .get
= bch2_xattr_get_handler
,
402 .set
= bch2_xattr_set_handler
,
403 .flags
= KEY_TYPE_XATTR_INDEX_TRUSTED
,
406 static const struct xattr_handler bch_xattr_security_handler
= {
407 .prefix
= XATTR_SECURITY_PREFIX
,
408 .get
= bch2_xattr_get_handler
,
409 .set
= bch2_xattr_set_handler
,
410 .flags
= KEY_TYPE_XATTR_INDEX_SECURITY
,
413 #ifndef NO_BCACHEFS_FS
415 static int opt_to_inode_opt(int id
)
418 #define x(name, ...) \
419 case Opt_##name: return Inode_opt_##name;
427 static int __bch2_xattr_bcachefs_get(const struct xattr_handler
*handler
,
428 struct dentry
*dentry
, struct inode
*vinode
,
429 const char *name
, void *buffer
, size_t size
,
432 struct bch_inode_info
*inode
= to_bch_ei(vinode
);
433 struct bch_fs
*c
= inode
->v
.i_sb
->s_fs_info
;
434 struct bch_opts opts
=
435 bch2_inode_opts_to_opts(&inode
->ei_inode
);
436 const struct bch_option
*opt
;
437 int id
, inode_opt_id
;
438 struct printbuf out
= PRINTBUF
;
442 id
= bch2_opt_lookup(name
);
443 if (id
< 0 || !bch2_opt_is_inode_opt(id
))
446 inode_opt_id
= opt_to_inode_opt(id
);
447 if (inode_opt_id
< 0)
450 opt
= bch2_opt_table
+ id
;
452 if (!bch2_opt_defined_by_id(&opts
, id
))
456 !(inode
->ei_inode
.bi_fields_set
& (1 << inode_opt_id
)))
459 v
= bch2_opt_get_by_id(&opts
, id
);
460 bch2_opt_to_text(&out
, c
, c
->disk_sb
.sb
, opt
, v
, 0);
464 if (out
.allocation_failure
) {
470 memcpy(buffer
, out
.buf
, out
.pos
);
477 static int bch2_xattr_bcachefs_get(const struct xattr_handler
*handler
,
478 struct dentry
*dentry
, struct inode
*vinode
,
479 const char *name
, void *buffer
, size_t size
)
481 return __bch2_xattr_bcachefs_get(handler
, dentry
, vinode
,
482 name
, buffer
, size
, false);
485 struct inode_opt_set
{
491 static int inode_opt_set_fn(struct btree_trans
*trans
,
492 struct bch_inode_info
*inode
,
493 struct bch_inode_unpacked
*bi
,
496 struct inode_opt_set
*s
= p
;
499 bi
->bi_fields_set
|= 1U << s
->id
;
501 bi
->bi_fields_set
&= ~(1U << s
->id
);
503 bch2_inode_opt_set(bi
, s
->id
, s
->v
);
508 static int bch2_xattr_bcachefs_set(const struct xattr_handler
*handler
,
509 struct mnt_idmap
*idmap
,
510 struct dentry
*dentry
, struct inode
*vinode
,
511 const char *name
, const void *value
,
512 size_t size
, int flags
)
514 struct bch_inode_info
*inode
= to_bch_ei(vinode
);
515 struct bch_fs
*c
= inode
->v
.i_sb
->s_fs_info
;
516 const struct bch_option
*opt
;
518 struct inode_opt_set s
;
519 int opt_id
, inode_opt_id
, ret
;
521 opt_id
= bch2_opt_lookup(name
);
525 opt
= bch2_opt_table
+ opt_id
;
527 inode_opt_id
= opt_to_inode_opt(opt_id
);
528 if (inode_opt_id
< 0)
536 buf
= kmalloc(size
+ 1, GFP_KERNEL
);
539 memcpy(buf
, value
, size
);
542 ret
= bch2_opt_parse(c
, opt
, buf
, &v
, NULL
);
548 ret
= bch2_opt_check_may_set(c
, opt_id
, v
);
555 if (!IS_ROOT(dentry
)) {
556 struct bch_inode_info
*dir
=
557 to_bch_ei(d_inode(dentry
->d_parent
));
559 s
.v
= bch2_inode_opt_get(&dir
->ei_inode
, inode_opt_id
);
567 mutex_lock(&inode
->ei_update_lock
);
568 if (inode_opt_id
== Inode_opt_project
) {
570 * inode fields accessible via the xattr interface are stored
571 * with a +1 bias, so that 0 means unset:
573 ret
= bch2_set_projid(c
, inode
, s
.v
? s
.v
- 1 : 0);
578 ret
= bch2_write_inode(c
, inode
, inode_opt_set_fn
, &s
, 0);
580 mutex_unlock(&inode
->ei_update_lock
);
583 (opt_id
== Opt_background_compression
||
584 opt_id
== Opt_background_target
))
585 bch2_set_rebalance_needs_scan(c
, inode
->ei_inode
.bi_inum
);
587 return bch2_err_class(ret
);
590 static const struct xattr_handler bch_xattr_bcachefs_handler
= {
591 .prefix
= "bcachefs.",
592 .get
= bch2_xattr_bcachefs_get
,
593 .set
= bch2_xattr_bcachefs_set
,
596 static int bch2_xattr_bcachefs_get_effective(
597 const struct xattr_handler
*handler
,
598 struct dentry
*dentry
, struct inode
*vinode
,
599 const char *name
, void *buffer
, size_t size
)
601 return __bch2_xattr_bcachefs_get(handler
, dentry
, vinode
,
602 name
, buffer
, size
, true);
605 static const struct xattr_handler bch_xattr_bcachefs_effective_handler
= {
606 .prefix
= "bcachefs_effective.",
607 .get
= bch2_xattr_bcachefs_get_effective
,
608 .set
= bch2_xattr_bcachefs_set
,
611 #endif /* NO_BCACHEFS_FS */
613 const struct xattr_handler
*bch2_xattr_handlers
[] = {
614 &bch_xattr_user_handler
,
615 #ifdef CONFIG_BCACHEFS_POSIX_ACL
616 &nop_posix_acl_access
,
617 &nop_posix_acl_default
,
619 &bch_xattr_trusted_handler
,
620 &bch_xattr_security_handler
,
621 #ifndef NO_BCACHEFS_FS
622 &bch_xattr_bcachefs_handler
,
623 &bch_xattr_bcachefs_effective_handler
,
628 static const struct xattr_handler
*bch_xattr_handler_map
[] = {
629 [KEY_TYPE_XATTR_INDEX_USER
] = &bch_xattr_user_handler
,
630 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS
] =
631 &nop_posix_acl_access
,
632 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT
] =
633 &nop_posix_acl_default
,
634 [KEY_TYPE_XATTR_INDEX_TRUSTED
] = &bch_xattr_trusted_handler
,
635 [KEY_TYPE_XATTR_INDEX_SECURITY
] = &bch_xattr_security_handler
,
638 static const struct xattr_handler
*bch2_xattr_type_to_handler(unsigned type
)
640 return type
< ARRAY_SIZE(bch_xattr_handler_map
)
641 ? bch_xattr_handler_map
[type
]