Always initialise a struct super_type to zero
[thirdparty/mdadm.git] / super-intel.c
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 "mdmon.h"
22 #include <values.h>
23 #include <scsi/sg.h>
24 #include <ctype.h>
25
26 /* MPB == Metadata Parameter Block */
27 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
28 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
29 #define MPB_VERSION_RAID0 "1.0.00"
30 #define MPB_VERSION_RAID1 "1.1.00"
31 #define MPB_VERSION_RAID5 "1.2.02"
32 #define MAX_SIGNATURE_LENGTH 32
33 #define MAX_RAID_SERIAL_LEN 16
34 #define MPB_SECTOR_CNT 418
35 #define IMSM_RESERVED_SECTORS 4096
36
37 /* Disk configuration info. */
38 #define IMSM_MAX_DEVICES 255
39 struct imsm_disk {
40 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
41 __u32 total_blocks; /* 0xE8 - 0xEB total blocks */
42 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
43 __u32 status; /* 0xF0 - 0xF3 */
44 #define SPARE_DISK 0x01 /* Spare */
45 #define CONFIGURED_DISK 0x02 /* Member of some RaidDev */
46 #define FAILED_DISK 0x04 /* Permanent failure */
47 #define USABLE_DISK 0x08 /* Fully usable unless FAILED_DISK is set */
48
49 #define IMSM_DISK_FILLERS 5
50 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */
51 };
52
53 /* RAID map configuration infos. */
54 struct imsm_map {
55 __u32 pba_of_lba0; /* start address of partition */
56 __u32 blocks_per_member;/* blocks per member */
57 __u32 num_data_stripes; /* number of data stripes */
58 __u16 blocks_per_strip;
59 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
60 #define IMSM_T_STATE_NORMAL 0
61 #define IMSM_T_STATE_UNINITIALIZED 1
62 #define IMSM_T_STATE_DEGRADED 2 /* FIXME: is this correct? */
63 #define IMSM_T_STATE_FAILED 3 /* FIXME: is this correct? */
64 __u8 raid_level;
65 #define IMSM_T_RAID0 0
66 #define IMSM_T_RAID1 1
67 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
68 __u8 num_members; /* number of member disks */
69 __u8 reserved[3];
70 __u32 filler[7]; /* expansion area */
71 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
72 top byte special */
73 } __attribute__ ((packed));
74
75 struct imsm_vol {
76 __u32 reserved[2];
77 __u8 migr_state; /* Normal or Migrating */
78 __u8 migr_type; /* Initializing, Rebuilding, ... */
79 __u8 dirty;
80 __u8 fill[1];
81 __u32 filler[5];
82 struct imsm_map map[1];
83 /* here comes another one if migr_state */
84 } __attribute__ ((packed));
85
86 struct imsm_dev {
87 __u8 volume[MAX_RAID_SERIAL_LEN];
88 __u32 size_low;
89 __u32 size_high;
90 __u32 status; /* Persistent RaidDev status */
91 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
92 #define IMSM_DEV_FILLERS 12
93 __u32 filler[IMSM_DEV_FILLERS];
94 struct imsm_vol vol;
95 } __attribute__ ((packed));
96
97 struct imsm_super {
98 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
99 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
100 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
101 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
102 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
103 __u32 reserved[2]; /* 0x30 - 0x37 */
104 __u8 num_disks; /* 0x38 Number of configured disks */
105 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
106 __u8 fill[2]; /* 0x3A - 0x3B */
107 #define IMSM_FILLERS 39
108 __u32 filler[IMSM_FILLERS]; /* 0x3C - 0xD7 RAID_MPB_FILLERS */
109 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
110 /* here comes imsm_dev[num_raid_devs] */
111 } __attribute__ ((packed));
112
113 #ifndef MDASSEMBLE
114 static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
115 #endif
116
117 static unsigned int sector_count(__u32 bytes)
118 {
119 return ((bytes + (512-1)) & (~(512-1))) / 512;
120 }
121
122 static unsigned int mpb_sectors(struct imsm_super *mpb)
123 {
124 return sector_count(__le32_to_cpu(mpb->mpb_size));
125 }
126
127 /* internal representation of IMSM metadata */
128 struct intel_super {
129 union {
130 struct imsm_super *mpb;
131 void *buf;
132 };
133 int updates_pending; /* count of pending updates for mdmon */
134 int creating_imsm; /* flag to indicate container creation */
135 int creating_dev; /* index of raid device undergoing creation */
136 struct dl {
137 struct dl *next;
138 int index;
139 __u8 serial[MAX_RAID_SERIAL_LEN];
140 int major, minor;
141 char *devname;
142 int fd;
143 } *disks;
144 };
145
146 struct extent {
147 unsigned long long start, size;
148 };
149
150 static struct supertype *match_metadata_desc_imsm(char *arg)
151 {
152 struct supertype *st;
153
154 if (strcmp(arg, "imsm") != 0 &&
155 strcmp(arg, "default") != 0
156 )
157 return NULL;
158
159 st = malloc(sizeof(*st));
160 memset(st, 0, sizeof(*st));
161 st->ss = &super_imsm;
162 st->max_devs = IMSM_MAX_DEVICES;
163 st->minor_version = 0;
164 st->sb = NULL;
165 return st;
166 }
167
168 static struct supertype *match_metadata_desc_imsm_volume(char *arg)
169 {
170 struct supertype *st;
171
172 if (strcmp(arg, "imsm/volume") != 0 &&
173 strcmp(arg, "raid") != 0 &&
174 strcmp(arg, "default") != 0
175 )
176 return NULL;
177
178 st = malloc(sizeof(*st));
179 memset(st, 0, sizeof(*st));
180 st->ss = &super_imsm_volume;
181 st->max_devs = IMSM_MAX_DEVICES;
182 st->minor_version = 0;
183 st->sb = NULL;
184 return st;
185 }
186
187 static __u8 *get_imsm_version(struct imsm_super *mpb)
188 {
189 return &mpb->sig[MPB_SIG_LEN];
190 }
191
192 static struct imsm_disk *get_imsm_disk(struct imsm_super *mpb, __u8 index)
193 {
194 if (index > mpb->num_disks - 1)
195 return NULL;
196 return &mpb->disk[index];
197 }
198
199 static __u32 gen_imsm_checksum(struct imsm_super *mpb)
200 {
201 __u32 end = mpb->mpb_size / sizeof(end);
202 __u32 *p = (__u32 *) mpb;
203 __u32 sum = 0;
204
205 while (end--)
206 sum += __le32_to_cpu(*p++);
207
208 return sum - __le32_to_cpu(mpb->check_sum);
209 }
210
211 static size_t sizeof_imsm_dev(struct imsm_dev *dev)
212 {
213 size_t size = sizeof(*dev);
214
215 /* each map has disk_ord_tbl[num_members - 1] additional space */
216 size += sizeof(__u32) * (dev->vol.map[0].num_members - 1);
217
218 /* migrating means an additional map */
219 if (dev->vol.migr_state) {
220 size += sizeof(struct imsm_map);
221 size += sizeof(__u32) * (dev->vol.map[1].num_members - 1);
222 }
223
224 return size;
225 }
226
227 static struct imsm_dev *get_imsm_dev(struct imsm_super *mpb, __u8 index)
228 {
229 int offset;
230 int i;
231 void *_mpb = mpb;
232
233 if (index > mpb->num_raid_devs - 1)
234 return NULL;
235
236 /* devices start after all disks */
237 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
238
239 for (i = 0; i <= index; i++)
240 if (i == index)
241 return _mpb + offset;
242 else
243 offset += sizeof_imsm_dev(_mpb + offset);
244
245 return NULL;
246 }
247
248 static __u32 get_imsm_disk_idx(struct imsm_map *map, int slot)
249 {
250 __u32 *ord_tbl = &map->disk_ord_tbl[slot];
251
252 /* top byte is 'special' */
253 return __le32_to_cpu(*ord_tbl & ~(0xff << 24));
254 }
255
256 static int get_imsm_raid_level(struct imsm_map *map)
257 {
258 if (map->raid_level == 1) {
259 if (map->num_members == 2)
260 return 1;
261 else
262 return 10;
263 }
264
265 return map->raid_level;
266 }
267
268 static int cmp_extent(const void *av, const void *bv)
269 {
270 const struct extent *a = av;
271 const struct extent *b = bv;
272 if (a->start < b->start)
273 return -1;
274 if (a->start > b->start)
275 return 1;
276 return 0;
277 }
278
279 static struct extent *get_extents(struct intel_super *super, struct dl *dl)
280 {
281 /* find a list of used extents on the given physical device */
282 struct imsm_super *mpb = super->mpb;
283 struct imsm_disk *disk;
284 struct extent *rv, *e;
285 int i, j;
286 int memberships = 0;
287
288 disk = get_imsm_disk(mpb, dl->index);
289 if (!disk)
290 return NULL;
291
292 for (i = 0; i < mpb->num_raid_devs; i++) {
293 struct imsm_dev *dev = get_imsm_dev(mpb, i);
294 struct imsm_map *map = dev->vol.map;
295
296 for (j = 0; j < map->num_members; j++) {
297 __u32 index = get_imsm_disk_idx(map, j);
298
299 if (index == dl->index)
300 memberships++;
301 }
302 }
303 rv = malloc(sizeof(struct extent) * (memberships + 1));
304 if (!rv)
305 return NULL;
306 e = rv;
307
308 for (i = 0; i < mpb->num_raid_devs; i++) {
309 struct imsm_dev *dev = get_imsm_dev(mpb, i);
310 struct imsm_map *map = dev->vol.map;
311
312 for (j = 0; j < map->num_members; j++) {
313 __u32 index = get_imsm_disk_idx(map, j);
314
315 if (index == dl->index) {
316 e->start = __le32_to_cpu(map->pba_of_lba0);
317 e->size = __le32_to_cpu(map->blocks_per_member);
318 e++;
319 }
320 }
321 }
322 qsort(rv, memberships, sizeof(*rv), cmp_extent);
323
324 e->start = __le32_to_cpu(disk->total_blocks) -
325 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
326 e->size = 0;
327 return rv;
328 }
329
330 #ifndef MDASSEMBLE
331 static void print_imsm_dev(struct imsm_dev *dev, int index)
332 {
333 __u64 sz;
334 int slot;
335 struct imsm_map *map = dev->vol.map;
336
337 printf("\n");
338 printf("[%s]:\n", dev->volume);
339 printf(" RAID Level : %d\n", get_imsm_raid_level(map));
340 printf(" Members : %d\n", map->num_members);
341 for (slot = 0; slot < map->num_members; slot++)
342 if (index == get_imsm_disk_idx(map, slot))
343 break;
344 if (slot < map->num_members)
345 printf(" This Slot : %d\n", slot);
346 else
347 printf(" This Slot : ?\n");
348 sz = __le32_to_cpu(dev->size_high);
349 sz <<= 32;
350 sz += __le32_to_cpu(dev->size_low);
351 printf(" Array Size : %llu%s\n", (unsigned long long)sz,
352 human_size(sz * 512));
353 sz = __le32_to_cpu(map->blocks_per_member);
354 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz,
355 human_size(sz * 512));
356 printf(" Sector Offset : %u\n",
357 __le32_to_cpu(map->pba_of_lba0));
358 printf(" Num Stripes : %u\n",
359 __le32_to_cpu(map->num_data_stripes));
360 printf(" Chunk Size : %u KiB\n",
361 __le16_to_cpu(map->blocks_per_strip) / 2);
362 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
363 printf(" Migrate State : %s\n", dev->vol.migr_state ? "migrating" : "idle");
364 printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
365 printf(" Map State : %s\n", map_state_str[map->map_state]);
366 }
367
368 static void print_imsm_disk(struct imsm_super *mpb, int index)
369 {
370 struct imsm_disk *disk = get_imsm_disk(mpb, index);
371 char str[MAX_RAID_SERIAL_LEN];
372 __u32 s;
373 __u64 sz;
374
375 printf("\n");
376 snprintf(str, MAX_RAID_SERIAL_LEN, "%s", disk->serial);
377 printf(" Disk%02d Serial : %s\n", index, str);
378 s = __le32_to_cpu(disk->status);
379 printf(" State :%s%s%s%s\n", s&SPARE_DISK ? " spare" : "",
380 s&CONFIGURED_DISK ? " active" : "",
381 s&FAILED_DISK ? " failed" : "",
382 s&USABLE_DISK ? " usable" : "");
383 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
384 sz = __le32_to_cpu(disk->total_blocks) -
385 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS * mpb->num_raid_devs);
386 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
387 human_size(sz * 512));
388 }
389
390 static void examine_super_imsm(struct supertype *st, char *homehost)
391 {
392 struct intel_super *super = st->sb;
393 struct imsm_super *mpb = super->mpb;
394 char str[MAX_SIGNATURE_LENGTH];
395 int i;
396 __u32 sum;
397
398 snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
399 printf(" Magic : %s\n", str);
400 snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
401 printf(" Version : %s\n", get_imsm_version(mpb));
402 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
403 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
404 sum = __le32_to_cpu(mpb->check_sum);
405 printf(" Checksum : %08x %s\n", sum,
406 gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
407 printf(" MPB Sectors : %d\n", mpb_sectors(mpb));
408 printf(" Disks : %d\n", mpb->num_disks);
409 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
410 print_imsm_disk(mpb, super->disks->index);
411 for (i = 0; i < mpb->num_raid_devs; i++)
412 print_imsm_dev(get_imsm_dev(mpb, i), super->disks->index);
413 for (i = 0; i < mpb->num_disks; i++) {
414 if (i == super->disks->index)
415 continue;
416 print_imsm_disk(mpb, i);
417 }
418 }
419
420 static void brief_examine_super_imsm(struct supertype *st)
421 {
422 struct intel_super *super = st->sb;
423 struct imsm_super *mpb = super->mpb;
424
425 printf("ARRAY /dev/imsm family=%08x metadata=external:imsm\n",
426 __le32_to_cpu(mpb->family_num));
427 }
428
429 static void detail_super_imsm(struct supertype *st, char *homehost)
430 {
431 printf("%s\n", __FUNCTION__);
432 }
433
434 static void brief_detail_super_imsm(struct supertype *st)
435 {
436 printf("%s\n", __FUNCTION__);
437 }
438 #endif
439
440 static int match_home_imsm(struct supertype *st, char *homehost)
441 {
442 printf("%s\n", __FUNCTION__);
443
444 return 0;
445 }
446
447 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
448 {
449 printf("%s\n", __FUNCTION__);
450 }
451
452 static void
453 get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
454 {
455 __u8 *v = get_imsm_version(mpb);
456 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
457 char major[] = { 0, 0, 0 };
458 char minor[] = { 0 ,0, 0 };
459 char patch[] = { 0, 0, 0 };
460 char *ver_parse[] = { major, minor, patch };
461 int i, j;
462
463 i = j = 0;
464 while (*v != '\0' && v < end) {
465 if (*v != '.' && j < 2)
466 ver_parse[i][j++] = *v;
467 else {
468 i++;
469 j = 0;
470 }
471 v++;
472 }
473
474 *m = strtol(minor, NULL, 0);
475 *p = strtol(patch, NULL, 0);
476 }
477
478 static int imsm_level_to_layout(int level)
479 {
480 switch (level) {
481 case 0:
482 case 1:
483 return 0;
484 case 5:
485 case 6:
486 return ALGORITHM_LEFT_SYMMETRIC;
487 case 10:
488 return 0x102; //FIXME is this correct?
489 }
490 return -1;
491 }
492
493 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
494 {
495 struct intel_super *super = st->sb;
496 struct imsm_super *mpb = super->mpb;
497 struct imsm_disk *disk;
498 __u32 s;
499
500 info->array.major_version = 2000;
501 get_imsm_numerical_version(mpb, &info->array.minor_version,
502 &info->array.patch_version);
503 info->array.raid_disks = mpb->num_disks;
504 info->array.level = LEVEL_CONTAINER;
505 info->array.layout = 0;
506 info->array.md_minor = -1;
507 info->array.ctime = 0; /* N/A for imsm */
508 info->array.utime = 0;
509 info->array.chunk_size = 0;
510
511 info->disk.major = 0;
512 info->disk.minor = 0;
513 info->disk.raid_disk = -1;
514 info->reshape_active = 0;
515 strcpy(info->text_version, "imsm");
516 info->disk.number = -1;
517 info->disk.state = 0;
518
519 if (super->disks) {
520 info->disk.number = super->disks->index;
521 info->disk.raid_disk = super->disks->index;
522 disk = get_imsm_disk(mpb, super->disks->index);
523 s = __le32_to_cpu(disk->status);
524 info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
525 info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
526 info->disk.state |= s & USABLE_DISK ? (1 << MD_DISK_SYNC) : 0;
527 }
528 }
529
530 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info)
531 {
532 struct intel_super *super = st->sb;
533 struct imsm_super *mpb = super->mpb;
534 struct imsm_dev *dev = get_imsm_dev(mpb, info->container_member);
535 struct imsm_map *map = &dev->vol.map[0];
536
537 info->array.major_version = 2000;
538 get_imsm_numerical_version(mpb, &info->array.minor_version,
539 &info->array.patch_version);
540 info->array.raid_disks = map->num_members;
541 info->array.level = get_imsm_raid_level(map);
542 info->array.layout = imsm_level_to_layout(info->array.level);
543 info->array.md_minor = -1;
544 info->array.ctime = 0;
545 info->array.utime = 0;
546 info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip * 512);
547
548 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
549 info->component_size = __le32_to_cpu(map->blocks_per_member);
550
551 info->disk.major = 0;
552 info->disk.minor = 0;
553
554 sprintf(info->text_version, "/%s/%d",
555 devnum2devname(st->container_dev),
556 info->container_member);
557 }
558
559 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
560 char *update, char *devname, int verbose,
561 int uuid_set, char *homehost)
562 {
563 /* FIXME */
564
565 /* For 'assemble' and 'force' we need to return non-zero if any
566 * change was made. For others, the return value is ignored.
567 * Update options are:
568 * force-one : This device looks a bit old but needs to be included,
569 * update age info appropriately.
570 * assemble: clear any 'faulty' flag to allow this device to
571 * be assembled.
572 * force-array: Array is degraded but being forced, mark it clean
573 * if that will be needed to assemble it.
574 *
575 * newdev: not used ????
576 * grow: Array has gained a new device - this is currently for
577 * linear only
578 * resync: mark as dirty so a resync will happen.
579 * name: update the name - preserving the homehost
580 *
581 * Following are not relevant for this imsm:
582 * sparc2.2 : update from old dodgey metadata
583 * super-minor: change the preferred_minor number
584 * summaries: update redundant counters.
585 * uuid: Change the uuid of the array to match watch is given
586 * homehost: update the recorded homehost
587 * _reshape_progress: record new reshape_progress position.
588 */
589 int rv = 0;
590 //struct intel_super *super = st->sb;
591 //struct imsm_super *mpb = super->mpb;
592
593 if (strcmp(update, "grow") == 0) {
594 }
595 if (strcmp(update, "resync") == 0) {
596 /* dev->vol.dirty = 1; */
597 }
598
599 /* IMSM has no concept of UUID or homehost */
600
601 return rv;
602 }
603
604 static size_t disks_to_mpb_size(int disks)
605 {
606 size_t size;
607
608 size = sizeof(struct imsm_super);
609 size += (disks - 1) * sizeof(struct imsm_disk);
610 size += 2 * sizeof(struct imsm_dev);
611 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
612 size += (4 - 2) * sizeof(struct imsm_map);
613 /* 4 possible disk_ord_tbl's */
614 size += 4 * (disks - 1) * sizeof(__u32);
615
616 return size;
617 }
618
619 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
620 {
621 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
622 return 0;
623
624 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
625 }
626
627 static int compare_super_imsm(struct supertype *st, struct supertype *tst)
628 {
629 /*
630 * return:
631 * 0 same, or first was empty, and second was copied
632 * 1 second had wrong number
633 * 2 wrong uuid
634 * 3 wrong other info
635 */
636 struct intel_super *first = st->sb;
637 struct intel_super *sec = tst->sb;
638
639 if (!first) {
640 st->sb = tst->sb;
641 tst->sb = NULL;
642 return 0;
643 }
644
645 if (memcmp(first->mpb->sig, sec->mpb->sig, MAX_SIGNATURE_LENGTH) != 0)
646 return 3;
647 if (first->mpb->family_num != sec->mpb->family_num)
648 return 3;
649 if (first->mpb->mpb_size != sec->mpb->mpb_size)
650 return 3;
651 if (first->mpb->check_sum != sec->mpb->check_sum)
652 return 3;
653
654 return 0;
655 }
656
657 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
658
659 static int imsm_read_serial(int fd, char *devname,
660 __u8 serial[MAX_RAID_SERIAL_LEN])
661 {
662 unsigned char scsi_serial[255];
663 int sg_fd;
664 int rv;
665 int rsp_len;
666 int i, cnt;
667
668 memset(scsi_serial, 0, sizeof(scsi_serial));
669
670 sg_fd = sysfs_disk_to_sg(fd);
671 if (sg_fd < 0) {
672 if (devname)
673 fprintf(stderr,
674 Name ": Failed to open sg interface for %s: %s\n",
675 devname, strerror(errno));
676 return 1;
677 }
678
679 rv = scsi_get_serial(sg_fd, scsi_serial, sizeof(scsi_serial));
680 close(sg_fd);
681
682 if (rv != 0) {
683 if (devname)
684 fprintf(stderr,
685 Name ": Failed to retrieve serial for %s\n",
686 devname);
687 return rv;
688 }
689
690 rsp_len = scsi_serial[3];
691 for (i = 0, cnt = 0; i < rsp_len; i++) {
692 if (!isspace(scsi_serial[4 + i]))
693 serial[cnt++] = scsi_serial[4 + i];
694 if (cnt == MAX_RAID_SERIAL_LEN)
695 break;
696 }
697
698 serial[MAX_RAID_SERIAL_LEN - 1] = '\0';
699
700 return 0;
701 }
702
703 static int
704 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
705 {
706 struct imsm_super *mpb = super->mpb;
707 struct dl *dl;
708 struct stat stb;
709 struct imsm_disk *disk;
710 int rv;
711 int i;
712
713 dl = malloc(sizeof(*dl));
714 if (!dl) {
715 if (devname)
716 fprintf(stderr,
717 Name ": failed to allocate disk buffer for %s\n",
718 devname);
719 return 2;
720 }
721 memset(dl, 0, sizeof(*dl));
722
723 fstat(fd, &stb);
724 dl->major = major(stb.st_rdev);
725 dl->minor = minor(stb.st_rdev);
726 dl->next = super->disks;
727 dl->fd = keep_fd ? fd : -1;
728 dl->devname = devname ? strdup(devname) : NULL;
729 dl->index = -1;
730 super->disks = dl;
731 rv = imsm_read_serial(fd, devname, dl->serial);
732
733 if (rv != 0)
734 return 2;
735
736 /* look up this disk's index */
737 for (i = 0; i < mpb->num_disks; i++) {
738 disk = get_imsm_disk(mpb, i);
739
740 if (memcmp(disk->serial, dl->serial, MAX_RAID_SERIAL_LEN) == 0)
741 break;
742 }
743
744 if (i > mpb->num_disks)
745 return 2;
746
747 dl->index = i;
748
749 return 0;
750 }
751
752 /* load_imsm_mpb - read matrix metadata
753 * allocates super->mpb to be freed by free_super
754 */
755 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
756 {
757 unsigned long long dsize;
758 size_t len, mpb_size;
759 unsigned long long sectors;
760 struct stat;
761 struct imsm_super anchor;
762 __u32 check_sum;
763
764 memset(super, 0, sizeof(*super));
765 get_dev_size(fd, NULL, &dsize);
766
767 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
768 if (devname)
769 fprintf(stderr,
770 Name ": Cannot seek to anchor block on %s: %s\n",
771 devname, strerror(errno));
772 return 1;
773 }
774
775 len = sizeof(anchor);
776 if (read(fd, &anchor, len) != len) {
777 if (devname)
778 fprintf(stderr,
779 Name ": Cannot read anchor block on %s: %s\n",
780 devname, strerror(errno));
781 return 1;
782 }
783
784 if (strncmp((char *) anchor.sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
785 if (devname)
786 fprintf(stderr,
787 Name ": no IMSM anchor on %s\n", devname);
788 return 2;
789 }
790
791 mpb_size = __le32_to_cpu(anchor.mpb_size);
792 super->mpb = malloc(mpb_size < 512 ? 512 : mpb_size);
793 if (!super->mpb) {
794 if (devname)
795 fprintf(stderr,
796 Name ": unable to allocate %zu byte mpb buffer\n",
797 mpb_size);
798 return 2;
799 }
800 memcpy(super->buf, &anchor, sizeof(anchor));
801
802 /* read the rest of the first block */
803 len = 512 - sizeof(anchor);
804 if (read(fd, super->buf + sizeof(anchor), len) != len) {
805 if (devname)
806 fprintf(stderr,
807 Name ": Cannot read anchor remainder on %s: %s\n",
808 devname, strerror(errno));
809 return 2;
810 }
811
812 sectors = mpb_sectors(&anchor) - 1;
813 if (!sectors)
814 return load_imsm_disk(fd, super, devname, 0);
815
816 /* read the extended mpb */
817 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
818 if (devname)
819 fprintf(stderr,
820 Name ": Cannot seek to extended mpb on %s: %s\n",
821 devname, strerror(errno));
822 return 1;
823 }
824
825 len = mpb_size - 512;
826 if (read(fd, super->buf + 512, len) != len) {
827 if (devname)
828 fprintf(stderr,
829 Name ": Cannot read extended mpb on %s: %s\n",
830 devname, strerror(errno));
831 return 2;
832 }
833
834 check_sum = gen_imsm_checksum(super->mpb);
835 if (check_sum != __le32_to_cpu(super->mpb->check_sum)) {
836 if (devname)
837 fprintf(stderr,
838 Name ": IMSM checksum %x != %x on %s\n",
839 check_sum, __le32_to_cpu(super->mpb->check_sum),
840 devname);
841 return 2;
842 }
843
844 return load_imsm_disk(fd, super, devname, 0);
845 }
846
847 struct superswitch super_imsm_container;
848
849 static void free_imsm_disks(struct intel_super *super)
850 {
851 while (super->disks) {
852 struct dl *d = super->disks;
853
854 super->disks = d->next;
855 if (d->fd >= 0)
856 close(d->fd);
857 if (d->devname)
858 free(d->devname);
859 free(d);
860 }
861 }
862
863 static void free_imsm(struct intel_super *super)
864 {
865 if (super->mpb)
866 free(super->mpb);
867 free_imsm_disks(super);
868 free(super);
869 }
870
871
872 static void free_super_imsm(struct supertype *st)
873 {
874 struct intel_super *super = st->sb;
875
876 if (!super)
877 return;
878
879 free_imsm(super);
880 st->sb = NULL;
881 }
882
883 static struct intel_super *alloc_super(int creating_imsm)
884 {
885 struct intel_super *super = malloc(sizeof(*super));
886
887 if (super) {
888 memset(super, 0, sizeof(*super));
889 super->creating_imsm = creating_imsm;
890 super->creating_dev = -1;
891 }
892
893 return super;
894 }
895
896 #ifndef MDASSEMBLE
897 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
898 char *devname, int keep_fd)
899 {
900 struct mdinfo *sra;
901 struct intel_super *super;
902 struct mdinfo *sd, *best = NULL;
903 __u32 bestgen = 0;
904 __u32 gen;
905 char nm[20];
906 int dfd;
907 int rv;
908
909 /* check if this disk is a member of an active array */
910 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
911 if (!sra)
912 return 1;
913
914 if (sra->array.major_version != -1 ||
915 sra->array.minor_version != -2 ||
916 strcmp(sra->text_version, "imsm") != 0)
917 return 1;
918
919 super = alloc_super(0);
920 if (!super)
921 return 1;
922
923 /* find the most up to date disk in this array */
924 for (sd = sra->devs; sd; sd = sd->next) {
925 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
926 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
927 if (!dfd) {
928 free_imsm(super);
929 return 2;
930 }
931 rv = load_imsm_mpb(dfd, super, NULL);
932 if (!keep_fd)
933 close(dfd);
934 if (rv == 0) {
935 gen = __le32_to_cpu(super->mpb->generation_num);
936 if (!best || gen > bestgen) {
937 bestgen = gen;
938 best = sd;
939 }
940 } else {
941 free_imsm(super);
942 return 2;
943 }
944 }
945
946 if (!best) {
947 free_imsm(super);
948 return 1;
949 }
950
951 /* load the most up to date anchor */
952 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
953 dfd = dev_open(nm, O_RDONLY);
954 if (!dfd) {
955 free_imsm(super);
956 return 1;
957 }
958 rv = load_imsm_mpb(dfd, super, NULL);
959 close(dfd);
960 if (rv != 0) {
961 free_imsm(super);
962 return 2;
963 }
964
965 /* reset the disk list */
966 free_imsm_disks(super);
967
968 /* populate disk list */
969 for (sd = sra->devs ; sd ; sd = sd->next) {
970 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
971 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
972 if (!dfd) {
973 free_imsm(super);
974 return 2;
975 }
976 load_imsm_disk(dfd, super, NULL, keep_fd);
977 if (!keep_fd)
978 close(dfd);
979 }
980
981 *sbp = super;
982 if (st->ss == NULL) {
983 st->ss = &super_imsm_container;
984 st->minor_version = 0;
985 st->max_devs = IMSM_MAX_DEVICES;
986 st->container_dev = fd2devnum(fd);
987 }
988
989 return 0;
990 }
991 #endif
992
993 static int load_super_imsm(struct supertype *st, int fd, char *devname)
994 {
995 struct intel_super *super;
996 int rv;
997
998 #ifndef MDASSEMBLE
999 if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0)
1000 return 0;
1001 #endif
1002
1003 super = alloc_super(0);
1004 if (!super) {
1005 fprintf(stderr,
1006 Name ": malloc of %zu failed.\n",
1007 sizeof(*super));
1008 return 1;
1009 }
1010
1011 rv = load_imsm_mpb(fd, super, devname);
1012
1013 if (rv) {
1014 if (devname)
1015 fprintf(stderr,
1016 Name ": Failed to load all information "
1017 "sections on %s\n", devname);
1018 free_imsm(super);
1019 return rv;
1020 }
1021
1022 st->sb = super;
1023 if (st->ss == NULL) {
1024 st->ss = &super_imsm;
1025 st->minor_version = 0;
1026 st->max_devs = IMSM_MAX_DEVICES;
1027 }
1028
1029 return 0;
1030 }
1031
1032 static int init_zero_imsm(struct supertype *st, mdu_array_info_t *info,
1033 unsigned long long size, char *name,
1034 char *homehost, int *uuid)
1035 {
1036 st->sb = NULL;
1037 return 0;
1038 }
1039
1040 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
1041 unsigned long long size, char *name,
1042 char *homehost, int *uuid)
1043 {
1044 /* This is primarily called by Create when creating a new array.
1045 * We will then get add_to_super called for each component, and then
1046 * write_init_super called to write it out to each device.
1047 * For IMSM, Create can create on fresh devices or on a pre-existing
1048 * array.
1049 * To create on a pre-existing array a different method will be called.
1050 * This one is just for fresh drives.
1051 */
1052 struct intel_super *super;
1053 struct imsm_super *mpb;
1054 size_t mpb_size;
1055
1056 super = alloc_super(1);
1057 if (!super)
1058 return 0;
1059 mpb_size = disks_to_mpb_size(info->nr_disks);
1060 mpb = malloc(mpb_size);
1061 if (!mpb) {
1062 free(super);
1063 return 0;
1064 }
1065 memset(mpb, 0, mpb_size);
1066
1067 memcpy(mpb->sig, MPB_SIGNATURE, strlen(MPB_SIGNATURE));
1068 memcpy(mpb->sig + strlen(MPB_SIGNATURE), MPB_VERSION_RAID5,
1069 strlen(MPB_VERSION_RAID5));
1070 mpb->mpb_size = mpb_size;
1071
1072 super->mpb = mpb;
1073 st->sb = super;
1074 return 1;
1075 }
1076
1077 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
1078 unsigned long long size, char *name,
1079 char *homehost, int *uuid)
1080 {
1081 /* We are creating a volume inside a pre-existing container.
1082 * so st->sb is already set.
1083 */
1084 struct intel_super *super = st->sb;
1085 struct imsm_super *mpb = super->mpb;
1086 struct imsm_dev *dev;
1087 struct imsm_vol *vol;
1088 struct imsm_map *map;
1089 int idx = mpb->num_raid_devs;
1090 int i;
1091 unsigned long long array_blocks;
1092 unsigned long long sz;
1093 __u32 offset = 0;
1094
1095 if (mpb->num_raid_devs >= 2) {
1096 fprintf(stderr, Name": This imsm-container already has the "
1097 "maximum of 2 volumes\n");
1098 return 0;
1099 }
1100
1101 super->creating_dev = idx;
1102 mpb->num_raid_devs++;
1103 dev = get_imsm_dev(mpb, idx);
1104 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
1105 array_blocks = calc_array_size(info->level, info->raid_disks,
1106 info->layout, info->chunk_size,
1107 info->size*2);
1108 dev->size_low = __cpu_to_le32((__u32) array_blocks);
1109 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
1110 dev->status = __cpu_to_le32(0);
1111 dev->reserved_blocks = __cpu_to_le32(0);
1112 vol = &dev->vol;
1113 vol->migr_state = 0;
1114 vol->migr_type = 0;
1115 vol->dirty = 0;
1116 for (i = 0; i < idx; i++) {
1117 struct imsm_dev *prev = get_imsm_dev(mpb, i);
1118 struct imsm_map *pmap = &prev->vol.map[0];
1119
1120 offset += __le32_to_cpu(pmap->blocks_per_member);
1121 offset += IMSM_RESERVED_SECTORS;
1122 }
1123 map = &vol->map[0];
1124 map->pba_of_lba0 = __cpu_to_le32(offset);
1125 sz = info->size * 2;
1126 map->blocks_per_member = __cpu_to_le32(sz);
1127 map->blocks_per_strip = __cpu_to_le16(info->chunk_size >> 9);
1128 map->num_data_stripes = __cpu_to_le32(sz / (info->chunk_size >> 9));
1129 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
1130 IMSM_T_STATE_NORMAL;
1131 if (info->level == 10)
1132 map->raid_level = 1;
1133 else
1134 map->raid_level = info->level;
1135 map->num_members = info->raid_disks;
1136 for (i = 0; i < map->num_members; i++) {
1137 /* initialized in add_to_super */
1138 map->disk_ord_tbl[i] = __cpu_to_le32(0);
1139 }
1140
1141 return 1;
1142 }
1143
1144 static void add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
1145 int fd, char *devname)
1146 {
1147 struct intel_super *super = st->sb;
1148 struct imsm_super *mpb = super->mpb;
1149 struct imsm_disk *disk;
1150 struct dl *dd;
1151 unsigned long long size;
1152 __u32 status, id;
1153 int rv;
1154 struct stat stb;
1155
1156 fstat(fd, &stb);
1157 dd = malloc(sizeof(*dd));
1158 if (!dd) {
1159 fprintf(stderr,
1160 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
1161 abort();
1162 }
1163 memset(dd, 0, sizeof(*dd));
1164 dd->major = major(stb.st_rdev);
1165 dd->minor = minor(stb.st_rdev);
1166 dd->index = dk->number;
1167 dd->devname = devname ? strdup(devname) : NULL;
1168 dd->next = super->disks;
1169 dd->fd = fd;
1170 rv = imsm_read_serial(fd, devname, dd->serial);
1171 if (rv) {
1172 fprintf(stderr,
1173 Name ": failed to retrieve scsi serial "
1174 "using \'%s\' instead\n", devname);
1175 strcpy((char *) dd->serial, devname);
1176 }
1177
1178 if (mpb->num_disks <= dk->number)
1179 mpb->num_disks = dk->number + 1;
1180
1181 disk = get_imsm_disk(mpb, dk->number);
1182 get_dev_size(fd, NULL, &size);
1183 size /= 512;
1184 status = USABLE_DISK | SPARE_DISK;
1185 strcpy((char *) disk->serial, (char *) dd->serial);
1186 disk->total_blocks = __cpu_to_le32(size);
1187 disk->status = __cpu_to_le32(status);
1188 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
1189 disk->scsi_id = __cpu_to_le32(id);
1190 else
1191 disk->scsi_id = __cpu_to_le32(0);
1192
1193 /* update the family number if we are creating a container */
1194 if (super->creating_imsm)
1195 mpb->family_num = __cpu_to_le32(gen_imsm_checksum(mpb));
1196
1197 super->disks = dd;
1198 }
1199
1200 static void add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
1201 int fd, char *devname)
1202 {
1203 struct intel_super *super = st->sb;
1204 struct imsm_super *mpb = super->mpb;
1205 struct dl *dl;
1206 struct imsm_dev *dev;
1207 struct imsm_map *map;
1208 struct imsm_disk *disk;
1209 __u32 status;
1210
1211 if (super->creating_dev == -1) {
1212 fprintf(stderr, Name ": no active raid device\n");
1213 abort();
1214 }
1215
1216 dev = get_imsm_dev(mpb, super->creating_dev);
1217 map = &dev->vol.map[0];
1218
1219 for (dl = super->disks; dl ; dl = dl->next)
1220 if (dl->major == dk->major &&
1221 dl->minor == dk->minor)
1222 break;
1223 if (!dl || ! (dk->state & (1<<MD_DISK_SYNC)))
1224 return;
1225
1226 map->disk_ord_tbl[dk->number] = __cpu_to_le32(dl->index);
1227
1228 disk = get_imsm_disk(mpb, dl->index);
1229 status = CONFIGURED_DISK | USABLE_DISK;
1230 disk->status = __cpu_to_le32(status);
1231 }
1232
1233 static int store_imsm_mpb(int fd, struct intel_super *super);
1234
1235 static int write_super_imsm(struct intel_super *super, int doclose)
1236 {
1237 struct imsm_super *mpb = super->mpb;
1238 struct dl *d;
1239 __u32 generation;
1240 __u32 sum;
1241
1242 /* 'generation' is incremented everytime the metadata is written */
1243 generation = __le32_to_cpu(mpb->generation_num);
1244 generation++;
1245 mpb->generation_num = __cpu_to_le32(generation);
1246
1247 /* recalculate checksum */
1248 sum = gen_imsm_checksum(mpb);
1249 mpb->check_sum = __cpu_to_le32(sum);
1250
1251 for (d = super->disks; d ; d = d->next) {
1252 if (store_imsm_mpb(d->fd, super)) {
1253 fprintf(stderr, "%s: failed for device %d:%d %s\n",
1254 __func__, d->major, d->minor, strerror(errno));
1255 return 0;
1256 }
1257 if (doclose) {
1258 close(d->fd);
1259 d->fd = -1;
1260 }
1261 }
1262
1263 return 1;
1264 }
1265
1266 static int write_init_super_imsm(struct supertype *st)
1267 {
1268 return write_super_imsm(st->sb, 1);
1269 }
1270
1271 static int store_zero_imsm(struct supertype *st, int fd)
1272 {
1273 unsigned long long dsize;
1274 char buf[512];
1275
1276 get_dev_size(fd, NULL, &dsize);
1277
1278 /* first block is stored on second to last sector of the disk */
1279 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
1280 return 1;
1281
1282 memset(buf, 0, sizeof(buf));
1283 if (write(fd, buf, sizeof(buf)) != sizeof(buf))
1284 return 1;
1285
1286 return 0;
1287 }
1288
1289 static void getinfo_super_n_imsm_container(struct supertype *st, struct mdinfo *info)
1290 {
1291 /* just need offset and size...
1292 * of the metadata
1293 */
1294 struct intel_super *super = st->sb;
1295 struct imsm_super *mpb = super->mpb;
1296 struct imsm_disk *disk = get_imsm_disk(mpb, info->disk.number);
1297
1298 info->data_offset = __le32_to_cpu(disk->total_blocks) -
1299 (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
1300 info->component_size = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1301 }
1302
1303 static void getinfo_super_n_imsm_volume(struct supertype *st, struct mdinfo *info)
1304 {
1305 /* Find the particular details for info->disk.raid_disk.
1306 * This includes data_offset, component_size,
1307 */
1308 struct intel_super *super = st->sb;
1309 struct imsm_super *mpb = super->mpb;
1310 struct imsm_dev *dev = get_imsm_dev(mpb, super->creating_dev);
1311 struct imsm_map *map = &dev->vol.map[0];
1312
1313 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
1314 info->component_size = __le32_to_cpu(map->blocks_per_member);
1315 }
1316
1317 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
1318 int raiddisks, int chunk, unsigned long long size,
1319 char *dev, unsigned long long *freesize)
1320 {
1321 int fd, cfd;
1322 struct mdinfo *sra;
1323
1324 /* if given unused devices create a container
1325 * if given given devices in a container create a member volume
1326 */
1327 if (level == LEVEL_CONTAINER) {
1328 st->ss = &super_imsm_container;
1329 if (dev) {
1330 /* validate the container, dev == NULL */
1331 int rv = st->ss->validate_geometry(st, level, layout,
1332 raiddisks, chunk,
1333 size,
1334 NULL, freesize);
1335 if (rv)
1336 return rv;
1337 }
1338 return st->ss->validate_geometry(st, level, layout, raiddisks,
1339 chunk, size, dev, freesize);
1340 }
1341
1342 if (st->sb) {
1343 /* creating in a given container */
1344 st->ss = &super_imsm_volume;
1345 if (dev) {
1346 int rv = st->ss->validate_geometry(st, level, layout,
1347 raiddisks, chunk,
1348 size,
1349 NULL, freesize);
1350 if (rv)
1351 return rv;
1352 }
1353 return st->ss->validate_geometry(st, level, layout, raiddisks,
1354 chunk, size, dev, freesize);
1355 }
1356
1357 /* limit creation to the following levels */
1358 if (!dev)
1359 switch (level) {
1360 case 0:
1361 case 1:
1362 case 10:
1363 case 5:
1364 break;
1365 default:
1366 return 1;
1367 }
1368
1369 /* This device needs to be a device in an 'imsm' container */
1370 fd = open(dev, O_RDONLY|O_EXCL, 0);
1371 if (fd >= 0) {
1372 fprintf(stderr,
1373 Name ": Cannot create this array on device %s\n",
1374 dev);
1375 close(fd);
1376 return 0;
1377 }
1378 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
1379 fprintf(stderr, Name ": Cannot open %s: %s\n",
1380 dev, strerror(errno));
1381 return 0;
1382 }
1383 /* Well, it is in use by someone, maybe an 'imsm' container. */
1384 cfd = open_container(fd);
1385 if (cfd < 0) {
1386 close(fd);
1387 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
1388 dev);
1389 return 0;
1390 }
1391 sra = sysfs_read(cfd, 0, GET_VERSION);
1392 close(fd);
1393 if (sra && sra->array.major_version == -1 &&
1394 strcmp(sra->text_version, "imsm") == 0) {
1395 /* This is a member of a imsm container. Load the container
1396 * and try to create a volume
1397 */
1398 struct intel_super *super;
1399 st->ss = &super_imsm_volume;
1400 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, 1) == 0) {
1401 st->sb = super;
1402 st->container_dev = fd2devnum(cfd);
1403 close(cfd);
1404 return st->ss->validate_geometry(st, level, layout,
1405 raiddisks, chunk, size,
1406 dev, freesize);
1407 }
1408 close(cfd);
1409 } else /* may belong to another container */
1410 return 0;
1411
1412 return 1;
1413 }
1414
1415 static int validate_geometry_imsm_container(struct supertype *st, int level,
1416 int layout, int raiddisks, int chunk,
1417 unsigned long long size, char *dev,
1418 unsigned long long *freesize)
1419 {
1420 int fd;
1421 unsigned long long ldsize;
1422
1423 if (level != LEVEL_CONTAINER)
1424 return 0;
1425 if (!dev)
1426 return 1;
1427
1428 fd = open(dev, O_RDONLY|O_EXCL, 0);
1429 if (fd < 0) {
1430 fprintf(stderr, Name ": Cannot open %s: %s\n",
1431 dev, strerror(errno));
1432 return 0;
1433 }
1434 if (!get_dev_size(fd, dev, &ldsize)) {
1435 close(fd);
1436 return 0;
1437 }
1438 close(fd);
1439
1440 *freesize = avail_size_imsm(st, ldsize >> 9);
1441
1442 return 1;
1443 }
1444
1445 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
1446 * FIX ME add ahci details
1447 */
1448 static int validate_geometry_imsm_volume(struct supertype *st, int level,
1449 int layout, int raiddisks, int chunk,
1450 unsigned long long size, char *dev,
1451 unsigned long long *freesize)
1452 {
1453 struct stat stb;
1454 struct intel_super *super = st->sb;
1455 struct dl *dl;
1456 unsigned long long pos = 0;
1457 unsigned long long maxsize;
1458 struct extent *e;
1459 int i;
1460
1461 if (level == LEVEL_CONTAINER)
1462 return 0;
1463
1464 if (level == 1 && raiddisks > 2) {
1465 fprintf(stderr, Name ": imsm does not support more than 2 "
1466 "in a raid1 configuration\n");
1467 return 0;
1468 }
1469
1470 /* We must have the container info already read in. */
1471 if (!super)
1472 return 0;
1473
1474 if (!dev) {
1475 /* General test: make sure there is space for
1476 * 'raiddisks' device extents of size 'size'.
1477 */
1478 unsigned long long minsize = size*2 /* convert to blocks */;
1479 int dcnt = 0;
1480 if (minsize == 0)
1481 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
1482 for (dl = super->disks; dl ; dl = dl->next) {
1483 int found = 0;
1484
1485 i = 0;
1486 e = get_extents(super, dl);
1487 if (!e) continue;
1488 do {
1489 unsigned long long esize;
1490 esize = e[i].start - pos;
1491 if (esize >= minsize)
1492 found = 1;
1493 pos = e[i].start + e[i].size;
1494 i++;
1495 } while (e[i-1].size);
1496 if (found)
1497 dcnt++;
1498 free(e);
1499 }
1500 if (dcnt < raiddisks) {
1501 fprintf(stderr, Name ": Not enough devices with space "
1502 "for this array (%d < %d)\n",
1503 dcnt, raiddisks);
1504 return 0;
1505 }
1506 return 1;
1507 }
1508 /* This device must be a member of the set */
1509 if (stat(dev, &stb) < 0)
1510 return 0;
1511 if ((S_IFMT & stb.st_mode) != S_IFBLK)
1512 return 0;
1513 for (dl = super->disks ; dl ; dl = dl->next) {
1514 if (dl->major == major(stb.st_rdev) &&
1515 dl->minor == minor(stb.st_rdev))
1516 break;
1517 }
1518 if (!dl) {
1519 fprintf(stderr, Name ": %s is not in the same imsm set\n",
1520 dev);
1521 return 0;
1522 }
1523 e = get_extents(super, dl);
1524 maxsize = 0;
1525 i = 0;
1526 if (e) do {
1527 unsigned long long esize;
1528 esize = e[i].start - pos;
1529 if (esize >= maxsize)
1530 maxsize = esize;
1531 pos = e[i].start + e[i].size;
1532 i++;
1533 } while (e[i-1].size);
1534 *freesize = maxsize;
1535
1536 return 1;
1537 }
1538
1539 static struct mdinfo *container_content_imsm(struct supertype *st)
1540 {
1541 /* Given a container loaded by load_super_imsm_all,
1542 * extract information about all the arrays into
1543 * an mdinfo tree.
1544 *
1545 * For each imsm_dev create an mdinfo, fill it in,
1546 * then look for matching devices in super->disks
1547 * and create appropriate device mdinfo.
1548 */
1549 struct intel_super *super = st->sb;
1550 struct imsm_super *mpb = super->mpb;
1551 struct mdinfo *rest = NULL;
1552 int i;
1553
1554 for (i = 0; i < mpb->num_raid_devs; i++) {
1555 struct imsm_dev *dev = get_imsm_dev(mpb, i);
1556 struct imsm_vol *vol = &dev->vol;
1557 struct imsm_map *map = vol->map;
1558 struct mdinfo *this;
1559 __u64 sz;
1560 int slot;
1561
1562 this = malloc(sizeof(*this));
1563 memset(this, 0, sizeof(*this));
1564 this->next = rest;
1565 rest = this;
1566
1567 this->array.major_version = 2000;
1568 get_imsm_numerical_version(mpb, &this->array.minor_version,
1569 &this->array.patch_version);
1570 this->array.level = get_imsm_raid_level(map);
1571 this->array.raid_disks = map->num_members;
1572 this->array.layout = imsm_level_to_layout(this->array.level);
1573 this->array.md_minor = -1;
1574 this->array.ctime = 0;
1575 this->array.utime = 0;
1576 this->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9;
1577 this->array.state = !vol->dirty;
1578 this->container_member = i;
1579 if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty)
1580 this->resync_start = 0;
1581 else
1582 this->resync_start = ~0ULL;
1583
1584 strncpy(this->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1585 this->name[MAX_RAID_SERIAL_LEN] = 0;
1586
1587 sprintf(this->text_version, "/%s/%d",
1588 devnum2devname(st->container_dev),
1589 this->container_member);
1590
1591 memset(this->uuid, 0, sizeof(this->uuid));
1592
1593 sz = __le32_to_cpu(dev->size_high);
1594 sz <<= 32;
1595 sz += __le32_to_cpu(dev->size_low);
1596 this->component_size = sz;
1597 this->array.size = this->component_size / 2;
1598
1599 for (slot = 0 ; slot < map->num_members; slot++) {
1600 struct imsm_disk *disk;
1601 struct mdinfo *info_d;
1602 struct dl *d;
1603 int idx;
1604 __u32 s;
1605
1606 idx = __le32_to_cpu(map->disk_ord_tbl[slot] & ~(0xff << 24));
1607 for (d = super->disks; d ; d = d->next)
1608 if (d->index == idx)
1609 break;
1610
1611 if (d == NULL)
1612 break; /* shouldn't this be continue ?? */
1613
1614 info_d = malloc(sizeof(*info_d));
1615 if (!info_d)
1616 break; /* ditto ?? */
1617 memset(info_d, 0, sizeof(*info_d));
1618 info_d->next = this->devs;
1619 this->devs = info_d;
1620
1621 disk = get_imsm_disk(mpb, idx);
1622 s = __le32_to_cpu(disk->status);
1623
1624 info_d->disk.number = d->index;
1625 info_d->disk.major = d->major;
1626 info_d->disk.minor = d->minor;
1627 info_d->disk.raid_disk = slot;
1628 info_d->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
1629 info_d->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
1630 info_d->disk.state |= s & USABLE_DISK ? (1 << MD_DISK_SYNC) : 0;
1631
1632 this->array.working_disks++;
1633
1634 info_d->events = __le32_to_cpu(mpb->generation_num);
1635 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
1636 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
1637 if (d->devname)
1638 strcpy(info_d->name, d->devname);
1639 }
1640 }
1641
1642 return rest;
1643 }
1644
1645
1646 static int imsm_open_new(struct supertype *c, struct active_array *a,
1647 char *inst)
1648 {
1649 dprintf("imsm: open_new %s\n", inst);
1650 a->info.container_member = atoi(inst);
1651 return 0;
1652 }
1653
1654 static __u8 imsm_check_degraded(struct imsm_super *mpb, int n, int failed)
1655 {
1656 struct imsm_dev *dev = get_imsm_dev(mpb, n);
1657 struct imsm_map *map = dev->vol.map;
1658
1659 if (!failed)
1660 return map->map_state;
1661
1662 switch (get_imsm_raid_level(map)) {
1663 case 0:
1664 return IMSM_T_STATE_FAILED;
1665 break;
1666 case 1:
1667 if (failed < map->num_members)
1668 return IMSM_T_STATE_DEGRADED;
1669 else
1670 return IMSM_T_STATE_FAILED;
1671 break;
1672 case 10:
1673 {
1674 /**
1675 * check to see if any mirrors have failed,
1676 * otherwise we are degraded
1677 */
1678 int device_per_mirror = 2; /* FIXME is this always the case?
1679 * and are they always adjacent?
1680 */
1681 int failed = 0;
1682 int i;
1683
1684 for (i = 0; i < map->num_members; i++) {
1685 int idx = get_imsm_disk_idx(map, i);
1686 struct imsm_disk *disk = get_imsm_disk(mpb, idx);
1687
1688 if (__le32_to_cpu(disk->status) & FAILED_DISK)
1689 failed++;
1690
1691 if (failed >= device_per_mirror)
1692 return IMSM_T_STATE_FAILED;
1693
1694 /* reset 'failed' for next mirror set */
1695 if (!((i + 1) % device_per_mirror))
1696 failed = 0;
1697 }
1698
1699 return IMSM_T_STATE_DEGRADED;
1700 }
1701 case 5:
1702 if (failed < 2)
1703 return IMSM_T_STATE_DEGRADED;
1704 else
1705 return IMSM_T_STATE_FAILED;
1706 break;
1707 default:
1708 break;
1709 }
1710
1711 return map->map_state;
1712 }
1713
1714 static int imsm_count_failed(struct imsm_super *mpb, struct imsm_map *map)
1715 {
1716 int i;
1717 int failed = 0;
1718 struct imsm_disk *disk;
1719
1720 for (i = 0; i < map->num_members; i++) {
1721 int idx = get_imsm_disk_idx(map, i);
1722
1723 disk = get_imsm_disk(mpb, idx);
1724 if (__le32_to_cpu(disk->status) & FAILED_DISK)
1725 failed++;
1726 }
1727
1728 return failed;
1729 }
1730
1731 static void imsm_set_array_state(struct active_array *a, int consistent)
1732 {
1733 int inst = a->info.container_member;
1734 struct intel_super *super = a->container->sb;
1735 struct imsm_dev *dev = get_imsm_dev(super->mpb, inst);
1736 struct imsm_map *map = &dev->vol.map[0];
1737 int dirty = !consistent;
1738 int failed;
1739 __u8 map_state;
1740
1741 if (a->resync_start == ~0ULL) {
1742 failed = imsm_count_failed(super->mpb, map);
1743 map_state = imsm_check_degraded(super->mpb, inst, failed);
1744 if (!failed)
1745 map_state = IMSM_T_STATE_NORMAL;
1746 if (map->map_state != map_state) {
1747 dprintf("imsm: map_state %d: %d\n",
1748 inst, map_state);
1749 map->map_state = map_state;
1750 super->updates_pending++;
1751 }
1752 }
1753
1754 if (dev->vol.dirty != dirty) {
1755 dprintf("imsm: mark '%s' (%llu)\n",
1756 dirty?"dirty":"clean", a->resync_start);
1757
1758 dev->vol.dirty = dirty;
1759 super->updates_pending++;
1760 }
1761 }
1762
1763 static void imsm_set_disk(struct active_array *a, int n, int state)
1764 {
1765 int inst = a->info.container_member;
1766 struct intel_super *super = a->container->sb;
1767 struct imsm_dev *dev = get_imsm_dev(super->mpb, inst);
1768 struct imsm_map *map = dev->vol.map;
1769 struct imsm_disk *disk;
1770 __u32 status;
1771 int failed = 0;
1772 int new_failure = 0;
1773
1774 if (n > map->num_members)
1775 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
1776 n, map->num_members - 1);
1777
1778 if (n < 0)
1779 return;
1780
1781 dprintf("imsm: set_disk %d:%x\n", n, state);
1782
1783 disk = get_imsm_disk(super->mpb, get_imsm_disk_idx(map, n));
1784
1785 /* check if we have seen this failure before */
1786 status = __le32_to_cpu(disk->status);
1787 if ((state & DS_FAULTY) && !(status & FAILED_DISK)) {
1788 status |= FAILED_DISK;
1789 disk->status = __cpu_to_le32(status);
1790 new_failure = 1;
1791 }
1792
1793 /**
1794 * the number of failures have changed, count up 'failed' to determine
1795 * degraded / failed status
1796 */
1797 if (new_failure && map->map_state != IMSM_T_STATE_FAILED)
1798 failed = imsm_count_failed(super->mpb, map);
1799
1800 if (failed)
1801 map->map_state = imsm_check_degraded(super->mpb, inst, failed);
1802
1803 if (new_failure)
1804 super->updates_pending++;
1805 }
1806
1807 static int store_imsm_mpb(int fd, struct intel_super *super)
1808 {
1809 struct imsm_super *mpb = super->mpb;
1810 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
1811 unsigned long long dsize;
1812 unsigned long long sectors;
1813
1814 get_dev_size(fd, NULL, &dsize);
1815
1816 if (mpb_size > 512) {
1817 /* -1 to account for anchor */
1818 sectors = mpb_sectors(mpb) - 1;
1819
1820 /* write the extended mpb to the sectors preceeding the anchor */
1821 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
1822 return 1;
1823
1824 if (write(fd, super->buf + 512, mpb_size - 512) != mpb_size - 512)
1825 return 1;
1826 }
1827
1828 /* first block is stored on second to last sector of the disk */
1829 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
1830 return 1;
1831
1832 if (write(fd, super->buf, 512) != 512)
1833 return 1;
1834
1835 fsync(fd);
1836
1837 return 0;
1838 }
1839
1840 static void imsm_sync_metadata(struct supertype *container)
1841 {
1842 struct intel_super *super = container->sb;
1843
1844 if (!super->updates_pending)
1845 return;
1846
1847 write_super_imsm(super, 0);
1848
1849 super->updates_pending = 0;
1850 }
1851
1852 struct superswitch super_imsm = {
1853 #ifndef MDASSEMBLE
1854 .examine_super = examine_super_imsm,
1855 .brief_examine_super = brief_examine_super_imsm,
1856 .detail_super = detail_super_imsm,
1857 .brief_detail_super = brief_detail_super_imsm,
1858 #endif
1859 .match_home = match_home_imsm,
1860 .uuid_from_super= uuid_from_super_imsm,
1861 .getinfo_super = getinfo_super_imsm,
1862 .update_super = update_super_imsm,
1863
1864 .avail_size = avail_size_imsm,
1865
1866 .compare_super = compare_super_imsm,
1867
1868 .load_super = load_super_imsm,
1869 .init_super = init_zero_imsm,
1870 .store_super = store_zero_imsm,
1871 .free_super = free_super_imsm,
1872 .match_metadata_desc = match_metadata_desc_imsm,
1873 .getinfo_super_n = getinfo_super_n_imsm_container,
1874
1875 .validate_geometry = validate_geometry_imsm,
1876 .major = 2000,
1877 .swapuuid = 0,
1878 .external = 1,
1879
1880 /* for mdmon */
1881 .open_new = imsm_open_new,
1882 .load_super = load_super_imsm,
1883 .set_array_state= imsm_set_array_state,
1884 .set_disk = imsm_set_disk,
1885 .sync_metadata = imsm_sync_metadata,
1886 };
1887
1888 /* super_imsm_container is set by validate_geometry_imsm when given a
1889 * device that is not part of any array
1890 */
1891 struct superswitch super_imsm_container = {
1892
1893 .validate_geometry = validate_geometry_imsm_container,
1894 .init_super = init_super_imsm,
1895 .add_to_super = add_to_super_imsm,
1896 .write_init_super = write_init_super_imsm,
1897 .getinfo_super = getinfo_super_imsm,
1898 .getinfo_super_n = getinfo_super_n_imsm_container,
1899 .load_super = load_super_imsm,
1900
1901 #ifndef MDASSEMBLE
1902 .examine_super = examine_super_imsm,
1903 .brief_examine_super = brief_examine_super_imsm,
1904 .detail_super = detail_super_imsm,
1905 .brief_detail_super = brief_detail_super_imsm,
1906 #endif
1907
1908 .free_super = free_super_imsm,
1909
1910 .container_content = container_content_imsm,
1911
1912 .major = 2000,
1913 .swapuuid = 0,
1914 .external = 1,
1915 };
1916
1917 struct superswitch super_imsm_volume = {
1918 .update_super = update_super_imsm,
1919 .init_super = init_super_imsm_volume,
1920 .add_to_super = add_to_super_imsm_volume,
1921 .getinfo_super = getinfo_super_imsm_volume,
1922 .getinfo_super_n = getinfo_super_n_imsm_volume,
1923 .write_init_super = write_init_super_imsm,
1924
1925 .load_super = load_super_imsm,
1926 .free_super = free_super_imsm,
1927 .match_metadata_desc = match_metadata_desc_imsm_volume,
1928
1929
1930 .validate_geometry = validate_geometry_imsm_volume,
1931 .major = 2001,
1932 .swapuuid = 0,
1933 .external = 2,
1934 };