From patchwork Thu Jan 3 07:32:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 10746959 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 599426C5 for ; Thu, 3 Jan 2019 07:32:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 465182793B for ; Thu, 3 Jan 2019 07:32:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3A65F2844B; Thu, 3 Jan 2019 07:32:35 +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=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, 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 D309E2793B for ; Thu, 3 Jan 2019 07:32:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729962AbfACHcd (ORCPT ); Thu, 3 Jan 2019 02:32:33 -0500 Received: from mx2.suse.de ([195.135.220.15]:46046 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729953AbfACHcd (ORCPT ); Thu, 3 Jan 2019 02:32:33 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 40037B00E for ; Thu, 3 Jan 2019 07:32:31 +0000 (UTC) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH v2 3/5] btrfs-progs: uuid: Port kernel btrfs_uuid_tree_lookup() Date: Thu, 3 Jan 2019 15:32:19 +0800 Message-Id: <20190103073221.10525-4-wqu@suse.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190103073221.10525-1-wqu@suse.com> References: <20190103073221.10525-1-wqu@suse.com> MIME-Version: 1.0 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 Although we have btrfs_uuid_tree_lookup_any(), it's an online function utilizing tree search ioctl, not an offline search function. This patch will port kernel btrfs_uuid_tree_lookup() into btrfs-progs for later proper uuid tree initialization. Signed-off-by: Qu Wenruo --- uuid-tree.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/uuid-tree.c b/uuid-tree.c index 320eb67e1404..e0bc51a5b8dc 100644 --- a/uuid-tree.c +++ b/uuid-tree.c @@ -23,6 +23,7 @@ #include "transaction.h" #include "disk-io.h" #include "print-tree.h" +#include "utils.h" static void btrfs_uuid_to_key(const u8 *uuid, u64 *key_objectid, @@ -32,8 +33,11 @@ static void btrfs_uuid_to_key(const u8 *uuid, u64 *key_objectid, *key_offset = get_unaligned_le64(uuid + sizeof(u64)); } - -/* return -ENOENT for !found, < 0 for errors, or 0 if an item was found */ +/* + * Search uuid tree of a *MOUNTED* btrfs (online) + * + * return -ENOENT for !found, < 0 for errors, or 0 if an item was found + */ static int btrfs_uuid_tree_lookup_any(int fd, const u8 *uuid, u8 type, u64 *subid) { @@ -103,3 +107,68 @@ int btrfs_lookup_uuid_received_subvol_item(int fd, const u8 *uuid, BTRFS_UUID_KEY_RECEIVED_SUBVOL, subvol_id); } + +/* + * Search uuid tree of an *UNMOUNTED* btrfs (offline) + * + * return -ENOENT for !found, < 0 for errors, or 0 if an item was found + */ +static int btrfs_uuid_tree_lookup(struct btrfs_root *uuid_root, u8 *uuid, + u8 type, u64 subid) +{ + int ret; + struct btrfs_path *path = NULL; + struct extent_buffer *eb; + int slot; + u32 item_size; + unsigned long offset; + struct btrfs_key key; + + if (!uuid_root) { + ret = -ENOENT; + goto out; + } + + path = btrfs_alloc_path(); + if (!path) { + ret = -ENOMEM; + goto out; + } + + btrfs_uuid_to_key(uuid, &key.objectid, &key.offset); + key.type = type; + ret = btrfs_search_slot(NULL, uuid_root, &key, path, 0, 0); + if (ret < 0) { + goto out; + } else if (ret > 0) { + ret = -ENOENT; + goto out; + } + + eb = path->nodes[0]; + slot = path->slots[0]; + item_size = btrfs_item_size_nr(eb, slot); + offset = btrfs_item_ptr_offset(eb, slot); + ret = -ENOENT; + + if (!IS_ALIGNED(item_size, sizeof(u64))) { + warning("uuid item with illegal size %lu!", + (unsigned long)item_size); + goto out; + } + while (item_size) { + __le64 data; + + read_extent_buffer(eb, &data, offset, sizeof(data)); + if (le64_to_cpu(data) == subid) { + ret = 0; + break; + } + offset += sizeof(data); + item_size -= sizeof(data); + } + +out: + btrfs_free_path(path); + return ret; +}