1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_update.h"
14 #include <linux/dcache.h> /* struct qstr */
15 #include <linux/generic-radix-tree.h>
17 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
19 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum)
21 struct btree_iter *iter;
26 for_each_btree_key(trans, iter, BTREE_ID_EXTENTS,
27 POS(inum, 0), 0, k, ret) {
28 if (k.k->p.inode != inum)
31 if (bkey_extent_is_allocation(k.k))
35 bch2_trans_iter_free(trans, iter);
37 return ret ?: sectors;
40 static int remove_dirent(struct btree_trans *trans,
41 struct bkey_s_c_dirent dirent)
43 struct bch_fs *c = trans->c;
45 struct bch_inode_unpacked dir_inode;
46 struct bch_hash_info dir_hash_info;
47 u64 dir_inum = dirent.k->p.inode;
51 name.len = bch2_dirent_name_bytes(dirent);
52 buf = kmalloc(name.len + 1, GFP_KERNEL);
56 memcpy(buf, dirent.v->d_name, name.len);
60 /* Unlock so we don't deadlock, after copying name: */
61 bch2_trans_unlock(trans);
63 ret = bch2_inode_find_by_inum(c, dir_inum, &dir_inode);
65 bch_err(c, "remove_dirent: err %i looking up directory inode", ret);
69 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
71 ret = bch2_dirent_delete(c, dir_inum, &dir_hash_info, &name, NULL);
73 bch_err(c, "remove_dirent: err %i deleting dirent", ret);
79 static int reattach_inode(struct bch_fs *c,
80 struct bch_inode_unpacked *lostfound_inode,
83 struct bch_inode_unpacked inode_u;
88 snprintf(name_buf, sizeof(name_buf), "%llu", inum);
89 name = (struct qstr) QSTR(name_buf);
91 ret = bch2_trans_do(c, NULL,
94 bch2_link_trans(&trans, lostfound_inode->bi_inum,
95 inum, &inode_u, &name));
97 bch_err(c, "error %i reattaching inode %llu", ret, inum);
102 struct inode_walker {
103 bool first_this_inode;
106 struct bch_inode_unpacked inode;
109 static struct inode_walker inode_walker_init(void)
111 return (struct inode_walker) {
117 static int walk_inode(struct btree_trans *trans,
118 struct inode_walker *w, u64 inum)
120 if (inum != w->cur_inum) {
121 int ret = bch2_inode_find_by_inum_trans(trans, inum,
124 if (ret && ret != -ENOENT)
127 w->have_inode = !ret;
129 w->first_this_inode = true;
131 w->first_this_inode = false;
138 struct bch_hash_info info;
140 /* start of current chain of hash collisions: */
141 struct btree_iter *chain;
143 /* next offset in current chain of hash collisions: */
147 static void hash_check_init(struct hash_check *h)
153 static void hash_stop_chain(struct btree_trans *trans,
154 struct hash_check *h)
157 bch2_trans_iter_free(trans, h->chain);
161 static void hash_check_set_inode(struct btree_trans *trans,
162 struct hash_check *h,
163 const struct bch_inode_unpacked *bi)
165 h->info = bch2_hash_info_init(trans->c, bi);
166 hash_stop_chain(trans, h);
169 static int hash_redo_key(const struct bch_hash_desc desc,
170 struct btree_trans *trans, struct hash_check *h,
171 struct btree_iter *k_iter, struct bkey_s_c k,
177 tmp = kmalloc(bkey_bytes(k.k), GFP_KERNEL);
181 bkey_reassemble(tmp, k);
183 ret = bch2_btree_delete_at(trans, k_iter, 0);
187 bch2_hash_set(trans, desc, &h->info, k_iter->pos.inode,
188 tmp, BCH_HASH_SET_MUST_CREATE);
189 ret = bch2_trans_commit(trans, NULL, NULL,
191 BTREE_INSERT_LAZY_RW);
197 static int fsck_hash_delete_at(struct btree_trans *trans,
198 const struct bch_hash_desc desc,
199 struct bch_hash_info *info,
200 struct btree_iter *iter)
204 ret = bch2_hash_delete_at(trans, desc, info, iter) ?:
205 bch2_trans_commit(trans, NULL, NULL,
208 BTREE_INSERT_LAZY_RW);
210 ret = bch2_btree_iter_traverse(iter);
218 static int hash_check_duplicates(struct btree_trans *trans,
219 const struct bch_hash_desc desc, struct hash_check *h,
220 struct btree_iter *k_iter, struct bkey_s_c k)
222 struct bch_fs *c = trans->c;
223 struct btree_iter *iter;
228 if (!bkey_cmp(h->chain->pos, k_iter->pos))
231 iter = bch2_trans_copy_iter(trans, h->chain);
232 BUG_ON(IS_ERR(iter));
234 for_each_btree_key_continue(iter, 0, k2, ret) {
235 if (bkey_cmp(k2.k->p, k.k->p) >= 0)
238 if (fsck_err_on(k2.k->type == desc.key_type &&
239 !desc.cmp_bkey(k, k2), c,
240 "duplicate hash table keys:\n%s",
241 (bch2_bkey_val_to_text(&PBUF(buf), c,
243 ret = fsck_hash_delete_at(trans, desc, &h->info, k_iter);
251 bch2_trans_iter_free(trans, iter);
255 static void hash_set_chain_start(struct btree_trans *trans,
256 const struct bch_hash_desc desc,
257 struct hash_check *h,
258 struct btree_iter *k_iter, struct bkey_s_c k)
260 bool hole = (k.k->type != KEY_TYPE_whiteout &&
261 k.k->type != desc.key_type);
263 if (hole || k.k->p.offset > h->chain_end + 1)
264 hash_stop_chain(trans, h);
268 h->chain = bch2_trans_copy_iter(trans, k_iter);
269 BUG_ON(IS_ERR(h->chain));
272 h->chain_end = k.k->p.offset;
276 static bool key_has_correct_hash(struct btree_trans *trans,
277 const struct bch_hash_desc desc,
278 struct hash_check *h,
279 struct btree_iter *k_iter, struct bkey_s_c k)
283 hash_set_chain_start(trans, desc, h, k_iter, k);
285 if (k.k->type != desc.key_type)
288 hash = desc.hash_bkey(&h->info, k);
290 return hash >= h->chain->pos.offset &&
291 hash <= k.k->p.offset;
294 static int hash_check_key(struct btree_trans *trans,
295 const struct bch_hash_desc desc, struct hash_check *h,
296 struct btree_iter *k_iter, struct bkey_s_c k)
298 struct bch_fs *c = trans->c;
303 hash_set_chain_start(trans, desc, h, k_iter, k);
305 if (k.k->type != desc.key_type)
308 hashed = desc.hash_bkey(&h->info, k);
310 if (fsck_err_on(hashed < h->chain->pos.offset ||
311 hashed > k.k->p.offset, c,
312 "hash table key at wrong offset: btree %u, %llu, "
313 "hashed to %llu chain starts at %llu\n%s",
314 desc.btree_id, k.k->p.offset,
315 hashed, h->chain->pos.offset,
316 (bch2_bkey_val_to_text(&PBUF(buf), c,
318 ret = hash_redo_key(desc, trans, h, k_iter, k, hashed);
320 bch_err(c, "hash_redo_key err %i", ret);
326 ret = hash_check_duplicates(trans, desc, h, k_iter, k);
331 static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h,
332 struct btree_iter *iter, struct bkey_s_c *k)
334 struct bch_fs *c = trans->c;
335 struct bkey_i_dirent *d = NULL;
341 if (key_has_correct_hash(trans, bch2_dirent_hash_desc, h, iter, *k))
344 len = bch2_dirent_name_bytes(bkey_s_c_to_dirent(*k));
347 memcpy(buf, bkey_s_c_to_dirent(*k).v->d_name, len);
350 d = kmalloc(bkey_bytes(k->k), GFP_KERNEL);
352 bch_err(c, "memory allocation failure");
356 bkey_reassemble(&d->k_i, *k);
363 d->k.u64s = BKEY_U64s + dirent_val_u64s(len);
365 BUG_ON(bkey_val_bytes(&d->k) <
366 offsetof(struct bch_dirent, d_name) + len);
368 memset(d->v.d_name + len, 0,
369 bkey_val_bytes(&d->k) -
370 offsetof(struct bch_dirent, d_name) - len);
372 hash = bch2_dirent_hash_desc.hash_bkey(&h->info,
373 bkey_i_to_s_c(&d->k_i));
374 } while (hash < h->chain->pos.offset ||
375 hash > k->k->p.offset);
377 if (fsck_err(c, "dirent with junk at end, was %s (%zu) now %s (%u)",
378 buf, strlen(buf), d->v.d_name, len)) {
379 bch2_trans_update(trans, iter, &d->k_i);
381 ret = bch2_trans_commit(trans, NULL, NULL,
383 BTREE_INSERT_LAZY_RW);
387 *k = bch2_btree_iter_peek(iter);
389 BUG_ON(k->k->type != KEY_TYPE_dirent);
396 hash = bch2_dirent_hash_desc.hash_bkey(&h->info, *k);
398 if (fsck_err(c, "cannot fix dirent by removing trailing garbage %s (%zu)\n"
399 "hash table key at wrong offset: btree %u, offset %llu, "
400 "hashed to %llu chain starts at %llu\n%s",
401 buf, strlen(buf), BTREE_ID_DIRENTS,
402 k->k->p.offset, hash, h->chain->pos.offset,
403 (bch2_bkey_val_to_text(&PBUF(buf), c,
405 ret = hash_redo_key(bch2_dirent_hash_desc, trans,
408 bch_err(c, "hash_redo_key err %i", ret);
416 static int bch2_inode_truncate(struct bch_fs *c, u64 inode_nr, u64 new_size)
418 return bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
419 POS(inode_nr, round_up(new_size, block_bytes(c)) >> 9),
420 POS(inode_nr + 1, 0), NULL);
424 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
425 * that i_size an i_sectors are consistent
428 static int check_extents(struct bch_fs *c)
430 struct inode_walker w = inode_walker_init();
431 struct btree_trans trans;
432 struct btree_iter *iter;
437 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
439 bch_verbose(c, "checking extents");
441 iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
442 POS(BCACHEFS_ROOT_INO, 0), 0);
444 for_each_btree_key_continue(iter, 0, k, ret) {
445 ret = walk_inode(&trans, &w, k.k->p.inode);
449 if (fsck_err_on(!w.have_inode, c,
450 "extent type %u for missing inode %llu",
451 k.k->type, k.k->p.inode) ||
452 fsck_err_on(w.have_inode &&
453 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
454 "extent type %u for non regular file, inode %llu mode %o",
455 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
456 bch2_trans_unlock(&trans);
458 ret = bch2_inode_truncate(c, k.k->p.inode, 0);
464 if (fsck_err_on(w.first_this_inode &&
466 !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
467 w.inode.bi_sectors !=
468 (i_sectors = bch2_count_inode_sectors(&trans, w.cur_inum)),
469 c, "i_sectors wrong: got %llu, should be %llu",
470 w.inode.bi_sectors, i_sectors)) {
471 struct bkey_inode_buf p;
473 w.inode.bi_sectors = i_sectors;
475 bch2_trans_unlock(&trans);
477 bch2_inode_pack(&p, &w.inode);
479 ret = bch2_btree_insert(c, BTREE_ID_INODES,
480 &p.inode.k_i, NULL, NULL,
482 BTREE_INSERT_LAZY_RW);
484 bch_err(c, "error in fsck: error %i updating inode", ret);
488 /* revalidate iterator: */
489 k = bch2_btree_iter_peek(iter);
492 if (fsck_err_on(w.have_inode &&
493 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
494 k.k->type != KEY_TYPE_reservation &&
495 k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
496 "extent type %u offset %llu past end of inode %llu, i_size %llu",
497 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
498 bch2_trans_unlock(&trans);
500 ret = bch2_inode_truncate(c, k.k->p.inode,
511 return bch2_trans_exit(&trans) ?: ret;
515 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
519 static int check_dirents(struct bch_fs *c)
521 struct inode_walker w = inode_walker_init();
523 struct btree_trans trans;
524 struct btree_iter *iter;
530 bch_verbose(c, "checking dirents");
532 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
536 iter = bch2_trans_get_iter(&trans, BTREE_ID_DIRENTS,
537 POS(BCACHEFS_ROOT_INO, 0), 0);
539 for_each_btree_key_continue(iter, 0, k, ret) {
540 struct bkey_s_c_dirent d;
541 struct bch_inode_unpacked target;
545 ret = walk_inode(&trans, &w, k.k->p.inode);
549 if (fsck_err_on(!w.have_inode, c,
550 "dirent in nonexisting directory:\n%s",
551 (bch2_bkey_val_to_text(&PBUF(buf), c,
553 fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
554 "dirent in non directory inode type %u:\n%s",
555 mode_to_type(w.inode.bi_mode),
556 (bch2_bkey_val_to_text(&PBUF(buf), c,
558 ret = bch2_btree_delete_at(&trans, iter, 0);
564 if (w.first_this_inode && w.have_inode)
565 hash_check_set_inode(&trans, &h, &w.inode);
567 ret = check_dirent_hash(&trans, &h, iter, &k);
578 if (k.k->type != KEY_TYPE_dirent)
581 d = bkey_s_c_to_dirent(k);
582 d_inum = le64_to_cpu(d.v->d_inum);
584 name_len = bch2_dirent_name_bytes(d);
586 if (fsck_err_on(!name_len, c, "empty dirent") ||
587 fsck_err_on(name_len == 1 &&
588 !memcmp(d.v->d_name, ".", 1), c,
590 fsck_err_on(name_len == 2 &&
591 !memcmp(d.v->d_name, "..", 2), c,
593 fsck_err_on(name_len == 2 &&
594 !memcmp(d.v->d_name, "..", 2), c,
596 fsck_err_on(memchr(d.v->d_name, '/', name_len), c,
597 "dirent name has invalid chars")) {
598 ret = remove_dirent(&trans, d);
604 if (fsck_err_on(d_inum == d.k->p.inode, c,
605 "dirent points to own directory:\n%s",
606 (bch2_bkey_val_to_text(&PBUF(buf), c,
608 ret = remove_dirent(&trans, d);
614 ret = bch2_inode_find_by_inum_trans(&trans, d_inum, &target);
615 if (ret && ret != -ENOENT)
621 if (fsck_err_on(!have_target, c,
622 "dirent points to missing inode:\n%s",
623 (bch2_bkey_val_to_text(&PBUF(buf), c,
625 ret = remove_dirent(&trans, d);
631 if (fsck_err_on(have_target &&
633 mode_to_type(target.bi_mode), c,
634 "incorrect d_type: should be %u:\n%s",
635 mode_to_type(target.bi_mode),
636 (bch2_bkey_val_to_text(&PBUF(buf), c,
638 struct bkey_i_dirent *n;
640 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
646 bkey_reassemble(&n->k_i, d.s_c);
647 n->v.d_type = mode_to_type(target.bi_mode);
649 bch2_trans_update(&trans, iter, &n->k_i);
651 ret = bch2_trans_commit(&trans, NULL, NULL,
653 BTREE_INSERT_LAZY_RW);
661 hash_stop_chain(&trans, &h);
667 return bch2_trans_exit(&trans) ?: ret;
671 * Walk xattrs: verify that they all have a corresponding inode
674 static int check_xattrs(struct bch_fs *c)
676 struct inode_walker w = inode_walker_init();
678 struct btree_trans trans;
679 struct btree_iter *iter;
683 bch_verbose(c, "checking xattrs");
687 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
689 iter = bch2_trans_get_iter(&trans, BTREE_ID_XATTRS,
690 POS(BCACHEFS_ROOT_INO, 0), 0);
692 for_each_btree_key_continue(iter, 0, k, ret) {
693 ret = walk_inode(&trans, &w, k.k->p.inode);
697 if (fsck_err_on(!w.have_inode, c,
698 "xattr for missing inode %llu",
700 ret = bch2_btree_delete_at(&trans, iter, 0);
706 if (w.first_this_inode && w.have_inode)
707 hash_check_set_inode(&trans, &h, &w.inode);
709 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
718 return bch2_trans_exit(&trans) ?: ret;
721 /* Get root directory, create if it doesn't exist: */
722 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
724 struct bkey_inode_buf packed;
727 bch_verbose(c, "checking root directory");
729 ret = bch2_inode_find_by_inum(c, BCACHEFS_ROOT_INO, root_inode);
730 if (ret && ret != -ENOENT)
733 if (fsck_err_on(ret, c, "root directory missing"))
736 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
737 "root inode not a directory"))
744 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
746 root_inode->bi_inum = BCACHEFS_ROOT_INO;
748 bch2_inode_pack(&packed, root_inode);
750 return bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
753 BTREE_INSERT_LAZY_RW);
756 /* Get lost+found, create if it doesn't exist: */
757 static int check_lostfound(struct bch_fs *c,
758 struct bch_inode_unpacked *root_inode,
759 struct bch_inode_unpacked *lostfound_inode)
761 struct qstr lostfound = QSTR("lost+found");
762 struct bch_hash_info root_hash_info =
763 bch2_hash_info_init(c, root_inode);
767 bch_verbose(c, "checking lost+found");
769 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
772 bch_notice(c, "creating lost+found");
773 goto create_lostfound;
776 ret = bch2_inode_find_by_inum(c, inum, lostfound_inode);
777 if (ret && ret != -ENOENT)
780 if (fsck_err_on(ret, c, "lost+found missing"))
781 goto create_lostfound;
783 if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
784 "lost+found inode not a directory"))
785 goto create_lostfound;
791 bch2_inode_init_early(c, lostfound_inode);
793 ret = bch2_trans_do(c, NULL,
796 BTREE_INSERT_LAZY_RW,
797 bch2_create_trans(&trans,
798 BCACHEFS_ROOT_INO, root_inode,
799 lostfound_inode, &lostfound,
800 0, 0, S_IFDIR|0700, 0, NULL, NULL));
802 bch_err(c, "error creating lost+found: %i", ret);
807 struct inode_bitmap {
812 static inline bool inode_bitmap_test(struct inode_bitmap *b, size_t nr)
814 return nr < b->size ? test_bit(nr, b->bits) : false;
817 static inline int inode_bitmap_set(struct inode_bitmap *b, size_t nr)
820 size_t new_size = max_t(size_t, max_t(size_t,
826 new_size = roundup_pow_of_two(new_size);
827 n = krealloc(b->bits, new_size / 8, GFP_KERNEL|__GFP_ZERO);
836 __set_bit(nr, b->bits);
844 struct pathbuf_entry {
850 static int path_down(struct pathbuf *p, u64 inum)
852 if (p->nr == p->size) {
853 size_t new_size = max_t(size_t, 256UL, p->size * 2);
854 void *n = krealloc(p->entries,
855 new_size * sizeof(p->entries[0]),
864 p->entries[p->nr++] = (struct pathbuf_entry) {
872 static int check_directory_structure(struct bch_fs *c,
873 struct bch_inode_unpacked *lostfound_inode)
875 struct inode_bitmap dirs_done = { NULL, 0 };
876 struct pathbuf path = { 0, 0, NULL };
877 struct pathbuf_entry *e;
878 struct btree_trans trans;
879 struct btree_iter *iter;
881 struct bkey_s_c_dirent dirent;
882 bool had_unreachable;
886 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
888 bch_verbose(c, "checking directory structure");
892 had_unreachable = false;
894 ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
896 bch_err(c, "memory allocation failure in inode_bitmap_set()");
900 ret = path_down(&path, BCACHEFS_ROOT_INO);
906 e = &path.entries[path.nr - 1];
908 if (e->offset == U64_MAX)
911 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS,
912 POS(e->inum, e->offset + 1), 0, k, ret) {
913 if (k.k->p.inode != e->inum)
916 e->offset = k.k->p.offset;
918 if (k.k->type != KEY_TYPE_dirent)
921 dirent = bkey_s_c_to_dirent(k);
923 if (dirent.v->d_type != DT_DIR)
926 d_inum = le64_to_cpu(dirent.v->d_inum);
928 if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
929 "directory %llu has multiple hardlinks",
931 ret = remove_dirent(&trans, dirent);
937 ret = inode_bitmap_set(&dirs_done, d_inum);
939 bch_err(c, "memory allocation failure in inode_bitmap_set()");
943 ret = path_down(&path, d_inum);
948 ret = bch2_trans_iter_free(&trans, iter);
950 bch_err(c, "btree error %i in fsck", ret);
955 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
957 bch_err(c, "btree error %i in fsck", ret);
964 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS_MIN, 0);
966 for_each_btree_key_continue(iter, 0, k, ret) {
967 if (k.k->type != KEY_TYPE_inode)
970 if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
973 ret = bch2_empty_dir_trans(&trans, k.k->p.inode);
979 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.inode), c,
980 "unreachable directory found (inum %llu)",
982 bch2_trans_unlock(&trans);
984 ret = reattach_inode(c, lostfound_inode, k.k->p.inode);
989 had_unreachable = true;
992 bch2_trans_iter_free(&trans, iter);
996 if (had_unreachable) {
997 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
998 kfree(dirs_done.bits);
1000 memset(&dirs_done, 0, sizeof(dirs_done));
1001 memset(&path, 0, sizeof(path));
1006 ret = bch2_trans_exit(&trans) ?: ret;
1007 kfree(dirs_done.bits);
1008 kfree(path.entries);
1017 typedef GENRADIX(struct nlink) nlink_table;
1019 static void inc_link(struct bch_fs *c, nlink_table *links,
1020 u64 range_start, u64 *range_end,
1025 if (inum < range_start || inum >= *range_end)
1028 link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
1030 bch_verbose(c, "allocation failed during fsck - will need another pass");
1042 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
1043 u64 range_start, u64 *range_end)
1045 struct btree_trans trans;
1046 struct btree_iter *iter;
1048 struct bkey_s_c_dirent d;
1052 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1054 inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
1056 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS, POS_MIN, 0, k, ret) {
1057 switch (k.k->type) {
1058 case KEY_TYPE_dirent:
1059 d = bkey_s_c_to_dirent(k);
1060 d_inum = le64_to_cpu(d.v->d_inum);
1062 if (d.v->d_type == DT_DIR)
1063 inc_link(c, links, range_start, range_end,
1064 d.k->p.inode, true);
1066 inc_link(c, links, range_start, range_end,
1072 bch2_trans_cond_resched(&trans);
1074 ret = bch2_trans_exit(&trans) ?: ret;
1076 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
1081 static int check_inode_nlink(struct bch_fs *c,
1082 struct bch_inode_unpacked *lostfound_inode,
1083 struct bch_inode_unpacked *u,
1087 u32 i_nlink = bch2_inode_nlink_get(u);
1089 link->count * nlink_bias(u->bi_mode) +
1094 * These should have been caught/fixed by earlier passes, we don't
1097 if (S_ISDIR(u->bi_mode) && link->count > 1) {
1098 need_fsck_err(c, "directory %llu with multiple hardlinks: %u",
1099 u->bi_inum, link->count);
1103 if (S_ISDIR(u->bi_mode) && !link->count) {
1104 need_fsck_err(c, "unreachable directory found (inum %llu)",
1109 if (!S_ISDIR(u->bi_mode) && link->dir_count) {
1110 need_fsck_err(c, "non directory with subdirectories",
1116 !(u->bi_flags & BCH_INODE_UNLINKED) &&
1117 (c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK))) {
1118 if (fsck_err(c, "unreachable inode %llu not marked as unlinked (type %u)",
1119 u->bi_inum, mode_to_type(u->bi_mode)) ==
1123 ret = reattach_inode(c, lostfound_inode, u->bi_inum);
1128 real_i_nlink = nlink_bias(u->bi_mode) + link->dir_count;
1132 if (i_nlink < link->count) {
1133 if (fsck_err(c, "inode %llu i_link too small (%u < %u, type %i)",
1134 u->bi_inum, i_nlink, link->count,
1135 mode_to_type(u->bi_mode)) == FSCK_ERR_IGNORE)
1140 if (i_nlink != real_i_nlink &&
1142 if (fsck_err(c, "filesystem marked clean, "
1143 "but inode %llu has wrong i_nlink "
1144 "(type %u i_nlink %u, should be %u)",
1145 u->bi_inum, mode_to_type(u->bi_mode),
1146 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1151 if (i_nlink != real_i_nlink &&
1152 (c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK))) {
1153 if (fsck_err(c, "inode %llu has wrong i_nlink "
1154 "(type %u i_nlink %u, should be %u)",
1155 u->bi_inum, mode_to_type(u->bi_mode),
1156 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1161 if (real_i_nlink && i_nlink != real_i_nlink)
1162 bch_verbose(c, "setting inode %llu nlink from %u to %u",
1163 u->bi_inum, i_nlink, real_i_nlink);
1165 if (i_nlink != real_i_nlink) {
1166 bch2_inode_nlink_set(u, real_i_nlink);
1173 static int check_inode(struct btree_trans *trans,
1174 struct bch_inode_unpacked *lostfound_inode,
1175 struct btree_iter *iter,
1176 struct bkey_s_c_inode inode,
1179 struct bch_fs *c = trans->c;
1180 struct bch_inode_unpacked u;
1181 bool do_update = false;
1184 ret = bch2_inode_unpack(inode, &u);
1186 bch2_trans_unlock(trans);
1188 if (bch2_fs_inconsistent_on(ret, c,
1189 "error unpacking inode %llu in fsck",
1194 ret = check_inode_nlink(c, lostfound_inode, &u, link,
1200 if (u.bi_flags & BCH_INODE_UNLINKED &&
1202 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
1204 bch_verbose(c, "deleting inode %llu", u.bi_inum);
1206 ret = bch2_inode_rm(c, u.bi_inum);
1208 bch_err(c, "error in fsck: error %i while deleting inode", ret);
1212 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
1214 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
1216 bch_verbose(c, "truncating inode %llu", u.bi_inum);
1219 * XXX: need to truncate partial blocks too here - or ideally
1220 * just switch units to bytes and that issue goes away
1223 ret = bch2_inode_truncate(c, u.bi_inum, u.bi_size);
1225 bch_err(c, "error in fsck: error %i truncating inode", ret);
1230 * We truncated without our normal sector accounting hook, just
1231 * make sure we recalculate it:
1233 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
1235 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
1239 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
1241 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
1245 bch_verbose(c, "recounting sectors for inode %llu",
1248 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
1250 bch_err(c, "error in fsck: error %i recounting inode sectors",
1255 u.bi_sectors = sectors;
1256 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
1261 struct bkey_inode_buf p;
1263 bch2_inode_pack(&p, &u);
1264 bch2_trans_update(trans, iter, &p.inode.k_i);
1266 ret = bch2_trans_commit(trans, NULL, NULL,
1267 BTREE_INSERT_NOFAIL|
1268 BTREE_INSERT_LAZY_RW);
1269 if (ret && ret != -EINTR)
1270 bch_err(c, "error in fsck: error %i "
1271 "updating inode", ret);
1278 static int bch2_gc_walk_inodes(struct bch_fs *c,
1279 struct bch_inode_unpacked *lostfound_inode,
1281 u64 range_start, u64 range_end)
1283 struct btree_trans trans;
1284 struct btree_iter *iter;
1286 struct nlink *link, zero_links = { 0, 0 };
1287 struct genradix_iter nlinks_iter;
1288 int ret = 0, ret2 = 0;
1291 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1293 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES,
1294 POS(range_start, 0), 0);
1295 nlinks_iter = genradix_iter_init(links, 0);
1297 while ((k = bch2_btree_iter_peek(iter)).k &&
1298 !(ret2 = bkey_err(k))) {
1299 peek_nlinks: link = genradix_iter_peek(&nlinks_iter, links);
1301 if (!link && (!k.k || iter->pos.inode >= range_end))
1304 nlinks_pos = range_start + nlinks_iter.pos;
1305 if (iter->pos.inode > nlinks_pos) {
1306 /* Should have been caught by dirents pass: */
1307 need_fsck_err_on(link && link->count, c,
1308 "missing inode %llu (nlink %u)",
1309 nlinks_pos, link->count);
1310 genradix_iter_advance(&nlinks_iter, links);
1314 if (iter->pos.inode < nlinks_pos || !link)
1317 if (k.k && k.k->type == KEY_TYPE_inode) {
1318 ret = check_inode(&trans, lostfound_inode, iter,
1319 bkey_s_c_to_inode(k), link);
1320 BUG_ON(ret == -EINTR);
1324 /* Should have been caught by dirents pass: */
1325 need_fsck_err_on(link->count, c,
1326 "missing inode %llu (nlink %u)",
1327 nlinks_pos, link->count);
1330 if (nlinks_pos == iter->pos.inode)
1331 genradix_iter_advance(&nlinks_iter, links);
1333 bch2_btree_iter_next(iter);
1334 bch2_trans_cond_resched(&trans);
1337 bch2_trans_exit(&trans);
1340 bch_err(c, "error in fsck: btree error %i while walking inodes", ret2);
1346 static int check_inode_nlinks(struct bch_fs *c,
1347 struct bch_inode_unpacked *lostfound_inode)
1350 u64 this_iter_range_start, next_iter_range_start = 0;
1353 bch_verbose(c, "checking inode nlinks");
1355 genradix_init(&links);
1358 this_iter_range_start = next_iter_range_start;
1359 next_iter_range_start = U64_MAX;
1361 ret = bch2_gc_walk_dirents(c, &links,
1362 this_iter_range_start,
1363 &next_iter_range_start);
1367 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1368 this_iter_range_start,
1369 next_iter_range_start);
1373 genradix_free(&links);
1374 } while (next_iter_range_start != U64_MAX);
1376 genradix_free(&links);
1382 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1383 * Doesn't fix them yet, mainly because they haven't yet been observed:
1385 int bch2_fsck_full(struct bch_fs *c)
1387 struct bch_inode_unpacked root_inode, lostfound_inode;
1389 return check_extents(c) ?:
1392 check_root(c, &root_inode) ?:
1393 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1394 check_directory_structure(c, &lostfound_inode) ?:
1395 check_inode_nlinks(c, &lostfound_inode);
1398 int bch2_fsck_inode_nlink(struct bch_fs *c)
1400 struct bch_inode_unpacked root_inode, lostfound_inode;
1402 return check_root(c, &root_inode) ?:
1403 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1404 check_inode_nlinks(c, &lostfound_inode);
1407 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1409 struct btree_trans trans;
1410 struct btree_iter *iter;
1412 struct bkey_s_c_inode inode;
1415 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1417 for_each_btree_key(&trans, iter, BTREE_ID_INODES, POS_MIN, 0, k, ret) {
1418 if (k.k->type != KEY_TYPE_inode)
1421 inode = bkey_s_c_to_inode(k);
1423 if (inode.v->bi_flags &
1424 (BCH_INODE_I_SIZE_DIRTY|
1425 BCH_INODE_I_SECTORS_DIRTY|
1426 BCH_INODE_UNLINKED)) {
1427 ret = check_inode(&trans, NULL, iter, inode, NULL);
1428 BUG_ON(ret == -EINTR);
1433 BUG_ON(ret == -EINTR);
1435 return bch2_trans_exit(&trans) ?: ret;