[PATCH v2] xfs: allow ro mounts if rtdev or logdev are read-only

Hans Holmberg posted 1 patch 9 months, 1 week ago
fs/xfs/xfs_super.c | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)
[PATCH v2] xfs: allow ro mounts if rtdev or logdev are read-only
Posted by Hans Holmberg 9 months, 1 week ago
Allow read-only mounts on rtdevs and logdevs that are marked as
read-only and make sure those mounts can't be remounted read-write.

Use the sb_open_mode helper to make sure that we don't try to open
devices with write access enabled for read-only mounts.

Signed-off-by: Hans Holmberg <hans.holmberg@wdc.com>
---

Changes since v1:
 - Switched to using the sb_open_mode helper that does exactly
   what we want.

 fs/xfs/xfs_super.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c
index b2dd0c0bf509..5e456a6073ca 100644
--- a/fs/xfs/xfs_super.c
+++ b/fs/xfs/xfs_super.c
@@ -380,10 +380,11 @@ xfs_blkdev_get(
 	struct file		**bdev_filep)
 {
 	int			error = 0;
+	blk_mode_t		mode;
 
-	*bdev_filep = bdev_file_open_by_path(name,
-		BLK_OPEN_READ | BLK_OPEN_WRITE | BLK_OPEN_RESTRICT_WRITES,
-		mp->m_super, &fs_holder_ops);
+	mode = sb_open_mode(mp->m_super->s_flags);
+	*bdev_filep = bdev_file_open_by_path(name, mode,
+			mp->m_super, &fs_holder_ops);
 	if (IS_ERR(*bdev_filep)) {
 		error = PTR_ERR(*bdev_filep);
 		*bdev_filep = NULL;
@@ -1969,6 +1970,20 @@ xfs_remount_rw(
 	struct xfs_sb		*sbp = &mp->m_sb;
 	int error;
 
+	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp &&
+	    bdev_read_only(mp->m_logdev_targp->bt_bdev)) {
+		xfs_warn(mp,
+			"ro->rw transition prohibited by read-only logdev");
+		return -EACCES;
+	}
+
+	if (mp->m_rtdev_targp &&
+	    bdev_read_only(mp->m_rtdev_targp->bt_bdev)) {
+		xfs_warn(mp,
+			"ro->rw transition prohibited by read-only rtdev");
+		return -EACCES;
+	}
+
 	if (xfs_has_norecovery(mp)) {
 		xfs_warn(mp,
 			"ro->rw transition prohibited on norecovery mount");
-- 
2.34.1
Re: [PATCH v2] xfs: allow ro mounts if rtdev or logdev are read-only
Posted by Carlos Maiolino 9 months, 1 week ago
On Wed, 30 Apr 2025 08:35:34 +0000, Hans Holmberg wrote:
> Allow read-only mounts on rtdevs and logdevs that are marked as
> read-only and make sure those mounts can't be remounted read-write.
> 
> Use the sb_open_mode helper to make sure that we don't try to open
> devices with write access enabled for read-only mounts.
> 
> 
> [...]

Applied to for-next, thanks!

[1/1] xfs: allow ro mounts if rtdev or logdev are read-only
      commit: bfecc4091e07a47696ac922783216d9e9ea46c97

Best regards,
-- 
Carlos Maiolino <cem@kernel.org>
Re: [PATCH v2] xfs: allow ro mounts if rtdev or logdev are read-only
Posted by hch 9 months, 1 week ago
On Wed, Apr 30, 2025 at 08:35:34AM +0000, Hans Holmberg wrote:
> +	blk_mode_t		mode;
>  
> -	*bdev_filep = bdev_file_open_by_path(name,
> -		BLK_OPEN_READ | BLK_OPEN_WRITE | BLK_OPEN_RESTRICT_WRITES,
> -		mp->m_super, &fs_holder_ops);
> +	mode = sb_open_mode(mp->m_super->s_flags);
> +	*bdev_filep = bdev_file_open_by_path(name, mode,
> +			mp->m_super, &fs_holder_ops);

We don't really need the local variable for mode now.

Otherwise looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>
Re: [PATCH v2] xfs: allow ro mounts if rtdev or logdev are read-only
Posted by Carlos Maiolino 9 months, 1 week ago
On Wed, Apr 30, 2025 at 08:35:34AM +0000, Hans Holmberg wrote:
> Allow read-only mounts on rtdevs and logdevs that are marked as
> read-only and make sure those mounts can't be remounted read-write.
> 
> Use the sb_open_mode helper to make sure that we don't try to open
> devices with write access enabled for read-only mounts.
> 
> Signed-off-by: Hans Holmberg <hans.holmberg@wdc.com>

This looks good, thanks Hans!

Reviewed-by: Carlos Maiolino <cmaiolino@redhat.com>

> ---
> 
> Changes since v1:
>  - Switched to using the sb_open_mode helper that does exactly
>    what we want.
> 
>  fs/xfs/xfs_super.c | 21 ++++++++++++++++++---
>  1 file changed, 18 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c
> index b2dd0c0bf509..5e456a6073ca 100644
> --- a/fs/xfs/xfs_super.c
> +++ b/fs/xfs/xfs_super.c
> @@ -380,10 +380,11 @@ xfs_blkdev_get(
>  	struct file		**bdev_filep)
>  {
>  	int			error = 0;
> +	blk_mode_t		mode;
> 
> -	*bdev_filep = bdev_file_open_by_path(name,
> -		BLK_OPEN_READ | BLK_OPEN_WRITE | BLK_OPEN_RESTRICT_WRITES,
> -		mp->m_super, &fs_holder_ops);
> +	mode = sb_open_mode(mp->m_super->s_flags);
> +	*bdev_filep = bdev_file_open_by_path(name, mode,
> +			mp->m_super, &fs_holder_ops);
>  	if (IS_ERR(*bdev_filep)) {
>  		error = PTR_ERR(*bdev_filep);
>  		*bdev_filep = NULL;
> @@ -1969,6 +1970,20 @@ xfs_remount_rw(
>  	struct xfs_sb		*sbp = &mp->m_sb;
>  	int error;
> 
> +	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp &&
> +	    bdev_read_only(mp->m_logdev_targp->bt_bdev)) {
> +		xfs_warn(mp,
> +			"ro->rw transition prohibited by read-only logdev");
> +		return -EACCES;
> +	}
> +
> +	if (mp->m_rtdev_targp &&
> +	    bdev_read_only(mp->m_rtdev_targp->bt_bdev)) {
> +		xfs_warn(mp,
> +			"ro->rw transition prohibited by read-only rtdev");
> +		return -EACCES;
> +	}
> +
>  	if (xfs_has_norecovery(mp)) {
>  		xfs_warn(mp,
>  			"ro->rw transition prohibited on norecovery mount");
> --
> 2.34.1
>