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