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.
20 #include "kerncompat.h"
28 #include <sys/types.h>
29 #include <lzo/lzoconf.h>
30 #include <lzo/lzo1x.h>
34 #include <sys/types.h>
35 #include <sys/xattr.h>
39 #include "print-tree.h"
40 #include "transaction.h"
46 static char fs_name[4096];
47 static char path_name[4096];
48 static int get_snaps = 0;
49 static int verbose = 0;
50 static int ignore_errors = 0;
51 static int overwrite = 0;
52 static int get_xattrs = 0;
53 static int dry_run = 0;
56 #define PAGE_CACHE_SIZE 4096
57 #define lzo1x_worst_compress(x) ((x) + ((x) / 16) + 64 + 3)
59 static int decompress_zlib(char *inbuf, char *outbuf, u64 compress_len,
65 memset(&strm, 0, sizeof(strm));
66 ret = inflateInit(&strm);
68 fprintf(stderr, "inflate init returnd %d\n", ret);
72 strm.avail_in = compress_len;
73 strm.next_in = (unsigned char *)inbuf;
74 strm.avail_out = decompress_len;
75 strm.next_out = (unsigned char *)outbuf;
76 ret = inflate(&strm, Z_NO_FLUSH);
77 if (ret != Z_STREAM_END) {
78 (void)inflateEnd(&strm);
79 fprintf(stderr, "failed to inflate: %d\n", ret);
83 (void)inflateEnd(&strm);
86 static inline size_t read_compress_length(unsigned char *buf)
89 memcpy(&dlen, buf, LZO_LEN);
90 return le32_to_cpu(dlen);
93 static int decompress_lzo(unsigned char *inbuf, char *outbuf, u64 compress_len,
104 if (ret != LZO_E_OK) {
105 fprintf(stderr, "lzo init returned %d\n", ret);
109 tot_len = read_compress_length(inbuf);
113 while (tot_in < tot_len) {
116 in_len = read_compress_length(inbuf);
118 if ((tot_in + LZO_LEN + in_len) > tot_len) {
119 fprintf(stderr, "bad compress length %lu\n",
120 (unsigned long)in_len);
127 new_len = lzo1x_worst_compress(PAGE_CACHE_SIZE);
128 ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len,
129 (unsigned char *)outbuf,
130 (void *)&new_len, NULL);
131 if (ret != LZO_E_OK) {
132 fprintf(stderr, "failed to inflate: %d\n", ret);
141 * If the 4 byte header does not fit to the rest of the page we
142 * have to move to the next one, unless we read some garbage
144 mod_page = tot_in % PAGE_CACHE_SIZE;
145 rem_page = PAGE_CACHE_SIZE - mod_page;
146 if (rem_page < LZO_LEN) {
152 *decompress_len = out_len;
157 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
158 u64 *decompress_len, int compress)
161 case BTRFS_COMPRESS_ZLIB:
162 return decompress_zlib(inbuf, outbuf, compress_len,
164 case BTRFS_COMPRESS_LZO:
165 return decompress_lzo((unsigned char *)inbuf, outbuf, compress_len,
171 fprintf(stderr, "invalid compression type: %d\n", compress);
175 static int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
180 struct extent_buffer *c;
181 struct extent_buffer *next = NULL;
184 for (; level < BTRFS_MAX_LEVEL; level++) {
185 if (path->nodes[level])
189 if (level >= BTRFS_MAX_LEVEL)
192 slot = path->slots[level] + 1;
194 while(level < BTRFS_MAX_LEVEL) {
195 if (!path->nodes[level])
198 slot = path->slots[level] + offset;
199 c = path->nodes[level];
200 if (slot >= btrfs_header_nritems(c)) {
202 if (level == BTRFS_MAX_LEVEL)
209 reada_for_search(root, path, level, slot, 0);
211 next = read_node_slot(root, c, slot);
212 if (extent_buffer_uptodate(next))
216 path->slots[level] = slot;
219 c = path->nodes[level];
220 free_extent_buffer(c);
221 path->nodes[level] = next;
222 path->slots[level] = 0;
226 reada_for_search(root, path, level, 0, 0);
227 next = read_node_slot(root, next, 0);
228 if (!extent_buffer_uptodate(next))
234 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
236 struct extent_buffer *leaf = path->nodes[0];
237 struct btrfs_file_extent_item *fi;
248 fi = btrfs_item_ptr(leaf, path->slots[0],
249 struct btrfs_file_extent_item);
250 ptr = btrfs_file_extent_inline_start(fi);
251 len = btrfs_file_extent_inline_len(leaf, path->slots[0], fi);
252 inline_item_len = btrfs_file_extent_inline_item_len(leaf, btrfs_item_nr(path->slots[0]));
253 read_extent_buffer(leaf, buf, ptr, inline_item_len);
255 compress = btrfs_file_extent_compression(leaf, fi);
256 if (compress == BTRFS_COMPRESS_NONE) {
257 done = pwrite(fd, buf, len, pos);
259 fprintf(stderr, "Short inline write, wanted %d, did "
260 "%zd: %d\n", len, done, errno);
266 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
267 outbuf = calloc(1, ram_size);
269 fprintf(stderr, "No memory\n");
273 ret = decompress(buf, outbuf, len, &ram_size, compress);
279 done = pwrite(fd, outbuf, ram_size, pos);
281 if (done < ram_size) {
282 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
283 "did %zd: %d\n", ram_size, done, errno);
290 static int copy_one_extent(struct btrfs_root *root, int fd,
291 struct extent_buffer *leaf,
292 struct btrfs_file_extent_item *fi, u64 pos)
294 struct btrfs_multi_bio *multi = NULL;
295 struct btrfs_device *device;
296 char *inbuf, *outbuf = NULL;
297 ssize_t done, total = 0;
313 compress = btrfs_file_extent_compression(leaf, fi);
314 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
315 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
316 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
317 offset = btrfs_file_extent_offset(leaf, fi);
318 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
319 size_left = disk_size;
320 if (compress == BTRFS_COMPRESS_NONE)
323 if (verbose && offset)
324 printf("offset is %Lu\n", offset);
325 /* we found a hole */
329 inbuf = malloc(size_left);
331 fprintf(stderr, "No memory\n");
335 if (compress != BTRFS_COMPRESS_NONE) {
336 outbuf = calloc(1, ram_size);
338 fprintf(stderr, "No memory\n");
345 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
346 bytenr, &length, &multi, mirror_num, NULL);
348 fprintf(stderr, "Error mapping block %d\n", ret);
351 device = multi->stripes[0].dev;
354 dev_bytenr = multi->stripes[0].physical;
357 if (size_left < length)
360 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
361 /* Need both checks, or we miss negative values due to u64 conversion */
362 if (done < 0 || done < length) {
363 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
366 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
367 if (mirror_num > num_copies) {
369 fprintf(stderr, "Exhausted mirrors trying to read\n");
372 fprintf(stderr, "Trying another mirror\n");
383 if (compress == BTRFS_COMPRESS_NONE) {
384 while (total < num_bytes) {
385 done = pwrite(fd, inbuf+total, num_bytes-total,
389 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
398 ret = decompress(inbuf, outbuf, disk_size, &ram_size, compress);
400 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
403 if (mirror_num >= num_copies) {
407 fprintf(stderr, "Trying another mirror\n");
411 while (total < num_bytes) {
412 done = pwrite(fd, outbuf + offset + total,
433 static enum loop_response ask_to_continue(const char *file)
438 printf("We seem to be looping a lot on %s, do you want to keep going "
439 "on ? (y/N/a): ", file);
441 ret = fgets(buf, 2, stdin);
442 if (*ret == '\n' || tolower(*ret) == 'n')
444 if (tolower(*ret) == 'a')
446 if (tolower(*ret) != 'y') {
447 printf("Please enter one of 'y', 'n', or 'a': ");
451 return LOOP_CONTINUE;
455 static int set_file_xattrs(struct btrfs_root *root, u64 inode,
456 int fd, const char *file_name)
458 struct btrfs_key key;
459 struct btrfs_path *path;
460 struct extent_buffer *leaf;
461 struct btrfs_dir_item *di;
470 key.objectid = inode;
471 key.type = BTRFS_XATTR_ITEM_KEY;
474 path = btrfs_alloc_path();
478 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
482 leaf = path->nodes[0];
484 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
486 ret = next_leaf(root, path);
489 "Error searching for extended attributes: %d\n",
493 /* No more leaves to search */
497 leaf = path->nodes[0];
502 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
503 if (key.type != BTRFS_XATTR_ITEM_KEY || key.objectid != inode)
506 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
507 di = btrfs_item_ptr(leaf, path->slots[0],
508 struct btrfs_dir_item);
510 while (cur < total_len) {
511 len = btrfs_dir_name_len(leaf, di);
512 if (len > name_len) {
514 name = (char *) malloc(len + 1);
520 read_extent_buffer(leaf, name,
521 (unsigned long)(di + 1), len);
525 len = btrfs_dir_data_len(leaf, di);
526 if (len > data_len) {
528 data = (char *) malloc(len);
534 read_extent_buffer(leaf, data,
535 (unsigned long)(di + 1) + name_len,
539 if (fsetxattr(fd, name, data, data_len, 0)) {
543 "Error setting extended attribute %s on file %s: %s\n",
544 name, file_name, strerror(err));
547 len = sizeof(*di) + name_len + data_len;
549 di = (struct btrfs_dir_item *)((char *)di + len);
555 btrfs_free_path(path);
563 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
566 struct extent_buffer *leaf;
567 struct btrfs_path *path;
568 struct btrfs_file_extent_item *fi;
569 struct btrfs_inode_item *inode_item;
570 struct btrfs_key found_key;
577 path = btrfs_alloc_path();
579 fprintf(stderr, "Ran out of memory\n");
583 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
585 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
586 struct btrfs_inode_item);
587 found_size = btrfs_inode_size(path->nodes[0], inode_item);
589 btrfs_release_path(path);
592 key->type = BTRFS_EXTENT_DATA_KEY;
594 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
596 fprintf(stderr, "Error searching %d\n", ret);
597 btrfs_free_path(path);
601 leaf = path->nodes[0];
603 ret = next_leaf(root, path);
605 fprintf(stderr, "Error getting next leaf %d\n",
607 btrfs_free_path(path);
609 } else if (ret > 0) {
610 /* No more leaves to search */
611 btrfs_free_path(path);
614 leaf = path->nodes[0];
618 if (loops >= 0 && loops++ >= 1024) {
619 enum loop_response resp;
621 resp = ask_to_continue(file);
622 if (resp == LOOP_STOP)
624 else if (resp == LOOP_CONTINUE)
626 else if (resp == LOOP_DONTASK)
629 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
631 ret = next_leaf(root, path);
633 fprintf(stderr, "Error searching %d\n", ret);
634 btrfs_free_path(path);
637 /* No more leaves to search */
638 btrfs_free_path(path);
641 leaf = path->nodes[0];
645 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
646 if (found_key.objectid != key->objectid)
648 if (found_key.type != key->type)
650 fi = btrfs_item_ptr(leaf, path->slots[0],
651 struct btrfs_file_extent_item);
652 extent_type = btrfs_file_extent_type(leaf, fi);
653 compression = btrfs_file_extent_compression(leaf, fi);
654 if (compression >= BTRFS_COMPRESS_LAST) {
655 fprintf(stderr, "Don't support compression yet %d\n",
657 btrfs_free_path(path);
661 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
663 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
664 ret = copy_one_inline(fd, path, found_key.offset);
666 btrfs_free_path(path);
669 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
670 ret = copy_one_extent(root, fd, leaf, fi,
673 btrfs_free_path(path);
677 printf("Weird extent type %d\n", extent_type);
683 btrfs_free_path(path);
686 ret = ftruncate(fd, (loff_t)found_size);
691 ret = set_file_xattrs(root, key->objectid, fd, file);
698 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
699 const char *output_rootdir, const char *in_dir,
702 struct btrfs_path *path;
703 struct extent_buffer *leaf;
704 struct btrfs_dir_item *dir_item;
705 struct btrfs_key found_key, location;
706 char filename[BTRFS_NAME_LEN + 1];
707 unsigned long name_ptr;
714 path = btrfs_alloc_path();
716 fprintf(stderr, "Ran out of memory\n");
721 key->type = BTRFS_DIR_INDEX_KEY;
723 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
725 fprintf(stderr, "Error searching %d\n", ret);
726 btrfs_free_path(path);
730 leaf = path->nodes[0];
733 printf("No leaf after search, looking for the next "
735 ret = next_leaf(root, path);
737 fprintf(stderr, "Error getting next leaf %d\n",
739 btrfs_free_path(path);
741 } else if (ret > 0) {
742 /* No more leaves to search */
744 printf("Reached the end of the tree looking "
745 "for the directory\n");
746 btrfs_free_path(path);
749 leaf = path->nodes[0];
753 if (loops++ >= 1024) {
754 printf("We have looped trying to restore files in %s "
755 "too many times to be making progress, "
756 "stopping\n", in_dir);
760 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
762 ret = next_leaf(root, path);
764 fprintf(stderr, "Error searching %d\n",
766 btrfs_free_path(path);
768 } else if (ret > 0) {
769 /* No more leaves to search */
771 printf("Reached the end of "
772 "the tree searching the"
774 btrfs_free_path(path);
777 leaf = path->nodes[0];
781 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
782 if (found_key.objectid != key->objectid) {
784 printf("Found objectid=%Lu, key=%Lu\n",
785 found_key.objectid, key->objectid);
788 if (found_key.type != key->type) {
790 printf("Found type=%u, want=%u\n",
791 found_key.type, key->type);
794 dir_item = btrfs_item_ptr(leaf, path->slots[0],
795 struct btrfs_dir_item);
796 name_ptr = (unsigned long)(dir_item + 1);
797 name_len = btrfs_dir_name_len(leaf, dir_item);
798 read_extent_buffer(leaf, filename, name_ptr, name_len);
799 filename[name_len] = '\0';
800 type = btrfs_dir_type(leaf, dir_item);
801 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
803 /* full path from root of btrfs being restored */
804 snprintf(fs_name, 4096, "%s/%s", in_dir, filename);
806 if (mreg && REG_NOMATCH == regexec(mreg, fs_name, 0, NULL, 0))
809 /* full path from system root */
810 snprintf(path_name, 4096, "%s%s", output_rootdir, fs_name);
813 * At this point we're only going to restore directories and
814 * files, no symlinks or anything else.
816 if (type == BTRFS_FT_REG_FILE) {
821 ret = stat(path_name, &st);
824 if (verbose || !warn)
825 printf("Skipping existing file"
829 printf("If you wish to overwrite use "
830 "the -o option to overwrite\n");
837 printf("Restoring %s\n", path_name);
840 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
842 fprintf(stderr, "Error creating %s: %d\n",
846 btrfs_free_path(path);
850 ret = copy_file(root, fd, &location, path_name);
853 fprintf(stderr, "Error copying data for %s\n",
857 btrfs_free_path(path);
860 } else if (type == BTRFS_FT_DIR) {
861 struct btrfs_root *search_root = root;
862 char *dir = strdup(fs_name);
865 fprintf(stderr, "Ran out of memory\n");
866 btrfs_free_path(path);
870 if (location.type == BTRFS_ROOT_ITEM_KEY) {
872 * If we are a snapshot and this is the index
873 * object to ourselves just skip it.
875 if (location.objectid ==
876 root->root_key.objectid) {
881 location.offset = (u64)-1;
882 search_root = btrfs_read_fs_root(root->fs_info,
884 if (IS_ERR(search_root)) {
886 fprintf(stderr, "Error reading "
887 "subvolume %s: %lu\n",
889 PTR_ERR(search_root));
892 btrfs_free_path(path);
893 return PTR_ERR(search_root);
897 * A subvolume will have a key.offset of 0, a
898 * snapshot will have key.offset of a transid.
900 if (search_root->root_key.offset != 0 &&
903 printf("Skipping snapshot %s\n",
907 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
911 printf("Restoring %s\n", path_name);
917 ret = mkdir(path_name, 0755);
918 if (ret && errno != EEXIST) {
920 fprintf(stderr, "Error mkdiring %s: %d\n",
924 btrfs_free_path(path);
928 ret = search_dir(search_root, &location,
929 output_rootdir, dir, mreg);
932 fprintf(stderr, "Error searching %s\n",
936 btrfs_free_path(path);
945 printf("Done searching %s\n", in_dir);
946 btrfs_free_path(path);
950 static int do_list_roots(struct btrfs_root *root)
952 struct btrfs_key key;
953 struct btrfs_key found_key;
954 struct btrfs_disk_key disk_key;
955 struct btrfs_path *path;
956 struct extent_buffer *leaf;
957 struct btrfs_root_item ri;
958 unsigned long offset;
962 root = root->fs_info->tree_root;
963 path = btrfs_alloc_path();
965 fprintf(stderr, "Failed to alloc path\n");
971 key.type = BTRFS_ROOT_ITEM_KEY;
973 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
975 fprintf(stderr, "Failed to do search %d\n", ret);
976 btrfs_free_path(path);
980 leaf = path->nodes[0];
983 slot = path->slots[0];
984 if (slot >= btrfs_header_nritems(leaf)) {
985 ret = btrfs_next_leaf(root, path);
988 leaf = path->nodes[0];
989 slot = path->slots[0];
991 btrfs_item_key(leaf, &disk_key, slot);
992 btrfs_disk_key_to_cpu(&found_key, &disk_key);
993 if (btrfs_key_type(&found_key) != BTRFS_ROOT_ITEM_KEY) {
998 offset = btrfs_item_ptr_offset(leaf, slot);
999 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
1001 btrfs_print_key(&disk_key);
1002 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
1003 btrfs_root_level(&ri));
1006 btrfs_free_path(path);
1011 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
1012 int super_mirror, int list_roots)
1014 struct btrfs_fs_info *fs_info = NULL;
1015 struct btrfs_root *root = NULL;
1019 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1020 bytenr = btrfs_sb_offset(i);
1021 fs_info = open_ctree_fs_info(dev, bytenr, root_location,
1022 OPEN_CTREE_PARTIAL);
1025 fprintf(stderr, "Could not open root, trying backup super\n");
1032 * All we really need to succeed is reading the chunk tree, everything
1033 * else we can do by hand, since we only need to read the tree root and
1036 if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
1039 root = fs_info->tree_root;
1041 root_location = btrfs_super_root(fs_info->super_copy);
1042 generation = btrfs_super_generation(fs_info->super_copy);
1043 root->node = read_tree_block(root, root_location,
1044 root->leafsize, generation);
1045 if (!extent_buffer_uptodate(root->node)) {
1046 fprintf(stderr, "Error opening tree root\n");
1052 if (!list_roots && !fs_info->fs_root) {
1053 struct btrfs_key key;
1055 key.objectid = BTRFS_FS_TREE_OBJECTID;
1056 key.type = BTRFS_ROOT_ITEM_KEY;
1057 key.offset = (u64)-1;
1058 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
1059 if (IS_ERR(fs_info->fs_root)) {
1060 fprintf(stderr, "Couldn't read fs root: %ld\n",
1061 PTR_ERR(fs_info->fs_root));
1062 close_ctree(fs_info->tree_root);
1067 if (list_roots && do_list_roots(fs_info->tree_root)) {
1068 close_ctree(fs_info->tree_root);
1072 return fs_info->fs_root;
1075 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
1077 struct btrfs_path *path;
1078 struct btrfs_key found_key;
1079 struct btrfs_key key;
1084 key.type = BTRFS_DIR_INDEX_KEY;
1087 path = btrfs_alloc_path();
1089 fprintf(stderr, "Ran out of memory\n");
1093 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1095 fprintf(stderr, "Error searching %d\n", ret);
1099 if (!path->nodes[0]) {
1100 fprintf(stderr, "No leaf!\n");
1104 for (i = path->slots[0];
1105 i < btrfs_header_nritems(path->nodes[0]); i++) {
1106 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
1107 if (found_key.type != key.type)
1110 printf("Using objectid %Lu for first dir\n",
1111 found_key.objectid);
1112 *objectid = found_key.objectid;
1117 ret = next_leaf(root, path);
1119 fprintf(stderr, "Error getting next leaf %d\n",
1122 } else if (ret > 0) {
1123 fprintf(stderr, "No more leaves\n");
1126 } while (!path->nodes[0]);
1129 printf("Couldn't find a dir index item\n");
1131 btrfs_free_path(path);
1135 const char * const cmd_restore_usage[] = {
1136 "btrfs restore [options] <device> <path> | -l <device>",
1137 "Try to restore files from a damaged filesystem (unmounted)",
1140 "-x get extended attributes",
1144 "-t <bytenr> tree location",
1145 "-f <bytenr> filesystem location",
1146 "-u <mirror> super mirror",
1147 "-r <rootid> root objectid",
1149 "-l list tree roots",
1150 "-D|--dry-run dry run (only list files that would be recovered)",
1151 "--path-regex <regex>",
1152 " restore only filenames matching regex,",
1153 " you have to use following syntax (possibly quoted):",
1154 " ^/(|home(|/username(|/Desktop(|/.*))))$",
1155 "-c ignore case (--path-regrex only)",
1159 int cmd_restore(int argc, char **argv)
1161 struct btrfs_root *root;
1162 struct btrfs_key key;
1164 u64 tree_location = 0;
1165 u64 fs_location = 0;
1166 u64 root_objectid = 0;
1169 int super_mirror = 0;
1172 const char *match_regstr = NULL;
1173 int match_cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
1174 regex_t match_reg, *mreg = NULL;
1179 int option_index = 0;
1180 static const struct option long_options[] = {
1181 { "path-regex", 1, NULL, 256},
1182 { "dry-run", 0, NULL, 'D'},
1186 opt = getopt_long(argc, argv, "sxviot:u:df:r:lDc", long_options,
1205 tree_location = arg_strtou64(optarg);
1208 fs_location = arg_strtou64(optarg);
1211 super_mirror = arg_strtou64(optarg);
1212 if (super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1213 fprintf(stderr, "Super mirror not "
1222 root_objectid = arg_strtou64(optarg);
1223 if (!is_fstree(root_objectid)) {
1224 fprintf(stderr, "objectid %llu is not a valid fs/file tree\n",
1236 match_cflags |= REG_ICASE;
1238 /* long option without single letter alternative */
1240 match_regstr = optarg;
1246 usage(cmd_restore_usage);
1250 if (!list_roots && check_argc_min(argc - optind, 2))
1251 usage(cmd_restore_usage);
1252 else if (list_roots && check_argc_min(argc - optind, 1))
1253 usage(cmd_restore_usage);
1255 if (fs_location && root_objectid) {
1256 fprintf(stderr, "don't use -f and -r at the same time.\n");
1260 if ((ret = check_mounted(argv[optind])) < 0) {
1261 fprintf(stderr, "Could not check mount status: %s\n",
1265 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
1269 root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1276 if (fs_location != 0) {
1277 free_extent_buffer(root->node);
1278 root->node = read_tree_block(root, fs_location, root->leafsize, 0);
1279 if (!extent_buffer_uptodate(root->node)) {
1280 fprintf(stderr, "Failed to read fs location\n");
1286 memset(path_name, 0, 4096);
1288 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1289 dir_name[sizeof dir_name - 1] = 0;
1291 /* Strip the trailing / on the dir name */
1292 len = strlen(dir_name);
1293 while (len && dir_name[--len] == '/') {
1294 dir_name[len] = '\0';
1297 if (root_objectid != 0) {
1298 struct btrfs_root *orig_root = root;
1300 key.objectid = root_objectid;
1301 key.type = BTRFS_ROOT_ITEM_KEY;
1302 key.offset = (u64)-1;
1303 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1305 fprintf(stderr, "fail to read root %llu: %s\n",
1306 root_objectid, strerror(-PTR_ERR(root)));
1316 ret = find_first_dir(root, &key.objectid);
1320 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1324 ret = regcomp(&match_reg, match_regstr, match_cflags);
1326 regerror(ret, &match_reg, reg_err, sizeof(reg_err));
1327 fprintf(stderr, "Regex compile failed: %s\n", reg_err);
1334 printf("This is a dry-run, no files are going to be restored\n");
1336 ret = search_dir(root, &key, dir_name, "", mreg);