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 __destroy_inode(&inode->v); 192 kmem_cache_free(bch2_inode_cache, inode); 193 inode = old; 194 } else { 195 mutex_lock(&c->vfs_inodes_lock); 196 list_add(&inode->ei_vfs_inode_list, &c->vfs_inodes_list); 197 mutex_unlock(&c->vfs_inodes_lock); 198 /* 199 * we really don't want insert_inode_locked2() to be setting 200 * I_NEW... 201 */ 202 unlock_new_inode(&inode->v); 203 } 204 205 return inode; 206 } 207 208 #define memalloc_flags_do(_flags, _do) \ 209 ({ \ 210 unsigned _saved_flags = memalloc_flags_save(_flags); \ 211 typeof(_do) _ret = _do; \ 212 memalloc_noreclaim_restore(_saved_flags); \ 213 _ret; \ 214 }) 215 216 static struct inode *bch2_alloc_inode(struct super_block *sb) 217 { 218 BUG(); 219 } 220 221 static struct bch_inode_info *__bch2_new_inode(struct bch_fs *c) 222 { 223 struct bch_inode_info *inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS); 224 if (!inode) 225 return NULL; 226 227 inode_init_once(&inode->v); 228 mutex_init(&inode->ei_update_lock); 229 two_state_lock_init(&inode->ei_pagecache_lock); 230 INIT_LIST_HEAD(&inode->ei_vfs_inode_list); 231 mutex_init(&inode->ei_quota_lock); 232 inode->v.i_state = 0; 233 234 if (unlikely(inode_init_always(c->vfs_sb, &inode->v))) { 235 kmem_cache_free(bch2_inode_cache, inode); 236 return NULL; 237 } 238 239 return inode; 240 } 241 242 /* 243 * Allocate a new inode, dropping/retaking btree locks if necessary: 244 */ 245 static struct bch_inode_info *bch2_new_inode(struct btree_trans *trans) 246 { 247 struct bch_inode_info *inode = 248 memalloc_flags_do(PF_MEMALLOC_NORECLAIM|PF_MEMALLOC_NOWARN, 249 __bch2_new_inode(trans->c)); 250 251 if (unlikely(!inode)) { 252 int ret = drop_locks_do(trans, (inode = __bch2_new_inode(trans->c)) ? 0 : -ENOMEM); 253 if (ret && inode) { 254 __destroy_inode(&inode->v); 255 kmem_cache_free(bch2_inode_cache, inode); 256 } 257 if (ret) 258 return ERR_PTR(ret); 259 } 260 261 return inode; 262 } 263 264 struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum) 265 { 266 struct bch_inode_info *inode = 267 to_bch_ei(ilookup5_nowait(c->vfs_sb, 268 bch2_inode_hash(inum), 269 bch2_iget5_test, 270 &inum)); 271 if (inode) 272 return &inode->v; 273 274 struct btree_trans *trans = bch2_trans_get(c); 275 276 struct bch_inode_unpacked inode_u; 277 struct bch_subvolume subvol; 278 int ret = lockrestart_do(trans, 279 bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?: 280 bch2_inode_find_by_inum_trans(trans, inum, &inode_u)) ?: 281 PTR_ERR_OR_ZERO(inode = bch2_new_inode(trans)); 282 if (!ret) { 283 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol); 284 inode = bch2_inode_insert(c, inode); 285 } 286 bch2_trans_put(trans); 287 288 return ret ? ERR_PTR(ret) : &inode->v; 289 } 290 291 struct bch_inode_info * 292 __bch2_create(struct mnt_idmap *idmap, 293 struct bch_inode_info *dir, struct dentry *dentry, 294 umode_t mode, dev_t rdev, subvol_inum snapshot_src, 295 unsigned flags) 296 { 297 struct bch_fs *c = dir->v.i_sb->s_fs_info; 298 struct btree_trans *trans; 299 struct bch_inode_unpacked dir_u; 300 struct bch_inode_info *inode; 301 struct bch_inode_unpacked inode_u; 302 struct posix_acl *default_acl = NULL, *acl = NULL; 303 subvol_inum inum; 304 struct bch_subvolume subvol; 305 u64 journal_seq = 0; 306 int ret; 307 308 /* 309 * preallocate acls + vfs inode before btree transaction, so that 310 * nothing can fail after the transaction succeeds: 311 */ 312 #ifdef CONFIG_BCACHEFS_POSIX_ACL 313 ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl); 314 if (ret) 315 return ERR_PTR(ret); 316 #endif 317 inode = __bch2_new_inode(c); 318 if (unlikely(!inode)) { 319 inode = ERR_PTR(-ENOMEM); 320 goto err; 321 } 322 323 bch2_inode_init_early(c, &inode_u); 324 325 if (!(flags & BCH_CREATE_TMPFILE)) 326 mutex_lock(&dir->ei_update_lock); 327 328 trans = bch2_trans_get(c); 329 retry: 330 bch2_trans_begin(trans); 331 332 ret = bch2_subvol_is_ro_trans(trans, dir->ei_subvol) ?: 333 bch2_create_trans(trans, 334 inode_inum(dir), &dir_u, &inode_u, 335 !(flags & BCH_CREATE_TMPFILE) 336 ? &dentry->d_name : NULL, 337 from_kuid(i_user_ns(&dir->v), current_fsuid()), 338 from_kgid(i_user_ns(&dir->v), current_fsgid()), 339 mode, rdev, 340 default_acl, acl, snapshot_src, flags) ?: 341 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1, 342 KEY_TYPE_QUOTA_PREALLOC); 343 if (unlikely(ret)) 344 goto err_before_quota; 345 346 inum.subvol = inode_u.bi_subvol ?: dir->ei_subvol; 347 inum.inum = inode_u.bi_inum; 348 349 ret = bch2_subvolume_get(trans, inum.subvol, true, 350 BTREE_ITER_with_updates, &subvol) ?: 351 bch2_trans_commit(trans, NULL, &journal_seq, 0); 352 if (unlikely(ret)) { 353 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1, 354 KEY_TYPE_QUOTA_WARN); 355 err_before_quota: 356 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 357 goto retry; 358 goto err_trans; 359 } 360 361 if (!(flags & BCH_CREATE_TMPFILE)) { 362 bch2_inode_update_after_write(trans, dir, &dir_u, 363 ATTR_MTIME|ATTR_CTIME); 364 mutex_unlock(&dir->ei_update_lock); 365 } 366 367 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol); 368 369 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl); 370 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl); 371 372 /* 373 * we must insert the new inode into the inode cache before calling 374 * bch2_trans_exit() and dropping locks, else we could race with another 375 * thread pulling the inode in and modifying it: 376 */ 377 inode = bch2_inode_insert(c, inode); 378 bch2_trans_put(trans); 379 err: 380 posix_acl_release(default_acl); 381 posix_acl_release(acl); 382 return inode; 383 err_trans: 384 if (!(flags & BCH_CREATE_TMPFILE)) 385 mutex_unlock(&dir->ei_update_lock); 386 387 bch2_trans_put(trans); 388 make_bad_inode(&inode->v); 389 iput(&inode->v); 390 inode = ERR_PTR(ret); 391 goto err; 392 } 393 394 /* methods */ 395 396 static struct bch_inode_info *bch2_lookup_trans(struct btree_trans *trans, 397 subvol_inum dir, struct bch_hash_info *dir_hash_info, 398 const struct qstr *name) 399 { 400 struct bch_fs *c = trans->c; 401 struct btree_iter dirent_iter = {}; 402 subvol_inum inum = {}; 403 struct printbuf buf = PRINTBUF; 404 405 struct bkey_s_c k = bch2_hash_lookup(trans, &dirent_iter, bch2_dirent_hash_desc, 406 dir_hash_info, dir, name, 0); 407 int ret = bkey_err(k); 408 if (ret) 409 return ERR_PTR(ret); 410 411 ret = bch2_dirent_read_target(trans, dir, bkey_s_c_to_dirent(k), &inum); 412 if (ret > 0) 413 ret = -ENOENT; 414 if (ret) 415 goto err; 416 417 struct bch_inode_info *inode = 418 to_bch_ei(ilookup5_nowait(c->vfs_sb, 419 bch2_inode_hash(inum), 420 bch2_iget5_test, 421 &inum)); 422 if (inode) 423 goto out; 424 425 struct bch_subvolume subvol; 426 struct bch_inode_unpacked inode_u; 427 ret = bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?: 428 bch2_inode_find_by_inum_nowarn_trans(trans, inum, &inode_u) ?: 429 PTR_ERR_OR_ZERO(inode = bch2_new_inode(trans)); 430 431 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), 432 c, "dirent to missing inode:\n %s", 433 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)); 434 if (ret) 435 goto err; 436 437 /* regular files may have hardlinks: */ 438 if (bch2_fs_inconsistent_on(bch2_inode_should_have_bp(&inode_u) && 439 !bkey_eq(k.k->p, POS(inode_u.bi_dir, inode_u.bi_dir_offset)), 440 c, 441 "dirent points to inode that does not point back:\n %s", 442 (bch2_bkey_val_to_text(&buf, c, k), 443 prt_printf(&buf, "\n "), 444 bch2_inode_unpacked_to_text(&buf, &inode_u), 445 buf.buf))) { 446 ret = -ENOENT; 447 goto err; 448 } 449 450 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol); 451 inode = bch2_inode_insert(c, inode); 452 out: 453 bch2_trans_iter_exit(trans, &dirent_iter); 454 printbuf_exit(&buf); 455 return inode; 456 err: 457 inode = ERR_PTR(ret); 458 goto out; 459 } 460 461 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry, 462 unsigned int flags) 463 { 464 struct bch_fs *c = vdir->i_sb->s_fs_info; 465 struct bch_inode_info *dir = to_bch_ei(vdir); 466 struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode); 467 468 struct bch_inode_info *inode; 469 bch2_trans_do(c, NULL, NULL, 0, 470 PTR_ERR_OR_ZERO(inode = bch2_lookup_trans(trans, inode_inum(dir), 471 &hash, &dentry->d_name))); 472 if (IS_ERR(inode)) 473 inode = NULL; 474 475 return d_splice_alias(&inode->v, dentry); 476 } 477 478 static int bch2_mknod(struct mnt_idmap *idmap, 479 struct inode *vdir, struct dentry *dentry, 480 umode_t mode, dev_t rdev) 481 { 482 struct bch_inode_info *inode = 483 __bch2_create(idmap, to_bch_ei(vdir), dentry, mode, rdev, 484 (subvol_inum) { 0 }, 0); 485 486 if (IS_ERR(inode)) 487 return bch2_err_class(PTR_ERR(inode)); 488 489 d_instantiate(dentry, &inode->v); 490 return 0; 491 } 492 493 static int bch2_create(struct mnt_idmap *idmap, 494 struct inode *vdir, struct dentry *dentry, 495 umode_t mode, bool excl) 496 { 497 return bch2_mknod(idmap, vdir, dentry, mode|S_IFREG, 0); 498 } 499 500 static int __bch2_link(struct bch_fs *c, 501 struct bch_inode_info *inode, 502 struct bch_inode_info *dir, 503 struct dentry *dentry) 504 { 505 struct btree_trans *trans = bch2_trans_get(c); 506 struct bch_inode_unpacked dir_u, inode_u; 507 int ret; 508 509 mutex_lock(&inode->ei_update_lock); 510 511 ret = commit_do(trans, NULL, NULL, 0, 512 bch2_link_trans(trans, 513 inode_inum(dir), &dir_u, 514 inode_inum(inode), &inode_u, 515 &dentry->d_name)); 516 517 if (likely(!ret)) { 518 bch2_inode_update_after_write(trans, dir, &dir_u, 519 ATTR_MTIME|ATTR_CTIME); 520 bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME); 521 } 522 523 bch2_trans_put(trans); 524 mutex_unlock(&inode->ei_update_lock); 525 return ret; 526 } 527 528 static int bch2_link(struct dentry *old_dentry, struct inode *vdir, 529 struct dentry *dentry) 530 { 531 struct bch_fs *c = vdir->i_sb->s_fs_info; 532 struct bch_inode_info *dir = to_bch_ei(vdir); 533 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode); 534 int ret; 535 536 lockdep_assert_held(&inode->v.i_rwsem); 537 538 ret = bch2_subvol_is_ro(c, dir->ei_subvol) ?: 539 bch2_subvol_is_ro(c, inode->ei_subvol) ?: 540 __bch2_link(c, inode, dir, dentry); 541 if (unlikely(ret)) 542 return bch2_err_class(ret); 543 544 ihold(&inode->v); 545 d_instantiate(dentry, &inode->v); 546 return 0; 547 } 548 549 int __bch2_unlink(struct inode *vdir, struct dentry *dentry, 550 bool deleting_snapshot) 551 { 552 struct bch_fs *c = vdir->i_sb->s_fs_info; 553 struct bch_inode_info *dir = to_bch_ei(vdir); 554 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode); 555 struct bch_inode_unpacked dir_u, inode_u; 556 struct btree_trans *trans = bch2_trans_get(c); 557 int ret; 558 559 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode); 560 561 ret = commit_do(trans, NULL, NULL, 562 BCH_TRANS_COMMIT_no_enospc, 563 bch2_unlink_trans(trans, 564 inode_inum(dir), &dir_u, 565 &inode_u, &dentry->d_name, 566 deleting_snapshot)); 567 if (unlikely(ret)) 568 goto err; 569 570 bch2_inode_update_after_write(trans, dir, &dir_u, 571 ATTR_MTIME|ATTR_CTIME); 572 bch2_inode_update_after_write(trans, inode, &inode_u, 573 ATTR_MTIME); 574 575 if (inode_u.bi_subvol) { 576 /* 577 * Subvolume deletion is asynchronous, but we still want to tell 578 * the VFS that it's been deleted here: 579 */ 580 set_nlink(&inode->v, 0); 581 } 582 err: 583 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode); 584 bch2_trans_put(trans); 585 586 return ret; 587 } 588 589 static int bch2_unlink(struct inode *vdir, struct dentry *dentry) 590 { 591 struct bch_inode_info *dir= to_bch_ei(vdir); 592 struct bch_fs *c = dir->v.i_sb->s_fs_info; 593 594 int ret = bch2_subvol_is_ro(c, dir->ei_subvol) ?: 595 __bch2_unlink(vdir, dentry, false); 596 return bch2_err_class(ret); 597 } 598 599 static int bch2_symlink(struct mnt_idmap *idmap, 600 struct inode *vdir, struct dentry *dentry, 601 const char *symname) 602 { 603 struct bch_fs *c = vdir->i_sb->s_fs_info; 604 struct bch_inode_info *dir = to_bch_ei(vdir), *inode; 605 int ret; 606 607 inode = __bch2_create(idmap, dir, dentry, S_IFLNK|S_IRWXUGO, 0, 608 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE); 609 if (IS_ERR(inode)) 610 return bch2_err_class(PTR_ERR(inode)); 611 612 inode_lock(&inode->v); 613 ret = page_symlink(&inode->v, symname, strlen(symname) + 1); 614 inode_unlock(&inode->v); 615 616 if (unlikely(ret)) 617 goto err; 618 619 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX); 620 if (unlikely(ret)) 621 goto err; 622 623 ret = __bch2_link(c, inode, dir, dentry); 624 if (unlikely(ret)) 625 goto err; 626 627 d_instantiate(dentry, &inode->v); 628 return 0; 629 err: 630 iput(&inode->v); 631 return bch2_err_class(ret); 632 } 633 634 static int bch2_mkdir(struct mnt_idmap *idmap, 635 struct inode *vdir, struct dentry *dentry, umode_t mode) 636 { 637 return bch2_mknod(idmap, vdir, dentry, mode|S_IFDIR, 0); 638 } 639 640 static int bch2_rename2(struct mnt_idmap *idmap, 641 struct inode *src_vdir, struct dentry *src_dentry, 642 struct inode *dst_vdir, struct dentry *dst_dentry, 643 unsigned flags) 644 { 645 struct bch_fs *c = src_vdir->i_sb->s_fs_info; 646 struct bch_inode_info *src_dir = to_bch_ei(src_vdir); 647 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir); 648 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode); 649 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode); 650 struct bch_inode_unpacked dst_dir_u, src_dir_u; 651 struct bch_inode_unpacked src_inode_u, dst_inode_u; 652 struct btree_trans *trans; 653 enum bch_rename_mode mode = flags & RENAME_EXCHANGE 654 ? BCH_RENAME_EXCHANGE 655 : dst_dentry->d_inode 656 ? BCH_RENAME_OVERWRITE : BCH_RENAME; 657 int ret; 658 659 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE)) 660 return -EINVAL; 661 662 if (mode == BCH_RENAME_OVERWRITE) { 663 ret = filemap_write_and_wait_range(src_inode->v.i_mapping, 664 0, LLONG_MAX); 665 if (ret) 666 return ret; 667 } 668 669 trans = bch2_trans_get(c); 670 671 bch2_lock_inodes(INODE_UPDATE_LOCK, 672 src_dir, 673 dst_dir, 674 src_inode, 675 dst_inode); 676 677 ret = bch2_subvol_is_ro_trans(trans, src_dir->ei_subvol) ?: 678 bch2_subvol_is_ro_trans(trans, dst_dir->ei_subvol); 679 if (ret) 680 goto err; 681 682 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) { 683 ret = bch2_fs_quota_transfer(c, src_inode, 684 dst_dir->ei_qid, 685 1 << QTYP_PRJ, 686 KEY_TYPE_QUOTA_PREALLOC); 687 if (ret) 688 goto err; 689 } 690 691 if (mode == BCH_RENAME_EXCHANGE && 692 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) { 693 ret = bch2_fs_quota_transfer(c, dst_inode, 694 src_dir->ei_qid, 695 1 << QTYP_PRJ, 696 KEY_TYPE_QUOTA_PREALLOC); 697 if (ret) 698 goto err; 699 } 700 701 ret = commit_do(trans, NULL, NULL, 0, 702 bch2_rename_trans(trans, 703 inode_inum(src_dir), &src_dir_u, 704 inode_inum(dst_dir), &dst_dir_u, 705 &src_inode_u, 706 &dst_inode_u, 707 &src_dentry->d_name, 708 &dst_dentry->d_name, 709 mode)); 710 if (unlikely(ret)) 711 goto err; 712 713 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum); 714 BUG_ON(dst_inode && 715 dst_inode->v.i_ino != dst_inode_u.bi_inum); 716 717 bch2_inode_update_after_write(trans, src_dir, &src_dir_u, 718 ATTR_MTIME|ATTR_CTIME); 719 720 if (src_dir != dst_dir) 721 bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u, 722 ATTR_MTIME|ATTR_CTIME); 723 724 bch2_inode_update_after_write(trans, src_inode, &src_inode_u, 725 ATTR_CTIME); 726 727 if (dst_inode) 728 bch2_inode_update_after_write(trans, dst_inode, &dst_inode_u, 729 ATTR_CTIME); 730 err: 731 bch2_trans_put(trans); 732 733 bch2_fs_quota_transfer(c, src_inode, 734 bch_qid(&src_inode->ei_inode), 735 1 << QTYP_PRJ, 736 KEY_TYPE_QUOTA_NOCHECK); 737 if (dst_inode) 738 bch2_fs_quota_transfer(c, dst_inode, 739 bch_qid(&dst_inode->ei_inode), 740 1 << QTYP_PRJ, 741 KEY_TYPE_QUOTA_NOCHECK); 742 743 bch2_unlock_inodes(INODE_UPDATE_LOCK, 744 src_dir, 745 dst_dir, 746 src_inode, 747 dst_inode); 748 749 return bch2_err_class(ret); 750 } 751 752 static void bch2_setattr_copy(struct mnt_idmap *idmap, 753 struct bch_inode_info *inode, 754 struct bch_inode_unpacked *bi, 755 struct iattr *attr) 756 { 757 struct bch_fs *c = inode->v.i_sb->s_fs_info; 758 unsigned int ia_valid = attr->ia_valid; 759 760 if (ia_valid & ATTR_UID) 761 bi->bi_uid = from_kuid(i_user_ns(&inode->v), attr->ia_uid); 762 if (ia_valid & ATTR_GID) 763 bi->bi_gid = from_kgid(i_user_ns(&inode->v), attr->ia_gid); 764 765 if (ia_valid & ATTR_SIZE) 766 bi->bi_size = attr->ia_size; 767 768 if (ia_valid & ATTR_ATIME) 769 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime); 770 if (ia_valid & ATTR_MTIME) 771 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime); 772 if (ia_valid & ATTR_CTIME) 773 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime); 774 775 if (ia_valid & ATTR_MODE) { 776 umode_t mode = attr->ia_mode; 777 kgid_t gid = ia_valid & ATTR_GID 778 ? attr->ia_gid 779 : inode->v.i_gid; 780 781 if (!in_group_p(gid) && 782 !capable_wrt_inode_uidgid(idmap, &inode->v, CAP_FSETID)) 783 mode &= ~S_ISGID; 784 bi->bi_mode = mode; 785 } 786 } 787 788 int bch2_setattr_nonsize(struct mnt_idmap *idmap, 789 struct bch_inode_info *inode, 790 struct iattr *attr) 791 { 792 struct bch_fs *c = inode->v.i_sb->s_fs_info; 793 struct bch_qid qid; 794 struct btree_trans *trans; 795 struct btree_iter inode_iter = { NULL }; 796 struct bch_inode_unpacked inode_u; 797 struct posix_acl *acl = NULL; 798 int ret; 799 800 mutex_lock(&inode->ei_update_lock); 801 802 qid = inode->ei_qid; 803 804 if (attr->ia_valid & ATTR_UID) 805 qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), attr->ia_uid); 806 807 if (attr->ia_valid & ATTR_GID) 808 qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), attr->ia_gid); 809 810 ret = bch2_fs_quota_transfer(c, inode, qid, ~0, 811 KEY_TYPE_QUOTA_PREALLOC); 812 if (ret) 813 goto err; 814 815 trans = bch2_trans_get(c); 816 retry: 817 bch2_trans_begin(trans); 818 kfree(acl); 819 acl = NULL; 820 821 ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inode_inum(inode), 822 BTREE_ITER_intent); 823 if (ret) 824 goto btree_err; 825 826 bch2_setattr_copy(idmap, inode, &inode_u, attr); 827 828 if (attr->ia_valid & ATTR_MODE) { 829 ret = bch2_acl_chmod(trans, inode_inum(inode), &inode_u, 830 inode_u.bi_mode, &acl); 831 if (ret) 832 goto btree_err; 833 } 834 835 ret = bch2_inode_write(trans, &inode_iter, &inode_u) ?: 836 bch2_trans_commit(trans, NULL, NULL, 837 BCH_TRANS_COMMIT_no_enospc); 838 btree_err: 839 bch2_trans_iter_exit(trans, &inode_iter); 840 841 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 842 goto retry; 843 if (unlikely(ret)) 844 goto err_trans; 845 846 bch2_inode_update_after_write(trans, inode, &inode_u, attr->ia_valid); 847 848 if (acl) 849 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl); 850 err_trans: 851 bch2_trans_put(trans); 852 err: 853 mutex_unlock(&inode->ei_update_lock); 854 855 return bch2_err_class(ret); 856 } 857 858 static int bch2_getattr(struct mnt_idmap *idmap, 859 const struct path *path, struct kstat *stat, 860 u32 request_mask, unsigned query_flags) 861 { 862 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry)); 863 struct bch_fs *c = inode->v.i_sb->s_fs_info; 864 865 stat->dev = inode->v.i_sb->s_dev; 866 stat->ino = inode->v.i_ino; 867 stat->mode = inode->v.i_mode; 868 stat->nlink = inode->v.i_nlink; 869 stat->uid = inode->v.i_uid; 870 stat->gid = inode->v.i_gid; 871 stat->rdev = inode->v.i_rdev; 872 stat->size = i_size_read(&inode->v); 873 stat->atime = inode_get_atime(&inode->v); 874 stat->mtime = inode_get_mtime(&inode->v); 875 stat->ctime = inode_get_ctime(&inode->v); 876 stat->blksize = block_bytes(c); 877 stat->blocks = inode->v.i_blocks; 878 879 stat->subvol = inode->ei_subvol; 880 stat->result_mask |= STATX_SUBVOL; 881 882 if (request_mask & STATX_BTIME) { 883 stat->result_mask |= STATX_BTIME; 884 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime); 885 } 886 887 if (inode->ei_inode.bi_flags & BCH_INODE_immutable) 888 stat->attributes |= STATX_ATTR_IMMUTABLE; 889 stat->attributes_mask |= STATX_ATTR_IMMUTABLE; 890 891 if (inode->ei_inode.bi_flags & BCH_INODE_append) 892 stat->attributes |= STATX_ATTR_APPEND; 893 stat->attributes_mask |= STATX_ATTR_APPEND; 894 895 if (inode->ei_inode.bi_flags & BCH_INODE_nodump) 896 stat->attributes |= STATX_ATTR_NODUMP; 897 stat->attributes_mask |= STATX_ATTR_NODUMP; 898 899 return 0; 900 } 901 902 static int bch2_setattr(struct mnt_idmap *idmap, 903 struct dentry *dentry, struct iattr *iattr) 904 { 905 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode); 906 struct bch_fs *c = inode->v.i_sb->s_fs_info; 907 int ret; 908 909 lockdep_assert_held(&inode->v.i_rwsem); 910 911 ret = bch2_subvol_is_ro(c, inode->ei_subvol) ?: 912 setattr_prepare(idmap, dentry, iattr); 913 if (ret) 914 return ret; 915 916 return iattr->ia_valid & ATTR_SIZE 917 ? bchfs_truncate(idmap, inode, iattr) 918 : bch2_setattr_nonsize(idmap, inode, iattr); 919 } 920 921 static int bch2_tmpfile(struct mnt_idmap *idmap, 922 struct inode *vdir, struct file *file, umode_t mode) 923 { 924 struct bch_inode_info *inode = 925 __bch2_create(idmap, to_bch_ei(vdir), 926 file->f_path.dentry, mode, 0, 927 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE); 928 929 if (IS_ERR(inode)) 930 return bch2_err_class(PTR_ERR(inode)); 931 932 d_mark_tmpfile(file, &inode->v); 933 d_instantiate(file->f_path.dentry, &inode->v); 934 return finish_open_simple(file, 0); 935 } 936 937 static int bch2_fill_extent(struct bch_fs *c, 938 struct fiemap_extent_info *info, 939 struct bkey_s_c k, unsigned flags) 940 { 941 if (bkey_extent_is_direct_data(k.k)) { 942 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); 943 const union bch_extent_entry *entry; 944 struct extent_ptr_decoded p; 945 int ret; 946 947 if (k.k->type == KEY_TYPE_reflink_v) 948 flags |= FIEMAP_EXTENT_SHARED; 949 950 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) { 951 int flags2 = 0; 952 u64 offset = p.ptr.offset; 953 954 if (p.ptr.unwritten) 955 flags2 |= FIEMAP_EXTENT_UNWRITTEN; 956 957 if (p.crc.compression_type) 958 flags2 |= FIEMAP_EXTENT_ENCODED; 959 else 960 offset += p.crc.offset; 961 962 if ((offset & (block_sectors(c) - 1)) || 963 (k.k->size & (block_sectors(c) - 1))) 964 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED; 965 966 ret = fiemap_fill_next_extent(info, 967 bkey_start_offset(k.k) << 9, 968 offset << 9, 969 k.k->size << 9, flags|flags2); 970 if (ret) 971 return ret; 972 } 973 974 return 0; 975 } else if (bkey_extent_is_inline_data(k.k)) { 976 return fiemap_fill_next_extent(info, 977 bkey_start_offset(k.k) << 9, 978 0, k.k->size << 9, 979 flags| 980 FIEMAP_EXTENT_DATA_INLINE); 981 } else if (k.k->type == KEY_TYPE_reservation) { 982 return fiemap_fill_next_extent(info, 983 bkey_start_offset(k.k) << 9, 984 0, k.k->size << 9, 985 flags| 986 FIEMAP_EXTENT_DELALLOC| 987 FIEMAP_EXTENT_UNWRITTEN); 988 } else { 989 BUG(); 990 } 991 } 992 993 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info, 994 u64 start, u64 len) 995 { 996 struct bch_fs *c = vinode->i_sb->s_fs_info; 997 struct bch_inode_info *ei = to_bch_ei(vinode); 998 struct btree_trans *trans; 999 struct btree_iter iter; 1000 struct bkey_s_c k; 1001 struct bkey_buf cur, prev; 1002 unsigned offset_into_extent, sectors; 1003 bool have_extent = false; 1004 u32 snapshot; 1005 int ret = 0; 1006 1007 ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC); 1008 if (ret) 1009 return ret; 1010 1011 struct bpos end = POS(ei->v.i_ino, (start + len) >> 9); 1012 if (start + len < start) 1013 return -EINVAL; 1014 1015 start >>= 9; 1016 1017 bch2_bkey_buf_init(&cur); 1018 bch2_bkey_buf_init(&prev); 1019 trans = bch2_trans_get(c); 1020 retry: 1021 bch2_trans_begin(trans); 1022 1023 ret = bch2_subvolume_get_snapshot(trans, ei->ei_subvol, &snapshot); 1024 if (ret) 1025 goto err; 1026 1027 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, 1028 SPOS(ei->v.i_ino, start, snapshot), 0); 1029 1030 while (!(ret = btree_trans_too_many_iters(trans)) && 1031 (k = bch2_btree_iter_peek_upto(&iter, end)).k && 1032 !(ret = bkey_err(k))) { 1033 enum btree_id data_btree = BTREE_ID_extents; 1034 1035 if (!bkey_extent_is_data(k.k) && 1036 k.k->type != KEY_TYPE_reservation) { 1037 bch2_btree_iter_advance(&iter); 1038 continue; 1039 } 1040 1041 offset_into_extent = iter.pos.offset - 1042 bkey_start_offset(k.k); 1043 sectors = k.k->size - offset_into_extent; 1044 1045 bch2_bkey_buf_reassemble(&cur, c, k); 1046 1047 ret = bch2_read_indirect_extent(trans, &data_btree, 1048 &offset_into_extent, &cur); 1049 if (ret) 1050 break; 1051 1052 k = bkey_i_to_s_c(cur.k); 1053 bch2_bkey_buf_realloc(&prev, c, k.k->u64s); 1054 1055 sectors = min(sectors, k.k->size - offset_into_extent); 1056 1057 bch2_cut_front(POS(k.k->p.inode, 1058 bkey_start_offset(k.k) + 1059 offset_into_extent), 1060 cur.k); 1061 bch2_key_resize(&cur.k->k, sectors); 1062 cur.k->k.p = iter.pos; 1063 cur.k->k.p.offset += cur.k->k.size; 1064 1065 if (have_extent) { 1066 bch2_trans_unlock(trans); 1067 ret = bch2_fill_extent(c, info, 1068 bkey_i_to_s_c(prev.k), 0); 1069 if (ret) 1070 break; 1071 } 1072 1073 bkey_copy(prev.k, cur.k); 1074 have_extent = true; 1075 1076 bch2_btree_iter_set_pos(&iter, 1077 POS(iter.pos.inode, iter.pos.offset + sectors)); 1078 1079 ret = bch2_trans_relock(trans); 1080 if (ret) 1081 break; 1082 } 1083 start = iter.pos.offset; 1084 bch2_trans_iter_exit(trans, &iter); 1085 err: 1086 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 1087 goto retry; 1088 1089 if (!ret && have_extent) { 1090 bch2_trans_unlock(trans); 1091 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k), 1092 FIEMAP_EXTENT_LAST); 1093 } 1094 1095 bch2_trans_put(trans); 1096 bch2_bkey_buf_exit(&cur, c); 1097 bch2_bkey_buf_exit(&prev, c); 1098 return ret < 0 ? ret : 0; 1099 } 1100 1101 static const struct vm_operations_struct bch_vm_ops = { 1102 .fault = bch2_page_fault, 1103 .map_pages = filemap_map_pages, 1104 .page_mkwrite = bch2_page_mkwrite, 1105 }; 1106 1107 static int bch2_mmap(struct file *file, struct vm_area_struct *vma) 1108 { 1109 file_accessed(file); 1110 1111 vma->vm_ops = &bch_vm_ops; 1112 return 0; 1113 } 1114 1115 /* Directories: */ 1116 1117 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence) 1118 { 1119 return generic_file_llseek_size(file, offset, whence, 1120 S64_MAX, S64_MAX); 1121 } 1122 1123 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx) 1124 { 1125 struct bch_inode_info *inode = file_bch_inode(file); 1126 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1127 1128 if (!dir_emit_dots(file, ctx)) 1129 return 0; 1130 1131 int ret = bch2_readdir(c, inode_inum(inode), ctx); 1132 1133 bch_err_fn(c, ret); 1134 return bch2_err_class(ret); 1135 } 1136 1137 static int bch2_open(struct inode *vinode, struct file *file) 1138 { 1139 if (file->f_flags & (O_WRONLY|O_RDWR)) { 1140 struct bch_inode_info *inode = to_bch_ei(vinode); 1141 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1142 1143 int ret = bch2_subvol_is_ro(c, inode->ei_subvol); 1144 if (ret) 1145 return ret; 1146 } 1147 1148 return generic_file_open(vinode, file); 1149 } 1150 1151 static const struct file_operations bch_file_operations = { 1152 .open = bch2_open, 1153 .llseek = bch2_llseek, 1154 .read_iter = bch2_read_iter, 1155 .write_iter = bch2_write_iter, 1156 .mmap = bch2_mmap, 1157 .fsync = bch2_fsync, 1158 .splice_read = filemap_splice_read, 1159 .splice_write = iter_file_splice_write, 1160 .fallocate = bch2_fallocate_dispatch, 1161 .unlocked_ioctl = bch2_fs_file_ioctl, 1162 #ifdef CONFIG_COMPAT 1163 .compat_ioctl = bch2_compat_fs_ioctl, 1164 #endif 1165 .remap_file_range = bch2_remap_file_range, 1166 }; 1167 1168 static const struct inode_operations bch_file_inode_operations = { 1169 .getattr = bch2_getattr, 1170 .setattr = bch2_setattr, 1171 .fiemap = bch2_fiemap, 1172 .listxattr = bch2_xattr_list, 1173 #ifdef CONFIG_BCACHEFS_POSIX_ACL 1174 .get_acl = bch2_get_acl, 1175 .set_acl = bch2_set_acl, 1176 #endif 1177 }; 1178 1179 static const struct inode_operations bch_dir_inode_operations = { 1180 .lookup = bch2_lookup, 1181 .create = bch2_create, 1182 .link = bch2_link, 1183 .unlink = bch2_unlink, 1184 .symlink = bch2_symlink, 1185 .mkdir = bch2_mkdir, 1186 .rmdir = bch2_unlink, 1187 .mknod = bch2_mknod, 1188 .rename = bch2_rename2, 1189 .getattr = bch2_getattr, 1190 .setattr = bch2_setattr, 1191 .tmpfile = bch2_tmpfile, 1192 .listxattr = bch2_xattr_list, 1193 #ifdef CONFIG_BCACHEFS_POSIX_ACL 1194 .get_acl = bch2_get_acl, 1195 .set_acl = bch2_set_acl, 1196 #endif 1197 }; 1198 1199 static const struct file_operations bch_dir_file_operations = { 1200 .llseek = bch2_dir_llseek, 1201 .read = generic_read_dir, 1202 .iterate_shared = bch2_vfs_readdir, 1203 .fsync = bch2_fsync, 1204 .unlocked_ioctl = bch2_fs_file_ioctl, 1205 #ifdef CONFIG_COMPAT 1206 .compat_ioctl = bch2_compat_fs_ioctl, 1207 #endif 1208 }; 1209 1210 static const struct inode_operations bch_symlink_inode_operations = { 1211 .get_link = page_get_link, 1212 .getattr = bch2_getattr, 1213 .setattr = bch2_setattr, 1214 .listxattr = bch2_xattr_list, 1215 #ifdef CONFIG_BCACHEFS_POSIX_ACL 1216 .get_acl = bch2_get_acl, 1217 .set_acl = bch2_set_acl, 1218 #endif 1219 }; 1220 1221 static const struct inode_operations bch_special_inode_operations = { 1222 .getattr = bch2_getattr, 1223 .setattr = bch2_setattr, 1224 .listxattr = bch2_xattr_list, 1225 #ifdef CONFIG_BCACHEFS_POSIX_ACL 1226 .get_acl = bch2_get_acl, 1227 .set_acl = bch2_set_acl, 1228 #endif 1229 }; 1230 1231 static const struct address_space_operations bch_address_space_operations = { 1232 .read_folio = bch2_read_folio, 1233 .writepages = bch2_writepages, 1234 .readahead = bch2_readahead, 1235 .dirty_folio = filemap_dirty_folio, 1236 .write_begin = bch2_write_begin, 1237 .write_end = bch2_write_end, 1238 .invalidate_folio = bch2_invalidate_folio, 1239 .release_folio = bch2_release_folio, 1240 .direct_IO = noop_direct_IO, 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