imsm: initial Intel(R) Matrix Storage Manager support
[thirdparty/mdadm.git] / super-intel.c
CommitLineData
cdddbdbc
DW
1/*
2 * mdadm - Intel(R) Matrix Storage Manager Support
3 *
4 * Copyright (C) 2002-2007 Intel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "mdadm.h"
21#include <values.h>
22#include <scsi/sg.h>
23#include <ctype.h>
24
25/* MPB == Metadata Parameter Block */
26#define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
27#define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
28#define MPB_VERSION_RAID0 "1.0.00"
29#define MPB_VERSION_RAID1 "1.1.00"
30#define MPB_VERSION_RAID5 "1.2.02"
31#define MAX_SIGNATURE_LENGTH 32
32#define MAX_RAID_SERIAL_LEN 16
33
34/* Disk configuration info. */
35#define IMSM_MAX_DEVICES 255
36struct imsm_disk {
37 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
38 __u32 total_blocks; /* 0xE8 - 0xEB total blocks */
39 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
40 __u32 status; /* 0xF0 - 0xF3 */
41#define SPARE_DISK 0x01 /* Spare */
42#define CONFIGURED_DISK 0x02 /* Member of some RaidDev */
43#define FAILED_DISK 0x04 /* Permanent failure */
44#define USABLE_DISK 0x08 /* Fully usable unless FAILED_DISK is set */
45
46#define IMSM_DISK_FILLERS 5
47 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */
48};
49
50/* RAID map configuration infos. */
51struct imsm_map {
52 __u32 pba_of_lba0; /* start address of partition */
53 __u32 blocks_per_member;/* blocks per member */
54 __u32 num_data_stripes; /* number of data stripes */
55 __u16 blocks_per_strip;
56 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
57#define IMSM_T_STATE_NORMAL 0
58#define IMSM_T_STATE_UNINITIALIZED 1
59#define IMSM_T_STATE_DEGRADED 2 /* FIXME: is this correct? */
60#define IMSM_T_STATE_FAILED 3 /* FIXME: is this correct? */
61 __u8 raid_level;
62#define IMSM_T_RAID0 0
63#define IMSM_T_RAID1 1
64#define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
65 __u8 num_members; /* number of member disks */
66 __u8 reserved[3];
67 __u32 filler[7]; /* expansion area */
68 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
69 top byte special */
70} __attribute__ ((packed));
71
72struct imsm_vol {
73 __u32 reserved[2];
74 __u8 migr_state; /* Normal or Migrating */
75 __u8 migr_type; /* Initializing, Rebuilding, ... */
76 __u8 dirty;
77 __u8 fill[1];
78 __u32 filler[5];
79 struct imsm_map map[1];
80 /* here comes another one if migr_state */
81} __attribute__ ((packed));
82
83struct imsm_dev {
84 __u8 volume[MAX_RAID_SERIAL_LEN];
85 __u32 size_low;
86 __u32 size_high;
87 __u32 status; /* Persistent RaidDev status */
88 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
89#define IMSM_DEV_FILLERS 12
90 __u32 filler[IMSM_DEV_FILLERS];
91 struct imsm_vol vol;
92} __attribute__ ((packed));
93
94struct imsm_super {
95 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
96 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
97 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
98 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
99 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
100 __u32 reserved[2]; /* 0x30 - 0x37 */
101 __u8 num_disks; /* 0x38 Number of configured disks */
102 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
103 __u8 fill[2]; /* 0x3A - 0x3B */
104#define IMSM_FILLERS 39
105 __u32 filler[IMSM_FILLERS]; /* 0x3C - 0xD7 RAID_MPB_FILLERS */
106 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
107 /* here comes imsm_dev[num_raid_devs] */
108} __attribute__ ((packed));
109
110#ifndef MDASSEMBLE
111static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
112#endif
113
114static unsigned long long mpb_sectors(struct imsm_super *mpb)
115{
116 __u32 size = __le32_to_cpu(mpb->mpb_size);
117
118 return ((size + (512-1)) & (~(512-1))) / 512;
119}
120
121/* internal representation of IMSM metadata */
122struct intel_super {
123 union {
124 struct imsm_super *mpb;
125 void *buf;
126 };
127 struct dl {
128 struct dl *next;
129 int index;
130 __u8 serial[MAX_RAID_SERIAL_LEN];
131 int major, minor;
132 char *devname;
133 int fd;
134 } *disks;
135};
136
137static struct supertype *match_metadata_desc_imsm(char *arg)
138{
139 struct supertype *st;
140
141 if (strcmp(arg, "imsm") != 0 &&
142 strcmp(arg, "default") != 0
143 )
144 return NULL;
145
146 st = malloc(sizeof(*st));
147 st->ss = &super_imsm;
148 st->max_devs = IMSM_MAX_DEVICES;
149 st->minor_version = 0;
150 st->sb = NULL;
151 return st;
152}
153
154static struct supertype *match_metadata_desc_imsm_raid(char *arg)
155{
156 struct supertype *st;
157
158 if (strcmp(arg, "imsm/raid") != 0 &&
159 strcmp(arg, "raid") != 0 &&
160 strcmp(arg, "default") != 0
161 )
162 return NULL;
163
164 st = malloc(sizeof(*st));
165 st->ss = &super_imsm_raid;
166 st->max_devs = IMSM_MAX_DEVICES;
167 st->minor_version = 0;
168 st->sb = NULL;
169 return st;
170}
171
172static __u8 *get_imsm_version(struct imsm_super *mpb)
173{
174 return &mpb->sig[MPB_SIG_LEN];
175}
176
177static struct imsm_disk *get_imsm_disk(struct imsm_super *mpb, __u8 index)
178{
179 if (index > mpb->num_disks - 1)
180 return NULL;
181 return &mpb->disk[index];
182}
183
184static __u32 gen_imsm_checksum(struct imsm_super *mpb)
185{
186 __u32 end = mpb->mpb_size / sizeof(end);
187 __u32 *p = (__u32 *) mpb;
188 __u32 sum = 0;
189
190 while (end--)
191 sum += __le32_to_cpu(*p++);
192
193 return sum - __le32_to_cpu(mpb->check_sum);
194}
195
196static size_t sizeof_imsm_dev(struct imsm_dev *dev)
197{
198 size_t size = sizeof(*dev);
199
200 /* each map has disk_ord_tbl[num_members - 1] additional space */
201 size += sizeof(__u32) * (dev->vol.map[0].num_members - 1);
202
203 /* migrating means an additional map */
204 if (dev->vol.migr_state) {
205 size += sizeof(struct imsm_map);
206 size += sizeof(__u32) * (dev->vol.map[1].num_members - 1);
207 }
208
209 return size;
210}
211
212static struct imsm_dev *get_imsm_dev(struct imsm_super *mpb, __u8 index)
213{
214 int offset;
215 int i;
216 void *_mpb = mpb;
217
218 if (index > mpb->num_raid_devs - 1)
219 return NULL;
220
221 /* devices start after all disks */
222 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
223
224 for (i = 0; i <= index; i++)
225 if (i == index)
226 return _mpb + offset;
227 else
228 offset += sizeof_imsm_dev(_mpb + offset);
229
230 return NULL;
231}
232
233static __u32 get_imsm_disk_idx(struct imsm_map *map, int slot)
234{
235 __u32 *ord_tbl = &map->disk_ord_tbl[slot];
236
237 /* top byte is 'special' */
238 return __le32_to_cpu(*ord_tbl & ~(0xff << 24));
239}
240
241#ifndef MDASSEMBLE
242static int get_imsm_raid_level(struct imsm_map *map)
243{
244 if (map->raid_level == 1) {
245 if (map->num_members == 2)
246 return 1;
247 else
248 return 10;
249 }
250
251 return map->raid_level;
252}
253
254static void print_imsm_dev(struct imsm_dev *dev, int index)
255{
256 __u64 sz;
257 int slot;
258 struct imsm_map *map = dev->vol.map;
259
260 printf("\n");
261 printf("[%s]:\n", dev->volume);
262 printf(" RAID Level : %d\n", get_imsm_raid_level(map));
263 printf(" Members : %d\n", map->num_members);
264 for (slot = 0; slot < map->num_members; slot++)
265 if (index == get_imsm_disk_idx(map, slot))
266 break;
267 if (slot < map->num_members)
268 printf(" This Slot : %d\n", slot);
269 else
270 printf(" This Slot : ?\n");
271 sz = __le32_to_cpu(dev->size_high);
272 sz <<= 32;
273 sz += __le32_to_cpu(dev->size_low);
274 printf(" Array Size : %llu%s\n", (unsigned long long)sz,
275 human_size(sz * 512));
276 sz = __le32_to_cpu(map->blocks_per_member);
277 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz,
278 human_size(sz * 512));
279 printf(" Sector Offset : %u\n",
280 __le32_to_cpu(map->pba_of_lba0));
281 printf(" Num Stripes : %u\n",
282 __le32_to_cpu(map->num_data_stripes));
283 printf(" Chunk Size : %u KiB\n",
284 __le16_to_cpu(map->blocks_per_strip) / 2);
285 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
286 printf(" Migrate State : %s\n", dev->vol.migr_state ? "migrating" : "idle");
287 printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
288 printf(" Map State : %s\n", map_state_str[map->map_state]);
289}
290
291static void print_imsm_disk(struct imsm_super *mpb, int index)
292{
293 struct imsm_disk *disk = get_imsm_disk(mpb, index);
294 char str[MAX_RAID_SERIAL_LEN];
295 __u32 s;
296 __u64 sz;
297
298 printf("\n");
299 snprintf(str, MAX_RAID_SERIAL_LEN, "%s", disk->serial);
300 printf(" Disk%02d Serial : %s\n", index, str);
301 s = __le32_to_cpu(disk->status);
302 printf(" State :%s%s%s%s\n", s&SPARE_DISK ? " spare" : "",
303 s&CONFIGURED_DISK ? " active" : "",
304 s&FAILED_DISK ? " failed" : "",
305 s&USABLE_DISK ? " usable" : "");
306 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
307 sz = __le32_to_cpu(disk->total_blocks) - mpb_sectors(mpb);
308 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
309 human_size(sz * 512));
310}
311
312static void examine_super_imsm(struct supertype *st, char *homehost)
313{
314 struct intel_super *super = st->sb;
315 struct imsm_super *mpb = super->mpb;
316 char str[MAX_SIGNATURE_LENGTH];
317 int i;
318 __u32 sum;
319
320 snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
321 printf(" Magic : %s\n", str);
322 snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
323 printf(" Version : %s\n", get_imsm_version(mpb));
324 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
325 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
326 sum = __le32_to_cpu(mpb->check_sum);
327 printf(" Checksum : %08x %s\n", sum,
328 gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
329 printf(" Disks : %d\n", mpb->num_disks);
330 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
331 print_imsm_disk(mpb, super->disks->index);
332 for (i = 0; i < mpb->num_raid_devs; i++)
333 print_imsm_dev(get_imsm_dev(mpb, i), super->disks->index);
334 for (i = 0; i < mpb->num_disks; i++) {
335 if (i == super->disks->index)
336 continue;
337 print_imsm_disk(mpb, i);
338 }
339}
340
341static void brief_examine_super_imsm(struct supertype *st)
342{
343 struct intel_super *super = st->sb;
344 struct imsm_super *mpb = super->mpb;
345
346 printf("ARRAY /dev/imsm family=%08x metadata=external:imsm\n",
347 __le32_to_cpu(mpb->family_num));
348}
349
350static void detail_super_imsm(struct supertype *st, char *homehost)
351{
352 printf("%s\n", __FUNCTION__);
353}
354
355static void brief_detail_super_imsm(struct supertype *st)
356{
357 printf("%s\n", __FUNCTION__);
358}
359#endif
360
361static int match_home_imsm(struct supertype *st, char *homehost)
362{
363 printf("%s\n", __FUNCTION__);
364
365 return 0;
366}
367
368static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
369{
370 printf("%s\n", __FUNCTION__);
371}
372
373static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
374{
375 struct intel_super *super = st->sb;
376 struct imsm_super *mpb = super->mpb;
377 struct imsm_disk *disk;
378 __u32 s;
379 __u8 *v = get_imsm_version(mpb);
380 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
381 char major[] = { 0, 0, 0 };
382 char minor[] = { 0 ,0, 0 };
383 char patch[] = { 0, 0, 0 };
384 char *ver_parse[] = { major, minor, patch };
385 int i, j;
386
387 i = j = 0;
388 while (*v != '\0' && v < end) {
389 if (*v != '.' && j < 2)
390 ver_parse[i][j++] = *v;
391 else {
392 i++;
393 j = 0;
394 }
395 v++;
396 }
397
398 info->array.major_version = 2000;
399 info->array.minor_version = strtol(minor, NULL, 0);
400 info->array.patch_version = strtol(patch, NULL, 0);
401 info->array.raid_disks = mpb->num_disks;
402 info->array.level = LEVEL_CONTAINER;
403 info->array.layout = 0;
404 info->array.md_minor = -1;
405 info->array.ctime = __le32_to_cpu(mpb->generation_num); //??
406 info->array.utime = 0;
407 info->array.chunk_size = 0;
408
409 info->disk.major = 0;
410 info->disk.minor = 0;
411 info->disk.number = super->disks->index;
412 info->disk.raid_disk = -1;
413 /* is this disk a member of a raid device? */
414 for (i = 0; i < mpb->num_raid_devs; i++) {
415 struct imsm_dev *dev = get_imsm_dev(mpb, i);
416 struct imsm_map *map = dev->vol.map;
417
418 for (j = 0; j < map->num_members; j++) {
419 __u32 index = get_imsm_disk_idx(map, j);
420
421 if (index == super->disks->index) {
422 info->disk.raid_disk = super->disks->index;
423 break;
424 }
425 }
426 if (info->disk.raid_disk != -1)
427 break;
428 }
429 disk = get_imsm_disk(mpb, super->disks->index);
430 s = __le32_to_cpu(disk->status);
431 info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
432 info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
433 info->disk.state |= s & USABLE_DISK ? (1 << MD_DISK_SYNC) : 0;
434 info->reshape_active = 0;
435}
436
437static void getinfo_super_imsm_raid(struct supertype *st, struct mdinfo *info)
438{
439 printf("%s\n", __FUNCTION__);
440}
441
442static int update_super_imsm(struct supertype *st, struct mdinfo *info,
443 char *update, char *devname, int verbose,
444 int uuid_set, char *homehost)
445{
446 return 0;
447}
448
449static __u64 avail_size_imsm(struct supertype *st, __u64 size)
450{
451 printf("%s\n", __FUNCTION__);
452
453 return 0;
454}
455
456static int compare_super_imsm(struct supertype *st, struct supertype *tst)
457{
458 /*
459 * return:
460 * 0 same, or first was empty, and second was copied
461 * 1 second had wrong number
462 * 2 wrong uuid
463 * 3 wrong other info
464 */
465 struct intel_super *first = st->sb;
466 struct intel_super *sec = tst->sb;
467
468 if (!first) {
469 st->sb = tst->sb;
470 tst->sb = NULL;
471 return 0;
472 }
473
474 if (memcmp(first->mpb->sig, sec->mpb->sig, MAX_SIGNATURE_LENGTH) != 0)
475 return 3;
476 if (first->mpb->family_num != sec->mpb->family_num)
477 return 3;
478 if (first->mpb->mpb_size != sec->mpb->mpb_size)
479 return 3;
480 if (first->mpb->check_sum != sec->mpb->check_sum)
481 return 3;
482
483 return 0;
484}
485
486extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
487
488static int imsm_read_serial(int fd, char *devname,
489 __u8 serial[MAX_RAID_SERIAL_LEN])
490{
491 unsigned char scsi_serial[255];
492 struct stat stb;
493 int sg_fd;
494 int minor;
495 char sg_path[20];
496 int rv;
497 int rsp_len;
498 int i, cnt;
499
500 memset(scsi_serial, 0, sizeof(scsi_serial));
501 fstat(fd, &stb);
502 minor = minor(stb.st_rdev);
503 minor /= 16;
504
505 sprintf(sg_path, "/dev/sg%d", minor);
506 sg_fd = open(sg_path, O_RDONLY);
507 if (sg_fd < 0) {
508 if (devname)
509 fprintf(stderr,
510 Name ": Failed to open %s for %s: %s\n",
511 sg_path, devname, strerror(errno));
512 return 1;
513 }
514
515 rv = scsi_get_serial(sg_fd, scsi_serial, sizeof(scsi_serial));
516 close(sg_fd);
517
518 if (rv != 0) {
519 if (devname)
520 fprintf(stderr,
521 Name ": Failed to retrieve serial for %s\n",
522 devname);
523 return rv;
524 }
525
526 rsp_len = scsi_serial[3];
527 for (i = 0, cnt = 0; i < rsp_len; i++) {
528 if (!isspace(scsi_serial[4 + i]))
529 serial[cnt++] = scsi_serial[4 + i];
530 if (cnt == MAX_RAID_SERIAL_LEN)
531 break;
532 }
533
534 serial[MAX_RAID_SERIAL_LEN - 1] = '\0';
535
536 return 0;
537}
538
539static int
540load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
541{
542 struct imsm_super *mpb = super->mpb;
543 struct dl *dl;
544 struct stat stb;
545 struct imsm_disk *disk;
546 int rv;
547 int i;
548
549 dl = malloc(sizeof(*dl));
550 if (!dl) {
551 if (devname)
552 fprintf(stderr,
553 Name ": failed to allocate disk buffer for %s\n",
554 devname);
555 return 2;
556 }
557 memset(dl, 0, sizeof(*dl));
558
559 fstat(fd, &stb);
560 dl->major = major(stb.st_rdev);
561 dl->minor = minor(stb.st_rdev);
562 dl->next = super->disks;
563 dl->fd = keep_fd ? fd : -1;
564 dl->devname = devname ? strdup(devname) : NULL;
565 dl->index = -1;
566 super->disks = dl;
567 rv = imsm_read_serial(fd, devname, dl->serial);
568
569 if (rv != 0)
570 return 2;
571
572 /* look up this disk's index */
573 for (i = 0; i < mpb->num_disks; i++) {
574 disk = get_imsm_disk(mpb, i);
575
576 if (memcmp(disk->serial, dl->serial, MAX_RAID_SERIAL_LEN) == 0)
577 break;
578 }
579
580 if (i > mpb->num_disks)
581 return 2;
582
583 dl->index = i;
584
585 return 0;
586}
587
588/* load_imsm_mpb - read matrix metadata
589 * allocates super->mpb to be freed by free_super
590 */
591static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
592{
593 unsigned long long dsize;
594 size_t len, mpb_size;
595 unsigned long long sectors;
596 struct stat;
597 struct imsm_super anchor;
598 __u32 check_sum;
599
600 memset(super, 0, sizeof(*super));
601 get_dev_size(fd, NULL, &dsize);
602
603 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
604 if (devname)
605 fprintf(stderr,
606 Name ": Cannot seek to anchor block on %s: %s\n",
607 devname, strerror(errno));
608 return 1;
609 }
610
611 len = sizeof(anchor);
612 if (read(fd, &anchor, len) != len) {
613 if (devname)
614 fprintf(stderr,
615 Name ": Cannot read anchor block on %s: %s\n",
616 devname, strerror(errno));
617 return 1;
618 }
619
620 if (strncmp((char *) anchor.sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
621 if (devname)
622 fprintf(stderr,
623 Name ": no IMSM anchor on %s\n", devname);
624 return 2;
625 }
626
627 mpb_size = __le32_to_cpu(anchor.mpb_size);
628 super->mpb = malloc(mpb_size < 512 ? 512 : mpb_size);
629 if (!super->mpb) {
630 if (devname)
631 fprintf(stderr,
632 Name ": unable to allocate %zu byte mpb buffer\n",
633 mpb_size);
634 return 2;
635 }
636 memcpy(super->buf, &anchor, sizeof(anchor));
637
638 /* read the rest of the first block */
639 len = 512 - sizeof(anchor);
640 if (read(fd, super->buf + sizeof(anchor), len) != len) {
641 if (devname)
642 fprintf(stderr,
643 Name ": Cannot read anchor remainder on %s: %s\n",
644 devname, strerror(errno));
645 return 2;
646 }
647
648 sectors = mpb_sectors(&anchor) - 1;
649 if (!sectors)
650 return load_imsm_disk(fd, super, devname, 0);
651
652 /* read the extended mpb */
653 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
654 if (devname)
655 fprintf(stderr,
656 Name ": Cannot seek to extended mpb on %s: %s\n",
657 devname, strerror(errno));
658 return 1;
659 }
660
661 len = mpb_size - 512;
662 if (read(fd, super->buf + 512, len) != len) {
663 if (devname)
664 fprintf(stderr,
665 Name ": Cannot read extended mpb on %s: %s\n",
666 devname, strerror(errno));
667 return 2;
668 }
669
670 check_sum = gen_imsm_checksum(super->mpb);
671 if (check_sum != __le32_to_cpu(super->mpb->check_sum)) {
672 if (devname)
673 fprintf(stderr,
674 Name ": IMSM checksum %x != %x on %s\n",
675 check_sum, __le32_to_cpu(super->mpb->check_sum),
676 devname);
677 return 2;
678 }
679
680 return load_imsm_disk(fd, super, devname, 0);
681}
682
683struct superswitch super_imsm_container;
684
685static void free_imsm_disks(struct intel_super *super)
686{
687 while (super->disks) {
688 struct dl *d = super->disks;
689
690 super->disks = d->next;
691 if (d->fd >= 0)
692 close(d->fd);
693 if (d->devname)
694 free(d->devname);
695 free(d);
696 }
697}
698
699static void free_imsm(struct intel_super *super)
700{
701 if (super->mpb)
702 free(super->mpb);
703 free_imsm_disks(super);
704 free(super);
705}
706
707
708static void free_super_imsm(struct supertype *st)
709{
710 struct intel_super *super = st->sb;
711
712 if (!super)
713 return;
714
715 free_imsm(super);
716 st->sb = NULL;
717}
718
719#ifndef MDASSEMBLE
720static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
721 char *devname, int keep_fd)
722{
723 struct mdinfo *sra;
724 struct intel_super *super;
725 struct mdinfo *sd, *best = NULL;
726 __u32 bestgen = 0;
727 __u32 gen;
728 char nm[20];
729 int dfd;
730 int rv;
731
732 /* check if this disk is a member of an active array */
733 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
734 if (!sra)
735 return 1;
736
737 if (sra->array.major_version != -1 ||
738 sra->array.minor_version != -2 ||
739 strcmp(sra->text_version, "imsm") != 0)
740 return 1;
741
742 super = malloc(sizeof(*super));
743 if (!super)
744 return 1;
745
746 /* find the most up to date disk in this array */
747 for (sd = sra->devs; sd; sd = sd->next) {
748 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
749 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
750 if (!dfd) {
751 free_imsm(super);
752 return 2;
753 }
754 rv = load_imsm_mpb(dfd, super, NULL);
755 if (!keep_fd)
756 close(dfd);
757 if (rv == 0) {
758 gen = __le32_to_cpu(super->mpb->generation_num);
759 if (!best || gen > bestgen) {
760 bestgen = gen;
761 best = sd;
762 }
763 } else {
764 free_imsm(super);
765 return 2;
766 }
767 }
768
769 if (!best) {
770 free_imsm(super);
771 return 1;
772 }
773
774 /* load the most up to date anchor */
775 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
776 dfd = dev_open(nm, O_RDONLY);
777 if (!dfd) {
778 free_imsm(super);
779 return 1;
780 }
781 rv = load_imsm_mpb(dfd, super, NULL);
782 close(dfd);
783 if (rv != 0) {
784 free_imsm(super);
785 return 2;
786 }
787
788 /* reset the disk list */
789 free_imsm_disks(super);
790
791 /* populate disk list */
792 for (sd = sra->devs ; sd ; sd = sd->next) {
793 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
794 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
795 if (!dfd) {
796 free_imsm(super);
797 return 2;
798 }
799 load_imsm_disk(dfd, super, NULL, keep_fd);
800 if (!keep_fd)
801 close(dfd);
802 }
803
804 *sbp = super;
805 if (st->ss == NULL) {
806 st->ss = &super_imsm_container;
807 st->minor_version = 0;
808 st->max_devs = IMSM_MAX_DEVICES;
809 }
810
811 return 0;
812}
813#endif
814
815static int load_super_imsm(struct supertype *st, int fd, char *devname)
816{
817 struct intel_super *super;
818 int rv;
819
820#ifndef MDASSEMBLE
821 if (load_super_imsm_all(st, fd, &st->sb, devname, 0) == 0)
822 return 0;
823#endif
824
825 super = malloc(sizeof(*super));
826 if (!super) {
827 fprintf(stderr,
828 Name ": malloc of %zu failed.\n",
829 sizeof(*super));
830 return 1;
831 }
832
833 rv = load_imsm_mpb(fd, super, devname);
834
835 if (rv) {
836 if (devname)
837 fprintf(stderr,
838 Name ": Failed to load all information "
839 "sections on %s\n", devname);
840 free_imsm(super);
841 return rv;
842 }
843
844 st->sb = super;
845 if (st->ss == NULL) {
846 st->ss = &super_imsm;
847 st->minor_version = 0;
848 st->max_devs = IMSM_MAX_DEVICES;
849 }
850
851 return 0;
852}
853
854static int init_zero_imsm(struct supertype *st, mdu_array_info_t *info,
855 unsigned long long size, char *name,
856 char *homehost, int *uuid)
857{
858 printf("%s\n", __FUNCTION__);
859
860 return 0;
861}
862
863static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
864 unsigned long long size, char *name,
865 char *homehost, int *uuid)
866{
867 printf("%s\n", __FUNCTION__);
868
869 return 0;
870}
871
872static int init_super_imsm_raid(struct supertype *st, mdu_array_info_t *info,
873 unsigned long long size, char *name,
874 char *homehost, int *uuid)
875{
876 printf("%s\n", __FUNCTION__);
877
878 return 0;
879}
880
881static void add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dinfo,
882 int fd, char *devname)
883{
884 printf("%s\n", __FUNCTION__);
885}
886
887static void add_to_super_imsm_raid(struct supertype *st, mdu_disk_info_t *dinfo,
888 int fd, char *devname)
889{
890 printf("%s\n", __FUNCTION__);
891}
892
893static int write_init_super_imsm(struct supertype *st)
894{
895 printf("%s\n", __FUNCTION__);
896
897 return 0;
898}
899
900static int store_zero_imsm(struct supertype *st, int fd)
901{
902 printf("%s\n", __FUNCTION__);
903
904 return 0;
905}
906
907static void getinfo_super_n_container(struct supertype *st, struct mdinfo *info)
908{
909 printf("%s\n", __FUNCTION__);
910}
911
912static void getinfo_super_n_raid(struct supertype *st, struct mdinfo *info)
913{
914 printf("%s\n", __FUNCTION__);
915}
916
917static int validate_geometry_imsm(struct supertype *st, int level, int layout,
918 int raiddisks, int chunk, unsigned long long size,
919 char *subdev, unsigned long long *freesize)
920{
921 printf("%s\n", __FUNCTION__);
922
923 return 0;
924}
925
926static int validate_geometry_imsm_container(struct supertype *st, int level,
927 int layout, int raiddisks, int chunk,
928 unsigned long long size, char *subdev,
929 unsigned long long *freesize)
930{
931 printf("%s\n", __FUNCTION__);
932
933 return 0;
934}
935
936static int validate_geometry_imsm_raid(struct supertype *st, int level,
937 int layout, int raiddisks, int chunk,
938 unsigned long long size, char *subdev,
939 unsigned long long *freesize)
940{
941 printf("%s\n", __FUNCTION__);
942
943 return 0;
944}
945
946
947
948static struct mdinfo *container_content_imsm(struct supertype *st)
949{
950 printf("%s\n", __FUNCTION__);
951
952 return NULL;
953}
954
955struct superswitch super_imsm = {
956#ifndef MDASSEMBLE
957 .examine_super = examine_super_imsm,
958 .brief_examine_super = brief_examine_super_imsm,
959 .detail_super = detail_super_imsm,
960 .brief_detail_super = brief_detail_super_imsm,
961#endif
962 .match_home = match_home_imsm,
963 .uuid_from_super= uuid_from_super_imsm,
964 .getinfo_super = getinfo_super_imsm,
965 .update_super = update_super_imsm,
966
967 .avail_size = avail_size_imsm,
968
969 .compare_super = compare_super_imsm,
970
971 .load_super = load_super_imsm,
972 .init_super = init_zero_imsm,
973 .store_super = store_zero_imsm,
974 .free_super = free_super_imsm,
975 .match_metadata_desc = match_metadata_desc_imsm,
976 .getinfo_super_n = getinfo_super_n_container,
977
978 .validate_geometry = validate_geometry_imsm,
979 .major = 2000,
980 .swapuuid = 0,
981 .external = 1,
982 .text_version = "imsm",
983};
984
985/* super_imsm_container is set by validate_geometry_imsm when given a
986 * device that is not part of any array
987 */
988struct superswitch super_imsm_container = {
989
990 .validate_geometry = validate_geometry_imsm_container,
991 .init_super = init_super_imsm,
992 .add_to_super = add_to_super_imsm,
993 .write_init_super = write_init_super_imsm,
994 .getinfo_super = getinfo_super_imsm,
995 .getinfo_super_n = getinfo_super_n_container,
996 .load_super = load_super_imsm,
997
998#ifndef MDASSEMBLE
999 .examine_super = examine_super_imsm,
1000 .brief_examine_super = brief_examine_super_imsm,
1001 .detail_super = detail_super_imsm,
1002 .brief_detail_super = brief_detail_super_imsm,
1003#endif
1004
1005 .free_super = free_super_imsm,
1006
1007 .container_content = container_content_imsm,
1008
1009 .major = 2000,
1010 .swapuuid = 0,
1011 .external = 1,
1012 .text_version = "imsm",
1013};
1014
1015struct superswitch super_imsm_raid = {
1016 .update_super = update_super_imsm,
1017 .init_super = init_super_imsm_raid,
1018 .add_to_super = add_to_super_imsm_raid,
1019 .getinfo_super = getinfo_super_imsm_raid,
1020 .getinfo_super_n = getinfo_super_n_raid,
1021 .write_init_super = write_init_super_imsm,
1022
1023 .load_super = load_super_imsm,
1024 .free_super = free_super_imsm,
1025 .match_metadata_desc = match_metadata_desc_imsm_raid,
1026
1027
1028 .validate_geometry = validate_geometry_imsm_raid,
1029 .major = 2001,
1030 .swapuuid = 0,
1031 .external = 2,
1032 .text_version = "imsm",
1033};