btrfs-progs: alias btrfs device delete to btrfs device remove
[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 #include <pthread.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <dirent.h>
27 #include <zlib.h>
28 #include <getopt.h>
29
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 "volumes.h"
37 #include "extent_io.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 fs_chunk {
69         u64 logical;
70         u64 physical;
71         u64 bytes;
72         struct rb_node l;
73         struct rb_node p;
74         struct list_head list;
75 };
76
77 struct async_work {
78         struct list_head list;
79         struct list_head ordered;
80         u64 start;
81         u64 size;
82         u8 *buffer;
83         size_t bufsize;
84         int error;
85 };
86
87 struct metadump_struct {
88         struct btrfs_root *root;
89         FILE *out;
90
91         struct meta_cluster *cluster;
92
93         pthread_t *threads;
94         size_t num_threads;
95         pthread_mutex_t mutex;
96         pthread_cond_t cond;
97         struct rb_root name_tree;
98
99         struct list_head list;
100         struct list_head ordered;
101         size_t num_items;
102         size_t num_ready;
103
104         u64 pending_start;
105         u64 pending_size;
106
107         int compress_level;
108         int done;
109         int data;
110         int sanitize_names;
111
112         int error;
113 };
114
115 struct name {
116         struct rb_node n;
117         char *val;
118         char *sub;
119         u32 len;
120 };
121
122 struct mdrestore_struct {
123         FILE *in;
124         FILE *out;
125
126         pthread_t *threads;
127         size_t num_threads;
128         pthread_mutex_t mutex;
129         pthread_cond_t cond;
130
131         struct rb_root chunk_tree;
132         struct rb_root physical_tree;
133         struct list_head list;
134         struct list_head overlapping_chunks;
135         size_t num_items;
136         u32 leafsize;
137         u64 devid;
138         u64 alloced_chunks;
139         u64 last_physical_offset;
140         u8 uuid[BTRFS_UUID_SIZE];
141         u8 fsid[BTRFS_FSID_SIZE];
142
143         int compress_method;
144         int done;
145         int error;
146         int old_restore;
147         int fixup_offset;
148         int multi_devices;
149         int clear_space_cache;
150         struct btrfs_fs_info *info;
151 };
152
153 static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
154                                    u64 search, u64 cluster_bytenr);
155 static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size);
156
157 static void csum_block(u8 *buf, size_t len)
158 {
159         char result[BTRFS_CRC32_SIZE];
160         u32 crc = ~(u32)0;
161         crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len - BTRFS_CSUM_SIZE);
162         btrfs_csum_final(crc, result);
163         memcpy(buf, result, BTRFS_CRC32_SIZE);
164 }
165
166 static int has_name(struct btrfs_key *key)
167 {
168         switch (key->type) {
169         case BTRFS_DIR_ITEM_KEY:
170         case BTRFS_DIR_INDEX_KEY:
171         case BTRFS_INODE_REF_KEY:
172         case BTRFS_INODE_EXTREF_KEY:
173         case BTRFS_XATTR_ITEM_KEY:
174                 return 1;
175         default:
176                 break;
177         }
178
179         return 0;
180 }
181
182 static char *generate_garbage(u32 name_len)
183 {
184         char *buf = malloc(name_len);
185         int i;
186
187         if (!buf)
188                 return NULL;
189
190         for (i = 0; i < name_len; i++) {
191                 char c = rand() % 94 + 33;
192
193                 if (c == '/')
194                         c++;
195                 buf[i] = c;
196         }
197
198         return buf;
199 }
200
201 static int name_cmp(struct rb_node *a, struct rb_node *b, int fuzz)
202 {
203         struct name *entry = rb_entry(a, struct name, n);
204         struct name *ins = rb_entry(b, struct name, n);
205         u32 len;
206
207         len = min(ins->len, entry->len);
208         return memcmp(ins->val, entry->val, len);
209 }
210
211 static int chunk_cmp(struct rb_node *a, struct rb_node *b, int fuzz)
212 {
213         struct fs_chunk *entry = rb_entry(a, struct fs_chunk, l);
214         struct fs_chunk *ins = rb_entry(b, struct fs_chunk, l);
215
216         if (fuzz && ins->logical >= entry->logical &&
217             ins->logical < entry->logical + entry->bytes)
218                 return 0;
219
220         if (ins->logical < entry->logical)
221                 return -1;
222         else if (ins->logical > entry->logical)
223                 return 1;
224         return 0;
225 }
226
227 static int physical_cmp(struct rb_node *a, struct rb_node *b, int fuzz)
228 {
229         struct fs_chunk *entry = rb_entry(a, struct fs_chunk, p);
230         struct fs_chunk *ins = rb_entry(b, struct fs_chunk, p);
231
232         if (fuzz && ins->physical >= entry->physical &&
233             ins->physical < entry->physical + entry->bytes)
234                 return 0;
235
236         if (fuzz && entry->physical >= ins->physical &&
237             entry->physical < ins->physical + ins->bytes)
238                 return 0;
239
240         if (ins->physical < entry->physical)
241                 return -1;
242         else if (ins->physical > entry->physical)
243                 return 1;
244         return 0;
245 }
246
247 static void tree_insert(struct rb_root *root, struct rb_node *ins,
248                         int (*cmp)(struct rb_node *a, struct rb_node *b,
249                                    int fuzz))
250 {
251         struct rb_node ** p = &root->rb_node;
252         struct rb_node * parent = NULL;
253         int dir;
254
255         while(*p) {
256                 parent = *p;
257
258                 dir = cmp(*p, ins, 1);
259                 if (dir < 0)
260                         p = &(*p)->rb_left;
261                 else if (dir > 0)
262                         p = &(*p)->rb_right;
263                 else
264                         BUG();
265         }
266
267         rb_link_node(ins, parent, p);
268         rb_insert_color(ins, root);
269 }
270
271 static struct rb_node *tree_search(struct rb_root *root,
272                                    struct rb_node *search,
273                                    int (*cmp)(struct rb_node *a,
274                                               struct rb_node *b, int fuzz),
275                                    int fuzz)
276 {
277         struct rb_node *n = root->rb_node;
278         int dir;
279
280         while (n) {
281                 dir = cmp(n, search, fuzz);
282                 if (dir < 0)
283                         n = n->rb_left;
284                 else if (dir > 0)
285                         n = n->rb_right;
286                 else
287                         return n;
288         }
289
290         return NULL;
291 }
292
293 static u64 logical_to_physical(struct mdrestore_struct *mdres, u64 logical, u64 *size)
294 {
295         struct fs_chunk *fs_chunk;
296         struct rb_node *entry;
297         struct fs_chunk search;
298         u64 offset;
299
300         if (logical == BTRFS_SUPER_INFO_OFFSET)
301                 return logical;
302
303         search.logical = logical;
304         entry = tree_search(&mdres->chunk_tree, &search.l, chunk_cmp, 1);
305         if (!entry) {
306                 if (mdres->in != stdin)
307                         printf("Couldn't find a chunk, using logical\n");
308                 return logical;
309         }
310         fs_chunk = rb_entry(entry, struct fs_chunk, l);
311         if (fs_chunk->logical > logical || fs_chunk->logical + fs_chunk->bytes < logical)
312                 BUG();
313         offset = search.logical - fs_chunk->logical;
314
315         *size = min(*size, fs_chunk->bytes + fs_chunk->logical - logical);
316         return fs_chunk->physical + offset;
317 }
318
319
320 static char *find_collision(struct metadump_struct *md, char *name,
321                             u32 name_len)
322 {
323         struct name *val;
324         struct rb_node *entry;
325         struct name tmp;
326         unsigned long checksum;
327         int found = 0;
328         int i;
329
330         tmp.val = name;
331         tmp.len = name_len;
332         entry = tree_search(&md->name_tree, &tmp.n, name_cmp, 0);
333         if (entry) {
334                 val = rb_entry(entry, struct name, n);
335                 free(name);
336                 return val->sub;
337         }
338
339         val = malloc(sizeof(struct name));
340         if (!val) {
341                 fprintf(stderr, "Couldn't sanitize name, enomem\n");
342                 free(name);
343                 return NULL;
344         }
345
346         memset(val, 0, sizeof(*val));
347
348         val->val = name;
349         val->len = name_len;
350         val->sub = malloc(name_len);
351         if (!val->sub) {
352                 fprintf(stderr, "Couldn't sanitize name, enomem\n");
353                 free(val);
354                 free(name);
355                 return NULL;
356         }
357
358         checksum = crc32c(~1, val->val, name_len);
359         memset(val->sub, ' ', name_len);
360         i = 0;
361         while (1) {
362                 if (crc32c(~1, val->sub, name_len) == checksum &&
363                     memcmp(val->sub, val->val, val->len)) {
364                         found = 1;
365                         break;
366                 }
367
368                 if (val->sub[i] == 127) {
369                         do {
370                                 i++;
371                                 if (i >= name_len)
372                                         break;
373                         } while (val->sub[i] == 127);
374
375                         if (i >= name_len)
376                                 break;
377                         val->sub[i]++;
378                         if (val->sub[i] == '/')
379                                 val->sub[i]++;
380                         memset(val->sub, ' ', i);
381                         i = 0;
382                         continue;
383                 } else {
384                         val->sub[i]++;
385                         if (val->sub[i] == '/')
386                                 val->sub[i]++;
387                 }
388         }
389
390         if (!found) {
391                 fprintf(stderr, "Couldn't find a collision for '%.*s', "
392                         "generating normal garbage, it won't match indexes\n",
393                         val->len, val->val);
394                 for (i = 0; i < name_len; i++) {
395                         char c = rand() % 94 + 33;
396
397                         if (c == '/')
398                                 c++;
399                         val->sub[i] = c;
400                 }
401         }
402
403         tree_insert(&md->name_tree, &val->n, name_cmp);
404         return val->sub;
405 }
406
407 static void sanitize_dir_item(struct metadump_struct *md, struct extent_buffer *eb,
408                               int slot)
409 {
410         struct btrfs_dir_item *dir_item;
411         char *buf;
412         char *garbage;
413         unsigned long name_ptr;
414         u32 total_len;
415         u32 cur = 0;
416         u32 this_len;
417         u32 name_len;
418         int free_garbage = (md->sanitize_names == 1);
419
420         dir_item = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
421         total_len = btrfs_item_size_nr(eb, slot);
422         while (cur < total_len) {
423                 this_len = sizeof(*dir_item) +
424                         btrfs_dir_name_len(eb, dir_item) +
425                         btrfs_dir_data_len(eb, dir_item);
426                 name_ptr = (unsigned long)(dir_item + 1);
427                 name_len = btrfs_dir_name_len(eb, dir_item);
428
429                 if (md->sanitize_names > 1) {
430                         buf = malloc(name_len);
431                         if (!buf) {
432                                 fprintf(stderr, "Couldn't sanitize name, "
433                                         "enomem\n");
434                                 return;
435                         }
436                         read_extent_buffer(eb, buf, name_ptr, name_len);
437                         garbage = find_collision(md, buf, name_len);
438                 } else {
439                         garbage = generate_garbage(name_len);
440                 }
441                 if (!garbage) {
442                         fprintf(stderr, "Couldn't sanitize name, enomem\n");
443                         return;
444                 }
445                 write_extent_buffer(eb, garbage, name_ptr, name_len);
446                 cur += this_len;
447                 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
448                                                      this_len);
449                 if (free_garbage)
450                         free(garbage);
451         }
452 }
453
454 static void sanitize_inode_ref(struct metadump_struct *md,
455                                struct extent_buffer *eb, int slot, int ext)
456 {
457         struct btrfs_inode_extref *extref;
458         struct btrfs_inode_ref *ref;
459         char *garbage, *buf;
460         unsigned long ptr;
461         unsigned long name_ptr;
462         u32 item_size;
463         u32 cur_offset = 0;
464         int len;
465         int free_garbage = (md->sanitize_names == 1);
466
467         item_size = btrfs_item_size_nr(eb, slot);
468         ptr = btrfs_item_ptr_offset(eb, slot);
469         while (cur_offset < item_size) {
470                 if (ext) {
471                         extref = (struct btrfs_inode_extref *)(ptr +
472                                                                cur_offset);
473                         name_ptr = (unsigned long)(&extref->name);
474                         len = btrfs_inode_extref_name_len(eb, extref);
475                         cur_offset += sizeof(*extref);
476                 } else {
477                         ref = (struct btrfs_inode_ref *)(ptr + cur_offset);
478                         len = btrfs_inode_ref_name_len(eb, ref);
479                         name_ptr = (unsigned long)(ref + 1);
480                         cur_offset += sizeof(*ref);
481                 }
482                 cur_offset += len;
483
484                 if (md->sanitize_names > 1) {
485                         buf = malloc(len);
486                         if (!buf) {
487                                 fprintf(stderr, "Couldn't sanitize name, "
488                                         "enomem\n");
489                                 return;
490                         }
491                         read_extent_buffer(eb, buf, name_ptr, len);
492                         garbage = find_collision(md, buf, len);
493                 } else {
494                         garbage = generate_garbage(len);
495                 }
496
497                 if (!garbage) {
498                         fprintf(stderr, "Couldn't sanitize name, enomem\n");
499                         return;
500                 }
501                 write_extent_buffer(eb, garbage, name_ptr, len);
502                 if (free_garbage)
503                         free(garbage);
504         }
505 }
506
507 static void sanitize_xattr(struct metadump_struct *md,
508                            struct extent_buffer *eb, int slot)
509 {
510         struct btrfs_dir_item *dir_item;
511         unsigned long data_ptr;
512         u32 data_len;
513
514         dir_item = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
515         data_len = btrfs_dir_data_len(eb, dir_item);
516
517         data_ptr = (unsigned long)((char *)(dir_item + 1) +
518                                    btrfs_dir_name_len(eb, dir_item));
519         memset_extent_buffer(eb, 0, data_ptr, data_len);
520 }
521
522 static void sanitize_name(struct metadump_struct *md, u8 *dst,
523                           struct extent_buffer *src, struct btrfs_key *key,
524                           int slot)
525 {
526         struct extent_buffer *eb;
527
528         eb = alloc_dummy_eb(src->start, src->len);
529         if (!eb) {
530                 fprintf(stderr, "Couldn't sanitize name, no memory\n");
531                 return;
532         }
533
534         memcpy(eb->data, dst, eb->len);
535
536         switch (key->type) {
537         case BTRFS_DIR_ITEM_KEY:
538         case BTRFS_DIR_INDEX_KEY:
539                 sanitize_dir_item(md, eb, slot);
540                 break;
541         case BTRFS_INODE_REF_KEY:
542                 sanitize_inode_ref(md, eb, slot, 0);
543                 break;
544         case BTRFS_INODE_EXTREF_KEY:
545                 sanitize_inode_ref(md, eb, slot, 1);
546                 break;
547         case BTRFS_XATTR_ITEM_KEY:
548                 sanitize_xattr(md, eb, slot);
549                 break;
550         default:
551                 break;
552         }
553
554         memcpy(dst, eb->data, eb->len);
555         free(eb);
556 }
557
558 /*
559  * zero inline extents and csum items
560  */
561 static void zero_items(struct metadump_struct *md, u8 *dst,
562                        struct extent_buffer *src)
563 {
564         struct btrfs_file_extent_item *fi;
565         struct btrfs_item *item;
566         struct btrfs_key key;
567         u32 nritems = btrfs_header_nritems(src);
568         size_t size;
569         unsigned long ptr;
570         int i, extent_type;
571
572         for (i = 0; i < nritems; i++) {
573                 item = btrfs_item_nr(i);
574                 btrfs_item_key_to_cpu(src, &key, i);
575                 if (key.type == BTRFS_CSUM_ITEM_KEY) {
576                         size = btrfs_item_size_nr(src, i);
577                         memset(dst + btrfs_leaf_data(src) +
578                                btrfs_item_offset_nr(src, i), 0, size);
579                         continue;
580                 }
581
582                 if (md->sanitize_names && has_name(&key)) {
583                         sanitize_name(md, dst, src, &key, i);
584                         continue;
585                 }
586
587                 if (key.type != BTRFS_EXTENT_DATA_KEY)
588                         continue;
589
590                 fi = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
591                 extent_type = btrfs_file_extent_type(src, fi);
592                 if (extent_type != BTRFS_FILE_EXTENT_INLINE)
593                         continue;
594
595                 ptr = btrfs_file_extent_inline_start(fi);
596                 size = btrfs_file_extent_inline_item_len(src, item);
597                 memset(dst + ptr, 0, size);
598         }
599 }
600
601 /*
602  * copy buffer and zero useless data in the buffer
603  */
604 static void copy_buffer(struct metadump_struct *md, u8 *dst,
605                         struct extent_buffer *src)
606 {
607         int level;
608         size_t size;
609         u32 nritems;
610
611         memcpy(dst, src->data, src->len);
612         if (src->start == BTRFS_SUPER_INFO_OFFSET)
613                 return;
614
615         level = btrfs_header_level(src);
616         nritems = btrfs_header_nritems(src);
617
618         if (nritems == 0) {
619                 size = sizeof(struct btrfs_header);
620                 memset(dst + size, 0, src->len - size);
621         } else if (level == 0) {
622                 size = btrfs_leaf_data(src) +
623                         btrfs_item_offset_nr(src, nritems - 1) -
624                         btrfs_item_nr_offset(nritems);
625                 memset(dst + btrfs_item_nr_offset(nritems), 0, size);
626                 zero_items(md, dst, src);
627         } else {
628                 size = offsetof(struct btrfs_node, ptrs) +
629                         sizeof(struct btrfs_key_ptr) * nritems;
630                 memset(dst + size, 0, src->len - size);
631         }
632         csum_block(dst, src->len);
633 }
634
635 static void *dump_worker(void *data)
636 {
637         struct metadump_struct *md = (struct metadump_struct *)data;
638         struct async_work *async;
639         int ret;
640
641         while (1) {
642                 pthread_mutex_lock(&md->mutex);
643                 while (list_empty(&md->list)) {
644                         if (md->done) {
645                                 pthread_mutex_unlock(&md->mutex);
646                                 goto out;
647                         }
648                         pthread_cond_wait(&md->cond, &md->mutex);
649                 }
650                 async = list_entry(md->list.next, struct async_work, list);
651                 list_del_init(&async->list);
652                 pthread_mutex_unlock(&md->mutex);
653
654                 if (md->compress_level > 0) {
655                         u8 *orig = async->buffer;
656
657                         async->bufsize = compressBound(async->size);
658                         async->buffer = malloc(async->bufsize);
659                         if (!async->buffer) {
660                                 fprintf(stderr, "Error allocing buffer\n");
661                                 pthread_mutex_lock(&md->mutex);
662                                 if (!md->error)
663                                         md->error = -ENOMEM;
664                                 pthread_mutex_unlock(&md->mutex);
665                                 pthread_exit(NULL);
666                         }
667
668                         ret = compress2(async->buffer,
669                                          (unsigned long *)&async->bufsize,
670                                          orig, async->size, md->compress_level);
671
672                         if (ret != Z_OK)
673                                 async->error = 1;
674
675                         free(orig);
676                 }
677
678                 pthread_mutex_lock(&md->mutex);
679                 md->num_ready++;
680                 pthread_mutex_unlock(&md->mutex);
681         }
682 out:
683         pthread_exit(NULL);
684 }
685
686 static void meta_cluster_init(struct metadump_struct *md, u64 start)
687 {
688         struct meta_cluster_header *header;
689
690         md->num_items = 0;
691         md->num_ready = 0;
692         header = &md->cluster->header;
693         header->magic = cpu_to_le64(HEADER_MAGIC);
694         header->bytenr = cpu_to_le64(start);
695         header->nritems = cpu_to_le32(0);
696         header->compress = md->compress_level > 0 ?
697                            COMPRESS_ZLIB : COMPRESS_NONE;
698 }
699
700 static void metadump_destroy(struct metadump_struct *md, int num_threads)
701 {
702         int i;
703         struct rb_node *n;
704
705         pthread_mutex_lock(&md->mutex);
706         md->done = 1;
707         pthread_cond_broadcast(&md->cond);
708         pthread_mutex_unlock(&md->mutex);
709
710         for (i = 0; i < num_threads; i++)
711                 pthread_join(md->threads[i], NULL);
712
713         pthread_cond_destroy(&md->cond);
714         pthread_mutex_destroy(&md->mutex);
715
716         while ((n = rb_first(&md->name_tree))) {
717                 struct name *name;
718
719                 name = rb_entry(n, struct name, n);
720                 rb_erase(n, &md->name_tree);
721                 free(name->val);
722                 free(name->sub);
723                 free(name);
724         }
725         free(md->threads);
726         free(md->cluster);
727 }
728
729 static int metadump_init(struct metadump_struct *md, struct btrfs_root *root,
730                          FILE *out, int num_threads, int compress_level,
731                          int sanitize_names)
732 {
733         int i, ret = 0;
734
735         memset(md, 0, sizeof(*md));
736         pthread_cond_init(&md->cond, NULL);
737         pthread_mutex_init(&md->mutex, NULL);
738         INIT_LIST_HEAD(&md->list);
739         INIT_LIST_HEAD(&md->ordered);
740         md->root = root;
741         md->out = out;
742         md->pending_start = (u64)-1;
743         md->compress_level = compress_level;
744         md->cluster = calloc(1, BLOCK_SIZE);
745         md->sanitize_names = sanitize_names;
746         if (sanitize_names > 1)
747                 crc32c_optimization_init();
748
749         if (!md->cluster) {
750                 pthread_cond_destroy(&md->cond);
751                 pthread_mutex_destroy(&md->mutex);
752                 return -ENOMEM;
753         }
754
755         meta_cluster_init(md, 0);
756         if (!num_threads)
757                 return 0;
758
759         md->name_tree.rb_node = NULL;
760         md->num_threads = num_threads;
761         md->threads = calloc(num_threads, sizeof(pthread_t));
762         if (!md->threads) {
763                 free(md->cluster);
764                 pthread_cond_destroy(&md->cond);
765                 pthread_mutex_destroy(&md->mutex);
766                 return -ENOMEM;
767         }
768
769         for (i = 0; i < num_threads; i++) {
770                 ret = pthread_create(md->threads + i, NULL, dump_worker, md);
771                 if (ret)
772                         break;
773         }
774
775         if (ret)
776                 metadump_destroy(md, i + 1);
777
778         return ret;
779 }
780
781 static int write_zero(FILE *out, size_t size)
782 {
783         static char zero[BLOCK_SIZE];
784         return fwrite(zero, size, 1, out);
785 }
786
787 static int write_buffers(struct metadump_struct *md, u64 *next)
788 {
789         struct meta_cluster_header *header = &md->cluster->header;
790         struct meta_cluster_item *item;
791         struct async_work *async;
792         u64 bytenr = 0;
793         u32 nritems = 0;
794         int ret;
795         int err = 0;
796
797         if (list_empty(&md->ordered))
798                 goto out;
799
800         /* wait until all buffers are compressed */
801         while (!err && md->num_items > md->num_ready) {
802                 struct timespec ts = {
803                         .tv_sec = 0,
804                         .tv_nsec = 10000000,
805                 };
806                 pthread_mutex_unlock(&md->mutex);
807                 nanosleep(&ts, NULL);
808                 pthread_mutex_lock(&md->mutex);
809                 err = md->error;
810         }
811
812         if (err) {
813                 fprintf(stderr, "One of the threads errored out %s\n",
814                                 strerror(err));
815                 goto out;
816         }
817
818         /* setup and write index block */
819         list_for_each_entry(async, &md->ordered, ordered) {
820                 item = md->cluster->items + nritems;
821                 item->bytenr = cpu_to_le64(async->start);
822                 item->size = cpu_to_le32(async->bufsize);
823                 nritems++;
824         }
825         header->nritems = cpu_to_le32(nritems);
826
827         ret = fwrite(md->cluster, BLOCK_SIZE, 1, md->out);
828         if (ret != 1) {
829                 fprintf(stderr, "Error writing out cluster: %d\n", errno);
830                 return -EIO;
831         }
832
833         /* write buffers */
834         bytenr += le64_to_cpu(header->bytenr) + BLOCK_SIZE;
835         while (!list_empty(&md->ordered)) {
836                 async = list_entry(md->ordered.next, struct async_work,
837                                    ordered);
838                 list_del_init(&async->ordered);
839
840                 bytenr += async->bufsize;
841                 if (!err)
842                         ret = fwrite(async->buffer, async->bufsize, 1,
843                                      md->out);
844                 if (ret != 1) {
845                         err = -EIO;
846                         ret = 0;
847                         fprintf(stderr, "Error writing out cluster: %d\n",
848                                 errno);
849                 }
850
851                 free(async->buffer);
852                 free(async);
853         }
854
855         /* zero unused space in the last block */
856         if (!err && bytenr & BLOCK_MASK) {
857                 size_t size = BLOCK_SIZE - (bytenr & BLOCK_MASK);
858
859                 bytenr += size;
860                 ret = write_zero(md->out, size);
861                 if (ret != 1) {
862                         fprintf(stderr, "Error zeroing out buffer: %d\n",
863                                 errno);
864                         err = -EIO;
865                 }
866         }
867 out:
868         *next = bytenr;
869         return err;
870 }
871
872 static int read_data_extent(struct metadump_struct *md,
873                             struct async_work *async)
874 {
875         struct btrfs_multi_bio *multi = NULL;
876         struct btrfs_device *device;
877         u64 bytes_left = async->size;
878         u64 logical = async->start;
879         u64 offset = 0;
880         u64 bytenr;
881         u64 read_len;
882         ssize_t done;
883         int fd;
884         int ret;
885
886         while (bytes_left) {
887                 read_len = bytes_left;
888                 ret = btrfs_map_block(&md->root->fs_info->mapping_tree, READ,
889                                       logical, &read_len, &multi, 0, NULL);
890                 if (ret) {
891                         fprintf(stderr, "Couldn't map data block %d\n", ret);
892                         return ret;
893                 }
894
895                 device = multi->stripes[0].dev;
896
897                 if (device->fd == 0) {
898                         fprintf(stderr,
899                                 "Device we need to read from is not open\n");
900                         free(multi);
901                         return -EIO;
902                 }
903                 fd = device->fd;
904                 bytenr = multi->stripes[0].physical;
905                 free(multi);
906
907                 read_len = min(read_len, bytes_left);
908                 done = pread64(fd, async->buffer+offset, read_len, bytenr);
909                 if (done < read_len) {
910                         if (done < 0)
911                                 fprintf(stderr, "Error reading extent %d\n",
912                                         errno);
913                         else
914                                 fprintf(stderr, "Short read\n");
915                         return -EIO;
916                 }
917
918                 bytes_left -= done;
919                 offset += done;
920                 logical += done;
921         }
922
923         return 0;
924 }
925
926 static int get_dev_fd(struct btrfs_root *root)
927 {
928         struct btrfs_device *dev;
929
930         dev = list_first_entry(&root->fs_info->fs_devices->devices,
931                                struct btrfs_device, dev_list);
932         return dev->fd;
933 }
934
935 static int flush_pending(struct metadump_struct *md, int done)
936 {
937         struct async_work *async = NULL;
938         struct extent_buffer *eb;
939         u64 blocksize = md->root->nodesize;
940         u64 start;
941         u64 size;
942         size_t offset;
943         int ret = 0;
944
945         if (md->pending_size) {
946                 async = calloc(1, sizeof(*async));
947                 if (!async)
948                         return -ENOMEM;
949
950                 async->start = md->pending_start;
951                 async->size = md->pending_size;
952                 async->bufsize = async->size;
953                 async->buffer = malloc(async->bufsize);
954                 if (!async->buffer) {
955                         free(async);
956                         return -ENOMEM;
957                 }
958                 offset = 0;
959                 start = async->start;
960                 size = async->size;
961
962                 if (md->data) {
963                         ret = read_data_extent(md, async);
964                         if (ret) {
965                                 free(async->buffer);
966                                 free(async);
967                                 return ret;
968                         }
969                 }
970
971                 /*
972                  * Balance can make the mapping not cover the super block, so
973                  * just copy directly from one of the devices.
974                  */
975                 if (start == BTRFS_SUPER_INFO_OFFSET) {
976                         int fd = get_dev_fd(md->root);
977
978                         ret = pread64(fd, async->buffer, size, start);
979                         if (ret < size) {
980                                 free(async->buffer);
981                                 free(async);
982                                 fprintf(stderr, "Error reading superblock\n");
983                                 return -EIO;
984                         }
985                         size = 0;
986                         ret = 0;
987                 }
988
989                 while (!md->data && size > 0) {
990                         u64 this_read = min(blocksize, size);
991                         eb = read_tree_block(md->root, start, this_read, 0);
992                         if (!extent_buffer_uptodate(eb)) {
993                                 free(async->buffer);
994                                 free(async);
995                                 fprintf(stderr,
996                                         "Error reading metadata block\n");
997                                 return -EIO;
998                         }
999                         copy_buffer(md, async->buffer + offset, eb);
1000                         free_extent_buffer(eb);
1001                         start += this_read;
1002                         offset += this_read;
1003                         size -= this_read;
1004                 }
1005
1006                 md->pending_start = (u64)-1;
1007                 md->pending_size = 0;
1008         } else if (!done) {
1009                 return 0;
1010         }
1011
1012         pthread_mutex_lock(&md->mutex);
1013         if (async) {
1014                 list_add_tail(&async->ordered, &md->ordered);
1015                 md->num_items++;
1016                 if (md->compress_level > 0) {
1017                         list_add_tail(&async->list, &md->list);
1018                         pthread_cond_signal(&md->cond);
1019                 } else {
1020                         md->num_ready++;
1021                 }
1022         }
1023         if (md->num_items >= ITEMS_PER_CLUSTER || done) {
1024                 ret = write_buffers(md, &start);
1025                 if (ret)
1026                         fprintf(stderr, "Error writing buffers %d\n",
1027                                 errno);
1028                 else
1029                         meta_cluster_init(md, start);
1030         }
1031         pthread_mutex_unlock(&md->mutex);
1032         return ret;
1033 }
1034
1035 static int add_extent(u64 start, u64 size, struct metadump_struct *md,
1036                       int data)
1037 {
1038         int ret;
1039         if (md->data != data ||
1040             md->pending_size + size > MAX_PENDING_SIZE ||
1041             md->pending_start + md->pending_size != start) {
1042                 ret = flush_pending(md, 0);
1043                 if (ret)
1044                         return ret;
1045                 md->pending_start = start;
1046         }
1047         readahead_tree_block(md->root, start, size, 0);
1048         md->pending_size += size;
1049         md->data = data;
1050         return 0;
1051 }
1052
1053 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1054 static int is_tree_block(struct btrfs_root *extent_root,
1055                          struct btrfs_path *path, u64 bytenr)
1056 {
1057         struct extent_buffer *leaf;
1058         struct btrfs_key key;
1059         u64 ref_objectid;
1060         int ret;
1061
1062         leaf = path->nodes[0];
1063         while (1) {
1064                 struct btrfs_extent_ref_v0 *ref_item;
1065                 path->slots[0]++;
1066                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1067                         ret = btrfs_next_leaf(extent_root, path);
1068                         if (ret < 0)
1069                                 return ret;
1070                         if (ret > 0)
1071                                 break;
1072                         leaf = path->nodes[0];
1073                 }
1074                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1075                 if (key.objectid != bytenr)
1076                         break;
1077                 if (key.type != BTRFS_EXTENT_REF_V0_KEY)
1078                         continue;
1079                 ref_item = btrfs_item_ptr(leaf, path->slots[0],
1080                                           struct btrfs_extent_ref_v0);
1081                 ref_objectid = btrfs_ref_objectid_v0(leaf, ref_item);
1082                 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID)
1083                         return 1;
1084                 break;
1085         }
1086         return 0;
1087 }
1088 #endif
1089
1090 static int copy_tree_blocks(struct btrfs_root *root, struct extent_buffer *eb,
1091                             struct metadump_struct *metadump, int root_tree)
1092 {
1093         struct extent_buffer *tmp;
1094         struct btrfs_root_item *ri;
1095         struct btrfs_key key;
1096         u64 bytenr;
1097         int level;
1098         int nritems = 0;
1099         int i = 0;
1100         int ret;
1101
1102         ret = add_extent(btrfs_header_bytenr(eb), root->leafsize, metadump, 0);
1103         if (ret) {
1104                 fprintf(stderr, "Error adding metadata block\n");
1105                 return ret;
1106         }
1107
1108         if (btrfs_header_level(eb) == 0 && !root_tree)
1109                 return 0;
1110
1111         level = btrfs_header_level(eb);
1112         nritems = btrfs_header_nritems(eb);
1113         for (i = 0; i < nritems; i++) {
1114                 if (level == 0) {
1115                         btrfs_item_key_to_cpu(eb, &key, i);
1116                         if (key.type != BTRFS_ROOT_ITEM_KEY)
1117                                 continue;
1118                         ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
1119                         bytenr = btrfs_disk_root_bytenr(eb, ri);
1120                         tmp = read_tree_block(root, bytenr, root->leafsize, 0);
1121                         if (!extent_buffer_uptodate(tmp)) {
1122                                 fprintf(stderr,
1123                                         "Error reading log root block\n");
1124                                 return -EIO;
1125                         }
1126                         ret = copy_tree_blocks(root, tmp, metadump, 0);
1127                         free_extent_buffer(tmp);
1128                         if (ret)
1129                                 return ret;
1130                 } else {
1131                         bytenr = btrfs_node_blockptr(eb, i);
1132                         tmp = read_tree_block(root, bytenr, root->leafsize, 0);
1133                         if (!extent_buffer_uptodate(tmp)) {
1134                                 fprintf(stderr, "Error reading log block\n");
1135                                 return -EIO;
1136                         }
1137                         ret = copy_tree_blocks(root, tmp, metadump, root_tree);
1138                         free_extent_buffer(tmp);
1139                         if (ret)
1140                                 return ret;
1141                 }
1142         }
1143
1144         return 0;
1145 }
1146
1147 static int copy_log_trees(struct btrfs_root *root,
1148                           struct metadump_struct *metadump,
1149                           struct btrfs_path *path)
1150 {
1151         u64 blocknr = btrfs_super_log_root(root->fs_info->super_copy);
1152
1153         if (blocknr == 0)
1154                 return 0;
1155
1156         if (!root->fs_info->log_root_tree ||
1157             !root->fs_info->log_root_tree->node) {
1158                 fprintf(stderr, "Error copying tree log, it wasn't setup\n");
1159                 return -EIO;
1160         }
1161
1162         return copy_tree_blocks(root, root->fs_info->log_root_tree->node,
1163                                 metadump, 1);
1164 }
1165
1166 static int copy_space_cache(struct btrfs_root *root,
1167                             struct metadump_struct *metadump,
1168                             struct btrfs_path *path)
1169 {
1170         struct extent_buffer *leaf;
1171         struct btrfs_file_extent_item *fi;
1172         struct btrfs_key key;
1173         u64 bytenr, num_bytes;
1174         int ret;
1175
1176         root = root->fs_info->tree_root;
1177
1178         key.objectid = 0;
1179         key.type = BTRFS_EXTENT_DATA_KEY;
1180         key.offset = 0;
1181
1182         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1183         if (ret < 0) {
1184                 fprintf(stderr, "Error searching for free space inode %d\n",
1185                         ret);
1186                 return ret;
1187         }
1188
1189         leaf = path->nodes[0];
1190
1191         while (1) {
1192                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1193                         ret = btrfs_next_leaf(root, path);
1194                         if (ret < 0) {
1195                                 fprintf(stderr, "Error going to next leaf "
1196                                         "%d\n", ret);
1197                                 return ret;
1198                         }
1199                         if (ret > 0)
1200                                 break;
1201                         leaf = path->nodes[0];
1202                 }
1203
1204                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1205                 if (key.type != BTRFS_EXTENT_DATA_KEY) {
1206                         path->slots[0]++;
1207                         continue;
1208                 }
1209
1210                 fi = btrfs_item_ptr(leaf, path->slots[0],
1211                                     struct btrfs_file_extent_item);
1212                 if (btrfs_file_extent_type(leaf, fi) !=
1213                     BTRFS_FILE_EXTENT_REG) {
1214                         path->slots[0]++;
1215                         continue;
1216                 }
1217
1218                 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1219                 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1220                 ret = add_extent(bytenr, num_bytes, metadump, 1);
1221                 if (ret) {
1222                         fprintf(stderr, "Error adding space cache blocks %d\n",
1223                                 ret);
1224                         btrfs_release_path(path);
1225                         return ret;
1226                 }
1227                 path->slots[0]++;
1228         }
1229
1230         return 0;
1231 }
1232
1233 static int copy_from_extent_tree(struct metadump_struct *metadump,
1234                                  struct btrfs_path *path)
1235 {
1236         struct btrfs_root *extent_root;
1237         struct extent_buffer *leaf;
1238         struct btrfs_extent_item *ei;
1239         struct btrfs_key key;
1240         u64 bytenr;
1241         u64 num_bytes;
1242         int ret;
1243
1244         extent_root = metadump->root->fs_info->extent_root;
1245         bytenr = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
1246         key.objectid = bytenr;
1247         key.type = BTRFS_EXTENT_ITEM_KEY;
1248         key.offset = 0;
1249
1250         ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1251         if (ret < 0) {
1252                 fprintf(stderr, "Error searching extent root %d\n", ret);
1253                 return ret;
1254         }
1255         ret = 0;
1256
1257         leaf = path->nodes[0];
1258
1259         while (1) {
1260                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1261                         ret = btrfs_next_leaf(extent_root, path);
1262                         if (ret < 0) {
1263                                 fprintf(stderr, "Error going to next leaf %d"
1264                                         "\n", ret);
1265                                 break;
1266                         }
1267                         if (ret > 0) {
1268                                 ret = 0;
1269                                 break;
1270                         }
1271                         leaf = path->nodes[0];
1272                 }
1273
1274                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1275                 if (key.objectid < bytenr ||
1276                     (key.type != BTRFS_EXTENT_ITEM_KEY &&
1277                      key.type != BTRFS_METADATA_ITEM_KEY)) {
1278                         path->slots[0]++;
1279                         continue;
1280                 }
1281
1282                 bytenr = key.objectid;
1283                 if (key.type == BTRFS_METADATA_ITEM_KEY)
1284                         num_bytes = extent_root->leafsize;
1285                 else
1286                         num_bytes = key.offset;
1287
1288                 if (btrfs_item_size_nr(leaf, path->slots[0]) > sizeof(*ei)) {
1289                         ei = btrfs_item_ptr(leaf, path->slots[0],
1290                                             struct btrfs_extent_item);
1291                         if (btrfs_extent_flags(leaf, ei) &
1292                             BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1293                                 ret = add_extent(bytenr, num_bytes, metadump,
1294                                                  0);
1295                                 if (ret) {
1296                                         fprintf(stderr, "Error adding block "
1297                                                 "%d\n", ret);
1298                                         break;
1299                                 }
1300                         }
1301                 } else {
1302 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1303                         ret = is_tree_block(extent_root, path, bytenr);
1304                         if (ret < 0) {
1305                                 fprintf(stderr, "Error checking tree block "
1306                                         "%d\n", ret);
1307                                 break;
1308                         }
1309
1310                         if (ret) {
1311                                 ret = add_extent(bytenr, num_bytes, metadump,
1312                                                  0);
1313                                 if (ret) {
1314                                         fprintf(stderr, "Error adding block "
1315                                                 "%d\n", ret);
1316                                         break;
1317                                 }
1318                         }
1319                         ret = 0;
1320 #else
1321                         fprintf(stderr, "Either extent tree corruption or "
1322                                 "you haven't built with V0 support\n");
1323                         ret = -EIO;
1324                         break;
1325 #endif
1326                 }
1327                 bytenr += num_bytes;
1328         }
1329
1330         btrfs_release_path(path);
1331
1332         return ret;
1333 }
1334
1335 static int create_metadump(const char *input, FILE *out, int num_threads,
1336                            int compress_level, int sanitize, int walk_trees)
1337 {
1338         struct btrfs_root *root;
1339         struct btrfs_path *path = NULL;
1340         struct metadump_struct metadump;
1341         int ret;
1342         int err = 0;
1343
1344         root = open_ctree(input, 0, 0);
1345         if (!root) {
1346                 fprintf(stderr, "Open ctree failed\n");
1347                 return -EIO;
1348         }
1349
1350         BUG_ON(root->nodesize != root->leafsize);
1351
1352         ret = metadump_init(&metadump, root, out, num_threads,
1353                             compress_level, sanitize);
1354         if (ret) {
1355                 fprintf(stderr, "Error initing metadump %d\n", ret);
1356                 close_ctree(root);
1357                 return ret;
1358         }
1359
1360         ret = add_extent(BTRFS_SUPER_INFO_OFFSET, BTRFS_SUPER_INFO_SIZE,
1361                         &metadump, 0);
1362         if (ret) {
1363                 fprintf(stderr, "Error adding metadata %d\n", ret);
1364                 err = ret;
1365                 goto out;
1366         }
1367
1368         path = btrfs_alloc_path();
1369         if (!path) {
1370                 fprintf(stderr, "Out of memory allocing path\n");
1371                 err = -ENOMEM;
1372                 goto out;
1373         }
1374
1375         if (walk_trees) {
1376                 ret = copy_tree_blocks(root, root->fs_info->chunk_root->node,
1377                                        &metadump, 1);
1378                 if (ret) {
1379                         err = ret;
1380                         goto out;
1381                 }
1382
1383                 ret = copy_tree_blocks(root, root->fs_info->tree_root->node,
1384                                        &metadump, 1);
1385                 if (ret) {
1386                         err = ret;
1387                         goto out;
1388                 }
1389         } else {
1390                 ret = copy_from_extent_tree(&metadump, path);
1391                 if (ret) {
1392                         err = ret;
1393                         goto out;
1394                 }
1395         }
1396
1397         ret = copy_log_trees(root, &metadump, path);
1398         if (ret) {
1399                 err = ret;
1400                 goto out;
1401         }
1402
1403         ret = copy_space_cache(root, &metadump, path);
1404 out:
1405         ret = flush_pending(&metadump, 1);
1406         if (ret) {
1407                 if (!err)
1408                         err = ret;
1409                 fprintf(stderr, "Error flushing pending %d\n", ret);
1410         }
1411
1412         metadump_destroy(&metadump, num_threads);
1413
1414         btrfs_free_path(path);
1415         ret = close_ctree(root);
1416         return err ? err : ret;
1417 }
1418
1419 static void update_super_old(u8 *buffer)
1420 {
1421         struct btrfs_super_block *super = (struct btrfs_super_block *)buffer;
1422         struct btrfs_chunk *chunk;
1423         struct btrfs_disk_key *key;
1424         u32 sectorsize = btrfs_super_sectorsize(super);
1425         u64 flags = btrfs_super_flags(super);
1426
1427         flags |= BTRFS_SUPER_FLAG_METADUMP;
1428         btrfs_set_super_flags(super, flags);
1429
1430         key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1431         chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1432                                        sizeof(struct btrfs_disk_key));
1433
1434         btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1435         btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1436         btrfs_set_disk_key_offset(key, 0);
1437
1438         btrfs_set_stack_chunk_length(chunk, (u64)-1);
1439         btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1440         btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN);
1441         btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1442         btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1443         btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1444         btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1445         btrfs_set_stack_chunk_num_stripes(chunk, 1);
1446         btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1447         chunk->stripe.devid = super->dev_item.devid;
1448         btrfs_set_stack_stripe_offset(&chunk->stripe, 0);
1449         memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1450         btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1451         csum_block(buffer, BTRFS_SUPER_INFO_SIZE);
1452 }
1453
1454 static int update_super(struct mdrestore_struct *mdres, u8 *buffer)
1455 {
1456         struct btrfs_super_block *super = (struct btrfs_super_block *)buffer;
1457         struct btrfs_chunk *chunk;
1458         struct btrfs_disk_key *disk_key;
1459         struct btrfs_key key;
1460         u64 flags = btrfs_super_flags(super);
1461         u32 new_array_size = 0;
1462         u32 array_size;
1463         u32 cur = 0;
1464         u8 *ptr, *write_ptr;
1465         int old_num_stripes;
1466
1467         write_ptr = ptr = super->sys_chunk_array;
1468         array_size = btrfs_super_sys_array_size(super);
1469
1470         while (cur < array_size) {
1471                 disk_key = (struct btrfs_disk_key *)ptr;
1472                 btrfs_disk_key_to_cpu(&key, disk_key);
1473
1474                 new_array_size += sizeof(*disk_key);
1475                 memmove(write_ptr, ptr, sizeof(*disk_key));
1476
1477                 write_ptr += sizeof(*disk_key);
1478                 ptr += sizeof(*disk_key);
1479                 cur += sizeof(*disk_key);
1480
1481                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1482                         u64 physical, size = 0;
1483
1484                         chunk = (struct btrfs_chunk *)ptr;
1485                         old_num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1486                         chunk = (struct btrfs_chunk *)write_ptr;
1487
1488                         memmove(write_ptr, ptr, sizeof(*chunk));
1489                         btrfs_set_stack_chunk_num_stripes(chunk, 1);
1490                         btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1491                         btrfs_set_stack_chunk_type(chunk,
1492                                                    BTRFS_BLOCK_GROUP_SYSTEM);
1493                         btrfs_set_stack_stripe_devid(&chunk->stripe,
1494                                                      super->dev_item.devid);
1495                         physical = logical_to_physical(mdres, key.offset,
1496                                                        &size);
1497                         if (size != (u64)-1)
1498                                 btrfs_set_stack_stripe_offset(&chunk->stripe,
1499                                                               physical);
1500                         memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid,
1501                                BTRFS_UUID_SIZE);
1502                         new_array_size += sizeof(*chunk);
1503                 } else {
1504                         fprintf(stderr, "Bogus key in the sys chunk array "
1505                                 "%d\n", key.type);
1506                         return -EIO;
1507                 }
1508                 write_ptr += sizeof(*chunk);
1509                 ptr += btrfs_chunk_item_size(old_num_stripes);
1510                 cur += btrfs_chunk_item_size(old_num_stripes);
1511         }
1512
1513         if (mdres->clear_space_cache)
1514                 btrfs_set_super_cache_generation(super, 0);
1515
1516         flags |= BTRFS_SUPER_FLAG_METADUMP_V2;
1517         btrfs_set_super_flags(super, flags);
1518         btrfs_set_super_sys_array_size(super, new_array_size);
1519         csum_block(buffer, BTRFS_SUPER_INFO_SIZE);
1520
1521         return 0;
1522 }
1523
1524 static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size)
1525 {
1526         struct extent_buffer *eb;
1527
1528         eb = malloc(sizeof(struct extent_buffer) + size);
1529         if (!eb)
1530                 return NULL;
1531         memset(eb, 0, sizeof(struct extent_buffer) + size);
1532
1533         eb->start = bytenr;
1534         eb->len = size;
1535         return eb;
1536 }
1537
1538 static void truncate_item(struct extent_buffer *eb, int slot, u32 new_size)
1539 {
1540         struct btrfs_item *item;
1541         u32 nritems;
1542         u32 old_size;
1543         u32 old_data_start;
1544         u32 size_diff;
1545         u32 data_end;
1546         int i;
1547
1548         old_size = btrfs_item_size_nr(eb, slot);
1549         if (old_size == new_size)
1550                 return;
1551
1552         nritems = btrfs_header_nritems(eb);
1553         data_end = btrfs_item_offset_nr(eb, nritems - 1);
1554
1555         old_data_start = btrfs_item_offset_nr(eb, slot);
1556         size_diff = old_size - new_size;
1557
1558         for (i = slot; i < nritems; i++) {
1559                 u32 ioff;
1560                 item = btrfs_item_nr(i);
1561                 ioff = btrfs_item_offset(eb, item);
1562                 btrfs_set_item_offset(eb, item, ioff + size_diff);
1563         }
1564
1565         memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end + size_diff,
1566                               btrfs_leaf_data(eb) + data_end,
1567                               old_data_start + new_size - data_end);
1568         item = btrfs_item_nr(slot);
1569         btrfs_set_item_size(eb, item, new_size);
1570 }
1571
1572 static int fixup_chunk_tree_block(struct mdrestore_struct *mdres,
1573                                   struct async_work *async, u8 *buffer,
1574                                   size_t size)
1575 {
1576         struct extent_buffer *eb;
1577         size_t size_left = size;
1578         u64 bytenr = async->start;
1579         int i;
1580
1581         if (size_left % mdres->leafsize)
1582                 return 0;
1583
1584         eb = alloc_dummy_eb(bytenr, mdres->leafsize);
1585         if (!eb)
1586                 return -ENOMEM;
1587
1588         while (size_left) {
1589                 eb->start = bytenr;
1590                 memcpy(eb->data, buffer, mdres->leafsize);
1591
1592                 if (btrfs_header_bytenr(eb) != bytenr)
1593                         break;
1594                 if (memcmp(mdres->fsid,
1595                            eb->data + offsetof(struct btrfs_header, fsid),
1596                            BTRFS_FSID_SIZE))
1597                         break;
1598
1599                 if (btrfs_header_owner(eb) != BTRFS_CHUNK_TREE_OBJECTID)
1600                         goto next;
1601
1602                 if (btrfs_header_level(eb) != 0)
1603                         goto next;
1604
1605                 for (i = 0; i < btrfs_header_nritems(eb); i++) {
1606                         struct btrfs_chunk chunk;
1607                         struct btrfs_key key;
1608                         u64 type, physical, size = (u64)-1;
1609
1610                         btrfs_item_key_to_cpu(eb, &key, i);
1611                         if (key.type != BTRFS_CHUNK_ITEM_KEY)
1612                                 continue;
1613                         truncate_item(eb, i, sizeof(chunk));
1614                         read_extent_buffer(eb, &chunk,
1615                                            btrfs_item_ptr_offset(eb, i),
1616                                            sizeof(chunk));
1617
1618                         size = 0;
1619                         physical = logical_to_physical(mdres, key.offset,
1620                                                        &size);
1621
1622                         /* Zero out the RAID profile */
1623                         type = btrfs_stack_chunk_type(&chunk);
1624                         type &= (BTRFS_BLOCK_GROUP_DATA |
1625                                  BTRFS_BLOCK_GROUP_SYSTEM |
1626                                  BTRFS_BLOCK_GROUP_METADATA |
1627                                  BTRFS_BLOCK_GROUP_DUP);
1628                         btrfs_set_stack_chunk_type(&chunk, type);
1629
1630                         btrfs_set_stack_chunk_num_stripes(&chunk, 1);
1631                         btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
1632                         btrfs_set_stack_stripe_devid(&chunk.stripe, mdres->devid);
1633                         if (size != (u64)-1)
1634                                 btrfs_set_stack_stripe_offset(&chunk.stripe,
1635                                                               physical);
1636                         memcpy(chunk.stripe.dev_uuid, mdres->uuid,
1637                                BTRFS_UUID_SIZE);
1638                         write_extent_buffer(eb, &chunk,
1639                                             btrfs_item_ptr_offset(eb, i),
1640                                             sizeof(chunk));
1641                 }
1642                 memcpy(buffer, eb->data, eb->len);
1643                 csum_block(buffer, eb->len);
1644 next:
1645                 size_left -= mdres->leafsize;
1646                 buffer += mdres->leafsize;
1647                 bytenr += mdres->leafsize;
1648         }
1649
1650         free(eb);
1651         return 0;
1652 }
1653
1654 static void write_backup_supers(int fd, u8 *buf)
1655 {
1656         struct btrfs_super_block *super = (struct btrfs_super_block *)buf;
1657         struct stat st;
1658         u64 size;
1659         u64 bytenr;
1660         int i;
1661         int ret;
1662
1663         if (fstat(fd, &st)) {
1664                 fprintf(stderr, "Couldn't stat restore point, won't be able "
1665                         "to write backup supers: %d\n", errno);
1666                 return;
1667         }
1668
1669         size = btrfs_device_size(fd, &st);
1670
1671         for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1672                 bytenr = btrfs_sb_offset(i);
1673                 if (bytenr + BTRFS_SUPER_INFO_SIZE > size)
1674                         break;
1675                 btrfs_set_super_bytenr(super, bytenr);
1676                 csum_block(buf, BTRFS_SUPER_INFO_SIZE);
1677                 ret = pwrite64(fd, buf, BTRFS_SUPER_INFO_SIZE, bytenr);
1678                 if (ret < BTRFS_SUPER_INFO_SIZE) {
1679                         if (ret < 0)
1680                                 fprintf(stderr, "Problem writing out backup "
1681                                         "super block %d, err %d\n", i, errno);
1682                         else
1683                                 fprintf(stderr, "Short write writing out "
1684                                         "backup super block\n");
1685                         break;
1686                 }
1687         }
1688 }
1689
1690 static void *restore_worker(void *data)
1691 {
1692         struct mdrestore_struct *mdres = (struct mdrestore_struct *)data;
1693         struct async_work *async;
1694         size_t size;
1695         u8 *buffer;
1696         u8 *outbuf;
1697         int outfd;
1698         int ret;
1699         int compress_size = MAX_PENDING_SIZE * 4;
1700
1701         outfd = fileno(mdres->out);
1702         buffer = malloc(compress_size);
1703         if (!buffer) {
1704                 fprintf(stderr, "Error allocing buffer\n");
1705                 pthread_mutex_lock(&mdres->mutex);
1706                 if (!mdres->error)
1707                         mdres->error = -ENOMEM;
1708                 pthread_mutex_unlock(&mdres->mutex);
1709                 pthread_exit(NULL);
1710         }
1711
1712         while (1) {
1713                 u64 bytenr;
1714                 off_t offset = 0;
1715                 int err = 0;
1716
1717                 pthread_mutex_lock(&mdres->mutex);
1718                 while (!mdres->leafsize || list_empty(&mdres->list)) {
1719                         if (mdres->done) {
1720                                 pthread_mutex_unlock(&mdres->mutex);
1721                                 goto out;
1722                         }
1723                         pthread_cond_wait(&mdres->cond, &mdres->mutex);
1724                 }
1725                 async = list_entry(mdres->list.next, struct async_work, list);
1726                 list_del_init(&async->list);
1727                 pthread_mutex_unlock(&mdres->mutex);
1728
1729                 if (mdres->compress_method == COMPRESS_ZLIB) {
1730                         size = compress_size; 
1731                         ret = uncompress(buffer, (unsigned long *)&size,
1732                                          async->buffer, async->bufsize);
1733                         if (ret != Z_OK) {
1734                                 fprintf(stderr, "Error decompressing %d\n",
1735                                         ret);
1736                                 err = -EIO;
1737                         }
1738                         outbuf = buffer;
1739                 } else {
1740                         outbuf = async->buffer;
1741                         size = async->bufsize;
1742                 }
1743
1744                 if (!mdres->multi_devices) {
1745                         if (async->start == BTRFS_SUPER_INFO_OFFSET) {
1746                                 if (mdres->old_restore) {
1747                                         update_super_old(outbuf);
1748                                 } else {
1749                                         ret = update_super(mdres, outbuf);
1750                                         if (ret)
1751                                                 err = ret;
1752                                 }
1753                         } else if (!mdres->old_restore) {
1754                                 ret = fixup_chunk_tree_block(mdres, async, outbuf, size);
1755                                 if (ret)
1756                                         err = ret;
1757                         }
1758                 }
1759
1760                 if (!mdres->fixup_offset) {
1761                         while (size) {
1762                                 u64 chunk_size = size;
1763                                 if (!mdres->multi_devices && !mdres->old_restore)
1764                                         bytenr = logical_to_physical(mdres,
1765                                                                      async->start + offset,
1766                                                                      &chunk_size);
1767                                 else
1768                                         bytenr = async->start + offset;
1769
1770                                 ret = pwrite64(outfd, outbuf+offset, chunk_size,
1771                                                bytenr);
1772                                 if (ret != chunk_size) {
1773                                         if (ret < 0) {
1774                                                 fprintf(stderr, "Error writing to "
1775                                                         "device %d\n", errno);
1776                                                 err = errno;
1777                                                 break;
1778                                         } else {
1779                                                 fprintf(stderr, "Short write\n");
1780                                                 err = -EIO;
1781                                                 break;
1782                                         }
1783                                 }
1784                                 size -= chunk_size;
1785                                 offset += chunk_size;
1786                         }
1787                 } else if (async->start != BTRFS_SUPER_INFO_OFFSET) {
1788                         ret = write_data_to_disk(mdres->info, outbuf, async->start, size, 0);
1789                         if (ret) {
1790                                 printk("Error write data\n");
1791                                 exit(1);
1792                         }
1793                 }
1794
1795
1796                 /* backup super blocks are already there at fixup_offset stage */
1797                 if (!mdres->multi_devices && async->start == BTRFS_SUPER_INFO_OFFSET)
1798                         write_backup_supers(outfd, outbuf);
1799
1800                 pthread_mutex_lock(&mdres->mutex);
1801                 if (err && !mdres->error)
1802                         mdres->error = err;
1803                 mdres->num_items--;
1804                 pthread_mutex_unlock(&mdres->mutex);
1805
1806                 free(async->buffer);
1807                 free(async);
1808         }
1809 out:
1810         free(buffer);
1811         pthread_exit(NULL);
1812 }
1813
1814 static void mdrestore_destroy(struct mdrestore_struct *mdres, int num_threads)
1815 {
1816         struct rb_node *n;
1817         int i;
1818
1819         while ((n = rb_first(&mdres->chunk_tree))) {
1820                 struct fs_chunk *entry;
1821
1822                 entry = rb_entry(n, struct fs_chunk, l);
1823                 rb_erase(n, &mdres->chunk_tree);
1824                 rb_erase(&entry->p, &mdres->physical_tree);
1825                 free(entry);
1826         }
1827         pthread_mutex_lock(&mdres->mutex);
1828         mdres->done = 1;
1829         pthread_cond_broadcast(&mdres->cond);
1830         pthread_mutex_unlock(&mdres->mutex);
1831
1832         for (i = 0; i < num_threads; i++)
1833                 pthread_join(mdres->threads[i], NULL);
1834
1835         pthread_cond_destroy(&mdres->cond);
1836         pthread_mutex_destroy(&mdres->mutex);
1837         free(mdres->threads);
1838 }
1839
1840 static int mdrestore_init(struct mdrestore_struct *mdres,
1841                           FILE *in, FILE *out, int old_restore,
1842                           int num_threads, int fixup_offset,
1843                           struct btrfs_fs_info *info, int multi_devices)
1844 {
1845         int i, ret = 0;
1846
1847         memset(mdres, 0, sizeof(*mdres));
1848         pthread_cond_init(&mdres->cond, NULL);
1849         pthread_mutex_init(&mdres->mutex, NULL);
1850         INIT_LIST_HEAD(&mdres->list);
1851         INIT_LIST_HEAD(&mdres->overlapping_chunks);
1852         mdres->in = in;
1853         mdres->out = out;
1854         mdres->old_restore = old_restore;
1855         mdres->chunk_tree.rb_node = NULL;
1856         mdres->fixup_offset = fixup_offset;
1857         mdres->info = info;
1858         mdres->multi_devices = multi_devices;
1859         mdres->clear_space_cache = 0;
1860         mdres->last_physical_offset = 0;
1861         mdres->alloced_chunks = 0;
1862
1863         if (!num_threads)
1864                 return 0;
1865
1866         mdres->num_threads = num_threads;
1867         mdres->threads = calloc(num_threads, sizeof(pthread_t));
1868         if (!mdres->threads)
1869                 return -ENOMEM;
1870         for (i = 0; i < num_threads; i++) {
1871                 ret = pthread_create(mdres->threads + i, NULL, restore_worker,
1872                                      mdres);
1873                 if (ret)
1874                         break;
1875         }
1876         if (ret)
1877                 mdrestore_destroy(mdres, i + 1);
1878         return ret;
1879 }
1880
1881 static int fill_mdres_info(struct mdrestore_struct *mdres,
1882                            struct async_work *async)
1883 {
1884         struct btrfs_super_block *super;
1885         u8 *buffer = NULL;
1886         u8 *outbuf;
1887         int ret;
1888
1889         /* We've already been initialized */
1890         if (mdres->leafsize)
1891                 return 0;
1892
1893         if (mdres->compress_method == COMPRESS_ZLIB) {
1894                 size_t size = MAX_PENDING_SIZE * 2;
1895
1896                 buffer = malloc(MAX_PENDING_SIZE * 2);
1897                 if (!buffer)
1898                         return -ENOMEM;
1899                 ret = uncompress(buffer, (unsigned long *)&size,
1900                                  async->buffer, async->bufsize);
1901                 if (ret != Z_OK) {
1902                         fprintf(stderr, "Error decompressing %d\n", ret);
1903                         free(buffer);
1904                         return -EIO;
1905                 }
1906                 outbuf = buffer;
1907         } else {
1908                 outbuf = async->buffer;
1909         }
1910
1911         super = (struct btrfs_super_block *)outbuf;
1912         mdres->leafsize = btrfs_super_leafsize(super);
1913         memcpy(mdres->fsid, super->fsid, BTRFS_FSID_SIZE);
1914         memcpy(mdres->uuid, super->dev_item.uuid,
1915                        BTRFS_UUID_SIZE);
1916         mdres->devid = le64_to_cpu(super->dev_item.devid);
1917         free(buffer);
1918         return 0;
1919 }
1920
1921 static int add_cluster(struct meta_cluster *cluster,
1922                        struct mdrestore_struct *mdres, u64 *next)
1923 {
1924         struct meta_cluster_item *item;
1925         struct meta_cluster_header *header = &cluster->header;
1926         struct async_work *async;
1927         u64 bytenr;
1928         u32 i, nritems;
1929         int ret;
1930
1931         mdres->compress_method = header->compress;
1932
1933         bytenr = le64_to_cpu(header->bytenr) + BLOCK_SIZE;
1934         nritems = le32_to_cpu(header->nritems);
1935         for (i = 0; i < nritems; i++) {
1936                 item = &cluster->items[i];
1937                 async = calloc(1, sizeof(*async));
1938                 if (!async) {
1939                         fprintf(stderr, "Error allocating async\n");
1940                         return -ENOMEM;
1941                 }
1942                 async->start = le64_to_cpu(item->bytenr);
1943                 async->bufsize = le32_to_cpu(item->size);
1944                 async->buffer = malloc(async->bufsize);
1945                 if (!async->buffer) {
1946                         fprintf(stderr, "Error allocing async buffer\n");
1947                         free(async);
1948                         return -ENOMEM;
1949                 }
1950                 ret = fread(async->buffer, async->bufsize, 1, mdres->in);
1951                 if (ret != 1) {
1952                         fprintf(stderr, "Error reading buffer %d\n", errno);
1953                         free(async->buffer);
1954                         free(async);
1955                         return -EIO;
1956                 }
1957                 bytenr += async->bufsize;
1958
1959                 pthread_mutex_lock(&mdres->mutex);
1960                 if (async->start == BTRFS_SUPER_INFO_OFFSET) {
1961                         ret = fill_mdres_info(mdres, async);
1962                         if (ret) {
1963                                 fprintf(stderr, "Error setting up restore\n");
1964                                 pthread_mutex_unlock(&mdres->mutex);
1965                                 free(async->buffer);
1966                                 free(async);
1967                                 return ret;
1968                         }
1969                 }
1970                 list_add_tail(&async->list, &mdres->list);
1971                 mdres->num_items++;
1972                 pthread_cond_signal(&mdres->cond);
1973                 pthread_mutex_unlock(&mdres->mutex);
1974         }
1975         if (bytenr & BLOCK_MASK) {
1976                 char buffer[BLOCK_MASK];
1977                 size_t size = BLOCK_SIZE - (bytenr & BLOCK_MASK);
1978
1979                 bytenr += size;
1980                 ret = fread(buffer, size, 1, mdres->in);
1981                 if (ret != 1) {
1982                         fprintf(stderr, "Error reading in buffer %d\n", errno);
1983                         return -EIO;
1984                 }
1985         }
1986         *next = bytenr;
1987         return 0;
1988 }
1989
1990 static int wait_for_worker(struct mdrestore_struct *mdres)
1991 {
1992         int ret = 0;
1993
1994         pthread_mutex_lock(&mdres->mutex);
1995         ret = mdres->error;
1996         while (!ret && mdres->num_items > 0) {
1997                 struct timespec ts = {
1998                         .tv_sec = 0,
1999                         .tv_nsec = 10000000,
2000                 };
2001                 pthread_mutex_unlock(&mdres->mutex);
2002                 nanosleep(&ts, NULL);
2003                 pthread_mutex_lock(&mdres->mutex);
2004                 ret = mdres->error;
2005         }
2006         pthread_mutex_unlock(&mdres->mutex);
2007         return ret;
2008 }
2009
2010 static int read_chunk_block(struct mdrestore_struct *mdres, u8 *buffer,
2011                             u64 bytenr, u64 item_bytenr, u32 bufsize,
2012                             u64 cluster_bytenr)
2013 {
2014         struct extent_buffer *eb;
2015         int ret = 0;
2016         int i;
2017
2018         eb = alloc_dummy_eb(bytenr, mdres->leafsize);
2019         if (!eb) {
2020                 ret = -ENOMEM;
2021                 goto out;
2022         }
2023
2024         while (item_bytenr != bytenr) {
2025                 buffer += mdres->leafsize;
2026                 item_bytenr += mdres->leafsize;
2027         }
2028
2029         memcpy(eb->data, buffer, mdres->leafsize);
2030         if (btrfs_header_bytenr(eb) != bytenr) {
2031                 fprintf(stderr, "Eb bytenr doesn't match found bytenr\n");
2032                 ret = -EIO;
2033                 goto out;
2034         }
2035
2036         if (memcmp(mdres->fsid, eb->data + offsetof(struct btrfs_header, fsid),
2037                    BTRFS_FSID_SIZE)) {
2038                 fprintf(stderr, "Fsid doesn't match\n");
2039                 ret = -EIO;
2040                 goto out;
2041         }
2042
2043         if (btrfs_header_owner(eb) != BTRFS_CHUNK_TREE_OBJECTID) {
2044                 fprintf(stderr, "Does not belong to the chunk tree\n");
2045                 ret = -EIO;
2046                 goto out;
2047         }
2048
2049         for (i = 0; i < btrfs_header_nritems(eb); i++) {
2050                 struct btrfs_chunk chunk;
2051                 struct fs_chunk *fs_chunk;
2052                 struct btrfs_key key;
2053
2054                 if (btrfs_header_level(eb)) {
2055                         u64 blockptr = btrfs_node_blockptr(eb, i);
2056
2057                         ret = search_for_chunk_blocks(mdres, blockptr,
2058                                                       cluster_bytenr);
2059                         if (ret)
2060                                 break;
2061                         continue;
2062                 }
2063
2064                 /* Yay a leaf!  We loves leafs! */
2065                 btrfs_item_key_to_cpu(eb, &key, i);
2066                 if (key.type != BTRFS_CHUNK_ITEM_KEY)
2067                         continue;
2068
2069                 fs_chunk = malloc(sizeof(struct fs_chunk));
2070                 if (!fs_chunk) {
2071                         fprintf(stderr, "Erorr allocating chunk\n");
2072                         ret = -ENOMEM;
2073                         break;
2074                 }
2075                 memset(fs_chunk, 0, sizeof(*fs_chunk));
2076                 read_extent_buffer(eb, &chunk, btrfs_item_ptr_offset(eb, i),
2077                                    sizeof(chunk));
2078
2079                 fs_chunk->logical = key.offset;
2080                 fs_chunk->physical = btrfs_stack_stripe_offset(&chunk.stripe);
2081                 fs_chunk->bytes = btrfs_stack_chunk_length(&chunk);
2082                 INIT_LIST_HEAD(&fs_chunk->list);
2083                 if (tree_search(&mdres->physical_tree, &fs_chunk->p,
2084                                 physical_cmp, 1) != NULL)
2085                         list_add(&fs_chunk->list, &mdres->overlapping_chunks);
2086                 else
2087                         tree_insert(&mdres->physical_tree, &fs_chunk->p,
2088                                     physical_cmp);
2089                 if (fs_chunk->physical + fs_chunk->bytes >
2090                     mdres->last_physical_offset)
2091                         mdres->last_physical_offset = fs_chunk->physical +
2092                                 fs_chunk->bytes;
2093                 mdres->alloced_chunks += fs_chunk->bytes;
2094                 tree_insert(&mdres->chunk_tree, &fs_chunk->l, chunk_cmp);
2095         }
2096 out:
2097         free(eb);
2098         return ret;
2099 }
2100
2101 /* If you have to ask you aren't worthy */
2102 static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
2103                                    u64 search, u64 cluster_bytenr)
2104 {
2105         struct meta_cluster *cluster;
2106         struct meta_cluster_header *header;
2107         struct meta_cluster_item *item;
2108         u64 current_cluster = cluster_bytenr, bytenr;
2109         u64 item_bytenr;
2110         u32 bufsize, nritems, i;
2111         u32 max_size = MAX_PENDING_SIZE * 2;
2112         u8 *buffer, *tmp = NULL;
2113         int ret = 0;
2114
2115         cluster = malloc(BLOCK_SIZE);
2116         if (!cluster) {
2117                 fprintf(stderr, "Error allocating cluster\n");
2118                 return -ENOMEM;
2119         }
2120
2121         buffer = malloc(max_size);
2122         if (!buffer) {
2123                 fprintf(stderr, "Error allocing buffer\n");
2124                 free(cluster);
2125                 return -ENOMEM;
2126         }
2127
2128         if (mdres->compress_method == COMPRESS_ZLIB) {
2129                 tmp = malloc(max_size);
2130                 if (!tmp) {
2131                         fprintf(stderr, "Error allocing tmp buffer\n");
2132                         free(cluster);
2133                         free(buffer);
2134                         return -ENOMEM;
2135                 }
2136         }
2137
2138         bytenr = current_cluster;
2139         while (1) {
2140                 if (fseek(mdres->in, current_cluster, SEEK_SET)) {
2141                         fprintf(stderr, "Error seeking: %d\n", errno);
2142                         ret = -EIO;
2143                         break;
2144                 }
2145
2146                 ret = fread(cluster, BLOCK_SIZE, 1, mdres->in);
2147                 if (ret == 0) {
2148                         if (cluster_bytenr != 0) {
2149                                 cluster_bytenr = 0;
2150                                 current_cluster = 0;
2151                                 bytenr = 0;
2152                                 continue;
2153                         }
2154                         printf("ok this is where we screwed up?\n");
2155                         ret = -EIO;
2156                         break;
2157                 } else if (ret < 0) {
2158                         fprintf(stderr, "Error reading image\n");
2159                         break;
2160                 }
2161                 ret = 0;
2162
2163                 header = &cluster->header;
2164                 if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
2165                     le64_to_cpu(header->bytenr) != current_cluster) {
2166                         fprintf(stderr, "bad header in metadump image\n");
2167                         ret = -EIO;
2168                         break;
2169                 }
2170
2171                 bytenr += BLOCK_SIZE;
2172                 nritems = le32_to_cpu(header->nritems);
2173                 for (i = 0; i < nritems; i++) {
2174                         size_t size;
2175
2176                         item = &cluster->items[i];
2177                         bufsize = le32_to_cpu(item->size);
2178                         item_bytenr = le64_to_cpu(item->bytenr);
2179
2180                         if (bufsize > max_size) {
2181                                 fprintf(stderr, "item %u size %u too big\n",
2182                                         i, bufsize);
2183                                 ret = -EIO;
2184                                 break;
2185                         }
2186
2187                         if (mdres->compress_method == COMPRESS_ZLIB) {
2188                                 ret = fread(tmp, bufsize, 1, mdres->in);
2189                                 if (ret != 1) {
2190                                         fprintf(stderr, "Error reading: %d\n",
2191                                                 errno);
2192                                         ret = -EIO;
2193                                         break;
2194                                 }
2195
2196                                 size = max_size;
2197                                 ret = uncompress(buffer,
2198                                                  (unsigned long *)&size, tmp,
2199                                                  bufsize);
2200                                 if (ret != Z_OK) {
2201                                         fprintf(stderr, "Error decompressing "
2202                                                 "%d\n", ret);
2203                                         ret = -EIO;
2204                                         break;
2205                                 }
2206                         } else {
2207                                 ret = fread(buffer, bufsize, 1, mdres->in);
2208                                 if (ret != 1) {
2209                                         fprintf(stderr, "Error reading: %d\n",
2210                                                 errno);
2211                                         ret = -EIO;
2212                                         break;
2213                                 }
2214                                 size = bufsize;
2215                         }
2216                         ret = 0;
2217
2218                         if (item_bytenr <= search &&
2219                             item_bytenr + size > search) {
2220                                 ret = read_chunk_block(mdres, buffer, search,
2221                                                        item_bytenr, size,
2222                                                        current_cluster);
2223                                 if (!ret)
2224                                         ret = 1;
2225                                 break;
2226                         }
2227                         bytenr += bufsize;
2228                 }
2229                 if (ret) {
2230                         if (ret > 0)
2231                                 ret = 0;
2232                         break;
2233                 }
2234                 if (bytenr & BLOCK_MASK)
2235                         bytenr += BLOCK_SIZE - (bytenr & BLOCK_MASK);
2236                 current_cluster = bytenr;
2237         }
2238
2239         free(tmp);
2240         free(buffer);
2241         free(cluster);
2242         return ret;
2243 }
2244
2245 static int build_chunk_tree(struct mdrestore_struct *mdres,
2246                             struct meta_cluster *cluster)
2247 {
2248         struct btrfs_super_block *super;
2249         struct meta_cluster_header *header;
2250         struct meta_cluster_item *item = NULL;
2251         u64 chunk_root_bytenr = 0;
2252         u32 i, nritems;
2253         u64 bytenr = 0;
2254         u8 *buffer;
2255         int ret;
2256
2257         /* We can't seek with stdin so don't bother doing this */
2258         if (mdres->in == stdin)
2259                 return 0;
2260
2261         ret = fread(cluster, BLOCK_SIZE, 1, mdres->in);
2262         if (ret <= 0) {
2263                 fprintf(stderr, "Error reading in cluster: %d\n", errno);
2264                 return -EIO;
2265         }
2266         ret = 0;
2267
2268         header = &cluster->header;
2269         if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
2270             le64_to_cpu(header->bytenr) != 0) {
2271                 fprintf(stderr, "bad header in metadump image\n");
2272                 return -EIO;
2273         }
2274
2275         bytenr += BLOCK_SIZE;
2276         mdres->compress_method = header->compress;
2277         nritems = le32_to_cpu(header->nritems);
2278         for (i = 0; i < nritems; i++) {
2279                 item = &cluster->items[i];
2280
2281                 if (le64_to_cpu(item->bytenr) == BTRFS_SUPER_INFO_OFFSET)
2282                         break;
2283                 bytenr += le32_to_cpu(item->size);
2284                 if (fseek(mdres->in, le32_to_cpu(item->size), SEEK_CUR)) {
2285                         fprintf(stderr, "Error seeking: %d\n", errno);
2286                         return -EIO;
2287                 }
2288         }
2289
2290         if (!item || le64_to_cpu(item->bytenr) != BTRFS_SUPER_INFO_OFFSET) {
2291                 fprintf(stderr, "Huh, didn't find the super?\n");
2292                 return -EINVAL;
2293         }
2294
2295         buffer = malloc(le32_to_cpu(item->size));
2296         if (!buffer) {
2297                 fprintf(stderr, "Error allocing buffer\n");
2298                 return -ENOMEM;
2299         }
2300
2301         ret = fread(buffer, le32_to_cpu(item->size), 1, mdres->in);
2302         if (ret != 1) {
2303                 fprintf(stderr, "Error reading buffer: %d\n", errno);
2304                 free(buffer);
2305                 return -EIO;
2306         }
2307
2308         if (mdres->compress_method == COMPRESS_ZLIB) {
2309                 size_t size = MAX_PENDING_SIZE * 2;
2310                 u8 *tmp;
2311
2312                 tmp = malloc(MAX_PENDING_SIZE * 2);
2313                 if (!tmp) {
2314                         free(buffer);
2315                         return -ENOMEM;
2316                 }
2317                 ret = uncompress(tmp, (unsigned long *)&size,
2318                                  buffer, le32_to_cpu(item->size));
2319                 if (ret != Z_OK) {
2320                         fprintf(stderr, "Error decompressing %d\n", ret);
2321                         free(buffer);
2322                         free(tmp);
2323                         return -EIO;
2324                 }
2325                 free(buffer);
2326                 buffer = tmp;
2327         }
2328
2329         pthread_mutex_lock(&mdres->mutex);
2330         super = (struct btrfs_super_block *)buffer;
2331         chunk_root_bytenr = btrfs_super_chunk_root(super);
2332         mdres->leafsize = btrfs_super_leafsize(super);
2333         memcpy(mdres->fsid, super->fsid, BTRFS_FSID_SIZE);
2334         memcpy(mdres->uuid, super->dev_item.uuid,
2335                        BTRFS_UUID_SIZE);
2336         mdres->devid = le64_to_cpu(super->dev_item.devid);
2337         free(buffer);
2338         pthread_mutex_unlock(&mdres->mutex);
2339
2340         return search_for_chunk_blocks(mdres, chunk_root_bytenr, 0);
2341 }
2342
2343 static int range_contains_super(u64 physical, u64 bytes)
2344 {
2345         u64 super_bytenr;
2346         int i;
2347
2348         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2349                 super_bytenr = btrfs_sb_offset(i);
2350                 if (super_bytenr >= physical &&
2351                     super_bytenr < physical + bytes)
2352                         return 1;
2353         }
2354
2355         return 0;
2356 }
2357
2358 static void remap_overlapping_chunks(struct mdrestore_struct *mdres)
2359 {
2360         struct fs_chunk *fs_chunk;
2361
2362         while (!list_empty(&mdres->overlapping_chunks)) {
2363                 fs_chunk = list_first_entry(&mdres->overlapping_chunks,
2364                                             struct fs_chunk, list);
2365                 list_del_init(&fs_chunk->list);
2366                 if (range_contains_super(fs_chunk->physical,
2367                                          fs_chunk->bytes)) {
2368                         fprintf(stderr, "Remapping a chunk that had a super "
2369                                 "mirror inside of it, clearing space cache "
2370                                 "so we don't end up with corruption\n");
2371                         mdres->clear_space_cache = 1;
2372                 }
2373                 fs_chunk->physical = mdres->last_physical_offset;
2374                 tree_insert(&mdres->physical_tree, &fs_chunk->p, physical_cmp);
2375                 mdres->last_physical_offset += fs_chunk->bytes;
2376         }
2377 }
2378
2379 static int fixup_devices(struct btrfs_fs_info *fs_info,
2380                          struct mdrestore_struct *mdres, off_t dev_size)
2381 {
2382         struct btrfs_trans_handle *trans;
2383         struct btrfs_dev_item *dev_item;
2384         struct btrfs_path *path;
2385         struct extent_buffer *leaf;
2386         struct btrfs_root *root = fs_info->chunk_root;
2387         struct btrfs_key key;
2388         u64 devid, cur_devid;
2389         int ret;
2390
2391         path = btrfs_alloc_path();
2392         if (!path) {
2393                 fprintf(stderr, "Error alloc'ing path\n");
2394                 return -ENOMEM;
2395         }
2396
2397         trans = btrfs_start_transaction(fs_info->tree_root, 1);
2398         if (IS_ERR(trans)) {
2399                 fprintf(stderr, "Error starting transaction %ld\n",
2400                         PTR_ERR(trans));
2401                 btrfs_free_path(path);
2402                 return PTR_ERR(trans);
2403         }
2404
2405         dev_item = &fs_info->super_copy->dev_item;
2406
2407         devid = btrfs_stack_device_id(dev_item);
2408
2409         btrfs_set_stack_device_total_bytes(dev_item, dev_size);
2410         btrfs_set_stack_device_bytes_used(dev_item, mdres->alloced_chunks);
2411
2412         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2413         key.type = BTRFS_DEV_ITEM_KEY;
2414         key.offset = 0;
2415
2416 again:
2417         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2418         if (ret < 0) {
2419                 fprintf(stderr, "search failed %d\n", ret);
2420                 exit(1);
2421         }
2422
2423         while (1) {
2424                 leaf = path->nodes[0];
2425                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2426                         ret = btrfs_next_leaf(root, path);
2427                         if (ret < 0) {
2428                                 fprintf(stderr, "Error going to next leaf "
2429                                         "%d\n", ret);
2430                                 exit(1);
2431                         }
2432                         if (ret > 0) {
2433                                 ret = 0;
2434                                 break;
2435                         }
2436                         leaf = path->nodes[0];
2437                 }
2438
2439                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2440                 if (key.type > BTRFS_DEV_ITEM_KEY)
2441                         break;
2442                 if (key.type != BTRFS_DEV_ITEM_KEY) {
2443                         path->slots[0]++;
2444                         continue;
2445                 }
2446
2447                 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2448                                           struct btrfs_dev_item);
2449                 cur_devid = btrfs_device_id(leaf, dev_item);
2450                 if (devid != cur_devid) {
2451                         ret = btrfs_del_item(trans, root, path);
2452                         if (ret) {
2453                                 fprintf(stderr, "Error deleting item %d\n",
2454                                         ret);
2455                                 exit(1);
2456                         }
2457                         btrfs_release_path(path);
2458                         goto again;
2459                 }
2460
2461                 btrfs_set_device_total_bytes(leaf, dev_item, dev_size);
2462                 btrfs_set_device_bytes_used(leaf, dev_item,
2463                                             mdres->alloced_chunks);
2464                 btrfs_mark_buffer_dirty(leaf);
2465                 path->slots[0]++;
2466         }
2467
2468         btrfs_free_path(path);
2469         ret = btrfs_commit_transaction(trans, fs_info->tree_root);
2470         if (ret) {
2471                 fprintf(stderr, "Commit failed %d\n", ret);
2472                 return ret;
2473         }
2474         return 0;
2475 }
2476
2477 static int restore_metadump(const char *input, FILE *out, int old_restore,
2478                             int num_threads, int fixup_offset,
2479                             const char *target, int multi_devices)
2480 {
2481         struct meta_cluster *cluster = NULL;
2482         struct meta_cluster_header *header;
2483         struct mdrestore_struct mdrestore;
2484         struct btrfs_fs_info *info = NULL;
2485         u64 bytenr = 0;
2486         FILE *in = NULL;
2487         int ret = 0;
2488
2489         if (!strcmp(input, "-")) {
2490                 in = stdin;
2491         } else {
2492                 in = fopen(input, "r");
2493                 if (!in) {
2494                         perror("unable to open metadump image");
2495                         return 1;
2496                 }
2497         }
2498
2499         /* NOTE: open with write mode */
2500         if (fixup_offset) {
2501                 BUG_ON(!target);
2502                 info = open_ctree_fs_info(target, 0, 0,
2503                                           OPEN_CTREE_WRITES |
2504                                           OPEN_CTREE_RESTORE |
2505                                           OPEN_CTREE_PARTIAL);
2506                 if (!info) {
2507                         fprintf(stderr, "%s: open ctree failed\n", __func__);
2508                         ret = -EIO;
2509                         goto failed_open;
2510                 }
2511         }
2512
2513         cluster = malloc(BLOCK_SIZE);
2514         if (!cluster) {
2515                 fprintf(stderr, "Error allocating cluster\n");
2516                 ret = -ENOMEM;
2517                 goto failed_info;
2518         }
2519
2520         ret = mdrestore_init(&mdrestore, in, out, old_restore, num_threads,
2521                              fixup_offset, info, multi_devices);
2522         if (ret) {
2523                 fprintf(stderr, "Error initing mdrestore %d\n", ret);
2524                 goto failed_cluster;
2525         }
2526
2527         if (!multi_devices && !old_restore) {
2528                 ret = build_chunk_tree(&mdrestore, cluster);
2529                 if (ret)
2530                         goto out;
2531                 if (!list_empty(&mdrestore.overlapping_chunks))
2532                         remap_overlapping_chunks(&mdrestore);
2533         }
2534
2535         if (in != stdin && fseek(in, 0, SEEK_SET)) {
2536                 fprintf(stderr, "Error seeking %d\n", errno);
2537                 goto out;
2538         }
2539
2540         while (!mdrestore.error) {
2541                 ret = fread(cluster, BLOCK_SIZE, 1, in);
2542                 if (!ret)
2543                         break;
2544
2545                 header = &cluster->header;
2546                 if (le64_to_cpu(header->magic) != HEADER_MAGIC ||
2547                     le64_to_cpu(header->bytenr) != bytenr) {
2548                         fprintf(stderr, "bad header in metadump image\n");
2549                         ret = -EIO;
2550                         break;
2551                 }
2552                 ret = add_cluster(cluster, &mdrestore, &bytenr);
2553                 if (ret) {
2554                         fprintf(stderr, "Error adding cluster\n");
2555                         break;
2556                 }
2557         }
2558         ret = wait_for_worker(&mdrestore);
2559
2560         if (!ret && !multi_devices && !old_restore) {
2561                 struct btrfs_root *root;
2562                 struct stat st;
2563
2564                 root = open_ctree_fd(fileno(out), target, 0,
2565                                           OPEN_CTREE_PARTIAL |
2566                                           OPEN_CTREE_WRITES |
2567                                           OPEN_CTREE_NO_DEVICES);
2568                 if (!root) {
2569                         fprintf(stderr, "unable to open %s\n", target);
2570                         ret = -EIO;
2571                         goto out;
2572                 }
2573                 info = root->fs_info;
2574
2575                 if (stat(target, &st)) {
2576                         fprintf(stderr, "statting %s failed\n", target);
2577                         close_ctree(info->chunk_root);
2578                         return 1;
2579                 }
2580
2581                 ret = fixup_devices(info, &mdrestore, st.st_size);
2582                 close_ctree(info->chunk_root);
2583                 if (ret)
2584                         goto out;
2585         }
2586 out:
2587         mdrestore_destroy(&mdrestore, num_threads);
2588 failed_cluster:
2589         free(cluster);
2590 failed_info:
2591         if (fixup_offset && info)
2592                 close_ctree(info->chunk_root);
2593 failed_open:
2594         if (in != stdin)
2595                 fclose(in);
2596         return ret;
2597 }
2598
2599 static int update_disk_super_on_device(struct btrfs_fs_info *info,
2600                                        const char *other_dev, u64 cur_devid)
2601 {
2602         struct btrfs_key key;
2603         struct extent_buffer *leaf;
2604         struct btrfs_path path;
2605         struct btrfs_dev_item *dev_item;
2606         struct btrfs_super_block *disk_super;
2607         char dev_uuid[BTRFS_UUID_SIZE];
2608         char fs_uuid[BTRFS_UUID_SIZE];
2609         u64 devid, type, io_align, io_width;
2610         u64 sector_size, total_bytes, bytes_used;
2611         char *buf;
2612         int fp;
2613         int ret;
2614
2615         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2616         key.type = BTRFS_DEV_ITEM_KEY;
2617         key.offset = cur_devid;
2618
2619         btrfs_init_path(&path);
2620         ret = btrfs_search_slot(NULL, info->chunk_root, &key, &path, 0, 0); 
2621         if (ret) {
2622                 fprintf(stderr, "search key fails\n");
2623                 exit(1);
2624         }
2625
2626         leaf = path.nodes[0];
2627         dev_item = btrfs_item_ptr(leaf, path.slots[0],
2628                                   struct btrfs_dev_item);
2629
2630         devid = btrfs_device_id(leaf, dev_item);
2631         if (devid != cur_devid) {
2632                 printk("devid %llu mismatch with %llu\n", devid, cur_devid);
2633                 exit(1);
2634         }
2635
2636         type = btrfs_device_type(leaf, dev_item);
2637         io_align = btrfs_device_io_align(leaf, dev_item);
2638         io_width = btrfs_device_io_width(leaf, dev_item);
2639         sector_size = btrfs_device_sector_size(leaf, dev_item);
2640         total_bytes = btrfs_device_total_bytes(leaf, dev_item);
2641         bytes_used = btrfs_device_bytes_used(leaf, dev_item);
2642         read_extent_buffer(leaf, dev_uuid, (unsigned long)btrfs_device_uuid(dev_item), BTRFS_UUID_SIZE);
2643         read_extent_buffer(leaf, fs_uuid, (unsigned long)btrfs_device_fsid(dev_item), BTRFS_UUID_SIZE);
2644
2645         btrfs_release_path(&path);
2646
2647         printk("update disk super on %s devid=%llu\n", other_dev, devid);
2648
2649         /* update other devices' super block */
2650         fp = open(other_dev, O_CREAT | O_RDWR, 0600);
2651         if (fp < 0) {
2652                 fprintf(stderr, "could not open %s\n", other_dev);
2653                 exit(1);
2654         }
2655
2656         buf = malloc(BTRFS_SUPER_INFO_SIZE);
2657         if (!buf) {
2658                 ret = -ENOMEM;
2659                 close(fp);
2660                 return ret;
2661         }
2662
2663         memcpy(buf, info->super_copy, BTRFS_SUPER_INFO_SIZE);
2664
2665         disk_super = (struct btrfs_super_block *)buf;
2666         dev_item = &disk_super->dev_item;
2667
2668         btrfs_set_stack_device_type(dev_item, type);
2669         btrfs_set_stack_device_id(dev_item, devid);
2670         btrfs_set_stack_device_total_bytes(dev_item, total_bytes);
2671         btrfs_set_stack_device_bytes_used(dev_item, bytes_used);
2672         btrfs_set_stack_device_io_align(dev_item, io_align);
2673         btrfs_set_stack_device_io_width(dev_item, io_width);
2674         btrfs_set_stack_device_sector_size(dev_item, sector_size);
2675         memcpy(dev_item->uuid, dev_uuid, BTRFS_UUID_SIZE);
2676         memcpy(dev_item->fsid, fs_uuid, BTRFS_UUID_SIZE);
2677         csum_block((u8 *)buf, BTRFS_SUPER_INFO_SIZE);
2678
2679         ret = pwrite64(fp, buf, BTRFS_SUPER_INFO_SIZE, BTRFS_SUPER_INFO_OFFSET);
2680         if (ret != BTRFS_SUPER_INFO_SIZE) {
2681                 ret = -EIO;
2682                 goto out;
2683         }
2684
2685         write_backup_supers(fp, (u8 *)buf);
2686
2687 out:
2688         free(buf);
2689         close(fp);
2690         return 0;
2691 }
2692
2693 static void print_usage(int ret)
2694 {
2695         fprintf(stderr, "usage: btrfs-image [options] source target\n");
2696         fprintf(stderr, "\t-r      \trestore metadump image\n");
2697         fprintf(stderr, "\t-c value\tcompression level (0 ~ 9)\n");
2698         fprintf(stderr, "\t-t value\tnumber of threads (1 ~ 32)\n");
2699         fprintf(stderr, "\t-o      \tdon't mess with the chunk tree when restoring\n");
2700         fprintf(stderr, "\t-s      \tsanitize file names, use once to just use garbage, use twice if you want crc collisions\n");
2701         fprintf(stderr, "\t-w      \twalk all trees instead of using extent tree, do this if your extent tree is broken\n");
2702         fprintf(stderr, "\t-m      \trestore for multiple devices\n");
2703         fprintf(stderr, "\n");
2704         fprintf(stderr, "\tIn the dump mode, source is the btrfs device and target is the output file (use '-' for stdout).\n");
2705         fprintf(stderr, "\tIn the restore mode, source is the dumped image and target is the btrfs device/file.\n");
2706         exit(ret);
2707 }
2708
2709 int main(int argc, char *argv[])
2710 {
2711         char *source;
2712         char *target;
2713         u64 num_threads = 1;
2714         u64 compress_level = 0;
2715         int create = 1;
2716         int old_restore = 0;
2717         int walk_trees = 0;
2718         int multi_devices = 0;
2719         int ret;
2720         int sanitize = 0;
2721         int dev_cnt = 0;
2722         int usage_error = 0;
2723         FILE *out;
2724
2725         while (1) {
2726                 static const struct option long_options[] = {
2727                         { "help", no_argument, NULL, GETOPT_VAL_HELP},
2728                         { NULL, 0, NULL, 0 }
2729                 };
2730                 int c = getopt_long(argc, argv, "rc:t:oswm", long_options, NULL);
2731                 if (c < 0)
2732                         break;
2733                 switch (c) {
2734                 case 'r':
2735                         create = 0;
2736                         break;
2737                 case 't':
2738                         num_threads = arg_strtou64(optarg);
2739                         if (num_threads > 32)
2740                                 print_usage(1);
2741                         break;
2742                 case 'c':
2743                         compress_level = arg_strtou64(optarg);
2744                         if (compress_level > 9)
2745                                 print_usage(1);
2746                         break;
2747                 case 'o':
2748                         old_restore = 1;
2749                         break;
2750                 case 's':
2751                         sanitize++;
2752                         break;
2753                 case 'w':
2754                         walk_trees = 1;
2755                         break;
2756                 case 'm':
2757                         create = 0;
2758                         multi_devices = 1;
2759                         break;
2760                         case GETOPT_VAL_HELP:
2761                 default:
2762                         print_usage(c != GETOPT_VAL_HELP);
2763                 }
2764         }
2765
2766         argc = argc - optind;
2767         set_argv0(argv);
2768         if (check_argc_min(argc, 2))
2769                 print_usage(1);
2770
2771         dev_cnt = argc - 1;
2772
2773         if (create) {
2774                 if (old_restore) {
2775                         fprintf(stderr, "Usage error: create and restore cannot be used at the same time\n");
2776                         usage_error++;
2777                 }
2778         } else {
2779                 if (walk_trees || sanitize || compress_level) {
2780                         fprintf(stderr, "Usage error: use -w, -s, -c options for restore makes no sense\n");
2781                         usage_error++;
2782                 }
2783                 if (multi_devices && dev_cnt < 2) {
2784                         fprintf(stderr, "Usage error: not enough devices specified for -m option\n");
2785                         usage_error++;
2786                 }
2787                 if (!multi_devices && dev_cnt != 1) {
2788                         fprintf(stderr, "Usage error: accepts only 1 device without -m option\n");
2789                         usage_error++;
2790                 }
2791         }
2792
2793         if (usage_error)
2794                 print_usage(1);
2795
2796         source = argv[optind];
2797         target = argv[optind + 1];
2798
2799         if (create && !strcmp(target, "-")) {
2800                 out = stdout;
2801         } else {
2802                 out = fopen(target, "w+");
2803                 if (!out) {
2804                         perror("unable to create target file");
2805                         exit(1);
2806                 }
2807         }
2808
2809         if (num_threads == 1 && compress_level > 0) {
2810                 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
2811                 if (num_threads <= 0)
2812                         num_threads = 1;
2813         }
2814
2815         if (create) {
2816                 ret = check_mounted(source);
2817                 if (ret < 0) {
2818                         fprintf(stderr, "Could not check mount status: %s\n",
2819                                 strerror(-ret));
2820                         exit(1);
2821                 } else if (ret)
2822                         fprintf(stderr,
2823                 "WARNING: The device is mounted. Make sure the filesystem is quiescent.\n");
2824
2825                 ret = create_metadump(source, out, num_threads,
2826                                       compress_level, sanitize, walk_trees);
2827         } else {
2828                 ret = restore_metadump(source, out, old_restore, num_threads,
2829                                        0, target, multi_devices);
2830         }
2831         if (ret) {
2832                 printk("%s failed (%s)\n", (create) ? "create" : "restore",
2833                        strerror(errno));
2834                 goto out;
2835         }
2836
2837          /* extended support for multiple devices */
2838         if (!create && multi_devices) {
2839                 struct btrfs_fs_info *info;
2840                 u64 total_devs;
2841                 int i;
2842
2843                 info = open_ctree_fs_info(target, 0, 0,
2844                                           OPEN_CTREE_PARTIAL |
2845                                           OPEN_CTREE_RESTORE);
2846                 if (!info) {
2847                         int e = errno;
2848                         fprintf(stderr, "unable to open %s error = %s\n",
2849                                 target, strerror(e));
2850                         return 1;
2851                 }
2852
2853                 total_devs = btrfs_super_num_devices(info->super_copy);
2854                 if (total_devs != dev_cnt) {
2855                         printk("it needs %llu devices but has only %d\n",
2856                                 total_devs, dev_cnt);
2857                         close_ctree(info->chunk_root);
2858                         goto out;
2859                 }
2860
2861                 /* update super block on other disks */
2862                 for (i = 2; i <= dev_cnt; i++) {
2863                         ret = update_disk_super_on_device(info,
2864                                         argv[optind + i], (u64)i);
2865                         if (ret) {
2866                                 printk("update disk super failed devid=%d (error=%d)\n",
2867                                         i, ret);
2868                                 close_ctree(info->chunk_root);
2869                                 exit(1);
2870                         }
2871                 }
2872
2873                 close_ctree(info->chunk_root);
2874
2875                 /* fix metadata block to map correct chunk */
2876                 ret = restore_metadump(source, out, 0, num_threads, 1,
2877                                        target, 1);
2878                 if (ret) {
2879                         fprintf(stderr, "fix metadump failed (error=%d)\n",
2880                                 ret);
2881                         exit(1);
2882                 }
2883         }
2884 out:
2885         if (out == stdout) {
2886                 fflush(out);
2887         } else {
2888                 fclose(out);
2889                 if (ret && create) {
2890                         int unlink_ret;
2891
2892                         unlink_ret = unlink(target);
2893                         if (unlink_ret)
2894                                 fprintf(stderr,
2895                                         "unlink output file failed : %s\n",
2896                                         strerror(errno));
2897                 }
2898         }
2899
2900         return !!ret;
2901 }