2 * Copyright (C) 2012 Alexander Block. 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.
19 #include <sys/ioctl.h>
22 #include "send-utils.h"
24 #include "btrfs-list.h"
26 static int btrfs_subvolid_resolve_sub(int fd, char *path, size_t *path_len,
29 int btrfs_subvolid_resolve(int fd, char *path, size_t path_len, u64 subvol_id)
35 path[path_len] = '\0';
36 return btrfs_subvolid_resolve_sub(fd, path, &path_len, subvol_id);
39 static int btrfs_subvolid_resolve_sub(int fd, char *path, size_t *path_len,
43 struct btrfs_ioctl_search_args search_arg;
44 struct btrfs_ioctl_ino_lookup_args ino_lookup_arg;
45 struct btrfs_ioctl_search_header *search_header;
46 struct btrfs_root_ref *backref_item;
48 if (subvol_id == BTRFS_FS_TREE_OBJECTID) {
56 memset(&search_arg, 0, sizeof(search_arg));
57 search_arg.key.tree_id = BTRFS_ROOT_TREE_OBJECTID;
58 search_arg.key.min_objectid = subvol_id;
59 search_arg.key.max_objectid = subvol_id;
60 search_arg.key.min_type = BTRFS_ROOT_BACKREF_KEY;
61 search_arg.key.max_type = BTRFS_ROOT_BACKREF_KEY;
62 search_arg.key.max_offset = (u64)-1;
63 search_arg.key.max_transid = (u64)-1;
64 search_arg.key.nr_items = 1;
65 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &search_arg);
68 "ioctl(BTRFS_IOC_TREE_SEARCH, subvol_id %llu) ret=%d, error: %s\n",
69 (unsigned long long)subvol_id, ret, strerror(errno));
73 if (search_arg.key.nr_items < 1) {
75 "failed to lookup subvol_id %llu!\n",
76 (unsigned long long)subvol_id);
79 search_header = (struct btrfs_ioctl_search_header *)search_arg.buf;
80 backref_item = (struct btrfs_root_ref *)(search_header + 1);
81 if (search_header->offset != BTRFS_FS_TREE_OBJECTID) {
84 sub_ret = btrfs_subvolid_resolve_sub(fd, path, path_len,
85 search_header->offset);
94 if (btrfs_stack_root_ref_dirid(backref_item) !=
95 BTRFS_FIRST_FREE_OBJECTID) {
98 memset(&ino_lookup_arg, 0, sizeof(ino_lookup_arg));
99 ino_lookup_arg.treeid = search_header->offset;
100 ino_lookup_arg.objectid =
101 btrfs_stack_root_ref_dirid(backref_item);
102 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_lookup_arg);
105 "ioctl(BTRFS_IOC_INO_LOOKUP) ret=%d, error: %s\n",
106 ret, strerror(errno));
110 len = strlen(ino_lookup_arg.name);
113 strcat(path, ino_lookup_arg.name);
117 if (*path_len < btrfs_stack_root_ref_name_len(backref_item))
119 strncat(path, (char *)(backref_item + 1),
120 btrfs_stack_root_ref_name_len(backref_item));
121 (*path_len) -= btrfs_stack_root_ref_name_len(backref_item);
125 static struct rb_node *tree_insert(struct rb_root *root,
126 struct subvol_info *si,
127 enum subvol_search_type type)
129 struct rb_node ** p = &root->rb_node;
130 struct rb_node * parent = NULL;
131 struct subvol_info *entry;
136 if (type == subvol_search_by_received_uuid) {
137 entry = rb_entry(parent, struct subvol_info,
140 comp = memcmp(entry->received_uuid, si->received_uuid,
143 if (entry->stransid < si->stransid)
145 else if (entry->stransid > si->stransid)
150 } else if (type == subvol_search_by_uuid) {
151 entry = rb_entry(parent, struct subvol_info,
153 comp = memcmp(entry->uuid, si->uuid, BTRFS_UUID_SIZE);
154 } else if (type == subvol_search_by_root_id) {
155 entry = rb_entry(parent, struct subvol_info,
157 comp = entry->root_id - si->root_id;
158 } else if (type == subvol_search_by_path) {
159 entry = rb_entry(parent, struct subvol_info,
161 comp = strcmp(entry->path, si->path);
174 if (type == subvol_search_by_received_uuid) {
175 rb_link_node(&si->rb_received_node, parent, p);
176 rb_insert_color(&si->rb_received_node, root);
177 } else if (type == subvol_search_by_uuid) {
178 rb_link_node(&si->rb_local_node, parent, p);
179 rb_insert_color(&si->rb_local_node, root);
180 } else if (type == subvol_search_by_root_id) {
181 rb_link_node(&si->rb_root_id_node, parent, p);
182 rb_insert_color(&si->rb_root_id_node, root);
183 } else if (type == subvol_search_by_path) {
184 rb_link_node(&si->rb_path_node, parent, p);
185 rb_insert_color(&si->rb_path_node, root);
190 static struct subvol_info *tree_search(struct rb_root *root,
191 u64 root_id, const u8 *uuid,
192 u64 stransid, const char *path,
193 enum subvol_search_type type)
195 struct rb_node * n = root->rb_node;
196 struct subvol_info *entry;
200 if (type == subvol_search_by_received_uuid) {
201 entry = rb_entry(n, struct subvol_info,
203 comp = memcmp(entry->received_uuid, uuid,
206 if (entry->stransid < stransid)
208 else if (entry->stransid > stransid)
213 } else if (type == subvol_search_by_uuid) {
214 entry = rb_entry(n, struct subvol_info, rb_local_node);
215 comp = memcmp(entry->uuid, uuid, BTRFS_UUID_SIZE);
216 } else if (type == subvol_search_by_root_id) {
217 entry = rb_entry(n, struct subvol_info, rb_root_id_node);
218 comp = entry->root_id - root_id;
219 } else if (type == subvol_search_by_path) {
220 entry = rb_entry(n, struct subvol_info, rb_path_node);
221 comp = strcmp(entry->path, path);
235 static int count_bytes(void *buf, int len, char b)
239 for (i = 0; i < len; i++) {
240 if (((char*)buf)[i] == b)
246 void subvol_uuid_search_add(struct subvol_uuid_search *s,
247 struct subvol_info *si)
251 tree_insert(&s->root_id_subvols, si, subvol_search_by_root_id);
252 tree_insert(&s->path_subvols, si, subvol_search_by_path);
254 cnt = count_bytes(si->uuid, BTRFS_UUID_SIZE, 0);
255 if (cnt != BTRFS_UUID_SIZE)
256 tree_insert(&s->local_subvols, si, subvol_search_by_uuid);
257 cnt = count_bytes(si->received_uuid, BTRFS_UUID_SIZE, 0);
258 if (cnt != BTRFS_UUID_SIZE)
259 tree_insert(&s->received_subvols, si,
260 subvol_search_by_received_uuid);
263 struct subvol_info *subvol_uuid_search(struct subvol_uuid_search *s,
264 u64 root_id, const u8 *uuid, u64 transid,
266 enum subvol_search_type type)
268 struct rb_root *root;
269 if (type == subvol_search_by_received_uuid)
270 root = &s->received_subvols;
271 else if (type == subvol_search_by_uuid)
272 root = &s->local_subvols;
273 else if (type == subvol_search_by_root_id)
274 root = &s->root_id_subvols;
275 else if (type == subvol_search_by_path)
276 root = &s->path_subvols;
279 return tree_search(root, root_id, uuid, transid, path, type);
282 int subvol_uuid_search_init(int mnt_fd, struct subvol_uuid_search *s)
285 struct btrfs_ioctl_search_args args;
286 struct btrfs_ioctl_search_key *sk = &args.key;
287 struct btrfs_ioctl_search_header *sh;
288 struct btrfs_root_item *root_item_ptr;
289 struct btrfs_root_item root_item;
290 struct subvol_info *si = NULL;
291 int root_item_valid = 0;
292 unsigned long off = 0;
297 memset(&args, 0, sizeof(args));
299 sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
301 sk->max_objectid = (u64)-1;
302 sk->max_offset = (u64)-1;
303 sk->max_transid = (u64)-1;
304 sk->min_type = BTRFS_ROOT_ITEM_KEY;
305 sk->max_type = BTRFS_ROOT_BACKREF_KEY;
309 ret = ioctl(mnt_fd, BTRFS_IOC_TREE_SEARCH, &args);
312 fprintf(stderr, "ERROR: can't perform the search- %s\n",
316 if (sk->nr_items == 0)
321 for (i = 0; i < sk->nr_items; i++) {
322 sh = (struct btrfs_ioctl_search_header *)(args.buf +
326 if ((sh->objectid != 5 &&
327 sh->objectid < BTRFS_FIRST_FREE_OBJECTID) ||
328 sh->objectid > BTRFS_LAST_FREE_OBJECTID)
331 if (sh->type == BTRFS_ROOT_ITEM_KEY) {
332 /* older kernels don't have uuids+times */
333 if (sh->len < sizeof(root_item)) {
337 root_item_ptr = (struct btrfs_root_item *)
339 memcpy(&root_item, root_item_ptr,
342 } else if (sh->type == BTRFS_ROOT_BACKREF_KEY ||
344 if (!root_item_valid)
347 path = btrfs_list_path_for_root(mnt_fd,
353 fprintf(stderr, "ERROR: unable to "
360 si = calloc(1, sizeof(*si));
361 si->root_id = sh->objectid;
362 memcpy(si->uuid, root_item.uuid,
364 memcpy(si->parent_uuid, root_item.parent_uuid,
366 memcpy(si->received_uuid,
367 root_item.received_uuid,
369 si->ctransid = btrfs_root_ctransid(&root_item);
370 si->otransid = btrfs_root_otransid(&root_item);
371 si->stransid = btrfs_root_stransid(&root_item);
372 si->rtransid = btrfs_root_rtransid(&root_item);
374 subvol_uuid_search_add(s, si);
384 * record the mins in sk so we can make sure the
385 * next search doesn't repeat this root
387 sk->min_objectid = sh->objectid;
388 sk->min_offset = sh->offset;
389 sk->min_type = sh->type;
392 if (sk->min_offset < (u64)-1)
394 else if (sk->min_objectid < (u64)-1) {
407 * It's safe to call this function even without the subvol_uuid_search_init()
408 * call before as long as the subvol_uuid_search structure is all-zero.
410 void subvol_uuid_search_finit(struct subvol_uuid_search *s)
412 struct rb_root *root = &s->root_id_subvols;
413 struct rb_node *node;
415 while ((node = rb_first(root))) {
416 struct subvol_info *entry =
417 rb_entry(node, struct subvol_info, rb_root_id_node);
420 rb_erase(node, root);
424 s->root_id_subvols = RB_ROOT;
425 s->local_subvols = RB_ROOT;
426 s->received_subvols = RB_ROOT;
427 s->path_subvols = RB_ROOT;
430 char *path_cat(const char *p1, const char *p2)
432 int p1_len = strlen(p1);
433 int p2_len = strlen(p2);
434 char *new = malloc(p1_len + p2_len + 2);
436 if (p1_len && p1[p1_len - 1] == '/')
438 if (p2_len && p2[p2_len - 1] == '/')
440 sprintf(new, "%.*s/%.*s", p1_len, p1, p2_len, p2);
445 char *path_cat3(const char *p1, const char *p2, const char *p3)
447 int p1_len = strlen(p1);
448 int p2_len = strlen(p2);
449 int p3_len = strlen(p3);
450 char *new = malloc(p1_len + p2_len + p3_len + 3);
452 if (p1_len && p1[p1_len - 1] == '/')
454 if (p2_len && p2[p2_len - 1] == '/')
456 if (p3_len && p3[p3_len - 1] == '/')
458 sprintf(new, "%.*s/%.*s/%.*s", p1_len, p1, p2_len, p2, p3_len, p3);