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 path_name[4096];
43 static int get_snaps = 0;
44 static int verbose = 0;
45 static int ignore_errors = 0;
46 static int overwrite = 0;
48 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
54 memset(&strm, 0, sizeof(strm));
55 ret = inflateInit(&strm);
57 fprintf(stderr, "inflate init returnd %d\n", ret);
61 strm.avail_in = compress_len;
62 strm.next_in = (unsigned char *)inbuf;
63 strm.avail_out = decompress_len;
64 strm.next_out = (unsigned char *)outbuf;
65 ret = inflate(&strm, Z_NO_FLUSH);
66 if (ret != Z_STREAM_END) {
67 (void)inflateEnd(&strm);
68 fprintf(stderr, "failed to inflate: %d\n", ret);
72 (void)inflateEnd(&strm);
76 int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
80 struct extent_buffer *c;
81 struct extent_buffer *next = NULL;
83 for (; level < BTRFS_MAX_LEVEL; level++) {
84 if (path->nodes[level])
88 if (level == BTRFS_MAX_LEVEL)
91 slot = path->slots[level] + 1;
93 while(level < BTRFS_MAX_LEVEL) {
94 if (!path->nodes[level])
97 slot = path->slots[level] + 1;
98 c = path->nodes[level];
99 if (slot >= btrfs_header_nritems(c)) {
101 if (level == BTRFS_MAX_LEVEL)
107 reada_for_search(root, path, level, slot, 0);
109 next = read_node_slot(root, c, slot);
112 path->slots[level] = slot;
115 c = path->nodes[level];
116 free_extent_buffer(c);
117 path->nodes[level] = next;
118 path->slots[level] = 0;
122 reada_for_search(root, path, level, 0, 0);
123 next = read_node_slot(root, next, 0);
128 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
130 struct extent_buffer *leaf = path->nodes[0];
131 struct btrfs_file_extent_item *fi;
141 fi = btrfs_item_ptr(leaf, path->slots[0],
142 struct btrfs_file_extent_item);
143 ptr = btrfs_file_extent_inline_start(fi);
144 len = btrfs_file_extent_inline_item_len(leaf,
145 btrfs_item_nr(leaf, path->slots[0]));
146 read_extent_buffer(leaf, buf, ptr, len);
148 compress = btrfs_file_extent_compression(leaf, fi);
149 if (compress == BTRFS_COMPRESS_NONE) {
150 done = pwrite(fd, buf, len, pos);
152 fprintf(stderr, "Short inline write, wanted %d, did "
153 "%zd: %d\n", len, done, errno);
159 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
160 outbuf = malloc(ram_size);
162 fprintf(stderr, "No memory\n");
166 ret = decompress(buf, outbuf, len, ram_size);
172 done = pwrite(fd, outbuf, ram_size, pos);
174 if (done < ram_size) {
175 fprintf(stderr, "Short compressed inline write, wanted %d, "
176 "did %zd: %d\n", ram_size, done, errno);
183 static int copy_one_extent(struct btrfs_root *root, int fd,
184 struct extent_buffer *leaf,
185 struct btrfs_file_extent_item *fi, u64 pos)
187 struct btrfs_multi_bio *multi = NULL;
188 struct btrfs_device *device;
189 char *inbuf, *outbuf = NULL;
190 ssize_t done, total = 0;
204 compress = btrfs_file_extent_compression(leaf, fi);
205 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
206 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
207 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
208 size_left = disk_size;
210 /* we found a hole */
214 inbuf = malloc(disk_size);
216 fprintf(stderr, "No memory\n");
220 if (compress != BTRFS_COMPRESS_NONE) {
221 outbuf = malloc(ram_size);
223 fprintf(stderr, "No memory\n");
230 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
231 bytenr, &length, &multi, mirror_num, NULL);
233 fprintf(stderr, "Error mapping block %d\n", ret);
236 device = multi->stripes[0].dev;
239 dev_bytenr = multi->stripes[0].physical;
242 if (size_left < length)
245 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
246 /* Need both checks, or we miss negative values due to u64 conversion */
247 if (done < 0 || done < length) {
248 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
251 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
252 if (mirror_num > num_copies) {
254 fprintf(stderr, "Exhausted mirrors trying to read\n");
257 fprintf(stderr, "Trying another mirror\n");
268 if (compress == BTRFS_COMPRESS_NONE) {
269 while (total < ram_size) {
270 done = pwrite(fd, inbuf+total, ram_size-total,
274 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
283 ret = decompress(inbuf, outbuf, disk_size, ram_size);
285 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
288 if (mirror_num >= num_copies) {
292 fprintf(stderr, "Trying another mirror\n");
296 while (total < ram_size) {
297 done = pwrite(fd, outbuf+total, ram_size-total, pos+total);
310 static int ask_to_continue(const char *file)
315 printf("We seem to be looping a lot on %s, do you want to keep going "
316 "on ? (y/N): ", file);
318 ret = fgets(buf, 2, stdin);
319 if (*ret == '\n' || tolower(*ret) == 'n')
321 if (tolower(*ret) != 'y') {
322 printf("Please enter either 'y' or 'n': ");
330 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
333 struct extent_buffer *leaf;
334 struct btrfs_path *path;
335 struct btrfs_file_extent_item *fi;
336 struct btrfs_inode_item *inode_item;
337 struct btrfs_key found_key;
344 path = btrfs_alloc_path();
346 fprintf(stderr, "Ran out of memory\n");
349 path->skip_locking = 1;
351 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
353 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
354 struct btrfs_inode_item);
355 found_size = btrfs_inode_size(path->nodes[0], inode_item);
357 btrfs_release_path(root, path);
360 key->type = BTRFS_EXTENT_DATA_KEY;
362 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
364 fprintf(stderr, "Error searching %d\n", ret);
365 btrfs_free_path(path);
369 leaf = path->nodes[0];
371 ret = next_leaf(root, path);
373 fprintf(stderr, "Error getting next leaf %d\n",
375 btrfs_free_path(path);
377 } else if (ret > 0) {
378 /* No more leaves to search */
379 btrfs_free_path(path);
382 leaf = path->nodes[0];
386 if (loops++ >= 1024) {
387 ret = ask_to_continue(file);
392 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
394 ret = next_leaf(root, path);
396 fprintf(stderr, "Error searching %d\n", ret);
397 btrfs_free_path(path);
400 /* No more leaves to search */
401 btrfs_free_path(path);
404 leaf = path->nodes[0];
408 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
409 if (found_key.objectid != key->objectid)
411 if (found_key.type != key->type)
413 fi = btrfs_item_ptr(leaf, path->slots[0],
414 struct btrfs_file_extent_item);
415 extent_type = btrfs_file_extent_type(leaf, fi);
416 compression = btrfs_file_extent_compression(leaf, fi);
417 if (compression >= BTRFS_COMPRESS_LAST) {
418 fprintf(stderr, "Don't support compression yet %d\n",
420 btrfs_free_path(path);
424 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
426 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
427 ret = copy_one_inline(fd, path, found_key.offset);
429 btrfs_free_path(path);
432 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
433 ret = copy_one_extent(root, fd, leaf, fi,
436 btrfs_free_path(path);
440 printf("Weird extent type %d\n", extent_type);
446 btrfs_free_path(path);
449 ret = ftruncate(fd, (loff_t)found_size);
456 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
459 struct btrfs_path *path;
460 struct extent_buffer *leaf;
461 struct btrfs_dir_item *dir_item;
462 struct btrfs_key found_key, location;
463 char filename[BTRFS_NAME_LEN + 1];
464 unsigned long name_ptr;
471 path = btrfs_alloc_path();
473 fprintf(stderr, "Ran out of memory\n");
476 path->skip_locking = 1;
479 key->type = BTRFS_DIR_INDEX_KEY;
481 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
483 fprintf(stderr, "Error searching %d\n", ret);
484 btrfs_free_path(path);
488 leaf = path->nodes[0];
491 printf("No leaf after search, looking for the next "
493 ret = next_leaf(root, path);
495 fprintf(stderr, "Error getting next leaf %d\n",
497 btrfs_free_path(path);
499 } else if (ret > 0) {
500 /* No more leaves to search */
502 printf("Reached the end of the tree looking "
503 "for the directory\n");
504 btrfs_free_path(path);
507 leaf = path->nodes[0];
511 if (loops++ >= 1024) {
512 printf("We have looped trying to restore files in %s "
513 "too many times to be making progress, "
518 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
520 ret = next_leaf(root, path);
522 fprintf(stderr, "Error searching %d\n",
524 btrfs_free_path(path);
526 } else if (ret > 0) {
527 /* No more leaves to search */
529 printf("Reached the end of "
530 "the tree searching the"
532 btrfs_free_path(path);
535 leaf = path->nodes[0];
539 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
540 if (found_key.objectid != key->objectid) {
542 printf("Found objectid=%Lu, key=%Lu\n",
543 found_key.objectid, key->objectid);
546 if (found_key.type != key->type) {
548 printf("Found type=%u, want=%u\n",
549 found_key.type, key->type);
552 dir_item = btrfs_item_ptr(leaf, path->slots[0],
553 struct btrfs_dir_item);
554 name_ptr = (unsigned long)(dir_item + 1);
555 name_len = btrfs_dir_name_len(leaf, dir_item);
556 read_extent_buffer(leaf, filename, name_ptr, name_len);
557 filename[name_len] = '\0';
558 type = btrfs_dir_type(leaf, dir_item);
559 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
561 snprintf(path_name, 4096, "%s/%s", dir, filename);
565 * At this point we're only going to restore directories and
566 * files, no symlinks or anything else.
568 if (type == BTRFS_FT_REG_FILE) {
573 ret = stat(path_name, &st);
576 if (verbose || !warn)
577 printf("Skipping existing file"
581 printf("If you wish to overwrite use "
582 "the -o option to overwrite\n");
589 printf("Restoring %s\n", path_name);
590 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
592 fprintf(stderr, "Error creating %s: %d\n",
596 btrfs_free_path(path);
600 ret = copy_file(root, fd, &location, path_name);
605 btrfs_free_path(path);
608 } else if (type == BTRFS_FT_DIR) {
609 struct btrfs_root *search_root = root;
610 char *dir = strdup(path_name);
613 fprintf(stderr, "Ran out of memory\n");
614 btrfs_free_path(path);
618 if (location.type == BTRFS_ROOT_ITEM_KEY) {
620 * If we are a snapshot and this is the index
621 * object to ourselves just skip it.
623 if (location.objectid ==
624 root->root_key.objectid) {
629 search_root = btrfs_read_fs_root(root->fs_info,
631 if (IS_ERR(search_root)) {
633 fprintf(stderr, "Error reading "
634 "subvolume %s: %lu\n",
636 PTR_ERR(search_root));
639 btrfs_free_path(path);
640 return PTR_ERR(search_root);
644 * A subvolume will have a key.offset of 0, a
645 * snapshot will have key.offset of a transid.
647 if (search_root->root_key.offset != 0 &&
650 printf("Skipping snapshot %s\n",
654 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
658 printf("Restoring %s\n", path_name);
661 ret = mkdir(path_name, 0755);
662 if (ret && errno != EEXIST) {
664 fprintf(stderr, "Error mkdiring %s: %d\n",
668 btrfs_free_path(path);
672 ret = search_dir(search_root, &location, dir);
677 btrfs_free_path(path);
686 printf("Done searching %s\n", dir);
687 btrfs_free_path(path);
691 static struct btrfs_root *open_fs(const char *dev, u64 root_location, int super_mirror)
693 struct btrfs_root *root;
697 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
698 bytenr = btrfs_sb_offset(i);
699 root = open_ctree_recovery(dev, bytenr, root_location);
702 fprintf(stderr, "Could not open root, trying backup super\n");
708 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
710 struct btrfs_path *path;
711 struct btrfs_key found_key;
712 struct btrfs_key key;
717 key.type = BTRFS_DIR_INDEX_KEY;
720 path = btrfs_alloc_path();
722 fprintf(stderr, "Ran out of memory\n");
726 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
728 fprintf(stderr, "Error searching %d\n", ret);
732 if (!path->nodes[0]) {
733 fprintf(stderr, "No leaf!\n");
737 for (i = path->slots[0];
738 i < btrfs_header_nritems(path->nodes[0]); i++) {
739 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
740 if (found_key.type != key.type)
743 printf("Using objectid %Lu for first dir\n",
745 *objectid = found_key.objectid;
750 ret = next_leaf(root, path);
752 fprintf(stderr, "Error getting next leaf %d\n",
755 } else if (ret > 0) {
756 fprintf(stderr, "No more leaves\n");
759 } while (!path->nodes[0]);
762 printf("Couldn't find a dir index item\n");
764 btrfs_free_path(path);
768 const char * const cmd_restore_usage[] = {
769 "btrfs restore [options] <device>",
770 "Try to restore files from a damaged filesystem (unmounted)",
777 "-f <offset> filesystem location",
778 "-u <block> super mirror",
783 int cmd_restore(int argc, char **argv)
785 struct btrfs_root *root;
786 struct btrfs_key key;
788 u64 tree_location = 0;
790 u64 root_objectid = 0;
794 int super_mirror = 0;
797 while ((opt = getopt(argc, argv, "sviot:u:df:r:")) != -1) {
813 tree_location = (u64)strtoll(optarg, NULL, 10);
815 fprintf(stderr, "Tree location not valid\n");
821 fs_location = (u64)strtoll(optarg, NULL, 10);
823 fprintf(stderr, "Fs location not valid\n");
829 super_mirror = (int)strtol(optarg, NULL, 10);
831 super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
832 fprintf(stderr, "Super mirror not "
842 root_objectid = (u64)strtoll(optarg, NULL, 10);
844 fprintf(stderr, "Root objectid not valid\n");
849 usage(cmd_restore_usage);
853 if (optind + 1 >= argc)
854 usage(cmd_restore_usage);
856 if ((ret = check_mounted(argv[optind])) < 0) {
857 fprintf(stderr, "Could not check mount status: %s\n",
861 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
865 root = open_fs(argv[optind], tree_location, super_mirror);
869 if (fs_location != 0) {
870 free_extent_buffer(root->node);
871 root->node = read_tree_block(root, fs_location, 4096, 0);
873 fprintf(stderr, "Failed to read fs location\n");
878 memset(path_name, 0, 4096);
880 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
881 dir_name[sizeof dir_name - 1] = 0;
883 /* Strip the trailing / on the dir name */
884 len = strlen(dir_name);
885 while (len && dir_name[--len] == '/') {
886 dir_name[len] = '\0';
889 if (root_objectid != 0) {
890 struct btrfs_root *orig_root = root;
892 key.objectid = root_objectid;
893 key.type = BTRFS_ROOT_ITEM_KEY;
894 key.offset = (u64)-1;
895 root = btrfs_read_fs_root(orig_root->fs_info, &key);
897 fprintf(stderr, "Error reading root\n");
907 ret = find_first_dir(root, &key.objectid);
911 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
914 ret = search_dir(root, &key, dir_name);