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