From patchwork Fri Aug 21 03:21:26 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 7047761 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.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 9E1289F372 for ; Fri, 21 Aug 2015 03:23:37 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C7F7620585 for ; Fri, 21 Aug 2015 03:23:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DEAF0204CF for ; Fri, 21 Aug 2015 03:23:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752242AbbHUDXc (ORCPT ); Thu, 20 Aug 2015 23:23:32 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:34575 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1751609AbbHUDXb (ORCPT ); Thu, 20 Aug 2015 23:23:31 -0400 X-IronPort-AV: E=Sophos;i="5.15,520,1432569600"; d="scan'208";a="99871461" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 21 Aug 2015 11:26:42 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id t7L3NOfp009355; Fri, 21 Aug 2015 11:23:24 +0800 Received: from localhost.localdomain (10.167.226.33) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Fri, 21 Aug 2015 11:23:27 +0800 From: Qu Wenruo To: CC: Subject: [PATCH 1/2] btrfs-progs: Avoid reading bad fd in case of missing device. Date: Fri, 21 Aug 2015 11:21:26 +0800 Message-ID: <1440127287-21368-1-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.5.0 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 Offline btrfs tools, like btrfs-image, will infinitely loop when there is missing device. The reason is, for missing device, it's fd will be set to -1, but before we reading, we only check the fd validation by checking if it's 0. So in that case, -1 will pass the validation check, and cause pread to return 0, and loop to read. Just change the validation check from "== 0" to "<= 0" to avoid such problem. Reported-by: Timothy Normand Miller Signed-off-by: Qu Wenruo --- btrfs-image.c | 2 +- disk-io.c | 4 ++-- extent_io.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/btrfs-image.c b/btrfs-image.c index 3684a05..b225325 100644 --- a/btrfs-image.c +++ b/btrfs-image.c @@ -894,7 +894,7 @@ static int read_data_extent(struct metadump_struct *md, device = multi->stripes[0].dev; - if (device->fd == 0) { + if (device->fd <= 0) { fprintf(stderr, "Device we need to read from is not open\n"); free(multi); diff --git a/disk-io.c b/disk-io.c index fdcfd6d..d8ca101 100644 --- a/disk-io.c +++ b/disk-io.c @@ -266,7 +266,7 @@ int read_whole_eb(struct btrfs_fs_info *info, struct extent_buffer *eb, int mirr } device = multi->stripes[0].dev; - if (device->fd == 0) { + if (device->fd <= 0) { kfree(multi); return -EIO; } @@ -385,7 +385,7 @@ int read_extent_data(struct btrfs_root *root, char *data, } device = multi->stripes[0].dev; - if (device->fd == 0) + if (device->fd <= 0) goto err; if (*len > max_len) *len = max_len; diff --git a/extent_io.c b/extent_io.c index 5d49710..07695ef 100644 --- a/extent_io.c +++ b/extent_io.c @@ -714,7 +714,7 @@ int read_data_from_disk(struct btrfs_fs_info *info, void *buf, u64 offset, device = multi->stripes[0].dev; read_len = min(bytes_left, read_len); - if (device->fd == 0) { + if (device->fd <= 0) { kfree(multi); return -EIO; } @@ -790,7 +790,7 @@ int write_data_to_disk(struct btrfs_fs_info *info, void *buf, u64 offset, raid_map = NULL; } else while (dev_nr < multi->num_stripes) { device = multi->stripes[dev_nr].dev; - if (device->fd == 0) { + if (device->fd <= 0) { kfree(multi); return -EIO; }