Skip to contentSkip to conversation

Supporting same fsid mounting through the temp-fsid feature

PATCH In review Guilherme G. Piccoli 3 participants 13 messages Sep 13, 2023 – Sep 21, 2023

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

v4 cover Supporting same fsid mounting through the temp-fsid feature

Hi, this is the 4th round of the same fsid mounting patch. Our goal is to allow btrfs to have the same filesystem mounting at the same time. The main change this time was the name of the feature, from single-dev to temp-fsid, which seems the less disliked name heh

More details in the patches' changelogs.
Thanks,

Guilherme

Guilherme G. Piccoli (2):
btrfs-progs: Add the temp-fsid feature (to both mkfs/tune)
btrfs: Introduce the temp-fsid feature

btrfs-progs:

 common/fsfeatures.c        |  7 ++++
 kernel-shared/ctree.h      |  3 +-
 kernel-shared/uapi/btrfs.h |  7 ++++
 mkfs/main.c                |  4 ++-
 tune/main.c                | 72 +++++++++++++++++++++++++-------------
 5 files changed, 66 insertions(+), 27 deletions(-)

kernel:

 fs/btrfs/disk-io.c         | 18 +++++++++-
 fs/btrfs/fs.h              |  3 +-
 fs/btrfs/ioctl.c           | 18 ++++++++++
 fs/btrfs/sysfs.c           |  2 ++
 fs/btrfs/volumes.c         | 70 +++++++++++++++++++++++++++++++-------
 fs/btrfs/volumes.h         |  5 +++
 include/uapi/linux/btrfs.h |  7 ++++
 7 files changed, 109 insertions(+), 14 deletions(-)
Signature · 1 line
2.42.0
#1 lore raw

PATCH v4 1/2 btrfs-progs: Add the temp-fsid feature (to both mkfs/tune)

The temp-fsid feature allows a device to be mounted regardless of its fsid already being present in another device - in other words, this feature disables RAID modes / metadata_uuid, allowing users to mount the same fsid at the same time in the system.

Introduce hereby the feature to both mkfs (-O temp-fsid) and btrfstune (--convert-to-temp-fsid), syncing the kernel-shared headers as well. The feature is a compat_ro, its kernel version was set to v6.7.

---

V4:

- Rebased against v6.5.1, taking into account the patch ("tune: do not allow multiple incompatible features to be set"); created a new group for this feature;

- Renamed the feature to temp-fsid;

- Moved kernel version compat to 6.7.

V3: https://lore.kernel.org/linux-btrfs/[email protected]/

 common/fsfeatures.c        |  7 ++++
 kernel-shared/ctree.h      |  3 +-
 kernel-shared/uapi/btrfs.h |  7 ++++
 mkfs/main.c                |  4 ++-
 tune/main.c                | 72 +++++++++++++++++++++++++-------------
 5 files changed, 66 insertions(+), 27 deletions(-)
common/fsfeatures.c +7 -0
index 1c993b594288..e39aa0a622be 100644
@@ -161,6 +161,13 @@ static const struct btrfs_feature mkfs_features[] = {
 		VERSION_NULL(default),
 		.desc		= "RAID1 with 3 or 4 copies"
 	},
+	{
+		.name		= "temp-fsid",
+		.compat_ro_flag	= BTRFS_FEATURE_COMPAT_RO_TEMP_FSID,
+		.sysfs_name	= "temp_fsid",
+		VERSION_TO_STRING2(compat, 6,7),
+		.desc		= "temp fsid (allows same fsid mounting)"
+	},
 #ifdef BTRFS_ZONED
 	{
 		.name		= "zoned",
kernel-shared/ctree.h +2 -1
index 59533879b939..ca5777f0098b 100644
@@ -86,7 +86,8 @@ static inline u32 __BTRFS_LEAF_DATA_SIZE(u32 nodesize)
 	(BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE |	\
 	 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID | \
 	 BTRFS_FEATURE_COMPAT_RO_VERITY |		\
-	 BTRFS_FEATURE_COMPAT_RO_BLOCK_GROUP_TREE)
+	 BTRFS_FEATURE_COMPAT_RO_BLOCK_GROUP_TREE |	\
+	 BTRFS_FEATURE_COMPAT_RO_TEMP_FSID)
 
 #if EXPERIMENTAL
 #define BTRFS_FEATURE_INCOMPAT_SUPP			\
