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 = bch2_trans_kmalloc(trans, name.len + 1);
56 memcpy(buf, dirent.v->d_name, name.len);
60 ret = bch2_inode_find_by_inum_trans(trans, dir_inum, &dir_inode);
61 if (ret && ret != -EINTR)
62 bch_err(c, "remove_dirent: err %i looking up directory inode", ret);
66 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
68 ret = bch2_hash_delete(trans, bch2_dirent_hash_desc,
69 &dir_hash_info, dir_inum, &name);
70 if (ret && ret != -EINTR)
71 bch_err(c, "remove_dirent: err %i deleting dirent", ret);
78 static int remove_dirent(struct btree_trans *trans,
79 struct bkey_s_c_dirent dirent)
81 return __bch2_trans_do(trans, NULL, NULL,
84 __remove_dirent(trans, dirent));
87 static int reattach_inode(struct bch_fs *c,
88 struct bch_inode_unpacked *lostfound_inode,
91 struct bch_inode_unpacked dir_u, inode_u;
96 snprintf(name_buf, sizeof(name_buf), "%llu", inum);
97 name = (struct qstr) QSTR(name_buf);
99 ret = bch2_trans_do(c, NULL, NULL,
100 BTREE_INSERT_LAZY_RW,
101 bch2_link_trans(&trans, lostfound_inode->bi_inum,
102 inum, &dir_u, &inode_u, &name));
104 bch_err(c, "error %i reattaching inode %llu", ret, inum);
109 struct inode_walker {
110 bool first_this_inode;
113 struct bch_inode_unpacked inode;
116 static struct inode_walker inode_walker_init(void)
118 return (struct inode_walker) {
124 static int walk_inode(struct btree_trans *trans,
125 struct inode_walker *w, u64 inum)
127 if (inum != w->cur_inum) {
128 int ret = bch2_inode_find_by_inum_trans(trans, inum,
131 if (ret && ret != -ENOENT)
134 w->have_inode = !ret;
136 w->first_this_inode = true;
138 w->first_this_inode = false;
145 struct bch_hash_info info;
147 /* start of current chain of hash collisions: */
148 struct btree_iter *chain;
150 /* next offset in current chain of hash collisions: */
154 static void hash_check_init(struct hash_check *h)
160 static void hash_stop_chain(struct btree_trans *trans,
161 struct hash_check *h)
164 bch2_trans_iter_free(trans, h->chain);
168 static void hash_check_set_inode(struct btree_trans *trans,
169 struct hash_check *h,
170 const struct bch_inode_unpacked *bi)
172 h->info = bch2_hash_info_init(trans->c, bi);
173 hash_stop_chain(trans, h);
176 static int hash_redo_key(const struct bch_hash_desc desc,
177 struct btree_trans *trans, struct hash_check *h,
178 struct btree_iter *k_iter, struct bkey_s_c k,
181 struct bkey_i delete;
184 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
188 bkey_reassemble(tmp, k);
190 bkey_init(&delete.k);
191 delete.k.p = k_iter->pos;
192 bch2_trans_update(trans, k_iter, &delete, 0);
194 return bch2_hash_set(trans, desc, &h->info, k_iter->pos.inode,
195 tmp, BCH_HASH_SET_MUST_CREATE);
198 static int fsck_hash_delete_at(struct btree_trans *trans,
199 const struct bch_hash_desc desc,
200 struct bch_hash_info *info,
201 struct btree_iter *iter)
205 ret = bch2_hash_delete_at(trans, desc, info, iter) ?:
206 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, k), buf))) {
317 ret = __bch2_trans_do(trans, NULL, NULL,
318 BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
319 hash_redo_key(desc, trans, h, k_iter, k, hashed));
321 bch_err(c, "hash_redo_key err %i", ret);
327 ret = hash_check_duplicates(trans, desc, h, k_iter, k);
332 static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h,
333 struct btree_iter *iter, struct bkey_s_c *k)
335 struct bch_fs *c = trans->c;
336 struct bkey_i_dirent *d = NULL;
342 if (key_has_correct_hash(trans, bch2_dirent_hash_desc, h, iter, *k))
345 len = bch2_dirent_name_bytes(bkey_s_c_to_dirent(*k));
348 memcpy(buf, bkey_s_c_to_dirent(*k).v->d_name, len);
351 d = kmalloc(bkey_bytes(k->k), GFP_KERNEL);
353 bch_err(c, "memory allocation failure");
357 bkey_reassemble(&d->k_i, *k);
364 d->k.u64s = BKEY_U64s + dirent_val_u64s(len);
366 BUG_ON(bkey_val_bytes(&d->k) <
367 offsetof(struct bch_dirent, d_name) + len);
369 memset(d->v.d_name + len, 0,
370 bkey_val_bytes(&d->k) -
371 offsetof(struct bch_dirent, d_name) - len);
373 hash = bch2_dirent_hash_desc.hash_bkey(&h->info,
374 bkey_i_to_s_c(&d->k_i));
375 } while (hash < h->chain->pos.offset ||
376 hash > k->k->p.offset);
378 if (fsck_err(c, "dirent with junk at end, was %s (%zu) now %s (%u)",
379 buf, strlen(buf), d->v.d_name, len)) {
380 ret = __bch2_trans_do(trans, NULL, NULL,
382 BTREE_INSERT_LAZY_RW,
383 (bch2_trans_update(trans, iter, &d->k_i, 0), 0));
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 = __bch2_trans_do(trans, NULL, NULL,
406 BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
407 hash_redo_key(bch2_dirent_hash_desc, trans,
410 bch_err(c, "hash_redo_key err %i", ret);
418 static int bch2_inode_truncate(struct bch_fs *c, u64 inode_nr, u64 new_size)
420 return bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
421 POS(inode_nr, round_up(new_size, block_bytes(c)) >> 9),
422 POS(inode_nr + 1, 0), NULL);
425 static int bch2_fix_overlapping_extent(struct btree_trans *trans,
426 struct btree_iter *iter,
427 struct bkey_s_c k, struct bpos cut_at)
429 struct btree_iter *u_iter;
433 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
434 ret = PTR_ERR_OR_ZERO(u);
438 bkey_reassemble(u, k);
439 bch2_cut_front(cut_at, u);
441 u_iter = bch2_trans_copy_iter(trans, iter);
442 ret = PTR_ERR_OR_ZERO(u_iter);
447 * We don't want to go through the
448 * extent_handle_overwrites path:
450 __bch2_btree_iter_set_pos(u_iter, u->k.p, false);
453 * XXX: this is going to leave disk space
454 * accounting slightly wrong
456 ret = bch2_trans_update(trans, u_iter, u, 0);
457 bch2_trans_iter_put(trans, u_iter);
462 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
463 * that i_size an i_sectors are consistent
466 static int check_extents(struct bch_fs *c)
468 struct inode_walker w = inode_walker_init();
469 struct btree_trans trans;
470 struct btree_iter *iter;
472 struct bkey prev = KEY(0, 0, 0);
476 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
478 bch_verbose(c, "checking extents");
480 iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
481 POS(BCACHEFS_ROOT_INO, 0), 0);
483 for_each_btree_key_continue(iter, 0, k, ret) {
484 if (bkey_cmp(prev.p, bkey_start_pos(k.k)) > 0) {
488 bch2_bkey_to_text(&PBUF(buf1), &prev);
489 bch2_bkey_to_text(&PBUF(buf2), k.k);
491 if (fsck_err(c, "overlapping extents: %s, %s", buf1, buf2)) {
492 ret = __bch2_trans_do(&trans, NULL, NULL,
494 BTREE_INSERT_LAZY_RW,
495 bch2_fix_overlapping_extent(&trans,
503 ret = walk_inode(&trans, &w, k.k->p.inode);
507 if (fsck_err_on(!w.have_inode, c,
508 "extent type %u for missing inode %llu",
509 k.k->type, k.k->p.inode) ||
510 fsck_err_on(w.have_inode &&
511 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
512 "extent type %u for non regular file, inode %llu mode %o",
513 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
514 bch2_trans_unlock(&trans);
516 ret = bch2_inode_truncate(c, k.k->p.inode, 0);
522 if (fsck_err_on(w.first_this_inode &&
524 !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
525 w.inode.bi_sectors !=
526 (i_sectors = bch2_count_inode_sectors(&trans, w.cur_inum)),
527 c, "i_sectors wrong: got %llu, should be %llu",
528 w.inode.bi_sectors, i_sectors)) {
529 struct bkey_inode_buf p;
531 w.inode.bi_sectors = i_sectors;
533 bch2_trans_unlock(&trans);
535 bch2_inode_pack(&p, &w.inode);
537 ret = bch2_btree_insert(c, BTREE_ID_INODES,
538 &p.inode.k_i, NULL, NULL,
540 BTREE_INSERT_LAZY_RW);
542 bch_err(c, "error in fsck: error %i updating inode", ret);
546 /* revalidate iterator: */
547 k = bch2_btree_iter_peek(iter);
550 if (fsck_err_on(w.have_inode &&
551 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
552 k.k->type != KEY_TYPE_reservation &&
553 k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
554 "extent type %u offset %llu past end of inode %llu, i_size %llu",
555 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
556 bch2_trans_unlock(&trans);
558 ret = bch2_inode_truncate(c, k.k->p.inode,
569 return bch2_trans_exit(&trans) ?: ret;
573 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
577 static int check_dirents(struct bch_fs *c)
579 struct inode_walker w = inode_walker_init();
581 struct btree_trans trans;
582 struct btree_iter *iter;
588 bch_verbose(c, "checking dirents");
590 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
594 iter = bch2_trans_get_iter(&trans, BTREE_ID_DIRENTS,
595 POS(BCACHEFS_ROOT_INO, 0), 0);
597 for_each_btree_key_continue(iter, 0, k, ret) {
598 struct bkey_s_c_dirent d;
599 struct bch_inode_unpacked target;
603 ret = walk_inode(&trans, &w, k.k->p.inode);
607 if (fsck_err_on(!w.have_inode, c,
608 "dirent in nonexisting directory:\n%s",
609 (bch2_bkey_val_to_text(&PBUF(buf), c,
611 fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
612 "dirent in non directory inode type %u:\n%s",
613 mode_to_type(w.inode.bi_mode),
614 (bch2_bkey_val_to_text(&PBUF(buf), c,
616 ret = bch2_btree_delete_at(&trans, iter, 0);
622 if (w.first_this_inode && w.have_inode)
623 hash_check_set_inode(&trans, &h, &w.inode);
625 ret = check_dirent_hash(&trans, &h, iter, &k);
636 if (k.k->type != KEY_TYPE_dirent)
639 d = bkey_s_c_to_dirent(k);
640 d_inum = le64_to_cpu(d.v->d_inum);
642 name_len = bch2_dirent_name_bytes(d);
644 if (fsck_err_on(!name_len, c, "empty dirent") ||
645 fsck_err_on(name_len == 1 &&
646 !memcmp(d.v->d_name, ".", 1), c,
648 fsck_err_on(name_len == 2 &&
649 !memcmp(d.v->d_name, "..", 2), c,
651 fsck_err_on(name_len == 2 &&
652 !memcmp(d.v->d_name, "..", 2), c,
654 fsck_err_on(memchr(d.v->d_name, '/', name_len), c,
655 "dirent name has invalid chars")) {
656 ret = remove_dirent(&trans, d);
662 if (fsck_err_on(d_inum == d.k->p.inode, c,
663 "dirent points to own directory:\n%s",
664 (bch2_bkey_val_to_text(&PBUF(buf), c,
666 ret = remove_dirent(&trans, d);
672 ret = bch2_inode_find_by_inum_trans(&trans, d_inum, &target);
673 if (ret && ret != -ENOENT)
679 if (fsck_err_on(!have_target, c,
680 "dirent points to missing inode:\n%s",
681 (bch2_bkey_val_to_text(&PBUF(buf), c,
683 ret = remove_dirent(&trans, d);
689 if (fsck_err_on(have_target &&
691 mode_to_type(target.bi_mode), c,
692 "incorrect d_type: should be %u:\n%s",
693 mode_to_type(target.bi_mode),
694 (bch2_bkey_val_to_text(&PBUF(buf), c,
696 struct bkey_i_dirent *n;
698 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
704 bkey_reassemble(&n->k_i, d.s_c);
705 n->v.d_type = mode_to_type(target.bi_mode);
707 ret = __bch2_trans_do(&trans, NULL, NULL,
709 BTREE_INSERT_LAZY_RW,
710 (bch2_trans_update(&trans, iter, &n->k_i, 0), 0));
718 hash_stop_chain(&trans, &h);
724 return bch2_trans_exit(&trans) ?: ret;
728 * Walk xattrs: verify that they all have a corresponding inode
731 static int check_xattrs(struct bch_fs *c)
733 struct inode_walker w = inode_walker_init();
735 struct btree_trans trans;
736 struct btree_iter *iter;
740 bch_verbose(c, "checking xattrs");
744 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
746 iter = bch2_trans_get_iter(&trans, BTREE_ID_XATTRS,
747 POS(BCACHEFS_ROOT_INO, 0), 0);
749 for_each_btree_key_continue(iter, 0, k, ret) {
750 ret = walk_inode(&trans, &w, k.k->p.inode);
754 if (fsck_err_on(!w.have_inode, c,
755 "xattr for missing inode %llu",
757 ret = bch2_btree_delete_at(&trans, iter, 0);
763 if (w.first_this_inode && w.have_inode)
764 hash_check_set_inode(&trans, &h, &w.inode);
766 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
775 return bch2_trans_exit(&trans) ?: ret;
778 /* Get root directory, create if it doesn't exist: */
779 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
781 struct bkey_inode_buf packed;
784 bch_verbose(c, "checking root directory");
786 ret = bch2_inode_find_by_inum(c, BCACHEFS_ROOT_INO, root_inode);
787 if (ret && ret != -ENOENT)
790 if (fsck_err_on(ret, c, "root directory missing"))
793 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
794 "root inode not a directory"))
801 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
803 root_inode->bi_inum = BCACHEFS_ROOT_INO;
805 bch2_inode_pack(&packed, root_inode);
807 return bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
810 BTREE_INSERT_LAZY_RW);
813 /* Get lost+found, create if it doesn't exist: */
814 static int check_lostfound(struct bch_fs *c,
815 struct bch_inode_unpacked *root_inode,
816 struct bch_inode_unpacked *lostfound_inode)
818 struct qstr lostfound = QSTR("lost+found");
819 struct bch_hash_info root_hash_info =
820 bch2_hash_info_init(c, root_inode);
824 bch_verbose(c, "checking lost+found");
826 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
829 bch_notice(c, "creating lost+found");
830 goto create_lostfound;
833 ret = bch2_inode_find_by_inum(c, inum, lostfound_inode);
834 if (ret && ret != -ENOENT)
837 if (fsck_err_on(ret, c, "lost+found missing"))
838 goto create_lostfound;
840 if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
841 "lost+found inode not a directory"))
842 goto create_lostfound;
848 bch2_inode_init_early(c, lostfound_inode);
850 ret = bch2_trans_do(c, NULL, NULL,
852 BTREE_INSERT_LAZY_RW,
853 bch2_create_trans(&trans,
854 BCACHEFS_ROOT_INO, root_inode,
855 lostfound_inode, &lostfound,
856 0, 0, S_IFDIR|0700, 0, NULL, NULL));
858 bch_err(c, "error creating lost+found: %i", ret);
863 struct inode_bitmap {
868 static inline bool inode_bitmap_test(struct inode_bitmap *b, size_t nr)
870 return nr < b->size ? test_bit(nr, b->bits) : false;
873 static inline int inode_bitmap_set(struct inode_bitmap *b, size_t nr)
876 size_t new_size = max_t(size_t, max_t(size_t,
882 new_size = roundup_pow_of_two(new_size);
883 n = krealloc(b->bits, new_size / 8, GFP_KERNEL|__GFP_ZERO);
892 __set_bit(nr, b->bits);
900 struct pathbuf_entry {
906 static int path_down(struct pathbuf *p, u64 inum)
908 if (p->nr == p->size) {
909 size_t new_size = max_t(size_t, 256UL, p->size * 2);
910 void *n = krealloc(p->entries,
911 new_size * sizeof(p->entries[0]),
920 p->entries[p->nr++] = (struct pathbuf_entry) {
928 static int check_directory_structure(struct bch_fs *c,
929 struct bch_inode_unpacked *lostfound_inode)
931 struct inode_bitmap dirs_done = { NULL, 0 };
932 struct pathbuf path = { 0, 0, NULL };
933 struct pathbuf_entry *e;
934 struct btree_trans trans;
935 struct btree_iter *iter;
937 struct bkey_s_c_dirent dirent;
938 bool had_unreachable;
942 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
944 bch_verbose(c, "checking directory structure");
948 had_unreachable = false;
950 ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
952 bch_err(c, "memory allocation failure in inode_bitmap_set()");
956 ret = path_down(&path, BCACHEFS_ROOT_INO);
962 e = &path.entries[path.nr - 1];
964 if (e->offset == U64_MAX)
967 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS,
968 POS(e->inum, e->offset + 1), 0, k, ret) {
969 if (k.k->p.inode != e->inum)
972 e->offset = k.k->p.offset;
974 if (k.k->type != KEY_TYPE_dirent)
977 dirent = bkey_s_c_to_dirent(k);
979 if (dirent.v->d_type != DT_DIR)
982 d_inum = le64_to_cpu(dirent.v->d_inum);
984 if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
985 "directory %llu has multiple hardlinks",
987 ret = remove_dirent(&trans, dirent);
993 ret = inode_bitmap_set(&dirs_done, d_inum);
995 bch_err(c, "memory allocation failure in inode_bitmap_set()");
999 ret = path_down(&path, d_inum);
1004 ret = bch2_trans_iter_free(&trans, iter);
1006 bch_err(c, "btree error %i in fsck", ret);
1011 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
1013 bch_err(c, "btree error %i in fsck", ret);
1020 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS_MIN, 0);
1022 for_each_btree_key_continue(iter, 0, k, ret) {
1023 if (k.k->type != KEY_TYPE_inode)
1026 if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
1029 ret = bch2_empty_dir_trans(&trans, k.k->p.inode);
1035 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.inode), c,
1036 "unreachable directory found (inum %llu)",
1038 bch2_trans_unlock(&trans);
1040 ret = reattach_inode(c, lostfound_inode, k.k->p.inode);
1045 had_unreachable = true;
1048 bch2_trans_iter_free(&trans, iter);
1052 if (had_unreachable) {
1053 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
1054 kfree(dirs_done.bits);
1055 kfree(path.entries);
1056 memset(&dirs_done, 0, sizeof(dirs_done));
1057 memset(&path, 0, sizeof(path));
1062 ret = bch2_trans_exit(&trans) ?: ret;
1063 kfree(dirs_done.bits);
1064 kfree(path.entries);
1073 typedef GENRADIX(struct nlink) nlink_table;
1075 static void inc_link(struct bch_fs *c, nlink_table *links,
1076 u64 range_start, u64 *range_end,
1081 if (inum < range_start || inum >= *range_end)
1084 link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
1086 bch_verbose(c, "allocation failed during fsck - will need another pass");
1098 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
1099 u64 range_start, u64 *range_end)
1101 struct btree_trans trans;
1102 struct btree_iter *iter;
1104 struct bkey_s_c_dirent d;
1108 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1110 inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
1112 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS, POS_MIN, 0, k, ret) {
1113 switch (k.k->type) {
1114 case KEY_TYPE_dirent:
1115 d = bkey_s_c_to_dirent(k);
1116 d_inum = le64_to_cpu(d.v->d_inum);
1118 if (d.v->d_type == DT_DIR)
1119 inc_link(c, links, range_start, range_end,
1120 d.k->p.inode, true);
1122 inc_link(c, links, range_start, range_end,
1128 bch2_trans_cond_resched(&trans);
1130 ret = bch2_trans_exit(&trans) ?: ret;
1132 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
1137 static int check_inode_nlink(struct bch_fs *c,
1138 struct bch_inode_unpacked *lostfound_inode,
1139 struct bch_inode_unpacked *u,
1143 u32 i_nlink = bch2_inode_nlink_get(u);
1145 link->count * nlink_bias(u->bi_mode) +
1150 * These should have been caught/fixed by earlier passes, we don't
1153 if (S_ISDIR(u->bi_mode) && link->count > 1) {
1154 need_fsck_err(c, "directory %llu with multiple hardlinks: %u",
1155 u->bi_inum, link->count);
1159 if (S_ISDIR(u->bi_mode) && !link->count) {
1160 need_fsck_err(c, "unreachable directory found (inum %llu)",
1165 if (!S_ISDIR(u->bi_mode) && link->dir_count) {
1166 need_fsck_err(c, "non directory with subdirectories",
1172 !(u->bi_flags & BCH_INODE_UNLINKED) &&
1173 (c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
1174 if (fsck_err(c, "unreachable inode %llu not marked as unlinked (type %u)",
1175 u->bi_inum, mode_to_type(u->bi_mode)) ==
1179 ret = reattach_inode(c, lostfound_inode, u->bi_inum);
1184 real_i_nlink = nlink_bias(u->bi_mode) + link->dir_count;
1188 if (i_nlink < link->count) {
1189 if (fsck_err(c, "inode %llu i_link too small (%u < %u, type %i)",
1190 u->bi_inum, i_nlink, link->count,
1191 mode_to_type(u->bi_mode)) == FSCK_ERR_IGNORE)
1196 if (i_nlink != real_i_nlink &&
1198 if (fsck_err(c, "filesystem marked clean, "
1199 "but inode %llu has wrong i_nlink "
1200 "(type %u i_nlink %u, should be %u)",
1201 u->bi_inum, mode_to_type(u->bi_mode),
1202 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1207 if (i_nlink != real_i_nlink &&
1208 (c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
1209 if (fsck_err(c, "inode %llu has wrong i_nlink "
1210 "(type %u i_nlink %u, should be %u)",
1211 u->bi_inum, mode_to_type(u->bi_mode),
1212 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1217 if (real_i_nlink && i_nlink != real_i_nlink)
1218 bch_verbose(c, "setting inode %llu nlink from %u to %u",
1219 u->bi_inum, i_nlink, real_i_nlink);
1221 if (i_nlink != real_i_nlink) {
1222 bch2_inode_nlink_set(u, real_i_nlink);
1229 static int check_inode(struct btree_trans *trans,
1230 struct bch_inode_unpacked *lostfound_inode,
1231 struct btree_iter *iter,
1232 struct bkey_s_c_inode inode,
1235 struct bch_fs *c = trans->c;
1236 struct bch_inode_unpacked u;
1237 bool do_update = false;
1240 ret = bch2_inode_unpack(inode, &u);
1242 bch2_trans_unlock(trans);
1244 if (bch2_fs_inconsistent_on(ret, c,
1245 "error unpacking inode %llu in fsck",
1250 ret = check_inode_nlink(c, lostfound_inode, &u, link,
1256 if (u.bi_flags & BCH_INODE_UNLINKED &&
1258 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
1260 bch_verbose(c, "deleting inode %llu", u.bi_inum);
1262 ret = bch2_inode_rm(c, u.bi_inum);
1264 bch_err(c, "error in fsck: error %i while deleting inode", ret);
1268 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
1270 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
1272 bch_verbose(c, "truncating inode %llu", u.bi_inum);
1275 * XXX: need to truncate partial blocks too here - or ideally
1276 * just switch units to bytes and that issue goes away
1279 ret = bch2_inode_truncate(c, u.bi_inum, u.bi_size);
1281 bch_err(c, "error in fsck: error %i truncating inode", ret);
1286 * We truncated without our normal sector accounting hook, just
1287 * make sure we recalculate it:
1289 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
1291 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
1295 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
1297 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
1301 bch_verbose(c, "recounting sectors for inode %llu",
1304 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
1306 bch_err(c, "error in fsck: error %i recounting inode sectors",
1311 u.bi_sectors = sectors;
1312 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
1317 struct bkey_inode_buf p;
1319 bch2_inode_pack(&p, &u);
1321 ret = __bch2_trans_do(trans, NULL, NULL,
1322 BTREE_INSERT_NOFAIL|
1323 BTREE_INSERT_LAZY_RW,
1324 (bch2_trans_update(trans, iter, &p.inode.k_i, 0), 0));
1326 bch_err(c, "error in fsck: error %i "
1327 "updating inode", ret);
1334 static int bch2_gc_walk_inodes(struct bch_fs *c,
1335 struct bch_inode_unpacked *lostfound_inode,
1337 u64 range_start, u64 range_end)
1339 struct btree_trans trans;
1340 struct btree_iter *iter;
1342 struct nlink *link, zero_links = { 0, 0 };
1343 struct genradix_iter nlinks_iter;
1344 int ret = 0, ret2 = 0;
1347 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1349 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES,
1350 POS(range_start, 0), 0);
1351 nlinks_iter = genradix_iter_init(links, 0);
1353 while ((k = bch2_btree_iter_peek(iter)).k &&
1354 !(ret2 = bkey_err(k))) {
1355 peek_nlinks: link = genradix_iter_peek(&nlinks_iter, links);
1357 if (!link && (!k.k || iter->pos.inode >= range_end))
1360 nlinks_pos = range_start + nlinks_iter.pos;
1361 if (iter->pos.inode > nlinks_pos) {
1362 /* Should have been caught by dirents pass: */
1363 need_fsck_err_on(link && link->count, c,
1364 "missing inode %llu (nlink %u)",
1365 nlinks_pos, link->count);
1366 genradix_iter_advance(&nlinks_iter, links);
1370 if (iter->pos.inode < nlinks_pos || !link)
1373 if (k.k && k.k->type == KEY_TYPE_inode) {
1374 ret = check_inode(&trans, lostfound_inode, iter,
1375 bkey_s_c_to_inode(k), link);
1376 BUG_ON(ret == -EINTR);
1380 /* Should have been caught by dirents pass: */
1381 need_fsck_err_on(link->count, c,
1382 "missing inode %llu (nlink %u)",
1383 nlinks_pos, link->count);
1386 if (nlinks_pos == iter->pos.inode)
1387 genradix_iter_advance(&nlinks_iter, links);
1389 bch2_btree_iter_next(iter);
1390 bch2_trans_cond_resched(&trans);
1393 bch2_trans_exit(&trans);
1396 bch_err(c, "error in fsck: btree error %i while walking inodes", ret2);
1402 static int check_inode_nlinks(struct bch_fs *c,
1403 struct bch_inode_unpacked *lostfound_inode)
1406 u64 this_iter_range_start, next_iter_range_start = 0;
1409 bch_verbose(c, "checking inode nlinks");
1411 genradix_init(&links);
1414 this_iter_range_start = next_iter_range_start;
1415 next_iter_range_start = U64_MAX;
1417 ret = bch2_gc_walk_dirents(c, &links,
1418 this_iter_range_start,
1419 &next_iter_range_start);
1423 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1424 this_iter_range_start,
1425 next_iter_range_start);
1429 genradix_free(&links);
1430 } while (next_iter_range_start != U64_MAX);
1432 genradix_free(&links);
1438 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1439 * Doesn't fix them yet, mainly because they haven't yet been observed:
1441 int bch2_fsck_full(struct bch_fs *c)
1443 struct bch_inode_unpacked root_inode, lostfound_inode;
1445 return check_extents(c) ?:
1448 check_root(c, &root_inode) ?:
1449 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1450 check_directory_structure(c, &lostfound_inode) ?:
1451 check_inode_nlinks(c, &lostfound_inode);
1454 int bch2_fsck_inode_nlink(struct bch_fs *c)
1456 struct bch_inode_unpacked root_inode, lostfound_inode;
1458 return check_root(c, &root_inode) ?:
1459 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1460 check_inode_nlinks(c, &lostfound_inode);
1463 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1465 struct btree_trans trans;
1466 struct btree_iter *iter;
1468 struct bkey_s_c_inode inode;
1471 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1473 for_each_btree_key(&trans, iter, BTREE_ID_INODES, POS_MIN, 0, k, ret) {
1474 if (k.k->type != KEY_TYPE_inode)
1477 inode = bkey_s_c_to_inode(k);
1479 if (inode.v->bi_flags &
1480 (BCH_INODE_I_SIZE_DIRTY|
1481 BCH_INODE_I_SECTORS_DIRTY|
1482 BCH_INODE_UNLINKED)) {
1483 ret = check_inode(&trans, NULL, iter, inode, NULL);
1484 BUG_ON(ret == -EINTR);
1489 BUG_ON(ret == -EINTR);
1491 return bch2_trans_exit(&trans) ?: ret;