Btrfs-progs: add an option for specifying the root to restore
[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, "ret is %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
202         compress = btrfs_file_extent_compression(leaf, fi);
203         bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
204         disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
205         ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
206         size_left = disk_size;
207
208         /* we found a hole */
209         if (disk_size == 0)
210                 return 0;
211
212         inbuf = malloc(disk_size);
213         if (!inbuf) {
214                 fprintf(stderr, "No memory\n");
215                 return -1;
216         }
217
218         if (compress != BTRFS_COMPRESS_NONE) {
219                 outbuf = malloc(ram_size);
220                 if (!outbuf) {
221                         fprintf(stderr, "No memory\n");
222                         free(inbuf);
223                         return -1;
224                 }
225         }
226 again:
227         length = size_left;
228         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
229                               bytenr, &length, &multi, 0, NULL);
230         if (ret) {
231                 free(inbuf);
232                 free(outbuf);
233                 fprintf(stderr, "Error mapping block %d\n", ret);
234                 return ret;
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                 free(inbuf);
249                 free(outbuf);
250                 fprintf(stderr, "Short read %d\n", errno);
251                 return -1;
252         }
253
254         count += length;
255         bytenr += length;
256         if (size_left)
257                 goto again;
258
259
260         if (compress == BTRFS_COMPRESS_NONE) {
261                 while (total < ram_size) {
262                         done = pwrite(fd, inbuf+total, ram_size-total,
263                                       pos+total);
264                         if (done < 0) {
265                                 free(inbuf);
266                                 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
267                                 return -1;
268                         }
269                         total += done;
270                 }
271                 free(inbuf);
272                 return 0;
273         }
274
275         ret = decompress(inbuf, outbuf, disk_size, ram_size);
276         free(inbuf);
277         if (ret) {
278                 free(outbuf);
279                 return ret;
280         }
281
282         while (total < ram_size) {
283                 done = pwrite(fd, outbuf+total, ram_size-total, pos+total);
284                 if (done < 0) {
285                         free(outbuf);
286                         fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
287                         return -1;
288                 }
289                 total += done;
290         }
291         free(outbuf);
292
293         return 0;
294 }
295
296 static int ask_to_continue(const char *file)
297 {
298         char buf[2];
299         char *ret;
300
301         printf("We seem to be looping a lot on %s, do you want to keep going "
302                "on ? (y/N): ", file);
303 again:
304         ret = fgets(buf, 2, stdin);
305         if (*ret == '\n' || tolower(*ret) == 'n')
306                 return 1;
307         if (tolower(*ret) != 'y') {
308                 printf("Please enter either 'y' or 'n': ");
309                 goto again;
310         }
311
312         return 0;
313 }
314
315
316 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
317                      const char *file)
318 {
319         struct extent_buffer *leaf;
320         struct btrfs_path *path;
321         struct btrfs_file_extent_item *fi;
322         struct btrfs_inode_item *inode_item;
323         struct btrfs_key found_key;
324         int ret;
325         int extent_type;
326         int compression;
327         int loops = 0;
328         u64 found_size = 0;
329
330         path = btrfs_alloc_path();
331         if (!path) {
332                 fprintf(stderr, "Ran out of memory\n");
333                 return -1;
334         }
335         path->skip_locking = 1;
336
337         ret = btrfs_lookup_inode(NULL, root, path, key, 0);
338         if (ret == 0) {
339                 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
340                                     struct btrfs_inode_item);
341                 found_size = btrfs_inode_size(path->nodes[0], inode_item);
342         }
343         btrfs_release_path(root, path);
344
345         key->offset = 0;
346         key->type = BTRFS_EXTENT_DATA_KEY;
347
348         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
349         if (ret < 0) {
350                 fprintf(stderr, "Error searching %d\n", ret);
351                 btrfs_free_path(path);
352                 return ret;
353         }
354
355         leaf = path->nodes[0];
356         while (!leaf) {
357                 ret = next_leaf(root, path);
358                 if (ret < 0) {
359                         fprintf(stderr, "Error getting next leaf %d\n",
360                                 ret);
361                         btrfs_free_path(path);
362                         return ret;
363                 } else if (ret > 0) {
364                         /* No more leaves to search */
365                         btrfs_free_path(path);
366                         return 0;
367                 }
368                 leaf = path->nodes[0];
369         }
370
371         while (1) {
372                 if (loops++ >= 1024) {
373                         ret = ask_to_continue(file);
374                         if (ret)
375                                 break;
376                         loops = 0;
377                 }
378                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
379                         do {
380                                 ret = next_leaf(root, path);
381                                 if (ret < 0) {
382                                         fprintf(stderr, "Error searching %d\n", ret);
383                                         btrfs_free_path(path);
384                                         return ret;
385                                 } else if (ret) {
386                                         /* No more leaves to search */
387                                         btrfs_free_path(path);
388                                         goto set_size;
389                                 }
390                                 leaf = path->nodes[0];
391                         } while (!leaf);
392                         continue;
393                 }
394                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
395                 if (found_key.objectid != key->objectid)
396                         break;
397                 if (found_key.type != key->type)
398                         break;
399                 fi = btrfs_item_ptr(leaf, path->slots[0],
400                                     struct btrfs_file_extent_item);
401                 extent_type = btrfs_file_extent_type(leaf, fi);
402                 compression = btrfs_file_extent_compression(leaf, fi);
403                 if (compression >= BTRFS_COMPRESS_LAST) {
404                         fprintf(stderr, "Don't support compression yet %d\n",
405                                 compression);
406                         btrfs_free_path(path);
407                         return -1;
408                 }
409
410                 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
411                         goto next;
412                 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
413                         ret = copy_one_inline(fd, path, found_key.offset);
414                         if (ret) {
415                                 btrfs_free_path(path);
416                                 return -1;
417                         }
418                 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
419                         ret = copy_one_extent(root, fd, leaf, fi,
420                                               found_key.offset);
421                         if (ret) {
422                                 btrfs_free_path(path);
423                                 return ret;
424                         }
425                 } else {
426                         printf("Weird extent type %d\n", extent_type);
427                 }
428 next:
429                 path->slots[0]++;
430         }
431
432         btrfs_free_path(path);
433 set_size:
434         if (found_size) {
435                 ret = ftruncate(fd, (loff_t)found_size);
436                 if (ret)
437                         return ret;
438         }
439         return 0;
440 }
441
442 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
443                       const char *dir)
444 {
445         struct btrfs_path *path;
446         struct extent_buffer *leaf;
447         struct btrfs_dir_item *dir_item;
448         struct btrfs_key found_key, location;
449         char filename[BTRFS_NAME_LEN + 1];
450         unsigned long name_ptr;
451         int name_len;
452         int ret;
453         int fd;
454         int loops = 0;
455         u8 type;
456
457         path = btrfs_alloc_path();
458         if (!path) {
459                 fprintf(stderr, "Ran out of memory\n");
460                 return -1;
461         }
462         path->skip_locking = 1;
463
464         key->offset = 0;
465         key->type = BTRFS_DIR_INDEX_KEY;
466
467         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
468         if (ret < 0) {
469                 fprintf(stderr, "Error searching %d\n", ret);
470                 btrfs_free_path(path);
471                 return ret;
472         }
473
474         leaf = path->nodes[0];
475         while (!leaf) {
476                 if (verbose > 1)
477                         printf("No leaf after search, looking for the next "
478                                "leaf\n");
479                 ret = next_leaf(root, path);
480                 if (ret < 0) {
481                         fprintf(stderr, "Error getting next leaf %d\n",
482                                 ret);
483                         btrfs_free_path(path);
484                         return ret;
485                 } else if (ret > 0) {
486                         /* No more leaves to search */
487                         if (verbose)
488                                 printf("Reached the end of the tree looking "
489                                        "for the directory\n");
490                         btrfs_free_path(path);
491                         return 0;
492                 }
493                 leaf = path->nodes[0];
494         }
495
496         while (leaf) {
497                 if (loops++ >= 1024) {
498                         printf("We have looped trying to restore files in %s "
499                                "too many times to be making progress, "
500                                "stopping\n", dir);
501                         break;
502                 }
503
504                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
505                         do {
506                                 ret = next_leaf(root, path);
507                                 if (ret < 0) {
508                                         fprintf(stderr, "Error searching %d\n",
509                                                 ret);
510                                         btrfs_free_path(path);
511                                         return ret;
512                                 } else if (ret > 0) {
513                                         /* No more leaves to search */
514                                         if (verbose)
515                                                 printf("Reached the end of "
516                                                        "the tree searching the"
517                                                        " directory\n");
518                                         btrfs_free_path(path);
519                                         return 0;
520                                 }
521                                 leaf = path->nodes[0];
522                         } while (!leaf);
523                         continue;
524                 }
525                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
526                 if (found_key.objectid != key->objectid) {
527                         if (verbose > 1)
528                                 printf("Found objectid=%Lu, key=%Lu\n",
529                                        found_key.objectid, key->objectid);
530                         break;
531                 }
532                 if (found_key.type != key->type) {
533                         if (verbose > 1)
534                                 printf("Found type=%u, want=%u\n",
535                                        found_key.type, key->type);
536                         break;
537                 }
538                 dir_item = btrfs_item_ptr(leaf, path->slots[0],
539                                           struct btrfs_dir_item);
540                 name_ptr = (unsigned long)(dir_item + 1);
541                 name_len = btrfs_dir_name_len(leaf, dir_item);
542                 read_extent_buffer(leaf, filename, name_ptr, name_len);
543                 filename[name_len] = '\0';
544                 type = btrfs_dir_type(leaf, dir_item);
545                 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
546
547                 snprintf(path_name, 4096, "%s/%s", dir, filename);
548
549
550                 /*
551                  * At this point we're only going to restore directories and
552                  * files, no symlinks or anything else.
553                  */
554                 if (type == BTRFS_FT_REG_FILE) {
555                         if (!overwrite) {
556                                 static int warn = 0;
557                                 struct stat st;
558
559                                 ret = stat(path_name, &st);
560                                 if (!ret) {
561                                         loops = 0;
562                                         if (verbose || !warn)
563                                                 printf("Skipping existing file"
564                                                        " %s\n", path_name);
565                                         if (warn)
566                                                 goto next;
567                                         printf("If you wish to overwrite use "
568                                                "the -o option to overwrite\n");
569                                         warn = 1;
570                                         goto next;
571                                 }
572                                 ret = 0;
573                         }
574                         if (verbose)
575                                 printf("Restoring %s\n", path_name);
576                         fd = open(path_name, O_CREAT|O_WRONLY, 0644);
577                         if (fd < 0) {
578                                 fprintf(stderr, "Error creating %s: %d\n",
579                                         path_name, errno);
580                                 if (ignore_errors)
581                                         goto next;
582                                 btrfs_free_path(path);
583                                 return -1;
584                         }
585                         loops = 0;
586                         ret = copy_file(root, fd, &location, path_name);
587                         close(fd);
588                         if (ret) {
589                                 if (ignore_errors)
590                                         goto next;
591                                 btrfs_free_path(path);
592                                 return ret;
593                         }
594                 } else if (type == BTRFS_FT_DIR) {
595                         struct btrfs_root *search_root = root;
596                         char *dir = strdup(path_name);
597
598                         if (!dir) {
599                                 fprintf(stderr, "Ran out of memory\n");
600                                 btrfs_free_path(path);
601                                 return -1;
602                         }
603
604                         if (location.type == BTRFS_ROOT_ITEM_KEY) {
605                                 /*
606                                  * If we are a snapshot and this is the index
607                                  * object to ourselves just skip it.
608                                  */
609                                 if (location.objectid ==
610                                     root->root_key.objectid) {
611                                         free(dir);
612                                         goto next;
613                                 }
614
615                                 search_root = btrfs_read_fs_root(root->fs_info,
616                                                                  &location);
617                                 if (IS_ERR(search_root)) {
618                                         free(dir);
619                                         fprintf(stderr, "Error reading "
620                                                 "subvolume %s: %lu\n",
621                                                 path_name,
622                                                 PTR_ERR(search_root));
623                                         if (ignore_errors)
624                                                 goto next;
625                                         btrfs_free_path(path);
626                                         return PTR_ERR(search_root);
627                                 }
628
629                                 /*
630                                  * A subvolume will have a key.offset of 0, a
631                                  * snapshot will have key.offset of a transid.
632                                  */
633                                 if (search_root->root_key.offset != 0 &&
634                                     get_snaps == 0) {
635                                         free(dir);
636                                         printf("Skipping snapshot %s\n",
637                                                filename);
638                                         goto next;
639                                 }
640                                 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
641                         }
642
643                         if (verbose)
644                                 printf("Restoring %s\n", path_name);
645
646                         errno = 0;
647                         ret = mkdir(path_name, 0755);
648                         if (ret && errno != EEXIST) {
649                                 free(dir);
650                                 fprintf(stderr, "Error mkdiring %s: %d\n",
651                                         path_name, errno);
652                                 if (ignore_errors)
653                                         goto next;
654                                 btrfs_free_path(path);
655                                 return -1;
656                         }
657                         loops = 0;
658                         ret = search_dir(search_root, &location, dir);
659                         free(dir);
660                         if (ret) {
661                                 if (ignore_errors)
662                                         goto next;
663                                 btrfs_free_path(path);
664                                 return ret;
665                         }
666                 }
667 next:
668                 path->slots[0]++;
669         }
670
671         if (verbose)
672                 printf("Done searching %s\n", dir);
673         btrfs_free_path(path);
674         return 0;
675 }
676
677 static struct btrfs_root *open_fs(const char *dev, u64 root_location, int super_mirror)
678 {
679         struct btrfs_root *root;
680         u64 bytenr;
681         int i;
682
683         for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
684                 bytenr = btrfs_sb_offset(i);
685                 root = open_ctree_recovery(dev, bytenr, root_location);
686                 if (root)
687                         return root;
688                 fprintf(stderr, "Could not open root, trying backup super\n");
689         }
690
691         return NULL;
692 }
693
694 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
695 {
696         struct btrfs_path *path;
697         struct btrfs_key found_key;
698         struct btrfs_key key;
699         int ret = -1;
700         int i;
701
702         key.objectid = 0;
703         key.type = BTRFS_DIR_INDEX_KEY;
704         key.offset = 0;
705
706         path = btrfs_alloc_path();
707         if (!path) {
708                 fprintf(stderr, "Ran out of memory\n");
709                 return ret;
710         }
711
712         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
713         if (ret < 0) {
714                 fprintf(stderr, "Error searching %d\n", ret);
715                 goto out;
716         }
717
718         if (!path->nodes[0]) {
719                 fprintf(stderr, "No leaf!\n");
720                 goto out;
721         }
722 again:
723         for (i = path->slots[0];
724              i < btrfs_header_nritems(path->nodes[0]); i++) {
725                 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
726                 if (found_key.type != key.type)
727                         continue;
728
729                 printf("Using objectid %Lu for first dir\n",
730                        found_key.objectid);
731                 *objectid = found_key.objectid;
732                 ret = 0;
733                 goto out;
734         }
735         do {
736                 ret = next_leaf(root, path);
737                 if (ret < 0) {
738                         fprintf(stderr, "Error getting next leaf %d\n",
739                                 ret);
740                         goto out;
741                 } else if (ret > 0) {
742                         fprintf(stderr, "No more leaves\n");
743                         goto out;
744                 }
745         } while (!path->nodes[0]);
746         if (path->nodes[0])
747                 goto again;
748         printf("Couldn't find a dir index item\n");
749 out:
750         btrfs_free_path(path);
751         return ret;
752 }
753
754 const char * const cmd_restore_usage[] = {
755         "btrfs restore [options] <device>",
756         "Try to restore files from a damaged filesystem (unmounted)",
757         "",
758         "-s              get snapshots",
759         "-v              verbose",
760         "-i              ignore errors",
761         "-o              overwrite",
762         "-t              tree location",
763         "-f <offset>     filesystem location",
764         "-u <block>      super mirror",
765         "-d              find dir",
766         NULL
767 };
768
769 int cmd_restore(int argc, char **argv)
770 {
771         struct btrfs_root *root;
772         struct btrfs_key key;
773         char dir_name[128];
774         u64 tree_location = 0;
775         u64 fs_location = 0;
776         u64 root_objectid = 0;
777         int len;
778         int ret;
779         int opt;
780         int super_mirror = 0;
781         int find_dir = 0;
782
783         while ((opt = getopt(argc, argv, "sviot:u:df:r:")) != -1) {
784                 switch (opt) {
785                         case 's':
786                                 get_snaps = 1;
787                                 break;
788                         case 'v':
789                                 verbose++;
790                                 break;
791                         case 'i':
792                                 ignore_errors = 1;
793                                 break;
794                         case 'o':
795                                 overwrite = 1;
796                                 break;
797                         case 't':
798                                 errno = 0;
799                                 tree_location = (u64)strtoll(optarg, NULL, 10);
800                                 if (errno != 0) {
801                                         fprintf(stderr, "Tree location not valid\n");
802                                         exit(1);
803                                 }
804                                 break;
805                         case 'f':
806                                 errno = 0;
807                                 fs_location = (u64)strtoll(optarg, NULL, 10);
808                                 if (errno != 0) {
809                                         fprintf(stderr, "Fs location not valid\n");
810                                         exit(1);
811                                 }
812                                 break;
813                         case 'u':
814                                 errno = 0;
815                                 super_mirror = (int)strtol(optarg, NULL, 10);
816                                 if (errno != 0 ||
817                                     super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
818                                         fprintf(stderr, "Super mirror not "
819                                                 "valid\n");
820                                         exit(1);
821                                 }
822                                 break;
823                         case 'd':
824                                 find_dir = 1;
825                                 break;
826                         case 'r':
827                                 errno = 0;
828                                 root_objectid = (u64)strtoll(optarg, NULL, 10);
829                                 if (errno != 0) {
830                                         fprintf(stderr, "Root objectid not valid\n");
831                                         exit(1);
832                                 }
833                                 break;
834                         default:
835                                 usage(cmd_restore_usage);
836                 }
837         }
838
839         if (optind + 1 >= argc)
840                 usage(cmd_restore_usage);
841
842         if ((ret = check_mounted(argv[optind])) < 0) {
843                 fprintf(stderr, "Could not check mount status: %s\n",
844                         strerror(-ret));
845                 return ret;
846         } else if (ret) {
847                 fprintf(stderr, "%s is currently mounted.  Aborting.\n", argv[optind]);
848                 return 1;
849         }
850
851         root = open_fs(argv[optind], tree_location, super_mirror);
852         if (root == NULL)
853                 return 1;
854
855         if (fs_location != 0) {
856                 free_extent_buffer(root->node);
857                 root->node = read_tree_block(root, fs_location, 4096, 0);
858                 if (!root->node) {
859                         fprintf(stderr, "Failed to read fs location\n");
860                         goto out;
861                 }
862         }
863
864         memset(path_name, 0, 4096);
865
866         strncpy(dir_name, argv[optind + 1], sizeof dir_name);
867         dir_name[sizeof dir_name - 1] = 0;
868
869         /* Strip the trailing / on the dir name */
870         len = strlen(dir_name);
871         while (len && dir_name[--len] == '/') {
872                 dir_name[len] = '\0';
873         }
874
875         if (root_objectid != 0) {
876                 struct btrfs_root *orig_root = root;
877
878                 key.objectid = root_objectid;
879                 key.type = BTRFS_ROOT_ITEM_KEY;
880                 key.offset = (u64)-1;
881                 root = btrfs_read_fs_root(orig_root->fs_info, &key);
882                 if (IS_ERR(root)) {
883                         fprintf(stderr, "Error reading root\n");
884                         root = orig_root;
885                         ret = 1;
886                         goto out;
887                 }
888                 key.type = 0;
889                 key.offset = 0;
890         }
891
892         if (find_dir) {
893                 ret = find_first_dir(root, &key.objectid);
894                 if (ret)
895                         goto out;
896         } else {
897                 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
898         }
899
900         ret = search_dir(root, &key, dir_name);
901
902 out:
903         close_ctree(root);
904         return ret;
905 }