1 // SPDX-License-Identifier: GPL-2.0
4 #include "bkey_on_stack.h"
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 __remove_dirent(struct btree_trans *trans,
42 struct bkey_s_c_dirent dirent)
44 struct bch_fs *c = trans->c;
46 struct bch_inode_unpacked dir_inode;
47 struct bch_hash_info dir_hash_info;
48 u64 dir_inum = dirent.k->p.inode;
52 name.len = bch2_dirent_name_bytes(dirent);
53 buf = bch2_trans_kmalloc(trans, name.len + 1);
57 memcpy(buf, dirent.v->d_name, name.len);
61 ret = bch2_inode_find_by_inum_trans(trans, dir_inum, &dir_inode);
62 if (ret && ret != -EINTR)
63 bch_err(c, "remove_dirent: err %i looking up directory inode", ret);
67 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
69 ret = bch2_hash_delete(trans, bch2_dirent_hash_desc,
70 &dir_hash_info, dir_inum, &name);
71 if (ret && ret != -EINTR)
72 bch_err(c, "remove_dirent: err %i deleting dirent", ret);
79 static int remove_dirent(struct btree_trans *trans,
80 struct bkey_s_c_dirent dirent)
82 return __bch2_trans_do(trans, NULL, NULL,
85 __remove_dirent(trans, dirent));
88 static int reattach_inode(struct bch_fs *c,
89 struct bch_inode_unpacked *lostfound_inode,
92 struct bch_inode_unpacked dir_u, inode_u;
97 snprintf(name_buf, sizeof(name_buf), "%llu", inum);
98 name = (struct qstr) QSTR(name_buf);
100 ret = bch2_trans_do(c, NULL, NULL,
101 BTREE_INSERT_LAZY_RW,
102 bch2_link_trans(&trans, lostfound_inode->bi_inum,
103 inum, &dir_u, &inode_u, &name));
105 bch_err(c, "error %i reattaching inode %llu", ret, inum);
110 struct inode_walker {
111 bool first_this_inode;
114 struct bch_inode_unpacked inode;
117 static struct inode_walker inode_walker_init(void)
119 return (struct inode_walker) {
125 static int walk_inode(struct btree_trans *trans,
126 struct inode_walker *w, u64 inum)
128 if (inum != w->cur_inum) {
129 int ret = bch2_inode_find_by_inum_trans(trans, inum,
132 if (ret && ret != -ENOENT)
135 w->have_inode = !ret;
137 w->first_this_inode = true;
139 w->first_this_inode = false;
146 struct bch_hash_info info;
148 /* start of current chain of hash collisions: */
149 struct btree_iter *chain;
151 /* next offset in current chain of hash collisions: */
155 static void hash_check_init(struct hash_check *h)
161 static void hash_stop_chain(struct btree_trans *trans,
162 struct hash_check *h)
165 bch2_trans_iter_free(trans, h->chain);
169 static void hash_check_set_inode(struct btree_trans *trans,
170 struct hash_check *h,
171 const struct bch_inode_unpacked *bi)
173 h->info = bch2_hash_info_init(trans->c, bi);
174 hash_stop_chain(trans, h);
177 static int hash_redo_key(const struct bch_hash_desc desc,
178 struct btree_trans *trans, struct hash_check *h,
179 struct btree_iter *k_iter, struct bkey_s_c k,
182 struct bkey_i delete;
185 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
189 bkey_reassemble(tmp, k);
191 bkey_init(&delete.k);
192 delete.k.p = k_iter->pos;
193 bch2_trans_update(trans, k_iter, &delete, 0);
195 return bch2_hash_set(trans, desc, &h->info, k_iter->pos.inode,
196 tmp, BCH_HASH_SET_MUST_CREATE);
199 static int fsck_hash_delete_at(struct btree_trans *trans,
200 const struct bch_hash_desc desc,
201 struct bch_hash_info *info,
202 struct btree_iter *iter)
206 ret = bch2_hash_delete_at(trans, desc, info, iter) ?:
207 bch2_trans_commit(trans, NULL, NULL,
209 BTREE_INSERT_LAZY_RW);
211 ret = bch2_btree_iter_traverse(iter);
219 static int hash_check_duplicates(struct btree_trans *trans,
220 const struct bch_hash_desc desc, struct hash_check *h,
221 struct btree_iter *k_iter, struct bkey_s_c k)
223 struct bch_fs *c = trans->c;
224 struct btree_iter *iter;
229 if (!bkey_cmp(h->chain->pos, k_iter->pos))
232 iter = bch2_trans_copy_iter(trans, h->chain);
233 BUG_ON(IS_ERR(iter));
235 for_each_btree_key_continue(iter, 0, k2, ret) {
236 if (bkey_cmp(k2.k->p, k.k->p) >= 0)
239 if (fsck_err_on(k2.k->type == desc.key_type &&
240 !desc.cmp_bkey(k, k2), c,
241 "duplicate hash table keys:\n%s",
242 (bch2_bkey_val_to_text(&PBUF(buf), c,
244 ret = fsck_hash_delete_at(trans, desc, &h->info, k_iter);
252 bch2_trans_iter_free(trans, iter);
256 static void hash_set_chain_start(struct btree_trans *trans,
257 const struct bch_hash_desc desc,
258 struct hash_check *h,
259 struct btree_iter *k_iter, struct bkey_s_c k)
261 bool hole = (k.k->type != KEY_TYPE_whiteout &&
262 k.k->type != desc.key_type);
264 if (hole || k.k->p.offset > h->chain_end + 1)
265 hash_stop_chain(trans, h);
269 h->chain = bch2_trans_copy_iter(trans, k_iter);
270 BUG_ON(IS_ERR(h->chain));
273 h->chain_end = k.k->p.offset;
277 static bool key_has_correct_hash(struct btree_trans *trans,
278 const struct bch_hash_desc desc,
279 struct hash_check *h,
280 struct btree_iter *k_iter, struct bkey_s_c k)
284 hash_set_chain_start(trans, desc, h, k_iter, k);
286 if (k.k->type != desc.key_type)
289 hash = desc.hash_bkey(&h->info, k);
291 return hash >= h->chain->pos.offset &&
292 hash <= k.k->p.offset;
295 static int hash_check_key(struct btree_trans *trans,
296 const struct bch_hash_desc desc, struct hash_check *h,
297 struct btree_iter *k_iter, struct bkey_s_c k)
299 struct bch_fs *c = trans->c;
304 hash_set_chain_start(trans, desc, h, k_iter, k);
306 if (k.k->type != desc.key_type)
309 hashed = desc.hash_bkey(&h->info, k);
311 if (fsck_err_on(hashed < h->chain->pos.offset ||
312 hashed > k.k->p.offset, c,
313 "hash table key at wrong offset: btree %u, %llu, "
314 "hashed to %llu chain starts at %llu\n%s",
315 desc.btree_id, k.k->p.offset,
316 hashed, h->chain->pos.offset,
317 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf))) {
318 ret = __bch2_trans_do(trans, NULL, NULL,
319 BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
320 hash_redo_key(desc, trans, h, k_iter, k, hashed));
322 bch_err(c, "hash_redo_key err %i", ret);
328 ret = hash_check_duplicates(trans, desc, h, k_iter, k);
333 static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h,
334 struct btree_iter *iter, struct bkey_s_c *k)
336 struct bch_fs *c = trans->c;
337 struct bkey_i_dirent *d = NULL;
343 if (key_has_correct_hash(trans, bch2_dirent_hash_desc, h, iter, *k))
346 len = bch2_dirent_name_bytes(bkey_s_c_to_dirent(*k));
349 memcpy(buf, bkey_s_c_to_dirent(*k).v->d_name, len);
352 d = kmalloc(bkey_bytes(k->k), GFP_KERNEL);
354 bch_err(c, "memory allocation failure");
358 bkey_reassemble(&d->k_i, *k);
365 d->k.u64s = BKEY_U64s + dirent_val_u64s(len);
367 BUG_ON(bkey_val_bytes(&d->k) <
368 offsetof(struct bch_dirent, d_name) + len);
370 memset(d->v.d_name + len, 0,
371 bkey_val_bytes(&d->k) -
372 offsetof(struct bch_dirent, d_name) - len);
374 hash = bch2_dirent_hash_desc.hash_bkey(&h->info,
375 bkey_i_to_s_c(&d->k_i));
376 } while (hash < h->chain->pos.offset ||
377 hash > k->k->p.offset);
379 if (fsck_err(c, "dirent with junk at end, was %s (%zu) now %s (%u)",
380 buf, strlen(buf), d->v.d_name, len)) {
381 ret = __bch2_trans_do(trans, NULL, NULL,
383 BTREE_INSERT_LAZY_RW,
384 (bch2_trans_update(trans, iter, &d->k_i, 0), 0));
388 *k = bch2_btree_iter_peek(iter);
390 BUG_ON(k->k->type != KEY_TYPE_dirent);
397 hash = bch2_dirent_hash_desc.hash_bkey(&h->info, *k);
399 if (fsck_err(c, "cannot fix dirent by removing trailing garbage %s (%zu)\n"
400 "hash table key at wrong offset: btree %u, offset %llu, "
401 "hashed to %llu chain starts at %llu\n%s",
402 buf, strlen(buf), BTREE_ID_DIRENTS,
403 k->k->p.offset, hash, h->chain->pos.offset,
404 (bch2_bkey_val_to_text(&PBUF(buf), c,
406 ret = __bch2_trans_do(trans, NULL, NULL,
407 BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
408 hash_redo_key(bch2_dirent_hash_desc, trans,
411 bch_err(c, "hash_redo_key err %i", ret);
419 static int bch2_inode_truncate(struct bch_fs *c, u64 inode_nr, u64 new_size)
421 return bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
422 POS(inode_nr, round_up(new_size, block_bytes(c)) >> 9),
423 POS(inode_nr + 1, 0), NULL);
426 static int bch2_fix_overlapping_extent(struct btree_trans *trans,
427 struct btree_iter *iter,
428 struct bkey_s_c k, struct bpos cut_at)
430 struct btree_iter *u_iter;
434 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
435 ret = PTR_ERR_OR_ZERO(u);
439 bkey_reassemble(u, k);
440 bch2_cut_front(cut_at, u);
442 u_iter = bch2_trans_copy_iter(trans, iter);
443 ret = PTR_ERR_OR_ZERO(u_iter);
448 * We don't want to go through the
449 * extent_handle_overwrites path:
451 __bch2_btree_iter_set_pos(u_iter, u->k.p, false);
454 * XXX: this is going to leave disk space
455 * accounting slightly wrong
457 ret = bch2_trans_update(trans, u_iter, u, 0);
458 bch2_trans_iter_put(trans, u_iter);
463 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
464 * that i_size an i_sectors are consistent
467 static int check_extents(struct bch_fs *c)
469 struct inode_walker w = inode_walker_init();
470 struct btree_trans trans;
471 struct btree_iter *iter;
473 struct bkey_on_stack prev;
477 bkey_on_stack_init(&prev);
478 prev.k->k = KEY(0, 0, 0);
479 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
481 bch_verbose(c, "checking extents");
483 iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
484 POS(BCACHEFS_ROOT_INO, 0),
487 for_each_btree_key_continue(iter, 0, k, ret) {
488 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
492 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
493 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
495 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2)) {
496 ret = __bch2_trans_do(&trans, NULL, NULL,
498 BTREE_INSERT_LAZY_RW,
499 bch2_fix_overlapping_extent(&trans,
500 iter, k, prev.k->k.p));
505 bkey_on_stack_reassemble(&prev, c, k);
507 ret = walk_inode(&trans, &w, k.k->p.inode);
511 if (fsck_err_on(!w.have_inode, c,
512 "extent type %u for missing inode %llu",
513 k.k->type, k.k->p.inode) ||
514 fsck_err_on(w.have_inode &&
515 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
516 "extent type %u for non regular file, inode %llu mode %o",
517 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
518 bch2_trans_unlock(&trans);
520 ret = bch2_inode_truncate(c, k.k->p.inode, 0);
526 if (fsck_err_on(w.first_this_inode &&
528 !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
529 w.inode.bi_sectors !=
530 (i_sectors = bch2_count_inode_sectors(&trans, w.cur_inum)),
531 c, "inode %llu has incorrect i_sectors: got %llu, should be %llu",
533 w.inode.bi_sectors, i_sectors)) {
534 struct bkey_inode_buf p;
536 w.inode.bi_sectors = i_sectors;
538 bch2_trans_unlock(&trans);
540 bch2_inode_pack(&p, &w.inode);
542 ret = bch2_btree_insert(c, BTREE_ID_INODES,
543 &p.inode.k_i, NULL, NULL,
545 BTREE_INSERT_LAZY_RW);
547 bch_err(c, "error in fsck: error %i updating inode", ret);
551 /* revalidate iterator: */
552 k = bch2_btree_iter_peek(iter);
555 if (fsck_err_on(w.have_inode &&
556 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
557 k.k->type != KEY_TYPE_reservation &&
558 k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
559 "extent type %u offset %llu past end of inode %llu, i_size %llu",
560 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
561 bch2_trans_unlock(&trans);
563 ret = bch2_inode_truncate(c, k.k->p.inode,
574 bkey_on_stack_exit(&prev, c);
575 return bch2_trans_exit(&trans) ?: ret;
579 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
583 static int check_dirents(struct bch_fs *c)
585 struct inode_walker w = inode_walker_init();
587 struct btree_trans trans;
588 struct btree_iter *iter;
594 bch_verbose(c, "checking dirents");
596 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
600 iter = bch2_trans_get_iter(&trans, BTREE_ID_DIRENTS,
601 POS(BCACHEFS_ROOT_INO, 0), 0);
603 for_each_btree_key_continue(iter, 0, k, ret) {
604 struct bkey_s_c_dirent d;
605 struct bch_inode_unpacked target;
609 ret = walk_inode(&trans, &w, k.k->p.inode);
613 if (fsck_err_on(!w.have_inode, c,
614 "dirent in nonexisting directory:\n%s",
615 (bch2_bkey_val_to_text(&PBUF(buf), c,
617 fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
618 "dirent in non directory inode type %u:\n%s",
619 mode_to_type(w.inode.bi_mode),
620 (bch2_bkey_val_to_text(&PBUF(buf), c,
622 ret = bch2_btree_delete_at(&trans, iter, 0);
628 if (w.first_this_inode && w.have_inode)
629 hash_check_set_inode(&trans, &h, &w.inode);
631 ret = check_dirent_hash(&trans, &h, iter, &k);
642 if (k.k->type != KEY_TYPE_dirent)
645 d = bkey_s_c_to_dirent(k);
646 d_inum = le64_to_cpu(d.v->d_inum);
648 name_len = bch2_dirent_name_bytes(d);
650 if (fsck_err_on(!name_len, c, "empty dirent") ||
651 fsck_err_on(name_len == 1 &&
652 !memcmp(d.v->d_name, ".", 1), c,
654 fsck_err_on(name_len == 2 &&
655 !memcmp(d.v->d_name, "..", 2), c,
657 fsck_err_on(name_len == 2 &&
658 !memcmp(d.v->d_name, "..", 2), c,
660 fsck_err_on(memchr(d.v->d_name, '/', name_len), c,
661 "dirent name has invalid chars")) {
662 ret = remove_dirent(&trans, d);
668 if (fsck_err_on(d_inum == d.k->p.inode, c,
669 "dirent points to own directory:\n%s",
670 (bch2_bkey_val_to_text(&PBUF(buf), c,
672 ret = remove_dirent(&trans, d);
678 ret = bch2_inode_find_by_inum_trans(&trans, d_inum, &target);
679 if (ret && ret != -ENOENT)
685 if (fsck_err_on(!have_target, c,
686 "dirent points to missing inode:\n%s",
687 (bch2_bkey_val_to_text(&PBUF(buf), c,
689 ret = remove_dirent(&trans, d);
695 if (fsck_err_on(have_target &&
697 mode_to_type(target.bi_mode), c,
698 "incorrect d_type: should be %u:\n%s",
699 mode_to_type(target.bi_mode),
700 (bch2_bkey_val_to_text(&PBUF(buf), c,
702 struct bkey_i_dirent *n;
704 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
710 bkey_reassemble(&n->k_i, d.s_c);
711 n->v.d_type = mode_to_type(target.bi_mode);
713 ret = __bch2_trans_do(&trans, NULL, NULL,
715 BTREE_INSERT_LAZY_RW,
716 (bch2_trans_update(&trans, iter, &n->k_i, 0), 0));
724 hash_stop_chain(&trans, &h);
730 return bch2_trans_exit(&trans) ?: ret;
734 * Walk xattrs: verify that they all have a corresponding inode
737 static int check_xattrs(struct bch_fs *c)
739 struct inode_walker w = inode_walker_init();
741 struct btree_trans trans;
742 struct btree_iter *iter;
746 bch_verbose(c, "checking xattrs");
750 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
752 iter = bch2_trans_get_iter(&trans, BTREE_ID_XATTRS,
753 POS(BCACHEFS_ROOT_INO, 0), 0);
755 for_each_btree_key_continue(iter, 0, k, ret) {
756 ret = walk_inode(&trans, &w, k.k->p.inode);
760 if (fsck_err_on(!w.have_inode, c,
761 "xattr for missing inode %llu",
763 ret = bch2_btree_delete_at(&trans, iter, 0);
769 if (w.first_this_inode && w.have_inode)
770 hash_check_set_inode(&trans, &h, &w.inode);
772 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
781 return bch2_trans_exit(&trans) ?: ret;
784 /* Get root directory, create if it doesn't exist: */
785 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
787 struct bkey_inode_buf packed;
790 bch_verbose(c, "checking root directory");
792 ret = bch2_inode_find_by_inum(c, BCACHEFS_ROOT_INO, root_inode);
793 if (ret && ret != -ENOENT)
796 if (fsck_err_on(ret, c, "root directory missing"))
799 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
800 "root inode not a directory"))
807 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
809 root_inode->bi_inum = BCACHEFS_ROOT_INO;
811 bch2_inode_pack(&packed, root_inode);
813 return bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
816 BTREE_INSERT_LAZY_RW);
819 /* Get lost+found, create if it doesn't exist: */
820 static int check_lostfound(struct bch_fs *c,
821 struct bch_inode_unpacked *root_inode,
822 struct bch_inode_unpacked *lostfound_inode)
824 struct qstr lostfound = QSTR("lost+found");
825 struct bch_hash_info root_hash_info =
826 bch2_hash_info_init(c, root_inode);
830 bch_verbose(c, "checking lost+found");
832 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
835 bch_notice(c, "creating lost+found");
836 goto create_lostfound;
839 ret = bch2_inode_find_by_inum(c, inum, lostfound_inode);
840 if (ret && ret != -ENOENT)
843 if (fsck_err_on(ret, c, "lost+found missing"))
844 goto create_lostfound;
846 if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
847 "lost+found inode not a directory"))
848 goto create_lostfound;
854 bch2_inode_init_early(c, lostfound_inode);
856 ret = bch2_trans_do(c, NULL, NULL,
858 BTREE_INSERT_LAZY_RW,
859 bch2_create_trans(&trans,
860 BCACHEFS_ROOT_INO, root_inode,
861 lostfound_inode, &lostfound,
862 0, 0, S_IFDIR|0700, 0, NULL, NULL));
864 bch_err(c, "error creating lost+found: %i", ret);
869 struct inode_bitmap {
874 static inline bool inode_bitmap_test(struct inode_bitmap *b, size_t nr)
876 return nr < b->size ? test_bit(nr, b->bits) : false;
879 static inline int inode_bitmap_set(struct inode_bitmap *b, size_t nr)
882 size_t new_size = max_t(size_t, max_t(size_t,
888 new_size = roundup_pow_of_two(new_size);
889 n = krealloc(b->bits, new_size / 8, GFP_KERNEL|__GFP_ZERO);
898 __set_bit(nr, b->bits);
906 struct pathbuf_entry {
912 static int path_down(struct pathbuf *p, u64 inum)
914 if (p->nr == p->size) {
915 size_t new_size = max_t(size_t, 256UL, p->size * 2);
916 void *n = krealloc(p->entries,
917 new_size * sizeof(p->entries[0]),
926 p->entries[p->nr++] = (struct pathbuf_entry) {
934 static int check_directory_structure(struct bch_fs *c,
935 struct bch_inode_unpacked *lostfound_inode)
937 struct inode_bitmap dirs_done = { NULL, 0 };
938 struct pathbuf path = { 0, 0, NULL };
939 struct pathbuf_entry *e;
940 struct btree_trans trans;
941 struct btree_iter *iter;
943 struct bkey_s_c_dirent dirent;
944 bool had_unreachable;
948 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
950 bch_verbose(c, "checking directory structure");
954 had_unreachable = false;
956 ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
958 bch_err(c, "memory allocation failure in inode_bitmap_set()");
962 ret = path_down(&path, BCACHEFS_ROOT_INO);
968 e = &path.entries[path.nr - 1];
970 if (e->offset == U64_MAX)
973 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS,
974 POS(e->inum, e->offset + 1), 0, k, ret) {
975 if (k.k->p.inode != e->inum)
978 e->offset = k.k->p.offset;
980 if (k.k->type != KEY_TYPE_dirent)
983 dirent = bkey_s_c_to_dirent(k);
985 if (dirent.v->d_type != DT_DIR)
988 d_inum = le64_to_cpu(dirent.v->d_inum);
990 if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
991 "directory %llu has multiple hardlinks",
993 ret = remove_dirent(&trans, dirent);
999 ret = inode_bitmap_set(&dirs_done, d_inum);
1001 bch_err(c, "memory allocation failure in inode_bitmap_set()");
1005 ret = path_down(&path, d_inum);
1010 ret = bch2_trans_iter_free(&trans, iter);
1012 bch_err(c, "btree error %i in fsck", ret);
1017 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
1019 bch_err(c, "btree error %i in fsck", ret);
1026 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS_MIN, 0);
1028 for_each_btree_key_continue(iter, 0, k, ret) {
1029 if (k.k->type != KEY_TYPE_inode)
1032 if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
1035 ret = bch2_empty_dir_trans(&trans, k.k->p.inode);
1041 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.inode), c,
1042 "unreachable directory found (inum %llu)",
1044 bch2_trans_unlock(&trans);
1046 ret = reattach_inode(c, lostfound_inode, k.k->p.inode);
1051 had_unreachable = true;
1054 bch2_trans_iter_free(&trans, iter);
1058 if (had_unreachable) {
1059 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
1060 kfree(dirs_done.bits);
1061 kfree(path.entries);
1062 memset(&dirs_done, 0, sizeof(dirs_done));
1063 memset(&path, 0, sizeof(path));
1068 ret = bch2_trans_exit(&trans) ?: ret;
1069 kfree(dirs_done.bits);
1070 kfree(path.entries);
1079 typedef GENRADIX(struct nlink) nlink_table;
1081 static void inc_link(struct bch_fs *c, nlink_table *links,
1082 u64 range_start, u64 *range_end,
1087 if (inum < range_start || inum >= *range_end)
1090 link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
1092 bch_verbose(c, "allocation failed during fsck - will need another pass");
1104 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
1105 u64 range_start, u64 *range_end)
1107 struct btree_trans trans;
1108 struct btree_iter *iter;
1110 struct bkey_s_c_dirent d;
1114 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1116 inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
1118 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS, POS_MIN, 0, k, ret) {
1119 switch (k.k->type) {
1120 case KEY_TYPE_dirent:
1121 d = bkey_s_c_to_dirent(k);
1122 d_inum = le64_to_cpu(d.v->d_inum);
1124 if (d.v->d_type == DT_DIR)
1125 inc_link(c, links, range_start, range_end,
1126 d.k->p.inode, true);
1128 inc_link(c, links, range_start, range_end,
1134 bch2_trans_cond_resched(&trans);
1136 ret = bch2_trans_exit(&trans) ?: ret;
1138 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
1143 static int check_inode_nlink(struct bch_fs *c,
1144 struct bch_inode_unpacked *lostfound_inode,
1145 struct bch_inode_unpacked *u,
1149 u32 i_nlink = bch2_inode_nlink_get(u);
1151 link->count * nlink_bias(u->bi_mode) +
1156 * These should have been caught/fixed by earlier passes, we don't
1159 if (S_ISDIR(u->bi_mode) && link->count > 1) {
1160 need_fsck_err(c, "directory %llu with multiple hardlinks: %u",
1161 u->bi_inum, link->count);
1165 if (S_ISDIR(u->bi_mode) && !link->count) {
1166 need_fsck_err(c, "unreachable directory found (inum %llu)",
1171 if (!S_ISDIR(u->bi_mode) && link->dir_count) {
1172 need_fsck_err(c, "non directory with subdirectories",
1178 !(u->bi_flags & BCH_INODE_UNLINKED) &&
1179 (c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
1180 if (fsck_err(c, "unreachable inode %llu not marked as unlinked (type %u)",
1181 u->bi_inum, mode_to_type(u->bi_mode)) ==
1185 ret = reattach_inode(c, lostfound_inode, u->bi_inum);
1190 real_i_nlink = nlink_bias(u->bi_mode) + link->dir_count;
1194 if (i_nlink < link->count) {
1195 if (fsck_err(c, "inode %llu i_link too small (%u < %u, type %i)",
1196 u->bi_inum, i_nlink, link->count,
1197 mode_to_type(u->bi_mode)) == FSCK_ERR_IGNORE)
1202 if (i_nlink != real_i_nlink &&
1204 if (fsck_err(c, "filesystem marked clean, "
1205 "but inode %llu has wrong i_nlink "
1206 "(type %u i_nlink %u, should be %u)",
1207 u->bi_inum, mode_to_type(u->bi_mode),
1208 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1213 if (i_nlink != real_i_nlink &&
1214 (c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
1215 if (fsck_err(c, "inode %llu has wrong i_nlink "
1216 "(type %u i_nlink %u, should be %u)",
1217 u->bi_inum, mode_to_type(u->bi_mode),
1218 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1223 if (real_i_nlink && i_nlink != real_i_nlink)
1224 bch_verbose(c, "setting inode %llu nlink from %u to %u",
1225 u->bi_inum, i_nlink, real_i_nlink);
1227 if (i_nlink != real_i_nlink) {
1228 bch2_inode_nlink_set(u, real_i_nlink);
1235 static int check_inode(struct btree_trans *trans,
1236 struct bch_inode_unpacked *lostfound_inode,
1237 struct btree_iter *iter,
1238 struct bkey_s_c_inode inode,
1241 struct bch_fs *c = trans->c;
1242 struct bch_inode_unpacked u;
1243 bool do_update = false;
1246 ret = bch2_inode_unpack(inode, &u);
1248 bch2_trans_unlock(trans);
1250 if (bch2_fs_inconsistent_on(ret, c,
1251 "error unpacking inode %llu in fsck",
1256 ret = check_inode_nlink(c, lostfound_inode, &u, link,
1262 if (u.bi_flags & BCH_INODE_UNLINKED &&
1264 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
1266 bch_verbose(c, "deleting inode %llu", u.bi_inum);
1268 ret = bch2_inode_rm(c, u.bi_inum);
1270 bch_err(c, "error in fsck: error %i while deleting inode", ret);
1274 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
1276 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
1278 bch_verbose(c, "truncating inode %llu", u.bi_inum);
1281 * XXX: need to truncate partial blocks too here - or ideally
1282 * just switch units to bytes and that issue goes away
1285 ret = bch2_inode_truncate(c, u.bi_inum, u.bi_size);
1287 bch_err(c, "error in fsck: error %i truncating inode", ret);
1292 * We truncated without our normal sector accounting hook, just
1293 * make sure we recalculate it:
1295 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
1297 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
1301 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
1303 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
1307 bch_verbose(c, "recounting sectors for inode %llu",
1310 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
1312 bch_err(c, "error in fsck: error %i recounting inode sectors",
1317 u.bi_sectors = sectors;
1318 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
1323 struct bkey_inode_buf p;
1325 bch2_inode_pack(&p, &u);
1327 ret = __bch2_trans_do(trans, NULL, NULL,
1328 BTREE_INSERT_NOFAIL|
1329 BTREE_INSERT_LAZY_RW,
1330 (bch2_trans_update(trans, iter, &p.inode.k_i, 0), 0));
1332 bch_err(c, "error in fsck: error %i "
1333 "updating inode", ret);
1340 static int bch2_gc_walk_inodes(struct bch_fs *c,
1341 struct bch_inode_unpacked *lostfound_inode,
1343 u64 range_start, u64 range_end)
1345 struct btree_trans trans;
1346 struct btree_iter *iter;
1348 struct nlink *link, zero_links = { 0, 0 };
1349 struct genradix_iter nlinks_iter;
1350 int ret = 0, ret2 = 0;
1353 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1355 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES,
1356 POS(range_start, 0), 0);
1357 nlinks_iter = genradix_iter_init(links, 0);
1359 while ((k = bch2_btree_iter_peek(iter)).k &&
1360 !(ret2 = bkey_err(k))) {
1361 peek_nlinks: link = genradix_iter_peek(&nlinks_iter, links);
1363 if (!link && (!k.k || iter->pos.inode >= range_end))
1366 nlinks_pos = range_start + nlinks_iter.pos;
1367 if (iter->pos.inode > nlinks_pos) {
1368 /* Should have been caught by dirents pass: */
1369 need_fsck_err_on(link && link->count, c,
1370 "missing inode %llu (nlink %u)",
1371 nlinks_pos, link->count);
1372 genradix_iter_advance(&nlinks_iter, links);
1376 if (iter->pos.inode < nlinks_pos || !link)
1379 if (k.k && k.k->type == KEY_TYPE_inode) {
1380 ret = check_inode(&trans, lostfound_inode, iter,
1381 bkey_s_c_to_inode(k), link);
1382 BUG_ON(ret == -EINTR);
1386 /* Should have been caught by dirents pass: */
1387 need_fsck_err_on(link->count, c,
1388 "missing inode %llu (nlink %u)",
1389 nlinks_pos, link->count);
1392 if (nlinks_pos == iter->pos.inode)
1393 genradix_iter_advance(&nlinks_iter, links);
1395 bch2_btree_iter_next(iter);
1396 bch2_trans_cond_resched(&trans);
1399 bch2_trans_exit(&trans);
1402 bch_err(c, "error in fsck: btree error %i while walking inodes", ret2);
1408 static int check_inode_nlinks(struct bch_fs *c,
1409 struct bch_inode_unpacked *lostfound_inode)
1412 u64 this_iter_range_start, next_iter_range_start = 0;
1415 bch_verbose(c, "checking inode nlinks");
1417 genradix_init(&links);
1420 this_iter_range_start = next_iter_range_start;
1421 next_iter_range_start = U64_MAX;
1423 ret = bch2_gc_walk_dirents(c, &links,
1424 this_iter_range_start,
1425 &next_iter_range_start);
1429 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1430 this_iter_range_start,
1431 next_iter_range_start);
1435 genradix_free(&links);
1436 } while (next_iter_range_start != U64_MAX);
1438 genradix_free(&links);
1444 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1445 * Doesn't fix them yet, mainly because they haven't yet been observed:
1447 int bch2_fsck_full(struct bch_fs *c)
1449 struct bch_inode_unpacked root_inode, lostfound_inode;
1451 return check_extents(c) ?:
1454 check_root(c, &root_inode) ?:
1455 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1456 check_directory_structure(c, &lostfound_inode) ?:
1457 check_inode_nlinks(c, &lostfound_inode);
1460 int bch2_fsck_inode_nlink(struct bch_fs *c)
1462 struct bch_inode_unpacked root_inode, lostfound_inode;
1464 return check_root(c, &root_inode) ?:
1465 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1466 check_inode_nlinks(c, &lostfound_inode);
1469 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1471 struct btree_trans trans;
1472 struct btree_iter *iter;
1474 struct bkey_s_c_inode inode;
1477 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1479 for_each_btree_key(&trans, iter, BTREE_ID_INODES, POS_MIN, 0, k, ret) {
1480 if (k.k->type != KEY_TYPE_inode)
1483 inode = bkey_s_c_to_inode(k);
1485 if (inode.v->bi_flags &
1486 (BCH_INODE_I_SIZE_DIRTY|
1487 BCH_INODE_I_SECTORS_DIRTY|
1488 BCH_INODE_UNLINKED)) {
1489 ret = check_inode(&trans, NULL, iter, inode, NULL);
1490 BUG_ON(ret == -EINTR);
1495 BUG_ON(ret == -EINTR);
1497 return bch2_trans_exit(&trans) ?: ret;