This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
super-ddf.c
5241 lines (4795 loc) · 141 KB
/
super-ddf.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) 2006-2014 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]>
*
* Specifications for DDF taken from Common RAID DDF Specification Revision 1.2
* (July 28 2006). Reused by permission of SNIA.
*/
#define HAVE_STDINT_H 1
#include "mdadm.h"
#include "mdmon.h"
#include "sha1.h"
#include <values.h>
#include <stddef.h>
/* a non-official T10 name for creation GUIDs */
static char T10[] = "Linux-MD";
/* DDF timestamps are 1980 based, so we need to add
* second-in-decade-of-seventies to convert to linux timestamps.
* 10 years with 2 leap years.
*/
#define DECADE (3600*24*(365*10+2))
unsigned long crc32(
unsigned long crc,
const unsigned char *buf,
unsigned len);
#define DDF_NOTFOUND (~0U)
#define DDF_CONTAINER (DDF_NOTFOUND-1)
/* Default for safe_mode_delay. Same value as for IMSM.
*/
static const int DDF_SAFE_MODE_DELAY = 4000;
/* The DDF metadata handling.
* DDF metadata lives at the end of the device.
* The last 512 byte block provides an 'anchor' which is used to locate
* the rest of the metadata which usually lives immediately behind the anchor.
*
* Note:
* - all multibyte numeric fields are bigendian.
* - all strings are space padded.
*
*/
typedef struct __be16 {
__u16 _v16;
} be16;
#define be16_eq(x, y) ((x)._v16 == (y)._v16)
#define be16_and(x, y) ((x)._v16 & (y)._v16)
#define be16_or(x, y) ((x)._v16 | (y)._v16)
#define be16_clear(x, y) ((x)._v16 &= ~(y)._v16)
#define be16_set(x, y) ((x)._v16 |= (y)._v16)
typedef struct __be32 {
__u32 _v32;
} be32;
#define be32_eq(x, y) ((x)._v32 == (y)._v32)
typedef struct __be64 {
__u64 _v64;
} be64;
#define be64_eq(x, y) ((x)._v64 == (y)._v64)
#define be16_to_cpu(be) __be16_to_cpu((be)._v16)
static inline be16 cpu_to_be16(__u16 x)
{
be16 be = { ._v16 = __cpu_to_be16(x) };
return be;
}
#define be32_to_cpu(be) __be32_to_cpu((be)._v32)
static inline be32 cpu_to_be32(__u32 x)
{
be32 be = { ._v32 = __cpu_to_be32(x) };
return be;
}
#define be64_to_cpu(be) __be64_to_cpu((be)._v64)
static inline be64 cpu_to_be64(__u64 x)
{
be64 be = { ._v64 = __cpu_to_be64(x) };
return be;
}
/* Primary Raid Level (PRL) */
#define DDF_RAID0 0x00
#define DDF_RAID1 0x01
#define DDF_RAID3 0x03
#define DDF_RAID4 0x04
#define DDF_RAID5 0x05
#define DDF_RAID1E 0x11
#define DDF_JBOD 0x0f
#define DDF_CONCAT 0x1f
#define DDF_RAID5E 0x15
#define DDF_RAID5EE 0x25
#define DDF_RAID6 0x06
/* Raid Level Qualifier (RLQ) */
#define DDF_RAID0_SIMPLE 0x00
#define DDF_RAID1_SIMPLE 0x00 /* just 2 devices in this plex */
#define DDF_RAID1_MULTI 0x01 /* exactly 3 devices in this plex */
#define DDF_RAID3_0 0x00 /* parity in first extent */
#define DDF_RAID3_N 0x01 /* parity in last extent */
#define DDF_RAID4_0 0x00 /* parity in first extent */
#define DDF_RAID4_N 0x01 /* parity in last extent */
/* these apply to raid5e and raid5ee as well */
#define DDF_RAID5_0_RESTART 0x00 /* same as 'right asymmetric' - layout 1 */
#define DDF_RAID6_0_RESTART 0x01 /* raid6 different from raid5 here!!! */
#define DDF_RAID5_N_RESTART 0x02 /* same as 'left asymmetric' - layout 0 */
#define DDF_RAID5_N_CONTINUE 0x03 /* same as 'left symmetric' - layout 2 */
#define DDF_RAID1E_ADJACENT 0x00 /* raid10 nearcopies==2 */
#define DDF_RAID1E_OFFSET 0x01 /* raid10 offsetcopies==2 */
/* Secondary RAID Level (SRL) */
#define DDF_2STRIPED 0x00 /* This is weirder than RAID0 !! */
#define DDF_2MIRRORED 0x01
#define DDF_2CONCAT 0x02
#define DDF_2SPANNED 0x03 /* This is also weird - be careful */
/* Magic numbers */
#define DDF_HEADER_MAGIC cpu_to_be32(0xDE11DE11)
#define DDF_CONTROLLER_MAGIC cpu_to_be32(0xAD111111)
#define DDF_PHYS_RECORDS_MAGIC cpu_to_be32(0x22222222)
#define DDF_PHYS_DATA_MAGIC cpu_to_be32(0x33333333)
#define DDF_VIRT_RECORDS_MAGIC cpu_to_be32(0xDDDDDDDD)
#define DDF_VD_CONF_MAGIC cpu_to_be32(0xEEEEEEEE)
#define DDF_SPARE_ASSIGN_MAGIC cpu_to_be32(0x55555555)
#define DDF_VU_CONF_MAGIC cpu_to_be32(0x88888888)
#define DDF_VENDOR_LOG_MAGIC cpu_to_be32(0x01dBEEF0)
#define DDF_BBM_LOG_MAGIC cpu_to_be32(0xABADB10C)
#define DDF_GUID_LEN 24
#define DDF_REVISION_0 "01.00.00"
#define DDF_REVISION_2 "01.02.00"
struct ddf_header {
be32 magic; /* DDF_HEADER_MAGIC */
be32 crc;
char guid[DDF_GUID_LEN];
char revision[8]; /* 01.02.00 */
be32 seq; /* starts at '1' */
be32 timestamp;
__u8 openflag;
__u8 foreignflag;
__u8 enforcegroups;
__u8 pad0; /* 0xff */
__u8 pad1[12]; /* 12 * 0xff */
/* 64 bytes so far */
__u8 header_ext[32]; /* reserved: fill with 0xff */
be64 primary_lba;
be64 secondary_lba;
__u8 type;
__u8 pad2[3]; /* 0xff */
be32 workspace_len; /* sectors for vendor space -
* at least 32768(sectors) */
be64 workspace_lba;
be16 max_pd_entries; /* one of 15, 63, 255, 1023, 4095 */
be16 max_vd_entries; /* 2^(4,6,8,10,12)-1 : i.e. as above */
be16 max_partitions; /* i.e. max num of configuration
record entries per disk */
be16 config_record_len; /* 1 +ROUNDUP(max_primary_element_entries
*12/512) */
be16 max_primary_element_entries; /* 16, 64, 256, 1024, or 4096 */
__u8 pad3[54]; /* 0xff */
/* 192 bytes so far */
be32 controller_section_offset;
be32 controller_section_length;
be32 phys_section_offset;
be32 phys_section_length;
be32 virt_section_offset;
be32 virt_section_length;
be32 config_section_offset;
be32 config_section_length;
be32 data_section_offset;
be32 data_section_length;
be32 bbm_section_offset;
be32 bbm_section_length;
be32 diag_space_offset;
be32 diag_space_length;
be32 vendor_offset;
be32 vendor_length;
/* 256 bytes so far */
__u8 pad4[256]; /* 0xff */
};
/* type field */
#define DDF_HEADER_ANCHOR 0x00
#define DDF_HEADER_PRIMARY 0x01
#define DDF_HEADER_SECONDARY 0x02
/* The content of the 'controller section' - global scope */
struct ddf_controller_data {
be32 magic; /* DDF_CONTROLLER_MAGIC */
be32 crc;
char guid[DDF_GUID_LEN];
struct controller_type {
be16 vendor_id;
be16 device_id;
be16 sub_vendor_id;
be16 sub_device_id;
} type;
char product_id[16];
__u8 pad[8]; /* 0xff */
__u8 vendor_data[448];
};
/* The content of phys_section - global scope */
struct phys_disk {
be32 magic; /* DDF_PHYS_RECORDS_MAGIC */
be32 crc;
be16 used_pdes; /* This is a counter, not a max - the list
* of used entries may not be dense */
be16 max_pdes;
__u8 pad[52];
struct phys_disk_entry {
char guid[DDF_GUID_LEN];
be32 refnum;
be16 type;
be16 state;
be64 config_size; /* DDF structures must be after here */
char path[18]; /* Another horrible structure really
* but is "used for information
* purposes only" */
__u8 pad[6];
} entries[0];
};
/* phys_disk_entry.type is a bitmap - bigendian remember */
#define DDF_Forced_PD_GUID 1
#define DDF_Active_in_VD 2
#define DDF_Global_Spare 4 /* VD_CONF records are ignored */
#define DDF_Spare 8 /* overrides Global_spare */
#define DDF_Foreign 16
#define DDF_Legacy 32 /* no DDF on this device */
#define DDF_Interface_mask 0xf00
#define DDF_Interface_SCSI 0x100
#define DDF_Interface_SAS 0x200
#define DDF_Interface_SATA 0x300
#define DDF_Interface_FC 0x400
/* phys_disk_entry.state is a bigendian bitmap */
#define DDF_Online 1
#define DDF_Failed 2 /* overrides 1,4,8 */
#define DDF_Rebuilding 4
#define DDF_Transition 8
#define DDF_SMART 16
#define DDF_ReadErrors 32
#define DDF_Missing 64
/* The content of the virt_section global scope */
struct virtual_disk {
be32 magic; /* DDF_VIRT_RECORDS_MAGIC */
be32 crc;
be16 populated_vdes;
be16 max_vdes;
__u8 pad[52];
struct virtual_entry {
char guid[DDF_GUID_LEN];
be16 unit;
__u16 pad0; /* 0xffff */
be16 guid_crc;
be16 type;
__u8 state;
__u8 init_state;
__u8 pad1[14];
char name[16];
} entries[0];
};
/* virtual_entry.type is a bitmap - bigendian */
#define DDF_Shared 1
#define DDF_Enforce_Groups 2
#define DDF_Unicode 4
#define DDF_Owner_Valid 8
/* virtual_entry.state is a bigendian bitmap */
#define DDF_state_mask 0x7
#define DDF_state_optimal 0x0
#define DDF_state_degraded 0x1
#define DDF_state_deleted 0x2
#define DDF_state_missing 0x3
#define DDF_state_failed 0x4
#define DDF_state_part_optimal 0x5
#define DDF_state_morphing 0x8
#define DDF_state_inconsistent 0x10
/* virtual_entry.init_state is a bigendian bitmap */
#define DDF_initstate_mask 0x03
#define DDF_init_not 0x00
#define DDF_init_quick 0x01 /* initialisation is progress.
* i.e. 'state_inconsistent' */
#define DDF_init_full 0x02
#define DDF_access_mask 0xc0
#define DDF_access_rw 0x00
#define DDF_access_ro 0x80
#define DDF_access_blocked 0xc0
/* The content of the config_section - local scope
* It has multiple records each config_record_len sectors
* They can be vd_config or spare_assign
*/
struct vd_config {
be32 magic; /* DDF_VD_CONF_MAGIC */
be32 crc;
char guid[DDF_GUID_LEN];
be32 timestamp;
be32 seqnum;
__u8 pad0[24];
be16 prim_elmnt_count;
__u8 chunk_shift; /* 0 == 512, 1==1024 etc */
__u8 prl;
__u8 rlq;
__u8 sec_elmnt_count;
__u8 sec_elmnt_seq;
__u8 srl;
be64 blocks; /* blocks per component could be different
* on different component devices...(only
* for concat I hope) */
be64 array_blocks; /* blocks in array */
__u8 pad1[8];
be32 spare_refs[8]; /* This is used to detect missing spares.
* As we don't have an interface for that
* the values are ignored.
*/
__u8 cache_pol[8];
__u8 bg_rate;
__u8 pad2[3];
__u8 pad3[52];
__u8 pad4[192];
__u8 v0[32]; /* reserved- 0xff */
__u8 v1[32]; /* reserved- 0xff */
__u8 v2[16]; /* reserved- 0xff */
__u8 v3[16]; /* reserved- 0xff */
__u8 vendor[32];
be32 phys_refnum[0]; /* refnum of each disk in sequence */
/*__u64 lba_offset[0]; LBA offset in each phys. Note extents in a
bvd are always the same size */
};
#define LBA_OFFSET(ddf, vd) ((be64 *) &(vd)->phys_refnum[(ddf)->mppe])
/* vd_config.cache_pol[7] is a bitmap */
#define DDF_cache_writeback 1 /* else writethrough */
#define DDF_cache_wadaptive 2 /* only applies if writeback */
#define DDF_cache_readahead 4
#define DDF_cache_radaptive 8 /* only if doing read-ahead */
#define DDF_cache_ifnobatt 16 /* even to write cache if battery is poor */
#define DDF_cache_wallowed 32 /* enable write caching */
#define DDF_cache_rallowed 64 /* enable read caching */
struct spare_assign {
be32 magic; /* DDF_SPARE_ASSIGN_MAGIC */
be32 crc;
be32 timestamp;
__u8 reserved[7];
__u8 type;
be16 populated; /* SAEs used */
be16 max; /* max SAEs */
__u8 pad[8];
struct spare_assign_entry {
char guid[DDF_GUID_LEN];
be16 secondary_element;
__u8 pad[6];
} spare_ents[0];
};
/* spare_assign.type is a bitmap */
#define DDF_spare_dedicated 0x1 /* else global */
#define DDF_spare_revertible 0x2 /* else committable */
#define DDF_spare_active 0x4 /* else not active */
#define DDF_spare_affinity 0x8 /* enclosure affinity */
/* The data_section contents - local scope */
struct disk_data {
be32 magic; /* DDF_PHYS_DATA_MAGIC */
be32 crc;
char guid[DDF_GUID_LEN];
be32 refnum; /* crc of some magic drive data ... */
__u8 forced_ref; /* set when above was not result of magic */
__u8 forced_guid; /* set if guid was forced rather than magic */
__u8 vendor[32];
__u8 pad[442];
};
/* bbm_section content */
struct bad_block_log {
be32 magic;
be32 crc;
be16 entry_count;
be32 spare_count;
__u8 pad[10];
be64 first_spare;
struct mapped_block {
be64 defective_start;
be32 replacement_start;
be16 remap_count;
__u8 pad[2];
} entries[0];
};
/* Struct for internally holding ddf structures */
/* The DDF structure stored on each device is potentially
* quite different, as some data is global and some is local.
* The global data is:
* - ddf header
* - controller_data
* - Physical disk records
* - Virtual disk records
* The local data is:
* - Configuration records
* - Physical Disk data section
* ( and Bad block and vendor which I don't care about yet).
*
* The local data is parsed into separate lists as it is read
* and reconstructed for writing. This means that we only need
* to make config changes once and they are automatically
* propagated to all devices.
* The global (config and disk data) records are each in a list
* of separate data structures. When writing we find the entry
* or entries applicable to the particular device.
*/
struct ddf_super {
struct ddf_header anchor, primary, secondary;
struct ddf_controller_data controller;
struct ddf_header *active;
struct phys_disk *phys;
struct virtual_disk *virt;
char *conf;
int pdsize, vdsize;
unsigned int max_part, mppe, conf_rec_len;
int currentdev;
int updates_pending;
struct vcl {
union {
char space[512];
struct {
struct vcl *next;
unsigned int vcnum; /* index into ->virt */
/* For an array with a secondary level there are
* multiple vd_config structures, all with the same
* guid but with different sec_elmnt_seq.
* One of these structures is in 'conf' below.
* The others are in other_bvds, not in any
* particular order.
*/
struct vd_config **other_bvds;
__u64 *block_sizes; /* NULL if all the same */
};
};
struct vd_config conf;
} *conflist, *currentconf;
struct dl {
union {
char space[512];
struct {
struct dl *next;
int major, minor;
char *devname;
int fd;
unsigned long long size; /* sectors */
be64 primary_lba; /* sectors */
be64 secondary_lba; /* sectors */
be64 workspace_lba; /* sectors */
int pdnum; /* index in ->phys */
struct spare_assign *spare;
void *mdupdate; /* hold metadata update */
/* These fields used by auto-layout */
int raiddisk; /* slot to fill in autolayout */
__u64 esize;
int displayed;
};
};
struct disk_data disk;
struct vcl *vlist[0]; /* max_part in size */
} *dlist, *add_list;
};
static int load_super_ddf_all(struct supertype *st, int fd,
void **sbp, char *devname);
static int get_svd_state(const struct ddf_super *, const struct vcl *);
static int
validate_geometry_ddf_container(struct supertype *st,
int level, int layout, int raiddisks,
int chunk, unsigned long long size,
unsigned long long data_offset,
char *dev, unsigned long long *freesize,
int verbose);
static int validate_geometry_ddf_bvd(struct supertype *st,
int level, int layout, int raiddisks,
int *chunk, unsigned long long size,
unsigned long long data_offset,
char *dev, unsigned long long *freesize,
int verbose);
static void free_super_ddf(struct supertype *st);
static int all_ff(const char *guid);
static unsigned int get_pd_index_from_refnum(const struct vcl *vc,
be32 refnum, unsigned int nmax,
const struct vd_config **bvd,
unsigned int *idx);
static void getinfo_super_ddf(struct supertype *st, struct mdinfo *info, char *map);
static void uuid_from_ddf_guid(const char *guid, int uuid[4]);
static void uuid_from_super_ddf(struct supertype *st, int uuid[4]);
static void _ddf_array_name(char *name, const struct ddf_super *ddf, int i);
static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info, char *map);
static int init_super_ddf_bvd(struct supertype *st,
mdu_array_info_t *info,
unsigned long long size,
char *name, char *homehost,
int *uuid, unsigned long long data_offset);
#if DEBUG
static void pr_state(struct ddf_super *ddf, const char *msg)
{
unsigned int i;
dprintf("%s: ", msg);
for (i = 0; i < be16_to_cpu(ddf->active->max_vd_entries); i++) {
if (all_ff(ddf->virt->entries[i].guid))
continue;
dprintf_cont("%u(s=%02x i=%02x) ", i,
ddf->virt->entries[i].state,
ddf->virt->entries[i].init_state);
}
dprintf_cont("\n");
}
#else
static void pr_state(const struct ddf_super *ddf, const char *msg) {}
#endif
static void _ddf_set_updates_pending(struct ddf_super *ddf, struct vd_config *vc,
const char *func)
{
if (vc) {
vc->timestamp = cpu_to_be32(time(0)-DECADE);
vc->seqnum = cpu_to_be32(be32_to_cpu(vc->seqnum) + 1);
}
if (ddf->updates_pending)
return;
ddf->updates_pending = 1;
ddf->active->seq = cpu_to_be32((be32_to_cpu(ddf->active->seq)+1));
pr_state(ddf, func);
}
#define ddf_set_updates_pending(x,v) _ddf_set_updates_pending((x), (v), __func__)
static be32 calc_crc(void *buf, int len)
{
/* crcs are always at the same place as in the ddf_header */
struct ddf_header *ddf = buf;
be32 oldcrc = ddf->crc;
__u32 newcrc;
ddf->crc = cpu_to_be32(0xffffffff);
newcrc = crc32(0, buf, len);
ddf->crc = oldcrc;
/* The crc is stored (like everything) bigendian, so convert
* here for simplicity
*/
return cpu_to_be32(newcrc);
}
#define DDF_INVALID_LEVEL 0xff
#define DDF_NO_SECONDARY 0xff
static int err_bad_md_layout(const mdu_array_info_t *array)
{
pr_err("RAID%d layout %x with %d disks is unsupported for DDF\n",
array->level, array->layout, array->raid_disks);
return -1;
}
static int layout_md2ddf(const mdu_array_info_t *array,
struct vd_config *conf)
{
be16 prim_elmnt_count = cpu_to_be16(array->raid_disks);
__u8 prl = DDF_INVALID_LEVEL, rlq = 0;
__u8 sec_elmnt_count = 1;
__u8 srl = DDF_NO_SECONDARY;
switch (array->level) {
case LEVEL_LINEAR:
prl = DDF_CONCAT;
break;
case 0:
rlq = DDF_RAID0_SIMPLE;
prl = DDF_RAID0;
break;
case 1:
switch (array->raid_disks) {
case 2:
rlq = DDF_RAID1_SIMPLE;
break;
case 3:
rlq = DDF_RAID1_MULTI;
break;
default:
return err_bad_md_layout(array);
}
prl = DDF_RAID1;
break;
case 4:
if (array->layout != 0)
return err_bad_md_layout(array);
rlq = DDF_RAID4_N;
prl = DDF_RAID4;
break;
case 5:
switch (array->layout) {
case ALGORITHM_LEFT_ASYMMETRIC:
rlq = DDF_RAID5_N_RESTART;
break;
case ALGORITHM_RIGHT_ASYMMETRIC:
rlq = DDF_RAID5_0_RESTART;
break;
case ALGORITHM_LEFT_SYMMETRIC:
rlq = DDF_RAID5_N_CONTINUE;
break;
case ALGORITHM_RIGHT_SYMMETRIC:
/* not mentioned in standard */
default:
return err_bad_md_layout(array);
}
prl = DDF_RAID5;
break;
case 6:
switch (array->layout) {
case ALGORITHM_ROTATING_N_RESTART:
rlq = DDF_RAID5_N_RESTART;
break;
case ALGORITHM_ROTATING_ZERO_RESTART:
rlq = DDF_RAID6_0_RESTART;
break;
case ALGORITHM_ROTATING_N_CONTINUE:
rlq = DDF_RAID5_N_CONTINUE;
break;
default:
return err_bad_md_layout(array);
}
prl = DDF_RAID6;
break;
case 10:
if (array->raid_disks % 2 == 0 && array->layout == 0x102) {
rlq = DDF_RAID1_SIMPLE;
prim_elmnt_count = cpu_to_be16(2);
sec_elmnt_count = array->raid_disks / 2;
srl = DDF_2SPANNED;
prl = DDF_RAID1;
} else if (array->raid_disks % 3 == 0 &&
array->layout == 0x103) {
rlq = DDF_RAID1_MULTI;
prim_elmnt_count = cpu_to_be16(3);
sec_elmnt_count = array->raid_disks / 3;
srl = DDF_2SPANNED;
prl = DDF_RAID1;
} else if (array->layout == 0x201) {
prl = DDF_RAID1E;
rlq = DDF_RAID1E_OFFSET;
} else if (array->layout == 0x102) {
prl = DDF_RAID1E;
rlq = DDF_RAID1E_ADJACENT;
} else
return err_bad_md_layout(array);
break;
default:
return err_bad_md_layout(array);
}
conf->prl = prl;
conf->prim_elmnt_count = prim_elmnt_count;
conf->rlq = rlq;
conf->srl = srl;
conf->sec_elmnt_count = sec_elmnt_count;
return 0;
}
static int err_bad_ddf_layout(const struct vd_config *conf)
{
pr_err("DDF RAID %u qualifier %u with %u disks is unsupported\n",
conf->prl, conf->rlq, be16_to_cpu(conf->prim_elmnt_count));
return -1;
}
static int layout_ddf2md(const struct vd_config *conf,
mdu_array_info_t *array)
{
int level = LEVEL_UNSUPPORTED;
int layout = 0;
int raiddisks = be16_to_cpu(conf->prim_elmnt_count);
if (conf->sec_elmnt_count > 1) {
/* see also check_secondary() */
if (conf->prl != DDF_RAID1 ||
(conf->srl != DDF_2STRIPED && conf->srl != DDF_2SPANNED)) {
pr_err("Unsupported secondary RAID level %u/%u\n",
conf->prl, conf->srl);
return -1;
}
if (raiddisks == 2 && conf->rlq == DDF_RAID1_SIMPLE)
layout = 0x102;
else if (raiddisks == 3 && conf->rlq == DDF_RAID1_MULTI)
layout = 0x103;
else
return err_bad_ddf_layout(conf);
raiddisks *= conf->sec_elmnt_count;
level = 10;
goto good;
}
switch (conf->prl) {
case DDF_CONCAT:
level = LEVEL_LINEAR;
break;
case DDF_RAID0:
if (conf->rlq != DDF_RAID0_SIMPLE)
return err_bad_ddf_layout(conf);
level = 0;
break;
case DDF_RAID1:
if (!((conf->rlq == DDF_RAID1_SIMPLE && raiddisks == 2) ||
(conf->rlq == DDF_RAID1_MULTI && raiddisks == 3)))
return err_bad_ddf_layout(conf);
level = 1;
break;
case DDF_RAID1E:
if (conf->rlq == DDF_RAID1E_ADJACENT)
layout = 0x102;
else if (conf->rlq == DDF_RAID1E_OFFSET)
layout = 0x201;
else
return err_bad_ddf_layout(conf);
level = 10;
break;
case DDF_RAID4:
if (conf->rlq != DDF_RAID4_N)
return err_bad_ddf_layout(conf);
level = 4;
break;
case DDF_RAID5:
switch (conf->rlq) {
case DDF_RAID5_N_RESTART:
layout = ALGORITHM_LEFT_ASYMMETRIC;
break;
case DDF_RAID5_0_RESTART:
layout = ALGORITHM_RIGHT_ASYMMETRIC;
break;
case DDF_RAID5_N_CONTINUE:
layout = ALGORITHM_LEFT_SYMMETRIC;
break;
default:
return err_bad_ddf_layout(conf);
}
level = 5;
break;
case DDF_RAID6:
switch (conf->rlq) {
case DDF_RAID5_N_RESTART:
layout = ALGORITHM_ROTATING_N_RESTART;
break;
case DDF_RAID6_0_RESTART:
layout = ALGORITHM_ROTATING_ZERO_RESTART;
break;
case DDF_RAID5_N_CONTINUE:
layout = ALGORITHM_ROTATING_N_CONTINUE;
break;
default:
return err_bad_ddf_layout(conf);
}
level = 6;
break;
default:
return err_bad_ddf_layout(conf);
};
good:
array->level = level;
array->layout = layout;
array->raid_disks = raiddisks;
return 0;
}
static int load_ddf_header(int fd, unsigned long long lba,
unsigned long long size,
int type,
struct ddf_header *hdr, struct ddf_header *anchor)
{
/* read a ddf header (primary or secondary) from fd/lba
* and check that it is consistent with anchor
* Need to check:
* magic, crc, guid, rev, and LBA's header_type, and
* everything after header_type must be the same
*/
if (lba >= size-1)
return 0;
if (lseek64(fd, lba<<9, 0) < 0)
return 0;
if (read(fd, hdr, 512) != 512)
return 0;
if (!be32_eq(hdr->magic, DDF_HEADER_MAGIC)) {
pr_err("bad header magic\n");
return 0;
}
if (!be32_eq(calc_crc(hdr, 512), hdr->crc)) {
pr_err("bad CRC\n");
return 0;
}
if (memcmp(anchor->guid, hdr->guid, DDF_GUID_LEN) != 0 ||
memcmp(anchor->revision, hdr->revision, 8) != 0 ||
!be64_eq(anchor->primary_lba, hdr->primary_lba) ||
!be64_eq(anchor->secondary_lba, hdr->secondary_lba) ||
hdr->type != type ||
memcmp(anchor->pad2, hdr->pad2, 512 -
offsetof(struct ddf_header, pad2)) != 0) {
pr_err("header mismatch\n");
return 0;
}
/* Looks good enough to me... */
return 1;
}
static void *load_section(int fd, struct ddf_super *super, void *buf,
be32 offset_be, be32 len_be, int check)
{
unsigned long long offset = be32_to_cpu(offset_be);
unsigned long long len = be32_to_cpu(len_be);
int dofree = (buf == NULL);
if (check)
if (len != 2 && len != 8 && len != 32 &&
len != 128 && len != 512)
return NULL;
if (len > 1024)
return NULL;
if (!buf && posix_memalign(&buf, 512, len<<9) != 0)
buf = NULL;
if (!buf)
return NULL;
if (super->active->type == 1)
offset += be64_to_cpu(super->active->primary_lba);
else
offset += be64_to_cpu(super->active->secondary_lba);
if ((unsigned long long)lseek64(fd, offset<<9, 0) != (offset<<9)) {
if (dofree)
free(buf);
return NULL;
}
if ((unsigned long long)read(fd, buf, len<<9) != (len<<9)) {
if (dofree)
free(buf);
return NULL;
}
return buf;
}
static int load_ddf_headers(int fd, struct ddf_super *super, char *devname)
{
unsigned long long dsize;
get_dev_size(fd, NULL, &dsize);
if (lseek64(fd, dsize-512, 0) < 0) {
if (devname)
pr_err("Cannot seek to anchor block on %s: %s\n",
devname, strerror(errno));
return 1;
}
if (read(fd, &super->anchor, 512) != 512) {
if (devname)
pr_err("Cannot read anchor block on %s: %s\n",
devname, strerror(errno));
return 1;
}
if (!be32_eq(super->anchor.magic, DDF_HEADER_MAGIC)) {
if (devname)
pr_err("no DDF anchor found on %s\n",
devname);
return 2;
}
if (!be32_eq(calc_crc(&super->anchor, 512), super->anchor.crc)) {
if (devname)
pr_err("bad CRC on anchor on %s\n",
devname);
return 2;
}
if (memcmp(super->anchor.revision, DDF_REVISION_0, 8) != 0 &&
memcmp(super->anchor.revision, DDF_REVISION_2, 8) != 0) {
if (devname)
pr_err("can only support super revision %.8s and earlier, not %.8s on %s\n",
DDF_REVISION_2, super->anchor.revision,devname);
return 2;
}
super->active = NULL;
if (load_ddf_header(fd, be64_to_cpu(super->anchor.primary_lba),
dsize >> 9, 1,
&super->primary, &super->anchor) == 0) {
if (devname)
pr_err("Failed to load primary DDF header on %s\n", devname);
} else
super->active = &super->primary;
if (load_ddf_header(fd, be64_to_cpu(super->anchor.secondary_lba),
dsize >> 9, 2,
&super->secondary, &super->anchor)) {
if (super->active == NULL ||
(be32_to_cpu(super->primary.seq)
< be32_to_cpu(super->secondary.seq) &&
!super->secondary.openflag) ||
(be32_to_cpu(super->primary.seq) ==
be32_to_cpu(super->secondary.seq) &&
super->primary.openflag && !super->secondary.openflag))
super->active = &super->secondary;
} else if (devname &&
be64_to_cpu(super->anchor.secondary_lba) != ~(__u64)0)
pr_err("Failed to load secondary DDF header on %s\n",
devname);
if (super->active == NULL)
return 2;
return 0;
}
static int load_ddf_global(int fd, struct ddf_super *super, char *devname)
{
void *ok;
ok = load_section(fd, super, &super->controller,
super->active->controller_section_offset,
super->active->controller_section_length,
0);
super->phys = load_section(fd, super, NULL,
super->active->phys_section_offset,
super->active->phys_section_length,
1);
super->pdsize = be32_to_cpu(super->active->phys_section_length) * 512;
super->virt = load_section(fd, super, NULL,
super->active->virt_section_offset,
super->active->virt_section_length,
1);
super->vdsize = be32_to_cpu(super->active->virt_section_length) * 512;
if (!ok ||
!super->phys ||
!super->virt) {
free(super->phys);
free(super->virt);
super->phys = NULL;
super->virt = NULL;
return 2;
}
super->conflist = NULL;
super->dlist = NULL;
super->max_part = be16_to_cpu(super->active->max_partitions);
super->mppe = be16_to_cpu(super->active->max_primary_element_entries);
super->conf_rec_len = be16_to_cpu(super->active->config_record_len);
return 0;
}
#define DDF_UNUSED_BVD 0xff
static int alloc_other_bvds(const struct ddf_super *ddf, struct vcl *vcl)
{
unsigned int n_vds = vcl->conf.sec_elmnt_count - 1;
unsigned int i, vdsize;
void *p;
if (n_vds == 0) {
vcl->other_bvds = NULL;
return 0;
}
vdsize = ddf->conf_rec_len * 512;
if (posix_memalign(&p, 512, n_vds *
(vdsize + sizeof(struct vd_config *))) != 0)