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 <attr/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;
58 #define PAGE_CACHE_SIZE 4096
59 #define lzo1x_worst_compress(x) ((x) + ((x) / 16) + 64 + 3)
61 static int decompress_zlib(char *inbuf, char *outbuf, u64 compress_len,
67 memset(&strm, 0, sizeof(strm));
68 ret = inflateInit(&strm);
70 fprintf(stderr, "inflate init returnd %d\n", ret);
74 strm.avail_in = compress_len;
75 strm.next_in = (unsigned char *)inbuf;
76 strm.avail_out = decompress_len;
77 strm.next_out = (unsigned char *)outbuf;
78 ret = inflate(&strm, Z_NO_FLUSH);
79 if (ret != Z_STREAM_END) {
80 (void)inflateEnd(&strm);
81 fprintf(stderr, "failed to inflate: %d\n", ret);
85 (void)inflateEnd(&strm);
88 static inline size_t read_compress_length(unsigned char *buf)
91 memcpy(&dlen, buf, LZO_LEN);
92 return le32_to_cpu(dlen);
95 static int decompress_lzo(unsigned char *inbuf, char *outbuf, u64 compress_len,
106 if (ret != LZO_E_OK) {
107 fprintf(stderr, "lzo init returned %d\n", ret);
111 tot_len = read_compress_length(inbuf);
115 while (tot_in < tot_len) {
116 in_len = read_compress_length(inbuf);
120 new_len = lzo1x_worst_compress(PAGE_CACHE_SIZE);
121 ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len,
122 (unsigned char *)outbuf,
123 (void *)&new_len, NULL);
124 if (ret != LZO_E_OK) {
125 fprintf(stderr, "failed to inflate: %d\n", ret);
134 *decompress_len = out_len;
139 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
140 u64 *decompress_len, int compress)
143 case BTRFS_COMPRESS_ZLIB:
144 return decompress_zlib(inbuf, outbuf, compress_len,
146 case BTRFS_COMPRESS_LZO:
147 return decompress_lzo((unsigned char *)inbuf, outbuf, compress_len,
153 fprintf(stderr, "invalid compression type: %d\n", compress);
157 static int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
162 struct extent_buffer *c;
163 struct extent_buffer *next = NULL;
166 for (; level < BTRFS_MAX_LEVEL; level++) {
167 if (path->nodes[level])
171 if (level == BTRFS_MAX_LEVEL)
174 slot = path->slots[level] + 1;
176 while(level < BTRFS_MAX_LEVEL) {
177 if (!path->nodes[level])
180 slot = path->slots[level] + offset;
181 c = path->nodes[level];
182 if (slot >= btrfs_header_nritems(c)) {
184 if (level == BTRFS_MAX_LEVEL)
190 reada_for_search(root, path, level, slot, 0);
192 next = read_node_slot(root, c, slot);
197 path->slots[level] = slot;
200 c = path->nodes[level];
201 free_extent_buffer(c);
202 path->nodes[level] = next;
203 path->slots[level] = 0;
207 reada_for_search(root, path, level, 0, 0);
208 next = read_node_slot(root, next, 0);
215 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
217 struct extent_buffer *leaf = path->nodes[0];
218 struct btrfs_file_extent_item *fi;
228 fi = btrfs_item_ptr(leaf, path->slots[0],
229 struct btrfs_file_extent_item);
230 ptr = btrfs_file_extent_inline_start(fi);
231 len = btrfs_file_extent_inline_item_len(leaf,
232 btrfs_item_nr(leaf, path->slots[0]));
233 read_extent_buffer(leaf, buf, ptr, len);
235 compress = btrfs_file_extent_compression(leaf, fi);
236 if (compress == BTRFS_COMPRESS_NONE) {
237 done = pwrite(fd, buf, len, pos);
239 fprintf(stderr, "Short inline write, wanted %d, did "
240 "%zd: %d\n", len, done, errno);
246 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
247 outbuf = malloc(ram_size);
249 fprintf(stderr, "No memory\n");
253 ret = decompress(buf, outbuf, len, &ram_size, compress);
259 done = pwrite(fd, outbuf, ram_size, pos);
261 if (done < ram_size) {
262 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
263 "did %zd: %d\n", ram_size, done, errno);
270 static int copy_one_extent(struct btrfs_root *root, int fd,
271 struct extent_buffer *leaf,
272 struct btrfs_file_extent_item *fi, u64 pos)
274 struct btrfs_multi_bio *multi = NULL;
275 struct btrfs_device *device;
276 char *inbuf, *outbuf = NULL;
277 ssize_t done, total = 0;
292 compress = btrfs_file_extent_compression(leaf, fi);
293 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
294 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
295 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
296 offset = btrfs_file_extent_offset(leaf, fi);
297 size_left = disk_size;
300 printf("offset is %Lu\n", offset);
301 /* we found a hole */
305 inbuf = malloc(disk_size);
307 fprintf(stderr, "No memory\n");
311 if (compress != BTRFS_COMPRESS_NONE) {
312 outbuf = malloc(ram_size);
314 fprintf(stderr, "No memory\n");
321 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
322 bytenr, &length, &multi, mirror_num, NULL);
324 fprintf(stderr, "Error mapping block %d\n", ret);
327 device = multi->stripes[0].dev;
330 dev_bytenr = multi->stripes[0].physical;
333 if (size_left < length)
336 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
337 /* Need both checks, or we miss negative values due to u64 conversion */
338 if (done < 0 || done < length) {
339 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
342 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
343 if (mirror_num > num_copies) {
345 fprintf(stderr, "Exhausted mirrors trying to read\n");
348 fprintf(stderr, "Trying another mirror\n");
359 if (compress == BTRFS_COMPRESS_NONE) {
360 while (total < ram_size) {
361 done = pwrite(fd, inbuf+total, ram_size-total,
365 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
374 ret = decompress(inbuf, outbuf, disk_size, &ram_size, compress);
376 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
379 if (mirror_num >= num_copies) {
383 fprintf(stderr, "Trying another mirror\n");
387 while (total < ram_size) {
388 done = pwrite(fd, outbuf+total, ram_size-total, pos+total);
401 static int ask_to_continue(const char *file)
406 printf("We seem to be looping a lot on %s, do you want to keep going "
407 "on ? (y/N): ", file);
409 ret = fgets(buf, 2, stdin);
410 if (*ret == '\n' || tolower(*ret) == 'n')
412 if (tolower(*ret) != 'y') {
413 printf("Please enter either 'y' or 'n': ");
421 static int set_file_xattrs(struct btrfs_root *root, u64 inode,
422 int fd, const char *file_name)
424 struct btrfs_key key;
425 struct btrfs_path *path;
426 struct extent_buffer *leaf;
427 struct btrfs_dir_item *di;
436 key.objectid = inode;
437 key.type = BTRFS_XATTR_ITEM_KEY;
440 path = btrfs_alloc_path();
444 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
448 leaf = path->nodes[0];
450 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
452 ret = next_leaf(root, path);
455 "Error searching for extended attributes: %d\n",
459 /* No more leaves to search */
463 leaf = path->nodes[0];
468 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
469 if (key.type != BTRFS_XATTR_ITEM_KEY || key.objectid != inode)
472 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
473 di = btrfs_item_ptr(leaf, path->slots[0],
474 struct btrfs_dir_item);
476 while (cur < total_len) {
477 len = btrfs_dir_name_len(leaf, di);
478 if (len > name_len) {
480 name = (char *) malloc(len + 1);
486 read_extent_buffer(leaf, name,
487 (unsigned long)(di + 1), len);
491 len = btrfs_dir_data_len(leaf, di);
492 if (len > data_len) {
494 data = (char *) malloc(len);
500 read_extent_buffer(leaf, data,
501 (unsigned long)(di + 1) + name_len,
505 if (fsetxattr(fd, name, data, data_len, 0)) {
509 "Error setting extended attribute %s on file %s: %s\n",
510 name, file_name, strerror(err));
513 len = sizeof(*di) + name_len + data_len;
515 di = (struct btrfs_dir_item *)((char *)di + len);
521 btrfs_free_path(path);
529 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
532 struct extent_buffer *leaf;
533 struct btrfs_path *path;
534 struct btrfs_file_extent_item *fi;
535 struct btrfs_inode_item *inode_item;
536 struct btrfs_key found_key;
543 path = btrfs_alloc_path();
545 fprintf(stderr, "Ran out of memory\n");
548 path->skip_locking = 1;
550 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
552 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
553 struct btrfs_inode_item);
554 found_size = btrfs_inode_size(path->nodes[0], inode_item);
556 btrfs_release_path(path);
559 key->type = BTRFS_EXTENT_DATA_KEY;
561 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
563 fprintf(stderr, "Error searching %d\n", ret);
564 btrfs_free_path(path);
568 leaf = path->nodes[0];
570 ret = next_leaf(root, path);
572 fprintf(stderr, "Error getting next leaf %d\n",
574 btrfs_free_path(path);
576 } else if (ret > 0) {
577 /* No more leaves to search */
578 btrfs_free_path(path);
581 leaf = path->nodes[0];
585 if (loops++ >= 1024) {
586 ret = ask_to_continue(file);
591 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
593 ret = next_leaf(root, path);
595 fprintf(stderr, "Error searching %d\n", ret);
596 btrfs_free_path(path);
599 /* No more leaves to search */
600 btrfs_free_path(path);
603 leaf = path->nodes[0];
607 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
608 if (found_key.objectid != key->objectid)
610 if (found_key.type != key->type)
612 fi = btrfs_item_ptr(leaf, path->slots[0],
613 struct btrfs_file_extent_item);
614 extent_type = btrfs_file_extent_type(leaf, fi);
615 compression = btrfs_file_extent_compression(leaf, fi);
616 if (compression >= BTRFS_COMPRESS_LAST) {
617 fprintf(stderr, "Don't support compression yet %d\n",
619 btrfs_free_path(path);
623 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
625 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
626 ret = copy_one_inline(fd, path, found_key.offset);
628 btrfs_free_path(path);
631 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
632 ret = copy_one_extent(root, fd, leaf, fi,
635 btrfs_free_path(path);
639 printf("Weird extent type %d\n", extent_type);
645 btrfs_free_path(path);
648 ret = ftruncate(fd, (loff_t)found_size);
653 ret = set_file_xattrs(root, key->objectid, fd, file);
660 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
661 const char *output_rootdir, const char *in_dir,
664 struct btrfs_path *path;
665 struct extent_buffer *leaf;
666 struct btrfs_dir_item *dir_item;
667 struct btrfs_key found_key, location;
668 char filename[BTRFS_NAME_LEN + 1];
669 unsigned long name_ptr;
676 path = btrfs_alloc_path();
678 fprintf(stderr, "Ran out of memory\n");
681 path->skip_locking = 1;
684 key->type = BTRFS_DIR_INDEX_KEY;
686 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
688 fprintf(stderr, "Error searching %d\n", ret);
689 btrfs_free_path(path);
693 leaf = path->nodes[0];
696 printf("No leaf after search, looking for the next "
698 ret = next_leaf(root, path);
700 fprintf(stderr, "Error getting next leaf %d\n",
702 btrfs_free_path(path);
704 } else if (ret > 0) {
705 /* No more leaves to search */
707 printf("Reached the end of the tree looking "
708 "for the directory\n");
709 btrfs_free_path(path);
712 leaf = path->nodes[0];
716 if (loops++ >= 1024) {
717 printf("We have looped trying to restore files in %s "
718 "too many times to be making progress, "
719 "stopping\n", in_dir);
723 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
725 ret = next_leaf(root, path);
727 fprintf(stderr, "Error searching %d\n",
729 btrfs_free_path(path);
731 } else if (ret > 0) {
732 /* No more leaves to search */
734 printf("Reached the end of "
735 "the tree searching the"
737 btrfs_free_path(path);
740 leaf = path->nodes[0];
744 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
745 if (found_key.objectid != key->objectid) {
747 printf("Found objectid=%Lu, key=%Lu\n",
748 found_key.objectid, key->objectid);
751 if (found_key.type != key->type) {
753 printf("Found type=%u, want=%u\n",
754 found_key.type, key->type);
757 dir_item = btrfs_item_ptr(leaf, path->slots[0],
758 struct btrfs_dir_item);
759 name_ptr = (unsigned long)(dir_item + 1);
760 name_len = btrfs_dir_name_len(leaf, dir_item);
761 read_extent_buffer(leaf, filename, name_ptr, name_len);
762 filename[name_len] = '\0';
763 type = btrfs_dir_type(leaf, dir_item);
764 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
766 /* full path from root of btrfs being restored */
767 snprintf(fs_name, 4096, "%s/%s", in_dir, filename);
769 if (mreg && REG_NOMATCH == regexec(mreg, fs_name, 0, NULL, 0))
772 /* full path from system root */
773 snprintf(path_name, 4096, "%s%s", output_rootdir, fs_name);
776 * At this point we're only going to restore directories and
777 * files, no symlinks or anything else.
779 if (type == BTRFS_FT_REG_FILE) {
784 ret = stat(path_name, &st);
787 if (verbose || !warn)
788 printf("Skipping existing file"
792 printf("If you wish to overwrite use "
793 "the -o option to overwrite\n");
800 printf("Restoring %s\n", path_name);
801 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
803 fprintf(stderr, "Error creating %s: %d\n",
807 btrfs_free_path(path);
811 ret = copy_file(root, fd, &location, path_name);
816 btrfs_free_path(path);
819 } else if (type == BTRFS_FT_DIR) {
820 struct btrfs_root *search_root = root;
821 char *dir = strdup(fs_name);
824 fprintf(stderr, "Ran out of memory\n");
825 btrfs_free_path(path);
829 if (location.type == BTRFS_ROOT_ITEM_KEY) {
831 * If we are a snapshot and this is the index
832 * object to ourselves just skip it.
834 if (location.objectid ==
835 root->root_key.objectid) {
840 location.offset = (u64)-1;
841 search_root = btrfs_read_fs_root(root->fs_info,
843 if (IS_ERR(search_root)) {
845 fprintf(stderr, "Error reading "
846 "subvolume %s: %lu\n",
848 PTR_ERR(search_root));
851 btrfs_free_path(path);
852 return PTR_ERR(search_root);
856 * A subvolume will have a key.offset of 0, a
857 * snapshot will have key.offset of a transid.
859 if (search_root->root_key.offset != 0 &&
862 printf("Skipping snapshot %s\n",
866 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
870 printf("Restoring %s\n", path_name);
873 ret = mkdir(path_name, 0755);
874 if (ret && errno != EEXIST) {
876 fprintf(stderr, "Error mkdiring %s: %d\n",
880 btrfs_free_path(path);
884 ret = search_dir(search_root, &location,
885 output_rootdir, dir, mreg);
890 btrfs_free_path(path);
899 printf("Done searching %s\n", in_dir);
900 btrfs_free_path(path);
904 static int do_list_roots(struct btrfs_root *root)
906 struct btrfs_key key;
907 struct btrfs_key found_key;
908 struct btrfs_disk_key disk_key;
909 struct btrfs_path *path;
910 struct extent_buffer *leaf;
911 struct btrfs_root_item ri;
912 unsigned long offset;
916 root = root->fs_info->tree_root;
917 path = btrfs_alloc_path();
919 fprintf(stderr, "Failed to alloc path\n");
925 key.type = BTRFS_ROOT_ITEM_KEY;
927 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
929 fprintf(stderr, "Failed to do search %d\n", ret);
930 btrfs_free_path(path);
935 leaf = path->nodes[0];
936 slot = path->slots[0];
937 if (slot >= btrfs_header_nritems(leaf)) {
938 ret = btrfs_next_leaf(root, path);
941 leaf = path->nodes[0];
942 slot = path->slots[0];
944 btrfs_item_key(leaf, &disk_key, slot);
945 btrfs_disk_key_to_cpu(&found_key, &disk_key);
946 if (btrfs_key_type(&found_key) != BTRFS_ROOT_ITEM_KEY) {
951 offset = btrfs_item_ptr_offset(leaf, slot);
952 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
954 btrfs_print_key(&disk_key);
955 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
956 btrfs_root_level(&ri));
959 btrfs_free_path(path);
964 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
965 int super_mirror, int list_roots)
967 struct btrfs_fs_info *fs_info = NULL;
968 struct btrfs_root *root = NULL;
972 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
973 bytenr = btrfs_sb_offset(i);
974 fs_info = open_ctree_fs_info(dev, bytenr, root_location, 0, 1);
977 fprintf(stderr, "Could not open root, trying backup super\n");
984 * All we really need to succeed is reading the chunk tree, everything
985 * else we can do by hand, since we only need to read the tree root and
988 if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
991 root = fs_info->tree_root;
993 root_location = btrfs_super_root(fs_info->super_copy);
994 generation = btrfs_super_generation(fs_info->super_copy);
995 root->node = read_tree_block(root, root_location,
996 root->leafsize, generation);
997 if (!extent_buffer_uptodate(root->node)) {
998 fprintf(stderr, "Error opening tree root\n");
1004 if (!list_roots && !fs_info->fs_root) {
1005 struct btrfs_key key;
1007 key.objectid = BTRFS_FS_TREE_OBJECTID;
1008 key.type = BTRFS_ROOT_ITEM_KEY;
1009 key.offset = (u64)-1;
1010 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
1011 if (IS_ERR(fs_info->fs_root)) {
1012 fprintf(stderr, "Couldn't read fs root: %ld\n",
1013 PTR_ERR(fs_info->fs_root));
1014 close_ctree(fs_info->tree_root);
1019 if (list_roots && do_list_roots(fs_info->tree_root)) {
1020 close_ctree(fs_info->tree_root);
1024 return fs_info->fs_root;
1027 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
1029 struct btrfs_path *path;
1030 struct btrfs_key found_key;
1031 struct btrfs_key key;
1036 key.type = BTRFS_DIR_INDEX_KEY;
1039 path = btrfs_alloc_path();
1041 fprintf(stderr, "Ran out of memory\n");
1045 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1047 fprintf(stderr, "Error searching %d\n", ret);
1051 if (!path->nodes[0]) {
1052 fprintf(stderr, "No leaf!\n");
1056 for (i = path->slots[0];
1057 i < btrfs_header_nritems(path->nodes[0]); i++) {
1058 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
1059 if (found_key.type != key.type)
1062 printf("Using objectid %Lu for first dir\n",
1063 found_key.objectid);
1064 *objectid = found_key.objectid;
1069 ret = next_leaf(root, path);
1071 fprintf(stderr, "Error getting next leaf %d\n",
1074 } else if (ret > 0) {
1075 fprintf(stderr, "No more leaves\n");
1078 } while (!path->nodes[0]);
1081 printf("Couldn't find a dir index item\n");
1083 btrfs_free_path(path);
1087 static struct option long_options[] = {
1088 { "path-regex", 1, NULL, 256},
1092 const char * const cmd_restore_usage[] = {
1093 "btrfs restore [options] <device> <path> | -l <device>",
1094 "Try to restore files from a damaged filesystem (unmounted)",
1097 "-x get extended attributes",
1101 "-t <location> tree location",
1102 "-f <offset> filesystem location",
1103 "-u <block> super mirror",
1104 "-r <rootid> root objectid",
1106 "-l list tree roots",
1107 "--path-regex <regex>",
1108 " restore only filenames matching regex,",
1109 " you have to use following syntax (possibly quoted):",
1110 " ^/(|home(|/username(|/Desktop(|/.*))))$",
1114 int cmd_restore(int argc, char **argv)
1116 struct btrfs_root *root;
1117 struct btrfs_key key;
1119 u64 tree_location = 0;
1120 u64 fs_location = 0;
1121 u64 root_objectid = 0;
1125 int option_index = 0;
1126 int super_mirror = 0;
1129 const char *match_regstr = NULL;
1130 int match_cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
1131 regex_t match_reg, *mreg = NULL;
1134 while ((opt = getopt_long(argc, argv, "sxviot:u:df:r:lc", long_options,
1135 &option_index)) != -1) {
1152 tree_location = (u64)strtoll(optarg, NULL, 10);
1154 fprintf(stderr, "Tree location not valid\n");
1160 fs_location = (u64)strtoll(optarg, NULL, 10);
1162 fprintf(stderr, "Fs location not valid\n");
1168 super_mirror = (int)strtol(optarg, NULL, 10);
1170 super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1171 fprintf(stderr, "Super mirror not "
1181 root_objectid = (u64)strtoll(optarg, NULL, 10);
1183 fprintf(stderr, "Root objectid not valid\n");
1191 match_cflags |= REG_ICASE;
1193 /* long option without single letter alternative */
1195 match_regstr = optarg;
1201 usage(cmd_restore_usage);
1205 if (!list_roots && optind + 1 >= argc)
1206 usage(cmd_restore_usage);
1207 else if (list_roots && optind >= argc)
1208 usage(cmd_restore_usage);
1210 if ((ret = check_mounted(argv[optind])) < 0) {
1211 fprintf(stderr, "Could not check mount status: %s\n",
1215 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
1219 root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1226 if (fs_location != 0) {
1227 free_extent_buffer(root->node);
1228 root->node = read_tree_block(root, fs_location, root->leafsize, 0);
1230 fprintf(stderr, "Failed to read fs location\n");
1235 memset(path_name, 0, 4096);
1237 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1238 dir_name[sizeof dir_name - 1] = 0;
1240 /* Strip the trailing / on the dir name */
1241 len = strlen(dir_name);
1242 while (len && dir_name[--len] == '/') {
1243 dir_name[len] = '\0';
1246 if (root_objectid != 0) {
1247 struct btrfs_root *orig_root = root;
1249 key.objectid = root_objectid;
1250 key.type = BTRFS_ROOT_ITEM_KEY;
1251 key.offset = (u64)-1;
1252 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1254 fprintf(stderr, "Error reading root\n");
1264 ret = find_first_dir(root, &key.objectid);
1268 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1272 ret = regcomp(&match_reg, match_regstr, match_cflags);
1274 regerror(ret, &match_reg, reg_err, sizeof(reg_err));
1275 fprintf(stderr, "Regex compile failed: %s\n", reg_err);
1281 ret = search_dir(root, &key, dir_name, "", mreg);