1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_cache.h"
6 #include "btree_update.h"
11 #include "fs-common.h"
15 #include "recovery_passes.h"
20 #include <linux/bsearch.h>
21 #include <linux/dcache.h> /* struct qstr */
24 * XXX: this is handling transaction restarts without returning
25 * -BCH_ERR_transaction_restart_nested, this is not how we do things anymore:
27 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
32 int ret = for_each_btree_key_upto(trans, iter, BTREE_ID_extents,
33 SPOS(inum, 0, snapshot),
36 if (bkey_extent_is_allocation(k.k))
41 return ret ?: sectors;
44 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
49 int ret = for_each_btree_key_upto(trans, iter, BTREE_ID_dirents,
50 SPOS(inum, 0, snapshot),
53 if (k.k->type == KEY_TYPE_dirent &&
54 bkey_s_c_to_dirent(k).v->d_type == DT_DIR)
59 return ret ?: subdirs;
62 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
63 u32 *snapshot, u64 *inum)
65 struct bch_subvolume s;
66 int ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
68 *snapshot = le32_to_cpu(s.snapshot);
69 *inum = le64_to_cpu(s.inode);
73 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
74 struct bch_inode_unpacked *inode)
76 struct btree_iter iter;
80 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
82 BTREE_ITER_all_snapshots);
83 k = bch2_btree_iter_peek(&iter);
88 if (!k.k || !bkey_eq(k.k->p, POS(0, inode_nr))) {
89 ret = -BCH_ERR_ENOENT_inode;
93 ret = bch2_inode_unpack(k, inode);
95 bch_err_msg(trans->c, ret, "fetching inode %llu", inode_nr);
96 bch2_trans_iter_exit(trans, &iter);
100 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
101 struct bch_inode_unpacked *inode,
104 struct btree_iter iter;
108 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
109 SPOS(0, inode_nr, *snapshot), 0);
114 ret = bkey_is_inode(k.k)
115 ? bch2_inode_unpack(k, inode)
116 : -BCH_ERR_ENOENT_inode;
118 *snapshot = iter.pos.snapshot;
120 bch2_trans_iter_exit(trans, &iter);
124 static int lookup_dirent_in_snapshot(struct btree_trans *trans,
125 struct bch_hash_info hash_info,
126 subvol_inum dir, struct qstr *name,
127 u64 *target, unsigned *type, u32 snapshot)
129 struct btree_iter iter;
130 struct bkey_s_c k = bch2_hash_lookup_in_snapshot(trans, &iter, bch2_dirent_hash_desc,
131 &hash_info, dir, name, 0, snapshot);
132 int ret = bkey_err(k);
136 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
137 *target = le64_to_cpu(d.v->d_inum);
139 bch2_trans_iter_exit(trans, &iter);
143 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
145 struct bch_fs *c = trans->c;
146 struct btree_iter iter;
147 struct bch_inode_unpacked dir_inode;
148 struct bch_hash_info dir_hash_info;
151 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
155 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
157 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_intent);
159 ret = bch2_btree_iter_traverse(&iter) ?:
160 bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
161 &dir_hash_info, &iter,
162 BTREE_UPDATE_internal_snapshot_node);
163 bch2_trans_iter_exit(trans, &iter);
169 /* Get lost+found, create if it doesn't exist: */
170 static int lookup_lostfound(struct btree_trans *trans, u32 snapshot,
171 struct bch_inode_unpacked *lostfound,
172 u64 reattaching_inum)
174 struct bch_fs *c = trans->c;
175 struct qstr lostfound_str = QSTR("lost+found");
180 struct bch_snapshot_tree st;
181 ret = bch2_snapshot_tree_lookup(trans,
182 bch2_snapshot_tree(c, snapshot), &st);
186 subvol_inum root_inum = { .subvol = le32_to_cpu(st.master_subvol) };
188 struct bch_subvolume subvol;
189 ret = bch2_subvolume_get(trans, le32_to_cpu(st.master_subvol),
191 bch_err_msg(c, ret, "looking up root subvol %u for snapshot %u",
192 le32_to_cpu(st.master_subvol), snapshot);
197 struct btree_iter iter;
198 struct bkey_i_subvolume *subvol = bch2_bkey_get_mut_typed(trans, &iter,
199 BTREE_ID_subvolumes, POS(0, le32_to_cpu(st.master_subvol)),
201 ret = PTR_ERR_OR_ZERO(subvol);
205 subvol->v.inode = cpu_to_le64(reattaching_inum);
206 bch2_trans_iter_exit(trans, &iter);
209 root_inum.inum = le64_to_cpu(subvol.inode);
211 struct bch_inode_unpacked root_inode;
212 struct bch_hash_info root_hash_info;
213 u32 root_inode_snapshot = snapshot;
214 ret = lookup_inode(trans, root_inum.inum, &root_inode, &root_inode_snapshot);
215 bch_err_msg(c, ret, "looking up root inode %llu for subvol %u",
216 root_inum.inum, le32_to_cpu(st.master_subvol));
220 root_hash_info = bch2_hash_info_init(c, &root_inode);
222 ret = lookup_dirent_in_snapshot(trans, root_hash_info, root_inum,
223 &lostfound_str, &inum, &d_type, snapshot);
224 if (bch2_err_matches(ret, ENOENT))
225 goto create_lostfound;
231 if (d_type != DT_DIR) {
232 bch_err(c, "error looking up lost+found: not a directory");
233 return -BCH_ERR_ENOENT_not_directory;
237 * The bch2_check_dirents pass has already run, dangling dirents
238 * shouldn't exist here:
240 ret = lookup_inode(trans, inum, lostfound, &snapshot);
241 bch_err_msg(c, ret, "looking up lost+found %llu:%u in (root inode %llu, snapshot root %u)",
242 inum, snapshot, root_inum.inum, bch2_snapshot_root(c, snapshot));
247 * XXX: we could have a nicer log message here if we had a nice way to
248 * walk backpointers to print a path
250 bch_notice(c, "creating lost+found in snapshot %u", le32_to_cpu(st.root_snapshot));
252 u64 now = bch2_current_time(c);
253 struct btree_iter lostfound_iter = { NULL };
254 u64 cpu = raw_smp_processor_id();
256 bch2_inode_init_early(c, lostfound);
257 bch2_inode_init_late(lostfound, now, 0, 0, S_IFDIR|0700, 0, &root_inode);
258 lostfound->bi_dir = root_inode.bi_inum;
260 root_inode.bi_nlink++;
262 ret = bch2_inode_create(trans, &lostfound_iter, lostfound, snapshot, cpu);
266 bch2_btree_iter_set_snapshot(&lostfound_iter, snapshot);
267 ret = bch2_btree_iter_traverse(&lostfound_iter);
271 ret = bch2_dirent_create_snapshot(trans,
272 0, root_inode.bi_inum, snapshot, &root_hash_info,
273 mode_to_type(lostfound->bi_mode),
276 &lostfound->bi_dir_offset,
277 STR_HASH_must_create) ?:
278 bch2_inode_write_flags(trans, &lostfound_iter, lostfound,
279 BTREE_UPDATE_internal_snapshot_node);
281 bch_err_msg(c, ret, "creating lost+found");
282 bch2_trans_iter_exit(trans, &lostfound_iter);
286 static int reattach_inode(struct btree_trans *trans,
287 struct bch_inode_unpacked *inode,
290 struct bch_hash_info dir_hash;
291 struct bch_inode_unpacked lostfound;
295 u32 dirent_snapshot = inode_snapshot;
298 if (inode->bi_subvol) {
299 inode->bi_parent_subvol = BCACHEFS_ROOT_SUBVOL;
302 ret = subvol_lookup(trans, inode->bi_parent_subvol,
303 &dirent_snapshot, &root_inum);
307 snprintf(name_buf, sizeof(name_buf), "subvol-%u", inode->bi_subvol);
309 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
312 ret = lookup_lostfound(trans, dirent_snapshot, &lostfound, inode->bi_inum);
316 if (S_ISDIR(inode->bi_mode)) {
317 lostfound.bi_nlink++;
319 ret = __bch2_fsck_write_inode(trans, &lostfound, U32_MAX);
324 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
326 name = (struct qstr) QSTR(name_buf);
328 ret = bch2_dirent_create_snapshot(trans,
329 inode->bi_parent_subvol, lostfound.bi_inum,
334 inode->bi_subvol ?: inode->bi_inum,
336 STR_HASH_must_create);
340 inode->bi_dir = lostfound.bi_inum;
341 inode->bi_dir_offset = dir_offset;
343 return __bch2_fsck_write_inode(trans, inode, inode_snapshot);
346 static int remove_backpointer(struct btree_trans *trans,
347 struct bch_inode_unpacked *inode)
349 struct btree_iter iter;
350 struct bkey_s_c_dirent d;
353 d = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_dirents,
354 POS(inode->bi_dir, inode->bi_dir_offset), 0,
357 __remove_dirent(trans, d.k->p);
358 bch2_trans_iter_exit(trans, &iter);
362 static int reattach_subvol(struct btree_trans *trans, struct bkey_s_c_subvolume s)
364 struct bch_fs *c = trans->c;
366 struct bch_inode_unpacked inode;
367 int ret = bch2_inode_find_by_inum_trans(trans,
368 (subvol_inum) { s.k->p.offset, le64_to_cpu(s.v->inode) },
373 ret = remove_backpointer(trans, &inode);
374 bch_err_msg(c, ret, "removing dirent");
378 ret = reattach_inode(trans, &inode, le32_to_cpu(s.v->snapshot));
379 bch_err_msg(c, ret, "reattaching inode %llu", inode.bi_inum);
383 static int reconstruct_subvol(struct btree_trans *trans, u32 snapshotid, u32 subvolid, u64 inum)
385 struct bch_fs *c = trans->c;
387 if (!bch2_snapshot_is_leaf(c, snapshotid)) {
388 bch_err(c, "need to reconstruct subvol, but have interior node snapshot");
389 return -BCH_ERR_fsck_repair_unimplemented;
393 * If inum isn't set, that means we're being called from check_dirents,
394 * not check_inodes - the root of this subvolume doesn't exist or we
395 * would have found it there:
398 struct btree_iter inode_iter = {};
399 struct bch_inode_unpacked new_inode;
400 u64 cpu = raw_smp_processor_id();
402 bch2_inode_init_early(c, &new_inode);
403 bch2_inode_init_late(&new_inode, bch2_current_time(c), 0, 0, S_IFDIR|0755, 0, NULL);
405 new_inode.bi_subvol = subvolid;
407 int ret = bch2_inode_create(trans, &inode_iter, &new_inode, snapshotid, cpu) ?:
408 bch2_btree_iter_traverse(&inode_iter) ?:
409 bch2_inode_write(trans, &inode_iter, &new_inode);
410 bch2_trans_iter_exit(trans, &inode_iter);
414 inum = new_inode.bi_inum;
417 bch_info(c, "reconstructing subvol %u with root inode %llu", subvolid, inum);
419 struct bkey_i_subvolume *new_subvol = bch2_trans_kmalloc(trans, sizeof(*new_subvol));
420 int ret = PTR_ERR_OR_ZERO(new_subvol);
424 bkey_subvolume_init(&new_subvol->k_i);
425 new_subvol->k.p.offset = subvolid;
426 new_subvol->v.snapshot = cpu_to_le32(snapshotid);
427 new_subvol->v.inode = cpu_to_le64(inum);
428 ret = bch2_btree_insert_trans(trans, BTREE_ID_subvolumes, &new_subvol->k_i, 0);
432 struct btree_iter iter;
433 struct bkey_i_snapshot *s = bch2_bkey_get_mut_typed(trans, &iter,
434 BTREE_ID_snapshots, POS(0, snapshotid),
436 ret = PTR_ERR_OR_ZERO(s);
437 bch_err_msg(c, ret, "getting snapshot %u", snapshotid);
441 u32 snapshot_tree = le32_to_cpu(s->v.tree);
443 s->v.subvol = cpu_to_le32(subvolid);
444 SET_BCH_SNAPSHOT_SUBVOL(&s->v, true);
445 bch2_trans_iter_exit(trans, &iter);
447 struct bkey_i_snapshot_tree *st = bch2_bkey_get_mut_typed(trans, &iter,
448 BTREE_ID_snapshot_trees, POS(0, snapshot_tree),
450 ret = PTR_ERR_OR_ZERO(st);
451 bch_err_msg(c, ret, "getting snapshot tree %u", snapshot_tree);
455 if (!st->v.master_subvol)
456 st->v.master_subvol = cpu_to_le32(subvolid);
458 bch2_trans_iter_exit(trans, &iter);
462 static int reconstruct_inode(struct btree_trans *trans, u32 snapshot, u64 inum, u64 size, unsigned mode)
464 struct bch_fs *c = trans->c;
465 struct bch_inode_unpacked new_inode;
467 bch2_inode_init_early(c, &new_inode);
468 bch2_inode_init_late(&new_inode, bch2_current_time(c), 0, 0, mode|0755, 0, NULL);
469 new_inode.bi_size = size;
470 new_inode.bi_inum = inum;
472 return __bch2_fsck_write_inode(trans, &new_inode, snapshot);
475 static int reconstruct_reg_inode(struct btree_trans *trans, u32 snapshot, u64 inum)
477 struct btree_iter iter = {};
479 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, SPOS(inum, U64_MAX, snapshot), 0);
480 struct bkey_s_c k = bch2_btree_iter_peek_prev(&iter);
481 bch2_trans_iter_exit(trans, &iter);
482 int ret = bkey_err(k);
486 return reconstruct_inode(trans, snapshot, inum, k.k->p.offset << 9, S_IFREG);
489 struct snapshots_seen {
491 snapshot_id_list ids;
494 static inline void snapshots_seen_exit(struct snapshots_seen *s)
496 darray_exit(&s->ids);
499 static inline void snapshots_seen_init(struct snapshots_seen *s)
501 memset(s, 0, sizeof(*s));
504 static int snapshots_seen_add_inorder(struct bch_fs *c, struct snapshots_seen *s, u32 id)
507 __darray_for_each(s->ids, i) {
514 int ret = darray_insert_item(&s->ids, i - s->ids.data, id);
516 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
521 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
522 enum btree_id btree_id, struct bpos pos)
524 if (!bkey_eq(s->pos, pos))
528 return snapshot_list_add_nodup(c, &s->ids, pos.snapshot);
532 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
533 * and @ancestor hasn't been overwritten in @seen
535 * @c: filesystem handle
536 * @seen: list of snapshot ids already seen at current position
537 * @id: descendent snapshot id
538 * @ancestor: ancestor snapshot id
540 * Returns: whether key in @ancestor snapshot is visible in @id snapshot
542 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
543 u32 id, u32 ancestor)
547 EBUG_ON(id > ancestor);
549 /* @ancestor should be the snapshot most recently added to @seen */
550 EBUG_ON(ancestor != seen->pos.snapshot);
551 EBUG_ON(ancestor != darray_last(seen->ids));
556 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
560 * We know that @id is a descendant of @ancestor, we're checking if
561 * we've seen a key that overwrote @ancestor - i.e. also a descendent of
562 * @ascestor and with @id as a descendent.
564 * But we already know that we're scanning IDs between @id and @ancestor
565 * numerically, since snapshot ID lists are kept sorted, so if we find
566 * an id that's an ancestor of @id we're done:
569 for (i = seen->ids.nr - 2;
570 i >= 0 && seen->ids.data[i] >= id;
572 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i]))
579 * ref_visible - given a key with snapshot id @src that points to a key with
580 * snapshot id @dst, test whether there is some snapshot in which @dst is
583 * @c: filesystem handle
584 * @s: list of snapshot IDs already seen at @src
585 * @src: snapshot ID of src key
586 * @dst: snapshot ID of dst key
587 * Returns: true if there is some snapshot in which @dst is visible
589 * Assumes we're visiting @src keys in natural key order
591 static bool ref_visible(struct bch_fs *c, struct snapshots_seen *s,
595 ? key_visible_in_snapshot(c, s, dst, src)
596 : bch2_snapshot_is_ancestor(c, src, dst);
599 static int ref_visible2(struct bch_fs *c,
600 u32 src, struct snapshots_seen *src_seen,
601 u32 dst, struct snapshots_seen *dst_seen)
605 swap(dst_seen, src_seen);
607 return key_visible_in_snapshot(c, src_seen, dst, src);
610 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
611 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
612 (_i)->snapshot <= (_snapshot); _i++) \
613 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
615 struct inode_walker_entry {
616 struct bch_inode_unpacked inode;
622 struct inode_walker {
623 bool first_this_inode;
624 bool recalculate_sums;
625 struct bpos last_pos;
627 DARRAY(struct inode_walker_entry) inodes;
630 static void inode_walker_exit(struct inode_walker *w)
632 darray_exit(&w->inodes);
635 static struct inode_walker inode_walker_init(void)
637 return (struct inode_walker) { 0, };
640 static int add_inode(struct bch_fs *c, struct inode_walker *w,
641 struct bkey_s_c inode)
643 struct bch_inode_unpacked u;
645 BUG_ON(bch2_inode_unpack(inode, &u));
647 return darray_push(&w->inodes, ((struct inode_walker_entry) {
649 .snapshot = inode.k->p.snapshot,
653 static int get_inodes_all_snapshots(struct btree_trans *trans,
654 struct inode_walker *w, u64 inum)
656 struct bch_fs *c = trans->c;
657 struct btree_iter iter;
661 w->recalculate_sums = false;
664 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
665 BTREE_ITER_all_snapshots, k, ret) {
666 if (k.k->p.offset != inum)
669 if (bkey_is_inode(k.k))
672 bch2_trans_iter_exit(trans, &iter);
677 w->first_this_inode = true;
681 static struct inode_walker_entry *
682 lookup_inode_for_snapshot(struct bch_fs *c, struct inode_walker *w, struct bkey_s_c k)
684 bool is_whiteout = k.k->type == KEY_TYPE_whiteout;
686 struct inode_walker_entry *i;
687 __darray_for_each(w->inodes, i)
688 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, i->snapshot))
693 BUG_ON(k.k->p.snapshot > i->snapshot);
695 if (k.k->p.snapshot != i->snapshot && !is_whiteout) {
696 struct inode_walker_entry new = *i;
698 new.snapshot = k.k->p.snapshot;
701 struct printbuf buf = PRINTBUF;
702 bch2_bkey_val_to_text(&buf, c, k);
704 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u\n"
705 "unexpected because we should always update the inode when we update a key in that inode\n"
707 w->last_pos.inode, k.k->p.snapshot, i->snapshot, buf.buf);
710 while (i > w->inodes.data && i[-1].snapshot > k.k->p.snapshot)
713 size_t pos = i - w->inodes.data;
714 int ret = darray_insert_item(&w->inodes, pos, new);
718 i = w->inodes.data + pos;
724 static struct inode_walker_entry *walk_inode(struct btree_trans *trans,
725 struct inode_walker *w,
728 if (w->last_pos.inode != k.k->p.inode) {
729 int ret = get_inodes_all_snapshots(trans, w, k.k->p.inode);
732 } else if (bkey_cmp(w->last_pos, k.k->p)) {
733 darray_for_each(w->inodes, i)
734 i->seen_this_pos = false;
737 w->last_pos = k.k->p;
739 return lookup_inode_for_snapshot(trans->c, w, k);
742 static int get_visible_inodes(struct btree_trans *trans,
743 struct inode_walker *w,
744 struct snapshots_seen *s,
747 struct bch_fs *c = trans->c;
748 struct btree_iter iter;
754 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
755 BTREE_ITER_all_snapshots, k, ret) {
756 if (k.k->p.offset != inum)
759 if (!ref_visible(c, s, s->pos.snapshot, k.k->p.snapshot))
762 if (bkey_is_inode(k.k))
765 if (k.k->p.snapshot >= s->pos.snapshot)
768 bch2_trans_iter_exit(trans, &iter);
773 static int check_key_has_snapshot(struct btree_trans *trans,
774 struct btree_iter *iter,
777 struct bch_fs *c = trans->c;
778 struct printbuf buf = PRINTBUF;
781 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
782 bkey_in_missing_snapshot,
783 "key in missing snapshot: %s",
784 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
785 ret = bch2_btree_delete_at(trans, iter,
786 BTREE_UPDATE_internal_snapshot_node) ?: 1;
792 static int hash_redo_key(struct btree_trans *trans,
793 const struct bch_hash_desc desc,
794 struct bch_hash_info *hash_info,
795 struct btree_iter *k_iter, struct bkey_s_c k)
797 struct bkey_i *delete;
800 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
802 return PTR_ERR(delete);
804 tmp = bch2_bkey_make_mut_noupdate(trans, k);
808 bkey_init(&delete->k);
809 delete->k.p = k_iter->pos;
810 return bch2_btree_iter_traverse(k_iter) ?:
811 bch2_trans_update(trans, k_iter, delete, 0) ?:
812 bch2_hash_set_in_snapshot(trans, desc, hash_info,
813 (subvol_inum) { 0, k.k->p.inode },
814 k.k->p.snapshot, tmp,
815 STR_HASH_must_create|
816 BTREE_UPDATE_internal_snapshot_node) ?:
817 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
820 static int hash_check_key(struct btree_trans *trans,
821 const struct bch_hash_desc desc,
822 struct bch_hash_info *hash_info,
823 struct btree_iter *k_iter, struct bkey_s_c hash_k)
825 struct bch_fs *c = trans->c;
826 struct btree_iter iter = { NULL };
827 struct printbuf buf = PRINTBUF;
832 if (hash_k.k->type != desc.key_type)
835 hash = desc.hash_bkey(hash_info, hash_k);
837 if (likely(hash == hash_k.k->p.offset))
840 if (hash_k.k->p.offset < hash)
843 for_each_btree_key_norestart(trans, iter, desc.btree_id,
844 SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
845 BTREE_ITER_slots, k, ret) {
846 if (bkey_eq(k.k->p, hash_k.k->p))
849 if (fsck_err_on(k.k->type == desc.key_type &&
850 !desc.cmp_bkey(k, hash_k), c,
851 hash_table_key_duplicate,
852 "duplicate hash table keys:\n%s",
853 (printbuf_reset(&buf),
854 bch2_bkey_val_to_text(&buf, c, hash_k),
856 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
860 if (bkey_deleted(k.k)) {
861 bch2_trans_iter_exit(trans, &iter);
866 bch2_trans_iter_exit(trans, &iter);
870 if (fsck_err(c, hash_table_key_wrong_offset,
871 "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
872 bch2_btree_id_str(desc.btree_id), hash_k.k->p.inode, hash_k.k->p.offset, hash,
873 (printbuf_reset(&buf),
874 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
875 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
879 ret = -BCH_ERR_transaction_restart_nested;
885 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
886 struct btree_iter *iter,
889 return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
892 static struct bkey_s_c_dirent inode_get_dirent(struct btree_trans *trans,
893 struct btree_iter *iter,
894 struct bch_inode_unpacked *inode,
897 if (inode->bi_subvol) {
899 int ret = subvol_lookup(trans, inode->bi_parent_subvol, snapshot, &inum);
901 return ((struct bkey_s_c_dirent) { .k = ERR_PTR(ret) });
904 return dirent_get_by_pos(trans, iter, SPOS(inode->bi_dir, inode->bi_dir_offset, *snapshot));
907 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
908 struct bkey_s_c_dirent d)
910 return inode->bi_dir == d.k->p.inode &&
911 inode->bi_dir_offset == d.k->p.offset;
914 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
915 struct bch_inode_unpacked *inode)
917 return d.v->d_type == DT_SUBVOL
918 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
919 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
922 static int check_inode_deleted_list(struct btree_trans *trans, struct bpos p)
924 struct btree_iter iter;
925 struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_deleted_inodes, p, 0);
926 int ret = bkey_err(k) ?: k.k->type == KEY_TYPE_set;
927 bch2_trans_iter_exit(trans, &iter);
931 static int check_inode_dirent_inode(struct btree_trans *trans, struct bkey_s_c inode_k,
932 struct bch_inode_unpacked *inode,
933 u32 inode_snapshot, bool *write_inode)
935 struct bch_fs *c = trans->c;
936 struct printbuf buf = PRINTBUF;
938 struct btree_iter dirent_iter = {};
939 struct bkey_s_c_dirent d = inode_get_dirent(trans, &dirent_iter, inode, &inode_snapshot);
940 int ret = bkey_err(d);
941 if (ret && !bch2_err_matches(ret, ENOENT))
945 c, inode_points_to_missing_dirent,
946 "inode points to missing dirent\n%s",
947 (bch2_bkey_val_to_text(&buf, c, inode_k), buf.buf)) ||
948 fsck_err_on(!ret && !dirent_points_to_inode(d, inode),
949 c, inode_points_to_wrong_dirent,
950 "inode points to dirent that does not point back:\n%s",
951 (bch2_bkey_val_to_text(&buf, c, inode_k),
953 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
955 * We just clear the backpointer fields for now. If we find a
956 * dirent that points to this inode in check_dirents(), we'll
957 * update it then; then when we get to check_path() if the
958 * backpointer is still 0 we'll reattach it.
961 inode->bi_dir_offset = 0;
962 inode->bi_flags &= ~BCH_INODE_backptr_untrusted;
968 bch2_trans_iter_exit(trans, &dirent_iter);
974 static int check_inode(struct btree_trans *trans,
975 struct btree_iter *iter,
977 struct bch_inode_unpacked *prev,
978 struct snapshots_seen *s,
981 struct bch_fs *c = trans->c;
982 struct bch_inode_unpacked u;
983 bool do_update = false;
986 ret = check_key_has_snapshot(trans, iter, k);
992 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
996 if (!bkey_is_inode(k.k))
999 BUG_ON(bch2_inode_unpack(k, &u));
1002 !(u.bi_flags & (BCH_INODE_i_size_dirty|
1003 BCH_INODE_i_sectors_dirty|
1004 BCH_INODE_unlinked)))
1007 if (prev->bi_inum != u.bi_inum)
1010 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
1011 inode_d_type(prev) != inode_d_type(&u),
1012 c, inode_snapshot_mismatch,
1013 "inodes in different snapshots don't match")) {
1014 bch_err(c, "repair not implemented yet");
1015 return -BCH_ERR_fsck_repair_unimplemented;
1018 if ((u.bi_flags & (BCH_INODE_i_size_dirty|BCH_INODE_unlinked)) &&
1019 bch2_key_has_snapshot_overwrites(trans, BTREE_ID_inodes, k.k->p)) {
1020 struct bpos new_min_pos;
1022 ret = bch2_propagate_key_to_snapshot_leaves(trans, iter->btree_id, k, &new_min_pos);
1026 u.bi_flags &= ~BCH_INODE_i_size_dirty|BCH_INODE_unlinked;
1028 ret = __bch2_fsck_write_inode(trans, &u, iter->pos.snapshot);
1030 bch_err_msg(c, ret, "in fsck updating inode");
1034 if (!bpos_eq(new_min_pos, POS_MIN))
1035 bch2_btree_iter_set_pos(iter, bpos_predecessor(new_min_pos));
1039 if (u.bi_flags & BCH_INODE_unlinked) {
1040 ret = check_inode_deleted_list(trans, k.k->p);
1044 fsck_err_on(!ret, c, unlinked_inode_not_on_deleted_list,
1045 "inode %llu:%u unlinked, but not on deleted list",
1046 u.bi_inum, k.k->p.snapshot);
1050 if (u.bi_flags & BCH_INODE_unlinked &&
1052 fsck_err(c, inode_unlinked_but_clean,
1053 "filesystem marked clean, but inode %llu unlinked",
1055 ret = bch2_inode_rm_snapshot(trans, u.bi_inum, iter->pos.snapshot);
1056 bch_err_msg(c, ret, "in fsck deleting inode");
1060 if (u.bi_flags & BCH_INODE_i_size_dirty &&
1062 fsck_err(c, inode_i_size_dirty_but_clean,
1063 "filesystem marked clean, but inode %llu has i_size dirty",
1065 bch_verbose(c, "truncating inode %llu", u.bi_inum);
1068 * XXX: need to truncate partial blocks too here - or ideally
1069 * just switch units to bytes and that issue goes away
1071 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
1072 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
1073 iter->pos.snapshot),
1074 POS(u.bi_inum, U64_MAX),
1076 bch_err_msg(c, ret, "in fsck truncating inode");
1081 * We truncated without our normal sector accounting hook, just
1082 * make sure we recalculate it:
1084 u.bi_flags |= BCH_INODE_i_sectors_dirty;
1086 u.bi_flags &= ~BCH_INODE_i_size_dirty;
1090 if (u.bi_flags & BCH_INODE_i_sectors_dirty &&
1092 fsck_err(c, inode_i_sectors_dirty_but_clean,
1093 "filesystem marked clean, but inode %llu has i_sectors dirty",
1097 bch_verbose(c, "recounting sectors for inode %llu",
1100 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
1102 bch_err_msg(c, sectors, "in fsck recounting inode sectors");
1106 u.bi_sectors = sectors;
1107 u.bi_flags &= ~BCH_INODE_i_sectors_dirty;
1111 if (u.bi_flags & BCH_INODE_backptr_untrusted) {
1113 u.bi_dir_offset = 0;
1114 u.bi_flags &= ~BCH_INODE_backptr_untrusted;
1118 if (u.bi_dir || u.bi_dir_offset) {
1119 ret = check_inode_dirent_inode(trans, k, &u, k.k->p.snapshot, &do_update);
1124 if (fsck_err_on(u.bi_parent_subvol &&
1125 (u.bi_subvol == 0 ||
1126 u.bi_subvol == BCACHEFS_ROOT_SUBVOL),
1127 c, inode_bi_parent_nonzero,
1128 "inode %llu:%u has subvol %u but nonzero parent subvol %u",
1129 u.bi_inum, k.k->p.snapshot, u.bi_subvol, u.bi_parent_subvol)) {
1130 u.bi_parent_subvol = 0;
1135 struct bch_subvolume s;
1137 ret = bch2_subvolume_get(trans, u.bi_subvol, false, 0, &s);
1138 if (ret && !bch2_err_matches(ret, ENOENT))
1141 if (ret && (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_subvolumes))) {
1142 ret = reconstruct_subvol(trans, k.k->p.snapshot, u.bi_subvol, u.bi_inum);
1146 if (fsck_err_on(ret,
1147 c, inode_bi_subvol_missing,
1148 "inode %llu:%u bi_subvol points to missing subvolume %u",
1149 u.bi_inum, k.k->p.snapshot, u.bi_subvol) ||
1150 fsck_err_on(le64_to_cpu(s.inode) != u.bi_inum ||
1151 !bch2_snapshot_is_ancestor(c, le32_to_cpu(s.snapshot),
1153 c, inode_bi_subvol_wrong,
1154 "inode %llu:%u points to subvol %u, but subvol points to %llu:%u",
1155 u.bi_inum, k.k->p.snapshot, u.bi_subvol,
1156 le64_to_cpu(s.inode),
1157 le32_to_cpu(s.snapshot))) {
1159 u.bi_parent_subvol = 0;
1165 ret = __bch2_fsck_write_inode(trans, &u, iter->pos.snapshot);
1166 bch_err_msg(c, ret, "in fsck updating inode");
1176 int bch2_check_inodes(struct bch_fs *c)
1178 bool full = c->opts.fsck;
1179 struct bch_inode_unpacked prev = { 0 };
1180 struct snapshots_seen s;
1182 snapshots_seen_init(&s);
1184 int ret = bch2_trans_run(c,
1185 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
1187 BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
1188 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1189 check_inode(trans, &iter, k, &prev, &s, full)));
1191 snapshots_seen_exit(&s);
1196 static int check_i_sectors_notnested(struct btree_trans *trans, struct inode_walker *w)
1198 struct bch_fs *c = trans->c;
1202 darray_for_each(w->inodes, i) {
1203 if (i->inode.bi_sectors == i->count)
1206 count2 = bch2_count_inode_sectors(trans, w->last_pos.inode, i->snapshot);
1208 if (w->recalculate_sums)
1211 if (i->count != count2) {
1212 bch_err_ratelimited(c, "fsck counted i_sectors wrong for inode %llu:%u: got %llu should be %llu",
1213 w->last_pos.inode, i->snapshot, i->count, count2);
1214 return -BCH_ERR_internal_fsck_err;
1217 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_i_sectors_dirty),
1218 c, inode_i_sectors_wrong,
1219 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1220 w->last_pos.inode, i->snapshot,
1221 i->inode.bi_sectors, i->count)) {
1222 i->inode.bi_sectors = i->count;
1223 ret = bch2_fsck_write_inode(trans, &i->inode, i->snapshot);
1233 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1235 u32 restart_count = trans->restart_count;
1236 return check_i_sectors_notnested(trans, w) ?:
1237 trans_was_restarted(trans, restart_count);
1243 struct snapshots_seen seen;
1246 struct extent_ends {
1247 struct bpos last_pos;
1248 DARRAY(struct extent_end) e;
1251 static void extent_ends_reset(struct extent_ends *extent_ends)
1253 darray_for_each(extent_ends->e, i)
1254 snapshots_seen_exit(&i->seen);
1255 extent_ends->e.nr = 0;
1258 static void extent_ends_exit(struct extent_ends *extent_ends)
1260 extent_ends_reset(extent_ends);
1261 darray_exit(&extent_ends->e);
1264 static void extent_ends_init(struct extent_ends *extent_ends)
1266 memset(extent_ends, 0, sizeof(*extent_ends));
1269 static int extent_ends_at(struct bch_fs *c,
1270 struct extent_ends *extent_ends,
1271 struct snapshots_seen *seen,
1274 struct extent_end *i, n = (struct extent_end) {
1275 .offset = k.k->p.offset,
1276 .snapshot = k.k->p.snapshot,
1280 n.seen.ids.data = kmemdup(seen->ids.data,
1281 sizeof(seen->ids.data[0]) * seen->ids.size,
1283 if (!n.seen.ids.data)
1284 return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1286 __darray_for_each(extent_ends->e, i) {
1287 if (i->snapshot == k.k->p.snapshot) {
1288 snapshots_seen_exit(&i->seen);
1293 if (i->snapshot >= k.k->p.snapshot)
1297 return darray_insert_item(&extent_ends->e, i - extent_ends->e.data, n);
1300 static int overlapping_extents_found(struct btree_trans *trans,
1301 enum btree_id btree,
1302 struct bpos pos1, struct snapshots_seen *pos1_seen,
1305 struct extent_end *extent_end)
1307 struct bch_fs *c = trans->c;
1308 struct printbuf buf = PRINTBUF;
1309 struct btree_iter iter1, iter2 = { NULL };
1310 struct bkey_s_c k1, k2;
1313 BUG_ON(bkey_le(pos1, bkey_start_pos(&pos2)));
1315 bch2_trans_iter_init(trans, &iter1, btree, pos1,
1316 BTREE_ITER_all_snapshots|
1317 BTREE_ITER_not_extents);
1318 k1 = bch2_btree_iter_peek_upto(&iter1, POS(pos1.inode, U64_MAX));
1323 prt_str(&buf, "\n ");
1324 bch2_bkey_val_to_text(&buf, c, k1);
1326 if (!bpos_eq(pos1, k1.k->p)) {
1327 prt_str(&buf, "\n wanted\n ");
1328 bch2_bpos_to_text(&buf, pos1);
1329 prt_str(&buf, "\n ");
1330 bch2_bkey_to_text(&buf, &pos2);
1332 bch_err(c, "%s: error finding first overlapping extent when repairing, got%s",
1334 ret = -BCH_ERR_internal_fsck_err;
1338 bch2_trans_copy_iter(&iter2, &iter1);
1341 bch2_btree_iter_advance(&iter2);
1343 k2 = bch2_btree_iter_peek_upto(&iter2, POS(pos1.inode, U64_MAX));
1348 if (bpos_ge(k2.k->p, pos2.p))
1352 prt_str(&buf, "\n ");
1353 bch2_bkey_val_to_text(&buf, c, k2);
1355 if (bpos_gt(k2.k->p, pos2.p) ||
1356 pos2.size != k2.k->size) {
1357 bch_err(c, "%s: error finding seconding overlapping extent when repairing%s",
1359 ret = -BCH_ERR_internal_fsck_err;
1363 prt_printf(&buf, "\n overwriting %s extent",
1364 pos1.snapshot >= pos2.p.snapshot ? "first" : "second");
1366 if (fsck_err(c, extent_overlapping,
1367 "overlapping extents%s", buf.buf)) {
1368 struct btree_iter *old_iter = &iter1;
1369 struct disk_reservation res = { 0 };
1371 if (pos1.snapshot < pos2.p.snapshot) {
1376 trans->extra_disk_res += bch2_bkey_sectors_compressed(k2);
1378 ret = bch2_trans_update_extent_overwrite(trans, old_iter,
1379 BTREE_UPDATE_internal_snapshot_node,
1381 bch2_trans_commit(trans, &res, NULL, BCH_TRANS_COMMIT_no_enospc);
1382 bch2_disk_reservation_put(c, &res);
1389 if (pos1.snapshot == pos2.p.snapshot) {
1391 * We overwrote the first extent, and did the overwrite
1392 * in the same snapshot:
1394 extent_end->offset = bkey_start_offset(&pos2);
1395 } else if (pos1.snapshot > pos2.p.snapshot) {
1397 * We overwrote the first extent in pos2's snapshot:
1399 ret = snapshots_seen_add_inorder(c, pos1_seen, pos2.p.snapshot);
1402 * We overwrote the second extent - restart
1403 * check_extent() from the top:
1405 ret = -BCH_ERR_transaction_restart_nested;
1410 bch2_trans_iter_exit(trans, &iter2);
1411 bch2_trans_iter_exit(trans, &iter1);
1412 printbuf_exit(&buf);
1416 static int check_overlapping_extents(struct btree_trans *trans,
1417 struct snapshots_seen *seen,
1418 struct extent_ends *extent_ends,
1420 struct btree_iter *iter,
1423 struct bch_fs *c = trans->c;
1426 /* transaction restart, running again */
1427 if (bpos_eq(extent_ends->last_pos, k.k->p))
1430 if (extent_ends->last_pos.inode != k.k->p.inode)
1431 extent_ends_reset(extent_ends);
1433 darray_for_each(extent_ends->e, i) {
1434 if (i->offset <= bkey_start_offset(k.k))
1437 if (!ref_visible2(c,
1438 k.k->p.snapshot, seen,
1439 i->snapshot, &i->seen))
1442 ret = overlapping_extents_found(trans, iter->btree_id,
1443 SPOS(iter->pos.inode,
1452 extent_ends->last_pos = k.k->p;
1457 static int check_extent_overbig(struct btree_trans *trans, struct btree_iter *iter,
1460 struct bch_fs *c = trans->c;
1461 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
1462 struct bch_extent_crc_unpacked crc;
1463 const union bch_extent_entry *i;
1464 unsigned encoded_extent_max_sectors = c->opts.encoded_extent_max >> 9;
1466 bkey_for_each_crc(k.k, ptrs, crc, i)
1467 if (crc_is_encoded(crc) &&
1468 crc.uncompressed_size > encoded_extent_max_sectors) {
1469 struct printbuf buf = PRINTBUF;
1471 bch2_bkey_val_to_text(&buf, c, k);
1472 bch_err(c, "overbig encoded extent, please report this:\n %s", buf.buf);
1473 printbuf_exit(&buf);
1479 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1481 struct inode_walker *inode,
1482 struct snapshots_seen *s,
1483 struct extent_ends *extent_ends)
1485 struct bch_fs *c = trans->c;
1486 struct inode_walker_entry *i;
1487 struct printbuf buf = PRINTBUF;
1490 ret = check_key_has_snapshot(trans, iter, k);
1492 ret = ret < 0 ? ret : 0;
1496 if (inode->last_pos.inode != k.k->p.inode) {
1497 ret = check_i_sectors(trans, inode);
1502 i = walk_inode(trans, inode, k);
1503 ret = PTR_ERR_OR_ZERO(i);
1507 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1511 if (k.k->type != KEY_TYPE_whiteout) {
1512 if (!i && (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_inodes))) {
1513 ret = reconstruct_reg_inode(trans, k.k->p.snapshot, k.k->p.inode) ?:
1514 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
1518 inode->last_pos.inode--;
1519 ret = -BCH_ERR_transaction_restart_nested;
1523 if (fsck_err_on(!i, c, extent_in_missing_inode,
1524 "extent in missing inode:\n %s",
1525 (printbuf_reset(&buf),
1526 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1529 if (fsck_err_on(i &&
1530 !S_ISREG(i->inode.bi_mode) &&
1531 !S_ISLNK(i->inode.bi_mode),
1532 c, extent_in_non_reg_inode,
1533 "extent in non regular inode mode %o:\n %s",
1535 (printbuf_reset(&buf),
1536 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1539 ret = check_overlapping_extents(trans, s, extent_ends, k, iter,
1540 &inode->recalculate_sums);
1546 * Check inodes in reverse order, from oldest snapshots to newest,
1547 * starting from the inode that matches this extent's snapshot. If we
1548 * didn't have one, iterate over all inodes:
1551 i = inode->inodes.data + inode->inodes.nr - 1;
1554 inode->inodes.data && i >= inode->inodes.data;
1556 if (i->snapshot > k.k->p.snapshot ||
1557 !key_visible_in_snapshot(c, s, i->snapshot, k.k->p.snapshot))
1560 if (k.k->type != KEY_TYPE_whiteout) {
1561 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_i_size_dirty) &&
1562 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1563 !bkey_extent_is_reservation(k),
1564 c, extent_past_end_of_inode,
1565 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1566 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1567 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1568 struct btree_iter iter2;
1570 bch2_trans_copy_iter(&iter2, iter);
1571 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1572 ret = bch2_btree_iter_traverse(&iter2) ?:
1573 bch2_btree_delete_at(trans, &iter2,
1574 BTREE_UPDATE_internal_snapshot_node);
1575 bch2_trans_iter_exit(trans, &iter2);
1579 iter->k.type = KEY_TYPE_whiteout;
1582 if (bkey_extent_is_allocation(k.k))
1583 i->count += k.k->size;
1586 i->seen_this_pos = true;
1589 if (k.k->type != KEY_TYPE_whiteout) {
1590 ret = extent_ends_at(c, extent_ends, s, k);
1597 printbuf_exit(&buf);
1601 ret = bch2_btree_delete_at(trans, iter, BTREE_UPDATE_internal_snapshot_node);
1606 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1607 * that i_size an i_sectors are consistent
1609 int bch2_check_extents(struct bch_fs *c)
1611 struct inode_walker w = inode_walker_init();
1612 struct snapshots_seen s;
1613 struct extent_ends extent_ends;
1614 struct disk_reservation res = { 0 };
1616 snapshots_seen_init(&s);
1617 extent_ends_init(&extent_ends);
1619 int ret = bch2_trans_run(c,
1620 for_each_btree_key_commit(trans, iter, BTREE_ID_extents,
1621 POS(BCACHEFS_ROOT_INO, 0),
1622 BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
1624 BCH_TRANS_COMMIT_no_enospc, ({
1625 bch2_disk_reservation_put(c, &res);
1626 check_extent(trans, &iter, k, &w, &s, &extent_ends) ?:
1627 check_extent_overbig(trans, &iter, k);
1629 check_i_sectors_notnested(trans, &w));
1631 bch2_disk_reservation_put(c, &res);
1632 extent_ends_exit(&extent_ends);
1633 inode_walker_exit(&w);
1634 snapshots_seen_exit(&s);
1640 int bch2_check_indirect_extents(struct bch_fs *c)
1642 struct disk_reservation res = { 0 };
1644 int ret = bch2_trans_run(c,
1645 for_each_btree_key_commit(trans, iter, BTREE_ID_reflink,
1647 BTREE_ITER_prefetch, k,
1649 BCH_TRANS_COMMIT_no_enospc, ({
1650 bch2_disk_reservation_put(c, &res);
1651 check_extent_overbig(trans, &iter, k);
1654 bch2_disk_reservation_put(c, &res);
1659 static int check_subdir_count_notnested(struct btree_trans *trans, struct inode_walker *w)
1661 struct bch_fs *c = trans->c;
1665 darray_for_each(w->inodes, i) {
1666 if (i->inode.bi_nlink == i->count)
1669 count2 = bch2_count_subdirs(trans, w->last_pos.inode, i->snapshot);
1673 if (i->count != count2) {
1674 bch_err_ratelimited(c, "fsck counted subdirectories wrong for inum %llu:%u: got %llu should be %llu",
1675 w->last_pos.inode, i->snapshot, i->count, count2);
1677 if (i->inode.bi_nlink == i->count)
1681 if (fsck_err_on(i->inode.bi_nlink != i->count,
1682 c, inode_dir_wrong_nlink,
1683 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1684 w->last_pos.inode, i->snapshot, i->inode.bi_nlink, i->count)) {
1685 i->inode.bi_nlink = i->count;
1686 ret = bch2_fsck_write_inode(trans, &i->inode, i->snapshot);
1696 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1698 u32 restart_count = trans->restart_count;
1699 return check_subdir_count_notnested(trans, w) ?:
1700 trans_was_restarted(trans, restart_count);
1703 static int check_dirent_inode_dirent(struct btree_trans *trans,
1704 struct btree_iter *iter,
1705 struct bkey_s_c_dirent d,
1706 struct bch_inode_unpacked *target,
1707 u32 target_snapshot)
1709 struct bch_fs *c = trans->c;
1710 struct printbuf buf = PRINTBUF;
1713 if (inode_points_to_dirent(target, d))
1716 if (bch2_inode_should_have_bp(target) &&
1717 !fsck_err(c, inode_wrong_backpointer,
1718 "dirent points to inode that does not point back:\n %s",
1719 (bch2_bkey_val_to_text(&buf, c, d.s_c),
1720 prt_printf(&buf, "\n "),
1721 bch2_inode_unpacked_to_text(&buf, target),
1725 if (!target->bi_dir &&
1726 !target->bi_dir_offset) {
1727 target->bi_dir = d.k->p.inode;
1728 target->bi_dir_offset = d.k->p.offset;
1729 return __bch2_fsck_write_inode(trans, target, target_snapshot);
1732 struct btree_iter bp_iter = { NULL };
1733 struct bkey_s_c_dirent bp_dirent = dirent_get_by_pos(trans, &bp_iter,
1734 SPOS(target->bi_dir, target->bi_dir_offset, target_snapshot));
1735 ret = bkey_err(bp_dirent);
1736 if (ret && !bch2_err_matches(ret, ENOENT))
1739 bool backpointer_exists = !ret;
1742 if (fsck_err_on(!backpointer_exists,
1743 c, inode_wrong_backpointer,
1744 "inode %llu:%u has wrong backpointer:\n"
1746 "should be %llu:%llu",
1747 target->bi_inum, target_snapshot,
1749 target->bi_dir_offset,
1752 target->bi_dir = d.k->p.inode;
1753 target->bi_dir_offset = d.k->p.offset;
1754 ret = __bch2_fsck_write_inode(trans, target, target_snapshot);
1758 bch2_bkey_val_to_text(&buf, c, d.s_c);
1760 if (backpointer_exists)
1761 bch2_bkey_val_to_text(&buf, c, bp_dirent.s_c);
1763 if (fsck_err_on(backpointer_exists &&
1764 (S_ISDIR(target->bi_mode) ||
1766 c, inode_dir_multiple_links,
1767 "%s %llu:%u with multiple links\n%s",
1768 S_ISDIR(target->bi_mode) ? "directory" : "subvolume",
1769 target->bi_inum, target_snapshot, buf.buf)) {
1770 ret = __remove_dirent(trans, d.k->p);
1775 * hardlinked file with nlink 0:
1776 * We're just adjusting nlink here so check_nlinks() will pick
1777 * it up, it ignores inodes with nlink 0
1779 if (fsck_err_on(backpointer_exists && !target->bi_nlink,
1780 c, inode_multiple_links_but_nlink_0,
1781 "inode %llu:%u type %s has multiple links but i_nlink 0\n%s",
1782 target->bi_inum, target_snapshot, bch2_d_types[d.v->d_type], buf.buf)) {
1784 target->bi_flags &= ~BCH_INODE_unlinked;
1785 ret = __bch2_fsck_write_inode(trans, target, target_snapshot);
1792 bch2_trans_iter_exit(trans, &bp_iter);
1794 printbuf_exit(&buf);
1799 static int check_dirent_target(struct btree_trans *trans,
1800 struct btree_iter *iter,
1801 struct bkey_s_c_dirent d,
1802 struct bch_inode_unpacked *target,
1803 u32 target_snapshot)
1805 struct bch_fs *c = trans->c;
1806 struct bkey_i_dirent *n;
1807 struct printbuf buf = PRINTBUF;
1810 ret = check_dirent_inode_dirent(trans, iter, d, target, target_snapshot);
1814 if (fsck_err_on(d.v->d_type != inode_d_type(target),
1815 c, dirent_d_type_wrong,
1816 "incorrect d_type: got %s, should be %s:\n%s",
1817 bch2_d_type_str(d.v->d_type),
1818 bch2_d_type_str(inode_d_type(target)),
1819 (printbuf_reset(&buf),
1820 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1821 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1822 ret = PTR_ERR_OR_ZERO(n);
1826 bkey_reassemble(&n->k_i, d.s_c);
1827 n->v.d_type = inode_d_type(target);
1828 if (n->v.d_type == DT_SUBVOL) {
1829 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1830 n->v.d_child_subvol = cpu_to_le32(target->bi_subvol);
1832 n->v.d_inum = cpu_to_le64(target->bi_inum);
1835 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1839 d = dirent_i_to_s_c(n);
1843 printbuf_exit(&buf);
1848 /* find a subvolume that's a descendent of @snapshot: */
1849 static int find_snapshot_subvol(struct btree_trans *trans, u32 snapshot, u32 *subvolid)
1851 struct btree_iter iter;
1855 for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN, 0, k, ret) {
1856 if (k.k->type != KEY_TYPE_subvolume)
1859 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
1860 if (bch2_snapshot_is_ancestor(trans->c, le32_to_cpu(s.v->snapshot), snapshot)) {
1861 bch2_trans_iter_exit(trans, &iter);
1862 *subvolid = k.k->p.offset;
1869 bch2_trans_iter_exit(trans, &iter);
1873 static int check_dirent_to_subvol(struct btree_trans *trans, struct btree_iter *iter,
1874 struct bkey_s_c_dirent d)
1876 struct bch_fs *c = trans->c;
1877 struct btree_iter subvol_iter = {};
1878 struct bch_inode_unpacked subvol_root;
1879 u32 parent_subvol = le32_to_cpu(d.v->d_parent_subvol);
1880 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1881 u32 parent_snapshot;
1882 u32 new_parent_subvol = 0;
1884 struct printbuf buf = PRINTBUF;
1887 ret = subvol_lookup(trans, parent_subvol, &parent_snapshot, &parent_inum);
1888 if (ret && !bch2_err_matches(ret, ENOENT))
1892 (!ret && !bch2_snapshot_is_ancestor(c, parent_snapshot, d.k->p.snapshot))) {
1893 int ret2 = find_snapshot_subvol(trans, d.k->p.snapshot, &new_parent_subvol);
1894 if (ret2 && !bch2_err_matches(ret, ENOENT))
1899 !new_parent_subvol &&
1900 (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_subvolumes))) {
1902 * Couldn't find a subvol for dirent's snapshot - but we lost
1903 * subvols, so we need to reconstruct:
1905 ret = reconstruct_subvol(trans, d.k->p.snapshot, parent_subvol, 0);
1909 parent_snapshot = d.k->p.snapshot;
1912 if (fsck_err_on(ret, c, dirent_to_missing_parent_subvol,
1913 "dirent parent_subvol points to missing subvolume\n%s",
1914 (bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf)) ||
1915 fsck_err_on(!ret && !bch2_snapshot_is_ancestor(c, parent_snapshot, d.k->p.snapshot),
1916 c, dirent_not_visible_in_parent_subvol,
1917 "dirent not visible in parent_subvol (not an ancestor of subvol snap %u)\n%s",
1919 (bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1920 if (!new_parent_subvol) {
1921 bch_err(c, "could not find a subvol for snapshot %u", d.k->p.snapshot);
1922 return -BCH_ERR_fsck_repair_unimplemented;
1925 struct bkey_i_dirent *new_dirent = bch2_bkey_make_mut_typed(trans, iter, &d.s_c, 0, dirent);
1926 ret = PTR_ERR_OR_ZERO(new_dirent);
1930 new_dirent->v.d_parent_subvol = cpu_to_le32(new_parent_subvol);
1933 struct bkey_s_c_subvolume s =
1934 bch2_bkey_get_iter_typed(trans, &subvol_iter,
1935 BTREE_ID_subvolumes, POS(0, target_subvol),
1937 ret = bkey_err(s.s_c);
1938 if (ret && !bch2_err_matches(ret, ENOENT))
1942 if (fsck_err(c, dirent_to_missing_subvol,
1943 "dirent points to missing subvolume\n%s",
1944 (bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf)))
1945 return __remove_dirent(trans, d.k->p);
1950 if (fsck_err_on(le32_to_cpu(s.v->fs_path_parent) != parent_subvol,
1951 c, subvol_fs_path_parent_wrong,
1952 "subvol with wrong fs_path_parent, should be be %u\n%s",
1954 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
1955 struct bkey_i_subvolume *n =
1956 bch2_bkey_make_mut_typed(trans, &subvol_iter, &s.s_c, 0, subvolume);
1957 ret = PTR_ERR_OR_ZERO(n);
1961 n->v.fs_path_parent = cpu_to_le32(parent_subvol);
1964 u64 target_inum = le64_to_cpu(s.v->inode);
1965 u32 target_snapshot = le32_to_cpu(s.v->snapshot);
1967 ret = lookup_inode(trans, target_inum, &subvol_root, &target_snapshot);
1968 if (ret && !bch2_err_matches(ret, ENOENT))
1972 bch_err(c, "subvol %u points to missing inode root %llu", target_subvol, target_inum);
1973 ret = -BCH_ERR_fsck_repair_unimplemented;
1978 if (fsck_err_on(!ret && parent_subvol != subvol_root.bi_parent_subvol,
1979 c, inode_bi_parent_wrong,
1980 "subvol root %llu has wrong bi_parent_subvol: got %u, should be %u",
1982 subvol_root.bi_parent_subvol, parent_subvol)) {
1983 subvol_root.bi_parent_subvol = parent_subvol;
1984 ret = __bch2_fsck_write_inode(trans, &subvol_root, target_snapshot);
1989 ret = check_dirent_target(trans, iter, d, &subvol_root,
1996 bch2_trans_iter_exit(trans, &subvol_iter);
1997 printbuf_exit(&buf);
2001 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
2003 struct bch_hash_info *hash_info,
2004 struct inode_walker *dir,
2005 struct inode_walker *target,
2006 struct snapshots_seen *s)
2008 struct bch_fs *c = trans->c;
2009 struct inode_walker_entry *i;
2010 struct printbuf buf = PRINTBUF;
2013 ret = check_key_has_snapshot(trans, iter, k);
2015 ret = ret < 0 ? ret : 0;
2019 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
2023 if (k.k->type == KEY_TYPE_whiteout)
2026 if (dir->last_pos.inode != k.k->p.inode) {
2027 ret = check_subdir_count(trans, dir);
2032 BUG_ON(!btree_iter_path(trans, iter)->should_be_locked);
2034 i = walk_inode(trans, dir, k);
2035 ret = PTR_ERR_OR_ZERO(i);
2039 if (dir->first_this_inode && dir->inodes.nr)
2040 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
2041 dir->first_this_inode = false;
2043 if (!i && (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_inodes))) {
2044 ret = reconstruct_inode(trans, k.k->p.snapshot, k.k->p.inode, 0, S_IFDIR) ?:
2045 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
2049 dir->last_pos.inode--;
2050 ret = -BCH_ERR_transaction_restart_nested;
2054 if (fsck_err_on(!i, c, dirent_in_missing_dir_inode,
2055 "dirent in nonexisting directory:\n%s",
2056 (printbuf_reset(&buf),
2057 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
2058 ret = bch2_btree_delete_at(trans, iter,
2059 BTREE_UPDATE_internal_snapshot_node);
2066 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode),
2067 c, dirent_in_non_dir_inode,
2068 "dirent in non directory inode type %s:\n%s",
2069 bch2_d_type_str(inode_d_type(&i->inode)),
2070 (printbuf_reset(&buf),
2071 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
2072 ret = bch2_btree_delete_at(trans, iter, 0);
2076 ret = hash_check_key(trans, bch2_dirent_hash_desc, hash_info, iter, k);
2080 /* dirent has been deleted */
2085 if (k.k->type != KEY_TYPE_dirent)
2088 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
2090 if (d.v->d_type == DT_SUBVOL) {
2091 ret = check_dirent_to_subvol(trans, iter, d);
2095 ret = get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
2099 if (fsck_err_on(!target->inodes.nr,
2100 c, dirent_to_missing_inode,
2101 "dirent points to missing inode:\n%s",
2102 (printbuf_reset(&buf),
2103 bch2_bkey_val_to_text(&buf, c, k),
2105 ret = __remove_dirent(trans, d.k->p);
2110 darray_for_each(target->inodes, i) {
2111 ret = check_dirent_target(trans, iter, d,
2112 &i->inode, i->snapshot);
2117 if (d.v->d_type == DT_DIR)
2118 for_each_visible_inode(c, s, dir, d.k->p.snapshot, i)
2124 printbuf_exit(&buf);
2130 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
2133 int bch2_check_dirents(struct bch_fs *c)
2135 struct inode_walker dir = inode_walker_init();
2136 struct inode_walker target = inode_walker_init();
2137 struct snapshots_seen s;
2138 struct bch_hash_info hash_info;
2140 snapshots_seen_init(&s);
2142 int ret = bch2_trans_run(c,
2143 for_each_btree_key_commit(trans, iter, BTREE_ID_dirents,
2144 POS(BCACHEFS_ROOT_INO, 0),
2145 BTREE_ITER_prefetch|BTREE_ITER_all_snapshots,
2148 BCH_TRANS_COMMIT_no_enospc,
2149 check_dirent(trans, &iter, k, &hash_info, &dir, &target, &s)) ?:
2150 check_subdir_count_notnested(trans, &dir));
2152 snapshots_seen_exit(&s);
2153 inode_walker_exit(&dir);
2154 inode_walker_exit(&target);
2159 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
2161 struct bch_hash_info *hash_info,
2162 struct inode_walker *inode)
2164 struct bch_fs *c = trans->c;
2165 struct inode_walker_entry *i;
2168 ret = check_key_has_snapshot(trans, iter, k);
2174 i = walk_inode(trans, inode, k);
2175 ret = PTR_ERR_OR_ZERO(i);
2179 if (inode->first_this_inode && inode->inodes.nr)
2180 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
2181 inode->first_this_inode = false;
2183 if (fsck_err_on(!i, c, xattr_in_missing_inode,
2184 "xattr for missing inode %llu",
2186 return bch2_btree_delete_at(trans, iter, 0);
2191 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
2198 * Walk xattrs: verify that they all have a corresponding inode
2200 int bch2_check_xattrs(struct bch_fs *c)
2202 struct inode_walker inode = inode_walker_init();
2203 struct bch_hash_info hash_info;
2206 ret = bch2_trans_run(c,
2207 for_each_btree_key_commit(trans, iter, BTREE_ID_xattrs,
2208 POS(BCACHEFS_ROOT_INO, 0),
2209 BTREE_ITER_prefetch|BTREE_ITER_all_snapshots,
2212 BCH_TRANS_COMMIT_no_enospc,
2213 check_xattr(trans, &iter, k, &hash_info, &inode)));
2218 static int check_root_trans(struct btree_trans *trans)
2220 struct bch_fs *c = trans->c;
2221 struct bch_inode_unpacked root_inode;
2226 ret = subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
2227 if (ret && !bch2_err_matches(ret, ENOENT))
2230 if (mustfix_fsck_err_on(ret, c, root_subvol_missing,
2231 "root subvol missing")) {
2232 struct bkey_i_subvolume *root_subvol =
2233 bch2_trans_kmalloc(trans, sizeof(*root_subvol));
2234 ret = PTR_ERR_OR_ZERO(root_subvol);
2239 inum = BCACHEFS_ROOT_INO;
2241 bkey_subvolume_init(&root_subvol->k_i);
2242 root_subvol->k.p.offset = BCACHEFS_ROOT_SUBVOL;
2243 root_subvol->v.flags = 0;
2244 root_subvol->v.snapshot = cpu_to_le32(snapshot);
2245 root_subvol->v.inode = cpu_to_le64(inum);
2246 ret = bch2_btree_insert_trans(trans, BTREE_ID_subvolumes, &root_subvol->k_i, 0);
2247 bch_err_msg(c, ret, "writing root subvol");
2252 ret = lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
2253 if (ret && !bch2_err_matches(ret, ENOENT))
2256 if (mustfix_fsck_err_on(ret, c, root_dir_missing,
2257 "root directory missing") ||
2258 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode),
2259 c, root_inode_not_dir,
2260 "root inode not a directory")) {
2261 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
2263 root_inode.bi_inum = inum;
2265 ret = __bch2_fsck_write_inode(trans, &root_inode, snapshot);
2266 bch_err_msg(c, ret, "writing root inode");
2273 /* Get root directory, create if it doesn't exist: */
2274 int bch2_check_root(struct bch_fs *c)
2276 int ret = bch2_trans_do(c, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2277 check_root_trans(trans));
2282 typedef DARRAY(u32) darray_u32;
2284 static bool darray_u32_has(darray_u32 *d, u32 v)
2286 darray_for_each(*d, i)
2293 * We've checked that inode backpointers point to valid dirents; here, it's
2294 * sufficient to check that the subvolume root has a dirent:
2296 static int subvol_has_dirent(struct btree_trans *trans, struct bkey_s_c_subvolume s)
2298 struct bch_inode_unpacked inode;
2299 int ret = bch2_inode_find_by_inum_trans(trans,
2300 (subvol_inum) { s.k->p.offset, le64_to_cpu(s.v->inode) },
2305 return inode.bi_dir != 0;
2308 static int check_subvol_path(struct btree_trans *trans, struct btree_iter *iter, struct bkey_s_c k)
2310 struct bch_fs *c = trans->c;
2311 struct btree_iter parent_iter = {};
2312 darray_u32 subvol_path = {};
2313 struct printbuf buf = PRINTBUF;
2316 if (k.k->type != KEY_TYPE_subvolume)
2319 while (k.k->p.offset != BCACHEFS_ROOT_SUBVOL) {
2320 ret = darray_push(&subvol_path, k.k->p.offset);
2324 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
2326 ret = subvol_has_dirent(trans, s);
2330 if (fsck_err_on(!ret,
2331 c, subvol_unreachable,
2332 "unreachable subvolume %s",
2333 (bch2_bkey_val_to_text(&buf, c, s.s_c),
2335 ret = reattach_subvol(trans, s);
2339 u32 parent = le32_to_cpu(s.v->fs_path_parent);
2341 if (darray_u32_has(&subvol_path, parent)) {
2342 if (fsck_err(c, subvol_loop, "subvolume loop"))
2343 ret = reattach_subvol(trans, s);
2347 bch2_trans_iter_exit(trans, &parent_iter);
2348 bch2_trans_iter_init(trans, &parent_iter,
2349 BTREE_ID_subvolumes, POS(0, parent), 0);
2350 k = bch2_btree_iter_peek_slot(&parent_iter);
2355 if (fsck_err_on(k.k->type != KEY_TYPE_subvolume,
2356 c, subvol_unreachable,
2357 "unreachable subvolume %s",
2358 (bch2_bkey_val_to_text(&buf, c, s.s_c),
2360 ret = reattach_subvol(trans, s);
2366 printbuf_exit(&buf);
2367 darray_exit(&subvol_path);
2368 bch2_trans_iter_exit(trans, &parent_iter);
2372 int bch2_check_subvolume_structure(struct bch_fs *c)
2374 int ret = bch2_trans_run(c,
2375 for_each_btree_key_commit(trans, iter,
2376 BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_prefetch, k,
2377 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2378 check_subvol_path(trans, &iter, k)));
2383 struct pathbuf_entry {
2388 typedef DARRAY(struct pathbuf_entry) pathbuf;
2390 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
2392 darray_for_each(*p, i)
2393 if (i->inum == inum &&
2394 i->snapshot == snapshot)
2400 * Check that a given inode is reachable from its subvolume root - we already
2401 * verified subvolume connectivity:
2403 * XXX: we should also be verifying that inodes are in the right subvolumes
2405 static int check_path(struct btree_trans *trans, pathbuf *p, struct bkey_s_c inode_k)
2407 struct bch_fs *c = trans->c;
2408 struct btree_iter inode_iter = {};
2409 struct bch_inode_unpacked inode;
2410 struct printbuf buf = PRINTBUF;
2411 u32 snapshot = inode_k.k->p.snapshot;
2416 BUG_ON(bch2_inode_unpack(inode_k, &inode));
2418 while (!inode.bi_subvol) {
2419 struct btree_iter dirent_iter;
2420 struct bkey_s_c_dirent d;
2421 u32 parent_snapshot = snapshot;
2423 d = inode_get_dirent(trans, &dirent_iter, &inode, &parent_snapshot);
2424 ret = bkey_err(d.s_c);
2425 if (ret && !bch2_err_matches(ret, ENOENT))
2428 if (!ret && !dirent_points_to_inode(d, &inode)) {
2429 bch2_trans_iter_exit(trans, &dirent_iter);
2430 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
2433 if (bch2_err_matches(ret, ENOENT)) {
2435 if (fsck_err(c, inode_unreachable,
2436 "unreachable inode\n%s",
2437 (printbuf_reset(&buf),
2438 bch2_bkey_val_to_text(&buf, c, inode_k),
2440 ret = reattach_inode(trans, &inode, snapshot);
2444 bch2_trans_iter_exit(trans, &dirent_iter);
2446 if (!S_ISDIR(inode.bi_mode))
2449 ret = darray_push(p, ((struct pathbuf_entry) {
2450 .inum = inode.bi_inum,
2451 .snapshot = snapshot,
2456 snapshot = parent_snapshot;
2458 bch2_trans_iter_exit(trans, &inode_iter);
2459 inode_k = bch2_bkey_get_iter(trans, &inode_iter, BTREE_ID_inodes,
2460 SPOS(0, inode.bi_dir, snapshot), 0);
2461 ret = bkey_err(inode_k) ?:
2462 !bkey_is_inode(inode_k.k) ? -BCH_ERR_ENOENT_inode
2463 : bch2_inode_unpack(inode_k, &inode);
2465 /* Should have been caught in dirents pass */
2466 if (!bch2_err_matches(ret, BCH_ERR_transaction_restart))
2467 bch_err(c, "error looking up parent directory: %i", ret);
2471 snapshot = inode_k.k->p.snapshot;
2473 if (path_is_dup(p, inode.bi_inum, snapshot)) {
2474 /* XXX print path */
2475 bch_err(c, "directory structure loop");
2477 darray_for_each(*p, i)
2478 pr_err("%llu:%u", i->inum, i->snapshot);
2479 pr_err("%llu:%u", inode.bi_inum, snapshot);
2481 if (fsck_err(c, dir_loop, "directory structure loop")) {
2482 ret = remove_backpointer(trans, &inode);
2483 bch_err_msg(c, ret, "removing dirent");
2487 ret = reattach_inode(trans, &inode, snapshot);
2488 bch_err_msg(c, ret, "reattaching inode %llu", inode.bi_inum);
2495 bch2_trans_iter_exit(trans, &inode_iter);
2496 printbuf_exit(&buf);
2502 * Check for unreachable inodes, as well as loops in the directory structure:
2503 * After bch2_check_dirents(), if an inode backpointer doesn't exist that means it's
2506 int bch2_check_directory_structure(struct bch_fs *c)
2508 pathbuf path = { 0, };
2511 ret = bch2_trans_run(c,
2512 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes, POS_MIN,
2514 BTREE_ITER_prefetch|
2515 BTREE_ITER_all_snapshots, k,
2516 NULL, NULL, BCH_TRANS_COMMIT_no_enospc, ({
2517 if (!bkey_is_inode(k.k))
2520 if (bch2_inode_flags(k) & BCH_INODE_unlinked)
2523 check_path(trans, &path, k);
2531 struct nlink_table {
2542 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2543 u64 inum, u32 snapshot)
2545 if (t->nr == t->size) {
2546 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2547 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2550 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2552 return -BCH_ERR_ENOMEM_fsck_add_nlink;
2556 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2564 t->d[t->nr++] = (struct nlink) {
2566 .snapshot = snapshot,
2572 static int nlink_cmp(const void *_l, const void *_r)
2574 const struct nlink *l = _l;
2575 const struct nlink *r = _r;
2577 return cmp_int(l->inum, r->inum);
2580 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2581 struct nlink_table *links,
2582 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2584 struct nlink *link, key = {
2585 .inum = inum, .snapshot = U32_MAX,
2588 if (inum < range_start || inum >= range_end)
2591 link = __inline_bsearch(&key, links->d, links->nr,
2592 sizeof(links->d[0]), nlink_cmp);
2596 while (link > links->d && link[0].inum == link[-1].inum)
2599 for (; link < links->d + links->nr && link->inum == inum; link++)
2600 if (ref_visible(c, s, snapshot, link->snapshot)) {
2602 if (link->snapshot >= snapshot)
2608 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2609 struct nlink_table *t,
2610 u64 start, u64 *end)
2612 int ret = bch2_trans_run(c,
2613 for_each_btree_key(trans, iter, BTREE_ID_inodes,
2616 BTREE_ITER_prefetch|
2617 BTREE_ITER_all_snapshots, k, ({
2618 if (!bkey_is_inode(k.k))
2621 /* Should never fail, checked by bch2_inode_invalid: */
2622 struct bch_inode_unpacked u;
2623 BUG_ON(bch2_inode_unpack(k, &u));
2626 * Backpointer and directory structure checks are sufficient for
2627 * directories, since they can't have hardlinks:
2629 if (S_ISDIR(u.bi_mode))
2635 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2637 *end = k.k->p.offset;
2649 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2650 u64 range_start, u64 range_end)
2652 struct snapshots_seen s;
2654 snapshots_seen_init(&s);
2656 int ret = bch2_trans_run(c,
2657 for_each_btree_key(trans, iter, BTREE_ID_dirents, POS_MIN,
2659 BTREE_ITER_prefetch|
2660 BTREE_ITER_all_snapshots, k, ({
2661 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2665 if (k.k->type == KEY_TYPE_dirent) {
2666 struct bkey_s_c_dirent d = bkey_s_c_to_dirent(k);
2668 if (d.v->d_type != DT_DIR &&
2669 d.v->d_type != DT_SUBVOL)
2670 inc_link(c, &s, links, range_start, range_end,
2671 le64_to_cpu(d.v->d_inum), d.k->p.snapshot);
2676 snapshots_seen_exit(&s);
2682 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2684 struct nlink_table *links,
2685 size_t *idx, u64 range_end)
2687 struct bch_fs *c = trans->c;
2688 struct bch_inode_unpacked u;
2689 struct nlink *link = &links->d[*idx];
2692 if (k.k->p.offset >= range_end)
2695 if (!bkey_is_inode(k.k))
2698 BUG_ON(bch2_inode_unpack(k, &u));
2700 if (S_ISDIR(u.bi_mode))
2706 while ((cmp_int(link->inum, k.k->p.offset) ?:
2707 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2708 BUG_ON(*idx == links->nr);
2709 link = &links->d[++*idx];
2712 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count,
2713 c, inode_wrong_nlink,
2714 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2715 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2716 bch2_inode_nlink_get(&u), link->count)) {
2717 bch2_inode_nlink_set(&u, link->count);
2718 ret = __bch2_fsck_write_inode(trans, &u, k.k->p.snapshot);
2725 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2726 struct nlink_table *links,
2727 u64 range_start, u64 range_end)
2731 int ret = bch2_trans_run(c,
2732 for_each_btree_key_commit(trans, iter, BTREE_ID_inodes,
2733 POS(0, range_start),
2734 BTREE_ITER_intent|BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
2735 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2736 check_nlinks_update_inode(trans, &iter, k, links, &idx, range_end)));
2738 bch_err(c, "error in fsck walking inodes: %s", bch2_err_str(ret));
2745 int bch2_check_nlinks(struct bch_fs *c)
2747 struct nlink_table links = { 0 };
2748 u64 this_iter_range_start, next_iter_range_start = 0;
2752 this_iter_range_start = next_iter_range_start;
2753 next_iter_range_start = U64_MAX;
2755 ret = check_nlinks_find_hardlinks(c, &links,
2756 this_iter_range_start,
2757 &next_iter_range_start);
2759 ret = check_nlinks_walk_dirents(c, &links,
2760 this_iter_range_start,
2761 next_iter_range_start);
2765 ret = check_nlinks_update_hardlinks(c, &links,
2766 this_iter_range_start,
2767 next_iter_range_start);
2772 } while (next_iter_range_start != U64_MAX);
2779 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2782 struct bkey_s_c_reflink_p p;
2783 struct bkey_i_reflink_p *u;
2785 if (k.k->type != KEY_TYPE_reflink_p)
2788 p = bkey_s_c_to_reflink_p(k);
2790 if (!p.v->front_pad && !p.v->back_pad)
2793 u = bch2_trans_kmalloc(trans, sizeof(*u));
2794 int ret = PTR_ERR_OR_ZERO(u);
2798 bkey_reassemble(&u->k_i, k);
2802 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_norun);
2805 int bch2_fix_reflink_p(struct bch_fs *c)
2807 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2810 int ret = bch2_trans_run(c,
2811 for_each_btree_key_commit(trans, iter,
2812 BTREE_ID_extents, POS_MIN,
2813 BTREE_ITER_intent|BTREE_ITER_prefetch|
2814 BTREE_ITER_all_snapshots, k,
2815 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
2816 fix_reflink_p_key(trans, &iter, k)));