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, &new_len, NULL);
117 if (ret != LZO_E_OK) {
118 fprintf(stderr, "failed to inflate: %d\n", ret);
127 *decompress_len = out_len;
132 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
133 u64 *decompress_len, int compress)
136 case BTRFS_COMPRESS_ZLIB:
137 return decompress_zlib(inbuf, outbuf, compress_len,
139 case BTRFS_COMPRESS_LZO:
140 return decompress_lzo((unsigned char *)inbuf, outbuf, compress_len,
146 fprintf(stderr, "invalid compression type: %d\n", compress);
150 int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
154 struct extent_buffer *c;
155 struct extent_buffer *next = NULL;
157 for (; level < BTRFS_MAX_LEVEL; level++) {
158 if (path->nodes[level])
162 if (level == BTRFS_MAX_LEVEL)
165 slot = path->slots[level] + 1;
167 while(level < BTRFS_MAX_LEVEL) {
168 if (!path->nodes[level])
171 slot = path->slots[level] + 1;
172 c = path->nodes[level];
173 if (slot >= btrfs_header_nritems(c)) {
175 if (level == BTRFS_MAX_LEVEL)
181 reada_for_search(root, path, level, slot, 0);
183 next = read_node_slot(root, c, slot);
186 path->slots[level] = slot;
189 c = path->nodes[level];
190 free_extent_buffer(c);
191 path->nodes[level] = next;
192 path->slots[level] = 0;
196 reada_for_search(root, path, level, 0, 0);
197 next = read_node_slot(root, next, 0);
202 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
204 struct extent_buffer *leaf = path->nodes[0];
205 struct btrfs_file_extent_item *fi;
215 fi = btrfs_item_ptr(leaf, path->slots[0],
216 struct btrfs_file_extent_item);
217 ptr = btrfs_file_extent_inline_start(fi);
218 len = btrfs_file_extent_inline_item_len(leaf,
219 btrfs_item_nr(leaf, path->slots[0]));
220 read_extent_buffer(leaf, buf, ptr, len);
222 compress = btrfs_file_extent_compression(leaf, fi);
223 if (compress == BTRFS_COMPRESS_NONE) {
224 done = pwrite(fd, buf, len, pos);
226 fprintf(stderr, "Short inline write, wanted %d, did "
227 "%zd: %d\n", len, done, errno);
233 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
234 outbuf = malloc(ram_size);
236 fprintf(stderr, "No memory\n");
240 ret = decompress(buf, outbuf, len, &ram_size, compress);
246 done = pwrite(fd, outbuf, ram_size, pos);
248 if (done < ram_size) {
249 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
250 "did %zd: %d\n", ram_size, done, errno);
257 static int copy_one_extent(struct btrfs_root *root, int fd,
258 struct extent_buffer *leaf,
259 struct btrfs_file_extent_item *fi, u64 pos)
261 struct btrfs_multi_bio *multi = NULL;
262 struct btrfs_device *device;
263 char *inbuf, *outbuf = NULL;
264 ssize_t done, total = 0;
279 compress = btrfs_file_extent_compression(leaf, fi);
280 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
281 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
282 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
283 offset = btrfs_file_extent_offset(leaf, fi);
284 size_left = disk_size;
287 printf("offset is %Lu\n", offset);
288 /* we found a hole */
292 inbuf = malloc(disk_size);
294 fprintf(stderr, "No memory\n");
298 if (compress != BTRFS_COMPRESS_NONE) {
299 outbuf = malloc(ram_size);
301 fprintf(stderr, "No memory\n");
308 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
309 bytenr, &length, &multi, mirror_num, NULL);
311 fprintf(stderr, "Error mapping block %d\n", ret);
314 device = multi->stripes[0].dev;
317 dev_bytenr = multi->stripes[0].physical;
320 if (size_left < length)
323 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
324 /* Need both checks, or we miss negative values due to u64 conversion */
325 if (done < 0 || done < length) {
326 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
329 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
330 if (mirror_num > num_copies) {
332 fprintf(stderr, "Exhausted mirrors trying to read\n");
335 fprintf(stderr, "Trying another mirror\n");
346 if (compress == BTRFS_COMPRESS_NONE) {
347 while (total < ram_size) {
348 done = pwrite(fd, inbuf+total, ram_size-total,
352 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
361 ret = decompress(inbuf, outbuf, disk_size, &ram_size, compress);
363 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
366 if (mirror_num >= num_copies) {
370 fprintf(stderr, "Trying another mirror\n");
374 while (total < ram_size) {
375 done = pwrite(fd, outbuf+total, ram_size-total, pos+total);
388 static int ask_to_continue(const char *file)
393 printf("We seem to be looping a lot on %s, do you want to keep going "
394 "on ? (y/N): ", file);
396 ret = fgets(buf, 2, stdin);
397 if (*ret == '\n' || tolower(*ret) == 'n')
399 if (tolower(*ret) != 'y') {
400 printf("Please enter either 'y' or 'n': ");
408 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
411 struct extent_buffer *leaf;
412 struct btrfs_path *path;
413 struct btrfs_file_extent_item *fi;
414 struct btrfs_inode_item *inode_item;
415 struct btrfs_key found_key;
422 path = btrfs_alloc_path();
424 fprintf(stderr, "Ran out of memory\n");
427 path->skip_locking = 1;
429 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
431 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
432 struct btrfs_inode_item);
433 found_size = btrfs_inode_size(path->nodes[0], inode_item);
435 btrfs_release_path(root, path);
438 key->type = BTRFS_EXTENT_DATA_KEY;
440 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
442 fprintf(stderr, "Error searching %d\n", ret);
443 btrfs_free_path(path);
447 leaf = path->nodes[0];
449 ret = next_leaf(root, path);
451 fprintf(stderr, "Error getting next leaf %d\n",
453 btrfs_free_path(path);
455 } else if (ret > 0) {
456 /* No more leaves to search */
457 btrfs_free_path(path);
460 leaf = path->nodes[0];
464 if (loops++ >= 1024) {
465 ret = ask_to_continue(file);
470 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
472 ret = next_leaf(root, path);
474 fprintf(stderr, "Error searching %d\n", ret);
475 btrfs_free_path(path);
478 /* No more leaves to search */
479 btrfs_free_path(path);
482 leaf = path->nodes[0];
486 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
487 if (found_key.objectid != key->objectid)
489 if (found_key.type != key->type)
491 fi = btrfs_item_ptr(leaf, path->slots[0],
492 struct btrfs_file_extent_item);
493 extent_type = btrfs_file_extent_type(leaf, fi);
494 compression = btrfs_file_extent_compression(leaf, fi);
495 if (compression >= BTRFS_COMPRESS_LAST) {
496 fprintf(stderr, "Don't support compression yet %d\n",
498 btrfs_free_path(path);
502 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
504 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
505 ret = copy_one_inline(fd, path, found_key.offset);
507 btrfs_free_path(path);
510 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
511 ret = copy_one_extent(root, fd, leaf, fi,
514 btrfs_free_path(path);
518 printf("Weird extent type %d\n", extent_type);
524 btrfs_free_path(path);
527 ret = ftruncate(fd, (loff_t)found_size);
534 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
535 const char *output_rootdir, const char *dir)
537 struct btrfs_path *path;
538 struct extent_buffer *leaf;
539 struct btrfs_dir_item *dir_item;
540 struct btrfs_key found_key, location;
541 char filename[BTRFS_NAME_LEN + 1];
542 unsigned long name_ptr;
549 path = btrfs_alloc_path();
551 fprintf(stderr, "Ran out of memory\n");
554 path->skip_locking = 1;
557 key->type = BTRFS_DIR_INDEX_KEY;
559 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
561 fprintf(stderr, "Error searching %d\n", ret);
562 btrfs_free_path(path);
566 leaf = path->nodes[0];
569 printf("No leaf after search, looking for the next "
571 ret = next_leaf(root, path);
573 fprintf(stderr, "Error getting next leaf %d\n",
575 btrfs_free_path(path);
577 } else if (ret > 0) {
578 /* No more leaves to search */
580 printf("Reached the end of the tree looking "
581 "for the directory\n");
582 btrfs_free_path(path);
585 leaf = path->nodes[0];
589 if (loops++ >= 1024) {
590 printf("We have looped trying to restore files in %s "
591 "too many times to be making progress, "
596 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
598 ret = next_leaf(root, path);
600 fprintf(stderr, "Error searching %d\n",
602 btrfs_free_path(path);
604 } else if (ret > 0) {
605 /* No more leaves to search */
607 printf("Reached the end of "
608 "the tree searching the"
610 btrfs_free_path(path);
613 leaf = path->nodes[0];
617 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
618 if (found_key.objectid != key->objectid) {
620 printf("Found objectid=%Lu, key=%Lu\n",
621 found_key.objectid, key->objectid);
624 if (found_key.type != key->type) {
626 printf("Found type=%u, want=%u\n",
627 found_key.type, key->type);
630 dir_item = btrfs_item_ptr(leaf, path->slots[0],
631 struct btrfs_dir_item);
632 name_ptr = (unsigned long)(dir_item + 1);
633 name_len = btrfs_dir_name_len(leaf, dir_item);
634 read_extent_buffer(leaf, filename, name_ptr, name_len);
635 filename[name_len] = '\0';
636 type = btrfs_dir_type(leaf, dir_item);
637 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
639 /* full path from root of btrfs being restored */
640 snprintf(fs_name, 4096, "%s/%s", dir, filename);
642 /* full path from system root */
643 snprintf(path_name, 4096, "%s%s", output_rootdir, fs_name);
646 * At this point we're only going to restore directories and
647 * files, no symlinks or anything else.
649 if (type == BTRFS_FT_REG_FILE) {
654 ret = stat(path_name, &st);
657 if (verbose || !warn)
658 printf("Skipping existing file"
662 printf("If you wish to overwrite use "
663 "the -o option to overwrite\n");
670 printf("Restoring %s\n", path_name);
671 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
673 fprintf(stderr, "Error creating %s: %d\n",
677 btrfs_free_path(path);
681 ret = copy_file(root, fd, &location, path_name);
686 btrfs_free_path(path);
689 } else if (type == BTRFS_FT_DIR) {
690 struct btrfs_root *search_root = root;
691 char *dir = strdup(fs_name);
694 fprintf(stderr, "Ran out of memory\n");
695 btrfs_free_path(path);
699 if (location.type == BTRFS_ROOT_ITEM_KEY) {
701 * If we are a snapshot and this is the index
702 * object to ourselves just skip it.
704 if (location.objectid ==
705 root->root_key.objectid) {
710 search_root = btrfs_read_fs_root(root->fs_info,
712 if (IS_ERR(search_root)) {
714 fprintf(stderr, "Error reading "
715 "subvolume %s: %lu\n",
717 PTR_ERR(search_root));
720 btrfs_free_path(path);
721 return PTR_ERR(search_root);
725 * A subvolume will have a key.offset of 0, a
726 * snapshot will have key.offset of a transid.
728 if (search_root->root_key.offset != 0 &&
731 printf("Skipping snapshot %s\n",
735 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
739 printf("Restoring %s\n", path_name);
742 ret = mkdir(path_name, 0755);
743 if (ret && errno != EEXIST) {
745 fprintf(stderr, "Error mkdiring %s: %d\n",
749 btrfs_free_path(path);
753 ret = search_dir(search_root, &location,
754 output_rootdir, dir);
759 btrfs_free_path(path);
768 printf("Done searching %s\n", dir);
769 btrfs_free_path(path);
773 static struct btrfs_root *open_fs(const char *dev, u64 root_location, int super_mirror)
775 struct btrfs_root *root;
779 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
780 bytenr = btrfs_sb_offset(i);
781 root = open_ctree_recovery(dev, bytenr, root_location);
784 fprintf(stderr, "Could not open root, trying backup super\n");
790 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
792 struct btrfs_path *path;
793 struct btrfs_key found_key;
794 struct btrfs_key key;
799 key.type = BTRFS_DIR_INDEX_KEY;
802 path = btrfs_alloc_path();
804 fprintf(stderr, "Ran out of memory\n");
808 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
810 fprintf(stderr, "Error searching %d\n", ret);
814 if (!path->nodes[0]) {
815 fprintf(stderr, "No leaf!\n");
819 for (i = path->slots[0];
820 i < btrfs_header_nritems(path->nodes[0]); i++) {
821 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
822 if (found_key.type != key.type)
825 printf("Using objectid %Lu for first dir\n",
827 *objectid = found_key.objectid;
832 ret = next_leaf(root, path);
834 fprintf(stderr, "Error getting next leaf %d\n",
837 } else if (ret > 0) {
838 fprintf(stderr, "No more leaves\n");
841 } while (!path->nodes[0]);
844 printf("Couldn't find a dir index item\n");
846 btrfs_free_path(path);
850 const char * const cmd_restore_usage[] = {
851 "btrfs restore [options] <device>",
852 "Try to restore files from a damaged filesystem (unmounted)",
859 "-f <offset> filesystem location",
860 "-u <block> super mirror",
865 int cmd_restore(int argc, char **argv)
867 struct btrfs_root *root;
868 struct btrfs_key key;
870 u64 tree_location = 0;
872 u64 root_objectid = 0;
876 int super_mirror = 0;
879 while ((opt = getopt(argc, argv, "sviot:u:df:r:")) != -1) {
895 tree_location = (u64)strtoll(optarg, NULL, 10);
897 fprintf(stderr, "Tree location not valid\n");
903 fs_location = (u64)strtoll(optarg, NULL, 10);
905 fprintf(stderr, "Fs location not valid\n");
911 super_mirror = (int)strtol(optarg, NULL, 10);
913 super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
914 fprintf(stderr, "Super mirror not "
924 root_objectid = (u64)strtoll(optarg, NULL, 10);
926 fprintf(stderr, "Root objectid not valid\n");
931 usage(cmd_restore_usage);
935 if (optind + 1 >= argc)
936 usage(cmd_restore_usage);
938 if ((ret = check_mounted(argv[optind])) < 0) {
939 fprintf(stderr, "Could not check mount status: %s\n",
943 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
947 root = open_fs(argv[optind], tree_location, super_mirror);
951 if (fs_location != 0) {
952 free_extent_buffer(root->node);
953 root->node = read_tree_block(root, fs_location, 4096, 0);
955 fprintf(stderr, "Failed to read fs location\n");
960 memset(path_name, 0, 4096);
962 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
963 dir_name[sizeof dir_name - 1] = 0;
965 /* Strip the trailing / on the dir name */
966 len = strlen(dir_name);
967 while (len && dir_name[--len] == '/') {
968 dir_name[len] = '\0';
971 if (root_objectid != 0) {
972 struct btrfs_root *orig_root = root;
974 key.objectid = root_objectid;
975 key.type = BTRFS_ROOT_ITEM_KEY;
976 key.offset = (u64)-1;
977 root = btrfs_read_fs_root(orig_root->fs_info, &key);
979 fprintf(stderr, "Error reading root\n");
989 ret = find_first_dir(root, &key.objectid);
993 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
996 ret = search_dir(root, &key, dir_name, "");