forked from kdave/btrfs-progs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btrfs-image.c
2937 lines (2565 loc) · 68.5 KB
/
btrfs-image.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008 Oracle. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <zlib.h>
#include <getopt.h>
#include "kerncompat.h"
#include "crc32c.h"
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"
#include "utils.h"
#include "volumes.h"
#include "extent_io.h"
#define HEADER_MAGIC 0xbd5c25e27295668bULL
#define MAX_PENDING_SIZE (256 * 1024)
#define BLOCK_SIZE 1024
#define BLOCK_MASK (BLOCK_SIZE - 1)
#define COMPRESS_NONE 0
#define COMPRESS_ZLIB 1
struct meta_cluster_item {
__le64 bytenr;
__le32 size;
} __attribute__ ((__packed__));
struct meta_cluster_header {
__le64 magic;
__le64 bytenr;
__le32 nritems;
u8 compress;
} __attribute__ ((__packed__));
/* cluster header + index items + buffers */
struct meta_cluster {
struct meta_cluster_header header;
struct meta_cluster_item items[];
} __attribute__ ((__packed__));
#define ITEMS_PER_CLUSTER ((BLOCK_SIZE - sizeof(struct meta_cluster)) / \
sizeof(struct meta_cluster_item))
struct fs_chunk {
u64 logical;
u64 physical;
/*
* physical_dup only store additonal physical for BTRFS_BLOCK_GROUP_DUP
* currently restore only support single and DUP
* TODO: modify this structure and the function related to this
* structure for support RAID*
*/
u64 physical_dup;
u64 bytes;
struct rb_node l;
struct rb_node p;
struct list_head list;
};
struct async_work {
struct list_head list;
struct list_head ordered;
u64 start;
u64 size;
u8 *buffer;
size_t bufsize;
int error;
};
struct metadump_struct {
struct btrfs_root *root;
FILE *out;
struct meta_cluster *cluster;
pthread_t *threads;
size_t num_threads;
pthread_mutex_t mutex;
pthread_cond_t cond;
struct rb_root name_tree;
struct list_head list;
struct list_head ordered;
size_t num_items;
size_t num_ready;
u64 pending_start;
u64 pending_size;
int compress_level;
int done;
int data;
int sanitize_names;
int error;
};
struct name {
struct rb_node n;
char *val;
char *sub;
u32 len;
};
struct mdrestore_struct {
FILE *in;
FILE *out;
pthread_t *threads;
size_t num_threads;
pthread_mutex_t mutex;
pthread_cond_t cond;
struct rb_root chunk_tree;
struct rb_root physical_tree;
struct list_head list;
struct list_head overlapping_chunks;
size_t num_items;
u32 nodesize;
u64 devid;
u64 alloced_chunks;
u64 last_physical_offset;
u8 uuid[BTRFS_UUID_SIZE];
u8 fsid[BTRFS_FSID_SIZE];
int compress_method;
int done;
int error;
int old_restore;
int fixup_offset;
int multi_devices;
int clear_space_cache;
struct btrfs_fs_info *info;
};
static int search_for_chunk_blocks(struct mdrestore_struct *mdres,
u64 search, u64 cluster_bytenr);
static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size);
static void csum_block(u8 *buf, size_t len)
{
char result[BTRFS_CRC32_SIZE];
u32 crc = ~(u32)0;
crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len - BTRFS_CSUM_SIZE);
btrfs_csum_final(crc, result);
memcpy(buf, result, BTRFS_CRC32_SIZE);
}
static int has_name(struct btrfs_key *key)
{
switch (key->type) {
case BTRFS_DIR_ITEM_KEY:
case BTRFS_DIR_INDEX_KEY:
case BTRFS_INODE_REF_KEY:
case BTRFS_INODE_EXTREF_KEY:
case BTRFS_XATTR_ITEM_KEY:
return 1;
default:
break;
}
return 0;
}
static char *generate_garbage(u32 name_len)
{
char *buf = malloc(name_len);
int i;
if (!buf)
return NULL;
for (i = 0; i < name_len; i++) {
char c = rand_range(94) + 33;
if (c == '/')
c++;
buf[i] = c;
}
return buf;
}
static int name_cmp(struct rb_node *a, struct rb_node *b, int fuzz)
{
struct name *entry = rb_entry(a, struct name, n);
struct name *ins = rb_entry(b, struct name, n);
u32 len;
len = min(ins->len, entry->len);
return memcmp(ins->val, entry->val, len);
}
static int chunk_cmp(struct rb_node *a, struct rb_node *b, int fuzz)
{
struct fs_chunk *entry = rb_entry(a, struct fs_chunk, l);
struct fs_chunk *ins = rb_entry(b, struct fs_chunk, l);
if (fuzz && ins->logical >= entry->logical &&
ins->logical < entry->logical + entry->bytes)
return 0;
if (ins->logical < entry->logical)
return -1;
else if (ins->logical > entry->logical)
return 1;
return 0;
}
static int physical_cmp(struct rb_node *a, struct rb_node *b, int fuzz)
{
struct fs_chunk *entry = rb_entry(a, struct fs_chunk, p);
struct fs_chunk *ins = rb_entry(b, struct fs_chunk, p);
if (fuzz && ins->physical >= entry->physical &&
ins->physical < entry->physical + entry->bytes)
return 0;
if (fuzz && entry->physical >= ins->physical &&
entry->physical < ins->physical + ins->bytes)
return 0;
if (ins->physical < entry->physical)
return -1;
else if (ins->physical > entry->physical)
return 1;
return 0;
}
static void tree_insert(struct rb_root *root, struct rb_node *ins,
int (*cmp)(struct rb_node *a, struct rb_node *b,
int fuzz))
{
struct rb_node ** p = &root->rb_node;
struct rb_node * parent = NULL;
int dir;
while(*p) {
parent = *p;
dir = cmp(*p, ins, 1);
if (dir < 0)
p = &(*p)->rb_left;
else if (dir > 0)
p = &(*p)->rb_right;
else
BUG();
}
rb_link_node(ins, parent, p);
rb_insert_color(ins, root);
}
static struct rb_node *tree_search(struct rb_root *root,
struct rb_node *search,
int (*cmp)(struct rb_node *a,
struct rb_node *b, int fuzz),
int fuzz)
{
struct rb_node *n = root->rb_node;
int dir;
while (n) {
dir = cmp(n, search, fuzz);
if (dir < 0)
n = n->rb_left;
else if (dir > 0)
n = n->rb_right;
else
return n;
}
return NULL;
}
static u64 logical_to_physical(struct mdrestore_struct *mdres, u64 logical,
u64 *size, u64 *physical_dup)
{
struct fs_chunk *fs_chunk;
struct rb_node *entry;
struct fs_chunk search;
u64 offset;
if (logical == BTRFS_SUPER_INFO_OFFSET)
return logical;
search.logical = logical;
entry = tree_search(&mdres->chunk_tree, &search.l, chunk_cmp, 1);
if (!entry) {
if (mdres->in != stdin)
printf("Couldn't find a chunk, using logical\n");
return logical;
}
fs_chunk = rb_entry(entry, struct fs_chunk, l);
if (fs_chunk->logical > logical || fs_chunk->logical + fs_chunk->bytes < logical)
BUG();
offset = search.logical - fs_chunk->logical;
if (physical_dup) {
/* Only in dup case, physical_dup is not equal to 0 */
if (fs_chunk->physical_dup)
*physical_dup = fs_chunk->physical_dup + offset;
else
*physical_dup = 0;
}
*size = min(*size, fs_chunk->bytes + fs_chunk->logical - logical);
return fs_chunk->physical + offset;
}
static char *find_collision(struct metadump_struct *md, char *name,
u32 name_len)
{
struct name *val;
struct rb_node *entry;
struct name tmp;
unsigned long checksum;
int found = 0;
int i;
tmp.val = name;
tmp.len = name_len;
entry = tree_search(&md->name_tree, &tmp.n, name_cmp, 0);
if (entry) {
val = rb_entry(entry, struct name, n);
free(name);
return val->sub;
}
val = malloc(sizeof(struct name));
if (!val) {
fprintf(stderr, "Couldn't sanitize name, enomem\n");
free(name);
return NULL;
}
memset(val, 0, sizeof(*val));
val->val = name;
val->len = name_len;
val->sub = malloc(name_len);
if (!val->sub) {
fprintf(stderr, "Couldn't sanitize name, enomem\n");
free(val);
free(name);
return NULL;
}
checksum = crc32c(~1, val->val, name_len);
memset(val->sub, ' ', name_len);
i = 0;
while (1) {
if (crc32c(~1, val->sub, name_len) == checksum &&
memcmp(val->sub, val->val, val->len)) {
found = 1;
break;
}
if (val->sub[i] == 127) {
do {
i++;
if (i >= name_len)
break;
} while (val->sub[i] == 127);
if (i >= name_len)
break;
val->sub[i]++;
if (val->sub[i] == '/')
val->sub[i]++;
memset(val->sub, ' ', i);
i = 0;
continue;
} else {
val->sub[i]++;
if (val->sub[i] == '/')
val->sub[i]++;
}
}
if (!found) {
fprintf(stderr, "Couldn't find a collision for '%.*s', "
"generating normal garbage, it won't match indexes\n",
val->len, val->val);
for (i = 0; i < name_len; i++) {
char c = rand_range(94) + 33;
if (c == '/')
c++;
val->sub[i] = c;
}
}
tree_insert(&md->name_tree, &val->n, name_cmp);
return val->sub;
}
static void sanitize_dir_item(struct metadump_struct *md, struct extent_buffer *eb,
int slot)
{
struct btrfs_dir_item *dir_item;
char *buf;
char *garbage;
unsigned long name_ptr;
u32 total_len;
u32 cur = 0;
u32 this_len;
u32 name_len;
int free_garbage = (md->sanitize_names == 1);
dir_item = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
total_len = btrfs_item_size_nr(eb, slot);
while (cur < total_len) {
this_len = sizeof(*dir_item) +
btrfs_dir_name_len(eb, dir_item) +
btrfs_dir_data_len(eb, dir_item);
name_ptr = (unsigned long)(dir_item + 1);
name_len = btrfs_dir_name_len(eb, dir_item);
if (md->sanitize_names > 1) {
buf = malloc(name_len);
if (!buf) {
fprintf(stderr, "Couldn't sanitize name, "
"enomem\n");
return;
}
read_extent_buffer(eb, buf, name_ptr, name_len);
garbage = find_collision(md, buf, name_len);
} else {
garbage = generate_garbage(name_len);
}
if (!garbage) {
fprintf(stderr, "Couldn't sanitize name, enomem\n");
return;
}
write_extent_buffer(eb, garbage, name_ptr, name_len);
cur += this_len;
dir_item = (struct btrfs_dir_item *)((char *)dir_item +
this_len);
if (free_garbage)
free(garbage);
}
}
static void sanitize_inode_ref(struct metadump_struct *md,
struct extent_buffer *eb, int slot, int ext)
{
struct btrfs_inode_extref *extref;
struct btrfs_inode_ref *ref;
char *garbage, *buf;
unsigned long ptr;
unsigned long name_ptr;
u32 item_size;
u32 cur_offset = 0;
int len;
int free_garbage = (md->sanitize_names == 1);
item_size = btrfs_item_size_nr(eb, slot);
ptr = btrfs_item_ptr_offset(eb, slot);
while (cur_offset < item_size) {
if (ext) {
extref = (struct btrfs_inode_extref *)(ptr +
cur_offset);
name_ptr = (unsigned long)(&extref->name);
len = btrfs_inode_extref_name_len(eb, extref);
cur_offset += sizeof(*extref);
} else {
ref = (struct btrfs_inode_ref *)(ptr + cur_offset);
len = btrfs_inode_ref_name_len(eb, ref);
name_ptr = (unsigned long)(ref + 1);
cur_offset += sizeof(*ref);
}
cur_offset += len;
if (md->sanitize_names > 1) {
buf = malloc(len);
if (!buf) {
fprintf(stderr, "Couldn't sanitize name, "
"enomem\n");
return;
}
read_extent_buffer(eb, buf, name_ptr, len);
garbage = find_collision(md, buf, len);
} else {
garbage = generate_garbage(len);
}
if (!garbage) {
fprintf(stderr, "Couldn't sanitize name, enomem\n");
return;
}
write_extent_buffer(eb, garbage, name_ptr, len);
if (free_garbage)
free(garbage);
}
}
static void sanitize_xattr(struct metadump_struct *md,
struct extent_buffer *eb, int slot)
{
struct btrfs_dir_item *dir_item;
unsigned long data_ptr;
u32 data_len;
dir_item = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
data_len = btrfs_dir_data_len(eb, dir_item);
data_ptr = (unsigned long)((char *)(dir_item + 1) +
btrfs_dir_name_len(eb, dir_item));
memset_extent_buffer(eb, 0, data_ptr, data_len);
}
static void sanitize_name(struct metadump_struct *md, u8 *dst,
struct extent_buffer *src, struct btrfs_key *key,
int slot)
{
struct extent_buffer *eb;
eb = alloc_dummy_eb(src->start, src->len);
if (!eb) {
fprintf(stderr, "Couldn't sanitize name, no memory\n");
return;
}
memcpy(eb->data, dst, eb->len);
switch (key->type) {
case BTRFS_DIR_ITEM_KEY:
case BTRFS_DIR_INDEX_KEY:
sanitize_dir_item(md, eb, slot);
break;
case BTRFS_INODE_REF_KEY:
sanitize_inode_ref(md, eb, slot, 0);
break;
case BTRFS_INODE_EXTREF_KEY:
sanitize_inode_ref(md, eb, slot, 1);
break;
case BTRFS_XATTR_ITEM_KEY:
sanitize_xattr(md, eb, slot);
break;
default:
break;
}
memcpy(dst, eb->data, eb->len);
free(eb);
}
/*
* zero inline extents and csum items
*/
static void zero_items(struct metadump_struct *md, u8 *dst,
struct extent_buffer *src)
{
struct btrfs_file_extent_item *fi;
struct btrfs_item *item;
struct btrfs_key key;
u32 nritems = btrfs_header_nritems(src);
size_t size;
unsigned long ptr;
int i, extent_type;
for (i = 0; i < nritems; i++) {
item = btrfs_item_nr(i);
btrfs_item_key_to_cpu(src, &key, i);
if (key.type == BTRFS_CSUM_ITEM_KEY) {
size = btrfs_item_size_nr(src, i);
memset(dst + btrfs_leaf_data(src) +
btrfs_item_offset_nr(src, i), 0, size);
continue;
}
if (md->sanitize_names && has_name(&key)) {
sanitize_name(md, dst, src, &key, i);
continue;
}
if (key.type != BTRFS_EXTENT_DATA_KEY)
continue;
fi = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
extent_type = btrfs_file_extent_type(src, fi);
if (extent_type != BTRFS_FILE_EXTENT_INLINE)
continue;
ptr = btrfs_file_extent_inline_start(fi);
size = btrfs_file_extent_inline_item_len(src, item);
memset(dst + ptr, 0, size);
}
}
/*
* copy buffer and zero useless data in the buffer
*/
static void copy_buffer(struct metadump_struct *md, u8 *dst,
struct extent_buffer *src)
{
int level;
size_t size;
u32 nritems;
memcpy(dst, src->data, src->len);
if (src->start == BTRFS_SUPER_INFO_OFFSET)
return;
level = btrfs_header_level(src);
nritems = btrfs_header_nritems(src);
if (nritems == 0) {
size = sizeof(struct btrfs_header);
memset(dst + size, 0, src->len - size);
} else if (level == 0) {
size = btrfs_leaf_data(src) +
btrfs_item_offset_nr(src, nritems - 1) -
btrfs_item_nr_offset(nritems);
memset(dst + btrfs_item_nr_offset(nritems), 0, size);
zero_items(md, dst, src);
} else {
size = offsetof(struct btrfs_node, ptrs) +
sizeof(struct btrfs_key_ptr) * nritems;
memset(dst + size, 0, src->len - size);
}
csum_block(dst, src->len);
}
static void *dump_worker(void *data)
{
struct metadump_struct *md = (struct metadump_struct *)data;
struct async_work *async;
int ret;
while (1) {
pthread_mutex_lock(&md->mutex);
while (list_empty(&md->list)) {
if (md->done) {
pthread_mutex_unlock(&md->mutex);
goto out;
}
pthread_cond_wait(&md->cond, &md->mutex);
}
async = list_entry(md->list.next, struct async_work, list);
list_del_init(&async->list);
pthread_mutex_unlock(&md->mutex);
if (md->compress_level > 0) {
u8 *orig = async->buffer;
async->bufsize = compressBound(async->size);
async->buffer = malloc(async->bufsize);
if (!async->buffer) {
fprintf(stderr, "Error allocating buffer\n");
pthread_mutex_lock(&md->mutex);
if (!md->error)
md->error = -ENOMEM;
pthread_mutex_unlock(&md->mutex);
pthread_exit(NULL);
}
ret = compress2(async->buffer,
(unsigned long *)&async->bufsize,
orig, async->size, md->compress_level);
if (ret != Z_OK)
async->error = 1;
free(orig);
}
pthread_mutex_lock(&md->mutex);
md->num_ready++;
pthread_mutex_unlock(&md->mutex);
}
out:
pthread_exit(NULL);
}
static void meta_cluster_init(struct metadump_struct *md, u64 start)
{
struct meta_cluster_header *header;
md->num_items = 0;
md->num_ready = 0;
header = &md->cluster->header;
header->magic = cpu_to_le64(HEADER_MAGIC);
header->bytenr = cpu_to_le64(start);
header->nritems = cpu_to_le32(0);
header->compress = md->compress_level > 0 ?
COMPRESS_ZLIB : COMPRESS_NONE;
}
static void metadump_destroy(struct metadump_struct *md, int num_threads)
{
int i;
struct rb_node *n;
pthread_mutex_lock(&md->mutex);
md->done = 1;
pthread_cond_broadcast(&md->cond);
pthread_mutex_unlock(&md->mutex);
for (i = 0; i < num_threads; i++)
pthread_join(md->threads[i], NULL);
pthread_cond_destroy(&md->cond);
pthread_mutex_destroy(&md->mutex);
while ((n = rb_first(&md->name_tree))) {
struct name *name;
name = rb_entry(n, struct name, n);
rb_erase(n, &md->name_tree);
free(name->val);
free(name->sub);
free(name);
}
free(md->threads);
free(md->cluster);
}
static int metadump_init(struct metadump_struct *md, struct btrfs_root *root,
FILE *out, int num_threads, int compress_level,
int sanitize_names)
{
int i, ret = 0;
memset(md, 0, sizeof(*md));
md->cluster = calloc(1, BLOCK_SIZE);
if (!md->cluster)
return -ENOMEM;
md->threads = calloc(num_threads, sizeof(pthread_t));
if (!md->threads) {
free(md->cluster);
return -ENOMEM;
}
INIT_LIST_HEAD(&md->list);
INIT_LIST_HEAD(&md->ordered);
md->root = root;
md->out = out;
md->pending_start = (u64)-1;
md->compress_level = compress_level;
md->sanitize_names = sanitize_names;
if (sanitize_names > 1)
crc32c_optimization_init();
md->name_tree.rb_node = NULL;
md->num_threads = num_threads;
pthread_cond_init(&md->cond, NULL);
pthread_mutex_init(&md->mutex, NULL);
meta_cluster_init(md, 0);
if (!num_threads)
return 0;
for (i = 0; i < num_threads; i++) {
ret = pthread_create(md->threads + i, NULL, dump_worker, md);
if (ret)
break;
}
if (ret)
metadump_destroy(md, i + 1);
return ret;
}
static int write_zero(FILE *out, size_t size)
{
static char zero[BLOCK_SIZE];
return fwrite(zero, size, 1, out);
}
static int write_buffers(struct metadump_struct *md, u64 *next)
{
struct meta_cluster_header *header = &md->cluster->header;
struct meta_cluster_item *item;
struct async_work *async;
u64 bytenr = 0;
u32 nritems = 0;
int ret;
int err = 0;
if (list_empty(&md->ordered))
goto out;
/* wait until all buffers are compressed */
while (!err && md->num_items > md->num_ready) {
struct timespec ts = {
.tv_sec = 0,
.tv_nsec = 10000000,
};
pthread_mutex_unlock(&md->mutex);
nanosleep(&ts, NULL);
pthread_mutex_lock(&md->mutex);
err = md->error;
}
if (err) {
fprintf(stderr, "One of the threads errored out %s\n",
strerror(err));
goto out;
}
/* setup and write index block */
list_for_each_entry(async, &md->ordered, ordered) {
item = md->cluster->items + nritems;
item->bytenr = cpu_to_le64(async->start);
item->size = cpu_to_le32(async->bufsize);
nritems++;
}
header->nritems = cpu_to_le32(nritems);
ret = fwrite(md->cluster, BLOCK_SIZE, 1, md->out);
if (ret != 1) {
fprintf(stderr, "Error writing out cluster: %d\n", errno);
return -EIO;
}
/* write buffers */
bytenr += le64_to_cpu(header->bytenr) + BLOCK_SIZE;
while (!list_empty(&md->ordered)) {
async = list_entry(md->ordered.next, struct async_work,
ordered);
list_del_init(&async->ordered);
bytenr += async->bufsize;
if (!err)
ret = fwrite(async->buffer, async->bufsize, 1,
md->out);
if (ret != 1) {
err = -EIO;
ret = 0;
fprintf(stderr, "Error writing out cluster: %d\n",
errno);
}
free(async->buffer);
free(async);
}
/* zero unused space in the last block */
if (!err && bytenr & BLOCK_MASK) {
size_t size = BLOCK_SIZE - (bytenr & BLOCK_MASK);
bytenr += size;
ret = write_zero(md->out, size);
if (ret != 1) {
fprintf(stderr, "Error zeroing out buffer: %d\n",
errno);
err = -EIO;
}
}
out:
*next = bytenr;
return err;
}
static int read_data_extent(struct metadump_struct *md,
struct async_work *async)
{
struct btrfs_root *root = md->root;
u64 bytes_left = async->size;
u64 logical = async->start;
u64 offset = 0;
u64 read_len;
int num_copies;
int cur_mirror;
int ret;
num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, logical,
bytes_left);
/* Try our best to read data, just like read_tree_block() */
for (cur_mirror = 0; cur_mirror < num_copies; cur_mirror++) {
while (bytes_left) {
read_len = bytes_left;
ret = read_extent_data(root,
(char *)(async->buffer + offset),
logical, &read_len, cur_mirror);
if (ret < 0)
break;
offset += read_len;
logical += read_len;
bytes_left -= read_len;
}
}
if (bytes_left)
return -EIO;
return 0;
}
static int get_dev_fd(struct btrfs_root *root)
{
struct btrfs_device *dev;
dev = list_first_entry(&root->fs_info->fs_devices->devices,
struct btrfs_device, dev_list);
return dev->fd;
}
static int flush_pending(struct metadump_struct *md, int done)
{
struct async_work *async = NULL;
struct extent_buffer *eb;
u64 blocksize = md->root->nodesize;
u64 start;
u64 size;
size_t offset;
int ret = 0;
if (md->pending_size) {
async = calloc(1, sizeof(*async));
if (!async)
return -ENOMEM;
async->start = md->pending_start;
async->size = md->pending_size;
async->bufsize = async->size;
async->buffer = malloc(async->bufsize);
if (!async->buffer) {
free(async);
return -ENOMEM;
}
offset = 0;
start = async->start;
size = async->size;
if (md->data) {
ret = read_data_extent(md, async);
if (ret) {
free(async->buffer);
free(async);
return ret;
}
}
/*
* Balance can make the mapping not cover the super block, so
* just copy directly from one of the devices.
*/
if (start == BTRFS_SUPER_INFO_OFFSET) {
int fd = get_dev_fd(md->root);
ret = pread64(fd, async->buffer, size, start);
if (ret < size) {
free(async->buffer);
free(async);
fprintf(stderr, "Error reading superblock\n");
return -EIO;
}
size = 0;
ret = 0;
}
while (!md->data && size > 0) {
u64 this_read = min(blocksize, size);
eb = read_tree_block(md->root, start, this_read, 0);
if (!extent_buffer_uptodate(eb)) {
free(async->buffer);
free(async);
fprintf(stderr,
"Error reading metadata block\n");
return -EIO;
}
copy_buffer(md, async->buffer + offset, eb);
free_extent_buffer(eb);
start += this_read;
offset += this_read;
size -= this_read;
}
md->pending_start = (u64)-1;
md->pending_size = 0;
} else if (!done) {
return 0;
}