btrfs-progs: fsck-tests: add test case with keyed data backref with reloc tree blocks
[btrfs-progs-unstable/devel.git] / uuid-tree.c
blob320eb67e1404943c73a278e83112e2d08b87b0a6
1 /*
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.
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <uuid/uuid.h>
21 #include <sys/ioctl.h>
22 #include "ctree.h"
23 #include "transaction.h"
24 #include "disk-io.h"
25 #include "print-tree.h"
28 static void btrfs_uuid_to_key(const u8 *uuid, u64 *key_objectid,
29 u64 *key_offset)
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,
38 u64 *subid)
40 int ret;
41 u64 key_objectid = 0;
42 u64 key_offset;
43 struct btrfs_ioctl_search_args search_arg;
44 struct btrfs_ioctl_search_header *search_header;
45 u32 item_size;
46 __le64 lesubid;
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);
61 if (ret < 0) {
62 fprintf(stderr,
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);
66 ret = -ENOENT;
67 goto out;
70 if (search_arg.key.nr_items < 1) {
71 ret = -ENOENT;
72 goto out;
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);
79 ret = -ENOENT;
80 goto out;
81 } else {
82 ret = 0;
85 /* return first stored id */
86 memcpy(&lesubid, search_header + 1, sizeof(lesubid));
87 *subid = le64_to_cpu(lesubid);
89 out:
90 return ret;
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,
96 subvol_id);
99 int btrfs_lookup_uuid_received_subvol_item(int fd, const u8 *uuid,
100 u64 *subvol_id)
102 return btrfs_uuid_tree_lookup_any(fd, uuid,
103 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
104 subvol_id);