diff mbox

[v2] btrfs-progs: print-tree: Enehance uuid item print

Message ID 20171031082850.19583-1-wqu@suse.com (mailing list archive)
State New, archived
Headers show

Commit Message

Qu Wenruo Oct. 31, 2017, 8:28 a.m. UTC
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 <wqu@suse.com>
---
Inspired by UUID related work from Misono-san.
v2:
  Use explicit btrfs_key_to_uuid() function to do the convert, and add
  more comment why we should do that. Suggested by Nikolay.
---
 print-tree.c | 34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)

Comments

Nikolay Borisov Oct. 31, 2017, 8:47 a.m. UTC | #1
On 31.10.2017 10:28, Qu Wenruo wrote:
> 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 <wqu@suse.com>
> ---
> Inspired by UUID related work from Misono-san.
> v2:
>   Use explicit btrfs_key_to_uuid() function to do the convert, and add
>   more comment why we should do that. Suggested by Nikolay.
> ---
>  print-tree.c | 34 +++++++++++++++++++++++++++++++---
>  1 file changed, 31 insertions(+), 3 deletions(-)
> 
> diff --git a/print-tree.c b/print-tree.c
> index 3c585e31f1fc..ab6f6c392519 100644
> --- a/print-tree.c
> +++ b/print-tree.c
> @@ -803,14 +803,42 @@ 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_key_to_uuid(struct btrfs_key *key, u8 *uuid)
>  {
> +	/*
> +	 * UUID we use is in byte order, which means:
"byte order" term per-se doesn't really say anything about how bytes are
stored, we either have little endian or big endian. So this sentence
needs to be more explicit "BTRFS always represents uuid in little endian"

> +	 *          0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
> +	 * UUID:0x 12 34 56-78 90-ab cd-ef 12-34 56 78 90 ab cd ef
> +	 *
> +	 * And we store them as is in btrfs_*disk*_key:
This could be "BTRFS' on-disk format also uses little-endian so the
in-memory uuid is stored on-disk without any change to endiannes"


> +	 *
> +	 *         Low bit        High bit|Low bit        High bit
> +	 * LE64:0x 12 34 56-78 90-ab cd-ef|12-34 56 78 90 ab cd ef
> +	 *             key.objectid       |     key.offset
> +	 *
> +	 * So here we just need to convert from native endian to LE to
> +	 * get the byte order UUID.
> +	 */
> +	put_unaligned_le64(key->objectid, uuid);
> +	put_unaligned_le64(key->offset, uuid + sizeof(u64));
> +}
> +
> +static void print_uuid_item(struct extent_buffer *l, int slot,
> +			    unsigned long offset, u32 item_size)
> +{
> +	struct btrfs_key key;
> +	char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
> +	u8 uuid[BTRFS_UUID_SIZE];
> +
> +	btrfs_item_key_to_cpu(l, &key, slot);
> +	btrfs_key_to_uuid(&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 +1325,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: {
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Qu Wenruo Oct. 31, 2017, 10:09 a.m. UTC | #2
On 2017年10月31日 16:47, Nikolay Borisov wrote:
> 
> 
> On 31.10.2017 10:28, Qu Wenruo wrote:
>> 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 <wqu@suse.com>
>> ---
>> Inspired by UUID related work from Misono-san.
>> v2:
>>   Use explicit btrfs_key_to_uuid() function to do the convert, and add
>>   more comment why we should do that. Suggested by Nikolay.
>> ---
>>  print-tree.c | 34 +++++++++++++++++++++++++++++++---
>>  1 file changed, 31 insertions(+), 3 deletions(-)
>>
>> diff --git a/print-tree.c b/print-tree.c
>> index 3c585e31f1fc..ab6f6c392519 100644
>> --- a/print-tree.c
>> +++ b/print-tree.c
>> @@ -803,14 +803,42 @@ 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_key_to_uuid(struct btrfs_key *key, u8 *uuid)
>>  {
>> +	/*
>> +	 * UUID we use is in byte order, which means:
> "byte order" term per-se doesn't really say anything about how bytes are
> stored, we either have little endian or big endian. So this sentence
> needs to be more explicit "BTRFS always represents uuid in little endian"

Well, it turned out that I get confused with the term "byte order" and
"network order", and in this case I mean "network order" which is also BE.

And just in case, if you're referring the UUID as u128, then it's
definitely big endian.

> 
>> +	 *          0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
>> +	 * UUID:0x 12 34 56-78 90-ab cd-ef 12-34 56 78 90 ab cd ef
>> +	 *
>> +	 * And we store them as is in btrfs_*disk*_key:
> This could be "BTRFS' on-disk format also uses little-endian so the
> in-memory uuid is stored on-disk without any change to endiannes"

While I'm always confused by your mention of endian in this case.

Here UUID is just 16 bytes, why endian is envolved? (We're not talking
about true UUID)
Isn't endian only affecting structure over 1 byte?

Here is just a stream of bytes, why endian is affected in UUID?
We didn't even trust uuid as u128 at all.

To me, only when we access things like key->objectid/offset we cares
about endian. For key->type and CSUM, there is no endian at all as we
are just accessing bytes. (Unless we convert them into u32/u64/u128)

Thanks,
Qu

> 
> 
>> +	 *
>> +	 *         Low bit        High bit|Low bit        High bit
>> +	 * LE64:0x 12 34 56-78 90-ab cd-ef|12-34 56 78 90 ab cd ef
>> +	 *             key.objectid       |     key.offset
>> +	 *
>> +	 * So here we just need to convert from native endian to LE to
>> +	 * get the byte order UUID.
>> +	 */
>> +	put_unaligned_le64(key->objectid, uuid);
>> +	put_unaligned_le64(key->offset, uuid + sizeof(u64));
>> +}
>> +
>> +static void print_uuid_item(struct extent_buffer *l, int slot,
>> +			    unsigned long offset, u32 item_size)
>> +{
>> +	struct btrfs_key key;
>> +	char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
>> +	u8 uuid[BTRFS_UUID_SIZE];
>> +
>> +	btrfs_item_key_to_cpu(l, &key, slot);
>> +	btrfs_key_to_uuid(&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 +1325,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: {
>>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
Nikolay Borisov Oct. 31, 2017, 10:27 a.m. UTC | #3
On 31.10.2017 12:09, Qu Wenruo wrote:
> 
> 
> On 2017年10月31日 16:47, Nikolay Borisov wrote:
>>
>>
>> On 31.10.2017 10:28, Qu Wenruo wrote:
>>> 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 <wqu@suse.com>
>>> ---
>>> Inspired by UUID related work from Misono-san.
>>> v2:
>>>   Use explicit btrfs_key_to_uuid() function to do the convert, and add
>>>   more comment why we should do that. Suggested by Nikolay.
>>> ---
>>>  print-tree.c | 34 +++++++++++++++++++++++++++++++---
>>>  1 file changed, 31 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/print-tree.c b/print-tree.c
>>> index 3c585e31f1fc..ab6f6c392519 100644
>>> --- a/print-tree.c
>>> +++ b/print-tree.c
>>> @@ -803,14 +803,42 @@ 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_key_to_uuid(struct btrfs_key *key, u8 *uuid)
>>>  {
>>> +	/*
>>> +	 * UUID we use is in byte order, which means:
>> "byte order" term per-se doesn't really say anything about how bytes are
>> stored, we either have little endian or big endian. So this sentence
>> needs to be more explicit "BTRFS always represents uuid in little endian"
> 
> Well, it turned out that I get confused with the term "byte order" and
> "network order", and in this case I mean "network order" which is also BE.
> 
> And just in case, if you're referring the UUID as u128, then it's
> definitely big endian.
> 
>>
>>> +	 *          0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
>>> +	 * UUID:0x 12 34 56-78 90-ab cd-ef 12-34 56 78 90 ab cd ef
>>> +	 *
>>> +	 * And we store them as is in btrfs_*disk*_key:
>> This could be "BTRFS' on-disk format also uses little-endian so the
>> in-memory uuid is stored on-disk without any change to endiannes"
> 
> While I'm always confused by your mention of endian in this case.
> 
> Here UUID is just 16 bytes, why endian is envolved? (We're not talking
> about true UUID)
> Isn't endian only affecting structure over 1 byte?
> 
> Here is just a stream of bytes, why endian is affected in UUID?
> We didn't even trust uuid as u128 at all.
> 
> To me, only when we access things like key->objectid/offset we cares
> about endian. For key->type and CSUM, there is no endian at all as we
> are just accessing bytes. (Unless we convert them into u32/u64/u128)

I don't understand where your confusion arises from but here is how I
understand things.

Btrfs generates uids by invoking uuid_le_gen (which actually calls
guid_gen). guid_gen in turn generates a 128 bit LE value, consisting of
16 bytes. Then in order to add this uuid to the uuid tree we actually
split those 16 bytes into 2 u64 values by calling btrfs_uuid_to_key and
assigne them to the objectid/offset members of the btrfs key. At this
point the endiannes of each of these 2 values is important since they
are being treated as u64.

So actually order is important and btrfs chose to work with LE values
explicitly so what I'm asking is to document this fact explicitly in the
code handling this otherwise one has to go and dig around the code to
figure out why you are doing an on-disk (LE)  -> host cpu(could be LE or
BE) -> LE conversion.

I hope this makes things clear



> 
> Thanks,
> Qu
> 
>>
>>
>>> +	 *
>>> +	 *         Low bit        High bit|Low bit        High bit
>>> +	 * LE64:0x 12 34 56-78 90-ab cd-ef|12-34 56 78 90 ab cd ef
>>> +	 *             key.objectid       |     key.offset
>>> +	 *
>>> +	 * So here we just need to convert from native endian to LE to
>>> +	 * get the byte order UUID.
>>> +	 */
>>> +	put_unaligned_le64(key->objectid, uuid);
>>> +	put_unaligned_le64(key->offset, uuid + sizeof(u64));
>>> +}
>>> +
>>> +static void print_uuid_item(struct extent_buffer *l, int slot,
>>> +			    unsigned long offset, u32 item_size)
>>> +{
>>> +	struct btrfs_key key;
>>> +	char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
>>> +	u8 uuid[BTRFS_UUID_SIZE];
>>> +
>>> +	btrfs_item_key_to_cpu(l, &key, slot);
>>> +	btrfs_key_to_uuid(&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 +1325,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: {
>>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>>
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Qu Wenruo Oct. 31, 2017, 10:54 a.m. UTC | #4
On 2017年10月31日 18:27, Nikolay Borisov wrote:
> 
> 
> On 31.10.2017 12:09, Qu Wenruo wrote:
>>
>>
>> On 2017年10月31日 16:47, Nikolay Borisov wrote:
>>>
>>>
>>> On 31.10.2017 10:28, Qu Wenruo wrote:
>>>> 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 <wqu@suse.com>
>>>> ---
>>>> Inspired by UUID related work from Misono-san.
>>>> v2:
>>>>   Use explicit btrfs_key_to_uuid() function to do the convert, and add
>>>>   more comment why we should do that. Suggested by Nikolay.
>>>> ---
>>>>  print-tree.c | 34 +++++++++++++++++++++++++++++++---
>>>>  1 file changed, 31 insertions(+), 3 deletions(-)
>>>>
>>>> diff --git a/print-tree.c b/print-tree.c
>>>> index 3c585e31f1fc..ab6f6c392519 100644
>>>> --- a/print-tree.c
>>>> +++ b/print-tree.c
>>>> @@ -803,14 +803,42 @@ 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_key_to_uuid(struct btrfs_key *key, u8 *uuid)
>>>>  {
>>>> +	/*
>>>> +	 * UUID we use is in byte order, which means:
>>> "byte order" term per-se doesn't really say anything about how bytes are
>>> stored, we either have little endian or big endian. So this sentence
>>> needs to be more explicit "BTRFS always represents uuid in little endian"
>>
>> Well, it turned out that I get confused with the term "byte order" and
>> "network order", and in this case I mean "network order" which is also BE.
>>
>> And just in case, if you're referring the UUID as u128, then it's
>> definitely big endian.
>>
>>>
>>>> +	 *          0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
>>>> +	 * UUID:0x 12 34 56-78 90-ab cd-ef 12-34 56 78 90 ab cd ef
>>>> +	 *
>>>> +	 * And we store them as is in btrfs_*disk*_key:
>>> This could be "BTRFS' on-disk format also uses little-endian so the
>>> in-memory uuid is stored on-disk without any change to endiannes"
>>
>> While I'm always confused by your mention of endian in this case.
>>
>> Here UUID is just 16 bytes, why endian is envolved? (We're not talking
>> about true UUID)
>> Isn't endian only affecting structure over 1 byte?
>>
>> Here is just a stream of bytes, why endian is affected in UUID?
>> We didn't even trust uuid as u128 at all.
>>
>> To me, only when we access things like key->objectid/offset we cares
>> about endian. For key->type and CSUM, there is no endian at all as we
>> are just accessing bytes. (Unless we convert them into u32/u64/u128)
> 
> I don't understand where your confusion arises from but here is how I
> understand things.
> 
> Btrfs generates uids by invoking uuid_le_gen (which actually calls
> guid_gen). guid_gen in turn generates a 128 bit LE value, consisting of
> 16 bytes.

Here, just 16 bytes, an array of bytes, no endian involved at all.

> Then in order to add this uuid to the uuid tree we actually
> split those 16 bytes into 2 u64 values by calling btrfs_uuid_to_key and
> assigne them to the objectid/offset members of the btrfs key.

Only at this specially timing we need to care about endian.

Because we are converting an array to u32, which is larger than one
byte, we need to consider endian.

And what we do is, forcing the 8 bytes array to be treated as u64 native
endian. And then convert native endian u64 to LE64.

The only endian like problem I can think of here is, which
(object/offset) represents the leading half (offset +0).

> At this
> point the endiannes of each of these 2 values is important since they
> are being treated as u64.

If kernel code is changed to things like:
---
memcpy(&disk_key.objectid, uuid , 8);
memcpy(&disk_key.offset, uuid + 8, 8);
---

Maybe it's easier for both us to understand each other.

> 
> So actually order is important and btrfs chose to work with LE values
> explicitly so what I'm asking is to document this fact explicitly in the
> code handling this otherwise one has to go and dig around the code to
> figure out why you are doing an on-disk (LE)  -> host cpu(could be LE or
> BE) -> LE conversion.

But since UUID is just an array of bytes, what ever the endian btrfs
choose doesn't affect the UUID at all.

Even we choose on-disk BE, it doesn't matter at all.
We only needs to get an array of bytes.

Just like what we treat the uuid in root_item, just
memcpy()/write_extent_buffer(), no one really cares about endian at all,
since it's just an array of bytes, not affected at all.


So here my point is, there is no endian for BTRFS uuid.

And in fact CPU native endian is not even involved at all.
We only need to recover an array of bytes, and no matter what endian
we're using, we will get the same array of bytes, both in memory and on
disk.
Just like what we did by calling
memcpy()/write_extent_buffer()/read_extent_buffer() and without even
thinking about endian.

That's also why I want to get rid of the put_unaligned_u64() and just
put a memcpy().

Thanks,
Qu

> 
> I hope this makes things clear
> 
> 
> 
>>
>> Thanks,
>> Qu
>>
>>>
>>>
>>>> +	 *
>>>> +	 *         Low bit        High bit|Low bit        High bit
>>>> +	 * LE64:0x 12 34 56-78 90-ab cd-ef|12-34 56 78 90 ab cd ef
>>>> +	 *             key.objectid       |     key.offset
>>>> +	 *
>>>> +	 * So here we just need to convert from native endian to LE to
>>>> +	 * get the byte order UUID.
>>>> +	 */
>>>> +	put_unaligned_le64(key->objectid, uuid);
>>>> +	put_unaligned_le64(key->offset, uuid + sizeof(u64));
>>>> +}
>>>> +
>>>> +static void print_uuid_item(struct extent_buffer *l, int slot,
>>>> +			    unsigned long offset, u32 item_size)
>>>> +{
>>>> +	struct btrfs_key key;
>>>> +	char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
>>>> +	u8 uuid[BTRFS_UUID_SIZE];
>>>> +
>>>> +	btrfs_item_key_to_cpu(l, &key, slot);
>>>> +	btrfs_key_to_uuid(&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 +1325,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: {
>>>>
>>> --
>>> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
>>> the body of a message to majordomo@vger.kernel.org
>>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>>>
>>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
diff mbox

Patch

diff --git a/print-tree.c b/print-tree.c
index 3c585e31f1fc..ab6f6c392519 100644
--- a/print-tree.c
+++ b/print-tree.c
@@ -803,14 +803,42 @@  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_key_to_uuid(struct btrfs_key *key, u8 *uuid)
 {
+	/*
+	 * UUID we use is in byte order, which means:
+	 *          0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
+	 * UUID:0x 12 34 56-78 90-ab cd-ef 12-34 56 78 90 ab cd ef
+	 *
+	 * And we store them as is in btrfs_*disk*_key:
+	 *
+	 *         Low bit        High bit|Low bit        High bit
+	 * LE64:0x 12 34 56-78 90-ab cd-ef|12-34 56 78 90 ab cd ef
+	 *             key.objectid       |     key.offset
+	 *
+	 * So here we just need to convert from native endian to LE to
+	 * get the byte order UUID.
+	 */
+	put_unaligned_le64(key->objectid, uuid);
+	put_unaligned_le64(key->offset, uuid + sizeof(u64));
+}
+
+static void print_uuid_item(struct extent_buffer *l, int slot,
+			    unsigned long offset, u32 item_size)
+{
+	struct btrfs_key key;
+	char uuid_str[BTRFS_UUID_UNPARSED_SIZE];
+	u8 uuid[BTRFS_UUID_SIZE];
+
+	btrfs_item_key_to_cpu(l, &key, slot);
+	btrfs_key_to_uuid(&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 +1325,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: {