From patchwork Wed Nov 12 05:52:11 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 5285491 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0F9FE9F440 for ; Wed, 12 Nov 2014 05:51:59 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 429722010F for ; Wed, 12 Nov 2014 05:51:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 12226200EC for ; Wed, 12 Nov 2014 05:51:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753318AbaKLFvt (ORCPT ); Wed, 12 Nov 2014 00:51:49 -0500 Received: from cn.fujitsu.com ([59.151.112.132]:3863 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1752909AbaKLFvr (ORCPT ); Wed, 12 Nov 2014 00:51:47 -0500 X-IronPort-AV: E=Sophos;i="5.04,848,1406563200"; d="scan'208";a="43252659" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 12 Nov 2014 13:48:33 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id sAC5pUu6008652 for ; Wed, 12 Nov 2014 13:51:30 +0800 Received: from localhost.localdomain (10.167.226.33) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Wed, 12 Nov 2014 13:51:47 +0800 From: Qu Wenruo To: Subject: [PATCH 1/4] btrfs-progs: Check sb_bytenr with device size before scanning one device. Date: Wed, 12 Nov 2014 13:52:11 +0800 Message-ID: <1415771534-7979-1-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.1.3 MIME-Version: 1.0 X-Originating-IP: [10.167.226.33] 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.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham 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 When using btrfs check with -s option, if using '-s 2' on a small device which doesn't have the third superblock, "No valid Btrfs found" will be output, but it is not appropriate. So check sb_bytenr against device size before scanning a device and output proper error message. Signed-off-by: Qu Wenruo --- disk-io.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/disk-io.c b/disk-io.c index 77fc610..d2599b5 100644 --- a/disk-io.c +++ b/disk-io.c @@ -1000,10 +1000,20 @@ int btrfs_scan_fs_devices(int fd, const char *path, u64 sb_bytenr, int super_recover) { u64 total_devs; + u64 dev_size; int ret; if (!sb_bytenr) sb_bytenr = BTRFS_SUPER_INFO_OFFSET; + dev_size = lseek(fd, 0, SEEK_END); + if (dev_size < 0) + return (int)(dev_size); + lseek(fd, 0, SEEK_SET); + if (sb_bytenr > dev_size) { + fprintf(stderr, "Superblock bytenr is larger than device size\n"); + return -EINVAL; + } + ret = btrfs_scan_one_device(fd, path, fs_devices, &total_devs, sb_bytenr, super_recover); if (ret) {