Btrfs-progs: try other mirrors if decompression fails
[platform/upstream/btrfs-progs.git] / cmds-restore.c
1 /*
2  * Copyright (C) 2011 Red Hat.  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 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21
22 #include "kerncompat.h"
23
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <zlib.h>
31
32 #include "ctree.h"
33 #include "disk-io.h"
34 #include "print-tree.h"
35 #include "transaction.h"
36 #include "list.h"
37 #include "version.h"
38 #include "volumes.h"
39 #include "utils.h"
40 #include "commands.h"
41
42 static char path_name[4096];
43 static int get_snaps = 0;
44 static int verbose = 0;
45 static int ignore_errors = 0;
46 static int overwrite = 0;
47
48 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
49                       u64 decompress_len)
50 {
51         z_stream strm;
52         int ret;
53
54         memset(&strm, 0, sizeof(strm));
55         ret = inflateInit(&strm);
56         if (ret != Z_OK) {
57                 fprintf(stderr, "inflate init returnd %d\n", ret);
58                 return -1;
59         }
60
61         strm.avail_in = compress_len;
62         strm.next_in = (unsigned char *)inbuf;
63         strm.avail_out = decompress_len;
64         strm.next_out = (unsigned char *)outbuf;
65         ret = inflate(&strm, Z_NO_FLUSH);
66         if (ret != Z_STREAM_END) {
67                 (void)inflateEnd(&strm);
68                 fprintf(stderr, "failed to inflate: %d\n", ret);
69                 return -1;
70         }
71
72         (void)inflateEnd(&strm);
73         return 0;
74 }
75
76 int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
77 {
78         int slot;
79         int level = 1;
80         struct extent_buffer *c;
81         struct extent_buffer *next = NULL;
82
83         for (; level < BTRFS_MAX_LEVEL; level++) {
84                 if (path->nodes[level])
85                         break;
86         }
87
88         if (level == BTRFS_MAX_LEVEL)
89                 return 1;
90
91         slot = path->slots[level] + 1;
92
93         while(level < BTRFS_MAX_LEVEL) {
94                 if (!path->nodes[level])
95                         return 1;
96
97                 slot = path->slots[level] + 1;
98                 c = path->nodes[level];
99                 if (slot >= btrfs_header_nritems(c)) {
100                         level++;
101                         if (level == BTRFS_MAX_LEVEL)
102                                 return 1;
103                         continue;
104                 }
105
106                 if (path->reada)
107                         reada_for_search(root, path, level, slot, 0);
108
109                 next = read_node_slot(root, c, slot);
110                 break;
111         }
112         path->slots[level] = slot;
113         while(1) {
114                 level--;
115                 c = path->nodes[level];
116                 free_extent_buffer(c);
117                 path->nodes[level] = next;
118                 path->slots[level] = 0;
119                 if (!level)
120                         break;
121                 if (path->reada)
122                         reada_for_search(root, path, level, 0, 0);
123                 next = read_node_slot(root, next, 0);
124         }
125         return 0;
126 }
127
128 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
129 {
130         struct extent_buffer *leaf = path->nodes[0];
131         struct btrfs_file_extent_item *fi;
132         char buf[4096];
133         char *outbuf;
134         ssize_t done;
135         unsigned long ptr;
136         int ret;
137         int len;
138         int ram_size;
139         int compress;
140
141         fi = btrfs_item_ptr(leaf, path->slots[0],
142                             struct btrfs_file_extent_item);
143         ptr = btrfs_file_extent_inline_start(fi);
144         len = btrfs_file_extent_inline_item_len(leaf,
145                                         btrfs_item_nr(leaf, path->slots[0]));
146         read_extent_buffer(leaf, buf, ptr, len);
147
148         compress = btrfs_file_extent_compression(leaf, fi);
149         if (compress == BTRFS_COMPRESS_NONE) {
150                 done = pwrite(fd, buf, len, pos);
151                 if (done < len) {
152                         fprintf(stderr, "Short inline write, wanted %d, did "
153                                 "%zd: %d\n", len, done, errno);
154                         return -1;
155                 }
156                 return 0;
157         }
158
159         ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
160         outbuf = malloc(ram_size);
161         if (!outbuf) {
162                 fprintf(stderr, "No memory\n");
163                 return -1;
164         }
165
166         ret = decompress(buf, outbuf, len, ram_size);
167         if (ret) {
168                 free(outbuf);
169                 return ret;
170         }
171
172         done = pwrite(fd, outbuf, ram_size, pos);
173         free(outbuf);
174         if (done < ram_size) {
175                 fprintf(stderr, "Short compressed inline write, wanted %d, "
176                         "did %zd: %d\n", ram_size, done, errno);
177                 return -1;
178         }
179
180         return 0;
181 }
182
183 static int copy_one_extent(struct btrfs_root *root, int fd,
184                            struct extent_buffer *leaf,
185                            struct btrfs_file_extent_item *fi, u64 pos)
186 {
187         struct btrfs_multi_bio *multi = NULL;
188         struct btrfs_device *device;
189         char *inbuf, *outbuf = NULL;
190         ssize_t done, total = 0;
191         u64 bytenr;
192         u64 ram_size;
193         u64 disk_size;
194         u64 length;
195         u64 size_left;
196         u64 dev_bytenr;
197         u64 count = 0;
198         int compress;
199         int ret;
200         int dev_fd;
201         int mirror_num = 0;
202         int num_copies;
203
204         compress = btrfs_file_extent_compression(leaf, fi);
205         bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
206         disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
207         ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
208         size_left = disk_size;
209
210         /* we found a hole */
211         if (disk_size == 0)
212                 return 0;
213
214         inbuf = malloc(disk_size);
215         if (!inbuf) {
216                 fprintf(stderr, "No memory\n");
217                 return -1;
218         }
219
220         if (compress != BTRFS_COMPRESS_NONE) {
221                 outbuf = malloc(ram_size);
222                 if (!outbuf) {
223                         fprintf(stderr, "No memory\n");
224                         free(inbuf);
225                         return -1;
226                 }
227         }
228 again:
229         length = size_left;
230         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
231                               bytenr, &length, &multi, mirror_num, NULL);
232         if (ret) {
233                 fprintf(stderr, "Error mapping block %d\n", ret);
234                 goto out;
235         }
236         device = multi->stripes[0].dev;
237         dev_fd = device->fd;
238         device->total_ios++;
239         dev_bytenr = multi->stripes[0].physical;
240         kfree(multi);
241
242         if (size_left < length)
243                 length = size_left;
244         size_left -= length;
245
246         done = pread(dev_fd, inbuf+count, length, dev_bytenr);
247         if (done < length) {
248                 ret = -1;
249                 fprintf(stderr, "Short read %d\n", errno);
250                 goto out;
251         }
252
253         count += length;
254         bytenr += length;
255         if (size_left)
256                 goto again;
257
258         if (compress == BTRFS_COMPRESS_NONE) {
259                 while (total < ram_size) {
260                         done = pwrite(fd, inbuf+total, ram_size-total,
261                                       pos+total);
262                         if (done < 0) {
263                                 ret = -1;
264                                 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
265                                 goto out;
266                         }
267                         total += done;
268                 }
269                 ret = 0;
270                 goto out;
271         }
272
273         ret = decompress(inbuf, outbuf, disk_size, ram_size);
274         if (ret) {
275                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
276                                               bytenr, length);
277                 mirror_num++;
278                 if (mirror_num >= num_copies) {
279                         ret = -1;
280                         goto out;
281                 }
282                 fprintf(stderr, "Trying another mirror\n");
283                 goto again;
284         }
285
286         while (total < ram_size) {
287                 done = pwrite(fd, outbuf+total, ram_size-total, pos+total);
288                 if (done < 0) {
289                         ret = -1;
290                         goto out;
291                 }
292                 total += done;
293         }
294 out:
295         free(inbuf);
296         free(outbuf);
297         return ret;
298 }
299
300 static int ask_to_continue(const char *file)
301 {
302         char buf[2];
303         char *ret;
304
305         printf("We seem to be looping a lot on %s, do you want to keep going "
306                "on ? (y/N): ", file);
307 again:
308         ret = fgets(buf, 2, stdin);
309         if (*ret == '\n' || tolower(*ret) == 'n')
310                 return 1;
311         if (tolower(*ret) != 'y') {
312                 printf("Please enter either 'y' or 'n': ");
313                 goto again;
314         }
315
316         return 0;
317 }
318
319
320 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
321                      const char *file)
322 {
323         struct extent_buffer *leaf;
324         struct btrfs_path *path;
325         struct btrfs_file_extent_item *fi;
326         struct btrfs_inode_item *inode_item;
327         struct btrfs_key found_key;
328         int ret;
329         int extent_type;
330         int compression;
331         int loops = 0;
332         u64 found_size = 0;
333
334         path = btrfs_alloc_path();
335         if (!path) {
336                 fprintf(stderr, "Ran out of memory\n");
337                 return -1;
338         }
339         path->skip_locking = 1;
340
341         ret = btrfs_lookup_inode(NULL, root, path, key, 0);
342         if (ret == 0) {
343                 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
344                                     struct btrfs_inode_item);
345                 found_size = btrfs_inode_size(path->nodes[0], inode_item);
346         }
347         btrfs_release_path(root, path);
348
349         key->offset = 0;
350         key->type = BTRFS_EXTENT_DATA_KEY;
351
352         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
353         if (ret < 0) {
354                 fprintf(stderr, "Error searching %d\n", ret);
355                 btrfs_free_path(path);
356                 return ret;
357         }
358
359         leaf = path->nodes[0];
360         while (!leaf) {
361                 ret = next_leaf(root, path);
362                 if (ret < 0) {
363                         fprintf(stderr, "Error getting next leaf %d\n",
364                                 ret);
365                         btrfs_free_path(path);
366                         return ret;
367                 } else if (ret > 0) {
368                         /* No more leaves to search */
369                         btrfs_free_path(path);
370                         return 0;
371                 }
372                 leaf = path->nodes[0];
373         }
374
375         while (1) {
376                 if (loops++ >= 1024) {
377                         ret = ask_to_continue(file);
378                         if (ret)
379                                 break;
380                         loops = 0;
381                 }
382                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
383                         do {
384                                 ret = next_leaf(root, path);
385                                 if (ret < 0) {
386                                         fprintf(stderr, "Error searching %d\n", ret);
387                                         btrfs_free_path(path);
388                                         return ret;
389                                 } else if (ret) {
390                                         /* No more leaves to search */
391                                         btrfs_free_path(path);
392                                         goto set_size;
393                                 }
394                                 leaf = path->nodes[0];
395                         } while (!leaf);
396                         continue;
397                 }
398                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
399                 if (found_key.objectid != key->objectid)
400                         break;
401                 if (found_key.type != key->type)
402                         break;
403                 fi = btrfs_item_ptr(leaf, path->slots[0],
404                                     struct btrfs_file_extent_item);
405                 extent_type = btrfs_file_extent_type(leaf, fi);
406                 compression = btrfs_file_extent_compression(leaf, fi);
407                 if (compression >= BTRFS_COMPRESS_LAST) {
408                         fprintf(stderr, "Don't support compression yet %d\n",
409                                 compression);
410                         btrfs_free_path(path);
411                         return -1;
412                 }
413
414                 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
415                         goto next;
416                 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
417                         ret = copy_one_inline(fd, path, found_key.offset);
418                         if (ret) {
419                                 btrfs_free_path(path);
420                                 return -1;
421                         }
422                 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
423                         ret = copy_one_extent(root, fd, leaf, fi,
424                                               found_key.offset);
425                         if (ret) {
426                                 btrfs_free_path(path);
427                                 return ret;
428                         }
429                 } else {
430                         printf("Weird extent type %d\n", extent_type);
431                 }
432 next:
433                 path->slots[0]++;
434         }
435
436         btrfs_free_path(path);
437 set_size:
438         if (found_size) {
439                 ret = ftruncate(fd, (loff_t)found_size);
440                 if (ret)
441                         return ret;
442         }
443         return 0;
444 }
445
446 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
447                       const char *dir)
448 {
449         struct btrfs_path *path;
450         struct extent_buffer *leaf;
451         struct btrfs_dir_item *dir_item;
452         struct btrfs_key found_key, location;
453         char filename[BTRFS_NAME_LEN + 1];
454         unsigned long name_ptr;
455         int name_len;
456         int ret;
457         int fd;
458         int loops = 0;
459         u8 type;
460
461         path = btrfs_alloc_path();
462         if (!path) {
463                 fprintf(stderr, "Ran out of memory\n");
464                 return -1;
465         }
466         path->skip_locking = 1;
467
468         key->offset = 0;
469         key->type = BTRFS_DIR_INDEX_KEY;
470
471         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
472         if (ret < 0) {
473                 fprintf(stderr, "Error searching %d\n", ret);
474                 btrfs_free_path(path);
475                 return ret;
476         }
477
478         leaf = path->nodes[0];
479         while (!leaf) {
480                 if (verbose > 1)
481                         printf("No leaf after search, looking for the next "
482                                "leaf\n");
483                 ret = next_leaf(root, path);
484                 if (ret < 0) {
485                         fprintf(stderr, "Error getting next leaf %d\n",
486                                 ret);
487                         btrfs_free_path(path);
488                         return ret;
489                 } else if (ret > 0) {
490                         /* No more leaves to search */
491                         if (verbose)
492                                 printf("Reached the end of the tree looking "
493                                        "for the directory\n");
494                         btrfs_free_path(path);
495                         return 0;
496                 }
497                 leaf = path->nodes[0];
498         }
499
500         while (leaf) {
501                 if (loops++ >= 1024) {
502                         printf("We have looped trying to restore files in %s "
503                                "too many times to be making progress, "
504                                "stopping\n", dir);
505                         break;
506                 }
507
508                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
509                         do {
510                                 ret = next_leaf(root, path);
511                                 if (ret < 0) {
512                                         fprintf(stderr, "Error searching %d\n",
513                                                 ret);
514                                         btrfs_free_path(path);
515                                         return ret;
516                                 } else if (ret > 0) {
517                                         /* No more leaves to search */
518                                         if (verbose)
519                                                 printf("Reached the end of "
520                                                        "the tree searching the"
521                                                        " directory\n");
522                                         btrfs_free_path(path);
523                                         return 0;
524                                 }
525                                 leaf = path->nodes[0];
526                         } while (!leaf);
527                         continue;
528                 }
529                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
530                 if (found_key.objectid != key->objectid) {
531                         if (verbose > 1)
532                                 printf("Found objectid=%Lu, key=%Lu\n",
533                                        found_key.objectid, key->objectid);
534                         break;
535                 }
536                 if (found_key.type != key->type) {
537                         if (verbose > 1)
538                                 printf("Found type=%u, want=%u\n",
539                                        found_key.type, key->type);
540                         break;
541                 }
542                 dir_item = btrfs_item_ptr(leaf, path->slots[0],
543                                           struct btrfs_dir_item);
544                 name_ptr = (unsigned long)(dir_item + 1);
545                 name_len = btrfs_dir_name_len(leaf, dir_item);
546                 read_extent_buffer(leaf, filename, name_ptr, name_len);
547                 filename[name_len] = '\0';
548                 type = btrfs_dir_type(leaf, dir_item);
549                 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
550
551                 snprintf(path_name, 4096, "%s/%s", dir, filename);
552
553
554                 /*
555                  * At this point we're only going to restore directories and
556                  * files, no symlinks or anything else.
557                  */
558                 if (type == BTRFS_FT_REG_FILE) {
559                         if (!overwrite) {
560                                 static int warn = 0;
561                                 struct stat st;
562
563                                 ret = stat(path_name, &st);
564                                 if (!ret) {
565                                         loops = 0;
566                                         if (verbose || !warn)
567                                                 printf("Skipping existing file"
568                                                        " %s\n", path_name);
569                                         if (warn)
570                                                 goto next;
571                                         printf("If you wish to overwrite use "
572                                                "the -o option to overwrite\n");
573                                         warn = 1;
574                                         goto next;
575                                 }
576                                 ret = 0;
577                         }
578                         if (verbose)
579                                 printf("Restoring %s\n", path_name);
580                         fd = open(path_name, O_CREAT|O_WRONLY, 0644);
581                         if (fd < 0) {
582                                 fprintf(stderr, "Error creating %s: %d\n",
583                                         path_name, errno);
584                                 if (ignore_errors)
585                                         goto next;
586                                 btrfs_free_path(path);
587                                 return -1;
588                         }
589                         loops = 0;
590                         ret = copy_file(root, fd, &location, path_name);
591                         close(fd);
592                         if (ret) {
593                                 if (ignore_errors)
594                                         goto next;
595                                 btrfs_free_path(path);
596                                 return ret;
597                         }
598                 } else if (type == BTRFS_FT_DIR) {
599                         struct btrfs_root *search_root = root;
600                         char *dir = strdup(path_name);
601
602                         if (!dir) {
603                                 fprintf(stderr, "Ran out of memory\n");
604                                 btrfs_free_path(path);
605                                 return -1;
606                         }
607
608                         if (location.type == BTRFS_ROOT_ITEM_KEY) {
609                                 /*
610                                  * If we are a snapshot and this is the index
611                                  * object to ourselves just skip it.
612                                  */
613                                 if (location.objectid ==
614                                     root->root_key.objectid) {
615                                         free(dir);
616                                         goto next;
617                                 }
618
619                                 search_root = btrfs_read_fs_root(root->fs_info,
620                                                                  &location);
621                                 if (IS_ERR(search_root)) {
622                                         free(dir);
623                                         fprintf(stderr, "Error reading "
624                                                 "subvolume %s: %lu\n",
625                                                 path_name,
626                                                 PTR_ERR(search_root));
627                                         if (ignore_errors)
628                                                 goto next;
629                                         btrfs_free_path(path);
630                                         return PTR_ERR(search_root);
631                                 }
632
633                                 /*
634                                  * A subvolume will have a key.offset of 0, a
635                                  * snapshot will have key.offset of a transid.
636                                  */
637                                 if (search_root->root_key.offset != 0 &&
638                                     get_snaps == 0) {
639                                         free(dir);
640                                         printf("Skipping snapshot %s\n",
641                                                filename);
642                                         goto next;
643                                 }
644                                 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
645                         }
646
647                         if (verbose)
648                                 printf("Restoring %s\n", path_name);
649
650                         errno = 0;
651                         ret = mkdir(path_name, 0755);
652                         if (ret && errno != EEXIST) {
653                                 free(dir);
654                                 fprintf(stderr, "Error mkdiring %s: %d\n",
655                                         path_name, errno);
656                                 if (ignore_errors)
657                                         goto next;
658                                 btrfs_free_path(path);
659                                 return -1;
660                         }
661                         loops = 0;
662                         ret = search_dir(search_root, &location, dir);
663                         free(dir);
664                         if (ret) {
665                                 if (ignore_errors)
666                                         goto next;
667                                 btrfs_free_path(path);
668                                 return ret;
669                         }
670                 }
671 next:
672                 path->slots[0]++;
673         }
674
675         if (verbose)
676                 printf("Done searching %s\n", dir);
677         btrfs_free_path(path);
678         return 0;
679 }
680
681 static struct btrfs_root *open_fs(const char *dev, u64 root_location, int super_mirror)
682 {
683         struct btrfs_root *root;
684         u64 bytenr;
685         int i;
686
687         for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
688                 bytenr = btrfs_sb_offset(i);
689                 root = open_ctree_recovery(dev, bytenr, root_location);
690                 if (root)
691                         return root;
692                 fprintf(stderr, "Could not open root, trying backup super\n");
693         }
694
695         return NULL;
696 }
697
698 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
699 {
700         struct btrfs_path *path;
701         struct btrfs_key found_key;
702         struct btrfs_key key;
703         int ret = -1;
704         int i;
705
706         key.objectid = 0;
707         key.type = BTRFS_DIR_INDEX_KEY;
708         key.offset = 0;
709
710         path = btrfs_alloc_path();
711         if (!path) {
712                 fprintf(stderr, "Ran out of memory\n");
713                 return ret;
714         }
715
716         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
717         if (ret < 0) {
718                 fprintf(stderr, "Error searching %d\n", ret);
719                 goto out;
720         }
721
722         if (!path->nodes[0]) {
723                 fprintf(stderr, "No leaf!\n");
724                 goto out;
725         }
726 again:
727         for (i = path->slots[0];
728              i < btrfs_header_nritems(path->nodes[0]); i++) {
729                 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
730                 if (found_key.type != key.type)
731                         continue;
732
733                 printf("Using objectid %Lu for first dir\n",
734                        found_key.objectid);
735                 *objectid = found_key.objectid;
736                 ret = 0;
737                 goto out;
738         }
739         do {
740                 ret = next_leaf(root, path);
741                 if (ret < 0) {
742                         fprintf(stderr, "Error getting next leaf %d\n",
743                                 ret);
744                         goto out;
745                 } else if (ret > 0) {
746                         fprintf(stderr, "No more leaves\n");
747                         goto out;
748                 }
749         } while (!path->nodes[0]);
750         if (path->nodes[0])
751                 goto again;
752         printf("Couldn't find a dir index item\n");
753 out:
754         btrfs_free_path(path);
755         return ret;
756 }
757
758 const char * const cmd_restore_usage[] = {
759         "btrfs restore [options] <device>",
760         "Try to restore files from a damaged filesystem (unmounted)",
761         "",
762         "-s              get snapshots",
763         "-v              verbose",
764         "-i              ignore errors",
765         "-o              overwrite",
766         "-t              tree location",
767         "-f <offset>     filesystem location",
768         "-u <block>      super mirror",
769         "-d              find dir",
770         NULL
771 };
772
773 int cmd_restore(int argc, char **argv)
774 {
775         struct btrfs_root *root;
776         struct btrfs_key key;
777         char dir_name[128];
778         u64 tree_location = 0;
779         u64 fs_location = 0;
780         u64 root_objectid = 0;
781         int len;
782         int ret;
783         int opt;
784         int super_mirror = 0;
785         int find_dir = 0;
786
787         while ((opt = getopt(argc, argv, "sviot:u:df:r:")) != -1) {
788                 switch (opt) {
789                         case 's':
790                                 get_snaps = 1;
791                                 break;
792                         case 'v':
793                                 verbose++;
794                                 break;
795                         case 'i':
796                                 ignore_errors = 1;
797                                 break;
798                         case 'o':
799                                 overwrite = 1;
800                                 break;
801                         case 't':
802                                 errno = 0;
803                                 tree_location = (u64)strtoll(optarg, NULL, 10);
804                                 if (errno != 0) {
805                                         fprintf(stderr, "Tree location not valid\n");
806                                         exit(1);
807                                 }
808                                 break;
809                         case 'f':
810                                 errno = 0;
811                                 fs_location = (u64)strtoll(optarg, NULL, 10);
812                                 if (errno != 0) {
813                                         fprintf(stderr, "Fs location not valid\n");
814                                         exit(1);
815                                 }
816                                 break;
817                         case 'u':
818                                 errno = 0;
819                                 super_mirror = (int)strtol(optarg, NULL, 10);
820                                 if (errno != 0 ||
821                                     super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
822                                         fprintf(stderr, "Super mirror not "
823                                                 "valid\n");
824                                         exit(1);
825                                 }
826                                 break;
827                         case 'd':
828                                 find_dir = 1;
829                                 break;
830                         case 'r':
831                                 errno = 0;
832                                 root_objectid = (u64)strtoll(optarg, NULL, 10);
833                                 if (errno != 0) {
834                                         fprintf(stderr, "Root objectid not valid\n");
835                                         exit(1);
836                                 }
837                                 break;
838                         default:
839                                 usage(cmd_restore_usage);
840                 }
841         }
842
843         if (optind + 1 >= argc)
844                 usage(cmd_restore_usage);
845
846         if ((ret = check_mounted(argv[optind])) < 0) {
847                 fprintf(stderr, "Could not check mount status: %s\n",
848                         strerror(-ret));
849                 return ret;
850         } else if (ret) {
851                 fprintf(stderr, "%s is currently mounted.  Aborting.\n", argv[optind]);
852                 return 1;
853         }
854
855         root = open_fs(argv[optind], tree_location, super_mirror);
856         if (root == NULL)
857                 return 1;
858
859         if (fs_location != 0) {
860                 free_extent_buffer(root->node);
861                 root->node = read_tree_block(root, fs_location, 4096, 0);
862                 if (!root->node) {
863                         fprintf(stderr, "Failed to read fs location\n");
864                         goto out;
865                 }
866         }
867
868         memset(path_name, 0, 4096);
869
870         strncpy(dir_name, argv[optind + 1], sizeof dir_name);
871         dir_name[sizeof dir_name - 1] = 0;
872
873         /* Strip the trailing / on the dir name */
874         len = strlen(dir_name);
875         while (len && dir_name[--len] == '/') {
876                 dir_name[len] = '\0';
877         }
878
879         if (root_objectid != 0) {
880                 struct btrfs_root *orig_root = root;
881
882                 key.objectid = root_objectid;
883                 key.type = BTRFS_ROOT_ITEM_KEY;
884                 key.offset = (u64)-1;
885                 root = btrfs_read_fs_root(orig_root->fs_info, &key);
886                 if (IS_ERR(root)) {
887                         fprintf(stderr, "Error reading root\n");
888                         root = orig_root;
889                         ret = 1;
890                         goto out;
891                 }
892                 key.type = 0;
893                 key.offset = 0;
894         }
895
896         if (find_dir) {
897                 ret = find_first_dir(root, &key.objectid);
898                 if (ret)
899                         goto out;
900         } else {
901                 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
902         }
903
904         ret = search_dir(root, &key, dir_name);
905
906 out:
907         close_ctree(root);
908         return ret;
909 }