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);
121 new_len = lzo1x_worst_compress(PAGE_CACHE_SIZE);
122 ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len,
123 (unsigned char *)outbuf,
124 (void *)&new_len, NULL);
125 if (ret != LZO_E_OK) {
126 fprintf(stderr, "failed to inflate: %d\n", ret);
135 *decompress_len = out_len;
140 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
141 u64 *decompress_len, int compress)
144 case BTRFS_COMPRESS_ZLIB:
145 return decompress_zlib(inbuf, outbuf, compress_len,
147 case BTRFS_COMPRESS_LZO:
148 return decompress_lzo((unsigned char *)inbuf, outbuf, compress_len,
154 fprintf(stderr, "invalid compression type: %d\n", compress);
158 static int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
163 struct extent_buffer *c;
164 struct extent_buffer *next = NULL;
167 for (; level < BTRFS_MAX_LEVEL; level++) {
168 if (path->nodes[level])
172 if (level == BTRFS_MAX_LEVEL)
175 slot = path->slots[level] + 1;
177 while(level < BTRFS_MAX_LEVEL) {
178 if (!path->nodes[level])
181 slot = path->slots[level] + offset;
182 c = path->nodes[level];
183 if (slot >= btrfs_header_nritems(c)) {
185 if (level == BTRFS_MAX_LEVEL)
191 reada_for_search(root, path, level, slot, 0);
193 next = read_node_slot(root, c, slot);
198 path->slots[level] = slot;
201 c = path->nodes[level];
202 free_extent_buffer(c);
203 path->nodes[level] = next;
204 path->slots[level] = 0;
208 reada_for_search(root, path, level, 0, 0);
209 next = read_node_slot(root, next, 0);
216 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
218 struct extent_buffer *leaf = path->nodes[0];
219 struct btrfs_file_extent_item *fi;
229 fi = btrfs_item_ptr(leaf, path->slots[0],
230 struct btrfs_file_extent_item);
231 ptr = btrfs_file_extent_inline_start(fi);
232 len = btrfs_file_extent_inline_item_len(leaf,
233 btrfs_item_nr(path->slots[0]));
234 read_extent_buffer(leaf, buf, ptr, len);
236 compress = btrfs_file_extent_compression(leaf, fi);
237 if (compress == BTRFS_COMPRESS_NONE) {
238 done = pwrite(fd, buf, len, pos);
240 fprintf(stderr, "Short inline write, wanted %d, did "
241 "%zd: %d\n", len, done, errno);
247 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
248 outbuf = malloc(ram_size);
250 fprintf(stderr, "No memory\n");
254 ret = decompress(buf, outbuf, len, &ram_size, compress);
260 done = pwrite(fd, outbuf, ram_size, pos);
262 if (done < ram_size) {
263 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
264 "did %zd: %d\n", ram_size, done, errno);
271 static int copy_one_extent(struct btrfs_root *root, int fd,
272 struct extent_buffer *leaf,
273 struct btrfs_file_extent_item *fi, u64 pos)
275 struct btrfs_multi_bio *multi = NULL;
276 struct btrfs_device *device;
277 char *inbuf, *outbuf = NULL;
278 ssize_t done, total = 0;
294 compress = btrfs_file_extent_compression(leaf, fi);
295 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
296 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
297 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
298 offset = btrfs_file_extent_offset(leaf, fi);
299 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
300 size_left = num_bytes;
301 if (compress == BTRFS_COMPRESS_NONE)
305 printf("offset is %Lu\n", offset);
306 /* we found a hole */
310 inbuf = malloc(size_left);
312 fprintf(stderr, "No memory\n");
316 if (compress != BTRFS_COMPRESS_NONE) {
317 outbuf = malloc(ram_size);
319 fprintf(stderr, "No memory\n");
326 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
327 bytenr, &length, &multi, mirror_num, NULL);
329 fprintf(stderr, "Error mapping block %d\n", ret);
332 device = multi->stripes[0].dev;
335 dev_bytenr = multi->stripes[0].physical;
338 if (size_left < length)
341 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
342 /* Need both checks, or we miss negative values due to u64 conversion */
343 if (done < 0 || done < length) {
344 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
347 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
348 if (mirror_num > num_copies) {
350 fprintf(stderr, "Exhausted mirrors trying to read\n");
353 fprintf(stderr, "Trying another mirror\n");
364 if (compress == BTRFS_COMPRESS_NONE) {
365 while (total < num_bytes) {
366 done = pwrite(fd, inbuf+total, num_bytes-total,
370 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
379 ret = decompress(inbuf, outbuf, num_bytes, &ram_size, compress);
381 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
384 if (mirror_num >= num_copies) {
388 fprintf(stderr, "Trying another mirror\n");
392 while (total < num_bytes) {
393 done = pwrite(fd, outbuf + offset + total,
408 static int ask_to_continue(const char *file)
413 printf("We seem to be looping a lot on %s, do you want to keep going "
414 "on ? (y/N): ", file);
416 ret = fgets(buf, 2, stdin);
417 if (*ret == '\n' || tolower(*ret) == 'n')
419 if (tolower(*ret) != 'y') {
420 printf("Please enter either 'y' or 'n': ");
428 static int set_file_xattrs(struct btrfs_root *root, u64 inode,
429 int fd, const char *file_name)
431 struct btrfs_key key;
432 struct btrfs_path *path;
433 struct extent_buffer *leaf;
434 struct btrfs_dir_item *di;
443 key.objectid = inode;
444 key.type = BTRFS_XATTR_ITEM_KEY;
447 path = btrfs_alloc_path();
451 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
455 leaf = path->nodes[0];
457 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
459 ret = next_leaf(root, path);
462 "Error searching for extended attributes: %d\n",
466 /* No more leaves to search */
470 leaf = path->nodes[0];
475 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
476 if (key.type != BTRFS_XATTR_ITEM_KEY || key.objectid != inode)
479 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
480 di = btrfs_item_ptr(leaf, path->slots[0],
481 struct btrfs_dir_item);
483 while (cur < total_len) {
484 len = btrfs_dir_name_len(leaf, di);
485 if (len > name_len) {
487 name = (char *) malloc(len + 1);
493 read_extent_buffer(leaf, name,
494 (unsigned long)(di + 1), len);
498 len = btrfs_dir_data_len(leaf, di);
499 if (len > data_len) {
501 data = (char *) malloc(len);
507 read_extent_buffer(leaf, data,
508 (unsigned long)(di + 1) + name_len,
512 if (fsetxattr(fd, name, data, data_len, 0)) {
516 "Error setting extended attribute %s on file %s: %s\n",
517 name, file_name, strerror(err));
520 len = sizeof(*di) + name_len + data_len;
522 di = (struct btrfs_dir_item *)((char *)di + len);
528 btrfs_free_path(path);
536 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
539 struct extent_buffer *leaf;
540 struct btrfs_path *path;
541 struct btrfs_file_extent_item *fi;
542 struct btrfs_inode_item *inode_item;
543 struct btrfs_key found_key;
550 path = btrfs_alloc_path();
552 fprintf(stderr, "Ran out of memory\n");
555 path->skip_locking = 1;
557 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
559 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
560 struct btrfs_inode_item);
561 found_size = btrfs_inode_size(path->nodes[0], inode_item);
563 btrfs_release_path(path);
566 key->type = BTRFS_EXTENT_DATA_KEY;
568 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
570 fprintf(stderr, "Error searching %d\n", ret);
571 btrfs_free_path(path);
575 leaf = path->nodes[0];
577 ret = next_leaf(root, path);
579 fprintf(stderr, "Error getting next leaf %d\n",
581 btrfs_free_path(path);
583 } else if (ret > 0) {
584 /* No more leaves to search */
585 btrfs_free_path(path);
588 leaf = path->nodes[0];
592 if (loops++ >= 1024) {
593 ret = ask_to_continue(file);
598 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
600 ret = next_leaf(root, path);
602 fprintf(stderr, "Error searching %d\n", ret);
603 btrfs_free_path(path);
606 /* No more leaves to search */
607 btrfs_free_path(path);
610 leaf = path->nodes[0];
614 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
615 if (found_key.objectid != key->objectid)
617 if (found_key.type != key->type)
619 fi = btrfs_item_ptr(leaf, path->slots[0],
620 struct btrfs_file_extent_item);
621 extent_type = btrfs_file_extent_type(leaf, fi);
622 compression = btrfs_file_extent_compression(leaf, fi);
623 if (compression >= BTRFS_COMPRESS_LAST) {
624 fprintf(stderr, "Don't support compression yet %d\n",
626 btrfs_free_path(path);
630 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
632 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
633 ret = copy_one_inline(fd, path, found_key.offset);
635 btrfs_free_path(path);
638 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
639 ret = copy_one_extent(root, fd, leaf, fi,
642 btrfs_free_path(path);
646 printf("Weird extent type %d\n", extent_type);
652 btrfs_free_path(path);
655 ret = ftruncate(fd, (loff_t)found_size);
660 ret = set_file_xattrs(root, key->objectid, fd, file);
667 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
668 const char *output_rootdir, const char *in_dir,
671 struct btrfs_path *path;
672 struct extent_buffer *leaf;
673 struct btrfs_dir_item *dir_item;
674 struct btrfs_key found_key, location;
675 char filename[BTRFS_NAME_LEN + 1];
676 unsigned long name_ptr;
683 path = btrfs_alloc_path();
685 fprintf(stderr, "Ran out of memory\n");
688 path->skip_locking = 1;
691 key->type = BTRFS_DIR_INDEX_KEY;
693 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
695 fprintf(stderr, "Error searching %d\n", ret);
696 btrfs_free_path(path);
700 leaf = path->nodes[0];
703 printf("No leaf after search, looking for the next "
705 ret = next_leaf(root, path);
707 fprintf(stderr, "Error getting next leaf %d\n",
709 btrfs_free_path(path);
711 } else if (ret > 0) {
712 /* No more leaves to search */
714 printf("Reached the end of the tree looking "
715 "for the directory\n");
716 btrfs_free_path(path);
719 leaf = path->nodes[0];
723 if (loops++ >= 1024) {
724 printf("We have looped trying to restore files in %s "
725 "too many times to be making progress, "
726 "stopping\n", in_dir);
730 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
732 ret = next_leaf(root, path);
734 fprintf(stderr, "Error searching %d\n",
736 btrfs_free_path(path);
738 } else if (ret > 0) {
739 /* No more leaves to search */
741 printf("Reached the end of "
742 "the tree searching the"
744 btrfs_free_path(path);
747 leaf = path->nodes[0];
751 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
752 if (found_key.objectid != key->objectid) {
754 printf("Found objectid=%Lu, key=%Lu\n",
755 found_key.objectid, key->objectid);
758 if (found_key.type != key->type) {
760 printf("Found type=%u, want=%u\n",
761 found_key.type, key->type);
764 dir_item = btrfs_item_ptr(leaf, path->slots[0],
765 struct btrfs_dir_item);
766 name_ptr = (unsigned long)(dir_item + 1);
767 name_len = btrfs_dir_name_len(leaf, dir_item);
768 read_extent_buffer(leaf, filename, name_ptr, name_len);
769 filename[name_len] = '\0';
770 type = btrfs_dir_type(leaf, dir_item);
771 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
773 /* full path from root of btrfs being restored */
774 snprintf(fs_name, 4096, "%s/%s", in_dir, filename);
776 if (mreg && REG_NOMATCH == regexec(mreg, fs_name, 0, NULL, 0))
779 /* full path from system root */
780 snprintf(path_name, 4096, "%s%s", output_rootdir, fs_name);
783 * At this point we're only going to restore directories and
784 * files, no symlinks or anything else.
786 if (type == BTRFS_FT_REG_FILE) {
791 ret = stat(path_name, &st);
794 if (verbose || !warn)
795 printf("Skipping existing file"
799 printf("If you wish to overwrite use "
800 "the -o option to overwrite\n");
807 printf("Restoring %s\n", path_name);
810 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
812 fprintf(stderr, "Error creating %s: %d\n",
816 btrfs_free_path(path);
820 ret = copy_file(root, fd, &location, path_name);
825 btrfs_free_path(path);
828 } else if (type == BTRFS_FT_DIR) {
829 struct btrfs_root *search_root = root;
830 char *dir = strdup(fs_name);
833 fprintf(stderr, "Ran out of memory\n");
834 btrfs_free_path(path);
838 if (location.type == BTRFS_ROOT_ITEM_KEY) {
840 * If we are a snapshot and this is the index
841 * object to ourselves just skip it.
843 if (location.objectid ==
844 root->root_key.objectid) {
849 location.offset = (u64)-1;
850 search_root = btrfs_read_fs_root(root->fs_info,
852 if (IS_ERR(search_root)) {
854 fprintf(stderr, "Error reading "
855 "subvolume %s: %lu\n",
857 PTR_ERR(search_root));
860 btrfs_free_path(path);
861 return PTR_ERR(search_root);
865 * A subvolume will have a key.offset of 0, a
866 * snapshot will have key.offset of a transid.
868 if (search_root->root_key.offset != 0 &&
871 printf("Skipping snapshot %s\n",
875 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
879 printf("Restoring %s\n", path_name);
885 ret = mkdir(path_name, 0755);
886 if (ret && errno != EEXIST) {
888 fprintf(stderr, "Error mkdiring %s: %d\n",
892 btrfs_free_path(path);
896 ret = search_dir(search_root, &location,
897 output_rootdir, dir, mreg);
902 btrfs_free_path(path);
911 printf("Done searching %s\n", in_dir);
912 btrfs_free_path(path);
916 static int do_list_roots(struct btrfs_root *root)
918 struct btrfs_key key;
919 struct btrfs_key found_key;
920 struct btrfs_disk_key disk_key;
921 struct btrfs_path *path;
922 struct extent_buffer *leaf;
923 struct btrfs_root_item ri;
924 unsigned long offset;
928 root = root->fs_info->tree_root;
929 path = btrfs_alloc_path();
931 fprintf(stderr, "Failed to alloc path\n");
937 key.type = BTRFS_ROOT_ITEM_KEY;
939 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
941 fprintf(stderr, "Failed to do search %d\n", ret);
942 btrfs_free_path(path);
947 leaf = path->nodes[0];
948 slot = path->slots[0];
949 if (slot >= btrfs_header_nritems(leaf)) {
950 ret = btrfs_next_leaf(root, path);
953 leaf = path->nodes[0];
954 slot = path->slots[0];
956 btrfs_item_key(leaf, &disk_key, slot);
957 btrfs_disk_key_to_cpu(&found_key, &disk_key);
958 if (btrfs_key_type(&found_key) != BTRFS_ROOT_ITEM_KEY) {
963 offset = btrfs_item_ptr_offset(leaf, slot);
964 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
966 btrfs_print_key(&disk_key);
967 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
968 btrfs_root_level(&ri));
971 btrfs_free_path(path);
976 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
977 int super_mirror, int list_roots)
979 struct btrfs_fs_info *fs_info = NULL;
980 struct btrfs_root *root = NULL;
984 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
985 bytenr = btrfs_sb_offset(i);
986 fs_info = open_ctree_fs_info(dev, bytenr, root_location,
990 fprintf(stderr, "Could not open root, trying backup super\n");
997 * All we really need to succeed is reading the chunk tree, everything
998 * else we can do by hand, since we only need to read the tree root and
1001 if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
1004 root = fs_info->tree_root;
1006 root_location = btrfs_super_root(fs_info->super_copy);
1007 generation = btrfs_super_generation(fs_info->super_copy);
1008 root->node = read_tree_block(root, root_location,
1009 root->leafsize, generation);
1010 if (!extent_buffer_uptodate(root->node)) {
1011 fprintf(stderr, "Error opening tree root\n");
1017 if (!list_roots && !fs_info->fs_root) {
1018 struct btrfs_key key;
1020 key.objectid = BTRFS_FS_TREE_OBJECTID;
1021 key.type = BTRFS_ROOT_ITEM_KEY;
1022 key.offset = (u64)-1;
1023 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
1024 if (IS_ERR(fs_info->fs_root)) {
1025 fprintf(stderr, "Couldn't read fs root: %ld\n",
1026 PTR_ERR(fs_info->fs_root));
1027 close_ctree(fs_info->tree_root);
1032 if (list_roots && do_list_roots(fs_info->tree_root)) {
1033 close_ctree(fs_info->tree_root);
1037 return fs_info->fs_root;
1040 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
1042 struct btrfs_path *path;
1043 struct btrfs_key found_key;
1044 struct btrfs_key key;
1049 key.type = BTRFS_DIR_INDEX_KEY;
1052 path = btrfs_alloc_path();
1054 fprintf(stderr, "Ran out of memory\n");
1058 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1060 fprintf(stderr, "Error searching %d\n", ret);
1064 if (!path->nodes[0]) {
1065 fprintf(stderr, "No leaf!\n");
1069 for (i = path->slots[0];
1070 i < btrfs_header_nritems(path->nodes[0]); i++) {
1071 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
1072 if (found_key.type != key.type)
1075 printf("Using objectid %Lu for first dir\n",
1076 found_key.objectid);
1077 *objectid = found_key.objectid;
1082 ret = next_leaf(root, path);
1084 fprintf(stderr, "Error getting next leaf %d\n",
1087 } else if (ret > 0) {
1088 fprintf(stderr, "No more leaves\n");
1091 } while (!path->nodes[0]);
1094 printf("Couldn't find a dir index item\n");
1096 btrfs_free_path(path);
1100 static struct option long_options[] = {
1101 { "path-regex", 1, NULL, 256},
1102 { "dry-run", 0, NULL, 'D'},
1106 const char * const cmd_restore_usage[] = {
1107 "btrfs restore [options] <device> <path> | -l <device>",
1108 "Try to restore files from a damaged filesystem (unmounted)",
1111 "-x get extended attributes",
1115 "-t <location> tree location",
1116 "-f <offset> filesystem location",
1117 "-u <block> super mirror",
1118 "-r <rootid> root objectid",
1120 "-l list tree roots",
1121 "-D|--dry-run dry run (only list files that would be recovered)",
1122 "--path-regex <regex>",
1123 " restore only filenames matching regex,",
1124 " you have to use following syntax (possibly quoted):",
1125 " ^/(|home(|/username(|/Desktop(|/.*))))$",
1129 int cmd_restore(int argc, char **argv)
1131 struct btrfs_root *root;
1132 struct btrfs_key key;
1134 u64 tree_location = 0;
1135 u64 fs_location = 0;
1136 u64 root_objectid = 0;
1140 int option_index = 0;
1141 int super_mirror = 0;
1144 const char *match_regstr = NULL;
1145 int match_cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
1146 regex_t match_reg, *mreg = NULL;
1149 while ((opt = getopt_long(argc, argv, "sxviot:u:df:r:lDc", long_options,
1150 &option_index)) != -1) {
1166 tree_location = arg_strtou64(optarg);
1169 fs_location = arg_strtou64(optarg);
1172 super_mirror = arg_strtou64(optarg);
1173 if (super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1174 fprintf(stderr, "Super mirror not "
1183 root_objectid = arg_strtou64(optarg);
1192 match_cflags |= REG_ICASE;
1194 /* long option without single letter alternative */
1196 match_regstr = optarg;
1202 usage(cmd_restore_usage);
1206 if (!list_roots && optind + 1 >= argc)
1207 usage(cmd_restore_usage);
1208 else if (list_roots && optind >= argc)
1209 usage(cmd_restore_usage);
1211 if ((ret = check_mounted(argv[optind])) < 0) {
1212 fprintf(stderr, "Could not check mount status: %s\n",
1216 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
1220 root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1227 if (fs_location != 0) {
1228 free_extent_buffer(root->node);
1229 root->node = read_tree_block(root, fs_location, root->leafsize, 0);
1231 fprintf(stderr, "Failed to read fs location\n");
1236 memset(path_name, 0, 4096);
1238 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1239 dir_name[sizeof dir_name - 1] = 0;
1241 /* Strip the trailing / on the dir name */
1242 len = strlen(dir_name);
1243 while (len && dir_name[--len] == '/') {
1244 dir_name[len] = '\0';
1247 if (root_objectid != 0) {
1248 struct btrfs_root *orig_root = root;
1250 key.objectid = root_objectid;
1251 key.type = BTRFS_ROOT_ITEM_KEY;
1252 key.offset = (u64)-1;
1253 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1255 fprintf(stderr, "Error reading root\n");
1265 ret = find_first_dir(root, &key.objectid);
1269 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1273 ret = regcomp(&match_reg, match_regstr, match_cflags);
1275 regerror(ret, &match_reg, reg_err, sizeof(reg_err));
1276 fprintf(stderr, "Regex compile failed: %s\n", reg_err);
1283 printf("This is a dry-run, no files are going to be restored\n");
1285 ret = search_dir(root, &key, dir_name, "", mreg);