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.
22 #include <sys/types.h>
30 #include "kerncompat.h"
34 #include "transaction.h"
37 #include "extent_io.h"
39 #include "image/metadump.h"
40 #include "image/sanitize.h"
42 #define MAX_WORKER_THREADS (32)
45 struct list_head list;
46 struct list_head ordered;
54 struct metadump_struct {
55 struct btrfs_root *root;
59 struct meta_cluster cluster;
60 char meta_cluster_bytes[BLOCK_SIZE];
63 pthread_t threads[MAX_WORKER_THREADS];
65 pthread_mutex_t mutex;
67 struct rb_root name_tree;
69 struct list_head list;
70 struct list_head ordered;
80 enum sanitize_mode sanitize_names;
85 struct mdrestore_struct {
89 pthread_t threads[MAX_WORKER_THREADS];
91 pthread_mutex_t mutex;
94 struct rb_root chunk_tree;
95 struct rb_root physical_tree;
96 struct list_head list;
97 struct list_head overlapping_chunks;
102 u64 last_physical_offset;
103 u8 uuid[BTRFS_UUID_SIZE];
104 u8 fsid[BTRFS_FSID_SIZE];
112 int clear_space_cache;
113 struct btrfs_fs_info *info;
116 static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
117 u64 search, u64 cluster_bytenr);
118 static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size);
120 static void csum_block(u8 *buf, size_t len)
122 u8 result[BTRFS_CRC32_SIZE];
124 crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len - BTRFS_CSUM_SIZE);
125 btrfs_csum_final(crc, result);
126 memcpy(buf, result, BTRFS_CRC32_SIZE);
129 static int has_name(struct btrfs_key *key)
132 case BTRFS_DIR_ITEM_KEY:
133 case BTRFS_DIR_INDEX_KEY:
134 case BTRFS_INODE_REF_KEY:
135 case BTRFS_INODE_EXTREF_KEY:
136 case BTRFS_XATTR_ITEM_KEY:
145 static int chunk_cmp(struct rb_node *a, struct rb_node *b, int fuzz)
147 struct fs_chunk *entry = rb_entry(a, struct fs_chunk, l);
148 struct fs_chunk *ins = rb_entry(b, struct fs_chunk, l);
150 if (fuzz && ins->logical >= entry->logical &&
151 ins->logical < entry->logical + entry->bytes)
154 if (ins->logical < entry->logical)
156 else if (ins->logical > entry->logical)
161 static int physical_cmp(struct rb_node *a, struct rb_node *b, int fuzz)
163 struct fs_chunk *entry = rb_entry(a, struct fs_chunk, p);
164 struct fs_chunk *ins = rb_entry(b, struct fs_chunk, p);
166 if (fuzz && ins->physical >= entry->physical &&
167 ins->physical < entry->physical + entry->bytes)
170 if (fuzz && entry->physical >= ins->physical &&
171 entry->physical < ins->physical + ins->bytes)
174 if (ins->physical < entry->physical)
176 else if (ins->physical > entry->physical)
181 static void tree_insert(struct rb_root *root, struct rb_node *ins,
182 int (*cmp)(struct rb_node *a, struct rb_node *b,
185 struct rb_node ** p = &root->rb_node;
186 struct rb_node * parent = NULL;
192 dir = cmp(*p, ins, 1);
201 rb_link_node(ins, parent, p);
202 rb_insert_color(ins, root);
205 static struct rb_node *tree_search(struct rb_root *root,
206 struct rb_node *search,
207 int (*cmp)(struct rb_node *a,
208 struct rb_node *b, int fuzz),
211 struct rb_node *n = root->rb_node;
215 dir = cmp(n, search, fuzz);
227 static u64 logical_to_physical(struct mdrestore_struct *mdres, u64 logical,
228 u64 *size, u64 *physical_dup)
230 struct fs_chunk *fs_chunk;
231 struct rb_node *entry;
232 struct fs_chunk search;
235 if (logical == BTRFS_SUPER_INFO_OFFSET)
238 search.logical = logical;
239 entry = tree_search(&mdres->chunk_tree, &search.l, chunk_cmp, 1);
241 if (mdres->in != stdin)
242 warning("cannot find a chunk, using logical");
245 fs_chunk = rb_entry(entry, struct fs_chunk, l);
246 if (fs_chunk->logical > logical || fs_chunk->logical + fs_chunk->bytes < logical)
248 offset = search.logical - fs_chunk->logical;
251 /* Only in dup case, physical_dup is not equal to 0 */
252 if (fs_chunk->physical_dup)
253 *physical_dup = fs_chunk->physical_dup + offset;
258 *size = min(*size, fs_chunk->bytes + fs_chunk->logical - logical);
259 return fs_chunk->physical + offset;
263 * zero inline extents and csum items
265 static void zero_items(struct metadump_struct *md, u8 *dst,
266 struct extent_buffer *src)
268 struct btrfs_file_extent_item *fi;
269 struct btrfs_item *item;
270 struct btrfs_key key;
271 u32 nritems = btrfs_header_nritems(src);
276 for (i = 0; i < nritems; i++) {
277 item = btrfs_item_nr(i);
278 btrfs_item_key_to_cpu(src, &key, i);
279 if (key.type == BTRFS_CSUM_ITEM_KEY) {
280 size = btrfs_item_size_nr(src, i);
281 memset(dst + btrfs_leaf_data(src) +
282 btrfs_item_offset_nr(src, i), 0, size);
286 if (md->sanitize_names && has_name(&key)) {
287 sanitize_name(md->sanitize_names, &md->name_tree, dst,
292 if (key.type != BTRFS_EXTENT_DATA_KEY)
295 fi = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
296 extent_type = btrfs_file_extent_type(src, fi);
297 if (extent_type != BTRFS_FILE_EXTENT_INLINE)
300 ptr = btrfs_file_extent_inline_start(fi);
301 size = btrfs_file_extent_inline_item_len(src, item);
302 memset(dst + ptr, 0, size);
307 * copy buffer and zero useless data in the buffer
309 static void copy_buffer(struct metadump_struct *md, u8 *dst,
310 struct extent_buffer *src)
316 memcpy(dst, src->data, src->len);
317 if (src->start == BTRFS_SUPER_INFO_OFFSET)
320 level = btrfs_header_level(src);
321 nritems = btrfs_header_nritems(src);
324 size = sizeof(struct btrfs_header);
325 memset(dst + size, 0, src->len - size);
326 } else if (level == 0) {
327 size = btrfs_leaf_data(src) +
328 btrfs_item_offset_nr(src, nritems - 1) -
329 btrfs_item_nr_offset(nritems);
330 memset(dst + btrfs_item_nr_offset(nritems), 0, size);
331 zero_items(md, dst, src);
333 size = offsetof(struct btrfs_node, ptrs) +
334 sizeof(struct btrfs_key_ptr) * nritems;
335 memset(dst + size, 0, src->len - size);
337 csum_block(dst, src->len);
340 static void *dump_worker(void *data)
342 struct metadump_struct *md = (struct metadump_struct *)data;
343 struct async_work *async;
347 pthread_mutex_lock(&md->mutex);
348 while (list_empty(&md->list)) {
350 pthread_mutex_unlock(&md->mutex);
353 pthread_cond_wait(&md->cond, &md->mutex);
355 async = list_entry(md->list.next, struct async_work, list);
356 list_del_init(&async->list);
357 pthread_mutex_unlock(&md->mutex);
359 if (md->compress_level > 0) {
360 u8 *orig = async->buffer;
362 async->bufsize = compressBound(async->size);
363 async->buffer = malloc(async->bufsize);
364 if (!async->buffer) {
365 error("not enough memory for async buffer");
366 pthread_mutex_lock(&md->mutex);
369 pthread_mutex_unlock(&md->mutex);
373 ret = compress2(async->buffer,
374 (unsigned long *)&async->bufsize,
375 orig, async->size, md->compress_level);
383 pthread_mutex_lock(&md->mutex);
385 pthread_mutex_unlock(&md->mutex);
391 static void meta_cluster_init(struct metadump_struct *md, u64 start)
393 struct meta_cluster_header *header;
397 header = &md->cluster.header;
398 header->magic = cpu_to_le64(HEADER_MAGIC);
399 header->bytenr = cpu_to_le64(start);
400 header->nritems = cpu_to_le32(0);
401 header->compress = md->compress_level > 0 ?
402 COMPRESS_ZLIB : COMPRESS_NONE;
405 static void metadump_destroy(struct metadump_struct *md, int num_threads)
410 pthread_mutex_lock(&md->mutex);
412 pthread_cond_broadcast(&md->cond);
413 pthread_mutex_unlock(&md->mutex);
415 for (i = 0; i < num_threads; i++)
416 pthread_join(md->threads[i], NULL);
418 pthread_cond_destroy(&md->cond);
419 pthread_mutex_destroy(&md->mutex);
421 while ((n = rb_first(&md->name_tree))) {
424 name = rb_entry(n, struct name, n);
425 rb_erase(n, &md->name_tree);
432 static int metadump_init(struct metadump_struct *md, struct btrfs_root *root,
433 FILE *out, int num_threads, int compress_level,
434 enum sanitize_mode sanitize_names)
438 memset(md, 0, sizeof(*md));
439 INIT_LIST_HEAD(&md->list);
440 INIT_LIST_HEAD(&md->ordered);
443 md->pending_start = (u64)-1;
444 md->compress_level = compress_level;
445 md->sanitize_names = sanitize_names;
446 if (sanitize_names == SANITIZE_COLLISIONS)
447 crc32c_optimization_init();
449 md->name_tree.rb_node = NULL;
450 md->num_threads = num_threads;
451 pthread_cond_init(&md->cond, NULL);
452 pthread_mutex_init(&md->mutex, NULL);
453 meta_cluster_init(md, 0);
458 for (i = 0; i < num_threads; i++) {
459 ret = pthread_create(md->threads + i, NULL, dump_worker, md);
465 metadump_destroy(md, i + 1);
470 static int write_zero(FILE *out, size_t size)
472 static char zero[BLOCK_SIZE];
473 return fwrite(zero, size, 1, out);
476 static int write_buffers(struct metadump_struct *md, u64 *next)
478 struct meta_cluster_header *header = &md->cluster.header;
479 struct meta_cluster_item *item;
480 struct async_work *async;
486 if (list_empty(&md->ordered))
489 /* wait until all buffers are compressed */
490 while (!err && md->num_items > md->num_ready) {
491 struct timespec ts = {
495 pthread_mutex_unlock(&md->mutex);
496 nanosleep(&ts, NULL);
497 pthread_mutex_lock(&md->mutex);
502 error("one of the threads failed: %s", strerror(-err));
506 /* setup and write index block */
507 list_for_each_entry(async, &md->ordered, ordered) {
508 item = &md->cluster.items[nritems];
509 item->bytenr = cpu_to_le64(async->start);
510 item->size = cpu_to_le32(async->bufsize);
513 header->nritems = cpu_to_le32(nritems);
515 ret = fwrite(&md->cluster, BLOCK_SIZE, 1, md->out);
517 error("unable to write out cluster: %m");
522 bytenr += le64_to_cpu(header->bytenr) + BLOCK_SIZE;
523 while (!list_empty(&md->ordered)) {
524 async = list_entry(md->ordered.next, struct async_work,
526 list_del_init(&async->ordered);
528 bytenr += async->bufsize;
530 ret = fwrite(async->buffer, async->bufsize, 1,
533 error("unable to write out cluster: %m");
542 /* zero unused space in the last block */
543 if (!err && bytenr & BLOCK_MASK) {
544 size_t size = BLOCK_SIZE - (bytenr & BLOCK_MASK);
547 ret = write_zero(md->out, size);
549 error("unable to zero out buffer: %m");
558 static int read_data_extent(struct metadump_struct *md,
559 struct async_work *async)
561 struct btrfs_root *root = md->root;
562 struct btrfs_fs_info *fs_info = root->fs_info;
563 u64 bytes_left = async->size;
564 u64 logical = async->start;
571 num_copies = btrfs_num_copies(root->fs_info, logical, bytes_left);
573 /* Try our best to read data, just like read_tree_block() */
574 for (cur_mirror = 0; cur_mirror < num_copies; cur_mirror++) {
576 read_len = bytes_left;
577 ret = read_extent_data(fs_info,
578 (char *)(async->buffer + offset),
579 logical, &read_len, cur_mirror);
584 bytes_left -= read_len;
592 static int get_dev_fd(struct btrfs_root *root)
594 struct btrfs_device *dev;
596 dev = list_first_entry(&root->fs_info->fs_devices->devices,
597 struct btrfs_device, dev_list);
601 static int flush_pending(struct metadump_struct *md, int done)
603 struct async_work *async = NULL;
604 struct extent_buffer *eb;
610 if (md->pending_size) {
611 async = calloc(1, sizeof(*async));
615 async->start = md->pending_start;
616 async->size = md->pending_size;
617 async->bufsize = async->size;
618 async->buffer = malloc(async->bufsize);
619 if (!async->buffer) {
624 start = async->start;
628 ret = read_data_extent(md, async);
637 * Balance can make the mapping not cover the super block, so
638 * just copy directly from one of the devices.
640 if (start == BTRFS_SUPER_INFO_OFFSET) {
641 int fd = get_dev_fd(md->root);
643 ret = pread64(fd, async->buffer, size, start);
647 error("unable to read superblock at %llu: %m",
648 (unsigned long long)start);
655 while (!md->data && size > 0) {
656 u64 this_read = min((u64)md->root->fs_info->nodesize,
659 eb = read_tree_block(md->root->fs_info, start, 0);
660 if (!extent_buffer_uptodate(eb)) {
663 error("unable to read metadata block %llu",
664 (unsigned long long)start);
667 copy_buffer(md, async->buffer + offset, eb);
668 free_extent_buffer(eb);
674 md->pending_start = (u64)-1;
675 md->pending_size = 0;
680 pthread_mutex_lock(&md->mutex);
682 list_add_tail(&async->ordered, &md->ordered);
684 if (md->compress_level > 0) {
685 list_add_tail(&async->list, &md->list);
686 pthread_cond_signal(&md->cond);
691 if (md->num_items >= ITEMS_PER_CLUSTER || done) {
692 ret = write_buffers(md, &start);
694 error("unable to write buffers: %s", strerror(-ret));
696 meta_cluster_init(md, start);
698 pthread_mutex_unlock(&md->mutex);
702 static int add_extent(u64 start, u64 size, struct metadump_struct *md,
706 if (md->data != data ||
707 md->pending_size + size > MAX_PENDING_SIZE ||
708 md->pending_start + md->pending_size != start) {
709 ret = flush_pending(md, 0);
712 md->pending_start = start;
714 readahead_tree_block(md->root->fs_info, start, 0);
715 md->pending_size += size;
720 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
721 static int is_tree_block(struct btrfs_root *extent_root,
722 struct btrfs_path *path, u64 bytenr)
724 struct extent_buffer *leaf;
725 struct btrfs_key key;
729 leaf = path->nodes[0];
731 struct btrfs_extent_ref_v0 *ref_item;
733 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
734 ret = btrfs_next_leaf(extent_root, path);
739 leaf = path->nodes[0];
741 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
742 if (key.objectid != bytenr)
744 if (key.type != BTRFS_EXTENT_REF_V0_KEY)
746 ref_item = btrfs_item_ptr(leaf, path->slots[0],
747 struct btrfs_extent_ref_v0);
748 ref_objectid = btrfs_ref_objectid_v0(leaf, ref_item);
749 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID)
757 static int copy_tree_blocks(struct btrfs_root *root, struct extent_buffer *eb,
758 struct metadump_struct *metadump, int root_tree)
760 struct extent_buffer *tmp;
761 struct btrfs_root_item *ri;
762 struct btrfs_key key;
763 struct btrfs_fs_info *fs_info = root->fs_info;
770 ret = add_extent(btrfs_header_bytenr(eb), fs_info->nodesize,
773 error("unable to add metadata block %llu: %d",
774 btrfs_header_bytenr(eb), ret);
778 if (btrfs_header_level(eb) == 0 && !root_tree)
781 level = btrfs_header_level(eb);
782 nritems = btrfs_header_nritems(eb);
783 for (i = 0; i < nritems; i++) {
785 btrfs_item_key_to_cpu(eb, &key, i);
786 if (key.type != BTRFS_ROOT_ITEM_KEY)
788 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
789 bytenr = btrfs_disk_root_bytenr(eb, ri);
790 tmp = read_tree_block(fs_info, bytenr, 0);
791 if (!extent_buffer_uptodate(tmp)) {
792 error("unable to read log root block");
795 ret = copy_tree_blocks(root, tmp, metadump, 0);
796 free_extent_buffer(tmp);
800 bytenr = btrfs_node_blockptr(eb, i);
801 tmp = read_tree_block(fs_info, bytenr, 0);
802 if (!extent_buffer_uptodate(tmp)) {
803 error("unable to read log root block");
806 ret = copy_tree_blocks(root, tmp, metadump, root_tree);
807 free_extent_buffer(tmp);
816 static int copy_log_trees(struct btrfs_root *root,
817 struct metadump_struct *metadump)
819 u64 blocknr = btrfs_super_log_root(root->fs_info->super_copy);
824 if (!root->fs_info->log_root_tree ||
825 !root->fs_info->log_root_tree->node) {
826 error("unable to copy tree log, it has not been setup");
830 return copy_tree_blocks(root, root->fs_info->log_root_tree->node,
834 static int copy_space_cache(struct btrfs_root *root,
835 struct metadump_struct *metadump,
836 struct btrfs_path *path)
838 struct extent_buffer *leaf;
839 struct btrfs_file_extent_item *fi;
840 struct btrfs_key key;
841 u64 bytenr, num_bytes;
844 root = root->fs_info->tree_root;
847 key.type = BTRFS_EXTENT_DATA_KEY;
850 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
852 error("free space inode not found: %d", ret);
856 leaf = path->nodes[0];
859 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
860 ret = btrfs_next_leaf(root, path);
862 error("cannot go to next leaf %d", ret);
867 leaf = path->nodes[0];
870 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
871 if (key.type != BTRFS_EXTENT_DATA_KEY) {
876 fi = btrfs_item_ptr(leaf, path->slots[0],
877 struct btrfs_file_extent_item);
878 if (btrfs_file_extent_type(leaf, fi) !=
879 BTRFS_FILE_EXTENT_REG) {
884 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
885 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
886 ret = add_extent(bytenr, num_bytes, metadump, 1);
888 error("unable to add space cache blocks %d", ret);
889 btrfs_release_path(path);
898 static int copy_from_extent_tree(struct metadump_struct *metadump,
899 struct btrfs_path *path)
901 struct btrfs_root *extent_root;
902 struct extent_buffer *leaf;
903 struct btrfs_extent_item *ei;
904 struct btrfs_key key;
909 extent_root = metadump->root->fs_info->extent_root;
910 bytenr = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
911 key.objectid = bytenr;
912 key.type = BTRFS_EXTENT_ITEM_KEY;
915 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
917 error("extent root not found: %d", ret);
922 leaf = path->nodes[0];
925 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
926 ret = btrfs_next_leaf(extent_root, path);
928 error("cannot go to next leaf %d", ret);
935 leaf = path->nodes[0];
938 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
939 if (key.objectid < bytenr ||
940 (key.type != BTRFS_EXTENT_ITEM_KEY &&
941 key.type != BTRFS_METADATA_ITEM_KEY)) {
946 bytenr = key.objectid;
947 if (key.type == BTRFS_METADATA_ITEM_KEY) {
948 num_bytes = extent_root->fs_info->nodesize;
950 num_bytes = key.offset;
953 if (num_bytes == 0) {
954 error("extent length 0 at bytenr %llu key type %d",
955 (unsigned long long)bytenr, key.type);
960 if (btrfs_item_size_nr(leaf, path->slots[0]) > sizeof(*ei)) {
961 ei = btrfs_item_ptr(leaf, path->slots[0],
962 struct btrfs_extent_item);
963 if (btrfs_extent_flags(leaf, ei) &
964 BTRFS_EXTENT_FLAG_TREE_BLOCK) {
965 ret = add_extent(bytenr, num_bytes, metadump,
968 error("unable to add block %llu: %d",
969 (unsigned long long)bytenr, ret);
974 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
975 ret = is_tree_block(extent_root, path, bytenr);
977 error("failed to check tree block %llu: %d",
978 (unsigned long long)bytenr, ret);
983 ret = add_extent(bytenr, num_bytes, metadump,
986 error("unable to add block %llu: %d",
987 (unsigned long long)bytenr, ret);
994 "either extent tree is corrupted or you haven't built with V0 support");
1002 btrfs_release_path(path);
1007 static int create_metadump(const char *input, FILE *out, int num_threads,
1008 int compress_level, enum sanitize_mode sanitize,
1011 struct btrfs_root *root;
1012 struct btrfs_path path;
1013 struct metadump_struct metadump;
1017 root = open_ctree(input, 0, 0);
1019 error("open ctree failed");
1023 ret = metadump_init(&metadump, root, out, num_threads,
1024 compress_level, sanitize);
1026 error("failed to initialize metadump: %d", ret);
1031 ret = add_extent(BTRFS_SUPER_INFO_OFFSET, BTRFS_SUPER_INFO_SIZE,
1034 error("unable to add metadata: %d", ret);
1039 btrfs_init_path(&path);
1042 ret = copy_tree_blocks(root, root->fs_info->chunk_root->node,
1049 ret = copy_tree_blocks(root, root->fs_info->tree_root->node,
1056 ret = copy_from_extent_tree(&metadump, &path);
1063 ret = copy_log_trees(root, &metadump);
1069 ret = copy_space_cache(root, &metadump, &path);
1071 ret = flush_pending(&metadump, 1);
1075 error("failed to flush pending data: %d", ret);
1078 metadump_destroy(&metadump, num_threads);
1080 btrfs_release_path(&path);
1081 ret = close_ctree(root);
1082 return err ? err : ret;
1085 static void update_super_old(u8 *buffer)
1087 struct btrfs_super_block *super = (struct btrfs_super_block *)buffer;
1088 struct btrfs_chunk *chunk;
1089 struct btrfs_disk_key *key;
1090 u32 sectorsize = btrfs_super_sectorsize(super);
1091 u64 flags = btrfs_super_flags(super);
1093 flags |= BTRFS_SUPER_FLAG_METADUMP;
1094 btrfs_set_super_flags(super, flags);
1096 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1097 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1098 sizeof(struct btrfs_disk_key));
1100 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1101 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1102 btrfs_set_disk_key_offset(key, 0);
1104 btrfs_set_stack_chunk_length(chunk, (u64)-1);
1105 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1106 btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN);
1107 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1108 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1109 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1110 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1111 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1112 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1113 chunk->stripe.devid = super->dev_item.devid;
1114 btrfs_set_stack_stripe_offset(&chunk->stripe, 0);
1115 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1116 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1117 csum_block(buffer, BTRFS_SUPER_INFO_SIZE);
1120 static int update_super(struct mdrestore_struct *mdres, u8 *buffer)
1122 struct btrfs_super_block *super = (struct btrfs_super_block *)buffer;
1123 struct btrfs_chunk *chunk;
1124 struct btrfs_disk_key *disk_key;
1125 struct btrfs_key key;
1126 u64 flags = btrfs_super_flags(super);
1127 u32 new_array_size = 0;
1130 u8 *ptr, *write_ptr;
1131 int old_num_stripes;
1133 write_ptr = ptr = super->sys_chunk_array;
1134 array_size = btrfs_super_sys_array_size(super);
1136 while (cur < array_size) {
1137 disk_key = (struct btrfs_disk_key *)ptr;
1138 btrfs_disk_key_to_cpu(&key, disk_key);
1140 new_array_size += sizeof(*disk_key);
1141 memmove(write_ptr, ptr, sizeof(*disk_key));
1143 write_ptr += sizeof(*disk_key);
1144 ptr += sizeof(*disk_key);
1145 cur += sizeof(*disk_key);
1147 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1148 u64 type, physical, physical_dup, size = 0;
1150 chunk = (struct btrfs_chunk *)ptr;
1151 old_num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1152 chunk = (struct btrfs_chunk *)write_ptr;
1154 memmove(write_ptr, ptr, sizeof(*chunk));
1155 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1156 type = btrfs_stack_chunk_type(chunk);
1157 if (type & BTRFS_BLOCK_GROUP_DUP) {
1158 new_array_size += sizeof(struct btrfs_stripe);
1159 write_ptr += sizeof(struct btrfs_stripe);
1161 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1162 btrfs_set_stack_chunk_type(chunk,
1163 BTRFS_BLOCK_GROUP_SYSTEM);
1165 chunk->stripe.devid = super->dev_item.devid;
1166 physical = logical_to_physical(mdres, key.offset,
1167 &size, &physical_dup);
1168 if (size != (u64)-1)
1169 btrfs_set_stack_stripe_offset(&chunk->stripe,
1171 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid,
1173 new_array_size += sizeof(*chunk);
1175 error("bogus key in the sys array %d", key.type);
1178 write_ptr += sizeof(*chunk);
1179 ptr += btrfs_chunk_item_size(old_num_stripes);
1180 cur += btrfs_chunk_item_size(old_num_stripes);
1183 if (mdres->clear_space_cache)
1184 btrfs_set_super_cache_generation(super, 0);
1186 flags |= BTRFS_SUPER_FLAG_METADUMP_V2;
1187 btrfs_set_super_flags(super, flags);
1188 btrfs_set_super_sys_array_size(super, new_array_size);
1189 btrfs_set_super_num_devices(super, 1);
1190 csum_block(buffer, BTRFS_SUPER_INFO_SIZE);
1195 static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size)
1197 struct extent_buffer *eb;
1199 eb = calloc(1, sizeof(struct extent_buffer) + size);
1208 static void truncate_item(struct extent_buffer *eb, int slot, u32 new_size)
1210 struct btrfs_item *item;
1218 old_size = btrfs_item_size_nr(eb, slot);
1219 if (old_size == new_size)
1222 nritems = btrfs_header_nritems(eb);
1223 data_end = btrfs_item_offset_nr(eb, nritems - 1);
1225 old_data_start = btrfs_item_offset_nr(eb, slot);
1226 size_diff = old_size - new_size;
1228 for (i = slot; i < nritems; i++) {
1230 item = btrfs_item_nr(i);
1231 ioff = btrfs_item_offset(eb, item);
1232 btrfs_set_item_offset(eb, item, ioff + size_diff);
1235 memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end + size_diff,
1236 btrfs_leaf_data(eb) + data_end,
1237 old_data_start + new_size - data_end);
1238 item = btrfs_item_nr(slot);
1239 btrfs_set_item_size(eb, item, new_size);
1242 static int fixup_chunk_tree_block(struct mdrestore_struct *mdres,
1243 struct async_work *async, u8 *buffer,
1246 struct extent_buffer *eb;
1247 size_t size_left = size;
1248 u64 bytenr = async->start;
1251 if (size_left % mdres->nodesize)
1254 eb = alloc_dummy_eb(bytenr, mdres->nodesize);
1260 memcpy(eb->data, buffer, mdres->nodesize);
1262 if (btrfs_header_bytenr(eb) != bytenr)
1264 if (memcmp(mdres->fsid,
1265 eb->data + offsetof(struct btrfs_header, fsid),
1269 if (btrfs_header_owner(eb) != BTRFS_CHUNK_TREE_OBJECTID)
1272 if (btrfs_header_level(eb) != 0)
1275 for (i = 0; i < btrfs_header_nritems(eb); i++) {
1276 struct btrfs_chunk *chunk;
1277 struct btrfs_key key;
1278 u64 type, physical, physical_dup, size = (u64)-1;
1280 btrfs_item_key_to_cpu(eb, &key, i);
1281 if (key.type != BTRFS_CHUNK_ITEM_KEY)
1285 physical = logical_to_physical(mdres, key.offset,
1286 &size, &physical_dup);
1289 truncate_item(eb, i, sizeof(*chunk));
1290 chunk = btrfs_item_ptr(eb, i, struct btrfs_chunk);
1293 /* Zero out the RAID profile */
1294 type = btrfs_chunk_type(eb, chunk);
1295 type &= (BTRFS_BLOCK_GROUP_DATA |
1296 BTRFS_BLOCK_GROUP_SYSTEM |
1297 BTRFS_BLOCK_GROUP_METADATA |
1298 BTRFS_BLOCK_GROUP_DUP);
1299 btrfs_set_chunk_type(eb, chunk, type);
1302 btrfs_set_chunk_num_stripes(eb, chunk, 1);
1303 btrfs_set_chunk_sub_stripes(eb, chunk, 0);
1304 btrfs_set_stripe_devid_nr(eb, chunk, 0, mdres->devid);
1305 if (size != (u64)-1)
1306 btrfs_set_stripe_offset_nr(eb, chunk, 0,
1308 /* update stripe 2 offset */
1310 btrfs_set_stripe_offset_nr(eb, chunk, 1,
1313 write_extent_buffer(eb, mdres->uuid,
1314 (unsigned long)btrfs_stripe_dev_uuid_nr(
1318 memcpy(buffer, eb->data, eb->len);
1319 csum_block(buffer, eb->len);
1321 size_left -= mdres->nodesize;
1322 buffer += mdres->nodesize;
1323 bytenr += mdres->nodesize;
1330 static void write_backup_supers(int fd, u8 *buf)
1332 struct btrfs_super_block *super = (struct btrfs_super_block *)buf;
1339 if (fstat(fd, &st)) {
1341 "cannot stat restore point, won't be able to write backup supers: %m");
1345 size = btrfs_device_size(fd, &st);
1347 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1348 bytenr = btrfs_sb_offset(i);
1349 if (bytenr + BTRFS_SUPER_INFO_SIZE > size)
1351 btrfs_set_super_bytenr(super, bytenr);
1352 csum_block(buf, BTRFS_SUPER_INFO_SIZE);
1353 ret = pwrite64(fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
1354 if (ret < BTRFS_SUPER_INFO_SIZE) {
1357 "problem writing out backup super block %d: %m", i);
1359 error("short write writing out backup super block");
1365 static void *restore_worker(void *data)
1367 struct mdrestore_struct *mdres = (struct mdrestore_struct *)data;
1368 struct async_work *async;
1374 int compress_size = MAX_PENDING_SIZE * 4;
1376 outfd = fileno(mdres->out);
1377 buffer = malloc(compress_size);
1379 error("not enough memory for restore worker buffer");
1380 pthread_mutex_lock(&mdres->mutex);
1382 mdres->error = -ENOMEM;
1383 pthread_mutex_unlock(&mdres->mutex);
1388 u64 bytenr, physical_dup;
1392 pthread_mutex_lock(&mdres->mutex);
1393 while (!mdres->nodesize || list_empty(&mdres->list)) {
1395 pthread_mutex_unlock(&mdres->mutex);
1398 pthread_cond_wait(&mdres->cond, &mdres->mutex);
1400 async = list_entry(mdres->list.next, struct async_work, list);
1401 list_del_init(&async->list);
1403 if (mdres->compress_method == COMPRESS_ZLIB) {
1404 size = compress_size;
1405 pthread_mutex_unlock(&mdres->mutex);
1406 ret = uncompress(buffer, (unsigned long *)&size,
1407 async->buffer, async->bufsize);
1408 pthread_mutex_lock(&mdres->mutex);
1410 error("decompression failed with %d", ret);
1415 outbuf = async->buffer;
1416 size = async->bufsize;
1419 if (!mdres->multi_devices) {
1420 if (async->start == BTRFS_SUPER_INFO_OFFSET) {
1421 if (mdres->old_restore) {
1422 update_super_old(outbuf);
1424 ret = update_super(mdres, outbuf);
1428 } else if (!mdres->old_restore) {
1429 ret = fixup_chunk_tree_block(mdres, async, outbuf, size);
1435 if (!mdres->fixup_offset) {
1437 u64 chunk_size = size;
1439 if (!mdres->multi_devices && !mdres->old_restore)
1440 bytenr = logical_to_physical(mdres,
1441 async->start + offset,
1445 bytenr = async->start + offset;
1447 ret = pwrite64(outfd, outbuf+offset, chunk_size,
1449 if (ret != chunk_size)
1453 ret = pwrite64(outfd, outbuf+offset,
1456 if (ret != chunk_size)
1460 offset += chunk_size;
1465 error("unable to write to device: %m");
1468 error("short write");
1472 } else if (async->start != BTRFS_SUPER_INFO_OFFSET) {
1473 ret = write_data_to_disk(mdres->info, outbuf, async->start, size, 0);
1475 error("failed to write data");
1481 /* backup super blocks are already there at fixup_offset stage */
1482 if (!mdres->multi_devices && async->start == BTRFS_SUPER_INFO_OFFSET)
1483 write_backup_supers(outfd, outbuf);
1485 if (err && !mdres->error)
1488 pthread_mutex_unlock(&mdres->mutex);
1490 free(async->buffer);
1498 static void mdrestore_destroy(struct mdrestore_struct *mdres, int num_threads)
1503 while ((n = rb_first(&mdres->chunk_tree))) {
1504 struct fs_chunk *entry;
1506 entry = rb_entry(n, struct fs_chunk, l);
1507 rb_erase(n, &mdres->chunk_tree);
1508 rb_erase(&entry->p, &mdres->physical_tree);
1511 pthread_mutex_lock(&mdres->mutex);
1513 pthread_cond_broadcast(&mdres->cond);
1514 pthread_mutex_unlock(&mdres->mutex);
1516 for (i = 0; i < num_threads; i++)
1517 pthread_join(mdres->threads[i], NULL);
1519 pthread_cond_destroy(&mdres->cond);
1520 pthread_mutex_destroy(&mdres->mutex);
1523 static int mdrestore_init(struct mdrestore_struct *mdres,
1524 FILE *in, FILE *out, int old_restore,
1525 int num_threads, int fixup_offset,
1526 struct btrfs_fs_info *info, int multi_devices)
1530 memset(mdres, 0, sizeof(*mdres));
1531 pthread_cond_init(&mdres->cond, NULL);
1532 pthread_mutex_init(&mdres->mutex, NULL);
1533 INIT_LIST_HEAD(&mdres->list);
1534 INIT_LIST_HEAD(&mdres->overlapping_chunks);
1537 mdres->old_restore = old_restore;
1538 mdres->chunk_tree.rb_node = NULL;
1539 mdres->fixup_offset = fixup_offset;
1541 mdres->multi_devices = multi_devices;
1542 mdres->clear_space_cache = 0;
1543 mdres->last_physical_offset = 0;
1544 mdres->alloced_chunks = 0;
1549 mdres->num_threads = num_threads;
1550 for (i = 0; i < num_threads; i++) {
1551 ret = pthread_create(&mdres->threads[i], NULL, restore_worker,
1554 /* pthread_create returns errno directly */
1560 mdrestore_destroy(mdres, i + 1);
1564 static int fill_mdres_info(struct mdrestore_struct *mdres,
1565 struct async_work *async)
1567 struct btrfs_super_block *super;
1572 /* We've already been initialized */
1573 if (mdres->nodesize)
1576 if (mdres->compress_method == COMPRESS_ZLIB) {
1577 size_t size = MAX_PENDING_SIZE * 2;
1579 buffer = malloc(MAX_PENDING_SIZE * 2);
1582 ret = uncompress(buffer, (unsigned long *)&size,
1583 async->buffer, async->bufsize);
1585 error("decompression failed with %d", ret);
1591 outbuf = async->buffer;
1594 super = (struct btrfs_super_block *)outbuf;
1595 mdres->nodesize = btrfs_super_nodesize(super);
1596 memcpy(mdres->fsid, super->fsid, BTRFS_FSID_SIZE);
1597 memcpy(mdres->uuid, super->dev_item.uuid,
1599 mdres->devid = le64_to_cpu(super->dev_item.devid);
1604 static int add_cluster(struct meta_cluster *cluster,
1605 struct mdrestore_struct *mdres, u64 *next)
1607 struct meta_cluster_item *item;
1608 struct meta_cluster_header *header = &cluster->header;
1609 struct async_work *async;
1614 pthread_mutex_lock(&mdres->mutex);
1615 mdres->compress_method = header->compress;
1616 pthread_mutex_unlock(&mdres->mutex);
1618 bytenr = le64_to_cpu(header->bytenr) + BLOCK_SIZE;
1619 nritems = le32_to_cpu(header->nritems);
1620 for (i = 0; i < nritems; i++) {
1621 item = &cluster->items[i];
1622 async = calloc(1, sizeof(*async));
1624 error("not enough memory for async data");
1627 async->start = le64_to_cpu(item->bytenr);
1628 async->bufsize = le32_to_cpu(item->size);
1629 async->buffer = malloc(async->bufsize);
1630 if (!async->buffer) {
1631 error("not enough memory for async buffer");
1635 ret = fread(async->buffer, async->bufsize, 1, mdres->in);
1637 error("unable to read buffer: %m");
1638 free(async->buffer);
1642 bytenr += async->bufsize;
1644 pthread_mutex_lock(&mdres->mutex);
1645 if (async->start == BTRFS_SUPER_INFO_OFFSET) {
1646 ret = fill_mdres_info(mdres, async);
1648 error("unable to set up restore state");
1649 pthread_mutex_unlock(&mdres->mutex);
1650 free(async->buffer);
1655 list_add_tail(&async->list, &mdres->list);
1657 pthread_cond_signal(&mdres->cond);
1658 pthread_mutex_unlock(&mdres->mutex);
1660 if (bytenr & BLOCK_MASK) {
1661 char buffer[BLOCK_MASK];
1662 size_t size = BLOCK_SIZE - (bytenr & BLOCK_MASK);
1665 ret = fread(buffer, size, 1, mdres->in);
1667 error("failed to read buffer: %m");
1675 static int wait_for_worker(struct mdrestore_struct *mdres)
1679 pthread_mutex_lock(&mdres->mutex);
1681 while (!ret && mdres->num_items > 0) {
1682 struct timespec ts = {
1684 .tv_nsec = 10000000,
1686 pthread_mutex_unlock(&mdres->mutex);
1687 nanosleep(&ts, NULL);
1688 pthread_mutex_lock(&mdres->mutex);
1691 pthread_mutex_unlock(&mdres->mutex);
1695 static int read_chunk_block(struct mdrestore_struct *mdres, u8 *buffer,
1696 u64 bytenr, u64 item_bytenr, u32 bufsize,
1699 struct extent_buffer *eb;
1703 eb = alloc_dummy_eb(bytenr, mdres->nodesize);
1709 while (item_bytenr != bytenr) {
1710 buffer += mdres->nodesize;
1711 item_bytenr += mdres->nodesize;
1714 memcpy(eb->data, buffer, mdres->nodesize);
1715 if (btrfs_header_bytenr(eb) != bytenr) {
1716 error("eb bytenr does not match found bytenr: %llu != %llu",
1717 (unsigned long long)btrfs_header_bytenr(eb),
1718 (unsigned long long)bytenr);
1723 if (memcmp(mdres->fsid, eb->data + offsetof(struct btrfs_header, fsid),
1725 error("filesystem UUID of eb %llu does not match",
1726 (unsigned long long)bytenr);
1731 if (btrfs_header_owner(eb) != BTRFS_CHUNK_TREE_OBJECTID) {
1732 error("wrong eb %llu owner %llu",
1733 (unsigned long long)bytenr,
1734 (unsigned long long)btrfs_header_owner(eb));
1739 for (i = 0; i < btrfs_header_nritems(eb); i++) {
1740 struct btrfs_chunk *chunk;
1741 struct fs_chunk *fs_chunk;
1742 struct btrfs_key key;
1745 if (btrfs_header_level(eb)) {
1746 u64 blockptr = btrfs_node_blockptr(eb, i);
1748 ret = search_for_chunk_blocks(mdres, blockptr,
1755 /* Yay a leaf! We loves leafs! */
1756 btrfs_item_key_to_cpu(eb, &key, i);
1757 if (key.type != BTRFS_CHUNK_ITEM_KEY)
1760 fs_chunk = malloc(sizeof(struct fs_chunk));
1762 error("not enough memory to allocate chunk");
1766 memset(fs_chunk, 0, sizeof(*fs_chunk));
1767 chunk = btrfs_item_ptr(eb, i, struct btrfs_chunk);
1769 fs_chunk->logical = key.offset;
1770 fs_chunk->physical = btrfs_stripe_offset_nr(eb, chunk, 0);
1771 fs_chunk->bytes = btrfs_chunk_length(eb, chunk);
1772 INIT_LIST_HEAD(&fs_chunk->list);
1773 if (tree_search(&mdres->physical_tree, &fs_chunk->p,
1774 physical_cmp, 1) != NULL)
1775 list_add(&fs_chunk->list, &mdres->overlapping_chunks);
1777 tree_insert(&mdres->physical_tree, &fs_chunk->p,
1780 type = btrfs_chunk_type(eb, chunk);
1781 if (type & BTRFS_BLOCK_GROUP_DUP) {
1782 fs_chunk->physical_dup =
1783 btrfs_stripe_offset_nr(eb, chunk, 1);
1786 if (fs_chunk->physical_dup + fs_chunk->bytes >
1787 mdres->last_physical_offset)
1788 mdres->last_physical_offset = fs_chunk->physical_dup +
1790 else if (fs_chunk->physical + fs_chunk->bytes >
1791 mdres->last_physical_offset)
1792 mdres->last_physical_offset = fs_chunk->physical +
1794 mdres->alloced_chunks += fs_chunk->bytes;
1795 /* in dup case, fs_chunk->bytes should add twice */
1796 if (fs_chunk->physical_dup)
1797 mdres->alloced_chunks += fs_chunk->bytes;
1798 tree_insert(&mdres->chunk_tree, &fs_chunk->l, chunk_cmp);
1805 /* If you have to ask you aren't worthy */
1806 static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
1807 u64 search, u64 cluster_bytenr)
1809 struct meta_cluster *cluster;
1810 struct meta_cluster_header *header;
1811 struct meta_cluster_item *item;
1812 u64 current_cluster = cluster_bytenr, bytenr;
1814 u32 bufsize, nritems, i;
1815 u32 max_size = MAX_PENDING_SIZE * 2;
1816 u8 *buffer, *tmp = NULL;
1819 cluster = malloc(BLOCK_SIZE);
1821 error("not enough memory for cluster");
1825 buffer = malloc(max_size);
1827 error("not enough memory for buffer");
1832 if (mdres->compress_method == COMPRESS_ZLIB) {
1833 tmp = malloc(max_size);
1835 error("not enough memory for buffer");
1842 bytenr = current_cluster;
1844 if (fseek(mdres->in, current_cluster, SEEK_SET)) {
1845 error("seek failed: %m");
1850 ret = fread(cluster, BLOCK_SIZE, 1, mdres->in);
1852 if (cluster_bytenr != 0) {
1854 current_cluster = 0;
1859 "unknown state after reading cluster at %llu, probably corrupted data",
1863 } else if (ret < 0) {
1864 error("unable to read image at %llu: %m",
1865 (unsigned long long)cluster_bytenr);
1870 header = &cluster->header;
1871 if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
1872 le64_to_cpu(header->bytenr) != current_cluster) {
1873 error("bad header in metadump image");
1878 bytenr += BLOCK_SIZE;
1879 nritems = le32_to_cpu(header->nritems);
1880 for (i = 0; i < nritems; i++) {
1883 item = &cluster->items[i];
1884 bufsize = le32_to_cpu(item->size);
1885 item_bytenr = le64_to_cpu(item->bytenr);
1887 if (bufsize > max_size) {
1888 error("item %u too big: %u > %u", i, bufsize,
1894 if (mdres->compress_method == COMPRESS_ZLIB) {
1895 ret = fread(tmp, bufsize, 1, mdres->in);
1897 error("read error: %m");
1903 ret = uncompress(buffer,
1904 (unsigned long *)&size, tmp,
1907 error("decompression failed with %d",
1913 ret = fread(buffer, bufsize, 1, mdres->in);
1915 error("read error: %m");
1923 if (item_bytenr <= search &&
1924 item_bytenr + size > search) {
1925 ret = read_chunk_block(mdres, buffer, search,
1939 if (bytenr & BLOCK_MASK)
1940 bytenr += BLOCK_SIZE - (bytenr & BLOCK_MASK);
1941 current_cluster = bytenr;
1950 static int build_chunk_tree(struct mdrestore_struct *mdres,
1951 struct meta_cluster *cluster)
1953 struct btrfs_super_block *super;
1954 struct meta_cluster_header *header;
1955 struct meta_cluster_item *item = NULL;
1956 u64 chunk_root_bytenr = 0;
1962 /* We can't seek with stdin so don't bother doing this */
1963 if (mdres->in == stdin)
1966 ret = fread(cluster, BLOCK_SIZE, 1, mdres->in);
1968 error("unable to read cluster: %m");
1973 header = &cluster->header;
1974 if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
1975 le64_to_cpu(header->bytenr) != 0) {
1976 error("bad header in metadump image");
1980 bytenr += BLOCK_SIZE;
1981 mdres->compress_method = header->compress;
1982 nritems = le32_to_cpu(header->nritems);
1983 for (i = 0; i < nritems; i++) {
1984 item = &cluster->items[i];
1986 if (le64_to_cpu(item->bytenr) == BTRFS_SUPER_INFO_OFFSET)
1988 bytenr += le32_to_cpu(item->size);
1989 if (fseek(mdres->in, le32_to_cpu(item->size), SEEK_CUR)) {
1990 error("seek failed: %m");
1995 if (!item || le64_to_cpu(item->bytenr) != BTRFS_SUPER_INFO_OFFSET) {
1996 error("did not find superblock at %llu",
1997 le64_to_cpu(item->bytenr));
2001 buffer = malloc(le32_to_cpu(item->size));
2003 error("not enough memory to allocate buffer");
2007 ret = fread(buffer, le32_to_cpu(item->size), 1, mdres->in);
2009 error("unable to read buffer: %m");
2014 if (mdres->compress_method == COMPRESS_ZLIB) {
2015 size_t size = MAX_PENDING_SIZE * 2;
2018 tmp = malloc(MAX_PENDING_SIZE * 2);
2023 ret = uncompress(tmp, (unsigned long *)&size,
2024 buffer, le32_to_cpu(item->size));
2026 error("decompression failed with %d", ret);
2035 pthread_mutex_lock(&mdres->mutex);
2036 super = (struct btrfs_super_block *)buffer;
2037 chunk_root_bytenr = btrfs_super_chunk_root(super);
2038 mdres->nodesize = btrfs_super_nodesize(super);
2039 memcpy(mdres->fsid, super->fsid, BTRFS_FSID_SIZE);
2040 memcpy(mdres->uuid, super->dev_item.uuid,
2042 mdres->devid = le64_to_cpu(super->dev_item.devid);
2044 pthread_mutex_unlock(&mdres->mutex);
2046 return search_for_chunk_blocks(mdres, chunk_root_bytenr, 0);
2049 static int range_contains_super(u64 physical, u64 bytes)
2054 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2055 super_bytenr = btrfs_sb_offset(i);
2056 if (super_bytenr >= physical &&
2057 super_bytenr < physical + bytes)
2064 static void remap_overlapping_chunks(struct mdrestore_struct *mdres)
2066 struct fs_chunk *fs_chunk;
2068 while (!list_empty(&mdres->overlapping_chunks)) {
2069 fs_chunk = list_first_entry(&mdres->overlapping_chunks,
2070 struct fs_chunk, list);
2071 list_del_init(&fs_chunk->list);
2072 if (range_contains_super(fs_chunk->physical,
2075 "remapping a chunk that had a super mirror inside of it, clearing space cache so we don't end up with corruption");
2076 mdres->clear_space_cache = 1;
2078 fs_chunk->physical = mdres->last_physical_offset;
2079 tree_insert(&mdres->physical_tree, &fs_chunk->p, physical_cmp);
2080 mdres->last_physical_offset += fs_chunk->bytes;
2084 static int fixup_devices(struct btrfs_fs_info *fs_info,
2085 struct mdrestore_struct *mdres, off_t dev_size)
2087 struct btrfs_trans_handle *trans;
2088 struct btrfs_dev_item *dev_item;
2089 struct btrfs_path path;
2090 struct extent_buffer *leaf;
2091 struct btrfs_root *root = fs_info->chunk_root;
2092 struct btrfs_key key;
2093 u64 devid, cur_devid;
2096 trans = btrfs_start_transaction(fs_info->tree_root, 1);
2097 if (IS_ERR(trans)) {
2098 error("cannot starting transaction %ld", PTR_ERR(trans));
2099 return PTR_ERR(trans);
2102 dev_item = &fs_info->super_copy->dev_item;
2104 devid = btrfs_stack_device_id(dev_item);
2106 btrfs_set_stack_device_total_bytes(dev_item, dev_size);
2107 btrfs_set_stack_device_bytes_used(dev_item, mdres->alloced_chunks);
2109 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2110 key.type = BTRFS_DEV_ITEM_KEY;
2113 btrfs_init_path(&path);
2116 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
2118 error("search failed: %d", ret);
2123 leaf = path.nodes[0];
2124 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2125 ret = btrfs_next_leaf(root, &path);
2127 error("cannot go to next leaf %d", ret);
2134 leaf = path.nodes[0];
2137 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2138 if (key.type > BTRFS_DEV_ITEM_KEY)
2140 if (key.type != BTRFS_DEV_ITEM_KEY) {
2145 dev_item = btrfs_item_ptr(leaf, path.slots[0],
2146 struct btrfs_dev_item);
2147 cur_devid = btrfs_device_id(leaf, dev_item);
2148 if (devid != cur_devid) {
2149 ret = btrfs_del_item(trans, root, &path);
2151 error("cannot delete item: %d", ret);
2154 btrfs_release_path(&path);
2158 btrfs_set_device_total_bytes(leaf, dev_item, dev_size);
2159 btrfs_set_device_bytes_used(leaf, dev_item,
2160 mdres->alloced_chunks);
2161 btrfs_mark_buffer_dirty(leaf);
2165 btrfs_release_path(&path);
2166 ret = btrfs_commit_transaction(trans, fs_info->tree_root);
2168 error("unable to commit transaction: %d", ret);
2174 static int restore_metadump(const char *input, FILE *out, int old_restore,
2175 int num_threads, int fixup_offset,
2176 const char *target, int multi_devices)
2178 struct meta_cluster *cluster = NULL;
2179 struct meta_cluster_header *header;
2180 struct mdrestore_struct mdrestore;
2181 struct btrfs_fs_info *info = NULL;
2186 if (!strcmp(input, "-")) {
2189 in = fopen(input, "r");
2191 error("unable to open metadump image: %m");
2196 /* NOTE: open with write mode */
2198 info = open_ctree_fs_info(target, 0, 0, 0,
2200 OPEN_CTREE_RESTORE |
2201 OPEN_CTREE_PARTIAL);
2203 error("open ctree failed");
2209 cluster = malloc(BLOCK_SIZE);
2211 error("not enough memory for cluster");
2216 ret = mdrestore_init(&mdrestore, in, out, old_restore, num_threads,
2217 fixup_offset, info, multi_devices);
2219 error("failed to initialize metadata restore state: %d", ret);
2220 goto failed_cluster;
2223 if (!multi_devices && !old_restore) {
2224 ret = build_chunk_tree(&mdrestore, cluster);
2227 if (!list_empty(&mdrestore.overlapping_chunks))
2228 remap_overlapping_chunks(&mdrestore);
2231 if (in != stdin && fseek(in, 0, SEEK_SET)) {
2232 error("seek failed: %m");
2236 while (!mdrestore.error) {
2237 ret = fread(cluster, BLOCK_SIZE, 1, in);
2241 header = &cluster->header;
2242 if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
2243 le64_to_cpu(header->bytenr) != bytenr) {
2244 error("bad header in metadump image");
2248 ret = add_cluster(cluster, &mdrestore, &bytenr);
2250 error("failed to add cluster: %d", ret);
2254 ret = wait_for_worker(&mdrestore);
2256 if (!ret && !multi_devices && !old_restore) {
2257 struct btrfs_root *root;
2260 root = open_ctree_fd(fileno(out), target, 0,
2261 OPEN_CTREE_PARTIAL |
2263 OPEN_CTREE_NO_DEVICES);
2265 error("open ctree failed in %s", target);
2269 info = root->fs_info;
2271 if (stat(target, &st)) {
2272 error("stat %s failed: %m", target);
2273 close_ctree(info->chunk_root);
2278 ret = fixup_devices(info, &mdrestore, st.st_size);
2279 close_ctree(info->chunk_root);
2284 mdrestore_destroy(&mdrestore, num_threads);
2288 if (fixup_offset && info)
2289 close_ctree(info->chunk_root);
2296 static int update_disk_super_on_device(struct btrfs_fs_info *info,
2297 const char *other_dev, u64 cur_devid)
2299 struct btrfs_key key;
2300 struct extent_buffer *leaf;
2301 struct btrfs_path path;
2302 struct btrfs_dev_item *dev_item;
2303 struct btrfs_super_block *disk_super;
2304 char dev_uuid[BTRFS_UUID_SIZE];
2305 char fs_uuid[BTRFS_UUID_SIZE];
2306 u64 devid, type, io_align, io_width;
2307 u64 sector_size, total_bytes, bytes_used;
2308 char buf[BTRFS_SUPER_INFO_SIZE];
2312 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2313 key.type = BTRFS_DEV_ITEM_KEY;
2314 key.offset = cur_devid;
2316 btrfs_init_path(&path);
2317 ret = btrfs_search_slot(NULL, info->chunk_root, &key, &path, 0, 0);
2319 error("search key failed: %d", ret);
2324 leaf = path.nodes[0];
2325 dev_item = btrfs_item_ptr(leaf, path.slots[0],
2326 struct btrfs_dev_item);
2328 devid = btrfs_device_id(leaf, dev_item);
2329 if (devid != cur_devid) {
2330 error("devid mismatch: %llu != %llu",
2331 (unsigned long long)devid,
2332 (unsigned long long)cur_devid);
2337 type = btrfs_device_type(leaf, dev_item);
2338 io_align = btrfs_device_io_align(leaf, dev_item);
2339 io_width = btrfs_device_io_width(leaf, dev_item);
2340 sector_size = btrfs_device_sector_size(leaf, dev_item);
2341 total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2342 bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2343 read_extent_buffer(leaf, dev_uuid, (unsigned long)btrfs_device_uuid(dev_item), BTRFS_UUID_SIZE);
2344 read_extent_buffer(leaf, fs_uuid, (unsigned long)btrfs_device_fsid(dev_item), BTRFS_UUID_SIZE);
2346 btrfs_release_path(&path);
2348 printf("update disk super on %s devid=%llu\n", other_dev, devid);
2350 /* update other devices' super block */
2351 fp = open(other_dev, O_CREAT | O_RDWR, 0600);
2353 error("could not open %s: %m", other_dev);
2358 memcpy(buf, info->super_copy, BTRFS_SUPER_INFO_SIZE);
2360 disk_super = (struct btrfs_super_block *)buf;
2361 dev_item = &disk_super->dev_item;
2363 btrfs_set_stack_device_type(dev_item, type);
2364 btrfs_set_stack_device_id(dev_item, devid);
2365 btrfs_set_stack_device_total_bytes(dev_item, total_bytes);
2366 btrfs_set_stack_device_bytes_used(dev_item, bytes_used);
2367 btrfs_set_stack_device_io_align(dev_item, io_align);
2368 btrfs_set_stack_device_io_width(dev_item, io_width);
2369 btrfs_set_stack_device_sector_size(dev_item, sector_size);
2370 memcpy(dev_item->uuid, dev_uuid, BTRFS_UUID_SIZE);
2371 memcpy(dev_item->fsid, fs_uuid, BTRFS_UUID_SIZE);
2372 csum_block((u8 *)buf, BTRFS_SUPER_INFO_SIZE);
2374 ret = pwrite64(fp, buf, BTRFS_SUPER_INFO_SIZE, BTRFS_SUPER_INFO_OFFSET);
2375 if (ret != BTRFS_SUPER_INFO_SIZE) {
2377 error("cannot write superblock: %s", strerror(ret));
2379 error("cannot write superblock");
2384 write_backup_supers(fp, (u8 *)buf);
2392 static void print_usage(int ret)
2394 printf("usage: btrfs-image [options] source target\n");
2395 printf("\t-r \trestore metadump image\n");
2396 printf("\t-c value\tcompression level (0 ~ 9)\n");
2397 printf("\t-t value\tnumber of threads (1 ~ 32)\n");
2398 printf("\t-o \tdon't mess with the chunk tree when restoring\n");
2399 printf("\t-s \tsanitize file names, use once to just use garbage, use twice if you want crc collisions\n");
2400 printf("\t-w \twalk all trees instead of using extent tree, do this if your extent tree is broken\n");
2401 printf("\t-m \trestore for multiple devices\n");
2403 printf("\tIn the dump mode, source is the btrfs device and target is the output file (use '-' for stdout).\n");
2404 printf("\tIn the restore mode, source is the dumped image and target is the btrfs device/file.\n");
2408 int main(int argc, char *argv[])
2412 u64 num_threads = 0;
2413 u64 compress_level = 0;
2415 int old_restore = 0;
2417 int multi_devices = 0;
2419 enum sanitize_mode sanitize = SANITIZE_NONE;
2421 int usage_error = 0;
2425 static const struct option long_options[] = {
2426 { "help", no_argument, NULL, GETOPT_VAL_HELP},
2427 { NULL, 0, NULL, 0 }
2429 int c = getopt_long(argc, argv, "rc:t:oswm", long_options, NULL);
2437 num_threads = arg_strtou64(optarg);
2438 if (num_threads > MAX_WORKER_THREADS) {
2439 error("number of threads out of range: %llu > %d",
2440 (unsigned long long)num_threads,
2441 MAX_WORKER_THREADS);
2446 compress_level = arg_strtou64(optarg);
2447 if (compress_level > 9) {
2448 error("compression level out of range: %llu",
2449 (unsigned long long)compress_level);
2457 if (sanitize == SANITIZE_NONE)
2458 sanitize = SANITIZE_NAMES;
2459 else if (sanitize == SANITIZE_NAMES)
2460 sanitize = SANITIZE_COLLISIONS;
2469 case GETOPT_VAL_HELP:
2471 print_usage(c != GETOPT_VAL_HELP);
2476 if (check_argc_min(argc - optind, 2))
2479 dev_cnt = argc - optind - 1;
2484 "create and restore cannot be used at the same time");
2488 if (walk_trees || sanitize != SANITIZE_NONE || compress_level) {
2490 "useing -w, -s, -c options for restore makes no sense");
2493 if (multi_devices && dev_cnt < 2) {
2494 error("not enough devices specified for -m option");
2497 if (!multi_devices && dev_cnt != 1) {
2498 error("accepts only 1 device without -m option");
2506 source = argv[optind];
2507 target = argv[optind + 1];
2509 if (create && !strcmp(target, "-")) {
2512 out = fopen(target, "w+");
2514 error("unable to create target file %s", target);
2519 if (compress_level > 0 || create == 0) {
2520 if (num_threads == 0) {
2521 long tmp = sysconf(_SC_NPROCESSORS_ONLN);
2532 ret = check_mounted(source);
2534 warning("unable to check mount status of: %s",
2537 warning("%s already mounted, results may be inaccurate",
2541 ret = create_metadump(source, out, num_threads,
2542 compress_level, sanitize, walk_trees);
2544 ret = restore_metadump(source, out, old_restore, num_threads,
2545 0, target, multi_devices);
2548 error("%s failed: %m", (create) ? "create" : "restore");
2552 /* extended support for multiple devices */
2553 if (!create && multi_devices) {
2554 struct btrfs_fs_info *info;
2558 info = open_ctree_fs_info(target, 0, 0, 0,
2559 OPEN_CTREE_PARTIAL |
2560 OPEN_CTREE_RESTORE);
2562 error("open ctree failed at %s", target);
2566 total_devs = btrfs_super_num_devices(info->super_copy);
2567 if (total_devs != dev_cnt) {
2568 error("it needs %llu devices but has only %d",
2569 total_devs, dev_cnt);
2570 close_ctree(info->chunk_root);
2574 /* update super block on other disks */
2575 for (i = 2; i <= dev_cnt; i++) {
2576 ret = update_disk_super_on_device(info,
2577 argv[optind + i], (u64)i);
2579 error("update disk superblock failed devid %d: %d",
2581 close_ctree(info->chunk_root);
2586 close_ctree(info->chunk_root);
2588 /* fix metadata block to map correct chunk */
2589 ret = restore_metadump(source, out, 0, num_threads, 1,
2592 error("unable to fixup metadump: %d", ret);
2597 if (out == stdout) {
2601 if (ret && create) {
2604 unlink_ret = unlink(target);
2606 error("unlink output file %s failed: %m",
2611 btrfs_close_all_devices();