1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
13 #include "subvolume.h"
17 #include <linux/bsearch.h>
18 #include <linux/dcache.h> /* struct qstr */
20 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
22 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
25 struct btree_iter iter;
30 for_each_btree_key(trans, iter, BTREE_ID_extents,
31 SPOS(inum, 0, snapshot), 0, k, ret) {
32 if (k.k->p.inode != inum)
35 if (bkey_extent_is_allocation(k.k))
39 bch2_trans_iter_exit(trans, &iter);
41 return ret ?: sectors;
44 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
47 struct btree_iter iter;
49 struct bkey_s_c_dirent d;
53 for_each_btree_key(trans, iter, BTREE_ID_dirents,
54 SPOS(inum, 0, snapshot), 0, k, ret) {
55 if (k.k->p.inode != inum)
58 if (k.k->type != KEY_TYPE_dirent)
61 d = bkey_s_c_to_dirent(k);
62 if (d.v->d_type == DT_DIR)
66 bch2_trans_iter_exit(trans, &iter);
68 return ret ?: subdirs;
71 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
74 struct btree_iter iter;
78 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
80 k = bch2_btree_iter_peek_slot(&iter);
85 if (k.k->type != KEY_TYPE_snapshot) {
86 bch_err(trans->c, "snapshot %u not fonud", snapshot);
91 *subvol = le32_to_cpu(bkey_s_c_to_snapshot(k).v->subvol);
93 bch2_trans_iter_exit(trans, &iter);
98 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
99 u32 *snapshot, u64 *inum)
101 struct bch_subvolume s;
104 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
106 *snapshot = le32_to_cpu(s.snapshot);
107 *inum = le64_to_cpu(s.inode);
111 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
112 u32 *snapshot, u64 *inum)
114 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
117 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
118 struct bch_inode_unpacked *inode)
120 struct btree_iter iter;
124 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
126 BTREE_ITER_ALL_SNAPSHOTS);
127 k = bch2_btree_iter_peek(&iter);
132 if (!k.k || bkey_cmp(k.k->p, POS(0, inode_nr))) {
137 ret = bch2_inode_unpack(k, inode);
139 if (ret && ret != -EINTR)
140 bch_err(trans->c, "error %i fetching inode %llu",
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 struct btree_iter iter;
154 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
155 SPOS(0, inode_nr, *snapshot), 0);
156 k = bch2_btree_iter_peek_slot(&iter);
161 ret = bkey_is_inode(k.k)
162 ? bch2_inode_unpack(k, inode)
165 *snapshot = iter.pos.snapshot;
167 if (ret && ret != -EINTR)
168 bch_err(trans->c, "error %i fetching inode %llu:%u",
169 ret, inode_nr, *snapshot);
170 bch2_trans_iter_exit(trans, &iter);
174 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
175 struct bch_inode_unpacked *inode,
178 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
181 static int __lookup_dirent(struct btree_trans *trans,
182 struct bch_hash_info hash_info,
183 subvol_inum dir, struct qstr *name,
184 u64 *target, unsigned *type)
186 struct btree_iter iter;
187 struct bkey_s_c_dirent d;
190 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
191 &hash_info, dir, name, 0);
195 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
196 *target = le64_to_cpu(d.v->d_inum);
198 bch2_trans_iter_exit(trans, &iter);
202 static int __write_inode(struct btree_trans *trans,
203 struct bch_inode_unpacked *inode,
206 struct btree_iter iter;
209 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
210 SPOS(0, inode->bi_inum, snapshot),
213 ret = bch2_btree_iter_traverse(&iter) ?:
214 bch2_inode_write(trans, &iter, inode);
215 bch2_trans_iter_exit(trans, &iter);
219 static int write_inode(struct btree_trans *trans,
220 struct bch_inode_unpacked *inode,
223 int ret = __bch2_trans_do(trans, NULL, NULL,
225 BTREE_INSERT_LAZY_RW,
226 __write_inode(trans, inode, snapshot));
228 bch_err(trans->c, "error in fsck: error %i updating inode", ret);
232 static int fsck_inode_rm(struct btree_trans *trans, u64 inum, u32 snapshot)
234 struct btree_iter iter = { NULL };
235 struct bkey_i_inode_generation delete;
236 struct bch_inode_unpacked inode_u;
240 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
241 SPOS(inum, 0, snapshot),
242 SPOS(inum, U64_MAX, snapshot),
244 bch2_btree_delete_range_trans(trans, BTREE_ID_dirents,
245 SPOS(inum, 0, snapshot),
246 SPOS(inum, U64_MAX, snapshot),
248 bch2_btree_delete_range_trans(trans, BTREE_ID_xattrs,
249 SPOS(inum, 0, snapshot),
250 SPOS(inum, U64_MAX, snapshot),
255 bch2_trans_begin(trans);
257 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
258 SPOS(0, inum, snapshot), BTREE_ITER_INTENT);
259 k = bch2_btree_iter_peek_slot(&iter);
265 if (!bkey_is_inode(k.k)) {
266 bch2_fs_inconsistent(trans->c,
267 "inode %llu:%u not found when deleting",
273 bch2_inode_unpack(k, &inode_u);
275 /* Subvolume root? */
276 if (inode_u.bi_subvol) {
277 ret = bch2_subvolume_delete(trans, inode_u.bi_subvol);
282 bkey_inode_generation_init(&delete.k_i);
283 delete.k.p = iter.pos;
284 delete.v.bi_generation = cpu_to_le32(inode_u.bi_generation + 1);
286 ret = bch2_trans_update(trans, &iter, &delete.k_i, 0) ?:
287 bch2_trans_commit(trans, NULL, NULL,
288 BTREE_INSERT_NOFAIL);
290 bch2_trans_iter_exit(trans, &iter);
297 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
299 struct bch_fs *c = trans->c;
300 struct btree_iter iter;
301 struct bch_inode_unpacked dir_inode;
302 struct bch_hash_info dir_hash_info;
305 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
309 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
311 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
313 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
314 &dir_hash_info, &iter, 0);
315 bch2_trans_iter_exit(trans, &iter);
319 /* Get lost+found, create if it doesn't exist: */
320 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
321 struct bch_inode_unpacked *lostfound)
323 struct bch_fs *c = trans->c;
324 struct bch_inode_unpacked root;
325 struct bch_hash_info root_hash_info;
326 struct qstr lostfound_str = QSTR("lost+found");
327 subvol_inum root_inum = { .subvol = subvol };
333 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
337 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
341 root_hash_info = bch2_hash_info_init(c, &root);
343 ret = __lookup_dirent(trans, root_hash_info, root_inum,
344 &lostfound_str, &inum, &d_type);
345 if (ret == -ENOENT) {
346 bch_notice(c, "creating lost+found");
347 goto create_lostfound;
350 if (ret && ret != -EINTR)
351 bch_err(c, "error looking up lost+found: %i", ret);
355 if (d_type != DT_DIR) {
356 bch_err(c, "error looking up lost+found: not a directory");
361 * The check_dirents pass has already run, dangling dirents
362 * shouldn't exist here:
364 return __lookup_inode(trans, inum, lostfound, &snapshot);
367 bch2_inode_init_early(c, lostfound);
369 ret = bch2_create_trans(trans, root_inum, &root,
370 lostfound, &lostfound_str,
371 0, 0, S_IFDIR|0700, 0, NULL, NULL,
372 (subvol_inum) { }, 0);
373 if (ret && ret != -EINTR)
374 bch_err(c, "error creating lost+found: %i", ret);
378 static int __reattach_inode(struct btree_trans *trans,
379 struct bch_inode_unpacked *inode,
382 struct bch_hash_info dir_hash;
383 struct bch_inode_unpacked lostfound;
390 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
394 ret = lookup_lostfound(trans, subvol, &lostfound);
398 if (S_ISDIR(inode->bi_mode)) {
399 lostfound.bi_nlink++;
401 ret = __write_inode(trans, &lostfound, U32_MAX);
406 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
408 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
409 name = (struct qstr) QSTR(name_buf);
411 ret = bch2_dirent_create(trans,
414 .inum = lostfound.bi_inum,
418 &name, inode->bi_inum, &dir_offset,
419 BCH_HASH_SET_MUST_CREATE);
423 inode->bi_dir = lostfound.bi_inum;
424 inode->bi_dir_offset = dir_offset;
426 return __write_inode(trans, inode, inode_snapshot);
429 static int reattach_inode(struct btree_trans *trans,
430 struct bch_inode_unpacked *inode,
433 int ret = __bch2_trans_do(trans, NULL, NULL,
434 BTREE_INSERT_LAZY_RW|
436 __reattach_inode(trans, inode, inode_snapshot));
438 bch_err(trans->c, "error %i reattaching inode %llu",
439 ret, inode->bi_inum);
446 static int remove_backpointer(struct btree_trans *trans,
447 struct bch_inode_unpacked *inode)
449 struct btree_iter iter;
453 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents,
454 POS(inode->bi_dir, inode->bi_dir_offset), 0);
455 k = bch2_btree_iter_peek_slot(&iter);
459 if (k.k->type != KEY_TYPE_dirent) {
464 ret = __remove_dirent(trans, k.k->p);
466 bch2_trans_iter_exit(trans, &iter);
470 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s, struct bpos pos)
472 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
474 if (bkey_cmp(s->pos, pos))
478 /* Might get called multiple times due to lock restarts */
479 if (s->ids.nr && s->ids.data[s->ids.nr - 1] == pos.snapshot)
482 return snapshots_seen_add(c, s, pos.snapshot);
486 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
487 * and @ancestor hasn't been overwritten in @seen
489 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
491 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
492 u32 id, u32 ancestor)
496 BUG_ON(id > ancestor);
498 id = snapshot_t(c, id)->equiv;
499 ancestor = snapshot_t(c, ancestor)->equiv;
501 /* @ancestor should be the snapshot most recently added to @seen */
502 BUG_ON(!seen->ids.nr || seen->ids.data[seen->ids.nr - 1] != ancestor);
503 BUG_ON(seen->pos.snapshot != ancestor);
508 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
511 for (i = seen->ids.nr - 2;
512 i >= 0 && seen->ids.data[i] >= id;
514 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i]) &&
515 bch2_snapshot_is_ancestor(c, seen->ids.data[i], ancestor))
522 * ref_visible - given a key with snapshot id @src that points to a key with
523 * snapshot id @dst, test whether there is some snapshot in which @dst is
526 * This assumes we're visiting @src keys in natural key order.
528 * @s - list of snapshot IDs already seen at @src
529 * @src - snapshot ID of src key
530 * @dst - snapshot ID of dst key
532 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
536 ? key_visible_in_snapshot(c, s, dst, src)
537 : bch2_snapshot_is_ancestor(c, src, dst);
540 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
541 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && (_i)->snapshot <= (_snapshot); _i++)\
542 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
544 struct inode_walker_entry {
545 struct bch_inode_unpacked inode;
550 struct inode_walker {
551 bool first_this_inode;
554 DARRAY(struct inode_walker_entry) inodes;
557 static void inode_walker_exit(struct inode_walker *w)
559 darray_exit(w->inodes);
562 static struct inode_walker inode_walker_init(void)
564 return (struct inode_walker) { 0, };
567 static int add_inode(struct bch_fs *c, struct inode_walker *w,
568 struct bkey_s_c inode)
570 struct bch_inode_unpacked u;
572 BUG_ON(bch2_inode_unpack(inode, &u));
574 return darray_push(w->inodes, ((struct inode_walker_entry) {
576 .snapshot = snapshot_t(c, inode.k->p.snapshot)->equiv,
580 static int __walk_inode(struct btree_trans *trans,
581 struct inode_walker *w, struct bpos pos)
583 struct bch_fs *c = trans->c;
584 struct btree_iter iter;
586 unsigned i, ancestor_pos;
589 pos.snapshot = snapshot_t(c, pos.snapshot)->equiv;
591 if (pos.inode == w->cur_inum) {
592 w->first_this_inode = false;
593 goto lookup_snapshot;
598 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, pos.inode),
599 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
600 if (k.k->p.offset != pos.inode)
603 if (bkey_is_inode(k.k))
606 bch2_trans_iter_exit(trans, &iter);
611 w->cur_inum = pos.inode;
612 w->first_this_inode = true;
614 for (i = 0; i < w->inodes.nr; i++)
615 if (bch2_snapshot_is_ancestor(c, pos.snapshot, w->inodes.data[i].snapshot))
619 BUG_ON(pos.snapshot > w->inodes.data[i].snapshot);
621 if (pos.snapshot != w->inodes.data[i].snapshot) {
624 while (i && w->inodes.data[i - 1].snapshot > pos.snapshot)
627 ret = darray_insert_item(w->inodes, i, w->inodes.data[ancestor_pos]);
631 w->inodes.data[i].snapshot = pos.snapshot;
632 w->inodes.data[i].count = 0;
638 static int __get_visible_inodes(struct btree_trans *trans,
639 struct inode_walker *w,
640 struct snapshots_seen *s,
643 struct bch_fs *c = trans->c;
644 struct btree_iter iter;
650 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
651 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
652 if (k.k->p.offset != inum)
655 if (!bkey_is_inode(k.k))
658 if (ref_visible(c, s, s->pos.snapshot, k.k->p.snapshot)) {
660 if (k.k->p.snapshot >= s->pos.snapshot)
664 bch2_trans_iter_exit(trans, &iter);
669 static int check_key_has_snapshot(struct btree_trans *trans,
670 struct btree_iter *iter,
673 struct bch_fs *c = trans->c;
674 struct printbuf buf = PRINTBUF;
677 if (mustfix_fsck_err_on(!snapshot_t(c, k.k->p.snapshot)->equiv, c,
678 "key in missing snapshot: %s",
679 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
680 ret = bch2_btree_delete_at(trans, iter,
681 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
687 static int hash_redo_key(struct btree_trans *trans,
688 const struct bch_hash_desc desc,
689 struct bch_hash_info *hash_info,
690 struct btree_iter *k_iter, struct bkey_s_c k)
692 bch_err(trans->c, "hash_redo_key() not implemented yet");
695 struct bkey_i *delete;
698 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
700 return PTR_ERR(delete);
702 tmp = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
706 bkey_reassemble(tmp, k);
708 bkey_init(&delete->k);
709 delete->k.p = k_iter->pos;
710 return bch2_btree_iter_traverse(k_iter) ?:
711 bch2_trans_update(trans, k_iter, delete, 0) ?:
712 bch2_hash_set(trans, desc, hash_info, k_iter->pos.inode, tmp, 0);
716 static int hash_check_key(struct btree_trans *trans,
717 const struct bch_hash_desc desc,
718 struct bch_hash_info *hash_info,
719 struct btree_iter *k_iter, struct bkey_s_c hash_k)
721 struct bch_fs *c = trans->c;
722 struct btree_iter iter = { NULL };
723 struct printbuf buf = PRINTBUF;
728 if (hash_k.k->type != desc.key_type)
731 hash = desc.hash_bkey(hash_info, hash_k);
733 if (likely(hash == hash_k.k->p.offset))
736 if (hash_k.k->p.offset < hash)
739 for_each_btree_key(trans, iter, desc.btree_id, POS(hash_k.k->p.inode, hash),
740 BTREE_ITER_SLOTS, k, ret) {
741 if (!bkey_cmp(k.k->p, hash_k.k->p))
744 if (fsck_err_on(k.k->type == desc.key_type &&
745 !desc.cmp_bkey(k, hash_k), c,
746 "duplicate hash table keys:\n%s",
747 (printbuf_reset(&buf),
748 bch2_bkey_val_to_text(&buf, c, hash_k),
750 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
754 if (bkey_deleted(k.k)) {
755 bch2_trans_iter_exit(trans, &iter);
761 bch2_trans_iter_exit(trans, &iter);
765 if (fsck_err(c, "hash table key at wrong offset: btree %u inode %llu offset %llu, "
766 "hashed to %llu\n%s",
767 desc.btree_id, hash_k.k->p.inode, hash_k.k->p.offset, hash,
768 (printbuf_reset(&buf),
769 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf)) == FSCK_ERR_IGNORE)
772 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
774 bch_err(c, "hash_redo_key err %i", ret);
782 static int check_inode(struct btree_trans *trans,
783 struct btree_iter *iter,
784 struct bch_inode_unpacked *prev,
787 struct bch_fs *c = trans->c;
789 struct bch_inode_unpacked u;
790 bool do_update = false;
793 k = bch2_btree_iter_peek(iter);
801 ret = check_key_has_snapshot(trans, iter, k);
803 return ret < 0 ? ret : 0;
806 * if snapshot id isn't a leaf node, skip it - deletion in
807 * particular is not atomic, so on the internal snapshot nodes
808 * we can see inodes marked for deletion after a clean shutdown
810 if (bch2_snapshot_internal_node(c, k.k->p.snapshot))
813 if (!bkey_is_inode(k.k))
816 BUG_ON(bch2_inode_unpack(k, &u));
819 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
820 BCH_INODE_I_SECTORS_DIRTY|
821 BCH_INODE_UNLINKED)))
824 if (prev->bi_inum != u.bi_inum)
827 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
828 inode_d_type(prev) != inode_d_type(&u), c,
829 "inodes in different snapshots don't match")) {
830 bch_err(c, "repair not implemented yet");
834 if (u.bi_flags & BCH_INODE_UNLINKED &&
836 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
838 bch2_trans_unlock(trans);
841 ret = fsck_inode_rm(trans, u.bi_inum, iter->pos.snapshot);
843 bch_err(c, "error in fsck: error %i while deleting inode", ret);
847 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
849 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
851 bch_verbose(c, "truncating inode %llu", u.bi_inum);
853 bch2_trans_unlock(trans);
857 * XXX: need to truncate partial blocks too here - or ideally
858 * just switch units to bytes and that issue goes away
860 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
861 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
863 POS(u.bi_inum, U64_MAX),
866 bch_err(c, "error in fsck: error %i truncating inode", ret);
871 * We truncated without our normal sector accounting hook, just
872 * make sure we recalculate it:
874 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
876 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
880 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
882 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
886 bch_verbose(c, "recounting sectors for inode %llu",
889 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
891 bch_err(c, "error in fsck: error %i recounting inode sectors",
896 u.bi_sectors = sectors;
897 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
901 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
904 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
909 ret = write_inode(trans, &u, iter->pos.snapshot);
911 bch_err(c, "error in fsck: error %i "
912 "updating inode", ret);
919 static int check_inodes(struct bch_fs *c, bool full)
921 struct btree_trans trans;
922 struct btree_iter iter;
923 struct bch_inode_unpacked prev = { 0 };
926 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
928 bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes, POS_MIN,
931 BTREE_ITER_ALL_SNAPSHOTS);
934 ret = __bch2_trans_do(&trans, NULL, NULL,
935 BTREE_INSERT_LAZY_RW|
937 check_inode(&trans, &iter, &prev, full));
940 } while (bch2_btree_iter_advance(&iter));
941 bch2_trans_iter_exit(&trans, &iter);
943 bch2_trans_exit(&trans);
947 static int check_subvol(struct btree_trans *trans,
948 struct btree_iter *iter)
951 struct bkey_s_c_subvolume subvol;
954 k = bch2_btree_iter_peek(iter);
962 if (k.k->type != KEY_TYPE_subvolume)
965 subvol = bkey_s_c_to_subvolume(k);
967 if (BCH_SUBVOLUME_UNLINKED(subvol.v)) {
968 ret = bch2_subvolume_delete(trans, iter->pos.offset);
969 if (ret && ret != -EINTR)
970 bch_err(trans->c, "error deleting subvolume %llu: %i",
971 iter->pos.offset, ret);
980 static int check_subvols(struct bch_fs *c)
982 struct btree_trans trans;
983 struct btree_iter iter;
986 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
988 bch2_trans_iter_init(&trans, &iter, BTREE_ID_subvolumes,
991 BTREE_ITER_PREFETCH);
994 ret = __bch2_trans_do(&trans, NULL, NULL,
995 BTREE_INSERT_LAZY_RW|
997 check_subvol(&trans, &iter));
1000 } while (bch2_btree_iter_advance(&iter));
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 struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1049 struct btree_iter *iter,
1055 bch2_trans_iter_init(trans, iter, BTREE_ID_dirents, pos, 0);
1056 k = bch2_btree_iter_peek_slot(iter);
1058 if (!ret && k.k->type != KEY_TYPE_dirent)
1061 bch2_trans_iter_exit(trans, iter);
1062 return (struct bkey_s_c_dirent) { .k = ERR_PTR(ret) };
1065 return bkey_s_c_to_dirent(k);
1068 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1069 struct bkey_s_c_dirent d)
1071 return inode->bi_dir == d.k->p.inode &&
1072 inode->bi_dir_offset == d.k->p.offset;
1075 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1076 struct bch_inode_unpacked *inode)
1078 return d.v->d_type == DT_SUBVOL
1079 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1080 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1083 static int inode_backpointer_exists(struct btree_trans *trans,
1084 struct bch_inode_unpacked *inode,
1087 struct btree_iter iter;
1088 struct bkey_s_c_dirent d;
1091 d = dirent_get_by_pos(trans, &iter,
1092 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1093 ret = bkey_err(d.s_c);
1097 ret = dirent_points_to_inode(d, inode);
1098 bch2_trans_iter_exit(trans, &iter);
1102 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1104 struct bch_fs *c = trans->c;
1105 struct inode_walker_entry *i;
1106 int ret = 0, ret2 = 0;
1109 darray_for_each(w->inodes, i) {
1110 if (i->inode.bi_sectors == i->count)
1113 count2 = lockrestart_do(trans,
1114 bch2_count_inode_sectors(trans, w->cur_inum, i->snapshot));
1116 if (i->count != count2) {
1117 bch_err(c, "fsck counted i_sectors wrong: got %llu should be %llu",
1120 if (i->inode.bi_sectors == i->count)
1124 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1125 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1126 w->cur_inum, i->snapshot,
1127 i->inode.bi_sectors, i->count) == FSCK_ERR_IGNORE)
1130 i->inode.bi_sectors = i->count;
1131 ret = write_inode(trans, &i->inode, i->snapshot);
1140 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1141 struct inode_walker *inode,
1142 struct snapshots_seen *s)
1144 struct bch_fs *c = trans->c;
1146 struct inode_walker_entry *i;
1147 struct printbuf buf = PRINTBUF;
1150 k = bch2_btree_iter_peek(iter);
1158 ret = check_key_has_snapshot(trans, iter, k);
1160 ret = ret < 0 ? ret : 0;
1164 ret = snapshots_seen_update(c, s, k.k->p);
1168 if (k.k->type == KEY_TYPE_whiteout)
1171 if (inode->cur_inum != k.k->p.inode) {
1172 ret = check_i_sectors(trans, inode);
1177 if (bkey_cmp(prev.k->k.p, bkey_start_pos(k.k)) > 0) {
1181 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(prev.k));
1182 bch2_bkey_val_to_text(&PBUF(buf2), c, k);
1184 if (fsck_err(c, "overlapping extents:\n%s\n%s", buf1, buf2)) {
1185 ret = fix_overlapping_extent(trans, k, prev.k->k.p) ?: -EINTR;
1190 ret = __walk_inode(trans, inode, k.k->p);
1194 if (fsck_err_on(ret == INT_MAX, c,
1195 "extent in missing inode:\n %s",
1196 (printbuf_reset(&buf),
1197 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1198 ret = bch2_btree_delete_at(trans, iter,
1199 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1203 if (ret == INT_MAX) {
1208 i = inode->inodes.data + ret;
1211 if (fsck_err_on(!S_ISREG(i->inode.bi_mode) &&
1212 !S_ISLNK(i->inode.bi_mode), c,
1213 "extent in non regular inode mode %o:\n %s",
1215 (printbuf_reset(&buf),
1216 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1217 ret = bch2_btree_delete_at(trans, iter,
1218 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1222 if (!bch2_snapshot_internal_node(c, k.k->p.snapshot)) {
1223 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i) {
1224 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1225 k.k->type != KEY_TYPE_reservation &&
1226 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9, c,
1227 "extent type %u offset %llu past end of inode %llu, i_size %llu",
1228 k.k->type, k.k->p.offset, k.k->p.inode, i->inode.bi_size)) {
1230 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1231 SPOS(k.k->p.inode, round_up(i->inode.bi_size, block_bytes(c)) >> 9,
1233 POS(k.k->p.inode, U64_MAX),
1240 if (bkey_extent_is_allocation(k.k))
1241 for_each_visible_inode(c, s, inode, k.k->p.snapshot, i)
1242 i->count += k.k->size;
1244 bch2_bkey_buf_reassemble(&prev, c, k);
1250 printbuf_exit(&buf);
1255 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1256 * that i_size an i_sectors are consistent
1259 static int check_extents(struct bch_fs *c)
1261 struct inode_walker w = inode_walker_init();
1262 struct snapshots_seen s;
1263 struct btree_trans trans;
1264 struct btree_iter iter;
1268 struct bkey_buf prev;
1269 bch2_bkey_buf_init(&prev);
1270 prev.k->k = KEY(0, 0, 0);
1272 snapshots_seen_init(&s);
1273 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1275 bch_verbose(c, "checking extents");
1277 bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
1278 POS(BCACHEFS_ROOT_INO, 0),
1280 BTREE_ITER_PREFETCH|
1281 BTREE_ITER_ALL_SNAPSHOTS);
1284 ret = __bch2_trans_do(&trans, NULL, NULL,
1285 BTREE_INSERT_LAZY_RW|
1286 BTREE_INSERT_NOFAIL,
1287 check_extent(&trans, &iter, &w, &s));
1290 } while (bch2_btree_iter_advance(&iter));
1291 bch2_trans_iter_exit(&trans, &iter);
1293 bch2_bkey_buf_exit(&prev, c);
1295 inode_walker_exit(&w);
1296 bch2_trans_exit(&trans);
1297 snapshots_seen_exit(&s);
1302 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1304 struct bch_fs *c = trans->c;
1305 struct inode_walker_entry *i;
1306 int ret = 0, ret2 = 0;
1309 darray_for_each(w->inodes, i) {
1310 if (i->inode.bi_nlink == i->count)
1313 count2 = bch2_count_subdirs(trans, w->cur_inum, i->snapshot);
1317 if (i->count != count2) {
1318 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1321 if (i->inode.bi_nlink == i->count)
1325 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1326 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1327 w->cur_inum, i->snapshot, i->inode.bi_nlink, i->count)) {
1328 i->inode.bi_nlink = i->count;
1329 ret = write_inode(trans, &i->inode, i->snapshot);
1339 static int check_dirent_target(struct btree_trans *trans,
1340 struct btree_iter *iter,
1341 struct bkey_s_c_dirent d,
1342 struct bch_inode_unpacked *target,
1343 u32 target_snapshot)
1345 struct bch_fs *c = trans->c;
1346 struct bkey_i_dirent *n;
1347 bool backpointer_exists = true;
1348 struct printbuf buf = PRINTBUF;
1351 if (!target->bi_dir &&
1352 !target->bi_dir_offset) {
1353 target->bi_dir = d.k->p.inode;
1354 target->bi_dir_offset = d.k->p.offset;
1356 ret = __write_inode(trans, target, target_snapshot);
1361 if (!inode_points_to_dirent(target, d)) {
1362 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1366 backpointer_exists = ret;
1369 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1370 backpointer_exists, c,
1371 "directory %llu with multiple links",
1373 ret = __remove_dirent(trans, d.k->p);
1377 if (fsck_err_on(backpointer_exists &&
1378 !target->bi_nlink, c,
1379 "inode %llu has multiple links but i_nlink 0",
1382 target->bi_flags &= ~BCH_INODE_UNLINKED;
1384 ret = __write_inode(trans, target, target_snapshot);
1389 if (fsck_err_on(!backpointer_exists, c,
1390 "inode %llu:%u has wrong backpointer:\n"
1392 "should be %llu:%llu",
1393 target->bi_inum, target_snapshot,
1395 target->bi_dir_offset,
1398 target->bi_dir = d.k->p.inode;
1399 target->bi_dir_offset = d.k->p.offset;
1401 ret = __write_inode(trans, target, target_snapshot);
1407 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1408 "incorrect d_type: got %s, should be %s:\n%s",
1409 bch2_d_type_str(d.v->d_type),
1410 bch2_d_type_str(inode_d_type(target)),
1411 (printbuf_reset(&buf),
1412 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1413 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1414 ret = PTR_ERR_OR_ZERO(n);
1418 bkey_reassemble(&n->k_i, d.s_c);
1419 n->v.d_type = inode_d_type(target);
1421 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1425 d = dirent_i_to_s_c(n);
1428 if (d.v->d_type == DT_SUBVOL &&
1429 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1430 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1431 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1432 le32_to_cpu(d.v->d_parent_subvol),
1433 target->bi_parent_subvol))) {
1434 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1435 ret = PTR_ERR_OR_ZERO(n);
1439 bkey_reassemble(&n->k_i, d.s_c);
1440 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1442 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1446 d = dirent_i_to_s_c(n);
1451 printbuf_exit(&buf);
1455 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1456 struct bch_hash_info *hash_info,
1457 struct inode_walker *dir,
1458 struct inode_walker *target,
1459 struct snapshots_seen *s)
1461 struct bch_fs *c = trans->c;
1463 struct bkey_s_c_dirent d;
1464 struct inode_walker_entry *i;
1465 struct printbuf buf = PRINTBUF;
1468 k = bch2_btree_iter_peek(iter);
1476 ret = check_key_has_snapshot(trans, iter, k);
1478 ret = ret < 0 ? ret : 0;
1482 ret = snapshots_seen_update(c, s, k.k->p);
1486 if (k.k->type == KEY_TYPE_whiteout)
1489 if (dir->cur_inum != k.k->p.inode) {
1490 ret = check_subdir_count(trans, dir);
1495 ret = __walk_inode(trans, dir, k.k->p);
1499 if (fsck_err_on(ret == INT_MAX, c,
1500 "dirent in nonexisting directory:\n%s",
1501 (printbuf_reset(&buf),
1502 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1503 ret = bch2_btree_delete_at(trans, iter,
1504 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1508 if (ret == INT_MAX) {
1513 i = dir->inodes.data + ret;
1516 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1517 "dirent in non directory inode type %s:\n%s",
1518 bch2_d_type_str(inode_d_type(&i->inode)),
1519 (printbuf_reset(&buf),
1520 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1521 ret = bch2_btree_delete_at(trans, iter, 0);
1525 if (dir->first_this_inode)
1526 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1528 ret = hash_check_key(trans, bch2_dirent_hash_desc,
1529 hash_info, iter, k);
1533 /* dirent has been deleted */
1538 if (k.k->type != KEY_TYPE_dirent)
1541 d = bkey_s_c_to_dirent(k);
1543 if (d.v->d_type == DT_SUBVOL) {
1544 struct bch_inode_unpacked subvol_root;
1545 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1546 u32 target_snapshot;
1549 ret = __subvol_lookup(trans, target_subvol,
1550 &target_snapshot, &target_inum);
1551 if (ret && ret != -ENOENT)
1554 if (fsck_err_on(ret, c,
1555 "dirent points to missing subvolume %llu",
1556 le64_to_cpu(d.v->d_child_subvol))) {
1557 ret = __remove_dirent(trans, d.k->p);
1561 ret = __lookup_inode(trans, target_inum,
1562 &subvol_root, &target_snapshot);
1563 if (ret && ret != -ENOENT)
1566 if (fsck_err_on(ret, c,
1567 "subvolume %u points to missing subvolume root %llu",
1570 bch_err(c, "repair not implemented yet");
1575 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1576 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1578 subvol_root.bi_subvol, target_subvol)) {
1579 subvol_root.bi_subvol = target_subvol;
1580 ret = __write_inode(trans, &subvol_root, target_snapshot);
1585 ret = check_dirent_target(trans, iter, d, &subvol_root,
1590 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1594 if (fsck_err_on(!target->inodes.nr, c,
1595 "dirent points to missing inode:\n%s",
1596 (printbuf_reset(&buf),
1597 bch2_bkey_val_to_text(&buf, c, k),
1599 ret = __remove_dirent(trans, d.k->p);
1604 darray_for_each(target->inodes, i) {
1605 ret = check_dirent_target(trans, iter, d,
1606 &i->inode, i->snapshot);
1612 if (d.v->d_type == DT_DIR)
1613 for_each_visible_inode(c, s, dir, d.k->p.snapshot, i)
1619 printbuf_exit(&buf);
1624 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1628 static int check_dirents(struct bch_fs *c)
1630 struct inode_walker dir = inode_walker_init();
1631 struct inode_walker target = inode_walker_init();
1632 struct snapshots_seen s;
1633 struct bch_hash_info hash_info;
1634 struct btree_trans trans;
1635 struct btree_iter iter;
1638 bch_verbose(c, "checking dirents");
1640 snapshots_seen_init(&s);
1641 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1643 bch2_trans_iter_init(&trans, &iter, BTREE_ID_dirents,
1644 POS(BCACHEFS_ROOT_INO, 0),
1646 BTREE_ITER_PREFETCH|
1647 BTREE_ITER_ALL_SNAPSHOTS);
1650 ret = __bch2_trans_do(&trans, NULL, NULL,
1651 BTREE_INSERT_LAZY_RW|
1652 BTREE_INSERT_NOFAIL,
1653 check_dirent(&trans, &iter, &hash_info,
1654 &dir, &target, &s));
1657 } while (bch2_btree_iter_advance(&iter));
1658 bch2_trans_iter_exit(&trans, &iter);
1660 bch2_trans_exit(&trans);
1661 snapshots_seen_exit(&s);
1662 inode_walker_exit(&dir);
1663 inode_walker_exit(&target);
1667 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1668 struct bch_hash_info *hash_info,
1669 struct inode_walker *inode)
1671 struct bch_fs *c = trans->c;
1675 k = bch2_btree_iter_peek(iter);
1683 ret = check_key_has_snapshot(trans, iter, k);
1687 ret = __walk_inode(trans, inode, k.k->p);
1691 if (fsck_err_on(ret == INT_MAX, c,
1692 "xattr for missing inode %llu",
1694 return bch2_btree_delete_at(trans, iter, 0);
1701 if (inode->first_this_inode)
1702 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1704 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1710 * Walk xattrs: verify that they all have a corresponding inode
1713 static int check_xattrs(struct bch_fs *c)
1715 struct inode_walker inode = inode_walker_init();
1716 struct bch_hash_info hash_info;
1717 struct btree_trans trans;
1718 struct btree_iter iter;
1721 bch_verbose(c, "checking xattrs");
1723 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1725 bch2_trans_iter_init(&trans, &iter, BTREE_ID_xattrs,
1726 POS(BCACHEFS_ROOT_INO, 0),
1728 BTREE_ITER_PREFETCH|
1729 BTREE_ITER_ALL_SNAPSHOTS);
1732 ret = __bch2_trans_do(&trans, NULL, NULL,
1733 BTREE_INSERT_LAZY_RW|
1734 BTREE_INSERT_NOFAIL,
1735 check_xattr(&trans, &iter, &hash_info,
1739 } while (bch2_btree_iter_advance(&iter));
1740 bch2_trans_iter_exit(&trans, &iter);
1742 bch2_trans_exit(&trans);
1746 static int check_root_trans(struct btree_trans *trans)
1748 struct bch_fs *c = trans->c;
1749 struct bch_inode_unpacked root_inode;
1754 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1755 if (ret && ret != -ENOENT)
1758 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1759 struct bkey_i_subvolume root_subvol;
1762 inum = BCACHEFS_ROOT_INO;
1764 bkey_subvolume_init(&root_subvol.k_i);
1765 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1766 root_subvol.v.flags = 0;
1767 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1768 root_subvol.v.inode = cpu_to_le64(inum);
1769 ret = __bch2_trans_do(trans, NULL, NULL,
1770 BTREE_INSERT_NOFAIL|
1771 BTREE_INSERT_LAZY_RW,
1772 __bch2_btree_insert(trans, BTREE_ID_subvolumes, &root_subvol.k_i));
1774 bch_err(c, "error writing root subvol: %i", ret);
1780 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1781 if (ret && ret != -ENOENT)
1784 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1785 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1786 "root inode not a directory")) {
1787 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1789 root_inode.bi_inum = inum;
1791 ret = __write_inode(trans, &root_inode, snapshot);
1793 bch_err(c, "error writing root inode: %i", ret);
1800 /* Get root directory, create if it doesn't exist: */
1802 static int check_root(struct bch_fs *c)
1804 bch_verbose(c, "checking root directory");
1806 return bch2_trans_do(c, NULL, NULL,
1807 BTREE_INSERT_NOFAIL|
1808 BTREE_INSERT_LAZY_RW,
1809 check_root_trans(&trans));
1812 struct pathbuf_entry {
1817 typedef DARRAY(struct pathbuf_entry) pathbuf;
1819 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1821 struct pathbuf_entry *i;
1823 darray_for_each(*p, i)
1824 if (i->inum == inum &&
1825 i->snapshot == snapshot)
1831 static int path_down(struct bch_fs *c, pathbuf *p,
1832 u64 inum, u32 snapshot)
1834 int ret = darray_push(*p, ((struct pathbuf_entry) {
1836 .snapshot = snapshot,
1840 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1846 * Check that a given inode is reachable from the root:
1848 * XXX: we should also be verifying that inodes are in the right subvolumes
1850 static int check_path(struct btree_trans *trans,
1852 struct bch_inode_unpacked *inode,
1855 struct bch_fs *c = trans->c;
1858 snapshot = snapshot_t(c, snapshot)->equiv;
1861 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1862 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1863 struct btree_iter dirent_iter;
1864 struct bkey_s_c_dirent d;
1865 u32 parent_snapshot = snapshot;
1867 if (inode->bi_subvol) {
1870 ret = subvol_lookup(trans, inode->bi_parent_subvol,
1871 &parent_snapshot, &inum);
1876 ret = lockrestart_do(trans,
1877 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
1878 SPOS(inode->bi_dir, inode->bi_dir_offset,
1879 parent_snapshot))).k));
1880 if (ret && ret != -ENOENT)
1883 if (!ret && !dirent_points_to_inode(d, inode)) {
1884 bch2_trans_iter_exit(trans, &dirent_iter);
1888 if (ret == -ENOENT) {
1889 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
1890 inode->bi_inum, snapshot,
1891 bch2_d_type_str(inode_d_type(inode)),
1894 inode->bi_dir_offset))
1895 ret = reattach_inode(trans, inode, snapshot);
1899 bch2_trans_iter_exit(trans, &dirent_iter);
1901 if (!S_ISDIR(inode->bi_mode))
1904 ret = path_down(c, p, inode->bi_inum, snapshot);
1906 bch_err(c, "memory allocation failure");
1910 snapshot = parent_snapshot;
1912 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
1914 /* Should have been caught in dirents pass */
1915 bch_err(c, "error looking up parent directory: %i", ret);
1919 if (path_is_dup(p, inode->bi_inum, snapshot)) {
1920 struct pathbuf_entry *i;
1922 /* XXX print path */
1923 bch_err(c, "directory structure loop");
1925 darray_for_each(*p, i)
1926 pr_err("%llu:%u", i->inum, i->snapshot);
1927 pr_err("%llu:%u", inode->bi_inum, snapshot);
1929 if (!fsck_err(c, "directory structure loop"))
1932 ret = __bch2_trans_do(trans, NULL, NULL,
1933 BTREE_INSERT_NOFAIL|
1934 BTREE_INSERT_LAZY_RW,
1935 remove_backpointer(trans, inode));
1937 bch_err(c, "error removing dirent: %i", ret);
1941 ret = reattach_inode(trans, inode, snapshot);
1946 bch_err(c, "%s: err %i", __func__, ret);
1951 * Check for unreachable inodes, as well as loops in the directory structure:
1952 * After check_dirents(), if an inode backpointer doesn't exist that means it's
1956 static int check_directory_structure(struct bch_fs *c)
1958 struct btree_trans trans;
1959 struct btree_iter iter;
1961 struct bch_inode_unpacked u;
1962 pathbuf path = { 0, };
1965 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1967 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
1969 BTREE_ITER_PREFETCH|
1970 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
1971 if (!bkey_is_inode(k.k))
1974 ret = bch2_inode_unpack(k, &u);
1976 /* Should have been caught earlier in fsck: */
1977 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
1981 if (u.bi_flags & BCH_INODE_UNLINKED)
1984 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
1988 bch2_trans_iter_exit(&trans, &iter);
1990 BUG_ON(ret == -EINTR);
1994 bch2_trans_exit(&trans);
1998 struct nlink_table {
2009 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2010 u64 inum, u32 snapshot)
2012 if (t->nr == t->size) {
2013 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2014 void *d = kvmalloc(new_size * sizeof(t->d[0]), GFP_KERNEL);
2016 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2022 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2030 t->d[t->nr++] = (struct nlink) {
2032 .snapshot = snapshot,
2038 static int nlink_cmp(const void *_l, const void *_r)
2040 const struct nlink *l = _l;
2041 const struct nlink *r = _r;
2043 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2046 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2047 struct nlink_table *links,
2048 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2050 struct nlink *link, key = {
2051 .inum = inum, .snapshot = U32_MAX,
2054 if (inum < range_start || inum >= range_end)
2057 link = __inline_bsearch(&key, links->d, links->nr,
2058 sizeof(links->d[0]), nlink_cmp);
2062 while (link > links->d && link[0].inum == link[-1].inum)
2065 for (; link < links->d + links->nr && link->inum == inum; link++)
2066 if (ref_visible(c, s, snapshot, link->snapshot)) {
2068 if (link->snapshot >= snapshot)
2074 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2075 struct nlink_table *t,
2076 u64 start, u64 *end)
2078 struct btree_trans trans;
2079 struct btree_iter iter;
2081 struct bch_inode_unpacked u;
2084 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2086 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2089 BTREE_ITER_PREFETCH|
2090 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2091 if (!bkey_is_inode(k.k))
2094 /* Should never fail, checked by bch2_inode_invalid: */
2095 BUG_ON(bch2_inode_unpack(k, &u));
2098 * Backpointer and directory structure checks are sufficient for
2099 * directories, since they can't have hardlinks:
2101 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2107 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2109 *end = k.k->p.offset;
2115 bch2_trans_iter_exit(&trans, &iter);
2116 bch2_trans_exit(&trans);
2119 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2125 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2126 u64 range_start, u64 range_end)
2128 struct btree_trans trans;
2129 struct snapshots_seen s;
2130 struct btree_iter iter;
2132 struct bkey_s_c_dirent d;
2135 snapshots_seen_init(&s);
2137 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2139 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2141 BTREE_ITER_PREFETCH|
2142 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2143 ret = snapshots_seen_update(c, &s, k.k->p);
2147 switch (k.k->type) {
2148 case KEY_TYPE_dirent:
2149 d = bkey_s_c_to_dirent(k);
2151 if (d.v->d_type != DT_DIR &&
2152 d.v->d_type != DT_SUBVOL)
2153 inc_link(c, &s, links, range_start, range_end,
2154 le64_to_cpu(d.v->d_inum),
2159 bch2_trans_iter_exit(&trans, &iter);
2162 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2164 bch2_trans_exit(&trans);
2165 snapshots_seen_exit(&s);
2170 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2171 struct nlink_table *links,
2172 u64 range_start, u64 range_end)
2174 struct btree_trans trans;
2175 struct btree_iter iter;
2177 struct bch_inode_unpacked u;
2178 struct nlink *link = links->d;
2181 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2183 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2184 POS(0, range_start),
2186 BTREE_ITER_PREFETCH|
2187 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2188 if (k.k->p.offset >= range_end)
2191 if (!bkey_is_inode(k.k))
2194 BUG_ON(bch2_inode_unpack(k, &u));
2196 if (S_ISDIR(le16_to_cpu(u.bi_mode)))
2202 while ((cmp_int(link->inum, k.k->p.offset) ?:
2203 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2205 BUG_ON(link >= links->d + links->nr);
2208 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2209 "inode %llu has wrong i_nlink (type %u i_nlink %u, should be %u)",
2210 u.bi_inum, mode_to_type(u.bi_mode),
2211 bch2_inode_nlink_get(&u), link->count)) {
2212 bch2_inode_nlink_set(&u, link->count);
2214 ret = write_inode(&trans, &u, k.k->p.snapshot);
2216 bch_err(c, "error in fsck: error %i updating inode", ret);
2220 bch2_trans_iter_exit(&trans, &iter);
2221 bch2_trans_exit(&trans);
2224 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2230 static int check_nlinks(struct bch_fs *c)
2232 struct nlink_table links = { 0 };
2233 u64 this_iter_range_start, next_iter_range_start = 0;
2236 bch_verbose(c, "checking inode nlinks");
2239 this_iter_range_start = next_iter_range_start;
2240 next_iter_range_start = U64_MAX;
2242 ret = check_nlinks_find_hardlinks(c, &links,
2243 this_iter_range_start,
2244 &next_iter_range_start);
2246 ret = check_nlinks_walk_dirents(c, &links,
2247 this_iter_range_start,
2248 next_iter_range_start);
2252 ret = check_nlinks_update_hardlinks(c, &links,
2253 this_iter_range_start,
2254 next_iter_range_start);
2259 } while (next_iter_range_start != U64_MAX);
2266 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter)
2269 struct bkey_s_c_reflink_p p;
2270 struct bkey_i_reflink_p *u;
2273 k = bch2_btree_iter_peek(iter);
2281 if (k.k->type != KEY_TYPE_reflink_p)
2284 p = bkey_s_c_to_reflink_p(k);
2286 if (!p.v->front_pad && !p.v->back_pad)
2289 u = bch2_trans_kmalloc(trans, sizeof(*u));
2290 ret = PTR_ERR_OR_ZERO(u);
2294 bkey_reassemble(&u->k_i, k);
2298 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2302 static int fix_reflink_p(struct bch_fs *c)
2304 struct btree_trans trans;
2305 struct btree_iter iter;
2309 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2312 bch_verbose(c, "fixing reflink_p keys");
2314 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2316 for_each_btree_key(&trans, iter, BTREE_ID_extents, POS_MIN,
2318 BTREE_ITER_PREFETCH|
2319 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2320 if (k.k->type == KEY_TYPE_reflink_p) {
2321 ret = __bch2_trans_do(&trans, NULL, NULL,
2322 BTREE_INSERT_NOFAIL|
2323 BTREE_INSERT_LAZY_RW,
2324 fix_reflink_p_key(&trans, &iter));
2329 bch2_trans_iter_exit(&trans, &iter);
2331 bch2_trans_exit(&trans);
2336 * Checks for inconsistencies that shouldn't happen, unless we have a bug.
2337 * Doesn't fix them yet, mainly because they haven't yet been observed:
2339 int bch2_fsck_full(struct bch_fs *c)
2341 return bch2_fs_snapshots_check(c) ?:
2342 check_inodes(c, true) ?:
2348 check_directory_structure(c) ?:
2353 int bch2_fsck_walk_inodes_only(struct bch_fs *c)
2355 return check_inodes(c, false);