From patchwork Wed Jun 17 07:49:02 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 6622411 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 9DAE0C0433 for ; Wed, 17 Jun 2015 07:49:41 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id B92C120838 for ; Wed, 17 Jun 2015 07:49:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B5C8220837 for ; Wed, 17 Jun 2015 07:49:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932279AbbFQHtb (ORCPT ); Wed, 17 Jun 2015 03:49:31 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:43300 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1758110AbbFQHtK (ORCPT ); Wed, 17 Jun 2015 03:49:10 -0400 X-IronPort-AV: E=Sophos;i="5.13,625,1427731200"; d="scan'208";a="97190032" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 17 Jun 2015 15:53:17 +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 t5H7lWi6015773 for ; Wed, 17 Jun 2015 15:47:32 +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, 17 Jun 2015 15:49:12 +0800 From: Qu Wenruo To: Subject: [PATCH 3/5] Btrfs-progs: map-logical: Introduce print_mapping_info function. Date: Wed, 17 Jun 2015 15:49:02 +0800 Message-ID: <1434527344-314-4-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1434527344-314-1-git-send-email-quwenruo@cn.fujitsu.com> References: <1434527344-314-1-git-send-email-quwenruo@cn.fujitsu.com> 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 The new function will print the mapping info of given range [logical, logical+len). Note, caller must ensure the range are completely inside an extent. Or btrfs_map_block can return -ENOENT. Signed-off-by: Qu Wenruo --- btrfs-map-logical.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/btrfs-map-logical.c b/btrfs-map-logical.c index 8442779..22ece82 100644 --- a/btrfs-map-logical.c +++ b/btrfs-map-logical.c @@ -93,6 +93,69 @@ out: return ret; } +static int __print_mapping_info(struct btrfs_fs_info *fs_info, u64 logical, + u64 len, int mirror_num) +{ + struct btrfs_multi_bio *multi = NULL; + u64 cur_offset = 0; + u64 cur_len; + int ret = 0; + + while (cur_offset < len) { + struct btrfs_device *device; + int i; + + cur_len = len - cur_offset; + ret = btrfs_map_block(&fs_info->mapping_tree, READ, + logical + cur_offset, &cur_len, + &multi, mirror_num, NULL); + if (ret) { + fprintf(info_file, + "Error: fails to map mirror%d logical %llu: %s\n", + mirror_num, logical, strerror(-ret)); + return ret; + } + for (i = 0; i < multi->num_stripes; i++) { + device = multi->stripes[i].dev; + fprintf(info_file, + "mirror %d logical %Lu physical %Lu device %s\n", + mirror_num, logical + cur_offset, + multi->stripes[0].physical, + device->name); + } + kfree(multi); + multi = NULL; + cur_offset += cur_len; + } + return ret; +} + +/* + * Logical and len is the exact value of a extent. + * And offset is the offset inside the extent. It's only used for case + * where user only want to print part of the extent. + * + * Caller *MUST* ensure the range [logical,logical+len) are in one extent. + * Or we can encounter the following case, causing a -ENOENT error: + * |<-----given parameter------>| + * |<------ Extent A ----->| + */ +static int print_mapping_info(struct btrfs_fs_info *fs_info, u64 logical, + u64 len) +{ + int num_copies; + int mirror_num; + int ret = 0; + + num_copies = btrfs_num_copies(&fs_info->mapping_tree, logical, len); + for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) { + ret = __print_mapping_info(fs_info, logical, len, mirror_num); + if (ret < 0) + return ret; + } + return ret; +} + static struct extent_buffer * debug_read_block(struct btrfs_root *root, u64 bytenr, u32 blocksize, u64 copy) {