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