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