2022-07-19 23:54:08

by Jeremy Bongio

[permalink] [raw]
Subject: [PATCH v3] tune2fs: Add support for get/set UUID ioctls.

When mounted, there is a race condition between changing the filesystem
UUID and changing other aspects of the filesystem, like mounting, resizing,
changing features, etc. Using these ioctls to get/set the UUID ensures the
filesystem is not being resized.

Signed-off-by: Jeremy Bongio <[email protected]>
---

Changes in V3:

Added modified utility to commit heading.

Simplified return value of handle_fsuuid.

Added get_mounted_fsuuid and set_mounted_fsuuid for clarity.

misc/tune2fs.c | 113 +++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 95 insertions(+), 18 deletions(-)

diff --git a/misc/tune2fs.c b/misc/tune2fs.c
index 6c162ba5..36863acf 100644
--- a/misc/tune2fs.c
+++ b/misc/tune2fs.c
@@ -82,11 +82,25 @@ extern int optind;
#define FS_IOC_GETFSLABEL _IOR(0x94, 49, char[FSLABEL_MAX])
#endif

+struct fsuuid {
+ __u32 fsu_len;
+ __u32 fsu_flags;
+ __u8 fsu_uuid[];
+};
+
+#ifndef EXT4_IOC_GETFSUUID
+#define EXT4_IOC_GETFSUUID _IOR('f', 44, struct fsuuid)
+#endif
+
+#ifndef EXT4_IOC_SETFSUUID
+#define EXT4_IOC_SETFSUUID _IOW('f', 44, struct fsuuid)
+#endif
+
extern int ask_yn(const char *string, int def);

const char *program_name = "tune2fs";
char *device_name;
-char *new_label, *new_last_mounted, *new_UUID;
+char *new_label, *new_last_mounted, *requested_uuid;
char *io_options;
static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
static int m_flag, M_flag, Q_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
@@ -2102,7 +2116,7 @@ static void parse_tune2fs_options(int argc, char **argv)
open_flag = EXT2_FLAG_RW;
break;
case 'U':
- new_UUID = optarg;
+ requested_uuid = optarg;
U_flag = 1;
open_flag = EXT2_FLAG_RW |
EXT2_FLAG_JOURNAL_DEV_OK;
@@ -3078,6 +3092,61 @@ int handle_fslabel(int setlabel) {
return 0;
}

