From patchwork Wed Sep 19 12:56:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Tulak X-Patchwork-Id: 10605831 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 75AE815E8 for ; Wed, 19 Sep 2018 12:56:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5A6682B2AF for ; Wed, 19 Sep 2018 12:56:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4B3012B29C; Wed, 19 Sep 2018 12:56:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ECFA82B29C for ; Wed, 19 Sep 2018 12:56:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731576AbeISSeQ (ORCPT ); Wed, 19 Sep 2018 14:34:16 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35724 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731419AbeISSeQ (ORCPT ); Wed, 19 Sep 2018 14:34:16 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 229B33082261; Wed, 19 Sep 2018 12:56:27 +0000 (UTC) Received: from honza-mbp.redhat.com (unknown [10.40.205.89]) by smtp.corp.redhat.com (Postfix) with ESMTP id F1D857B02A; Wed, 19 Sep 2018 12:56:25 +0000 (UTC) From: Jan Tulak To: linux-xfs@vger.kernel.org Cc: sandeen@sandeen.net, Jan Tulak Subject: [PATCH 1/2] mkfs: discard only after all validations Date: Wed, 19 Sep 2018 14:56:16 +0200 Message-Id: <20180919125617.28048-1-jtulak@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Wed, 19 Sep 2018 12:56:27 +0000 (UTC) Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Discard should happen only when everything has been validated, just before we start writing to the device. If it happens earlier, it is possible that mkfs will abort, but managed to already wipe data. This patch moves the discard to the latest possible moment. Signed-off-by: Jan Tulak --- mkfs/xfs_mkfs.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/mkfs/xfs_mkfs.c b/mkfs/xfs_mkfs.c index 2e53c1e8..81d9859a 100644 --- a/mkfs/xfs_mkfs.c +++ b/mkfs/xfs_mkfs.c @@ -2389,8 +2389,7 @@ _("log stripe unit (%d bytes) is too large (maximum is 256KiB)\n" static void open_devices( struct mkfs_params *cfg, - struct libxfs_xinit *xi, - bool discard) + struct libxfs_xinit *xi) { uint64_t sector_mask; @@ -2419,8 +2418,16 @@ open_devices( xi->dsize &= sector_mask; xi->rtsize &= sector_mask; xi->logBBsize &= (uint64_t)-1 << (max(cfg->lsectorlog, 10) - BBSHIFT); +} - +static void +discard_data( + struct libxfs_xinit *xi, + bool discard) +{ + /* + * This function has to be called after libxfs has been initialized. + */ if (!discard) return; @@ -3901,7 +3908,7 @@ main( /* * Open and validate the device configurations */ - open_devices(&cfg, &xi, (discard && !dry_run)); + open_devices(&cfg, &xi); validate_datadev(&cfg, &cli); validate_logdev(&cfg, &cli, &logfile); validate_rtdev(&cfg, &cli, &rtfile); @@ -3952,6 +3959,11 @@ main( exit(0); } + /* + * All values have been validated, discard the old device layout. + */ + discard_data(&xi, (discard && !dry_run)); + /* * we need the libxfs buffer cache from here on in. */ From patchwork Wed Sep 19 12:56:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Tulak X-Patchwork-Id: 10605833 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 175435A4 for ; Wed, 19 Sep 2018 12:56:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 095742B29C for ; Wed, 19 Sep 2018 12:56:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F1CAB2B2C1; Wed, 19 Sep 2018 12:56:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9B3702B29C for ; Wed, 19 Sep 2018 12:56:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731591AbeISSeT (ORCPT ); Wed, 19 Sep 2018 14:34:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56332 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731419AbeISSeT (ORCPT ); Wed, 19 Sep 2018 14:34:19 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B0D1486678; Wed, 19 Sep 2018 12:56:29 +0000 (UTC) Received: from honza-mbp.redhat.com (unknown [10.40.205.89]) by smtp.corp.redhat.com (Postfix) with ESMTP id BD94A7B02C; Wed, 19 Sep 2018 12:56:28 +0000 (UTC) From: Jan Tulak To: linux-xfs@vger.kernel.org Cc: sandeen@sandeen.net, Jan Tulak Subject: [PATCH 2/2] mkfs: move 'mounted' check before 'existing fs' check Date: Wed, 19 Sep 2018 14:56:17 +0200 Message-Id: <20180919125617.28048-2-jtulak@redhat.com> In-Reply-To: <20180919125617.28048-1-jtulak@redhat.com> References: <20180919125617.28048-1-jtulak@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 19 Sep 2018 12:56:29 +0000 (UTC) Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Check if a device is mounted (and issue an error about that) before asking for -f flag. Example of the old behaviour I'm changing: $ mkfs.xfs /dev/sda1 mkfs.xfs: /dev/sda1 appears to contain an existing filesystem (xfs). mkfs.xfs: Use the -f option to force overwrite. $ mkfs.xfs -f /dev/sda1 mkfs.xfs: /dev/sda1 contains a mounted filesystem Signed-off-by: Jan Tulak Reviewed-by: Darrick J. Wong --- mkfs/xfs_mkfs.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) -- 2.18.0 diff --git a/mkfs/xfs_mkfs.c b/mkfs/xfs_mkfs.c index 81d9859a..97e78647 100644 --- a/mkfs/xfs_mkfs.c +++ b/mkfs/xfs_mkfs.c @@ -1012,7 +1012,6 @@ check_device_type( bool no_size, bool no_name, int *create, - bool force_overwrite, const char *optname) { struct stat statbuf; @@ -1043,13 +1042,6 @@ check_device_type( return; } - if (!force_overwrite && check_overwrite(name)) { - fprintf(stderr, - _("%s: Use the -f option to force overwrite.\n"), - progname); - exit(1); - } - /* * We only want to completely truncate and recreate an existing file if * we were specifically told it was a file. Set the create flag only in @@ -1079,6 +1071,20 @@ check_device_type( usage(); } +static void +validate_overwrite( + const char *name, + bool force_overwrite) +{ + if (!force_overwrite && check_overwrite(name)) { + fprintf(stderr, + _("%s: Use the -f option to force overwrite.\n"), + progname); + exit(1); + } + +} + static void validate_ag_geometry( int blocklog, @@ -1731,18 +1737,15 @@ validate_sectorsize( * files or block devices and set the control parameters correctly. */ check_device_type(dfile, &cli->xi->disfile, !cli->dsize, !dfile, - dry_run ? NULL : &cli->xi->dcreat, - force_overwrite, "d"); + dry_run ? NULL : &cli->xi->dcreat, "d"); if (!cli->loginternal) check_device_type(cli->xi->logname, &cli->xi->lisfile, !cli->logsize, !cli->xi->logname, - dry_run ? NULL : &cli->xi->lcreat, - force_overwrite, "l"); + dry_run ? NULL : &cli->xi->lcreat, "l"); if (cli->xi->rtname) check_device_type(cli->xi->rtname, &cli->xi->risfile, !cli->rtsize, !cli->xi->rtname, - dry_run ? NULL : &cli->xi->rcreat, - force_overwrite, "r"); + dry_run ? NULL : &cli->xi->rcreat, "r"); /* * Explicitly disable direct IO for image files so we don't error out on @@ -3909,6 +3912,7 @@ main( * Open and validate the device configurations */ open_devices(&cfg, &xi); + validate_overwrite(dfile, force_overwrite); validate_datadev(&cfg, &cli); validate_logdev(&cfg, &cli, &logfile); validate_rtdev(&cfg, &cli, &rtfile);