Linux XFS filesystem development
 help / color / mirror / Atom feed
From: Dave Chinner <dgc@kernel.org>
To: linux-xfs@vger.kernel.org
Cc: cem@kernel.org
Subject: [PATCH 10/59] xfs: check the atomic write reservation against the current log size
Date: Tue, 17 Nov 2026 10:14:27 +1100	[thread overview]
Message-ID: <20261116231707.3625263-11-dgc@kernel.org> (raw)
In-Reply-To: <20261116231707.3625263-1-dgc@kernel.org>

Changing max_atomic_write on remount can disturb transactions running on the
filesystem. The new reservation is checked by computing the minimum log size
for the whole reservation table. That calculation temporarily changes the
mount-wide rmap btree height and resets the live atomic ioend reservation to
its default. It is only safe at mkfs and mount time, but remount runs it on a
live filesystem. A transaction started in that window can see the wrong btree
height or get the wrong atomic ioend reservation.

Mount has already checked every other reservation against the log. Only the
new atomic ioend reservation needs checking. Invert the minimum log size
calculation to give the largest reservation that the current log can hold. It
reads only the superblock log geometry, so it is safe on a live filesystem.
The atomic ioend reservation is now only updated once the new value is known
to fit.

xfs_calc_atomic_write_log_geometry() is unchanged and is still used by mkfs to
size the log.

Fixes: 4528b9052731 ("xfs: allow sysadmins to specify a maximum atomic write limit at mount time")
Assisted-by: LLM
Signed-off-by: Dave Chinner <dgc@kernel.org>
---
 fs/xfs/libxfs/xfs_log_rlimit.c | 45 ++++++++++++++++++++++++++++++++++
 fs/xfs/libxfs/xfs_shared.h     |  1 +
 fs/xfs/libxfs/xfs_trans_resv.c | 45 ++++++++++++++++++++++++++++------
 fs/xfs/xfs_trace.h             | 34 +++++++++++++++++++++++++
 4 files changed, 118 insertions(+), 7 deletions(-)

diff --git a/fs/xfs/libxfs/xfs_log_rlimit.c b/fs/xfs/libxfs/xfs_log_rlimit.c
index 37712b2f8757..685d4797913e 100644
--- a/fs/xfs/libxfs/xfs_log_rlimit.c
+++ b/fs/xfs/libxfs/xfs_log_rlimit.c
@@ -249,3 +249,48 @@ xfs_log_calc_minimum_size(
 
 	return XFS_BB_TO_FSB(mp, min_logblks);
 }
+
+/*
+ * Calculate the largest transaction reservation, in bytes, that fits in the
+ * current log. The reservation is a unit reservation from
+ * xfs_log_calc_unit_res() multiplied by the log count, as in
+ * xfs_log_calc_minimum_size().
+ *
+ * This is the inverse of xfs_log_calc_minimum_size(): a reservation fits in the
+ * log if, and only if, it is no larger than the value returned here. The two
+ * functions must be kept in step.
+ *
+ * xfs_log_calc_minimum_size() temporarily changes mount state to calculate the
+ * reservations, so it can only be used at mkfs and mount time. This function
+ * only reads the superblock log geometry, so it is safe to use on a live
+ * filesystem.
+ */
+int
+xfs_log_calc_max_res(
+	struct xfs_mount	*mp)
+{
+	int			logbbs;
+	int			lsunit = 0;
+
+	/* The superblock verifier limits the log to XFS_MAX_LOG_BYTES. */
+	logbbs = (int)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
+	logbbs /= XFS_MIN_LOG_FACTOR;
+
+	if (xfs_has_logv2(mp) && mp->m_sb.sb_logsunit > 1)
+		lsunit = BTOBB(mp->m_sb.sb_logsunit);
+
+	if (lsunit) {
+		logbbs -= 2 * lsunit;
+		if (logbbs > 0)
+			logbbs = rounddown(logbbs, lsunit);
+	} else {
+		logbbs -= 2 * BBSIZE;
+	}
+
+	/* Mount has already checked that the log fits its reservations. */
+	ASSERT(logbbs > 0);
+	if (logbbs <= 0)
+		return 0;
+	ASSERT(logbbs < BTOBB(XFS_MAX_LOG_BYTES) / 2);
+	return BBTOB(logbbs);
+}
diff --git a/fs/xfs/libxfs/xfs_shared.h b/fs/xfs/libxfs/xfs_shared.h
index b1e0d9bc1f7d..ced25294b57c 100644
--- a/fs/xfs/libxfs/xfs_shared.h
+++ b/fs/xfs/libxfs/xfs_shared.h
@@ -124,6 +124,7 @@ static inline bool xfs_btree_is_rtrefcount(const struct xfs_btree_ops *ops)
 /* log size calculation functions */
 int	xfs_log_calc_unit_res(struct xfs_mount *mp, int unit_bytes);
 int	xfs_log_calc_minimum_size(struct xfs_mount *);
