639 lines · c
1// SPDX-License-Identifier: GPL-2.02 3#include "bcachefs.h"4#include "acl.h"5#include "bkey_methods.h"6#include "btree_update.h"7#include "extents.h"8#include "fs.h"9#include "rebalance.h"10#include "str_hash.h"11#include "xattr.h"12 13#include <linux/dcache.h>14#include <linux/posix_acl_xattr.h>15#include <linux/xattr.h>16 17static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned);18 19static u64 bch2_xattr_hash(const struct bch_hash_info *info,20 const struct xattr_search_key *key)21{22 struct bch_str_hash_ctx ctx;23 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);27 28 return bch2_str_hash_end(&ctx, info);29}30 31static u64 xattr_hash_key(const struct bch_hash_info *info, const void *key)32{33 return bch2_xattr_hash(info, key);34}35 36static u64 xattr_hash_bkey(const struct bch_hash_info *info, struct bkey_s_c k)37{38 struct bkey_s_c_xattr x = bkey_s_c_to_xattr(k);39 40 return bch2_xattr_hash(info,41 &X_SEARCH(x.v->x_type, x.v->x_name, x.v->x_name_len));42}43 44static bool xattr_cmp_key(struct bkey_s_c _l, const void *_r)45{46 struct bkey_s_c_xattr l = bkey_s_c_to_xattr(_l);47 const struct xattr_search_key *r = _r;48 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);52}53 54static bool xattr_cmp_bkey(struct bkey_s_c _l, struct bkey_s_c _r)55{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);58 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);62}63 64const 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,71};72 73int bch2_xattr_validate(struct bch_fs *c, struct bkey_s_c k,74 enum bch_validate_flags flags)75{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));79 int ret = 0;80 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);85 86 /* XXX why +4 ? */87 val_u64s = xattr_val_u64s(xattr.v->x_name_len,88 le16_to_cpu(xattr.v->x_val_len) + 4);89 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);94 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);98 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");102fsck_err:103 return ret;104}105 106void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c,107 struct bkey_s_c k)108{109 const struct xattr_handler *handler;110 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);111 112 handler = bch2_xattr_type_to_handler(xattr.v->x_type);113 if (handler && handler->prefix)114 prt_printf(out, "%s", handler->prefix);115 else if (handler)116 prt_printf(out, "(type %u)", xattr.v->x_type);117 else118 prt_printf(out, "(unknown type %u)", xattr.v->x_type);119 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);124 125 val_len = min_t(int, val_len, max_name_val_bytes - name_len);126 name_len = min(name_len, max_name_val_bytes);127 128 prt_printf(out, "%.*s:%.*s",129 name_len, xattr.v->x_name,130 val_len, (char *) xattr_val(xattr.v));131 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) {134 prt_char(out, ' ');135 bch2_acl_to_text(out, xattr_val(xattr.v),136 le16_to_cpu(xattr.v->x_val_len));137 }138}139 140static 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)142{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);149 if (ret)150 return ret;151 152 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k);153 ret = le16_to_cpu(xattr.v->x_val_len);154 if (buffer) {155 if (ret > size)156 ret = -ERANGE;157 else158 memcpy(buffer, xattr_val(xattr.v), ret);159 }160 bch2_trans_iter_exit(trans, &iter);161 return ret;162}163 164int 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,168 int type, int flags)169{170 struct bch_fs *c = trans->c;171 struct btree_iter inode_iter = { NULL };172 int ret;173 174 ret = bch2_subvol_is_ro_trans(trans, inum.subvol) ?:175 bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_intent);176 if (ret)177 return ret;178 179 inode_u->bi_ctime = bch2_current_time(c);180 181 ret = bch2_inode_write(trans, &inode_iter, inode_u);182 bch2_trans_iter_exit(trans, &inode_iter);183 184 if (ret)185 return ret;186 187 if (value) {188 struct bkey_i_xattr *xattr;189 unsigned namelen = strlen(name);190 unsigned u64s = BKEY_U64s +191 xattr_val_u64s(namelen, size);192 193 if (u64s > U8_MAX)194 return -ERANGE;195 196 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64));197 if (IS_ERR(xattr))198 return PTR_ERR(xattr);199 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);207 208 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info,209 inum, &xattr->k_i,210 (flags & XATTR_CREATE ? STR_HASH_must_create : 0)|211 (flags & XATTR_REPLACE ? STR_HASH_must_replace : 0));212 } else {213 struct xattr_search_key search =214 X_SEARCH(type, name, strlen(name));215 216 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc,217 hash_info, inum, &search);218 }219 220 if (bch2_err_matches(ret, ENOENT))221 ret = flags & XATTR_REPLACE ? -ENODATA : 0;222 223 return ret;224}225 226struct xattr_buf {227 char *buf;228 size_t len;229 size_t used;230};231 232static int __bch2_xattr_emit(const char *prefix,233 const char *name, size_t name_len,234 struct xattr_buf *buf)235{236 const size_t prefix_len = strlen(prefix);237 const size_t total_len = prefix_len + name_len + 1;238 239 if (buf->buf) {240 if (buf->used + total_len > buf->len)241 return -ERANGE;242 243 memcpy(buf->buf + buf->used, prefix, prefix_len);244 memcpy(buf->buf + buf->used + prefix_len,245 name, name_len);246 buf->buf[buf->used + prefix_len + name_len] = '\0';247 }248 249 buf->used += total_len;250 return 0;251}252 253static inline const char *bch2_xattr_prefix(unsigned type, struct dentry *dentry)254{255 const struct xattr_handler *handler = bch2_xattr_type_to_handler(type);256 257 if (!xattr_handler_can_list(handler, dentry))258 return NULL;259 260 return xattr_prefix(handler);261}262 263static int bch2_xattr_emit(struct dentry *dentry,264 const struct bch_xattr *xattr,265 struct xattr_buf *buf)266{267 const char *prefix;268 269 prefix = bch2_xattr_prefix(xattr->x_type, dentry);270 if (!prefix)271 return 0;272 273 return __bch2_xattr_emit(prefix, xattr->x_name, xattr->x_name_len, buf);274}275 276static int bch2_xattr_list_bcachefs(struct bch_fs *c,277 struct bch_inode_unpacked *inode,278 struct xattr_buf *buf,279 bool all)280{281 const char *prefix = all ? "bcachefs_effective." : "bcachefs.";282 unsigned id;283 int ret = 0;284 u64 v;285 286 for (id = 0; id < Inode_opt_nr; id++) {287 v = bch2_inode_opt_get(inode, id);288 if (!v)289 continue;290 291 if (!all &&292 !(inode->bi_fields_set & (1 << id)))293 continue;294 295 ret = __bch2_xattr_emit(prefix, bch2_inode_opts[id],296 strlen(bch2_inode_opts[id]), buf);297 if (ret)298 break;299 }300 301 return ret;302}303 304ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)305{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;310 311 int ret = bch2_trans_run(c,312 for_each_btree_key_in_subvolume_upto(trans, iter, BTREE_ID_xattrs,313 POS(inum, offset),314 POS(inum, U64_MAX),315 inode->ei_inum.subvol, 0, k, ({316 if (k.k->type != KEY_TYPE_xattr)317 continue;318 319 bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v, &buf);320 }))) ?:321 bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, false) ?:322 bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, true);323 324 return ret ? bch2_err_class(ret) : buf.used;325}326 327static 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)330{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));335 336 if (ret < 0 && bch2_err_matches(ret, ENOENT))337 ret = -ENODATA;338 339 return bch2_err_class(ret);340}341 342static 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)347{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;352 int ret;353 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));360 361 return bch2_err_class(ret);362}363 364static 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,369};370 371static bool bch2_xattr_trusted_list(struct dentry *dentry)372{373 return capable(CAP_SYS_ADMIN);374}375 376static 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,382};383 384static 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,389};390 391#ifndef NO_BCACHEFS_FS392 393static int opt_to_inode_opt(int id)394{395 switch (id) {396#define x(name, ...) \397 case Opt_##name: return Inode_opt_##name;398 BCH_INODE_OPTS()399#undef x400 default:401 return -1;402 }403}404 405static 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,408 bool all)409{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;417 int ret;418 u64 v;419 420 id = bch2_opt_lookup(name);421 if (id < 0 || !bch2_opt_is_inode_opt(id))422 return -EINVAL;423 424 inode_opt_id = opt_to_inode_opt(id);425 if (inode_opt_id < 0)426 return -EINVAL;427 428 opt = bch2_opt_table + id;429 430 if (!bch2_opt_defined_by_id(&opts, id))431 return -ENODATA;432 433 if (!all &&434 !(inode->ei_inode.bi_fields_set & (1 << inode_opt_id)))435 return -ENODATA;436 437 v = bch2_opt_get_by_id(&opts, id);438 bch2_opt_to_text(&out, c, c->disk_sb.sb, opt, v, 0);439 440 ret = out.pos;441 442 if (out.allocation_failure) {443 ret = -ENOMEM;444 } else if (buffer) {445 if (out.pos > size)446 ret = -ERANGE;447 else448 memcpy(buffer, out.buf, out.pos);449 }450 451 printbuf_exit(&out);452 return ret;453}454 455static 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)458{459 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,460 name, buffer, size, false);461}462 463struct inode_opt_set {464 int id;465 u64 v;466 bool defined;467};468 469static int inode_opt_set_fn(struct btree_trans *trans,470 struct bch_inode_info *inode,471 struct bch_inode_unpacked *bi,472 void *p)473{474 struct inode_opt_set *s = p;475 476 if (s->defined)477 bi->bi_fields_set |= 1U << s->id;478 else479 bi->bi_fields_set &= ~(1U << s->id);480 481 bch2_inode_opt_set(bi, s->id, s->v);482 483 return 0;484}485 486static 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)491{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;495 char *buf;496 struct inode_opt_set s;497 int opt_id, inode_opt_id, ret;498 499 opt_id = bch2_opt_lookup(name);500 if (opt_id < 0)501 return -EINVAL;502 503 opt = bch2_opt_table + opt_id;504 505 inode_opt_id = opt_to_inode_opt(opt_id);506 if (inode_opt_id < 0)507 return -EINVAL;508 509 s.id = inode_opt_id;510 511 if (value) {512 u64 v = 0;513 514 buf = kmalloc(size + 1, GFP_KERNEL);515 if (!buf)516 return -ENOMEM;517 memcpy(buf, value, size);518 buf[size] = '\0';519 520 ret = bch2_opt_parse(c, opt, buf, &v, NULL);521 kfree(buf);522 523 if (ret < 0)524 goto err_class_exit;525 526 ret = bch2_opt_check_may_set(c, opt_id, v);527 if (ret < 0)528 goto err_class_exit;529 530 s.v = v + 1;531 s.defined = true;532 } else {533 /*534 * Check if this option was set on the parent - if so, switched535 * back to inheriting from the parent:536 *537 * rename() also has to deal with keeping inherited options up538 * to date - see bch2_reinherit_attrs()539 */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));544 545 s.v = bch2_inode_opt_get(&dir->ei_inode, inode_opt_id);546 } else {547 s.v = 0;548 }549 spin_unlock(&dentry->d_lock);550 551 s.defined = false;552 }553 554 mutex_lock(&inode->ei_update_lock);555 if (inode_opt_id == Inode_opt_project) {556 /*557 * inode fields accessible via the xattr interface are stored558 * with a +1 bias, so that 0 means unset:559 */560 ret = bch2_set_projid(c, inode, s.v ? s.v - 1 : 0);561 if (ret)562 goto err;563 }564 565 ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);566err:567 mutex_unlock(&inode->ei_update_lock);568 569 if (value &&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);574 575err_class_exit:576 return bch2_err_class(ret);577}578 579static const struct xattr_handler bch_xattr_bcachefs_handler = {580 .prefix = "bcachefs.",581 .get = bch2_xattr_bcachefs_get,582 .set = bch2_xattr_bcachefs_set,583};584 585static 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)589{590 return __bch2_xattr_bcachefs_get(handler, dentry, vinode,591 name, buffer, size, true);592}593 594/* Noop - xattrs in the bcachefs_effective namespace are inherited */595static 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)600{601 return 0;602}603 604static 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,608};609 610#endif /* NO_BCACHEFS_FS */611 612const 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_FS617 &bch_xattr_bcachefs_handler,618 &bch_xattr_bcachefs_effective_handler,619#endif620 NULL621};622 623static 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,631};632 633static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type)634{635 return type < ARRAY_SIZE(bch_xattr_handler_map)636 ? bch_xattr_handler_map[type]637 : NULL;638}639