imsm: turn off curr_migr_unit updates
[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 /* FIXME add curr_migr_unit to resync_start conversion */
1222 info->resync_start = 0;
1223 else
1224 info->resync_start = ~0ULL;
1225
1226 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1227 info->name[MAX_RAID_SERIAL_LEN] = 0;
1228
1229 info->array.major_version = -1;
1230 info->array.minor_version = -2;
1231 sprintf(info->text_version, "/%s/%d",
1232 devnum2devname(st->container_dev),
1233 info->container_member);
1234 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
1235 uuid_from_super_imsm(st, info->uuid);
1236 }
1237
1238 /* check the config file to see if we can return a real uuid for this spare */
1239 static void fixup_container_spare_uuid(struct mdinfo *inf)
1240 {
1241 struct mddev_ident_s *array_list;
1242
1243 if (inf->array.level != LEVEL_CONTAINER ||
1244 memcmp(inf->uuid, uuid_match_any, sizeof(int[4])) != 0)
1245 return;
1246
1247 array_list = conf_get_ident(NULL);
1248
1249 for (; array_list; array_list = array_list->next) {
1250 if (array_list->uuid_set) {
1251 struct supertype *_sst; /* spare supertype */
1252 struct supertype *_cst; /* container supertype */
1253
1254 _cst = array_list->st;
1255 _sst = _cst->ss->match_metadata_desc(inf->text_version);
1256 if (_sst) {
1257 memcpy(inf->uuid, array_list->uuid, sizeof(int[4]));
1258 free(_sst);
1259 break;
1260 }
1261 }
1262 }
1263 }
1264
1265 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
1266 {
1267 struct intel_super *super = st->sb;
1268 struct imsm_disk *disk;
1269 __u32 s;
1270
1271 if (super->current_vol >= 0) {
1272 getinfo_super_imsm_volume(st, info);
1273 return;
1274 }
1275
1276 /* Set raid_disks to zero so that Assemble will always pull in valid
1277 * spares
1278 */
1279 info->array.raid_disks = 0;
1280 info->array.level = LEVEL_CONTAINER;
1281 info->array.layout = 0;
1282 info->array.md_minor = -1;
1283 info->array.ctime = 0; /* N/A for imsm */
1284 info->array.utime = 0;
1285 info->array.chunk_size = 0;
1286
1287 info->disk.major = 0;
1288 info->disk.minor = 0;
1289 info->disk.raid_disk = -1;
1290 info->reshape_active = 0;
1291 info->array.major_version = -1;
1292 info->array.minor_version = -2;
1293 strcpy(info->text_version, "imsm");
1294 info->safe_mode_delay = 0;
1295 info->disk.number = -1;
1296 info->disk.state = 0;
1297 info->name[0] = 0;
1298
1299 if (super->disks) {
1300 __u32 reserved = imsm_reserved_sectors(super, super->disks);
1301
1302 disk = &super->disks->disk;
1303 info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved;
1304 info->component_size = reserved;
1305 s = disk->status;
1306 info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
1307 /* we don't change info->disk.raid_disk here because
1308 * this state will be finalized in mdmon after we have
1309 * found the 'most fresh' version of the metadata
1310 */
1311 info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
1312 info->disk.state |= s & SPARE_DISK ? 0 : (1 << MD_DISK_SYNC);
1313 }
1314
1315 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1316 * ->compare_super may have updated the 'num_raid_devs' field for spares
1317 */
1318 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
1319 uuid_from_super_imsm(st, info->uuid);
1320 else {
1321 memcpy(info->uuid, uuid_match_any, sizeof(int[4]));
1322 fixup_container_spare_uuid(info);
1323 }
1324 }
1325
1326 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
1327 char *update, char *devname, int verbose,
1328 int uuid_set, char *homehost)
1329 {
1330 /* FIXME */
1331
1332 /* For 'assemble' and 'force' we need to return non-zero if any
1333 * change was made. For others, the return value is ignored.
1334 * Update options are:
1335 * force-one : This device looks a bit old but needs to be included,
1336 * update age info appropriately.
1337 * assemble: clear any 'faulty' flag to allow this device to
1338 * be assembled.
1339 * force-array: Array is degraded but being forced, mark it clean
1340 * if that will be needed to assemble it.
1341 *
1342 * newdev: not used ????
1343 * grow: Array has gained a new device - this is currently for
1344 * linear only
1345 * resync: mark as dirty so a resync will happen.
1346 * name: update the name - preserving the homehost
1347 *
1348 * Following are not relevant for this imsm:
1349 * sparc2.2 : update from old dodgey metadata
1350 * super-minor: change the preferred_minor number
1351 * summaries: update redundant counters.
1352 * uuid: Change the uuid of the array to match watch is given
1353 * homehost: update the recorded homehost
1354 * _reshape_progress: record new reshape_progress position.
1355 */
1356 int rv = 0;
1357 //struct intel_super *super = st->sb;
1358 //struct imsm_super *mpb = super->mpb;
1359
1360 if (strcmp(update, "grow") == 0) {
1361 }
1362 if (strcmp(update, "resync") == 0) {
1363 /* dev->vol.dirty = 1; */
1364 }
1365
1366 /* IMSM has no concept of UUID or homehost */
1367
1368 return rv;
1369 }
1370
1371 static size_t disks_to_mpb_size(int disks)
1372 {
1373 size_t size;
1374
1375 size = sizeof(struct imsm_super);
1376 size += (disks - 1) * sizeof(struct imsm_disk);
1377 size += 2 * sizeof(struct imsm_dev);
1378 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1379 size += (4 - 2) * sizeof(struct imsm_map);
1380 /* 4 possible disk_ord_tbl's */
1381 size += 4 * (disks - 1) * sizeof(__u32);
1382
1383 return size;
1384 }
1385
1386 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
1387 {
1388 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
1389 return 0;
1390
1391 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
1392 }
1393
1394 static void free_devlist(struct intel_super *super)
1395 {
1396 struct intel_dev *dv;
1397
1398 while (super->devlist) {
1399 dv = super->devlist->next;
1400 free(super->devlist->dev);
1401 free(super->devlist);
1402 super->devlist = dv;
1403 }
1404 }
1405
1406 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
1407 {
1408 memcpy(dest, src, sizeof_imsm_dev(src, 0));
1409 }
1410
1411 static int compare_super_imsm(struct supertype *st, struct supertype *tst)
1412 {
1413 /*
1414 * return:
1415 * 0 same, or first was empty, and second was copied
1416 * 1 second had wrong number
1417 * 2 wrong uuid
1418 * 3 wrong other info
1419 */
1420 struct intel_super *first = st->sb;
1421 struct intel_super *sec = tst->sb;
1422
1423 if (!first) {
1424 st->sb = tst->sb;
1425 tst->sb = NULL;
1426 return 0;
1427 }
1428
1429 if (memcmp(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH) != 0)
1430 return 3;
1431
1432 /* if an anchor does not have num_raid_devs set then it is a free
1433 * floating spare
1434 */
1435 if (first->anchor->num_raid_devs > 0 &&
1436 sec->anchor->num_raid_devs > 0) {
1437 if (first->anchor->family_num != sec->anchor->family_num)
1438 return 3;
1439 }
1440
1441 /* if 'first' is a spare promote it to a populated mpb with sec's
1442 * family number
1443 */
1444 if (first->anchor->num_raid_devs == 0 &&
1445 sec->anchor->num_raid_devs > 0) {
1446 int i;
1447 struct intel_dev *dv;
1448 struct imsm_dev *dev;
1449
1450 /* we need to copy raid device info from sec if an allocation
1451 * fails here we don't associate the spare
1452 */
1453 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
1454 dv = malloc(sizeof(*dv));
1455 if (!dv)
1456 break;
1457 dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
1458 if (!dev) {
1459 free(dv);
1460 break;
1461 }
1462 dv->dev = dev;
1463 dv->index = i;
1464 dv->next = first->devlist;
1465 first->devlist = dv;
1466 }
1467 if (i <= sec->anchor->num_raid_devs) {
1468 /* allocation failure */
1469 free_devlist(first);
1470 fprintf(stderr, "imsm: failed to associate spare\n");
1471 return 3;
1472 }
1473 for (i = 0; i < sec->anchor->num_raid_devs; i++)
1474 imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
1475
1476 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
1477 first->anchor->family_num = sec->anchor->family_num;
1478 }
1479
1480 return 0;
1481 }
1482
1483 static void fd2devname(int fd, char *name)
1484 {
1485 struct stat st;
1486 char path[256];
1487 char dname[100];
1488 char *nm;
1489 int rv;
1490
1491 name[0] = '\0';
1492 if (fstat(fd, &st) != 0)
1493 return;
1494 sprintf(path, "/sys/dev/block/%d:%d",
1495 major(st.st_rdev), minor(st.st_rdev));
1496
1497 rv = readlink(path, dname, sizeof(dname));
1498 if (rv <= 0)
1499 return;
1500
1501 dname[rv] = '\0';
1502 nm = strrchr(dname, '/');
1503 nm++;
1504 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
1505 }
1506
1507
1508 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
1509
1510 static int imsm_read_serial(int fd, char *devname,
1511 __u8 serial[MAX_RAID_SERIAL_LEN])
1512 {
1513 unsigned char scsi_serial[255];
1514 int rv;
1515 int rsp_len;
1516 int len;
1517 char *dest;
1518 char *src;
1519 char *rsp_buf;
1520 int i;
1521
1522 memset(scsi_serial, 0, sizeof(scsi_serial));
1523
1524 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
1525
1526 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
1527 memset(serial, 0, MAX_RAID_SERIAL_LEN);
1528 fd2devname(fd, (char *) serial);
1529 return 0;
1530 }
1531
1532 if (rv != 0) {
1533 if (devname)
1534 fprintf(stderr,
1535 Name ": Failed to retrieve serial for %s\n",
1536 devname);
1537 return rv;
1538 }
1539
1540 rsp_len = scsi_serial[3];
1541 if (!rsp_len) {
1542 if (devname)
1543 fprintf(stderr,
1544 Name ": Failed to retrieve serial for %s\n",
1545 devname);
1546 return 2;
1547 }
1548 rsp_buf = (char *) &scsi_serial[4];
1549
1550 /* trim all whitespace and non-printable characters and convert
1551 * ':' to ';'
1552 */
1553 for (i = 0, dest = rsp_buf; i < rsp_len; i++) {
1554 src = &rsp_buf[i];
1555 if (*src > 0x20) {
1556 /* ':' is reserved for use in placeholder serial
1557 * numbers for missing disks
1558 */
1559 if (*src == ':')
1560 *dest++ = ';';
1561 else
1562 *dest++ = *src;
1563 }
1564 }
1565 len = dest - rsp_buf;
1566 dest = rsp_buf;
1567
1568 /* truncate leading characters */
1569 if (len > MAX_RAID_SERIAL_LEN) {
1570 dest += len - MAX_RAID_SERIAL_LEN;
1571 len = MAX_RAID_SERIAL_LEN;
1572 }
1573
1574 memset(serial, 0, MAX_RAID_SERIAL_LEN);
1575 memcpy(serial, dest, len);
1576
1577 return 0;
1578 }
1579
1580 static int serialcmp(__u8 *s1, __u8 *s2)
1581 {
1582 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
1583 }
1584
1585 static void serialcpy(__u8 *dest, __u8 *src)
1586 {
1587 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
1588 }
1589
1590 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
1591 {
1592 struct dl *dl;
1593
1594 for (dl = super->disks; dl; dl = dl->next)
1595 if (serialcmp(dl->serial, serial) == 0)
1596 break;
1597
1598 return dl;
1599 }
1600
1601 static int
1602 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
1603 {
1604 struct dl *dl;
1605 struct stat stb;
1606 int rv;
1607 int i;
1608 int alloc = 1;
1609 __u8 serial[MAX_RAID_SERIAL_LEN];
1610
1611 rv = imsm_read_serial(fd, devname, serial);
1612
1613 if (rv != 0)
1614 return 2;
1615
1616 /* check if this is a disk we have seen before. it may be a spare in
1617 * super->disks while the current anchor believes it is a raid member,
1618 * check if we need to update dl->index
1619 */
1620 dl = serial_to_dl(serial, super);
1621 if (!dl)
1622 dl = malloc(sizeof(*dl));
1623 else
1624 alloc = 0;
1625
1626 if (!dl) {
1627 if (devname)
1628 fprintf(stderr,
1629 Name ": failed to allocate disk buffer for %s\n",
1630 devname);
1631 return 2;
1632 }
1633
1634 if (alloc) {
1635 fstat(fd, &stb);
1636 dl->major = major(stb.st_rdev);
1637 dl->minor = minor(stb.st_rdev);
1638 dl->next = super->disks;
1639 dl->fd = keep_fd ? fd : -1;
1640 dl->devname = devname ? strdup(devname) : NULL;
1641 serialcpy(dl->serial, serial);
1642 dl->index = -2;
1643 dl->e = NULL;
1644 } else if (keep_fd) {
1645 close(dl->fd);
1646 dl->fd = fd;
1647 }
1648
1649 /* look up this disk's index in the current anchor */
1650 for (i = 0; i < super->anchor->num_disks; i++) {
1651 struct imsm_disk *disk_iter;
1652
1653 disk_iter = __get_imsm_disk(super->anchor, i);
1654
1655 if (serialcmp(disk_iter->serial, dl->serial) == 0) {
1656 dl->disk = *disk_iter;
1657 /* only set index on disks that are a member of a
1658 * populated contianer, i.e. one with raid_devs
1659 */
1660 if (dl->disk.status & FAILED_DISK)
1661 dl->index = -2;
1662 else if (dl->disk.status & SPARE_DISK)
1663 dl->index = -1;
1664 else
1665 dl->index = i;
1666
1667 break;
1668 }
1669 }
1670
1671 /* no match, maybe a stale failed drive */
1672 if (i == super->anchor->num_disks && dl->index >= 0) {
1673 dl->disk = *__get_imsm_disk(super->anchor, dl->index);
1674 if (dl->disk.status & FAILED_DISK)
1675 dl->index = -2;
1676 }
1677
1678 if (alloc)
1679 super->disks = dl;
1680
1681 return 0;
1682 }
1683
1684 #ifndef MDASSEMBLE
1685 /* When migrating map0 contains the 'destination' state while map1
1686 * contains the current state. When not migrating map0 contains the
1687 * current state. This routine assumes that map[0].map_state is set to
1688 * the current array state before being called.
1689 *
1690 * Migration is indicated by one of the following states
1691 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
1692 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
1693 * map1state=unitialized)
1694 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
1695 * map1state=normal)
1696 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
1697 * map1state=degraded)
1698 */
1699 static void migrate(struct imsm_dev *dev, __u8 to_state, int migr_type)
1700 {
1701 struct imsm_map *dest;
1702 struct imsm_map *src = get_imsm_map(dev, 0);
1703
1704 dev->vol.migr_state = 1;
1705 set_migr_type(dev, migr_type);
1706 dev->vol.curr_migr_unit = 0;
1707 dest = get_imsm_map(dev, 1);
1708
1709 /* duplicate and then set the target end state in map[0] */
1710 memcpy(dest, src, sizeof_imsm_map(src));
1711 if (migr_type == MIGR_REBUILD) {
1712 __u32 ord;
1713 int i;
1714
1715 for (i = 0; i < src->num_members; i++) {
1716 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
1717 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
1718 }
1719 }
1720
1721 src->map_state = to_state;
1722 }
1723
1724 static void end_migration(struct imsm_dev *dev, __u8 map_state)
1725 {
1726 struct imsm_map *map = get_imsm_map(dev, 0);
1727 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
1728 int i;
1729
1730 /* merge any IMSM_ORD_REBUILD bits that were not successfully
1731 * completed in the last migration.
1732 *
1733 * FIXME add support for online capacity expansion and
1734 * raid-level-migration
1735 */
1736 for (i = 0; i < prev->num_members; i++)
1737 map->disk_ord_tbl[i] |= prev->disk_ord_tbl[i];
1738
1739 dev->vol.migr_state = 0;
1740 dev->vol.curr_migr_unit = 0;
1741 map->map_state = map_state;
1742 }
1743 #endif
1744
1745 static int parse_raid_devices(struct intel_super *super)
1746 {
1747 int i;
1748 struct imsm_dev *dev_new;
1749 size_t len, len_migr;
1750 size_t space_needed = 0;
1751 struct imsm_super *mpb = super->anchor;
1752
1753 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1754 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
1755 struct intel_dev *dv;
1756
1757 len = sizeof_imsm_dev(dev_iter, 0);
1758 len_migr = sizeof_imsm_dev(dev_iter, 1);
1759 if (len_migr > len)
1760 space_needed += len_migr - len;
1761
1762 dv = malloc(sizeof(*dv));
1763 if (!dv)
1764 return 1;
1765 dev_new = malloc(len_migr);
1766 if (!dev_new) {
1767 free(dv);
1768 return 1;
1769 }
1770 imsm_copy_dev(dev_new, dev_iter);
1771 dv->dev = dev_new;
1772 dv->index = i;
1773 dv->next = super->devlist;
1774 super->devlist = dv;
1775 }
1776
1777 /* ensure that super->buf is large enough when all raid devices
1778 * are migrating
1779 */
1780 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
1781 void *buf;
1782
1783 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512);
1784 if (posix_memalign(&buf, 512, len) != 0)
1785 return 1;
1786
1787 memcpy(buf, super->buf, super->len);
1788 memset(buf + super->len, 0, len - super->len);
1789 free(super->buf);
1790 super->buf = buf;
1791 super->len = len;
1792 }
1793
1794 return 0;
1795 }
1796
1797 /* retrieve a pointer to the bbm log which starts after all raid devices */
1798 struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
1799 {
1800 void *ptr = NULL;
1801
1802 if (__le32_to_cpu(mpb->bbm_log_size)) {
1803 ptr = mpb;
1804 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
1805 }
1806
1807 return ptr;
1808 }
1809
1810 static void __free_imsm(struct intel_super *super, int free_disks);
1811
1812 /* load_imsm_mpb - read matrix metadata
1813 * allocates super->mpb to be freed by free_super
1814 */
1815 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
1816 {
1817 unsigned long long dsize;
1818 unsigned long long sectors;
1819 struct stat;
1820 struct imsm_super *anchor;
1821 __u32 check_sum;
1822 int rc;
1823
1824 get_dev_size(fd, NULL, &dsize);
1825
1826 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
1827 if (devname)
1828 fprintf(stderr,
1829 Name ": Cannot seek to anchor block on %s: %s\n",
1830 devname, strerror(errno));
1831 return 1;
1832 }
1833
1834 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
1835 if (devname)
1836 fprintf(stderr,
1837 Name ": Failed to allocate imsm anchor buffer"
1838 " on %s\n", devname);
1839 return 1;
1840 }
1841 if (read(fd, anchor, 512) != 512) {
1842 if (devname)
1843 fprintf(stderr,
1844 Name ": Cannot read anchor block on %s: %s\n",
1845 devname, strerror(errno));
1846 free(anchor);
1847 return 1;
1848 }
1849
1850 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
1851 if (devname)
1852 fprintf(stderr,
1853 Name ": no IMSM anchor on %s\n", devname);
1854 free(anchor);
1855 return 2;
1856 }
1857
1858 __free_imsm(super, 0);
1859 super->len = ROUND_UP(anchor->mpb_size, 512);
1860 if (posix_memalign(&super->buf, 512, super->len) != 0) {
1861 if (devname)
1862 fprintf(stderr,
1863 Name ": unable to allocate %zu byte mpb buffer\n",
1864 super->len);
1865 free(anchor);
1866 return 2;
1867 }
1868 memcpy(super->buf, anchor, 512);
1869
1870 sectors = mpb_sectors(anchor) - 1;
1871 free(anchor);
1872 if (!sectors) {
1873 check_sum = __gen_imsm_checksum(super->anchor);
1874 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
1875 if (devname)
1876 fprintf(stderr,
1877 Name ": IMSM checksum %x != %x on %s\n",
1878 check_sum,
1879 __le32_to_cpu(super->anchor->check_sum),
1880 devname);
1881 return 2;
1882 }
1883
1884 rc = load_imsm_disk(fd, super, devname, 0);
1885 if (rc == 0)
1886 rc = parse_raid_devices(super);
1887 return rc;
1888 }
1889
1890 /* read the extended mpb */
1891 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
1892 if (devname)
1893 fprintf(stderr,
1894 Name ": Cannot seek to extended mpb on %s: %s\n",
1895 devname, strerror(errno));
1896 return 1;
1897 }
1898
1899 if (read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
1900 if (devname)
1901 fprintf(stderr,
1902 Name ": Cannot read extended mpb on %s: %s\n",
1903 devname, strerror(errno));
1904 return 2;
1905 }
1906
1907 check_sum = __gen_imsm_checksum(super->anchor);
1908 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
1909 if (devname)
1910 fprintf(stderr,
1911 Name ": IMSM checksum %x != %x on %s\n",
1912 check_sum, __le32_to_cpu(super->anchor->check_sum),
1913 devname);
1914 return 3;
1915 }
1916
1917 /* FIXME the BBM log is disk specific so we cannot use this global
1918 * buffer for all disks. Ok for now since we only look at the global
1919 * bbm_log_size parameter to gate assembly
1920 */
1921 super->bbm_log = __get_imsm_bbm_log(super->anchor);
1922
1923 rc = load_imsm_disk(fd, super, devname, 0);
1924 if (rc == 0)
1925 rc = parse_raid_devices(super);
1926
1927 return rc;
1928 }
1929
1930 static void __free_imsm_disk(struct dl *d)
1931 {
1932 if (d->fd >= 0)
1933 close(d->fd);
1934 if (d->devname)
1935 free(d->devname);
1936 if (d->e)
1937 free(d->e);
1938 free(d);
1939
1940 }
1941 static void free_imsm_disks(struct intel_super *super)
1942 {
1943 struct dl *d;
1944
1945 while (super->disks) {
1946 d = super->disks;
1947 super->disks = d->next;
1948 __free_imsm_disk(d);
1949 }
1950 while (super->missing) {
1951 d = super->missing;
1952 super->missing = d->next;
1953 __free_imsm_disk(d);
1954 }
1955
1956 }
1957
1958 /* free all the pieces hanging off of a super pointer */
1959 static void __free_imsm(struct intel_super *super, int free_disks)
1960 {
1961 if (super->buf) {
1962 free(super->buf);
1963 super->buf = NULL;
1964 }
1965 if (free_disks)
1966 free_imsm_disks(super);
1967 free_devlist(super);
1968 if (super->hba) {
1969 free((void *) super->hba);
1970 super->hba = NULL;
1971 }
1972 }
1973
1974 static void free_imsm(struct intel_super *super)
1975 {
1976 __free_imsm(super, 1);
1977 free(super);
1978 }
1979
1980 static void free_super_imsm(struct supertype *st)
1981 {
1982 struct intel_super *super = st->sb;
1983
1984 if (!super)
1985 return;
1986
1987 free_imsm(super);
1988 st->sb = NULL;
1989 }
1990
1991 static struct intel_super *alloc_super(int creating_imsm)
1992 {
1993 struct intel_super *super = malloc(sizeof(*super));
1994
1995 if (super) {
1996 memset(super, 0, sizeof(*super));
1997 super->creating_imsm = creating_imsm;
1998 super->current_vol = -1;
1999 super->create_offset = ~((__u32 ) 0);
2000 if (!check_env("IMSM_NO_PLATFORM"))
2001 super->orom = find_imsm_orom();
2002 if (super->orom && !check_env("IMSM_TEST_OROM")) {
2003 struct sys_dev *list, *ent;
2004
2005 /* find the first intel ahci controller */
2006 list = find_driver_devices("pci", "ahci");
2007 for (ent = list; ent; ent = ent->next)
2008 if (devpath_to_vendor(ent->path) == 0x8086)
2009 break;
2010 if (ent) {
2011 super->hba = ent->path;
2012 ent->path = NULL;
2013 }
2014 free_sys_dev(&list);
2015 }
2016 }
2017
2018 return super;
2019 }
2020
2021 #ifndef MDASSEMBLE
2022 /* find_missing - helper routine for load_super_imsm_all that identifies
2023 * disks that have disappeared from the system. This routine relies on
2024 * the mpb being uptodate, which it is at load time.
2025 */
2026 static int find_missing(struct intel_super *super)
2027 {
2028 int i;
2029 struct imsm_super *mpb = super->anchor;
2030 struct dl *dl;
2031 struct imsm_disk *disk;
2032
2033 for (i = 0; i < mpb->num_disks; i++) {
2034 disk = __get_imsm_disk(mpb, i);
2035 dl = serial_to_dl(disk->serial, super);
2036 if (dl)
2037 continue;
2038
2039 dl = malloc(sizeof(*dl));
2040 if (!dl)
2041 return 1;
2042 dl->major = 0;
2043 dl->minor = 0;
2044 dl->fd = -1;
2045 dl->devname = strdup("missing");
2046 dl->index = i;
2047 serialcpy(dl->serial, disk->serial);
2048 dl->disk = *disk;
2049 dl->e = NULL;
2050 dl->next = super->missing;
2051 super->missing = dl;
2052 }
2053
2054 return 0;
2055 }
2056
2057 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
2058 char *devname, int keep_fd)
2059 {
2060 struct mdinfo *sra;
2061 struct intel_super *super;
2062 struct mdinfo *sd, *best = NULL;
2063 __u32 bestgen = 0;
2064 __u32 gen;
2065 char nm[20];
2066 int dfd;
2067 int rv;
2068 int devnum = fd2devnum(fd);
2069 int retry;
2070 enum sysfs_read_flags flags;
2071
2072 flags = GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE;
2073 if (mdmon_running(devnum))
2074 flags |= SKIP_GONE_DEVS;
2075
2076 /* check if 'fd' an opened container */
2077 sra = sysfs_read(fd, 0, flags);
2078 if (!sra)
2079 return 1;
2080
2081 if (sra->array.major_version != -1 ||
2082 sra->array.minor_version != -2 ||
2083 strcmp(sra->text_version, "imsm") != 0)
2084 return 1;
2085
2086 super = alloc_super(0);
2087 if (!super)
2088 return 1;
2089
2090 /* find the most up to date disk in this array, skipping spares */
2091 for (sd = sra->devs; sd; sd = sd->next) {
2092 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2093 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
2094 if (dfd < 0) {
2095 free_imsm(super);
2096 return 2;
2097 }
2098 rv = load_imsm_mpb(dfd, super, NULL);
2099
2100 /* retry the load if we might have raced against mdmon */
2101 if (rv == 3 && mdmon_running(devnum))
2102 for (retry = 0; retry < 3; retry++) {
2103 usleep(3000);
2104 rv = load_imsm_mpb(dfd, super, NULL);
2105 if (rv != 3)
2106 break;
2107 }
2108 if (!keep_fd)
2109 close(dfd);
2110 if (rv == 0) {
2111 if (super->anchor->num_raid_devs == 0)
2112 gen = 0;
2113 else
2114 gen = __le32_to_cpu(super->anchor->generation_num);
2115 if (!best || gen > bestgen) {
2116 bestgen = gen;
2117 best = sd;
2118 }
2119 } else {
2120 free_imsm(super);
2121 return rv;
2122 }
2123 }
2124
2125 if (!best) {
2126 free_imsm(super);
2127 return 1;
2128 }
2129
2130 /* load the most up to date anchor */
2131 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
2132 dfd = dev_open(nm, O_RDONLY);
2133 if (dfd < 0) {
2134 free_imsm(super);
2135 return 1;
2136 }
2137 rv = load_imsm_mpb(dfd, super, NULL);
2138 close(dfd);
2139 if (rv != 0) {
2140 free_imsm(super);
2141 return 2;
2142 }
2143
2144 /* re-parse the disk list with the current anchor */
2145 for (sd = sra->devs ; sd ; sd = sd->next) {
2146 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2147 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
2148 if (dfd < 0) {
2149 free_imsm(super);
2150 return 2;
2151 }
2152 load_imsm_disk(dfd, super, NULL, keep_fd);
2153 if (!keep_fd)
2154 close(dfd);
2155 }
2156
2157
2158 if (find_missing(super) != 0) {
2159 free_imsm(super);
2160 return 2;
2161 }
2162
2163 if (st->subarray[0]) {
2164 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
2165 super->current_vol = atoi(st->subarray);
2166 else
2167 return 1;
2168 }
2169
2170 *sbp = super;
2171 st->container_dev = devnum;
2172 if (st->ss == NULL) {
2173 st->ss = &super_imsm;
2174 st->minor_version = 0;
2175 st->max_devs = IMSM_MAX_DEVICES;
2176 }
2177 st->loaded_container = 1;
2178
2179 return 0;
2180 }
2181 #endif
2182
2183 static int load_super_imsm(struct supertype *st, int fd, char *devname)
2184 {
2185 struct intel_super *super;
2186 int rv;
2187
2188 #ifndef MDASSEMBLE
2189 if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0)
2190 return 0;
2191 #endif
2192 if (st->subarray[0])
2193 return 1; /* FIXME */
2194
2195 super = alloc_super(0);
2196 if (!super) {
2197 fprintf(stderr,
2198 Name ": malloc of %zu failed.\n",
2199 sizeof(*super));
2200 return 1;
2201 }
2202
2203 rv = load_imsm_mpb(fd, super, devname);
2204
2205 if (rv) {
2206 if (devname)
2207 fprintf(stderr,
2208 Name ": Failed to load all information "
2209 "sections on %s\n", devname);
2210 free_imsm(super);
2211 return rv;
2212 }
2213
2214 st->sb = super;
2215 if (st->ss == NULL) {
2216 st->ss = &super_imsm;
2217 st->minor_version = 0;
2218 st->max_devs = IMSM_MAX_DEVICES;
2219 }
2220 st->loaded_container = 0;
2221
2222 return 0;
2223 }
2224
2225 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
2226 {
2227 if (info->level == 1)
2228 return 128;
2229 return info->chunk_size >> 9;
2230 }
2231
2232 static __u32 info_to_num_data_stripes(mdu_array_info_t *info, int num_domains)
2233 {
2234 __u32 num_stripes;
2235
2236 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
2237 num_stripes /= num_domains;
2238
2239 return num_stripes;
2240 }
2241
2242 static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
2243 {
2244 if (info->level == 1)
2245 return info->size * 2;
2246 else
2247 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
2248 }
2249
2250 static void imsm_update_version_info(struct intel_super *super)
2251 {
2252 /* update the version and attributes */
2253 struct imsm_super *mpb = super->anchor;
2254 char *version;
2255 struct imsm_dev *dev;
2256 struct imsm_map *map;
2257 int i;
2258
2259 for (i = 0; i < mpb->num_raid_devs; i++) {
2260 dev = get_imsm_dev(super, i);
2261 map = get_imsm_map(dev, 0);
2262 if (__le32_to_cpu(dev->size_high) > 0)
2263 mpb->attributes |= MPB_ATTRIB_2TB;
2264
2265 /* FIXME detect when an array spans a port multiplier */
2266 #if 0
2267 mpb->attributes |= MPB_ATTRIB_PM;
2268 #endif
2269
2270 if (mpb->num_raid_devs > 1 ||
2271 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
2272 version = MPB_VERSION_ATTRIBS;
2273 switch (get_imsm_raid_level(map)) {
2274 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
2275 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
2276 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
2277 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
2278 }
2279 } else {
2280 if (map->num_members >= 5)
2281 version = MPB_VERSION_5OR6_DISK_ARRAY;
2282 else if (dev->status == DEV_CLONE_N_GO)
2283 version = MPB_VERSION_CNG;
2284 else if (get_imsm_raid_level(map) == 5)
2285 version = MPB_VERSION_RAID5;
2286 else if (map->num_members >= 3)
2287 version = MPB_VERSION_3OR4_DISK_ARRAY;
2288 else if (get_imsm_raid_level(map) == 1)
2289 version = MPB_VERSION_RAID1;
2290 else
2291 version = MPB_VERSION_RAID0;
2292 }
2293 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
2294 }
2295 }
2296
2297 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
2298 unsigned long long size, char *name,
2299 char *homehost, int *uuid)
2300 {
2301 /* We are creating a volume inside a pre-existing container.
2302 * so st->sb is already set.
2303 */
2304 struct intel_super *super = st->sb;
2305 struct imsm_super *mpb = super->anchor;
2306 struct intel_dev *dv;
2307 struct imsm_dev *dev;
2308 struct imsm_vol *vol;
2309 struct imsm_map *map;
2310 int idx = mpb->num_raid_devs;
2311 int i;
2312 unsigned long long array_blocks;
2313 size_t size_old, size_new;
2314 __u32 num_data_stripes;
2315
2316 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
2317 fprintf(stderr, Name": This imsm-container already has the "
2318 "maximum of %d volumes\n", super->orom->vpa);
2319 return 0;
2320 }
2321
2322 /* ensure the mpb is large enough for the new data */
2323 size_old = __le32_to_cpu(mpb->mpb_size);
2324 size_new = disks_to_mpb_size(info->nr_disks);
2325 if (size_new > size_old) {
2326 void *mpb_new;
2327 size_t size_round = ROUND_UP(size_new, 512);
2328
2329 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
2330 fprintf(stderr, Name": could not allocate new mpb\n");
2331 return 0;
2332 }
2333 memcpy(mpb_new, mpb, size_old);
2334 free(mpb);
2335 mpb = mpb_new;
2336 super->anchor = mpb_new;
2337 mpb->mpb_size = __cpu_to_le32(size_new);
2338 memset(mpb_new + size_old, 0, size_round - size_old);
2339 }
2340 super->current_vol = idx;
2341 /* when creating the first raid device in this container set num_disks
2342 * to zero, i.e. delete this spare and add raid member devices in
2343 * add_to_super_imsm_volume()
2344 */
2345 if (super->current_vol == 0)
2346 mpb->num_disks = 0;
2347
2348 for (i = 0; i < super->current_vol; i++) {
2349 dev = get_imsm_dev(super, i);
2350 if (strncmp((char *) dev->volume, name,
2351 MAX_RAID_SERIAL_LEN) == 0) {
2352 fprintf(stderr, Name": '%s' is already defined for this container\n",
2353 name);
2354 return 0;
2355 }
2356 }
2357
2358 sprintf(st->subarray, "%d", idx);
2359 dv = malloc(sizeof(*dv));
2360 if (!dv) {
2361 fprintf(stderr, Name ": failed to allocate device list entry\n");
2362 return 0;
2363 }
2364 dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
2365 if (!dev) {
2366 free(dv);
2367 fprintf(stderr, Name": could not allocate raid device\n");
2368 return 0;
2369 }
2370 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
2371 if (info->level == 1)
2372 array_blocks = info_to_blocks_per_member(info);
2373 else
2374 array_blocks = calc_array_size(info->level, info->raid_disks,
2375 info->layout, info->chunk_size,
2376 info->size*2);
2377 dev->size_low = __cpu_to_le32((__u32) array_blocks);
2378 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
2379 dev->status = __cpu_to_le32(0);
2380 dev->reserved_blocks = __cpu_to_le32(0);
2381 vol = &dev->vol;
2382 vol->migr_state = 0;
2383 set_migr_type(dev, MIGR_INIT);
2384 vol->dirty = 0;
2385 vol->curr_migr_unit = 0;
2386 map = get_imsm_map(dev, 0);
2387 map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
2388 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
2389 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
2390 map->failed_disk_num = ~0;
2391 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
2392 IMSM_T_STATE_NORMAL;
2393
2394 if (info->level == 1 && info->raid_disks > 2) {
2395 fprintf(stderr, Name": imsm does not support more than 2 disks"
2396 "in a raid1 volume\n");
2397 return 0;
2398 }
2399 if (info->level == 10) {
2400 map->raid_level = 1;
2401 map->num_domains = info->raid_disks / 2;
2402 } else {
2403 map->raid_level = info->level;
2404 map->num_domains = 1;
2405 }
2406 num_data_stripes = info_to_num_data_stripes(info, map->num_domains);
2407 map->num_data_stripes = __cpu_to_le32(num_data_stripes);
2408
2409 map->num_members = info->raid_disks;
2410 for (i = 0; i < map->num_members; i++) {
2411 /* initialized in add_to_super */
2412 set_imsm_ord_tbl_ent(map, i, 0);
2413 }
2414 mpb->num_raid_devs++;
2415
2416 dv->dev = dev;
2417 dv->index = super->current_vol;
2418 dv->next = super->devlist;
2419 super->devlist = dv;
2420
2421 imsm_update_version_info(super);
2422
2423 return 1;
2424 }
2425
2426 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
2427 unsigned long long size, char *name,
2428 char *homehost, int *uuid)
2429 {
2430 /* This is primarily called by Create when creating a new array.
2431 * We will then get add_to_super called for each component, and then
2432 * write_init_super called to write it out to each device.
2433 * For IMSM, Create can create on fresh devices or on a pre-existing
2434 * array.
2435 * To create on a pre-existing array a different method will be called.
2436 * This one is just for fresh drives.
2437 */
2438 struct intel_super *super;
2439 struct imsm_super *mpb;
2440 size_t mpb_size;
2441 char *version;
2442
2443 if (!info) {
2444 st->sb = NULL;
2445 return 0;
2446 }
2447 if (st->sb)
2448 return init_super_imsm_volume(st, info, size, name, homehost,
2449 uuid);
2450
2451 super = alloc_super(1);
2452 if (!super)
2453 return 0;
2454 mpb_size = disks_to_mpb_size(info->nr_disks);
2455 if (posix_memalign(&super->buf, 512, mpb_size) != 0) {
2456 free(super);
2457 return 0;
2458 }
2459 mpb = super->buf;
2460 memset(mpb, 0, mpb_size);
2461
2462 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
2463
2464 version = (char *) mpb->sig;
2465 strcpy(version, MPB_SIGNATURE);
2466 version += strlen(MPB_SIGNATURE);
2467 strcpy(version, MPB_VERSION_RAID0);
2468 mpb->mpb_size = mpb_size;
2469
2470 st->sb = super;
2471 return 1;
2472 }
2473
2474 #ifndef MDASSEMBLE
2475 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
2476 int fd, char *devname)
2477 {
2478 struct intel_super *super = st->sb;
2479 struct imsm_super *mpb = super->anchor;
2480 struct dl *dl;
2481 struct imsm_dev *dev;
2482 struct imsm_map *map;
2483
2484 dev = get_imsm_dev(super, super->current_vol);
2485 map = get_imsm_map(dev, 0);
2486
2487 if (! (dk->state & (1<<MD_DISK_SYNC))) {
2488 fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
2489 devname);
2490 return 1;
2491 }
2492
2493 if (fd == -1) {
2494 /* we're doing autolayout so grab the pre-marked (in
2495 * validate_geometry) raid_disk
2496 */
2497 for (dl = super->disks; dl; dl = dl->next)
2498 if (dl->raiddisk == dk->raid_disk)
2499 break;
2500 } else {
2501 for (dl = super->disks; dl ; dl = dl->next)
2502 if (dl->major == dk->major &&
2503 dl->minor == dk->minor)
2504 break;
2505 }
2506
2507 if (!dl) {
2508 fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
2509 return 1;
2510 }
2511
2512 /* add a pristine spare to the metadata */
2513 if (dl->index < 0) {
2514 dl->index = super->anchor->num_disks;
2515 super->anchor->num_disks++;
2516 }
2517 set_imsm_ord_tbl_ent(map, dk->number, dl->index);
2518 dl->disk.status = CONFIGURED_DISK | USABLE_DISK;
2519
2520 /* if we are creating the first raid device update the family number */
2521 if (super->current_vol == 0) {
2522 __u32 sum;
2523 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
2524 struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
2525
2526 *_dev = *dev;
2527 *_disk = dl->disk;
2528 sum = __gen_imsm_checksum(mpb);
2529 mpb->family_num = __cpu_to_le32(sum);
2530 }
2531
2532 return 0;
2533 }
2534
2535 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
2536 int fd, char *devname)
2537 {
2538 struct intel_super *super = st->sb;
2539 struct dl *dd;
2540 unsigned long long size;
2541 __u32 id;
2542 int rv;
2543 struct stat stb;
2544
2545 /* if we are on an RAID enabled platform check that the disk is
2546 * attached to the raid controller
2547 */
2548 if (super->hba && !disk_attached_to_hba(fd, super->hba)) {
2549 fprintf(stderr,
2550 Name ": %s is not attached to the raid controller: %s\n",
2551 devname ? : "disk", super->hba);
2552 return 1;
2553 }
2554
2555 if (super->current_vol >= 0)
2556 return add_to_super_imsm_volume(st, dk, fd, devname);
2557
2558 fstat(fd, &stb);
2559 dd = malloc(sizeof(*dd));
2560 if (!dd) {
2561 fprintf(stderr,
2562 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
2563 return 1;
2564 }
2565 memset(dd, 0, sizeof(*dd));
2566 dd->major = major(stb.st_rdev);
2567 dd->minor = minor(stb.st_rdev);
2568 dd->index = -1;
2569 dd->devname = devname ? strdup(devname) : NULL;
2570 dd->fd = fd;
2571 dd->e = NULL;
2572 rv = imsm_read_serial(fd, devname, dd->serial);
2573 if (rv) {
2574 fprintf(stderr,
2575 Name ": failed to retrieve scsi serial, aborting\n");
2576 free(dd);
2577 abort();
2578 }
2579
2580 get_dev_size(fd, NULL, &size);
2581 size /= 512;
2582 serialcpy(dd->disk.serial, dd->serial);
2583 dd->disk.total_blocks = __cpu_to_le32(size);
2584 dd->disk.status = USABLE_DISK | SPARE_DISK;
2585 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
2586 dd->disk.scsi_id = __cpu_to_le32(id);
2587 else
2588 dd->disk.scsi_id = __cpu_to_le32(0);
2589
2590 if (st->update_tail) {
2591 dd->next = super->add;
2592 super->add = dd;
2593 } else {
2594 dd->next = super->disks;
2595 super->disks = dd;
2596 }
2597
2598 return 0;
2599 }
2600
2601 static int store_imsm_mpb(int fd, struct intel_super *super);
2602
2603 /* spare records have their own family number and do not have any defined raid
2604 * devices
2605 */
2606 static int write_super_imsm_spares(struct intel_super *super, int doclose)
2607 {
2608 struct imsm_super mpb_save;
2609 struct imsm_super *mpb = super->anchor;
2610 __u32 sum;
2611 struct dl *d;
2612
2613 mpb_save = *mpb;
2614 mpb->num_raid_devs = 0;
2615 mpb->num_disks = 1;
2616 mpb->mpb_size = sizeof(struct imsm_super);
2617 mpb->generation_num = __cpu_to_le32(1UL);
2618
2619 for (d = super->disks; d; d = d->next) {
2620 if (d->index != -1)
2621 continue;
2622
2623 mpb->disk[0] = d->disk;
2624 sum = __gen_imsm_checksum(mpb);
2625 mpb->family_num = __cpu_to_le32(sum);
2626 sum = __gen_imsm_checksum(mpb);
2627 mpb->check_sum = __cpu_to_le32(sum);
2628
2629 if (store_imsm_mpb(d->fd, super)) {
2630 fprintf(stderr, "%s: failed for device %d:%d %s\n",
2631 __func__, d->major, d->minor, strerror(errno));
2632 *mpb = mpb_save;
2633 return 1;
2634 }
2635 if (doclose) {
2636 close(d->fd);
2637 d->fd = -1;
2638 }
2639 }
2640
2641 *mpb = mpb_save;
2642 return 0;
2643 }
2644
2645 static int write_super_imsm(struct intel_super *super, int doclose)
2646 {
2647 struct imsm_super *mpb = super->anchor;
2648 struct dl *d;
2649 __u32 generation;
2650 __u32 sum;
2651 int spares = 0;
2652 int i;
2653 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
2654
2655 /* 'generation' is incremented everytime the metadata is written */
2656 generation = __le32_to_cpu(mpb->generation_num);
2657 generation++;
2658 mpb->generation_num = __cpu_to_le32(generation);
2659
2660 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
2661 for (d = super->disks; d; d = d->next) {
2662 if (d->index == -1)
2663 spares++;
2664 else
2665 mpb->disk[d->index] = d->disk;
2666 }
2667 for (d = super->missing; d; d = d->next)
2668 mpb->disk[d->index] = d->disk;
2669
2670 for (i = 0; i < mpb->num_raid_devs; i++) {
2671 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2672
2673 imsm_copy_dev(dev, get_imsm_dev(super, i));
2674 mpb_size += sizeof_imsm_dev(dev, 0);
2675 }
2676 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
2677 mpb->mpb_size = __cpu_to_le32(mpb_size);
2678
2679 /* recalculate checksum */
2680 sum = __gen_imsm_checksum(mpb);
2681 mpb->check_sum = __cpu_to_le32(sum);
2682
2683 /* write the mpb for disks that compose raid devices */
2684 for (d = super->disks; d ; d = d->next) {
2685 if (d->index < 0)
2686 continue;
2687 if (store_imsm_mpb(d->fd, super))
2688 fprintf(stderr, "%s: failed for device %d:%d %s\n",
2689 __func__, d->major, d->minor, strerror(errno));
2690 if (doclose) {
2691 close(d->fd);
2692 d->fd = -1;
2693 }
2694 }
2695
2696 if (spares)
2697 return write_super_imsm_spares(super, doclose);
2698
2699 return 0;
2700 }
2701
2702
2703 static int create_array(struct supertype *st)
2704 {
2705 size_t len;
2706 struct imsm_update_create_array *u;
2707 struct intel_super *super = st->sb;
2708 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
2709 struct imsm_map *map = get_imsm_map(dev, 0);
2710 struct disk_info *inf;
2711 struct imsm_disk *disk;
2712 int i;
2713 int idx;
2714
2715 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
2716 sizeof(*inf) * map->num_members;
2717 u = malloc(len);
2718 if (!u) {
2719 fprintf(stderr, "%s: failed to allocate update buffer\n",
2720 __func__);
2721 return 1;
2722 }
2723
2724 u->type = update_create_array;
2725 u->dev_idx = super->current_vol;
2726 imsm_copy_dev(&u->dev, dev);
2727 inf = get_disk_info(u);
2728 for (i = 0; i < map->num_members; i++) {
2729 idx = get_imsm_disk_idx(dev, i);
2730 disk = get_imsm_disk(super, idx);
2731 serialcpy(inf[i].serial, disk->serial);
2732 }
2733 append_metadata_update(st, u, len);
2734
2735 return 0;
2736 }
2737
2738 static int _add_disk(struct supertype *st)
2739 {
2740 struct intel_super *super = st->sb;
2741 size_t len;
2742 struct imsm_update_add_disk *u;
2743
2744 if (!super->add)
2745 return 0;
2746
2747 len = sizeof(*u);
2748 u = malloc(len);
2749 if (!u) {
2750 fprintf(stderr, "%s: failed to allocate update buffer\n",
2751 __func__);
2752 return 1;
2753 }
2754
2755 u->type = update_add_disk;
2756 append_metadata_update(st, u, len);
2757
2758 return 0;
2759 }
2760
2761 static int write_init_super_imsm(struct supertype *st)
2762 {
2763 if (st->update_tail) {
2764 /* queue the recently created array / added disk
2765 * as a metadata update */
2766 struct intel_super *super = st->sb;
2767 struct dl *d;
2768 int rv;
2769
2770 /* determine if we are creating a volume or adding a disk */
2771 if (super->current_vol < 0) {
2772 /* in the add disk case we are running in mdmon
2773 * context, so don't close fd's
2774 */
2775 return _add_disk(st);
2776 } else
2777 rv = create_array(st);
2778
2779 for (d = super->disks; d ; d = d->next) {
2780 close(d->fd);
2781 d->fd = -1;
2782 }
2783
2784 return rv;
2785 } else
2786 return write_super_imsm(st->sb, 1);
2787 }
2788 #endif
2789
2790 static int store_zero_imsm(struct supertype *st, int fd)
2791 {
2792 unsigned long long dsize;
2793 void *buf;
2794
2795 get_dev_size(fd, NULL, &dsize);
2796
2797 /* first block is stored on second to last sector of the disk */
2798 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
2799 return 1;
2800
2801 if (posix_memalign(&buf, 512, 512) != 0)
2802 return 1;
2803
2804 memset(buf, 0, 512);
2805 if (write(fd, buf, 512) != 512)
2806 return 1;
2807 return 0;
2808 }
2809
2810 static int imsm_bbm_log_size(struct imsm_super *mpb)
2811 {
2812 return __le32_to_cpu(mpb->bbm_log_size);
2813 }
2814
2815 #ifndef MDASSEMBLE
2816 static int validate_geometry_imsm_container(struct supertype *st, int level,
2817 int layout, int raiddisks, int chunk,
2818 unsigned long long size, char *dev,
2819 unsigned long long *freesize,
2820 int verbose)
2821 {
2822 int fd;
2823 unsigned long long ldsize;
2824 const struct imsm_orom *orom;
2825
2826 if (level != LEVEL_CONTAINER)
2827 return 0;
2828 if (!dev)
2829 return 1;
2830
2831 if (check_env("IMSM_NO_PLATFORM"))
2832 orom = NULL;
2833 else
2834 orom = find_imsm_orom();
2835 if (orom && raiddisks > orom->tds) {
2836 if (verbose)
2837 fprintf(stderr, Name ": %d exceeds maximum number of"
2838 " platform supported disks: %d\n",
2839 raiddisks, orom->tds);
2840 return 0;
2841 }
2842
2843 fd = open(dev, O_RDONLY|O_EXCL, 0);
2844 if (fd < 0) {
2845 if (verbose)
2846 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
2847 dev, strerror(errno));
2848 return 0;
2849 }
2850 if (!get_dev_size(fd, dev, &ldsize)) {
2851 close(fd);
2852 return 0;
2853 }
2854 close(fd);
2855
2856 *freesize = avail_size_imsm(st, ldsize >> 9);
2857
2858 return 1;
2859 }
2860
2861 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
2862 {
2863 const unsigned long long base_start = e[*idx].start;
2864 unsigned long long end = base_start + e[*idx].size;
2865 int i;
2866
2867 if (base_start == end)
2868 return 0;
2869
2870 *idx = *idx + 1;
2871 for (i = *idx; i < num_extents; i++) {
2872 /* extend overlapping extents */
2873 if (e[i].start >= base_start &&
2874 e[i].start <= end) {
2875 if (e[i].size == 0)
2876 return 0;
2877 if (e[i].start + e[i].size > end)
2878 end = e[i].start + e[i].size;
2879 } else if (e[i].start > end) {
2880 *idx = i;
2881 break;
2882 }
2883 }
2884
2885 return end - base_start;
2886 }
2887
2888 static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
2889 {
2890 /* build a composite disk with all known extents and generate a new
2891 * 'maxsize' given the "all disks in an array must share a common start
2892 * offset" constraint
2893 */
2894 struct extent *e = calloc(sum_extents, sizeof(*e));
2895 struct dl *dl;
2896 int i, j;
2897 int start_extent;
2898 unsigned long long pos;
2899 unsigned long long start = 0;
2900 unsigned long long maxsize;
2901 unsigned long reserve;
2902
2903 if (!e)
2904 return ~0ULL; /* error */
2905
2906 /* coalesce and sort all extents. also, check to see if we need to
2907 * reserve space between member arrays
2908 */
2909 j = 0;
2910 for (dl = super->disks; dl; dl = dl->next) {
2911 if (!dl->e)
2912 continue;
2913 for (i = 0; i < dl->extent_cnt; i++)
2914 e[j++] = dl->e[i];
2915 }
2916 qsort(e, sum_extents, sizeof(*e), cmp_extent);
2917
2918 /* merge extents */
2919 i = 0;
2920 j = 0;
2921 while (i < sum_extents) {
2922 e[j].start = e[i].start;
2923 e[j].size = find_size(e, &i, sum_extents);
2924 j++;
2925 if (e[j-1].size == 0)
2926 break;
2927 }
2928
2929 pos = 0;
2930 maxsize = 0;
2931 start_extent = 0;
2932 i = 0;
2933 do {
2934 unsigned long long esize;
2935
2936 esize = e[i].start - pos;
2937 if (esize >= maxsize) {
2938 maxsize = esize;
2939 start = pos;
2940 start_extent = i;
2941 }
2942 pos = e[i].start + e[i].size;
2943 i++;
2944 } while (e[i-1].size);
2945 free(e);
2946
2947 if (start_extent > 0)
2948 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
2949 else
2950 reserve = 0;
2951
2952 if (maxsize < reserve)
2953 return ~0ULL;
2954
2955 super->create_offset = ~((__u32) 0);
2956 if (start + reserve > super->create_offset)
2957 return ~0ULL; /* start overflows create_offset */
2958 super->create_offset = start + reserve;
2959
2960 return maxsize - reserve;
2961 }
2962
2963 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
2964 {
2965 if (level < 0 || level == 6 || level == 4)
2966 return 0;
2967
2968 /* if we have an orom prevent invalid raid levels */
2969 if (orom)
2970 switch (level) {
2971 case 0: return imsm_orom_has_raid0(orom);
2972 case 1:
2973 if (raiddisks > 2)
2974 return imsm_orom_has_raid1e(orom);
2975 return imsm_orom_has_raid1(orom) && raiddisks == 2;
2976 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
2977 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
2978 }
2979 else
2980 return 1; /* not on an Intel RAID platform so anything goes */
2981
2982 return 0;
2983 }
2984
2985 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
2986 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
2987 * FIX ME add ahci details
2988 */
2989 static int validate_geometry_imsm_volume(struct supertype *st, int level,
2990 int layout, int raiddisks, int chunk,
2991 unsigned long long size, char *dev,
2992 unsigned long long *freesize,
2993 int verbose)
2994 {
2995 struct stat stb;
2996 struct intel_super *super = st->sb;
2997 struct imsm_super *mpb = super->anchor;
2998 struct dl *dl;
2999 unsigned long long pos = 0;
3000 unsigned long long maxsize;
3001 struct extent *e;
3002 int i;
3003
3004 /* We must have the container info already read in. */
3005 if (!super)
3006 return 0;
3007
3008 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
3009 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3010 level, raiddisks, raiddisks > 1 ? "s" : "");
3011 return 0;
3012 }
3013 if (super->orom && level != 1 &&
3014 !imsm_orom_has_chunk(super->orom, chunk)) {
3015 pr_vrb(": platform does not support a chunk size of: %d\n", chunk);
3016 return 0;
3017 }
3018 if (layout != imsm_level_to_layout(level)) {
3019 if (level == 5)
3020 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
3021 else if (level == 10)
3022 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
3023 else
3024 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
3025 layout, level);
3026 return 0;
3027 }
3028
3029 if (!dev) {
3030 /* General test: make sure there is space for
3031 * 'raiddisks' device extents of size 'size' at a given
3032 * offset
3033 */
3034 unsigned long long minsize = size;
3035 unsigned long long start_offset = ~0ULL;
3036 int dcnt = 0;
3037 if (minsize == 0)
3038 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
3039 for (dl = super->disks; dl ; dl = dl->next) {
3040 int found = 0;
3041
3042 pos = 0;
3043 i = 0;
3044 e = get_extents(super, dl);
3045 if (!e) continue;
3046 do {
3047 unsigned long long esize;
3048 esize = e[i].start - pos;
3049 if (esize >= minsize)
3050 found = 1;
3051 if (found && start_offset == ~0ULL) {
3052 start_offset = pos;
3053 break;
3054 } else if (found && pos != start_offset) {
3055 found = 0;
3056 break;
3057 }
3058 pos = e[i].start + e[i].size;
3059 i++;
3060 } while (e[i-1].size);
3061 if (found)
3062 dcnt++;
3063 free(e);
3064 }
3065 if (dcnt < raiddisks) {
3066 if (verbose)
3067 fprintf(stderr, Name ": imsm: Not enough "
3068 "devices with space for this array "
3069 "(%d < %d)\n",
3070 dcnt, raiddisks);
3071 return 0;
3072 }
3073 return 1;
3074 }
3075
3076 /* This device must be a member of the set */
3077 if (stat(dev, &stb) < 0)
3078 return 0;
3079 if ((S_IFMT & stb.st_mode) != S_IFBLK)
3080 return 0;
3081 for (dl = super->disks ; dl ; dl = dl->next) {
3082 if (dl->major == major(stb.st_rdev) &&
3083 dl->minor == minor(stb.st_rdev))
3084 break;
3085 }
3086 if (!dl) {
3087 if (verbose)
3088 fprintf(stderr, Name ": %s is not in the "
3089 "same imsm set\n", dev);
3090 return 0;
3091 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
3092 /* If a volume is present then the current creation attempt
3093 * cannot incorporate new spares because the orom may not
3094 * understand this configuration (all member disks must be
3095 * members of each array in the container).
3096 */
3097 fprintf(stderr, Name ": %s is a spare and a volume"
3098 " is already defined for this container\n", dev);
3099 fprintf(stderr, Name ": The option-rom requires all member"
3100 " disks to be a member of all volumes\n");