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