-
Notifications
You must be signed in to change notification settings - Fork 13
/
chunk-recover.c
2192 lines (1934 loc) · 54.6 KB
/
chunk-recover.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) 2013 FUJITSU LIMITED. 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.
*/
#define _XOPEN_SOURCE 500
#define _GNU_SOURCE
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <uuid/uuid.h>
#include <pthread.h>
#include "kerncompat.h"
#include "list.h"
#include "radix-tree.h"
#include "ctree.h"
#include "extent-cache.h"
#include "disk-io.h"
#include "volumes.h"
#include "transaction.h"
#include "crc32c.h"
#include "utils.h"
#include "version.h"
#include "btrfsck.h"
#include "commands.h"
struct recover_control {
int verbose;
int yes;
u16 csum_size;
u32 sectorsize;
u32 leafsize;
u64 generation;
u64 chunk_root_generation;
struct btrfs_fs_devices *fs_devices;
struct cache_tree chunk;
struct block_group_tree bg;
struct device_extent_tree devext;
struct cache_tree eb_cache;
struct list_head good_chunks;
struct list_head bad_chunks;
struct list_head unrepaired_chunks;
pthread_mutex_t rc_lock;
};
struct extent_record {
struct cache_extent cache;
u64 generation;
u8 csum[BTRFS_CSUM_SIZE];
struct btrfs_device *devices[BTRFS_MAX_MIRRORS];
u64 offsets[BTRFS_MAX_MIRRORS];
int nmirrors;
};
struct device_scan {
struct recover_control *rc;
struct btrfs_device *dev;
int fd;
};
static struct extent_record *btrfs_new_extent_record(struct extent_buffer *eb)
{
struct extent_record *rec;
rec = malloc(sizeof(*rec));
if (!rec) {
fprintf(stderr, "Fail to allocate memory for extent record.\n");
exit(1);
}
memset(rec, 0, sizeof(*rec));
rec->cache.start = btrfs_header_bytenr(eb);
rec->cache.size = eb->len;
rec->generation = btrfs_header_generation(eb);
read_extent_buffer(eb, rec->csum, (unsigned long)btrfs_header_csum(eb),
BTRFS_CSUM_SIZE);
return rec;
}
static int process_extent_buffer(struct cache_tree *eb_cache,
struct extent_buffer *eb,
struct btrfs_device *device, u64 offset)
{
struct extent_record *rec;
struct extent_record *exist;
struct cache_extent *cache;
int ret = 0;
rec = btrfs_new_extent_record(eb);
if (!rec->cache.size)
goto free_out;
again:
cache = lookup_cache_extent(eb_cache,
rec->cache.start,
rec->cache.size);
if (cache) {
exist = container_of(cache, struct extent_record, cache);
if (exist->generation > rec->generation)
goto free_out;
if (exist->generation == rec->generation) {
if (exist->cache.start != rec->cache.start ||
exist->cache.size != rec->cache.size ||
memcmp(exist->csum, rec->csum, BTRFS_CSUM_SIZE)) {
ret = -EEXIST;
} else {
BUG_ON(exist->nmirrors >= BTRFS_MAX_MIRRORS);
exist->devices[exist->nmirrors] = device;
exist->offsets[exist->nmirrors] = offset;
exist->nmirrors++;
}
goto free_out;
}
remove_cache_extent(eb_cache, cache);
free(exist);
goto again;
}
rec->devices[0] = device;
rec->offsets[0] = offset;
rec->nmirrors++;
ret = insert_cache_extent(eb_cache, &rec->cache);
BUG_ON(ret);
out:
return ret;
free_out:
free(rec);
goto out;
}
static void free_extent_record(struct cache_extent *cache)
{
struct extent_record *er;
er = container_of(cache, struct extent_record, cache);
free(er);
}
FREE_EXTENT_CACHE_BASED_TREE(extent_record, free_extent_record);
static struct btrfs_chunk *create_chunk_item(struct chunk_record *record)
{
struct btrfs_chunk *ret;
struct btrfs_stripe *chunk_stripe;
int i;
if (!record || record->num_stripes == 0)
return NULL;
ret = malloc(btrfs_chunk_item_size(record->num_stripes));
if (!ret)
return NULL;
btrfs_set_stack_chunk_length(ret, record->length);
btrfs_set_stack_chunk_owner(ret, record->owner);
btrfs_set_stack_chunk_stripe_len(ret, record->stripe_len);
btrfs_set_stack_chunk_type(ret, record->type_flags);
btrfs_set_stack_chunk_io_align(ret, record->io_align);
btrfs_set_stack_chunk_io_width(ret, record->io_width);
btrfs_set_stack_chunk_sector_size(ret, record->sector_size);
btrfs_set_stack_chunk_num_stripes(ret, record->num_stripes);
btrfs_set_stack_chunk_sub_stripes(ret, record->sub_stripes);
for (i = 0, chunk_stripe = &ret->stripe; i < record->num_stripes;
i++, chunk_stripe++) {
btrfs_set_stack_stripe_devid(chunk_stripe,
record->stripes[i].devid);
btrfs_set_stack_stripe_offset(chunk_stripe,
record->stripes[i].offset);
memcpy(chunk_stripe->dev_uuid, record->stripes[i].dev_uuid,
BTRFS_UUID_SIZE);
}
return ret;
}
static void init_recover_control(struct recover_control *rc, int verbose,
int yes)
{
memset(rc, 0, sizeof(struct recover_control));
cache_tree_init(&rc->chunk);
cache_tree_init(&rc->eb_cache);
block_group_tree_init(&rc->bg);
device_extent_tree_init(&rc->devext);
INIT_LIST_HEAD(&rc->good_chunks);
INIT_LIST_HEAD(&rc->bad_chunks);
INIT_LIST_HEAD(&rc->unrepaired_chunks);
rc->verbose = verbose;
rc->yes = yes;
pthread_mutex_init(&rc->rc_lock, NULL);
}
static void free_recover_control(struct recover_control *rc)
{
free_block_group_tree(&rc->bg);
free_chunk_cache_tree(&rc->chunk);
free_device_extent_tree(&rc->devext);
free_extent_record_tree(&rc->eb_cache);
pthread_mutex_destroy(&rc->rc_lock);
}
static int process_block_group_item(struct block_group_tree *bg_cache,
struct extent_buffer *leaf,
struct btrfs_key *key, int slot)
{
struct block_group_record *rec;
struct block_group_record *exist;
struct cache_extent *cache;
int ret = 0;
rec = btrfs_new_block_group_record(leaf, key, slot);
if (!rec->cache.size)
goto free_out;
again:
cache = lookup_cache_extent(&bg_cache->tree,
rec->cache.start,
rec->cache.size);
if (cache) {
exist = container_of(cache, struct block_group_record, cache);
/*check the generation and replace if needed*/
if (exist->generation > rec->generation)
goto free_out;
if (exist->generation == rec->generation) {
int offset = offsetof(struct block_group_record,
generation);
/*
* According to the current kernel code, the following
* case is impossble, or there is something wrong in
* the kernel code.
*/
if (memcmp(((void *)exist) + offset,
((void *)rec) + offset,
sizeof(*rec) - offset))
ret = -EEXIST;
goto free_out;
}
remove_cache_extent(&bg_cache->tree, cache);
list_del_init(&exist->list);
free(exist);
/*
* We must do seach again to avoid the following cache.
* /--old bg 1--//--old bg 2--/
* /--new bg--/
*/
goto again;
}
ret = insert_block_group_record(bg_cache, rec);
BUG_ON(ret);
out:
return ret;
free_out:
free(rec);
goto out;
}
static int process_chunk_item(struct cache_tree *chunk_cache,
struct extent_buffer *leaf, struct btrfs_key *key,
int slot)
{
struct chunk_record *rec;
struct chunk_record *exist;
struct cache_extent *cache;
int ret = 0;
rec = btrfs_new_chunk_record(leaf, key, slot);
if (!rec->cache.size)
goto free_out;
again:
cache = lookup_cache_extent(chunk_cache, rec->offset, rec->length);
if (cache) {
exist = container_of(cache, struct chunk_record, cache);
if (exist->generation > rec->generation)
goto free_out;
if (exist->generation == rec->generation) {
int num_stripes = rec->num_stripes;
int rec_size = btrfs_chunk_record_size(num_stripes);
int offset = offsetof(struct chunk_record, generation);
if (exist->num_stripes != rec->num_stripes ||
memcmp(((void *)exist) + offset,
((void *)rec) + offset,
rec_size - offset))
ret = -EEXIST;
goto free_out;
}
remove_cache_extent(chunk_cache, cache);
free(exist);
goto again;
}
ret = insert_cache_extent(chunk_cache, &rec->cache);
BUG_ON(ret);
out:
return ret;
free_out:
free(rec);
goto out;
}
static int process_device_extent_item(struct device_extent_tree *devext_cache,
struct extent_buffer *leaf,
struct btrfs_key *key, int slot)
{
struct device_extent_record *rec;
struct device_extent_record *exist;
struct cache_extent *cache;
int ret = 0;
rec = btrfs_new_device_extent_record(leaf, key, slot);
if (!rec->cache.size)
goto free_out;
again:
cache = lookup_cache_extent2(&devext_cache->tree,
rec->cache.objectid,
rec->cache.start,
rec->cache.size);
if (cache) {
exist = container_of(cache, struct device_extent_record, cache);
if (exist->generation > rec->generation)
goto free_out;
if (exist->generation == rec->generation) {
int offset = offsetof(struct device_extent_record,
generation);
if (memcmp(((void *)exist) + offset,
((void *)rec) + offset,
sizeof(*rec) - offset))
ret = -EEXIST;
goto free_out;
}
remove_cache_extent(&devext_cache->tree, cache);
list_del_init(&exist->chunk_list);
list_del_init(&exist->device_list);
free(exist);
goto again;
}
ret = insert_device_extent_record(devext_cache, rec);
BUG_ON(ret);
out:
return ret;
free_out:
free(rec);
goto out;
}
static void print_block_group_info(struct block_group_record *rec, char *prefix)
{
if (prefix)
printf("%s", prefix);
printf("Block Group: start = %llu, len = %llu, flag = %llx\n",
rec->objectid, rec->offset, rec->flags);
}
static void print_block_group_tree(struct block_group_tree *tree)
{
struct cache_extent *cache;
struct block_group_record *rec;
printf("All Block Groups:\n");
for (cache = first_cache_extent(&tree->tree); cache;
cache = next_cache_extent(cache)) {
rec = container_of(cache, struct block_group_record, cache);
print_block_group_info(rec, "\t");
}
printf("\n");
}
static void print_stripe_info(struct stripe *data, char *prefix1, char *prefix2,
int index)
{
if (prefix1)
printf("%s", prefix1);
if (prefix2)
printf("%s", prefix2);
printf("[%2d] Stripe: devid = %llu, offset = %llu\n",
index, data->devid, data->offset);
}
static void print_chunk_self_info(struct chunk_record *rec, char *prefix)
{
int i;
if (prefix)
printf("%s", prefix);
printf("Chunk: start = %llu, len = %llu, type = %llx, num_stripes = %u\n",
rec->offset, rec->length, rec->type_flags, rec->num_stripes);
if (prefix)
printf("%s", prefix);
printf(" Stripes list:\n");
for (i = 0; i < rec->num_stripes; i++)
print_stripe_info(&rec->stripes[i], prefix, " ", i);
}
static void print_chunk_tree(struct cache_tree *tree)
{
struct cache_extent *n;
struct chunk_record *entry;
printf("All Chunks:\n");
for (n = first_cache_extent(tree); n;
n = next_cache_extent(n)) {
entry = container_of(n, struct chunk_record, cache);
print_chunk_self_info(entry, "\t");
}
printf("\n");
}
static void print_device_extent_info(struct device_extent_record *rec,
char *prefix)
{
if (prefix)
printf("%s", prefix);
printf("Device extent: devid = %llu, start = %llu, len = %llu, chunk offset = %llu\n",
rec->objectid, rec->offset, rec->length, rec->chunk_offset);
}
static void print_device_extent_tree(struct device_extent_tree *tree)
{
struct cache_extent *n;
struct device_extent_record *entry;
printf("All Device Extents:\n");
for (n = first_cache_extent(&tree->tree); n;
n = next_cache_extent(n)) {
entry = container_of(n, struct device_extent_record, cache);
print_device_extent_info(entry, "\t");
}
printf("\n");
}
static void print_device_info(struct btrfs_device *device, char *prefix)
{
if (prefix)
printf("%s", prefix);
printf("Device: id = %llu, name = %s\n",
device->devid, device->name);
}
static void print_all_devices(struct list_head *devices)
{
struct btrfs_device *dev;
printf("All Devices:\n");
list_for_each_entry(dev, devices, dev_list)
print_device_info(dev, "\t");
printf("\n");
}
static void print_scan_result(struct recover_control *rc)
{
if (!rc->verbose)
return;
printf("DEVICE SCAN RESULT:\n");
printf("Filesystem Information:\n");
printf("\tsectorsize: %d\n", rc->sectorsize);
printf("\tleafsize: %d\n", rc->leafsize);
printf("\ttree root generation: %llu\n", rc->generation);
printf("\tchunk root generation: %llu\n", rc->chunk_root_generation);
printf("\n");
print_all_devices(&rc->fs_devices->devices);
print_block_group_tree(&rc->bg);
print_chunk_tree(&rc->chunk);
print_device_extent_tree(&rc->devext);
}
static void print_chunk_info(struct chunk_record *chunk, char *prefix)
{
struct device_extent_record *devext;
int i;
print_chunk_self_info(chunk, prefix);
if (prefix)
printf("%s", prefix);
if (chunk->bg_rec)
print_block_group_info(chunk->bg_rec, " ");
else
printf(" No block group.\n");
if (prefix)
printf("%s", prefix);
if (list_empty(&chunk->dextents)) {
printf(" No device extent.\n");
} else {
printf(" Device extent list:\n");
i = 0;
list_for_each_entry(devext, &chunk->dextents, chunk_list) {
if (prefix)
printf("%s", prefix);
printf("%s[%2d]", " ", i);
print_device_extent_info(devext, NULL);
i++;
}
}
}
static void print_check_result(struct recover_control *rc)
{
struct chunk_record *chunk;
struct block_group_record *bg;
struct device_extent_record *devext;
int total = 0;
int good = 0;
int bad = 0;
if (!rc->verbose)
return;
printf("CHECK RESULT:\n");
printf("Healthy Chunks:\n");
list_for_each_entry(chunk, &rc->good_chunks, list) {
print_chunk_info(chunk, " ");
good++;
total++;
}
printf("Bad Chunks:\n");
list_for_each_entry(chunk, &rc->bad_chunks, list) {
print_chunk_info(chunk, " ");
bad++;
total++;
}
printf("\n");
printf("Total Chunks:\t%d\n", total);
printf(" Heathy:\t%d\n", good);
printf(" Bad:\t%d\n", bad);
printf("\n");
printf("Orphan Block Groups:\n");
list_for_each_entry(bg, &rc->bg.block_groups, list)
print_block_group_info(bg, " ");
printf("\n");
printf("Orphan Device Extents:\n");
list_for_each_entry(devext, &rc->devext.no_chunk_orphans, chunk_list)
print_device_extent_info(devext, " ");
}
static int check_chunk_by_metadata(struct recover_control *rc,
struct btrfs_root *root,
struct chunk_record *chunk, int bg_only)
{
int ret;
int i;
int slot;
struct btrfs_path path;
struct btrfs_key key;
struct btrfs_root *dev_root;
struct stripe *stripe;
struct btrfs_dev_extent *dev_extent;
struct btrfs_block_group_item *bg_ptr;
struct extent_buffer *l;
btrfs_init_path(&path);
if (bg_only)
goto bg_check;
dev_root = root->fs_info->dev_root;
for (i = 0; i < chunk->num_stripes; i++) {
stripe = &chunk->stripes[i];
key.objectid = stripe->devid;
key.offset = stripe->offset;
key.type = BTRFS_DEV_EXTENT_KEY;
ret = btrfs_search_slot(NULL, dev_root, &key, &path, 0, 0);
if (ret < 0) {
fprintf(stderr, "Search device extent failed(%d)\n",
ret);
btrfs_release_path(&path);
return ret;
} else if (ret > 0) {
if (rc->verbose)
fprintf(stderr,
"No device extent[%llu, %llu]\n",
stripe->devid, stripe->offset);
btrfs_release_path(&path);
return -ENOENT;
}
l = path.nodes[0];
slot = path.slots[0];
dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
if (chunk->offset !=
btrfs_dev_extent_chunk_offset(l, dev_extent)) {
if (rc->verbose)
fprintf(stderr,
"Device tree unmatch with chunks dev_extent[%llu, %llu], chunk[%llu, %llu]\n",
btrfs_dev_extent_chunk_offset(l,
dev_extent),
btrfs_dev_extent_length(l, dev_extent),
chunk->offset, chunk->length);
btrfs_release_path(&path);
return -ENOENT;
}
btrfs_release_path(&path);
}
bg_check:
key.objectid = chunk->offset;
key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
key.offset = chunk->length;
ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, &path,
0, 0);
if (ret < 0) {
fprintf(stderr, "Search block group failed(%d)\n", ret);
btrfs_release_path(&path);
return ret;
} else if (ret > 0) {
if (rc->verbose)
fprintf(stderr, "No block group[%llu, %llu]\n",
key.objectid, key.offset);
btrfs_release_path(&path);
return -ENOENT;
}
l = path.nodes[0];
slot = path.slots[0];
bg_ptr = btrfs_item_ptr(l, slot, struct btrfs_block_group_item);
if (chunk->type_flags != btrfs_disk_block_group_flags(l, bg_ptr)) {
if (rc->verbose)
fprintf(stderr,
"Chunk[%llu, %llu]'s type(%llu) is differemt with Block Group's type(%llu)\n",
chunk->offset, chunk->length, chunk->type_flags,
btrfs_disk_block_group_flags(l, bg_ptr));
btrfs_release_path(&path);
return -ENOENT;
}
btrfs_release_path(&path);
return 0;
}
static int check_all_chunks_by_metadata(struct recover_control *rc,
struct btrfs_root *root)
{
struct chunk_record *chunk;
struct chunk_record *next;
LIST_HEAD(orphan_chunks);
int ret = 0;
int err;
list_for_each_entry_safe(chunk, next, &rc->good_chunks, list) {
err = check_chunk_by_metadata(rc, root, chunk, 0);
if (err) {
if (err == -ENOENT)
list_move_tail(&chunk->list, &orphan_chunks);
else if (err && !ret)
ret = err;
}
}
list_for_each_entry_safe(chunk, next, &rc->unrepaired_chunks, list) {
err = check_chunk_by_metadata(rc, root, chunk, 1);
if (err == -ENOENT)
list_move_tail(&chunk->list, &orphan_chunks);
else if (err && !ret)
ret = err;
}
list_for_each_entry(chunk, &rc->bad_chunks, list) {
err = check_chunk_by_metadata(rc, root, chunk, 1);
if (err != -ENOENT && !ret)
ret = err ? err : -EINVAL;
}
list_splice(&orphan_chunks, &rc->bad_chunks);
return ret;
}
static int extract_metadata_record(struct recover_control *rc,
struct extent_buffer *leaf)
{
struct btrfs_key key;
int ret = 0;
int i;
u32 nritems;
nritems = btrfs_header_nritems(leaf);
for (i = 0; i < nritems; i++) {
btrfs_item_key_to_cpu(leaf, &key, i);
switch (key.type) {
case BTRFS_BLOCK_GROUP_ITEM_KEY:
pthread_mutex_lock(&rc->rc_lock);
ret = process_block_group_item(&rc->bg, leaf, &key, i);
pthread_mutex_unlock(&rc->rc_lock);
break;
case BTRFS_CHUNK_ITEM_KEY:
pthread_mutex_lock(&rc->rc_lock);
ret = process_chunk_item(&rc->chunk, leaf, &key, i);
pthread_mutex_unlock(&rc->rc_lock);
break;
case BTRFS_DEV_EXTENT_KEY:
pthread_mutex_lock(&rc->rc_lock);
ret = process_device_extent_item(&rc->devext, leaf,
&key, i);
pthread_mutex_unlock(&rc->rc_lock);
break;
}
if (ret)
break;
}
return ret;
}
static inline int is_super_block_address(u64 offset)
{
int i;
for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
if (offset == btrfs_sb_offset(i))
return 1;
}
return 0;
}
static int scan_one_device(void *dev_scan_struct)
{
struct extent_buffer *buf;
u64 bytenr;
int ret = 0;
struct device_scan *dev_scan = (struct device_scan *)dev_scan_struct;
struct recover_control *rc = dev_scan->rc;
struct btrfs_device *device = dev_scan->dev;
int fd = dev_scan->fd;
int oldtype;
ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
if (ret)
return 1;
buf = malloc(sizeof(*buf) + rc->leafsize);
if (!buf)
return -ENOMEM;
buf->len = rc->leafsize;
bytenr = 0;
while (1) {
if (is_super_block_address(bytenr))
bytenr += rc->sectorsize;
if (pread64(fd, buf->data, rc->leafsize, bytenr) <
rc->leafsize)
break;
if (memcmp_extent_buffer(buf, rc->fs_devices->fsid,
btrfs_header_fsid(),
BTRFS_FSID_SIZE)) {
bytenr += rc->sectorsize;
continue;
}
if (verify_tree_block_csum_silent(buf, rc->csum_size)) {
bytenr += rc->sectorsize;
continue;
}
pthread_mutex_lock(&rc->rc_lock);
ret = process_extent_buffer(&rc->eb_cache, buf, device, bytenr);
pthread_mutex_unlock(&rc->rc_lock);
if (ret)
goto out;
if (btrfs_header_level(buf) != 0)
goto next_node;
switch (btrfs_header_owner(buf)) {
case BTRFS_EXTENT_TREE_OBJECTID:
case BTRFS_DEV_TREE_OBJECTID:
/* different tree use different generation */
if (btrfs_header_generation(buf) > rc->generation)
break;
ret = extract_metadata_record(rc, buf);
if (ret)
goto out;
break;
case BTRFS_CHUNK_TREE_OBJECTID:
if (btrfs_header_generation(buf) >
rc->chunk_root_generation)
break;
ret = extract_metadata_record(rc, buf);
if (ret)
goto out;
break;
}
next_node:
bytenr += rc->leafsize;
}
out:
close(fd);
free(buf);
return ret;
}
static int scan_devices(struct recover_control *rc)
{
int ret = 0;
int fd;
struct btrfs_device *dev;
struct device_scan *dev_scans;
pthread_t *t_scans;
int *t_rets;
int devnr = 0;
int devidx = 0;
int cancel_from = 0;
int cancel_to = 0;
int i;
list_for_each_entry(dev, &rc->fs_devices->devices, dev_list)
devnr++;
dev_scans = (struct device_scan *)malloc(sizeof(struct device_scan)
* devnr);
if (!dev_scans)
return -ENOMEM;
t_scans = (pthread_t *)malloc(sizeof(pthread_t) * devnr);
if (!t_scans)
return -ENOMEM;
t_rets = (int *)malloc(sizeof(int) * devnr);
if (!t_rets)
return -ENOMEM;
list_for_each_entry(dev, &rc->fs_devices->devices, dev_list) {
fd = open(dev->name, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Failed to open device %s\n",
dev->name);
ret = 1;
goto out2;
}
dev_scans[devidx].rc = rc;
dev_scans[devidx].dev = dev;
dev_scans[devidx].fd = fd;
ret = pthread_create(&t_scans[devidx], NULL,
(void *)scan_one_device,
(void *)&dev_scans[devidx]);
if (ret) {
cancel_from = 0;
cancel_to = devidx - 1;
goto out1;
}
devidx++;
}
i = 0;
while (i < devidx) {
ret = pthread_join(t_scans[i], (void **)&t_rets[i]);
if (ret || t_rets[i]) {
ret = 1;
cancel_from = i + 1;
cancel_to = devnr - 1;
goto out1;
}
i++;
}
out1:
while (ret && (cancel_from <= cancel_to)) {
pthread_cancel(t_scans[cancel_from]);
cancel_from++;
}
out2:
free(dev_scans);
free(t_scans);
free(t_rets);
return !!ret;
}
static int build_device_map_by_chunk_record(struct btrfs_root *root,
struct chunk_record *chunk)
{
int ret = 0;
int i;
u64 devid;
u8 uuid[BTRFS_UUID_SIZE];
u16 num_stripes;
struct btrfs_mapping_tree *map_tree;
struct map_lookup *map;
struct stripe *stripe;
map_tree = &root->fs_info->mapping_tree;
num_stripes = chunk->num_stripes;
map = malloc(btrfs_map_lookup_size(num_stripes));
if (!map)
return -ENOMEM;
map->ce.start = chunk->offset;
map->ce.size = chunk->length;
map->num_stripes = num_stripes;
map->io_width = chunk->io_width;
map->io_align = chunk->io_align;
map->sector_size = chunk->sector_size;
map->stripe_len = chunk->stripe_len;
map->type = chunk->type_flags;
map->sub_stripes = chunk->sub_stripes;
for (i = 0, stripe = chunk->stripes; i < num_stripes; i++, stripe++) {
devid = stripe->devid;
memcpy(uuid, stripe->dev_uuid, BTRFS_UUID_SIZE);
map->stripes[i].physical = stripe->offset;
map->stripes[i].dev = btrfs_find_device(root, devid,
uuid, NULL);
if (!map->stripes[i].dev) {
kfree(map);
return -EIO;
}
}
ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
return ret;
}
static int build_device_maps_by_chunk_records(struct recover_control *rc,
struct btrfs_root *root)
{
int ret = 0;
struct chunk_record *chunk;
list_for_each_entry(chunk, &rc->good_chunks, list) {
ret = build_device_map_by_chunk_record(root, chunk);
if (ret)
return ret;
}
return ret;
}
static int block_group_remove_all_extent_items(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct block_group_record *bg)
{
struct btrfs_fs_info *fs_info = root->fs_info;
struct btrfs_key key;
struct btrfs_path path;
struct extent_buffer *leaf;
u64 start = bg->objectid;
u64 end = bg->objectid + bg->offset;
u64 old_val;
int nitems;
int ret;
int i;
int del_s, del_nr;
btrfs_init_path(&path);
root = root->fs_info->extent_root;
key.objectid = start;
key.offset = 0;
key.type = BTRFS_EXTENT_ITEM_KEY;
again:
ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
if (ret < 0)
goto err;
else if (ret > 0)
ret = 0;
leaf = path.nodes[0];
nitems = btrfs_header_nritems(leaf);
if (!nitems) {
/* The tree is empty. */
ret = 0;
goto err;
}
if (path.slots[0] >= nitems) {
ret = btrfs_next_leaf(root, &path);
if (ret < 0)
goto err;
if (ret > 0) {
ret = 0;
goto err;
}
leaf = path.nodes[0];
btrfs_item_key_to_cpu(leaf, &key, 0);
if (key.objectid >= end)
goto err;
btrfs_release_path(&path);
goto again;
}
del_nr = 0;
del_s = -1;
for (i = path.slots[0]; i < nitems; i++) {
btrfs_item_key_to_cpu(leaf, &key, i);