Set 'metadata_version' for container_member in Incremental_container
[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{
f352c545
DW
446 /* FIXME */
447
448 /* For 'assemble' and 'force' we need to return non-zero if any
449 * change was made. For others, the return value is ignored.
450 * Update options are:
451 * force-one : This device looks a bit old but needs to be included,
452 * update age info appropriately.
453 * assemble: clear any 'faulty' flag to allow this device to
454 * be assembled.
455 * force-array: Array is degraded but being forced, mark it clean
456 * if that will be needed to assemble it.
457 *
458 * newdev: not used ????
459 * grow: Array has gained a new device - this is currently for
460 * linear only
461 * resync: mark as dirty so a resync will happen.
462 * name: update the name - preserving the homehost
463 *
464 * Following are not relevant for this imsm:
465 * sparc2.2 : update from old dodgey metadata
466 * super-minor: change the preferred_minor number
467 * summaries: update redundant counters.
468 * uuid: Change the uuid of the array to match watch is given
469 * homehost: update the recorded homehost
470 * _reshape_progress: record new reshape_progress position.
471 */
472 int rv = 0;
473 //struct intel_super *super = st->sb;
474 //struct imsm_super *mpb = super->mpb;
475
476 if (strcmp(update, "grow") == 0) {
477 }
478 if (strcmp(update, "resync") == 0) {
479 /* dev->vol.dirty = 1; */
480 }
481
482 /* IMSM has no concept of UUID or homehost */
483
484 return rv;
cdddbdbc
DW
485}
486
487static __u64 avail_size_imsm(struct supertype *st, __u64 size)
488{
489 printf("%s\n", __FUNCTION__);
490
491 return 0;
492}
493
494static int compare_super_imsm(struct supertype *st, struct supertype *tst)
495{
496 /*
497 * return:
498 * 0 same, or first was empty, and second was copied
499 * 1 second had wrong number
500 * 2 wrong uuid
501 * 3 wrong other info
502 */
503 struct intel_super *first = st->sb;
504 struct intel_super *sec = tst->sb;
505
506 if (!first) {
507 st->sb = tst->sb;
508 tst->sb = NULL;
509 return 0;
510 }
511
512 if (memcmp(first->mpb->sig, sec->mpb->sig, MAX_SIGNATURE_LENGTH) != 0)
513 return 3;
514 if (first->mpb->family_num != sec->mpb->family_num)
515 return 3;
516 if (first->mpb->mpb_size != sec->mpb->mpb_size)
517 return 3;
518 if (first->mpb->check_sum != sec->mpb->check_sum)
519 return 3;
520
521 return 0;
522}
523
524extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
525
526static int imsm_read_serial(int fd, char *devname,
527 __u8 serial[MAX_RAID_SERIAL_LEN])
528{
529 unsigned char scsi_serial[255];
530 struct stat stb;
531 int sg_fd;
532 int minor;
533 char sg_path[20];
534 int rv;
535 int rsp_len;
536 int i, cnt;
537
538 memset(scsi_serial, 0, sizeof(scsi_serial));
539 fstat(fd, &stb);
540 minor = minor(stb.st_rdev);
541 minor /= 16;
542
543 sprintf(sg_path, "/dev/sg%d", minor);
544 sg_fd = open(sg_path, O_RDONLY);
545 if (sg_fd < 0) {
546 if (devname)
547 fprintf(stderr,
548 Name ": Failed to open %s for %s: %s\n",
549 sg_path, devname, strerror(errno));
550 return 1;
551 }
552
553 rv = scsi_get_serial(sg_fd, scsi_serial, sizeof(scsi_serial));
554 close(sg_fd);
555
556 if (rv != 0) {
557 if (devname)
558 fprintf(stderr,
559 Name ": Failed to retrieve serial for %s\n",
560 devname);
561 return rv;
562 }
563
564 rsp_len = scsi_serial[3];
565 for (i = 0, cnt = 0; i < rsp_len; i++) {
566 if (!isspace(scsi_serial[4 + i]))
567 serial[cnt++] = scsi_serial[4 + i];
568 if (cnt == MAX_RAID_SERIAL_LEN)
569 break;
570 }
571
572 serial[MAX_RAID_SERIAL_LEN - 1] = '\0';
573
574 return 0;
575}
576
577static int
578load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
579{
580 struct imsm_super *mpb = super->mpb;
581 struct dl *dl;
582 struct stat stb;
583 struct imsm_disk *disk;
584 int rv;
585 int i;
586
587 dl = malloc(sizeof(*dl));
588 if (!dl) {
589 if (devname)
590 fprintf(stderr,
591 Name ": failed to allocate disk buffer for %s\n",
592 devname);
593 return 2;
594 }
595 memset(dl, 0, sizeof(*dl));
596
597 fstat(fd, &stb);
598 dl->major = major(stb.st_rdev);
599 dl->minor = minor(stb.st_rdev);
600 dl->next = super->disks;
601 dl->fd = keep_fd ? fd : -1;
602 dl->devname = devname ? strdup(devname) : NULL;
603 dl->index = -1;
604 super->disks = dl;
605 rv = imsm_read_serial(fd, devname, dl->serial);
606
607 if (rv != 0)
608 return 2;
609
610 /* look up this disk's index */
611 for (i = 0; i < mpb->num_disks; i++) {
612 disk = get_imsm_disk(mpb, i);
613
614 if (memcmp(disk->serial, dl->serial, MAX_RAID_SERIAL_LEN) == 0)
615 break;
616 }
617
618 if (i > mpb->num_disks)
619 return 2;
620
621 dl->index = i;
622
623 return 0;
624}
625
626/* load_imsm_mpb - read matrix metadata
627 * allocates super->mpb to be freed by free_super
628 */
629static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
630{
631 unsigned long long dsize;
632 size_t len, mpb_size;
633 unsigned long long sectors;
634 struct stat;
635 struct imsm_super anchor;
636 __u32 check_sum;
637
638 memset(super, 0, sizeof(*super));
639 get_dev_size(fd, NULL, &dsize);
640
641 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
642 if (devname)
643 fprintf(stderr,
644 Name ": Cannot seek to anchor block on %s: %s\n",
645 devname, strerror(errno));
646 return 1;
647 }
648
649 len = sizeof(anchor);
650 if (read(fd, &anchor, len) != len) {
651 if (devname)
652 fprintf(stderr,
653 Name ": Cannot read anchor block on %s: %s\n",
654 devname, strerror(errno));
655 return 1;
656 }
657
658 if (strncmp((char *) anchor.sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
659 if (devname)
660 fprintf(stderr,
661 Name ": no IMSM anchor on %s\n", devname);
662 return 2;
663 }
664
665 mpb_size = __le32_to_cpu(anchor.mpb_size);
666 super->mpb = malloc(mpb_size < 512 ? 512 : mpb_size);
667 if (!super->mpb) {
668 if (devname)
669 fprintf(stderr,
670 Name ": unable to allocate %zu byte mpb buffer\n",
671 mpb_size);
672 return 2;
673 }
674 memcpy(super->buf, &anchor, sizeof(anchor));
675
676 /* read the rest of the first block */
677 len = 512 - sizeof(anchor);
678 if (read(fd, super->buf + sizeof(anchor), len) != len) {
679 if (devname)
680 fprintf(stderr,
681 Name ": Cannot read anchor remainder on %s: %s\n",
682 devname, strerror(errno));
683 return 2;
684 }
685
686 sectors = mpb_sectors(&anchor) - 1;
687 if (!sectors)
688 return load_imsm_disk(fd, super, devname, 0);
689
690 /* read the extended mpb */
691 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
692 if (devname)
693 fprintf(stderr,
694 Name ": Cannot seek to extended mpb on %s: %s\n",
695 devname, strerror(errno));
696 return 1;
697 }
698
699 len = mpb_size - 512;
700 if (read(fd, super->buf + 512, len) != len) {
701 if (devname)
702 fprintf(stderr,
703 Name ": Cannot read extended mpb on %s: %s\n",
704 devname, strerror(errno));
705 return 2;
706 }
707
708 check_sum = gen_imsm_checksum(super->mpb);
709 if (check_sum != __le32_to_cpu(super->mpb->check_sum)) {
710 if (devname)
711 fprintf(stderr,
712 Name ": IMSM checksum %x != %x on %s\n",
713 check_sum, __le32_to_cpu(super->mpb->check_sum),
714 devname);
715 return 2;
716 }
717
718 return load_imsm_disk(fd, super, devname, 0);
719}
720
721struct superswitch super_imsm_container;
722
723static void free_imsm_disks(struct intel_super *super)
724{
725 while (super->disks) {
726 struct dl *d = super->disks;
727
728 super->disks = d->next;
729 if (d->fd >= 0)
730 close(d->fd);
731 if (d->devname)
732 free(d->devname);
733 free(d);
734 }
735}
736
737static void free_imsm(struct intel_super *super)
738{
739 if (super->mpb)
740 free(super->mpb);
741 free_imsm_disks(super);
742 free(super);
743}
744
745
746static void free_super_imsm(struct supertype *st)
747{
748 struct intel_super *super = st->sb;
749
750 if (!super)
751 return;
752
753 free_imsm(super);
754 st->sb = NULL;
755}
756
757#ifndef MDASSEMBLE
758static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
759 char *devname, int keep_fd)
760{
761 struct mdinfo *sra;
762 struct intel_super *super;
763 struct mdinfo *sd, *best = NULL;
764 __u32 bestgen = 0;
765 __u32 gen;
766 char nm[20];
767 int dfd;
768 int rv;
769
770 /* check if this disk is a member of an active array */
771 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
772 if (!sra)
773 return 1;
774
775 if (sra->array.major_version != -1 ||
776 sra->array.minor_version != -2 ||
777 strcmp(sra->text_version, "imsm") != 0)
778 return 1;
779
780 super = malloc(sizeof(*super));
781 if (!super)
782 return 1;
783
784 /* find the most up to date disk in this array */
785 for (sd = sra->devs; sd; sd = sd->next) {
786 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
787 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
788 if (!dfd) {
789 free_imsm(super);
790 return 2;
791 }
792 rv = load_imsm_mpb(dfd, super, NULL);
793 if (!keep_fd)
794 close(dfd);
795 if (rv == 0) {
796 gen = __le32_to_cpu(super->mpb->generation_num);
797 if (!best || gen > bestgen) {
798 bestgen = gen;
799 best = sd;
800 }
801 } else {
802 free_imsm(super);
803 return 2;
804 }
805 }
806
807 if (!best) {
808 free_imsm(super);
809 return 1;
810 }
811
812 /* load the most up to date anchor */
813 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
814 dfd = dev_open(nm, O_RDONLY);
815 if (!dfd) {
816 free_imsm(super);
817 return 1;
818 }
819 rv = load_imsm_mpb(dfd, super, NULL);
820 close(dfd);
821 if (rv != 0) {
822 free_imsm(super);
823 return 2;
824 }
825
826 /* reset the disk list */
827 free_imsm_disks(super);
828
829 /* populate disk list */
830 for (sd = sra->devs ; sd ; sd = sd->next) {
831 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
832 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
833 if (!dfd) {
834 free_imsm(super);
835 return 2;
836 }
837 load_imsm_disk(dfd, super, NULL, keep_fd);
838 if (!keep_fd)
839 close(dfd);
840 }
841
842 *sbp = super;
843 if (st->ss == NULL) {
844 st->ss = &super_imsm_container;
845 st->minor_version = 0;
846 st->max_devs = IMSM_MAX_DEVICES;
847 }
848
849 return 0;
850}
851#endif
852
853static int load_super_imsm(struct supertype *st, int fd, char *devname)
854{
855 struct intel_super *super;
856 int rv;
857
858#ifndef MDASSEMBLE
859 if (load_super_imsm_all(st, fd, &st->sb, devname, 0) == 0)
860 return 0;
861#endif
862
863 super = malloc(sizeof(*super));
864 if (!super) {
865 fprintf(stderr,
866 Name ": malloc of %zu failed.\n",
867 sizeof(*super));
868 return 1;
869 }
870
871 rv = load_imsm_mpb(fd, super, devname);
872
873 if (rv) {
874 if (devname)
875 fprintf(stderr,
876 Name ": Failed to load all information "
877 "sections on %s\n", devname);
878 free_imsm(super);
879 return rv;
880 }
881
882 st->sb = super;
883 if (st->ss == NULL) {
884 st->ss = &super_imsm;
885 st->minor_version = 0;
886 st->max_devs = IMSM_MAX_DEVICES;
887 }
888
889 return 0;
890}
891
892static int init_zero_imsm(struct supertype *st, mdu_array_info_t *info,
893 unsigned long long size, char *name,
894 char *homehost, int *uuid)
895{
896 printf("%s\n", __FUNCTION__);
897
898 return 0;
899}
900
901static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
902 unsigned long long size, char *name,
903 char *homehost, int *uuid)
904{
905 printf("%s\n", __FUNCTION__);
906
907 return 0;
908}
909
910static int init_super_imsm_raid(struct supertype *st, mdu_array_info_t *info,
911 unsigned long long size, char *name,
912 char *homehost, int *uuid)
913{
914 printf("%s\n", __FUNCTION__);
915
916 return 0;
917}
918
919static void add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dinfo,
920 int fd, char *devname)
921{
922 printf("%s\n", __FUNCTION__);
923}
924
925static void add_to_super_imsm_raid(struct supertype *st, mdu_disk_info_t *dinfo,
926 int fd, char *devname)
927{
928 printf("%s\n", __FUNCTION__);
929}
930
931static int write_init_super_imsm(struct supertype *st)
932{
933 printf("%s\n", __FUNCTION__);
934
935 return 0;
936}
937
938static int store_zero_imsm(struct supertype *st, int fd)
939{
940 printf("%s\n", __FUNCTION__);
941
942 return 0;
943}
944
f352c545 945static void getinfo_super_n_imsm_container(struct supertype *st, struct mdinfo *info)
cdddbdbc 946{
f352c545
DW
947 /* just need offset and size...
948 * of the metadata??
949 */
950 struct intel_super *super = st->sb;
951 struct imsm_super *mpb = super->mpb;
952 struct imsm_disk *disk = get_imsm_disk(mpb, info->disk.number);
953 int sect = mpb_sectors(mpb);
954
955 info->data_offset = __le32_to_cpu(disk->total_blocks) - (2 + sect - 1);
956 info->component_size = sect;
cdddbdbc
DW
957}
958
959static void getinfo_super_n_raid(struct supertype *st, struct mdinfo *info)
960{
961 printf("%s\n", __FUNCTION__);
962}
963
964static int validate_geometry_imsm(struct supertype *st, int level, int layout,
965 int raiddisks, int chunk, unsigned long long size,
966 char *subdev, unsigned long long *freesize)
967{
968 printf("%s\n", __FUNCTION__);
969
970 return 0;
971}
972
973static int validate_geometry_imsm_container(struct supertype *st, int level,
974 int layout, int raiddisks, int chunk,
975 unsigned long long size, char *subdev,
976 unsigned long long *freesize)
977{
978 printf("%s\n", __FUNCTION__);
979
980 return 0;
981}
982
983static int validate_geometry_imsm_raid(struct supertype *st, int level,
984 int layout, int raiddisks, int chunk,
985 unsigned long long size, char *subdev,
986 unsigned long long *freesize)
987{
988 printf("%s\n", __FUNCTION__);
989
990 return 0;
991}
992
993
994
995static struct mdinfo *container_content_imsm(struct supertype *st)
996{
997 printf("%s\n", __FUNCTION__);
998
999 return NULL;
1000}
1001
1002struct superswitch super_imsm = {
1003#ifndef MDASSEMBLE
1004 .examine_super = examine_super_imsm,
1005 .brief_examine_super = brief_examine_super_imsm,
1006 .detail_super = detail_super_imsm,
1007 .brief_detail_super = brief_detail_super_imsm,
1008#endif
1009 .match_home = match_home_imsm,
1010 .uuid_from_super= uuid_from_super_imsm,
1011 .getinfo_super = getinfo_super_imsm,
1012 .update_super = update_super_imsm,
1013
1014 .avail_size = avail_size_imsm,
1015
1016 .compare_super = compare_super_imsm,
1017
1018 .load_super = load_super_imsm,
1019 .init_super = init_zero_imsm,
1020 .store_super = store_zero_imsm,
1021 .free_super = free_super_imsm,
1022 .match_metadata_desc = match_metadata_desc_imsm,
f352c545 1023 .getinfo_super_n = getinfo_super_n_imsm_container,
cdddbdbc
DW
1024
1025 .validate_geometry = validate_geometry_imsm,
1026 .major = 2000,
1027 .swapuuid = 0,
1028 .external = 1,
1029 .text_version = "imsm",
1030};
1031
1032/* super_imsm_container is set by validate_geometry_imsm when given a
1033 * device that is not part of any array
1034 */
1035struct superswitch super_imsm_container = {
1036
1037 .validate_geometry = validate_geometry_imsm_container,
1038 .init_super = init_super_imsm,
1039 .add_to_super = add_to_super_imsm,
1040 .write_init_super = write_init_super_imsm,
1041 .getinfo_super = getinfo_super_imsm,
f352c545 1042 .getinfo_super_n = getinfo_super_n_imsm_container,
cdddbdbc
DW
1043 .load_super = load_super_imsm,
1044
1045#ifndef MDASSEMBLE
1046 .examine_super = examine_super_imsm,
1047 .brief_examine_super = brief_examine_super_imsm,
1048 .detail_super = detail_super_imsm,
1049 .brief_detail_super = brief_detail_super_imsm,
1050#endif
1051
1052 .free_super = free_super_imsm,
1053
1054 .container_content = container_content_imsm,
1055
1056 .major = 2000,
1057 .swapuuid = 0,
1058 .external = 1,
1059 .text_version = "imsm",
1060};
1061
1062struct superswitch super_imsm_raid = {
1063 .update_super = update_super_imsm,
1064 .init_super = init_super_imsm_raid,
1065 .add_to_super = add_to_super_imsm_raid,
1066 .getinfo_super = getinfo_super_imsm_raid,
1067 .getinfo_super_n = getinfo_super_n_raid,
1068 .write_init_super = write_init_super_imsm,
1069
1070 .load_super = load_super_imsm,
1071 .free_super = free_super_imsm,
1072 .match_metadata_desc = match_metadata_desc_imsm_raid,
1073
1074
1075 .validate_geometry = validate_geometry_imsm_raid,
1076 .major = 2001,
1077 .swapuuid = 0,
1078 .external = 2,
1079 .text_version = "imsm",
1080};