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"
34 #include "print-tree.h"
35 #include "transaction.h"
42 static char fs_name[4096];
43 static char path_name[4096];
44 static int get_snaps = 0;
45 static int verbose = 0;
46 static int ignore_errors = 0;
47 static int overwrite = 0;
49 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
55 memset(&strm, 0, sizeof(strm));
56 ret = inflateInit(&strm);
58 fprintf(stderr, "inflate init returnd %d\n", ret);
62 strm.avail_in = compress_len;
63 strm.next_in = (unsigned char *)inbuf;
64 strm.avail_out = decompress_len;
65 strm.next_out = (unsigned char *)outbuf;
66 ret = inflate(&strm, Z_NO_FLUSH);
67 if (ret != Z_STREAM_END) {
68 (void)inflateEnd(&strm);
69 fprintf(stderr, "failed to inflate: %d\n", ret);
73 (void)inflateEnd(&strm);
77 int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
81 struct extent_buffer *c;
82 struct extent_buffer *next = NULL;
84 for (; level < BTRFS_MAX_LEVEL; level++) {
85 if (path->nodes[level])
89 if (level == BTRFS_MAX_LEVEL)
92 slot = path->slots[level] + 1;
94 while(level < BTRFS_MAX_LEVEL) {
95 if (!path->nodes[level])
98 slot = path->slots[level] + 1;
99 c = path->nodes[level];
100 if (slot >= btrfs_header_nritems(c)) {
102 if (level == BTRFS_MAX_LEVEL)
108 reada_for_search(root, path, level, slot, 0);
110 next = read_node_slot(root, c, slot);
113 path->slots[level] = slot;
116 c = path->nodes[level];
117 free_extent_buffer(c);
118 path->nodes[level] = next;
119 path->slots[level] = 0;
123 reada_for_search(root, path, level, 0, 0);
124 next = read_node_slot(root, next, 0);
129 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
131 struct extent_buffer *leaf = path->nodes[0];
132 struct btrfs_file_extent_item *fi;
142 fi = btrfs_item_ptr(leaf, path->slots[0],
143 struct btrfs_file_extent_item);
144 ptr = btrfs_file_extent_inline_start(fi);
145 len = btrfs_file_extent_inline_item_len(leaf,
146 btrfs_item_nr(leaf, path->slots[0]));
147 read_extent_buffer(leaf, buf, ptr, len);
149 compress = btrfs_file_extent_compression(leaf, fi);
150 if (compress == BTRFS_COMPRESS_NONE) {
151 done = pwrite(fd, buf, len, pos);
153 fprintf(stderr, "Short inline write, wanted %d, did "
154 "%zd: %d\n", len, done, errno);
160 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
161 outbuf = malloc(ram_size);
163 fprintf(stderr, "No memory\n");
167 ret = decompress(buf, outbuf, len, ram_size);
173 done = pwrite(fd, outbuf, ram_size, pos);
175 if (done < ram_size) {
176 fprintf(stderr, "Short compressed inline write, wanted %d, "
177 "did %zd: %d\n", ram_size, done, errno);
184 static int copy_one_extent(struct btrfs_root *root, int fd,
185 struct extent_buffer *leaf,
186 struct btrfs_file_extent_item *fi, u64 pos)
188 struct btrfs_multi_bio *multi = NULL;
189 struct btrfs_device *device;
190 char *inbuf, *outbuf = NULL;
191 ssize_t done, total = 0;
205 compress = btrfs_file_extent_compression(leaf, fi);
206 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
207 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
208 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
209 size_left = disk_size;
211 /* we found a hole */
215 inbuf = malloc(disk_size);
217 fprintf(stderr, "No memory\n");
221 if (compress != BTRFS_COMPRESS_NONE) {
222 outbuf = malloc(ram_size);
224 fprintf(stderr, "No memory\n");
231 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
232 bytenr, &length, &multi, mirror_num, NULL);
234 fprintf(stderr, "Error mapping block %d\n", ret);
237 device = multi->stripes[0].dev;
240 dev_bytenr = multi->stripes[0].physical;
243 if (size_left < length)
246 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
247 /* Need both checks, or we miss negative values due to u64 conversion */
248 if (done < 0 || done < length) {
249 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
252 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
253 if (mirror_num > num_copies) {
255 fprintf(stderr, "Exhausted mirrors trying to read\n");
258 fprintf(stderr, "Trying another mirror\n");
269 if (compress == BTRFS_COMPRESS_NONE) {
270 while (total < ram_size) {
271 done = pwrite(fd, inbuf+total, ram_size-total,
275 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
284 ret = decompress(inbuf, outbuf, disk_size, ram_size);
286 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
289 if (mirror_num >= num_copies) {
293 fprintf(stderr, "Trying another mirror\n");
297 while (total < ram_size) {
298 done = pwrite(fd, outbuf+total, ram_size-total, pos+total);
311 static int ask_to_continue(const char *file)
316 printf("We seem to be looping a lot on %s, do you want to keep going "
317 "on ? (y/N): ", file);
319 ret = fgets(buf, 2, stdin);
320 if (*ret == '\n' || tolower(*ret) == 'n')
322 if (tolower(*ret) != 'y') {
323 printf("Please enter either 'y' or 'n': ");
331 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
334 struct extent_buffer *leaf;
335 struct btrfs_path *path;
336 struct btrfs_file_extent_item *fi;
337 struct btrfs_inode_item *inode_item;
338 struct btrfs_key found_key;
345 path = btrfs_alloc_path();
347 fprintf(stderr, "Ran out of memory\n");
350 path->skip_locking = 1;
352 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
354 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
355 struct btrfs_inode_item);
356 found_size = btrfs_inode_size(path->nodes[0], inode_item);
358 btrfs_release_path(root, path);
361 key->type = BTRFS_EXTENT_DATA_KEY;
363 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
365 fprintf(stderr, "Error searching %d\n", ret);
366 btrfs_free_path(path);
370 leaf = path->nodes[0];
372 ret = next_leaf(root, path);
374 fprintf(stderr, "Error getting next leaf %d\n",
376 btrfs_free_path(path);
378 } else if (ret > 0) {
379 /* No more leaves to search */
380 btrfs_free_path(path);
383 leaf = path->nodes[0];
387 if (loops++ >= 1024) {
388 ret = ask_to_continue(file);
393 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
395 ret = next_leaf(root, path);
397 fprintf(stderr, "Error searching %d\n", ret);
398 btrfs_free_path(path);
401 /* No more leaves to search */
402 btrfs_free_path(path);
405 leaf = path->nodes[0];
409 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
410 if (found_key.objectid != key->objectid)
412 if (found_key.type != key->type)
414 fi = btrfs_item_ptr(leaf, path->slots[0],
415 struct btrfs_file_extent_item);
416 extent_type = btrfs_file_extent_type(leaf, fi);
417 compression = btrfs_file_extent_compression(leaf, fi);
418 if (compression >= BTRFS_COMPRESS_LAST) {
419 fprintf(stderr, "Don't support compression yet %d\n",
421 btrfs_free_path(path);
425 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
427 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
428 ret = copy_one_inline(fd, path, found_key.offset);
430 btrfs_free_path(path);
433 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
434 ret = copy_one_extent(root, fd, leaf, fi,
437 btrfs_free_path(path);
441 printf("Weird extent type %d\n", extent_type);
447 btrfs_free_path(path);
450 ret = ftruncate(fd, (loff_t)found_size);
457 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
458 const char *output_rootdir, const char *dir)
460 struct btrfs_path *path;
461 struct extent_buffer *leaf;
462 struct btrfs_dir_item *dir_item;
463 struct btrfs_key found_key, location;
464 char filename[BTRFS_NAME_LEN + 1];
465 unsigned long name_ptr;
472 path = btrfs_alloc_path();
474 fprintf(stderr, "Ran out of memory\n");
477 path->skip_locking = 1;
480 key->type = BTRFS_DIR_INDEX_KEY;
482 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
484 fprintf(stderr, "Error searching %d\n", ret);
485 btrfs_free_path(path);
489 leaf = path->nodes[0];
492 printf("No leaf after search, looking for the next "
494 ret = next_leaf(root, path);
496 fprintf(stderr, "Error getting next leaf %d\n",
498 btrfs_free_path(path);
500 } else if (ret > 0) {
501 /* No more leaves to search */
503 printf("Reached the end of the tree looking "
504 "for the directory\n");
505 btrfs_free_path(path);
508 leaf = path->nodes[0];
512 if (loops++ >= 1024) {
513 printf("We have looped trying to restore files in %s "
514 "too many times to be making progress, "
519 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
521 ret = next_leaf(root, path);
523 fprintf(stderr, "Error searching %d\n",
525 btrfs_free_path(path);
527 } else if (ret > 0) {
528 /* No more leaves to search */
530 printf("Reached the end of "
531 "the tree searching the"
533 btrfs_free_path(path);
536 leaf = path->nodes[0];
540 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
541 if (found_key.objectid != key->objectid) {
543 printf("Found objectid=%Lu, key=%Lu\n",
544 found_key.objectid, key->objectid);
547 if (found_key.type != key->type) {
549 printf("Found type=%u, want=%u\n",
550 found_key.type, key->type);
553 dir_item = btrfs_item_ptr(leaf, path->slots[0],
554 struct btrfs_dir_item);
555 name_ptr = (unsigned long)(dir_item + 1);
556 name_len = btrfs_dir_name_len(leaf, dir_item);
557 read_extent_buffer(leaf, filename, name_ptr, name_len);
558 filename[name_len] = '\0';
559 type = btrfs_dir_type(leaf, dir_item);
560 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
562 /* full path from root of btrfs being restored */
563 snprintf(fs_name, 4096, "%s/%s", dir, filename);
565 /* full path from system root */
566 snprintf(path_name, 4096, "%s%s", output_rootdir, fs_name);
569 * At this point we're only going to restore directories and
570 * files, no symlinks or anything else.
572 if (type == BTRFS_FT_REG_FILE) {
577 ret = stat(path_name, &st);
580 if (verbose || !warn)
581 printf("Skipping existing file"
585 printf("If you wish to overwrite use "
586 "the -o option to overwrite\n");
593 printf("Restoring %s\n", path_name);
594 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
596 fprintf(stderr, "Error creating %s: %d\n",
600 btrfs_free_path(path);
604 ret = copy_file(root, fd, &location, path_name);
609 btrfs_free_path(path);
612 } else if (type == BTRFS_FT_DIR) {
613 struct btrfs_root *search_root = root;
614 char *dir = strdup(fs_name);
617 fprintf(stderr, "Ran out of memory\n");
618 btrfs_free_path(path);
622 if (location.type == BTRFS_ROOT_ITEM_KEY) {
624 * If we are a snapshot and this is the index
625 * object to ourselves just skip it.
627 if (location.objectid ==
628 root->root_key.objectid) {
633 search_root = btrfs_read_fs_root(root->fs_info,
635 if (IS_ERR(search_root)) {
637 fprintf(stderr, "Error reading "
638 "subvolume %s: %lu\n",
640 PTR_ERR(search_root));
643 btrfs_free_path(path);
644 return PTR_ERR(search_root);
648 * A subvolume will have a key.offset of 0, a
649 * snapshot will have key.offset of a transid.
651 if (search_root->root_key.offset != 0 &&
654 printf("Skipping snapshot %s\n",
658 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
662 printf("Restoring %s\n", path_name);
665 ret = mkdir(path_name, 0755);
666 if (ret && errno != EEXIST) {
668 fprintf(stderr, "Error mkdiring %s: %d\n",
672 btrfs_free_path(path);
676 ret = search_dir(search_root, &location,
677 output_rootdir, dir);
682 btrfs_free_path(path);
691 printf("Done searching %s\n", dir);
692 btrfs_free_path(path);
696 static struct btrfs_root *open_fs(const char *dev, u64 root_location, int super_mirror)
698 struct btrfs_root *root;
702 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
703 bytenr = btrfs_sb_offset(i);
704 root = open_ctree_recovery(dev, bytenr, root_location);
707 fprintf(stderr, "Could not open root, trying backup super\n");
713 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
715 struct btrfs_path *path;
716 struct btrfs_key found_key;
717 struct btrfs_key key;
722 key.type = BTRFS_DIR_INDEX_KEY;
725 path = btrfs_alloc_path();
727 fprintf(stderr, "Ran out of memory\n");
731 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
733 fprintf(stderr, "Error searching %d\n", ret);
737 if (!path->nodes[0]) {
738 fprintf(stderr, "No leaf!\n");
742 for (i = path->slots[0];
743 i < btrfs_header_nritems(path->nodes[0]); i++) {
744 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
745 if (found_key.type != key.type)
748 printf("Using objectid %Lu for first dir\n",
750 *objectid = found_key.objectid;
755 ret = next_leaf(root, path);
757 fprintf(stderr, "Error getting next leaf %d\n",
760 } else if (ret > 0) {
761 fprintf(stderr, "No more leaves\n");
764 } while (!path->nodes[0]);
767 printf("Couldn't find a dir index item\n");
769 btrfs_free_path(path);
773 const char * const cmd_restore_usage[] = {
774 "btrfs restore [options] <device>",
775 "Try to restore files from a damaged filesystem (unmounted)",
782 "-f <offset> filesystem location",
783 "-u <block> super mirror",
788 int cmd_restore(int argc, char **argv)
790 struct btrfs_root *root;
791 struct btrfs_key key;
793 u64 tree_location = 0;
795 u64 root_objectid = 0;
799 int super_mirror = 0;
802 while ((opt = getopt(argc, argv, "sviot:u:df:r:")) != -1) {
818 tree_location = (u64)strtoll(optarg, NULL, 10);
820 fprintf(stderr, "Tree location not valid\n");
826 fs_location = (u64)strtoll(optarg, NULL, 10);
828 fprintf(stderr, "Fs location not valid\n");
834 super_mirror = (int)strtol(optarg, NULL, 10);
836 super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
837 fprintf(stderr, "Super mirror not "
847 root_objectid = (u64)strtoll(optarg, NULL, 10);
849 fprintf(stderr, "Root objectid not valid\n");
854 usage(cmd_restore_usage);
858 if (optind + 1 >= argc)
859 usage(cmd_restore_usage);
861 if ((ret = check_mounted(argv[optind])) < 0) {
862 fprintf(stderr, "Could not check mount status: %s\n",
866 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
870 root = open_fs(argv[optind], tree_location, super_mirror);
874 if (fs_location != 0) {
875 free_extent_buffer(root->node);
876 root->node = read_tree_block(root, fs_location, 4096, 0);
878 fprintf(stderr, "Failed to read fs location\n");
883 memset(path_name, 0, 4096);
885 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
886 dir_name[sizeof dir_name - 1] = 0;
888 /* Strip the trailing / on the dir name */
889 len = strlen(dir_name);
890 while (len && dir_name[--len] == '/') {
891 dir_name[len] = '\0';
894 if (root_objectid != 0) {
895 struct btrfs_root *orig_root = root;
897 key.objectid = root_objectid;
898 key.type = BTRFS_ROOT_ITEM_KEY;
899 key.offset = (u64)-1;
900 root = btrfs_read_fs_root(orig_root->fs_info, &key);
902 fprintf(stderr, "Error reading root\n");
912 ret = find_first_dir(root, &key.objectid);
916 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
919 ret = search_dir(root, &key, dir_name, "");