1 // SPDX-License-Identifier: GPL-2.0 2 #ifndef NO_BCACHEFS_FS 3 4 #include "bcachefs.h" 5 #include "acl.h" 6 #include "bkey_buf.h" 7 #include "btree_update.h" 8 #include "buckets.h" 9 #include "chardev.h" 10 #include "dirent.h" 11 #include "errcode.h" 12 #include "extents.h" 13 #include "fs.h" 14 #include "fs-common.h" 15 #include "fs-io.h" 16 #include "fs-ioctl.h" 17 #include "fs-io-buffered.h" 18 #include "fs-io-direct.h" 19 #include "fs-io-pagecache.h" 20 #include "fsck.h" 21 #include "inode.h" 22 #include "io_read.h" 23 #include "journal.h" 24 #include "keylist.h" 25 #include "quota.h" 26 #include "snapshot.h" 27 #include "super.h" 28 #include "xattr.h" 29 30 #include <linux/aio.h> 31 #include <linux/backing-dev.h> 32 #include <linux/exportfs.h> 33 #include <linux/fiemap.h> 34 #include <linux/module.h> 35 #include <linux/pagemap.h> 36 #include <linux/posix_acl.h> 37 #include <linux/random.h> 38 #include <linux/seq_file.h> 39 #include <linux/statfs.h> 40 #include <linux/string.h> 41 #include <linux/xattr.h> 42 43 static struct kmem_cache *bch2_inode_cache; 44 45 static void bch2_vfs_inode_init(struct btree_trans *, subvol_inum, 46 struct bch_inode_info *, 47 struct bch_inode_unpacked *, 48 struct bch_subvolume *); 49 50 void bch2_inode_update_after_write(struct btree_trans *trans, 51 struct bch_inode_info *inode, 52 struct bch_inode_unpacked *bi, 53 unsigned fields) 54 { 55 struct bch_fs *c = trans->c; 56 57 BUG_ON(bi->bi_inum != inode->v.i_ino); 58 59 bch2_assert_pos_locked(trans, BTREE_ID_inodes, 60 POS(0, bi->bi_inum), 61 c->opts.inodes_use_key_cache); 62 63 set_nlink(&inode->v, bch2_inode_nlink_get(bi)); 64 i_uid_write(&inode->v, bi->bi_uid); 65 i_gid_write(&inode->v, bi->bi_gid); 66 inode->v.i_mode = bi->bi_mode; 67 68 if (fields & ATTR_ATIME) 69 inode_set_atime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_atime)); 70 if (fields & ATTR_MTIME) 71 inode_set_mtime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_mtime)); 72 if (fields & ATTR_CTIME) 73 inode_set_ctime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_ctime)); 74 75 inode->ei_inode = *bi; 76 77 bch2_inode_flags_to_vfs(inode); 78 } 79 80 int __must_check bch2_write_inode(struct bch_fs *c, 81 struct bch_inode_info *inode, 82 inode_set_fn set, 83 void *p, unsigned fields) 84 { 85 struct btree_trans *trans = bch2_trans_get(c); 86 struct btree_iter iter = { NULL }; 87 struct bch_inode_unpacked inode_u; 88 int ret; 89 retry: 90 bch2_trans_begin(trans); 91 92 ret = bch2_inode_peek(trans, &iter, &inode_u, inode_inum(inode), 93 BTREE_ITER_intent) ?: 94 (set ? set(trans, inode, &inode_u, p) : 0) ?: 95 bch2_inode_write(trans, &iter, &inode_u) ?: 96 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc); 97 98 /* 99 * the btree node lock protects inode->ei_inode, not ei_update_lock; 100 * this is important for inode updates via bchfs_write_index_update 101 */ 102 if (!ret) 103 bch2_inode_update_after_write(trans, inode, &inode_u, fields); 104 105 bch2_trans_iter_exit(trans, &iter); 106 107 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 108 goto retry; 109 110 bch2_fs_fatal_err_on(bch2_err_matches(ret, ENOENT), c, 111 "%s: inode %u:%llu not found when updating", 112 bch2_err_str(ret), 113 inode_inum(inode).subvol, 114 inode_inum(inode).inum); 115 116 bch2_trans_put(trans); 117 return ret < 0 ? ret : 0; 118 } 119 120 int bch2_fs_quota_transfer(struct bch_fs *c, 121 struct bch_inode_info *inode, 122 struct bch_qid new_qid, 123 unsigned qtypes, 124 enum quota_acct_mode mode) 125 { 126 unsigned i; 127 int ret; 128 129 qtypes &= enabled_qtypes(c); 130 131 for (i = 0; i < QTYP_NR; i++) 132 if (new_qid.q[i] == inode->ei_qid.q[i]) 133 qtypes &= ~(1U << i); 134 135 if (!qtypes) 136 return 0; 137 138 mutex_lock(&inode->ei_quota_lock); 139 140 ret = bch2_quota_transfer(c, qtypes, new_qid, 141 inode->ei_qid, 142 inode->v.i_blocks + 143 inode->ei_quota_reserved, 144 mode); 145 if (!ret) 146 for (i = 0; i < QTYP_NR; i++) 147 if (qtypes & (1 << i)) 148 inode->ei_qid.q[i] = new_qid.q[i]; 149 150 mutex_unlock(&inode->ei_quota_lock); 151 152 return ret; 153 } 154 155 static int bch2_iget5_test(struct inode *vinode, void *p) 156 { 157 struct bch_inode_info *inode = to_bch_ei(vinode); 158 subvol_inum *inum = p; 159 160 return inode->ei_subvol == inum->subvol && 161 inode->ei_inode.bi_inum == inum->inum; 162 } 163 164 static int bch2_iget5_set(struct inode *vinode, void *p) 165 { 166 struct bch_inode_info *inode = to_bch_ei(vinode); 167 subvol_inum *inum = p; 168 169 inode->v.i_ino = inum->inum; 170 inode->ei_subvol = inum->subvol; 171 inode->ei_inode.bi_inum = inum->inum; 172 return 0; 173 } 174 175 static unsigned bch2_inode_hash(subvol_inum inum) 176 { 177 return jhash_3words(inum.subvol, inum.inum >> 32, inum.inum, JHASH_INITVAL); 178 } 179 180 static struct bch_inode_info *bch2_inode_insert(struct bch_fs *c, struct bch_inode_info *inode) 181 { 182 subvol_inum inum = inode_inum(inode); 183 struct bch_inode_info *old = to_bch_ei(inode_insert5(&inode->v, 184 bch2_inode_hash(inum), 185 bch2_iget5_test, 186 bch2_iget5_set, 187 &inum)); 188 BUG_ON(!old); 189 190 if (unlikely(old != inode)) { 191 discard_new_inode(&inode->v); 192 inode = old; 193 } else { 194 mutex_lock(&c->vfs_inodes_lock); 195 list_add(&inode->ei_vfs_inode_list, &c->vfs_inodes_list); 196 mutex_unlock(&c->vfs_inodes_lock); 197 /* 198 * we really don't want insert_inode_locked2() to be setting 199 * I_NEW... 200 */ 201 unlock_new_inode(&inode->v); 202 } 203 204 return inode; 205 } 206 207 #define memalloc_flags_do(_flags, _do) \ 208 ({ \ 209 unsigned _saved_flags = memalloc_flags_save(_flags); \ 210 typeof(_do) _ret = _do; \ 211 memalloc_noreclaim_restore(_saved_flags); \ 212 _ret; \ 213 }) 214 215 static struct inode *bch2_alloc_inode(struct super_block *sb) 216 { 217 BUG(); 218 } 219 220 static struct bch_inode_info *__bch2_new_inode(struct bch_fs *c) 221 { 222 struct bch_inode_info *inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS); 223 if (!inode) 224 return NULL; 225 226 inode_init_once(&inode->v); 227 mutex_init(&inode->ei_update_lock); 228 two_state_lock_init(&inode->ei_pagecache_lock); 229 INIT_LIST_HEAD(&inode->ei_vfs_inode_list); 230 inode->ei_flags = 0; 231 mutex_init(&inode->ei_quota_lock); 232 memset(&inode->ei_devs_need_flush, 0, sizeof(inode->ei_devs_need_flush)); 233 inode->v.i_state = 0; 234 235 if (unlikely(inode_init_always(c->vfs_sb, &inode->v))) { 236 kmem_cache_free(bch2_inode_cache, inode); 237 return NULL; 238 } 239 240 return inode; 241 } 242 243 /* 244 * Allocate a new inode, dropping/retaking btree locks if necessary: 245 */ 246 static struct bch_inode_info *bch2_new_inode(struct btree_trans *trans) 247 { 248 struct bch_inode_info *inode = 249 memalloc_flags_do(PF_MEMALLOC_NORECLAIM|PF_MEMALLOC_NOWARN, 250 __bch2_new_inode(trans->c)); 251 252 if (unlikely(!inode)) { 253 int ret = drop_locks_do(trans, (inode = __bch2_new_inode(trans->c)) ? 0 : -ENOMEM); 254 if (ret && inode) { 255 __destroy_inode(&inode->v); 256 kmem_cache_free(bch2_inode_cache, inode); 257 } 258 if (ret) 259 return ERR_PTR(ret); 260 } 261 262 return inode; 263 } 264 265 struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum) 266 { 267 struct bch_inode_info *inode = 268 to_bch_ei(ilookup5_nowait(c->vfs_sb, 269 bch2_inode_hash(inum), 270 bch2_iget5_test, 271 &inum)); 272 if (inode) 273 return &inode->v; 274 275 struct btree_trans *trans = bch2_trans_get(c); 276 277 struct bch_inode_unpacked inode_u; 278 struct bch_subvolume subvol; 279 int ret = lockrestart_do(trans, 280 bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?: 281 bch2_inode_find_by_inum_trans(trans, inum, &inode_u)) ?: 282 PTR_ERR_OR_ZERO(inode = bch2_new_inode(trans)); 283 if (!ret) { 284 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol); 285 inode = bch2_inode_insert(c, inode); 286 } 287 bch2_trans_put(trans); 288 289 return ret ? ERR_PTR(ret) : &inode->v; 290 } 291 292 struct bch_inode_info * 293 __bch2_create(struct mnt_idmap *idmap, 294 struct bch_inode_info *dir, struct dentry *dentry, 295 umode_t mode, dev_t rdev, subvol_inum snapshot_src, 296 unsigned flags) 297 { 298 struct bch_fs *c = dir->v.i_sb->s_fs_info; 299 struct btree_trans *trans; 300 struct bch_inode_unpacked dir_u; 301 struct bch_inode_info *inode; 302 struct bch_inode_unpacked inode_u; 303 struct posix_acl *default_acl = NULL, *acl = NULL; 304 subvol_inum inum; 305 struct bch_subvolume subvol; 306 u64 journal_seq = 0; 307 int ret; 308 309 /* 310 * preallocate acls + vfs inode before btree transaction, so that 311 * nothing can fail after the transaction succeeds: 312 */ 313 #ifdef CONFIG_BCACHEFS_POSIX_ACL 314 ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl); 315 if (ret) 316 return ERR_PTR(ret); 317 #endif 318 inode = __bch2_new_inode(c); 319 if (unlikely(!inode)) { 320 inode = ERR_PTR(-ENOMEM); 321 goto err; 322 } 323 324 bch2_inode_init_early(c, &inode_u); 325 326 if (!(flags & BCH_CREATE_TMPFILE)) 327 mutex_lock(&dir->ei_update_lock); 328 329 trans = bch2_trans_get(c); 330 retry: 331 bch2_trans_begin(trans); 332 333 ret = bch2_subvol_is_ro_trans(trans, dir->ei_subvol) ?: 334 bch2_create_trans(trans, 335 inode_inum(dir), &dir_u, &inode_u, 336 !(flags & BCH_CREATE_TMPFILE) 337 ? &dentry->d_name : NULL, 338 from_kuid(i_user_ns(&dir->v), current_fsuid()), 339 from_kgid(i_user_ns(&dir->v), current_fsgid()), 340 mode, rdev, 341 default_acl, acl, snapshot_src, flags) ?: 342 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1, 343 KEY_TYPE_QUOTA_PREALLOC); 344 if (unlikely(ret)) 345 goto err_before_quota; 346 347 inum.subvol = inode_u.bi_subvol ?: dir->ei_subvol; 348 inum.inum = inode_u.bi_inum; 349 350 ret = bch2_subvolume_get(trans, inum.subvol, true, 351 BTREE_ITER_with_updates, &subvol) ?: 352 bch2_trans_commit(trans, NULL, &journal_seq, 0); 353 if (unlikely(ret)) { 354 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1, 355 KEY_TYPE_QUOTA_WARN); 356 err_before_quota: 357 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 358 goto retry; 359 goto err_trans; 360 } 361 362 if (!(flags & BCH_CREATE_TMPFILE)) { 363 bch2_inode_update_after_write(trans, dir, &dir_u, 364 ATTR_MTIME|ATTR_CTIME); 365 mutex_unlock(&dir->ei_update_lock); 366 } 367 368 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol); 369 370 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl); 371 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl); 372 373 /* 374 * we must insert the new inode into the inode cache before calling 375 * bch2_trans_exit() and dropping locks, else we could race with another 376 * thread pulling the inode in and modifying it: 377 */ 378 inode = bch2_inode_insert(c, inode); 379 bch2_trans_put(trans); 380 err: 381 posix_acl_release(default_acl); 382 posix_acl_release(acl); 383 return inode; 384 err_trans: 385 if (!(flags & BCH_CREATE_TMPFILE)) 386 mutex_unlock(&dir->ei_update_lock); 387 388 bch2_trans_put(trans); 389 make_bad_inode(&inode->v); 390 iput(&inode->v); 391 inode = ERR_PTR(ret); 392 goto err; 393 } 394 395 /* methods */ 396 397 static struct bch_inode_info *bch2_lookup_trans(struct btree_trans *trans, 398 subvol_inum dir, struct bch_hash_info *dir_hash_info, 399 const struct qstr *name) 400 { 401 struct bch_fs *c = trans->c; 402 struct btree_iter dirent_iter = {}; 403 subvol_inum inum = {}; 404 struct printbuf buf = PRINTBUF; 405 406 struct bkey_s_c k = bch2_hash_lookup(trans, &dirent_iter, bch2_dirent_hash_desc, 407 dir_hash_info, dir, name, 0); 408 int ret = bkey_err(k); 409 if (ret) 410 return ERR_PTR(ret); 411 412 ret = bch2_dirent_read_target(trans, dir, bkey_s_c_to_dirent(k), &inum); 413 if (ret > 0) 414 ret = -ENOENT; 415 if (ret) 416 goto err; 417 418 struct bch_inode_info *inode = 419 to_bch_ei(ilookup5_nowait(c->vfs_sb, 420 bch2_inode_hash(inum), 421 bch2_iget5_test, 422 &inum)); 423 if (inode) 424 goto out; 425 426 struct bch_subvolume subvol; 427 struct bch_inode_unpacked inode_u; 428 ret = bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?: 429 bch2_inode_find_by_inum_nowarn_trans(trans, inum, &inode_u) ?: 430 PTR_ERR_OR_ZERO(inode = bch2_new_inode(trans)); 431 432 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), 433 c, "dirent to missing inode:\n %s", 434 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)); 435 if (ret) 436 goto err; 437 438 /* regular files may have hardlinks: */ 439 if (bch2_fs_inconsistent_on(bch2_inode_should_have_bp(&inode_u) && 440 !bkey_eq(k.k->p, POS(inode_u.bi_dir, inode_u.bi_dir_offset)), 441 c, 442 "dirent points to inode that does not point back:\n %s", 443 (bch2_bkey_val_to_text(&buf, c, k), 444 prt_printf(&buf, "\n "), 445 bch2_inode_unpacked_to_text(&buf, &inode_u), 446 buf.buf))) { 447 ret = -ENOENT; 448 goto err; 449 } 450 451 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol); 452 inode = bch2_inode_insert(c, inode); 453 out: 454 bch2_trans_iter_exit(trans, &dirent_iter); 455 printbuf_exit(&buf); 456 return inode; 457 err: 458 inode = ERR_PTR(ret); 459 goto out; 460 } 461 462 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry, 463 unsigned int flags) 464 { 465 struct bch_fs *c = vdir->i_sb->s_fs_info; 466 struct bch_inode_info *dir = to_bch_ei(vdir); 467 struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode); 468 469 struct bch_inode_info *inode; 470 bch2_trans_do(c, NULL, NULL, 0, 471 PTR_ERR_OR_ZERO(inode = bch2_lookup_trans(trans, inode_inum(dir), 472 &hash, &dentry->d_name))); 473 if (IS_ERR(inode)) 474 inode = NULL; 475 476 return d_splice_alias(&inode->v, dentry); 477 } 478 479 static int bch2_mknod(struct mnt_idmap *idmap, 480 struct inode *vdir, struct dentry *dentry, 481 umode_t mode, dev_t rdev) 482 { 483 struct bch_inode_info *inode = 484 __bch2_create(idmap, to_bch_ei(vdir), dentry, mode, rdev, 485 (subvol_inum) { 0 }, 0); 486 487 if (IS_ERR(inode)) 488 return bch2_err_class(PTR_ERR(inode)); 489 490 d_instantiate(dentry, &inode->v); 491 return 0; 492 } 493 494 static int bch2_create(struct mnt_idmap *idmap, 495 struct inode *vdir, struct dentry *dentry, 496 umode_t mode, bool excl) 497 { 498 return bch2_mknod(idmap, vdir, dentry, mode|S_IFREG, 0); 499 } 500 501 static int __bch2_link(struct bch_fs *c, 502 struct bch_inode_info *inode, 503 struct bch_inode_info *dir, 504 struct dentry *dentry) 505 { 506 struct btree_trans *trans = bch2_trans_get(c); 507 struct bch_inode_unpacked dir_u, inode_u; 508 int ret; 509 510 mutex_lock(&inode->ei_update_lock); 511 512 ret = commit_do(trans, NULL, NULL, 0, 513 bch2_link_trans(trans, 514 inode_inum(dir), &dir_u, 515 inode_inum(inode), &inode_u, 516 &dentry->d_name)); 517 518 if (likely(!ret)) { 519 bch2_inode_update_after_write(trans, dir, &dir_u, 520 ATTR_MTIME|ATTR_CTIME); 521 bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME); 522 } 523 524 bch2_trans_put(trans); 525 mutex_unlock(&inode->ei_update_lock); 526 return ret; 527 } 528 529 static int bch2_link(struct dentry *old_dentry, struct inode *vdir, 530 struct dentry *dentry) 531 { 532 struct bch_fs *c = vdir->i_sb->s_fs_info; 533 struct bch_inode_info *dir = to_bch_ei(vdir); 534 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode); 535 int ret; 536 537 lockdep_assert_held(&inode->v.i_rwsem); 538 539 ret = bch2_subvol_is_ro(c, dir->ei_subvol) ?: 540 bch2_subvol_is_ro(c, inode->ei_subvol) ?: 541 __bch2_link(c, inode, dir, dentry); 542 if (unlikely(ret)) 543 return bch2_err_class(ret); 544 545 ihold(&inode->v); 546 d_instantiate(dentry, &inode->v); 547 return 0; 548 } 549 550 int __bch2_unlink(struct inode *vdir, struct dentry *dentry, 551 bool deleting_snapshot) 552 { 553 struct bch_fs *c = vdir->i_sb->s_fs_info; 554 struct bch_inode_info *dir = to_bch_ei(vdir); 555 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode); 556 struct bch_inode_unpacked dir_u, inode_u; 557 struct btree_trans *trans = bch2_trans_get(c); 558 int ret; 559 560 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode); 561 562 ret = commit_do(trans, NULL, NULL, 563 BCH_TRANS_COMMIT_no_enospc, 564 bch2_unlink_trans(trans, 565 inode_inum(dir), &dir_u, 566 &inode_u, &dentry->d_name, 567 deleting_snapshot)); 568 if (unlikely(ret)) 569 goto err; 570 571 bch2_inode_update_after_write(trans, dir, &dir_u, 572 ATTR_MTIME|ATTR_CTIME); 573 bch2_inode_update_after_write(trans, inode, &inode_u, 574 ATTR_MTIME); 575 576 if (inode_u.bi_subvol) { 577 /* 578 * Subvolume deletion is asynchronous, but we still want to tell 579 * the VFS that it's been deleted here: 580 */ 581 set_nlink(&inode->v, 0); 582 } 583 err: 584 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode); 585 bch2_trans_put(trans); 586 587 return ret; 588 } 589 590 static int bch2_unlink(struct inode *vdir, struct dentry *dentry) 591 { 592 struct bch_inode_info *dir= to_bch_ei(vdir); 593 struct bch_fs *c = dir->v.i_sb->s_fs_info; 594 595 int ret = bch2_subvol_is_ro(c, dir->ei_subvol) ?: 596 __bch2_unlink(vdir, dentry, false); 597 return bch2_err_class(ret); 598 } 599 600 static int bch2_symlink(struct mnt_idmap *idmap, 601 struct inode *vdir, struct dentry *dentry, 602 const char *symname) 603 { 604 struct bch_fs *c = vdir->i_sb->s_fs_info; 605 struct bch_inode_info *dir = to_bch_ei(vdir), *inode; 606 int ret; 607 608 inode = __bch2_create(idmap, dir, dentry, S_IFLNK|S_IRWXUGO, 0, 609 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE); 610 if (IS_ERR(inode)) 611 return bch2_err_class(PTR_ERR(inode)); 612 613 inode_lock(&inode->v); 614 ret = page_symlink(&inode->v, symname, strlen(symname) + 1); 615 inode_unlock(&inode->v); 616 617 if (unlikely(ret)) 618 goto err; 619 620 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX); 621 if (unlikely(ret)) 622 goto err; 623 624 ret = __bch2_link(c, inode, dir, dentry); 625 if (unlikely(ret)) 626 goto err; 627 628 d_instantiate(dentry, &inode->v); 629 return 0; 630 err: 631 iput(&inode->v); 632 return bch2_err_class(ret); 633 } 634 635 static int bch2_mkdir(struct mnt_idmap *idmap, 636 struct inode *vdir, struct dentry *dentry, umode_t mode) 637 { 638 return bch2_mknod(idmap, vdir, dentry, mode|S_IFDIR, 0); 639 } 640 641 static int bch2_rename2(struct mnt_idmap *idmap, 642 struct inode *src_vdir, struct dentry *src_dentry, 643 struct inode *dst_vdir, struct dentry *dst_dentry, 644 unsigned flags) 645 { 646 struct bch_fs *c = src_vdir->i_sb->s_fs_info; 647 struct bch_inode_info *src_dir = to_bch_ei(src_vdir); 648 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir); 649 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode); 650 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode); 651 struct bch_inode_unpacked dst_dir_u, src_dir_u; 652 struct bch_inode_unpacked src_inode_u, dst_inode_u; 653 struct btree_trans *trans; 654 enum bch_rename_mode mode = flags & RENAME_EXCHANGE 655 ? BCH_RENAME_EXCHANGE 656 : dst_dentry->d_inode 657 ? BCH_RENAME_OVERWRITE : BCH_RENAME; 658 int ret; 659 660 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE)) 661 return -EINVAL; 662 663 if (mode == BCH_RENAME_OVERWRITE) { 664 ret = filemap_write_and_wait_range(src_inode->v.i_mapping, 665 0, LLONG_MAX); 666 if (ret) 667 return ret; 668 } 669 670 trans = bch2_trans_get(c); 671 672 bch2_lock_inodes(INODE_UPDATE_LOCK, 673 src_dir, 674 dst_dir, 675 src_inode, 676 dst_inode); 677 678 ret = bch2_subvol_is_ro_trans(trans, src_dir->ei_subvol) ?: 679 bch2_subvol_is_ro_trans(trans, dst_dir->ei_subvol); 680 if (ret) 681 goto err; 682 683 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) { 684 ret = bch2_fs_quota_transfer(c, src_inode, 685 dst_dir->ei_qid, 686 1 << QTYP_PRJ, 687 KEY_TYPE_QUOTA_PREALLOC); 688 if (ret) 689 goto err; 690 } 691 692 if (mode == BCH_RENAME_EXCHANGE && 693 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) { 694 ret = bch2_fs_quota_transfer(c, dst_inode, 695 src_dir->ei_qid, 696 1 << QTYP_PRJ, 697 KEY_TYPE_QUOTA_PREALLOC); 698 if (ret) 699 goto err; 700 } 701 702 ret = commit_do(trans, NULL, NULL, 0, 703 bch2_rename_trans(trans, 704 inode_inum(src_dir), &src_dir_u, 705 inode_inum(dst_dir), &dst_dir_u, 706 &src_inode_u, 707 &dst_inode_u, 708 &src_dentry->d_name, 709 &dst_dentry->d_name, 710 mode)); 711 if (unlikely(ret)) 712 goto err; 713 714 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum); 715 BUG_ON(dst_inode && 716 dst_inode->v.i_ino != dst_inode_u.bi_inum); 717 718 bch2_inode_update_after_write(trans, src_dir, &src_dir_u, 719 ATTR_MTIME|ATTR_CTIME); 720 721 if (src_dir != dst_dir) 722 bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u, 723 ATTR_MTIME|ATTR_CTIME); 724 725 bch2_inode_update_after_write(trans, src_inode, &src_inode_u, 726 ATTR_CTIME); 727 728 if (dst_inode) 729 bch2_inode_update_after_write(trans, dst_inode, &dst_inode_u, 730 ATTR_CTIME); 731 err: 732 bch2_trans_put(trans); 733 734 bch2_fs_quota_transfer(c, src_inode, 735 bch_qid(&src_inode->ei_inode), 736 1 << QTYP_PRJ, 737 KEY_TYPE_QUOTA_NOCHECK); 738 if (dst_inode) 739 bch2_fs_quota_transfer(c, dst_inode, 740 bch_qid(&dst_inode->ei_inode), 741 1 << QTYP_PRJ, 742 KEY_TYPE_QUOTA_NOCHECK); 743 744 bch2_unlock_inodes(INODE_UPDATE_LOCK, 745 src_dir, 746 dst_dir, 747 src_inode, 748 dst_inode); 749 750 return bch2_err_class(ret); 751 } 752 753 static void bch2_setattr_copy(struct mnt_idmap *idmap, 754 struct bch_inode_info *inode, 755 struct bch_inode_unpacked *bi, 756 struct iattr *attr) 757 { 758 struct bch_fs *c = inode->v.i_sb->s_fs_info; 759 unsigned int ia_valid = attr->ia_valid; 760 761 if (ia_valid & ATTR_UID) 762 bi->bi_uid = from_kuid(i_user_ns(&inode->v), attr->ia_uid); 763 if (ia_valid & ATTR_GID) 764 bi->bi_gid = from_kgid(i_user_ns(&inode->v), attr->ia_gid); 765 766 if (ia_valid & ATTR_SIZE) 767 bi->bi_size = attr->ia_size; 768 769 if (ia_valid & ATTR_ATIME) 770 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime); 771 if (ia_valid & ATTR_MTIME) 772 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime); 773 if (ia_valid & ATTR_CTIME) 774 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime); 775 776 if (ia_valid & ATTR_MODE) { 777 umode_t mode = attr->ia_mode; 778 kgid_t gid = ia_valid & ATTR_GID 779 ? attr->ia_gid 780 : inode->v.i_gid; 781 782 if (!in_group_p(gid) && 783 !capable_wrt_inode_uidgid(idmap, &inode->v, CAP_FSETID)) 784 mode &= ~S_ISGID; 785 bi->bi_mode = mode; 786 } 787 } 788 789 int bch2_setattr_nonsize(struct mnt_idmap *idmap, 790 struct bch_inode_info *inode, 791 struct iattr *attr) 792 { 793 struct bch_fs *c = inode->v.i_sb->s_fs_info; 794 struct bch_qid qid; 795 struct btree_trans *trans; 796 struct btree_iter inode_iter = { NULL }; 797 struct bch_inode_unpacked inode_u; 798 struct posix_acl *acl = NULL; 799 int ret; 800 801 mutex_lock(&inode->ei_update_lock); 802 803 qid = inode->ei_qid; 804 805 if (attr->ia_valid & ATTR_UID) 806 qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), attr->ia_uid); 807 808 if (attr->ia_valid & ATTR_GID) 809 qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), attr->ia_gid); 810 811 ret = bch2_fs_quota_transfer(c, inode, qid, ~0, 812 KEY_TYPE_QUOTA_PREALLOC); 813 if (ret) 814 goto err; 815 816 trans = bch2_trans_get(c); 817 retry: 818 bch2_trans_begin(trans); 819 kfree(acl); 820 acl = NULL; 821 822 ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inode_inum(inode), 823 BTREE_ITER_intent); 824 if (ret) 825 goto btree_err; 826 827 bch2_setattr_copy(idmap, inode, &inode_u, attr); 828 829 if (attr->ia_valid & ATTR_MODE) { 830 ret = bch2_acl_chmod(trans, inode_inum(inode), &inode_u, 831 inode_u.bi_mode, &acl); 832 if (ret) 833 goto btree_err; 834 } 835 836 ret = bch2_inode_write(trans, &inode_iter, &inode_u) ?: 837 bch2_trans_commit(trans, NULL, NULL, 838 BCH_TRANS_COMMIT_no_enospc); 839 btree_err: 840 bch2_trans_iter_exit(trans, &inode_iter); 841 842 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 843 goto retry; 844 if (unlikely(ret)) 845 goto err_trans; 846 847 bch2_inode_update_after_write(trans, inode, &inode_u, attr->ia_valid); 848 849 if (acl) 850 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl); 851 err_trans: 852 bch2_trans_put(trans); 853 err: 854 mutex_unlock(&inode->ei_update_lock); 855 856 return bch2_err_class(ret); 857 } 858 859 static int bch2_getattr(struct mnt_idmap *idmap, 860 const struct path *path, struct kstat *stat, 861 u32 request_mask, unsigned query_flags) 862 { 863 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry)); 864 struct bch_fs *c = inode->v.i_sb->s_fs_info; 865 866 stat->dev = inode->v.i_sb->s_dev; 867 stat->ino = inode->v.i_ino; 868 stat->mode = inode->v.i_mode; 869 stat->nlink = inode->v.i_nlink; 870 stat->uid = inode->v.i_uid; 871 stat->gid = inode->v.i_gid; 872 stat->rdev = inode->v.i_rdev; 873 stat->size = i_size_read(&inode->v); 874 stat->atime = inode_get_atime(&inode->v); 875 stat->mtime = inode_get_mtime(&inode->v); 876 stat->ctime = inode_get_ctime(&inode->v); 877 stat->blksize = block_bytes(c); 878 stat->blocks = inode->v.i_blocks; 879 880 stat->subvol = inode->ei_subvol; 881 stat->result_mask |= STATX_SUBVOL; 882 883 if (request_mask & STATX_BTIME) { 884 stat->result_mask |= STATX_BTIME; 885 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime); 886 } 887 888 if (inode->ei_inode.bi_flags & BCH_INODE_immutable) 889 stat->attributes |= STATX_ATTR_IMMUTABLE; 890 stat->attributes_mask |= STATX_ATTR_IMMUTABLE; 891 892 if (inode->ei_inode.bi_flags & BCH_INODE_append) 893 stat->attributes |= STATX_ATTR_APPEND; 894 stat->attributes_mask |= STATX_ATTR_APPEND; 895 896 if (inode->ei_inode.bi_flags & BCH_INODE_nodump) 897 stat->attributes |= STATX_ATTR_NODUMP; 898 stat->attributes_mask |= STATX_ATTR_NODUMP; 899 900 return 0; 901 } 902 903 static int bch2_setattr(struct mnt_idmap *idmap, 904 struct dentry *dentry, struct iattr *iattr) 905 { 906 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode); 907 struct bch_fs *c = inode->v.i_sb->s_fs_info; 908 int ret; 909 910 lockdep_assert_held(&inode->v.i_rwsem); 911 912 ret = bch2_subvol_is_ro(c, inode->ei_subvol) ?: 913 setattr_prepare(idmap, dentry, iattr); 914 if (ret) 915 return ret; 916 917 return iattr->ia_valid & ATTR_SIZE 918 ? bchfs_truncate(idmap, inode, iattr) 919 : bch2_setattr_nonsize(idmap, inode, iattr); 920 } 921 922 static int bch2_tmpfile(struct mnt_idmap *idmap, 923 struct inode *vdir, struct file *file, umode_t mode) 924 { 925 struct bch_inode_info *inode = 926 __bch2_create(idmap, to_bch_ei(vdir), 927 file->f_path.dentry, mode, 0, 928 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE); 929 930 if (IS_ERR(inode)) 931 return bch2_err_class(PTR_ERR(inode)); 932 933 d_mark_tmpfile(file, &inode->v); 934 d_instantiate(file->f_path.dentry, &inode->v); 935 return finish_open_simple(file, 0); 936 } 937 938 static int bch2_fill_extent(struct bch_fs *c, 939 struct fiemap_extent_info *info, 940 struct bkey_s_c k, unsigned flags) 941 { 942 if (bkey_extent_is_direct_data(k.k)) { 943 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); 944 const union bch_extent_entry *entry; 945 struct extent_ptr_decoded p; 946 int ret; 947 948 if (k.k->type == KEY_TYPE_reflink_v) 949 flags |= FIEMAP_EXTENT_SHARED; 950 951 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) { 952 int flags2 = 0; 953 u64 offset = p.ptr.offset; 954 955 if (p.ptr.unwritten) 956 flags2 |= FIEMAP_EXTENT_UNWRITTEN; 957 958 if (p.crc.compression_type) 959 flags2 |= FIEMAP_EXTENT_ENCODED; 960 else 961 offset += p.crc.offset; 962 963 if ((offset & (block_sectors(c) - 1)) || 964 (k.k->size & (block_sectors(c) - 1))) 965 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED; 966 967 ret = fiemap_fill_next_extent(info, 968 bkey_start_offset(k.k) << 9, 969 offset << 9, 970 k.k->size << 9, flags|flags2); 971 if (ret) 972 return ret; 973 } 974 975 return 0; 976 } else if (bkey_extent_is_inline_data(k.k)) { 977 return fiemap_fill_next_extent(info, 978 bkey_start_offset(k.k) << 9, 979 0, k.k->size << 9, 980 flags| 981 FIEMAP_EXTENT_DATA_INLINE); 982 } else if (k.k->type == KEY_TYPE_reservation) { 983 return fiemap_fill_next_extent(info, 984 bkey_start_offset(k.k) << 9, 985 0, k.k->size << 9, 986 flags| 987 FIEMAP_EXTENT_DELALLOC| 988 FIEMAP_EXTENT_UNWRITTEN); 989 } else { 990 BUG(); 991 } 992 } 993 994 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info, 995 u64 start, u64 len) 996 { 997 struct bch_fs *c = vinode->i_sb->s_fs_info; 998 struct bch_inode_info *ei = to_bch_ei(vinode); 999 struct btree_trans *trans; 1000 struct btree_iter iter; 1001 struct bkey_s_c k; 1002 struct bkey_buf cur, prev; 1003 unsigned offset_into_extent, sectors; 1004 bool have_extent = false; 1005 u32 snapshot; 1006 int ret = 0; 1007 1008 ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC); 1009 if (ret) 1010 return ret; 1011 1012 struct bpos end = POS(ei->v.i_ino, (start + len) >> 9); 1013 if (start + len < start) 1014 return -EINVAL; 1015 1016 start >>= 9; 1017 1018 bch2_bkey_buf_init(&cur); 1019 bch2_bkey_buf_init(&prev); 1020 trans = bch2_trans_get(c); 1021 retry: 1022 bch2_trans_begin(trans); 1023 1024 ret = bch2_subvolume_get_snapshot(trans, ei->ei_subvol, &snapshot); 1025 if (ret) 1026 goto err; 1027 1028 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, 1029 SPOS(ei->v.i_ino, start, snapshot), 0); 1030 1031 while (!(ret = btree_trans_too_many_iters(trans)) && 1032 (k = bch2_btree_iter_peek_upto(&iter, end)).k && 1033 !(ret = bkey_err(k))) { 1034 enum btree_id data_btree = BTREE_ID_extents; 1035 1036 if (!bkey_extent_is_data(k.k) && 1037 k.k->type != KEY_TYPE_reservation) { 1038 bch2_btree_iter_advance(&iter); 1039 continue; 1040 } 1041 1042 offset_into_extent = iter.pos.offset - 1043 bkey_start_offset(k.k); 1044 sectors = k.k->size - offset_into_extent; 1045 1046 bch2_bkey_buf_reassemble(&cur, c, k); 1047 1048 ret = bch2_read_indirect_extent(trans, &data_btree, 1049 &offset_into_extent, &cur); 1050 if (ret) 1051 break; 1052 1053 k = bkey_i_to_s_c(cur.k); 1054 bch2_bkey_buf_realloc(&prev, c, k.k->u64s); 1055 1056 sectors = min(sectors, k.k->size - offset_into_extent); 1057 1058 bch2_cut_front(POS(k.k->p.inode, 1059 bkey_start_offset(k.k) + 1060 offset_into_extent), 1061 cur.k); 1062 bch2_key_resize(&cur.k->k, sectors); 1063 cur.k->k.p = iter.pos; 1064 cur.k->k.p.offset += cur.k->k.size; 1065 1066 if (have_extent) { 1067 bch2_trans_unlock(trans); 1068 ret = bch2_fill_extent(c, info, 1069 bkey_i_to_s_c(prev.k), 0); 1070 if (ret) 1071 break; 1072 } 1073 1074 bkey_copy(prev.k, cur.k); 1075 have_extent = true; 1076 1077 bch2_btree_iter_set_pos(&iter, 1078 POS(iter.pos.inode, iter.pos.offset + sectors)); 1079 1080 ret = bch2_trans_relock(trans); 1081 if (ret) 1082 break; 1083 } 1084 start = iter.pos.offset; 1085 bch2_trans_iter_exit(trans, &iter); 1086 err: 1087 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 1088 goto retry; 1089 1090 if (!ret && have_extent) { 1091 bch2_trans_unlock(trans); 1092 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k), 1093 FIEMAP_EXTENT_LAST); 1094 } 1095 1096 bch2_trans_put(trans); 1097 bch2_bkey_buf_exit(&cur, c); 1098 bch2_bkey_buf_exit(&prev, c); 1099 return ret < 0 ? ret : 0; 1100 } 1101 1102 static const struct vm_operations_struct bch_vm_ops = { 1103 .fault = bch2_page_fault, 1104 .map_pages = filemap_map_pages, 1105 .page_mkwrite = bch2_page_mkwrite, 1106 }; 1107 1108 static int bch2_mmap(struct file *file, struct vm_area_struct *vma) 1109 { 1110 file_accessed(file); 1111 1112 vma->vm_ops = &bch_vm_ops; 1113 return 0; 1114 } 1115 1116 /* Directories: */ 1117 1118 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence) 1119 { 1120 return generic_file_llseek_size(file, offset, whence, 1121 S64_MAX, S64_MAX); 1122 } 1123 1124 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx) 1125 { 1126 struct bch_inode_info *inode = file_bch_inode(file); 1127 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1128 1129 if (!dir_emit_dots(file, ctx)) 1130 return 0; 1131 1132 int ret = bch2_readdir(c, inode_inum(inode), ctx); 1133 1134 bch_err_fn(c, ret); 1135 return bch2_err_class(ret); 1136 } 1137 1138 static int bch2_open(struct inode *vinode, struct file *file) 1139 { 1140 if (file->f_flags & (O_WRONLY|O_RDWR)) { 1141 struct bch_inode_info *inode = to_bch_ei(vinode); 1142 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1143 1144 int ret = bch2_subvol_is_ro(c, inode->ei_subvol); 1145 if (ret) 1146 return ret; 1147 } 1148 1149 file->f_mode |= FMODE_CAN_ODIRECT; 1150 1151 return generic_file_open(vinode, file); 1152 } 1153 1154 static const struct file_operations bch_file_operations = { 1155 .open = bch2_open, 1156 .llseek = bch2_llseek, 1157 .read_iter = bch2_read_iter, 1158 .write_iter = bch2_write_iter, 1159 .mmap = bch2_mmap, 1160 .fsync = bch2_fsync, 1161 .splice_read = filemap_splice_read, 1162 .splice_write = iter_file_splice_write, 1163 .fallocate = bch2_fallocate_dispatch, 1164 .unlocked_ioctl = bch2_fs_file_ioctl, 1165 #ifdef CONFIG_COMPAT 1166 .compat_ioctl = bch2_compat_fs_ioctl, 1167 #endif 1168 .remap_file_range = bch2_remap_file_range, 1169 }; 1170 1171 static const struct inode_operations bch_file_inode_operations = { 1172 .getattr = bch2_getattr, 1173 .setattr = bch2_setattr, 1174 .fiemap = bch2_fiemap, 1175 .listxattr = bch2_xattr_list, 1176 #ifdef CONFIG_BCACHEFS_POSIX_ACL 1177 .get_acl = bch2_get_acl, 1178 .set_acl = bch2_set_acl, 1179 #endif 1180 }; 1181 1182 static const struct inode_operations bch_dir_inode_operations = { 1183 .lookup = bch2_lookup, 1184 .create = bch2_create, 1185 .link = bch2_link, 1186 .unlink = bch2_unlink, 1187 .symlink = bch2_symlink, 1188 .mkdir = bch2_mkdir, 1189 .rmdir = bch2_unlink, 1190 .mknod = bch2_mknod, 1191 .rename = bch2_rename2, 1192 .getattr = bch2_getattr, 1193 .setattr = bch2_setattr, 1194 .tmpfile = bch2_tmpfile, 1195 .listxattr = bch2_xattr_list, 1196 #ifdef CONFIG_BCACHEFS_POSIX_ACL 1197 .get_acl = bch2_get_acl, 1198 .set_acl = bch2_set_acl, 1199 #endif 1200 }; 1201 1202 static const struct file_operations bch_dir_file_operations = { 1203 .llseek = bch2_dir_llseek, 1204 .read = generic_read_dir, 1205 .iterate_shared = bch2_vfs_readdir, 1206 .fsync = bch2_fsync, 1207 .unlocked_ioctl = bch2_fs_file_ioctl, 1208 #ifdef CONFIG_COMPAT 1209 .compat_ioctl = bch2_compat_fs_ioctl, 1210 #endif 1211 }; 1212 1213 static const struct inode_operations bch_symlink_inode_operations = { 1214 .get_link = page_get_link, 1215 .getattr = bch2_getattr, 1216 .setattr = bch2_setattr, 1217 .listxattr = bch2_xattr_list, 1218 #ifdef CONFIG_BCACHEFS_POSIX_ACL 1219 .get_acl = bch2_get_acl, 1220 .set_acl = bch2_set_acl, 1221 #endif 1222 }; 1223 1224 static const struct inode_operations bch_special_inode_operations = { 1225 .getattr = bch2_getattr, 1226 .setattr = bch2_setattr, 1227 .listxattr = bch2_xattr_list, 1228 #ifdef CONFIG_BCACHEFS_POSIX_ACL 1229 .get_acl = bch2_get_acl, 1230 .set_acl = bch2_set_acl, 1231 #endif 1232 }; 1233 1234 static const struct address_space_operations bch_address_space_operations = { 1235 .read_folio = bch2_read_folio, 1236 .writepages = bch2_writepages, 1237 .readahead = bch2_readahead, 1238 .dirty_folio = filemap_dirty_folio, 1239 .write_begin = bch2_write_begin, 1240 .write_end = bch2_write_end, 1241 .invalidate_folio = bch2_invalidate_folio, 1242 .release_folio = bch2_release_folio, 1243 #ifdef CONFIG_MIGRATION 1244 .migrate_folio = filemap_migrate_folio, 1245 #endif 1246 .error_remove_folio = generic_error_remove_folio, 1247 }; 1248 1249 struct bcachefs_fid { 1250 u64 inum; 1251 u32 subvol; 1252 u32 gen; 1253 } __packed; 1254 1255 struct bcachefs_fid_with_parent { 1256 struct bcachefs_fid fid; 1257 struct bcachefs_fid dir; 1258 } __packed; 1259 1260 static int bcachefs_fid_valid(int fh_len, int fh_type) 1261 { 1262 switch (fh_type) { 1263 case FILEID_BCACHEFS_WITHOUT_PARENT: 1264 return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32); 1265 case FILEID_BCACHEFS_WITH_PARENT: 1266 return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32); 1267 default: 1268 return false; 1269 } 1270 } 1271 1272 static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode) 1273 { 1274 return (struct bcachefs_fid) { 1275 .inum = inode->ei_inode.bi_inum, 1276 .subvol = inode->ei_subvol, 1277 .gen = inode->ei_inode.bi_generation, 1278 }; 1279 } 1280 1281 static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len, 1282 struct inode *vdir) 1283 { 1284 struct bch_inode_info *inode = to_bch_ei(vinode); 1285 struct bch_inode_info *dir = to_bch_ei(vdir); 1286 int min_len; 1287 1288 if (!S_ISDIR(inode->v.i_mode) && dir) { 1289 struct bcachefs_fid_with_parent *fid = (void *) fh; 1290 1291 min_len = sizeof(*fid) / sizeof(u32); 1292 if (*len < min_len) { 1293 *len = min_len; 1294 return FILEID_INVALID; 1295 } 1296 1297 fid->fid = bch2_inode_to_fid(inode); 1298 fid->dir = bch2_inode_to_fid(dir); 1299 1300 *len = min_len; 1301 return FILEID_BCACHEFS_WITH_PARENT; 1302 } else { 1303 struct bcachefs_fid *fid = (void *) fh; 1304 1305 min_len = sizeof(*fid) / sizeof(u32); 1306 if (*len < min_len) { 1307 *len = min_len; 1308 return FILEID_INVALID; 1309 } 1310 *fid = bch2_inode_to_fid(inode); 1311 1312 *len = min_len; 1313 return FILEID_BCACHEFS_WITHOUT_PARENT; 1314 } 1315 } 1316 1317 static struct inode *bch2_nfs_get_inode(struct super_block *sb, 1318 struct bcachefs_fid fid) 1319 { 1320 struct bch_fs *c = sb->s_fs_info; 1321 struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) { 1322 .subvol = fid.subvol, 1323 .inum = fid.inum, 1324 }); 1325 if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) { 1326 iput(vinode); 1327 vinode = ERR_PTR(-ESTALE); 1328 } 1329 return vinode; 1330 } 1331 1332 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid, 1333 int fh_len, int fh_type) 1334 { 1335 struct bcachefs_fid *fid = (void *) _fid; 1336 1337 if (!bcachefs_fid_valid(fh_len, fh_type)) 1338 return NULL; 1339 1340 return d_obtain_alias(bch2_nfs_get_inode(sb, *fid)); 1341 } 1342 1343 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid, 1344 int fh_len, int fh_type) 1345 { 1346 struct bcachefs_fid_with_parent *fid = (void *) _fid; 1347 1348 if (!bcachefs_fid_valid(fh_len, fh_type) || 1349 fh_type != FILEID_BCACHEFS_WITH_PARENT) 1350 return NULL; 1351 1352 return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir)); 1353 } 1354 1355 static struct dentry *bch2_get_parent(struct dentry *child) 1356 { 1357 struct bch_inode_info *inode = to_bch_ei(child->d_inode); 1358 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1359 subvol_inum parent_inum = { 1360 .subvol = inode->ei_inode.bi_parent_subvol ?: 1361 inode->ei_subvol, 1362 .inum = inode->ei_inode.bi_dir, 1363 }; 1364 1365 return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum)); 1366 } 1367 1368 static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child) 1369 { 1370 struct bch_inode_info *inode = to_bch_ei(child->d_inode); 1371 struct bch_inode_info *dir = to_bch_ei(parent->d_inode); 1372 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1373 struct btree_trans *trans; 1374 struct btree_iter iter1; 1375 struct btree_iter iter2; 1376 struct bkey_s_c k; 1377 struct bkey_s_c_dirent d; 1378 struct bch_inode_unpacked inode_u; 1379 subvol_inum target; 1380 u32 snapshot; 1381 struct qstr dirent_name; 1382 unsigned name_len = 0; 1383 int ret; 1384 1385 if (!S_ISDIR(dir->v.i_mode)) 1386 return -EINVAL; 1387 1388 trans = bch2_trans_get(c); 1389 1390 bch2_trans_iter_init(trans, &iter1, BTREE_ID_dirents, 1391 POS(dir->ei_inode.bi_inum, 0), 0); 1392 bch2_trans_iter_init(trans, &iter2, BTREE_ID_dirents, 1393 POS(dir->ei_inode.bi_inum, 0), 0); 1394 retry: 1395 bch2_trans_begin(trans); 1396 1397 ret = bch2_subvolume_get_snapshot(trans, dir->ei_subvol, &snapshot); 1398 if (ret) 1399 goto err; 1400 1401 bch2_btree_iter_set_snapshot(&iter1, snapshot); 1402 bch2_btree_iter_set_snapshot(&iter2, snapshot); 1403 1404 ret = bch2_inode_find_by_inum_trans(trans, inode_inum(inode), &inode_u); 1405 if (ret) 1406 goto err; 1407 1408 if (inode_u.bi_dir == dir->ei_inode.bi_inum) { 1409 bch2_btree_iter_set_pos(&iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset)); 1410 1411 k = bch2_btree_iter_peek_slot(&iter1); 1412 ret = bkey_err(k); 1413 if (ret) 1414 goto err; 1415 1416 if (k.k->type != KEY_TYPE_dirent) { 1417 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode; 1418 goto err; 1419 } 1420 1421 d = bkey_s_c_to_dirent(k); 1422 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target); 1423 if (ret > 0) 1424 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode; 1425 if (ret) 1426 goto err; 1427 1428 if (target.subvol == inode->ei_subvol && 1429 target.inum == inode->ei_inode.bi_inum) 1430 goto found; 1431 } else { 1432 /* 1433 * File with multiple hardlinks and our backref is to the wrong 1434 * directory - linear search: 1435 */ 1436 for_each_btree_key_continue_norestart(iter2, 0, k, ret) { 1437 if (k.k->p.inode > dir->ei_inode.bi_inum) 1438 break; 1439 1440 if (k.k->type != KEY_TYPE_dirent) 1441 continue; 1442 1443 d = bkey_s_c_to_dirent(k); 1444 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target); 1445 if (ret < 0) 1446 break; 1447 if (ret) 1448 continue; 1449 1450 if (target.subvol == inode->ei_subvol && 1451 target.inum == inode->ei_inode.bi_inum) 1452 goto found; 1453 } 1454 } 1455 1456 ret = -ENOENT; 1457 goto err; 1458 found: 1459 dirent_name = bch2_dirent_get_name(d); 1460 1461 name_len = min_t(unsigned, dirent_name.len, NAME_MAX); 1462 memcpy(name, dirent_name.name, name_len); 1463 name[name_len] = '\0'; 1464 err: 1465 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 1466 goto retry; 1467 1468 bch2_trans_iter_exit(trans, &iter1); 1469 bch2_trans_iter_exit(trans, &iter2); 1470 bch2_trans_put(trans); 1471 1472 return ret; 1473 } 1474 1475 static const struct export_operations bch_export_ops = { 1476 .encode_fh = bch2_encode_fh, 1477 .fh_to_dentry = bch2_fh_to_dentry, 1478 .fh_to_parent = bch2_fh_to_parent, 1479 .get_parent = bch2_get_parent, 1480 .get_name = bch2_get_name, 1481 }; 1482 1483 static void bch2_vfs_inode_init(struct btree_trans *trans, subvol_inum inum, 1484 struct bch_inode_info *inode, 1485 struct bch_inode_unpacked *bi, 1486 struct bch_subvolume *subvol) 1487 { 1488 bch2_iget5_set(&inode->v, &inum); 1489 bch2_inode_update_after_write(trans, inode, bi, ~0); 1490 1491 if (BCH_SUBVOLUME_SNAP(subvol)) 1492 set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags); 1493 else 1494 clear_bit(EI_INODE_SNAPSHOT, &inode->ei_flags); 1495 1496 inode->v.i_blocks = bi->bi_sectors; 1497 inode->v.i_ino = bi->bi_inum; 1498 inode->v.i_rdev = bi->bi_dev; 1499 inode->v.i_generation = bi->bi_generation; 1500 inode->v.i_size = bi->bi_size; 1501 1502 inode->ei_flags = 0; 1503 inode->ei_quota_reserved = 0; 1504 inode->ei_qid = bch_qid(bi); 1505 inode->ei_subvol = inum.subvol; 1506 1507 inode->v.i_mapping->a_ops = &bch_address_space_operations; 1508 1509 switch (inode->v.i_mode & S_IFMT) { 1510 case S_IFREG: 1511 inode->v.i_op = &bch_file_inode_operations; 1512 inode->v.i_fop = &bch_file_operations; 1513 break; 1514 case S_IFDIR: 1515 inode->v.i_op = &bch_dir_inode_operations; 1516 inode->v.i_fop = &bch_dir_file_operations; 1517 break; 1518 case S_IFLNK: 1519 inode_nohighmem(&inode->v); 1520 inode->v.i_op = &bch_symlink_inode_operations; 1521 break; 1522 default: 1523 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev); 1524 inode->v.i_op = &bch_special_inode_operations; 1525 break; 1526 } 1527 1528 mapping_set_large_folios(inode->v.i_mapping); 1529 } 1530 1531 static void bch2_free_inode(struct inode *vinode) 1532 { 1533 kmem_cache_free(bch2_inode_cache, to_bch_ei(vinode)); 1534 } 1535 1536 static int inode_update_times_fn(struct btree_trans *trans, 1537 struct bch_inode_info *inode, 1538 struct bch_inode_unpacked *bi, 1539 void *p) 1540 { 1541 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1542 1543 bi->bi_atime = timespec_to_bch2_time(c, inode_get_atime(&inode->v)); 1544 bi->bi_mtime = timespec_to_bch2_time(c, inode_get_mtime(&inode->v)); 1545 bi->bi_ctime = timespec_to_bch2_time(c, inode_get_ctime(&inode->v)); 1546 1547 return 0; 1548 } 1549 1550 static int bch2_vfs_write_inode(struct inode *vinode, 1551 struct writeback_control *wbc) 1552 { 1553 struct bch_fs *c = vinode->i_sb->s_fs_info; 1554 struct bch_inode_info *inode = to_bch_ei(vinode); 1555 int ret; 1556 1557 mutex_lock(&inode->ei_update_lock); 1558 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL, 1559 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME); 1560 mutex_unlock(&inode->ei_update_lock); 1561 1562 return bch2_err_class(ret); 1563 } 1564 1565 static void bch2_evict_inode(struct inode *vinode) 1566 { 1567 struct bch_fs *c = vinode->i_sb->s_fs_info; 1568 struct bch_inode_info *inode = to_bch_ei(vinode); 1569 1570 truncate_inode_pages_final(&inode->v.i_data); 1571 1572 clear_inode(&inode->v); 1573 1574 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved); 1575 1576 if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) { 1577 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks), 1578 KEY_TYPE_QUOTA_WARN); 1579 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1, 1580 KEY_TYPE_QUOTA_WARN); 1581 bch2_inode_rm(c, inode_inum(inode)); 1582 } 1583 1584 mutex_lock(&c->vfs_inodes_lock); 1585 list_del_init(&inode->ei_vfs_inode_list); 1586 mutex_unlock(&c->vfs_inodes_lock); 1587 } 1588 1589 void bch2_evict_subvolume_inodes(struct bch_fs *c, snapshot_id_list *s) 1590 { 1591 struct bch_inode_info *inode; 1592 DARRAY(struct bch_inode_info *) grabbed; 1593 bool clean_pass = false, this_pass_clean; 1594 1595 /* 1596 * Initially, we scan for inodes without I_DONTCACHE, then mark them to 1597 * be pruned with d_mark_dontcache(). 1598 * 1599 * Once we've had a clean pass where we didn't find any inodes without 1600 * I_DONTCACHE, we wait for them to be freed: 1601 */ 1602 1603 darray_init(&grabbed); 1604 darray_make_room(&grabbed, 1024); 1605 again: 1606 cond_resched(); 1607 this_pass_clean = true; 1608 1609 mutex_lock(&c->vfs_inodes_lock); 1610 list_for_each_entry(inode, &c->vfs_inodes_list, ei_vfs_inode_list) { 1611 if (!snapshot_list_has_id(s, inode->ei_subvol)) 1612 continue; 1613 1614 if (!(inode->v.i_state & I_DONTCACHE) && 1615 !(inode->v.i_state & I_FREEING) && 1616 igrab(&inode->v)) { 1617 this_pass_clean = false; 1618 1619 if (darray_push_gfp(&grabbed, inode, GFP_ATOMIC|__GFP_NOWARN)) { 1620 iput(&inode->v); 1621 break; 1622 } 1623 } else if (clean_pass && this_pass_clean) { 1624 wait_queue_head_t *wq = bit_waitqueue(&inode->v.i_state, __I_NEW); 1625 DEFINE_WAIT_BIT(wait, &inode->v.i_state, __I_NEW); 1626 1627 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); 1628 mutex_unlock(&c->vfs_inodes_lock); 1629 1630 schedule(); 1631 finish_wait(wq, &wait.wq_entry); 1632 goto again; 1633 } 1634 } 1635 mutex_unlock(&c->vfs_inodes_lock); 1636 1637 darray_for_each(grabbed, i) { 1638 inode = *i; 1639 d_mark_dontcache(&inode->v); 1640 d_prune_aliases(&inode->v); 1641 iput(&inode->v); 1642 } 1643 grabbed.nr = 0; 1644 1645 if (!clean_pass || !this_pass_clean) { 1646 clean_pass = this_pass_clean; 1647 goto again; 1648 } 1649 1650 darray_exit(&grabbed); 1651 } 1652 1653 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf) 1654 { 1655 struct super_block *sb = dentry->d_sb; 1656 struct bch_fs *c = sb->s_fs_info; 1657 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c); 1658 unsigned shift = sb->s_blocksize_bits - 9; 1659 /* 1660 * this assumes inodes take up 64 bytes, which is a decent average 1661 * number: 1662 */ 1663 u64 avail_inodes = ((usage.capacity - usage.used) << 3); 1664 1665 buf->f_type = BCACHEFS_STATFS_MAGIC; 1666 buf->f_bsize = sb->s_blocksize; 1667 buf->f_blocks = usage.capacity >> shift; 1668 buf->f_bfree = usage.free >> shift; 1669 buf->f_bavail = avail_factor(usage.free) >> shift; 1670 1671 buf->f_files = usage.nr_inodes + avail_inodes; 1672 buf->f_ffree = avail_inodes; 1673 1674 buf->f_fsid = uuid_to_fsid(c->sb.user_uuid.b); 1675 buf->f_namelen = BCH_NAME_MAX; 1676 1677 return 0; 1678 } 1679 1680 static int bch2_sync_fs(struct super_block *sb, int wait) 1681 { 1682 struct bch_fs *c = sb->s_fs_info; 1683 int ret; 1684 1685 if (c->opts.journal_flush_disabled) 1686 return 0; 1687 1688 if (!wait) { 1689 bch2_journal_flush_async(&c->journal, NULL); 1690 return 0; 1691 } 1692 1693 ret = bch2_journal_flush(&c->journal); 1694 return bch2_err_class(ret); 1695 } 1696 1697 static struct bch_fs *bch2_path_to_fs(const char *path) 1698 { 1699 struct bch_fs *c; 1700 dev_t dev; 1701 int ret; 1702 1703 ret = lookup_bdev(path, &dev); 1704 if (ret) 1705 return ERR_PTR(ret); 1706 1707 c = bch2_dev_to_fs(dev); 1708 if (c) 1709 closure_put(&c->cl); 1710 return c ?: ERR_PTR(-ENOENT); 1711 } 1712 1713 static int bch2_remount(struct super_block *sb, int *flags, char *data) 1714 { 1715 struct bch_fs *c = sb->s_fs_info; 1716 struct bch_opts opts = bch2_opts_empty(); 1717 int ret; 1718 1719 ret = bch2_parse_mount_opts(c, &opts, data); 1720 if (ret) 1721 goto err; 1722 1723 opt_set(opts, read_only, (*flags & SB_RDONLY) != 0); 1724 1725 if (opts.read_only != c->opts.read_only) { 1726 down_write(&c->state_lock); 1727 1728 if (opts.read_only) { 1729 bch2_fs_read_only(c); 1730 1731 sb->s_flags |= SB_RDONLY; 1732 } else { 1733 ret = bch2_fs_read_write(c); 1734 if (ret) { 1735 bch_err(c, "error going rw: %i", ret); 1736 up_write(&c->state_lock); 1737 ret = -EINVAL; 1738 goto err; 1739 } 1740 1741 sb->s_flags &= ~SB_RDONLY; 1742 } 1743 1744 c->opts.read_only = opts.read_only; 1745 1746 up_write(&c->state_lock); 1747 } 1748 1749 if (opt_defined(opts, errors)) 1750 c->opts.errors = opts.errors; 1751 err: 1752 return bch2_err_class(ret); 1753 } 1754 1755 static int bch2_show_devname(struct seq_file *seq, struct dentry *root) 1756 { 1757 struct bch_fs *c = root->d_sb->s_fs_info; 1758 bool first = true; 1759 1760 for_each_online_member(c, ca) { 1761 if (!first) 1762 seq_putc(seq, ':'); 1763 first = false; 1764 seq_puts(seq, ca->disk_sb.sb_name); 1765 } 1766 1767 return 0; 1768 } 1769 1770 static int bch2_show_options(struct seq_file *seq, struct dentry *root) 1771 { 1772 struct bch_fs *c = root->d_sb->s_fs_info; 1773 enum bch_opt_id i; 1774 struct printbuf buf = PRINTBUF; 1775 int ret = 0; 1776 1777 for (i = 0; i < bch2_opts_nr; i++) { 1778 const struct bch_option *opt = &bch2_opt_table[i]; 1779 u64 v = bch2_opt_get_by_id(&c->opts, i); 1780 1781 if (!(opt->flags & OPT_MOUNT)) 1782 continue; 1783 1784 if (v == bch2_opt_get_by_id(&bch2_opts_default, i)) 1785 continue; 1786 1787 printbuf_reset(&buf); 1788 bch2_opt_to_text(&buf, c, c->disk_sb.sb, opt, v, 1789 OPT_SHOW_MOUNT_STYLE); 1790 seq_putc(seq, ','); 1791 seq_puts(seq, buf.buf); 1792 } 1793 1794 if (buf.allocation_failure) 1795 ret = -ENOMEM; 1796 printbuf_exit(&buf); 1797 return ret; 1798 } 1799 1800 static void bch2_put_super(struct super_block *sb) 1801 { 1802 struct bch_fs *c = sb->s_fs_info; 1803 1804 __bch2_fs_stop(c); 1805 } 1806 1807 /* 1808 * bcachefs doesn't currently integrate intwrite freeze protection but the 1809 * internal write references serve the same purpose. Therefore reuse the 1810 * read-only transition code to perform the quiesce. The caveat is that we don't 1811 * currently have the ability to block tasks that want a write reference while 1812 * the superblock is frozen. This is fine for now, but we should either add 1813 * blocking support or find a way to integrate sb_start_intwrite() and friends. 1814 */ 1815 static int bch2_freeze(struct super_block *sb) 1816 { 1817 struct bch_fs *c = sb->s_fs_info; 1818 1819 down_write(&c->state_lock); 1820 bch2_fs_read_only(c); 1821 up_write(&c->state_lock); 1822 return 0; 1823 } 1824 1825 static int bch2_unfreeze(struct super_block *sb) 1826 { 1827 struct bch_fs *c = sb->s_fs_info; 1828 int ret; 1829 1830 if (test_bit(BCH_FS_emergency_ro, &c->flags)) 1831 return 0; 1832 1833 down_write(&c->state_lock); 1834 ret = bch2_fs_read_write(c); 1835 up_write(&c->state_lock); 1836 return ret; 1837 } 1838 1839 static const struct super_operations bch_super_operations = { 1840 .alloc_inode = bch2_alloc_inode, 1841 .free_inode = bch2_free_inode, 1842 .write_inode = bch2_vfs_write_inode, 1843 .evict_inode = bch2_evict_inode, 1844 .sync_fs = bch2_sync_fs, 1845 .statfs = bch2_statfs, 1846 .show_devname = bch2_show_devname, 1847 .show_options = bch2_show_options, 1848 .remount_fs = bch2_remount, 1849 .put_super = bch2_put_super, 1850 .freeze_fs = bch2_freeze, 1851 .unfreeze_fs = bch2_unfreeze, 1852 }; 1853 1854 static int bch2_set_super(struct super_block *s, void *data) 1855 { 1856 s->s_fs_info = data; 1857 return 0; 1858 } 1859 1860 static int bch2_noset_super(struct super_block *s, void *data) 1861 { 1862 return -EBUSY; 1863 } 1864 1865 typedef DARRAY(struct bch_fs *) darray_fs; 1866 1867 static int bch2_test_super(struct super_block *s, void *data) 1868 { 1869 struct bch_fs *c = s->s_fs_info; 1870 darray_fs *d = data; 1871 1872 if (!c) 1873 return false; 1874 1875 darray_for_each(*d, i) 1876 if (c != *i) 1877 return false; 1878 return true; 1879 } 1880 1881 static struct dentry *bch2_mount(struct file_system_type *fs_type, 1882 int flags, const char *dev_name, void *data) 1883 { 1884 struct bch_fs *c; 1885 struct super_block *sb; 1886 struct inode *vinode; 1887 struct bch_opts opts = bch2_opts_empty(); 1888 int ret; 1889 1890 opt_set(opts, read_only, (flags & SB_RDONLY) != 0); 1891 1892 ret = bch2_parse_mount_opts(NULL, &opts, data); 1893 if (ret) { 1894 ret = bch2_err_class(ret); 1895 return ERR_PTR(ret); 1896 } 1897 1898 if (!dev_name || strlen(dev_name) == 0) 1899 return ERR_PTR(-EINVAL); 1900 1901 darray_str devs; 1902 ret = bch2_split_devs(dev_name, &devs); 1903 if (ret) 1904 return ERR_PTR(ret); 1905 1906 darray_fs devs_to_fs = {}; 1907 darray_for_each(devs, i) { 1908 ret = darray_push(&devs_to_fs, bch2_path_to_fs(*i)); 1909 if (ret) { 1910 sb = ERR_PTR(ret); 1911 goto got_sb; 1912 } 1913 } 1914 1915 sb = sget(fs_type, bch2_test_super, bch2_noset_super, flags|SB_NOSEC, &devs_to_fs); 1916 if (!IS_ERR(sb)) 1917 goto got_sb; 1918 1919 c = bch2_fs_open(devs.data, devs.nr, opts); 1920 if (IS_ERR(c)) { 1921 sb = ERR_CAST(c); 1922 goto got_sb; 1923 } 1924 1925 /* Some options can't be parsed until after the fs is started: */ 1926 ret = bch2_parse_mount_opts(c, &opts, data); 1927 if (ret) { 1928 bch2_fs_stop(c); 1929 sb = ERR_PTR(ret); 1930 goto got_sb; 1931 } 1932 1933 bch2_opts_apply(&c->opts, opts); 1934 1935 sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c); 1936 if (IS_ERR(sb)) 1937 bch2_fs_stop(c); 1938 got_sb: 1939 darray_exit(&devs_to_fs); 1940 bch2_darray_str_exit(&devs); 1941 1942 if (IS_ERR(sb)) { 1943 ret = PTR_ERR(sb); 1944 goto err; 1945 } 1946 1947 c = sb->s_fs_info; 1948 1949 if (sb->s_root) { 1950 if ((flags ^ sb->s_flags) & SB_RDONLY) { 1951 ret = -EBUSY; 1952 goto err_put_super; 1953 } 1954 goto out; 1955 } 1956 1957 sb->s_blocksize = block_bytes(c); 1958 sb->s_blocksize_bits = ilog2(block_bytes(c)); 1959 sb->s_maxbytes = MAX_LFS_FILESIZE; 1960 sb->s_op = &bch_super_operations; 1961 sb->s_export_op = &bch_export_ops; 1962 #ifdef CONFIG_BCACHEFS_QUOTA 1963 sb->s_qcop = &bch2_quotactl_operations; 1964 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ; 1965 #endif 1966 sb->s_xattr = bch2_xattr_handlers; 1967 sb->s_magic = BCACHEFS_STATFS_MAGIC; 1968 sb->s_time_gran = c->sb.nsec_per_time_unit; 1969 sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1; 1970 sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec); 1971 sb->s_uuid = c->sb.user_uuid; 1972 sb->s_shrink->seeks = 0; 1973 c->vfs_sb = sb; 1974 strscpy(sb->s_id, c->name, sizeof(sb->s_id)); 1975 1976 ret = super_setup_bdi(sb); 1977 if (ret) 1978 goto err_put_super; 1979 1980 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES; 1981 1982 for_each_online_member(c, ca) { 1983 struct block_device *bdev = ca->disk_sb.bdev; 1984 1985 /* XXX: create an anonymous device for multi device filesystems */ 1986 sb->s_bdev = bdev; 1987 sb->s_dev = bdev->bd_dev; 1988 percpu_ref_put(&ca->io_ref); 1989 break; 1990 } 1991 1992 c->dev = sb->s_dev; 1993 1994 #ifdef CONFIG_BCACHEFS_POSIX_ACL 1995 if (c->opts.acl) 1996 sb->s_flags |= SB_POSIXACL; 1997 #endif 1998 1999 sb->s_shrink->seeks = 0; 2000 2001 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM); 2002 ret = PTR_ERR_OR_ZERO(vinode); 2003 bch_err_msg(c, ret, "mounting: error getting root inode"); 2004 if (ret) 2005 goto err_put_super; 2006 2007 sb->s_root = d_make_root(vinode); 2008 if (!sb->s_root) { 2009 bch_err(c, "error mounting: error allocating root dentry"); 2010 ret = -ENOMEM; 2011 goto err_put_super; 2012 } 2013 2014 sb->s_flags |= SB_ACTIVE; 2015 out: 2016 return dget(sb->s_root); 2017 2018 err_put_super: 2019 __bch2_fs_stop(c); 2020 deactivate_locked_super(sb); 2021 err: 2022 /* 2023 * On an inconsistency error in recovery we might see an -EROFS derived 2024 * errorcode (from the journal), but we don't want to return that to 2025 * userspace as that causes util-linux to retry the mount RO - which is 2026 * confusing: 2027 */ 2028 if (bch2_err_matches(ret, EROFS) && ret != -EROFS) 2029 ret = -EIO; 2030 return ERR_PTR(bch2_err_class(ret)); 2031 } 2032 2033 static void bch2_kill_sb(struct super_block *sb) 2034 { 2035 struct bch_fs *c = sb->s_fs_info; 2036 2037 generic_shutdown_super(sb); 2038 bch2_fs_free(c); 2039 } 2040 2041 static struct file_system_type bcache_fs_type = { 2042 .owner = THIS_MODULE, 2043 .name = "bcachefs", 2044 .mount = bch2_mount, 2045 .kill_sb = bch2_kill_sb, 2046 .fs_flags = FS_REQUIRES_DEV, 2047 }; 2048 2049 MODULE_ALIAS_FS("bcachefs"); 2050 2051 void bch2_vfs_exit(void) 2052 { 2053 unregister_filesystem(&bcache_fs_type); 2054 kmem_cache_destroy(bch2_inode_cache); 2055 } 2056 2057 int __init bch2_vfs_init(void) 2058 { 2059 int ret = -ENOMEM; 2060 2061 bch2_inode_cache = KMEM_CACHE(bch_inode_info, SLAB_RECLAIM_ACCOUNT); 2062 if (!bch2_inode_cache) 2063 goto err; 2064 2065 ret = register_filesystem(&bcache_fs_type); 2066 if (ret) 2067 goto err; 2068 2069 return 0; 2070 err: 2071 bch2_vfs_exit(); 2072 return ret; 2073 } 2074 2075 #endif /* NO_BCACHEFS_FS */ 2076