summaryrefslogtreecommitdiffstats
path: root/super-intel.c
blob: 948c6b30e8b9bc0e60e5ab6b35c724fcf2d81022 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
/*
 * mdadm - Intel(R) Matrix Storage Manager Support
 *
 * Copyright (C) 2002-2007 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.
 */

#include "mdadm.h"
#include <values.h>
#include <scsi/sg.h>
#include <ctype.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_RAID5 "1.2.02"
#define MAX_SIGNATURE_LENGTH  32
#define MAX_RAID_SERIAL_LEN   16

/* 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;		 /* 0xE8 - 0xEB total blocks */
	__u32 scsi_id;			 /* 0xEC - 0xEF scsi ID */
	__u32 status;			 /* 0xF0 - 0xF3 */
#define SPARE_DISK      0x01  /* Spare */
#define CONFIGURED_DISK 0x02  /* Member of some RaidDev */
#define FAILED_DISK     0x04  /* Permanent failure */
#define USABLE_DISK     0x08  /* Fully usable unless FAILED_DISK is set */

#define	IMSM_DISK_FILLERS	5
	__u32 filler[IMSM_DISK_FILLERS]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */
};

/* RAID map configuration infos. */
struct imsm_map {
	__u32 pba_of_lba0;	/* start address of partition */
	__u32 blocks_per_member;/* blocks per member */
	__u32 num_data_stripes;	/* 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 /* FIXME: is this correct? */
#define IMSM_T_STATE_FAILED 3 /* FIXME: is this correct? */
	__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  reserved[3];
	__u32 filler[7];	/* expansion area */
	__u32 disk_ord_tbl[1];	/* disk_ord_tbl[num_members],
				   top byte special */
} __attribute__ ((packed));

struct imsm_vol {
	__u32 reserved[2];
	__u8  migr_state;	/* Normal or Migrating */
	__u8  migr_type;	/* Initializing, Rebuilding, ... */
	__u8  dirty;
	__u8  fill[1];
	__u32 filler[5];
	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;
	__u32 status;	/* Persistent RaidDev status */
	__u32 reserved_blocks; /* Reserved blocks at beginning of volume */
#define IMSM_DEV_FILLERS 12
	__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 reserved[2];		/* 0x30 - 0x37 */
	__u8 num_disks;			/* 0x38 Number of configured disks */
	__u8 num_raid_devs;		/* 0x39 Number of configured volumes */
	__u8 fill[2];			/* 0x3A - 0x3B */
#define IMSM_FILLERS 39
	__u32 filler[IMSM_FILLERS];	/* 0x3C - 0xD7 RAID_MPB_FILLERS */
	struct imsm_disk disk[1];	/* 0xD8 diskTbl[numDisks] */
	/* here comes imsm_dev[num_raid_devs] */
} __attribute__ ((packed));

#ifndef MDASSEMBLE
static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
#endif

static unsigned long long mpb_sectors(struct imsm_super *mpb)
{
	__u32 size = __le32_to_cpu(mpb->mpb_size);

	return ((size + (512-1)) & (~(512-1))) / 512;
}

/* internal representation of IMSM metadata */
struct intel_super {
	union {
		struct imsm_super *mpb;
		void *buf;
	};
	struct dl {
		struct dl *next;
		int index;
		__u8 serial[MAX_RAID_SERIAL_LEN];
		int major, minor;
		char *devname;
		int fd;
	} *disks;
};

static struct supertype *match_metadata_desc_imsm(char *arg)
{
	struct supertype *st;

	if (strcmp(arg, "imsm") != 0 &&
	    strcmp(arg, "default") != 0
		)
		return NULL;

	st = malloc(sizeof(*st));
	st->ss = &super_imsm;
	st->max_devs = IMSM_MAX_DEVICES;
	st->minor_version = 0;
	st->sb = NULL;
	return st;
}

static struct supertype *match_metadata_desc_imsm_raid(char *arg)
{
	struct supertype *st;

