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 snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
100 return lockrestart_do(trans, __snapshot_lookup_subvol(trans, snapshot, subvol));
103 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
104 u32 *snapshot, u64 *inum)
106 struct bch_subvolume s;
109 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
111 *snapshot = le32_to_cpu(s.snapshot);
112 *inum = le64_to_cpu(s.inode);
116 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
117 u32 *snapshot, u64 *inum)
119 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
122 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
123 struct bch_inode_unpacked *inode,
126 struct btree_iter iter;
130 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
131 SPOS(0, inode_nr, *snapshot), 0);
132 k = bch2_btree_iter_peek_slot(&iter);
137 *snapshot = iter.pos.snapshot;
138 ret = k.k->type == KEY_TYPE_inode
139 ? bch2_inode_unpack(bkey_s_c_to_inode(k), inode)
142 bch2_trans_iter_exit(trans, &iter);
146 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
147 struct bch_inode_unpacked *inode,
150 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
153 static int __lookup_dirent(struct btree_trans *trans,
154 struct bch_hash_info hash_info,
155 subvol_inum dir, struct qstr *name,
156 u64 *target, unsigned *type)
158 struct btree_iter iter;
159 struct bkey_s_c_dirent d;
162 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
163 &hash_info, dir, name, 0);
167 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
168 *target = le64_to_cpu(d.v->d_inum);
170 bch2_trans_iter_exit(trans, &iter);
174 static int lookup_dirent(struct btree_trans *trans,
175 struct bch_hash_info hash_info,
176 subvol_inum dir, struct qstr *name,
177 u64 *target, unsigned *type)
179 return lockrestart_do(trans,
180 __lookup_dirent(trans, hash_info, dir, name, target, type));
183 static int __write_inode(struct btree_trans *trans,
184 struct bch_inode_unpacked *inode,
187 struct btree_iter iter;
190 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
191 SPOS(0, inode->bi_inum, snapshot),
194 ret = bch2_btree_iter_traverse(&iter) ?:
195 bch2_inode_write(trans, &iter, inode);
196 bch2_trans_iter_exit(trans, &iter);
200 static int write_inode(struct btree_trans *trans,
201 struct bch_inode_unpacked *inode,
204 int ret = __bch2_trans_do(trans, NULL, NULL,
206 BTREE_INSERT_LAZY_RW,
207 __write_inode(trans, inode, snapshot));
209 bch_err(trans->c, "error in fsck: error %i updating inode", ret);
213 static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
215 struct btree_iter iter = { NULL };
216 struct bkey_i_inode_generation delete;
217 struct bch_inode_unpacked inode_u;
221 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
222 SPOS(inum, 0, snapshot),
223 SPOS(inum, U64_MAX, snapshot),
225 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
226 SPOS(inum, 0, snapshot),
227 SPOS(inum, U64_MAX, snapshot),
229 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
230 SPOS(inum, 0, snapshot),
231 SPOS(inum, U64_MAX, snapshot),
236 bch2_trans_begin(trans);
238 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
239 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
240 k = bch2_btree_iter_peek_slot(&iter);
246 if (k.k->type != KEY_TYPE_inode) {
247 bch2_fs_inconsistent(trans->c,
248 "inode %llu:%u not found when deleting",
254 bch2_inode_unpack(bkey_s_c_to_inode(k), &inode_u);
256 /* Subvolume root? */
257 if (inode_u.bi_subvol) {
258 ret = bch2_subvolume_delete(trans, inode_u.bi_subvol, -1);
263 bkey_inode_generation_init(&delete.k_i);
264 delete.k.p = iter.pos;
265 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
267 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
268 bch2_trans_commit(trans, NULL, NULL,
269 BTREE_INSERT_NOFAIL);
271 bch2_trans_iter_exit(trans, &iter);
278 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
280 struct bch_fs *c = trans->c;
281 struct btree_iter iter;
282 struct bch_inode_unpacked dir_inode;
283 struct bch_hash_info dir_hash_info;
286 ret = lookup_inode(trans, pos.inode, &dir_inode, NULL);
290 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
292 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
294 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
295 &dir_hash_info, &iter, 0);
296 bch2_trans_iter_exit(trans, &iter);
300 static int remove_dirent(struct btree_trans *trans, struct bpos pos)
302 int ret = __bch2_trans_do(trans, NULL, NULL,
304 BTREE_INSERT_LAZY_RW,
305 __remove_dirent(trans, pos));
307 bch_err(trans->c, "remove_dirent: err %i deleting dirent", ret);
311 /* Get lost+found, create if it doesn't exist: */
312 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
313 struct bch_inode_unpacked *lostfound)
315 struct bch_fs *c = trans->c;
316 struct bch_inode_unpacked root;
317 struct bch_hash_info root_hash_info;
318 struct qstr lostfound_str = QSTR("lost+found");
319 subvol_inum root_inum = { .subvol = subvol };
325 ret = subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
329 ret = lookup_inode(trans, root_inum.inum, &root, &snapshot);
331 bch_err(c, "error fetching subvol root: %i", ret);
335 root_hash_info = bch2_hash_info_init(c, &root);
337 ret = lookup_dirent(trans, root_hash_info, root_inum,
338 &lostfound_str, &inum, &d_type);
339 if (ret == -ENOENT) {
340 bch_notice(c, "creating lost+found");
341 goto create_lostfound;
345 bch_err(c, "error looking up lost+found: %i", ret);
349 if (d_type != DT_DIR) {
350 bch_err(c, "error looking up lost+found: not a directory");
355 ret = lookup_inode(trans, inum, lostfound, &snapshot);
356 if (ret && ret != -ENOENT) {
358 * The check_dirents pass has already run, dangling dirents
359 * shouldn't exist here:
361 bch_err(c, "error looking up lost+found: %i", ret);
365 if (ret == -ENOENT) {
367 bch2_inode_init_early(c, lostfound);
369 ret = __bch2_trans_do(trans, NULL, NULL,
371 BTREE_INSERT_LAZY_RW,
372 bch2_create_trans(trans, root_inum, &root,
373 lostfound, &lostfound_str,
374 0, 0, S_IFDIR|0700, 0, NULL, NULL,
375 (subvol_inum) { }, 0));
377 bch_err(c, "error creating lost+found: %i", ret);
383 static int reattach_inode(struct btree_trans *trans,
384 struct bch_inode_unpacked *inode,
387 struct bch_hash_info dir_hash;
388 struct bch_inode_unpacked lostfound;
395 ret = snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
399 ret = lookup_lostfound(trans, subvol, &lostfound);
403 if (S_ISDIR(inode->bi_mode)) {
404 lostfound.bi_nlink++;
406 ret = write_inode(trans, &lostfound, U32_MAX);
411 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
413 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
414 name = (struct qstr) QSTR(name_buf);
416 ret = __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
417 bch2_dirent_create(trans,
420 .inum = lostfound.bi_inum,
423 mode_to_type(inode->bi_mode),
424 &name, inode->bi_inum, &dir_offset,
425 BCH_HASH_SET_MUST_CREATE));
427 bch_err(trans->c, "error %i reattaching inode %llu",
428 ret, inode->bi_inum);
432 inode->bi_dir = lostfound.bi_inum;
433 inode->bi_dir_offset = dir_offset;
435 return write_inode(trans, inode, inode_snapshot);
438 static int remove_backpointer(struct btree_trans *trans,
439 struct bch_inode_unpacked *inode)
441 struct btree_iter iter;
445 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
446 POS(inode->bi_dir, inode->bi_dir_offset), 0);
447 k = bch2_btree_iter_peek_slot(&iter);
451 if (k.k->type != KEY_TYPE_dirent) {
456 ret = remove_dirent(trans, k.k->p);
458 bch2_trans_iter_exit(trans, &iter);
462 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s, struct bpos pos)
464 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
466 if (bkey_cmp(s->pos, pos))
470 /* Might get called multiple times due to lock restarts */
471 if (s->nr && s->d[s->nr - 1] == pos.snapshot)
474 return snapshots_seen_add(c, s, pos.snapshot);
478 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
479 * and @ancestor hasn't been overwritten in @seen
481 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
483 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
484 u32 id, u32 ancestor)
488 BUG_ON(id > ancestor);
490 id = snapshot_t(c, id)->equiv;
491 ancestor = snapshot_t(c, ancestor)->equiv;
493 /* @ancestor should be the snapshot most recently added to @seen */
494 BUG_ON(!seen->nr || seen->d[seen->nr - 1] != ancestor);
495 BUG_ON(seen->pos.snapshot != ancestor);
500 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
503 for (i = seen->nr - 2;
504 i >= 0 && seen->d[i] >= id;
506 if (bch2_snapshot_is_ancestor(c, id, seen->d[i]) &&
507 bch2_snapshot_is_ancestor(c, seen->d[i], ancestor))
514 * ref_visible - given a key with snapshot id @src that points to a key with
515 * snapshot id @dst, test whether there is some snapshot in which @dst is
518 * This assumes we're visiting @src keys in natural key order.
520 * @s - list of snapshot IDs already seen at @src
521 * @src - snapshot ID of src key
522 * @dst - snapshot ID of dst key
524 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
528 ? key_visible_in_snapshot(c, s, dst, src)
529 : bch2_snapshot_is_ancestor(c, src, dst);
532 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
533 for (_i = (_w)->d; _i < (_w)->d + (_w)->nr && (_i)->snapshot <= (_snapshot); _i++)\
534 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
536 struct inode_walker {
537 bool first_this_inode;
542 struct inode_walker_entry {
543 struct bch_inode_unpacked inode;
549 static void inode_walker_exit(struct inode_walker *w)
555 static struct inode_walker inode_walker_init(void)
557 return (struct inode_walker) { 0, };
560 static int inode_walker_realloc(struct inode_walker *w)
562 if (w->nr == w->size) {
563 size_t new_size = max_t(size_t, 8UL, w->size * 2);
564 void *d = krealloc(w->d, new_size * sizeof(w->d[0]),
576 static int add_inode(struct bch_fs *c, struct inode_walker *w,
577 struct bkey_s_c_inode inode)
579 struct bch_inode_unpacked u;
582 ret = inode_walker_realloc(w);
586 BUG_ON(bch2_inode_unpack(inode, &u));
588 w->d[w->nr++] = (struct inode_walker_entry) {
590 .snapshot = snapshot_t(c, inode.k->p.snapshot)->equiv,
596 static int __walk_inode(struct btree_trans *trans,
597 struct inode_walker *w, struct bpos pos)
599 struct bch_fs *c = trans->c;
600 struct btree_iter iter;
602 unsigned i, ancestor_pos;
605 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
607 if (pos.inode == w->cur_inum) {
608 w->first_this_inode = false;
609 goto lookup_snapshot;
614 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, pos.inode),
615 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
616 if (k.k->p.offset != pos.inode)
619 if (k.k->type == KEY_TYPE_inode)
620 add_inode(c, w, bkey_s_c_to_inode(k));
622 bch2_trans_iter_exit(trans, &iter);
627 w->cur_inum = pos.inode;
628 w->first_this_inode = true;
630 for (i = 0; i < w->nr; i++)
631 if (bch2_snapshot_is_ancestor(c, pos.snapshot, w->d[i].snapshot))
635 BUG_ON(pos.snapshot > w->d[i].snapshot);
637 if (pos.snapshot != w->d[i].snapshot) {
640 while (i && w->d[i - 1].snapshot > pos.snapshot)
643 ret = inode_walker_realloc(w);
647 array_insert_item(w->d, w->nr, i, w->d[ancestor_pos]);
648 w->d[i].snapshot = pos.snapshot;
655 static int walk_inode(struct btree_trans *trans,
656 struct inode_walker *w, struct bpos pos)
658 return lockrestart_do(trans, __walk_inode(trans, w, pos));
661 static int __get_visible_inodes(struct btree_trans *trans,
662 struct inode_walker *w,
663 struct snapshots_seen *s,
666 struct bch_fs *c = trans->c;
667 struct btree_iter iter;
673 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
674 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
675 if (k.k->p.offset != inum)
678 if (k.k->type != KEY_TYPE_inode)
681 if (ref_visible(c, s, s->pos.snapshot, k.k->p.snapshot)) {
682 add_inode(c, w, bkey_s_c_to_inode(k));
683 if (k.k->p.snapshot >= s->pos.snapshot)
687 bch2_trans_iter_exit(trans, &iter);
692 static int check_key_has_snapshot(struct btree_trans *trans,
693 struct btree_iter *iter,
696 struct bch_fs *c = trans->c;
700 if (fsck_err_on(!snapshot_t(c, k.k->p.snapshot)->equiv, c,
701 "key in missing snapshot: %s",
702 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf))) {
703 ret = __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
704 bch2_btree_delete_at(trans, iter,
705 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE));
706 return ret ?: -EINTR;
712 static int hash_redo_key(struct btree_trans *trans,
713 const struct bch_hash_desc desc,
714 struct bch_hash_info *hash_info,
715 struct btree_iter *k_iter, struct bkey_s_c k)
717 bch_err(trans->c, "hash_redo_key() not implemented yet");
720 struct bkey_i *delete;
723 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
725 return PTR_ERR(delete);
727 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
731 bkey_reassemble(tmp, k);
733 bkey_init(&delete->k);
734 delete->k.p = k_iter->pos;
735 return bch2_btree_iter_traverse(k_iter) ?:
736 bch2_trans_update(trans, k_iter, delete, 0) ?:
737 bch2_hash_set(trans, desc, hash_info, k_iter->pos.inode, tmp, 0);
741 static int fsck_hash_delete_at(struct btree_trans *trans,
742 const struct bch_hash_desc desc,
743 struct bch_hash_info *info,
744 struct btree_iter *iter)
748 ret = bch2_hash_delete_at(trans, desc, info, iter, 0) ?:
749 bch2_trans_commit(trans, NULL, NULL,
751 BTREE_INSERT_LAZY_RW);
753 ret = bch2_btree_iter_traverse(iter);
761 static int hash_check_key(struct btree_trans *trans,
762 const struct bch_hash_desc desc,
763 struct bch_hash_info *hash_info,
764 struct btree_iter *k_iter, struct bkey_s_c hash_k)
766 struct bch_fs *c = trans->c;
767 struct btree_iter iter = { NULL };
773 if (hash_k.k->type != desc.key_type)
776 hash = desc.hash_bkey(hash_info, hash_k);
778 if (likely(hash == hash_k.k->p.offset))
781 if (hash_k.k->p.offset < hash)
784 for_each_btree_key(trans, iter, desc.btree_id, POS(hash_k.k->p.inode, hash),
785 BTREE_ITER_SLOTS, k, ret) {
786 if (!bkey_cmp(k.k->p, hash_k.k->p))
789 if (fsck_err_on(k.k->type == desc.key_type &&
790 !desc.cmp_bkey(k, hash_k), c,
791 "duplicate hash table keys:\n%s",
792 (bch2_bkey_val_to_text(&PBUF(buf), c,
794 ret = fsck_hash_delete_at(trans, desc, hash_info, k_iter);
801 if (bkey_deleted(k.k)) {
802 bch2_trans_iter_exit(trans, &iter);
807 bch2_trans_iter_exit(trans, &iter);
810 if (fsck_err(c, "hash table key at wrong offset: btree %u inode %llu offset %llu, "
811 "hashed to %llu\n%s",
812 desc.btree_id, hash_k.k->p.inode, hash_k.k->p.offset, hash,
813 (bch2_bkey_val_to_text(&PBUF(buf), c, hash_k), buf)) == FSCK_ERR_IGNORE)
816 ret = __bch2_trans_do(trans, NULL, NULL,
817 BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
818 hash_redo_key(trans, desc, hash_info, k_iter, hash_k));
820 bch_err(c, "hash_redo_key err %i", ret);
828 static int check_inode(struct btree_trans *trans,
829 struct btree_iter *iter,
830 struct bch_inode_unpacked *prev,
831 struct bch_inode_unpacked u)
833 struct bch_fs *c = trans->c;
834 bool do_update = false;
837 if (fsck_err_on(prev &&
838 (prev->bi_hash_seed != u.bi_hash_seed ||
839 mode_to_type(prev->bi_mode) != mode_to_type(u.bi_mode)), c,
840 "inodes in different snapshots don't match")) {
841 bch_err(c, "repair not implemented yet");
845 if (u.bi_flags & BCH_INODE_UNLINKED &&
847 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
849 bch2_trans_unlock(trans);
852 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
854 bch_err(c, "error in fsck: error %i while deleting inode", ret);
858 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
860 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
862 bch_verbose(c, "truncating inode %llu", u.bi_inum);
864 bch2_trans_unlock(trans);
868 * XXX: need to truncate partial blocks too here - or ideally
869 * just switch units to bytes and that issue goes away
871 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
872 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
874 POS(u.bi_inum, U64_MAX),
877 bch_err(c, "error in fsck: error %i truncating inode", ret);
882 * We truncated without our normal sector accounting hook, just
883 * make sure we recalculate it:
885 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
887 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
891 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
893 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
897 bch_verbose(c, "recounting sectors for inode %llu",
900 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
902 bch_err(c, "error in fsck: error %i recounting inode sectors",
907 u.bi_sectors = sectors;
908 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
912 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
915 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
920 ret = write_inode(trans, &u, iter->pos.snapshot);
922 bch_err(c, "error in fsck: error %i "
923 "updating inode", ret);
930 static int check_inodes(struct bch_fs *c, bool full)
932 struct btree_trans trans;
933 struct btree_iter iter;
935 struct bkey_s_c_inode inode;
936 struct bch_inode_unpacked prev, u;
939 memset(&prev, 0, sizeof(prev));
941 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
943 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
946 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
947 ret = check_key_has_snapshot(&trans, &iter, k);
952 * if snapshot id isn't a leaf node, skip it - deletion in
953 * particular is not atomic, so on the internal snapshot nodes
954 * we can see inodes marked for deletion after a clean shutdown
956 if (bch2_snapshot_internal_node(c, k.k->p.snapshot))
959 if (k.k->type != KEY_TYPE_inode)
962 inode = bkey_s_c_to_inode(k);
965 !(inode.v->bi_flags & (BCH_INODE_I_SIZE_DIRTY|
966 BCH_INODE_I_SECTORS_DIRTY|
967 BCH_INODE_UNLINKED)))
970 BUG_ON(bch2_inode_unpack(inode, &u));
972 ret = check_inode(&trans, &iter,
973 full && prev.bi_inum == u.bi_inum
980 bch2_trans_iter_exit(&trans, &iter);
982 BUG_ON(ret == -EINTR);
984 bch2_trans_exit(&trans);
989 static int check_subvols(struct bch_fs *c)
991 struct btree_trans trans;
992 struct btree_iter iter;
996 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
998 for_each_btree_key(&trans, iter, BTREE_ID_subvolumes, POS_MIN,
1001 bch2_trans_iter_exit(&trans, &iter);
1003 bch2_trans_exit(&trans);
1008 * Checking for overlapping extents needs to be reimplemented
1011 static int fix_overlapping_extent(struct btree_trans *trans,
1012 struct bkey_s_c k, struct bpos cut_at)
1014 struct btree_iter iter;
1018 u = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
1019 ret = PTR_ERR_OR_ZERO(u);
1023 bkey_reassemble(u, k);
1024 bch2_cut_front(cut_at, u);
1028 * We don't want to go through the extent_handle_overwrites path:
1030 * XXX: this is going to screw up disk accounting, extent triggers
1031 * assume things about extent overwrites - we should be running the
1032 * triggers manually here
1034 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, u->k.p,
1035 BTREE_ITER_INTENT|BTREE_ITER_NOT_EXTENTS);
1037 BUG_ON(iter.flags & BTREE_ITER_IS_EXTENTS);
1038 ret = bch2_btree_iter_traverse(&iter) ?:
1039 bch2_trans_update(trans, &iter, u, BTREE_TRIGGER_NORUN) ?:
1040 bch2_trans_commit(trans, NULL, NULL,
1041 BTREE_INSERT_NOFAIL|
1042 BTREE_INSERT_LAZY_RW);
1043 bch2_trans_iter_exit(trans, &iter);
1048 static int inode_backpointer_exists(struct btree_trans *trans,
1049 struct bch_inode_unpacked *inode,
1052 struct btree_iter iter;
1054 u32 target_subvol, target_snapshot;
1058 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
1059 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot), 0);
1060 k = bch2_btree_iter_peek_slot(&iter);
1064 if (k.k->type != KEY_TYPE_dirent)
1067 ret = __bch2_dirent_read_target(trans, bkey_s_c_to_dirent(k),
1075 ret = target_inum == inode->bi_inum;
1077 bch2_trans_iter_exit(trans, &iter);
1081 static bool inode_backpointer_matches(struct bkey_s_c_dirent d,
1082 struct bch_inode_unpacked *inode)
1084 return d.k->p.inode == inode->bi_dir &&
1085 d.k->p.offset == inode->bi_dir_offset;
1088 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1090 struct bch_fs *c = trans->c;
1091 struct inode_walker_entry *i;
1092 int ret = 0, ret2 = 0;
1095 for (i = w->d; i < w->d + w->nr; i++) {
1096 if (i->inode.bi_sectors == i->count)
1099 count2 = lockrestart_do(trans,
1100 bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot));
1102 if (i->count != count2) {
1103 bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1106 if (i->inode.bi_sectors == i->count)
1110 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1111 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1112 w->cur_inum, i->snapshot,
1113 i->inode.bi_sectors, i->count) == FSCK_ERR_IGNORE)
1116 i->inode.bi_sectors = i->count;
1117 ret = write_inode(trans, &i->inode, i->snapshot);
1126 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1127 struct inode_walker *inode,
1128 struct snapshots_seen *s)
1130 struct bch_fs *c = trans->c;
1132 struct inode_walker_entry *i;
1136 k = bch2_btree_iter_peek(iter);
1144 ret = check_key_has_snapshot(trans, iter, k);
1148 ret = snapshots_seen_update(c, s, k.k->p);
1152 if (k.k->type == KEY_TYPE_whiteout)
1155 if (inode->cur_inum != k.k->p.inode) {
1156 ret = check_i_sectors(trans, inode);
1161 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
1165 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
1166 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
1168 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2))
1169 return fix_overlapping_extent(trans, k, prev.k->k.p) ?: -EINTR;
1172 ret = __walk_inode(trans, inode, k.k->p);
1176 if (fsck_err_on(ret == INT_MAX, c,
1177 "extent in missing inode:\n %s",
1178 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1179 return __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
1180 bch2_btree_delete_at(trans, iter,
1181 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE));
1189 if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1190 !S_ISLNK(i->inode.bi_mode), c,
1191 "extent in non regular inode mode %o:\n %s",
1193 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1194 return __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
1195 bch2_btree_delete_at(trans, iter,
1196 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE));
1198 if (!bch2_snapshot_internal_node(c, k.k->p.snapshot)) {
1199 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i) {
1200 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1201 k.k->type != KEY_TYPE_reservation &&
1202 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9, c,
1203 "extent type %u offset %llu past end of inode %llu, i_size %llu",
1204 k.k->type, k.k->p.offset, k.k->p.inode, i->inode.bi_size)) {
1206 return bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1207 SPOS(k.k->p.inode, round_up(i->inode.bi_size, block_bytes(c)) >> 9,
1209 POS(k.k->p.inode, U64_MAX),
1215 if (bkey_extent_is_allocation(k.k))
1216 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i)
1217 i->count += k.k->size;
1219 bch2_bkey_buf_reassemble(&prev, c, k);
1227 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1228 * that i_size an i_sectors are consistent
1231 static int check_extents(struct bch_fs *c)
1233 struct inode_walker w = inode_walker_init();
1234 struct snapshots_seen s;
1235 struct btree_trans trans;
1236 struct btree_iter iter;
1240 struct bkey_buf prev;
1241 bch2_bkey_buf_init(&prev);
1242 prev.k->k = KEY(0, 0, 0);
1244 snapshots_seen_init(&s);
1245 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1247 bch_verbose(c, "checking extents");
1249 bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
1250 POS(BCACHEFS_ROOT_INO, 0),
1252 BTREE_ITER_PREFETCH|
1253 BTREE_ITER_ALL_SNAPSHOTS);
1256 ret = lockrestart_do(&trans,
1257 check_extent(&trans, &iter, &w, &s));
1260 } while (bch2_btree_iter_advance(&iter));
1261 bch2_trans_iter_exit(&trans, &iter);
1263 bch2_bkey_buf_exit(&prev, c);
1265 inode_walker_exit(&w);
1266 bch2_trans_exit(&trans);
1267 snapshots_seen_exit(&s);
1272 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1274 struct bch_fs *c = trans->c;
1275 struct inode_walker_entry *i;
1276 int ret = 0, ret2 = 0;
1279 for (i = w->d; i < w->d + w->nr; i++) {
1280 if (i->inode.bi_nlink == i->count)
1283 count2 = lockrestart_do(trans,
1284 bch2_count_subdirs(trans, w->cur_inum, i->snapshot));
1286 if (i->count != count2) {
1287 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1290 if (i->inode.bi_nlink == i->count)
1294 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1295 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1296 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1297 i->inode.bi_nlink = i->count;
1298 ret = write_inode(trans, &i->inode, i->snapshot);
1308 static int check_dirent_target(struct btree_trans *trans,
1309 struct btree_iter *iter,
1310 struct bkey_s_c_dirent d,
1311 struct bch_inode_unpacked *target,
1312 u32 target_snapshot)
1314 struct bch_fs *c = trans->c;
1315 bool backpointer_exists = true;
1319 if (!target->bi_dir &&
1320 !target->bi_dir_offset) {
1321 target->bi_dir = d.k->p.inode;
1322 target->bi_dir_offset = d.k->p.offset;
1324 ret = write_inode(trans, target, target_snapshot);
1329 if (!inode_backpointer_matches(d, target)) {
1330 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1334 backpointer_exists = ret;
1337 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1338 backpointer_exists, c,
1339 "directory %llu with multiple links",
1341 ret = remove_dirent(trans, d.k->p);
1347 if (fsck_err_on(backpointer_exists &&
1348 !target->bi_nlink, c,
1349 "inode %llu has multiple links but i_nlink 0",
1352 target->bi_flags &= ~BCH_INODE_UNLINKED;
1354 ret = write_inode(trans, target, target_snapshot);
1359 if (fsck_err_on(!backpointer_exists, c,
1360 "inode %llu:%u has wrong backpointer:\n"
1362 "should be %llu:%llu",
1363 target->bi_inum, target_snapshot,
1365 target->bi_dir_offset,
1368 target->bi_dir = d.k->p.inode;
1369 target->bi_dir_offset = d.k->p.offset;
1371 ret = write_inode(trans, target, target_snapshot);
1377 if (fsck_err_on(vfs_d_type(d.v->d_type) != mode_to_type(target->bi_mode), c,
1378 "incorrect d_type: should be %u:\n%s",
1379 mode_to_type(target->bi_mode),
1380 (bch2_bkey_val_to_text(&PBUF(buf), c, d.s_c), buf))) {
1381 struct bkey_i_dirent *n;
1383 n = kmalloc(bkey_bytes(d.k), GFP_KERNEL);
1389 bkey_reassemble(&n->k_i, d.s_c);
1390 n->v.d_type = mode_to_type(target->bi_mode);
1392 ret = __bch2_trans_do(trans, NULL, NULL,
1393 BTREE_INSERT_NOFAIL|
1394 BTREE_INSERT_LAZY_RW,
1395 bch2_trans_update(trans, iter, &n->k_i, 0));
1405 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1406 struct bch_hash_info *hash_info,
1407 struct inode_walker *dir,
1408 struct inode_walker *target,
1409 struct snapshots_seen *s)
1411 struct bch_fs *c = trans->c;
1413 struct bkey_s_c_dirent d;
1414 struct inode_walker_entry *i;
1415 u32 target_snapshot;
1421 k = bch2_btree_iter_peek(iter);
1429 ret = check_key_has_snapshot(trans, iter, k);
1433 ret = snapshots_seen_update(c, s, k.k->p);
1437 if (k.k->type == KEY_TYPE_whiteout)
1440 if (dir->cur_inum != k.k->p.inode) {
1441 ret = check_subdir_count(trans, dir);
1446 ret = __walk_inode(trans, dir, k.k->p);
1450 if (fsck_err_on(ret == INT_MAX, c,
1451 "dirent in nonexisting directory:\n%s",
1452 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1453 return __bch2_trans_do(trans, NULL, NULL, BTREE_INSERT_LAZY_RW,
1454 bch2_btree_delete_at(trans, iter,
1455 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE));
1463 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1464 "dirent in non directory inode type %u:\n%s",
1465 mode_to_type(i->inode.bi_mode),
1466 (bch2_bkey_val_to_text(&PBUF(buf), c, k), buf)))
1467 return __bch2_trans_do(trans, NULL, NULL, 0,
1468 bch2_btree_delete_at(trans, iter, 0));
1470 if (dir->first_this_inode)
1471 *hash_info = bch2_hash_info_init(c, &dir->d[0].inode);
1473 ret = hash_check_key(trans, bch2_dirent_hash_desc,
1474 hash_info, iter, k);
1477 if (ret) /* dirent has been deleted */
1480 if (k.k->type != KEY_TYPE_dirent)
1483 d = bkey_s_c_to_dirent(k);
1485 ret = __bch2_dirent_read_target(trans, d,
1490 if (ret && ret != -ENOENT)
1493 if (fsck_err_on(ret, c,
1494 "dirent points to missing subvolume %llu",
1495 le64_to_cpu(d.v->d_inum)))
1496 return remove_dirent(trans, d.k->p);
1498 if (target_subvol) {
1499 struct bch_inode_unpacked subvol_root;
1501 ret = __lookup_inode(trans, target_inum,
1502 &subvol_root, &target_snapshot);
1503 if (ret && ret != -ENOENT)
1506 if (fsck_err_on(ret, c,
1507 "subvolume %u points to missing subvolume root %llu",
1510 bch_err(c, "repair not implemented yet");
1514 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1515 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1517 subvol_root.bi_subvol, target_subvol)) {
1518 subvol_root.bi_subvol = target_subvol;
1519 ret = write_inode(trans, &subvol_root, target_snapshot);
1524 ret = check_dirent_target(trans, iter, d, &subvol_root,
1529 ret = __get_visible_inodes(trans, target, s, target_inum);
1533 if (fsck_err_on(!target->nr, c,
1534 "dirent points to missing inode:\n%s",
1535 (bch2_bkey_val_to_text(&PBUF(buf), c,
1537 ret = remove_dirent(trans, d.k->p);
1542 for (i = target->d; i < target->d + target->nr; i++) {
1543 ret = check_dirent_target(trans, iter, d,
1544 &i->inode, i->snapshot);
1550 if (d.v->d_type == DT_DIR)
1551 for_each_visible_inode(c, s, dir, d.k->p.snapshot, i)
1559 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1563 static int check_dirents(struct bch_fs *c)
1565 struct inode_walker dir = inode_walker_init();
1566 struct inode_walker target = inode_walker_init();
1567 struct snapshots_seen s;
1568 struct bch_hash_info hash_info;
1569 struct btree_trans trans;
1570 struct btree_iter iter;
1573 bch_verbose(c, "checking dirents");
1575 snapshots_seen_init(&s);
1576 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1578 bch2_trans_iter_init(&trans, &iter, BTREE_ID_dirents,
1579 POS(BCACHEFS_ROOT_INO, 0),
1581 BTREE_ITER_PREFETCH|
1582 BTREE_ITER_ALL_SNAPSHOTS);
1585 ret = lockrestart_do(&trans,
1586 check_dirent(&trans, &iter, &hash_info,
1587 &dir, &target, &s));
1590 } while (bch2_btree_iter_advance(&iter));
1591 bch2_trans_iter_exit(&trans, &iter);
1593 bch2_trans_exit(&trans);
1594 snapshots_seen_exit(&s);
1595 inode_walker_exit(&dir);
1596 inode_walker_exit(&target);
1601 * Walk xattrs: verify that they all have a corresponding inode
1604 static int check_xattrs(struct bch_fs *c)
1606 struct inode_walker w = inode_walker_init();
1607 struct bch_hash_info hash_info;
1608 struct btree_trans trans;
1609 struct btree_iter iter;
1613 bch_verbose(c, "checking xattrs");
1615 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1617 bch2_trans_iter_init(&trans, &iter, BTREE_ID_xattrs,
1618 POS(BCACHEFS_ROOT_INO, 0),
1620 BTREE_ITER_PREFETCH|
1621 BTREE_ITER_ALL_SNAPSHOTS);
1623 bch2_trans_begin(&trans);
1625 while ((k = bch2_btree_iter_peek(&iter)).k &&
1626 !(ret = bkey_err(k))) {
1627 ret = check_key_has_snapshot(&trans, &iter, k);
1631 ret = walk_inode(&trans, &w, k.k->p);
1635 if (fsck_err_on(ret == INT_MAX, c,
1636 "xattr for missing inode %llu",
1638 ret = bch2_btree_delete_at(&trans, &iter, 0);
1648 if (w.first_this_inode)
1649 hash_info = bch2_hash_info_init(c, &w.d[0].inode);
1651 ret = hash_check_key(&trans, bch2_xattr_hash_desc,
1652 &hash_info, &iter, k);
1656 bch2_btree_iter_advance(&iter);
1662 bch2_trans_iter_exit(&trans, &iter);
1663 bch2_trans_exit(&trans);
1667 /* Get root directory, create if it doesn't exist: */
1668 static int check_root(struct bch_fs *c)
1670 struct btree_trans trans;
1671 struct bch_inode_unpacked root_inode;
1676 bch2_trans_init(&trans, c, 0, 0);
1678 bch_verbose(c, "checking root directory");
1680 ret = subvol_lookup(&trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1681 if (ret && ret != -ENOENT)
1684 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1685 struct bkey_i_subvolume root_subvol;
1688 inum = BCACHEFS_ROOT_INO;
1690 bkey_subvolume_init(&root_subvol.k_i);
1691 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1692 root_subvol.v.flags = 0;
1693 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1694 root_subvol.v.inode = cpu_to_le64(inum);
1695 ret = __bch2_trans_do(&trans, NULL, NULL,
1696 BTREE_INSERT_NOFAIL|
1697 BTREE_INSERT_LAZY_RW,
1698 __bch2_btree_insert(&trans, BTREE_ID_subvolumes, &root_subvol.k_i));
1700 bch_err(c, "error writing root subvol: %i", ret);
1706 ret = lookup_inode(&trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1707 if (ret && ret != -ENOENT)
1710 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1711 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1712 "root inode not a directory")) {
1713 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1715 root_inode.bi_inum = inum;
1717 ret = write_inode(&trans, &root_inode, snapshot);
1719 bch_err(c, "error writing root inode: %i", ret);
1723 bch2_trans_exit(&trans);
1731 struct pathbuf_entry {
1737 static bool path_is_dup(struct pathbuf *p, u64 inum, u32 snapshot)
1739 struct pathbuf_entry *i;
1741 for (i = p->entries; i < p->entries + p->nr; i++)
1742 if (i->inum == inum &&
1743 i->snapshot == snapshot)
1749 static int path_down(struct pathbuf *p, u64 inum, u32 snapshot)
1751 if (p->nr == p->size) {
1752 size_t new_size = max_t(size_t, 256UL, p->size * 2);
1753 void *n = krealloc(p->entries,
1754 new_size * sizeof(p->entries[0]),
1764 p->entries[p->nr++] = (struct pathbuf_entry) {
1766 .snapshot = snapshot,
1772 * Check that a given inode is reachable from the root:
1774 * XXX: we should also be verifying that inodes are in the right subvolumes
1776 static int check_path(struct btree_trans *trans,
1778 struct bch_inode_unpacked *inode,
1781 struct bch_fs *c = trans->c;
1784 snapshot = snapshot_t(c, snapshot)->equiv;
1787 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1788 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1789 u32 parent_snapshot = snapshot;
1791 if (inode->bi_parent_subvol) {
1794 ret = subvol_lookup(trans, inode->bi_parent_subvol,
1795 &parent_snapshot, &inum);
1800 ret = lockrestart_do(trans,
1801 inode_backpointer_exists(trans, inode, parent_snapshot));
1806 if (fsck_err(c, "unreachable inode %llu:%u, type %u nlink %u backptr %llu:%llu",
1807 inode->bi_inum, snapshot,
1808 mode_to_type(inode->bi_mode),
1811 inode->bi_dir_offset))
1812 ret = reattach_inode(trans, inode, snapshot);
1817 if (!S_ISDIR(inode->bi_mode))
1820 ret = path_down(p, inode->bi_inum, snapshot);
1822 bch_err(c, "memory allocation failure");
1826 snapshot = parent_snapshot;
1828 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1830 /* Should have been caught in dirents pass */
1831 bch_err(c, "error looking up parent directory: %i", ret);
1835 if (path_is_dup(p, inode->bi_inum, snapshot)) {
1836 struct pathbuf_entry *i;
1838 /* XXX print path */
1839 bch_err(c, "directory structure loop");
1841 for (i = p->entries; i < p->entries + p->nr; i++)
1842 pr_err("%llu:%u", i->inum, i->snapshot);
1843 pr_err("%llu:%u", inode->bi_inum, snapshot);
1845 if (!fsck_err(c, "directory structure loop"))
1848 ret = lockrestart_do(trans,
1849 remove_backpointer(trans, inode));
1851 bch_err(c, "error removing dirent: %i", ret);
1855 ret = reattach_inode(trans, inode, snapshot);
1860 bch_err(c, "%s: err %i", __func__, ret);
1865 * Check for unreachable inodes, as well as loops in the directory structure:
1866 * After check_dirents(), if an inode backpointer doesn't exist that means it's
1869 static int check_directory_structure(struct bch_fs *c)
1871 struct btree_trans trans;
1872 struct btree_iter iter;
1874 struct bch_inode_unpacked u;
1875 struct pathbuf path = { 0, 0, NULL };
1878 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1880 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
1882 BTREE_ITER_PREFETCH|
1883 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
1884 if (k.k->type != KEY_TYPE_inode)
1887 ret = bch2_inode_unpack(bkey_s_c_to_inode(k), &u);
1889 /* Should have been caught earlier in fsck: */
1890 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
1894 if (u.bi_flags & BCH_INODE_UNLINKED)
1897 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
1901 bch2_trans_iter_exit(&trans, &iter);
1903 BUG_ON(ret == -EINTR);
1905 kfree(path.entries);
1907 bch2_trans_exit(&trans);
1911 struct nlink_table {
1922 static int add_nlink(struct nlink_table *t, u64 inum, u32 snapshot)
1924 if (t->nr == t->size) {
1925 size_t new_size = max_t(size_t, 128UL, t->size * 2);
1926 void *d = kvmalloc(new_size * sizeof(t->d[0]), GFP_KERNEL);
1932 memcpy(d, t->d, t->size * sizeof(t->d[0]));
1940 t->d[t->nr++] = (struct nlink) {
1942 .snapshot = snapshot,
1948 static int nlink_cmp(const void *_l, const void *_r)
1950 const struct nlink *l = _l;
1951 const struct nlink *r = _r;
1953 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
1956 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
1957 struct nlink_table *links,
1958 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
1960 struct nlink *link, key = {
1961 .inum = inum, .snapshot = U32_MAX,
1964 if (inum < range_start || inum >= range_end)
1967 link = __inline_bsearch(&key, links->d, links->nr,
1968 sizeof(links->d[0]), nlink_cmp);
1972 while (link > links->d && link[0].inum == link[-1].inum)
1975 for (; link < links->d + links->nr && link->inum == inum; link++)
1976 if (ref_visible(c, s, snapshot, link->snapshot)) {
1978 if (link->snapshot >= snapshot)
1984 static int check_nlinks_find_hardlinks(struct bch_fs *c,
1985 struct nlink_table *t,
1986 u64 start, u64 *end)
1988 struct btree_trans trans;
1989 struct btree_iter iter;
1991 struct bkey_s_c_inode inode;
1992 struct bch_inode_unpacked u;
1995 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1997 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2000 BTREE_ITER_PREFETCH|
2001 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2002 if (k.k->type != KEY_TYPE_inode)
2005 inode = bkey_s_c_to_inode(k);
2008 * Backpointer and directory structure checks are sufficient for
2009 * directories, since they can't have hardlinks:
2011 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
2014 /* Should never fail, checked by bch2_inode_invalid: */
2015 BUG_ON(bch2_inode_unpack(inode, &u));
2020 ret = add_nlink(t, k.k->p.offset, k.k->p.snapshot);
2022 *end = k.k->p.offset;
2028 bch2_trans_iter_exit(&trans, &iter);
2029 bch2_trans_exit(&trans);
2032 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2038 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2039 u64 range_start, u64 range_end)
2041 struct btree_trans trans;
2042 struct snapshots_seen s;
2043 struct btree_iter iter;
2045 struct bkey_s_c_dirent d;
2048 snapshots_seen_init(&s);
2050 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2052 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2054 BTREE_ITER_PREFETCH|
2055 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2056 ret = snapshots_seen_update(c, &s, k.k->p);
2060 switch (k.k->type) {
2061 case KEY_TYPE_dirent:
2062 d = bkey_s_c_to_dirent(k);
2064 if (d.v->d_type != DT_DIR &&
2065 d.v->d_type != DT_SUBVOL)
2066 inc_link(c, &s, links, range_start, range_end,
2067 le64_to_cpu(d.v->d_inum),
2072 bch2_trans_cond_resched(&trans);
2074 bch2_trans_iter_exit(&trans, &iter);
2077 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2079 bch2_trans_exit(&trans);
2080 snapshots_seen_exit(&s);
2085 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2086 struct nlink_table *links,
2087 u64 range_start, u64 range_end)
2089 struct btree_trans trans;
2090 struct btree_iter iter;
2092 struct bkey_s_c_inode inode;
2093 struct bch_inode_unpacked u;
2094 struct nlink *link = links->d;
2097 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2099 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2100 POS(0, range_start),
2102 BTREE_ITER_PREFETCH|
2103 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2104 if (k.k->p.offset >= range_end)
2107 if (k.k->type != KEY_TYPE_inode)
2110 inode = bkey_s_c_to_inode(k);
2111 if (S_ISDIR(le16_to_cpu(inode.v->bi_mode)))
2114 BUG_ON(bch2_inode_unpack(inode, &u));
2119 while ((cmp_int(link->inum, k.k->p.offset) ?:
2120 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2122 BUG_ON(link >= links->d + links->nr);
2125 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2126 "inode %llu has wrong i_nlink (type %u i_nlink %u, should be %u)",
2127 u.bi_inum, mode_to_type(u.bi_mode),
2128 bch2_inode_nlink_get(&u), link->count)) {
2129 bch2_inode_nlink_set(&u, link->count);
2131 ret = write_inode(&trans, &u, k.k->p.snapshot);
2133 bch_err(c, "error in fsck: error %i updating inode", ret);
2137 bch2_trans_iter_exit(&trans, &iter);
2138 bch2_trans_exit(&trans);
2141 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2147 static int check_nlinks(struct bch_fs *c)
2149 struct nlink_table links = { 0 };
2150 u64 this_iter_range_start, next_iter_range_start = 0;
2153 bch_verbose(c, "checking inode nlinks");
2156 this_iter_range_start = next_iter_range_start;
2157 next_iter_range_start = U64_MAX;
2159 ret = check_nlinks_find_hardlinks(c, &links,
2160 this_iter_range_start,
2161 &next_iter_range_start);
2163 ret = check_nlinks_walk_dirents(c, &links,
2164 this_iter_range_start,
2165 next_iter_range_start);
2169 ret = check_nlinks_update_hardlinks(c, &links,
2170 this_iter_range_start,
2171 next_iter_range_start);
2176 } while (next_iter_range_start != U64_MAX);
2183 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter)
2186 struct bkey_s_c_reflink_p p;
2187 struct bkey_i_reflink_p *u;
2190 k = bch2_btree_iter_peek(iter);
2198 if (k.k->type != KEY_TYPE_reflink_p)
2201 p = bkey_s_c_to_reflink_p(k);
2203 if (!p.v->front_pad && !p.v->back_pad)
2206 u = bch2_trans_kmalloc(trans, sizeof(*u));
2207 ret = PTR_ERR_OR_ZERO(u);
2211 bkey_reassemble(&u->k_i, k);
2215 return bch2_trans_update(trans, iter, &u->k_i, 0);
2218 static int fix_reflink_p(struct bch_fs *c)
2220 struct btree_trans trans;
2221 struct btree_iter iter;
2225 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2228 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2230 for_each_btree_key(&trans, iter, BTREE_ID_extents, POS_MIN,
2232 BTREE_ITER_PREFETCH|
2233 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2234 if (k.k->type == KEY_TYPE_reflink_p) {
2235 ret = __bch2_trans_do(&trans, NULL, NULL,
2236 BTREE_INSERT_NOFAIL|
2237 BTREE_INSERT_LAZY_RW,
2238 fix_reflink_p_key(&trans, &iter));
2243 bch2_trans_iter_exit(&trans, &iter);
2245 bch2_trans_exit(&trans);
2250 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
2251 * Doesn't fix them yet, mainly because they haven't yet been observed:
2253 int bch2_fsck_full(struct bch_fs *c)
2255 return bch2_fs_snapshots_check(c) ?:
2256 check_inodes(c, true) ?:
2262 check_directory_structure(c) ?:
2267 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
2269 return check_inodes(c, false);