From patchwork Fri Nov 24 05:21:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Misono Tomohiro X-Patchwork-Id: 10073565 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1DA616037F for ; Fri, 24 Nov 2017 05:21:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 063942A317 for ; Fri, 24 Nov 2017 05:21:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EDABA2A31C; Fri, 24 Nov 2017 05:21:44 +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=-6.9 required=2.0 tests=BAYES_00,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 4CA642A317 for ; Fri, 24 Nov 2017 05:21:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752351AbdKXFVl (ORCPT ); Fri, 24 Nov 2017 00:21:41 -0500 Received: from mgwkm01.jp.fujitsu.com ([202.219.69.168]:15610 "EHLO mgwkm01.jp.fujitsu.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752242AbdKXFVk (ORCPT ); Fri, 24 Nov 2017 00:21:40 -0500 Received: from kw-mxauth.gw.nic.fujitsu.com (unknown [192.168.231.132]) by mgwkm01.jp.fujitsu.com with smtp id 4baf_5ceb_5a038b2f_f69a_4167_8e43_3cafbefb02f0; Fri, 24 Nov 2017 14:21:33 +0900 Received: from g01jpfmpwyt01.exch.g01.fujitsu.local (g01jpfmpwyt01.exch.g01.fujitsu.local [10.128.193.38]) by kw-mxauth.gw.nic.fujitsu.com (Postfix) with ESMTP id 64530AC00B1 for ; Fri, 24 Nov 2017 14:21:30 +0900 (JST) Received: from g01jpexchyt37.g01.fujitsu.local (unknown [10.128.193.4]) by g01jpfmpwyt01.exch.g01.fujitsu.local (Postfix) with ESMTP id AD7F8BF8009 for ; Fri, 24 Nov 2017 14:21:29 +0900 (JST) X-SecurityPolicyCheck: OK by SHieldMailChecker v2.5.2 X-SHieldMailCheckerPolicyVersion: FJ-ISEC-20170217-enc X-SHieldMailCheckerMailID: 8595057988ef4f598ba2a925477c55b8 To: linux-btrfs From: "Misono, Tomohiro" Subject: [PATCH] btrfs-progs: mkfs: check the status of file at mkfs Message-ID: Date: Fri, 24 Nov 2017 14:21:15 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 Content-Language: en-US X-SecurityPolicyCheck-GC: OK by FENCE-Mail X-TM-AS-MML: disable Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently, only the status of block devices is checked at mkfs, but we should also check for regular files whether they are already formatted or mounted to prevent overwrite accidentally. Device status is checked by test_dev_for_mkfs(). The part which is not related to block device is split from this and used for both block device and regular file. Signed-off-by: Tomohiro Misono --- mkfs/common.c | 46 ++++++++++++++++++++++++++++++---------------- mkfs/common.h | 1 + mkfs/main.c | 8 ++++++-- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/mkfs/common.c b/mkfs/common.c index 71318b10..5bf56828 100644 --- a/mkfs/common.c +++ b/mkfs/common.c @@ -632,23 +632,9 @@ int test_dev_for_mkfs(const char *file, int force_overwrite) error("%s is a swap device", file); return 1; } - if (!force_overwrite) { - if (check_overwrite(file)) { - error("use the -f option to force overwrite of %s", - file); - return 1; - } - } - ret = check_mounted(file); - if (ret < 0) { - error("cannot check mount status of %s: %s", file, - strerror(-ret)); + ret = test_status_for_mkfs(file, force_overwrite); + if (ret) return 1; - } - if (ret == 1) { - error("%s is mounted", file); - return 1; - } /* check if the device is busy */ fd = open(file, O_RDWR|O_EXCL); if (fd < 0) { @@ -669,6 +655,34 @@ int test_dev_for_mkfs(const char *file, int force_overwrite) return 0; } +/* + * check if the file (device) is formatted or mounted + */ +int test_status_for_mkfs(const char *file, int force_overwrite) +{ + int ret; + + if (!force_overwrite) { + if (check_overwrite(file)) { + error("use the -f option to force overwrite of %s", + file); + return 1; + } + } + ret = check_mounted(file); + if (ret < 0) { + error("cannot check mount status of %s: %s", file, + strerror(-ret)); + return 1; + } + if (ret == 1) { + error("%s is mounted", file); + return 1; + } + + return 0; +} + int is_vol_small(const char *file) { int fd = -1; diff --git a/mkfs/common.h b/mkfs/common.h index 3757e9e7..f5ee5ee4 100644 --- a/mkfs/common.h +++ b/mkfs/common.h @@ -72,6 +72,7 @@ int test_minimum_size(const char *file, u64 min_dev_size); int is_vol_small(const char *file); int test_num_disk_vs_raid(u64 metadata_profile, u64 data_profile, u64 dev_cnt, int mixed, int ssd); +int test_status_for_mkfs(const char *file, int force_overwrite); int test_dev_for_mkfs(const char *file, int force_overwrite); #endif diff --git a/mkfs/main.c b/mkfs/main.c index a69a699f..e405e5a2 100644 --- a/mkfs/main.c +++ b/mkfs/main.c @@ -1598,8 +1598,12 @@ int main(int argc, char **argv) while (dev_cnt-- > 0) { file = argv[optind++]; if (is_block_device(file) == 1) - if (test_dev_for_mkfs(file, force_overwrite)) - goto error; + ret = test_dev_for_mkfs(file, force_overwrite); + else + ret = test_status_for_mkfs(file, force_overwrite); + + if (ret) + goto error; } optind = saved_optind;