2 * Copyright (C) 2008 Oracle. 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
24 #include <sys/types.h>
30 #include "kerncompat.h"
34 #include "transaction.h"
38 #include "extent_io.h"
40 #define HEADER_MAGIC 0xbd5c25e27295668bULL
41 #define MAX_PENDING_SIZE (256 * 1024)
42 #define BLOCK_SIZE 1024
43 #define BLOCK_MASK (BLOCK_SIZE - 1)
45 #define COMPRESS_NONE 0
46 #define COMPRESS_ZLIB 1
48 struct meta_cluster_item {
51 } __attribute__ ((__packed__));
53 struct meta_cluster_header {
58 } __attribute__ ((__packed__));
60 /* cluster header + index items + buffers */
62 struct meta_cluster_header header;
63 struct meta_cluster_item items[];
64 } __attribute__ ((__packed__));
66 #define ITEMS_PER_CLUSTER ((BLOCK_SIZE - sizeof(struct meta_cluster)) / \
67 sizeof(struct meta_cluster_item))
77 struct list_head list;
78 struct list_head ordered;
86 struct metadump_struct {
87 struct btrfs_root *root;
90 struct meta_cluster *cluster;
94 pthread_mutex_t mutex;
96 struct rb_root name_tree;
98 struct list_head list;
99 struct list_head ordered;
119 struct mdrestore_struct {
125 pthread_mutex_t mutex;
128 struct rb_root chunk_tree;
129 struct list_head list;
133 u8 uuid[BTRFS_UUID_SIZE];
134 u8 fsid[BTRFS_FSID_SIZE];
142 struct btrfs_fs_info *info;
145 static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
146 u64 search, u64 cluster_bytenr);
147 static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size);
149 static void csum_block(u8 *buf, size_t len)
151 char result[BTRFS_CRC32_SIZE];
153 crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len - BTRFS_CSUM_SIZE);
154 btrfs_csum_final(crc, result);
155 memcpy(buf, result, BTRFS_CRC32_SIZE);
158 static int has_name(struct btrfs_key *key)
161 case BTRFS_DIR_ITEM_KEY:
162 case BTRFS_DIR_INDEX_KEY:
163 case BTRFS_INODE_REF_KEY:
164 case BTRFS_INODE_EXTREF_KEY:
165 case BTRFS_XATTR_ITEM_KEY:
174 static char *generate_garbage(u32 name_len)
176 char *buf = malloc(name_len);
182 for (i = 0; i < name_len; i++) {
183 char c = rand() % 94 + 33;
193 static int name_cmp(struct rb_node *a, struct rb_node *b, int fuzz)
195 struct name *entry = rb_entry(a, struct name, n);
196 struct name *ins = rb_entry(b, struct name, n);
199 len = min(ins->len, entry->len);
200 return memcmp(ins->val, entry->val, len);
203 static int chunk_cmp(struct rb_node *a, struct rb_node *b, int fuzz)
205 struct fs_chunk *entry = rb_entry(a, struct fs_chunk, n);
206 struct fs_chunk *ins = rb_entry(b, struct fs_chunk, n);
208 if (fuzz && ins->logical >= entry->logical &&
209 ins->logical < entry->logical + entry->bytes)
212 if (ins->logical < entry->logical)
214 else if (ins->logical > entry->logical)
219 static void tree_insert(struct rb_root *root, struct rb_node *ins,
220 int (*cmp)(struct rb_node *a, struct rb_node *b,
223 struct rb_node ** p = &root->rb_node;
224 struct rb_node * parent = NULL;
230 dir = cmp(*p, ins, 0);
239 rb_link_node(ins, parent, p);
240 rb_insert_color(ins, root);
243 static struct rb_node *tree_search(struct rb_root *root,
244 struct rb_node *search,
245 int (*cmp)(struct rb_node *a,
246 struct rb_node *b, int fuzz),
249 struct rb_node *n = root->rb_node;
253 dir = cmp(n, search, fuzz);
265 static char *find_collision(struct metadump_struct *md, char *name,
269 struct rb_node *entry;
271 unsigned long checksum;
277 entry = tree_search(&md->name_tree, &tmp.n, name_cmp, 0);
279 val = rb_entry(entry, struct name, n);
284 val = malloc(sizeof(struct name));
286 fprintf(stderr, "Couldn't sanitize name, enomem\n");
290 memset(val, 0, sizeof(*val));
294 val->sub = malloc(name_len);
296 fprintf(stderr, "Couldn't sanitize name, enomem\n");
301 checksum = crc32c(~1, val->val, name_len);
302 memset(val->sub, ' ', name_len);
305 if (crc32c(~1, val->sub, name_len) == checksum &&
306 memcmp(val->sub, val->val, val->len)) {
311 if (val->sub[i] == 127) {
316 } while (val->sub[i] == 127);
321 if (val->sub[i] == '/')
323 memset(val->sub, ' ', i);
328 if (val->sub[i] == '/')
334 fprintf(stderr, "Couldn't find a collision for '%.*s', "
335 "generating normal garbage, it won't match indexes\n",
337 for (i = 0; i < name_len; i++) {
338 char c = rand() % 94 + 33;
346 tree_insert(&md->name_tree, &val->n, name_cmp);
350 static void sanitize_dir_item(struct metadump_struct *md, struct extent_buffer *eb,
353 struct btrfs_dir_item *dir_item;
356 unsigned long name_ptr;
361 int free_garbage = (md->sanitize_names == 1);
363 dir_item = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
364 total_len = btrfs_item_size_nr(eb, slot);
365 while (cur < total_len) {
366 this_len = sizeof(*dir_item) +
367 btrfs_dir_name_len(eb, dir_item) +
368 btrfs_dir_data_len(eb, dir_item);
369 name_ptr = (unsigned long)(dir_item + 1);
370 name_len = btrfs_dir_name_len(eb, dir_item);
372 if (md->sanitize_names > 1) {
373 buf = malloc(name_len);
375 fprintf(stderr, "Couldn't sanitize name, "
379 read_extent_buffer(eb, buf, name_ptr, name_len);
380 garbage = find_collision(md, buf, name_len);
382 garbage = generate_garbage(name_len);
385 fprintf(stderr, "Couldn't sanitize name, enomem\n");
388 write_extent_buffer(eb, garbage, name_ptr, name_len);
390 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
397 static void sanitize_inode_ref(struct metadump_struct *md,
398 struct extent_buffer *eb, int slot, int ext)
400 struct btrfs_inode_extref *extref;
401 struct btrfs_inode_ref *ref;
404 unsigned long name_ptr;
408 int free_garbage = (md->sanitize_names == 1);
410 item_size = btrfs_item_size_nr(eb, slot);
411 ptr = btrfs_item_ptr_offset(eb, slot);
412 while (cur_offset < item_size) {
414 extref = (struct btrfs_inode_extref *)(ptr +
416 name_ptr = (unsigned long)(&extref->name);
417 len = btrfs_inode_extref_name_len(eb, extref);
418 cur_offset += sizeof(*extref);
420 ref = (struct btrfs_inode_ref *)(ptr + cur_offset);
421 len = btrfs_inode_ref_name_len(eb, ref);
422 name_ptr = (unsigned long)(ref + 1);
423 cur_offset += sizeof(*ref);
427 if (md->sanitize_names > 1) {
430 fprintf(stderr, "Couldn't sanitize name, "
434 read_extent_buffer(eb, buf, name_ptr, len);
435 garbage = find_collision(md, buf, len);
437 garbage = generate_garbage(len);
441 fprintf(stderr, "Couldn't sanitize name, enomem\n");
444 write_extent_buffer(eb, garbage, name_ptr, len);
450 static void sanitize_xattr(struct metadump_struct *md,
451 struct extent_buffer *eb, int slot)
453 struct btrfs_dir_item *dir_item;
454 unsigned long data_ptr;
457 dir_item = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
458 data_len = btrfs_dir_data_len(eb, dir_item);
460 data_ptr = (unsigned long)((char *)(dir_item + 1) +
461 btrfs_dir_name_len(eb, dir_item));
462 memset_extent_buffer(eb, 0, data_ptr, data_len);
465 static void sanitize_name(struct metadump_struct *md, u8 *dst,
466 struct extent_buffer *src, struct btrfs_key *key,
469 struct extent_buffer *eb;
471 eb = alloc_dummy_eb(src->start, src->len);
473 fprintf(stderr, "Couldn't sanitize name, no memory\n");
477 memcpy(eb->data, dst, eb->len);
480 case BTRFS_DIR_ITEM_KEY:
481 case BTRFS_DIR_INDEX_KEY:
482 sanitize_dir_item(md, eb, slot);
484 case BTRFS_INODE_REF_KEY:
485 sanitize_inode_ref(md, eb, slot, 0);
487 case BTRFS_INODE_EXTREF_KEY:
488 sanitize_inode_ref(md, eb, slot, 1);
490 case BTRFS_XATTR_ITEM_KEY:
491 sanitize_xattr(md, eb, slot);
497 memcpy(dst, eb->data, eb->len);
502 * zero inline extents and csum items
504 static void zero_items(struct metadump_struct *md, u8 *dst,
505 struct extent_buffer *src)
507 struct btrfs_file_extent_item *fi;
508 struct btrfs_item *item;
509 struct btrfs_key key;
510 u32 nritems = btrfs_header_nritems(src);
515 for (i = 0; i < nritems; i++) {
516 item = btrfs_item_nr(src, i);
517 btrfs_item_key_to_cpu(src, &key, i);
518 if (key.type == BTRFS_CSUM_ITEM_KEY) {
519 size = btrfs_item_size_nr(src, i);
520 memset(dst + btrfs_leaf_data(src) +
521 btrfs_item_offset_nr(src, i), 0, size);
525 if (md->sanitize_names && has_name(&key)) {
526 sanitize_name(md, dst, src, &key, i);
530 if (key.type != BTRFS_EXTENT_DATA_KEY)
533 fi = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
534 extent_type = btrfs_file_extent_type(src, fi);
535 if (extent_type != BTRFS_FILE_EXTENT_INLINE)
538 ptr = btrfs_file_extent_inline_start(fi);
539 size = btrfs_file_extent_inline_item_len(src, item);
540 memset(dst + ptr, 0, size);
545 * copy buffer and zero useless data in the buffer
547 static void copy_buffer(struct metadump_struct *md, u8 *dst,
548 struct extent_buffer *src)
554 memcpy(dst, src->data, src->len);
555 if (src->start == BTRFS_SUPER_INFO_OFFSET)
558 level = btrfs_header_level(src);
559 nritems = btrfs_header_nritems(src);
562 size = sizeof(struct btrfs_header);
563 memset(dst + size, 0, src->len - size);
564 } else if (level == 0) {
565 size = btrfs_leaf_data(src) +
566 btrfs_item_offset_nr(src, nritems - 1) -
567 btrfs_item_nr_offset(nritems);
568 memset(dst + btrfs_item_nr_offset(nritems), 0, size);
569 zero_items(md, dst, src);
571 size = offsetof(struct btrfs_node, ptrs) +
572 sizeof(struct btrfs_key_ptr) * nritems;
573 memset(dst + size, 0, src->len - size);
575 csum_block(dst, src->len);
578 static void *dump_worker(void *data)
580 struct metadump_struct *md = (struct metadump_struct *)data;
581 struct async_work *async;
585 pthread_mutex_lock(&md->mutex);
586 while (list_empty(&md->list)) {
588 pthread_mutex_unlock(&md->mutex);
591 pthread_cond_wait(&md->cond, &md->mutex);
593 async = list_entry(md->list.next, struct async_work, list);
594 list_del_init(&async->list);
595 pthread_mutex_unlock(&md->mutex);
597 if (md->compress_level > 0) {
598 u8 *orig = async->buffer;
600 async->bufsize = compressBound(async->size);
601 async->buffer = malloc(async->bufsize);
603 ret = compress2(async->buffer,
604 (unsigned long *)&async->bufsize,
605 orig, async->size, md->compress_level);
613 pthread_mutex_lock(&md->mutex);
615 pthread_mutex_unlock(&md->mutex);
621 static void meta_cluster_init(struct metadump_struct *md, u64 start)
623 struct meta_cluster_header *header;
627 header = &md->cluster->header;
628 header->magic = cpu_to_le64(HEADER_MAGIC);
629 header->bytenr = cpu_to_le64(start);
630 header->nritems = cpu_to_le32(0);
631 header->compress = md->compress_level > 0 ?
632 COMPRESS_ZLIB : COMPRESS_NONE;
635 static int metadump_init(struct metadump_struct *md, struct btrfs_root *root,
636 FILE *out, int num_threads, int compress_level,
641 memset(md, 0, sizeof(*md));
642 pthread_cond_init(&md->cond, NULL);
643 pthread_mutex_init(&md->mutex, NULL);
644 INIT_LIST_HEAD(&md->list);
645 INIT_LIST_HEAD(&md->ordered);
648 md->pending_start = (u64)-1;
649 md->compress_level = compress_level;
650 md->cluster = calloc(1, BLOCK_SIZE);
651 md->sanitize_names = sanitize_names;
652 if (sanitize_names > 1)
653 crc32c_optimization_init();
656 pthread_cond_destroy(&md->cond);
657 pthread_mutex_destroy(&md->mutex);
661 meta_cluster_init(md, 0);
665 md->name_tree.rb_node = NULL;
666 md->num_threads = num_threads;
667 md->threads = calloc(num_threads, sizeof(pthread_t));
670 pthread_cond_destroy(&md->cond);
671 pthread_mutex_destroy(&md->mutex);
675 for (i = 0; i < num_threads; i++) {
676 ret = pthread_create(md->threads + i, NULL, dump_worker, md);
682 pthread_mutex_lock(&md->mutex);
684 pthread_cond_broadcast(&md->cond);
685 pthread_mutex_unlock(&md->mutex);
687 for (i--; i >= 0; i--)
688 pthread_join(md->threads[i], NULL);
690 pthread_cond_destroy(&md->cond);
691 pthread_mutex_destroy(&md->mutex);
699 static void metadump_destroy(struct metadump_struct *md)
704 pthread_mutex_lock(&md->mutex);
706 pthread_cond_broadcast(&md->cond);
707 pthread_mutex_unlock(&md->mutex);
709 for (i = 0; i < md->num_threads; i++)
710 pthread_join(md->threads[i], NULL);
712 pthread_cond_destroy(&md->cond);
713 pthread_mutex_destroy(&md->mutex);
715 while ((n = rb_first(&md->name_tree))) {
718 name = rb_entry(n, struct name, n);
719 rb_erase(n, &md->name_tree);
728 static int write_zero(FILE *out, size_t size)
730 static char zero[BLOCK_SIZE];
731 return fwrite(zero, size, 1, out);
734 static int write_buffers(struct metadump_struct *md, u64 *next)
736 struct meta_cluster_header *header = &md->cluster->header;
737 struct meta_cluster_item *item;
738 struct async_work *async;
744 if (list_empty(&md->ordered))
747 /* wait until all buffers are compressed */
748 while (md->num_items > md->num_ready) {
749 struct timespec ts = {
753 pthread_mutex_unlock(&md->mutex);
754 nanosleep(&ts, NULL);
755 pthread_mutex_lock(&md->mutex);
758 /* setup and write index block */
759 list_for_each_entry(async, &md->ordered, ordered) {
760 item = md->cluster->items + nritems;
761 item->bytenr = cpu_to_le64(async->start);
762 item->size = cpu_to_le32(async->bufsize);
765 header->nritems = cpu_to_le32(nritems);
767 ret = fwrite(md->cluster, BLOCK_SIZE, 1, md->out);
769 fprintf(stderr, "Error writing out cluster: %d\n", errno);
774 bytenr += le64_to_cpu(header->bytenr) + BLOCK_SIZE;
775 while (!list_empty(&md->ordered)) {
776 async = list_entry(md->ordered.next, struct async_work,
778 list_del_init(&async->ordered);
780 bytenr += async->bufsize;
782 ret = fwrite(async->buffer, async->bufsize, 1,
787 fprintf(stderr, "Error writing out cluster: %d\n",
795 /* zero unused space in the last block */
796 if (!err && bytenr & BLOCK_MASK) {
797 size_t size = BLOCK_SIZE - (bytenr & BLOCK_MASK);
800 ret = write_zero(md->out, size);
802 fprintf(stderr, "Error zeroing out buffer: %d\n",
812 static int read_data_extent(struct metadump_struct *md,
813 struct async_work *async)
815 struct btrfs_multi_bio *multi = NULL;
816 struct btrfs_device *device;
817 u64 bytes_left = async->size;
818 u64 logical = async->start;
827 read_len = bytes_left;
828 ret = btrfs_map_block(&md->root->fs_info->mapping_tree, READ,
829 logical, &read_len, &multi, 0, NULL);
831 fprintf(stderr, "Couldn't map data block %d\n", ret);
835 device = multi->stripes[0].dev;
837 if (device->fd == 0) {
839 "Device we need to read from is not open\n");
844 bytenr = multi->stripes[0].physical;
847 read_len = min(read_len, bytes_left);
848 done = pread64(fd, async->buffer+offset, read_len, bytenr);
849 if (done < read_len) {
851 fprintf(stderr, "Error reading extent %d\n",
854 fprintf(stderr, "Short read\n");
866 static int flush_pending(struct metadump_struct *md, int done)
868 struct async_work *async = NULL;
869 struct extent_buffer *eb;
870 u64 blocksize = md->root->nodesize;
876 if (md->pending_size) {
877 async = calloc(1, sizeof(*async));
881 async->start = md->pending_start;
882 async->size = md->pending_size;
883 async->bufsize = async->size;
884 async->buffer = malloc(async->bufsize);
885 if (!async->buffer) {
890 start = async->start;
894 ret = read_data_extent(md, async);
902 while (!md->data && size > 0) {
903 u64 this_read = min(blocksize, size);
904 eb = read_tree_block(md->root, start, this_read, 0);
909 "Error reading metadata block\n");
912 copy_buffer(md, async->buffer + offset, eb);
913 free_extent_buffer(eb);
919 md->pending_start = (u64)-1;
920 md->pending_size = 0;
925 pthread_mutex_lock(&md->mutex);
927 list_add_tail(&async->ordered, &md->ordered);
929 if (md->compress_level > 0) {
930 list_add_tail(&async->list, &md->list);
931 pthread_cond_signal(&md->cond);
936 if (md->num_items >= ITEMS_PER_CLUSTER || done) {
937 ret = write_buffers(md, &start);
939 fprintf(stderr, "Error writing buffers %d\n",
942 meta_cluster_init(md, start);
944 pthread_mutex_unlock(&md->mutex);
948 static int add_extent(u64 start, u64 size, struct metadump_struct *md,
952 if (md->data != data ||
953 md->pending_size + size > MAX_PENDING_SIZE ||
954 md->pending_start + md->pending_size != start) {
955 ret = flush_pending(md, 0);
958 md->pending_start = start;
960 readahead_tree_block(md->root, start, size, 0);
961 md->pending_size += size;
966 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
967 static int is_tree_block(struct btrfs_root *extent_root,
968 struct btrfs_path *path, u64 bytenr)
970 struct extent_buffer *leaf;
971 struct btrfs_key key;
975 leaf = path->nodes[0];
977 struct btrfs_extent_ref_v0 *ref_item;
979 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
980 ret = btrfs_next_leaf(extent_root, path);
985 leaf = path->nodes[0];
987 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
988 if (key.objectid != bytenr)
990 if (key.type != BTRFS_EXTENT_REF_V0_KEY)
992 ref_item = btrfs_item_ptr(leaf, path->slots[0],
993 struct btrfs_extent_ref_v0);
994 ref_objectid = btrfs_ref_objectid_v0(leaf, ref_item);
995 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID)
1003 static int copy_tree_blocks(struct btrfs_root *root, struct extent_buffer *eb,
1004 struct metadump_struct *metadump, int root_tree)
1006 struct extent_buffer *tmp;
1007 struct btrfs_root_item *ri;
1008 struct btrfs_key key;
1015 ret = add_extent(btrfs_header_bytenr(eb), root->leafsize, metadump, 0);
1017 fprintf(stderr, "Error adding metadata block\n");
1021 if (btrfs_header_level(eb) == 0 && !root_tree)
1024 level = btrfs_header_level(eb);
1025 nritems = btrfs_header_nritems(eb);
1026 for (i = 0; i < nritems; i++) {
1028 btrfs_item_key_to_cpu(eb, &key, i);
1029 if (key.type != BTRFS_ROOT_ITEM_KEY)
1031 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
1032 bytenr = btrfs_disk_root_bytenr(eb, ri);
1033 tmp = read_tree_block(root, bytenr, root->leafsize, 0);
1036 "Error reading log root block\n");
1039 ret = copy_tree_blocks(root, tmp, metadump, 0);
1040 free_extent_buffer(tmp);
1044 bytenr = btrfs_node_blockptr(eb, i);
1045 tmp = read_tree_block(root, bytenr, root->leafsize, 0);
1047 fprintf(stderr, "Error reading log block\n");
1050 ret = copy_tree_blocks(root, tmp, metadump, root_tree);
1051 free_extent_buffer(tmp);
1060 static int copy_log_trees(struct btrfs_root *root,
1061 struct metadump_struct *metadump,
1062 struct btrfs_path *path)
1064 u64 blocknr = btrfs_super_log_root(root->fs_info->super_copy);
1069 if (!root->fs_info->log_root_tree ||
1070 !root->fs_info->log_root_tree->node) {
1071 fprintf(stderr, "Error copying tree log, it wasn't setup\n");
1075 return copy_tree_blocks(root, root->fs_info->log_root_tree->node,
1079 static int copy_space_cache(struct btrfs_root *root,
1080 struct metadump_struct *metadump,
1081 struct btrfs_path *path)
1083 struct extent_buffer *leaf;
1084 struct btrfs_file_extent_item *fi;
1085 struct btrfs_key key;
1086 u64 bytenr, num_bytes;
1089 root = root->fs_info->tree_root;
1092 key.type = BTRFS_EXTENT_DATA_KEY;
1095 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1097 fprintf(stderr, "Error searching for free space inode %d\n",
1103 leaf = path->nodes[0];
1104 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1105 ret = btrfs_next_leaf(root, path);
1107 fprintf(stderr, "Error going to next leaf "
1113 leaf = path->nodes[0];
1116 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1117 if (key.type != BTRFS_EXTENT_DATA_KEY) {
1122 fi = btrfs_item_ptr(leaf, path->slots[0],
1123 struct btrfs_file_extent_item);
1124 if (btrfs_file_extent_type(leaf, fi) !=
1125 BTRFS_FILE_EXTENT_REG) {
1130 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1131 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1132 ret = add_extent(bytenr, num_bytes, metadump, 1);
1134 fprintf(stderr, "Error adding space cache blocks %d\n",
1136 btrfs_release_path(path);
1145 static int copy_from_extent_tree(struct metadump_struct *metadump,
1146 struct btrfs_path *path)
1148 struct btrfs_root *extent_root;
1149 struct extent_buffer *leaf;
1150 struct btrfs_extent_item *ei;
1151 struct btrfs_key key;
1156 extent_root = metadump->root->fs_info->extent_root;
1157 bytenr = BTRFS_SUPER_INFO_OFFSET + 4096;
1158 key.objectid = bytenr;
1159 key.type = BTRFS_EXTENT_ITEM_KEY;
1162 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1164 fprintf(stderr, "Error searching extent root %d\n", ret);
1170 leaf = path->nodes[0];
1171 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1172 ret = btrfs_next_leaf(extent_root, path);
1174 fprintf(stderr, "Error going to next leaf %d"
1182 leaf = path->nodes[0];
1185 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1186 if (key.objectid < bytenr ||
1187 (key.type != BTRFS_EXTENT_ITEM_KEY &&
1188 key.type != BTRFS_METADATA_ITEM_KEY)) {
1193 bytenr = key.objectid;
1194 if (key.type == BTRFS_METADATA_ITEM_KEY)
1195 num_bytes = extent_root->leafsize;
1197 num_bytes = key.offset;
1199 if (btrfs_item_size_nr(leaf, path->slots[0]) > sizeof(*ei)) {
1200 ei = btrfs_item_ptr(leaf, path->slots[0],
1201 struct btrfs_extent_item);
1202 if (btrfs_extent_flags(leaf, ei) &
1203 BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1204 ret = add_extent(bytenr, num_bytes, metadump,
1207 fprintf(stderr, "Error adding block "
1213 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1214 ret = is_tree_block(extent_root, path, bytenr);
1216 fprintf(stderr, "Error checking tree block "
1222 ret = add_extent(bytenr, num_bytes, metadump,
1225 fprintf(stderr, "Error adding block "
1232 fprintf(stderr, "Either extent tree corruption or "
1233 "you haven't built with V0 support\n");
1238 bytenr += num_bytes;
1241 btrfs_release_path(path);
1246 static int create_metadump(const char *input, FILE *out, int num_threads,
1247 int compress_level, int sanitize, int walk_trees)
1249 struct btrfs_root *root;
1250 struct btrfs_path *path = NULL;
1251 struct metadump_struct metadump;
1255 root = open_ctree(input, 0, 0);
1257 fprintf(stderr, "Open ctree failed\n");
1261 BUG_ON(root->nodesize != root->leafsize);
1263 ret = metadump_init(&metadump, root, out, num_threads,
1264 compress_level, sanitize);
1266 fprintf(stderr, "Error initing metadump %d\n", ret);
1271 ret = add_extent(BTRFS_SUPER_INFO_OFFSET, 4096, &metadump, 0);
1273 fprintf(stderr, "Error adding metadata %d\n", ret);
1278 path = btrfs_alloc_path();
1280 fprintf(stderr, "Out of memory allocing path\n");
1286 ret = copy_tree_blocks(root, root->fs_info->chunk_root->node,
1293 ret = copy_tree_blocks(root, root->fs_info->tree_root->node,
1300 ret = copy_from_extent_tree(&metadump, path);
1307 ret = copy_log_trees(root, &metadump, path);
1313 ret = copy_space_cache(root, &metadump, path);
1315 ret = flush_pending(&metadump, 1);
1319 fprintf(stderr, "Error flushing pending %d\n", ret);
1322 metadump_destroy(&metadump);
1324 btrfs_free_path(path);
1325 ret = close_ctree(root);
1326 return err ? err : ret;
1329 static void update_super_old(u8 *buffer)
1331 struct btrfs_super_block *super = (struct btrfs_super_block *)buffer;
1332 struct btrfs_chunk *chunk;
1333 struct btrfs_disk_key *key;
1334 u32 sectorsize = btrfs_super_sectorsize(super);
1335 u64 flags = btrfs_super_flags(super);
1337 flags |= BTRFS_SUPER_FLAG_METADUMP;
1338 btrfs_set_super_flags(super, flags);
1340 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1341 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1342 sizeof(struct btrfs_disk_key));
1344 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1345 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1346 btrfs_set_disk_key_offset(key, 0);
1348 btrfs_set_stack_chunk_length(chunk, (u64)-1);
1349 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1350 btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024);
1351 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1352 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1353 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1354 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1355 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1356 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1357 chunk->stripe.devid = super->dev_item.devid;
1358 btrfs_set_stack_stripe_offset(&chunk->stripe, 0);
1359 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1360 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1361 csum_block(buffer, 4096);
1364 static int update_super(u8 *buffer)
1366 struct btrfs_super_block *super = (struct btrfs_super_block *)buffer;
1367 struct btrfs_chunk *chunk;
1368 struct btrfs_disk_key *disk_key;
1369 struct btrfs_key key;
1370 u32 new_array_size = 0;
1374 u8 *ptr, *write_ptr;
1375 int old_num_stripes;
1377 write_ptr = ptr = super->sys_chunk_array;
1378 array_size = btrfs_super_sys_array_size(super);
1380 while (cur < array_size) {
1381 disk_key = (struct btrfs_disk_key *)ptr;
1382 btrfs_disk_key_to_cpu(&key, disk_key);
1384 new_array_size += sizeof(*disk_key);
1385 memmove(write_ptr, ptr, sizeof(*disk_key));
1387 write_ptr += sizeof(*disk_key);
1388 ptr += sizeof(*disk_key);
1389 cur += sizeof(*disk_key);
1390 new_cur += sizeof(*disk_key);
1392 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1393 chunk = (struct btrfs_chunk *)ptr;
1394 old_num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1395 chunk = (struct btrfs_chunk *)write_ptr;
1397 memmove(write_ptr, ptr, sizeof(*chunk));
1398 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1399 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1400 btrfs_set_stack_chunk_type(chunk,
1401 BTRFS_BLOCK_GROUP_SYSTEM);
1402 chunk->stripe.devid = super->dev_item.devid;
1403 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid,
1405 new_array_size += sizeof(*chunk);
1406 new_cur += sizeof(*chunk);
1408 fprintf(stderr, "Bogus key in the sys chunk array "
1412 write_ptr += sizeof(*chunk);
1413 ptr += btrfs_chunk_item_size(old_num_stripes);
1414 cur += btrfs_chunk_item_size(old_num_stripes);
1417 btrfs_set_super_sys_array_size(super, new_array_size);
1418 csum_block(buffer, 4096);
1423 static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size)
1425 struct extent_buffer *eb;
1427 eb = malloc(sizeof(struct extent_buffer) + size);
1430 memset(eb, 0, sizeof(struct extent_buffer) + size);
1437 static void truncate_item(struct extent_buffer *eb, int slot, u32 new_size)
1439 struct btrfs_item *item;
1447 old_size = btrfs_item_size_nr(eb, slot);
1448 if (old_size == new_size)
1451 nritems = btrfs_header_nritems(eb);
1452 data_end = btrfs_item_offset_nr(eb, nritems - 1);
1454 old_data_start = btrfs_item_offset_nr(eb, slot);
1455 size_diff = old_size - new_size;
1457 for (i = slot; i < nritems; i++) {
1459 item = btrfs_item_nr(eb, i);
1460 ioff = btrfs_item_offset(eb, item);
1461 btrfs_set_item_offset(eb, item, ioff + size_diff);
1464 memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end + size_diff,
1465 btrfs_leaf_data(eb) + data_end,
1466 old_data_start + new_size - data_end);
1467 item = btrfs_item_nr(eb, slot);
1468 btrfs_set_item_size(eb, item, new_size);
1471 static int fixup_chunk_tree_block(struct mdrestore_struct *mdres,
1472 struct async_work *async, u8 *buffer,
1475 struct extent_buffer *eb;
1476 size_t size_left = size;
1477 u64 bytenr = async->start;
1480 if (size_left % mdres->leafsize)
1483 eb = alloc_dummy_eb(bytenr, mdres->leafsize);
1489 memcpy(eb->data, buffer, mdres->leafsize);
1491 if (btrfs_header_bytenr(eb) != bytenr)
1493 if (memcmp(mdres->fsid,
1494 eb->data + offsetof(struct btrfs_header, fsid),
1498 if (btrfs_header_owner(eb) != BTRFS_CHUNK_TREE_OBJECTID)
1501 if (btrfs_header_level(eb) != 0)
1504 for (i = 0; i < btrfs_header_nritems(eb); i++) {
1505 struct btrfs_chunk chunk;
1506 struct btrfs_key key;
1509 btrfs_item_key_to_cpu(eb, &key, i);
1510 if (key.type != BTRFS_CHUNK_ITEM_KEY)
1512 truncate_item(eb, i, sizeof(chunk));
1513 read_extent_buffer(eb, &chunk,
1514 btrfs_item_ptr_offset(eb, i),
1517 /* Zero out the RAID profile */
1518 type = btrfs_stack_chunk_type(&chunk);
1519 type &= (BTRFS_BLOCK_GROUP_DATA |
1520 BTRFS_BLOCK_GROUP_SYSTEM |
1521 BTRFS_BLOCK_GROUP_METADATA);
1522 btrfs_set_stack_chunk_type(&chunk, type);
1524 btrfs_set_stack_chunk_num_stripes(&chunk, 1);
1525 btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
1526 btrfs_set_stack_stripe_devid(&chunk.stripe, mdres->devid);
1527 memcpy(chunk.stripe.dev_uuid, mdres->uuid,
1529 write_extent_buffer(eb, &chunk,
1530 btrfs_item_ptr_offset(eb, i),
1533 memcpy(buffer, eb->data, eb->len);
1534 csum_block(buffer, eb->len);
1536 size_left -= mdres->leafsize;
1537 buffer += mdres->leafsize;
1538 bytenr += mdres->leafsize;
1544 static void write_backup_supers(int fd, u8 *buf)
1546 struct btrfs_super_block *super = (struct btrfs_super_block *)buf;
1553 if (fstat(fd, &st)) {
1554 fprintf(stderr, "Couldn't stat restore point, won't be able "
1555 "to write backup supers: %d\n", errno);
1559 size = btrfs_device_size(fd, &st);
1561 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1562 bytenr = btrfs_sb_offset(i);
1563 if (bytenr + 4096 > size)
1565 btrfs_set_super_bytenr(super, bytenr);
1566 csum_block(buf, 4096);
1567 ret = pwrite64(fd, buf, 4096, bytenr);
1570 fprintf(stderr, "Problem writing out backup "
1571 "super block %d, err %d\n", i, errno);
1573 fprintf(stderr, "Short write writing out "
1574 "backup super block\n");
1580 static u64 logical_to_physical(struct mdrestore_struct *mdres, u64 logical, u64 *size)
1582 struct fs_chunk *fs_chunk;
1583 struct rb_node *entry;
1584 struct fs_chunk search;
1587 if (logical == BTRFS_SUPER_INFO_OFFSET)
1590 search.logical = logical;
1591 entry = tree_search(&mdres->chunk_tree, &search.n, chunk_cmp, 1);
1593 if (mdres->in != stdin)
1594 printf("Couldn't find a chunk, using logical\n");
1597 fs_chunk = rb_entry(entry, struct fs_chunk, n);
1598 if (fs_chunk->logical > logical || fs_chunk->logical + fs_chunk->bytes < logical)
1600 offset = search.logical - fs_chunk->logical;
1602 *size = min(*size, fs_chunk->bytes + fs_chunk->logical - logical);
1603 return fs_chunk->physical + offset;
1606 static void *restore_worker(void *data)
1608 struct mdrestore_struct *mdres = (struct mdrestore_struct *)data;
1609 struct async_work *async;
1615 int compress_size = MAX_PENDING_SIZE * 4;
1617 outfd = fileno(mdres->out);
1618 buffer = malloc(compress_size);
1620 fprintf(stderr, "Error allocing buffer\n");
1621 pthread_mutex_lock(&mdres->mutex);
1623 mdres->error = -ENOMEM;
1624 pthread_mutex_unlock(&mdres->mutex);
1633 pthread_mutex_lock(&mdres->mutex);
1634 while (!mdres->leafsize || list_empty(&mdres->list)) {
1636 pthread_mutex_unlock(&mdres->mutex);
1639 pthread_cond_wait(&mdres->cond, &mdres->mutex);
1641 async = list_entry(mdres->list.next, struct async_work, list);
1642 list_del_init(&async->list);
1643 pthread_mutex_unlock(&mdres->mutex);
1645 if (mdres->compress_method == COMPRESS_ZLIB) {
1646 size = compress_size;
1647 ret = uncompress(buffer, (unsigned long *)&size,
1648 async->buffer, async->bufsize);
1650 fprintf(stderr, "Error decompressing %d\n",
1656 outbuf = async->buffer;
1657 size = async->bufsize;
1660 if (!mdres->multi_devices) {
1661 if (async->start == BTRFS_SUPER_INFO_OFFSET) {
1662 if (mdres->old_restore) {
1663 update_super_old(outbuf);
1665 ret = update_super(outbuf);
1669 } else if (!mdres->old_restore) {
1670 ret = fixup_chunk_tree_block(mdres, async, outbuf, size);
1676 if (!mdres->fixup_offset) {
1678 u64 chunk_size = size;
1679 if (!mdres->multi_devices)
1680 bytenr = logical_to_physical(mdres,
1681 async->start + offset,
1684 bytenr = async->start + offset;
1686 ret = pwrite64(outfd, outbuf+offset, chunk_size,
1688 if (ret != chunk_size) {
1690 fprintf(stderr, "Error writing to "
1691 "device %d\n", errno);
1695 fprintf(stderr, "Short write\n");
1701 offset += chunk_size;
1703 } else if (async->start != BTRFS_SUPER_INFO_OFFSET) {
1704 ret = write_data_to_disk(mdres->info, outbuf, async->start, size, 0);
1706 printk("Error write data\n");
1712 /* backup super blocks are already there at fixup_offset stage */
1713 if (!mdres->multi_devices && async->start == BTRFS_SUPER_INFO_OFFSET)
1714 write_backup_supers(outfd, outbuf);
1716 pthread_mutex_lock(&mdres->mutex);
1717 if (err && !mdres->error)
1720 pthread_mutex_unlock(&mdres->mutex);
1722 free(async->buffer);
1730 static void mdrestore_destroy(struct mdrestore_struct *mdres)
1735 while ((n = rb_first(&mdres->chunk_tree))) {
1736 struct fs_chunk *entry;
1738 entry = rb_entry(n, struct fs_chunk, n);
1739 rb_erase(n, &mdres->chunk_tree);
1742 pthread_mutex_lock(&mdres->mutex);
1744 pthread_cond_broadcast(&mdres->cond);
1745 pthread_mutex_unlock(&mdres->mutex);
1747 for (i = 0; i < mdres->num_threads; i++)
1748 pthread_join(mdres->threads[i], NULL);
1750 pthread_cond_destroy(&mdres->cond);
1751 pthread_mutex_destroy(&mdres->mutex);
1752 free(mdres->threads);
1755 static int mdrestore_init(struct mdrestore_struct *mdres,
1756 FILE *in, FILE *out, int old_restore,
1757 int num_threads, int fixup_offset,
1758 struct btrfs_fs_info *info, int multi_devices)
1762 memset(mdres, 0, sizeof(*mdres));
1763 pthread_cond_init(&mdres->cond, NULL);
1764 pthread_mutex_init(&mdres->mutex, NULL);
1765 INIT_LIST_HEAD(&mdres->list);
1768 mdres->old_restore = old_restore;
1769 mdres->chunk_tree.rb_node = NULL;
1770 mdres->fixup_offset = fixup_offset;
1772 mdres->multi_devices = multi_devices;
1777 mdres->num_threads = num_threads;
1778 mdres->threads = calloc(num_threads, sizeof(pthread_t));
1779 if (!mdres->threads)
1781 for (i = 0; i < num_threads; i++) {
1782 ret = pthread_create(mdres->threads + i, NULL, restore_worker,
1788 mdrestore_destroy(mdres);
1792 static int fill_mdres_info(struct mdrestore_struct *mdres,
1793 struct async_work *async)
1795 struct btrfs_super_block *super;
1800 /* We've already been initialized */
1801 if (mdres->leafsize)
1804 if (mdres->compress_method == COMPRESS_ZLIB) {
1805 size_t size = MAX_PENDING_SIZE * 2;
1807 buffer = malloc(MAX_PENDING_SIZE * 2);
1810 ret = uncompress(buffer, (unsigned long *)&size,
1811 async->buffer, async->bufsize);
1813 fprintf(stderr, "Error decompressing %d\n", ret);
1819 outbuf = async->buffer;
1822 super = (struct btrfs_super_block *)outbuf;
1823 mdres->leafsize = btrfs_super_leafsize(super);
1824 memcpy(mdres->fsid, super->fsid, BTRFS_FSID_SIZE);
1825 memcpy(mdres->uuid, super->dev_item.uuid,
1827 mdres->devid = le64_to_cpu(super->dev_item.devid);
1832 static int add_cluster(struct meta_cluster *cluster,
1833 struct mdrestore_struct *mdres, u64 *next)
1835 struct meta_cluster_item *item;
1836 struct meta_cluster_header *header = &cluster->header;
1837 struct async_work *async;
1842 BUG_ON(mdres->num_items);
1843 mdres->compress_method = header->compress;
1845 bytenr = le64_to_cpu(header->bytenr) + BLOCK_SIZE;
1846 nritems = le32_to_cpu(header->nritems);
1847 for (i = 0; i < nritems; i++) {
1848 item = &cluster->items[i];
1849 async = calloc(1, sizeof(*async));
1851 fprintf(stderr, "Error allocating async\n");
1854 async->start = le64_to_cpu(item->bytenr);
1855 async->bufsize = le32_to_cpu(item->size);
1856 async->buffer = malloc(async->bufsize);
1857 if (!async->buffer) {
1858 fprintf(stderr, "Error allocing async buffer\n");
1862 ret = fread(async->buffer, async->bufsize, 1, mdres->in);
1864 fprintf(stderr, "Error reading buffer %d\n", errno);
1865 free(async->buffer);
1869 bytenr += async->bufsize;
1871 pthread_mutex_lock(&mdres->mutex);
1872 if (async->start == BTRFS_SUPER_INFO_OFFSET) {
1873 ret = fill_mdres_info(mdres, async);
1875 fprintf(stderr, "Error setting up restore\n");
1876 pthread_mutex_unlock(&mdres->mutex);
1877 free(async->buffer);
1882 list_add_tail(&async->list, &mdres->list);
1884 pthread_cond_signal(&mdres->cond);
1885 pthread_mutex_unlock(&mdres->mutex);
1887 if (bytenr & BLOCK_MASK) {
1888 char buffer[BLOCK_MASK];
1889 size_t size = BLOCK_SIZE - (bytenr & BLOCK_MASK);
1892 ret = fread(buffer, size, 1, mdres->in);
1894 fprintf(stderr, "Error reading in buffer %d\n", errno);
1902 static int wait_for_worker(struct mdrestore_struct *mdres)
1906 pthread_mutex_lock(&mdres->mutex);
1908 while (!ret && mdres->num_items > 0) {
1909 struct timespec ts = {
1911 .tv_nsec = 10000000,
1913 pthread_mutex_unlock(&mdres->mutex);
1914 nanosleep(&ts, NULL);
1915 pthread_mutex_lock(&mdres->mutex);
1918 pthread_mutex_unlock(&mdres->mutex);
1922 static int read_chunk_block(struct mdrestore_struct *mdres, u8 *buffer,
1923 u64 bytenr, u64 item_bytenr, u32 bufsize,
1926 struct extent_buffer *eb;
1930 eb = alloc_dummy_eb(bytenr, mdres->leafsize);
1936 while (item_bytenr != bytenr) {
1937 buffer += mdres->leafsize;
1938 item_bytenr += mdres->leafsize;
1941 memcpy(eb->data, buffer, mdres->leafsize);
1942 if (btrfs_header_bytenr(eb) != bytenr) {
1943 fprintf(stderr, "Eb bytenr doesn't match found bytenr\n");
1948 if (memcmp(mdres->fsid, eb->data + offsetof(struct btrfs_header, fsid),
1950 fprintf(stderr, "Fsid doesn't match\n");
1955 if (btrfs_header_owner(eb) != BTRFS_CHUNK_TREE_OBJECTID) {
1956 fprintf(stderr, "Does not belong to the chunk tree\n");
1961 for (i = 0; i < btrfs_header_nritems(eb); i++) {
1962 struct btrfs_chunk chunk;
1963 struct fs_chunk *fs_chunk;
1964 struct btrfs_key key;
1966 if (btrfs_header_level(eb)) {
1967 u64 blockptr = btrfs_node_blockptr(eb, i);
1969 ret = search_for_chunk_blocks(mdres, blockptr,
1976 /* Yay a leaf! We loves leafs! */
1977 btrfs_item_key_to_cpu(eb, &key, i);
1978 if (key.type != BTRFS_CHUNK_ITEM_KEY)
1981 fs_chunk = malloc(sizeof(struct fs_chunk));
1983 fprintf(stderr, "Erorr allocating chunk\n");
1987 memset(fs_chunk, 0, sizeof(*fs_chunk));
1988 read_extent_buffer(eb, &chunk, btrfs_item_ptr_offset(eb, i),
1991 fs_chunk->logical = key.offset;
1992 fs_chunk->physical = btrfs_stack_stripe_offset(&chunk.stripe);
1993 fs_chunk->bytes = btrfs_stack_chunk_length(&chunk);
1994 tree_insert(&mdres->chunk_tree, &fs_chunk->n, chunk_cmp);
2001 /* If you have to ask you aren't worthy */
2002 static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
2003 u64 search, u64 cluster_bytenr)
2005 struct meta_cluster *cluster;
2006 struct meta_cluster_header *header;
2007 struct meta_cluster_item *item;
2008 u64 current_cluster = cluster_bytenr, bytenr;
2010 u32 bufsize, nritems, i;
2011 u8 *buffer, *tmp = NULL;
2014 cluster = malloc(BLOCK_SIZE);
2016 fprintf(stderr, "Error allocating cluster\n");
2020 buffer = malloc(MAX_PENDING_SIZE * 2);
2022 fprintf(stderr, "Error allocing buffer\n");
2027 if (mdres->compress_method == COMPRESS_ZLIB) {
2028 tmp = malloc(MAX_PENDING_SIZE * 2);
2030 fprintf(stderr, "Error allocing tmp buffer\n");
2037 bytenr = current_cluster;
2039 if (fseek(mdres->in, current_cluster, SEEK_SET)) {
2040 fprintf(stderr, "Error seeking: %d\n", errno);
2045 ret = fread(cluster, BLOCK_SIZE, 1, mdres->in);
2047 if (cluster_bytenr != 0) {
2049 current_cluster = 0;
2053 printf("ok this is where we screwed up?\n");
2056 } else if (ret < 0) {
2057 fprintf(stderr, "Error reading image\n");
2062 header = &cluster->header;
2063 if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
2064 le64_to_cpu(header->bytenr) != current_cluster) {
2065 fprintf(stderr, "bad header in metadump image\n");
2070 bytenr += BLOCK_SIZE;
2071 nritems = le32_to_cpu(header->nritems);
2072 for (i = 0; i < nritems; i++) {
2075 item = &cluster->items[i];
2076 bufsize = le32_to_cpu(item->size);
2077 item_bytenr = le64_to_cpu(item->bytenr);
2079 if (mdres->compress_method == COMPRESS_ZLIB) {
2080 ret = fread(tmp, bufsize, 1, mdres->in);
2082 fprintf(stderr, "Error reading: %d\n",
2088 size = MAX_PENDING_SIZE * 2;
2089 ret = uncompress(buffer,
2090 (unsigned long *)&size, tmp,
2093 fprintf(stderr, "Error decompressing "
2099 ret = fread(buffer, bufsize, 1, mdres->in);
2101 fprintf(stderr, "Error reading: %d\n",
2110 if (item_bytenr <= search &&
2111 item_bytenr + size > search) {
2112 ret = read_chunk_block(mdres, buffer, search,
2126 if (bytenr & BLOCK_MASK)
2127 bytenr += BLOCK_SIZE - (bytenr & BLOCK_MASK);
2128 current_cluster = bytenr;
2137 static int build_chunk_tree(struct mdrestore_struct *mdres,
2138 struct meta_cluster *cluster)
2140 struct btrfs_super_block *super;
2141 struct meta_cluster_header *header;
2142 struct meta_cluster_item *item = NULL;
2143 u64 chunk_root_bytenr = 0;
2149 /* We can't seek with stdin so don't bother doing this */
2150 if (mdres->in == stdin)
2153 ret = fread(cluster, BLOCK_SIZE, 1, mdres->in);
2155 fprintf(stderr, "Error reading in cluster: %d\n", errno);
2160 header = &cluster->header;
2161 if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
2162 le64_to_cpu(header->bytenr) != 0) {
2163 fprintf(stderr, "bad header in metadump image\n");
2167 bytenr += BLOCK_SIZE;
2168 mdres->compress_method = header->compress;
2169 nritems = le32_to_cpu(header->nritems);
2170 for (i = 0; i < nritems; i++) {
2171 item = &cluster->items[i];
2173 if (le64_to_cpu(item->bytenr) == BTRFS_SUPER_INFO_OFFSET)
2175 bytenr += le32_to_cpu(item->size);
2176 if (fseek(mdres->in, le32_to_cpu(item->size), SEEK_CUR)) {
2177 fprintf(stderr, "Error seeking: %d\n", errno);
2182 if (!item || le64_to_cpu(item->bytenr) != BTRFS_SUPER_INFO_OFFSET) {
2183 fprintf(stderr, "Huh, didn't find the super?\n");
2187 buffer = malloc(le32_to_cpu(item->size));
2189 fprintf(stderr, "Error allocing buffer\n");
2193 ret = fread(buffer, le32_to_cpu(item->size), 1, mdres->in);
2195 fprintf(stderr, "Error reading buffer: %d\n", errno);
2200 if (mdres->compress_method == COMPRESS_ZLIB) {
2201 size_t size = MAX_PENDING_SIZE * 2;
2204 tmp = malloc(MAX_PENDING_SIZE * 2);
2209 ret = uncompress(tmp, (unsigned long *)&size,
2210 buffer, le32_to_cpu(item->size));
2212 fprintf(stderr, "Error decompressing %d\n", ret);
2221 super = (struct btrfs_super_block *)buffer;
2222 chunk_root_bytenr = btrfs_super_chunk_root(super);
2223 mdres->leafsize = btrfs_super_leafsize(super);
2224 memcpy(mdres->fsid, super->fsid, BTRFS_FSID_SIZE);
2225 memcpy(mdres->uuid, super->dev_item.uuid,
2227 mdres->devid = le64_to_cpu(super->dev_item.devid);
2230 return search_for_chunk_blocks(mdres, chunk_root_bytenr, 0);
2233 static int __restore_metadump(const char *input, FILE *out, int old_restore,
2234 int num_threads, int fixup_offset,
2235 const char *target, int multi_devices)
2237 struct meta_cluster *cluster = NULL;
2238 struct meta_cluster_header *header;
2239 struct mdrestore_struct mdrestore;
2240 struct btrfs_fs_info *info = NULL;
2245 if (!strcmp(input, "-")) {
2248 in = fopen(input, "r");
2250 perror("unable to open metadump image");
2255 /* NOTE: open with write mode */
2258 info = open_ctree_fs_info_restore(target, 0, 0, 1, 1);
2260 fprintf(stderr, "%s: open ctree failed\n", __func__);
2266 cluster = malloc(BLOCK_SIZE);
2268 fprintf(stderr, "Error allocating cluster\n");
2273 ret = mdrestore_init(&mdrestore, in, out, old_restore, num_threads,
2274 fixup_offset, info, multi_devices);
2276 fprintf(stderr, "Error initing mdrestore %d\n", ret);
2277 goto failed_cluster;
2280 if (!multi_devices) {
2281 ret = build_chunk_tree(&mdrestore, cluster);
2286 if (in != stdin && fseek(in, 0, SEEK_SET)) {
2287 fprintf(stderr, "Error seeking %d\n", errno);
2292 ret = fread(cluster, BLOCK_SIZE, 1, in);
2296 header = &cluster->header;
2297 if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
2298 le64_to_cpu(header->bytenr) != bytenr) {
2299 fprintf(stderr, "bad header in metadump image\n");
2303 ret = add_cluster(cluster, &mdrestore, &bytenr);
2305 fprintf(stderr, "Error adding cluster\n");
2309 ret = wait_for_worker(&mdrestore);
2311 fprintf(stderr, "One of the threads errored out %d\n",
2317 mdrestore_destroy(&mdrestore);
2321 if (fixup_offset && info)
2322 close_ctree(info->chunk_root);
2329 static int restore_metadump(const char *input, FILE *out, int old_restore,
2330 int num_threads, int multi_devices)
2332 return __restore_metadump(input, out, old_restore, num_threads, 0, NULL,
2336 static int fixup_metadump(const char *input, FILE *out, int num_threads,
2339 return __restore_metadump(input, out, 0, num_threads, 1, target, 1);
2342 static int update_disk_super_on_device(struct btrfs_fs_info *info,
2343 const char *other_dev, u64 cur_devid)
2345 struct btrfs_key key;
2346 struct extent_buffer *leaf;
2347 struct btrfs_path path;
2348 struct btrfs_dev_item *dev_item;
2349 struct btrfs_super_block *disk_super;
2350 char dev_uuid[BTRFS_UUID_SIZE];
2351 char fs_uuid[BTRFS_UUID_SIZE];
2352 u64 devid, type, io_align, io_width;
2353 u64 sector_size, total_bytes, bytes_used;
2358 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2359 key.type = BTRFS_DEV_ITEM_KEY;
2360 key.offset = cur_devid;
2362 btrfs_init_path(&path);
2363 ret = btrfs_search_slot(NULL, info->chunk_root, &key, &path, 0, 0);
2365 fprintf(stderr, "search key fails\n");
2369 leaf = path.nodes[0];
2370 dev_item = btrfs_item_ptr(leaf, path.slots[0],
2371 struct btrfs_dev_item);
2373 devid = btrfs_device_id(leaf, dev_item);
2374 if (devid != cur_devid) {
2375 printk("devid %llu mismatch with %llu\n", devid, cur_devid);
2379 type = btrfs_device_type(leaf, dev_item);
2380 io_align = btrfs_device_io_align(leaf, dev_item);
2381 io_width = btrfs_device_io_width(leaf, dev_item);
2382 sector_size = btrfs_device_sector_size(leaf, dev_item);
2383 total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2384 bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2385 read_extent_buffer(leaf, dev_uuid, (unsigned long)btrfs_device_uuid(dev_item), BTRFS_UUID_SIZE);
2386 read_extent_buffer(leaf, fs_uuid, (unsigned long)btrfs_device_fsid(dev_item), BTRFS_UUID_SIZE);
2388 btrfs_release_path(&path);
2390 printk("update disk super on %s devid=%llu\n", other_dev, devid);
2392 /* update other devices' super block */
2393 fp = open(other_dev, O_CREAT | O_RDWR, 0600);
2395 fprintf(stderr, "could not open %s\n", other_dev);
2399 buf = malloc(BTRFS_SUPER_INFO_SIZE);
2405 memcpy(buf, info->super_copy, BTRFS_SUPER_INFO_SIZE);
2407 disk_super = (struct btrfs_super_block *)buf;
2408 dev_item = &disk_super->dev_item;
2410 btrfs_set_stack_device_type(dev_item, type);
2411 btrfs_set_stack_device_id(dev_item, devid);
2412 btrfs_set_stack_device_total_bytes(dev_item, total_bytes);
2413 btrfs_set_stack_device_bytes_used(dev_item, bytes_used);
2414 btrfs_set_stack_device_io_align(dev_item, io_align);
2415 btrfs_set_stack_device_io_width(dev_item, io_width);
2416 btrfs_set_stack_device_sector_size(dev_item, sector_size);
2417 memcpy(dev_item->uuid, dev_uuid, BTRFS_UUID_SIZE);
2418 memcpy(dev_item->fsid, fs_uuid, BTRFS_UUID_SIZE);
2419 csum_block((u8 *)buf, BTRFS_SUPER_INFO_SIZE);
2421 ret = pwrite64(fp, buf, BTRFS_SUPER_INFO_SIZE, BTRFS_SUPER_INFO_OFFSET);
2422 if (ret != BTRFS_SUPER_INFO_SIZE) {
2427 write_backup_supers(fp, (u8 *)buf);
2435 static void print_usage(void)
2437 fprintf(stderr, "usage: btrfs-image [options] source target\n");
2438 fprintf(stderr, "\t-r \trestore metadump image\n");
2439 fprintf(stderr, "\t-c value\tcompression level (0 ~ 9)\n");
2440 fprintf(stderr, "\t-t value\tnumber of threads (1 ~ 32)\n");
2441 fprintf(stderr, "\t-o \tdon't mess with the chunk tree when restoring\n");
2442 fprintf(stderr, "\t-s \tsanitize file names, use once to just use garbage, use twice if you want crc collisions\n");
2443 fprintf(stderr, "\t-w \twalk all trees instead of using extent tree, do this if your extent tree is broken\n");
2447 int main(int argc, char *argv[])
2451 int num_threads = 0;
2452 int compress_level = 0;
2454 int old_restore = 0;
2456 int multi_devices = 0;
2463 int c = getopt(argc, argv, "rc:t:oswm");
2471 num_threads = atoi(optarg);
2472 if (num_threads <= 0 || num_threads > 32)
2476 compress_level = atoi(optarg);
2477 if (compress_level < 0 || compress_level > 9)
2498 if ((old_restore) && create)
2501 argc = argc - optind;
2504 if (multi_devices && dev_cnt < 2)
2506 if (!multi_devices && dev_cnt != 1)
2509 source = argv[optind];
2510 target = argv[optind + 1];
2512 if (create && !strcmp(target, "-")) {
2515 out = fopen(target, "w+");
2517 perror("unable to create target file");
2522 if (num_threads == 0 && compress_level > 0) {
2523 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
2524 if (num_threads <= 0)
2529 ret = create_metadump(source, out, num_threads,
2530 compress_level, sanitize, walk_trees);
2532 ret = restore_metadump(source, out, old_restore, 1,
2535 printk("%s failed (%s)\n", (create) ? "create" : "restore",
2540 /* extended support for multiple devices */
2541 if (!create && multi_devices) {
2542 struct btrfs_fs_info *info;
2546 info = open_ctree_fs_info_restore(target, 0, 0, 0, 1);
2549 fprintf(stderr, "unable to open %s error = %s\n",
2550 target, strerror(e));
2554 total_devs = btrfs_super_num_devices(info->super_copy);
2555 if (total_devs != dev_cnt) {
2556 printk("it needs %llu devices but has only %d\n",
2557 total_devs, dev_cnt);
2558 close_ctree(info->chunk_root);
2562 /* update super block on other disks */
2563 for (i = 2; i <= dev_cnt; i++) {
2564 ret = update_disk_super_on_device(info,
2565 argv[optind + i], (u64)i);
2567 printk("update disk super failed devid=%d (error=%d)\n",
2569 close_ctree(info->chunk_root);
2574 close_ctree(info->chunk_root);
2576 /* fix metadata block to map correct chunk */
2577 ret = fixup_metadump(source, out, 1, target);
2579 fprintf(stderr, "fix metadump failed (error=%d)\n",