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