public inbox for linux-bcachefs@vger.kernel.org
 help / color / mirror / Atom feed
From: Kent Overstreet <kent.overstreet@linux.dev>
To: linux-bcachefs@vger.kernel.org
Cc: Kent Overstreet <kent.overstreet@linux.dev>
Subject: [PATCH 34/34] bcachefs: Don't recurse in check_discard_freespace_key
Date: Fri, 29 Nov 2024 15:27:33 -0500	[thread overview]
Message-ID: <20241129202736.2713679-35-kent.overstreet@linux.dev> (raw)
In-Reply-To: <20241129202736.2713679-1-kent.overstreet@linux.dev>

When calling check_discard_freeespace_key from the allocator, we can't
repair without recursing - run it asynchronously instead.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
---
 fs/bcachefs/alloc_background.c | 72 ++++++++++++++++++++++++++++++----
 fs/bcachefs/alloc_background.h |  2 +-
 fs/bcachefs/alloc_foreground.c |  2 +-
 fs/bcachefs/bcachefs.h         |  1 +
 4 files changed, 67 insertions(+), 10 deletions(-)

diff --git a/fs/bcachefs/alloc_background.c b/fs/bcachefs/alloc_background.c
index e8c246e5803c..b2d570453351 100644
--- a/fs/bcachefs/alloc_background.c
+++ b/fs/bcachefs/alloc_background.c
@@ -1338,7 +1338,40 @@ int bch2_check_alloc_hole_bucket_gens(struct btree_trans *trans,
 	return ret;
 }
 
-int bch2_check_discard_freespace_key(struct btree_trans *trans, struct btree_iter *iter, u8 *gen)
+struct check_discard_freespace_key_async {
+	struct work_struct	work;
+	struct bch_fs		*c;
+	struct bbpos		pos;
+};
+
+static int bch2_recheck_discard_freespace_key(struct btree_trans *trans, struct bbpos pos)
+{
+	struct btree_iter iter;
+	struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter, pos.btree, pos.pos, 0);
+	int ret = bkey_err(k);
+	if (ret)
+		return ret;
+
+	u8 gen;
+	ret = k.k->type != KEY_TYPE_set
+		? bch2_check_discard_freespace_key(trans, &iter, &gen, false)
+		: 0;
+	bch2_trans_iter_exit(trans, &iter);
+	return ret;
+}
+
+static void check_discard_freespace_key_work(struct work_struct *work)
+{
+	struct check_discard_freespace_key_async *w =
+		container_of(work, struct check_discard_freespace_key_async, work);
+
+	bch2_trans_do(w->c, bch2_recheck_discard_freespace_key(trans, w->pos));
+	bch2_write_ref_put(w->c, BCH_WRITE_REF_check_discard_freespace_key);
+	kfree(w);
+}
+
+int bch2_check_discard_freespace_key(struct btree_trans *trans, struct btree_iter *iter, u8 *gen,
+				     bool async_repair)
 {
 	struct bch_fs *c = trans->c;
 	enum bch_data_type state = iter->btree_id == BTREE_ID_need_discard
@@ -1351,7 +1384,8 @@ int bch2_check_discard_freespace_key(struct btree_trans *trans, struct btree_ite
 	u64 genbits = iter->pos.offset & (~0ULL << 56);
 
 	struct btree_iter alloc_iter;
-	struct bkey_s_c alloc_k = bch2_bkey_get_iter(trans, &alloc_iter, BTREE_ID_alloc, bucket, BTREE_ITER_cached);
+	struct bkey_s_c alloc_k = bch2_bkey_get_iter(trans, &alloc_iter,
+						     BTREE_ID_alloc, bucket, BTREE_ITER_cached);
 	int ret = bkey_err(alloc_k);
 	if (ret)
 		return ret;
@@ -1392,17 +1426,39 @@ int bch2_check_discard_freespace_key(struct btree_trans *trans, struct btree_ite
 	printbuf_exit(&buf);
 	return ret;
 delete:
-	ret =   bch2_btree_bit_mod_iter(trans, iter, false) ?:
-		bch2_trans_commit(trans, NULL, NULL,
-			BCH_TRANS_COMMIT_no_enospc) ?:
-		-BCH_ERR_transaction_restart_commit;
-	goto out;
+	if (!async_repair) {
+		ret =   bch2_btree_bit_mod_iter(trans, iter, false) ?:
+			bch2_trans_commit(trans, NULL, NULL,
+				BCH_TRANS_COMMIT_no_enospc) ?:
+			-BCH_ERR_transaction_restart_commit;
+		goto out;
+	} else {
+		/*
+		 * We can't repair here when called from the allocator path: the
+		 * commit will recurse back into the allocator
+		 */
+		struct check_discard_freespace_key_async *w =
+			kzalloc(sizeof(*w), GFP_KERNEL);
+		if (!w)
+			goto out;
+
+		if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_check_discard_freespace_key)) {
+			kfree(w);
+			goto out;
+		}
+
+		INIT_WORK(&w->work, check_discard_freespace_key_work);
+		w->c = c;
+		w->pos = BBPOS(iter->btree_id, iter->pos);
+		queue_work(c->write_ref_wq, &w->work);
+		goto out;
+	}
 }
 
 static int bch2_check_discard_freespace_key_fsck(struct btree_trans *trans, struct btree_iter *iter)
 {
 	u8 gen;
-	int ret = bch2_check_discard_freespace_key(trans, iter, &gen);
+	int ret = bch2_check_discard_freespace_key(trans, iter, &gen, false);
 	return ret < 0 ? ret : 0;
 }
 
