Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-block.git] / fs / bcachefs / fsck.c
CommitLineData
1c6fdbd8
KO
1// SPDX-License-Identifier: GPL-2.0
2
3#include "bcachefs.h"
e5ea0529 4#include "bcachefs_ioctl.h"
07a1006a 5#include "bkey_buf.h"
88dfe193 6#include "btree_cache.h"
1c6fdbd8 7#include "btree_update.h"
e3dc75eb 8#include "buckets.h"
91d961ba 9#include "darray.h"
1c6fdbd8
KO
10#include "dirent.h"
11#include "error.h"
16005147 12#include "fs.h"
1c6fdbd8
KO
13#include "fsck.h"
14#include "inode.h"
15#include "keylist.h"
4fcd4de0 16#include "namei.h"
d2554263 17#include "recovery_passes.h"
8e877caa 18#include "snapshot.h"
1c6fdbd8 19#include "super.h"
e5ea0529 20#include "thread_with_file.h"
1c6fdbd8
KO
21#include "xattr.h"
22
fc51b041 23#include <linux/bsearch.h>
1c6fdbd8 24#include <linux/dcache.h> /* struct qstr */
1c6fdbd8 25
09b9c72b
KO
26static int dirent_points_to_inode_nowarn(struct bch_fs *c,
27 struct bkey_s_c_dirent d,
abaa6d4f 28 struct bch_inode_unpacked *inode)
a6508079
KO
29{
30 if (d.v->d_type == DT_SUBVOL
31 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
32 : le64_to_cpu(d.v->d_inum) == inode->bi_inum)
33 return 0;
09b9c72b 34 return bch_err_throw(c, ENOENT_dirent_doesnt_match_inode);
a6508079
KO
35}
36
37static void dirent_inode_mismatch_msg(struct printbuf *out,
38 struct bch_fs *c,
39 struct bkey_s_c_dirent dirent,
40 struct bch_inode_unpacked *inode)
41{
42 prt_str(out, "inode points to dirent that does not point back:");
43 prt_newline(out);
44 bch2_bkey_val_to_text(out, c, dirent.s_c);
45 prt_newline(out);
46 bch2_inode_unpacked_to_text(out, inode);
47}
48
49static int dirent_points_to_inode(struct bch_fs *c,
50 struct bkey_s_c_dirent dirent,
51 struct bch_inode_unpacked *inode)
52{
09b9c72b 53 int ret = dirent_points_to_inode_nowarn(c, dirent, inode);
a6508079
KO
54 if (ret) {
55 struct printbuf buf = PRINTBUF;
56 dirent_inode_mismatch_msg(&buf, c, dirent, inode);
57 bch_warn(c, "%s", buf.buf);
58 printbuf_exit(&buf);
59 }
60 return ret;
61}
62
42590b53
KO
63/*
64 * XXX: this is handling transaction restarts without returning
65 * -BCH_ERR_transaction_restart_nested, this is not how we do things anymore:
66 */
ef1669ff
KO
67static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
68 u32 snapshot)
424eb881 69{
424eb881
KO
70 u64 sectors = 0;
71
000fe8d5 72 int ret = for_each_btree_key_max(trans, iter, BTREE_ID_extents,
44ddd8ad
KO
73 SPOS(inum, 0, snapshot),
74 POS(inum, U64_MAX),
75 0, k, ({
424eb881
KO
76 if (bkey_extent_is_allocation(k.k))
77 sectors += k.k->size;
44ddd8ad
KO
78 0;
79 }));
94f651e2
KO
80
81 return ret ?: sectors;
424eb881
KO
82}
83
ef1669ff
KO
84static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
85 u32 snapshot)
86{
ef1669ff 87 u64 subdirs = 0;
ef1669ff 88
000fe8d5 89 int ret = for_each_btree_key_max(trans, iter, BTREE_ID_dirents,
3a860b5a
KO
90 SPOS(inum, 0, snapshot),
91 POS(inum, U64_MAX),
44ddd8ad
KO
92 0, k, ({
93 if (k.k->type == KEY_TYPE_dirent &&
94 bkey_s_c_to_dirent(k).v->d_type == DT_DIR)
ef1669ff 95 subdirs++;
44ddd8ad
KO
96 0;
97 }));
ef1669ff
KO
98
99 return ret ?: subdirs;
100}
101
c98d132e
KO
102static int subvol_lookup(struct btree_trans *trans, u32 subvol,
103 u32 *snapshot, u64 *inum)
81ed9ce3 104{
97996ddf 105 struct bch_subvolume s;
ce701571 106 int ret = bch2_subvolume_get(trans, subvol, false, &s);
81ed9ce3 107
97996ddf
KO
108 *snapshot = le32_to_cpu(s.snapshot);
109 *inum = le64_to_cpu(s.inode);
81ed9ce3 110 return ret;
81ed9ce3
KO
111}
112
d2fda304 113static int lookup_dirent_in_snapshot(struct btree_trans *trans,
ef1669ff
KO
114 struct bch_hash_info hash_info,
115 subvol_inum dir, struct qstr *name,
d2fda304 116 u64 *target, unsigned *type, u32 snapshot)
ef1669ff
KO
117{
118 struct btree_iter iter;
ac01928b
KO
119 struct bkey_s_c k = bch2_hash_lookup_in_snapshot(trans, &iter, bch2_dirent_hash_desc,
120 &hash_info, dir, name, 0, snapshot);
121 int ret = bkey_err(k);
ef1669ff
KO
122 if (ret)
123 return ret;
124
d6cf8958 125 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
ef1669ff
KO
126 *target = le64_to_cpu(d.v->d_inum);
127 *type = d.v->d_type;
128 bch2_trans_iter_exit(trans, &iter);
129 return 0;
130}
131
b5e4cd08
KO
132/*
133 * Find any subvolume associated with a tree of snapshots
134 * We can't rely on master_subvol - it might have been deleted.
135 */
136static int find_snapshot_tree_subvol(struct btree_trans *trans,
137 u32 tree_id, u32 *subvol)
138{
139 struct btree_iter iter;
140 struct bkey_s_c k;
141 int ret;
142
143 for_each_btree_key_norestart(trans, iter, BTREE_ID_snapshots, POS_MIN, 0, k, ret) {
144 if (k.k->type != KEY_TYPE_snapshot)
145 continue;
146
147 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(k);
148 if (le32_to_cpu(s.v->tree) != tree_id)
149 continue;
150
151 if (s.v->subvol) {
152 *subvol = le32_to_cpu(s.v->subvol);
153 goto found;
154 }
155 }
09b9c72b 156 ret = bch_err_throw(trans->c, ENOENT_no_snapshot_tree_subvol);
b5e4cd08
KO
157found:
158 bch2_trans_iter_exit(trans, &iter);
159 return ret;
160}
161
58686a25 162/* Get lost+found, create if it doesn't exist: */
d296e7b1 163static int lookup_lostfound(struct btree_trans *trans, u32 snapshot,
cc053290
KO
164 struct bch_inode_unpacked *lostfound,
165 u64 reattaching_inum)
1c6fdbd8 166{
58686a25 167 struct bch_fs *c = trans->c;
58686a25 168 struct qstr lostfound_str = QSTR("lost+found");
9180ad2e 169 struct btree_iter lostfound_iter = {};
ef1669ff
KO
170 u64 inum = 0;
171 unsigned d_type = 0;
58686a25
KO
172 int ret;
173
d296e7b1
KO
174 struct bch_snapshot_tree st;
175 ret = bch2_snapshot_tree_lookup(trans,
176 bch2_snapshot_tree(c, snapshot), &st);
ef1669ff
KO
177 if (ret)
178 return ret;
81ed9ce3 179
b5e4cd08
KO
180 u32 subvolid;
181 ret = find_snapshot_tree_subvol(trans,
182 bch2_snapshot_tree(c, snapshot), &subvolid);
183 bch_err_msg(c, ret, "finding subvol associated with snapshot tree %u",
184 bch2_snapshot_tree(c, snapshot));
185 if (ret)
186 return ret;
d296e7b1 187
cc053290 188 struct bch_subvolume subvol;
b5e4cd08
KO
189 ret = bch2_subvolume_get(trans, subvolid, false, &subvol);
190 bch_err_msg(c, ret, "looking up subvol %u for snapshot %u", subvolid, snapshot);
285b181a 191 if (ret)
58686a25
KO
192 return ret;
193
cc053290
KO
194 if (!subvol.inode) {
195 struct btree_iter iter;
196 struct bkey_i_subvolume *subvol = bch2_bkey_get_mut_typed(trans, &iter,
b5e4cd08 197 BTREE_ID_subvolumes, POS(0, subvolid),
cc053290
KO
198 0, subvolume);
199 ret = PTR_ERR_OR_ZERO(subvol);
200 if (ret)
201 return ret;
202
203 subvol->v.inode = cpu_to_le64(reattaching_inum);
204 bch2_trans_iter_exit(trans, &iter);
205 }
206
b5e4cd08
KO
207 subvol_inum root_inum = {
208 .subvol = subvolid,
209 .inum = le64_to_cpu(subvol.inode)
210 };
cc053290 211
d296e7b1
KO
212 struct bch_inode_unpacked root_inode;
213 struct bch_hash_info root_hash_info;
77eac89c 214 ret = bch2_inode_find_by_inum_snapshot(trans, root_inum.inum, snapshot, &root_inode, 0);
cc053290 215 bch_err_msg(c, ret, "looking up root inode %llu for subvol %u",
b5e4cd08 216 root_inum.inum, subvolid);
d296e7b1
KO
217 if (ret)
218 return ret;
219
220 root_hash_info = bch2_hash_info_init(c, &root_inode);
ef1669ff 221
d2fda304
KO
222 ret = lookup_dirent_in_snapshot(trans, root_hash_info, root_inum,
223 &lostfound_str, &inum, &d_type, snapshot);
d296e7b1 224 if (bch2_err_matches(ret, ENOENT))
58686a25 225 goto create_lostfound;
58686a25 226
d2a990d1 227 bch_err_fn(c, ret);
285b181a 228 if (ret)
ef1669ff 229 return ret;
ef1669ff
KO
230
231 if (d_type != DT_DIR) {
232 bch_err(c, "error looking up lost+found: not a directory");
09b9c72b 233 return bch_err_throw(c, ENOENT_not_directory);
ef1669ff
KO
234 }
235
285b181a 236 /*
067d228b 237 * The bch2_check_dirents pass has already run, dangling dirents
285b181a
KO
238 * shouldn't exist here:
239 */
77eac89c 240 ret = bch2_inode_find_by_inum_snapshot(trans, inum, snapshot, lostfound, 0);
d2fda304
KO
241 bch_err_msg(c, ret, "looking up lost+found %llu:%u in (root inode %llu, snapshot root %u)",
242 inum, snapshot, root_inum.inum, bch2_snapshot_root(c, snapshot));
243 return ret;
58686a25 244
58686a25 245create_lostfound:
fda7b1ff
KO
246 /*
247 * we always create lost+found in the root snapshot; we don't want
248 * different branches of the snapshot tree to have different lost+found
249 */
250 snapshot = le32_to_cpu(st.root_snapshot);
d296e7b1
KO
251 /*
252 * XXX: we could have a nicer log message here if we had a nice way to
253 * walk backpointers to print a path
254 */
f7727a67
KO
255 struct printbuf path = PRINTBUF;
256 ret = bch2_inum_to_path(trans, root_inum, &path);
257 if (ret)
258 goto err;
259
260 bch_notice(c, "creating %s/lost+found in subvol %llu snapshot %u",
261 path.buf, root_inum.subvol, snapshot);
262 printbuf_exit(&path);
d296e7b1
KO
263
264 u64 now = bch2_current_time(c);
d296e7b1
KO
265 u64 cpu = raw_smp_processor_id();
266
285b181a 267 bch2_inode_init_early(c, lostfound);
4ba99dde 268 bch2_inode_init_late(c, lostfound, now, 0, 0, S_IFDIR|0700, 0, &root_inode);
d296e7b1 269 lostfound->bi_dir = root_inode.bi_inum;
72350ee0 270 lostfound->bi_snapshot = le32_to_cpu(st.root_snapshot);
d296e7b1
KO
271
272 root_inode.bi_nlink++;
273
274 ret = bch2_inode_create(trans, &lostfound_iter, lostfound, snapshot, cpu);
275 if (ret)
276 goto err;
285b181a 277
9180ad2e
KO
278 bch2_btree_iter_set_snapshot(trans, &lostfound_iter, snapshot);
279 ret = bch2_btree_iter_traverse(trans, &lostfound_iter);
d296e7b1
KO
280 if (ret)
281 goto err;
282
283 ret = bch2_dirent_create_snapshot(trans,
56e23047 284 0, root_inode.bi_inum, snapshot, &root_hash_info,
d296e7b1
KO
285 mode_to_type(lostfound->bi_mode),
286 &lostfound_str,
287 lostfound->bi_inum,
288 &lostfound->bi_dir_offset,
ecd76c5f 289 BTREE_UPDATE_internal_snapshot_node|
5dd8c60e 290 STR_HASH_must_create) ?:
d296e7b1 291 bch2_inode_write_flags(trans, &lostfound_iter, lostfound,
5dd8c60e 292 BTREE_UPDATE_internal_snapshot_node);
d296e7b1 293err:
d2a990d1 294 bch_err_msg(c, ret, "creating lost+found");
d296e7b1 295 bch2_trans_iter_exit(trans, &lostfound_iter);
285b181a 296 return ret;
58686a25
KO
297}
298
38864ecc
KO
299static inline bool inode_should_reattach(struct bch_inode_unpacked *inode)
300{
301 if (inode->bi_inum == BCACHEFS_ROOT_INO &&
302 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)
303 return false;
304
261592ba
KO
305 /*
306 * Subvolume roots are special: older versions of subvolume roots may be
307 * disconnected, it's only the newest version that matters.
308 *
309 * We only keep a single dirent pointing to a subvolume root, i.e.
310 * older versions of snapshots will not have a different dirent pointing
311 * to the same subvolume root.
312 *
313 * This is because dirents that point to subvolumes are only visible in
314 * the parent subvolume - versioning is not needed - and keeping them
315 * around would break fsck, because when we're crossing subvolumes we
316 * don't have a consistent snapshot ID to do check the inode <-> dirent
317 * relationships.
318 *
319 * Thus, a subvolume root that's been renamed after a snapshot will have
320 * a disconnected older version - that's expected.
321 *
322 * Note that taking a snapshot always updates the root inode (to update
323 * the dirent backpointer), so a subvolume root inode with
324 * BCH_INODE_has_child_snapshot is never visible.
325 */
326 if (inode->bi_subvol &&
327 (inode->bi_flags & BCH_INODE_has_child_snapshot))
328 return false;
329
bbc3a0b1
KO
330 return !bch2_inode_has_backpointer(inode) &&
331 !(inode->bi_flags & BCH_INODE_unlinked);
38864ecc
KO
332}
333
334static int maybe_delete_dirent(struct btree_trans *trans, struct bpos d_pos, u32 snapshot)
335{
336 struct btree_iter iter;
337 struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_dirents,
338 SPOS(d_pos.inode, d_pos.offset, snapshot),
339 BTREE_ITER_intent|
340 BTREE_ITER_with_updates);
341 int ret = bkey_err(k);
342 if (ret)
343 return ret;
344
345 if (bpos_eq(k.k->p, d_pos)) {
346 /*
347 * delet_at() doesn't work because the update path doesn't
348 * internally use BTREE_ITER_with_updates yet
349 */
350 struct bkey_i *k = bch2_trans_kmalloc(trans, sizeof(*k));
351 ret = PTR_ERR_OR_ZERO(k);
352 if (ret)
353 goto err;
354
355 bkey_init(&k->k);
356 k->k.type = KEY_TYPE_whiteout;
357 k->k.p = iter.pos;
358 ret = bch2_trans_update(trans, &iter, k, BTREE_UPDATE_internal_snapshot_node);
359 }
360err:
361 bch2_trans_iter_exit(trans, &iter);
362 return ret;
363}
364
72350ee0 365static int reattach_inode(struct btree_trans *trans, struct bch_inode_unpacked *inode)
58686a25 366{
7a086baa 367 struct bch_fs *c = trans->c;
58686a25 368 struct bch_inode_unpacked lostfound;
1c6fdbd8 369 char name_buf[20];
1c6fdbd8
KO
370 int ret;
371
38864ecc 372 u32 dirent_snapshot = inode->bi_snapshot;
56e23047
KO
373 if (inode->bi_subvol) {
374 inode->bi_parent_subvol = BCACHEFS_ROOT_SUBVOL;
375
9fb09ace
KO
376 struct btree_iter subvol_iter;
377 struct bkey_i_subvolume *subvol =
378 bch2_bkey_get_mut_typed(trans, &subvol_iter,
379 BTREE_ID_subvolumes, POS(0, inode->bi_subvol),
380 0, subvolume);
381 ret = PTR_ERR_OR_ZERO(subvol);
382 if (ret)
383 return ret;
384
385 subvol->v.fs_path_parent = BCACHEFS_ROOT_SUBVOL;
386 bch2_trans_iter_exit(trans, &subvol_iter);
387
56e23047
KO
388 u64 root_inum;
389 ret = subvol_lookup(trans, inode->bi_parent_subvol,
390 &dirent_snapshot, &root_inum);
391 if (ret)
392 return ret;
393
394 snprintf(name_buf, sizeof(name_buf), "subvol-%u", inode->bi_subvol);
395 } else {
396 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
397 }
398
cc053290 399 ret = lookup_lostfound(trans, dirent_snapshot, &lostfound, inode->bi_inum);
176cf4bf 400 if (ret)
d3ff7fec 401 return ret;
176cf4bf 402
8d6ac823
KO
403 bch_verbose(c, "got lostfound inum %llu", lostfound.bi_inum);
404
20826fe6 405 lostfound.bi_nlink += S_ISDIR(inode->bi_mode);
176cf4bf 406
20826fe6 407 /* ensure lost+found inode is also present in inode snapshot */
72350ee0
KO
408 if (!inode->bi_subvol) {
409 BUG_ON(!bch2_snapshot_is_ancestor(c, inode->bi_snapshot, lostfound.bi_snapshot));
410 lostfound.bi_snapshot = inode->bi_snapshot;
411 }
412
413 ret = __bch2_fsck_write_inode(trans, &lostfound);
20826fe6
KO
414 if (ret)
415 return ret;
176cf4bf 416
38864ecc 417 struct bch_hash_info dir_hash = bch2_hash_info_init(c, &lostfound);
c1feab95 418 struct qstr name = QSTR(name_buf);
176cf4bf 419
38864ecc 420 inode->bi_dir = lostfound.bi_inum;
176cf4bf 421
d296e7b1 422 ret = bch2_dirent_create_snapshot(trans,
56e23047
KO
423 inode->bi_parent_subvol, lostfound.bi_inum,
424 dirent_snapshot,
d296e7b1
KO
425 &dir_hash,
426 inode_d_type(inode),
56e23047
KO
427 &name,
428 inode->bi_subvol ?: inode->bi_inum,
38864ecc 429 &inode->bi_dir_offset,
ecd76c5f 430 BTREE_UPDATE_internal_snapshot_node|
5dd8c60e 431 STR_HASH_must_create);
7a086baa
KO
432 if (ret) {
433 bch_err_msg(c, ret, "error creating dirent");
285b181a 434 return ret;
7a086baa 435 }
285b181a 436
38864ecc
KO
437 ret = __bch2_fsck_write_inode(trans, inode);
438 if (ret)
439 return ret;
440
8d6ac823
KO
441 {
442 CLASS(printbuf, buf)();
443 ret = bch2_inum_snapshot_to_path(trans, inode->bi_inum,
444 inode->bi_snapshot, NULL, &buf);
445 if (ret)
446 return ret;
447
448 bch_info(c, "reattached at %s", buf.buf);
449 }
450
38864ecc
KO
451 /*
452 * Fix up inodes in child snapshots: if they should also be reattached
453 * update the backpointer field, if they should not be we need to emit
454 * whiteouts for the dirent we just created.
455 */
456 if (!inode->bi_subvol && bch2_snapshot_is_leaf(c, inode->bi_snapshot) <= 0) {
457 snapshot_id_list whiteouts_done;
458 struct btree_iter iter;
459 struct bkey_s_c k;
460
461 darray_init(&whiteouts_done);
462
463 for_each_btree_key_reverse_norestart(trans, iter,
464 BTREE_ID_inodes, SPOS(0, inode->bi_inum, inode->bi_snapshot - 1),
465 BTREE_ITER_all_snapshots|BTREE_ITER_intent, k, ret) {
466 if (k.k->p.offset != inode->bi_inum)
467 break;
468
469 if (!bkey_is_inode(k.k) ||
470 !bch2_snapshot_is_ancestor(c, k.k->p.snapshot, inode->bi_snapshot) ||
471 snapshot_list_has_ancestor(c, &whiteouts_done, k.k->p.snapshot))
472 continue;
473
474 struct bch_inode_unpacked child_inode;
644457ed
KO
475 ret = bch2_inode_unpack(k, &child_inode);
476 if (ret)
477 break;
38864ecc
KO
478
479 if (!inode_should_reattach(&child_inode)) {
480 ret = maybe_delete_dirent(trans,
481 SPOS(lostfound.bi_inum, inode->bi_dir_offset,
482 dirent_snapshot),
483 k.k->p.snapshot);
484 if (ret)
485 break;
486
487 ret = snapshot_list_add(c, &whiteouts_done, k.k->p.snapshot);
488 if (ret)
489 break;
490 } else {
491 iter.snapshot = k.k->p.snapshot;
492 child_inode.bi_dir = inode->bi_dir;
493 child_inode.bi_dir_offset = inode->bi_dir_offset;
494
495 ret = bch2_inode_write_flags(trans, &iter, &child_inode,
496 BTREE_UPDATE_internal_snapshot_node);
497 if (ret)
498 break;
499 }
500 }
501 darray_exit(&whiteouts_done);
502 bch2_trans_iter_exit(trans, &iter);
503 }
285b181a 504
38864ecc 505 return ret;
285b181a
KO
506}
507
1325ccf2
KO
508static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
509 struct btree_iter *iter,
510 struct bpos pos)
511{
512 return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
513}
514
d3ff7fec
KO
515static int remove_backpointer(struct btree_trans *trans,
516 struct bch_inode_unpacked *inode)
517{
bbc3a0b1 518 if (!bch2_inode_has_backpointer(inode))
0b0f0ad9 519 return 0;
d3ff7fec 520
c1ca07a4
KO
521 u32 snapshot = inode->bi_snapshot;
522
523 if (inode->bi_parent_subvol) {
524 int ret = bch2_subvolume_get_snapshot(trans, inode->bi_parent_subvol, &snapshot);
525 if (ret)
526 return ret;
527 }
528
0b0f0ad9
KO
529 struct bch_fs *c = trans->c;
530 struct btree_iter iter;
1325ccf2 531 struct bkey_s_c_dirent d = dirent_get_by_pos(trans, &iter,
c1ca07a4 532 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1325ccf2
KO
533 int ret = bkey_err(d) ?:
534 dirent_points_to_inode(c, d, inode) ?:
758ea4ff 535 bch2_fsck_remove_dirent(trans, d.k->p);
67e0dd8f 536 bch2_trans_iter_exit(trans, &iter);
d3ff7fec
KO
537 return ret;
538}
539
663db5a5
KO
540static int reattach_subvol(struct btree_trans *trans, struct bkey_s_c_subvolume s)
541{
542 struct bch_fs *c = trans->c;
543
544 struct bch_inode_unpacked inode;
545 int ret = bch2_inode_find_by_inum_trans(trans,
546 (subvol_inum) { s.k->p.offset, le64_to_cpu(s.v->inode) },
547 &inode);
548 if (ret)
549 return ret;
550
551 ret = remove_backpointer(trans, &inode);
a6508079
KO
552 if (!bch2_err_matches(ret, ENOENT))
553 bch_err_msg(c, ret, "removing dirent");
663db5a5
KO
554 if (ret)
555 return ret;
556
72350ee0 557 ret = reattach_inode(trans, &inode);
663db5a5
KO
558 bch_err_msg(c, ret, "reattaching inode %llu", inode.bi_inum);
559 return ret;
560}
561
cc053290
KO
562static int reconstruct_subvol(struct btree_trans *trans, u32 snapshotid, u32 subvolid, u64 inum)
563{
564 struct bch_fs *c = trans->c;
565
566 if (!bch2_snapshot_is_leaf(c, snapshotid)) {
567 bch_err(c, "need to reconstruct subvol, but have interior node snapshot");
09b9c72b 568 return bch_err_throw(c, fsck_repair_unimplemented);
cc053290
KO
569 }
570
571 /*
572 * If inum isn't set, that means we're being called from check_dirents,
573 * not check_inodes - the root of this subvolume doesn't exist or we
574 * would have found it there:
575 */
576 if (!inum) {
577 struct btree_iter inode_iter = {};
578 struct bch_inode_unpacked new_inode;
579 u64 cpu = raw_smp_processor_id();
580
581 bch2_inode_init_early(c, &new_inode);
4ba99dde 582 bch2_inode_init_late(c, &new_inode, bch2_current_time(c), 0, 0, S_IFDIR|0755, 0, NULL);
cc053290
KO
583
584 new_inode.bi_subvol = subvolid;
585
586 int ret = bch2_inode_create(trans, &inode_iter, &new_inode, snapshotid, cpu) ?:
9180ad2e 587 bch2_btree_iter_traverse(trans, &inode_iter) ?:
cc053290
KO
588 bch2_inode_write(trans, &inode_iter, &new_inode);
589 bch2_trans_iter_exit(trans, &inode_iter);
590 if (ret)
591 return ret;
592
593 inum = new_inode.bi_inum;
594 }
595
596 bch_info(c, "reconstructing subvol %u with root inode %llu", subvolid, inum);
597
598 struct bkey_i_subvolume *new_subvol = bch2_trans_kmalloc(trans, sizeof(*new_subvol));
599 int ret = PTR_ERR_OR_ZERO(new_subvol);
600 if (ret)
601 return ret;
602
603 bkey_subvolume_init(&new_subvol->k_i);
604 new_subvol->k.p.offset = subvolid;
605 new_subvol->v.snapshot = cpu_to_le32(snapshotid);
606 new_subvol->v.inode = cpu_to_le64(inum);
607 ret = bch2_btree_insert_trans(trans, BTREE_ID_subvolumes, &new_subvol->k_i, 0);
608 if (ret)
609 return ret;
610
611 struct btree_iter iter;
612 struct bkey_i_snapshot *s = bch2_bkey_get_mut_typed(trans, &iter,
613 BTREE_ID_snapshots, POS(0, snapshotid),
614 0, snapshot);
615 ret = PTR_ERR_OR_ZERO(s);
616 bch_err_msg(c, ret, "getting snapshot %u", snapshotid);
617 if (ret)
618 return ret;
619
620 u32 snapshot_tree = le32_to_cpu(s->v.tree);
621
622 s->v.subvol = cpu_to_le32(subvolid);
623 SET_BCH_SNAPSHOT_SUBVOL(&s->v, true);
624 bch2_trans_iter_exit(trans, &iter);
625
626 struct bkey_i_snapshot_tree *st = bch2_bkey_get_mut_typed(trans, &iter,
627 BTREE_ID_snapshot_trees, POS(0, snapshot_tree),
628 0, snapshot_tree);
629 ret = PTR_ERR_OR_ZERO(st);
630 bch_err_msg(c, ret, "getting snapshot tree %u", snapshot_tree);
631 if (ret)
632 return ret;
633
634 if (!st->v.master_subvol)
635 st->v.master_subvol = cpu_to_le32(subvolid);
636
637 bch2_trans_iter_exit(trans, &iter);
638 return 0;
639}
640
c13d526d 641static int reconstruct_inode(struct btree_trans *trans, enum btree_id btree, u32 snapshot, u64 inum)
09d4c2ac
KO
642{
643 struct bch_fs *c = trans->c;
c13d526d
KO
644 unsigned i_mode = S_IFREG;
645 u64 i_size = 0;
09d4c2ac 646
c13d526d
KO
647 switch (btree) {
648 case BTREE_ID_extents: {
649 struct btree_iter iter = {};
09d4c2ac 650
c13d526d 651 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, SPOS(inum, U64_MAX, snapshot), 0);
9180ad2e 652 struct bkey_s_c k = bch2_btree_iter_peek_prev_min(trans, &iter, POS(inum, 0));
c13d526d
KO
653 bch2_trans_iter_exit(trans, &iter);
654 int ret = bkey_err(k);
655 if (ret)
656 return ret;
09d4c2ac 657
c13d526d
KO
658 i_size = k.k->p.offset << 9;
659 break;
660 }
661 case BTREE_ID_dirents:
662 i_mode = S_IFDIR;
663 break;
664 case BTREE_ID_xattrs:
665 break;
666 default:
667 BUG();
668 }
09d4c2ac 669
c13d526d
KO
670 struct bch_inode_unpacked new_inode;
671 bch2_inode_init_early(c, &new_inode);
4ba99dde 672 bch2_inode_init_late(c, &new_inode, bch2_current_time(c), 0, 0, i_mode|0600, 0, NULL);
c13d526d
KO
673 new_inode.bi_size = i_size;
674 new_inode.bi_inum = inum;
72350ee0 675 new_inode.bi_snapshot = snapshot;
09d4c2ac 676
72350ee0 677 return __bch2_fsck_write_inode(trans, &new_inode);
09d4c2ac
KO
678}
679
49124d8a
KO
680static inline void snapshots_seen_exit(struct snapshots_seen *s)
681{
682 darray_exit(&s->ids);
683}
684
685static inline void snapshots_seen_init(struct snapshots_seen *s)
686{
687 memset(s, 0, sizeof(*s));
688}
689
e2bd0617
KO
690static int snapshots_seen_add_inorder(struct bch_fs *c, struct snapshots_seen *s, u32 id)
691{
5a2d1521 692 u32 *i;
defd9e39 693 __darray_for_each(s->ids, i) {
5a2d1521 694 if (*i == id)
e2bd0617 695 return 0;
5a2d1521 696 if (*i > id)
e2bd0617
KO
697 break;
698 }
699
5a2d1521 700 int ret = darray_insert_item(&s->ids, i - s->ids.data, id);
e2bd0617
KO
701 if (ret)
702 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
703 s->ids.size);
704 return ret;
705}
706
49124d8a
KO
707static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
708 enum btree_id btree_id, struct bpos pos)
ef1669ff 709{
e88a75eb 710 if (!bkey_eq(s->pos, pos))
91d961ba 711 s->ids.nr = 0;
ef1669ff 712 s->pos = pos;
49124d8a 713
5a2d1521 714 return snapshot_list_add_nodup(c, &s->ids, pos.snapshot);
ef1669ff
KO
715}
716
717/**
718 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
719 * and @ancestor hasn't been overwritten in @seen
720 *
96dea3d5
KO
721 * @c: filesystem handle
722 * @seen: list of snapshot ids already seen at current position
723 * @id: descendent snapshot id
724 * @ancestor: ancestor snapshot id
725 *
726 * Returns: whether key in @ancestor snapshot is visible in @id snapshot
ef1669ff
KO
727 */
728static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
729 u32 id, u32 ancestor)
730{
464ee192 731 EBUG_ON(id > ancestor);
ef1669ff 732
ef1669ff
KO
733 if (id == ancestor)
734 return true;
735
736 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
737 return false;
738
464ee192
KO
739 /*
740 * We know that @id is a descendant of @ancestor, we're checking if
741 * we've seen a key that overwrote @ancestor - i.e. also a descendent of
742 * @ascestor and with @id as a descendent.
743 *
744 * But we already know that we're scanning IDs between @id and @ancestor
745 * numerically, since snapshot ID lists are kept sorted, so if we find
746 * an id that's an ancestor of @id we're done:
747 */
1df31086
KO
748 darray_for_each_reverse(seen->ids, i)
749 if (*i != ancestor && bch2_snapshot_is_ancestor(c, id, *i))
ef1669ff
KO
750 return false;
751
752 return true;
753}
754
755/**
756 * ref_visible - given a key with snapshot id @src that points to a key with
757 * snapshot id @dst, test whether there is some snapshot in which @dst is
758 * visible.
759 *
96dea3d5
KO
760 * @c: filesystem handle
761 * @s: list of snapshot IDs already seen at @src
762 * @src: snapshot ID of src key
763 * @dst: snapshot ID of dst key
764 * Returns: true if there is some snapshot in which @dst is visible
ef1669ff 765 *
96dea3d5 766 * Assumes we're visiting @src keys in natural key order
ef1669ff 767 */
96dea3d5
KO
768static bool ref_visible(struct bch_fs *c, struct snapshots_seen *s,
769 u32 src, u32 dst)
ef1669ff
KO
770{
771 return dst <= src
772 ? key_visible_in_snapshot(c, s, dst, src)
773 : bch2_snapshot_is_ancestor(c, src, dst);
774}
775
c58029ec
DH
776static int ref_visible2(struct bch_fs *c,
777 u32 src, struct snapshots_seen *src_seen,
778 u32 dst, struct snapshots_seen *dst_seen)
779{
c58029ec
DH
780 if (dst > src) {
781 swap(dst, src);
782 swap(dst_seen, src_seen);
783 }
784 return key_visible_in_snapshot(c, src_seen, dst, src);
785}
786
49124d8a
KO
787#define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
788 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
6f2bbd57
KO
789 (_i)->inode.bi_snapshot <= (_snapshot); _i++) \
790 if (key_visible_in_snapshot(_c, _s, _i->inode.bi_snapshot, _snapshot))
ef1669ff 791
91d961ba
KO
792struct inode_walker_entry {
793 struct bch_inode_unpacked inode;
855070dc 794 bool whiteout;
91d961ba 795 u64 count;
b9ddb3e1 796 u64 i_size;
91d961ba
KO
797};
798
1c6fdbd8 799struct inode_walker {
ef1669ff 800 bool first_this_inode;
3672bda8 801 bool have_inodes;
43b81a4e 802 bool recalculate_sums;
f9f52bc4 803 struct bpos last_pos;
ef1669ff 804
91d961ba 805 DARRAY(struct inode_walker_entry) inodes;
15734b5e 806 snapshot_id_list deletes;
1c6fdbd8
KO
807};
808
ef1669ff
KO
809static void inode_walker_exit(struct inode_walker *w)
810{
91d961ba 811 darray_exit(&w->inodes);
15734b5e 812 darray_exit(&w->deletes);
ef1669ff
KO
813}
814
1c6fdbd8
KO
815static struct inode_walker inode_walker_init(void)
816{
ef1669ff
KO
817 return (struct inode_walker) { 0, };
818}
819
ef1669ff 820static int add_inode(struct bch_fs *c, struct inode_walker *w,
3e52c222 821 struct bkey_s_c inode)
ef1669ff 822{
855070dc
KO
823 int ret = darray_push(&w->inodes, ((struct inode_walker_entry) {
824 .whiteout = !bkey_is_inode(inode.k),
91d961ba 825 }));
855070dc
KO
826 if (ret)
827 return ret;
828
829 struct inode_walker_entry *n = &darray_last(w->inodes);
830 if (!n->whiteout) {
831 return bch2_inode_unpack(inode, &n->inode);
832 } else {
b17d7bdb 833 n->inode.bi_inum = inode.k->p.offset;
855070dc
KO
834 n->inode.bi_snapshot = inode.k->p.snapshot;
835 return 0;
836 }
1c6fdbd8
KO
837}
838
06dcca51
KO
839static int get_inodes_all_snapshots(struct btree_trans *trans,
840 struct inode_walker *w, u64 inum)
1c6fdbd8 841{
ef1669ff
KO
842 struct bch_fs *c = trans->c;
843 struct btree_iter iter;
844 struct bkey_s_c k;
ef1669ff 845 int ret;
1c6fdbd8 846
3672bda8
KO
847 /*
848 * We no longer have inodes for w->last_pos; clear this to avoid
849 * screwing up check_i_sectors/check_subdir_count if we take a
850 * transaction restart here:
851 */
852 w->have_inodes = false;
43b81a4e 853 w->recalculate_sums = false;
91d961ba 854 w->inodes.nr = 0;
ef1669ff 855
855070dc
KO
856 for_each_btree_key_max_norestart(trans, iter,
857 BTREE_ID_inodes, POS(0, inum), SPOS(0, inum, U32_MAX),
858 BTREE_ITER_all_snapshots, k, ret) {
859 ret = add_inode(c, w, k);
860 if (ret)
ef1669ff 861 break;
ef1669ff
KO
862 }
863 bch2_trans_iter_exit(trans, &iter);
864
865 if (ret)
866 return ret;
867
f9f52bc4 868 w->first_this_inode = true;
3672bda8 869 w->have_inodes = true;
27b2df98 870 return 0;
06dcca51
KO
871}
872
855070dc
KO
873static int get_visible_inodes(struct btree_trans *trans,
874 struct inode_walker *w,
875 struct snapshots_seen *s,
876 u64 inum)
877{
878 struct bch_fs *c = trans->c;
879 struct btree_iter iter;
880 struct bkey_s_c k;
881 int ret;
882
883 w->inodes.nr = 0;
884 w->deletes.nr = 0;
885
886 for_each_btree_key_reverse_norestart(trans, iter, BTREE_ID_inodes, SPOS(0, inum, s->pos.snapshot),
887 BTREE_ITER_all_snapshots, k, ret) {
888 if (k.k->p.offset != inum)
889 break;
890
891 if (!ref_visible(c, s, s->pos.snapshot, k.k->p.snapshot))
892 continue;
893
894 if (snapshot_list_has_ancestor(c, &w->deletes, k.k->p.snapshot))
895 continue;
896
897 ret = bkey_is_inode(k.k)
898 ? add_inode(c, w, k)
899 : snapshot_list_add(c, &w->deletes, k.k->p.snapshot);
900 if (ret)
901 break;
902 }
903 bch2_trans_iter_exit(trans, &iter);
904
905 return ret;
906}
907
06dcca51 908static struct inode_walker_entry *
0afdf496 909lookup_inode_for_snapshot(struct btree_trans *trans, struct inode_walker *w, struct bkey_s_c k)
06dcca51 910{
0afdf496 911 struct bch_fs *c = trans->c;
06dcca51 912
5802caf7
KO
913 struct inode_walker_entry *i = darray_find_p(w->inodes, i,
914 bch2_snapshot_is_ancestor(c, k.k->p.snapshot, i->inode.bi_snapshot));
06dcca51 915
5802caf7
KO
916 if (!i)
917 return NULL;
ef1669ff 918
0afdf496
KO
919 struct printbuf buf = PRINTBUF;
920 int ret = 0;
971a1503 921
0afdf496
KO
922 if (fsck_err_on(k.k->p.snapshot != i->inode.bi_snapshot,
923 trans, snapshot_key_missing_inode_snapshot,
924 "have key for inode %llu:%u but have inode in ancestor snapshot %u\n"
971a1503
KO
925 "unexpected because we should always update the inode when we update a key in that inode\n"
926 "%s",
0afdf496
KO
927 w->last_pos.inode, k.k->p.snapshot, i->inode.bi_snapshot,
928 (bch2_bkey_val_to_text(&buf, c, k),
929 buf.buf))) {
0afdf496 930 if (!i->whiteout) {
c27e5782
KO
931 struct bch_inode_unpacked new = i->inode;
932 new.bi_snapshot = k.k->p.snapshot;
0afdf496
KO
933 ret = __bch2_fsck_write_inode(trans, &new);
934 } else {
c27e5782 935 struct bkey_i whiteout;
0afdf496
KO
936 bkey_init(&whiteout.k);
937 whiteout.k.type = KEY_TYPE_whiteout;
c27e5782 938 whiteout.k.p = SPOS(0, i->inode.bi_inum, k.k->p.snapshot);
0afdf496
KO
939 ret = bch2_btree_insert_nonextent(trans, BTREE_ID_inodes,
940 &whiteout,
941 BTREE_UPDATE_internal_snapshot_node);
942 }
943
944 if (ret)
945 goto fsck_err;
946
947 ret = bch2_trans_commit(trans, NULL, NULL, 0);
948 if (ret)
949 goto fsck_err;
950
951 struct inode_walker_entry new_entry = *i;
952
953 new_entry.inode.bi_snapshot = k.k->p.snapshot;
954 new_entry.count = 0;
955 new_entry.i_size = 0;
ef1669ff 956
6f2bbd57 957 while (i > w->inodes.data && i[-1].inode.bi_snapshot > k.k->p.snapshot)
ef1669ff
KO
958 --i;
959
971a1503 960 size_t pos = i - w->inodes.data;
0afdf496 961 ret = darray_insert_item(&w->inodes, pos, new_entry);
ef1669ff 962 if (ret)
0afdf496 963 goto fsck_err;
20e6d9a8 964
09b9c72b 965 ret = bch_err_throw(c, transaction_restart_nested);
0afdf496 966 goto fsck_err;
ef1669ff
KO
967 }
968
0afdf496 969 printbuf_exit(&buf);
ef1669ff 970 return i;
0afdf496
KO
971fsck_err:
972 printbuf_exit(&buf);
973 return ERR_PTR(ret);
1c6fdbd8
KO
974}
975
06dcca51 976static struct inode_walker_entry *walk_inode(struct btree_trans *trans,
971a1503
KO
977 struct inode_walker *w,
978 struct bkey_s_c k)
06dcca51 979{
971a1503
KO
980 if (w->last_pos.inode != k.k->p.inode) {
981 int ret = get_inodes_all_snapshots(trans, w, k.k->p.inode);
f9f52bc4
KO
982 if (ret)
983 return ERR_PTR(ret);
f9f52bc4
KO
984 }
985
971a1503 986 w->last_pos = k.k->p;
06dcca51 987
0afdf496 988 return lookup_inode_for_snapshot(trans, w, k);
06dcca51
KO
989}
990
bc6d2d10
KO
991/*
992 * Prefer to delete the first one, since that will be the one at the wrong
993 * offset:
994 * return value: 0 -> delete k1, 1 -> delete k2
995 */
d4c9fc00
KO
996int bch2_fsck_update_backpointers(struct btree_trans *trans,
997 struct snapshots_seen *s,
998 const struct bch_hash_desc desc,
999 struct bch_hash_info *hash_info,
1000 struct bkey_i *new)
bc6d2d10
KO
1001{
1002 if (new->k.type != KEY_TYPE_dirent)
1003 return 0;
1004
1005 struct bkey_i_dirent *d = bkey_i_to_dirent(new);
1006 struct inode_walker target = inode_walker_init();
1007 int ret = 0;
1008
1009 if (d->v.d_type == DT_SUBVOL) {
165815c2
KO
1010 bch_err(trans->c, "%s does not support DT_SUBVOL", __func__);
1011 ret = -BCH_ERR_fsck_repair_unimplemented;
bc6d2d10
KO
1012 } else {
1013 ret = get_visible_inodes(trans, &target, s, le64_to_cpu(d->v.d_inum));
1014 if (ret)
1015 goto err;
1016
1017 darray_for_each(target.inodes, i) {
1018 i->inode.bi_dir_offset = d->k.p.offset;
1019 ret = __bch2_fsck_write_inode(trans, &i->inode);
1020 if (ret)
1021 goto err;
1022 }
1023 }
1024err:
1025 inode_walker_exit(&target);
1026 return ret;
1027}
1028
0b17618f
KO
1029static struct bkey_s_c_dirent inode_get_dirent(struct btree_trans *trans,
1030 struct btree_iter *iter,
1031 struct bch_inode_unpacked *inode,
1032 u32 *snapshot)
1033{
1034 if (inode->bi_subvol) {
1035 u64 inum;
1036 int ret = subvol_lookup(trans, inode->bi_parent_subvol, snapshot, &inum);
1037 if (ret)
1038 return ((struct bkey_s_c_dirent) { .k = ERR_PTR(ret) });
1039 }
1040
1041 return dirent_get_by_pos(trans, iter, SPOS(inode->bi_dir, inode->bi_dir_offset, *snapshot));
1042}
1043
359d1bad
KO
1044static int check_inode_deleted_list(struct btree_trans *trans, struct bpos p)
1045{
1046 struct btree_iter iter;
1047 struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_deleted_inodes, p, 0);
52f3a72f 1048 int ret = bkey_err(k) ?: k.k->type == KEY_TYPE_set;
359d1bad 1049 bch2_trans_iter_exit(trans, &iter);
52f3a72f 1050 return ret;
359d1bad
KO
1051}
1052
a6508079 1053static int check_inode_dirent_inode(struct btree_trans *trans,
0b17618f 1054 struct bch_inode_unpacked *inode,
a6508079 1055 bool *write_inode)
0b17618f
KO
1056{
1057 struct bch_fs *c = trans->c;
1058 struct printbuf buf = PRINTBUF;
1059
a6508079 1060 u32 inode_snapshot = inode->bi_snapshot;
0b17618f
KO
1061 struct btree_iter dirent_iter = {};
1062 struct bkey_s_c_dirent d = inode_get_dirent(trans, &dirent_iter, inode, &inode_snapshot);
1063 int ret = bkey_err(d);
1064 if (ret && !bch2_err_matches(ret, ENOENT))
1065 return ret;
1066
09b9c72b 1067 if ((ret || dirent_points_to_inode_nowarn(c, d, inode)) &&
261592ba
KO
1068 inode->bi_subvol &&
1069 (inode->bi_flags & BCH_INODE_has_child_snapshot)) {
1070 /* Older version of a renamed subvolume root: we won't have a
1071 * correct dirent for it. That's expected, see
1072 * inode_should_reattach().
1073 *
1074 * We don't clear the backpointer field when doing the rename
1075 * because there might be arbitrarily many versions in older
1076 * snapshots.
1077 */
1078 inode->bi_dir = 0;
1079 inode->bi_dir_offset = 0;
1080 *write_inode = true;
1081 goto out;
1082 }
1083
0b17618f 1084 if (fsck_err_on(ret,
a850bde6 1085 trans, inode_points_to_missing_dirent,
0b17618f 1086 "inode points to missing dirent\n%s",
a6508079 1087 (bch2_inode_unpacked_to_text(&buf, inode), buf.buf)) ||
09b9c72b 1088 fsck_err_on(!ret && dirent_points_to_inode_nowarn(c, d, inode),
a850bde6 1089 trans, inode_points_to_wrong_dirent,
a6508079
KO
1090 "%s",
1091 (printbuf_reset(&buf),
1092 dirent_inode_mismatch_msg(&buf, c, d, inode),
1093 buf.buf))) {
0b17618f
KO
1094 /*
1095 * We just clear the backpointer fields for now. If we find a
1096 * dirent that points to this inode in check_dirents(), we'll
1097 * update it then; then when we get to check_path() if the
1098 * backpointer is still 0 we'll reattach it.
1099 */
1100 inode->bi_dir = 0;
1101 inode->bi_dir_offset = 0;
0b17618f
KO
1102 *write_inode = true;
1103 }
261592ba 1104out:
0b17618f
KO
1105 ret = 0;
1106fsck_err:
1107 bch2_trans_iter_exit(trans, &dirent_iter);
1108 printbuf_exit(&buf);
1109 bch_err_fn(c, ret);
1110 return ret;
1111}
1112
5c16add5
KO
1113static int check_inode(struct btree_trans *trans,
1114 struct btree_iter *iter,
a1783320 1115 struct bkey_s_c k,
15a3836c 1116 struct bch_inode_unpacked *snapshot_root,
cba31b7e 1117 struct snapshots_seen *s)
5c16add5
KO
1118{
1119 struct bch_fs *c = trans->c;
c7da5ee2 1120 struct printbuf buf = PRINTBUF;
285b181a 1121 struct bch_inode_unpacked u;
5c16add5 1122 bool do_update = false;
285b181a
KO
1123 int ret;
1124
08f50005 1125 ret = bch2_check_key_has_snapshot(trans, iter, k);
e492e7b6
KO
1126 if (ret < 0)
1127 goto err;
285b181a 1128 if (ret)
e492e7b6 1129 return 0;
285b181a 1130
49124d8a
KO
1131 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1132 if (ret)
1133 goto err;
1134
3e52c222 1135 if (!bkey_is_inode(k.k))
285b181a
KO
1136 return 0;
1137
644457ed
KO
1138 ret = bch2_inode_unpack(k, &u);
1139 if (ret)
1140 goto err;
285b181a 1141
15a3836c 1142 if (snapshot_root->bi_inum != u.bi_inum) {
123d2d09 1143 ret = bch2_inode_find_snapshot_root(trans, u.bi_inum, snapshot_root);
15a3836c
KO
1144 if (ret)
1145 goto err;
1146 }
285b181a 1147
fdd0807f
KO
1148 if (u.bi_hash_seed != snapshot_root->bi_hash_seed ||
1149 INODE_STR_HASH(&u) != INODE_STR_HASH(snapshot_root)) {
1150 ret = bch2_repair_inode_hash_info(trans, snapshot_root);
1151 BUG_ON(ret == -BCH_ERR_fsck_repair_unimplemented);
1152 if (ret)
1153 goto err;
c7da5ee2
KO
1154 }
1155
4ba99dde
KO
1156 ret = bch2_check_inode_has_case_insensitive(trans, &u, &s->ids, &do_update);
1157 if (ret)
1158 goto err;
1159
bbc3a0b1 1160 if (bch2_inode_has_backpointer(&u)) {
c7da5ee2
KO
1161 ret = check_inode_dirent_inode(trans, &u, &do_update);
1162 if (ret)
1163 goto err;
1164 }
1165
bbc3a0b1
KO
1166 if (fsck_err_on(bch2_inode_has_backpointer(&u) &&
1167 (u.bi_flags & BCH_INODE_unlinked),
c7da5ee2
KO
1168 trans, inode_unlinked_but_has_dirent,
1169 "inode unlinked but has dirent\n%s",
1170 (printbuf_reset(&buf),
1171 bch2_inode_unpacked_to_text(&buf, &u),
1172 buf.buf))) {
1173 u.bi_flags &= ~BCH_INODE_unlinked;
1174 do_update = true;
ef1669ff 1175 }
5c16add5 1176
c9306a91
KO
1177 if (S_ISDIR(u.bi_mode) && (u.bi_flags & BCH_INODE_unlinked)) {
1178 /* Check for this early so that check_unreachable_inode() will reattach it */
1179
1180 ret = bch2_empty_dir_snapshot(trans, k.k->p.offset, 0, k.k->p.snapshot);
1181 if (ret && ret != -BCH_ERR_ENOTEMPTY_dir_not_empty)
1182 goto err;
1183
1184 fsck_err_on(ret, trans, inode_dir_unlinked_but_not_empty,
1185 "dir unlinked but not empty\n%s",
1186 (printbuf_reset(&buf),
1187 bch2_inode_unpacked_to_text(&buf, &u),
1188 buf.buf));
1189 u.bi_flags &= ~BCH_INODE_unlinked;
1190 do_update = true;
1191 ret = 0;
1192 }
1193
36a2fdf7
KO
1194 if (fsck_err_on(S_ISDIR(u.bi_mode) && u.bi_size,
1195 trans, inode_dir_has_nonzero_i_size,
1196 "directory %llu:%u with nonzero i_size %lli",
1197 u.bi_inum, u.bi_snapshot, u.bi_size)) {
1198 u.bi_size = 0;
1199 do_update = true;
1200 }
1201
9b23fdbd
KO
1202 ret = bch2_inode_has_child_snapshots(trans, k.k->p);
1203 if (ret < 0)
1204 goto err;
69c8e6ce 1205
9b23fdbd
KO
1206 if (fsck_err_on(ret != !!(u.bi_flags & BCH_INODE_has_child_snapshot),
1207 trans, inode_has_child_snapshots_wrong,
1208 "inode has_child_snapshots flag wrong (should be %u)\n%s",
1209 ret,
1210 (printbuf_reset(&buf),
1211 bch2_inode_unpacked_to_text(&buf, &u),
1212 buf.buf))) {
d2a990d1 1213 if (ret)
9b23fdbd
KO
1214 u.bi_flags |= BCH_INODE_has_child_snapshot;
1215 else
1216 u.bi_flags &= ~BCH_INODE_has_child_snapshot;
1217 do_update = true;
a111901f 1218 }
9b23fdbd 1219 ret = 0;
a111901f 1220
9b23fdbd
KO
1221 if ((u.bi_flags & BCH_INODE_unlinked) &&
1222 !(u.bi_flags & BCH_INODE_has_child_snapshot)) {
fd65378d
KO
1223 if (!test_bit(BCH_FS_started, &c->flags)) {
1224 /*
1225 * If we're not in online fsck, don't delete unlinked
1226 * inodes, just make sure they're on the deleted list.
1227 *
1228 * They might be referred to by a logged operation -
1229 * i.e. we might have crashed in the middle of a
1230 * truncate on an unlinked but open file - so we want to
1231 * let the delete_dead_inodes kill it after resuming
1232 * logged ops.
1233 */
1234 ret = check_inode_deleted_list(trans, k.k->p);
1235 if (ret < 0)
c7da5ee2 1236 goto err_noprint;
fd65378d
KO
1237
1238 fsck_err_on(!ret,
1239 trans, unlinked_inode_not_on_deleted_list,
1240 "inode %llu:%u unlinked, but not on deleted list",
1241 u.bi_inum, k.k->p.snapshot);
1242
1243 ret = bch2_btree_bit_mod_buffered(trans, BTREE_ID_deleted_inodes, k.k->p, 1);
1244 if (ret)
1245 goto err;
1246 } else {
9d861787
KO
1247 ret = bch2_inode_or_descendents_is_open(trans, k.k->p);
1248 if (ret < 0)
1249 goto err;
1250
1251 if (fsck_err_on(!ret,
fd65378d 1252 trans, inode_unlinked_and_not_open,
4746ee18 1253 "inode %llu:%u unlinked and not open",
fd65378d
KO
1254 u.bi_inum, u.bi_snapshot)) {
1255 ret = bch2_inode_rm_snapshot(trans, u.bi_inum, iter->pos.snapshot);
1256 bch_err_msg(c, ret, "in fsck deleting inode");
c7da5ee2 1257 goto err_noprint;
fd65378d 1258 }
9d861787 1259 ret = 0;
fd65378d 1260 }
5c16add5
KO
1261 }
1262
0b498a5a
KO
1263 if (fsck_err_on(u.bi_parent_subvol &&
1264 (u.bi_subvol == 0 ||
1265 u.bi_subvol == BCACHEFS_ROOT_SUBVOL),
a850bde6 1266 trans, inode_bi_parent_nonzero,
0b498a5a
KO
1267 "inode %llu:%u has subvol %u but nonzero parent subvol %u",
1268 u.bi_inum, k.k->p.snapshot, u.bi_subvol, u.bi_parent_subvol)) {
1269 u.bi_parent_subvol = 0;
1270 do_update = true;
1271 }
1272
f2b02d09
KO
1273 if (u.bi_subvol) {
1274 struct bch_subvolume s;
1275
ce701571 1276 ret = bch2_subvolume_get(trans, u.bi_subvol, false, &s);
f2b02d09
KO
1277 if (ret && !bch2_err_matches(ret, ENOENT))
1278 goto err;
1279
cc053290
KO
1280 if (ret && (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_subvolumes))) {
1281 ret = reconstruct_subvol(trans, k.k->p.snapshot, u.bi_subvol, u.bi_inum);
1282 goto do_update;
1283 }
1284
f2b02d09 1285 if (fsck_err_on(ret,
a850bde6 1286 trans, inode_bi_subvol_missing,
f2b02d09
KO
1287 "inode %llu:%u bi_subvol points to missing subvolume %u",
1288 u.bi_inum, k.k->p.snapshot, u.bi_subvol) ||
1289 fsck_err_on(le64_to_cpu(s.inode) != u.bi_inum ||
1290 !bch2_snapshot_is_ancestor(c, le32_to_cpu(s.snapshot),
1291 k.k->p.snapshot),
a850bde6 1292 trans, inode_bi_subvol_wrong,
f2b02d09
KO
1293 "inode %llu:%u points to subvol %u, but subvol points to %llu:%u",
1294 u.bi_inum, k.k->p.snapshot, u.bi_subvol,
1295 le64_to_cpu(s.inode),
1296 le32_to_cpu(s.snapshot))) {
1297 u.bi_subvol = 0;
1298 u.bi_parent_subvol = 0;
1299 do_update = true;
1300 }
1301 }
4746ee18
KO
1302
1303 if (fsck_err_on(u.bi_journal_seq > journal_cur_seq(&c->journal),
1304 trans, inode_journal_seq_in_future,
1305 "inode journal seq in future (currently at %llu)\n%s",
1306 journal_cur_seq(&c->journal),
1307 (printbuf_reset(&buf),
1308 bch2_inode_unpacked_to_text(&buf, &u),
1309 buf.buf))) {
1310 u.bi_journal_seq = journal_cur_seq(&c->journal);
1311 do_update = true;
1312 }
cc053290 1313do_update:
5c16add5 1314 if (do_update) {
72350ee0 1315 ret = __bch2_fsck_write_inode(trans, &u);
d2a990d1 1316 bch_err_msg(c, ret, "in fsck updating inode");
a190cbcf 1317 if (ret)
c7da5ee2 1318 goto err_noprint;
5c16add5 1319 }
e492e7b6 1320err:
5c16add5 1321fsck_err:
d2a990d1 1322 bch_err_fn(c, ret);
c7da5ee2
KO
1323err_noprint:
1324 printbuf_exit(&buf);
5c16add5
KO
1325 return ret;
1326}
1327
067d228b 1328int bch2_check_inodes(struct bch_fs *c)
5c16add5 1329{
15a3836c 1330 struct bch_inode_unpacked snapshot_root = {};
49124d8a 1331 struct snapshots_seen s;
5c16add5 1332
49124d8a 1333 snapshots_seen_init(&s);
5c16add5 1334
4eb3877e
KO
1335 int ret = bch2_trans_run(c,
1336 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
1337 POS_MIN,
5dd8c60e 1338 BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
4eb3877e 1339 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
15a3836c 1340 check_inode(trans, &iter, k, &snapshot_root, &s)));
ef1669ff 1341
49124d8a 1342 snapshots_seen_exit(&s);
d2a990d1 1343 bch_err_fn(c, ret);
285b181a
KO
1344 return ret;
1345}
ef1669ff 1346
38864ecc
KO
1347static int find_oldest_inode_needs_reattach(struct btree_trans *trans,
1348 struct bch_inode_unpacked *inode)
1349{
1350 struct bch_fs *c = trans->c;
1351 struct btree_iter iter;
1352 struct bkey_s_c k;
1353 int ret = 0;
1354
1355 /*
1356 * We look for inodes to reattach in natural key order, leaves first,
1357 * but we should do the reattach at the oldest version that needs to be
1358 * reattached:
1359 */
1360 for_each_btree_key_norestart(trans, iter,
1361 BTREE_ID_inodes,
1362 SPOS(0, inode->bi_inum, inode->bi_snapshot + 1),
1363 BTREE_ITER_all_snapshots, k, ret) {
1364 if (k.k->p.offset != inode->bi_inum)
1365 break;
1366
1367 if (!bch2_snapshot_is_ancestor(c, inode->bi_snapshot, k.k->p.snapshot))
1368 continue;
1369
1370 if (!bkey_is_inode(k.k))
1371 break;
1372
1373 struct bch_inode_unpacked parent_inode;
644457ed
KO
1374 ret = bch2_inode_unpack(k, &parent_inode);
1375 if (ret)
1376 break;
38864ecc
KO
1377
1378 if (!inode_should_reattach(&parent_inode))
1379 break;
1380
1381 *inode = parent_inode;
1382 }
1383 bch2_trans_iter_exit(trans, &iter);
1384
1385 return ret;
1386}
1387
bade9711
KO
1388static int check_unreachable_inode(struct btree_trans *trans,
1389 struct btree_iter *iter,
1390 struct bkey_s_c k)
1391{
bade9711
KO
1392 struct printbuf buf = PRINTBUF;
1393 int ret = 0;
1394
1395 if (!bkey_is_inode(k.k))
1396 return 0;
1397
1398 struct bch_inode_unpacked inode;
644457ed
KO
1399 ret = bch2_inode_unpack(k, &inode);
1400 if (ret)
1401 return ret;
bade9711 1402
38864ecc 1403 if (!inode_should_reattach(&inode))
bade9711
KO
1404 return 0;
1405
38864ecc
KO
1406 ret = find_oldest_inode_needs_reattach(trans, &inode);
1407 if (ret)
1408 return ret;
bade9711 1409
38864ecc
KO
1410 if (fsck_err(trans, inode_unreachable,
1411 "unreachable inode:\n%s",
1412 (bch2_inode_unpacked_to_text(&buf, &inode),
1413 buf.buf)))
bade9711
KO
1414 ret = reattach_inode(trans, &inode);
1415fsck_err:
1416 printbuf_exit(&buf);
1417 return ret;
1418}
1419
1420/*
1421 * Reattach unreachable (but not unlinked) inodes
1422 *
1423 * Run after check_inodes() and check_dirents(), so we node that inode
1424 * backpointer fields point to valid dirents, and every inode that has a dirent
1425 * that points to it has its backpointer field set - so we're just looking for
1426 * non-unlinked inodes without backpointers:
38864ecc
KO
1427 *
1428 * XXX: this is racy w.r.t. hardlink removal in online fsck
bade9711
KO
1429 */
1430int bch2_check_unreachable_inodes(struct bch_fs *c)
1431{
1432 int ret = bch2_trans_run(c,
1433 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
1434 POS_MIN,
1435 BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
1436 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1437 check_unreachable_inode(trans, &iter, k)));
1438 bch_err_fn(c, ret);
1439 return ret;
1440}
1441
c13d526d
KO
1442static inline bool btree_matches_i_mode(enum btree_id btree, unsigned mode)
1443{
1444 switch (btree) {
1445 case BTREE_ID_extents:
1446 return S_ISREG(mode) || S_ISLNK(mode);
1447 case BTREE_ID_dirents:
1448 return S_ISDIR(mode);
1449 case BTREE_ID_xattrs:
1450 return true;
1451 default:
1452 BUG();
1453 }
1454}
1455
1456static int check_key_has_inode(struct btree_trans *trans,
1457 struct btree_iter *iter,
1458 struct inode_walker *inode,
1459 struct inode_walker_entry *i,
1460 struct bkey_s_c k)
1461{
1462 struct bch_fs *c = trans->c;
1463 struct printbuf buf = PRINTBUF;
f2a701fd 1464 struct btree_iter iter2 = {};
c13d526d
KO
1465 int ret = PTR_ERR_OR_ZERO(i);
1466 if (ret)
1467 return ret;
1468
1469 if (k.k->type == KEY_TYPE_whiteout)
1470 goto out;
1471
855070dc
KO
1472 bool have_inode = i && !i->whiteout;
1473
f2a701fd
KO
1474 if (!have_inode && (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_inodes)))
1475 goto reconstruct;
c13d526d 1476
f2a701fd
KO
1477 if (have_inode && btree_matches_i_mode(iter->btree_id, i->inode.bi_mode))
1478 goto out;
1479
1480 prt_printf(&buf, ", ");
1481
1482 bool have_old_inode = false;
1483 darray_for_each(inode->inodes, i2)
1484 if (!i2->whiteout &&
1485 bch2_snapshot_is_ancestor(c, k.k->p.snapshot, i2->inode.bi_snapshot) &&
1486 btree_matches_i_mode(iter->btree_id, i2->inode.bi_mode)) {
1487 prt_printf(&buf, "but found good inode in older snapshot\n");
1488 bch2_inode_unpacked_to_text(&buf, &i2->inode);
1489 prt_newline(&buf);
1490 have_old_inode = true;
1491 break;
1492 }
1493
1494 struct bkey_s_c k2;
1495 unsigned nr_keys = 0;
1496
1497 prt_printf(&buf, "found keys:\n");
1498
1499 for_each_btree_key_max_norestart(trans, iter2, iter->btree_id,
1500 SPOS(k.k->p.inode, 0, k.k->p.snapshot),
1501 POS(k.k->p.inode, U64_MAX),
1502 0, k2, ret) {
1503 nr_keys++;
1504 if (nr_keys <= 10) {
1505 bch2_bkey_val_to_text(&buf, c, k2);
1506 prt_newline(&buf);
1507 }
1508 if (nr_keys >= 100)
1509 break;
c13d526d
KO
1510 }
1511
f2a701fd
KO
1512 if (ret)
1513 goto err;
c13d526d 1514
f2a701fd
KO
1515 if (nr_keys > 100)
1516 prt_printf(&buf, "found > %u keys for this missing inode\n", nr_keys);
1517 else if (nr_keys > 10)
1518 prt_printf(&buf, "found %u keys for this missing inode\n", nr_keys);
1519
1520 if (!have_inode) {
1521 if (fsck_err_on(!have_inode,
1522 trans, key_in_missing_inode,
1523 "key in missing inode%s", buf.buf)) {
1524 /*
1525 * Maybe a deletion that raced with data move, or something
1526 * weird like that? But if we know the inode was deleted, or
1527 * it's just a few keys, we can safely delete them.
1528 *
1529 * If it's many keys, we should probably recreate the inode
1530 */
1531 if (have_old_inode || nr_keys <= 2)
1532 goto delete;
1533 else
1534 goto reconstruct;
1535 }
1536 } else {
1537 /*
1538 * not autofix, this one would be a giant wtf - bit error in the
1539 * inode corrupting i_mode?
1540 *
1541 * may want to try repairing inode instead of deleting
1542 */
1543 if (fsck_err_on(!btree_matches_i_mode(iter->btree_id, i->inode.bi_mode),
1544 trans, key_in_wrong_inode_type,
1545 "key for wrong inode mode %o%s",
1546 i->inode.bi_mode, buf.buf))
1547 goto delete;
1548 }
c13d526d
KO
1549out:
1550err:
1551fsck_err:
f2a701fd 1552 bch2_trans_iter_exit(trans, &iter2);
c13d526d
KO
1553 printbuf_exit(&buf);
1554 bch_err_fn(c, ret);
1555 return ret;
1556delete:
f2a701fd
KO
1557 /*
1558 * XXX: print out more info
1559 * count up extents for this inode, check if we have different inode in
1560 * an older snapshot version, perhaps decide if we want to reconstitute
1561 */
c13d526d
KO
1562 ret = bch2_btree_delete_at(trans, iter, BTREE_UPDATE_internal_snapshot_node);
1563 goto out;
f2a701fd
KO
1564reconstruct:
1565 ret = reconstruct_inode(trans, iter->btree_id, k.k->p.snapshot, k.k->p.inode) ?:
1566 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
1567 if (ret)
1568 goto err;
1569
1570 inode->last_pos.inode--;
1571 ret = bch_err_throw(c, transaction_restart_nested);
1572 goto out;
c13d526d
KO
1573}
1574
109ea419 1575static int check_i_sectors_notnested(struct btree_trans *trans, struct inode_walker *w)
ef1669ff
KO
1576{
1577 struct bch_fs *c = trans->c;
0763c552 1578 int ret = 0;
ef1669ff
KO
1579 s64 count2;
1580
91d961ba 1581 darray_for_each(w->inodes, i) {
ef1669ff
KO
1582 if (i->inode.bi_sectors == i->count)
1583 continue;
1584
6f2bbd57 1585 count2 = bch2_count_inode_sectors(trans, w->last_pos.inode, i->inode.bi_snapshot);
ef1669ff 1586
43b81a4e 1587 if (w->recalculate_sums)
ef1669ff 1588 i->count = count2;
43b81a4e
KO
1589
1590 if (i->count != count2) {
fa14b504 1591 bch_err_ratelimited(c, "fsck counted i_sectors wrong for inode %llu:%u: got %llu should be %llu",
6f2bbd57 1592 w->last_pos.inode, i->inode.bi_snapshot, i->count, count2);
7e5b8e00 1593 i->count = count2;
ef1669ff
KO
1594 }
1595
103ffe9a 1596 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_i_sectors_dirty),
a850bde6 1597 trans, inode_i_sectors_wrong,
b65db750 1598 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
6f2bbd57 1599 w->last_pos.inode, i->inode.bi_snapshot,
b65db750 1600 i->inode.bi_sectors, i->count)) {
1ed0a5d2 1601 i->inode.bi_sectors = i->count;
72350ee0 1602 ret = bch2_fsck_write_inode(trans, &i->inode);
1ed0a5d2
KO
1603 if (ret)
1604 break;
1ed0a5d2 1605 }
ef1669ff
KO
1606 }
1607fsck_err:
d2a990d1 1608 bch_err_fn(c, ret);
109ea419
KO
1609 return ret;
1610}
1611
1612static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1613{
1614 u32 restart_count = trans->restart_count;
1615 return check_i_sectors_notnested(trans, w) ?:
1616 trans_was_restarted(trans, restart_count);
ef1669ff
KO
1617}
1618
c58029ec
DH
1619struct extent_end {
1620 u32 snapshot;
1621 u64 offset;
1622 struct snapshots_seen seen;
1623};
1624
a397b8df
KO
1625struct extent_ends {
1626 struct bpos last_pos;
1627 DARRAY(struct extent_end) e;
1628};
1629
1630static void extent_ends_reset(struct extent_ends *extent_ends)
1631{
a397b8df
KO
1632 darray_for_each(extent_ends->e, i)
1633 snapshots_seen_exit(&i->seen);
a397b8df
KO
1634 extent_ends->e.nr = 0;
1635}
1636
1637static void extent_ends_exit(struct extent_ends *extent_ends)
1638{
1639 extent_ends_reset(extent_ends);
1640 darray_exit(&extent_ends->e);
1641}
1642
1643static void extent_ends_init(struct extent_ends *extent_ends)
1644{
1645 memset(extent_ends, 0, sizeof(*extent_ends));
1646}
1647
1648static int extent_ends_at(struct bch_fs *c,
1649 struct extent_ends *extent_ends,
1650 struct snapshots_seen *seen,
1651 struct bkey_s_c k)
1652{
1653 struct extent_end *i, n = (struct extent_end) {
1654 .offset = k.k->p.offset,
1655 .snapshot = k.k->p.snapshot,
1656 .seen = *seen,
1657 };
1658
1659 n.seen.ids.data = kmemdup(seen->ids.data,
1660 sizeof(seen->ids.data[0]) * seen->ids.size,
1661 GFP_KERNEL);
1662 if (!n.seen.ids.data)
09b9c72b 1663 return bch_err_throw(c, ENOMEM_fsck_extent_ends_at);
a397b8df 1664
defd9e39 1665 __darray_for_each(extent_ends->e, i) {
a397b8df
KO
1666 if (i->snapshot == k.k->p.snapshot) {
1667 snapshots_seen_exit(&i->seen);
1668 *i = n;
1669 return 0;
1670 }
1671
1672 if (i->snapshot >= k.k->p.snapshot)
1673 break;
1674 }
1675
1676 return darray_insert_item(&extent_ends->e, i - extent_ends->e.data, n);
1677}
c58029ec 1678
43b81a4e
KO
1679static int overlapping_extents_found(struct btree_trans *trans,
1680 enum btree_id btree,
e2bd0617
KO
1681 struct bpos pos1, struct snapshots_seen *pos1_seen,
1682 struct bkey pos2,
1683 bool *fixed,
1684 struct extent_end *extent_end)
454377d8 1685{
43b81a4e
KO
1686 struct bch_fs *c = trans->c;
1687 struct printbuf buf = PRINTBUF;
9180ad2e 1688 struct btree_iter iter1, iter2 = {};
e2bd0617 1689 struct bkey_s_c k1, k2;
454377d8
KO
1690 int ret;
1691
43b81a4e
KO
1692 BUG_ON(bkey_le(pos1, bkey_start_pos(&pos2)));
1693
e2bd0617 1694 bch2_trans_iter_init(trans, &iter1, btree, pos1,
5dd8c60e
KO
1695 BTREE_ITER_all_snapshots|
1696 BTREE_ITER_not_extents);
9180ad2e 1697 k1 = bch2_btree_iter_peek_max(trans, &iter1, POS(pos1.inode, U64_MAX));
e2bd0617 1698 ret = bkey_err(k1);
454377d8 1699 if (ret)
43b81a4e
KO
1700 goto err;
1701
1ece5323 1702 prt_newline(&buf);
e2bd0617 1703 bch2_bkey_val_to_text(&buf, c, k1);
43b81a4e 1704
e2bd0617 1705 if (!bpos_eq(pos1, k1.k->p)) {
1ece5323 1706 prt_str(&buf, "\nwanted\n ");
e2bd0617 1707 bch2_bpos_to_text(&buf, pos1);
1ece5323 1708 prt_str(&buf, "\n");
e2bd0617
KO
1709 bch2_bkey_to_text(&buf, &pos2);
1710
1711 bch_err(c, "%s: error finding first overlapping extent when repairing, got%s",
43b81a4e 1712 __func__, buf.buf);
09b9c72b 1713 ret = bch_err_throw(c, internal_fsck_err);
43b81a4e
KO
1714 goto err;
1715 }
1716
9180ad2e 1717 bch2_trans_copy_iter(trans, &iter2, &iter1);
e2bd0617 1718
43b81a4e 1719 while (1) {
9180ad2e 1720 bch2_btree_iter_advance(trans, &iter2);
43b81a4e 1721
9180ad2e 1722 k2 = bch2_btree_iter_peek_max(trans, &iter2, POS(pos1.inode, U64_MAX));
e2bd0617 1723 ret = bkey_err(k2);
43b81a4e
KO
1724 if (ret)
1725 goto err;
1726
e2bd0617 1727 if (bpos_ge(k2.k->p, pos2.p))
43b81a4e 1728 break;
43b81a4e
KO
1729 }
1730
1ece5323 1731 prt_newline(&buf);
e2bd0617 1732 bch2_bkey_val_to_text(&buf, c, k2);
43b81a4e 1733
e2bd0617
KO
1734 if (bpos_gt(k2.k->p, pos2.p) ||
1735 pos2.size != k2.k->size) {
43b81a4e
KO
1736 bch_err(c, "%s: error finding seconding overlapping extent when repairing%s",
1737 __func__, buf.buf);
09b9c72b 1738 ret = bch_err_throw(c, internal_fsck_err);
43b81a4e
KO
1739 goto err;
1740 }
1741
1ece5323 1742 prt_printf(&buf, "\noverwriting %s extent",
e2bd0617
KO
1743 pos1.snapshot >= pos2.p.snapshot ? "first" : "second");
1744
a850bde6 1745 if (fsck_err(trans, extent_overlapping,
b65db750 1746 "overlapping extents%s", buf.buf)) {
e2bd0617
KO
1747 struct btree_iter *old_iter = &iter1;
1748 struct disk_reservation res = { 0 };
454377d8 1749
e2bd0617
KO
1750 if (pos1.snapshot < pos2.p.snapshot) {
1751 old_iter = &iter2;
1752 swap(k1, k2);
1753 }
1754
6474b706 1755 trans->extra_disk_res += bch2_bkey_sectors_compressed(k2);
e2bd0617
KO
1756
1757 ret = bch2_trans_update_extent_overwrite(trans, old_iter,
5dd8c60e 1758 BTREE_UPDATE_internal_snapshot_node,
e2bd0617 1759 k1, k2) ?:
3f0e297d 1760 bch2_trans_commit(trans, &res, NULL, BCH_TRANS_COMMIT_no_enospc);
e2bd0617
KO
1761 bch2_disk_reservation_put(c, &res);
1762
7337f9f1
KO
1763 bch_info(c, "repair ret %s", bch2_err_str(ret));
1764
e2bd0617 1765 if (ret)
43b81a4e
KO
1766 goto err;
1767
1768 *fixed = true;
e2bd0617
KO
1769
1770 if (pos1.snapshot == pos2.p.snapshot) {
1771 /*
1772 * We overwrote the first extent, and did the overwrite
1773 * in the same snapshot:
1774 */
1775 extent_end->offset = bkey_start_offset(&pos2);
1776 } else if (pos1.snapshot > pos2.p.snapshot) {
1777 /*
1778 * We overwrote the first extent in pos2's snapshot:
1779 */
1780 ret = snapshots_seen_add_inorder(c, pos1_seen, pos2.p.snapshot);
1781 } else {
1782 /*
1783 * We overwrote the second extent - restart
1784 * check_extent() from the top:
1785 */
09b9c72b 1786 ret = bch_err_throw(c, transaction_restart_nested);
e2bd0617 1787 }
43b81a4e
KO
1788 }
1789fsck_err:
1790err:
e2bd0617
KO
1791 bch2_trans_iter_exit(trans, &iter2);
1792 bch2_trans_iter_exit(trans, &iter1);
43b81a4e
KO
1793 printbuf_exit(&buf);
1794 return ret;
454377d8
KO
1795}
1796
c58029ec
DH
1797static int check_overlapping_extents(struct btree_trans *trans,
1798 struct snapshots_seen *seen,
a397b8df 1799 struct extent_ends *extent_ends,
c58029ec 1800 struct bkey_s_c k,
e2bd0617
KO
1801 struct btree_iter *iter,
1802 bool *fixed)
c58029ec
DH
1803{
1804 struct bch_fs *c = trans->c;
c58029ec
DH
1805 int ret = 0;
1806
a397b8df
KO
1807 /* transaction restart, running again */
1808 if (bpos_eq(extent_ends->last_pos, k.k->p))
1809 return 0;
1810
1811 if (extent_ends->last_pos.inode != k.k->p.inode)
1812 extent_ends_reset(extent_ends);
1813
1814 darray_for_each(extent_ends->e, i) {
1815 if (i->offset <= bkey_start_offset(k.k))
c58029ec
DH
1816 continue;
1817
1818 if (!ref_visible2(c,
1819 k.k->p.snapshot, seen,
1820 i->snapshot, &i->seen))
1821 continue;
1822
a397b8df
KO
1823 ret = overlapping_extents_found(trans, iter->btree_id,
1824 SPOS(iter->pos.inode,
1825 i->offset,
1826 i->snapshot),
e2bd0617
KO
1827 &i->seen,
1828 *k.k, fixed, i);
a397b8df
KO
1829 if (ret)
1830 goto err;
c58029ec
DH
1831 }
1832
a397b8df
KO
1833 extent_ends->last_pos = k.k->p;
1834err:
e2bd0617 1835 return ret;
c58029ec
DH
1836}
1837
9db2f860
KO
1838static int check_extent_overbig(struct btree_trans *trans, struct btree_iter *iter,
1839 struct bkey_s_c k)
1840{
1841 struct bch_fs *c = trans->c;
1842 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1843 struct bch_extent_crc_unpacked crc;
1844 const union bch_extent_entry *i;
1845 unsigned encoded_extent_max_sectors = c->opts.encoded_extent_max >> 9;
1846
1847 bkey_for_each_crc(k.k, ptrs, crc, i)
1848 if (crc_is_encoded(crc) &&
1849 crc.uncompressed_size > encoded_extent_max_sectors) {
1850 struct printbuf buf = PRINTBUF;
1851
1852 bch2_bkey_val_to_text(&buf, c, k);
1853 bch_err(c, "overbig encoded extent, please report this:\n %s", buf.buf);
1854 printbuf_exit(&buf);
1855 }
1856
1857 return 0;
1858}
1859
ef1669ff 1860static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
a1783320 1861 struct bkey_s_c k,
ef1669ff 1862 struct inode_walker *inode,
c58029ec 1863 struct snapshots_seen *s,
3672bda8
KO
1864 struct extent_ends *extent_ends,
1865 struct disk_reservation *res)
ef1669ff
KO
1866{
1867 struct bch_fs *c = trans->c;
fa8e94fa 1868 struct printbuf buf = PRINTBUF;
ef1669ff 1869 int ret = 0;
ef1669ff 1870
08f50005 1871 ret = bch2_check_key_has_snapshot(trans, iter, k);
fa8e94fa
KO
1872 if (ret) {
1873 ret = ret < 0 ? ret : 0;
1874 goto out;
1875 }
ef1669ff 1876
3672bda8 1877 if (inode->last_pos.inode != k.k->p.inode && inode->have_inodes) {
ef1669ff
KO
1878 ret = check_i_sectors(trans, inode);
1879 if (ret)
fa8e94fa 1880 goto err;
ef1669ff 1881 }
292dea86 1882
c13d526d
KO
1883 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1884 if (ret)
1885 goto err;
1886
3672bda8
KO
1887 struct inode_walker_entry *extent_i = walk_inode(trans, inode, k);
1888 ret = PTR_ERR_OR_ZERO(extent_i);
a57f4d61
KO
1889 if (ret)
1890 goto err;
1891
3672bda8 1892 ret = check_key_has_inode(trans, iter, inode, extent_i, k);
c58029ec
DH
1893 if (ret)
1894 goto err;
ef1669ff 1895
650eb16b 1896 if (k.k->type != KEY_TYPE_whiteout) {
5a2d1521 1897 ret = check_overlapping_extents(trans, s, extent_ends, k, iter,
e2bd0617 1898 &inode->recalculate_sums);
650eb16b 1899 if (ret)
e2bd0617 1900 goto err;
ef1669ff 1901
3672bda8
KO
1902 /*
1903 * Check inodes in reverse order, from oldest snapshots to
1904 * newest, starting from the inode that matches this extent's
1905 * snapshot. If we didn't have one, iterate over all inodes:
1906 */
1907 for (struct inode_walker_entry *i = extent_i ?: &darray_last(inode->inodes);
1908 inode->inodes.data && i >= inode->inodes.data;
1909 --i) {
6f2bbd57
KO
1910 if (i->inode.bi_snapshot > k.k->p.snapshot ||
1911 !key_visible_in_snapshot(c, s, i->inode.bi_snapshot, k.k->p.snapshot))
3672bda8 1912 continue;
0d06b4ec 1913
19133440
KO
1914 u64 last_block = round_up(i->inode.bi_size, block_bytes(c)) >> 9;
1915
1916 if (fsck_err_on(k.k->p.offset > last_block &&
b65db750 1917 !bkey_extent_is_reservation(k),
a850bde6 1918 trans, extent_past_end_of_inode,
1ece5323 1919 "extent type past end of inode %llu:%u, i_size %llu\n%s",
6f2bbd57 1920 i->inode.bi_inum, i->inode.bi_snapshot, i->inode.bi_size,
650eb16b 1921 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
19133440
KO
1922 struct bkey_i *whiteout = bch2_trans_kmalloc(trans, sizeof(*whiteout));
1923 ret = PTR_ERR_OR_ZERO(whiteout);
1924 if (ret)
1925 goto err;
650eb16b 1926
19133440
KO
1927 bkey_init(&whiteout->k);
1928 whiteout->k.p = SPOS(k.k->p.inode,
1929 last_block,
1930 i->inode.bi_snapshot);
1931 bch2_key_resize(&whiteout->k,
1932 min(KEY_SIZE_MAX & (~0 << c->block_bits),
1933 U64_MAX - whiteout->k.p.offset));
1934
1935
1936 /*
1937 * Need a normal (not BTREE_ITER_all_snapshots)
1938 * iterator, if we're deleting in a different
1939 * snapshot and need to emit a whiteout
1940 */
1941 struct btree_iter iter2;
1942 bch2_trans_iter_init(trans, &iter2, BTREE_ID_extents,
1943 bkey_start_pos(&whiteout->k),
1944 BTREE_ITER_intent);
9180ad2e 1945 ret = bch2_btree_iter_traverse(trans, &iter2) ?:
19133440 1946 bch2_trans_update(trans, &iter2, whiteout,
5dd8c60e 1947 BTREE_UPDATE_internal_snapshot_node);
650eb16b 1948 bch2_trans_iter_exit(trans, &iter2);
0d06b4ec
KO
1949 if (ret)
1950 goto err;
650eb16b 1951
650eb16b 1952 iter->k.type = KEY_TYPE_whiteout;
3672bda8 1953 break;
ef1669ff 1954 }
3672bda8
KO
1955 }
1956 }
1957
1958 ret = bch2_trans_commit(trans, res, NULL, BCH_TRANS_COMMIT_no_enospc);
1959 if (ret)
1960 goto err;
a0076086 1961
3672bda8
KO
1962 if (bkey_extent_is_allocation(k.k)) {
1963 for (struct inode_walker_entry *i = extent_i ?: &darray_last(inode->inodes);
1964 inode->inodes.data && i >= inode->inodes.data;
1965 --i) {
855070dc
KO
1966 if (i->whiteout ||
1967 i->inode.bi_snapshot > k.k->p.snapshot ||
6f2bbd57 1968 !key_visible_in_snapshot(c, s, i->inode.bi_snapshot, k.k->p.snapshot))
3672bda8
KO
1969 continue;
1970
1971 i->count += k.k->size;
ef1669ff 1972 }
a0076086 1973 }
eab3a3ce
KO
1974
1975 if (k.k->type != KEY_TYPE_whiteout) {
1976 ret = extent_ends_at(c, extent_ends, s, k);
1977 if (ret)
1978 goto err;
1979 }
fa8e94fa
KO
1980out:
1981err:
ef1669ff 1982fsck_err:
fa8e94fa 1983 printbuf_exit(&buf);
d2a990d1 1984 bch_err_fn(c, ret);
8a85b20c
KO
1985 return ret;
1986}
1987
1c6fdbd8
KO
1988/*
1989 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1990 * that i_size an i_sectors are consistent
1991 */
067d228b 1992int bch2_check_extents(struct bch_fs *c)
1c6fdbd8
KO
1993{
1994 struct inode_walker w = inode_walker_init();
ef1669ff 1995 struct snapshots_seen s;
a397b8df 1996 struct extent_ends extent_ends;
e3dc75eb 1997 struct disk_reservation res = { 0 };
1c6fdbd8 1998
ef1669ff 1999 snapshots_seen_init(&s);
a397b8df 2000 extent_ends_init(&extent_ends);
424eb881 2001
4eb3877e 2002 int ret = bch2_trans_run(c,
3672bda8 2003 for_each_btree_key(trans, iter, BTREE_ID_extents,
4eb3877e 2004 POS(BCACHEFS_ROOT_INO, 0),
3672bda8 2005 BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k, ({
4eb3877e 2006 bch2_disk_reservation_put(c, &res);
3672bda8 2007 check_extent(trans, &iter, k, &w, &s, &extent_ends, &res) ?:
4eb3877e
KO
2008 check_extent_overbig(trans, &iter, k);
2009 })) ?:
109ea419 2010 check_i_sectors_notnested(trans, &w));
c58029ec 2011
e3dc75eb 2012 bch2_disk_reservation_put(c, &res);
a397b8df 2013 extent_ends_exit(&extent_ends);
ef1669ff 2014 inode_walker_exit(&w);
ef1669ff
KO
2015 snapshots_seen_exit(&s);
2016
d2a990d1 2017 bch_err_fn(c, ret);
ef1669ff
KO
2018 return ret;
2019}
2020
9db2f860
KO
2021int bch2_check_indirect_extents(struct bch_fs *c)
2022{
9db2f860 2023 struct disk_reservation res = { 0 };
9db2f860 2024
4eb3877e
KO
2025 int ret = bch2_trans_run(c,
2026 for_each_btree_key_commit(trans, iter, BTREE_ID_reflink,
2027 POS_MIN,
5dd8c60e 2028 BTREE_ITER_prefetch, k,
4eb3877e
KO
2029 &res, NULL,
2030 BCH_TRANS_COMMIT_no_enospc, ({
2031 bch2_disk_reservation_put(c, &res);
2032 check_extent_overbig(trans, &iter, k);
2033 })));
9db2f860
KO
2034
2035 bch2_disk_reservation_put(c, &res);
9db2f860
KO
2036 bch_err_fn(c, ret);
2037 return ret;
2038}
2039
109ea419 2040static int check_subdir_count_notnested(struct btree_trans *trans, struct inode_walker *w)
ef1669ff
KO
2041{
2042 struct bch_fs *c = trans->c;
0763c552 2043 int ret = 0;
ef1669ff
KO
2044 s64 count2;
2045
91d961ba 2046 darray_for_each(w->inodes, i) {
ef1669ff
KO
2047 if (i->inode.bi_nlink == i->count)
2048 continue;
2049
6f2bbd57 2050 count2 = bch2_count_subdirs(trans, w->last_pos.inode, i->inode.bi_snapshot);
9e343161
KO
2051 if (count2 < 0)
2052 return count2;
ef1669ff
KO
2053
2054 if (i->count != count2) {
fa14b504 2055 bch_err_ratelimited(c, "fsck counted subdirectories wrong for inum %llu:%u: got %llu should be %llu",
6f2bbd57 2056 w->last_pos.inode, i->inode.bi_snapshot, i->count, count2);
ef1669ff
KO
2057 i->count = count2;
2058 if (i->inode.bi_nlink == i->count)
2059 continue;
2060 }
2061
583ba52a
KO
2062 if (i->inode.bi_nlink != i->count) {
2063 CLASS(printbuf, buf)();
2064
2065 lockrestart_do(trans,
2066 bch2_inum_snapshot_to_path(trans, w->last_pos.inode,
2067 i->inode.bi_snapshot, NULL, &buf));
2068
2069 if (fsck_err_on(i->inode.bi_nlink != i->count,
2070 trans, inode_dir_wrong_nlink,
2071 "directory with wrong i_nlink: got %u, should be %llu\n%s",
2072 i->inode.bi_nlink, i->count, buf.buf)) {
2073 i->inode.bi_nlink = i->count;
2074 ret = bch2_fsck_write_inode(trans, &i->inode);
2075 if (ret)
2076 break;
2077 }
abcecb49 2078 }
ef1669ff
KO
2079 }
2080fsck_err:
d2a990d1 2081 bch_err_fn(c, ret);
109ea419
KO
2082 return ret;
2083}
2084
b9ddb3e1 2085static int check_subdir_dirents_count(struct btree_trans *trans, struct inode_walker *w)
109ea419
KO
2086{
2087 u32 restart_count = trans->restart_count;
2088 return check_subdir_count_notnested(trans, w) ?:
2089 trans_was_restarted(trans, restart_count);
ef1669ff 2090}
abcecb49 2091
c60b7f80
KO
2092/* find a subvolume that's a descendent of @snapshot: */
2093static int find_snapshot_subvol(struct btree_trans *trans, u32 snapshot, u32 *subvolid)
2094{
2095 struct btree_iter iter;
2096 struct bkey_s_c k;
2097 int ret;
2098
2099 for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN, 0, k, ret) {
2100 if (k.k->type != KEY_TYPE_subvolume)
2101 continue;
2102
2103 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
2104 if (bch2_snapshot_is_ancestor(trans->c, le32_to_cpu(s.v->snapshot), snapshot)) {
2105 bch2_trans_iter_exit(trans, &iter);
2106 *subvolid = k.k->p.offset;
2107 goto found;
2108 }
2109 }
2110 if (!ret)
2111 ret = -ENOENT;
2112found:
2113 bch2_trans_iter_exit(trans, &iter);
2114 return ret;
2115}
2116
dab18704 2117noinline_for_stack
ea27001e
KO
2118static int check_dirent_to_subvol(struct btree_trans *trans, struct btree_iter *iter,
2119 struct bkey_s_c_dirent d)
11def188
KO
2120{
2121 struct bch_fs *c = trans->c;
b8628a25 2122 struct btree_iter subvol_iter = {};
11def188 2123 struct bch_inode_unpacked subvol_root;
f4e68c85 2124 u32 parent_subvol = le32_to_cpu(d.v->d_parent_subvol);
11def188 2125 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
b8628a25 2126 u32 parent_snapshot;
cc053290 2127 u32 new_parent_subvol = 0;
b8628a25 2128 u64 parent_inum;
c60b7f80 2129 struct printbuf buf = PRINTBUF;
11def188
KO
2130 int ret = 0;
2131
c60b7f80
KO
2132 ret = subvol_lookup(trans, parent_subvol, &parent_snapshot, &parent_inum);
2133 if (ret && !bch2_err_matches(ret, ENOENT))
2134 return ret;
2135
cc053290
KO
2136 if (ret ||
2137 (!ret && !bch2_snapshot_is_ancestor(c, parent_snapshot, d.k->p.snapshot))) {
2138 int ret2 = find_snapshot_subvol(trans, d.k->p.snapshot, &new_parent_subvol);
2139 if (ret2 && !bch2_err_matches(ret, ENOENT))
2140 return ret2;
2141 }
2142
2143 if (ret &&
2144 !new_parent_subvol &&
2145 (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_subvolumes))) {
2146 /*
2147 * Couldn't find a subvol for dirent's snapshot - but we lost
2148 * subvols, so we need to reconstruct:
2149 */
2150 ret = reconstruct_subvol(trans, d.k->p.snapshot, parent_subvol, 0);
2151 if (ret)
2152 return ret;
2153
2154 parent_snapshot = d.k->p.snapshot;
2155 }
2156
a850bde6
KO
2157 if (fsck_err_on(ret,
2158 trans, dirent_to_missing_parent_subvol,
c60b7f80
KO
2159 "dirent parent_subvol points to missing subvolume\n%s",
2160 (bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf)) ||
2161 fsck_err_on(!ret && !bch2_snapshot_is_ancestor(c, parent_snapshot, d.k->p.snapshot),
a850bde6 2162 trans, dirent_not_visible_in_parent_subvol,
c60b7f80
KO
2163 "dirent not visible in parent_subvol (not an ancestor of subvol snap %u)\n%s",
2164 parent_snapshot,
2165 (bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
cc053290
KO
2166 if (!new_parent_subvol) {
2167 bch_err(c, "could not find a subvol for snapshot %u", d.k->p.snapshot);
09b9c72b 2168 return bch_err_throw(c, fsck_repair_unimplemented);
cc053290 2169 }
c60b7f80
KO
2170
2171 struct bkey_i_dirent *new_dirent = bch2_bkey_make_mut_typed(trans, iter, &d.s_c, 0, dirent);
2172 ret = PTR_ERR_OR_ZERO(new_dirent);
2173 if (ret)
2174 goto err;
2175
2176 new_dirent->v.d_parent_subvol = cpu_to_le32(new_parent_subvol);
2177 }
2178
b8628a25
KO
2179 struct bkey_s_c_subvolume s =
2180 bch2_bkey_get_iter_typed(trans, &subvol_iter,
2181 BTREE_ID_subvolumes, POS(0, target_subvol),
2182 0, subvolume);
2183 ret = bkey_err(s.s_c);
11def188 2184 if (ret && !bch2_err_matches(ret, ENOENT))
bde41d9a 2185 goto err;
11def188 2186
f5d58d0c 2187 if (ret) {
a850bde6 2188 if (fsck_err(trans, dirent_to_missing_subvol,
f5d58d0c
KO
2189 "dirent points to missing subvolume\n%s",
2190 (bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf)))
758ea4ff 2191 return bch2_fsck_remove_dirent(trans, d.k->p);
f5d58d0c
KO
2192 ret = 0;
2193 goto out;
2194 }
11def188 2195
bde41d9a
KO
2196 if (le32_to_cpu(s.v->fs_path_parent) != parent_subvol) {
2197 printbuf_reset(&buf);
2198
2199 prt_printf(&buf, "subvol with wrong fs_path_parent, should be be %u\n",
2200 parent_subvol);
2201
2202 ret = bch2_inum_to_path(trans, (subvol_inum) { s.k->p.offset,
2203 le64_to_cpu(s.v->inode) }, &buf);
b8628a25
KO
2204 if (ret)
2205 goto err;
bde41d9a
KO
2206 prt_newline(&buf);
2207 bch2_bkey_val_to_text(&buf, c, s.s_c);
b8628a25 2208
bde41d9a
KO
2209 if (fsck_err(trans, subvol_fs_path_parent_wrong, "%s", buf.buf)) {
2210 struct bkey_i_subvolume *n =
2211 bch2_bkey_make_mut_typed(trans, &subvol_iter, &s.s_c, 0, subvolume);
2212 ret = PTR_ERR_OR_ZERO(n);
2213 if (ret)
2214 goto err;
2215
2216 n->v.fs_path_parent = cpu_to_le32(parent_subvol);
2217 }
b8628a25
KO
2218 }
2219
2220 u64 target_inum = le64_to_cpu(s.v->inode);
2221 u32 target_snapshot = le32_to_cpu(s.v->snapshot);
2222
77eac89c
KO
2223 ret = bch2_inode_find_by_inum_snapshot(trans, target_inum, target_snapshot,
2224 &subvol_root, 0);
11def188 2225 if (ret && !bch2_err_matches(ret, ENOENT))
cc053290 2226 goto err;
11def188 2227
cc053290
KO
2228 if (ret) {
2229 bch_err(c, "subvol %u points to missing inode root %llu", target_subvol, target_inum);
09b9c72b 2230 ret = bch_err_throw(c, fsck_repair_unimplemented);
cc053290
KO
2231 goto err;
2232 }
2233
2234 if (fsck_err_on(!ret && parent_subvol != subvol_root.bi_parent_subvol,
a850bde6 2235 trans, inode_bi_parent_wrong,
f4e68c85
KO
2236 "subvol root %llu has wrong bi_parent_subvol: got %u, should be %u",
2237 target_inum,
2238 subvol_root.bi_parent_subvol, parent_subvol)) {
2239 subvol_root.bi_parent_subvol = parent_subvol;
72350ee0
KO
2240 subvol_root.bi_snapshot = le32_to_cpu(s.v->snapshot);
2241 ret = __bch2_fsck_write_inode(trans, &subvol_root);
f4e68c85 2242 if (ret)
cc053290 2243 goto err;
f4e68c85
KO
2244 }
2245
9b0d00a3 2246 ret = bch2_check_dirent_target(trans, iter, d, &subvol_root, true);
11def188 2247 if (ret)
cc053290 2248 goto err;
f5d58d0c 2249out:
c60b7f80 2250err:
11def188 2251fsck_err:
b8628a25 2252 bch2_trans_iter_exit(trans, &subvol_iter);
c60b7f80 2253 printbuf_exit(&buf);
11def188
KO
2254 return ret;
2255}
2256
914f2786 2257static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
a1783320 2258 struct bkey_s_c k,
914f2786 2259 struct bch_hash_info *hash_info,
ef1669ff
KO
2260 struct inode_walker *dir,
2261 struct inode_walker *target,
c72def52
KO
2262 struct snapshots_seen *s,
2263 bool *need_second_pass)
1c6fdbd8 2264{
914f2786 2265 struct bch_fs *c = trans->c;
ef1669ff 2266 struct inode_walker_entry *i;
fa8e94fa
KO
2267 struct printbuf buf = PRINTBUF;
2268 int ret = 0;
d69f41d6 2269
08f50005 2270 ret = bch2_check_key_has_snapshot(trans, iter, k);
fa8e94fa
KO
2271 if (ret) {
2272 ret = ret < 0 ? ret : 0;
2273 goto out;
2274 }
1c6fdbd8 2275
49124d8a 2276 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
914f2786 2277 if (ret)
fa8e94fa 2278 goto err;
8a85b20c 2279
ef1669ff 2280 if (k.k->type == KEY_TYPE_whiteout)
fa8e94fa 2281 goto out;
ef1669ff 2282
3672bda8 2283 if (dir->last_pos.inode != k.k->p.inode && dir->have_inodes) {
b9ddb3e1 2284 ret = check_subdir_dirents_count(trans, dir);
ef1669ff 2285 if (ret)
fa8e94fa 2286 goto err;
ef1669ff
KO
2287 }
2288
971a1503 2289 i = walk_inode(trans, dir, k);
06dcca51 2290 ret = PTR_ERR_OR_ZERO(i);
ef1669ff 2291 if (ret < 0)
fa8e94fa 2292 goto err;
1c6fdbd8 2293
c13d526d
KO
2294 ret = check_key_has_inode(trans, iter, dir, i, k);
2295 if (ret)
09d4c2ac 2296 goto err;
ef1669ff 2297
855070dc 2298 if (!i || i->whiteout)
fa8e94fa 2299 goto out;
ef1669ff 2300
c13d526d
KO
2301 if (dir->first_this_inode)
2302 *hash_info = bch2_hash_info_init(c, &i->inode);
2303 dir->first_this_inode = false;
8a85b20c 2304
b938d3c9 2305 hash_info->cf_encoding = bch2_inode_casefold(c, &i->inode) ? c->cf_encoding : NULL;
b938d3c9 2306
c72def52
KO
2307 ret = bch2_str_hash_check_key(trans, s, &bch2_dirent_hash_desc, hash_info,
2308 iter, k, need_second_pass);
914f2786 2309 if (ret < 0)
fa8e94fa
KO
2310 goto err;
2311 if (ret) {
2312 /* dirent has been deleted */
2313 ret = 0;
2314 goto out;
2315 }
7ac2c55e 2316
914f2786 2317 if (k.k->type != KEY_TYPE_dirent)
fa8e94fa 2318 goto out;
1c6fdbd8 2319
09d4c2ac 2320 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
1c6fdbd8 2321
010c8946
KO
2322 /* check casefold */
2323 if (fsck_err_on(d.v->d_casefold != !!hash_info->cf_encoding,
2324 trans, dirent_casefold_mismatch,
2325 "dirent casefold does not match dir casefold\n%s",
2326 (printbuf_reset(&buf),
2327 bch2_bkey_val_to_text(&buf, c, k),
2328 buf.buf))) {
2bf380c0
KO
2329 subvol_inum dir_inum = { .subvol = d.v->d_type == DT_SUBVOL
2330 ? le32_to_cpu(d.v->d_parent_subvol)
2331 : 0,
2332 };
010c8946 2333 u64 target = d.v->d_type == DT_SUBVOL
016c4b48
KO
2334 ? le32_to_cpu(d.v->d_child_subvol)
2335 : le64_to_cpu(d.v->d_inum);
2bf380c0
KO
2336 struct qstr name = bch2_dirent_get_name(d);
2337
2338 struct bkey_i_dirent *new_d =
2339 bch2_dirent_create_key(trans, hash_info, dir_inum,
2340 d.v->d_type, &name, NULL, target);
2341 ret = PTR_ERR_OR_ZERO(new_d);
2342 if (ret)
2343 goto out;
010c8946 2344
2bf380c0
KO
2345 new_d->k.p.inode = d.k->p.inode;
2346 new_d->k.p.snapshot = d.k->p.snapshot;
2347
2348 struct btree_iter dup_iter = {};
2349 ret = bch2_hash_delete_at(trans,
010c8946
KO
2350 bch2_dirent_hash_desc, hash_info, iter,
2351 BTREE_UPDATE_internal_snapshot_node) ?:
2bf380c0
KO
2352 bch2_str_hash_repair_key(trans, s,
2353 &bch2_dirent_hash_desc, hash_info,
2354 iter, bkey_i_to_s_c(&new_d->k_i),
2355 &dup_iter, bkey_s_c_null,
2356 need_second_pass);
010c8946
KO
2357 goto out;
2358 }
2359
4db65027 2360 if (d.v->d_type == DT_SUBVOL) {
ea27001e 2361 ret = check_dirent_to_subvol(trans, iter, d);
914f2786 2362 if (ret)
fa8e94fa 2363 goto err;
ef1669ff 2364 } else {
5a2d1521 2365 ret = get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
ef1669ff 2366 if (ret)
fa8e94fa 2367 goto err;
1c6fdbd8 2368
b65db750 2369 if (fsck_err_on(!target->inodes.nr,
a850bde6 2370 trans, dirent_to_missing_inode,
5a2d1521 2371 "dirent points to missing inode:\n%s",
fa8e94fa
KO
2372 (printbuf_reset(&buf),
2373 bch2_bkey_val_to_text(&buf, c, k),
2374 buf.buf))) {
758ea4ff 2375 ret = bch2_fsck_remove_dirent(trans, d.k->p);
ef1669ff 2376 if (ret)
fa8e94fa 2377 goto err;
1c6fdbd8
KO
2378 }
2379
91d961ba 2380 darray_for_each(target->inodes, i) {
9b0d00a3 2381 ret = bch2_check_dirent_target(trans, iter, d, &i->inode, true);
ef1669ff 2382 if (ret)
fa8e94fa 2383 goto err;
d3ff7fec 2384 }
15734b5e
KO
2385
2386 darray_for_each(target->deletes, i)
2387 if (fsck_err_on(!snapshot_list_has_id(&s->ids, *i),
2388 trans, dirent_to_overwritten_inode,
2389 "dirent points to inode overwritten in snapshot %u:\n%s",
2390 *i,
2391 (printbuf_reset(&buf),
2392 bch2_bkey_val_to_text(&buf, c, k),
2393 buf.buf))) {
2394 struct btree_iter delete_iter;
2395 bch2_trans_iter_init(trans, &delete_iter,
2396 BTREE_ID_dirents,
2397 SPOS(k.k->p.inode, k.k->p.offset, *i),
2398 BTREE_ITER_intent);
9180ad2e 2399 ret = bch2_btree_iter_traverse(trans, &delete_iter) ?:
15734b5e
KO
2400 bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
2401 hash_info,
2402 &delete_iter,
2403 BTREE_UPDATE_internal_snapshot_node);
2404 bch2_trans_iter_exit(trans, &delete_iter);
2405 if (ret)
2406 goto err;
2407
2408 }
11def188 2409 }
3672bda8
KO
2410
2411 ret = bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
2412 if (ret)
2413 goto err;
2414
b9ddb3e1
KO
2415 for_each_visible_inode(c, s, dir, d.k->p.snapshot, i) {
2416 if (d.v->d_type == DT_DIR)
3672bda8 2417 i->count++;
b9ddb3e1
KO
2418 i->i_size += bkey_bytes(d.k);
2419 }
fa8e94fa
KO
2420out:
2421err:
914f2786 2422fsck_err:
fa8e94fa 2423 printbuf_exit(&buf);
914f2786
KO
2424 return ret;
2425}
1c6fdbd8 2426
914f2786
KO
2427/*
2428 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
2429 * validate d_type
2430 */
067d228b 2431int bch2_check_dirents(struct bch_fs *c)
914f2786 2432{
ef1669ff
KO
2433 struct inode_walker dir = inode_walker_init();
2434 struct inode_walker target = inode_walker_init();
2435 struct snapshots_seen s;
914f2786 2436 struct bch_hash_info hash_info;
c72def52 2437 bool need_second_pass = false, did_second_pass = false;
01d925f7 2438 int ret;
1c6fdbd8 2439
ef1669ff 2440 snapshots_seen_init(&s);
c72def52 2441again:
01d925f7 2442 ret = bch2_trans_run(c,
1cda5b88 2443 for_each_btree_key_commit(trans, iter, BTREE_ID_dirents,
4eb3877e 2444 POS(BCACHEFS_ROOT_INO, 0),
3672bda8 2445 BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
1cda5b88 2446 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
c72def52
KO
2447 check_dirent(trans, &iter, k, &hash_info, &dir, &target, &s,
2448 &need_second_pass)) ?:
109ea419 2449 check_subdir_count_notnested(trans, &dir));
914f2786 2450
c72def52
KO
2451 if (!ret && need_second_pass && !did_second_pass) {
2452 bch_info(c, "check_dirents requires second pass");
2453 swap(did_second_pass, need_second_pass);
2454 goto again;
2455 }
2456
2457 if (!ret && need_second_pass) {
2458 bch_err(c, "dirents not repairing");
2459 ret = -EINVAL;
2460 }
2461
ef1669ff
KO
2462 snapshots_seen_exit(&s);
2463 inode_walker_exit(&dir);
2464 inode_walker_exit(&target);
d2a990d1 2465 bch_err_fn(c, ret);
ef1669ff 2466 return ret;
1c6fdbd8
KO
2467}
2468
285b181a 2469static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
a1783320 2470 struct bkey_s_c k,
285b181a
KO
2471 struct bch_hash_info *hash_info,
2472 struct inode_walker *inode)
2473{
2474 struct bch_fs *c = trans->c;
285b181a 2475
c72def52 2476 int ret = bch2_check_key_has_snapshot(trans, iter, k);
3ff34756 2477 if (ret < 0)
285b181a 2478 return ret;
3ff34756
KO
2479 if (ret)
2480 return 0;
285b181a 2481
c72def52 2482 struct inode_walker_entry *i = walk_inode(trans, inode, k);
06dcca51
KO
2483 ret = PTR_ERR_OR_ZERO(i);
2484 if (ret)
285b181a
KO
2485 return ret;
2486
c13d526d
KO
2487 ret = check_key_has_inode(trans, iter, inode, i, k);
2488 if (ret)
2489 return ret;
285b181a 2490
855070dc 2491 if (!i || i->whiteout)
285b181a
KO
2492 return 0;
2493
c13d526d
KO
2494 if (inode->first_this_inode)
2495 *hash_info = bch2_hash_info_init(c, &i->inode);
2496 inode->first_this_inode = false;
2497
c72def52
KO
2498 bool need_second_pass = false;
2499 return bch2_str_hash_check_key(trans, NULL, &bch2_xattr_hash_desc, hash_info,
2500 iter, k, &need_second_pass);
285b181a
KO
2501}
2502
1c6fdbd8
KO
2503/*
2504 * Walk xattrs: verify that they all have a corresponding inode
2505 */
067d228b 2506int bch2_check_xattrs(struct bch_fs *c)
1c6fdbd8 2507{
285b181a 2508 struct inode_walker inode = inode_walker_init();
7ac2c55e 2509 struct bch_hash_info hash_info;
1c6fdbd8
KO
2510 int ret = 0;
2511
6bd68ec2
KO
2512 ret = bch2_trans_run(c,
2513 for_each_btree_key_commit(trans, iter, BTREE_ID_xattrs,
a1783320 2514 POS(BCACHEFS_ROOT_INO, 0),
5dd8c60e 2515 BTREE_ITER_prefetch|BTREE_ITER_all_snapshots,
a1783320
KO
2516 k,
2517 NULL, NULL,
3f0e297d 2518 BCH_TRANS_COMMIT_no_enospc,
6bd68ec2 2519 check_xattr(trans, &iter, k, &hash_info, &inode)));
3e878fe5
KO
2520
2521 inode_walker_exit(&inode);
d2a990d1 2522 bch_err_fn(c, ret);
9a796fdb 2523 return ret;
1c6fdbd8
KO
2524}
2525
285b181a 2526static int check_root_trans(struct btree_trans *trans)
1c6fdbd8 2527{
285b181a 2528 struct bch_fs *c = trans->c;
ef1669ff 2529 struct bch_inode_unpacked root_inode;
d3ff7fec 2530 u32 snapshot;
ef1669ff 2531 u64 inum;
1c6fdbd8
KO
2532 int ret;
2533
c98d132e 2534 ret = subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
e47a390a 2535 if (ret && !bch2_err_matches(ret, ENOENT))
1c6fdbd8
KO
2536 return ret;
2537
a850bde6 2538 if (mustfix_fsck_err_on(ret, trans, root_subvol_missing,
b65db750 2539 "root subvol missing")) {
dcc1c045
KO
2540 struct bkey_i_subvolume *root_subvol =
2541 bch2_trans_kmalloc(trans, sizeof(*root_subvol));
2542 ret = PTR_ERR_OR_ZERO(root_subvol);
2543 if (ret)
2544 goto err;
1c6fdbd8 2545
ef1669ff
KO
2546 snapshot = U32_MAX;
2547 inum = BCACHEFS_ROOT_INO;
1c6fdbd8 2548
dcc1c045
KO
2549 bkey_subvolume_init(&root_subvol->k_i);
2550 root_subvol->k.p.offset = BCACHEFS_ROOT_SUBVOL;
2551 root_subvol->v.flags = 0;
2552 root_subvol->v.snapshot = cpu_to_le32(snapshot);
2553 root_subvol->v.inode = cpu_to_le64(inum);
2554 ret = bch2_btree_insert_trans(trans, BTREE_ID_subvolumes, &root_subvol->k_i, 0);
d2a990d1
KO
2555 bch_err_msg(c, ret, "writing root subvol");
2556 if (ret)
ef1669ff 2557 goto err;
ef1669ff
KO
2558 }
2559
77eac89c
KO
2560 ret = bch2_inode_find_by_inum_snapshot(trans, BCACHEFS_ROOT_INO, snapshot,
2561 &root_inode, 0);
e47a390a 2562 if (ret && !bch2_err_matches(ret, ENOENT))
ef1669ff 2563 return ret;
1c6fdbd8 2564
a850bde6
KO
2565 if (mustfix_fsck_err_on(ret,
2566 trans, root_dir_missing,
b65db750
KO
2567 "root directory missing") ||
2568 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode),
a850bde6 2569 trans, root_inode_not_dir,
ef1669ff
KO
2570 "root inode not a directory")) {
2571 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
2572 0, NULL);
2573 root_inode.bi_inum = inum;
72350ee0 2574 root_inode.bi_snapshot = snapshot;
1c6fdbd8 2575
72350ee0 2576 ret = __bch2_fsck_write_inode(trans, &root_inode);
d2a990d1 2577 bch_err_msg(c, ret, "writing root inode");
ef1669ff
KO
2578 }
2579err:
2580fsck_err:
ef1669ff 2581 return ret;
1c6fdbd8
KO
2582}
2583
285b181a 2584/* Get root directory, create if it doesn't exist: */
067d228b 2585int bch2_check_root(struct bch_fs *c)
285b181a 2586{
a0d11fee 2587 int ret = bch2_trans_commit_do(c, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
6bd68ec2 2588 check_root_trans(trans));
d2a990d1 2589 bch_err_fn(c, ret);
1bb3c2a9 2590 return ret;
285b181a
KO
2591}
2592
663db5a5
KO
2593static bool darray_u32_has(darray_u32 *d, u32 v)
2594{
2595 darray_for_each(*d, i)
2596 if (*i == v)
2597 return true;
2598 return false;
2599}
2600
663db5a5
KO
2601static int check_subvol_path(struct btree_trans *trans, struct btree_iter *iter, struct bkey_s_c k)
2602{
2603 struct bch_fs *c = trans->c;
2604 struct btree_iter parent_iter = {};
2605 darray_u32 subvol_path = {};
2606 struct printbuf buf = PRINTBUF;
2607 int ret = 0;
2608
2609 if (k.k->type != KEY_TYPE_subvolume)
2610 return 0;
2611
7029cc4d
KO
2612 subvol_inum start = {
2613 .subvol = k.k->p.offset,
2614 .inum = le64_to_cpu(bkey_s_c_to_subvolume(k).v->inode),
2615 };
2616
663db5a5
KO
2617 while (k.k->p.offset != BCACHEFS_ROOT_SUBVOL) {
2618 ret = darray_push(&subvol_path, k.k->p.offset);
2619 if (ret)
2620 goto err;
2621
2622 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
2623
3a5895e3
KO
2624 struct bch_inode_unpacked subvol_root;
2625 ret = bch2_inode_find_by_inum_trans(trans,
2626 (subvol_inum) { s.k->p.offset, le64_to_cpu(s.v->inode) },
2627 &subvol_root);
2628 if (ret)
663db5a5
KO
2629 break;
2630
663db5a5
KO
2631 u32 parent = le32_to_cpu(s.v->fs_path_parent);
2632
2633 if (darray_u32_has(&subvol_path, parent)) {
6b86da92 2634 printbuf_reset(&buf);
7029cc4d 2635 prt_printf(&buf, "subvolume loop: ");
6b86da92 2636
7029cc4d
KO
2637 ret = bch2_inum_to_path(trans, start, &buf);
2638 if (ret)
2639 goto err;
6b86da92
KO
2640
2641 if (fsck_err(trans, subvol_loop, "%s", buf.buf))
663db5a5
KO
2642 ret = reattach_subvol(trans, s);
2643 break;
2644 }
2645
2646 bch2_trans_iter_exit(trans, &parent_iter);
2647 bch2_trans_iter_init(trans, &parent_iter,
2648 BTREE_ID_subvolumes, POS(0, parent), 0);
9180ad2e 2649 k = bch2_btree_iter_peek_slot(trans, &parent_iter);
663db5a5
KO
2650 ret = bkey_err(k);
2651 if (ret)
2652 goto err;
2653
2654 if (fsck_err_on(k.k->type != KEY_TYPE_subvolume,
a850bde6 2655 trans, subvol_unreachable,
663db5a5 2656 "unreachable subvolume %s",
6b86da92
KO
2657 (printbuf_reset(&buf),
2658 bch2_bkey_val_to_text(&buf, c, s.s_c),
663db5a5
KO
2659 buf.buf))) {
2660 ret = reattach_subvol(trans, s);
2661 break;
2662 }
2663 }
2664fsck_err:
2665err:
2666 printbuf_exit(&buf);
2667 darray_exit(&subvol_path);
2668 bch2_trans_iter_exit(trans, &parent_iter);
2669 return ret;
2670}
2671
2672int bch2_check_subvolume_structure(struct bch_fs *c)
2673{
2674 int ret = bch2_trans_run(c,
2675 for_each_btree_key_commit(trans, iter,
5dd8c60e 2676 BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_prefetch, k,
663db5a5
KO
2677 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2678 check_subvol_path(trans, &iter, k)));
2679 bch_err_fn(c, ret);
2680 return ret;
2681}
2682
495ba899
KO
2683static int bch2_bi_depth_renumber_one(struct btree_trans *trans,
2684 u64 inum, u32 snapshot,
59c50511
KO
2685 u32 new_depth)
2686{
2687 struct btree_iter iter;
2688 struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
495ba899 2689 SPOS(0, inum, snapshot), 0);
59c50511
KO
2690
2691 struct bch_inode_unpacked inode;
2692 int ret = bkey_err(k) ?:
2693 !bkey_is_inode(k.k) ? -BCH_ERR_ENOENT_inode
2694 : bch2_inode_unpack(k, &inode);
2695 if (ret)
2696 goto err;
2697
2698 if (inode.bi_depth != new_depth) {
2699 inode.bi_depth = new_depth;
2700 ret = __bch2_fsck_write_inode(trans, &inode) ?:
2701 bch2_trans_commit(trans, NULL, NULL, 0);
2702 }
2703err:
2704 bch2_trans_iter_exit(trans, &iter);
2705 return ret;
2706}
2707
495ba899
KO
2708static int bch2_bi_depth_renumber(struct btree_trans *trans, darray_u64 *path,
2709 u32 snapshot, u32 new_bi_depth)
59c50511
KO
2710{
2711 u32 restart_count = trans->restart_count;
2712 int ret = 0;
2713
2714 darray_for_each_reverse(*path, i) {
2715 ret = nested_lockrestart_do(trans,
495ba899 2716 bch2_bi_depth_renumber_one(trans, *i, snapshot, new_bi_depth));
59c50511
KO
2717 bch_err_fn(trans->c, ret);
2718 if (ret)
2719 break;
2720
2721 new_bi_depth++;
2722 }
2723
2724 return ret ?: trans_was_restarted(trans, restart_count);
2725}
2726
59c50511 2727static int check_path_loop(struct btree_trans *trans, struct bkey_s_c inode_k)
1c6fdbd8 2728{
d3ff7fec 2729 struct bch_fs *c = trans->c;
3a136177 2730 struct btree_iter inode_iter = {};
495ba899 2731 darray_u64 path = {};
3a136177 2732 struct printbuf buf = PRINTBUF;
5a2d1521 2733 u32 snapshot = inode_k.k->p.snapshot;
59c50511
KO
2734 bool redo_bi_depth = false;
2735 u32 min_bi_depth = U32_MAX;
1c6fdbd8
KO
2736 int ret = 0;
2737
495ba899
KO
2738 struct bpos start = inode_k.k->p;
2739
644457ed
KO
2740 struct bch_inode_unpacked inode;
2741 ret = bch2_inode_unpack(inode_k, &inode);
2742 if (ret)
2743 return ret;
688a7694 2744
bbc3a0b1
KO
2745 /*
2746 * If we're running full fsck, check_dirents() will have already ran,
2747 * and we shouldn't see any missing backpointers here - otherwise that's
2748 * handled separately, by check_unreachable_inodes
2749 */
2750 while (!inode.bi_subvol &&
2751 bch2_inode_has_backpointer(&inode)) {
4db65027
KO
2752 struct btree_iter dirent_iter;
2753 struct bkey_s_c_dirent d;
6e0c886d 2754
495ba899
KO
2755 d = dirent_get_by_pos(trans, &dirent_iter,
2756 SPOS(inode.bi_dir, inode.bi_dir_offset, snapshot));
c98d132e 2757 ret = bkey_err(d.s_c);
e47a390a 2758 if (ret && !bch2_err_matches(ret, ENOENT))
59c50511 2759 goto out;
1c6fdbd8 2760
a6508079 2761 if (!ret && (ret = dirent_points_to_inode(c, d, &inode)))
4db65027 2762 bch2_trans_iter_exit(trans, &dirent_iter);
4db65027 2763
e47a390a 2764 if (bch2_err_matches(ret, ENOENT)) {
bade9711
KO
2765 printbuf_reset(&buf);
2766 bch2_bkey_val_to_text(&buf, c, inode_k);
2767 bch_err(c, "unreachable inode in check_directory_structure: %s\n%s",
2768 bch2_err_str(ret), buf.buf);
3a136177 2769 goto out;
d3ff7fec 2770 }
4db65027
KO
2771
2772 bch2_trans_iter_exit(trans, &dirent_iter);
1c6fdbd8 2773
495ba899 2774 ret = darray_push(&path, inode.bi_inum);
663db5a5 2775 if (ret)
d3ff7fec 2776 return ret;
1c6fdbd8 2777
3a136177
KO
2778 bch2_trans_iter_exit(trans, &inode_iter);
2779 inode_k = bch2_bkey_get_iter(trans, &inode_iter, BTREE_ID_inodes,
2780 SPOS(0, inode.bi_dir, snapshot), 0);
59c50511
KO
2781
2782 struct bch_inode_unpacked parent_inode;
3a136177
KO
2783 ret = bkey_err(inode_k) ?:
2784 !bkey_is_inode(inode_k.k) ? -BCH_ERR_ENOENT_inode
59c50511 2785 : bch2_inode_unpack(inode_k, &parent_inode);
6e0c886d
KO
2786 if (ret) {
2787 /* Should have been caught in dirents pass */
cecc3282 2788 bch_err_msg(c, ret, "error looking up parent directory");
59c50511 2789 goto out;
6e0c886d
KO
2790 }
2791
59c50511
KO
2792 min_bi_depth = parent_inode.bi_depth;
2793
2794 if (parent_inode.bi_depth < inode.bi_depth &&
2795 min_bi_depth < U16_MAX)
2796 break;
2797
2798 inode = parent_inode;
59c50511 2799 redo_bi_depth = true;
3a136177 2800
495ba899 2801 if (darray_find(path, inode.bi_inum)) {
6b86da92 2802 printbuf_reset(&buf);
495ba899
KO
2803 prt_printf(&buf, "directory structure loop in snapshot %u: ",
2804 snapshot);
2805
2806 ret = bch2_inum_snapshot_to_path(trans, start.offset, start.snapshot, NULL, &buf);
2807 if (ret)
2808 goto out;
2809
2810 if (c->opts.verbose) {
2811 prt_newline(&buf);
2812 darray_for_each(path, i)
2813 prt_printf(&buf, "%llu ", *i);
2814 }
6e0c886d 2815
6b86da92 2816 if (fsck_err(trans, dir_loop, "%s", buf.buf)) {
663db5a5 2817 ret = remove_backpointer(trans, &inode);
c98d132e 2818 bch_err_msg(c, ret, "removing dirent");
663db5a5 2819 if (ret)
fbf913cb 2820 goto out;
1c6fdbd8 2821
72350ee0 2822 ret = reattach_inode(trans, &inode);
688a7694 2823 bch_err_msg(c, ret, "reattaching inode %llu", inode.bi_inum);
663db5a5 2824 }
59c50511
KO
2825
2826 goto out;
1c6fdbd8 2827 }
1c6fdbd8 2828 }
59c50511
KO
2829
2830 if (inode.bi_subvol)
2831 min_bi_depth = 0;
2832
2833 if (redo_bi_depth)
495ba899 2834 ret = bch2_bi_depth_renumber(trans, &path, snapshot, min_bi_depth);
3a136177 2835out:
d3ff7fec 2836fsck_err:
3a136177 2837 bch2_trans_iter_exit(trans, &inode_iter);
59c50511 2838 darray_exit(&path);
3a136177 2839 printbuf_exit(&buf);
d2a990d1 2840 bch_err_fn(c, ret);
d3ff7fec
KO
2841 return ret;
2842}
1c6fdbd8 2843
d3ff7fec 2844/*
bade9711
KO
2845 * Check for loops in the directory structure: all other connectivity issues
2846 * have been fixed by prior passes
d3ff7fec 2847 */
067d228b 2848int bch2_check_directory_structure(struct bch_fs *c)
d3ff7fec 2849{
59c50511 2850 int ret = bch2_trans_run(c,
3e5ceaa5 2851 for_each_btree_key_reverse_commit(trans, iter, BTREE_ID_inodes, POS_MIN,
5dd8c60e
KO
2852 BTREE_ITER_intent|
2853 BTREE_ITER_prefetch|
2854 BTREE_ITER_all_snapshots, k,
c98d132e 2855 NULL, NULL, BCH_TRANS_COMMIT_no_enospc, ({
59c50511 2856 if (!S_ISDIR(bkey_inode_mode(k)))
c98d132e 2857 continue;
1c6fdbd8 2858
688a7694 2859 if (bch2_inode_flags(k) & BCH_INODE_unlinked)
c98d132e 2860 continue;
ef1669ff 2861
59c50511 2862 check_path_loop(trans, k);
c98d132e 2863 })));
27b2df98 2864
d2a990d1 2865 bch_err_fn(c, ret);
9a796fdb 2866 return ret;
1c6fdbd8
KO
2867}
2868
fc51b041
KO
2869struct nlink_table {
2870 size_t nr;
2871 size_t size;
1c6fdbd8 2872
fc51b041
KO
2873 struct nlink {
2874 u64 inum;
2875 u32 snapshot;
2876 u32 count;
2877 } *d;
2878};
1c6fdbd8 2879
f0f41a6d
KO
2880static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2881 u64 inum, u32 snapshot)
1c6fdbd8 2882{
fc51b041
KO
2883 if (t->nr == t->size) {
2884 size_t new_size = max_t(size_t, 128UL, t->size * 2);
3e3e02e6
KO
2885 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2886
fc51b041 2887 if (!d) {
f0f41a6d
KO
2888 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2889 new_size);
09b9c72b 2890 return bch_err_throw(c, ENOMEM_fsck_add_nlink);
fc51b041 2891 }
1c6fdbd8 2892
82355e28
KO
2893 if (t->d)
2894 memcpy(d, t->d, t->size * sizeof(t->d[0]));
fc51b041 2895 kvfree(t->d);
1c6fdbd8 2896
fc51b041
KO
2897 t->d = d;
2898 t->size = new_size;
2bb748a6
KO
2899 }
2900
fc51b041
KO
2901
2902 t->d[t->nr++] = (struct nlink) {
2903 .inum = inum,
2904 .snapshot = snapshot,
2905 };
2906
2907 return 0;
2908}
2909
2910static int nlink_cmp(const void *_l, const void *_r)
2911{
2912 const struct nlink *l = _l;
2913 const struct nlink *r = _r;
2914
db18ef1a 2915 return cmp_int(l->inum, r->inum);
fc51b041
KO
2916}
2917
ef1669ff
KO
2918static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2919 struct nlink_table *links,
2920 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
fc51b041
KO
2921{
2922 struct nlink *link, key = {
2923 .inum = inum, .snapshot = U32_MAX,
2924 };
2925
2926 if (inum < range_start || inum >= range_end)
1c6fdbd8 2927 return;
fc51b041
KO
2928
2929 link = __inline_bsearch(&key, links->d, links->nr,
2930 sizeof(links->d[0]), nlink_cmp);
ef1669ff
KO
2931 if (!link)
2932 return;
2933
2934 while (link > links->d && link[0].inum == link[-1].inum)
2935 --link;
2936
2937 for (; link < links->d + links->nr && link->inum == inum; link++)
2938 if (ref_visible(c, s, snapshot, link->snapshot)) {
2939 link->count++;
2940 if (link->snapshot >= snapshot)
2941 break;
2942 }
fc51b041
KO
2943}
2944
2945noinline_for_stack
2946static int check_nlinks_find_hardlinks(struct bch_fs *c,
2947 struct nlink_table *t,
2948 u64 start, u64 *end)
2949{
27b2df98 2950 int ret = bch2_trans_run(c,
5028b907
KO
2951 for_each_btree_key(trans, iter, BTREE_ID_inodes,
2952 POS(0, start),
5dd8c60e
KO
2953 BTREE_ITER_intent|
2954 BTREE_ITER_prefetch|
2955 BTREE_ITER_all_snapshots, k, ({
27b2df98
KO
2956 if (!bkey_is_inode(k.k))
2957 continue;
fc51b041 2958
27b2df98 2959 /* Should never fail, checked by bch2_inode_invalid: */
80eab7a7 2960 struct bch_inode_unpacked u;
644457ed
KO
2961 _ret3 = bch2_inode_unpack(k, &u);
2962 if (_ret3)
2963 break;
fc51b041 2964
27b2df98
KO
2965 /*
2966 * Backpointer and directory structure checks are sufficient for
2967 * directories, since they can't have hardlinks:
2968 */
2969 if (S_ISDIR(u.bi_mode))
2970 continue;
fc51b041 2971
bade9711
KO
2972 /*
2973 * Previous passes ensured that bi_nlink is nonzero if
2974 * it had multiple hardlinks:
2975 */
27b2df98
KO
2976 if (!u.bi_nlink)
2977 continue;
fc51b041 2978
27b2df98
KO
2979 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2980 if (ret) {
2981 *end = k.k->p.offset;
2982 ret = 0;
2983 break;
2984 }
2985 0;
2986 })));
1c6fdbd8 2987
27b2df98 2988 bch_err_fn(c, ret);
fc51b041 2989 return ret;
1c6fdbd8
KO
2990}
2991
2992noinline_for_stack
fc51b041
KO
2993static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2994 u64 range_start, u64 range_end)
1c6fdbd8 2995{
ef1669ff 2996 struct snapshots_seen s;
1c6fdbd8 2997
ef1669ff
KO
2998 snapshots_seen_init(&s);
2999
27b2df98 3000 int ret = bch2_trans_run(c,
5028b907 3001 for_each_btree_key(trans, iter, BTREE_ID_dirents, POS_MIN,
5dd8c60e
KO
3002 BTREE_ITER_intent|
3003 BTREE_ITER_prefetch|
3004 BTREE_ITER_all_snapshots, k, ({
27b2df98
KO
3005 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
3006 if (ret)
3007 break;
1c6fdbd8 3008
80eab7a7
KO
3009 if (k.k->type == KEY_TYPE_dirent) {
3010 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
abcecb49 3011
27b2df98
KO
3012 if (d.v->d_type != DT_DIR &&
3013 d.v->d_type != DT_SUBVOL)
3014 inc_link(c, &s, links, range_start, range_end,
5a2d1521 3015 le64_to_cpu(d.v->d_inum), d.k->p.snapshot);
27b2df98
KO
3016 }
3017 0;
3018 })));
1c6fdbd8 3019
ef1669ff 3020 snapshots_seen_exit(&s);
27b2df98
KO
3021
3022 bch_err_fn(c, ret);
1c6fdbd8
KO
3023 return ret;
3024}
3025
eace11a7
KO
3026static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
3027 struct bkey_s_c k,
3028 struct nlink_table *links,
3029 size_t *idx, u64 range_end)
3030{
eace11a7
KO
3031 struct bch_inode_unpacked u;
3032 struct nlink *link = &links->d[*idx];
3033 int ret = 0;
3034
3035 if (k.k->p.offset >= range_end)
3036 return 1;
3037
3038 if (!bkey_is_inode(k.k))
3039 return 0;
3040
644457ed
KO
3041 ret = bch2_inode_unpack(k, &u);
3042 if (ret)
3043 return ret;
eace11a7 3044
73bd774d 3045 if (S_ISDIR(u.bi_mode))
eace11a7
KO
3046 return 0;
3047
3048 if (!u.bi_nlink)
3049 return 0;
3050
3051 while ((cmp_int(link->inum, k.k->p.offset) ?:
3052 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
3053 BUG_ON(*idx == links->nr);
3054 link = &links->d[++*idx];
3055 }
3056
b65db750 3057 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count,
a850bde6 3058 trans, inode_wrong_nlink,
eace11a7
KO
3059 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
3060 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
3061 bch2_inode_nlink_get(&u), link->count)) {
3062 bch2_inode_nlink_set(&u, link->count);
72350ee0 3063 ret = __bch2_fsck_write_inode(trans, &u);
eace11a7
KO
3064 }
3065fsck_err:
3066 return ret;
3067}
3068
1c6fdbd8 3069noinline_for_stack
fc51b041
KO
3070static int check_nlinks_update_hardlinks(struct bch_fs *c,
3071 struct nlink_table *links,
1c6fdbd8
KO
3072 u64 range_start, u64 range_end)
3073{
eace11a7 3074 size_t idx = 0;
1c6fdbd8 3075
80eab7a7 3076 int ret = bch2_trans_run(c,
6bd68ec2
KO
3077 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
3078 POS(0, range_start),
5dd8c60e 3079 BTREE_ITER_intent|BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
3f0e297d 3080 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
6bd68ec2 3081 check_nlinks_update_inode(trans, &iter, k, links, &idx, range_end)));
eace11a7 3082 if (ret < 0) {
8b58623f 3083 bch_err(c, "error in fsck walking inodes: %s", bch2_err_str(ret));
eace11a7
KO
3084 return ret;
3085 }
1c6fdbd8 3086
eace11a7 3087 return 0;
1c6fdbd8
KO
3088}
3089
067d228b 3090int bch2_check_nlinks(struct bch_fs *c)
1c6fdbd8 3091{
fc51b041 3092 struct nlink_table links = { 0 };
1c6fdbd8
KO
3093 u64 this_iter_range_start, next_iter_range_start = 0;
3094 int ret = 0;
3095
1c6fdbd8
KO
3096 do {
3097 this_iter_range_start = next_iter_range_start;
3098 next_iter_range_start = U64_MAX;
3099
fc51b041
KO
3100 ret = check_nlinks_find_hardlinks(c, &links,
3101 this_iter_range_start,
3102 &next_iter_range_start);
3103
3104 ret = check_nlinks_walk_dirents(c, &links,
1c6fdbd8 3105 this_iter_range_start,
fc51b041 3106 next_iter_range_start);
1c6fdbd8
KO
3107 if (ret)
3108 break;
3109
fc51b041 3110 ret = check_nlinks_update_hardlinks(c, &links,
1c6fdbd8
KO
3111 this_iter_range_start,
3112 next_iter_range_start);
3113 if (ret)
3114 break;
3115
fc51b041 3116 links.nr = 0;
1c6fdbd8
KO
3117 } while (next_iter_range_start != U64_MAX);
3118
fc51b041 3119 kvfree(links.d);
d2a990d1 3120 bch_err_fn(c, ret);
1c6fdbd8
KO
3121 return ret;
3122}
3123
eace11a7
KO
3124static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
3125 struct bkey_s_c k)
bfe88863 3126{
bfe88863
KO
3127 struct bkey_s_c_reflink_p p;
3128 struct bkey_i_reflink_p *u;
bfe88863 3129
bfe88863
KO
3130 if (k.k->type != KEY_TYPE_reflink_p)
3131 return 0;
3132
3133 p = bkey_s_c_to_reflink_p(k);
3134
6d76aefe 3135 if (!p.v->front_pad && !p.v->back_pad)
bfe88863
KO
3136 return 0;
3137
3138 u = bch2_trans_kmalloc(trans, sizeof(*u));
cf904c8d 3139 int ret = PTR_ERR_OR_ZERO(u);
bfe88863
KO
3140 if (ret)
3141 return ret;
3142
3143 bkey_reassemble(&u->k_i, k);
6d76aefe
KO
3144 u->v.front_pad = 0;
3145 u->v.back_pad = 0;
bfe88863 3146
5dd8c60e 3147 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_norun);
bfe88863
KO
3148}
3149
067d228b 3150int bch2_fix_reflink_p(struct bch_fs *c)
bfe88863 3151{
bfe88863
KO
3152 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
3153 return 0;
3154
80eab7a7 3155 int ret = bch2_trans_run(c,
6bd68ec2 3156 for_each_btree_key_commit(trans, iter,
1bb3c2a9 3157 BTREE_ID_extents, POS_MIN,
5dd8c60e
KO
3158 BTREE_ITER_intent|BTREE_ITER_prefetch|
3159 BTREE_ITER_all_snapshots, k,
3f0e297d 3160 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
6bd68ec2 3161 fix_reflink_p_key(trans, &iter, k)));
d2a990d1 3162 bch_err_fn(c, ret);
bfe88863
KO
3163 return ret;
3164}
e5ea0529 3165
c9b9afe7
KO
3166#ifndef NO_BCACHEFS_CHARDEV
3167
e5ea0529
KO
3168struct fsck_thread {
3169 struct thread_with_stdio thr;
3170 struct bch_fs *c;
3171 struct bch_opts opts;
3172};
3173
3174static void bch2_fsck_thread_exit(struct thread_with_stdio *_thr)
3175{
3176 struct fsck_thread *thr = container_of(_thr, struct fsck_thread, thr);
3177 kfree(thr);
3178}
3179
3180static int bch2_fsck_offline_thread_fn(struct thread_with_stdio *stdio)
3181{
3182 struct fsck_thread *thr = container_of(stdio, struct fsck_thread, thr);
3183 struct bch_fs *c = thr->c;
3184
3185 int ret = PTR_ERR_OR_ZERO(c);
3186 if (ret)
3187 return ret;
3188
3189 ret = bch2_fs_start(thr->c);
3190 if (ret)
3191 goto err;
3192
3193 if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
3194 bch2_stdio_redirect_printf(&stdio->stdio, false, "%s: errors fixed\n", c->name);
3195 ret |= 1;
3196 }
3197 if (test_bit(BCH_FS_error, &c->flags)) {
3198 bch2_stdio_redirect_printf(&stdio->stdio, false, "%s: still has errors\n", c->name);
3199 ret |= 4;
3200 }
3201err:
3202 bch2_fs_stop(c);
3203 return ret;
3204}
3205
3206static const struct thread_with_stdio_ops bch2_offline_fsck_ops = {
3207 .exit = bch2_fsck_thread_exit,
3208 .fn = bch2_fsck_offline_thread_fn,
3209};
3210
3211long bch2_ioctl_fsck_offline(struct bch_ioctl_fsck_offline __user *user_arg)
3212{
3213 struct bch_ioctl_fsck_offline arg;
3214 struct fsck_thread *thr = NULL;
a349868b 3215 darray_const_str devs = {};
e5ea0529
KO
3216 long ret = 0;
3217
3218 if (copy_from_user(&arg, user_arg, sizeof(arg)))
3219 return -EFAULT;
3220
3221 if (arg.flags)
3222 return -EINVAL;
3223
3224 if (!capable(CAP_SYS_ADMIN))
3225 return -EPERM;
3226
3227 for (size_t i = 0; i < arg.nr_devs; i++) {
3228 u64 dev_u64;
3229 ret = copy_from_user_errcode(&dev_u64, &user_arg->devs[i], sizeof(u64));
3230 if (ret)
3231 goto err;
3232
3233 char *dev_str = strndup_user((char __user *)(unsigned long) dev_u64, PATH_MAX);
3234 ret = PTR_ERR_OR_ZERO(dev_str);
3235 if (ret)
3236 goto err;
3237
3238 ret = darray_push(&devs, dev_str);
3239 if (ret) {
3240 kfree(dev_str);
3241 goto err;
3242 }
3243 }
3244
3245 thr = kzalloc(sizeof(*thr), GFP_KERNEL);
3246 if (!thr) {
3247 ret = -ENOMEM;
3248 goto err;
3249 }
3250
3251 thr->opts = bch2_opts_empty();
3252
3253 if (arg.opts) {
3254 char *optstr = strndup_user((char __user *)(unsigned long) arg.opts, 1 << 16);
3255 ret = PTR_ERR_OR_ZERO(optstr) ?:
a7cdf227 3256 bch2_parse_mount_opts(NULL, &thr->opts, NULL, optstr, false);
e5ea0529
KO
3257 if (!IS_ERR(optstr))
3258 kfree(optstr);
3259
3260 if (ret)
3261 goto err;
3262 }
3263
3264 opt_set(thr->opts, stdio, (u64)(unsigned long)&thr->thr.stdio);
3265 opt_set(thr->opts, read_only, 1);
3266 opt_set(thr->opts, ratelimit_errors, 0);
3267
3268 /* We need request_key() to be called before we punt to kthread: */
3269 opt_set(thr->opts, nostart, true);
3270
3271 bch2_thread_with_stdio_init(&thr->thr, &bch2_offline_fsck_ops);
3272
a349868b 3273 thr->c = bch2_fs_open(&devs, &thr->opts);
e5ea0529
KO
3274
3275 if (!IS_ERR(thr->c) &&
3276 thr->c->opts.errors == BCH_ON_ERROR_panic)
3277 thr->c->opts.errors = BCH_ON_ERROR_ro;
3278
3279 ret = __bch2_run_thread_with_stdio(&thr->thr);
3280out:
3281 darray_for_each(devs, i)
3282 kfree(*i);
3283 darray_exit(&devs);
3284 return ret;
3285err:
3286 if (thr)
3287 bch2_fsck_thread_exit(&thr->thr);
3288 pr_err("ret %s", bch2_err_str(ret));
3289 goto out;
3290}
3291
3292static int bch2_fsck_online_thread_fn(struct thread_with_stdio *stdio)
3293{
3294 struct fsck_thread *thr = container_of(stdio, struct fsck_thread, thr);
3295 struct bch_fs *c = thr->c;
3296
3297 c->stdio_filter = current;
3298 c->stdio = &thr->thr.stdio;
3299
3300 /*
3301 * XXX: can we figure out a way to do this without mucking with c->opts?
3302 */
3303 unsigned old_fix_errors = c->opts.fix_errors;
3304 if (opt_defined(thr->opts, fix_errors))
3305 c->opts.fix_errors = thr->opts.fix_errors;
3306 else
3307 c->opts.fix_errors = FSCK_FIX_ask;
3308
3309 c->opts.fsck = true;
367cad09 3310 set_bit(BCH_FS_in_fsck, &c->flags);
e5ea0529 3311
ab355520 3312 int ret = bch2_run_online_recovery_passes(c, ~0ULL);
e5ea0529 3313
367cad09 3314 clear_bit(BCH_FS_in_fsck, &c->flags);
e5ea0529
KO
3315 bch_err_fn(c, ret);
3316
3317 c->stdio = NULL;
3318 c->stdio_filter = NULL;
3319 c->opts.fix_errors = old_fix_errors;
3320
68708efc 3321 up(&c->recovery.run_lock);
e5ea0529
KO
3322 bch2_ro_ref_put(c);
3323 return ret;
3324}
3325
3326static const struct thread_with_stdio_ops bch2_online_fsck_ops = {
3327 .exit = bch2_fsck_thread_exit,
3328 .fn = bch2_fsck_online_thread_fn,
3329};
3330
3331long bch2_ioctl_fsck_online(struct bch_fs *c, struct bch_ioctl_fsck_online arg)
3332{
3333 struct fsck_thread *thr = NULL;
3334 long ret = 0;
3335
3336 if (arg.flags)
3337 return -EINVAL;
3338
3339 if (!capable(CAP_SYS_ADMIN))
3340 return -EPERM;
3341
3342 if (!bch2_ro_ref_tryget(c))
3343 return -EROFS;
3344
68708efc 3345 if (down_trylock(&c->recovery.run_lock)) {
e5ea0529
KO
3346 bch2_ro_ref_put(c);
3347 return -EAGAIN;
3348 }
3349
3350 thr = kzalloc(sizeof(*thr), GFP_KERNEL);
3351 if (!thr) {
3352 ret = -ENOMEM;
3353 goto err;
3354 }
3355
3356 thr->c = c;
3357 thr->opts = bch2_opts_empty();
3358
3359 if (arg.opts) {
3360 char *optstr = strndup_user((char __user *)(unsigned long) arg.opts, 1 << 16);
3361
3362 ret = PTR_ERR_OR_ZERO(optstr) ?:
a7cdf227 3363 bch2_parse_mount_opts(c, &thr->opts, NULL, optstr, false);
e5ea0529
KO
3364 if (!IS_ERR(optstr))
3365 kfree(optstr);
3366
3367 if (ret)
3368 goto err;
3369 }
3370
3371 ret = bch2_run_thread_with_stdio(&thr->thr, &bch2_online_fsck_ops);
3372err:
3373 if (ret < 0) {
3374 bch_err_fn(c, ret);
3375 if (thr)
3376 bch2_fsck_thread_exit(&thr->thr);
68708efc 3377 up(&c->recovery.run_lock);
e5ea0529
KO
3378 bch2_ro_ref_put(c);
3379 }
3380 return ret;
3381}
c9b9afe7
KO
3382
3383#endif /* NO_BCACHEFS_CHARDEV */