3 #include "btree_update.h"
13 #include <linux/dcache.h> /* struct qstr */
14 #include <linux/generic-radix-tree.h>
16 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
18 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum)
20 struct btree_iter *iter;
24 for_each_btree_key(trans, iter, BTREE_ID_EXTENTS, POS(inum, 0), 0, k) {
25 if (k.k->p.inode != inum)
28 if (bkey_extent_is_allocation(k.k))
32 return bch2_trans_iter_free(trans, iter) ?: sectors;
35 static int remove_dirent(struct btree_trans *trans,
36 struct bkey_s_c_dirent dirent)
38 struct bch_fs *c = trans->c;
40 struct bch_inode_unpacked dir_inode;
41 struct bch_hash_info dir_hash_info;
42 u64 dir_inum = dirent.k->p.inode;
46 name.len = bch2_dirent_name_bytes(dirent);
47 buf = kmalloc(name.len + 1, GFP_KERNEL);
51 memcpy(buf, dirent.v->d_name, name.len);
55 /* Unlock so we don't deadlock, after copying name: */
56 bch2_btree_trans_unlock(trans);
58 ret = bch2_inode_find_by_inum(c, dir_inum, &dir_inode);
60 bch_err(c, "remove_dirent: err %i looking up directory inode", ret);
64 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
66 ret = bch2_dirent_delete(c, dir_inum, &dir_hash_info, &name, NULL);
68 bch_err(c, "remove_dirent: err %i deleting dirent", ret);
74 static int reattach_inode(struct bch_fs *c,
75 struct bch_inode_unpacked *lostfound_inode,
78 struct bch_hash_info lostfound_hash_info =
79 bch2_hash_info_init(c, lostfound_inode);
80 struct bkey_inode_buf packed;
85 snprintf(name_buf, sizeof(name_buf), "%llu", inum);
86 name = (struct qstr) QSTR(name_buf);
88 lostfound_inode->bi_nlink++;
90 bch2_inode_pack(&packed, lostfound_inode);
92 ret = bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
95 BTREE_INSERT_LAZY_RW);
97 bch_err(c, "error %i reattaching inode %llu while updating lost+found",
102 ret = bch2_dirent_create(c, lostfound_inode->bi_inum,
103 &lostfound_hash_info,
104 DT_DIR, &name, inum, NULL,
106 BTREE_INSERT_LAZY_RW);
108 bch_err(c, "error %i reattaching inode %llu while creating new dirent",
115 struct inode_walker {
116 bool first_this_inode;
119 struct bch_inode_unpacked inode;
122 static struct inode_walker inode_walker_init(void)
124 return (struct inode_walker) {
130 static int walk_inode(struct btree_trans *trans,
131 struct inode_walker *w, u64 inum)
133 if (inum != w->cur_inum) {
134 int ret = bch2_inode_find_by_inum_trans(trans, inum,
137 if (ret && ret != -ENOENT)
140 w->have_inode = !ret;
142 w->first_this_inode = true;
144 w->first_this_inode = false;
151 struct bch_hash_info info;
153 /* start of current chain of hash collisions: */
154 struct btree_iter *chain;
156 /* next offset in current chain of hash collisions: */
160 static void hash_check_init(struct hash_check *h)
165 static void hash_stop_chain(struct btree_trans *trans,
166 struct hash_check *h)
169 bch2_trans_iter_free(trans, h->chain);
173 static void hash_check_set_inode(struct btree_trans *trans,
174 struct hash_check *h,
175 const struct bch_inode_unpacked *bi)
177 h->info = bch2_hash_info_init(trans->c, bi);
178 hash_stop_chain(trans, h);
181 static int hash_redo_key(const struct bch_hash_desc desc,
182 struct btree_trans *trans, struct hash_check *h,
183 struct btree_iter *k_iter, struct bkey_s_c k,
189 tmp = kmalloc(bkey_bytes(k.k), GFP_KERNEL);
193 bkey_reassemble(tmp, k);
195 ret = bch2_btree_delete_at(trans, k_iter, 0);
199 bch2_hash_set(trans, desc, &h->info, k_iter->pos.inode,
200 tmp, BCH_HASH_SET_MUST_CREATE);
201 ret = bch2_trans_commit(trans, NULL, NULL,
203 BTREE_INSERT_LAZY_RW);
209 static int fsck_hash_delete_at(struct btree_trans *trans,
210 const struct bch_hash_desc desc,
211 struct bch_hash_info *info,
212 struct btree_iter *iter)
216 ret = bch2_hash_delete_at(trans, desc, info, iter) ?:
217 bch2_trans_commit(trans, NULL, NULL,
220 BTREE_INSERT_LAZY_RW);
222 ret = bch2_btree_iter_traverse(iter);
230 static int hash_check_duplicates(struct btree_trans *trans,
231 const struct bch_hash_desc desc, struct hash_check *h,
232 struct btree_iter *k_iter, struct bkey_s_c k)
234 struct bch_fs *c = trans->c;
235 struct btree_iter *iter;
240 if (!bkey_cmp(h->chain->pos, k_iter->pos))
243 iter = bch2_trans_copy_iter(trans, h->chain);
244 BUG_ON(IS_ERR(iter));
246 for_each_btree_key_continue(iter, 0, k2) {
247 if (bkey_cmp(k2.k->p, k.k->p) >= 0)
250 if (fsck_err_on(k2.k->type == desc.key_type &&
251 !desc.cmp_bkey(k, k2), c,
252 "duplicate hash table keys:\n%s",
253 (bch2_bkey_val_to_text(&PBUF(buf), c,
255 ret = fsck_hash_delete_at(trans, desc, &h->info, k_iter);
263 bch2_trans_iter_free(trans, iter);
267 static void hash_set_chain_start(struct btree_trans *trans,
268 const struct bch_hash_desc desc,
269 struct hash_check *h,
270 struct btree_iter *k_iter, struct bkey_s_c k)
272 bool hole = (k.k->type != KEY_TYPE_whiteout &&
273 k.k->type != desc.key_type);
275 if (hole || k.k->p.offset > h->chain_end + 1)
276 hash_stop_chain(trans, h);
280 h->chain = bch2_trans_copy_iter(trans, k_iter);
281 BUG_ON(IS_ERR(h->chain));
284 h->chain_end = k.k->p.offset;
288 static bool key_has_correct_hash(struct btree_trans *trans,
289 const struct bch_hash_desc desc,
290 struct hash_check *h,
291 struct btree_iter *k_iter, struct bkey_s_c k)
295 hash_set_chain_start(trans, desc, h, k_iter, k);
297 if (k.k->type != desc.key_type)
300 hash = desc.hash_bkey(&h->info, k);
302 return hash >= h->chain->pos.offset &&
303 hash <= k.k->p.offset;
306 static int hash_check_key(struct btree_trans *trans,
307 const struct bch_hash_desc desc, struct hash_check *h,
308 struct btree_iter *k_iter, struct bkey_s_c k)
310 struct bch_fs *c = trans->c;
315 hash_set_chain_start(trans, desc, h, k_iter, k);
317 if (k.k->type != desc.key_type)
320 hashed = desc.hash_bkey(&h->info, k);
322 if (fsck_err_on(hashed < h->chain->pos.offset ||
323 hashed > k.k->p.offset, c,
324 "hash table key at wrong offset: btree %u, %llu, "
325 "hashed to %llu chain starts at %llu\n%s",
326 desc.btree_id, k.k->p.offset,
327 hashed, h->chain->pos.offset,
328 (bch2_bkey_val_to_text(&PBUF(buf), c,
330 ret = hash_redo_key(desc, trans, h, k_iter, k, hashed);
332 bch_err(c, "hash_redo_key err %i", ret);
338 ret = hash_check_duplicates(trans, desc, h, k_iter, k);
343 static int check_dirent_hash(struct btree_trans *trans, struct hash_check *h,
344 struct btree_iter *iter, struct bkey_s_c *k)
346 struct bch_fs *c = trans->c;
347 struct bkey_i_dirent *d = NULL;
353 if (key_has_correct_hash(trans, bch2_dirent_hash_desc, h, iter, *k))
356 len = bch2_dirent_name_bytes(bkey_s_c_to_dirent(*k));
359 memcpy(buf, bkey_s_c_to_dirent(*k).v->d_name, len);
362 d = kmalloc(bkey_bytes(k->k), GFP_KERNEL);
364 bch_err(c, "memory allocation failure");
368 bkey_reassemble(&d->k_i, *k);
375 d->k.u64s = BKEY_U64s + dirent_val_u64s(len);
377 BUG_ON(bkey_val_bytes(&d->k) <
378 offsetof(struct bch_dirent, d_name) + len);
380 memset(d->v.d_name + len, 0,
381 bkey_val_bytes(&d->k) -
382 offsetof(struct bch_dirent, d_name) - len);
384 hash = bch2_dirent_hash_desc.hash_bkey(&h->info,
385 bkey_i_to_s_c(&d->k_i));
386 } while (hash < h->chain->pos.offset ||
387 hash > k->k->p.offset);
389 if (fsck_err(c, "dirent with junk at end, was %s (%zu) now %s (%u)",
390 buf, strlen(buf), d->v.d_name, len)) {
391 bch2_trans_update(trans, BTREE_INSERT_ENTRY(iter, &d->k_i));
393 ret = bch2_trans_commit(trans, NULL, NULL,
395 BTREE_INSERT_LAZY_RW);
399 *k = bch2_btree_iter_peek(iter);
401 BUG_ON(k->k->type != KEY_TYPE_dirent);
408 hash = bch2_dirent_hash_desc.hash_bkey(&h->info, *k);
410 if (fsck_err(c, "cannot fix dirent by removing trailing garbage %s (%zu)\n"
411 "hash table key at wrong offset: btree %u, offset %llu, "
412 "hashed to %llu chain starts at %llu\n%s",
413 buf, strlen(buf), BTREE_ID_DIRENTS,
414 k->k->p.offset, hash, h->chain->pos.offset,
415 (bch2_bkey_val_to_text(&PBUF(buf), c,
417 ret = hash_redo_key(bch2_dirent_hash_desc, trans,
420 bch_err(c, "hash_redo_key err %i", ret);
428 static int bch2_inode_truncate(struct bch_fs *c, u64 inode_nr, u64 new_size)
430 return bch2_btree_delete_range(c, BTREE_ID_EXTENTS,
431 POS(inode_nr, round_up(new_size, block_bytes(c)) >> 9),
432 POS(inode_nr + 1, 0), NULL);
436 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
437 * that i_size an i_sectors are consistent
440 static int check_extents(struct bch_fs *c)
442 struct inode_walker w = inode_walker_init();
443 struct btree_trans trans;
444 struct btree_iter *iter;
449 bch2_trans_init(&trans, c);
450 bch2_trans_preload_iters(&trans);
452 bch_verbose(c, "checking extents");
454 iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
455 POS(BCACHEFS_ROOT_INO, 0), 0);
457 for_each_btree_key_continue(iter, 0, k) {
458 ret = walk_inode(&trans, &w, k.k->p.inode);
462 if (fsck_err_on(!w.have_inode, c,
463 "extent type %u for missing inode %llu",
464 k.k->type, k.k->p.inode) ||
465 fsck_err_on(w.have_inode &&
466 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
467 "extent type %u for non regular file, inode %llu mode %o",
468 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
469 bch2_trans_unlock(&trans);
471 ret = bch2_inode_truncate(c, k.k->p.inode, 0);
477 if (fsck_err_on(w.first_this_inode &&
479 !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
480 w.inode.bi_sectors !=
481 (i_sectors = bch2_count_inode_sectors(&trans, w.cur_inum)),
482 c, "i_sectors wrong: got %llu, should be %llu",
483 w.inode.bi_sectors, i_sectors)) {
484 struct bkey_inode_buf p;
486 w.inode.bi_sectors = i_sectors;
488 bch2_trans_unlock(&trans);
490 bch2_inode_pack(&p, &w.inode);
492 ret = bch2_btree_insert(c, BTREE_ID_INODES,
493 &p.inode.k_i, NULL, NULL,
495 BTREE_INSERT_LAZY_RW);
497 bch_err(c, "error in fs gc: error %i "
498 "updating inode", ret);
502 /* revalidate iterator: */
503 k = bch2_btree_iter_peek(iter);
506 if (fsck_err_on(w.have_inode &&
507 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
508 k.k->type != KEY_TYPE_reservation &&
509 k.k->p.offset > round_up(w.inode.bi_size, PAGE_SIZE) >> 9, c,
510 "extent type %u offset %llu past end of inode %llu, i_size %llu",
511 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
512 bch2_trans_unlock(&trans);
514 ret = bch2_inode_truncate(c, k.k->p.inode,
525 return bch2_trans_exit(&trans) ?: ret;
529 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
533 static int check_dirents(struct bch_fs *c)
535 struct inode_walker w = inode_walker_init();
537 struct btree_trans trans;
538 struct btree_iter *iter;
544 bch_verbose(c, "checking dirents");
546 bch2_trans_init(&trans, c);
547 bch2_trans_preload_iters(&trans);
551 iter = bch2_trans_get_iter(&trans, BTREE_ID_DIRENTS,
552 POS(BCACHEFS_ROOT_INO, 0), 0);
554 for_each_btree_key_continue(iter, 0, k) {
555 struct bkey_s_c_dirent d;
556 struct bch_inode_unpacked target;
560 ret = walk_inode(&trans, &w, k.k->p.inode);
564 if (fsck_err_on(!w.have_inode, c,
565 "dirent in nonexisting directory:\n%s",
566 (bch2_bkey_val_to_text(&PBUF(buf), c,
568 fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
569 "dirent in non directory inode type %u:\n%s",
570 mode_to_type(w.inode.bi_mode),
571 (bch2_bkey_val_to_text(&PBUF(buf), c,
573 ret = bch2_btree_delete_at(&trans, iter, 0);
579 if (w.first_this_inode && w.have_inode)
580 hash_check_set_inode(&trans, &h, &w.inode);
582 ret = check_dirent_hash(&trans, &h, iter, &k);
593 if (k.k->type != KEY_TYPE_dirent)
596 d = bkey_s_c_to_dirent(k);
597 d_inum = le64_to_cpu(d.v->d_inum);
599 name_len = bch2_dirent_name_bytes(d);
601 if (fsck_err_on(!name_len, c, "empty dirent") ||
602 fsck_err_on(name_len == 1 &&
603 !memcmp(d.v->d_name, ".", 1), c,
605 fsck_err_on(name_len == 2 &&
606 !memcmp(d.v->d_name, "..", 2), c,
608 fsck_err_on(name_len == 2 &&
609 !memcmp(d.v->d_name, "..", 2), c,
611 fsck_err_on(memchr(d.v->d_name, '/', name_len), c,
612 "dirent name has invalid chars")) {
613 ret = remove_dirent(&trans, d);
619 if (fsck_err_on(d_inum == d.k->p.inode, c,
620 "dirent points to own directory:\n%s",
621 (bch2_bkey_val_to_text(&PBUF(buf), c,
623 ret = remove_dirent(&trans, d);
629 ret = bch2_inode_find_by_inum_trans(&trans, d_inum, &target);
630 if (ret && ret != -ENOENT)
636 if (fsck_err_on(!have_target, c,
637 "dirent points to missing inode:\n%s",
638 (bch2_bkey_val_to_text(&PBUF(buf), c,
640 ret = remove_dirent(&trans, d);
646 if (fsck_err_on(have_target &&
648 mode_to_type(target.bi_mode), c,
649 "incorrect d_type: should be %u:\n%s",
650 mode_to_type(target.bi_mode),
651 (bch2_bkey_val_to_text(&PBUF(buf), c,
653 struct bkey_i_dirent *n;
655 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
661 bkey_reassemble(&n->k_i, d.s_c);
662 n->v.d_type = mode_to_type(target.bi_mode);
664 bch2_trans_update(&trans,
665 BTREE_INSERT_ENTRY(iter, &n->k_i));
667 ret = bch2_trans_commit(&trans, NULL, NULL,
669 BTREE_INSERT_LAZY_RW);
677 hash_stop_chain(&trans, &h);
683 return bch2_trans_exit(&trans) ?: ret;
687 * Walk xattrs: verify that they all have a corresponding inode
690 static int check_xattrs(struct bch_fs *c)
692 struct inode_walker w = inode_walker_init();
694 struct btree_trans trans;
695 struct btree_iter *iter;
699 bch_verbose(c, "checking xattrs");
703 bch2_trans_init(&trans, c);
704 bch2_trans_preload_iters(&trans);
706 iter = bch2_trans_get_iter(&trans, BTREE_ID_XATTRS,
707 POS(BCACHEFS_ROOT_INO, 0), 0);
709 for_each_btree_key_continue(iter, 0, k) {
710 ret = walk_inode(&trans, &w, k.k->p.inode);
714 if (fsck_err_on(!w.have_inode, c,
715 "xattr for missing inode %llu",
717 ret = bch2_btree_delete_at(&trans, iter, 0);
723 if (w.first_this_inode && w.have_inode)
724 hash_check_set_inode(&trans, &h, &w.inode);
726 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
735 return bch2_trans_exit(&trans) ?: ret;
738 /* Get root directory, create if it doesn't exist: */
739 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
741 struct bkey_inode_buf packed;
744 bch_verbose(c, "checking root directory");
746 ret = bch2_inode_find_by_inum(c, BCACHEFS_ROOT_INO, root_inode);
747 if (ret && ret != -ENOENT)
750 if (fsck_err_on(ret, c, "root directory missing"))
753 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
754 "root inode not a directory"))
761 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO,
763 root_inode->bi_inum = BCACHEFS_ROOT_INO;
765 bch2_inode_pack(&packed, root_inode);
767 return bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
770 BTREE_INSERT_LAZY_RW);
773 /* Get lost+found, create if it doesn't exist: */
774 static int check_lostfound(struct bch_fs *c,
775 struct bch_inode_unpacked *root_inode,
776 struct bch_inode_unpacked *lostfound_inode)
778 struct qstr lostfound = QSTR("lost+found");
779 struct bch_hash_info root_hash_info =
780 bch2_hash_info_init(c, root_inode);
781 struct bkey_inode_buf packed;
785 bch_verbose(c, "checking lost+found");
787 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
790 bch_notice(c, "creating lost+found");
791 goto create_lostfound;
794 ret = bch2_inode_find_by_inum(c, inum, lostfound_inode);
795 if (ret && ret != -ENOENT)
798 if (fsck_err_on(ret, c, "lost+found missing"))
799 goto create_lostfound;
801 if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
802 "lost+found inode not a directory"))
803 goto create_lostfound;
809 root_inode->bi_nlink++;
811 bch2_inode_pack(&packed, root_inode);
813 ret = bch2_btree_insert(c, BTREE_ID_INODES, &packed.inode.k_i,
816 BTREE_INSERT_LAZY_RW);
820 bch2_inode_init(c, lostfound_inode, 0, 0, S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO,
823 ret = bch2_inode_create(c, lostfound_inode, BLOCKDEV_INODE_MAX, 0,
824 &c->unused_inode_hint);
828 ret = bch2_dirent_create(c, BCACHEFS_ROOT_INO, &root_hash_info, DT_DIR,
829 &lostfound, lostfound_inode->bi_inum, NULL,
831 BTREE_INSERT_LAZY_RW);
838 struct inode_bitmap {
843 static inline bool inode_bitmap_test(struct inode_bitmap *b, size_t nr)
845 return nr < b->size ? test_bit(nr, b->bits) : false;
848 static inline int inode_bitmap_set(struct inode_bitmap *b, size_t nr)
851 size_t new_size = max_t(size_t, max_t(size_t,
857 new_size = roundup_pow_of_two(new_size);
858 n = krealloc(b->bits, new_size / 8, GFP_KERNEL|__GFP_ZERO);
867 __set_bit(nr, b->bits);
875 struct pathbuf_entry {
881 static int path_down(struct pathbuf *p, u64 inum)
883 if (p->nr == p->size) {
884 size_t new_size = max_t(size_t, 256UL, p->size * 2);
885 void *n = krealloc(p->entries,
886 new_size * sizeof(p->entries[0]),
895 p->entries[p->nr++] = (struct pathbuf_entry) {
903 static int check_directory_structure(struct bch_fs *c,
904 struct bch_inode_unpacked *lostfound_inode)
906 struct inode_bitmap dirs_done = { NULL, 0 };
907 struct pathbuf path = { 0, 0, NULL };
908 struct pathbuf_entry *e;
909 struct btree_trans trans;
910 struct btree_iter *iter;
912 struct bkey_s_c_dirent dirent;
913 bool had_unreachable;
917 bch2_trans_init(&trans, c);
918 bch2_trans_preload_iters(&trans);
920 bch_verbose(c, "checking directory structure");
924 had_unreachable = false;
926 ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
928 bch_err(c, "memory allocation failure in inode_bitmap_set()");
932 ret = path_down(&path, BCACHEFS_ROOT_INO);
938 e = &path.entries[path.nr - 1];
940 if (e->offset == U64_MAX)
943 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS,
944 POS(e->inum, e->offset + 1), 0, k) {
945 if (k.k->p.inode != e->inum)
948 e->offset = k.k->p.offset;
950 if (k.k->type != KEY_TYPE_dirent)
953 dirent = bkey_s_c_to_dirent(k);
955 if (dirent.v->d_type != DT_DIR)
958 d_inum = le64_to_cpu(dirent.v->d_inum);
960 if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
961 "directory %llu has multiple hardlinks",
963 ret = remove_dirent(&trans, dirent);
969 ret = inode_bitmap_set(&dirs_done, d_inum);
971 bch_err(c, "memory allocation failure in inode_bitmap_set()");
975 ret = path_down(&path, d_inum);
980 ret = bch2_trans_iter_free(&trans, iter);
982 bch_err(c, "btree error %i in fsck", ret);
987 ret = bch2_trans_iter_free(&trans, iter);
989 bch_err(c, "btree error %i in fsck", ret);
996 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES, POS_MIN, 0);
998 for_each_btree_key_continue(iter, 0, k) {
999 if (k.k->type != KEY_TYPE_inode)
1002 if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
1005 ret = bch2_empty_dir_trans(&trans, k.k->p.inode);
1011 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.inode), c,
1012 "unreachable directory found (inum %llu)",
1014 bch2_btree_trans_unlock(&trans);
1016 ret = reattach_inode(c, lostfound_inode, k.k->p.inode);
1021 had_unreachable = true;
1024 ret = bch2_trans_iter_free(&trans, iter);
1028 if (had_unreachable) {
1029 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
1030 kfree(dirs_done.bits);
1031 kfree(path.entries);
1032 memset(&dirs_done, 0, sizeof(dirs_done));
1033 memset(&path, 0, sizeof(path));
1038 ret = bch2_trans_exit(&trans) ?: ret;
1039 kfree(dirs_done.bits);
1040 kfree(path.entries);
1049 typedef GENRADIX(struct nlink) nlink_table;
1051 static void inc_link(struct bch_fs *c, nlink_table *links,
1052 u64 range_start, u64 *range_end,
1057 if (inum < range_start || inum >= *range_end)
1060 link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
1062 bch_verbose(c, "allocation failed during fs gc - will need another pass");
1074 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
1075 u64 range_start, u64 *range_end)
1077 struct btree_trans trans;
1078 struct btree_iter *iter;
1080 struct bkey_s_c_dirent d;
1084 bch2_trans_init(&trans, c);
1085 bch2_trans_preload_iters(&trans);
1087 inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
1089 for_each_btree_key(&trans, iter, BTREE_ID_DIRENTS, POS_MIN, 0, k) {
1090 switch (k.k->type) {
1091 case KEY_TYPE_dirent:
1092 d = bkey_s_c_to_dirent(k);
1093 d_inum = le64_to_cpu(d.v->d_inum);
1095 if (d.v->d_type == DT_DIR)
1096 inc_link(c, links, range_start, range_end,
1097 d.k->p.inode, true);
1099 inc_link(c, links, range_start, range_end,
1105 bch2_trans_cond_resched(&trans);
1107 ret = bch2_trans_exit(&trans);
1109 bch_err(c, "error in fs gc: btree error %i while walking dirents", ret);
1114 static int check_inode_nlink(struct bch_fs *c,
1115 struct bch_inode_unpacked *lostfound_inode,
1116 struct bch_inode_unpacked *u,
1120 u32 i_nlink = u->bi_flags & BCH_INODE_UNLINKED
1122 : u->bi_nlink + nlink_bias(u->bi_mode);
1124 link->count * nlink_bias(u->bi_mode) +
1129 * These should have been caught/fixed by earlier passes, we don't
1132 if (S_ISDIR(u->bi_mode) && link->count > 1) {
1133 need_fsck_err(c, "directory %llu with multiple hardlinks: %u",
1134 u->bi_inum, link->count);
1138 if (S_ISDIR(u->bi_mode) && !link->count) {
1139 need_fsck_err(c, "unreachable directory found (inum %llu)",
1144 if (!S_ISDIR(u->bi_mode) && link->dir_count) {
1145 need_fsck_err(c, "non directory with subdirectories",
1151 !(u->bi_flags & BCH_INODE_UNLINKED) &&
1152 (c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK))) {
1153 if (fsck_err(c, "unreachable inode %llu not marked as unlinked (type %u)",
1154 u->bi_inum, mode_to_type(u->bi_mode)) ==
1158 ret = reattach_inode(c, lostfound_inode, u->bi_inum);
1163 real_i_nlink = nlink_bias(u->bi_mode) + link->dir_count;
1167 if (i_nlink < link->count) {
1168 if (fsck_err(c, "inode %llu i_link too small (%u < %u, type %i)",
1169 u->bi_inum, i_nlink, link->count,
1170 mode_to_type(u->bi_mode)) == FSCK_ERR_IGNORE)
1175 if (i_nlink != real_i_nlink &&
1177 if (fsck_err(c, "filesystem marked clean, "
1178 "but inode %llu has wrong i_nlink "
1179 "(type %u i_nlink %u, should be %u)",
1180 u->bi_inum, mode_to_type(u->bi_mode),
1181 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1186 if (i_nlink != real_i_nlink &&
1187 (c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK))) {
1188 if (fsck_err(c, "inode %llu has wrong i_nlink "
1189 "(type %u i_nlink %u, should be %u)",
1190 u->bi_inum, mode_to_type(u->bi_mode),
1191 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1196 if (real_i_nlink && i_nlink != real_i_nlink)
1197 bch_verbose(c, "setting inode %llu nlink from %u to %u",
1198 u->bi_inum, i_nlink, real_i_nlink);
1200 if (i_nlink != real_i_nlink) {
1202 u->bi_nlink = real_i_nlink - nlink_bias(u->bi_mode);
1203 u->bi_flags &= ~BCH_INODE_UNLINKED;
1206 u->bi_flags |= BCH_INODE_UNLINKED;
1215 static int check_inode(struct btree_trans *trans,
1216 struct bch_inode_unpacked *lostfound_inode,
1217 struct btree_iter *iter,
1218 struct bkey_s_c_inode inode,
1221 struct bch_fs *c = trans->c;
1222 struct bch_inode_unpacked u;
1223 bool do_update = false;
1226 ret = bch2_inode_unpack(inode, &u);
1228 bch2_btree_trans_unlock(trans);
1230 if (bch2_fs_inconsistent_on(ret, c,
1231 "error unpacking inode %llu in fsck",
1236 ret = check_inode_nlink(c, lostfound_inode, &u, link,
1242 if (u.bi_flags & BCH_INODE_UNLINKED &&
1244 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
1246 bch_verbose(c, "deleting inode %llu", u.bi_inum);
1248 ret = bch2_inode_rm(c, u.bi_inum);
1250 bch_err(c, "error in fs gc: error %i "
1251 "while deleting inode", ret);
1255 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
1257 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
1259 bch_verbose(c, "truncating inode %llu", u.bi_inum);
1262 * XXX: need to truncate partial blocks too here - or ideally
1263 * just switch units to bytes and that issue goes away
1266 ret = bch2_inode_truncate(c, u.bi_inum, u.bi_size);
1268 bch_err(c, "error in fs gc: error %i "
1269 "truncating inode", ret);
1274 * We truncated without our normal sector accounting hook, just
1275 * make sure we recalculate it:
1277 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
1279 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
1283 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
1285 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
1289 bch_verbose(c, "recounting sectors for inode %llu",
1292 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
1294 bch_err(c, "error in fs gc: error %i "
1295 "recounting inode sectors",
1300 u.bi_sectors = sectors;
1301 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
1306 struct bkey_inode_buf p;
1308 bch2_inode_pack(&p, &u);
1309 bch2_trans_update(trans, BTREE_INSERT_ENTRY(iter, &p.inode.k_i));
1311 ret = bch2_trans_commit(trans, NULL, NULL,
1312 BTREE_INSERT_NOFAIL|
1313 BTREE_INSERT_LAZY_RW);
1314 if (ret && ret != -EINTR)
1315 bch_err(c, "error in fs gc: error %i "
1316 "updating inode", ret);
1323 static int bch2_gc_walk_inodes(struct bch_fs *c,
1324 struct bch_inode_unpacked *lostfound_inode,
1326 u64 range_start, u64 range_end)
1328 struct btree_trans trans;
1329 struct btree_iter *iter;
1331 struct nlink *link, zero_links = { 0, 0 };
1332 struct genradix_iter nlinks_iter;
1333 int ret = 0, ret2 = 0;
1336 bch2_trans_init(&trans, c);
1337 bch2_trans_preload_iters(&trans);
1339 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES,
1340 POS(range_start, 0), 0);
1341 nlinks_iter = genradix_iter_init(links, 0);
1343 while ((k = bch2_btree_iter_peek(iter)).k &&
1344 !(ret2 = bkey_err(k))) {
1345 peek_nlinks: link = genradix_iter_peek(&nlinks_iter, links);
1347 if (!link && (!k.k || iter->pos.inode >= range_end))
1350 nlinks_pos = range_start + nlinks_iter.pos;
1351 if (iter->pos.inode > nlinks_pos) {
1352 /* Should have been caught by dirents pass: */
1353 need_fsck_err_on(link && link->count, c,
1354 "missing inode %llu (nlink %u)",
1355 nlinks_pos, link->count);
1356 genradix_iter_advance(&nlinks_iter, links);
1360 if (iter->pos.inode < nlinks_pos || !link)
1363 if (k.k && k.k->type == KEY_TYPE_inode) {
1364 ret = check_inode(&trans, lostfound_inode, iter,
1365 bkey_s_c_to_inode(k), link);
1366 BUG_ON(ret == -EINTR);
1370 /* Should have been caught by dirents pass: */
1371 need_fsck_err_on(link->count, c,
1372 "missing inode %llu (nlink %u)",
1373 nlinks_pos, link->count);
1376 if (nlinks_pos == iter->pos.inode)
1377 genradix_iter_advance(&nlinks_iter, links);
1379 bch2_btree_iter_next(iter);
1380 bch2_trans_cond_resched(&trans);
1383 bch2_trans_exit(&trans);
1386 bch_err(c, "error in fs gc: btree error %i while walking inodes", ret2);
1392 static int check_inode_nlinks(struct bch_fs *c,
1393 struct bch_inode_unpacked *lostfound_inode)
1396 u64 this_iter_range_start, next_iter_range_start = 0;
1399 bch_verbose(c, "checking inode nlinks");
1401 genradix_init(&links);
1404 this_iter_range_start = next_iter_range_start;
1405 next_iter_range_start = U64_MAX;
1407 ret = bch2_gc_walk_dirents(c, &links,
1408 this_iter_range_start,
1409 &next_iter_range_start);
1413 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1414 this_iter_range_start,
1415 next_iter_range_start);
1419 genradix_free(&links);
1420 } while (next_iter_range_start != U64_MAX);
1422 genradix_free(&links);
1428 static int check_inodes_fast(struct bch_fs *c)
1430 struct btree_trans trans;
1431 struct btree_iter *iter;
1433 struct bkey_s_c_inode inode;
1436 bch2_trans_init(&trans, c);
1437 bch2_trans_preload_iters(&trans);
1439 iter = bch2_trans_get_iter(&trans, BTREE_ID_INODES,
1442 for_each_btree_key_continue(iter, 0, k) {
1443 if (k.k->type != KEY_TYPE_inode)
1446 inode = bkey_s_c_to_inode(k);
1448 if (inode.v->bi_flags &
1449 (BCH_INODE_I_SIZE_DIRTY|
1450 BCH_INODE_I_SECTORS_DIRTY|
1451 BCH_INODE_UNLINKED)) {
1452 ret = check_inode(&trans, NULL, iter, inode, NULL);
1453 BUG_ON(ret == -EINTR);
1459 ret2 = bch2_trans_exit(&trans);
1465 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1466 * Doesn't fix them yet, mainly because they haven't yet been observed:
1468 static int bch2_fsck_full(struct bch_fs *c)
1470 struct bch_inode_unpacked root_inode, lostfound_inode;
1473 bch_verbose(c, "starting fsck:");
1474 ret = check_extents(c) ?:
1477 check_root(c, &root_inode) ?:
1478 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1479 check_directory_structure(c, &lostfound_inode) ?:
1480 check_inode_nlinks(c, &lostfound_inode);
1482 bch2_flush_fsck_errs(c);
1483 bch_verbose(c, "fsck done");
1488 static int bch2_fsck_inode_nlink(struct bch_fs *c)
1490 struct bch_inode_unpacked root_inode, lostfound_inode;
1493 bch_verbose(c, "checking inode link counts:");
1494 ret = check_root(c, &root_inode) ?:
1495 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1496 check_inode_nlinks(c, &lostfound_inode);
1498 bch2_flush_fsck_errs(c);
1499 bch_verbose(c, "done");
1504 static int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1508 bch_verbose(c, "walking inodes:");
1509 ret = check_inodes_fast(c);
1511 bch2_flush_fsck_errs(c);
1512 bch_verbose(c, "done");
1517 int bch2_fsck(struct bch_fs *c)
1520 return bch2_fsck_full(c);
1525 return c->sb.features & (1 << BCH_FEATURE_ATOMIC_NLINK)
1526 ? bch2_fsck_walk_inodes_only(c)
1527 : bch2_fsck_inode_nlink(c);