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(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;
293 compress
= btrfs_file_extent_compression(leaf
, fi
);
294 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
295 disk_size
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
296 ram_size
= btrfs_file_extent_ram_bytes(leaf
, fi
);
297 offset
= btrfs_file_extent_offset(leaf
, fi
);
298 num_bytes
= btrfs_file_extent_num_bytes(leaf
, fi
);
299 size_left
= num_bytes
;
303 printf("offset is %Lu\n", offset
);
304 /* we found a hole */
308 inbuf
= malloc(size_left
);
310 fprintf(stderr
, "No memory\n");
314 if (compress
!= BTRFS_COMPRESS_NONE
) {
315 outbuf
= malloc(ram_size
);
317 fprintf(stderr
, "No memory\n");
324 ret
= btrfs_map_block(&root
->fs_info
->mapping_tree
, READ
,
325 bytenr
, &length
, &multi
, mirror_num
, NULL
);
327 fprintf(stderr
, "Error mapping block %d\n", ret
);
330 device
= multi
->stripes
[0].dev
;
333 dev_bytenr
= multi
->stripes
[0].physical
;
336 if (size_left
< length
)
339 done
= pread(dev_fd
, inbuf
+count
, length
, dev_bytenr
);
340 /* Need both checks, or we miss negative values due to u64 conversion */
341 if (done
< 0 || done
< length
) {
342 num_copies
= btrfs_num_copies(&root
->fs_info
->mapping_tree
,
345 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
346 if (mirror_num
> num_copies
) {
348 fprintf(stderr
, "Exhausted mirrors trying to read\n");
351 fprintf(stderr
, "Trying another mirror\n");
362 if (compress
== BTRFS_COMPRESS_NONE
) {
363 while (total
< num_bytes
) {
364 done
= pwrite(fd
, inbuf
+total
, num_bytes
-total
,
368 fprintf(stderr
, "Error writing: %d %s\n", errno
, strerror(errno
));
377 ret
= decompress(inbuf
, outbuf
, num_bytes
, &ram_size
, compress
);
379 num_copies
= btrfs_num_copies(&root
->fs_info
->mapping_tree
,
382 if (mirror_num
>= num_copies
) {
386 fprintf(stderr
, "Trying another mirror\n");
390 while (total
< ram_size
) {
391 done
= pwrite(fd
, outbuf
+total
, ram_size
-total
, pos
+total
);
404 static int ask_to_continue(const char *file
)
409 printf("We seem to be looping a lot on %s, do you want to keep going "
410 "on ? (y/N): ", file
);
412 ret
= fgets(buf
, 2, stdin
);
413 if (*ret
== '\n' || tolower(*ret
) == 'n')
415 if (tolower(*ret
) != 'y') {
416 printf("Please enter either 'y' or 'n': ");
424 static int set_file_xattrs(struct btrfs_root
*root
, u64 inode
,
425 int fd
, const char *file_name
)
427 struct btrfs_key key
;
428 struct btrfs_path
*path
;
429 struct extent_buffer
*leaf
;
430 struct btrfs_dir_item
*di
;
439 key
.objectid
= inode
;
440 key
.type
= BTRFS_XATTR_ITEM_KEY
;
443 path
= btrfs_alloc_path();
447 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
451 leaf
= path
->nodes
[0];
453 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
455 ret
= next_leaf(root
, path
);
458 "Error searching for extended attributes: %d\n",
462 /* No more leaves to search */
466 leaf
= path
->nodes
[0];
471 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
472 if (key
.type
!= BTRFS_XATTR_ITEM_KEY
|| key
.objectid
!= inode
)
475 total_len
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
476 di
= btrfs_item_ptr(leaf
, path
->slots
[0],
477 struct btrfs_dir_item
);
479 while (cur
< total_len
) {
480 len
= btrfs_dir_name_len(leaf
, di
);
481 if (len
> name_len
) {
483 name
= (char *) malloc(len
+ 1);
489 read_extent_buffer(leaf
, name
,
490 (unsigned long)(di
+ 1), len
);
494 len
= btrfs_dir_data_len(leaf
, di
);
495 if (len
> data_len
) {
497 data
= (char *) malloc(len
);
503 read_extent_buffer(leaf
, data
,
504 (unsigned long)(di
+ 1) + name_len
,
508 if (fsetxattr(fd
, name
, data
, data_len
, 0)) {
512 "Error setting extended attribute %s on file %s: %s\n",
513 name
, file_name
, strerror(err
));
516 len
= sizeof(*di
) + name_len
+ data_len
;
518 di
= (struct btrfs_dir_item
*)((char *)di
+ len
);
524 btrfs_free_path(path
);
532 static int copy_file(struct btrfs_root
*root
, int fd
, struct btrfs_key
*key
,
535 struct extent_buffer
*leaf
;
536 struct btrfs_path
*path
;
537 struct btrfs_file_extent_item
*fi
;
538 struct btrfs_inode_item
*inode_item
;
539 struct btrfs_key found_key
;
546 path
= btrfs_alloc_path();
548 fprintf(stderr
, "Ran out of memory\n");
551 path
->skip_locking
= 1;
553 ret
= btrfs_lookup_inode(NULL
, root
, path
, key
, 0);
555 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
556 struct btrfs_inode_item
);
557 found_size
= btrfs_inode_size(path
->nodes
[0], inode_item
);
559 btrfs_release_path(path
);
562 key
->type
= BTRFS_EXTENT_DATA_KEY
;
564 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
566 fprintf(stderr
, "Error searching %d\n", ret
);
567 btrfs_free_path(path
);
571 leaf
= path
->nodes
[0];
573 ret
= next_leaf(root
, path
);
575 fprintf(stderr
, "Error getting next leaf %d\n",
577 btrfs_free_path(path
);
579 } else if (ret
> 0) {
580 /* No more leaves to search */
581 btrfs_free_path(path
);
584 leaf
= path
->nodes
[0];
588 if (loops
++ >= 1024) {
589 ret
= ask_to_continue(file
);
594 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
596 ret
= next_leaf(root
, path
);
598 fprintf(stderr
, "Error searching %d\n", ret
);
599 btrfs_free_path(path
);
602 /* No more leaves to search */
603 btrfs_free_path(path
);
606 leaf
= path
->nodes
[0];
610 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
611 if (found_key
.objectid
!= key
->objectid
)
613 if (found_key
.type
!= key
->type
)
615 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
616 struct btrfs_file_extent_item
);
617 extent_type
= btrfs_file_extent_type(leaf
, fi
);
618 compression
= btrfs_file_extent_compression(leaf
, fi
);
619 if (compression
>= BTRFS_COMPRESS_LAST
) {
620 fprintf(stderr
, "Don't support compression yet %d\n",
622 btrfs_free_path(path
);
626 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
)
628 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
629 ret
= copy_one_inline(fd
, path
, found_key
.offset
);
631 btrfs_free_path(path
);
634 } else if (extent_type
== BTRFS_FILE_EXTENT_REG
) {
635 ret
= copy_one_extent(root
, fd
, leaf
, fi
,
638 btrfs_free_path(path
);
642 printf("Weird extent type %d\n", extent_type
);
648 btrfs_free_path(path
);
651 ret
= ftruncate(fd
, (loff_t
)found_size
);
656 ret
= set_file_xattrs(root
, key
->objectid
, fd
, file
);
663 static int search_dir(struct btrfs_root
*root
, struct btrfs_key
*key
,
664 const char *output_rootdir
, const char *in_dir
,
667 struct btrfs_path
*path
;
668 struct extent_buffer
*leaf
;
669 struct btrfs_dir_item
*dir_item
;
670 struct btrfs_key found_key
, location
;
671 char filename
[BTRFS_NAME_LEN
+ 1];
672 unsigned long name_ptr
;
679 path
= btrfs_alloc_path();
681 fprintf(stderr
, "Ran out of memory\n");
684 path
->skip_locking
= 1;
687 key
->type
= BTRFS_DIR_INDEX_KEY
;
689 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
691 fprintf(stderr
, "Error searching %d\n", ret
);
692 btrfs_free_path(path
);
696 leaf
= path
->nodes
[0];
699 printf("No leaf after search, looking for the next "
701 ret
= next_leaf(root
, path
);
703 fprintf(stderr
, "Error getting next leaf %d\n",
705 btrfs_free_path(path
);
707 } else if (ret
> 0) {
708 /* No more leaves to search */
710 printf("Reached the end of the tree looking "
711 "for the directory\n");
712 btrfs_free_path(path
);
715 leaf
= path
->nodes
[0];
719 if (loops
++ >= 1024) {
720 printf("We have looped trying to restore files in %s "
721 "too many times to be making progress, "
722 "stopping\n", in_dir
);
726 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
728 ret
= next_leaf(root
, path
);
730 fprintf(stderr
, "Error searching %d\n",
732 btrfs_free_path(path
);
734 } else if (ret
> 0) {
735 /* No more leaves to search */
737 printf("Reached the end of "
738 "the tree searching the"
740 btrfs_free_path(path
);
743 leaf
= path
->nodes
[0];
747 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
748 if (found_key
.objectid
!= key
->objectid
) {
750 printf("Found objectid=%Lu, key=%Lu\n",
751 found_key
.objectid
, key
->objectid
);
754 if (found_key
.type
!= key
->type
) {
756 printf("Found type=%u, want=%u\n",
757 found_key
.type
, key
->type
);
760 dir_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
761 struct btrfs_dir_item
);
762 name_ptr
= (unsigned long)(dir_item
+ 1);
763 name_len
= btrfs_dir_name_len(leaf
, dir_item
);
764 read_extent_buffer(leaf
, filename
, name_ptr
, name_len
);
765 filename
[name_len
] = '\0';
766 type
= btrfs_dir_type(leaf
, dir_item
);
767 btrfs_dir_item_key_to_cpu(leaf
, dir_item
, &location
);
769 /* full path from root of btrfs being restored */
770 snprintf(fs_name
, 4096, "%s/%s", in_dir
, filename
);
772 if (mreg
&& REG_NOMATCH
== regexec(mreg
, fs_name
, 0, NULL
, 0))
775 /* full path from system root */
776 snprintf(path_name
, 4096, "%s%s", output_rootdir
, fs_name
);
779 * At this point we're only going to restore directories and
780 * files, no symlinks or anything else.
782 if (type
== BTRFS_FT_REG_FILE
) {
787 ret
= stat(path_name
, &st
);
790 if (verbose
|| !warn
)
791 printf("Skipping existing file"
795 printf("If you wish to overwrite use "
796 "the -o option to overwrite\n");
803 printf("Restoring %s\n", path_name
);
804 fd
= open(path_name
, O_CREAT
|O_WRONLY
, 0644);
806 fprintf(stderr
, "Error creating %s: %d\n",
810 btrfs_free_path(path
);
814 ret
= copy_file(root
, fd
, &location
, path_name
);
819 btrfs_free_path(path
);
822 } else if (type
== BTRFS_FT_DIR
) {
823 struct btrfs_root
*search_root
= root
;
824 char *dir
= strdup(fs_name
);
827 fprintf(stderr
, "Ran out of memory\n");
828 btrfs_free_path(path
);
832 if (location
.type
== BTRFS_ROOT_ITEM_KEY
) {
834 * If we are a snapshot and this is the index
835 * object to ourselves just skip it.
837 if (location
.objectid
==
838 root
->root_key
.objectid
) {
843 location
.offset
= (u64
)-1;
844 search_root
= btrfs_read_fs_root(root
->fs_info
,
846 if (IS_ERR(search_root
)) {
848 fprintf(stderr
, "Error reading "
849 "subvolume %s: %lu\n",
851 PTR_ERR(search_root
));
854 btrfs_free_path(path
);
855 return PTR_ERR(search_root
);
859 * A subvolume will have a key.offset of 0, a
860 * snapshot will have key.offset of a transid.
862 if (search_root
->root_key
.offset
!= 0 &&
865 printf("Skipping snapshot %s\n",
869 location
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
873 printf("Restoring %s\n", path_name
);
876 ret
= mkdir(path_name
, 0755);
877 if (ret
&& errno
!= EEXIST
) {
879 fprintf(stderr
, "Error mkdiring %s: %d\n",
883 btrfs_free_path(path
);
887 ret
= search_dir(search_root
, &location
,
888 output_rootdir
, dir
, mreg
);
893 btrfs_free_path(path
);
902 printf("Done searching %s\n", in_dir
);
903 btrfs_free_path(path
);
907 static int do_list_roots(struct btrfs_root
*root
)
909 struct btrfs_key key
;
910 struct btrfs_key found_key
;
911 struct btrfs_disk_key disk_key
;
912 struct btrfs_path
*path
;
913 struct extent_buffer
*leaf
;
914 struct btrfs_root_item ri
;
915 unsigned long offset
;
919 root
= root
->fs_info
->tree_root
;
920 path
= btrfs_alloc_path();
922 fprintf(stderr
, "Failed to alloc path\n");
928 key
.type
= BTRFS_ROOT_ITEM_KEY
;
930 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
932 fprintf(stderr
, "Failed to do search %d\n", ret
);
933 btrfs_free_path(path
);
938 leaf
= path
->nodes
[0];
939 slot
= path
->slots
[0];
940 if (slot
>= btrfs_header_nritems(leaf
)) {
941 ret
= btrfs_next_leaf(root
, path
);
944 leaf
= path
->nodes
[0];
945 slot
= path
->slots
[0];
947 btrfs_item_key(leaf
, &disk_key
, slot
);
948 btrfs_disk_key_to_cpu(&found_key
, &disk_key
);
949 if (btrfs_key_type(&found_key
) != BTRFS_ROOT_ITEM_KEY
) {
954 offset
= btrfs_item_ptr_offset(leaf
, slot
);
955 read_extent_buffer(leaf
, &ri
, offset
, sizeof(ri
));
957 btrfs_print_key(&disk_key
);
958 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri
),
959 btrfs_root_level(&ri
));
962 btrfs_free_path(path
);
967 static struct btrfs_root
*open_fs(const char *dev
, u64 root_location
,
968 int super_mirror
, int list_roots
)
970 struct btrfs_fs_info
*fs_info
= NULL
;
971 struct btrfs_root
*root
= NULL
;
975 for (i
= super_mirror
; i
< BTRFS_SUPER_MIRROR_MAX
; i
++) {
976 bytenr
= btrfs_sb_offset(i
);
977 fs_info
= open_ctree_fs_info(dev
, bytenr
, root_location
,
981 fprintf(stderr
, "Could not open root, trying backup super\n");
988 * All we really need to succeed is reading the chunk tree, everything
989 * else we can do by hand, since we only need to read the tree root and
992 if (!extent_buffer_uptodate(fs_info
->tree_root
->node
)) {
995 root
= fs_info
->tree_root
;
997 root_location
= btrfs_super_root(fs_info
->super_copy
);
998 generation
= btrfs_super_generation(fs_info
->super_copy
);
999 root
->node
= read_tree_block(root
, root_location
,
1000 root
->leafsize
, generation
);
1001 if (!extent_buffer_uptodate(root
->node
)) {
1002 fprintf(stderr
, "Error opening tree root\n");
1008 if (!list_roots
&& !fs_info
->fs_root
) {
1009 struct btrfs_key key
;
1011 key
.objectid
= BTRFS_FS_TREE_OBJECTID
;
1012 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1013 key
.offset
= (u64
)-1;
1014 fs_info
->fs_root
= btrfs_read_fs_root_no_cache(fs_info
, &key
);
1015 if (IS_ERR(fs_info
->fs_root
)) {
1016 fprintf(stderr
, "Couldn't read fs root: %ld\n",
1017 PTR_ERR(fs_info
->fs_root
));
1018 close_ctree(fs_info
->tree_root
);
1023 if (list_roots
&& do_list_roots(fs_info
->tree_root
)) {
1024 close_ctree(fs_info
->tree_root
);
1028 return fs_info
->fs_root
;
1031 static int find_first_dir(struct btrfs_root
*root
, u64
*objectid
)
1033 struct btrfs_path
*path
;
1034 struct btrfs_key found_key
;
1035 struct btrfs_key key
;
1040 key
.type
= BTRFS_DIR_INDEX_KEY
;
1043 path
= btrfs_alloc_path();
1045 fprintf(stderr
, "Ran out of memory\n");
1049 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1051 fprintf(stderr
, "Error searching %d\n", ret
);
1055 if (!path
->nodes
[0]) {
1056 fprintf(stderr
, "No leaf!\n");
1060 for (i
= path
->slots
[0];
1061 i
< btrfs_header_nritems(path
->nodes
[0]); i
++) {
1062 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
, i
);
1063 if (found_key
.type
!= key
.type
)
1066 printf("Using objectid %Lu for first dir\n",
1067 found_key
.objectid
);
1068 *objectid
= found_key
.objectid
;
1073 ret
= next_leaf(root
, path
);
1075 fprintf(stderr
, "Error getting next leaf %d\n",
1078 } else if (ret
> 0) {
1079 fprintf(stderr
, "No more leaves\n");
1082 } while (!path
->nodes
[0]);
1085 printf("Couldn't find a dir index item\n");
1087 btrfs_free_path(path
);
1091 static struct option long_options
[] = {
1092 { "path-regex", 1, NULL
, 256},
1096 const char * const cmd_restore_usage
[] = {
1097 "btrfs restore [options] <device> <path> | -l <device>",
1098 "Try to restore files from a damaged filesystem (unmounted)",
1101 "-x get extended attributes",
1105 "-t <location> tree location",
1106 "-f <offset> filesystem location",
1107 "-u <block> super mirror",
1108 "-r <rootid> root objectid",
1110 "-l list tree roots",
1111 "--path-regex <regex>",
1112 " restore only filenames matching regex,",
1113 " you have to use following syntax (possibly quoted):",
1114 " ^/(|home(|/username(|/Desktop(|/.*))))$",
1118 int cmd_restore(int argc
, char **argv
)
1120 struct btrfs_root
*root
;
1121 struct btrfs_key key
;
1123 u64 tree_location
= 0;
1124 u64 fs_location
= 0;
1125 u64 root_objectid
= 0;
1129 int option_index
= 0;
1130 int super_mirror
= 0;
1133 const char *match_regstr
= NULL
;
1134 int match_cflags
= REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
;
1135 regex_t match_reg
, *mreg
= NULL
;
1138 while ((opt
= getopt_long(argc
, argv
, "sxviot:u:df:r:lc", long_options
,
1139 &option_index
)) != -1) {
1156 tree_location
= (u64
)strtoll(optarg
, NULL
, 10);
1158 fprintf(stderr
, "Tree location not valid\n");
1164 fs_location
= (u64
)strtoll(optarg
, NULL
, 10);
1166 fprintf(stderr
, "Fs location not valid\n");
1172 super_mirror
= (int)strtol(optarg
, NULL
, 10);
1174 super_mirror
>= BTRFS_SUPER_MIRROR_MAX
) {
1175 fprintf(stderr
, "Super mirror not "
1185 root_objectid
= (u64
)strtoll(optarg
, NULL
, 10);
1187 fprintf(stderr
, "Root objectid not valid\n");
1195 match_cflags
|= REG_ICASE
;
1197 /* long option without single letter alternative */
1199 match_regstr
= optarg
;
1205 usage(cmd_restore_usage
);
1209 if (!list_roots
&& optind
+ 1 >= argc
)
1210 usage(cmd_restore_usage
);
1211 else if (list_roots
&& optind
>= argc
)
1212 usage(cmd_restore_usage
);
1214 if ((ret
= check_mounted(argv
[optind
])) < 0) {
1215 fprintf(stderr
, "Could not check mount status: %s\n",
1219 fprintf(stderr
, "%s is currently mounted. Aborting.\n", argv
[optind
]);
1223 root
= open_fs(argv
[optind
], tree_location
, super_mirror
, list_roots
);
1230 if (fs_location
!= 0) {
1231 free_extent_buffer(root
->node
);
1232 root
->node
= read_tree_block(root
, fs_location
, root
->leafsize
, 0);
1234 fprintf(stderr
, "Failed to read fs location\n");
1239 memset(path_name
, 0, 4096);
1241 strncpy(dir_name
, argv
[optind
+ 1], sizeof dir_name
);
1242 dir_name
[sizeof dir_name
- 1] = 0;
1244 /* Strip the trailing / on the dir name */
1245 len
= strlen(dir_name
);
1246 while (len
&& dir_name
[--len
] == '/') {
1247 dir_name
[len
] = '\0';
1250 if (root_objectid
!= 0) {
1251 struct btrfs_root
*orig_root
= root
;
1253 key
.objectid
= root_objectid
;
1254 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1255 key
.offset
= (u64
)-1;
1256 root
= btrfs_read_fs_root(orig_root
->fs_info
, &key
);
1258 fprintf(stderr
, "Error reading root\n");
1268 ret
= find_first_dir(root
, &key
.objectid
);
1272 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
1276 ret
= regcomp(&match_reg
, match_regstr
, match_cflags
);
1278 regerror(ret
, &match_reg
, reg_err
, sizeof(reg_err
));
1279 fprintf(stderr
, "Regex compile failed: %s\n", reg_err
);
1285 ret
= search_dir(root
, &key
, dir_name
, "", mreg
);