	if (strcmp(arg, "imsm/raid") != 0 &&
	    strcmp(arg, "raid") != 0 &&
	    strcmp(arg, "default") != 0
		)
		return NULL;

	st = malloc(sizeof(*st));
	st->ss = &super_imsm_raid;
	st->max_devs = IMSM_MAX_DEVICES;
	st->minor_version = 0;
	st->sb = NULL;
	return st;
}

static __u8 *get_imsm_version(struct imsm_super *mpb)
{
	return &mpb->sig[MPB_SIG_LEN];
}

static struct imsm_disk *get_imsm_disk(struct imsm_super *mpb, __u8 index)
{
	if (index > mpb->num_disks - 1)
		return NULL;
	return &mpb->disk[index];
}

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++);

        return sum - __le32_to_cpu(mpb->check_sum);
}

static size_t sizeof_imsm_dev(struct imsm_dev *dev)
{
	size_t size = sizeof(*dev);

	/* each map has disk_ord_tbl[num_members - 1] additional space */
	size += sizeof(__u32) * (dev->vol.map[0].num_members - 1);

	/* migrating means an additional map */
	if (dev->vol.migr_state) {
		size += sizeof(struct imsm_map);
		size += sizeof(__u32) * (dev->vol.map[1].num_members - 1);
	}

	return size;
}

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 - 1)
		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);

	return NULL;
}

static __u32 get_imsm_disk_idx(struct imsm_map *map, int slot)
{
	__u32 *ord_tbl = &map->disk_ord_tbl[slot];

	/* top byte is 'special' */
	return __le32_to_cpu(*ord_tbl & ~(0xff << 24));
}

