From patchwork Wed Aug 13 00:13:55 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guangyu Sun X-Patchwork-Id: 4715501 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 37186C0338 for ; Wed, 13 Aug 2014 00:15:10 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 77EF620160 for ; Wed, 13 Aug 2014 00:15:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 55A5820172 for ; Wed, 13 Aug 2014 00:15:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752093AbaHMAO7 (ORCPT ); Tue, 12 Aug 2014 20:14:59 -0400 Received: from userp1040.oracle.com ([156.151.31.81]:38736 "EHLO userp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751415AbaHMAO6 (ORCPT ); Tue, 12 Aug 2014 20:14:58 -0400 Received: from acsinet22.oracle.com (acsinet22.oracle.com [141.146.126.238]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id s7D0EuEd008559 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 13 Aug 2014 00:14:57 GMT Received: from userz7021.oracle.com (userz7021.oracle.com [156.151.31.85]) by acsinet22.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id s7D0EtiW009074 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Wed, 13 Aug 2014 00:14:56 GMT Received: from abhmp0010.oracle.com (abhmp0010.oracle.com [141.146.116.16]) by userz7021.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id s7D0Etfb023880 for ; Wed, 13 Aug 2014 00:14:55 GMT Received: from guasun.us.oracle.com (/130.35.70.6) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 12 Aug 2014 17:14:55 -0700 From: Guangyu Sun To: linux-btrfs@vger.kernel.org Subject: [PATCH 2/2] btrfs-progs: don't zero dev if dev size is smaller than -r rootdir size Date: Tue, 12 Aug 2014 17:13:55 -0700 Message-Id: <1407888835-15739-2-git-send-email-guangyu.sun@oracle.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1407888835-15739-1-git-send-email-guangyu.sun@oracle.com> References: <1407888835-15739-1-git-send-email-guangyu.sun@oracle.com> X-Source-IP: acsinet22.oracle.com [141.146.126.238] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Even if the size of rootdir given by -r option is larger than the specified partition, zero_output_file() is still called. It will take time to fill up the partition with zero if the partition is big, and end up with an EIO. The size should be checked before zeroing the partition. Signed-off-by: Guangyu Sun --- mkfs.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mkfs.c b/mkfs.c index 71aea40..ea61180 100644 --- a/mkfs.c +++ b/mkfs.c @@ -1475,12 +1475,31 @@ int main(int ac, char **av) exit(1); } } else { + struct stat st; + u64 size; + fd = open_target(file); if (fd < 0) { fprintf(stderr, "unable to open the %s\n", file); exit(1); } + if (fstat(fd, &st) < 0) { + fprintf(stderr, "unable to stat %s\n", file); + exit(1); + } + + size = btrfs_device_size(fd, &st); + if (size == 0) { + fprintf(stderr, "unable to find %s size\n", file); + exit(1); + } + + if (size < block_count) { + fprintf(stderr, "%s is smaller than requested size\n", file); + exit(1); + } + first_file = file; ret = zero_output_file(fd, block_count, sectorsize); if (ret) {