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