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_hash_info lostfound_hash_info =
84 bch2_hash_info_init(c, lostfound_inode);
85 struct bkey_inode_buf packed;
90 snprintf(name_buf, sizeof(name_buf), "%llu", inum);
91 name = (struct qstr) QSTR(name_buf);
93 lostfound_inode->bi_nlink++;
95 bch2_inode_pack(&packed, lostfound_inode);
97 ret = bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
100 BTREE_INSERT_LAZY_RW);
102 bch_err(c, "error %i reattaching inode %llu while updating lost+found",
107 ret = bch2_dirent_create(c, lostfound_inode->bi_inum,
108 &lostfound_hash_info,
109 DT_DIR, &name, inum, NULL,
111 BTREE_INSERT_LAZY_RW);
113 bch_err(c, "error %i reattaching inode %llu while creating new dirent",
120 struct inode_walker {
121 bool first_this_inode;
124 struct bch_inode_unpacked inode;
127 static struct inode_walker inode_walker_init(void)
129 return (struct inode_walker) {
135 static int walk_inode(struct btree_trans *trans,
136 struct inode_walker *w, u64 inum)
138 if (inum != w->cur_inum) {
139 int ret = bch2_inode_find_by_inum_trans(trans, inum,
142 if (ret && ret != -ENOENT)
145 w->have_inode = !ret;
147 w->first_this_inode = true;
149 w->first_this_inode = false;
156 struct bch_hash_info info;
158 /* start of current chain of hash collisions: */
159 struct btree_iter *chain;
161 /* next offset in current chain of hash collisions: */
165 static void hash_check_init(struct hash_check *h)
170 static void hash_stop_chain(struct btree_trans *trans,
171 struct hash_check *h)
174 bch2_trans_iter_free(trans, h->chain);
178 static void hash_check_set_inode(struct btree_trans *trans,
179 struct hash_check *h,
180 const struct bch_inode_unpacked *bi)
182 h->info = bch2_hash_info_init(trans->c, bi);
183 hash_stop_chain(trans, h);
186 static int hash_redo_key(const struct bch_hash_desc desc,
187 struct btree_trans *trans, struct hash_check *h,
188 struct btree_iter *k_iter, struct bkey_s_c k,
194 tmp = kmalloc(bkey_bytes(k.k), GFP_KERNEL);
198 bkey_reassemble(tmp, k);
200 ret = bch2_btree_delete_at(trans, k_iter, 0);
204 bch2_hash_set(trans, desc, &h->info, k_iter->pos.inode,
205 tmp, BCH_HASH_SET_MUST_CREATE);
206 ret = bch2_trans_commit(trans, NULL, NULL,
208 BTREE_INSERT_LAZY_RW);
214 static int fsck_hash_delete_at(struct btree_trans *trans,
215 const struct bch_hash_desc desc,
216 struct bch_hash_info *info,
217 struct btree_iter *iter)
221 ret = bch2_hash_delete_at(trans, desc, info, iter) ?:
222 bch2_trans_commit(trans, NULL, NULL,
225 BTREE_INSERT_LAZY_RW);
227 ret = bch2_btree_iter_traverse(iter);
235 static int hash_check_duplicates(struct btree_trans *trans,
236 const struct bch_hash_desc desc, struct hash_check *h,
237 struct btree_iter *k_iter, struct bkey_s_c k)
239 struct bch_fs *c = trans->c;
240 struct btree_iter *iter;
245 if (!bkey_cmp(h->chain->pos, k_iter->pos))
248 iter = bch2_trans_copy_iter(trans, h->chain);
249 BUG_ON(IS_ERR(iter));
251 for_each_btree_key_continue(iter, 0, k2) {
252 if (bkey_cmp(k2.k->p, k.k->p) >= 0)
255 if (fsck_err_on(k2.k->type == desc.key_type &&
256 !desc.cmp_bkey(k, k2), c,
257 "duplicate hash table keys:\n%s",
258 (bch2_bkey_val_to_text(&PBUF(buf), c,
260 ret = fsck_hash_delete_at(trans, desc, &h->info, k_iter);
268 bch2_trans_iter_free(trans, iter);
272 static void hash_set_chain_start(struct btree_trans *trans,
273 const struct bch_hash_desc desc,
274 struct hash_check *h,
275 struct btree_iter *k_iter, struct bkey_s_c k)
277 bool hole = (k.k->type != KEY_TYPE_whiteout &&
278 k.k->type != desc.key_type);
280 if (hole || k.k->p.offset > h->chain_end + 1)
281 hash_stop_chain(trans, h);
285 h->chain = bch2_trans_copy_iter(trans, k_iter);
286 BUG_ON(IS_ERR(h->chain));
289 h->chain_end = k.k->p.offset;
293 static bool key_has_correct_hash(struct btree_trans *trans,
294 const struct bch_hash_desc desc,
295 struct hash_check *h,
296 struct btree_iter *k_iter, struct bkey_s_c k)
300 hash_set_chain_start(trans, desc, h, k_iter, k);
302 if (k.k->type != desc.key_type)
305 hash = desc.hash_bkey(&h->info, k);
307 return hash >= h->chain->pos.offset &&
308 hash <= k.k->p.offset;
311 static int hash_check_key(struct btree_trans *trans,
312 const struct bch_hash_desc desc, struct hash_check *h,
313 struct btree_iter *k_iter, struct bkey_s_c k)
315 struct bch_fs *c = trans->c;
320 hash_set_chain_start(trans, desc, h, k_iter, k);
322 if (k.k->type != desc.key_type)
325 hashed = desc.hash_bkey(&h->info, k);
327 if (fsck_err_on(hashed < h->chain->pos.offset ||
328 hashed > k.k->p.offset, c,
329 "hash table key at wrong offset: btree %u, %llu, "
330 "hashed to %llu chain starts at %llu\n%s",
331 desc.btree_id, k.k->p.offset,
332 hashed, h->chain->pos.offset,
333 (bch2_bkey_val_to_text(&PBUF(buf), c,
335 ret = hash_redo_key(desc, trans, h, k_iter, k, hashed);
337 bch_err(c, "hash_redo_key err %i", ret);
343 ret = hash_check_duplicates(trans, desc, h, k_iter, k);
348 static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h,
349 struct btree_iter *iter, struct bkey_s_c *k)
351 struct bch_fs *c = trans->c;
352 struct bkey_i_dirent *d = NULL;
358 if (key_has_correct_hash(trans, bch2_dirent_hash_desc, h, iter, *k))
361 len = bch2_dirent_name_bytes(bkey_s_c_to_dirent(*k));
364 memcpy(buf, bkey_s_c_to_dirent(*k).v->d_name, len);
367 d = kmalloc(bkey_bytes(k->k), GFP_KERNEL);
369 bch_err(c, "memory allocation failure");
373 bkey_reassemble(&d->k_i, *k);
380 d->k.u64s = BKEY_U64s + dirent_val_u64s(len);
382 BUG_ON(bkey_val_bytes(&d->k) <
383 offsetof(struct bch_dirent, d_name) + len);
385 memset(d->v.d_name + len, 0,
386 bkey_val_bytes(&d->k) -
387 offsetof(struct bch_dirent, d_name) - len);
389 hash = bch2_dirent_hash_desc.hash_bkey(&h->info,
390 bkey_i_to_s_c(&d->k_i));
391 } while (hash < h->chain->pos.offset ||
392 hash > k->k->p.offset);
394 if (fsck_err(c, "dirent with junk at end, was %s (%zu) now %s (%u)",
395 buf, strlen(buf), d->v.d_name, len)) {
396 bch2_trans_update(trans, BTREE_INSERT_ENTRY(iter, &d->k_i));
398 ret = bch2_trans_commit(trans, NULL, NULL,
400 BTREE_INSERT_LAZY_RW);
404 *k = bch2_btree_iter_peek(iter);
406 BUG_ON(k->k->type != KEY_TYPE_dirent);
413 hash = bch2_dirent_hash_desc.hash_bkey(&h->info, *k);
415 if (fsck_err(c, "cannot fix dirent by removing trailing garbage %s (%zu)\n"
416 "hash table key at wrong offset: btree %u, offset %llu, "
417 "hashed to %llu chain starts at %llu\n%s",
418 buf, strlen(buf), BTREE_ID_DIRENTS,
419 k->k->p.offset, hash, h->chain->pos.offset,
420 (bch2_bkey_val_to_text(&PBUF(buf), c,
422 ret = hash_redo_key(bch2_dirent_hash_desc, trans,
425 bch_err(c, "hash_redo_key err %i", ret);
433 static int bch2_inode_truncate(struct bch_fs *c, u64 inode_nr, u64 new_size)
435 return bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
436 POS(inode_nr, round_up(new_size, block_bytes(c)) >> 9),
437 POS(inode_nr + 1, 0), NULL);
441 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
442 * that i_size an i_sectors are consistent
445 static int check_extents(struct bch_fs *c)
447 struct inode_walker w = inode_walker_init();
448 struct btree_trans trans;
449 struct btree_iter *iter;
454 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
456 bch_verbose(c, "checking extents");
458 iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
459 POS(BCACHEFS_ROOT_INO, 0), 0);
461 for_each_btree_key_continue(iter, 0, k) {
462 ret = walk_inode(&trans, &w, k.k->p.inode);
466 if (fsck_err_on(!w.have_inode, c,
467 "extent type %u for missing inode %llu",
468 k.k->type, k.k->p.inode) ||
469 fsck_err_on(w.have_inode &&
470 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
471 "extent type %u for non regular file, inode %llu mode %o",
472 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
473 bch2_trans_unlock(&trans);
475 ret = bch2_inode_truncate(c, k.k->p.inode, 0);
481 if (fsck_err_on(w.first_this_inode &&
483 !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
484 w.inode.bi_sectors !=
485 (i_sectors = bch2_count_inode_sectors(&trans, w.cur_inum)),
486 c, "i_sectors wrong: got %llu, should be %llu",
487 w.inode.bi_sectors, i_sectors)) {
488 struct bkey_inode_buf p;
490 w.inode.bi_sectors = i_sectors;
492 bch2_trans_unlock(&trans);
494 bch2_inode_pack(&p, &w.inode);
496 ret = bch2_btree_insert(c, BTREE_ID_INODES,
497 &p.inode.k_i, NULL, NULL,
499 BTREE_INSERT_LAZY_RW);
501 bch_err(c, "error in fsck: error %i updating inode", ret);
505 /* revalidate iterator: */
506 k = bch2_btree_iter_peek(iter);
509 if (fsck_err_on(w.have_inode &&
510 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
511 k.k->type != KEY_TYPE_reservation &&
512 k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
513 "extent type %u offset %llu past end of inode %llu, i_size %llu",
514 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
515 bch2_trans_unlock(&trans);
517 ret = bch2_inode_truncate(c, k.k->p.inode,
528 return bch2_trans_exit(&trans) ?: ret;
532 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
536 static int check_dirents(struct bch_fs *c)
538 struct inode_walker w = inode_walker_init();
540 struct btree_trans trans;
541 struct btree_iter *iter;
547 bch_verbose(c, "checking dirents");
549 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
553 iter = bch2_trans_get_iter(&trans, BTREE_ID_DIRENTS,
554 POS(BCACHEFS_ROOT_INO, 0), 0);
556 for_each_btree_key_continue(iter, 0, k) {
557 struct bkey_s_c_dirent d;
558 struct bch_inode_unpacked target;
562 ret = walk_inode(&trans, &w, k.k->p.inode);
566 if (fsck_err_on(!w.have_inode, c,
567 "dirent in nonexisting directory:\n%s",
568 (bch2_bkey_val_to_text(&PBUF(buf), c,
570 fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
571 "dirent in non directory inode type %u:\n%s",
572 mode_to_type(w.inode.bi_mode),
573 (bch2_bkey_val_to_text(&PBUF(buf), c,
575 ret = bch2_btree_delete_at(&trans, iter, 0);
581 if (w.first_this_inode && w.have_inode)
582 hash_check_set_inode(&trans, &h, &w.inode);
584 ret = check_dirent_hash(&trans, &h, iter, &k);
595 if (k.k->type != KEY_TYPE_dirent)
598 d = bkey_s_c_to_dirent(k);
599 d_inum = le64_to_cpu(d.v->d_inum);
601 name_len = bch2_dirent_name_bytes(d);
603 if (fsck_err_on(!name_len, c, "empty dirent") ||
604 fsck_err_on(name_len == 1 &&
605 !memcmp(d.v->d_name, ".", 1), c,
607 fsck_err_on(name_len == 2 &&
608 !memcmp(d.v->d_name, "..", 2), c,
610 fsck_err_on(name_len == 2 &&
611 !memcmp(d.v->d_name, "..", 2), c,
613 fsck_err_on(memchr(d.v->d_name, '/', name_len), c,
614 "dirent name has invalid chars")) {
615 ret = remove_dirent(&trans, d);
621 if (fsck_err_on(d_inum == d.k->p.inode, c,
622 "dirent points to own directory:\n%s",
623 (bch2_bkey_val_to_text(&PBUF(buf), c,
625 ret = remove_dirent(&trans, d);
631 ret = bch2_inode_find_by_inum_trans(&trans, d_inum, &target);
632 if (ret && ret != -ENOENT)
638 if (fsck_err_on(!have_target, c,
639 "dirent points to missing inode:\n%s",
640 (bch2_bkey_val_to_text(&PBUF(buf), c,
642 ret = remove_dirent(&trans, d);
648 if (fsck_err_on(have_target &&
650 mode_to_type(target.bi_mode), c,
651 "incorrect d_type: should be %u:\n%s",
652 mode_to_type(target.bi_mode),
653 (bch2_bkey_val_to_text(&PBUF(buf), c,
655 struct bkey_i_dirent *n;
657 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
663 bkey_reassemble(&n->k_i, d.s_c);
664 n->v.d_type = mode_to_type(target.bi_mode);
666 bch2_trans_update(&trans,
667 BTREE_INSERT_ENTRY(iter, &n->k_i));
669 ret = bch2_trans_commit(&trans, NULL, NULL,
671 BTREE_INSERT_LAZY_RW);
679 hash_stop_chain(&trans, &h);
685 return bch2_trans_exit(&trans) ?: ret;
689 * Walk xattrs: verify that they all have a corresponding inode
692 static int check_xattrs(struct bch_fs *c)
694 struct inode_walker w = inode_walker_init();
696 struct btree_trans trans;
697 struct btree_iter *iter;
701 bch_verbose(c, "checking xattrs");
705 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
707 iter = bch2_trans_get_iter(&trans, BTREE_ID_XATTRS,
708 POS(BCACHEFS_ROOT_INO, 0), 0);
710 for_each_btree_key_continue(iter, 0, k) {
711 ret = walk_inode(&trans, &w, k.k->p.inode);
715 if (fsck_err_on(!w.have_inode, c,
716 "xattr for missing inode %llu",
718 ret = bch2_btree_delete_at(&trans, iter, 0);
724 if (w.first_this_inode && w.have_inode)
725 hash_check_set_inode(&trans, &h, &w.inode);
727 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
736 return bch2_trans_exit(&trans) ?: ret;
739 /* Get root directory, create if it doesn't exist: */
740 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
742 struct bkey_inode_buf packed;
745 bch_verbose(c, "checking root directory");
747 ret = bch2_inode_find_by_inum(c, BCACHEFS_ROOT_INO, root_inode);
748 if (ret && ret != -ENOENT)
751 if (fsck_err_on(ret, c, "root directory missing"))
754 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
755 "root inode not a directory"))
762 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO,
764 root_inode->bi_inum = BCACHEFS_ROOT_INO;
766 bch2_inode_pack(&packed, root_inode);
768 return bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
771 BTREE_INSERT_LAZY_RW);
774 /* Get lost+found, create if it doesn't exist: */
775 static int check_lostfound(struct bch_fs *c,
776 struct bch_inode_unpacked *root_inode,
777 struct bch_inode_unpacked *lostfound_inode)
779 struct qstr lostfound = QSTR("lost+found");
780 struct bch_hash_info root_hash_info =
781 bch2_hash_info_init(c, root_inode);
782 struct bkey_inode_buf packed;
786 bch_verbose(c, "checking lost+found");
788 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
791 bch_notice(c, "creating lost+found");
792 goto create_lostfound;
795 ret = bch2_inode_find_by_inum(c, inum, lostfound_inode);
796 if (ret && ret != -ENOENT)
799 if (fsck_err_on(ret, c, "lost+found missing"))
800 goto create_lostfound;
802 if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
803 "lost+found inode not a directory"))
804 goto create_lostfound;
810 root_inode->bi_nlink++;
812 bch2_inode_pack(&packed, root_inode);
814 ret = bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
817 BTREE_INSERT_LAZY_RW);
821 bch2_inode_init(c, lostfound_inode, 0, 0, S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO,
824 ret = bch2_inode_create(c, lostfound_inode, BLOCKDEV_INODE_MAX, 0,
825 &c->unused_inode_hint);
829 ret = bch2_dirent_create(c, BCACHEFS_ROOT_INO, &root_hash_info, DT_DIR,
830 &lostfound, lostfound_inode->bi_inum, NULL,
832 BTREE_INSERT_LAZY_RW);
839 struct inode_bitmap {
844 static inline bool inode_bitmap_test(struct inode_bitmap *b, size_t nr)
846 return nr < b->size ? test_bit(nr, b->bits) : false;
849 static inline int inode_bitmap_set(struct inode_bitmap *b, size_t nr)
852 size_t new_size = max_t(size_t, max_t(size_t,
858 new_size = roundup_pow_of_two(new_size);
859 n = krealloc(b->bits, new_size / 8, GFP_KERNEL|__GFP_ZERO);
868 __set_bit(nr, b->bits);
876 struct pathbuf_entry {
882 static int path_down(struct pathbuf *p, u64 inum)
884 if (p->nr == p->size) {
885 size_t new_size = max_t(size_t, 256UL, p->size * 2);
886 void *n = krealloc(p->entries,
887 new_size * sizeof(p->entries[0]),
896 p->entries[p->nr++] = (struct pathbuf_entry) {
904 static int check_directory_structure(struct bch_fs *c,
905 struct bch_inode_unpacked *lostfound_inode)
907 struct inode_bitmap dirs_done = { NULL, 0 };
908 struct pathbuf path = { 0, 0, NULL };
909 struct pathbuf_entry *e;
910 struct btree_trans trans;
911 struct btree_iter *iter;
913 struct bkey_s_c_dirent dirent;
914 bool had_unreachable;
918 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
920 bch_verbose(c, "checking directory structure");
924 had_unreachable = false;
926 ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
928 bch_err(c, "memory allocation failure in inode_bitmap_set()");
932 ret = path_down(&path, BCACHEFS_ROOT_INO);
938 e = &path.entries[path.nr - 1];
940 if (e->offset == U64_MAX)
943 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS,
944 POS(e->inum, e->offset + 1), 0, k, ret) {
945 if (k.k->p.inode != e->inum)
948 e->offset = k.k->p.offset;
950 if (k.k->type != KEY_TYPE_dirent)
953 dirent = bkey_s_c_to_dirent(k);
955 if (dirent.v->d_type != DT_DIR)
958 d_inum = le64_to_cpu(dirent.v->d_inum);
960 if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
961 "directory %llu has multiple hardlinks",
963 ret = remove_dirent(&trans, dirent);
969 ret = inode_bitmap_set(&dirs_done, d_inum);
971 bch_err(c, "memory allocation failure in inode_bitmap_set()");
975 ret = path_down(&path, d_inum);
980 ret = bch2_trans_iter_free(&trans, iter);
982 bch_err(c, "btree error %i in fsck", ret);
987 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
989 bch_err(c, "btree error %i in fsck", ret);
996 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS_MIN, 0);
998 for_each_btree_key_continue(iter, 0, k) {
999 if (k.k->type != KEY_TYPE_inode)
1002 if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
1005 ret = bch2_empty_dir_trans(&trans, k.k->p.inode);
1011 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.inode), c,
1012 "unreachable directory found (inum %llu)",
1014 bch2_trans_unlock(&trans);
1016 ret = reattach_inode(c, lostfound_inode, k.k->p.inode);
1021 had_unreachable = true;
1024 ret = bch2_trans_iter_free(&trans, iter);
1028 if (had_unreachable) {
1029 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
1030 kfree(dirs_done.bits);
1031 kfree(path.entries);
1032 memset(&dirs_done, 0, sizeof(dirs_done));
1033 memset(&path, 0, sizeof(path));
1038 ret = bch2_trans_exit(&trans) ?: ret;
1039 kfree(dirs_done.bits);
1040 kfree(path.entries);
1049 typedef GENRADIX(struct nlink) nlink_table;
1051 static void inc_link(struct bch_fs *c, nlink_table *links,
1052 u64 range_start, u64 *range_end,
1057 if (inum < range_start || inum >= *range_end)
1060 link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
1062 bch_verbose(c, "allocation failed during fsck - will need another pass");
1074 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
1075 u64 range_start, u64 *range_end)
1077 struct btree_trans trans;
1078 struct btree_iter *iter;
1080 struct bkey_s_c_dirent d;
1084 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1086 inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
1088 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS, POS_MIN, 0, k, ret) {
1089 switch (k.k->type) {
1090 case KEY_TYPE_dirent:
1091 d = bkey_s_c_to_dirent(k);
1092 d_inum = le64_to_cpu(d.v->d_inum);
1094 if (d.v->d_type == DT_DIR)
1095 inc_link(c, links, range_start, range_end,
1096 d.k->p.inode, true);
1098 inc_link(c, links, range_start, range_end,
1104 bch2_trans_cond_resched(&trans);
1106 ret = bch2_trans_exit(&trans) ?: ret;
1108 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
1113 static int check_inode_nlink(struct bch_fs *c,
1114 struct bch_inode_unpacked *lostfound_inode,
1115 struct bch_inode_unpacked *u,
1119 u32 i_nlink = u->bi_flags & BCH_INODE_UNLINKED
1121 : u->bi_nlink + nlink_bias(u->bi_mode);
1123 link->count * nlink_bias(u->bi_mode) +
1128 * These should have been caught/fixed by earlier passes, we don't
1131 if (S_ISDIR(u->bi_mode) && link->count > 1) {
1132 need_fsck_err(c, "directory %llu with multiple hardlinks: %u",
1133 u->bi_inum, link->count);
1137 if (S_ISDIR(u->bi_mode) && !link->count) {
1138 need_fsck_err(c, "unreachable directory found (inum %llu)",
1143 if (!S_ISDIR(u->bi_mode) && link->dir_count) {
1144 need_fsck_err(c, "non directory with subdirectories",
1150 !(u->bi_flags & BCH_INODE_UNLINKED) &&
1151 (c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK))) {
1152 if (fsck_err(c, "unreachable inode %llu not marked as unlinked (type %u)",
1153 u->bi_inum, mode_to_type(u->bi_mode)) ==
1157 ret = reattach_inode(c, lostfound_inode, u->bi_inum);
1162 real_i_nlink = nlink_bias(u->bi_mode) + link->dir_count;
1166 if (i_nlink < link->count) {
1167 if (fsck_err(c, "inode %llu i_link too small (%u < %u, type %i)",
1168 u->bi_inum, i_nlink, link->count,
1169 mode_to_type(u->bi_mode)) == FSCK_ERR_IGNORE)
1174 if (i_nlink != real_i_nlink &&
1176 if (fsck_err(c, "filesystem marked clean, "
1177 "but inode %llu has wrong i_nlink "
1178 "(type %u i_nlink %u, should be %u)",
1179 u->bi_inum, mode_to_type(u->bi_mode),
1180 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1185 if (i_nlink != real_i_nlink &&
1186 (c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK))) {
1187 if (fsck_err(c, "inode %llu has wrong i_nlink "
1188 "(type %u i_nlink %u, should be %u)",
1189 u->bi_inum, mode_to_type(u->bi_mode),
1190 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1195 if (real_i_nlink && i_nlink != real_i_nlink)
1196 bch_verbose(c, "setting inode %llu nlink from %u to %u",
1197 u->bi_inum, i_nlink, real_i_nlink);
1199 if (i_nlink != real_i_nlink) {
1201 u->bi_nlink = real_i_nlink - nlink_bias(u->bi_mode);
1202 u->bi_flags &= ~BCH_INODE_UNLINKED;
1205 u->bi_flags |= BCH_INODE_UNLINKED;
1214 static int check_inode(struct btree_trans *trans,
1215 struct bch_inode_unpacked *lostfound_inode,
1216 struct btree_iter *iter,
1217 struct bkey_s_c_inode inode,
1220 struct bch_fs *c = trans->c;
1221 struct bch_inode_unpacked u;
1222 bool do_update = false;
1225 ret = bch2_inode_unpack(inode, &u);
1227 bch2_trans_unlock(trans);
1229 if (bch2_fs_inconsistent_on(ret, c,
1230 "error unpacking inode %llu in fsck",
1235 ret = check_inode_nlink(c, lostfound_inode, &u, link,
1241 if (u.bi_flags & BCH_INODE_UNLINKED &&
1243 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
1245 bch_verbose(c, "deleting inode %llu", u.bi_inum);
1247 ret = bch2_inode_rm(c, u.bi_inum);
1249 bch_err(c, "error in fsck: error %i while deleting inode", ret);
1253 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
1255 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
1257 bch_verbose(c, "truncating inode %llu", u.bi_inum);
1260 * XXX: need to truncate partial blocks too here - or ideally
1261 * just switch units to bytes and that issue goes away
1264 ret = bch2_inode_truncate(c, u.bi_inum, u.bi_size);
1266 bch_err(c, "error in fsck: error %i truncating inode", ret);
1271 * We truncated without our normal sector accounting hook, just
1272 * make sure we recalculate it:
1274 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
1276 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
1280 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
1282 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
1286 bch_verbose(c, "recounting sectors for inode %llu",
1289 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
1291 bch_err(c, "error in fsck: error %i recounting inode sectors",
1296 u.bi_sectors = sectors;
1297 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
1302 struct bkey_inode_buf p;
1304 bch2_inode_pack(&p, &u);
1305 bch2_trans_update(trans, BTREE_INSERT_ENTRY(iter, &p.inode.k_i));
1307 ret = bch2_trans_commit(trans, NULL, NULL,
1308 BTREE_INSERT_NOFAIL|
1309 BTREE_INSERT_LAZY_RW);
1310 if (ret && ret != -EINTR)
1311 bch_err(c, "error in fsck: error %i "
1312 "updating inode", ret);
1319 static int bch2_gc_walk_inodes(struct bch_fs *c,
1320 struct bch_inode_unpacked *lostfound_inode,
1322 u64 range_start, u64 range_end)
1324 struct btree_trans trans;
1325 struct btree_iter *iter;
1327 struct nlink *link, zero_links = { 0, 0 };
1328 struct genradix_iter nlinks_iter;
1329 int ret = 0, ret2 = 0;
1332 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1334 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES,
1335 POS(range_start, 0), 0);
1336 nlinks_iter = genradix_iter_init(links, 0);
1338 while ((k = bch2_btree_iter_peek(iter)).k &&
1339 !(ret2 = bkey_err(k))) {
1340 peek_nlinks: link = genradix_iter_peek(&nlinks_iter, links);
1342 if (!link && (!k.k || iter->pos.inode >= range_end))
1345 nlinks_pos = range_start + nlinks_iter.pos;
1346 if (iter->pos.inode > nlinks_pos) {
1347 /* Should have been caught by dirents pass: */
1348 need_fsck_err_on(link && link->count, c,
1349 "missing inode %llu (nlink %u)",
1350 nlinks_pos, link->count);
1351 genradix_iter_advance(&nlinks_iter, links);
1355 if (iter->pos.inode < nlinks_pos || !link)
1358 if (k.k && k.k->type == KEY_TYPE_inode) {
1359 ret = check_inode(&trans, lostfound_inode, iter,
1360 bkey_s_c_to_inode(k), link);
1361 BUG_ON(ret == -EINTR);
1365 /* Should have been caught by dirents pass: */
1366 need_fsck_err_on(link->count, c,
1367 "missing inode %llu (nlink %u)",
1368 nlinks_pos, link->count);
1371 if (nlinks_pos == iter->pos.inode)
1372 genradix_iter_advance(&nlinks_iter, links);
1374 bch2_btree_iter_next(iter);
1375 bch2_trans_cond_resched(&trans);
1378 bch2_trans_exit(&trans);
1381 bch_err(c, "error in fsck: btree error %i while walking inodes", ret2);
1387 static int check_inode_nlinks(struct bch_fs *c,
1388 struct bch_inode_unpacked *lostfound_inode)
1391 u64 this_iter_range_start, next_iter_range_start = 0;
1394 bch_verbose(c, "checking inode nlinks");
1396 genradix_init(&links);
1399 this_iter_range_start = next_iter_range_start;
1400 next_iter_range_start = U64_MAX;
1402 ret = bch2_gc_walk_dirents(c, &links,
1403 this_iter_range_start,
1404 &next_iter_range_start);
1408 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1409 this_iter_range_start,
1410 next_iter_range_start);
1414 genradix_free(&links);
1415 } while (next_iter_range_start != U64_MAX);
1417 genradix_free(&links);
1423 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1424 * Doesn't fix them yet, mainly because they haven't yet been observed:
1426 int bch2_fsck_full(struct bch_fs *c)
1428 struct bch_inode_unpacked root_inode, lostfound_inode;
1430 return check_extents(c) ?:
1433 check_root(c, &root_inode) ?:
1434 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1435 check_directory_structure(c, &lostfound_inode) ?:
1436 check_inode_nlinks(c, &lostfound_inode);
1439 int bch2_fsck_inode_nlink(struct bch_fs *c)
1441 struct bch_inode_unpacked root_inode, lostfound_inode;
1443 return check_root(c, &root_inode) ?:
1444 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1445 check_inode_nlinks(c, &lostfound_inode);
1448 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1450 struct btree_trans trans;
1451 struct btree_iter *iter;
1453 struct bkey_s_c_inode inode;
1456 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1458 for_each_btree_key(&trans, iter, BTREE_ID_INODES, POS_MIN, 0, k, ret) {
1459 if (k.k->type != KEY_TYPE_inode)
1462 inode = bkey_s_c_to_inode(k);
1464 if (inode.v->bi_flags &
1465 (BCH_INODE_I_SIZE_DIRTY|
1466 BCH_INODE_I_SECTORS_DIRTY|
1467 BCH_INODE_UNLINKED)) {
1468 ret = check_inode(&trans, NULL, iter, inode, NULL);
1469 BUG_ON(ret == -EINTR);
1474 BUG_ON(ret == -EINTR);
1476 return bch2_trans_exit(&trans) ?: ret;