From patchwork Wed Nov 1 01:50:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 10035833 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 CDF7D600C5 for ; Wed, 1 Nov 2017 01:50:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B7B1B28B25 for ; Wed, 1 Nov 2017 01:50:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AA66928B3E; Wed, 1 Nov 2017 01:50:11 +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 26EB428B25 for ; Wed, 1 Nov 2017 01:50:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754083AbdKABuI (ORCPT ); Tue, 31 Oct 2017 21:50:08 -0400 Received: from victor.provo.novell.com ([137.65.250.26]:59084 "EHLO prv3-mh.provo.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754075AbdKABuH (ORCPT ); Tue, 31 Oct 2017 21:50:07 -0400 Received: from adam-pc.lan (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (NOT encrypted); Tue, 31 Oct 2017 19:50:04 -0600 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz, nborisov@suse.com Subject: [PATCH v3] btrfs-progs: print-tree: Enehance uuid item print Date: Wed, 1 Nov 2017 09:50:01 +0800 Message-Id: <20171101015001.22101-1-wqu@suse.com> X-Mailer: git-send-email 2.14.3 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 For key type BTRFS_UUID_KEY_SUBVOL or BTRFS_UUID_KEY_RECEIVED_SUBVOL the key objectid and key offset is just half of the UUID. However we just print the key as %llu, which is converted from little endian, not byte order for UUID, nor the traditional 36 bytes human readable uuid format. Although true engineer can easily handle it by convert it in their brain, but to make it easier for search, output the result UUID using the 36 chars format. Signed-off-by: Qu Wenruo --- Inspired by UUID related work from Misono-san. v2: Add comment to explain the possible endian hassle, suggested by Nikolay. Extract the key to uuid convert into its own function v3: Don't bother the whole endian hassle at all, by following the same method we handle normal uuid and csum to directly memcpy() data from btrfs_disk_key. As long as we treat on-disk data and uuid as array of btrfs, there is no need to bother endian hassle at all. --- print-tree.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/print-tree.c b/print-tree.c index 3c585e31f1fc..2e2a4d607b47 100644 --- a/print-tree.c +++ b/print-tree.c @@ -803,14 +803,38 @@ void btrfs_print_key(struct btrfs_disk_key *disk_key) } } -static void print_uuid_item(struct extent_buffer *l, unsigned long offset, - u32 item_size) +static void btrfs_disk_key_to_uuid(struct btrfs_disk_key *disk_key, u8 *uuid) { + /* + * Btrfs treats uuid as u8[16], and store them directly into disk_key, + * where objectid is treated as u8[8] and stores uuid[0]~uuid[7]. + * And offset is also treated as u8[8] and stores uuid[8]~uuid[16]. + * + * So here we only need to directly get byte array from disk_key, and + * don't need to bother the endian hassle. + * Just like what we do for normal uuid and csum[]. + */ + memcpy(uuid, &disk_key->objectid, 8); + memcpy(uuid + 8, &disk_key->offset, 8); +} + +static void print_uuid_item(struct extent_buffer *l, int slot, + unsigned long offset, u32 item_size) +{ + struct btrfs_disk_key disk_key; + char uuid_str[BTRFS_UUID_UNPARSED_SIZE]; + u8 uuid[BTRFS_UUID_SIZE]; + + btrfs_item_key(l, &disk_key, slot); + btrfs_disk_key_to_uuid(&disk_key, uuid); + uuid_unparse(uuid, uuid_str); + if (item_size & (sizeof(u64) - 1)) { printf("btrfs: uuid item with illegal size %lu!\n", (unsigned long)item_size); return; } + printf("\t\tuuid %s\n", uuid_str); while (item_size) { __le64 subvol_id; @@ -1297,7 +1321,7 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *eb) break; case BTRFS_UUID_KEY_SUBVOL: case BTRFS_UUID_KEY_RECEIVED_SUBVOL: - print_uuid_item(eb, btrfs_item_ptr_offset(eb, i), + print_uuid_item(eb, i, btrfs_item_ptr_offset(eb, i), btrfs_item_size_nr(eb, i)); break; case BTRFS_STRING_ITEM_KEY: {