diff --git a/fs/bcachefs/alloc_background.h b/fs/bcachefs/alloc_background.h
index 8cacddd188f4..de25ba4ee94b 100644
--- a/fs/bcachefs/alloc_background.h
+++ b/fs/bcachefs/alloc_background.h
@@ -310,7 +310,7 @@ int bch2_trigger_alloc(struct btree_trans *, enum btree_id, unsigned,
 		       struct bkey_s_c, struct bkey_s,
 		       enum btree_iter_update_trigger_flags);
 
-int bch2_check_discard_freespace_key(struct btree_trans *, struct btree_iter *, u8 *);
+int bch2_check_discard_freespace_key(struct btree_trans *, struct btree_iter *, u8 *, bool);
 int bch2_check_alloc_info(struct bch_fs *);
 int bch2_check_alloc_to_lru_refs(struct bch_fs *);
 void bch2_dev_do_discards(struct bch_dev *);
diff --git a/fs/bcachefs/alloc_foreground.c b/fs/bcachefs/alloc_foreground.c
index 4d1ff7f1f302..c40a76df76b8 100644
--- a/fs/bcachefs/alloc_foreground.c
+++ b/fs/bcachefs/alloc_foreground.c
@@ -281,7 +281,7 @@ static struct open_bucket *try_alloc_bucket(struct btree_trans *trans, struct bc
 	u64 b = freespace_iter->pos.offset & ~(~0ULL << 56);
 	u8 gen;
 
-	int ret = bch2_check_discard_freespace_key(trans, freespace_iter, &gen);
+	int ret = bch2_check_discard_freespace_key(trans, freespace_iter, &gen, true);
 	if (ret < 0)
 		return ERR_PTR(ret);
 	if (ret)
diff --git a/fs/bcachefs/bcachefs.h b/fs/bcachefs/bcachefs.h
index d88129503bc5..c16937e54734 100644
--- a/fs/bcachefs/bcachefs.h
+++ b/fs/bcachefs/bcachefs.h
@@ -680,6 +680,7 @@ struct btree_trans_buf {
 	x(dio_write)							\
 	x(discard)							\
 	x(discard_fast)							\
+	x(check_discard_freespace_key)					\
 	x(invalidate)							\
 	x(delete_dead_snapshots)					\
 	x(gc_gens)							\
-- 
2.45.2


      parent reply	other threads:[~2024-11-29 20:28 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-29 20:26 [PATCH 00/34] a whole raft of bugfixes Kent Overstreet
2024-11-29 20:27 ` [PATCH 01/34] bcachefs: BCH_ERR_btree_node_read_error_cached Kent Overstreet
2024-11-29 20:27 ` [PATCH 02/34] bcachefs: Use separate rhltable for bch2_inode_or_descendents_is_open() Kent Overstreet
2024-11-29 20:27 ` [PATCH 03/34] bcachefs: errcode cleanup: journal errors Kent Overstreet
2024-11-29 20:27 ` [PATCH 04/34] bcachefs: disk_accounting: bch2_dev_rcu -> bch2_dev_rcu_noerror Kent Overstreet
2024-11-29 20:27 ` [PATCH 05/34] bcachefs: Fix accounting_read when we rewind Kent Overstreet
2024-11-29 20:27 ` [PATCH 06/34] bcachefs: backpointer_to_missing_ptr is now autofix Kent Overstreet
2024-11-29 20:27 ` [PATCH 07/34] bcachefs: Fix btree node scan when unknown btree IDs are present Kent Overstreet
2024-11-29 20:27 ` [PATCH 08/34] bcachefs: Kill bch2_bucket_alloc_new_fs() Kent Overstreet
2024-11-29 20:27 ` [PATCH 09/34] bcachefs: Bad btree roots are now autofix Kent Overstreet
2024-11-29 20:27 ` [PATCH 10/34] bcachefs: Fix dup/misordered check in btree node read Kent Overstreet
2024-11-29 20:27 ` [PATCH 11/34] bcachefs: Don't try to en/decrypt when encryption not available Kent Overstreet
2024-11-29 20:27 ` [PATCH 12/34] bcachefs: Change "disk accounting version 0" check to commit only Kent Overstreet
2024-11-29 20:27 ` [PATCH 13/34] bcachefs: Fix bch2_btree_node_update_key_early() Kent Overstreet
2024-11-29 20:27 ` [PATCH 14/34] bcachefs: Go RW earlier, for normal rw mount Kent Overstreet
2024-11-29 20:27 ` [PATCH 15/34] bcachefs: Fix null ptr deref in btree_path_lock_root() Kent Overstreet
2024-11-29 20:27 ` [PATCH 16/34] bcachefs: Ignore empty btree root journal entries Kent Overstreet
2024-11-29 20:27 ` [PATCH 17/34] bcachefs: struct bkey_validate_context Kent Overstreet
2024-11-29 20:27 ` [PATCH 18/34] bcachefs: Make topology errors autofix Kent Overstreet
2024-11-29 20:27 ` [PATCH 19/34] bcachefs: BCH_FS_recovery_running Kent Overstreet
2024-11-29 20:27 ` [PATCH 20/34] bcachefs: dio write: Take ref on mm_struct when using asynchronously Kent Overstreet
2024-11-29 22:38   ` Jens Axboe
2024-12-05  1:55   ` Kent Overstreet
2024-11-29 20:27 ` [PATCH 21/34] bcachefs: Guard against journal seq overflow Kent Overstreet
2024-11-29 20:27 ` [PATCH 22/34] bcachefs: Issue a transaction restart after commit in repair Kent Overstreet
2024-11-29 20:27 ` [PATCH 23/34] bcachefs: Guard against backpointers to unknown btrees Kent Overstreet
2024-11-29 20:27 ` [PATCH 24/34] bcachefs: Fix journal_iter list corruption Kent Overstreet
2024-11-29 20:27 ` [PATCH 25/34] bcachefs: add missing printbuf_reset() Kent Overstreet
2024-11-29 20:27 ` [PATCH 26/34] bcachefs: mark more errors AUTOFIX Kent Overstreet
2024-11-29 20:27 ` [PATCH 27/34] bcachefs: Don't error out when logging fsck error Kent Overstreet
2024-11-29 20:27 ` [PATCH 28/34] bcachefs: do_fsck_ask_yn() Kent Overstreet
2024-11-29 20:27 ` [PATCH 29/34] bcachefs: Check for bucket journal seq in the future Kent Overstreet
2024-11-29 20:27 ` [PATCH 30/34] bcachefs: Check for inode " Kent Overstreet
2024-11-29 20:27 ` [PATCH 31/34] bcachefs: cryptographic MACs on superblock are not (yet?) supported Kent Overstreet
2024-11-29 20:27 ` [PATCH 32/34] bcachefs: bch2_trans_relock() is trylock for lockdep Kent Overstreet
2024-11-29 20:27 ` [PATCH 33/34] bcachefs: Check for extent crc uncompressed/compressed size mismatch Kent Overstreet
2024-11-29 20:27 ` Kent Overstreet [this message]

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=20241129202736.2713679-35-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