From patchwork Wed Oct 24 02:04:47 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rock Lee X-Patchwork-Id: 1635281 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id C82EE3FD4E for ; Wed, 24 Oct 2012 02:13:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933990Ab2JXCM5 (ORCPT ); Tue, 23 Oct 2012 22:12:57 -0400 Received: from m53-178.qiye.163.com ([123.58.178.53]:45153 "EHLO m53-178.qiye.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755420Ab2JXCM5 (ORCPT ); Tue, 23 Oct 2012 22:12:57 -0400 X-Greylist: delayed 442 seconds by postgrey-1.27 at vger.kernel.org; Tue, 23 Oct 2012 22:12:56 EDT Received: from mail-la0-f46.google.com (mail-la0-f46.google.com [209.85.215.46]) by m53-178.qiye.163.com (HMail) with ESMTPA id C249A1228C4E for ; Wed, 24 Oct 2012 10:05:29 +0800 (CST) Received: by mail-la0-f46.google.com with SMTP id h6so12387lag.19 for ; Tue, 23 Oct 2012 19:05:27 -0700 (PDT) Received: by 10.112.44.132 with SMTP id e4mr5860753lbm.101.1351044327485; Tue, 23 Oct 2012 19:05:27 -0700 (PDT) MIME-Version: 1.0 Received: by 10.152.36.73 with HTTP; Tue, 23 Oct 2012 19:04:47 -0700 (PDT) From: Rock Lee Date: Wed, 24 Oct 2012 10:04:47 +0800 Message-ID: Subject: [BUG][BTRFS-PROGS] Fix Bug to corrupt the img file To: linux-btrfs@vger.kernel.org X-HM-Spam-Status: e1koWUFPN1dZCBgUCR5ZQUpVQ0lCQkJCQklITEtNS05PTFdZCQ4XHghZQVkoKz0kKzooKCQyNSQz Pjo*PilBS1VLQDYjJCI#KCQyNSQzPjo*PilBS1VLQCsvKSQiPigkMjUkMz46Pz4pQUtVS0A4NC41 LykiJC4oQUtVS0ApPjwyNDUkNTQpLzMkOjY#KTI4OkFLVUtAPyI1OjYyOCQyKyQ1NCQyNSQzPjo* PilBS1VLQDIrJEokNjI1Li8#JDg1LyRLJEpLQUtVS0AyKyRISyQ2MjUuLz4kODUvJEskTktBS1VL QDIrJE4kNjI1Li8#JDg1LyRLJEpLQUtVS0AyKyQvND86IiQ4NS8kSyRKS0tBS1VLQDIrJEokMzQu KSQ4NS8kSyRKS0tBS1VLQCguOSQ#QUpVTk5APTUkKC45JD41LDQpPygkMzcxJEpLS0lLSkFLVUlD WQY+ X-HM-Sender-Digest: e1kSHx4VD1lBWUc6MzY6KSo4Njo4LkoRNDUUNj81KBgaChBVSlVKSE5KS09PSElCSExNVTMWGhIX VQESFhIXFDsYFB8eVg8JEhgQVRgUFkVZV1kMHhlZQR0aFwgeBg++ Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Fix Bug to corrupt the img file Reproduce steps: > dd if=/dev/zero of=btrfs-small.img bs=1M count=1 > ls -lh btrfs-small.img -rw-rw-r--. 1 rock rock 1.0M Oct 24 09:51 btrfs-small.img > mkfs.btrfs btrfs-small.img -rw-rw-r--. 1 rock rock 2.0M Oct 24 09:53 btrfs-small.img Here you can see the original img file's size goes larger to 2Mbytes. Signed-off-by: Rock Lee --- utils.c | 8 ++++++-- 1 files changed, 6 insertions(+), 2 deletions(-) exit(1); diff --git a/utils.c b/utils.c index 205e667..3c88d2e 100644 --- a/utils.c +++ b/utils.c @@ -441,7 +441,7 @@ static int zero_blocks(int fd, off_t start, size_t len) return ret; } -static int zero_dev_start(int fd) +static int zero_dev_start(int fd, u64 dev_size) { off_t start = 0; size_t len = 2 * 1024 * 1024; @@ -451,6 +451,10 @@ static int zero_dev_start(int fd) start = 1024; len -= 1024; #endif + + if (dev_size < len) + return -EIO; + return zero_blocks(fd, start, len); } @@ -572,7 +576,7 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, discard_blocks(fd, 0, block_count); } - ret = zero_dev_start(fd); + ret = zero_dev_start(fd, block_count); if (ret) { fprintf(stderr, "failed to zero device start %d\n", ret);