From: Kent Overstreet <kent.overstreet@linux.dev>
To: linux-bcachefs@vger.kernel.org
Cc: Kent Overstreet <kent.overstreet@linux.dev>
Subject: [PATCH 21/37] bcachefs: fsck: Improve check_key_has_inode()
Date: Wed, 18 Jun 2025 17:54:12 -0400 [thread overview]
Message-ID: <20250618215431.738317-22-kent.overstreet@linux.dev> (raw)
In-Reply-To: <20250618215431.738317-1-kent.overstreet@linux.dev>
Print out more info when we find a key (extent, dirent, xattr) for a
missing inode - was there a good inode in an older snapshot, full(ish)
list of keys for that missing inode, so we can make better decisions on
how to repair.
If it looks like it should've been deleted, autofix it. If we ever hit
the non-autofix cases, we'll want to write more repair code (possibly
reconstituting the inode).
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
---
fs/bcachefs/fsck.c | 108 ++++++++++++++++++++++++++-------
fs/bcachefs/sb-errors_format.h | 2 +-
2 files changed, 88 insertions(+), 22 deletions(-)
diff --git a/fs/bcachefs/fsck.c b/fs/bcachefs/fsck.c
index 48810a8e5d4b..f25d33fb3e23 100644
--- a/fs/bcachefs/fsck.c
+++ b/fs/bcachefs/fsck.c
@@ -1436,6 +1436,7 @@ static int check_key_has_inode(struct btree_trans *trans,
{
struct bch_fs *c = trans->c;
struct printbuf buf = PRINTBUF;
+ struct btree_iter iter2 = {};
int ret = PTR_ERR_OR_ZERO(i);
if (ret)
return ret;
@@ -1445,40 +1446,105 @@ static int check_key_has_inode(struct btree_trans *trans,
bool have_inode = i && !i->whiteout;
- if (!have_inode && (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_inodes))) {
- ret = reconstruct_inode(trans, iter->btree_id, k.k->p.snapshot, k.k->p.inode) ?:
- bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
- if (ret)
- goto err;
+ if (!have_inode && (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_inodes)))
+ goto reconstruct;
- inode->last_pos.inode--;
- ret = bch_err_throw(c, transaction_restart_nested);
- goto err;
+ if (have_inode && btree_matches_i_mode(iter->btree_id, i->inode.bi_mode))
+ goto out;
+
+ prt_printf(&buf, ", ");
+
+ bool have_old_inode = false;
+ darray_for_each(inode->inodes, i2)
+ if (!i2->whiteout &&
+ bch2_snapshot_is_ancestor(c, k.k->p.snapshot, i2->inode.bi_snapshot) &&
+ btree_matches_i_mode(iter->btree_id, i2->inode.bi_mode)) {
+ prt_printf(&buf, "but found good inode in older snapshot\n");
+ bch2_inode_unpacked_to_text(&buf, &i2->inode);
+ prt_newline(&buf);
+ have_old_inode = true;
+ break;
+ }
+
+ struct bkey_s_c k2;
+ unsigned nr_keys = 0;
+
+ prt_printf(&buf, "found keys:\n");
+
+ for_each_btree_key_max_norestart(trans, iter2, iter->btree_id,
+ SPOS(k.k->p.inode, 0, k.k->p.snapshot),
+ POS(k.k->p.inode, U64_MAX),
+ 0, k2, ret) {
+ nr_keys++;
+ if (nr_keys <= 10) {
+ bch2_bkey_val_to_text(&buf, c, k2);
+ prt_newline(&buf);
+ }
+ if (nr_keys >= 100)
+ break;
}
- if (fsck_err_on(!have_inode,
- trans, key_in_missing_inode,
- "key in missing inode:\n%s",
- (printbuf_reset(&buf),
- bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
- goto delete;
+ if (ret)
+ goto err;
- if (fsck_err_on(have_inode && !btree_matches_i_mode(iter->btree_id, i->inode.bi_mode),
- trans, key_in_wrong_inode_type,
- "key for wrong inode mode %o:\n%s",
- i->inode.bi_mode,
- (printbuf_reset(&buf),
- bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
- goto delete;
+ if (nr_keys > 100)
+ prt_printf(&buf, "found > %u keys for this missing inode\n", nr_keys);
+ else if (nr_keys > 10)
+ prt_printf(&buf, "found %u keys for this missing inode\n", nr_keys);
+
+ if (!have_inode) {
+ if (fsck_err_on(!have_inode,
+ trans, key_in_missing_inode,
+ "key in missing inode%s", buf.buf)) {
+ /*
+ * Maybe a deletion that raced with data move, or something
+ * weird like that? But if we know the inode was deleted, or
+ * it's just a few keys, we can safely delete them.
+ *
+ * If it's many keys, we should probably recreate the inode
+ */
+ if (have_old_inode || nr_keys <= 2)
+ goto delete;
+ else
+ goto reconstruct;
+ }
+ } else {
+ /*
+ * not autofix, this one would be a giant wtf - bit error in the
+ * inode corrupting i_mode?
+ *
+ * may want to try repairing inode instead of deleting
+ */
+ if (fsck_err_on(!btree_matches_i_mode(iter->btree_id, i->inode.bi_mode),
+ trans, key_in_wrong_inode_type,
+ "key for wrong inode mode %o%s",
+ i->inode.bi_mode, buf.buf))
+ goto delete;
+ }
out:
err:
fsck_err:
+ bch2_trans_iter_exit(trans, &iter2);
printbuf_exit(&buf);
bch_err_fn(c, ret);
return ret;
delete:
+ /*
+ * XXX: print out more info
+ * count up extents for this inode, check if we have different inode in
+ * an older snapshot version, perhaps decide if we want to reconstitute
+ */
ret = bch2_btree_delete_at(trans, iter, BTREE_UPDATE_internal_snapshot_node);
goto out;
+reconstruct:
+ ret = reconstruct_inode(trans, iter->btree_id, k.k->p.snapshot, k.k->p.inode) ?:
+ bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
+ if (ret)
+ goto err;
+
+ inode->last_pos.inode--;
+ ret = bch_err_throw(c, transaction_restart_nested);
+ goto out;
}
static int check_i_sectors_notnested(struct btree_trans *trans, struct inode_walker *w)
diff --git a/fs/bcachefs/sb-errors_format.h b/fs/bcachefs/sb-errors_format.h
index 56971ebe0f78..f1aa40542a0e 100644
--- a/fs/bcachefs/sb-errors_format.h
+++ b/fs/bcachefs/sb-errors_format.h
@@ -251,7 +251,7 @@ enum bch_fsck_flags {
x(deleted_inode_not_unlinked, 214, FSCK_AUTOFIX) \
x(deleted_inode_has_child_snapshots, 288, FSCK_AUTOFIX) \
x(extent_overlapping, 215, 0) \
- x(key_in_missing_inode, 216, 0) \
+ x(key_in_missing_inode, 216, FSCK_AUTOFIX) \
x(key_in_wrong_inode_type, 217, 0) \
x(extent_past_end_of_inode, 218, FSCK_AUTOFIX) \
x(dirent_empty_name, 219, 0) \
--
2.50.0
next prev parent reply other threads:[~2025-06-18 21:55 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-18 21:53 [PATCH 00/37] 6.16-rc3 hotfixes series Kent Overstreet
2025-06-18 21:53 ` [PATCH 01/37] bcachefs: trace_extent_trim_atomic Kent Overstreet
2025-06-18 21:53 ` [PATCH 02/37] bcachefs: btree iter tracepoints Kent Overstreet
2025-06-18 21:53 ` [PATCH 03/37] bcachefs: Don't allocate new memory when mempool is exhausted Kent Overstreet
2025-06-18 21:53 ` [PATCH 04/37] bcachefs: Fix alloc_req use after free Kent Overstreet
2025-06-18 21:53 ` [PATCH 05/37] bcachefs: Add missing EBUG_ON Kent Overstreet
2025-06-18 21:53 ` [PATCH 06/37] bcachefs: Delay calculation of trans->journal_u64s Kent Overstreet
2025-06-18 21:53 ` [PATCH 07/37] bcachefs: Fix bch2_journal_keys_peek_prev_min() Kent Overstreet
2025-06-18 21:53 ` [PATCH 08/37] bcachefs: btree_iter: fix updates, journal overlay Kent Overstreet
2025-06-18 21:54 ` [PATCH 09/37] bcachefs: better __bch2_snapshot_is_ancestor() assert Kent Overstreet
2025-06-18 21:54 ` [PATCH 10/37] bcachefs: pass last_seq into fs_journal_start() Kent Overstreet
2025-06-18 21:54 ` [PATCH 11/37] bcachefs: Fix "now allowing incompatible features" message Kent Overstreet
2025-06-18 21:54 ` [PATCH 12/37] bcachefs: Fix snapshot_key_missing_inode_snapshot repair Kent Overstreet
2025-06-18 21:54 ` [PATCH 13/37] bcachefs: fsck: fix add_inode() Kent Overstreet
2025-06-18 21:54 ` [PATCH 14/37] bcachefs: fsck: fix extent past end of inode repair Kent Overstreet
2025-06-18 21:54 ` [PATCH 15/37] bcachefs: opts.journal_rewind Kent Overstreet
2025-06-18 21:54 ` [PATCH 16/37] bcachefs: Kill unused tracepoints Kent Overstreet
2025-06-18 21:54 ` [PATCH 17/37] bcachefs: mark more errors autofix Kent Overstreet
2025-06-18 21:54 ` [PATCH 18/37] bcachefs: Move bset size check before csum check Kent Overstreet
2025-06-18 21:54 ` [PATCH 19/37] bcachefs: Fix pool->alloc NULL pointer dereference Kent Overstreet
2025-06-18 21:54 ` [PATCH 20/37] bcachefs: don't return fsck_fix for unfixable node errors in __btree_err Kent Overstreet
2025-06-18 21:54 ` Kent Overstreet [this message]
2025-06-18 21:54 ` [PATCH 22/37] bcachefs: Call bch2_fs_init_rw() early if we'll be going rw Kent Overstreet
2025-06-18 21:54 ` [PATCH 23/37] bcachefs: Fix __bch2_inum_to_path() when crossing subvol boundaries Kent Overstreet
2025-06-18 21:54 ` [PATCH 24/37] bcachefs: fsck: Print path when we find a subvol loop Kent Overstreet
2025-06-18 21:54 ` [PATCH 25/37] bcachefs: fsck: Fix remove_backpointer() for subvol roots Kent Overstreet
2025-06-18 21:54 ` [PATCH 26/37] bcachefs: fsck: Fix reattach_inode() " Kent Overstreet
2025-06-18 21:54 ` [PATCH 27/37] bcachefs: fsck: check_directory_structure runs in reverse order Kent Overstreet
2025-06-18 21:54 ` [PATCH 28/37] bcachefs: fsck: additional diagnostics for reattach_inode() Kent Overstreet
2025-06-18 21:54 ` [PATCH 29/37] bcachefs: fsck: check_subdir_count logs path Kent Overstreet
2025-06-18 21:54 ` [PATCH 30/37] bcachefs: fsck: Fix check_path_loop() + snapshots Kent Overstreet
2025-06-18 21:54 ` [PATCH 31/37] bcachefs: Fix bch2_read_bio_to_text() Kent Overstreet
2025-06-18 21:54 ` [PATCH 32/37] bcachefs: Fix restart handling in btree_node_scrub_work() Kent Overstreet
2025-06-18 21:54 ` [PATCH 33/37] bcachefs: fsck: Fix check_directory_structure when no check_dirents Kent Overstreet
2025-06-18 21:54 ` [PATCH 34/37] bcachefs: fsck: fix unhandled restart in topology repair Kent Overstreet
2025-06-18 21:54 ` [PATCH 35/37] bcachefs: fsck: Fix oops in key_visible_in_snapshot() Kent Overstreet
2025-06-18 21:54 ` [PATCH 36/37] bcachefs: fix spurious error in read_btree_roots() Kent Overstreet
2025-06-18 21:54 ` [PATCH 37/37] bcachefs: Fix missing newlines before ero Kent Overstreet
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250618215431.738317-22-kent.overstreet@linux.dev \
--to=kent.overstreet@linux.dev \
--cc=linux-bcachefs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).