From patchwork Fri Jul 15 01:40:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Bo X-Patchwork-Id: 9230995 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 2FFA160574 for ; Fri, 15 Jul 2016 01:36:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1E16828324 for ; Fri, 15 Jul 2016 01:36:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1040728326; Fri, 15 Jul 2016 01:36:32 +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, UNPARSEABLE_RELAY 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 9D49728324 for ; Fri, 15 Jul 2016 01:36:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751394AbcGOBg3 (ORCPT ); Thu, 14 Jul 2016 21:36:29 -0400 Received: from aserp1040.oracle.com ([141.146.126.69]:32010 "EHLO aserp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751279AbcGOBg1 (ORCPT ); Thu, 14 Jul 2016 21:36:27 -0400 Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id u6F1aN8O029722 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Jul 2016 01:36:23 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userv0022.oracle.com (8.14.4/8.13.8) with ESMTP id u6F1aM7b010557 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Jul 2016 01:36:23 GMT Received: from abhmp0017.oracle.com (abhmp0017.oracle.com [141.146.116.23]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id u6F1aMm4019709; Fri, 15 Jul 2016 01:36:22 GMT Received: from localhost.us.oracle.com (/10.211.47.181) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 15 Jul 2016 01:36:22 +0000 From: Liu Bo To: linux-btrfs@vger.kernel.org Cc: David Sterba Subject: [PATCH] Btrfs-progs: fix btrfs-map-logical to only print extent mapping info Date: Thu, 14 Jul 2016 18:40:27 -0700 Message-Id: <1468546827-28501-1-git-send-email-bo.li.liu@oracle.com> X-Mailer: git-send-email 2.5.5 X-Source-IP: userv0022.oracle.com [156.151.31.74] 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 I have a valid btrfs image which contains, ... item 10 key (1103101952 BLOCK_GROUP_ITEM 1288372224) itemoff 15947 itemsize 24 block group used 655360 chunk_objectid 256 flags DATA|RAID5 item 11 key (1103364096 EXTENT_ITEM 131072) itemoff 15894 itemsize 53 extent refs 1 gen 11 flags DATA extent data backref root 5 objectid 258 offset 0 count 1 item 12 key (1103888384 EXTENT_ITEM 262144) itemoff 15841 itemsize 53 extent refs 1 gen 15 flags DATA extent data backref root 1 objectid 256 offset 0 count 1 item 13 key (1104281600 EXTENT_ITEM 262144) itemoff 15788 itemsize 53 extent refs 1 gen 15 flags DATA extent data backref root 1 objectid 257 offset 0 count 1 ... The extent [1103364096, 131072) has length 131072, but if we run "btrfs-map-logical -l 1103364096 -b $((65536 * 3)) /dev/sda" it will return mapping info 's of non-existing extents. It's because it assumes that extents's are contiguous on logical address, when it's not true, after one loop (cur_logical += cur_len) and mapping the next extent, we can get an extent that is out of our search range and we end up with a negative @real_len and printing all mapping infos till the disk end. Signed-off-by: Liu Bo Reviewed-by: Qu Wenruo --- btrfs-map-logical.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/btrfs-map-logical.c b/btrfs-map-logical.c index fd0286d..f421a50 100644 --- a/btrfs-map-logical.c +++ b/btrfs-map-logical.c @@ -329,6 +329,11 @@ int main(int argc, char **argv) goto out_close_fd; if (ret > 0) break; + /* check again if there is overlap. */ + if (cur_logical + cur_len < logical || + cur_logical >= logical + bytes) + break; + real_logical = max(logical, cur_logical); real_len = min(logical + bytes, cur_logical + cur_len) - real_logical;