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