1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
15 #include <linux/dcache.h> /* struct qstr */
16 #include <linux/generic-radix-tree.h>
18 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
20 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum)
22 struct btree_iter *iter;
27 for_each_btree_key(trans, iter, BTREE_ID_extents,
28 POS(inum, 0), 0, k, ret) {
29 if (k.k->p.inode != inum)
32 if (bkey_extent_is_allocation(k.k))
36 bch2_trans_iter_free(trans, iter);
38 return ret ?: sectors;
41 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
42 struct bch_inode_unpacked *inode,
45 struct btree_iter *iter;
49 iter = bch2_trans_get_iter(trans, BTREE_ID_inodes,
51 k = bch2_btree_iter_peek_slot(iter);
57 *snapshot = iter->pos.snapshot;
58 ret = k.k->type == KEY_TYPE_inode
59 ? bch2_inode_unpack(bkey_s_c_to_inode(k), inode)
62 bch2_trans_iter_free(trans, iter);
66 static int write_inode(struct btree_trans *trans,
67 struct bch_inode_unpacked *inode,
70 struct btree_iter *inode_iter =
71 bch2_trans_get_iter(trans, BTREE_ID_inodes,
72 SPOS(0, inode->bi_inum, snapshot),
74 int ret = __bch2_trans_do(trans, NULL, NULL,
77 bch2_inode_write(trans, inode_iter, inode));
78 bch2_trans_iter_put(trans, inode_iter);
80 bch_err(trans->c, "error in fsck: error %i updating inode", ret);
84 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
86 struct bch_fs *c = trans->c;
87 struct btree_iter *iter;
88 struct bch_inode_unpacked dir_inode;
89 struct bch_hash_info dir_hash_info;
92 ret = lookup_inode(trans, pos.inode, &dir_inode, NULL);
96 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
98 iter = bch2_trans_get_iter(trans, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
100 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
101 &dir_hash_info, iter);
102 bch2_trans_iter_put(trans, iter);
106 static int remove_dirent(struct btree_trans *trans, struct bpos pos)
108 int ret = __bch2_trans_do(trans, NULL, NULL,
110 BTREE_INSERT_LAZY_RW,
111 __remove_dirent(trans, pos));
113 bch_err(trans->c, "remove_dirent: err %i deleting dirent", ret);
117 static int __reattach_inode(struct btree_trans *trans,
118 struct bch_inode_unpacked *lostfound,
121 struct bch_hash_info dir_hash =
122 bch2_hash_info_init(trans->c, lostfound);
123 struct bch_inode_unpacked inode_u;
130 snprintf(name_buf, sizeof(name_buf), "%llu", inum);
131 name = (struct qstr) QSTR(name_buf);
133 ret = lookup_inode(trans, inum, &inode_u, &snapshot);
137 if (S_ISDIR(inode_u.bi_mode)) {
138 lostfound->bi_nlink++;
140 ret = write_inode(trans, lostfound, U32_MAX);
145 ret = bch2_dirent_create(trans, lostfound->bi_inum, &dir_hash,
146 mode_to_type(inode_u.bi_mode),
147 &name, inum, &dir_offset,
148 BCH_HASH_SET_MUST_CREATE);
152 inode_u.bi_dir = lostfound->bi_inum;
153 inode_u.bi_dir_offset = dir_offset;
155 return write_inode(trans, &inode_u, U32_MAX);
158 static int reattach_inode(struct btree_trans *trans,
159 struct bch_inode_unpacked *lostfound,
162 int ret = __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
163 __reattach_inode(trans, lostfound, inum));
165 bch_err(trans->c, "error %i reattaching inode %llu", ret, inum);
170 static int remove_backpointer(struct btree_trans *trans,
171 struct bch_inode_unpacked *inode)
173 struct btree_iter *iter;
177 iter = bch2_trans_get_iter(trans, BTREE_ID_dirents,
178 POS(inode->bi_dir, inode->bi_dir_offset), 0);
179 k = bch2_btree_iter_peek_slot(iter);
183 if (k.k->type != KEY_TYPE_dirent) {
188 ret = remove_dirent(trans, k.k->p);
190 bch2_trans_iter_put(trans, iter);
194 struct inode_walker {
195 bool first_this_inode;
199 struct bch_inode_unpacked inode;
202 static struct inode_walker inode_walker_init(void)
204 return (struct inode_walker) {
210 static int walk_inode(struct btree_trans *trans,
211 struct inode_walker *w, u64 inum)
213 if (inum != w->cur_inum) {
214 int ret = lookup_inode(trans, inum, &w->inode, &w->snapshot);
216 if (ret && ret != -ENOENT)
219 w->have_inode = !ret;
221 w->first_this_inode = true;
223 w->first_this_inode = false;
229 static int hash_redo_key(struct btree_trans *trans,
230 const struct bch_hash_desc desc,
231 struct bch_hash_info *hash_info,
232 struct btree_iter *k_iter, struct bkey_s_c k)
234 struct bkey_i delete;
237 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
241 bkey_reassemble(tmp, k);
243 bkey_init(&delete.k);
244 delete.k.p = k_iter->pos;
245 bch2_trans_update(trans, k_iter, &delete, 0);
247 return bch2_hash_set(trans, desc, hash_info, k_iter->pos.inode,
251 static int fsck_hash_delete_at(struct btree_trans *trans,
252 const struct bch_hash_desc desc,
253 struct bch_hash_info *info,
254 struct btree_iter *iter)
258 ret = bch2_hash_delete_at(trans, desc, info, iter) ?:
259 bch2_trans_commit(trans, NULL, NULL,
261 BTREE_INSERT_LAZY_RW);
263 ret = bch2_btree_iter_traverse(iter);
271 static int hash_check_key(struct btree_trans *trans,
272 const struct bch_hash_desc desc,
273 struct bch_hash_info *hash_info,
274 struct btree_iter *k_iter, struct bkey_s_c hash_k)
276 struct bch_fs *c = trans->c;
277 struct btree_iter *iter = NULL;
283 if (hash_k.k->type != desc.key_type)
286 hash = desc.hash_bkey(hash_info, hash_k);
288 if (likely(hash == hash_k.k->p.offset))
291 if (hash_k.k->p.offset < hash)
294 for_each_btree_key(trans, iter, desc.btree_id, POS(hash_k.k->p.inode, hash),
295 BTREE_ITER_SLOTS, k, ret) {
296 if (!bkey_cmp(k.k->p, hash_k.k->p))
299 if (fsck_err_on(k.k->type == desc.key_type &&
300 !desc.cmp_bkey(k, hash_k), c,
301 "duplicate hash table keys:\n%s",
302 (bch2_bkey_val_to_text(&PBUF(buf), c,
304 ret = fsck_hash_delete_at(trans, desc, hash_info, k_iter);
311 if (bkey_deleted(k.k)) {
312 bch2_trans_iter_free(trans, iter);
317 bch2_trans_iter_free(trans, iter);
320 if (fsck_err(c, "hash table key at wrong offset: btree %u inode %llu offset %llu, "
321 "hashed to %llu should be at %llu\n%s",
322 desc.btree_id, hash_k.k->p.inode, hash_k.k->p.offset,
323 hash, iter->pos.offset,
324 (bch2_bkey_val_to_text(&PBUF(buf), c, hash_k), buf)) == FSCK_ERR_IGNORE)
327 ret = __bch2_trans_do(trans, NULL, NULL,
328 BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
329 hash_redo_key(trans, desc, hash_info, k_iter, hash_k));
331 bch_err(c, "hash_redo_key err %i", ret);
339 static int check_inode(struct btree_trans *trans,
340 struct btree_iter *iter,
341 struct bkey_s_c_inode inode)
343 struct bch_fs *c = trans->c;
344 struct bch_inode_unpacked u;
345 bool do_update = false;
348 ret = bch2_inode_unpack(inode, &u);
350 if (bch2_fs_inconsistent_on(ret, c,
351 "error unpacking inode %llu in fsck",
355 if (u.bi_flags & BCH_INODE_UNLINKED &&
357 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
359 bch_verbose(c, "deleting inode %llu", u.bi_inum);
361 bch2_trans_unlock(trans);
364 ret = bch2_inode_rm(c, u.bi_inum, false);
366 bch_err(c, "error in fsck: error %i while deleting inode", ret);
370 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
372 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
374 bch_verbose(c, "truncating inode %llu", u.bi_inum);
376 bch2_trans_unlock(trans);
380 * XXX: need to truncate partial blocks too here - or ideally
381 * just switch units to bytes and that issue goes away
383 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
384 POS(u.bi_inum, round_up(u.bi_size, block_bytes(c))),
385 POS(u.bi_inum, U64_MAX),
388 bch_err(c, "error in fsck: error %i truncating inode", ret);
393 * We truncated without our normal sector accounting hook, just
394 * make sure we recalculate it:
396 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
398 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
402 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
404 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
408 bch_verbose(c, "recounting sectors for inode %llu",
411 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
413 bch_err(c, "error in fsck: error %i recounting inode sectors",
418 u.bi_sectors = sectors;
419 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
423 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
426 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
431 ret = __bch2_trans_do(trans, NULL, NULL,
433 BTREE_INSERT_LAZY_RW,
434 bch2_inode_write(trans, iter, &u));
436 bch_err(c, "error in fsck: error %i "
437 "updating inode", ret);
444 static int check_inodes(struct bch_fs *c, bool full)
446 struct btree_trans trans;
447 struct btree_iter *iter;
449 struct bkey_s_c_inode inode;
452 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
454 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN, 0, k, ret) {
455 if (k.k->type != KEY_TYPE_inode)
458 inode = bkey_s_c_to_inode(k);
461 (inode.v->bi_flags & (BCH_INODE_I_SIZE_DIRTY|
462 BCH_INODE_I_SECTORS_DIRTY|
463 BCH_INODE_UNLINKED))) {
464 ret = check_inode(&trans, iter, inode);
469 bch2_trans_iter_put(&trans, iter);
471 BUG_ON(ret == -EINTR);
473 return bch2_trans_exit(&trans) ?: ret;
476 static int fix_overlapping_extent(struct btree_trans *trans,
477 struct bkey_s_c k, struct bpos cut_at)
479 struct btree_iter *iter;
483 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
484 ret = PTR_ERR_OR_ZERO(u);
488 bkey_reassemble(u, k);
489 bch2_cut_front(cut_at, u);
493 * We don't want to go through the extent_handle_overwrites path:
495 * XXX: this is going to screw up disk accounting, extent triggers
496 * assume things about extent overwrites - we should be running the
497 * triggers manually here
499 iter = bch2_trans_get_iter(trans, BTREE_ID_extents, u->k.p,
500 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
502 BUG_ON(iter->flags & BTREE_ITER_IS_EXTENTS);
503 bch2_trans_update(trans, iter, u, BTREE_TRIGGER_NORUN);
504 bch2_trans_iter_put(trans, iter);
506 return bch2_trans_commit(trans, NULL, NULL,
508 BTREE_INSERT_LAZY_RW);
511 static int inode_backpointer_exists(struct btree_trans *trans,
512 struct bch_inode_unpacked *inode)
514 struct btree_iter *iter;
518 iter = bch2_trans_get_iter(trans, BTREE_ID_dirents,
519 POS(inode->bi_dir, inode->bi_dir_offset), 0);
520 k = bch2_btree_iter_peek_slot(iter);
524 if (k.k->type != KEY_TYPE_dirent)
527 ret = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum) == inode->bi_inum;
529 bch2_trans_iter_free(trans, iter);
533 static bool inode_backpointer_matches(struct bkey_s_c_dirent d,
534 struct bch_inode_unpacked *inode)
536 return d.k->p.inode == inode->bi_dir &&
537 d.k->p.offset == inode->bi_dir_offset;
541 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
542 * that i_size an i_sectors are consistent
545 static int check_extents(struct bch_fs *c)
547 struct inode_walker w = inode_walker_init();
548 struct btree_trans trans;
549 struct btree_iter *iter;
551 struct bkey_buf prev;
555 bch2_bkey_buf_init(&prev);
556 prev.k->k = KEY(0, 0, 0);
557 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
559 bch_verbose(c, "checking extents");
561 iter = bch2_trans_get_iter(&trans, BTREE_ID_extents,
562 POS(BCACHEFS_ROOT_INO, 0),
565 while ((k = bch2_btree_iter_peek(iter)).k &&
566 !(ret = bkey_err(k))) {
568 w.cur_inum != k.k->p.inode &&
569 !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
570 fsck_err_on(w.inode.bi_sectors != i_sectors, c,
571 "inode %llu has incorrect i_sectors: got %llu, should be %llu",
573 w.inode.bi_sectors, i_sectors)) {
574 w.inode.bi_sectors = i_sectors;
576 ret = write_inode(&trans, &w.inode, w.snapshot);
581 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
585 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
586 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
588 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2))
589 return fix_overlapping_extent(&trans, k, prev.k->k.p) ?: -EINTR;
592 ret = walk_inode(&trans, &w, k.k->p.inode);
596 if (w.first_this_inode)
599 if (fsck_err_on(!w.have_inode, c,
600 "extent type %u for missing inode %llu",
601 k.k->type, k.k->p.inode) ||
602 fsck_err_on(w.have_inode &&
603 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
604 "extent type %u for non regular file, inode %llu mode %o",
605 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
607 return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
608 POS(k.k->p.inode, 0),
609 POS(k.k->p.inode, U64_MAX),
613 if (fsck_err_on(w.have_inode &&
614 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
615 k.k->type != KEY_TYPE_reservation &&
616 k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
617 "extent type %u offset %llu past end of inode %llu, i_size %llu",
618 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
620 return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
621 POS(k.k->p.inode, round_up(w.inode.bi_size, block_bytes(c))),
622 POS(k.k->p.inode, U64_MAX),
626 if (bkey_extent_is_allocation(k.k))
627 i_sectors += k.k->size;
628 bch2_bkey_buf_reassemble(&prev, c, k);
630 bch2_btree_iter_advance(iter);
635 bch2_trans_iter_put(&trans, iter);
636 bch2_bkey_buf_exit(&prev, c);
637 return bch2_trans_exit(&trans) ?: ret;
641 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
645 static int check_dirents(struct bch_fs *c)
647 struct inode_walker w = inode_walker_init();
648 struct bch_hash_info hash_info;
649 struct btree_trans trans;
650 struct btree_iter *iter;
653 unsigned nr_subdirs = 0;
656 bch_verbose(c, "checking dirents");
658 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
660 iter = bch2_trans_get_iter(&trans, BTREE_ID_dirents,
661 POS(BCACHEFS_ROOT_INO, 0), 0);
663 while ((k = bch2_btree_iter_peek(iter)).k &&
664 !(ret = bkey_err(k))) {
665 struct bkey_s_c_dirent d;
666 struct bch_inode_unpacked target;
669 bool backpointer_exists = true;
673 w.cur_inum != k.k->p.inode &&
674 fsck_err_on(w.inode.bi_nlink != nr_subdirs, c,
675 "directory %llu with wrong i_nlink: got %u, should be %u",
676 w.inode.bi_inum, w.inode.bi_nlink, nr_subdirs)) {
677 w.inode.bi_nlink = nr_subdirs;
678 ret = write_inode(&trans, &w.inode, w.snapshot);
683 ret = walk_inode(&trans, &w, k.k->p.inode);
687 if (w.first_this_inode)
690 if (fsck_err_on(!w.have_inode, c,
691 "dirent in nonexisting directory:\n%s",
692 (bch2_bkey_val_to_text(&PBUF(buf), c,
694 fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
695 "dirent in non directory inode type %u:\n%s",
696 mode_to_type(w.inode.bi_mode),
697 (bch2_bkey_val_to_text(&PBUF(buf), c,
699 ret = lockrestart_do(&trans,
700 bch2_btree_delete_at(&trans, iter, 0));
709 if (w.first_this_inode)
710 hash_info = bch2_hash_info_init(c, &w.inode);
712 ret = hash_check_key(&trans, bch2_dirent_hash_desc,
713 &hash_info, iter, k);
721 if (k.k->type != KEY_TYPE_dirent)
724 d = bkey_s_c_to_dirent(k);
725 d_inum = le64_to_cpu(d.v->d_inum);
727 ret = lookup_inode(&trans, d_inum, &target, &target_snapshot);
728 if (ret && ret != -ENOENT)
734 if (fsck_err_on(!have_target, c,
735 "dirent points to missing inode:\n%s",
736 (bch2_bkey_val_to_text(&PBUF(buf), c,
738 ret = remove_dirent(&trans, d.k->p);
747 if (!target.bi_dir &&
748 !target.bi_dir_offset) {
749 target.bi_dir = k.k->p.inode;
750 target.bi_dir_offset = k.k->p.offset;
752 ret = write_inode(&trans, &target, target_snapshot);
757 if (!inode_backpointer_matches(d, &target)) {
758 ret = inode_backpointer_exists(&trans, &target);
762 backpointer_exists = ret;
765 if (fsck_err_on(S_ISDIR(target.bi_mode) &&
766 backpointer_exists, c,
767 "directory %llu with multiple links",
769 ret = remove_dirent(&trans, d.k->p);
775 if (fsck_err_on(backpointer_exists &&
777 "inode %llu has multiple links but i_nlink 0",
780 target.bi_flags &= ~BCH_INODE_UNLINKED;
782 ret = write_inode(&trans, &target, target_snapshot);
787 if (fsck_err_on(!backpointer_exists, c,
788 "inode %llu has wrong backpointer:\n"
790 "should be %llu:%llu",
793 target.bi_dir_offset,
796 target.bi_dir = k.k->p.inode;
797 target.bi_dir_offset = k.k->p.offset;
799 ret = write_inode(&trans, &target, target_snapshot);
805 if (fsck_err_on(d.v->d_type != mode_to_type(target.bi_mode), c,
806 "incorrect d_type: should be %u:\n%s",
807 mode_to_type(target.bi_mode),
808 (bch2_bkey_val_to_text(&PBUF(buf), c,
810 struct bkey_i_dirent *n;
812 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
818 bkey_reassemble(&n->k_i, d.s_c);
819 n->v.d_type = mode_to_type(target.bi_mode);
821 ret = __bch2_trans_do(&trans, NULL, NULL,
823 BTREE_INSERT_LAZY_RW,
824 (bch2_trans_update(&trans, iter, &n->k_i, 0), 0));
831 nr_subdirs += d.v->d_type == DT_DIR;
833 bch2_btree_iter_advance(iter);
840 bch2_trans_iter_put(&trans, iter);
841 return bch2_trans_exit(&trans) ?: ret;
845 * Walk xattrs: verify that they all have a corresponding inode
848 static int check_xattrs(struct bch_fs *c)
850 struct inode_walker w = inode_walker_init();
851 struct bch_hash_info hash_info;
852 struct btree_trans trans;
853 struct btree_iter *iter;
857 bch_verbose(c, "checking xattrs");
859 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
861 iter = bch2_trans_get_iter(&trans, BTREE_ID_xattrs,
862 POS(BCACHEFS_ROOT_INO, 0), 0);
864 while ((k = bch2_btree_iter_peek(iter)).k &&
865 !(ret = bkey_err(k))) {
866 ret = walk_inode(&trans, &w, k.k->p.inode);
870 if (fsck_err_on(!w.have_inode, c,
871 "xattr for missing inode %llu",
873 ret = bch2_btree_delete_at(&trans, iter, 0);
879 if (w.first_this_inode && w.have_inode)
880 hash_info = bch2_hash_info_init(c, &w.inode);
882 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
883 &hash_info, iter, k);
887 bch2_btree_iter_advance(iter);
893 bch2_trans_iter_put(&trans, iter);
894 return bch2_trans_exit(&trans) ?: ret;
897 /* Get root directory, create if it doesn't exist: */
898 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
900 struct bkey_inode_buf packed;
904 bch_verbose(c, "checking root directory");
906 ret = bch2_trans_do(c, NULL, NULL, 0,
907 lookup_inode(&trans, BCACHEFS_ROOT_INO, root_inode, &snapshot));
908 if (ret && ret != -ENOENT)
911 if (fsck_err_on(ret, c, "root directory missing"))
914 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
915 "root inode not a directory"))
922 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
924 root_inode->bi_inum = BCACHEFS_ROOT_INO;
926 bch2_inode_pack(c, &packed, root_inode);
928 return bch2_btree_insert(c, BTREE_ID_inodes, &packed.inode.k_i,
931 BTREE_INSERT_LAZY_RW);
934 /* Get lost+found, create if it doesn't exist: */
935 static int check_lostfound(struct bch_fs *c,
936 struct bch_inode_unpacked *root_inode,
937 struct bch_inode_unpacked *lostfound_inode)
939 struct qstr lostfound = QSTR("lost+found");
940 struct bch_hash_info root_hash_info =
941 bch2_hash_info_init(c, root_inode);
946 bch_verbose(c, "checking lost+found");
948 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
951 bch_notice(c, "creating lost+found");
952 goto create_lostfound;
955 ret = bch2_trans_do(c, NULL, NULL, 0,
956 lookup_inode(&trans, inum, lostfound_inode, &snapshot));
957 if (ret && ret != -ENOENT)
960 if (fsck_err_on(ret, c, "lost+found missing"))
961 goto create_lostfound;
963 if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
964 "lost+found inode not a directory"))
965 goto create_lostfound;
971 bch2_inode_init_early(c, lostfound_inode);
973 ret = bch2_trans_do(c, NULL, NULL,
975 BTREE_INSERT_LAZY_RW,
976 bch2_create_trans(&trans,
977 BCACHEFS_ROOT_INO, root_inode,
978 lostfound_inode, &lostfound,
979 0, 0, S_IFDIR|0700, 0, NULL, NULL));
981 bch_err(c, "error creating lost+found: %i", ret);
990 struct pathbuf_entry {
995 static int path_down(struct pathbuf *p, u64 inum)
997 if (p->nr == p->size) {
998 size_t new_size = max_t(size_t, 256UL, p->size * 2);
999 void *n = krealloc(p->entries,
1000 new_size * sizeof(p->entries[0]),
1010 p->entries[p->nr++] = (struct pathbuf_entry) {
1016 static int check_path(struct btree_trans *trans,
1017 struct bch_inode_unpacked *lostfound,
1019 struct bch_inode_unpacked *inode)
1021 struct bch_fs *c = trans->c;
1028 while (inode->bi_inum != BCACHEFS_ROOT_INO) {
1029 ret = lockrestart_do(trans,
1030 inode_backpointer_exists(trans, inode));
1035 if (fsck_err(c, "unreachable inode %llu, type %u nlink %u backptr %llu:%llu",
1037 mode_to_type(inode->bi_mode),
1040 inode->bi_dir_offset))
1041 ret = reattach_inode(trans, lostfound, inode->bi_inum);
1046 if (!S_ISDIR(inode->bi_mode))
1049 ret = path_down(p, inode->bi_inum);
1051 bch_err(c, "memory allocation failure");
1055 for (i = 0; i < p->nr; i++) {
1056 if (inode->bi_dir != p->entries[i].inum)
1059 /* XXX print path */
1060 if (!fsck_err(c, "directory structure loop"))
1063 ret = lockrestart_do(trans,
1064 remove_backpointer(trans, inode));
1066 bch_err(c, "error removing dirent: %i", ret);
1070 ret = reattach_inode(trans, lostfound, inode->bi_inum);
1074 ret = lockrestart_do(trans,
1075 lookup_inode(trans, inode->bi_dir, inode, &snapshot));
1077 /* Should have been caught in dirents pass */
1078 bch_err(c, "error looking up parent directory: %i", ret);
1084 bch_err(c, "%s: err %i", __func__, ret);
1089 * Check for unreachable inodes, as well as loops in the directory structure:
1090 * After check_dirents(), if an inode backpointer doesn't exist that means it's
1093 static int check_directory_structure(struct bch_fs *c,
1094 struct bch_inode_unpacked *lostfound)
1096 struct btree_trans trans;
1097 struct btree_iter *iter;
1099 struct bch_inode_unpacked u;
1100 struct pathbuf path = { 0, 0, NULL };
1103 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1105 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN, 0, k, ret) {
1106 if (k.k->type != KEY_TYPE_inode)
1109 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), &u);
1111 /* Should have been caught earlier in fsck: */
1112 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
1116 ret = check_path(&trans, lostfound, &path, &u);
1120 bch2_trans_iter_put(&trans, iter);
1122 BUG_ON(ret == -EINTR);
1124 kfree(path.entries);
1126 return bch2_trans_exit(&trans) ?: ret;
1133 typedef GENRADIX(struct nlink) nlink_table;
1135 static void inc_link(struct bch_fs *c, nlink_table *links,
1136 u64 range_start, u64 *range_end, u64 inum)
1140 if (inum < range_start || inum >= *range_end)
1143 if (inum - range_start >= SIZE_MAX / sizeof(struct nlink)) {
1148 link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
1150 bch_verbose(c, "allocation failed during fsck - will need another pass");
1159 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
1160 u64 range_start, u64 *range_end)
1162 struct btree_trans trans;
1163 struct btree_iter *iter;
1165 struct bkey_s_c_dirent d;
1168 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1170 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN, 0, k, ret) {
1171 switch (k.k->type) {
1172 case KEY_TYPE_dirent:
1173 d = bkey_s_c_to_dirent(k);
1175 if (d.v->d_type != DT_DIR)
1176 inc_link(c, links, range_start, range_end,
1177 le64_to_cpu(d.v->d_inum));
1181 bch2_trans_cond_resched(&trans);
1183 bch2_trans_iter_put(&trans, iter);
1185 ret = bch2_trans_exit(&trans) ?: ret;
1187 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
1192 static int check_inode_nlink(struct btree_trans *trans,
1193 struct bch_inode_unpacked *lostfound_inode,
1194 struct btree_iter *iter,
1195 struct bkey_s_c_inode inode,
1198 struct bch_fs *c = trans->c;
1199 struct bch_inode_unpacked u;
1203 * Backpointer and directory structure checks are sufficient for
1204 * directories, since they can't have hardlinks:
1206 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
1210 bch_err(c, "no links found to inode %llu", inode.k->p.offset);
1214 ret = bch2_inode_unpack(inode, &u);
1216 /* Should never happen, checked by bch2_inode_invalid: */
1217 if (bch2_fs_inconsistent_on(ret, c,
1218 "error unpacking inode %llu in fsck",
1222 if (fsck_err_on(bch2_inode_nlink_get(&u) != nlink, c,
1223 "inode %llu has wrong i_nlink (type %u i_nlink %u, should be %u)",
1224 u.bi_inum, mode_to_type(u.bi_mode),
1225 bch2_inode_nlink_get(&u), nlink)) {
1226 bch2_inode_nlink_set(&u, nlink);
1228 ret = __bch2_trans_do(trans, NULL, NULL,
1229 BTREE_INSERT_NOFAIL|
1230 BTREE_INSERT_LAZY_RW,
1231 bch2_inode_write(trans, iter, &u));
1233 bch_err(c, "error in fsck: error %i updating inode", ret);
1240 static int bch2_gc_walk_inodes(struct bch_fs *c,
1241 struct bch_inode_unpacked *lostfound_inode,
1243 u64 range_start, u64 range_end)
1245 struct btree_trans trans;
1246 struct btree_iter *iter;
1251 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1253 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
1254 POS(0, range_start), 0, k, ret) {
1255 if (!k.k || k.k->p.offset >= range_end)
1258 if (k.k->type != KEY_TYPE_inode)
1261 link = genradix_ptr(links, k.k->p.offset - range_start);
1262 ret = check_inode_nlink(&trans, lostfound_inode, iter,
1263 bkey_s_c_to_inode(k), link ? link->count : 0);
1268 bch2_trans_iter_put(&trans, iter);
1269 bch2_trans_exit(&trans);
1272 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
1278 static int check_nlinks(struct bch_fs *c,
1279 struct bch_inode_unpacked *lostfound_inode)
1282 u64 this_iter_range_start, next_iter_range_start = 0;
1285 bch_verbose(c, "checking inode nlinks");
1287 genradix_init(&links);
1290 this_iter_range_start = next_iter_range_start;
1291 next_iter_range_start = U64_MAX;
1293 ret = bch2_gc_walk_dirents(c, &links,
1294 this_iter_range_start,
1295 &next_iter_range_start);
1299 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1300 this_iter_range_start,
1301 next_iter_range_start);
1305 genradix_free(&links);
1306 } while (next_iter_range_start != U64_MAX);
1308 genradix_free(&links);
1314 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1315 * Doesn't fix them yet, mainly because they haven't yet been observed:
1317 int bch2_fsck_full(struct bch_fs *c)
1319 struct bch_inode_unpacked root_inode, lostfound_inode;
1321 return check_inodes(c, true) ?:
1325 check_root(c, &root_inode) ?:
1326 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1327 check_directory_structure(c, &lostfound_inode) ?:
1328 check_nlinks(c, &lostfound_inode);
1331 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1333 return check_inodes(c, false);