forked from md-raid-utilities/mdadm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
super1.c
3034 lines (2736 loc) · 82.1 KB
/
super1.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
/*
* mdadm - manage Linux "md" devices aka RAID arrays.
*
* Copyright (C) 2001-2016 Neil Brown <[email protected]>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 02111-1307 USA
*
* Author: Neil Brown
* Email: <[email protected]>
*/
#include <stddef.h>
#include "mdadm.h"
/*
* The version-1 superblock :
* All numeric fields are little-endian.
*
* total size: 256 bytes plus 2 per device.
* 1K allows 384 devices.
*/
struct mdp_superblock_1 {
/* constant array information - 128 bytes */
__u32 magic; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
__u32 major_version; /* 1 */
__u32 feature_map; /* 0 for now */
__u32 pad0; /* always set to 0 when writing */
__u8 set_uuid[16]; /* user-space generated. */
char set_name[32]; /* set and interpreted by user-space */
__u64 ctime; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
__u32 level; /* -4 (multipath), -1 (linear), 0,1,4,5 */
__u32 layout; /* used for raid5, raid6, raid10, and raid0 */
__u64 size; /* used size of component devices, in 512byte sectors */
__u32 chunksize; /* in 512byte sectors */
__u32 raid_disks;
union {
__u32 bitmap_offset; /* sectors after start of superblock that bitmap starts
* NOTE: signed, so bitmap can be before superblock
* only meaningful of feature_map[0] is set.
*/
/* only meaningful when feature_map[MD_FEATURE_PPL] is set */
struct {
__s16 offset; /* sectors from start of superblock that ppl starts */
__u16 size; /* ppl size in sectors */
} ppl;
};
/* These are only valid with feature bit '4' */
__u32 new_level; /* new level we are reshaping to */
__u64 reshape_position; /* next address in array-space for reshape */
__u32 delta_disks; /* change in number of raid_disks */
__u32 new_layout; /* new layout */
__u32 new_chunk; /* new chunk size (sectors) */
__u32 new_offset; /* signed number to add to data_offset in new
* layout. 0 == no-change. This can be
* different on each device in the array.
*/
/* constant this-device information - 64 bytes */
__u64 data_offset; /* sector start of data, often 0 */
__u64 data_size; /* sectors in this device that can be used for data */
__u64 super_offset; /* sector start of this superblock */
union {
__u64 recovery_offset;/* sectors before this offset (from data_offset) have been recovered */
__u64 journal_tail;/* journal tail of journal device (from data_offset) */
};
__u32 dev_number; /* permanent identifier of this device - not role in raid */
__u32 cnt_corrected_read; /* number of read errors that were corrected by re-writing */
__u8 device_uuid[16]; /* user-space setable, ignored by kernel */
__u8 devflags; /* per-device flags. Only one defined...*/
#define WriteMostly1 1 /* mask for writemostly flag in above */
#define FailFast1 2 /* Device should get FailFast requests */
/* bad block log. If there are any bad blocks the feature flag is set.
* if offset and size are non-zero, that space is reserved and available.
*/
__u8 bblog_shift; /* shift from sectors to block size for badblock list */
__u16 bblog_size; /* number of sectors reserved for badblock list */
__u32 bblog_offset; /* sector offset from superblock to bblog, signed */
/* array state information - 64 bytes */
__u64 utime; /* 40 bits second, 24 bits microseconds */
__u64 events; /* incremented when superblock updated */
__u64 resync_offset; /* data before this offset (from data_offset) known to be in sync */
__u32 sb_csum; /* checksum upto dev_roles[max_dev] */
__u32 max_dev; /* size of dev_roles[] array to consider */
__u8 pad3[64-32]; /* set to 0 when writing */
/* device state information. Indexed by dev_number.
* 2 bytes per device
* Note there are no per-device state flags. State information is rolled
* into the 'roles' value. If a device is spare or faulty, then it doesn't
* have a meaningful role.
*/
__u16 dev_roles[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
};
#define MAX_SB_SIZE 4096
/* bitmap super size is 256, but we round up to a sector for alignment */
#define BM_SUPER_SIZE 512
#define MAX_DEVS ((int)(MAX_SB_SIZE - sizeof(struct mdp_superblock_1)) / 2)
#define SUPER1_SIZE (MAX_SB_SIZE + BM_SUPER_SIZE \
+ sizeof(struct misc_dev_info))
struct misc_dev_info {
__u64 device_size;
};
#define MULTIPLE_PPL_AREA_SIZE_SUPER1 (1024 * 1024) /* Size of the whole
* mutliple PPL area
*/
/* feature_map bits */
#define MD_FEATURE_BITMAP_OFFSET 1
#define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
* must be honoured
*/
#define MD_FEATURE_RESHAPE_ACTIVE 4
#define MD_FEATURE_BAD_BLOCKS 8 /* badblock list is not empty */
#define MD_FEATURE_REPLACEMENT 16 /* This device is replacing an
* active device with same 'role'.
* 'recovery_offset' is also set.
*/
#define MD_FEATURE_RESHAPE_BACKWARDS 32 /* Reshape doesn't change number
* of devices, but is going
* backwards anyway.
*/
#define MD_FEATURE_NEW_OFFSET 64 /* new_offset must be honoured */
#define MD_FEATURE_BITMAP_VERSIONED 256 /* bitmap version number checked properly */
#define MD_FEATURE_JOURNAL 512 /* support write journal */
#define MD_FEATURE_PPL 1024 /* support PPL */
#define MD_FEATURE_MUTLIPLE_PPLS 2048 /* support for multiple PPLs */
#define MD_FEATURE_RAID0_LAYOUT 4096 /* layout is meaningful in RAID0 */
#define MD_FEATURE_ALL (MD_FEATURE_BITMAP_OFFSET \
|MD_FEATURE_RECOVERY_OFFSET \
|MD_FEATURE_RESHAPE_ACTIVE \
|MD_FEATURE_BAD_BLOCKS \
|MD_FEATURE_REPLACEMENT \
|MD_FEATURE_RESHAPE_BACKWARDS \
|MD_FEATURE_NEW_OFFSET \
|MD_FEATURE_BITMAP_VERSIONED \
|MD_FEATURE_JOURNAL \
|MD_FEATURE_PPL \
|MD_FEATURE_MULTIPLE_PPLS \
|MD_FEATURE_RAID0_LAYOUT \
)
static int role_from_sb(struct mdp_superblock_1 *sb)
{
unsigned int d;
int role;
d = __le32_to_cpu(sb->dev_number);
if (d < __le32_to_cpu(sb->max_dev))
role = __le16_to_cpu(sb->dev_roles[d]);
else
role = MD_DISK_ROLE_SPARE;
return role;
}
/* return how many bytes are needed for bitmap, for cluster-md each node
* should have it's own bitmap */
static unsigned int calc_bitmap_size(bitmap_super_t *bms, unsigned int boundary)
{
unsigned long long bits, bytes;
bits = bitmap_bits(__le64_to_cpu(bms->sync_size),
__le32_to_cpu(bms->chunksize));
bytes = (bits+7) >> 3;
bytes += sizeof(bitmap_super_t);
bytes = ROUND_UP(bytes, boundary);
return bytes;
}
static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
{
unsigned int disk_csum, csum;
unsigned long long newcsum;
int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
unsigned int *isuper = (unsigned int *)sb;
/* make sure I can count... */
if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
offsetof(struct mdp_superblock_1, utime) != 192 ||
sizeof(struct mdp_superblock_1) != 256) {
fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
}
disk_csum = sb->sb_csum;
sb->sb_csum = 0;
newcsum = 0;
for (; size >= 4; size -= 4) {
newcsum += __le32_to_cpu(*isuper);
isuper++;
}
if (size == 2)
newcsum += __le16_to_cpu(*(unsigned short*) isuper);
csum = (newcsum & 0xffffffff) + (newcsum >> 32);
sb->sb_csum = disk_csum;
return __cpu_to_le32(csum);
}
/*
* Information related to file descriptor used for aligned reads/writes.
* Cache the block size.
*/
struct align_fd {
int fd;
int blk_sz;
};
static void init_afd(struct align_fd *afd, int fd)
{
afd->fd = fd;
if (!get_dev_sector_size(afd->fd, NULL, (unsigned int *)&afd->blk_sz))
afd->blk_sz = 512;
}
static char abuf[4096+4096];
static int aread(struct align_fd *afd, void *buf, int len)
{
/* aligned read.
* On devices with a 4K sector size, we need to read
* the full sector and copy relevant bits into
* the buffer
*/
int bsize, iosize;
char *b;
int n;
bsize = afd->blk_sz;
if (!bsize || bsize > 4096 || len > 4096) {
if (!bsize)
fprintf(stderr, "WARNING - aread() called with invalid block size\n");
return -1;
}
b = ROUND_UP_PTR((char *)abuf, 4096);
for (iosize = 0; iosize < len; iosize += bsize)
;
n = read(afd->fd, b, iosize);
if (n <= 0)
return n;
if (lseek(afd->fd, len - n, 1) < 0) {
pr_err("lseek fails\n");
return -1;
}
if (n > len)
n = len;
memcpy(buf, b, n);
return n;
}
static int awrite(struct align_fd *afd, void *buf, int len)
{
/* aligned write.
* On devices with a 4K sector size, we need to write
* the full sector. We pre-read if the sector is larger
* than the write.
* The address must be sector-aligned.
*/
int bsize, iosize;
char *b;
int n;
bsize = afd->blk_sz;
if (!bsize || bsize > 4096 || len > 4096) {
if (!bsize)
fprintf(stderr, "WARNING - awrite() called with invalid block size\n");
return -1;
}
b = ROUND_UP_PTR((char *)abuf, 4096);
for (iosize = 0; iosize < len ; iosize += bsize)
;
if (len != iosize) {
n = read(afd->fd, b, iosize);
if (n <= 0)
return n;
if (lseek(afd->fd, -n, 1) < 0) {
pr_err("lseek fails\n");
return -1;
}
}
memcpy(b, buf, len);
n = write(afd->fd, b, iosize);
if (n <= 0)
return n;
if (lseek(afd->fd, len - n, 1) < 0) {
pr_err("lseek fails\n");
return -1;
}
return len;
}
static inline unsigned int md_feature_any_ppl_on(__u32 feature_map)
{
return ((__cpu_to_le32(feature_map) &
(MD_FEATURE_PPL | MD_FEATURE_MUTLIPLE_PPLS)));
}
static inline unsigned int choose_ppl_space(int chunk)
{
return (PPL_HEADER_SIZE >> 9) + (chunk > 128*2 ? chunk : 128*2);
}
static void examine_super1(struct supertype *st, char *homehost)
{
struct mdp_superblock_1 *sb = st->sb;
bitmap_super_t *bms = (bitmap_super_t *)(((char *)sb) + MAX_SB_SIZE);
time_t atime;
unsigned int d;
int role;
int delta_extra = 0;
int i;
char *c;
int l = homehost ? strlen(homehost) : 0;
int layout;
unsigned long long sb_offset;
struct mdinfo info;
int inconsistent = 0;
unsigned int expected_csum = 0;
expected_csum = calc_sb_1_csum(sb);
printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
printf(" Version : 1");
sb_offset = __le64_to_cpu(sb->super_offset);
if (sb_offset <= 4)
printf(".1\n");
else if (sb_offset <= 8)
printf(".2\n");
else
printf(".0\n");
printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
printf(" Array UUID : ");
for (i = 0; i < 16; i++) {
if ((i & 3) == 0 && i != 0)
printf(":");
printf("%02x", sb->set_uuid[i]);
}
printf("\n");
printf(" Name : %.32s", sb->set_name);
if (l > 0 && l < 32 &&
sb->set_name[l] == ':' &&
strncmp(sb->set_name, homehost, l) == 0)
printf(" (local to host %s)", homehost);
printf("\n");
if (bms->nodes > 0 &&
(__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET))
printf(" Cluster Name : %-64s\n", bms->cluster_name);
atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
printf(" Creation Time : %.24s\n", ctime(&atime));
c=map_num(pers, __le32_to_cpu(sb->level));
printf(" Raid Level : %s\n", c?c:"-unknown-");
printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
printf("\n");
printf(" Avail Dev Size : %llu sectors%s\n",
(unsigned long long)__le64_to_cpu(sb->data_size),
human_size(__le64_to_cpu(sb->data_size)<<9));
if (__le32_to_cpu(sb->level) > 0) {
int ddsks = 0, ddsks_denom = 1;
switch(__le32_to_cpu(sb->level)) {
case 1: ddsks=1;break;
case 4:
case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
case 10:
layout = __le32_to_cpu(sb->layout);
ddsks = __le32_to_cpu(sb->raid_disks);
ddsks_denom = (layout&255) * ((layout>>8)&255);
}
if (ddsks) {
long long asize = __le64_to_cpu(sb->size);
asize = (asize << 9) * ddsks / ddsks_denom;
printf(" Array Size : %llu KiB%s\n",
asize >> 10, human_size(asize));
}
if (sb->size != sb->data_size)
printf(" Used Dev Size : %llu sectors%s\n",
(unsigned long long)__le64_to_cpu(sb->size),
human_size(__le64_to_cpu(sb->size)<<9));
}
if (sb->data_offset)
printf(" Data Offset : %llu sectors\n",
(unsigned long long)__le64_to_cpu(sb->data_offset));
if (sb->new_offset &&
(__le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) {
unsigned long long offset = __le64_to_cpu(sb->data_offset);
offset += (signed)(int32_t)__le32_to_cpu(sb->new_offset);
printf(" New Offset : %llu sectors\n", offset);
}
printf(" Super Offset : %llu sectors\n",
(unsigned long long)__le64_to_cpu(sb->super_offset));
if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
printf("Recovery Offset : %llu sectors\n",
(unsigned long long)__le64_to_cpu(sb->recovery_offset));
st->ss->getinfo_super(st, &info, NULL);
if (info.space_after != 1 &&
!(__le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) {
printf(" Unused Space : before=%llu sectors, ",
info.space_before);
if (info.space_after < INT64_MAX)
printf("after=%llu sectors\n", info.space_after);
else
printf("after=-%llu sectors DEVICE TOO SMALL\n",
UINT64_MAX - info.space_after);
}
printf(" State : %s%s\n",
(__le64_to_cpu(sb->resync_offset) + 1) ? "active":"clean",
(info.space_after > INT64_MAX) ? " TRUNCATED DEVICE" : "");
printf(" Device UUID : ");
for (i = 0; i < 16; i++) {
if ((i & 3)==0 && i != 0)
printf(":");
printf("%02x", sb->device_uuid[i]);
}
printf("\n");
printf("\n");
if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
printf("Internal Bitmap : %ld sectors from superblock\n",
(long)(int32_t)__le32_to_cpu(sb->bitmap_offset));
} else if (md_feature_any_ppl_on(sb->feature_map)) {
printf(" PPL : %u sectors at offset %d sectors from superblock\n",
__le16_to_cpu(sb->ppl.size),
__le16_to_cpu(sb->ppl.offset));
}
if (sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE)) {
printf(" Reshape pos'n : %llu%s\n", (unsigned long long)
__le64_to_cpu(sb->reshape_position)/2,
human_size(__le64_to_cpu(sb->reshape_position)<<9));
if (__le32_to_cpu(sb->delta_disks)) {
printf(" Delta Devices : %d",
__le32_to_cpu(sb->delta_disks));
printf(" (%d->%d)\n",
__le32_to_cpu(sb->raid_disks) -
__le32_to_cpu(sb->delta_disks),
__le32_to_cpu(sb->raid_disks));
if ((int)__le32_to_cpu(sb->delta_disks) < 0)
delta_extra = -__le32_to_cpu(sb->delta_disks);
}
if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
c = map_num(pers, __le32_to_cpu(sb->new_level));
printf(" New Level : %s\n", c?c:"-unknown-");
}
if (__le32_to_cpu(sb->new_layout) !=
__le32_to_cpu(sb->layout)) {
if (__le32_to_cpu(sb->level) == 5) {
c = map_num(r5layout,
__le32_to_cpu(sb->new_layout));
printf(" New Layout : %s\n", c?c:"-unknown-");
}
if (__le32_to_cpu(sb->level) == 6) {
c = map_num(r6layout,
__le32_to_cpu(sb->new_layout));
printf(" New Layout : %s\n", c?c:"-unknown-");
}
if (__le32_to_cpu(sb->level) == 10) {
printf(" New Layout :");
print_r10_layout(__le32_to_cpu(sb->new_layout));
printf("\n");
}
}
if (__le32_to_cpu(sb->new_chunk) !=
__le32_to_cpu(sb->chunksize))
printf(" New Chunksize : %dK\n",
__le32_to_cpu(sb->new_chunk)/2);
printf("\n");
}
if (sb->devflags) {
printf(" Flags :");
if (sb->devflags & WriteMostly1)
printf(" write-mostly");
if (sb->devflags & FailFast1)
printf(" failfast");
printf("\n");
}
atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
printf(" Update Time : %.24s\n", ctime(&atime));
if (sb->bblog_size && sb->bblog_offset) {
printf(" Bad Block Log : %d entries available at offset %ld sectors",
__le16_to_cpu(sb->bblog_size)*512/8,
(long)(int32_t)__le32_to_cpu(sb->bblog_offset));
if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
printf(" - bad blocks present.");
printf("\n");
}
if (expected_csum == sb->sb_csum)
printf(" Checksum : %x - correct\n",
__le32_to_cpu(sb->sb_csum));
else
printf(" Checksum : %x - expected %x\n",
__le32_to_cpu(sb->sb_csum),
__le32_to_cpu(expected_csum));
printf(" Events : %llu\n",
(unsigned long long)__le64_to_cpu(sb->events));
printf("\n");
if (__le32_to_cpu(sb->level) == 0 &&
(sb->feature_map & __cpu_to_le32(MD_FEATURE_RAID0_LAYOUT))) {
c = map_num(r0layout, __le32_to_cpu(sb->layout));
printf(" Layout : %s\n", c?c:"-unknown-");
}
if (__le32_to_cpu(sb->level) == 5) {
c = map_num(r5layout, __le32_to_cpu(sb->layout));
printf(" Layout : %s\n", c?c:"-unknown-");
}
if (__le32_to_cpu(sb->level) == 6) {
c = map_num(r6layout, __le32_to_cpu(sb->layout));
printf(" Layout : %s\n", c?c:"-unknown-");
}
if (__le32_to_cpu(sb->level) == 10) {
int lo = __le32_to_cpu(sb->layout);
printf(" Layout :");
print_r10_layout(lo);
printf("\n");
}
switch(__le32_to_cpu(sb->level)) {
case 0:
case 4:
case 5:
case 6:
case 10:
printf(" Chunk Size : %dK\n",
__le32_to_cpu(sb->chunksize)/2);
break;
case -1:
printf(" Rounding : %dK\n",
__le32_to_cpu(sb->chunksize)/2);
break;
default:
break;
}
printf("\n");
printf(" Device Role : ");
role = role_from_sb(sb);
if (role >= MD_DISK_ROLE_FAULTY)
printf("spare\n");
else if (role == MD_DISK_ROLE_JOURNAL)
printf("Journal\n");
else if (sb->feature_map & __cpu_to_le32(MD_FEATURE_REPLACEMENT))
printf("Replacement device %d\n", role);
else
printf("Active device %d\n", role);
printf(" Array State : ");
for (d = 0; d < __le32_to_cpu(sb->raid_disks) + delta_extra; d++) {
int cnt = 0;
unsigned int i;
for (i = 0; i < __le32_to_cpu(sb->max_dev); i++) {
unsigned int role = __le16_to_cpu(sb->dev_roles[i]);
if (role == d)
cnt++;
}
if (cnt == 2 && __le32_to_cpu(sb->level) > 0)
printf("R");
else if (cnt == 1)
printf("A");
else if (cnt == 0)
printf(".");
else {
printf("?");
inconsistent = 1;
}
}
printf(" ('A' == active, '.' == missing, 'R' == replacing)");
printf("\n");
for (d = 0; d < __le32_to_cpu(sb->max_dev); d++) {
unsigned int r = __le16_to_cpu(sb->dev_roles[d]);
if (r <= MD_DISK_ROLE_MAX &&
r > __le32_to_cpu(sb->raid_disks) + delta_extra)
inconsistent = 1;
}
if (inconsistent) {
printf("WARNING Array state is inconsistent - each number should appear only once\n");
for (d = 0; d < __le32_to_cpu(sb->max_dev); d++)
if (__le16_to_cpu(sb->dev_roles[d]) >=
MD_DISK_ROLE_FAULTY)
printf(" %d:-", d);
else
printf(" %d:%d", d,
__le16_to_cpu(sb->dev_roles[d]));
printf("\n");
}
}
static void brief_examine_super1(struct supertype *st, int verbose)
{
struct mdp_superblock_1 *sb = st->sb;
int i;
unsigned long long sb_offset;
char *nm;
char *c = map_num(pers, __le32_to_cpu(sb->level));
nm = strchr(sb->set_name, ':');
if (nm)
nm++;
else if (sb->set_name[0])
nm = sb->set_name;
else
nm = NULL;
printf("ARRAY ");
if (nm) {
printf(DEV_MD_DIR "%s", nm);
putchar(' ');
}
if (verbose && c)
printf(" level=%s", c);
sb_offset = __le64_to_cpu(sb->super_offset);
if (sb_offset <= 4)
printf(" metadata=1.1 ");
else if (sb_offset <= 8)
printf(" metadata=1.2 ");
else
printf(" metadata=1.0 ");
if (verbose)
printf("num-devices=%d ", __le32_to_cpu(sb->raid_disks));
printf("UUID=");
for (i = 0; i < 16; i++) {
if ((i & 3)==0 && i != 0)
printf(":");
printf("%02x", sb->set_uuid[i]);
}
printf("\n");
}
static void export_examine_super1(struct supertype *st)
{
struct mdp_superblock_1 *sb = st->sb;
int i;
int len = 32;
int layout;
printf("MD_LEVEL=%s\n", map_num_s(pers, __le32_to_cpu(sb->level)));
printf("MD_DEVICES=%d\n", __le32_to_cpu(sb->raid_disks));
for (i = 0; i < 32; i++)
if (sb->set_name[i] == '\n' || sb->set_name[i] == '\0') {
len = i;
break;
}
if (len)
printf("MD_NAME=%.*s\n", len, sb->set_name);
if (__le32_to_cpu(sb->level) > 0) {
int ddsks = 0, ddsks_denom = 1;
switch(__le32_to_cpu(sb->level)) {
case 1:
ddsks = 1;
break;
case 4:
case 5:
ddsks = __le32_to_cpu(sb->raid_disks)-1;
break;
case 6:
ddsks = __le32_to_cpu(sb->raid_disks)-2;
break;
case 10:
layout = __le32_to_cpu(sb->layout);
ddsks = __le32_to_cpu(sb->raid_disks);
ddsks_denom = (layout&255) * ((layout>>8)&255);
}
if (ddsks) {
long long asize = __le64_to_cpu(sb->size);
asize = (asize << 9) * ddsks / ddsks_denom;
printf("MD_ARRAY_SIZE=%s\n",
human_size_brief(asize, JEDEC));
}
}
printf("MD_UUID=");
for (i = 0; i < 16; i++) {
if ((i & 3) == 0 && i != 0)
printf(":");
printf("%02x", sb->set_uuid[i]);
}
printf("\n");
printf("MD_UPDATE_TIME=%llu\n",
__le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL);
printf("MD_DEV_UUID=");
for (i = 0; i < 16; i++) {
if ((i & 3) == 0 && i != 0)
printf(":");
printf("%02x", sb->device_uuid[i]);
}
printf("\n");
printf("MD_EVENTS=%llu\n",
(unsigned long long)__le64_to_cpu(sb->events));
}
static int copy_metadata1(struct supertype *st, int from, int to)
{
/* Read superblock. If it looks good, write it out.
* Then if a bitmap is present, copy that.
* And if a bad-block-list is present, copy that too.
*/
void *buf;
unsigned long long dsize, sb_offset;
const int bufsize = 4*1024;
struct mdp_superblock_1 super, *sb;
if (posix_memalign(&buf, 4096, bufsize) != 0)
return 1;
if (!get_dev_size(from, NULL, &dsize))
goto err;
dsize >>= 9;
if (dsize < 24)
goto err;
switch(st->minor_version) {
case 0:
sb_offset = dsize;
sb_offset -= 8*2;
sb_offset &= ~(4*2-1);
break;
case 1:
sb_offset = 0;
break;
case 2:
sb_offset = 4*2;
break;
default:
goto err;
}
if (lseek64(from, sb_offset << 9, 0) < 0LL)
goto err;
if (read(from, buf, bufsize) != bufsize)
goto err;
sb = buf;
super = *sb; // save most of sb for when we reuse buf
if (__le32_to_cpu(super.magic) != MD_SB_MAGIC ||
__le32_to_cpu(super.major_version) != 1 ||
__le64_to_cpu(super.super_offset) != sb_offset ||
calc_sb_1_csum(sb) != super.sb_csum)
goto err;
if (lseek64(to, sb_offset << 9, 0) < 0LL)
goto err;
if (write(to, buf, bufsize) != bufsize)
goto err;
if (super.feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET)) {
unsigned long long bitmap_offset = sb_offset;
int bytes = 4096; // just an estimate.
int written = 0;
struct align_fd afrom, ato;
init_afd(&afrom, from);
init_afd(&ato, to);
bitmap_offset += (int32_t)__le32_to_cpu(super.bitmap_offset);
if (lseek64(from, bitmap_offset<<9, 0) < 0)
goto err;
if (lseek64(to, bitmap_offset<<9, 0) < 0)
goto err;
for (written = 0; written < bytes ; ) {
int n = bytes - written;
if (n > 4096)
n = 4096;
if (aread(&afrom, buf, n) != n)
goto err;
if (written == 0) {
/* have the header, can calculate
* correct bitmap bytes */
bitmap_super_t *bms;
bms = (void *)buf;
bytes = calc_bitmap_size(bms, 512);
if (n > bytes)
n = bytes;
}
if (awrite(&ato, buf, n) != n)
goto err;
written += n;
}
}
if (super.bblog_size != 0 &&
__le16_to_cpu(super.bblog_size) <= 100 &&
super.bblog_offset != 0 &&
(super.feature_map & __le32_to_cpu(MD_FEATURE_BAD_BLOCKS))) {
/* There is a bad block log */
unsigned long long bb_offset = sb_offset;
int bytes = __le16_to_cpu(super.bblog_size) * 512;
int written = 0;
struct align_fd afrom, ato;
init_afd(&afrom, from);
init_afd(&ato, to);
bb_offset += (int32_t)__le32_to_cpu(super.bblog_offset);
if (lseek64(from, bb_offset<<9, 0) < 0)
goto err;
if (lseek64(to, bb_offset<<9, 0) < 0)
goto err;
for (written = 0; written < bytes ; ) {
int n = bytes - written;
if (n > 4096)
n = 4096;
if (aread(&afrom, buf, n) != n)
goto err;
if (awrite(&ato, buf, n) != n)
goto err;
written += n;
}
}
free(buf);
return 0;
err:
free(buf);
return 1;
}
static void detail_super1(struct supertype *st, char *homehost, char *subarray)
{
struct mdp_superblock_1 *sb = st->sb;
bitmap_super_t *bms = (bitmap_super_t *)(((char *)sb) + MAX_SB_SIZE);
int i;
int l = homehost ? strlen(homehost) : 0;
printf(" Name : %.32s", sb->set_name);
if (l > 0 && l < 32 && sb->set_name[l] == ':' &&
strncmp(sb->set_name, homehost, l) == 0)
printf(" (local to host %s)", homehost);
if (bms->nodes > 0 &&
(__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET))
printf("\n Cluster Name : %-64s", bms->cluster_name);
printf("\n UUID : ");
for (i = 0; i < 16; i++) {
if ((i & 3) == 0 && i != 0)
printf(":");
printf("%02x", sb->set_uuid[i]);
}
printf("\n Events : %llu\n\n",
(unsigned long long)__le64_to_cpu(sb->events));
}
static void brief_detail_super1(struct supertype *st, char *subarray)
{
struct mdp_superblock_1 *sb = st->sb;
int i;
printf(" UUID=");
for (i = 0; i < 16; i++) {
if ((i & 3) == 0 && i != 0)
printf(":");
printf("%02x", sb->set_uuid[i]);
}
}
static void export_detail_super1(struct supertype *st)
{
struct mdp_superblock_1 *sb = st->sb;
int i;
int len = 32;
for (i = 0; i < 32; i++)
if (sb->set_name[i] == '\n' || sb->set_name[i] == '\0') {
len = i;
break;
}
if (len)
printf("MD_NAME=%.*s\n", len, sb->set_name);
}
static int examine_badblocks_super1(struct supertype *st, int fd, char *devname)
{
struct mdp_superblock_1 *sb = st->sb;
unsigned long long offset;
int size;
__u64 *bbl, *bbp;
int i;
if (!sb->bblog_size || __le16_to_cpu(sb->bblog_size) > 100 ||
!sb->bblog_offset){
printf("No bad-blocks list configured on %s\n", devname);
return 0;
}
if ((sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS)) == 0) {
printf("Bad-blocks list is empty in %s\n", devname);
return 0;
}
size = __le16_to_cpu(sb->bblog_size)* 512;
if (posix_memalign((void **)&bbl, 4096, size) != 0) {
pr_err("could not allocate badblocks list\n");
return 0;
}
offset = __le64_to_cpu(sb->super_offset) +
(int)__le32_to_cpu(sb->bblog_offset);
offset <<= 9;
if (lseek64(fd, offset, 0) < 0) {
pr_err("Cannot seek to bad-blocks list\n");
free(bbl);
return 1;
}
if (read(fd, bbl, size) != size) {
pr_err("Cannot read bad-blocks list\n");
free(bbl);
return 1;
}
/* 64bits per entry. 10 bits is block-count, 54 bits is block
* offset. Blocks are sectors unless bblog->shift makes them bigger
*/
bbp = (__u64*)bbl;
printf("Bad-blocks on %s:\n", devname);
for (i = 0; i < size/8; i++, bbp++) {
__u64 bb = __le64_to_cpu(*bbp);
int count = bb & 0x3ff;
unsigned long long sector = bb >> 10;
if (bb + 1 == 0)
break;
sector <<= sb->bblog_shift;
count <<= sb->bblog_shift;
printf("%20llu for %d sectors\n", sector, count);
}
free(bbl);
return 0;
}
static int match_home1(struct supertype *st, char *homehost)
{
struct mdp_superblock_1 *sb = st->sb;
int l = homehost ? strlen(homehost) : 0;
return (l > 0 && l < 32 && sb->set_name[l] == ':' &&
strncmp(sb->set_name, homehost, l) == 0);
}
static void uuid_from_super1(struct supertype *st, int uuid[4])
{
struct mdp_superblock_1 *super = st->sb;
char *cuuid = (char *)uuid;
int i;
for (i = 0; i < 16; i++)
cuuid[i] = super->set_uuid[i];
}
static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
{
struct mdp_superblock_1 *sb = st->sb;
struct bitmap_super_s *bsb = (void *)(((char *)sb) + MAX_SB_SIZE);
struct misc_dev_info *misc =
(void *)(((char *)sb) + MAX_SB_SIZE+BM_SUPER_SIZE);
int working = 0;
unsigned int i;
unsigned int role;
unsigned int map_disks = info->array.raid_disks;
unsigned long long super_offset;
unsigned long long data_size;
memset(info, 0, sizeof(*info));
info->array.major_version = 1;
info->array.minor_version = st->minor_version;
info->array.patch_version = 0;
info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
info->array.level = __le32_to_cpu(sb->level);
info->array.layout = __le32_to_cpu(sb->layout);
info->array.md_minor = -1;
info->array.ctime = __le64_to_cpu(sb->ctime);
info->array.utime = __le64_to_cpu(sb->utime);
info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
info->array.state =
(__le64_to_cpu(sb->resync_offset) == MaxSector) ? 1 : 0;