1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
10 #include "fs-common.h"
15 #include "subvolume.h"
19 #include <linux/bsearch.h>
20 #include <linux/dcache.h> /* struct qstr */
22 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
25 * XXX: this is handling transaction restarts without returning
26 * -BCH_ERR_transaction_restart_nested, this is not how we do things anymore:
28 static s64 bch2_count_inode_sectors(struct btree_trans *trans, u64 inum,
31 struct btree_iter iter;
36 for_each_btree_key_upto(trans, iter, BTREE_ID_extents,
37 SPOS(inum, 0, snapshot),
40 if (bkey_extent_is_allocation(k.k))
43 bch2_trans_iter_exit(trans, &iter);
45 return ret ?: sectors;
48 static s64 bch2_count_subdirs(struct btree_trans *trans, u64 inum,
51 struct btree_iter iter;
53 struct bkey_s_c_dirent d;
57 for_each_btree_key_upto(trans, iter, BTREE_ID_dirents,
58 SPOS(inum, 0, snapshot),
61 if (k.k->type != KEY_TYPE_dirent)
64 d = bkey_s_c_to_dirent(k);
65 if (d.v->d_type == DT_DIR)
68 bch2_trans_iter_exit(trans, &iter);
70 return ret ?: subdirs;
73 static int __snapshot_lookup_subvol(struct btree_trans *trans, u32 snapshot,
76 struct bch_snapshot s;
77 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots,
81 *subvol = le32_to_cpu(s.subvol);
82 else if (bch2_err_matches(ret, ENOENT))
83 bch_err(trans->c, "snapshot %u not fonud", snapshot);
88 static int __subvol_lookup(struct btree_trans *trans, u32 subvol,
89 u32 *snapshot, u64 *inum)
91 struct bch_subvolume s;
94 ret = bch2_subvolume_get(trans, subvol, false, 0, &s);
96 *snapshot = le32_to_cpu(s.snapshot);
97 *inum = le64_to_cpu(s.inode);
101 static int subvol_lookup(struct btree_trans *trans, u32 subvol,
102 u32 *snapshot, u64 *inum)
104 return lockrestart_do(trans, __subvol_lookup(trans, subvol, snapshot, inum));
107 static int lookup_first_inode(struct btree_trans *trans, u64 inode_nr,
108 struct bch_inode_unpacked *inode)
110 struct btree_iter iter;
114 bch2_trans_iter_init(trans, &iter, BTREE_ID_inodes,
116 BTREE_ITER_ALL_SNAPSHOTS);
117 k = bch2_btree_iter_peek(&iter);
122 if (!k.k || !bkey_eq(k.k->p, POS(0, inode_nr))) {
123 ret = -BCH_ERR_ENOENT_inode;
127 ret = bch2_inode_unpack(k, inode);
129 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
130 bch_err(trans->c, "error fetching inode %llu: %s",
131 inode_nr, bch2_err_str(ret));
132 bch2_trans_iter_exit(trans, &iter);
136 static int __lookup_inode(struct btree_trans *trans, u64 inode_nr,
137 struct bch_inode_unpacked *inode,
140 struct btree_iter iter;
144 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
145 SPOS(0, inode_nr, *snapshot), 0);
150 ret = bkey_is_inode(k.k)
151 ? bch2_inode_unpack(k, inode)
152 : -BCH_ERR_ENOENT_inode;
154 *snapshot = iter.pos.snapshot;
156 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
157 bch_err(trans->c, "error fetching inode %llu:%u: %s",
158 inode_nr, *snapshot, bch2_err_str(ret));
159 bch2_trans_iter_exit(trans, &iter);
163 static int lookup_inode(struct btree_trans *trans, u64 inode_nr,
164 struct bch_inode_unpacked *inode,
167 return lockrestart_do(trans, __lookup_inode(trans, inode_nr, inode, snapshot));
170 static int __lookup_dirent(struct btree_trans *trans,
171 struct bch_hash_info hash_info,
172 subvol_inum dir, struct qstr *name,
173 u64 *target, unsigned *type)
175 struct btree_iter iter;
176 struct bkey_s_c_dirent d;
179 ret = bch2_hash_lookup(trans, &iter, bch2_dirent_hash_desc,
180 &hash_info, dir, name, 0);
184 d = bkey_s_c_to_dirent(bch2_btree_iter_peek_slot(&iter));
185 *target = le64_to_cpu(d.v->d_inum);
187 bch2_trans_iter_exit(trans, &iter);
191 static int __write_inode(struct btree_trans *trans,
192 struct bch_inode_unpacked *inode,
195 struct bkey_inode_buf *inode_p =
196 bch2_trans_kmalloc(trans, sizeof(*inode_p));
199 return PTR_ERR(inode_p);
201 bch2_inode_pack(inode_p, inode);
202 inode_p->inode.k.p.snapshot = snapshot;
204 return bch2_btree_insert_nonextent(trans, BTREE_ID_inodes,
206 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
209 static int write_inode(struct btree_trans *trans,
210 struct bch_inode_unpacked *inode,
213 int ret = commit_do(trans, NULL, NULL,
215 BTREE_INSERT_LAZY_RW,
216 __write_inode(trans, inode, snapshot));
218 bch_err(trans->c, "error in fsck: error updating inode: %s",
223 static int __remove_dirent(struct btree_trans *trans, struct bpos pos)
225 struct bch_fs *c = trans->c;
226 struct btree_iter iter;
227 struct bch_inode_unpacked dir_inode;
228 struct bch_hash_info dir_hash_info;
231 ret = lookup_first_inode(trans, pos.inode, &dir_inode);
235 dir_hash_info = bch2_hash_info_init(c, &dir_inode);
237 bch2_trans_iter_init(trans, &iter, BTREE_ID_dirents, pos, BTREE_ITER_INTENT);
239 ret = bch2_hash_delete_at(trans, bch2_dirent_hash_desc,
240 &dir_hash_info, &iter,
241 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
242 bch2_trans_iter_exit(trans, &iter);
244 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
249 /* Get lost+found, create if it doesn't exist: */
250 static int lookup_lostfound(struct btree_trans *trans, u32 subvol,
251 struct bch_inode_unpacked *lostfound)
253 struct bch_fs *c = trans->c;
254 struct bch_inode_unpacked root;
255 struct bch_hash_info root_hash_info;
256 struct qstr lostfound_str = QSTR("lost+found");
257 subvol_inum root_inum = { .subvol = subvol };
263 ret = __subvol_lookup(trans, subvol, &snapshot, &root_inum.inum);
267 ret = __lookup_inode(trans, root_inum.inum, &root, &snapshot);
271 root_hash_info = bch2_hash_info_init(c, &root);
273 ret = __lookup_dirent(trans, root_hash_info, root_inum,
274 &lostfound_str, &inum, &d_type);
275 if (bch2_err_matches(ret, ENOENT)) {
276 bch_notice(c, "creating lost+found");
277 goto create_lostfound;
280 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
281 bch_err(c, "error looking up lost+found: %s", bch2_err_str(ret));
285 if (d_type != DT_DIR) {
286 bch_err(c, "error looking up lost+found: not a directory");
291 * The bch2_check_dirents pass has already run, dangling dirents
292 * shouldn't exist here:
294 return __lookup_inode(trans, inum, lostfound, &snapshot);
297 bch2_inode_init_early(c, lostfound);
299 ret = bch2_create_trans(trans, root_inum, &root,
300 lostfound, &lostfound_str,
301 0, 0, S_IFDIR|0700, 0, NULL, NULL,
302 (subvol_inum) { }, 0);
303 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
304 bch_err(c, "error creating lost+found: %s", bch2_err_str(ret));
308 static int __reattach_inode(struct btree_trans *trans,
309 struct bch_inode_unpacked *inode,
312 struct bch_hash_info dir_hash;
313 struct bch_inode_unpacked lostfound;
320 ret = __snapshot_lookup_subvol(trans, inode_snapshot, &subvol);
324 ret = lookup_lostfound(trans, subvol, &lostfound);
328 if (S_ISDIR(inode->bi_mode)) {
329 lostfound.bi_nlink++;
331 ret = __write_inode(trans, &lostfound, U32_MAX);
336 dir_hash = bch2_hash_info_init(trans->c, &lostfound);
338 snprintf(name_buf, sizeof(name_buf), "%llu", inode->bi_inum);
339 name = (struct qstr) QSTR(name_buf);
341 ret = bch2_dirent_create(trans,
344 .inum = lostfound.bi_inum,
348 &name, inode->bi_inum, &dir_offset,
349 BCH_HASH_SET_MUST_CREATE);
353 inode->bi_dir = lostfound.bi_inum;
354 inode->bi_dir_offset = dir_offset;
356 return __write_inode(trans, inode, inode_snapshot);
359 static int reattach_inode(struct btree_trans *trans,
360 struct bch_inode_unpacked *inode,
363 int ret = commit_do(trans, NULL, NULL,
364 BTREE_INSERT_LAZY_RW|
366 __reattach_inode(trans, inode, inode_snapshot));
368 bch_err(trans->c, "error reattaching inode %llu: %s",
369 inode->bi_inum, bch2_err_str(ret));
376 static int remove_backpointer(struct btree_trans *trans,
377 struct bch_inode_unpacked *inode)
379 struct btree_iter iter;
380 struct bkey_s_c_dirent d;
383 d = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_dirents,
384 POS(inode->bi_dir, inode->bi_dir_offset), 0,
387 __remove_dirent(trans, d.k->p);
388 bch2_trans_iter_exit(trans, &iter);
392 struct snapshots_seen_entry {
397 struct snapshots_seen {
399 DARRAY(struct snapshots_seen_entry) ids;
402 static inline void snapshots_seen_exit(struct snapshots_seen *s)
404 darray_exit(&s->ids);
407 static inline void snapshots_seen_init(struct snapshots_seen *s)
409 memset(s, 0, sizeof(*s));
412 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
413 enum btree_id btree_id, struct bpos pos)
415 struct snapshots_seen_entry *i, n = {
417 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
421 if (!bkey_eq(s->pos, pos))
425 s->pos.snapshot = n.equiv;
427 darray_for_each(s->ids, i) {
432 * We currently don't rigorously track for snapshot cleanup
433 * needing to be run, so it shouldn't be a fsck error yet:
435 if (i->equiv == n.equiv) {
436 bch_err(c, "snapshot deletion did not finish:\n"
437 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
438 bch2_btree_ids[btree_id],
439 pos.inode, pos.offset,
440 i->id, n.id, n.equiv);
441 return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_delete_dead_snapshots);
445 ret = darray_push(&s->ids, n);
447 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
453 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
454 * and @ancestor hasn't been overwritten in @seen
456 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
458 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
459 u32 id, u32 ancestor)
463 EBUG_ON(id > ancestor);
464 EBUG_ON(!bch2_snapshot_is_equiv(c, id));
465 EBUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
467 /* @ancestor should be the snapshot most recently added to @seen */
468 EBUG_ON(ancestor != seen->pos.snapshot);
469 EBUG_ON(ancestor != seen->ids.data[seen->ids.nr - 1].equiv);
474 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
478 * We know that @id is a descendant of @ancestor, we're checking if
479 * we've seen a key that overwrote @ancestor - i.e. also a descendent of
480 * @ascestor and with @id as a descendent.
482 * But we already know that we're scanning IDs between @id and @ancestor
483 * numerically, since snapshot ID lists are kept sorted, so if we find
484 * an id that's an ancestor of @id we're done:
487 for (i = seen->ids.nr - 2;
488 i >= 0 && seen->ids.data[i].equiv >= id;
490 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv))
497 * ref_visible - given a key with snapshot id @src that points to a key with
498 * snapshot id @dst, test whether there is some snapshot in which @dst is
501 * This assumes we're visiting @src keys in natural key order.
503 * @s - list of snapshot IDs already seen at @src
504 * @src - snapshot ID of src key
505 * @dst - snapshot ID of dst key
507 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
511 ? key_visible_in_snapshot(c, s, dst, src)
512 : bch2_snapshot_is_ancestor(c, src, dst);
515 static int ref_visible2(struct bch_fs *c,
516 u32 src, struct snapshots_seen *src_seen,
517 u32 dst, struct snapshots_seen *dst_seen)
519 src = bch2_snapshot_equiv(c, src);
520 dst = bch2_snapshot_equiv(c, dst);
524 swap(dst_seen, src_seen);
526 return key_visible_in_snapshot(c, src_seen, dst, src);
529 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
530 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
531 (_i)->snapshot <= (_snapshot); _i++) \
532 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
534 struct inode_walker_entry {
535 struct bch_inode_unpacked inode;
541 struct inode_walker {
542 bool first_this_inode;
543 bool recalculate_sums;
544 struct bpos last_pos;
546 DARRAY(struct inode_walker_entry) inodes;
549 static void inode_walker_exit(struct inode_walker *w)
551 darray_exit(&w->inodes);
554 static struct inode_walker inode_walker_init(void)
556 return (struct inode_walker) { 0, };
559 static int add_inode(struct bch_fs *c, struct inode_walker *w,
560 struct bkey_s_c inode)
562 struct bch_inode_unpacked u;
564 BUG_ON(bch2_inode_unpack(inode, &u));
566 return darray_push(&w->inodes, ((struct inode_walker_entry) {
568 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
572 static int get_inodes_all_snapshots(struct btree_trans *trans,
573 struct inode_walker *w, u64 inum)
575 struct bch_fs *c = trans->c;
576 struct btree_iter iter;
578 u32 restart_count = trans->restart_count;
581 w->recalculate_sums = false;
584 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
585 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
586 if (k.k->p.offset != inum)
589 if (bkey_is_inode(k.k))
592 bch2_trans_iter_exit(trans, &iter);
597 w->first_this_inode = true;
599 if (trans_was_restarted(trans, restart_count))
600 return -BCH_ERR_transaction_restart_nested;
605 static struct inode_walker_entry *
606 lookup_inode_for_snapshot(struct bch_fs *c, struct inode_walker *w,
607 u32 snapshot, bool is_whiteout)
609 struct inode_walker_entry *i;
611 snapshot = bch2_snapshot_equiv(c, snapshot);
613 darray_for_each(w->inodes, i)
614 if (bch2_snapshot_is_ancestor(c, snapshot, i->snapshot))
619 BUG_ON(snapshot > i->snapshot);
621 if (snapshot != i->snapshot && !is_whiteout) {
622 struct inode_walker_entry new = *i;
626 new.snapshot = snapshot;
629 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
630 w->last_pos.inode, snapshot, i->snapshot);
632 while (i > w->inodes.data && i[-1].snapshot > snapshot)
635 pos = i - w->inodes.data;
636 ret = darray_insert_item(&w->inodes, pos, new);
640 i = w->inodes.data + pos;
646 static struct inode_walker_entry *walk_inode(struct btree_trans *trans,
647 struct inode_walker *w, struct bpos pos,
650 if (w->last_pos.inode != pos.inode) {
651 int ret = get_inodes_all_snapshots(trans, w, pos.inode);
654 } else if (bkey_cmp(w->last_pos, pos)) {
655 struct inode_walker_entry *i;
657 darray_for_each(w->inodes, i)
658 i->seen_this_pos = false;
664 return lookup_inode_for_snapshot(trans->c, w, pos.snapshot, is_whiteout);
667 static int __get_visible_inodes(struct btree_trans *trans,
668 struct inode_walker *w,
669 struct snapshots_seen *s,
672 struct bch_fs *c = trans->c;
673 struct btree_iter iter;
679 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
680 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
681 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
683 if (k.k->p.offset != inum)
686 if (!ref_visible(c, s, s->pos.snapshot, equiv))
689 if (bkey_is_inode(k.k))
692 if (equiv >= s->pos.snapshot)
695 bch2_trans_iter_exit(trans, &iter);
700 static int check_key_has_snapshot(struct btree_trans *trans,
701 struct btree_iter *iter,
704 struct bch_fs *c = trans->c;
705 struct printbuf buf = PRINTBUF;
708 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
709 "key in missing snapshot: %s",
710 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
711 ret = bch2_btree_delete_at(trans, iter,
712 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
718 static int hash_redo_key(struct btree_trans *trans,
719 const struct bch_hash_desc desc,
720 struct bch_hash_info *hash_info,
721 struct btree_iter *k_iter, struct bkey_s_c k)
723 struct bkey_i *delete;
726 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
728 return PTR_ERR(delete);
730 tmp = bch2_bkey_make_mut_noupdate(trans, k);
734 bkey_init(&delete->k);
735 delete->k.p = k_iter->pos;
736 return bch2_btree_iter_traverse(k_iter) ?:
737 bch2_trans_update(trans, k_iter, delete, 0) ?:
738 bch2_hash_set_snapshot(trans, desc, hash_info,
739 (subvol_inum) { 0, k.k->p.inode },
740 k.k->p.snapshot, tmp,
741 BCH_HASH_SET_MUST_CREATE,
742 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
743 bch2_trans_commit(trans, NULL, NULL,
745 BTREE_INSERT_LAZY_RW);
748 static int hash_check_key(struct btree_trans *trans,
749 const struct bch_hash_desc desc,
750 struct bch_hash_info *hash_info,
751 struct btree_iter *k_iter, struct bkey_s_c hash_k)
753 struct bch_fs *c = trans->c;
754 struct btree_iter iter = { NULL };
755 struct printbuf buf = PRINTBUF;
760 if (hash_k.k->type != desc.key_type)
763 hash = desc.hash_bkey(hash_info, hash_k);
765 if (likely(hash == hash_k.k->p.offset))
768 if (hash_k.k->p.offset < hash)
771 for_each_btree_key_norestart(trans, iter, desc.btree_id,
772 SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
773 BTREE_ITER_SLOTS, k, ret) {
774 if (bkey_eq(k.k->p, hash_k.k->p))
777 if (fsck_err_on(k.k->type == desc.key_type &&
778 !desc.cmp_bkey(k, hash_k), c,
779 "duplicate hash table keys:\n%s",
780 (printbuf_reset(&buf),
781 bch2_bkey_val_to_text(&buf, c, hash_k),
783 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
787 if (bkey_deleted(k.k)) {
788 bch2_trans_iter_exit(trans, &iter);
793 bch2_trans_iter_exit(trans, &iter);
797 if (fsck_err(c, "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
798 bch2_btree_ids[desc.btree_id], hash_k.k->p.inode, hash_k.k->p.offset, hash,
799 (printbuf_reset(&buf),
800 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
801 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
802 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
803 bch_err(c, "hash_redo_key err %s", bch2_err_str(ret));
806 ret = -BCH_ERR_transaction_restart_nested;
812 static int check_inode(struct btree_trans *trans,
813 struct btree_iter *iter,
815 struct bch_inode_unpacked *prev,
816 struct snapshots_seen *s,
819 struct bch_fs *c = trans->c;
820 struct bch_inode_unpacked u;
821 bool do_update = false;
824 ret = check_key_has_snapshot(trans, iter, k);
830 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
835 * if snapshot id isn't a leaf node, skip it - deletion in
836 * particular is not atomic, so on the internal snapshot nodes
837 * we can see inodes marked for deletion after a clean shutdown
839 if (bch2_snapshot_is_internal_node(c, k.k->p.snapshot))
842 if (!bkey_is_inode(k.k))
845 BUG_ON(bch2_inode_unpack(k, &u));
848 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
849 BCH_INODE_I_SECTORS_DIRTY|
850 BCH_INODE_UNLINKED)))
853 if (prev->bi_inum != u.bi_inum)
856 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
857 inode_d_type(prev) != inode_d_type(&u), c,
858 "inodes in different snapshots don't match")) {
859 bch_err(c, "repair not implemented yet");
863 if (u.bi_flags & BCH_INODE_UNLINKED &&
865 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
867 bch2_trans_unlock(trans);
870 ret = bch2_inode_rm_snapshot(trans, u.bi_inum, iter->pos.snapshot);
871 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
872 bch_err(c, "error in fsck: error while deleting inode: %s",
877 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
879 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
881 bch_verbose(c, "truncating inode %llu", u.bi_inum);
883 bch2_trans_unlock(trans);
887 * XXX: need to truncate partial blocks too here - or ideally
888 * just switch units to bytes and that issue goes away
890 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
891 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
893 POS(u.bi_inum, U64_MAX),
895 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
896 bch_err(c, "error in fsck: error truncating inode: %s",
902 * We truncated without our normal sector accounting hook, just
903 * make sure we recalculate it:
905 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
907 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
911 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
913 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
917 bch_verbose(c, "recounting sectors for inode %llu",
920 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
922 bch_err(c, "error in fsck: error recounting inode sectors: %s",
923 bch2_err_str(sectors));
927 u.bi_sectors = sectors;
928 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
932 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
935 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
940 ret = __write_inode(trans, &u, iter->pos.snapshot);
942 bch_err(c, "error in fsck: error updating inode: %s",
953 int bch2_check_inodes(struct bch_fs *c)
955 bool full = c->opts.fsck;
956 struct btree_trans trans;
957 struct btree_iter iter;
958 struct bch_inode_unpacked prev = { 0 };
959 struct snapshots_seen s;
963 snapshots_seen_init(&s);
964 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
966 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
968 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
969 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
970 check_inode(&trans, &iter, k, &prev, &s, full));
972 bch2_trans_exit(&trans);
973 snapshots_seen_exit(&s);
979 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
980 struct btree_iter *iter,
983 return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
986 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
987 struct bkey_s_c_dirent d)
989 return inode->bi_dir == d.k->p.inode &&
990 inode->bi_dir_offset == d.k->p.offset;
993 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
994 struct bch_inode_unpacked *inode)
996 return d.v->d_type == DT_SUBVOL
997 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
998 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1001 static int inode_backpointer_exists(struct btree_trans *trans,
1002 struct bch_inode_unpacked *inode,
1005 struct btree_iter iter;
1006 struct bkey_s_c_dirent d;
1009 d = dirent_get_by_pos(trans, &iter,
1010 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1013 return bch2_err_matches(ret, ENOENT) ? 0 : ret;
1015 ret = dirent_points_to_inode(d, inode);
1016 bch2_trans_iter_exit(trans, &iter);
1020 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1022 struct bch_fs *c = trans->c;
1023 struct inode_walker_entry *i;
1024 u32 restart_count = trans->restart_count;
1028 darray_for_each(w->inodes, i) {
1029 if (i->inode.bi_sectors == i->count)
1032 count2 = bch2_count_inode_sectors(trans, w->last_pos.inode, i->snapshot);
1034 if (w->recalculate_sums)
1037 if (i->count != count2) {
1038 bch_err(c, "fsck counted i_sectors wrong for inode %llu:%u: got %llu should be %llu",
1039 w->last_pos.inode, i->snapshot, i->count, count2);
1040 return -BCH_ERR_internal_fsck_err;
1043 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1044 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1045 w->last_pos.inode, i->snapshot,
1046 i->inode.bi_sectors, i->count)) {
1047 i->inode.bi_sectors = i->count;
1048 ret = write_inode(trans, &i->inode, i->snapshot);
1056 if (!ret && trans_was_restarted(trans, restart_count))
1057 ret = -BCH_ERR_transaction_restart_nested;
1064 struct snapshots_seen seen;
1067 struct extent_ends {
1068 struct bpos last_pos;
1069 DARRAY(struct extent_end) e;
1072 static void extent_ends_reset(struct extent_ends *extent_ends)
1074 struct extent_end *i;
1076 darray_for_each(extent_ends->e, i)
1077 snapshots_seen_exit(&i->seen);
1079 extent_ends->e.nr = 0;
1082 static void extent_ends_exit(struct extent_ends *extent_ends)
1084 extent_ends_reset(extent_ends);
1085 darray_exit(&extent_ends->e);
1088 static void extent_ends_init(struct extent_ends *extent_ends)
1090 memset(extent_ends, 0, sizeof(*extent_ends));
1093 static int extent_ends_at(struct bch_fs *c,
1094 struct extent_ends *extent_ends,
1095 struct snapshots_seen *seen,
1098 struct extent_end *i, n = (struct extent_end) {
1099 .offset = k.k->p.offset,
1100 .snapshot = k.k->p.snapshot,
1104 n.seen.ids.data = kmemdup(seen->ids.data,
1105 sizeof(seen->ids.data[0]) * seen->ids.size,
1107 if (!n.seen.ids.data)
1108 return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1110 darray_for_each(extent_ends->e, i) {
1111 if (i->snapshot == k.k->p.snapshot) {
1112 snapshots_seen_exit(&i->seen);
1117 if (i->snapshot >= k.k->p.snapshot)
1121 return darray_insert_item(&extent_ends->e, i - extent_ends->e.data, n);
1124 static int overlapping_extents_found(struct btree_trans *trans,
1125 enum btree_id btree,
1126 struct bpos pos1, struct bkey pos2,
1128 struct extent_end *extent_end)
1130 struct bch_fs *c = trans->c;
1131 struct printbuf buf = PRINTBUF;
1132 struct btree_iter iter1, iter2 = { NULL };
1133 struct bkey_s_c k1, k2;
1136 BUG_ON(bkey_le(pos1, bkey_start_pos(&pos2)));
1138 bch2_trans_iter_init(trans, &iter1, btree, pos1,
1139 BTREE_ITER_ALL_SNAPSHOTS|
1140 BTREE_ITER_NOT_EXTENTS);
1141 k1 = bch2_btree_iter_peek_upto(&iter1, POS(pos1.inode, U64_MAX));
1146 prt_str(&buf, "\n ");
1147 bch2_bkey_val_to_text(&buf, c, k1);
1149 if (!bpos_eq(pos1, k1.k->p)) {
1150 prt_str(&buf, "\n wanted\n ");
1151 bch2_bpos_to_text(&buf, pos1);
1152 prt_str(&buf, "\n ");
1153 bch2_bkey_to_text(&buf, &pos2);
1155 bch_err(c, "%s: error finding first overlapping extent when repairing, got%s",
1157 ret = -BCH_ERR_internal_fsck_err;
1161 bch2_trans_copy_iter(&iter2, &iter1);
1164 bch2_btree_iter_advance(&iter2);
1166 k2 = bch2_btree_iter_peek_upto(&iter2, POS(pos1.inode, U64_MAX));
1171 if (bpos_ge(k2.k->p, pos2.p))
1175 prt_str(&buf, "\n ");
1176 bch2_bkey_val_to_text(&buf, c, k2);
1178 if (bpos_gt(k2.k->p, pos2.p) ||
1179 pos2.size != k2.k->size) {
1180 bch_err(c, "%s: error finding seconding overlapping extent when repairing%s",
1182 ret = -BCH_ERR_internal_fsck_err;
1186 prt_printf(&buf, "\n overwriting %s extent",
1187 pos1.snapshot >= pos2.p.snapshot ? "first" : "second");
1189 if (fsck_err(c, "overlapping extents%s", buf.buf)) {
1190 struct btree_iter *old_iter = &iter1;
1191 struct disk_reservation res = { 0 };
1193 if (pos1.snapshot < pos2.p.snapshot) {
1198 trans->extra_journal_res += bch2_bkey_sectors_compressed(k2);
1200 ret = bch2_trans_update_extent_overwrite(trans, old_iter,
1201 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE,
1203 bch2_trans_commit(trans, &res, NULL,
1204 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL);
1205 bch2_disk_reservation_put(c, &res);
1212 if (pos1.snapshot == pos2.p.snapshot)
1213 extent_end->offset = bkey_start_offset(&pos2);
1215 ret = -BCH_ERR_transaction_restart_nested;
1219 bch2_trans_iter_exit(trans, &iter2);
1220 bch2_trans_iter_exit(trans, &iter1);
1221 printbuf_exit(&buf);
1225 static int check_overlapping_extents(struct btree_trans *trans,
1226 struct snapshots_seen *seen,
1227 struct extent_ends *extent_ends,
1230 struct btree_iter *iter,
1233 struct bch_fs *c = trans->c;
1234 struct extent_end *i;
1237 /* transaction restart, running again */
1238 if (bpos_eq(extent_ends->last_pos, k.k->p))
1241 if (extent_ends->last_pos.inode != k.k->p.inode)
1242 extent_ends_reset(extent_ends);
1244 darray_for_each(extent_ends->e, i) {
1245 if (i->offset <= bkey_start_offset(k.k))
1248 if (!ref_visible2(c,
1249 k.k->p.snapshot, seen,
1250 i->snapshot, &i->seen))
1253 ret = overlapping_extents_found(trans, iter->btree_id,
1254 SPOS(iter->pos.inode,
1262 ret = extent_ends_at(c, extent_ends, seen, k);
1266 extent_ends->last_pos = k.k->p;
1271 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1273 struct inode_walker *inode,
1274 struct snapshots_seen *s,
1275 struct extent_ends *extent_ends)
1277 struct bch_fs *c = trans->c;
1278 struct inode_walker_entry *i;
1279 struct printbuf buf = PRINTBUF;
1280 struct bpos equiv = k.k->p;
1283 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1285 ret = check_key_has_snapshot(trans, iter, k);
1287 ret = ret < 0 ? ret : 0;
1291 if (inode->last_pos.inode != k.k->p.inode) {
1292 ret = check_i_sectors(trans, inode);
1297 i = walk_inode(trans, inode, equiv, k.k->type == KEY_TYPE_whiteout);
1298 ret = PTR_ERR_OR_ZERO(i);
1302 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1306 if (k.k->type != KEY_TYPE_whiteout) {
1307 if (fsck_err_on(!i, c,
1308 "extent in missing inode:\n %s",
1309 (printbuf_reset(&buf),
1310 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1313 if (fsck_err_on(i &&
1314 !S_ISREG(i->inode.bi_mode) &&
1315 !S_ISLNK(i->inode.bi_mode), c,
1316 "extent in non regular inode mode %o:\n %s",
1318 (printbuf_reset(&buf),
1319 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1322 ret = check_overlapping_extents(trans, s, extent_ends, k,
1323 equiv.snapshot, iter,
1324 &inode->recalculate_sums);
1330 * Check inodes in reverse order, from oldest snapshots to newest,
1331 * starting from the inode that matches this extent's snapshot. If we
1332 * didn't have one, iterate over all inodes:
1335 i = inode->inodes.data + inode->inodes.nr - 1;
1338 inode->inodes.data && i >= inode->inodes.data;
1340 if (i->snapshot > equiv.snapshot ||
1341 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1344 if (k.k->type != KEY_TYPE_whiteout) {
1345 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1346 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1347 !bkey_extent_is_reservation(k), c,
1348 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1349 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1350 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1351 struct btree_iter iter2;
1353 bch2_trans_copy_iter(&iter2, iter);
1354 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1355 ret = bch2_btree_iter_traverse(&iter2) ?:
1356 bch2_btree_delete_at(trans, &iter2,
1357 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1358 bch2_trans_iter_exit(trans, &iter2);
1362 iter->k.type = KEY_TYPE_whiteout;
1365 if (bkey_extent_is_allocation(k.k))
1366 i->count += k.k->size;
1369 i->seen_this_pos = true;
1374 printbuf_exit(&buf);
1376 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1380 ret = bch2_btree_delete_at(trans, iter, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1385 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1386 * that i_size an i_sectors are consistent
1388 int bch2_check_extents(struct bch_fs *c)
1390 struct inode_walker w = inode_walker_init();
1391 struct snapshots_seen s;
1392 struct btree_trans trans;
1393 struct btree_iter iter;
1395 struct extent_ends extent_ends;
1396 struct disk_reservation res = { 0 };
1399 snapshots_seen_init(&s);
1400 extent_ends_init(&extent_ends);
1401 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 4096);
1403 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_extents,
1404 POS(BCACHEFS_ROOT_INO, 0),
1405 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1407 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL, ({
1408 bch2_disk_reservation_put(c, &res);
1409 check_extent(&trans, &iter, k, &w, &s, &extent_ends);
1411 check_i_sectors(&trans, &w);
1413 bch2_disk_reservation_put(c, &res);
1414 extent_ends_exit(&extent_ends);
1415 inode_walker_exit(&w);
1416 bch2_trans_exit(&trans);
1417 snapshots_seen_exit(&s);
1424 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1426 struct bch_fs *c = trans->c;
1427 struct inode_walker_entry *i;
1428 u32 restart_count = trans->restart_count;
1432 darray_for_each(w->inodes, i) {
1433 if (i->inode.bi_nlink == i->count)
1436 count2 = bch2_count_subdirs(trans, w->last_pos.inode, i->snapshot);
1440 if (i->count != count2) {
1441 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1444 if (i->inode.bi_nlink == i->count)
1448 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1449 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1450 w->last_pos.inode, i->snapshot, i->inode.bi_nlink, i->count)) {
1451 i->inode.bi_nlink = i->count;
1452 ret = write_inode(trans, &i->inode, i->snapshot);
1460 if (!ret && trans_was_restarted(trans, restart_count))
1461 ret = -BCH_ERR_transaction_restart_nested;
1465 static int check_dirent_target(struct btree_trans *trans,
1466 struct btree_iter *iter,
1467 struct bkey_s_c_dirent d,
1468 struct bch_inode_unpacked *target,
1469 u32 target_snapshot)
1471 struct bch_fs *c = trans->c;
1472 struct bkey_i_dirent *n;
1473 bool backpointer_exists = true;
1474 struct printbuf buf = PRINTBUF;
1477 if (!target->bi_dir &&
1478 !target->bi_dir_offset) {
1479 target->bi_dir = d.k->p.inode;
1480 target->bi_dir_offset = d.k->p.offset;
1482 ret = __write_inode(trans, target, target_snapshot);
1487 if (!inode_points_to_dirent(target, d)) {
1488 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1492 backpointer_exists = ret;
1495 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1496 backpointer_exists, c,
1497 "directory %llu with multiple links",
1499 ret = __remove_dirent(trans, d.k->p);
1503 if (fsck_err_on(backpointer_exists &&
1504 !target->bi_nlink, c,
1505 "inode %llu type %s has multiple links but i_nlink 0",
1506 target->bi_inum, bch2_d_types[d.v->d_type])) {
1508 target->bi_flags &= ~BCH_INODE_UNLINKED;
1510 ret = __write_inode(trans, target, target_snapshot);
1515 if (fsck_err_on(!backpointer_exists, c,
1516 "inode %llu:%u has wrong backpointer:\n"
1518 "should be %llu:%llu",
1519 target->bi_inum, target_snapshot,
1521 target->bi_dir_offset,
1524 target->bi_dir = d.k->p.inode;
1525 target->bi_dir_offset = d.k->p.offset;
1527 ret = __write_inode(trans, target, target_snapshot);
1533 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1534 "incorrect d_type: got %s, should be %s:\n%s",
1535 bch2_d_type_str(d.v->d_type),
1536 bch2_d_type_str(inode_d_type(target)),
1537 (printbuf_reset(&buf),
1538 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1539 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1540 ret = PTR_ERR_OR_ZERO(n);
1544 bkey_reassemble(&n->k_i, d.s_c);
1545 n->v.d_type = inode_d_type(target);
1547 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1551 d = dirent_i_to_s_c(n);
1554 if (d.v->d_type == DT_SUBVOL &&
1555 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1556 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1557 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1558 le32_to_cpu(d.v->d_parent_subvol),
1559 target->bi_parent_subvol))) {
1560 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1561 ret = PTR_ERR_OR_ZERO(n);
1565 bkey_reassemble(&n->k_i, d.s_c);
1566 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1568 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1572 d = dirent_i_to_s_c(n);
1577 printbuf_exit(&buf);
1579 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1584 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1586 struct bch_hash_info *hash_info,
1587 struct inode_walker *dir,
1588 struct inode_walker *target,
1589 struct snapshots_seen *s)
1591 struct bch_fs *c = trans->c;
1592 struct bkey_s_c_dirent d;
1593 struct inode_walker_entry *i;
1594 struct printbuf buf = PRINTBUF;
1598 ret = check_key_has_snapshot(trans, iter, k);
1600 ret = ret < 0 ? ret : 0;
1605 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1607 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1611 if (k.k->type == KEY_TYPE_whiteout)
1614 if (dir->last_pos.inode != k.k->p.inode) {
1615 ret = check_subdir_count(trans, dir);
1620 BUG_ON(!iter->path->should_be_locked);
1622 i = walk_inode(trans, dir, equiv, k.k->type == KEY_TYPE_whiteout);
1623 ret = PTR_ERR_OR_ZERO(i);
1627 if (dir->first_this_inode && dir->inodes.nr)
1628 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1629 dir->first_this_inode = false;
1631 if (fsck_err_on(!i, c,
1632 "dirent in nonexisting directory:\n%s",
1633 (printbuf_reset(&buf),
1634 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1635 ret = bch2_btree_delete_at(trans, iter,
1636 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1643 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1644 "dirent in non directory inode type %s:\n%s",
1645 bch2_d_type_str(inode_d_type(&i->inode)),
1646 (printbuf_reset(&buf),
1647 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1648 ret = bch2_btree_delete_at(trans, iter, 0);
1652 ret = hash_check_key(trans, bch2_dirent_hash_desc, hash_info, iter, k);
1656 /* dirent has been deleted */
1661 if (k.k->type != KEY_TYPE_dirent)
1664 d = bkey_s_c_to_dirent(k);
1666 if (d.v->d_type == DT_SUBVOL) {
1667 struct bch_inode_unpacked subvol_root;
1668 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1669 u32 target_snapshot;
1672 ret = __subvol_lookup(trans, target_subvol,
1673 &target_snapshot, &target_inum);
1674 if (ret && !bch2_err_matches(ret, ENOENT))
1677 if (fsck_err_on(ret, c,
1678 "dirent points to missing subvolume %u",
1679 le32_to_cpu(d.v->d_child_subvol))) {
1680 ret = __remove_dirent(trans, d.k->p);
1684 ret = __lookup_inode(trans, target_inum,
1685 &subvol_root, &target_snapshot);
1686 if (ret && !bch2_err_matches(ret, ENOENT))
1689 if (fsck_err_on(ret, c,
1690 "subvolume %u points to missing subvolume root %llu",
1693 bch_err(c, "repair not implemented yet");
1698 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1699 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1701 subvol_root.bi_subvol, target_subvol)) {
1702 subvol_root.bi_subvol = target_subvol;
1703 ret = __write_inode(trans, &subvol_root, target_snapshot);
1708 ret = check_dirent_target(trans, iter, d, &subvol_root,
1713 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1717 if (fsck_err_on(!target->inodes.nr, c,
1718 "dirent points to missing inode: (equiv %u)\n%s",
1720 (printbuf_reset(&buf),
1721 bch2_bkey_val_to_text(&buf, c, k),
1723 ret = __remove_dirent(trans, d.k->p);
1728 darray_for_each(target->inodes, i) {
1729 ret = check_dirent_target(trans, iter, d,
1730 &i->inode, i->snapshot);
1736 if (d.v->d_type == DT_DIR)
1737 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1743 printbuf_exit(&buf);
1745 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1751 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1754 int bch2_check_dirents(struct bch_fs *c)
1756 struct inode_walker dir = inode_walker_init();
1757 struct inode_walker target = inode_walker_init();
1758 struct snapshots_seen s;
1759 struct bch_hash_info hash_info;
1760 struct btree_trans trans;
1761 struct btree_iter iter;
1765 snapshots_seen_init(&s);
1766 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1768 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_dirents,
1769 POS(BCACHEFS_ROOT_INO, 0),
1770 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1773 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1774 check_dirent(&trans, &iter, k, &hash_info, &dir, &target, &s));
1776 bch2_trans_exit(&trans);
1777 snapshots_seen_exit(&s);
1778 inode_walker_exit(&dir);
1779 inode_walker_exit(&target);
1786 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1788 struct bch_hash_info *hash_info,
1789 struct inode_walker *inode)
1791 struct bch_fs *c = trans->c;
1792 struct inode_walker_entry *i;
1795 ret = check_key_has_snapshot(trans, iter, k);
1799 i = walk_inode(trans, inode, k.k->p, k.k->type == KEY_TYPE_whiteout);
1800 ret = PTR_ERR_OR_ZERO(i);
1804 if (inode->first_this_inode && inode->inodes.nr)
1805 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1806 inode->first_this_inode = false;
1808 if (fsck_err_on(!i, c,
1809 "xattr for missing inode %llu",
1811 return bch2_btree_delete_at(trans, iter, 0);
1816 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1818 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1824 * Walk xattrs: verify that they all have a corresponding inode
1826 int bch2_check_xattrs(struct bch_fs *c)
1828 struct inode_walker inode = inode_walker_init();
1829 struct bch_hash_info hash_info;
1830 struct btree_trans trans;
1831 struct btree_iter iter;
1835 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1837 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_xattrs,
1838 POS(BCACHEFS_ROOT_INO, 0),
1839 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1842 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1843 check_xattr(&trans, &iter, k, &hash_info, &inode));
1845 bch2_trans_exit(&trans);
1852 static int check_root_trans(struct btree_trans *trans)
1854 struct bch_fs *c = trans->c;
1855 struct bch_inode_unpacked root_inode;
1860 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1861 if (ret && !bch2_err_matches(ret, ENOENT))
1864 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1865 struct bkey_i_subvolume root_subvol;
1868 inum = BCACHEFS_ROOT_INO;
1870 bkey_subvolume_init(&root_subvol.k_i);
1871 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1872 root_subvol.v.flags = 0;
1873 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1874 root_subvol.v.inode = cpu_to_le64(inum);
1875 ret = commit_do(trans, NULL, NULL,
1876 BTREE_INSERT_NOFAIL|
1877 BTREE_INSERT_LAZY_RW,
1878 __bch2_btree_insert(trans, BTREE_ID_subvolumes,
1879 &root_subvol.k_i, 0));
1881 bch_err(c, "error writing root subvol: %s", bch2_err_str(ret));
1887 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1888 if (ret && !bch2_err_matches(ret, ENOENT))
1891 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1892 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1893 "root inode not a directory")) {
1894 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1896 root_inode.bi_inum = inum;
1898 ret = __write_inode(trans, &root_inode, snapshot);
1900 bch_err(c, "error writing root inode: %s", bch2_err_str(ret));
1907 /* Get root directory, create if it doesn't exist: */
1908 int bch2_check_root(struct bch_fs *c)
1912 ret = bch2_trans_do(c, NULL, NULL,
1913 BTREE_INSERT_NOFAIL|
1914 BTREE_INSERT_LAZY_RW,
1915 check_root_trans(&trans));
1922 struct pathbuf_entry {
1927 typedef DARRAY(struct pathbuf_entry) pathbuf;
1929 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1931 struct pathbuf_entry *i;
1933 darray_for_each(*p, i)
1934 if (i->inum == inum &&
1935 i->snapshot == snapshot)
1941 static int path_down(struct bch_fs *c, pathbuf *p,
1942 u64 inum, u32 snapshot)
1944 int ret = darray_push(p, ((struct pathbuf_entry) {
1946 .snapshot = snapshot,
1950 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
1956 * Check that a given inode is reachable from the root:
1958 * XXX: we should also be verifying that inodes are in the right subvolumes
1960 static int check_path(struct btree_trans *trans,
1962 struct bch_inode_unpacked *inode,
1965 struct bch_fs *c = trans->c;
1968 snapshot = bch2_snapshot_equiv(c, snapshot);
1971 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
1972 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
1973 struct btree_iter dirent_iter;
1974 struct bkey_s_c_dirent d;
1975 u32 parent_snapshot = snapshot;
1977 if (inode->bi_subvol) {
1980 ret = subvol_lookup(trans, inode->bi_parent_subvol,
1981 &parent_snapshot, &inum);
1986 ret = lockrestart_do(trans,
1987 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
1988 SPOS(inode->bi_dir, inode->bi_dir_offset,
1989 parent_snapshot))).k));
1990 if (ret && !bch2_err_matches(ret, ENOENT))
1993 if (!ret && !dirent_points_to_inode(d, inode)) {
1994 bch2_trans_iter_exit(trans, &dirent_iter);
1995 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
1998 if (bch2_err_matches(ret, ENOENT)) {
1999 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
2000 inode->bi_inum, snapshot,
2001 bch2_d_type_str(inode_d_type(inode)),
2004 inode->bi_dir_offset))
2005 ret = reattach_inode(trans, inode, snapshot);
2009 bch2_trans_iter_exit(trans, &dirent_iter);
2011 if (!S_ISDIR(inode->bi_mode))
2014 ret = path_down(c, p, inode->bi_inum, snapshot);
2016 bch_err(c, "memory allocation failure");
2020 snapshot = parent_snapshot;
2022 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
2024 /* Should have been caught in dirents pass */
2025 bch_err(c, "error looking up parent directory: %i", ret);
2029 if (path_is_dup(p, inode->bi_inum, snapshot)) {
2030 struct pathbuf_entry *i;
2032 /* XXX print path */
2033 bch_err(c, "directory structure loop");
2035 darray_for_each(*p, i)
2036 pr_err("%llu:%u", i->inum, i->snapshot);
2037 pr_err("%llu:%u", inode->bi_inum, snapshot);
2039 if (!fsck_err(c, "directory structure loop"))
2042 ret = commit_do(trans, NULL, NULL,
2043 BTREE_INSERT_NOFAIL|
2044 BTREE_INSERT_LAZY_RW,
2045 remove_backpointer(trans, inode));
2047 bch_err(c, "error removing dirent: %i", ret);
2051 ret = reattach_inode(trans, inode, snapshot);
2061 * Check for unreachable inodes, as well as loops in the directory structure:
2062 * After bch2_check_dirents(), if an inode backpointer doesn't exist that means it's
2065 int bch2_check_directory_structure(struct bch_fs *c)
2067 struct btree_trans trans;
2068 struct btree_iter iter;
2070 struct bch_inode_unpacked u;
2071 pathbuf path = { 0, };
2074 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2076 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
2078 BTREE_ITER_PREFETCH|
2079 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2080 if (!bkey_is_inode(k.k))
2083 ret = bch2_inode_unpack(k, &u);
2085 /* Should have been caught earlier in fsck: */
2086 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2090 if (u.bi_flags & BCH_INODE_UNLINKED)
2093 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
2097 bch2_trans_iter_exit(&trans, &iter);
2098 bch2_trans_exit(&trans);
2106 /* check_nlink pass: */
2108 struct nlink_table {
2119 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2120 u64 inum, u32 snapshot)
2122 if (t->nr == t->size) {
2123 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2124 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2127 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2129 return -BCH_ERR_ENOMEM_fsck_add_nlink;
2133 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2141 t->d[t->nr++] = (struct nlink) {
2143 .snapshot = snapshot,
2149 static int nlink_cmp(const void *_l, const void *_r)
2151 const struct nlink *l = _l;
2152 const struct nlink *r = _r;
2154 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2157 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2158 struct nlink_table *links,
2159 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2161 struct nlink *link, key = {
2162 .inum = inum, .snapshot = U32_MAX,
2165 if (inum < range_start || inum >= range_end)
2168 link = __inline_bsearch(&key, links->d, links->nr,
2169 sizeof(links->d[0]), nlink_cmp);
2173 while (link > links->d && link[0].inum == link[-1].inum)
2176 for (; link < links->d + links->nr && link->inum == inum; link++)
2177 if (ref_visible(c, s, snapshot, link->snapshot)) {
2179 if (link->snapshot >= snapshot)
2185 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2186 struct nlink_table *t,
2187 u64 start, u64 *end)
2189 struct btree_trans trans;
2190 struct btree_iter iter;
2192 struct bch_inode_unpacked u;
2195 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2197 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2200 BTREE_ITER_PREFETCH|
2201 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2202 if (!bkey_is_inode(k.k))
2205 /* Should never fail, checked by bch2_inode_invalid: */
2206 BUG_ON(bch2_inode_unpack(k, &u));
2209 * Backpointer and directory structure checks are sufficient for
2210 * directories, since they can't have hardlinks:
2212 if (S_ISDIR(u.bi_mode))
2218 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2220 *end = k.k->p.offset;
2226 bch2_trans_iter_exit(&trans, &iter);
2227 bch2_trans_exit(&trans);
2230 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2236 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2237 u64 range_start, u64 range_end)
2239 struct btree_trans trans;
2240 struct snapshots_seen s;
2241 struct btree_iter iter;
2243 struct bkey_s_c_dirent d;
2246 snapshots_seen_init(&s);
2248 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2250 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2252 BTREE_ITER_PREFETCH|
2253 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2254 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2258 switch (k.k->type) {
2259 case KEY_TYPE_dirent:
2260 d = bkey_s_c_to_dirent(k);
2262 if (d.v->d_type != DT_DIR &&
2263 d.v->d_type != DT_SUBVOL)
2264 inc_link(c, &s, links, range_start, range_end,
2265 le64_to_cpu(d.v->d_inum),
2266 bch2_snapshot_equiv(c, d.k->p.snapshot));
2270 bch2_trans_iter_exit(&trans, &iter);
2273 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2275 bch2_trans_exit(&trans);
2276 snapshots_seen_exit(&s);
2280 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2282 struct nlink_table *links,
2283 size_t *idx, u64 range_end)
2285 struct bch_fs *c = trans->c;
2286 struct bch_inode_unpacked u;
2287 struct nlink *link = &links->d[*idx];
2290 if (k.k->p.offset >= range_end)
2293 if (!bkey_is_inode(k.k))
2296 BUG_ON(bch2_inode_unpack(k, &u));
2298 if (S_ISDIR(u.bi_mode))
2304 while ((cmp_int(link->inum, k.k->p.offset) ?:
2305 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2306 BUG_ON(*idx == links->nr);
2307 link = &links->d[++*idx];
2310 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2311 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2312 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2313 bch2_inode_nlink_get(&u), link->count)) {
2314 bch2_inode_nlink_set(&u, link->count);
2315 ret = __write_inode(trans, &u, k.k->p.snapshot);
2322 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2323 struct nlink_table *links,
2324 u64 range_start, u64 range_end)
2326 struct btree_trans trans;
2327 struct btree_iter iter;
2332 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2334 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
2335 POS(0, range_start),
2336 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2337 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
2338 check_nlinks_update_inode(&trans, &iter, k, links, &idx, range_end));
2340 bch2_trans_exit(&trans);
2343 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2350 int bch2_check_nlinks(struct bch_fs *c)
2352 struct nlink_table links = { 0 };
2353 u64 this_iter_range_start, next_iter_range_start = 0;
2357 this_iter_range_start = next_iter_range_start;
2358 next_iter_range_start = U64_MAX;
2360 ret = check_nlinks_find_hardlinks(c, &links,
2361 this_iter_range_start,
2362 &next_iter_range_start);
2364 ret = check_nlinks_walk_dirents(c, &links,
2365 this_iter_range_start,
2366 next_iter_range_start);
2370 ret = check_nlinks_update_hardlinks(c, &links,
2371 this_iter_range_start,
2372 next_iter_range_start);
2377 } while (next_iter_range_start != U64_MAX);
2386 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2389 struct bkey_s_c_reflink_p p;
2390 struct bkey_i_reflink_p *u;
2393 if (k.k->type != KEY_TYPE_reflink_p)
2396 p = bkey_s_c_to_reflink_p(k);
2398 if (!p.v->front_pad && !p.v->back_pad)
2401 u = bch2_trans_kmalloc(trans, sizeof(*u));
2402 ret = PTR_ERR_OR_ZERO(u);
2406 bkey_reassemble(&u->k_i, k);
2410 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2413 int bch2_fix_reflink_p(struct bch_fs *c)
2415 struct btree_iter iter;
2419 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2422 ret = bch2_trans_run(c,
2423 for_each_btree_key_commit(&trans, iter,
2424 BTREE_ID_extents, POS_MIN,
2425 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|
2426 BTREE_ITER_ALL_SNAPSHOTS, k,
2427 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
2428 fix_reflink_p_key(&trans, &iter, k)));