#ifndef MDASSEMBLE
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 void print_imsm_dev(struct imsm_dev *dev, int index)
{
	__u64 sz;
	int slot;
	struct imsm_map *map = dev->vol.map;

	printf("\n");
	printf("[%s]:\n", dev->volume);
	printf("     RAID Level : %d\n", get_imsm_raid_level(map));
	printf("        Members : %d\n", map->num_members);
	for (slot = 0; slot < map->num_members; slot++)
		if (index == get_imsm_disk_idx(map, slot))
			break;
	if (slot < map->num_members)
		printf("      This Slot : %d\n", slot);
	else
		printf("      This Slot : ?\n");
	sz = __le32_to_cpu(dev->size_high);
	sz <<= 32;
	sz += __le32_to_cpu(dev->size_low);
	printf("     Array Size : %llu%s\n", (unsigned long long)sz,
	       human_size(sz * 512));
	sz = __le32_to_cpu(map->blocks_per_member);
	printf("   Per Dev Size : %llu%s\n", (unsigned long long)sz,
	       human_size(sz * 512));
	printf("  Sector Offset : %u\n",
		__le32_to_cpu(map->pba_of_lba0));
	printf("    Num Stripes : %u\n",
		__le32_to_cpu(map->num_data_stripes));
	printf("     Chunk Size : %u KiB\n",
		__le16_to_cpu(map->blocks_per_strip) / 2);
	printf("       Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
	printf("  Migrate State : %s\n", dev->vol.migr_state ? "migrating" : "idle");
	printf("    Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
	printf("      Map State : %s\n", map_state_str[map->map_state]);
}

static void print_imsm_disk(struct imsm_super *mpb, int index)
{
	struct imsm_disk *disk = get_imsm_disk(mpb, index);
	char str[MAX_RAID_SERIAL_LEN];
	__u32 s;
	__u64 sz;

	printf("\n");
	snprintf(str, MAX_RAID_SERIAL_LEN, "%s", disk->serial);
	printf("  Disk%02d Serial : %s\n", index, str);
	s = __le32_to_cpu(disk->status);
	printf("          State :%s%s%s%s\n", s&SPARE_DISK ? " spare" : "",
					      s&CONFIGURED_DISK ? " active" : "",
					      s&FAILED_DISK ? " failed" : "",
					      s&USABLE_DISK ? " usable" : "");
	printf("             Id : %08x\n", __le32_to_cpu(disk->scsi_id));
	sz = __le32_to_cpu(disk->total_blocks) - mpb_sectors(mpb);
	printf("    Usable Size : %llu%s\n", (unsigned long long)sz,
	       human_size(sz * 512));
}

static void examine_super_imsm(struct supertype *st, char *homehost)
{
	struct intel_super *super = st->sb;
	struct imsm_super *mpb = super->mpb;
	char str[MAX_SIGNATURE_LENGTH];
	int i;
	__u32 sum;

	snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
	printf("          Magic : %s\n", str);
	snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
	printf("        Version : %s\n", get_imsm_version(mpb));
	printf("         Family : %08x\n", __le32_to_cpu(mpb->family_num));
	printf("     Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
	sum = __le32_to_cpu(mpb->check_sum);
	printf("       Checksum : %08x %s\n", sum,
		gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
	printf("          Disks : %d\n", mpb->num_disks);
	printf("   RAID Devices : %d\n", mpb->num_raid_devs);
	print_imsm_disk(mpb, super->disks->index);
	for (i = 0; i < mpb->num_raid_devs; i++)
		print_imsm_dev(get_imsm_dev(mpb, i), super->disks->index);
	for (i = 0; i < mpb->num_disks; i++) {
		if (i == super->disks->index)
			continue;
		print_imsm_disk(mpb, i);
	}
}

static void brief_examine_super_imsm(struct supertype *st)
{
	struct intel_super *super = st->sb;
	struct imsm_super *mpb = super->mpb;

	printf("ARRAY /dev/imsm family=%08x metadata=external:imsm\n",
		__le32_to_cpu(mpb->family_num));
}

static void detail_super_imsm(struct supertype *st, char *homehost)
{
	printf("%s\n", __FUNCTION__);
}

static void brief_detail_super_imsm(struct supertype *st)
{
	printf("%s\n", __FUNCTION__);
}
#endif

static int match_home_imsm(struct supertype *st, char *homehost)
{
	printf("%s\n", __FUNCTION__);

	return 0;
}

static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
{
	printf("%s\n", __FUNCTION__);
}

static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
{
	struct intel_super *super = st->sb;
	struct imsm_super *mpb = super->mpb;
	struct imsm_disk *disk;
	__u32 s;
	__u8 *v = get_imsm_version(mpb);
	__u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
	char major[] = { 0, 0, 0 };
	char minor[] = { 0 ,0, 0 };
	char patch[] = { 0, 0, 0 };
	char *ver_parse[] = { major, minor, patch };
	int i, j;

	i = j = 0;
	while (*v != '\0' && v < end) {
		if (*v != '.' && j < 2)
			ver_parse[i][j++] = *v;
		else {
			i++;
			j = 0;
		}
		v++;
	}

	info->array.major_version = 2000;
	info->array.minor_version = strtol(minor, NULL, 0);
	info->array.patch_version = strtol(patch, NULL, 0);
	info->array.raid_disks    = mpb->num_disks;
	info->array.level         = LEVEL_CONTAINER;
	info->array.layout        = 0;
	info->array.md_minor      = -1;
	info->array.ctime         = __le32_to_cpu(mpb->generation_num); //??
	info->array.utime         = 0;
	info->array.chunk_size    = 0;

	info->disk.major = 0;
	info->disk.minor = 0;
	info->disk.number = super->disks->index;
	info->disk.raid_disk = -1;
	/* is this disk a member of a raid device? */
	for (i = 0; i < mpb->num_raid_devs; i++) {
		struct imsm_dev *dev = get_imsm_dev(mpb, i);
		struct imsm_map *map = dev->vol.map;

		for (j = 0; j < map->num_members; j++) {
			__u32 index = get_imsm_disk_idx(map, j);

			if (index == super->disks->index) {
				info->disk.raid_disk = super->disks->index;
				break;
			}
		}
		if (info->disk.raid_disk != -1)
			break;
	}
	disk = get_imsm_disk(mpb, super->disks->index);
	s = __le32_to_cpu(disk->status);
	info->disk.state  = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
	info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
	info->disk.state |= s & USABLE_DISK ? (1 << MD_DISK_SYNC) : 0;
	info->reshape_active = 0;
}

static void getinfo_super_imsm_raid(struct supertype *st, struct mdinfo *info)
{
	printf("%s\n", __FUNCTION__);
}

static int update_super_imsm(struct supertype *st, struct mdinfo *info,
			     char *update, char *devname, int verbose,
			     int uuid_set, char *homehost)
{
	return 0;
}

static __u64 avail_size_imsm(struct supertype *st, __u64 size)
{
	printf("%s\n", __FUNCTION__);

	return 0;
}

static int compare_super_imsm(struct supertype *st, struct supertype *tst)
{
	/*
	 * return:
	 *  0 same, or first was empty, and second was copied
	 *  1 second had wrong number
	 *  2 wrong uuid
	 *  3 wrong other info
	 */
	struct intel_super *first = st->sb;
	struct intel_super *sec = tst->sb;

        if (!first) {
                st->sb = tst->sb;
                tst->sb = NULL;
                return 0;
        }

	if (memcmp(first->mpb->sig, sec->mpb->sig, MAX_SIGNATURE_LENGTH) != 0)
		return 3;
	if (first->mpb->family_num != sec->mpb->family_num)
		return 3;
	if (first->mpb->mpb_size != sec->mpb->mpb_size)
		return 3;
	if (first->mpb->check_sum != sec->mpb->check_sum)
		return 3;

	return 0;
}

extern int scsi_get_serial(int fd, void *buf, size_t buf_len);

static int imsm_read_serial(int fd, char *devname,
			    __u8 serial[MAX_RAID_SERIAL_LEN])
{
	unsigned char scsi_serial[255];
	struct stat stb;
	int sg_fd;
	int minor;
	char sg_path[20];
	int rv;
	int rsp_len;
	int i, cnt;

	memset(scsi_serial, 0, sizeof(scsi_serial));
	fstat(fd, &stb);
	minor = minor(stb.st_rdev);
	minor /= 16;

	sprintf(sg_path, "/dev/sg%d", minor);
	sg_fd = open(sg_path, O_RDONLY);
	if (sg_fd < 0) {
		if (devname)
			fprintf(stderr,
				Name ": Failed to open %s for %s: %s\n",
				sg_path,  devname, strerror(errno));
		return 1;
	}

	rv = scsi_get_serial(sg_fd, scsi_serial, sizeof(scsi_serial));
	close(sg_fd);

	if (rv != 0) {
		if (devname)
			fprintf(stderr,
				Name ": Failed to retrieve serial for %s\n",
				devname);
		return rv;
	}

	rsp_len = scsi_serial[3];
	for (i = 0, cnt = 0; i < rsp_len; i++) {
		if (!isspace(scsi_serial[4 + i]))
			serial[cnt++] = scsi_serial[4 + i];
		if (cnt == MAX_RAID_SERIAL_LEN)
			break;
	}

	serial[MAX_RAID_SERIAL_LEN - 1] = '\0';

	return 0;
}

static int
load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
{
	struct imsm_super *mpb = super->mpb;
	struct dl *dl;
	struct stat stb;
	struct imsm_disk *disk;
	int rv;
	int i;

	dl = malloc(sizeof(*dl));
	if (!dl) {
		if (devname)
			fprintf(stderr,
				Name ": failed to allocate disk buffer for %s\n",
				devname);
		return 2;
	}
	memset(dl, 0, sizeof(*dl));

	fstat(fd, &stb);
	dl->major = major(stb.st_rdev);
	dl->minor = minor(stb.st_rdev);
	dl->next = super->disks;
	dl->fd = keep_fd ? fd : -1;
	dl->devname = devname ? strdup(devname) : NULL;
	dl->index = -1;
	super->disks = dl;
	rv = imsm_read_serial(fd, devname, dl->serial);

	if (rv != 0)
		return 2;

	/* look up this disk's index */
	for (i = 0; i < mpb->num_disks; i++) {
		disk = get_imsm_disk(mpb, i);

		if (memcmp(disk->serial, dl->serial, MAX_RAID_SERIAL_LEN) == 0)
			break;
	}

	if (i > mpb->num_disks)
		return 2;

	dl->index = i;

	return 0;
}

/* load_imsm_mpb - read matrix metadata
 * allocates super->mpb to be freed by free_super
 */
static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
{
	unsigned long long dsize;
	size_t len, mpb_size;
	unsigned long long sectors;
	struct stat;
	struct imsm_super anchor;
	__u32 check_sum;

	memset(super, 0, sizeof(*super));
	get_dev_size(fd, NULL, &dsize);

	if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
		if (devname)
			fprintf(stderr,
				Name ": Cannot seek to anchor block on %s: %s\n",
				devname, strerror(errno));
		return 1;
	}

	len = sizeof(anchor);
	if (read(fd, &anchor, len) != len) {
		if (devname)
			fprintf(stderr,
				Name ": Cannot read anchor block on %s: %s\n",
				devname, strerror(errno));
		return 1;
	}

	if (strncmp((char *) anchor.sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
		if (devname)
			fprintf(stderr,
				Name ": no IMSM anchor on %s\n", devname);
		return 2;
	}

	mpb_size = __le32_to_cpu(anchor.mpb_size);
	super->mpb = malloc(mpb_size < 512 ? 512 : mpb_size);
	if (!super->mpb) {
		if (devname)
			fprintf(stderr,
				Name ": unable to allocate %zu byte mpb buffer\n",
				mpb_size);
		return 2;
	}
	memcpy(super->buf, &anchor, sizeof(anchor));

	/* read the rest of the first block */
	len = 512 - sizeof(anchor);
	if (read(fd, super->buf + sizeof(anchor), len) != len) {
		if (devname)
			fprintf(stderr,
				Name ": Cannot read anchor remainder on %s: %s\n",
				devname, strerror(errno));
		return 2;
	}

	sectors = mpb_sectors(&anchor) - 1;
	if (!sectors)
		return load_imsm_disk(fd, super, devname, 0);

	/* read the extended mpb */
	if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
		if (devname)
			fprintf(stderr,
				Name ": Cannot seek to extended mpb on %s: %s\n",
				devname, strerror(errno));
		return 1;
	}

	len = mpb_size - 512;
	if (read(fd, super->buf + 512, len) != len) {
		if (devname)
			fprintf(stderr,
				Name ": Cannot read extended mpb on %s: %s\n",
				devname, strerror(errno));
		return 2;
	}

	check_sum = gen_imsm_checksum(super->mpb);
	if (check_sum != __le32_to_cpu(super->mpb->check_sum)) {
		if (devname)
			fprintf(stderr,
				Name ": IMSM checksum %x != %x on %s\n",
				check_sum, __le32_to_cpu(super->mpb->check_sum),
				devname);
		return 2;
	}

	return load_imsm_disk(fd, super, devname, 0);
}

struct superswitch super_imsm_container;

static void free_imsm_disks(struct intel_super *super)
{
	while (super->disks) {
		struct dl *d = super->disks;

		super->disks = d->next;
		if (d->fd >= 0)
			close(d->fd);
		if (d->devname)
			free(d->devname);
		free(d);
	}
}

static void free_imsm(struct intel_super *super)
{
	if (super->mpb)
		free(super->mpb);
	free_imsm_disks(super);
	free(super);
}


static void free_super_imsm(struct supertype *st)
{
	struct intel_super *super = st->sb;

	if (!super)
		return;

	free_imsm(super);
	st->sb = NULL;
}

#ifndef MDASSEMBLE
static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
			       char *devname, int keep_fd)
{
	struct mdinfo *sra;
	struct intel_super *super;
	struct mdinfo *sd, *best = NULL;
	__u32 bestgen = 0;
	__u32 gen;
	char nm[20];
	int dfd;
	int rv;

	/* check if this disk is a member of an active array */
	sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
	if (!sra)
		return 1;

	if (sra->array.major_version != -1 ||
	    sra->array.minor_version != -2 ||
	    strcmp(sra->text_version, "imsm") != 0)
		return 1;

	super = malloc(sizeof(*super));
	if (!super)
		return 1;

	/* find the most up to date disk in this array */
	for (sd = sra->devs; sd; sd = sd->next) {
		sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
		dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
		if (!dfd) {
			free_imsm(super);
			return 2;
		}
		rv = load_imsm_mpb(dfd, super, NULL);
		if (!keep_fd)
			close(dfd);
		if (rv == 0) {
			gen = __le32_to_cpu(super->mpb->generation_num);
			if (!best || gen > bestgen) {
				bestgen = gen;
				best = sd;
			}
		} else {
			free_imsm(super);
			return 2;
		}
	}

	if (!best) {
		free_imsm(super);
		return 1;
	}

	/* load the most up to date anchor */
	sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
	dfd = dev_open(nm, O_RDONLY);
	if (!dfd) {
		free_imsm(super);
		return 1;
	}
	rv = load_imsm_mpb(dfd, super, NULL);
	close(dfd);
	if (rv != 0) {
		free_imsm(super);
		return 2;
	}

	/* reset the disk list */
	free_imsm_disks(super);

	/* populate disk list */
	for (sd = sra->devs ; sd ; sd = sd->next) {
		sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
		dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
		if (!dfd) {
			free_imsm(super);
			return 2;
		}
		load_imsm_disk(dfd, super, NULL, keep_fd);
		if (!keep_fd)
			close(dfd);
	}

	*sbp = super;
	if (st->ss == NULL) {
		st->ss = &super_imsm_container;
		st->minor_version = 0;
		st->max_devs = IMSM_MAX_DEVICES;
	}

	return 0;
}
#endif

static int load_super_imsm(struct supertype *st, int fd, char *devname)
{
	struct intel_super *super;
	int rv;

#ifndef MDASSEMBLE
	if (load_super_imsm_all(st, fd, &st->sb, devname, 0) == 0)
		return 0;
#endif

	super = malloc(sizeof(*super));
	if (!super) {
		fprintf(stderr,
			Name ": malloc of %zu failed.\n",
			sizeof(*super));
		return 1;
	}

	rv = load_imsm_mpb(fd, super, devname);

	if (rv) {
		if (devname)
			fprintf(stderr,
				Name ": Failed to load all information "
				"sections on %s\n", devname);
		free_imsm(super);
		return rv;
	}

	st->sb = super;
	if (st->ss == NULL) {
		st->ss = &super_imsm;
		st->minor_version = 0;
		st->max_devs = IMSM_MAX_DEVICES;
	}

	return 0;
}

static int init_zero_imsm(struct supertype *st, mdu_array_info_t *info,
			  unsigned long long size, char *name,
			  char *homehost, int *uuid)
{
	printf("%s\n", __FUNCTION__);

	return 0;
}

static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
			   unsigned long long size, char *name,
			   char *homehost, int *uuid)
{
	printf("%s\n", __FUNCTION__);

	return 0;
}

static int init_super_imsm_raid(struct supertype *st, mdu_array_info_t *info,
				unsigned long long size, char *name,
				char *homehost, int *uuid)
{
	printf("%s\n", __FUNCTION__);

	return 0;
}

static void add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dinfo,
			      int fd, char *devname)
{
	printf("%s\n", __FUNCTION__);
}

