btrfs-progs: mkfs should first check all disks before writing to a disk
[platform/upstream/btrfs-progs.git] / btrfs-image.c
1 /*
2  * Copyright (C) 2008 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <dirent.h>
29 #include <zlib.h>
30 #include "kerncompat.h"
31 #include "crc32c.h"
32 #include "ctree.h"
33 #include "disk-io.h"
34 #include "transaction.h"
35 #include "utils.h"
36 #include "version.h"
37 #include "volumes.h"
38
39 #define HEADER_MAGIC            0xbd5c25e27295668bULL
40 #define MAX_PENDING_SIZE        (256 * 1024)
41 #define BLOCK_SIZE              1024
42 #define BLOCK_MASK              (BLOCK_SIZE - 1)
43
44 #define COMPRESS_NONE           0
45 #define COMPRESS_ZLIB           1
46
47 struct meta_cluster_item {
48         __le64 bytenr;
49         __le32 size;
50 } __attribute__ ((__packed__));
51
52 struct meta_cluster_header {
53         __le64 magic;
54         __le64 bytenr;
55         __le32 nritems;
56         u8 compress;
57 } __attribute__ ((__packed__));
58
59 /* cluster header + index items + buffers */
60 struct meta_cluster {
61         struct meta_cluster_header header;
62         struct meta_cluster_item items[];
63 } __attribute__ ((__packed__));
64
65 #define ITEMS_PER_CLUSTER ((BLOCK_SIZE - sizeof(struct meta_cluster)) / \
66                            sizeof(struct meta_cluster_item))
67
68 struct async_work {
69         struct list_head list;
70         struct list_head ordered;
71         u64 start;
72         u64 size;
73         u8 *buffer;
74         size_t bufsize;
75         int error;
76 };
77
78 struct metadump_struct {
79         struct btrfs_root *root;
80         FILE *out;
81
82         struct meta_cluster *cluster;
83
84         pthread_t *threads;
85         size_t num_threads;
86         pthread_mutex_t mutex;
87         pthread_cond_t cond;
88
89         struct list_head list;
90         struct list_head ordered;
91         size_t num_items;
92         size_t num_ready;
93
94         u64 pending_start;
95         u64 pending_size;
96
97         int compress_level;
98         int done;
99         int data;
100 };
101
102 struct mdrestore_struct {
103         FILE *in;
104         FILE *out;
105
106         pthread_t *threads;
107         size_t num_threads;
108         pthread_mutex_t mutex;
109         pthread_cond_t cond;
110
111         struct list_head list;
112         size_t num_items;
113         u64 leafsize;
114         u64 devid;
115         u8 uuid[BTRFS_UUID_SIZE];
116         u8 fsid[BTRFS_FSID_SIZE];
117
118         int compress_method;
119         int done;
120         int error;
121         int old_restore;
122 };
123
124 static void csum_block(u8 *buf, size_t len)
125 {
126         char result[BTRFS_CRC32_SIZE];
127         u32 crc = ~(u32)0;
128         crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len - BTRFS_CSUM_SIZE);
129         btrfs_csum_final(crc, result);
130         memcpy(buf, result, BTRFS_CRC32_SIZE);
131 }
132
133 /*
134  * zero inline extents and csum items
135  */
136 static void zero_items(u8 *dst, struct extent_buffer *src)
137 {
138         struct btrfs_file_extent_item *fi;
139         struct btrfs_item *item;
140         struct btrfs_key key;
141         u32 nritems = btrfs_header_nritems(src);
142         size_t size;
143         unsigned long ptr;
144         int i, extent_type;
145
146         for (i = 0; i < nritems; i++) {
147                 item = btrfs_item_nr(src, i);
148                 btrfs_item_key_to_cpu(src, &key, i);
149                 if (key.type == BTRFS_CSUM_ITEM_KEY) {
150                         size = btrfs_item_size_nr(src, i);
151                         memset(dst + btrfs_leaf_data(src) +
152                                btrfs_item_offset_nr(src, i), 0, size);
153                         continue;
154                 }
155                 if (key.type != BTRFS_EXTENT_DATA_KEY)
156                         continue;
157
158                 fi = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
159                 extent_type = btrfs_file_extent_type(src, fi);
160                 if (extent_type != BTRFS_FILE_EXTENT_INLINE)
161                         continue;
162
163                 ptr = btrfs_file_extent_inline_start(fi);
164                 size = btrfs_file_extent_inline_item_len(src, item);
165                 memset(dst + ptr, 0, size);
166         }
167 }
168
169 /*
170  * copy buffer and zero useless data in the buffer
171  */
172 static void copy_buffer(u8 *dst, struct extent_buffer *src)
173 {
174         int level;
175         size_t size;
176         u32 nritems;
177
178         memcpy(dst, src->data, src->len);
179         if (src->start == BTRFS_SUPER_INFO_OFFSET)
180                 return;
181
182         level = btrfs_header_level(src);
183         nritems = btrfs_header_nritems(src);
184
185         if (nritems == 0) {
186                 size = sizeof(struct btrfs_header);
187                 memset(dst + size, 0, src->len - size);
188         } else if (level == 0) {
189                 size = btrfs_leaf_data(src) +
190                         btrfs_item_offset_nr(src, nritems - 1) -
191                         btrfs_item_nr_offset(nritems);
192                 memset(dst + btrfs_item_nr_offset(nritems), 0, size);
193                 zero_items(dst, src);
194         } else {
195                 size = offsetof(struct btrfs_node, ptrs) +
196                         sizeof(struct btrfs_key_ptr) * nritems;
197                 memset(dst + size, 0, src->len - size);
198         }
199         csum_block(dst, src->len);
200 }
201
202 static void *dump_worker(void *data)
203 {
204         struct metadump_struct *md = (struct metadump_struct *)data;
205         struct async_work *async;
206         int ret;
207
208         while (1) {
209                 pthread_mutex_lock(&md->mutex);
210                 while (list_empty(&md->list)) {
211                         if (md->done) {
212                                 pthread_mutex_unlock(&md->mutex);
213                                 goto out;
214                         }
215                         pthread_cond_wait(&md->cond, &md->mutex);
216                 }
217                 async = list_entry(md->list.next, struct async_work, list);
218                 list_del_init(&async->list);
219                 pthread_mutex_unlock(&md->mutex);
220
221                 if (md->compress_level > 0) {
222                         u8 *orig = async->buffer;
223
224                         async->bufsize = compressBound(async->size);
225                         async->buffer = malloc(async->bufsize);
226
227                         ret = compress2(async->buffer,
228                                          (unsigned long *)&async->bufsize,
229                                          orig, async->size, md->compress_level);
230
231                         if (ret != Z_OK)
232                                 async->error = 1;
233
234                         free(orig);
235                 }
236
237                 pthread_mutex_lock(&md->mutex);
238                 md->num_ready++;
239                 pthread_mutex_unlock(&md->mutex);
240         }
241 out:
242         pthread_exit(NULL);
243 }
244
245 static void meta_cluster_init(struct metadump_struct *md, u64 start)
246 {
247         struct meta_cluster_header *header;
248
249         md->num_items = 0;
250         md->num_ready = 0;
251         header = &md->cluster->header;
252         header->magic = cpu_to_le64(HEADER_MAGIC);
253         header->bytenr = cpu_to_le64(start);
254         header->nritems = cpu_to_le32(0);
255         header->compress = md->compress_level > 0 ?
256                            COMPRESS_ZLIB : COMPRESS_NONE;
257 }
258
259 static int metadump_init(struct metadump_struct *md, struct btrfs_root *root,
260                          FILE *out, int num_threads, int compress_level)
261 {
262         int i, ret = 0;
263
264         memset(md, 0, sizeof(*md));
265         pthread_cond_init(&md->cond, NULL);
266         pthread_mutex_init(&md->mutex, NULL);
267         INIT_LIST_HEAD(&md->list);
268         INIT_LIST_HEAD(&md->ordered);
269         md->root = root;
270         md->out = out;
271         md->pending_start = (u64)-1;
272         md->compress_level = compress_level;
273         md->cluster = calloc(1, BLOCK_SIZE);
274         if (!md->cluster) {
275                 pthread_cond_destroy(&md->cond);
276                 pthread_mutex_destroy(&md->mutex);
277                 return -ENOMEM;
278         }
279
280         meta_cluster_init(md, 0);
281         if (!num_threads)
282                 return 0;
283
284         md->num_threads = num_threads;
285         md->threads = calloc(num_threads, sizeof(pthread_t));
286         if (!md->threads) {
287                 free(md->cluster);
288                 pthread_cond_destroy(&md->cond);
289                 pthread_mutex_destroy(&md->mutex);
290                 return -ENOMEM;
291         }
292
293         for (i = 0; i < num_threads; i++) {
294                 ret = pthread_create(md->threads + i, NULL, dump_worker, md);
295                 if (ret)
296                         break;
297         }
298
299         if (ret) {
300                 pthread_mutex_lock(&md->mutex);
301                 md->done = 1;
302                 pthread_cond_broadcast(&md->cond);
303                 pthread_mutex_unlock(&md->mutex);
304
305                 for (i--; i >= 0; i--)
306                         pthread_join(md->threads[i], NULL);
307
308                 pthread_cond_destroy(&md->cond);
309                 pthread_mutex_destroy(&md->mutex);
310                 free(md->cluster);
311                 free(md->threads);
312         }
313
314         return ret;
315 }
316
317 static void metadump_destroy(struct metadump_struct *md)
318 {
319         int i;
320         pthread_mutex_lock(&md->mutex);
321         md->done = 1;
322         pthread_cond_broadcast(&md->cond);
323         pthread_mutex_unlock(&md->mutex);
324
325         for (i = 0; i < md->num_threads; i++)
326                 pthread_join(md->threads[i], NULL);
327
328         pthread_cond_destroy(&md->cond);
329         pthread_mutex_destroy(&md->mutex);
330         free(md->threads);
331         free(md->cluster);
332 }
333
334 static int write_zero(FILE *out, size_t size)
335 {
336         static char zero[BLOCK_SIZE];
337         return fwrite(zero, size, 1, out);
338 }
339
340 static int write_buffers(struct metadump_struct *md, u64 *next)
341 {
342         struct meta_cluster_header *header = &md->cluster->header;
343         struct meta_cluster_item *item;
344         struct async_work *async;
345         u64 bytenr = 0;
346         u32 nritems = 0;
347         int ret;
348         int err = 0;
349
350         if (list_empty(&md->ordered))
351                 goto out;
352
353         /* wait until all buffers are compressed */
354         while (md->num_items > md->num_ready) {
355                 struct timespec ts = {
356                         .tv_sec = 0,
357                         .tv_nsec = 10000000,
358                 };
359                 pthread_mutex_unlock(&md->mutex);
360                 nanosleep(&ts, NULL);
361                 pthread_mutex_lock(&md->mutex);
362         }
363
364         /* setup and write index block */
365         list_for_each_entry(async, &md->ordered, ordered) {
366                 item = md->cluster->items + nritems;
367                 item->bytenr = cpu_to_le64(async->start);
368                 item->size = cpu_to_le32(async->bufsize);
369                 nritems++;
370         }
371         header->nritems = cpu_to_le32(nritems);
372
373         ret = fwrite(md->cluster, BLOCK_SIZE, 1, md->out);
374         if (ret != 1) {
375                 fprintf(stderr, "Error writing out cluster: %d\n", errno);
376                 return -EIO;
377         }
378
379         /* write buffers */
380         bytenr += le64_to_cpu(header->bytenr) + BLOCK_SIZE;
381         while (!list_empty(&md->ordered)) {
382                 async = list_entry(md->ordered.next, struct async_work,
383                                    ordered);
384                 list_del_init(&async->ordered);
385
386                 bytenr += async->bufsize;
387                 if (!err)
388                         ret = fwrite(async->buffer, async->bufsize, 1,
389                                      md->out);
390                 if (ret != 1) {
391                         err = -EIO;
392                         ret = 0;
393                         fprintf(stderr, "Error writing out cluster: %d\n",
394                                 errno);
395                 }
396
397                 free(async->buffer);
398                 free(async);
399         }
400
401         /* zero unused space in the last block */
402         if (!err && bytenr & BLOCK_MASK) {
403                 size_t size = BLOCK_SIZE - (bytenr & BLOCK_MASK);
404
405                 bytenr += size;
406                 ret = write_zero(md->out, size);
407                 if (ret != 1) {
408                         fprintf(stderr, "Error zeroing out buffer: %d\n",
409                                 errno);
410                         err = -EIO;
411                 }
412         }
413 out:
414         *next = bytenr;
415         return err;
416 }
417
418 static int read_data_extent(struct metadump_struct *md,
419                             struct async_work *async)
420 {
421         struct btrfs_multi_bio *multi = NULL;
422         struct btrfs_device *device;
423         u64 bytes_left = async->size;
424         u64 logical = async->start;
425         u64 offset = 0;
426         u64 bytenr;
427         u64 read_len;
428         ssize_t done;
429         int fd;
430         int ret;
431
432         while (bytes_left) {
433                 read_len = bytes_left;
434                 ret = btrfs_map_block(&md->root->fs_info->mapping_tree, READ,
435                                       logical, &read_len, &multi, 0, NULL);
436                 if (ret) {
437                         fprintf(stderr, "Couldn't map data block %d\n", ret);
438                         return ret;
439                 }
440
441                 device = multi->stripes[0].dev;
442
443                 if (device->fd == 0) {
444                         fprintf(stderr,
445                                 "Device we need to read from is not open\n");
446                         free(multi);
447                         return -EIO;
448                 }
449                 fd = device->fd;
450                 bytenr = multi->stripes[0].physical;
451                 free(multi);
452
453                 read_len = min(read_len, bytes_left);
454                 done = pread64(fd, async->buffer+offset, read_len, bytenr);
455                 if (done < read_len) {
456                         if (done < 0)
457                                 fprintf(stderr, "Error reading extent %d\n",
458                                         errno);
459                         else
460                                 fprintf(stderr, "Short read\n");
461                         return -EIO;
462                 }
463
464                 bytes_left -= done;
465                 offset += done;
466                 logical += done;
467         }
468
469         return 0;
470 }
471
472 static int flush_pending(struct metadump_struct *md, int done)
473 {
474         struct async_work *async = NULL;
475         struct extent_buffer *eb;
476         u64 blocksize = md->root->nodesize;
477         u64 start;
478         u64 size;
479         size_t offset;
480         int ret = 0;
481
482         if (md->pending_size) {
483                 async = calloc(1, sizeof(*async));
484                 if (!async)
485                         return -ENOMEM;
486
487                 async->start = md->pending_start;
488                 async->size = md->pending_size;
489                 async->bufsize = async->size;
490                 async->buffer = malloc(async->bufsize);
491                 if (!async->buffer) {
492                         free(async);
493                         return -ENOMEM;
494                 }
495                 offset = 0;
496                 start = async->start;
497                 size = async->size;
498
499                 if (md->data) {
500                         ret = read_data_extent(md, async);
501                         if (ret) {
502                                 free(async->buffer);
503                                 free(async);
504                                 return ret;
505                         }
506                 }
507
508                 while (!md->data && size > 0) {
509                         eb = read_tree_block(md->root, start, blocksize, 0);
510                         if (!eb) {
511                                 free(async->buffer);
512                                 free(async);
513                                 fprintf(stderr,
514                                         "Error reading metadata block\n");
515                                 return -EIO;
516                         }
517                         copy_buffer(async->buffer + offset, eb);
518                         free_extent_buffer(eb);
519                         start += blocksize;
520                         offset += blocksize;
521                         size -= blocksize;
522                 }
523
524                 md->pending_start = (u64)-1;
525                 md->pending_size = 0;
526         } else if (!done) {
527                 return 0;
528         }
529
530         pthread_mutex_lock(&md->mutex);
531         if (async) {
532                 list_add_tail(&async->ordered, &md->ordered);
533                 md->num_items++;
534                 if (md->compress_level > 0) {
535                         list_add_tail(&async->list, &md->list);
536                         pthread_cond_signal(&md->cond);
537                 } else {
538                         md->num_ready++;
539                 }
540         }
541         if (md->num_items >= ITEMS_PER_CLUSTER || done) {
542                 ret = write_buffers(md, &start);
543                 if (ret)
544                         fprintf(stderr, "Error writing buffers %d\n",
545                                 errno);
546                 else
547                         meta_cluster_init(md, start);
548         }
549         pthread_mutex_unlock(&md->mutex);
550         return ret;
551 }
552
553 static int add_extent(u64 start, u64 size, struct metadump_struct *md,
554                       int data)
555 {
556         int ret;
557         if (md->data != data ||
558             md->pending_size + size > MAX_PENDING_SIZE ||
559             md->pending_start + md->pending_size != start) {
560                 ret = flush_pending(md, 0);
561                 if (ret)
562                         return ret;
563                 md->pending_start = start;
564         }
565         readahead_tree_block(md->root, start, size, 0);
566         md->pending_size += size;
567         md->data = data;
568         return 0;
569 }
570
571 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
572 static int is_tree_block(struct btrfs_root *extent_root,
573                          struct btrfs_path *path, u64 bytenr)
574 {
575         struct extent_buffer *leaf;
576         struct btrfs_key key;
577         u64 ref_objectid;
578         int ret;
579
580         leaf = path->nodes[0];
581         while (1) {
582                 struct btrfs_extent_ref_v0 *ref_item;
583                 path->slots[0]++;
584                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
585                         ret = btrfs_next_leaf(extent_root, path);
586                         if (ret < 0)
587                                 return ret;
588                         if (ret > 0)
589                                 break;
590                         leaf = path->nodes[0];
591                 }
592                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
593                 if (key.objectid != bytenr)
594                         break;
595                 if (key.type != BTRFS_EXTENT_REF_V0_KEY)
596                         continue;
597                 ref_item = btrfs_item_ptr(leaf, path->slots[0],
598                                           struct btrfs_extent_ref_v0);
599                 ref_objectid = btrfs_ref_objectid_v0(leaf, ref_item);
600                 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID)
601                         return 1;
602                 break;
603         }
604         return 0;
605 }
606 #endif
607
608 static int copy_log_blocks(struct btrfs_root *root, struct extent_buffer *eb,
609                            struct metadump_struct *metadump,
610                            int log_root_tree)
611 {
612         struct extent_buffer *tmp;
613         struct btrfs_root_item *ri;
614         struct btrfs_key key;
615         u64 bytenr;
616         int level;
617         int nritems = 0;
618         int i = 0;
619         int ret;
620
621         ret = add_extent(btrfs_header_bytenr(eb), root->leafsize, metadump, 0);
622         if (ret) {
623                 fprintf(stderr, "Error adding metadata block\n");
624                 return ret;
625         }
626
627         if (btrfs_header_level(eb) == 0 && !log_root_tree)
628                 return 0;
629
630         level = btrfs_header_level(eb);
631         nritems = btrfs_header_nritems(eb);
632         for (i = 0; i < nritems; i++) {
633                 if (level == 0) {
634                         btrfs_item_key_to_cpu(eb, &key, i);
635                         if (key.type != BTRFS_ROOT_ITEM_KEY)
636                                 continue;
637                         ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
638                         bytenr = btrfs_disk_root_bytenr(eb, ri);
639                         tmp = read_tree_block(root, bytenr, root->leafsize, 0);
640                         if (!tmp) {
641                                 fprintf(stderr,
642                                         "Error reading log root block\n");
643                                 return -EIO;
644                         }
645                         ret = copy_log_blocks(root, tmp, metadump, 0);
646                         free_extent_buffer(tmp);
647                         if (ret)
648                                 return ret;
649                 } else {
650                         bytenr = btrfs_node_blockptr(eb, i);
651                         tmp = read_tree_block(root, bytenr, root->leafsize, 0);
652                         if (!tmp) {
653                                 fprintf(stderr, "Error reading log block\n");
654                                 return -EIO;
655                         }
656                         ret = copy_log_blocks(root, tmp, metadump,
657                                               log_root_tree);
658                         free_extent_buffer(tmp);
659                         if (ret)
660                                 return ret;
661                 }
662         }
663
664         return 0;
665 }
666
667 static int copy_log_trees(struct btrfs_root *root,
668                           struct metadump_struct *metadump,
669                           struct btrfs_path *path)
670 {
671         u64 blocknr = btrfs_super_log_root(root->fs_info->super_copy);
672
673         if (blocknr == 0)
674                 return 0;
675
676         if (!root->fs_info->log_root_tree ||
677             !root->fs_info->log_root_tree->node) {
678                 fprintf(stderr, "Error copying tree log, it wasn't setup\n");
679                 return -EIO;
680         }
681
682         return copy_log_blocks(root, root->fs_info->log_root_tree->node,
683                                metadump, 1);
684 }
685
686 static int copy_space_cache(struct btrfs_root *root,
687                             struct metadump_struct *metadump,
688                             struct btrfs_path *path)
689 {
690         struct extent_buffer *leaf;
691         struct btrfs_file_extent_item *fi;
692         struct btrfs_key key;
693         u64 bytenr, num_bytes;
694         int ret;
695
696         root = root->fs_info->tree_root;
697
698         key.objectid = 0;
699         key.type = BTRFS_EXTENT_DATA_KEY;
700         key.offset = 0;
701
702         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
703         if (ret < 0) {
704                 fprintf(stderr, "Error searching for free space inode %d\n",
705                         ret);
706                 return ret;
707         }
708
709         while (1) {
710                 leaf = path->nodes[0];
711                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
712                         ret = btrfs_next_leaf(root, path);
713                         if (ret < 0) {
714                                 fprintf(stderr, "Error going to next leaf "
715                                         "%d\n", ret);
716                                 return ret;
717                         }
718                         if (ret > 0)
719                                 break;
720                         leaf = path->nodes[0];
721                 }
722
723                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
724                 if (key.type != BTRFS_EXTENT_DATA_KEY) {
725                         path->slots[0]++;
726                         continue;
727                 }
728
729                 fi = btrfs_item_ptr(leaf, path->slots[0],
730                                     struct btrfs_file_extent_item);
731                 if (btrfs_file_extent_type(leaf, fi) !=
732                     BTRFS_FILE_EXTENT_REG) {
733                         path->slots[0]++;
734                         continue;
735                 }
736
737                 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
738                 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
739                 ret = add_extent(bytenr, num_bytes, metadump, 1);
740                 if (ret) {
741                         fprintf(stderr, "Error adding space cache blocks %d\n",
742                                 ret);
743                         btrfs_release_path(root, path);
744                         return ret;
745                 }
746                 path->slots[0]++;
747         }
748
749         return 0;
750 }
751
752 static int create_metadump(const char *input, FILE *out, int num_threads,
753                            int compress_level)
754 {
755         struct btrfs_root *root;
756         struct btrfs_root *extent_root;
757         struct btrfs_path *path = NULL;
758         struct extent_buffer *leaf;
759         struct btrfs_extent_item *ei;
760         struct btrfs_key key;
761         struct metadump_struct metadump;
762         u64 bytenr;
763         u64 num_bytes;
764         int ret;
765         int err = 0;
766
767         root = open_ctree(input, 0, 0);
768         if (!root) {
769                 fprintf(stderr, "Open ctree failed\n");
770                 return -EIO;
771         }
772
773         BUG_ON(root->nodesize != root->leafsize);
774
775         ret = metadump_init(&metadump, root, out, num_threads,
776                             compress_level);
777         if (ret) {
778                 fprintf(stderr, "Error initing metadump %d\n", ret);
779                 close_ctree(root);
780                 return ret;
781         }
782
783         ret = add_extent(BTRFS_SUPER_INFO_OFFSET, 4096, &metadump, 0);
784         if (ret) {
785                 fprintf(stderr, "Error adding metadata %d\n", ret);
786                 err = ret;
787                 goto out;
788         }
789
790         extent_root = root->fs_info->extent_root;
791         path = btrfs_alloc_path();
792         if (!path) {
793                 fprintf(stderr, "Out of memory allocing path\n");
794                 err = -ENOMEM;
795                 goto out;
796         }
797         bytenr = BTRFS_SUPER_INFO_OFFSET + 4096;
798         key.objectid = bytenr;
799         key.type = BTRFS_EXTENT_ITEM_KEY;
800         key.offset = 0;
801
802         ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
803         if (ret < 0) {
804                 fprintf(stderr, "Error searching extent root %d\n", ret);
805                 err = ret;
806                 goto out;
807         }
808
809         while (1) {
810                 leaf = path->nodes[0];
811                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
812                         ret = btrfs_next_leaf(extent_root, path);
813                         if (ret < 0) {
814                                 fprintf(stderr, "Error going to next leaf %d"
815                                         "\n", ret);
816                                 err = ret;
817                                 goto out;
818                         }
819                         if (ret > 0)
820                                 break;
821                         leaf = path->nodes[0];
822                 }
823
824                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
825                 if (key.objectid < bytenr ||
826                     key.type != BTRFS_EXTENT_ITEM_KEY) {
827                         path->slots[0]++;
828                         continue;
829                 }
830
831                 bytenr = key.objectid;
832                 num_bytes = key.offset;
833
834                 if (btrfs_item_size_nr(leaf, path->slots[0]) > sizeof(*ei)) {
835                         ei = btrfs_item_ptr(leaf, path->slots[0],
836                                             struct btrfs_extent_item);
837                         if (btrfs_extent_flags(leaf, ei) &
838                             BTRFS_EXTENT_FLAG_TREE_BLOCK) {
839                                 ret = add_extent(bytenr, num_bytes, &metadump,
840                                                  0);
841                                 if (ret) {
842                                         fprintf(stderr, "Error adding block "
843                                                 "%d\n", ret);
844                                         err = ret;
845                                         goto out;
846                                 }
847                         }
848                 } else {
849 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
850                         ret = is_tree_block(extent_root, path, bytenr);
851                         if (ret < 0) {
852                                 fprintf(stderr, "Error checking tree block "
853                                         "%d\n", ret);
854                                 err = ret;
855                                 goto out;
856                         }
857
858                         if (ret) {
859                                 ret = add_extent(bytenr, num_bytes, &metadump,
860                                                  0);
861                                 if (ret) {
862                                         fprintf(stderr, "Error adding block "
863                                                 "%d\n", ret);
864                                         err = ret;
865                                         goto out;
866                                 }
867                         }
868 #else
869                         fprintf(stderr, "Either extent tree corruption or "
870                                 "you haven't built with V0 support\n");
871                         err = -EIO;
872                         goto out;
873 #endif
874                 }
875                 bytenr += num_bytes;
876         }
877
878         btrfs_release_path(root, path);
879
880         ret = copy_log_trees(root, &metadump, path);
881         if (ret) {
882                 err = ret;
883                 goto out;
884         }
885
886         ret = copy_space_cache(root, &metadump, path);
887 out:
888         ret = flush_pending(&metadump, 1);
889         if (ret) {
890                 if (!err)
891                         ret = err;
892                 fprintf(stderr, "Error flushing pending %d\n", ret);
893         }
894
895         metadump_destroy(&metadump);
896
897         btrfs_free_path(path);
898         ret = close_ctree(root);
899         return err ? err : ret;
900 }
901
902 static void update_super_old(u8 *buffer)
903 {
904         struct btrfs_super_block *super = (struct btrfs_super_block *)buffer;
905         struct btrfs_chunk *chunk;
906         struct btrfs_disk_key *key;
907         u32 sectorsize = btrfs_super_sectorsize(super);
908         u64 flags = btrfs_super_flags(super);
909
910         flags |= BTRFS_SUPER_FLAG_METADUMP;
911         btrfs_set_super_flags(super, flags);
912
913         key = (struct btrfs_disk_key *)(super->sys_chunk_array);
914         chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
915                                        sizeof(struct btrfs_disk_key));
916
917         btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
918         btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
919         btrfs_set_disk_key_offset(key, 0);
920
921         btrfs_set_stack_chunk_length(chunk, (u64)-1);
922         btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
923         btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024);
924         btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
925         btrfs_set_stack_chunk_io_align(chunk, sectorsize);
926         btrfs_set_stack_chunk_io_width(chunk, sectorsize);
927         btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
928         btrfs_set_stack_chunk_num_stripes(chunk, 1);
929         btrfs_set_stack_chunk_sub_stripes(chunk, 0);
930         chunk->stripe.devid = super->dev_item.devid;
931         chunk->stripe.offset = cpu_to_le64(0);
932         memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
933         btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
934         csum_block(buffer, 4096);
935 }
936
937 static int update_super(u8 *buffer)
938 {
939         struct btrfs_super_block *super = (struct btrfs_super_block *)buffer;
940         struct btrfs_chunk *chunk;
941         struct btrfs_disk_key *disk_key;
942         struct btrfs_key key;
943         u32 new_array_size = 0;
944         u32 array_size;
945         u32 cur = 0;
946         u32 new_cur = 0;
947         u8 *ptr, *write_ptr;
948         int old_num_stripes;
949
950         write_ptr = ptr = super->sys_chunk_array;
951         array_size = btrfs_super_sys_array_size(super);
952
953         while (cur < array_size) {
954                 disk_key = (struct btrfs_disk_key *)ptr;
955                 btrfs_disk_key_to_cpu(&key, disk_key);
956
957                 new_array_size += sizeof(*disk_key);
958                 memmove(write_ptr, ptr, sizeof(*disk_key));
959
960                 write_ptr += sizeof(*disk_key);
961                 ptr += sizeof(*disk_key);
962                 cur += sizeof(*disk_key);
963                 new_cur += sizeof(*disk_key);
964
965                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
966                         chunk = (struct btrfs_chunk *)ptr;
967                         old_num_stripes = btrfs_stack_chunk_num_stripes(chunk);
968                         chunk = (struct btrfs_chunk *)write_ptr;
969
970                         memmove(write_ptr, ptr, sizeof(*chunk));
971                         btrfs_set_stack_chunk_num_stripes(chunk, 1);
972                         btrfs_set_stack_chunk_sub_stripes(chunk, 0);
973                         btrfs_set_stack_chunk_type(chunk,
974                                                    BTRFS_BLOCK_GROUP_SYSTEM);
975                         chunk->stripe.devid = super->dev_item.devid;
976                         chunk->stripe.offset = cpu_to_le64(key.offset);
977                         memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid,
978                                BTRFS_UUID_SIZE);
979                         new_array_size += sizeof(*chunk);
980                         new_cur += sizeof(*chunk);
981                 } else {
982                         fprintf(stderr, "Bogus key in the sys chunk array "
983                                 "%d\n", key.type);
984                         return -EIO;
985                 }
986                 write_ptr += sizeof(*chunk);
987                 ptr += btrfs_chunk_item_size(old_num_stripes);
988                 cur += btrfs_chunk_item_size(old_num_stripes);
989         }
990
991         btrfs_set_super_sys_array_size(super, new_array_size);
992         csum_block(buffer, 4096);
993
994         return 0;
995 }
996
997 static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size)
998 {
999         struct extent_buffer *eb;
1000
1001         eb = malloc(sizeof(struct extent_buffer) + size);
1002         if (!eb)
1003                 return NULL;
1004         memset(eb, 0, sizeof(struct extent_buffer) + size);
1005
1006         eb->start = bytenr;
1007         eb->len = size;
1008         return eb;
1009 }
1010
1011 static void truncate_item(struct extent_buffer *eb, int slot, u32 new_size)
1012 {
1013         struct btrfs_item *item;
1014         u32 nritems;
1015         u32 old_size;
1016         u32 old_data_start;
1017         u32 size_diff;
1018         u32 data_end;
1019         int i;
1020
1021         old_size = btrfs_item_size_nr(eb, slot);
1022         if (old_size == new_size)
1023                 return;
1024
1025         nritems = btrfs_header_nritems(eb);
1026         data_end = btrfs_item_offset_nr(eb, nritems - 1);
1027
1028         old_data_start = btrfs_item_offset_nr(eb, slot);
1029         size_diff = old_size - new_size;
1030
1031         for (i = slot; i < nritems; i++) {
1032                 u32 ioff;
1033                 item = btrfs_item_nr(eb, i);
1034                 ioff = btrfs_item_offset(eb, item);
1035                 btrfs_set_item_offset(eb, item, ioff + size_diff);
1036         }
1037
1038         memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end + size_diff,
1039                               btrfs_leaf_data(eb) + data_end,
1040                               old_data_start + new_size - data_end);
1041         item = btrfs_item_nr(eb, slot);
1042         btrfs_set_item_size(eb, item, new_size);
1043 }
1044
1045 static int fixup_chunk_tree_block(struct mdrestore_struct *mdres,
1046                                   struct async_work *async, u8 *buffer,
1047                                   size_t size)
1048 {
1049         struct extent_buffer *eb;
1050         size_t size_left = size;
1051         u64 bytenr = async->start;
1052         int i;
1053
1054         if (size_left % mdres->leafsize)
1055                 return 0;
1056
1057         eb = alloc_dummy_eb(bytenr, mdres->leafsize);
1058         if (!eb)
1059                 return -ENOMEM;
1060
1061         while (size_left) {
1062                 eb->start = bytenr;
1063                 memcpy(eb->data, buffer, mdres->leafsize);
1064
1065                 if (btrfs_header_bytenr(eb) != bytenr)
1066                         break;
1067                 if (memcmp(mdres->fsid,
1068                            eb->data + offsetof(struct btrfs_header, fsid),
1069                            BTRFS_FSID_SIZE))
1070                         break;
1071
1072                 if (btrfs_header_owner(eb) != BTRFS_CHUNK_TREE_OBJECTID)
1073                         goto next;
1074
1075                 if (btrfs_header_level(eb) != 0)
1076                         goto next;
1077
1078                 for (i = 0; i < btrfs_header_nritems(eb); i++) {
1079                         struct btrfs_chunk chunk;
1080                         struct btrfs_key key;
1081                         u64 type;
1082
1083                         btrfs_item_key_to_cpu(eb, &key, i);
1084                         if (key.type != BTRFS_CHUNK_ITEM_KEY)
1085                                 continue;
1086                         truncate_item(eb, i, sizeof(chunk));
1087                         read_extent_buffer(eb, &chunk,
1088                                            btrfs_item_ptr_offset(eb, i),
1089                                            sizeof(chunk));
1090
1091                         /* Zero out the RAID profile */
1092                         type = btrfs_stack_chunk_type(&chunk);
1093                         type &= (BTRFS_BLOCK_GROUP_DATA |
1094                                  BTRFS_BLOCK_GROUP_SYSTEM |
1095                                  BTRFS_BLOCK_GROUP_METADATA);
1096                         btrfs_set_stack_chunk_type(&chunk, type);
1097
1098                         btrfs_set_stack_chunk_num_stripes(&chunk, 1);
1099                         btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
1100                         btrfs_set_stack_stripe_devid(&chunk.stripe, mdres->devid);
1101                         btrfs_set_stack_stripe_offset(&chunk.stripe, key.offset);
1102                         memcpy(chunk.stripe.dev_uuid, mdres->uuid,
1103                                BTRFS_UUID_SIZE);
1104                         write_extent_buffer(eb, &chunk,
1105                                             btrfs_item_ptr_offset(eb, i),
1106                                             sizeof(chunk));
1107                 }
1108                 memcpy(buffer, eb->data, eb->len);
1109                 csum_block(buffer, eb->len);
1110 next:
1111                 size_left -= mdres->leafsize;
1112                 buffer += mdres->leafsize;
1113                 bytenr += mdres->leafsize;
1114         }
1115
1116         return 0;
1117 }
1118
1119 static void write_backup_supers(int fd, u8 *buf)
1120 {
1121         struct stat st;
1122         u64 size;
1123         u64 bytenr;
1124         int i;
1125         int ret;
1126
1127         if (fstat(fd, &st)) {
1128                 fprintf(stderr, "Couldn't stat restore point, won't be able "
1129                         "to write backup supers: %d\n", errno);
1130                 return;
1131         }
1132
1133         size = btrfs_device_size(fd, &st);
1134
1135         for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1136                 bytenr = btrfs_sb_offset(i);
1137                 if (bytenr + 4096 > size)
1138                         break;
1139                 ret = pwrite64(fd, buf, 4096, bytenr);
1140                 if (ret < 4096) {
1141                         if (ret < 0)
1142                                 fprintf(stderr, "Problem writing out backup "
1143                                         "super block %d, err %d\n", i, errno);
1144                         else
1145                                 fprintf(stderr, "Short write writing out "
1146                                         "backup super block\n");
1147                         break;
1148                 }
1149         }
1150 }
1151
1152 static void *restore_worker(void *data)
1153 {
1154         struct mdrestore_struct *mdres = (struct mdrestore_struct *)data;
1155         struct async_work *async;
1156         size_t size;
1157         u8 *buffer;
1158         u8 *outbuf;
1159         int outfd;
1160         int ret;
1161
1162         outfd = fileno(mdres->out);
1163         buffer = malloc(MAX_PENDING_SIZE * 2);
1164         if (!buffer) {
1165                 fprintf(stderr, "Error allocing buffer\n");
1166                 pthread_mutex_lock(&mdres->mutex);
1167                 if (!mdres->error)
1168                         mdres->error = -ENOMEM;
1169                 pthread_mutex_unlock(&mdres->mutex);
1170                 goto out;
1171         }
1172
1173         while (1) {
1174                 int err = 0;
1175
1176                 pthread_mutex_lock(&mdres->mutex);
1177                 while (!mdres->leafsize || list_empty(&mdres->list)) {
1178                         if (mdres->done) {
1179                                 pthread_mutex_unlock(&mdres->mutex);
1180                                 goto out;
1181                         }
1182                         pthread_cond_wait(&mdres->cond, &mdres->mutex);
1183                 }
1184                 async = list_entry(mdres->list.next, struct async_work, list);
1185                 list_del_init(&async->list);
1186                 pthread_mutex_unlock(&mdres->mutex);
1187
1188                 if (mdres->compress_method == COMPRESS_ZLIB) {
1189                         size = MAX_PENDING_SIZE * 2;
1190                         ret = uncompress(buffer, (unsigned long *)&size,
1191                                          async->buffer, async->bufsize);
1192                         if (ret != Z_OK) {
1193                                 fprintf(stderr, "Error decompressing %d\n",
1194                                         ret);
1195                                 err = -EIO;
1196                         }
1197                         outbuf = buffer;
1198                 } else {
1199                         outbuf = async->buffer;
1200                         size = async->bufsize;
1201                 }
1202
1203                 if (async->start == BTRFS_SUPER_INFO_OFFSET) {
1204                         if (mdres->old_restore) {
1205                                 update_super_old(outbuf);
1206                         } else {
1207                                 ret = update_super(outbuf);
1208                                 if (ret)
1209                                         err = ret;
1210                         }
1211                 } else if (!mdres->old_restore) {
1212                         ret = fixup_chunk_tree_block(mdres, async, outbuf, size);
1213                         if (ret)
1214                                 err = ret;
1215                 }
1216
1217                 ret = pwrite64(outfd, outbuf, size, async->start);
1218                 if (ret < size) {
1219                         if (ret < 0) {
1220                                 fprintf(stderr, "Error writing to device %d\n",
1221                                         errno);
1222                                 err = errno;
1223                         } else {
1224                                 fprintf(stderr, "Short write\n");
1225                                 err = -EIO;
1226                         }
1227                 }
1228
1229                 if (async->start == BTRFS_SUPER_INFO_OFFSET)
1230                         write_backup_supers(outfd, outbuf);
1231
1232                 pthread_mutex_lock(&mdres->mutex);
1233                 if (err && !mdres->error)
1234                         mdres->error = err;
1235                 mdres->num_items--;
1236                 pthread_mutex_unlock(&mdres->mutex);
1237
1238                 free(async->buffer);
1239                 free(async);
1240         }
1241 out:
1242         free(buffer);
1243         pthread_exit(NULL);
1244 }
1245
1246 static void mdrestore_destroy(struct mdrestore_struct *mdres)
1247 {
1248         int i;
1249         pthread_mutex_lock(&mdres->mutex);
1250         mdres->done = 1;
1251         pthread_cond_broadcast(&mdres->cond);
1252         pthread_mutex_unlock(&mdres->mutex);
1253
1254         for (i = 0; i < mdres->num_threads; i++)
1255                 pthread_join(mdres->threads[i], NULL);
1256
1257         pthread_cond_destroy(&mdres->cond);
1258         pthread_mutex_destroy(&mdres->mutex);
1259         free(mdres->threads);
1260 }
1261
1262 static int mdrestore_init(struct mdrestore_struct *mdres,
1263                           FILE *in, FILE *out, int old_restore,
1264                           int num_threads)
1265 {
1266         int i, ret = 0;
1267
1268         memset(mdres, 0, sizeof(*mdres));
1269         pthread_cond_init(&mdres->cond, NULL);
1270         pthread_mutex_init(&mdres->mutex, NULL);
1271         INIT_LIST_HEAD(&mdres->list);
1272         mdres->in = in;
1273         mdres->out = out;
1274         mdres->old_restore = old_restore;
1275
1276         if (!num_threads)
1277                 return 0;
1278
1279         mdres->num_threads = num_threads;
1280         mdres->threads = calloc(num_threads, sizeof(pthread_t));
1281         if (!mdres->threads)
1282                 return -ENOMEM;
1283         for (i = 0; i < num_threads; i++) {
1284                 ret = pthread_create(mdres->threads + i, NULL, restore_worker,
1285                                      mdres);
1286                 if (ret)
1287                         break;
1288         }
1289         if (ret)
1290                 mdrestore_destroy(mdres);
1291         return ret;
1292 }
1293
1294 static int fill_mdres_info(struct mdrestore_struct *mdres,
1295                            struct async_work *async)
1296 {
1297         struct btrfs_super_block *super;
1298         u8 *buffer = NULL;
1299         u8 *outbuf;
1300         int ret;
1301
1302         if (mdres->compress_method == COMPRESS_ZLIB) {
1303                 size_t size = MAX_PENDING_SIZE * 2;
1304
1305                 buffer = malloc(MAX_PENDING_SIZE * 2);
1306                 if (!buffer)
1307                         return -ENOMEM;
1308                 ret = uncompress(buffer, (unsigned long *)&size,
1309                                  async->buffer, async->bufsize);
1310                 if (ret != Z_OK) {
1311                         fprintf(stderr, "Error decompressing %d\n", ret);
1312                         free(buffer);
1313                         return -EIO;
1314                 }
1315                 outbuf = buffer;
1316         } else {
1317                 outbuf = async->buffer;
1318         }
1319
1320         super = (struct btrfs_super_block *)outbuf;
1321         mdres->leafsize = btrfs_super_leafsize(super);
1322         memcpy(mdres->fsid, super->fsid, BTRFS_FSID_SIZE);
1323         memcpy(mdres->uuid, super->dev_item.uuid,
1324                        BTRFS_UUID_SIZE);
1325         mdres->devid = le64_to_cpu(super->dev_item.devid);
1326         free(buffer);
1327         return 0;
1328 }
1329
1330 static int add_cluster(struct meta_cluster *cluster,
1331                        struct mdrestore_struct *mdres, u64 *next)
1332 {
1333         struct meta_cluster_item *item;
1334         struct meta_cluster_header *header = &cluster->header;
1335         struct async_work *async;
1336         u64 bytenr;
1337         u32 i, nritems;
1338         int ret;
1339
1340         BUG_ON(mdres->num_items);
1341         mdres->compress_method = header->compress;
1342
1343         bytenr = le64_to_cpu(header->bytenr) + BLOCK_SIZE;
1344         nritems = le32_to_cpu(header->nritems);
1345         for (i = 0; i < nritems; i++) {
1346                 item = &cluster->items[i];
1347                 async = calloc(1, sizeof(*async));
1348                 if (!async) {
1349                         fprintf(stderr, "Error allocating async\n");
1350                         return -ENOMEM;
1351                 }
1352                 async->start = le64_to_cpu(item->bytenr);
1353                 async->bufsize = le32_to_cpu(item->size);
1354                 async->buffer = malloc(async->bufsize);
1355                 if (!async->buffer) {
1356                         fprintf(stderr, "Error allocing async buffer\n");
1357                         free(async);
1358                         return -ENOMEM;
1359                 }
1360                 ret = fread(async->buffer, async->bufsize, 1, mdres->in);
1361                 if (ret != 1) {
1362                         fprintf(stderr, "Error reading buffer %d\n", errno);
1363                         free(async->buffer);
1364                         free(async);
1365                         return -EIO;
1366                 }
1367                 bytenr += async->bufsize;
1368
1369                 pthread_mutex_lock(&mdres->mutex);
1370                 if (async->start == BTRFS_SUPER_INFO_OFFSET) {
1371                         ret = fill_mdres_info(mdres, async);
1372                         if (ret) {
1373                                 fprintf(stderr, "Error setting up restore\n");
1374                                 pthread_mutex_unlock(&mdres->mutex);
1375                                 free(async->buffer);
1376                                 free(async);
1377                                 return ret;
1378                         }
1379                 }
1380                 list_add_tail(&async->list, &mdres->list);
1381                 mdres->num_items++;
1382                 pthread_cond_signal(&mdres->cond);
1383                 pthread_mutex_unlock(&mdres->mutex);
1384         }
1385         if (bytenr & BLOCK_MASK) {
1386                 char buffer[BLOCK_MASK];
1387                 size_t size = BLOCK_SIZE - (bytenr & BLOCK_MASK);
1388
1389                 bytenr += size;
1390                 ret = fread(buffer, size, 1, mdres->in);
1391                 if (ret != 1) {
1392                         fprintf(stderr, "Error reading in buffer %d\n", errno);
1393                         return -EIO;
1394                 }
1395         }
1396         *next = bytenr;
1397         return 0;
1398 }
1399
1400 static int wait_for_worker(struct mdrestore_struct *mdres)
1401 {
1402         int ret = 0;
1403
1404         pthread_mutex_lock(&mdres->mutex);
1405         ret = mdres->error;
1406         while (!ret && mdres->num_items > 0) {
1407                 struct timespec ts = {
1408                         .tv_sec = 0,
1409                         .tv_nsec = 10000000,
1410                 };
1411                 pthread_mutex_unlock(&mdres->mutex);
1412                 nanosleep(&ts, NULL);
1413                 pthread_mutex_lock(&mdres->mutex);
1414                 ret = mdres->error;
1415         }
1416         pthread_mutex_unlock(&mdres->mutex);
1417         return ret;
1418 }
1419
1420 static int restore_metadump(const char *input, FILE *out, int old_restore,
1421                             int num_threads)
1422 {
1423         struct meta_cluster *cluster = NULL;
1424         struct meta_cluster_header *header;
1425         struct mdrestore_struct mdrestore;
1426         u64 bytenr = 0;
1427         FILE *in = NULL;
1428         int ret = 0;
1429
1430         if (!strcmp(input, "-")) {
1431                 in = stdin;
1432         } else {
1433                 in = fopen(input, "r");
1434                 if (!in) {
1435                         perror("unable to open metadump image");
1436                         return 1;
1437                 }
1438         }
1439
1440         cluster = malloc(BLOCK_SIZE);
1441         if (!cluster) {
1442                 fprintf(stderr, "Error allocating cluster\n");
1443                 if (in != stdin)
1444                         fclose(in);
1445                 return -ENOMEM;
1446         }
1447
1448         ret = mdrestore_init(&mdrestore, in, out, old_restore, num_threads);
1449         if (ret) {
1450                 fprintf(stderr, "Error initing mdrestore %d\n", ret);
1451                 if (in != stdin)
1452                         fclose(in);
1453                 free(cluster);
1454                 return ret;
1455         }
1456
1457         while (1) {
1458                 ret = fread(cluster, BLOCK_SIZE, 1, in);
1459                 if (!ret)
1460                         break;
1461
1462                 header = &cluster->header;
1463                 if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
1464                     le64_to_cpu(header->bytenr) != bytenr) {
1465                         fprintf(stderr, "bad header in metadump image\n");
1466                         ret = -EIO;
1467                         break;
1468                 }
1469                 ret = add_cluster(cluster, &mdrestore, &bytenr);
1470                 if (ret) {
1471                         fprintf(stderr, "Error adding cluster\n");
1472                         break;
1473                 }
1474
1475                 ret = wait_for_worker(&mdrestore);
1476                 if (ret) {
1477                         fprintf(stderr, "One of the threads errored out %d\n",
1478                                 ret);
1479                         break;
1480                 }
1481         }
1482
1483         mdrestore_destroy(&mdrestore);
1484         free(cluster);
1485         if (in != stdin)
1486                 fclose(in);
1487         return ret;
1488 }
1489
1490 static void print_usage(void)
1491 {
1492         fprintf(stderr, "usage: btrfs-image [options] source target\n");
1493         fprintf(stderr, "\t-r      \trestore metadump image\n");
1494         fprintf(stderr, "\t-c value\tcompression level (0 ~ 9)\n");
1495         fprintf(stderr, "\t-t value\tnumber of threads (1 ~ 32)\n");
1496         fprintf(stderr, "\t-o      \tdon't mess with the chunk tree when restoring\n");
1497         exit(1);
1498 }
1499
1500 int main(int argc, char *argv[])
1501 {
1502         char *source;
1503         char *target;
1504         int num_threads = 0;
1505         int compress_level = 0;
1506         int create = 1;
1507         int old_restore = 0;
1508         int ret;
1509         FILE *out;
1510
1511         while (1) {
1512                 int c = getopt(argc, argv, "rc:t:o");
1513                 if (c < 0)
1514                         break;
1515                 switch (c) {
1516                 case 'r':
1517                         create = 0;
1518                         break;
1519                 case 't':
1520                         num_threads = atoi(optarg);
1521                         if (num_threads <= 0 || num_threads > 32)
1522                                 print_usage();
1523                         break;
1524                 case 'c':
1525                         compress_level = atoi(optarg);
1526                         if (compress_level < 0 || compress_level > 9)
1527                                 print_usage();
1528                         break;
1529                 case 'o':
1530                         old_restore = 1;
1531                         break;
1532                 default:
1533                         print_usage();
1534                 }
1535         }
1536
1537         if (old_restore && create)
1538                 print_usage();
1539
1540         argc = argc - optind;
1541         if (argc != 2)
1542                 print_usage();
1543         source = argv[optind];
1544         target = argv[optind + 1];
1545
1546         if (create && !strcmp(target, "-")) {
1547                 out = stdout;
1548         } else {
1549                 out = fopen(target, "w+");
1550                 if (!out) {
1551                         perror("unable to create target file");
1552                         exit(1);
1553                 }
1554         }
1555
1556         if (num_threads == 0 && compress_level > 0) {
1557                 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
1558                 if (num_threads <= 0)
1559                         num_threads = 1;
1560         }
1561
1562         if (create)
1563                 ret = create_metadump(source, out, num_threads,
1564                                       compress_level);
1565         else
1566                 ret = restore_metadump(source, out, old_restore, 1);
1567
1568         if (out == stdout)
1569                 fflush(out);
1570         else
1571                 fclose(out);
1572
1573         return ret;
1574 }