1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
12 #include "subvolume.h"
16 #include <linux/bsearch.h>
17 #include <linux/dcache.h> /* struct qstr */
19 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
21 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
24 struct btree_iter iter;
29 for_each_btree_key(trans, iter, BTREE_ID_extents,
30 SPOS(inum, 0, snapshot), 0, k, ret) {
31 if (k.k->p.inode != inum)
34 if (bkey_extent_is_allocation(k.k))
38 bch2_trans_iter_exit(trans, &iter);
40 return ret ?: sectors;
43 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
46 struct btree_iter iter;
48 struct bkey_s_c_dirent d;
52 for_each_btree_key(trans, iter, BTREE_ID_dirents,
53 SPOS(inum, 0, snapshot), 0, k, ret) {
54 if (k.k->p.inode != inum)
57 if (k.k->type != KEY_TYPE_dirent)
60 d = bkey_s_c_to_dirent(k);
61 if (d.v->d_type == DT_DIR)
65 bch2_trans_iter_exit(trans, &iter);
67 return ret ?: subdirs;
70 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
73 struct btree_iter iter;
77 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
79 k = bch2_btree_iter_peek_slot(&iter);
84 if (k.k->type != KEY_TYPE_snapshot) {
85 bch_err(trans->c, "snapshot %u not fonud", snapshot);
90 *subvol = le32_to_cpu(bkey_s_c_to_snapshot(k).v->subvol);
92 bch2_trans_iter_exit(trans, &iter);
97 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
98 u32 *snapshot, u64 *inum)
100 struct bch_subvolume s;
103 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
105 *snapshot = le32_to_cpu(s.snapshot);
106 *inum = le64_to_cpu(s.inode);
110 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
111 u32 *snapshot, u64 *inum)
113 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
116 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
117 struct bch_inode_unpacked *inode,
120 struct btree_iter iter;
124 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
125 SPOS(0, inode_nr, *snapshot), 0);
126 k = bch2_btree_iter_peek_slot(&iter);
131 ret = k.k->type == KEY_TYPE_inode
132 ? bch2_inode_unpack(bkey_s_c_to_inode(k), inode)
135 *snapshot = iter.pos.snapshot;
137 if (ret && ret != -EINTR)
138 bch_err(trans->c, "error %i fetching inode %llu:%u",
139 ret, inode_nr, *snapshot);
140 bch2_trans_iter_exit(trans, &iter);
144 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
145 struct bch_inode_unpacked *inode,
148 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
151 static int __lookup_dirent(struct btree_trans *trans,
152 struct bch_hash_info hash_info,
153 subvol_inum dir, struct qstr *name,
154 u64 *target, unsigned *type)
156 struct btree_iter iter;
157 struct bkey_s_c_dirent d;
160 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
161 &hash_info, dir, name, 0);
165 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
166 *target = le64_to_cpu(d.v->d_inum);
168 bch2_trans_iter_exit(trans, &iter);
172 static int __write_inode(struct btree_trans *trans,
173 struct bch_inode_unpacked *inode,
176 struct btree_iter iter;
179 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
180 SPOS(0, inode->bi_inum, snapshot),
183 ret = bch2_btree_iter_traverse(&iter) ?:
184 bch2_inode_write(trans, &iter, inode);
185 bch2_trans_iter_exit(trans, &iter);
189 static int write_inode(struct btree_trans *trans,
190 struct bch_inode_unpacked *inode,
193 int ret = __bch2_trans_do(trans, NULL, NULL,
195 BTREE_INSERT_LAZY_RW,
196 __write_inode(trans, inode, snapshot));
198 bch_err(trans->c, "error in fsck: error %i updating inode", ret);
202 static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
204 struct btree_iter iter = { NULL };
205 struct bkey_i_inode_generation delete;
206 struct bch_inode_unpacked inode_u;
210 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
211 SPOS(inum, 0, snapshot),
212 SPOS(inum, U64_MAX, snapshot),
214 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
215 SPOS(inum, 0, snapshot),
216 SPOS(inum, U64_MAX, snapshot),
218 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
219 SPOS(inum, 0, snapshot),
220 SPOS(inum, U64_MAX, snapshot),
225 bch2_trans_begin(trans);
227 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
228 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
229 k = bch2_btree_iter_peek_slot(&iter);
235 if (k.k->type != KEY_TYPE_inode) {
236 bch2_fs_inconsistent(trans->c,
237 "inode %llu:%u not found when deleting",
243 bch2_inode_unpack(bkey_s_c_to_inode(k), &inode_u);
245 /* Subvolume root? */
246 if (inode_u.bi_subvol) {
247 ret = bch2_subvolume_delete(trans, inode_u.bi_subvol);
252 bkey_inode_generation_init(&delete.k_i);
253 delete.k.p = iter.pos;
254 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
256 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
257 bch2_trans_commit(trans, NULL, NULL,
258 BTREE_INSERT_NOFAIL);
260 bch2_trans_iter_exit(trans, &iter);
267 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
269 struct bch_fs *c = trans->c;
270 struct btree_iter iter;
271 struct bch_inode_unpacked dir_inode;
272 struct bch_hash_info dir_hash_info;
275 ret = __lookup_inode(trans, pos.inode, &dir_inode, NULL);
279 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
281 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
283 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
284 &dir_hash_info, &iter, 0);
285 bch2_trans_iter_exit(trans, &iter);
289 /* Get lost+found, create if it doesn't exist: */
290 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
291 struct bch_inode_unpacked *lostfound)
293 struct bch_fs *c = trans->c;
294 struct bch_inode_unpacked root;
295 struct bch_hash_info root_hash_info;
296 struct qstr lostfound_str = QSTR("lost+found");
297 subvol_inum root_inum = { .subvol = subvol };
303 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
307 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
311 root_hash_info = bch2_hash_info_init(c, &root);
313 ret = __lookup_dirent(trans, root_hash_info, root_inum,
314 &lostfound_str, &inum, &d_type);
315 if (ret == -ENOENT) {
316 bch_notice(c, "creating lost+found");
317 goto create_lostfound;
320 if (ret && ret != -EINTR)
321 bch_err(c, "error looking up lost+found: %i", ret);
325 if (d_type != DT_DIR) {
326 bch_err(c, "error looking up lost+found: not a directory");
331 * The check_dirents pass has already run, dangling dirents
332 * shouldn't exist here:
334 return __lookup_inode(trans, inum, lostfound, &snapshot);
337 bch2_inode_init_early(c, lostfound);
339 ret = bch2_create_trans(trans, root_inum, &root,
340 lostfound, &lostfound_str,
341 0, 0, S_IFDIR|0700, 0, NULL, NULL,
342 (subvol_inum) { }, 0);
343 if (ret && ret != -EINTR)
344 bch_err(c, "error creating lost+found: %i", ret);
348 static int __reattach_inode(struct btree_trans *trans,
349 struct bch_inode_unpacked *inode,
352 struct bch_hash_info dir_hash;
353 struct bch_inode_unpacked lostfound;
360 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
364 ret = lookup_lostfound(trans, subvol, &lostfound);
368 if (S_ISDIR(inode->bi_mode)) {
369 lostfound.bi_nlink++;
371 ret = __write_inode(trans, &lostfound, U32_MAX);
376 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
378 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
379 name = (struct qstr) QSTR(name_buf);
381 ret = bch2_dirent_create(trans,
384 .inum = lostfound.bi_inum,
388 &name, inode->bi_inum, &dir_offset,
389 BCH_HASH_SET_MUST_CREATE);
393 inode->bi_dir = lostfound.bi_inum;
394 inode->bi_dir_offset = dir_offset;
396 return __write_inode(trans, inode, inode_snapshot);
399 static int reattach_inode(struct btree_trans *trans,
400 struct bch_inode_unpacked *inode,
403 int ret = __bch2_trans_do(trans, NULL, NULL,
404 BTREE_INSERT_LAZY_RW|
406 __reattach_inode(trans, inode, inode_snapshot));
408 bch_err(trans->c, "error %i reattaching inode %llu",
409 ret, inode->bi_inum);
416 static int remove_backpointer(struct btree_trans *trans,
417 struct bch_inode_unpacked *inode)
419 struct btree_iter iter;
423 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
424 POS(inode->bi_dir, inode->bi_dir_offset), 0);
425 k = bch2_btree_iter_peek_slot(&iter);
429 if (k.k->type != KEY_TYPE_dirent) {
434 ret = __remove_dirent(trans, k.k->p);
436 bch2_trans_iter_exit(trans, &iter);
440 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s, struct bpos pos)
442 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
444 if (bkey_cmp(s->pos, pos))
448 /* Might get called multiple times due to lock restarts */
449 if (s->nr && s->d[s->nr - 1] == pos.snapshot)
452 return snapshots_seen_add(c, s, pos.snapshot);
456 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
457 * and @ancestor hasn't been overwritten in @seen
459 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
461 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
462 u32 id, u32 ancestor)
466 BUG_ON(id > ancestor);
468 id = snapshot_t(c, id)->equiv;
469 ancestor = snapshot_t(c, ancestor)->equiv;
471 /* @ancestor should be the snapshot most recently added to @seen */
472 BUG_ON(!seen->nr || seen->d[seen->nr - 1] != ancestor);
473 BUG_ON(seen->pos.snapshot != ancestor);
478 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
481 for (i = seen->nr - 2;
482 i >= 0 && seen->d[i] >= id;
484 if (bch2_snapshot_is_ancestor(c, id, seen->d[i]) &&
485 bch2_snapshot_is_ancestor(c, seen->d[i], ancestor))
492 * ref_visible - given a key with snapshot id @src that points to a key with
493 * snapshot id @dst, test whether there is some snapshot in which @dst is
496 * This assumes we're visiting @src keys in natural key order.
498 * @s - list of snapshot IDs already seen at @src
499 * @src - snapshot ID of src key
500 * @dst - snapshot ID of dst key
502 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
506 ? key_visible_in_snapshot(c, s, dst, src)
507 : bch2_snapshot_is_ancestor(c, src, dst);
510 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
511 for (_i = (_w)->d; _i < (_w)->d + (_w)->nr && (_i)->snapshot <= (_snapshot); _i++)\
512 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
514 struct inode_walker {
515 bool first_this_inode;
520 struct inode_walker_entry {
521 struct bch_inode_unpacked inode;
527 static void inode_walker_exit(struct inode_walker *w)
533 static struct inode_walker inode_walker_init(void)
535 return (struct inode_walker) { 0, };
538 static int inode_walker_realloc(struct inode_walker *w)
540 if (w->nr == w->size) {
541 size_t new_size = max_t(size_t, 8UL, w->size * 2);
542 void *d = krealloc(w->d, new_size * sizeof(w->d[0]),
554 static int add_inode(struct bch_fs *c, struct inode_walker *w,
555 struct bkey_s_c_inode inode)
557 struct bch_inode_unpacked u;
560 ret = inode_walker_realloc(w);
564 BUG_ON(bch2_inode_unpack(inode, &u));
566 w->d[w->nr++] = (struct inode_walker_entry) {
568 .snapshot = snapshot_t(c, inode.k->p.snapshot)->equiv,
574 static int __walk_inode(struct btree_trans *trans,
575 struct inode_walker *w, struct bpos pos)
577 struct bch_fs *c = trans->c;
578 struct btree_iter iter;
580 unsigned i, ancestor_pos;
583 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
585 if (pos.inode == w->cur_inum) {
586 w->first_this_inode = false;
587 goto lookup_snapshot;
592 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, pos.inode),
593 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
594 if (k.k->p.offset != pos.inode)
597 if (k.k->type == KEY_TYPE_inode)
598 add_inode(c, w, bkey_s_c_to_inode(k));
600 bch2_trans_iter_exit(trans, &iter);
605 w->cur_inum = pos.inode;
606 w->first_this_inode = true;
608 for (i = 0; i < w->nr; i++)
609 if (bch2_snapshot_is_ancestor(c, pos.snapshot, w->d[i].snapshot))
613 BUG_ON(pos.snapshot > w->d[i].snapshot);
615 if (pos.snapshot != w->d[i].snapshot) {
618 while (i && w->d[i - 1].snapshot > pos.snapshot)
621 ret = inode_walker_realloc(w);
625 array_insert_item(w->d, w->nr, i, w->d[ancestor_pos]);
626 w->d[i].snapshot = pos.snapshot;
633 static int __get_visible_inodes(struct btree_trans *trans,
634 struct inode_walker *w,
635 struct snapshots_seen *s,
638 struct bch_fs *c = trans->c;
639 struct btree_iter iter;
645 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
646 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
647 if (k.k->p.offset != inum)
650 if (k.k->type != KEY_TYPE_inode)
653 if (ref_visible(c, s, s->pos.snapshot, k.k->p.snapshot)) {
654 add_inode(c, w, bkey_s_c_to_inode(k));
655 if (k.k->p.snapshot >= s->pos.snapshot)
659 bch2_trans_iter_exit(trans, &iter);
664 static int check_key_has_snapshot(struct btree_trans *trans,
665 struct btree_iter *iter,
668 struct bch_fs *c = trans->c;
672 if (fsck_err_on(!snapshot_t(c, k.k->p.snapshot)->equiv, c,
673 "key in missing snapshot: %s",
674 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
675 return bch2_btree_delete_at(trans, iter,
676 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
681 static int hash_redo_key(struct btree_trans *trans,
682 const struct bch_hash_desc desc,
683 struct bch_hash_info *hash_info,
684 struct btree_iter *k_iter, struct bkey_s_c k)
686 bch_err(trans->c, "hash_redo_key() not implemented yet");
689 struct bkey_i *delete;
692 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
694 return PTR_ERR(delete);
696 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
700 bkey_reassemble(tmp, k);
702 bkey_init(&delete->k);
703 delete->k.p = k_iter->pos;
704 return bch2_btree_iter_traverse(k_iter) ?:
705 bch2_trans_update(trans, k_iter, delete, 0) ?:
706 bch2_hash_set(trans, desc, hash_info, k_iter->pos.inode, tmp, 0);
710 static int hash_check_key(struct btree_trans *trans,
711 const struct bch_hash_desc desc,
712 struct bch_hash_info *hash_info,
713 struct btree_iter *k_iter, struct bkey_s_c hash_k)
715 struct bch_fs *c = trans->c;
716 struct btree_iter iter = { NULL };
722 if (hash_k.k->type != desc.key_type)
725 hash = desc.hash_bkey(hash_info, hash_k);
727 if (likely(hash == hash_k.k->p.offset))
730 if (hash_k.k->p.offset < hash)
733 for_each_btree_key(trans, iter, desc.btree_id, POS(hash_k.k->p.inode, hash),
734 BTREE_ITER_SLOTS, k, ret) {
735 if (!bkey_cmp(k.k->p, hash_k.k->p))
738 if (fsck_err_on(k.k->type == desc.key_type &&
739 !desc.cmp_bkey(k, hash_k), c,
740 "duplicate hash table keys:\n%s",
741 (bch2_bkey_val_to_text(&PBUF(buf), c,
743 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
747 if (bkey_deleted(k.k)) {
748 bch2_trans_iter_exit(trans, &iter);
753 bch2_trans_iter_exit(trans, &iter);
756 if (fsck_err(c, "hash table key at wrong offset: btree %u inode %llu offset %llu, "
757 "hashed to %llu\n%s",
758 desc.btree_id, hash_k.k->p.inode, hash_k.k->p.offset, hash,
759 (bch2_bkey_val_to_text(&PBUF(buf), c, hash_k), buf)) == FSCK_ERR_IGNORE)
762 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
764 bch_err(c, "hash_redo_key err %i", ret);
772 static int check_inode(struct btree_trans *trans,
773 struct btree_iter *iter,
774 struct bch_inode_unpacked *prev,
777 struct bch_fs *c = trans->c;
779 struct bkey_s_c_inode inode;
780 struct bch_inode_unpacked u;
781 bool do_update = false;
784 k = bch2_btree_iter_peek(iter);
792 ret = check_key_has_snapshot(trans, iter, k);
794 return ret < 0 ? ret : 0;
797 * if snapshot id isn't a leaf node, skip it - deletion in
798 * particular is not atomic, so on the internal snapshot nodes
799 * we can see inodes marked for deletion after a clean shutdown
801 if (bch2_snapshot_internal_node(c, k.k->p.snapshot))
804 if (k.k->type != KEY_TYPE_inode)
807 inode = bkey_s_c_to_inode(k);
810 !(inode.v->bi_flags & (BCH_INODE_I_SIZE_DIRTY|
811 BCH_INODE_I_SECTORS_DIRTY|
812 BCH_INODE_UNLINKED)))
815 BUG_ON(bch2_inode_unpack(inode, &u));
817 if (prev->bi_inum != u.bi_inum)
820 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
821 inode_d_type(prev) != inode_d_type(&u), c,
822 "inodes in different snapshots don't match")) {
823 bch_err(c, "repair not implemented yet");
827 if (u.bi_flags & BCH_INODE_UNLINKED &&
829 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
831 bch2_trans_unlock(trans);
834 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
836 bch_err(c, "error in fsck: error %i while deleting inode", ret);
840 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
842 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
844 bch_verbose(c, "truncating inode %llu", u.bi_inum);
846 bch2_trans_unlock(trans);
850 * XXX: need to truncate partial blocks too here - or ideally
851 * just switch units to bytes and that issue goes away
853 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
854 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
856 POS(u.bi_inum, U64_MAX),
859 bch_err(c, "error in fsck: error %i truncating inode", ret);
864 * We truncated without our normal sector accounting hook, just
865 * make sure we recalculate it:
867 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
869 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
873 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
875 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
879 bch_verbose(c, "recounting sectors for inode %llu",
882 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
884 bch_err(c, "error in fsck: error %i recounting inode sectors",
889 u.bi_sectors = sectors;
890 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
894 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
897 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
902 ret = write_inode(trans, &u, iter->pos.snapshot);
904 bch_err(c, "error in fsck: error %i "
905 "updating inode", ret);
912 static int check_inodes(struct bch_fs *c, bool full)
914 struct btree_trans trans;
915 struct btree_iter iter;
916 struct bch_inode_unpacked prev = { 0 };
919 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
921 bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes,
922 POS(BCACHEFS_ROOT_INO, 0),
925 BTREE_ITER_ALL_SNAPSHOTS);
928 ret = __bch2_trans_do(&trans, NULL, NULL,
929 BTREE_INSERT_LAZY_RW|
931 check_inode(&trans, &iter, &prev, full));
934 } while (bch2_btree_iter_advance(&iter));
935 bch2_trans_iter_exit(&trans, &iter);
937 bch2_trans_exit(&trans);
941 static int check_subvol(struct btree_trans *trans,
942 struct btree_iter *iter)
945 struct bkey_s_c_subvolume subvol;
948 k = bch2_btree_iter_peek(iter);
956 if (k.k->type != KEY_TYPE_subvolume)
959 subvol = bkey_s_c_to_subvolume(k);
961 if (BCH_SUBVOLUME_UNLINKED(subvol.v)) {
962 ret = bch2_subvolume_delete(trans, iter->pos.offset);
963 if (ret && ret != -EINTR)
964 bch_err(trans->c, "error deleting subvolume %llu: %i",
965 iter->pos.offset, ret);
974 static int check_subvols(struct bch_fs *c)
976 struct btree_trans trans;
977 struct btree_iter iter;
980 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
982 bch2_trans_iter_init(&trans, &iter, BTREE_ID_subvolumes,
985 BTREE_ITER_PREFETCH);
988 ret = __bch2_trans_do(&trans, NULL, NULL,
989 BTREE_INSERT_LAZY_RW|
991 check_subvol(&trans, &iter));
994 } while (bch2_btree_iter_advance(&iter));
995 bch2_trans_iter_exit(&trans, &iter);
997 bch2_trans_exit(&trans);
1002 * Checking for overlapping extents needs to be reimplemented
1005 static int fix_overlapping_extent(struct btree_trans *trans,
1006 struct bkey_s_c k, struct bpos cut_at)
1008 struct btree_iter iter;
1012 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1013 ret = PTR_ERR_OR_ZERO(u);
1017 bkey_reassemble(u, k);
1018 bch2_cut_front(cut_at, u);
1022 * We don't want to go through the extent_handle_overwrites path:
1024 * XXX: this is going to screw up disk accounting, extent triggers
1025 * assume things about extent overwrites - we should be running the
1026 * triggers manually here
1028 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, u->k.p,
1029 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
1031 BUG_ON(iter.flags & BTREE_ITER_IS_EXTENTS);
1032 ret = bch2_btree_iter_traverse(&iter) ?:
1033 bch2_trans_update(trans, &iter, u, BTREE_TRIGGER_NORUN) ?:
1034 bch2_trans_commit(trans, NULL, NULL,
1035 BTREE_INSERT_NOFAIL|
1036 BTREE_INSERT_LAZY_RW);
1037 bch2_trans_iter_exit(trans, &iter);
1042 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1043 struct btree_iter *iter,
1049 bch2_trans_iter_init(trans, iter, BTREE_ID_dirents, pos, 0);
1050 k = bch2_btree_iter_peek_slot(iter);
1052 if (!ret && k.k->type != KEY_TYPE_dirent)
1055 bch2_trans_iter_exit(trans, iter);
1056 return (struct bkey_s_c_dirent) { .k = ERR_PTR(ret) };
1059 return bkey_s_c_to_dirent(k);
1062 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1063 struct bkey_s_c_dirent d)
1065 return inode->bi_dir == d.k->p.inode &&
1066 inode->bi_dir_offset == d.k->p.offset;
1069 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1070 struct bch_inode_unpacked *inode)
1072 return d.v->d_type == DT_SUBVOL
1073 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1074 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1077 static int inode_backpointer_exists(struct btree_trans *trans,
1078 struct bch_inode_unpacked *inode,
1081 struct btree_iter iter;
1082 struct bkey_s_c_dirent d;
1085 d = dirent_get_by_pos(trans, &iter,
1086 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1087 ret = bkey_err(d.s_c);
1091 ret = dirent_points_to_inode(d, inode);
1092 bch2_trans_iter_exit(trans, &iter);
1096 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1098 struct bch_fs *c = trans->c;
1099 struct inode_walker_entry *i;
1100 int ret = 0, ret2 = 0;
1103 for (i = w->d; i < w->d + w->nr; i++) {
1104 if (i->inode.bi_sectors == i->count)
1107 count2 = lockrestart_do(trans,
1108 bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot));
1110 if (i->count != count2) {
1111 bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1114 if (i->inode.bi_sectors == i->count)
1118 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1119 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1120 w->cur_inum, i->snapshot,
1121 i->inode.bi_sectors, i->count) == FSCK_ERR_IGNORE)
1124 i->inode.bi_sectors = i->count;
1125 ret = write_inode(trans, &i->inode, i->snapshot);
1134 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1135 struct inode_walker *inode,
1136 struct snapshots_seen *s)
1138 struct bch_fs *c = trans->c;
1140 struct inode_walker_entry *i;
1144 k = bch2_btree_iter_peek(iter);
1152 ret = check_key_has_snapshot(trans, iter, k);
1154 return ret < 0 ? ret : 0;
1156 ret = snapshots_seen_update(c, s, k.k->p);
1160 if (k.k->type == KEY_TYPE_whiteout)
1163 if (inode->cur_inum != k.k->p.inode) {
1164 ret = check_i_sectors(trans, inode);
1169 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
1173 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
1174 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
1176 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2))
1177 return fix_overlapping_extent(trans, k, prev.k->k.p) ?: -EINTR;
1180 ret = __walk_inode(trans, inode, k.k->p);
1184 if (fsck_err_on(ret == INT_MAX, c,
1185 "extent in missing inode:\n %s",
1186 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1187 return bch2_btree_delete_at(trans, iter,
1188 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1196 if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1197 !S_ISLNK(i->inode.bi_mode), c,
1198 "extent in non regular inode mode %o:\n %s",
1200 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1201 return bch2_btree_delete_at(trans, iter,
1202 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1204 if (!bch2_snapshot_internal_node(c, k.k->p.snapshot)) {
1205 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i) {
1206 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1207 k.k->type != KEY_TYPE_reservation &&
1208 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9, c,
1209 "extent type %u offset %llu past end of inode %llu, i_size %llu",
1210 k.k->type, k.k->p.offset, k.k->p.inode, i->inode.bi_size)) {
1212 return bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1213 SPOS(k.k->p.inode, round_up(i->inode.bi_size, block_bytes(c)) >> 9,
1215 POS(k.k->p.inode, U64_MAX),
1221 if (bkey_extent_is_allocation(k.k))
1222 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i)
1223 i->count += k.k->size;
1225 bch2_bkey_buf_reassemble(&prev, c, k);
1233 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1234 * that i_size an i_sectors are consistent
1237 static int check_extents(struct bch_fs *c)
1239 struct inode_walker w = inode_walker_init();
1240 struct snapshots_seen s;
1241 struct btree_trans trans;
1242 struct btree_iter iter;
1246 struct bkey_buf prev;
1247 bch2_bkey_buf_init(&prev);
1248 prev.k->k = KEY(0, 0, 0);
1250 snapshots_seen_init(&s);
1251 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1253 bch_verbose(c, "checking extents");
1255 bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
1256 POS(BCACHEFS_ROOT_INO, 0),
1258 BTREE_ITER_PREFETCH|
1259 BTREE_ITER_ALL_SNAPSHOTS);
1262 ret = __bch2_trans_do(&trans, NULL, NULL,
1263 BTREE_INSERT_LAZY_RW|
1264 BTREE_INSERT_NOFAIL,
1265 check_extent(&trans, &iter, &w, &s));
1268 } while (bch2_btree_iter_advance(&iter));
1269 bch2_trans_iter_exit(&trans, &iter);
1271 bch2_bkey_buf_exit(&prev, c);
1273 inode_walker_exit(&w);
1274 bch2_trans_exit(&trans);
1275 snapshots_seen_exit(&s);
1280 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1282 struct bch_fs *c = trans->c;
1283 struct inode_walker_entry *i;
1284 int ret = 0, ret2 = 0;
1287 for (i = w->d; i < w->d + w->nr; i++) {
1288 if (i->inode.bi_nlink == i->count)
1291 count2 = lockrestart_do(trans,
1292 bch2_count_subdirs(trans, w->cur_inum, i->snapshot));
1294 if (i->count != count2) {
1295 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1298 if (i->inode.bi_nlink == i->count)
1302 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1303 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1304 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1305 i->inode.bi_nlink = i->count;
1306 ret = write_inode(trans, &i->inode, i->snapshot);
1316 static int check_dirent_target(struct btree_trans *trans,
1317 struct btree_iter *iter,
1318 struct bkey_s_c_dirent d,
1319 struct bch_inode_unpacked *target,
1320 u32 target_snapshot)
1322 struct bch_fs *c = trans->c;
1323 struct bkey_i_dirent *n;
1324 bool backpointer_exists = true;
1328 if (!target->bi_dir &&
1329 !target->bi_dir_offset) {
1330 target->bi_dir = d.k->p.inode;
1331 target->bi_dir_offset = d.k->p.offset;
1333 ret = __write_inode(trans, target, target_snapshot);
1338 if (!inode_points_to_dirent(target, d)) {
1339 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1343 backpointer_exists = ret;
1346 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1347 backpointer_exists, c,
1348 "directory %llu with multiple links",
1350 ret = __remove_dirent(trans, d.k->p);
1356 if (fsck_err_on(backpointer_exists &&
1357 !target->bi_nlink, c,
1358 "inode %llu has multiple links but i_nlink 0",
1361 target->bi_flags &= ~BCH_INODE_UNLINKED;
1363 ret = __write_inode(trans, target, target_snapshot);
1368 if (fsck_err_on(!backpointer_exists, c,
1369 "inode %llu:%u has wrong backpointer:\n"
1371 "should be %llu:%llu",
1372 target->bi_inum, target_snapshot,
1374 target->bi_dir_offset,
1377 target->bi_dir = d.k->p.inode;
1378 target->bi_dir_offset = d.k->p.offset;
1380 ret = __write_inode(trans, target, target_snapshot);
1386 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1387 "incorrect d_type: got %s, should be %s:\n%s",
1388 bch2_d_type_str(d.v->d_type),
1389 bch2_d_type_str(inode_d_type(target)),
1390 (bch2_bkey_val_to_text(&PBUF(buf), c, d.s_c), buf))) {
1391 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1392 ret = PTR_ERR_OR_ZERO(n);
1396 bkey_reassemble(&n->k_i, d.s_c);
1397 n->v.d_type = inode_d_type(target);
1399 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1403 d = dirent_i_to_s_c(n);
1406 if (d.v->d_type == DT_SUBVOL &&
1407 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1408 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1409 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1410 le32_to_cpu(d.v->d_parent_subvol),
1411 target->bi_parent_subvol))) {
1412 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1413 ret = PTR_ERR_OR_ZERO(n);
1417 bkey_reassemble(&n->k_i, d.s_c);
1418 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1420 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1424 d = dirent_i_to_s_c(n);
1431 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1432 struct bch_hash_info *hash_info,
1433 struct inode_walker *dir,
1434 struct inode_walker *target,
1435 struct snapshots_seen *s)
1437 struct bch_fs *c = trans->c;
1439 struct bkey_s_c_dirent d;
1440 struct inode_walker_entry *i;
1444 k = bch2_btree_iter_peek(iter);
1452 ret = check_key_has_snapshot(trans, iter, k);
1454 return ret < 0 ? ret : 0;
1456 ret = snapshots_seen_update(c, s, k.k->p);
1460 if (k.k->type == KEY_TYPE_whiteout)
1463 if (dir->cur_inum != k.k->p.inode) {
1464 ret = check_subdir_count(trans, dir);
1469 ret = __walk_inode(trans, dir, k.k->p);
1473 if (fsck_err_on(ret == INT_MAX, c,
1474 "dirent in nonexisting directory:\n%s",
1475 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1476 return bch2_btree_delete_at(trans, iter,
1477 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1485 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1486 "dirent in non directory inode type %s:\n%s",
1487 bch2_d_type_str(inode_d_type(&i->inode)),
1488 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1489 return bch2_btree_delete_at(trans, iter, 0);
1491 if (dir->first_this_inode)
1492 *hash_info = bch2_hash_info_init(c, &dir->d[0].inode);
1494 ret = hash_check_key(trans, bch2_dirent_hash_desc,
1495 hash_info, iter, k);
1498 if (ret) /* dirent has been deleted */
1501 if (k.k->type != KEY_TYPE_dirent)
1504 d = bkey_s_c_to_dirent(k);
1506 if (d.v->d_type == DT_SUBVOL) {
1507 struct bch_inode_unpacked subvol_root;
1508 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1509 u32 target_snapshot;
1512 ret = __subvol_lookup(trans, target_subvol,
1513 &target_snapshot, &target_inum);
1514 if (ret && ret != -ENOENT)
1517 if (fsck_err_on(ret, c,
1518 "dirent points to missing subvolume %llu",
1519 le64_to_cpu(d.v->d_child_subvol)))
1520 return __remove_dirent(trans, d.k->p);
1522 ret = __lookup_inode(trans, target_inum,
1523 &subvol_root, &target_snapshot);
1524 if (ret && ret != -ENOENT)
1527 if (fsck_err_on(ret, c,
1528 "subvolume %u points to missing subvolume root %llu",
1531 bch_err(c, "repair not implemented yet");
1535 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1536 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1538 subvol_root.bi_subvol, target_subvol)) {
1539 subvol_root.bi_subvol = target_subvol;
1540 ret = __write_inode(trans, &subvol_root, target_snapshot);
1545 ret = check_dirent_target(trans, iter, d, &subvol_root,
1550 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1554 if (fsck_err_on(!target->nr, c,
1555 "dirent points to missing inode:\n%s",
1556 (bch2_bkey_val_to_text(&PBUF(buf), c,
1558 ret = __remove_dirent(trans, d.k->p);
1563 for (i = target->d; i < target->d + target->nr; i++) {
1564 ret = check_dirent_target(trans, iter, d,
1565 &i->inode, i->snapshot);
1571 if (d.v->d_type == DT_DIR)
1572 for_each_visible_inode(c, s, dir, d.k->p.snapshot, i)
1580 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1584 static int check_dirents(struct bch_fs *c)
1586 struct inode_walker dir = inode_walker_init();
1587 struct inode_walker target = inode_walker_init();
1588 struct snapshots_seen s;
1589 struct bch_hash_info hash_info;
1590 struct btree_trans trans;
1591 struct btree_iter iter;
1594 bch_verbose(c, "checking dirents");
1596 snapshots_seen_init(&s);
1597 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1599 bch2_trans_iter_init(&trans, &iter, BTREE_ID_dirents,
1600 POS(BCACHEFS_ROOT_INO, 0),
1602 BTREE_ITER_PREFETCH|
1603 BTREE_ITER_ALL_SNAPSHOTS);
1606 ret = __bch2_trans_do(&trans, NULL, NULL,
1607 BTREE_INSERT_LAZY_RW|
1608 BTREE_INSERT_NOFAIL,
1609 check_dirent(&trans, &iter, &hash_info,
1610 &dir, &target, &s));
1613 } while (bch2_btree_iter_advance(&iter));
1614 bch2_trans_iter_exit(&trans, &iter);
1616 bch2_trans_exit(&trans);
1617 snapshots_seen_exit(&s);
1618 inode_walker_exit(&dir);
1619 inode_walker_exit(&target);
1623 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1624 struct bch_hash_info *hash_info,
1625 struct inode_walker *inode)
1627 struct bch_fs *c = trans->c;
1631 k = bch2_btree_iter_peek(iter);
1639 ret = check_key_has_snapshot(trans, iter, k);
1643 ret = __walk_inode(trans, inode, k.k->p);
1647 if (fsck_err_on(ret == INT_MAX, c,
1648 "xattr for missing inode %llu",
1650 return bch2_btree_delete_at(trans, iter, 0);
1657 if (inode->first_this_inode)
1658 *hash_info = bch2_hash_info_init(c, &inode->d[0].inode);
1660 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1666 * Walk xattrs: verify that they all have a corresponding inode
1669 static int check_xattrs(struct bch_fs *c)
1671 struct inode_walker inode = inode_walker_init();
1672 struct bch_hash_info hash_info;
1673 struct btree_trans trans;
1674 struct btree_iter iter;
1677 bch_verbose(c, "checking xattrs");
1679 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1681 bch2_trans_iter_init(&trans, &iter, BTREE_ID_xattrs,
1682 POS(BCACHEFS_ROOT_INO, 0),
1684 BTREE_ITER_PREFETCH|
1685 BTREE_ITER_ALL_SNAPSHOTS);
1688 ret = __bch2_trans_do(&trans, NULL, NULL,
1689 BTREE_INSERT_LAZY_RW|
1690 BTREE_INSERT_NOFAIL,
1691 check_xattr(&trans, &iter, &hash_info,
1695 } while (bch2_btree_iter_advance(&iter));
1696 bch2_trans_iter_exit(&trans, &iter);
1698 bch2_trans_exit(&trans);
1702 static int check_root_trans(struct btree_trans *trans)
1704 struct bch_fs *c = trans->c;
1705 struct bch_inode_unpacked root_inode;
1710 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1711 if (ret && ret != -ENOENT)
1714 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1715 struct bkey_i_subvolume root_subvol;
1718 inum = BCACHEFS_ROOT_INO;
1720 bkey_subvolume_init(&root_subvol.k_i);
1721 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1722 root_subvol.v.flags = 0;
1723 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1724 root_subvol.v.inode = cpu_to_le64(inum);
1725 ret = __bch2_trans_do(trans, NULL, NULL,
1726 BTREE_INSERT_NOFAIL|
1727 BTREE_INSERT_LAZY_RW,
1728 __bch2_btree_insert(trans, BTREE_ID_subvolumes, &root_subvol.k_i));
1730 bch_err(c, "error writing root subvol: %i", ret);
1736 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1737 if (ret && ret != -ENOENT)
1740 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1741 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1742 "root inode not a directory")) {
1743 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1745 root_inode.bi_inum = inum;
1747 ret = __write_inode(trans, &root_inode, snapshot);
1749 bch_err(c, "error writing root inode: %i", ret);
1756 /* Get root directory, create if it doesn't exist: */
1758 static int check_root(struct bch_fs *c)
1760 bch_verbose(c, "checking root directory");
1762 return bch2_trans_do(c, NULL, NULL,
1763 BTREE_INSERT_NOFAIL|
1764 BTREE_INSERT_LAZY_RW,
1765 check_root_trans(&trans));
1772 struct pathbuf_entry {
1778 static bool path_is_dup(struct pathbuf *p, u64 inum, u32 snapshot)
1780 struct pathbuf_entry *i;
1782 for (i = p->entries; i < p->entries + p->nr; i++)
1783 if (i->inum == inum &&
1784 i->snapshot == snapshot)
1790 static int path_down(struct pathbuf *p, u64 inum, u32 snapshot)
1792 if (p->nr == p->size) {
1793 size_t new_size = max_t(size_t, 256UL, p->size * 2);
1794 void *n = krealloc(p->entries,
1795 new_size * sizeof(p->entries[0]),
1805 p->entries[p->nr++] = (struct pathbuf_entry) {
1807 .snapshot = snapshot,
1813 * Check that a given inode is reachable from the root:
1815 * XXX: we should also be verifying that inodes are in the right subvolumes
1817 static int check_path(struct btree_trans *trans,
1819 struct bch_inode_unpacked *inode,
1822 struct bch_fs *c = trans->c;
1825 snapshot = snapshot_t(c, snapshot)->equiv;
1828 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1829 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1830 struct btree_iter dirent_iter;
1831 struct bkey_s_c_dirent d;
1832 u32 parent_snapshot = snapshot;
1834 if (inode->bi_subvol) {
1837 ret = subvol_lookup(trans, inode->bi_parent_subvol,
1838 &parent_snapshot, &inum);
1843 ret = lockrestart_do(trans,
1844 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
1845 SPOS(inode->bi_dir, inode->bi_dir_offset,
1846 parent_snapshot))).k));
1847 if (ret && ret != -ENOENT)
1850 if (!ret && !dirent_points_to_inode(d, inode)) {
1851 bch2_trans_iter_exit(trans, &dirent_iter);
1855 if (ret == -ENOENT) {
1856 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
1857 inode->bi_inum, snapshot,
1858 bch2_d_type_str(inode_d_type(inode)),
1861 inode->bi_dir_offset))
1862 ret = reattach_inode(trans, inode, snapshot);
1866 bch2_trans_iter_exit(trans, &dirent_iter);
1868 if (!S_ISDIR(inode->bi_mode))
1871 ret = path_down(p, inode->bi_inum, snapshot);
1873 bch_err(c, "memory allocation failure");
1877 snapshot = parent_snapshot;
1879 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1881 /* Should have been caught in dirents pass */
1882 bch_err(c, "error looking up parent directory: %i", ret);
1886 if (path_is_dup(p, inode->bi_inum, snapshot)) {
1887 struct pathbuf_entry *i;
1889 /* XXX print path */
1890 bch_err(c, "directory structure loop");
1892 for (i = p->entries; i < p->entries + p->nr; i++)
1893 pr_err("%llu:%u", i->inum, i->snapshot);
1894 pr_err("%llu:%u", inode->bi_inum, snapshot);
1896 if (!fsck_err(c, "directory structure loop"))
1899 ret = __bch2_trans_do(trans, NULL, NULL,
1900 BTREE_INSERT_NOFAIL|
1901 BTREE_INSERT_LAZY_RW,
1902 remove_backpointer(trans, inode));
1904 bch_err(c, "error removing dirent: %i", ret);
1908 ret = reattach_inode(trans, inode, snapshot);
1913 bch_err(c, "%s: err %i", __func__, ret);
1918 * Check for unreachable inodes, as well as loops in the directory structure:
1919 * After check_dirents(), if an inode backpointer doesn't exist that means it's
1923 static int check_directory_structure(struct bch_fs *c)
1925 struct btree_trans trans;
1926 struct btree_iter iter;
1928 struct bch_inode_unpacked u;
1929 struct pathbuf path = { 0, 0, NULL };
1932 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1934 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
1936 BTREE_ITER_PREFETCH|
1937 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
1938 if (k.k->type != KEY_TYPE_inode)
1941 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), &u);
1943 /* Should have been caught earlier in fsck: */
1944 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
1948 if (u.bi_flags & BCH_INODE_UNLINKED)
1951 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
1955 bch2_trans_iter_exit(&trans, &iter);
1957 BUG_ON(ret == -EINTR);
1959 kfree(path.entries);
1961 bch2_trans_exit(&trans);
1965 struct nlink_table {
1976 static int add_nlink(struct nlink_table *t, u64 inum, u32 snapshot)
1978 if (t->nr == t->size) {
1979 size_t new_size = max_t(size_t, 128UL, t->size * 2);
1980 void *d = kvmalloc(new_size * sizeof(t->d[0]), GFP_KERNEL);
1986 memcpy(d, t->d, t->size * sizeof(t->d[0]));
1994 t->d[t->nr++] = (struct nlink) {
1996 .snapshot = snapshot,
2002 static int nlink_cmp(const void *_l, const void *_r)
2004 const struct nlink *l = _l;
2005 const struct nlink *r = _r;
2007 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2010 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2011 struct nlink_table *links,
2012 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2014 struct nlink *link, key = {
2015 .inum = inum, .snapshot = U32_MAX,
2018 if (inum < range_start || inum >= range_end)
2021 link = __inline_bsearch(&key, links->d, links->nr,
2022 sizeof(links->d[0]), nlink_cmp);
2026 while (link > links->d && link[0].inum == link[-1].inum)
2029 for (; link < links->d + links->nr && link->inum == inum; link++)
2030 if (ref_visible(c, s, snapshot, link->snapshot)) {
2032 if (link->snapshot >= snapshot)
2038 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2039 struct nlink_table *t,
2040 u64 start, u64 *end)
2042 struct btree_trans trans;
2043 struct btree_iter iter;
2045 struct bkey_s_c_inode inode;
2046 struct bch_inode_unpacked u;
2049 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2051 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2054 BTREE_ITER_PREFETCH|
2055 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2056 if (k.k->type != KEY_TYPE_inode)
2059 inode = bkey_s_c_to_inode(k);
2062 * Backpointer and directory structure checks are sufficient for
2063 * directories, since they can't have hardlinks:
2065 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
2068 /* Should never fail, checked by bch2_inode_invalid: */
2069 BUG_ON(bch2_inode_unpack(inode, &u));
2074 ret = add_nlink(t, k.k->p.offset, k.k->p.snapshot);
2076 *end = k.k->p.offset;
2082 bch2_trans_iter_exit(&trans, &iter);
2083 bch2_trans_exit(&trans);
2086 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2092 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2093 u64 range_start, u64 range_end)
2095 struct btree_trans trans;
2096 struct snapshots_seen s;
2097 struct btree_iter iter;
2099 struct bkey_s_c_dirent d;
2102 snapshots_seen_init(&s);
2104 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2106 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2108 BTREE_ITER_PREFETCH|
2109 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2110 ret = snapshots_seen_update(c, &s, k.k->p);
2114 switch (k.k->type) {
2115 case KEY_TYPE_dirent:
2116 d = bkey_s_c_to_dirent(k);
2118 if (d.v->d_type != DT_DIR &&
2119 d.v->d_type != DT_SUBVOL)
2120 inc_link(c, &s, links, range_start, range_end,
2121 le64_to_cpu(d.v->d_inum),
2126 bch2_trans_iter_exit(&trans, &iter);
2129 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2131 bch2_trans_exit(&trans);
2132 snapshots_seen_exit(&s);
2137 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2138 struct nlink_table *links,
2139 u64 range_start, u64 range_end)
2141 struct btree_trans trans;
2142 struct btree_iter iter;
2144 struct bkey_s_c_inode inode;
2145 struct bch_inode_unpacked u;
2146 struct nlink *link = links->d;
2149 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2151 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2152 POS(0, range_start),
2154 BTREE_ITER_PREFETCH|
2155 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2156 if (k.k->p.offset >= range_end)
2159 if (k.k->type != KEY_TYPE_inode)
2162 inode = bkey_s_c_to_inode(k);
2163 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
2166 BUG_ON(bch2_inode_unpack(inode, &u));
2171 while ((cmp_int(link->inum, k.k->p.offset) ?:
2172 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2174 BUG_ON(link >= links->d + links->nr);
2177 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2178 "inode %llu has wrong i_nlink (type %u i_nlink %u, should be %u)",
2179 u.bi_inum, mode_to_type(u.bi_mode),
2180 bch2_inode_nlink_get(&u), link->count)) {
2181 bch2_inode_nlink_set(&u, link->count);
2183 ret = write_inode(&trans, &u, k.k->p.snapshot);
2185 bch_err(c, "error in fsck: error %i updating inode", ret);
2189 bch2_trans_iter_exit(&trans, &iter);
2190 bch2_trans_exit(&trans);
2193 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2199 static int check_nlinks(struct bch_fs *c)
2201 struct nlink_table links = { 0 };
2202 u64 this_iter_range_start, next_iter_range_start = 0;
2205 bch_verbose(c, "checking inode nlinks");
2208 this_iter_range_start = next_iter_range_start;
2209 next_iter_range_start = U64_MAX;
2211 ret = check_nlinks_find_hardlinks(c, &links,
2212 this_iter_range_start,
2213 &next_iter_range_start);
2215 ret = check_nlinks_walk_dirents(c, &links,
2216 this_iter_range_start,
2217 next_iter_range_start);
2221 ret = check_nlinks_update_hardlinks(c, &links,
2222 this_iter_range_start,
2223 next_iter_range_start);
2228 } while (next_iter_range_start != U64_MAX);
2235 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter)
2238 struct bkey_s_c_reflink_p p;
2239 struct bkey_i_reflink_p *u;
2242 k = bch2_btree_iter_peek(iter);
2250 if (k.k->type != KEY_TYPE_reflink_p)
2253 p = bkey_s_c_to_reflink_p(k);
2255 if (!p.v->front_pad && !p.v->back_pad)
2258 u = bch2_trans_kmalloc(trans, sizeof(*u));
2259 ret = PTR_ERR_OR_ZERO(u);
2263 bkey_reassemble(&u->k_i, k);
2267 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2271 static int fix_reflink_p(struct bch_fs *c)
2273 struct btree_trans trans;
2274 struct btree_iter iter;
2278 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2281 bch_verbose(c, "fixing reflink_p keys");
2283 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2285 for_each_btree_key(&trans, iter, BTREE_ID_extents, POS_MIN,
2287 BTREE_ITER_PREFETCH|
2288 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2289 if (k.k->type == KEY_TYPE_reflink_p) {
2290 ret = __bch2_trans_do(&trans, NULL, NULL,
2291 BTREE_INSERT_NOFAIL|
2292 BTREE_INSERT_LAZY_RW,
2293 fix_reflink_p_key(&trans, &iter));
2298 bch2_trans_iter_exit(&trans, &iter);
2300 bch2_trans_exit(&trans);
2305 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
2306 * Doesn't fix them yet, mainly because they haven't yet been observed:
2308 int bch2_fsck_full(struct bch_fs *c)
2310 return bch2_fs_snapshots_check(c) ?:
2311 check_inodes(c, true) ?:
2317 check_directory_structure(c) ?:
2322 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
2324 return check_inodes(c, false);