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