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