Skip to contentSkip to conversation

Fix read-only superblock in case of subvol RO remount

PATCH In review Goldwyn Rodrigues 3 participants 8 messages Feb 10, 2022 – Feb 11, 2022

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

PATCH 1/1 Fix read-only superblock in case of subvol RO remount

If a read-write root mount is remounted as read-only, the subvolume is also set to read-only.

Use a rw_mounts counter to check the number of read-write mounts, and change superblock to read-only only in case there are no read-write subvol mounts.

Since sb->s_flags can change while calling legacy_reconfigure(), use sb->s_flags instead of fc->sb_flags to re-evaluate sb->s_flags in reconfigure_super().

Test case:
dd if=/dev/zero of=btrfsfs seek=240 count=0 bs=1M
mkfs.btrfs btrfsfs
mount btrfsfs /mnt
btrfs subvol create /mnt/sv
mount -o remount,ro /mnt
mount -osubvol=/sv btrfsfs /mnt/sv
findmnt # /mnt is RO, /mnt/sv RW
mount -o remount,ro /mnt
findmnt # /mnt is RO, /mnt/sv RO as well
umount /mnt{/sv,}
rm btrfsfs

fs/btrfs/ctree.h +3 -0
index a2991971c6b5..2bb6869f15af 100644
@@ -1060,6 +1060,9 @@ struct btrfs_fs_info {
 	spinlock_t zone_active_bgs_lock;
 	struct list_head zone_active_bgs;
 
+	/* Count of subvol mounts read-write */
+	int rw_mounts;
+
 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
 	spinlock_t ref_verify_lock;
 	struct rb_root block_tree;
fs/btrfs/super.c +10 -0
index 33cfc9e27451..32941e11e551 100644
@@ -1835,6 +1835,11 @@ static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
 	/* mount_subvol() will free subvol_name and mnt_root */
 	root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
 
+	if (!IS_ERR(root) && !(flags & SB_RDONLY)) {
+		struct btrfs_fs_info *fs_info = btrfs_sb(mnt_root->mnt_sb);
+		fs_info->rw_mounts++;
+	}
+
 out:
 	return root;
 }
@@ -1958,6 +1963,9 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
 		goto out;
 
 	if (*flags & SB_RDONLY) {
+
+		if (--fs_info->rw_mounts > 0)
+			goto out;
 		/*
 		 * this also happens on 'umount -rf' or on shutdown, when
 		 * the filesystem is busy.
@@ -2057,6 +2065,8 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
 		if (ret)
 			goto restore;
 
+		fs_info->rw_mounts++;
+
 		btrfs_clear_sb_rdonly(sb);
 
 		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
fs/super.c +2 -1
index f1d4a193602d..fd528f76f14b 100644
@@ -913,7 +913,8 @@ int reconfigure_super(struct fs_context *fc)
 	}
 
 	WRITE_ONCE(sb->s_flags, ((sb->s_flags & ~fc->sb_flags_mask) |
-				 (fc->sb_flags & fc->sb_flags_mask)));
+				 (sb->s_flags & fc->sb_flags_mask)));
+
 	/* Needs to be ordered wrt mnt_is_readonly() */
 	smp_wmb();
 	sb->s_readonly_remount = 0;
Signature · 1 line
Goldwyn
#1 lore raw

fs/super.c int reconfigure_super(struct fs_context *fc) · line 917

Quoted 4 lines · Goldwyn Rodrigues
fs/super.c +2 -1
 	WRITE_ONCE(sb->s_flags, ((sb->s_flags & ~fc->sb_flags_mask) |
-				 (fc->sb_flags & fc->sb_flags_mask)));
+				 (sb->s_flags & fc->sb_flags_mask)));
+

what?

(a & ~b) | (a & b) == a, no?

#2 lore raw
Quoted 9 lines · Matthew Wilcox

fs/super.c int reconfigure_super(struct fs_context *fc) · line 917

Quoted 4 lines · Goldwyn Rodrigues
fs/super.c +2 -1
 	WRITE_ONCE(sb->s_flags, ((sb->s_flags & ~fc->sb_flags_mask) |
-				 (fc->sb_flags & fc->sb_flags_mask)));
+				 (sb->s_flags & fc->sb_flags_mask)));
+

what?

(a & ~b) | (a & b) == a, no?

Yup! I am re-enrolling into middle school to learn bit-arithmetic.

Apparently, I don't need this change at all and can be done in btrfs code by changing the flags parameter.

Will post a patch to btrfs ML with the updated patch.

Signature · 1 line
Goldwyn
#3 lore raw
In reply to Goldwyn Rodrigues
Quoted 2 lines · Goldwyn Rodrigues

