btrfs-progs: restore: use bigger buffer for output path name
[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
20 #include "kerncompat.h"
21
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <lzo/lzoconf.h>
30 #include <lzo/lzo1x.h>
31 #include <zlib.h>
32 #include <regex.h>
33 #include <getopt.h>
34 #include <sys/types.h>
35 #include <sys/xattr.h>
36
37 #include "ctree.h"
38 #include "disk-io.h"
39 #include "print-tree.h"
40 #include "transaction.h"
41 #include "list.h"
42 #include "volumes.h"
43 #include "utils.h"
44 #include "commands.h"
45
46 static char fs_name[PATH_MAX];
47 static char path_name[PATH_MAX];
48 static char symlink_target[PATH_MAX];
49 static int get_snaps = 0;
50 static int verbose = 0;
51 static int restore_metadata = 0;
52 static int restore_symlinks = 0;
53 static int ignore_errors = 0;
54 static int overwrite = 0;
55 static int get_xattrs = 0;
56 static int dry_run = 0;
57
58 #define LZO_LEN 4
59 #define PAGE_CACHE_SIZE 4096
60 #define lzo1x_worst_compress(x) ((x) + ((x) / 16) + 64 + 3)
61
62 static int decompress_zlib(char *inbuf, char *outbuf, u64 compress_len,
63                            u64 decompress_len)
64 {
65         z_stream strm;
66         int ret;
67
68         memset(&strm, 0, sizeof(strm));
69         ret = inflateInit(&strm);
70         if (ret != Z_OK) {
71                 fprintf(stderr, "inflate init returnd %d\n", ret);
72                 return -1;
73         }
74
75         strm.avail_in = compress_len;
76         strm.next_in = (unsigned char *)inbuf;
77         strm.avail_out = decompress_len;
78         strm.next_out = (unsigned char *)outbuf;
79         ret = inflate(&strm, Z_NO_FLUSH);
80         if (ret != Z_STREAM_END) {
81                 (void)inflateEnd(&strm);
82                 fprintf(stderr, "failed to inflate: %d\n", ret);
83                 return -1;
84         }
85
86         (void)inflateEnd(&strm);
87         return 0;
88 }
89 static inline size_t read_compress_length(unsigned char *buf)
90 {
91         __le32 dlen;
92         memcpy(&dlen, buf, LZO_LEN);
93         return le32_to_cpu(dlen);
94 }
95
96 static int decompress_lzo(unsigned char *inbuf, char *outbuf, u64 compress_len,
97                           u64 *decompress_len)
98 {
99         size_t new_len;
100         size_t in_len;
101         size_t out_len = 0;
102         size_t tot_len;
103         size_t tot_in;
104         int ret;
105
106         ret = lzo_init();
107         if (ret != LZO_E_OK) {
108                 fprintf(stderr, "lzo init returned %d\n", ret);
109                 return -1;
110         }
111
112         tot_len = read_compress_length(inbuf);
113         inbuf += LZO_LEN;
114         tot_in = LZO_LEN;
115
116         while (tot_in < tot_len) {
117                 size_t mod_page;
118                 size_t rem_page;
119                 in_len = read_compress_length(inbuf);
120
121                 if ((tot_in + LZO_LEN + in_len) > tot_len) {
122                         fprintf(stderr, "bad compress length %lu\n",
123                                 (unsigned long)in_len);
124                         return -1;
125                 }
126
127                 inbuf += LZO_LEN;
128                 tot_in += LZO_LEN;
129
130                 new_len = lzo1x_worst_compress(PAGE_CACHE_SIZE);
131                 ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len,
132                                             (unsigned char *)outbuf,
133                                             (void *)&new_len, NULL);
134                 if (ret != LZO_E_OK) {
135                         fprintf(stderr, "failed to inflate: %d\n", ret);
136                         return -1;
137                 }
138                 out_len += new_len;
139                 outbuf += new_len;
140                 inbuf += in_len;
141                 tot_in += in_len;
142
143                 /*
144                  * If the 4 byte header does not fit to the rest of the page we
145                  * have to move to the next one, unless we read some garbage
146                  */
147                 mod_page = tot_in % PAGE_CACHE_SIZE;
148                 rem_page = PAGE_CACHE_SIZE - mod_page;
149                 if (rem_page < LZO_LEN) {
150                         inbuf += rem_page;
151                         tot_in += rem_page;
152                 }
153         }
154
155         *decompress_len = out_len;
156
157         return 0;
158 }
159
160 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
161                       u64 *decompress_len, int compress)
162 {
163         switch (compress) {
164         case BTRFS_COMPRESS_ZLIB:
165                 return decompress_zlib(inbuf, outbuf, compress_len,
166                                        *decompress_len);
167         case BTRFS_COMPRESS_LZO:
168                 return decompress_lzo((unsigned char *)inbuf, outbuf, compress_len,
169                                       decompress_len);
170         default:
171                 break;
172         }
173
174         fprintf(stderr, "invalid compression type: %d\n", compress);
175         return -1;
176 }
177
178 static int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
179 {
180         int slot;
181         int level = 1;
182         int offset = 1;
183         struct extent_buffer *c;
184         struct extent_buffer *next = NULL;
185
186 again:
187         for (; level < BTRFS_MAX_LEVEL; level++) {
188                 if (path->nodes[level])
189                         break;
190         }
191
192         if (level >= BTRFS_MAX_LEVEL)
193                 return 1;
194
195         slot = path->slots[level] + 1;
196
197         while(level < BTRFS_MAX_LEVEL) {
198                 if (!path->nodes[level])
199                         return 1;
200
201                 slot = path->slots[level] + offset;
202                 c = path->nodes[level];
203                 if (slot >= btrfs_header_nritems(c)) {
204                         level++;
205                         if (level == BTRFS_MAX_LEVEL)
206                                 return 1;
207                         offset = 1;
208                         continue;
209                 }
210
211                 if (path->reada)
212                         reada_for_search(root, path, level, slot, 0);
213
214                 next = read_node_slot(root, c, slot);
215                 if (extent_buffer_uptodate(next))
216                         break;
217                 offset++;
218         }
219         path->slots[level] = slot;
220         while(1) {
221                 level--;
222                 c = path->nodes[level];
223                 free_extent_buffer(c);
224                 path->nodes[level] = next;
225                 path->slots[level] = 0;
226                 if (!level)
227                         break;
228                 if (path->reada)
229                         reada_for_search(root, path, level, 0, 0);
230                 next = read_node_slot(root, next, 0);
231                 if (!extent_buffer_uptodate(next))
232                         goto again;
233         }
234         return 0;
235 }
236
237 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
238 {
239         struct extent_buffer *leaf = path->nodes[0];
240         struct btrfs_file_extent_item *fi;
241         char buf[4096];
242         char *outbuf;
243         u64 ram_size;
244         ssize_t done;
245         unsigned long ptr;
246         int ret;
247         int len;
248         int inline_item_len;
249         int compress;
250
251         fi = btrfs_item_ptr(leaf, path->slots[0],
252                             struct btrfs_file_extent_item);
253         ptr = btrfs_file_extent_inline_start(fi);
254         len = btrfs_file_extent_inline_len(leaf, path->slots[0], fi);
255         inline_item_len = btrfs_file_extent_inline_item_len(leaf, btrfs_item_nr(path->slots[0]));
256         read_extent_buffer(leaf, buf, ptr, inline_item_len);
257
258         compress = btrfs_file_extent_compression(leaf, fi);
259         if (compress == BTRFS_COMPRESS_NONE) {
260                 done = pwrite(fd, buf, len, pos);
261                 if (done < len) {
262                         fprintf(stderr, "Short inline write, wanted %d, did "
263                                 "%zd: %d\n", len, done, errno);
264                         return -1;
265                 }
266                 return 0;
267         }
268
269         ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
270         outbuf = calloc(1, ram_size);
271         if (!outbuf) {
272                 fprintf(stderr, "No memory\n");
273                 return -ENOMEM;
274         }
275
276         ret = decompress(buf, outbuf, len, &ram_size, compress);
277         if (ret) {
278                 free(outbuf);
279                 return ret;
280         }
281
282         done = pwrite(fd, outbuf, ram_size, pos);
283         free(outbuf);
284         if (done < ram_size) {
285                 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
286                         "did %zd: %d\n", ram_size, done, errno);
287                 return -1;
288         }
289
290         return 0;
291 }
292
293 static int copy_one_extent(struct btrfs_root *root, int fd,
294                            struct extent_buffer *leaf,
295                            struct btrfs_file_extent_item *fi, u64 pos)
296 {
297         struct btrfs_multi_bio *multi = NULL;
298         struct btrfs_device *device;
299         char *inbuf, *outbuf = NULL;
300         ssize_t done, total = 0;
301         u64 bytenr;
302         u64 ram_size;
303         u64 disk_size;
304         u64 num_bytes;
305         u64 length;
306         u64 size_left;
307         u64 dev_bytenr;
308         u64 offset;
309         u64 count = 0;
310         int compress;
311         int ret;
312         int dev_fd;
313         int mirror_num = 1;
314         int num_copies;
315
316         compress = btrfs_file_extent_compression(leaf, fi);
317         bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
318         disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
319         ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
320         offset = btrfs_file_extent_offset(leaf, fi);
321         num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
322         size_left = disk_size;
323         if (compress == BTRFS_COMPRESS_NONE)
324                 bytenr += offset;
325
326         if (verbose && offset)
327                 printf("offset is %Lu\n", offset);
328         /* we found a hole */
329         if (disk_size == 0)
330                 return 0;
331
332         inbuf = malloc(size_left);
333         if (!inbuf) {
334                 fprintf(stderr, "No memory\n");
335                 return -ENOMEM;
336         }
337
338         if (compress != BTRFS_COMPRESS_NONE) {
339                 outbuf = calloc(1, ram_size);
340                 if (!outbuf) {
341                         fprintf(stderr, "No memory\n");
342                         free(inbuf);
343                         return -ENOMEM;
344                 }
345         }
346 again:
347         length = size_left;
348         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
349                               bytenr, &length, &multi, mirror_num, NULL);
350         if (ret) {
351                 fprintf(stderr, "Error mapping block %d\n", ret);
352                 goto out;
353         }
354         device = multi->stripes[0].dev;
355         dev_fd = device->fd;
356         device->total_ios++;
357         dev_bytenr = multi->stripes[0].physical;
358         kfree(multi);
359
360         if (size_left < length)
361                 length = size_left;
362
363         done = pread(dev_fd, inbuf+count, length, dev_bytenr);
364         /* Need both checks, or we miss negative values due to u64 conversion */
365         if (done < 0 || done < length) {
366                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
367                                               bytenr, length);
368                 mirror_num++;
369                 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
370                 if (mirror_num > num_copies) {
371                         ret = -1;
372                         fprintf(stderr, "Exhausted mirrors trying to read\n");
373                         goto out;
374                 }
375                 fprintf(stderr, "Trying another mirror\n");
376                 goto again;
377         }
378
379         mirror_num = 1;
380         size_left -= length;
381         count += length;
382         bytenr += length;
383         if (size_left)
384                 goto again;
385
386         if (compress == BTRFS_COMPRESS_NONE) {
387                 while (total < num_bytes) {
388                         done = pwrite(fd, inbuf+total, num_bytes-total,
389                                       pos+total);
390                         if (done < 0) {
391                                 ret = -1;
392                                 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
393                                 goto out;
394                         }
395                         total += done;
396                 }
397                 ret = 0;
398                 goto out;
399         }
400
401         ret = decompress(inbuf, outbuf, disk_size, &ram_size, compress);
402         if (ret) {
403                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
404                                               bytenr, length);
405                 mirror_num++;
406                 if (mirror_num >= num_copies) {
407                         ret = -1;
408                         goto out;
409                 }
410                 fprintf(stderr, "Trying another mirror\n");
411                 goto again;
412         }
413
414         while (total < num_bytes) {
415                 done = pwrite(fd, outbuf + offset + total,
416                               num_bytes - total,
417                               pos + total);
418                 if (done < 0) {
419                         ret = -1;
420                         goto out;
421                 }
422                 total += done;
423         }
424 out:
425         free(inbuf);
426         free(outbuf);
427         return ret;
428 }
429
430 enum loop_response {
431         LOOP_STOP,
432         LOOP_CONTINUE,
433         LOOP_DONTASK
434 };
435
436 static enum loop_response ask_to_continue(const char *file)
437 {
438         char buf[2];
439         char *ret;
440
441         printf("We seem to be looping a lot on %s, do you want to keep going "
442                "on ? (y/N/a): ", file);
443 again:
444         ret = fgets(buf, 2, stdin);
445         if (*ret == '\n' || tolower(*ret) == 'n')
446                 return LOOP_STOP;
447         if (tolower(*ret) == 'a')
448                 return LOOP_DONTASK;
449         if (tolower(*ret) != 'y') {
450                 printf("Please enter one of 'y', 'n', or 'a': ");
451                 goto again;
452         }
453
454         return LOOP_CONTINUE;
455 }
456
457
458 static int set_file_xattrs(struct btrfs_root *root, u64 inode,
459                            int fd, const char *file_name)
460 {
461         struct btrfs_key key;
462         struct btrfs_path *path;
463         struct extent_buffer *leaf;
464         struct btrfs_dir_item *di;
465         u32 name_len = 0;
466         u32 data_len = 0;
467         u32 len = 0;
468         u32 cur, total_len;
469         char *name = NULL;
470         char *data = NULL;
471         int ret = 0;
472
473         key.objectid = inode;
474         key.type = BTRFS_XATTR_ITEM_KEY;
475         key.offset = 0;
476
477         path = btrfs_alloc_path();
478         if (!path)
479                 return -ENOMEM;
480
481         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
482         if (ret < 0)
483                 goto out;
484
485         leaf = path->nodes[0];
486         while (1) {
487                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
488                         do {
489                                 ret = next_leaf(root, path);
490                                 if (ret < 0) {
491                                         fprintf(stderr,
492                                                 "Error searching for extended attributes: %d\n",
493                                                 ret);
494                                         goto out;
495                                 } else if (ret) {
496                                         /* No more leaves to search */
497                                         ret = 0;
498                                         goto out;
499                                 }
500                                 leaf = path->nodes[0];
501                         } while (!leaf);
502                         continue;
503                 }
504
505                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
506                 if (key.type != BTRFS_XATTR_ITEM_KEY || key.objectid != inode)
507                         break;
508                 cur = 0;
509                 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
510                 di = btrfs_item_ptr(leaf, path->slots[0],
511                                     struct btrfs_dir_item);
512
513                 while (cur < total_len) {
514                         len = btrfs_dir_name_len(leaf, di);
515                         if (len > name_len) {
516                                 free(name);
517                                 name = (char *) malloc(len + 1);
518                                 if (!name) {
519                                         ret = -ENOMEM;
520                                         goto out;
521                                 }
522                         }
523                         read_extent_buffer(leaf, name,
524                                            (unsigned long)(di + 1), len);
525                         name[len] = '\0';
526                         name_len = len;
527
528                         len = btrfs_dir_data_len(leaf, di);
529                         if (len > data_len) {
530                                 free(data);
531                                 data = (char *) malloc(len);
532                                 if (!data) {
533                                         ret = -ENOMEM;
534                                         goto out;
535                                 }
536                         }
537                         read_extent_buffer(leaf, data,
538                                            (unsigned long)(di + 1) + name_len,
539                                            len);
540                         data_len = len;
541
542                         if (fsetxattr(fd, name, data, data_len, 0)) {
543                                 int err = errno;
544
545                                 fprintf(stderr,
546                                         "Error setting extended attribute %s on file %s: %s\n",
547                                         name, file_name, strerror(err));
548                         }
549
550                         len = sizeof(*di) + name_len + data_len;
551                         cur += len;
552                         di = (struct btrfs_dir_item *)((char *)di + len);
553                 }
554                 path->slots[0]++;
555         }
556         ret = 0;
557 out:
558         btrfs_free_path(path);
559         free(name);
560         free(data);
561
562         return ret;
563 }
564
565 static int copy_metadata(struct btrfs_root *root, int fd,
566                 struct btrfs_key *key)
567 {
568         struct btrfs_path *path;
569         struct btrfs_inode_item *inode_item;
570         int ret;
571
572         path = btrfs_alloc_path();
573         if (!path) {
574                 fprintf(stderr, "ERROR: Ran out of memory\n");
575                 return -ENOMEM;
576         }
577
578         ret = btrfs_lookup_inode(NULL, root, path, key, 0);
579         if (ret == 0) {
580                 struct btrfs_timespec *bts;
581                 struct timespec times[2];
582
583                 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
584                                 struct btrfs_inode_item);
585
586                 ret = fchown(fd, btrfs_inode_uid(path->nodes[0], inode_item),
587                                 btrfs_inode_gid(path->nodes[0], inode_item));
588                 if (ret) {
589                         fprintf(stderr, "ERROR: Failed to change owner: %s\n",
590                                         strerror(errno));
591                         goto out;
592                 }
593
594                 ret = fchmod(fd, btrfs_inode_mode(path->nodes[0], inode_item));
595                 if (ret) {
596                         fprintf(stderr, "ERROR: Failed to change mode: %s\n",
597                                         strerror(errno));
598                         goto out;
599                 }
600
601                 bts = btrfs_inode_atime(inode_item);
602                 times[0].tv_sec = btrfs_timespec_sec(path->nodes[0], bts);
603                 times[0].tv_nsec = btrfs_timespec_nsec(path->nodes[0], bts);
604
605                 bts = btrfs_inode_mtime(inode_item);
606                 times[1].tv_sec = btrfs_timespec_sec(path->nodes[0], bts);
607                 times[1].tv_nsec = btrfs_timespec_nsec(path->nodes[0], bts);
608
609                 ret = futimens(fd, times);
610                 if (ret) {
611                         fprintf(stderr, "ERROR: Failed to set times: %s\n",
612                                         strerror(errno));
613                         goto out;
614                 }
615         }
616 out:
617         btrfs_free_path(path);
618         return ret;
619 }
620
621 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
622                      const char *file)
623 {
624         struct extent_buffer *leaf;
625         struct btrfs_path *path;
626         struct btrfs_file_extent_item *fi;
627         struct btrfs_inode_item *inode_item;
628         struct btrfs_timespec *bts;
629         struct btrfs_key found_key;
630         int ret;
631         int extent_type;
632         int compression;
633         int loops = 0;
634         u64 found_size = 0;
635         struct timespec times[2];
636         int times_ok = 0;
637
638         path = btrfs_alloc_path();
639         if (!path) {
640                 fprintf(stderr, "Ran out of memory\n");
641                 return -ENOMEM;
642         }
643
644         ret = btrfs_lookup_inode(NULL, root, path, key, 0);
645         if (ret == 0) {
646                 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
647                                     struct btrfs_inode_item);
648                 found_size = btrfs_inode_size(path->nodes[0], inode_item);
649
650                 if (restore_metadata) {
651                         /*
652                          * Change the ownership and mode now, set times when
653                          * copyout is finished.
654                          */
655
656                         ret = fchown(fd, btrfs_inode_uid(path->nodes[0], inode_item),
657                                         btrfs_inode_gid(path->nodes[0], inode_item));
658                         if (ret && !ignore_errors)
659                                 goto out;
660
661                         ret = fchmod(fd, btrfs_inode_mode(path->nodes[0], inode_item));
662                         if (ret && !ignore_errors)
663                                 goto out;
664
665                         bts = btrfs_inode_atime(inode_item);
666                         times[0].tv_sec = btrfs_timespec_sec(path->nodes[0], bts);
667                         times[0].tv_nsec = btrfs_timespec_nsec(path->nodes[0], bts);
668
669                         bts = btrfs_inode_mtime(inode_item);
670                         times[1].tv_sec = btrfs_timespec_sec(path->nodes[0], bts);
671                         times[1].tv_nsec = btrfs_timespec_nsec(path->nodes[0], bts);
672                         times_ok = 1;
673                 }
674         }
675         btrfs_release_path(path);
676
677         key->offset = 0;
678         key->type = BTRFS_EXTENT_DATA_KEY;
679
680         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
681         if (ret < 0) {
682                 fprintf(stderr, "Error searching %d\n", ret);
683                 goto out;
684         }
685
686         leaf = path->nodes[0];
687         while (!leaf) {
688                 ret = next_leaf(root, path);
689                 if (ret < 0) {
690                         fprintf(stderr, "Error getting next leaf %d\n",
691                                 ret);
692                         goto out;
693                 } else if (ret > 0) {
694                         /* No more leaves to search */
695                         ret = 0;
696                         goto out;
697                 }
698                 leaf = path->nodes[0];
699         }
700
701         while (1) {
702                 if (loops >= 0 && loops++ >= 1024) {
703                         enum loop_response resp;
704
705                         resp = ask_to_continue(file);
706                         if (resp == LOOP_STOP)
707                                 break;
708                         else if (resp == LOOP_CONTINUE)
709                                 loops = 0;
710                         else if (resp == LOOP_DONTASK)
711                                 loops = -1;
712                 }
713                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
714                         do {
715                                 ret = next_leaf(root, path);
716                                 if (ret < 0) {
717                                         fprintf(stderr, "Error searching %d\n", ret);
718                                         goto out;
719                                 } else if (ret) {
720                                         /* No more leaves to search */
721                                         btrfs_free_path(path);
722                                         goto set_size;
723                                 }
724                                 leaf = path->nodes[0];
725                         } while (!leaf);
726                         continue;
727                 }
728                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
729                 if (found_key.objectid != key->objectid)
730                         break;
731                 if (found_key.type != key->type)
732                         break;
733                 fi = btrfs_item_ptr(leaf, path->slots[0],
734                                     struct btrfs_file_extent_item);
735                 extent_type = btrfs_file_extent_type(leaf, fi);
736                 compression = btrfs_file_extent_compression(leaf, fi);
737                 if (compression >= BTRFS_COMPRESS_LAST) {
738                         fprintf(stderr, "Don't support compression yet %d\n",
739                                 compression);
740                         ret = -1;
741                         goto out;
742                 }
743
744                 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
745                         goto next;
746                 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
747                         ret = copy_one_inline(fd, path, found_key.offset);
748                         if (ret)
749                                 goto out;
750                 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
751                         ret = copy_one_extent(root, fd, leaf, fi,
752                                               found_key.offset);
753                         if (ret)
754                                 goto out;
755                 } else {
756                         printf("Weird extent type %d\n", extent_type);
757                 }
758 next:
759                 path->slots[0]++;
760         }
761
762         btrfs_free_path(path);
763 set_size:
764         if (found_size) {
765                 ret = ftruncate(fd, (loff_t)found_size);
766                 if (ret)
767                         return ret;
768         }
769         if (get_xattrs) {
770                 ret = set_file_xattrs(root, key->objectid, fd, file);
771                 if (ret)
772                         return ret;
773         }
774         if (restore_metadata && times_ok) {
775                 ret = futimens(fd, times);
776                 if (ret)
777                         return ret;
778         }
779         return 0;
780
781 out:
782         btrfs_free_path(path);
783         return ret;
784 }
785
786 /*
787  * returns:
788  *  0 if the file exists and should be skipped.
789  *  1 if the file does NOT exist
790  *  2 if the file exists but is OK to overwrite
791  */
792 static int overwrite_ok(const char * path)
793 {
794         static int warn = 0;
795         struct stat st;
796         int ret;
797
798         /* don't be fooled by symlinks */
799         ret = fstatat(-1, path_name, &st, AT_SYMLINK_NOFOLLOW);
800
801         if (!ret) {
802                 if (overwrite)
803                         return 2;
804
805                 if (verbose || !warn)
806                         printf("Skipping existing file"
807                                    " %s\n", path);
808                 if (!warn)
809                         printf("If you wish to overwrite use -o\n");
810                 warn = 1;
811                 return 0;
812         }
813         return 1;
814 }
815
816 static int copy_symlink(struct btrfs_root *root, struct btrfs_key *key,
817                      const char *file)
818 {
819         struct btrfs_path *path;
820         struct extent_buffer *leaf;
821         struct btrfs_file_extent_item *extent_item;
822         struct btrfs_inode_item *inode_item;
823         u32 len;
824         u32 name_offset;
825         int ret;
826         struct btrfs_timespec *bts;
827         struct timespec times[2];
828
829         ret = overwrite_ok(path_name);
830         if (ret == 0)
831             return 0; /* skip this file */
832
833         /* symlink() can't overwrite, so unlink first */
834         if (ret == 2) {
835                 ret = unlink(path_name);
836                 if (ret) {
837                         fprintf(stderr, "failed to unlink '%s' for overwrite\n",
838                                         path_name);
839                         return ret;
840                 }
841         }
842
843         key->type = BTRFS_EXTENT_DATA_KEY;
844         key->offset = 0;
845
846         path = btrfs_alloc_path();
847         if (!path)
848                 return -ENOMEM;
849
850         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
851         if (ret < 0)
852                 goto out;
853
854         leaf = path->nodes[0];
855         if (!leaf) {
856                 fprintf(stderr, "Error getting leaf for symlink '%s'\n", file);
857                 ret = -1;
858                 goto out;
859         }
860
861         extent_item = btrfs_item_ptr(leaf, path->slots[0],
862                         struct btrfs_file_extent_item);
863
864         len = btrfs_file_extent_inline_item_len(leaf,
865                         btrfs_item_nr(path->slots[0]));
866         if (len > PATH_MAX) {
867                 fprintf(stderr, "Symlink '%s' target length %d is longer than PATH_MAX\n",
868                                 fs_name, len);
869                 ret = -1;
870                 goto out;
871         }
872
873         name_offset = (unsigned long) extent_item
874                         + offsetof(struct btrfs_file_extent_item, disk_bytenr);
875         read_extent_buffer(leaf, symlink_target, name_offset, len);
876
877         symlink_target[len] = 0;
878
879         if (!dry_run) {
880                 ret = symlink(symlink_target, path_name);
881                 if (ret < 0) {
882                         fprintf(stderr, "Failed to restore symlink '%s': %s\n",
883                                         path_name, strerror(errno));
884                         goto out;
885                 }
886         }
887         printf("SYMLINK: '%s' => '%s'\n", path_name, symlink_target);
888
889         ret = 0;
890         if (!restore_metadata)
891                 goto out;
892
893         /*
894          * Symlink metadata operates differently than files/directories, so do
895          * our own work here.
896          */
897         key->type = BTRFS_INODE_ITEM_KEY;
898         key->offset = 0;
899
900         btrfs_release_path(path);
901
902         ret = btrfs_lookup_inode(NULL, root, path, key, 0);
903         if (ret) {
904                 fprintf(stderr, "Failed to lookup inode for '%s'\n", file);
905                 goto out;
906         }
907
908         inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
909                         struct btrfs_inode_item);
910
911         ret = fchownat(-1, file, btrfs_inode_uid(path->nodes[0], inode_item),
912                                    btrfs_inode_gid(path->nodes[0], inode_item),
913                                    AT_SYMLINK_NOFOLLOW);
914         if (ret) {
915                 fprintf(stderr, "Failed to change owner: %s\n",
916                                 strerror(errno));
917                 goto out;
918         }
919
920         bts = btrfs_inode_atime(inode_item);
921         times[0].tv_sec  = btrfs_timespec_sec(path->nodes[0], bts);
922         times[0].tv_nsec = btrfs_timespec_nsec(path->nodes[0], bts);
923
924         bts = btrfs_inode_mtime(inode_item);
925         times[1].tv_sec  = btrfs_timespec_sec(path->nodes[0], bts);
926         times[1].tv_nsec = btrfs_timespec_nsec(path->nodes[0], bts);
927
928         ret = utimensat(-1, file, times, AT_SYMLINK_NOFOLLOW);
929         if (ret)
930                 fprintf(stderr, "Failed to set times: %s\n", strerror(errno));
931 out:
932         btrfs_free_path(path);
933         return ret;
934 }
935
936 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
937                       const char *output_rootdir, const char *in_dir,
938                       const regex_t *mreg)
939 {
940         struct btrfs_path *path;
941         struct extent_buffer *leaf;
942         struct btrfs_dir_item *dir_item;
943         struct btrfs_key found_key, location;
944         char filename[BTRFS_NAME_LEN + 1];
945         unsigned long name_ptr;
946         int name_len;
947         int ret = 0;
948         int fd;
949         int loops = 0;
950         u8 type;
951
952         path = btrfs_alloc_path();
953         if (!path) {
954                 fprintf(stderr, "Ran out of memory\n");
955                 return -ENOMEM;
956         }
957
958         key->offset = 0;
959         key->type = BTRFS_DIR_INDEX_KEY;
960
961         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
962         if (ret < 0) {
963                 fprintf(stderr, "Error searching %d\n", ret);
964                 goto out;
965         }
966
967         ret = 0;
968
969         leaf = path->nodes[0];
970         while (!leaf) {
971                 if (verbose > 1)
972                         printf("No leaf after search, looking for the next "
973                                "leaf\n");
974                 ret = next_leaf(root, path);
975                 if (ret < 0) {
976                         fprintf(stderr, "Error getting next leaf %d\n",
977                                 ret);
978                         goto out;
979                 } else if (ret > 0) {
980                         /* No more leaves to search */
981                         if (verbose)
982                                 printf("Reached the end of the tree looking "
983                                        "for the directory\n");
984                         ret = 0;
985                         goto out;
986                 }
987                 leaf = path->nodes[0];
988         }
989
990         while (leaf) {
991                 if (loops++ >= 1024) {
992                         printf("We have looped trying to restore files in %s "
993                                "too many times to be making progress, "
994                                "stopping\n", in_dir);
995                         break;
996                 }
997
998                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
999                         do {
1000                                 ret = next_leaf(root, path);
1001                                 if (ret < 0) {
1002                                         fprintf(stderr, "Error searching %d\n",
1003                                                 ret);
1004                                         goto out;
1005                                 } else if (ret > 0) {
1006                                         /* No more leaves to search */
1007                                         if (verbose)
1008                                                 printf("Reached the end of "
1009                                                        "the tree searching the"
1010                                                        " directory\n");
1011                                         ret = 0;
1012                                         goto out;
1013                                 }
1014                                 leaf = path->nodes[0];
1015                         } while (!leaf);
1016                         continue;
1017                 }
1018                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1019                 if (found_key.objectid != key->objectid) {
1020                         if (verbose > 1)
1021                                 printf("Found objectid=%Lu, key=%Lu\n",
1022                                        found_key.objectid, key->objectid);
1023                         break;
1024                 }
1025                 if (found_key.type != key->type) {
1026                         if (verbose > 1)
1027                                 printf("Found type=%u, want=%u\n",
1028                                        found_key.type, key->type);
1029                         break;
1030                 }
1031                 dir_item = btrfs_item_ptr(leaf, path->slots[0],
1032                                           struct btrfs_dir_item);
1033                 name_ptr = (unsigned long)(dir_item + 1);
1034                 name_len = btrfs_dir_name_len(leaf, dir_item);
1035                 read_extent_buffer(leaf, filename, name_ptr, name_len);
1036                 filename[name_len] = '\0';
1037                 type = btrfs_dir_type(leaf, dir_item);
1038                 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
1039
1040                 /* full path from root of btrfs being restored */
1041                 snprintf(fs_name, PATH_MAX, "%s/%s", in_dir, filename);
1042
1043                 if (mreg && REG_NOMATCH == regexec(mreg, fs_name, 0, NULL, 0))
1044                         goto next;
1045
1046                 /* full path from system root */
1047                 snprintf(path_name, PATH_MAX, "%s%s", output_rootdir, fs_name);
1048
1049                 /*
1050                  * Restore directories, files, symlinks and metadata.
1051                  */
1052                 if (type == BTRFS_FT_REG_FILE) {
1053                         if (!overwrite_ok(path_name))
1054                                 goto next;
1055
1056                         if (verbose)
1057                                 printf("Restoring %s\n", path_name);
1058                         if (dry_run)
1059                                 goto next;
1060                         fd = open(path_name, O_CREAT|O_WRONLY, 0644);
1061                         if (fd < 0) {
1062                                 fprintf(stderr, "Error creating %s: %d\n",
1063                                         path_name, errno);
1064                                 if (ignore_errors)
1065                                         goto next;
1066                                 ret = -1;
1067                                 goto out;
1068                         }
1069                         loops = 0;
1070                         ret = copy_file(root, fd, &location, path_name);
1071                         close(fd);
1072                         if (ret) {
1073                                 fprintf(stderr, "Error copying data for %s\n",
1074                                         path_name);
1075                                 if (ignore_errors)
1076                                         goto next;
1077                                 goto out;
1078                         }
1079                 } else if (type == BTRFS_FT_DIR) {
1080                         struct btrfs_root *search_root = root;
1081                         char *dir = strdup(fs_name);
1082
1083                         if (!dir) {
1084                                 fprintf(stderr, "Ran out of memory\n");
1085                                 ret = -ENOMEM;
1086                                 goto out;
1087                         }
1088
1089                         if (location.type == BTRFS_ROOT_ITEM_KEY) {
1090                                 /*
1091                                  * If we are a snapshot and this is the index
1092                                  * object to ourselves just skip it.
1093                                  */
1094                                 if (location.objectid ==
1095                                     root->root_key.objectid) {
1096                                         free(dir);
1097                                         goto next;
1098                                 }
1099
1100                                 location.offset = (u64)-1;
1101                                 search_root = btrfs_read_fs_root(root->fs_info,
1102                                                                  &location);
1103                                 if (IS_ERR(search_root)) {
1104                                         free(dir);
1105                                         fprintf(stderr, "Error reading "
1106                                                 "subvolume %s: %lu\n",
1107                                                 path_name,
1108                                                 PTR_ERR(search_root));
1109                                         if (ignore_errors)
1110                                                 goto next;
1111                                         ret = PTR_ERR(search_root);
1112                                         goto out;
1113                                 }
1114
1115                                 /*
1116                                  * A subvolume will have a key.offset of 0, a
1117                                  * snapshot will have key.offset of a transid.
1118                                  */
1119                                 if (search_root->root_key.offset != 0 &&
1120                                     get_snaps == 0) {
1121                                         free(dir);
1122                                         printf("Skipping snapshot %s\n",
1123                                                filename);
1124                                         goto next;
1125                                 }
1126                                 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
1127                         }
1128
1129                         if (verbose)
1130                                 printf("Restoring %s\n", path_name);
1131
1132                         errno = 0;
1133                         if (dry_run)
1134                                 ret = 0;
1135                         else
1136                                 ret = mkdir(path_name, 0755);
1137                         if (ret && errno != EEXIST) {
1138                                 free(dir);
1139                                 fprintf(stderr, "Error mkdiring %s: %d\n",
1140                                         path_name, errno);
1141                                 if (ignore_errors)
1142                                         goto next;
1143                                 ret = -1;
1144                                 goto out;
1145                         }
1146                         loops = 0;
1147                         ret = search_dir(search_root, &location,
1148                                          output_rootdir, dir, mreg);
1149                         free(dir);
1150                         if (ret) {
1151                                 fprintf(stderr, "Error searching %s\n",
1152                                         path_name);
1153                                 if (ignore_errors)
1154                                         goto next;
1155                                 goto out;
1156                         }
1157                 } else if (type == BTRFS_FT_SYMLINK) {
1158                         if (restore_symlinks)
1159                                 ret = copy_symlink(root, &location, path_name);
1160                         if (ret < 0) {
1161                                 if (ignore_errors)
1162                                         goto next;
1163                                 btrfs_free_path(path);
1164                                 return ret;
1165                         }
1166                 }
1167 next:
1168                 path->slots[0]++;
1169         }
1170
1171         if (restore_metadata) {
1172                 snprintf(path_name, PATH_MAX, "%s%s", output_rootdir, in_dir);
1173                 fd = open(path_name, O_RDONLY);
1174                 if (fd < 0) {
1175                         fprintf(stderr, "ERROR: Failed to access %s to restore metadata\n",
1176                                         path_name);
1177                         if (!ignore_errors) {
1178                                 ret = -1;
1179                                 goto out;
1180                         }
1181                 } else {
1182                         /*
1183                          * Set owner/mode/time on the directory as well
1184                          */
1185                         key->type = BTRFS_INODE_ITEM_KEY;
1186                         ret = copy_metadata(root, fd, key);
1187                         close(fd);
1188                         if (ret && !ignore_errors)
1189                                 goto out;
1190                 }
1191         }
1192
1193         if (verbose)
1194                 printf("Done searching %s\n", in_dir);
1195 out:
1196         btrfs_free_path(path);
1197         return ret;
1198 }
1199
1200 static int do_list_roots(struct btrfs_root *root)
1201 {
1202         struct btrfs_key key;
1203         struct btrfs_key found_key;
1204         struct btrfs_disk_key disk_key;
1205         struct btrfs_path *path;
1206         struct extent_buffer *leaf;
1207         struct btrfs_root_item ri;
1208         unsigned long offset;
1209         int slot;
1210         int ret;
1211
1212         root = root->fs_info->tree_root;
1213         path = btrfs_alloc_path();
1214         if (!path) {
1215                 fprintf(stderr, "Failed to alloc path\n");
1216                 return -ENOMEM;
1217         }
1218
1219         key.offset = 0;
1220         key.objectid = 0;
1221         key.type = BTRFS_ROOT_ITEM_KEY;
1222
1223         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1224         if (ret < 0) {
1225                 fprintf(stderr, "Failed to do search %d\n", ret);
1226                 btrfs_free_path(path);
1227                 return -1;
1228         }
1229
1230         leaf = path->nodes[0];
1231
1232         while (1) {
1233                 slot = path->slots[0];
1234                 if (slot >= btrfs_header_nritems(leaf)) {
1235                         ret = btrfs_next_leaf(root, path);
1236                         if (ret)
1237                                 break;
1238                         leaf = path->nodes[0];
1239                         slot = path->slots[0];
1240                 }
1241                 btrfs_item_key(leaf, &disk_key, slot);
1242                 btrfs_disk_key_to_cpu(&found_key, &disk_key);
1243                 if (btrfs_key_type(&found_key) != BTRFS_ROOT_ITEM_KEY) {
1244                         path->slots[0]++;
1245                         continue;
1246                 }
1247
1248                 offset = btrfs_item_ptr_offset(leaf, slot);
1249                 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
1250                 printf(" tree ");
1251                 btrfs_print_key(&disk_key);
1252                 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
1253                        btrfs_root_level(&ri));
1254                 path->slots[0]++;
1255         }
1256         btrfs_free_path(path);
1257
1258         return 0;
1259 }
1260
1261 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
1262                                   int super_mirror, int list_roots)
1263 {
1264         struct btrfs_fs_info *fs_info = NULL;
1265         struct btrfs_root *root = NULL;
1266         u64 bytenr;
1267         int i;
1268
1269         for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1270                 bytenr = btrfs_sb_offset(i);
1271                 fs_info = open_ctree_fs_info(dev, bytenr, root_location,
1272                                              OPEN_CTREE_PARTIAL);
1273                 if (fs_info)
1274                         break;
1275                 fprintf(stderr, "Could not open root, trying backup super\n");
1276         }
1277
1278         if (!fs_info)
1279                 return NULL;
1280
1281         /*
1282          * All we really need to succeed is reading the chunk tree, everything
1283          * else we can do by hand, since we only need to read the tree root and
1284          * the fs_root.
1285          */
1286         if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
1287                 u64 generation;
1288
1289                 root = fs_info->tree_root;
1290                 if (!root_location)
1291                         root_location = btrfs_super_root(fs_info->super_copy);
1292                 generation = btrfs_super_generation(fs_info->super_copy);
1293                 root->node = read_tree_block(root, root_location,
1294                                              root->leafsize, generation);
1295                 if (!extent_buffer_uptodate(root->node)) {
1296                         fprintf(stderr, "Error opening tree root\n");
1297                         close_ctree(root);
1298                         return NULL;
1299                 }
1300         }
1301
1302         if (!list_roots && !fs_info->fs_root) {
1303                 struct btrfs_key key;
1304
1305                 key.objectid = BTRFS_FS_TREE_OBJECTID;
1306                 key.type = BTRFS_ROOT_ITEM_KEY;
1307                 key.offset = (u64)-1;
1308                 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
1309                 if (IS_ERR(fs_info->fs_root)) {
1310                         fprintf(stderr, "Couldn't read fs root: %ld\n",
1311                                 PTR_ERR(fs_info->fs_root));
1312                         close_ctree(fs_info->tree_root);
1313                         return NULL;
1314                 }
1315         }
1316
1317         if (list_roots && do_list_roots(fs_info->tree_root)) {
1318                 close_ctree(fs_info->tree_root);
1319                 return NULL;
1320         }
1321
1322         return fs_info->fs_root;
1323 }
1324
1325 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
1326 {
1327         struct btrfs_path *path;
1328         struct btrfs_key found_key;
1329         struct btrfs_key key;
1330         int ret = -1;
1331         int i;
1332
1333         key.objectid = 0;
1334         key.type = BTRFS_DIR_INDEX_KEY;
1335         key.offset = 0;
1336
1337         path = btrfs_alloc_path();
1338         if (!path) {
1339                 fprintf(stderr, "Ran out of memory\n");
1340                 return ret;
1341         }
1342
1343         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1344         if (ret < 0) {
1345                 fprintf(stderr, "Error searching %d\n", ret);
1346                 goto out;
1347         }
1348
1349         if (!path->nodes[0]) {
1350                 fprintf(stderr, "No leaf!\n");
1351                 goto out;
1352         }
1353 again:
1354         for (i = path->slots[0];
1355              i < btrfs_header_nritems(path->nodes[0]); i++) {
1356                 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
1357                 if (found_key.type != key.type)
1358                         continue;
1359
1360                 printf("Using objectid %Lu for first dir\n",
1361                        found_key.objectid);
1362                 *objectid = found_key.objectid;
1363                 ret = 0;
1364                 goto out;
1365         }
1366         do {
1367                 ret = next_leaf(root, path);
1368                 if (ret < 0) {
1369                         fprintf(stderr, "Error getting next leaf %d\n",
1370                                 ret);
1371                         goto out;
1372                 } else if (ret > 0) {
1373                         fprintf(stderr, "No more leaves\n");
1374                         goto out;
1375                 }
1376         } while (!path->nodes[0]);
1377         if (path->nodes[0])
1378                 goto again;
1379         printf("Couldn't find a dir index item\n");
1380 out:
1381         btrfs_free_path(path);
1382         return ret;
1383 }
1384
1385 const char * const cmd_restore_usage[] = {
1386         "btrfs restore [options] <device> <path> | -l <device>",
1387         "Try to restore files from a damaged filesystem (unmounted)",
1388         "",
1389         "-s              get snapshots",
1390         "-x              get extended attributes",
1391         "-m|--metadata   restore owner, mode and times",
1392         "-S|--symlinks   restore symbolic links"
1393         "-v              verbose",
1394         "-i              ignore errors",
1395         "-o              overwrite",
1396         "-t <bytenr>     tree location",
1397         "-f <bytenr>     filesystem location",
1398         "-u <mirror>     super mirror",
1399         "-r <rootid>     root objectid",
1400         "-d              find dir",
1401         "-l              list tree roots",
1402         "-D|--dry-run    dry run (only list files that would be recovered)",
1403         "--path-regex <regex>",
1404         "                restore only filenames matching regex,",
1405         "                you have to use following syntax (possibly quoted):",
1406         "                ^/(|home(|/username(|/Desktop(|/.*))))$",
1407         "-c              ignore case (--path-regex only)",
1408         NULL
1409 };
1410
1411 int cmd_restore(int argc, char **argv)
1412 {
1413         struct btrfs_root *root;
1414         struct btrfs_key key;
1415         char dir_name[PATH_MAX];
1416         u64 tree_location = 0;
1417         u64 fs_location = 0;
1418         u64 root_objectid = 0;
1419         int len;
1420         int ret;
1421         int super_mirror = 0;
1422         int find_dir = 0;
1423         int list_roots = 0;
1424         const char *match_regstr = NULL;
1425         int match_cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
1426         regex_t match_reg, *mreg = NULL;
1427         char reg_err[256];
1428
1429         while (1) {
1430                 int opt;
1431                 static const struct option long_options[] = {
1432                         { "path-regex", required_argument, NULL, 256},
1433                         { "dry-run", no_argument, NULL, 'D'},
1434                         { "metadata", no_argument, NULL, 'm'},
1435                         { "symlinks", no_argument, NULL, 'S'},
1436                         { NULL, 0, NULL, 0}
1437                 };
1438
1439                 opt = getopt_long(argc, argv, "sSxviot:u:dmf:r:lDc", long_options,
1440                                         NULL);
1441                 if (opt < 0)
1442                         break;
1443
1444                 switch (opt) {
1445                         case 's':
1446                                 get_snaps = 1;
1447                                 break;
1448                         case 'v':
1449                                 verbose++;
1450                                 break;
1451                         case 'i':
1452                                 ignore_errors = 1;
1453                                 break;
1454                         case 'o':
1455                                 overwrite = 1;
1456                                 break;
1457                         case 't':
1458                                 tree_location = arg_strtou64(optarg);
1459                                 break;
1460                         case 'f':
1461                                 fs_location = arg_strtou64(optarg);
1462                                 break;
1463                         case 'u':
1464                                 super_mirror = arg_strtou64(optarg);
1465                                 if (super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1466                                         fprintf(stderr, "Super mirror not "
1467                                                 "valid\n");
1468                                         exit(1);
1469                                 }
1470                                 break;
1471                         case 'd':
1472                                 find_dir = 1;
1473                                 break;
1474                         case 'r':
1475                                 root_objectid = arg_strtou64(optarg);
1476                                 if (!is_fstree(root_objectid)) {
1477                                         fprintf(stderr, "objectid %llu is not a valid fs/file tree\n",
1478                                                         root_objectid);
1479                                         exit(1);
1480                                 }
1481                                 break;
1482                         case 'l':
1483                                 list_roots = 1;
1484                                 break;
1485                         case 'm':
1486                                 restore_metadata = 1;
1487                                 break;
1488                         case 'S':
1489                                 restore_symlinks = 1;
1490                                 break;
1491                         case 'D':
1492                                 dry_run = 1;
1493                                 break;
1494                         case 'c':
1495                                 match_cflags |= REG_ICASE;
1496                                 break;
1497                         /* long option without single letter alternative */
1498                         case 256:
1499                                 match_regstr = optarg;
1500                                 break;
1501                         case 'x':
1502                                 get_xattrs = 1;
1503                                 break;
1504                         default:
1505                                 usage(cmd_restore_usage);
1506                 }
1507         }
1508
1509         if (!list_roots && check_argc_min(argc - optind, 2))
1510                 usage(cmd_restore_usage);
1511         else if (list_roots && check_argc_min(argc - optind, 1))
1512                 usage(cmd_restore_usage);
1513
1514         if (fs_location && root_objectid) {
1515                 fprintf(stderr, "don't use -f and -r at the same time.\n");
1516                 return 1;
1517         }
1518
1519         if ((ret = check_mounted(argv[optind])) < 0) {
1520                 fprintf(stderr, "Could not check mount status: %s\n",
1521                         strerror(-ret));
1522                 return 1;
1523         } else if (ret) {
1524                 fprintf(stderr, "%s is currently mounted.  Aborting.\n", argv[optind]);
1525                 return 1;
1526         }
1527
1528         root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1529         if (root == NULL)
1530                 return 1;
1531
1532         if (list_roots)
1533                 goto out;
1534
1535         if (fs_location != 0) {
1536                 free_extent_buffer(root->node);
1537                 root->node = read_tree_block(root, fs_location, root->leafsize, 0);
1538                 if (!extent_buffer_uptodate(root->node)) {
1539                         fprintf(stderr, "Failed to read fs location\n");
1540                         ret = 1;
1541                         goto out;
1542                 }
1543         }
1544
1545         memset(path_name, 0, PATH_MAX);
1546
1547         if (strlen(argv[optind + 1]) >= PATH_MAX) {
1548                 fprintf(stderr, "ERROR: path too long\n");
1549                 ret = 1;
1550                 goto out;
1551         }
1552         strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1553         dir_name[sizeof dir_name - 1] = 0;
1554
1555         /* Strip the trailing / on the dir name */
1556         len = strlen(dir_name);
1557         while (len && dir_name[--len] == '/') {
1558                 dir_name[len] = '\0';
1559         }
1560
1561         if (root_objectid != 0) {
1562                 struct btrfs_root *orig_root = root;
1563
1564                 key.objectid = root_objectid;
1565                 key.type = BTRFS_ROOT_ITEM_KEY;
1566                 key.offset = (u64)-1;
1567                 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1568                 if (IS_ERR(root)) {
1569                         fprintf(stderr, "fail to read root %llu: %s\n",
1570                                         root_objectid, strerror(-PTR_ERR(root)));
1571                         root = orig_root;
1572                         ret = 1;
1573                         goto out;
1574                 }
1575                 key.type = 0;
1576                 key.offset = 0;
1577         }
1578
1579         if (find_dir) {
1580                 ret = find_first_dir(root, &key.objectid);
1581                 if (ret)
1582                         goto out;
1583         } else {
1584                 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1585         }
1586
1587         if (match_regstr) {
1588                 ret = regcomp(&match_reg, match_regstr, match_cflags);
1589                 if (ret) {
1590                         regerror(ret, &match_reg, reg_err, sizeof(reg_err));
1591                         fprintf(stderr, "Regex compile failed: %s\n", reg_err);
1592                         goto out;
1593                 }
1594                 mreg = &match_reg;
1595         }
1596
1597         if (dry_run)
1598                 printf("This is a dry-run, no files are going to be restored\n");
1599
1600         ret = search_dir(root, &key, dir_name, "", mreg);
1601
1602 out:
1603         if (mreg)
1604                 regfree(mreg);
1605         close_ctree(root);
1606         return !!ret;
1607 }