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