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
)
199 reada_for_search(root
, path
, level
, slot
, 0);
201 next
= read_node_slot(root
, c
, slot
);
206 path
->slots
[level
] = slot
;
209 c
= path
->nodes
[level
];
210 free_extent_buffer(c
);
211 path
->nodes
[level
] = next
;
212 path
->slots
[level
] = 0;
216 reada_for_search(root
, path
, level
, 0, 0);
217 next
= read_node_slot(root
, next
, 0);
224 static int copy_one_inline(int fd
, struct btrfs_path
*path
, u64 pos
)
226 struct extent_buffer
*leaf
= path
->nodes
[0];
227 struct btrfs_file_extent_item
*fi
;
238 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
239 struct btrfs_file_extent_item
);
240 ptr
= btrfs_file_extent_inline_start(fi
);
241 len
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], fi
);
242 inline_item_len
= btrfs_file_extent_inline_item_len(leaf
, btrfs_item_nr(path
->slots
[0]));
243 read_extent_buffer(leaf
, buf
, ptr
, inline_item_len
);
245 compress
= btrfs_file_extent_compression(leaf
, fi
);
246 if (compress
== BTRFS_COMPRESS_NONE
) {
247 done
= pwrite(fd
, buf
, len
, pos
);
249 fprintf(stderr
, "Short inline write, wanted %d, did "
250 "%zd: %d\n", len
, done
, errno
);
256 ram_size
= btrfs_file_extent_ram_bytes(leaf
, fi
);
257 outbuf
= calloc(1, ram_size
);
259 fprintf(stderr
, "No memory\n");
263 ret
= decompress(buf
, outbuf
, len
, &ram_size
, compress
);
269 done
= pwrite(fd
, outbuf
, ram_size
, pos
);
271 if (done
< ram_size
) {
272 fprintf(stderr
, "Short compressed inline write, wanted %Lu, "
273 "did %zd: %d\n", ram_size
, done
, errno
);
280 static int copy_one_extent(struct btrfs_root
*root
, int fd
,
281 struct extent_buffer
*leaf
,
282 struct btrfs_file_extent_item
*fi
, u64 pos
)
284 struct btrfs_multi_bio
*multi
= NULL
;
285 struct btrfs_device
*device
;
286 char *inbuf
, *outbuf
= NULL
;
287 ssize_t done
, total
= 0;
303 compress
= btrfs_file_extent_compression(leaf
, fi
);
304 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
305 disk_size
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
306 ram_size
= btrfs_file_extent_ram_bytes(leaf
, fi
);
307 offset
= btrfs_file_extent_offset(leaf
, fi
);
308 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
309 size_left
= disk_size
;
310 if (compress
== BTRFS_COMPRESS_NONE
)
313 if (verbose
&& offset
)
314 printf("offset is %Lu\n", offset
);
315 /* we found a hole */
319 inbuf
= malloc(size_left
);
321 fprintf(stderr
, "No memory\n");
325 if (compress
!= BTRFS_COMPRESS_NONE
) {
326 outbuf
= calloc(1, ram_size
);
328 fprintf(stderr
, "No memory\n");
335 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
336 bytenr
, &length
, &multi
, mirror_num
, NULL
);
338 fprintf(stderr
, "Error mapping block %d\n", ret
);
341 device
= multi
->stripes
[0].dev
;
344 dev_bytenr
= multi
->stripes
[0].physical
;
347 if (size_left
< length
)
350 done
= pread(dev_fd
, inbuf
+count
, length
, dev_bytenr
);
351 /* Need both checks, or we miss negative values due to u64 conversion */
352 if (done
< 0 || done
< length
) {
353 num_copies
= btrfs_num_copies(&root
->fs_info
->mapping_tree
,
356 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
357 if (mirror_num
> num_copies
) {
359 fprintf(stderr
, "Exhausted mirrors trying to read\n");
362 fprintf(stderr
, "Trying another mirror\n");
373 if (compress
== BTRFS_COMPRESS_NONE
) {
374 while (total
< num_bytes
) {
375 done
= pwrite(fd
, inbuf
+total
, num_bytes
-total
,
379 fprintf(stderr
, "Error writing: %d %s\n", errno
, strerror(errno
));
388 ret
= decompress(inbuf
, outbuf
, disk_size
, &ram_size
, compress
);
390 num_copies
= btrfs_num_copies(&root
->fs_info
->mapping_tree
,
393 if (mirror_num
>= num_copies
) {
397 fprintf(stderr
, "Trying another mirror\n");
401 while (total
< num_bytes
) {
402 done
= pwrite(fd
, outbuf
+ offset
+ total
,
423 static enum loop_response
ask_to_continue(const char *file
)
428 printf("We seem to be looping a lot on %s, do you want to keep going "
429 "on ? (y/N/a): ", file
);
431 ret
= fgets(buf
, 2, stdin
);
432 if (*ret
== '\n' || tolower(*ret
) == 'n')
434 if (tolower(*ret
) == 'a')
436 if (tolower(*ret
) != 'y') {
437 printf("Please enter one of 'y', 'n', or 'a': ");
441 return LOOP_CONTINUE
;
445 static int set_file_xattrs(struct btrfs_root
*root
, u64 inode
,
446 int fd
, const char *file_name
)
448 struct btrfs_key key
;
449 struct btrfs_path
*path
;
450 struct extent_buffer
*leaf
;
451 struct btrfs_dir_item
*di
;
460 key
.objectid
= inode
;
461 key
.type
= BTRFS_XATTR_ITEM_KEY
;
464 path
= btrfs_alloc_path();
468 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
472 leaf
= path
->nodes
[0];
474 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
476 ret
= next_leaf(root
, path
);
479 "Error searching for extended attributes: %d\n",
483 /* No more leaves to search */
487 leaf
= path
->nodes
[0];
492 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
493 if (key
.type
!= BTRFS_XATTR_ITEM_KEY
|| key
.objectid
!= inode
)
496 total_len
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
497 di
= btrfs_item_ptr(leaf
, path
->slots
[0],
498 struct btrfs_dir_item
);
500 while (cur
< total_len
) {
501 len
= btrfs_dir_name_len(leaf
, di
);
502 if (len
> name_len
) {
504 name
= (char *) malloc(len
+ 1);
510 read_extent_buffer(leaf
, name
,
511 (unsigned long)(di
+ 1), len
);
515 len
= btrfs_dir_data_len(leaf
, di
);
516 if (len
> data_len
) {
518 data
= (char *) malloc(len
);
524 read_extent_buffer(leaf
, data
,
525 (unsigned long)(di
+ 1) + name_len
,
529 if (fsetxattr(fd
, name
, data
, data_len
, 0)) {
533 "Error setting extended attribute %s on file %s: %s\n",
534 name
, file_name
, strerror(err
));
537 len
= sizeof(*di
) + name_len
+ data_len
;
539 di
= (struct btrfs_dir_item
*)((char *)di
+ len
);
545 btrfs_free_path(path
);
553 static int copy_file(struct btrfs_root
*root
, int fd
, struct btrfs_key
*key
,
556 struct extent_buffer
*leaf
;
557 struct btrfs_path
*path
;
558 struct btrfs_file_extent_item
*fi
;
559 struct btrfs_inode_item
*inode_item
;
560 struct btrfs_key found_key
;
567 path
= btrfs_alloc_path();
569 fprintf(stderr
, "Ran out of memory\n");
573 ret
= btrfs_lookup_inode(NULL
, root
, path
, key
, 0);
575 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
576 struct btrfs_inode_item
);
577 found_size
= btrfs_inode_size(path
->nodes
[0], inode_item
);
579 btrfs_release_path(path
);
582 key
->type
= BTRFS_EXTENT_DATA_KEY
;
584 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
586 fprintf(stderr
, "Error searching %d\n", ret
);
587 btrfs_free_path(path
);
591 leaf
= path
->nodes
[0];
593 ret
= next_leaf(root
, path
);
595 fprintf(stderr
, "Error getting next leaf %d\n",
597 btrfs_free_path(path
);
599 } else if (ret
> 0) {
600 /* No more leaves to search */
601 btrfs_free_path(path
);
604 leaf
= path
->nodes
[0];
608 if (loops
>= 0 && loops
++ >= 1024) {
609 enum loop_response resp
;
611 resp
= ask_to_continue(file
);
612 if (resp
== LOOP_STOP
)
614 else if (resp
== LOOP_CONTINUE
)
616 else if (resp
== LOOP_DONTASK
)
619 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
621 ret
= next_leaf(root
, path
);
623 fprintf(stderr
, "Error searching %d\n", ret
);
624 btrfs_free_path(path
);
627 /* No more leaves to search */
628 btrfs_free_path(path
);
631 leaf
= path
->nodes
[0];
635 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
636 if (found_key
.objectid
!= key
->objectid
)
638 if (found_key
.type
!= key
->type
)
640 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
641 struct btrfs_file_extent_item
);
642 extent_type
= btrfs_file_extent_type(leaf
, fi
);
643 compression
= btrfs_file_extent_compression(leaf
, fi
);
644 if (compression
>= BTRFS_COMPRESS_LAST
) {
645 fprintf(stderr
, "Don't support compression yet %d\n",
647 btrfs_free_path(path
);
651 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
)
653 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
654 ret
= copy_one_inline(fd
, path
, found_key
.offset
);
656 btrfs_free_path(path
);
659 } else if (extent_type
== BTRFS_FILE_EXTENT_REG
) {
660 ret
= copy_one_extent(root
, fd
, leaf
, fi
,
663 btrfs_free_path(path
);
667 printf("Weird extent type %d\n", extent_type
);
673 btrfs_free_path(path
);
676 ret
= ftruncate(fd
, (loff_t
)found_size
);
681 ret
= set_file_xattrs(root
, key
->objectid
, fd
, file
);
688 static int search_dir(struct btrfs_root
*root
, struct btrfs_key
*key
,
689 const char *output_rootdir
, const char *in_dir
,
692 struct btrfs_path
*path
;
693 struct extent_buffer
*leaf
;
694 struct btrfs_dir_item
*dir_item
;
695 struct btrfs_key found_key
, location
;
696 char filename
[BTRFS_NAME_LEN
+ 1];
697 unsigned long name_ptr
;
704 path
= btrfs_alloc_path();
706 fprintf(stderr
, "Ran out of memory\n");
711 key
->type
= BTRFS_DIR_INDEX_KEY
;
713 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
715 fprintf(stderr
, "Error searching %d\n", ret
);
716 btrfs_free_path(path
);
720 leaf
= path
->nodes
[0];
723 printf("No leaf after search, looking for the next "
725 ret
= next_leaf(root
, path
);
727 fprintf(stderr
, "Error getting next leaf %d\n",
729 btrfs_free_path(path
);
731 } else if (ret
> 0) {
732 /* No more leaves to search */
734 printf("Reached the end of the tree looking "
735 "for the directory\n");
736 btrfs_free_path(path
);
739 leaf
= path
->nodes
[0];
743 if (loops
++ >= 1024) {
744 printf("We have looped trying to restore files in %s "
745 "too many times to be making progress, "
746 "stopping\n", in_dir
);
750 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
752 ret
= next_leaf(root
, path
);
754 fprintf(stderr
, "Error searching %d\n",
756 btrfs_free_path(path
);
758 } else if (ret
> 0) {
759 /* No more leaves to search */
761 printf("Reached the end of "
762 "the tree searching the"
764 btrfs_free_path(path
);
767 leaf
= path
->nodes
[0];
771 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
772 if (found_key
.objectid
!= key
->objectid
) {
774 printf("Found objectid=%Lu, key=%Lu\n",
775 found_key
.objectid
, key
->objectid
);
778 if (found_key
.type
!= key
->type
) {
780 printf("Found type=%u, want=%u\n",
781 found_key
.type
, key
->type
);
784 dir_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
785 struct btrfs_dir_item
);
786 name_ptr
= (unsigned long)(dir_item
+ 1);
787 name_len
= btrfs_dir_name_len(leaf
, dir_item
);
788 read_extent_buffer(leaf
, filename
, name_ptr
, name_len
);
789 filename
[name_len
] = '\0';
790 type
= btrfs_dir_type(leaf
, dir_item
);
791 btrfs_dir_item_key_to_cpu(leaf
, dir_item
, &location
);
793 /* full path from root of btrfs being restored */
794 snprintf(fs_name
, 4096, "%s/%s", in_dir
, filename
);
796 if (mreg
&& REG_NOMATCH
== regexec(mreg
, fs_name
, 0, NULL
, 0))
799 /* full path from system root */
800 snprintf(path_name
, 4096, "%s%s", output_rootdir
, fs_name
);
803 * At this point we're only going to restore directories and
804 * files, no symlinks or anything else.
806 if (type
== BTRFS_FT_REG_FILE
) {
811 ret
= stat(path_name
, &st
);
814 if (verbose
|| !warn
)
815 printf("Skipping existing file"
819 printf("If you wish to overwrite use "
820 "the -o option to overwrite\n");
827 printf("Restoring %s\n", path_name
);
830 fd
= open(path_name
, O_CREAT
|O_WRONLY
, 0644);
832 fprintf(stderr
, "Error creating %s: %d\n",
836 btrfs_free_path(path
);
840 ret
= copy_file(root
, fd
, &location
, path_name
);
843 fprintf(stderr
, "Error copying data for %s\n",
847 btrfs_free_path(path
);
850 } else if (type
== BTRFS_FT_DIR
) {
851 struct btrfs_root
*search_root
= root
;
852 char *dir
= strdup(fs_name
);
855 fprintf(stderr
, "Ran out of memory\n");
856 btrfs_free_path(path
);
860 if (location
.type
== BTRFS_ROOT_ITEM_KEY
) {
862 * If we are a snapshot and this is the index
863 * object to ourselves just skip it.
865 if (location
.objectid
==
866 root
->root_key
.objectid
) {
871 location
.offset
= (u64
)-1;
872 search_root
= btrfs_read_fs_root(root
->fs_info
,
874 if (IS_ERR(search_root
)) {
876 fprintf(stderr
, "Error reading "
877 "subvolume %s: %lu\n",
879 PTR_ERR(search_root
));
882 btrfs_free_path(path
);
883 return PTR_ERR(search_root
);
887 * A subvolume will have a key.offset of 0, a
888 * snapshot will have key.offset of a transid.
890 if (search_root
->root_key
.offset
!= 0 &&
893 printf("Skipping snapshot %s\n",
897 location
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
901 printf("Restoring %s\n", path_name
);
907 ret
= mkdir(path_name
, 0755);
908 if (ret
&& errno
!= EEXIST
) {
910 fprintf(stderr
, "Error mkdiring %s: %d\n",
914 btrfs_free_path(path
);
918 ret
= search_dir(search_root
, &location
,
919 output_rootdir
, dir
, mreg
);
922 fprintf(stderr
, "Error searching %s\n",
926 btrfs_free_path(path
);
935 printf("Done searching %s\n", in_dir
);
936 btrfs_free_path(path
);
940 static int do_list_roots(struct btrfs_root
*root
)
942 struct btrfs_key key
;
943 struct btrfs_key found_key
;
944 struct btrfs_disk_key disk_key
;
945 struct btrfs_path
*path
;
946 struct extent_buffer
*leaf
;
947 struct btrfs_root_item ri
;
948 unsigned long offset
;
952 root
= root
->fs_info
->tree_root
;
953 path
= btrfs_alloc_path();
955 fprintf(stderr
, "Failed to alloc path\n");
961 key
.type
= BTRFS_ROOT_ITEM_KEY
;
963 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
965 fprintf(stderr
, "Failed to do search %d\n", ret
);
966 btrfs_free_path(path
);
971 leaf
= path
->nodes
[0];
972 slot
= path
->slots
[0];
973 if (slot
>= btrfs_header_nritems(leaf
)) {
974 ret
= btrfs_next_leaf(root
, path
);
977 leaf
= path
->nodes
[0];
978 slot
= path
->slots
[0];
980 btrfs_item_key(leaf
, &disk_key
, slot
);
981 btrfs_disk_key_to_cpu(&found_key
, &disk_key
);
982 if (btrfs_key_type(&found_key
) != BTRFS_ROOT_ITEM_KEY
) {
987 offset
= btrfs_item_ptr_offset(leaf
, slot
);
988 read_extent_buffer(leaf
, &ri
, offset
, sizeof(ri
));
990 btrfs_print_key(&disk_key
);
991 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri
),
992 btrfs_root_level(&ri
));
995 btrfs_free_path(path
);
1000 static struct btrfs_root
*open_fs(const char *dev
, u64 root_location
,
1001 int super_mirror
, int list_roots
)
1003 struct btrfs_fs_info
*fs_info
= NULL
;
1004 struct btrfs_root
*root
= NULL
;
1008 for (i
= super_mirror
; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
1009 bytenr
= btrfs_sb_offset(i
);
1010 fs_info
= open_ctree_fs_info(dev
, bytenr
, root_location
,
1011 OPEN_CTREE_PARTIAL
);
1014 fprintf(stderr
, "Could not open root, trying backup super\n");
1021 * All we really need to succeed is reading the chunk tree, everything
1022 * else we can do by hand, since we only need to read the tree root and
1025 if (!extent_buffer_uptodate(fs_info
->tree_root
->node
)) {
1028 root
= fs_info
->tree_root
;
1030 root_location
= btrfs_super_root(fs_info
->super_copy
);
1031 generation
= btrfs_super_generation(fs_info
->super_copy
);
1032 root
->node
= read_tree_block(root
, root_location
,
1033 root
->leafsize
, generation
);
1034 if (!extent_buffer_uptodate(root
->node
)) {
1035 fprintf(stderr
, "Error opening tree root\n");
1041 if (!list_roots
&& !fs_info
->fs_root
) {
1042 struct btrfs_key key
;
1044 key
.objectid
= BTRFS_FS_TREE_OBJECTID
;
1045 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1046 key
.offset
= (u64
)-1;
1047 fs_info
->fs_root
= btrfs_read_fs_root_no_cache(fs_info
, &key
);
1048 if (IS_ERR(fs_info
->fs_root
)) {
1049 fprintf(stderr
, "Couldn't read fs root: %ld\n",
1050 PTR_ERR(fs_info
->fs_root
));
1051 close_ctree(fs_info
->tree_root
);
1056 if (list_roots
&& do_list_roots(fs_info
->tree_root
)) {
1057 close_ctree(fs_info
->tree_root
);
1061 return fs_info
->fs_root
;
1064 static int find_first_dir(struct btrfs_root
*root
, u64
*objectid
)
1066 struct btrfs_path
*path
;
1067 struct btrfs_key found_key
;
1068 struct btrfs_key key
;
1073 key
.type
= BTRFS_DIR_INDEX_KEY
;
1076 path
= btrfs_alloc_path();
1078 fprintf(stderr
, "Ran out of memory\n");
1082 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1084 fprintf(stderr
, "Error searching %d\n", ret
);
1088 if (!path
->nodes
[0]) {
1089 fprintf(stderr
, "No leaf!\n");
1093 for (i
= path
->slots
[0];
1094 i
< btrfs_header_nritems(path
->nodes
[0]); i
++) {
1095 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
, i
);
1096 if (found_key
.type
!= key
.type
)
1099 printf("Using objectid %Lu for first dir\n",
1100 found_key
.objectid
);
1101 *objectid
= found_key
.objectid
;
1106 ret
= next_leaf(root
, path
);
1108 fprintf(stderr
, "Error getting next leaf %d\n",
1111 } else if (ret
> 0) {
1112 fprintf(stderr
, "No more leaves\n");
1115 } while (!path
->nodes
[0]);
1118 printf("Couldn't find a dir index item\n");
1120 btrfs_free_path(path
);
1124 static struct option long_options
[] = {
1125 { "path-regex", 1, NULL
, 256},
1126 { "dry-run", 0, NULL
, 'D'},
1130 const char * const cmd_restore_usage
[] = {
1131 "btrfs restore [options] <device> <path> | -l <device>",
1132 "Try to restore files from a damaged filesystem (unmounted)",
1135 "-x get extended attributes",
1139 "-t <bytenr> tree location",
1140 "-f <bytenr> filesystem location",
1141 "-u <mirror> super mirror",
1142 "-r <rootid> root objectid",
1144 "-l list tree roots",
1145 "-D|--dry-run dry run (only list files that would be recovered)",
1146 "--path-regex <regex>",
1147 " restore only filenames matching regex,",
1148 " you have to use following syntax (possibly quoted):",
1149 " ^/(|home(|/username(|/Desktop(|/.*))))$",
1150 "-c ignore case (--path-regrex only)",
1154 int cmd_restore(int argc
, char **argv
)
1156 struct btrfs_root
*root
;
1157 struct btrfs_key key
;
1159 u64 tree_location
= 0;
1160 u64 fs_location
= 0;
1161 u64 root_objectid
= 0;
1165 int option_index
= 0;
1166 int super_mirror
= 0;
1169 const char *match_regstr
= NULL
;
1170 int match_cflags
= REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
;
1171 regex_t match_reg
, *mreg
= NULL
;
1174 while ((opt
= getopt_long(argc
, argv
, "sxviot:u:df:r:lDc", long_options
,
1175 &option_index
)) != -1) {
1191 tree_location
= arg_strtou64(optarg
);
1194 fs_location
= arg_strtou64(optarg
);
1197 super_mirror
= arg_strtou64(optarg
);
1198 if (super_mirror
>= BTRFS_SUPER_MIRROR_MAX
) {
1199 fprintf(stderr
, "Super mirror not "
1208 root_objectid
= arg_strtou64(optarg
);
1209 if (!is_fstree(root_objectid
)) {
1210 fprintf(stderr
, "objectid %llu is not a valid fs/file tree\n",
1222 match_cflags
|= REG_ICASE
;
1224 /* long option without single letter alternative */
1226 match_regstr
= optarg
;
1232 usage(cmd_restore_usage
);
1236 if (!list_roots
&& check_argc_min(argc
- optind
, 2))
1237 usage(cmd_restore_usage
);
1238 else if (list_roots
&& check_argc_min(argc
- optind
, 1))
1239 usage(cmd_restore_usage
);
1241 if (fs_location
&& root_objectid
) {
1242 fprintf(stderr
, "don't use -f and -r at the same time.\n");
1246 if ((ret
= check_mounted(argv
[optind
])) < 0) {
1247 fprintf(stderr
, "Could not check mount status: %s\n",
1251 fprintf(stderr
, "%s is currently mounted. Aborting.\n", argv
[optind
]);
1255 root
= open_fs(argv
[optind
], tree_location
, super_mirror
, list_roots
);
1262 if (fs_location
!= 0) {
1263 free_extent_buffer(root
->node
);
1264 root
->node
= read_tree_block(root
, fs_location
, root
->leafsize
, 0);
1266 fprintf(stderr
, "Failed to read fs location\n");
1272 memset(path_name
, 0, 4096);
1274 strncpy(dir_name
, argv
[optind
+ 1], sizeof dir_name
);
1275 dir_name
[sizeof dir_name
- 1] = 0;
1277 /* Strip the trailing / on the dir name */
1278 len
= strlen(dir_name
);
1279 while (len
&& dir_name
[--len
] == '/') {
1280 dir_name
[len
] = '\0';
1283 if (root_objectid
!= 0) {
1284 struct btrfs_root
*orig_root
= root
;
1286 key
.objectid
= root_objectid
;
1287 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1288 key
.offset
= (u64
)-1;
1289 root
= btrfs_read_fs_root(orig_root
->fs_info
, &key
);
1291 fprintf(stderr
, "fail to read root %llu: %s\n",
1292 root_objectid
, strerror(-PTR_ERR(root
)));
1302 ret
= find_first_dir(root
, &key
.objectid
);
1306 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
1310 ret
= regcomp(&match_reg
, match_regstr
, match_cflags
);
1312 regerror(ret
, &match_reg
, reg_err
, sizeof(reg_err
));
1313 fprintf(stderr
, "Regex compile failed: %s\n", reg_err
);
1320 printf("This is a dry-run, no files are going to be restored\n");
1322 ret
= search_dir(root
, &key
, dir_name
, "", mreg
);