+int	xfs_log_calc_max_res(struct xfs_mount *mp);
 
 struct xfs_trans_res;
 void	xfs_log_get_max_trans_res(struct xfs_mount *mp,
diff --git a/fs/xfs/libxfs/xfs_trans_resv.c b/fs/xfs/libxfs/xfs_trans_resv.c
index fadbed82cf07..372b0640357c 100644
--- a/fs/xfs/libxfs/xfs_trans_resv.c
+++ b/fs/xfs/libxfs/xfs_trans_resv.c
@@ -1521,6 +1521,10 @@ xfs_calc_max_atomic_write_fsblocks(
  * Compute the log blocks and transaction reservation needed to complete an
  * atomic write of a given number of blocks.  Worst case, each block requires
  * separate handling.  A return value of 0 means something went wrong.
+ *
+ * This is for mkfs to size the log. It temporarily modifies the mount's
+ * transaction reservations and btree geometry, so it must not be used on a
+ * live filesystem.
  */
 xfs_extlen_t
 xfs_calc_atomic_write_log_geometry(
@@ -1559,29 +1563,56 @@ xfs_calc_atomic_write_log_geometry(
 /*
  * Compute the transaction reservation needed to complete an out of place
  * atomic write of a given number of blocks.
+ *
+ * This is called at mount and remount time, so it must not modify any mount
+ * state other than the atomic ioend reservation. The new reservation is
+ * checked against the largest reservation the current log can hold, and the
+ * atomic ioend reservation is only updated once it is known to fit.
  */
 int
 xfs_calc_atomic_write_reservation(
 	struct xfs_mount	*mp,
 	xfs_extlen_t		blockcount)
 {
-	unsigned int		new_logres;
-	xfs_extlen_t		min_logblocks;
+	struct xfs_trans_resv	*resp = M_RES(mp);
+	struct xfs_trans_res	tres;
+	unsigned int		per_intent, step_size;
+	unsigned int		logres;
+	uint64_t		need_res = 0;
+	int			max_res;
 
 	/*
 	 * If the caller doesn't ask for a specific atomic write size, then
 	 * use the defaults.
 	 */
 	if (blockcount == 0) {
-		xfs_calc_default_atomic_ioend_reservation(M_RES(mp));
+		xfs_calc_default_atomic_ioend_reservation(resp);
 		return 0;
 	}
 
-	min_logblocks = xfs_calc_atomic_write_log_geometry(mp, blockcount,
-			&new_logres);
-	if (!min_logblocks || min_logblocks > mp->m_sb.sb_logblocks)
+	per_intent = xfs_calc_atomic_write_ioend_geometry(mp, &step_size);
+
+	/* Check for overflows */
+	if (check_mul_overflow(blockcount, per_intent, &logres) ||
+	    check_add_overflow(logres, step_size, &logres) ||
+	    logres > INT_MAX)
+		return -EINVAL;
+
+	/* The atomic ioend reservation is based on the default. */
+	tres = resp->tr_itruncate;
+	tres.tr_logres = logres;
+
+	need_res = xfs_log_calc_unit_res(mp, tres.tr_logres);
+	if (tres.tr_logcount > 1)
+		need_res *= tres.tr_logcount;
+	max_res = xfs_log_calc_max_res(mp);
+
+	trace_xfs_calc_atomic_write_reservation(mp, per_intent, step_size,
+			blockcount, logres, need_res, max_res);
+
+	if (need_res > max_res)
 		return -EINVAL;
 
-	M_RES(mp)->tr_atomic_ioend.tr_logres = new_logres;
+	resp->tr_atomic_ioend = tres;
 	return 0;
 }
diff --git a/fs/xfs/xfs_trace.h b/fs/xfs/xfs_trace.h
index 28d49f158b22..0e195a0835ec 100644
--- a/fs/xfs/xfs_trace.h
+++ b/fs/xfs/xfs_trace.h
@@ -264,6 +264,40 @@ TRACE_EVENT(xfs_calc_max_atomic_write_log_geometry,
 		  __entry->logres)
 );
 
+TRACE_EVENT(xfs_calc_atomic_write_reservation,
+	TP_PROTO(struct xfs_mount *mp, unsigned int per_intent,
+		 unsigned int step_size, unsigned int blockcount,
+		 unsigned int logres, uint64_t need_res, int max_res),
+	TP_ARGS(mp, per_intent, step_size, blockcount, logres, need_res,
+		max_res),
+	TP_STRUCT__entry(
+		__field(dev_t, dev)
+		__field(unsigned int, per_intent)
+		__field(unsigned int, step_size)
+		__field(unsigned int, blockcount)
+		__field(unsigned int, logres)
+		__field(uint64_t, need_res)
+		__field(int, max_res)
+	),
+	TP_fast_assign(
+		__entry->dev = mp->m_super->s_dev;
+		__entry->per_intent = per_intent;
+		__entry->step_size = step_size;
+		__entry->blockcount = blockcount;
+		__entry->logres = logres;
+		__entry->need_res = need_res;
+		__entry->max_res = max_res;
+	),
+	TP_printk("dev %d:%d per_intent %u step_size %u blockcount %u logres %u need_res %llu max_res %d",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->per_intent,
+		  __entry->step_size,
+		  __entry->blockcount,
+		  __entry->logres,
+		  __entry->need_res,
+		  __entry->max_res)
+);
+
 TRACE_EVENT(xlog_intent_recovery_failed,
 	TP_PROTO(struct xfs_mount *mp, const struct xfs_defer_op_type *ops,
 		 int error),
-- 
2.55.0


  parent reply	other threads:[~2026-11-16 23:17 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 23:14 [V2 PATCH 00/59] Validate journal log items during log recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 01/59] xfs: set the unmount record operation header length Dave Chinner
2026-11-16 23:14 ` [PATCH 02/59] xfs: fix the rt rmap btree maximum level checks Dave Chinner
2026-11-16 23:14 ` [PATCH 03/59] xfs: allow an empty rt refcount btree root before the rt volume exists Dave Chinner
2026-11-16 23:14 ` [PATCH 04/59] xfs: recompute the rt btree maxlevels when growfs changes them Dave Chinner
2026-11-16 23:14 ` [PATCH 05/59] xfs: recompute the transaction reservations after growfs Dave Chinner
2026-11-16 23:14 ` [PATCH 06/59] xfs: skip missing groups when freeing them Dave Chinner
2026-11-16 23:14 ` [PATCH 07/59] xfs: update the rt geometry when recovering a superblock change Dave Chinner
2026-11-16 23:14 ` [PATCH 08/59] xfs: reinitialise the filesystem geometry after log recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 09/59] xfs: apply mount superblock updates " Dave Chinner
2026-11-16 23:14 ` Dave Chinner [this message]
2026-11-16 23:14 ` [PATCH 11/59] xfs: fix the bmap btree maximum height calculation Dave Chinner
2026-11-16 23:14 ` [PATCH 12/59] xfs: add log recovery item validation design document Dave Chinner
2026-11-16 23:14 ` [PATCH 13/59] xfs: lift transaction header parsing out of the region assembly Dave Chinner
2026-11-16 23:14 ` [PATCH 14/59] xfs: track transaction header decode state explicitly Dave Chinner
2026-11-16 23:14 ` [PATCH 15/59] xfs: track the log item being rebuilt with an explicit pointer Dave Chinner
2026-11-16 23:14 ` [PATCH 16/59] xfs: complete log items explicitly during recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 17/59] xfs: add log item validation infrastructure to log recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 18/59] xfs: recover the transaction header as a validated item type Dave Chinner
2026-11-16 23:14 ` [PATCH 19/59] xfs: factor out xlog_recover_validate_ophdr() Dave Chinner
2026-11-16 23:14 ` [PATCH 20/59] xfs: validate operation header length and alignment during recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 21/59] xfs: validate the operation header padding " Dave Chinner
2026-11-16 23:14 ` [PATCH 22/59] xfs: validate the operation header flags " Dave Chinner
2026-11-16 23:14 ` [PATCH 23/59] xfs: enforce zero length control records " Dave Chinner
2026-11-16 23:14 ` [PATCH 24/59] xfs: validate the unmount record " Dave Chinner
2026-11-16 23:14 ` [PATCH 25/59] xfs: handle zero length continuation op headers " Dave Chinner
2026-11-16 23:14 ` [PATCH 26/59] xfs: bound the assembled region size " Dave Chinner
2026-11-16 23:14 ` [PATCH 27/59] xfs: dump the corrupt region when log item validation fails Dave Chinner
2026-11-16 23:14 ` [PATCH 28/59] xfs: validate inode create and quota off log items during recovery Dave Chinner
2026-11-16 23:14 ` [PATCH 29/59] xfs: validate extent free " Dave Chinner
2026-11-16 23:14 ` [PATCH 30/59] xfs: validate and convert extent free intents " Dave Chinner
2026-11-16 23:14 ` [PATCH 31/59] xfs: validate and convert extent free done items " Dave Chinner
2026-11-16 23:14 ` [PATCH 32/59] xfs: validate rmap update intents " Dave Chinner
2026-11-16 23:14 ` [PATCH 33/59] xfs: validate rmap update done items " Dave Chinner
2026-11-16 23:14 ` [PATCH 34/59] xfs: validate refcount update intents " Dave Chinner
2026-11-16 23:14 ` [PATCH 35/59] xfs: validate refcount update done items " Dave Chinner
2026-11-16 23:14 ` [PATCH 36/59] xfs: validate bmap update intents " Dave Chinner
2026-11-16 23:14 ` [PATCH 37/59] xfs: validate bmap update done items " Dave Chinner
2026-11-16 23:14 ` [PATCH 38/59] xfs: validate mapping exchange intents " Dave Chinner
2026-11-16 23:14 ` [PATCH 39/59] xfs: validate mapping exchange done items " Dave Chinner
2026-11-16 23:14 ` [PATCH 40/59] xfs: validate dquot " Dave Chinner
2026-11-16 23:14 ` [PATCH 41/59] xfs: validate attr done " Dave Chinner
2026-11-16 23:14 ` [PATCH 42/59] xfs: validate attr intents " Dave Chinner
2026-11-16 23:15 ` [PATCH 43/59] xfs: convert inode items to host format " Dave Chinner
2026-11-16 23:15 ` [PATCH 44/59] xfs: allow xfs_imap() to map without an inode btree lookup Dave Chinner
2026-11-16 23:15 ` [PATCH 45/59] xfs: validate the inode item format header during recovery Dave Chinner
2026-11-16 23:15 ` [PATCH 46/59] xfs: validate the logged inode core " Dave Chinner
2026-11-16 23:15 ` [PATCH 47/59] xfs: validate the logged inode forks " Dave Chinner
2026-11-16 23:15 ` [PATCH 48/59] xfs: validate logged local fork contents " Dave Chinner
2026-11-16 23:15 ` [PATCH 49/59] xfs: validate logged extent " Dave Chinner
2026-11-16 23:15 ` [PATCH 50/59] xfs: allow a long format btree root to be checked without a cursor Dave Chinner
2026-11-16 23:15 ` [PATCH 51/59] xfs: validate logged btree fork roots during recovery Dave Chinner
2026-11-16 23:15 ` [PATCH 52/59] xfs: validate logged metadata " Dave Chinner
2026-11-16 23:15 ` [PATCH 53/59] xfs: check logged btree fork roots fit in the inode fork " Dave Chinner
2026-11-16 23:15 ` [PATCH 54/59] xfs: validate buffer item region count " Dave Chinner
2026-11-16 23:15 ` [PATCH 55/59] xfs: validate buffer item regions " Dave Chinner
2026-11-16 23:15 ` [PATCH 56/59] xfs: validate the whole buffer item " Dave Chinner
2026-11-16 23:15 ` [PATCH 57/59] xfs: remove redundant buffer checks from recovery replay Dave Chinner
2026-11-16 23:15 ` [PATCH 58/59] xfs: fail recovery on a buffer type mismatch Dave Chinner
2026-11-16 23:15 ` [PATCH 59/59] xfs: validate logged dquot buffers during recovery Dave Chinner

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=20261116231707.3625263-11-dgc@kernel.org \
    --to=dgc@kernel.org \
    --cc=cem@kernel.org \
    --cc=linux-xfs@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