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);
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);
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 __bch2_btree_iter_set_pos(u_iter, u->k.p, false);
448 * XXX: this is going to leave disk space
449 * accounting slightly wrong
451 ret = bch2_trans_update(trans, u_iter, u, 0);
452 bch2_trans_iter_put(trans, u_iter);
457 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
458 * that i_size an i_sectors are consistent
461 static int check_extents(struct bch_fs *c)
463 struct inode_walker w = inode_walker_init();
464 struct btree_trans trans;
465 struct btree_iter *iter;
467 struct bkey_on_stack prev;
471 bkey_on_stack_init(&prev);
472 prev.k->k = KEY(0, 0, 0);
473 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
475 bch_verbose(c, "checking extents");
477 iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
478 POS(BCACHEFS_ROOT_INO, 0),
481 for_each_btree_key_continue(iter, 0, k, ret) {
483 * due to retry errors we might see the same extent twice:
485 if (bkey_cmp(prev.k->k.p, k.k->p) &&
486 bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
490 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
491 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
493 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2)) {
494 ret = __bch2_trans_do(&trans, NULL, NULL,
496 BTREE_INSERT_LAZY_RW,
497 bch2_fix_overlapping_extent(&trans,
498 iter, k, prev.k->k.p));
503 bkey_on_stack_reassemble(&prev, c, k);
505 ret = walk_inode(&trans, &w, k.k->p.inode);
509 if (fsck_err_on(!w.have_inode, c,
510 "extent type %u for missing inode %llu",
511 k.k->type, k.k->p.inode) ||
512 fsck_err_on(w.have_inode &&
513 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
514 "extent type %u for non regular file, inode %llu mode %o",
515 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
516 bch2_trans_unlock(&trans);
518 ret = bch2_inode_truncate(c, k.k->p.inode, 0);
524 if (fsck_err_on(w.first_this_inode &&
526 !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
527 w.inode.bi_sectors !=
528 (i_sectors = bch2_count_inode_sectors(&trans, w.cur_inum)),
529 c, "inode %llu has incorrect i_sectors: got %llu, should be %llu",
531 w.inode.bi_sectors, i_sectors)) {
532 struct bkey_inode_buf p;
534 w.inode.bi_sectors = i_sectors;
536 bch2_trans_unlock(&trans);
538 bch2_inode_pack(c, &p, &w.inode);
540 ret = bch2_btree_insert(c, BTREE_ID_INODES,
541 &p.inode.k_i, NULL, NULL,
543 BTREE_INSERT_LAZY_RW);
545 bch_err(c, "error in fsck: error %i updating inode", ret);
549 /* revalidate iterator: */
550 k = bch2_btree_iter_peek(iter);
553 if (fsck_err_on(w.have_inode &&
554 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
555 k.k->type != KEY_TYPE_reservation &&
556 k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
557 "extent type %u offset %llu past end of inode %llu, i_size %llu",
558 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
559 bch2_trans_unlock(&trans);
561 ret = bch2_inode_truncate(c, k.k->p.inode,
572 bkey_on_stack_exit(&prev, c);
573 return bch2_trans_exit(&trans) ?: ret;
577 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
581 static int check_dirents(struct bch_fs *c)
583 struct inode_walker w = inode_walker_init();
585 struct btree_trans trans;
586 struct btree_iter *iter;
592 bch_verbose(c, "checking dirents");
594 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
598 iter = bch2_trans_get_iter(&trans, BTREE_ID_DIRENTS,
599 POS(BCACHEFS_ROOT_INO, 0), 0);
601 for_each_btree_key_continue(iter, 0, k, ret) {
602 struct bkey_s_c_dirent d;
603 struct bch_inode_unpacked target;
607 ret = walk_inode(&trans, &w, k.k->p.inode);
611 if (fsck_err_on(!w.have_inode, c,
612 "dirent in nonexisting directory:\n%s",
613 (bch2_bkey_val_to_text(&PBUF(buf), c,
615 fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
616 "dirent in non directory inode type %u:\n%s",
617 mode_to_type(w.inode.bi_mode),
618 (bch2_bkey_val_to_text(&PBUF(buf), c,
620 ret = bch2_btree_delete_at(&trans, iter, 0);
626 if (w.first_this_inode && w.have_inode)
627 hash_check_set_inode(&trans, &h, &w.inode);
629 ret = check_dirent_hash(&trans, &h, iter, &k);
640 if (k.k->type != KEY_TYPE_dirent)
643 d = bkey_s_c_to_dirent(k);
644 d_inum = le64_to_cpu(d.v->d_inum);
646 name_len = bch2_dirent_name_bytes(d);
648 if (fsck_err_on(!name_len, c, "empty dirent") ||
649 fsck_err_on(name_len == 1 &&
650 !memcmp(d.v->d_name, ".", 1), c,
652 fsck_err_on(name_len == 2 &&
653 !memcmp(d.v->d_name, "..", 2), c,
655 fsck_err_on(name_len == 2 &&
656 !memcmp(d.v->d_name, "..", 2), c,
658 fsck_err_on(memchr(d.v->d_name, '/', name_len), c,
659 "dirent name has invalid chars")) {
660 ret = remove_dirent(&trans, d);
666 if (fsck_err_on(d_inum == d.k->p.inode, c,
667 "dirent points to own directory:\n%s",
668 (bch2_bkey_val_to_text(&PBUF(buf), c,
670 ret = remove_dirent(&trans, d);
676 ret = bch2_inode_find_by_inum_trans(&trans, d_inum, &target);
677 if (ret && ret != -ENOENT)
683 if (fsck_err_on(!have_target, c,
684 "dirent points to missing inode:\n%s",
685 (bch2_bkey_val_to_text(&PBUF(buf), c,
687 ret = remove_dirent(&trans, d);
693 if (fsck_err_on(have_target &&
695 mode_to_type(target.bi_mode), c,
696 "incorrect d_type: should be %u:\n%s",
697 mode_to_type(target.bi_mode),
698 (bch2_bkey_val_to_text(&PBUF(buf), c,
700 struct bkey_i_dirent *n;
702 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
708 bkey_reassemble(&n->k_i, d.s_c);
709 n->v.d_type = mode_to_type(target.bi_mode);
711 ret = __bch2_trans_do(&trans, NULL, NULL,
713 BTREE_INSERT_LAZY_RW,
714 (bch2_trans_update(&trans, iter, &n->k_i, 0), 0));
722 hash_stop_chain(&trans, &h);
728 return bch2_trans_exit(&trans) ?: ret;
732 * Walk xattrs: verify that they all have a corresponding inode
735 static int check_xattrs(struct bch_fs *c)
737 struct inode_walker w = inode_walker_init();
739 struct btree_trans trans;
740 struct btree_iter *iter;
744 bch_verbose(c, "checking xattrs");
748 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
750 iter = bch2_trans_get_iter(&trans, BTREE_ID_XATTRS,
751 POS(BCACHEFS_ROOT_INO, 0), 0);
753 for_each_btree_key_continue(iter, 0, k, ret) {
754 ret = walk_inode(&trans, &w, k.k->p.inode);
758 if (fsck_err_on(!w.have_inode, c,
759 "xattr for missing inode %llu",
761 ret = bch2_btree_delete_at(&trans, iter, 0);
767 if (w.first_this_inode && w.have_inode)
768 hash_check_set_inode(&trans, &h, &w.inode);
770 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
779 return bch2_trans_exit(&trans) ?: ret;
782 /* Get root directory, create if it doesn't exist: */
783 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
785 struct bkey_inode_buf packed;
788 bch_verbose(c, "checking root directory");
790 ret = bch2_inode_find_by_inum(c, BCACHEFS_ROOT_INO, root_inode);
791 if (ret && ret != -ENOENT)
794 if (fsck_err_on(ret, c, "root directory missing"))
797 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
798 "root inode not a directory"))
805 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
807 root_inode->bi_inum = BCACHEFS_ROOT_INO;
809 bch2_inode_pack(c, &packed, root_inode);
811 return bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
814 BTREE_INSERT_LAZY_RW);
817 /* Get lost+found, create if it doesn't exist: */
818 static int check_lostfound(struct bch_fs *c,
819 struct bch_inode_unpacked *root_inode,
820 struct bch_inode_unpacked *lostfound_inode)
822 struct qstr lostfound = QSTR("lost+found");
823 struct bch_hash_info root_hash_info =
824 bch2_hash_info_init(c, root_inode);
828 bch_verbose(c, "checking lost+found");
830 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
833 bch_notice(c, "creating lost+found");
834 goto create_lostfound;
837 ret = bch2_inode_find_by_inum(c, inum, lostfound_inode);
838 if (ret && ret != -ENOENT)
841 if (fsck_err_on(ret, c, "lost+found missing"))
842 goto create_lostfound;
844 if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
845 "lost+found inode not a directory"))
846 goto create_lostfound;
852 bch2_inode_init_early(c, lostfound_inode);
854 ret = bch2_trans_do(c, NULL, NULL,
856 BTREE_INSERT_LAZY_RW,
857 bch2_create_trans(&trans,
858 BCACHEFS_ROOT_INO, root_inode,
859 lostfound_inode, &lostfound,
860 0, 0, S_IFDIR|0700, 0, NULL, NULL));
862 bch_err(c, "error creating lost+found: %i", ret);
867 typedef GENRADIX(unsigned long) inode_bitmap;
869 static inline bool inode_bitmap_test(inode_bitmap *b, size_t nr)
871 unsigned long *w = genradix_ptr(b, nr / BITS_PER_LONG);
872 return w ? test_bit(nr & (BITS_PER_LONG - 1), w) : false;
875 static inline int inode_bitmap_set(inode_bitmap *b, size_t nr)
877 unsigned long *w = genradix_ptr_alloc(b, nr / BITS_PER_LONG, GFP_KERNEL);
882 *w |= 1UL << (nr & (BITS_PER_LONG - 1));
890 struct pathbuf_entry {
896 static int path_down(struct pathbuf *p, u64 inum)
898 if (p->nr == p->size) {
899 size_t new_size = max_t(size_t, 256UL, p->size * 2);
900 void *n = krealloc(p->entries,
901 new_size * sizeof(p->entries[0]),
910 p->entries[p->nr++] = (struct pathbuf_entry) {
918 static int check_directory_structure(struct bch_fs *c,
919 struct bch_inode_unpacked *lostfound_inode)
921 inode_bitmap dirs_done;
922 struct pathbuf path = { 0, 0, NULL };
923 struct pathbuf_entry *e;
924 struct btree_trans trans;
925 struct btree_iter *iter;
927 struct bkey_s_c_dirent dirent;
928 bool had_unreachable;
932 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
934 bch_verbose(c, "checking directory structure");
938 genradix_init(&dirs_done);
939 had_unreachable = false;
941 ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
943 bch_err(c, "memory allocation failure in inode_bitmap_set()");
947 ret = path_down(&path, BCACHEFS_ROOT_INO);
953 e = &path.entries[path.nr - 1];
955 if (e->offset == U64_MAX)
958 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS,
959 POS(e->inum, e->offset + 1), 0, k, ret) {
960 if (k.k->p.inode != e->inum)
963 e->offset = k.k->p.offset;
965 if (k.k->type != KEY_TYPE_dirent)
968 dirent = bkey_s_c_to_dirent(k);
970 if (dirent.v->d_type != DT_DIR)
973 d_inum = le64_to_cpu(dirent.v->d_inum);
975 if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
976 "directory %llu has multiple hardlinks",
978 ret = remove_dirent(&trans, dirent);
984 ret = inode_bitmap_set(&dirs_done, d_inum);
986 bch_err(c, "memory allocation failure in inode_bitmap_set()");
990 ret = path_down(&path, d_inum);
995 ret = bch2_trans_iter_free(&trans, iter);
997 bch_err(c, "btree error %i in fsck", ret);
1002 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
1004 bch_err(c, "btree error %i in fsck", ret);
1011 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS_MIN, 0);
1013 for_each_btree_key_continue(iter, 0, k, ret) {
1014 if (k.k->type != KEY_TYPE_inode)
1017 if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
1020 ret = bch2_empty_dir_trans(&trans, k.k->p.inode);
1026 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.offset), c,
1027 "unreachable directory found (inum %llu)",
1029 bch2_trans_unlock(&trans);
1031 ret = reattach_inode(c, lostfound_inode, k.k->p.offset);
1036 had_unreachable = true;
1039 bch2_trans_iter_free(&trans, iter);
1043 if (had_unreachable) {
1044 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
1045 genradix_free(&dirs_done);
1046 kfree(path.entries);
1047 memset(&dirs_done, 0, sizeof(dirs_done));
1048 memset(&path, 0, sizeof(path));
1053 ret = bch2_trans_exit(&trans) ?: ret;
1054 genradix_free(&dirs_done);
1055 kfree(path.entries);
1064 typedef GENRADIX(struct nlink) nlink_table;
1066 static void inc_link(struct bch_fs *c, nlink_table *links,
1067 u64 range_start, u64 *range_end,
1072 if (inum < range_start || inum >= *range_end)
1075 link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
1077 bch_verbose(c, "allocation failed during fsck - will need another pass");
1089 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
1090 u64 range_start, u64 *range_end)
1092 struct btree_trans trans;
1093 struct btree_iter *iter;
1095 struct bkey_s_c_dirent d;
1099 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1101 inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
1103 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS, POS_MIN, 0, k, ret) {
1104 switch (k.k->type) {
1105 case KEY_TYPE_dirent:
1106 d = bkey_s_c_to_dirent(k);
1107 d_inum = le64_to_cpu(d.v->d_inum);
1109 if (d.v->d_type == DT_DIR)
1110 inc_link(c, links, range_start, range_end,
1111 d.k->p.inode, true);
1113 inc_link(c, links, range_start, range_end,
1119 bch2_trans_cond_resched(&trans);
1121 ret = bch2_trans_exit(&trans) ?: ret;
1123 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
1128 static int check_inode_nlink(struct bch_fs *c,
1129 struct bch_inode_unpacked *lostfound_inode,
1130 struct bch_inode_unpacked *u,
1134 u32 i_nlink = bch2_inode_nlink_get(u);
1136 link->count * nlink_bias(u->bi_mode) +
1141 * These should have been caught/fixed by earlier passes, we don't
1144 if (S_ISDIR(u->bi_mode) && link->count > 1) {
1145 need_fsck_err(c, "directory %llu with multiple hardlinks: %u",
1146 u->bi_inum, link->count);
1150 if (S_ISDIR(u->bi_mode) && !link->count) {
1151 need_fsck_err(c, "unreachable directory found (inum %llu)",
1156 if (!S_ISDIR(u->bi_mode) && link->dir_count) {
1157 need_fsck_err(c, "non directory with subdirectories (inum %llu)",
1163 !(u->bi_flags & BCH_INODE_UNLINKED) &&
1164 (c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
1165 if (fsck_err(c, "unreachable inode %llu not marked as unlinked (type %u)",
1166 u->bi_inum, mode_to_type(u->bi_mode)) ==
1170 ret = reattach_inode(c, lostfound_inode, u->bi_inum);
1175 real_i_nlink = nlink_bias(u->bi_mode) + link->dir_count;
1179 if (i_nlink < link->count) {
1180 if (fsck_err(c, "inode %llu i_link too small (%u < %u, type %i)",
1181 u->bi_inum, i_nlink, link->count,
1182 mode_to_type(u->bi_mode)) == FSCK_ERR_IGNORE)
1187 if (i_nlink != real_i_nlink &&
1189 if (fsck_err(c, "filesystem marked clean, "
1190 "but inode %llu has wrong i_nlink "
1191 "(type %u i_nlink %u, should be %u)",
1192 u->bi_inum, mode_to_type(u->bi_mode),
1193 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1198 if (i_nlink != real_i_nlink &&
1199 (c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
1200 if (fsck_err(c, "inode %llu has wrong i_nlink "
1201 "(type %u i_nlink %u, should be %u)",
1202 u->bi_inum, mode_to_type(u->bi_mode),
1203 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1208 if (real_i_nlink && i_nlink != real_i_nlink)
1209 bch_verbose(c, "setting inode %llu nlink from %u to %u",
1210 u->bi_inum, i_nlink, real_i_nlink);
1212 if (i_nlink != real_i_nlink) {
1213 bch2_inode_nlink_set(u, real_i_nlink);
1220 static int check_inode(struct btree_trans *trans,
1221 struct bch_inode_unpacked *lostfound_inode,
1222 struct btree_iter *iter,
1223 struct bkey_s_c_inode inode,
1226 struct bch_fs *c = trans->c;
1227 struct bch_inode_unpacked u;
1228 bool do_update = false;
1231 ret = bch2_inode_unpack(inode, &u);
1233 bch2_trans_unlock(trans);
1235 if (bch2_fs_inconsistent_on(ret, c,
1236 "error unpacking inode %llu in fsck",
1241 ret = check_inode_nlink(c, lostfound_inode, &u, link,
1247 if (u.bi_flags & BCH_INODE_UNLINKED &&
1249 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
1251 bch_verbose(c, "deleting inode %llu", u.bi_inum);
1255 ret = bch2_inode_rm(c, u.bi_inum, false);
1257 bch_err(c, "error in fsck: error %i while deleting inode", ret);
1261 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
1263 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
1265 bch_verbose(c, "truncating inode %llu", u.bi_inum);
1270 * XXX: need to truncate partial blocks too here - or ideally
1271 * just switch units to bytes and that issue goes away
1274 ret = bch2_inode_truncate(c, u.bi_inum, u.bi_size);
1276 bch_err(c, "error in fsck: error %i truncating inode", ret);
1281 * We truncated without our normal sector accounting hook, just
1282 * make sure we recalculate it:
1284 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
1286 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
1290 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
1292 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
1296 bch_verbose(c, "recounting sectors for inode %llu",
1299 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
1301 bch_err(c, "error in fsck: error %i recounting inode sectors",
1306 u.bi_sectors = sectors;
1307 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
1312 struct bkey_inode_buf p;
1314 bch2_inode_pack(c, &p, &u);
1316 ret = __bch2_trans_do(trans, NULL, NULL,
1317 BTREE_INSERT_NOFAIL|
1318 BTREE_INSERT_LAZY_RW,
1319 (bch2_trans_update(trans, iter, &p.inode.k_i, 0), 0));
1321 bch_err(c, "error in fsck: error %i "
1322 "updating inode", ret);
1329 static int bch2_gc_walk_inodes(struct bch_fs *c,
1330 struct bch_inode_unpacked *lostfound_inode,
1332 u64 range_start, u64 range_end)
1334 struct btree_trans trans;
1335 struct btree_iter *iter;
1337 struct nlink *link, zero_links = { 0, 0 };
1338 struct genradix_iter nlinks_iter;
1339 int ret = 0, ret2 = 0;
1342 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1344 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES,
1345 POS(0, range_start), 0);
1346 nlinks_iter = genradix_iter_init(links, 0);
1348 while ((k = bch2_btree_iter_peek(iter)).k &&
1349 !(ret2 = bkey_err(k))) {
1350 peek_nlinks: link = genradix_iter_peek(&nlinks_iter, links);
1352 if (!link && (!k.k || iter->pos.offset >= range_end))
1355 nlinks_pos = range_start + nlinks_iter.pos;
1356 if (iter->pos.offset > nlinks_pos) {
1357 /* Should have been caught by dirents pass: */
1358 need_fsck_err_on(link && link->count, c,
1359 "missing inode %llu (nlink %u)",
1360 nlinks_pos, link->count);
1361 genradix_iter_advance(&nlinks_iter, links);
1365 if (iter->pos.offset < nlinks_pos || !link)
1368 if (k.k && k.k->type == KEY_TYPE_inode) {
1369 ret = check_inode(&trans, lostfound_inode, iter,
1370 bkey_s_c_to_inode(k), link);
1371 BUG_ON(ret == -EINTR);
1375 /* Should have been caught by dirents pass: */
1376 need_fsck_err_on(link->count, c,
1377 "missing inode %llu (nlink %u)",
1378 nlinks_pos, link->count);
1381 if (nlinks_pos == iter->pos.offset)
1382 genradix_iter_advance(&nlinks_iter, links);
1384 bch2_btree_iter_next(iter);
1385 bch2_trans_cond_resched(&trans);
1388 bch2_trans_exit(&trans);
1391 bch_err(c, "error in fsck: btree error %i while walking inodes", ret2);
1397 static int check_inode_nlinks(struct bch_fs *c,
1398 struct bch_inode_unpacked *lostfound_inode)
1401 u64 this_iter_range_start, next_iter_range_start = 0;
1404 bch_verbose(c, "checking inode nlinks");
1406 genradix_init(&links);
1409 this_iter_range_start = next_iter_range_start;
1410 next_iter_range_start = U64_MAX;
1412 ret = bch2_gc_walk_dirents(c, &links,
1413 this_iter_range_start,
1414 &next_iter_range_start);
1418 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1419 this_iter_range_start,
1420 next_iter_range_start);
1424 genradix_free(&links);
1425 } while (next_iter_range_start != U64_MAX);
1427 genradix_free(&links);
1433 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1434 * Doesn't fix them yet, mainly because they haven't yet been observed:
1436 int bch2_fsck_full(struct bch_fs *c)
1438 struct bch_inode_unpacked root_inode, lostfound_inode;
1440 return check_extents(c) ?:
1443 check_root(c, &root_inode) ?:
1444 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1445 check_directory_structure(c, &lostfound_inode) ?:
1446 check_inode_nlinks(c, &lostfound_inode);
1449 int bch2_fsck_inode_nlink(struct bch_fs *c)
1451 struct bch_inode_unpacked root_inode, lostfound_inode;
1453 return check_root(c, &root_inode) ?:
1454 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1455 check_inode_nlinks(c, &lostfound_inode);
1458 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1460 struct btree_trans trans;
1461 struct btree_iter *iter;
1463 struct bkey_s_c_inode inode;
1466 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1468 for_each_btree_key(&trans, iter, BTREE_ID_INODES, POS_MIN, 0, k, ret) {
1469 if (k.k->type != KEY_TYPE_inode)
1472 inode = bkey_s_c_to_inode(k);
1474 if (inode.v->bi_flags &
1475 (BCH_INODE_I_SIZE_DIRTY|
1476 BCH_INODE_I_SECTORS_DIRTY|
1477 BCH_INODE_UNLINKED)) {
1478 ret = check_inode(&trans, NULL, iter, inode, NULL);
1479 BUG_ON(ret == -EINTR);
1484 BUG_ON(ret == -EINTR);
1486 return bch2_trans_exit(&trans) ?: ret;