imsm: add 'verify', 'verify with fixup', and 'general' migration types
[thirdparty/mdadm.git] / super-intel.c
1 /*
2 * mdadm - Intel(R) Matrix Storage Manager Support
3 *
4 * Copyright (C) 2002-2008 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 #define HAVE_STDINT_H 1
21 #include "mdadm.h"
22 #include "mdmon.h"
23 #include "sha1.h"
24 #include "platform-intel.h"
25 #include <values.h>
26 #include <scsi/sg.h>
27 #include <ctype.h>
28 #include <dirent.h>
29
30 /* MPB == Metadata Parameter Block */
31 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
32 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
33 #define MPB_VERSION_RAID0 "1.0.00"
34 #define MPB_VERSION_RAID1 "1.1.00"
35 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
36 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
37 #define MPB_VERSION_RAID5 "1.2.02"
38 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
39 #define MPB_VERSION_CNG "1.2.06"
40 #define MPB_VERSION_ATTRIBS "1.3.00"
41 #define MAX_SIGNATURE_LENGTH 32
42 #define MAX_RAID_SERIAL_LEN 16
43
44 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
45 #define MPB_ATTRIB_PM __cpu_to_le32(0x40000000)
46 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
47 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
48 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
50 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
51 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
52 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
53
54 #define MPB_SECTOR_CNT 418
55 #define IMSM_RESERVED_SECTORS 4096
56
57 /* Disk configuration info. */
58 #define IMSM_MAX_DEVICES 255
59 struct imsm_disk {
60 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
61 __u32 total_blocks; /* 0xE8 - 0xEB total blocks */
62 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
63 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
64 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
65 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
66 #define USABLE_DISK __cpu_to_le32(0x08) /* Fully usable unless FAILED_DISK is set */
67 __u32 status; /* 0xF0 - 0xF3 */
68 __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
69 #define IMSM_DISK_FILLERS 4
70 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */
71 };
72
73 /* RAID map configuration infos. */
74 struct imsm_map {
75 __u32 pba_of_lba0; /* start address of partition */
76 __u32 blocks_per_member;/* blocks per member */
77 __u32 num_data_stripes; /* number of data stripes */
78 __u16 blocks_per_strip;
79 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
80 #define IMSM_T_STATE_NORMAL 0
81 #define IMSM_T_STATE_UNINITIALIZED 1
82 #define IMSM_T_STATE_DEGRADED 2
83 #define IMSM_T_STATE_FAILED 3
84 __u8 raid_level;
85 #define IMSM_T_RAID0 0
86 #define IMSM_T_RAID1 1
87 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
88 __u8 num_members; /* number of member disks */
89 __u8 num_domains; /* number of parity domains */
90 __u8 failed_disk_num; /* valid only when state is degraded */
91 __u8 reserved[1];
92 __u32 filler[7]; /* expansion area */
93 #define IMSM_ORD_REBUILD (1 << 24)
94 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
95 * top byte contains some flags
96 */
97 } __attribute__ ((packed));
98
99 struct imsm_vol {
100 __u32 curr_migr_unit;
101 __u32 checkpoint_id; /* id to access curr_migr_unit */
102 __u8 migr_state; /* Normal or Migrating */
103 #define MIGR_INIT 0
104 #define MIGR_REBUILD 1
105 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
106 #define MIGR_GEN_MIGR 3
107 #define MIGR_STATE_CHANGE 4
108 #define MIGR_REPAIR 5
109 __u8 migr_type; /* Initializing, Rebuilding, ... */
110 __u8 dirty;
111 __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
112 __u16 verify_errors; /* number of mismatches */
113 __u16 bad_blocks; /* number of bad blocks during verify */
114 __u32 filler[4];
115 struct imsm_map map[1];
116 /* here comes another one if migr_state */
117 } __attribute__ ((packed));
118
119 struct imsm_dev {
120 __u8 volume[MAX_RAID_SERIAL_LEN];
121 __u32 size_low;
122 __u32 size_high;
123 #define DEV_BOOTABLE __cpu_to_le32(0x01)
124 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
125 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
126 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
127 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
128 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
129 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
130 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
131 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
132 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
133 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
134 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
135 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
136 __u32 status; /* Persistent RaidDev status */
137 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
138 __u8 migr_priority;
139 __u8 num_sub_vols;
140 __u8 tid;
141 __u8 cng_master_disk;
142 __u16 cache_policy;
143 __u8 cng_state;
144 __u8 cng_sub_state;
145 #define IMSM_DEV_FILLERS 10
146 __u32 filler[IMSM_DEV_FILLERS];
147 struct imsm_vol vol;
148 } __attribute__ ((packed));
149
150 struct imsm_super {
151 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
152 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
153 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
154 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
155 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
156 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
157 __u32 attributes; /* 0x34 - 0x37 */
158 __u8 num_disks; /* 0x38 Number of configured disks */
159 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
160 __u8 error_log_pos; /* 0x3A */
161 __u8 fill[1]; /* 0x3B */
162 __u32 cache_size; /* 0x3c - 0x40 in mb */
163 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
164 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
165 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
166 #define IMSM_FILLERS 35
167 __u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
168 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
169 /* here comes imsm_dev[num_raid_devs] */
170 /* here comes BBM logs */
171 } __attribute__ ((packed));
172
173 #define BBM_LOG_MAX_ENTRIES 254
174
175 struct bbm_log_entry {
176 __u64 defective_block_start;
177 #define UNREADABLE 0xFFFFFFFF
178 __u32 spare_block_offset;
179 __u16 remapped_marked_count;
180 __u16 disk_ordinal;
181 } __attribute__ ((__packed__));
182
183 struct bbm_log {
184 __u32 signature; /* 0xABADB10C */
185 __u32 entry_count;
186 __u32 reserved_spare_block_count; /* 0 */
187 __u32 reserved; /* 0xFFFF */
188 __u64 first_spare_lba;
189 struct bbm_log_entry mapped_block_entries[BBM_LOG_MAX_ENTRIES];
190 } __attribute__ ((__packed__));
191
192
193 #ifndef MDASSEMBLE
194 static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
195 #endif
196
197 static __u8 migr_type(struct imsm_dev *dev)
198 {
199 if (dev->vol.migr_type == MIGR_VERIFY &&
200 dev->status & DEV_VERIFY_AND_FIX)
201 return MIGR_REPAIR;
202 else
203 return dev->vol.migr_type;
204 }
205
206 static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
207 {
208 /* for compatibility with older oroms convert MIGR_REPAIR, into
209 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
210 */
211 if (migr_type == MIGR_REPAIR) {
212 dev->vol.migr_type = MIGR_VERIFY;
213 dev->status |= DEV_VERIFY_AND_FIX;
214 } else {
215 dev->vol.migr_type = migr_type;
216 dev->status &= ~DEV_VERIFY_AND_FIX;
217 }
218 }
219
220 static unsigned int sector_count(__u32 bytes)
221 {
222 return ((bytes + (512-1)) & (~(512-1))) / 512;
223 }
224
225 static unsigned int mpb_sectors(struct imsm_super *mpb)
226 {
227 return sector_count(__le32_to_cpu(mpb->mpb_size));
228 }
229
230 struct intel_dev {
231 struct imsm_dev *dev;
232 struct intel_dev *next;
233 int index;
234 };
235
236 /* internal representation of IMSM metadata */
237 struct intel_super {
238 union {
239 void *buf; /* O_DIRECT buffer for reading/writing metadata */
240 struct imsm_super *anchor; /* immovable parameters */
241 };
242 size_t len; /* size of the 'buf' allocation */
243 void *next_buf; /* for realloc'ing buf from the manager */
244 size_t next_len;
245 int updates_pending; /* count of pending updates for mdmon */
246 int creating_imsm; /* flag to indicate container creation */
247 int current_vol; /* index of raid device undergoing creation */
248 __u32 create_offset; /* common start for 'current_vol' */
249 struct intel_dev *devlist;
250 struct dl {
251 struct dl *next;
252 int index;
253 __u8 serial[MAX_RAID_SERIAL_LEN];
254 int major, minor;
255 char *devname;
256 struct imsm_disk disk;
257 int fd;
258 int extent_cnt;
259 struct extent *e; /* for determining freespace @ create */
260 int raiddisk; /* slot to fill in autolayout */
261 } *disks;
262 struct dl *add; /* list of disks to add while mdmon active */
263 struct dl *missing; /* disks removed while we weren't looking */
264 struct bbm_log *bbm_log;
265 const char *hba; /* device path of the raid controller for this metadata */
266 const struct imsm_orom *orom; /* platform firmware support */
267 };
268
269 struct extent {
270 unsigned long long start, size;
271 };
272
273 /* definition of messages passed to imsm_process_update */
274 enum imsm_update_type {
275 update_activate_spare,
276 update_create_array,
277 update_add_disk,
278 };
279
280 struct imsm_update_activate_spare {
281 enum imsm_update_type type;
282 struct dl *dl;
283 int slot;
284 int array;
285 struct imsm_update_activate_spare *next;
286 };
287
288 struct disk_info {
289 __u8 serial[MAX_RAID_SERIAL_LEN];
290 };
291
292 struct imsm_update_create_array {
293 enum imsm_update_type type;
294 int dev_idx;
295 struct imsm_dev dev;
296 };
297
298 struct imsm_update_add_disk {
299 enum imsm_update_type type;
300 };
301
302 static struct supertype *match_metadata_desc_imsm(char *arg)
303 {
304 struct supertype *st;
305
306 if (strcmp(arg, "imsm") != 0 &&
307 strcmp(arg, "default") != 0
308 )
309 return NULL;
310
311 st = malloc(sizeof(*st));
312 memset(st, 0, sizeof(*st));
313 st->ss = &super_imsm;
314 st->max_devs = IMSM_MAX_DEVICES;
315 st->minor_version = 0;
316 st->sb = NULL;
317 return st;
318 }
319
320 #ifndef MDASSEMBLE
321 static __u8 *get_imsm_version(struct imsm_super *mpb)
322 {
323 return &mpb->sig[MPB_SIG_LEN];
324 }
325 #endif
326
327 /* retrieve a disk directly from the anchor when the anchor is known to be
328 * up-to-date, currently only at load time
329 */
330 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
331 {
332 if (index >= mpb->num_disks)
333 return NULL;
334 return &mpb->disk[index];
335 }
336
337 #ifndef MDASSEMBLE
338 /* retrieve a disk from the parsed metadata */
339 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
340 {
341 struct dl *d;
342
343 for (d = super->disks; d; d = d->next)
344 if (d->index == index)
345 return &d->disk;
346
347 return NULL;
348 }
349 #endif
350
351 /* generate a checksum directly from the anchor when the anchor is known to be
352 * up-to-date, currently only at load or write_super after coalescing
353 */
354 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
355 {
356 __u32 end = mpb->mpb_size / sizeof(end);
357 __u32 *p = (__u32 *) mpb;
358 __u32 sum = 0;
359
360 while (end--) {
361 sum += __le32_to_cpu(*p);
362 p++;
363 }
364
365 return sum - __le32_to_cpu(mpb->check_sum);
366 }
367
368 static size_t sizeof_imsm_map(struct imsm_map *map)
369 {
370 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
371 }
372
373 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
374 {
375 struct imsm_map *map = &dev->vol.map[0];
376
377 if (second_map && !dev->vol.migr_state)
378 return NULL;
379 else if (second_map) {
380 void *ptr = map;
381
382 return ptr + sizeof_imsm_map(map);
383 } else
384 return map;
385
386 }
387
388 /* return the size of the device.
389 * migr_state increases the returned size if map[0] were to be duplicated
390 */
391 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
392 {
393 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
394 sizeof_imsm_map(get_imsm_map(dev, 0));
395
396 /* migrating means an additional map */
397 if (dev->vol.migr_state)
398 size += sizeof_imsm_map(get_imsm_map(dev, 1));
399 else if (migr_state)
400 size += sizeof_imsm_map(get_imsm_map(dev, 0));
401
402 return size;
403 }
404
405 #ifndef MDASSEMBLE
406 /* retrieve disk serial number list from a metadata update */
407 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
408 {
409 void *u = update;
410 struct disk_info *inf;
411
412 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
413 sizeof_imsm_dev(&update->dev, 0);
414
415 return inf;
416 }
417 #endif
418
419 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
420 {
421 int offset;
422 int i;
423 void *_mpb = mpb;
424
425 if (index >= mpb->num_raid_devs)
426 return NULL;
427
428 /* devices start after all disks */
429 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
430
431 for (i = 0; i <= index; i++)
432 if (i == index)
433 return _mpb + offset;
434 else
435 offset += sizeof_imsm_dev(_mpb + offset, 0);
436
437 return NULL;
438 }
439
440 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
441 {
442 struct intel_dev *dv;
443
444 if (index >= super->anchor->num_raid_devs)
445 return NULL;
446 for (dv = super->devlist; dv; dv = dv->next)
447 if (dv->index == index)
448 return dv->dev;
449 return NULL;
450 }
451
452 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, int slot)
453 {
454 struct imsm_map *map;
455
456 if (dev->vol.migr_state)
457 map = get_imsm_map(dev, 1);
458 else
459 map = get_imsm_map(dev, 0);
460
461 /* top byte identifies disk under rebuild */
462 return __le32_to_cpu(map->disk_ord_tbl[slot]);
463 }
464
465 #define ord_to_idx(ord) (((ord) << 8) >> 8)
466 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot)
467 {
468 __u32 ord = get_imsm_ord_tbl_ent(dev, slot);
469
470 return ord_to_idx(ord);
471 }
472
473 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
474 {
475 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
476 }
477
478 static int get_imsm_disk_slot(struct imsm_map *map, int idx)
479 {
480 int slot;
481 __u32 ord;
482
483 for (slot = 0; slot < map->num_members; slot++) {
484 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
485 if (ord_to_idx(ord) == idx)
486 return slot;
487 }
488
489 return -1;
490 }
491
492 static int get_imsm_raid_level(struct imsm_map *map)
493 {
494 if (map->raid_level == 1) {
495 if (map->num_members == 2)
496 return 1;
497 else
498 return 10;
499 }
500
501 return map->raid_level;
502 }
503
504 static int cmp_extent(const void *av, const void *bv)
505 {
506 const struct extent *a = av;
507 const struct extent *b = bv;
508 if (a->start < b->start)
509 return -1;
510 if (a->start > b->start)
511 return 1;
512 return 0;
513 }
514
515 static int count_memberships(struct dl *dl, struct intel_super *super)
516 {
517 int memberships = 0;
518 int i;
519
520 for (i = 0; i < super->anchor->num_raid_devs; i++) {
521 struct imsm_dev *dev = get_imsm_dev(super, i);
522 struct imsm_map *map = get_imsm_map(dev, 0);
523
524 if (get_imsm_disk_slot(map, dl->index) >= 0)
525 memberships++;
526 }
527
528 return memberships;
529 }
530
531 static struct extent *get_extents(struct intel_super *super, struct dl *dl)
532 {
533 /* find a list of used extents on the given physical device */
534 struct extent *rv, *e;
535 int i;
536 int memberships = count_memberships(dl, super);
537 __u32 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
538
539 rv = malloc(sizeof(struct extent) * (memberships + 1));
540 if (!rv)
541 return NULL;
542 e = rv;
543
544 for (i = 0; i < super->anchor->num_raid_devs; i++) {
545 struct imsm_dev *dev = get_imsm_dev(super, i);
546 struct imsm_map *map = get_imsm_map(dev, 0);
547
548 if (get_imsm_disk_slot(map, dl->index) >= 0) {
549 e->start = __le32_to_cpu(map->pba_of_lba0);
550 e->size = __le32_to_cpu(map->blocks_per_member);
551 e++;
552 }
553 }
554 qsort(rv, memberships, sizeof(*rv), cmp_extent);
555
556 /* determine the start of the metadata
557 * when no raid devices are defined use the default
558 * ...otherwise allow the metadata to truncate the value
559 * as is the case with older versions of imsm
560 */
561 if (memberships) {
562 struct extent *last = &rv[memberships - 1];
563 __u32 remainder;
564
565 remainder = __le32_to_cpu(dl->disk.total_blocks) -
566 (last->start + last->size);
567 /* round down to 1k block to satisfy precision of the kernel
568 * 'size' interface
569 */
570 remainder &= ~1UL;
571 /* make sure remainder is still sane */
572 if (remainder < ROUND_UP(super->len, 512) >> 9)
573 remainder = ROUND_UP(super->len, 512) >> 9;
574 if (reservation > remainder)
575 reservation = remainder;
576 }
577 e->start = __le32_to_cpu(dl->disk.total_blocks) - reservation;
578 e->size = 0;
579 return rv;
580 }
581
582 /* try to determine how much space is reserved for metadata from
583 * the last get_extents() entry, otherwise fallback to the
584 * default
585 */
586 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
587 {
588 struct extent *e;
589 int i;
590 __u32 rv;
591
592 /* for spares just return a minimal reservation which will grow
593 * once the spare is picked up by an array
594 */
595 if (dl->index == -1)
596 return MPB_SECTOR_CNT;
597
598 e = get_extents(super, dl);
599 if (!e)
600 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
601
602 /* scroll to last entry */
603 for (i = 0; e[i].size; i++)
604 continue;
605
606 rv = __le32_to_cpu(dl->disk.total_blocks) - e[i].start;
607
608 free(e);
609
610 return rv;
611 }
612
613 #ifndef MDASSEMBLE
614 static void print_imsm_dev(struct imsm_dev *dev, char *uuid, int disk_idx)
615 {
616 __u64 sz;
617 int slot;
618 struct imsm_map *map = get_imsm_map(dev, 0);
619 __u32 ord;
620
621 printf("\n");
622 printf("[%.16s]:\n", dev->volume);
623 printf(" UUID : %s\n", uuid);
624 printf(" RAID Level : %d\n", get_imsm_raid_level(map));
625 printf(" Members : %d\n", map->num_members);
626 slot = get_imsm_disk_slot(map, disk_idx);
627 if (slot >= 0) {
628 ord = get_imsm_ord_tbl_ent(dev, slot);
629 printf(" This Slot : %d%s\n", slot,
630 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
631 } else
632 printf(" This Slot : ?\n");
633 sz = __le32_to_cpu(dev->size_high);
634 sz <<= 32;
635 sz += __le32_to_cpu(dev->size_low);
636 printf(" Array Size : %llu%s\n", (unsigned long long)sz,
637 human_size(sz * 512));
638 sz = __le32_to_cpu(map->blocks_per_member);
639 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz,
640 human_size(sz * 512));
641 printf(" Sector Offset : %u\n",
642 __le32_to_cpu(map->pba_of_lba0));
643 printf(" Num Stripes : %u\n",
644 __le32_to_cpu(map->num_data_stripes));
645 printf(" Chunk Size : %u KiB\n",
646 __le16_to_cpu(map->blocks_per_strip) / 2);
647 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
648 printf(" Migrate State : %s", dev->vol.migr_state ? "migrating" : "idle\n");
649 if (dev->vol.migr_state) {
650 if (migr_type(dev) == MIGR_INIT)
651 printf(": initializing\n");
652 else if (migr_type(dev) == MIGR_REBUILD)
653 printf(": rebuilding\n");
654 else if (migr_type(dev) == MIGR_VERIFY)
655 printf(": check\n");
656 else if (migr_type(dev) == MIGR_GEN_MIGR)
657 printf(": general migration\n");
658 else if (migr_type(dev) == MIGR_STATE_CHANGE)
659 printf(": state change\n");
660 else if (migr_type(dev) == MIGR_REPAIR)
661 printf(": repair\n");
662 else
663 printf(": <unknown:%d>\n", migr_type(dev));
664 }
665 printf(" Map State : %s", map_state_str[map->map_state]);
666 if (dev->vol.migr_state) {
667 struct imsm_map *map = get_imsm_map(dev, 1);
668 printf(" <-- %s", map_state_str[map->map_state]);
669 }
670 printf("\n");
671 printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
672 }
673
674 static void print_imsm_disk(struct imsm_super *mpb, int index, __u32 reserved)
675 {
676 struct imsm_disk *disk = __get_imsm_disk(mpb, index);
677 char str[MAX_RAID_SERIAL_LEN + 1];
678 __u32 s;
679 __u64 sz;
680
681 if (index < 0)
682 return;
683
684 printf("\n");
685 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
686 printf(" Disk%02d Serial : %s\n", index, str);
687 s = disk->status;
688 printf(" State :%s%s%s%s\n", s&SPARE_DISK ? " spare" : "",
689 s&CONFIGURED_DISK ? " active" : "",
690 s&FAILED_DISK ? " failed" : "",
691 s&USABLE_DISK ? " usable" : "");
692 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
693 sz = __le32_to_cpu(disk->total_blocks) - reserved;
694 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
695 human_size(sz * 512));
696 }
697
698 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info);
699
700 static void examine_super_imsm(struct supertype *st, char *homehost)
701 {
702 struct intel_super *super = st->sb;
703 struct imsm_super *mpb = super->anchor;
704 char str[MAX_SIGNATURE_LENGTH];
705 int i;
706 struct mdinfo info;
707 char nbuf[64];
708 __u32 sum;
709 __u32 reserved = imsm_reserved_sectors(super, super->disks);
710
711
712 snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
713 printf(" Magic : %s\n", str);
714 snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
715 printf(" Version : %s\n", get_imsm_version(mpb));
716 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
717 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
718 getinfo_super_imsm(st, &info);
719 fname_from_uuid(st, &info, nbuf, ':');
720 printf(" UUID : %s\n", nbuf + 5);
721 sum = __le32_to_cpu(mpb->check_sum);
722 printf(" Checksum : %08x %s\n", sum,
723 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
724 printf(" MPB Sectors : %d\n", mpb_sectors(mpb));
725 printf(" Disks : %d\n", mpb->num_disks);
726 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
727 print_imsm_disk(mpb, super->disks->index, reserved);
728 if (super->bbm_log) {
729 struct bbm_log *log = super->bbm_log;
730
731 printf("\n");
732 printf("Bad Block Management Log:\n");
733 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
734 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
735 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
736 printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count));
737 printf(" First Spare : %llx\n", __le64_to_cpu(log->first_spare_lba));
738 }
739 for (i = 0; i < mpb->num_raid_devs; i++) {
740 struct mdinfo info;
741 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
742
743 super->current_vol = i;
744 getinfo_super_imsm(st, &info);
745 fname_from_uuid(st, &info, nbuf, ':');
746 print_imsm_dev(dev, nbuf + 5, super->disks->index);
747 }
748 for (i = 0; i < mpb->num_disks; i++) {
749 if (i == super->disks->index)
750 continue;
751 print_imsm_disk(mpb, i, reserved);
752 }
753 }
754
755 static void brief_examine_super_imsm(struct supertype *st)
756 {
757 /* We just write a generic IMSM ARRAY entry */
758 struct mdinfo info;
759 char nbuf[64];
760 char nbuf1[64];
761 struct intel_super *super = st->sb;
762 int i;
763
764 if (!super->anchor->num_raid_devs)
765 return;
766
767 getinfo_super_imsm(st, &info);
768 fname_from_uuid(st, &info, nbuf, ':');
769 printf("ARRAY metadata=imsm auto=md UUID=%s\n", nbuf + 5);
770 for (i = 0; i < super->anchor->num_raid_devs; i++) {
771 struct imsm_dev *dev = get_imsm_dev(super, i);
772
773 super->current_vol = i;
774 getinfo_super_imsm(st, &info);
775 fname_from_uuid(st, &info, nbuf1, ':');
776 printf("ARRAY /dev/md/%.16s container=%s\n"
777 " member=%d auto=mdp UUID=%s\n",
778 dev->volume, nbuf + 5, i, nbuf1 + 5);
779 }
780 }
781
782 static void export_examine_super_imsm(struct supertype *st)
783 {
784 struct intel_super *super = st->sb;
785 struct imsm_super *mpb = super->anchor;
786 struct mdinfo info;
787 char nbuf[64];
788
789 getinfo_super_imsm(st, &info);
790 fname_from_uuid(st, &info, nbuf, ':');
791 printf("MD_METADATA=imsm\n");
792 printf("MD_LEVEL=container\n");
793 printf("MD_UUID=%s\n", nbuf+5);
794 printf("MD_DEVICES=%u\n", mpb->num_disks);
795 }
796
797 static void detail_super_imsm(struct supertype *st, char *homehost)
798 {
799 struct mdinfo info;
800 char nbuf[64];
801
802 getinfo_super_imsm(st, &info);
803 fname_from_uuid(st, &info, nbuf, ':');
804 printf("\n UUID : %s\n", nbuf + 5);
805 }
806
807 static void brief_detail_super_imsm(struct supertype *st)
808 {
809 struct mdinfo info;
810 char nbuf[64];
811 getinfo_super_imsm(st, &info);
812 fname_from_uuid(st, &info, nbuf, ':');
813 printf(" UUID=%s", nbuf + 5);
814 }
815
816 static int imsm_read_serial(int fd, char *devname, __u8 *serial);
817 static void fd2devname(int fd, char *name);
818
819 static int imsm_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
820 {
821 /* dump an unsorted list of devices attached to ahci, as well as
822 * non-connected ports
823 */
824 int hba_len = strlen(hba_path) + 1;
825 struct dirent *ent;
826 DIR *dir;
827 char *path = NULL;
828 int err = 0;
829 unsigned long port_mask = (1 << port_count) - 1;
830
831 if (port_count > sizeof(port_mask) * 8) {
832 if (verbose)
833 fprintf(stderr, Name ": port_count %d out of range\n", port_count);
834 return 2;
835 }
836
837 /* scroll through /sys/dev/block looking for devices attached to
838 * this hba
839 */
840 dir = opendir("/sys/dev/block");
841 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
842 int fd;
843 char model[64];
844 char vendor[64];
845 char buf[1024];
846 int major, minor;
847 char *device;
848 char *c;
849 int port;
850 int type;
851
852 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
853 continue;
854 path = devt_to_devpath(makedev(major, minor));
855 if (!path)
856 continue;
857 if (!path_attached_to_hba(path, hba_path)) {
858 free(path);
859 path = NULL;
860 continue;
861 }
862
863 /* retrieve the scsi device type */
864 if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) {
865 if (verbose)
866 fprintf(stderr, Name ": failed to allocate 'device'\n");
867 err = 2;
868 break;
869 }
870 sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
871 if (load_sys(device, buf) != 0) {
872 if (verbose)
873 fprintf(stderr, Name ": failed to read device type for %s\n",
874 path);
875 err = 2;
876 free(device);
877 break;
878 }
879 type = strtoul(buf, NULL, 10);
880
881 /* if it's not a disk print the vendor and model */
882 if (!(type == 0 || type == 7 || type == 14)) {
883 vendor[0] = '\0';
884 model[0] = '\0';
885 sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
886 if (load_sys(device, buf) == 0) {
887 strncpy(vendor, buf, sizeof(vendor));
888 vendor[sizeof(vendor) - 1] = '\0';
889 c = (char *) &vendor[sizeof(vendor) - 1];
890 while (isspace(*c) || *c == '\0')
891 *c-- = '\0';
892
893 }
894 sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
895 if (load_sys(device, buf) == 0) {
896 strncpy(model, buf, sizeof(model));
897 model[sizeof(model) - 1] = '\0';
898 c = (char *) &model[sizeof(model) - 1];
899 while (isspace(*c) || *c == '\0')
900 *c-- = '\0';
901 }
902
903 if (vendor[0] && model[0])
904 sprintf(buf, "%.64s %.64s", vendor, model);
905 else
906 switch (type) { /* numbers from hald/linux/device.c */
907 case 1: sprintf(buf, "tape"); break;
908 case 2: sprintf(buf, "printer"); break;
909 case 3: sprintf(buf, "processor"); break;
910 case 4:
911 case 5: sprintf(buf, "cdrom"); break;
912 case 6: sprintf(buf, "scanner"); break;
913 case 8: sprintf(buf, "media_changer"); break;
914 case 9: sprintf(buf, "comm"); break;
915 case 12: sprintf(buf, "raid"); break;
916 default: sprintf(buf, "unknown");
917 }
918 } else
919 buf[0] = '\0';
920 free(device);
921
922 /* chop device path to 'host%d' and calculate the port number */
923 c = strchr(&path[hba_len], '/');
924 *c = '\0';
925 if (sscanf(&path[hba_len], "host%d", &port) == 1)
926 port -= host_base;
927 else {
928 if (verbose) {
929 *c = '/'; /* repair the full string */
930 fprintf(stderr, Name ": failed to determine port number for %s\n",
931 path);
932 }
933 err = 2;
934 break;
935 }
936
937 /* mark this port as used */
938 port_mask &= ~(1 << port);
939
940 /* print out the device information */
941 if (buf[0]) {
942 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
943 continue;
944 }
945
946 fd = dev_open(ent->d_name, O_RDONLY);
947 if (fd < 0)
948 printf(" Port%d : - disk info unavailable -\n", port);
949 else {
950 fd2devname(fd, buf);
951 printf(" Port%d : %s", port, buf);
952 if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0)
953 printf(" (%s)\n", buf);
954 else
955 printf("()\n");
956 }
957 close(fd);
958 free(path);
959 path = NULL;
960 }
961 if (path)
962 free(path);
963 if (dir)
964 closedir(dir);
965 if (err == 0) {
966 int i;
967
968 for (i = 0; i < port_count; i++)
969 if (port_mask & (1 << i))
970 printf(" Port%d : - no device attached -\n", i);
971 }
972
973 return err;
974 }
975
976 static int detail_platform_imsm(int verbose, int enumerate_only)
977 {
978 /* There are two components to imsm platform support, the ahci SATA
979 * controller and the option-rom. To find the SATA controller we
980 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
981 * controller with the Intel vendor id is present. This approach
982 * allows mdadm to leverage the kernel's ahci detection logic, with the
983 * caveat that if ahci.ko is not loaded mdadm will not be able to
984 * detect platform raid capabilities. The option-rom resides in a
985 * platform "Adapter ROM". We scan for its signature to retrieve the
986 * platform capabilities. If raid support is disabled in the BIOS the
987 * option-rom capability structure will not be available.
988 */
989 const struct imsm_orom *orom;
990 struct sys_dev *list, *hba;
991 DIR *dir;
992 struct dirent *ent;
993 const char *hba_path;
994 int host_base = 0;
995 int port_count = 0;
996
997 if (enumerate_only) {
998 if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
999 return 0;
1000 return 2;
1001 }
1002
1003 list = find_driver_devices("pci", "ahci");
1004 for (hba = list; hba; hba = hba->next)
1005 if (devpath_to_vendor(hba->path) == 0x8086)
1006 break;
1007
1008 if (!hba) {
1009 if (verbose)
1010 fprintf(stderr, Name ": unable to find active ahci controller\n");
1011 free_sys_dev(&list);
1012 return 2;
1013 } else if (verbose)
1014 fprintf(stderr, Name ": found Intel SATA AHCI Controller\n");
1015 hba_path = hba->path;
1016 hba->path = NULL;
1017 free_sys_dev(&list);
1018
1019 orom = find_imsm_orom();
1020 if (!orom) {
1021 if (verbose)
1022 fprintf(stderr, Name ": imsm option-rom not found\n");
1023 return 2;
1024 }
1025
1026 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1027 printf(" Version : %d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
1028 orom->hotfix_ver, orom->build);
1029 printf(" RAID Levels :%s%s%s%s%s\n",
1030 imsm_orom_has_raid0(orom) ? " raid0" : "",
1031 imsm_orom_has_raid1(orom) ? " raid1" : "",
1032 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
1033 imsm_orom_has_raid10(orom) ? " raid10" : "",
1034 imsm_orom_has_raid5(orom) ? " raid5" : "");
1035 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1036 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
1037 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
1038 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
1039 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
1040 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
1041 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
1042 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
1043 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
1044 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
1045 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
1046 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
1047 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
1048 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
1049 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
1050 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
1051 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
1052 printf(" Max Disks : %d\n", orom->tds);
1053 printf(" Max Volumes : %d\n", orom->vpa);
1054 printf(" I/O Controller : %s\n", hba_path);
1055
1056 /* find the smallest scsi host number to determine a port number base */
1057 dir = opendir(hba_path);
1058 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
1059 int host;
1060
1061 if (sscanf(ent->d_name, "host%d", &host) != 1)
1062 continue;
1063 if (port_count == 0)
1064 host_base = host;
1065 else if (host < host_base)
1066 host_base = host;
1067
1068 if (host + 1 > port_count + host_base)
1069 port_count = host + 1 - host_base;
1070
1071 }
1072 if (dir)
1073 closedir(dir);
1074
1075 if (!port_count || imsm_enumerate_ports(hba_path, port_count,
1076 host_base, verbose) != 0) {
1077 if (verbose)
1078 fprintf(stderr, Name ": failed to enumerate ports\n");
1079 return 2;
1080 }
1081
1082 return 0;
1083 }
1084 #endif
1085
1086 static int match_home_imsm(struct supertype *st, char *homehost)
1087 {
1088 /* the imsm metadata format does not specify any host
1089 * identification information. We return -1 since we can never
1090 * confirm nor deny whether a given array is "meant" for this
1091 * host. We rely on compare_super and the 'family_num' field to
1092 * exclude member disks that do not belong, and we rely on
1093 * mdadm.conf to specify the arrays that should be assembled.
1094 * Auto-assembly may still pick up "foreign" arrays.
1095 */
1096
1097 return -1;
1098 }
1099
1100 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
1101 {
1102 /* The uuid returned here is used for:
1103 * uuid to put into bitmap file (Create, Grow)
1104 * uuid for backup header when saving critical section (Grow)
1105 * comparing uuids when re-adding a device into an array
1106 * In these cases the uuid required is that of the data-array,
1107 * not the device-set.
1108 * uuid to recognise same set when adding a missing device back
1109 * to an array. This is a uuid for the device-set.
1110 *
1111 * For each of these we can make do with a truncated
1112 * or hashed uuid rather than the original, as long as
1113 * everyone agrees.
1114 * In each case the uuid required is that of the data-array,
1115 * not the device-set.
1116 */
1117 /* imsm does not track uuid's so we synthesis one using sha1 on
1118 * - The signature (Which is constant for all imsm array, but no matter)
1119 * - the family_num of the container
1120 * - the index number of the volume
1121 * - the 'serial' number of the volume.
1122 * Hopefully these are all constant.
1123 */
1124 struct intel_super *super = st->sb;
1125
1126 char buf[20];
1127 struct sha1_ctx ctx;
1128 struct imsm_dev *dev = NULL;
1129
1130 sha1_init_ctx(&ctx);
1131 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
1132 sha1_process_bytes(&super->anchor->family_num, sizeof(__u32), &ctx);
1133 if (super->current_vol >= 0)
1134 dev = get_imsm_dev(super, super->current_vol);
1135 if (dev) {
1136 __u32 vol = super->current_vol;
1137 sha1_process_bytes(&vol, sizeof(vol), &ctx);
1138 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
1139 }
1140 sha1_finish_ctx(&ctx, buf);
1141 memcpy(uuid, buf, 4*4);
1142 }
1143
1144 #if 0
1145 static void
1146 get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
1147 {
1148 __u8 *v = get_imsm_version(mpb);
1149 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
1150 char major[] = { 0, 0, 0 };
1151 char minor[] = { 0 ,0, 0 };
1152 char patch[] = { 0, 0, 0 };
1153 char *ver_parse[] = { major, minor, patch };
1154 int i, j;
1155
1156 i = j = 0;
1157 while (*v != '\0' && v < end) {
1158 if (*v != '.' && j < 2)
1159 ver_parse[i][j++] = *v;
1160 else {
1161 i++;
1162 j = 0;
1163 }
1164 v++;
1165 }
1166
1167 *m = strtol(minor, NULL, 0);
1168 *p = strtol(patch, NULL, 0);
1169 }
1170 #endif
1171
1172 static int imsm_level_to_layout(int level)
1173 {
1174 switch (level) {
1175 case 0:
1176 case 1:
1177 return 0;
1178 case 5:
1179 case 6:
1180 return ALGORITHM_LEFT_ASYMMETRIC;
1181 case 10:
1182 return 0x102;
1183 }
1184 return UnSet;
1185 }
1186
1187 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info)
1188 {
1189 struct intel_super *super = st->sb;
1190 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
1191 struct imsm_map *map = get_imsm_map(dev, 0);
1192 struct dl *dl;
1193
1194 for (dl = super->disks; dl; dl = dl->next)
1195 if (dl->raiddisk == info->disk.raid_disk)
1196 break;
1197 info->container_member = super->current_vol;
1198 info->array.raid_disks = map->num_members;
1199 info->array.level = get_imsm_raid_level(map);
1200 info->array.layout = imsm_level_to_layout(info->array.level);
1201 info->array.md_minor = -1;
1202 info->array.ctime = 0;
1203 info->array.utime = 0;
1204 info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9;
1205 info->array.state = !dev->vol.dirty;
1206
1207 info->disk.major = 0;
1208 info->disk.minor = 0;
1209 if (dl) {
1210 info->disk.major = dl->major;
1211 info->disk.minor = dl->minor;
1212 }
1213
1214 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
1215 info->component_size = __le32_to_cpu(map->blocks_per_member);
1216 memset(info->uuid, 0, sizeof(info->uuid));
1217
1218 if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty)
1219 info->resync_start = 0;
1220 else if (dev->vol.migr_state)
1221 info->resync_start = __le32_to_cpu(dev->vol.curr_migr_unit);
1222 else
1223 info->resync_start = ~0ULL;
1224
1225 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1226 info->name[MAX_RAID_SERIAL_LEN] = 0;
1227
1228 info->array.major_version = -1;
1229 info->array.minor_version = -2;
1230 sprintf(info->text_version, "/%s/%d",
1231 devnum2devname(st->container_dev),
1232 info->container_member);
1233 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
1234 uuid_from_super_imsm(st, info->uuid);
1235 }
1236
1237 /* check the config file to see if we can return a real uuid for this spare */
1238 static void fixup_container_spare_uuid(struct mdinfo *inf)
1239 {
1240 struct mddev_ident_s *array_list;
1241
1242 if (inf->array.level != LEVEL_CONTAINER ||
1243 memcmp(inf->uuid, uuid_match_any, sizeof(int[4])) != 0)
1244 return;
1245
1246 array_list = conf_get_ident(NULL);
1247
1248 for (; array_list; array_list = array_list->next) {
1249 if (array_list->uuid_set) {
1250 struct supertype *_sst; /* spare supertype */
1251 struct supertype *_cst; /* container supertype */
1252
1253 _cst = array_list->st;
1254 _sst = _cst->ss->match_metadata_desc(inf->text_version);
1255 if (_sst) {
1256 memcpy(inf->uuid, array_list->uuid, sizeof(int[4]));
1257 free(_sst);
1258 break;
1259 }
1260 }
1261 }
1262 }
1263
1264 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
1265 {
1266 struct intel_super *super = st->sb;
1267 struct imsm_disk *disk;
1268 __u32 s;
1269
1270 if (super->current_vol >= 0) {
1271 getinfo_super_imsm_volume(st, info);
1272 return;
1273 }
1274
1275 /* Set raid_disks to zero so that Assemble will always pull in valid
1276 * spares
1277 */
1278 info->array.raid_disks = 0;
1279 info->array.level = LEVEL_CONTAINER;
1280 info->array.layout = 0;
1281 info->array.md_minor = -1;
1282 info->array.ctime = 0; /* N/A for imsm */
1283 info->array.utime = 0;
1284 info->array.chunk_size = 0;
1285
1286 info->disk.major = 0;
1287 info->disk.minor = 0;
1288 info->disk.raid_disk = -1;
1289 info->reshape_active = 0;
1290 info->array.major_version = -1;
1291 info->array.minor_version = -2;
1292 strcpy(info->text_version, "imsm");
1293 info->safe_mode_delay = 0;
1294 info->disk.number = -1;
1295 info->disk.state = 0;
1296 info->name[0] = 0;
1297
1298 if (super->disks) {
1299 __u32 reserved = imsm_reserved_sectors(super, super->disks);
1300
1301 disk = &super->disks->disk;
1302 info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved;
1303 info->component_size = reserved;
1304 s = disk->status;
1305 info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
1306 /* we don't change info->disk.raid_disk here because
1307 * this state will be finalized in mdmon after we have
1308 * found the 'most fresh' version of the metadata
1309 */
1310 info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
1311 info->disk.state |= s & SPARE_DISK ? 0 : (1 << MD_DISK_SYNC);
1312 }
1313
1314 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1315 * ->compare_super may have updated the 'num_raid_devs' field for spares
1316 */
1317 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
1318 uuid_from_super_imsm(st, info->uuid);
1319 else {
1320 memcpy(info->uuid, uuid_match_any, sizeof(int[4]));
1321 fixup_container_spare_uuid(info);
1322 }
1323 }
1324
1325 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
1326 char *update, char *devname, int verbose,
1327 int uuid_set, char *homehost)
1328 {
1329 /* FIXME */
1330
1331 /* For 'assemble' and 'force' we need to return non-zero if any
1332 * change was made. For others, the return value is ignored.
1333 * Update options are:
1334 * force-one : This device looks a bit old but needs to be included,
1335 * update age info appropriately.
1336 * assemble: clear any 'faulty' flag to allow this device to
1337 * be assembled.
1338 * force-array: Array is degraded but being forced, mark it clean
1339 * if that will be needed to assemble it.
1340 *
1341 * newdev: not used ????
1342 * grow: Array has gained a new device - this is currently for
1343 * linear only
1344 * resync: mark as dirty so a resync will happen.
1345 * name: update the name - preserving the homehost
1346 *
1347 * Following are not relevant for this imsm:
1348 * sparc2.2 : update from old dodgey metadata
1349 * super-minor: change the preferred_minor number
1350 * summaries: update redundant counters.
1351 * uuid: Change the uuid of the array to match watch is given
1352 * homehost: update the recorded homehost
1353 * _reshape_progress: record new reshape_progress position.
1354 */
1355 int rv = 0;
1356 //struct intel_super *super = st->sb;
1357 //struct imsm_super *mpb = super->mpb;
1358
1359 if (strcmp(update, "grow") == 0) {
1360 }
1361 if (strcmp(update, "resync") == 0) {
1362 /* dev->vol.dirty = 1; */
1363 }
1364
1365 /* IMSM has no concept of UUID or homehost */
1366
1367 return rv;
1368 }
1369
1370 static size_t disks_to_mpb_size(int disks)
1371 {
1372 size_t size;
1373
1374 size = sizeof(struct imsm_super);
1375 size += (disks - 1) * sizeof(struct imsm_disk);
1376 size += 2 * sizeof(struct imsm_dev);
1377 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1378 size += (4 - 2) * sizeof(struct imsm_map);
1379 /* 4 possible disk_ord_tbl's */
1380 size += 4 * (disks - 1) * sizeof(__u32);
1381
1382 return size;
1383 }
1384
1385 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
1386 {
1387 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
1388 return 0;
1389
1390 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
1391 }
1392
1393 static void free_devlist(struct intel_super *super)
1394 {
1395 struct intel_dev *dv;
1396
1397 while (super->devlist) {
1398 dv = super->devlist->next;
1399 free(super->devlist->dev);
1400 free(super->devlist);
1401 super->devlist = dv;
1402 }
1403 }
1404
1405 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
1406 {
1407 memcpy(dest, src, sizeof_imsm_dev(src, 0));
1408 }
1409
1410 static int compare_super_imsm(struct supertype *st, struct supertype *tst)
1411 {
1412 /*
1413 * return:
1414 * 0 same, or first was empty, and second was copied
1415 * 1 second had wrong number
1416 * 2 wrong uuid
1417 * 3 wrong other info
1418 */
1419 struct intel_super *first = st->sb;
1420 struct intel_super *sec = tst->sb;
1421
1422 if (!first) {
1423 st->sb = tst->sb;
1424 tst->sb = NULL;
1425 return 0;
1426 }
1427
1428 if (memcmp(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH) != 0)
1429 return 3;
1430
1431 /* if an anchor does not have num_raid_devs set then it is a free
1432 * floating spare
1433 */
1434 if (first->anchor->num_raid_devs > 0 &&
1435 sec->anchor->num_raid_devs > 0) {
1436 if (first->anchor->family_num != sec->anchor->family_num)
1437 return 3;
1438 }
1439
1440 /* if 'first' is a spare promote it to a populated mpb with sec's
1441 * family number
1442 */
1443 if (first->anchor->num_raid_devs == 0 &&
1444 sec->anchor->num_raid_devs > 0) {
1445 int i;
1446 struct intel_dev *dv;
1447 struct imsm_dev *dev;
1448
1449 /* we need to copy raid device info from sec if an allocation
1450 * fails here we don't associate the spare
1451 */
1452 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
1453 dv = malloc(sizeof(*dv));
1454 if (!dv)
1455 break;
1456 dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
1457 if (!dev) {
1458 free(dv);
1459 break;
1460 }
1461 dv->dev = dev;
1462 dv->index = i;
1463 dv->next = first->devlist;
1464 first->devlist = dv;
1465 }
1466 if (i <= sec->anchor->num_raid_devs) {
1467 /* allocation failure */
1468 free_devlist(first);
1469 fprintf(stderr, "imsm: failed to associate spare\n");
1470 return 3;
1471 }
1472 for (i = 0; i < sec->anchor->num_raid_devs; i++)
1473 imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
1474
1475 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
1476 first->anchor->family_num = sec->anchor->family_num;
1477 }
1478
1479 return 0;
1480 }
1481
1482 static void fd2devname(int fd, char *name)
1483 {
1484 struct stat st;
1485 char path[256];
1486 char dname[100];
1487 char *nm;
1488 int rv;
1489
1490 name[0] = '\0';
1491 if (fstat(fd, &st) != 0)
1492 return;
1493 sprintf(path, "/sys/dev/block/%d:%d",
1494 major(st.st_rdev), minor(st.st_rdev));
1495
1496 rv = readlink(path, dname, sizeof(dname));
1497 if (rv <= 0)
1498 return;
1499
1500 dname[rv] = '\0';
1501 nm = strrchr(dname, '/');
1502 nm++;
1503 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
1504 }
1505
1506
1507 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
1508
1509 static int imsm_read_serial(int fd, char *devname,
1510 __u8 serial[MAX_RAID_SERIAL_LEN])
1511 {
1512 unsigned char scsi_serial[255];
1513 int rv;
1514 int rsp_len;
1515 int len;
1516 char *dest;
1517 char *src;
1518 char *rsp_buf;
1519 int i;
1520
1521 memset(scsi_serial, 0, sizeof(scsi_serial));
1522
1523 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
1524
1525 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
1526 memset(serial, 0, MAX_RAID_SERIAL_LEN);
1527 fd2devname(fd, (char *) serial);
1528 return 0;
1529 }
1530
1531 if (rv != 0) {
1532 if (devname)
1533 fprintf(stderr,
1534 Name ": Failed to retrieve serial for %s\n",
1535 devname);
1536 return rv;
1537 }
1538
1539 rsp_len = scsi_serial[3];
1540 if (!rsp_len) {
1541 if (devname)
1542 fprintf(stderr,
1543 Name ": Failed to retrieve serial for %s\n",
1544 devname);
1545 return 2;
1546 }
1547 rsp_buf = (char *) &scsi_serial[4];
1548
1549 /* trim all whitespace and non-printable characters and convert
1550 * ':' to ';'
1551 */
1552 for (i = 0, dest = rsp_buf; i < rsp_len; i++) {
1553 src = &rsp_buf[i];
1554 if (*src > 0x20) {
1555 /* ':' is reserved for use in placeholder serial
1556 * numbers for missing disks
1557 */
1558 if (*src == ':')
1559 *dest++ = ';';
1560 else
1561 *dest++ = *src;
1562 }
1563 }
1564 len = dest - rsp_buf;
1565 dest = rsp_buf;
1566
1567 /* truncate leading characters */
1568 if (len > MAX_RAID_SERIAL_LEN) {
1569 dest += len - MAX_RAID_SERIAL_LEN;
1570 len = MAX_RAID_SERIAL_LEN;
1571 }
1572
1573 memset(serial, 0, MAX_RAID_SERIAL_LEN);
1574 memcpy(serial, dest, len);
1575
1576 return 0;
1577 }
1578
1579 static int serialcmp(__u8 *s1, __u8 *s2)
1580 {
1581 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
1582 }
1583
1584 static void serialcpy(__u8 *dest, __u8 *src)
1585 {
1586 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
1587 }
1588
1589 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
1590 {
1591 struct dl *dl;
1592
1593 for (dl = super->disks; dl; dl = dl->next)
1594 if (serialcmp(dl->serial, serial) == 0)
1595 break;
1596
1597 return dl;
1598 }
1599
1600 static int
1601 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
1602 {
1603 struct dl *dl;
1604 struct stat stb;
1605 int rv;
1606 int i;
1607 int alloc = 1;
1608 __u8 serial[MAX_RAID_SERIAL_LEN];
1609
1610 rv = imsm_read_serial(fd, devname, serial);
1611
1612 if (rv != 0)
1613 return 2;
1614
1615 /* check if this is a disk we have seen before. it may be a spare in
1616 * super->disks while the current anchor believes it is a raid member,
1617 * check if we need to update dl->index
1618 */
1619 dl = serial_to_dl(serial, super);
1620 if (!dl)
1621 dl = malloc(sizeof(*dl));
1622 else
1623 alloc = 0;
1624
1625 if (!dl) {
1626 if (devname)
1627 fprintf(stderr,
1628 Name ": failed to allocate disk buffer for %s\n",
1629 devname);
1630 return 2;
1631 }
1632
1633 if (alloc) {
1634 fstat(fd, &stb);
1635 dl->major = major(stb.st_rdev);
1636 dl->minor = minor(stb.st_rdev);
1637 dl->next = super->disks;
1638 dl->fd = keep_fd ? fd : -1;
1639 dl->devname = devname ? strdup(devname) : NULL;
1640 serialcpy(dl->serial, serial);
1641 dl->index = -2;
1642 dl->e = NULL;
1643 } else if (keep_fd) {
1644 close(dl->fd);
1645 dl->fd = fd;
1646 }
1647
1648 /* look up this disk's index in the current anchor */
1649 for (i = 0; i < super->anchor->num_disks; i++) {
1650 struct imsm_disk *disk_iter;
1651
1652 disk_iter = __get_imsm_disk(super->anchor, i);
1653
1654 if (serialcmp(disk_iter->serial, dl->serial) == 0) {
1655 dl->disk = *disk_iter;
1656 /* only set index on disks that are a member of a
1657 * populated contianer, i.e. one with raid_devs
1658 */
1659 if (dl->disk.status & FAILED_DISK)
1660 dl->index = -2;
1661 else if (dl->disk.status & SPARE_DISK)
1662 dl->index = -1;
1663 else
1664 dl->index = i;
1665
1666 break;
1667 }
1668 }
1669
1670 /* no match, maybe a stale failed drive */
1671 if (i == super->anchor->num_disks && dl->index >= 0) {
1672 dl->disk = *__get_imsm_disk(super->anchor, dl->index);
1673 if (dl->disk.status & FAILED_DISK)
1674 dl->index = -2;
1675 }
1676
1677 if (alloc)
1678 super->disks = dl;
1679
1680 return 0;
1681 }
1682
1683 #ifndef MDASSEMBLE
1684 /* When migrating map0 contains the 'destination' state while map1
1685 * contains the current state. When not migrating map0 contains the
1686 * current state. This routine assumes that map[0].map_state is set to
1687 * the current array state before being called.
1688 *
1689 * Migration is indicated by one of the following states
1690 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
1691 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
1692 * map1state=unitialized)
1693 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
1694 * map1state=normal)
1695 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
1696 * map1state=degraded)
1697 */
1698 static void migrate(struct imsm_dev *dev, __u8 to_state, int migr_type)
1699 {
1700 struct imsm_map *dest;
1701 struct imsm_map *src = get_imsm_map(dev, 0);
1702
1703 dev->vol.migr_state = 1;
1704 set_migr_type(dev, migr_type);
1705 dev->vol.curr_migr_unit = 0;
1706 dest = get_imsm_map(dev, 1);
1707
1708 /* duplicate and then set the target end state in map[0] */
1709 memcpy(dest, src, sizeof_imsm_map(src));
1710 if (migr_type == MIGR_REBUILD) {
1711 __u32 ord;
1712 int i;
1713
1714 for (i = 0; i < src->num_members; i++) {
1715 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
1716 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
1717 }
1718 }
1719
1720 src->map_state = to_state;
1721 }
1722
1723 static void end_migration(struct imsm_dev *dev, __u8 map_state)
1724 {
1725 struct imsm_map *map = get_imsm_map(dev, 0);
1726 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
1727 int i;
1728
1729 /* merge any IMSM_ORD_REBUILD bits that were not successfully
1730 * completed in the last migration.
1731 *
1732 * FIXME add support for online capacity expansion and
1733 * raid-level-migration
1734 */
1735 for (i = 0; i < prev->num_members; i++)
1736 map->disk_ord_tbl[i] |= prev->disk_ord_tbl[i];
1737
1738 dev->vol.migr_state = 0;
1739 dev->vol.curr_migr_unit = 0;
1740 map->map_state = map_state;
1741 }
1742 #endif
1743
1744 static int parse_raid_devices(struct intel_super *super)
1745 {
1746 int i;
1747 struct imsm_dev *dev_new;
1748 size_t len, len_migr;
1749 size_t space_needed = 0;
1750 struct imsm_super *mpb = super->anchor;
1751
1752 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1753 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
1754 struct intel_dev *dv;
1755
1756 len = sizeof_imsm_dev(dev_iter, 0);
1757 len_migr = sizeof_imsm_dev(dev_iter, 1);
1758 if (len_migr > len)
1759 space_needed += len_migr - len;
1760
1761 dv = malloc(sizeof(*dv));
1762 if (!dv)
1763 return 1;
1764 dev_new = malloc(len_migr);
1765 if (!dev_new) {
1766 free(dv);
1767 return 1;
1768 }
1769 imsm_copy_dev(dev_new, dev_iter);
1770 dv->dev = dev_new;
1771 dv->index = i;
1772 dv->next = super->devlist;
1773 super->devlist = dv;
1774 }
1775
1776 /* ensure that super->buf is large enough when all raid devices
1777 * are migrating
1778 */
1779 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
1780 void *buf;
1781
1782 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512);
1783 if (posix_memalign(&buf, 512, len) != 0)
1784 return 1;
1785
1786 memcpy(buf, super->buf, super->len);
1787 memset(buf + super->len, 0, len - super->len);
1788 free(super->buf);
1789 super->buf = buf;
1790 super->len = len;
1791 }
1792
1793 return 0;
1794 }
1795
1796 /* retrieve a pointer to the bbm log which starts after all raid devices */
1797 struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
1798 {
1799 void *ptr = NULL;
1800
1801 if (__le32_to_cpu(mpb->bbm_log_size)) {
1802 ptr = mpb;
1803 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
1804 }
1805
1806 return ptr;
1807 }
1808
1809 static void __free_imsm(struct intel_super *super, int free_disks);
1810
1811 /* load_imsm_mpb - read matrix metadata
1812 * allocates super->mpb to be freed by free_super
1813 */
1814 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
1815 {
1816 unsigned long long dsize;
1817 unsigned long long sectors;
1818 struct stat;
1819 struct imsm_super *anchor;
1820 __u32 check_sum;
1821 int rc;
1822
1823 get_dev_size(fd, NULL, &dsize);
1824
1825 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
1826 if (devname)
1827 fprintf(stderr,
1828 Name ": Cannot seek to anchor block on %s: %s\n",
1829 devname, strerror(errno));
1830 return 1;
1831 }
1832
1833 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
1834 if (devname)
1835 fprintf(stderr,
1836 Name ": Failed to allocate imsm anchor buffer"
1837 " on %s\n", devname);
1838 return 1;
1839 }
1840 if (read(fd, anchor, 512) != 512) {
1841 if (devname)
1842 fprintf(stderr,
1843 Name ": Cannot read anchor block on %s: %s\n",
1844 devname, strerror(errno));
1845 free(anchor);
1846 return 1;
1847 }
1848
1849 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
1850 if (devname)
1851 fprintf(stderr,
1852 Name ": no IMSM anchor on %s\n", devname);
1853 free(anchor);
1854 return 2;
1855 }
1856
1857 __free_imsm(super, 0);
1858 super->len = ROUND_UP(anchor->mpb_size, 512);
1859 if (posix_memalign(&super->buf, 512, super->len) != 0) {
1860 if (devname)
1861 fprintf(stderr,
1862 Name ": unable to allocate %zu byte mpb buffer\n",
1863 super->len);
1864 free(anchor);
1865 return 2;
1866 }
1867 memcpy(super->buf, anchor, 512);
1868
1869 sectors = mpb_sectors(anchor) - 1;
1870 free(anchor);
1871 if (!sectors) {
1872 check_sum = __gen_imsm_checksum(super->anchor);
1873 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
1874 if (devname)
1875 fprintf(stderr,
1876 Name ": IMSM checksum %x != %x on %s\n",
1877 check_sum,
1878 __le32_to_cpu(super->anchor->check_sum),
1879 devname);
1880 return 2;
1881 }
1882
1883 rc = load_imsm_disk(fd, super, devname, 0);
1884 if (rc == 0)
1885 rc = parse_raid_devices(super);
1886 return rc;
1887 }
1888
1889 /* read the extended mpb */
1890 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
1891 if (devname)
1892 fprintf(stderr,
1893 Name ": Cannot seek to extended mpb on %s: %s\n",
1894 devname, strerror(errno));
1895 return 1;
1896 }
1897
1898 if (read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
1899 if (devname)
1900 fprintf(stderr,
1901 Name ": Cannot read extended mpb on %s: %s\n",
1902 devname, strerror(errno));
1903 return 2;
1904 }
1905
1906 check_sum = __gen_imsm_checksum(super->anchor);
1907 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
1908 if (devname)
1909 fprintf(stderr,
1910 Name ": IMSM checksum %x != %x on %s\n",
1911 check_sum, __le32_to_cpu(super->anchor->check_sum),
1912 devname);
1913 return 3;
1914 }
1915
1916 /* FIXME the BBM log is disk specific so we cannot use this global
1917 * buffer for all disks. Ok for now since we only look at the global
1918 * bbm_log_size parameter to gate assembly
1919 */
1920 super->bbm_log = __get_imsm_bbm_log(super->anchor);
1921
1922 rc = load_imsm_disk(fd, super, devname, 0);
1923 if (rc == 0)
1924 rc = parse_raid_devices(super);
1925
1926 return rc;
1927 }
1928
1929 static void __free_imsm_disk(struct dl *d)
1930 {
1931 if (d->fd >= 0)
1932 close(d->fd);
1933 if (d->devname)
1934 free(d->devname);
1935 if (d->e)
1936 free(d->e);
1937 free(d);
1938
1939 }
1940 static void free_imsm_disks(struct intel_super *super)
1941 {
1942 struct dl *d;
1943
1944 while (super->disks) {
1945 d = super->disks;
1946 super->disks = d->next;
1947 __free_imsm_disk(d);
1948 }
1949 while (super->missing) {
1950 d = super->missing;
1951 super->missing = d->next;
1952 __free_imsm_disk(d);
1953 }
1954
1955 }
1956
1957 /* free all the pieces hanging off of a super pointer */
1958 static void __free_imsm(struct intel_super *super, int free_disks)
1959 {
1960 if (super->buf) {
1961 free(super->buf);
1962 super->buf = NULL;
1963 }
1964 if (free_disks)
1965 free_imsm_disks(super);
1966 free_devlist(super);
1967 if (super->hba) {
1968 free((void *) super->hba);
1969 super->hba = NULL;
1970 }
1971 }
1972
1973 static void free_imsm(struct intel_super *super)
1974 {
1975 __free_imsm(super, 1);
1976 free(super);
1977 }
1978
1979 static void free_super_imsm(struct supertype *st)
1980 {
1981 struct intel_super *super = st->sb;
1982
1983 if (!super)
1984 return;
1985
1986 free_imsm(super);
1987 st->sb = NULL;
1988 }
1989
1990 static struct intel_super *alloc_super(int creating_imsm)
1991 {
1992 struct intel_super *super = malloc(sizeof(*super));
1993
1994 if (super) {
1995 memset(super, 0, sizeof(*super));
1996 super->creating_imsm = creating_imsm;
1997 super->current_vol = -1;
1998 super->create_offset = ~((__u32 ) 0);
1999 if (!check_env("IMSM_NO_PLATFORM"))
2000 super->orom = find_imsm_orom();
2001 if (super->orom && !check_env("IMSM_TEST_OROM")) {
2002 struct sys_dev *list, *ent;
2003
2004 /* find the first intel ahci controller */
2005 list = find_driver_devices("pci", "ahci");
2006 for (ent = list; ent; ent = ent->next)
2007 if (devpath_to_vendor(ent->path) == 0x8086)
2008 break;
2009 if (ent) {
2010 super->hba = ent->path;
2011 ent->path = NULL;
2012 }
2013 free_sys_dev(&list);
2014 }
2015 }
2016
2017 return super;
2018 }
2019
2020 #ifndef MDASSEMBLE
2021 /* find_missing - helper routine for load_super_imsm_all that identifies
2022 * disks that have disappeared from the system. This routine relies on
2023 * the mpb being uptodate, which it is at load time.
2024 */
2025 static int find_missing(struct intel_super *super)
2026 {
2027 int i;
2028 struct imsm_super *mpb = super->anchor;
2029 struct dl *dl;
2030 struct imsm_disk *disk;
2031
2032 for (i = 0; i < mpb->num_disks; i++) {
2033 disk = __get_imsm_disk(mpb, i);
2034 dl = serial_to_dl(disk->serial, super);
2035 if (dl)
2036 continue;
2037
2038 dl = malloc(sizeof(*dl));
2039 if (!dl)
2040 return 1;
2041 dl->major = 0;
2042 dl->minor = 0;
2043 dl->fd = -1;
2044 dl->devname = strdup("missing");
2045 dl->index = i;
2046 serialcpy(dl->serial, disk->serial);
2047 dl->disk = *disk;
2048 dl->e = NULL;
2049 dl->next = super->missing;
2050 super->missing = dl;
2051 }
2052
2053 return 0;
2054 }
2055
2056 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
2057 char *devname, int keep_fd)
2058 {
2059 struct mdinfo *sra;
2060 struct intel_super *super;
2061 struct mdinfo *sd, *best = NULL;
2062 __u32 bestgen = 0;
2063 __u32 gen;
2064 char nm[20];
2065 int dfd;
2066 int rv;
2067 int devnum = fd2devnum(fd);
2068 int retry;
2069 enum sysfs_read_flags flags;
2070
2071 flags = GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE;
2072 if (mdmon_running(devnum))
2073 flags |= SKIP_GONE_DEVS;
2074
2075 /* check if 'fd' an opened container */
2076 sra = sysfs_read(fd, 0, flags);
2077 if (!sra)
2078 return 1;
2079
2080 if (sra->array.major_version != -1 ||
2081 sra->array.minor_version != -2 ||
2082 strcmp(sra->text_version, "imsm") != 0)
2083 return 1;
2084
2085 super = alloc_super(0);
2086 if (!super)
2087 return 1;
2088
2089 /* find the most up to date disk in this array, skipping spares */
2090 for (sd = sra->devs; sd; sd = sd->next) {
2091 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2092 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
2093 if (dfd < 0) {
2094 free_imsm(super);
2095 return 2;
2096 }
2097 rv = load_imsm_mpb(dfd, super, NULL);
2098
2099 /* retry the load if we might have raced against mdmon */
2100 if (rv == 3 && mdmon_running(devnum))
2101 for (retry = 0; retry < 3; retry++) {
2102 usleep(3000);
2103 rv = load_imsm_mpb(dfd, super, NULL);
2104 if (rv != 3)
2105 break;
2106 }
2107 if (!keep_fd)
2108 close(dfd);
2109 if (rv == 0) {
2110 if (super->anchor->num_raid_devs == 0)
2111 gen = 0;
2112 else
2113 gen = __le32_to_cpu(super->anchor->generation_num);
2114 if (!best || gen > bestgen) {
2115 bestgen = gen;
2116 best = sd;
2117 }
2118 } else {
2119 free_imsm(super);
2120 return rv;
2121 }
2122 }
2123
2124 if (!best) {
2125 free_imsm(super);
2126 return 1;
2127 }
2128
2129 /* load the most up to date anchor */
2130 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
2131 dfd = dev_open(nm, O_RDONLY);
2132 if (dfd < 0) {
2133 free_imsm(super);
2134 return 1;
2135 }
2136 rv = load_imsm_mpb(dfd, super, NULL);
2137 close(dfd);
2138 if (rv != 0) {
2139 free_imsm(super);
2140 return 2;
2141 }
2142
2143 /* re-parse the disk list with the current anchor */
2144 for (sd = sra->devs ; sd ; sd = sd->next) {
2145 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2146 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
2147 if (dfd < 0) {
2148 free_imsm(super);
2149 return 2;
2150 }
2151 load_imsm_disk(dfd, super, NULL, keep_fd);
2152 if (!keep_fd)
2153 close(dfd);
2154 }
2155
2156
2157 if (find_missing(super) != 0) {
2158 free_imsm(super);
2159 return 2;
2160 }
2161
2162 if (st->subarray[0]) {
2163 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
2164 super->current_vol = atoi(st->subarray);
2165 else
2166 return 1;
2167 }
2168
2169 *sbp = super;
2170 st->container_dev = devnum;
2171 if (st->ss == NULL) {
2172 st->ss = &super_imsm;
2173 st->minor_version = 0;
2174 st->max_devs = IMSM_MAX_DEVICES;
2175 }
2176 st->loaded_container = 1;
2177
2178 return 0;
2179 }
2180 #endif
2181
2182 static int load_super_imsm(struct supertype *st, int fd, char *devname)
2183 {
2184 struct intel_super *super;
2185 int rv;
2186
2187 #ifndef MDASSEMBLE
2188 if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0)
2189 return 0;
2190 #endif
2191 if (st->subarray[0])
2192 return 1; /* FIXME */
2193
2194 super = alloc_super(0);
2195 if (!super) {
2196 fprintf(stderr,
2197 Name ": malloc of %zu failed.\n",
2198 sizeof(*super));
2199 return 1;
2200 }
2201
2202 rv = load_imsm_mpb(fd, super, devname);
2203
2204 if (rv) {
2205 if (devname)
2206 fprintf(stderr,
2207 Name ": Failed to load all information "
2208 "sections on %s\n", devname);
2209 free_imsm(super);
2210 return rv;
2211 }
2212
2213 st->sb = super;
2214 if (st->ss == NULL) {
2215 st->ss = &super_imsm;
2216 st->minor_version = 0;
2217 st->max_devs = IMSM_MAX_DEVICES;
2218 }
2219 st->loaded_container = 0;
2220
2221 return 0;
2222 }
2223
2224 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
2225 {
2226 if (info->level == 1)
2227 return 128;
2228 return info->chunk_size >> 9;
2229 }
2230
2231 static __u32 info_to_num_data_stripes(mdu_array_info_t *info, int num_domains)
2232 {
2233 __u32 num_stripes;
2234
2235 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
2236 num_stripes /= num_domains;
2237
2238 return num_stripes;
2239 }
2240
2241 static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
2242 {
2243 if (info->level == 1)
2244 return info->size * 2;
2245 else
2246 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
2247 }
2248
2249 static void imsm_update_version_info(struct intel_super *super)
2250 {
2251 /* update the version and attributes */
2252 struct imsm_super *mpb = super->anchor;
2253 char *version;
2254 struct imsm_dev *dev;
2255 struct imsm_map *map;
2256 int i;
2257
2258 for (i = 0; i < mpb->num_raid_devs; i++) {
2259 dev = get_imsm_dev(super, i);
2260 map = get_imsm_map(dev, 0);
2261 if (__le32_to_cpu(dev->size_high) > 0)
2262 mpb->attributes |= MPB_ATTRIB_2TB;
2263
2264 /* FIXME detect when an array spans a port multiplier */
2265 #if 0
2266 mpb->attributes |= MPB_ATTRIB_PM;
2267 #endif
2268
2269 if (mpb->num_raid_devs > 1 ||
2270 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
2271 version = MPB_VERSION_ATTRIBS;
2272 switch (get_imsm_raid_level(map)) {
2273 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
2274 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
2275 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
2276 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
2277 }
2278 } else {
2279 if (map->num_members >= 5)
2280 version = MPB_VERSION_5OR6_DISK_ARRAY;
2281 else if (dev->status == DEV_CLONE_N_GO)
2282 version = MPB_VERSION_CNG;
2283 else if (get_imsm_raid_level(map) == 5)
2284 version = MPB_VERSION_RAID5;
2285 else if (map->num_members >= 3)
2286 version = MPB_VERSION_3OR4_DISK_ARRAY;
2287 else if (get_imsm_raid_level(map) == 1)
2288 version = MPB_VERSION_RAID1;
2289 else
2290 version = MPB_VERSION_RAID0;
2291 }
2292 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
2293 }
2294 }
2295
2296 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
2297 unsigned long long size, char *name,
2298 char *homehost, int *uuid)
2299 {
2300 /* We are creating a volume inside a pre-existing container.
2301 * so st->sb is already set.
2302 */
2303 struct intel_super *super = st->sb;
2304 struct imsm_super *mpb = super->anchor;
2305 struct intel_dev *dv;
2306 struct imsm_dev *dev;
2307 struct imsm_vol *vol;
2308 struct imsm_map *map;
2309 int idx = mpb->num_raid_devs;
2310 int i;
2311 unsigned long long array_blocks;
2312 size_t size_old, size_new;
2313 __u32 num_data_stripes;
2314
2315 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
2316 fprintf(stderr, Name": This imsm-container already has the "
2317 "maximum of %d volumes\n", super->orom->vpa);
2318 return 0;
2319 }
2320
2321 /* ensure the mpb is large enough for the new data */
2322 size_old = __le32_to_cpu(mpb->mpb_size);
2323 size_new = disks_to_mpb_size(info->nr_disks);
2324 if (size_new > size_old) {
2325 void *mpb_new;
2326 size_t size_round = ROUND_UP(size_new, 512);
2327
2328 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
2329 fprintf(stderr, Name": could not allocate new mpb\n");
2330 return 0;
2331 }
2332 memcpy(mpb_new, mpb, size_old);
2333 free(mpb);
2334 mpb = mpb_new;
2335 super->anchor = mpb_new;
2336 mpb->mpb_size = __cpu_to_le32(size_new);
2337 memset(mpb_new + size_old, 0, size_round - size_old);
2338 }
2339 super->current_vol = idx;
2340 /* when creating the first raid device in this container set num_disks
2341 * to zero, i.e. delete this spare and add raid member devices in
2342 * add_to_super_imsm_volume()
2343 */
2344 if (super->current_vol == 0)
2345 mpb->num_disks = 0;
2346
2347 for (i = 0; i < super->current_vol; i++) {
2348 dev = get_imsm_dev(super, i);
2349 if (strncmp((char *) dev->volume, name,
2350 MAX_RAID_SERIAL_LEN) == 0) {
2351 fprintf(stderr, Name": '%s' is already defined for this container\n",
2352 name);
2353 return 0;
2354 }
2355 }
2356
2357 sprintf(st->subarray, "%d", idx);
2358 dv = malloc(sizeof(*dv));
2359 if (!dv) {
2360 fprintf(stderr, Name ": failed to allocate device list entry\n");
2361 return 0;
2362 }
2363 dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
2364 if (!dev) {
2365 free(dv);
2366 fprintf(stderr, Name": could not allocate raid device\n");
2367 return 0;
2368 }
2369 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
2370 if (info->level == 1)
2371 array_blocks = info_to_blocks_per_member(info);
2372 else
2373 array_blocks = calc_array_size(info->level, info->raid_disks,
2374 info->layout, info->chunk_size,
2375 info->size*2);
2376 dev->size_low = __cpu_to_le32((__u32) array_blocks);
2377 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
2378 dev->status = __cpu_to_le32(0);
2379 dev->reserved_blocks = __cpu_to_le32(0);
2380 vol = &dev->vol;
2381 vol->migr_state = 0;
2382 set_migr_type(dev, MIGR_INIT);
2383 vol->dirty = 0;
2384 vol->curr_migr_unit = 0;
2385 map = get_imsm_map(dev, 0);
2386 map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
2387 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
2388 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
2389 map->failed_disk_num = ~0;
2390 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
2391 IMSM_T_STATE_NORMAL;
2392
2393 if (info->level == 1 && info->raid_disks > 2) {
2394 fprintf(stderr, Name": imsm does not support more than 2 disks"
2395 "in a raid1 volume\n");
2396 return 0;
2397 }
2398 if (info->level == 10) {
2399 map->raid_level = 1;
2400 map->num_domains = info->raid_disks / 2;
2401 } else {
2402 map->raid_level = info->level;
2403 map->num_domains = 1;
2404 }
2405 num_data_stripes = info_to_num_data_stripes(info, map->num_domains);
2406 map->num_data_stripes = __cpu_to_le32(num_data_stripes);
2407
2408 map->num_members = info->raid_disks;
2409 for (i = 0; i < map->num_members; i++) {
2410 /* initialized in add_to_super */
2411 set_imsm_ord_tbl_ent(map, i, 0);
2412 }
2413 mpb->num_raid_devs++;
2414
2415 dv->dev = dev;
2416 dv->index = super->current_vol;
2417 dv->next = super->devlist;
2418 super->devlist = dv;
2419
2420 imsm_update_version_info(super);
2421
2422 return 1;
2423 }
2424
2425 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
2426 unsigned long long size, char *name,
2427 char *homehost, int *uuid)
2428 {
2429 /* This is primarily called by Create when creating a new array.
2430 * We will then get add_to_super called for each component, and then
2431 * write_init_super called to write it out to each device.
2432 * For IMSM, Create can create on fresh devices or on a pre-existing
2433 * array.
2434 * To create on a pre-existing array a different method will be called.
2435 * This one is just for fresh drives.
2436 */
2437 struct intel_super *super;
2438 struct imsm_super *mpb;
2439 size_t mpb_size;
2440 char *version;
2441
2442 if (!info) {
2443 st->sb = NULL;
2444 return 0;
2445 }
2446 if (st->sb)
2447 return init_super_imsm_volume(st, info, size, name, homehost,
2448 uuid);
2449
2450 super = alloc_super(1);
2451 if (!super)
2452 return 0;
2453 mpb_size = disks_to_mpb_size(info->nr_disks);
2454 if (posix_memalign(&super->buf, 512, mpb_size) != 0) {
2455 free(super);
2456 return 0;
2457 }
2458 mpb = super->buf;
2459 memset(mpb, 0, mpb_size);
2460
2461 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
2462
2463 version = (char *) mpb->sig;
2464 strcpy(version, MPB_SIGNATURE);
2465 version += strlen(MPB_SIGNATURE);
2466 strcpy(version, MPB_VERSION_RAID0);
2467 mpb->mpb_size = mpb_size;
2468
2469 st->sb = super;
2470 return 1;
2471 }
2472
2473 #ifndef MDASSEMBLE
2474 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
2475 int fd, char *devname)
2476 {
2477 struct intel_super *super = st->sb;
2478 struct imsm_super *mpb = super->anchor;
2479 struct dl *dl;
2480 struct imsm_dev *dev;
2481 struct imsm_map *map;
2482
2483 dev = get_imsm_dev(super, super->current_vol);
2484 map = get_imsm_map(dev, 0);
2485
2486 if (! (dk->state & (1<<MD_DISK_SYNC))) {
2487 fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
2488 devname);
2489 return 1;
2490 }
2491
2492 if (fd == -1) {
2493 /* we're doing autolayout so grab the pre-marked (in
2494 * validate_geometry) raid_disk
2495 */
2496 for (dl = super->disks; dl; dl = dl->next)
2497 if (dl->raiddisk == dk->raid_disk)
2498 break;
2499 } else {
2500 for (dl = super->disks; dl ; dl = dl->next)
2501 if (dl->major == dk->major &&
2502 dl->minor == dk->minor)
2503 break;
2504 }
2505
2506 if (!dl) {
2507 fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
2508 return 1;
2509 }
2510
2511 /* add a pristine spare to the metadata */
2512 if (dl->index < 0) {
2513 dl->index = super->anchor->num_disks;
2514 super->anchor->num_disks++;
2515 }
2516 set_imsm_ord_tbl_ent(map, dk->number, dl->index);
2517 dl->disk.status = CONFIGURED_DISK | USABLE_DISK;
2518
2519 /* if we are creating the first raid device update the family number */
2520 if (super->current_vol == 0) {
2521 __u32 sum;
2522 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
2523 struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
2524
2525 *_dev = *dev;
2526 *_disk = dl->disk;
2527 sum = __gen_imsm_checksum(mpb);
2528 mpb->family_num = __cpu_to_le32(sum);
2529 }
2530
2531 return 0;
2532 }
2533
2534 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
2535 int fd, char *devname)
2536 {
2537 struct intel_super *super = st->sb;
2538 struct dl *dd;
2539 unsigned long long size;
2540 __u32 id;
2541 int rv;
2542 struct stat stb;
2543
2544 /* if we are on an RAID enabled platform check that the disk is
2545 * attached to the raid controller
2546 */
2547 if (super->hba && !disk_attached_to_hba(fd, super->hba)) {
2548 fprintf(stderr,
2549 Name ": %s is not attached to the raid controller: %s\n",
2550 devname ? : "disk", super->hba);
2551 return 1;
2552 }
2553
2554 if (super->current_vol >= 0)
2555 return add_to_super_imsm_volume(st, dk, fd, devname);
2556
2557 fstat(fd, &stb);
2558 dd = malloc(sizeof(*dd));
2559 if (!dd) {
2560 fprintf(stderr,
2561 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
2562 return 1;
2563 }
2564 memset(dd, 0, sizeof(*dd));
2565 dd->major = major(stb.st_rdev);
2566 dd->minor = minor(stb.st_rdev);
2567 dd->index = -1;
2568 dd->devname = devname ? strdup(devname) : NULL;
2569 dd->fd = fd;
2570 dd->e = NULL;
2571 rv = imsm_read_serial(fd, devname, dd->serial);
2572 if (rv) {
2573 fprintf(stderr,
2574 Name ": failed to retrieve scsi serial, aborting\n");
2575 free(dd);
2576 abort();
2577 }
2578
2579 get_dev_size(fd, NULL, &size);
2580 size /= 512;
2581 serialcpy(dd->disk.serial, dd->serial);
2582 dd->disk.total_blocks = __cpu_to_le32(size);
2583 dd->disk.status = USABLE_DISK | SPARE_DISK;
2584 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
2585 dd->disk.scsi_id = __cpu_to_le32(id);
2586 else
2587 dd->disk.scsi_id = __cpu_to_le32(0);
2588
2589 if (st->update_tail) {
2590 dd->next = super->add;
2591 super->add = dd;
2592 } else {
2593 dd->next = super->disks;
2594 super->disks = dd;
2595 }
2596
2597 return 0;
2598 }
2599
2600 static int store_imsm_mpb(int fd, struct intel_super *super);
2601
2602 /* spare records have their own family number and do not have any defined raid
2603 * devices
2604 */
2605 static int write_super_imsm_spares(struct intel_super *super, int doclose)
2606 {
2607 struct imsm_super mpb_save;
2608 struct imsm_super *mpb = super->anchor;
2609 __u32 sum;
2610 struct dl *d;
2611
2612 mpb_save = *mpb;
2613 mpb->num_raid_devs = 0;
2614 mpb->num_disks = 1;
2615 mpb->mpb_size = sizeof(struct imsm_super);
2616 mpb->generation_num = __cpu_to_le32(1UL);
2617
2618 for (d = super->disks; d; d = d->next) {
2619 if (d->index != -1)
2620 continue;
2621
2622 mpb->disk[0] = d->disk;
2623 sum = __gen_imsm_checksum(mpb);
2624 mpb->family_num = __cpu_to_le32(sum);
2625 sum = __gen_imsm_checksum(mpb);
2626 mpb->check_sum = __cpu_to_le32(sum);
2627
2628 if (store_imsm_mpb(d->fd, super)) {
2629 fprintf(stderr, "%s: failed for device %d:%d %s\n",
2630 __func__, d->major, d->minor, strerror(errno));
2631 *mpb = mpb_save;
2632 return 1;
2633 }
2634 if (doclose) {
2635 close(d->fd);
2636 d->fd = -1;
2637 }
2638 }
2639
2640 *mpb = mpb_save;
2641 return 0;
2642 }
2643
2644 static int write_super_imsm(struct intel_super *super, int doclose)
2645 {
2646 struct imsm_super *mpb = super->anchor;
2647 struct dl *d;
2648 __u32 generation;
2649 __u32 sum;
2650 int spares = 0;
2651 int i;
2652 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
2653
2654 /* 'generation' is incremented everytime the metadata is written */
2655 generation = __le32_to_cpu(mpb->generation_num);
2656 generation++;
2657 mpb->generation_num = __cpu_to_le32(generation);
2658
2659 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
2660 for (d = super->disks; d; d = d->next) {
2661 if (d->index == -1)
2662 spares++;
2663 else
2664 mpb->disk[d->index] = d->disk;
2665 }
2666 for (d = super->missing; d; d = d->next)
2667 mpb->disk[d->index] = d->disk;
2668
2669 for (i = 0; i < mpb->num_raid_devs; i++) {
2670 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2671
2672 imsm_copy_dev(dev, get_imsm_dev(super, i));
2673 mpb_size += sizeof_imsm_dev(dev, 0);
2674 }
2675 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
2676 mpb->mpb_size = __cpu_to_le32(mpb_size);
2677
2678 /* recalculate checksum */
2679 sum = __gen_imsm_checksum(mpb);
2680 mpb->check_sum = __cpu_to_le32(sum);
2681
2682 /* write the mpb for disks that compose raid devices */
2683 for (d = super->disks; d ; d = d->next) {
2684 if (d->index < 0)
2685 continue;
2686 if (store_imsm_mpb(d->fd, super))
2687 fprintf(stderr, "%s: failed for device %d:%d %s\n",
2688 __func__, d->major, d->minor, strerror(errno));
2689 if (doclose) {
2690 close(d->fd);
2691 d->fd = -1;
2692 }
2693 }
2694
2695 if (spares)
2696 return write_super_imsm_spares(super, doclose);
2697
2698 return 0;
2699 }
2700
2701
2702 static int create_array(struct supertype *st)
2703 {
2704 size_t len;
2705 struct imsm_update_create_array *u;
2706 struct intel_super *super = st->sb;
2707 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
2708 struct imsm_map *map = get_imsm_map(dev, 0);
2709 struct disk_info *inf;
2710 struct imsm_disk *disk;
2711 int i;
2712 int idx;
2713
2714 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
2715 sizeof(*inf) * map->num_members;
2716 u = malloc(len);
2717 if (!u) {
2718 fprintf(stderr, "%s: failed to allocate update buffer\n",
2719 __func__);
2720 return 1;
2721 }
2722
2723 u->type = update_create_array;
2724 u->dev_idx = super->current_vol;
2725 imsm_copy_dev(&u->dev, dev);
2726 inf = get_disk_info(u);
2727 for (i = 0; i < map->num_members; i++) {
2728 idx = get_imsm_disk_idx(dev, i);
2729 disk = get_imsm_disk(super, idx);
2730 serialcpy(inf[i].serial, disk->serial);
2731 }
2732 append_metadata_update(st, u, len);
2733
2734 return 0;
2735 }
2736
2737 static int _add_disk(struct supertype *st)
2738 {
2739 struct intel_super *super = st->sb;
2740 size_t len;
2741 struct imsm_update_add_disk *u;
2742
2743 if (!super->add)
2744 return 0;
2745
2746 len = sizeof(*u);
2747 u = malloc(len);
2748 if (!u) {
2749 fprintf(stderr, "%s: failed to allocate update buffer\n",
2750 __func__);
2751 return 1;
2752 }
2753
2754 u->type = update_add_disk;
2755 append_metadata_update(st, u, len);
2756
2757 return 0;
2758 }
2759
2760 static int write_init_super_imsm(struct supertype *st)
2761 {
2762 if (st->update_tail) {
2763 /* queue the recently created array / added disk
2764 * as a metadata update */
2765 struct intel_super *super = st->sb;
2766 struct dl *d;
2767 int rv;
2768
2769 /* determine if we are creating a volume or adding a disk */
2770 if (super->current_vol < 0) {
2771 /* in the add disk case we are running in mdmon
2772 * context, so don't close fd's
2773 */
2774 return _add_disk(st);
2775 } else
2776 rv = create_array(st);
2777
2778 for (d = super->disks; d ; d = d->next) {
2779 close(d->fd);
2780 d->fd = -1;
2781 }
2782
2783 return rv;
2784 } else
2785 return write_super_imsm(st->sb, 1);
2786 }
2787 #endif
2788
2789 static int store_zero_imsm(struct supertype *st, int fd)
2790 {
2791 unsigned long long dsize;
2792 void *buf;
2793
2794 get_dev_size(fd, NULL, &dsize);
2795
2796 /* first block is stored on second to last sector of the disk */
2797 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
2798 return 1;
2799
2800 if (posix_memalign(&buf, 512, 512) != 0)
2801 return 1;
2802
2803 memset(buf, 0, 512);
2804 if (write(fd, buf, 512) != 512)
2805 return 1;
2806 return 0;
2807 }
2808
2809 static int imsm_bbm_log_size(struct imsm_super *mpb)
2810 {
2811 return __le32_to_cpu(mpb->bbm_log_size);
2812 }
2813
2814 #ifndef MDASSEMBLE
2815 static int validate_geometry_imsm_container(struct supertype *st, int level,
2816 int layout, int raiddisks, int chunk,
2817 unsigned long long size, char *dev,
2818 unsigned long long *freesize,
2819 int verbose)
2820 {
2821 int fd;
2822 unsigned long long ldsize;
2823 const struct imsm_orom *orom;
2824
2825 if (level != LEVEL_CONTAINER)
2826 return 0;
2827 if (!dev)
2828 return 1;
2829
2830 if (check_env("IMSM_NO_PLATFORM"))
2831 orom = NULL;
2832 else
2833 orom = find_imsm_orom();
2834 if (orom && raiddisks > orom->tds) {
2835 if (verbose)
2836 fprintf(stderr, Name ": %d exceeds maximum number of"
2837 " platform supported disks: %d\n",
2838 raiddisks, orom->tds);
2839 return 0;
2840 }
2841
2842 fd = open(dev, O_RDONLY|O_EXCL, 0);
2843 if (fd < 0) {
2844 if (verbose)
2845 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
2846 dev, strerror(errno));
2847 return 0;
2848 }
2849 if (!get_dev_size(fd, dev, &ldsize)) {
2850 close(fd);
2851 return 0;
2852 }
2853 close(fd);
2854
2855 *freesize = avail_size_imsm(st, ldsize >> 9);
2856
2857 return 1;
2858 }
2859
2860 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
2861 {
2862 const unsigned long long base_start = e[*idx].start;
2863 unsigned long long end = base_start + e[*idx].size;
2864 int i;
2865
2866 if (base_start == end)
2867 return 0;
2868
2869 *idx = *idx + 1;
2870 for (i = *idx; i < num_extents; i++) {
2871 /* extend overlapping extents */
2872 if (e[i].start >= base_start &&
2873 e[i].start <= end) {
2874 if (e[i].size == 0)
2875 return 0;
2876 if (e[i].start + e[i].size > end)
2877 end = e[i].start + e[i].size;
2878 } else if (e[i].start > end) {
2879 *idx = i;
2880 break;
2881 }
2882 }
2883
2884 return end - base_start;
2885 }
2886
2887 static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
2888 {
2889 /* build a composite disk with all known extents and generate a new
2890 * 'maxsize' given the "all disks in an array must share a common start
2891 * offset" constraint
2892 */
2893 struct extent *e = calloc(sum_extents, sizeof(*e));
2894 struct dl *dl;
2895 int i, j;
2896 int start_extent;
2897 unsigned long long pos;
2898 unsigned long long start = 0;
2899 unsigned long long maxsize;
2900 unsigned long reserve;
2901
2902 if (!e)
2903 return ~0ULL; /* error */
2904
2905 /* coalesce and sort all extents. also, check to see if we need to
2906 * reserve space between member arrays
2907 */
2908 j = 0;
2909 for (dl = super->disks; dl; dl = dl->next) {
2910 if (!dl->e)
2911 continue;
2912 for (i = 0; i < dl->extent_cnt; i++)
2913 e[j++] = dl->e[i];
2914 }
2915 qsort(e, sum_extents, sizeof(*e), cmp_extent);
2916
2917 /* merge extents */
2918 i = 0;
2919 j = 0;
2920 while (i < sum_extents) {
2921 e[j].start = e[i].start;
2922 e[j].size = find_size(e, &i, sum_extents);
2923 j++;
2924 if (e[j-1].size == 0)
2925 break;
2926 }
2927
2928 pos = 0;
2929 maxsize = 0;
2930 start_extent = 0;
2931 i = 0;
2932 do {
2933 unsigned long long esize;
2934
2935 esize = e[i].start - pos;
2936 if (esize >= maxsize) {
2937 maxsize = esize;
2938 start = pos;
2939 start_extent = i;
2940 }
2941 pos = e[i].start + e[i].size;
2942 i++;
2943 } while (e[i-1].size);
2944 free(e);
2945
2946 if (start_extent > 0)
2947 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
2948 else
2949 reserve = 0;
2950
2951 if (maxsize < reserve)
2952 return ~0ULL;
2953
2954 super->create_offset = ~((__u32) 0);
2955 if (start + reserve > super->create_offset)
2956 return ~0ULL; /* start overflows create_offset */
2957 super->create_offset = start + reserve;
2958
2959 return maxsize - reserve;
2960 }
2961
2962 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
2963 {
2964 if (level < 0 || level == 6 || level == 4)
2965 return 0;
2966
2967 /* if we have an orom prevent invalid raid levels */
2968 if (orom)
2969 switch (level) {
2970 case 0: return imsm_orom_has_raid0(orom);
2971 case 1:
2972 if (raiddisks > 2)
2973 return imsm_orom_has_raid1e(orom);
2974 return imsm_orom_has_raid1(orom) && raiddisks == 2;
2975 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
2976 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
2977 }
2978 else
2979 return 1; /* not on an Intel RAID platform so anything goes */
2980
2981 return 0;
2982 }
2983
2984 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
2985 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
2986 * FIX ME add ahci details
2987 */
2988 static int validate_geometry_imsm_volume(struct supertype *st, int level,
2989 int layout, int raiddisks, int chunk,
2990 unsigned long long size, char *dev,
2991 unsigned long long *freesize,
2992 int verbose)
2993 {
2994 struct stat stb;
2995 struct intel_super *super = st->sb;
2996 struct imsm_super *mpb = super->anchor;
2997 struct dl *dl;
2998 unsigned long long pos = 0;
2999 unsigned long long maxsize;
3000 struct extent *e;
3001 int i;
3002
3003 /* We must have the container info already read in. */
3004 if (!super)
3005 return 0;
3006
3007 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
3008 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3009 level, raiddisks, raiddisks > 1 ? "s" : "");
3010 return 0;
3011 }
3012 if (super->orom && level != 1 &&
3013 !imsm_orom_has_chunk(super->orom, chunk)) {
3014 pr_vrb(": platform does not support a chunk size of: %d\n", chunk);
3015 return 0;
3016 }
3017 if (layout != imsm_level_to_layout(level)) {
3018 if (level == 5)
3019 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
3020 else if (level == 10)
3021 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
3022 else
3023 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
3024 layout, level);
3025 return 0;
3026 }
3027
3028 if (!dev) {
3029 /* General test: make sure there is space for
3030 * 'raiddisks' device extents of size 'size' at a given
3031 * offset
3032 */
3033 unsigned long long minsize = size;
3034 unsigned long long start_offset = ~0ULL;
3035 int dcnt = 0;
3036 if (minsize == 0)
3037 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
3038 for (dl = super->disks; dl ; dl = dl->next) {
3039 int found = 0;
3040
3041 pos = 0;
3042 i = 0;
3043 e = get_extents(super, dl);
3044 if (!e) continue;
3045 do {
3046 unsigned long long esize;
3047 esize = e[i].start - pos;
3048 if (esize >= minsize)
3049 found = 1;
3050 if (found && start_offset == ~0ULL) {
3051 start_offset = pos;
3052 break;
3053 } else if (found && pos != start_offset) {
3054 found = 0;
3055 break;
3056 }
3057 pos = e[i].start + e[i].size;
3058 i++;
3059 } while (e[i-1].size);
3060 if (found)
3061 dcnt++;
3062 free(e);
3063 }
3064 if (dcnt < raiddisks) {
3065 if (verbose)
3066 fprintf(stderr, Name ": imsm: Not enough "
3067 "devices with space for this array "
3068 "(%d < %d)\n",
3069 dcnt, raiddisks);
3070 return 0;
3071 }
3072 return 1;
3073 }
3074
3075 /* This device must be a member of the set */
3076 if (stat(dev, &stb) < 0)
3077 return 0;
3078 if ((S_IFMT & stb.st_mode) != S_IFBLK)
3079 return 0;
3080 for (dl = super->disks ; dl ; dl = dl->next) {
3081 if (dl->major == major(stb.st_rdev) &&
3082 dl->minor == minor(stb.st_rdev))
3083 break;
3084 }
3085 if (!dl) {
3086 if (verbose)
3087 fprintf(stderr, Name ": %s is not in the "
3088 "same imsm set\n", dev);
3089 return 0;
3090 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
3091 /* If a volume is present then the current creation attempt
3092 * cannot incorporate new spares because the orom may not
3093 * understand this configuration (all member disks must be
3094 * members of each array in the container).
3095 */
3096 fprintf(stderr, Name ": %s is a spare and a volume"
3097 " is already defined for this container\n", dev);
3098 fprintf(stderr, Name ": The option-rom requires all member"
3099 " disks to be a member of all volumes\n");
3100