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", 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 *decompress_len = out_len;
146 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
147 u64 *decompress_len, int compress)
150 case BTRFS_COMPRESS_ZLIB:
151 return decompress_zlib(inbuf, outbuf, compress_len,
153 case BTRFS_COMPRESS_LZO:
154 return decompress_lzo((unsigned char *)inbuf, outbuf, compress_len,
160 fprintf(stderr, "invalid compression type: %d\n", compress);
164 static int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
169 struct extent_buffer *c;
170 struct extent_buffer *next = NULL;
173 for (; level < BTRFS_MAX_LEVEL; level++) {
174 if (path->nodes[level])
178 if (level == BTRFS_MAX_LEVEL)
181 slot = path->slots[level] + 1;
183 while(level < BTRFS_MAX_LEVEL) {
184 if (!path->nodes[level])
187 slot = path->slots[level] + offset;
188 c = path->nodes[level];
189 if (slot >= btrfs_header_nritems(c)) {
191 if (level == BTRFS_MAX_LEVEL)
197 reada_for_search(root, path, level, slot, 0);
199 next = read_node_slot(root, c, slot);
204 path->slots[level] = slot;
207 c = path->nodes[level];
208 free_extent_buffer(c);
209 path->nodes[level] = next;
210 path->slots[level] = 0;
214 reada_for_search(root, path, level, 0, 0);
215 next = read_node_slot(root, next, 0);
222 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
224 struct extent_buffer *leaf = path->nodes[0];
225 struct btrfs_file_extent_item *fi;
235 fi = btrfs_item_ptr(leaf, path->slots[0],
236 struct btrfs_file_extent_item);
237 ptr = btrfs_file_extent_inline_start(fi);
238 len = btrfs_file_extent_inline_len(leaf, path->slots[0], fi);
239 read_extent_buffer(leaf, buf, ptr, len);
241 compress = btrfs_file_extent_compression(leaf, fi);
242 if (compress == BTRFS_COMPRESS_NONE) {
243 done = pwrite(fd, buf, len, pos);
245 fprintf(stderr, "Short inline write, wanted %d, did "
246 "%zd: %d\n", len, done, errno);
252 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
253 outbuf = malloc(ram_size);
255 fprintf(stderr, "No memory\n");
259 ret = decompress(buf, outbuf, len, &ram_size, compress);
265 done = pwrite(fd, outbuf, ram_size, pos);
267 if (done < ram_size) {
268 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
269 "did %zd: %d\n", ram_size, done, errno);
276 static int copy_one_extent(struct btrfs_root *root, int fd,
277 struct extent_buffer *leaf,
278 struct btrfs_file_extent_item *fi, u64 pos)
280 struct btrfs_multi_bio *multi = NULL;
281 struct btrfs_device *device;
282 char *inbuf, *outbuf = NULL;
283 ssize_t done, total = 0;
299 compress = btrfs_file_extent_compression(leaf, fi);
300 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
301 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
302 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
303 offset = btrfs_file_extent_offset(leaf, fi);
304 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
305 size_left = disk_size;
306 if (compress == BTRFS_COMPRESS_NONE)
310 printf("offset is %Lu\n", offset);
311 /* we found a hole */
315 inbuf = malloc(size_left);
317 fprintf(stderr, "No memory\n");
321 if (compress != BTRFS_COMPRESS_NONE) {
322 outbuf = malloc(ram_size);
324 fprintf(stderr, "No memory\n");
331 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
332 bytenr, &length, &multi, mirror_num, NULL);
334 fprintf(stderr, "Error mapping block %d\n", ret);
337 device = multi->stripes[0].dev;
340 dev_bytenr = multi->stripes[0].physical;
343 if (size_left < length)
346 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
347 /* Need both checks, or we miss negative values due to u64 conversion */
348 if (done < 0 || done < length) {
349 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
352 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
353 if (mirror_num > num_copies) {
355 fprintf(stderr, "Exhausted mirrors trying to read\n");
358 fprintf(stderr, "Trying another mirror\n");
369 if (compress == BTRFS_COMPRESS_NONE) {
370 while (total < num_bytes) {
371 done = pwrite(fd, inbuf+total, num_bytes-total,
375 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
384 ret = decompress(inbuf, outbuf, disk_size, &ram_size, compress);
386 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
389 if (mirror_num >= num_copies) {
393 fprintf(stderr, "Trying another mirror\n");
397 while (total < num_bytes) {
398 done = pwrite(fd, outbuf + offset + total,
413 static int ask_to_continue(const char *file)
418 printf("We seem to be looping a lot on %s, do you want to keep going "
419 "on ? (y/N): ", file);
421 ret = fgets(buf, 2, stdin);
422 if (*ret == '\n' || tolower(*ret) == 'n')
424 if (tolower(*ret) != 'y') {
425 printf("Please enter either 'y' or 'n': ");
433 static int set_file_xattrs(struct btrfs_root *root, u64 inode,
434 int fd, const char *file_name)
436 struct btrfs_key key;
437 struct btrfs_path *path;
438 struct extent_buffer *leaf;
439 struct btrfs_dir_item *di;
448 key.objectid = inode;
449 key.type = BTRFS_XATTR_ITEM_KEY;
452 path = btrfs_alloc_path();
456 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
460 leaf = path->nodes[0];
462 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
464 ret = next_leaf(root, path);
467 "Error searching for extended attributes: %d\n",
471 /* No more leaves to search */
475 leaf = path->nodes[0];
480 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
481 if (key.type != BTRFS_XATTR_ITEM_KEY || key.objectid != inode)
484 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
485 di = btrfs_item_ptr(leaf, path->slots[0],
486 struct btrfs_dir_item);
488 while (cur < total_len) {
489 len = btrfs_dir_name_len(leaf, di);
490 if (len > name_len) {
492 name = (char *) malloc(len + 1);
498 read_extent_buffer(leaf, name,
499 (unsigned long)(di + 1), len);
503 len = btrfs_dir_data_len(leaf, di);
504 if (len > data_len) {
506 data = (char *) malloc(len);
512 read_extent_buffer(leaf, data,
513 (unsigned long)(di + 1) + name_len,
517 if (fsetxattr(fd, name, data, data_len, 0)) {
521 "Error setting extended attribute %s on file %s: %s\n",
522 name, file_name, strerror(err));
525 len = sizeof(*di) + name_len + data_len;
527 di = (struct btrfs_dir_item *)((char *)di + len);
533 btrfs_free_path(path);
541 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
544 struct extent_buffer *leaf;
545 struct btrfs_path *path;
546 struct btrfs_file_extent_item *fi;
547 struct btrfs_inode_item *inode_item;
548 struct btrfs_key found_key;
555 path = btrfs_alloc_path();
557 fprintf(stderr, "Ran out of memory\n");
560 path->skip_locking = 1;
562 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
564 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
565 struct btrfs_inode_item);
566 found_size = btrfs_inode_size(path->nodes[0], inode_item);
568 btrfs_release_path(path);
571 key->type = BTRFS_EXTENT_DATA_KEY;
573 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
575 fprintf(stderr, "Error searching %d\n", ret);
576 btrfs_free_path(path);
580 leaf = path->nodes[0];
582 ret = next_leaf(root, path);
584 fprintf(stderr, "Error getting next leaf %d\n",
586 btrfs_free_path(path);
588 } else if (ret > 0) {
589 /* No more leaves to search */
590 btrfs_free_path(path);
593 leaf = path->nodes[0];
597 if (loops++ >= 1024) {
598 ret = ask_to_continue(file);
603 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
605 ret = next_leaf(root, path);
607 fprintf(stderr, "Error searching %d\n", ret);
608 btrfs_free_path(path);
611 /* No more leaves to search */
612 btrfs_free_path(path);
615 leaf = path->nodes[0];
619 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
620 if (found_key.objectid != key->objectid)
622 if (found_key.type != key->type)
624 fi = btrfs_item_ptr(leaf, path->slots[0],
625 struct btrfs_file_extent_item);
626 extent_type = btrfs_file_extent_type(leaf, fi);
627 compression = btrfs_file_extent_compression(leaf, fi);
628 if (compression >= BTRFS_COMPRESS_LAST) {
629 fprintf(stderr, "Don't support compression yet %d\n",
631 btrfs_free_path(path);
635 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
637 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
638 ret = copy_one_inline(fd, path, found_key.offset);
640 btrfs_free_path(path);
643 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
644 ret = copy_one_extent(root, fd, leaf, fi,
647 btrfs_free_path(path);
651 printf("Weird extent type %d\n", extent_type);
657 btrfs_free_path(path);
660 ret = ftruncate(fd, (loff_t)found_size);
665 ret = set_file_xattrs(root, key->objectid, fd, file);
672 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
673 const char *output_rootdir, const char *in_dir,
676 struct btrfs_path *path;
677 struct extent_buffer *leaf;
678 struct btrfs_dir_item *dir_item;
679 struct btrfs_key found_key, location;
680 char filename[BTRFS_NAME_LEN + 1];
681 unsigned long name_ptr;
688 path = btrfs_alloc_path();
690 fprintf(stderr, "Ran out of memory\n");
693 path->skip_locking = 1;
696 key->type = BTRFS_DIR_INDEX_KEY;
698 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
700 fprintf(stderr, "Error searching %d\n", ret);
701 btrfs_free_path(path);
705 leaf = path->nodes[0];
708 printf("No leaf after search, looking for the next "
710 ret = next_leaf(root, path);
712 fprintf(stderr, "Error getting next leaf %d\n",
714 btrfs_free_path(path);
716 } else if (ret > 0) {
717 /* No more leaves to search */
719 printf("Reached the end of the tree looking "
720 "for the directory\n");
721 btrfs_free_path(path);
724 leaf = path->nodes[0];
728 if (loops++ >= 1024) {
729 printf("We have looped trying to restore files in %s "
730 "too many times to be making progress, "
731 "stopping\n", in_dir);
735 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
737 ret = next_leaf(root, path);
739 fprintf(stderr, "Error searching %d\n",
741 btrfs_free_path(path);
743 } else if (ret > 0) {
744 /* No more leaves to search */
746 printf("Reached the end of "
747 "the tree searching the"
749 btrfs_free_path(path);
752 leaf = path->nodes[0];
756 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
757 if (found_key.objectid != key->objectid) {
759 printf("Found objectid=%Lu, key=%Lu\n",
760 found_key.objectid, key->objectid);
763 if (found_key.type != key->type) {
765 printf("Found type=%u, want=%u\n",
766 found_key.type, key->type);
769 dir_item = btrfs_item_ptr(leaf, path->slots[0],
770 struct btrfs_dir_item);
771 name_ptr = (unsigned long)(dir_item + 1);
772 name_len = btrfs_dir_name_len(leaf, dir_item);
773 read_extent_buffer(leaf, filename, name_ptr, name_len);
774 filename[name_len] = '\0';
775 type = btrfs_dir_type(leaf, dir_item);
776 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
778 /* full path from root of btrfs being restored */
779 snprintf(fs_name, 4096, "%s/%s", in_dir, filename);
781 if (mreg && REG_NOMATCH == regexec(mreg, fs_name, 0, NULL, 0))
784 /* full path from system root */
785 snprintf(path_name, 4096, "%s%s", output_rootdir, fs_name);
788 * At this point we're only going to restore directories and
789 * files, no symlinks or anything else.
791 if (type == BTRFS_FT_REG_FILE) {
796 ret = stat(path_name, &st);
799 if (verbose || !warn)
800 printf("Skipping existing file"
804 printf("If you wish to overwrite use "
805 "the -o option to overwrite\n");
812 printf("Restoring %s\n", path_name);
815 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
817 fprintf(stderr, "Error creating %s: %d\n",
821 btrfs_free_path(path);
825 ret = copy_file(root, fd, &location, path_name);
830 btrfs_free_path(path);
833 } else if (type == BTRFS_FT_DIR) {
834 struct btrfs_root *search_root = root;
835 char *dir = strdup(fs_name);
838 fprintf(stderr, "Ran out of memory\n");
839 btrfs_free_path(path);
843 if (location.type == BTRFS_ROOT_ITEM_KEY) {
845 * If we are a snapshot and this is the index
846 * object to ourselves just skip it.
848 if (location.objectid ==
849 root->root_key.objectid) {
854 location.offset = (u64)-1;
855 search_root = btrfs_read_fs_root(root->fs_info,
857 if (IS_ERR(search_root)) {
859 fprintf(stderr, "Error reading "
860 "subvolume %s: %lu\n",
862 PTR_ERR(search_root));
865 btrfs_free_path(path);
866 return PTR_ERR(search_root);
870 * A subvolume will have a key.offset of 0, a
871 * snapshot will have key.offset of a transid.
873 if (search_root->root_key.offset != 0 &&
876 printf("Skipping snapshot %s\n",
880 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
884 printf("Restoring %s\n", path_name);
890 ret = mkdir(path_name, 0755);
891 if (ret && errno != EEXIST) {
893 fprintf(stderr, "Error mkdiring %s: %d\n",
897 btrfs_free_path(path);
901 ret = search_dir(search_root, &location,
902 output_rootdir, dir, mreg);
907 btrfs_free_path(path);
916 printf("Done searching %s\n", in_dir);
917 btrfs_free_path(path);
921 static int do_list_roots(struct btrfs_root *root)
923 struct btrfs_key key;
924 struct btrfs_key found_key;
925 struct btrfs_disk_key disk_key;
926 struct btrfs_path *path;
927 struct extent_buffer *leaf;
928 struct btrfs_root_item ri;
929 unsigned long offset;
933 root = root->fs_info->tree_root;
934 path = btrfs_alloc_path();
936 fprintf(stderr, "Failed to alloc path\n");
942 key.type = BTRFS_ROOT_ITEM_KEY;
944 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
946 fprintf(stderr, "Failed to do search %d\n", ret);
947 btrfs_free_path(path);
952 leaf = path->nodes[0];
953 slot = path->slots[0];
954 if (slot >= btrfs_header_nritems(leaf)) {
955 ret = btrfs_next_leaf(root, path);
958 leaf = path->nodes[0];
959 slot = path->slots[0];
961 btrfs_item_key(leaf, &disk_key, slot);
962 btrfs_disk_key_to_cpu(&found_key, &disk_key);
963 if (btrfs_key_type(&found_key) != BTRFS_ROOT_ITEM_KEY) {
968 offset = btrfs_item_ptr_offset(leaf, slot);
969 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
971 btrfs_print_key(&disk_key);
972 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
973 btrfs_root_level(&ri));
976 btrfs_free_path(path);
981 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
982 int super_mirror, int list_roots)
984 struct btrfs_fs_info *fs_info = NULL;
985 struct btrfs_root *root = NULL;
989 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
990 bytenr = btrfs_sb_offset(i);
991 fs_info = open_ctree_fs_info(dev, bytenr, root_location,
995 fprintf(stderr, "Could not open root, trying backup super\n");
1002 * All we really need to succeed is reading the chunk tree, everything
1003 * else we can do by hand, since we only need to read the tree root and
1006 if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
1009 root = fs_info->tree_root;
1011 root_location = btrfs_super_root(fs_info->super_copy);
1012 generation = btrfs_super_generation(fs_info->super_copy);
1013 root->node = read_tree_block(root, root_location,
1014 root->leafsize, generation);
1015 if (!extent_buffer_uptodate(root->node)) {
1016 fprintf(stderr, "Error opening tree root\n");
1022 if (!list_roots && !fs_info->fs_root) {
1023 struct btrfs_key key;
1025 key.objectid = BTRFS_FS_TREE_OBJECTID;
1026 key.type = BTRFS_ROOT_ITEM_KEY;
1027 key.offset = (u64)-1;
1028 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
1029 if (IS_ERR(fs_info->fs_root)) {
1030 fprintf(stderr, "Couldn't read fs root: %ld\n",
1031 PTR_ERR(fs_info->fs_root));
1032 close_ctree(fs_info->tree_root);
1037 if (list_roots && do_list_roots(fs_info->tree_root)) {
1038 close_ctree(fs_info->tree_root);
1042 return fs_info->fs_root;
1045 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
1047 struct btrfs_path *path;
1048 struct btrfs_key found_key;
1049 struct btrfs_key key;
1054 key.type = BTRFS_DIR_INDEX_KEY;
1057 path = btrfs_alloc_path();
1059 fprintf(stderr, "Ran out of memory\n");
1063 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1065 fprintf(stderr, "Error searching %d\n", ret);
1069 if (!path->nodes[0]) {
1070 fprintf(stderr, "No leaf!\n");
1074 for (i = path->slots[0];
1075 i < btrfs_header_nritems(path->nodes[0]); i++) {
1076 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
1077 if (found_key.type != key.type)
1080 printf("Using objectid %Lu for first dir\n",
1081 found_key.objectid);
1082 *objectid = found_key.objectid;
1087 ret = next_leaf(root, path);
1089 fprintf(stderr, "Error getting next leaf %d\n",
1092 } else if (ret > 0) {
1093 fprintf(stderr, "No more leaves\n");
1096 } while (!path->nodes[0]);
1099 printf("Couldn't find a dir index item\n");
1101 btrfs_free_path(path);
1105 static struct option long_options[] = {
1106 { "path-regex", 1, NULL, 256},
1107 { "dry-run", 0, NULL, 'D'},
1111 const char * const cmd_restore_usage[] = {
1112 "btrfs restore [options] <device> <path> | -l <device>",
1113 "Try to restore files from a damaged filesystem (unmounted)",
1116 "-x get extended attributes",
1120 "-t <bytenr> tree location",
1121 "-f <bytenr> filesystem location",
1122 "-u <mirror> super mirror",
1123 "-r <rootid> root objectid",
1125 "-l list tree roots",
1126 "-D|--dry-run dry run (only list files that would be recovered)",
1127 "--path-regex <regex>",
1128 " restore only filenames matching regex,",
1129 " you have to use following syntax (possibly quoted):",
1130 " ^/(|home(|/username(|/Desktop(|/.*))))$",
1131 "-c ignore case (--path-regrex only)",
1135 int cmd_restore(int argc, char **argv)
1137 struct btrfs_root *root;
1138 struct btrfs_key key;
1140 u64 tree_location = 0;
1141 u64 fs_location = 0;
1142 u64 root_objectid = 0;
1146 int option_index = 0;
1147 int super_mirror = 0;
1150 const char *match_regstr = NULL;
1151 int match_cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
1152 regex_t match_reg, *mreg = NULL;
1155 while ((opt = getopt_long(argc, argv, "sxviot:u:df:r:lDc", long_options,
1156 &option_index)) != -1) {
1172 tree_location = arg_strtou64(optarg);
1175 fs_location = arg_strtou64(optarg);
1178 super_mirror = arg_strtou64(optarg);
1179 if (super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1180 fprintf(stderr, "Super mirror not "
1189 root_objectid = arg_strtou64(optarg);
1190 if (!is_fstree(root_objectid)) {
1191 fprintf(stderr, "objectid %llu is not a valid fs/file tree\n",
1203 match_cflags |= REG_ICASE;
1205 /* long option without single letter alternative */
1207 match_regstr = optarg;
1213 usage(cmd_restore_usage);
1217 if (!list_roots && optind + 1 >= argc)
1218 usage(cmd_restore_usage);
1219 else if (list_roots && optind >= argc)
1220 usage(cmd_restore_usage);
1222 if (fs_location && root_objectid) {
1223 fprintf(stderr, "don't use -f and -r at the same time.\n");
1227 if ((ret = check_mounted(argv[optind])) < 0) {
1228 fprintf(stderr, "Could not check mount status: %s\n",
1232 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
1236 root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1243 if (fs_location != 0) {
1244 free_extent_buffer(root->node);
1245 root->node = read_tree_block(root, fs_location, root->leafsize, 0);
1247 fprintf(stderr, "Failed to read fs location\n");
1253 memset(path_name, 0, 4096);
1255 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1256 dir_name[sizeof dir_name - 1] = 0;
1258 /* Strip the trailing / on the dir name */
1259 len = strlen(dir_name);
1260 while (len && dir_name[--len] == '/') {
1261 dir_name[len] = '\0';
1264 if (root_objectid != 0) {
1265 struct btrfs_root *orig_root = root;
1267 key.objectid = root_objectid;
1268 key.type = BTRFS_ROOT_ITEM_KEY;
1269 key.offset = (u64)-1;
1270 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1272 fprintf(stderr, "fail to read root %llu: %s\n",
1273 root_objectid, strerror(-PTR_ERR(root)));
1283 ret = find_first_dir(root, &key.objectid);
1287 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1291 ret = regcomp(&match_reg, match_regstr, match_cflags);
1293 regerror(ret, &match_reg, reg_err, sizeof(reg_err));
1294 fprintf(stderr, "Regex compile failed: %s\n", reg_err);
1301 printf("This is a dry-run, no files are going to be restored\n");
1303 ret = search_dir(root, &key, dir_name, "", mreg);