kernel-shared/uapi/btrfs.h +7 -0
index 85b04f89a2a9..8c3e47e7d711 100644
@@ -336,6 +336,13 @@ _static_assert(sizeof(struct btrfs_ioctl_fs_info_args) == 1024);
  */
 #define BTRFS_FEATURE_COMPAT_RO_BLOCK_GROUP_TREE	(1ULL << 3)
 
+/*
+ * A random fsid is generated for TEMP_FSID devices (as flagged by the
+ * corresponding compat_ro flag), in order to cope with same-fsid FS
+ * mounts.
+ */
+#define BTRFS_FEATURE_COMPAT_RO_TEMP_FSID	(1ULL << 4)
+
 #define BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF	(1ULL << 0)
 #define BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL	(1ULL << 1)
 #define BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS	(1ULL << 2)
mkfs/main.c +3 -1
index 1c5d668e1e02..911daafefb1c 100644
@@ -1025,6 +1025,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
 	char *label = NULL;
 	int nr_global_roots = sysconf(_SC_NPROCESSORS_ONLN);
 	char *source_dir = NULL;
+	bool temp_fsid;
 
 	cpu_detect_flags();
 	hash_init_accel();
@@ -1218,6 +1219,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
 		usage(&mkfs_cmd, 1);
 
 	opt_zoned = !!(features.incompat_flags & BTRFS_FEATURE_INCOMPAT_ZONED);
+	temp_fsid = !!(features.compat_ro_flags & BTRFS_FEATURE_COMPAT_RO_TEMP_FSID);
 
 	if (source_dir && device_count > 1) {
 		error("the option -r is limited to a single device");
@@ -1815,7 +1817,7 @@ out:
 		device_count = argc - optind;
 		while (device_count-- > 0) {
 			file = argv[optind++];
-			if (path_is_block_device(file) == 1)
+			if (path_is_block_device(file) == 1 && !temp_fsid)
 				btrfs_register_one_device(file);
 		}
 	}
tune/main.c +47 -25

135 lines not on this page · raw message

Signature · 1 line
2.42.0
#2 lore raw

PATCH v4 2/2 btrfs: Introduce the temp-fsid feature

In reply to Guilherme G. Piccoli

Btrfs doesn't currently support to mount 2 different devices holding the same filesystem - the fsid is exposed as a unique identifier by the driver. This case is supported though in some other common filesystems, like ext4.

Supporting the same-fsid mounts has the advantage of allowing btrfs to be used in A/B partitioned devices, like mobile phones or the Steam Deck for example. Without this support, it's not safe for users to keep the same "image version" in both A and B partitions, a setup that is quite common for development, for example. Also, as a big bonus, it allows fs integrity check based on block devices for RO devices (whereas currently it is required that both have different fsid, breaking the block device hash comparison).

Such same-fsid mounting is hereby added through the usage of the filesystem feature "temp-fsid" - when such feature is used, btrfs generates a random fsid for the filesystem and leverages the long-term present metadata_uuid infrastructure to enable the usage of this secondary "virtual" fsid, effectively requiring few non-invasive changes to the code and no new potential corner cases.

In order to prevent more code complexity and corner cases, the temp-fsid feature is not allowed when the metadata_uuid flag is present on the fs, or if the device is on fsid-change state. Device removal/replace is also disabled for filesystems presenting the feature.

---

V4:

- Rebased against the github misc-next branch (of 2023-09-13); notice it already includes the patch: ("btrfs: scan forget for no instance of dev"), that was folded into the original commit;

- Patch ("btrfs: scan but don't register device on single device filesystem") was took into account - now we don't need to mess with the function btrfs_scan_one_device() here, since it already has the "mounting" argument;

- Improved the description of the fsid/metadata_uuid relation in volumes.h comment (thanks Anand!);

- Dropped the '\n' in the btrfs_{err/info} prints (also thanks Anand!);

- Switched the feature name for temp-fsid - seems the "less disliked" name, though personally I'd prefer virtual-fsid; also, that could be easily changed according the maintainers agreement.

