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