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 lookup_inode(struct btree_trans *trans, u64 inode_nr,
42 struct bch_inode_unpacked *inode,
45 struct btree_iter *iter;
49 iter = bch2_trans_get_iter(trans, BTREE_ID_inodes,
51 k = bch2_btree_iter_peek_slot(iter);
57 *snapshot = iter->pos.snapshot;
58 ret = k.k->type == KEY_TYPE_inode
59 ? bch2_inode_unpack(bkey_s_c_to_inode(k), inode)
62 bch2_trans_iter_put(trans, iter);
66 static int write_inode(struct btree_trans *trans,
67 struct bch_inode_unpacked *inode,
70 struct btree_iter *inode_iter =
71 bch2_trans_get_iter(trans, BTREE_ID_inodes,
72 SPOS(0, inode->bi_inum, snapshot),
74 int ret = __bch2_trans_do(trans, NULL, NULL,
77 bch2_inode_write(trans, inode_iter, inode));
78 bch2_trans_iter_put(trans, inode_iter);
80 bch_err(trans->c, "error in fsck: error %i updating inode", ret);
84 static int __remove_dirent(struct btree_trans *trans,
85 struct bkey_s_c_dirent dirent)
87 struct bch_fs *c = trans->c;
89 struct bch_inode_unpacked dir_inode;
90 struct bch_hash_info dir_hash_info;
91 u64 dir_inum = dirent.k->p.inode;
95 name.len = bch2_dirent_name_bytes(dirent);
96 buf = bch2_trans_kmalloc(trans, name.len + 1);
100 memcpy(buf, dirent.v->d_name, name.len);
101 buf[name.len] = '\0';
104 ret = lookup_inode(trans, dir_inum, &dir_inode, NULL);
105 if (ret && ret != -EINTR)
106 bch_err(c, "remove_dirent: err %i looking up directory inode", ret);
110 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
112 ret = bch2_hash_delete(trans, bch2_dirent_hash_desc,
113 &dir_hash_info, dir_inum, &name);
114 if (ret && ret != -EINTR)
115 bch_err(c, "remove_dirent: err %i deleting dirent", ret);
122 static int remove_dirent(struct btree_trans *trans,
123 struct bkey_s_c_dirent dirent)
125 return __bch2_trans_do(trans, NULL, NULL,
127 BTREE_INSERT_LAZY_RW,
128 __remove_dirent(trans, dirent));
131 static int reattach_inode(struct bch_fs *c,
132 struct bch_inode_unpacked *lostfound_inode,
135 struct bch_inode_unpacked dir_u, inode_u;
140 snprintf(name_buf, sizeof(name_buf), "%llu", inum);
141 name = (struct qstr) QSTR(name_buf);
143 ret = bch2_trans_do(c, NULL, NULL,
144 BTREE_INSERT_LAZY_RW,
145 bch2_link_trans(&trans, lostfound_inode->bi_inum,
146 inum, &dir_u, &inode_u, &name));
148 bch_err(c, "error %i reattaching inode %llu", ret, inum);
153 struct inode_walker {
154 bool first_this_inode;
158 struct bch_inode_unpacked inode;
161 static struct inode_walker inode_walker_init(void)
163 return (struct inode_walker) {
169 static int walk_inode(struct btree_trans *trans,
170 struct inode_walker *w, u64 inum)
172 if (inum != w->cur_inum) {
173 int ret = lookup_inode(trans, inum, &w->inode, &w->snapshot);
175 if (ret && ret != -ENOENT)
178 w->have_inode = !ret;
180 w->first_this_inode = true;
182 w->first_this_inode = false;
188 static int hash_redo_key(struct btree_trans *trans,
189 const struct bch_hash_desc desc,
190 struct bch_hash_info *hash_info,
191 struct btree_iter *k_iter, struct bkey_s_c k)
193 struct bkey_i delete;
196 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
200 bkey_reassemble(tmp, k);
202 bkey_init(&delete.k);
203 delete.k.p = k_iter->pos;
204 bch2_trans_update(trans, k_iter, &delete, 0);
206 return bch2_hash_set(trans, desc, hash_info, k_iter->pos.inode,
210 static int fsck_hash_delete_at(struct btree_trans *trans,
211 const struct bch_hash_desc desc,
212 struct bch_hash_info *info,
213 struct btree_iter *iter)
217 ret = bch2_hash_delete_at(trans, desc, info, iter) ?:
218 bch2_trans_commit(trans, NULL, NULL,
220 BTREE_INSERT_LAZY_RW);
222 ret = bch2_btree_iter_traverse(iter);
230 static int hash_check_key(struct btree_trans *trans,
231 const struct bch_hash_desc desc,
232 struct bch_hash_info *hash_info,
233 struct btree_iter *k_iter, struct bkey_s_c hash_k)
235 struct bch_fs *c = trans->c;
236 struct btree_iter *iter = NULL;
242 if (hash_k.k->type != desc.key_type)
245 hash = desc.hash_bkey(hash_info, hash_k);
247 if (likely(hash == hash_k.k->p.offset))
250 if (hash_k.k->p.offset < hash)
253 for_each_btree_key(trans, iter, desc.btree_id, POS(hash_k.k->p.inode, hash),
254 BTREE_ITER_SLOTS, k, ret) {
255 if (!bkey_cmp(k.k->p, hash_k.k->p))
258 if (fsck_err_on(k.k->type == desc.key_type &&
259 !desc.cmp_bkey(k, hash_k), c,
260 "duplicate hash table keys:\n%s",
261 (bch2_bkey_val_to_text(&PBUF(buf), c,
263 ret = fsck_hash_delete_at(trans, desc, hash_info, k_iter);
270 if (bkey_deleted(k.k)) {
271 bch2_trans_iter_free(trans, iter);
276 bch2_trans_iter_free(trans, iter);
279 if (fsck_err(c, "hash table key at wrong offset: btree %u inode %llu offset %llu, "
280 "hashed to %llu should be at %llu\n%s",
281 desc.btree_id, hash_k.k->p.inode, hash_k.k->p.offset,
282 hash, iter->pos.offset,
283 (bch2_bkey_val_to_text(&PBUF(buf), c, hash_k), buf)) == FSCK_ERR_IGNORE)
286 ret = __bch2_trans_do(trans, NULL, NULL,
287 BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
288 hash_redo_key(trans, desc, hash_info, k_iter, hash_k));
290 bch_err(c, "hash_redo_key err %i", ret);
298 static int check_inode(struct btree_trans *trans,
299 struct btree_iter *iter,
300 struct bkey_s_c_inode inode)
302 struct bch_fs *c = trans->c;
303 struct bch_inode_unpacked u;
304 bool do_update = false;
307 ret = bch2_inode_unpack(inode, &u);
309 if (bch2_fs_inconsistent_on(ret, c,
310 "error unpacking inode %llu in fsck",
314 if (u.bi_flags & BCH_INODE_UNLINKED &&
316 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
318 bch_verbose(c, "deleting inode %llu", u.bi_inum);
320 bch2_trans_unlock(trans);
323 ret = bch2_inode_rm(c, u.bi_inum, false);
325 bch_err(c, "error in fsck: error %i while deleting inode", ret);
329 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
331 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
333 bch_verbose(c, "truncating inode %llu", u.bi_inum);
335 bch2_trans_unlock(trans);
339 * XXX: need to truncate partial blocks too here - or ideally
340 * just switch units to bytes and that issue goes away
342 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
343 POS(u.bi_inum, round_up(u.bi_size, block_bytes(c))),
344 POS(u.bi_inum, U64_MAX),
347 bch_err(c, "error in fsck: error %i truncating inode", ret);
352 * We truncated without our normal sector accounting hook, just
353 * make sure we recalculate it:
355 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
357 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
361 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
363 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
367 bch_verbose(c, "recounting sectors for inode %llu",
370 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
372 bch_err(c, "error in fsck: error %i recounting inode sectors",
377 u.bi_sectors = sectors;
378 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
382 if (!S_ISDIR(u.bi_mode) &&
384 !(u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) &&
385 (fsck_err_on(c->sb.version >= bcachefs_metadata_version_inode_backpointers, c,
386 "inode missing BCH_INODE_BACKPTR_UNTRUSTED flags") ||
387 c->opts.version_upgrade)) {
388 u.bi_flags |= BCH_INODE_BACKPTR_UNTRUSTED;
393 struct bkey_inode_buf p;
395 bch2_inode_pack(c, &p, &u);
396 p.inode.k.p = iter->pos;
398 ret = __bch2_trans_do(trans, NULL, NULL,
400 BTREE_INSERT_LAZY_RW,
401 (bch2_trans_update(trans, iter, &p.inode.k_i, 0), 0));
403 bch_err(c, "error in fsck: error %i "
404 "updating inode", ret);
411 static int check_inodes(struct bch_fs *c, bool full)
413 struct btree_trans trans;
414 struct btree_iter *iter;
416 struct bkey_s_c_inode inode;
419 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
421 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN, 0, k, ret) {
422 if (k.k->type != KEY_TYPE_inode)
425 inode = bkey_s_c_to_inode(k);
428 (inode.v->bi_flags & (BCH_INODE_I_SIZE_DIRTY|
429 BCH_INODE_I_SECTORS_DIRTY|
430 BCH_INODE_UNLINKED))) {
431 ret = check_inode(&trans, iter, inode);
436 bch2_trans_iter_put(&trans, iter);
438 BUG_ON(ret == -EINTR);
440 return bch2_trans_exit(&trans) ?: ret;
443 static int fix_overlapping_extent(struct btree_trans *trans,
444 struct bkey_s_c k, struct bpos cut_at)
446 struct btree_iter *iter;
450 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
451 ret = PTR_ERR_OR_ZERO(u);
455 bkey_reassemble(u, k);
456 bch2_cut_front(cut_at, u);
460 * We don't want to go through the extent_handle_overwrites path:
462 * XXX: this is going to screw up disk accounting, extent triggers
463 * assume things about extent overwrites - we should be running the
464 * triggers manually here
466 iter = bch2_trans_get_iter(trans, BTREE_ID_extents, u->k.p,
467 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
469 BUG_ON(iter->flags & BTREE_ITER_IS_EXTENTS);
470 bch2_trans_update(trans, iter, u, BTREE_TRIGGER_NORUN);
471 bch2_trans_iter_put(trans, iter);
473 return bch2_trans_commit(trans, NULL, NULL,
475 BTREE_INSERT_LAZY_RW);
478 static int inode_backpointer_exists(struct btree_trans *trans,
479 struct bch_inode_unpacked *inode)
481 struct btree_iter *iter;
485 iter = bch2_trans_get_iter(trans, BTREE_ID_dirents,
486 POS(inode->bi_dir, inode->bi_dir_offset), 0);
487 k = bch2_btree_iter_peek_slot(iter);
491 if (k.k->type != KEY_TYPE_dirent)
494 ret = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum) == inode->bi_inum;
496 bch2_trans_iter_free(trans, iter);
500 static bool inode_backpointer_matches(struct bkey_s_c_dirent d,
501 struct bch_inode_unpacked *inode)
503 return d.k->p.inode == inode->bi_dir &&
504 d.k->p.offset == inode->bi_dir_offset;
508 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
509 * that i_size an i_sectors are consistent
512 static int check_extents(struct bch_fs *c)
514 struct inode_walker w = inode_walker_init();
515 struct btree_trans trans;
516 struct btree_iter *iter;
518 struct bkey_buf prev;
522 bch2_bkey_buf_init(&prev);
523 prev.k->k = KEY(0, 0, 0);
524 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
526 bch_verbose(c, "checking extents");
528 iter = bch2_trans_get_iter(&trans, BTREE_ID_extents,
529 POS(BCACHEFS_ROOT_INO, 0),
532 while ((k = bch2_btree_iter_peek(iter)).k &&
533 !(ret = bkey_err(k))) {
535 w.cur_inum != k.k->p.inode &&
536 !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
537 fsck_err_on(w.inode.bi_sectors != i_sectors, c,
538 "inode %llu has incorrect i_sectors: got %llu, should be %llu",
540 w.inode.bi_sectors, i_sectors)) {
541 w.inode.bi_sectors = i_sectors;
543 ret = write_inode(&trans, &w.inode, w.snapshot);
548 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
552 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
553 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
555 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2))
556 return fix_overlapping_extent(&trans, k, prev.k->k.p) ?: -EINTR;
559 ret = walk_inode(&trans, &w, k.k->p.inode);
563 if (w.first_this_inode)
566 if (fsck_err_on(!w.have_inode, c,
567 "extent type %u for missing inode %llu",
568 k.k->type, k.k->p.inode) ||
569 fsck_err_on(w.have_inode &&
570 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
571 "extent type %u for non regular file, inode %llu mode %o",
572 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
574 return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
575 POS(k.k->p.inode, 0),
576 POS(k.k->p.inode, U64_MAX),
580 if (fsck_err_on(w.have_inode &&
581 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
582 k.k->type != KEY_TYPE_reservation &&
583 k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
584 "extent type %u offset %llu past end of inode %llu, i_size %llu",
585 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
587 return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
588 POS(k.k->p.inode, round_up(w.inode.bi_size, block_bytes(c))),
589 POS(k.k->p.inode, U64_MAX),
593 if (bkey_extent_is_allocation(k.k))
594 i_sectors += k.k->size;
595 bch2_bkey_buf_reassemble(&prev, c, k);
597 bch2_btree_iter_advance(iter);
602 bch2_trans_iter_put(&trans, iter);
603 bch2_bkey_buf_exit(&prev, c);
604 return bch2_trans_exit(&trans) ?: ret;
608 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
612 static int check_dirents(struct bch_fs *c)
614 struct inode_walker w = inode_walker_init();
615 struct bch_hash_info hash_info;
616 struct btree_trans trans;
617 struct btree_iter *iter;
620 unsigned nr_subdirs = 0;
623 bch_verbose(c, "checking dirents");
625 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
627 iter = bch2_trans_get_iter(&trans, BTREE_ID_dirents,
628 POS(BCACHEFS_ROOT_INO, 0), 0);
630 while ((k = bch2_btree_iter_peek(iter)).k &&
631 !(ret = bkey_err(k))) {
632 struct bkey_s_c_dirent d;
633 struct bch_inode_unpacked target;
636 bool backpointer_exists = true;
640 w.cur_inum != k.k->p.inode &&
641 fsck_err_on(w.inode.bi_nlink != nr_subdirs, c,
642 "directory %llu with wrong i_nlink: got %u, should be %u",
643 w.inode.bi_inum, w.inode.bi_nlink, nr_subdirs)) {
644 w.inode.bi_nlink = nr_subdirs;
645 ret = write_inode(&trans, &w.inode, w.snapshot);
650 ret = walk_inode(&trans, &w, k.k->p.inode);
654 if (w.first_this_inode)
657 if (fsck_err_on(!w.have_inode, c,
658 "dirent in nonexisting directory:\n%s",
659 (bch2_bkey_val_to_text(&PBUF(buf), c,
661 fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
662 "dirent in non directory inode type %u:\n%s",
663 mode_to_type(w.inode.bi_mode),
664 (bch2_bkey_val_to_text(&PBUF(buf), c,
666 ret = bch2_btree_delete_at(&trans, iter, 0);
675 if (w.first_this_inode)
676 hash_info = bch2_hash_info_init(c, &w.inode);
678 ret = hash_check_key(&trans, bch2_dirent_hash_desc,
679 &hash_info, iter, k);
687 if (k.k->type != KEY_TYPE_dirent)
690 d = bkey_s_c_to_dirent(k);
691 d_inum = le64_to_cpu(d.v->d_inum);
693 ret = lookup_inode(&trans, d_inum, &target, &target_snapshot);
694 if (ret && ret != -ENOENT)
700 if (fsck_err_on(!have_target, c,
701 "dirent points to missing inode:\n%s",
702 (bch2_bkey_val_to_text(&PBUF(buf), c,
704 ret = remove_dirent(&trans, d);
713 if (!inode_backpointer_matches(d, &target)) {
714 ret = inode_backpointer_exists(&trans, &target);
718 backpointer_exists = ret;
722 if (fsck_err_on(S_ISDIR(target.bi_mode) &&
723 !inode_backpointer_matches(d, &target) &&
724 backpointer_exists, c,
725 "directory %llu with multiple links",
727 ret = remove_dirent(&trans, d);
733 if (!inode_backpointer_matches(d, &target) &&
734 (S_ISDIR(target.bi_mode) || !target.bi_nlink)) {
735 if (backpointer_exists) {
736 if (!fsck_err(c, "inode %llu has multiple links but i_nlink 0",
741 target.bi_flags |= BCH_INODE_BACKPTR_UNTRUSTED;
743 if (c->sb.version >= bcachefs_metadata_version_inode_backpointers &&
744 !(target.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) &&
745 !fsck_err(c, "inode %llu has wrong backpointer:\n"
747 "should be %llu:%llu",
750 target.bi_dir_offset,
755 target.bi_dir = k.k->p.inode;
756 target.bi_dir_offset = k.k->p.offset;
757 target.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
760 ret = write_inode(&trans, &target, target_snapshot);
766 if (fsck_err_on(d.v->d_type != mode_to_type(target.bi_mode), c,
767 "incorrect d_type: should be %u:\n%s",
768 mode_to_type(target.bi_mode),
769 (bch2_bkey_val_to_text(&PBUF(buf), c,
771 struct bkey_i_dirent *n;
773 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
779 bkey_reassemble(&n->k_i, d.s_c);
780 n->v.d_type = mode_to_type(target.bi_mode);
782 ret = __bch2_trans_do(&trans, NULL, NULL,
784 BTREE_INSERT_LAZY_RW,
785 (bch2_trans_update(&trans, iter, &n->k_i, 0), 0));
792 nr_subdirs += d.v->d_type == DT_DIR;
794 bch2_btree_iter_advance(iter);
801 bch2_trans_iter_put(&trans, iter);
802 return bch2_trans_exit(&trans) ?: ret;
806 * Walk xattrs: verify that they all have a corresponding inode
809 static int check_xattrs(struct bch_fs *c)
811 struct inode_walker w = inode_walker_init();
812 struct bch_hash_info hash_info;
813 struct btree_trans trans;
814 struct btree_iter *iter;
818 bch_verbose(c, "checking xattrs");
820 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
822 iter = bch2_trans_get_iter(&trans, BTREE_ID_xattrs,
823 POS(BCACHEFS_ROOT_INO, 0), 0);
825 while ((k = bch2_btree_iter_peek(iter)).k &&
826 !(ret = bkey_err(k))) {
827 ret = walk_inode(&trans, &w, k.k->p.inode);
831 if (fsck_err_on(!w.have_inode, c,
832 "xattr for missing inode %llu",
834 ret = bch2_btree_delete_at(&trans, iter, 0);
840 if (w.first_this_inode && w.have_inode)
841 hash_info = bch2_hash_info_init(c, &w.inode);
843 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
844 &hash_info, iter, k);
848 bch2_btree_iter_advance(iter);
854 bch2_trans_iter_put(&trans, iter);
855 return bch2_trans_exit(&trans) ?: ret;
858 /* Get root directory, create if it doesn't exist: */
859 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
861 struct bkey_inode_buf packed;
864 bch_verbose(c, "checking root directory");
866 ret = bch2_trans_do(c, NULL, NULL, 0,
867 __bch2_inode_find_by_inum_trans(&trans, BCACHEFS_ROOT_INO,
869 if (ret && ret != -ENOENT)
872 if (fsck_err_on(ret, c, "root directory missing"))
875 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
876 "root inode not a directory"))
883 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
885 root_inode->bi_inum = BCACHEFS_ROOT_INO;
887 bch2_inode_pack(c, &packed, root_inode);
889 return bch2_btree_insert(c, BTREE_ID_inodes, &packed.inode.k_i,
892 BTREE_INSERT_LAZY_RW);
895 /* Get lost+found, create if it doesn't exist: */
896 static int check_lostfound(struct bch_fs *c,
897 struct bch_inode_unpacked *root_inode,
898 struct bch_inode_unpacked *lostfound_inode)
900 struct qstr lostfound = QSTR("lost+found");
901 struct bch_hash_info root_hash_info =
902 bch2_hash_info_init(c, root_inode);
906 bch_verbose(c, "checking lost+found");
908 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
911 bch_notice(c, "creating lost+found");
912 goto create_lostfound;
915 ret = bch2_trans_do(c, NULL, NULL, 0,
916 __bch2_inode_find_by_inum_trans(&trans, inum, lostfound_inode, 0));
917 if (ret && ret != -ENOENT)
920 if (fsck_err_on(ret, c, "lost+found missing"))
921 goto create_lostfound;
923 if (fsck_err_on(!S_ISDIR(lostfound_inode->bi_mode), c,
924 "lost+found inode not a directory"))
925 goto create_lostfound;
931 bch2_inode_init_early(c, lostfound_inode);
933 ret = bch2_trans_do(c, NULL, NULL,
935 BTREE_INSERT_LAZY_RW,
936 bch2_create_trans(&trans,
937 BCACHEFS_ROOT_INO, root_inode,
938 lostfound_inode, &lostfound,
939 0, 0, S_IFDIR|0700, 0, NULL, NULL));
941 bch_err(c, "error creating lost+found: %i", ret);
946 typedef GENRADIX(unsigned long) inode_bitmap;
948 static inline bool inode_bitmap_test(inode_bitmap *b, size_t nr)
950 unsigned long *w = genradix_ptr(b, nr / BITS_PER_LONG);
951 return w ? test_bit(nr & (BITS_PER_LONG - 1), w) : false;
954 static inline int inode_bitmap_set(inode_bitmap *b, size_t nr)
956 unsigned long *w = genradix_ptr_alloc(b, nr / BITS_PER_LONG, GFP_KERNEL);
961 *w |= 1UL << (nr & (BITS_PER_LONG - 1));
969 struct pathbuf_entry {
975 static int path_down(struct pathbuf *p, u64 inum)
977 if (p->nr == p->size) {
978 size_t new_size = max_t(size_t, 256UL, p->size * 2);
979 void *n = krealloc(p->entries,
980 new_size * sizeof(p->entries[0]),
989 p->entries[p->nr++] = (struct pathbuf_entry) {
997 static int check_directory_structure(struct bch_fs *c,
998 struct bch_inode_unpacked *lostfound_inode)
1000 inode_bitmap dirs_done;
1001 struct pathbuf path = { 0, 0, NULL };
1002 struct pathbuf_entry *e;
1003 struct btree_trans trans;
1004 struct btree_iter *iter;
1006 struct bkey_s_c_dirent dirent;
1007 bool had_unreachable;
1011 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1013 bch_verbose(c, "checking directory structure");
1017 genradix_init(&dirs_done);
1018 had_unreachable = false;
1020 ret = inode_bitmap_set(&dirs_done, BCACHEFS_ROOT_INO);
1022 bch_err(c, "memory allocation failure in inode_bitmap_set()");
1026 ret = path_down(&path, BCACHEFS_ROOT_INO);
1032 e = &path.entries[path.nr - 1];
1034 if (e->offset == U64_MAX)
1037 for_each_btree_key(&trans, iter, BTREE_ID_dirents,
1038 POS(e->inum, e->offset + 1), 0, k, ret) {
1039 if (k.k->p.inode != e->inum)
1042 e->offset = k.k->p.offset;
1044 if (k.k->type != KEY_TYPE_dirent)
1047 dirent = bkey_s_c_to_dirent(k);
1049 if (dirent.v->d_type != DT_DIR)
1052 d_inum = le64_to_cpu(dirent.v->d_inum);
1054 if (fsck_err_on(inode_bitmap_test(&dirs_done, d_inum), c,
1055 "directory %llu has multiple hardlinks",
1057 ret = remove_dirent(&trans, dirent);
1063 ret = inode_bitmap_set(&dirs_done, d_inum);
1065 bch_err(c, "memory allocation failure in inode_bitmap_set()");
1069 ret = path_down(&path, d_inum);
1074 ret = bch2_trans_iter_free(&trans, iter);
1076 bch_err(c, "btree error %i in fsck", ret);
1081 ret = bch2_trans_iter_free(&trans, iter) ?: ret;
1083 bch_err(c, "btree error %i in fsck", ret);
1090 iter = bch2_trans_get_iter(&trans, BTREE_ID_inodes, POS_MIN, 0);
1092 for_each_btree_key_continue(iter, 0, k, ret) {
1093 if (k.k->type != KEY_TYPE_inode)
1096 if (!S_ISDIR(le16_to_cpu(bkey_s_c_to_inode(k).v->bi_mode)))
1099 ret = bch2_empty_dir_trans(&trans, k.k->p.inode);
1105 if (fsck_err_on(!inode_bitmap_test(&dirs_done, k.k->p.offset), c,
1106 "unreachable directory found (inum %llu)",
1108 bch2_trans_unlock(&trans);
1110 ret = reattach_inode(c, lostfound_inode, k.k->p.offset);
1115 had_unreachable = true;
1118 bch2_trans_iter_free(&trans, iter);
1122 if (had_unreachable) {
1123 bch_info(c, "reattached unreachable directories, restarting pass to check for loops");
1124 genradix_free(&dirs_done);
1125 kfree(path.entries);
1126 memset(&dirs_done, 0, sizeof(dirs_done));
1127 memset(&path, 0, sizeof(path));
1132 ret = bch2_trans_exit(&trans) ?: ret;
1133 genradix_free(&dirs_done);
1134 kfree(path.entries);
1142 typedef GENRADIX(struct nlink) nlink_table;
1144 static void inc_link(struct bch_fs *c, nlink_table *links,
1145 u64 range_start, u64 *range_end, u64 inum)
1149 if (inum < range_start || inum >= *range_end)
1152 if (inum - range_start >= SIZE_MAX / sizeof(struct nlink)) {
1157 link = genradix_ptr_alloc(links, inum - range_start, GFP_KERNEL);
1159 bch_verbose(c, "allocation failed during fsck - will need another pass");
1168 static int bch2_gc_walk_dirents(struct bch_fs *c, nlink_table *links,
1169 u64 range_start, u64 *range_end)
1171 struct btree_trans trans;
1172 struct btree_iter *iter;
1174 struct bkey_s_c_dirent d;
1177 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1179 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN, 0, k, ret) {
1180 switch (k.k->type) {
1181 case KEY_TYPE_dirent:
1182 d = bkey_s_c_to_dirent(k);
1184 if (d.v->d_type != DT_DIR)
1185 inc_link(c, links, range_start, range_end,
1186 le64_to_cpu(d.v->d_inum));
1190 bch2_trans_cond_resched(&trans);
1192 bch2_trans_iter_put(&trans, iter);
1194 ret = bch2_trans_exit(&trans) ?: ret;
1196 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
1201 static int check_inode_nlink(struct btree_trans *trans,
1202 struct bch_inode_unpacked *lostfound_inode,
1203 struct btree_iter *iter,
1204 struct bkey_s_c_inode inode,
1207 struct bch_fs *c = trans->c;
1208 struct bch_inode_unpacked u;
1212 * Backpointer and directory structure checks are sufficient for
1213 * directories, since they can't have hardlinks:
1215 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
1218 ret = bch2_inode_unpack(inode, &u);
1220 /* Should never happen, checked by bch2_inode_invalid: */
1221 if (bch2_fs_inconsistent_on(ret, c,
1222 "error unpacking inode %llu in fsck",
1226 /* Improved directory structure pass will catch this: */
1227 if (fsck_err_on(!nlink, c,
1228 "unreachable inode %llu not marked as unlinked (type %u)",
1229 u.bi_inum, mode_to_type(u.bi_mode))) {
1230 ret = reattach_inode(c, lostfound_inode, u.bi_inum);
1237 if (fsck_err_on(bch2_inode_nlink_get(&u) != nlink, c,
1238 "inode %llu has wrong i_nlink (type %u i_nlink %u, should be %u)",
1239 u.bi_inum, mode_to_type(u.bi_mode),
1240 bch2_inode_nlink_get(&u), nlink)) {
1241 struct bkey_inode_buf p;
1244 u.bi_flags |= BCH_INODE_BACKPTR_UNTRUSTED;
1246 bch2_inode_nlink_set(&u, nlink);
1247 bch2_inode_pack(c, &p, &u);
1248 p.inode.k.p = iter->pos;
1250 ret = __bch2_trans_do(trans, NULL, NULL,
1251 BTREE_INSERT_NOFAIL|
1252 BTREE_INSERT_LAZY_RW,
1253 (bch2_trans_update(trans, iter, &p.inode.k_i, 0), 0));
1255 bch_err(c, "error in fsck: error %i updating inode", ret);
1262 static int bch2_gc_walk_inodes(struct bch_fs *c,
1263 struct bch_inode_unpacked *lostfound_inode,
1265 u64 range_start, u64 range_end)
1267 struct btree_trans trans;
1268 struct btree_iter *iter;
1273 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1275 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
1276 POS(0, range_start), 0, k, ret) {
1277 if (!k.k || k.k->p.offset >= range_end)
1280 if (k.k->type != KEY_TYPE_inode)
1283 link = genradix_ptr(links, k.k->p.offset - range_start);
1284 ret = check_inode_nlink(&trans, lostfound_inode, iter,
1285 bkey_s_c_to_inode(k), link ? link->count : 0);
1290 bch2_trans_iter_put(&trans, iter);
1291 bch2_trans_exit(&trans);
1294 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
1300 static int check_nlinks(struct bch_fs *c,
1301 struct bch_inode_unpacked *lostfound_inode)
1304 u64 this_iter_range_start, next_iter_range_start = 0;
1307 bch_verbose(c, "checking inode nlinks");
1309 genradix_init(&links);
1312 this_iter_range_start = next_iter_range_start;
1313 next_iter_range_start = U64_MAX;
1315 ret = bch2_gc_walk_dirents(c, &links,
1316 this_iter_range_start,
1317 &next_iter_range_start);
1321 ret = bch2_gc_walk_inodes(c, lostfound_inode, &links,
1322 this_iter_range_start,
1323 next_iter_range_start);
1327 genradix_free(&links);
1328 } while (next_iter_range_start != U64_MAX);
1330 genradix_free(&links);
1336 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1337 * Doesn't fix them yet, mainly because they haven't yet been observed:
1339 int bch2_fsck_full(struct bch_fs *c)
1341 struct bch_inode_unpacked root_inode, lostfound_inode;
1343 return check_inodes(c, true) ?:
1347 check_root(c, &root_inode) ?:
1348 check_lostfound(c, &root_inode, &lostfound_inode) ?:
1349 check_directory_structure(c, &lostfound_inode) ?:
1350 check_nlinks(c, &lostfound_inode);
1353 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1355 return check_inodes(c, false);