All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] btrfs: Fix subvol turns RO if root is remounted RO
@ 2022-02-11 14:09 Goldwyn Rodrigues
  2022-02-11 16:42 ` Graham Cobb
  2022-02-11 16:44 ` David Sterba
  0 siblings, 2 replies; 5+ messages in thread
From: Goldwyn Rodrigues @ 2022-02-11 14:09 UTC (permalink / raw)
  To: linux-btrfs

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.
Disable SB_RDONLY in flags passed so superblock does not change
read-only.


Test case:
DEV=/dev/vdb
mkfs.btrfs -f $DEV
mount $DEV /mnt
btrfs subvol create /mnt/sv
mount -o remount,ro /mnt
mount -o subvol=/sv $DEV /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,}


Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index a2991971c6b5..2bb6869f15af 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -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;
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 33cfc9e27451..2072759d5f22 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -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,11 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
 		goto out;
 
 	if (*flags & SB_RDONLY) {
+
+		if (--fs_info->rw_mounts > 0) {
+			*flags &= ~SB_RDONLY;
+			goto out;
+		}
 		/*
 		 * this also happens on 'umount -rf' or on shutdown, when
 		 * the filesystem is busy.
@@ -2057,6 +2067,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);

-- 
Goldwyn

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

end of thread, other threads:[~2022-02-12 11:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-11 14:09 [PATCH v2] btrfs: Fix subvol turns RO if root is remounted RO Goldwyn Rodrigues
2022-02-11 16:42 ` Graham Cobb
2022-02-11 19:44   ` Goldwyn Rodrigues
2022-02-12 11:06     ` Graham Cobb
2022-02-11 16:44 ` David Sterba

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.