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-io.h" 15 #include "fs-ioctl.h" 16 #include "fs-io-buffered.h" 17 #include "fs-io-direct.h" 18 #include "fs-io-pagecache.h" 19 #include "fsck.h" 20 #include "inode.h" 21 #include "io_read.h" 22 #include "journal.h" 23 #include "keylist.h" 24 #include "namei.h" 25 #include "quota.h" 26 #include "rebalance.h" 27 #include "snapshot.h" 28 #include "super.h" 29 #include "xattr.h" 30 #include "trace.h" 31 32 #include <linux/aio.h> 33 #include <linux/backing-dev.h> 34 #include <linux/exportfs.h> 35 #include <linux/fiemap.h> 36 #include <linux/fileattr.h> 37 #include <linux/fs_context.h> 38 #include <linux/module.h> 39 #include <linux/pagemap.h> 40 #include <linux/posix_acl.h> 41 #include <linux/random.h> 42 #include <linux/seq_file.h> 43 #include <linux/siphash.h> 44 #include <linux/statfs.h> 45 #include <linux/string.h> 46 #include <linux/xattr.h> 47 48 static struct kmem_cache *bch2_inode_cache; 49 50 static void bch2_vfs_inode_init(struct btree_trans *, subvol_inum, 51 struct bch_inode_info *, 52 struct bch_inode_unpacked *, 53 struct bch_subvolume *); 54 55 /* Set VFS inode flags from bcachefs inode: */ 56 static inline void bch2_inode_flags_to_vfs(struct bch_fs *c, struct bch_inode_info *inode) 57 { 58 static const __maybe_unused unsigned bch_flags_to_vfs[] = { 59 [__BCH_INODE_sync] = S_SYNC, 60 [__BCH_INODE_immutable] = S_IMMUTABLE, 61 [__BCH_INODE_append] = S_APPEND, 62 [__BCH_INODE_noatime] = S_NOATIME, 63 }; 64 65 set_flags(bch_flags_to_vfs, inode->ei_inode.bi_flags, inode->v.i_flags); 66 67 if (bch2_inode_casefold(c, &inode->ei_inode)) 68 inode->v.i_flags |= S_CASEFOLD; 69 else 70 inode->v.i_flags &= ~S_CASEFOLD; 71 } 72 73 void bch2_inode_update_after_write(struct btree_trans *trans, 74 struct bch_inode_info *inode, 75 struct bch_inode_unpacked *bi, 76 unsigned fields) 77 { 78 struct bch_fs *c = trans->c; 79 80 BUG_ON(bi->bi_inum != inode->v.i_ino); 81 82 bch2_assert_pos_locked(trans, BTREE_ID_inodes, POS(0, bi->bi_inum)); 83 84 set_nlink(&inode->v, bch2_inode_nlink_get(bi)); 85 i_uid_write(&inode->v, bi->bi_uid); 86 i_gid_write(&inode->v, bi->bi_gid); 87 inode->v.i_mode = bi->bi_mode; 88 89 if (fields & ATTR_SIZE) 90 i_size_write(&inode->v, bi->bi_size); 91 92 if (fields & ATTR_ATIME) 93 inode_set_atime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_atime)); 94 if (fields & ATTR_MTIME) 95 inode_set_mtime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_mtime)); 96 if (fields & ATTR_CTIME) 97 inode_set_ctime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_ctime)); 98 99 inode->ei_inode = *bi; 100 101 bch2_inode_flags_to_vfs(c, inode); 102 } 103 104 int __must_check bch2_write_inode(struct bch_fs *c, 105 struct bch_inode_info *inode, 106 inode_set_fn set, 107 void *p, unsigned fields) 108 { 109 struct btree_trans *trans = bch2_trans_get(c); 110 struct btree_iter iter = {}; 111 struct bch_inode_unpacked inode_u; 112 int ret; 113 retry: 114 bch2_trans_begin(trans); 115 116 ret = bch2_inode_peek(trans, &iter, &inode_u, inode_inum(inode), BTREE_ITER_intent); 117 if (ret) 118 goto err; 119 120 struct bch_extent_rebalance old_r = bch2_inode_rebalance_opts_get(c, &inode_u); 121 122 ret = (set ? set(trans, inode, &inode_u, p) : 0); 123 if (ret) 124 goto err; 125 126 struct bch_extent_rebalance new_r = bch2_inode_rebalance_opts_get(c, &inode_u); 127 bool rebalance_changed = memcmp(&old_r, &new_r, sizeof(new_r)); 128 129 if (rebalance_changed) { 130 ret = bch2_set_rebalance_needs_scan_trans(trans, inode_u.bi_inum); 131 if (ret) 132 goto err; 133 } 134 135 ret = bch2_inode_write(trans, &iter, &inode_u) ?: 136 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc); 137 138 /* 139 * the btree node lock protects inode->ei_inode, not ei_update_lock; 140 * this is important for inode updates via bchfs_write_index_update 141 */ 142 if (!ret) 143 bch2_inode_update_after_write(trans, inode, &inode_u, fields); 144 err: 145 bch2_trans_iter_exit(trans, &iter); 146 147 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 148 goto retry; 149 150 if (rebalance_changed) 151 bch2_rebalance_wakeup(c); 152 153 bch2_fs_fatal_err_on(bch2_err_matches(ret, ENOENT), c, 154 "%s: inode %llu:%llu not found when updating", 155 bch2_err_str(ret), 156 inode_inum(inode).subvol, 157 inode_inum(inode).inum); 158 159 bch2_trans_put(trans); 160 return ret < 0 ? ret : 0; 161 } 162 163 int bch2_fs_quota_transfer(struct bch_fs *c, 164 struct bch_inode_info *inode, 165 struct bch_qid new_qid, 166 unsigned qtypes, 167 enum quota_acct_mode mode) 168 { 169 unsigned i; 170 int ret; 171 172 qtypes &= enabled_qtypes(c); 173 174 for (i = 0; i < QTYP_NR; i++) 175 if (new_qid.q[i] == inode->ei_qid.q[i]) 176 qtypes &= ~(1U << i); 177 178 if (!qtypes) 179 return 0; 180 181 mutex_lock(&inode->ei_quota_lock); 182 183 ret = bch2_quota_transfer(c, qtypes, new_qid, 184 inode->ei_qid, 185 inode->v.i_blocks + 186 inode->ei_quota_reserved, 187 mode); 188 if (!ret) 189 for (i = 0; i < QTYP_NR; i++) 190 if (qtypes & (1 << i)) 191 inode->ei_qid.q[i] = new_qid.q[i]; 192 193 mutex_unlock(&inode->ei_quota_lock); 194 195 return ret; 196 } 197 198 static u32 bch2_vfs_inode_hash_fn(const void *data, u32 len, u32 seed) 199 { 200 const subvol_inum *inum = data; 201 siphash_key_t k = { .key[0] = seed }; 202 203 return siphash_2u64(inum->subvol, inum->inum, &k); 204 } 205 206 static u32 bch2_vfs_inode_obj_hash_fn(const void *data, u32 len, u32 seed) 207 { 208 const struct bch_inode_info *inode = data; 209 210 return bch2_vfs_inode_hash_fn(&inode->ei_inum, sizeof(inode->ei_inum), seed); 211 } 212 213 static int bch2_vfs_inode_cmp_fn(struct rhashtable_compare_arg *arg, 214 const void *obj) 215 { 216 const struct bch_inode_info *inode = obj; 217 const subvol_inum *v = arg->key; 218 219 return !subvol_inum_eq(inode->ei_inum, *v); 220 } 221 222 static const struct rhashtable_params bch2_vfs_inodes_params = { 223 .head_offset = offsetof(struct bch_inode_info, hash), 224 .key_offset = offsetof(struct bch_inode_info, ei_inum), 225 .key_len = sizeof(subvol_inum), 226 .hashfn = bch2_vfs_inode_hash_fn, 227 .obj_hashfn = bch2_vfs_inode_obj_hash_fn, 228 .obj_cmpfn = bch2_vfs_inode_cmp_fn, 229 .automatic_shrinking = true, 230 }; 231 232 static const struct rhashtable_params bch2_vfs_inodes_by_inum_params = { 233 .head_offset = offsetof(struct bch_inode_info, by_inum_hash), 234 .key_offset = offsetof(struct bch_inode_info, ei_inum.inum), 235 .key_len = sizeof(u64), 236 .automatic_shrinking = true, 237 }; 238 239 int bch2_inode_or_descendents_is_open(struct btree_trans *trans, struct bpos p) 240 { 241 struct bch_fs *c = trans->c; 242 struct rhltable *ht = &c->vfs_inodes_by_inum_table; 243 u64 inum = p.offset; 244 DARRAY(u32) subvols; 245 int ret = 0; 246 247 if (!test_bit(BCH_FS_started, &c->flags)) 248 return false; 249 250 darray_init(&subvols); 251 restart_from_top: 252 253 /* 254 * Tweaked version of __rhashtable_lookup(); we need to get a list of 255 * subvolumes in which the given inode number is open. 256 * 257 * For this to work, we don't include the subvolume ID in the key that 258 * we hash - all inodes with the same inode number regardless of 259 * subvolume will hash to the same slot. 260 * 261 * This will be less than ideal if the same file is ever open 262 * simultaneously in many different snapshots: 263 */ 264 rcu_read_lock(); 265 struct rhash_lock_head __rcu *const *bkt; 266 struct rhash_head *he; 267 unsigned int hash; 268 struct bucket_table *tbl = rht_dereference_rcu(ht->ht.tbl, &ht->ht); 269 restart: 270 hash = rht_key_hashfn(&ht->ht, tbl, &inum, bch2_vfs_inodes_by_inum_params); 271 bkt = rht_bucket(tbl, hash); 272 do { 273 struct bch_inode_info *inode; 274 275 rht_for_each_entry_rcu_from(inode, he, rht_ptr_rcu(bkt), tbl, hash, hash) { 276 if (inode->ei_inum.inum == inum) { 277 ret = darray_push_gfp(&subvols, inode->ei_inum.subvol, 278 GFP_NOWAIT|__GFP_NOWARN); 279 if (ret) { 280 rcu_read_unlock(); 281 ret = darray_make_room(&subvols, 1); 282 if (ret) 283 goto err; 284 subvols.nr = 0; 285 goto restart_from_top; 286 } 287 } 288 } 289 /* An object might have been moved to a different hash chain, 290 * while we walk along it - better check and retry. 291 */ 292 } while (he != RHT_NULLS_MARKER(bkt)); 293 294 /* Ensure we see any new tables. */ 295 smp_rmb(); 296 297 tbl = rht_dereference_rcu(tbl->future_tbl, &ht->ht); 298 if (unlikely(tbl)) 299 goto restart; 300 rcu_read_unlock(); 301 302 darray_for_each(subvols, i) { 303 u32 snap; 304 ret = bch2_subvolume_get_snapshot(trans, *i, &snap); 305 if (ret) 306 goto err; 307 308 ret = bch2_snapshot_is_ancestor(c, snap, p.snapshot); 309 if (ret) 310 break; 311 } 312 err: 313 darray_exit(&subvols); 314 return ret; 315 } 316 317 static struct bch_inode_info *__bch2_inode_hash_find(struct bch_fs *c, subvol_inum inum) 318 { 319 return rhashtable_lookup_fast(&c->vfs_inodes_table, &inum, bch2_vfs_inodes_params); 320 } 321 322 static void __wait_on_freeing_inode(struct bch_fs *c, 323 struct bch_inode_info *inode, 324 subvol_inum inum) 325 { 326 wait_queue_head_t *wq; 327 struct wait_bit_queue_entry wait; 328 329 wq = inode_bit_waitqueue(&wait, &inode->v, __I_NEW); 330 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); 331 spin_unlock(&inode->v.i_lock); 332 333 if (__bch2_inode_hash_find(c, inum) == inode) 334 schedule_timeout(HZ * 10); 335 finish_wait(wq, &wait.wq_entry); 336 } 337 338 static struct bch_inode_info *bch2_inode_hash_find(struct bch_fs *c, struct btree_trans *trans, 339 subvol_inum inum) 340 { 341 struct bch_inode_info *inode; 342 repeat: 343 inode = __bch2_inode_hash_find(c, inum); 344 if (inode) { 345 spin_lock(&inode->v.i_lock); 346 if (!test_bit(EI_INODE_HASHED, &inode->ei_flags)) { 347 spin_unlock(&inode->v.i_lock); 348 return NULL; 349 } 350 if ((inode->v.i_state & (I_FREEING|I_WILL_FREE))) { 351 if (!trans) { 352 __wait_on_freeing_inode(c, inode, inum); 353 } else { 354 int ret = drop_locks_do(trans, 355 (__wait_on_freeing_inode(c, inode, inum), 0)); 356 if (ret) 357 return ERR_PTR(ret); 358 } 359 goto repeat; 360 } 361 __iget(&inode->v); 362 spin_unlock(&inode->v.i_lock); 363 } 364 365 return inode; 366 } 367 368 static void bch2_inode_hash_remove(struct bch_fs *c, struct bch_inode_info *inode) 369 { 370 spin_lock(&inode->v.i_lock); 371 bool remove = test_and_clear_bit(EI_INODE_HASHED, &inode->ei_flags); 372 spin_unlock(&inode->v.i_lock); 373 374 if (remove) { 375 int ret = rhltable_remove(&c->vfs_inodes_by_inum_table, 376 &inode->by_inum_hash, bch2_vfs_inodes_by_inum_params); 377 BUG_ON(ret); 378 379 ret = rhashtable_remove_fast(&c->vfs_inodes_table, 380 &inode->hash, bch2_vfs_inodes_params); 381 BUG_ON(ret); 382 inode->v.i_hash.pprev = NULL; 383 /* 384 * This pairs with the bch2_inode_hash_find() -> 385 * __wait_on_freeing_inode() path 386 */ 387 inode_wake_up_bit(&inode->v, __I_NEW); 388 } 389 } 390 391 static struct bch_inode_info *bch2_inode_hash_insert(struct bch_fs *c, 392 struct btree_trans *trans, 393 struct bch_inode_info *inode) 394 { 395 struct bch_inode_info *old = inode; 396 397 set_bit(EI_INODE_HASHED, &inode->ei_flags); 398 retry: 399 if (unlikely(rhashtable_lookup_insert_key(&c->vfs_inodes_table, 400 &inode->ei_inum, 401 &inode->hash, 402 bch2_vfs_inodes_params))) { 403 old = bch2_inode_hash_find(c, trans, inode->ei_inum); 404 if (!old) 405 goto retry; 406 407 clear_bit(EI_INODE_HASHED, &inode->ei_flags); 408 409 /* 410 * bcachefs doesn't use I_NEW; we have no use for it since we 411 * only insert fully created inodes in the inode hash table. But 412 * discard_new_inode() expects it to be set... 413 */ 414 inode->v.i_state |= I_NEW; 415 /* 416 * We don't want bch2_evict_inode() to delete the inode on disk, 417 * we just raced and had another inode in cache. Normally new 418 * inodes don't have nlink == 0 - except tmpfiles do... 419 */ 420 set_nlink(&inode->v, 1); 421 discard_new_inode(&inode->v); 422 return old; 423 } else { 424 int ret = rhltable_insert(&c->vfs_inodes_by_inum_table, 425 &inode->by_inum_hash, 426 bch2_vfs_inodes_by_inum_params); 427 BUG_ON(ret); 428 429 inode_fake_hash(&inode->v); 430 431 inode_sb_list_add(&inode->v); 432 433 mutex_lock(&c->vfs_inodes_lock); 434 list_add(&inode->ei_vfs_inode_list, &c->vfs_inodes_list); 435 mutex_unlock(&c->vfs_inodes_lock); 436 return inode; 437 } 438 } 439 440 #define memalloc_flags_do(_flags, _do) \ 441 ({ \ 442 unsigned _saved_flags = memalloc_flags_save(_flags); \ 443 typeof(_do) _ret = _do; \ 444 memalloc_noreclaim_restore(_saved_flags); \ 445 _ret; \ 446 }) 447 448 static struct inode *bch2_alloc_inode(struct super_block *sb) 449 { 450 BUG(); 451 } 452 453 static struct bch_inode_info *__bch2_new_inode(struct bch_fs *c, gfp_t gfp) 454 { 455 struct bch_inode_info *inode = alloc_inode_sb(c->vfs_sb, 456 bch2_inode_cache, gfp); 457 if (!inode) 458 return NULL; 459 460 inode_init_once(&inode->v); 461 mutex_init(&inode->ei_update_lock); 462 two_state_lock_init(&inode->ei_pagecache_lock); 463 INIT_LIST_HEAD(&inode->ei_vfs_inode_list); 464 inode->ei_flags = 0; 465 mutex_init(&inode->ei_quota_lock); 466 memset(&inode->ei_devs_need_flush, 0, sizeof(inode->ei_devs_need_flush)); 467 468 if (unlikely(inode_init_always_gfp(c->vfs_sb, &inode->v, gfp))) { 469 kmem_cache_free(bch2_inode_cache, inode); 470 return NULL; 471 } 472 473 return inode; 474 } 475 476 /* 477 * Allocate a new inode, dropping/retaking btree locks if necessary: 478 */ 479 static struct bch_inode_info *bch2_new_inode(struct btree_trans *trans) 480 { 481 struct bch_inode_info *inode = __bch2_new_inode(trans->c, GFP_NOWAIT); 482 483 if (unlikely(!inode)) { 484 int ret = drop_locks_do(trans, (inode = __bch2_new_inode(trans->c, GFP_NOFS)) ? 0 : -ENOMEM); 485 if (ret && inode) { 486 __destroy_inode(&inode->v); 487 kmem_cache_free(bch2_inode_cache, inode); 488 } 489 if (ret) 490 return ERR_PTR(ret); 491 } 492 493 return inode; 494 } 495 496 static struct bch_inode_info *bch2_inode_hash_init_insert(struct btree_trans *trans, 497 subvol_inum inum, 498 struct bch_inode_unpacked *bi, 499 struct bch_subvolume *subvol) 500 { 501 struct bch_inode_info *inode = bch2_new_inode(trans); 502 if (IS_ERR(inode)) 503 return inode; 504 505 bch2_vfs_inode_init(trans, inum, inode, bi, subvol); 506 507 return bch2_inode_hash_insert(trans->c, trans, inode); 508 509 } 510 511 struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum) 512 { 513 struct bch_inode_info *inode = bch2_inode_hash_find(c, NULL, inum); 514 if (inode) 515 return &inode->v; 516 517 struct btree_trans *trans = bch2_trans_get(c); 518 519 struct bch_inode_unpacked inode_u; 520 struct bch_subvolume subvol; 521 int ret = lockrestart_do(trans, 522 bch2_subvolume_get(trans, inum.subvol, true, &subvol) ?: 523 bch2_inode_find_by_inum_trans(trans, inum, &inode_u)) ?: 524 PTR_ERR_OR_ZERO(inode = bch2_inode_hash_init_insert(trans, inum, &inode_u, &subvol)); 525 bch2_trans_put(trans); 526 527 return ret ? ERR_PTR(ret) : &inode->v; 528 } 529 530 struct bch_inode_info * 531 __bch2_create(struct mnt_idmap *idmap, 532 struct bch_inode_info *dir, struct dentry *dentry, 533 umode_t mode, dev_t rdev, subvol_inum snapshot_src, 534 unsigned flags) 535 { 536 struct bch_fs *c = dir->v.i_sb->s_fs_info; 537 struct btree_trans *trans; 538 struct bch_inode_unpacked dir_u; 539 struct bch_inode_info *inode; 540 struct bch_inode_unpacked inode_u; 541 struct posix_acl *default_acl = NULL, *acl = NULL; 542 subvol_inum inum; 543 struct bch_subvolume subvol; 544 u64 journal_seq = 0; 545 kuid_t kuid; 546 kgid_t kgid; 547 int ret; 548 549 /* 550 * preallocate acls + vfs inode before btree transaction, so that 551 * nothing can fail after the transaction succeeds: 552 */ 553 #ifdef CONFIG_BCACHEFS_POSIX_ACL 554 ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl); 555 if (ret) 556 return ERR_PTR(ret); 557 #endif 558 inode = __bch2_new_inode(c, GFP_NOFS); 559 if (unlikely(!inode)) { 560 inode = ERR_PTR(-ENOMEM); 561 goto err; 562 } 563 564 bch2_inode_init_early(c, &inode_u); 565 566 if (!(flags & BCH_CREATE_TMPFILE)) 567 mutex_lock(&dir->ei_update_lock); 568 569 trans = bch2_trans_get(c); 570 retry: 571 bch2_trans_begin(trans); 572 573 kuid = mapped_fsuid(idmap, i_user_ns(&dir->v)); 574 kgid = mapped_fsgid(idmap, i_user_ns(&dir->v)); 575 ret = bch2_subvol_is_ro_trans(trans, dir->ei_inum.subvol) ?: 576 bch2_create_trans(trans, 577 inode_inum(dir), &dir_u, &inode_u, 578 !(flags & BCH_CREATE_TMPFILE) 579 ? &dentry->d_name : NULL, 580 from_kuid(i_user_ns(&dir->v), kuid), 581 from_kgid(i_user_ns(&dir->v), kgid), 582 mode, rdev, 583 default_acl, acl, snapshot_src, flags) ?: 584 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1, 585 KEY_TYPE_QUOTA_PREALLOC); 586 if (unlikely(ret)) 587 goto err_before_quota; 588 589 inum.subvol = inode_u.bi_subvol ?: dir->ei_inum.subvol; 590 inum.inum = inode_u.bi_inum; 591 592 ret = bch2_subvolume_get(trans, inum.subvol, true, &subvol) ?: 593 bch2_trans_commit(trans, NULL, &journal_seq, 0); 594 if (unlikely(ret)) { 595 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1, 596 KEY_TYPE_QUOTA_WARN); 597 err_before_quota: 598 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 599 goto retry; 600 goto err_trans; 601 } 602 603 if (!(flags & BCH_CREATE_TMPFILE)) { 604 bch2_inode_update_after_write(trans, dir, &dir_u, 605 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE); 606 mutex_unlock(&dir->ei_update_lock); 607 } 608 609 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol); 610 611 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl); 612 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl); 613 614 /* 615 * we must insert the new inode into the inode cache before calling 616 * bch2_trans_exit() and dropping locks, else we could race with another 617 * thread pulling the inode in and modifying it: 618 * 619 * also, calling bch2_inode_hash_insert() without passing in the 620 * transaction object is sketchy - if we could ever end up in 621 * __wait_on_freeing_inode(), we'd risk deadlock. 622 * 623 * But that shouldn't be possible, since we still have the inode locked 624 * that we just created, and we _really_ can't take a transaction 625 * restart here. 626 */ 627 inode = bch2_inode_hash_insert(c, NULL, inode); 628 bch2_trans_put(trans); 629 err: 630 posix_acl_release(default_acl); 631 posix_acl_release(acl); 632 return inode; 633 err_trans: 634 if (!(flags & BCH_CREATE_TMPFILE)) 635 mutex_unlock(&dir->ei_update_lock); 636 637 bch2_trans_put(trans); 638 make_bad_inode(&inode->v); 639 iput(&inode->v); 640 inode = ERR_PTR(ret); 641 goto err; 642 } 643 644 /* methods */ 645 646 static struct bch_inode_info *bch2_lookup_trans(struct btree_trans *trans, 647 subvol_inum dir, struct bch_hash_info *dir_hash_info, 648 const struct qstr *name) 649 { 650 struct bch_fs *c = trans->c; 651 subvol_inum inum = {}; 652 struct printbuf buf = PRINTBUF; 653 654 struct qstr lookup_name; 655 int ret = bch2_maybe_casefold(trans, dir_hash_info, name, &lookup_name); 656 if (ret) 657 return ERR_PTR(ret); 658 659 struct btree_iter dirent_iter = {}; 660 struct bkey_s_c k = bch2_hash_lookup(trans, &dirent_iter, bch2_dirent_hash_desc, 661 dir_hash_info, dir, &lookup_name, 0); 662 ret = bkey_err(k); 663 if (ret) 664 return ERR_PTR(ret); 665 666 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k); 667 668 ret = bch2_dirent_read_target(trans, dir, d, &inum); 669 if (ret > 0) 670 ret = -ENOENT; 671 if (ret) 672 goto err; 673 674 struct bch_inode_info *inode = bch2_inode_hash_find(c, trans, inum); 675 if (inode) 676 goto out; 677 678 /* 679 * Note: if check/repair needs it, we commit before 680 * bch2_inode_hash_init_insert(), as after that point we can't take a 681 * restart - not in the top level loop with a commit_do(), like we 682 * usually do: 683 */ 684 685 struct bch_subvolume subvol; 686 struct bch_inode_unpacked inode_u; 687 ret = bch2_subvolume_get(trans, inum.subvol, true, &subvol) ?: 688 bch2_inode_find_by_inum_nowarn_trans(trans, inum, &inode_u) ?: 689 bch2_check_dirent_target(trans, &dirent_iter, d, &inode_u, false) ?: 690 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc) ?: 691 PTR_ERR_OR_ZERO(inode = bch2_inode_hash_init_insert(trans, inum, &inode_u, &subvol)); 692 693 /* 694 * don't remove it: check_inodes might find another inode that points 695 * back to this dirent 696 */ 697 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), 698 c, "dirent to missing inode:\n%s", 699 (bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf)); 700 if (ret) 701 goto err; 702 out: 703 bch2_trans_iter_exit(trans, &dirent_iter); 704 printbuf_exit(&buf); 705 return inode; 706 err: 707 inode = ERR_PTR(ret); 708 goto out; 709 } 710 711 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry, 712 unsigned int flags) 713 { 714 struct bch_fs *c = vdir->i_sb->s_fs_info; 715 struct bch_inode_info *dir = to_bch_ei(vdir); 716 struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode); 717 718 struct bch_inode_info *inode; 719 bch2_trans_do(c, 720 PTR_ERR_OR_ZERO(inode = bch2_lookup_trans(trans, inode_inum(dir), 721 &hash, &dentry->d_name))); 722 if (IS_ERR(inode)) 723 inode = NULL; 724 725 #ifdef CONFIG_UNICODE 726 if (!inode && IS_CASEFOLDED(vdir)) { 727 /* 728 * Do not cache a negative dentry in casefolded directories 729 * as it would need to be invalidated in the following situation: 730 * - Lookup file "blAH" in a casefolded directory 731 * - Creation of file "BLAH" in a casefolded directory 732 * - Lookup file "blAH" in a casefolded directory 733 * which would fail if we had a negative dentry. 734 * 735 * We should come back to this when VFS has a method to handle 736 * this edgecase. 737 */ 738 return NULL; 739 } 740 #endif 741 742 return d_splice_alias(&inode->v, dentry); 743 } 744 745 static int bch2_mknod(struct mnt_idmap *idmap, 746 struct inode *vdir, struct dentry *dentry, 747 umode_t mode, dev_t rdev) 748 { 749 struct bch_inode_info *inode = 750 __bch2_create(idmap, to_bch_ei(vdir), dentry, mode, rdev, 751 (subvol_inum) { 0 }, 0); 752 753 if (IS_ERR(inode)) 754 return bch2_err_class(PTR_ERR(inode)); 755 756 d_instantiate(dentry, &inode->v); 757 return 0; 758 } 759 760 static int bch2_create(struct mnt_idmap *idmap, 761 struct inode *vdir, struct dentry *dentry, 762 umode_t mode, bool excl) 763 { 764 return bch2_mknod(idmap, vdir, dentry, mode|S_IFREG, 0); 765 } 766 767 static int __bch2_link(struct bch_fs *c, 768 struct bch_inode_info *inode, 769 struct bch_inode_info *dir, 770 struct dentry *dentry) 771 { 772 struct bch_inode_unpacked dir_u, inode_u; 773 int ret; 774 775 mutex_lock(&inode->ei_update_lock); 776 struct btree_trans *trans = bch2_trans_get(c); 777 778 ret = commit_do(trans, NULL, NULL, 0, 779 bch2_link_trans(trans, 780 inode_inum(dir), &dir_u, 781 inode_inum(inode), &inode_u, 782 &dentry->d_name)); 783 784 if (likely(!ret)) { 785 bch2_inode_update_after_write(trans, dir, &dir_u, 786 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE); 787 bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME); 788 } 789 790 bch2_trans_put(trans); 791 mutex_unlock(&inode->ei_update_lock); 792 return ret; 793 } 794 795 static int bch2_link(struct dentry *old_dentry, struct inode *vdir, 796 struct dentry *dentry) 797 { 798 struct bch_fs *c = vdir->i_sb->s_fs_info; 799 struct bch_inode_info *dir = to_bch_ei(vdir); 800 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode); 801 int ret; 802 803 lockdep_assert_held(&inode->v.i_rwsem); 804 805 ret = bch2_subvol_is_ro(c, dir->ei_inum.subvol) ?: 806 bch2_subvol_is_ro(c, inode->ei_inum.subvol) ?: 807 __bch2_link(c, inode, dir, dentry); 808 if (unlikely(ret)) 809 return bch2_err_class(ret); 810 811 ihold(&inode->v); 812 d_instantiate(dentry, &inode->v); 813 return 0; 814 } 815 816 int __bch2_unlink(struct inode *vdir, struct dentry *dentry, 817 bool deleting_snapshot) 818 { 819 struct bch_fs *c = vdir->i_sb->s_fs_info; 820 struct bch_inode_info *dir = to_bch_ei(vdir); 821 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode); 822 struct bch_inode_unpacked dir_u, inode_u; 823 int ret; 824 825 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode); 826 827 struct btree_trans *trans = bch2_trans_get(c); 828 829 ret = commit_do(trans, NULL, NULL, 830 BCH_TRANS_COMMIT_no_enospc, 831 bch2_unlink_trans(trans, 832 inode_inum(dir), &dir_u, 833 &inode_u, &dentry->d_name, 834 deleting_snapshot)); 835 if (unlikely(ret)) 836 goto err; 837 838 bch2_inode_update_after_write(trans, dir, &dir_u, 839 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE); 840 bch2_inode_update_after_write(trans, inode, &inode_u, 841 ATTR_MTIME); 842 843 if (inode_u.bi_subvol) { 844 /* 845 * Subvolume deletion is asynchronous, but we still want to tell 846 * the VFS that it's been deleted here: 847 */ 848 set_nlink(&inode->v, 0); 849 } 850 851 if (IS_CASEFOLDED(vdir)) 852 d_invalidate(dentry); 853 err: 854 bch2_trans_put(trans); 855 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode); 856 857 return ret; 858 } 859 860 static int bch2_unlink(struct inode *vdir, struct dentry *dentry) 861 { 862 struct bch_inode_info *dir= to_bch_ei(vdir); 863 struct bch_fs *c = dir->v.i_sb->s_fs_info; 864 865 int ret = bch2_subvol_is_ro(c, dir->ei_inum.subvol) ?: 866 __bch2_unlink(vdir, dentry, false); 867 return bch2_err_class(ret); 868 } 869 870 static int bch2_symlink(struct mnt_idmap *idmap, 871 struct inode *vdir, struct dentry *dentry, 872 const char *symname) 873 { 874 struct bch_fs *c = vdir->i_sb->s_fs_info; 875 struct bch_inode_info *dir = to_bch_ei(vdir), *inode; 876 int ret; 877 878 inode = __bch2_create(idmap, dir, dentry, S_IFLNK|S_IRWXUGO, 0, 879 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE); 880 if (IS_ERR(inode)) 881 return bch2_err_class(PTR_ERR(inode)); 882 883 inode_lock(&inode->v); 884 ret = page_symlink(&inode->v, symname, strlen(symname) + 1); 885 inode_unlock(&inode->v); 886 887 if (unlikely(ret)) 888 goto err; 889 890 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX); 891 if (unlikely(ret)) 892 goto err; 893 894 ret = __bch2_link(c, inode, dir, dentry); 895 if (unlikely(ret)) 896 goto err; 897 898 d_instantiate(dentry, &inode->v); 899 return 0; 900 err: 901 iput(&inode->v); 902 return bch2_err_class(ret); 903 } 904 905 static struct dentry *bch2_mkdir(struct mnt_idmap *idmap, 906 struct inode *vdir, struct dentry *dentry, umode_t mode) 907 { 908 return ERR_PTR(bch2_mknod(idmap, vdir, dentry, mode|S_IFDIR, 0)); 909 } 910 911 static int bch2_rename2(struct mnt_idmap *idmap, 912 struct inode *src_vdir, struct dentry *src_dentry, 913 struct inode *dst_vdir, struct dentry *dst_dentry, 914 unsigned flags) 915 { 916 struct bch_fs *c = src_vdir->i_sb->s_fs_info; 917 struct bch_inode_info *src_dir = to_bch_ei(src_vdir); 918 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir); 919 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode); 920 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode); 921 struct bch_inode_unpacked dst_dir_u, src_dir_u; 922 struct bch_inode_unpacked src_inode_u, dst_inode_u, *whiteout_inode_u; 923 struct btree_trans *trans; 924 enum bch_rename_mode mode = flags & RENAME_EXCHANGE 925 ? BCH_RENAME_EXCHANGE 926 : dst_dentry->d_inode 927 ? BCH_RENAME_OVERWRITE : BCH_RENAME; 928 bool whiteout = !!(flags & RENAME_WHITEOUT); 929 int ret; 930 931 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE|RENAME_WHITEOUT)) 932 return -EINVAL; 933 934 if (mode == BCH_RENAME_OVERWRITE) { 935 ret = filemap_write_and_wait_range(src_inode->v.i_mapping, 936 0, LLONG_MAX); 937 if (ret) 938 return ret; 939 } 940 941 bch2_lock_inodes(INODE_UPDATE_LOCK, 942 src_dir, 943 dst_dir, 944 src_inode, 945 dst_inode); 946 947 trans = bch2_trans_get(c); 948 949 ret = bch2_subvol_is_ro_trans(trans, src_dir->ei_inum.subvol) ?: 950 bch2_subvol_is_ro_trans(trans, dst_dir->ei_inum.subvol); 951 if (ret) 952 goto err_tx_restart; 953 954 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) { 955 ret = bch2_fs_quota_transfer(c, src_inode, 956 dst_dir->ei_qid, 957 1 << QTYP_PRJ, 958 KEY_TYPE_QUOTA_PREALLOC); 959 if (ret) 960 goto err; 961 } 962 963 if (mode == BCH_RENAME_EXCHANGE && 964 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) { 965 ret = bch2_fs_quota_transfer(c, dst_inode, 966 src_dir->ei_qid, 967 1 << QTYP_PRJ, 968 KEY_TYPE_QUOTA_PREALLOC); 969 if (ret) 970 goto err; 971 } 972 retry: 973 bch2_trans_begin(trans); 974 975 ret = bch2_rename_trans(trans, 976 inode_inum(src_dir), &src_dir_u, 977 inode_inum(dst_dir), &dst_dir_u, 978 &src_inode_u, 979 &dst_inode_u, 980 &src_dentry->d_name, 981 &dst_dentry->d_name, 982 mode); 983 if (unlikely(ret)) 984 goto err_tx_restart; 985 986 if (whiteout) { 987 whiteout_inode_u = bch2_trans_kmalloc_nomemzero(trans, sizeof(*whiteout_inode_u)); 988 ret = PTR_ERR_OR_ZERO(whiteout_inode_u); 989 if (unlikely(ret)) 990 goto err_tx_restart; 991 bch2_inode_init_early(c, whiteout_inode_u); 992 993 ret = bch2_create_trans(trans, 994 inode_inum(src_dir), &src_dir_u, 995 whiteout_inode_u, 996 &src_dentry->d_name, 997 from_kuid(i_user_ns(&src_dir->v), current_fsuid()), 998 from_kgid(i_user_ns(&src_dir->v), current_fsgid()), 999 S_IFCHR|WHITEOUT_MODE, 0, 1000 NULL, NULL, (subvol_inum) { 0 }, 0) ?: 1001 bch2_quota_acct(c, bch_qid(whiteout_inode_u), Q_INO, 1, 1002 KEY_TYPE_QUOTA_PREALLOC); 1003 if (unlikely(ret)) 1004 goto err_tx_restart; 1005 } 1006 1007 ret = bch2_trans_commit(trans, NULL, NULL, 0); 1008 if (unlikely(ret)) { 1009 err_tx_restart: 1010 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 1011 goto retry; 1012 goto err; 1013 } 1014 1015 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum); 1016 BUG_ON(dst_inode && 1017 dst_inode->v.i_ino != dst_inode_u.bi_inum); 1018 1019 bch2_inode_update_after_write(trans, src_dir, &src_dir_u, 1020 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE); 1021 1022 if (src_dir != dst_dir) 1023 bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u, 1024 ATTR_MTIME|ATTR_CTIME|ATTR_SIZE); 1025 1026 bch2_inode_update_after_write(trans, src_inode, &src_inode_u, 1027 ATTR_CTIME); 1028 1029 if (dst_inode) 1030 bch2_inode_update_after_write(trans, dst_inode, &dst_inode_u, 1031 ATTR_CTIME); 1032 err: 1033 bch2_trans_put(trans); 1034 1035 bch2_fs_quota_transfer(c, src_inode, 1036 bch_qid(&src_inode->ei_inode), 1037 1 << QTYP_PRJ, 1038 KEY_TYPE_QUOTA_NOCHECK); 1039 if (dst_inode) 1040 bch2_fs_quota_transfer(c, dst_inode, 1041 bch_qid(&dst_inode->ei_inode), 1042 1 << QTYP_PRJ, 1043 KEY_TYPE_QUOTA_NOCHECK); 1044 1045 bch2_unlock_inodes(INODE_UPDATE_LOCK, 1046 src_dir, 1047 dst_dir, 1048 src_inode, 1049 dst_inode); 1050 1051 return bch2_err_class(ret); 1052 } 1053 1054 static void bch2_setattr_copy(struct mnt_idmap *idmap, 1055 struct bch_inode_info *inode, 1056 struct bch_inode_unpacked *bi, 1057 struct iattr *attr) 1058 { 1059 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1060 unsigned int ia_valid = attr->ia_valid; 1061 kuid_t kuid; 1062 kgid_t kgid; 1063 1064 if (ia_valid & ATTR_UID) { 1065 kuid = from_vfsuid(idmap, i_user_ns(&inode->v), attr->ia_vfsuid); 1066 bi->bi_uid = from_kuid(i_user_ns(&inode->v), kuid); 1067 } 1068 if (ia_valid & ATTR_GID) { 1069 kgid = from_vfsgid(idmap, i_user_ns(&inode->v), attr->ia_vfsgid); 1070 bi->bi_gid = from_kgid(i_user_ns(&inode->v), kgid); 1071 } 1072 1073 if (ia_valid & ATTR_SIZE) 1074 bi->bi_size = attr->ia_size; 1075 1076 if (ia_valid & ATTR_ATIME) 1077 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime); 1078 if (ia_valid & ATTR_MTIME) 1079 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime); 1080 if (ia_valid & ATTR_CTIME) 1081 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime); 1082 1083 if (ia_valid & ATTR_MODE) { 1084 umode_t mode = attr->ia_mode; 1085 kgid_t gid = ia_valid & ATTR_GID 1086 ? kgid 1087 : inode->v.i_gid; 1088 1089 if (!in_group_or_capable(idmap, &inode->v, 1090 make_vfsgid(idmap, i_user_ns(&inode->v), gid))) 1091 mode &= ~S_ISGID; 1092 bi->bi_mode = mode; 1093 } 1094 } 1095 1096 int bch2_setattr_nonsize(struct mnt_idmap *idmap, 1097 struct bch_inode_info *inode, 1098 struct iattr *attr) 1099 { 1100 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1101 struct bch_qid qid; 1102 struct btree_trans *trans; 1103 struct btree_iter inode_iter = {}; 1104 struct bch_inode_unpacked inode_u; 1105 struct posix_acl *acl = NULL; 1106 kuid_t kuid; 1107 kgid_t kgid; 1108 int ret; 1109 1110 mutex_lock(&inode->ei_update_lock); 1111 1112 qid = inode->ei_qid; 1113 1114 if (attr->ia_valid & ATTR_UID) { 1115 kuid = from_vfsuid(idmap, i_user_ns(&inode->v), attr->ia_vfsuid); 1116 qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), kuid); 1117 } 1118 1119 if (attr->ia_valid & ATTR_GID) { 1120 kgid = from_vfsgid(idmap, i_user_ns(&inode->v), attr->ia_vfsgid); 1121 qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), kgid); 1122 } 1123 1124 ret = bch2_fs_quota_transfer(c, inode, qid, ~0, 1125 KEY_TYPE_QUOTA_PREALLOC); 1126 if (ret) 1127 goto err; 1128 1129 trans = bch2_trans_get(c); 1130 retry: 1131 bch2_trans_begin(trans); 1132 kfree(acl); 1133 acl = NULL; 1134 1135 ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inode_inum(inode), 1136 BTREE_ITER_intent); 1137 if (ret) 1138 goto btree_err; 1139 1140 bch2_setattr_copy(idmap, inode, &inode_u, attr); 1141 1142 if (attr->ia_valid & ATTR_MODE) { 1143 ret = bch2_acl_chmod(trans, inode_inum(inode), &inode_u, 1144 inode_u.bi_mode, &acl); 1145 if (ret) 1146 goto btree_err; 1147 } 1148 1149 ret = bch2_inode_write(trans, &inode_iter, &inode_u) ?: 1150 bch2_trans_commit(trans, NULL, NULL, 1151 BCH_TRANS_COMMIT_no_enospc); 1152 btree_err: 1153 bch2_trans_iter_exit(trans, &inode_iter); 1154 1155 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 1156 goto retry; 1157 if (unlikely(ret)) 1158 goto err_trans; 1159 1160 bch2_inode_update_after_write(trans, inode, &inode_u, attr->ia_valid); 1161 1162 if (acl) 1163 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl); 1164 err_trans: 1165 bch2_trans_put(trans); 1166 err: 1167 mutex_unlock(&inode->ei_update_lock); 1168 1169 return bch2_err_class(ret); 1170 } 1171 1172 static int bch2_getattr(struct mnt_idmap *idmap, 1173 const struct path *path, struct kstat *stat, 1174 u32 request_mask, unsigned query_flags) 1175 { 1176 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry)); 1177 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1178 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, &inode->v); 1179 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, &inode->v); 1180 1181 stat->dev = inode->v.i_sb->s_dev; 1182 stat->ino = inode->v.i_ino; 1183 stat->mode = inode->v.i_mode; 1184 stat->nlink = inode->v.i_nlink; 1185 stat->uid = vfsuid_into_kuid(vfsuid); 1186 stat->gid = vfsgid_into_kgid(vfsgid); 1187 stat->rdev = inode->v.i_rdev; 1188 stat->size = i_size_read(&inode->v); 1189 stat->atime = inode_get_atime(&inode->v); 1190 stat->mtime = inode_get_mtime(&inode->v); 1191 stat->ctime = inode_get_ctime(&inode->v); 1192 stat->blksize = block_bytes(c); 1193 stat->blocks = inode->v.i_blocks; 1194 1195 stat->subvol = inode->ei_inum.subvol; 1196 stat->result_mask |= STATX_SUBVOL; 1197 1198 if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->v.i_mode)) { 1199 stat->result_mask |= STATX_DIOALIGN; 1200 /* 1201 * this is incorrect; we should be tracking this in superblock, 1202 * and checking the alignment of open devices 1203 */ 1204 stat->dio_mem_align = SECTOR_SIZE; 1205 stat->dio_offset_align = block_bytes(c); 1206 } 1207 1208 if (request_mask & STATX_BTIME) { 1209 stat->result_mask |= STATX_BTIME; 1210 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime); 1211 } 1212 1213 if (inode->ei_inode.bi_flags & BCH_INODE_immutable) 1214 stat->attributes |= STATX_ATTR_IMMUTABLE; 1215 stat->attributes_mask |= STATX_ATTR_IMMUTABLE; 1216 1217 if (inode->ei_inode.bi_flags & BCH_INODE_append) 1218 stat->attributes |= STATX_ATTR_APPEND; 1219 stat->attributes_mask |= STATX_ATTR_APPEND; 1220 1221 if (inode->ei_inode.bi_flags & BCH_INODE_nodump) 1222 stat->attributes |= STATX_ATTR_NODUMP; 1223 stat->attributes_mask |= STATX_ATTR_NODUMP; 1224 1225 return 0; 1226 } 1227 1228 static int bch2_setattr(struct mnt_idmap *idmap, 1229 struct dentry *dentry, struct iattr *iattr) 1230 { 1231 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode); 1232 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1233 int ret; 1234 1235 lockdep_assert_held(&inode->v.i_rwsem); 1236 1237 ret = bch2_subvol_is_ro(c, inode->ei_inum.subvol) ?: 1238 setattr_prepare(idmap, dentry, iattr); 1239 if (ret) 1240 return ret; 1241 1242 return iattr->ia_valid & ATTR_SIZE 1243 ? bchfs_truncate(idmap, inode, iattr) 1244 : bch2_setattr_nonsize(idmap, inode, iattr); 1245 } 1246 1247 static int bch2_tmpfile(struct mnt_idmap *idmap, 1248 struct inode *vdir, struct file *file, umode_t mode) 1249 { 1250 struct bch_inode_info *inode = 1251 __bch2_create(idmap, to_bch_ei(vdir), 1252 file->f_path.dentry, mode, 0, 1253 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE); 1254 1255 if (IS_ERR(inode)) 1256 return bch2_err_class(PTR_ERR(inode)); 1257 1258 d_mark_tmpfile(file, &inode->v); 1259 d_instantiate(file->f_path.dentry, &inode->v); 1260 return finish_open_simple(file, 0); 1261 } 1262 1263 struct bch_fiemap_extent { 1264 struct bkey_buf kbuf; 1265 unsigned flags; 1266 }; 1267 1268 static int bch2_fill_extent(struct bch_fs *c, 1269 struct fiemap_extent_info *info, 1270 struct bch_fiemap_extent *fe) 1271 { 1272 struct bkey_s_c k = bkey_i_to_s_c(fe->kbuf.k); 1273 unsigned flags = fe->flags; 1274 1275 BUG_ON(!k.k->size); 1276 1277 if (bkey_extent_is_direct_data(k.k)) { 1278 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); 1279 const union bch_extent_entry *entry; 1280 struct extent_ptr_decoded p; 1281 int ret; 1282 1283 if (k.k->type == KEY_TYPE_reflink_v) 1284 flags |= FIEMAP_EXTENT_SHARED; 1285 1286 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) { 1287 int flags2 = 0; 1288 u64 offset = p.ptr.offset; 1289 1290 if (p.ptr.unwritten) 1291 flags2 |= FIEMAP_EXTENT_UNWRITTEN; 1292 1293 if (p.crc.compression_type) 1294 flags2 |= FIEMAP_EXTENT_ENCODED; 1295 else 1296 offset += p.crc.offset; 1297 1298 if ((offset & (block_sectors(c) - 1)) || 1299 (k.k->size & (block_sectors(c) - 1))) 1300 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED; 1301 1302 ret = fiemap_fill_next_extent(info, 1303 bkey_start_offset(k.k) << 9, 1304 offset << 9, 1305 k.k->size << 9, flags|flags2); 1306 if (ret) 1307 return ret; 1308 } 1309 1310 return 0; 1311 } else if (bkey_extent_is_inline_data(k.k)) { 1312 return fiemap_fill_next_extent(info, 1313 bkey_start_offset(k.k) << 9, 1314 0, k.k->size << 9, 1315 flags| 1316 FIEMAP_EXTENT_DATA_INLINE); 1317 } else if (k.k->type == KEY_TYPE_reservation) { 1318 return fiemap_fill_next_extent(info, 1319 bkey_start_offset(k.k) << 9, 1320 0, k.k->size << 9, 1321 flags| 1322 FIEMAP_EXTENT_DELALLOC| 1323 FIEMAP_EXTENT_UNWRITTEN); 1324 } else { 1325 BUG(); 1326 } 1327 } 1328 1329 /* 1330 * Scan a range of an inode for data in pagecache. 1331 * 1332 * Intended to be retryable, so don't modify the output params until success is 1333 * imminent. 1334 */ 1335 static int 1336 bch2_fiemap_hole_pagecache(struct inode *vinode, u64 *start, u64 *end, 1337 bool nonblock) 1338 { 1339 loff_t dstart, dend; 1340 1341 dstart = bch2_seek_pagecache_data(vinode, *start, *end, 0, nonblock); 1342 if (dstart < 0) 1343 return dstart; 1344 1345 if (dstart == *end) { 1346 *start = dstart; 1347 return 0; 1348 } 1349 1350 dend = bch2_seek_pagecache_hole(vinode, dstart, *end, 0, nonblock); 1351 if (dend < 0) 1352 return dend; 1353 1354 /* race */ 1355 BUG_ON(dstart == dend); 1356 1357 *start = dstart; 1358 *end = dend; 1359 return 0; 1360 } 1361 1362 /* 1363 * Scan a range of pagecache that corresponds to a file mapping hole in the 1364 * extent btree. If data is found, fake up an extent key so it looks like a 1365 * delalloc extent to the rest of the fiemap processing code. 1366 */ 1367 static int 1368 bch2_next_fiemap_pagecache_extent(struct btree_trans *trans, struct bch_inode_info *inode, 1369 u64 start, u64 end, struct bch_fiemap_extent *cur) 1370 { 1371 struct bch_fs *c = trans->c; 1372 struct bkey_i_extent *delextent; 1373 struct bch_extent_ptr ptr = {}; 1374 loff_t dstart = start << 9, dend = end << 9; 1375 int ret; 1376 1377 /* 1378 * We hold btree locks here so we cannot block on folio locks without 1379 * dropping trans locks first. Run a nonblocking scan for the common 1380 * case of no folios over holes and fall back on failure. 1381 * 1382 * Note that dropping locks like this is technically racy against 1383 * writeback inserting to the extent tree, but a non-sync fiemap scan is 1384 * fundamentally racy with writeback anyways. Therefore, just report the 1385 * range as delalloc regardless of whether we have to cycle trans locks. 1386 */ 1387 ret = bch2_fiemap_hole_pagecache(&inode->v, &dstart, &dend, true); 1388 if (ret == -EAGAIN) 1389 ret = drop_locks_do(trans, 1390 bch2_fiemap_hole_pagecache(&inode->v, &dstart, &dend, false)); 1391 if (ret < 0) 1392 return ret; 1393 1394 /* 1395 * Create a fake extent key in the buffer. We have to add a dummy extent 1396 * pointer for the fill code to add an extent entry. It's explicitly 1397 * zeroed to reflect delayed allocation (i.e. phys offset 0). 1398 */ 1399 bch2_bkey_buf_realloc(&cur->kbuf, c, sizeof(*delextent) / sizeof(u64)); 1400 delextent = bkey_extent_init(cur->kbuf.k); 1401 delextent->k.p = POS(inode->ei_inum.inum, dend >> 9); 1402 delextent->k.size = (dend - dstart) >> 9; 1403 bch2_bkey_append_ptr(&delextent->k_i, ptr); 1404 1405 cur->flags = FIEMAP_EXTENT_DELALLOC; 1406 1407 return 0; 1408 } 1409 1410 static int bch2_next_fiemap_extent(struct btree_trans *trans, 1411 struct bch_inode_info *inode, 1412 u64 start, u64 end, 1413 struct bch_fiemap_extent *cur) 1414 { 1415 u32 snapshot; 1416 int ret = bch2_subvolume_get_snapshot(trans, inode->ei_inum.subvol, &snapshot); 1417 if (ret) 1418 return ret; 1419 1420 struct btree_iter iter; 1421 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, 1422 SPOS(inode->ei_inum.inum, start, snapshot), 0); 1423 1424 struct bkey_s_c k = 1425 bch2_btree_iter_peek_max(trans, &iter, POS(inode->ei_inum.inum, end)); 1426 ret = bkey_err(k); 1427 if (ret) 1428 goto err; 1429 1430 u64 pagecache_end = k.k ? max(start, bkey_start_offset(k.k)) : end; 1431 1432 ret = bch2_next_fiemap_pagecache_extent(trans, inode, start, pagecache_end, cur); 1433 if (ret) 1434 goto err; 1435 1436 struct bpos pagecache_start = bkey_start_pos(&cur->kbuf.k->k); 1437 1438 /* 1439 * Does the pagecache or the btree take precedence? 1440 * 1441 * It _should_ be the pagecache, so that we correctly report delalloc 1442 * extents when dirty in the pagecache (we're COW, after all). 1443 * 1444 * But we'd have to add per-sector writeback tracking to 1445 * bch_folio_state, otherwise we report delalloc extents for clean 1446 * cached data in the pagecache. 1447 * 1448 * We should do this, but even then fiemap won't report stable mappings: 1449 * on bcachefs data moves around in the background (copygc, rebalance) 1450 * and we don't provide a way for userspace to lock that out. 1451 */ 1452 if (k.k && 1453 bkey_le(bpos_max(iter.pos, bkey_start_pos(k.k)), 1454 pagecache_start)) { 1455 bch2_bkey_buf_reassemble(&cur->kbuf, trans->c, k); 1456 bch2_cut_front(iter.pos, cur->kbuf.k); 1457 bch2_cut_back(POS(inode->ei_inum.inum, end), cur->kbuf.k); 1458 cur->flags = 0; 1459 } else if (k.k) { 1460 bch2_cut_back(bkey_start_pos(k.k), cur->kbuf.k); 1461 } 1462 1463 if (cur->kbuf.k->k.type == KEY_TYPE_reflink_p) { 1464 unsigned sectors = cur->kbuf.k->k.size; 1465 s64 offset_into_extent = 0; 1466 enum btree_id data_btree = BTREE_ID_extents; 1467 ret = bch2_read_indirect_extent(trans, &data_btree, &offset_into_extent, 1468 &cur->kbuf); 1469 if (ret) 1470 goto err; 1471 1472 struct bkey_i *k = cur->kbuf.k; 1473 sectors = min_t(unsigned, sectors, k->k.size - offset_into_extent); 1474 1475 bch2_cut_front(POS(k->k.p.inode, 1476 bkey_start_offset(&k->k) + offset_into_extent), 1477 k); 1478 bch2_key_resize(&k->k, sectors); 1479 k->k.p = iter.pos; 1480 k->k.p.offset += k->k.size; 1481 } 1482 err: 1483 bch2_trans_iter_exit(trans, &iter); 1484 return ret; 1485 } 1486 1487 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info, 1488 u64 start, u64 len) 1489 { 1490 struct bch_fs *c = vinode->i_sb->s_fs_info; 1491 struct bch_inode_info *ei = to_bch_ei(vinode); 1492 struct btree_trans *trans; 1493 struct bch_fiemap_extent cur, prev; 1494 int ret = 0; 1495 1496 ret = fiemap_prep(&ei->v, info, start, &len, 0); 1497 if (ret) 1498 return ret; 1499 1500 if (start + len < start) 1501 return -EINVAL; 1502 1503 start >>= 9; 1504 u64 end = (start + len) >> 9; 1505 1506 bch2_bkey_buf_init(&cur.kbuf); 1507 bch2_bkey_buf_init(&prev.kbuf); 1508 bkey_init(&prev.kbuf.k->k); 1509 1510 trans = bch2_trans_get(c); 1511 1512 while (start < end) { 1513 ret = lockrestart_do(trans, 1514 bch2_next_fiemap_extent(trans, ei, start, end, &cur)); 1515 if (ret) 1516 goto err; 1517 1518 BUG_ON(bkey_start_offset(&cur.kbuf.k->k) < start); 1519 BUG_ON(cur.kbuf.k->k.p.offset > end); 1520 1521 if (bkey_start_offset(&cur.kbuf.k->k) == end) 1522 break; 1523 1524 start = cur.kbuf.k->k.p.offset; 1525 1526 if (!bkey_deleted(&prev.kbuf.k->k)) { 1527 bch2_trans_unlock(trans); 1528 ret = bch2_fill_extent(c, info, &prev); 1529 if (ret) 1530 goto err; 1531 } 1532 1533 bch2_bkey_buf_copy(&prev.kbuf, c, cur.kbuf.k); 1534 prev.flags = cur.flags; 1535 } 1536 1537 if (!bkey_deleted(&prev.kbuf.k->k)) { 1538 bch2_trans_unlock(trans); 1539 prev.flags |= FIEMAP_EXTENT_LAST; 1540 ret = bch2_fill_extent(c, info, &prev); 1541 } 1542 err: 1543 bch2_trans_put(trans); 1544 bch2_bkey_buf_exit(&cur.kbuf, c); 1545 bch2_bkey_buf_exit(&prev.kbuf, c); 1546 1547 return bch2_err_class(ret < 0 ? ret : 0); 1548 } 1549 1550 static const struct vm_operations_struct bch_vm_ops = { 1551 .fault = bch2_page_fault, 1552 .map_pages = filemap_map_pages, 1553 .page_mkwrite = bch2_page_mkwrite, 1554 }; 1555 1556 static int bch2_mmap(struct file *file, struct vm_area_struct *vma) 1557 { 1558 file_accessed(file); 1559 1560 vma->vm_ops = &bch_vm_ops; 1561 return 0; 1562 } 1563 1564 /* Directories: */ 1565 1566 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence) 1567 { 1568 return generic_file_llseek_size(file, offset, whence, 1569 S64_MAX, S64_MAX); 1570 } 1571 1572 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx) 1573 { 1574 struct bch_inode_info *inode = file_bch_inode(file); 1575 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1576 struct bch_hash_info hash = bch2_hash_info_init(c, &inode->ei_inode); 1577 1578 if (!dir_emit_dots(file, ctx)) 1579 return 0; 1580 1581 int ret = bch2_readdir(c, inode_inum(inode), &hash, ctx); 1582 1583 bch_err_fn(c, ret); 1584 return bch2_err_class(ret); 1585 } 1586 1587 static int bch2_open(struct inode *vinode, struct file *file) 1588 { 1589 if (file->f_flags & (O_WRONLY|O_RDWR)) { 1590 struct bch_inode_info *inode = to_bch_ei(vinode); 1591 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1592 1593 int ret = bch2_subvol_is_ro(c, inode->ei_inum.subvol); 1594 if (ret) 1595 return ret; 1596 } 1597 1598 file->f_mode |= FMODE_CAN_ODIRECT; 1599 1600 return generic_file_open(vinode, file); 1601 } 1602 1603 /* bcachefs inode flags -> FS_IOC_GETFLAGS: */ 1604 static const __maybe_unused unsigned bch_flags_to_uflags[] = { 1605 [__BCH_INODE_sync] = FS_SYNC_FL, 1606 [__BCH_INODE_immutable] = FS_IMMUTABLE_FL, 1607 [__BCH_INODE_append] = FS_APPEND_FL, 1608 [__BCH_INODE_nodump] = FS_NODUMP_FL, 1609 [__BCH_INODE_noatime] = FS_NOATIME_FL, 1610 }; 1611 1612 /* bcachefs inode flags -> FS_IOC_FSGETXATTR: */ 1613 static const __maybe_unused unsigned bch_flags_to_xflags[] = { 1614 [__BCH_INODE_sync] = FS_XFLAG_SYNC, 1615 [__BCH_INODE_immutable] = FS_XFLAG_IMMUTABLE, 1616 [__BCH_INODE_append] = FS_XFLAG_APPEND, 1617 [__BCH_INODE_nodump] = FS_XFLAG_NODUMP, 1618 [__BCH_INODE_noatime] = FS_XFLAG_NOATIME, 1619 }; 1620 1621 static int bch2_fileattr_get(struct dentry *dentry, 1622 struct fileattr *fa) 1623 { 1624 struct bch_inode_info *inode = to_bch_ei(d_inode(dentry)); 1625 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1626 1627 fileattr_fill_xflags(fa, map_flags(bch_flags_to_xflags, inode->ei_inode.bi_flags)); 1628 1629 if (inode->ei_inode.bi_fields_set & (1 << Inode_opt_project)) 1630 fa->fsx_xflags |= FS_XFLAG_PROJINHERIT; 1631 1632 if (bch2_inode_casefold(c, &inode->ei_inode)) 1633 fa->flags |= FS_CASEFOLD_FL; 1634 1635 fa->fsx_projid = inode->ei_qid.q[QTYP_PRJ]; 1636 return 0; 1637 } 1638 1639 struct flags_set { 1640 unsigned mask; 1641 unsigned flags; 1642 unsigned projid; 1643 bool set_project; 1644 bool set_casefold; 1645 bool casefold; 1646 }; 1647 1648 static int fssetxattr_inode_update_fn(struct btree_trans *trans, 1649 struct bch_inode_info *inode, 1650 struct bch_inode_unpacked *bi, 1651 void *p) 1652 { 1653 struct bch_fs *c = trans->c; 1654 struct flags_set *s = p; 1655 1656 /* 1657 * We're relying on btree locking here for exclusion with other ioctl 1658 * calls - use the flags in the btree (@bi), not inode->i_flags: 1659 */ 1660 if (!S_ISREG(bi->bi_mode) && 1661 !S_ISDIR(bi->bi_mode) && 1662 (s->flags & (BCH_INODE_nodump|BCH_INODE_noatime)) != s->flags) 1663 return -EINVAL; 1664 1665 if (s->casefold != bch2_inode_casefold(c, bi)) { 1666 int ret = bch2_inode_set_casefold(trans, inode_inum(inode), bi, s->casefold); 1667 if (ret) 1668 return ret; 1669 } 1670 1671 if (s->set_project) { 1672 bi->bi_project = s->projid; 1673 bi->bi_fields_set |= BIT(Inode_opt_project); 1674 } 1675 1676 bi->bi_flags &= ~s->mask; 1677 bi->bi_flags |= s->flags; 1678 1679 bi->bi_ctime = timespec_to_bch2_time(c, current_time(&inode->v)); 1680 return 0; 1681 } 1682 1683 static int bch2_fileattr_set(struct mnt_idmap *idmap, 1684 struct dentry *dentry, 1685 struct fileattr *fa) 1686 { 1687 struct bch_inode_info *inode = to_bch_ei(d_inode(dentry)); 1688 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1689 struct flags_set s = {}; 1690 int ret; 1691 1692 if (fa->fsx_valid) { 1693 fa->fsx_xflags &= ~FS_XFLAG_PROJINHERIT; 1694 1695 s.mask = map_defined(bch_flags_to_xflags); 1696 s.flags |= map_flags_rev(bch_flags_to_xflags, fa->fsx_xflags); 1697 if (fa->fsx_xflags) 1698 return -EOPNOTSUPP; 1699 1700 if (fa->fsx_projid >= U32_MAX) 1701 return -EINVAL; 1702 1703 /* 1704 * inode fields accessible via the xattr interface are stored with a +1 1705 * bias, so that 0 means unset: 1706 */ 1707 if ((inode->ei_inode.bi_project || 1708 fa->fsx_projid) && 1709 inode->ei_inode.bi_project != fa->fsx_projid + 1) { 1710 s.projid = fa->fsx_projid + 1; 1711 s.set_project = true; 1712 } 1713 } 1714 1715 if (fa->flags_valid) { 1716 s.mask = map_defined(bch_flags_to_uflags); 1717 1718 s.set_casefold = true; 1719 s.casefold = (fa->flags & FS_CASEFOLD_FL) != 0; 1720 fa->flags &= ~FS_CASEFOLD_FL; 1721 1722 s.flags |= map_flags_rev(bch_flags_to_uflags, fa->flags); 1723 if (fa->flags) 1724 return -EOPNOTSUPP; 1725 } 1726 1727 mutex_lock(&inode->ei_update_lock); 1728 ret = bch2_subvol_is_ro(c, inode->ei_inum.subvol) ?: 1729 (s.set_project 1730 ? bch2_set_projid(c, inode, fa->fsx_projid) 1731 : 0) ?: 1732 bch2_write_inode(c, inode, fssetxattr_inode_update_fn, &s, 1733 ATTR_CTIME); 1734 mutex_unlock(&inode->ei_update_lock); 1735 return ret; 1736 } 1737 1738 static const struct file_operations bch_file_operations = { 1739 .open = bch2_open, 1740 .llseek = bch2_llseek, 1741 .read_iter = bch2_read_iter, 1742 .write_iter = bch2_write_iter, 1743 .mmap = bch2_mmap, 1744 .get_unmapped_area = thp_get_unmapped_area, 1745 .fsync = bch2_fsync, 1746 .splice_read = filemap_splice_read, 1747 .splice_write = iter_file_splice_write, 1748 .fallocate = bch2_fallocate_dispatch, 1749 .unlocked_ioctl = bch2_fs_file_ioctl, 1750 #ifdef CONFIG_COMPAT 1751 .compat_ioctl = bch2_compat_fs_ioctl, 1752 #endif 1753 .remap_file_range = bch2_remap_file_range, 1754 }; 1755 1756 static const struct inode_operations bch_file_inode_operations = { 1757 .getattr = bch2_getattr, 1758 .setattr = bch2_setattr, 1759 .fiemap = bch2_fiemap, 1760 .listxattr = bch2_xattr_list, 1761 #ifdef CONFIG_BCACHEFS_POSIX_ACL 1762 .get_inode_acl = bch2_get_acl, 1763 .set_acl = bch2_set_acl, 1764 #endif 1765 .fileattr_get = bch2_fileattr_get, 1766 .fileattr_set = bch2_fileattr_set, 1767 }; 1768 1769 static const struct inode_operations bch_dir_inode_operations = { 1770 .lookup = bch2_lookup, 1771 .create = bch2_create, 1772 .link = bch2_link, 1773 .unlink = bch2_unlink, 1774 .symlink = bch2_symlink, 1775 .mkdir = bch2_mkdir, 1776 .rmdir = bch2_unlink, 1777 .mknod = bch2_mknod, 1778 .rename = bch2_rename2, 1779 .getattr = bch2_getattr, 1780 .setattr = bch2_setattr, 1781 .tmpfile = bch2_tmpfile, 1782 .listxattr = bch2_xattr_list, 1783 #ifdef CONFIG_BCACHEFS_POSIX_ACL 1784 .get_inode_acl = bch2_get_acl, 1785 .set_acl = bch2_set_acl, 1786 #endif 1787 .fileattr_get = bch2_fileattr_get, 1788 .fileattr_set = bch2_fileattr_set, 1789 }; 1790 1791 static const struct file_operations bch_dir_file_operations = { 1792 .llseek = bch2_dir_llseek, 1793 .read = generic_read_dir, 1794 .iterate_shared = bch2_vfs_readdir, 1795 .fsync = bch2_fsync, 1796 .unlocked_ioctl = bch2_fs_file_ioctl, 1797 #ifdef CONFIG_COMPAT 1798 .compat_ioctl = bch2_compat_fs_ioctl, 1799 #endif 1800 }; 1801 1802 static const struct inode_operations bch_symlink_inode_operations = { 1803 .get_link = page_get_link, 1804 .getattr = bch2_getattr, 1805 .setattr = bch2_setattr, 1806 .listxattr = bch2_xattr_list, 1807 #ifdef CONFIG_BCACHEFS_POSIX_ACL 1808 .get_inode_acl = bch2_get_acl, 1809 .set_acl = bch2_set_acl, 1810 #endif 1811 .fileattr_get = bch2_fileattr_get, 1812 .fileattr_set = bch2_fileattr_set, 1813 }; 1814 1815 static const struct inode_operations bch_special_inode_operations = { 1816 .getattr = bch2_getattr, 1817 .setattr = bch2_setattr, 1818 .listxattr = bch2_xattr_list, 1819 #ifdef CONFIG_BCACHEFS_POSIX_ACL 1820 .get_inode_acl = bch2_get_acl, 1821 .set_acl = bch2_set_acl, 1822 #endif 1823 .fileattr_get = bch2_fileattr_get, 1824 .fileattr_set = bch2_fileattr_set, 1825 }; 1826 1827 static const struct address_space_operations bch_address_space_operations = { 1828 .read_folio = bch2_read_folio, 1829 .writepages = bch2_writepages, 1830 .readahead = bch2_readahead, 1831 .dirty_folio = filemap_dirty_folio, 1832 .write_begin = bch2_write_begin, 1833 .write_end = bch2_write_end, 1834 .invalidate_folio = bch2_invalidate_folio, 1835 .release_folio = bch2_release_folio, 1836 #ifdef CONFIG_MIGRATION 1837 .migrate_folio = filemap_migrate_folio, 1838 #endif 1839 .error_remove_folio = generic_error_remove_folio, 1840 }; 1841 1842 struct bcachefs_fid { 1843 u64 inum; 1844 u32 subvol; 1845 u32 gen; 1846 } __packed; 1847 1848 struct bcachefs_fid_with_parent { 1849 struct bcachefs_fid fid; 1850 struct bcachefs_fid dir; 1851 } __packed; 1852 1853 static int bcachefs_fid_valid(int fh_len, int fh_type) 1854 { 1855 switch (fh_type) { 1856 case FILEID_BCACHEFS_WITHOUT_PARENT: 1857 return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32); 1858 case FILEID_BCACHEFS_WITH_PARENT: 1859 return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32); 1860 default: 1861 return false; 1862 } 1863 } 1864 1865 static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode) 1866 { 1867 return (struct bcachefs_fid) { 1868 .inum = inode->ei_inum.inum, 1869 .subvol = inode->ei_inum.subvol, 1870 .gen = inode->ei_inode.bi_generation, 1871 }; 1872 } 1873 1874 static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len, 1875 struct inode *vdir) 1876 { 1877 struct bch_inode_info *inode = to_bch_ei(vinode); 1878 struct bch_inode_info *dir = to_bch_ei(vdir); 1879 int min_len; 1880 1881 if (!S_ISDIR(inode->v.i_mode) && dir) { 1882 struct bcachefs_fid_with_parent *fid = (void *) fh; 1883 1884 min_len = sizeof(*fid) / sizeof(u32); 1885 if (*len < min_len) { 1886 *len = min_len; 1887 return FILEID_INVALID; 1888 } 1889 1890 fid->fid = bch2_inode_to_fid(inode); 1891 fid->dir = bch2_inode_to_fid(dir); 1892 1893 *len = min_len; 1894 return FILEID_BCACHEFS_WITH_PARENT; 1895 } else { 1896 struct bcachefs_fid *fid = (void *) fh; 1897 1898 min_len = sizeof(*fid) / sizeof(u32); 1899 if (*len < min_len) { 1900 *len = min_len; 1901 return FILEID_INVALID; 1902 } 1903 *fid = bch2_inode_to_fid(inode); 1904 1905 *len = min_len; 1906 return FILEID_BCACHEFS_WITHOUT_PARENT; 1907 } 1908 } 1909 1910 static struct inode *bch2_nfs_get_inode(struct super_block *sb, 1911 struct bcachefs_fid fid) 1912 { 1913 struct bch_fs *c = sb->s_fs_info; 1914 struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) { 1915 .subvol = fid.subvol, 1916 .inum = fid.inum, 1917 }); 1918 if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) { 1919 iput(vinode); 1920 vinode = ERR_PTR(-ESTALE); 1921 } 1922 return vinode; 1923 } 1924 1925 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid, 1926 int fh_len, int fh_type) 1927 { 1928 struct bcachefs_fid *fid = (void *) _fid; 1929 1930 if (!bcachefs_fid_valid(fh_len, fh_type)) 1931 return NULL; 1932 1933 return d_obtain_alias(bch2_nfs_get_inode(sb, *fid)); 1934 } 1935 1936 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid, 1937 int fh_len, int fh_type) 1938 { 1939 struct bcachefs_fid_with_parent *fid = (void *) _fid; 1940 1941 if (!bcachefs_fid_valid(fh_len, fh_type) || 1942 fh_type != FILEID_BCACHEFS_WITH_PARENT) 1943 return NULL; 1944 1945 return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir)); 1946 } 1947 1948 static struct dentry *bch2_get_parent(struct dentry *child) 1949 { 1950 struct bch_inode_info *inode = to_bch_ei(child->d_inode); 1951 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1952 subvol_inum parent_inum = { 1953 .subvol = inode->ei_inode.bi_parent_subvol ?: 1954 inode->ei_inum.subvol, 1955 .inum = inode->ei_inode.bi_dir, 1956 }; 1957 1958 return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum)); 1959 } 1960 1961 static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child) 1962 { 1963 struct bch_inode_info *inode = to_bch_ei(child->d_inode); 1964 struct bch_inode_info *dir = to_bch_ei(parent->d_inode); 1965 struct bch_fs *c = inode->v.i_sb->s_fs_info; 1966 struct btree_trans *trans; 1967 struct btree_iter iter1; 1968 struct btree_iter iter2; 1969 struct bkey_s_c k; 1970 struct bkey_s_c_dirent d; 1971 struct bch_inode_unpacked inode_u; 1972 subvol_inum target; 1973 u32 snapshot; 1974 struct qstr dirent_name; 1975 unsigned name_len = 0; 1976 int ret; 1977 1978 if (!S_ISDIR(dir->v.i_mode)) 1979 return -EINVAL; 1980 1981 trans = bch2_trans_get(c); 1982 1983 bch2_trans_iter_init(trans, &iter1, BTREE_ID_dirents, 1984 POS(dir->ei_inode.bi_inum, 0), 0); 1985 bch2_trans_iter_init(trans, &iter2, BTREE_ID_dirents, 1986 POS(dir->ei_inode.bi_inum, 0), 0); 1987 retry: 1988 bch2_trans_begin(trans); 1989 1990 ret = bch2_subvolume_get_snapshot(trans, dir->ei_inum.subvol, &snapshot); 1991 if (ret) 1992 goto err; 1993 1994 bch2_btree_iter_set_snapshot(trans, &iter1, snapshot); 1995 bch2_btree_iter_set_snapshot(trans, &iter2, snapshot); 1996 1997 ret = bch2_inode_find_by_inum_trans(trans, inode_inum(inode), &inode_u); 1998 if (ret) 1999 goto err; 2000 2001 if (inode_u.bi_dir == dir->ei_inode.bi_inum) { 2002 bch2_btree_iter_set_pos(trans, &iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset)); 2003 2004 k = bch2_btree_iter_peek_slot(trans, &iter1); 2005 ret = bkey_err(k); 2006 if (ret) 2007 goto err; 2008 2009 if (k.k->type != KEY_TYPE_dirent) { 2010 ret = bch_err_throw(c, ENOENT_dirent_doesnt_match_inode); 2011 goto err; 2012 } 2013 2014 d = bkey_s_c_to_dirent(k); 2015 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target); 2016 if (ret > 0) 2017 ret = bch_err_throw(c, ENOENT_dirent_doesnt_match_inode); 2018 if (ret) 2019 goto err; 2020 2021 if (subvol_inum_eq(target, inode->ei_inum)) 2022 goto found; 2023 } else { 2024 /* 2025 * File with multiple hardlinks and our backref is to the wrong 2026 * directory - linear search: 2027 */ 2028 for_each_btree_key_continue_norestart(trans, iter2, 0, k, ret) { 2029 if (k.k->p.inode > dir->ei_inode.bi_inum) 2030 break; 2031 2032 if (k.k->type != KEY_TYPE_dirent) 2033 continue; 2034 2035 d = bkey_s_c_to_dirent(k); 2036 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target); 2037 if (ret < 0) 2038 break; 2039 if (ret) 2040 continue; 2041 2042 if (subvol_inum_eq(target, inode->ei_inum)) 2043 goto found; 2044 } 2045 } 2046 2047 ret = -ENOENT; 2048 goto err; 2049 found: 2050 dirent_name = bch2_dirent_get_name(d); 2051 2052 name_len = min_t(unsigned, dirent_name.len, NAME_MAX); 2053 memcpy(name, dirent_name.name, name_len); 2054 name[name_len] = '\0'; 2055 err: 2056 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 2057 goto retry; 2058 2059 bch2_trans_iter_exit(trans, &iter1); 2060 bch2_trans_iter_exit(trans, &iter2); 2061 bch2_trans_put(trans); 2062 2063 return ret; 2064 } 2065 2066 static const struct export_operations bch_export_ops = { 2067 .encode_fh = bch2_encode_fh, 2068 .fh_to_dentry = bch2_fh_to_dentry, 2069 .fh_to_parent = bch2_fh_to_parent, 2070 .get_parent = bch2_get_parent, 2071 .get_name = bch2_get_name, 2072 }; 2073 2074 static void bch2_vfs_inode_init(struct btree_trans *trans, 2075 subvol_inum inum, 2076 struct bch_inode_info *inode, 2077 struct bch_inode_unpacked *bi, 2078 struct bch_subvolume *subvol) 2079 { 2080 inode->v.i_ino = inum.inum; 2081 inode->ei_inum = inum; 2082 inode->ei_inode.bi_inum = inum.inum; 2083 bch2_inode_update_after_write(trans, inode, bi, ~0); 2084 2085 inode->v.i_blocks = bi->bi_sectors; 2086 inode->v.i_rdev = bi->bi_dev; 2087 inode->v.i_generation = bi->bi_generation; 2088 inode->v.i_size = bi->bi_size; 2089 2090 inode->ei_flags = 0; 2091 inode->ei_quota_reserved = 0; 2092 inode->ei_qid = bch_qid(bi); 2093 2094 if (BCH_SUBVOLUME_SNAP(subvol)) 2095 set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags); 2096 2097 inode->v.i_mapping->a_ops = &bch_address_space_operations; 2098 2099 switch (inode->v.i_mode & S_IFMT) { 2100 case S_IFREG: 2101 inode->v.i_op = &bch_file_inode_operations; 2102 inode->v.i_fop = &bch_file_operations; 2103 break; 2104 case S_IFDIR: 2105 inode->v.i_op = &bch_dir_inode_operations; 2106 inode->v.i_fop = &bch_dir_file_operations; 2107 break; 2108 case S_IFLNK: 2109 inode_nohighmem(&inode->v); 2110 inode->v.i_op = &bch_symlink_inode_operations; 2111 break; 2112 default: 2113 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev); 2114 inode->v.i_op = &bch_special_inode_operations; 2115 break; 2116 } 2117 2118 mapping_set_folio_min_order(inode->v.i_mapping, 2119 get_order(trans->c->opts.block_size)); 2120 } 2121 2122 static void bch2_free_inode(struct inode *vinode) 2123 { 2124 kmem_cache_free(bch2_inode_cache, to_bch_ei(vinode)); 2125 } 2126 2127 static int inode_update_times_fn(struct btree_trans *trans, 2128 struct bch_inode_info *inode, 2129 struct bch_inode_unpacked *bi, 2130 void *p) 2131 { 2132 struct bch_fs *c = inode->v.i_sb->s_fs_info; 2133 2134 bi->bi_atime = timespec_to_bch2_time(c, inode_get_atime(&inode->v)); 2135 bi->bi_mtime = timespec_to_bch2_time(c, inode_get_mtime(&inode->v)); 2136 bi->bi_ctime = timespec_to_bch2_time(c, inode_get_ctime(&inode->v)); 2137 2138 return 0; 2139 } 2140 2141 static int bch2_vfs_write_inode(struct inode *vinode, 2142 struct writeback_control *wbc) 2143 { 2144 struct bch_fs *c = vinode->i_sb->s_fs_info; 2145 struct bch_inode_info *inode = to_bch_ei(vinode); 2146 int ret; 2147 2148 mutex_lock(&inode->ei_update_lock); 2149 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL, 2150 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME); 2151 mutex_unlock(&inode->ei_update_lock); 2152 2153 return bch2_err_class(ret); 2154 } 2155 2156 static void bch2_evict_inode(struct inode *vinode) 2157 { 2158 struct bch_fs *c = vinode->i_sb->s_fs_info; 2159 struct bch_inode_info *inode = to_bch_ei(vinode); 2160 bool delete = !inode->v.i_nlink && !is_bad_inode(&inode->v); 2161 2162 /* 2163 * evict() has waited for outstanding writeback, we'll do no more IO 2164 * through this inode: it's safe to remove from VFS inode hashtable here 2165 * 2166 * Do that now so that other threads aren't blocked from pulling it back 2167 * in, there's no reason for them to be: 2168 */ 2169 if (!delete) 2170 bch2_inode_hash_remove(c, inode); 2171 2172 truncate_inode_pages_final(&inode->v.i_data); 2173 2174 clear_inode(&inode->v); 2175 2176 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved); 2177 2178 if (delete) { 2179 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks), 2180 KEY_TYPE_QUOTA_WARN); 2181 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1, 2182 KEY_TYPE_QUOTA_WARN); 2183 int ret = bch2_inode_rm(c, inode_inum(inode)); 2184 if (ret && !bch2_err_matches(ret, EROFS)) { 2185 bch_err_msg(c, ret, "VFS incorrectly tried to delete inode %llu:%llu", 2186 inode->ei_inum.subvol, 2187 inode->ei_inum.inum); 2188 bch2_sb_error_count(c, BCH_FSCK_ERR_vfs_bad_inode_rm); 2189 } 2190 2191 /* 2192 * If we are deleting, we need it present in the vfs hash table 2193 * so that fsck can check if unlinked inodes are still open: 2194 */ 2195 bch2_inode_hash_remove(c, inode); 2196 } 2197 2198 mutex_lock(&c->vfs_inodes_lock); 2199 list_del_init(&inode->ei_vfs_inode_list); 2200 mutex_unlock(&c->vfs_inodes_lock); 2201 } 2202 2203 void bch2_evict_subvolume_inodes(struct bch_fs *c, snapshot_id_list *s) 2204 { 2205 struct bch_inode_info *inode; 2206 DARRAY(struct bch_inode_info *) grabbed; 2207 bool clean_pass = false, this_pass_clean; 2208 2209 /* 2210 * Initially, we scan for inodes without I_DONTCACHE, then mark them to 2211 * be pruned with d_mark_dontcache(). 2212 * 2213 * Once we've had a clean pass where we didn't find any inodes without 2214 * I_DONTCACHE, we wait for them to be freed: 2215 */ 2216 2217 darray_init(&grabbed); 2218 darray_make_room(&grabbed, 1024); 2219 again: 2220 cond_resched(); 2221 this_pass_clean = true; 2222 2223 mutex_lock(&c->vfs_inodes_lock); 2224 list_for_each_entry(inode, &c->vfs_inodes_list, ei_vfs_inode_list) { 2225 if (!snapshot_list_has_id(s, inode->ei_inum.subvol)) 2226 continue; 2227 2228 if (!(inode->v.i_state & I_DONTCACHE) && 2229 !(inode->v.i_state & I_FREEING) && 2230 igrab(&inode->v)) { 2231 this_pass_clean = false; 2232 2233 if (darray_push_gfp(&grabbed, inode, GFP_ATOMIC|__GFP_NOWARN)) { 2234 iput(&inode->v); 2235 break; 2236 } 2237 } else if (clean_pass && this_pass_clean) { 2238 struct wait_bit_queue_entry wqe; 2239 struct wait_queue_head *wq_head; 2240 2241 wq_head = inode_bit_waitqueue(&wqe, &inode->v, __I_NEW); 2242 prepare_to_wait_event(wq_head, &wqe.wq_entry, 2243 TASK_UNINTERRUPTIBLE); 2244 mutex_unlock(&c->vfs_inodes_lock); 2245 2246 schedule(); 2247 finish_wait(wq_head, &wqe.wq_entry); 2248 goto again; 2249 } 2250 } 2251 mutex_unlock(&c->vfs_inodes_lock); 2252 2253 darray_for_each(grabbed, i) { 2254 inode = *i; 2255 d_mark_dontcache(&inode->v); 2256 d_prune_aliases(&inode->v); 2257 iput(&inode->v); 2258 } 2259 grabbed.nr = 0; 2260 2261 if (!clean_pass || !this_pass_clean) { 2262 clean_pass = this_pass_clean; 2263 goto again; 2264 } 2265 2266 darray_exit(&grabbed); 2267 } 2268 2269 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf) 2270 { 2271 struct super_block *sb = dentry->d_sb; 2272 struct bch_fs *c = sb->s_fs_info; 2273 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c); 2274 unsigned shift = sb->s_blocksize_bits - 9; 2275 /* 2276 * this assumes inodes take up 64 bytes, which is a decent average 2277 * number: 2278 */ 2279 u64 avail_inodes = ((usage.capacity - usage.used) << 3); 2280 2281 buf->f_type = BCACHEFS_STATFS_MAGIC; 2282 buf->f_bsize = sb->s_blocksize; 2283 buf->f_blocks = usage.capacity >> shift; 2284 buf->f_bfree = usage.free >> shift; 2285 buf->f_bavail = avail_factor(usage.free) >> shift; 2286 2287 buf->f_files = usage.nr_inodes + avail_inodes; 2288 buf->f_ffree = avail_inodes; 2289 2290 buf->f_fsid = uuid_to_fsid(c->sb.user_uuid.b); 2291 buf->f_namelen = BCH_NAME_MAX; 2292 2293 return 0; 2294 } 2295 2296 static int bch2_sync_fs(struct super_block *sb, int wait) 2297 { 2298 struct bch_fs *c = sb->s_fs_info; 2299 int ret; 2300 2301 trace_bch2_sync_fs(sb, wait); 2302 2303 if (c->opts.journal_flush_disabled) 2304 return 0; 2305 2306 if (!wait) { 2307 bch2_journal_flush_async(&c->journal, NULL); 2308 return 0; 2309 } 2310 2311 ret = bch2_journal_flush(&c->journal); 2312 return bch2_err_class(ret); 2313 } 2314 2315 static struct bch_fs *bch2_path_to_fs(const char *path) 2316 { 2317 struct bch_fs *c; 2318 dev_t dev; 2319 int ret; 2320 2321 ret = lookup_bdev(path, &dev); 2322 if (ret) 2323 return ERR_PTR(ret); 2324 2325 c = bch2_dev_to_fs(dev); 2326 if (c) 2327 closure_put(&c->cl); 2328 return c ?: ERR_PTR(-ENOENT); 2329 } 2330 2331 static int bch2_show_devname(struct seq_file *seq, struct dentry *root) 2332 { 2333 struct bch_fs *c = root->d_sb->s_fs_info; 2334 bool first = true; 2335 2336 guard(rcu)(); 2337 for_each_online_member_rcu(c, ca) { 2338 if (!first) 2339 seq_putc(seq, ':'); 2340 first = false; 2341 seq_puts(seq, ca->disk_sb.sb_name); 2342 } 2343 2344 return 0; 2345 } 2346 2347 static int bch2_show_options(struct seq_file *seq, struct dentry *root) 2348 { 2349 struct bch_fs *c = root->d_sb->s_fs_info; 2350 struct printbuf buf = PRINTBUF; 2351 2352 bch2_opts_to_text(&buf, c->opts, c, c->disk_sb.sb, 2353 OPT_MOUNT, OPT_HIDDEN, OPT_SHOW_MOUNT_STYLE); 2354 printbuf_nul_terminate(&buf); 2355 seq_printf(seq, ",%s", buf.buf); 2356 2357 int ret = buf.allocation_failure ? -ENOMEM : 0; 2358 printbuf_exit(&buf); 2359 return ret; 2360 } 2361 2362 static void bch2_put_super(struct super_block *sb) 2363 { 2364 struct bch_fs *c = sb->s_fs_info; 2365 2366 __bch2_fs_stop(c); 2367 } 2368 2369 /* 2370 * bcachefs doesn't currently integrate intwrite freeze protection but the 2371 * internal write references serve the same purpose. Therefore reuse the 2372 * read-only transition code to perform the quiesce. The caveat is that we don't 2373 * currently have the ability to block tasks that want a write reference while 2374 * the superblock is frozen. This is fine for now, but we should either add 2375 * blocking support or find a way to integrate sb_start_intwrite() and friends. 2376 */ 2377 static int bch2_freeze(struct super_block *sb) 2378 { 2379 struct bch_fs *c = sb->s_fs_info; 2380 2381 down_write(&c->state_lock); 2382 bch2_fs_read_only(c); 2383 up_write(&c->state_lock); 2384 return 0; 2385 } 2386 2387 static int bch2_unfreeze(struct super_block *sb) 2388 { 2389 struct bch_fs *c = sb->s_fs_info; 2390 int ret; 2391 2392 if (test_bit(BCH_FS_emergency_ro, &c->flags)) 2393 return 0; 2394 2395 down_write(&c->state_lock); 2396 ret = bch2_fs_read_write(c); 2397 up_write(&c->state_lock); 2398 return ret; 2399 } 2400 2401 static const struct super_operations bch_super_operations = { 2402 .alloc_inode = bch2_alloc_inode, 2403 .free_inode = bch2_free_inode, 2404 .write_inode = bch2_vfs_write_inode, 2405 .evict_inode = bch2_evict_inode, 2406 .sync_fs = bch2_sync_fs, 2407 .statfs = bch2_statfs, 2408 .show_devname = bch2_show_devname, 2409 .show_options = bch2_show_options, 2410 .put_super = bch2_put_super, 2411 .freeze_fs = bch2_freeze, 2412 .unfreeze_fs = bch2_unfreeze, 2413 }; 2414 2415 static int bch2_set_super(struct super_block *s, void *data) 2416 { 2417 s->s_fs_info = data; 2418 return 0; 2419 } 2420 2421 static int bch2_noset_super(struct super_block *s, void *data) 2422 { 2423 return -EBUSY; 2424 } 2425 2426 typedef DARRAY(struct bch_fs *) darray_fs; 2427 2428 static int bch2_test_super(struct super_block *s, void *data) 2429 { 2430 struct bch_fs *c = s->s_fs_info; 2431 darray_fs *d = data; 2432 2433 if (!c) 2434 return false; 2435 2436 darray_for_each(*d, i) 2437 if (c != *i) 2438 return false; 2439 return true; 2440 } 2441 2442 static int bch2_fs_get_tree(struct fs_context *fc) 2443 { 2444 struct bch_fs *c; 2445 struct super_block *sb; 2446 struct inode *vinode; 2447 struct bch2_opts_parse *opts_parse = fc->fs_private; 2448 struct bch_opts opts = opts_parse->opts; 2449 darray_const_str devs; 2450 darray_fs devs_to_fs = {}; 2451 int ret; 2452 2453 opt_set(opts, read_only, (fc->sb_flags & SB_RDONLY) != 0); 2454 opt_set(opts, nostart, true); 2455 2456 if (!fc->source || strlen(fc->source) == 0) 2457 return -EINVAL; 2458 2459 ret = bch2_split_devs(fc->source, &devs); 2460 if (ret) 2461 return ret; 2462 2463 darray_for_each(devs, i) { 2464 ret = darray_push(&devs_to_fs, bch2_path_to_fs(*i)); 2465 if (ret) 2466 goto err; 2467 } 2468 2469 sb = sget(fc->fs_type, bch2_test_super, bch2_noset_super, fc->sb_flags|SB_NOSEC, &devs_to_fs); 2470 if (!IS_ERR(sb)) 2471 goto got_sb; 2472 2473 c = bch2_fs_open(&devs, &opts); 2474 ret = PTR_ERR_OR_ZERO(c); 2475 if (ret) 2476 goto err; 2477 2478 if (opt_defined(opts, discard)) 2479 set_bit(BCH_FS_discard_mount_opt_set, &c->flags); 2480 2481 /* Some options can't be parsed until after the fs is started: */ 2482 opts = bch2_opts_empty(); 2483 ret = bch2_parse_mount_opts(c, &opts, NULL, opts_parse->parse_later.buf, false); 2484 if (ret) 2485 goto err_stop_fs; 2486 2487 bch2_opts_apply(&c->opts, opts); 2488 2489 ret = bch2_fs_start(c); 2490 if (ret) 2491 goto err_stop_fs; 2492 2493 /* 2494 * We might be doing a RO mount because other options required it, or we 2495 * have no alloc info and it's a small image with no room to regenerate 2496 * it 2497 */ 2498 if (c->opts.read_only) 2499 fc->sb_flags |= SB_RDONLY; 2500 2501 sb = sget(fc->fs_type, NULL, bch2_set_super, fc->sb_flags|SB_NOSEC, c); 2502 ret = PTR_ERR_OR_ZERO(sb); 2503 if (ret) 2504 goto err_stop_fs; 2505 got_sb: 2506 c = sb->s_fs_info; 2507 2508 if (sb->s_root) { 2509 if ((fc->sb_flags ^ sb->s_flags) & SB_RDONLY) { 2510 ret = -EBUSY; 2511 goto err_put_super; 2512 } 2513 goto out; 2514 } 2515 2516 sb->s_blocksize = block_bytes(c); 2517 sb->s_blocksize_bits = ilog2(block_bytes(c)); 2518 sb->s_maxbytes = MAX_LFS_FILESIZE; 2519 sb->s_op = &bch_super_operations; 2520 sb->s_export_op = &bch_export_ops; 2521 #ifdef CONFIG_BCACHEFS_QUOTA 2522 sb->s_qcop = &bch2_quotactl_operations; 2523 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ; 2524 #endif 2525 sb->s_xattr = bch2_xattr_handlers; 2526 sb->s_magic = BCACHEFS_STATFS_MAGIC; 2527 sb->s_time_gran = c->sb.nsec_per_time_unit; 2528 sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1; 2529 sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec); 2530 super_set_uuid(sb, c->sb.user_uuid.b, sizeof(c->sb.user_uuid)); 2531 2532 if (c->sb.multi_device) 2533 super_set_sysfs_name_uuid(sb); 2534 else 2535 strscpy(sb->s_sysfs_name, c->name, sizeof(sb->s_sysfs_name)); 2536 2537 sb->s_shrink->seeks = 0; 2538 c->vfs_sb = sb; 2539 strscpy(sb->s_id, c->name, sizeof(sb->s_id)); 2540 2541 ret = super_setup_bdi(sb); 2542 if (ret) 2543 goto err_put_super; 2544 2545 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES; 2546 2547 scoped_guard(rcu) { 2548 for_each_online_member_rcu(c, ca) { 2549 struct block_device *bdev = ca->disk_sb.bdev; 2550 2551 /* XXX: create an anonymous device for multi device filesystems */ 2552 sb->s_bdev = bdev; 2553 sb->s_dev = bdev->bd_dev; 2554 break; 2555 } 2556 } 2557 2558 c->dev = sb->s_dev; 2559 2560 #ifdef CONFIG_BCACHEFS_POSIX_ACL 2561 if (c->opts.acl) 2562 sb->s_flags |= SB_POSIXACL; 2563 #endif 2564 2565 sb->s_shrink->seeks = 0; 2566 2567 #ifdef CONFIG_UNICODE 2568 sb->s_encoding = c->cf_encoding; 2569 #endif 2570 generic_set_sb_d_ops(sb); 2571 2572 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM); 2573 ret = PTR_ERR_OR_ZERO(vinode); 2574 bch_err_msg(c, ret, "mounting: error getting root inode"); 2575 if (ret) 2576 goto err_put_super; 2577 2578 sb->s_root = d_make_root(vinode); 2579 if (!sb->s_root) { 2580 bch_err(c, "error mounting: error allocating root dentry"); 2581 ret = -ENOMEM; 2582 goto err_put_super; 2583 } 2584 2585 sb->s_flags |= SB_ACTIVE; 2586 out: 2587 fc->root = dget(sb->s_root); 2588 err: 2589 darray_exit(&devs_to_fs); 2590 bch2_darray_str_exit(&devs); 2591 if (ret) 2592 pr_err("error: %s", bch2_err_str(ret)); 2593 /* 2594 * On an inconsistency error in recovery we might see an -EROFS derived 2595 * errorcode (from the journal), but we don't want to return that to 2596 * userspace as that causes util-linux to retry the mount RO - which is 2597 * confusing: 2598 */ 2599 if (bch2_err_matches(ret, EROFS) && ret != -EROFS) 2600 ret = -EIO; 2601 return bch2_err_class(ret); 2602 2603 err_stop_fs: 2604 bch2_fs_stop(c); 2605 goto err; 2606 2607 err_put_super: 2608 if (!sb->s_root) 2609 __bch2_fs_stop(c); 2610 deactivate_locked_super(sb); 2611 goto err; 2612 } 2613 2614 static void bch2_kill_sb(struct super_block *sb) 2615 { 2616 struct bch_fs *c = sb->s_fs_info; 2617 2618 generic_shutdown_super(sb); 2619 bch2_fs_free(c); 2620 } 2621 2622 static void bch2_fs_context_free(struct fs_context *fc) 2623 { 2624 struct bch2_opts_parse *opts = fc->fs_private; 2625 2626 if (opts) { 2627 printbuf_exit(&opts->parse_later); 2628 kfree(opts); 2629 } 2630 } 2631 2632 static int bch2_fs_parse_param(struct fs_context *fc, 2633 struct fs_parameter *param) 2634 { 2635 /* 2636 * the "source" param, i.e., the name of the device(s) to mount, 2637 * is handled by the VFS layer. 2638 */ 2639 if (!strcmp(param->key, "source")) 2640 return -ENOPARAM; 2641 2642 struct bch2_opts_parse *opts = fc->fs_private; 2643 struct bch_fs *c = NULL; 2644 2645 /* for reconfigure, we already have a struct bch_fs */ 2646 if (fc->root) 2647 c = fc->root->d_sb->s_fs_info; 2648 2649 int ret = bch2_parse_one_mount_opt(c, &opts->opts, 2650 &opts->parse_later, param->key, 2651 param->string); 2652 if (ret) 2653 pr_err("Error parsing option %s: %s", param->key, bch2_err_str(ret)); 2654 2655 return bch2_err_class(ret); 2656 } 2657 2658 static int bch2_fs_reconfigure(struct fs_context *fc) 2659 { 2660 struct super_block *sb = fc->root->d_sb; 2661 struct bch2_opts_parse *opts = fc->fs_private; 2662 struct bch_fs *c = sb->s_fs_info; 2663 int ret = 0; 2664 2665 opt_set(opts->opts, read_only, (fc->sb_flags & SB_RDONLY) != 0); 2666 2667 if (opts->opts.read_only != c->opts.read_only) { 2668 down_write(&c->state_lock); 2669 2670 if (opts->opts.read_only) { 2671 bch2_fs_read_only(c); 2672 2673 sb->s_flags |= SB_RDONLY; 2674 } else { 2675 ret = bch2_fs_read_write(c); 2676 if (ret) { 2677 bch_err(c, "error going rw: %i", ret); 2678 up_write(&c->state_lock); 2679 ret = -EINVAL; 2680 goto err; 2681 } 2682 2683 sb->s_flags &= ~SB_RDONLY; 2684 } 2685 2686 c->opts.read_only = opts->opts.read_only; 2687 2688 up_write(&c->state_lock); 2689 } 2690 2691 if (opt_defined(opts->opts, errors)) 2692 c->opts.errors = opts->opts.errors; 2693 err: 2694 return bch2_err_class(ret); 2695 } 2696 2697 static const struct fs_context_operations bch2_context_ops = { 2698 .free = bch2_fs_context_free, 2699 .parse_param = bch2_fs_parse_param, 2700 .get_tree = bch2_fs_get_tree, 2701 .reconfigure = bch2_fs_reconfigure, 2702 }; 2703 2704 static int bch2_init_fs_context(struct fs_context *fc) 2705 { 2706 struct bch2_opts_parse *opts = kzalloc(sizeof(*opts), GFP_KERNEL); 2707 2708 if (!opts) 2709 return -ENOMEM; 2710 2711 opts->parse_later = PRINTBUF; 2712 2713 fc->ops = &bch2_context_ops; 2714 fc->fs_private = opts; 2715 2716 return 0; 2717 } 2718 2719 void bch2_fs_vfs_exit(struct bch_fs *c) 2720 { 2721 if (c->vfs_inodes_by_inum_table.ht.tbl) 2722 rhltable_destroy(&c->vfs_inodes_by_inum_table); 2723 if (c->vfs_inodes_table.tbl) 2724 rhashtable_destroy(&c->vfs_inodes_table); 2725 } 2726 2727 int bch2_fs_vfs_init(struct bch_fs *c) 2728 { 2729 return rhashtable_init(&c->vfs_inodes_table, &bch2_vfs_inodes_params) ?: 2730 rhltable_init(&c->vfs_inodes_by_inum_table, &bch2_vfs_inodes_by_inum_params); 2731 } 2732 2733 static struct file_system_type bcache_fs_type = { 2734 .owner = THIS_MODULE, 2735 .name = "bcachefs", 2736 .init_fs_context = bch2_init_fs_context, 2737 .kill_sb = bch2_kill_sb, 2738 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP | FS_LBS, 2739 }; 2740 2741 MODULE_ALIAS_FS("bcachefs"); 2742 2743 void bch2_vfs_exit(void) 2744 { 2745 unregister_filesystem(&bcache_fs_type); 2746 kmem_cache_destroy(bch2_inode_cache); 2747 } 2748 2749 int __init bch2_vfs_init(void) 2750 { 2751 int ret = -ENOMEM; 2752 2753 bch2_inode_cache = KMEM_CACHE(bch_inode_info, SLAB_RECLAIM_ACCOUNT | 2754 SLAB_ACCOUNT); 2755 if (!bch2_inode_cache) 2756 goto err; 2757 2758 ret = register_filesystem(&bcache_fs_type); 2759 if (ret) 2760 goto err; 2761 2762 return 0; 2763 err: 2764 bch2_vfs_exit(); 2765 return ret; 2766 } 2767 2768 #endif /* NO_BCACHEFS_FS */ 2769