From patchwork Mon Sep 26 04:54:26 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 9350145 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 608CC6077B for ; Mon, 26 Sep 2016 04:54:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 51B4C28D21 for ; Mon, 26 Sep 2016 04:54:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 468A228D31; Mon, 26 Sep 2016 04:54:52 +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 C3CE528D21 for ; Mon, 26 Sep 2016 04:54:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751934AbcIZEyc (ORCPT ); Mon, 26 Sep 2016 00:54:32 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:52033 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1751375AbcIZEyb (ORCPT ); Mon, 26 Sep 2016 00:54:31 -0400 X-IronPort-AV: E=Sophos;i="5.20,367,1444665600"; d="scan'208";a="863822" Received: from unknown (HELO cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 26 Sep 2016 12:54:28 +0800 Received: from adam-work.localdomain (unknown [10.167.226.34]) by cn.fujitsu.com (Postfix) with ESMTP id 5E1E04043CE5; Mon, 26 Sep 2016 12:54:27 +0800 (CST) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz Subject: [PATCH] btrfs-progs: Return more meaningful value for btrfs_read_deve_super Date: Mon, 26 Sep 2016 12:54:26 +0800 Message-Id: <20160926045426.14340-1-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.10.0 MIME-Version: 1.0 X-yoursite-MailScanner-ID: 5E1E04043CE5.AF136 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: quwenruo@cn.fujitsu.com 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 btrfs_read_dev_super() only returns 0 or -1, which doesn't really help, caller won't know if it's caused by bad superblock or superblock out of range. Return -errno if pread64() return -1, and return -EOF if none or part of the super is read out, and return what check_super() returned. So caller can get -EIO to catch real corrupted super blocks. Signed-off-by: Qu Wenruo --- changelog: v2: Use better return values from pread64() and check_super(). v3: Fix wrong comment --- disk-io.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/disk-io.c b/disk-io.c index 854c285..2052bb3 100644 --- a/disk-io.c +++ b/disk-io.c @@ -1590,14 +1590,20 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr, if (sb_bytenr != BTRFS_SUPER_INFO_OFFSET) { ret = pread64(fd, buf, BTRFS_SUPER_INFO_SIZE, sb_bytenr); + /* real error */ + if (ret < 0) + return -errno; + + /* Not large enough sb, return -ENOENT instead of normal -EIO */ if (ret < BTRFS_SUPER_INFO_SIZE) - return -1; + return -ENOENT; if (btrfs_super_bytenr(buf) != sb_bytenr) - return -1; + return -EIO; - if (check_super(buf, sbflags)) - return -1; + ret = check_super(buf, sbflags); + if (ret < 0) + return ret; memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE); return 0; }