static void add_to_super_imsm_raid(struct supertype *st, mdu_disk_info_t *dinfo,
				   int fd, char *devname)
{
	printf("%s\n", __FUNCTION__);
}

static int write_init_super_imsm(struct supertype *st)
{
	printf("%s\n", __FUNCTION__);

	return 0;
}

static int store_zero_imsm(struct supertype *st, int fd)
{
	printf("%s\n", __FUNCTION__);

	return 0;
}

static void getinfo_super_n_container(struct supertype *st, struct mdinfo *info)
{
	printf("%s\n", __FUNCTION__);
}

static void getinfo_super_n_raid(struct supertype *st, struct mdinfo *info)
{
	printf("%s\n", __FUNCTION__);
}

static int validate_geometry_imsm(struct supertype *st, int level, int layout,
				  int raiddisks, int chunk, unsigned long long size,
				  char *subdev, unsigned long long *freesize)
{
	printf("%s\n", __FUNCTION__);

	return 0;
}

static int validate_geometry_imsm_container(struct supertype *st, int level,
					    int layout, int raiddisks, int chunk,
					    unsigned long long size, char *subdev,
					    unsigned long long *freesize)
{
	printf("%s\n", __FUNCTION__);

	return 0;
}

static int validate_geometry_imsm_raid(struct supertype *st, int level,
				       int layout, int raiddisks, int chunk,
				       unsigned long long size, char *subdev,
				       unsigned long long *freesize)
{
	printf("%s\n", __FUNCTION__);

	return 0;
}



