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