[PATCH v11 02/16] xfs: only call xfs_setsize_buftarg once per buffer target

John Garry
2025-05-04T09:00:13Z

From: "Darrick J. Wong" <djwong@kernel.org>

It's silly to call xfs_setsize_buftarg from xfs_alloc_buftarg with the
block device LBA size because we don't need to ask the block layer to
validate a geometry number that it provided us. Instead, set the
preliminary bt_meta_sector* fields to the LBA size in preparation for
reading the primary super.

However, we still want to flush and invalidate the pagecache for all
three block devices before we start reading metadata from those devices.
Move the sync_blockdev calls into a separate helper function, and call
it immediately after xfs_open_devices creates the buftargs.

This will enable a subsequent patch to validate hw atomic write geometry
against the filesystem geometry.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: John Garry <john.g.garry@oracle.com>
Signed-off-by: John Garry <john.g.garry@oracle.com>
---
fs/xfs/xfs_buf.c | 14 +++++---------
fs/xfs/xfs_buf.h | 5 +++++
fs/xfs/xfs_super.c | 33 +++++++++++++++++++++++++++++++++
3 files changed, 43 insertions(+), 9 deletions(-)

diff --git a/fs/xfs/xfs_buf.c b/fs/xfs/xfs_buf.c
index 5ae77ffdc947..292891d6ff69 100644
--- a/fs/xfs/xfs_buf.c
+++ b/fs/xfs/xfs_buf.c
@@ -1733,11 +1733,7 @@ xfs_setsize_buftarg(
return -EINVAL;
}

- /*
- * Flush the block device pagecache so our bios see anything dirtied
- * before mount.
- */
- return sync_blockdev(btp->bt_bdev);
+ return 0;
}

int
@@ -1810,10 +1806,10 @@ xfs_alloc_buftarg(
* When allocating the buftargs we have not yet read the super block and
* thus don't know the file system sector size yet.
*/
- if (xfs_setsize_buftarg(btp, bdev_logical_block_size(btp->bt_bdev)))
- goto error_free;
- if (xfs_init_buftarg(btp, bdev_logical_block_size(btp->bt_bdev),
- mp->m_super->s_id))
+ btp->bt_meta_sectorsize = bdev_logical_block_size(btp->bt_bdev);
+ btp->bt_meta_sectormask = btp->bt_meta_sectorsize - 1;
+
+ if (xfs_init_buftarg(btp, btp->bt_meta_sectorsize, mp->m_super->s_id))
goto error_free;

return btp;
diff --git a/fs/xfs/xfs_buf.h b/fs/xfs/xfs_buf.h
index d0b065a9a9f0..132210705602 100644
--- a/fs/xfs/xfs_buf.h
+++ b/fs/xfs/xfs_buf.h
@@ -383,6 +383,11 @@ int xfs_buf_reverify(struct xfs_buf *bp, const struct xfs_buf_ops *ops);
bool xfs_verify_magic(struct xfs_buf *bp, __be32 dmagic);
bool xfs_verify_magic16(struct xfs_buf *bp, __be16 dmagic);

+static inline int xfs_buftarg_sync(struct xfs_buftarg *btp)
+{
+ return sync_blockdev(btp->bt_bdev);
+}
+
/* for xfs_buf_mem.c only: */
int xfs_init_buftarg(struct xfs_buftarg *btp, size_t logical_sectorsize,
const char *descr);
diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c
index 5e456a6073ca..45e188466e51 100644
--- a/fs/xfs/xfs_super.c
+++ b/fs/xfs/xfs_super.c
@@ -520,6 +520,35 @@ xfs_open_devices(
return error;
}

+/*
+ * Flush and invalidate all devices' pagecaches before reading any metadata
+ * because XFS doesn't use the bdev pagecache.
+ */
+STATIC int
+xfs_preflush_devices(
+ struct xfs_mount *mp)
+{
+ int error;
+
+ error = xfs_buftarg_sync(mp->m_ddev_targp);
+ if (error)
+ return error;
+
+ if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
+ error = xfs_buftarg_sync(mp->m_ddev_targp);
+ if (error)
+ return error;
+ }
+
+ if (mp->m_rtdev_targp) {
+ error = xfs_buftarg_sync(mp->m_rtdev_targp);
+ if (error)
+ return error;
+ }
+
+ return 0;
+}
+
/*
* Setup xfs_mount buffer target pointers based on superblock
*/
@@ -1672,6 +1701,10 @@ xfs_fs_fill_super(
if (error)
return error;

+ error = xfs_preflush_devices(mp);
+ if (error)
+ goto out_shutdown_devices;
+
if (xfs_debugfs) {
mp->m_debugfs = xfs_debugfs_mkdir(mp->m_super->s_id,
xfs_debugfs);
--
2.31.1