Linux NFS development
 help / color / mirror / Atom feed
* [PATCH 0/2] Ignore SB_RDONLY when mounting and remounting nfs
@ 2025-03-04 13:05 Li Lingfeng
  2025-03-04 13:05 ` [PATCH 1/2] nfs: clear SB_RDONLY before getting superblock Li Lingfeng
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Li Lingfeng @ 2025-03-04 13:05 UTC (permalink / raw)
  To: trondmy, anna
  Cc: linux-nfs, linux-kernel, ehagberg, linux-nfs, yukuai1, houtao1,
	yi.zhang, yangerkun, lilingfeng, lilingfeng3

When NFS_MOUNT_UNSHARED is not set, NFS forces the sharing of a
superblock among each filesystem that mounts sub-directories elonging to
a single exported root path.

To prevent interference between different filesystems, ignore SB_RDONLY
when mounting and remounting nfs.

Li Lingfeng (2):
  nfs: clear SB_RDONLY before getting superblock
  nfs: ignore SB_RDONLY when remounting nfs

 fs/nfs/super.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

-- 
2.31.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] nfs: clear SB_RDONLY before getting superblock
  2025-03-04 13:05 [PATCH 0/2] Ignore SB_RDONLY when mounting and remounting nfs Li Lingfeng
@ 2025-03-04 13:05 ` Li Lingfeng
  2025-03-04 16:22   ` Eric Hagberg
  2025-03-04 13:05 ` [PATCH 2/2] nfs: ignore SB_RDONLY when remounting nfs Li Lingfeng
  2025-04-09 11:39 ` [PATCH 0/2] Ignore SB_RDONLY when mounting and " Li Lingfeng
  2 siblings, 1 reply; 5+ messages in thread
From: Li Lingfeng @ 2025-03-04 13:05 UTC (permalink / raw)
  To: trondmy, anna
  Cc: linux-nfs, linux-kernel, ehagberg, linux-nfs, yukuai1, houtao1,
	yi.zhang, yangerkun, lilingfeng, lilingfeng3

As described in the link, commit 52cb7f8f1778 ("nfs: ignore SB_RDONLY when
mounting nfs") removed the check for the ro flag when determining whether
to share the superblock, which caused issues when mounting different
subdirectories under the same export directory via NFSv3. However, this
change did not affect NFSv4.

For NFSv3:
1) A single superblock is created for the initial mount.
2) When mounted read-only, this superblock carries the SB_RDONLY flag.
3) Before commit 52cb7f8f1778 ("nfs: ignore SB_RDONLY when mounting nfs"):
Subsequent rw mounts would not share the existing ro superblock due to
flag mismatch, creating a new superblock without SB_RDONLY.
After the commit:
  The SB_RDONLY flag is ignored during superblock comparison, and this leads
  to sharing the existing superblock even for rw mounts.
  Ultimately results in write operations being rejected at the VFS layer.

For NFSv4:
1) Multiple superblocks are created and the last one will be kept.
2) The actually used superblock for ro mounts doesn't carry SB_RDONLY flag.
Therefore, commit 52cb7f8f1778 doesn't affect NFSv4 mounts.

Clear SB_RDONLY before getting superblock when NFS_MOUNT_UNSHARED is not
set to fix it.

Fixes: 52cb7f8f1778 ("nfs: ignore SB_RDONLY when mounting nfs")
Closes: https://lore.kernel.org/all/12d7ea53-1202-4e21-a7ef-431c94758ce5@app.fastmail.com/T/
Signed-off-by: Li Lingfeng <lilingfeng3@huawei.com>
---
 fs/nfs/super.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/fs/nfs/super.c b/fs/nfs/super.c
index aeb715b4a690..3e5528c2c822 100644
--- a/fs/nfs/super.c
+++ b/fs/nfs/super.c
@@ -1304,8 +1304,17 @@ int nfs_get_tree_common(struct fs_context *fc)
 	if (IS_ERR(server))
 		return PTR_ERR(server);
 
+	/*
+	 * When NFS_MOUNT_UNSHARED is not set, NFS forces the sharing of a
+	 * superblock among each filesystem that mounts sub-directories
+	 * belonging to a single exported root path.
+	 * To prevent interference between different filesystems, the
+	 * SB_RDONLY flag should be removed from the superblock.
+	 */
 	if (server->flags & NFS_MOUNT_UNSHARED)
 		compare_super = NULL;
+	else
+		fc->sb_flags &= ~SB_RDONLY;
 
 	/* -o noac implies -o sync */
 	if (server->flags & NFS_MOUNT_NOAC)
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] nfs: ignore SB_RDONLY when remounting nfs
  2025-03-04 13:05 [PATCH 0/2] Ignore SB_RDONLY when mounting and remounting nfs Li Lingfeng
  2025-03-04 13:05 ` [PATCH 1/2] nfs: clear SB_RDONLY before getting superblock Li Lingfeng