Previous versions:
V3: https://lore.kernel.org/linux-btrfs/[email protected]/

V2: https://lore.kernel.org/linux-btrfs/[email protected]/

V1: https://lore.kernel.org/linux-btrfs/[email protected]/

 fs/btrfs/disk-io.c         | 18 +++++++++-
 fs/btrfs/fs.h              |  3 +-
 fs/btrfs/ioctl.c           | 18 ++++++++++
 fs/btrfs/sysfs.c           |  2 ++
 fs/btrfs/volumes.c         | 70 +++++++++++++++++++++++++++++++-------
 fs/btrfs/volumes.h         |  5 +++
 include/uapi/linux/btrfs.h |  7 ++++
 7 files changed, 109 insertions(+), 14 deletions(-)
fs/btrfs/disk-io.c +17 -1
index 163f37ad1b27..860f98486d0d 100644
@@ -2302,6 +2302,7 @@ int btrfs_validate_super(struct btrfs_fs_info *fs_info,
 {
 	u64 nodesize = btrfs_super_nodesize(sb);
 	u64 sectorsize = btrfs_super_sectorsize(sb);
+	u8 *fsid;
 	int ret = 0;
 
 	if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
@@ -2382,7 +2383,22 @@ int btrfs_validate_super(struct btrfs_fs_info *fs_info,
 		ret = -EINVAL;
 	}
 
-	if (memcmp(fs_info->fs_devices->fsid, sb->fsid, BTRFS_FSID_SIZE) != 0) {
+	/*
+	 * For TEMP_FSID devices, btrfs creates a random fsid and makes
+	 * use of the metadata_uuid infrastructure in order to allow, for
+	 * example, two devices with same fsid getting mounted at the same
+	 * time. But notice no changes happen at the disk level, the random
+	 * generated fsid is a driver abstraction, not written to the disk.
+	 * That's the reason we're required here to compare the fsid  with
+	 * the metadata_uuid for such devices. See volumes.h for a more
+	 * descriptive analysis of the relation between fsid/metadata_uuid.
+	 */
+	if (btrfs_fs_compat_ro(fs_info, TEMP_FSID))
+		fsid = fs_info->fs_devices->metadata_uuid;
+	else
+		fsid = fs_info->fs_devices->fsid;
+
+	if (memcmp(fsid, sb->fsid, BTRFS_FSID_SIZE) != 0) {
 		btrfs_err(fs_info,
 		"superblock fsid doesn't match fsid of fs_devices: %pU != %pU",
 			  sb->fsid, fs_info->fs_devices->fsid);
fs/btrfs/fs.h +2 -1
index d84a390336fc..97fbbed79f0e 100644
@@ -196,7 +196,8 @@ enum {
 	(BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE |	\
 	 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID | \
 	 BTRFS_FEATURE_COMPAT_RO_VERITY |		\
-	 BTRFS_FEATURE_COMPAT_RO_BLOCK_GROUP_TREE)
+	 BTRFS_FEATURE_COMPAT_RO_BLOCK_GROUP_TREE |	\
+	 BTRFS_FEATURE_COMPAT_RO_TEMP_FSID)
 
 #define BTRFS_FEATURE_COMPAT_RO_SAFE_SET	0ULL
 #define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR	0ULL
fs/btrfs/ioctl.c +18 -0
index 018ea98b239a..0278ede9e308 100644
@@ -2684,6 +2684,12 @@ static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
+	if (btrfs_fs_compat_ro(fs_info, TEMP_FSID)) {
+		btrfs_err(fs_info,
+			  "device removal is unsupported on TEMP_FSID devices");
+		return -EINVAL;
+	}
+
 	vol_args = memdup_user(arg, sizeof(*vol_args));
 	if (IS_ERR(vol_args))
 		return PTR_ERR(vol_args);
@@ -2750,6 +2756,12 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
+	if (btrfs_fs_compat_ro(fs_info, TEMP_FSID)) {
+		btrfs_err(fs_info,
+			  "device removal is unsupported on TEMP_FSID devices");
+		return -EINVAL;
+	}
+
 	vol_args = memdup_user(arg, sizeof(*vol_args));
 	if (IS_ERR(vol_args))
 		return PTR_ERR(vol_args);
@@ -3274,6 +3286,12 @@ static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
+	if (btrfs_fs_compat_ro(fs_info, TEMP_FSID)) {
+		btrfs_err(fs_info,
+			  "device replace is unsupported on TEMP_FSID devices");
+		return -EINVAL;
+	}
+
 	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
 		btrfs_err(fs_info, "device replace not supported on extent tree v2 yet");
 		return -EINVAL;
fs/btrfs/sysfs.c +2 -0
index b1d1ac25237b..995773ece844 100644
@@ -290,6 +290,7 @@ BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
 BTRFS_FEAT_ATTR_COMPAT_RO(block_group_tree, BLOCK_GROUP_TREE);
+BTRFS_FEAT_ATTR_COMPAT_RO(temp_fsid, TEMP_FSID);
 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
 #ifdef CONFIG_BLK_DEV_ZONED
 BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
@@ -322,6 +323,7 @@ static struct attribute *btrfs_supported_feature_attrs[] = {
 	BTRFS_FEAT_ATTR_PTR(free_space_tree),
 	BTRFS_FEAT_ATTR_PTR(raid1c34),
 	BTRFS_FEAT_ATTR_PTR(block_group_tree),
+	BTRFS_FEAT_ATTR_PTR(temp_fsid),
 #ifdef CONFIG_BLK_DEV_ZONED
 	BTRFS_FEAT_ATTR_PTR(zoned),
 #endif
fs/btrfs/volumes.c +58 -12

101 lines not on this page · raw message

fs/btrfs/volumes.h +5 -0
index b513b2846793..13d486ed8095 100644
@@ -299,6 +299,11 @@ struct btrfs_fs_devices {
 	 *   - When the BTRFS_FEATURE_INCOMPAT_METADATA_UUID flag is set:
 	 *       fs_devices->fsid == sb->fsid
 	 *       fs_devices->metadata_uuid == sb->metadata_uuid
+	 *
+	 *   - When the BTRFS_FEATURE_COMPAT_RO_TEMP_FSID flag is set:
+	 *       fs_devices->fsid == random; [see the function prepare_random_fsid()]
+	 *       fs_devices->metadata_uuid = sb->fsid;
+	 *       sb->metadata_uuid == 0;
 	 */
 	u8 metadata_uuid[BTRFS_FSID_SIZE];
 
include/uapi/linux/btrfs.h +7 -0
index dbb8b96da50d..594a240a20d4 100644
@@ -313,6 +313,13 @@ struct btrfs_ioctl_fs_info_args {
  */
 #define BTRFS_FEATURE_COMPAT_RO_BLOCK_GROUP_TREE	(1ULL << 3)
 
+/*
+ * A random fsid is generated for TEMP_FSID devices (as flagged by the
+ * corresponding compat_ro flag), in order to cope with same-fsid FS
+ * mounts.
+ */
+#define BTRFS_FEATURE_COMPAT_RO_TEMP_FSID	(1ULL << 4)
+
 #define BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF	(1ULL << 0)
 #define BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL	(1ULL << 1)
 #define BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS	(1ULL << 2)
Signature · 1 line
2.42.0
#3 lore raw

Re: [PATCH v4 2/2] btrfs: Introduce the temp-fsid feature

Quoted 42 lines · Guilherme G. Piccoli

Btrfs doesn't currently support to mount 2 different devices holding the same filesystem - the fsid is exposed as a unique identifier by the driver. This case is supported though in some other common filesystems, like ext4.

Supporting the same-fsid mounts has the advantage of allowing btrfs to be used in A/B partitioned devices, like mobile phones or the Steam Deck for example. Without this support, it's not safe for users to keep the same "image version" in both A and B partitions, a setup that is quite common for development, for example. Also, as a big bonus, it allows fs integrity check based on block devices for RO devices (whereas currently it is required that both have different fsid, breaking the block device hash comparison).

Such same-fsid mounting is hereby added through the usage of the filesystem feature "temp-fsid" - when such feature is used, btrfs generates a random fsid for the filesystem and leverages the long-term present metadata_uuid infrastructure to enable the usage of this secondary "virtual" fsid, effectively requiring few non-invasive changes to the code and no new potential corner cases.

In order to prevent more code complexity and corner cases, the temp-fsid feature is not allowed when the metadata_uuid flag is present on the fs, or if the device is on fsid-change state. Device removal/replace is also disabled for filesystems presenting the feature.

---

V4:

- Rebased against the github misc-next branch (of 2023-09-13); notice it already includes the patch: ("btrfs: scan forget for no instance of dev"), that was folded into the original commit;

- Patch ("btrfs: scan but don't register device on single device filesystem") was took into account - now we don't need to mess with the function btrfs_scan_one_device() here, since it already has the "mounting" argument;

Quoted 8 lines

- Improved the description of the fsid/metadata_uuid relation in volumes.h comment (thanks Anand!);

- Dropped the '\n' in the btrfs_{err/info} prints (also thanks Anand!);

- Switched the feature name for temp-fsid - seems the "less disliked" name, though personally I'd prefer virtual-fsid; also, that could be easily changed according the maintainers agreement.

Let's stick to temp-fsid for now, I like that it says the fsid is temporary, virtual could be potentially stored permanently (like another metadata_uuid).

I've added the patch to for-next, with some fixups, mostly stylistic. I'll add the btrfs-progs part soon so we have the support for testing. The feature seems to be complete regarding the original idea, if you have any updates please send them separate patches or replies to this thread. Thanks.

#4 lore raw

Re: [PATCH v4 2/2] btrfs: Introduce the temp-fsid feature

Quoted 11 lines · David Sterba

[...]
Let's stick to temp-fsid for now, I like that it says the fsid is temporary, virtual could be potentially stored permanently (like another metadata_uuid).

I've added the patch to for-next, with some fixups, mostly stylistic. I'll add the btrfs-progs part soon so we have the support for testing. The feature seems to be complete regarding the original idea, if you have any updates please send them separate patches or replies to this thread. Thanks.

Thanks a bunch David, much appreciated!
BTW, thanks a lot all reviewers, was a great and productive discussion.

For testing, likely you're aware but I think doesn't harm to mention here as well: there's a fstests case for this feature here ->

https://lore.kernel.org/linux-btrfs/[email protected]/

Cheers,

Guilherme

#5 lore raw

Re: [PATCH v4 2/2] btrfs: Introduce the temp-fsid feature

Quoted 13 lines · Guilherme G. Piccoli
Quoted 11 lines · David Sterba

[...]
Let's stick to temp-fsid for now, I like that it says the fsid is temporary, virtual could be potentially stored permanently (like another metadata_uuid).

I've added the patch to for-next, with some fixups, mostly stylistic. I'll add the btrfs-progs part soon so we have the support for testing. The feature seems to be complete regarding the original idea, if you have any updates please send them separate patches or replies to this thread. Thanks.

Quoted 5 lines

Thanks a bunch David, much appreciated!
BTW, thanks a lot all reviewers, was a great and productive discussion.

For testing, likely you're aware but I think doesn't harm to mention here as well: there's a fstests case for this feature here ->

This must successfully pass the remaining Btrfs fstests test cases with the MKFS_OPTION="-O temp-fsid" configuration option, or it should call not run for the incompatible feature.

I have observed that the following test case is failing with this patch:

 $ mkfs.btrfs -fq /dev/sdb1 :0
 $ btrfstune --convert-to-temp-fsid /dev/sdb1 :0
 $ mount /dev/sdb1 /btrfs :0

Mount /dev/sdb1 again at a different mount point and look for the copied file 'messages':

 $ cp /var/log/messages /btrfs :0
 $ mount /dev/sdb1 /btrfs1 :0
 $ ls -l /btrfs1 :0
 total 0   <-- empty

The copied file is missing because we consider each mount as a new fsid. This means subvolume mounts are also not working. Some operating systems mount $HOME as a subvolume, so those won't work either.

To resolve this, we can use devt to match in the device list and find the matching fs_devices or NULL.

Thanks, Anand

Quoted 1 line

https://lore.kernel.org/linux-btrfs/[email protected]/

#6 lore raw

Re: [PATCH v4 2/2] btrfs: Introduce the temp-fsid feature

Quoted 5 lines · Anand Jain

[...]
This must successfully pass the remaining Btrfs fstests test cases with the MKFS_OPTION="-O temp-fsid" configuration option, or it should call not run for the incompatible feature.

I kinda disagree here - this feature is not compatible with anything else, so I don't think it's fair to expect mounting with temp-fsid will just pass all other tests, specially for things like (the real) metadata_uuid or extra devices, like device removal...

Quoted 15 lines

I have observed that the following test case is failing with this patch:

  $ mkfs.btrfs -fq /dev/sdb1 :0
  $ btrfstune --convert-to-temp-fsid /dev/sdb1 :0
  $ mount /dev/sdb1 /btrfs :0

Mount /dev/sdb1 again at a different mount point and look for the copied file 'messages':

  $ cp /var/log/messages /btrfs :0
  $ mount /dev/sdb1 /btrfs1 :0
  $ ls -l /btrfs1 :0
  total 0   <-- empty
Quoted 6 lines

The copied file is missing because we consider each mount as a new fsid. This means subvolume mounts are also not working. Some operating systems mount $HOME as a subvolume, so those won't work either.

To resolve this, we can use devt to match in the device list and find the matching fs_devices or NULL.

Ugh, this one is ugly. Thanks for noticing that, I think this needs fixing indeed.

I've tried here, mounted the same temp-fsid btrfs device in 2 different mount points, and wrote two different files on each. The mount A can only see the file A, mount B can only see file B. Then after unmouting both, I cannot mount anymore with errors in ctree, so it got corrupted.

The way I think we could resolve this is by forbidding mounting a temp-fsid twice - after the random uuid generation, we could check for all fs_devices present and if any of it has the same metadata_uuid, we check if it's the same dev_t and bail.

The purpose of the feature is for having the same filesystem in different devices able to mount at the same time, but on different mount points. WDYT?

Cheers!

#7 lore raw

Re: [PATCH v4 2/2] btrfs: Introduce the temp-fsid feature

Quoted 10 lines · Guilherme G. Piccoli
Quoted 4 lines · Anand Jain

[...]
This must successfully pass the remaining Btrfs fstests test cases with the MKFS_OPTION="-O temp-fsid" configuration option, or it should call not run for the incompatible feature.

I kinda disagree here - this feature is not compatible with anything else, so I don't think it's fair to expect mounting with temp-fsid will just pass all other tests, specially for things like (the real) metadata_uuid or extra devices, like device removal...

Yeah, fstests are not in general ready for enabling some feature from the outside (mkfs, or mount options). Some of them work as long as they're orthogonal but some tests need to detect that and skip. In this case all multidevice tests would fail.

For test coverage there should be at lest one test that verifies known set of compatible features or usecases we care about in comibnation with the temp-fsid.

Quoted 30 lines
Quoted 15 lines

I have observed that the following test case is failing with this patch:

  $ mkfs.btrfs -fq /dev/sdb1 :0
  $ btrfstune --convert-to-temp-fsid /dev/sdb1 :0
  $ mount /dev/sdb1 /btrfs :0

Mount /dev/sdb1 again at a different mount point and look for the copied file 'messages':

  $ cp /var/log/messages /btrfs :0
  $ mount /dev/sdb1 /btrfs1 :0
  $ ls -l /btrfs1 :0
  total 0   <-- empty
Quoted 6 lines

The copied file is missing because we consider each mount as a new fsid. This means subvolume mounts are also not working. Some operating systems mount $HOME as a subvolume, so those won't work either.

To resolve this, we can use devt to match in the device list and find the matching fs_devices or NULL.

Ugh, this one is ugly. Thanks for noticing that, I think this needs fixing indeed.

I've tried here, mounted the same temp-fsid btrfs device in 2 different mount points, and wrote two different files on each. The mount A can only see the file A, mount B can only see file B. Then after unmouting both, I cannot mount anymore with errors in ctree, so it got corrupted.

Quoted 8 lines

The way I think we could resolve this is by forbidding mounting a temp-fsid twice - after the random uuid generation, we could check for all fs_devices present and if any of it has the same metadata_uuid, we check if it's the same dev_t and bail.

The purpose of the feature is for having the same filesystem in different devices able to mount at the same time, but on different mount points. WDYT?

The subvolume mount is a common use case and I hope it continues to work. Currently it does not seem so as said above, for correctness we may need to prevent it. We might find more and this should be known or fixed before final release.

#8 lore raw

Re: [PATCH v4 2/2] btrfs: Introduce the temp-fsid feature

Quoted 12 lines · David Sterba
Quoted 10 lines · Guilherme G. Piccoli
Quoted 4 lines · Anand Jain

[...]
This must successfully pass the remaining Btrfs fstests test cases with the MKFS_OPTION="-O temp-fsid" configuration option, or it should call not run for the incompatible feature.

I kinda disagree here - this feature is not compatible with anything else, so I don't think it's fair to expect mounting with temp-fsid will just pass all other tests, specially for things like (the real) metadata_uuid or extra devices, like device removal...

Quoted 9 lines

Yeah, fstests are not in general ready for enabling some feature from the outside (mkfs, or mount options). Some of them work as long as they're orthogonal but some tests need to detect that and skip. In this case all multidevice tests would fail.

For test coverage there should be at lest one test that verifies known set of compatible features or usecases we care about in comibnation with the temp-fsid.

If this patch had undergone verification using fstests for a single device, it might have already resolved the data corruption issue I recently reported. While it could be an isolated bug, having fstests confirm that 'yes, everything else is verified except for multi-device support' would be helpful.

The MKFS_OPTION="-O temp_fsid" config would help create temp_fsid in fstests, it may not be too difficult to verify.

Also, as commented in v3, btrfs-progs needs to support listing single devices with conflicting fsids while they are unmounted.

Thanks, Anand

Quoted 43 lines
Quoted 30 lines
Quoted 15 lines

I have observed that the following test case is failing with this patch:

   $ mkfs.btrfs -fq /dev/sdb1 :0
   $ btrfstune --convert-to-temp-fsid /dev/sdb1 :0
   $ mount /dev/sdb1 /btrfs :0

Mount /dev/sdb1 again at a different mount point and look for the copied file 'messages':

   $ cp /var/log/messages /btrfs :0
   $ mount /dev/sdb1 /btrfs1 :0
   $ ls -l /btrfs1 :0
   total 0   <-- empty
Quoted 6 lines

The copied file is missing because we consider each mount as a new fsid. This means subvolume mounts are also not working. Some operating systems mount $HOME as a subvolume, so those won't work either.

To resolve this, we can use devt to match in the device list and find the matching fs_devices or NULL.

Ugh, this one is ugly. Thanks for noticing that, I think this needs fixing indeed.

I've tried here, mounted the same temp-fsid btrfs device in 2 different mount points, and wrote two different files on each. The mount A can only see the file A, mount B can only see file B. Then after unmouting both, I cannot mount anymore with errors in ctree, so it got corrupted.

Quoted 8 lines

The way I think we could resolve this is by forbidding mounting a temp-fsid twice - after the random uuid generation, we could check for all fs_devices present and if any of it has the same metadata_uuid, we check if it's the same dev_t and bail.

The purpose of the feature is for having the same filesystem in different devices able to mount at the same time, but on different mount points. WDYT?

The subvolume mount is a common use case and I hope it continues to work. Currently it does not seem so as said above, for correctness we may need to prevent it. We might find more and this should be known or fixed before final release.

#9 lore raw

Re: [PATCH v4 2/2] btrfs: Introduce the temp-fsid feature

In reply to David Sterba
Quoted 6 lines · David Sterba

[...]

Quoted 5 lines

The way I think we could resolve this is by forbidding mounting a temp-fsid twice - after the random uuid generation, we could check for all fs_devices present and if any of it has the same metadata_uuid, we check if it's the same dev_t and bail.

Quoted 9 lines
Quoted 3 lines

The purpose of the feature is for having the same filesystem in different devices able to mount at the same time, but on different mount points. WDYT?

The subvolume mount is a common use case and I hope it continues to work. Currently it does not seem so as said above, for correctness we may need to prevent it. We might find more and this should be known or fixed before final release.

Thanks David, fully agree. And special thanks Anand for unveiling this important flaw. I'll work a small follow-up patch preventing that (will be in conference + travels so that might take some days...)

Cheers!

#10 lore raw

Re: [PATCH v4 2/2] btrfs: Introduce the temp-fsid feature

In reply to Guilherme G. Piccoli
Quoted 10 lines
fs/btrfs/volumes.c +8 -0
+
+static void prepare_random_fsid(struct btrfs_super_block *disk_super,
+				 const char *path)
+{
+	struct btrfs_fs_devices *fs_devices;
+	u8 vfsid[BTRFS_FSID_SIZE];
+	bool dup_fsid = true;
+

fs/btrfs/volumes.c static struct btrfs_fs_devices *find_fsid_reverted_metadata( · line 787

Quoted 10 lines
fs/btrfs/volumes.c +10 -0
+	while (dup_fsid) {
+		dup_fsid = false;
+		generate_random_uuid(vfsid);
+
+		list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
+			if (!memcmp(vfsid, fs_devices->fsid, BTRFS_FSID_SIZE) ||
+			    !memcmp(vfsid, fs_devices->metadata_uuid,
+				    BTRFS_FSID_SIZE))
+				dup_fsid = true;
+		}

I've noticed this section of the code a few times, but I don't believe I've mentioned it before. We've been using generate_random_guid() and generate_random_uuid() without checking for UUID clashes. Why extra uuid clash check here?

Thanks, Anand

#11 lore raw

Re: [PATCH v4 2/2] btrfs: Introduce the temp-fsid feature

Quoted 18 lines · Anand Jain

[...]

fs/btrfs/volumes.c static struct btrfs_fs_devices *find_fsid_reverted_metadata( · line 787

Quoted 10 lines
fs/btrfs/volumes.c +10 -0
+	while (dup_fsid) {
+		dup_fsid = false;
+		generate_random_uuid(vfsid);
+
+		list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
+			if (!memcmp(vfsid, fs_devices->fsid, BTRFS_FSID_SIZE) ||
+			    !memcmp(vfsid, fs_devices->metadata_uuid,
+				    BTRFS_FSID_SIZE))
+				dup_fsid = true;
+		}

I've noticed this section of the code a few times, but I don't believe I've mentioned it before. We've been using generate_random_guid() and generate_random_uuid() without checking for UUID clashes. Why extra uuid clash check here?

Hi Anand, what would happen if the UUID clashes here? Imagine we have another device with the same uuid (incredibly small chance, but...), I guess this would break in the subsequent path of fs_devices addition, hence I added this check, which is really cheap. We need to generate a really unique uuid here as the temp one.

Do you see any con in having this check? I'd say we should maybe even check in the other places the code is generating a random uuid but not checking for duplicity currently...

#12 lore raw

Re: [PATCH v4 2/2] btrfs: Introduce the temp-fsid feature

Quoted 25 lines · Guilherme G. Piccoli
Quoted 17 lines · Anand Jain

[...]

fs/btrfs/volumes.c static struct btrfs_fs_devices *find_fsid_reverted_metadata( · line 787

Quoted 10 lines
fs/btrfs/volumes.c +10 -0
+	while (dup_fsid) {
+		dup_fsid = false;
+		generate_random_uuid(vfsid);
+
+		list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
+			if (!memcmp(vfsid, fs_devices->fsid, BTRFS_FSID_SIZE) ||
+			    !memcmp(vfsid, fs_devices->metadata_uuid,
+				    BTRFS_FSID_SIZE))
+				dup_fsid = true;
+		}

I've noticed this section of the code a few times, but I don't believe I've mentioned it before. We've been using generate_random_guid() and generate_random_uuid() without checking for UUID clashes. Why extra uuid clash check here?

Hi Anand, what would happen if the UUID clashes here? Imagine we have another device with the same uuid (incredibly small chance, but...), I guess this would break in the subsequent path of fs_devices addition, hence I added this check, which is really cheap. We need to generate a really unique uuid here as the temp one.

Quoted 3 lines

Do you see any con in having this check? I'd say we should maybe even check in the other places the code is generating a random uuid but not checking for duplicity currently...

The duplicate uuid is unlikely so the loop would run once but making sure it's not duplicate does not hurt so let's keep it.