btrfs-progs: restore: use long option for the path regex
[platform/upstream/btrfs-progs.git] / cmds-restore.c
1 /*
2  * Copyright (C) 2011 Red Hat.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21
22 #include "kerncompat.h"
23
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <lzo/lzoconf.h>
32 #include <lzo/lzo1x.h>
33 #include <zlib.h>
34 #include <regex.h>
35 #include <getopt.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 "version.h"
43 #include "volumes.h"
44 #include "utils.h"
45 #include "commands.h"
46
47 static char fs_name[4096];
48 static char path_name[4096];
49 static int get_snaps = 0;
50 static int verbose = 0;
51 static int ignore_errors = 0;
52 static int overwrite = 0;
53
54 #define LZO_LEN 4
55 #define PAGE_CACHE_SIZE 4096
56 #define lzo1x_worst_compress(x) ((x) + ((x) / 16) + 64 + 3)
57
58 static int decompress_zlib(char *inbuf, char *outbuf, u64 compress_len,
59                            u64 decompress_len)
60 {
61         z_stream strm;
62         int ret;
63
64         memset(&strm, 0, sizeof(strm));
65         ret = inflateInit(&strm);
66         if (ret != Z_OK) {
67                 fprintf(stderr, "inflate init returnd %d\n", ret);
68                 return -1;
69         }
70
71         strm.avail_in = compress_len;
72         strm.next_in = (unsigned char *)inbuf;
73         strm.avail_out = decompress_len;
74         strm.next_out = (unsigned char *)outbuf;
75         ret = inflate(&strm, Z_NO_FLUSH);
76         if (ret != Z_STREAM_END) {
77                 (void)inflateEnd(&strm);
78                 fprintf(stderr, "failed to inflate: %d\n", ret);
79                 return -1;
80         }
81
82         (void)inflateEnd(&strm);
83         return 0;
84 }
85 static inline size_t read_compress_length(unsigned char *buf)
86 {
87         __le32 dlen;
88         memcpy(&dlen, buf, LZO_LEN);
89         return le32_to_cpu(dlen);
90 }
91
92 static int decompress_lzo(unsigned char *inbuf, char *outbuf, u64 compress_len,
93                           u64 *decompress_len)
94 {
95         size_t new_len;
96         size_t in_len;
97         size_t out_len = 0;
98         size_t tot_len;
99         size_t tot_in;
100         int ret;
101
102         ret = lzo_init();
103         if (ret != LZO_E_OK) {
104                 fprintf(stderr, "lzo init returned %d\n", ret);
105                 return -1;
106         }
107
108         tot_len = read_compress_length(inbuf);
109         inbuf += LZO_LEN;
110         tot_in = LZO_LEN;
111
112         while (tot_in < tot_len) {
113                 in_len = read_compress_length(inbuf);
114                 inbuf += LZO_LEN;
115                 tot_in += LZO_LEN;
116
117                 new_len = lzo1x_worst_compress(PAGE_CACHE_SIZE);
118                 ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len,
119                                             (unsigned char *)outbuf,
120                                             (void *)&new_len, NULL);
121                 if (ret != LZO_E_OK) {
122                         fprintf(stderr, "failed to inflate: %d\n", ret);
123                         return -1;
124                 }
125                 out_len += new_len;
126                 outbuf += new_len;
127                 inbuf += in_len;
128                 tot_in += in_len;
129         }
130
131         *decompress_len = out_len;
132
133         return 0;
134 }
135
136 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
137                       u64 *decompress_len, int compress)
138 {
139         switch (compress) {
140         case BTRFS_COMPRESS_ZLIB:
141                 return decompress_zlib(inbuf, outbuf, compress_len,
142                                        *decompress_len);
143         case BTRFS_COMPRESS_LZO:
144                 return decompress_lzo((unsigned char *)inbuf, outbuf, compress_len,
145                                       decompress_len);
146         default:
147                 break;
148         }
149
150         fprintf(stderr, "invalid compression type: %d\n", compress);
151         return -1;
152 }
153
154 int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
155 {
156         int slot;
157         int level = 1;
158         int offset = 1;
159         struct extent_buffer *c;
160         struct extent_buffer *next = NULL;
161
162 again:
163         for (; level < BTRFS_MAX_LEVEL; level++) {
164                 if (path->nodes[level])
165                         break;
166         }
167
168         if (level == BTRFS_MAX_LEVEL)
169                 return 1;
170
171         slot = path->slots[level] + 1;
172
173         while(level < BTRFS_MAX_LEVEL) {
174                 if (!path->nodes[level])
175                         return 1;
176
177                 slot = path->slots[level] + offset;
178                 c = path->nodes[level];
179                 if (slot >= btrfs_header_nritems(c)) {
180                         level++;
181                         if (level == BTRFS_MAX_LEVEL)
182                                 return 1;
183                         continue;
184                 }
185
186                 if (path->reada)
187                         reada_for_search(root, path, level, slot, 0);
188
189                 next = read_node_slot(root, c, slot);
190                 if (next)
191                         break;
192                 offset++;
193         }
194         path->slots[level] = slot;
195         while(1) {
196                 level--;
197                 c = path->nodes[level];
198                 free_extent_buffer(c);
199                 path->nodes[level] = next;
200                 path->slots[level] = 0;
201                 if (!level)
202                         break;
203                 if (path->reada)
204                         reada_for_search(root, path, level, 0, 0);
205                 next = read_node_slot(root, next, 0);
206                 if (!next)
207                         goto again;
208         }
209         return 0;
210 }
211
212 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
213 {
214         struct extent_buffer *leaf = path->nodes[0];
215         struct btrfs_file_extent_item *fi;
216         char buf[4096];
217         char *outbuf;
218         u64 ram_size;
219         ssize_t done;
220         unsigned long ptr;
221         int ret;
222         int len;
223         int compress;
224
225         fi = btrfs_item_ptr(leaf, path->slots[0],
226                             struct btrfs_file_extent_item);
227         ptr = btrfs_file_extent_inline_start(fi);
228         len = btrfs_file_extent_inline_item_len(leaf,
229                                         btrfs_item_nr(leaf, path->slots[0]));
230         read_extent_buffer(leaf, buf, ptr, len);
231
232         compress = btrfs_file_extent_compression(leaf, fi);
233         if (compress == BTRFS_COMPRESS_NONE) {
234                 done = pwrite(fd, buf, len, pos);
235                 if (done < len) {
236                         fprintf(stderr, "Short inline write, wanted %d, did "
237                                 "%zd: %d\n", len, done, errno);
238                         return -1;
239                 }
240                 return 0;
241         }
242
243         ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
244         outbuf = malloc(ram_size);
245         if (!outbuf) {
246                 fprintf(stderr, "No memory\n");
247                 return -1;
248         }
249
250         ret = decompress(buf, outbuf, len, &ram_size, compress);
251         if (ret) {
252                 free(outbuf);
253                 return ret;
254         }
255
256         done = pwrite(fd, outbuf, ram_size, pos);
257         free(outbuf);
258         if (done < ram_size) {
259                 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
260                         "did %zd: %d\n", ram_size, done, errno);
261                 return -1;
262         }
263
264         return 0;
265 }
266
267 static int copy_one_extent(struct btrfs_root *root, int fd,
268                            struct extent_buffer *leaf,
269                            struct btrfs_file_extent_item *fi, u64 pos)
270 {
271         struct btrfs_multi_bio *multi = NULL;
272         struct btrfs_device *device;
273         char *inbuf, *outbuf = NULL;
274         ssize_t done, total = 0;
275         u64 bytenr;
276         u64 ram_size;
277         u64 disk_size;
278         u64 length;
279         u64 size_left;
280         u64 dev_bytenr;
281         u64 offset;
282         u64 count = 0;
283         int compress;
284         int ret;
285         int dev_fd;
286         int mirror_num = 1;
287         int num_copies;
288
289         compress = btrfs_file_extent_compression(leaf, fi);
290         bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
291         disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
292         ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
293         offset = btrfs_file_extent_offset(leaf, fi);
294         size_left = disk_size;
295
296         if (offset)
297                 printf("offset is %Lu\n", offset);
298         /* we found a hole */
299         if (disk_size == 0)
300                 return 0;
301
302         inbuf = malloc(disk_size);
303         if (!inbuf) {
304                 fprintf(stderr, "No memory\n");
305                 return -1;
306         }
307
308         if (compress != BTRFS_COMPRESS_NONE) {
309                 outbuf = malloc(ram_size);
310                 if (!outbuf) {
311                         fprintf(stderr, "No memory\n");
312                         free(inbuf);
313                         return -1;
314                 }
315         }
316 again:
317         length = size_left;
318         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
319                               bytenr, &length, &multi, mirror_num, NULL);
320         if (ret) {
321                 fprintf(stderr, "Error mapping block %d\n", ret);
322                 goto out;
323         }
324         device = multi->stripes[0].dev;
325         dev_fd = device->fd;
326         device->total_ios++;
327         dev_bytenr = multi->stripes[0].physical;
328         kfree(multi);
329
330         if (size_left < length)
331                 length = size_left;
332
333         done = pread(dev_fd, inbuf+count, length, dev_bytenr);
334         /* Need both checks, or we miss negative values due to u64 conversion */
335         if (done < 0 || done < length) {
336                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
337                                               bytenr, length);
338                 mirror_num++;
339                 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
340                 if (mirror_num > num_copies) {
341                         ret = -1;
342                         fprintf(stderr, "Exhausted mirrors trying to read\n");
343                         goto out;
344                 }
345                 fprintf(stderr, "Trying another mirror\n");
346                 goto again;
347         }
348
349         mirror_num = 1;
350         size_left -= length;
351         count += length;
352         bytenr += length;
353         if (size_left)
354                 goto again;
355
356         if (compress == BTRFS_COMPRESS_NONE) {
357                 while (total < ram_size) {
358                         done = pwrite(fd, inbuf+total, ram_size-total,
359                                       pos+total);
360                         if (done < 0) {
361                                 ret = -1;
362                                 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
363                                 goto out;
364                         }
365                         total += done;
366                 }
367                 ret = 0;
368                 goto out;
369         }
370
371         ret = decompress(inbuf, outbuf, disk_size, &ram_size, compress);
372         if (ret) {
373                 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
374                                               bytenr, length);
375                 mirror_num++;
376                 if (mirror_num >= num_copies) {
377                         ret = -1;
378                         goto out;
379                 }
380                 fprintf(stderr, "Trying another mirror\n");
381                 goto again;
382         }
383
384         while (total < ram_size) {
385                 done = pwrite(fd, outbuf+total, ram_size-total, pos+total);
386                 if (done < 0) {
387                         ret = -1;
388                         goto out;
389                 }
390                 total += done;
391         }
392 out:
393         free(inbuf);
394         free(outbuf);
395         return ret;
396 }
397
398 static int ask_to_continue(const char *file)
399 {
400         char buf[2];
401         char *ret;
402
403         printf("We seem to be looping a lot on %s, do you want to keep going "
404                "on ? (y/N): ", file);
405 again:
406         ret = fgets(buf, 2, stdin);
407         if (*ret == '\n' || tolower(*ret) == 'n')
408                 return 1;
409         if (tolower(*ret) != 'y') {
410                 printf("Please enter either 'y' or 'n': ");
411                 goto again;
412         }
413
414         return 0;
415 }
416
417
418 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
419                      const char *file)
420 {
421         struct extent_buffer *leaf;
422         struct btrfs_path *path;
423         struct btrfs_file_extent_item *fi;
424         struct btrfs_inode_item *inode_item;
425         struct btrfs_key found_key;
426         int ret;
427         int extent_type;
428         int compression;
429         int loops = 0;
430         u64 found_size = 0;
431
432         path = btrfs_alloc_path();
433         if (!path) {
434                 fprintf(stderr, "Ran out of memory\n");
435                 return -1;
436         }
437         path->skip_locking = 1;
438
439         ret = btrfs_lookup_inode(NULL, root, path, key, 0);
440         if (ret == 0) {
441                 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
442                                     struct btrfs_inode_item);
443                 found_size = btrfs_inode_size(path->nodes[0], inode_item);
444         }
445         btrfs_release_path(root, path);
446
447         key->offset = 0;
448         key->type = BTRFS_EXTENT_DATA_KEY;
449
450         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
451         if (ret < 0) {
452                 fprintf(stderr, "Error searching %d\n", ret);
453                 btrfs_free_path(path);
454                 return ret;
455         }
456
457         leaf = path->nodes[0];
458         while (!leaf) {
459                 ret = next_leaf(root, path);
460                 if (ret < 0) {
461                         fprintf(stderr, "Error getting next leaf %d\n",
462                                 ret);
463                         btrfs_free_path(path);
464                         return ret;
465                 } else if (ret > 0) {
466                         /* No more leaves to search */
467                         btrfs_free_path(path);
468                         return 0;
469                 }
470                 leaf = path->nodes[0];
471         }
472
473         while (1) {
474                 if (loops++ >= 1024) {
475                         ret = ask_to_continue(file);
476                         if (ret)
477                                 break;
478                         loops = 0;
479                 }
480                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
481                         do {
482                                 ret = next_leaf(root, path);
483                                 if (ret < 0) {
484                                         fprintf(stderr, "Error searching %d\n", ret);
485                                         btrfs_free_path(path);
486                                         return ret;
487                                 } else if (ret) {
488                                         /* No more leaves to search */
489                                         btrfs_free_path(path);
490                                         goto set_size;
491                                 }
492                                 leaf = path->nodes[0];
493                         } while (!leaf);
494                         continue;
495                 }
496                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
497                 if (found_key.objectid != key->objectid)
498                         break;
499                 if (found_key.type != key->type)
500                         break;
501                 fi = btrfs_item_ptr(leaf, path->slots[0],
502                                     struct btrfs_file_extent_item);
503                 extent_type = btrfs_file_extent_type(leaf, fi);
504                 compression = btrfs_file_extent_compression(leaf, fi);
505                 if (compression >= BTRFS_COMPRESS_LAST) {
506                         fprintf(stderr, "Don't support compression yet %d\n",
507                                 compression);
508                         btrfs_free_path(path);
509                         return -1;
510                 }
511
512                 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
513                         goto next;
514                 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
515                         ret = copy_one_inline(fd, path, found_key.offset);
516                         if (ret) {
517                                 btrfs_free_path(path);
518                                 return -1;
519                         }
520                 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
521                         ret = copy_one_extent(root, fd, leaf, fi,
522                                               found_key.offset);
523                         if (ret) {
524                                 btrfs_free_path(path);
525                                 return ret;
526                         }
527                 } else {
528                         printf("Weird extent type %d\n", extent_type);
529                 }
530 next:
531                 path->slots[0]++;
532         }
533
534         btrfs_free_path(path);
535 set_size:
536         if (found_size) {
537                 ret = ftruncate(fd, (loff_t)found_size);
538                 if (ret)
539                         return ret;
540         }
541         return 0;
542 }
543
544 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
545                       const char *output_rootdir, const char *dir,
546                       const regex_t *mreg)
547 {
548         struct btrfs_path *path;
549         struct extent_buffer *leaf;
550         struct btrfs_dir_item *dir_item;
551         struct btrfs_key found_key, location;
552         char filename[BTRFS_NAME_LEN + 1];
553         unsigned long name_ptr;
554         int name_len;
555         int ret;
556         int fd;
557         int loops = 0;
558         u8 type;
559
560         path = btrfs_alloc_path();
561         if (!path) {
562                 fprintf(stderr, "Ran out of memory\n");
563                 return -1;
564         }
565         path->skip_locking = 1;
566
567         key->offset = 0;
568         key->type = BTRFS_DIR_INDEX_KEY;
569
570         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
571         if (ret < 0) {
572                 fprintf(stderr, "Error searching %d\n", ret);
573                 btrfs_free_path(path);
574                 return ret;
575         }
576
577         leaf = path->nodes[0];
578         while (!leaf) {
579                 if (verbose > 1)
580                         printf("No leaf after search, looking for the next "
581                                "leaf\n");
582                 ret = next_leaf(root, path);
583                 if (ret < 0) {
584                         fprintf(stderr, "Error getting next leaf %d\n",
585                                 ret);
586                         btrfs_free_path(path);
587                         return ret;
588                 } else if (ret > 0) {
589                         /* No more leaves to search */
590                         if (verbose)
591                                 printf("Reached the end of the tree looking "
592                                        "for the directory\n");
593                         btrfs_free_path(path);
594                         return 0;
595                 }
596                 leaf = path->nodes[0];
597         }
598
599         while (leaf) {
600                 if (loops++ >= 1024) {
601                         printf("We have looped trying to restore files in %s "
602                                "too many times to be making progress, "
603                                "stopping\n", dir);
604                         break;
605                 }
606
607                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
608                         do {
609                                 ret = next_leaf(root, path);
610                                 if (ret < 0) {
611                                         fprintf(stderr, "Error searching %d\n",
612                                                 ret);
613                                         btrfs_free_path(path);
614                                         return ret;
615                                 } else if (ret > 0) {
616                                         /* No more leaves to search */
617                                         if (verbose)
618                                                 printf("Reached the end of "
619                                                        "the tree searching the"
620                                                        " directory\n");
621                                         btrfs_free_path(path);
622                                         return 0;
623                                 }
624                                 leaf = path->nodes[0];
625                         } while (!leaf);
626                         continue;
627                 }
628                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
629                 if (found_key.objectid != key->objectid) {
630                         if (verbose > 1)
631                                 printf("Found objectid=%Lu, key=%Lu\n",
632                                        found_key.objectid, key->objectid);
633                         break;
634                 }
635                 if (found_key.type != key->type) {
636                         if (verbose > 1)
637                                 printf("Found type=%u, want=%u\n",
638                                        found_key.type, key->type);
639                         break;
640                 }
641                 dir_item = btrfs_item_ptr(leaf, path->slots[0],
642                                           struct btrfs_dir_item);
643                 name_ptr = (unsigned long)(dir_item + 1);
644                 name_len = btrfs_dir_name_len(leaf, dir_item);
645                 read_extent_buffer(leaf, filename, name_ptr, name_len);
646                 filename[name_len] = '\0';
647                 type = btrfs_dir_type(leaf, dir_item);
648                 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
649
650                 /* full path from root of btrfs being restored */
651                 snprintf(fs_name, 4096, "%s/%s", dir, filename);
652
653                 if (mreg && REG_NOMATCH == regexec(mreg, fs_name, 0, NULL, 0))
654                         goto next;
655
656                 /* full path from system root */
657                 snprintf(path_name, 4096, "%s%s", output_rootdir, fs_name);
658
659                 /*
660                  * At this point we're only going to restore directories and
661                  * files, no symlinks or anything else.
662                  */
663                 if (type == BTRFS_FT_REG_FILE) {
664                         if (!overwrite) {
665                                 static int warn = 0;
666                                 struct stat st;
667
668                                 ret = stat(path_name, &st);
669                                 if (!ret) {
670                                         loops = 0;
671                                         if (verbose || !warn)
672                                                 printf("Skipping existing file"
673                                                        " %s\n", path_name);
674                                         if (warn)
675                                                 goto next;
676                                         printf("If you wish to overwrite use "
677                                                "the -o option to overwrite\n");
678                                         warn = 1;
679                                         goto next;
680                                 }
681                                 ret = 0;
682                         }
683                         if (verbose)
684                                 printf("Restoring %s\n", path_name);
685                         fd = open(path_name, O_CREAT|O_WRONLY, 0644);
686                         if (fd < 0) {
687                                 fprintf(stderr, "Error creating %s: %d\n",
688                                         path_name, errno);
689                                 if (ignore_errors)
690                                         goto next;
691                                 btrfs_free_path(path);
692                                 return -1;
693                         }
694                         loops = 0;
695                         ret = copy_file(root, fd, &location, path_name);
696                         close(fd);
697                         if (ret) {
698                                 if (ignore_errors)
699                                         goto next;
700                                 btrfs_free_path(path);
701                                 return ret;
702                         }
703                 } else if (type == BTRFS_FT_DIR) {
704                         struct btrfs_root *search_root = root;
705                         char *dir = strdup(fs_name);
706
707                         if (!dir) {
708                                 fprintf(stderr, "Ran out of memory\n");
709                                 btrfs_free_path(path);
710                                 return -1;
711                         }
712
713                         if (location.type == BTRFS_ROOT_ITEM_KEY) {
714                                 /*
715                                  * If we are a snapshot and this is the index
716                                  * object to ourselves just skip it.
717                                  */
718                                 if (location.objectid ==
719                                     root->root_key.objectid) {
720                                         free(dir);
721                                         goto next;
722                                 }
723
724                                 location.offset = (u64)-1;
725                                 search_root = btrfs_read_fs_root(root->fs_info,
726                                                                  &location);
727                                 if (IS_ERR(search_root)) {
728                                         free(dir);
729                                         fprintf(stderr, "Error reading "
730                                                 "subvolume %s: %lu\n",
731                                                 path_name,
732                                                 PTR_ERR(search_root));
733                                         if (ignore_errors)
734                                                 goto next;
735                                         btrfs_free_path(path);
736                                         return PTR_ERR(search_root);
737                                 }
738
739                                 /*
740                                  * A subvolume will have a key.offset of 0, a
741                                  * snapshot will have key.offset of a transid.
742                                  */
743                                 if (search_root->root_key.offset != 0 &&
744                                     get_snaps == 0) {
745                                         free(dir);
746                                         printf("Skipping snapshot %s\n",
747                                                filename);
748                                         goto next;
749                                 }
750                                 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
751                         }
752
753                         if (verbose)
754                                 printf("Restoring %s\n", path_name);
755
756                         errno = 0;
757                         ret = mkdir(path_name, 0755);
758                         if (ret && errno != EEXIST) {
759                                 free(dir);
760                                 fprintf(stderr, "Error mkdiring %s: %d\n",
761                                         path_name, errno);
762                                 if (ignore_errors)
763                                         goto next;
764                                 btrfs_free_path(path);
765                                 return -1;
766                         }
767                         loops = 0;
768                         ret = search_dir(search_root, &location,
769                                          output_rootdir, dir, mreg);
770                         free(dir);
771                         if (ret) {
772                                 if (ignore_errors)
773                                         goto next;
774                                 btrfs_free_path(path);
775                                 return ret;
776                         }
777                 }
778 next:
779                 path->slots[0]++;
780         }
781
782         if (verbose)
783                 printf("Done searching %s\n", dir);
784         btrfs_free_path(path);
785         return 0;
786 }
787
788 static int do_list_roots(struct btrfs_root *root)
789 {
790         struct btrfs_key key;
791         struct btrfs_key found_key;
792         struct btrfs_disk_key disk_key;
793         struct btrfs_path *path;
794         struct extent_buffer *leaf;
795         struct btrfs_root_item ri;
796         unsigned long offset;
797         int slot;
798         int ret;
799
800         root = root->fs_info->tree_root;
801         path = btrfs_alloc_path();
802         if (!path) {
803                 fprintf(stderr, "Failed to alloc path\n");
804                 return -1;
805         }
806
807         key.offset = 0;
808         key.objectid = 0;
809         key.type = BTRFS_ROOT_ITEM_KEY;
810
811         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
812         if (ret < 0) {
813                 fprintf(stderr, "Failed to do search %d\n", ret);
814                 btrfs_free_path(path);
815                 return -1;
816         }
817
818         while (1) {
819                 leaf = path->nodes[0];
820                 slot = path->slots[0];
821                 if (slot >= btrfs_header_nritems(leaf)) {
822                         ret = btrfs_next_leaf(root, path);
823                         if (ret)
824                                 break;
825                         leaf = path->nodes[0];
826                         slot = path->slots[0];
827                 }
828                 btrfs_item_key(leaf, &disk_key, slot);
829                 btrfs_disk_key_to_cpu(&found_key, &disk_key);
830                 if (btrfs_key_type(&found_key) != BTRFS_ROOT_ITEM_KEY) {
831                         path->slots[0]++;
832                         continue;
833                 }
834
835                 offset = btrfs_item_ptr_offset(leaf, slot);
836                 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
837                 printf(" tree ");
838                 btrfs_print_key(&disk_key);
839                 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
840                        btrfs_root_level(&ri));
841                 path->slots[0]++;
842         }
843         btrfs_free_path(path);
844
845         return 0;
846 }
847
848 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
849                                   int super_mirror, int list_roots)
850 {
851         struct btrfs_fs_info *fs_info = NULL;
852         struct btrfs_root *root = NULL;
853         u64 bytenr;
854         int i;
855
856         for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
857                 bytenr = btrfs_sb_offset(i);
858                 fs_info = open_ctree_fs_info(dev, bytenr, root_location, 0, 1);
859                 if (fs_info)
860                         break;
861                 fprintf(stderr, "Could not open root, trying backup super\n");
862         }
863
864         if (!fs_info)
865                 return NULL;
866
867         /*
868          * All we really need to succeed is reading the chunk tree, everything
869          * else we can do by hand, since we only need to read the tree root and
870          * the fs_root.
871          */
872         if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
873                 u64 generation;
874
875                 root = fs_info->tree_root;
876                 if (!root_location)
877                         root_location = btrfs_super_root(fs_info->super_copy);
878                 generation = btrfs_super_generation(fs_info->super_copy);
879                 root->node = read_tree_block(root, root_location,
880                                              root->leafsize, generation);
881                 if (!extent_buffer_uptodate(root->node)) {
882                         fprintf(stderr, "Error opening tree root\n");
883                         close_ctree(root);
884                         return NULL;
885                 }
886         }
887
888         if (!list_roots && !fs_info->fs_root) {
889                 struct btrfs_key key;
890
891                 key.objectid = BTRFS_FS_TREE_OBJECTID;
892                 key.type = BTRFS_ROOT_ITEM_KEY;
893                 key.offset = (u64)-1;
894                 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
895                 if (IS_ERR(fs_info->fs_root)) {
896                         fprintf(stderr, "Couldn't read fs root: %ld\n",
897                                 PTR_ERR(fs_info->fs_root));
898                         close_ctree(fs_info->tree_root);
899                         return NULL;
900                 }
901         }
902
903         if (list_roots && do_list_roots(fs_info->tree_root)) {
904                 close_ctree(fs_info->tree_root);
905                 return NULL;
906         }
907
908         return fs_info->fs_root;
909 }
910
911 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
912 {
913         struct btrfs_path *path;
914         struct btrfs_key found_key;
915         struct btrfs_key key;
916         int ret = -1;
917         int i;
918
919         key.objectid = 0;
920         key.type = BTRFS_DIR_INDEX_KEY;
921         key.offset = 0;
922
923         path = btrfs_alloc_path();
924         if (!path) {
925                 fprintf(stderr, "Ran out of memory\n");
926                 return ret;
927         }
928
929         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
930         if (ret < 0) {
931                 fprintf(stderr, "Error searching %d\n", ret);
932                 goto out;
933         }
934
935         if (!path->nodes[0]) {
936                 fprintf(stderr, "No leaf!\n");
937                 goto out;
938         }
939 again:
940         for (i = path->slots[0];
941              i < btrfs_header_nritems(path->nodes[0]); i++) {
942                 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
943                 if (found_key.type != key.type)
944                         continue;
945
946                 printf("Using objectid %Lu for first dir\n",
947                        found_key.objectid);
948                 *objectid = found_key.objectid;
949                 ret = 0;
950                 goto out;
951         }
952         do {
953                 ret = next_leaf(root, path);
954                 if (ret < 0) {
955                         fprintf(stderr, "Error getting next leaf %d\n",
956                                 ret);
957                         goto out;
958                 } else if (ret > 0) {
959                         fprintf(stderr, "No more leaves\n");
960                         goto out;
961                 }
962         } while (!path->nodes[0]);
963         if (path->nodes[0])
964                 goto again;
965         printf("Couldn't find a dir index item\n");
966 out:
967         btrfs_free_path(path);
968         return ret;
969 }
970
971 static struct option long_options[] = {
972         { "path-regex", 1, NULL, 256},
973         { 0, 0, 0, 0}
974 };
975
976 const char * const cmd_restore_usage[] = {
977         "btrfs restore [options] <device>",
978         "Try to restore files from a damaged filesystem (unmounted)",
979         "",
980         "-s              get snapshots",
981         "-v              verbose",
982         "-i              ignore errors",
983         "-o              overwrite",
984         "-t              tree location",
985         "-f <offset>     filesystem location",
986         "-u <block>      super mirror",
987         "-d              find dir",
988         "--path-regex <regex>",
989         "                restore only filenames matching regex,",
990         "                you have to use following syntax (possibly quoted):",
991         "                ^/(|home(|/username(|/Desktop(|/.*))))$",
992         NULL
993 };
994
995 int cmd_restore(int argc, char **argv)
996 {
997         struct btrfs_root *root;
998         struct btrfs_key key;
999         char dir_name[128];
1000         u64 tree_location = 0;
1001         u64 fs_location = 0;
1002         u64 root_objectid = 0;
1003         int len;
1004         int ret;
1005         int opt;
1006         int option_index = 0;
1007         int super_mirror = 0;
1008         int find_dir = 0;
1009         int list_roots = 0;
1010         const char *match_regstr = NULL;
1011         int match_cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
1012         regex_t match_reg, *mreg = NULL;
1013         char reg_err[256];
1014
1015         while ((opt = getopt_long(argc, argv, "sviot:u:df:r:lc", long_options,
1016                                         &option_index)) != -1) {
1017
1018                 switch (opt) {
1019                         case 's':
1020                                 get_snaps = 1;
1021                                 break;
1022                         case 'v':
1023                                 verbose++;
1024                                 break;
1025                         case 'i':
1026                                 ignore_errors = 1;
1027                                 break;
1028                         case 'o':
1029                                 overwrite = 1;
1030                                 break;
1031                         case 't':
1032                                 errno = 0;
1033                                 tree_location = (u64)strtoll(optarg, NULL, 10);
1034                                 if (errno != 0) {
1035                                         fprintf(stderr, "Tree location not valid\n");
1036                                         exit(1);
1037                                 }
1038                                 break;
1039                         case 'f':
1040                                 errno = 0;
1041                                 fs_location = (u64)strtoll(optarg, NULL, 10);
1042                                 if (errno != 0) {
1043                                         fprintf(stderr, "Fs location not valid\n");
1044                                         exit(1);
1045                                 }
1046                                 break;
1047                         case 'u':
1048                                 errno = 0;
1049                                 super_mirror = (int)strtol(optarg, NULL, 10);
1050                                 if (errno != 0 ||
1051                                     super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1052                                         fprintf(stderr, "Super mirror not "
1053                                                 "valid\n");
1054                                         exit(1);
1055                                 }
1056                                 break;
1057                         case 'd':
1058                                 find_dir = 1;
1059                                 break;
1060                         case 'r':
1061                                 errno = 0;
1062                                 root_objectid = (u64)strtoll(optarg, NULL, 10);
1063                                 if (errno != 0) {
1064                                         fprintf(stderr, "Root objectid not valid\n");
1065                                         exit(1);
1066                                 }
1067                                 break;
1068                         case 'l':
1069                                 list_roots = 1;
1070                                 break;
1071                         case 'c':
1072                                 match_cflags |= REG_ICASE;
1073                                 break;
1074                         /* long option without single letter alternative */
1075                         case 256:
1076                                 match_regstr = optarg;
1077                                 break;
1078                         default:
1079                                 usage(cmd_restore_usage);
1080                 }
1081         }
1082
1083         if (!list_roots && optind + 1 >= argc)
1084                 usage(cmd_restore_usage);
1085         else if (list_roots && optind >= argc)
1086                 usage(cmd_restore_usage);
1087
1088         if ((ret = check_mounted(argv[optind])) < 0) {
1089                 fprintf(stderr, "Could not check mount status: %s\n",
1090                         strerror(-ret));
1091                 return ret;
1092         } else if (ret) {
1093                 fprintf(stderr, "%s is currently mounted.  Aborting.\n", argv[optind]);
1094                 return 1;
1095         }
1096
1097         root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1098         if (root == NULL)
1099                 return 1;
1100
1101         if (list_roots)
1102                 goto out;
1103
1104         if (fs_location != 0) {
1105                 free_extent_buffer(root->node);
1106                 root->node = read_tree_block(root, fs_location, root->leafsize, 0);
1107                 if (!root->node) {
1108                         fprintf(stderr, "Failed to read fs location\n");
1109                         goto out;
1110                 }
1111         }
1112
1113         memset(path_name, 0, 4096);
1114
1115         strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1116         dir_name[sizeof dir_name - 1] = 0;
1117
1118         /* Strip the trailing / on the dir name */
1119         len = strlen(dir_name);
1120         while (len && dir_name[--len] == '/') {
1121                 dir_name[len] = '\0';
1122         }
1123
1124         if (root_objectid != 0) {
1125                 struct btrfs_root *orig_root = root;
1126
1127                 key.objectid = root_objectid;
1128                 key.type = BTRFS_ROOT_ITEM_KEY;
1129                 key.offset = (u64)-1;
1130                 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1131                 if (IS_ERR(root)) {
1132                         fprintf(stderr, "Error reading root\n");
1133                         root = orig_root;
1134                         ret = 1;
1135                         goto out;
1136                 }
1137                 key.type = 0;
1138                 key.offset = 0;
1139         }
1140
1141         if (find_dir) {
1142                 ret = find_first_dir(root, &key.objectid);
1143                 if (ret)
1144                         goto out;
1145         } else {
1146                 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1147         }
1148
1149         if (match_regstr) {
1150                 ret = regcomp(&match_reg, match_regstr, match_cflags);
1151                 if (ret) {
1152                         regerror(ret, &match_reg, reg_err, sizeof(reg_err));
1153                         fprintf(stderr, "Regex compile failed: %s\n", reg_err);
1154                         goto out;
1155                 }
1156                 mreg = &match_reg;
1157         }
1158
1159         ret = search_dir(root, &key, dir_name, "", mreg);
1160
1161 out:
1162         if (mreg)
1163                 regfree(mreg);
1164         close_ctree(root);
1165         return ret;
1166 }