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"
39 #define HEADER_MAGIC 0xbd5c25e27295668bULL
40 #define MAX_PENDING_SIZE (256 * 1024)
41 #define BLOCK_SIZE 1024
42 #define BLOCK_MASK (BLOCK_SIZE - 1)
44 #define COMPRESS_NONE 0
45 #define COMPRESS_ZLIB 1
47 struct meta_cluster_item {
50 } __attribute__ ((__packed__));
52 struct meta_cluster_header {
57 } __attribute__ ((__packed__));
59 /* cluster header + index items + buffers */
61 struct meta_cluster_header header;
62 struct meta_cluster_item items[];
63 } __attribute__ ((__packed__));
65 #define ITEMS_PER_CLUSTER ((BLOCK_SIZE - sizeof(struct meta_cluster)) / \
66 sizeof(struct meta_cluster_item))
69 struct list_head list;
70 struct list_head ordered;
77 struct metadump_struct {
78 struct btrfs_root *root;
81 struct meta_cluster *cluster;
85 pthread_mutex_t mutex;
88 struct list_head list;
89 struct list_head ordered;
100 struct mdrestore_struct {
106 pthread_mutex_t mutex;
109 struct list_head list;
116 static void csum_block(u8 *buf, size_t len)
118 char result[BTRFS_CRC32_SIZE];
120 crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len - BTRFS_CSUM_SIZE);
121 btrfs_csum_final(crc, result);
122 memcpy(buf, result, BTRFS_CRC32_SIZE);
126 * zero inline extents and csum items
128 static void zero_items(u8 *dst, struct extent_buffer *src)
130 struct btrfs_file_extent_item *fi;
131 struct btrfs_item *item;
132 struct btrfs_key key;
133 u32 nritems = btrfs_header_nritems(src);
138 for (i = 0; i < nritems; i++) {
139 item = btrfs_item_nr(src, i);
140 btrfs_item_key_to_cpu(src, &key, i);
141 if (key.type == BTRFS_CSUM_ITEM_KEY) {
142 size = btrfs_item_size_nr(src, i);
143 memset(dst + btrfs_leaf_data(src) +
144 btrfs_item_offset_nr(src, i), 0, size);
147 if (key.type != BTRFS_EXTENT_DATA_KEY)
150 fi = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
151 extent_type = btrfs_file_extent_type(src, fi);
152 if (extent_type != BTRFS_FILE_EXTENT_INLINE)
155 ptr = btrfs_file_extent_inline_start(fi);
156 size = btrfs_file_extent_inline_item_len(src, item);
157 memset(dst + ptr, 0, size);
162 * copy buffer and zero useless data in the buffer
164 static void copy_buffer(u8 *dst, struct extent_buffer *src)
170 memcpy(dst, src->data, src->len);
171 if (src->start == BTRFS_SUPER_INFO_OFFSET)
174 level = btrfs_header_level(src);
175 nritems = btrfs_header_nritems(src);
178 size = sizeof(struct btrfs_header);
179 memset(dst + size, 0, src->len - size);
180 } else if (level == 0) {
181 size = btrfs_leaf_data(src) +
182 btrfs_item_offset_nr(src, nritems - 1) -
183 btrfs_item_nr_offset(nritems);
184 memset(dst + btrfs_item_nr_offset(nritems), 0, size);
185 zero_items(dst, src);
187 size = offsetof(struct btrfs_node, ptrs) +
188 sizeof(struct btrfs_key_ptr) * nritems;
189 memset(dst + size, 0, src->len - size);
191 csum_block(dst, src->len);
194 static void *dump_worker(void *data)
196 struct metadump_struct *md = (struct metadump_struct *)data;
197 struct async_work *async;
201 pthread_mutex_lock(&md->mutex);
202 while (list_empty(&md->list)) {
204 pthread_mutex_unlock(&md->mutex);
207 pthread_cond_wait(&md->cond, &md->mutex);
209 async = list_entry(md->list.next, struct async_work, list);
210 list_del_init(&async->list);
211 pthread_mutex_unlock(&md->mutex);
213 if (md->compress_level > 0) {
214 u8 *orig = async->buffer;
216 async->bufsize = compressBound(async->size);
217 async->buffer = malloc(async->bufsize);
219 ret = compress2(async->buffer,
220 (unsigned long *)&async->bufsize,
221 orig, async->size, md->compress_level);
227 pthread_mutex_lock(&md->mutex);
229 pthread_mutex_unlock(&md->mutex);
235 static void meta_cluster_init(struct metadump_struct *md, u64 start)
237 struct meta_cluster_header *header;
241 header = &md->cluster->header;
242 header->magic = cpu_to_le64(HEADER_MAGIC);
243 header->bytenr = cpu_to_le64(start);
244 header->nritems = cpu_to_le32(0);
245 header->compress = md->compress_level > 0 ?
246 COMPRESS_ZLIB : COMPRESS_NONE;
249 static int metadump_init(struct metadump_struct *md, struct btrfs_root *root,
250 FILE *out, int num_threads, int compress_level)
254 memset(md, 0, sizeof(*md));
255 pthread_cond_init(&md->cond, NULL);
256 pthread_mutex_init(&md->mutex, NULL);
257 INIT_LIST_HEAD(&md->list);
258 INIT_LIST_HEAD(&md->ordered);
261 md->pending_start = (u64)-1;
262 md->compress_level = compress_level;
263 md->cluster = calloc(1, BLOCK_SIZE);
267 meta_cluster_init(md, 0);
271 md->num_threads = num_threads;
272 md->threads = calloc(num_threads, sizeof(pthread_t));
275 for (i = 0; i < num_threads; i++) {
276 ret = pthread_create(md->threads + i, NULL, dump_worker, md);
283 static void metadump_destroy(struct metadump_struct *md)
286 pthread_mutex_lock(&md->mutex);
288 pthread_cond_broadcast(&md->cond);
289 pthread_mutex_unlock(&md->mutex);
291 for (i = 0; i < md->num_threads; i++)
292 pthread_join(md->threads[i], NULL);
294 pthread_cond_destroy(&md->cond);
295 pthread_mutex_destroy(&md->mutex);
300 static int write_zero(FILE *out, size_t size)
302 static char zero[BLOCK_SIZE];
303 return fwrite(zero, size, 1, out);
306 static int write_buffers(struct metadump_struct *md, u64 *next)
308 struct meta_cluster_header *header = &md->cluster->header;
309 struct meta_cluster_item *item;
310 struct async_work *async;
315 if (list_empty(&md->ordered))
318 /* wait until all buffers are compressed */
319 while (md->num_items > md->num_ready) {
320 struct timespec ts = {
324 pthread_mutex_unlock(&md->mutex);
325 nanosleep(&ts, NULL);
326 pthread_mutex_lock(&md->mutex);
329 /* setup and write index block */
330 list_for_each_entry(async, &md->ordered, ordered) {
331 item = md->cluster->items + nritems;
332 item->bytenr = cpu_to_le64(async->start);
333 item->size = cpu_to_le32(async->bufsize);
336 header->nritems = cpu_to_le32(nritems);
338 ret = fwrite(md->cluster, BLOCK_SIZE, 1, md->out);
342 bytenr += le64_to_cpu(header->bytenr) + BLOCK_SIZE;
343 while (!list_empty(&md->ordered)) {
344 async = list_entry(md->ordered.next, struct async_work,
346 list_del_init(&async->ordered);
348 bytenr += async->bufsize;
349 ret = fwrite(async->buffer, async->bufsize, 1, md->out);
356 /* zero unused space in the last block */
357 if (bytenr & BLOCK_MASK) {
358 size_t size = BLOCK_SIZE - (bytenr & BLOCK_MASK);
361 ret = write_zero(md->out, size);
369 static int flush_pending(struct metadump_struct *md, int done)
371 struct async_work *async = NULL;
372 struct extent_buffer *eb;
373 u64 blocksize = md->root->nodesize;
379 if (md->pending_size) {
380 async = calloc(1, sizeof(*async));
384 async->start = md->pending_start;
385 async->size = md->pending_size;
386 async->bufsize = async->size;
387 async->buffer = malloc(async->bufsize);
390 start = async->start;
393 eb = read_tree_block(md->root, start, blocksize, 0);
395 copy_buffer(async->buffer + offset, eb);
396 free_extent_buffer(eb);
402 md->pending_start = (u64)-1;
403 md->pending_size = 0;
408 pthread_mutex_lock(&md->mutex);
410 list_add_tail(&async->ordered, &md->ordered);
412 if (md->compress_level > 0) {
413 list_add_tail(&async->list, &md->list);
414 pthread_cond_signal(&md->cond);
419 if (md->num_items >= ITEMS_PER_CLUSTER || done) {
420 ret = write_buffers(md, &start);
422 meta_cluster_init(md, start);
424 pthread_mutex_unlock(&md->mutex);
428 static int add_metadata(u64 start, u64 size, struct metadump_struct *md)
431 if (md->pending_size + size > MAX_PENDING_SIZE ||
432 md->pending_start + md->pending_size != start) {
433 ret = flush_pending(md, 0);
436 md->pending_start = start;
438 readahead_tree_block(md->root, start, size, 0);
439 md->pending_size += size;
443 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
444 static int is_tree_block(struct btrfs_root *extent_root,
445 struct btrfs_path *path, u64 bytenr)
447 struct extent_buffer *leaf;
448 struct btrfs_key key;
452 leaf = path->nodes[0];
454 struct btrfs_extent_ref_v0 *ref_item;
456 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
457 ret = btrfs_next_leaf(extent_root, path);
461 leaf = path->nodes[0];
463 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
464 if (key.objectid != bytenr)
466 if (key.type != BTRFS_EXTENT_REF_V0_KEY)
468 ref_item = btrfs_item_ptr(leaf, path->slots[0],
469 struct btrfs_extent_ref_v0);
470 ref_objectid = btrfs_ref_objectid_v0(leaf, ref_item);
471 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID)
479 static int create_metadump(const char *input, FILE *out, int num_threads,
482 struct btrfs_root *root;
483 struct btrfs_root *extent_root;
484 struct btrfs_path *path;
485 struct extent_buffer *leaf;
486 struct btrfs_extent_item *ei;
487 struct btrfs_key key;
488 struct metadump_struct metadump;
493 root = open_ctree(input, 0, 0);
495 fprintf(stderr, "Open ctree failed\n");
499 BUG_ON(root->nodesize != root->leafsize);
501 ret = metadump_init(&metadump, root, out, num_threads,
505 ret = add_metadata(BTRFS_SUPER_INFO_OFFSET, 4096, &metadump);
508 extent_root = root->fs_info->extent_root;
509 path = btrfs_alloc_path();
511 bytenr = BTRFS_SUPER_INFO_OFFSET + 4096;
512 key.objectid = bytenr;
513 key.type = BTRFS_EXTENT_ITEM_KEY;
516 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
520 leaf = path->nodes[0];
521 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
522 ret = btrfs_next_leaf(extent_root, path);
526 leaf = path->nodes[0];
529 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
530 if (key.objectid < bytenr ||
531 key.type != BTRFS_EXTENT_ITEM_KEY) {
536 bytenr = key.objectid;
537 num_bytes = key.offset;
539 if (btrfs_item_size_nr(leaf, path->slots[0]) > sizeof(*ei)) {
540 ei = btrfs_item_ptr(leaf, path->slots[0],
541 struct btrfs_extent_item);
542 if (btrfs_extent_flags(leaf, ei) &
543 BTRFS_EXTENT_FLAG_TREE_BLOCK) {
544 ret = add_metadata(bytenr, num_bytes,
549 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
550 if (is_tree_block(extent_root, path, bytenr)) {
551 ret = add_metadata(bytenr, num_bytes,
562 ret = flush_pending(&metadump, 1);
565 metadump_destroy(&metadump);
567 btrfs_free_path(path);
568 ret = close_ctree(root);
572 static void update_super(u8 *buffer)
574 struct btrfs_super_block *super = (struct btrfs_super_block *)buffer;
575 struct btrfs_chunk *chunk;
576 struct btrfs_disk_key *key;
577 u32 sectorsize = btrfs_super_sectorsize(super);
578 u64 flags = btrfs_super_flags(super);
580 flags |= BTRFS_SUPER_FLAG_METADUMP;
581 btrfs_set_super_flags(super, flags);
583 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
584 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
585 sizeof(struct btrfs_disk_key));
587 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
588 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
589 btrfs_set_disk_key_offset(key, 0);
591 btrfs_set_stack_chunk_length(chunk, (u64)-1);
592 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
593 btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024);
594 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
595 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
596 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
597 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
598 btrfs_set_stack_chunk_num_stripes(chunk, 1);
599 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
600 chunk->stripe.devid = super->dev_item.devid;
601 chunk->stripe.offset = cpu_to_le64(0);
602 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
603 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
604 csum_block(buffer, 4096);
607 static void *restore_worker(void *data)
609 struct mdrestore_struct *mdres = (struct mdrestore_struct *)data;
610 struct async_work *async;
617 outfd = fileno(mdres->out);
618 buffer = malloc(MAX_PENDING_SIZE * 2);
622 pthread_mutex_lock(&mdres->mutex);
623 while (list_empty(&mdres->list)) {
625 pthread_mutex_unlock(&mdres->mutex);
628 pthread_cond_wait(&mdres->cond, &mdres->mutex);
630 async = list_entry(mdres->list.next, struct async_work, list);
631 list_del_init(&async->list);
632 pthread_mutex_unlock(&mdres->mutex);
634 if (mdres->compress_method == COMPRESS_ZLIB) {
635 size = MAX_PENDING_SIZE * 2;
636 ret = uncompress(buffer, (unsigned long *)&size,
637 async->buffer, async->bufsize);
641 outbuf = async->buffer;
642 size = async->bufsize;
645 if (async->start == BTRFS_SUPER_INFO_OFFSET)
646 update_super(outbuf);
648 ret = pwrite64(outfd, outbuf, size, async->start);
651 pthread_mutex_lock(&mdres->mutex);
653 pthread_mutex_unlock(&mdres->mutex);
663 static int mdrestore_init(struct mdrestore_struct *mdres,
664 FILE *in, FILE *out, int num_threads)
668 memset(mdres, 0, sizeof(*mdres));
669 pthread_cond_init(&mdres->cond, NULL);
670 pthread_mutex_init(&mdres->mutex, NULL);
671 INIT_LIST_HEAD(&mdres->list);
678 mdres->num_threads = num_threads;
679 mdres->threads = calloc(num_threads, sizeof(pthread_t));
682 for (i = 0; i < num_threads; i++) {
683 ret = pthread_create(mdres->threads + i, NULL, restore_worker,
691 static void mdrestore_destroy(struct mdrestore_struct *mdres)
694 pthread_mutex_lock(&mdres->mutex);
696 pthread_cond_broadcast(&mdres->cond);
697 pthread_mutex_unlock(&mdres->mutex);
699 for (i = 0; i < mdres->num_threads; i++)
700 pthread_join(mdres->threads[i], NULL);
702 pthread_cond_destroy(&mdres->cond);
703 pthread_mutex_destroy(&mdres->mutex);
704 free(mdres->threads);
707 static int add_cluster(struct meta_cluster *cluster,
708 struct mdrestore_struct *mdres, u64 *next)
710 struct meta_cluster_item *item;
711 struct meta_cluster_header *header = &cluster->header;
712 struct async_work *async;
717 BUG_ON(mdres->num_items);
718 mdres->compress_method = header->compress;
720 bytenr = le64_to_cpu(header->bytenr) + BLOCK_SIZE;
721 nritems = le32_to_cpu(header->nritems);
722 for (i = 0; i < nritems; i++) {
723 item = &cluster->items[i];
724 async = calloc(1, sizeof(*async));
725 async->start = le64_to_cpu(item->bytenr);
726 async->bufsize = le32_to_cpu(item->size);
727 async->buffer = malloc(async->bufsize);
728 ret = fread(async->buffer, async->bufsize, 1, mdres->in);
730 bytenr += async->bufsize;
732 pthread_mutex_lock(&mdres->mutex);
733 list_add_tail(&async->list, &mdres->list);
735 pthread_cond_signal(&mdres->cond);
736 pthread_mutex_unlock(&mdres->mutex);
738 if (bytenr & BLOCK_MASK) {
739 char buffer[BLOCK_MASK];
740 size_t size = BLOCK_SIZE - (bytenr & BLOCK_MASK);
743 ret = fread(buffer, size, 1, mdres->in);
750 static int wait_for_worker(struct mdrestore_struct *mdres)
752 pthread_mutex_lock(&mdres->mutex);
753 while (mdres->num_items > 0) {
754 struct timespec ts = {
758 pthread_mutex_unlock(&mdres->mutex);
759 nanosleep(&ts, NULL);
760 pthread_mutex_lock(&mdres->mutex);
762 pthread_mutex_unlock(&mdres->mutex);
766 static int restore_metadump(const char *input, FILE *out, int num_threads)
768 struct meta_cluster *cluster;
769 struct meta_cluster_header *header;
770 struct mdrestore_struct mdrestore;
775 if (!strcmp(input, "-")) {
778 in = fopen(input, "r");
780 perror("unable to open metadump image");
785 cluster = malloc(BLOCK_SIZE);
788 ret = mdrestore_init(&mdrestore, in, out, num_threads);
792 ret = fread(cluster, BLOCK_SIZE, 1, in);
796 header = &cluster->header;
797 if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
798 le64_to_cpu(header->bytenr) != bytenr) {
799 fprintf(stderr, "bad header in metadump image\n");
802 ret = add_cluster(cluster, &mdrestore, &bytenr);
805 wait_for_worker(&mdrestore);
808 mdrestore_destroy(&mdrestore);
815 static void print_usage(void)
817 fprintf(stderr, "usage: btrfs-image [options] source target\n");
818 fprintf(stderr, "\t-r \trestore metadump image\n");
819 fprintf(stderr, "\t-c value\tcompression level (0 ~ 9)\n");
820 fprintf(stderr, "\t-t value\tnumber of threads (1 ~ 32)\n");
824 int main(int argc, char *argv[])
829 int compress_level = 0;
835 int c = getopt(argc, argv, "rc:t:");
843 num_threads = atoi(optarg);
844 if (num_threads <= 0 || num_threads > 32)
848 compress_level = atoi(optarg);
849 if (compress_level < 0 || compress_level > 9)
857 argc = argc - optind;
860 source = argv[optind];
861 target = argv[optind + 1];
863 if (create && !strcmp(target, "-")) {
866 out = fopen(target, "w+");
868 perror("unable to create target file");
873 if (num_threads == 0 && compress_level > 0) {
874 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
875 if (num_threads <= 0)
880 ret = create_metadump(source, out, num_threads,
883 ret = restore_metadump(source, out, 1);