1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
15 #include <linux/dcache.h> /* struct qstr */
16 #include <linux/generic-radix-tree.h>
18 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
20 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum)
22 struct btree_iter *iter;
27 for_each_btree_key(trans, iter, BTREE_ID_extents,
28 POS(inum, 0), 0, k, ret) {
29 if (k.k->p.inode != inum)
32 if (bkey_extent_is_allocation(k.k))
36 bch2_trans_iter_free(trans, iter);
38 return ret ?: sectors;
41 static int __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, 0);
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,
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);
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_hash_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);
270 h->chain_end = k.k->p.offset;
274 static bool key_has_correct_hash(struct btree_trans *trans,
275 const struct bch_hash_desc desc,
276 struct hash_check *h,
277 struct btree_iter *k_iter, struct bkey_s_c k)
281 hash_set_chain_start(trans, desc, h, k_iter, k);
283 if (k.k->type != desc.key_type)
286 hash = desc.hash_bkey(&h->info, k);
288 return hash >= h->chain->pos.offset &&
289 hash <= k.k->p.offset;
292 static int hash_check_key(struct btree_trans *trans,
293 const struct bch_hash_desc desc, struct hash_check *h,
294 struct btree_iter *k_iter, struct bkey_s_c k)
296 struct bch_fs *c = trans->c;
301 hash_set_chain_start(trans, desc, h, k_iter, k);
303 if (k.k->type != desc.key_type)
306 hashed = desc.hash_bkey(&h->info, k);
308 if (fsck_err_on(hashed < h->chain->pos.offset ||
309 hashed > k.k->p.offset, c,
310 "hash table key at wrong offset: btree %u, %llu, "
311 "hashed to %llu chain starts at %llu\n%s",
312 desc.btree_id, k.k->p.offset,
313 hashed, h->chain->pos.offset,
314 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf))) {
315 ret = __bch2_trans_do(trans, NULL, NULL,
316 BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
317 hash_redo_key(desc, trans, h, k_iter, k, hashed));
319 bch_err(c, "hash_redo_key err %i", ret);
325 ret = hash_check_duplicates(trans, desc, h, k_iter, k);
330 static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h,
331 struct btree_iter *iter, struct bkey_s_c *k)
333 struct bch_fs *c = trans->c;
334 struct bkey_i_dirent *d = NULL;
340 if (key_has_correct_hash(trans, bch2_dirent_hash_desc, h, iter, *k))
343 len = bch2_dirent_name_bytes(bkey_s_c_to_dirent(*k));
346 memcpy(buf, bkey_s_c_to_dirent(*k).v->d_name, len);
349 d = kmalloc(bkey_bytes(k->k), GFP_KERNEL);
351 bch_err(c, "memory allocation failure");
355 bkey_reassemble(&d->k_i, *k);
362 d->k.u64s = BKEY_U64s + dirent_val_u64s(len);
364 BUG_ON(bkey_val_bytes(&d->k) <
365 offsetof(struct bch_dirent, d_name) + len);
367 memset(d->v.d_name + len, 0,
368 bkey_val_bytes(&d->k) -
369 offsetof(struct bch_dirent, d_name) - len);
371 hash = bch2_dirent_hash_desc.hash_bkey(&h->info,
372 bkey_i_to_s_c(&d->k_i));
373 } while (hash < h->chain->pos.offset ||
374 hash > k->k->p.offset);
376 if (fsck_err(c, "dirent with junk at end, was %s (%zu) now %s (%u)",
377 buf, strlen(buf), d->v.d_name, len)) {
378 ret = __bch2_trans_do(trans, NULL, NULL,
380 BTREE_INSERT_LAZY_RW,
381 (bch2_trans_update(trans, iter, &d->k_i, 0), 0));
385 *k = bch2_btree_iter_peek(iter);
387 BUG_ON(k->k->type != KEY_TYPE_dirent);
394 hash = bch2_dirent_hash_desc.hash_bkey(&h->info, *k);
396 if (fsck_err(c, "cannot fix dirent by removing trailing garbage %s (%zu)\n"
397 "hash table key at wrong offset: btree %u, offset %llu, "
398 "hashed to %llu chain starts at %llu\n%s",
399 buf, strlen(buf), BTREE_ID_dirents,
400 k->k->p.offset, hash, h->chain->pos.offset,
401 (bch2_bkey_val_to_text(&PBUF(buf), c,
403 ret = __bch2_trans_do(trans, NULL, NULL,
404 BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
405 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);
423 static int bch2_fix_overlapping_extent(struct btree_trans *trans,
424 struct btree_iter *iter,
425 struct bkey_s_c k, struct bpos cut_at)
427 struct btree_iter *u_iter;
431 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
432 ret = PTR_ERR_OR_ZERO(u);
436 bkey_reassemble(u, k);
437 bch2_cut_front(cut_at, u);
439 u_iter = bch2_trans_copy_iter(trans, iter);
442 * We don't want to go through the
443 * extent_handle_overwrites path:
445 u_iter->flags &= ~BTREE_ITER_IS_EXTENTS;
446 bch2_btree_iter_set_pos(u_iter, u->k.p);
449 * XXX: this is going to leave disk space
450 * accounting slightly wrong
452 ret = bch2_trans_update(trans, u_iter, u, 0);
453 bch2_trans_iter_put(trans, u_iter);
458 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
459 * that i_size an i_sectors are consistent
462 static int check_extents(struct bch_fs *c)
464 struct inode_walker w = inode_walker_init();
465 struct btree_trans trans;
466 struct btree_iter *iter;
468 struct bkey_buf prev;
472 bch2_bkey_buf_init(&prev);
473 prev.k->k = KEY(0, 0, 0);
474 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
476 bch_verbose(c, "checking extents");
478 iter = bch2_trans_get_iter(&trans, BTREE_ID_extents,
479 POS(BCACHEFS_ROOT_INO, 0),
482 for_each_btree_key_continue(iter, 0, k, ret) {
484 * due to retry errors we might see the same extent twice:
486 if (bkey_cmp(prev.k->k.p, k.k->p) &&
487 bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
491 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
492 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
494 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2)) {
495 ret = __bch2_trans_do(&trans, NULL, NULL,
497 BTREE_INSERT_LAZY_RW,
498 bch2_fix_overlapping_extent(&trans,
499 iter, k, prev.k->k.p));
504 bch2_bkey_buf_reassemble(&prev, c, k);
506 ret = walk_inode(&trans, &w, k.k->p.inode);
510 if (fsck_err_on(!w.have_inode, c,
511 "extent type %u for missing inode %llu",
512 k.k->type, k.k->p.inode) ||
513 fsck_err_on(w.have_inode &&
514 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
515 "extent type %u for non regular file, inode %llu mode %o",
516 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
517 bch2_trans_unlock(&trans);
519 ret = bch2_inode_truncate(c, k.k->p.inode, 0);
525 if (fsck_err_on(w.first_this_inode &&
527 !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
528 w.inode.bi_sectors !=
529 (i_sectors = bch2_count_inode_sectors(&trans, w.cur_inum)),
530 c, "inode %llu has incorrect i_sectors: got %llu, should be %llu",
532 w.inode.bi_sectors, i_sectors)) {
533 struct bkey_inode_buf p;
535 w.inode.bi_sectors = i_sectors;
537 bch2_trans_unlock(&trans);
539 bch2_inode_pack(c, &p, &w.inode);
541 ret = bch2_btree_insert(c, BTREE_ID_inodes,
542 &p.inode.k_i, NULL, NULL,
544 BTREE_INSERT_LAZY_RW);
546 bch_err(c, "error in fsck: error %i updating inode", ret);
550 /* revalidate iterator: */
551 k = bch2_btree_iter_peek(iter);
554 if (fsck_err_on(w.have_inode &&
555 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
556 k.k->type != KEY_TYPE_reservation &&
557 k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
558 "extent type %u offset %llu past end of inode %llu, i_size %llu",
559 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
560 bch2_trans_unlock(&trans);
562 ret = bch2_inode_truncate(c, k.k->p.inode,
573 bch2_bkey_buf_exit(&prev, c);
574 return bch2_trans_exit(&trans) ?: ret;
578 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
582 static int check_dirents(struct bch_fs *c)
584 struct inode_walker w = inode_walker_init();
586 struct btree_trans trans;
587 struct btree_iter *iter;
593 bch_verbose(c, "checking dirents");
595 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
599 iter = bch2_trans_get_iter(&trans, BTREE_ID_dirents,
600 POS(BCACHEFS_ROOT_INO, 0), 0);
602 for_each_btree_key_continue(iter, 0, k, ret) {
603 struct bkey_s_c_dirent d;
604 struct bch_inode_unpacked target;
608 ret = walk_inode(&trans, &w, k.k->p.inode);
612 if (fsck_err_on(!w.have_inode, c,
613 "dirent in nonexisting directory:\n%s",
614 (bch2_bkey_val_to_text(&PBUF(buf), c,
616 fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
617 "dirent in non directory inode type %u:\n%s",
618 mode_to_type(w.inode.bi_mode),
619 (bch2_bkey_val_to_text(&PBUF(buf), c,
621 ret = bch2_btree_delete_at(&trans, iter, 0);
627 if (w.first_this_inode && w.have_inode)
628 hash_check_set_inode(&trans, &h, &w.inode);
630 ret = check_dirent_hash(&trans, &h, iter, &k);
641 if (k.k->type != KEY_TYPE_dirent)
644 d = bkey_s_c_to_dirent(k);
645 d_inum = le64_to_cpu(d.v->d_inum);
647 name_len = bch2_dirent_name_bytes(d);
649 if (fsck_err_on(!name_len, c, "empty dirent") ||
650 fsck_err_on(name_len == 1 &&
651 !memcmp(d.v->d_name, ".", 1), c,
653 fsck_err_on(name_len == 2 &&
654 !memcmp(d.v->d_name, "..", 2), c,
656 fsck_err_on(name_len == 2 &&
657 !memcmp(d.v->d_name, "..", 2), c,
659 fsck_err_on(memchr(d.v->d_name, '/', name_len), c,
660 "dirent name has invalid chars")) {
661 ret = remove_dirent(&trans, d);
667 if (fsck_err_on(d_inum == d.k->p.inode, c,
668 "dirent points to own directory:\n%s",
669 (bch2_bkey_val_to_text(&PBUF(buf), c,
671 ret = remove_dirent(&trans, d);
677 ret = __bch2_inode_find_by_inum_trans(&trans, d_inum, &target, 0);
678 if (ret && ret != -ENOENT)
684 if (fsck_err_on(!have_target, c,
685 "dirent points to missing inode:\n%s",
686 (bch2_bkey_val_to_text(&PBUF(buf), c,
688 ret = remove_dirent(&trans, d);
694 if (fsck_err_on(have_target &&
696 mode_to_type(target.bi_mode), c,
697 "incorrect d_type: should be %u:\n%s",
698 mode_to_type(target.bi_mode),
699 (bch2_bkey_val_to_text(&PBUF(buf), c,
701 struct bkey_i_dirent *n;
703 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
709 bkey_reassemble(&n->k_i, d.s_c);
710 n->v.d_type = mode_to_type(target.bi_mode);
712 ret = __bch2_trans_do(&trans, NULL, NULL,
714 BTREE_INSERT_LAZY_RW,
715 (bch2_trans_update(&trans, iter, &n->k_i, 0), 0));
723 hash_stop_chain(&trans, &h);
729 return bch2_trans_exit(&trans) ?: ret;
733 * Walk xattrs: verify that they all have a corresponding inode
736 static int check_xattrs(struct bch_fs *c)
738 struct inode_walker w = inode_walker_init();
740 struct btree_trans trans;
741 struct btree_iter *iter;
745 bch_verbose(c, "checking xattrs");
749 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
751 iter = bch2_trans_get_iter(&trans, BTREE_ID_xattrs,
752 POS(BCACHEFS_ROOT_INO, 0), 0);
754 for_each_btree_key_continue(iter, 0, k, ret) {
755 ret = walk_inode(&trans, &w, k.k->p.inode);
759 if (fsck_err_on(!w.have_inode, c,
760 "xattr for missing inode %llu",
762 ret = bch2_btree_delete_at(&trans, iter, 0);
768 if (w.first_this_inode && w.have_inode)
769 hash_check_set_inode(&trans, &h, &w.inode);
771 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
780 return bch2_trans_exit(&trans) ?: ret;
783 /* Get root directory, create if it doesn't exist: */
784 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
786 struct bkey_inode_buf packed;
789 bch_verbose(c, "checking root directory");
791 ret = bch2_trans_do(c, NULL, NULL, 0,
792 __bch2_inode_find_by_inum_trans(&trans, BCACHEFS_ROOT_INO,
794 if (ret && ret != -ENOENT)
797 if (fsck_err_on(ret, c, "root directory missing"))
800 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
801 "root inode not a directory"))
808 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
810 root_inode->bi_inum = BCACHEFS_ROOT_INO;
812 bch2_inode_pack(c, &packed, root_inode);
814 return bch2_btree_insert(c, BTREE_ID_inodes, &packed.inode.k_i,
817 BTREE_INSERT_LAZY_RW);
820 /* Get lost+found, create if it doesn't exist: */
821 static int check_lostfound(struct bch_fs *c,
822 struct bch_inode_unpacked *root_inode,
823 struct bch_inode_unpacked *lostfound_inode)
825 struct qstr lostfound = QSTR("lost+found");
826 struct bch_hash_info root_hash_info =
827 bch2_hash_info_init(c, root_inode);
831 bch_verbose(c, "checking lost+found");
833 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
836 bch_notice(c, "creating lost+found");
837 goto create_lostfound;
840 ret = bch2_trans_do(c, NULL, NULL, 0,
841 __bch2_inode_find_by_inum_trans(&trans, inum, lostfound_inode, 0));
842 if (ret && ret != -ENOENT)
845 if (fsck_err_on(ret, c, "lost+found missing"))
846 goto create_lostfound;
848 if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
849 "lost+found inode not a directory"))
850 goto create_lostfound;
856 bch2_inode_init_early(c, lostfound_inode);
858 ret = bch2_trans_do(c, NULL, NULL,
860 BTREE_INSERT_LAZY_RW,
861 bch2_create_trans(&trans,
862 BCACHEFS_ROOT_INO, root_inode,
863 lostfound_inode, &lostfound,
864 0, 0, S_IFDIR|0700, 0, NULL, NULL));
866 bch_err(c, "error creating lost+found: %i", ret);
871 typedef GENRADIX(unsigned long) inode_bitmap;
873 static inline bool inode_bitmap_test(inode_bitmap *b, size_t nr)
875 unsigned long *w = genradix_ptr(b, nr / BITS_PER_LONG);
876 return w ? test_bit(nr & (BITS_PER_LONG - 1), w) : false;
879 static inline int inode_bitmap_set(inode_bitmap *b, size_t nr)
881 unsigned long *w = genradix_ptr_alloc(b, nr / BITS_PER_LONG, GFP_KERNEL);
886 *w |= 1UL << (nr & (BITS_PER_LONG - 1));
894 struct pathbuf_entry {
900 static int path_down(struct pathbuf *p, u64 inum)
902 if (p->nr == p->size) {
903 size_t new_size = max_t(size_t, 256UL, p->size * 2);
904 void *n = krealloc(p->entries,
905 new_size * sizeof(p->entries[0]),
914 p->entries[p->nr++] = (struct pathbuf_entry) {
922 static int check_directory_structure(struct bch_fs *c,
923 struct bch_inode_unpacked *lostfound_inode)
925 inode_bitmap dirs_done;
926 struct pathbuf path = { 0, 0, NULL };
927 struct pathbuf_entry *e;
928 struct btree_trans trans;
929 struct btree_iter *iter;
931 struct bkey_s_c_dirent dirent;
932 bool had_unreachable;
936 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
938 bch_verbose(c, "checking directory structure");
942 genradix_init(&dirs_done);
943 had_unreachable = false;
945 ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
947 bch_err(c, "memory allocation failure in inode_bitmap_set()");
951 ret = path_down(&path, BCACHEFS_ROOT_INO);
957 e = &path.entries[path.nr - 1];
959 if (e->offset == U64_MAX)
962 for_each_btree_key(&trans, iter, BTREE_ID_dirents,
963 POS(e->inum, e->offset + 1), 0, k, ret) {
964 if (k.k->p.inode != e->inum)
967 e->offset = k.k->p.offset;
969 if (k.k->type != KEY_TYPE_dirent)
972 dirent = bkey_s_c_to_dirent(k);
974 if (dirent.v->d_type != DT_DIR)
977 d_inum = le64_to_cpu(dirent.v->d_inum);
979 if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
980 "directory %llu has multiple hardlinks",
982 ret = remove_dirent(&trans, dirent);
988 ret = inode_bitmap_set(&dirs_done, d_inum);
990 bch_err(c, "memory allocation failure in inode_bitmap_set()");
994 ret = path_down(&path, d_inum);
999 ret = bch2_trans_iter_free(&trans, iter);
1001 bch_err(c, "btree error %i in fsck", ret);
1006 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
1008 bch_err(c, "btree error %i in fsck", ret);
1015 iter = bch2_trans_get_iter(&trans, BTREE_ID_inodes, POS_MIN, 0);
1017 for_each_btree_key_continue(iter, 0, k, ret) {
1018 if (k.k->type != KEY_TYPE_inode)
1021 if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
1024 ret = bch2_empty_dir_trans(&trans, k.k->p.inode);
1030 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.offset), c,
1031 "unreachable directory found (inum %llu)",
1033 bch2_trans_unlock(&trans);
1035 ret = reattach_inode(c, lostfound_inode, k.k->p.offset);
1040 had_unreachable = true;
1043 bch2_trans_iter_free(&trans, iter);
1047 if (had_unreachable) {
1048 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
1049 genradix_free(&dirs_done);
1050 kfree(path.entries);
1051 memset(&dirs_done, 0, sizeof(dirs_done));
1052 memset(&path, 0, sizeof(path));
1057 ret = bch2_trans_exit(&trans) ?: ret;
1058 genradix_free(&dirs_done);
1059 kfree(path.entries);
1068 typedef GENRADIX(struct nlink) nlink_table;
1070 static void inc_link(struct bch_fs *c, nlink_table *links,
1071 u64 range_start, u64 *range_end,
1076 if (inum < range_start || inum >= *range_end)
1079 if (inum - range_start >= SIZE_MAX / sizeof(struct nlink)) {
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 (inum %llu)",
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);
1264 ret = bch2_inode_rm(c, u.bi_inum, false);
1266 bch_err(c, "error in fsck: error %i while deleting inode", ret);
1270 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
1272 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
1274 bch_verbose(c, "truncating inode %llu", u.bi_inum);
1279 * XXX: need to truncate partial blocks too here - or ideally
1280 * just switch units to bytes and that issue goes away
1283 ret = bch2_inode_truncate(c, u.bi_inum, u.bi_size);
1285 bch_err(c, "error in fsck: error %i truncating inode", ret);
1290 * We truncated without our normal sector accounting hook, just
1291 * make sure we recalculate it:
1293 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
1295 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
1299 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
1301 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
1305 bch_verbose(c, "recounting sectors for inode %llu",
1308 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
1310 bch_err(c, "error in fsck: error %i recounting inode sectors",
1315 u.bi_sectors = sectors;
1316 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
1321 struct bkey_inode_buf p;
1323 bch2_inode_pack(c, &p, &u);
1325 ret = __bch2_trans_do(trans, NULL, NULL,
1326 BTREE_INSERT_NOFAIL|
1327 BTREE_INSERT_LAZY_RW,
1328 (bch2_trans_update(trans, iter, &p.inode.k_i, 0), 0));
1330 bch_err(c, "error in fsck: error %i "
1331 "updating inode", ret);
1338 static int bch2_gc_walk_inodes(struct bch_fs *c,
1339 struct bch_inode_unpacked *lostfound_inode,
1341 u64 range_start, u64 range_end)
1343 struct btree_trans trans;
1344 struct btree_iter *iter;
1346 struct nlink *link, zero_links = { 0, 0 };
1347 struct genradix_iter nlinks_iter;
1348 int ret = 0, ret2 = 0;
1351 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1353 iter = bch2_trans_get_iter(&trans, BTREE_ID_inodes,
1354 POS(0, range_start), 0);
1355 nlinks_iter = genradix_iter_init(links, 0);
1357 while ((k = bch2_btree_iter_peek(iter)).k &&
1358 !(ret2 = bkey_err(k)) &&
1359 iter->pos.offset < range_end) {
1360 peek_nlinks: link = genradix_iter_peek(&nlinks_iter, links);
1362 if (!link && (!k.k || iter->pos.offset >= range_end))
1365 nlinks_pos = range_start + nlinks_iter.pos;
1367 if (link && nlinks_pos < iter->pos.offset) {
1368 /* Should have been caught by dirents pass: */
1369 need_fsck_err_on(link->count, c,
1370 "missing inode %llu (nlink %u)",
1371 nlinks_pos, link->count);
1372 genradix_iter_advance(&nlinks_iter, links);
1376 if (!link || nlinks_pos > iter->pos.offset)
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.offset)
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;