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:
173 static char *generate_garbage(u32 name_len)
175 char *buf = malloc(name_len);
181 for (i = 0; i < name_len; i++) {
182 char c = rand() % 94 + 33;
192 static int name_cmp(struct rb_node *a, struct rb_node *b, int fuzz)
194 struct name *entry = rb_entry(a, struct name, n);
195 struct name *ins = rb_entry(b, struct name, n);
198 len = min(ins->len, entry->len);
199 return memcmp(ins->val, entry->val, len);
202 static int chunk_cmp(struct rb_node *a, struct rb_node *b, int fuzz)
204 struct fs_chunk *entry = rb_entry(a, struct fs_chunk, n);
205 struct fs_chunk *ins = rb_entry(b, struct fs_chunk, n);
207 if (fuzz && ins->logical >= entry->logical &&
208 ins->logical < entry->logical + entry->bytes)
211 if (ins->logical < entry->logical)
213 else if (ins->logical > entry->logical)
218 static void tree_insert(struct rb_root *root, struct rb_node *ins,
219 int (*cmp)(struct rb_node *a, struct rb_node *b,
222 struct rb_node ** p = &root->rb_node;
223 struct rb_node * parent = NULL;
229 dir = cmp(*p, ins, 0);
238 rb_link_node(ins, parent, p);
239 rb_insert_color(ins, root);
242 static struct rb_node *tree_search(struct rb_root *root,
243 struct rb_node *search,
244 int (*cmp)(struct rb_node *a,
245 struct rb_node *b, int fuzz),
248 struct rb_node *n = root->rb_node;
252 dir = cmp(n, search, fuzz);
264 static char *find_collision(struct metadump_struct *md, char *name,
268 struct rb_node *entry;
270 unsigned long checksum;
276 entry = tree_search(&md->name_tree, &tmp.n, name_cmp, 0);
278 val = rb_entry(entry, struct name, n);
283 val = malloc(sizeof(struct name));
285 fprintf(stderr, "Couldn't sanitize name, enomem\n");
289 memset(val, 0, sizeof(*val));
293 val->sub = malloc(name_len);
295 fprintf(stderr, "Couldn't sanitize name, enomem\n");
300 checksum = crc32c(~1, val->val, name_len);
301 memset(val->sub, ' ', name_len);
304 if (crc32c(~1, val->sub, name_len) == checksum &&
305 memcmp(val->sub, val->val, val->len)) {
310 if (val->sub[i] == 127) {
315 } while (val->sub[i] == 127);
320 if (val->sub[i] == '/')
322 memset(val->sub, ' ', i);
327 if (val->sub[i] == '/')
333 fprintf(stderr, "Couldn't find a collision for '%.*s', "
334 "generating normal garbage, it won't match indexes\n",
336 for (i = 0; i < name_len; i++) {
337 char c = rand() % 94 + 33;
345 tree_insert(&md->name_tree, &val->n, name_cmp);
349 static void sanitize_dir_item(struct metadump_struct *md, struct extent_buffer *eb,
352 struct btrfs_dir_item *dir_item;
355 unsigned long name_ptr;
360 int free_garbage = (md->sanitize_names == 1);
362 dir_item = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
363 total_len = btrfs_item_size_nr(eb, slot);
364 while (cur < total_len) {
365 this_len = sizeof(*dir_item) +
366 btrfs_dir_name_len(eb, dir_item) +
367 btrfs_dir_data_len(eb, dir_item);
368 name_ptr = (unsigned long)(dir_item + 1);
369 name_len = btrfs_dir_name_len(eb, dir_item);
371 if (md->sanitize_names > 1) {
372 buf = malloc(name_len);
374 fprintf(stderr, "Couldn't sanitize name, "
378 read_extent_buffer(eb, buf, name_ptr, name_len);
379 garbage = find_collision(md, buf, name_len);
381 garbage = generate_garbage(name_len);
384 fprintf(stderr, "Couldn't sanitize name, enomem\n");
387 write_extent_buffer(eb, garbage, name_ptr, name_len);
389 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
396 static void sanitize_inode_ref(struct metadump_struct *md,
397 struct extent_buffer *eb, int slot, int ext)
399 struct btrfs_inode_extref *extref;
400 struct btrfs_inode_ref *ref;
403 unsigned long name_ptr;
407 int free_garbage = (md->sanitize_names == 1);
409 item_size = btrfs_item_size_nr(eb, slot);
410 ptr = btrfs_item_ptr_offset(eb, slot);
411 while (cur_offset < item_size) {
413 extref = (struct btrfs_inode_extref *)(ptr +
415 name_ptr = (unsigned long)(&extref->name);
416 len = btrfs_inode_extref_name_len(eb, extref);
417 cur_offset += sizeof(*extref);
419 ref = (struct btrfs_inode_ref *)(ptr + cur_offset);
420 len = btrfs_inode_ref_name_len(eb, ref);
421 name_ptr = (unsigned long)(ref + 1);
422 cur_offset += sizeof(*ref);
426 if (md->sanitize_names > 1) {
429 fprintf(stderr, "Couldn't sanitize name, "
433 read_extent_buffer(eb, buf, name_ptr, len);
434 garbage = find_collision(md, buf, len);
436 garbage = generate_garbage(len);
440 fprintf(stderr, "Couldn't sanitize name, enomem\n");
443 write_extent_buffer(eb, garbage, name_ptr, len);
449 static void sanitize_name(struct metadump_struct *md, u8 *dst,
450 struct extent_buffer *src, struct btrfs_key *key,
453 struct extent_buffer *eb;
455 eb = alloc_dummy_eb(src->start, src->len);
457 fprintf(stderr, "Couldn't sanitize name, no memory\n");
461 memcpy(eb->data, dst, eb->len);
464 case BTRFS_DIR_ITEM_KEY:
465 case BTRFS_DIR_INDEX_KEY:
466 sanitize_dir_item(md, eb, slot);
468 case BTRFS_INODE_REF_KEY:
469 sanitize_inode_ref(md, eb, slot, 0);
471 case BTRFS_INODE_EXTREF_KEY:
472 sanitize_inode_ref(md, eb, slot, 1);
478 memcpy(dst, eb->data, eb->len);
483 * zero inline extents and csum items
485 static void zero_items(struct metadump_struct *md, u8 *dst,
486 struct extent_buffer *src)
488 struct btrfs_file_extent_item *fi;
489 struct btrfs_item *item;
490 struct btrfs_key key;
491 u32 nritems = btrfs_header_nritems(src);
496 for (i = 0; i < nritems; i++) {
497 item = btrfs_item_nr(src, i);
498 btrfs_item_key_to_cpu(src, &key, i);
499 if (key.type == BTRFS_CSUM_ITEM_KEY) {
500 size = btrfs_item_size_nr(src, i);
501 memset(dst + btrfs_leaf_data(src) +
502 btrfs_item_offset_nr(src, i), 0, size);
506 if (md->sanitize_names && has_name(&key)) {
507 sanitize_name(md, dst, src, &key, i);
511 if (key.type != BTRFS_EXTENT_DATA_KEY)
514 fi = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
515 extent_type = btrfs_file_extent_type(src, fi);
516 if (extent_type != BTRFS_FILE_EXTENT_INLINE)
519 ptr = btrfs_file_extent_inline_start(fi);
520 size = btrfs_file_extent_inline_item_len(src, item);
521 memset(dst + ptr, 0, size);
526 * copy buffer and zero useless data in the buffer
528 static void copy_buffer(struct metadump_struct *md, u8 *dst,
529 struct extent_buffer *src)
535 memcpy(dst, src->data, src->len);
536 if (src->start == BTRFS_SUPER_INFO_OFFSET)
539 level = btrfs_header_level(src);
540 nritems = btrfs_header_nritems(src);
543 size = sizeof(struct btrfs_header);
544 memset(dst + size, 0, src->len - size);
545 } else if (level == 0) {
546 size = btrfs_leaf_data(src) +
547 btrfs_item_offset_nr(src, nritems - 1) -
548 btrfs_item_nr_offset(nritems);
549 memset(dst + btrfs_item_nr_offset(nritems), 0, size);
550 zero_items(md, dst, src);
552 size = offsetof(struct btrfs_node, ptrs) +
553 sizeof(struct btrfs_key_ptr) * nritems;
554 memset(dst + size, 0, src->len - size);
556 csum_block(dst, src->len);
559 static void *dump_worker(void *data)
561 struct metadump_struct *md = (struct metadump_struct *)data;
562 struct async_work *async;
566 pthread_mutex_lock(&md->mutex);
567 while (list_empty(&md->list)) {
569 pthread_mutex_unlock(&md->mutex);
572 pthread_cond_wait(&md->cond, &md->mutex);
574 async = list_entry(md->list.next, struct async_work, list);
575 list_del_init(&async->list);
576 pthread_mutex_unlock(&md->mutex);
578 if (md->compress_level > 0) {
579 u8 *orig = async->buffer;
581 async->bufsize = compressBound(async->size);
582 async->buffer = malloc(async->bufsize);
584 ret = compress2(async->buffer,
585 (unsigned long *)&async->bufsize,
586 orig, async->size, md->compress_level);
594 pthread_mutex_lock(&md->mutex);
596 pthread_mutex_unlock(&md->mutex);
602 static void meta_cluster_init(struct metadump_struct *md, u64 start)
604 struct meta_cluster_header *header;
608 header = &md->cluster->header;
609 header->magic = cpu_to_le64(HEADER_MAGIC);
610 header->bytenr = cpu_to_le64(start);
611 header->nritems = cpu_to_le32(0);
612 header->compress = md->compress_level > 0 ?
613 COMPRESS_ZLIB : COMPRESS_NONE;
616 static int metadump_init(struct metadump_struct *md, struct btrfs_root *root,
617 FILE *out, int num_threads, int compress_level,
622 memset(md, 0, sizeof(*md));
623 pthread_cond_init(&md->cond, NULL);
624 pthread_mutex_init(&md->mutex, NULL);
625 INIT_LIST_HEAD(&md->list);
626 INIT_LIST_HEAD(&md->ordered);
629 md->pending_start = (u64)-1;
630 md->compress_level = compress_level;
631 md->cluster = calloc(1, BLOCK_SIZE);
632 md->sanitize_names = sanitize_names;
633 if (sanitize_names > 1)
634 crc32c_optimization_init();
637 pthread_cond_destroy(&md->cond);
638 pthread_mutex_destroy(&md->mutex);
642 meta_cluster_init(md, 0);
646 md->name_tree.rb_node = NULL;
647 md->num_threads = num_threads;
648 md->threads = calloc(num_threads, sizeof(pthread_t));
651 pthread_cond_destroy(&md->cond);
652 pthread_mutex_destroy(&md->mutex);
656 for (i = 0; i < num_threads; i++) {
657 ret = pthread_create(md->threads + i, NULL, dump_worker, md);
663 pthread_mutex_lock(&md->mutex);
665 pthread_cond_broadcast(&md->cond);
666 pthread_mutex_unlock(&md->mutex);
668 for (i--; i >= 0; i--)
669 pthread_join(md->threads[i], NULL);
671 pthread_cond_destroy(&md->cond);
672 pthread_mutex_destroy(&md->mutex);
680 static void metadump_destroy(struct metadump_struct *md)
685 pthread_mutex_lock(&md->mutex);
687 pthread_cond_broadcast(&md->cond);
688 pthread_mutex_unlock(&md->mutex);
690 for (i = 0; i < md->num_threads; i++)
691 pthread_join(md->threads[i], NULL);
693 pthread_cond_destroy(&md->cond);
694 pthread_mutex_destroy(&md->mutex);
696 while ((n = rb_first(&md->name_tree))) {
699 name = rb_entry(n, struct name, n);
700 rb_erase(n, &md->name_tree);
709 static int write_zero(FILE *out, size_t size)
711 static char zero[BLOCK_SIZE];
712 return fwrite(zero, size, 1, out);
715 static int write_buffers(struct metadump_struct *md, u64 *next)
717 struct meta_cluster_header *header = &md->cluster->header;
718 struct meta_cluster_item *item;
719 struct async_work *async;
725 if (list_empty(&md->ordered))
728 /* wait until all buffers are compressed */
729 while (md->num_items > md->num_ready) {
730 struct timespec ts = {
734 pthread_mutex_unlock(&md->mutex);
735 nanosleep(&ts, NULL);
736 pthread_mutex_lock(&md->mutex);
739 /* setup and write index block */
740 list_for_each_entry(async, &md->ordered, ordered) {
741 item = md->cluster->items + nritems;
742 item->bytenr = cpu_to_le64(async->start);
743 item->size = cpu_to_le32(async->bufsize);
746 header->nritems = cpu_to_le32(nritems);
748 ret = fwrite(md->cluster, BLOCK_SIZE, 1, md->out);
750 fprintf(stderr, "Error writing out cluster: %d\n", errno);
755 bytenr += le64_to_cpu(header->bytenr) + BLOCK_SIZE;
756 while (!list_empty(&md->ordered)) {
757 async = list_entry(md->ordered.next, struct async_work,
759 list_del_init(&async->ordered);
761 bytenr += async->bufsize;
763 ret = fwrite(async->buffer, async->bufsize, 1,
768 fprintf(stderr, "Error writing out cluster: %d\n",
776 /* zero unused space in the last block */
777 if (!err && bytenr & BLOCK_MASK) {
778 size_t size = BLOCK_SIZE - (bytenr & BLOCK_MASK);
781 ret = write_zero(md->out, size);
783 fprintf(stderr, "Error zeroing out buffer: %d\n",
793 static int read_data_extent(struct metadump_struct *md,
794 struct async_work *async)
796 struct btrfs_multi_bio *multi = NULL;
797 struct btrfs_device *device;
798 u64 bytes_left = async->size;
799 u64 logical = async->start;
808 read_len = bytes_left;
809 ret = btrfs_map_block(&md->root->fs_info->mapping_tree, READ,
810 logical, &read_len, &multi, 0, NULL);
812 fprintf(stderr, "Couldn't map data block %d\n", ret);
816 device = multi->stripes[0].dev;
818 if (device->fd == 0) {
820 "Device we need to read from is not open\n");
825 bytenr = multi->stripes[0].physical;
828 read_len = min(read_len, bytes_left);
829 done = pread64(fd, async->buffer+offset, read_len, bytenr);
830 if (done < read_len) {
832 fprintf(stderr, "Error reading extent %d\n",
835 fprintf(stderr, "Short read\n");
847 static int flush_pending(struct metadump_struct *md, int done)
849 struct async_work *async = NULL;
850 struct extent_buffer *eb;
851 u64 blocksize = md->root->nodesize;
857 if (md->pending_size) {
858 async = calloc(1, sizeof(*async));
862 async->start = md->pending_start;
863 async->size = md->pending_size;
864 async->bufsize = async->size;
865 async->buffer = malloc(async->bufsize);
866 if (!async->buffer) {
871 start = async->start;
875 ret = read_data_extent(md, async);
883 while (!md->data && size > 0) {
884 u64 this_read = min(blocksize, size);
885 eb = read_tree_block(md->root, start, this_read, 0);
890 "Error reading metadata block\n");
893 copy_buffer(md, async->buffer + offset, eb);
894 free_extent_buffer(eb);
900 md->pending_start = (u64)-1;
901 md->pending_size = 0;
906 pthread_mutex_lock(&md->mutex);
908 list_add_tail(&async->ordered, &md->ordered);
910 if (md->compress_level > 0) {
911 list_add_tail(&async->list, &md->list);
912 pthread_cond_signal(&md->cond);
917 if (md->num_items >= ITEMS_PER_CLUSTER || done) {
918 ret = write_buffers(md, &start);
920 fprintf(stderr, "Error writing buffers %d\n",
923 meta_cluster_init(md, start);
925 pthread_mutex_unlock(&md->mutex);
929 static int add_extent(u64 start, u64 size, struct metadump_struct *md,
933 if (md->data != data ||
934 md->pending_size + size > MAX_PENDING_SIZE ||
935 md->pending_start + md->pending_size != start) {
936 ret = flush_pending(md, 0);
939 md->pending_start = start;
941 readahead_tree_block(md->root, start, size, 0);
942 md->pending_size += size;
947 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
948 static int is_tree_block(struct btrfs_root *extent_root,
949 struct btrfs_path *path, u64 bytenr)
951 struct extent_buffer *leaf;
952 struct btrfs_key key;
956 leaf = path->nodes[0];
958 struct btrfs_extent_ref_v0 *ref_item;
960 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
961 ret = btrfs_next_leaf(extent_root, path);
966 leaf = path->nodes[0];
968 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
969 if (key.objectid != bytenr)
971 if (key.type != BTRFS_EXTENT_REF_V0_KEY)
973 ref_item = btrfs_item_ptr(leaf, path->slots[0],
974 struct btrfs_extent_ref_v0);
975 ref_objectid = btrfs_ref_objectid_v0(leaf, ref_item);
976 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID)
984 static int copy_tree_blocks(struct btrfs_root *root, struct extent_buffer *eb,
985 struct metadump_struct *metadump, int root_tree)
987 struct extent_buffer *tmp;
988 struct btrfs_root_item *ri;
989 struct btrfs_key key;
996 ret = add_extent(btrfs_header_bytenr(eb), root->leafsize, metadump, 0);
998 fprintf(stderr, "Error adding metadata block\n");
1002 if (btrfs_header_level(eb) == 0 && !root_tree)
1005 level = btrfs_header_level(eb);
1006 nritems = btrfs_header_nritems(eb);
1007 for (i = 0; i < nritems; i++) {
1009 btrfs_item_key_to_cpu(eb, &key, i);
1010 if (key.type != BTRFS_ROOT_ITEM_KEY)
1012 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
1013 bytenr = btrfs_disk_root_bytenr(eb, ri);
1014 tmp = read_tree_block(root, bytenr, root->leafsize, 0);
1017 "Error reading log root block\n");
1020 ret = copy_tree_blocks(root, tmp, metadump, 0);
1021 free_extent_buffer(tmp);
1025 bytenr = btrfs_node_blockptr(eb, i);
1026 tmp = read_tree_block(root, bytenr, root->leafsize, 0);
1028 fprintf(stderr, "Error reading log block\n");
1031 ret = copy_tree_blocks(root, tmp, metadump, root_tree);
1032 free_extent_buffer(tmp);
1041 static int copy_log_trees(struct btrfs_root *root,
1042 struct metadump_struct *metadump,
1043 struct btrfs_path *path)
1045 u64 blocknr = btrfs_super_log_root(root->fs_info->super_copy);
1050 if (!root->fs_info->log_root_tree ||
1051 !root->fs_info->log_root_tree->node) {
1052 fprintf(stderr, "Error copying tree log, it wasn't setup\n");
1056 return copy_tree_blocks(root, root->fs_info->log_root_tree->node,
1060 static int copy_space_cache(struct btrfs_root *root,
1061 struct metadump_struct *metadump,
1062 struct btrfs_path *path)
1064 struct extent_buffer *leaf;
1065 struct btrfs_file_extent_item *fi;
1066 struct btrfs_key key;
1067 u64 bytenr, num_bytes;
1070 root = root->fs_info->tree_root;
1073 key.type = BTRFS_EXTENT_DATA_KEY;
1076 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1078 fprintf(stderr, "Error searching for free space inode %d\n",
1084 leaf = path->nodes[0];
1085 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1086 ret = btrfs_next_leaf(root, path);
1088 fprintf(stderr, "Error going to next leaf "
1094 leaf = path->nodes[0];
1097 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1098 if (key.type != BTRFS_EXTENT_DATA_KEY) {
1103 fi = btrfs_item_ptr(leaf, path->slots[0],
1104 struct btrfs_file_extent_item);
1105 if (btrfs_file_extent_type(leaf, fi) !=
1106 BTRFS_FILE_EXTENT_REG) {
1111 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1112 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1113 ret = add_extent(bytenr, num_bytes, metadump, 1);
1115 fprintf(stderr, "Error adding space cache blocks %d\n",
1117 btrfs_release_path(root, path);
1126 static int copy_from_extent_tree(struct metadump_struct *metadump,
1127 struct btrfs_path *path)
1129 struct btrfs_root *extent_root;
1130 struct extent_buffer *leaf;
1131 struct btrfs_extent_item *ei;
1132 struct btrfs_key key;
1137 extent_root = metadump->root->fs_info->extent_root;
1138 bytenr = BTRFS_SUPER_INFO_OFFSET + 4096;
1139 key.objectid = bytenr;
1140 key.type = BTRFS_EXTENT_ITEM_KEY;
1143 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1145 fprintf(stderr, "Error searching extent root %d\n", ret);
1151 leaf = path->nodes[0];
1152 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1153 ret = btrfs_next_leaf(extent_root, path);
1155 fprintf(stderr, "Error going to next leaf %d"
1163 leaf = path->nodes[0];
1166 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1167 if (key.objectid < bytenr ||
1168 (key.type != BTRFS_EXTENT_ITEM_KEY &&
1169 key.type != BTRFS_METADATA_ITEM_KEY)) {
1174 bytenr = key.objectid;
1175 if (key.type == BTRFS_METADATA_ITEM_KEY)
1176 num_bytes = extent_root->leafsize;
1178 num_bytes = key.offset;
1180 if (btrfs_item_size_nr(leaf, path->slots[0]) > sizeof(*ei)) {
1181 ei = btrfs_item_ptr(leaf, path->slots[0],
1182 struct btrfs_extent_item);
1183 if (btrfs_extent_flags(leaf, ei) &
1184 BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1185 ret = add_extent(bytenr, num_bytes, metadump,
1188 fprintf(stderr, "Error adding block "
1194 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1195 ret = is_tree_block(extent_root, path, bytenr);
1197 fprintf(stderr, "Error checking tree block "
1203 ret = add_extent(bytenr, num_bytes, metadump,
1206 fprintf(stderr, "Error adding block "
1213 fprintf(stderr, "Either extent tree corruption or "
1214 "you haven't built with V0 support\n");
1219 bytenr += num_bytes;
1222 btrfs_release_path(extent_root, path);
1227 static int create_metadump(const char *input, FILE *out, int num_threads,
1228 int compress_level, int sanitize, int walk_trees)
1230 struct btrfs_root *root;
1231 struct btrfs_path *path = NULL;
1232 struct metadump_struct metadump;
1236 root = open_ctree(input, 0, 0);
1238 fprintf(stderr, "Open ctree failed\n");
1242 BUG_ON(root->nodesize != root->leafsize);
1244 ret = metadump_init(&metadump, root, out, num_threads,
1245 compress_level, sanitize);
1247 fprintf(stderr, "Error initing metadump %d\n", ret);
1252 ret = add_extent(BTRFS_SUPER_INFO_OFFSET, 4096, &metadump, 0);
1254 fprintf(stderr, "Error adding metadata %d\n", ret);
1259 path = btrfs_alloc_path();
1261 fprintf(stderr, "Out of memory allocing path\n");
1267 ret = copy_tree_blocks(root, root->fs_info->chunk_root->node,
1274 ret = copy_tree_blocks(root, root->fs_info->tree_root->node,
1281 ret = copy_from_extent_tree(&metadump, path);
1288 ret = copy_log_trees(root, &metadump, path);
1294 ret = copy_space_cache(root, &metadump, path);
1296 ret = flush_pending(&metadump, 1);
1300 fprintf(stderr, "Error flushing pending %d\n", ret);
1303 metadump_destroy(&metadump);
1305 btrfs_free_path(path);
1306 ret = close_ctree(root);
1307 return err ? err : ret;
1310 static void update_super_old(u8 *buffer)
1312 struct btrfs_super_block *super = (struct btrfs_super_block *)buffer;
1313 struct btrfs_chunk *chunk;
1314 struct btrfs_disk_key *key;
1315 u32 sectorsize = btrfs_super_sectorsize(super);
1316 u64 flags = btrfs_super_flags(super);
1318 flags |= BTRFS_SUPER_FLAG_METADUMP;
1319 btrfs_set_super_flags(super, flags);
1321 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1322 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1323 sizeof(struct btrfs_disk_key));
1325 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1326 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1327 btrfs_set_disk_key_offset(key, 0);
1329 btrfs_set_stack_chunk_length(chunk, (u64)-1);
1330 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1331 btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024);
1332 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1333 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1334 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1335 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1336 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1337 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1338 chunk->stripe.devid = super->dev_item.devid;
1339 btrfs_set_stack_stripe_offset(&chunk->stripe, 0);
1340 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1341 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1342 csum_block(buffer, 4096);
1345 static int update_super(u8 *buffer)
1347 struct btrfs_super_block *super = (struct btrfs_super_block *)buffer;
1348 struct btrfs_chunk *chunk;
1349 struct btrfs_disk_key *disk_key;
1350 struct btrfs_key key;
1351 u32 new_array_size = 0;
1355 u8 *ptr, *write_ptr;
1356 int old_num_stripes;
1358 write_ptr = ptr = super->sys_chunk_array;
1359 array_size = btrfs_super_sys_array_size(super);
1361 while (cur < array_size) {
1362 disk_key = (struct btrfs_disk_key *)ptr;
1363 btrfs_disk_key_to_cpu(&key, disk_key);
1365 new_array_size += sizeof(*disk_key);
1366 memmove(write_ptr, ptr, sizeof(*disk_key));
1368 write_ptr += sizeof(*disk_key);
1369 ptr += sizeof(*disk_key);
1370 cur += sizeof(*disk_key);
1371 new_cur += sizeof(*disk_key);
1373 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1374 chunk = (struct btrfs_chunk *)ptr;
1375 old_num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1376 chunk = (struct btrfs_chunk *)write_ptr;
1378 memmove(write_ptr, ptr, sizeof(*chunk));
1379 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1380 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1381 btrfs_set_stack_chunk_type(chunk,
1382 BTRFS_BLOCK_GROUP_SYSTEM);
1383 chunk->stripe.devid = super->dev_item.devid;
1384 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid,
1386 new_array_size += sizeof(*chunk);
1387 new_cur += sizeof(*chunk);
1389 fprintf(stderr, "Bogus key in the sys chunk array "
1393 write_ptr += sizeof(*chunk);
1394 ptr += btrfs_chunk_item_size(old_num_stripes);
1395 cur += btrfs_chunk_item_size(old_num_stripes);
1398 btrfs_set_super_sys_array_size(super, new_array_size);
1399 csum_block(buffer, 4096);
1404 static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size)
1406 struct extent_buffer *eb;
1408 eb = malloc(sizeof(struct extent_buffer) + size);
1411 memset(eb, 0, sizeof(struct extent_buffer) + size);
1418 static void truncate_item(struct extent_buffer *eb, int slot, u32 new_size)
1420 struct btrfs_item *item;
1428 old_size = btrfs_item_size_nr(eb, slot);
1429 if (old_size == new_size)
1432 nritems = btrfs_header_nritems(eb);
1433 data_end = btrfs_item_offset_nr(eb, nritems - 1);
1435 old_data_start = btrfs_item_offset_nr(eb, slot);
1436 size_diff = old_size - new_size;
1438 for (i = slot; i < nritems; i++) {
1440 item = btrfs_item_nr(eb, i);
1441 ioff = btrfs_item_offset(eb, item);
1442 btrfs_set_item_offset(eb, item, ioff + size_diff);
1445 memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end + size_diff,
1446 btrfs_leaf_data(eb) + data_end,
1447 old_data_start + new_size - data_end);
1448 item = btrfs_item_nr(eb, slot);
1449 btrfs_set_item_size(eb, item, new_size);
1452 static int fixup_chunk_tree_block(struct mdrestore_struct *mdres,
1453 struct async_work *async, u8 *buffer,
1456 struct extent_buffer *eb;
1457 size_t size_left = size;
1458 u64 bytenr = async->start;
1461 if (size_left % mdres->leafsize)
1464 eb = alloc_dummy_eb(bytenr, mdres->leafsize);
1470 memcpy(eb->data, buffer, mdres->leafsize);
1472 if (btrfs_header_bytenr(eb) != bytenr)
1474 if (memcmp(mdres->fsid,
1475 eb->data + offsetof(struct btrfs_header, fsid),
1479 if (btrfs_header_owner(eb) != BTRFS_CHUNK_TREE_OBJECTID)
1482 if (btrfs_header_level(eb) != 0)
1485 for (i = 0; i < btrfs_header_nritems(eb); i++) {
1486 struct btrfs_chunk chunk;
1487 struct btrfs_key key;
1490 btrfs_item_key_to_cpu(eb, &key, i);
1491 if (key.type != BTRFS_CHUNK_ITEM_KEY)
1493 truncate_item(eb, i, sizeof(chunk));
1494 read_extent_buffer(eb, &chunk,
1495 btrfs_item_ptr_offset(eb, i),
1498 /* Zero out the RAID profile */
1499 type = btrfs_stack_chunk_type(&chunk);
1500 type &= (BTRFS_BLOCK_GROUP_DATA |
1501 BTRFS_BLOCK_GROUP_SYSTEM |
1502 BTRFS_BLOCK_GROUP_METADATA);
1503 btrfs_set_stack_chunk_type(&chunk, type);
1505 btrfs_set_stack_chunk_num_stripes(&chunk, 1);
1506 btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
1507 btrfs_set_stack_stripe_devid(&chunk.stripe, mdres->devid);
1508 memcpy(chunk.stripe.dev_uuid, mdres->uuid,
1510 write_extent_buffer(eb, &chunk,
1511 btrfs_item_ptr_offset(eb, i),
1514 memcpy(buffer, eb->data, eb->len);
1515 csum_block(buffer, eb->len);
1517 size_left -= mdres->leafsize;
1518 buffer += mdres->leafsize;
1519 bytenr += mdres->leafsize;
1525 static void write_backup_supers(int fd, u8 *buf)
1527 struct btrfs_super_block *super = (struct btrfs_super_block *)buf;
1534 if (fstat(fd, &st)) {
1535 fprintf(stderr, "Couldn't stat restore point, won't be able "
1536 "to write backup supers: %d\n", errno);
1540 size = btrfs_device_size(fd, &st);
1542 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1543 bytenr = btrfs_sb_offset(i);
1544 if (bytenr + 4096 > size)
1546 btrfs_set_super_bytenr(super, bytenr);
1547 csum_block(buf, 4096);
1548 ret = pwrite64(fd, buf, 4096, bytenr);
1551 fprintf(stderr, "Problem writing out backup "
1552 "super block %d, err %d\n", i, errno);
1554 fprintf(stderr, "Short write writing out "
1555 "backup super block\n");
1561 static u64 logical_to_physical(struct mdrestore_struct *mdres, u64 logical, u64 *size)
1563 struct fs_chunk *fs_chunk;
1564 struct rb_node *entry;
1565 struct fs_chunk search;
1568 if (logical == BTRFS_SUPER_INFO_OFFSET)
1571 search.logical = logical;
1572 entry = tree_search(&mdres->chunk_tree, &search.n, chunk_cmp, 1);
1574 if (mdres->in != stdin)
1575 printf("Couldn't find a chunk, using logical\n");
1578 fs_chunk = rb_entry(entry, struct fs_chunk, n);
1579 if (fs_chunk->logical > logical || fs_chunk->logical + fs_chunk->bytes < logical)
1581 offset = search.logical - fs_chunk->logical;
1583 *size = min(*size, fs_chunk->bytes + fs_chunk->logical - logical);
1584 return fs_chunk->physical + offset;
1587 static void *restore_worker(void *data)
1589 struct mdrestore_struct *mdres = (struct mdrestore_struct *)data;
1590 struct async_work *async;
1596 int compress_size = MAX_PENDING_SIZE * 4;
1598 outfd = fileno(mdres->out);
1599 buffer = malloc(compress_size);
1601 fprintf(stderr, "Error allocing buffer\n");
1602 pthread_mutex_lock(&mdres->mutex);
1604 mdres->error = -ENOMEM;
1605 pthread_mutex_unlock(&mdres->mutex);
1614 pthread_mutex_lock(&mdres->mutex);
1615 while (!mdres->leafsize || list_empty(&mdres->list)) {
1617 pthread_mutex_unlock(&mdres->mutex);
1620 pthread_cond_wait(&mdres->cond, &mdres->mutex);
1622 async = list_entry(mdres->list.next, struct async_work, list);
1623 list_del_init(&async->list);
1624 pthread_mutex_unlock(&mdres->mutex);
1626 if (mdres->compress_method == COMPRESS_ZLIB) {
1627 size = compress_size;
1628 ret = uncompress(buffer, (unsigned long *)&size,
1629 async->buffer, async->bufsize);
1631 fprintf(stderr, "Error decompressing %d\n",
1637 outbuf = async->buffer;
1638 size = async->bufsize;
1641 if (!mdres->multi_devices) {
1642 if (async->start == BTRFS_SUPER_INFO_OFFSET) {
1643 if (mdres->old_restore) {
1644 update_super_old(outbuf);
1646 ret = update_super(outbuf);
1650 } else if (!mdres->old_restore) {
1651 ret = fixup_chunk_tree_block(mdres, async, outbuf, size);
1657 if (!mdres->fixup_offset) {
1659 u64 chunk_size = size;
1660 if (!mdres->multi_devices)
1661 bytenr = logical_to_physical(mdres,
1662 async->start + offset,
1665 bytenr = async->start + offset;
1667 ret = pwrite64(outfd, outbuf+offset, chunk_size,
1669 if (ret != chunk_size) {
1671 fprintf(stderr, "Error writing to "
1672 "device %d\n", errno);
1676 fprintf(stderr, "Short write\n");
1682 offset += chunk_size;
1684 } else if (async->start != BTRFS_SUPER_INFO_OFFSET) {
1685 ret = write_data_to_disk(mdres->info, outbuf, async->start, size, 0);
1687 printk("Error write data\n");
1693 /* backup super blocks are already there at fixup_offset stage */
1694 if (!mdres->multi_devices && async->start == BTRFS_SUPER_INFO_OFFSET)
1695 write_backup_supers(outfd, outbuf);
1697 pthread_mutex_lock(&mdres->mutex);
1698 if (err && !mdres->error)
1701 pthread_mutex_unlock(&mdres->mutex);
1703 free(async->buffer);
1711 static void mdrestore_destroy(struct mdrestore_struct *mdres)
1716 while ((n = rb_first(&mdres->chunk_tree))) {
1717 struct fs_chunk *entry;
1719 entry = rb_entry(n, struct fs_chunk, n);
1720 rb_erase(n, &mdres->chunk_tree);
1723 pthread_mutex_lock(&mdres->mutex);
1725 pthread_cond_broadcast(&mdres->cond);
1726 pthread_mutex_unlock(&mdres->mutex);
1728 for (i = 0; i < mdres->num_threads; i++)
1729 pthread_join(mdres->threads[i], NULL);
1731 pthread_cond_destroy(&mdres->cond);
1732 pthread_mutex_destroy(&mdres->mutex);
1733 free(mdres->threads);
1736 static int mdrestore_init(struct mdrestore_struct *mdres,
1737 FILE *in, FILE *out, int old_restore,
1738 int num_threads, int fixup_offset,
1739 struct btrfs_fs_info *info, int multi_devices)
1743 memset(mdres, 0, sizeof(*mdres));
1744 pthread_cond_init(&mdres->cond, NULL);
1745 pthread_mutex_init(&mdres->mutex, NULL);
1746 INIT_LIST_HEAD(&mdres->list);
1749 mdres->old_restore = old_restore;
1750 mdres->chunk_tree.rb_node = NULL;
1751 mdres->fixup_offset = fixup_offset;
1753 mdres->multi_devices = multi_devices;
1758 mdres->num_threads = num_threads;
1759 mdres->threads = calloc(num_threads, sizeof(pthread_t));
1760 if (!mdres->threads)
1762 for (i = 0; i < num_threads; i++) {
1763 ret = pthread_create(mdres->threads + i, NULL, restore_worker,
1769 mdrestore_destroy(mdres);
1773 static int fill_mdres_info(struct mdrestore_struct *mdres,
1774 struct async_work *async)
1776 struct btrfs_super_block *super;
1781 /* We've already been initialized */
1782 if (mdres->leafsize)
1785 if (mdres->compress_method == COMPRESS_ZLIB) {
1786 size_t size = MAX_PENDING_SIZE * 2;
1788 buffer = malloc(MAX_PENDING_SIZE * 2);
1791 ret = uncompress(buffer, (unsigned long *)&size,
1792 async->buffer, async->bufsize);
1794 fprintf(stderr, "Error decompressing %d\n", ret);
1800 outbuf = async->buffer;
1803 super = (struct btrfs_super_block *)outbuf;
1804 mdres->leafsize = btrfs_super_leafsize(super);
1805 memcpy(mdres->fsid, super->fsid, BTRFS_FSID_SIZE);
1806 memcpy(mdres->uuid, super->dev_item.uuid,
1808 mdres->devid = le64_to_cpu(super->dev_item.devid);
1813 static int add_cluster(struct meta_cluster *cluster,
1814 struct mdrestore_struct *mdres, u64 *next)
1816 struct meta_cluster_item *item;
1817 struct meta_cluster_header *header = &cluster->header;
1818 struct async_work *async;
1823 BUG_ON(mdres->num_items);
1824 mdres->compress_method = header->compress;
1826 bytenr = le64_to_cpu(header->bytenr) + BLOCK_SIZE;
1827 nritems = le32_to_cpu(header->nritems);
1828 for (i = 0; i < nritems; i++) {
1829 item = &cluster->items[i];
1830 async = calloc(1, sizeof(*async));
1832 fprintf(stderr, "Error allocating async\n");
1835 async->start = le64_to_cpu(item->bytenr);
1836 async->bufsize = le32_to_cpu(item->size);
1837 async->buffer = malloc(async->bufsize);
1838 if (!async->buffer) {
1839 fprintf(stderr, "Error allocing async buffer\n");
1843 ret = fread(async->buffer, async->bufsize, 1, mdres->in);
1845 fprintf(stderr, "Error reading buffer %d\n", errno);
1846 free(async->buffer);
1850 bytenr += async->bufsize;
1852 pthread_mutex_lock(&mdres->mutex);
1853 if (async->start == BTRFS_SUPER_INFO_OFFSET) {
1854 ret = fill_mdres_info(mdres, async);
1856 fprintf(stderr, "Error setting up restore\n");
1857 pthread_mutex_unlock(&mdres->mutex);
1858 free(async->buffer);
1863 list_add_tail(&async->list, &mdres->list);
1865 pthread_cond_signal(&mdres->cond);
1866 pthread_mutex_unlock(&mdres->mutex);
1868 if (bytenr & BLOCK_MASK) {
1869 char buffer[BLOCK_MASK];
1870 size_t size = BLOCK_SIZE - (bytenr & BLOCK_MASK);
1873 ret = fread(buffer, size, 1, mdres->in);
1875 fprintf(stderr, "Error reading in buffer %d\n", errno);
1883 static int wait_for_worker(struct mdrestore_struct *mdres)
1887 pthread_mutex_lock(&mdres->mutex);
1889 while (!ret && mdres->num_items > 0) {
1890 struct timespec ts = {
1892 .tv_nsec = 10000000,
1894 pthread_mutex_unlock(&mdres->mutex);
1895 nanosleep(&ts, NULL);
1896 pthread_mutex_lock(&mdres->mutex);
1899 pthread_mutex_unlock(&mdres->mutex);
1903 static int read_chunk_block(struct mdrestore_struct *mdres, u8 *buffer,
1904 u64 bytenr, u64 item_bytenr, u32 bufsize,
1907 struct extent_buffer *eb;
1911 eb = alloc_dummy_eb(bytenr, mdres->leafsize);
1917 while (item_bytenr != bytenr) {
1918 buffer += mdres->leafsize;
1919 item_bytenr += mdres->leafsize;
1922 memcpy(eb->data, buffer, mdres->leafsize);
1923 if (btrfs_header_bytenr(eb) != bytenr) {
1924 fprintf(stderr, "Eb bytenr doesn't match found bytenr\n");
1929 if (memcmp(mdres->fsid, eb->data + offsetof(struct btrfs_header, fsid),
1931 fprintf(stderr, "Fsid doesn't match\n");
1936 if (btrfs_header_owner(eb) != BTRFS_CHUNK_TREE_OBJECTID) {
1937 fprintf(stderr, "Does not belong to the chunk tree\n");
1942 for (i = 0; i < btrfs_header_nritems(eb); i++) {
1943 struct btrfs_chunk chunk;
1944 struct fs_chunk *fs_chunk;
1945 struct btrfs_key key;
1947 if (btrfs_header_level(eb)) {
1948 u64 blockptr = btrfs_node_blockptr(eb, i);
1950 ret = search_for_chunk_blocks(mdres, blockptr,
1957 /* Yay a leaf! We loves leafs! */
1958 btrfs_item_key_to_cpu(eb, &key, i);
1959 if (key.type != BTRFS_CHUNK_ITEM_KEY)
1962 fs_chunk = malloc(sizeof(struct fs_chunk));
1964 fprintf(stderr, "Erorr allocating chunk\n");
1968 memset(fs_chunk, 0, sizeof(*fs_chunk));
1969 read_extent_buffer(eb, &chunk, btrfs_item_ptr_offset(eb, i),
1972 fs_chunk->logical = key.offset;
1973 fs_chunk->physical = btrfs_stack_stripe_offset(&chunk.stripe);
1974 fs_chunk->bytes = btrfs_stack_chunk_length(&chunk);
1975 tree_insert(&mdres->chunk_tree, &fs_chunk->n, chunk_cmp);
1982 /* If you have to ask you aren't worthy */
1983 static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
1984 u64 search, u64 cluster_bytenr)
1986 struct meta_cluster *cluster;
1987 struct meta_cluster_header *header;
1988 struct meta_cluster_item *item;
1989 u64 current_cluster = cluster_bytenr, bytenr;
1991 u32 bufsize, nritems, i;
1992 u8 *buffer, *tmp = NULL;
1995 cluster = malloc(BLOCK_SIZE);
1997 fprintf(stderr, "Error allocating cluster\n");
2001 buffer = malloc(MAX_PENDING_SIZE * 2);
2003 fprintf(stderr, "Error allocing buffer\n");
2008 if (mdres->compress_method == COMPRESS_ZLIB) {
2009 tmp = malloc(MAX_PENDING_SIZE * 2);
2011 fprintf(stderr, "Error allocing tmp buffer\n");
2018 bytenr = current_cluster;
2020 if (fseek(mdres->in, current_cluster, SEEK_SET)) {
2021 fprintf(stderr, "Error seeking: %d\n", errno);
2026 ret = fread(cluster, BLOCK_SIZE, 1, mdres->in);
2028 if (cluster_bytenr != 0) {
2030 current_cluster = 0;
2034 printf("ok this is where we screwed up?\n");
2037 } else if (ret < 0) {
2038 fprintf(stderr, "Error reading image\n");
2043 header = &cluster->header;
2044 if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
2045 le64_to_cpu(header->bytenr) != current_cluster) {
2046 fprintf(stderr, "bad header in metadump image\n");
2051 bytenr += BLOCK_SIZE;
2052 nritems = le32_to_cpu(header->nritems);
2053 for (i = 0; i < nritems; i++) {
2056 item = &cluster->items[i];
2057 bufsize = le32_to_cpu(item->size);
2058 item_bytenr = le64_to_cpu(item->bytenr);
2060 if (mdres->compress_method == COMPRESS_ZLIB) {
2061 ret = fread(tmp, bufsize, 1, mdres->in);
2063 fprintf(stderr, "Error reading: %d\n",
2069 size = MAX_PENDING_SIZE * 2;
2070 ret = uncompress(buffer,
2071 (unsigned long *)&size, tmp,
2074 fprintf(stderr, "Error decompressing "
2080 ret = fread(buffer, bufsize, 1, mdres->in);
2082 fprintf(stderr, "Error reading: %d\n",
2091 if (item_bytenr <= search &&
2092 item_bytenr + size > search) {
2093 ret = read_chunk_block(mdres, buffer, search,
2107 if (bytenr & BLOCK_MASK)
2108 bytenr += BLOCK_SIZE - (bytenr & BLOCK_MASK);
2109 current_cluster = bytenr;
2118 static int build_chunk_tree(struct mdrestore_struct *mdres,
2119 struct meta_cluster *cluster)
2121 struct btrfs_super_block *super;
2122 struct meta_cluster_header *header;
2123 struct meta_cluster_item *item = NULL;
2124 u64 chunk_root_bytenr = 0;
2130 /* We can't seek with stdin so don't bother doing this */
2131 if (mdres->in == stdin)
2134 ret = fread(cluster, BLOCK_SIZE, 1, mdres->in);
2136 fprintf(stderr, "Error reading in cluster: %d\n", errno);
2141 header = &cluster->header;
2142 if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
2143 le64_to_cpu(header->bytenr) != 0) {
2144 fprintf(stderr, "bad header in metadump image\n");
2148 bytenr += BLOCK_SIZE;
2149 mdres->compress_method = header->compress;
2150 nritems = le32_to_cpu(header->nritems);
2151 for (i = 0; i < nritems; i++) {
2152 item = &cluster->items[i];
2154 if (le64_to_cpu(item->bytenr) == BTRFS_SUPER_INFO_OFFSET)
2156 bytenr += le32_to_cpu(item->size);
2157 if (fseek(mdres->in, le32_to_cpu(item->size), SEEK_CUR)) {
2158 fprintf(stderr, "Error seeking: %d\n", errno);
2163 if (!item || le64_to_cpu(item->bytenr) != BTRFS_SUPER_INFO_OFFSET) {
2164 fprintf(stderr, "Huh, didn't find the super?\n");
2168 buffer = malloc(le32_to_cpu(item->size));
2170 fprintf(stderr, "Error allocing buffer\n");
2174 ret = fread(buffer, le32_to_cpu(item->size), 1, mdres->in);
2176 fprintf(stderr, "Error reading buffer: %d\n", errno);
2181 if (mdres->compress_method == COMPRESS_ZLIB) {
2182 size_t size = MAX_PENDING_SIZE * 2;
2185 tmp = malloc(MAX_PENDING_SIZE * 2);
2190 ret = uncompress(tmp, (unsigned long *)&size,
2191 buffer, le32_to_cpu(item->size));
2193 fprintf(stderr, "Error decompressing %d\n", ret);
2202 super = (struct btrfs_super_block *)buffer;
2203 chunk_root_bytenr = btrfs_super_chunk_root(super);
2204 mdres->leafsize = btrfs_super_leafsize(super);
2205 memcpy(mdres->fsid, super->fsid, BTRFS_FSID_SIZE);
2206 memcpy(mdres->uuid, super->dev_item.uuid,
2208 mdres->devid = le64_to_cpu(super->dev_item.devid);
2211 return search_for_chunk_blocks(mdres, chunk_root_bytenr, 0);
2214 static int __restore_metadump(const char *input, FILE *out, int old_restore,
2215 int num_threads, int fixup_offset,
2216 const char *target, int multi_devices)
2218 struct meta_cluster *cluster = NULL;
2219 struct meta_cluster_header *header;
2220 struct mdrestore_struct mdrestore;
2221 struct btrfs_fs_info *info = NULL;
2226 if (!strcmp(input, "-")) {
2229 in = fopen(input, "r");
2231 perror("unable to open metadump image");
2236 /* NOTE: open with write mode */
2239 info = open_ctree_fs_info_restore(target, 0, 0, 1, 1);
2241 fprintf(stderr, "%s: open ctree failed\n", __func__);
2247 cluster = malloc(BLOCK_SIZE);
2249 fprintf(stderr, "Error allocating cluster\n");
2254 ret = mdrestore_init(&mdrestore, in, out, old_restore, num_threads,
2255 fixup_offset, info, multi_devices);
2257 fprintf(stderr, "Error initing mdrestore %d\n", ret);
2258 goto failed_cluster;
2261 if (!multi_devices) {
2262 ret = build_chunk_tree(&mdrestore, cluster);
2267 if (in != stdin && fseek(in, 0, SEEK_SET)) {
2268 fprintf(stderr, "Error seeking %d\n", errno);
2273 ret = fread(cluster, BLOCK_SIZE, 1, in);
2277 header = &cluster->header;
2278 if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
2279 le64_to_cpu(header->bytenr) != bytenr) {
2280 fprintf(stderr, "bad header in metadump image\n");
2284 ret = add_cluster(cluster, &mdrestore, &bytenr);
2286 fprintf(stderr, "Error adding cluster\n");
2290 ret = wait_for_worker(&mdrestore);
2292 fprintf(stderr, "One of the threads errored out %d\n",
2298 mdrestore_destroy(&mdrestore);
2302 if (fixup_offset && info)
2303 close_ctree(info->chunk_root);
2310 static int restore_metadump(const char *input, FILE *out, int old_restore,
2311 int num_threads, int multi_devices)
2313 return __restore_metadump(input, out, old_restore, num_threads, 0, NULL,
2317 static int fixup_metadump(const char *input, FILE *out, int num_threads,
2320 return __restore_metadump(input, out, 0, num_threads, 1, target, 1);
2323 static int update_disk_super_on_device(struct btrfs_fs_info *info,
2324 const char *other_dev, u64 cur_devid)
2326 struct btrfs_key key;
2327 struct extent_buffer *leaf;
2328 struct btrfs_path path;
2329 struct btrfs_dev_item *dev_item;
2330 struct btrfs_super_block *disk_super;
2331 char dev_uuid[BTRFS_UUID_SIZE];
2332 char fs_uuid[BTRFS_UUID_SIZE];
2333 u64 devid, type, io_align, io_width;
2334 u64 sector_size, total_bytes, bytes_used;
2339 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2340 key.type = BTRFS_DEV_ITEM_KEY;
2341 key.offset = cur_devid;
2343 btrfs_init_path(&path);
2344 ret = btrfs_search_slot(NULL, info->chunk_root, &key, &path, 0, 0);
2346 fprintf(stderr, "search key fails\n");
2350 leaf = path.nodes[0];
2351 dev_item = btrfs_item_ptr(leaf, path.slots[0],
2352 struct btrfs_dev_item);
2354 devid = btrfs_device_id(leaf, dev_item);
2355 if (devid != cur_devid) {
2356 printk("devid %llu mismatch with %llu\n", devid, cur_devid);
2360 type = btrfs_device_type(leaf, dev_item);
2361 io_align = btrfs_device_io_align(leaf, dev_item);
2362 io_width = btrfs_device_io_width(leaf, dev_item);
2363 sector_size = btrfs_device_sector_size(leaf, dev_item);
2364 total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2365 bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2366 read_extent_buffer(leaf, dev_uuid, (unsigned long)btrfs_device_uuid(dev_item), BTRFS_UUID_SIZE);
2367 read_extent_buffer(leaf, fs_uuid, (unsigned long)btrfs_device_fsid(dev_item), BTRFS_UUID_SIZE);
2369 btrfs_release_path(info->chunk_root, &path);
2371 printk("update disk super on %s devid=%llu\n", other_dev, devid);
2373 /* update other devices' super block */
2374 fp = open(other_dev, O_CREAT | O_RDWR, 0600);
2376 fprintf(stderr, "could not open %s\n", other_dev);
2380 buf = malloc(BTRFS_SUPER_INFO_SIZE);
2386 memcpy(buf, info->super_copy, BTRFS_SUPER_INFO_SIZE);
2388 disk_super = (struct btrfs_super_block *)buf;
2389 dev_item = &disk_super->dev_item;
2391 btrfs_set_stack_device_type(dev_item, type);
2392 btrfs_set_stack_device_id(dev_item, devid);
2393 btrfs_set_stack_device_total_bytes(dev_item, total_bytes);
2394 btrfs_set_stack_device_bytes_used(dev_item, bytes_used);
2395 btrfs_set_stack_device_io_align(dev_item, io_align);
2396 btrfs_set_stack_device_io_width(dev_item, io_width);
2397 btrfs_set_stack_device_sector_size(dev_item, sector_size);
2398 memcpy(dev_item->uuid, dev_uuid, BTRFS_UUID_SIZE);
2399 memcpy(dev_item->fsid, fs_uuid, BTRFS_UUID_SIZE);
2400 csum_block((u8 *)buf, BTRFS_SUPER_INFO_SIZE);
2402 ret = pwrite64(fp, buf, BTRFS_SUPER_INFO_SIZE, BTRFS_SUPER_INFO_OFFSET);
2403 if (ret != BTRFS_SUPER_INFO_SIZE) {
2408 write_backup_supers(fp, (u8 *)buf);
2416 static void print_usage(void)
2418 fprintf(stderr, "usage: btrfs-image [options] source target\n");
2419 fprintf(stderr, "\t-r \trestore metadump image\n");
2420 fprintf(stderr, "\t-c value\tcompression level (0 ~ 9)\n");
2421 fprintf(stderr, "\t-t value\tnumber of threads (1 ~ 32)\n");
2422 fprintf(stderr, "\t-o \tdon't mess with the chunk tree when restoring\n");
2423 fprintf(stderr, "\t-s \tsanitize file names, use once to just use garbage, use twice if you want crc collisions\n");
2424 fprintf(stderr, "\t-w \twalk all trees instead of using extent tree, do this if your extent tree is broken\n");
2428 int main(int argc, char *argv[])
2432 int num_threads = 0;
2433 int compress_level = 0;
2435 int old_restore = 0;
2437 int multi_devices = 0;
2444 int c = getopt(argc, argv, "rc:t:oswm");
2452 num_threads = atoi(optarg);
2453 if (num_threads <= 0 || num_threads > 32)
2457 compress_level = atoi(optarg);
2458 if (compress_level < 0 || compress_level > 9)
2479 if ((old_restore) && create)
2482 argc = argc - optind;
2485 if (multi_devices && dev_cnt < 2)
2487 if (!multi_devices && dev_cnt != 1)
2490 source = argv[optind];
2491 target = argv[optind + 1];
2493 if (create && !strcmp(target, "-")) {
2496 out = fopen(target, "w+");
2498 perror("unable to create target file");
2503 if (num_threads == 0 && compress_level > 0) {
2504 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
2505 if (num_threads <= 0)
2510 ret = create_metadump(source, out, num_threads,
2511 compress_level, sanitize, walk_trees);
2513 ret = restore_metadump(source, out, old_restore, 1,
2516 printk("%s failed (%s)\n", (create) ? "create" : "restore",
2521 /* extended support for multiple devices */
2522 if (!create && multi_devices) {
2523 struct btrfs_fs_info *info;
2527 info = open_ctree_fs_info_restore(target, 0, 0, 0, 1);
2530 fprintf(stderr, "unable to open %s error = %s\n",
2531 target, strerror(e));
2535 total_devs = btrfs_super_num_devices(info->super_copy);
2536 if (total_devs != dev_cnt) {
2537 printk("it needs %llu devices but has only %d\n",
2538 total_devs, dev_cnt);
2539 close_ctree(info->chunk_root);
2543 /* update super block on other disks */
2544 for (i = 2; i <= dev_cnt; i++) {
2545 ret = update_disk_super_on_device(info,
2546 argv[optind + i], (u64)i);
2548 printk("update disk super failed devid=%d (error=%d)\n",
2550 close_ctree(info->chunk_root);
2555 close_ctree(info->chunk_root);
2557 /* fix metadata block to map correct chunk */
2558 ret = fixup_metadump(source, out, 1, target);
2560 fprintf(stderr, "fix metadump failed (error=%d)\n",