2 * Copyright (C) STRATO AG 2013. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #include <uuid/uuid.h>
21 #include <sys/ioctl.h>
23 #include "transaction.h"
25 #include "print-tree.h"
28 static void btrfs_uuid_to_key(const u8
*uuid
, u64
*key_objectid
,
31 *key_objectid
= get_unaligned_le64(uuid
);
32 *key_offset
= get_unaligned_le64(uuid
+ sizeof(u64
));
36 /* return -ENOENT for !found, < 0 for errors, or 0 if an item was found */
37 static int btrfs_uuid_tree_lookup_any(int fd
, const u8
*uuid
, u8 type
,
43 struct btrfs_ioctl_search_args search_arg
;
44 struct btrfs_ioctl_search_header
*search_header
;
48 btrfs_uuid_to_key(uuid
, &key_objectid
, &key_offset
);
50 memset(&search_arg
, 0, sizeof(search_arg
));
51 search_arg
.key
.tree_id
= BTRFS_UUID_TREE_OBJECTID
;
52 search_arg
.key
.min_objectid
= key_objectid
;
53 search_arg
.key
.max_objectid
= key_objectid
;
54 search_arg
.key
.min_type
= type
;
55 search_arg
.key
.max_type
= type
;
56 search_arg
.key
.min_offset
= key_offset
;
57 search_arg
.key
.max_offset
= key_offset
;
58 search_arg
.key
.max_transid
= (u64
)-1;
59 search_arg
.key
.nr_items
= 1;
60 ret
= ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &search_arg
);
63 "ioctl(BTRFS_IOC_TREE_SEARCH, uuid, key %016llx, UUID_KEY, %016llx) ret=%d, error: %m\n",
64 (unsigned long long)key_objectid
,
65 (unsigned long long)key_offset
, ret
);
70 if (search_arg
.key
.nr_items
< 1) {
74 search_header
= (struct btrfs_ioctl_search_header
*)(search_arg
.buf
);
75 item_size
= btrfs_search_header_len(search_header
);
76 if ((item_size
& (sizeof(u64
) - 1)) || item_size
== 0) {
77 printf("btrfs: uuid item with illegal size %lu!\n",
78 (unsigned long)item_size
);
85 /* return first stored id */
86 memcpy(&lesubid
, search_header
+ 1, sizeof(lesubid
));
87 *subid
= le64_to_cpu(lesubid
);
93 int btrfs_lookup_uuid_subvol_item(int fd
, const u8
*uuid
, u64
*subvol_id
)
95 return btrfs_uuid_tree_lookup_any(fd
, uuid
, BTRFS_UUID_KEY_SUBVOL
,
99 int btrfs_lookup_uuid_received_subvol_item(int fd
, const u8
*uuid
,
102 return btrfs_uuid_tree_lookup_any(fd
, uuid
,
103 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,