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"
41 static char path_name[4096];
42 static int get_snaps = 0;
43 static int verbose = 0;
44 static int ignore_errors = 0;
45 static int overwrite = 0;
47 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
53 memset(&strm, 0, sizeof(strm));
54 ret = inflateInit(&strm);
56 fprintf(stderr, "inflate init returnd %d\n", ret);
60 strm.avail_in = compress_len;
61 strm.next_in = (unsigned char *)inbuf;
62 strm.avail_out = decompress_len;
63 strm.next_out = (unsigned char *)outbuf;
64 ret = inflate(&strm, Z_NO_FLUSH);
65 if (ret != Z_STREAM_END) {
66 (void)inflateEnd(&strm);
67 fprintf(stderr, "ret is %d\n", ret);
71 (void)inflateEnd(&strm);
75 int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
79 struct extent_buffer *c;
80 struct extent_buffer *next = NULL;
82 for (; level < BTRFS_MAX_LEVEL; level++) {
83 if (path->nodes[level])
87 if (level == BTRFS_MAX_LEVEL)
90 slot = path->slots[level] + 1;
92 while(level < BTRFS_MAX_LEVEL) {
93 if (!path->nodes[level])
96 slot = path->slots[level] + 1;
97 c = path->nodes[level];
98 if (slot >= btrfs_header_nritems(c)) {
100 if (level == BTRFS_MAX_LEVEL)
106 reada_for_search(root, path, level, slot, 0);
108 next = read_node_slot(root, c, slot);
111 path->slots[level] = slot;
114 c = path->nodes[level];
115 free_extent_buffer(c);
116 path->nodes[level] = next;
117 path->slots[level] = 0;
121 reada_for_search(root, path, level, 0, 0);
122 next = read_node_slot(root, next, 0);
127 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
129 struct extent_buffer *leaf = path->nodes[0];
130 struct btrfs_file_extent_item *fi;
140 fi = btrfs_item_ptr(leaf, path->slots[0],
141 struct btrfs_file_extent_item);
142 ptr = btrfs_file_extent_inline_start(fi);
143 len = btrfs_file_extent_inline_item_len(leaf,
144 btrfs_item_nr(leaf, path->slots[0]));
145 read_extent_buffer(leaf, buf, ptr, len);
147 compress = btrfs_file_extent_compression(leaf, fi);
148 if (compress == BTRFS_COMPRESS_NONE) {
149 done = pwrite(fd, buf, len, pos);
151 fprintf(stderr, "Short inline write, wanted %d, did "
152 "%zd: %d\n", len, done, errno);
158 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
159 outbuf = malloc(ram_size);
161 fprintf(stderr, "No memory\n");
165 ret = decompress(buf, outbuf, len, ram_size);
171 done = pwrite(fd, outbuf, ram_size, pos);
173 if (done < ram_size) {
174 fprintf(stderr, "Short compressed inline write, wanted %d, "
175 "did %zd: %d\n", ram_size, done, errno);
182 static int copy_one_extent(struct btrfs_root *root, int fd,
183 struct extent_buffer *leaf,
184 struct btrfs_file_extent_item *fi, u64 pos)
186 struct btrfs_multi_bio *multi = NULL;
187 struct btrfs_device *device;
188 char *inbuf, *outbuf = NULL;
189 ssize_t done, total = 0;
201 compress = btrfs_file_extent_compression(leaf, fi);
202 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
203 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
204 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
205 size_left = disk_size;
207 /* we found a hole */
211 inbuf = malloc(disk_size);
213 fprintf(stderr, "No memory\n");
217 if (compress != BTRFS_COMPRESS_NONE) {
218 outbuf = malloc(ram_size);
220 fprintf(stderr, "No memory\n");
227 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
228 bytenr, &length, &multi, 0, NULL);
232 fprintf(stderr, "Error mapping block %d\n", ret);
235 device = multi->stripes[0].dev;
238 dev_bytenr = multi->stripes[0].physical;
241 if (size_left < length)
245 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
249 fprintf(stderr, "Short read %d\n", errno);
259 if (compress == BTRFS_COMPRESS_NONE) {
260 while (total < ram_size) {
261 done = pwrite(fd, inbuf+total, ram_size-total,
265 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
274 ret = decompress(inbuf, outbuf, disk_size, ram_size);
281 while (total < ram_size) {
282 done = pwrite(fd, outbuf+total, ram_size-total, pos+total);
285 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
295 static int ask_to_continue(const char *file)
300 printf("We seem to be looping a lot on %s, do you want to keep going "
301 "on ? (y/N): ", file);
303 ret = fgets(buf, 2, stdin);
304 if (*ret == '\n' || tolower(*ret) == 'n')
306 if (tolower(*ret) != 'y') {
307 printf("Please enter either 'y' or 'n': ");
315 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
318 struct extent_buffer *leaf;
319 struct btrfs_path *path;
320 struct btrfs_file_extent_item *fi;
321 struct btrfs_inode_item *inode_item;
322 struct btrfs_key found_key;
329 path = btrfs_alloc_path();
331 fprintf(stderr, "Ran out of memory\n");
334 path->skip_locking = 1;
336 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
338 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
339 struct btrfs_inode_item);
340 found_size = btrfs_inode_size(path->nodes[0], inode_item);
342 btrfs_release_path(root, path);
345 key->type = BTRFS_EXTENT_DATA_KEY;
347 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
349 fprintf(stderr, "Error searching %d\n", ret);
350 btrfs_free_path(path);
354 leaf = path->nodes[0];
356 ret = next_leaf(root, path);
358 fprintf(stderr, "Error getting next leaf %d\n",
360 btrfs_free_path(path);
362 } else if (ret > 0) {
363 /* No more leaves to search */
364 btrfs_free_path(path);
367 leaf = path->nodes[0];
371 if (loops++ >= 1024) {
372 ret = ask_to_continue(file);
377 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
379 ret = next_leaf(root, path);
381 fprintf(stderr, "Error searching %d\n", ret);
382 btrfs_free_path(path);
385 /* No more leaves to search */
386 btrfs_free_path(path);
389 leaf = path->nodes[0];
393 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
394 if (found_key.objectid != key->objectid)
396 if (found_key.type != key->type)
398 fi = btrfs_item_ptr(leaf, path->slots[0],
399 struct btrfs_file_extent_item);
400 extent_type = btrfs_file_extent_type(leaf, fi);
401 compression = btrfs_file_extent_compression(leaf, fi);
402 if (compression >= BTRFS_COMPRESS_LAST) {
403 fprintf(stderr, "Don't support compression yet %d\n",
405 btrfs_free_path(path);
409 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
411 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
412 ret = copy_one_inline(fd, path, found_key.offset);
414 btrfs_free_path(path);
417 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
418 ret = copy_one_extent(root, fd, leaf, fi,
421 btrfs_free_path(path);
425 printf("Weird extent type %d\n", extent_type);
431 btrfs_free_path(path);
434 ret = ftruncate(fd, (loff_t)found_size);
441 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
444 struct btrfs_path *path;
445 struct extent_buffer *leaf;
446 struct btrfs_dir_item *dir_item;
447 struct btrfs_key found_key, location;
448 char filename[BTRFS_NAME_LEN + 1];
449 unsigned long name_ptr;
456 path = btrfs_alloc_path();
458 fprintf(stderr, "Ran out of memory\n");
461 path->skip_locking = 1;
464 key->type = BTRFS_DIR_INDEX_KEY;
466 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
468 fprintf(stderr, "Error searching %d\n", ret);
469 btrfs_free_path(path);
473 leaf = path->nodes[0];
476 printf("No leaf after search, looking for the next "
478 ret = next_leaf(root, path);
480 fprintf(stderr, "Error getting next leaf %d\n",
482 btrfs_free_path(path);
484 } else if (ret > 0) {
485 /* No more leaves to search */
487 printf("Reached the end of the tree looking "
488 "for the directory\n");
489 btrfs_free_path(path);
492 leaf = path->nodes[0];
496 if (loops++ >= 1024) {
497 printf("We have looped trying to restore files in %s "
498 "too many times to be making progress, "
503 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
505 ret = next_leaf(root, path);
507 fprintf(stderr, "Error searching %d\n",
509 btrfs_free_path(path);
511 } else if (ret > 0) {
512 /* No more leaves to search */
514 printf("Reached the end of "
515 "the tree searching the"
517 btrfs_free_path(path);
520 leaf = path->nodes[0];
524 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
525 if (found_key.objectid != key->objectid) {
527 printf("Found objectid=%Lu, key=%Lu\n",
528 found_key.objectid, key->objectid);
531 if (found_key.type != key->type) {
533 printf("Found type=%u, want=%u\n",
534 found_key.type, key->type);
537 dir_item = btrfs_item_ptr(leaf, path->slots[0],
538 struct btrfs_dir_item);
539 name_ptr = (unsigned long)(dir_item + 1);
540 name_len = btrfs_dir_name_len(leaf, dir_item);
541 read_extent_buffer(leaf, filename, name_ptr, name_len);
542 filename[name_len] = '\0';
543 type = btrfs_dir_type(leaf, dir_item);
544 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
546 snprintf(path_name, 4096, "%s/%s", dir, filename);
550 * At this point we're only going to restore directories and
551 * files, no symlinks or anything else.
553 if (type == BTRFS_FT_REG_FILE) {
558 ret = stat(path_name, &st);
561 if (verbose || !warn)
562 printf("Skipping existing file"
566 printf("If you wish to overwrite use "
567 "the -o option to overwrite\n");
574 printf("Restoring %s\n", path_name);
575 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
577 fprintf(stderr, "Error creating %s: %d\n",
581 btrfs_free_path(path);
585 ret = copy_file(root, fd, &location, path_name);
590 btrfs_free_path(path);
593 } else if (type == BTRFS_FT_DIR) {
594 struct btrfs_root *search_root = root;
595 char *dir = strdup(path_name);
598 fprintf(stderr, "Ran out of memory\n");
599 btrfs_free_path(path);
603 if (location.type == BTRFS_ROOT_ITEM_KEY) {
605 * If we are a snapshot and this is the index
606 * object to ourselves just skip it.
608 if (location.objectid ==
609 root->root_key.objectid) {
614 search_root = btrfs_read_fs_root(root->fs_info,
616 if (IS_ERR(search_root)) {
618 fprintf(stderr, "Error reading "
619 "subvolume %s: %lu\n",
621 PTR_ERR(search_root));
624 btrfs_free_path(path);
625 return PTR_ERR(search_root);
629 * A subvolume will have a key.offset of 0, a
630 * snapshot will have key.offset of a transid.
632 if (search_root->root_key.offset != 0 &&
635 printf("Skipping snapshot %s\n",
639 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
643 printf("Restoring %s\n", path_name);
646 ret = mkdir(path_name, 0755);
647 if (ret && errno != EEXIST) {
649 fprintf(stderr, "Error mkdiring %s: %d\n",
653 btrfs_free_path(path);
657 ret = search_dir(search_root, &location, dir);
662 btrfs_free_path(path);
671 printf("Done searching %s\n", dir);
672 btrfs_free_path(path);
678 fprintf(stderr, "Usage: restore [-svio] [-t disk offset] <device> "
682 static struct btrfs_root *open_fs(const char *dev, u64 root_location, int super_mirror)
684 struct btrfs_root *root;
688 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
689 bytenr = btrfs_sb_offset(i);
690 root = open_ctree_recovery(dev, bytenr, root_location);
693 fprintf(stderr, "Could not open root, trying backup super\n");
699 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
701 struct btrfs_path *path;
702 struct btrfs_key found_key;
703 struct btrfs_key key;
708 key.type = BTRFS_DIR_INDEX_KEY;
711 path = btrfs_alloc_path();
713 fprintf(stderr, "Ran out of memory\n");
717 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
719 fprintf(stderr, "Error searching %d\n", ret);
723 if (!path->nodes[0]) {
724 fprintf(stderr, "No leaf!\n");
728 for (i = path->slots[0];
729 i < btrfs_header_nritems(path->nodes[0]); i++) {
730 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
731 if (found_key.type != key.type)
734 printf("Using objectid %Lu for first dir\n",
736 *objectid = found_key.objectid;
741 ret = next_leaf(root, path);
743 fprintf(stderr, "Error getting next leaf %d\n",
746 } else if (ret > 0) {
747 fprintf(stderr, "No more leaves\n");
750 } while (!path->nodes[0]);
753 printf("Couldn't find a dir index item\n");
755 btrfs_free_path(path);
759 int main(int argc, char **argv)
761 struct btrfs_root *root;
762 struct btrfs_key key;
764 u64 tree_location = 0;
769 int super_mirror = 0;
772 while ((opt = getopt(argc, argv, "sviot:u:df:")) != -1) {
788 tree_location = (u64)strtoll(optarg, NULL, 10);
790 fprintf(stderr, "Tree location not valid\n");
796 fs_location = (u64)strtoll(optarg, NULL, 10);
798 fprintf(stderr, "Fs location not valid\n");
804 super_mirror = (int)strtol(optarg, NULL, 10);
806 super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
807 fprintf(stderr, "Super mirror not "
821 if (optind + 1 >= argc) {
826 if ((ret = check_mounted(argv[optind])) < 0) {
827 fprintf(stderr, "Could not check mount status: %s\n",
831 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
835 root = open_fs(argv[optind], tree_location, super_mirror);
839 if (fs_location != 0) {
840 free_extent_buffer(root->node);
841 root->node = read_tree_block(root, fs_location, 4096, 0);
843 fprintf(stderr, "Failed to read fs location\n");
848 printf("Root objectid is %Lu\n", root->objectid);
850 memset(path_name, 0, 4096);
852 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
853 dir_name[sizeof dir_name - 1] = 0;
855 /* Strip the trailing / on the dir name */
856 len = strlen(dir_name);
857 while (len && dir_name[--len] == '/') {
858 dir_name[len] = '\0';
862 ret = find_first_dir(root, &key.objectid);
866 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
869 ret = search_dir(root->fs_info->fs_root, &key, dir_name);