static struct mdinfo *container_content_imsm(struct supertype *st)
{
	printf("%s\n", __FUNCTION__);

	return NULL;
}

struct superswitch super_imsm = {
#ifndef	MDASSEMBLE
	.examine_super	= examine_super_imsm,
	.brief_examine_super = brief_examine_super_imsm,
	.detail_super	= detail_super_imsm,
	.brief_detail_super = brief_detail_super_imsm,
#endif
	.match_home	= match_home_imsm,
	.uuid_from_super= uuid_from_super_imsm,
	.getinfo_super  = getinfo_super_imsm,
	.update_super	= update_super_imsm,

	.avail_size	= avail_size_imsm,

	.compare_super	= compare_super_imsm,

	.load_super	= load_super_imsm,
	.init_super	= init_zero_imsm,
	.store_super	= store_zero_imsm,
	.free_super	= free_super_imsm,
	.match_metadata_desc = match_metadata_desc_imsm,
	.getinfo_super_n  = getinfo_super_n_container,

	.validate_geometry = validate_geometry_imsm,
	.major		= 2000,
	.swapuuid	= 0,
	.external	= 1,
	.text_version	= "imsm",
};

/* super_imsm_container is set by validate_geometry_imsm when given a
 * device that is not part of any array
 */
struct superswitch super_imsm_container = {

