]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/recovery.c
8ecd4abc8eebdffdf5ca12154df75c874e7a4df6
[bcachefs-tools-debian] / libbcachefs / recovery.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "alloc_background.h"
5 #include "btree_gc.h"
6 #include "btree_update.h"
7 #include "btree_update_interior.h"
8 #include "btree_io.h"
9 #include "buckets.h"
10 #include "dirent.h"
11 #include "ec.h"
12 #include "error.h"
13 #include "fs-common.h"
14 #include "fsck.h"
15 #include "journal_io.h"
16 #include "journal_reclaim.h"
17 #include "journal_seq_blacklist.h"
18 #include "quota.h"
19 #include "recovery.h"
20 #include "replicas.h"
21 #include "super-io.h"
22
23 #include <linux/sort.h>
24 #include <linux/stat.h>
25
26 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
27
28 /* iterate over keys read from the journal: */
29
30 struct journal_iter bch2_journal_iter_init(struct journal_keys *keys,
31                                            enum btree_id id)
32 {
33         return (struct journal_iter) {
34                 .keys           = keys,
35                 .k              = keys->d,
36                 .btree_id       = id,
37         };
38 }
39
40 struct bkey_s_c bch2_journal_iter_peek(struct journal_iter *iter)
41 {
42         while (1) {
43                 if (iter->k == iter->keys->d + iter->keys->nr)
44                         return bkey_s_c_null;
45
46                 if (iter->k->btree_id == iter->btree_id)
47                         return bkey_i_to_s_c(iter->k->k);
48
49                 iter->k++;
50         }
51
52         return bkey_s_c_null;
53 }
54
55 struct bkey_s_c bch2_journal_iter_next(struct journal_iter *iter)
56 {
57         if (iter->k == iter->keys->d + iter->keys->nr)
58                 return bkey_s_c_null;
59
60         iter->k++;
61         return bch2_journal_iter_peek(iter);
62 }
63
64 /* sort and dedup all keys in the journal: */
65
66 static void journal_entries_free(struct list_head *list)
67 {
68
69         while (!list_empty(list)) {
70                 struct journal_replay *i =
71                         list_first_entry(list, struct journal_replay, list);
72                 list_del(&i->list);
73                 kvpfree(i, offsetof(struct journal_replay, j) +
74                         vstruct_bytes(&i->j));
75         }
76 }
77
78 static int journal_sort_key_cmp(const void *_l, const void *_r)
79 {
80         const struct journal_key *l = _l;
81         const struct journal_key *r = _r;
82
83         return cmp_int(l->btree_id, r->btree_id) ?:
84                 bkey_cmp(l->pos, r->pos) ?:
85                 cmp_int(l->journal_seq, r->journal_seq) ?:
86                 cmp_int(l->journal_offset, r->journal_offset);
87 }
88
89 static int journal_sort_seq_cmp(const void *_l, const void *_r)
90 {
91         const struct journal_key *l = _l;
92         const struct journal_key *r = _r;
93
94         return cmp_int(l->journal_seq, r->journal_seq) ?:
95                 cmp_int(l->btree_id, r->btree_id) ?:
96                 bkey_cmp(l->pos, r->pos);
97 }
98
99 static void journal_keys_sift(struct journal_keys *keys, struct journal_key *i)
100 {
101         while (i + 1 < keys->d + keys->nr &&
102                journal_sort_key_cmp(i, i + 1) > 0) {
103                 swap(i[0], i[1]);
104                 i++;
105         }
106 }
107
108 static void journal_keys_free(struct journal_keys *keys)
109 {
110         struct journal_key *i;
111
112         for_each_journal_key(*keys, i)
113                 if (i->allocated)
114                         kfree(i->k);
115         kvfree(keys->d);
116         keys->d = NULL;
117         keys->nr = 0;
118 }
119
120 static struct journal_keys journal_keys_sort(struct list_head *journal_entries)
121 {
122         struct journal_replay *p;
123         struct jset_entry *entry;
124         struct bkey_i *k, *_n;
125         struct journal_keys keys = { NULL }, keys_deduped = { NULL };
126         struct journal_key *i;
127         size_t nr_keys = 0;
128
129         list_for_each_entry(p, journal_entries, list)
130                 for_each_jset_key(k, _n, entry, &p->j)
131                         nr_keys++;
132
133         keys.journal_seq_base = keys_deduped.journal_seq_base =
134                 le64_to_cpu(list_first_entry(journal_entries,
135                                              struct journal_replay,
136                                              list)->j.seq);
137
138         keys.d = kvmalloc(sizeof(keys.d[0]) * nr_keys, GFP_KERNEL);
139         if (!keys.d)
140                 goto err;
141
142         keys_deduped.d = kvmalloc(sizeof(keys.d[0]) * nr_keys * 2, GFP_KERNEL);
143         if (!keys_deduped.d)
144                 goto err;
145
146         list_for_each_entry(p, journal_entries, list)
147                 for_each_jset_key(k, _n, entry, &p->j)
148                         keys.d[keys.nr++] = (struct journal_key) {
149                                 .btree_id       = entry->btree_id,
150                                 .pos            = bkey_start_pos(&k->k),
151                                 .k              = k,
152                                 .journal_seq    = le64_to_cpu(p->j.seq) -
153                                         keys.journal_seq_base,
154                                 .journal_offset = k->_data - p->j._data,
155                         };
156
157         sort(keys.d, nr_keys, sizeof(keys.d[0]), journal_sort_key_cmp, NULL);
158
159         i = keys.d;
160         while (i < keys.d + keys.nr) {
161                 if (i + 1 < keys.d + keys.nr &&
162                     i[0].btree_id == i[1].btree_id &&
163                     !bkey_cmp(i[0].pos, i[1].pos)) {
164                         if (bkey_cmp(i[0].k->k.p, i[1].k->k.p) <= 0) {
165                                 i++;
166                         } else {
167                                 bch2_cut_front(i[1].k->k.p, i[0].k);
168                                 i[0].pos = i[1].k->k.p;
169                                 journal_keys_sift(&keys, i);
170                         }
171                         continue;
172                 }
173
174                 if (i + 1 < keys.d + keys.nr &&
175                     i[0].btree_id == i[1].btree_id &&
176                     bkey_cmp(i[0].k->k.p, bkey_start_pos(&i[1].k->k)) > 0) {
177                         if ((cmp_int(i[0].journal_seq, i[1].journal_seq) ?:
178                              cmp_int(i[0].journal_offset, i[1].journal_offset)) < 0) {
179                                 if (bkey_cmp(i[0].k->k.p, i[1].k->k.p) <= 0) {
180                                         bch2_cut_back(bkey_start_pos(&i[1].k->k), i[0].k);
181                                 } else {
182                                         struct bkey_i *split =
183                                                 kmalloc(bkey_bytes(i[0].k), GFP_KERNEL);
184
185                                         if (!split)
186                                                 goto err;
187
188                                         bkey_copy(split, i[0].k);
189                                         bch2_cut_back(bkey_start_pos(&i[1].k->k), split);
190                                         keys_deduped.d[keys_deduped.nr++] = (struct journal_key) {
191                                                 .btree_id       = i[0].btree_id,
192                                                 .allocated      = true,
193                                                 .pos            = bkey_start_pos(&split->k),
194                                                 .k              = split,
195                                                 .journal_seq    = i[0].journal_seq,
196                                                 .journal_offset = i[0].journal_offset,
197                                         };
198
199                                         bch2_cut_front(i[1].k->k.p, i[0].k);
200                                         i[0].pos = i[1].k->k.p;
201                                         journal_keys_sift(&keys, i);
202                                         continue;
203                                 }
204                         } else {
205                                 if (bkey_cmp(i[0].k->k.p, i[1].k->k.p) >= 0) {
206                                         i[1] = i[0];
207                                         i++;
208                                         continue;
209                                 } else {
210                                         bch2_cut_front(i[0].k->k.p, i[1].k);
211                                         i[1].pos = i[0].k->k.p;
212                                         journal_keys_sift(&keys, i + 1);
213                                         continue;
214                                 }
215                         }
216                 }
217
218                 keys_deduped.d[keys_deduped.nr++] = *i++;
219         }
220
221         kvfree(keys.d);
222         return keys_deduped;
223 err:
224         journal_keys_free(&keys_deduped);
225         kvfree(keys.d);
226         return (struct journal_keys) { NULL };
227 }
228
229 /* journal replay: */
230
231 static void replay_now_at(struct journal *j, u64 seq)
232 {
233         BUG_ON(seq < j->replay_journal_seq);
234         BUG_ON(seq > j->replay_journal_seq_end);
235
236         while (j->replay_journal_seq < seq)
237                 bch2_journal_pin_put(j, j->replay_journal_seq++);
238 }
239
240 static int bch2_extent_replay_key(struct bch_fs *c, enum btree_id btree_id,
241                                   struct bkey_i *k)
242 {
243         struct btree_trans trans;
244         struct btree_iter *iter, *split_iter;
245         /*
246          * We might cause compressed extents to be split, so we need to pass in
247          * a disk_reservation:
248          */
249         struct disk_reservation disk_res =
250                 bch2_disk_reservation_init(c, 0);
251         struct bkey_i *split;
252         struct bpos atomic_end;
253         /*
254          * Some extents aren't equivalent - w.r.t. what the triggers do
255          * - if they're split:
256          */
257         bool remark_if_split = bch2_bkey_sectors_compressed(bkey_i_to_s_c(k)) ||
258                 k->k.type == KEY_TYPE_reflink_p;
259         bool remark = false;
260         int ret;
261
262         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
263 retry:
264         bch2_trans_begin(&trans);
265
266         iter = bch2_trans_get_iter(&trans, btree_id,
267                                    bkey_start_pos(&k->k),
268                                    BTREE_ITER_INTENT);
269
270         do {
271                 ret = bch2_btree_iter_traverse(iter);
272                 if (ret)
273                         goto err;
274
275                 atomic_end = bpos_min(k->k.p, iter->l[0].b->key.k.p);
276
277                 split_iter = bch2_trans_copy_iter(&trans, iter);
278                 ret = PTR_ERR_OR_ZERO(split_iter);
279                 if (ret)
280                         goto err;
281
282                 split = bch2_trans_kmalloc(&trans, bkey_bytes(&k->k));
283                 ret = PTR_ERR_OR_ZERO(split);
284                 if (ret)
285                         goto err;
286
287                 if (!remark &&
288                     remark_if_split &&
289                     bkey_cmp(atomic_end, k->k.p) < 0) {
290                         ret = bch2_disk_reservation_add(c, &disk_res,
291                                         k->k.size *
292                                         bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(k)),
293                                         BCH_DISK_RESERVATION_NOFAIL);
294                         BUG_ON(ret);
295
296                         remark = true;
297                 }
298
299                 bkey_copy(split, k);
300                 bch2_cut_front(split_iter->pos, split);
301                 bch2_cut_back(atomic_end, split);
302
303                 bch2_trans_update(&trans, split_iter, split, !remark
304                                   ? BTREE_TRIGGER_NORUN
305                                   : BTREE_TRIGGER_NOOVERWRITES);
306                 bch2_btree_iter_set_pos(iter, split->k.p);
307         } while (bkey_cmp(iter->pos, k->k.p) < 0);
308
309         if (remark) {
310                 ret = bch2_trans_mark_key(&trans, bkey_i_to_s_c(k),
311                                           0, -((s64) k->k.size),
312                                           BTREE_TRIGGER_OVERWRITE);
313                 if (ret)
314                         goto err;
315         }
316
317         ret = bch2_trans_commit(&trans, &disk_res, NULL,
318                                 BTREE_INSERT_NOFAIL|
319                                 BTREE_INSERT_LAZY_RW|
320                                 BTREE_INSERT_JOURNAL_REPLAY);
321 err:
322         if (ret == -EINTR)
323                 goto retry;
324
325         bch2_disk_reservation_put(c, &disk_res);
326
327         return bch2_trans_exit(&trans) ?: ret;
328 }
329
330 static int __bch2_journal_replay_key(struct btree_trans *trans,
331                                      enum btree_id id, struct bkey_i *k)
332 {
333         struct btree_iter *iter;
334
335         iter = bch2_trans_get_iter(trans, id, bkey_start_pos(&k->k),
336                                    BTREE_ITER_INTENT);
337         if (IS_ERR(iter))
338                 return PTR_ERR(iter);
339
340         bch2_trans_update(trans, iter, k, BTREE_TRIGGER_NORUN);
341         return 0;
342 }
343
344 static int bch2_journal_replay_key(struct bch_fs *c, enum btree_id id,
345                                    struct bkey_i *k)
346 {
347         return bch2_trans_do(c, NULL, NULL,
348                              BTREE_INSERT_NOFAIL|
349                              BTREE_INSERT_LAZY_RW|
350                              BTREE_INSERT_JOURNAL_REPLAY,
351                              __bch2_journal_replay_key(&trans, id, k));
352 }
353
354 static int bch2_journal_replay(struct bch_fs *c,
355                                struct journal_keys keys)
356 {
357         struct journal *j = &c->journal;
358         struct journal_key *i;
359         int ret;
360
361         sort(keys.d, keys.nr, sizeof(keys.d[0]), journal_sort_seq_cmp, NULL);
362
363         for_each_journal_key(keys, i) {
364                 replay_now_at(j, keys.journal_seq_base + i->journal_seq);
365
366                 if (i->btree_id == BTREE_ID_ALLOC)
367                         ret = bch2_alloc_replay_key(c, i->k);
368                 else if (btree_node_type_is_extents(i->btree_id))
369                         ret = bch2_extent_replay_key(c, i->btree_id, i->k);
370                 else
371                         ret = bch2_journal_replay_key(c, i->btree_id, i->k);
372
373                 if (ret) {
374                         bch_err(c, "journal replay: error %d while replaying key",
375                                 ret);
376                         return ret;
377                 }
378
379                 cond_resched();
380         }
381
382         replay_now_at(j, j->replay_journal_seq_end);
383         j->replay_journal_seq = 0;
384
385         bch2_journal_set_replay_done(j);
386         bch2_journal_flush_all_pins(j);
387         return bch2_journal_error(j);
388 }
389
390 static bool journal_empty(struct list_head *journal)
391 {
392         return list_empty(journal) ||
393                 journal_entry_empty(&list_last_entry(journal,
394                                         struct journal_replay, list)->j);
395 }
396
397 static int
398 verify_journal_entries_not_blacklisted_or_missing(struct bch_fs *c,
399                                                   struct list_head *journal)
400 {
401         struct journal_replay *i =
402                 list_last_entry(journal, struct journal_replay, list);
403         u64 start_seq   = le64_to_cpu(i->j.last_seq);
404         u64 end_seq     = le64_to_cpu(i->j.seq);
405         u64 seq         = start_seq;
406         int ret = 0;
407
408         list_for_each_entry(i, journal, list) {
409                 fsck_err_on(seq != le64_to_cpu(i->j.seq), c,
410                         "journal entries %llu-%llu missing! (replaying %llu-%llu)",
411                         seq, le64_to_cpu(i->j.seq) - 1,
412                         start_seq, end_seq);
413
414                 seq = le64_to_cpu(i->j.seq);
415
416                 fsck_err_on(bch2_journal_seq_is_blacklisted(c, seq, false), c,
417                             "found blacklisted journal entry %llu", seq);
418
419                 do {
420                         seq++;
421                 } while (bch2_journal_seq_is_blacklisted(c, seq, false));
422         }
423 fsck_err:
424         return ret;
425 }
426
427 /* journal replay early: */
428
429 static int journal_replay_entry_early(struct bch_fs *c,
430                                       struct jset_entry *entry)
431 {
432         int ret = 0;
433
434         switch (entry->type) {
435         case BCH_JSET_ENTRY_btree_root: {
436                 struct btree_root *r;
437
438                 if (entry->btree_id >= BTREE_ID_NR) {
439                         bch_err(c, "filesystem has unknown btree type %u",
440                                 entry->btree_id);
441                         return -EINVAL;
442                 }
443
444                 r = &c->btree_roots[entry->btree_id];
445
446                 if (entry->u64s) {
447                         r->level = entry->level;
448                         bkey_copy(&r->key, &entry->start[0]);
449                         r->error = 0;
450                 } else {
451                         r->error = -EIO;
452                 }
453                 r->alive = true;
454                 break;
455         }
456         case BCH_JSET_ENTRY_usage: {
457                 struct jset_entry_usage *u =
458                         container_of(entry, struct jset_entry_usage, entry);
459
460                 switch (entry->btree_id) {
461                 case FS_USAGE_RESERVED:
462                         if (entry->level < BCH_REPLICAS_MAX)
463                                 c->usage_base->persistent_reserved[entry->level] =
464                                         le64_to_cpu(u->v);
465                         break;
466                 case FS_USAGE_INODES:
467                         c->usage_base->nr_inodes = le64_to_cpu(u->v);
468                         break;
469                 case FS_USAGE_KEY_VERSION:
470                         atomic64_set(&c->key_version,
471                                      le64_to_cpu(u->v));
472                         break;
473                 }
474
475                 break;
476         }
477         case BCH_JSET_ENTRY_data_usage: {
478                 struct jset_entry_data_usage *u =
479                         container_of(entry, struct jset_entry_data_usage, entry);
480                 ret = bch2_replicas_set_usage(c, &u->r,
481                                               le64_to_cpu(u->v));
482                 break;
483         }
484         case BCH_JSET_ENTRY_blacklist: {
485                 struct jset_entry_blacklist *bl_entry =
486                         container_of(entry, struct jset_entry_blacklist, entry);
487
488                 ret = bch2_journal_seq_blacklist_add(c,
489                                 le64_to_cpu(bl_entry->seq),
490                                 le64_to_cpu(bl_entry->seq) + 1);
491                 break;
492         }
493         case BCH_JSET_ENTRY_blacklist_v2: {
494                 struct jset_entry_blacklist_v2 *bl_entry =
495                         container_of(entry, struct jset_entry_blacklist_v2, entry);
496
497                 ret = bch2_journal_seq_blacklist_add(c,
498                                 le64_to_cpu(bl_entry->start),
499                                 le64_to_cpu(bl_entry->end) + 1);
500                 break;
501         }
502         }
503
504         return ret;
505 }
506
507 static int journal_replay_early(struct bch_fs *c,
508                                 struct bch_sb_field_clean *clean,
509                                 struct list_head *journal)
510 {
511         struct jset_entry *entry;
512         int ret;
513
514         if (clean) {
515                 c->bucket_clock[READ].hand = le16_to_cpu(clean->read_clock);
516                 c->bucket_clock[WRITE].hand = le16_to_cpu(clean->write_clock);
517
518                 for (entry = clean->start;
519                      entry != vstruct_end(&clean->field);
520                      entry = vstruct_next(entry)) {
521                         ret = journal_replay_entry_early(c, entry);
522                         if (ret)
523                                 return ret;
524                 }
525         } else {
526                 struct journal_replay *i =
527                         list_last_entry(journal, struct journal_replay, list);
528
529                 c->bucket_clock[READ].hand = le16_to_cpu(i->j.read_clock);
530                 c->bucket_clock[WRITE].hand = le16_to_cpu(i->j.write_clock);
531
532                 list_for_each_entry(i, journal, list)
533                         vstruct_for_each(&i->j, entry) {
534                                 ret = journal_replay_entry_early(c, entry);
535                                 if (ret)
536                                         return ret;
537                         }
538         }
539
540         bch2_fs_usage_initialize(c);
541
542         return 0;
543 }
544
545 /* sb clean section: */
546
547 static struct bkey_i *btree_root_find(struct bch_fs *c,
548                                       struct bch_sb_field_clean *clean,
549                                       struct jset *j,
550                                       enum btree_id id, unsigned *level)
551 {
552         struct bkey_i *k;
553         struct jset_entry *entry, *start, *end;
554
555         if (clean) {
556                 start = clean->start;
557                 end = vstruct_end(&clean->field);
558         } else {
559                 start = j->start;
560                 end = vstruct_last(j);
561         }
562
563         for (entry = start; entry < end; entry = vstruct_next(entry))
564                 if (entry->type == BCH_JSET_ENTRY_btree_root &&
565                     entry->btree_id == id)
566                         goto found;
567
568         return NULL;
569 found:
570         if (!entry->u64s)
571                 return ERR_PTR(-EINVAL);
572
573         k = entry->start;
574         *level = entry->level;
575         return k;
576 }
577
578 static int verify_superblock_clean(struct bch_fs *c,
579                                    struct bch_sb_field_clean **cleanp,
580                                    struct jset *j)
581 {
582         unsigned i;
583         struct bch_sb_field_clean *clean = *cleanp;
584         int ret = 0;
585
586         if (!c->sb.clean || !j)
587                 return 0;
588
589         if (mustfix_fsck_err_on(j->seq != clean->journal_seq, c,
590                         "superblock journal seq (%llu) doesn't match journal (%llu) after clean shutdown",
591                         le64_to_cpu(clean->journal_seq),
592                         le64_to_cpu(j->seq))) {
593                 kfree(clean);
594                 *cleanp = NULL;
595                 return 0;
596         }
597
598         mustfix_fsck_err_on(j->read_clock != clean->read_clock, c,
599                         "superblock read clock doesn't match journal after clean shutdown");
600         mustfix_fsck_err_on(j->write_clock != clean->write_clock, c,
601                         "superblock read clock doesn't match journal after clean shutdown");
602
603         for (i = 0; i < BTREE_ID_NR; i++) {
604                 struct bkey_i *k1, *k2;
605                 unsigned l1 = 0, l2 = 0;
606
607                 k1 = btree_root_find(c, clean, NULL, i, &l1);
608                 k2 = btree_root_find(c, NULL, j, i, &l2);
609
610                 if (!k1 && !k2)
611                         continue;
612
613                 mustfix_fsck_err_on(!k1 || !k2 ||
614                                     IS_ERR(k1) ||
615                                     IS_ERR(k2) ||
616                                     k1->k.u64s != k2->k.u64s ||
617                                     memcmp(k1, k2, bkey_bytes(k1)) ||
618                                     l1 != l2, c,
619                         "superblock btree root doesn't match journal after clean shutdown");
620         }
621 fsck_err:
622         return ret;
623 }
624
625 static struct bch_sb_field_clean *read_superblock_clean(struct bch_fs *c)
626 {
627         struct bch_sb_field_clean *clean, *sb_clean;
628         int ret;
629
630         mutex_lock(&c->sb_lock);
631         sb_clean = bch2_sb_get_clean(c->disk_sb.sb);
632
633         if (fsck_err_on(!sb_clean, c,
634                         "superblock marked clean but clean section not present")) {
635                 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
636                 c->sb.clean = false;
637                 mutex_unlock(&c->sb_lock);
638                 return NULL;
639         }
640
641         clean = kmemdup(sb_clean, vstruct_bytes(&sb_clean->field),
642                         GFP_KERNEL);
643         if (!clean) {
644                 mutex_unlock(&c->sb_lock);
645                 return ERR_PTR(-ENOMEM);
646         }
647
648         if (le16_to_cpu(c->disk_sb.sb->version) <
649             bcachefs_metadata_version_bkey_renumber)
650                 bch2_sb_clean_renumber(clean, READ);
651
652         mutex_unlock(&c->sb_lock);
653
654         return clean;
655 fsck_err:
656         mutex_unlock(&c->sb_lock);
657         return ERR_PTR(ret);
658 }
659
660 static int read_btree_roots(struct bch_fs *c)
661 {
662         unsigned i;
663         int ret = 0;
664
665         for (i = 0; i < BTREE_ID_NR; i++) {
666                 struct btree_root *r = &c->btree_roots[i];
667
668                 if (!r->alive)
669                         continue;
670
671                 if (i == BTREE_ID_ALLOC &&
672                     c->opts.reconstruct_alloc) {
673                         c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
674                         continue;
675                 }
676
677
678                 if (r->error) {
679                         __fsck_err(c, i == BTREE_ID_ALLOC
680                                    ? FSCK_CAN_IGNORE : 0,
681                                    "invalid btree root %s",
682                                    bch2_btree_ids[i]);
683                         if (i == BTREE_ID_ALLOC)
684                                 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
685                 }
686
687                 ret = bch2_btree_root_read(c, i, &r->key, r->level);
688                 if (ret) {
689                         __fsck_err(c, i == BTREE_ID_ALLOC
690                                    ? FSCK_CAN_IGNORE : 0,
691                                    "error reading btree root %s",
692                                    bch2_btree_ids[i]);
693                         if (i == BTREE_ID_ALLOC)
694                                 c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
695                 }
696         }
697
698         for (i = 0; i < BTREE_ID_NR; i++)
699                 if (!c->btree_roots[i].b)
700                         bch2_btree_root_alloc(c, i);
701 fsck_err:
702         return ret;
703 }
704
705 int bch2_fs_recovery(struct bch_fs *c)
706 {
707         const char *err = "cannot allocate memory";
708         struct bch_sb_field_clean *clean = NULL;
709         u64 journal_seq;
710         LIST_HEAD(journal_entries);
711         struct journal_keys journal_keys = { NULL };
712         bool wrote = false, write_sb = false;
713         int ret;
714
715         if (c->sb.clean)
716                 clean = read_superblock_clean(c);
717         ret = PTR_ERR_OR_ZERO(clean);
718         if (ret)
719                 goto err;
720
721         if (c->sb.clean)
722                 bch_info(c, "recovering from clean shutdown, journal seq %llu",
723                          le64_to_cpu(clean->journal_seq));
724
725         if (!c->replicas.entries) {
726                 bch_info(c, "building replicas info");
727                 set_bit(BCH_FS_REBUILD_REPLICAS, &c->flags);
728         }
729
730         if (!c->sb.clean || c->opts.fsck) {
731                 struct jset *j;
732
733                 ret = bch2_journal_read(c, &journal_entries);
734                 if (ret)
735                         goto err;
736
737                 if (mustfix_fsck_err_on(c->sb.clean && !journal_empty(&journal_entries), c,
738                                 "filesystem marked clean but journal not empty")) {
739                         c->sb.compat &= ~(1ULL << BCH_COMPAT_FEAT_ALLOC_INFO);
740                         SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
741                         c->sb.clean = false;
742                 }
743
744                 if (!c->sb.clean && list_empty(&journal_entries)) {
745                         bch_err(c, "no journal entries found");
746                         ret = BCH_FSCK_REPAIR_IMPOSSIBLE;
747                         goto err;
748                 }
749
750                 journal_keys = journal_keys_sort(&journal_entries);
751                 if (!journal_keys.d) {
752                         ret = -ENOMEM;
753                         goto err;
754                 }
755
756                 j = &list_last_entry(&journal_entries,
757                                      struct journal_replay, list)->j;
758
759                 ret = verify_superblock_clean(c, &clean, j);
760                 if (ret)
761                         goto err;
762
763                 journal_seq = le64_to_cpu(j->seq) + 1;
764         } else {
765                 journal_seq = le64_to_cpu(clean->journal_seq) + 1;
766         }
767
768         ret = journal_replay_early(c, clean, &journal_entries);
769         if (ret)
770                 goto err;
771
772         if (!c->sb.clean) {
773                 ret = bch2_journal_seq_blacklist_add(c,
774                                                      journal_seq,
775                                                      journal_seq + 4);
776                 if (ret) {
777                         bch_err(c, "error creating new journal seq blacklist entry");
778                         goto err;
779                 }
780
781                 journal_seq += 4;
782         }
783
784         ret = bch2_blacklist_table_initialize(c);
785
786         if (!list_empty(&journal_entries)) {
787                 ret = verify_journal_entries_not_blacklisted_or_missing(c,
788                                                         &journal_entries);
789                 if (ret)
790                         goto err;
791         }
792
793         ret = bch2_fs_journal_start(&c->journal, journal_seq,
794                                     &journal_entries);
795         if (ret)
796                 goto err;
797
798         ret = read_btree_roots(c);
799         if (ret)
800                 goto err;
801
802         bch_verbose(c, "starting alloc read");
803         err = "error reading allocation information";
804         ret = bch2_alloc_read(c, &journal_keys);
805         if (ret)
806                 goto err;
807         bch_verbose(c, "alloc read done");
808
809         bch_verbose(c, "starting stripes_read");
810         err = "error reading stripes";
811         ret = bch2_stripes_read(c, &journal_keys);
812         if (ret)
813                 goto err;
814         bch_verbose(c, "stripes_read done");
815
816         set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
817
818         if ((c->sb.compat & (1ULL << BCH_COMPAT_FEAT_ALLOC_INFO)) &&
819             !(c->sb.compat & (1ULL << BCH_COMPAT_FEAT_ALLOC_METADATA))) {
820                 /*
821                  * interior btree node updates aren't consistent with the
822                  * journal; after an unclean shutdown we have to walk all
823                  * pointers to metadata:
824                  */
825                 bch_info(c, "starting metadata mark and sweep");
826                 err = "error in mark and sweep";
827                 ret = bch2_gc(c, NULL, true, true);
828                 if (ret)
829                         goto err;
830                 bch_verbose(c, "mark and sweep done");
831         }
832
833         if (c->opts.fsck ||
834             !(c->sb.compat & (1ULL << BCH_COMPAT_FEAT_ALLOC_INFO)) ||
835             test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags)) {
836                 bch_info(c, "starting mark and sweep");
837                 err = "error in mark and sweep";
838                 ret = bch2_gc(c, &journal_keys, true, false);
839                 if (ret)
840                         goto err;
841                 bch_verbose(c, "mark and sweep done");
842         }
843
844         clear_bit(BCH_FS_REBUILD_REPLICAS, &c->flags);
845         set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags);
846
847         /*
848          * Skip past versions that might have possibly been used (as nonces),
849          * but hadn't had their pointers written:
850          */
851         if (c->sb.encryption_type && !c->sb.clean)
852                 atomic64_add(1 << 16, &c->key_version);
853
854         if (c->opts.norecovery)
855                 goto out;
856
857         bch_verbose(c, "starting journal replay");
858         err = "journal replay failed";
859         ret = bch2_journal_replay(c, journal_keys);
860         if (ret)
861                 goto err;
862         bch_verbose(c, "journal replay done");
863
864         if (!c->opts.nochanges) {
865                 /*
866                  * note that even when filesystem was clean there might be work
867                  * to do here, if we ran gc (because of fsck) which recalculated
868                  * oldest_gen:
869                  */
870                 bch_verbose(c, "writing allocation info");
871                 err = "error writing out alloc info";
872                 ret = bch2_stripes_write(c, BTREE_INSERT_LAZY_RW, &wrote) ?:
873                         bch2_alloc_write(c, BTREE_INSERT_LAZY_RW, &wrote);
874                 if (ret) {
875                         bch_err(c, "error writing alloc info");
876                         goto err;
877                 }
878                 bch_verbose(c, "alloc write done");
879
880                 set_bit(BCH_FS_ALLOC_WRITTEN, &c->flags);
881         }
882
883         if (!c->sb.clean) {
884                 if (!(c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
885                         bch_info(c, "checking inode link counts");
886                         err = "error in recovery";
887                         ret = bch2_fsck_inode_nlink(c);
888                         if (ret)
889                                 goto err;
890                         bch_verbose(c, "check inodes done");
891
892                 } else {
893                         bch_verbose(c, "checking for deleted inodes");
894                         err = "error in recovery";
895                         ret = bch2_fsck_walk_inodes_only(c);
896                         if (ret)
897                                 goto err;
898                         bch_verbose(c, "check inodes done");
899                 }
900         }
901
902         if (c->opts.fsck) {
903                 bch_info(c, "starting fsck");
904                 err = "error in fsck";
905                 ret = bch2_fsck_full(c);
906                 if (ret)
907                         goto err;
908                 bch_verbose(c, "fsck done");
909         }
910
911         if (enabled_qtypes(c)) {
912                 bch_verbose(c, "reading quotas");
913                 ret = bch2_fs_quota_read(c);
914                 if (ret)
915                         goto err;
916                 bch_verbose(c, "quotas done");
917         }
918
919         mutex_lock(&c->sb_lock);
920         if (c->opts.version_upgrade) {
921                 if (c->sb.version < bcachefs_metadata_version_new_versioning)
922                         c->disk_sb.sb->version_min =
923                                 le16_to_cpu(bcachefs_metadata_version_min);
924                 c->disk_sb.sb->version = le16_to_cpu(bcachefs_metadata_version_current);
925                 c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_new_siphash;
926                 c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_new_extent_overwrite;
927                 write_sb = true;
928         }
929
930         if (!test_bit(BCH_FS_ERROR, &c->flags)) {
931                 c->disk_sb.sb->compat[0] |= 1ULL << BCH_COMPAT_FEAT_ALLOC_INFO;
932                 write_sb = true;
933         }
934
935         if (c->opts.fsck &&
936             !test_bit(BCH_FS_ERROR, &c->flags)) {
937                 c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_atomic_nlink;
938                 SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 0);
939                 write_sb = true;
940         }
941
942         if (write_sb)
943                 bch2_write_super(c);
944         mutex_unlock(&c->sb_lock);
945
946         if (c->journal_seq_blacklist_table &&
947             c->journal_seq_blacklist_table->nr > 128)
948                 queue_work(system_long_wq, &c->journal_seq_blacklist_gc_work);
949 out:
950         ret = 0;
951 err:
952 fsck_err:
953         set_bit(BCH_FS_FSCK_DONE, &c->flags);
954         bch2_flush_fsck_errs(c);
955
956         journal_keys_free(&journal_keys);
957         journal_entries_free(&journal_entries);
958         kfree(clean);
959         if (ret)
960                 bch_err(c, "Error in recovery: %s (%i)", err, ret);
961         else
962                 bch_verbose(c, "ret %i", ret);
963         return ret;
964 }
965
966 int bch2_fs_initialize(struct bch_fs *c)
967 {
968         struct bch_inode_unpacked root_inode, lostfound_inode;
969         struct bkey_inode_buf packed_inode;
970         struct qstr lostfound = QSTR("lost+found");
971         const char *err = "cannot allocate memory";
972         struct bch_dev *ca;
973         LIST_HEAD(journal);
974         unsigned i;
975         int ret;
976
977         bch_notice(c, "initializing new filesystem");
978
979         mutex_lock(&c->sb_lock);
980         for_each_online_member(ca, c, i)
981                 bch2_mark_dev_superblock(c, ca, 0);
982         mutex_unlock(&c->sb_lock);
983
984         set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
985         set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags);
986
987         for (i = 0; i < BTREE_ID_NR; i++)
988                 bch2_btree_root_alloc(c, i);
989
990         err = "unable to allocate journal buckets";
991         for_each_online_member(ca, c, i) {
992                 ret = bch2_dev_journal_alloc(ca);
993                 if (ret) {
994                         percpu_ref_put(&ca->io_ref);
995                         goto err;
996                 }
997         }
998
999         /*
1000          * journal_res_get() will crash if called before this has
1001          * set up the journal.pin FIFO and journal.cur pointer:
1002          */
1003         bch2_fs_journal_start(&c->journal, 1, &journal);
1004         bch2_journal_set_replay_done(&c->journal);
1005
1006         bch2_inode_init(c, &root_inode, 0, 0,
1007                         S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO, 0, NULL);
1008         root_inode.bi_inum = BCACHEFS_ROOT_INO;
1009         bch2_inode_pack(&packed_inode, &root_inode);
1010
1011         err = "error creating root directory";
1012         ret = bch2_btree_insert(c, BTREE_ID_INODES,
1013                                 &packed_inode.inode.k_i,
1014                                 NULL, NULL, BTREE_INSERT_LAZY_RW);
1015         if (ret)
1016                 goto err;
1017
1018         bch2_inode_init_early(c, &lostfound_inode);
1019
1020         err = "error creating lost+found";
1021         ret = bch2_trans_do(c, NULL, NULL, 0,
1022                 bch2_create_trans(&trans, BCACHEFS_ROOT_INO,
1023                                   &root_inode, &lostfound_inode,
1024                                   &lostfound,
1025                                   0, 0, S_IFDIR|0700, 0,
1026                                   NULL, NULL));
1027         if (ret)
1028                 goto err;
1029
1030         if (enabled_qtypes(c)) {
1031                 ret = bch2_fs_quota_read(c);
1032                 if (ret)
1033                         goto err;
1034         }
1035
1036         err = "error writing first journal entry";
1037         ret = bch2_journal_meta(&c->journal);
1038         if (ret)
1039                 goto err;
1040
1041         mutex_lock(&c->sb_lock);
1042         c->disk_sb.sb->version = c->disk_sb.sb->version_min =
1043                 le16_to_cpu(bcachefs_metadata_version_current);
1044         c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_atomic_nlink;
1045         c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_new_siphash;
1046         c->disk_sb.sb->features[0] |= 1ULL << BCH_FEATURE_new_extent_overwrite;
1047
1048         SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
1049         SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
1050
1051         bch2_write_super(c);
1052         mutex_unlock(&c->sb_lock);
1053
1054         return 0;
1055 err:
1056         pr_err("Error initializing new filesystem: %s (%i)", err, ret);
1057         return ret;
1058 }