From patchwork Thu Nov 13 01:19:29 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gui Hecheng X-Patchwork-Id: 5293041 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 5B9A19F2F1 for ; Thu, 13 Nov 2014 01:21:17 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 78CDE201F2 for ; Thu, 13 Nov 2014 01:21:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8F75C201CE for ; Thu, 13 Nov 2014 01:21:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753481AbaKMBVL (ORCPT ); Wed, 12 Nov 2014 20:21:11 -0500 Received: from cn.fujitsu.com ([59.151.112.132]:59876 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1753395AbaKMBVK (ORCPT ); Wed, 12 Nov 2014 20:21:10 -0500 X-IronPort-AV: E=Sophos;i="5.04,848,1406563200"; d="scan'208";a="43305016" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 13 Nov 2014 09:17:56 +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 sAD1KteN025255; Thu, 13 Nov 2014 09:20:55 +0800 Received: from localhost.localdomain (10.167.226.111) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Thu, 13 Nov 2014 09:21:19 +0800 From: Gui Hecheng To: CC: , Gui Hecheng Subject: [PATCH v2 2/2] btrfs-progs: fix wrong num_devices for btrfs fi show with seed devices Date: Thu, 13 Nov 2014 09:19:29 +0800 Message-ID: <1415841569-2302-1-git-send-email-guihc.fnst@cn.fujitsu.com> X-Mailer: git-send-email 1.8.1.4 MIME-Version: 1.0 X-Originating-IP: [10.167.226.111] 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 @fi_args->num_devices in @get_fs_info() does not include seed devices. We could just correct it by searching the chunk tree and count how many dev_items there are in total which includes seed devices. Signed-off-by: Gui Hecheng --- changelog v1->v2: adopt more clear function name @find_max_device_id() @search_chunk_tree_for_fs_info() --- utils.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/utils.c b/utils.c index f3fca4f..0d3d7fa 100644 --- a/utils.c +++ b/utils.c @@ -1812,6 +1812,75 @@ int get_device_info(int fd, u64 devid, return ret ? -errno : 0; } +static u64 find_max_device_id(struct btrfs_ioctl_search_args *search_args, + int nr_items) +{ + struct btrfs_dev_item *dev_item; + char *buf = search_args->buf; + + buf += (nr_items - 1) * (sizeof(struct btrfs_ioctl_search_header) + + sizeof(struct btrfs_dev_item)); + buf += sizeof(struct btrfs_ioctl_search_header); + + dev_item = (struct btrfs_dev_item *)buf; + + return btrfs_stack_device_id(dev_item); +} + +static int search_chunk_tree_for_fs_info(int fd, + struct btrfs_ioctl_fs_info_args *fi_args) +{ + int ret; + int max_items; + u64 start_devid = 1; + struct btrfs_ioctl_search_args search_args; + struct btrfs_ioctl_search_key *search_key = &search_args.key; + + fi_args->num_devices = 0; + + max_items = BTRFS_SEARCH_ARGS_BUFSIZE + / (sizeof(struct btrfs_ioctl_search_header) + + sizeof(struct btrfs_dev_item)); + + search_key->tree_id = BTRFS_CHUNK_TREE_OBJECTID; + search_key->min_objectid = BTRFS_DEV_ITEMS_OBJECTID; + search_key->max_objectid = BTRFS_DEV_ITEMS_OBJECTID; + search_key->min_type = BTRFS_DEV_ITEM_KEY; + search_key->max_type = BTRFS_DEV_ITEM_KEY; + search_key->min_transid = 0; + search_key->max_transid = (u64)-1; + search_key->nr_items = max_items; + search_key->max_offset = (u64)-1; + +again: + search_key->min_offset = start_devid; + + ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &search_args); + if (ret < 0) + return -errno; + + fi_args->num_devices += (u64)search_key->nr_items; + + if (search_key->nr_items == max_items) { + start_devid = find_max_device_id(&search_args, + search_key->nr_items) + 1; + goto again; + } + + /* get the lastest max_id to stay consistent with the num_devices */ + if (search_key->nr_items == 0) + /* + * last tree_search returns an empty buf, use the devid of + * the last dev_item of the previous tree_search + */ + fi_args->max_id = start_devid - 1; + else + fi_args->max_id = find_max_device_id(&search_args, + search_key->nr_items); + + return 0; +} + /* * For a given path, fill in the ioctl fs_ and info_ args. * If the path is a btrfs mountpoint, fill info for all devices. @@ -1891,6 +1960,13 @@ int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args, ret = -errno; goto out; } + + /* + * The fs_args->num_devices does not include seed devices + */ + ret = search_chunk_tree_for_fs_info(fd, fi_args); + if (ret) + goto out; } if (!fi_args->num_devices)