@ 2025-03-04 13:05 ` Li Lingfeng
  2025-04-09 11:39 ` [PATCH 0/2] Ignore SB_RDONLY when mounting and " Li Lingfeng
  2 siblings, 0 replies; 5+ messages in thread
From: Li Lingfeng @ 2025-03-04 13:05 UTC (permalink / raw)
  To: trondmy, anna
  Cc: linux-nfs, linux-kernel, ehagberg, linux-nfs, yukuai1, houtao1,
	yi.zhang, yangerkun, lilingfeng, lilingfeng3

In some scenarios, when mounting NFS, more than one superblock may be
created. The final superblock used is the last one created, but only the
first superblock carries the ro flag passed from user space. If a ro flag
is added to the superblock via remount, it will trigger the issue
described in Link[1].

Link[2] attempted to address this by marking the superblock as ro during
the initial mount. However, this introduced a new problem in scenarios
where multiple mount points share the same superblock:
[root@a ~]# mount /dev/sdb /mnt/sdb
[root@a ~]# echo "/mnt/sdb *(rw,no_root_squash)" > /etc/exports
[root@a ~]# echo "/mnt/sdb/test_dir2 *(ro,no_root_squash)" >> /etc/exports
[root@a ~]# systemctl restart nfs-server
[root@a ~]# mount -t nfs -o rw 127.0.0.1:/mnt/sdb/test_dir1 /mnt/test_mp1
[root@a ~]# mount | grep nfs4
127.0.0.1:/mnt/sdb/test_dir1 on /mnt/test_mp1 type nfs4 (rw,relatime,...
[root@a ~]# mount -t nfs -o ro 127.0.0.1:/mnt/sdb/test_dir2 /mnt/test_mp2
[root@a ~]# mount | grep nfs4
127.0.0.1:/mnt/sdb/test_dir1 on /mnt/test_mp1 type nfs4 (ro,relatime,...
127.0.0.1:/mnt/sdb/test_dir2 on /mnt/test_mp2 type nfs4 (ro,relatime,...
[root@a ~]#

When mounting the second NFS, the shared superblock is marked as ro,
causing the previous NFS mount to become read-only.

To resolve both issues, the ro flag is no longer applied to the superblock
during remount. Instead, the ro flag on the mount is used to control
whether the mount point is read-only.

Fixes: 281cad46b34d ("NFS: Create a submount rpc_op")
Link[1]: https://lore.kernel.org/all/20240604112636.236517-3-lilingfeng@huaweicloud.com/
Link[2]: https://lore.kernel.org/all/20241130035818.1459775-1-lilingfeng3@huawei.com/
Signed-off-by: Li Lingfeng <lilingfeng3@huawei.com>
---
 fs/nfs/super.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/fs/nfs/super.c b/fs/nfs/super.c
index 3e5528c2c822..8f50447eb5d0 100644
--- a/fs/nfs/super.c
+++ b/fs/nfs/super.c
@@ -1047,6 +1047,16 @@ int nfs_reconfigure(struct fs_context *fc)
 
 	sync_filesystem(sb);
 
+	/*
+	 * The SB_RDONLY flag has been removed from the superblock during
+	 * mounts to prevent interference between different filesystems.
+	 * Similarly, it is also necessary to ignore the SB_RDONLY flag
+	 * during reconfiguration; otherwise, it may also result in the
+	 * creation of redundant superblocks when mounting a directory with
+	 * different rw and ro flags multiple times.
+	 */
+	fc->sb_flags_mask &= ~SB_RDONLY;
+
 	/*
 	 * Userspace mount programs that send binary options generally send
 	 * them populated with default values. We have no way to know which
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] nfs: clear SB_RDONLY before getting superblock
  2025-03-04 13:05 ` [PATCH 1/2] nfs: clear SB_RDONLY before getting superblock Li Lingfeng
@ 2025-03-04 16:22   ` Eric Hagberg
  0 siblings, 0 replies; 5+ messages in thread
From: Eric Hagberg @ 2025-03-04 16:22 UTC (permalink / raw)
  To: Li Lingfeng
  Cc: trondmy, anna, linux-nfs, linux-kernel, linux-nfs, yukuai1,
	houtao1, yi.zhang, yangerkun, lilingfeng

BTW - I tested applying both patches and the second one did fix the
issue reported in
https://lore.kernel.org/all/12d7ea53-1202-4e21-a7ef-431c94758ce5@app.fastmail.com/T/


On Tue, Mar 4, 2025 at 7:48 AM Li Lingfeng <lilingfeng3@huawei.com> wrote:
>
> As described in the link, commit 52cb7f8f1778 ("nfs: ignore SB_RDONLY when
> mounting nfs") removed the check for the ro flag when determining whether
> to share the superblock, which caused issues when mounting different
> subdirectories under the same export directory via NFSv3. However, this
> change did not affect NFSv4.
>
> For NFSv3:
> 1) A single superblock is created for the initial mount.
> 2) When mounted read-only, this superblock carries the SB_RDONLY flag.
> 3) Before commit 52cb7f8f1778 ("nfs: ignore SB_RDONLY when mounting nfs"):
> Subsequent rw mounts would not share the existing ro superblock due to
> flag mismatch, creating a new superblock without SB_RDONLY.
> After the commit:
>   The SB_RDONLY flag is ignored during superblock comparison, and this leads
>   to sharing the existing superblock even for rw mounts.
>   Ultimately results in write operations being rejected at the VFS layer.
>
> For NFSv4:
> 1) Multiple superblocks are created and the last one will be kept.
> 2) The actually used superblock for ro mounts doesn't carry SB_RDONLY flag.
> Therefore, commit 52cb7f8f1778 doesn't affect NFSv4 mounts.
>
> Clear SB_RDONLY before getting superblock when NFS_MOUNT_UNSHARED is not
> set to fix it.
>
> Fixes: 52cb7f8f1778 ("nfs: ignore SB_RDONLY when mounting nfs")
> Closes: https://lore.kernel.org/all/12d7ea53-1202-4e21-a7ef-431c94758ce5@app.fastmail.com/T/
> Signed-off-by: Li Lingfeng <lilingfeng3@huawei.com>
> ---
>  fs/nfs/super.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/fs/nfs/super.c b/fs/nfs/super.c
> index aeb715b4a690..3e5528c2c822 100644
> --- a/fs/nfs/super.c
> +++ b/fs/nfs/super.c
> @@ -1304,8 +1304,17 @@ int nfs_get_tree_common(struct fs_context *fc)
>         if (IS_ERR(server))
>                 return PTR_ERR(server);
>
> +       /*
> +        * When NFS_MOUNT_UNSHARED is not set, NFS forces the sharing of a
> +        * superblock among each filesystem that mounts sub-directories
> +        * belonging to a single exported root path.
> +        * To prevent interference between different filesystems, the
> +        * SB_RDONLY flag should be removed from the superblock.
> +        */
>         if (server->flags & NFS_MOUNT_UNSHARED)
>                 compare_super = NULL;
> +       else
> +               fc->sb_flags &= ~SB_RDONLY;
>
>         /* -o noac implies -o sync */
>         if (server->flags & NFS_MOUNT_NOAC)
> --
> 2.31.1
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/2] Ignore SB_RDONLY when mounting and remounting nfs
  2025-03-04 13:05 [PATCH 0/2] Ignore SB_RDONLY when mounting and remounting nfs Li Lingfeng
  2025-03-04 13:05 ` [PATCH 1/2] nfs: clear SB_RDONLY before getting superblock Li Lingfeng
  2025-03-04 13:05 ` [PATCH 2/2] nfs: ignore SB_RDONLY when remounting nfs Li Lingfeng
@ 2025-04-09 11:39 ` Li Lingfeng
  2 siblings, 0 replies; 5+ messages in thread
From: Li Lingfeng @ 2025-04-09 11:39 UTC (permalink / raw)
  To: trondmy, anna
  Cc: linux-nfs, linux-kernel, ehagberg, linux-nfs, yukuai1, houtao1,
	yi.zhang, yangerkun, lilingfeng

Friendly ping..

Thanks

在 2025/3/4 21:05, Li Lingfeng 写道:
> When NFS_MOUNT_UNSHARED is not set, NFS forces the sharing of a
> superblock among each filesystem that mounts sub-directories elonging to
> a single exported root path.
>
> To prevent interference between different filesystems, ignore SB_RDONLY
> when mounting and remounting nfs.
>
> Li Lingfeng (2):
>    nfs: clear SB_RDONLY before getting superblock
>    nfs: ignore SB_RDONLY when remounting nfs
>
>   fs/nfs/super.c | 19 +++++++++++++++++++
>   1 file changed, 19 insertions(+)
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-04-09 11:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-04 13:05 [PATCH 0/2] Ignore SB_RDONLY when mounting and remounting nfs Li Lingfeng
2025-03-04 13:05 ` [PATCH 1/2] nfs: clear SB_RDONLY before getting superblock Li Lingfeng
2025-03-04 16:22   ` Eric Hagberg
2025-03-04 13:05 ` [PATCH 2/2] nfs: ignore SB_RDONLY when remounting nfs Li Lingfeng
2025-04-09 11:39 ` [PATCH 0/2] Ignore SB_RDONLY when mounting and " Li Lingfeng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox