1 // SPDX-License-Identifier: GPL-2.0 2 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 17 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned); 18 19 static 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 31 static u64 xattr_hash_key(const struct bch_hash_info *info, const void *key) 32 { 33 return bch2_xattr_hash(info, key); 34 } 35 36 static 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 44 static 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 54 static 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 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, 71 }; 72 73 int bch2_xattr_invalid(const struct bch_fs *c, struct bkey_s_c k, 74 enum bkey_invalid_flags flags, 75 struct printbuf *err) 76 { 77 const struct xattr_handler *handler; 78 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k); 79 80 if (bkey_val_u64s(k.k) < 81 xattr_val_u64s(xattr.v->x_name_len, 82 le16_to_cpu(xattr.v->x_val_len))) { 83 prt_printf(err, "value too small (%zu < %u)", 84 bkey_val_u64s(k.k), 85 xattr_val_u64s(xattr.v->x_name_len, 86 le16_to_cpu(xattr.v->x_val_len))); 87 return -BCH_ERR_invalid_bkey; 88 } 89 90 /* XXX why +4 ? */ 91 if (bkey_val_u64s(k.k) > 92 xattr_val_u64s(xattr.v->x_name_len, 93 le16_to_cpu(xattr.v->x_val_len) + 4)) { 94 prt_printf(err, "value too big (%zu > %u)", 95 bkey_val_u64s(k.k), 96 xattr_val_u64s(xattr.v->x_name_len, 97 le16_to_cpu(xattr.v->x_val_len) + 4)); 98 return -BCH_ERR_invalid_bkey; 99 } 100 101 handler = bch2_xattr_type_to_handler(xattr.v->x_type); 102 if (!handler) { 103 prt_printf(err, "invalid type (%u)", xattr.v->x_type); 104 return -BCH_ERR_invalid_bkey; 105 } 106 107 if (memchr(xattr.v->x_name, '\0', xattr.v->x_name_len)) { 108 prt_printf(err, "xattr name has invalid characters"); 109 return -BCH_ERR_invalid_bkey; 110 } 111 112 return 0; 113 } 114 115 void bch2_xattr_to_text(struct printbuf *out, struct bch_fs *c, 116 struct bkey_s_c k) 117 { 118 const struct xattr_handler *handler; 119 struct bkey_s_c_xattr xattr = bkey_s_c_to_xattr(k); 120 121 handler = bch2_xattr_type_to_handler(xattr.v->x_type); 122 if (handler && handler->prefix) 123 prt_printf(out, "%s", handler->prefix); 124 else if (handler) 125 prt_printf(out, "(type %u)", xattr.v->x_type); 126 else 127 prt_printf(out, "(unknown type %u)", xattr.v->x_type); 128 129 prt_printf(out, "%.*s:%.*s", 130 xattr.v->x_name_len, 131 xattr.v->x_name, 132 le16_to_cpu(xattr.v->x_val_len), 133 (char *) xattr_val(xattr.v)); 134 135 if (xattr.v->x_type == KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS || 136 xattr.v->x_type == KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT) { 137 prt_char(out, ' '); 138 bch2_acl_to_text(out, xattr_val(xattr.v), 139 le16_to_cpu(xattr.v->x_val_len)); 140 } 141 } 142 143 static int bch2_xattr_get_trans(struct btree_trans *trans, struct bch_inode_info *inode, 144 const char *name, void *buffer, size_t size, int type) 145 { 146 struct bch_hash_info hash = bch2_hash_info_init(trans->c, &inode->ei_inode); 147 struct xattr_search_key search = X_SEARCH(type, name, strlen(name)); 148 struct btree_iter iter; 149 struct bkey_s_c_xattr xattr; 150 struct bkey_s_c k; 151 int ret; 152 153 ret = bch2_hash_lookup(trans, &iter, bch2_xattr_hash_desc, &hash, 154 inode_inum(inode), &search, 0); 155 if (ret) 156 goto err1; 157 158 k = bch2_btree_iter_peek_slot(&iter); 159 ret = bkey_err(k); 160 if (ret) 161 goto err2; 162 163 xattr = bkey_s_c_to_xattr(k); 164 ret = le16_to_cpu(xattr.v->x_val_len); 165 if (buffer) { 166 if (ret > size) 167 ret = -ERANGE; 168 else 169 memcpy(buffer, xattr_val(xattr.v), ret); 170 } 171 err2: 172 bch2_trans_iter_exit(trans, &iter); 173 err1: 174 return ret < 0 && bch2_err_matches(ret, ENOENT) ? -ENODATA : ret; 175 } 176 177 int bch2_xattr_set(struct btree_trans *trans, subvol_inum inum, 178 struct bch_inode_unpacked *inode_u, 179 const struct bch_hash_info *hash_info, 180 const char *name, const void *value, size_t size, 181 int type, int flags) 182 { 183 struct bch_fs *c = trans->c; 184 struct btree_iter inode_iter = { NULL }; 185 int ret; 186 187 ret = bch2_inode_peek(trans, &inode_iter, inode_u, inum, BTREE_ITER_INTENT); 188 if (ret) 189 return ret; 190 191 inode_u->bi_ctime = bch2_current_time(c); 192 193 ret = bch2_inode_write(trans, &inode_iter, inode_u); 194 bch2_trans_iter_exit(trans, &inode_iter); 195 196 if (ret) 197 return ret; 198 199 if (value) { 200 struct bkey_i_xattr *xattr; 201 unsigned namelen = strlen(name); 202 unsigned u64s = BKEY_U64s + 203 xattr_val_u64s(namelen, size); 204 205 if (u64s > U8_MAX) 206 return -ERANGE; 207 208 xattr = bch2_trans_kmalloc(trans, u64s * sizeof(u64)); 209 if (IS_ERR(xattr)) 210 return PTR_ERR(xattr); 211 212 bkey_xattr_init(&xattr->k_i); 213 xattr->k.u64s = u64s; 214 xattr->v.x_type = type; 215 xattr->v.x_name_len = namelen; 216 xattr->v.x_val_len = cpu_to_le16(size); 217 memcpy(xattr->v.x_name, name, namelen); 218 memcpy(xattr_val(&xattr->v), value, size); 219 220 ret = bch2_hash_set(trans, bch2_xattr_hash_desc, hash_info, 221 inum, &xattr->k_i, 222 (flags & XATTR_CREATE ? BCH_HASH_SET_MUST_CREATE : 0)| 223 (flags & XATTR_REPLACE ? BCH_HASH_SET_MUST_REPLACE : 0)); 224 } else { 225 struct xattr_search_key search = 226 X_SEARCH(type, name, strlen(name)); 227 228 ret = bch2_hash_delete(trans, bch2_xattr_hash_desc, 229 hash_info, inum, &search); 230 } 231 232 if (bch2_err_matches(ret, ENOENT)) 233 ret = flags & XATTR_REPLACE ? -ENODATA : 0; 234 235 return ret; 236 } 237 238 struct xattr_buf { 239 char *buf; 240 size_t len; 241 size_t used; 242 }; 243 244 static int __bch2_xattr_emit(const char *prefix, 245 const char *name, size_t name_len, 246 struct xattr_buf *buf) 247 { 248 const size_t prefix_len = strlen(prefix); 249 const size_t total_len = prefix_len + name_len + 1; 250 251 if (buf->buf) { 252 if (buf->used + total_len > buf->len) 253 return -ERANGE; 254 255 memcpy(buf->buf + buf->used, prefix, prefix_len); 256 memcpy(buf->buf + buf->used + prefix_len, 257 name, name_len); 258 buf->buf[buf->used + prefix_len + name_len] = '\0'; 259 } 260 261 buf->used += total_len; 262 return 0; 263 } 264 265 static int bch2_xattr_emit(struct dentry *dentry, 266 const struct bch_xattr *xattr, 267 struct xattr_buf *buf) 268 { 269 const struct xattr_handler *handler = 270 bch2_xattr_type_to_handler(xattr->x_type); 271 272 return handler && (!handler->list || handler->list(dentry)) 273 ? __bch2_xattr_emit(handler->prefix ?: handler->name, 274 xattr->x_name, xattr->x_name_len, buf) 275 : 0; 276 } 277 278 static int bch2_xattr_list_bcachefs(struct bch_fs *c, 279 struct bch_inode_unpacked *inode, 280 struct xattr_buf *buf, 281 bool all) 282 { 283 const char *prefix = all ? "bcachefs_effective." : "bcachefs."; 284 unsigned id; 285 int ret = 0; 286 u64 v; 287 288 for (id = 0; id < Inode_opt_nr; id++) { 289 v = bch2_inode_opt_get(inode, id); 290 if (!v) 291 continue; 292 293 if (!all && 294 !(inode->bi_fields_set & (1 << id))) 295 continue; 296 297 ret = __bch2_xattr_emit(prefix, bch2_inode_opts[id], 298 strlen(bch2_inode_opts[id]), buf); 299 if (ret) 300 break; 301 } 302 303 return ret; 304 } 305 306 ssize_t bch2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size) 307 { 308 struct bch_fs *c = dentry->d_sb->s_fs_info; 309 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode); 310 struct btree_trans *trans = bch2_trans_get(c); 311 struct btree_iter iter; 312 struct bkey_s_c k; 313 struct xattr_buf buf = { .buf = buffer, .len = buffer_size }; 314 u64 offset = 0, inum = inode->ei_inode.bi_inum; 315 u32 snapshot; 316 int ret; 317 retry: 318 bch2_trans_begin(trans); 319 iter = (struct btree_iter) { NULL }; 320 321 ret = bch2_subvolume_get_snapshot(trans, inode->ei_subvol, &snapshot); 322 if (ret) 323 goto err; 324 325 for_each_btree_key_upto_norestart(trans, iter, BTREE_ID_xattrs, 326 SPOS(inum, offset, snapshot), 327 POS(inum, U64_MAX), 0, k, ret) { 328 if (k.k->type != KEY_TYPE_xattr) 329 continue; 330 331 ret = bch2_xattr_emit(dentry, bkey_s_c_to_xattr(k).v, &buf); 332 if (ret) 333 break; 334 } 335 336 offset = iter.pos.offset; 337 bch2_trans_iter_exit(trans, &iter); 338 err: 339 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 340 goto retry; 341 342 bch2_trans_put(trans); 343 344 if (ret) 345 goto out; 346 347 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, false); 348 if (ret) 349 goto out; 350 351 ret = bch2_xattr_list_bcachefs(c, &inode->ei_inode, &buf, true); 352 if (ret) 353 goto out; 354 355 return buf.used; 356 out: 357 return bch2_err_class(ret); 358 } 359 360 static int bch2_xattr_get_handler(const struct xattr_handler *handler, 361 struct dentry *dentry, struct inode *vinode, 362 const char *name, void *buffer, size_t size) 363 { 364 struct bch_inode_info *inode = to_bch_ei(vinode); 365 struct bch_fs *c = inode->v.i_sb->s_fs_info; 366 int ret = bch2_trans_do(c, NULL, NULL, 0, 367 bch2_xattr_get_trans(trans, inode, name, buffer, size, handler->flags)); 368 369 return bch2_err_class(ret); 370 } 371 372 static int bch2_xattr_set_handler(const struct xattr_handler *handler, 373 struct mnt_idmap *idmap, 374 struct dentry *dentry, struct inode *vinode, 375 const char *name, const void *value, 376 size_t size, int flags) 377 { 378 struct bch_inode_info *inode = to_bch_ei(vinode); 379 struct bch_fs *c = inode->v.i_sb->s_fs_info; 380 struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode); 381 struct bch_inode_unpacked inode_u; 382 int ret; 383 384 ret = bch2_trans_run(c, 385 commit_do(trans, NULL, NULL, 0, 386 bch2_xattr_set(trans, inode_inum(inode), &inode_u, 387 &hash, name, value, size, 388 handler->flags, flags)) ?: 389 (bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME), 0)); 390 391 return bch2_err_class(ret); 392 } 393 394 static const struct xattr_handler bch_xattr_user_handler = { 395 .prefix = XATTR_USER_PREFIX, 396 .get = bch2_xattr_get_handler, 397 .set = bch2_xattr_set_handler, 398 .flags = KEY_TYPE_XATTR_INDEX_USER, 399 }; 400 401 static bool bch2_xattr_trusted_list(struct dentry *dentry) 402 { 403 return capable(CAP_SYS_ADMIN); 404 } 405 406 static const struct xattr_handler bch_xattr_trusted_handler = { 407 .prefix = XATTR_TRUSTED_PREFIX, 408 .list = bch2_xattr_trusted_list, 409 .get = bch2_xattr_get_handler, 410 .set = bch2_xattr_set_handler, 411 .flags = KEY_TYPE_XATTR_INDEX_TRUSTED, 412 }; 413 414 static const struct xattr_handler bch_xattr_security_handler = { 415 .prefix = XATTR_SECURITY_PREFIX, 416 .get = bch2_xattr_get_handler, 417 .set = bch2_xattr_set_handler, 418 .flags = KEY_TYPE_XATTR_INDEX_SECURITY, 419 }; 420 421 #ifndef NO_BCACHEFS_FS 422 423 static int opt_to_inode_opt(int id) 424 { 425 switch (id) { 426 #define x(name, ...) \ 427 case Opt_##name: return Inode_opt_##name; 428 BCH_INODE_OPTS() 429 #undef x 430 default: 431 return -1; 432 } 433 } 434 435 static int __bch2_xattr_bcachefs_get(const struct xattr_handler *handler, 436 struct dentry *dentry, struct inode *vinode, 437 const char *name, void *buffer, size_t size, 438 bool all) 439 { 440 struct bch_inode_info *inode = to_bch_ei(vinode); 441 struct bch_fs *c = inode->v.i_sb->s_fs_info; 442 struct bch_opts opts = 443 bch2_inode_opts_to_opts(&inode->ei_inode); 444 const struct bch_option *opt; 445 int id, inode_opt_id; 446 struct printbuf out = PRINTBUF; 447 int ret; 448 u64 v; 449 450 id = bch2_opt_lookup(name); 451 if (id < 0 || !bch2_opt_is_inode_opt(id)) 452 return -EINVAL; 453 454 inode_opt_id = opt_to_inode_opt(id); 455 if (inode_opt_id < 0) 456 return -EINVAL; 457 458 opt = bch2_opt_table + id; 459 460 if (!bch2_opt_defined_by_id(&opts, id)) 461 return -ENODATA; 462 463 if (!all && 464 !(inode->ei_inode.bi_fields_set & (1 << inode_opt_id))) 465 return -ENODATA; 466 467 v = bch2_opt_get_by_id(&opts, id); 468 bch2_opt_to_text(&out, c, c->disk_sb.sb, opt, v, 0); 469 470 ret = out.pos; 471 472 if (out.allocation_failure) { 473 ret = -ENOMEM; 474 } else if (buffer) { 475 if (out.pos > size) 476 ret = -ERANGE; 477 else 478 memcpy(buffer, out.buf, out.pos); 479 } 480 481 printbuf_exit(&out); 482 return ret; 483 } 484 485 static int bch2_xattr_bcachefs_get(const struct xattr_handler *handler, 486 struct dentry *dentry, struct inode *vinode, 487 const char *name, void *buffer, size_t size) 488 { 489 return __bch2_xattr_bcachefs_get(handler, dentry, vinode, 490 name, buffer, size, false); 491 } 492 493 struct inode_opt_set { 494 int id; 495 u64 v; 496 bool defined; 497 }; 498 499 static int inode_opt_set_fn(struct btree_trans *trans, 500 struct bch_inode_info *inode, 501 struct bch_inode_unpacked *bi, 502 void *p) 503 { 504 struct inode_opt_set *s = p; 505 506 if (s->defined) 507 bi->bi_fields_set |= 1U << s->id; 508 else 509 bi->bi_fields_set &= ~(1U << s->id); 510 511 bch2_inode_opt_set(bi, s->id, s->v); 512 513 return 0; 514 } 515 516 static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler, 517 struct mnt_idmap *idmap, 518 struct dentry *dentry, struct inode *vinode, 519 const char *name, const void *value, 520 size_t size, int flags) 521 { 522 struct bch_inode_info *inode = to_bch_ei(vinode); 523 struct bch_fs *c = inode->v.i_sb->s_fs_info; 524 const struct bch_option *opt; 525 char *buf; 526 struct inode_opt_set s; 527 int opt_id, inode_opt_id, ret; 528 529 opt_id = bch2_opt_lookup(name); 530 if (opt_id < 0) 531 return -EINVAL; 532 533 opt = bch2_opt_table + opt_id; 534 535 inode_opt_id = opt_to_inode_opt(opt_id); 536 if (inode_opt_id < 0) 537 return -EINVAL; 538 539 s.id = inode_opt_id; 540 541 if (value) { 542 u64 v = 0; 543 544 buf = kmalloc(size + 1, GFP_KERNEL); 545 if (!buf) 546 return -ENOMEM; 547 memcpy(buf, value, size); 548 buf[size] = '\0'; 549 550 ret = bch2_opt_parse(c, opt, buf, &v, NULL); 551 kfree(buf); 552 553 if (ret < 0) 554 return ret; 555 556 ret = bch2_opt_check_may_set(c, opt_id, v); 557 if (ret < 0) 558 return ret; 559 560 s.v = v + 1; 561 s.defined = true; 562 } else { 563 if (!IS_ROOT(dentry)) { 564 struct bch_inode_info *dir = 565 to_bch_ei(d_inode(dentry->d_parent)); 566 567 s.v = bch2_inode_opt_get(&dir->ei_inode, inode_opt_id); 568 } else { 569 s.v = 0; 570 } 571 572 s.defined = false; 573 } 574 575 mutex_lock(&inode->ei_update_lock); 576 if (inode_opt_id == Inode_opt_project) { 577 /* 578 * inode fields accessible via the xattr interface are stored 579 * with a +1 bias, so that 0 means unset: 580 */ 581 ret = bch2_set_projid(c, inode, s.v ? s.v - 1 : 0); 582 if (ret) 583 goto err; 584 } 585 586 ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0); 587 err: 588 mutex_unlock(&inode->ei_update_lock); 589 590 if (value && 591 (opt_id == Opt_background_compression || 592 opt_id == Opt_background_target)) 593 bch2_rebalance_add_work(c, inode->v.i_blocks); 594 595 return bch2_err_class(ret); 596 } 597 598 static const struct xattr_handler bch_xattr_bcachefs_handler = { 599 .prefix = "bcachefs.", 600 .get = bch2_xattr_bcachefs_get, 601 .set = bch2_xattr_bcachefs_set, 602 }; 603 604 static int bch2_xattr_bcachefs_get_effective( 605 const struct xattr_handler *handler, 606 struct dentry *dentry, struct inode *vinode, 607 const char *name, void *buffer, size_t size) 608 { 609 return __bch2_xattr_bcachefs_get(handler, dentry, vinode, 610 name, buffer, size, true); 611 } 612 613 static const struct xattr_handler bch_xattr_bcachefs_effective_handler = { 614 .prefix = "bcachefs_effective.", 615 .get = bch2_xattr_bcachefs_get_effective, 616 .set = bch2_xattr_bcachefs_set, 617 }; 618 619 #endif /* NO_BCACHEFS_FS */ 620 621 const struct xattr_handler *bch2_xattr_handlers[] = { 622 &bch_xattr_user_handler, 623 #ifdef CONFIG_BCACHEFS_POSIX_ACL 624 &nop_posix_acl_access, 625 &nop_posix_acl_default, 626 #endif 627 &bch_xattr_trusted_handler, 628 &bch_xattr_security_handler, 629 #ifndef NO_BCACHEFS_FS 630 &bch_xattr_bcachefs_handler, 631 &bch_xattr_bcachefs_effective_handler, 632 #endif 633 NULL 634 }; 635 636 static const struct xattr_handler *bch_xattr_handler_map[] = { 637 [KEY_TYPE_XATTR_INDEX_USER] = &bch_xattr_user_handler, 638 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_ACCESS] = 639 &nop_posix_acl_access, 640 [KEY_TYPE_XATTR_INDEX_POSIX_ACL_DEFAULT] = 641 &nop_posix_acl_default, 642 [KEY_TYPE_XATTR_INDEX_TRUSTED] = &bch_xattr_trusted_handler, 643 [KEY_TYPE_XATTR_INDEX_SECURITY] = &bch_xattr_security_handler, 644 }; 645 646 static const struct xattr_handler *bch2_xattr_type_to_handler(unsigned type) 647 { 648 return type < ARRAY_SIZE(bch_xattr_handler_map) 649 ? bch_xattr_handler_map[type] 650 : NULL; 651 } 652