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"
47 static char fs_name[4096];
48 static char path_name[4096];
49 static int get_snaps = 0;
50 static int verbose = 0;
51 static int ignore_errors = 0;
52 static int overwrite = 0;
53 static int get_xattrs = 0;
54 static int dry_run = 0;
57 #define PAGE_CACHE_SIZE 4096
58 #define lzo1x_worst_compress(x) ((x) + ((x) / 16) + 64 + 3)
60 static int decompress_zlib(char *inbuf, char *outbuf, u64 compress_len,
66 memset(&strm, 0, sizeof(strm));
67 ret = inflateInit(&strm);
69 fprintf(stderr, "inflate init returnd %d\n", ret);
73 strm.avail_in = compress_len;
74 strm.next_in = (unsigned char *)inbuf;
75 strm.avail_out = decompress_len;
76 strm.next_out = (unsigned char *)outbuf;
77 ret = inflate(&strm, Z_NO_FLUSH);
78 if (ret != Z_STREAM_END) {
79 (void)inflateEnd(&strm);
80 fprintf(stderr, "failed to inflate: %d\n", ret);
84 (void)inflateEnd(&strm);
87 static inline size_t read_compress_length(unsigned char *buf)
90 memcpy(&dlen, buf, LZO_LEN);
91 return le32_to_cpu(dlen);
94 static int decompress_lzo(unsigned char *inbuf, char *outbuf, u64 compress_len,
105 if (ret != LZO_E_OK) {
106 fprintf(stderr, "lzo init returned %d\n", ret);
110 tot_len = read_compress_length(inbuf);
114 while (tot_in < tot_len) {
115 in_len = read_compress_length(inbuf);
117 if ((tot_in + LZO_LEN + in_len) > tot_len) {
118 fprintf(stderr, "bad compress length %lu\n",
119 (unsigned long)in_len);
126 new_len = lzo1x_worst_compress(PAGE_CACHE_SIZE);
127 ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len,
128 (unsigned char *)outbuf,
129 (void *)&new_len, NULL);
130 if (ret != LZO_E_OK) {
131 fprintf(stderr, "failed to inflate: %d\n", ret);
140 *decompress_len = out_len;
145 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
146 u64 *decompress_len, int compress)
149 case BTRFS_COMPRESS_ZLIB:
150 return decompress_zlib(inbuf, outbuf, compress_len,
152 case BTRFS_COMPRESS_LZO:
153 return decompress_lzo((unsigned char *)inbuf, outbuf, compress_len,
159 fprintf(stderr, "invalid compression type: %d\n", compress);
163 static int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
168 struct extent_buffer *c;
169 struct extent_buffer *next = NULL;
172 for (; level < BTRFS_MAX_LEVEL; level++) {
173 if (path->nodes[level])
177 if (level >= BTRFS_MAX_LEVEL)
180 slot = path->slots[level] + 1;
182 while(level < BTRFS_MAX_LEVEL) {
183 if (!path->nodes[level])
186 slot = path->slots[level] + offset;
187 c = path->nodes[level];
188 if (slot >= btrfs_header_nritems(c)) {
190 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;
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 inline_item_len = btrfs_file_extent_inline_item_len(leaf, btrfs_item_nr(path->slots[0]));
241 read_extent_buffer(leaf, buf, ptr, inline_item_len);
243 compress = btrfs_file_extent_compression(leaf, fi);
244 if (compress == BTRFS_COMPRESS_NONE) {
245 done = pwrite(fd, buf, len, pos);
247 fprintf(stderr, "Short inline write, wanted %d, did "
248 "%zd: %d\n", len, done, errno);
254 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
255 outbuf = calloc(1, ram_size);
257 fprintf(stderr, "No memory\n");
261 ret = decompress(buf, outbuf, len, &ram_size, compress);
267 done = pwrite(fd, outbuf, ram_size, pos);
269 if (done < ram_size) {
270 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
271 "did %zd: %d\n", ram_size, done, errno);
278 static int copy_one_extent(struct btrfs_root *root, int fd,
279 struct extent_buffer *leaf,
280 struct btrfs_file_extent_item *fi, u64 pos)
282 struct btrfs_multi_bio *multi = NULL;
283 struct btrfs_device *device;
284 char *inbuf, *outbuf = NULL;
285 ssize_t done, total = 0;
301 compress = btrfs_file_extent_compression(leaf, fi);
302 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
303 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
304 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
305 offset = btrfs_file_extent_offset(leaf, fi);
306 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
307 size_left = disk_size;
308 if (compress == BTRFS_COMPRESS_NONE)
311 if (verbose && offset)
312 printf("offset is %Lu\n", offset);
313 /* we found a hole */
317 inbuf = malloc(size_left);
319 fprintf(stderr, "No memory\n");
323 if (compress != BTRFS_COMPRESS_NONE) {
324 outbuf = calloc(1, ram_size);
326 fprintf(stderr, "No memory\n");
333 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
334 bytenr, &length, &multi, mirror_num, NULL);
336 fprintf(stderr, "Error mapping block %d\n", ret);
339 device = multi->stripes[0].dev;
342 dev_bytenr = multi->stripes[0].physical;
345 if (size_left < length)
348 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
349 /* Need both checks, or we miss negative values due to u64 conversion */
350 if (done < 0 || done < length) {
351 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
354 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
355 if (mirror_num > num_copies) {
357 fprintf(stderr, "Exhausted mirrors trying to read\n");
360 fprintf(stderr, "Trying another mirror\n");
371 if (compress == BTRFS_COMPRESS_NONE) {
372 while (total < num_bytes) {
373 done = pwrite(fd, inbuf+total, num_bytes-total,
377 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
386 ret = decompress(inbuf, outbuf, disk_size, &ram_size, compress);
388 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
391 if (mirror_num >= num_copies) {
395 fprintf(stderr, "Trying another mirror\n");
399 while (total < num_bytes) {
400 done = pwrite(fd, outbuf + offset + total,
421 static enum loop_response ask_to_continue(const char *file)
426 printf("We seem to be looping a lot on %s, do you want to keep going "
427 "on ? (y/N/a): ", file);
429 ret = fgets(buf, 2, stdin);
430 if (*ret == '\n' || tolower(*ret) == 'n')
432 if (tolower(*ret) == 'a')
434 if (tolower(*ret) != 'y') {
435 printf("Please enter one of 'y', 'n', or 'a': ");
439 return LOOP_CONTINUE;
443 static int set_file_xattrs(struct btrfs_root *root, u64 inode,
444 int fd, const char *file_name)
446 struct btrfs_key key;
447 struct btrfs_path *path;
448 struct extent_buffer *leaf;
449 struct btrfs_dir_item *di;
458 key.objectid = inode;
459 key.type = BTRFS_XATTR_ITEM_KEY;
462 path = btrfs_alloc_path();
466 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
470 leaf = path->nodes[0];
472 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
474 ret = next_leaf(root, path);
477 "Error searching for extended attributes: %d\n",
481 /* No more leaves to search */
485 leaf = path->nodes[0];
490 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
491 if (key.type != BTRFS_XATTR_ITEM_KEY || key.objectid != inode)
494 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
495 di = btrfs_item_ptr(leaf, path->slots[0],
496 struct btrfs_dir_item);
498 while (cur < total_len) {
499 len = btrfs_dir_name_len(leaf, di);
500 if (len > name_len) {
502 name = (char *) malloc(len + 1);
508 read_extent_buffer(leaf, name,
509 (unsigned long)(di + 1), len);
513 len = btrfs_dir_data_len(leaf, di);
514 if (len > data_len) {
516 data = (char *) malloc(len);
522 read_extent_buffer(leaf, data,
523 (unsigned long)(di + 1) + name_len,
527 if (fsetxattr(fd, name, data, data_len, 0)) {
531 "Error setting extended attribute %s on file %s: %s\n",
532 name, file_name, strerror(err));
535 len = sizeof(*di) + name_len + data_len;
537 di = (struct btrfs_dir_item *)((char *)di + len);
543 btrfs_free_path(path);
551 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
554 struct extent_buffer *leaf;
555 struct btrfs_path *path;
556 struct btrfs_file_extent_item *fi;
557 struct btrfs_inode_item *inode_item;
558 struct btrfs_key found_key;
565 path = btrfs_alloc_path();
567 fprintf(stderr, "Ran out of memory\n");
571 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
573 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
574 struct btrfs_inode_item);
575 found_size = btrfs_inode_size(path->nodes[0], inode_item);
577 btrfs_release_path(path);
580 key->type = BTRFS_EXTENT_DATA_KEY;
582 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
584 fprintf(stderr, "Error searching %d\n", ret);
585 btrfs_free_path(path);
589 leaf = path->nodes[0];
591 ret = next_leaf(root, path);
593 fprintf(stderr, "Error getting next leaf %d\n",
595 btrfs_free_path(path);
597 } else if (ret > 0) {
598 /* No more leaves to search */
599 btrfs_free_path(path);
602 leaf = path->nodes[0];
606 if (loops >= 0 && loops++ >= 1024) {
607 enum loop_response resp;
609 resp = ask_to_continue(file);
610 if (resp == LOOP_STOP)
612 else if (resp == LOOP_CONTINUE)
614 else if (resp == LOOP_DONTASK)
617 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
619 ret = next_leaf(root, path);
621 fprintf(stderr, "Error searching %d\n", ret);
622 btrfs_free_path(path);
625 /* No more leaves to search */
626 btrfs_free_path(path);
629 leaf = path->nodes[0];
633 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
634 if (found_key.objectid != key->objectid)
636 if (found_key.type != key->type)
638 fi = btrfs_item_ptr(leaf, path->slots[0],
639 struct btrfs_file_extent_item);
640 extent_type = btrfs_file_extent_type(leaf, fi);
641 compression = btrfs_file_extent_compression(leaf, fi);
642 if (compression >= BTRFS_COMPRESS_LAST) {
643 fprintf(stderr, "Don't support compression yet %d\n",
645 btrfs_free_path(path);
649 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
651 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
652 ret = copy_one_inline(fd, path, found_key.offset);
654 btrfs_free_path(path);
657 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
658 ret = copy_one_extent(root, fd, leaf, fi,
661 btrfs_free_path(path);
665 printf("Weird extent type %d\n", extent_type);
671 btrfs_free_path(path);
674 ret = ftruncate(fd, (loff_t)found_size);
679 ret = set_file_xattrs(root, key->objectid, fd, file);
686 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
687 const char *output_rootdir, const char *in_dir,
690 struct btrfs_path *path;
691 struct extent_buffer *leaf;
692 struct btrfs_dir_item *dir_item;
693 struct btrfs_key found_key, location;
694 char filename[BTRFS_NAME_LEN + 1];
695 unsigned long name_ptr;
702 path = btrfs_alloc_path();
704 fprintf(stderr, "Ran out of memory\n");
709 key->type = BTRFS_DIR_INDEX_KEY;
711 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
713 fprintf(stderr, "Error searching %d\n", ret);
714 btrfs_free_path(path);
718 leaf = path->nodes[0];
721 printf("No leaf after search, looking for the next "
723 ret = next_leaf(root, path);
725 fprintf(stderr, "Error getting next leaf %d\n",
727 btrfs_free_path(path);
729 } else if (ret > 0) {
730 /* No more leaves to search */
732 printf("Reached the end of the tree looking "
733 "for the directory\n");
734 btrfs_free_path(path);
737 leaf = path->nodes[0];
741 if (loops++ >= 1024) {
742 printf("We have looped trying to restore files in %s "
743 "too many times to be making progress, "
744 "stopping\n", in_dir);
748 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
750 ret = next_leaf(root, path);
752 fprintf(stderr, "Error searching %d\n",
754 btrfs_free_path(path);
756 } else if (ret > 0) {
757 /* No more leaves to search */
759 printf("Reached the end of "
760 "the tree searching the"
762 btrfs_free_path(path);
765 leaf = path->nodes[0];
769 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
770 if (found_key.objectid != key->objectid) {
772 printf("Found objectid=%Lu, key=%Lu\n",
773 found_key.objectid, key->objectid);
776 if (found_key.type != key->type) {
778 printf("Found type=%u, want=%u\n",
779 found_key.type, key->type);
782 dir_item = btrfs_item_ptr(leaf, path->slots[0],
783 struct btrfs_dir_item);
784 name_ptr = (unsigned long)(dir_item + 1);
785 name_len = btrfs_dir_name_len(leaf, dir_item);
786 read_extent_buffer(leaf, filename, name_ptr, name_len);
787 filename[name_len] = '\0';
788 type = btrfs_dir_type(leaf, dir_item);
789 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
791 /* full path from root of btrfs being restored */
792 snprintf(fs_name, 4096, "%s/%s", in_dir, filename);
794 if (mreg && REG_NOMATCH == regexec(mreg, fs_name, 0, NULL, 0))
797 /* full path from system root */
798 snprintf(path_name, 4096, "%s%s", output_rootdir, fs_name);
801 * At this point we're only going to restore directories and
802 * files, no symlinks or anything else.
804 if (type == BTRFS_FT_REG_FILE) {
809 ret = stat(path_name, &st);
812 if (verbose || !warn)
813 printf("Skipping existing file"
817 printf("If you wish to overwrite use "
818 "the -o option to overwrite\n");
825 printf("Restoring %s\n", path_name);
828 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
830 fprintf(stderr, "Error creating %s: %d\n",
834 btrfs_free_path(path);
838 ret = copy_file(root, fd, &location, path_name);
841 fprintf(stderr, "Error copying data for %s\n",
845 btrfs_free_path(path);
848 } else if (type == BTRFS_FT_DIR) {
849 struct btrfs_root *search_root = root;
850 char *dir = strdup(fs_name);
853 fprintf(stderr, "Ran out of memory\n");
854 btrfs_free_path(path);
858 if (location.type == BTRFS_ROOT_ITEM_KEY) {
860 * If we are a snapshot and this is the index
861 * object to ourselves just skip it.
863 if (location.objectid ==
864 root->root_key.objectid) {
869 location.offset = (u64)-1;
870 search_root = btrfs_read_fs_root(root->fs_info,
872 if (IS_ERR(search_root)) {
874 fprintf(stderr, "Error reading "
875 "subvolume %s: %lu\n",
877 PTR_ERR(search_root));
880 btrfs_free_path(path);
881 return PTR_ERR(search_root);
885 * A subvolume will have a key.offset of 0, a
886 * snapshot will have key.offset of a transid.
888 if (search_root->root_key.offset != 0 &&
891 printf("Skipping snapshot %s\n",
895 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
899 printf("Restoring %s\n", path_name);
905 ret = mkdir(path_name, 0755);
906 if (ret && errno != EEXIST) {
908 fprintf(stderr, "Error mkdiring %s: %d\n",
912 btrfs_free_path(path);
916 ret = search_dir(search_root, &location,
917 output_rootdir, dir, mreg);
920 fprintf(stderr, "Error searching %s\n",
924 btrfs_free_path(path);
933 printf("Done searching %s\n", in_dir);
934 btrfs_free_path(path);
938 static int do_list_roots(struct btrfs_root *root)
940 struct btrfs_key key;
941 struct btrfs_key found_key;
942 struct btrfs_disk_key disk_key;
943 struct btrfs_path *path;
944 struct extent_buffer *leaf;
945 struct btrfs_root_item ri;
946 unsigned long offset;
950 root = root->fs_info->tree_root;
951 path = btrfs_alloc_path();
953 fprintf(stderr, "Failed to alloc path\n");
959 key.type = BTRFS_ROOT_ITEM_KEY;
961 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
963 fprintf(stderr, "Failed to do search %d\n", ret);
964 btrfs_free_path(path);
968 leaf = path->nodes[0];
971 slot = path->slots[0];
972 if (slot >= btrfs_header_nritems(leaf)) {
973 ret = btrfs_next_leaf(root, path);
976 leaf = path->nodes[0];
977 slot = path->slots[0];
979 btrfs_item_key(leaf, &disk_key, slot);
980 btrfs_disk_key_to_cpu(&found_key, &disk_key);
981 if (btrfs_key_type(&found_key) != BTRFS_ROOT_ITEM_KEY) {
986 offset = btrfs_item_ptr_offset(leaf, slot);
987 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
989 btrfs_print_key(&disk_key);
990 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
991 btrfs_root_level(&ri));
994 btrfs_free_path(path);
999 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
1000 int super_mirror, int list_roots)
1002 struct btrfs_fs_info *fs_info = NULL;
1003 struct btrfs_root *root = NULL;
1007 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1008 bytenr = btrfs_sb_offset(i);
1009 fs_info = open_ctree_fs_info(dev, bytenr, root_location,
1010 OPEN_CTREE_PARTIAL);
1013 fprintf(stderr, "Could not open root, trying backup super\n");
1020 * All we really need to succeed is reading the chunk tree, everything
1021 * else we can do by hand, since we only need to read the tree root and
1024 if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
1027 root = fs_info->tree_root;
1029 root_location = btrfs_super_root(fs_info->super_copy);
1030 generation = btrfs_super_generation(fs_info->super_copy);
1031 root->node = read_tree_block(root, root_location,
1032 root->leafsize, generation);
1033 if (!extent_buffer_uptodate(root->node)) {
1034 fprintf(stderr, "Error opening tree root\n");
1040 if (!list_roots && !fs_info->fs_root) {
1041 struct btrfs_key key;
1043 key.objectid = BTRFS_FS_TREE_OBJECTID;
1044 key.type = BTRFS_ROOT_ITEM_KEY;
1045 key.offset = (u64)-1;
1046 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
1047 if (IS_ERR(fs_info->fs_root)) {
1048 fprintf(stderr, "Couldn't read fs root: %ld\n",
1049 PTR_ERR(fs_info->fs_root));
1050 close_ctree(fs_info->tree_root);
1055 if (list_roots && do_list_roots(fs_info->tree_root)) {
1056 close_ctree(fs_info->tree_root);
1060 return fs_info->fs_root;
1063 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
1065 struct btrfs_path *path;
1066 struct btrfs_key found_key;
1067 struct btrfs_key key;
1072 key.type = BTRFS_DIR_INDEX_KEY;
1075 path = btrfs_alloc_path();
1077 fprintf(stderr, "Ran out of memory\n");
1081 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1083 fprintf(stderr, "Error searching %d\n", ret);
1087 if (!path->nodes[0]) {
1088 fprintf(stderr, "No leaf!\n");
1092 for (i = path->slots[0];
1093 i < btrfs_header_nritems(path->nodes[0]); i++) {
1094 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
1095 if (found_key.type != key.type)
1098 printf("Using objectid %Lu for first dir\n",
1099 found_key.objectid);
1100 *objectid = found_key.objectid;
1105 ret = next_leaf(root, path);
1107 fprintf(stderr, "Error getting next leaf %d\n",
1110 } else if (ret > 0) {
1111 fprintf(stderr, "No more leaves\n");
1114 } while (!path->nodes[0]);
1117 printf("Couldn't find a dir index item\n");
1119 btrfs_free_path(path);
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;
1157 int super_mirror = 0;
1160 const char *match_regstr = NULL;
1161 int match_cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
1162 regex_t match_reg, *mreg = NULL;
1167 int option_index = 0;
1168 static const struct option long_options[] = {
1169 { "path-regex", 1, NULL, 256},
1170 { "dry-run", 0, NULL, 'D'},
1174 opt = getopt_long(argc, argv, "sxviot:u:df:r:lDc", long_options,
1193 tree_location = arg_strtou64(optarg);
1196 fs_location = arg_strtou64(optarg);
1199 super_mirror = arg_strtou64(optarg);
1200 if (super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1201 fprintf(stderr, "Super mirror not "
1210 root_objectid = arg_strtou64(optarg);
1211 if (!is_fstree(root_objectid)) {
1212 fprintf(stderr, "objectid %llu is not a valid fs/file tree\n",
1224 match_cflags |= REG_ICASE;
1226 /* long option without single letter alternative */
1228 match_regstr = optarg;
1234 usage(cmd_restore_usage);
1238 if (!list_roots && check_argc_min(argc - optind, 2))
1239 usage(cmd_restore_usage);
1240 else if (list_roots && check_argc_min(argc - optind, 1))
1241 usage(cmd_restore_usage);
1243 if (fs_location && root_objectid) {
1244 fprintf(stderr, "don't use -f and -r at the same time.\n");
1248 if ((ret = check_mounted(argv[optind])) < 0) {
1249 fprintf(stderr, "Could not check mount status: %s\n",
1253 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
1257 root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1264 if (fs_location != 0) {
1265 free_extent_buffer(root->node);
1266 root->node = read_tree_block(root, fs_location, root->leafsize, 0);
1268 fprintf(stderr, "Failed to read fs location\n");
1274 memset(path_name, 0, 4096);
1276 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1277 dir_name[sizeof dir_name - 1] = 0;
1279 /* Strip the trailing / on the dir name */
1280 len = strlen(dir_name);
1281 while (len && dir_name[--len] == '/') {
1282 dir_name[len] = '\0';
1285 if (root_objectid != 0) {
1286 struct btrfs_root *orig_root = root;
1288 key.objectid = root_objectid;
1289 key.type = BTRFS_ROOT_ITEM_KEY;
1290 key.offset = (u64)-1;
1291 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1293 fprintf(stderr, "fail to read root %llu: %s\n",
1294 root_objectid, strerror(-PTR_ERR(root)));
1304 ret = find_first_dir(root, &key.objectid);
1308 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1312 ret = regcomp(&match_reg, match_regstr, match_cflags);
1314 regerror(ret, &match_reg, reg_err, sizeof(reg_err));
1315 fprintf(stderr, "Regex compile failed: %s\n", reg_err);
1322 printf("This is a dry-run, no files are going to be restored\n");
1324 ret = search_dir(root, &key, dir_name, "", mreg);