Skip to contentSkip to conversation

Ignore SB_RDONLY when mounting and remounting nfs

PATCH In review Li Lingfeng 2 participants 5 messages Mar 4, 2025 – Apr 9, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Ignore SB_RDONLY when mounting and remounting nfs

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(+)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/2 nfs: clear SB_RDONLY before getting superblock

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")
---
 fs/nfs/super.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/nfs/super.c +9 -0
index aeb715b4a690..3e5528c2c822 100644
@@ -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)
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH 1/2] nfs: clear SB_RDONLY before getting superblock

BTW - I tested applying both patches and the second one did fix the issue reported in
https://lore.kernel.org/all/[email protected]/T/

Quoted 58 lines · Li Lingfeng

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")
---
 fs/nfs/super.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/nfs/super.c +9 -1
index aeb715b4a690..3e5528c2c822 100644
@@ -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

#3 lore raw

PATCH 2/2 nfs: ignore SB_RDONLY when remounting nfs

In reply to Li Lingfeng

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/[email protected]/
Link[2]: https://lore.kernel.org/all/[email protected]/

---
 fs/nfs/super.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
fs/nfs/super.c +10 -0
index 3e5528c2c822..8f50447eb5d0 100644
@@ -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
Signature · 1 line
2.31.1
#4 lore raw
In reply to Li Lingfeng

Friendly ping..

Thanks

Quoted 14 lines · 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(+)