Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp4142539imw; Tue, 19 Jul 2022 00:00:28 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uMM8benoJLH9UsAa5JHCOckJYgYfseTDqNDCRSXAa7aOF/agqVedclAqlJCu4qwesOjg83 X-Received: by 2002:a05:6402:84a:b0:426:262d:967e with SMTP id b10-20020a056402084a00b00426262d967emr40483674edz.286.1658214028372; Tue, 19 Jul 2022 00:00:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658214028; cv=none; d=google.com; s=arc-20160816; b=KlLOJSRkPyrmvF9JTnmkQPfA+rsQigT1EAX9B1tX4F7QwbuNjkbJCfByys8U3Vyl1+ 4x9kTJSzVZMbIMEQm8ztBiuSGySSn/sBro6HSeX/oN7Lzr242SqtZP971xrBYkcN6hGp TEAt4yclocmWnKbp4PvMg2LJHb6PHrPvBvc2r5m8KrM9tdWSwqpYrs1dozeeaKOvAiJn Cb1xd5rH7Fxde5q5GjWGqEdiss7bAf4ut8Q00QkxQd1pOSRxVPWcp0aY37Qt2YbQHrVy nLanB/9TPmuRCnbDye2JjaWrBcXKYb1BVm0RrtTqr+ofR98lPjyGmqMsiiJuOzUbwezf 1ixw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=0Gaofz2SsLryolN9aUM5kOQ+B7lUKOwHiqvay7zstoU=; b=ZBRPTOCV7uj4T5el96ayx7Pzmp+kTUji62YQ1G4fMmjDOaxooXViEm5/kG3rbZ/39y ZrfdfZxIXHtRH/U8sYUZJZeY2N9x8xMInh9fp+EmvUxHxZcPwIApJz60OkhSTmPnoFCs zTVPznxEio//oY36jx3AtiWW1SqPoZuCXTx9EaDKe8bOWNALIhbkZU2IAf88cN/oe4tO mjrks5S05jQh/Og6RlO3ocags6fPFfqprluG37Ae/HigOqB7OB2gYKqRrMyHR77iUzZg 9GoG4n3+cWgydyS10R1/oE7hLBfyj7ceFZtVHLDwn7iSExkNO7rhc6xPvT34AxIhFry6 7wAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SMY4e2wQ; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j22-20020a170906431600b00722e55f1eccsi17063452ejm.198.2022.07.19.00.00.02; Tue, 19 Jul 2022 00:00:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SMY4e2wQ; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232245AbiGSG4s (ORCPT + 99 others); Tue, 19 Jul 2022 02:56:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229969AbiGSG4s (ORCPT ); Tue, 19 Jul 2022 02:56:48 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7C4F24F1F for ; Mon, 18 Jul 2022 23:56:46 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id t15so477831pjo.1 for ; Mon, 18 Jul 2022 23:56:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=0Gaofz2SsLryolN9aUM5kOQ+B7lUKOwHiqvay7zstoU=; b=SMY4e2wQDzMMEWaewtWW9immy53jKoAiUu3HArP295w/im3YtjdQ0YdIAWfAxxkPyI 9Rh7lO0BegSqQjwWjhGBBGrKEbU7FlYqsyGwPqle0jnM6RJX4G6/FeAEb52AOJeUbmV5 NSKRuYhVXqr5LbhjtbcsBfFyz1MViU2vR0AFgoATNaiIrRQiEDxS4haPv3PKJi+GZv/e kCMWE3dgX4K6n4j6nEmSczzRsUcv1DTo3wJxzFRKp2/sjB84DAL2ycS6usmqGZkKFOJF MwrSV96RjWFN6VU8c0JdHRshOdVIQ3HaBO5XVUi3br7mIt39Y3HKJ2FznQgS1xN3pcIR XBJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=0Gaofz2SsLryolN9aUM5kOQ+B7lUKOwHiqvay7zstoU=; b=Nrv83Jm1/JOBZY+sl/NYz+s8RU7Iz8+bYTvjE3PPNcfBoiSRJ6gQY3tZM0eSe/ExpS 1H2RbU7Sj3y5v2gHExpxSCb/5sA41SgbYjWESQLnGluFlp6SXJxad98yw+v91cATMeCM 21M3Vv2ov/AgPnIbWwQe56kUubGKPXOF9MTKi0IzGwTpDT6B5bunLNO4r4o/klzYbtIM i5BP3ggspfJT7VNHkhPyxt69+2gHTNl6gSyBChbvFKOZzz4YuV4bs4yDrZSUeXsFz8uf SaI5JGM5Bg6939oiaVibbSM2jFGtc/yYPETh45oTQhNbKnPMhichpygiUmtuuvqG4+f2 N6/Q== X-Gm-Message-State: AJIora/HC1o/8YWCiEHS0SRYTz2MozCYK1VY8c3U7pmNCYe1YIjBk+0W pziSjHONC5e3BCvVDL9KV6bXKIkCxpTMMQ== X-Received: by 2002:a17:902:8f87:b0:16c:2c88:39ec with SMTP id z7-20020a1709028f8700b0016c2c8839ecmr32856548plo.52.1658213806321; Mon, 18 Jul 2022 23:56:46 -0700 (PDT) Received: from jbongio9100214.roam.corp.google.com (cpe-104-173-199-31.socal.res.rr.com. [104.173.199.31]) by smtp.googlemail.com with ESMTPSA id r18-20020a632b12000000b00415d873b7a2sm9231335pgr.11.2022.07.18.23.56.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 23:56:45 -0700 (PDT) From: Jeremy Bongio To: Ted Tso Cc: linux-ext4@vger.kernel.org, Jeremy Bongio Subject: [PATCH v2] Add support for get/set UUID ioctls. Date: Mon, 18 Jul 2022 23:56:37 -0700 Message-Id: <20220719065637.154309-1-bongiojp@gmail.com> X-Mailer: git-send-email 2.37.0.170.g444d1eabd0-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org 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 --- fu_* fields are now named fsu_*. Removed EXT4_IOC_SETFSUUID_FLAG_BLOCKING flag. fsu_flags is initialized to 0. misc/tune2fs.c | 104 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 86 insertions(+), 18 deletions(-) diff --git a/misc/tune2fs.c b/misc/tune2fs.c index 6c162ba5..39399d83 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,52 @@ 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 on error + * -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) { + if (ioctl(fd, EXT4_IOC_GETFSUUID, fsuuid)) + ret = -1; + } else { + if (ioctl(fd, EXT4_IOC_SETFSUUID, fsuuid)) + ret = -1; + } + close(fd); + return ret; +} + + #ifndef BUILD_AS_LIB int main(int argc, char **argv) #else @@ -3454,6 +3514,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 +3568,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 = handle_fsuuid(old_uuid, true); + 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 = handle_fsuuid(new_uuid, false); + 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 +3619,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