]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fsck.c
Update bcachefs sources to 8a65cc4951 bcachefs: Improve bch2_dev_freespace_init()
[bcachefs-tools-debian] / libbcachefs / fsck.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "bkey_buf.h"
5 #include "btree_update.h"
6 #include "darray.h"
7 #include "dirent.h"
8 #include "error.h"
9 #include "fs-common.h"
10 #include "fsck.h"
11 #include "inode.h"
12 #include "keylist.h"
13 #include "subvolume.h"
14 #include "super.h"
15 #include "xattr.h"
16
17 #include <linux/bsearch.h>
18 #include <linux/dcache.h> /* struct qstr */
19
20 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
21
22 /*
23  * XXX: this is handling transaction restarts without returning
24  * -BCH_ERR_transaction_restart_nested, this is not how we do things anymore:
25  */
26 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
27                                     u32 snapshot)
28 {
29         struct btree_iter iter;
30         struct bkey_s_c k;
31         u64 sectors = 0;
32         int ret;
33
34         for_each_btree_key(trans, iter, BTREE_ID_extents,
35                            SPOS(inum, 0, snapshot), 0, k, ret) {
36                 if (k.k->p.inode != inum)
37                         break;
38
39                 if (bkey_extent_is_allocation(k.k))
40                         sectors += k.k->size;
41         }
42
43         bch2_trans_iter_exit(trans, &iter);
44
45         return ret ?: sectors;
46 }
47
48 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
49                                     u32 snapshot)
50 {
51         struct btree_iter iter;
52         struct bkey_s_c k;
53         struct bkey_s_c_dirent d;
54         u64 subdirs = 0;
55         int ret;
56
57         for_each_btree_key(trans, iter, BTREE_ID_dirents,
58                            SPOS(inum, 0, snapshot), 0, k, ret) {
59                 if (k.k->p.inode != inum)
60                         break;
61
62                 if (k.k->type != KEY_TYPE_dirent)
63                         continue;
64
65                 d = bkey_s_c_to_dirent(k);
66                 if (d.v->d_type == DT_DIR)
67                         subdirs++;
68         }
69
70         bch2_trans_iter_exit(trans, &iter);
71
72         return ret ?: subdirs;
73 }
74
75 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
76                                     u32 *subvol)
77 {
78         struct btree_iter iter;
79         struct bkey_s_c k;
80         int ret;
81
82         bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
83                              POS(0, snapshot), 0);
84         k = bch2_btree_iter_peek_slot(&iter);
85         ret = bkey_err(k);
86         if (ret)
87                 goto err;
88
89         if (k.k->type != KEY_TYPE_snapshot) {
90                 bch_err(trans->c, "snapshot %u not fonud", snapshot);
91                 ret = -ENOENT;
92                 goto err;
93         }
94
95         *subvol = le32_to_cpu(bkey_s_c_to_snapshot(k).v->subvol);
96 err:
97         bch2_trans_iter_exit(trans, &iter);
98         return ret;
99
100 }
101
102 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
103                            u32 *snapshot, u64 *inum)
104 {
105         struct bch_subvolume s;
106         int ret;
107
108         ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
109
110         *snapshot = le32_to_cpu(s.snapshot);
111         *inum = le64_to_cpu(s.inode);
112         return ret;
113 }
114
115 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
116                          u32 *snapshot, u64 *inum)
117 {
118         return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
119 }
120
121 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
122                               struct bch_inode_unpacked *inode)
123 {
124         struct btree_iter iter;
125         struct bkey_s_c k;
126         int ret;
127
128         bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
129                              POS(0, inode_nr),
130                              BTREE_ITER_ALL_SNAPSHOTS);
131         k = bch2_btree_iter_peek(&iter);
132         ret = bkey_err(k);
133         if (ret)
134                 goto err;
135
136         if (!k.k || !bkey_eq(k.k->p, POS(0, inode_nr))) {
137                 ret = -ENOENT;
138                 goto err;
139         }
140
141         ret = bch2_inode_unpack(k, inode);
142 err:
143         if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
144                 bch_err(trans->c, "error fetching inode %llu: %s",
145                         inode_nr, bch2_err_str(ret));
146         bch2_trans_iter_exit(trans, &iter);
147         return ret;
148 }
149
150 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
151                           struct bch_inode_unpacked *inode,
152                           u32 *snapshot)
153 {
154         struct btree_iter iter;
155         struct bkey_s_c k;
156         int ret;
157
158         bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
159                              SPOS(0, inode_nr, *snapshot), 0);
160         k = bch2_btree_iter_peek_slot(&iter);
161         ret = bkey_err(k);
162         if (ret)
163                 goto err;
164
165         ret = bkey_is_inode(k.k)
166                 ? bch2_inode_unpack(k, inode)
167                 : -ENOENT;
168         if (!ret)
169                 *snapshot = iter.pos.snapshot;
170 err:
171         if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
172                 bch_err(trans->c, "error fetching inode %llu:%u: %s",
173                         inode_nr, *snapshot, bch2_err_str(ret));
174         bch2_trans_iter_exit(trans, &iter);
175         return ret;
176 }
177
178 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
179                         struct bch_inode_unpacked *inode,
180                         u32 *snapshot)
181 {
182         return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
183 }
184
185 static int __lookup_dirent(struct btree_trans *trans,
186                            struct bch_hash_info hash_info,
187                            subvol_inum dir, struct qstr *name,
188                            u64 *target, unsigned *type)
189 {
190         struct btree_iter iter;
191         struct bkey_s_c_dirent d;
192         int ret;
193
194         ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
195                                &hash_info, dir, name, 0);
196         if (ret)
197                 return ret;
198
199         d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
200         *target = le64_to_cpu(d.v->d_inum);
201         *type = d.v->d_type;
202         bch2_trans_iter_exit(trans, &iter);
203         return 0;
204 }
205
206 static int __write_inode(struct btree_trans *trans,
207                          struct bch_inode_unpacked *inode,
208                          u32 snapshot)
209 {
210         struct btree_iter iter;
211         int ret;
212
213         bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
214                             SPOS(0, inode->bi_inum, snapshot),
215                             BTREE_ITER_INTENT);
216
217         ret   = bch2_btree_iter_traverse(&iter) ?:
218                 bch2_inode_write(trans, &iter, inode);
219         bch2_trans_iter_exit(trans, &iter);
220         return ret;
221 }
222
223 static int write_inode(struct btree_trans *trans,
224                        struct bch_inode_unpacked *inode,
225                        u32 snapshot)
226 {
227         int ret = commit_do(trans, NULL, NULL,
228                                   BTREE_INSERT_NOFAIL|
229                                   BTREE_INSERT_LAZY_RW,
230                                   __write_inode(trans, inode, snapshot));
231         if (ret)
232                 bch_err(trans->c, "error in fsck: error updating inode: %s",
233                         bch2_err_str(ret));
234         return ret;
235 }
236
237 static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
238 {
239         struct bch_fs *c = trans->c;
240         struct btree_iter iter = { NULL };
241         struct bkey_i_inode_generation delete;
242         struct bch_inode_unpacked inode_u;
243         struct bkey_s_c k;
244         int ret;
245
246         do {
247                 ret   = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
248                                                       SPOS(inum, 0, snapshot),
249                                                       SPOS(inum, U64_MAX, snapshot),
250                                                       0, NULL) ?:
251                         bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
252                                                       SPOS(inum, 0, snapshot),
253                                                       SPOS(inum, U64_MAX, snapshot),
254                                                       0, NULL) ?:
255                         bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
256                                                       SPOS(inum, 0, snapshot),
257                                                       SPOS(inum, U64_MAX, snapshot),
258                                                       0, NULL);
259         } while (ret == -BCH_ERR_transaction_restart_nested);
260         if (ret)
261                 goto err;
262 retry:
263         bch2_trans_begin(trans);
264
265         bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
266                              SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
267         k = bch2_btree_iter_peek_slot(&iter);
268
269         ret = bkey_err(k);
270         if (ret)
271                 goto err;
272
273         if (!bkey_is_inode(k.k)) {
274                 bch2_fs_inconsistent(c,
275                                      "inode %llu:%u not found when deleting",
276                                      inum, snapshot);
277                 ret = -EIO;
278                 goto err;
279         }
280
281         bch2_inode_unpack(k, &inode_u);
282
283         /* Subvolume root? */
284         if (inode_u.bi_subvol)
285                 bch_warn(c, "deleting inode %llu marked as unlinked, but also a subvolume root!?", inode_u.bi_inum);
286
287         bkey_inode_generation_init(&delete.k_i);
288         delete.k.p = iter.pos;
289         delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
290
291         ret   = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
292                 bch2_trans_commit(trans, NULL, NULL,
293                                 BTREE_INSERT_NOFAIL);
294 err:
295         bch2_trans_iter_exit(trans, &iter);
296         if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
297                 goto retry;
298
299         return ret ?: -BCH_ERR_transaction_restart_nested;
300 }
301
302 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
303 {
304         struct bch_fs *c = trans->c;
305         struct btree_iter iter;
306         struct bch_inode_unpacked dir_inode;
307         struct bch_hash_info dir_hash_info;
308         int ret;
309
310         ret = lookup_first_inode(trans, pos.inode, &dir_inode);
311         if (ret)
312                 goto err;
313
314         dir_hash_info = bch2_hash_info_init(c, &dir_inode);
315
316         bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
317
318         ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
319                                   &dir_hash_info, &iter,
320                                   BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
321         bch2_trans_iter_exit(trans, &iter);
322 err:
323         if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
324                 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
325         return ret;
326 }
327
328 /* Get lost+found, create if it doesn't exist: */
329 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
330                             struct bch_inode_unpacked *lostfound)
331 {
332         struct bch_fs *c = trans->c;
333         struct bch_inode_unpacked root;
334         struct bch_hash_info root_hash_info;
335         struct qstr lostfound_str = QSTR("lost+found");
336         subvol_inum root_inum = { .subvol = subvol };
337         u64 inum = 0;
338         unsigned d_type = 0;
339         u32 snapshot;
340         int ret;
341
342         ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
343         if (ret)
344                 return ret;
345
346         ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
347         if (ret)
348                 return ret;
349
350         root_hash_info = bch2_hash_info_init(c, &root);
351
352         ret = __lookup_dirent(trans, root_hash_info, root_inum,
353                             &lostfound_str, &inum, &d_type);
354         if (ret == -ENOENT) {
355                 bch_notice(c, "creating lost+found");
356                 goto create_lostfound;
357         }
358
359         if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
360                 bch_err(c, "error looking up lost+found: %s", bch2_err_str(ret));
361         if (ret)
362                 return ret;
363
364         if (d_type != DT_DIR) {
365                 bch_err(c, "error looking up lost+found: not a directory");
366                 return ret;
367         }
368
369         /*
370          * The check_dirents pass has already run, dangling dirents
371          * shouldn't exist here:
372          */
373         return __lookup_inode(trans, inum, lostfound, &snapshot);
374
375 create_lostfound:
376         bch2_inode_init_early(c, lostfound);
377
378         ret = bch2_create_trans(trans, root_inum, &root,
379                                 lostfound, &lostfound_str,
380                                 0, 0, S_IFDIR|0700, 0, NULL, NULL,
381                                 (subvol_inum) { }, 0);
382         if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
383                 bch_err(c, "error creating lost+found: %s", bch2_err_str(ret));
384         return ret;
385 }
386
387 static int __reattach_inode(struct btree_trans *trans,
388                           struct bch_inode_unpacked *inode,
389                           u32 inode_snapshot)
390 {
391         struct bch_hash_info dir_hash;
392         struct bch_inode_unpacked lostfound;
393         char name_buf[20];
394         struct qstr name;
395         u64 dir_offset = 0;
396         u32 subvol;
397         int ret;
398
399         ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
400         if (ret)
401                 return ret;
402
403         ret = lookup_lostfound(trans, subvol, &lostfound);
404         if (ret)
405                 return ret;
406
407         if (S_ISDIR(inode->bi_mode)) {
408                 lostfound.bi_nlink++;
409
410                 ret = __write_inode(trans, &lostfound, U32_MAX);
411                 if (ret)
412                         return ret;
413         }
414
415         dir_hash = bch2_hash_info_init(trans->c, &lostfound);
416
417         snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
418         name = (struct qstr) QSTR(name_buf);
419
420         ret = bch2_dirent_create(trans,
421                                  (subvol_inum) {
422                                         .subvol = subvol,
423                                         .inum = lostfound.bi_inum,
424                                  },
425                                  &dir_hash,
426                                  inode_d_type(inode),
427                                  &name, inode->bi_inum, &dir_offset,
428                                  BCH_HASH_SET_MUST_CREATE);
429         if (ret)
430                 return ret;
431
432         inode->bi_dir           = lostfound.bi_inum;
433         inode->bi_dir_offset    = dir_offset;
434
435         return __write_inode(trans, inode, inode_snapshot);
436 }
437
438 static int reattach_inode(struct btree_trans *trans,
439                           struct bch_inode_unpacked *inode,
440                           u32 inode_snapshot)
441 {
442         int ret = commit_do(trans, NULL, NULL,
443                                   BTREE_INSERT_LAZY_RW|
444                                   BTREE_INSERT_NOFAIL,
445                         __reattach_inode(trans, inode, inode_snapshot));
446         if (ret) {
447                 bch_err(trans->c, "error reattaching inode %llu: %s",
448                         inode->bi_inum, bch2_err_str(ret));
449                 return ret;
450         }
451
452         return ret;
453 }
454
455 static int remove_backpointer(struct btree_trans *trans,
456                               struct bch_inode_unpacked *inode)
457 {
458         struct btree_iter iter;
459         struct bkey_s_c k;
460         int ret;
461
462         bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
463                              POS(inode->bi_dir, inode->bi_dir_offset), 0);
464         k = bch2_btree_iter_peek_slot(&iter);
465         ret = bkey_err(k);
466         if (ret)
467                 goto out;
468         if (k.k->type != KEY_TYPE_dirent) {
469                 ret = -ENOENT;
470                 goto out;
471         }
472
473         ret = __remove_dirent(trans, k.k->p);
474 out:
475         bch2_trans_iter_exit(trans, &iter);
476         return ret;
477 }
478
479 struct snapshots_seen_entry {
480         u32                             id;
481         u32                             equiv;
482 };
483
484 struct snapshots_seen {
485         struct bpos                     pos;
486         DARRAY(struct snapshots_seen_entry) ids;
487 };
488
489 static inline void snapshots_seen_exit(struct snapshots_seen *s)
490 {
491         darray_exit(&s->ids);
492 }
493
494 static inline void snapshots_seen_init(struct snapshots_seen *s)
495 {
496         memset(s, 0, sizeof(*s));
497 }
498
499 static int snapshots_seen_add(struct bch_fs *c, struct snapshots_seen *s, u32 id)
500 {
501         struct snapshots_seen_entry *i, n = { id, id };
502         int ret;
503
504         darray_for_each(s->ids, i) {
505                 if (n.equiv < i->equiv)
506                         break;
507
508                 if (i->equiv == n.equiv) {
509                         bch_err(c, "%s(): adding duplicate snapshot", __func__);
510                         return -EINVAL;
511                 }
512         }
513
514         ret = darray_insert_item(&s->ids, i - s->ids.data, n);
515         if (ret)
516                 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
517                         s->ids.size);
518         return ret;
519 }
520
521 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
522                                  enum btree_id btree_id, struct bpos pos)
523 {
524         struct snapshots_seen_entry *i, n = {
525                 .id     = pos.snapshot,
526                 .equiv  = bch2_snapshot_equiv(c, pos.snapshot),
527         };
528         int ret = 0;
529
530         if (!bkey_eq(s->pos, pos))
531                 s->ids.nr = 0;
532
533         pos.snapshot = n.equiv;
534         s->pos = pos;
535
536         darray_for_each(s->ids, i)
537                 if (i->equiv == n.equiv) {
538                         if (fsck_err_on(i->id != n.id, c,
539                                         "snapshot deletion did not run correctly:\n"
540                                         "  duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
541                                         bch2_btree_ids[btree_id],
542                                         pos.inode, pos.offset,
543                                         i->id, n.id, n.equiv))
544                                 return -BCH_ERR_need_snapshot_cleanup;
545
546                         return 0;
547                 }
548
549         ret = darray_push(&s->ids, n);
550         if (ret)
551                 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
552                         s->ids.size);
553 fsck_err:
554         return ret;
555 }
556
557 /**
558  * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
559  * and @ancestor hasn't been overwritten in @seen
560  *
561  * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
562  */
563 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
564                                     u32 id, u32 ancestor)
565 {
566         ssize_t i;
567         u32 top = seen->ids.nr ? seen->ids.data[seen->ids.nr - 1].equiv : 0;
568
569         BUG_ON(id > ancestor);
570         BUG_ON(!bch2_snapshot_is_equiv(c, id));
571         BUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
572
573         /* @ancestor should be the snapshot most recently added to @seen */
574         BUG_ON(ancestor != seen->pos.snapshot);
575         BUG_ON(ancestor != top);
576
577         if (id == ancestor)
578                 return true;
579
580         if (!bch2_snapshot_is_ancestor(c, id, ancestor))
581                 return false;
582
583         for (i = seen->ids.nr - 2;
584              i >= 0 && seen->ids.data[i].equiv >= id;
585              --i)
586                 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv) &&
587                     bch2_snapshot_is_ancestor(c, seen->ids.data[i].equiv, ancestor))
588                         return false;
589
590         return true;
591 }
592
593 /**
594  * ref_visible - given a key with snapshot id @src that points to a key with
595  * snapshot id @dst, test whether there is some snapshot in which @dst is
596  * visible.
597  *
598  * This assumes we're visiting @src keys in natural key order.
599  *
600  * @s   - list of snapshot IDs already seen at @src
601  * @src - snapshot ID of src key
602  * @dst - snapshot ID of dst key
603  */
604 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
605                        u32 src, u32 dst)
606 {
607         return dst <= src
608                 ? key_visible_in_snapshot(c, s, dst, src)
609                 : bch2_snapshot_is_ancestor(c, src, dst);
610 }
611
612 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i)                               \
613         for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr &&        \
614              (_i)->snapshot <= (_snapshot); _i++)                                       \
615                 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
616
617 struct inode_walker_entry {
618         struct bch_inode_unpacked inode;
619         u32                     snapshot;
620         u64                     count;
621 };
622
623 struct inode_walker {
624         bool                            first_this_inode;
625         u64                             cur_inum;
626
627         DARRAY(struct inode_walker_entry) inodes;
628 };
629
630 static void inode_walker_exit(struct inode_walker *w)
631 {
632         darray_exit(&w->inodes);
633 }
634
635 static struct inode_walker inode_walker_init(void)
636 {
637         return (struct inode_walker) { 0, };
638 }
639
640 static int add_inode(struct bch_fs *c, struct inode_walker *w,
641                      struct bkey_s_c inode)
642 {
643         struct bch_inode_unpacked u;
644
645         BUG_ON(bch2_inode_unpack(inode, &u));
646
647         return darray_push(&w->inodes, ((struct inode_walker_entry) {
648                 .inode          = u,
649                 .snapshot       = bch2_snapshot_equiv(c, inode.k->p.snapshot),
650         }));
651 }
652
653 static int __walk_inode(struct btree_trans *trans,
654                         struct inode_walker *w, struct bpos pos)
655 {
656         struct bch_fs *c = trans->c;
657         struct btree_iter iter;
658         struct bkey_s_c k;
659         u32 restart_count = trans->restart_count;
660         unsigned i;
661         int ret;
662
663         pos.snapshot = bch2_snapshot_equiv(c, pos.snapshot);
664
665         if (pos.inode == w->cur_inum) {
666                 w->first_this_inode = false;
667                 goto lookup_snapshot;
668         }
669
670         w->inodes.nr = 0;
671
672         for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, pos.inode),
673                            BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
674                 if (k.k->p.offset != pos.inode)
675                         break;
676
677                 if (bkey_is_inode(k.k))
678                         add_inode(c, w, k);
679         }
680         bch2_trans_iter_exit(trans, &iter);
681
682         if (ret)
683                 return ret;
684
685         w->cur_inum             = pos.inode;
686         w->first_this_inode     = true;
687
688         if (trans_was_restarted(trans, restart_count))
689                 return -BCH_ERR_transaction_restart_nested;
690
691 lookup_snapshot:
692         for (i = 0; i < w->inodes.nr; i++)
693                 if (bch2_snapshot_is_ancestor(c, pos.snapshot, w->inodes.data[i].snapshot))
694                         goto found;
695         return INT_MAX;
696 found:
697         BUG_ON(pos.snapshot > w->inodes.data[i].snapshot);
698
699         if (pos.snapshot != w->inodes.data[i].snapshot) {
700                 struct inode_walker_entry e = w->inodes.data[i];
701
702                 e.snapshot = pos.snapshot;
703                 e.count = 0;
704
705                 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
706                          pos.inode, pos.snapshot, w->inodes.data[i].snapshot);
707
708                 while (i && w->inodes.data[i - 1].snapshot > pos.snapshot)
709                         --i;
710
711                 ret = darray_insert_item(&w->inodes, i, e);
712                 if (ret)
713                         return ret;
714         }
715
716         return i;
717 }
718
719 static int __get_visible_inodes(struct btree_trans *trans,
720                                 struct inode_walker *w,
721                                 struct snapshots_seen *s,
722                                 u64 inum)
723 {
724         struct bch_fs *c = trans->c;
725         struct btree_iter iter;
726         struct bkey_s_c k;
727         int ret;
728
729         w->inodes.nr = 0;
730
731         for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
732                            BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
733                 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
734
735                 if (k.k->p.offset != inum)
736                         break;
737
738                 if (!ref_visible(c, s, s->pos.snapshot, equiv))
739                         continue;
740
741                 if (bkey_is_inode(k.k))
742                         add_inode(c, w, k);
743
744                 if (equiv >= s->pos.snapshot)
745                         break;
746         }
747         bch2_trans_iter_exit(trans, &iter);
748
749         return ret;
750 }
751
752 static int check_key_has_snapshot(struct btree_trans *trans,
753                                   struct btree_iter *iter,
754                                   struct bkey_s_c k)
755 {
756         struct bch_fs *c = trans->c;
757         struct printbuf buf = PRINTBUF;
758         int ret = 0;
759
760         if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
761                         "key in missing snapshot: %s",
762                         (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
763                 ret = bch2_btree_delete_at(trans, iter,
764                                             BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
765 fsck_err:
766         printbuf_exit(&buf);
767         return ret;
768 }
769
770 static int hash_redo_key(struct btree_trans *trans,
771                          const struct bch_hash_desc desc,
772                          struct bch_hash_info *hash_info,
773                          struct btree_iter *k_iter, struct bkey_s_c k)
774 {
775         struct bkey_i *delete;
776         struct bkey_i *tmp;
777
778         delete = bch2_trans_kmalloc(trans, sizeof(*delete));
779         if (IS_ERR(delete))
780                 return PTR_ERR(delete);
781
782         tmp = bch2_bkey_make_mut(trans, k);
783         if (IS_ERR(tmp))
784                 return PTR_ERR(tmp);
785
786         bkey_init(&delete->k);
787         delete->k.p = k_iter->pos;
788         return  bch2_btree_iter_traverse(k_iter) ?:
789                 bch2_trans_update(trans, k_iter, delete, 0) ?:
790                 bch2_hash_set_snapshot(trans, desc, hash_info,
791                                        (subvol_inum) { 0, k.k->p.inode },
792                                        k.k->p.snapshot, tmp,
793                                        BCH_HASH_SET_MUST_CREATE,
794                                        BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
795                 bch2_trans_commit(trans, NULL, NULL,
796                                   BTREE_INSERT_NOFAIL|
797                                   BTREE_INSERT_LAZY_RW);
798 }
799
800 static int hash_check_key(struct btree_trans *trans,
801                           const struct bch_hash_desc desc,
802                           struct bch_hash_info *hash_info,
803                           struct btree_iter *k_iter, struct bkey_s_c hash_k)
804 {
805         struct bch_fs *c = trans->c;
806         struct btree_iter iter = { NULL };
807         struct printbuf buf = PRINTBUF;
808         struct bkey_s_c k;
809         u64 hash;
810         int ret = 0;
811
812         if (hash_k.k->type != desc.key_type)
813                 return 0;
814
815         hash = desc.hash_bkey(hash_info, hash_k);
816
817         if (likely(hash == hash_k.k->p.offset))
818                 return 0;
819
820         if (hash_k.k->p.offset < hash)
821                 goto bad_hash;
822
823         for_each_btree_key_norestart(trans, iter, desc.btree_id,
824                                      POS(hash_k.k->p.inode, hash),
825                                      BTREE_ITER_SLOTS, k, ret) {
826                 if (bkey_eq(k.k->p, hash_k.k->p))
827                         break;
828
829                 if (fsck_err_on(k.k->type == desc.key_type &&
830                                 !desc.cmp_bkey(k, hash_k), c,
831                                 "duplicate hash table keys:\n%s",
832                                 (printbuf_reset(&buf),
833                                  bch2_bkey_val_to_text(&buf, c, hash_k),
834                                  buf.buf))) {
835                         ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
836                         break;
837                 }
838
839                 if (bkey_deleted(k.k)) {
840                         bch2_trans_iter_exit(trans, &iter);
841                         goto bad_hash;
842                 }
843         }
844 out:
845         bch2_trans_iter_exit(trans, &iter);
846         printbuf_exit(&buf);
847         return ret;
848 bad_hash:
849         if (fsck_err(c, "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
850                      bch2_btree_ids[desc.btree_id], hash_k.k->p.inode, hash_k.k->p.offset, hash,
851                      (printbuf_reset(&buf),
852                       bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
853                 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
854                 if (ret) {
855                         bch_err(c, "hash_redo_key err %s", bch2_err_str(ret));
856                         return ret;
857                 }
858                 ret = -BCH_ERR_transaction_restart_nested;
859         }
860 fsck_err:
861         goto out;
862 }
863
864 static int check_inode(struct btree_trans *trans,
865                        struct btree_iter *iter,
866                        struct bkey_s_c k,
867                        struct bch_inode_unpacked *prev,
868                        struct snapshots_seen *s,
869                        bool full)
870 {
871         struct bch_fs *c = trans->c;
872         struct bch_inode_unpacked u;
873         bool do_update = false;
874         int ret;
875
876         ret = check_key_has_snapshot(trans, iter, k);
877         if (ret < 0)
878                 goto err;
879         if (ret)
880                 return 0;
881
882         ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
883         if (ret)
884                 goto err;
885
886         /*
887          * if snapshot id isn't a leaf node, skip it - deletion in
888          * particular is not atomic, so on the internal snapshot nodes
889          * we can see inodes marked for deletion after a clean shutdown
890          */
891         if (bch2_snapshot_internal_node(c, k.k->p.snapshot))
892                 return 0;
893
894         if (!bkey_is_inode(k.k))
895                 return 0;
896
897         BUG_ON(bch2_inode_unpack(k, &u));
898
899         if (!full &&
900             !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
901                             BCH_INODE_I_SECTORS_DIRTY|
902                             BCH_INODE_UNLINKED)))
903                 return 0;
904
905         if (prev->bi_inum != u.bi_inum)
906                 *prev = u;
907
908         if (fsck_err_on(prev->bi_hash_seed      != u.bi_hash_seed ||
909                         inode_d_type(prev)      != inode_d_type(&u), c,
910                         "inodes in different snapshots don't match")) {
911                 bch_err(c, "repair not implemented yet");
912                 return -EINVAL;
913         }
914
915         if (u.bi_flags & BCH_INODE_UNLINKED &&
916             (!c->sb.clean ||
917              fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
918                       u.bi_inum))) {
919                 bch2_trans_unlock(trans);
920                 bch2_fs_lazy_rw(c);
921
922                 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
923                 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
924                         bch_err(c, "error in fsck: error while deleting inode: %s",
925                                 bch2_err_str(ret));
926                 return ret;
927         }
928
929         if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
930             (!c->sb.clean ||
931              fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
932                       u.bi_inum))) {
933                 bch_verbose(c, "truncating inode %llu", u.bi_inum);
934
935                 bch2_trans_unlock(trans);
936                 bch2_fs_lazy_rw(c);
937
938                 /*
939                  * XXX: need to truncate partial blocks too here - or ideally
940                  * just switch units to bytes and that issue goes away
941                  */
942                 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
943                                 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
944                                      iter->pos.snapshot),
945                                 POS(u.bi_inum, U64_MAX),
946                                 0, NULL);
947                 if (ret) {
948                         bch_err(c, "error in fsck: error truncating inode: %s",
949                                 bch2_err_str(ret));
950                         return ret;
951                 }
952
953                 /*
954                  * We truncated without our normal sector accounting hook, just
955                  * make sure we recalculate it:
956                  */
957                 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
958
959                 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
960                 do_update = true;
961         }
962
963         if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
964             (!c->sb.clean ||
965              fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
966                       u.bi_inum))) {
967                 s64 sectors;
968
969                 bch_verbose(c, "recounting sectors for inode %llu",
970                             u.bi_inum);
971
972                 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
973                 if (sectors < 0) {
974                         bch_err(c, "error in fsck: error recounting inode sectors: %s",
975                                 bch2_err_str(sectors));
976                         return sectors;
977                 }
978
979                 u.bi_sectors = sectors;
980                 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
981                 do_update = true;
982         }
983
984         if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
985                 u.bi_dir = 0;
986                 u.bi_dir_offset = 0;
987                 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
988                 do_update = true;
989         }
990
991         if (do_update) {
992                 ret = __write_inode(trans, &u, iter->pos.snapshot);
993                 if (ret)
994                         bch_err(c, "error in fsck: error updating inode: %s",
995                                 bch2_err_str(ret));
996         }
997 err:
998 fsck_err:
999         if (ret)
1000                 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1001         return ret;
1002 }
1003
1004 noinline_for_stack
1005 static int check_inodes(struct bch_fs *c, bool full)
1006 {
1007         struct btree_trans trans;
1008         struct btree_iter iter;
1009         struct bch_inode_unpacked prev = { 0 };
1010         struct snapshots_seen s;
1011         struct bkey_s_c k;
1012         int ret;
1013
1014         snapshots_seen_init(&s);
1015         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1016
1017         ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
1018                         POS_MIN,
1019                         BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1020                         NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1021                 check_inode(&trans, &iter, k, &prev, &s, full));
1022
1023         bch2_trans_exit(&trans);
1024         snapshots_seen_exit(&s);
1025         if (ret)
1026                 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1027         return ret;
1028 }
1029
1030 /*
1031  * Checking for overlapping extents needs to be reimplemented
1032  */
1033 #if 0
1034 static int fix_overlapping_extent(struct btree_trans *trans,
1035                                        struct bkey_s_c k, struct bpos cut_at)
1036 {
1037         struct btree_iter iter;
1038         struct bkey_i *u;
1039         int ret;
1040
1041         u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1042         ret = PTR_ERR_OR_ZERO(u);
1043         if (ret)
1044                 return ret;
1045
1046         bkey_reassemble(u, k);
1047         bch2_cut_front(cut_at, u);
1048
1049
1050         /*
1051          * We don't want to go through the extent_handle_overwrites path:
1052          *
1053          * XXX: this is going to screw up disk accounting, extent triggers
1054          * assume things about extent overwrites - we should be running the
1055          * triggers manually here
1056          */
1057         bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, u->k.p,
1058                              BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
1059
1060         BUG_ON(iter.flags & BTREE_ITER_IS_EXTENTS);
1061         ret   = bch2_btree_iter_traverse(&iter) ?:
1062                 bch2_trans_update(trans, &iter, u, BTREE_TRIGGER_NORUN) ?:
1063                 bch2_trans_commit(trans, NULL, NULL,
1064                                   BTREE_INSERT_NOFAIL|
1065                                   BTREE_INSERT_LAZY_RW);
1066         bch2_trans_iter_exit(trans, &iter);
1067         return ret;
1068 }
1069 #endif
1070
1071 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1072                                                 struct btree_iter *iter,
1073                                                 struct bpos pos)
1074 {
1075         struct bkey_s_c k;
1076         int ret;
1077
1078         bch2_trans_iter_init(trans, iter, BTREE_ID_dirents, pos, 0);
1079         k = bch2_btree_iter_peek_slot(iter);
1080         ret = bkey_err(k);
1081         if (!ret && k.k->type != KEY_TYPE_dirent)
1082                 ret = -ENOENT;
1083         if (ret) {
1084                 bch2_trans_iter_exit(trans, iter);
1085                 return (struct bkey_s_c_dirent) { .k = ERR_PTR(ret) };
1086         }
1087
1088         return bkey_s_c_to_dirent(k);
1089 }
1090
1091 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1092                                    struct bkey_s_c_dirent d)
1093 {
1094         return  inode->bi_dir           == d.k->p.inode &&
1095                 inode->bi_dir_offset    == d.k->p.offset;
1096 }
1097
1098 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1099                                    struct bch_inode_unpacked *inode)
1100 {
1101         return d.v->d_type == DT_SUBVOL
1102                 ? le32_to_cpu(d.v->d_child_subvol)      == inode->bi_subvol
1103                 : le64_to_cpu(d.v->d_inum)              == inode->bi_inum;
1104 }
1105
1106 static int inode_backpointer_exists(struct btree_trans *trans,
1107                                     struct bch_inode_unpacked *inode,
1108                                     u32 snapshot)
1109 {
1110         struct btree_iter iter;
1111         struct bkey_s_c_dirent d;
1112         int ret;
1113
1114         d = dirent_get_by_pos(trans, &iter,
1115                         SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1116         ret = bkey_err(d.s_c);
1117         if (ret)
1118                 return ret == -ENOENT ? 0 : ret;
1119
1120         ret = dirent_points_to_inode(d, inode);
1121         bch2_trans_iter_exit(trans, &iter);
1122         return ret;
1123 }
1124
1125 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1126 {
1127         struct bch_fs *c = trans->c;
1128         struct inode_walker_entry *i;
1129         u32 restart_count = trans->restart_count;
1130         int ret = 0;
1131         s64 count2;
1132
1133         darray_for_each(w->inodes, i) {
1134                 if (i->inode.bi_sectors == i->count)
1135                         continue;
1136
1137                 count2 = bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot);
1138
1139                 if (i->count != count2) {
1140                         bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1141                                 i->count, count2);
1142                         i->count = count2;
1143                         if (i->inode.bi_sectors == i->count)
1144                                 continue;
1145                 }
1146
1147                 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1148                             "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1149                             w->cur_inum, i->snapshot,
1150                             i->inode.bi_sectors, i->count)) {
1151                         i->inode.bi_sectors = i->count;
1152                         ret = write_inode(trans, &i->inode, i->snapshot);
1153                         if (ret)
1154                                 break;
1155                 }
1156         }
1157 fsck_err:
1158         if (ret)
1159                 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1160         if (!ret && trans_was_restarted(trans, restart_count))
1161                 ret = -BCH_ERR_transaction_restart_nested;
1162         return ret;
1163 }
1164
1165 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1166                         struct bkey_s_c k,
1167                         struct inode_walker *inode,
1168                         struct snapshots_seen *s)
1169 {
1170         struct bch_fs *c = trans->c;
1171         struct inode_walker_entry *i;
1172         struct printbuf buf = PRINTBUF;
1173         struct bpos equiv;
1174         int ret = 0;
1175
1176         ret = check_key_has_snapshot(trans, iter, k);
1177         if (ret) {
1178                 ret = ret < 0 ? ret : 0;
1179                 goto out;
1180         }
1181
1182         equiv = k.k->p;
1183         equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1184
1185         ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1186         if (ret)
1187                 goto err;
1188
1189         if (k.k->type == KEY_TYPE_whiteout)
1190                 goto out;
1191
1192         if (inode->cur_inum != k.k->p.inode) {
1193                 ret = check_i_sectors(trans, inode);
1194                 if (ret)
1195                         goto err;
1196         }
1197
1198         BUG_ON(!iter->path->should_be_locked);
1199 #if 0
1200         if (bkey_gt(prev.k->k.p, bkey_start_pos(k.k))) {
1201                 char buf1[200];
1202                 char buf2[200];
1203
1204                 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
1205                 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
1206
1207                 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2)) {
1208                         ret = fix_overlapping_extent(trans, k, prev.k->k.p)
1209                                 ?: -BCH_ERR_transaction_restart_nested;
1210                         goto out;
1211                 }
1212         }
1213 #endif
1214         ret = __walk_inode(trans, inode, equiv);
1215         if (ret < 0)
1216                 goto err;
1217
1218         if (fsck_err_on(ret == INT_MAX, c,
1219                         "extent in missing inode:\n  %s",
1220                         (printbuf_reset(&buf),
1221                          bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1222                 ret = bch2_btree_delete_at(trans, iter,
1223                                             BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1224                 goto out;
1225         }
1226
1227         if (ret == INT_MAX) {
1228                 ret = 0;
1229                 goto out;
1230         }
1231
1232         i = inode->inodes.data + ret;
1233         ret = 0;
1234
1235         if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1236                         !S_ISLNK(i->inode.bi_mode), c,
1237                         "extent in non regular inode mode %o:\n  %s",
1238                         i->inode.bi_mode,
1239                         (printbuf_reset(&buf),
1240                          bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1241                 ret = bch2_btree_delete_at(trans, iter,
1242                                             BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1243                 goto out;
1244         }
1245
1246         /*
1247          * Check inodes in reverse order, from oldest snapshots to newest, so
1248          * that we emit the fewest number of whiteouts necessary:
1249          */
1250         for (i = inode->inodes.data + inode->inodes.nr - 1;
1251              i >= inode->inodes.data;
1252              --i) {
1253                 if (i->snapshot > equiv.snapshot ||
1254                     !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1255                         continue;
1256
1257                 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1258                                 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1259                                 !bkey_extent_is_reservation(k), c,
1260                                 "extent type past end of inode %llu:%u, i_size %llu\n  %s",
1261                                 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1262                                 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1263                         struct btree_iter iter2;
1264
1265                         bch2_trans_copy_iter(&iter2, iter);
1266                         bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1267                         ret =   bch2_btree_iter_traverse(&iter2) ?:
1268                                 bch2_btree_delete_at(trans, &iter2,
1269                                         BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1270                         bch2_trans_iter_exit(trans, &iter2);
1271                         if (ret)
1272                                 goto err;
1273
1274                         if (i->snapshot != equiv.snapshot) {
1275                                 ret = snapshots_seen_add(c, s, i->snapshot);
1276                                 if (ret)
1277                                         goto err;
1278                         }
1279                 }
1280         }
1281
1282         if (bkey_extent_is_allocation(k.k))
1283                 for_each_visible_inode(c, s, inode, equiv.snapshot, i)
1284                         i->count += k.k->size;
1285 #if 0
1286         bch2_bkey_buf_reassemble(&prev, c, k);
1287 #endif
1288
1289 out:
1290 err:
1291 fsck_err:
1292         printbuf_exit(&buf);
1293
1294         if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1295                 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1296         return ret;
1297 }
1298
1299 /*
1300  * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1301  * that i_size an i_sectors are consistent
1302  */
1303 noinline_for_stack
1304 static int check_extents(struct bch_fs *c)
1305 {
1306         struct inode_walker w = inode_walker_init();
1307         struct snapshots_seen s;
1308         struct btree_trans trans;
1309         struct btree_iter iter;
1310         struct bkey_s_c k;
1311         int ret = 0;
1312
1313 #if 0
1314         struct bkey_buf prev;
1315         bch2_bkey_buf_init(&prev);
1316         prev.k->k = KEY(0, 0, 0);
1317 #endif
1318         snapshots_seen_init(&s);
1319         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1320
1321         bch_verbose(c, "checking extents");
1322
1323         ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_extents,
1324                         POS(BCACHEFS_ROOT_INO, 0),
1325                         BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1326                         NULL, NULL,
1327                         BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1328                 check_extent(&trans, &iter, k, &w, &s));
1329 #if 0
1330         bch2_bkey_buf_exit(&prev, c);
1331 #endif
1332         inode_walker_exit(&w);
1333         bch2_trans_exit(&trans);
1334         snapshots_seen_exit(&s);
1335
1336         if (ret)
1337                 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1338         return ret;
1339 }
1340
1341 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1342 {
1343         struct bch_fs *c = trans->c;
1344         struct inode_walker_entry *i;
1345         u32 restart_count = trans->restart_count;
1346         int ret = 0;
1347         s64 count2;
1348
1349         darray_for_each(w->inodes, i) {
1350                 if (i->inode.bi_nlink == i->count)
1351                         continue;
1352
1353                 count2 = bch2_count_subdirs(trans, w->cur_inum, i->snapshot);
1354                 if (count2 < 0)
1355                         return count2;
1356
1357                 if (i->count != count2) {
1358                         bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1359                                 i->count, count2);
1360                         i->count = count2;
1361                         if (i->inode.bi_nlink == i->count)
1362                                 continue;
1363                 }
1364
1365                 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1366                                 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1367                                 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1368                         i->inode.bi_nlink = i->count;
1369                         ret = write_inode(trans, &i->inode, i->snapshot);
1370                         if (ret)
1371                                 break;
1372                 }
1373         }
1374 fsck_err:
1375         if (ret)
1376                 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1377         if (!ret && trans_was_restarted(trans, restart_count))
1378                 ret = -BCH_ERR_transaction_restart_nested;
1379         return ret;
1380 }
1381
1382 static int check_dirent_target(struct btree_trans *trans,
1383                                struct btree_iter *iter,
1384                                struct bkey_s_c_dirent d,
1385                                struct bch_inode_unpacked *target,
1386                                u32 target_snapshot)
1387 {
1388         struct bch_fs *c = trans->c;
1389         struct bkey_i_dirent *n;
1390         bool backpointer_exists = true;
1391         struct printbuf buf = PRINTBUF;
1392         int ret = 0;
1393
1394         if (!target->bi_dir &&
1395             !target->bi_dir_offset) {
1396                 target->bi_dir          = d.k->p.inode;
1397                 target->bi_dir_offset   = d.k->p.offset;
1398
1399                 ret = __write_inode(trans, target, target_snapshot);
1400                 if (ret)
1401                         goto err;
1402         }
1403
1404         if (!inode_points_to_dirent(target, d)) {
1405                 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1406                 if (ret < 0)
1407                         goto err;
1408
1409                 backpointer_exists = ret;
1410                 ret = 0;
1411
1412                 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1413                                 backpointer_exists, c,
1414                                 "directory %llu with multiple links",
1415                                 target->bi_inum)) {
1416                         ret = __remove_dirent(trans, d.k->p);
1417                         goto out;
1418                 }
1419
1420                 if (fsck_err_on(backpointer_exists &&
1421                                 !target->bi_nlink, c,
1422                                 "inode %llu type %s has multiple links but i_nlink 0",
1423                                 target->bi_inum, bch2_d_types[d.v->d_type])) {
1424                         target->bi_nlink++;
1425                         target->bi_flags &= ~BCH_INODE_UNLINKED;
1426
1427                         ret = __write_inode(trans, target, target_snapshot);
1428                         if (ret)
1429                                 goto err;
1430                 }
1431
1432                 if (fsck_err_on(!backpointer_exists, c,
1433                                 "inode %llu:%u has wrong backpointer:\n"
1434                                 "got       %llu:%llu\n"
1435                                 "should be %llu:%llu",
1436                                 target->bi_inum, target_snapshot,
1437                                 target->bi_dir,
1438                                 target->bi_dir_offset,
1439                                 d.k->p.inode,
1440                                 d.k->p.offset)) {
1441                         target->bi_dir          = d.k->p.inode;
1442                         target->bi_dir_offset   = d.k->p.offset;
1443
1444                         ret = __write_inode(trans, target, target_snapshot);
1445                         if (ret)
1446                                 goto err;
1447                 }
1448         }
1449
1450         if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1451                         "incorrect d_type: got %s, should be %s:\n%s",
1452                         bch2_d_type_str(d.v->d_type),
1453                         bch2_d_type_str(inode_d_type(target)),
1454                         (printbuf_reset(&buf),
1455                          bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1456                 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1457                 ret = PTR_ERR_OR_ZERO(n);
1458                 if (ret)
1459                         goto err;
1460
1461                 bkey_reassemble(&n->k_i, d.s_c);
1462                 n->v.d_type = inode_d_type(target);
1463
1464                 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1465                 if (ret)
1466                         goto err;
1467
1468                 d = dirent_i_to_s_c(n);
1469         }
1470
1471         if (d.v->d_type == DT_SUBVOL &&
1472             target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1473             (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1474              fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1475                       le32_to_cpu(d.v->d_parent_subvol),
1476                       target->bi_parent_subvol))) {
1477                 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1478                 ret = PTR_ERR_OR_ZERO(n);
1479                 if (ret)
1480                         goto err;
1481
1482                 bkey_reassemble(&n->k_i, d.s_c);
1483                 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1484
1485                 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1486                 if (ret)
1487                         goto err;
1488
1489                 d = dirent_i_to_s_c(n);
1490         }
1491 out:
1492 err:
1493 fsck_err:
1494         printbuf_exit(&buf);
1495
1496         if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1497                 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1498         return ret;
1499 }
1500
1501 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1502                         struct bkey_s_c k,
1503                         struct bch_hash_info *hash_info,
1504                         struct inode_walker *dir,
1505                         struct inode_walker *target,
1506                         struct snapshots_seen *s)
1507 {
1508         struct bch_fs *c = trans->c;
1509         struct bkey_s_c_dirent d;
1510         struct inode_walker_entry *i;
1511         struct printbuf buf = PRINTBUF;
1512         struct bpos equiv;
1513         int ret = 0;
1514
1515         ret = check_key_has_snapshot(trans, iter, k);
1516         if (ret) {
1517                 ret = ret < 0 ? ret : 0;
1518                 goto out;
1519         }
1520
1521         equiv = k.k->p;
1522         equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1523
1524         ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1525         if (ret)
1526                 goto err;
1527
1528         if (k.k->type == KEY_TYPE_whiteout)
1529                 goto out;
1530
1531         if (dir->cur_inum != k.k->p.inode) {
1532                 ret = check_subdir_count(trans, dir);
1533                 if (ret)
1534                         goto err;
1535         }
1536
1537         BUG_ON(!iter->path->should_be_locked);
1538
1539         ret = __walk_inode(trans, dir, equiv);
1540         if (ret < 0)
1541                 goto err;
1542
1543         if (fsck_err_on(ret == INT_MAX, c,
1544                         "dirent in nonexisting directory:\n%s",
1545                         (printbuf_reset(&buf),
1546                          bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1547                 ret = bch2_btree_delete_at(trans, iter,
1548                                 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1549                 goto out;
1550         }
1551
1552         if (ret == INT_MAX) {
1553                 ret = 0;
1554                 goto out;
1555         }
1556
1557         i = dir->inodes.data + ret;
1558         ret = 0;
1559
1560         if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1561                         "dirent in non directory inode type %s:\n%s",
1562                         bch2_d_type_str(inode_d_type(&i->inode)),
1563                         (printbuf_reset(&buf),
1564                          bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1565                 ret = bch2_btree_delete_at(trans, iter, 0);
1566                 goto out;
1567         }
1568
1569         if (dir->first_this_inode)
1570                 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1571
1572         ret = hash_check_key(trans, bch2_dirent_hash_desc,
1573                              hash_info, iter, k);
1574         if (ret < 0)
1575                 goto err;
1576         if (ret) {
1577                 /* dirent has been deleted */
1578                 ret = 0;
1579                 goto out;
1580         }
1581
1582         if (k.k->type != KEY_TYPE_dirent)
1583                 goto out;
1584
1585         d = bkey_s_c_to_dirent(k);
1586
1587         if (d.v->d_type == DT_SUBVOL) {
1588                 struct bch_inode_unpacked subvol_root;
1589                 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1590                 u32 target_snapshot;
1591                 u64 target_inum;
1592
1593                 ret = __subvol_lookup(trans, target_subvol,
1594                                       &target_snapshot, &target_inum);
1595                 if (ret && ret != -ENOENT)
1596                         goto err;
1597
1598                 if (fsck_err_on(ret, c,
1599                                 "dirent points to missing subvolume %llu",
1600                                 le64_to_cpu(d.v->d_child_subvol))) {
1601                         ret = __remove_dirent(trans, d.k->p);
1602                         goto err;
1603                 }
1604
1605                 ret = __lookup_inode(trans, target_inum,
1606                                    &subvol_root, &target_snapshot);
1607                 if (ret && ret != -ENOENT)
1608                         goto err;
1609
1610                 if (fsck_err_on(ret, c,
1611                                 "subvolume %u points to missing subvolume root %llu",
1612                                 target_subvol,
1613                                 target_inum)) {
1614                         bch_err(c, "repair not implemented yet");
1615                         ret = -EINVAL;
1616                         goto err;
1617                 }
1618
1619                 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1620                                 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1621                                 target_inum,
1622                                 subvol_root.bi_subvol, target_subvol)) {
1623                         subvol_root.bi_subvol = target_subvol;
1624                         ret = __write_inode(trans, &subvol_root, target_snapshot);
1625                         if (ret)
1626                                 goto err;
1627                 }
1628
1629                 ret = check_dirent_target(trans, iter, d, &subvol_root,
1630                                           target_snapshot);
1631                 if (ret)
1632                         goto err;
1633         } else {
1634                 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1635                 if (ret)
1636                         goto err;
1637
1638                 if (fsck_err_on(!target->inodes.nr, c,
1639                                 "dirent points to missing inode: (equiv %u)\n%s",
1640                                 equiv.snapshot,
1641                                 (printbuf_reset(&buf),
1642                                  bch2_bkey_val_to_text(&buf, c, k),
1643                                  buf.buf))) {
1644                         ret = __remove_dirent(trans, d.k->p);
1645                         if (ret)
1646                                 goto err;
1647                 }
1648
1649                 darray_for_each(target->inodes, i) {
1650                         ret = check_dirent_target(trans, iter, d,
1651                                                   &i->inode, i->snapshot);
1652                         if (ret)
1653                                 goto err;
1654                 }
1655         }
1656
1657         if (d.v->d_type == DT_DIR)
1658                 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1659                         i->count++;
1660
1661 out:
1662 err:
1663 fsck_err:
1664         printbuf_exit(&buf);
1665
1666         if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1667                 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1668         return ret;
1669 }
1670
1671 /*
1672  * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1673  * validate d_type
1674  */
1675 noinline_for_stack
1676 static int check_dirents(struct bch_fs *c)
1677 {
1678         struct inode_walker dir = inode_walker_init();
1679         struct inode_walker target = inode_walker_init();
1680         struct snapshots_seen s;
1681         struct bch_hash_info hash_info;
1682         struct btree_trans trans;
1683         struct btree_iter iter;
1684         struct bkey_s_c k;
1685         int ret = 0;
1686
1687         bch_verbose(c, "checking dirents");
1688
1689         snapshots_seen_init(&s);
1690         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1691
1692         ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_dirents,
1693                         POS(BCACHEFS_ROOT_INO, 0),
1694                         BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1695                         k,
1696                         NULL, NULL,
1697                         BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1698                 check_dirent(&trans, &iter, k, &hash_info, &dir, &target, &s));
1699
1700         bch2_trans_exit(&trans);
1701         snapshots_seen_exit(&s);
1702         inode_walker_exit(&dir);
1703         inode_walker_exit(&target);
1704
1705         if (ret)
1706                 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1707         return ret;
1708 }
1709
1710 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1711                        struct bkey_s_c k,
1712                        struct bch_hash_info *hash_info,
1713                        struct inode_walker *inode)
1714 {
1715         struct bch_fs *c = trans->c;
1716         int ret;
1717
1718         ret = check_key_has_snapshot(trans, iter, k);
1719         if (ret)
1720                 return ret;
1721
1722         ret = __walk_inode(trans, inode, k.k->p);
1723         if (ret < 0)
1724                 return ret;
1725
1726         if (fsck_err_on(ret == INT_MAX, c,
1727                         "xattr for missing inode %llu",
1728                         k.k->p.inode))
1729                 return bch2_btree_delete_at(trans, iter, 0);
1730
1731         if (ret == INT_MAX)
1732                 return 0;
1733
1734         ret = 0;
1735
1736         if (inode->first_this_inode)
1737                 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1738
1739         ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1740 fsck_err:
1741         if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1742                 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1743         return ret;
1744 }
1745
1746 /*
1747  * Walk xattrs: verify that they all have a corresponding inode
1748  */
1749 noinline_for_stack
1750 static int check_xattrs(struct bch_fs *c)
1751 {
1752         struct inode_walker inode = inode_walker_init();
1753         struct bch_hash_info hash_info;
1754         struct btree_trans trans;
1755         struct btree_iter iter;
1756         struct bkey_s_c k;
1757         int ret = 0;
1758
1759         bch_verbose(c, "checking xattrs");
1760
1761         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1762
1763         ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_xattrs,
1764                         POS(BCACHEFS_ROOT_INO, 0),
1765                         BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1766                         k,
1767                         NULL, NULL,
1768                         BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1769                 check_xattr(&trans, &iter, k, &hash_info, &inode));
1770
1771         bch2_trans_exit(&trans);
1772
1773         if (ret)
1774                 bch_err(c, "%s(): error %s", __func__, bch2_err_str(ret));
1775         return ret;
1776 }
1777
1778 static int check_root_trans(struct btree_trans *trans)
1779 {
1780         struct bch_fs *c = trans->c;
1781         struct bch_inode_unpacked root_inode;
1782         u32 snapshot;
1783         u64 inum;
1784         int ret;
1785
1786         ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1787         if (ret && ret != -ENOENT)
1788                 return ret;
1789
1790         if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1791                 struct bkey_i_subvolume root_subvol;
1792
1793                 snapshot        = U32_MAX;
1794                 inum            = BCACHEFS_ROOT_INO;
1795
1796                 bkey_subvolume_init(&root_subvol.k_i);
1797                 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1798                 root_subvol.v.flags     = 0;
1799                 root_subvol.v.snapshot  = cpu_to_le32(snapshot);
1800                 root_subvol.v.inode     = cpu_to_le64(inum);
1801                 ret = commit_do(trans, NULL, NULL,
1802                                       BTREE_INSERT_NOFAIL|
1803                                       BTREE_INSERT_LAZY_RW,
1804                         __bch2_btree_insert(trans, BTREE_ID_subvolumes, &root_subvol.k_i));
1805                 if (ret) {
1806                         bch_err(c, "error writing root subvol: %s", bch2_err_str(ret));
1807                         goto err;
1808                 }
1809
1810         }
1811
1812         ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1813         if (ret && ret != -ENOENT)
1814                 return ret;
1815
1816         if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1817             mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1818                                 "root inode not a directory")) {
1819                 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1820                                 0, NULL);
1821                 root_inode.bi_inum = inum;
1822
1823                 ret = __write_inode(trans, &root_inode, snapshot);
1824                 if (ret)
1825                         bch_err(c, "error writing root inode: %s", bch2_err_str(ret));
1826         }
1827 err:
1828 fsck_err:
1829         return ret;
1830 }
1831
1832 /* Get root directory, create if it doesn't exist: */
1833 noinline_for_stack
1834 static int check_root(struct bch_fs *c)
1835 {
1836         bch_verbose(c, "checking root directory");
1837
1838         return bch2_trans_do(c, NULL, NULL,
1839                              BTREE_INSERT_NOFAIL|
1840                              BTREE_INSERT_LAZY_RW,
1841                 check_root_trans(&trans));
1842 }
1843
1844 struct pathbuf_entry {
1845         u64     inum;
1846         u32     snapshot;
1847 };
1848
1849 typedef DARRAY(struct pathbuf_entry) pathbuf;
1850
1851 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1852 {
1853         struct pathbuf_entry *i;
1854
1855         darray_for_each(*p, i)
1856                 if (i->inum     == inum &&
1857                     i->snapshot == snapshot)
1858                         return true;
1859
1860         return false;
1861 }
1862
1863 static int path_down(struct bch_fs *c, pathbuf *p,
1864                      u64 inum, u32 snapshot)
1865 {
1866         int ret = darray_push(p, ((struct pathbuf_entry) {
1867                 .inum           = inum,
1868                 .snapshot       = snapshot,
1869         }));
1870
1871         if (ret)
1872                 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1873                         p->size);
1874         return ret;
1875 }
1876
1877 /*
1878  * Check that a given inode is reachable from the root:
1879  *
1880  * XXX: we should also be verifying that inodes are in the right subvolumes
1881  */
1882 static int check_path(struct btree_trans *trans,
1883                       pathbuf *p,
1884                       struct bch_inode_unpacked *inode,
1885                       u32 snapshot)
1886 {
1887         struct bch_fs *c = trans->c;
1888         int ret = 0;
1889
1890         snapshot = bch2_snapshot_equiv(c, snapshot);
1891         p->nr = 0;
1892
1893         while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1894                  inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1895                 struct btree_iter dirent_iter;
1896                 struct bkey_s_c_dirent d;
1897                 u32 parent_snapshot = snapshot;
1898
1899                 if (inode->bi_subvol) {
1900                         u64 inum;
1901
1902                         ret = subvol_lookup(trans, inode->bi_parent_subvol,
1903                                             &parent_snapshot, &inum);
1904                         if (ret)
1905                                 break;
1906                 }
1907
1908                 ret = lockrestart_do(trans,
1909                         PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
1910                                           SPOS(inode->bi_dir, inode->bi_dir_offset,
1911                                                parent_snapshot))).k));
1912                 if (ret && ret != -ENOENT)
1913                         break;
1914
1915                 if (!ret && !dirent_points_to_inode(d, inode)) {
1916                         bch2_trans_iter_exit(trans, &dirent_iter);
1917                         ret = -ENOENT;
1918                 }
1919
1920                 if (ret == -ENOENT) {
1921                         if (fsck_err(c,  "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
1922                                      inode->bi_inum, snapshot,
1923                                      bch2_d_type_str(inode_d_type(inode)),
1924                                      inode->bi_nlink,
1925                                      inode->bi_dir,
1926                                      inode->bi_dir_offset))
1927                                 ret = reattach_inode(trans, inode, snapshot);
1928                         break;
1929                 }
1930
1931                 bch2_trans_iter_exit(trans, &dirent_iter);
1932
1933                 if (!S_ISDIR(inode->bi_mode))
1934                         break;
1935
1936                 ret = path_down(c, p, inode->bi_inum, snapshot);
1937                 if (ret) {
1938                         bch_err(c, "memory allocation failure");
1939                         return ret;
1940                 }
1941
1942                 snapshot = parent_snapshot;
1943
1944                 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1945                 if (ret) {
1946                         /* Should have been caught in dirents pass */
1947                         bch_err(c, "error looking up parent directory: %i", ret);
1948                         break;
1949                 }
1950
1951                 if (path_is_dup(p, inode->bi_inum, snapshot)) {
1952                         struct pathbuf_entry *i;
1953
1954                         /* XXX print path */
1955                         bch_err(c, "directory structure loop");
1956
1957                         darray_for_each(*p, i)
1958                                 pr_err("%llu:%u", i->inum, i->snapshot);
1959                         pr_err("%llu:%u", inode->bi_inum, snapshot);
1960
1961                         if (!fsck_err(c, "directory structure loop"))
1962                                 return 0;
1963
1964                         ret = commit_do(trans, NULL, NULL,
1965                                               BTREE_INSERT_NOFAIL|
1966                                               BTREE_INSERT_LAZY_RW,
1967                                         remove_backpointer(trans, inode));
1968                         if (ret) {
1969                                 bch_err(c, "error removing dirent: %i", ret);
1970                                 break;
1971                         }
1972
1973                         ret = reattach_inode(trans, inode, snapshot);
1974                 }
1975         }
1976 fsck_err:
1977         if (ret)
1978                 bch_err(c, "%s: err %s", __func__, bch2_err_str(ret));
1979         return ret;
1980 }
1981
1982 /*
1983  * Check for unreachable inodes, as well as loops in the directory structure:
1984  * After check_dirents(), if an inode backpointer doesn't exist that means it's
1985  * unreachable:
1986  */
1987 noinline_for_stack
1988 static int check_directory_structure(struct bch_fs *c)
1989 {
1990         struct btree_trans trans;
1991         struct btree_iter iter;
1992         struct bkey_s_c k;
1993         struct bch_inode_unpacked u;
1994         pathbuf path = { 0, };
1995         int ret;
1996
1997         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1998
1999         for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
2000                            BTREE_ITER_INTENT|
2001                            BTREE_ITER_PREFETCH|
2002                            BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2003                 if (!bkey_is_inode(k.k))
2004                         continue;
2005
2006                 ret = bch2_inode_unpack(k, &u);
2007                 if (ret) {
2008                         /* Should have been caught earlier in fsck: */
2009                         bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2010                         break;
2011                 }
2012
2013                 if (u.bi_flags & BCH_INODE_UNLINKED)
2014                         continue;
2015
2016                 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
2017                 if (ret)
2018                         break;
2019         }
2020         bch2_trans_iter_exit(&trans, &iter);
2021
2022         darray_exit(&path);
2023
2024         bch2_trans_exit(&trans);
2025         return ret;
2026 }
2027
2028 struct nlink_table {
2029         size_t          nr;
2030         size_t          size;
2031
2032         struct nlink {
2033                 u64     inum;
2034                 u32     snapshot;
2035                 u32     count;
2036         }               *d;
2037 };
2038
2039 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2040                      u64 inum, u32 snapshot)
2041 {
2042         if (t->nr == t->size) {
2043                 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2044                 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2045
2046                 if (!d) {
2047                         bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2048                                 new_size);
2049                         return -ENOMEM;
2050                 }
2051
2052                 if (t->d)
2053                         memcpy(d, t->d, t->size * sizeof(t->d[0]));
2054                 kvfree(t->d);
2055
2056                 t->d = d;
2057                 t->size = new_size;
2058         }
2059
2060
2061         t->d[t->nr++] = (struct nlink) {
2062                 .inum           = inum,
2063                 .snapshot       = snapshot,
2064         };
2065
2066         return 0;
2067 }
2068
2069 static int nlink_cmp(const void *_l, const void *_r)
2070 {
2071         const struct nlink *l = _l;
2072         const struct nlink *r = _r;
2073
2074         return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2075 }
2076
2077 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2078                      struct nlink_table *links,
2079                      u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2080 {
2081         struct nlink *link, key = {
2082                 .inum = inum, .snapshot = U32_MAX,
2083         };
2084
2085         if (inum < range_start || inum >= range_end)
2086                 return;
2087
2088         link = __inline_bsearch(&key, links->d, links->nr,
2089                                 sizeof(links->d[0]), nlink_cmp);
2090         if (!link)
2091                 return;
2092
2093         while (link > links->d && link[0].inum == link[-1].inum)
2094                 --link;
2095
2096         for (; link < links->d + links->nr && link->inum == inum; link++)
2097                 if (ref_visible(c, s, snapshot, link->snapshot)) {
2098                         link->count++;
2099                         if (link->snapshot >= snapshot)
2100                                 break;
2101                 }
2102 }
2103
2104 noinline_for_stack
2105 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2106                                        struct nlink_table *t,
2107                                        u64 start, u64 *end)
2108 {
2109         struct btree_trans trans;
2110         struct btree_iter iter;
2111         struct bkey_s_c k;
2112         struct bch_inode_unpacked u;
2113         int ret = 0;
2114
2115         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2116
2117         for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2118                            POS(0, start),
2119                            BTREE_ITER_INTENT|
2120                            BTREE_ITER_PREFETCH|
2121                            BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2122                 if (!bkey_is_inode(k.k))
2123                         continue;
2124
2125                 /* Should never fail, checked by bch2_inode_invalid: */
2126                 BUG_ON(bch2_inode_unpack(k, &u));
2127
2128                 /*
2129                  * Backpointer and directory structure checks are sufficient for
2130                  * directories, since they can't have hardlinks:
2131                  */
2132                 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2133                         continue;
2134
2135                 if (!u.bi_nlink)
2136                         continue;
2137
2138                 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2139                 if (ret) {
2140                         *end = k.k->p.offset;
2141                         ret = 0;
2142                         break;
2143                 }
2144
2145         }
2146         bch2_trans_iter_exit(&trans, &iter);
2147         bch2_trans_exit(&trans);
2148
2149         if (ret)
2150                 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2151
2152         return ret;
2153 }
2154
2155 noinline_for_stack
2156 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2157                                      u64 range_start, u64 range_end)
2158 {
2159         struct btree_trans trans;
2160         struct snapshots_seen s;
2161         struct btree_iter iter;
2162         struct bkey_s_c k;
2163         struct bkey_s_c_dirent d;
2164         int ret;
2165
2166         snapshots_seen_init(&s);
2167
2168         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2169
2170         for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2171                            BTREE_ITER_INTENT|
2172                            BTREE_ITER_PREFETCH|
2173                            BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2174                 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2175                 if (ret)
2176                         break;
2177
2178                 switch (k.k->type) {
2179                 case KEY_TYPE_dirent:
2180                         d = bkey_s_c_to_dirent(k);
2181
2182                         if (d.v->d_type != DT_DIR &&
2183                             d.v->d_type != DT_SUBVOL)
2184                                 inc_link(c, &s, links, range_start, range_end,
2185                                          le64_to_cpu(d.v->d_inum),
2186                                          bch2_snapshot_equiv(c, d.k->p.snapshot));
2187                         break;
2188                 }
2189         }
2190         bch2_trans_iter_exit(&trans, &iter);
2191
2192         if (ret)
2193                 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2194
2195         bch2_trans_exit(&trans);
2196         snapshots_seen_exit(&s);
2197         return ret;
2198 }
2199
2200 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2201                                      struct bkey_s_c k,
2202                                      struct nlink_table *links,
2203                                      size_t *idx, u64 range_end)
2204 {
2205         struct bch_fs *c = trans->c;
2206         struct bch_inode_unpacked u;
2207         struct nlink *link = &links->d[*idx];
2208         int ret = 0;
2209
2210         if (k.k->p.offset >= range_end)
2211                 return 1;
2212
2213         if (!bkey_is_inode(k.k))
2214                 return 0;
2215
2216         BUG_ON(bch2_inode_unpack(k, &u));
2217
2218         if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2219                 return 0;
2220
2221         if (!u.bi_nlink)
2222                 return 0;
2223
2224         while ((cmp_int(link->inum, k.k->p.offset) ?:
2225                 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2226                 BUG_ON(*idx == links->nr);
2227                 link = &links->d[++*idx];
2228         }
2229
2230         if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2231                         "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2232                         u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2233                         bch2_inode_nlink_get(&u), link->count)) {
2234                 bch2_inode_nlink_set(&u, link->count);
2235                 ret = __write_inode(trans, &u, k.k->p.snapshot);
2236         }
2237 fsck_err:
2238         return ret;
2239 }
2240
2241 noinline_for_stack
2242 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2243                                struct nlink_table *links,
2244                                u64 range_start, u64 range_end)
2245 {
2246         struct btree_trans trans;
2247         struct btree_iter iter;
2248         struct bkey_s_c k;
2249         size_t idx = 0;
2250         int ret = 0;
2251
2252         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2253
2254         ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
2255                         POS(0, range_start),
2256                         BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2257                         NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
2258                 check_nlinks_update_inode(&trans, &iter, k, links, &idx, range_end));
2259
2260         bch2_trans_exit(&trans);
2261
2262         if (ret < 0) {
2263                 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2264                 return ret;
2265         }
2266
2267         return 0;
2268 }
2269
2270 noinline_for_stack
2271 static int check_nlinks(struct bch_fs *c)
2272 {
2273         struct nlink_table links = { 0 };
2274         u64 this_iter_range_start, next_iter_range_start = 0;
2275         int ret = 0;
2276
2277         bch_verbose(c, "checking inode nlinks");
2278
2279         do {
2280                 this_iter_range_start = next_iter_range_start;
2281                 next_iter_range_start = U64_MAX;
2282
2283                 ret = check_nlinks_find_hardlinks(c, &links,
2284                                                   this_iter_range_start,
2285                                                   &next_iter_range_start);
2286
2287                 ret = check_nlinks_walk_dirents(c, &links,
2288                                           this_iter_range_start,
2289                                           next_iter_range_start);
2290                 if (ret)
2291                         break;
2292
2293                 ret = check_nlinks_update_hardlinks(c, &links,
2294                                          this_iter_range_start,
2295                                          next_iter_range_start);
2296                 if (ret)
2297                         break;
2298
2299                 links.nr = 0;
2300         } while (next_iter_range_start != U64_MAX);
2301
2302         kvfree(links.d);
2303
2304         return ret;
2305 }
2306
2307 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2308                              struct bkey_s_c k)
2309 {
2310         struct bkey_s_c_reflink_p p;
2311         struct bkey_i_reflink_p *u;
2312         int ret;
2313
2314         if (k.k->type != KEY_TYPE_reflink_p)
2315                 return 0;
2316
2317         p = bkey_s_c_to_reflink_p(k);
2318
2319         if (!p.v->front_pad && !p.v->back_pad)
2320                 return 0;
2321
2322         u = bch2_trans_kmalloc(trans, sizeof(*u));
2323         ret = PTR_ERR_OR_ZERO(u);
2324         if (ret)
2325                 return ret;
2326
2327         bkey_reassemble(&u->k_i, k);
2328         u->v.front_pad  = 0;
2329         u->v.back_pad   = 0;
2330
2331         return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2332 }
2333
2334 noinline_for_stack
2335 static int fix_reflink_p(struct bch_fs *c)
2336 {
2337         struct btree_trans trans;
2338         struct btree_iter iter;
2339         struct bkey_s_c k;
2340         int ret;
2341
2342         if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2343                 return 0;
2344
2345         bch_verbose(c, "fixing reflink_p keys");
2346
2347         bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2348
2349         ret = for_each_btree_key_commit(&trans, iter,
2350                         BTREE_ID_extents, POS_MIN,
2351                         BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2352                         NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
2353                 fix_reflink_p_key(&trans, &iter, k));
2354
2355         bch2_trans_exit(&trans);
2356         return ret;
2357 }
2358
2359 /*
2360  * Checks for inconsistencies that shouldn't happen, unless we have a bug.
2361  * Doesn't fix them yet, mainly because they haven't yet been observed:
2362  */
2363 int bch2_fsck_full(struct bch_fs *c)
2364 {
2365         int ret;
2366 again:
2367         ret =   bch2_fs_check_snapshots(c) ?:
2368                 bch2_fs_check_subvols(c) ?:
2369                 bch2_delete_dead_snapshots(c) ?:
2370                 check_inodes(c, true) ?:
2371                 check_extents(c) ?:
2372                 check_dirents(c) ?:
2373                 check_xattrs(c) ?:
2374                 check_root(c) ?:
2375                 check_directory_structure(c) ?:
2376                 check_nlinks(c) ?:
2377                 fix_reflink_p(c);
2378
2379         if (bch2_err_matches(ret, BCH_ERR_need_snapshot_cleanup)) {
2380                 set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
2381                 goto again;
2382         }
2383
2384         return ret;
2385 }
2386
2387 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
2388 {
2389         return  bch2_fs_check_snapshots(c) ?:
2390                 bch2_fs_check_subvols(c) ?:
2391                 bch2_delete_dead_snapshots(c) ?:
2392                 check_inodes(c, false);
2393 }