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 fix_overlapping_extent(struct btree_trans *trans,
417 struct bkey_s_c k, struct bpos cut_at)
419 struct btree_iter *iter;
423 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
424 ret = PTR_ERR_OR_ZERO(u);
428 bkey_reassemble(u, k);
429 bch2_cut_front(cut_at, u);
433 * We don't want to go through the extent_handle_overwrites path:
435 * XXX: this is going to screw up disk accounting, extent triggers
436 * assume things about extent overwrites - we should be running the
437 * triggers manually here
439 iter = bch2_trans_get_iter(trans, BTREE_ID_extents, u->k.p,
440 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
442 BUG_ON(iter->flags & BTREE_ITER_IS_EXTENTS);
443 bch2_trans_update(trans, iter, u, BTREE_TRIGGER_NORUN);
444 bch2_trans_iter_put(trans, iter);
446 return bch2_trans_commit(trans, NULL, NULL,
448 BTREE_INSERT_LAZY_RW);
452 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
453 * that i_size an i_sectors are consistent
456 static int check_extents(struct bch_fs *c)
458 struct inode_walker w = inode_walker_init();
459 struct btree_trans trans;
460 struct btree_iter *iter;
462 struct bkey_buf prev;
466 bch2_bkey_buf_init(&prev);
467 prev.k->k = KEY(0, 0, 0);
468 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
470 bch_verbose(c, "checking extents");
472 iter = bch2_trans_get_iter(&trans, BTREE_ID_extents,
473 POS(BCACHEFS_ROOT_INO, 0),
476 while ((k = bch2_btree_iter_peek(iter)).k &&
477 !(ret = bkey_err(k))) {
479 w.cur_inum != k.k->p.inode &&
480 !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
481 fsck_err_on(w.inode.bi_sectors != i_sectors, c,
482 "inode %llu has incorrect i_sectors: got %llu, should be %llu",
484 w.inode.bi_sectors, i_sectors)) {
485 struct btree_iter *inode_iter =
486 bch2_trans_get_iter(&trans, BTREE_ID_inodes,
490 w.inode.bi_sectors = i_sectors;
492 ret = __bch2_trans_do(&trans, NULL, NULL,
494 BTREE_INSERT_LAZY_RW,
495 bch2_inode_write(&trans, inode_iter, &w.inode));
496 bch2_trans_iter_put(&trans, inode_iter);
501 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
505 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
506 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
508 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2))
509 return fix_overlapping_extent(&trans, k, prev.k->k.p) ?: -EINTR;
512 ret = walk_inode(&trans, &w, k.k->p.inode);
516 if (w.first_this_inode)
519 if (fsck_err_on(!w.have_inode, c,
520 "extent type %u for missing inode %llu",
521 k.k->type, k.k->p.inode) ||
522 fsck_err_on(w.have_inode &&
523 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
524 "extent type %u for non regular file, inode %llu mode %o",
525 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
527 return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
528 POS(k.k->p.inode, 0),
529 POS(k.k->p.inode, U64_MAX),
533 if (fsck_err_on(w.have_inode &&
534 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
535 k.k->type != KEY_TYPE_reservation &&
536 k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
537 "extent type %u offset %llu past end of inode %llu, i_size %llu",
538 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
540 return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
541 POS(k.k->p.inode, round_up(w.inode.bi_size, block_bytes(c))),
542 POS(k.k->p.inode, U64_MAX),
546 if (bkey_extent_is_allocation(k.k))
547 i_sectors += k.k->size;
548 bch2_bkey_buf_reassemble(&prev, c, k);
550 bch2_btree_iter_advance(iter);
555 bch2_trans_iter_put(&trans, iter);
556 bch2_bkey_buf_exit(&prev, c);
557 return bch2_trans_exit(&trans) ?: ret;
561 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
565 static int check_dirents(struct bch_fs *c)
567 struct inode_walker w = inode_walker_init();
569 struct btree_trans trans;
570 struct btree_iter *iter;
576 bch_verbose(c, "checking dirents");
578 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
582 iter = bch2_trans_get_iter(&trans, BTREE_ID_dirents,
583 POS(BCACHEFS_ROOT_INO, 0), 0);
585 while ((k = bch2_btree_iter_peek(iter)).k &&
586 !(ret = bkey_err(k))) {
587 struct bkey_s_c_dirent d;
588 struct bch_inode_unpacked target;
592 ret = walk_inode(&trans, &w, k.k->p.inode);
596 if (fsck_err_on(!w.have_inode, c,
597 "dirent in nonexisting directory:\n%s",
598 (bch2_bkey_val_to_text(&PBUF(buf), c,
600 fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
601 "dirent in non directory inode type %u:\n%s",
602 mode_to_type(w.inode.bi_mode),
603 (bch2_bkey_val_to_text(&PBUF(buf), c,
605 ret = bch2_btree_delete_at(&trans, iter, 0);
611 if (w.first_this_inode && w.have_inode)
612 hash_check_set_inode(&trans, &h, &w.inode);
614 ret = check_dirent_hash(&trans, &h, iter, &k);
625 if (k.k->type != KEY_TYPE_dirent)
628 d = bkey_s_c_to_dirent(k);
629 d_inum = le64_to_cpu(d.v->d_inum);
631 name_len = bch2_dirent_name_bytes(d);
633 if (fsck_err_on(!name_len, c, "empty dirent") ||
634 fsck_err_on(name_len == 1 &&
635 !memcmp(d.v->d_name, ".", 1), c,
637 fsck_err_on(name_len == 2 &&
638 !memcmp(d.v->d_name, "..", 2), c,
640 fsck_err_on(name_len == 2 &&
641 !memcmp(d.v->d_name, "..", 2), c,
643 fsck_err_on(memchr(d.v->d_name, '/', name_len), c,
644 "dirent name has invalid chars")) {
645 ret = remove_dirent(&trans, d);
651 if (fsck_err_on(d_inum == d.k->p.inode, c,
652 "dirent points to own directory:\n%s",
653 (bch2_bkey_val_to_text(&PBUF(buf), c,
655 ret = remove_dirent(&trans, d);
661 ret = __bch2_inode_find_by_inum_trans(&trans, d_inum, &target, 0);
662 if (ret && ret != -ENOENT)
668 if (fsck_err_on(!have_target, c,
669 "dirent points to missing inode:\n%s",
670 (bch2_bkey_val_to_text(&PBUF(buf), c,
672 ret = remove_dirent(&trans, d);
678 if (fsck_err_on(have_target &&
680 mode_to_type(target.bi_mode), c,
681 "incorrect d_type: should be %u:\n%s",
682 mode_to_type(target.bi_mode),
683 (bch2_bkey_val_to_text(&PBUF(buf), c,
685 struct bkey_i_dirent *n;
687 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
693 bkey_reassemble(&n->k_i, d.s_c);
694 n->v.d_type = mode_to_type(target.bi_mode);
696 ret = __bch2_trans_do(&trans, NULL, NULL,
698 BTREE_INSERT_LAZY_RW,
699 (bch2_trans_update(&trans, iter, &n->k_i, 0), 0));
706 bch2_btree_iter_advance(iter);
709 hash_stop_chain(&trans, &h);
715 bch2_trans_iter_put(&trans, h.chain);
716 bch2_trans_iter_put(&trans, iter);
717 return bch2_trans_exit(&trans) ?: ret;
721 * Walk xattrs: verify that they all have a corresponding inode
724 static int check_xattrs(struct bch_fs *c)
726 struct inode_walker w = inode_walker_init();
728 struct btree_trans trans;
729 struct btree_iter *iter;
733 bch_verbose(c, "checking xattrs");
737 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
739 iter = bch2_trans_get_iter(&trans, BTREE_ID_xattrs,
740 POS(BCACHEFS_ROOT_INO, 0), 0);
742 while ((k = bch2_btree_iter_peek(iter)).k &&
743 !(ret = bkey_err(k))) {
744 ret = walk_inode(&trans, &w, k.k->p.inode);
748 if (fsck_err_on(!w.have_inode, c,
749 "xattr for missing inode %llu",
751 ret = bch2_btree_delete_at(&trans, iter, 0);
757 if (w.first_this_inode && w.have_inode)
758 hash_check_set_inode(&trans, &h, &w.inode);
760 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
765 bch2_btree_iter_advance(iter);
771 bch2_trans_iter_put(&trans, h.chain);
772 bch2_trans_iter_put(&trans, iter);
773 return bch2_trans_exit(&trans) ?: ret;
776 /* Get root directory, create if it doesn't exist: */
777 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
779 struct bkey_inode_buf packed;
782 bch_verbose(c, "checking root directory");
784 ret = bch2_trans_do(c, NULL, NULL, 0,
785 __bch2_inode_find_by_inum_trans(&trans, BCACHEFS_ROOT_INO,
787 if (ret && ret != -ENOENT)
790 if (fsck_err_on(ret, c, "root directory missing"))
793 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
794 "root inode not a directory"))
801 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
803 root_inode->bi_inum = BCACHEFS_ROOT_INO;
805 bch2_inode_pack(c, &packed, root_inode);
807 return bch2_btree_insert(c, BTREE_ID_inodes, &packed.inode.k_i,
810 BTREE_INSERT_LAZY_RW);
813 /* Get lost+found, create if it doesn't exist: */
814 static int check_lostfound(struct bch_fs *c,
815 struct bch_inode_unpacked *root_inode,
816 struct bch_inode_unpacked *lostfound_inode)
818 struct qstr lostfound = QSTR("lost+found");
819 struct bch_hash_info root_hash_info =
820 bch2_hash_info_init(c, root_inode);
824 bch_verbose(c, "checking lost+found");
826 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
829 bch_notice(c, "creating lost+found");
830 goto create_lostfound;
833 ret = bch2_trans_do(c, NULL, NULL, 0,
834 __bch2_inode_find_by_inum_trans(&trans, inum, lostfound_inode, 0));
835 if (ret && ret != -ENOENT)
838 if (fsck_err_on(ret, c, "lost+found missing"))
839 goto create_lostfound;
841 if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
842 "lost+found inode not a directory"))
843 goto create_lostfound;
849 bch2_inode_init_early(c, lostfound_inode);
851 ret = bch2_trans_do(c, NULL, NULL,
853 BTREE_INSERT_LAZY_RW,
854 bch2_create_trans(&trans,
855 BCACHEFS_ROOT_INO, root_inode,
856 lostfound_inode, &lostfound,
857 0, 0, S_IFDIR|0700, 0, NULL, NULL));
859 bch_err(c, "error creating lost+found: %i", ret);
864 typedef GENRADIX(unsigned long) inode_bitmap;
866 static inline bool inode_bitmap_test(inode_bitmap *b, size_t nr)
868 unsigned long *w = genradix_ptr(b, nr / BITS_PER_LONG);
869 return w ? test_bit(nr & (BITS_PER_LONG - 1), w) : false;
872 static inline int inode_bitmap_set(inode_bitmap *b, size_t nr)
874 unsigned long *w = genradix_ptr_alloc(b, nr / BITS_PER_LONG, GFP_KERNEL);
879 *w |= 1UL << (nr & (BITS_PER_LONG - 1));
887 struct pathbuf_entry {
893 static int path_down(struct pathbuf *p, u64 inum)
895 if (p->nr == p->size) {
896 size_t new_size = max_t(size_t, 256UL, p->size * 2);
897 void *n = krealloc(p->entries,
898 new_size * sizeof(p->entries[0]),
907 p->entries[p->nr++] = (struct pathbuf_entry) {
915 static int check_directory_structure(struct bch_fs *c,
916 struct bch_inode_unpacked *lostfound_inode)
918 inode_bitmap dirs_done;
919 struct pathbuf path = { 0, 0, NULL };
920 struct pathbuf_entry *e;
921 struct btree_trans trans;
922 struct btree_iter *iter;
924 struct bkey_s_c_dirent dirent;
925 bool had_unreachable;
929 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
931 bch_verbose(c, "checking directory structure");
935 genradix_init(&dirs_done);
936 had_unreachable = false;
938 ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
940 bch_err(c, "memory allocation failure in inode_bitmap_set()");
944 ret = path_down(&path, BCACHEFS_ROOT_INO);
950 e = &path.entries[path.nr - 1];
952 if (e->offset == U64_MAX)
955 for_each_btree_key(&trans, iter, BTREE_ID_dirents,
956 POS(e->inum, e->offset + 1), 0, k, ret) {
957 if (k.k->p.inode != e->inum)
960 e->offset = k.k->p.offset;
962 if (k.k->type != KEY_TYPE_dirent)
965 dirent = bkey_s_c_to_dirent(k);
967 if (dirent.v->d_type != DT_DIR)
970 d_inum = le64_to_cpu(dirent.v->d_inum);
972 if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
973 "directory %llu has multiple hardlinks",
975 ret = remove_dirent(&trans, dirent);
981 ret = inode_bitmap_set(&dirs_done, d_inum);
983 bch_err(c, "memory allocation failure in inode_bitmap_set()");
987 ret = path_down(&path, d_inum);
992 ret = bch2_trans_iter_free(&trans, iter);
994 bch_err(c, "btree error %i in fsck", ret);
999 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
1001 bch_err(c, "btree error %i in fsck", ret);
1008 iter = bch2_trans_get_iter(&trans, BTREE_ID_inodes, POS_MIN, 0);
1010 for_each_btree_key_continue(iter, 0, k, ret) {
1011 if (k.k->type != KEY_TYPE_inode)
1014 if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
1017 ret = bch2_empty_dir_trans(&trans, k.k->p.inode);
1023 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.offset), c,
1024 "unreachable directory found (inum %llu)",
1026 bch2_trans_unlock(&trans);
1028 ret = reattach_inode(c, lostfound_inode, k.k->p.offset);
1033 had_unreachable = true;
1036 bch2_trans_iter_free(&trans, iter);
1040 if (had_unreachable) {
1041 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
1042 genradix_free(&dirs_done);
1043 kfree(path.entries);
1044 memset(&dirs_done, 0, sizeof(dirs_done));
1045 memset(&path, 0, sizeof(path));
1050 ret = bch2_trans_exit(&trans) ?: ret;
1051 genradix_free(&dirs_done);
1052 kfree(path.entries);
1061 typedef GENRADIX(struct nlink) nlink_table;
1063 static void inc_link(struct bch_fs *c, nlink_table *links,
1064 u64 range_start, u64 *range_end,
1069 if (inum < range_start || inum >= *range_end)
1072 if (inum - range_start >= SIZE_MAX / sizeof(struct nlink)) {
1077 link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
1079 bch_verbose(c, "allocation failed during fsck - will need another pass");
1091 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
1092 u64 range_start, u64 *range_end)
1094 struct btree_trans trans;
1095 struct btree_iter *iter;
1097 struct bkey_s_c_dirent d;
1101 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1103 inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
1105 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN, 0, k, ret) {
1106 switch (k.k->type) {
1107 case KEY_TYPE_dirent:
1108 d = bkey_s_c_to_dirent(k);
1109 d_inum = le64_to_cpu(d.v->d_inum);
1111 if (d.v->d_type == DT_DIR)
1112 inc_link(c, links, range_start, range_end,
1113 d.k->p.inode, true);
1115 inc_link(c, links, range_start, range_end,
1121 bch2_trans_cond_resched(&trans);
1123 bch2_trans_iter_put(&trans, iter);
1125 ret = bch2_trans_exit(&trans) ?: ret;
1127 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
1132 static int check_inode_nlink(struct bch_fs *c,
1133 struct bch_inode_unpacked *lostfound_inode,
1134 struct bch_inode_unpacked *u,
1138 u32 i_nlink = bch2_inode_nlink_get(u);
1140 link->count * nlink_bias(u->bi_mode) +
1145 * These should have been caught/fixed by earlier passes, we don't
1148 if (S_ISDIR(u->bi_mode) && link->count > 1) {
1149 need_fsck_err(c, "directory %llu with multiple hardlinks: %u",
1150 u->bi_inum, link->count);
1154 if (S_ISDIR(u->bi_mode) && !link->count) {
1155 need_fsck_err(c, "unreachable directory found (inum %llu)",
1160 if (!S_ISDIR(u->bi_mode) && link->dir_count) {
1161 need_fsck_err(c, "non directory with subdirectories (inum %llu)",
1167 !(u->bi_flags & BCH_INODE_UNLINKED) &&
1168 (c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
1169 if (fsck_err(c, "unreachable inode %llu not marked as unlinked (type %u)",
1170 u->bi_inum, mode_to_type(u->bi_mode)) ==
1174 ret = reattach_inode(c, lostfound_inode, u->bi_inum);
1179 real_i_nlink = nlink_bias(u->bi_mode) + link->dir_count;
1183 if (i_nlink < link->count) {
1184 if (fsck_err(c, "inode %llu i_link too small (%u < %u, type %i)",
1185 u->bi_inum, i_nlink, link->count,
1186 mode_to_type(u->bi_mode)) == FSCK_ERR_IGNORE)
1191 if (i_nlink != real_i_nlink &&
1193 if (fsck_err(c, "filesystem marked clean, "
1194 "but inode %llu has wrong i_nlink "
1195 "(type %u i_nlink %u, should be %u)",
1196 u->bi_inum, mode_to_type(u->bi_mode),
1197 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1202 if (i_nlink != real_i_nlink &&
1203 (c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
1204 if (fsck_err(c, "inode %llu has wrong i_nlink "
1205 "(type %u i_nlink %u, should be %u)",
1206 u->bi_inum, mode_to_type(u->bi_mode),
1207 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1212 if (real_i_nlink && i_nlink != real_i_nlink)
1213 bch_verbose(c, "setting inode %llu nlink from %u to %u",
1214 u->bi_inum, i_nlink, real_i_nlink);
1216 if (i_nlink != real_i_nlink) {
1217 bch2_inode_nlink_set(u, real_i_nlink);
1224 static int check_inode(struct btree_trans *trans,
1225 struct bch_inode_unpacked *lostfound_inode,
1226 struct btree_iter *iter,
1227 struct bkey_s_c_inode inode,
1230 struct bch_fs *c = trans->c;
1231 struct bch_inode_unpacked u;
1232 bool do_update = false;
1235 ret = bch2_inode_unpack(inode, &u);
1237 bch2_trans_unlock(trans);
1239 if (bch2_fs_inconsistent_on(ret, c,
1240 "error unpacking inode %llu in fsck",
1245 ret = check_inode_nlink(c, lostfound_inode, &u, link,
1251 if (u.bi_flags & BCH_INODE_UNLINKED &&
1253 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
1255 bch_verbose(c, "deleting inode %llu", u.bi_inum);
1259 ret = bch2_inode_rm(c, u.bi_inum, false);
1261 bch_err(c, "error in fsck: error %i while deleting inode", ret);
1265 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
1267 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
1269 bch_verbose(c, "truncating inode %llu", u.bi_inum);
1274 * XXX: need to truncate partial blocks too here - or ideally
1275 * just switch units to bytes and that issue goes away
1277 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1278 POS(u.bi_inum, round_up(u.bi_size, block_bytes(c))),
1279 POS(u.bi_inum, U64_MAX),
1282 bch_err(c, "error in fsck: error %i truncating inode", ret);
1287 * We truncated without our normal sector accounting hook, just
1288 * make sure we recalculate it:
1290 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
1292 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
1296 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
1298 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
1302 bch_verbose(c, "recounting sectors for inode %llu",
1305 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
1307 bch_err(c, "error in fsck: error %i recounting inode sectors",
1312 u.bi_sectors = sectors;
1313 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
1318 struct bkey_inode_buf p;
1320 bch2_inode_pack(c, &p, &u);
1322 ret = __bch2_trans_do(trans, NULL, NULL,
1323 BTREE_INSERT_NOFAIL|
1324 BTREE_INSERT_LAZY_RW,
1325 (bch2_trans_update(trans, iter, &p.inode.k_i, 0), 0));
1327 bch_err(c, "error in fsck: error %i "
1328 "updating inode", ret);
1335 static int bch2_gc_walk_inodes(struct bch_fs *c,
1336 struct bch_inode_unpacked *lostfound_inode,
1338 u64 range_start, u64 range_end)
1340 struct btree_trans trans;
1341 struct btree_iter *iter;
1343 struct nlink *link, zero_links = { 0, 0 };
1344 struct genradix_iter nlinks_iter;
1345 int ret = 0, ret2 = 0;
1348 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1350 iter = bch2_trans_get_iter(&trans, BTREE_ID_inodes,
1351 POS(0, range_start), 0);
1352 nlinks_iter = genradix_iter_init(links, 0);
1354 while ((k = bch2_btree_iter_peek(iter)).k &&
1355 !(ret2 = bkey_err(k)) &&
1356 iter->pos.offset < range_end) {
1357 peek_nlinks: link = genradix_iter_peek(&nlinks_iter, links);
1359 if (!link && (!k.k || iter->pos.offset >= range_end))
1362 nlinks_pos = range_start + nlinks_iter.pos;
1364 if (link && nlinks_pos < iter->pos.offset) {
1365 /* Should have been caught by dirents pass: */
1366 need_fsck_err_on(link->count, c,
1367 "missing inode %llu (nlink %u)",
1368 nlinks_pos, link->count);
1369 genradix_iter_advance(&nlinks_iter, links);
1373 if (!link || nlinks_pos > iter->pos.offset)
1376 if (k.k && k.k->type == KEY_TYPE_inode) {
1377 ret = check_inode(&trans, lostfound_inode, iter,
1378 bkey_s_c_to_inode(k), link);
1379 BUG_ON(ret == -EINTR);
1383 /* Should have been caught by dirents pass: */
1384 need_fsck_err_on(link->count, c,
1385 "missing inode %llu (nlink %u)",
1386 nlinks_pos, link->count);
1389 if (nlinks_pos == iter->pos.offset)
1390 genradix_iter_advance(&nlinks_iter, links);
1392 bch2_btree_iter_advance(iter);
1393 bch2_trans_cond_resched(&trans);
1396 bch2_trans_iter_put(&trans, iter);
1397 bch2_trans_exit(&trans);
1400 bch_err(c, "error in fsck: btree error %i while walking inodes", ret2);
1406 static int check_inode_nlinks(struct bch_fs *c,
1407 struct bch_inode_unpacked *lostfound_inode)
1410 u64 this_iter_range_start, next_iter_range_start = 0;
1413 bch_verbose(c, "checking inode nlinks");
1415 genradix_init(&links);
1418 this_iter_range_start = next_iter_range_start;
1419 next_iter_range_start = U64_MAX;
1421 ret = bch2_gc_walk_dirents(c, &links,
1422 this_iter_range_start,
1423 &next_iter_range_start);
1427 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1428 this_iter_range_start,
1429 next_iter_range_start);
1433 genradix_free(&links);
1434 } while (next_iter_range_start != U64_MAX);
1436 genradix_free(&links);
1442 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1443 * Doesn't fix them yet, mainly because they haven't yet been observed:
1445 int bch2_fsck_full(struct bch_fs *c)
1447 struct bch_inode_unpacked root_inode, lostfound_inode;
1449 return check_extents(c) ?:
1452 check_root(c, &root_inode) ?:
1453 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1454 check_directory_structure(c, &lostfound_inode) ?:
1455 check_inode_nlinks(c, &lostfound_inode);
1458 int bch2_fsck_inode_nlink(struct bch_fs *c)
1460 struct bch_inode_unpacked root_inode, lostfound_inode;
1462 return check_root(c, &root_inode) ?:
1463 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1464 check_inode_nlinks(c, &lostfound_inode);
1467 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1469 struct btree_trans trans;
1470 struct btree_iter *iter;
1472 struct bkey_s_c_inode inode;
1475 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1477 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN, 0, k, ret) {
1478 if (k.k->type != KEY_TYPE_inode)
1481 inode = bkey_s_c_to_inode(k);
1483 if (inode.v->bi_flags &
1484 (BCH_INODE_I_SIZE_DIRTY|
1485 BCH_INODE_I_SECTORS_DIRTY|
1486 BCH_INODE_UNLINKED)) {
1487 ret = check_inode(&trans, NULL, iter, inode, NULL);
1492 bch2_trans_iter_put(&trans, iter);
1494 BUG_ON(ret == -EINTR);
1496 return bch2_trans_exit(&trans) ?: ret;