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_first_inode(struct btree_trans *trans, u64 inode_nr,
117 struct bch_inode_unpacked *inode)
119 struct btree_iter iter;
123 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
125 BTREE_ITER_ALL_SNAPSHOTS);
126 k = bch2_btree_iter_peek(&iter);
131 if (!k.k || bkey_cmp(k.k->p, POS(0, inode_nr))) {
136 ret = bch2_inode_unpack(k, inode);
138 if (ret && ret != -EINTR)
139 bch_err(trans->c, "error %i fetching inode %llu",
141 bch2_trans_iter_exit(trans, &iter);
145 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
146 struct bch_inode_unpacked *inode,
149 struct btree_iter iter;
153 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
154 SPOS(0, inode_nr, *snapshot), 0);
155 k = bch2_btree_iter_peek_slot(&iter);
160 ret = bkey_is_inode(k.k)
161 ? bch2_inode_unpack(k, inode)
164 *snapshot = iter.pos.snapshot;
166 if (ret && ret != -EINTR)
167 bch_err(trans->c, "error %i fetching inode %llu:%u",
168 ret, inode_nr, *snapshot);
169 bch2_trans_iter_exit(trans, &iter);
173 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
174 struct bch_inode_unpacked *inode,
177 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
180 static int __lookup_dirent(struct btree_trans *trans,
181 struct bch_hash_info hash_info,
182 subvol_inum dir, struct qstr *name,
183 u64 *target, unsigned *type)
185 struct btree_iter iter;
186 struct bkey_s_c_dirent d;
189 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
190 &hash_info, dir, name, 0);
194 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
195 *target = le64_to_cpu(d.v->d_inum);
197 bch2_trans_iter_exit(trans, &iter);
201 static int __write_inode(struct btree_trans *trans,
202 struct bch_inode_unpacked *inode,
205 struct btree_iter iter;
208 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
209 SPOS(0, inode->bi_inum, snapshot),
212 ret = bch2_btree_iter_traverse(&iter) ?:
213 bch2_inode_write(trans, &iter, inode);
214 bch2_trans_iter_exit(trans, &iter);
218 static int write_inode(struct btree_trans *trans,
219 struct bch_inode_unpacked *inode,
222 int ret = __bch2_trans_do(trans, NULL, NULL,
224 BTREE_INSERT_LAZY_RW,
225 __write_inode(trans, inode, snapshot));
227 bch_err(trans->c, "error in fsck: error %i updating inode", ret);
231 static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
233 struct btree_iter iter = { NULL };
234 struct bkey_i_inode_generation delete;
235 struct bch_inode_unpacked inode_u;
239 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
240 SPOS(inum, 0, snapshot),
241 SPOS(inum, U64_MAX, snapshot),
243 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
244 SPOS(inum, 0, snapshot),
245 SPOS(inum, U64_MAX, snapshot),
247 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
248 SPOS(inum, 0, snapshot),
249 SPOS(inum, U64_MAX, snapshot),
254 bch2_trans_begin(trans);
256 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
257 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
258 k = bch2_btree_iter_peek_slot(&iter);
264 if (!bkey_is_inode(k.k)) {
265 bch2_fs_inconsistent(trans->c,
266 "inode %llu:%u not found when deleting",
272 bch2_inode_unpack(k, &inode_u);
274 /* Subvolume root? */
275 if (inode_u.bi_subvol) {
276 ret = bch2_subvolume_delete(trans, inode_u.bi_subvol);
281 bkey_inode_generation_init(&delete.k_i);
282 delete.k.p = iter.pos;
283 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
285 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
286 bch2_trans_commit(trans, NULL, NULL,
287 BTREE_INSERT_NOFAIL);
289 bch2_trans_iter_exit(trans, &iter);
296 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
298 struct bch_fs *c = trans->c;
299 struct btree_iter iter;
300 struct bch_inode_unpacked dir_inode;
301 struct bch_hash_info dir_hash_info;
304 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
308 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
310 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
312 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
313 &dir_hash_info, &iter, 0);
314 bch2_trans_iter_exit(trans, &iter);
318 /* Get lost+found, create if it doesn't exist: */
319 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
320 struct bch_inode_unpacked *lostfound)
322 struct bch_fs *c = trans->c;
323 struct bch_inode_unpacked root;
324 struct bch_hash_info root_hash_info;
325 struct qstr lostfound_str = QSTR("lost+found");
326 subvol_inum root_inum = { .subvol = subvol };
332 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
336 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
340 root_hash_info = bch2_hash_info_init(c, &root);
342 ret = __lookup_dirent(trans, root_hash_info, root_inum,
343 &lostfound_str, &inum, &d_type);
344 if (ret == -ENOENT) {
345 bch_notice(c, "creating lost+found");
346 goto create_lostfound;
349 if (ret && ret != -EINTR)
350 bch_err(c, "error looking up lost+found: %i", ret);
354 if (d_type != DT_DIR) {
355 bch_err(c, "error looking up lost+found: not a directory");
360 * The check_dirents pass has already run, dangling dirents
361 * shouldn't exist here:
363 return __lookup_inode(trans, inum, lostfound, &snapshot);
366 bch2_inode_init_early(c, lostfound);
368 ret = bch2_create_trans(trans, root_inum, &root,
369 lostfound, &lostfound_str,
370 0, 0, S_IFDIR|0700, 0, NULL, NULL,
371 (subvol_inum) { }, 0);
372 if (ret && ret != -EINTR)
373 bch_err(c, "error creating lost+found: %i", ret);
377 static int __reattach_inode(struct btree_trans *trans,
378 struct bch_inode_unpacked *inode,
381 struct bch_hash_info dir_hash;
382 struct bch_inode_unpacked lostfound;
389 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
393 ret = lookup_lostfound(trans, subvol, &lostfound);
397 if (S_ISDIR(inode->bi_mode)) {
398 lostfound.bi_nlink++;
400 ret = __write_inode(trans, &lostfound, U32_MAX);
405 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
407 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
408 name = (struct qstr) QSTR(name_buf);
410 ret = bch2_dirent_create(trans,
413 .inum = lostfound.bi_inum,
417 &name, inode->bi_inum, &dir_offset,
418 BCH_HASH_SET_MUST_CREATE);
422 inode->bi_dir = lostfound.bi_inum;
423 inode->bi_dir_offset = dir_offset;
425 return __write_inode(trans, inode, inode_snapshot);
428 static int reattach_inode(struct btree_trans *trans,
429 struct bch_inode_unpacked *inode,
432 int ret = __bch2_trans_do(trans, NULL, NULL,
433 BTREE_INSERT_LAZY_RW|
435 __reattach_inode(trans, inode, inode_snapshot));
437 bch_err(trans->c, "error %i reattaching inode %llu",
438 ret, inode->bi_inum);
445 static int remove_backpointer(struct btree_trans *trans,
446 struct bch_inode_unpacked *inode)
448 struct btree_iter iter;
452 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
453 POS(inode->bi_dir, inode->bi_dir_offset), 0);
454 k = bch2_btree_iter_peek_slot(&iter);
458 if (k.k->type != KEY_TYPE_dirent) {
463 ret = __remove_dirent(trans, k.k->p);
465 bch2_trans_iter_exit(trans, &iter);
469 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s, struct bpos pos)
471 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
473 if (bkey_cmp(s->pos, pos))
477 /* Might get called multiple times due to lock restarts */
478 if (s->nr && s->d[s->nr - 1] == pos.snapshot)
481 return snapshots_seen_add(c, s, pos.snapshot);
485 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
486 * and @ancestor hasn't been overwritten in @seen
488 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
490 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
491 u32 id, u32 ancestor)
495 BUG_ON(id > ancestor);
497 id = snapshot_t(c, id)->equiv;
498 ancestor = snapshot_t(c, ancestor)->equiv;
500 /* @ancestor should be the snapshot most recently added to @seen */
501 BUG_ON(!seen->nr || seen->d[seen->nr - 1] != ancestor);
502 BUG_ON(seen->pos.snapshot != ancestor);
507 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
510 for (i = seen->nr - 2;
511 i >= 0 && seen->d[i] >= id;
513 if (bch2_snapshot_is_ancestor(c, id, seen->d[i]) &&
514 bch2_snapshot_is_ancestor(c, seen->d[i], ancestor))
521 * ref_visible - given a key with snapshot id @src that points to a key with
522 * snapshot id @dst, test whether there is some snapshot in which @dst is
525 * This assumes we're visiting @src keys in natural key order.
527 * @s - list of snapshot IDs already seen at @src
528 * @src - snapshot ID of src key
529 * @dst - snapshot ID of dst key
531 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
535 ? key_visible_in_snapshot(c, s, dst, src)
536 : bch2_snapshot_is_ancestor(c, src, dst);
539 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
540 for (_i = (_w)->d; _i < (_w)->d + (_w)->nr && (_i)->snapshot <= (_snapshot); _i++)\
541 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
543 struct inode_walker {
544 bool first_this_inode;
549 struct inode_walker_entry {
550 struct bch_inode_unpacked inode;
556 static void inode_walker_exit(struct inode_walker *w)
562 static struct inode_walker inode_walker_init(void)
564 return (struct inode_walker) { 0, };
567 static int inode_walker_realloc(struct bch_fs *c, struct inode_walker *w)
569 if (w->nr == w->size) {
570 size_t new_size = max_t(size_t, 8UL, w->size * 2);
571 void *d = krealloc(w->d, new_size * sizeof(w->d[0]),
574 bch_err(c, "fsck: error allocating memory for inode_walker, size %zu",
586 static int add_inode(struct bch_fs *c, struct inode_walker *w,
587 struct bkey_s_c inode)
589 struct bch_inode_unpacked u;
592 ret = inode_walker_realloc(c, w);
596 BUG_ON(bch2_inode_unpack(inode, &u));
598 w->d[w->nr++] = (struct inode_walker_entry) {
600 .snapshot = snapshot_t(c, inode.k->p.snapshot)->equiv,
606 static int __walk_inode(struct btree_trans *trans,
607 struct inode_walker *w, struct bpos pos)
609 struct bch_fs *c = trans->c;
610 struct btree_iter iter;
612 unsigned i, ancestor_pos;
615 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
617 if (pos.inode == w->cur_inum) {
618 w->first_this_inode = false;
619 goto lookup_snapshot;
624 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, pos.inode),
625 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
626 if (k.k->p.offset != pos.inode)
629 if (bkey_is_inode(k.k))
632 bch2_trans_iter_exit(trans, &iter);
637 w->cur_inum = pos.inode;
638 w->first_this_inode = true;
640 for (i = 0; i < w->nr; i++)
641 if (bch2_snapshot_is_ancestor(c, pos.snapshot, w->d[i].snapshot))
645 BUG_ON(pos.snapshot > w->d[i].snapshot);
647 if (pos.snapshot != w->d[i].snapshot) {
650 while (i && w->d[i - 1].snapshot > pos.snapshot)
653 ret = inode_walker_realloc(c, w);
657 array_insert_item(w->d, w->nr, i, w->d[ancestor_pos]);
658 w->d[i].snapshot = pos.snapshot;
665 static int __get_visible_inodes(struct btree_trans *trans,
666 struct inode_walker *w,
667 struct snapshots_seen *s,
670 struct bch_fs *c = trans->c;
671 struct btree_iter iter;
677 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
678 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
679 if (k.k->p.offset != inum)
682 if (!bkey_is_inode(k.k))
685 if (ref_visible(c, s, s->pos.snapshot, k.k->p.snapshot)) {
687 if (k.k->p.snapshot >= s->pos.snapshot)
691 bch2_trans_iter_exit(trans, &iter);
696 static int check_key_has_snapshot(struct btree_trans *trans,
697 struct btree_iter *iter,
700 struct bch_fs *c = trans->c;
704 if (mustfix_fsck_err_on(!snapshot_t(c, k.k->p.snapshot)->equiv, c,
705 "key in missing snapshot: %s",
706 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
707 return bch2_btree_delete_at(trans, iter,
708 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
713 static int hash_redo_key(struct btree_trans *trans,
714 const struct bch_hash_desc desc,
715 struct bch_hash_info *hash_info,
716 struct btree_iter *k_iter, struct bkey_s_c k)
718 bch_err(trans->c, "hash_redo_key() not implemented yet");
721 struct bkey_i *delete;
724 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
726 return PTR_ERR(delete);
728 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
732 bkey_reassemble(tmp, k);
734 bkey_init(&delete->k);
735 delete->k.p = k_iter->pos;
736 return bch2_btree_iter_traverse(k_iter) ?:
737 bch2_trans_update(trans, k_iter, delete, 0) ?:
738 bch2_hash_set(trans, desc, hash_info, k_iter->pos.inode, tmp, 0);
742 static int hash_check_key(struct btree_trans *trans,
743 const struct bch_hash_desc desc,
744 struct bch_hash_info *hash_info,
745 struct btree_iter *k_iter, struct bkey_s_c hash_k)
747 struct bch_fs *c = trans->c;
748 struct btree_iter iter = { NULL };
754 if (hash_k.k->type != desc.key_type)
757 hash = desc.hash_bkey(hash_info, hash_k);
759 if (likely(hash == hash_k.k->p.offset))
762 if (hash_k.k->p.offset < hash)
765 for_each_btree_key(trans, iter, desc.btree_id, POS(hash_k.k->p.inode, hash),
766 BTREE_ITER_SLOTS, k, ret) {
767 if (!bkey_cmp(k.k->p, hash_k.k->p))
770 if (fsck_err_on(k.k->type == desc.key_type &&
771 !desc.cmp_bkey(k, hash_k), c,
772 "duplicate hash table keys:\n%s",
773 (bch2_bkey_val_to_text(&PBUF(buf), c,
775 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
779 if (bkey_deleted(k.k)) {
780 bch2_trans_iter_exit(trans, &iter);
785 bch2_trans_iter_exit(trans, &iter);
788 if (fsck_err(c, "hash table key at wrong offset: btree %u inode %llu offset %llu, "
789 "hashed to %llu\n%s",
790 desc.btree_id, hash_k.k->p.inode, hash_k.k->p.offset, hash,
791 (bch2_bkey_val_to_text(&PBUF(buf), c, hash_k), buf)) == FSCK_ERR_IGNORE)
794 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
796 bch_err(c, "hash_redo_key err %i", ret);
804 static int check_inode(struct btree_trans *trans,
805 struct btree_iter *iter,
806 struct bch_inode_unpacked *prev,
809 struct bch_fs *c = trans->c;
811 struct bch_inode_unpacked u;
812 bool do_update = false;
815 k = bch2_btree_iter_peek(iter);
823 ret = check_key_has_snapshot(trans, iter, k);
825 return ret < 0 ? ret : 0;
828 * if snapshot id isn't a leaf node, skip it - deletion in
829 * particular is not atomic, so on the internal snapshot nodes
830 * we can see inodes marked for deletion after a clean shutdown
832 if (bch2_snapshot_internal_node(c, k.k->p.snapshot))
835 if (!bkey_is_inode(k.k))
838 BUG_ON(bch2_inode_unpack(k, &u));
841 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
842 BCH_INODE_I_SECTORS_DIRTY|
843 BCH_INODE_UNLINKED)))
846 if (prev->bi_inum != u.bi_inum)
849 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
850 inode_d_type(prev) != inode_d_type(&u), c,
851 "inodes in different snapshots don't match")) {
852 bch_err(c, "repair not implemented yet");
856 if (u.bi_flags & BCH_INODE_UNLINKED &&
858 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
860 bch2_trans_unlock(trans);
863 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
865 bch_err(c, "error in fsck: error %i while deleting inode", ret);
869 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
871 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
873 bch_verbose(c, "truncating inode %llu", u.bi_inum);
875 bch2_trans_unlock(trans);
879 * XXX: need to truncate partial blocks too here - or ideally
880 * just switch units to bytes and that issue goes away
882 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
883 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
885 POS(u.bi_inum, U64_MAX),
888 bch_err(c, "error in fsck: error %i truncating inode", ret);
893 * We truncated without our normal sector accounting hook, just
894 * make sure we recalculate it:
896 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
898 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
902 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
904 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
908 bch_verbose(c, "recounting sectors for inode %llu",
911 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
913 bch_err(c, "error in fsck: error %i recounting inode sectors",
918 u.bi_sectors = sectors;
919 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
923 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
926 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
931 ret = write_inode(trans, &u, iter->pos.snapshot);
933 bch_err(c, "error in fsck: error %i "
934 "updating inode", ret);
941 static int check_inodes(struct bch_fs *c, bool full)
943 struct btree_trans trans;
944 struct btree_iter iter;
945 struct bch_inode_unpacked prev = { 0 };
948 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
950 bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes, POS_MIN,
953 BTREE_ITER_ALL_SNAPSHOTS);
956 ret = __bch2_trans_do(&trans, NULL, NULL,
957 BTREE_INSERT_LAZY_RW|
959 check_inode(&trans, &iter, &prev, full));
962 } while (bch2_btree_iter_advance(&iter));
963 bch2_trans_iter_exit(&trans, &iter);
965 bch2_trans_exit(&trans);
969 static int check_subvol(struct btree_trans *trans,
970 struct btree_iter *iter)
973 struct bkey_s_c_subvolume subvol;
976 k = bch2_btree_iter_peek(iter);
984 if (k.k->type != KEY_TYPE_subvolume)
987 subvol = bkey_s_c_to_subvolume(k);
989 if (BCH_SUBVOLUME_UNLINKED(subvol.v)) {
990 ret = bch2_subvolume_delete(trans, iter->pos.offset);
991 if (ret && ret != -EINTR)
992 bch_err(trans->c, "error deleting subvolume %llu: %i",
993 iter->pos.offset, ret);
1002 static int check_subvols(struct bch_fs *c)
1004 struct btree_trans trans;
1005 struct btree_iter iter;
1008 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1010 bch2_trans_iter_init(&trans, &iter, BTREE_ID_subvolumes,
1013 BTREE_ITER_PREFETCH);
1016 ret = __bch2_trans_do(&trans, NULL, NULL,
1017 BTREE_INSERT_LAZY_RW|
1018 BTREE_INSERT_NOFAIL,
1019 check_subvol(&trans, &iter));
1022 } while (bch2_btree_iter_advance(&iter));
1023 bch2_trans_iter_exit(&trans, &iter);
1025 bch2_trans_exit(&trans);
1030 * Checking for overlapping extents needs to be reimplemented
1033 static int fix_overlapping_extent(struct btree_trans *trans,
1034 struct bkey_s_c k, struct bpos cut_at)
1036 struct btree_iter iter;
1040 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1041 ret = PTR_ERR_OR_ZERO(u);
1045 bkey_reassemble(u, k);
1046 bch2_cut_front(cut_at, u);
1050 * We don't want to go through the extent_handle_overwrites path:
1052 * XXX: this is going to screw up disk accounting, extent triggers
1053 * assume things about extent overwrites - we should be running the
1054 * triggers manually here
1056 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, u->k.p,
1057 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
1059 BUG_ON(iter.flags & BTREE_ITER_IS_EXTENTS);
1060 ret = bch2_btree_iter_traverse(&iter) ?:
1061 bch2_trans_update(trans, &iter, u, BTREE_TRIGGER_NORUN) ?:
1062 bch2_trans_commit(trans, NULL, NULL,
1063 BTREE_INSERT_NOFAIL|
1064 BTREE_INSERT_LAZY_RW);
1065 bch2_trans_iter_exit(trans, &iter);
1070 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1071 struct btree_iter *iter,
1077 bch2_trans_iter_init(trans, iter, BTREE_ID_dirents, pos, 0);
1078 k = bch2_btree_iter_peek_slot(iter);
1080 if (!ret && k.k->type != KEY_TYPE_dirent)
1083 bch2_trans_iter_exit(trans, iter);
1084 return (struct bkey_s_c_dirent) { .k = ERR_PTR(ret) };
1087 return bkey_s_c_to_dirent(k);
1090 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1091 struct bkey_s_c_dirent d)
1093 return inode->bi_dir == d.k->p.inode &&
1094 inode->bi_dir_offset == d.k->p.offset;
1097 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1098 struct bch_inode_unpacked *inode)
1100 return d.v->d_type == DT_SUBVOL
1101 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1102 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1105 static int inode_backpointer_exists(struct btree_trans *trans,
1106 struct bch_inode_unpacked *inode,
1109 struct btree_iter iter;
1110 struct bkey_s_c_dirent d;
1113 d = dirent_get_by_pos(trans, &iter,
1114 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1115 ret = bkey_err(d.s_c);
1119 ret = dirent_points_to_inode(d, inode);
1120 bch2_trans_iter_exit(trans, &iter);
1124 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1126 struct bch_fs *c = trans->c;
1127 struct inode_walker_entry *i;
1128 int ret = 0, ret2 = 0;
1131 for (i = w->d; i < w->d + w->nr; i++) {
1132 if (i->inode.bi_sectors == i->count)
1135 count2 = lockrestart_do(trans,
1136 bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot));
1138 if (i->count != count2) {
1139 bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1142 if (i->inode.bi_sectors == i->count)
1146 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1147 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1148 w->cur_inum, i->snapshot,
1149 i->inode.bi_sectors, i->count) == FSCK_ERR_IGNORE)
1152 i->inode.bi_sectors = i->count;
1153 ret = write_inode(trans, &i->inode, i->snapshot);
1162 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1163 struct inode_walker *inode,
1164 struct snapshots_seen *s)
1166 struct bch_fs *c = trans->c;
1168 struct inode_walker_entry *i;
1172 k = bch2_btree_iter_peek(iter);
1180 ret = check_key_has_snapshot(trans, iter, k);
1182 return ret < 0 ? ret : 0;
1184 ret = snapshots_seen_update(c, s, k.k->p);
1188 if (k.k->type == KEY_TYPE_whiteout)
1191 if (inode->cur_inum != k.k->p.inode) {
1192 ret = check_i_sectors(trans, inode);
1197 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
1201 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
1202 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
1204 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2))
1205 return fix_overlapping_extent(trans, k, prev.k->k.p) ?: -EINTR;
1208 ret = __walk_inode(trans, inode, k.k->p);
1212 if (fsck_err_on(ret == INT_MAX, c,
1213 "extent in missing inode:\n %s",
1214 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1215 return bch2_btree_delete_at(trans, iter,
1216 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1224 if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1225 !S_ISLNK(i->inode.bi_mode), c,
1226 "extent in non regular inode mode %o:\n %s",
1228 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1229 return bch2_btree_delete_at(trans, iter,
1230 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1232 if (!bch2_snapshot_internal_node(c, k.k->p.snapshot)) {
1233 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i) {
1234 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1235 k.k->type != KEY_TYPE_reservation &&
1236 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9, c,
1237 "extent type %u offset %llu past end of inode %llu, i_size %llu",
1238 k.k->type, k.k->p.offset, k.k->p.inode, i->inode.bi_size)) {
1240 return bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1241 SPOS(k.k->p.inode, round_up(i->inode.bi_size, block_bytes(c)) >> 9,
1243 POS(k.k->p.inode, U64_MAX),
1249 if (bkey_extent_is_allocation(k.k))
1250 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i)
1251 i->count += k.k->size;
1253 bch2_bkey_buf_reassemble(&prev, c, k);
1261 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1262 * that i_size an i_sectors are consistent
1265 static int check_extents(struct bch_fs *c)
1267 struct inode_walker w = inode_walker_init();
1268 struct snapshots_seen s;
1269 struct btree_trans trans;
1270 struct btree_iter iter;
1274 struct bkey_buf prev;
1275 bch2_bkey_buf_init(&prev);
1276 prev.k->k = KEY(0, 0, 0);
1278 snapshots_seen_init(&s);
1279 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1281 bch_verbose(c, "checking extents");
1283 bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
1284 POS(BCACHEFS_ROOT_INO, 0),
1286 BTREE_ITER_PREFETCH|
1287 BTREE_ITER_ALL_SNAPSHOTS);
1290 ret = __bch2_trans_do(&trans, NULL, NULL,
1291 BTREE_INSERT_LAZY_RW|
1292 BTREE_INSERT_NOFAIL,
1293 check_extent(&trans, &iter, &w, &s));
1296 } while (bch2_btree_iter_advance(&iter));
1297 bch2_trans_iter_exit(&trans, &iter);
1299 bch2_bkey_buf_exit(&prev, c);
1301 inode_walker_exit(&w);
1302 bch2_trans_exit(&trans);
1303 snapshots_seen_exit(&s);
1308 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1310 struct bch_fs *c = trans->c;
1311 struct inode_walker_entry *i;
1312 int ret = 0, ret2 = 0;
1315 for (i = w->d; i < w->d + w->nr; i++) {
1316 if (i->inode.bi_nlink == i->count)
1319 count2 = bch2_count_subdirs(trans, w->cur_inum, i->snapshot);
1323 if (i->count != count2) {
1324 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1327 if (i->inode.bi_nlink == i->count)
1331 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1332 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1333 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1334 i->inode.bi_nlink = i->count;
1335 ret = write_inode(trans, &i->inode, i->snapshot);
1345 static int check_dirent_target(struct btree_trans *trans,
1346 struct btree_iter *iter,
1347 struct bkey_s_c_dirent d,
1348 struct bch_inode_unpacked *target,
1349 u32 target_snapshot)
1351 struct bch_fs *c = trans->c;
1352 struct bkey_i_dirent *n;
1353 bool backpointer_exists = true;
1357 if (!target->bi_dir &&
1358 !target->bi_dir_offset) {
1359 target->bi_dir = d.k->p.inode;
1360 target->bi_dir_offset = d.k->p.offset;
1362 ret = __write_inode(trans, target, target_snapshot);
1367 if (!inode_points_to_dirent(target, d)) {
1368 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1372 backpointer_exists = ret;
1375 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1376 backpointer_exists, c,
1377 "directory %llu with multiple links",
1379 ret = __remove_dirent(trans, d.k->p);
1385 if (fsck_err_on(backpointer_exists &&
1386 !target->bi_nlink, c,
1387 "inode %llu has multiple links but i_nlink 0",
1390 target->bi_flags &= ~BCH_INODE_UNLINKED;
1392 ret = __write_inode(trans, target, target_snapshot);
1397 if (fsck_err_on(!backpointer_exists, c,
1398 "inode %llu:%u has wrong backpointer:\n"
1400 "should be %llu:%llu",
1401 target->bi_inum, target_snapshot,
1403 target->bi_dir_offset,
1406 target->bi_dir = d.k->p.inode;
1407 target->bi_dir_offset = d.k->p.offset;
1409 ret = __write_inode(trans, target, target_snapshot);
1415 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1416 "incorrect d_type: got %s, should be %s:\n%s",
1417 bch2_d_type_str(d.v->d_type),
1418 bch2_d_type_str(inode_d_type(target)),
1419 (bch2_bkey_val_to_text(&PBUF(buf), c, d.s_c), buf))) {
1420 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1421 ret = PTR_ERR_OR_ZERO(n);
1425 bkey_reassemble(&n->k_i, d.s_c);
1426 n->v.d_type = inode_d_type(target);
1428 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1432 d = dirent_i_to_s_c(n);
1435 if (d.v->d_type == DT_SUBVOL &&
1436 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1437 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1438 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1439 le32_to_cpu(d.v->d_parent_subvol),
1440 target->bi_parent_subvol))) {
1441 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1442 ret = PTR_ERR_OR_ZERO(n);
1446 bkey_reassemble(&n->k_i, d.s_c);
1447 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1449 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1453 d = dirent_i_to_s_c(n);
1460 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1461 struct bch_hash_info *hash_info,
1462 struct inode_walker *dir,
1463 struct inode_walker *target,
1464 struct snapshots_seen *s)
1466 struct bch_fs *c = trans->c;
1468 struct bkey_s_c_dirent d;
1469 struct inode_walker_entry *i;
1473 k = bch2_btree_iter_peek(iter);
1481 ret = check_key_has_snapshot(trans, iter, k);
1483 return ret < 0 ? ret : 0;
1485 ret = snapshots_seen_update(c, s, k.k->p);
1489 if (k.k->type == KEY_TYPE_whiteout)
1492 if (dir->cur_inum != k.k->p.inode) {
1493 ret = check_subdir_count(trans, dir);
1498 ret = __walk_inode(trans, dir, k.k->p);
1502 if (fsck_err_on(ret == INT_MAX, c,
1503 "dirent in nonexisting directory:\n%s",
1504 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1505 return bch2_btree_delete_at(trans, iter,
1506 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1514 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1515 "dirent in non directory inode type %s:\n%s",
1516 bch2_d_type_str(inode_d_type(&i->inode)),
1517 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1518 return bch2_btree_delete_at(trans, iter, 0);
1520 if (dir->first_this_inode)
1521 *hash_info = bch2_hash_info_init(c, &dir->d[0].inode);
1523 ret = hash_check_key(trans, bch2_dirent_hash_desc,
1524 hash_info, iter, k);
1527 if (ret) /* dirent has been deleted */
1530 if (k.k->type != KEY_TYPE_dirent)
1533 d = bkey_s_c_to_dirent(k);
1535 if (d.v->d_type == DT_SUBVOL) {
1536 struct bch_inode_unpacked subvol_root;
1537 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1538 u32 target_snapshot;
1541 ret = __subvol_lookup(trans, target_subvol,
1542 &target_snapshot, &target_inum);
1543 if (ret && ret != -ENOENT)
1546 if (fsck_err_on(ret, c,
1547 "dirent points to missing subvolume %llu",
1548 le64_to_cpu(d.v->d_child_subvol)))
1549 return __remove_dirent(trans, d.k->p);
1551 ret = __lookup_inode(trans, target_inum,
1552 &subvol_root, &target_snapshot);
1553 if (ret && ret != -ENOENT)
1556 if (fsck_err_on(ret, c,
1557 "subvolume %u points to missing subvolume root %llu",
1560 bch_err(c, "repair not implemented yet");
1564 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1565 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1567 subvol_root.bi_subvol, target_subvol)) {
1568 subvol_root.bi_subvol = target_subvol;
1569 ret = __write_inode(trans, &subvol_root, target_snapshot);
1574 ret = check_dirent_target(trans, iter, d, &subvol_root,
1579 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1583 if (fsck_err_on(!target->nr, c,
1584 "dirent points to missing inode:\n%s",
1585 (bch2_bkey_val_to_text(&PBUF(buf), c,
1587 ret = __remove_dirent(trans, d.k->p);
1592 for (i = target->d; i < target->d + target->nr; i++) {
1593 ret = check_dirent_target(trans, iter, d,
1594 &i->inode, i->snapshot);
1600 if (d.v->d_type == DT_DIR)
1601 for_each_visible_inode(c, s, dir, d.k->p.snapshot, i)
1609 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1613 static int check_dirents(struct bch_fs *c)
1615 struct inode_walker dir = inode_walker_init();
1616 struct inode_walker target = inode_walker_init();
1617 struct snapshots_seen s;
1618 struct bch_hash_info hash_info;
1619 struct btree_trans trans;
1620 struct btree_iter iter;
1623 bch_verbose(c, "checking dirents");
1625 snapshots_seen_init(&s);
1626 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1628 bch2_trans_iter_init(&trans, &iter, BTREE_ID_dirents,
1629 POS(BCACHEFS_ROOT_INO, 0),
1631 BTREE_ITER_PREFETCH|
1632 BTREE_ITER_ALL_SNAPSHOTS);
1635 ret = __bch2_trans_do(&trans, NULL, NULL,
1636 BTREE_INSERT_LAZY_RW|
1637 BTREE_INSERT_NOFAIL,
1638 check_dirent(&trans, &iter, &hash_info,
1639 &dir, &target, &s));
1642 } while (bch2_btree_iter_advance(&iter));
1643 bch2_trans_iter_exit(&trans, &iter);
1645 bch2_trans_exit(&trans);
1646 snapshots_seen_exit(&s);
1647 inode_walker_exit(&dir);
1648 inode_walker_exit(&target);
1652 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1653 struct bch_hash_info *hash_info,
1654 struct inode_walker *inode)
1656 struct bch_fs *c = trans->c;
1660 k = bch2_btree_iter_peek(iter);
1668 ret = check_key_has_snapshot(trans, iter, k);
1672 ret = __walk_inode(trans, inode, k.k->p);
1676 if (fsck_err_on(ret == INT_MAX, c,
1677 "xattr for missing inode %llu",
1679 return bch2_btree_delete_at(trans, iter, 0);
1686 if (inode->first_this_inode)
1687 *hash_info = bch2_hash_info_init(c, &inode->d[0].inode);
1689 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1695 * Walk xattrs: verify that they all have a corresponding inode
1698 static int check_xattrs(struct bch_fs *c)
1700 struct inode_walker inode = inode_walker_init();
1701 struct bch_hash_info hash_info;
1702 struct btree_trans trans;
1703 struct btree_iter iter;
1706 bch_verbose(c, "checking xattrs");
1708 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1710 bch2_trans_iter_init(&trans, &iter, BTREE_ID_xattrs,
1711 POS(BCACHEFS_ROOT_INO, 0),
1713 BTREE_ITER_PREFETCH|
1714 BTREE_ITER_ALL_SNAPSHOTS);
1717 ret = __bch2_trans_do(&trans, NULL, NULL,
1718 BTREE_INSERT_LAZY_RW|
1719 BTREE_INSERT_NOFAIL,
1720 check_xattr(&trans, &iter, &hash_info,
1724 } while (bch2_btree_iter_advance(&iter));
1725 bch2_trans_iter_exit(&trans, &iter);
1727 bch2_trans_exit(&trans);
1731 static int check_root_trans(struct btree_trans *trans)
1733 struct bch_fs *c = trans->c;
1734 struct bch_inode_unpacked root_inode;
1739 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1740 if (ret && ret != -ENOENT)
1743 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1744 struct bkey_i_subvolume root_subvol;
1747 inum = BCACHEFS_ROOT_INO;
1749 bkey_subvolume_init(&root_subvol.k_i);
1750 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1751 root_subvol.v.flags = 0;
1752 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1753 root_subvol.v.inode = cpu_to_le64(inum);
1754 ret = __bch2_trans_do(trans, NULL, NULL,
1755 BTREE_INSERT_NOFAIL|
1756 BTREE_INSERT_LAZY_RW,
1757 __bch2_btree_insert(trans, BTREE_ID_subvolumes, &root_subvol.k_i));
1759 bch_err(c, "error writing root subvol: %i", ret);
1765 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1766 if (ret && ret != -ENOENT)
1769 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1770 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1771 "root inode not a directory")) {
1772 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1774 root_inode.bi_inum = inum;
1776 ret = __write_inode(trans, &root_inode, snapshot);
1778 bch_err(c, "error writing root inode: %i", ret);
1785 /* Get root directory, create if it doesn't exist: */
1787 static int check_root(struct bch_fs *c)
1789 bch_verbose(c, "checking root directory");
1791 return bch2_trans_do(c, NULL, NULL,
1792 BTREE_INSERT_NOFAIL|
1793 BTREE_INSERT_LAZY_RW,
1794 check_root_trans(&trans));
1801 struct pathbuf_entry {
1807 static bool path_is_dup(struct pathbuf *p, u64 inum, u32 snapshot)
1809 struct pathbuf_entry *i;
1811 for (i = p->entries; i < p->entries + p->nr; i++)
1812 if (i->inum == inum &&
1813 i->snapshot == snapshot)
1819 static int path_down(struct bch_fs *c, struct pathbuf *p,
1820 u64 inum, u32 snapshot)
1822 if (p->nr == p->size) {
1823 size_t new_size = max_t(size_t, 256UL, p->size * 2);
1824 void *n = krealloc(p->entries,
1825 new_size * sizeof(p->entries[0]),
1828 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1837 p->entries[p->nr++] = (struct pathbuf_entry) {
1839 .snapshot = snapshot,
1845 * Check that a given inode is reachable from the root:
1847 * XXX: we should also be verifying that inodes are in the right subvolumes
1849 static int check_path(struct btree_trans *trans,
1851 struct bch_inode_unpacked *inode,
1854 struct bch_fs *c = trans->c;
1857 snapshot = snapshot_t(c, snapshot)->equiv;
1860 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1861 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1862 struct btree_iter dirent_iter;
1863 struct bkey_s_c_dirent d;
1864 u32 parent_snapshot = snapshot;
1866 if (inode->bi_subvol) {
1869 ret = subvol_lookup(trans, inode->bi_parent_subvol,
1870 &parent_snapshot, &inum);
1875 ret = lockrestart_do(trans,
1876 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
1877 SPOS(inode->bi_dir, inode->bi_dir_offset,
1878 parent_snapshot))).k));
1879 if (ret && ret != -ENOENT)
1882 if (!ret && !dirent_points_to_inode(d, inode)) {
1883 bch2_trans_iter_exit(trans, &dirent_iter);
1887 if (ret == -ENOENT) {
1888 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
1889 inode->bi_inum, snapshot,
1890 bch2_d_type_str(inode_d_type(inode)),
1893 inode->bi_dir_offset))
1894 ret = reattach_inode(trans, inode, snapshot);
1898 bch2_trans_iter_exit(trans, &dirent_iter);
1900 if (!S_ISDIR(inode->bi_mode))
1903 ret = path_down(c, p, inode->bi_inum, snapshot);
1905 bch_err(c, "memory allocation failure");
1909 snapshot = parent_snapshot;
1911 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1913 /* Should have been caught in dirents pass */
1914 bch_err(c, "error looking up parent directory: %i", ret);
1918 if (path_is_dup(p, inode->bi_inum, snapshot)) {
1919 struct pathbuf_entry *i;
1921 /* XXX print path */
1922 bch_err(c, "directory structure loop");
1924 for (i = p->entries; i < p->entries + p->nr; i++)
1925 pr_err("%llu:%u", i->inum, i->snapshot);
1926 pr_err("%llu:%u", inode->bi_inum, snapshot);
1928 if (!fsck_err(c, "directory structure loop"))
1931 ret = __bch2_trans_do(trans, NULL, NULL,
1932 BTREE_INSERT_NOFAIL|
1933 BTREE_INSERT_LAZY_RW,
1934 remove_backpointer(trans, inode));
1936 bch_err(c, "error removing dirent: %i", ret);
1940 ret = reattach_inode(trans, inode, snapshot);
1945 bch_err(c, "%s: err %i", __func__, ret);
1950 * Check for unreachable inodes, as well as loops in the directory structure:
1951 * After check_dirents(), if an inode backpointer doesn't exist that means it's
1955 static int check_directory_structure(struct bch_fs *c)
1957 struct btree_trans trans;
1958 struct btree_iter iter;
1960 struct bch_inode_unpacked u;
1961 struct pathbuf path = { 0, 0, NULL };
1964 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1966 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
1968 BTREE_ITER_PREFETCH|
1969 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
1970 if (!bkey_is_inode(k.k))
1973 ret = bch2_inode_unpack(k, &u);
1975 /* Should have been caught earlier in fsck: */
1976 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
1980 if (u.bi_flags & BCH_INODE_UNLINKED)
1983 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
1987 bch2_trans_iter_exit(&trans, &iter);
1989 BUG_ON(ret == -EINTR);
1991 kfree(path.entries);
1993 bch2_trans_exit(&trans);
1997 struct nlink_table {
2008 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2009 u64 inum, u32 snapshot)
2011 if (t->nr == t->size) {
2012 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2013 void *d = kvmalloc(new_size * sizeof(t->d[0]), GFP_KERNEL);
2015 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2021 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2029 t->d[t->nr++] = (struct nlink) {
2031 .snapshot = snapshot,
2037 static int nlink_cmp(const void *_l, const void *_r)
2039 const struct nlink *l = _l;
2040 const struct nlink *r = _r;
2042 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2045 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2046 struct nlink_table *links,
2047 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2049 struct nlink *link, key = {
2050 .inum = inum, .snapshot = U32_MAX,
2053 if (inum < range_start || inum >= range_end)
2056 link = __inline_bsearch(&key, links->d, links->nr,
2057 sizeof(links->d[0]), nlink_cmp);
2061 while (link > links->d && link[0].inum == link[-1].inum)
2064 for (; link < links->d + links->nr && link->inum == inum; link++)
2065 if (ref_visible(c, s, snapshot, link->snapshot)) {
2067 if (link->snapshot >= snapshot)
2073 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2074 struct nlink_table *t,
2075 u64 start, u64 *end)
2077 struct btree_trans trans;
2078 struct btree_iter iter;
2080 struct bch_inode_unpacked u;
2083 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2085 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2088 BTREE_ITER_PREFETCH|
2089 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2090 if (!bkey_is_inode(k.k))
2093 /* Should never fail, checked by bch2_inode_invalid: */
2094 BUG_ON(bch2_inode_unpack(k, &u));
2097 * Backpointer and directory structure checks are sufficient for
2098 * directories, since they can't have hardlinks:
2100 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2106 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2108 *end = k.k->p.offset;
2114 bch2_trans_iter_exit(&trans, &iter);
2115 bch2_trans_exit(&trans);
2118 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2124 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2125 u64 range_start, u64 range_end)
2127 struct btree_trans trans;
2128 struct snapshots_seen s;
2129 struct btree_iter iter;
2131 struct bkey_s_c_dirent d;
2134 snapshots_seen_init(&s);
2136 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2138 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2140 BTREE_ITER_PREFETCH|
2141 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2142 ret = snapshots_seen_update(c, &s, k.k->p);
2146 switch (k.k->type) {
2147 case KEY_TYPE_dirent:
2148 d = bkey_s_c_to_dirent(k);
2150 if (d.v->d_type != DT_DIR &&
2151 d.v->d_type != DT_SUBVOL)
2152 inc_link(c, &s, links, range_start, range_end,
2153 le64_to_cpu(d.v->d_inum),
2158 bch2_trans_iter_exit(&trans, &iter);
2161 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2163 bch2_trans_exit(&trans);
2164 snapshots_seen_exit(&s);
2169 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2170 struct nlink_table *links,
2171 u64 range_start, u64 range_end)
2173 struct btree_trans trans;
2174 struct btree_iter iter;
2176 struct bch_inode_unpacked u;
2177 struct nlink *link = links->d;
2180 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2182 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2183 POS(0, range_start),
2185 BTREE_ITER_PREFETCH|
2186 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2187 if (k.k->p.offset >= range_end)
2190 if (!bkey_is_inode(k.k))
2193 BUG_ON(bch2_inode_unpack(k, &u));
2195 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2201 while ((cmp_int(link->inum, k.k->p.offset) ?:
2202 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2204 BUG_ON(link >= links->d + links->nr);
2207 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2208 "inode %llu has wrong i_nlink (type %u i_nlink %u, should be %u)",
2209 u.bi_inum, mode_to_type(u.bi_mode),
2210 bch2_inode_nlink_get(&u), link->count)) {
2211 bch2_inode_nlink_set(&u, link->count);
2213 ret = write_inode(&trans, &u, k.k->p.snapshot);
2215 bch_err(c, "error in fsck: error %i updating inode", ret);
2219 bch2_trans_iter_exit(&trans, &iter);
2220 bch2_trans_exit(&trans);
2223 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2229 static int check_nlinks(struct bch_fs *c)
2231 struct nlink_table links = { 0 };
2232 u64 this_iter_range_start, next_iter_range_start = 0;
2235 bch_verbose(c, "checking inode nlinks");
2238 this_iter_range_start = next_iter_range_start;
2239 next_iter_range_start = U64_MAX;
2241 ret = check_nlinks_find_hardlinks(c, &links,
2242 this_iter_range_start,
2243 &next_iter_range_start);
2245 ret = check_nlinks_walk_dirents(c, &links,
2246 this_iter_range_start,
2247 next_iter_range_start);
2251 ret = check_nlinks_update_hardlinks(c, &links,
2252 this_iter_range_start,
2253 next_iter_range_start);
2258 } while (next_iter_range_start != U64_MAX);
2265 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter)
2268 struct bkey_s_c_reflink_p p;
2269 struct bkey_i_reflink_p *u;
2272 k = bch2_btree_iter_peek(iter);
2280 if (k.k->type != KEY_TYPE_reflink_p)
2283 p = bkey_s_c_to_reflink_p(k);
2285 if (!p.v->front_pad && !p.v->back_pad)
2288 u = bch2_trans_kmalloc(trans, sizeof(*u));
2289 ret = PTR_ERR_OR_ZERO(u);
2293 bkey_reassemble(&u->k_i, k);
2297 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2301 static int fix_reflink_p(struct bch_fs *c)
2303 struct btree_trans trans;
2304 struct btree_iter iter;
2308 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2311 bch_verbose(c, "fixing reflink_p keys");
2313 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2315 for_each_btree_key(&trans, iter, BTREE_ID_extents, POS_MIN,
2317 BTREE_ITER_PREFETCH|
2318 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2319 if (k.k->type == KEY_TYPE_reflink_p) {
2320 ret = __bch2_trans_do(&trans, NULL, NULL,
2321 BTREE_INSERT_NOFAIL|
2322 BTREE_INSERT_LAZY_RW,
2323 fix_reflink_p_key(&trans, &iter));
2328 bch2_trans_iter_exit(&trans, &iter);
2330 bch2_trans_exit(&trans);
2335 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
2336 * Doesn't fix them yet, mainly because they haven't yet been observed:
2338 int bch2_fsck_full(struct bch_fs *c)
2340 return bch2_fs_snapshots_check(c) ?:
2341 check_inodes(c, true) ?:
2347 check_directory_structure(c) ?:
2352 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
2354 return check_inodes(c, false);