If a read-write root mount is remounted as read-only, the subvolume is also set to read-only.

Errrr... Isn't that exactly what I want?

If I have a btrfs filesystem with hundreds of subvols, some of which may be mounted into various places in my filesystem, I would expect that if I remount the main mountpoint as RO, that all the subvols become RO as well. I actually don't mind if the behaviour went further and remounting ANY of the mount points as RO would make them all RO.

My mental model is that mounting a subvol somewhere is rather like a bind mount. And a bind mount goes RO if the underlying fs goes RO - doesn't it?

Or am I just confused about what this patch is discussing?

Graham

Quoted 88 lines

Use a rw_mounts counter to check the number of read-write mounts, and change superblock to read-only only in case there are no read-write subvol mounts.

Since sb->s_flags can change while calling legacy_reconfigure(), use sb->s_flags instead of fc->sb_flags to re-evaluate sb->s_flags in reconfigure_super().

Test case:
dd if=/dev/zero of=btrfsfs seek=240 count=0 bs=1M
mkfs.btrfs btrfsfs
mount btrfsfs /mnt
btrfs subvol create /mnt/sv
mount -o remount,ro /mnt
mount -osubvol=/sv btrfsfs /mnt/sv
findmnt # /mnt is RO, /mnt/sv RW
mount -o remount,ro /mnt
findmnt # /mnt is RO, /mnt/sv RO as well
umount /mnt{/sv,}
rm btrfsfs

fs/btrfs/ctree.h +3 -0
index a2991971c6b5..2bb6869f15af 100644
@@ -1060,6 +1060,9 @@ struct btrfs_fs_info {
 	spinlock_t zone_active_bgs_lock;
 	struct list_head zone_active_bgs;
 
+	/* Count of subvol mounts read-write */
+	int rw_mounts;
+
 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
 	spinlock_t ref_verify_lock;
 	struct rb_root block_tree;
fs/btrfs/super.c +10 -0
index 33cfc9e27451..32941e11e551 100644
@@ -1835,6 +1835,11 @@ static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
 	/* mount_subvol() will free subvol_name and mnt_root */
 	root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
 
+	if (!IS_ERR(root) && !(flags & SB_RDONLY)) {
+		struct btrfs_fs_info *fs_info = btrfs_sb(mnt_root->mnt_sb);
+		fs_info->rw_mounts++;
+	}
+
 out:
 	return root;
 }
