2 * Copyright (C) 2011 Red Hat. All rights reserved.
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.
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.
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.
19 #define _XOPEN_SOURCE 500
22 #include "kerncompat.h"
30 #include <sys/types.h>
31 #include <lzo/lzoconf.h>
32 #include <lzo/lzo1x.h>
39 #include "print-tree.h"
40 #include "transaction.h"
47 static char fs_name[4096];
48 static char path_name[4096];
49 static int get_snaps = 0;
50 static int verbose = 0;
51 static int ignore_errors = 0;
52 static int overwrite = 0;
55 #define PAGE_CACHE_SIZE 4096
56 #define lzo1x_worst_compress(x) ((x) + ((x) / 16) + 64 + 3)
58 static int decompress_zlib(char *inbuf, char *outbuf, u64 compress_len,
64 memset(&strm, 0, sizeof(strm));
65 ret = inflateInit(&strm);
67 fprintf(stderr, "inflate init returnd %d\n", ret);
71 strm.avail_in = compress_len;
72 strm.next_in = (unsigned char *)inbuf;
73 strm.avail_out = decompress_len;
74 strm.next_out = (unsigned char *)outbuf;
75 ret = inflate(&strm, Z_NO_FLUSH);
76 if (ret != Z_STREAM_END) {
77 (void)inflateEnd(&strm);
78 fprintf(stderr, "failed to inflate: %d\n", ret);
82 (void)inflateEnd(&strm);
85 static inline size_t read_compress_length(unsigned char *buf)
88 memcpy(&dlen, buf, LZO_LEN);
89 return le32_to_cpu(dlen);
92 static int decompress_lzo(unsigned char *inbuf, char *outbuf, u64 compress_len,
103 if (ret != LZO_E_OK) {
104 fprintf(stderr, "lzo init returned %d\n", ret);
108 tot_len = read_compress_length(inbuf);
112 while (tot_in < tot_len) {
113 in_len = read_compress_length(inbuf);
117 new_len = lzo1x_worst_compress(PAGE_CACHE_SIZE);
118 ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len,
119 (unsigned char *)outbuf,
120 (void *)&new_len, NULL);
121 if (ret != LZO_E_OK) {
122 fprintf(stderr, "failed to inflate: %d\n", ret);
131 *decompress_len = out_len;
136 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
137 u64 *decompress_len, int compress)
140 case BTRFS_COMPRESS_ZLIB:
141 return decompress_zlib(inbuf, outbuf, compress_len,
143 case BTRFS_COMPRESS_LZO:
144 return decompress_lzo((unsigned char *)inbuf, outbuf, compress_len,
150 fprintf(stderr, "invalid compression type: %d\n", compress);
154 int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
159 struct extent_buffer *c;
160 struct extent_buffer *next = NULL;
163 for (; level < BTRFS_MAX_LEVEL; level++) {
164 if (path->nodes[level])
168 if (level == BTRFS_MAX_LEVEL)
171 slot = path->slots[level] + 1;
173 while(level < BTRFS_MAX_LEVEL) {
174 if (!path->nodes[level])
177 slot = path->slots[level] + offset;
178 c = path->nodes[level];
179 if (slot >= btrfs_header_nritems(c)) {
181 if (level == BTRFS_MAX_LEVEL)
187 reada_for_search(root, path, level, slot, 0);
189 next = read_node_slot(root, c, slot);
194 path->slots[level] = slot;
197 c = path->nodes[level];
198 free_extent_buffer(c);
199 path->nodes[level] = next;
200 path->slots[level] = 0;
204 reada_for_search(root, path, level, 0, 0);
205 next = read_node_slot(root, next, 0);
212 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
214 struct extent_buffer *leaf = path->nodes[0];
215 struct btrfs_file_extent_item *fi;
225 fi = btrfs_item_ptr(leaf, path->slots[0],
226 struct btrfs_file_extent_item);
227 ptr = btrfs_file_extent_inline_start(fi);
228 len = btrfs_file_extent_inline_item_len(leaf,
229 btrfs_item_nr(leaf, path->slots[0]));
230 read_extent_buffer(leaf, buf, ptr, len);
232 compress = btrfs_file_extent_compression(leaf, fi);
233 if (compress == BTRFS_COMPRESS_NONE) {
234 done = pwrite(fd, buf, len, pos);
236 fprintf(stderr, "Short inline write, wanted %d, did "
237 "%zd: %d\n", len, done, errno);
243 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
244 outbuf = malloc(ram_size);
246 fprintf(stderr, "No memory\n");
250 ret = decompress(buf, outbuf, len, &ram_size, compress);
256 done = pwrite(fd, outbuf, ram_size, pos);
258 if (done < ram_size) {
259 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
260 "did %zd: %d\n", ram_size, done, errno);
267 static int copy_one_extent(struct btrfs_root *root, int fd,
268 struct extent_buffer *leaf,
269 struct btrfs_file_extent_item *fi, u64 pos)
271 struct btrfs_multi_bio *multi = NULL;
272 struct btrfs_device *device;
273 char *inbuf, *outbuf = NULL;
274 ssize_t done, total = 0;
289 compress = btrfs_file_extent_compression(leaf, fi);
290 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
291 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
292 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
293 offset = btrfs_file_extent_offset(leaf, fi);
294 size_left = disk_size;
297 printf("offset is %Lu\n", offset);
298 /* we found a hole */
302 inbuf = malloc(disk_size);
304 fprintf(stderr, "No memory\n");
308 if (compress != BTRFS_COMPRESS_NONE) {
309 outbuf = malloc(ram_size);
311 fprintf(stderr, "No memory\n");
318 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
319 bytenr, &length, &multi, mirror_num, NULL);
321 fprintf(stderr, "Error mapping block %d\n", ret);
324 device = multi->stripes[0].dev;
327 dev_bytenr = multi->stripes[0].physical;
330 if (size_left < length)
333 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
334 /* Need both checks, or we miss negative values due to u64 conversion */
335 if (done < 0 || done < length) {
336 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
339 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
340 if (mirror_num > num_copies) {
342 fprintf(stderr, "Exhausted mirrors trying to read\n");
345 fprintf(stderr, "Trying another mirror\n");
356 if (compress == BTRFS_COMPRESS_NONE) {
357 while (total < ram_size) {
358 done = pwrite(fd, inbuf+total, ram_size-total,
362 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
371 ret = decompress(inbuf, outbuf, disk_size, &ram_size, compress);
373 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
376 if (mirror_num >= num_copies) {
380 fprintf(stderr, "Trying another mirror\n");
384 while (total < ram_size) {
385 done = pwrite(fd, outbuf+total, ram_size-total, pos+total);
398 static int ask_to_continue(const char *file)
403 printf("We seem to be looping a lot on %s, do you want to keep going "
404 "on ? (y/N): ", file);
406 ret = fgets(buf, 2, stdin);
407 if (*ret == '\n' || tolower(*ret) == 'n')
409 if (tolower(*ret) != 'y') {
410 printf("Please enter either 'y' or 'n': ");
418 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
421 struct extent_buffer *leaf;
422 struct btrfs_path *path;
423 struct btrfs_file_extent_item *fi;
424 struct btrfs_inode_item *inode_item;
425 struct btrfs_key found_key;
432 path = btrfs_alloc_path();
434 fprintf(stderr, "Ran out of memory\n");
437 path->skip_locking = 1;
439 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
441 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
442 struct btrfs_inode_item);
443 found_size = btrfs_inode_size(path->nodes[0], inode_item);
445 btrfs_release_path(root, path);
448 key->type = BTRFS_EXTENT_DATA_KEY;
450 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
452 fprintf(stderr, "Error searching %d\n", ret);
453 btrfs_free_path(path);
457 leaf = path->nodes[0];
459 ret = next_leaf(root, path);
461 fprintf(stderr, "Error getting next leaf %d\n",
463 btrfs_free_path(path);
465 } else if (ret > 0) {
466 /* No more leaves to search */
467 btrfs_free_path(path);
470 leaf = path->nodes[0];
474 if (loops++ >= 1024) {
475 ret = ask_to_continue(file);
480 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
482 ret = next_leaf(root, path);
484 fprintf(stderr, "Error searching %d\n", ret);
485 btrfs_free_path(path);
488 /* No more leaves to search */
489 btrfs_free_path(path);
492 leaf = path->nodes[0];
496 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
497 if (found_key.objectid != key->objectid)
499 if (found_key.type != key->type)
501 fi = btrfs_item_ptr(leaf, path->slots[0],
502 struct btrfs_file_extent_item);
503 extent_type = btrfs_file_extent_type(leaf, fi);
504 compression = btrfs_file_extent_compression(leaf, fi);
505 if (compression >= BTRFS_COMPRESS_LAST) {
506 fprintf(stderr, "Don't support compression yet %d\n",
508 btrfs_free_path(path);
512 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
514 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
515 ret = copy_one_inline(fd, path, found_key.offset);
517 btrfs_free_path(path);
520 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
521 ret = copy_one_extent(root, fd, leaf, fi,
524 btrfs_free_path(path);
528 printf("Weird extent type %d\n", extent_type);
534 btrfs_free_path(path);
537 ret = ftruncate(fd, (loff_t)found_size);
544 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
545 const char *output_rootdir, const char *dir,
548 struct btrfs_path *path;
549 struct extent_buffer *leaf;
550 struct btrfs_dir_item *dir_item;
551 struct btrfs_key found_key, location;
552 char filename[BTRFS_NAME_LEN + 1];
553 unsigned long name_ptr;
560 path = btrfs_alloc_path();
562 fprintf(stderr, "Ran out of memory\n");
565 path->skip_locking = 1;
568 key->type = BTRFS_DIR_INDEX_KEY;
570 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
572 fprintf(stderr, "Error searching %d\n", ret);
573 btrfs_free_path(path);
577 leaf = path->nodes[0];
580 printf("No leaf after search, looking for the next "
582 ret = next_leaf(root, path);
584 fprintf(stderr, "Error getting next leaf %d\n",
586 btrfs_free_path(path);
588 } else if (ret > 0) {
589 /* No more leaves to search */
591 printf("Reached the end of the tree looking "
592 "for the directory\n");
593 btrfs_free_path(path);
596 leaf = path->nodes[0];
600 if (loops++ >= 1024) {
601 printf("We have looped trying to restore files in %s "
602 "too many times to be making progress, "
607 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
609 ret = next_leaf(root, path);
611 fprintf(stderr, "Error searching %d\n",
613 btrfs_free_path(path);
615 } else if (ret > 0) {
616 /* No more leaves to search */
618 printf("Reached the end of "
619 "the tree searching the"
621 btrfs_free_path(path);
624 leaf = path->nodes[0];
628 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
629 if (found_key.objectid != key->objectid) {
631 printf("Found objectid=%Lu, key=%Lu\n",
632 found_key.objectid, key->objectid);
635 if (found_key.type != key->type) {
637 printf("Found type=%u, want=%u\n",
638 found_key.type, key->type);
641 dir_item = btrfs_item_ptr(leaf, path->slots[0],
642 struct btrfs_dir_item);
643 name_ptr = (unsigned long)(dir_item + 1);
644 name_len = btrfs_dir_name_len(leaf, dir_item);
645 read_extent_buffer(leaf, filename, name_ptr, name_len);
646 filename[name_len] = '\0';
647 type = btrfs_dir_type(leaf, dir_item);
648 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
650 /* full path from root of btrfs being restored */
651 snprintf(fs_name, 4096, "%s/%s", dir, filename);
653 if (mreg && REG_NOMATCH == regexec(mreg, fs_name, 0, NULL, 0))
656 /* full path from system root */
657 snprintf(path_name, 4096, "%s%s", output_rootdir, fs_name);
660 * At this point we're only going to restore directories and
661 * files, no symlinks or anything else.
663 if (type == BTRFS_FT_REG_FILE) {
668 ret = stat(path_name, &st);
671 if (verbose || !warn)
672 printf("Skipping existing file"
676 printf("If you wish to overwrite use "
677 "the -o option to overwrite\n");
684 printf("Restoring %s\n", path_name);
685 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
687 fprintf(stderr, "Error creating %s: %d\n",
691 btrfs_free_path(path);
695 ret = copy_file(root, fd, &location, path_name);
700 btrfs_free_path(path);
703 } else if (type == BTRFS_FT_DIR) {
704 struct btrfs_root *search_root = root;
705 char *dir = strdup(fs_name);
708 fprintf(stderr, "Ran out of memory\n");
709 btrfs_free_path(path);
713 if (location.type == BTRFS_ROOT_ITEM_KEY) {
715 * If we are a snapshot and this is the index
716 * object to ourselves just skip it.
718 if (location.objectid ==
719 root->root_key.objectid) {
724 location.offset = (u64)-1;
725 search_root = btrfs_read_fs_root(root->fs_info,
727 if (IS_ERR(search_root)) {
729 fprintf(stderr, "Error reading "
730 "subvolume %s: %lu\n",
732 PTR_ERR(search_root));
735 btrfs_free_path(path);
736 return PTR_ERR(search_root);
740 * A subvolume will have a key.offset of 0, a
741 * snapshot will have key.offset of a transid.
743 if (search_root->root_key.offset != 0 &&
746 printf("Skipping snapshot %s\n",
750 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
754 printf("Restoring %s\n", path_name);
757 ret = mkdir(path_name, 0755);
758 if (ret && errno != EEXIST) {
760 fprintf(stderr, "Error mkdiring %s: %d\n",
764 btrfs_free_path(path);
768 ret = search_dir(search_root, &location,
769 output_rootdir, dir, mreg);
774 btrfs_free_path(path);
783 printf("Done searching %s\n", dir);
784 btrfs_free_path(path);
788 static int do_list_roots(struct btrfs_root *root)
790 struct btrfs_key key;
791 struct btrfs_key found_key;
792 struct btrfs_disk_key disk_key;
793 struct btrfs_path *path;
794 struct extent_buffer *leaf;
795 struct btrfs_root_item ri;
796 unsigned long offset;
800 root = root->fs_info->tree_root;
801 path = btrfs_alloc_path();
803 fprintf(stderr, "Failed to alloc path\n");
809 key.type = BTRFS_ROOT_ITEM_KEY;
811 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
813 fprintf(stderr, "Failed to do search %d\n", ret);
814 btrfs_free_path(path);
819 leaf = path->nodes[0];
820 slot = path->slots[0];
821 if (slot >= btrfs_header_nritems(leaf)) {
822 ret = btrfs_next_leaf(root, path);
825 leaf = path->nodes[0];
826 slot = path->slots[0];
828 btrfs_item_key(leaf, &disk_key, slot);
829 btrfs_disk_key_to_cpu(&found_key, &disk_key);
830 if (btrfs_key_type(&found_key) != BTRFS_ROOT_ITEM_KEY) {
835 offset = btrfs_item_ptr_offset(leaf, slot);
836 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
838 btrfs_print_key(&disk_key);
839 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
840 btrfs_root_level(&ri));
843 btrfs_free_path(path);
848 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
849 int super_mirror, int list_roots)
851 struct btrfs_fs_info *fs_info = NULL;
852 struct btrfs_root *root = NULL;
856 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
857 bytenr = btrfs_sb_offset(i);
858 fs_info = open_ctree_fs_info(dev, bytenr, root_location, 0, 1);
861 fprintf(stderr, "Could not open root, trying backup super\n");
868 * All we really need to succeed is reading the chunk tree, everything
869 * else we can do by hand, since we only need to read the tree root and
872 if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
875 root = fs_info->tree_root;
877 root_location = btrfs_super_root(fs_info->super_copy);
878 generation = btrfs_super_generation(fs_info->super_copy);
879 root->node = read_tree_block(root, root_location,
880 root->leafsize, generation);
881 if (!extent_buffer_uptodate(root->node)) {
882 fprintf(stderr, "Error opening tree root\n");
888 if (!list_roots && !fs_info->fs_root) {
889 struct btrfs_key key;
891 key.objectid = BTRFS_FS_TREE_OBJECTID;
892 key.type = BTRFS_ROOT_ITEM_KEY;
893 key.offset = (u64)-1;
894 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
895 if (IS_ERR(fs_info->fs_root)) {
896 fprintf(stderr, "Couldn't read fs root: %ld\n",
897 PTR_ERR(fs_info->fs_root));
898 close_ctree(fs_info->tree_root);
903 if (list_roots && do_list_roots(fs_info->tree_root)) {
904 close_ctree(fs_info->tree_root);
908 return fs_info->fs_root;
911 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
913 struct btrfs_path *path;
914 struct btrfs_key found_key;
915 struct btrfs_key key;
920 key.type = BTRFS_DIR_INDEX_KEY;
923 path = btrfs_alloc_path();
925 fprintf(stderr, "Ran out of memory\n");
929 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
931 fprintf(stderr, "Error searching %d\n", ret);
935 if (!path->nodes[0]) {
936 fprintf(stderr, "No leaf!\n");
940 for (i = path->slots[0];
941 i < btrfs_header_nritems(path->nodes[0]); i++) {
942 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
943 if (found_key.type != key.type)
946 printf("Using objectid %Lu for first dir\n",
948 *objectid = found_key.objectid;
953 ret = next_leaf(root, path);
955 fprintf(stderr, "Error getting next leaf %d\n",
958 } else if (ret > 0) {
959 fprintf(stderr, "No more leaves\n");
962 } while (!path->nodes[0]);
965 printf("Couldn't find a dir index item\n");
967 btrfs_free_path(path);
971 static struct option long_options[] = {
972 { "path-regex", 1, NULL, 256},
976 const char * const cmd_restore_usage[] = {
977 "btrfs restore [options] <device>",
978 "Try to restore files from a damaged filesystem (unmounted)",
985 "-f <offset> filesystem location",
986 "-u <block> super mirror",
988 "--path-regex <regex>",
989 " restore only filenames matching regex,",
990 " you have to use following syntax (possibly quoted):",
991 " ^/(|home(|/username(|/Desktop(|/.*))))$",
995 int cmd_restore(int argc, char **argv)
997 struct btrfs_root *root;
998 struct btrfs_key key;
1000 u64 tree_location = 0;
1001 u64 fs_location = 0;
1002 u64 root_objectid = 0;
1006 int option_index = 0;
1007 int super_mirror = 0;
1010 const char *match_regstr = NULL;
1011 int match_cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
1012 regex_t match_reg, *mreg = NULL;
1015 while ((opt = getopt_long(argc, argv, "sviot:u:df:r:lc", long_options,
1016 &option_index)) != -1) {
1033 tree_location = (u64)strtoll(optarg, NULL, 10);
1035 fprintf(stderr, "Tree location not valid\n");
1041 fs_location = (u64)strtoll(optarg, NULL, 10);
1043 fprintf(stderr, "Fs location not valid\n");
1049 super_mirror = (int)strtol(optarg, NULL, 10);
1051 super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1052 fprintf(stderr, "Super mirror not "
1062 root_objectid = (u64)strtoll(optarg, NULL, 10);
1064 fprintf(stderr, "Root objectid not valid\n");
1072 match_cflags |= REG_ICASE;
1074 /* long option without single letter alternative */
1076 match_regstr = optarg;
1079 usage(cmd_restore_usage);
1083 if (!list_roots && optind + 1 >= argc)
1084 usage(cmd_restore_usage);
1085 else if (list_roots && optind >= argc)
1086 usage(cmd_restore_usage);
1088 if ((ret = check_mounted(argv[optind])) < 0) {
1089 fprintf(stderr, "Could not check mount status: %s\n",
1093 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
1097 root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1104 if (fs_location != 0) {
1105 free_extent_buffer(root->node);
1106 root->node = read_tree_block(root, fs_location, root->leafsize, 0);
1108 fprintf(stderr, "Failed to read fs location\n");
1113 memset(path_name, 0, 4096);
1115 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1116 dir_name[sizeof dir_name - 1] = 0;
1118 /* Strip the trailing / on the dir name */
1119 len = strlen(dir_name);
1120 while (len && dir_name[--len] == '/') {
1121 dir_name[len] = '\0';
1124 if (root_objectid != 0) {
1125 struct btrfs_root *orig_root = root;
1127 key.objectid = root_objectid;
1128 key.type = BTRFS_ROOT_ITEM_KEY;
1129 key.offset = (u64)-1;
1130 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1132 fprintf(stderr, "Error reading root\n");
1142 ret = find_first_dir(root, &key.objectid);
1146 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1150 ret = regcomp(&match_reg, match_regstr, match_cflags);
1152 regerror(ret, &match_reg, reg_err, sizeof(reg_err));
1153 fprintf(stderr, "Regex compile failed: %s\n", reg_err);
1159 ret = search_dir(root, &key, dir_name, "", mreg);