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