@@ -1958,6 +1963,9 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
 		goto out;
 
 	if (*flags & SB_RDONLY) {
+
+		if (--fs_info->rw_mounts > 0)
+			goto out;
 		/*
 		 * this also happens on 'umount -rf' or on shutdown, when
 		 * the filesystem is busy.
@@ -2057,6 +2065,8 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
 		if (ret)
 			goto restore;
 
+		fs_info->rw_mounts++;
+
 		btrfs_clear_sb_rdonly(sb);
 
 		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
fs/super.c +2 -1
index f1d4a193602d..fd528f76f14b 100644
@@ -913,7 +913,8 @@ int reconfigure_super(struct fs_context *fc)
 	}
 
 	WRITE_ONCE(sb->s_flags, ((sb->s_flags & ~fc->sb_flags_mask) |
-				 (fc->sb_flags & fc->sb_flags_mask)));
+				 (sb->s_flags & fc->sb_flags_mask)));
+
 	/* Needs to be ordered wrt mnt_is_readonly() */
 	smp_wmb();
 	sb->s_readonly_remount = 0;
#4 lore raw
Quoted 12 lines · Graham Cobb
Quoted 2 lines · Goldwyn Rodrigues

If a read-write root mount is remounted as read-only, the subvolume is also set to read-only.

Errrr... Isn't that exactly what I want?

If I have a btrfs filesystem with hundreds of subvols, some of which may be mounted into various places in my filesystem, I would expect that if I remount the main mountpoint as RO, that all the subvols become RO as well. I actually don't mind if the behaviour went further and remounting ANY of the mount points as RO would make them all RO.

Quoted 4 lines

My mental model is that mounting a subvol somewhere is rather like a bind mount. And a bind mount goes RO if the underlying fs goes RO - doesn't it?

If we want bind mount, we would use bind mount. subvolume mounts and bind mounts are different and should be treated as different features.

Quoted 1 line

Or am I just confused about what this patch is discussing?

Root can also be considered as a unique subvolume with a unique subvolume id and a unique name=/

Signature · 1 line
Goldwyn
#5 lore raw
Quoted 13 lines · Goldwyn Rodrigues
Quoted 12 lines · Graham Cobb
Quoted 2 lines · Goldwyn Rodrigues

If a read-write root mount is remounted as read-only, the subvolume is also set to read-only.

Errrr... Isn't that exactly what I want?

If I have a btrfs filesystem with hundreds of subvols, some of which may be mounted into various places in my filesystem, I would expect that if I remount the main mountpoint as RO, that all the subvols become RO as well. I actually don't mind if the behaviour went further and remounting ANY of the mount points as RO would make them all RO.

Quoted 7 lines
Quoted 4 lines

My mental model is that mounting a subvol somewhere is rather like a bind mount. And a bind mount goes RO if the underlying fs goes RO - doesn't it?

If we want bind mount, we would use bind mount. subvolume mounts and bind mounts are different and should be treated as different features.

Yes that's a good point. However, I am still not convinced that this is a change in behaviour that is obvious enough to justify the risk of disruption to existing systems, admin scripts or system managers.

Quoted 5 lines
Quoted 2 lines

Or am I just confused about what this patch is discussing?

Root can also be considered as a unique subvolume with a unique subvolume id and a unique name=/

But with an important special property that is different from all other
subvolumes: all other subvolumes are reachable from it.

#6 lore raw
Quoted 27 lines · Graham Cobb
Quoted 13 lines · Goldwyn Rodrigues
Quoted 12 lines · Graham Cobb
Quoted 2 lines · Goldwyn Rodrigues

If a read-write root mount is remounted as read-only, the subvolume is also set to read-only.

Errrr... Isn't that exactly what I want?

If I have a btrfs filesystem with hundreds of subvols, some of which may be mounted into various places in my filesystem, I would expect that if I remount the main mountpoint as RO, that all the subvols become RO as well. I actually don't mind if the behaviour went further and remounting ANY of the mount points as RO would make them all RO.

Quoted 7 lines
Quoted 4 lines

My mental model is that mounting a subvol somewhere is rather like a bind mount. And a bind mount goes RO if the underlying fs goes RO - doesn't it?

If we want bind mount, we would use bind mount. subvolume mounts and bind mounts are different and should be treated as different features.

Yes that's a good point. However, I am still not convinced that this is a change in behaviour that is obvious enough to justify the risk of disruption to existing systems, admin scripts or system managers.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Or am I just confused about what this patch is discussing?

Root can also be considered as a unique subvolume with a unique subvolume id and a unique name=/

But with an important special property that is different from all other
subvolumes: all other subvolumes are reachable from it.

I should be a bit clearer. Imagine you create a filesystem and then create two subvolumes within it: a and a/b. You are suggesting that the result of remounting the top level of the filesystem as RO causes different effect on whether subvolume b goes RO depending on whether subvolume a has also been mounted somewhere?

#7 lore raw
Quoted 28 lines · Graham Cobb
Quoted 27 lines · Graham Cobb
Quoted 13 lines · Goldwyn Rodrigues
Quoted 12 lines · Graham Cobb
Quoted 2 lines · Goldwyn Rodrigues

If a read-write root mount is remounted as read-only, the subvolume is also set to read-only.

Errrr... Isn't that exactly what I want?

If I have a btrfs filesystem with hundreds of subvols, some of which may be mounted into various places in my filesystem, I would expect that if I remount the main mountpoint as RO, that all the subvols become RO as well. I actually don't mind if the behaviour went further and remounting ANY of the mount points as RO would make them all RO.

Quoted 7 lines
Quoted 4 lines

My mental model is that mounting a subvol somewhere is rather like a bind mount. And a bind mount goes RO if the underlying fs goes RO - doesn't it?

If we want bind mount, we would use bind mount. subvolume mounts and bind mounts are different and should be treated as different features.

Yes that's a good point. However, I am still not convinced that this is a change in behaviour that is obvious enough to justify the risk of disruption to existing systems, admin scripts or system managers.

Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Or am I just confused about what this patch is discussing?

Root can also be considered as a unique subvolume with a unique subvolume id and a unique name=/

But with an important special property that is different from all other
subvolumes: all other subvolumes are reachable from it.

Not quite. You can mount a subvolume directly without mounting the root:

mount -o subvol=<subvolname> <device> <mountpoint>

Quoted 6 lines

I should be a bit clearer. Imagine you create a filesystem and then create two subvolumes within it: a and a/b. You are suggesting that the result of remounting the top level of the filesystem as RO causes different effect on whether subvolume b goes RO depending on whether subvolume a has also been mounted somewhere?

I did not understand the question, too many "whether"s. :)

Check the test case.

If you mount a root subvolume (/) and a root's subvolume (/a or /a/b in your case). You remount root as ro, the subvolume a or a/b mounts also become read-only.

On the other hand, if you mount a or a/b (RW) _after_ root is remounted RO, the subvolume mounts are RW.

Signature · 1 line
Goldwyn