1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
15 #include <linux/bsearch.h>
16 #include <linux/dcache.h> /* struct qstr */
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_free(trans, iter);
66 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
67 struct bch_inode_unpacked *inode,
70 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
73 static int __write_inode(struct btree_trans *trans,
74 struct bch_inode_unpacked *inode,
77 struct btree_iter *inode_iter =
78 bch2_trans_get_iter(trans, BTREE_ID_inodes,
79 SPOS(0, inode->bi_inum, snapshot),
81 int ret = bch2_inode_write(trans, inode_iter, inode);
82 bch2_trans_iter_put(trans, inode_iter);
86 static int write_inode(struct btree_trans *trans,
87 struct bch_inode_unpacked *inode,
90 int ret = __bch2_trans_do(trans, NULL, NULL,
93 __write_inode(trans, inode, snapshot));
95 bch_err(trans->c, "error in fsck: error %i updating inode", ret);
99 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
101 struct bch_fs *c = trans->c;
102 struct btree_iter *iter;
103 struct bch_inode_unpacked dir_inode;
104 struct bch_hash_info dir_hash_info;
107 ret = lookup_inode(trans, pos.inode, &dir_inode, NULL);
111 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
113 iter = bch2_trans_get_iter(trans, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
115 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
116 &dir_hash_info, iter);
117 bch2_trans_iter_put(trans, iter);
121 static int remove_dirent(struct btree_trans *trans, struct bpos pos)
123 int ret = __bch2_trans_do(trans, NULL, NULL,
125 BTREE_INSERT_LAZY_RW,
126 __remove_dirent(trans, pos));
128 bch_err(trans->c, "remove_dirent: err %i deleting dirent", ret);
132 /* Get lost+found, create if it doesn't exist: */
133 static int lookup_lostfound(struct btree_trans *trans,
134 struct bch_inode_unpacked *lostfound)
136 struct bch_fs *c = trans->c;
137 struct bch_inode_unpacked root;
138 struct bch_hash_info root_hash_info;
139 struct qstr lostfound_str = QSTR("lost+found");
144 ret = lookup_inode(trans, BCACHEFS_ROOT_INO, &root, &snapshot);
145 if (ret && ret != -ENOENT)
148 root_hash_info = bch2_hash_info_init(c, &root);
149 inum = bch2_dirent_lookup(c, BCACHEFS_ROOT_INO, &root_hash_info,
152 bch_notice(c, "creating lost+found");
153 goto create_lostfound;
156 ret = lookup_inode(trans, inum, lostfound, &snapshot);
157 if (ret && ret != -ENOENT) {
159 * The check_dirents pass has already run, dangling dirents
160 * shouldn't exist here:
162 bch_err(c, "error looking up lost+found: %i", ret);
166 if (ret == -ENOENT) {
168 bch2_inode_init_early(c, lostfound);
170 ret = __bch2_trans_do(trans, NULL, NULL,
172 BTREE_INSERT_LAZY_RW,
173 bch2_create_trans(trans,
174 BCACHEFS_ROOT_INO, &root,
177 0, 0, S_IFDIR|0700, 0, NULL, NULL));
179 bch_err(c, "error creating lost+found: %i", ret);
185 static int reattach_inode(struct btree_trans *trans,
186 struct bch_inode_unpacked *inode)
188 struct bch_hash_info dir_hash;
189 struct bch_inode_unpacked lostfound;
195 ret = lookup_lostfound(trans, &lostfound);
199 if (S_ISDIR(inode->bi_mode)) {
200 lostfound.bi_nlink++;
202 ret = write_inode(trans, &lostfound, U32_MAX);
207 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
209 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
210 name = (struct qstr) QSTR(name_buf);
212 ret = __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
213 bch2_dirent_create(trans, lostfound.bi_inum, &dir_hash,
214 mode_to_type(inode->bi_mode),
215 &name, inode->bi_inum, &dir_offset,
216 BCH_HASH_SET_MUST_CREATE));
218 bch_err(trans->c, "error %i reattaching inode %llu",
219 ret, inode->bi_inum);
223 inode->bi_dir = lostfound.bi_inum;
224 inode->bi_dir_offset = dir_offset;
226 return write_inode(trans, inode, U32_MAX);
229 static int remove_backpointer(struct btree_trans *trans,
230 struct bch_inode_unpacked *inode)
232 struct btree_iter *iter;
236 iter = bch2_trans_get_iter(trans, BTREE_ID_dirents,
237 POS(inode->bi_dir, inode->bi_dir_offset), 0);
238 k = bch2_btree_iter_peek_slot(iter);
242 if (k.k->type != KEY_TYPE_dirent) {
247 ret = remove_dirent(trans, k.k->p);
249 bch2_trans_iter_put(trans, iter);
253 struct inode_walker {
254 bool first_this_inode;
258 struct bch_inode_unpacked inode;
261 static struct inode_walker inode_walker_init(void)
263 return (struct inode_walker) {
269 static int walk_inode(struct btree_trans *trans,
270 struct inode_walker *w, u64 inum)
272 if (inum != w->cur_inum) {
273 int ret = lookup_inode(trans, inum, &w->inode, &w->snapshot);
275 if (ret && ret != -ENOENT)
278 w->have_inode = !ret;
280 w->first_this_inode = true;
282 w->first_this_inode = false;
288 static int hash_redo_key(struct btree_trans *trans,
289 const struct bch_hash_desc desc,
290 struct bch_hash_info *hash_info,
291 struct btree_iter *k_iter, struct bkey_s_c k)
293 struct bkey_i *delete;
296 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
298 return PTR_ERR(delete);
300 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
304 bkey_reassemble(tmp, k);
306 bkey_init(&delete->k);
307 delete->k.p = k_iter->pos;
308 bch2_trans_update(trans, k_iter, delete, 0);
310 return bch2_hash_set(trans, desc, hash_info, k_iter->pos.inode, tmp, 0);
313 static int fsck_hash_delete_at(struct btree_trans *trans,
314 const struct bch_hash_desc desc,
315 struct bch_hash_info *info,
316 struct btree_iter *iter)
320 ret = bch2_hash_delete_at(trans, desc, info, iter) ?:
321 bch2_trans_commit(trans, NULL, NULL,
323 BTREE_INSERT_LAZY_RW);
325 ret = bch2_btree_iter_traverse(iter);
333 static int hash_check_key(struct btree_trans *trans,
334 const struct bch_hash_desc desc,
335 struct bch_hash_info *hash_info,
336 struct btree_iter *k_iter, struct bkey_s_c hash_k)
338 struct bch_fs *c = trans->c;
339 struct btree_iter *iter = NULL;
345 if (hash_k.k->type != desc.key_type)
348 hash = desc.hash_bkey(hash_info, hash_k);
350 if (likely(hash == hash_k.k->p.offset))
353 if (hash_k.k->p.offset < hash)
356 for_each_btree_key(trans, iter, desc.btree_id, POS(hash_k.k->p.inode, hash),
357 BTREE_ITER_SLOTS, k, ret) {
358 if (!bkey_cmp(k.k->p, hash_k.k->p))
361 if (fsck_err_on(k.k->type == desc.key_type &&
362 !desc.cmp_bkey(k, hash_k), c,
363 "duplicate hash table keys:\n%s",
364 (bch2_bkey_val_to_text(&PBUF(buf), c,
366 ret = fsck_hash_delete_at(trans, desc, hash_info, k_iter);
373 if (bkey_deleted(k.k)) {
374 bch2_trans_iter_free(trans, iter);
379 bch2_trans_iter_free(trans, iter);
382 if (fsck_err(c, "hash table key at wrong offset: btree %u inode %llu offset %llu, "
383 "hashed to %llu\n%s",
384 desc.btree_id, hash_k.k->p.inode, hash_k.k->p.offset, hash,
385 (bch2_bkey_val_to_text(&PBUF(buf), c, hash_k), buf)) == FSCK_ERR_IGNORE)
388 ret = __bch2_trans_do(trans, NULL, NULL,
389 BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
390 hash_redo_key(trans, desc, hash_info, k_iter, hash_k));
392 bch_err(c, "hash_redo_key err %i", ret);
400 static int check_inode(struct btree_trans *trans,
401 struct btree_iter *iter,
402 struct bkey_s_c_inode inode)
404 struct bch_fs *c = trans->c;
405 struct bch_inode_unpacked u;
406 bool do_update = false;
409 ret = bch2_inode_unpack(inode, &u);
411 if (bch2_fs_inconsistent_on(ret, c,
412 "error unpacking inode %llu in fsck",
416 if (u.bi_flags & BCH_INODE_UNLINKED &&
418 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
420 bch_verbose(c, "deleting inode %llu", u.bi_inum);
422 bch2_trans_unlock(trans);
425 ret = bch2_inode_rm(c, u.bi_inum, false);
427 bch_err(c, "error in fsck: error %i while deleting inode", ret);
431 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
433 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
435 bch_verbose(c, "truncating inode %llu", u.bi_inum);
437 bch2_trans_unlock(trans);
441 * XXX: need to truncate partial blocks too here - or ideally
442 * just switch units to bytes and that issue goes away
444 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
445 POS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9),
446 POS(u.bi_inum, U64_MAX),
449 bch_err(c, "error in fsck: error %i truncating inode", ret);
454 * We truncated without our normal sector accounting hook, just
455 * make sure we recalculate it:
457 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
459 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
463 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
465 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
469 bch_verbose(c, "recounting sectors for inode %llu",
472 sectors = bch2_count_inode_sectors(trans, u.bi_inum);
474 bch_err(c, "error in fsck: error %i recounting inode sectors",
479 u.bi_sectors = sectors;
480 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
484 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
487 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
492 ret = __bch2_trans_do(trans, NULL, NULL,
494 BTREE_INSERT_LAZY_RW,
495 bch2_inode_write(trans, iter, &u));
497 bch_err(c, "error in fsck: error %i "
498 "updating inode", ret);
505 static int check_inodes(struct bch_fs *c, bool full)
507 struct btree_trans trans;
508 struct btree_iter *iter;
510 struct bkey_s_c_inode inode;
513 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
515 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
517 BTREE_ITER_PREFETCH, k, ret) {
518 if (k.k->type != KEY_TYPE_inode)
521 inode = bkey_s_c_to_inode(k);
524 (inode.v->bi_flags & (BCH_INODE_I_SIZE_DIRTY|
525 BCH_INODE_I_SECTORS_DIRTY|
526 BCH_INODE_UNLINKED))) {
527 ret = check_inode(&trans, iter, inode);
532 bch2_trans_iter_put(&trans, iter);
534 BUG_ON(ret == -EINTR);
536 return bch2_trans_exit(&trans) ?: ret;
539 static int fix_overlapping_extent(struct btree_trans *trans,
540 struct bkey_s_c k, struct bpos cut_at)
542 struct btree_iter *iter;
546 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
547 ret = PTR_ERR_OR_ZERO(u);
551 bkey_reassemble(u, k);
552 bch2_cut_front(cut_at, u);
556 * We don't want to go through the extent_handle_overwrites path:
558 * XXX: this is going to screw up disk accounting, extent triggers
559 * assume things about extent overwrites - we should be running the
560 * triggers manually here
562 iter = bch2_trans_get_iter(trans, BTREE_ID_extents, u->k.p,
563 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
565 BUG_ON(iter->flags & BTREE_ITER_IS_EXTENTS);
566 bch2_trans_update(trans, iter, u, BTREE_TRIGGER_NORUN);
567 bch2_trans_iter_put(trans, iter);
569 return bch2_trans_commit(trans, NULL, NULL,
571 BTREE_INSERT_LAZY_RW);
574 static int inode_backpointer_exists(struct btree_trans *trans,
575 struct bch_inode_unpacked *inode)
577 struct btree_iter *iter;
581 iter = bch2_trans_get_iter(trans, BTREE_ID_dirents,
582 POS(inode->bi_dir, inode->bi_dir_offset), 0);
583 k = bch2_btree_iter_peek_slot(iter);
587 if (k.k->type != KEY_TYPE_dirent)
590 ret = le64_to_cpu(bkey_s_c_to_dirent(k).v->d_inum) == inode->bi_inum;
592 bch2_trans_iter_free(trans, iter);
596 static bool inode_backpointer_matches(struct bkey_s_c_dirent d,
597 struct bch_inode_unpacked *inode)
599 return d.k->p.inode == inode->bi_dir &&
600 d.k->p.offset == inode->bi_dir_offset;
604 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
605 * that i_size an i_sectors are consistent
608 static int check_extents(struct bch_fs *c)
610 struct inode_walker w = inode_walker_init();
611 struct btree_trans trans;
612 struct btree_iter *iter;
614 struct bkey_buf prev;
618 bch2_bkey_buf_init(&prev);
619 prev.k->k = KEY(0, 0, 0);
620 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
622 bch_verbose(c, "checking extents");
624 iter = bch2_trans_get_iter(&trans, BTREE_ID_extents,
625 POS(BCACHEFS_ROOT_INO, 0),
627 BTREE_ITER_PREFETCH);
629 while ((k = bch2_btree_iter_peek(iter)).k &&
630 !(ret = bkey_err(k))) {
632 w.cur_inum != k.k->p.inode &&
633 !(w.inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY) &&
634 fsck_err_on(w.inode.bi_sectors != i_sectors, c,
635 "inode %llu has incorrect i_sectors: got %llu, should be %llu",
637 w.inode.bi_sectors, i_sectors)) {
638 w.inode.bi_sectors = i_sectors;
640 ret = write_inode(&trans, &w.inode, w.snapshot);
645 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
649 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
650 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
652 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2))
653 return fix_overlapping_extent(&trans, k, prev.k->k.p) ?: -EINTR;
656 ret = walk_inode(&trans, &w, k.k->p.inode);
660 if (w.first_this_inode)
663 if (fsck_err_on(!w.have_inode, c,
664 "extent type %u for missing inode %llu",
665 k.k->type, k.k->p.inode) ||
666 fsck_err_on(w.have_inode &&
667 !S_ISREG(w.inode.bi_mode) && !S_ISLNK(w.inode.bi_mode), c,
668 "extent type %u for non regular file, inode %llu mode %o",
669 k.k->type, k.k->p.inode, w.inode.bi_mode)) {
671 return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
672 POS(k.k->p.inode, 0),
673 POS(k.k->p.inode, U64_MAX),
677 if (fsck_err_on(w.have_inode &&
678 !(w.inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
679 k.k->type != KEY_TYPE_reservation &&
680 k.k->p.offset > round_up(w.inode.bi_size, block_bytes(c)) >> 9, c,
681 "extent type %u offset %llu past end of inode %llu, i_size %llu",
682 k.k->type, k.k->p.offset, k.k->p.inode, w.inode.bi_size)) {
684 return bch2_btree_delete_range_trans(&trans, BTREE_ID_extents,
685 POS(k.k->p.inode, round_up(w.inode.bi_size, block_bytes(c)) >> 9),
686 POS(k.k->p.inode, U64_MAX),
690 if (bkey_extent_is_allocation(k.k))
691 i_sectors += k.k->size;
692 bch2_bkey_buf_reassemble(&prev, c, k);
694 bch2_btree_iter_advance(iter);
699 bch2_trans_iter_put(&trans, iter);
700 bch2_bkey_buf_exit(&prev, c);
701 return bch2_trans_exit(&trans) ?: ret;
705 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
709 static int check_dirents(struct bch_fs *c)
711 struct inode_walker w = inode_walker_init();
712 struct bch_hash_info hash_info;
713 struct btree_trans trans;
714 struct btree_iter *iter;
717 unsigned nr_subdirs = 0;
720 bch_verbose(c, "checking dirents");
722 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
724 iter = bch2_trans_get_iter(&trans, BTREE_ID_dirents,
725 POS(BCACHEFS_ROOT_INO, 0),
727 BTREE_ITER_PREFETCH);
729 while ((k = bch2_btree_iter_peek(iter)).k &&
730 !(ret = bkey_err(k))) {
731 struct bkey_s_c_dirent d;
732 struct bch_inode_unpacked target;
735 bool backpointer_exists = true;
739 w.cur_inum != k.k->p.inode &&
740 fsck_err_on(w.inode.bi_nlink != nr_subdirs, c,
741 "directory %llu with wrong i_nlink: got %u, should be %u",
742 w.inode.bi_inum, w.inode.bi_nlink, nr_subdirs)) {
743 w.inode.bi_nlink = nr_subdirs;
744 ret = write_inode(&trans, &w.inode, w.snapshot);
749 ret = walk_inode(&trans, &w, k.k->p.inode);
753 if (w.first_this_inode)
756 if (fsck_err_on(!w.have_inode, c,
757 "dirent in nonexisting directory:\n%s",
758 (bch2_bkey_val_to_text(&PBUF(buf), c,
760 fsck_err_on(!S_ISDIR(w.inode.bi_mode), c,
761 "dirent in non directory inode type %u:\n%s",
762 mode_to_type(w.inode.bi_mode),
763 (bch2_bkey_val_to_text(&PBUF(buf), c,
765 ret = lockrestart_do(&trans,
766 bch2_btree_delete_at(&trans, iter, 0));
775 if (w.first_this_inode)
776 hash_info = bch2_hash_info_init(c, &w.inode);
778 ret = hash_check_key(&trans, bch2_dirent_hash_desc,
779 &hash_info, iter, k);
787 if (k.k->type != KEY_TYPE_dirent)
790 d = bkey_s_c_to_dirent(k);
791 d_inum = le64_to_cpu(d.v->d_inum);
793 ret = lookup_inode(&trans, d_inum, &target, &target_snapshot);
794 if (ret && ret != -ENOENT)
800 if (fsck_err_on(!have_target, c,
801 "dirent points to missing inode:\n%s",
802 (bch2_bkey_val_to_text(&PBUF(buf), c,
804 ret = remove_dirent(&trans, d.k->p);
813 if (!target.bi_dir &&
814 !target.bi_dir_offset) {
815 target.bi_dir = k.k->p.inode;
816 target.bi_dir_offset = k.k->p.offset;
818 ret = write_inode(&trans, &target, target_snapshot);
823 if (!inode_backpointer_matches(d, &target)) {
824 ret = inode_backpointer_exists(&trans, &target);
828 backpointer_exists = ret;
831 if (fsck_err_on(S_ISDIR(target.bi_mode) &&
832 backpointer_exists, c,
833 "directory %llu with multiple links",
835 ret = remove_dirent(&trans, d.k->p);
841 if (fsck_err_on(backpointer_exists &&
843 "inode %llu has multiple links but i_nlink 0",
846 target.bi_flags &= ~BCH_INODE_UNLINKED;
848 ret = write_inode(&trans, &target, target_snapshot);
853 if (fsck_err_on(!backpointer_exists, c,
854 "inode %llu has wrong backpointer:\n"
856 "should be %llu:%llu",
859 target.bi_dir_offset,
862 target.bi_dir = k.k->p.inode;
863 target.bi_dir_offset = k.k->p.offset;
865 ret = write_inode(&trans, &target, target_snapshot);
871 if (fsck_err_on(d.v->d_type != mode_to_type(target.bi_mode), c,
872 "incorrect d_type: should be %u:\n%s",
873 mode_to_type(target.bi_mode),
874 (bch2_bkey_val_to_text(&PBUF(buf), c,
876 struct bkey_i_dirent *n;
878 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
884 bkey_reassemble(&n->k_i, d.s_c);
885 n->v.d_type = mode_to_type(target.bi_mode);
887 ret = __bch2_trans_do(&trans, NULL, NULL,
889 BTREE_INSERT_LAZY_RW,
890 (bch2_trans_update(&trans, iter, &n->k_i, 0), 0));
897 nr_subdirs += d.v->d_type == DT_DIR;
899 bch2_btree_iter_advance(iter);
906 bch2_trans_iter_put(&trans, iter);
907 return bch2_trans_exit(&trans) ?: ret;
911 * Walk xattrs: verify that they all have a corresponding inode
914 static int check_xattrs(struct bch_fs *c)
916 struct inode_walker w = inode_walker_init();
917 struct bch_hash_info hash_info;
918 struct btree_trans trans;
919 struct btree_iter *iter;
923 bch_verbose(c, "checking xattrs");
925 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
927 iter = bch2_trans_get_iter(&trans, BTREE_ID_xattrs,
928 POS(BCACHEFS_ROOT_INO, 0),
930 BTREE_ITER_PREFETCH);
932 while ((k = bch2_btree_iter_peek(iter)).k &&
933 !(ret = bkey_err(k))) {
934 ret = walk_inode(&trans, &w, k.k->p.inode);
938 if (fsck_err_on(!w.have_inode, c,
939 "xattr for missing inode %llu",
941 ret = bch2_btree_delete_at(&trans, iter, 0);
947 if (w.first_this_inode && w.have_inode)
948 hash_info = bch2_hash_info_init(c, &w.inode);
950 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
951 &hash_info, iter, k);
955 bch2_btree_iter_advance(iter);
961 bch2_trans_iter_put(&trans, iter);
962 return bch2_trans_exit(&trans) ?: ret;
965 /* Get root directory, create if it doesn't exist: */
966 static int check_root(struct bch_fs *c, struct bch_inode_unpacked *root_inode)
968 struct bkey_inode_buf packed;
972 bch_verbose(c, "checking root directory");
974 ret = bch2_trans_do(c, NULL, NULL, 0,
975 lookup_inode(&trans, BCACHEFS_ROOT_INO, root_inode, &snapshot));
976 if (ret && ret != -ENOENT)
979 if (fsck_err_on(ret, c, "root directory missing"))
982 if (fsck_err_on(!S_ISDIR(root_inode->bi_mode), c,
983 "root inode not a directory"))
990 bch2_inode_init(c, root_inode, 0, 0, S_IFDIR|0755,
992 root_inode->bi_inum = BCACHEFS_ROOT_INO;
994 bch2_inode_pack(c, &packed, root_inode);
996 return bch2_btree_insert(c, BTREE_ID_inodes, &packed.inode.k_i,
999 BTREE_INSERT_LAZY_RW);
1006 struct pathbuf_entry {
1011 static int path_down(struct pathbuf *p, u64 inum)
1013 if (p->nr == p->size) {
1014 size_t new_size = max_t(size_t, 256UL, p->size * 2);
1015 void *n = krealloc(p->entries,
1016 new_size * sizeof(p->entries[0]),
1026 p->entries[p->nr++] = (struct pathbuf_entry) {
1032 static int check_path(struct btree_trans *trans,
1034 struct bch_inode_unpacked *inode)
1036 struct bch_fs *c = trans->c;
1043 while (inode->bi_inum != BCACHEFS_ROOT_INO) {
1044 ret = lockrestart_do(trans,
1045 inode_backpointer_exists(trans, inode));
1050 if (fsck_err(c, "unreachable inode %llu, type %u nlink %u backptr %llu:%llu",
1052 mode_to_type(inode->bi_mode),
1055 inode->bi_dir_offset))
1056 ret = reattach_inode(trans, inode);
1061 if (!S_ISDIR(inode->bi_mode))
1064 ret = path_down(p, inode->bi_inum);
1066 bch_err(c, "memory allocation failure");
1070 for (i = 0; i < p->nr; i++) {
1071 if (inode->bi_dir != p->entries[i].inum)
1074 /* XXX print path */
1075 if (!fsck_err(c, "directory structure loop"))
1078 ret = lockrestart_do(trans,
1079 remove_backpointer(trans, inode));
1081 bch_err(c, "error removing dirent: %i", ret);
1085 ret = reattach_inode(trans, inode);
1089 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1091 /* Should have been caught in dirents pass */
1092 bch_err(c, "error looking up parent directory: %i", ret);
1098 bch_err(c, "%s: err %i", __func__, ret);
1103 * Check for unreachable inodes, as well as loops in the directory structure:
1104 * After check_dirents(), if an inode backpointer doesn't exist that means it's
1107 static int check_directory_structure(struct bch_fs *c)
1109 struct btree_trans trans;
1110 struct btree_iter *iter;
1112 struct bch_inode_unpacked u;
1113 struct pathbuf path = { 0, 0, NULL };
1116 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1118 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
1120 BTREE_ITER_PREFETCH, k, ret) {
1121 if (k.k->type != KEY_TYPE_inode)
1124 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), &u);
1126 /* Should have been caught earlier in fsck: */
1127 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
1131 ret = check_path(&trans, &path, &u);
1135 bch2_trans_iter_put(&trans, iter);
1137 BUG_ON(ret == -EINTR);
1139 kfree(path.entries);
1141 return bch2_trans_exit(&trans) ?: ret;
1144 struct nlink_table {
1155 static int add_nlink(struct nlink_table *t, u64 inum, u32 snapshot)
1157 if (t->nr == t->size) {
1158 size_t new_size = max_t(size_t, 128UL, t->size * 2);
1159 void *d = kvmalloc(new_size * sizeof(t->d[0]), GFP_KERNEL);
1165 memcpy(d, t->d, t->size * sizeof(t->d[0]));
1173 t->d[t->nr++] = (struct nlink) {
1175 .snapshot = snapshot,
1181 static int nlink_cmp(const void *_l, const void *_r)
1183 const struct nlink *l = _l;
1184 const struct nlink *r = _r;
1186 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
1189 static void inc_link(struct bch_fs *c, struct nlink_table *links,
1190 u64 range_start, u64 range_end, u64 inum)
1192 struct nlink *link, key = {
1193 .inum = inum, .snapshot = U32_MAX,
1196 if (inum < range_start || inum >= range_end)
1199 link = __inline_bsearch(&key, links->d, links->nr,
1200 sizeof(links->d[0]), nlink_cmp);
1206 static int check_nlinks_find_hardlinks(struct bch_fs *c,
1207 struct nlink_table *t,
1208 u64 start, u64 *end)
1210 struct btree_trans trans;
1211 struct btree_iter *iter;
1213 struct bkey_s_c_inode inode;
1214 struct bch_inode_unpacked u;
1217 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1219 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
1222 BTREE_ITER_PREFETCH, k, ret) {
1223 if (k.k->type != KEY_TYPE_inode)
1226 inode = bkey_s_c_to_inode(k);
1229 * Backpointer and directory structure checks are sufficient for
1230 * directories, since they can't have hardlinks:
1232 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
1235 /* Should never fail, checked by bch2_inode_invalid: */
1236 BUG_ON(bch2_inode_unpack(inode, &u));
1241 ret = add_nlink(t, k.k->p.offset, k.k->p.snapshot);
1243 *end = k.k->p.offset;
1249 bch2_trans_iter_put(&trans, iter);
1250 bch2_trans_exit(&trans);
1253 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
1259 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
1260 u64 range_start, u64 range_end)
1262 struct btree_trans trans;
1263 struct btree_iter *iter;
1265 struct bkey_s_c_dirent d;
1268 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1270 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
1272 BTREE_ITER_PREFETCH, k, ret) {
1273 switch (k.k->type) {
1274 case KEY_TYPE_dirent:
1275 d = bkey_s_c_to_dirent(k);
1277 if (d.v->d_type != DT_DIR)
1278 inc_link(c, links, range_start, range_end,
1279 le64_to_cpu(d.v->d_inum));
1283 bch2_trans_cond_resched(&trans);
1285 bch2_trans_iter_put(&trans, iter);
1287 ret = bch2_trans_exit(&trans) ?: ret;
1289 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
1295 static int check_nlinks_update_hardlinks(struct bch_fs *c,
1296 struct nlink_table *links,
1297 u64 range_start, u64 range_end)
1299 struct btree_trans trans;
1300 struct btree_iter *iter;
1302 struct bkey_s_c_inode inode;
1303 struct bch_inode_unpacked u;
1304 struct nlink *link = links->d;
1307 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1309 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
1310 POS(0, range_start),
1312 BTREE_ITER_PREFETCH, k, ret) {
1313 if (k.k->p.offset >= range_end)
1316 if (k.k->type != KEY_TYPE_inode)
1319 inode = bkey_s_c_to_inode(k);
1320 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
1323 BUG_ON(bch2_inode_unpack(inode, &u));
1328 while (link->inum < k.k->p.offset) {
1330 BUG_ON(link >= links->d + links->nr);
1333 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
1334 "inode %llu has wrong i_nlink (type %u i_nlink %u, should be %u)",
1335 u.bi_inum, mode_to_type(u.bi_mode),
1336 bch2_inode_nlink_get(&u), link->count)) {
1337 bch2_inode_nlink_set(&u, link->count);
1339 ret = __bch2_trans_do(&trans, NULL, NULL,
1340 BTREE_INSERT_NOFAIL|
1341 BTREE_INSERT_LAZY_RW,
1342 bch2_inode_write(&trans, iter, &u));
1344 bch_err(c, "error in fsck: error %i updating inode", ret);
1348 bch2_trans_iter_put(&trans, iter);
1349 bch2_trans_exit(&trans);
1352 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
1358 static int check_nlinks(struct bch_fs *c)
1360 struct nlink_table links = { 0 };
1361 u64 this_iter_range_start, next_iter_range_start = 0;
1364 bch_verbose(c, "checking inode nlinks");
1367 this_iter_range_start = next_iter_range_start;
1368 next_iter_range_start = U64_MAX;
1370 ret = check_nlinks_find_hardlinks(c, &links,
1371 this_iter_range_start,
1372 &next_iter_range_start);
1374 ret = check_nlinks_walk_dirents(c, &links,
1375 this_iter_range_start,
1376 next_iter_range_start);
1380 ret = check_nlinks_update_hardlinks(c, &links,
1381 this_iter_range_start,
1382 next_iter_range_start);
1387 } while (next_iter_range_start != U64_MAX);
1395 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
1396 * Doesn't fix them yet, mainly because they haven't yet been observed:
1398 int bch2_fsck_full(struct bch_fs *c)
1400 struct bch_inode_unpacked root_inode;
1402 return check_inodes(c, true) ?:
1406 check_root(c, &root_inode) ?:
1407 check_directory_structure(c) ?:
1411 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
1413 return check_inodes(c, false);