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 (!target.bi_nlink &&
679 !(target.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) &&
680 (target.bi_dir != k.k->p.inode ||
681 target.bi_dir_offset != k.k->p.offset) &&
682 (fsck_err_on(c->sb.version >= bcachefs_metadata_version_inode_backpointers, c,
683 "inode %llu has wrong backpointer:\n"
685 "should be %llu:%llu",
688 target.bi_dir_offset,
691 c->opts.version_upgrade)) {
692 struct bkey_inode_buf p;
694 target.bi_dir = k.k->p.inode;
695 target.bi_dir_offset = k.k->p.offset;
696 bch2_trans_unlock(&trans);
698 bch2_inode_pack(c, &p, &target);
700 ret = bch2_btree_insert(c, BTREE_ID_inodes,
701 &p.inode.k_i, NULL, NULL,
703 BTREE_INSERT_LAZY_RW);
705 bch_err(c, "error in fsck: error %i updating inode", ret);
711 if (fsck_err_on(have_target &&
713 mode_to_type(target.bi_mode), c,
714 "incorrect d_type: should be %u:\n%s",
715 mode_to_type(target.bi_mode),
716 (bch2_bkey_val_to_text(&PBUF(buf), c,
718 struct bkey_i_dirent *n;
720 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
726 bkey_reassemble(&n->k_i, d.s_c);
727 n->v.d_type = mode_to_type(target.bi_mode);
729 ret = __bch2_trans_do(&trans, NULL, NULL,
731 BTREE_INSERT_LAZY_RW,
732 (bch2_trans_update(&trans, iter, &n->k_i, 0), 0));
739 bch2_btree_iter_advance(iter);
742 hash_stop_chain(&trans, &h);
748 bch2_trans_iter_put(&trans, h.chain);
749 bch2_trans_iter_put(&trans, iter);
750 return bch2_trans_exit(&trans) ?: ret;
754 * Walk xattrs: verify that they all have a corresponding inode
757 static int check_xattrs(struct bch_fs *c)
759 struct inode_walker w = inode_walker_init();
761 struct btree_trans trans;
762 struct btree_iter *iter;
766 bch_verbose(c, "checking xattrs");
770 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
772 iter = bch2_trans_get_iter(&trans, BTREE_ID_xattrs,
773 POS(BCACHEFS_ROOT_INO, 0), 0);
775 while ((k = bch2_btree_iter_peek(iter)).k &&
776 !(ret = bkey_err(k))) {
777 ret = walk_inode(&trans, &w, k.k->p.inode);
781 if (fsck_err_on(!w.have_inode, c,
782 "xattr for missing inode %llu",
784 ret = bch2_btree_delete_at(&trans, iter, 0);
790 if (w.first_this_inode && w.have_inode)
791 hash_check_set_inode(&trans, &h, &w.inode);
793 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
798 bch2_btree_iter_advance(iter);
804 bch2_trans_iter_put(&trans, h.chain);
805 bch2_trans_iter_put(&trans, iter);
806 return bch2_trans_exit(&trans) ?: ret;
809 /* Get root directory, create if it doesn't exist: */
810 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
812 struct bkey_inode_buf packed;
815 bch_verbose(c, "checking root directory");
817 ret = bch2_trans_do(c, NULL, NULL, 0,
818 __bch2_inode_find_by_inum_trans(&trans, BCACHEFS_ROOT_INO,
820 if (ret && ret != -ENOENT)
823 if (fsck_err_on(ret, c, "root directory missing"))
826 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
827 "root inode not a directory"))
834 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
836 root_inode->bi_inum = BCACHEFS_ROOT_INO;
838 bch2_inode_pack(c, &packed, root_inode);
840 return bch2_btree_insert(c, BTREE_ID_inodes, &packed.inode.k_i,
843 BTREE_INSERT_LAZY_RW);
846 /* Get lost+found, create if it doesn't exist: */
847 static int check_lostfound(struct bch_fs *c,
848 struct bch_inode_unpacked *root_inode,
849 struct bch_inode_unpacked *lostfound_inode)
851 struct qstr lostfound = QSTR("lost+found");
852 struct bch_hash_info root_hash_info =
853 bch2_hash_info_init(c, root_inode);
857 bch_verbose(c, "checking lost+found");
859 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
862 bch_notice(c, "creating lost+found");
863 goto create_lostfound;
866 ret = bch2_trans_do(c, NULL, NULL, 0,
867 __bch2_inode_find_by_inum_trans(&trans, inum, lostfound_inode, 0));
868 if (ret && ret != -ENOENT)
871 if (fsck_err_on(ret, c, "lost+found missing"))
872 goto create_lostfound;
874 if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
875 "lost+found inode not a directory"))
876 goto create_lostfound;
882 bch2_inode_init_early(c, lostfound_inode);
884 ret = bch2_trans_do(c, NULL, NULL,
886 BTREE_INSERT_LAZY_RW,
887 bch2_create_trans(&trans,
888 BCACHEFS_ROOT_INO, root_inode,
889 lostfound_inode, &lostfound,
890 0, 0, S_IFDIR|0700, 0, NULL, NULL));
892 bch_err(c, "error creating lost+found: %i", ret);
897 typedef GENRADIX(unsigned long) inode_bitmap;
899 static inline bool inode_bitmap_test(inode_bitmap *b, size_t nr)
901 unsigned long *w = genradix_ptr(b, nr / BITS_PER_LONG);
902 return w ? test_bit(nr & (BITS_PER_LONG - 1), w) : false;
905 static inline int inode_bitmap_set(inode_bitmap *b, size_t nr)
907 unsigned long *w = genradix_ptr_alloc(b, nr / BITS_PER_LONG, GFP_KERNEL);
912 *w |= 1UL << (nr & (BITS_PER_LONG - 1));
920 struct pathbuf_entry {
926 static int path_down(struct pathbuf *p, u64 inum)
928 if (p->nr == p->size) {
929 size_t new_size = max_t(size_t, 256UL, p->size * 2);
930 void *n = krealloc(p->entries,
931 new_size * sizeof(p->entries[0]),
940 p->entries[p->nr++] = (struct pathbuf_entry) {
948 static int check_directory_structure(struct bch_fs *c,
949 struct bch_inode_unpacked *lostfound_inode)
951 inode_bitmap dirs_done;
952 struct pathbuf path = { 0, 0, NULL };
953 struct pathbuf_entry *e;
954 struct btree_trans trans;
955 struct btree_iter *iter;
957 struct bkey_s_c_dirent dirent;
958 bool had_unreachable;
962 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
964 bch_verbose(c, "checking directory structure");
968 genradix_init(&dirs_done);
969 had_unreachable = false;
971 ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
973 bch_err(c, "memory allocation failure in inode_bitmap_set()");
977 ret = path_down(&path, BCACHEFS_ROOT_INO);
983 e = &path.entries[path.nr - 1];
985 if (e->offset == U64_MAX)
988 for_each_btree_key(&trans, iter, BTREE_ID_dirents,
989 POS(e->inum, e->offset + 1), 0, k, ret) {
990 if (k.k->p.inode != e->inum)
993 e->offset = k.k->p.offset;
995 if (k.k->type != KEY_TYPE_dirent)
998 dirent = bkey_s_c_to_dirent(k);
1000 if (dirent.v->d_type != DT_DIR)
1003 d_inum = le64_to_cpu(dirent.v->d_inum);
1005 if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
1006 "directory %llu has multiple hardlinks",
1008 ret = remove_dirent(&trans, dirent);
1014 ret = inode_bitmap_set(&dirs_done, d_inum);
1016 bch_err(c, "memory allocation failure in inode_bitmap_set()");
1020 ret = path_down(&path, d_inum);
1025 ret = bch2_trans_iter_free(&trans, iter);
1027 bch_err(c, "btree error %i in fsck", ret);
1032 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
1034 bch_err(c, "btree error %i in fsck", ret);
1041 iter = bch2_trans_get_iter(&trans, BTREE_ID_inodes, POS_MIN, 0);
1043 for_each_btree_key_continue(iter, 0, k, ret) {
1044 if (k.k->type != KEY_TYPE_inode)
1047 if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
1050 ret = bch2_empty_dir_trans(&trans, k.k->p.inode);
1056 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.offset), c,
1057 "unreachable directory found (inum %llu)",
1059 bch2_trans_unlock(&trans);
1061 ret = reattach_inode(c, lostfound_inode, k.k->p.offset);
1066 had_unreachable = true;
1069 bch2_trans_iter_free(&trans, iter);
1073 if (had_unreachable) {
1074 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
1075 genradix_free(&dirs_done);
1076 kfree(path.entries);
1077 memset(&dirs_done, 0, sizeof(dirs_done));
1078 memset(&path, 0, sizeof(path));
1083 ret = bch2_trans_exit(&trans) ?: ret;
1084 genradix_free(&dirs_done);
1085 kfree(path.entries);
1094 typedef GENRADIX(struct nlink) nlink_table;
1096 static void inc_link(struct bch_fs *c, nlink_table *links,
1097 u64 range_start, u64 *range_end,
1102 if (inum < range_start || inum >= *range_end)
1105 if (inum - range_start >= SIZE_MAX / sizeof(struct nlink)) {
1110 link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
1112 bch_verbose(c, "allocation failed during fsck - will need another pass");
1124 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
1125 u64 range_start, u64 *range_end)
1127 struct btree_trans trans;
1128 struct btree_iter *iter;
1130 struct bkey_s_c_dirent d;
1134 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1136 inc_link(c, links, range_start, range_end, BCACHEFS_ROOT_INO, false);
1138 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN, 0, k, ret) {
1139 switch (k.k->type) {
1140 case KEY_TYPE_dirent:
1141 d = bkey_s_c_to_dirent(k);
1142 d_inum = le64_to_cpu(d.v->d_inum);
1144 if (d.v->d_type == DT_DIR)
1145 inc_link(c, links, range_start, range_end,
1146 d.k->p.inode, true);
1148 inc_link(c, links, range_start, range_end,
1154 bch2_trans_cond_resched(&trans);
1156 bch2_trans_iter_put(&trans, iter);
1158 ret = bch2_trans_exit(&trans) ?: ret;
1160 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
1165 static int check_inode_nlink(struct bch_fs *c,
1166 struct bch_inode_unpacked *lostfound_inode,
1167 struct bch_inode_unpacked *u,
1171 u32 i_nlink = bch2_inode_nlink_get(u);
1173 link->count * nlink_bias(u->bi_mode) +
1178 * These should have been caught/fixed by earlier passes, we don't
1181 if (S_ISDIR(u->bi_mode) && link->count > 1) {
1182 need_fsck_err(c, "directory %llu with multiple hardlinks: %u",
1183 u->bi_inum, link->count);
1187 if (S_ISDIR(u->bi_mode) && !link->count) {
1188 need_fsck_err(c, "unreachable directory found (inum %llu)",
1193 if (!S_ISDIR(u->bi_mode) && link->dir_count) {
1194 need_fsck_err(c, "non directory with subdirectories (inum %llu)",
1200 !(u->bi_flags & BCH_INODE_UNLINKED) &&
1201 (c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
1202 if (fsck_err(c, "unreachable inode %llu not marked as unlinked (type %u)",
1203 u->bi_inum, mode_to_type(u->bi_mode)) ==
1207 ret = reattach_inode(c, lostfound_inode, u->bi_inum);
1212 real_i_nlink = nlink_bias(u->bi_mode) + link->dir_count;
1216 if (i_nlink < link->count) {
1217 if (fsck_err(c, "inode %llu i_link too small (%u < %u, type %i)",
1218 u->bi_inum, i_nlink, link->count,
1219 mode_to_type(u->bi_mode)) == FSCK_ERR_IGNORE)
1224 if (i_nlink != real_i_nlink &&
1226 if (fsck_err(c, "filesystem marked clean, "
1227 "but inode %llu has wrong i_nlink "
1228 "(type %u i_nlink %u, should be %u)",
1229 u->bi_inum, mode_to_type(u->bi_mode),
1230 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1235 if (i_nlink != real_i_nlink &&
1236 (c->sb.features & (1 << BCH_FEATURE_atomic_nlink))) {
1237 if (fsck_err(c, "inode %llu has wrong i_nlink "
1238 "(type %u i_nlink %u, should be %u)",
1239 u->bi_inum, mode_to_type(u->bi_mode),
1240 i_nlink, real_i_nlink) == FSCK_ERR_IGNORE)
1245 if (real_i_nlink && i_nlink != real_i_nlink)
1246 bch_verbose(c, "setting inode %llu nlink from %u to %u",
1247 u->bi_inum, i_nlink, real_i_nlink);
1249 if (i_nlink != real_i_nlink) {
1250 bch2_inode_nlink_set(u, real_i_nlink);
1257 static int check_inode(struct btree_trans *trans,
1258 struct bch_inode_unpacked *lostfound_inode,
1259 struct btree_iter *iter,
1260 struct bkey_s_c_inode inode,
1263 struct bch_fs *c = trans->c;
1264 struct bch_inode_unpacked u;
1265 bool do_update = false;
1268 ret = bch2_inode_unpack(inode, &u);
1270 bch2_trans_unlock(trans);
1272 if (bch2_fs_inconsistent_on(ret, c,
1273 "error unpacking inode %llu in fsck",
1278 ret = check_inode_nlink(c, lostfound_inode, &u, link,
1284 if (u.bi_flags & BCH_INODE_UNLINKED &&
1286 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
1288 bch_verbose(c, "deleting inode %llu", u.bi_inum);
1292 ret = bch2_inode_rm(c, u.bi_inum, false);
1294 bch_err(c, "error in fsck: error %i while deleting inode", ret);
1298 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
1300 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
1302 bch_verbose(c, "truncating inode %llu", u.bi_inum);
1307 * XXX: need to truncate partial blocks too here - or ideally
1308 * just switch units to bytes and that issue goes away
1310 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1311 POS(u.bi_inum, round_up(u.bi_size, block_bytes(c))),
1312 POS(u.bi_inum, U64_MAX),
1315 bch_err(c, "error in fsck: error %i truncating inode", ret);
1320 * We truncated without our normal sector accounting hook, just
1321 * make sure we recalculate it:
1323 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
1325 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
1329 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
1331 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
1335 bch_verbose(c, "recounting sectors for inode %llu",
1338 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
1340 bch_err(c, "error in fsck: error %i recounting inode sectors",
1345 u.bi_sectors = sectors;
1346 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
1350 if (!S_ISDIR(u.bi_mode) &&
1352 !(u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) &&
1353 (fsck_err_on(c->sb.version >= bcachefs_metadata_version_inode_backpointers, c,
1354 "inode missing BCH_INODE_BACKPTR_UNTRUSTED flags") ||
1355 c->opts.version_upgrade)) {
1356 u.bi_flags |= BCH_INODE_BACKPTR_UNTRUSTED;
1361 struct bkey_inode_buf p;
1363 bch2_inode_pack(c, &p, &u);
1365 ret = __bch2_trans_do(trans, NULL, NULL,
1366 BTREE_INSERT_NOFAIL|
1367 BTREE_INSERT_LAZY_RW,
1368 (bch2_trans_update(trans, iter, &p.inode.k_i, 0), 0));
1370 bch_err(c, "error in fsck: error %i "
1371 "updating inode", ret);
1378 static int bch2_gc_walk_inodes(struct bch_fs *c,
1379 struct bch_inode_unpacked *lostfound_inode,
1381 u64 range_start, u64 range_end)
1383 struct btree_trans trans;
1384 struct btree_iter *iter;
1386 struct nlink *link, zero_links = { 0, 0 };
1387 struct genradix_iter nlinks_iter;
1388 int ret = 0, ret2 = 0;
1391 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1393 iter = bch2_trans_get_iter(&trans, BTREE_ID_inodes,
1394 POS(0, range_start), 0);
1395 nlinks_iter = genradix_iter_init(links, 0);
1397 while ((k = bch2_btree_iter_peek(iter)).k &&
1398 !(ret2 = bkey_err(k)) &&
1399 iter->pos.offset < range_end) {
1400 peek_nlinks: link = genradix_iter_peek(&nlinks_iter, links);
1402 if (!link && (!k.k || iter->pos.offset >= range_end))
1405 nlinks_pos = range_start + nlinks_iter.pos;
1407 if (link && nlinks_pos < iter->pos.offset) {
1408 /* Should have been caught by dirents pass: */
1409 need_fsck_err_on(link->count, c,
1410 "missing inode %llu (nlink %u)",
1411 nlinks_pos, link->count);
1412 genradix_iter_advance(&nlinks_iter, links);
1416 if (!link || nlinks_pos > iter->pos.offset)
1419 if (k.k && k.k->type == KEY_TYPE_inode) {
1420 ret = check_inode(&trans, lostfound_inode, iter,
1421 bkey_s_c_to_inode(k), link);
1422 BUG_ON(ret == -EINTR);
1426 /* Should have been caught by dirents pass: */
1427 need_fsck_err_on(link->count, c,
1428 "missing inode %llu (nlink %u)",
1429 nlinks_pos, link->count);
1432 if (nlinks_pos == iter->pos.offset)
1433 genradix_iter_advance(&nlinks_iter, links);
1435 bch2_btree_iter_advance(iter);
1436 bch2_trans_cond_resched(&trans);
1439 bch2_trans_iter_put(&trans, iter);
1440 bch2_trans_exit(&trans);
1443 bch_err(c, "error in fsck: btree error %i while walking inodes", ret2);
1449 static int check_inode_nlinks(struct bch_fs *c,
1450 struct bch_inode_unpacked *lostfound_inode)
1453 u64 this_iter_range_start, next_iter_range_start = 0;
1456 bch_verbose(c, "checking inode nlinks");
1458 genradix_init(&links);
1461 this_iter_range_start = next_iter_range_start;
1462 next_iter_range_start = U64_MAX;
1464 ret = bch2_gc_walk_dirents(c, &links,
1465 this_iter_range_start,
1466 &next_iter_range_start);
1470 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1471 this_iter_range_start,
1472 next_iter_range_start);
1476 genradix_free(&links);
1477 } while (next_iter_range_start != U64_MAX);
1479 genradix_free(&links);
1485 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1486 * Doesn't fix them yet, mainly because they haven't yet been observed:
1488 int bch2_fsck_full(struct bch_fs *c)
1490 struct bch_inode_unpacked root_inode, lostfound_inode;
1492 return check_extents(c) ?:
1495 check_root(c, &root_inode) ?:
1496 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1497 check_directory_structure(c, &lostfound_inode) ?:
1498 check_inode_nlinks(c, &lostfound_inode);
1501 int bch2_fsck_inode_nlink(struct bch_fs *c)
1503 struct bch_inode_unpacked root_inode, lostfound_inode;
1505 return check_root(c, &root_inode) ?:
1506 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1507 check_inode_nlinks(c, &lostfound_inode);
1510 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1512 struct btree_trans trans;
1513 struct btree_iter *iter;
1515 struct bkey_s_c_inode inode;
1518 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1520 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN, 0, k, ret) {
1521 if (k.k->type != KEY_TYPE_inode)
1524 inode = bkey_s_c_to_inode(k);
1526 if (inode.v->bi_flags &
1527 (BCH_INODE_I_SIZE_DIRTY|
1528 BCH_INODE_I_SECTORS_DIRTY|
1529 BCH_INODE_UNLINKED)) {
1530 ret = check_inode(&trans, NULL, iter, inode, NULL);
1535 bch2_trans_iter_put(&trans, iter);
1537 BUG_ON(ret == -EINTR);
1539 return bch2_trans_exit(&trans) ?: ret;