btrfs-progs: subvol_uuid_search: return error code on memory allocation failure
[platform/upstream/btrfs-progs.git] / send-utils.c
1 /*
2  * Copyright (C) 2012 Alexander Block.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <sys/ioctl.h>
22 #include <uuid/uuid.h>
23 #include <limits.h>
24 #include <errno.h>
25
26 #include "ctree.h"
27 #include "send-utils.h"
28 #include "ioctl.h"
29 #include "btrfs-list.h"
30
31 static int btrfs_subvolid_resolve_sub(int fd, char *path, size_t *path_len,
32                                       u64 subvol_id);
33
34 static int btrfs_get_root_id_by_sub_path(int mnt_fd, const char *sub_path,
35                                          u64 *root_id)
36 {
37         int ret;
38         int subvol_fd;
39
40         subvol_fd = openat(mnt_fd, sub_path, O_RDONLY);
41         if (subvol_fd < 0) {
42                 ret = -errno;
43                 fprintf(stderr, "ERROR: open %s failed. %s\n", sub_path,
44                         strerror(-ret));
45                 return ret;
46         }
47
48         ret = btrfs_list_get_path_rootid(subvol_fd, root_id);
49         close(subvol_fd);
50         return ret;
51 }
52
53 static int btrfs_read_root_item_raw(int mnt_fd, u64 root_id, size_t buf_len,
54                                     u32 *read_len, void *buf)
55 {
56         int ret;
57         struct btrfs_ioctl_search_args args;
58         struct btrfs_ioctl_search_key *sk = &args.key;
59         struct btrfs_ioctl_search_header *sh;
60         unsigned long off = 0;
61         int found = 0;
62         int i;
63
64         *read_len = 0;
65         memset(&args, 0, sizeof(args));
66
67         sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
68
69         /*
70          * there may be more than one ROOT_ITEM key if there are
71          * snapshots pending deletion, we have to loop through
72          * them.
73          */
74         sk->min_objectid = root_id;
75         sk->max_objectid = root_id;
76         sk->max_type = BTRFS_ROOT_ITEM_KEY;
77         sk->min_type = BTRFS_ROOT_ITEM_KEY;
78         sk->max_offset = (u64)-1;
79         sk->max_transid = (u64)-1;
80         sk->nr_items = 4096;
81
82         while (1) {
83                 ret = ioctl(mnt_fd, BTRFS_IOC_TREE_SEARCH, &args);
84                 if (ret < 0) {
85                         fprintf(stderr,
86                                 "ERROR: can't perform the search - %s\n",
87                                 strerror(errno));
88                         return 0;
89                 }
90                 /* the ioctl returns the number of item it found in nr_items */
91                 if (sk->nr_items == 0)
92                         break;
93
94                 off = 0;
95                 for (i = 0; i < sk->nr_items; i++) {
96                         struct btrfs_root_item *item;
97                         sh = (struct btrfs_ioctl_search_header *)(args.buf +
98                                                                   off);
99
100                         off += sizeof(*sh);
101                         item = (struct btrfs_root_item *)(args.buf + off);
102                         off += btrfs_search_header_len(sh);
103
104                         sk->min_objectid = btrfs_search_header_objectid(sh);
105                         sk->min_type = btrfs_search_header_type(sh);
106                         sk->min_offset = btrfs_search_header_offset(sh);
107
108                         if (btrfs_search_header_objectid(sh) > root_id)
109                                 break;
110
111                         if (btrfs_search_header_objectid(sh) == root_id &&
112                             btrfs_search_header_type(sh) == BTRFS_ROOT_ITEM_KEY) {
113                                 if (btrfs_search_header_len(sh) > buf_len) {
114                                         /* btrfs-progs is too old for kernel */
115                                         fprintf(stderr,
116                                                 "ERROR: buf for read_root_item_raw() is too small, get newer btrfs tools!\n");
117                                         return -EOVERFLOW;
118                                 }
119                                 memcpy(buf, item, btrfs_search_header_len(sh));
120                                 *read_len = btrfs_search_header_len(sh);
121                                 found = 1;
122                         }
123                 }
124                 if (sk->min_offset < (u64)-1)
125                         sk->min_offset++;
126                 else
127                         break;
128
129                 if (sk->min_type != BTRFS_ROOT_ITEM_KEY ||
130                     sk->min_objectid != root_id)
131                         break;
132         }
133
134         return found ? 0 : -ENOENT;
135 }
136
137 /*
138  * Read a root item from the tree. In case we detect a root item smaller then
139  * sizeof(root_item), we know it's an old version of the root structure and
140  * initialize all new fields to zero. The same happens if we detect mismatching
141  * generation numbers as then we know the root was once mounted with an older
142  * kernel that was not aware of the root item structure change.
143  */
144 static int btrfs_read_root_item(int mnt_fd, u64 root_id,
145                                 struct btrfs_root_item *item)
146 {
147         int ret;
148         u32 read_len;
149
150         ret = btrfs_read_root_item_raw(mnt_fd, root_id, sizeof(*item),
151                                        &read_len, item);
152         if (ret)
153                 return ret;
154
155         if (read_len < sizeof(*item) ||
156             btrfs_root_generation(item) != btrfs_root_generation_v2(item))
157                 memset(&item->generation_v2, 0,
158                         sizeof(*item) - offsetof(struct btrfs_root_item,
159                                                  generation_v2));
160
161         return 0;
162 }
163
164 #ifdef BTRFS_COMPAT_SEND_NO_UUID_TREE
165 static struct rb_node *tree_insert(struct rb_root *root,
166                                    struct subvol_info *si,
167                                    enum subvol_search_type type)
168 {
169         struct rb_node **p = &root->rb_node;
170         struct rb_node *parent = NULL;
171         struct subvol_info *entry;
172         __s64 comp;
173
174         while (*p) {
175                 parent = *p;
176                 if (type == subvol_search_by_received_uuid) {
177                         entry = rb_entry(parent, struct subvol_info,
178                                         rb_received_node);
179
180                         comp = memcmp(entry->received_uuid, si->received_uuid,
181                                         BTRFS_UUID_SIZE);
182                         if (!comp) {
183                                 if (entry->stransid < si->stransid)
184                                         comp = -1;
185                                 else if (entry->stransid > si->stransid)
186                                         comp = 1;
187                                 else
188                                         comp = 0;
189                         }
190                 } else if (type == subvol_search_by_uuid) {
191                         entry = rb_entry(parent, struct subvol_info,
192                                         rb_local_node);
193                         comp = memcmp(entry->uuid, si->uuid, BTRFS_UUID_SIZE);
194                 } else if (type == subvol_search_by_root_id) {
195                         entry = rb_entry(parent, struct subvol_info,
196                                         rb_root_id_node);
197                         comp = entry->root_id - si->root_id;
198                 } else if (type == subvol_search_by_path) {
199                         entry = rb_entry(parent, struct subvol_info,
200                                         rb_path_node);
201                         comp = strcmp(entry->path, si->path);
202                 } else {
203                         BUG();
204                 }
205
206                 if (comp < 0)
207                         p = &(*p)->rb_left;
208                 else if (comp > 0)
209                         p = &(*p)->rb_right;
210                 else
211                         return parent;
212         }
213
214         if (type == subvol_search_by_received_uuid) {
215                 rb_link_node(&si->rb_received_node, parent, p);
216                 rb_insert_color(&si->rb_received_node, root);
217         } else if (type == subvol_search_by_uuid) {
218                 rb_link_node(&si->rb_local_node, parent, p);
219                 rb_insert_color(&si->rb_local_node, root);
220         } else if (type == subvol_search_by_root_id) {
221                 rb_link_node(&si->rb_root_id_node, parent, p);
222                 rb_insert_color(&si->rb_root_id_node, root);
223         } else if (type == subvol_search_by_path) {
224                 rb_link_node(&si->rb_path_node, parent, p);
225                 rb_insert_color(&si->rb_path_node, root);
226         }
227         return NULL;
228 }
229 #endif
230
231 int btrfs_subvolid_resolve(int fd, char *path, size_t path_len, u64 subvol_id)
232 {
233         if (path_len < 1)
234                 return -EOVERFLOW;
235         path[0] = '\0';
236         path_len--;
237         path[path_len] = '\0';
238         return btrfs_subvolid_resolve_sub(fd, path, &path_len, subvol_id);
239 }
240
241 static int btrfs_subvolid_resolve_sub(int fd, char *path, size_t *path_len,
242                                       u64 subvol_id)
243 {
244         int ret;
245         struct btrfs_ioctl_search_args search_arg;
246         struct btrfs_ioctl_ino_lookup_args ino_lookup_arg;
247         struct btrfs_ioctl_search_header *search_header;
248         struct btrfs_root_ref *backref_item;
249
250         if (subvol_id == BTRFS_FS_TREE_OBJECTID) {
251                 if (*path_len < 1)
252                         return -EOVERFLOW;
253                 *path = '\0';
254                 (*path_len)--;
255                 return 0;
256         }
257
258         memset(&search_arg, 0, sizeof(search_arg));
259         search_arg.key.tree_id = BTRFS_ROOT_TREE_OBJECTID;
260         search_arg.key.min_objectid = subvol_id;
261         search_arg.key.max_objectid = subvol_id;
262         search_arg.key.min_type = BTRFS_ROOT_BACKREF_KEY;
263         search_arg.key.max_type = BTRFS_ROOT_BACKREF_KEY;
264         search_arg.key.max_offset = (u64)-1;
265         search_arg.key.max_transid = (u64)-1;
266         search_arg.key.nr_items = 1;
267         ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &search_arg);
268         if (ret < 0) {
269                 fprintf(stderr,
270                         "ioctl(BTRFS_IOC_TREE_SEARCH, subvol_id %llu) ret=%d, error: %s\n",
271                         (unsigned long long)subvol_id, ret, strerror(errno));
272                 return ret;
273         }
274
275         if (search_arg.key.nr_items < 1) {
276                 fprintf(stderr,
277                         "failed to lookup subvol_id %llu!\n",
278                         (unsigned long long)subvol_id);
279                 return -ENOENT;
280         }
281         search_header = (struct btrfs_ioctl_search_header *)search_arg.buf;
282         backref_item = (struct btrfs_root_ref *)(search_header + 1);
283         if (btrfs_search_header_offset(search_header)
284             != BTRFS_FS_TREE_OBJECTID) {
285                 int sub_ret;
286
287                 sub_ret = btrfs_subvolid_resolve_sub(fd, path, path_len,
288                                 btrfs_search_header_offset(search_header));
289                 if (sub_ret)
290                         return sub_ret;
291                 if (*path_len < 1)
292                         return -EOVERFLOW;
293                 strcat(path, "/");
294                 (*path_len)--;
295         }
296
297         if (btrfs_stack_root_ref_dirid(backref_item) !=
298             BTRFS_FIRST_FREE_OBJECTID) {
299                 int len;
300
301                 memset(&ino_lookup_arg, 0, sizeof(ino_lookup_arg));
302                 ino_lookup_arg.treeid =
303                         btrfs_search_header_offset(search_header);
304                 ino_lookup_arg.objectid =
305                         btrfs_stack_root_ref_dirid(backref_item);
306                 ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_lookup_arg);
307                 if (ret < 0) {
308                         fprintf(stderr,
309                                 "ioctl(BTRFS_IOC_INO_LOOKUP) ret=%d, error: %s\n",
310                                 ret, strerror(errno));
311                         return ret;
312                 }
313
314                 len = strlen(ino_lookup_arg.name);
315                 if (*path_len < len)
316                         return -EOVERFLOW;
317                 strcat(path, ino_lookup_arg.name);
318                 (*path_len) -= len;
319         }
320
321         if (*path_len < btrfs_stack_root_ref_name_len(backref_item))
322                 return -EOVERFLOW;
323         strncat(path, (char *)(backref_item + 1),
324                 btrfs_stack_root_ref_name_len(backref_item));
325         (*path_len) -= btrfs_stack_root_ref_name_len(backref_item);
326         return 0;
327 }
328
329 #ifdef BTRFS_COMPAT_SEND_NO_UUID_TREE
330 static int count_bytes(void *buf, int len, char b)
331 {
332         int cnt = 0;
333         int i;
334
335         for (i = 0; i < len; i++) {
336                 if (((char *)buf)[i] == b)
337                         cnt++;
338         }
339         return cnt;
340 }
341
342 void subvol_uuid_search_add(struct subvol_uuid_search *s,
343                             struct subvol_info *si)
344 {
345         int cnt;
346
347         tree_insert(&s->root_id_subvols, si, subvol_search_by_root_id);
348         tree_insert(&s->path_subvols, si, subvol_search_by_path);
349
350         cnt = count_bytes(si->uuid, BTRFS_UUID_SIZE, 0);
351         if (cnt != BTRFS_UUID_SIZE)
352                 tree_insert(&s->local_subvols, si, subvol_search_by_uuid);
353         cnt = count_bytes(si->received_uuid, BTRFS_UUID_SIZE, 0);
354         if (cnt != BTRFS_UUID_SIZE)
355                 tree_insert(&s->received_subvols, si,
356                                 subvol_search_by_received_uuid);
357 }
358
359 static struct subvol_info *tree_search(struct rb_root *root,
360                                        u64 root_id, const u8 *uuid,
361                                        u64 stransid, const char *path,
362                                        enum subvol_search_type type)
363 {
364         struct rb_node *n = root->rb_node;
365         struct subvol_info *entry;
366         __s64 comp;
367
368         while (n) {
369                 if (type == subvol_search_by_received_uuid) {
370                         entry = rb_entry(n, struct subvol_info,
371                                         rb_received_node);
372                         comp = memcmp(entry->received_uuid, uuid,
373                                         BTRFS_UUID_SIZE);
374                         if (!comp) {
375                                 if (entry->stransid < stransid)
376                                         comp = -1;
377                                 else if (entry->stransid > stransid)
378                                         comp = 1;
379                                 else
380                                         comp = 0;
381                         }
382                 } else if (type == subvol_search_by_uuid) {
383                         entry = rb_entry(n, struct subvol_info, rb_local_node);
384                         comp = memcmp(entry->uuid, uuid, BTRFS_UUID_SIZE);
385                 } else if (type == subvol_search_by_root_id) {
386                         entry = rb_entry(n, struct subvol_info,
387                                          rb_root_id_node);
388                         comp = entry->root_id - root_id;
389                 } else if (type == subvol_search_by_path) {
390                         entry = rb_entry(n, struct subvol_info, rb_path_node);
391                         comp = strcmp(entry->path, path);
392                 } else {
393                         BUG();
394                 }
395                 if (comp < 0)
396                         n = n->rb_left;
397                 else if (comp > 0)
398                         n = n->rb_right;
399                 else
400                         return entry;
401         }
402         return NULL;
403 }
404
405 /*
406  * this function will be only called if kernel doesn't support uuid tree.
407  */
408 static struct subvol_info *subvol_uuid_search_old(struct subvol_uuid_search *s,
409                                        u64 root_id, const u8 *uuid, u64 transid,
410                                        const char *path,
411                                        enum subvol_search_type type)
412 {
413         struct rb_root *root;
414         if (type == subvol_search_by_received_uuid)
415                 root = &s->received_subvols;
416         else if (type == subvol_search_by_uuid)
417                 root = &s->local_subvols;
418         else if (type == subvol_search_by_root_id)
419                 root = &s->root_id_subvols;
420         else if (type == subvol_search_by_path)
421                 root = &s->path_subvols;
422         else
423                 return NULL;
424         return tree_search(root, root_id, uuid, transid, path, type);
425 }
426 #else
427 void subvol_uuid_search_add(struct subvol_uuid_search *s,
428                             struct subvol_info *si)
429 {
430         if (si) {
431                 free(si->path);
432                 free(si);
433         }
434 }
435 #endif
436
437 struct subvol_info *subvol_uuid_search(struct subvol_uuid_search *s,
438                                        u64 root_id, const u8 *uuid, u64 transid,
439                                        const char *path,
440                                        enum subvol_search_type type)
441 {
442         int ret = 0;
443         struct btrfs_root_item root_item;
444         struct subvol_info *info = NULL;
445
446 #ifdef BTRFS_COMPAT_SEND_NO_UUID_TREE
447         if (!s->uuid_tree_existed)
448                 return subvol_uuid_search_old(s, root_id, uuid, transid,
449                                              path, type);
450 #endif
451         switch (type) {
452         case subvol_search_by_received_uuid:
453                 ret = btrfs_lookup_uuid_received_subvol_item(s->mnt_fd, uuid,
454                                                              &root_id);
455                 break;
456         case subvol_search_by_uuid:
457                 ret = btrfs_lookup_uuid_subvol_item(s->mnt_fd, uuid, &root_id);
458                 break;
459         case subvol_search_by_root_id:
460                 break;
461         case subvol_search_by_path:
462                 ret = btrfs_get_root_id_by_sub_path(s->mnt_fd, path, &root_id);
463                 break;
464         default:
465                 ret = -EINVAL;
466                 break;
467         }
468
469         if (ret)
470                 goto out;
471
472         ret = btrfs_read_root_item(s->mnt_fd, root_id, &root_item);
473         if (ret)
474                 goto out;
475
476         info = calloc(1, sizeof(*info));
477         if (!info) {
478                 ret = -ENOMEM;
479                 goto out;
480         }
481         info->root_id = root_id;
482         memcpy(info->uuid, root_item.uuid, BTRFS_UUID_SIZE);
483         memcpy(info->received_uuid, root_item.received_uuid, BTRFS_UUID_SIZE);
484         memcpy(info->parent_uuid, root_item.parent_uuid, BTRFS_UUID_SIZE);
485         info->ctransid = btrfs_root_ctransid(&root_item);
486         info->otransid = btrfs_root_otransid(&root_item);
487         info->stransid = btrfs_root_stransid(&root_item);
488         info->rtransid = btrfs_root_rtransid(&root_item);
489         if (type == subvol_search_by_path) {
490                 info->path = strdup(path);
491         } else {
492                 info->path = malloc(PATH_MAX);
493                 if (!info->path) {
494                         ret = -ENOMEM;
495                         goto out;
496                 }
497                 ret = btrfs_subvolid_resolve(s->mnt_fd, info->path,
498                                              PATH_MAX, root_id);
499         }
500
501 out:
502         if (ret) {
503                 if (info) {
504                         free(info->path);
505                         free(info);
506                 }
507                 return ERR_PTR(ret);
508         }
509
510         return info;
511 }
512
513 #ifdef BTRFS_COMPAT_SEND_NO_UUID_TREE
514 static int is_uuid_tree_supported(int fd)
515 {
516         int ret;
517         struct btrfs_ioctl_search_args args;
518         struct btrfs_ioctl_search_key *sk = &args.key;
519
520         memset(&args, 0, sizeof(args));
521
522         sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
523
524         sk->min_objectid = BTRFS_UUID_TREE_OBJECTID;
525         sk->max_objectid = BTRFS_UUID_TREE_OBJECTID;
526         sk->max_type = BTRFS_ROOT_ITEM_KEY;
527         sk->min_type = BTRFS_ROOT_ITEM_KEY;
528         sk->max_offset = (u64)-1;
529         sk->max_transid = (u64)-1;
530         sk->nr_items = 1;
531
532         ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
533         if (ret < 0)
534                 return ret;
535
536         /* the ioctl returns the number of item it found in nr_items */
537         if (sk->nr_items == 0)
538                 return 0;
539
540         return 1;
541 }
542
543 /*
544  * this function is mainly used to read all root items
545  * it will be only used when we use older kernel which uuid
546  * tree is not supported yet
547  */
548 int subvol_uuid_search_init(int mnt_fd, struct subvol_uuid_search *s)
549 {
550         int ret;
551         struct btrfs_ioctl_search_args args;
552         struct btrfs_ioctl_search_key *sk = &args.key;
553         struct btrfs_ioctl_search_header *sh;
554         struct btrfs_root_item *root_item_ptr;
555         struct btrfs_root_item root_item = {};
556         struct subvol_info *si = NULL;
557         int root_item_valid = 0;
558         unsigned long off = 0;
559         int i;
560         char *path;
561
562         s->mnt_fd = mnt_fd;
563
564         s->root_id_subvols = RB_ROOT;
565         s->local_subvols = RB_ROOT;
566         s->received_subvols = RB_ROOT;
567         s->path_subvols = RB_ROOT;
568
569         ret = is_uuid_tree_supported(mnt_fd);
570         if (ret < 0) {
571                 fprintf(stderr,
572                         "ERROR: check if we support uuid tree fails - %s\n",
573                         strerror(errno));
574                 return ret;
575         } else if (ret) {
576                 /* uuid tree is supported */
577                 s->uuid_tree_existed = 1;
578                 return 0;
579         }
580         memset(&args, 0, sizeof(args));
581
582         sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
583
584         sk->max_objectid = (u64)-1;
585         sk->max_offset = (u64)-1;
586         sk->max_transid = (u64)-1;
587         sk->min_type = BTRFS_ROOT_ITEM_KEY;
588         sk->max_type = BTRFS_ROOT_BACKREF_KEY;
589         sk->nr_items = 4096;
590
591         while (1) {
592                 ret = ioctl(mnt_fd, BTRFS_IOC_TREE_SEARCH, &args);
593                 if (ret < 0) {
594                         fprintf(stderr, "ERROR: can't perform the search - %s\n",
595                                 strerror(errno));
596                         return ret;
597                 }
598                 if (sk->nr_items == 0)
599                         break;
600
601                 off = 0;
602
603                 for (i = 0; i < sk->nr_items; i++) {
604                         sh = (struct btrfs_ioctl_search_header *)(args.buf +
605                                                                   off);
606                         off += sizeof(*sh);
607
608                         if ((btrfs_search_header_objectid(sh) != 5 &&
609                              btrfs_search_header_objectid(sh)
610                              < BTRFS_FIRST_FREE_OBJECTID) ||
611                             btrfs_search_header_objectid(sh)
612                             > BTRFS_LAST_FREE_OBJECTID) {
613                                 goto skip;
614                         }
615
616                         if (btrfs_search_header_type(sh)
617                             == BTRFS_ROOT_ITEM_KEY) {
618                                 /* older kernels don't have uuids+times */
619                                 if (btrfs_search_header_len(sh)
620                                     < sizeof(root_item)) {
621                                         root_item_valid = 0;
622                                         goto skip;
623                                 }
624                                 root_item_ptr = (struct btrfs_root_item *)
625                                                 (args.buf + off);
626                                 memcpy(&root_item, root_item_ptr,
627                                                 sizeof(root_item));
628                                 root_item_valid = 1;
629                         } else if (btrfs_search_header_type(sh)
630                                    == BTRFS_ROOT_BACKREF_KEY ||
631                                    root_item_valid) {
632                                 if (!root_item_valid)
633                                         goto skip;
634
635                                 path = btrfs_list_path_for_root(mnt_fd,
636                                         btrfs_search_header_objectid(sh));
637                                 if (!path)
638                                         path = strdup("");
639                                 if (IS_ERR(path)) {
640                                         ret = PTR_ERR(path);
641                                         fprintf(stderr, "ERROR: unable to "
642                                                         "resolve path "
643                                                         "for root %llu\n",
644                                                 btrfs_search_header_objectid(sh));
645                                         goto out;
646                                 }
647
648                                 si = calloc(1, sizeof(*si));
649                                 si->root_id = btrfs_search_header_objectid(sh);
650                                 memcpy(si->uuid, root_item.uuid,
651                                                 BTRFS_UUID_SIZE);
652                                 memcpy(si->parent_uuid, root_item.parent_uuid,
653                                                 BTRFS_UUID_SIZE);
654                                 memcpy(si->received_uuid,
655                                                 root_item.received_uuid,
656                                                 BTRFS_UUID_SIZE);
657                                 si->ctransid = btrfs_root_ctransid(&root_item);
658                                 si->otransid = btrfs_root_otransid(&root_item);
659                                 si->stransid = btrfs_root_stransid(&root_item);
660                                 si->rtransid = btrfs_root_rtransid(&root_item);
661                                 si->path = path;
662                                 subvol_uuid_search_add(s, si);
663                                 root_item_valid = 0;
664                         } else {
665                                 goto skip;
666                         }
667
668 skip:
669                         off += btrfs_search_header_len(sh);
670
671                         /*
672                          * record the mins in sk so we can make sure the
673                          * next search doesn't repeat this root
674                          */
675                         sk->min_objectid = btrfs_search_header_objectid(sh);
676                         sk->min_offset = btrfs_search_header_offset(sh);
677                         sk->min_type = btrfs_search_header_type(sh);
678                 }
679                 sk->nr_items = 4096;
680                 if (sk->min_offset < (u64)-1)
681                         sk->min_offset++;
682                 else if (sk->min_objectid < (u64)-1) {
683                         sk->min_objectid++;
684                         sk->min_offset = 0;
685                         sk->min_type = 0;
686                 } else
687                         break;
688         }
689
690 out:
691         return ret;
692 }
693
694 void subvol_uuid_search_finit(struct subvol_uuid_search *s)
695 {
696         struct rb_root *root = &s->root_id_subvols;
697         struct rb_node *node;
698
699         if (!s->uuid_tree_existed)
700                 return;
701
702         while ((node = rb_first(root))) {
703                 struct subvol_info *entry =
704                         rb_entry(node, struct subvol_info, rb_root_id_node);
705
706                 free(entry->path);
707                 rb_erase(node, root);
708                 free(entry);
709         }
710
711         s->root_id_subvols = RB_ROOT;
712         s->local_subvols = RB_ROOT;
713         s->received_subvols = RB_ROOT;
714         s->path_subvols = RB_ROOT;
715 }
716 #else
717 int subvol_uuid_search_init(int mnt_fd, struct subvol_uuid_search *s)
718 {
719         s->mnt_fd = mnt_fd;
720
721         return 0;
722 }
723
724 void subvol_uuid_search_finit(struct subvol_uuid_search *s)
725 {
726 }
727 #endif
728
729 int path_cat_out(char *out, const char *p1, const char *p2)
730 {
731         int p1_len = strlen(p1);
732         int p2_len = strlen(p2);
733
734         if (p1_len + p2_len + 2 >= PATH_MAX)
735                 return -ENAMETOOLONG;
736
737         if (p1_len && p1[p1_len - 1] == '/')
738                 p1_len--;
739         if (p2_len && p2[p2_len - 1] == '/')
740                 p2_len--;
741         sprintf(out, "%.*s/%.*s", p1_len, p1, p2_len, p2);
742
743         return 0;
744 }
745
746 __attribute__((deprecated))
747 char *path_cat(const char *p1, const char *p2)
748 {
749         int p1_len = strlen(p1);
750         int p2_len = strlen(p2);
751         char *new = malloc(p1_len + p2_len + 2);
752
753         path_cat_out(new, p1, p2);
754
755         return new;
756 }
757
758 int path_cat3_out(char *out, const char *p1, const char *p2, const char *p3)
759 {
760         int p1_len = strlen(p1);
761         int p2_len = strlen(p2);
762         int p3_len = strlen(p3);
763
764         if (p1_len + p2_len + p3_len + 3 >= PATH_MAX)
765                 return -ENAMETOOLONG;
766
767         if (p1_len && p1[p1_len - 1] == '/')
768                 p1_len--;
769         if (p2_len && p2[p2_len - 1] == '/')
770                 p2_len--;
771         if (p3_len && p3[p3_len - 1] == '/')
772                 p3_len--;
773         sprintf(out, "%.*s/%.*s/%.*s", p1_len, p1, p2_len, p2, p3_len, p3);
774
775         return 0;
776 }
777
778 __attribute__((deprecated))
779 char *path_cat3(const char *p1, const char *p2, const char *p3)
780 {
781         int p1_len = strlen(p1);
782         int p2_len = strlen(p2);
783         int p3_len = strlen(p3);
784         char *new = malloc(p1_len + p2_len + p3_len + 3);
785
786         path_cat3_out(new, p1, p2, p3);
787
788         return new;
789 }