+/*
+ * Use EXT4_IOC_GETFSUUID/EXT4_IOC_SETFSUUID to get/set file system UUID.
+ * Return: 0 on success
+ * -1 when the old method should be used
+ */
+int handle_fsuuid(__u8 *uuid, bool get)
+{
+ errcode_t ret;
+ int mnt_flags, fd;
+ char label[FSLABEL_MAX];
+ int maxlen = FSLABEL_MAX - 1;
+ char mntpt[PATH_MAX + 1];
+ struct fsuuid *fsuuid = NULL;
+
+ fsuuid = malloc(sizeof(*fsuuid) + UUID_SIZE);
+ if (!fsuuid)
+ return -1;
+
+ memcpy(fsuuid->fsu_uuid, uuid, UUID_SIZE);
+ fsuuid->fsu_len = UUID_SIZE;
+ fsuuid->fsu_flags = 0;
+
+ ret = ext2fs_check_mount_point(device_name, &mnt_flags,
+ mntpt, sizeof(mntpt));
+ if (ret || !(mnt_flags & EXT2_MF_MOUNTED) ||
+ (!get && (mnt_flags & EXT2_MF_READONLY)) ||
+ !mntpt[0])
+ return -1;
+
+ fd = open(mntpt, O_RDONLY);
+ if (fd < 0)
+ return -1;
+
+ if (get)
+ ret = ioctl(fd, EXT4_IOC_GETFSUUID, fsuuid);
+ else
+ ret = ioctl(fd, EXT4_IOC_SETFSUUID, fsuuid);
+
+ close(fd);
+ if (ret)
+ return -1;
+ return 0;
+}
+
+static inline int get_mounted_fsuuid(__u8 *old_uuid)
+{
+ return handle_fsuuid(old_uuid, true);
+}
+
+static inline int set_mounted_fsuuid(__u8 *new_uuid)
+{
+ return handle_fsuuid(new_uuid, false);
+}
+
+
#ifndef BUILD_AS_LIB
int main(int argc, char **argv)
#else
@@ -3454,6 +3523,7 @@ _("Warning: The journal is dirty. You may wish to replay the journal like:\n\n"
dgrp_t i;
char buf[SUPERBLOCK_SIZE] __attribute__ ((aligned(8)));
__u8 old_uuid[UUID_SIZE];
+ uuid_t new_uuid;

if (ext2fs_has_feature_stable_inodes(fs->super)) {
fputs(_("Cannot change the UUID of this filesystem "
@@ -3507,25 +3577,34 @@ _("Warning: The journal is dirty. You may wish to replay the journal like:\n\n"
set_csum = 1;
}

- memcpy(old_uuid, sb->s_uuid, UUID_SIZE);
- if ((strcasecmp(new_UUID, "null") == 0) ||
- (strcasecmp(new_UUID, "clear") == 0)) {
- uuid_clear(sb->s_uuid);
- } else if (strcasecmp(new_UUID, "time") == 0) {
- uuid_generate_time(sb->s_uuid);
- } else if (strcasecmp(new_UUID, "random") == 0) {
- uuid_generate(sb->s_uuid);
- } else if (uuid_parse(new_UUID, sb->s_uuid)) {
+ rc = get_mounted_fsuuid(old_uuid);
+ if (rc == -1)
+ memcpy(old_uuid, sb->s_uuid, UUID_SIZE);
+
+ if ((strcasecmp(requested_uuid, "null") == 0) ||
+ (strcasecmp(requested_uuid, "clear") == 0)) {
+ uuid_clear(new_uuid);
+ } else if (strcasecmp(requested_uuid, "time") == 0) {
+ uuid_generate_time(new_uuid);
+ } else if (strcasecmp(requested_uuid, "random") == 0) {
+ uuid_generate(new_uuid);
+ } else if (uuid_parse(requested_uuid, new_uuid)) {
com_err(program_name, 0, "%s",
_("Invalid UUID format\n"));
rc = 1;
goto closefs;
}
- ext2fs_init_csum_seed(fs);
- if (set_csum) {
- for (i = 0; i < fs->group_desc_count; i++)
- ext2fs_group_desc_csum_set(fs, i);
- fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
+
+ rc = set_mounted_fsuuid(new_uuid);
+ if (rc == -1) {
+ memcpy(sb->s_uuid, new_uuid, UUID_SIZE);
+ ext2fs_init_csum_seed(fs);
+ if (set_csum) {
+ for (i = 0; i < fs->group_desc_count; i++)
+ ext2fs_group_desc_csum_set(fs, i);
+ fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
+ }
+ ext2fs_mark_super_dirty(fs);
}

/* If this is a journal dev, we need to copy UUID into jsb */
@@ -3549,8 +3628,6 @@ _("Warning: The journal is dirty. You may wish to replay the journal like:\n\n"
if ((rc = fs_update_journal_user(sb, old_uuid)))
goto closefs;
}
-
- ext2fs_mark_super_dirty(fs);
}

if (I_flag) {
--
2.37.0.170.g444d1eabd0-goog


2022-08-16 05:37:00

by Theodore Ts'o

[permalink] [raw]
Subject: Re: [PATCH v3] tune2fs: Add support for get/set UUID ioctls.

On Tue, Jul 19, 2022 at 04:52:04PM -0700, Jeremy Bongio wrote:
> +/*
> + * Use EXT4_IOC_GETFSUUID/EXT4_IOC_SETFSUUID to get/set file system UUID.
> + * Return: 0 on success
> + * -1 when the old method should be used
> + */
> +int handle_fsuuid(__u8 *uuid, bool get)
> +{
> + errcode_t ret;
> + int mnt_flags, fd;
> + char label[FSLABEL_MAX];
> + int maxlen = FSLABEL_MAX - 1;
> + char mntpt[PATH_MAX + 1];
> + struct fsuuid *fsuuid = NULL;
> +
> + fsuuid = malloc(sizeof(*fsuuid) + UUID_SIZE);
> + if (!fsuuid)
> + return -1;

fsuuid is not getting freed in this function, so this will leak
memory.

...
> + if (get)
> + ret = ioctl(fd, EXT4_IOC_GETFSUUID, fsuuid);
> + else
> + ret = ioctl(fd, EXT4_IOC_SETFSUUID, fsuuid);

In the EXT4_IOC_GETFSUUID case, you need to copy fsuuid->fsu_uuid to
uuid, so it is returned to the caller.


> + ret = ext2fs_check_mount_point(device_name, &mnt_flags,
> + mntpt, sizeof(mntpt));
> + if (ret || !(mnt_flags & EXT2_MF_MOUNTED) ||
> + (!get && (mnt_flags & EXT2_MF_READONLY)) ||
> + !mntpt[0])
> + return -1;

handle_fsuuid() is getting called twice by tune2fs when handling the
-U option, which means we're calling ext2fs_check_mount_point() twice.

And around line 3364, tune2fs calls ext2fs_check_if_mounted() which
fetches the mnt_flags but which doesn't get the mountpoint.

So I wonder if wouldn't be better off changing tune2fs's main() to
call ext2fs_check_mount_point() instead of ext2fs_check_if_mounted(),
and we can just determine the mountpoint once.

Then, instead of calling handle_fsuuid/[gs]et_mounted_fsuuid, in the
handling of -U, we can do something like this:

if (U_flag) {
int fd = -1;
struct fsuuid *fsuuid = NULL;
...

if ((mnt_flags & EXT2_MF_MOUNTED) &&
!(mnt_flags & EXT2_MF_READONLY) && mntpt) {
fd = open(mntpt, O_RDONLY);
if (fd >= 0) {
fsuuid = malloc(sizeof(*fsuuid) + UUID_SIZE);
if (!fsuuid) {
close(fd);
fd = -1;
}
}
}


In other words, we can just inline all of handle_fsuuid, and the call
to get_mounted_fsuuid() just becomes:

if (fd >= 0) {
fsuuid->fsu_len - UUID_SIZE;
fsuuid->fsu_flags = 0;
ret = ioctl(fd, EXT4_IOC_GETFSUUID, fsuuid);
}

... and similarly for set_mounted_fsuuid().


Then at the end of tune2fs -U processing, we can do something like this:

if (fd >= 0)
close(fd);
free(fsuuid);

- Ted