[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220816205214.145632-1-bongiojp@gmail.com>
Date: Tue, 16 Aug 2022 13:52:14 -0700
From: Jeremy Bongio <bongiojp@...il.com>
To: Ted Tso <tytso@....edu>
Cc: linux-ext4@...r.kernel.org, Jeremy Bongio <bongiojp@...il.com>
Subject: [PATCH v4] 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 <bongiojp@...il.com>
---
Changes in v4:
Ioctl calls are now inline. handle_fsuuid() is removed.
Fsuuid is freed.
ext2fs_check_if_mounted() call replaced with ext2fs_check_mount_point()
to avoid redundancy.
I tested mounted and unmounted code paths.
misc/tune2fs.c | 98 ++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 79 insertions(+), 19 deletions(-)
diff --git a/misc/tune2fs.c b/misc/tune2fs.c
index 6c162ba5..d0cb90ae 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;
@@ -3090,6 +3104,7 @@ int tune2fs_main(int argc, char **argv)
io_manager io_ptr, io_ptr_orig = NULL;
int rc = 0;
char default_undo_file[1] = { 0 };
+ char mntpt[PATH_MAX + 1];
#ifdef ENABLE_NLS
setlocale(LC_MESSAGES, "");
@@ -3237,9 +3252,10 @@ retry_open:
goto closefs;
}
- retval = ext2fs_check_if_mounted(device_name, &mount_flags);
+ retval = ext2fs_check_mount_point(device_name, &mount_flags,
+ mntpt, sizeof(mntpt));
if (retval) {
- com_err("ext2fs_check_if_mount", retval,
+ com_err("ext2fs_check_mount_point", retval,
_("while determining whether %s is mounted."),
device_name);
rc = 1;
@@ -3454,6 +3470,10 @@ _("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;
+ int fd = -1;
+ struct fsuuid *fsuuid = NULL;
+ errcode_t ret = -1;
if (ext2fs_has_feature_stable_inodes(fs->super)) {
fputs(_("Cannot change the UUID of this filesystem "
@@ -3507,25 +3527,62 @@ _("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)) {
+ if ((mount_flags & EXT2_MF_MOUNTED) &&
+ !(mount_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;
+ }
+ }
+ }
+
+ /* Get the filesystem uuid through the ioctl.
+ * If the filesystem is offline or the ioctl is unavailable or
+ * fails, fall back to directly modifiying the superblock.
+ */
+ if (fd >= 0) {
+ fsuuid->fsu_len = UUID_SIZE;
+ fsuuid->fsu_flags = 0;
+ ret = ioctl(fd, EXT4_IOC_GETFSUUID, fsuuid);
+ }
+ if (ret)
+ 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;
+
+ /* Set the filesystem uuid through the ioctl or fallback to
+ * directly modifying superblock.
+ */
+ if (fd >= 0) {
+ fsuuid->fsu_len - UUID_SIZE;
+ fsuuid->fsu_flags = 0;
+ memcpy(&fsuuid->fsu_uuid, new_uuid, UUID_SIZE);
+ ret = ioctl(fd, EXT4_IOC_SETFSUUID, fsuuid);
+ }
+ if (ret) {
+ 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 */
@@ -3550,7 +3607,10 @@ _("Warning: The journal is dirty. You may wish to replay the journal like:\n\n"
goto closefs;
}
- ext2fs_mark_super_dirty(fs);
+ if (fd >= 0)
+ close(fd);
+ if (fsuuid)
+ free(fsuuid);
}
if (I_flag) {
--
2.37.1.595.g718a3a8f04-goog
Powered by blists - more mailing lists