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 <lzo/lzoconf.h>
31 #include <lzo/lzo1x.h>
36 #include "print-tree.h"
37 #include "transaction.h"
44 static char fs_name[4096];
45 static char path_name[4096];
46 static int get_snaps = 0;
47 static int verbose = 0;
48 static int ignore_errors = 0;
49 static int overwrite = 0;
52 #define PAGE_CACHE_SIZE 4096
53 #define lzo1x_worst_compress(x) ((x) + ((x) / 16) + 64 + 3)
55 static int decompress_zlib(char *inbuf, char *outbuf, u64 compress_len,
61 memset(&strm, 0, sizeof(strm));
62 ret = inflateInit(&strm);
64 fprintf(stderr, "inflate init returnd %d\n", ret);
68 strm.avail_in = compress_len;
69 strm.next_in = (unsigned char *)inbuf;
70 strm.avail_out = decompress_len;
71 strm.next_out = (unsigned char *)outbuf;
72 ret = inflate(&strm, Z_NO_FLUSH);
73 if (ret != Z_STREAM_END) {
74 (void)inflateEnd(&strm);
75 fprintf(stderr, "failed to inflate: %d\n", ret);
79 (void)inflateEnd(&strm);
82 static inline size_t read_compress_length(unsigned char *buf)
85 memcpy(&dlen, buf, LZO_LEN);
86 return le32_to_cpu(dlen);
89 static int decompress_lzo(unsigned char *inbuf, char *outbuf, u64 compress_len,
100 if (ret != LZO_E_OK) {
101 fprintf(stderr, "lzo init returned %d\n", ret);
105 tot_len = read_compress_length(inbuf);
109 while (tot_in < tot_len) {
110 in_len = read_compress_length(inbuf);
114 new_len = lzo1x_worst_compress(PAGE_CACHE_SIZE);
115 ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len,
116 (unsigned char *)outbuf,
117 (void *)&new_len, NULL);
118 if (ret != LZO_E_OK) {
119 fprintf(stderr, "failed to inflate: %d\n", ret);
128 *decompress_len = out_len;
133 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
134 u64 *decompress_len, int compress)
137 case BTRFS_COMPRESS_ZLIB:
138 return decompress_zlib(inbuf, outbuf, compress_len,
140 case BTRFS_COMPRESS_LZO:
141 return decompress_lzo((unsigned char *)inbuf, outbuf, compress_len,
147 fprintf(stderr, "invalid compression type: %d\n", compress);
151 int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
155 struct extent_buffer *c;
156 struct extent_buffer *next = NULL;
158 for (; level < BTRFS_MAX_LEVEL; level++) {
159 if (path->nodes[level])
163 if (level == BTRFS_MAX_LEVEL)
166 slot = path->slots[level] + 1;
168 while(level < BTRFS_MAX_LEVEL) {
169 if (!path->nodes[level])
172 slot = path->slots[level] + 1;
173 c = path->nodes[level];
174 if (slot >= btrfs_header_nritems(c)) {
176 if (level == BTRFS_MAX_LEVEL)
182 reada_for_search(root, path, level, slot, 0);
184 next = read_node_slot(root, c, slot);
187 path->slots[level] = slot;
190 c = path->nodes[level];
191 free_extent_buffer(c);
192 path->nodes[level] = next;
193 path->slots[level] = 0;
197 reada_for_search(root, path, level, 0, 0);
198 next = read_node_slot(root, next, 0);
203 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
205 struct extent_buffer *leaf = path->nodes[0];
206 struct btrfs_file_extent_item *fi;
216 fi = btrfs_item_ptr(leaf, path->slots[0],
217 struct btrfs_file_extent_item);
218 ptr = btrfs_file_extent_inline_start(fi);
219 len = btrfs_file_extent_inline_item_len(leaf,
220 btrfs_item_nr(leaf, path->slots[0]));
221 read_extent_buffer(leaf, buf, ptr, len);
223 compress = btrfs_file_extent_compression(leaf, fi);
224 if (compress == BTRFS_COMPRESS_NONE) {
225 done = pwrite(fd, buf, len, pos);
227 fprintf(stderr, "Short inline write, wanted %d, did "
228 "%zd: %d\n", len, done, errno);
234 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
235 outbuf = malloc(ram_size);
237 fprintf(stderr, "No memory\n");
241 ret = decompress(buf, outbuf, len, &ram_size, compress);
247 done = pwrite(fd, outbuf, ram_size, pos);
249 if (done < ram_size) {
250 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
251 "did %zd: %d\n", ram_size, done, errno);
258 static int copy_one_extent(struct btrfs_root *root, int fd,
259 struct extent_buffer *leaf,
260 struct btrfs_file_extent_item *fi, u64 pos)
262 struct btrfs_multi_bio *multi = NULL;
263 struct btrfs_device *device;
264 char *inbuf, *outbuf = NULL;
265 ssize_t done, total = 0;
280 compress = btrfs_file_extent_compression(leaf, fi);
281 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
282 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
283 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
284 offset = btrfs_file_extent_offset(leaf, fi);
285 size_left = disk_size;
288 printf("offset is %Lu\n", offset);
289 /* we found a hole */
293 inbuf = malloc(disk_size);
295 fprintf(stderr, "No memory\n");
299 if (compress != BTRFS_COMPRESS_NONE) {
300 outbuf = malloc(ram_size);
302 fprintf(stderr, "No memory\n");
309 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
310 bytenr, &length, &multi, mirror_num, NULL);
312 fprintf(stderr, "Error mapping block %d\n", ret);
315 device = multi->stripes[0].dev;
318 dev_bytenr = multi->stripes[0].physical;
321 if (size_left < length)
324 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
325 /* Need both checks, or we miss negative values due to u64 conversion */
326 if (done < 0 || done < length) {
327 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
330 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
331 if (mirror_num > num_copies) {
333 fprintf(stderr, "Exhausted mirrors trying to read\n");
336 fprintf(stderr, "Trying another mirror\n");
347 if (compress == BTRFS_COMPRESS_NONE) {
348 while (total < ram_size) {
349 done = pwrite(fd, inbuf+total, ram_size-total,
353 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
362 ret = decompress(inbuf, outbuf, disk_size, &ram_size, compress);
364 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
367 if (mirror_num >= num_copies) {
371 fprintf(stderr, "Trying another mirror\n");
375 while (total < ram_size) {
376 done = pwrite(fd, outbuf+total, ram_size-total, pos+total);
389 static int ask_to_continue(const char *file)
394 printf("We seem to be looping a lot on %s, do you want to keep going "
395 "on ? (y/N): ", file);
397 ret = fgets(buf, 2, stdin);
398 if (*ret == '\n' || tolower(*ret) == 'n')
400 if (tolower(*ret) != 'y') {
401 printf("Please enter either 'y' or 'n': ");
409 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
412 struct extent_buffer *leaf;
413 struct btrfs_path *path;
414 struct btrfs_file_extent_item *fi;
415 struct btrfs_inode_item *inode_item;
416 struct btrfs_key found_key;
423 path = btrfs_alloc_path();
425 fprintf(stderr, "Ran out of memory\n");
428 path->skip_locking = 1;
430 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
432 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
433 struct btrfs_inode_item);
434 found_size = btrfs_inode_size(path->nodes[0], inode_item);
436 btrfs_release_path(root, path);
439 key->type = BTRFS_EXTENT_DATA_KEY;
441 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
443 fprintf(stderr, "Error searching %d\n", ret);
444 btrfs_free_path(path);
448 leaf = path->nodes[0];
450 ret = next_leaf(root, path);
452 fprintf(stderr, "Error getting next leaf %d\n",
454 btrfs_free_path(path);
456 } else if (ret > 0) {
457 /* No more leaves to search */
458 btrfs_free_path(path);
461 leaf = path->nodes[0];
465 if (loops++ >= 1024) {
466 ret = ask_to_continue(file);
471 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
473 ret = next_leaf(root, path);
475 fprintf(stderr, "Error searching %d\n", ret);
476 btrfs_free_path(path);
479 /* No more leaves to search */
480 btrfs_free_path(path);
483 leaf = path->nodes[0];
487 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
488 if (found_key.objectid != key->objectid)
490 if (found_key.type != key->type)
492 fi = btrfs_item_ptr(leaf, path->slots[0],
493 struct btrfs_file_extent_item);
494 extent_type = btrfs_file_extent_type(leaf, fi);
495 compression = btrfs_file_extent_compression(leaf, fi);
496 if (compression >= BTRFS_COMPRESS_LAST) {
497 fprintf(stderr, "Don't support compression yet %d\n",
499 btrfs_free_path(path);
503 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
505 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
506 ret = copy_one_inline(fd, path, found_key.offset);
508 btrfs_free_path(path);
511 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
512 ret = copy_one_extent(root, fd, leaf, fi,
515 btrfs_free_path(path);
519 printf("Weird extent type %d\n", extent_type);
525 btrfs_free_path(path);
528 ret = ftruncate(fd, (loff_t)found_size);
535 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
536 const char *output_rootdir, const char *dir)
538 struct btrfs_path *path;
539 struct extent_buffer *leaf;
540 struct btrfs_dir_item *dir_item;
541 struct btrfs_key found_key, location;
542 char filename[BTRFS_NAME_LEN + 1];
543 unsigned long name_ptr;
550 path = btrfs_alloc_path();
552 fprintf(stderr, "Ran out of memory\n");
555 path->skip_locking = 1;
558 key->type = BTRFS_DIR_INDEX_KEY;
560 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
562 fprintf(stderr, "Error searching %d\n", ret);
563 btrfs_free_path(path);
567 leaf = path->nodes[0];
570 printf("No leaf after search, looking for the next "
572 ret = next_leaf(root, path);
574 fprintf(stderr, "Error getting next leaf %d\n",
576 btrfs_free_path(path);
578 } else if (ret > 0) {
579 /* No more leaves to search */
581 printf("Reached the end of the tree looking "
582 "for the directory\n");
583 btrfs_free_path(path);
586 leaf = path->nodes[0];
590 if (loops++ >= 1024) {
591 printf("We have looped trying to restore files in %s "
592 "too many times to be making progress, "
597 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
599 ret = next_leaf(root, path);
601 fprintf(stderr, "Error searching %d\n",
603 btrfs_free_path(path);
605 } else if (ret > 0) {
606 /* No more leaves to search */
608 printf("Reached the end of "
609 "the tree searching the"
611 btrfs_free_path(path);
614 leaf = path->nodes[0];
618 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
619 if (found_key.objectid != key->objectid) {
621 printf("Found objectid=%Lu, key=%Lu\n",
622 found_key.objectid, key->objectid);
625 if (found_key.type != key->type) {
627 printf("Found type=%u, want=%u\n",
628 found_key.type, key->type);
631 dir_item = btrfs_item_ptr(leaf, path->slots[0],
632 struct btrfs_dir_item);
633 name_ptr = (unsigned long)(dir_item + 1);
634 name_len = btrfs_dir_name_len(leaf, dir_item);
635 read_extent_buffer(leaf, filename, name_ptr, name_len);
636 filename[name_len] = '\0';
637 type = btrfs_dir_type(leaf, dir_item);
638 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
640 /* full path from root of btrfs being restored */
641 snprintf(fs_name, 4096, "%s/%s", dir, filename);
643 /* full path from system root */
644 snprintf(path_name, 4096, "%s%s", output_rootdir, fs_name);
647 * At this point we're only going to restore directories and
648 * files, no symlinks or anything else.
650 if (type == BTRFS_FT_REG_FILE) {
655 ret = stat(path_name, &st);
658 if (verbose || !warn)
659 printf("Skipping existing file"
663 printf("If you wish to overwrite use "
664 "the -o option to overwrite\n");
671 printf("Restoring %s\n", path_name);
672 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
674 fprintf(stderr, "Error creating %s: %d\n",
678 btrfs_free_path(path);
682 ret = copy_file(root, fd, &location, path_name);
687 btrfs_free_path(path);
690 } else if (type == BTRFS_FT_DIR) {
691 struct btrfs_root *search_root = root;
692 char *dir = strdup(fs_name);
695 fprintf(stderr, "Ran out of memory\n");
696 btrfs_free_path(path);
700 if (location.type == BTRFS_ROOT_ITEM_KEY) {
702 * If we are a snapshot and this is the index
703 * object to ourselves just skip it.
705 if (location.objectid ==
706 root->root_key.objectid) {
711 location.offset = (u64)-1;
712 search_root = btrfs_read_fs_root(root->fs_info,
714 if (IS_ERR(search_root)) {
716 fprintf(stderr, "Error reading "
717 "subvolume %s: %lu\n",
719 PTR_ERR(search_root));
722 btrfs_free_path(path);
723 return PTR_ERR(search_root);
727 * A subvolume will have a key.offset of 0, a
728 * snapshot will have key.offset of a transid.
730 if (search_root->root_key.offset != 0 &&
733 printf("Skipping snapshot %s\n",
737 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
741 printf("Restoring %s\n", path_name);
744 ret = mkdir(path_name, 0755);
745 if (ret && errno != EEXIST) {
747 fprintf(stderr, "Error mkdiring %s: %d\n",
751 btrfs_free_path(path);
755 ret = search_dir(search_root, &location,
756 output_rootdir, dir);
761 btrfs_free_path(path);
770 printf("Done searching %s\n", dir);
771 btrfs_free_path(path);
775 static int do_list_roots(struct btrfs_root *root)
777 struct btrfs_key key;
778 struct btrfs_key found_key;
779 struct btrfs_disk_key disk_key;
780 struct btrfs_path *path;
781 struct extent_buffer *leaf;
782 struct btrfs_root_item ri;
783 unsigned long offset;
787 root = root->fs_info->tree_root;
788 path = btrfs_alloc_path();
790 fprintf(stderr, "Failed to alloc path\n");
796 key.type = BTRFS_ROOT_ITEM_KEY;
798 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
800 fprintf(stderr, "Failed to do search %d\n", ret);
801 btrfs_free_path(path);
806 leaf = path->nodes[0];
807 slot = path->slots[0];
808 if (slot >= btrfs_header_nritems(leaf)) {
809 ret = btrfs_next_leaf(root, path);
812 leaf = path->nodes[0];
813 slot = path->slots[0];
815 btrfs_item_key(leaf, &disk_key, slot);
816 btrfs_disk_key_to_cpu(&found_key, &disk_key);
817 if (btrfs_key_type(&found_key) != BTRFS_ROOT_ITEM_KEY) {
822 offset = btrfs_item_ptr_offset(leaf, slot);
823 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
825 btrfs_print_key(&disk_key);
826 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
827 btrfs_root_level(&ri));
830 btrfs_free_path(path);
835 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
836 int super_mirror, int list_roots)
838 struct btrfs_fs_info *fs_info = NULL;
839 struct btrfs_root *root = NULL;
843 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
844 bytenr = btrfs_sb_offset(i);
845 fs_info = open_ctree_fs_info(dev, bytenr, root_location, 0, 1);
848 fprintf(stderr, "Could not open root, trying backup super\n");
855 * All we really need to succeed is reading the chunk tree, everything
856 * else we can do by hand, since we only need to read the tree root and
859 if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
862 root = fs_info->tree_root;
864 root_location = btrfs_super_root(fs_info->super_copy);
865 generation = btrfs_super_generation(fs_info->super_copy);
866 root->node = read_tree_block(root, root_location,
867 root->leafsize, generation);
868 if (!extent_buffer_uptodate(root->node)) {
869 fprintf(stderr, "Error opening tree root\n");
875 if (!list_roots && !fs_info->fs_root) {
876 struct btrfs_key key;
878 key.objectid = BTRFS_FS_TREE_OBJECTID;
879 key.type = BTRFS_ROOT_ITEM_KEY;
880 key.offset = (u64)-1;
881 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
882 if (IS_ERR(fs_info->fs_root)) {
883 fprintf(stderr, "Couldn't read fs root: %ld\n",
884 PTR_ERR(fs_info->fs_root));
885 close_ctree(fs_info->tree_root);
890 if (list_roots && do_list_roots(fs_info->tree_root)) {
891 close_ctree(fs_info->tree_root);
895 return fs_info->fs_root;
898 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
900 struct btrfs_path *path;
901 struct btrfs_key found_key;
902 struct btrfs_key key;
907 key.type = BTRFS_DIR_INDEX_KEY;
910 path = btrfs_alloc_path();
912 fprintf(stderr, "Ran out of memory\n");
916 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
918 fprintf(stderr, "Error searching %d\n", ret);
922 if (!path->nodes[0]) {
923 fprintf(stderr, "No leaf!\n");
927 for (i = path->slots[0];
928 i < btrfs_header_nritems(path->nodes[0]); i++) {
929 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
930 if (found_key.type != key.type)
933 printf("Using objectid %Lu for first dir\n",
935 *objectid = found_key.objectid;
940 ret = next_leaf(root, path);
942 fprintf(stderr, "Error getting next leaf %d\n",
945 } else if (ret > 0) {
946 fprintf(stderr, "No more leaves\n");
949 } while (!path->nodes[0]);
952 printf("Couldn't find a dir index item\n");
954 btrfs_free_path(path);
958 const char * const cmd_restore_usage[] = {
959 "btrfs restore [options] <device>",
960 "Try to restore files from a damaged filesystem (unmounted)",
967 "-f <offset> filesystem location",
968 "-u <block> super mirror",
973 int cmd_restore(int argc, char **argv)
975 struct btrfs_root *root;
976 struct btrfs_key key;
978 u64 tree_location = 0;
980 u64 root_objectid = 0;
984 int super_mirror = 0;
988 while ((opt = getopt(argc, argv, "sviot:u:df:r:l")) != -1) {
1004 tree_location = (u64)strtoll(optarg, NULL, 10);
1006 fprintf(stderr, "Tree location not valid\n");
1012 fs_location = (u64)strtoll(optarg, NULL, 10);
1014 fprintf(stderr, "Fs location not valid\n");
1020 super_mirror = (int)strtol(optarg, NULL, 10);
1022 super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1023 fprintf(stderr, "Super mirror not "
1033 root_objectid = (u64)strtoll(optarg, NULL, 10);
1035 fprintf(stderr, "Root objectid not valid\n");
1043 usage(cmd_restore_usage);
1047 if (!list_roots && optind + 1 >= argc)
1048 usage(cmd_restore_usage);
1049 else if (list_roots && optind >= argc)
1050 usage(cmd_restore_usage);
1052 if ((ret = check_mounted(argv[optind])) < 0) {
1053 fprintf(stderr, "Could not check mount status: %s\n",
1057 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
1061 root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1068 if (fs_location != 0) {
1069 free_extent_buffer(root->node);
1070 root->node = read_tree_block(root, fs_location, 4096, 0);
1072 fprintf(stderr, "Failed to read fs location\n");
1077 memset(path_name, 0, 4096);
1079 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1080 dir_name[sizeof dir_name - 1] = 0;
1082 /* Strip the trailing / on the dir name */
1083 len = strlen(dir_name);
1084 while (len && dir_name[--len] == '/') {
1085 dir_name[len] = '\0';
1088 if (root_objectid != 0) {
1089 struct btrfs_root *orig_root = root;
1091 key.objectid = root_objectid;
1092 key.type = BTRFS_ROOT_ITEM_KEY;
1093 key.offset = (u64)-1;
1094 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1096 fprintf(stderr, "Error reading root\n");
1106 ret = find_first_dir(root, &key.objectid);
1110 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1113 ret = search_dir(root, &key, dir_name, "");