1 // SPDX-License-Identifier: GPL-2.0 2 3 #include "bcachefs.h" 4 #include "alloc_background.h" 5 #include "bkey_buf.h" 6 #include "btree_journal_iter.h" 7 #include "btree_node_scan.h" 8 #include "btree_update.h" 9 #include "btree_update_interior.h" 10 #include "btree_io.h" 11 #include "buckets.h" 12 #include "dirent.h" 13 #include "disk_accounting.h" 14 #include "errcode.h" 15 #include "error.h" 16 #include "journal_io.h" 17 #include "journal_reclaim.h" 18 #include "journal_seq_blacklist.h" 19 #include "logged_ops.h" 20 #include "move.h" 21 #include "movinggc.h" 22 #include "namei.h" 23 #include "quota.h" 24 #include "rebalance.h" 25 #include "recovery.h" 26 #include "recovery_passes.h" 27 #include "replicas.h" 28 #include "sb-clean.h" 29 #include "sb-downgrade.h" 30 #include "snapshot.h" 31 #include "super-io.h" 32 33 #include <linux/sort.h> 34 #include <linux/stat.h> 35 36 int bch2_btree_lost_data(struct bch_fs *c, 37 struct printbuf *msg, 38 enum btree_id btree) 39 { 40 u64 b = BIT_ULL(btree); 41 int ret = 0; 42 43 mutex_lock(&c->sb_lock); 44 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); 45 46 if (!(c->sb.btrees_lost_data & b)) { 47 prt_printf(msg, "flagging btree "); 48 bch2_btree_id_to_text(msg, btree); 49 prt_printf(msg, " lost data\n"); 50 51 ext->btrees_lost_data |= cpu_to_le64(b); 52 } 53 54 /* Once we have runtime self healing for topology errors we won't need this: */ 55 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_topology, 0) ?: ret; 56 57 /* Btree node accounting will be off: */ 58 __set_bit_le64(BCH_FSCK_ERR_accounting_mismatch, ext->errors_silent); 59 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_allocations, 0) ?: ret; 60 61 #ifdef CONFIG_BCACHEFS_DEBUG 62 /* 63 * These are much more minor, and don't need to be corrected right away, 64 * but in debug mode we want the next fsck run to be clean: 65 */ 66 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_lrus, 0) ?: ret; 67 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_backpointers_to_extents, 0) ?: ret; 68 #endif 69 70 switch (btree) { 71 case BTREE_ID_alloc: 72 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_alloc_info, 0) ?: ret; 73 74 __set_bit_le64(BCH_FSCK_ERR_alloc_key_data_type_wrong, ext->errors_silent); 75 __set_bit_le64(BCH_FSCK_ERR_alloc_key_gen_wrong, ext->errors_silent); 76 __set_bit_le64(BCH_FSCK_ERR_alloc_key_dirty_sectors_wrong, ext->errors_silent); 77 __set_bit_le64(BCH_FSCK_ERR_alloc_key_cached_sectors_wrong, ext->errors_silent); 78 __set_bit_le64(BCH_FSCK_ERR_alloc_key_stripe_wrong, ext->errors_silent); 79 __set_bit_le64(BCH_FSCK_ERR_alloc_key_stripe_redundancy_wrong, ext->errors_silent); 80 goto out; 81 case BTREE_ID_backpointers: 82 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_btree_backpointers, 0) ?: ret; 83 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_extents_to_backpointers, 0) ?: ret; 84 goto out; 85 case BTREE_ID_need_discard: 86 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_alloc_info, 0) ?: ret; 87 goto out; 88 case BTREE_ID_freespace: 89 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_alloc_info, 0) ?: ret; 90 goto out; 91 case BTREE_ID_bucket_gens: 92 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_alloc_info, 0) ?: ret; 93 goto out; 94 case BTREE_ID_lru: 95 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_alloc_info, 0) ?: ret; 96 goto out; 97 case BTREE_ID_accounting: 98 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_allocations, 0) ?: ret; 99 goto out; 100 case BTREE_ID_snapshots: 101 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_reconstruct_snapshots, 0) ?: ret; 102 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_topology, 0) ?: ret; 103 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_scan_for_btree_nodes, 0) ?: ret; 104 goto out; 105 default: 106 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_check_topology, 0) ?: ret; 107 ret = __bch2_run_explicit_recovery_pass(c, msg, BCH_RECOVERY_PASS_scan_for_btree_nodes, 0) ?: ret; 108 goto out; 109 } 110 out: 111 bch2_write_super(c); 112 mutex_unlock(&c->sb_lock); 113 114 return ret; 115 } 116 117 static void kill_btree(struct bch_fs *c, enum btree_id btree) 118 { 119 bch2_btree_id_root(c, btree)->alive = false; 120 bch2_shoot_down_journal_keys(c, btree, 0, BTREE_MAX_DEPTH, POS_MIN, SPOS_MAX); 121 } 122 123 /* for -o reconstruct_alloc: */ 124 void bch2_reconstruct_alloc(struct bch_fs *c) 125 { 126 mutex_lock(&c->sb_lock); 127 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); 128 129 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_allocations, ext->recovery_passes_required); 130 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_alloc_info, ext->recovery_passes_required); 131 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_lrus, ext->recovery_passes_required); 132 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_extents_to_backpointers, ext->recovery_passes_required); 133 __set_bit_le64(BCH_RECOVERY_PASS_STABLE_check_alloc_to_lru_refs, ext->recovery_passes_required); 134 135 __set_bit_le64(BCH_FSCK_ERR_ptr_to_missing_alloc_key, ext->errors_silent); 136 __set_bit_le64(BCH_FSCK_ERR_ptr_gen_newer_than_bucket_gen, ext->errors_silent); 137 __set_bit_le64(BCH_FSCK_ERR_stale_dirty_ptr, ext->errors_silent); 138 139 __set_bit_le64(BCH_FSCK_ERR_dev_usage_buckets_wrong, ext->errors_silent); 140 __set_bit_le64(BCH_FSCK_ERR_dev_usage_sectors_wrong, ext->errors_silent); 141 __set_bit_le64(BCH_FSCK_ERR_dev_usage_fragmented_wrong, ext->errors_silent); 142 143 __set_bit_le64(BCH_FSCK_ERR_fs_usage_btree_wrong, ext->errors_silent); 144 __set_bit_le64(BCH_FSCK_ERR_fs_usage_cached_wrong, ext->errors_silent); 145 __set_bit_le64(BCH_FSCK_ERR_fs_usage_persistent_reserved_wrong, ext->errors_silent); 146 __set_bit_le64(BCH_FSCK_ERR_fs_usage_replicas_wrong, ext->errors_silent); 147 148 __set_bit_le64(BCH_FSCK_ERR_alloc_key_to_missing_lru_entry, ext->errors_silent); 149 150 __set_bit_le64(BCH_FSCK_ERR_alloc_key_data_type_wrong, ext->errors_silent); 151 __set_bit_le64(BCH_FSCK_ERR_alloc_key_gen_wrong, ext->errors_silent); 152 __set_bit_le64(BCH_FSCK_ERR_alloc_key_dirty_sectors_wrong, ext->errors_silent); 153 __set_bit_le64(BCH_FSCK_ERR_alloc_key_cached_sectors_wrong, ext->errors_silent); 154 __set_bit_le64(BCH_FSCK_ERR_alloc_key_stripe_wrong, ext->errors_silent); 155 __set_bit_le64(BCH_FSCK_ERR_alloc_key_stripe_redundancy_wrong, ext->errors_silent); 156 __set_bit_le64(BCH_FSCK_ERR_need_discard_key_wrong, ext->errors_silent); 157 __set_bit_le64(BCH_FSCK_ERR_freespace_key_wrong, ext->errors_silent); 158 __set_bit_le64(BCH_FSCK_ERR_bucket_gens_key_wrong, ext->errors_silent); 159 __set_bit_le64(BCH_FSCK_ERR_freespace_hole_missing, ext->errors_silent); 160 __set_bit_le64(BCH_FSCK_ERR_ptr_to_missing_backpointer, ext->errors_silent); 161 __set_bit_le64(BCH_FSCK_ERR_lru_entry_bad, ext->errors_silent); 162 __set_bit_le64(BCH_FSCK_ERR_accounting_mismatch, ext->errors_silent); 163 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info); 164 165 c->opts.recovery_passes |= bch2_recovery_passes_from_stable(le64_to_cpu(ext->recovery_passes_required[0])); 166 167 c->disk_sb.sb->features[0] &= ~cpu_to_le64(BIT_ULL(BCH_FEATURE_no_alloc_info)); 168 169 bch2_write_super(c); 170 mutex_unlock(&c->sb_lock); 171 172 for (unsigned i = 0; i < btree_id_nr_alive(c); i++) 173 if (btree_id_is_alloc(i)) 174 kill_btree(c, i); 175 } 176 177 /* 178 * Btree node pointers have a field to stack a pointer to the in memory btree 179 * node; we need to zero out this field when reading in btree nodes, or when 180 * reading in keys from the journal: 181 */ 182 static void zero_out_btree_mem_ptr(struct journal_keys *keys) 183 { 184 darray_for_each(*keys, i) 185 if (i->k->k.type == KEY_TYPE_btree_ptr_v2) 186 bkey_i_to_btree_ptr_v2(i->k)->v.mem_ptr = 0; 187 } 188 189 /* journal replay: */ 190 191 static void replay_now_at(struct journal *j, u64 seq) 192 { 193 BUG_ON(seq < j->replay_journal_seq); 194 195 seq = min(seq, j->replay_journal_seq_end); 196 197 while (j->replay_journal_seq < seq) 198 bch2_journal_pin_put(j, j->replay_journal_seq++); 199 } 200 201 static int bch2_journal_replay_accounting_key(struct btree_trans *trans, 202 struct journal_key *k) 203 { 204 struct btree_iter iter; 205 bch2_trans_node_iter_init(trans, &iter, k->btree_id, k->k->k.p, 206 BTREE_MAX_DEPTH, k->level, 207 BTREE_ITER_intent); 208 int ret = bch2_btree_iter_traverse(trans, &iter); 209 if (ret) 210 goto out; 211 212 struct bkey u; 213 struct bkey_s_c old = bch2_btree_path_peek_slot(btree_iter_path(trans, &iter), &u); 214 215 /* Has this delta already been applied to the btree? */ 216 if (bversion_cmp(old.k->bversion, k->k->k.bversion) >= 0) { 217 ret = 0; 218 goto out; 219 } 220 221 struct bkey_i *new = k->k; 222 if (old.k->type == KEY_TYPE_accounting) { 223 new = bch2_bkey_make_mut_noupdate(trans, bkey_i_to_s_c(k->k)); 224 ret = PTR_ERR_OR_ZERO(new); 225 if (ret) 226 goto out; 227 228 bch2_accounting_accumulate(bkey_i_to_accounting(new), 229 bkey_s_c_to_accounting(old)); 230 } 231 232 trans->journal_res.seq = k->journal_seq; 233 234 ret = bch2_trans_update(trans, &iter, new, BTREE_TRIGGER_norun); 235 out: 236 bch2_trans_iter_exit(trans, &iter); 237 return ret; 238 } 239 240 static int bch2_journal_replay_key(struct btree_trans *trans, 241 struct journal_key *k) 242 { 243 struct btree_iter iter; 244 unsigned iter_flags = 245 BTREE_ITER_intent| 246 BTREE_ITER_not_extents; 247 unsigned update_flags = BTREE_TRIGGER_norun; 248 int ret; 249 250 if (k->overwritten) 251 return 0; 252 253 trans->journal_res.seq = k->journal_seq; 254 255 /* 256 * BTREE_UPDATE_key_cache_reclaim disables key cache lookup/update to 257 * keep the key cache coherent with the underlying btree. Nothing 258 * besides the allocator is doing updates yet so we don't need key cache 259 * coherency for non-alloc btrees, and key cache fills for snapshots 260 * btrees use BTREE_ITER_filter_snapshots, which isn't available until 261 * the snapshots recovery pass runs. 262 */ 263 if (!k->level && k->btree_id == BTREE_ID_alloc) 264 iter_flags |= BTREE_ITER_cached; 265 else 266 update_flags |= BTREE_UPDATE_key_cache_reclaim; 267 268 bch2_trans_node_iter_init(trans, &iter, k->btree_id, k->k->k.p, 269 BTREE_MAX_DEPTH, k->level, 270 iter_flags); 271 ret = bch2_btree_iter_traverse(trans, &iter); 272 if (ret) 273 goto out; 274 275 struct btree_path *path = btree_iter_path(trans, &iter); 276 if (unlikely(!btree_path_node(path, k->level) && 277 !k->allocated)) { 278 struct bch_fs *c = trans->c; 279 280 if (!(c->recovery.passes_complete & (BIT_ULL(BCH_RECOVERY_PASS_scan_for_btree_nodes)| 281 BIT_ULL(BCH_RECOVERY_PASS_check_topology)))) { 282 bch_err(c, "have key in journal replay for btree depth that does not exist, confused"); 283 ret = -EINVAL; 284 } 285 #if 0 286 bch2_trans_iter_exit(trans, &iter); 287 bch2_trans_node_iter_init(trans, &iter, k->btree_id, k->k->k.p, 288 BTREE_MAX_DEPTH, 0, iter_flags); 289 ret = bch2_btree_iter_traverse(trans, &iter) ?: 290 bch2_btree_increase_depth(trans, iter.path, 0) ?: 291 -BCH_ERR_transaction_restart_nested; 292 #endif 293 k->overwritten = true; 294 goto out; 295 } 296 297 /* Must be checked with btree locked: */ 298 if (k->overwritten) 299 goto out; 300 301 if (k->k->k.type == KEY_TYPE_accounting) { 302 struct bkey_i *n = bch2_trans_subbuf_alloc(trans, &trans->accounting, k->k->k.u64s); 303 ret = PTR_ERR_OR_ZERO(n); 304 if (ret) 305 goto out; 306 307 bkey_copy(n, k->k); 308 goto out; 309 } 310 311 ret = bch2_trans_update(trans, &iter, k->k, update_flags); 312 out: 313 bch2_trans_iter_exit(trans, &iter); 314 return ret; 315 } 316 317 static int journal_sort_seq_cmp(const void *_l, const void *_r) 318 { 319 const struct journal_key *l = *((const struct journal_key **)_l); 320 const struct journal_key *r = *((const struct journal_key **)_r); 321 322 /* 323 * Map 0 to U64_MAX, so that keys with journal_seq === 0 come last 324 * 325 * journal_seq == 0 means that the key comes from early repair, and 326 * should be inserted last so as to avoid overflowing the journal 327 */ 328 return cmp_int(l->journal_seq - 1, r->journal_seq - 1); 329 } 330 331 int bch2_journal_replay(struct bch_fs *c) 332 { 333 struct journal_keys *keys = &c->journal_keys; 334 DARRAY(struct journal_key *) keys_sorted = { 0 }; 335 struct journal *j = &c->journal; 336 u64 start_seq = c->journal_replay_seq_start; 337 u64 end_seq = c->journal_replay_seq_start; 338 struct btree_trans *trans = NULL; 339 bool immediate_flush = false; 340 int ret = 0; 341 342 if (keys->nr) { 343 ret = bch2_journal_log_msg(c, "Starting journal replay (%zu keys in entries %llu-%llu)", 344 keys->nr, start_seq, end_seq); 345 if (ret) 346 goto err; 347 } 348 349 BUG_ON(!atomic_read(&keys->ref)); 350 351 move_gap(keys, keys->nr); 352 trans = bch2_trans_get(c); 353 354 /* 355 * Replay accounting keys first: we can't allow the write buffer to 356 * flush accounting keys until we're done 357 */ 358 darray_for_each(*keys, k) { 359 if (!(k->k->k.type == KEY_TYPE_accounting && !k->allocated)) 360 continue; 361 362 cond_resched(); 363 364 ret = commit_do(trans, NULL, NULL, 365 BCH_TRANS_COMMIT_no_enospc| 366 BCH_TRANS_COMMIT_journal_reclaim| 367 BCH_TRANS_COMMIT_skip_accounting_apply| 368 BCH_TRANS_COMMIT_no_journal_res| 369 BCH_WATERMARK_reclaim, 370 bch2_journal_replay_accounting_key(trans, k)); 371 if (bch2_fs_fatal_err_on(ret, c, "error replaying accounting; %s", bch2_err_str(ret))) 372 goto err; 373 374 k->overwritten = true; 375 } 376 377 set_bit(BCH_FS_accounting_replay_done, &c->flags); 378 379 /* 380 * First, attempt to replay keys in sorted order. This is more 381 * efficient - better locality of btree access - but some might fail if 382 * that would cause a journal deadlock. 383 */ 384 darray_for_each(*keys, k) { 385 cond_resched(); 386 387 /* 388 * k->allocated means the key wasn't read in from the journal, 389 * rather it was from early repair code 390 */ 391 if (k->allocated) 392 immediate_flush = true; 393 394 /* Skip fastpath if we're low on space in the journal */ 395 ret = c->journal.watermark ? -1 : 396 commit_do(trans, NULL, NULL, 397 BCH_TRANS_COMMIT_no_enospc| 398 BCH_TRANS_COMMIT_journal_reclaim| 399 BCH_TRANS_COMMIT_skip_accounting_apply| 400 (!k->allocated ? BCH_TRANS_COMMIT_no_journal_res : 0), 401 bch2_journal_replay_key(trans, k)); 402 BUG_ON(!ret && !k->overwritten && k->k->k.type != KEY_TYPE_accounting); 403 if (ret) { 404 ret = darray_push(&keys_sorted, k); 405 if (ret) 406 goto err; 407 } 408 } 409 410 bch2_trans_unlock_long(trans); 411 /* 412 * Now, replay any remaining keys in the order in which they appear in 413 * the journal, unpinning those journal entries as we go: 414 */ 415 sort_nonatomic(keys_sorted.data, keys_sorted.nr, 416 sizeof(keys_sorted.data[0]), 417 journal_sort_seq_cmp, NULL); 418 419 darray_for_each(keys_sorted, kp) { 420 cond_resched(); 421 422 struct journal_key *k = *kp; 423 424 if (k->journal_seq) 425 replay_now_at(j, k->journal_seq); 426 else 427 replay_now_at(j, j->replay_journal_seq_end); 428 429 ret = commit_do(trans, NULL, NULL, 430 BCH_TRANS_COMMIT_no_enospc| 431 BCH_TRANS_COMMIT_skip_accounting_apply| 432 (!k->allocated 433 ? BCH_TRANS_COMMIT_no_journal_res|BCH_WATERMARK_reclaim 434 : 0), 435 bch2_journal_replay_key(trans, k)); 436 if (ret) { 437 struct printbuf buf = PRINTBUF; 438 bch2_btree_id_level_to_text(&buf, k->btree_id, k->level); 439 bch_err_msg(c, ret, "while replaying key at %s:", buf.buf); 440 printbuf_exit(&buf); 441 goto err; 442 } 443 444 BUG_ON(k->btree_id != BTREE_ID_accounting && !k->overwritten); 445 } 446 447 /* 448 * We need to put our btree_trans before calling flush_all_pins(), since 449 * that will use a btree_trans internally 450 */ 451 bch2_trans_put(trans); 452 trans = NULL; 453 454 if (!c->opts.retain_recovery_info && 455 c->recovery.pass_done >= BCH_RECOVERY_PASS_journal_replay) 456 bch2_journal_keys_put_initial(c); 457 458 replay_now_at(j, j->replay_journal_seq_end); 459 j->replay_journal_seq = 0; 460 461 bch2_journal_set_replay_done(j); 462 463 /* if we did any repair, flush it immediately */ 464 if (immediate_flush) { 465 bch2_journal_flush_all_pins(&c->journal); 466 ret = bch2_journal_meta(&c->journal); 467 } 468 469 if (keys->nr) 470 bch2_journal_log_msg(c, "journal replay finished"); 471 err: 472 if (trans) 473 bch2_trans_put(trans); 474 darray_exit(&keys_sorted); 475 bch_err_fn(c, ret); 476 return ret; 477 } 478 479 /* journal replay early: */ 480 481 static int journal_replay_entry_early(struct bch_fs *c, 482 struct jset_entry *entry) 483 { 484 int ret = 0; 485 486 switch (entry->type) { 487 case BCH_JSET_ENTRY_btree_root: { 488 489 if (unlikely(!entry->u64s)) 490 return 0; 491 492 if (fsck_err_on(entry->btree_id >= BTREE_ID_NR_MAX, 493 c, invalid_btree_id, 494 "invalid btree id %u (max %u)", 495 entry->btree_id, BTREE_ID_NR_MAX)) 496 return 0; 497 498 while (entry->btree_id >= c->btree_roots_extra.nr + BTREE_ID_NR) { 499 ret = darray_push(&c->btree_roots_extra, (struct btree_root) { NULL }); 500 if (ret) 501 return ret; 502 } 503 504 struct btree_root *r = bch2_btree_id_root(c, entry->btree_id); 505 506 r->level = entry->level; 507 bkey_copy(&r->key, (struct bkey_i *) entry->start); 508 r->error = 0; 509 r->alive = true; 510 break; 511 } 512 case BCH_JSET_ENTRY_usage: { 513 struct jset_entry_usage *u = 514 container_of(entry, struct jset_entry_usage, entry); 515 516 switch (entry->btree_id) { 517 case BCH_FS_USAGE_key_version: 518 atomic64_set(&c->key_version, le64_to_cpu(u->v)); 519 break; 520 } 521 break; 522 } 523 case BCH_JSET_ENTRY_blacklist: { 524 struct jset_entry_blacklist *bl_entry = 525 container_of(entry, struct jset_entry_blacklist, entry); 526 527 ret = bch2_journal_seq_blacklist_add(c, 528 le64_to_cpu(bl_entry->seq), 529 le64_to_cpu(bl_entry->seq) + 1); 530 break; 531 } 532 case BCH_JSET_ENTRY_blacklist_v2: { 533 struct jset_entry_blacklist_v2 *bl_entry = 534 container_of(entry, struct jset_entry_blacklist_v2, entry); 535 536 ret = bch2_journal_seq_blacklist_add(c, 537 le64_to_cpu(bl_entry->start), 538 le64_to_cpu(bl_entry->end) + 1); 539 break; 540 } 541 case BCH_JSET_ENTRY_clock: { 542 struct jset_entry_clock *clock = 543 container_of(entry, struct jset_entry_clock, entry); 544 545 atomic64_set(&c->io_clock[clock->rw].now, le64_to_cpu(clock->time)); 546 } 547 } 548 fsck_err: 549 return ret; 550 } 551 552 static int journal_replay_early(struct bch_fs *c, 553 struct bch_sb_field_clean *clean) 554 { 555 if (clean) { 556 for (struct jset_entry *entry = clean->start; 557 entry != vstruct_end(&clean->field); 558 entry = vstruct_next(entry)) { 559 int ret = journal_replay_entry_early(c, entry); 560 if (ret) 561 return ret; 562 } 563 } else { 564 struct genradix_iter iter; 565 struct journal_replay *i, **_i; 566 567 genradix_for_each(&c->journal_entries, iter, _i) { 568 i = *_i; 569 570 if (journal_replay_ignore(i)) 571 continue; 572 573 vstruct_for_each(&i->j, entry) { 574 int ret = journal_replay_entry_early(c, entry); 575 if (ret) 576 return ret; 577 } 578 } 579 } 580 581 return 0; 582 } 583 584 /* sb clean section: */ 585 586 static int read_btree_roots(struct bch_fs *c) 587 { 588 struct printbuf buf = PRINTBUF; 589 int ret = 0; 590 591 for (unsigned i = 0; i < btree_id_nr_alive(c); i++) { 592 struct btree_root *r = bch2_btree_id_root(c, i); 593 594 if (!r->alive) 595 continue; 596 597 printbuf_reset(&buf); 598 bch2_btree_id_level_to_text(&buf, i, r->level); 599 600 if (mustfix_fsck_err_on((ret = r->error), 601 c, btree_root_bkey_invalid, 602 "invalid btree root %s", 603 buf.buf) || 604 mustfix_fsck_err_on((ret = r->error = bch2_btree_root_read(c, i, &r->key, r->level)), 605 c, btree_root_read_error, 606 "error reading btree root %s: %s", 607 buf.buf, bch2_err_str(ret))) { 608 if (btree_id_is_alloc(i)) 609 r->error = 0; 610 ret = 0; 611 } 612 } 613 614 for (unsigned i = 0; i < BTREE_ID_NR; i++) { 615 struct btree_root *r = bch2_btree_id_root(c, i); 616 617 if (!r->b && !r->error) { 618 r->alive = false; 619 r->level = 0; 620 bch2_btree_root_alloc_fake(c, i, 0); 621 } 622 } 623 fsck_err: 624 printbuf_exit(&buf); 625 return ret; 626 } 627 628 static bool check_version_upgrade(struct bch_fs *c) 629 { 630 unsigned latest_version = bcachefs_metadata_version_current; 631 unsigned latest_compatible = min(latest_version, 632 bch2_latest_compatible_version(c->sb.version)); 633 unsigned old_version = c->sb.version_upgrade_complete ?: c->sb.version; 634 unsigned new_version = 0; 635 bool ret = false; 636 637 if (old_version < bcachefs_metadata_required_upgrade_below) { 638 if (c->opts.version_upgrade == BCH_VERSION_UPGRADE_incompatible || 639 latest_compatible < bcachefs_metadata_required_upgrade_below) 640 new_version = latest_version; 641 else 642 new_version = latest_compatible; 643 } else { 644 switch (c->opts.version_upgrade) { 645 case BCH_VERSION_UPGRADE_compatible: 646 new_version = latest_compatible; 647 break; 648 case BCH_VERSION_UPGRADE_incompatible: 649 new_version = latest_version; 650 break; 651 case BCH_VERSION_UPGRADE_none: 652 new_version = min(old_version, latest_version); 653 break; 654 } 655 } 656 657 if (new_version > old_version) { 658 struct printbuf buf = PRINTBUF; 659 660 if (old_version < bcachefs_metadata_required_upgrade_below) 661 prt_str(&buf, "Version upgrade required:\n"); 662 663 if (old_version != c->sb.version) { 664 prt_str(&buf, "Version upgrade from "); 665 bch2_version_to_text(&buf, c->sb.version_upgrade_complete); 666 prt_str(&buf, " to "); 667 bch2_version_to_text(&buf, c->sb.version); 668 prt_str(&buf, " incomplete\n"); 669 } 670 671 prt_printf(&buf, "Doing %s version upgrade from ", 672 BCH_VERSION_MAJOR(old_version) != BCH_VERSION_MAJOR(new_version) 673 ? "incompatible" : "compatible"); 674 bch2_version_to_text(&buf, old_version); 675 prt_str(&buf, " to "); 676 bch2_version_to_text(&buf, new_version); 677 prt_newline(&buf); 678 679 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); 680 __le64 passes = ext->recovery_passes_required[0]; 681 bch2_sb_set_upgrade(c, old_version, new_version); 682 passes = ext->recovery_passes_required[0] & ~passes; 683 684 if (passes) { 685 prt_str(&buf, " running recovery passes: "); 686 prt_bitflags(&buf, bch2_recovery_passes, 687 bch2_recovery_passes_from_stable(le64_to_cpu(passes))); 688 } 689 690 bch_notice(c, "%s", buf.buf); 691 printbuf_exit(&buf); 692 693 ret = true; 694 } 695 696 if (new_version > c->sb.version_incompat_allowed && 697 c->opts.version_upgrade == BCH_VERSION_UPGRADE_incompatible) { 698 struct printbuf buf = PRINTBUF; 699 700 prt_str(&buf, "Now allowing incompatible features up to "); 701 bch2_version_to_text(&buf, new_version); 702 prt_str(&buf, ", previously allowed up to "); 703 bch2_version_to_text(&buf, c->sb.version_incompat_allowed); 704 prt_newline(&buf); 705 706 bch_notice(c, "%s", buf.buf); 707 printbuf_exit(&buf); 708 709 ret = true; 710 } 711 712 if (ret) 713 bch2_sb_upgrade(c, new_version, 714 c->opts.version_upgrade == BCH_VERSION_UPGRADE_incompatible); 715 716 return ret; 717 } 718 719 int bch2_fs_recovery(struct bch_fs *c) 720 { 721 struct bch_sb_field_clean *clean = NULL; 722 struct jset *last_journal_entry = NULL; 723 u64 last_seq = 0, blacklist_seq, journal_seq; 724 int ret = 0; 725 726 if (c->sb.clean) { 727 clean = bch2_read_superblock_clean(c); 728 ret = PTR_ERR_OR_ZERO(clean); 729 if (ret) 730 goto err; 731 732 bch_info(c, "recovering from clean shutdown, journal seq %llu", 733 le64_to_cpu(clean->journal_seq)); 734 } else { 735 bch_info(c, "recovering from unclean shutdown"); 736 } 737 738 if (!(c->sb.features & (1ULL << BCH_FEATURE_new_extent_overwrite))) { 739 bch_err(c, "feature new_extent_overwrite not set, filesystem no longer supported"); 740 ret = -EINVAL; 741 goto err; 742 } 743 744 if (!c->sb.clean && 745 !(c->sb.features & (1ULL << BCH_FEATURE_extents_above_btree_updates))) { 746 bch_err(c, "filesystem needs recovery from older version; run fsck from older bcachefs-tools to fix"); 747 ret = -EINVAL; 748 goto err; 749 } 750 751 if (c->opts.norecovery) { 752 c->opts.recovery_pass_last = c->opts.recovery_pass_last 753 ? min(c->opts.recovery_pass_last, BCH_RECOVERY_PASS_snapshots_read) 754 : BCH_RECOVERY_PASS_snapshots_read; 755 c->opts.nochanges = true; 756 } 757 758 if (c->opts.nochanges) 759 c->opts.read_only = true; 760 761 if (c->opts.journal_rewind) { 762 bch_info(c, "rewinding journal, fsck required"); 763 c->opts.fsck = true; 764 } 765 766 if (go_rw_in_recovery(c)) { 767 /* 768 * start workqueues/kworkers early - kthread creation checks for 769 * pending signals, which is _very_ annoying 770 */ 771 ret = bch2_fs_init_rw(c); 772 if (ret) 773 goto err; 774 } 775 776 mutex_lock(&c->sb_lock); 777 struct bch_sb_field_ext *ext = bch2_sb_field_get(c->disk_sb.sb, ext); 778 bool write_sb = false; 779 780 if (BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb)) { 781 ext->recovery_passes_required[0] |= 782 cpu_to_le64(bch2_recovery_passes_to_stable(BIT_ULL(BCH_RECOVERY_PASS_check_topology))); 783 write_sb = true; 784 } 785 786 u64 sb_passes = bch2_recovery_passes_from_stable(le64_to_cpu(ext->recovery_passes_required[0])); 787 if (sb_passes) { 788 struct printbuf buf = PRINTBUF; 789 prt_str(&buf, "superblock requires following recovery passes to be run:\n "); 790 prt_bitflags(&buf, bch2_recovery_passes, sb_passes); 791 bch_info(c, "%s", buf.buf); 792 printbuf_exit(&buf); 793 } 794 795 if (bch2_check_version_downgrade(c)) { 796 struct printbuf buf = PRINTBUF; 797 798 prt_str(&buf, "Version downgrade required:"); 799 800 __le64 passes = ext->recovery_passes_required[0]; 801 bch2_sb_set_downgrade(c, 802 BCH_VERSION_MINOR(bcachefs_metadata_version_current), 803 BCH_VERSION_MINOR(c->sb.version)); 804 passes = ext->recovery_passes_required[0] & ~passes; 805 if (passes) { 806 prt_str(&buf, "\n running recovery passes: "); 807 prt_bitflags(&buf, bch2_recovery_passes, 808 bch2_recovery_passes_from_stable(le64_to_cpu(passes))); 809 } 810 811 bch_info(c, "%s", buf.buf); 812 printbuf_exit(&buf); 813 write_sb = true; 814 } 815 816 if (check_version_upgrade(c)) 817 write_sb = true; 818 819 c->opts.recovery_passes |= bch2_recovery_passes_from_stable(le64_to_cpu(ext->recovery_passes_required[0])); 820 821 if (c->sb.version_upgrade_complete < bcachefs_metadata_version_autofix_errors) { 822 SET_BCH_SB_ERROR_ACTION(c->disk_sb.sb, BCH_ON_ERROR_fix_safe); 823 write_sb = true; 824 } 825 826 if (write_sb) 827 bch2_write_super(c); 828 mutex_unlock(&c->sb_lock); 829 830 if (c->sb.clean) 831 set_bit(BCH_FS_clean_recovery, &c->flags); 832 if (c->opts.fsck) 833 set_bit(BCH_FS_in_fsck, &c->flags); 834 set_bit(BCH_FS_in_recovery, &c->flags); 835 836 ret = bch2_blacklist_table_initialize(c); 837 if (ret) { 838 bch_err(c, "error initializing blacklist table"); 839 goto err; 840 } 841 842 bch2_journal_pos_from_member_info_resume(c); 843 844 if (!c->sb.clean || c->opts.retain_recovery_info) { 845 struct genradix_iter iter; 846 struct journal_replay **i; 847 848 bch_verbose(c, "starting journal read"); 849 ret = bch2_journal_read(c, &last_seq, &blacklist_seq, &journal_seq); 850 if (ret) 851 goto err; 852 853 /* 854 * note: cmd_list_journal needs the blacklist table fully up to date so 855 * it can asterisk ignored journal entries: 856 */ 857 if (c->opts.read_journal_only) 858 goto out; 859 860 genradix_for_each_reverse(&c->journal_entries, iter, i) 861 if (!journal_replay_ignore(*i)) { 862 last_journal_entry = &(*i)->j; 863 break; 864 } 865 866 if (mustfix_fsck_err_on(c->sb.clean && 867 last_journal_entry && 868 !journal_entry_empty(last_journal_entry), c, 869 clean_but_journal_not_empty, 870 "filesystem marked clean but journal not empty")) { 871 c->sb.compat &= ~(1ULL << BCH_COMPAT_alloc_info); 872 SET_BCH_SB_CLEAN(c->disk_sb.sb, false); 873 c->sb.clean = false; 874 } 875 876 if (!last_journal_entry) { 877 fsck_err_on(!c->sb.clean, c, 878 dirty_but_no_journal_entries, 879 "no journal entries found"); 880 if (clean) 881 goto use_clean; 882 883 genradix_for_each_reverse(&c->journal_entries, iter, i) 884 if (*i) { 885 last_journal_entry = &(*i)->j; 886 (*i)->ignore_blacklisted = false; 887 (*i)->ignore_not_dirty= false; 888 /* 889 * This was probably a NO_FLUSH entry, 890 * so last_seq was garbage - but we know 891 * we're only using a single journal 892 * entry, set it here: 893 */ 894 (*i)->j.last_seq = (*i)->j.seq; 895 break; 896 } 897 } 898 899 ret = bch2_journal_keys_sort(c); 900 if (ret) 901 goto err; 902 903 if (c->sb.clean && last_journal_entry) { 904 ret = bch2_verify_superblock_clean(c, &clean, 905 last_journal_entry); 906 if (ret) 907 goto err; 908 } 909 } else { 910 use_clean: 911 if (!clean) { 912 bch_err(c, "no superblock clean section found"); 913 ret = bch_err_throw(c, fsck_repair_impossible); 914 goto err; 915 916 } 917 blacklist_seq = journal_seq = le64_to_cpu(clean->journal_seq) + 1; 918 } 919 920 c->journal_replay_seq_start = last_seq; 921 c->journal_replay_seq_end = blacklist_seq - 1; 922 923 zero_out_btree_mem_ptr(&c->journal_keys); 924 925 ret = journal_replay_early(c, clean); 926 if (ret) 927 goto err; 928 929 ret = bch2_fs_resize_on_mount(c); 930 if (ret) { 931 up_write(&c->state_lock); 932 goto err; 933 } 934 935 if (c->sb.features & BIT_ULL(BCH_FEATURE_small_image)) { 936 bch_info(c, "filesystem is an unresized image file, mounting ro"); 937 c->opts.read_only = true; 938 } 939 940 if (!c->opts.read_only && 941 (c->sb.features & BIT_ULL(BCH_FEATURE_no_alloc_info))) { 942 bch_info(c, "mounting a filesystem with no alloc info read-write; will recreate"); 943 944 bch2_reconstruct_alloc(c); 945 } else if (c->opts.reconstruct_alloc) { 946 bch2_journal_log_msg(c, "dropping alloc info"); 947 bch_info(c, "dropping and reconstructing all alloc info"); 948 949 bch2_reconstruct_alloc(c); 950 } 951 952 if (c->sb.features & BIT_ULL(BCH_FEATURE_no_alloc_info)) { 953 /* We can't go RW to fix errors without alloc info */ 954 if (c->opts.fix_errors == FSCK_FIX_yes || 955 c->opts.fix_errors == FSCK_FIX_ask) 956 c->opts.fix_errors = FSCK_FIX_no; 957 if (c->opts.errors == BCH_ON_ERROR_fix_safe) 958 c->opts.errors = BCH_ON_ERROR_continue; 959 } 960 961 /* 962 * After an unclean shutdown, skip then next few journal sequence 963 * numbers as they may have been referenced by btree writes that 964 * happened before their corresponding journal writes - those btree 965 * writes need to be ignored, by skipping and blacklisting the next few 966 * journal sequence numbers: 967 */ 968 if (!c->sb.clean) 969 journal_seq += JOURNAL_BUF_NR * 4; 970 971 if (blacklist_seq != journal_seq) { 972 ret = bch2_journal_log_msg(c, "blacklisting entries %llu-%llu", 973 blacklist_seq, journal_seq) ?: 974 bch2_journal_seq_blacklist_add(c, 975 blacklist_seq, journal_seq); 976 if (ret) { 977 bch_err_msg(c, ret, "error creating new journal seq blacklist entry"); 978 goto err; 979 } 980 } 981 982 ret = bch2_journal_log_msg(c, "starting journal at entry %llu, replaying %llu-%llu", 983 journal_seq, last_seq, blacklist_seq - 1) ?: 984 bch2_fs_journal_start(&c->journal, last_seq, journal_seq); 985 if (ret) 986 goto err; 987 988 /* 989 * Skip past versions that might have possibly been used (as nonces), 990 * but hadn't had their pointers written: 991 */ 992 if (c->sb.encryption_type && !c->sb.clean) 993 atomic64_add(1 << 16, &c->key_version); 994 995 ret = read_btree_roots(c); 996 if (ret) 997 goto err; 998 999 set_bit(BCH_FS_btree_running, &c->flags); 1000 1001 ret = bch2_sb_set_upgrade_extra(c); 1002 if (ret) 1003 goto err; 1004 1005 ret = bch2_run_recovery_passes(c, 0); 1006 if (ret) 1007 goto err; 1008 1009 /* 1010 * Normally set by the appropriate recovery pass: when cleared, this 1011 * indicates we're in early recovery and btree updates should be done by 1012 * being applied to the journal replay keys. _Must_ be cleared before 1013 * multithreaded use: 1014 */ 1015 set_bit(BCH_FS_may_go_rw, &c->flags); 1016 clear_bit(BCH_FS_in_fsck, &c->flags); 1017 1018 /* in case we don't run journal replay, i.e. norecovery mode */ 1019 set_bit(BCH_FS_accounting_replay_done, &c->flags); 1020 1021 bch2_async_btree_node_rewrites_flush(c); 1022 1023 /* fsync if we fixed errors */ 1024 if (test_bit(BCH_FS_errors_fixed, &c->flags)) { 1025 bch2_journal_flush_all_pins(&c->journal); 1026 bch2_journal_meta(&c->journal); 1027 } 1028 1029 /* If we fixed errors, verify that fs is actually clean now: */ 1030 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) && 1031 test_bit(BCH_FS_errors_fixed, &c->flags) && 1032 !test_bit(BCH_FS_errors_not_fixed, &c->flags) && 1033 !test_bit(BCH_FS_error, &c->flags)) { 1034 bch2_flush_fsck_errs(c); 1035 1036 bch_info(c, "Fixed errors, running fsck a second time to verify fs is clean"); 1037 clear_bit(BCH_FS_errors_fixed, &c->flags); 1038 1039 ret = bch2_run_recovery_passes(c, 1040 BCH_RECOVERY_PASS_check_alloc_info); 1041 if (ret) 1042 goto err; 1043 1044 if (test_bit(BCH_FS_errors_fixed, &c->flags) || 1045 test_bit(BCH_FS_errors_not_fixed, &c->flags)) { 1046 bch_err(c, "Second fsck run was not clean"); 1047 set_bit(BCH_FS_errors_not_fixed, &c->flags); 1048 } 1049 1050 set_bit(BCH_FS_errors_fixed, &c->flags); 1051 } 1052 1053 if (enabled_qtypes(c)) { 1054 bch_verbose(c, "reading quotas"); 1055 ret = bch2_fs_quota_read(c); 1056 if (ret) 1057 goto err; 1058 bch_verbose(c, "quotas done"); 1059 } 1060 1061 mutex_lock(&c->sb_lock); 1062 ext = bch2_sb_field_get(c->disk_sb.sb, ext); 1063 write_sb = false; 1064 1065 if (BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb) != le16_to_cpu(c->disk_sb.sb->version)) { 1066 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, le16_to_cpu(c->disk_sb.sb->version)); 1067 write_sb = true; 1068 } 1069 1070 if (!test_bit(BCH_FS_error, &c->flags) && 1071 !(c->disk_sb.sb->compat[0] & cpu_to_le64(1ULL << BCH_COMPAT_alloc_info))) { 1072 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_alloc_info); 1073 write_sb = true; 1074 } 1075 1076 if (!test_bit(BCH_FS_error, &c->flags) && 1077 !bch2_is_zero(ext->errors_silent, sizeof(ext->errors_silent))) { 1078 memset(ext->errors_silent, 0, sizeof(ext->errors_silent)); 1079 write_sb = true; 1080 } 1081 1082 if (c->opts.fsck && 1083 !test_bit(BCH_FS_error, &c->flags) && 1084 c->recovery.pass_done == BCH_RECOVERY_PASS_NR - 1 && 1085 ext->btrees_lost_data) { 1086 ext->btrees_lost_data = 0; 1087 write_sb = true; 1088 } 1089 1090 if (c->opts.fsck && 1091 !test_bit(BCH_FS_error, &c->flags) && 1092 !test_bit(BCH_FS_errors_not_fixed, &c->flags)) { 1093 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 0); 1094 SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 0); 1095 write_sb = true; 1096 } 1097 1098 if (bch2_blacklist_entries_gc(c)) 1099 write_sb = true; 1100 1101 if (write_sb) 1102 bch2_write_super(c); 1103 mutex_unlock(&c->sb_lock); 1104 1105 if (!(c->sb.compat & (1ULL << BCH_COMPAT_extents_above_btree_updates_done)) || 1106 c->sb.version_min < bcachefs_metadata_version_btree_ptr_sectors_written) { 1107 struct bch_move_stats stats; 1108 1109 bch2_move_stats_init(&stats, "recovery"); 1110 1111 struct printbuf buf = PRINTBUF; 1112 bch2_version_to_text(&buf, c->sb.version_min); 1113 bch_info(c, "scanning for old btree nodes: min_version %s", buf.buf); 1114 printbuf_exit(&buf); 1115 1116 ret = bch2_fs_read_write_early(c) ?: 1117 bch2_scan_old_btree_nodes(c, &stats); 1118 if (ret) 1119 goto err; 1120 bch_info(c, "scanning for old btree nodes done"); 1121 } 1122 1123 ret = 0; 1124 out: 1125 bch2_flush_fsck_errs(c); 1126 1127 if (!ret && 1128 test_bit(BCH_FS_need_delete_dead_snapshots, &c->flags) && 1129 !c->opts.nochanges) { 1130 bch2_fs_read_write_early(c); 1131 bch2_delete_dead_snapshots_async(c); 1132 } 1133 1134 bch_err_fn(c, ret); 1135 final_out: 1136 if (!IS_ERR(clean)) 1137 kfree(clean); 1138 return ret; 1139 err: 1140 fsck_err: 1141 { 1142 struct printbuf buf = PRINTBUF; 1143 bch2_log_msg_start(c, &buf); 1144 1145 prt_printf(&buf, "error in recovery: %s\n", bch2_err_str(ret)); 1146 bch2_fs_emergency_read_only2(c, &buf); 1147 1148 bch2_print_str(c, KERN_ERR, buf.buf); 1149 printbuf_exit(&buf); 1150 } 1151 goto final_out; 1152 } 1153 1154 int bch2_fs_initialize(struct bch_fs *c) 1155 { 1156 struct bch_inode_unpacked root_inode, lostfound_inode; 1157 struct bkey_inode_buf packed_inode; 1158 struct qstr lostfound = QSTR("lost+found"); 1159 struct bch_member *m; 1160 int ret; 1161 1162 bch_notice(c, "initializing new filesystem"); 1163 set_bit(BCH_FS_new_fs, &c->flags); 1164 1165 mutex_lock(&c->sb_lock); 1166 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_extents_above_btree_updates_done); 1167 c->disk_sb.sb->compat[0] |= cpu_to_le64(1ULL << BCH_COMPAT_bformat_overflow_done); 1168 1169 bch2_check_version_downgrade(c); 1170 1171 if (c->opts.version_upgrade != BCH_VERSION_UPGRADE_none) { 1172 bch2_sb_upgrade(c, bcachefs_metadata_version_current, false); 1173 SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current); 1174 bch2_write_super(c); 1175 } 1176 1177 for_each_member_device(c, ca) { 1178 m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 1179 SET_BCH_MEMBER_FREESPACE_INITIALIZED(m, false); 1180 ca->mi = bch2_mi_to_cpu(m); 1181 } 1182 1183 bch2_write_super(c); 1184 mutex_unlock(&c->sb_lock); 1185 1186 set_bit(BCH_FS_btree_running, &c->flags); 1187 set_bit(BCH_FS_may_go_rw, &c->flags); 1188 1189 for (unsigned i = 0; i < BTREE_ID_NR; i++) 1190 bch2_btree_root_alloc_fake(c, i, 0); 1191 1192 ret = bch2_fs_journal_alloc(c); 1193 if (ret) 1194 goto err; 1195 1196 /* 1197 * journal_res_get() will crash if called before this has 1198 * set up the journal.pin FIFO and journal.cur pointer: 1199 */ 1200 ret = bch2_fs_journal_start(&c->journal, 1, 1); 1201 if (ret) 1202 goto err; 1203 1204 ret = bch2_fs_read_write_early(c); 1205 if (ret) 1206 goto err; 1207 1208 set_bit(BCH_FS_accounting_replay_done, &c->flags); 1209 bch2_journal_set_replay_done(&c->journal); 1210 1211 for_each_member_device(c, ca) { 1212 ret = bch2_dev_usage_init(ca, false); 1213 if (ret) { 1214 bch2_dev_put(ca); 1215 goto err; 1216 } 1217 } 1218 1219 /* 1220 * Write out the superblock and journal buckets, now that we can do 1221 * btree updates 1222 */ 1223 bch_verbose(c, "marking superblocks"); 1224 ret = bch2_trans_mark_dev_sbs(c); 1225 bch_err_msg(c, ret, "marking superblocks"); 1226 if (ret) 1227 goto err; 1228 1229 ret = bch2_fs_freespace_init(c); 1230 if (ret) 1231 goto err; 1232 1233 ret = bch2_initialize_subvolumes(c); 1234 if (ret) 1235 goto err; 1236 1237 bch_verbose(c, "reading snapshots table"); 1238 ret = bch2_snapshots_read(c); 1239 if (ret) 1240 goto err; 1241 bch_verbose(c, "reading snapshots done"); 1242 1243 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755, 0, NULL); 1244 root_inode.bi_inum = BCACHEFS_ROOT_INO; 1245 root_inode.bi_subvol = BCACHEFS_ROOT_SUBVOL; 1246 bch2_inode_pack(&packed_inode, &root_inode); 1247 packed_inode.inode.k.p.snapshot = U32_MAX; 1248 1249 ret = bch2_btree_insert(c, BTREE_ID_inodes, &packed_inode.inode.k_i, NULL, 0, 0); 1250 bch_err_msg(c, ret, "creating root directory"); 1251 if (ret) 1252 goto err; 1253 1254 bch2_inode_init_early(c, &lostfound_inode); 1255 1256 ret = bch2_trans_commit_do(c, NULL, NULL, 0, 1257 bch2_create_trans(trans, 1258 BCACHEFS_ROOT_SUBVOL_INUM, 1259 &root_inode, &lostfound_inode, 1260 &lostfound, 1261 0, 0, S_IFDIR|0700, 0, 1262 NULL, NULL, (subvol_inum) { 0 }, 0)); 1263 bch_err_msg(c, ret, "creating lost+found"); 1264 if (ret) 1265 goto err; 1266 1267 c->recovery.pass_done = BCH_RECOVERY_PASS_NR - 1; 1268 1269 bch2_copygc_wakeup(c); 1270 bch2_rebalance_wakeup(c); 1271 1272 if (enabled_qtypes(c)) { 1273 ret = bch2_fs_quota_read(c); 1274 if (ret) 1275 goto err; 1276 } 1277 1278 ret = bch2_journal_flush(&c->journal); 1279 bch_err_msg(c, ret, "writing first journal entry"); 1280 if (ret) 1281 goto err; 1282 1283 mutex_lock(&c->sb_lock); 1284 SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true); 1285 SET_BCH_SB_CLEAN(c->disk_sb.sb, false); 1286 1287 bch2_write_super(c); 1288 mutex_unlock(&c->sb_lock); 1289 1290 c->recovery.curr_pass = BCH_RECOVERY_PASS_NR; 1291 return 0; 1292 err: 1293 bch_err_fn(c, ret); 1294 return ret; 1295 } 1296