1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_update.h"
10 #include "fs-common.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_add_inorder(struct bch_fs *c, struct snapshots_seen *s, u32 id)
414 struct snapshots_seen_entry *i, n = {
416 .equiv = bch2_snapshot_equiv(c, id),
420 darray_for_each(s->ids, i) {
427 ret = darray_insert_item(&s->ids, i - s->ids.data, n);
429 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
434 static int snapshots_seen_update(struct bch_fs *c, struct snapshots_seen *s,
435 enum btree_id btree_id, struct bpos pos)
437 struct snapshots_seen_entry *i, n = {
439 .equiv = bch2_snapshot_equiv(c, pos.snapshot),
443 if (!bkey_eq(s->pos, pos))
447 s->pos.snapshot = n.equiv;
449 darray_for_each(s->ids, i) {
454 * We currently don't rigorously track for snapshot cleanup
455 * needing to be run, so it shouldn't be a fsck error yet:
457 if (i->equiv == n.equiv) {
458 bch_err(c, "snapshot deletion did not finish:\n"
459 " duplicate keys in btree %s at %llu:%llu snapshots %u, %u (equiv %u)\n",
460 bch2_btree_ids[btree_id],
461 pos.inode, pos.offset,
462 i->id, n.id, n.equiv);
463 return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_delete_dead_snapshots);
467 ret = darray_push(&s->ids, n);
469 bch_err(c, "error reallocating snapshots_seen table (size %zu)",
475 * key_visible_in_snapshot - returns true if @id is a descendent of @ancestor,
476 * and @ancestor hasn't been overwritten in @seen
478 * That is, returns whether key in @ancestor snapshot is visible in @id snapshot
480 static bool key_visible_in_snapshot(struct bch_fs *c, struct snapshots_seen *seen,
481 u32 id, u32 ancestor)
485 EBUG_ON(id > ancestor);
486 EBUG_ON(!bch2_snapshot_is_equiv(c, id));
487 EBUG_ON(!bch2_snapshot_is_equiv(c, ancestor));
489 /* @ancestor should be the snapshot most recently added to @seen */
490 EBUG_ON(ancestor != seen->pos.snapshot);
491 EBUG_ON(ancestor != seen->ids.data[seen->ids.nr - 1].equiv);
496 if (!bch2_snapshot_is_ancestor(c, id, ancestor))
500 * We know that @id is a descendant of @ancestor, we're checking if
501 * we've seen a key that overwrote @ancestor - i.e. also a descendent of
502 * @ascestor and with @id as a descendent.
504 * But we already know that we're scanning IDs between @id and @ancestor
505 * numerically, since snapshot ID lists are kept sorted, so if we find
506 * an id that's an ancestor of @id we're done:
509 for (i = seen->ids.nr - 2;
510 i >= 0 && seen->ids.data[i].equiv >= id;
512 if (bch2_snapshot_is_ancestor(c, id, seen->ids.data[i].equiv))
519 * ref_visible - given a key with snapshot id @src that points to a key with
520 * snapshot id @dst, test whether there is some snapshot in which @dst is
523 * This assumes we're visiting @src keys in natural key order.
525 * @s - list of snapshot IDs already seen at @src
526 * @src - snapshot ID of src key
527 * @dst - snapshot ID of dst key
529 static int ref_visible(struct bch_fs *c, struct snapshots_seen *s,
533 ? key_visible_in_snapshot(c, s, dst, src)
534 : bch2_snapshot_is_ancestor(c, src, dst);
537 static int ref_visible2(struct bch_fs *c,
538 u32 src, struct snapshots_seen *src_seen,
539 u32 dst, struct snapshots_seen *dst_seen)
541 src = bch2_snapshot_equiv(c, src);
542 dst = bch2_snapshot_equiv(c, dst);
546 swap(dst_seen, src_seen);
548 return key_visible_in_snapshot(c, src_seen, dst, src);
551 #define for_each_visible_inode(_c, _s, _w, _snapshot, _i) \
552 for (_i = (_w)->inodes.data; _i < (_w)->inodes.data + (_w)->inodes.nr && \
553 (_i)->snapshot <= (_snapshot); _i++) \
554 if (key_visible_in_snapshot(_c, _s, _i->snapshot, _snapshot))
556 struct inode_walker_entry {
557 struct bch_inode_unpacked inode;
563 struct inode_walker {
564 bool first_this_inode;
565 bool recalculate_sums;
566 struct bpos last_pos;
568 DARRAY(struct inode_walker_entry) inodes;
571 static void inode_walker_exit(struct inode_walker *w)
573 darray_exit(&w->inodes);
576 static struct inode_walker inode_walker_init(void)
578 return (struct inode_walker) { 0, };
581 static int add_inode(struct bch_fs *c, struct inode_walker *w,
582 struct bkey_s_c inode)
584 struct bch_inode_unpacked u;
586 BUG_ON(bch2_inode_unpack(inode, &u));
588 return darray_push(&w->inodes, ((struct inode_walker_entry) {
590 .snapshot = bch2_snapshot_equiv(c, inode.k->p.snapshot),
594 static int get_inodes_all_snapshots(struct btree_trans *trans,
595 struct inode_walker *w, u64 inum)
597 struct bch_fs *c = trans->c;
598 struct btree_iter iter;
600 u32 restart_count = trans->restart_count;
603 w->recalculate_sums = false;
606 for_each_btree_key(trans, iter, BTREE_ID_inodes, POS(0, inum),
607 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
608 if (k.k->p.offset != inum)
611 if (bkey_is_inode(k.k))
614 bch2_trans_iter_exit(trans, &iter);
619 w->first_this_inode = true;
621 if (trans_was_restarted(trans, restart_count))
622 return -BCH_ERR_transaction_restart_nested;
627 static struct inode_walker_entry *
628 lookup_inode_for_snapshot(struct bch_fs *c, struct inode_walker *w,
629 u32 snapshot, bool is_whiteout)
631 struct inode_walker_entry *i;
633 snapshot = bch2_snapshot_equiv(c, snapshot);
635 darray_for_each(w->inodes, i)
636 if (bch2_snapshot_is_ancestor(c, snapshot, i->snapshot))
641 BUG_ON(snapshot > i->snapshot);
643 if (snapshot != i->snapshot && !is_whiteout) {
644 struct inode_walker_entry new = *i;
648 new.snapshot = snapshot;
651 bch_info(c, "have key for inode %llu:%u but have inode in ancestor snapshot %u",
652 w->last_pos.inode, snapshot, i->snapshot);
654 while (i > w->inodes.data && i[-1].snapshot > snapshot)
657 pos = i - w->inodes.data;
658 ret = darray_insert_item(&w->inodes, pos, new);
662 i = w->inodes.data + pos;
668 static struct inode_walker_entry *walk_inode(struct btree_trans *trans,
669 struct inode_walker *w, struct bpos pos,
672 if (w->last_pos.inode != pos.inode) {
673 int ret = get_inodes_all_snapshots(trans, w, pos.inode);
676 } else if (bkey_cmp(w->last_pos, pos)) {
677 struct inode_walker_entry *i;
679 darray_for_each(w->inodes, i)
680 i->seen_this_pos = false;
686 return lookup_inode_for_snapshot(trans->c, w, pos.snapshot, is_whiteout);
689 static int __get_visible_inodes(struct btree_trans *trans,
690 struct inode_walker *w,
691 struct snapshots_seen *s,
694 struct bch_fs *c = trans->c;
695 struct btree_iter iter;
701 for_each_btree_key_norestart(trans, iter, BTREE_ID_inodes, POS(0, inum),
702 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
703 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
705 if (k.k->p.offset != inum)
708 if (!ref_visible(c, s, s->pos.snapshot, equiv))
711 if (bkey_is_inode(k.k))
714 if (equiv >= s->pos.snapshot)
717 bch2_trans_iter_exit(trans, &iter);
722 static int check_key_has_snapshot(struct btree_trans *trans,
723 struct btree_iter *iter,
726 struct bch_fs *c = trans->c;
727 struct printbuf buf = PRINTBUF;
730 if (mustfix_fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot), c,
731 "key in missing snapshot: %s",
732 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
733 ret = bch2_btree_delete_at(trans, iter,
734 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: 1;
740 static int hash_redo_key(struct btree_trans *trans,
741 const struct bch_hash_desc desc,
742 struct bch_hash_info *hash_info,
743 struct btree_iter *k_iter, struct bkey_s_c k)
745 struct bkey_i *delete;
748 delete = bch2_trans_kmalloc(trans, sizeof(*delete));
750 return PTR_ERR(delete);
752 tmp = bch2_bkey_make_mut_noupdate(trans, k);
756 bkey_init(&delete->k);
757 delete->k.p = k_iter->pos;
758 return bch2_btree_iter_traverse(k_iter) ?:
759 bch2_trans_update(trans, k_iter, delete, 0) ?:
760 bch2_hash_set_snapshot(trans, desc, hash_info,
761 (subvol_inum) { 0, k.k->p.inode },
762 k.k->p.snapshot, tmp,
763 BCH_HASH_SET_MUST_CREATE,
764 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
765 bch2_trans_commit(trans, NULL, NULL,
767 BTREE_INSERT_LAZY_RW);
770 static int hash_check_key(struct btree_trans *trans,
771 const struct bch_hash_desc desc,
772 struct bch_hash_info *hash_info,
773 struct btree_iter *k_iter, struct bkey_s_c hash_k)
775 struct bch_fs *c = trans->c;
776 struct btree_iter iter = { NULL };
777 struct printbuf buf = PRINTBUF;
782 if (hash_k.k->type != desc.key_type)
785 hash = desc.hash_bkey(hash_info, hash_k);
787 if (likely(hash == hash_k.k->p.offset))
790 if (hash_k.k->p.offset < hash)
793 for_each_btree_key_norestart(trans, iter, desc.btree_id,
794 SPOS(hash_k.k->p.inode, hash, hash_k.k->p.snapshot),
795 BTREE_ITER_SLOTS, k, ret) {
796 if (bkey_eq(k.k->p, hash_k.k->p))
799 if (fsck_err_on(k.k->type == desc.key_type &&
800 !desc.cmp_bkey(k, hash_k), c,
801 "duplicate hash table keys:\n%s",
802 (printbuf_reset(&buf),
803 bch2_bkey_val_to_text(&buf, c, hash_k),
805 ret = bch2_hash_delete_at(trans, desc, hash_info, k_iter, 0) ?: 1;
809 if (bkey_deleted(k.k)) {
810 bch2_trans_iter_exit(trans, &iter);
815 bch2_trans_iter_exit(trans, &iter);
819 if (fsck_err(c, "hash table key at wrong offset: btree %s inode %llu offset %llu, hashed to %llu\n%s",
820 bch2_btree_ids[desc.btree_id], hash_k.k->p.inode, hash_k.k->p.offset, hash,
821 (printbuf_reset(&buf),
822 bch2_bkey_val_to_text(&buf, c, hash_k), buf.buf))) {
823 ret = hash_redo_key(trans, desc, hash_info, k_iter, hash_k);
824 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
825 bch_err(c, "hash_redo_key err %s", bch2_err_str(ret));
828 ret = -BCH_ERR_transaction_restart_nested;
834 static int check_inode(struct btree_trans *trans,
835 struct btree_iter *iter,
837 struct bch_inode_unpacked *prev,
838 struct snapshots_seen *s,
841 struct bch_fs *c = trans->c;
842 struct bch_inode_unpacked u;
843 bool do_update = false;
846 ret = check_key_has_snapshot(trans, iter, k);
852 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
856 if (!bkey_is_inode(k.k))
859 BUG_ON(bch2_inode_unpack(k, &u));
862 !(u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|
863 BCH_INODE_I_SECTORS_DIRTY|
864 BCH_INODE_UNLINKED)))
867 if (prev->bi_inum != u.bi_inum)
870 if (fsck_err_on(prev->bi_hash_seed != u.bi_hash_seed ||
871 inode_d_type(prev) != inode_d_type(&u), c,
872 "inodes in different snapshots don't match")) {
873 bch_err(c, "repair not implemented yet");
877 if ((u.bi_flags & (BCH_INODE_I_SIZE_DIRTY|BCH_INODE_UNLINKED)) &&
878 bch2_key_has_snapshot_overwrites(trans, BTREE_ID_inodes, k.k->p)) {
879 struct bpos new_min_pos;
881 ret = bch2_propagate_key_to_snapshot_leaves(trans, iter->btree_id, k, &new_min_pos);
885 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY|BCH_INODE_UNLINKED;
887 ret = __write_inode(trans, &u, iter->pos.snapshot);
889 bch_err_msg(c, ret, "in fsck: error updating inode");
893 if (!bpos_eq(new_min_pos, POS_MIN))
894 bch2_btree_iter_set_pos(iter, bpos_predecessor(new_min_pos));
898 if (u.bi_flags & BCH_INODE_UNLINKED &&
900 fsck_err(c, "filesystem marked clean, but inode %llu unlinked",
902 bch2_trans_unlock(trans);
905 ret = bch2_inode_rm_snapshot(trans, u.bi_inum, iter->pos.snapshot);
906 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
907 bch_err(c, "error in fsck: error while deleting inode: %s",
912 if (u.bi_flags & BCH_INODE_I_SIZE_DIRTY &&
914 fsck_err(c, "filesystem marked clean, but inode %llu has i_size dirty",
916 bch_verbose(c, "truncating inode %llu", u.bi_inum);
918 bch2_trans_unlock(trans);
922 * XXX: need to truncate partial blocks too here - or ideally
923 * just switch units to bytes and that issue goes away
925 ret = bch2_btree_delete_range_trans(trans, BTREE_ID_extents,
926 SPOS(u.bi_inum, round_up(u.bi_size, block_bytes(c)) >> 9,
928 POS(u.bi_inum, U64_MAX),
930 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
931 bch_err(c, "error in fsck: error truncating inode: %s",
937 * We truncated without our normal sector accounting hook, just
938 * make sure we recalculate it:
940 u.bi_flags |= BCH_INODE_I_SECTORS_DIRTY;
942 u.bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
946 if (u.bi_flags & BCH_INODE_I_SECTORS_DIRTY &&
948 fsck_err(c, "filesystem marked clean, but inode %llu has i_sectors dirty",
952 bch_verbose(c, "recounting sectors for inode %llu",
955 sectors = bch2_count_inode_sectors(trans, u.bi_inum, iter->pos.snapshot);
957 bch_err(c, "error in fsck: error recounting inode sectors: %s",
958 bch2_err_str(sectors));
962 u.bi_sectors = sectors;
963 u.bi_flags &= ~BCH_INODE_I_SECTORS_DIRTY;
967 if (u.bi_flags & BCH_INODE_BACKPTR_UNTRUSTED) {
970 u.bi_flags &= ~BCH_INODE_BACKPTR_UNTRUSTED;
975 ret = __write_inode(trans, &u, iter->pos.snapshot);
977 bch_err_msg(c, ret, "in fsck: error updating inode");
989 int bch2_check_inodes(struct bch_fs *c)
991 bool full = c->opts.fsck;
992 struct btree_trans trans;
993 struct btree_iter iter;
994 struct bch_inode_unpacked prev = { 0 };
995 struct snapshots_seen s;
999 snapshots_seen_init(&s);
1000 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1002 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
1004 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1005 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1006 check_inode(&trans, &iter, k, &prev, &s, full));
1008 bch2_trans_exit(&trans);
1009 snapshots_seen_exit(&s);
1015 static struct bkey_s_c_dirent dirent_get_by_pos(struct btree_trans *trans,
1016 struct btree_iter *iter,
1019 return bch2_bkey_get_iter_typed(trans, iter, BTREE_ID_dirents, pos, 0, dirent);
1022 static bool inode_points_to_dirent(struct bch_inode_unpacked *inode,
1023 struct bkey_s_c_dirent d)
1025 return inode->bi_dir == d.k->p.inode &&
1026 inode->bi_dir_offset == d.k->p.offset;
1029 static bool dirent_points_to_inode(struct bkey_s_c_dirent d,
1030 struct bch_inode_unpacked *inode)
1032 return d.v->d_type == DT_SUBVOL
1033 ? le32_to_cpu(d.v->d_child_subvol) == inode->bi_subvol
1034 : le64_to_cpu(d.v->d_inum) == inode->bi_inum;
1037 static int inode_backpointer_exists(struct btree_trans *trans,
1038 struct bch_inode_unpacked *inode,
1041 struct btree_iter iter;
1042 struct bkey_s_c_dirent d;
1045 d = dirent_get_by_pos(trans, &iter,
1046 SPOS(inode->bi_dir, inode->bi_dir_offset, snapshot));
1049 return bch2_err_matches(ret, ENOENT) ? 0 : ret;
1051 ret = dirent_points_to_inode(d, inode);
1052 bch2_trans_iter_exit(trans, &iter);
1056 static int check_i_sectors(struct btree_trans *trans, struct inode_walker *w)
1058 struct bch_fs *c = trans->c;
1059 struct inode_walker_entry *i;
1060 u32 restart_count = trans->restart_count;
1064 darray_for_each(w->inodes, i) {
1065 if (i->inode.bi_sectors == i->count)
1068 count2 = bch2_count_inode_sectors(trans, w->last_pos.inode, i->snapshot);
1070 if (w->recalculate_sums)
1073 if (i->count != count2) {
1074 bch_err(c, "fsck counted i_sectors wrong for inode %llu:%u: got %llu should be %llu",
1075 w->last_pos.inode, i->snapshot, i->count, count2);
1076 return -BCH_ERR_internal_fsck_err;
1079 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SECTORS_DIRTY), c,
1080 "inode %llu:%u has incorrect i_sectors: got %llu, should be %llu",
1081 w->last_pos.inode, i->snapshot,
1082 i->inode.bi_sectors, i->count)) {
1083 i->inode.bi_sectors = i->count;
1084 ret = write_inode(trans, &i->inode, i->snapshot);
1092 if (!ret && trans_was_restarted(trans, restart_count))
1093 ret = -BCH_ERR_transaction_restart_nested;
1100 struct snapshots_seen seen;
1103 struct extent_ends {
1104 struct bpos last_pos;
1105 DARRAY(struct extent_end) e;
1108 static void extent_ends_reset(struct extent_ends *extent_ends)
1110 struct extent_end *i;
1112 darray_for_each(extent_ends->e, i)
1113 snapshots_seen_exit(&i->seen);
1115 extent_ends->e.nr = 0;
1118 static void extent_ends_exit(struct extent_ends *extent_ends)
1120 extent_ends_reset(extent_ends);
1121 darray_exit(&extent_ends->e);
1124 static void extent_ends_init(struct extent_ends *extent_ends)
1126 memset(extent_ends, 0, sizeof(*extent_ends));
1129 static int extent_ends_at(struct bch_fs *c,
1130 struct extent_ends *extent_ends,
1131 struct snapshots_seen *seen,
1134 struct extent_end *i, n = (struct extent_end) {
1135 .offset = k.k->p.offset,
1136 .snapshot = k.k->p.snapshot,
1140 n.seen.ids.data = kmemdup(seen->ids.data,
1141 sizeof(seen->ids.data[0]) * seen->ids.size,
1143 if (!n.seen.ids.data)
1144 return -BCH_ERR_ENOMEM_fsck_extent_ends_at;
1146 darray_for_each(extent_ends->e, i) {
1147 if (i->snapshot == k.k->p.snapshot) {
1148 snapshots_seen_exit(&i->seen);
1153 if (i->snapshot >= k.k->p.snapshot)
1157 return darray_insert_item(&extent_ends->e, i - extent_ends->e.data, n);
1160 static int overlapping_extents_found(struct btree_trans *trans,
1161 enum btree_id btree,
1162 struct bpos pos1, struct snapshots_seen *pos1_seen,
1165 struct extent_end *extent_end)
1167 struct bch_fs *c = trans->c;
1168 struct printbuf buf = PRINTBUF;
1169 struct btree_iter iter1, iter2 = { NULL };
1170 struct bkey_s_c k1, k2;
1173 BUG_ON(bkey_le(pos1, bkey_start_pos(&pos2)));
1175 bch2_trans_iter_init(trans, &iter1, btree, pos1,
1176 BTREE_ITER_ALL_SNAPSHOTS|
1177 BTREE_ITER_NOT_EXTENTS);
1178 k1 = bch2_btree_iter_peek_upto(&iter1, POS(pos1.inode, U64_MAX));
1183 prt_str(&buf, "\n ");
1184 bch2_bkey_val_to_text(&buf, c, k1);
1186 if (!bpos_eq(pos1, k1.k->p)) {
1187 prt_str(&buf, "\n wanted\n ");
1188 bch2_bpos_to_text(&buf, pos1);
1189 prt_str(&buf, "\n ");
1190 bch2_bkey_to_text(&buf, &pos2);
1192 bch_err(c, "%s: error finding first overlapping extent when repairing, got%s",
1194 ret = -BCH_ERR_internal_fsck_err;
1198 bch2_trans_copy_iter(&iter2, &iter1);
1201 bch2_btree_iter_advance(&iter2);
1203 k2 = bch2_btree_iter_peek_upto(&iter2, POS(pos1.inode, U64_MAX));
1208 if (bpos_ge(k2.k->p, pos2.p))
1212 prt_str(&buf, "\n ");
1213 bch2_bkey_val_to_text(&buf, c, k2);
1215 if (bpos_gt(k2.k->p, pos2.p) ||
1216 pos2.size != k2.k->size) {
1217 bch_err(c, "%s: error finding seconding overlapping extent when repairing%s",
1219 ret = -BCH_ERR_internal_fsck_err;
1223 prt_printf(&buf, "\n overwriting %s extent",
1224 pos1.snapshot >= pos2.p.snapshot ? "first" : "second");
1226 if (fsck_err(c, "overlapping extents%s", buf.buf)) {
1227 struct btree_iter *old_iter = &iter1;
1228 struct disk_reservation res = { 0 };
1230 if (pos1.snapshot < pos2.p.snapshot) {
1235 trans->extra_journal_res += bch2_bkey_sectors_compressed(k2);
1237 ret = bch2_trans_update_extent_overwrite(trans, old_iter,
1238 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE,
1240 bch2_trans_commit(trans, &res, NULL,
1241 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL);
1242 bch2_disk_reservation_put(c, &res);
1249 if (pos1.snapshot == pos2.p.snapshot) {
1251 * We overwrote the first extent, and did the overwrite
1252 * in the same snapshot:
1254 extent_end->offset = bkey_start_offset(&pos2);
1255 } else if (pos1.snapshot > pos2.p.snapshot) {
1257 * We overwrote the first extent in pos2's snapshot:
1259 ret = snapshots_seen_add_inorder(c, pos1_seen, pos2.p.snapshot);
1262 * We overwrote the second extent - restart
1263 * check_extent() from the top:
1265 ret = -BCH_ERR_transaction_restart_nested;
1270 bch2_trans_iter_exit(trans, &iter2);
1271 bch2_trans_iter_exit(trans, &iter1);
1272 printbuf_exit(&buf);
1276 static int check_overlapping_extents(struct btree_trans *trans,
1277 struct snapshots_seen *seen,
1278 struct extent_ends *extent_ends,
1281 struct btree_iter *iter,
1284 struct bch_fs *c = trans->c;
1285 struct extent_end *i;
1288 /* transaction restart, running again */
1289 if (bpos_eq(extent_ends->last_pos, k.k->p))
1292 if (extent_ends->last_pos.inode != k.k->p.inode)
1293 extent_ends_reset(extent_ends);
1295 darray_for_each(extent_ends->e, i) {
1296 if (i->offset <= bkey_start_offset(k.k))
1299 if (!ref_visible2(c,
1300 k.k->p.snapshot, seen,
1301 i->snapshot, &i->seen))
1304 ret = overlapping_extents_found(trans, iter->btree_id,
1305 SPOS(iter->pos.inode,
1314 ret = extent_ends_at(c, extent_ends, seen, k);
1318 extent_ends->last_pos = k.k->p;
1323 static int check_extent(struct btree_trans *trans, struct btree_iter *iter,
1325 struct inode_walker *inode,
1326 struct snapshots_seen *s,
1327 struct extent_ends *extent_ends)
1329 struct bch_fs *c = trans->c;
1330 struct inode_walker_entry *i;
1331 struct printbuf buf = PRINTBUF;
1332 struct bpos equiv = k.k->p;
1335 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1337 ret = check_key_has_snapshot(trans, iter, k);
1339 ret = ret < 0 ? ret : 0;
1343 if (inode->last_pos.inode != k.k->p.inode) {
1344 ret = check_i_sectors(trans, inode);
1349 i = walk_inode(trans, inode, equiv, k.k->type == KEY_TYPE_whiteout);
1350 ret = PTR_ERR_OR_ZERO(i);
1354 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1358 if (k.k->type != KEY_TYPE_whiteout) {
1359 if (fsck_err_on(!i, c,
1360 "extent in missing inode:\n %s",
1361 (printbuf_reset(&buf),
1362 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1365 if (fsck_err_on(i &&
1366 !S_ISREG(i->inode.bi_mode) &&
1367 !S_ISLNK(i->inode.bi_mode), c,
1368 "extent in non regular inode mode %o:\n %s",
1370 (printbuf_reset(&buf),
1371 bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1374 ret = check_overlapping_extents(trans, s, extent_ends, k,
1375 equiv.snapshot, iter,
1376 &inode->recalculate_sums);
1382 * Check inodes in reverse order, from oldest snapshots to newest,
1383 * starting from the inode that matches this extent's snapshot. If we
1384 * didn't have one, iterate over all inodes:
1387 i = inode->inodes.data + inode->inodes.nr - 1;
1390 inode->inodes.data && i >= inode->inodes.data;
1392 if (i->snapshot > equiv.snapshot ||
1393 !key_visible_in_snapshot(c, s, i->snapshot, equiv.snapshot))
1396 if (k.k->type != KEY_TYPE_whiteout) {
1397 if (fsck_err_on(!(i->inode.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
1398 k.k->p.offset > round_up(i->inode.bi_size, block_bytes(c)) >> 9 &&
1399 !bkey_extent_is_reservation(k), c,
1400 "extent type past end of inode %llu:%u, i_size %llu\n %s",
1401 i->inode.bi_inum, i->snapshot, i->inode.bi_size,
1402 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1403 struct btree_iter iter2;
1405 bch2_trans_copy_iter(&iter2, iter);
1406 bch2_btree_iter_set_snapshot(&iter2, i->snapshot);
1407 ret = bch2_btree_iter_traverse(&iter2) ?:
1408 bch2_btree_delete_at(trans, &iter2,
1409 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1410 bch2_trans_iter_exit(trans, &iter2);
1414 iter->k.type = KEY_TYPE_whiteout;
1417 if (bkey_extent_is_allocation(k.k))
1418 i->count += k.k->size;
1421 i->seen_this_pos = true;
1426 printbuf_exit(&buf);
1428 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1432 ret = bch2_btree_delete_at(trans, iter, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1437 * Walk extents: verify that extents have a corresponding S_ISREG inode, and
1438 * that i_size an i_sectors are consistent
1440 int bch2_check_extents(struct bch_fs *c)
1442 struct inode_walker w = inode_walker_init();
1443 struct snapshots_seen s;
1444 struct btree_trans trans;
1445 struct btree_iter iter;
1447 struct extent_ends extent_ends;
1448 struct disk_reservation res = { 0 };
1451 snapshots_seen_init(&s);
1452 extent_ends_init(&extent_ends);
1453 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 4096);
1455 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_extents,
1456 POS(BCACHEFS_ROOT_INO, 0),
1457 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1459 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL, ({
1460 bch2_disk_reservation_put(c, &res);
1461 check_extent(&trans, &iter, k, &w, &s, &extent_ends);
1463 check_i_sectors(&trans, &w);
1465 bch2_disk_reservation_put(c, &res);
1466 extent_ends_exit(&extent_ends);
1467 inode_walker_exit(&w);
1468 bch2_trans_exit(&trans);
1469 snapshots_seen_exit(&s);
1476 static int check_subdir_count(struct btree_trans *trans, struct inode_walker *w)
1478 struct bch_fs *c = trans->c;
1479 struct inode_walker_entry *i;
1480 u32 restart_count = trans->restart_count;
1484 darray_for_each(w->inodes, i) {
1485 if (i->inode.bi_nlink == i->count)
1488 count2 = bch2_count_subdirs(trans, w->last_pos.inode, i->snapshot);
1492 if (i->count != count2) {
1493 bch_err(c, "fsck counted subdirectories wrong: got %llu should be %llu",
1496 if (i->inode.bi_nlink == i->count)
1500 if (fsck_err_on(i->inode.bi_nlink != i->count, c,
1501 "directory %llu:%u with wrong i_nlink: got %u, should be %llu",
1502 w->last_pos.inode, i->snapshot, i->inode.bi_nlink, i->count)) {
1503 i->inode.bi_nlink = i->count;
1504 ret = write_inode(trans, &i->inode, i->snapshot);
1512 if (!ret && trans_was_restarted(trans, restart_count))
1513 ret = -BCH_ERR_transaction_restart_nested;
1517 static int check_dirent_target(struct btree_trans *trans,
1518 struct btree_iter *iter,
1519 struct bkey_s_c_dirent d,
1520 struct bch_inode_unpacked *target,
1521 u32 target_snapshot)
1523 struct bch_fs *c = trans->c;
1524 struct bkey_i_dirent *n;
1525 bool backpointer_exists = true;
1526 struct printbuf buf = PRINTBUF;
1529 if (!target->bi_dir &&
1530 !target->bi_dir_offset) {
1531 target->bi_dir = d.k->p.inode;
1532 target->bi_dir_offset = d.k->p.offset;
1534 ret = __write_inode(trans, target, target_snapshot);
1539 if (!inode_points_to_dirent(target, d)) {
1540 ret = inode_backpointer_exists(trans, target, d.k->p.snapshot);
1544 backpointer_exists = ret;
1547 if (fsck_err_on(S_ISDIR(target->bi_mode) &&
1548 backpointer_exists, c,
1549 "directory %llu with multiple links",
1551 ret = __remove_dirent(trans, d.k->p);
1555 if (fsck_err_on(backpointer_exists &&
1556 !target->bi_nlink, c,
1557 "inode %llu type %s has multiple links but i_nlink 0",
1558 target->bi_inum, bch2_d_types[d.v->d_type])) {
1560 target->bi_flags &= ~BCH_INODE_UNLINKED;
1562 ret = __write_inode(trans, target, target_snapshot);
1567 if (fsck_err_on(!backpointer_exists, c,
1568 "inode %llu:%u has wrong backpointer:\n"
1570 "should be %llu:%llu",
1571 target->bi_inum, target_snapshot,
1573 target->bi_dir_offset,
1576 target->bi_dir = d.k->p.inode;
1577 target->bi_dir_offset = d.k->p.offset;
1579 ret = __write_inode(trans, target, target_snapshot);
1585 if (fsck_err_on(d.v->d_type != inode_d_type(target), c,
1586 "incorrect d_type: got %s, should be %s:\n%s",
1587 bch2_d_type_str(d.v->d_type),
1588 bch2_d_type_str(inode_d_type(target)),
1589 (printbuf_reset(&buf),
1590 bch2_bkey_val_to_text(&buf, c, d.s_c), buf.buf))) {
1591 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1592 ret = PTR_ERR_OR_ZERO(n);
1596 bkey_reassemble(&n->k_i, d.s_c);
1597 n->v.d_type = inode_d_type(target);
1599 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1603 d = dirent_i_to_s_c(n);
1606 if (d.v->d_type == DT_SUBVOL &&
1607 target->bi_parent_subvol != le32_to_cpu(d.v->d_parent_subvol) &&
1608 (c->sb.version < bcachefs_metadata_version_subvol_dirent ||
1609 fsck_err(c, "dirent has wrong d_parent_subvol field: got %u, should be %u",
1610 le32_to_cpu(d.v->d_parent_subvol),
1611 target->bi_parent_subvol))) {
1612 n = bch2_trans_kmalloc(trans, bkey_bytes(d.k));
1613 ret = PTR_ERR_OR_ZERO(n);
1617 bkey_reassemble(&n->k_i, d.s_c);
1618 n->v.d_parent_subvol = cpu_to_le32(target->bi_parent_subvol);
1620 ret = bch2_trans_update(trans, iter, &n->k_i, 0);
1624 d = dirent_i_to_s_c(n);
1629 printbuf_exit(&buf);
1631 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1636 static int check_dirent(struct btree_trans *trans, struct btree_iter *iter,
1638 struct bch_hash_info *hash_info,
1639 struct inode_walker *dir,
1640 struct inode_walker *target,
1641 struct snapshots_seen *s)
1643 struct bch_fs *c = trans->c;
1644 struct bkey_s_c_dirent d;
1645 struct inode_walker_entry *i;
1646 struct printbuf buf = PRINTBUF;
1650 ret = check_key_has_snapshot(trans, iter, k);
1652 ret = ret < 0 ? ret : 0;
1657 equiv.snapshot = bch2_snapshot_equiv(c, k.k->p.snapshot);
1659 ret = snapshots_seen_update(c, s, iter->btree_id, k.k->p);
1663 if (k.k->type == KEY_TYPE_whiteout)
1666 if (dir->last_pos.inode != k.k->p.inode) {
1667 ret = check_subdir_count(trans, dir);
1672 BUG_ON(!iter->path->should_be_locked);
1674 i = walk_inode(trans, dir, equiv, k.k->type == KEY_TYPE_whiteout);
1675 ret = PTR_ERR_OR_ZERO(i);
1679 if (dir->first_this_inode && dir->inodes.nr)
1680 *hash_info = bch2_hash_info_init(c, &dir->inodes.data[0].inode);
1681 dir->first_this_inode = false;
1683 if (fsck_err_on(!i, c,
1684 "dirent in nonexisting directory:\n%s",
1685 (printbuf_reset(&buf),
1686 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1687 ret = bch2_btree_delete_at(trans, iter,
1688 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1695 if (fsck_err_on(!S_ISDIR(i->inode.bi_mode), c,
1696 "dirent in non directory inode type %s:\n%s",
1697 bch2_d_type_str(inode_d_type(&i->inode)),
1698 (printbuf_reset(&buf),
1699 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1700 ret = bch2_btree_delete_at(trans, iter, 0);
1704 ret = hash_check_key(trans, bch2_dirent_hash_desc, hash_info, iter, k);
1708 /* dirent has been deleted */
1713 if (k.k->type != KEY_TYPE_dirent)
1716 d = bkey_s_c_to_dirent(k);
1718 if (d.v->d_type == DT_SUBVOL) {
1719 struct bch_inode_unpacked subvol_root;
1720 u32 target_subvol = le32_to_cpu(d.v->d_child_subvol);
1721 u32 target_snapshot;
1724 ret = __subvol_lookup(trans, target_subvol,
1725 &target_snapshot, &target_inum);
1726 if (ret && !bch2_err_matches(ret, ENOENT))
1729 if (fsck_err_on(ret, c,
1730 "dirent points to missing subvolume %u",
1731 le32_to_cpu(d.v->d_child_subvol))) {
1732 ret = __remove_dirent(trans, d.k->p);
1736 ret = __lookup_inode(trans, target_inum,
1737 &subvol_root, &target_snapshot);
1738 if (ret && !bch2_err_matches(ret, ENOENT))
1741 if (fsck_err_on(ret, c,
1742 "subvolume %u points to missing subvolume root %llu",
1745 bch_err(c, "repair not implemented yet");
1750 if (fsck_err_on(subvol_root.bi_subvol != target_subvol, c,
1751 "subvol root %llu has wrong bi_subvol field: got %u, should be %u",
1753 subvol_root.bi_subvol, target_subvol)) {
1754 subvol_root.bi_subvol = target_subvol;
1755 ret = __write_inode(trans, &subvol_root, target_snapshot);
1760 ret = check_dirent_target(trans, iter, d, &subvol_root,
1765 ret = __get_visible_inodes(trans, target, s, le64_to_cpu(d.v->d_inum));
1769 if (fsck_err_on(!target->inodes.nr, c,
1770 "dirent points to missing inode: (equiv %u)\n%s",
1772 (printbuf_reset(&buf),
1773 bch2_bkey_val_to_text(&buf, c, k),
1775 ret = __remove_dirent(trans, d.k->p);
1780 darray_for_each(target->inodes, i) {
1781 ret = check_dirent_target(trans, iter, d,
1782 &i->inode, i->snapshot);
1788 if (d.v->d_type == DT_DIR)
1789 for_each_visible_inode(c, s, dir, equiv.snapshot, i)
1795 printbuf_exit(&buf);
1797 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1803 * Walk dirents: verify that they all have a corresponding S_ISDIR inode,
1806 int bch2_check_dirents(struct bch_fs *c)
1808 struct inode_walker dir = inode_walker_init();
1809 struct inode_walker target = inode_walker_init();
1810 struct snapshots_seen s;
1811 struct bch_hash_info hash_info;
1812 struct btree_trans trans;
1813 struct btree_iter iter;
1817 snapshots_seen_init(&s);
1818 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1820 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_dirents,
1821 POS(BCACHEFS_ROOT_INO, 0),
1822 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1825 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1826 check_dirent(&trans, &iter, k, &hash_info, &dir, &target, &s));
1828 bch2_trans_exit(&trans);
1829 snapshots_seen_exit(&s);
1830 inode_walker_exit(&dir);
1831 inode_walker_exit(&target);
1838 static int check_xattr(struct btree_trans *trans, struct btree_iter *iter,
1840 struct bch_hash_info *hash_info,
1841 struct inode_walker *inode)
1843 struct bch_fs *c = trans->c;
1844 struct inode_walker_entry *i;
1847 ret = check_key_has_snapshot(trans, iter, k);
1851 i = walk_inode(trans, inode, k.k->p, k.k->type == KEY_TYPE_whiteout);
1852 ret = PTR_ERR_OR_ZERO(i);
1856 if (inode->first_this_inode && inode->inodes.nr)
1857 *hash_info = bch2_hash_info_init(c, &inode->inodes.data[0].inode);
1858 inode->first_this_inode = false;
1860 if (fsck_err_on(!i, c,
1861 "xattr for missing inode %llu",
1863 return bch2_btree_delete_at(trans, iter, 0);
1868 ret = hash_check_key(trans, bch2_xattr_hash_desc, hash_info, iter, k);
1870 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
1876 * Walk xattrs: verify that they all have a corresponding inode
1878 int bch2_check_xattrs(struct bch_fs *c)
1880 struct inode_walker inode = inode_walker_init();
1881 struct bch_hash_info hash_info;
1882 struct btree_trans trans;
1883 struct btree_iter iter;
1887 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
1889 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_xattrs,
1890 POS(BCACHEFS_ROOT_INO, 0),
1891 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS,
1894 BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
1895 check_xattr(&trans, &iter, k, &hash_info, &inode));
1897 bch2_trans_exit(&trans);
1904 static int check_root_trans(struct btree_trans *trans)
1906 struct bch_fs *c = trans->c;
1907 struct bch_inode_unpacked root_inode;
1912 ret = __subvol_lookup(trans, BCACHEFS_ROOT_SUBVOL, &snapshot, &inum);
1913 if (ret && !bch2_err_matches(ret, ENOENT))
1916 if (mustfix_fsck_err_on(ret, c, "root subvol missing")) {
1917 struct bkey_i_subvolume root_subvol;
1920 inum = BCACHEFS_ROOT_INO;
1922 bkey_subvolume_init(&root_subvol.k_i);
1923 root_subvol.k.p.offset = BCACHEFS_ROOT_SUBVOL;
1924 root_subvol.v.flags = 0;
1925 root_subvol.v.snapshot = cpu_to_le32(snapshot);
1926 root_subvol.v.inode = cpu_to_le64(inum);
1927 ret = commit_do(trans, NULL, NULL,
1928 BTREE_INSERT_NOFAIL|
1929 BTREE_INSERT_LAZY_RW,
1930 __bch2_btree_insert(trans, BTREE_ID_subvolumes,
1931 &root_subvol.k_i, 0));
1933 bch_err(c, "error writing root subvol: %s", bch2_err_str(ret));
1939 ret = __lookup_inode(trans, BCACHEFS_ROOT_INO, &root_inode, &snapshot);
1940 if (ret && !bch2_err_matches(ret, ENOENT))
1943 if (mustfix_fsck_err_on(ret, c, "root directory missing") ||
1944 mustfix_fsck_err_on(!S_ISDIR(root_inode.bi_mode), c,
1945 "root inode not a directory")) {
1946 bch2_inode_init(c, &root_inode, 0, 0, S_IFDIR|0755,
1948 root_inode.bi_inum = inum;
1950 ret = __write_inode(trans, &root_inode, snapshot);
1952 bch_err(c, "error writing root inode: %s", bch2_err_str(ret));
1959 /* Get root directory, create if it doesn't exist: */
1960 int bch2_check_root(struct bch_fs *c)
1964 ret = bch2_trans_do(c, NULL, NULL,
1965 BTREE_INSERT_NOFAIL|
1966 BTREE_INSERT_LAZY_RW,
1967 check_root_trans(&trans));
1974 struct pathbuf_entry {
1979 typedef DARRAY(struct pathbuf_entry) pathbuf;
1981 static bool path_is_dup(pathbuf *p, u64 inum, u32 snapshot)
1983 struct pathbuf_entry *i;
1985 darray_for_each(*p, i)
1986 if (i->inum == inum &&
1987 i->snapshot == snapshot)
1993 static int path_down(struct bch_fs *c, pathbuf *p,
1994 u64 inum, u32 snapshot)
1996 int ret = darray_push(p, ((struct pathbuf_entry) {
1998 .snapshot = snapshot,
2002 bch_err(c, "fsck: error allocating memory for pathbuf, size %zu",
2008 * Check that a given inode is reachable from the root:
2010 * XXX: we should also be verifying that inodes are in the right subvolumes
2012 static int check_path(struct btree_trans *trans,
2014 struct bch_inode_unpacked *inode,
2017 struct bch_fs *c = trans->c;
2020 snapshot = bch2_snapshot_equiv(c, snapshot);
2023 while (!(inode->bi_inum == BCACHEFS_ROOT_INO &&
2024 inode->bi_subvol == BCACHEFS_ROOT_SUBVOL)) {
2025 struct btree_iter dirent_iter;
2026 struct bkey_s_c_dirent d;
2027 u32 parent_snapshot = snapshot;
2029 if (inode->bi_subvol) {
2032 ret = subvol_lookup(trans, inode->bi_parent_subvol,
2033 &parent_snapshot, &inum);
2038 ret = lockrestart_do(trans,
2039 PTR_ERR_OR_ZERO((d = dirent_get_by_pos(trans, &dirent_iter,
2040 SPOS(inode->bi_dir, inode->bi_dir_offset,
2041 parent_snapshot))).k));
2042 if (ret && !bch2_err_matches(ret, ENOENT))
2045 if (!ret && !dirent_points_to_inode(d, inode)) {
2046 bch2_trans_iter_exit(trans, &dirent_iter);
2047 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
2050 if (bch2_err_matches(ret, ENOENT)) {
2051 if (fsck_err(c, "unreachable inode %llu:%u, type %s nlink %u backptr %llu:%llu",
2052 inode->bi_inum, snapshot,
2053 bch2_d_type_str(inode_d_type(inode)),
2056 inode->bi_dir_offset))
2057 ret = reattach_inode(trans, inode, snapshot);
2061 bch2_trans_iter_exit(trans, &dirent_iter);
2063 if (!S_ISDIR(inode->bi_mode))
2066 ret = path_down(c, p, inode->bi_inum, snapshot);
2068 bch_err(c, "memory allocation failure");
2072 snapshot = parent_snapshot;
2074 ret = lookup_inode(trans, inode->bi_dir, inode, &snapshot);
2076 /* Should have been caught in dirents pass */
2077 bch_err(c, "error looking up parent directory: %i", ret);
2081 if (path_is_dup(p, inode->bi_inum, snapshot)) {
2082 struct pathbuf_entry *i;
2084 /* XXX print path */
2085 bch_err(c, "directory structure loop");
2087 darray_for_each(*p, i)
2088 pr_err("%llu:%u", i->inum, i->snapshot);
2089 pr_err("%llu:%u", inode->bi_inum, snapshot);
2091 if (!fsck_err(c, "directory structure loop"))
2094 ret = commit_do(trans, NULL, NULL,
2095 BTREE_INSERT_NOFAIL|
2096 BTREE_INSERT_LAZY_RW,
2097 remove_backpointer(trans, inode));
2099 bch_err(c, "error removing dirent: %i", ret);
2103 ret = reattach_inode(trans, inode, snapshot);
2113 * Check for unreachable inodes, as well as loops in the directory structure:
2114 * After bch2_check_dirents(), if an inode backpointer doesn't exist that means it's
2117 int bch2_check_directory_structure(struct bch_fs *c)
2119 struct btree_trans trans;
2120 struct btree_iter iter;
2122 struct bch_inode_unpacked u;
2123 pathbuf path = { 0, };
2126 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2128 for_each_btree_key(&trans, iter, BTREE_ID_inodes, POS_MIN,
2130 BTREE_ITER_PREFETCH|
2131 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2132 if (!bkey_is_inode(k.k))
2135 ret = bch2_inode_unpack(k, &u);
2137 /* Should have been caught earlier in fsck: */
2138 bch_err(c, "error unpacking inode %llu: %i", k.k->p.offset, ret);
2142 if (u.bi_flags & BCH_INODE_UNLINKED)
2145 ret = check_path(&trans, &path, &u, iter.pos.snapshot);
2149 bch2_trans_iter_exit(&trans, &iter);
2150 bch2_trans_exit(&trans);
2158 /* check_nlink pass: */
2160 struct nlink_table {
2171 static int add_nlink(struct bch_fs *c, struct nlink_table *t,
2172 u64 inum, u32 snapshot)
2174 if (t->nr == t->size) {
2175 size_t new_size = max_t(size_t, 128UL, t->size * 2);
2176 void *d = kvmalloc_array(new_size, sizeof(t->d[0]), GFP_KERNEL);
2179 bch_err(c, "fsck: error allocating memory for nlink_table, size %zu",
2181 return -BCH_ERR_ENOMEM_fsck_add_nlink;
2185 memcpy(d, t->d, t->size * sizeof(t->d[0]));
2193 t->d[t->nr++] = (struct nlink) {
2195 .snapshot = snapshot,
2201 static int nlink_cmp(const void *_l, const void *_r)
2203 const struct nlink *l = _l;
2204 const struct nlink *r = _r;
2206 return cmp_int(l->inum, r->inum) ?: cmp_int(l->snapshot, r->snapshot);
2209 static void inc_link(struct bch_fs *c, struct snapshots_seen *s,
2210 struct nlink_table *links,
2211 u64 range_start, u64 range_end, u64 inum, u32 snapshot)
2213 struct nlink *link, key = {
2214 .inum = inum, .snapshot = U32_MAX,
2217 if (inum < range_start || inum >= range_end)
2220 link = __inline_bsearch(&key, links->d, links->nr,
2221 sizeof(links->d[0]), nlink_cmp);
2225 while (link > links->d && link[0].inum == link[-1].inum)
2228 for (; link < links->d + links->nr && link->inum == inum; link++)
2229 if (ref_visible(c, s, snapshot, link->snapshot)) {
2231 if (link->snapshot >= snapshot)
2237 static int check_nlinks_find_hardlinks(struct bch_fs *c,
2238 struct nlink_table *t,
2239 u64 start, u64 *end)
2241 struct btree_trans trans;
2242 struct btree_iter iter;
2244 struct bch_inode_unpacked u;
2247 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2249 for_each_btree_key(&trans, iter, BTREE_ID_inodes,
2252 BTREE_ITER_PREFETCH|
2253 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2254 if (!bkey_is_inode(k.k))
2257 /* Should never fail, checked by bch2_inode_invalid: */
2258 BUG_ON(bch2_inode_unpack(k, &u));
2261 * Backpointer and directory structure checks are sufficient for
2262 * directories, since they can't have hardlinks:
2264 if (S_ISDIR(u.bi_mode))
2270 ret = add_nlink(c, t, k.k->p.offset, k.k->p.snapshot);
2272 *end = k.k->p.offset;
2278 bch2_trans_iter_exit(&trans, &iter);
2279 bch2_trans_exit(&trans);
2282 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2288 static int check_nlinks_walk_dirents(struct bch_fs *c, struct nlink_table *links,
2289 u64 range_start, u64 range_end)
2291 struct btree_trans trans;
2292 struct snapshots_seen s;
2293 struct btree_iter iter;
2295 struct bkey_s_c_dirent d;
2298 snapshots_seen_init(&s);
2300 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2302 for_each_btree_key(&trans, iter, BTREE_ID_dirents, POS_MIN,
2304 BTREE_ITER_PREFETCH|
2305 BTREE_ITER_ALL_SNAPSHOTS, k, ret) {
2306 ret = snapshots_seen_update(c, &s, iter.btree_id, k.k->p);
2310 switch (k.k->type) {
2311 case KEY_TYPE_dirent:
2312 d = bkey_s_c_to_dirent(k);
2314 if (d.v->d_type != DT_DIR &&
2315 d.v->d_type != DT_SUBVOL)
2316 inc_link(c, &s, links, range_start, range_end,
2317 le64_to_cpu(d.v->d_inum),
2318 bch2_snapshot_equiv(c, d.k->p.snapshot));
2322 bch2_trans_iter_exit(&trans, &iter);
2325 bch_err(c, "error in fsck: btree error %i while walking dirents", ret);
2327 bch2_trans_exit(&trans);
2328 snapshots_seen_exit(&s);
2332 static int check_nlinks_update_inode(struct btree_trans *trans, struct btree_iter *iter,
2334 struct nlink_table *links,
2335 size_t *idx, u64 range_end)
2337 struct bch_fs *c = trans->c;
2338 struct bch_inode_unpacked u;
2339 struct nlink *link = &links->d[*idx];
2342 if (k.k->p.offset >= range_end)
2345 if (!bkey_is_inode(k.k))
2348 BUG_ON(bch2_inode_unpack(k, &u));
2350 if (S_ISDIR(u.bi_mode))
2356 while ((cmp_int(link->inum, k.k->p.offset) ?:
2357 cmp_int(link->snapshot, k.k->p.snapshot)) < 0) {
2358 BUG_ON(*idx == links->nr);
2359 link = &links->d[++*idx];
2362 if (fsck_err_on(bch2_inode_nlink_get(&u) != link->count, c,
2363 "inode %llu type %s has wrong i_nlink (%u, should be %u)",
2364 u.bi_inum, bch2_d_types[mode_to_type(u.bi_mode)],
2365 bch2_inode_nlink_get(&u), link->count)) {
2366 bch2_inode_nlink_set(&u, link->count);
2367 ret = __write_inode(trans, &u, k.k->p.snapshot);
2374 static int check_nlinks_update_hardlinks(struct bch_fs *c,
2375 struct nlink_table *links,
2376 u64 range_start, u64 range_end)
2378 struct btree_trans trans;
2379 struct btree_iter iter;
2384 bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
2386 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_inodes,
2387 POS(0, range_start),
2388 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
2389 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
2390 check_nlinks_update_inode(&trans, &iter, k, links, &idx, range_end));
2392 bch2_trans_exit(&trans);
2395 bch_err(c, "error in fsck: btree error %i while walking inodes", ret);
2402 int bch2_check_nlinks(struct bch_fs *c)
2404 struct nlink_table links = { 0 };
2405 u64 this_iter_range_start, next_iter_range_start = 0;
2409 this_iter_range_start = next_iter_range_start;
2410 next_iter_range_start = U64_MAX;
2412 ret = check_nlinks_find_hardlinks(c, &links,
2413 this_iter_range_start,
2414 &next_iter_range_start);
2416 ret = check_nlinks_walk_dirents(c, &links,
2417 this_iter_range_start,
2418 next_iter_range_start);
2422 ret = check_nlinks_update_hardlinks(c, &links,
2423 this_iter_range_start,
2424 next_iter_range_start);
2429 } while (next_iter_range_start != U64_MAX);
2438 static int fix_reflink_p_key(struct btree_trans *trans, struct btree_iter *iter,
2441 struct bkey_s_c_reflink_p p;
2442 struct bkey_i_reflink_p *u;
2445 if (k.k->type != KEY_TYPE_reflink_p)
2448 p = bkey_s_c_to_reflink_p(k);
2450 if (!p.v->front_pad && !p.v->back_pad)
2453 u = bch2_trans_kmalloc(trans, sizeof(*u));
2454 ret = PTR_ERR_OR_ZERO(u);
2458 bkey_reassemble(&u->k_i, k);
2462 return bch2_trans_update(trans, iter, &u->k_i, BTREE_TRIGGER_NORUN);
2465 int bch2_fix_reflink_p(struct bch_fs *c)
2467 struct btree_iter iter;
2471 if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix)
2474 ret = bch2_trans_run(c,
2475 for_each_btree_key_commit(&trans, iter,
2476 BTREE_ID_extents, POS_MIN,
2477 BTREE_ITER_INTENT|BTREE_ITER_PREFETCH|
2478 BTREE_ITER_ALL_SNAPSHOTS, k,
2479 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
2480 fix_reflink_p_key(&trans, &iter, k)));