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 <sys/types.h>
31 #include <lzo/lzoconf.h>
32 #include <lzo/lzo1x.h>
36 #include <sys/types.h>
37 #include <sys/xattr.h>
41 #include "print-tree.h"
42 #include "transaction.h"
49 static char fs_name[4096];
50 static char path_name[4096];
51 static int get_snaps = 0;
52 static int verbose = 0;
53 static int ignore_errors = 0;
54 static int overwrite = 0;
55 static int get_xattrs = 0;
56 static int dry_run = 0;
59 #define PAGE_CACHE_SIZE 4096
60 #define lzo1x_worst_compress(x) ((x) + ((x) / 16) + 64 + 3)
62 static int decompress_zlib(char *inbuf, char *outbuf, u64 compress_len,
68 memset(&strm, 0, sizeof(strm));
69 ret = inflateInit(&strm);
71 fprintf(stderr, "inflate init returnd %d\n", ret);
75 strm.avail_in = compress_len;
76 strm.next_in = (unsigned char *)inbuf;
77 strm.avail_out = decompress_len;
78 strm.next_out = (unsigned char *)outbuf;
79 ret = inflate(&strm, Z_NO_FLUSH);
80 if (ret != Z_STREAM_END) {
81 (void)inflateEnd(&strm);
82 fprintf(stderr, "failed to inflate: %d\n", ret);
86 (void)inflateEnd(&strm);
89 static inline size_t read_compress_length(unsigned char *buf)
92 memcpy(&dlen, buf, LZO_LEN);
93 return le32_to_cpu(dlen);
96 static int decompress_lzo(unsigned char *inbuf, char *outbuf, u64 compress_len,
107 if (ret != LZO_E_OK) {
108 fprintf(stderr, "lzo init returned %d\n", ret);
112 tot_len = read_compress_length(inbuf);
116 while (tot_in < tot_len) {
117 in_len = read_compress_length(inbuf);
119 if ((tot_in + LZO_LEN + in_len) > tot_len) {
120 fprintf(stderr, "bad compress length %lu\n",
121 (unsigned long)in_len);
128 new_len = lzo1x_worst_compress(PAGE_CACHE_SIZE);
129 ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len,
130 (unsigned char *)outbuf,
131 (void *)&new_len, NULL);
132 if (ret != LZO_E_OK) {
133 fprintf(stderr, "failed to inflate: %d\n", ret);
142 *decompress_len = out_len;
147 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
148 u64 *decompress_len, int compress)
151 case BTRFS_COMPRESS_ZLIB:
152 return decompress_zlib(inbuf, outbuf, compress_len,
154 case BTRFS_COMPRESS_LZO:
155 return decompress_lzo((unsigned char *)inbuf, outbuf, compress_len,
161 fprintf(stderr, "invalid compression type: %d\n", compress);
165 static int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
170 struct extent_buffer *c;
171 struct extent_buffer *next = NULL;
174 for (; level < BTRFS_MAX_LEVEL; level++) {
175 if (path->nodes[level])
179 if (level == BTRFS_MAX_LEVEL)
182 slot = path->slots[level] + 1;
184 while(level < BTRFS_MAX_LEVEL) {
185 if (!path->nodes[level])
188 slot = path->slots[level] + offset;
189 c = path->nodes[level];
190 if (slot >= btrfs_header_nritems(c)) {
192 if (level == BTRFS_MAX_LEVEL)
198 reada_for_search(root, path, level, slot, 0);
200 next = read_node_slot(root, c, slot);
205 path->slots[level] = slot;
208 c = path->nodes[level];
209 free_extent_buffer(c);
210 path->nodes[level] = next;
211 path->slots[level] = 0;
215 reada_for_search(root, path, level, 0, 0);
216 next = read_node_slot(root, next, 0);
223 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
225 struct extent_buffer *leaf = path->nodes[0];
226 struct btrfs_file_extent_item *fi;
236 fi = btrfs_item_ptr(leaf, path->slots[0],
237 struct btrfs_file_extent_item);
238 ptr = btrfs_file_extent_inline_start(fi);
239 len = btrfs_file_extent_inline_len(leaf, path->slots[0], fi);
240 read_extent_buffer(leaf, buf, ptr, len);
242 compress = btrfs_file_extent_compression(leaf, fi);
243 if (compress == BTRFS_COMPRESS_NONE) {
244 done = pwrite(fd, buf, len, pos);
246 fprintf(stderr, "Short inline write, wanted %d, did "
247 "%zd: %d\n", len, done, errno);
253 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
254 outbuf = malloc(ram_size);
256 fprintf(stderr, "No memory\n");
260 ret = decompress(buf, outbuf, len, &ram_size, compress);
266 done = pwrite(fd, outbuf, ram_size, pos);
268 if (done < ram_size) {
269 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
270 "did %zd: %d\n", ram_size, done, errno);
277 static int copy_one_extent(struct btrfs_root *root, int fd,
278 struct extent_buffer *leaf,
279 struct btrfs_file_extent_item *fi, u64 pos)
281 struct btrfs_multi_bio *multi = NULL;
282 struct btrfs_device *device;
283 char *inbuf, *outbuf = NULL;
284 ssize_t done, total = 0;
300 compress = btrfs_file_extent_compression(leaf, fi);
301 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
302 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
303 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
304 offset = btrfs_file_extent_offset(leaf, fi);
305 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
306 size_left = disk_size;
307 if (compress == BTRFS_COMPRESS_NONE)
311 printf("offset is %Lu\n", offset);
312 /* we found a hole */
316 inbuf = malloc(size_left);
318 fprintf(stderr, "No memory\n");
322 if (compress != BTRFS_COMPRESS_NONE) {
323 outbuf = malloc(ram_size);
325 fprintf(stderr, "No memory\n");
332 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
333 bytenr, &length, &multi, mirror_num, NULL);
335 fprintf(stderr, "Error mapping block %d\n", ret);
338 device = multi->stripes[0].dev;
341 dev_bytenr = multi->stripes[0].physical;
344 if (size_left < length)
347 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
348 /* Need both checks, or we miss negative values due to u64 conversion */
349 if (done < 0 || done < length) {
350 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
353 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
354 if (mirror_num > num_copies) {
356 fprintf(stderr, "Exhausted mirrors trying to read\n");
359 fprintf(stderr, "Trying another mirror\n");
370 if (compress == BTRFS_COMPRESS_NONE) {
371 while (total < num_bytes) {
372 done = pwrite(fd, inbuf+total, num_bytes-total,
376 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
385 ret = decompress(inbuf, outbuf, disk_size, &ram_size, compress);
387 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
390 if (mirror_num >= num_copies) {
394 fprintf(stderr, "Trying another mirror\n");
398 while (total < num_bytes) {
399 done = pwrite(fd, outbuf + offset + total,
420 static enum loop_response ask_to_continue(const char *file)
425 printf("We seem to be looping a lot on %s, do you want to keep going "
426 "on ? (y/N/a): ", file);
428 ret = fgets(buf, 2, stdin);
429 if (*ret == '\n' || tolower(*ret) == 'n')
431 if (tolower(*ret) == 'a')
433 if (tolower(*ret) != 'y') {
434 printf("Please enter one of 'y', 'n', or 'a': ");
438 return LOOP_CONTINUE;
442 static int set_file_xattrs(struct btrfs_root *root, u64 inode,
443 int fd, const char *file_name)
445 struct btrfs_key key;
446 struct btrfs_path *path;
447 struct extent_buffer *leaf;
448 struct btrfs_dir_item *di;
457 key.objectid = inode;
458 key.type = BTRFS_XATTR_ITEM_KEY;
461 path = btrfs_alloc_path();
465 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
469 leaf = path->nodes[0];
471 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
473 ret = next_leaf(root, path);
476 "Error searching for extended attributes: %d\n",
480 /* No more leaves to search */
484 leaf = path->nodes[0];
489 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
490 if (key.type != BTRFS_XATTR_ITEM_KEY || key.objectid != inode)
493 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
494 di = btrfs_item_ptr(leaf, path->slots[0],
495 struct btrfs_dir_item);
497 while (cur < total_len) {
498 len = btrfs_dir_name_len(leaf, di);
499 if (len > name_len) {
501 name = (char *) malloc(len + 1);
507 read_extent_buffer(leaf, name,
508 (unsigned long)(di + 1), len);
512 len = btrfs_dir_data_len(leaf, di);
513 if (len > data_len) {
515 data = (char *) malloc(len);
521 read_extent_buffer(leaf, data,
522 (unsigned long)(di + 1) + name_len,
526 if (fsetxattr(fd, name, data, data_len, 0)) {
530 "Error setting extended attribute %s on file %s: %s\n",
531 name, file_name, strerror(err));
534 len = sizeof(*di) + name_len + data_len;
536 di = (struct btrfs_dir_item *)((char *)di + len);
542 btrfs_free_path(path);
550 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
553 struct extent_buffer *leaf;
554 struct btrfs_path *path;
555 struct btrfs_file_extent_item *fi;
556 struct btrfs_inode_item *inode_item;
557 struct btrfs_key found_key;
564 path = btrfs_alloc_path();
566 fprintf(stderr, "Ran out of memory\n");
570 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
572 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
573 struct btrfs_inode_item);
574 found_size = btrfs_inode_size(path->nodes[0], inode_item);
576 btrfs_release_path(path);
579 key->type = BTRFS_EXTENT_DATA_KEY;
581 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
583 fprintf(stderr, "Error searching %d\n", ret);
584 btrfs_free_path(path);
588 leaf = path->nodes[0];
590 ret = next_leaf(root, path);
592 fprintf(stderr, "Error getting next leaf %d\n",
594 btrfs_free_path(path);
596 } else if (ret > 0) {
597 /* No more leaves to search */
598 btrfs_free_path(path);
601 leaf = path->nodes[0];
605 if (loops >= 0 && loops++ >= 1024) {
606 enum loop_response resp;
608 resp = ask_to_continue(file);
609 if (resp == LOOP_STOP)
611 else if (resp == LOOP_CONTINUE)
613 else if (resp == LOOP_DONTASK)
616 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
618 ret = next_leaf(root, path);
620 fprintf(stderr, "Error searching %d\n", ret);
621 btrfs_free_path(path);
624 /* No more leaves to search */
625 btrfs_free_path(path);
628 leaf = path->nodes[0];
632 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
633 if (found_key.objectid != key->objectid)
635 if (found_key.type != key->type)
637 fi = btrfs_item_ptr(leaf, path->slots[0],
638 struct btrfs_file_extent_item);
639 extent_type = btrfs_file_extent_type(leaf, fi);
640 compression = btrfs_file_extent_compression(leaf, fi);
641 if (compression >= BTRFS_COMPRESS_LAST) {
642 fprintf(stderr, "Don't support compression yet %d\n",
644 btrfs_free_path(path);
648 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
650 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
651 ret = copy_one_inline(fd, path, found_key.offset);
653 btrfs_free_path(path);
656 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
657 ret = copy_one_extent(root, fd, leaf, fi,
660 btrfs_free_path(path);
664 printf("Weird extent type %d\n", extent_type);
670 btrfs_free_path(path);
673 ret = ftruncate(fd, (loff_t)found_size);
678 ret = set_file_xattrs(root, key->objectid, fd, file);
685 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
686 const char *output_rootdir, const char *in_dir,
689 struct btrfs_path *path;
690 struct extent_buffer *leaf;
691 struct btrfs_dir_item *dir_item;
692 struct btrfs_key found_key, location;
693 char filename[BTRFS_NAME_LEN + 1];
694 unsigned long name_ptr;
701 path = btrfs_alloc_path();
703 fprintf(stderr, "Ran out of memory\n");
708 key->type = BTRFS_DIR_INDEX_KEY;
710 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
712 fprintf(stderr, "Error searching %d\n", ret);
713 btrfs_free_path(path);
717 leaf = path->nodes[0];
720 printf("No leaf after search, looking for the next "
722 ret = next_leaf(root, path);
724 fprintf(stderr, "Error getting next leaf %d\n",
726 btrfs_free_path(path);
728 } else if (ret > 0) {
729 /* No more leaves to search */
731 printf("Reached the end of the tree looking "
732 "for the directory\n");
733 btrfs_free_path(path);
736 leaf = path->nodes[0];
740 if (loops++ >= 1024) {
741 printf("We have looped trying to restore files in %s "
742 "too many times to be making progress, "
743 "stopping\n", in_dir);
747 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
749 ret = next_leaf(root, path);
751 fprintf(stderr, "Error searching %d\n",
753 btrfs_free_path(path);
755 } else if (ret > 0) {
756 /* No more leaves to search */
758 printf("Reached the end of "
759 "the tree searching the"
761 btrfs_free_path(path);
764 leaf = path->nodes[0];
768 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
769 if (found_key.objectid != key->objectid) {
771 printf("Found objectid=%Lu, key=%Lu\n",
772 found_key.objectid, key->objectid);
775 if (found_key.type != key->type) {
777 printf("Found type=%u, want=%u\n",
778 found_key.type, key->type);
781 dir_item = btrfs_item_ptr(leaf, path->slots[0],
782 struct btrfs_dir_item);
783 name_ptr = (unsigned long)(dir_item + 1);
784 name_len = btrfs_dir_name_len(leaf, dir_item);
785 read_extent_buffer(leaf, filename, name_ptr, name_len);
786 filename[name_len] = '\0';
787 type = btrfs_dir_type(leaf, dir_item);
788 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
790 /* full path from root of btrfs being restored */
791 snprintf(fs_name, 4096, "%s/%s", in_dir, filename);
793 if (mreg && REG_NOMATCH == regexec(mreg, fs_name, 0, NULL, 0))
796 /* full path from system root */
797 snprintf(path_name, 4096, "%s%s", output_rootdir, fs_name);
800 * At this point we're only going to restore directories and
801 * files, no symlinks or anything else.
803 if (type == BTRFS_FT_REG_FILE) {
808 ret = stat(path_name, &st);
811 if (verbose || !warn)
812 printf("Skipping existing file"
816 printf("If you wish to overwrite use "
817 "the -o option to overwrite\n");
824 printf("Restoring %s\n", path_name);
827 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
829 fprintf(stderr, "Error creating %s: %d\n",
833 btrfs_free_path(path);
837 ret = copy_file(root, fd, &location, path_name);
842 btrfs_free_path(path);
845 } else if (type == BTRFS_FT_DIR) {
846 struct btrfs_root *search_root = root;
847 char *dir = strdup(fs_name);
850 fprintf(stderr, "Ran out of memory\n");
851 btrfs_free_path(path);
855 if (location.type == BTRFS_ROOT_ITEM_KEY) {
857 * If we are a snapshot and this is the index
858 * object to ourselves just skip it.
860 if (location.objectid ==
861 root->root_key.objectid) {
866 location.offset = (u64)-1;
867 search_root = btrfs_read_fs_root(root->fs_info,
869 if (IS_ERR(search_root)) {
871 fprintf(stderr, "Error reading "
872 "subvolume %s: %lu\n",
874 PTR_ERR(search_root));
877 btrfs_free_path(path);
878 return PTR_ERR(search_root);
882 * A subvolume will have a key.offset of 0, a
883 * snapshot will have key.offset of a transid.
885 if (search_root->root_key.offset != 0 &&
888 printf("Skipping snapshot %s\n",
892 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
896 printf("Restoring %s\n", path_name);
902 ret = mkdir(path_name, 0755);
903 if (ret && errno != EEXIST) {
905 fprintf(stderr, "Error mkdiring %s: %d\n",
909 btrfs_free_path(path);
913 ret = search_dir(search_root, &location,
914 output_rootdir, dir, mreg);
919 btrfs_free_path(path);
928 printf("Done searching %s\n", in_dir);
929 btrfs_free_path(path);
933 static int do_list_roots(struct btrfs_root *root)
935 struct btrfs_key key;
936 struct btrfs_key found_key;
937 struct btrfs_disk_key disk_key;
938 struct btrfs_path *path;
939 struct extent_buffer *leaf;
940 struct btrfs_root_item ri;
941 unsigned long offset;
945 root = root->fs_info->tree_root;
946 path = btrfs_alloc_path();
948 fprintf(stderr, "Failed to alloc path\n");
954 key.type = BTRFS_ROOT_ITEM_KEY;
956 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
958 fprintf(stderr, "Failed to do search %d\n", ret);
959 btrfs_free_path(path);
964 leaf = path->nodes[0];
965 slot = path->slots[0];
966 if (slot >= btrfs_header_nritems(leaf)) {
967 ret = btrfs_next_leaf(root, path);
970 leaf = path->nodes[0];
971 slot = path->slots[0];
973 btrfs_item_key(leaf, &disk_key, slot);
974 btrfs_disk_key_to_cpu(&found_key, &disk_key);
975 if (btrfs_key_type(&found_key) != BTRFS_ROOT_ITEM_KEY) {
980 offset = btrfs_item_ptr_offset(leaf, slot);
981 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
983 btrfs_print_key(&disk_key);
984 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
985 btrfs_root_level(&ri));
988 btrfs_free_path(path);
993 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
994 int super_mirror, int list_roots)
996 struct btrfs_fs_info *fs_info = NULL;
997 struct btrfs_root *root = NULL;
1001 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1002 bytenr = btrfs_sb_offset(i);
1003 fs_info = open_ctree_fs_info(dev, bytenr, root_location,
1004 OPEN_CTREE_PARTIAL);
1007 fprintf(stderr, "Could not open root, trying backup super\n");
1014 * All we really need to succeed is reading the chunk tree, everything
1015 * else we can do by hand, since we only need to read the tree root and
1018 if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
1021 root = fs_info->tree_root;
1023 root_location = btrfs_super_root(fs_info->super_copy);
1024 generation = btrfs_super_generation(fs_info->super_copy);
1025 root->node = read_tree_block(root, root_location,
1026 root->leafsize, generation);
1027 if (!extent_buffer_uptodate(root->node)) {
1028 fprintf(stderr, "Error opening tree root\n");
1034 if (!list_roots && !fs_info->fs_root) {
1035 struct btrfs_key key;
1037 key.objectid = BTRFS_FS_TREE_OBJECTID;
1038 key.type = BTRFS_ROOT_ITEM_KEY;
1039 key.offset = (u64)-1;
1040 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
1041 if (IS_ERR(fs_info->fs_root)) {
1042 fprintf(stderr, "Couldn't read fs root: %ld\n",
1043 PTR_ERR(fs_info->fs_root));
1044 close_ctree(fs_info->tree_root);
1049 if (list_roots && do_list_roots(fs_info->tree_root)) {
1050 close_ctree(fs_info->tree_root);
1054 return fs_info->fs_root;
1057 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
1059 struct btrfs_path *path;
1060 struct btrfs_key found_key;
1061 struct btrfs_key key;
1066 key.type = BTRFS_DIR_INDEX_KEY;
1069 path = btrfs_alloc_path();
1071 fprintf(stderr, "Ran out of memory\n");
1075 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1077 fprintf(stderr, "Error searching %d\n", ret);
1081 if (!path->nodes[0]) {
1082 fprintf(stderr, "No leaf!\n");
1086 for (i = path->slots[0];
1087 i < btrfs_header_nritems(path->nodes[0]); i++) {
1088 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
1089 if (found_key.type != key.type)
1092 printf("Using objectid %Lu for first dir\n",
1093 found_key.objectid);
1094 *objectid = found_key.objectid;
1099 ret = next_leaf(root, path);
1101 fprintf(stderr, "Error getting next leaf %d\n",
1104 } else if (ret > 0) {
1105 fprintf(stderr, "No more leaves\n");
1108 } while (!path->nodes[0]);
1111 printf("Couldn't find a dir index item\n");
1113 btrfs_free_path(path);
1117 static struct option long_options[] = {
1118 { "path-regex", 1, NULL, 256},
1119 { "dry-run", 0, NULL, 'D'},
1123 const char * const cmd_restore_usage[] = {
1124 "btrfs restore [options] <device> <path> | -l <device>",
1125 "Try to restore files from a damaged filesystem (unmounted)",
1128 "-x get extended attributes",
1132 "-t <bytenr> tree location",
1133 "-f <bytenr> filesystem location",
1134 "-u <mirror> super mirror",
1135 "-r <rootid> root objectid",
1137 "-l list tree roots",
1138 "-D|--dry-run dry run (only list files that would be recovered)",
1139 "--path-regex <regex>",
1140 " restore only filenames matching regex,",
1141 " you have to use following syntax (possibly quoted):",
1142 " ^/(|home(|/username(|/Desktop(|/.*))))$",
1143 "-c ignore case (--path-regrex only)",
1147 int cmd_restore(int argc, char **argv)
1149 struct btrfs_root *root;
1150 struct btrfs_key key;
1152 u64 tree_location = 0;
1153 u64 fs_location = 0;
1154 u64 root_objectid = 0;
1158 int option_index = 0;
1159 int super_mirror = 0;
1162 const char *match_regstr = NULL;
1163 int match_cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
1164 regex_t match_reg, *mreg = NULL;
1167 while ((opt = getopt_long(argc, argv, "sxviot:u:df:r:lDc", long_options,
1168 &option_index)) != -1) {
1184 tree_location = arg_strtou64(optarg);
1187 fs_location = arg_strtou64(optarg);
1190 super_mirror = arg_strtou64(optarg);
1191 if (super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1192 fprintf(stderr, "Super mirror not "
1201 root_objectid = arg_strtou64(optarg);
1202 if (!is_fstree(root_objectid)) {
1203 fprintf(stderr, "objectid %llu is not a valid fs/file tree\n",
1215 match_cflags |= REG_ICASE;
1217 /* long option without single letter alternative */
1219 match_regstr = optarg;
1225 usage(cmd_restore_usage);
1230 if (!list_roots && check_argc_min(argc - optind, 2))
1231 usage(cmd_restore_usage);
1232 else if (list_roots && check_argc_min(argc - optind, 1))
1233 usage(cmd_restore_usage);
1235 if (fs_location && root_objectid) {
1236 fprintf(stderr, "don't use -f and -r at the same time.\n");
1240 if ((ret = check_mounted(argv[optind])) < 0) {
1241 fprintf(stderr, "Could not check mount status: %s\n",
1245 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
1249 root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1256 if (fs_location != 0) {
1257 free_extent_buffer(root->node);
1258 root->node = read_tree_block(root, fs_location, root->leafsize, 0);
1260 fprintf(stderr, "Failed to read fs location\n");
1266 memset(path_name, 0, 4096);
1268 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1269 dir_name[sizeof dir_name - 1] = 0;
1271 /* Strip the trailing / on the dir name */
1272 len = strlen(dir_name);
1273 while (len && dir_name[--len] == '/') {
1274 dir_name[len] = '\0';
1277 if (root_objectid != 0) {
1278 struct btrfs_root *orig_root = root;
1280 key.objectid = root_objectid;
1281 key.type = BTRFS_ROOT_ITEM_KEY;
1282 key.offset = (u64)-1;
1283 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1285 fprintf(stderr, "fail to read root %llu: %s\n",
1286 root_objectid, strerror(-PTR_ERR(root)));
1296 ret = find_first_dir(root, &key.objectid);
1300 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1304 ret = regcomp(&match_reg, match_regstr, match_cflags);
1306 regerror(ret, &match_reg, reg_err, sizeof(reg_err));
1307 fprintf(stderr, "Regex compile failed: %s\n", reg_err);
1314 printf("This is a dry-run, no files are going to be restored\n");
1316 ret = search_dir(root, &key, dir_name, "", mreg);