xref: /linux/fs/bcachefs/fsck.c (revision 566ab427f827b0256d3e8ce0235d088e6a9c28bd)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "bcachefs.h"
4 #include "bkey_buf.h"
5 #include "btree_cache.h"
6 #include "btree_update.h"
7 #include "buckets.h"
8 #include "darray.h"
9 #include "dirent.h"
10 #include "error.h"
11 #include "fs.h"
12 #include "fs-common.h"
13 #include "fsck.h"
14 #include "inode.h"
15 #include "keylist.h"
16 #include "recovery_passes.h"
17 #include "snapshot.h"
18 #include "super.h"
19 #include "xattr.h"
20 
21 #include <linux/bsearch.h>
22 #include <linux/dcache.h> /* struct qstr */
23 
24 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
25 				   struct bkey_s_c_dirent d)
26 {
27 	return  inode->bi_dir		== d.k->p.inode &&
28 		inode->bi_dir_offset	== d.k->p.offset;
29 }
30 
31 static int dirent_points_to_inode_nowarn(struct bkey_s_c_dirent d,
32 					 struct bch_inode_unpacked *inode)
33 {
34 	if (d.v->d_type == DT_SUBVOL
35 	    ? le32_to_cpu(d.v->d_child_subvol)	== inode->bi_subvol
36 	    : le64_to_cpu(d.v->d_inum)		== inode->bi_inum)
37 		return 0;
38 	return -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
39 }
40 
41 static void dirent_inode_mismatch_msg(struct printbuf *out,
42 				      struct bch_fs *c,
43 				      struct bkey_s_c_dirent dirent,
44 				      struct bch_inode_unpacked *inode)
45 {
46 	prt_str(out, "inode points to dirent that does not point back:");
47 	prt_newline(out);
48 	bch2_bkey_val_to_text(out, c, dirent.s_c);
49 	prt_newline(out);
50 	bch2_inode_unpacked_to_text(out, inode);
51 }
52 
53 static int dirent_points_to_inode(struct bch_fs *c,
54 				  struct bkey_s_c_dirent dirent,
55 				  struct bch_inode_unpacked *inode)
56 {
57 	int ret = dirent_points_to_inode_nowarn(dirent, inode);
58 	if (ret) {
59 		struct printbuf buf = PRINTBUF;
60 		dirent_inode_mismatch_msg(&buf, c, dirent, inode);
61 		bch_warn(c, "%s", buf.buf);
62 		printbuf_exit(&buf);
63 	}
64 	return ret;
65 }
66 
67 /*
68  * XXX: this is handling transaction restarts without returning
69  * -BCH_ERR_transaction_restart_nested, this is not how we do things anymore:
70  */
71 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
72 				    u32 snapshot)
73 {
74 	u64 sectors = 0;
75 
76 	int ret = for_each_btree_key_upto(trans, iter, BTREE_ID_extents,
77 				SPOS(inum, 0, snapshot),
78 				POS(inum, U64_MAX),
79 				0, k, ({
80 		if (bkey_extent_is_allocation(k.k))
81 			sectors += k.k->size;
82 		0;
83 	}));
84 
85 	return ret ?: sectors;
86 }
87 
88 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
89 				    u32 snapshot)
90 {
91 	u64 subdirs = 0;
92 
93 	int ret = for_each_btree_key_upto(trans, iter, BTREE_ID_dirents,
94 				    SPOS(inum, 0, snapshot),
95 				    POS(inum, U64_MAX),
96 				    0, k, ({
97 		if (k.k->type == KEY_TYPE_dirent &&
98 		    bkey_s_c_to_dirent(k).v->d_type == DT_DIR)
99 			subdirs++;
100 		0;
101 	}));
102 
103 	return ret ?: subdirs;
104 }
105 
106 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
107 			 u32 *snapshot, u64 *inum)
108 {
109 	struct bch_subvolume s;
110 	int ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
111 
112 	*snapshot = le32_to_cpu(s.snapshot);
113 	*inum = le64_to_cpu(s.inode);
114 	return ret;
115 }
116 
117 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
118 			      struct bch_inode_unpacked *inode)
119 {
120 	struct btree_iter iter;
121 	struct bkey_s_c k;
122 	int ret;
123 
124 	for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inode_nr),
125 				     BTREE_ITER_all_snapshots, k, ret) {
126 		if (k.k->p.offset != inode_nr)
127 			break;
128 		if (!bkey_is_inode(k.k))
129 			continue;
130 		ret = bch2_inode_unpack(k, inode);
131 		goto found;
132 	}
133 	ret = -BCH_ERR_ENOENT_inode;
134 found:
135 	bch_err_msg(trans->c, ret, "fetching inode %llu", inode_nr);
136 	bch2_trans_iter_exit(trans, &iter);
137 	return ret;
138 }
139 
140 static int lookup_inode(struct btree_trans *trans, u64 inode_nr, u32 snapshot,
141 			struct bch_inode_unpacked *inode)
142 {
143 	struct btree_iter iter;
144 	struct bkey_s_c k;
145 	int ret;
146 
147 	k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
148 			       SPOS(0, inode_nr, snapshot), 0);
149 	ret = bkey_err(k);
150 	if (ret)
151 		goto err;
152 
153 	ret = bkey_is_inode(k.k)
154 		? bch2_inode_unpack(k, inode)
155 		: -BCH_ERR_ENOENT_inode;
156 err:
157 	bch2_trans_iter_exit(trans, &iter);
158 	return ret;
159 }
160 
161 static int lookup_dirent_in_snapshot(struct btree_trans *trans,
162 			   struct bch_hash_info hash_info,
163 			   subvol_inum dir, struct qstr *name,
164 			   u64 *target, unsigned *type, u32 snapshot)
165 {
166 	struct btree_iter iter;
167 	struct bkey_s_c k = bch2_hash_lookup_in_snapshot(trans, &iter, bch2_dirent_hash_desc,
168 							 &hash_info, dir, name, 0, snapshot);
169 	int ret = bkey_err(k);
170 	if (ret)
171 		return ret;
172 
173 	struct bkey_s_c_dirent d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
174 	*target = le64_to_cpu(d.v->d_inum);
175 	*type = d.v->d_type;
176 	bch2_trans_iter_exit(trans, &iter);
177 	return 0;
178 }
179 
180 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
181 {
182 	struct bch_fs *c = trans->c;
183 	struct btree_iter iter;
184 	struct bch_inode_unpacked dir_inode;
185 	struct bch_hash_info dir_hash_info;
186 	int ret;
187 
188 	ret = lookup_first_inode(trans, pos.inode, &dir_inode);
189 	if (ret)
190 		goto err;
191 
192 	dir_hash_info = bch2_hash_info_init(c, &dir_inode);
193 
194 	bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_intent);
195 
196 	ret =   bch2_btree_iter_traverse(&iter) ?:
197 		bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
198 				    &dir_hash_info, &iter,
199 				    BTREE_UPDATE_internal_snapshot_node);
200 	bch2_trans_iter_exit(trans, &iter);
201 err:
202 	bch_err_fn(c, ret);
203 	return ret;
204 }
205 
206 /* Get lost+found, create if it doesn't exist: */
207 static int lookup_lostfound(struct btree_trans *trans, u32 snapshot,
208 			    struct bch_inode_unpacked *lostfound,
209 			    u64 reattaching_inum)
210 {
211 	struct bch_fs *c = trans->c;
212 	struct qstr lostfound_str = QSTR("lost+found");
213 	u64 inum = 0;
214 	unsigned d_type = 0;
215 	int ret;
216 
217 	struct bch_snapshot_tree st;
218 	ret = bch2_snapshot_tree_lookup(trans,
219 			bch2_snapshot_tree(c, snapshot), &st);
220 	if (ret)
221 		return ret;
222 
223 	subvol_inum root_inum = { .subvol = le32_to_cpu(st.master_subvol) };
224 
225 	struct bch_subvolume subvol;
226 	ret = bch2_subvolume_get(trans, le32_to_cpu(st.master_subvol),
227 				 false, 0, &subvol);
228 	bch_err_msg(c, ret, "looking up root subvol %u for snapshot %u",
229 		    le32_to_cpu(st.master_subvol), snapshot);
230 	if (ret)
231 		return ret;
232 
233 	if (!subvol.inode) {
234 		struct btree_iter iter;
235 		struct bkey_i_subvolume *subvol = bch2_bkey_get_mut_typed(trans, &iter,
236 				BTREE_ID_subvolumes, POS(0, le32_to_cpu(st.master_subvol)),
237 				0, subvolume);
238 		ret = PTR_ERR_OR_ZERO(subvol);
239 		if (ret)
240 			return ret;
241 
242 		subvol->v.inode = cpu_to_le64(reattaching_inum);
243 		bch2_trans_iter_exit(trans, &iter);
244 	}
245 
246 	root_inum.inum = le64_to_cpu(subvol.inode);
247 
248 	struct bch_inode_unpacked root_inode;
249 	struct bch_hash_info root_hash_info;
250 	ret = lookup_inode(trans, root_inum.inum, snapshot, &root_inode);
251 	bch_err_msg(c, ret, "looking up root inode %llu for subvol %u",
252 		    root_inum.inum, le32_to_cpu(st.master_subvol));
253 	if (ret)
254 		return ret;
255 
256 	root_hash_info = bch2_hash_info_init(c, &root_inode);
257 
258 	ret = lookup_dirent_in_snapshot(trans, root_hash_info, root_inum,
259 			      &lostfound_str, &inum, &d_type, snapshot);
260 	if (bch2_err_matches(ret, ENOENT))
261 		goto create_lostfound;
262 
263 	bch_err_fn(c, ret);
264 	if (ret)
265 		return ret;
266 
267 	if (d_type != DT_DIR) {
268 		bch_err(c, "error looking up lost+found: not a directory");
269 		return -BCH_ERR_ENOENT_not_directory;
270 	}
271 
272 	/*
273 	 * The bch2_check_dirents pass has already run, dangling dirents
274 	 * shouldn't exist here:
275 	 */
276 	ret = lookup_inode(trans, inum, snapshot, lostfound);
277 	bch_err_msg(c, ret, "looking up lost+found %llu:%u in (root inode %llu, snapshot root %u)",
278 		    inum, snapshot, root_inum.inum, bch2_snapshot_root(c, snapshot));
279 	return ret;
280 
281 create_lostfound:
282 	/*
283 	 * we always create lost+found in the root snapshot; we don't want
284 	 * different branches of the snapshot tree to have different lost+found
285 	 */
286 	snapshot = le32_to_cpu(st.root_snapshot);
287 	/*
288 	 * XXX: we could have a nicer log message here  if we had a nice way to
289 	 * walk backpointers to print a path
290 	 */
291 	bch_notice(c, "creating lost+found in subvol %llu snapshot %u",
292 		   root_inum.subvol, le32_to_cpu(st.root_snapshot));
293 
294 	u64 now = bch2_current_time(c);
295 	struct btree_iter lostfound_iter = { NULL };
296 	u64 cpu = raw_smp_processor_id();
297 
298 	bch2_inode_init_early(c, lostfound);
299 	bch2_inode_init_late(lostfound, now, 0, 0, S_IFDIR|0700, 0, &root_inode);
300 	lostfound->bi_dir = root_inode.bi_inum;
301 	lostfound->bi_snapshot = le32_to_cpu(st.root_snapshot);
302 
303 	root_inode.bi_nlink++;
304 
305 	ret = bch2_inode_create(trans, &lostfound_iter, lostfound, snapshot, cpu);
306 	if (ret)
307 		goto err;
308 
309 	bch2_btree_iter_set_snapshot(&lostfound_iter, snapshot);
310 	ret = bch2_btree_iter_traverse(&lostfound_iter);
311 	if (ret)
312 		goto err;
313 
314 	ret =   bch2_dirent_create_snapshot(trans,
315 				0, root_inode.bi_inum, snapshot, &root_hash_info,
316 				mode_to_type(lostfound->bi_mode),
317 				&lostfound_str,
318 				lostfound->bi_inum,
319 				&lostfound->bi_dir_offset,
320 				STR_HASH_must_create) ?:
321 		bch2_inode_write_flags(trans, &lostfound_iter, lostfound,
322 				       BTREE_UPDATE_internal_snapshot_node);
323 err:
324 	bch_err_msg(c, ret, "creating lost+found");
325 	bch2_trans_iter_exit(trans, &lostfound_iter);
326 	return ret;
327 }
328 
329 static int reattach_inode(struct btree_trans *trans, struct bch_inode_unpacked *inode)
330 {
331 	struct bch_fs *c = trans->c;
332 	struct bch_hash_info dir_hash;
333 	struct bch_inode_unpacked lostfound;
334 	char name_buf[20];
335 	struct qstr name;
336 	u64 dir_offset = 0;
337 	u32 dirent_snapshot = inode->bi_snapshot;
338 	int ret;
339 
340 	if (inode->bi_subvol) {
341 		inode->bi_parent_subvol = BCACHEFS_ROOT_SUBVOL;
342 
343 		u64 root_inum;
344 		ret = subvol_lookup(trans, inode->bi_parent_subvol,
345 				    &dirent_snapshot, &root_inum);
346 		if (ret)
347 			return ret;
348 
349 		snprintf(name_buf, sizeof(name_buf), "subvol-%u", inode->bi_subvol);
350 	} else {
351 		snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
352 	}
353 
354 	ret = lookup_lostfound(trans, dirent_snapshot, &lostfound, inode->bi_inum);
355 	if (ret)
356 		return ret;
357 
358 	lostfound.bi_nlink += S_ISDIR(inode->bi_mode);
359 
360 	/* ensure lost+found inode is also present in inode snapshot */
361 	if (!inode->bi_subvol) {
362 		BUG_ON(!bch2_snapshot_is_ancestor(c, inode->bi_snapshot, lostfound.bi_snapshot));
363 		lostfound.bi_snapshot = inode->bi_snapshot;
364 	}
365 
366 	ret = __bch2_fsck_write_inode(trans, &lostfound);
367 	if (ret)
368 		return ret;
369 
370 	dir_hash = bch2_hash_info_init(c, &lostfound);
371 
372 	name = (struct qstr) QSTR(name_buf);
373 
374 	ret = bch2_dirent_create_snapshot(trans,
375 				inode->bi_parent_subvol, lostfound.bi_inum,
376 				dirent_snapshot,
377 				&dir_hash,
378 				inode_d_type(inode),
379 				&name,
380 				inode->bi_subvol ?: inode->bi_inum,
381 				&dir_offset,
382 				STR_HASH_must_create);
383 	if (ret) {
384 		bch_err_msg(c, ret, "error creating dirent");
385 		return ret;
386 	}
387 
388 	inode->bi_dir		= lostfound.bi_inum;
389 	inode->bi_dir_offset	= dir_offset;
390 
391 	return __bch2_fsck_write_inode(trans, inode);
392 }
393 
394 static int remove_backpointer(struct btree_trans *trans,
395 			      struct bch_inode_unpacked *inode)
396 {
397 	if (!inode->bi_dir)
398 		return 0;
399 
400 	struct bch_fs *c = trans->c;
401 	struct btree_iter iter;
402 	struct bkey_s_c_dirent d =
403 		bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_dirents,
404 				     SPOS(inode->bi_dir, inode->bi_dir_offset, inode->bi_snapshot), 0,
405 				     dirent);
406 	int ret =   bkey_err(d) ?:
407 		dirent_points_to_inode(c, d, inode) ?:
408 		__remove_dirent(trans, d.k->p);
409 	bch2_trans_iter_exit(trans, &iter);
410 	return ret;
411 }
412 
413 static int reattach_subvol(struct btree_trans *trans, struct bkey_s_c_subvolume s)
414 {
415 	struct bch_fs *c = trans->c;
416 
417 	struct bch_inode_unpacked inode;
418 	int ret = bch2_inode_find_by_inum_trans(trans,
419 				(subvol_inum) { s.k->p.offset, le64_to_cpu(s.v->inode) },
420 				&inode);
421 	if (ret)
422 		return ret;
423 
424 	ret = remove_backpointer(trans, &inode);
425 	if (!bch2_err_matches(ret, ENOENT))
426 		bch_err_msg(c, ret, "removing dirent");
427 	if (ret)
428 		return ret;
429 
430 	ret = reattach_inode(trans, &inode);
431 	bch_err_msg(c, ret, "reattaching inode %llu", inode.bi_inum);
432 	return ret;
433 }
434 
435 static int reconstruct_subvol(struct btree_trans *trans, u32 snapshotid, u32 subvolid, u64 inum)
436 {
437 	struct bch_fs *c = trans->c;
438 
439 	if (!bch2_snapshot_is_leaf(c, snapshotid)) {
440 		bch_err(c, "need to reconstruct subvol, but have interior node snapshot");
441 		return -BCH_ERR_fsck_repair_unimplemented;
442 	}
443 
444 	/*
445 	 * If inum isn't set, that means we're being called from check_dirents,
446 	 * not check_inodes - the root of this subvolume doesn't exist or we
447 	 * would have found it there:
448 	 */
449 	if (!inum) {
450 		struct btree_iter inode_iter = {};
451 		struct bch_inode_unpacked new_inode;
452 		u64 cpu = raw_smp_processor_id();
453 
454 		bch2_inode_init_early(c, &new_inode);
455 		bch2_inode_init_late(&new_inode, bch2_current_time(c), 0, 0, S_IFDIR|0755, 0, NULL);
456 
457 		new_inode.bi_subvol = subvolid;
458 
459 		int ret = bch2_inode_create(trans, &inode_iter, &new_inode, snapshotid, cpu) ?:
460 			  bch2_btree_iter_traverse(&inode_iter) ?:
461 			  bch2_inode_write(trans, &inode_iter, &new_inode);
462 		bch2_trans_iter_exit(trans, &inode_iter);
463 		if (ret)
464 			return ret;
465 
466 		inum = new_inode.bi_inum;
467 	}
468 
469 	bch_info(c, "reconstructing subvol %u with root inode %llu", subvolid, inum);
470 
471 	struct bkey_i_subvolume *new_subvol = bch2_trans_kmalloc(trans, sizeof(*new_subvol));
472 	int ret = PTR_ERR_OR_ZERO(new_subvol);
473 	if (ret)
474 		return ret;
475 
476 	bkey_subvolume_init(&new_subvol->k_i);
477 	new_subvol->k.p.offset	= subvolid;
478 	new_subvol->v.snapshot	= cpu_to_le32(snapshotid);
479 	new_subvol->v.inode	= cpu_to_le64(inum);
480 	ret = bch2_btree_insert_trans(trans, BTREE_ID_subvolumes, &new_subvol->k_i, 0);
481 	if (ret)
482 		return ret;
483 
484 	struct btree_iter iter;
485 	struct bkey_i_snapshot *s = bch2_bkey_get_mut_typed(trans, &iter,
486 			BTREE_ID_snapshots, POS(0, snapshotid),
487 			0, snapshot);
488 	ret = PTR_ERR_OR_ZERO(s);
489 	bch_err_msg(c, ret, "getting snapshot %u", snapshotid);
490 	if (ret)
491 		return ret;
492 
493 	u32 snapshot_tree = le32_to_cpu(s->v.tree);
494 
495 	s->v.subvol = cpu_to_le32(subvolid);
496 	SET_BCH_SNAPSHOT_SUBVOL(&s->v, true);
497 	bch2_trans_iter_exit(trans, &iter);
498 
499 	struct bkey_i_snapshot_tree *st = bch2_bkey_get_mut_typed(trans, &iter,
500 			BTREE_ID_snapshot_trees, POS(0, snapshot_tree),
501 			0, snapshot_tree);
502 	ret = PTR_ERR_OR_ZERO(st);
503 	bch_err_msg(c, ret, "getting snapshot tree %u", snapshot_tree);
504 	if (ret)
505 		return ret;
506 
507 	if (!st->v.master_subvol)
508 		st->v.master_subvol = cpu_to_le32(subvolid);
509 
510 	bch2_trans_iter_exit(trans, &iter);
511 	return 0;
512 }
513 
514 static int reconstruct_inode(struct btree_trans *trans, enum btree_id btree, u32 snapshot, u64 inum)
515 {
516 	struct bch_fs *c = trans->c;
517 	unsigned i_mode = S_IFREG;
518 	u64 i_size = 0;
519 
520 	switch (btree) {
521 	case BTREE_ID_extents: {
522 		struct btree_iter iter = {};
523 
524 		bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, SPOS(inum, U64_MAX, snapshot), 0);
525 		struct bkey_s_c k = bch2_btree_iter_peek_prev(&iter);
526 		bch2_trans_iter_exit(trans, &iter);
527 		int ret = bkey_err(k);
528 		if (ret)
529 			return ret;
530 
531 		i_size = k.k->p.offset << 9;
532 		break;
533 	}
534 	case BTREE_ID_dirents:
535 		i_mode = S_IFDIR;
536 		break;
537 	case BTREE_ID_xattrs:
538 		break;
539 	default:
540 		BUG();
541 	}
542 
543 	struct bch_inode_unpacked new_inode;
544 	bch2_inode_init_early(c, &new_inode);
545 	bch2_inode_init_late(&new_inode, bch2_current_time(c), 0, 0, i_mode|0600, 0, NULL);
546 	new_inode.bi_size = i_size;
547 	new_inode.bi_inum = inum;
548 	new_inode.bi_snapshot = snapshot;
549 
550 	return __bch2_fsck_write_inode(trans, &new_inode);
551 }
552 
553 struct snapshots_seen {
554 	struct bpos			pos;
555 	snapshot_id_list		ids;
556 };
557 
558 static inline void snapshots_seen_exit(struct snapshots_seen *s)
559 {
560 	darray_exit(&s->ids);
561 }
562 
563 static inline void snapshots_seen_init(struct snapshots_seen *s)
564 {
565 	memset(s, 0, sizeof(*s));
566 }
567 
568 static int snapshots_seen_add_inorder(struct bch_fs *c, struct snapshots_seen *s, u32 id)
569 {
570 	u32 *i;
571 	__darray_for_each(s->ids, i) {
572 		if (*i == id)
573 			return 0;
574 		if (*i > id)
575 			break;
576 	}
577 
578 	int ret = darray_insert_item(&s->ids, i - s->ids.data, id);
579 	if (ret)
580 		bch_err(c, "error reallocating snapshots_seen table (size %zu)",
581 			s->ids.size);
582 	return ret;
583 }
584 
585 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
586 				 enum btree_id btree_id, struct bpos pos)
587 {
588 	if (!bkey_eq(s->pos, pos))
589 		s->ids.nr = 0;
590 	s->pos = pos;
591 
592 	return snapshot_list_add_nodup(c, &s->ids, pos.snapshot);
593 }
594 
595 /**
596  * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
597  * and @ancestor hasn't been overwritten in @seen
598  *
599  * @c:		filesystem handle
600  * @seen:	list of snapshot ids already seen at current position
601  * @id:		descendent snapshot id
602  * @ancestor:	ancestor snapshot id
603  *
604  * Returns:	whether key in @ancestor snapshot is visible in @id snapshot
605  */
606 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
607 				    u32 id, u32 ancestor)
608 {
609 	ssize_t i;
610 
611 	EBUG_ON(id > ancestor);
612 
613 	/* @ancestor should be the snapshot most recently added to @seen */
614 	EBUG_ON(ancestor != seen->pos.snapshot);
615 	EBUG_ON(ancestor != darray_last(seen->ids));
616 
617 	if (id == ancestor)
618 		return true;
619 
620 	if (!bch2_snapshot_is_ancestor(c, id, ancestor))
621 		return false;
622 
623 	/*
624 	 * We know that @id is a descendant of @ancestor, we're checking if
625 	 * we've seen a key that overwrote @ancestor - i.e. also a descendent of
626 	 * @ascestor and with @id as a descendent.
627 	 *
628 	 * But we already know that we're scanning IDs between @id and @ancestor
629 	 * numerically, since snapshot ID lists are kept sorted, so if we find
630 	 * an id that's an ancestor of @id we're done:
631 	 */
632 
633 	for (i = seen->ids.nr - 2;
634 	     i >= 0 && seen->ids.data[i] >= id;
635 	     --i)
636 		if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i]))
637 			return false;
638 
639 	return true;
640 }
641 
642 /**
643  * ref_visible - given a key with snapshot id @src that points to a key with
644  * snapshot id @dst, test whether there is some snapshot in which @dst is
645  * visible.
646  *
647  * @c:		filesystem handle
648  * @s:		list of snapshot IDs already seen at @src
649  * @src:	snapshot ID of src key
650  * @dst:	snapshot ID of dst key
651  * Returns:	true if there is some snapshot in which @dst is visible
652  *
653  * Assumes we're visiting @src keys in natural key order
654  */
655 static bool ref_visible(struct bch_fs *c, struct snapshots_seen *s,
656 			u32 src, u32 dst)
657 {
658 	return dst <= src
659 		? key_visible_in_snapshot(c, s, dst, src)
660 		: bch2_snapshot_is_ancestor(c, src, dst);
661 }
662 
663 static int ref_visible2(struct bch_fs *c,
664 			u32 src, struct snapshots_seen *src_seen,
665 			u32 dst, struct snapshots_seen *dst_seen)
666 {
667 	if (dst > src) {
668 		swap(dst, src);
669 		swap(dst_seen, src_seen);
670 	}
671 	return key_visible_in_snapshot(c, src_seen, dst, src);
672 }
673 
674 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i)				\
675 	for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr &&	\
676 	     (_i)->snapshot <= (_snapshot); _i++)					\
677 		if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
678 
679 struct inode_walker_entry {
680 	struct bch_inode_unpacked inode;
681 	u32			snapshot;
682 	u64			count;
683 };
684 
685 struct inode_walker {
686 	bool				first_this_inode;
687 	bool				have_inodes;
688 	bool				recalculate_sums;
689 	struct bpos			last_pos;
690 
691 	DARRAY(struct inode_walker_entry) inodes;
692 };
693 
694 static void inode_walker_exit(struct inode_walker *w)
695 {
696 	darray_exit(&w->inodes);
697 }
698 
699 static struct inode_walker inode_walker_init(void)
700 {
701 	return (struct inode_walker) { 0, };
702 }
703 
704 static int add_inode(struct bch_fs *c, struct inode_walker *w,
705 		     struct bkey_s_c inode)
706 {
707 	struct bch_inode_unpacked u;
708 
709 	BUG_ON(bch2_inode_unpack(inode, &u));
710 
711 	return darray_push(&w->inodes, ((struct inode_walker_entry) {
712 		.inode		= u,
713 		.snapshot	= inode.k->p.snapshot,
714 	}));
715 }
716 
717 static int get_inodes_all_snapshots(struct btree_trans *trans,
718 				    struct inode_walker *w, u64 inum)
719 {
720 	struct bch_fs *c = trans->c;
721 	struct btree_iter iter;
722 	struct bkey_s_c k;
723 	int ret;
724 
725 	/*
726 	 * We no longer have inodes for w->last_pos; clear this to avoid
727 	 * screwing up check_i_sectors/check_subdir_count if we take a
728 	 * transaction restart here:
729 	 */
730 	w->have_inodes = false;
731 	w->recalculate_sums = false;
732 	w->inodes.nr = 0;
733 
734 	for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
735 				     BTREE_ITER_all_snapshots, k, ret) {
736 		if (k.k->p.offset != inum)
737 			break;
738 
739 		if (bkey_is_inode(k.k))
740 			add_inode(c, w, k);
741 	}
742 	bch2_trans_iter_exit(trans, &iter);
743 
744 	if (ret)
745 		return ret;
746 
747 	w->first_this_inode = true;
748 	w->have_inodes = true;
749 	return 0;
750 }
751 
752 static struct inode_walker_entry *
753 lookup_inode_for_snapshot(struct bch_fs *c, struct inode_walker *w, struct bkey_s_c k)
754 {
755 	bool is_whiteout = k.k->type == KEY_TYPE_whiteout;
756 
757 	struct inode_walker_entry *i;
758 	__darray_for_each(w->inodes, i)
759 		if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, i->snapshot))
760 			goto found;
761 
762 	return NULL;
763 found:
764 	BUG_ON(k.k->p.snapshot > i->snapshot);
765 
766 	if (k.k->p.snapshot != i->snapshot && !is_whiteout) {
767 		struct inode_walker_entry new = *i;
768 
769 		new.snapshot = k.k->p.snapshot;
770 		new.count = 0;
771 
772 		struct printbuf buf = PRINTBUF;
773 		bch2_bkey_val_to_text(&buf, c, k);
774 
775 		bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u\n"
776 			 "unexpected because we should always update the inode when we update a key in that inode\n"
777 			 "%s",
778 			 w->last_pos.inode, k.k->p.snapshot, i->snapshot, buf.buf);
779 		printbuf_exit(&buf);
780 
781 		while (i > w->inodes.data && i[-1].snapshot > k.k->p.snapshot)
782 			--i;
783 
784 		size_t pos = i - w->inodes.data;
785 		int ret = darray_insert_item(&w->inodes, pos, new);
786 		if (ret)
787 			return ERR_PTR(ret);
788 
789 		i = w->inodes.data + pos;
790 	}
791 
792 	return i;
793 }
794 
795 static struct inode_walker_entry *walk_inode(struct btree_trans *trans,
796 					     struct inode_walker *w,
797 					     struct bkey_s_c k)
798 {
799 	if (w->last_pos.inode != k.k->p.inode) {
800 		int ret = get_inodes_all_snapshots(trans, w, k.k->p.inode);
801 		if (ret)
802 			return ERR_PTR(ret);
803 	}
804 
805 	w->last_pos = k.k->p;
806 
807 	return lookup_inode_for_snapshot(trans->c, w, k);
808 }
809 
810 static int get_visible_inodes(struct btree_trans *trans,
811 			      struct inode_walker *w,
812 			      struct snapshots_seen *s,
813 			      u64 inum)
814 {
815 	struct bch_fs *c = trans->c;
816 	struct btree_iter iter;
817 	struct bkey_s_c k;
818 	int ret;
819 
820 	w->inodes.nr = 0;
821 
822 	for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
823 			   BTREE_ITER_all_snapshots, k, ret) {
824 		if (k.k->p.offset != inum)
825 			break;
826 
827 		if (!ref_visible(c, s, s->pos.snapshot, k.k->p.snapshot))
828 			continue;
829 
830 		if (bkey_is_inode(k.k))
831 			add_inode(c, w, k);
832 
833 		if (k.k->p.snapshot >= s->pos.snapshot)
834 			break;
835 	}
836 	bch2_trans_iter_exit(trans, &iter);
837 
838 	return ret;
839 }
840 
841 static int hash_redo_key(struct btree_trans *trans,
842 			 const struct bch_hash_desc desc,
843 			 struct bch_hash_info *hash_info,
844 			 struct btree_iter *k_iter, struct bkey_s_c k)
845 {
846 	struct bkey_i *delete;
847 	struct bkey_i *tmp;
848 
849 	delete = bch2_trans_kmalloc(trans, sizeof(*delete));
850 	if (IS_ERR(delete))
851 		return PTR_ERR(delete);
852 
853 	tmp = bch2_bkey_make_mut_noupdate(trans, k);
854 	if (IS_ERR(tmp))
855 		return PTR_ERR(tmp);
856 
857 	bkey_init(&delete->k);
858 	delete->k.p = k_iter->pos;
859 	return  bch2_btree_iter_traverse(k_iter) ?:
860 		bch2_trans_update(trans, k_iter, delete, 0) ?:
861 		bch2_hash_set_in_snapshot(trans, desc, hash_info,
862 				       (subvol_inum) { 0, k.k->p.inode },
863 				       k.k->p.snapshot, tmp,
864 				       STR_HASH_must_create|
865 				       BTREE_UPDATE_internal_snapshot_node) ?:
866 		bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
867 }
868 
869 static int hash_check_key(struct btree_trans *trans,
870 			  const struct bch_hash_desc desc,
871 			  struct bch_hash_info *hash_info,
872 			  struct btree_iter *k_iter, struct bkey_s_c hash_k)
873 {
874 	struct bch_fs *c = trans->c;
875 	struct btree_iter iter = { NULL };
876 	struct printbuf buf = PRINTBUF;
877 	struct bkey_s_c k;
878 	u64 hash;
879 	int ret = 0;
880 
881 	if (hash_k.k->type != desc.key_type)
882 		return 0;
883 
884 	hash = desc.hash_bkey(hash_info, hash_k);
885 
886 	if (likely(hash == hash_k.k->p.offset))
887 		return 0;
888 
889 	if (hash_k.k->p.offset < hash)
890 		goto bad_hash;
891 
892 	for_each_btree_key_norestart(trans, iter, desc.btree_id,
893 				     SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
894 				     BTREE_ITER_slots, k, ret) {
895 		if (bkey_eq(k.k->p, hash_k.k->p))
896 			break;
897 
898 		if (fsck_err_on(k.k->type == desc.key_type &&
899 				!desc.cmp_bkey(k, hash_k),
900 				trans, hash_table_key_duplicate,
901 				"duplicate hash table keys:\n%s",
902 				(printbuf_reset(&buf),
903 				 bch2_bkey_val_to_text(&buf, c, hash_k),
904 				 buf.buf))) {
905 			ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
906 			break;
907 		}
908 
909 		if (bkey_deleted(k.k)) {
910 			bch2_trans_iter_exit(trans, &iter);
911 			goto bad_hash;
912 		}
913 	}
914 out:
915 	bch2_trans_iter_exit(trans, &iter);
916 	printbuf_exit(&buf);
917 	return ret;
918 bad_hash:
919 	if (fsck_err(trans, hash_table_key_wrong_offset,
920 		     "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
921 		     bch2_btree_id_str(desc.btree_id), hash_k.k->p.inode, hash_k.k->p.offset, hash,
922 		     (printbuf_reset(&buf),
923 		      bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
924 		ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
925 		bch_err_fn(c, ret);
926 		if (ret)
927 			return ret;
928 		ret = -BCH_ERR_transaction_restart_nested;
929 	}
930 fsck_err:
931 	goto out;
932 }
933 
934 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
935 						struct btree_iter *iter,
936 						struct bpos pos)
937 {
938 	return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
939 }
940 
941 static struct bkey_s_c_dirent inode_get_dirent(struct btree_trans *trans,
942 					       struct btree_iter *iter,
943 					       struct bch_inode_unpacked *inode,
944 					       u32 *snapshot)
945 {
946 	if (inode->bi_subvol) {
947 		u64 inum;
948 		int ret = subvol_lookup(trans, inode->bi_parent_subvol, snapshot, &inum);
949 		if (ret)
950 			return ((struct bkey_s_c_dirent) { .k = ERR_PTR(ret) });
951 	}
952 
953 	return dirent_get_by_pos(trans, iter, SPOS(inode->bi_dir, inode->bi_dir_offset, *snapshot));
954 }
955 
956 static int check_inode_deleted_list(struct btree_trans *trans, struct bpos p)
957 {
958 	struct btree_iter iter;
959 	struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_deleted_inodes, p, 0);
960 	int ret = bkey_err(k) ?: k.k->type == KEY_TYPE_set;
961 	bch2_trans_iter_exit(trans, &iter);
962 	return ret;
963 }
964 
965 static int check_inode_dirent_inode(struct btree_trans *trans,
966 				    struct bch_inode_unpacked *inode,
967 				    bool *write_inode)
968 {
969 	struct bch_fs *c = trans->c;
970 	struct printbuf buf = PRINTBUF;
971 
972 	u32 inode_snapshot = inode->bi_snapshot;
973 	struct btree_iter dirent_iter = {};
974 	struct bkey_s_c_dirent d = inode_get_dirent(trans, &dirent_iter, inode, &inode_snapshot);
975 	int ret = bkey_err(d);
976 	if (ret && !bch2_err_matches(ret, ENOENT))
977 		return ret;
978 
979 	if (fsck_err_on(ret,
980 			trans, inode_points_to_missing_dirent,
981 			"inode points to missing dirent\n%s",
982 			(bch2_inode_unpacked_to_text(&buf, inode), buf.buf)) ||
983 	    fsck_err_on(!ret && dirent_points_to_inode_nowarn(d, inode),
984 			trans, inode_points_to_wrong_dirent,
985 			"%s",
986 			(printbuf_reset(&buf),
987 			 dirent_inode_mismatch_msg(&buf, c, d, inode),
988 			 buf.buf))) {
989 		/*
990 		 * We just clear the backpointer fields for now. If we find a
991 		 * dirent that points to this inode in check_dirents(), we'll
992 		 * update it then; then when we get to check_path() if the
993 		 * backpointer is still 0 we'll reattach it.
994 		 */
995 		inode->bi_dir = 0;
996 		inode->bi_dir_offset = 0;
997 		inode->bi_flags &= ~BCH_INODE_backptr_untrusted;
998 		*write_inode = true;
999 	}
1000 
1001 	ret = 0;
1002 fsck_err:
1003 	bch2_trans_iter_exit(trans, &dirent_iter);
1004 	printbuf_exit(&buf);
1005 	bch_err_fn(c, ret);
1006 	return ret;
1007 }
1008 
1009 static bool bch2_inode_is_open(struct bch_fs *c, struct bpos p)
1010 {
1011 	subvol_inum inum = {
1012 		.subvol = snapshot_t(c, p.snapshot)->subvol,
1013 		.inum	= p.offset,
1014 	};
1015 
1016 	/* snapshot tree corruption, can't safely delete */
1017 	if (!inum.subvol) {
1018 		bch_warn_ratelimited(c, "%s(): snapshot %u has no subvol, unlinked but can't safely delete", __func__, p.snapshot);
1019 		return true;
1020 	}
1021 
1022 	return __bch2_inode_hash_find(c, inum) != NULL;
1023 }
1024 
1025 static int check_inode(struct btree_trans *trans,
1026 		       struct btree_iter *iter,
1027 		       struct bkey_s_c k,
1028 		       struct bch_inode_unpacked *prev,
1029 		       struct snapshots_seen *s,
1030 		       bool full)
1031 {
1032 	struct bch_fs *c = trans->c;
1033 	struct printbuf buf = PRINTBUF;
1034 	struct bch_inode_unpacked u;
1035 	bool do_update = false;
1036 	int ret;
1037 
1038 	ret = bch2_check_key_has_snapshot(trans, iter, k);
1039 	if (ret < 0)
1040 		goto err;
1041 	if (ret)
1042 		return 0;
1043 
1044 	ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1045 	if (ret)
1046 		goto err;
1047 
1048 	if (!bkey_is_inode(k.k))
1049 		return 0;
1050 
1051 	BUG_ON(bch2_inode_unpack(k, &u));
1052 
1053 	if (!full &&
1054 	    !(u.bi_flags & (BCH_INODE_i_size_dirty|
1055 			    BCH_INODE_i_sectors_dirty|
1056 			    BCH_INODE_unlinked)))
1057 		return 0;
1058 
1059 	if (prev->bi_inum != u.bi_inum)
1060 		*prev = u;
1061 
1062 	if (fsck_err_on(prev->bi_hash_seed	!= u.bi_hash_seed ||
1063 			inode_d_type(prev)	!= inode_d_type(&u),
1064 			trans, inode_snapshot_mismatch,
1065 			"inodes in different snapshots don't match")) {
1066 		bch_err(c, "repair not implemented yet");
1067 		ret = -BCH_ERR_fsck_repair_unimplemented;
1068 		goto err_noprint;
1069 	}
1070 
1071 	if (u.bi_dir || u.bi_dir_offset) {
1072 		ret = check_inode_dirent_inode(trans, &u, &do_update);
1073 		if (ret)
1074 			goto err;
1075 	}
1076 
1077 	if (fsck_err_on(u.bi_dir && (u.bi_flags & BCH_INODE_unlinked),
1078 			trans, inode_unlinked_but_has_dirent,
1079 			"inode unlinked but has dirent\n%s",
1080 			(printbuf_reset(&buf),
1081 			 bch2_inode_unpacked_to_text(&buf, &u),
1082 			 buf.buf))) {
1083 		u.bi_flags &= ~BCH_INODE_unlinked;
1084 		do_update = true;
1085 	}
1086 
1087 	if (S_ISDIR(u.bi_mode) && (u.bi_flags & BCH_INODE_unlinked)) {
1088 		/* Check for this early so that check_unreachable_inode() will reattach it */
1089 
1090 		ret = bch2_empty_dir_snapshot(trans, k.k->p.offset, 0, k.k->p.snapshot);
1091 		if (ret && ret != -BCH_ERR_ENOTEMPTY_dir_not_empty)
1092 			goto err;
1093 
1094 		fsck_err_on(ret, trans, inode_dir_unlinked_but_not_empty,
1095 			    "dir unlinked but not empty\n%s",
1096 			    (printbuf_reset(&buf),
1097 			     bch2_inode_unpacked_to_text(&buf, &u),
1098 			     buf.buf));
1099 		u.bi_flags &= ~BCH_INODE_unlinked;
1100 		do_update = true;
1101 		ret = 0;
1102 	}
1103 
1104 	if ((u.bi_flags & (BCH_INODE_i_size_dirty|BCH_INODE_unlinked)) &&
1105 	    bch2_key_has_snapshot_overwrites(trans, BTREE_ID_inodes, k.k->p)) {
1106 		struct bpos new_min_pos;
1107 
1108 		ret = bch2_propagate_key_to_snapshot_leaves(trans, iter->btree_id, k, &new_min_pos);
1109 		if (ret)
1110 			goto err;
1111 
1112 		u.bi_flags &= ~BCH_INODE_i_size_dirty|BCH_INODE_unlinked;
1113 
1114 		ret = __bch2_fsck_write_inode(trans, &u);
1115 
1116 		bch_err_msg(c, ret, "in fsck updating inode");
1117 		if (ret)
1118 			goto err_noprint;
1119 
1120 		if (!bpos_eq(new_min_pos, POS_MIN))
1121 			bch2_btree_iter_set_pos(iter, bpos_predecessor(new_min_pos));
1122 		goto err_noprint;
1123 	}
1124 
1125 	if (u.bi_flags & BCH_INODE_unlinked) {
1126 		if (!test_bit(BCH_FS_started, &c->flags)) {
1127 			/*
1128 			 * If we're not in online fsck, don't delete unlinked
1129 			 * inodes, just make sure they're on the deleted list.
1130 			 *
1131 			 * They might be referred to by a logged operation -
1132 			 * i.e. we might have crashed in the middle of a
1133 			 * truncate on an unlinked but open file - so we want to
1134 			 * let the delete_dead_inodes kill it after resuming
1135 			 * logged ops.
1136 			 */
1137 			ret = check_inode_deleted_list(trans, k.k->p);
1138 			if (ret < 0)
1139 				goto err_noprint;
1140 
1141 			fsck_err_on(!ret,
1142 				    trans, unlinked_inode_not_on_deleted_list,
1143 				    "inode %llu:%u unlinked, but not on deleted list",
1144 				    u.bi_inum, k.k->p.snapshot);
1145 
1146 			ret = bch2_btree_bit_mod_buffered(trans, BTREE_ID_deleted_inodes, k.k->p, 1);
1147 			if (ret)
1148 				goto err;
1149 		} else {
1150 			if (fsck_err_on(!bch2_inode_is_open(c, k.k->p),
1151 					trans, inode_unlinked_and_not_open,
1152 				      "inode %llu%u unlinked and not open",
1153 				      u.bi_inum, u.bi_snapshot)) {
1154 				ret = bch2_inode_rm_snapshot(trans, u.bi_inum, iter->pos.snapshot);
1155 				bch_err_msg(c, ret, "in fsck deleting inode");
1156 				goto err_noprint;
1157 			}
1158 		}
1159 	}
1160 
1161 	/* i_size_dirty is vestigal, since we now have logged ops for truncate * */
1162 	if (u.bi_flags & BCH_INODE_i_size_dirty &&
1163 	    (!test_bit(BCH_FS_clean_recovery, &c->flags) ||
1164 	     fsck_err(trans, inode_i_size_dirty_but_clean,
1165 		      "filesystem marked clean, but inode %llu has i_size dirty",
1166 		      u.bi_inum))) {
1167 		bch_verbose(c, "truncating inode %llu", u.bi_inum);
1168 
1169 		/*
1170 		 * XXX: need to truncate partial blocks too here - or ideally
1171 		 * just switch units to bytes and that issue goes away
1172 		 */
1173 		ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1174 				SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
1175 				     iter->pos.snapshot),
1176 				POS(u.bi_inum, U64_MAX),
1177 				0, NULL);
1178 		bch_err_msg(c, ret, "in fsck truncating inode");
1179 		if (ret)
1180 			return ret;
1181 
1182 		/*
1183 		 * We truncated without our normal sector accounting hook, just
1184 		 * make sure we recalculate it:
1185 		 */
1186 		u.bi_flags |= BCH_INODE_i_sectors_dirty;
1187 
1188 		u.bi_flags &= ~BCH_INODE_i_size_dirty;
1189 		do_update = true;
1190 	}
1191 
1192 	/* i_sectors_dirty is vestigal, i_sectors is always updated transactionally */
1193 	if (u.bi_flags & BCH_INODE_i_sectors_dirty &&
1194 	    (!test_bit(BCH_FS_clean_recovery, &c->flags) ||
1195 	     fsck_err(trans, inode_i_sectors_dirty_but_clean,
1196 		      "filesystem marked clean, but inode %llu has i_sectors dirty",
1197 		      u.bi_inum))) {
1198 		s64 sectors;
1199 
1200 		bch_verbose(c, "recounting sectors for inode %llu",
1201 			    u.bi_inum);
1202 
1203 		sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
1204 		if (sectors < 0) {
1205 			bch_err_msg(c, sectors, "in fsck recounting inode sectors");
1206 			return sectors;
1207 		}
1208 
1209 		u.bi_sectors = sectors;
1210 		u.bi_flags &= ~BCH_INODE_i_sectors_dirty;
1211 		do_update = true;
1212 	}
1213 
1214 	if (u.bi_flags & BCH_INODE_backptr_untrusted) {
1215 		u.bi_dir = 0;
1216 		u.bi_dir_offset = 0;
1217 		u.bi_flags &= ~BCH_INODE_backptr_untrusted;
1218 		do_update = true;
1219 	}
1220 
1221 	if (fsck_err_on(u.bi_parent_subvol &&
1222 			(u.bi_subvol == 0 ||
1223 			 u.bi_subvol == BCACHEFS_ROOT_SUBVOL),
1224 			trans, inode_bi_parent_nonzero,
1225 			"inode %llu:%u has subvol %u but nonzero parent subvol %u",
1226 			u.bi_inum, k.k->p.snapshot, u.bi_subvol, u.bi_parent_subvol)) {
1227 		u.bi_parent_subvol = 0;
1228 		do_update = true;
1229 	}
1230 
1231 	if (u.bi_subvol) {
1232 		struct bch_subvolume s;
1233 
1234 		ret = bch2_subvolume_get(trans, u.bi_subvol, false, 0, &s);
1235 		if (ret && !bch2_err_matches(ret, ENOENT))
1236 			goto err;
1237 
1238 		if (ret && (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_subvolumes))) {
1239 			ret = reconstruct_subvol(trans, k.k->p.snapshot, u.bi_subvol, u.bi_inum);
1240 			goto do_update;
1241 		}
1242 
1243 		if (fsck_err_on(ret,
1244 				trans, inode_bi_subvol_missing,
1245 				"inode %llu:%u bi_subvol points to missing subvolume %u",
1246 				u.bi_inum, k.k->p.snapshot, u.bi_subvol) ||
1247 		    fsck_err_on(le64_to_cpu(s.inode) != u.bi_inum ||
1248 				!bch2_snapshot_is_ancestor(c, le32_to_cpu(s.snapshot),
1249 							   k.k->p.snapshot),
1250 				trans, inode_bi_subvol_wrong,
1251 				"inode %llu:%u points to subvol %u, but subvol points to %llu:%u",
1252 				u.bi_inum, k.k->p.snapshot, u.bi_subvol,
1253 				le64_to_cpu(s.inode),
1254 				le32_to_cpu(s.snapshot))) {
1255 			u.bi_subvol = 0;
1256 			u.bi_parent_subvol = 0;
1257 			do_update = true;
1258 		}
1259 	}
1260 do_update:
1261 	if (do_update) {
1262 		ret = __bch2_fsck_write_inode(trans, &u);
1263 		bch_err_msg(c, ret, "in fsck updating inode");
1264 		if (ret)
1265 			goto err_noprint;
1266 	}
1267 err:
1268 fsck_err:
1269 	bch_err_fn(c, ret);
1270 err_noprint:
1271 	printbuf_exit(&buf);
1272 	return ret;
1273 }
1274 
1275 int bch2_check_inodes(struct bch_fs *c)
1276 {
1277 	bool full = c->opts.fsck;
1278 	struct bch_inode_unpacked prev = { 0 };
1279 	struct snapshots_seen s;
1280 
1281 	snapshots_seen_init(&s);
1282 
1283 	int ret = bch2_trans_run(c,
1284 		for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
1285 				POS_MIN,
1286 				BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
1287 				NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1288 			check_inode(trans, &iter, k, &prev, &s, full)));
1289 
1290 	snapshots_seen_exit(&s);
1291 	bch_err_fn(c, ret);
1292 	return ret;
1293 }
1294 
1295 static inline bool btree_matches_i_mode(enum btree_id btree, unsigned mode)
1296 {
1297 	switch (btree) {
1298 	case BTREE_ID_extents:
1299 		return S_ISREG(mode) || S_ISLNK(mode);
1300 	case BTREE_ID_dirents:
1301 		return S_ISDIR(mode);
1302 	case BTREE_ID_xattrs:
1303 		return true;
1304 	default:
1305 		BUG();
1306 	}
1307 }
1308 
1309 static int check_key_has_inode(struct btree_trans *trans,
1310 			       struct btree_iter *iter,
1311 			       struct inode_walker *inode,
1312 			       struct inode_walker_entry *i,
1313 			       struct bkey_s_c k)
1314 {
1315 	struct bch_fs *c = trans->c;
1316 	struct printbuf buf = PRINTBUF;
1317 	int ret = PTR_ERR_OR_ZERO(i);
1318 	if (ret)
1319 		return ret;
1320 
1321 	if (k.k->type == KEY_TYPE_whiteout)
1322 		goto out;
1323 
1324 	if (!i && (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_inodes))) {
1325 		ret =   reconstruct_inode(trans, iter->btree_id, k.k->p.snapshot, k.k->p.inode) ?:
1326 			bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
1327 		if (ret)
1328 			goto err;
1329 
1330 		inode->last_pos.inode--;
1331 		ret = -BCH_ERR_transaction_restart_nested;
1332 		goto err;
1333 	}
1334 
1335 	if (fsck_err_on(!i,
1336 			trans, key_in_missing_inode,
1337 			"key in missing inode:\n  %s",
1338 			(printbuf_reset(&buf),
1339 			 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1340 		goto delete;
1341 
1342 	if (fsck_err_on(i && !btree_matches_i_mode(iter->btree_id, i->inode.bi_mode),
1343 			trans, key_in_wrong_inode_type,
1344 			"key for wrong inode mode %o:\n  %s",
1345 			i->inode.bi_mode,
1346 			(printbuf_reset(&buf),
1347 			 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1348 		goto delete;
1349 out:
1350 err:
1351 fsck_err:
1352 	printbuf_exit(&buf);
1353 	bch_err_fn(c, ret);
1354 	return ret;
1355 delete:
1356 	ret = bch2_btree_delete_at(trans, iter, BTREE_UPDATE_internal_snapshot_node);
1357 	goto out;
1358 }
1359 
1360 static int check_i_sectors_notnested(struct btree_trans *trans, struct inode_walker *w)
1361 {
1362 	struct bch_fs *c = trans->c;
1363 	int ret = 0;
1364 	s64 count2;
1365 
1366 	darray_for_each(w->inodes, i) {
1367 		if (i->inode.bi_sectors == i->count)
1368 			continue;
1369 
1370 		count2 = bch2_count_inode_sectors(trans, w->last_pos.inode, i->snapshot);
1371 
1372 		if (w->recalculate_sums)
1373 			i->count = count2;
1374 
1375 		if (i->count != count2) {
1376 			bch_err_ratelimited(c, "fsck counted i_sectors wrong for inode %llu:%u: got %llu should be %llu",
1377 					    w->last_pos.inode, i->snapshot, i->count, count2);
1378 			return -BCH_ERR_internal_fsck_err;
1379 		}
1380 
1381 		if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_i_sectors_dirty),
1382 				trans, inode_i_sectors_wrong,
1383 				"inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1384 				w->last_pos.inode, i->snapshot,
1385 				i->inode.bi_sectors, i->count)) {
1386 			i->inode.bi_sectors = i->count;
1387 			ret = bch2_fsck_write_inode(trans, &i->inode);
1388 			if (ret)
1389 				break;
1390 		}
1391 	}
1392 fsck_err:
1393 	bch_err_fn(c, ret);
1394 	return ret;
1395 }
1396 
1397 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1398 {
1399 	u32 restart_count = trans->restart_count;
1400 	return check_i_sectors_notnested(trans, w) ?:
1401 		trans_was_restarted(trans, restart_count);
1402 }
1403 
1404 struct extent_end {
1405 	u32			snapshot;
1406 	u64			offset;
1407 	struct snapshots_seen	seen;
1408 };
1409 
1410 struct extent_ends {
1411 	struct bpos			last_pos;
1412 	DARRAY(struct extent_end)	e;
1413 };
1414 
1415 static void extent_ends_reset(struct extent_ends *extent_ends)
1416 {
1417 	darray_for_each(extent_ends->e, i)
1418 		snapshots_seen_exit(&i->seen);
1419 	extent_ends->e.nr = 0;
1420 }
1421 
1422 static void extent_ends_exit(struct extent_ends *extent_ends)
1423 {
1424 	extent_ends_reset(extent_ends);
1425 	darray_exit(&extent_ends->e);
1426 }
1427 
1428 static void extent_ends_init(struct extent_ends *extent_ends)
1429 {
1430 	memset(extent_ends, 0, sizeof(*extent_ends));
1431 }
1432 
1433 static int extent_ends_at(struct bch_fs *c,
1434 			  struct extent_ends *extent_ends,
1435 			  struct snapshots_seen *seen,
1436 			  struct bkey_s_c k)
1437 {
1438 	struct extent_end *i, n = (struct extent_end) {
1439 		.offset		= k.k->p.offset,
1440 		.snapshot	= k.k->p.snapshot,
1441 		.seen		= *seen,
1442 	};
1443 
1444 	n.seen.ids.data = kmemdup(seen->ids.data,
1445 			      sizeof(seen->ids.data[0]) * seen->ids.size,
1446 			      GFP_KERNEL);
1447 	if (!n.seen.ids.data)
1448 		return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1449 
1450 	__darray_for_each(extent_ends->e, i) {
1451 		if (i->snapshot == k.k->p.snapshot) {
1452 			snapshots_seen_exit(&i->seen);
1453 			*i = n;
1454 			return 0;
1455 		}
1456 
1457 		if (i->snapshot >= k.k->p.snapshot)
1458 			break;
1459 	}
1460 
1461 	return darray_insert_item(&extent_ends->e, i - extent_ends->e.data, n);
1462 }
1463 
1464 static int overlapping_extents_found(struct btree_trans *trans,
1465 				     enum btree_id btree,
1466 				     struct bpos pos1, struct snapshots_seen *pos1_seen,
1467 				     struct bkey pos2,
1468 				     bool *fixed,
1469 				     struct extent_end *extent_end)
1470 {
1471 	struct bch_fs *c = trans->c;
1472 	struct printbuf buf = PRINTBUF;
1473 	struct btree_iter iter1, iter2 = { NULL };
1474 	struct bkey_s_c k1, k2;
1475 	int ret;
1476 
1477 	BUG_ON(bkey_le(pos1, bkey_start_pos(&pos2)));
1478 
1479 	bch2_trans_iter_init(trans, &iter1, btree, pos1,
1480 			     BTREE_ITER_all_snapshots|
1481 			     BTREE_ITER_not_extents);
1482 	k1 = bch2_btree_iter_peek_upto(&iter1, POS(pos1.inode, U64_MAX));
1483 	ret = bkey_err(k1);
1484 	if (ret)
1485 		goto err;
1486 
1487 	prt_str(&buf, "\n  ");
1488 	bch2_bkey_val_to_text(&buf, c, k1);
1489 
1490 	if (!bpos_eq(pos1, k1.k->p)) {
1491 		prt_str(&buf, "\n  wanted\n  ");
1492 		bch2_bpos_to_text(&buf, pos1);
1493 		prt_str(&buf, "\n  ");
1494 		bch2_bkey_to_text(&buf, &pos2);
1495 
1496 		bch_err(c, "%s: error finding first overlapping extent when repairing, got%s",
1497 			__func__, buf.buf);
1498 		ret = -BCH_ERR_internal_fsck_err;
1499 		goto err;
1500 	}
1501 
1502 	bch2_trans_copy_iter(&iter2, &iter1);
1503 
1504 	while (1) {
1505 		bch2_btree_iter_advance(&iter2);
1506 
1507 		k2 = bch2_btree_iter_peek_upto(&iter2, POS(pos1.inode, U64_MAX));
1508 		ret = bkey_err(k2);
1509 		if (ret)
1510 			goto err;
1511 
1512 		if (bpos_ge(k2.k->p, pos2.p))
1513 			break;
1514 	}
1515 
1516 	prt_str(&buf, "\n  ");
1517 	bch2_bkey_val_to_text(&buf, c, k2);
1518 
1519 	if (bpos_gt(k2.k->p, pos2.p) ||
1520 	    pos2.size != k2.k->size) {
1521 		bch_err(c, "%s: error finding seconding overlapping extent when repairing%s",
1522 			__func__, buf.buf);
1523 		ret = -BCH_ERR_internal_fsck_err;
1524 		goto err;
1525 	}
1526 
1527 	prt_printf(&buf, "\n  overwriting %s extent",
1528 		   pos1.snapshot >= pos2.p.snapshot ? "first" : "second");
1529 
1530 	if (fsck_err(trans, extent_overlapping,
1531 		     "overlapping extents%s", buf.buf)) {
1532 		struct btree_iter *old_iter = &iter1;
1533 		struct disk_reservation res = { 0 };
1534 
1535 		if (pos1.snapshot < pos2.p.snapshot) {
1536 			old_iter = &iter2;
1537 			swap(k1, k2);
1538 		}
1539 
1540 		trans->extra_disk_res += bch2_bkey_sectors_compressed(k2);
1541 
1542 		ret =   bch2_trans_update_extent_overwrite(trans, old_iter,
1543 				BTREE_UPDATE_internal_snapshot_node,
1544 				k1, k2) ?:
1545 			bch2_trans_commit(trans, &res, NULL, BCH_TRANS_COMMIT_no_enospc);
1546 		bch2_disk_reservation_put(c, &res);
1547 
1548 		if (ret)
1549 			goto err;
1550 
1551 		*fixed = true;
1552 
1553 		if (pos1.snapshot == pos2.p.snapshot) {
1554 			/*
1555 			 * We overwrote the first extent, and did the overwrite
1556 			 * in the same snapshot:
1557 			 */
1558 			extent_end->offset = bkey_start_offset(&pos2);
1559 		} else if (pos1.snapshot > pos2.p.snapshot) {
1560 			/*
1561 			 * We overwrote the first extent in pos2's snapshot:
1562 			 */
1563 			ret = snapshots_seen_add_inorder(c, pos1_seen, pos2.p.snapshot);
1564 		} else {
1565 			/*
1566 			 * We overwrote the second extent - restart
1567 			 * check_extent() from the top:
1568 			 */
1569 			ret = -BCH_ERR_transaction_restart_nested;
1570 		}
1571 	}
1572 fsck_err:
1573 err:
1574 	bch2_trans_iter_exit(trans, &iter2);
1575 	bch2_trans_iter_exit(trans, &iter1);
1576 	printbuf_exit(&buf);
1577 	return ret;
1578 }
1579 
1580 static int check_overlapping_extents(struct btree_trans *trans,
1581 			      struct snapshots_seen *seen,
1582 			      struct extent_ends *extent_ends,
1583 			      struct bkey_s_c k,
1584 			      struct btree_iter *iter,
1585 			      bool *fixed)
1586 {
1587 	struct bch_fs *c = trans->c;
1588 	int ret = 0;
1589 
1590 	/* transaction restart, running again */
1591 	if (bpos_eq(extent_ends->last_pos, k.k->p))
1592 		return 0;
1593 
1594 	if (extent_ends->last_pos.inode != k.k->p.inode)
1595 		extent_ends_reset(extent_ends);
1596 
1597 	darray_for_each(extent_ends->e, i) {
1598 		if (i->offset <= bkey_start_offset(k.k))
1599 			continue;
1600 
1601 		if (!ref_visible2(c,
1602 				  k.k->p.snapshot, seen,
1603 				  i->snapshot, &i->seen))
1604 			continue;
1605 
1606 		ret = overlapping_extents_found(trans, iter->btree_id,
1607 						SPOS(iter->pos.inode,
1608 						     i->offset,
1609 						     i->snapshot),
1610 						&i->seen,
1611 						*k.k, fixed, i);
1612 		if (ret)
1613 			goto err;
1614 	}
1615 
1616 	extent_ends->last_pos = k.k->p;
1617 err:
1618 	return ret;
1619 }
1620 
1621 static int check_extent_overbig(struct btree_trans *trans, struct btree_iter *iter,
1622 				struct bkey_s_c k)
1623 {
1624 	struct bch_fs *c = trans->c;
1625 	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1626 	struct bch_extent_crc_unpacked crc;
1627 	const union bch_extent_entry *i;
1628 	unsigned encoded_extent_max_sectors = c->opts.encoded_extent_max >> 9;
1629 
1630 	bkey_for_each_crc(k.k, ptrs, crc, i)
1631 		if (crc_is_encoded(crc) &&
1632 		    crc.uncompressed_size > encoded_extent_max_sectors) {
1633 			struct printbuf buf = PRINTBUF;
1634 
1635 			bch2_bkey_val_to_text(&buf, c, k);
1636 			bch_err(c, "overbig encoded extent, please report this:\n  %s", buf.buf);
1637 			printbuf_exit(&buf);
1638 		}
1639 
1640 	return 0;
1641 }
1642 
1643 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1644 			struct bkey_s_c k,
1645 			struct inode_walker *inode,
1646 			struct snapshots_seen *s,
1647 			struct extent_ends *extent_ends,
1648 			struct disk_reservation *res)
1649 {
1650 	struct bch_fs *c = trans->c;
1651 	struct printbuf buf = PRINTBUF;
1652 	int ret = 0;
1653 
1654 	ret = bch2_check_key_has_snapshot(trans, iter, k);
1655 	if (ret) {
1656 		ret = ret < 0 ? ret : 0;
1657 		goto out;
1658 	}
1659 
1660 	if (inode->last_pos.inode != k.k->p.inode && inode->have_inodes) {
1661 		ret = check_i_sectors(trans, inode);
1662 		if (ret)
1663 			goto err;
1664 	}
1665 
1666 	ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1667 	if (ret)
1668 		goto err;
1669 
1670 	struct inode_walker_entry *extent_i = walk_inode(trans, inode, k);
1671 	ret = PTR_ERR_OR_ZERO(extent_i);
1672 	if (ret)
1673 		goto err;
1674 
1675 	ret = check_key_has_inode(trans, iter, inode, extent_i, k);
1676 	if (ret)
1677 		goto err;
1678 
1679 	if (k.k->type != KEY_TYPE_whiteout) {
1680 		ret = check_overlapping_extents(trans, s, extent_ends, k, iter,
1681 						&inode->recalculate_sums);
1682 		if (ret)
1683 			goto err;
1684 
1685 		/*
1686 		 * Check inodes in reverse order, from oldest snapshots to
1687 		 * newest, starting from the inode that matches this extent's
1688 		 * snapshot. If we didn't have one, iterate over all inodes:
1689 		 */
1690 		for (struct inode_walker_entry *i = extent_i ?: &darray_last(inode->inodes);
1691 		     inode->inodes.data && i >= inode->inodes.data;
1692 		     --i) {
1693 			if (i->snapshot > k.k->p.snapshot ||
1694 			    !key_visible_in_snapshot(c, s, i->snapshot, k.k->p.snapshot))
1695 				continue;
1696 
1697 			if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_i_size_dirty) &&
1698 					k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1699 					!bkey_extent_is_reservation(k),
1700 					trans, extent_past_end_of_inode,
1701 					"extent type past end of inode %llu:%u, i_size %llu\n  %s",
1702 					i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1703 					(bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1704 				struct btree_iter iter2;
1705 
1706 				bch2_trans_copy_iter(&iter2, iter);
1707 				bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1708 				ret =   bch2_btree_iter_traverse(&iter2) ?:
1709 					bch2_btree_delete_at(trans, &iter2,
1710 						BTREE_UPDATE_internal_snapshot_node);
1711 				bch2_trans_iter_exit(trans, &iter2);
1712 				if (ret)
1713 					goto err;
1714 
1715 				iter->k.type = KEY_TYPE_whiteout;
1716 				break;
1717 			}
1718 		}
1719 	}
1720 
1721 	ret = bch2_trans_commit(trans, res, NULL, BCH_TRANS_COMMIT_no_enospc);
1722 	if (ret)
1723 		goto err;
1724 
1725 	if (bkey_extent_is_allocation(k.k)) {
1726 		for (struct inode_walker_entry *i = extent_i ?: &darray_last(inode->inodes);
1727 		     inode->inodes.data && i >= inode->inodes.data;
1728 		     --i) {
1729 			if (i->snapshot > k.k->p.snapshot ||
1730 			    !key_visible_in_snapshot(c, s, i->snapshot, k.k->p.snapshot))
1731 				continue;
1732 
1733 			i->count += k.k->size;
1734 		}
1735 	}
1736 
1737 	if (k.k->type != KEY_TYPE_whiteout) {
1738 		ret = extent_ends_at(c, extent_ends, s, k);
1739 		if (ret)
1740 			goto err;
1741 	}
1742 out:
1743 err:
1744 fsck_err:
1745 	printbuf_exit(&buf);
1746 	bch_err_fn(c, ret);
1747 	return ret;
1748 }
1749 
1750 /*
1751  * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1752  * that i_size an i_sectors are consistent
1753  */
1754 int bch2_check_extents(struct bch_fs *c)
1755 {
1756 	struct inode_walker w = inode_walker_init();
1757 	struct snapshots_seen s;
1758 	struct extent_ends extent_ends;
1759 	struct disk_reservation res = { 0 };
1760 
1761 	snapshots_seen_init(&s);
1762 	extent_ends_init(&extent_ends);
1763 
1764 	int ret = bch2_trans_run(c,
1765 		for_each_btree_key(trans, iter, BTREE_ID_extents,
1766 				POS(BCACHEFS_ROOT_INO, 0),
1767 				BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k, ({
1768 			bch2_disk_reservation_put(c, &res);
1769 			check_extent(trans, &iter, k, &w, &s, &extent_ends, &res) ?:
1770 			check_extent_overbig(trans, &iter, k);
1771 		})) ?:
1772 		check_i_sectors_notnested(trans, &w));
1773 
1774 	bch2_disk_reservation_put(c, &res);
1775 	extent_ends_exit(&extent_ends);
1776 	inode_walker_exit(&w);
1777 	snapshots_seen_exit(&s);
1778 
1779 	bch_err_fn(c, ret);
1780 	return ret;
1781 }
1782 
1783 int bch2_check_indirect_extents(struct bch_fs *c)
1784 {
1785 	struct disk_reservation res = { 0 };
1786 
1787 	int ret = bch2_trans_run(c,
1788 		for_each_btree_key_commit(trans, iter, BTREE_ID_reflink,
1789 				POS_MIN,
1790 				BTREE_ITER_prefetch, k,
1791 				&res, NULL,
1792 				BCH_TRANS_COMMIT_no_enospc, ({
1793 			bch2_disk_reservation_put(c, &res);
1794 			check_extent_overbig(trans, &iter, k);
1795 		})));
1796 
1797 	bch2_disk_reservation_put(c, &res);
1798 	bch_err_fn(c, ret);
1799 	return ret;
1800 }
1801 
1802 static int check_subdir_count_notnested(struct btree_trans *trans, struct inode_walker *w)
1803 {
1804 	struct bch_fs *c = trans->c;
1805 	int ret = 0;
1806 	s64 count2;
1807 
1808 	darray_for_each(w->inodes, i) {
1809 		if (i->inode.bi_nlink == i->count)
1810 			continue;
1811 
1812 		count2 = bch2_count_subdirs(trans, w->last_pos.inode, i->snapshot);
1813 		if (count2 < 0)
1814 			return count2;
1815 
1816 		if (i->count != count2) {
1817 			bch_err_ratelimited(c, "fsck counted subdirectories wrong for inum %llu:%u: got %llu should be %llu",
1818 					    w->last_pos.inode, i->snapshot, i->count, count2);
1819 			i->count = count2;
1820 			if (i->inode.bi_nlink == i->count)
1821 				continue;
1822 		}
1823 
1824 		if (fsck_err_on(i->inode.bi_nlink != i->count,
1825 				trans, inode_dir_wrong_nlink,
1826 				"directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1827 				w->last_pos.inode, i->snapshot, i->inode.bi_nlink, i->count)) {
1828 			i->inode.bi_nlink = i->count;
1829 			ret = bch2_fsck_write_inode(trans, &i->inode);
1830 			if (ret)
1831 				break;
1832 		}
1833 	}
1834 fsck_err:
1835 	bch_err_fn(c, ret);
1836 	return ret;
1837 }
1838 
1839 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1840 {
1841 	u32 restart_count = trans->restart_count;
1842 	return check_subdir_count_notnested(trans, w) ?:
1843 		trans_was_restarted(trans, restart_count);
1844 }
1845 
1846 noinline_for_stack
1847 static int check_dirent_inode_dirent(struct btree_trans *trans,
1848 				   struct btree_iter *iter,
1849 				   struct bkey_s_c_dirent d,
1850 				   struct bch_inode_unpacked *target)
1851 {
1852 	struct bch_fs *c = trans->c;
1853 	struct printbuf buf = PRINTBUF;
1854 	struct btree_iter bp_iter = { NULL };
1855 	int ret = 0;
1856 
1857 	if (inode_points_to_dirent(target, d))
1858 		return 0;
1859 
1860 	if (!target->bi_dir &&
1861 	    !target->bi_dir_offset) {
1862 		fsck_err_on(S_ISDIR(target->bi_mode),
1863 			    trans, inode_dir_missing_backpointer,
1864 			    "directory with missing backpointer\n%s",
1865 			    (printbuf_reset(&buf),
1866 			     bch2_bkey_val_to_text(&buf, c, d.s_c),
1867 			     prt_printf(&buf, "\n"),
1868 			     bch2_inode_unpacked_to_text(&buf, target),
1869 			     buf.buf));
1870 
1871 		fsck_err_on(target->bi_flags & BCH_INODE_unlinked,
1872 			    trans, inode_unlinked_but_has_dirent,
1873 			    "inode unlinked but has dirent\n%s",
1874 			    (printbuf_reset(&buf),
1875 			     bch2_bkey_val_to_text(&buf, c, d.s_c),
1876 			     prt_printf(&buf, "\n"),
1877 			     bch2_inode_unpacked_to_text(&buf, target),
1878 			     buf.buf));
1879 
1880 		target->bi_flags &= ~BCH_INODE_unlinked;
1881 		target->bi_dir		= d.k->p.inode;
1882 		target->bi_dir_offset	= d.k->p.offset;
1883 		return __bch2_fsck_write_inode(trans, target);
1884 	}
1885 
1886 	if (bch2_inode_should_have_bp(target) &&
1887 	    !fsck_err(trans, inode_wrong_backpointer,
1888 		      "dirent points to inode that does not point back:\n  %s",
1889 		      (bch2_bkey_val_to_text(&buf, c, d.s_c),
1890 		       prt_printf(&buf, "\n  "),
1891 		       bch2_inode_unpacked_to_text(&buf, target),
1892 		       buf.buf)))
1893 		goto err;
1894 
1895 	struct bkey_s_c_dirent bp_dirent = dirent_get_by_pos(trans, &bp_iter,
1896 			      SPOS(target->bi_dir, target->bi_dir_offset, target->bi_snapshot));
1897 	ret = bkey_err(bp_dirent);
1898 	if (ret && !bch2_err_matches(ret, ENOENT))
1899 		goto err;
1900 
1901 	bool backpointer_exists = !ret;
1902 	ret = 0;
1903 
1904 	if (fsck_err_on(!backpointer_exists,
1905 			trans, inode_wrong_backpointer,
1906 			"inode %llu:%u has wrong backpointer:\n"
1907 			"got       %llu:%llu\n"
1908 			"should be %llu:%llu",
1909 			target->bi_inum, target->bi_snapshot,
1910 			target->bi_dir,
1911 			target->bi_dir_offset,
1912 			d.k->p.inode,
1913 			d.k->p.offset)) {
1914 		target->bi_dir		= d.k->p.inode;
1915 		target->bi_dir_offset	= d.k->p.offset;
1916 		ret = __bch2_fsck_write_inode(trans, target);
1917 		goto out;
1918 	}
1919 
1920 	bch2_bkey_val_to_text(&buf, c, d.s_c);
1921 	prt_newline(&buf);
1922 	if (backpointer_exists)
1923 		bch2_bkey_val_to_text(&buf, c, bp_dirent.s_c);
1924 
1925 	if (fsck_err_on(backpointer_exists &&
1926 			(S_ISDIR(target->bi_mode) ||
1927 			 target->bi_subvol),
1928 			trans, inode_dir_multiple_links,
1929 			"%s %llu:%u with multiple links\n%s",
1930 			S_ISDIR(target->bi_mode) ? "directory" : "subvolume",
1931 			target->bi_inum, target->bi_snapshot, buf.buf)) {
1932 		ret = __remove_dirent(trans, d.k->p);
1933 		goto out;
1934 	}
1935 
1936 	/*
1937 	 * hardlinked file with nlink 0:
1938 	 * We're just adjusting nlink here so check_nlinks() will pick
1939 	 * it up, it ignores inodes with nlink 0
1940 	 */
1941 	if (fsck_err_on(backpointer_exists && !target->bi_nlink,
1942 			trans, inode_multiple_links_but_nlink_0,
1943 			"inode %llu:%u type %s has multiple links but i_nlink 0\n%s",
1944 			target->bi_inum, target->bi_snapshot, bch2_d_types[d.v->d_type], buf.buf)) {
1945 		target->bi_nlink++;
1946 		target->bi_flags &= ~BCH_INODE_unlinked;
1947 		ret = __bch2_fsck_write_inode(trans, target);
1948 		if (ret)
1949 			goto err;
1950 	}
1951 out:
1952 err:
1953 fsck_err:
1954 	bch2_trans_iter_exit(trans, &bp_iter);
1955 	printbuf_exit(&buf);
1956 	bch_err_fn(c, ret);
1957 	return ret;
1958 }
1959 
1960 noinline_for_stack
1961 static int check_dirent_target(struct btree_trans *trans,
1962 			       struct btree_iter *iter,
1963 			       struct bkey_s_c_dirent d,
1964 			       struct bch_inode_unpacked *target)
1965 {
1966 	struct bch_fs *c = trans->c;
1967 	struct bkey_i_dirent *n;
1968 	struct printbuf buf = PRINTBUF;
1969 	int ret = 0;
1970 
1971 	ret = check_dirent_inode_dirent(trans, iter, d, target);
1972 	if (ret)
1973 		goto err;
1974 
1975 	if (fsck_err_on(d.v->d_type != inode_d_type(target),
1976 			trans, dirent_d_type_wrong,
1977 			"incorrect d_type: got %s, should be %s:\n%s",
1978 			bch2_d_type_str(d.v->d_type),
1979 			bch2_d_type_str(inode_d_type(target)),
1980 			(printbuf_reset(&buf),
1981 			 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1982 		n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1983 		ret = PTR_ERR_OR_ZERO(n);
1984 		if (ret)
1985 			goto err;
1986 
1987 		bkey_reassemble(&n->k_i, d.s_c);
1988 		n->v.d_type = inode_d_type(target);
1989 		if (n->v.d_type == DT_SUBVOL) {
1990 			n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1991 			n->v.d_child_subvol = cpu_to_le32(target->bi_subvol);
1992 		} else {
1993 			n->v.d_inum = cpu_to_le64(target->bi_inum);
1994 		}
1995 
1996 		ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1997 		if (ret)
1998 			goto err;
1999 
2000 		d = dirent_i_to_s_c(n);
2001 	}
2002 err:
2003 fsck_err:
2004 	printbuf_exit(&buf);
2005 	bch_err_fn(c, ret);
2006 	return ret;
2007 }
2008 
2009 /* find a subvolume that's a descendent of @snapshot: */
2010 static int find_snapshot_subvol(struct btree_trans *trans, u32 snapshot, u32 *subvolid)
2011 {
2012 	struct btree_iter iter;
2013 	struct bkey_s_c k;
2014 	int ret;
2015 
2016 	for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN, 0, k, ret) {
2017 		if (k.k->type != KEY_TYPE_subvolume)
2018 			continue;
2019 
2020 		struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
2021 		if (bch2_snapshot_is_ancestor(trans->c, le32_to_cpu(s.v->snapshot), snapshot)) {
2022 			bch2_trans_iter_exit(trans, &iter);
2023 			*subvolid = k.k->p.offset;
2024 			goto found;
2025 		}
2026 	}
2027 	if (!ret)
2028 		ret = -ENOENT;
2029 found:
2030 	bch2_trans_iter_exit(trans, &iter);
2031 	return ret;
2032 }
2033 
2034 noinline_for_stack
2035 static int check_dirent_to_subvol(struct btree_trans *trans, struct btree_iter *iter,
2036 				  struct bkey_s_c_dirent d)
2037 {
2038 	struct bch_fs *c = trans->c;
2039 	struct btree_iter subvol_iter = {};
2040 	struct bch_inode_unpacked subvol_root;
2041 	u32 parent_subvol = le32_to_cpu(d.v->d_parent_subvol);
2042 	u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
2043 	u32 parent_snapshot;
2044 	u32 new_parent_subvol = 0;
2045 	u64 parent_inum;
2046 	struct printbuf buf = PRINTBUF;
2047 	int ret = 0;
2048 
2049 	ret = subvol_lookup(trans, parent_subvol, &parent_snapshot, &parent_inum);
2050 	if (ret && !bch2_err_matches(ret, ENOENT))
2051 		return ret;
2052 
2053 	if (ret ||
2054 	    (!ret && !bch2_snapshot_is_ancestor(c, parent_snapshot, d.k->p.snapshot))) {
2055 		int ret2 = find_snapshot_subvol(trans, d.k->p.snapshot, &new_parent_subvol);
2056 		if (ret2 && !bch2_err_matches(ret, ENOENT))
2057 			return ret2;
2058 	}
2059 
2060 	if (ret &&
2061 	    !new_parent_subvol &&
2062 	    (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_subvolumes))) {
2063 		/*
2064 		 * Couldn't find a subvol for dirent's snapshot - but we lost
2065 		 * subvols, so we need to reconstruct:
2066 		 */
2067 		ret = reconstruct_subvol(trans, d.k->p.snapshot, parent_subvol, 0);
2068 		if (ret)
2069 			return ret;
2070 
2071 		parent_snapshot = d.k->p.snapshot;
2072 	}
2073 
2074 	if (fsck_err_on(ret,
2075 			trans, dirent_to_missing_parent_subvol,
2076 			"dirent parent_subvol points to missing subvolume\n%s",
2077 			(bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf)) ||
2078 	    fsck_err_on(!ret && !bch2_snapshot_is_ancestor(c, parent_snapshot, d.k->p.snapshot),
2079 			trans, dirent_not_visible_in_parent_subvol,
2080 			"dirent not visible in parent_subvol (not an ancestor of subvol snap %u)\n%s",
2081 			parent_snapshot,
2082 			(bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
2083 		if (!new_parent_subvol) {
2084 			bch_err(c, "could not find a subvol for snapshot %u", d.k->p.snapshot);
2085 			return -BCH_ERR_fsck_repair_unimplemented;
2086 		}
2087 
2088 		struct bkey_i_dirent *new_dirent = bch2_bkey_make_mut_typed(trans, iter, &d.s_c, 0, dirent);
2089 		ret = PTR_ERR_OR_ZERO(new_dirent);
2090 		if (ret)
2091 			goto err;
2092 
2093 		new_dirent->v.d_parent_subvol = cpu_to_le32(new_parent_subvol);
2094 	}
2095 
2096 	struct bkey_s_c_subvolume s =
2097 		bch2_bkey_get_iter_typed(trans, &subvol_iter,
2098 					 BTREE_ID_subvolumes, POS(0, target_subvol),
2099 					 0, subvolume);
2100 	ret = bkey_err(s.s_c);
2101 	if (ret && !bch2_err_matches(ret, ENOENT))
2102 		return ret;
2103 
2104 	if (ret) {
2105 		if (fsck_err(trans, dirent_to_missing_subvol,
2106 			     "dirent points to missing subvolume\n%s",
2107 			     (bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf)))
2108 			return __remove_dirent(trans, d.k->p);
2109 		ret = 0;
2110 		goto out;
2111 	}
2112 
2113 	if (fsck_err_on(le32_to_cpu(s.v->fs_path_parent) != parent_subvol,
2114 			trans, subvol_fs_path_parent_wrong,
2115 			"subvol with wrong fs_path_parent, should be be %u\n%s",
2116 			parent_subvol,
2117 			(bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
2118 		struct bkey_i_subvolume *n =
2119 			bch2_bkey_make_mut_typed(trans, &subvol_iter, &s.s_c, 0, subvolume);
2120 		ret = PTR_ERR_OR_ZERO(n);
2121 		if (ret)
2122 			goto err;
2123 
2124 		n->v.fs_path_parent = cpu_to_le32(parent_subvol);
2125 	}
2126 
2127 	u64 target_inum = le64_to_cpu(s.v->inode);
2128 	u32 target_snapshot = le32_to_cpu(s.v->snapshot);
2129 
2130 	ret = lookup_inode(trans, target_inum, target_snapshot, &subvol_root);
2131 	if (ret && !bch2_err_matches(ret, ENOENT))
2132 		goto err;
2133 
2134 	if (ret) {
2135 		bch_err(c, "subvol %u points to missing inode root %llu", target_subvol, target_inum);
2136 		ret = -BCH_ERR_fsck_repair_unimplemented;
2137 		goto err;
2138 	}
2139 
2140 	if (fsck_err_on(!ret && parent_subvol != subvol_root.bi_parent_subvol,
2141 			trans, inode_bi_parent_wrong,
2142 			"subvol root %llu has wrong bi_parent_subvol: got %u, should be %u",
2143 			target_inum,
2144 			subvol_root.bi_parent_subvol, parent_subvol)) {
2145 		subvol_root.bi_parent_subvol = parent_subvol;
2146 		subvol_root.bi_snapshot = le32_to_cpu(s.v->snapshot);
2147 		ret = __bch2_fsck_write_inode(trans, &subvol_root);
2148 		if (ret)
2149 			goto err;
2150 	}
2151 
2152 	ret = check_dirent_target(trans, iter, d, &subvol_root);
2153 	if (ret)
2154 		goto err;
2155 out:
2156 err:
2157 fsck_err:
2158 	bch2_trans_iter_exit(trans, &subvol_iter);
2159 	printbuf_exit(&buf);
2160 	return ret;
2161 }
2162 
2163 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
2164 			struct bkey_s_c k,
2165 			struct bch_hash_info *hash_info,
2166 			struct inode_walker *dir,
2167 			struct inode_walker *target,
2168 			struct snapshots_seen *s)
2169 {
2170 	struct bch_fs *c = trans->c;
2171 	struct inode_walker_entry *i;
2172 	struct printbuf buf = PRINTBUF;
2173 	int ret = 0;
2174 
2175 	ret = bch2_check_key_has_snapshot(trans, iter, k);
2176 	if (ret) {
2177 		ret = ret < 0 ? ret : 0;
2178 		goto out;
2179 	}
2180 
2181 	ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
2182 	if (ret)
2183 		goto err;
2184 
2185 	if (k.k->type == KEY_TYPE_whiteout)
2186 		goto out;
2187 
2188 	if (dir->last_pos.inode != k.k->p.inode && dir->have_inodes) {
2189 		ret = check_subdir_count(trans, dir);
2190 		if (ret)
2191 			goto err;
2192 	}
2193 
2194 	i = walk_inode(trans, dir, k);
2195 	ret = PTR_ERR_OR_ZERO(i);
2196 	if (ret < 0)
2197 		goto err;
2198 
2199 	ret = check_key_has_inode(trans, iter, dir, i, k);
2200 	if (ret)
2201 		goto err;
2202 
2203 	if (!i)
2204 		goto out;
2205 
2206 	if (dir->first_this_inode)
2207 		*hash_info = bch2_hash_info_init(c, &i->inode);
2208 	dir->first_this_inode = false;
2209 
2210 	ret = hash_check_key(trans, bch2_dirent_hash_desc, hash_info, iter, k);
2211 	if (ret < 0)
2212 		goto err;
2213 	if (ret) {
2214 		/* dirent has been deleted */
2215 		ret = 0;
2216 		goto out;
2217 	}
2218 
2219 	if (k.k->type != KEY_TYPE_dirent)
2220 		goto out;
2221 
2222 	struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
2223 
2224 	if (d.v->d_type == DT_SUBVOL) {
2225 		ret = check_dirent_to_subvol(trans, iter, d);
2226 		if (ret)
2227 			goto err;
2228 	} else {
2229 		ret = get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
2230 		if (ret)
2231 			goto err;
2232 
2233 		if (fsck_err_on(!target->inodes.nr,
2234 				trans, dirent_to_missing_inode,
2235 				"dirent points to missing inode:\n%s",
2236 				(printbuf_reset(&buf),
2237 				 bch2_bkey_val_to_text(&buf, c, k),
2238 				 buf.buf))) {
2239 			ret = __remove_dirent(trans, d.k->p);
2240 			if (ret)
2241 				goto err;
2242 		}
2243 
2244 		darray_for_each(target->inodes, i) {
2245 			ret = check_dirent_target(trans, iter, d, &i->inode);
2246 			if (ret)
2247 				goto err;
2248 		}
2249 	}
2250 
2251 	ret = bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
2252 	if (ret)
2253 		goto err;
2254 
2255 	if (d.v->d_type == DT_DIR)
2256 		for_each_visible_inode(c, s, dir, d.k->p.snapshot, i)
2257 			i->count++;
2258 out:
2259 err:
2260 fsck_err:
2261 	printbuf_exit(&buf);
2262 	bch_err_fn(c, ret);
2263 	return ret;
2264 }
2265 
2266 /*
2267  * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
2268  * validate d_type
2269  */
2270 int bch2_check_dirents(struct bch_fs *c)
2271 {
2272 	struct inode_walker dir = inode_walker_init();
2273 	struct inode_walker target = inode_walker_init();
2274 	struct snapshots_seen s;
2275 	struct bch_hash_info hash_info;
2276 
2277 	snapshots_seen_init(&s);
2278 
2279 	int ret = bch2_trans_run(c,
2280 		for_each_btree_key(trans, iter, BTREE_ID_dirents,
2281 				POS(BCACHEFS_ROOT_INO, 0),
2282 				BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
2283 			check_dirent(trans, &iter, k, &hash_info, &dir, &target, &s)) ?:
2284 		check_subdir_count_notnested(trans, &dir));
2285 
2286 	snapshots_seen_exit(&s);
2287 	inode_walker_exit(&dir);
2288 	inode_walker_exit(&target);
2289 	bch_err_fn(c, ret);
2290 	return ret;
2291 }
2292 
2293 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
2294 		       struct bkey_s_c k,
2295 		       struct bch_hash_info *hash_info,
2296 		       struct inode_walker *inode)
2297 {
2298 	struct bch_fs *c = trans->c;
2299 	struct inode_walker_entry *i;
2300 	int ret;
2301 
2302 	ret = bch2_check_key_has_snapshot(trans, iter, k);
2303 	if (ret < 0)
2304 		return ret;
2305 	if (ret)
2306 		return 0;
2307 
2308 	i = walk_inode(trans, inode, k);
2309 	ret = PTR_ERR_OR_ZERO(i);
2310 	if (ret)
2311 		return ret;
2312 
2313 	ret = check_key_has_inode(trans, iter, inode, i, k);
2314 	if (ret)
2315 		return ret;
2316 
2317 	if (!i)
2318 		return 0;
2319 
2320 	if (inode->first_this_inode)
2321 		*hash_info = bch2_hash_info_init(c, &i->inode);
2322 	inode->first_this_inode = false;
2323 
2324 	ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
2325 	bch_err_fn(c, ret);
2326 	return ret;
2327 }
2328 
2329 /*
2330  * Walk xattrs: verify that they all have a corresponding inode
2331  */
2332 int bch2_check_xattrs(struct bch_fs *c)
2333 {
2334 	struct inode_walker inode = inode_walker_init();
2335 	struct bch_hash_info hash_info;
2336 	int ret = 0;
2337 
2338 	ret = bch2_trans_run(c,
2339 		for_each_btree_key_commit(trans, iter, BTREE_ID_xattrs,
2340 			POS(BCACHEFS_ROOT_INO, 0),
2341 			BTREE_ITER_prefetch|BTREE_ITER_all_snapshots,
2342 			k,
2343 			NULL, NULL,
2344 			BCH_TRANS_COMMIT_no_enospc,
2345 		check_xattr(trans, &iter, k, &hash_info, &inode)));
2346 
2347 	inode_walker_exit(&inode);
2348 	bch_err_fn(c, ret);
2349 	return ret;
2350 }
2351 
2352 static int check_root_trans(struct btree_trans *trans)
2353 {
2354 	struct bch_fs *c = trans->c;
2355 	struct bch_inode_unpacked root_inode;
2356 	u32 snapshot;
2357 	u64 inum;
2358 	int ret;
2359 
2360 	ret = subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
2361 	if (ret && !bch2_err_matches(ret, ENOENT))
2362 		return ret;
2363 
2364 	if (mustfix_fsck_err_on(ret, trans, root_subvol_missing,
2365 				"root subvol missing")) {
2366 		struct bkey_i_subvolume *root_subvol =
2367 			bch2_trans_kmalloc(trans, sizeof(*root_subvol));
2368 		ret = PTR_ERR_OR_ZERO(root_subvol);
2369 		if (ret)
2370 			goto err;
2371 
2372 		snapshot	= U32_MAX;
2373 		inum		= BCACHEFS_ROOT_INO;
2374 
2375 		bkey_subvolume_init(&root_subvol->k_i);
2376 		root_subvol->k.p.offset = BCACHEFS_ROOT_SUBVOL;
2377 		root_subvol->v.flags	= 0;
2378 		root_subvol->v.snapshot	= cpu_to_le32(snapshot);
2379 		root_subvol->v.inode	= cpu_to_le64(inum);
2380 		ret = bch2_btree_insert_trans(trans, BTREE_ID_subvolumes, &root_subvol->k_i, 0);
2381 		bch_err_msg(c, ret, "writing root subvol");
2382 		if (ret)
2383 			goto err;
2384 	}
2385 
2386 	ret = lookup_inode(trans, BCACHEFS_ROOT_INO, snapshot, &root_inode);
2387 	if (ret && !bch2_err_matches(ret, ENOENT))
2388 		return ret;
2389 
2390 	if (mustfix_fsck_err_on(ret,
2391 				trans, root_dir_missing,
2392 				"root directory missing") ||
2393 	    mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode),
2394 				trans, root_inode_not_dir,
2395 				"root inode not a directory")) {
2396 		bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
2397 				0, NULL);
2398 		root_inode.bi_inum = inum;
2399 		root_inode.bi_snapshot = snapshot;
2400 
2401 		ret = __bch2_fsck_write_inode(trans, &root_inode);
2402 		bch_err_msg(c, ret, "writing root inode");
2403 	}
2404 err:
2405 fsck_err:
2406 	return ret;
2407 }
2408 
2409 /* Get root directory, create if it doesn't exist: */
2410 int bch2_check_root(struct bch_fs *c)
2411 {
2412 	int ret = bch2_trans_do(c, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2413 		check_root_trans(trans));
2414 	bch_err_fn(c, ret);
2415 	return ret;
2416 }
2417 
2418 typedef DARRAY(u32) darray_u32;
2419 
2420 static bool darray_u32_has(darray_u32 *d, u32 v)
2421 {
2422 	darray_for_each(*d, i)
2423 		if (*i == v)
2424 			return true;
2425 	return false;
2426 }
2427 
2428 static int check_subvol_path(struct btree_trans *trans, struct btree_iter *iter, struct bkey_s_c k)
2429 {
2430 	struct bch_fs *c = trans->c;
2431 	struct btree_iter parent_iter = {};
2432 	darray_u32 subvol_path = {};
2433 	struct printbuf buf = PRINTBUF;
2434 	int ret = 0;
2435 
2436 	if (k.k->type != KEY_TYPE_subvolume)
2437 		return 0;
2438 
2439 	while (k.k->p.offset != BCACHEFS_ROOT_SUBVOL) {
2440 		ret = darray_push(&subvol_path, k.k->p.offset);
2441 		if (ret)
2442 			goto err;
2443 
2444 		struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
2445 
2446 		struct bch_inode_unpacked subvol_root;
2447 		ret = bch2_inode_find_by_inum_trans(trans,
2448 					(subvol_inum) { s.k->p.offset, le64_to_cpu(s.v->inode) },
2449 					&subvol_root);
2450 		if (ret)
2451 			break;
2452 
2453 		/*
2454 		 * We've checked that inode backpointers point to valid dirents;
2455 		 * here, it's sufficient to check that the subvolume root has a
2456 		 * dirent:
2457 		 */
2458 		if (fsck_err_on(!subvol_root.bi_dir,
2459 				trans, subvol_unreachable,
2460 				"unreachable subvolume %s",
2461 				(bch2_bkey_val_to_text(&buf, c, s.s_c),
2462 				 prt_newline(&buf),
2463 				 bch2_inode_unpacked_to_text(&buf, &subvol_root),
2464 				 buf.buf))) {
2465 			ret = reattach_subvol(trans, s);
2466 			break;
2467 		}
2468 
2469 		u32 parent = le32_to_cpu(s.v->fs_path_parent);
2470 
2471 		if (darray_u32_has(&subvol_path, parent)) {
2472 			if (fsck_err(c, subvol_loop, "subvolume loop"))
2473 				ret = reattach_subvol(trans, s);
2474 			break;
2475 		}
2476 
2477 		bch2_trans_iter_exit(trans, &parent_iter);
2478 		bch2_trans_iter_init(trans, &parent_iter,
2479 				     BTREE_ID_subvolumes, POS(0, parent), 0);
2480 		k = bch2_btree_iter_peek_slot(&parent_iter);
2481 		ret = bkey_err(k);
2482 		if (ret)
2483 			goto err;
2484 
2485 		if (fsck_err_on(k.k->type != KEY_TYPE_subvolume,
2486 				trans, subvol_unreachable,
2487 				"unreachable subvolume %s",
2488 				(bch2_bkey_val_to_text(&buf, c, s.s_c),
2489 				 buf.buf))) {
2490 			ret = reattach_subvol(trans, s);
2491 			break;
2492 		}
2493 	}
2494 fsck_err:
2495 err:
2496 	printbuf_exit(&buf);
2497 	darray_exit(&subvol_path);
2498 	bch2_trans_iter_exit(trans, &parent_iter);
2499 	return ret;
2500 }
2501 
2502 int bch2_check_subvolume_structure(struct bch_fs *c)
2503 {
2504 	int ret = bch2_trans_run(c,
2505 		for_each_btree_key_commit(trans, iter,
2506 				BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_prefetch, k,
2507 				NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2508 			check_subvol_path(trans, &iter, k)));
2509 	bch_err_fn(c, ret);
2510 	return ret;
2511 }
2512 
2513 struct pathbuf_entry {
2514 	u64	inum;
2515 	u32	snapshot;
2516 };
2517 
2518 typedef DARRAY(struct pathbuf_entry) pathbuf;
2519 
2520 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
2521 {
2522 	darray_for_each(*p, i)
2523 		if (i->inum	== inum &&
2524 		    i->snapshot	== snapshot)
2525 			return true;
2526 	return false;
2527 }
2528 
2529 /*
2530  * Check that a given inode is reachable from its subvolume root - we already
2531  * verified subvolume connectivity:
2532  *
2533  * XXX: we should also be verifying that inodes are in the right subvolumes
2534  */
2535 static int check_path(struct btree_trans *trans, pathbuf *p, struct bkey_s_c inode_k)
2536 {
2537 	struct bch_fs *c = trans->c;
2538 	struct btree_iter inode_iter = {};
2539 	struct bch_inode_unpacked inode;
2540 	struct printbuf buf = PRINTBUF;
2541 	u32 snapshot = inode_k.k->p.snapshot;
2542 	int ret = 0;
2543 
2544 	p->nr = 0;
2545 
2546 	BUG_ON(bch2_inode_unpack(inode_k, &inode));
2547 
2548 	while (!inode.bi_subvol) {
2549 		struct btree_iter dirent_iter;
2550 		struct bkey_s_c_dirent d;
2551 		u32 parent_snapshot = snapshot;
2552 
2553 		d = inode_get_dirent(trans, &dirent_iter, &inode, &parent_snapshot);
2554 		ret = bkey_err(d.s_c);
2555 		if (ret && !bch2_err_matches(ret, ENOENT))
2556 			break;
2557 
2558 		if (!ret && (ret = dirent_points_to_inode(c, d, &inode)))
2559 			bch2_trans_iter_exit(trans, &dirent_iter);
2560 
2561 		if (bch2_err_matches(ret, ENOENT)) {
2562 			ret = 0;
2563 			if (fsck_err(trans, inode_unreachable,
2564 				     "unreachable inode\n%s",
2565 				     (printbuf_reset(&buf),
2566 				      bch2_bkey_val_to_text(&buf, c, inode_k),
2567 				      buf.buf)))
2568 				ret = reattach_inode(trans, &inode);
2569 			goto out;
2570 		}
2571 
2572 		bch2_trans_iter_exit(trans, &dirent_iter);
2573 
2574 		if (!S_ISDIR(inode.bi_mode))
2575 			break;
2576 
2577 		ret = darray_push(p, ((struct pathbuf_entry) {
2578 			.inum		= inode.bi_inum,
2579 			.snapshot	= snapshot,
2580 		}));
2581 		if (ret)
2582 			return ret;
2583 
2584 		snapshot = parent_snapshot;
2585 
2586 		bch2_trans_iter_exit(trans, &inode_iter);
2587 		inode_k = bch2_bkey_get_iter(trans, &inode_iter, BTREE_ID_inodes,
2588 					     SPOS(0, inode.bi_dir, snapshot), 0);
2589 		ret = bkey_err(inode_k) ?:
2590 			!bkey_is_inode(inode_k.k) ? -BCH_ERR_ENOENT_inode
2591 			: bch2_inode_unpack(inode_k, &inode);
2592 		if (ret) {
2593 			/* Should have been caught in dirents pass */
2594 			bch_err_msg(c, ret, "error looking up parent directory");
2595 			break;
2596 		}
2597 
2598 		snapshot = inode_k.k->p.snapshot;
2599 
2600 		if (path_is_dup(p, inode.bi_inum, snapshot)) {
2601 			/* XXX print path */
2602 			bch_err(c, "directory structure loop");
2603 
2604 			darray_for_each(*p, i)
2605 				pr_err("%llu:%u", i->inum, i->snapshot);
2606 			pr_err("%llu:%u", inode.bi_inum, snapshot);
2607 
2608 			if (fsck_err(trans, dir_loop, "directory structure loop")) {
2609 				ret = remove_backpointer(trans, &inode);
2610 				bch_err_msg(c, ret, "removing dirent");
2611 				if (ret)
2612 					break;
2613 
2614 				ret = reattach_inode(trans, &inode);
2615 				bch_err_msg(c, ret, "reattaching inode %llu", inode.bi_inum);
2616 			}
2617 			break;
2618 		}
2619 	}
2620 out:
2621 fsck_err:
2622 	bch2_trans_iter_exit(trans, &inode_iter);
2623 	printbuf_exit(&buf);
2624 	bch_err_fn(c, ret);
2625 	return ret;
2626 }
2627 
2628 /*
2629  * Check for unreachable inodes, as well as loops in the directory structure:
2630  * After bch2_check_dirents(), if an inode backpointer doesn't exist that means it's
2631  * unreachable:
2632  */
2633 int bch2_check_directory_structure(struct bch_fs *c)
2634 {
2635 	pathbuf path = { 0, };
2636 	int ret;
2637 
2638 	ret = bch2_trans_run(c,
2639 		for_each_btree_key_commit(trans, iter, BTREE_ID_inodes, POS_MIN,
2640 					  BTREE_ITER_intent|
2641 					  BTREE_ITER_prefetch|
2642 					  BTREE_ITER_all_snapshots, k,
2643 					  NULL, NULL, BCH_TRANS_COMMIT_no_enospc, ({
2644 			if (!bkey_is_inode(k.k))
2645 				continue;
2646 
2647 			if (bch2_inode_flags(k) & BCH_INODE_unlinked)
2648 				continue;
2649 
2650 			check_path(trans, &path, k);
2651 		})));
2652 	darray_exit(&path);
2653 
2654 	bch_err_fn(c, ret);
2655 	return ret;
2656 }
2657 
2658 struct nlink_table {
2659 	size_t		nr;
2660 	size_t		size;
2661 
2662 	struct nlink {
2663 		u64	inum;
2664 		u32	snapshot;
2665 		u32	count;
2666 	}		*d;
2667 };
2668 
2669 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2670 		     u64 inum, u32 snapshot)
2671 {
2672 	if (t->nr == t->size) {
2673 		size_t new_size = max_t(size_t, 128UL, t->size * 2);
2674 		void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2675 
2676 		if (!d) {
2677 			bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2678 				new_size);
2679 			return -BCH_ERR_ENOMEM_fsck_add_nlink;
2680 		}
2681 
2682 		if (t->d)
2683 			memcpy(d, t->d, t->size * sizeof(t->d[0]));
2684 		kvfree(t->d);
2685 
2686 		t->d = d;
2687 		t->size = new_size;
2688 	}
2689 
2690 
2691 	t->d[t->nr++] = (struct nlink) {
2692 		.inum		= inum,
2693 		.snapshot	= snapshot,
2694 	};
2695 
2696 	return 0;
2697 }
2698 
2699 static int nlink_cmp(const void *_l, const void *_r)
2700 {
2701 	const struct nlink *l = _l;
2702 	const struct nlink *r = _r;
2703 
2704 	return cmp_int(l->inum, r->inum);
2705 }
2706 
2707 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2708 		     struct nlink_table *links,
2709 		     u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2710 {
2711 	struct nlink *link, key = {
2712 		.inum = inum, .snapshot = U32_MAX,
2713 	};
2714 
2715 	if (inum < range_start || inum >= range_end)
2716 		return;
2717 
2718 	link = __inline_bsearch(&key, links->d, links->nr,
2719 				sizeof(links->d[0]), nlink_cmp);
2720 	if (!link)
2721 		return;
2722 
2723 	while (link > links->d && link[0].inum == link[-1].inum)
2724 		--link;
2725 
2726 	for (; link < links->d + links->nr && link->inum == inum; link++)
2727 		if (ref_visible(c, s, snapshot, link->snapshot)) {
2728 			link->count++;
2729 			if (link->snapshot >= snapshot)
2730 				break;
2731 		}
2732 }
2733 
2734 noinline_for_stack
2735 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2736 				       struct nlink_table *t,
2737 				       u64 start, u64 *end)
2738 {
2739 	int ret = bch2_trans_run(c,
2740 		for_each_btree_key(trans, iter, BTREE_ID_inodes,
2741 				   POS(0, start),
2742 				   BTREE_ITER_intent|
2743 				   BTREE_ITER_prefetch|
2744 				   BTREE_ITER_all_snapshots, k, ({
2745 			if (!bkey_is_inode(k.k))
2746 				continue;
2747 
2748 			/* Should never fail, checked by bch2_inode_invalid: */
2749 			struct bch_inode_unpacked u;
2750 			BUG_ON(bch2_inode_unpack(k, &u));
2751 
2752 			/*
2753 			 * Backpointer and directory structure checks are sufficient for
2754 			 * directories, since they can't have hardlinks:
2755 			 */
2756 			if (S_ISDIR(u.bi_mode))
2757 				continue;
2758 
2759 			if (!u.bi_nlink)
2760 				continue;
2761 
2762 			ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2763 			if (ret) {
2764 				*end = k.k->p.offset;
2765 				ret = 0;
2766 				break;
2767 			}
2768 			0;
2769 		})));
2770 
2771 	bch_err_fn(c, ret);
2772 	return ret;
2773 }
2774 
2775 noinline_for_stack
2776 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2777 				     u64 range_start, u64 range_end)
2778 {
2779 	struct snapshots_seen s;
2780 
2781 	snapshots_seen_init(&s);
2782 
2783 	int ret = bch2_trans_run(c,
2784 		for_each_btree_key(trans, iter, BTREE_ID_dirents, POS_MIN,
2785 				   BTREE_ITER_intent|
2786 				   BTREE_ITER_prefetch|
2787 				   BTREE_ITER_all_snapshots, k, ({
2788 			ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2789 			if (ret)
2790 				break;
2791 
2792 			if (k.k->type == KEY_TYPE_dirent) {
2793 				struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
2794 
2795 				if (d.v->d_type != DT_DIR &&
2796 				    d.v->d_type != DT_SUBVOL)
2797 					inc_link(c, &s, links, range_start, range_end,
2798 						 le64_to_cpu(d.v->d_inum), d.k->p.snapshot);
2799 			}
2800 			0;
2801 		})));
2802 
2803 	snapshots_seen_exit(&s);
2804 
2805 	bch_err_fn(c, ret);
2806 	return ret;
2807 }
2808 
2809 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2810 				     struct bkey_s_c k,
2811 				     struct nlink_table *links,
2812 				     size_t *idx, u64 range_end)
2813 {
2814 	struct bch_inode_unpacked u;
2815 	struct nlink *link = &links->d[*idx];
2816 	int ret = 0;
2817 
2818 	if (k.k->p.offset >= range_end)
2819 		return 1;
2820 
2821 	if (!bkey_is_inode(k.k))
2822 		return 0;
2823 
2824 	BUG_ON(bch2_inode_unpack(k, &u));
2825 
2826 	if (S_ISDIR(u.bi_mode))
2827 		return 0;
2828 
2829 	if (!u.bi_nlink)
2830 		return 0;
2831 
2832 	while ((cmp_int(link->inum, k.k->p.offset) ?:
2833 		cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2834 		BUG_ON(*idx == links->nr);
2835 		link = &links->d[++*idx];
2836 	}
2837 
2838 	if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count,
2839 			trans, inode_wrong_nlink,
2840 			"inode %llu type %s has wrong i_nlink (%u, should be %u)",
2841 			u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2842 			bch2_inode_nlink_get(&u), link->count)) {
2843 		bch2_inode_nlink_set(&u, link->count);
2844 		ret = __bch2_fsck_write_inode(trans, &u);
2845 	}
2846 fsck_err:
2847 	return ret;
2848 }
2849 
2850 noinline_for_stack
2851 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2852 			       struct nlink_table *links,
2853 			       u64 range_start, u64 range_end)
2854 {
2855 	size_t idx = 0;
2856 
2857 	int ret = bch2_trans_run(c,
2858 		for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
2859 				POS(0, range_start),
2860 				BTREE_ITER_intent|BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
2861 				NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2862 			check_nlinks_update_inode(trans, &iter, k, links, &idx, range_end)));
2863 	if (ret < 0) {
2864 		bch_err(c, "error in fsck walking inodes: %s", bch2_err_str(ret));
2865 		return ret;
2866 	}
2867 
2868 	return 0;
2869 }
2870 
2871 int bch2_check_nlinks(struct bch_fs *c)
2872 {
2873 	struct nlink_table links = { 0 };
2874 	u64 this_iter_range_start, next_iter_range_start = 0;
2875 	int ret = 0;
2876 
2877 	do {
2878 		this_iter_range_start = next_iter_range_start;
2879 		next_iter_range_start = U64_MAX;
2880 
2881 		ret = check_nlinks_find_hardlinks(c, &links,
2882 						  this_iter_range_start,
2883 						  &next_iter_range_start);
2884 
2885 		ret = check_nlinks_walk_dirents(c, &links,
2886 					  this_iter_range_start,
2887 					  next_iter_range_start);
2888 		if (ret)
2889 			break;
2890 
2891 		ret = check_nlinks_update_hardlinks(c, &links,
2892 					 this_iter_range_start,
2893 					 next_iter_range_start);
2894 		if (ret)
2895 			break;
2896 
2897 		links.nr = 0;
2898 	} while (next_iter_range_start != U64_MAX);
2899 
2900 	kvfree(links.d);
2901 	bch_err_fn(c, ret);
2902 	return ret;
2903 }
2904 
2905 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2906 			     struct bkey_s_c k)
2907 {
2908 	struct bkey_s_c_reflink_p p;
2909 	struct bkey_i_reflink_p *u;
2910 
2911 	if (k.k->type != KEY_TYPE_reflink_p)
2912 		return 0;
2913 
2914 	p = bkey_s_c_to_reflink_p(k);
2915 
2916 	if (!p.v->front_pad && !p.v->back_pad)
2917 		return 0;
2918 
2919 	u = bch2_trans_kmalloc(trans, sizeof(*u));
2920 	int ret = PTR_ERR_OR_ZERO(u);
2921 	if (ret)
2922 		return ret;
2923 
2924 	bkey_reassemble(&u->k_i, k);
2925 	u->v.front_pad	= 0;
2926 	u->v.back_pad	= 0;
2927 
2928 	return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_norun);
2929 }
2930 
2931 int bch2_fix_reflink_p(struct bch_fs *c)
2932 {
2933 	if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2934 		return 0;
2935 
2936 	int ret = bch2_trans_run(c,
2937 		for_each_btree_key_commit(trans, iter,
2938 				BTREE_ID_extents, POS_MIN,
2939 				BTREE_ITER_intent|BTREE_ITER_prefetch|
2940 				BTREE_ITER_all_snapshots, k,
2941 				NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2942 			fix_reflink_p_key(trans, &iter, k)));
2943 	bch_err_fn(c, ret);
2944 	return ret;
2945 }
2946