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 search_root = btrfs_read_fs_root(root->fs_info,
713 if (IS_ERR(search_root)) {
715 fprintf(stderr, "Error reading "
716 "subvolume %s: %lu\n",
718 PTR_ERR(search_root));
721 btrfs_free_path(path);
722 return PTR_ERR(search_root);
726 * A subvolume will have a key.offset of 0, a
727 * snapshot will have key.offset of a transid.
729 if (search_root->root_key.offset != 0 &&
732 printf("Skipping snapshot %s\n",
736 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
740 printf("Restoring %s\n", path_name);
743 ret = mkdir(path_name, 0755);
744 if (ret && errno != EEXIST) {
746 fprintf(stderr, "Error mkdiring %s: %d\n",
750 btrfs_free_path(path);
754 ret = search_dir(search_root, &location,
755 output_rootdir, dir);
760 btrfs_free_path(path);
769 printf("Done searching %s\n", dir);
770 btrfs_free_path(path);
774 static int do_list_roots(struct btrfs_root *root)
776 struct btrfs_key key;
777 struct btrfs_key found_key;
778 struct btrfs_disk_key disk_key;
779 struct btrfs_path *path;
780 struct extent_buffer *leaf;
781 struct btrfs_root_item ri;
782 unsigned long offset;
786 root = root->fs_info->tree_root;
787 path = btrfs_alloc_path();
789 fprintf(stderr, "Failed to alloc path\n");
795 key.type = BTRFS_ROOT_ITEM_KEY;
797 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
799 fprintf(stderr, "Failed to do search %d\n", ret);
800 btrfs_free_path(path);
805 leaf = path->nodes[0];
806 slot = path->slots[0];
807 if (slot >= btrfs_header_nritems(leaf)) {
808 ret = btrfs_next_leaf(root, path);
811 leaf = path->nodes[0];
812 slot = path->slots[0];
814 btrfs_item_key(leaf, &disk_key, slot);
815 btrfs_disk_key_to_cpu(&found_key, &disk_key);
816 if (btrfs_key_type(&found_key) != BTRFS_ROOT_ITEM_KEY) {
821 offset = btrfs_item_ptr_offset(leaf, slot);
822 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
824 btrfs_print_key(&disk_key);
825 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
826 btrfs_root_level(&ri));
829 btrfs_free_path(path);
834 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
835 int super_mirror, int list_roots)
837 struct btrfs_fs_info *fs_info = NULL;
838 struct btrfs_root *root = NULL;
842 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
843 bytenr = btrfs_sb_offset(i);
844 fs_info = open_ctree_fs_info(dev, bytenr, root_location, 0, 1);
847 fprintf(stderr, "Could not open root, trying backup super\n");
854 * All we really need to succeed is reading the chunk tree, everything
855 * else we can do by hand, since we only need to read the tree root and
858 if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
861 root = fs_info->tree_root;
863 root_location = btrfs_super_root(fs_info->super_copy);
864 generation = btrfs_super_generation(fs_info->super_copy);
865 root->node = read_tree_block(root, root_location,
866 root->leafsize, generation);
867 if (!extent_buffer_uptodate(root->node)) {
868 fprintf(stderr, "Error opening tree root\n");
874 if (!list_roots && !fs_info->fs_root) {
875 struct btrfs_key key;
877 key.objectid = BTRFS_FS_TREE_OBJECTID;
878 key.type = BTRFS_ROOT_ITEM_KEY;
879 key.offset = (u64)-1;
880 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
881 if (IS_ERR(fs_info->fs_root)) {
882 fprintf(stderr, "Couldn't read fs root: %ld\n",
883 PTR_ERR(fs_info->fs_root));
884 close_ctree(fs_info->tree_root);
889 if (list_roots && do_list_roots(fs_info->tree_root)) {
890 close_ctree(fs_info->tree_root);
894 return fs_info->fs_root;
897 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
899 struct btrfs_path *path;
900 struct btrfs_key found_key;
901 struct btrfs_key key;
906 key.type = BTRFS_DIR_INDEX_KEY;
909 path = btrfs_alloc_path();
911 fprintf(stderr, "Ran out of memory\n");
915 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
917 fprintf(stderr, "Error searching %d\n", ret);
921 if (!path->nodes[0]) {
922 fprintf(stderr, "No leaf!\n");
926 for (i = path->slots[0];
927 i < btrfs_header_nritems(path->nodes[0]); i++) {
928 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
929 if (found_key.type != key.type)
932 printf("Using objectid %Lu for first dir\n",
934 *objectid = found_key.objectid;
939 ret = next_leaf(root, path);
941 fprintf(stderr, "Error getting next leaf %d\n",
944 } else if (ret > 0) {
945 fprintf(stderr, "No more leaves\n");
948 } while (!path->nodes[0]);
951 printf("Couldn't find a dir index item\n");
953 btrfs_free_path(path);
957 const char * const cmd_restore_usage[] = {
958 "btrfs restore [options] <device>",
959 "Try to restore files from a damaged filesystem (unmounted)",
966 "-f <offset> filesystem location",
967 "-u <block> super mirror",
972 int cmd_restore(int argc, char **argv)
974 struct btrfs_root *root;
975 struct btrfs_key key;
977 u64 tree_location = 0;
979 u64 root_objectid = 0;
983 int super_mirror = 0;
987 while ((opt = getopt(argc, argv, "sviot:u:df:r:l")) != -1) {
1003 tree_location = (u64)strtoll(optarg, NULL, 10);
1005 fprintf(stderr, "Tree location not valid\n");
1011 fs_location = (u64)strtoll(optarg, NULL, 10);
1013 fprintf(stderr, "Fs location not valid\n");
1019 super_mirror = (int)strtol(optarg, NULL, 10);
1021 super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1022 fprintf(stderr, "Super mirror not "
1032 root_objectid = (u64)strtoll(optarg, NULL, 10);
1034 fprintf(stderr, "Root objectid not valid\n");
1042 usage(cmd_restore_usage);
1046 if (!list_roots && optind + 1 >= argc)
1047 usage(cmd_restore_usage);
1048 else if (list_roots && optind >= argc)
1049 usage(cmd_restore_usage);
1051 if ((ret = check_mounted(argv[optind])) < 0) {
1052 fprintf(stderr, "Could not check mount status: %s\n",
1056 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
1060 root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1067 if (fs_location != 0) {
1068 free_extent_buffer(root->node);
1069 root->node = read_tree_block(root, fs_location, 4096, 0);
1071 fprintf(stderr, "Failed to read fs location\n");
1076 memset(path_name, 0, 4096);
1078 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1079 dir_name[sizeof dir_name - 1] = 0;
1081 /* Strip the trailing / on the dir name */
1082 len = strlen(dir_name);
1083 while (len && dir_name[--len] == '/') {
1084 dir_name[len] = '\0';
1087 if (root_objectid != 0) {
1088 struct btrfs_root *orig_root = root;
1090 key.objectid = root_objectid;
1091 key.type = BTRFS_ROOT_ITEM_KEY;
1092 key.offset = (u64)-1;
1093 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1095 fprintf(stderr, "Error reading root\n");
1105 ret = find_first_dir(root, &key.objectid);
1109 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1112 ret = search_dir(root, &key, dir_name, "");