-
Notifications
You must be signed in to change notification settings - Fork 0
/
super-intel.c
10544 lines (9336 loc) · 274 KB
/
super-intel.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 - Intel(R) Matrix Storage Manager Support
*
* Copyright (C) 2002-2008 Intel Corporation
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define HAVE_STDINT_H 1
#include "mdadm.h"
#include "mdmon.h"
#include "sha1.h"
#include "platform-intel.h"
#include <values.h>
#include <scsi/sg.h>
#include <ctype.h>
#include <dirent.h>
/* MPB == Metadata Parameter Block */
#define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
#define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
#define MPB_VERSION_RAID0 "1.0.00"
#define MPB_VERSION_RAID1 "1.1.00"
#define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
#define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
#define MPB_VERSION_RAID5 "1.2.02"
#define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
#define MPB_VERSION_CNG "1.2.06"
#define MPB_VERSION_ATTRIBS "1.3.00"
#define MAX_SIGNATURE_LENGTH 32
#define MAX_RAID_SERIAL_LEN 16
/* supports RAID0 */
#define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
/* supports RAID1 */
#define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
/* supports RAID10 */
#define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
/* supports RAID1E */
#define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
/* supports RAID5 */
#define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
/* supports RAID CNG */
#define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
/* supports expanded stripe sizes of 256K, 512K and 1MB */
#define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
/* The OROM Support RST Caching of Volumes */
#define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
/* The OROM supports creating disks greater than 2TB */
#define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
/* The OROM supports Bad Block Management */
#define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
/* THe OROM Supports NVM Caching of Volumes */
#define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
/* The OROM supports creating volumes greater than 2TB */
#define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
/* originally for PMP, now it's wasted b/c. Never use this bit! */
#define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
/* Verify MPB contents against checksum after reading MPB */
#define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
/* Define all supported attributes that have to be accepted by mdadm
*/
#define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
MPB_ATTRIB_2TB | \
MPB_ATTRIB_2TB_DISK | \
MPB_ATTRIB_RAID0 | \
MPB_ATTRIB_RAID1 | \
MPB_ATTRIB_RAID10 | \
MPB_ATTRIB_RAID5 | \
MPB_ATTRIB_EXP_STRIPE_SIZE)
/* Define attributes that are unused but not harmful */
#define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
#define MPB_SECTOR_CNT 2210
#define IMSM_RESERVED_SECTORS 4096
#define NUM_BLOCKS_DIRTY_STRIPE_REGION 2056
#define SECT_PER_MB_SHIFT 11
/* Disk configuration info. */
#define IMSM_MAX_DEVICES 255
struct imsm_disk {
__u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
__u32 total_blocks_lo; /* 0xE8 - 0xEB total blocks lo */
__u32 scsi_id; /* 0xEC - 0xEF scsi ID */
#define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
#define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
#define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
__u32 status; /* 0xF0 - 0xF3 */
__u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
__u32 total_blocks_hi; /* 0xF4 - 0xF5 total blocks hi */
#define IMSM_DISK_FILLERS 3
__u32 filler[IMSM_DISK_FILLERS]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
};
/* map selector for map managment
*/
#define MAP_0 0
#define MAP_1 1
#define MAP_X -1
/* RAID map configuration infos. */
struct imsm_map {
__u32 pba_of_lba0_lo; /* start address of partition */
__u32 blocks_per_member_lo;/* blocks per member */
__u32 num_data_stripes_lo; /* number of data stripes */
__u16 blocks_per_strip;
__u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
#define IMSM_T_STATE_NORMAL 0
#define IMSM_T_STATE_UNINITIALIZED 1
#define IMSM_T_STATE_DEGRADED 2
#define IMSM_T_STATE_FAILED 3
__u8 raid_level;
#define IMSM_T_RAID0 0
#define IMSM_T_RAID1 1
#define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
__u8 num_members; /* number of member disks */
__u8 num_domains; /* number of parity domains */
__u8 failed_disk_num; /* valid only when state is degraded */
__u8 ddf;
__u32 pba_of_lba0_hi;
__u32 blocks_per_member_hi;
__u32 num_data_stripes_hi;
__u32 filler[4]; /* expansion area */
#define IMSM_ORD_REBUILD (1 << 24)
__u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
* top byte contains some flags
*/
} __attribute__ ((packed));
struct imsm_vol {
__u32 curr_migr_unit;
__u32 checkpoint_id; /* id to access curr_migr_unit */
__u8 migr_state; /* Normal or Migrating */
#define MIGR_INIT 0
#define MIGR_REBUILD 1
#define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
#define MIGR_GEN_MIGR 3
#define MIGR_STATE_CHANGE 4
#define MIGR_REPAIR 5
__u8 migr_type; /* Initializing, Rebuilding, ... */
__u8 dirty;
__u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
__u16 verify_errors; /* number of mismatches */
__u16 bad_blocks; /* number of bad blocks during verify */
__u32 filler[4];
struct imsm_map map[1];
/* here comes another one if migr_state */
} __attribute__ ((packed));
struct imsm_dev {
__u8 volume[MAX_RAID_SERIAL_LEN];
__u32 size_low;
__u32 size_high;
#define DEV_BOOTABLE __cpu_to_le32(0x01)
#define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
#define DEV_READ_COALESCING __cpu_to_le32(0x04)
#define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
#define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
#define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
#define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
#define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
#define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
#define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
#define DEV_CLONE_N_GO __cpu_to_le32(0x400)
#define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
#define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
__u32 status; /* Persistent RaidDev status */
__u32 reserved_blocks; /* Reserved blocks at beginning of volume */
__u8 migr_priority;
__u8 num_sub_vols;
__u8 tid;
__u8 cng_master_disk;
__u16 cache_policy;
__u8 cng_state;
__u8 cng_sub_state;
#define IMSM_DEV_FILLERS 10
__u32 filler[IMSM_DEV_FILLERS];
struct imsm_vol vol;
} __attribute__ ((packed));
struct imsm_super {
__u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
__u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
__u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
__u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
__u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
__u32 error_log_size; /* 0x30 - 0x33 in bytes */
__u32 attributes; /* 0x34 - 0x37 */
__u8 num_disks; /* 0x38 Number of configured disks */
__u8 num_raid_devs; /* 0x39 Number of configured volumes */
__u8 error_log_pos; /* 0x3A */
__u8 fill[1]; /* 0x3B */
__u32 cache_size; /* 0x3c - 0x40 in mb */
__u32 orig_family_num; /* 0x40 - 0x43 original family num */
__u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
__u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
#define IMSM_FILLERS 35
__u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
/* here comes imsm_dev[num_raid_devs] */
/* here comes BBM logs */
} __attribute__ ((packed));
#define BBM_LOG_MAX_ENTRIES 254
struct bbm_log_entry {
__u64 defective_block_start;
#define UNREADABLE 0xFFFFFFFF
__u32 spare_block_offset;
__u16 remapped_marked_count;
__u16 disk_ordinal;
} __attribute__ ((__packed__));
struct bbm_log {
__u32 signature; /* 0xABADB10C */
__u32 entry_count;
__u32 reserved_spare_block_count; /* 0 */
__u32 reserved; /* 0xFFFF */
__u64 first_spare_lba;
struct bbm_log_entry mapped_block_entries[BBM_LOG_MAX_ENTRIES];
} __attribute__ ((__packed__));
#ifndef MDASSEMBLE
static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
#endif
#define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
#define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
#define MIGR_REC_BUF_SIZE 512 /* size of migr_record i/o buffer */
#define MIGR_REC_POSITION 512 /* migr_record position offset on disk,
* MIGR_REC_BUF_SIZE <= MIGR_REC_POSITION
*/
#define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
* be recovered using srcMap */
#define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
* already been migrated and must
* be recovered from checkpoint area */
struct migr_record {
__u32 rec_status; /* Status used to determine how to restart
* migration in case it aborts
* in some fashion */
__u32 curr_migr_unit; /* 0..numMigrUnits-1 */
__u32 family_num; /* Family number of MPB
* containing the RaidDev
* that is migrating */
__u32 ascending_migr; /* True if migrating in increasing
* order of lbas */
__u32 blocks_per_unit; /* Num disk blocks per unit of operation */
__u32 dest_depth_per_unit; /* Num member blocks each destMap
* member disk
* advances per unit-of-operation */
__u32 ckpt_area_pba; /* Pba of first block of ckpt copy area */
__u32 dest_1st_member_lba; /* First member lba on first
* stripe of destination */
__u32 num_migr_units; /* Total num migration units-of-op */
__u32 post_migr_vol_cap; /* Size of volume after
* migration completes */
__u32 post_migr_vol_cap_hi; /* Expansion space for LBA64 */
__u32 ckpt_read_disk_num; /* Which member disk in destSubMap[0] the
* migration ckpt record was read from
* (for recovered migrations) */
} __attribute__ ((__packed__));
struct md_list {
/* usage marker:
* 1: load metadata
* 2: metadata does not match
* 4: already checked
*/
int used;
char *devname;
int found;
int container;
dev_t st_rdev;
struct md_list *next;
};
#define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
static __u8 migr_type(struct imsm_dev *dev)
{
if (dev->vol.migr_type == MIGR_VERIFY &&
dev->status & DEV_VERIFY_AND_FIX)
return MIGR_REPAIR;
else
return dev->vol.migr_type;
}
static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
{
/* for compatibility with older oroms convert MIGR_REPAIR, into
* MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
*/
if (migr_type == MIGR_REPAIR) {
dev->vol.migr_type = MIGR_VERIFY;
dev->status |= DEV_VERIFY_AND_FIX;
} else {
dev->vol.migr_type = migr_type;
dev->status &= ~DEV_VERIFY_AND_FIX;
}
}
static unsigned int sector_count(__u32 bytes)
{
return ROUND_UP(bytes, 512) / 512;
}
static unsigned int mpb_sectors(struct imsm_super *mpb)
{
return sector_count(__le32_to_cpu(mpb->mpb_size));
}
struct intel_dev {
struct imsm_dev *dev;
struct intel_dev *next;
unsigned index;
};
struct intel_hba {
enum sys_dev_type type;
char *path;
char *pci_id;
struct intel_hba *next;
};
enum action {
DISK_REMOVE = 1,
DISK_ADD
};
/* internal representation of IMSM metadata */
struct intel_super {
union {
void *buf; /* O_DIRECT buffer for reading/writing metadata */
struct imsm_super *anchor; /* immovable parameters */
};
union {
void *migr_rec_buf; /* buffer for I/O operations */
struct migr_record *migr_rec; /* migration record */
};
int clean_migration_record_by_mdmon; /* when reshape is switched to next
array, it indicates that mdmon is allowed to clean migration
record */
size_t len; /* size of the 'buf' allocation */
void *next_buf; /* for realloc'ing buf from the manager */
size_t next_len;
int updates_pending; /* count of pending updates for mdmon */
int current_vol; /* index of raid device undergoing creation */
unsigned long long create_offset; /* common start for 'current_vol' */
__u32 random; /* random data for seeding new family numbers */
struct intel_dev *devlist;
struct dl {
struct dl *next;
int index;
__u8 serial[MAX_RAID_SERIAL_LEN];
int major, minor;
char *devname;
struct imsm_disk disk;
int fd;
int extent_cnt;
struct extent *e; /* for determining freespace @ create */
int raiddisk; /* slot to fill in autolayout */
enum action action;
} *disks, *current_disk;
struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
active */
struct dl *missing; /* disks removed while we weren't looking */
struct bbm_log *bbm_log;
struct intel_hba *hba; /* device path of the raid controller for this metadata */
const struct imsm_orom *orom; /* platform firmware support */
struct intel_super *next; /* (temp) list for disambiguating family_num */
};
struct intel_disk {
struct imsm_disk disk;
#define IMSM_UNKNOWN_OWNER (-1)
int owner;
struct intel_disk *next;
};
struct extent {
unsigned long long start, size;
};
/* definitions of reshape process types */
enum imsm_reshape_type {
CH_TAKEOVER,
CH_MIGRATION,
CH_ARRAY_SIZE,
};
/* definition of messages passed to imsm_process_update */
enum imsm_update_type {
update_activate_spare,
update_create_array,
update_kill_array,
update_rename_array,
update_add_remove_disk,
update_reshape_container_disks,
update_reshape_migration,
update_takeover,
update_general_migration_checkpoint,
update_size_change,
};
struct imsm_update_activate_spare {
enum imsm_update_type type;
struct dl *dl;
int slot;
int array;
struct imsm_update_activate_spare *next;
};
struct geo_params {
char devnm[32];
char *dev_name;
unsigned long long size;
int level;
int layout;
int chunksize;
int raid_disks;
};
enum takeover_direction {
R10_TO_R0,
R0_TO_R10
};
struct imsm_update_takeover {
enum imsm_update_type type;
int subarray;
enum takeover_direction direction;
};
struct imsm_update_reshape {
enum imsm_update_type type;
int old_raid_disks;
int new_raid_disks;
int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
};
struct imsm_update_reshape_migration {
enum imsm_update_type type;
int old_raid_disks;
int new_raid_disks;
/* fields for array migration changes
*/
int subdev;
int new_level;
int new_layout;
int new_chunksize;
int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
};
struct imsm_update_size_change {
enum imsm_update_type type;
int subdev;
long long new_size;
};
struct imsm_update_general_migration_checkpoint {
enum imsm_update_type type;
__u32 curr_migr_unit;
};
struct disk_info {
__u8 serial[MAX_RAID_SERIAL_LEN];
};
struct imsm_update_create_array {
enum imsm_update_type type;
int dev_idx;
struct imsm_dev dev;
};
struct imsm_update_kill_array {
enum imsm_update_type type;
int dev_idx;
};
struct imsm_update_rename_array {
enum imsm_update_type type;
__u8 name[MAX_RAID_SERIAL_LEN];
int dev_idx;
};
struct imsm_update_add_remove_disk {
enum imsm_update_type type;
};
static const char *_sys_dev_type[] = {
[SYS_DEV_UNKNOWN] = "Unknown",
[SYS_DEV_SAS] = "SAS",
[SYS_DEV_SATA] = "SATA"
};
const char *get_sys_dev_type(enum sys_dev_type type)
{
if (type >= SYS_DEV_MAX)
type = SYS_DEV_UNKNOWN;
return _sys_dev_type[type];
}
static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
{
struct intel_hba *result = xmalloc(sizeof(*result));
result->type = device->type;
result->path = xstrdup(device->path);
result->next = NULL;
if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
result->pci_id++;
return result;
}
static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
{
struct intel_hba *result=NULL;
for (result = hba; result; result = result->next) {
if (result->type == device->type && strcmp(result->path, device->path) == 0)
break;
}
return result;
}
static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device)
{
struct intel_hba *hba;
/* check if disk attached to Intel HBA */
hba = find_intel_hba(super->hba, device);
if (hba != NULL)
return 1;
/* Check if HBA is already attached to super */
if (super->hba == NULL) {
super->hba = alloc_intel_hba(device);
return 1;
} else
/* IMSM metadata disallows to attach disks to multiple
* controllers.
*/
return 2;
}
static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
{
struct sys_dev *list, *elem;
char *disk_path;
if ((list = find_intel_devices()) == NULL)
return 0;
if (fd < 0)
disk_path = (char *) devname;
else
disk_path = diskfd_to_devpath(fd);
if (!disk_path)
return 0;
for (elem = list; elem; elem = elem->next)
if (path_attached_to_hba(disk_path, elem->path))
return elem;
if (disk_path != devname)
free(disk_path);
return NULL;
}
static int find_intel_hba_capability(int fd, struct intel_super *super,
char *devname);
static struct supertype *match_metadata_desc_imsm(char *arg)
{
struct supertype *st;
if (strcmp(arg, "imsm") != 0 &&
strcmp(arg, "default") != 0
)
return NULL;
st = xcalloc(1, sizeof(*st));
st->ss = &super_imsm;
st->max_devs = IMSM_MAX_DEVICES;
st->minor_version = 0;
st->sb = NULL;
return st;
}
#ifndef MDASSEMBLE
static __u8 *get_imsm_version(struct imsm_super *mpb)
{
return &mpb->sig[MPB_SIG_LEN];
}
#endif
/* retrieve a disk directly from the anchor when the anchor is known to be
* up-to-date, currently only at load time
*/
static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
{
if (index >= mpb->num_disks)
return NULL;
return &mpb->disk[index];
}
/* retrieve the disk description based on a index of the disk
* in the sub-array
*/
static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
{
struct dl *d;
for (d = super->disks; d; d = d->next)
if (d->index == index)
return d;
return NULL;
}
/* retrieve a disk from the parsed metadata */
static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
{
struct dl *dl;
dl = get_imsm_dl_disk(super, index);
if (dl)
return &dl->disk;
return NULL;
}
/* generate a checksum directly from the anchor when the anchor is known to be
* up-to-date, currently only at load or write_super after coalescing
*/
static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
{
__u32 end = mpb->mpb_size / sizeof(end);
__u32 *p = (__u32 *) mpb;
__u32 sum = 0;
while (end--) {
sum += __le32_to_cpu(*p);
p++;
}
return sum - __le32_to_cpu(mpb->check_sum);
}
static size_t sizeof_imsm_map(struct imsm_map *map)
{
return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
}
struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
{
/* A device can have 2 maps if it is in the middle of a migration.
* If second_map is:
* MAP_0 - we return the first map
* MAP_1 - we return the second map if it exists, else NULL
* MAP_X - we return the second map if it exists, else the first
*/
struct imsm_map *map = &dev->vol.map[0];
struct imsm_map *map2 = NULL;
if (dev->vol.migr_state)
map2 = (void *)map + sizeof_imsm_map(map);
switch (second_map) {
case MAP_0:
break;
case MAP_1:
map = map2;
break;
case MAP_X:
if (map2)
map = map2;
break;
default:
map = NULL;
}
return map;
}
/* return the size of the device.
* migr_state increases the returned size if map[0] were to be duplicated
*/
static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
{
size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
sizeof_imsm_map(get_imsm_map(dev, MAP_0));
/* migrating means an additional map */
if (dev->vol.migr_state)
size += sizeof_imsm_map(get_imsm_map(dev, MAP_1));
else if (migr_state)
size += sizeof_imsm_map(get_imsm_map(dev, MAP_0));
return size;
}
#ifndef MDASSEMBLE
/* retrieve disk serial number list from a metadata update */
static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
{
void *u = update;
struct disk_info *inf;
inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
sizeof_imsm_dev(&update->dev, 0);
return inf;
}
#endif
static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
{
int offset;
int i;
void *_mpb = mpb;
if (index >= mpb->num_raid_devs)
return NULL;
/* devices start after all disks */
offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
for (i = 0; i <= index; i++)
if (i == index)
return _mpb + offset;
else
offset += sizeof_imsm_dev(_mpb + offset, 0);
return NULL;
}
static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
{
struct intel_dev *dv;
if (index >= super->anchor->num_raid_devs)
return NULL;
for (dv = super->devlist; dv; dv = dv->next)
if (dv->index == index)
return dv->dev;
return NULL;
}
/*
* for second_map:
* == MAP_0 get first map
* == MAP_1 get second map
* == MAP_X than get map according to the current migr_state
*/
static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
int slot,
int second_map)
{
struct imsm_map *map;
map = get_imsm_map(dev, second_map);
/* top byte identifies disk under rebuild */
return __le32_to_cpu(map->disk_ord_tbl[slot]);
}
#define ord_to_idx(ord) (((ord) << 8) >> 8)
static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
{
__u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
return ord_to_idx(ord);
}
static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
{
map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
}
static int get_imsm_disk_slot(struct imsm_map *map, unsigned idx)
{
int slot;
__u32 ord;
for (slot = 0; slot < map->num_members; slot++) {
ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
if (ord_to_idx(ord) == idx)
return slot;
}
return -1;
}
static int get_imsm_raid_level(struct imsm_map *map)
{
if (map->raid_level == 1) {
if (map->num_members == 2)
return 1;
else
return 10;
}
return map->raid_level;
}
static int cmp_extent(const void *av, const void *bv)
{
const struct extent *a = av;
const struct extent *b = bv;
if (a->start < b->start)
return -1;
if (a->start > b->start)
return 1;
return 0;
}
static int count_memberships(struct dl *dl, struct intel_super *super)
{
int memberships = 0;
int i;
for (i = 0; i < super->anchor->num_raid_devs; i++) {
struct imsm_dev *dev = get_imsm_dev(super, i);
struct imsm_map *map = get_imsm_map(dev, MAP_0);
if (get_imsm_disk_slot(map, dl->index) >= 0)
memberships++;
}
return memberships;
}
static __u32 imsm_min_reserved_sectors(struct intel_super *super);
static int split_ull(unsigned long long n, __u32 *lo, __u32 *hi)
{
if (lo == 0 || hi == 0)
return 1;
*lo = __le32_to_cpu((unsigned)n);
*hi = __le32_to_cpu((unsigned)(n >> 32));
return 0;
}
static unsigned long long join_u32(__u32 lo, __u32 hi)
{
return (unsigned long long)__le32_to_cpu(lo) |
(((unsigned long long)__le32_to_cpu(hi)) << 32);
}
static unsigned long long total_blocks(struct imsm_disk *disk)
{
if (disk == NULL)
return 0;
return join_u32(disk->total_blocks_lo, disk->total_blocks_hi);
}
static unsigned long long pba_of_lba0(struct imsm_map *map)
{
if (map == NULL)
return 0;
return join_u32(map->pba_of_lba0_lo, map->pba_of_lba0_hi);
}
static unsigned long long blocks_per_member(struct imsm_map *map)
{
if (map == NULL)
return 0;
return join_u32(map->blocks_per_member_lo, map->blocks_per_member_hi);
}
#ifndef MDASSEMBLE
static unsigned long long num_data_stripes(struct imsm_map *map)
{
if (map == NULL)
return 0;
return join_u32(map->num_data_stripes_lo, map->num_data_stripes_hi);
}
static void set_total_blocks(struct imsm_disk *disk, unsigned long long n)
{
split_ull(n, &disk->total_blocks_lo, &disk->total_blocks_hi);
}
#endif
static void set_pba_of_lba0(struct imsm_map *map, unsigned long long n)
{
split_ull(n, &map->pba_of_lba0_lo, &map->pba_of_lba0_hi);
}
static void set_blocks_per_member(struct imsm_map *map, unsigned long long n)
{
split_ull(n, &map->blocks_per_member_lo, &map->blocks_per_member_hi);
}
static void set_num_data_stripes(struct imsm_map *map, unsigned long long n)
{
split_ull(n, &map->num_data_stripes_lo, &map->num_data_stripes_hi);
}
static struct extent *get_extents(struct intel_super *super, struct dl *dl)
{
/* find a list of used extents on the given physical device */
struct extent *rv, *e;
int i;
int memberships = count_memberships(dl, super);
__u32 reservation;
/* trim the reserved area for spares, so they can join any array
* regardless of whether the OROM has assigned sectors from the
* IMSM_RESERVED_SECTORS region
*/
if (dl->index == -1)
reservation = imsm_min_reserved_sectors(super);
else
reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
rv = xcalloc(sizeof(struct extent), (memberships + 1));
e = rv;
for (i = 0; i < super->anchor->num_raid_devs; i++) {
struct imsm_dev *dev = get_imsm_dev(super, i);
struct imsm_map *map = get_imsm_map(dev, MAP_0);
if (get_imsm_disk_slot(map, dl->index) >= 0) {
e->start = pba_of_lba0(map);
e->size = blocks_per_member(map);
e++;
}
}
qsort(rv, memberships, sizeof(*rv), cmp_extent);
/* determine the start of the metadata
* when no raid devices are defined use the default
* ...otherwise allow the metadata to truncate the value
* as is the case with older versions of imsm
*/
if (memberships) {
struct extent *last = &rv[memberships - 1];
unsigned long long remainder;
remainder = total_blocks(&dl->disk) - (last->start + last->size);
/* round down to 1k block to satisfy precision of the kernel
* 'size' interface
*/
remainder &= ~1UL;
/* make sure remainder is still sane */
if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
remainder = ROUND_UP(super->len, 512) >> 9;
if (reservation > remainder)
reservation = remainder;
}
e->start = total_blocks(&dl->disk) - reservation;
e->size = 0;
return rv;
}
/* try to determine how much space is reserved for metadata from
* the last get_extents() entry, otherwise fallback to the
* default
*/
static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
{
struct extent *e;
int i;
__u32 rv;
/* for spares just return a minimal reservation which will grow
* once the spare is picked up by an array
*/
if (dl->index == -1)
return MPB_SECTOR_CNT;
e = get_extents(super, dl);
if (!e)
return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
/* scroll to last entry */
for (i = 0; e[i].size; i++)
continue;