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