	.validate_geometry = validate_geometry_imsm_container,
	.init_super	= init_super_imsm,
	.add_to_super	= add_to_super_imsm,
	.write_init_super = write_init_super_imsm,
	.getinfo_super  = getinfo_super_imsm,
	.getinfo_super_n  = getinfo_super_n_container,
	.load_super	= load_super_imsm,

#ifndef MDASSEMBLE
	.examine_super	= examine_super_imsm,
	.brief_examine_super = brief_examine_super_imsm,
	.detail_super	= detail_super_imsm,
	.brief_detail_super = brief_detail_super_imsm,
#endif

	.free_super	= free_super_imsm,

	.container_content = container_content_imsm,

	.major		= 2000,
	.swapuuid	= 0,
	.external	= 1,
	.text_version	= "imsm",
};

struct superswitch super_imsm_raid = {
	.update_super	= update_super_imsm,
	.init_super	= init_super_imsm_raid,
	.add_to_super	= add_to_super_imsm_raid,
	.getinfo_super  = getinfo_super_imsm_raid,
	.getinfo_super_n  = getinfo_super_n_raid,
	.write_init_super = write_init_super_imsm,

	.load_super	= load_super_imsm,
	.free_super	= free_super_imsm,
	.match_metadata_desc = match_metadata_desc_imsm_raid,


	.validate_geometry = validate_geometry_imsm_raid,
	.major		= 2001,
	.swapuuid	= 0,
	.external	= 2,
	.text_version	= "imsm",
};