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