]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-intel.c
imsm: add support for checkpointing via 'curr_migr_unit'
[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 __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 ddf;
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 __u32 random; /* random data for seeding new family numbers */
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 struct intel_super *next; /* (temp) list for disambiguating family_num */
269 };
270
271 struct intel_disk {
272 struct imsm_disk disk;
273 #define IMSM_UNKNOWN_OWNER (-1)
274 int owner;
275 struct intel_disk *next;
276 };
277
278 struct extent {
279 unsigned long long start, size;
280 };
281
282 /* definition of messages passed to imsm_process_update */
283 enum imsm_update_type {
284 update_activate_spare,
285 update_create_array,
286 update_add_disk,
287 };
288
289 struct imsm_update_activate_spare {
290 enum imsm_update_type type;
291 struct dl *dl;
292 int slot;
293 int array;
294 struct imsm_update_activate_spare *next;
295 };
296
297 struct disk_info {
298 __u8 serial[MAX_RAID_SERIAL_LEN];
299 };
300
301 struct imsm_update_create_array {
302 enum imsm_update_type type;
303 int dev_idx;
304 struct imsm_dev dev;
305 };
306
307 struct imsm_update_add_disk {
308 enum imsm_update_type type;
309 };
310
311 static struct supertype *match_metadata_desc_imsm(char *arg)
312 {
313 struct supertype *st;
314
315 if (strcmp(arg, "imsm") != 0 &&
316 strcmp(arg, "default") != 0
317 )
318 return NULL;
319
320 st = malloc(sizeof(*st));
321 memset(st, 0, sizeof(*st));
322 st->ss = &super_imsm;
323 st->max_devs = IMSM_MAX_DEVICES;
324 st->minor_version = 0;
325 st->sb = NULL;
326 return st;
327 }
328
329 #ifndef MDASSEMBLE
330 static __u8 *get_imsm_version(struct imsm_super *mpb)
331 {
332 return &mpb->sig[MPB_SIG_LEN];
333 }
334 #endif
335
336 /* retrieve a disk directly from the anchor when the anchor is known to be
337 * up-to-date, currently only at load time
338 */
339 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
340 {
341 if (index >= mpb->num_disks)
342 return NULL;
343 return &mpb->disk[index];
344 }
345
346 #ifndef MDASSEMBLE
347 /* retrieve a disk from the parsed metadata */
348 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
349 {
350 struct dl *d;
351
352 for (d = super->disks; d; d = d->next)
353 if (d->index == index)
354 return &d->disk;
355
356 return NULL;
357 }
358 #endif
359
360 /* generate a checksum directly from the anchor when the anchor is known to be
361 * up-to-date, currently only at load or write_super after coalescing
362 */
363 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
364 {
365 __u32 end = mpb->mpb_size / sizeof(end);
366 __u32 *p = (__u32 *) mpb;
367 __u32 sum = 0;
368
369 while (end--) {
370 sum += __le32_to_cpu(*p);
371 p++;
372 }
373
374 return sum - __le32_to_cpu(mpb->check_sum);
375 }
376
377 static size_t sizeof_imsm_map(struct imsm_map *map)
378 {
379 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
380 }
381
382 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
383 {
384 struct imsm_map *map = &dev->vol.map[0];
385
386 if (second_map && !dev->vol.migr_state)
387 return NULL;
388 else if (second_map) {
389 void *ptr = map;
390
391 return ptr + sizeof_imsm_map(map);
392 } else
393 return map;
394
395 }
396
397 /* return the size of the device.
398 * migr_state increases the returned size if map[0] were to be duplicated
399 */
400 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
401 {
402 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
403 sizeof_imsm_map(get_imsm_map(dev, 0));
404
405 /* migrating means an additional map */
406 if (dev->vol.migr_state)
407 size += sizeof_imsm_map(get_imsm_map(dev, 1));
408 else if (migr_state)
409 size += sizeof_imsm_map(get_imsm_map(dev, 0));
410
411 return size;
412 }
413
414 #ifndef MDASSEMBLE
415 /* retrieve disk serial number list from a metadata update */
416 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
417 {
418 void *u = update;
419 struct disk_info *inf;
420
421 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
422 sizeof_imsm_dev(&update->dev, 0);
423
424 return inf;
425 }
426 #endif
427
428 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
429 {
430 int offset;
431 int i;
432 void *_mpb = mpb;
433
434 if (index >= mpb->num_raid_devs)
435 return NULL;
436
437 /* devices start after all disks */
438 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
439
440 for (i = 0; i <= index; i++)
441 if (i == index)
442 return _mpb + offset;
443 else
444 offset += sizeof_imsm_dev(_mpb + offset, 0);
445
446 return NULL;
447 }
448
449 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
450 {
451 struct intel_dev *dv;
452
453 if (index >= super->anchor->num_raid_devs)
454 return NULL;
455 for (dv = super->devlist; dv; dv = dv->next)
456 if (dv->index == index)
457 return dv->dev;
458 return NULL;
459 }
460
461 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, int slot)
462 {
463 struct imsm_map *map;
464
465 if (dev->vol.migr_state)
466 map = get_imsm_map(dev, 1);
467 else
468 map = get_imsm_map(dev, 0);
469
470 /* top byte identifies disk under rebuild */
471 return __le32_to_cpu(map->disk_ord_tbl[slot]);
472 }
473
474 #define ord_to_idx(ord) (((ord) << 8) >> 8)
475 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot)
476 {
477 __u32 ord = get_imsm_ord_tbl_ent(dev, slot);
478
479 return ord_to_idx(ord);
480 }
481
482 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
483 {
484 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
485 }
486
487 static int get_imsm_disk_slot(struct imsm_map *map, int idx)
488 {
489 int slot;
490 __u32 ord;
491
492 for (slot = 0; slot < map->num_members; slot++) {
493 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
494 if (ord_to_idx(ord) == idx)
495 return slot;
496 }
497
498 return -1;
499 }
500
501 static int get_imsm_raid_level(struct imsm_map *map)
502 {
503 if (map->raid_level == 1) {
504 if (map->num_members == 2)
505 return 1;
506 else
507 return 10;
508 }
509
510 return map->raid_level;
511 }
512
513 static int cmp_extent(const void *av, const void *bv)
514 {
515 const struct extent *a = av;
516 const struct extent *b = bv;
517 if (a->start < b->start)
518 return -1;
519 if (a->start > b->start)
520 return 1;
521 return 0;
522 }
523
524 static int count_memberships(struct dl *dl, struct intel_super *super)
525 {
526 int memberships = 0;
527 int i;
528
529 for (i = 0; i < super->anchor->num_raid_devs; i++) {
530 struct imsm_dev *dev = get_imsm_dev(super, i);
531 struct imsm_map *map = get_imsm_map(dev, 0);
532
533 if (get_imsm_disk_slot(map, dl->index) >= 0)
534 memberships++;
535 }
536
537 return memberships;
538 }
539
540 static struct extent *get_extents(struct intel_super *super, struct dl *dl)
541 {
542 /* find a list of used extents on the given physical device */
543 struct extent *rv, *e;
544 int i;
545 int memberships = count_memberships(dl, super);
546 __u32 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
547
548 rv = malloc(sizeof(struct extent) * (memberships + 1));
549 if (!rv)
550 return NULL;
551 e = rv;
552
553 for (i = 0; i < super->anchor->num_raid_devs; i++) {
554 struct imsm_dev *dev = get_imsm_dev(super, i);
555 struct imsm_map *map = get_imsm_map(dev, 0);
556
557 if (get_imsm_disk_slot(map, dl->index) >= 0) {
558 e->start = __le32_to_cpu(map->pba_of_lba0);
559 e->size = __le32_to_cpu(map->blocks_per_member);
560 e++;
561 }
562 }
563 qsort(rv, memberships, sizeof(*rv), cmp_extent);
564
565 /* determine the start of the metadata
566 * when no raid devices are defined use the default
567 * ...otherwise allow the metadata to truncate the value
568 * as is the case with older versions of imsm
569 */
570 if (memberships) {
571 struct extent *last = &rv[memberships - 1];
572 __u32 remainder;
573
574 remainder = __le32_to_cpu(dl->disk.total_blocks) -
575 (last->start + last->size);
576 /* round down to 1k block to satisfy precision of the kernel
577 * 'size' interface
578 */
579 remainder &= ~1UL;
580 /* make sure remainder is still sane */
581 if (remainder < ROUND_UP(super->len, 512) >> 9)
582 remainder = ROUND_UP(super->len, 512) >> 9;
583 if (reservation > remainder)
584 reservation = remainder;
585 }
586 e->start = __le32_to_cpu(dl->disk.total_blocks) - reservation;
587 e->size = 0;
588 return rv;
589 }
590
591 /* try to determine how much space is reserved for metadata from
592 * the last get_extents() entry, otherwise fallback to the
593 * default
594 */
595 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
596 {
597 struct extent *e;
598 int i;
599 __u32 rv;
600
601 /* for spares just return a minimal reservation which will grow
602 * once the spare is picked up by an array
603 */
604 if (dl->index == -1)
605 return MPB_SECTOR_CNT;
606
607 e = get_extents(super, dl);
608 if (!e)
609 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
610
611 /* scroll to last entry */
612 for (i = 0; e[i].size; i++)
613 continue;
614
615 rv = __le32_to_cpu(dl->disk.total_blocks) - e[i].start;
616
617 free(e);
618
619 return rv;
620 }
621
622 static int is_spare(struct imsm_disk *disk)
623 {
624 return (disk->status & SPARE_DISK) == SPARE_DISK;
625 }
626
627 static int is_configured(struct imsm_disk *disk)
628 {
629 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
630 }
631
632 static int is_failed(struct imsm_disk *disk)
633 {
634 return (disk->status & FAILED_DISK) == FAILED_DISK;
635 }
636
637 #ifndef MDASSEMBLE
638 static __u64 blocks_per_migr_unit(struct imsm_dev *dev);
639
640 static void print_imsm_dev(struct imsm_dev *dev, char *uuid, int disk_idx)
641 {
642 __u64 sz;
643 int slot;
644 struct imsm_map *map = get_imsm_map(dev, 0);
645 __u32 ord;
646
647 printf("\n");
648 printf("[%.16s]:\n", dev->volume);
649 printf(" UUID : %s\n", uuid);
650 printf(" RAID Level : %d\n", get_imsm_raid_level(map));
651 printf(" Members : %d\n", map->num_members);
652 slot = get_imsm_disk_slot(map, disk_idx);
653 if (slot >= 0) {
654 ord = get_imsm_ord_tbl_ent(dev, slot);
655 printf(" This Slot : %d%s\n", slot,
656 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
657 } else
658 printf(" This Slot : ?\n");
659 sz = __le32_to_cpu(dev->size_high);
660 sz <<= 32;
661 sz += __le32_to_cpu(dev->size_low);
662 printf(" Array Size : %llu%s\n", (unsigned long long)sz,
663 human_size(sz * 512));
664 sz = __le32_to_cpu(map->blocks_per_member);
665 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz,
666 human_size(sz * 512));
667 printf(" Sector Offset : %u\n",
668 __le32_to_cpu(map->pba_of_lba0));
669 printf(" Num Stripes : %u\n",
670 __le32_to_cpu(map->num_data_stripes));
671 printf(" Chunk Size : %u KiB\n",
672 __le16_to_cpu(map->blocks_per_strip) / 2);
673 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
674 printf(" Migrate State : ");
675 if (dev->vol.migr_state) {
676 if (migr_type(dev) == MIGR_INIT)
677 printf("initialize\n");
678 else if (migr_type(dev) == MIGR_REBUILD)
679 printf("rebuild\n");
680 else if (migr_type(dev) == MIGR_VERIFY)
681 printf("check\n");
682 else if (migr_type(dev) == MIGR_GEN_MIGR)
683 printf("general migration\n");
684 else if (migr_type(dev) == MIGR_STATE_CHANGE)
685 printf("state change\n");
686 else if (migr_type(dev) == MIGR_REPAIR)
687 printf("repair\n");
688 else
689 printf("<unknown:%d>\n", migr_type(dev));
690 } else
691 printf("idle\n");
692 printf(" Map State : %s", map_state_str[map->map_state]);
693 if (dev->vol.migr_state) {
694 struct imsm_map *map = get_imsm_map(dev, 1);
695
696 printf(" <-- %s", map_state_str[map->map_state]);
697 printf("\n Checkpoint : %u (%llu)",
698 __le32_to_cpu(dev->vol.curr_migr_unit),
699 blocks_per_migr_unit(dev));
700 }
701 printf("\n");
702 printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
703 }
704
705 static void print_imsm_disk(struct imsm_super *mpb, int index, __u32 reserved)
706 {
707 struct imsm_disk *disk = __get_imsm_disk(mpb, index);
708 char str[MAX_RAID_SERIAL_LEN + 1];
709 __u64 sz;
710
711 if (index < 0)
712 return;
713
714 printf("\n");
715 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
716 printf(" Disk%02d Serial : %s\n", index, str);
717 printf(" State :%s%s%s\n", is_spare(disk) ? " spare" : "",
718 is_configured(disk) ? " active" : "",
719 is_failed(disk) ? " failed" : "");
720 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
721 sz = __le32_to_cpu(disk->total_blocks) - reserved;
722 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
723 human_size(sz * 512));
724 }
725
726 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info);
727
728 static void examine_super_imsm(struct supertype *st, char *homehost)
729 {
730 struct intel_super *super = st->sb;
731 struct imsm_super *mpb = super->anchor;
732 char str[MAX_SIGNATURE_LENGTH];
733 int i;
734 struct mdinfo info;
735 char nbuf[64];
736 __u32 sum;
737 __u32 reserved = imsm_reserved_sectors(super, super->disks);
738
739
740 snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
741 printf(" Magic : %s\n", str);
742 snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
743 printf(" Version : %s\n", get_imsm_version(mpb));
744 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
745 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
746 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
747 getinfo_super_imsm(st, &info);
748 fname_from_uuid(st, &info, nbuf, ':');
749 printf(" UUID : %s\n", nbuf + 5);
750 sum = __le32_to_cpu(mpb->check_sum);
751 printf(" Checksum : %08x %s\n", sum,
752 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
753 printf(" MPB Sectors : %d\n", mpb_sectors(mpb));
754 printf(" Disks : %d\n", mpb->num_disks);
755 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
756 print_imsm_disk(mpb, super->disks->index, reserved);
757 if (super->bbm_log) {
758 struct bbm_log *log = super->bbm_log;
759
760 printf("\n");
761 printf("Bad Block Management Log:\n");
762 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
763 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
764 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
765 printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count));
766 printf(" First Spare : %llx\n",
767 (unsigned long long) __le64_to_cpu(log->first_spare_lba));
768 }
769 for (i = 0; i < mpb->num_raid_devs; i++) {
770 struct mdinfo info;
771 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
772
773 super->current_vol = i;
774 getinfo_super_imsm(st, &info);
775 fname_from_uuid(st, &info, nbuf, ':');
776 print_imsm_dev(dev, nbuf + 5, super->disks->index);
777 }
778 for (i = 0; i < mpb->num_disks; i++) {
779 if (i == super->disks->index)
780 continue;
781 print_imsm_disk(mpb, i, reserved);
782 }
783 }
784
785 static void brief_examine_super_imsm(struct supertype *st, int verbose)
786 {
787 /* We just write a generic IMSM ARRAY entry */
788 struct mdinfo info;
789 char nbuf[64];
790 struct intel_super *super = st->sb;
791
792 if (!super->anchor->num_raid_devs) {
793 printf("ARRAY metadata=imsm\n");
794 return;
795 }
796
797 getinfo_super_imsm(st, &info);
798 fname_from_uuid(st, &info, nbuf, ':');
799 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
800 }
801
802 static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
803 {
804 /* We just write a generic IMSM ARRAY entry */
805 struct mdinfo info;
806 char nbuf[64];
807 char nbuf1[64];
808 struct intel_super *super = st->sb;
809 int i;
810
811 if (!super->anchor->num_raid_devs)
812 return;
813
814 getinfo_super_imsm(st, &info);
815 fname_from_uuid(st, &info, nbuf, ':');
816 for (i = 0; i < super->anchor->num_raid_devs; i++) {
817 struct imsm_dev *dev = get_imsm_dev(super, i);
818
819 super->current_vol = i;
820 getinfo_super_imsm(st, &info);
821 fname_from_uuid(st, &info, nbuf1, ':');
822 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
823 dev->volume, nbuf + 5, i, nbuf1 + 5);
824 }
825 }
826
827 static void export_examine_super_imsm(struct supertype *st)
828 {
829 struct intel_super *super = st->sb;
830 struct imsm_super *mpb = super->anchor;
831 struct mdinfo info;
832 char nbuf[64];
833
834 getinfo_super_imsm(st, &info);
835 fname_from_uuid(st, &info, nbuf, ':');
836 printf("MD_METADATA=imsm\n");
837 printf("MD_LEVEL=container\n");
838 printf("MD_UUID=%s\n", nbuf+5);
839 printf("MD_DEVICES=%u\n", mpb->num_disks);
840 }
841
842 static void detail_super_imsm(struct supertype *st, char *homehost)
843 {
844 struct mdinfo info;
845 char nbuf[64];
846
847 getinfo_super_imsm(st, &info);
848 fname_from_uuid(st, &info, nbuf, ':');
849 printf("\n UUID : %s\n", nbuf + 5);
850 }
851
852 static void brief_detail_super_imsm(struct supertype *st)
853 {
854 struct mdinfo info;
855 char nbuf[64];
856 getinfo_super_imsm(st, &info);
857 fname_from_uuid(st, &info, nbuf, ':');
858 printf(" UUID=%s", nbuf + 5);
859 }
860
861 static int imsm_read_serial(int fd, char *devname, __u8 *serial);
862 static void fd2devname(int fd, char *name);
863
864 static int imsm_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
865 {
866 /* dump an unsorted list of devices attached to ahci, as well as
867 * non-connected ports
868 */
869 int hba_len = strlen(hba_path) + 1;
870 struct dirent *ent;
871 DIR *dir;
872 char *path = NULL;
873 int err = 0;
874 unsigned long port_mask = (1 << port_count) - 1;
875
876 if (port_count > sizeof(port_mask) * 8) {
877 if (verbose)
878 fprintf(stderr, Name ": port_count %d out of range\n", port_count);
879 return 2;
880 }
881
882 /* scroll through /sys/dev/block looking for devices attached to
883 * this hba
884 */
885 dir = opendir("/sys/dev/block");
886 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
887 int fd;
888 char model[64];
889 char vendor[64];
890 char buf[1024];
891 int major, minor;
892 char *device;
893 char *c;
894 int port;
895 int type;
896
897 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
898 continue;
899 path = devt_to_devpath(makedev(major, minor));
900 if (!path)
901 continue;
902 if (!path_attached_to_hba(path, hba_path)) {
903 free(path);
904 path = NULL;
905 continue;
906 }
907
908 /* retrieve the scsi device type */
909 if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) {
910 if (verbose)
911 fprintf(stderr, Name ": failed to allocate 'device'\n");
912 err = 2;
913 break;
914 }
915 sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
916 if (load_sys(device, buf) != 0) {
917 if (verbose)
918 fprintf(stderr, Name ": failed to read device type for %s\n",
919 path);
920 err = 2;
921 free(device);
922 break;
923 }
924 type = strtoul(buf, NULL, 10);
925
926 /* if it's not a disk print the vendor and model */
927 if (!(type == 0 || type == 7 || type == 14)) {
928 vendor[0] = '\0';
929 model[0] = '\0';
930 sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
931 if (load_sys(device, buf) == 0) {
932 strncpy(vendor, buf, sizeof(vendor));
933 vendor[sizeof(vendor) - 1] = '\0';
934 c = (char *) &vendor[sizeof(vendor) - 1];
935 while (isspace(*c) || *c == '\0')
936 *c-- = '\0';
937
938 }
939 sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
940 if (load_sys(device, buf) == 0) {
941 strncpy(model, buf, sizeof(model));
942 model[sizeof(model) - 1] = '\0';
943 c = (char *) &model[sizeof(model) - 1];
944 while (isspace(*c) || *c == '\0')
945 *c-- = '\0';
946 }
947
948 if (vendor[0] && model[0])
949 sprintf(buf, "%.64s %.64s", vendor, model);
950 else
951 switch (type) { /* numbers from hald/linux/device.c */
952 case 1: sprintf(buf, "tape"); break;
953 case 2: sprintf(buf, "printer"); break;
954 case 3: sprintf(buf, "processor"); break;
955 case 4:
956 case 5: sprintf(buf, "cdrom"); break;
957 case 6: sprintf(buf, "scanner"); break;
958 case 8: sprintf(buf, "media_changer"); break;
959 case 9: sprintf(buf, "comm"); break;
960 case 12: sprintf(buf, "raid"); break;
961 default: sprintf(buf, "unknown");
962 }
963 } else
964 buf[0] = '\0';
965 free(device);
966
967 /* chop device path to 'host%d' and calculate the port number */
968 c = strchr(&path[hba_len], '/');
969 *c = '\0';
970 if (sscanf(&path[hba_len], "host%d", &port) == 1)
971 port -= host_base;
972 else {
973 if (verbose) {
974 *c = '/'; /* repair the full string */
975 fprintf(stderr, Name ": failed to determine port number for %s\n",
976 path);
977 }
978 err = 2;
979 break;
980 }
981
982 /* mark this port as used */
983 port_mask &= ~(1 << port);
984
985 /* print out the device information */
986 if (buf[0]) {
987 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
988 continue;
989 }
990
991 fd = dev_open(ent->d_name, O_RDONLY);
992 if (fd < 0)
993 printf(" Port%d : - disk info unavailable -\n", port);
994 else {
995 fd2devname(fd, buf);
996 printf(" Port%d : %s", port, buf);
997 if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0)
998 printf(" (%s)\n", buf);
999 else
1000 printf("()\n");
1001 }
1002 close(fd);
1003 free(path);
1004 path = NULL;
1005 }
1006 if (path)
1007 free(path);
1008 if (dir)
1009 closedir(dir);
1010 if (err == 0) {
1011 int i;
1012
1013 for (i = 0; i < port_count; i++)
1014 if (port_mask & (1 << i))
1015 printf(" Port%d : - no device attached -\n", i);
1016 }
1017
1018 return err;
1019 }
1020
1021 static int detail_platform_imsm(int verbose, int enumerate_only)
1022 {
1023 /* There are two components to imsm platform support, the ahci SATA
1024 * controller and the option-rom. To find the SATA controller we
1025 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1026 * controller with the Intel vendor id is present. This approach
1027 * allows mdadm to leverage the kernel's ahci detection logic, with the
1028 * caveat that if ahci.ko is not loaded mdadm will not be able to
1029 * detect platform raid capabilities. The option-rom resides in a
1030 * platform "Adapter ROM". We scan for its signature to retrieve the
1031 * platform capabilities. If raid support is disabled in the BIOS the
1032 * option-rom capability structure will not be available.
1033 */
1034 const struct imsm_orom *orom;
1035 struct sys_dev *list, *hba;
1036 DIR *dir;
1037 struct dirent *ent;
1038 const char *hba_path;
1039 int host_base = 0;
1040 int port_count = 0;
1041
1042 if (enumerate_only) {
1043 if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
1044 return 0;
1045 return 2;
1046 }
1047
1048 list = find_driver_devices("pci", "ahci");
1049 for (hba = list; hba; hba = hba->next)
1050 if (devpath_to_vendor(hba->path) == 0x8086)
1051 break;
1052
1053 if (!hba) {
1054 if (verbose)
1055 fprintf(stderr, Name ": unable to find active ahci controller\n");
1056 free_sys_dev(&list);
1057 return 2;
1058 } else if (verbose)
1059 fprintf(stderr, Name ": found Intel SATA AHCI Controller\n");
1060 hba_path = hba->path;
1061 hba->path = NULL;
1062 free_sys_dev(&list);
1063
1064 orom = find_imsm_orom();
1065 if (!orom) {
1066 if (verbose)
1067 fprintf(stderr, Name ": imsm option-rom not found\n");
1068 return 2;
1069 }
1070
1071 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1072 printf(" Version : %d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
1073 orom->hotfix_ver, orom->build);
1074 printf(" RAID Levels :%s%s%s%s%s\n",
1075 imsm_orom_has_raid0(orom) ? " raid0" : "",
1076 imsm_orom_has_raid1(orom) ? " raid1" : "",
1077 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
1078 imsm_orom_has_raid10(orom) ? " raid10" : "",
1079 imsm_orom_has_raid5(orom) ? " raid5" : "");
1080 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1081 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
1082 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
1083 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
1084 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
1085 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
1086 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
1087 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
1088 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
1089 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
1090 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
1091 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
1092 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
1093 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
1094 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
1095 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
1096 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
1097 printf(" Max Disks : %d\n", orom->tds);
1098 printf(" Max Volumes : %d\n", orom->vpa);
1099 printf(" I/O Controller : %s\n", hba_path);
1100
1101 /* find the smallest scsi host number to determine a port number base */
1102 dir = opendir(hba_path);
1103 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
1104 int host;
1105
1106 if (sscanf(ent->d_name, "host%d", &host) != 1)
1107 continue;
1108 if (port_count == 0)
1109 host_base = host;
1110 else if (host < host_base)
1111 host_base = host;
1112
1113 if (host + 1 > port_count + host_base)
1114 port_count = host + 1 - host_base;
1115
1116 }
1117 if (dir)
1118 closedir(dir);
1119
1120 if (!port_count || imsm_enumerate_ports(hba_path, port_count,
1121 host_base, verbose) != 0) {
1122 if (verbose)
1123 fprintf(stderr, Name ": failed to enumerate ports\n");
1124 return 2;
1125 }
1126
1127 return 0;
1128 }
1129 #endif
1130
1131 static int match_home_imsm(struct supertype *st, char *homehost)
1132 {
1133 /* the imsm metadata format does not specify any host
1134 * identification information. We return -1 since we can never
1135 * confirm nor deny whether a given array is "meant" for this
1136 * host. We rely on compare_super and the 'family_num' fields to
1137 * exclude member disks that do not belong, and we rely on
1138 * mdadm.conf to specify the arrays that should be assembled.
1139 * Auto-assembly may still pick up "foreign" arrays.
1140 */
1141
1142 return -1;
1143 }
1144
1145 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
1146 {
1147 /* The uuid returned here is used for:
1148 * uuid to put into bitmap file (Create, Grow)
1149 * uuid for backup header when saving critical section (Grow)
1150 * comparing uuids when re-adding a device into an array
1151 * In these cases the uuid required is that of the data-array,
1152 * not the device-set.
1153 * uuid to recognise same set when adding a missing device back
1154 * to an array. This is a uuid for the device-set.
1155 *
1156 * For each of these we can make do with a truncated
1157 * or hashed uuid rather than the original, as long as
1158 * everyone agrees.
1159 * In each case the uuid required is that of the data-array,
1160 * not the device-set.
1161 */
1162 /* imsm does not track uuid's so we synthesis one using sha1 on
1163 * - The signature (Which is constant for all imsm array, but no matter)
1164 * - the orig_family_num of the container
1165 * - the index number of the volume
1166 * - the 'serial' number of the volume.
1167 * Hopefully these are all constant.
1168 */
1169 struct intel_super *super = st->sb;
1170
1171 char buf[20];
1172 struct sha1_ctx ctx;
1173 struct imsm_dev *dev = NULL;
1174 __u32 family_num;
1175
1176 /* some mdadm versions failed to set ->orig_family_num, in which
1177 * case fall back to ->family_num. orig_family_num will be
1178 * fixed up with the first metadata update.
1179 */
1180 family_num = super->anchor->orig_family_num;
1181 if (family_num == 0)
1182 family_num = super->anchor->family_num;
1183 sha1_init_ctx(&ctx);
1184 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
1185 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
1186 if (super->current_vol >= 0)
1187 dev = get_imsm_dev(super, super->current_vol);
1188 if (dev) {
1189 __u32 vol = super->current_vol;
1190 sha1_process_bytes(&vol, sizeof(vol), &ctx);
1191 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
1192 }
1193 sha1_finish_ctx(&ctx, buf);
1194 memcpy(uuid, buf, 4*4);
1195 }
1196
1197 #if 0
1198 static void
1199 get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
1200 {
1201 __u8 *v = get_imsm_version(mpb);
1202 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
1203 char major[] = { 0, 0, 0 };
1204 char minor[] = { 0 ,0, 0 };
1205 char patch[] = { 0, 0, 0 };
1206 char *ver_parse[] = { major, minor, patch };
1207 int i, j;
1208
1209 i = j = 0;
1210 while (*v != '\0' && v < end) {
1211 if (*v != '.' && j < 2)
1212 ver_parse[i][j++] = *v;
1213 else {
1214 i++;
1215 j = 0;
1216 }
1217 v++;
1218 }
1219
1220 *m = strtol(minor, NULL, 0);
1221 *p = strtol(patch, NULL, 0);
1222 }
1223 #endif
1224
1225 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
1226 {
1227 /* migr_strip_size when repairing or initializing parity */
1228 struct imsm_map *map = get_imsm_map(dev, 0);
1229 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1230
1231 switch (get_imsm_raid_level(map)) {
1232 case 5:
1233 case 10:
1234 return chunk;
1235 default:
1236 return 128*1024 >> 9;
1237 }
1238 }
1239
1240 static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
1241 {
1242 /* migr_strip_size when rebuilding a degraded disk, no idea why
1243 * this is different than migr_strip_size_resync(), but it's good
1244 * to be compatible
1245 */
1246 struct imsm_map *map = get_imsm_map(dev, 1);
1247 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1248
1249 switch (get_imsm_raid_level(map)) {
1250 case 1:
1251 case 10:
1252 if (map->num_members % map->num_domains == 0)
1253 return 128*1024 >> 9;
1254 else
1255 return chunk;
1256 case 5:
1257 return max((__u32) 64*1024 >> 9, chunk);
1258 default:
1259 return 128*1024 >> 9;
1260 }
1261 }
1262
1263 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
1264 {
1265 struct imsm_map *lo = get_imsm_map(dev, 0);
1266 struct imsm_map *hi = get_imsm_map(dev, 1);
1267 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
1268 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
1269
1270 return max((__u32) 1, hi_chunk / lo_chunk);
1271 }
1272
1273 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
1274 {
1275 struct imsm_map *lo = get_imsm_map(dev, 0);
1276 int level = get_imsm_raid_level(lo);
1277
1278 if (level == 1 || level == 10) {
1279 struct imsm_map *hi = get_imsm_map(dev, 1);
1280
1281 return hi->num_domains;
1282 } else
1283 return num_stripes_per_unit_resync(dev);
1284 }
1285
1286 static __u8 imsm_num_data_members(struct imsm_dev *dev)
1287 {
1288 /* named 'imsm_' because raid0, raid1 and raid10
1289 * counter-intuitively have the same number of data disks
1290 */
1291 struct imsm_map *map = get_imsm_map(dev, 0);
1292
1293 switch (get_imsm_raid_level(map)) {
1294 case 0:
1295 case 1:
1296 case 10:
1297 return map->num_members;
1298 case 5:
1299 return map->num_members - 1;
1300 default:
1301 dprintf("%s: unsupported raid level\n", __func__);
1302 return 0;
1303 }
1304 }
1305
1306 static __u32 parity_segment_depth(struct imsm_dev *dev)
1307 {
1308 struct imsm_map *map = get_imsm_map(dev, 0);
1309 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1310
1311 switch(get_imsm_raid_level(map)) {
1312 case 1:
1313 case 10:
1314 return chunk * map->num_domains;
1315 case 5:
1316 return chunk * map->num_members;
1317 default:
1318 return chunk;
1319 }
1320 }
1321
1322 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
1323 {
1324 struct imsm_map *map = get_imsm_map(dev, 1);
1325 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1326 __u32 strip = block / chunk;
1327
1328 switch (get_imsm_raid_level(map)) {
1329 case 1:
1330 case 10: {
1331 __u32 vol_strip = (strip * map->num_domains) + 1;
1332 __u32 vol_stripe = vol_strip / map->num_members;
1333
1334 return vol_stripe * chunk + block % chunk;
1335 } case 5: {
1336 __u32 stripe = strip / (map->num_members - 1);
1337
1338 return stripe * chunk + block % chunk;
1339 }
1340 default:
1341 return 0;
1342 }
1343 }
1344
1345 static __u64 blocks_per_migr_unit(struct imsm_dev *dev)
1346 {
1347 /* calculate the conversion factor between per member 'blocks'
1348 * (md/{resync,rebuild}_start) and imsm migration units, return
1349 * 0 for the 'not migrating' and 'unsupported migration' cases
1350 */
1351 if (!dev->vol.migr_state)
1352 return 0;
1353
1354 switch (migr_type(dev)) {
1355 case MIGR_VERIFY:
1356 case MIGR_REPAIR:
1357 case MIGR_INIT: {
1358 struct imsm_map *map = get_imsm_map(dev, 0);
1359 __u32 stripes_per_unit;
1360 __u32 blocks_per_unit;
1361 __u32 parity_depth;
1362 __u32 migr_chunk;
1363 __u32 block_map;
1364 __u32 block_rel;
1365 __u32 segment;
1366 __u32 stripe;
1367 __u8 disks;
1368
1369 /* yes, this is really the translation of migr_units to
1370 * per-member blocks in the 'resync' case
1371 */
1372 stripes_per_unit = num_stripes_per_unit_resync(dev);
1373 migr_chunk = migr_strip_blocks_resync(dev);
1374 disks = imsm_num_data_members(dev);
1375 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
1376 stripe = __le32_to_cpu(map->blocks_per_strip) * disks;
1377 segment = blocks_per_unit / stripe;
1378 block_rel = blocks_per_unit - segment * stripe;
1379 parity_depth = parity_segment_depth(dev);
1380 block_map = map_migr_block(dev, block_rel);
1381 return block_map + parity_depth * segment;
1382 }
1383 case MIGR_REBUILD: {
1384 __u32 stripes_per_unit;
1385 __u32 migr_chunk;
1386
1387 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
1388 migr_chunk = migr_strip_blocks_rebuild(dev);
1389 return migr_chunk * stripes_per_unit;
1390 }
1391 case MIGR_GEN_MIGR:
1392 case MIGR_STATE_CHANGE:
1393 default:
1394 return 0;
1395 }
1396 }
1397
1398 static int imsm_level_to_layout(int level)
1399 {
1400 switch (level) {
1401 case 0:
1402 case 1:
1403 return 0;
1404 case 5:
1405 case 6:
1406 return ALGORITHM_LEFT_ASYMMETRIC;
1407 case 10:
1408 return 0x102;
1409 }
1410 return UnSet;
1411 }
1412
1413 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info)
1414 {
1415 struct intel_super *super = st->sb;
1416 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
1417 struct imsm_map *map = get_imsm_map(dev, 0);
1418 struct dl *dl;
1419
1420 for (dl = super->disks; dl; dl = dl->next)
1421 if (dl->raiddisk == info->disk.raid_disk)
1422 break;
1423 info->container_member = super->current_vol;
1424 info->array.raid_disks = map->num_members;
1425 info->array.level = get_imsm_raid_level(map);
1426 info->array.layout = imsm_level_to_layout(info->array.level);
1427 info->array.md_minor = -1;
1428 info->array.ctime = 0;
1429 info->array.utime = 0;
1430 info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9;
1431 info->array.state = !dev->vol.dirty;
1432 info->custom_array_size = __le32_to_cpu(dev->size_high);
1433 info->custom_array_size <<= 32;
1434 info->custom_array_size |= __le32_to_cpu(dev->size_low);
1435
1436 info->disk.major = 0;
1437 info->disk.minor = 0;
1438 if (dl) {
1439 info->disk.major = dl->major;
1440 info->disk.minor = dl->minor;
1441 }
1442
1443 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
1444 info->component_size = __le32_to_cpu(map->blocks_per_member);
1445 memset(info->uuid, 0, sizeof(info->uuid));
1446
1447 if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty) {
1448 info->resync_start = 0;
1449 } else if (dev->vol.migr_state) {
1450 switch (migr_type(dev)) {
1451 case MIGR_REPAIR:
1452 case MIGR_INIT: {
1453 __u64 blocks_per_unit = blocks_per_migr_unit(dev);
1454 __u64 units = __le32_to_cpu(dev->vol.curr_migr_unit);
1455
1456 info->resync_start = blocks_per_unit * units;
1457 break;
1458 }
1459 case MIGR_VERIFY:
1460 /* we could emulate the checkpointing of
1461 * 'sync_action=check' migrations, but for now
1462 * we just immediately complete them
1463 */
1464 case MIGR_REBUILD:
1465 /* this is handled by container_content_imsm() */
1466 case MIGR_GEN_MIGR:
1467 case MIGR_STATE_CHANGE:
1468 /* FIXME handle other migrations */
1469 default:
1470 /* we are not dirty, so... */
1471 info->resync_start = MaxSector;
1472 }
1473 } else
1474 info->resync_start = MaxSector;
1475
1476 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1477 info->name[MAX_RAID_SERIAL_LEN] = 0;
1478
1479 info->array.major_version = -1;
1480 info->array.minor_version = -2;
1481 sprintf(info->text_version, "/%s/%d",
1482 devnum2devname(st->container_dev),
1483 info->container_member);
1484 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
1485 uuid_from_super_imsm(st, info->uuid);
1486 }
1487
1488 /* check the config file to see if we can return a real uuid for this spare */
1489 static void fixup_container_spare_uuid(struct mdinfo *inf)
1490 {
1491 struct mddev_ident_s *array_list;
1492
1493 if (inf->array.level != LEVEL_CONTAINER ||
1494 memcmp(inf->uuid, uuid_match_any, sizeof(int[4])) != 0)
1495 return;
1496
1497 array_list = conf_get_ident(NULL);
1498
1499 for (; array_list; array_list = array_list->next) {
1500 if (array_list->uuid_set) {
1501 struct supertype *_sst; /* spare supertype */
1502 struct supertype *_cst; /* container supertype */
1503
1504 _cst = array_list->st;
1505 if (_cst)
1506 _sst = _cst->ss->match_metadata_desc(inf->text_version);
1507 else
1508 _sst = NULL;
1509
1510 if (_sst) {
1511 memcpy(inf->uuid, array_list->uuid, sizeof(int[4]));
1512 free(_sst);
1513 break;
1514 }
1515 }
1516 }
1517 }
1518
1519 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
1520 {
1521 struct intel_super *super = st->sb;
1522 struct imsm_disk *disk;
1523
1524 if (super->current_vol >= 0) {
1525 getinfo_super_imsm_volume(st, info);
1526 return;
1527 }
1528
1529 /* Set raid_disks to zero so that Assemble will always pull in valid
1530 * spares
1531 */
1532 info->array.raid_disks = 0;
1533 info->array.level = LEVEL_CONTAINER;
1534 info->array.layout = 0;
1535 info->array.md_minor = -1;
1536 info->array.ctime = 0; /* N/A for imsm */
1537 info->array.utime = 0;
1538 info->array.chunk_size = 0;
1539
1540 info->disk.major = 0;
1541 info->disk.minor = 0;
1542 info->disk.raid_disk = -1;
1543 info->reshape_active = 0;
1544 info->array.major_version = -1;
1545 info->array.minor_version = -2;
1546 strcpy(info->text_version, "imsm");
1547 info->safe_mode_delay = 0;
1548 info->disk.number = -1;
1549 info->disk.state = 0;
1550 info->name[0] = 0;
1551
1552 if (super->disks) {
1553 __u32 reserved = imsm_reserved_sectors(super, super->disks);
1554
1555 disk = &super->disks->disk;
1556 info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved;
1557 info->component_size = reserved;
1558 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
1559 /* we don't change info->disk.raid_disk here because
1560 * this state will be finalized in mdmon after we have
1561 * found the 'most fresh' version of the metadata
1562 */
1563 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
1564 info->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
1565 }
1566
1567 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1568 * ->compare_super may have updated the 'num_raid_devs' field for spares
1569 */
1570 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
1571 uuid_from_super_imsm(st, info->uuid);
1572 else {
1573 memcpy(info->uuid, uuid_match_any, sizeof(int[4]));
1574 fixup_container_spare_uuid(info);
1575 }
1576 }
1577
1578 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
1579 char *update, char *devname, int verbose,
1580 int uuid_set, char *homehost)
1581 {
1582 /* For 'assemble' and 'force' we need to return non-zero if any
1583 * change was made. For others, the return value is ignored.
1584 * Update options are:
1585 * force-one : This device looks a bit old but needs to be included,
1586 * update age info appropriately.
1587 * assemble: clear any 'faulty' flag to allow this device to
1588 * be assembled.
1589 * force-array: Array is degraded but being forced, mark it clean
1590 * if that will be needed to assemble it.
1591 *
1592 * newdev: not used ????
1593 * grow: Array has gained a new device - this is currently for
1594 * linear only
1595 * resync: mark as dirty so a resync will happen.
1596 * name: update the name - preserving the homehost
1597 * uuid: Change the uuid of the array to match watch is given
1598 *
1599 * Following are not relevant for this imsm:
1600 * sparc2.2 : update from old dodgey metadata
1601 * super-minor: change the preferred_minor number
1602 * summaries: update redundant counters.
1603 * homehost: update the recorded homehost
1604 * _reshape_progress: record new reshape_progress position.
1605 */
1606 int rv = 1;
1607 struct intel_super *super = st->sb;
1608 struct imsm_super *mpb;
1609
1610 /* we can only update container info */
1611 if (!super || super->current_vol >= 0 || !super->anchor)
1612 return 1;
1613
1614 mpb = super->anchor;
1615
1616 if (strcmp(update, "uuid") == 0 && uuid_set && !info->update_private)
1617 fprintf(stderr,
1618 Name ": '--uuid' not supported for imsm metadata\n");
1619 else if (strcmp(update, "uuid") == 0 && uuid_set && info->update_private) {
1620 mpb->orig_family_num = *((__u32 *) info->update_private);
1621 rv = 0;
1622 } else if (strcmp(update, "uuid") == 0) {
1623 __u32 *new_family = malloc(sizeof(*new_family));
1624
1625 /* update orig_family_number with the incoming random
1626 * data, report the new effective uuid, and store the
1627 * new orig_family_num for future updates.
1628 */
1629 if (new_family) {
1630 memcpy(&mpb->orig_family_num, info->uuid, sizeof(__u32));
1631 uuid_from_super_imsm(st, info->uuid);
1632 *new_family = mpb->orig_family_num;
1633 info->update_private = new_family;
1634 rv = 0;
1635 }
1636 } else if (strcmp(update, "assemble") == 0)
1637 rv = 0;
1638 else
1639 fprintf(stderr,
1640 Name ": '--update=%s' not supported for imsm metadata\n",
1641 update);
1642
1643 /* successful update? recompute checksum */
1644 if (rv == 0)
1645 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
1646
1647 return rv;
1648 }
1649
1650 static size_t disks_to_mpb_size(int disks)
1651 {
1652 size_t size;
1653
1654 size = sizeof(struct imsm_super);
1655 size += (disks - 1) * sizeof(struct imsm_disk);
1656 size += 2 * sizeof(struct imsm_dev);
1657 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1658 size += (4 - 2) * sizeof(struct imsm_map);
1659 /* 4 possible disk_ord_tbl's */
1660 size += 4 * (disks - 1) * sizeof(__u32);
1661
1662 return size;
1663 }
1664
1665 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
1666 {
1667 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
1668 return 0;
1669
1670 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
1671 }
1672
1673 static void free_devlist(struct intel_super *super)
1674 {
1675 struct intel_dev *dv;
1676
1677 while (super->devlist) {
1678 dv = super->devlist->next;
1679 free(super->devlist->dev);
1680 free(super->devlist);
1681 super->devlist = dv;
1682 }
1683 }
1684
1685 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
1686 {
1687 memcpy(dest, src, sizeof_imsm_dev(src, 0));
1688 }
1689
1690 static int compare_super_imsm(struct supertype *st, struct supertype *tst)
1691 {
1692 /*
1693 * return:
1694 * 0 same, or first was empty, and second was copied
1695 * 1 second had wrong number
1696 * 2 wrong uuid
1697 * 3 wrong other info
1698 */
1699 struct intel_super *first = st->sb;
1700 struct intel_super *sec = tst->sb;
1701
1702 if (!first) {
1703 st->sb = tst->sb;
1704 tst->sb = NULL;
1705 return 0;
1706 }
1707
1708 /* if an anchor does not have num_raid_devs set then it is a free
1709 * floating spare
1710 */
1711 if (first->anchor->num_raid_devs > 0 &&
1712 sec->anchor->num_raid_devs > 0) {
1713 /* Determine if these disks might ever have been
1714 * related. Further disambiguation can only take place
1715 * in load_super_imsm_all
1716 */
1717 __u32 first_family = first->anchor->orig_family_num;
1718 __u32 sec_family = sec->anchor->orig_family_num;
1719
1720 if (memcmp(first->anchor->sig, sec->anchor->sig,
1721 MAX_SIGNATURE_LENGTH) != 0)
1722 return 3;
1723
1724 if (first_family == 0)
1725 first_family = first->anchor->family_num;
1726 if (sec_family == 0)
1727 sec_family = sec->anchor->family_num;
1728
1729 if (first_family != sec_family)
1730 return 3;
1731
1732 }
1733
1734
1735 /* if 'first' is a spare promote it to a populated mpb with sec's
1736 * family number
1737 */
1738 if (first->anchor->num_raid_devs == 0 &&
1739 sec->anchor->num_raid_devs > 0) {
1740 int i;
1741 struct intel_dev *dv;
1742 struct imsm_dev *dev;
1743
1744 /* we need to copy raid device info from sec if an allocation
1745 * fails here we don't associate the spare
1746 */
1747 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
1748 dv = malloc(sizeof(*dv));
1749 if (!dv)
1750 break;
1751 dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
1752 if (!dev) {
1753 free(dv);
1754 break;
1755 }
1756 dv->dev = dev;
1757 dv->index = i;
1758 dv->next = first->devlist;
1759 first->devlist = dv;
1760 }
1761 if (i < sec->anchor->num_raid_devs) {
1762 /* allocation failure */
1763 free_devlist(first);
1764 fprintf(stderr, "imsm: failed to associate spare\n");
1765 return 3;
1766 }
1767 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
1768 first->anchor->orig_family_num = sec->anchor->orig_family_num;
1769 first->anchor->family_num = sec->anchor->family_num;
1770 memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH);
1771 for (i = 0; i < sec->anchor->num_raid_devs; i++)
1772 imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
1773 }
1774
1775 return 0;
1776 }
1777
1778 static void fd2devname(int fd, char *name)
1779 {
1780 struct stat st;
1781 char path[256];
1782 char dname[100];
1783 char *nm;
1784 int rv;
1785
1786 name[0] = '\0';
1787 if (fstat(fd, &st) != 0)
1788 return;
1789 sprintf(path, "/sys/dev/block/%d:%d",
1790 major(st.st_rdev), minor(st.st_rdev));
1791
1792 rv = readlink(path, dname, sizeof(dname));
1793 if (rv <= 0)
1794 return;
1795
1796 dname[rv] = '\0';
1797 nm = strrchr(dname, '/');
1798 nm++;
1799 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
1800 }
1801
1802 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
1803
1804 static int imsm_read_serial(int fd, char *devname,
1805 __u8 serial[MAX_RAID_SERIAL_LEN])
1806 {
1807 unsigned char scsi_serial[255];
1808 int rv;
1809 int rsp_len;
1810 int len;
1811 char *dest;
1812 char *src;
1813 char *rsp_buf;
1814 int i;
1815
1816 memset(scsi_serial, 0, sizeof(scsi_serial));
1817
1818 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
1819
1820 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
1821 memset(serial, 0, MAX_RAID_SERIAL_LEN);
1822 fd2devname(fd, (char *) serial);
1823 return 0;
1824 }
1825
1826 if (rv != 0) {
1827 if (devname)
1828 fprintf(stderr,
1829 Name ": Failed to retrieve serial for %s\n",
1830 devname);
1831 return rv;
1832 }
1833
1834 rsp_len = scsi_serial[3];
1835 if (!rsp_len) {
1836 if (devname)
1837 fprintf(stderr,
1838 Name ": Failed to retrieve serial for %s\n",
1839 devname);
1840 return 2;
1841 }
1842 rsp_buf = (char *) &scsi_serial[4];
1843
1844 /* trim all whitespace and non-printable characters and convert
1845 * ':' to ';'
1846 */
1847 for (i = 0, dest = rsp_buf; i < rsp_len; i++) {
1848 src = &rsp_buf[i];
1849 if (*src > 0x20) {
1850 /* ':' is reserved for use in placeholder serial
1851 * numbers for missing disks
1852 */
1853 if (*src == ':')
1854 *dest++ = ';';
1855 else
1856 *dest++ = *src;
1857 }
1858 }
1859 len = dest - rsp_buf;
1860 dest = rsp_buf;
1861
1862 /* truncate leading characters */
1863 if (len > MAX_RAID_SERIAL_LEN) {
1864 dest += len - MAX_RAID_SERIAL_LEN;
1865 len = MAX_RAID_SERIAL_LEN;
1866 }
1867
1868 memset(serial, 0, MAX_RAID_SERIAL_LEN);
1869 memcpy(serial, dest, len);
1870
1871 return 0;
1872 }
1873
1874 static int serialcmp(__u8 *s1, __u8 *s2)
1875 {
1876 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
1877 }
1878
1879 static void serialcpy(__u8 *dest, __u8 *src)
1880 {
1881 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
1882 }
1883
1884 #ifndef MDASSEMBLE
1885 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
1886 {
1887 struct dl *dl;
1888
1889 for (dl = super->disks; dl; dl = dl->next)
1890 if (serialcmp(dl->serial, serial) == 0)
1891 break;
1892
1893 return dl;
1894 }
1895 #endif
1896
1897 static struct imsm_disk *
1898 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
1899 {
1900 int i;
1901
1902 for (i = 0; i < mpb->num_disks; i++) {
1903 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
1904
1905 if (serialcmp(disk->serial, serial) == 0) {
1906 if (idx)
1907 *idx = i;
1908 return disk;
1909 }
1910 }
1911
1912 return NULL;
1913 }
1914
1915 static int
1916 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
1917 {
1918 struct imsm_disk *disk;
1919 struct dl *dl;
1920 struct stat stb;
1921 int rv;
1922 char name[40];
1923 __u8 serial[MAX_RAID_SERIAL_LEN];
1924
1925 rv = imsm_read_serial(fd, devname, serial);
1926
1927 if (rv != 0)
1928 return 2;
1929
1930 dl = calloc(1, sizeof(*dl));
1931 if (!dl) {
1932 if (devname)
1933 fprintf(stderr,
1934 Name ": failed to allocate disk buffer for %s\n",
1935 devname);
1936 return 2;
1937 }
1938
1939 fstat(fd, &stb);
1940 dl->major = major(stb.st_rdev);
1941 dl->minor = minor(stb.st_rdev);
1942 dl->next = super->disks;
1943 dl->fd = keep_fd ? fd : -1;
1944 assert(super->disks == NULL);
1945 super->disks = dl;
1946 serialcpy(dl->serial, serial);
1947 dl->index = -2;
1948 dl->e = NULL;
1949 fd2devname(fd, name);
1950 if (devname)
1951 dl->devname = strdup(devname);
1952 else
1953 dl->devname = strdup(name);
1954
1955 /* look up this disk's index in the current anchor */
1956 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
1957 if (disk) {
1958 dl->disk = *disk;
1959 /* only set index on disks that are a member of a
1960 * populated contianer, i.e. one with raid_devs
1961 */
1962 if (is_failed(&dl->disk))
1963 dl->index = -2;
1964 else if (is_spare(&dl->disk))
1965 dl->index = -1;
1966 }
1967
1968 return 0;
1969 }
1970
1971 #ifndef MDASSEMBLE
1972 /* When migrating map0 contains the 'destination' state while map1
1973 * contains the current state. When not migrating map0 contains the
1974 * current state. This routine assumes that map[0].map_state is set to
1975 * the current array state before being called.
1976 *
1977 * Migration is indicated by one of the following states
1978 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
1979 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
1980 * map1state=unitialized)
1981 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
1982 * map1state=normal)
1983 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
1984 * map1state=degraded)
1985 */
1986 static void migrate(struct imsm_dev *dev, __u8 to_state, int migr_type)
1987 {
1988 struct imsm_map *dest;
1989 struct imsm_map *src = get_imsm_map(dev, 0);
1990
1991 dev->vol.migr_state = 1;
1992 set_migr_type(dev, migr_type);
1993 dev->vol.curr_migr_unit = 0;
1994 dest = get_imsm_map(dev, 1);
1995
1996 /* duplicate and then set the target end state in map[0] */
1997 memcpy(dest, src, sizeof_imsm_map(src));
1998 if (migr_type == MIGR_REBUILD) {
1999 __u32 ord;
2000 int i;
2001
2002 for (i = 0; i < src->num_members; i++) {
2003 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
2004 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
2005 }
2006 }
2007
2008 src->map_state = to_state;
2009 }
2010
2011 static void end_migration(struct imsm_dev *dev, __u8 map_state)
2012 {
2013 struct imsm_map *map = get_imsm_map(dev, 0);
2014 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
2015 int i;
2016
2017 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2018 * completed in the last migration.
2019 *
2020 * FIXME add support for online capacity expansion and
2021 * raid-level-migration
2022 */
2023 for (i = 0; i < prev->num_members; i++)
2024 map->disk_ord_tbl[i] |= prev->disk_ord_tbl[i];
2025
2026 dev->vol.migr_state = 0;
2027 dev->vol.curr_migr_unit = 0;
2028 map->map_state = map_state;
2029 }
2030 #endif
2031
2032 static int parse_raid_devices(struct intel_super *super)
2033 {
2034 int i;
2035 struct imsm_dev *dev_new;
2036 size_t len, len_migr;
2037 size_t space_needed = 0;
2038 struct imsm_super *mpb = super->anchor;
2039
2040 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2041 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
2042 struct intel_dev *dv;
2043
2044 len = sizeof_imsm_dev(dev_iter, 0);
2045 len_migr = sizeof_imsm_dev(dev_iter, 1);
2046 if (len_migr > len)
2047 space_needed += len_migr - len;
2048
2049 dv = malloc(sizeof(*dv));
2050 if (!dv)
2051 return 1;
2052 dev_new = malloc(len_migr);
2053 if (!dev_new) {
2054 free(dv);
2055 return 1;
2056 }
2057 imsm_copy_dev(dev_new, dev_iter);
2058 dv->dev = dev_new;
2059 dv->index = i;
2060 dv->next = super->devlist;
2061 super->devlist = dv;
2062 }
2063
2064 /* ensure that super->buf is large enough when all raid devices
2065 * are migrating
2066 */
2067 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
2068 void *buf;
2069
2070 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512);
2071 if (posix_memalign(&buf, 512, len) != 0)
2072 return 1;
2073
2074 memcpy(buf, super->buf, super->len);
2075 memset(buf + super->len, 0, len - super->len);
2076 free(super->buf);
2077 super->buf = buf;
2078 super->len = len;
2079 }
2080
2081 return 0;
2082 }
2083
2084 /* retrieve a pointer to the bbm log which starts after all raid devices */
2085 struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
2086 {
2087 void *ptr = NULL;
2088
2089 if (__le32_to_cpu(mpb->bbm_log_size)) {
2090 ptr = mpb;
2091 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
2092 }
2093
2094 return ptr;
2095 }
2096
2097 static void __free_imsm(struct intel_super *super, int free_disks);
2098
2099 /* load_imsm_mpb - read matrix metadata
2100 * allocates super->mpb to be freed by free_super
2101 */
2102 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
2103 {
2104 unsigned long long dsize;
2105 unsigned long long sectors;
2106 struct stat;
2107 struct imsm_super *anchor;
2108 __u32 check_sum;
2109
2110 get_dev_size(fd, NULL, &dsize);
2111
2112 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
2113 if (devname)
2114 fprintf(stderr,
2115 Name ": Cannot seek to anchor block on %s: %s\n",
2116 devname, strerror(errno));
2117 return 1;
2118 }
2119
2120 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
2121 if (devname)
2122 fprintf(stderr,
2123 Name ": Failed to allocate imsm anchor buffer"
2124 " on %s\n", devname);
2125 return 1;
2126 }
2127 if (read(fd, anchor, 512) != 512) {
2128 if (devname)
2129 fprintf(stderr,
2130 Name ": Cannot read anchor block on %s: %s\n",
2131 devname, strerror(errno));
2132 free(anchor);
2133 return 1;
2134 }
2135
2136 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
2137 if (devname)
2138 fprintf(stderr,
2139 Name ": no IMSM anchor on %s\n", devname);
2140 free(anchor);
2141 return 2;
2142 }
2143
2144 __free_imsm(super, 0);
2145 super->len = ROUND_UP(anchor->mpb_size, 512);
2146 if (posix_memalign(&super->buf, 512, super->len) != 0) {
2147 if (devname)
2148 fprintf(stderr,
2149 Name ": unable to allocate %zu byte mpb buffer\n",
2150 super->len);
2151 free(anchor);
2152 return 2;
2153 }
2154 memcpy(super->buf, anchor, 512);
2155
2156 sectors = mpb_sectors(anchor) - 1;
2157 free(anchor);
2158 if (!sectors) {
2159 check_sum = __gen_imsm_checksum(super->anchor);
2160 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
2161 if (devname)
2162 fprintf(stderr,
2163 Name ": IMSM checksum %x != %x on %s\n",
2164 check_sum,
2165 __le32_to_cpu(super->anchor->check_sum),
2166 devname);
2167 return 2;
2168 }
2169
2170 return 0;
2171 }
2172
2173 /* read the extended mpb */
2174 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
2175 if (devname)
2176 fprintf(stderr,
2177 Name ": Cannot seek to extended mpb on %s: %s\n",
2178 devname, strerror(errno));
2179 return 1;
2180 }
2181
2182 if (read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
2183 if (devname)
2184 fprintf(stderr,
2185 Name ": Cannot read extended mpb on %s: %s\n",
2186 devname, strerror(errno));
2187 return 2;
2188 }
2189
2190 check_sum = __gen_imsm_checksum(super->anchor);
2191 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
2192 if (devname)
2193 fprintf(stderr,
2194 Name ": IMSM checksum %x != %x on %s\n",
2195 check_sum, __le32_to_cpu(super->anchor->check_sum),
2196 devname);
2197 return 3;
2198 }
2199
2200 /* FIXME the BBM log is disk specific so we cannot use this global
2201 * buffer for all disks. Ok for now since we only look at the global
2202 * bbm_log_size parameter to gate assembly
2203 */
2204 super->bbm_log = __get_imsm_bbm_log(super->anchor);
2205
2206 return 0;
2207 }
2208
2209 static int
2210 load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
2211 {
2212 int err;
2213
2214 err = load_imsm_mpb(fd, super, devname);
2215 if (err)
2216 return err;
2217 err = load_imsm_disk(fd, super, devname, keep_fd);
2218 if (err)
2219 return err;
2220 err = parse_raid_devices(super);
2221
2222 return err;
2223 }
2224
2225 static void __free_imsm_disk(struct dl *d)
2226 {
2227 if (d->fd >= 0)
2228 close(d->fd);
2229 if (d->devname)
2230 free(d->devname);
2231 if (d->e)
2232 free(d->e);
2233 free(d);
2234
2235 }
2236 static void free_imsm_disks(struct intel_super *super)
2237 {
2238 struct dl *d;
2239
2240 while (super->disks) {
2241 d = super->disks;
2242 super->disks = d->next;
2243 __free_imsm_disk(d);
2244 }
2245 while (super->missing) {
2246 d = super->missing;
2247 super->missing = d->next;
2248 __free_imsm_disk(d);
2249 }
2250
2251 }
2252
2253 /* free all the pieces hanging off of a super pointer */
2254 static void __free_imsm(struct intel_super *super, int free_disks)
2255 {
2256 if (super->buf) {
2257 free(super->buf);
2258 super->buf = NULL;
2259 }
2260 if (free_disks)
2261 free_imsm_disks(super);
2262 free_devlist(super);
2263 if (super->hba) {
2264 free((void *) super->hba);
2265 super->hba = NULL;
2266 }
2267 }
2268
2269 static void free_imsm(struct intel_super *super)
2270 {
2271 __free_imsm(super, 1);
2272 free(super);
2273 }
2274
2275 static void free_super_imsm(struct supertype *st)
2276 {
2277 struct intel_super *super = st->sb;
2278
2279 if (!super)
2280 return;
2281
2282 free_imsm(super);
2283 st->sb = NULL;
2284 }
2285
2286 static struct intel_super *alloc_super(int creating_imsm)
2287 {
2288 struct intel_super *super = malloc(sizeof(*super));
2289
2290 if (super) {
2291 memset(super, 0, sizeof(*super));
2292 super->creating_imsm = creating_imsm;
2293 super->current_vol = -1;
2294 super->create_offset = ~((__u32 ) 0);
2295 if (!check_env("IMSM_NO_PLATFORM"))
2296 super->orom = find_imsm_orom();
2297 if (super->orom && !check_env("IMSM_TEST_OROM")) {
2298 struct sys_dev *list, *ent;
2299
2300 /* find the first intel ahci controller */
2301 list = find_driver_devices("pci", "ahci");
2302 for (ent = list; ent; ent = ent->next)
2303 if (devpath_to_vendor(ent->path) == 0x8086)
2304 break;
2305 if (ent) {
2306 super->hba = ent->path;
2307 ent->path = NULL;
2308 }
2309 free_sys_dev(&list);
2310 }
2311 }
2312
2313 return super;
2314 }
2315
2316 #ifndef MDASSEMBLE
2317 /* find_missing - helper routine for load_super_imsm_all that identifies
2318 * disks that have disappeared from the system. This routine relies on
2319 * the mpb being uptodate, which it is at load time.
2320 */
2321 static int find_missing(struct intel_super *super)
2322 {
2323 int i;
2324 struct imsm_super *mpb = super->anchor;
2325 struct dl *dl;
2326 struct imsm_disk *disk;
2327
2328 for (i = 0; i < mpb->num_disks; i++) {
2329 disk = __get_imsm_disk(mpb, i);
2330 dl = serial_to_dl(disk->serial, super);
2331 if (dl)
2332 continue;
2333
2334 dl = malloc(sizeof(*dl));
2335 if (!dl)
2336 return 1;
2337 dl->major = 0;
2338 dl->minor = 0;
2339 dl->fd = -1;
2340 dl->devname = strdup("missing");
2341 dl->index = i;
2342 serialcpy(dl->serial, disk->serial);
2343 dl->disk = *disk;
2344 dl->e = NULL;
2345 dl->next = super->missing;
2346 super->missing = dl;
2347 }
2348
2349 return 0;
2350 }
2351
2352 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
2353 {
2354 struct intel_disk *idisk = disk_list;
2355
2356 while (idisk) {
2357 if (serialcmp(idisk->disk.serial, serial) == 0)
2358 break;
2359 idisk = idisk->next;
2360 }
2361
2362 return idisk;
2363 }
2364
2365 static int __prep_thunderdome(struct intel_super **table, int tbl_size,
2366 struct intel_super *super,
2367 struct intel_disk **disk_list)
2368 {
2369 struct imsm_disk *d = &super->disks->disk;
2370 struct imsm_super *mpb = super->anchor;
2371 int i, j;
2372
2373 for (i = 0; i < tbl_size; i++) {
2374 struct imsm_super *tbl_mpb = table[i]->anchor;
2375 struct imsm_disk *tbl_d = &table[i]->disks->disk;
2376
2377 if (tbl_mpb->family_num == mpb->family_num) {
2378 if (tbl_mpb->check_sum == mpb->check_sum) {
2379 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2380 __func__, super->disks->major,
2381 super->disks->minor,
2382 table[i]->disks->major,
2383 table[i]->disks->minor);
2384 break;
2385 }
2386
2387 if (((is_configured(d) && !is_configured(tbl_d)) ||
2388 is_configured(d) == is_configured(tbl_d)) &&
2389 tbl_mpb->generation_num < mpb->generation_num) {
2390 /* current version of the mpb is a
2391 * better candidate than the one in
2392 * super_table, but copy over "cross
2393 * generational" status
2394 */
2395 struct intel_disk *idisk;
2396
2397 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2398 __func__, super->disks->major,
2399 super->disks->minor,
2400 table[i]->disks->major,
2401 table[i]->disks->minor);
2402
2403 idisk = disk_list_get(tbl_d->serial, *disk_list);
2404 if (idisk && is_failed(&idisk->disk))
2405 tbl_d->status |= FAILED_DISK;
2406 break;
2407 } else {
2408 struct intel_disk *idisk;
2409 struct imsm_disk *disk;
2410
2411 /* tbl_mpb is more up to date, but copy
2412 * over cross generational status before
2413 * returning
2414 */
2415 disk = __serial_to_disk(d->serial, mpb, NULL);
2416 if (disk && is_failed(disk))
2417 d->status |= FAILED_DISK;
2418
2419 idisk = disk_list_get(d->serial, *disk_list);
2420 if (idisk) {
2421 idisk->owner = i;
2422 if (disk && is_configured(disk))
2423 idisk->disk.status |= CONFIGURED_DISK;
2424 }
2425
2426 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2427 __func__, super->disks->major,
2428 super->disks->minor,
2429 table[i]->disks->major,
2430 table[i]->disks->minor);
2431
2432 return tbl_size;
2433 }
2434 }
2435 }
2436
2437 if (i >= tbl_size)
2438 table[tbl_size++] = super;
2439 else
2440 table[i] = super;
2441
2442 /* update/extend the merged list of imsm_disk records */
2443 for (j = 0; j < mpb->num_disks; j++) {
2444 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
2445 struct intel_disk *idisk;
2446
2447 idisk = disk_list_get(disk->serial, *disk_list);
2448 if (idisk) {
2449 idisk->disk.status |= disk->status;
2450 if (is_configured(&idisk->disk) ||
2451 is_failed(&idisk->disk))
2452 idisk->disk.status &= ~(SPARE_DISK);
2453 } else {
2454 idisk = calloc(1, sizeof(*idisk));
2455 if (!idisk)
2456 return -1;
2457 idisk->owner = IMSM_UNKNOWN_OWNER;
2458 idisk->disk = *disk;
2459 idisk->next = *disk_list;
2460 *disk_list = idisk;
2461 }
2462
2463 if (serialcmp(idisk->disk.serial, d->serial) == 0)
2464 idisk->owner = i;
2465 }
2466
2467 return tbl_size;
2468 }
2469
2470 static struct intel_super *
2471 validate_members(struct intel_super *super, struct intel_disk *disk_list,
2472 const int owner)
2473 {
2474 struct imsm_super *mpb = super->anchor;
2475 int ok_count = 0;
2476 int i;
2477
2478 for (i = 0; i < mpb->num_disks; i++) {
2479 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
2480 struct intel_disk *idisk;
2481
2482 idisk = disk_list_get(disk->serial, disk_list);
2483 if (idisk) {
2484 if (idisk->owner == owner ||
2485 idisk->owner == IMSM_UNKNOWN_OWNER)
2486 ok_count++;
2487 else
2488 dprintf("%s: '%.16s' owner %d != %d\n",
2489 __func__, disk->serial, idisk->owner,
2490 owner);
2491 } else {
2492 dprintf("%s: unknown disk %x [%d]: %.16s\n",
2493 __func__, __le32_to_cpu(mpb->family_num), i,
2494 disk->serial);
2495 break;
2496 }
2497 }
2498
2499 if (ok_count == mpb->num_disks)
2500 return super;
2501 return NULL;
2502 }
2503
2504 static void show_conflicts(__u32 family_num, struct intel_super *super_list)
2505 {
2506 struct intel_super *s;
2507
2508 for (s = super_list; s; s = s->next) {
2509 if (family_num != s->anchor->family_num)
2510 continue;
2511 fprintf(stderr, "Conflict, offlining family %#x on '%s'\n",
2512 __le32_to_cpu(family_num), s->disks->devname);
2513 }
2514 }
2515
2516 static struct intel_super *
2517 imsm_thunderdome(struct intel_super **super_list, int len)
2518 {
2519 struct intel_super *super_table[len];
2520 struct intel_disk *disk_list = NULL;
2521 struct intel_super *champion, *spare;
2522 struct intel_super *s, **del;
2523 int tbl_size = 0;
2524 int conflict;
2525 int i;
2526
2527 memset(super_table, 0, sizeof(super_table));
2528 for (s = *super_list; s; s = s->next)
2529 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
2530
2531 for (i = 0; i < tbl_size; i++) {
2532 struct imsm_disk *d;
2533 struct intel_disk *idisk;
2534 struct imsm_super *mpb = super_table[i]->anchor;
2535
2536 s = super_table[i];
2537 d = &s->disks->disk;
2538
2539 /* 'd' must appear in merged disk list for its
2540 * configuration to be valid
2541 */
2542 idisk = disk_list_get(d->serial, disk_list);
2543 if (idisk && idisk->owner == i)
2544 s = validate_members(s, disk_list, i);
2545 else
2546 s = NULL;
2547
2548 if (!s)
2549 dprintf("%s: marking family: %#x from %d:%d offline\n",
2550 __func__, mpb->family_num,
2551 super_table[i]->disks->major,
2552 super_table[i]->disks->minor);
2553 super_table[i] = s;
2554 }
2555
2556 /* This is where the mdadm implementation differs from the Windows
2557 * driver which has no strict concept of a container. We can only
2558 * assemble one family from a container, so when returning a prodigal
2559 * array member to this system the code will not be able to disambiguate
2560 * the container contents that should be assembled ("foreign" versus
2561 * "local"). It requires user intervention to set the orig_family_num
2562 * to a new value to establish a new container. The Windows driver in
2563 * this situation fixes up the volume name in place and manages the
2564 * foreign array as an independent entity.
2565 */
2566 s = NULL;
2567 spare = NULL;
2568 conflict = 0;
2569 for (i = 0; i < tbl_size; i++) {
2570 struct intel_super *tbl_ent = super_table[i];
2571 int is_spare = 0;
2572
2573 if (!tbl_ent)
2574 continue;
2575
2576 if (tbl_ent->anchor->num_raid_devs == 0) {
2577 spare = tbl_ent;
2578 is_spare = 1;
2579 }
2580
2581 if (s && !is_spare) {
2582 show_conflicts(tbl_ent->anchor->family_num, *super_list);
2583 conflict++;
2584 } else if (!s && !is_spare)
2585 s = tbl_ent;
2586 }
2587
2588 if (!s)
2589 s = spare;
2590 if (!s) {
2591 champion = NULL;
2592 goto out;
2593 }
2594 champion = s;
2595
2596 if (conflict)
2597 fprintf(stderr, "Chose family %#x on '%s', "
2598 "assemble conflicts to new container with '--update=uuid'\n",
2599 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
2600
2601 /* collect all dl's onto 'champion', and update them to
2602 * champion's version of the status
2603 */
2604 for (s = *super_list; s; s = s->next) {
2605 struct imsm_super *mpb = champion->anchor;
2606 struct dl *dl = s->disks;
2607
2608 if (s == champion)
2609 continue;
2610
2611 for (i = 0; i < mpb->num_disks; i++) {
2612 struct imsm_disk *disk;
2613
2614 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
2615 if (disk) {
2616 dl->disk = *disk;
2617 /* only set index on disks that are a member of
2618 * a populated contianer, i.e. one with
2619 * raid_devs
2620 */
2621 if (is_failed(&dl->disk))
2622 dl->index = -2;
2623 else if (is_spare(&dl->disk))
2624 dl->index = -1;
2625 break;
2626 }
2627 }
2628
2629 if (i >= mpb->num_disks) {
2630 struct intel_disk *idisk;
2631
2632 idisk = disk_list_get(dl->serial, disk_list);
2633 if (idisk && is_spare(&idisk->disk) &&
2634 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
2635 dl->index = -1;
2636 else {
2637 dl->index = -2;
2638 continue;
2639 }
2640 }
2641
2642 dl->next = champion->disks;
2643 champion->disks = dl;
2644 s->disks = NULL;
2645 }
2646
2647 /* delete 'champion' from super_list */
2648 for (del = super_list; *del; ) {
2649 if (*del == champion) {
2650 *del = (*del)->next;
2651 break;
2652 } else
2653 del = &(*del)->next;
2654 }
2655 champion->next = NULL;
2656
2657 out:
2658 while (disk_list) {
2659 struct intel_disk *idisk = disk_list;
2660
2661 disk_list = disk_list->next;
2662 free(idisk);
2663 }
2664
2665 return champion;
2666 }
2667
2668 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
2669 char *devname, int keep_fd)
2670 {
2671 struct mdinfo *sra;
2672 struct intel_super *super_list = NULL;
2673 struct intel_super *super = NULL;
2674 int devnum = fd2devnum(fd);
2675 struct mdinfo *sd;
2676 int retry;
2677 int err = 0;
2678 int i;
2679 enum sysfs_read_flags flags;
2680
2681 flags = GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE;
2682 if (mdmon_running(devnum))
2683 flags |= SKIP_GONE_DEVS;
2684
2685 /* check if 'fd' an opened container */
2686 sra = sysfs_read(fd, 0, flags);
2687 if (!sra)
2688 return 1;
2689
2690 if (sra->array.major_version != -1 ||
2691 sra->array.minor_version != -2 ||
2692 strcmp(sra->text_version, "imsm") != 0)
2693 return 1;
2694
2695 /* load all mpbs */
2696 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
2697 struct intel_super *s = alloc_super(0);
2698 char nm[20];
2699 int dfd;
2700
2701 err = 1;
2702 if (!s)
2703 goto error;
2704 s->next = super_list;
2705 super_list = s;
2706
2707 err = 2;
2708 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2709 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
2710 if (dfd < 0)
2711 goto error;
2712
2713 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
2714
2715 /* retry the load if we might have raced against mdmon */
2716 if (err == 3 && mdmon_running(devnum))
2717 for (retry = 0; retry < 3; retry++) {
2718 usleep(3000);
2719 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
2720 if (err != 3)
2721 break;
2722 }
2723 if (!keep_fd)
2724 close(dfd);
2725 if (err)
2726 goto error;
2727 }
2728
2729 /* all mpbs enter, maybe one leaves */
2730 super = imsm_thunderdome(&super_list, i);
2731 if (!super) {
2732 err = 1;
2733 goto error;
2734 }
2735
2736 if (find_missing(super) != 0) {
2737 free_imsm(super);
2738 err = 2;
2739 goto error;
2740 }
2741
2742 if (st->subarray[0]) {
2743 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
2744 super->current_vol = atoi(st->subarray);
2745 else {
2746 free_imsm(super);
2747 err = 1;
2748 goto error;
2749 }
2750 }
2751 err = 0;
2752
2753 error:
2754 while (super_list) {
2755 struct intel_super *s = super_list;
2756
2757 super_list = super_list->next;
2758 free_imsm(s);
2759 }
2760
2761 if (err)
2762 return err;
2763
2764 *sbp = super;
2765 st->container_dev = devnum;
2766 if (err == 0 && st->ss == NULL) {
2767 st->ss = &super_imsm;
2768 st->minor_version = 0;
2769 st->max_devs = IMSM_MAX_DEVICES;
2770 }
2771 st->loaded_container = 1;
2772
2773 return 0;
2774 }
2775 #endif
2776
2777 static int load_super_imsm(struct supertype *st, int fd, char *devname)
2778 {
2779 struct intel_super *super;
2780 int rv;
2781
2782 #ifndef MDASSEMBLE
2783 if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0)
2784 return 0;
2785 #endif
2786
2787 free_super_imsm(st);
2788
2789 super = alloc_super(0);
2790 if (!super) {
2791 fprintf(stderr,
2792 Name ": malloc of %zu failed.\n",
2793 sizeof(*super));
2794 return 1;
2795 }
2796
2797 rv = load_and_parse_mpb(fd, super, devname, 0);
2798
2799 if (rv) {
2800 if (devname)
2801 fprintf(stderr,
2802 Name ": Failed to load all information "
2803 "sections on %s\n", devname);
2804 free_imsm(super);
2805 return rv;
2806 }
2807
2808 if (st->subarray[0]) {
2809 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
2810 super->current_vol = atoi(st->subarray);
2811 else {
2812 free_imsm(super);
2813 return 1;
2814 }
2815 }
2816
2817 st->sb = super;
2818 if (st->ss == NULL) {
2819 st->ss = &super_imsm;
2820 st->minor_version = 0;
2821 st->max_devs = IMSM_MAX_DEVICES;
2822 }
2823 st->loaded_container = 0;
2824
2825 return 0;
2826 }
2827
2828 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
2829 {
2830 if (info->level == 1)
2831 return 128;
2832 return info->chunk_size >> 9;
2833 }
2834
2835 static __u32 info_to_num_data_stripes(mdu_array_info_t *info, int num_domains)
2836 {
2837 __u32 num_stripes;
2838
2839 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
2840 num_stripes /= num_domains;
2841
2842 return num_stripes;
2843 }
2844
2845 static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
2846 {
2847 if (info->level == 1)
2848 return info->size * 2;
2849 else
2850 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
2851 }
2852
2853 static void imsm_update_version_info(struct intel_super *super)
2854 {
2855 /* update the version and attributes */
2856 struct imsm_super *mpb = super->anchor;
2857 char *version;
2858 struct imsm_dev *dev;
2859 struct imsm_map *map;
2860 int i;
2861
2862 for (i = 0; i < mpb->num_raid_devs; i++) {
2863 dev = get_imsm_dev(super, i);
2864 map = get_imsm_map(dev, 0);
2865 if (__le32_to_cpu(dev->size_high) > 0)
2866 mpb->attributes |= MPB_ATTRIB_2TB;
2867
2868 /* FIXME detect when an array spans a port multiplier */
2869 #if 0
2870 mpb->attributes |= MPB_ATTRIB_PM;
2871 #endif
2872
2873 if (mpb->num_raid_devs > 1 ||
2874 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
2875 version = MPB_VERSION_ATTRIBS;
2876 switch (get_imsm_raid_level(map)) {
2877 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
2878 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
2879 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
2880 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
2881 }
2882 } else {
2883 if (map->num_members >= 5)
2884 version = MPB_VERSION_5OR6_DISK_ARRAY;
2885 else if (dev->status == DEV_CLONE_N_GO)
2886 version = MPB_VERSION_CNG;
2887 else if (get_imsm_raid_level(map) == 5)
2888 version = MPB_VERSION_RAID5;
2889 else if (map->num_members >= 3)
2890 version = MPB_VERSION_3OR4_DISK_ARRAY;
2891 else if (get_imsm_raid_level(map) == 1)
2892 version = MPB_VERSION_RAID1;
2893 else
2894 version = MPB_VERSION_RAID0;
2895 }
2896 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
2897 }
2898 }
2899
2900 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
2901 unsigned long long size, char *name,
2902 char *homehost, int *uuid)
2903 {
2904 /* We are creating a volume inside a pre-existing container.
2905 * so st->sb is already set.
2906 */
2907 struct intel_super *super = st->sb;
2908 struct imsm_super *mpb = super->anchor;
2909 struct intel_dev *dv;
2910 struct imsm_dev *dev;
2911 struct imsm_vol *vol;
2912 struct imsm_map *map;
2913 int idx = mpb->num_raid_devs;
2914 int i;
2915 unsigned long long array_blocks;
2916 size_t size_old, size_new;
2917 __u32 num_data_stripes;
2918
2919 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
2920 fprintf(stderr, Name": This imsm-container already has the "
2921 "maximum of %d volumes\n", super->orom->vpa);
2922 return 0;
2923 }
2924
2925 /* ensure the mpb is large enough for the new data */
2926 size_old = __le32_to_cpu(mpb->mpb_size);
2927 size_new = disks_to_mpb_size(info->nr_disks);
2928 if (size_new > size_old) {
2929 void *mpb_new;
2930 size_t size_round = ROUND_UP(size_new, 512);
2931
2932 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
2933 fprintf(stderr, Name": could not allocate new mpb\n");
2934 return 0;
2935 }
2936 memcpy(mpb_new, mpb, size_old);
2937 free(mpb);
2938 mpb = mpb_new;
2939 super->anchor = mpb_new;
2940 mpb->mpb_size = __cpu_to_le32(size_new);
2941 memset(mpb_new + size_old, 0, size_round - size_old);
2942 }
2943 super->current_vol = idx;
2944 /* when creating the first raid device in this container set num_disks
2945 * to zero, i.e. delete this spare and add raid member devices in
2946 * add_to_super_imsm_volume()
2947 */
2948 if (super->current_vol == 0)
2949 mpb->num_disks = 0;
2950
2951 for (i = 0; i < super->current_vol; i++) {
2952 dev = get_imsm_dev(super, i);
2953 if (strncmp((char *) dev->volume, name,
2954 MAX_RAID_SERIAL_LEN) == 0) {
2955 fprintf(stderr, Name": '%s' is already defined for this container\n",
2956 name);
2957 return 0;
2958 }
2959 }
2960
2961 sprintf(st->subarray, "%d", idx);
2962 dv = malloc(sizeof(*dv));
2963 if (!dv) {
2964 fprintf(stderr, Name ": failed to allocate device list entry\n");
2965 return 0;
2966 }
2967 dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
2968 if (!dev) {
2969 free(dv);
2970 fprintf(stderr, Name": could not allocate raid device\n");
2971 return 0;
2972 }
2973 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
2974 if (info->level == 1)
2975 array_blocks = info_to_blocks_per_member(info);
2976 else
2977 array_blocks = calc_array_size(info->level, info->raid_disks,
2978 info->layout, info->chunk_size,
2979 info->size*2);
2980 /* round array size down to closest MB */
2981 array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
2982
2983 dev->size_low = __cpu_to_le32((__u32) array_blocks);
2984 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
2985 dev->status = __cpu_to_le32(0);
2986 dev->reserved_blocks = __cpu_to_le32(0);
2987 vol = &dev->vol;
2988 vol->migr_state = 0;
2989 set_migr_type(dev, MIGR_INIT);
2990 vol->dirty = 0;
2991 vol->curr_migr_unit = 0;
2992 map = get_imsm_map(dev, 0);
2993 map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
2994 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
2995 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
2996 map->failed_disk_num = ~0;
2997 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
2998 IMSM_T_STATE_NORMAL;
2999 map->ddf = 1;
3000
3001 if (info->level == 1 && info->raid_disks > 2) {
3002 fprintf(stderr, Name": imsm does not support more than 2 disks"
3003 "in a raid1 volume\n");
3004 return 0;
3005 }
3006
3007 map->raid_level = info->level;
3008 if (info->level == 10) {
3009 map->raid_level = 1;
3010 map->num_domains = info->raid_disks / 2;
3011 } else if (info->level == 1)
3012 map->num_domains = info->raid_disks;
3013 else
3014 map->num_domains = 1;
3015
3016 num_data_stripes = info_to_num_data_stripes(info, map->num_domains);
3017 map->num_data_stripes = __cpu_to_le32(num_data_stripes);
3018
3019 map->num_members = info->raid_disks;
3020 for (i = 0; i < map->num_members; i++) {
3021 /* initialized in add_to_super */
3022 set_imsm_ord_tbl_ent(map, i, 0);
3023 }
3024 mpb->num_raid_devs++;
3025
3026 dv->dev = dev;
3027 dv->index = super->current_vol;
3028 dv->next = super->devlist;
3029 super->devlist = dv;
3030
3031 imsm_update_version_info(super);
3032
3033 return 1;
3034 }
3035
3036 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
3037 unsigned long long size, char *name,
3038 char *homehost, int *uuid)
3039 {
3040 /* This is primarily called by Create when creating a new array.
3041 * We will then get add_to_super called for each component, and then
3042 * write_init_super called to write it out to each device.
3043 * For IMSM, Create can create on fresh devices or on a pre-existing
3044 * array.
3045 * To create on a pre-existing array a different method will be called.
3046 * This one is just for fresh drives.
3047 */
3048 struct intel_super *super;
3049 struct imsm_super *mpb;
3050 size_t mpb_size;
3051 char *version;
3052
3053 if (st->sb)
3054 return init_super_imsm_volume(st, info, size, name, homehost, uuid);
3055
3056 if (info)
3057 mpb_size = disks_to_mpb_size(info->nr_disks);
3058 else
3059 mpb_size = 512;
3060
3061 super = alloc_super(1);
3062 if (super && posix_memalign(&super->buf, 512, mpb_size) != 0) {
3063 free(super);
3064 super = NULL;
3065 }
3066 if (!super) {
3067 fprintf(stderr, Name
3068 ": %s could not allocate superblock\n", __func__);
3069 return 0;
3070 }
3071 memset(super->buf, 0, mpb_size);
3072 mpb = super->buf;
3073 mpb->mpb_size = __cpu_to_le32(mpb_size);
3074 st->sb = super;
3075
3076 if (info == NULL) {
3077 /* zeroing superblock */
3078 return 0;
3079 }
3080
3081 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
3082
3083 version = (char *) mpb->sig;
3084 strcpy(version, MPB_SIGNATURE);
3085 version += strlen(MPB_SIGNATURE);
3086 strcpy(version, MPB_VERSION_RAID0);
3087
3088 return 1;
3089 }
3090
3091 #ifndef MDASSEMBLE
3092 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
3093 int fd, char *devname)
3094 {
3095 struct intel_super *super = st->sb;
3096 struct imsm_super *mpb = super->anchor;
3097 struct dl *dl;
3098 struct imsm_dev *dev;
3099 struct imsm_map *map;
3100
3101 dev = get_imsm_dev(super, super->current_vol);
3102 map = get_imsm_map(dev, 0);
3103
3104 if (! (dk->state & (1<<MD_DISK_SYNC))) {
3105 fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
3106 devname);
3107 return 1;
3108 }
3109
3110 if (fd == -1) {
3111 /* we're doing autolayout so grab the pre-marked (in
3112 * validate_geometry) raid_disk
3113 */
3114 for (dl = super->disks; dl; dl = dl->next)
3115 if (dl->raiddisk == dk->raid_disk)
3116 break;
3117 } else {
3118 for (dl = super->disks; dl ; dl = dl->next)
3119 if (dl->major == dk->major &&
3120 dl->minor == dk->minor)
3121 break;
3122 }
3123
3124 if (!dl) {
3125 fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
3126 return 1;
3127 }
3128
3129 /* add a pristine spare to the metadata */
3130 if (dl->index < 0) {
3131 dl->index = super->anchor->num_disks;
3132 super->anchor->num_disks++;
3133 }
3134 set_imsm_ord_tbl_ent(map, dk->number, dl->index);
3135 dl->disk.status = CONFIGURED_DISK;
3136
3137 /* if we are creating the first raid device update the family number */
3138 if (super->current_vol == 0) {
3139 __u32 sum;
3140 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
3141 struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
3142
3143 *_dev = *dev;
3144 *_disk = dl->disk;
3145 sum = random32();
3146 sum += __gen_imsm_checksum(mpb);
3147 mpb->family_num = __cpu_to_le32(sum);
3148 mpb->orig_family_num = mpb->family_num;
3149 }
3150
3151 return 0;
3152 }
3153
3154 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
3155 int fd, char *devname)
3156 {
3157 struct intel_super *super = st->sb;
3158 struct dl *dd;
3159 unsigned long long size;
3160 __u32 id;
3161 int rv;
3162 struct stat stb;
3163
3164 /* if we are on an RAID enabled platform check that the disk is
3165 * attached to the raid controller
3166 */
3167 if (super->hba && !disk_attached_to_hba(fd, super->hba)) {
3168 fprintf(stderr,
3169 Name ": %s is not attached to the raid controller: %s\n",
3170 devname ? : "disk", super->hba);
3171 return 1;
3172 }
3173
3174 if (super->current_vol >= 0)
3175 return add_to_super_imsm_volume(st, dk, fd, devname);
3176
3177 fstat(fd, &stb);
3178 dd = malloc(sizeof(*dd));
3179 if (!dd) {
3180 fprintf(stderr,
3181 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
3182 return 1;
3183 }
3184 memset(dd, 0, sizeof(*dd));
3185 dd->major = major(stb.st_rdev);
3186 dd->minor = minor(stb.st_rdev);
3187 dd->index = -1;
3188 dd->devname = devname ? strdup(devname) : NULL;
3189 dd->fd = fd;
3190 dd->e = NULL;
3191 rv = imsm_read_serial(fd, devname, dd->serial);
3192 if (rv) {
3193 fprintf(stderr,
3194 Name ": failed to retrieve scsi serial, aborting\n");
3195 free(dd);
3196 abort();
3197 }
3198
3199 get_dev_size(fd, NULL, &size);
3200 size /= 512;
3201 serialcpy(dd->disk.serial, dd->serial);
3202 dd->disk.total_blocks = __cpu_to_le32(size);
3203 dd->disk.status = SPARE_DISK;
3204 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
3205 dd->disk.scsi_id = __cpu_to_le32(id);
3206 else
3207 dd->disk.scsi_id = __cpu_to_le32(0);
3208
3209 if (st->update_tail) {
3210 dd->next = super->add;
3211 super->add = dd;
3212 } else {
3213 dd->next = super->disks;
3214 super->disks = dd;
3215 }
3216
3217 return 0;
3218 }
3219
3220 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
3221
3222 static union {
3223 char buf[512];
3224 struct imsm_super anchor;
3225 } spare_record __attribute__ ((aligned(512)));
3226
3227 /* spare records have their own family number and do not have any defined raid
3228 * devices
3229 */
3230 static int write_super_imsm_spares(struct intel_super *super, int doclose)
3231 {
3232 struct imsm_super *mpb = super->anchor;
3233 struct imsm_super *spare = &spare_record.anchor;
3234 __u32 sum;
3235 struct dl *d;
3236
3237 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)),
3238 spare->generation_num = __cpu_to_le32(1UL),
3239 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
3240 spare->num_disks = 1,
3241 spare->num_raid_devs = 0,
3242 spare->cache_size = mpb->cache_size,
3243 spare->pwr_cycle_count = __cpu_to_le32(1),
3244
3245 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
3246 MPB_SIGNATURE MPB_VERSION_RAID0);
3247
3248 for (d = super->disks; d; d = d->next) {
3249 if (d->index != -1)
3250 continue;
3251
3252 spare->disk[0] = d->disk;
3253 sum = __gen_imsm_checksum(spare);
3254 spare->family_num = __cpu_to_le32(sum);
3255 spare->orig_family_num = 0;
3256 sum = __gen_imsm_checksum(spare);
3257 spare->check_sum = __cpu_to_le32(sum);
3258
3259 if (store_imsm_mpb(d->fd, spare)) {
3260 fprintf(stderr, "%s: failed for device %d:%d %s\n",
3261 __func__, d->major, d->minor, strerror(errno));
3262 return 1;
3263 }
3264 if (doclose) {
3265 close(d->fd);
3266 d->fd = -1;
3267 }
3268 }
3269
3270 return 0;
3271 }
3272
3273 static int write_super_imsm(struct intel_super *super, int doclose)
3274 {
3275 struct imsm_super *mpb = super->anchor;
3276 struct dl *d;
3277 __u32 generation;
3278 __u32 sum;
3279 int spares = 0;
3280 int i;
3281 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
3282
3283 /* 'generation' is incremented everytime the metadata is written */
3284 generation = __le32_to_cpu(mpb->generation_num);
3285 generation++;
3286 mpb->generation_num = __cpu_to_le32(generation);
3287
3288 /* fix up cases where previous mdadm releases failed to set
3289 * orig_family_num
3290 */
3291 if (mpb->orig_family_num == 0)
3292 mpb->orig_family_num = mpb->family_num;
3293
3294 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
3295 for (d = super->disks; d; d = d->next) {
3296 if (d->index == -1)
3297 spares++;
3298 else
3299 mpb->disk[d->index] = d->disk;
3300 }
3301 for (d = super->missing; d; d = d->next)
3302 mpb->disk[d->index] = d->disk;
3303
3304 for (i = 0; i < mpb->num_raid_devs; i++) {
3305 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
3306
3307 imsm_copy_dev(dev, get_imsm_dev(super, i));
3308 mpb_size += sizeof_imsm_dev(dev, 0);
3309 }
3310 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
3311 mpb->mpb_size = __cpu_to_le32(mpb_size);
3312
3313 /* recalculate checksum */
3314 sum = __gen_imsm_checksum(mpb);
3315 mpb->check_sum = __cpu_to_le32(sum);
3316
3317 /* write the mpb for disks that compose raid devices */
3318 for (d = super->disks; d ; d = d->next) {
3319 if (d->index < 0)
3320 continue;
3321 if (store_imsm_mpb(d->fd, mpb))
3322 fprintf(stderr, "%s: failed for device %d:%d %s\n",
3323 __func__, d->major, d->minor, strerror(errno));
3324 if (doclose) {
3325 close(d->fd);
3326 d->fd = -1;
3327 }
3328 }
3329
3330 if (spares)
3331 return write_super_imsm_spares(super, doclose);
3332
3333 return 0;
3334 }
3335
3336
3337 static int create_array(struct supertype *st, int dev_idx)
3338 {
3339 size_t len;
3340 struct imsm_update_create_array *u;
3341 struct intel_super *super = st->sb;
3342 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3343 struct imsm_map *map = get_imsm_map(dev, 0);
3344 struct disk_info *inf;
3345 struct imsm_disk *disk;
3346 int i;
3347
3348 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
3349 sizeof(*inf) * map->num_members;
3350 u = malloc(len);
3351 if (!u) {
3352 fprintf(stderr, "%s: failed to allocate update buffer\n",
3353 __func__);
3354 return 1;
3355 }
3356
3357 u->type = update_create_array;
3358 u->dev_idx = dev_idx;
3359 imsm_copy_dev(&u->dev, dev);
3360 inf = get_disk_info(u);
3361 for (i = 0; i < map->num_members; i++) {
3362 int idx = get_imsm_disk_idx(dev, i);
3363
3364 disk = get_imsm_disk(super, idx);
3365 serialcpy(inf[i].serial, disk->serial);
3366 }
3367 append_metadata_update(st, u, len);
3368
3369 return 0;
3370 }
3371
3372 static int _add_disk(struct supertype *st)
3373 {
3374 struct intel_super *super = st->sb;
3375 size_t len;
3376 struct imsm_update_add_disk *u;
3377
3378 if (!super->add)
3379 return 0;
3380
3381 len = sizeof(*u);
3382 u = malloc(len);
3383 if (!u) {
3384 fprintf(stderr, "%s: failed to allocate update buffer\n",
3385 __func__);
3386 return 1;
3387 }
3388
3389 u->type = update_add_disk;
3390 append_metadata_update(st, u, len);
3391
3392 return 0;
3393 }
3394
3395 static int write_init_super_imsm(struct supertype *st)
3396 {
3397 struct intel_super *super = st->sb;
3398 int current_vol = super->current_vol;
3399
3400 /* we are done with current_vol reset it to point st at the container */
3401 super->current_vol = -1;
3402
3403 if (st->update_tail) {
3404 /* queue the recently created array / added disk
3405 * as a metadata update */
3406 struct dl *d;
3407 int rv;
3408
3409 /* determine if we are creating a volume or adding a disk */
3410 if (current_vol < 0) {
3411 /* in the add disk case we are running in mdmon
3412 * context, so don't close fd's
3413 */
3414 return _add_disk(st);
3415 } else
3416 rv = create_array(st, current_vol);
3417
3418 for (d = super->disks; d ; d = d->next) {
3419 close(d->fd);
3420 d->fd = -1;
3421 }
3422
3423 return rv;
3424 } else
3425 return write_super_imsm(st->sb, 1);
3426 }
3427 #endif
3428
3429 static int store_super_imsm(struct supertype *st, int fd)
3430 {
3431 struct intel_super *super = st->sb;
3432 struct imsm_super *mpb = super ? super->anchor : NULL;
3433
3434 if (!mpb)
3435 return 1;
3436
3437 #ifndef MDASSEMBLE
3438 return store_imsm_mpb(fd, mpb);
3439 #else
3440 return 1;
3441 #endif
3442 }
3443
3444 static int imsm_bbm_log_size(struct imsm_super *mpb)
3445 {
3446 return __le32_to_cpu(mpb->bbm_log_size);
3447 }
3448
3449 #ifndef MDASSEMBLE
3450 static int validate_geometry_imsm_container(struct supertype *st, int level,
3451 int layout, int raiddisks, int chunk,
3452 unsigned long long size, char *dev,
3453 unsigned long long *freesize,
3454 int verbose)
3455 {
3456 int fd;
3457 unsigned long long ldsize;
3458 const struct imsm_orom *orom;
3459
3460 if (level != LEVEL_CONTAINER)
3461 return 0;
3462 if (!dev)
3463 return 1;
3464
3465 if (check_env("IMSM_NO_PLATFORM"))
3466 orom = NULL;
3467 else
3468 orom = find_imsm_orom();
3469 if (orom && raiddisks > orom->tds) {
3470 if (verbose)
3471 fprintf(stderr, Name ": %d exceeds maximum number of"
3472 " platform supported disks: %d\n",
3473 raiddisks, orom->tds);
3474 return 0;
3475 }
3476
3477 fd = open(dev, O_RDONLY|O_EXCL, 0);
3478 if (fd < 0) {
3479 if (verbose)
3480 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
3481 dev, strerror(errno));
3482 return 0;
3483 }
3484 if (!get_dev_size(fd, dev, &ldsize)) {
3485 close(fd);
3486 return 0;
3487 }
3488 close(fd);
3489
3490 *freesize = avail_size_imsm(st, ldsize >> 9);
3491
3492 return 1;
3493 }
3494
3495 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
3496 {
3497 const unsigned long long base_start = e[*idx].start;
3498 unsigned long long end = base_start + e[*idx].size;
3499 int i;
3500
3501 if (base_start == end)
3502 return 0;
3503
3504 *idx = *idx + 1;
3505 for (i = *idx; i < num_extents; i++) {
3506 /* extend overlapping extents */
3507 if (e[i].start >= base_start &&
3508 e[i].start <= end) {
3509 if (e[i].size == 0)
3510 return 0;
3511 if (e[i].start + e[i].size > end)
3512 end = e[i].start + e[i].size;
3513 } else if (e[i].start > end) {
3514 *idx = i;
3515 break;
3516 }
3517 }
3518
3519 return end - base_start;
3520 }
3521
3522 static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
3523 {
3524 /* build a composite disk with all known extents and generate a new
3525 * 'maxsize' given the "all disks in an array must share a common start
3526 * offset" constraint
3527 */
3528 struct extent *e = calloc(sum_extents, sizeof(*e));
3529 struct dl *dl;
3530 int i, j;
3531 int start_extent;
3532 unsigned long long pos;
3533 unsigned long long start = 0;
3534 unsigned long long maxsize;
3535 unsigned long reserve;
3536
3537 if (!e)
3538 return 0;
3539
3540 /* coalesce and sort all extents. also, check to see if we need to
3541 * reserve space between member arrays
3542 */
3543 j = 0;
3544 for (dl = super->disks; dl; dl = dl->next) {
3545 if (!dl->e)
3546 continue;
3547 for (i = 0; i < dl->extent_cnt; i++)
3548 e[j++] = dl->e[i];
3549 }
3550 qsort(e, sum_extents, sizeof(*e), cmp_extent);
3551
3552 /* merge extents */
3553 i = 0;
3554 j = 0;
3555 while (i < sum_extents) {
3556 e[j].start = e[i].start;
3557 e[j].size = find_size(e, &i, sum_extents);
3558 j++;
3559 if (e[j-1].size == 0)
3560 break;
3561 }
3562
3563 pos = 0;
3564 maxsize = 0;
3565 start_extent = 0;
3566 i = 0;
3567 do {
3568 unsigned long long esize;
3569
3570 esize = e[i].start - pos;
3571 if (esize >= maxsize) {
3572 maxsize = esize;
3573 start = pos;
3574 start_extent = i;
3575 }
3576 pos = e[i].start + e[i].size;
3577 i++;
3578 } while (e[i-1].size);
3579 free(e);
3580
3581 if (maxsize == 0)
3582 return 0;
3583
3584 /* FIXME assumes volume at offset 0 is the first volume in a
3585 * container
3586 */
3587 if (start_extent > 0)
3588 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
3589 else
3590 reserve = 0;
3591
3592 if (maxsize < reserve)
3593 return 0;
3594
3595 super->create_offset = ~((__u32) 0);
3596 if (start + reserve > super->create_offset)
3597 return 0; /* start overflows create_offset */
3598 super->create_offset = start + reserve;
3599
3600 return maxsize - reserve;
3601 }
3602
3603 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
3604 {
3605 if (level < 0 || level == 6 || level == 4)
3606 return 0;
3607
3608 /* if we have an orom prevent invalid raid levels */
3609 if (orom)
3610 switch (level) {
3611 case 0: return imsm_orom_has_raid0(orom);
3612 case 1:
3613 if (raiddisks > 2)
3614 return imsm_orom_has_raid1e(orom);
3615 return imsm_orom_has_raid1(orom) && raiddisks == 2;
3616 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
3617 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
3618 }
3619 else
3620 return 1; /* not on an Intel RAID platform so anything goes */
3621
3622 return 0;
3623 }
3624
3625 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
3626 static int
3627 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
3628 int raiddisks, int chunk, int verbose)
3629 {
3630 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
3631 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3632 level, raiddisks, raiddisks > 1 ? "s" : "");
3633 return 0;
3634 }
3635 if (super->orom && level != 1 &&
3636 !imsm_orom_has_chunk(super->orom, chunk)) {
3637 pr_vrb(": platform does not support a chunk size of: %d\n", chunk);
3638 return 0;
3639 }
3640 if (layout != imsm_level_to_layout(level)) {
3641 if (level == 5)
3642 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
3643 else if (level == 10)
3644 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
3645 else
3646 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
3647 layout, level);
3648 return 0;
3649 }
3650
3651 return 1;
3652 }
3653
3654 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
3655 * FIX ME add ahci details
3656 */
3657 static int validate_geometry_imsm_volume(struct supertype *st, int level,
3658 int layout, int raiddisks, int chunk,
3659 unsigned long long size, char *dev,
3660 unsigned long long *freesize,
3661 int verbose)
3662 {
3663 struct stat stb;
3664 struct intel_super *super = st->sb;
3665 struct imsm_super *mpb = super->anchor;
3666 struct dl *dl;
3667 unsigned long long pos = 0;
3668 unsigned long long maxsize;
3669 struct extent *e;
3670 int i;
3671
3672 /* We must have the container info already read in. */
3673 if (!super)
3674 return 0;
3675
3676 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, verbose))
3677 return 0;
3678
3679 if (!dev) {
3680 /* General test: make sure there is space for
3681 * 'raiddisks' device extents of size 'size' at a given
3682 * offset
3683 */
3684 unsigned long long minsize = size;
3685 unsigned long long start_offset = MaxSector;
3686 int dcnt = 0;
3687 if (minsize == 0)
3688 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
3689 for (dl = super->disks; dl ; dl = dl->next) {
3690 int found = 0;
3691
3692 pos = 0;
3693 i = 0;
3694 e = get_extents(super, dl);
3695 if (!e) continue;
3696 do {
3697 unsigned long long esize;
3698 esize = e[i].start - pos;
3699 if (esize >= minsize)
3700 found = 1;
3701 if (found && start_offset == MaxSector) {
3702 start_offset = pos;
3703 break;
3704 } else if (found && pos != start_offset) {
3705 found = 0;
3706 break;
3707 }
3708 pos = e[i].start + e[i].size;
3709 i++;
3710 } while (e[i-1].size);
3711 if (found)
3712 dcnt++;
3713 free(e);
3714 }
3715 if (dcnt < raiddisks) {
3716 if (verbose)
3717 fprintf(stderr, Name ": imsm: Not enough "
3718 "devices with space for this array "
3719 "(%d < %d)\n",
3720 dcnt, raiddisks);
3721 return 0;
3722 }
3723 return 1;
3724 }
3725
3726 /* This device must be a member of the set */
3727 if (stat(dev, &stb) < 0)
3728 return 0;
3729 if ((S_IFMT & stb.st_mode) != S_IFBLK)
3730 return 0;
3731 for (dl = super->disks ; dl ; dl = dl->next) {
3732 if (dl->major == major(stb.st_rdev) &&
3733 dl->minor == minor(stb.st_rdev))
3734 break;
3735 }
3736 if (!dl) {
3737 if (verbose)
3738 fprintf(stderr, Name ": %s is not in the "
3739 "same imsm set\n", dev);
3740 return 0;
3741 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
3742 /* If a volume is present then the current creation attempt
3743 * cannot incorporate new spares because the orom may not
3744 * understand this configuration (all member disks must be
3745 * members of each array in the container).
3746 */
3747 fprintf(stderr, Name ": %s is a spare and a volume"
3748 " is already defined for this container\n", dev);
3749 fprintf(stderr, Name ": The option-rom requires all member"
3750 " disks to be a member of all volumes\n");
3751 return 0;
3752 }
3753
3754 /* retrieve the largest free space block */
3755 e = get_extents(super, dl);
3756 maxsize = 0;
3757 i = 0;
3758 if (e) {
3759 do {
3760 unsigned long long esize;
3761
3762 esize = e[i].start - pos;
3763 if (esize >= maxsize)
3764 maxsize = esize;
3765 pos = e[i].start + e[i].size;
3766 i++;
3767 } while (e[i-1].size);
3768 dl->e = e;
3769 dl->extent_cnt = i;
3770 } else {
3771 if (verbose)
3772 fprintf(stderr, Name ": unable to determine free space for: %s\n",
3773 dev);
3774 return 0;
3775 }
3776 if (maxsize < size) {
3777 if (verbose)
3778 fprintf(stderr, Name ": %s not enough space (%llu < %llu)\n",
3779 dev, maxsize, size);
3780 return 0;
3781 }
3782
3783 /* count total number of extents for merge */
3784 i = 0;
3785 for (dl = super->disks; dl; dl = dl->next)
3786 if (dl->e)
3787 i += dl->extent_cnt;
3788
3789 maxsize = merge_extents(super, i);
3790 if (maxsize < size || maxsize == 0) {
3791 if (verbose)
3792 fprintf(stderr, Name ": not enough space after merge (%llu < %llu)\n",
3793 maxsize, size);
3794 return 0;
3795 }
3796
3797 *freesize = maxsize;
3798
3799 return 1;
3800 }
3801
3802 static int reserve_space(struct supertype *st, int raiddisks,
3803 unsigned long long size, int chunk,
3804 unsigned long long *freesize)
3805 {
3806 struct intel_super *super = st->sb;
3807 struct imsm_super *mpb = super->anchor;
3808 struct dl *dl;
3809 int i;
3810 int extent_cnt;
3811 struct extent *e;
3812 unsigned long long maxsize;
3813 unsigned long long minsize;
3814 int cnt;
3815 int used;
3816
3817 /* find the largest common start free region of the possible disks */
3818 used = 0;
3819 extent_cnt = 0;
3820 cnt = 0;
3821 for (dl = super->disks; dl; dl = dl->next) {
3822 dl->raiddisk = -1;
3823
3824 if (dl->index >= 0)
3825 used++;
3826
3827 /* don't activate new spares if we are orom constrained
3828 * and there is already a volume active in the container
3829 */
3830 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
3831 continue;
3832
3833 e = get_extents(super, dl);
3834 if (!e)
3835 continue;
3836 for (i = 1; e[i-1].size; i++)
3837 ;
3838 dl->e = e;
3839 dl->extent_cnt = i;
3840 extent_cnt += i;
3841 cnt++;
3842 }
3843
3844 maxsize = merge_extents(super, extent_cnt);
3845 minsize = size;
3846 if (size == 0)
3847 minsize = chunk;
3848
3849 if (cnt < raiddisks ||
3850 (super->orom && used && used != raiddisks) ||
3851 maxsize < minsize ||
3852 maxsize == 0) {
3853 fprintf(stderr, Name ": not enough devices with space to create array.\n");
3854 return 0; /* No enough free spaces large enough */
3855 }
3856
3857 if (size == 0) {
3858 size = maxsize;
3859 if (chunk) {
3860 size /= chunk;
3861 size *= chunk;
3862 }
3863 }
3864
3865 cnt = 0;
3866 for (dl = super->disks; dl; dl = dl->next)
3867 if (dl->e)
3868 dl->raiddisk = cnt++;
3869
3870 *freesize = size;
3871
3872 return 1;
3873 }
3874
3875 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
3876 int raiddisks, int chunk, unsigned long long size,
3877 char *dev, unsigned long long *freesize,
3878 int verbose)
3879 {
3880 int fd, cfd;
3881 struct mdinfo *sra;
3882
3883 /* if given unused devices create a container
3884 * if given given devices in a container create a member volume
3885 */
3886 if (level == LEVEL_CONTAINER) {
3887 /* Must be a fresh device to add to a container */
3888 return validate_geometry_imsm_container(st, level, layout,
3889 raiddisks, chunk, size,
3890 dev, freesize,
3891 verbose);
3892 }
3893
3894 if (!dev) {
3895 if (st->sb && freesize) {
3896 /* we are being asked to automatically layout a
3897 * new volume based on the current contents of
3898 * the container. If the the parameters can be
3899 * satisfied reserve_space will record the disks,
3900 * start offset, and size of the volume to be
3901 * created. add_to_super and getinfo_super
3902 * detect when autolayout is in progress.
3903 */
3904 if (!validate_geometry_imsm_orom(st->sb, level, layout,
3905 raiddisks, chunk,
3906 verbose))
3907 return 0;
3908 return reserve_space(st, raiddisks, size, chunk, freesize);
3909 }
3910 return 1;
3911 }
3912 if (st->sb) {
3913 /* creating in a given container */
3914 return validate_geometry_imsm_volume(st, level, layout,
3915 raiddisks, chunk, size,
3916 dev, freesize, verbose);
3917 }
3918
3919 /* limit creation to the following levels */
3920 if (!dev)
3921 switch (level) {
3922 case 0:
3923 case 1:
3924 case 10:
3925 case 5:
3926 return 0;
3927 default:
3928 if (verbose)
3929 fprintf(stderr, Name
3930 ": IMSM only supports levels 0,1,5,10\n");
3931 return 1;
3932 }
3933
3934 /* This device needs to be a device in an 'imsm' container */
3935 fd = open(dev, O_RDONLY|O_EXCL, 0);
3936 if (fd >= 0) {
3937 if (verbose)
3938 fprintf(stderr,
3939 Name ": Cannot create this array on device %s\n",
3940 dev);
3941 close(fd);
3942 return 0;
3943 }
3944 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
3945 if (verbose)
3946 fprintf(stderr, Name ": Cannot open %s: %s\n",
3947 dev, strerror(errno));
3948 return 0;
3949 }
3950 /* Well, it is in use by someone, maybe an 'imsm' container. */
3951 cfd = open_container(fd);
3952 if (cfd < 0) {
3953 close(fd);
3954 if (verbose)
3955 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
3956 dev);
3957 return 0;
3958 }
3959 sra = sysfs_read(cfd, 0, GET_VERSION);
3960 close(fd);
3961 if (sra && sra->array.major_version == -1 &&
3962 strcmp(sra->text_version, "imsm") == 0) {
3963 /* This is a member of a imsm container. Load the container
3964 * and try to create a volume
3965 */
3966 struct intel_super *super;
3967
3968 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, 1) == 0) {
3969 st->sb = super;
3970 st->container_dev = fd2devnum(cfd);
3971 close(cfd);
3972 return validate_geometry_imsm_volume(st, level, layout,
3973 raiddisks, chunk,
3974 size, dev,
3975 freesize, verbose);
3976 }
3977 close(cfd);
3978 } else /* may belong to another container */
3979 return 0;
3980
3981 return 1;
3982 }
3983 #endif /* MDASSEMBLE */
3984
3985 static int is_rebuilding(struct imsm_dev *dev)
3986 {
3987 struct imsm_map *migr_map;
3988
3989 if (!dev->vol.migr_state)
3990 return 0;
3991
3992 if (migr_type(dev) != MIGR_REBUILD)
3993 return 0;
3994
3995 migr_map = get_imsm_map(dev, 1);
3996
3997 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
3998 return 1;
3999 else
4000 return 0;
4001 }
4002
4003 static void update_recovery_start(struct imsm_dev *dev, struct mdinfo *array)
4004 {
4005 struct mdinfo *rebuild = NULL;
4006 struct mdinfo *d;
4007 __u32 units;
4008
4009 if (!is_rebuilding(dev))
4010 return;
4011
4012 /* Find the rebuild target, but punt on the dual rebuild case */
4013 for (d = array->devs; d; d = d->next)
4014 if (d->recovery_start == 0) {
4015 if (rebuild)
4016 return;
4017 rebuild = d;
4018 }
4019
4020 units = __le32_to_cpu(dev->vol.curr_migr_unit);
4021 rebuild->recovery_start = units * blocks_per_migr_unit(dev);
4022 }
4023
4024
4025 static struct mdinfo *container_content_imsm(struct supertype *st)
4026 {
4027 /* Given a container loaded by load_super_imsm_all,
4028 * extract information about all the arrays into
4029 * an mdinfo tree.
4030 *
4031 * For each imsm_dev create an mdinfo, fill it in,
4032 * then look for matching devices in super->disks
4033 * and create appropriate device mdinfo.
4034 */
4035 struct intel_super *super = st->sb;
4036 struct imsm_super *mpb = super->anchor;
4037 struct mdinfo *rest = NULL;
4038 int i;
4039
4040 /* do not assemble arrays that might have bad blocks */
4041 if (imsm_bbm_log_size(super->anchor)) {
4042 fprintf(stderr, Name ": BBM log found in metadata. "
4043 "Cannot activate array(s).\n");
4044 return NULL;
4045 }
4046
4047 for (i = 0; i < mpb->num_raid_devs; i++) {
4048 struct imsm_dev *dev = get_imsm_dev(super, i);
4049 struct imsm_map *map = get_imsm_map(dev, 0);
4050 struct mdinfo *this;
4051 int slot;
4052
4053 /* do not publish arrays that are in the middle of an
4054 * unsupported migration
4055 */
4056 if (dev->vol.migr_state &&
4057 (migr_type(dev) == MIGR_GEN_MIGR ||
4058 migr_type(dev) == MIGR_STATE_CHANGE)) {
4059 fprintf(stderr, Name ": cannot assemble volume '%.16s':"
4060 " unsupported migration in progress\n",
4061 dev->volume);
4062 continue;
4063 }
4064
4065 this = malloc(sizeof(*this));
4066 memset(this, 0, sizeof(*this));
4067 this->next = rest;
4068
4069 super->current_vol = i;
4070 getinfo_super_imsm_volume(st, this);
4071 for (slot = 0 ; slot < map->num_members; slot++) {
4072 unsigned long long recovery_start;
4073 struct mdinfo *info_d;
4074 struct dl *d;
4075 int idx;
4076 int skip;
4077 __u32 ord;
4078
4079 skip = 0;
4080 idx = get_imsm_disk_idx(dev, slot);
4081 ord = get_imsm_ord_tbl_ent(dev, slot);
4082 for (d = super->disks; d ; d = d->next)
4083 if (d->index == idx)
4084 break;
4085
4086 recovery_start = MaxSector;
4087 if (d == NULL)
4088 skip = 1;
4089 if (d && is_failed(&d->disk))
4090 skip = 1;
4091 if (ord & IMSM_ORD_REBUILD)
4092 recovery_start = 0;
4093
4094 /*
4095 * if we skip some disks the array will be assmebled degraded;
4096 * reset resync start to avoid a dirty-degraded
4097 * situation when performing the intial sync
4098 *
4099 * FIXME handle dirty degraded
4100 */
4101 if ((skip || recovery_start == 0) && !dev->vol.dirty)
4102 this->resync_start = MaxSector;
4103 if (skip)
4104 continue;
4105
4106 info_d = calloc(1, sizeof(*info_d));
4107 if (!info_d) {
4108 fprintf(stderr, Name ": failed to allocate disk"
4109 " for volume %.16s\n", dev->volume);
4110 info_d = this->devs;
4111 while (info_d) {
4112 struct mdinfo *d = info_d->next;
4113
4114 free(info_d);
4115 info_d = d;
4116 }
4117 free(this);
4118 this = rest;
4119 break;
4120 }
4121 info_d->next = this->devs;
4122 this->devs = info_d;
4123
4124 info_d->disk.number = d->index;
4125 info_d->disk.major = d->major;
4126 info_d->disk.minor = d->minor;
4127 info_d->disk.raid_disk = slot;
4128 info_d->recovery_start = recovery_start;
4129
4130 if (info_d->recovery_start == MaxSector)
4131 this->array.working_disks++;
4132
4133 info_d->events = __le32_to_cpu(mpb->generation_num);
4134 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
4135 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
4136 if (d->devname)
4137 strcpy(info_d->name, d->devname);
4138 }
4139 /* now that the disk list is up-to-date fixup recovery_start */
4140 update_recovery_start(dev, this);
4141 rest = this;
4142 }
4143
4144 return rest;
4145 }
4146
4147
4148 #ifndef MDASSEMBLE
4149 static int imsm_open_new(struct supertype *c, struct active_array *a,
4150 char *inst)
4151 {
4152 struct intel_super *super = c->sb;
4153 struct imsm_super *mpb = super->anchor;
4154
4155 if (atoi(inst) >= mpb->num_raid_devs) {
4156 fprintf(stderr, "%s: subarry index %d, out of range\n",
4157 __func__, atoi(inst));
4158 return -ENODEV;
4159 }
4160
4161 dprintf("imsm: open_new %s\n", inst);
4162 a->info.container_member = atoi(inst);
4163 return 0;
4164 }
4165
4166 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed)
4167 {
4168 struct imsm_map *map = get_imsm_map(dev, 0);
4169
4170 if (!failed)
4171 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
4172 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
4173
4174 switch (get_imsm_raid_level(map)) {
4175 case 0:
4176 return IMSM_T_STATE_FAILED;
4177 break;
4178 case 1:
4179 if (failed < map->num_members)
4180 return IMSM_T_STATE_DEGRADED;
4181 else
4182 return IMSM_T_STATE_FAILED;
4183 break;
4184 case 10:
4185 {
4186 /**
4187 * check to see if any mirrors have failed, otherwise we
4188 * are degraded. Even numbered slots are mirrored on
4189 * slot+1
4190 */
4191 int i;
4192 /* gcc -Os complains that this is unused */
4193 int insync = insync;
4194
4195 for (i = 0; i < map->num_members; i++) {
4196 __u32 ord = get_imsm_ord_tbl_ent(dev, i);
4197 int idx = ord_to_idx(ord);
4198 struct imsm_disk *disk;
4199
4200 /* reset the potential in-sync count on even-numbered
4201 * slots. num_copies is always 2 for imsm raid10
4202 */
4203 if ((i & 1) == 0)
4204 insync = 2;
4205
4206 disk = get_imsm_disk(super, idx);
4207 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
4208 insync--;
4209
4210 /* no in-sync disks left in this mirror the
4211 * array has failed
4212 */
4213 if (insync == 0)
4214 return IMSM_T_STATE_FAILED;
4215 }
4216
4217 return IMSM_T_STATE_DEGRADED;
4218 }
4219 case 5:
4220 if (failed < 2)
4221 return IMSM_T_STATE_DEGRADED;
4222 else
4223 return IMSM_T_STATE_FAILED;
4224 break;
4225 default:
4226 break;
4227 }
4228
4229 return map->map_state;
4230 }
4231
4232 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev)
4233 {
4234 int i;
4235 int failed = 0;
4236 struct imsm_disk *disk;
4237 struct imsm_map *map = get_imsm_map(dev, 0);
4238 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
4239 __u32 ord;
4240 int idx;
4241
4242 /* at the beginning of migration we set IMSM_ORD_REBUILD on
4243 * disks that are being rebuilt. New failures are recorded to
4244 * map[0]. So we look through all the disks we started with and
4245 * see if any failures are still present, or if any new ones
4246 * have arrived
4247 *
4248 * FIXME add support for online capacity expansion and
4249 * raid-level-migration
4250 */
4251 for (i = 0; i < prev->num_members; i++) {
4252 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
4253 ord |= __le32_to_cpu(map->disk_ord_tbl[i]);
4254 idx = ord_to_idx(ord);
4255
4256 disk = get_imsm_disk(super, idx);
4257 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
4258 failed++;
4259 }
4260
4261 return failed;
4262 }
4263
4264 static int is_resyncing(struct imsm_dev *dev)
4265 {
4266 struct imsm_map *migr_map;
4267
4268 if (!dev->vol.migr_state)
4269 return 0;
4270
4271 if (migr_type(dev) == MIGR_INIT ||
4272 migr_type(dev) == MIGR_REPAIR)
4273 return 1;
4274
4275 migr_map = get_imsm_map(dev, 1);
4276
4277 if (migr_map->map_state == IMSM_T_STATE_NORMAL)
4278 return 1;
4279 else
4280 return 0;
4281 }
4282
4283 /* return true if we recorded new information */
4284 static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
4285 {
4286 __u32 ord;
4287 int slot;
4288 struct imsm_map *map;
4289
4290 /* new failures are always set in map[0] */
4291 map = get_imsm_map(dev, 0);
4292
4293 slot = get_imsm_disk_slot(map, idx);
4294 if (slot < 0)
4295 return 0;
4296
4297 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
4298 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
4299 return 0;
4300
4301 disk->status |= FAILED_DISK;
4302 disk->status &= ~CONFIGURED_DISK;
4303 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
4304 if (~map->failed_disk_num == 0)
4305 map->failed_disk_num = slot;
4306 return 1;
4307 }
4308
4309 static void mark_missing(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
4310 {
4311 mark_failure(dev, disk, idx);
4312
4313 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
4314 return;
4315
4316 disk->scsi_id = __cpu_to_le32(~(__u32)0);
4317 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
4318 }
4319
4320 /* Handle dirty -> clean transititions and resync. Degraded and rebuild
4321 * states are handled in imsm_set_disk() with one exception, when a
4322 * resync is stopped due to a new failure this routine will set the
4323 * 'degraded' state for the array.
4324 */
4325 static int imsm_set_array_state(struct active_array *a, int consistent)
4326 {
4327 int inst = a->info.container_member;
4328 struct intel_super *super = a->container->sb;
4329 struct imsm_dev *dev = get_imsm_dev(super, inst);
4330 struct imsm_map *map = get_imsm_map(dev, 0);
4331 int failed = imsm_count_failed(super, dev);
4332 __u8 map_state = imsm_check_degraded(super, dev, failed);
4333 __u32 blocks_per_unit;
4334
4335 /* before we activate this array handle any missing disks */
4336 if (consistent == 2 && super->missing) {
4337 struct dl *dl;
4338
4339 dprintf("imsm: mark missing\n");
4340 end_migration(dev, map_state);
4341 for (dl = super->missing; dl; dl = dl->next)
4342 mark_missing(dev, &dl->disk, dl->index);
4343 super->updates_pending++;
4344 }
4345
4346 if (consistent == 2 &&
4347 (!is_resync_complete(&a->info) ||
4348 map_state != IMSM_T_STATE_NORMAL ||
4349 dev->vol.migr_state))
4350 consistent = 0;
4351
4352 if (is_resync_complete(&a->info)) {
4353 /* complete intialization / resync,
4354 * recovery and interrupted recovery is completed in
4355 * ->set_disk
4356 */
4357 if (is_resyncing(dev)) {
4358 dprintf("imsm: mark resync done\n");
4359 end_migration(dev, map_state);
4360 super->updates_pending++;
4361 }
4362 } else if (!is_resyncing(dev) && !failed) {
4363 /* mark the start of the init process if nothing is failed */
4364 dprintf("imsm: mark resync start\n");
4365 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
4366 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_INIT);
4367 else
4368 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
4369 super->updates_pending++;
4370 }
4371
4372 /* check if we can update curr_migr_unit from resync_start, recovery_start */
4373 blocks_per_unit = blocks_per_migr_unit(dev);
4374 if (blocks_per_unit && failed <= 1) {
4375 __u32 units32;
4376 __u64 units;
4377
4378 if (migr_type(dev) == MIGR_REBUILD)
4379 units = min_recovery_start(&a->info) / blocks_per_unit;
4380 else
4381 units = a->info.resync_start / blocks_per_unit;
4382 units32 = units;
4383
4384 /* check that we did not overflow 32-bits, and that
4385 * curr_migr_unit needs updating
4386 */
4387 if (units32 == units &&
4388 __le32_to_cpu(dev->vol.curr_migr_unit) != units32) {
4389 dprintf("imsm: mark checkpoint (%u)\n", units32);
4390 dev->vol.curr_migr_unit = __cpu_to_le32(units32);
4391 super->updates_pending++;
4392 }
4393 }
4394
4395 /* mark dirty / clean */
4396 if (dev->vol.dirty != !consistent) {
4397 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
4398 if (consistent)
4399 dev->vol.dirty = 0;
4400 else
4401 dev->vol.dirty = 1;
4402 super->updates_pending++;
4403 }
4404 return consistent;
4405 }
4406
4407 static void imsm_set_disk(struct active_array *a, int n, int state)
4408 {
4409 int inst = a->info.container_member;
4410 struct intel_super *super = a->container->sb;
4411 struct imsm_dev *dev = get_imsm_dev(super, inst);
4412 struct imsm_map *map = get_imsm_map(dev, 0);
4413 struct imsm_disk *disk;
4414 int failed;
4415 __u32 ord;
4416 __u8 map_state;
4417
4418 if (n > map->num_members)
4419 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
4420 n, map->num_members - 1);
4421
4422 if (n < 0)
4423 return;
4424
4425 dprintf("imsm: set_disk %d:%x\n", n, state);
4426
4427 ord = get_imsm_ord_tbl_ent(dev, n);
4428 disk = get_imsm_disk(super, ord_to_idx(ord));
4429
4430 /* check for new failures */
4431 if (state & DS_FAULTY) {
4432 if (mark_failure(dev, disk, ord_to_idx(ord)))
4433 super->updates_pending++;
4434 }
4435
4436 /* check if in_sync */
4437 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
4438 struct imsm_map *migr_map = get_imsm_map(dev, 1);
4439
4440 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
4441 super->updates_pending++;
4442 }
4443
4444 failed = imsm_count_failed(super, dev);
4445 map_state = imsm_check_degraded(super, dev, failed);
4446
4447 /* check if recovery complete, newly degraded, or failed */
4448 if (map_state == IMSM_T_STATE_NORMAL && is_rebuilding(dev)) {
4449 end_migration(dev, map_state);
4450 map = get_imsm_map(dev, 0);
4451 map->failed_disk_num = ~0;
4452 super->updates_pending++;
4453 } else if (map_state == IMSM_T_STATE_DEGRADED &&
4454 map->map_state != map_state &&
4455 !dev->vol.migr_state) {
4456 dprintf("imsm: mark degraded\n");
4457 map->map_state = map_state;
4458 super->updates_pending++;
4459 } else if (map_state == IMSM_T_STATE_FAILED &&
4460 map->map_state != map_state) {
4461 dprintf("imsm: mark failed\n");
4462 end_migration(dev, map_state);
4463 super->updates_pending++;
4464 }
4465 }
4466
4467 static int store_imsm_mpb(int fd, struct imsm_super *mpb)
4468 {
4469 void *buf = mpb;
4470 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
4471 unsigned long long dsize;
4472 unsigned long long sectors;
4473
4474 get_dev_size(fd, NULL, &dsize);
4475
4476 if (mpb_size > 512) {
4477 /* -1 to account for anchor */
4478 sectors = mpb_sectors(mpb) - 1;
4479
4480 /* write the extended mpb to the sectors preceeding the anchor */
4481 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
4482 return 1;
4483
4484 if (write(fd, buf + 512, 512 * sectors) != 512 * sectors)
4485 return 1;
4486 }
4487
4488 /* first block is stored on second to last sector of the disk */
4489 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
4490 return 1;
4491
4492 if (write(fd, buf, 512) != 512)
4493 return 1;
4494
4495 return 0;
4496 }
4497
4498 static void imsm_sync_metadata(struct supertype *container)
4499 {
4500 struct intel_super *super = container->sb;
4501
4502 if (!super->updates_pending)
4503 return;
4504
4505 write_super_imsm(super, 0);
4506
4507 super->updates_pending = 0;
4508 }
4509
4510 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
4511 {
4512 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
4513 int i = get_imsm_disk_idx(dev, idx);
4514 struct dl *dl;
4515
4516 for (dl = super->disks; dl; dl = dl->next)
4517 if (dl->index == i)
4518 break;
4519
4520 if (dl && is_failed(&dl->disk))
4521 dl = NULL;
4522
4523 if (dl)
4524 dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor);
4525
4526 return dl;
4527 }
4528
4529 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
4530 struct active_array *a, int activate_new)
4531 {
4532 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
4533 int idx = get_imsm_disk_idx(dev, slot);
4534 struct imsm_super *mpb = super->anchor;
4535 struct imsm_map *map;
4536 unsigned long long pos;
4537 struct mdinfo *d;
4538 struct extent *ex;
4539 int i, j;
4540 int found;
4541 __u32 array_start;
4542 __u32 array_end;
4543 struct dl *dl;
4544
4545 for (dl = super->disks; dl; dl = dl->next) {
4546 /* If in this array, skip */
4547 for (d = a->info.devs ; d ; d = d->next)
4548 if (d->state_fd >= 0 &&
4549 d->disk.major == dl->major &&
4550 d->disk.minor == dl->minor) {
4551 dprintf("%x:%x already in array\n", dl->major, dl->minor);
4552 break;
4553 }
4554 if (d)
4555 continue;
4556
4557 /* skip in use or failed drives */
4558 if (is_failed(&dl->disk) || idx == dl->index ||
4559 dl->index == -2) {
4560 dprintf("%x:%x status (failed: %d index: %d)\n",
4561 dl->major, dl->minor, is_failed(&dl->disk), idx);
4562 continue;
4563 }
4564
4565 /* skip pure spares when we are looking for partially
4566 * assimilated drives
4567 */
4568 if (dl->index == -1 && !activate_new)
4569 continue;
4570
4571 /* Does this unused device have the requisite free space?
4572 * It needs to be able to cover all member volumes
4573 */
4574 ex = get_extents(super, dl);
4575 if (!ex) {
4576 dprintf("cannot get extents\n");
4577 continue;
4578 }
4579 for (i = 0; i < mpb->num_raid_devs; i++) {
4580 dev = get_imsm_dev(super, i);
4581 map = get_imsm_map(dev, 0);
4582
4583 /* check if this disk is already a member of
4584 * this array
4585 */
4586 if (get_imsm_disk_slot(map, dl->index) >= 0)
4587 continue;
4588
4589 found = 0;
4590 j = 0;
4591 pos = 0;
4592 array_start = __le32_to_cpu(map->pba_of_lba0);
4593 array_end = array_start +
4594 __le32_to_cpu(map->blocks_per_member) - 1;
4595
4596 do {
4597 /* check that we can start at pba_of_lba0 with
4598 * blocks_per_member of space
4599 */
4600 if (array_start >= pos && array_end < ex[j].start) {
4601 found = 1;
4602 break;
4603 }
4604 pos = ex[j].start + ex[j].size;
4605 j++;
4606 } while (ex[j-1].size);
4607
4608 if (!found)
4609 break;
4610 }
4611
4612 free(ex);
4613 if (i < mpb->num_raid_devs) {
4614 dprintf("%x:%x does not have %u to %u available\n",
4615 dl->major, dl->minor, array_start, array_end);
4616 /* No room */
4617 continue;
4618 }
4619 return dl;
4620 }
4621
4622 return dl;
4623 }
4624
4625 static struct mdinfo *imsm_activate_spare(struct active_array *a,
4626 struct metadata_update **updates)
4627 {
4628 /**
4629 * Find a device with unused free space and use it to replace a
4630 * failed/vacant region in an array. We replace failed regions one a
4631 * array at a time. The result is that a new spare disk will be added
4632 * to the first failed array and after the monitor has finished
4633 * propagating failures the remainder will be consumed.
4634 *
4635 * FIXME add a capability for mdmon to request spares from another
4636 * container.
4637 */
4638
4639 struct intel_super *super = a->container->sb;
4640 int inst = a->info.container_member;
4641 struct imsm_dev *dev = get_imsm_dev(super, inst);
4642 struct imsm_map *map = get_imsm_map(dev, 0);
4643 int failed = a->info.array.raid_disks;
4644 struct mdinfo *rv = NULL;
4645 struct mdinfo *d;
4646 struct mdinfo *di;
4647 struct metadata_update *mu;
4648 struct dl *dl;
4649 struct imsm_update_activate_spare *u;
4650 int num_spares = 0;
4651 int i;
4652
4653 for (d = a->info.devs ; d ; d = d->next) {
4654 if ((d->curr_state & DS_FAULTY) &&
4655 d->state_fd >= 0)
4656 /* wait for Removal to happen */
4657 return NULL;
4658 if (d->state_fd >= 0)
4659 failed--;
4660 }
4661
4662 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
4663 inst, failed, a->info.array.raid_disks, a->info.array.level);
4664 if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED)
4665 return NULL;
4666
4667 /* For each slot, if it is not working, find a spare */
4668 for (i = 0; i < a->info.array.raid_disks; i++) {
4669 for (d = a->info.devs ; d ; d = d->next)
4670 if (d->disk.raid_disk == i)
4671 break;
4672 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
4673 if (d && (d->state_fd >= 0))
4674 continue;
4675
4676 /*
4677 * OK, this device needs recovery. Try to re-add the
4678 * previous occupant of this slot, if this fails see if
4679 * we can continue the assimilation of a spare that was
4680 * partially assimilated, finally try to activate a new
4681 * spare.
4682 */
4683 dl = imsm_readd(super, i, a);
4684 if (!dl)
4685 dl = imsm_add_spare(super, i, a, 0);
4686 if (!dl)
4687 dl = imsm_add_spare(super, i, a, 1);
4688 if (!dl)
4689 continue;
4690
4691 /* found a usable disk with enough space */
4692 di = malloc(sizeof(*di));
4693 if (!di)
4694 continue;
4695 memset(di, 0, sizeof(*di));
4696
4697 /* dl->index will be -1 in the case we are activating a
4698 * pristine spare. imsm_process_update() will create a
4699 * new index in this case. Once a disk is found to be
4700 * failed in all member arrays it is kicked from the
4701 * metadata
4702 */
4703 di->disk.number = dl->index;
4704
4705 /* (ab)use di->devs to store a pointer to the device
4706 * we chose
4707 */
4708 di->devs = (struct mdinfo *) dl;
4709
4710 di->disk.raid_disk = i;
4711 di->disk.major = dl->major;
4712 di->disk.minor = dl->minor;
4713 di->disk.state = 0;
4714 di->recovery_start = 0;
4715 di->data_offset = __le32_to_cpu(map->pba_of_lba0);
4716 di->component_size = a->info.component_size;
4717 di->container_member = inst;
4718 super->random = random32();
4719 di->next = rv;
4720 rv = di;
4721 num_spares++;
4722 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
4723 i, di->data_offset);
4724
4725 break;
4726 }
4727
4728 if (!rv)
4729 /* No spares found */
4730 return rv;
4731 /* Now 'rv' has a list of devices to return.
4732 * Create a metadata_update record to update the
4733 * disk_ord_tbl for the array
4734 */
4735 mu = malloc(sizeof(*mu));
4736 if (mu) {
4737 mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
4738 if (mu->buf == NULL) {
4739 free(mu);
4740 mu = NULL;
4741 }
4742 }
4743 if (!mu) {
4744 while (rv) {
4745 struct mdinfo *n = rv->next;
4746
4747 free(rv);
4748 rv = n;
4749 }
4750 return NULL;
4751 }
4752
4753 mu->space = NULL;
4754 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
4755 mu->next = *updates;
4756 u = (struct imsm_update_activate_spare *) mu->buf;
4757
4758 for (di = rv ; di ; di = di->next) {
4759 u->type = update_activate_spare;
4760 u->dl = (struct dl *) di->devs;
4761 di->devs = NULL;
4762 u->slot = di->disk.raid_disk;
4763 u->array = inst;
4764 u->next = u + 1;
4765 u++;
4766 }
4767 (u-1)->next = NULL;
4768 *updates = mu;
4769
4770 return rv;
4771 }
4772
4773 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
4774 {
4775 struct imsm_dev *dev = get_imsm_dev(super, idx);
4776 struct imsm_map *map = get_imsm_map(dev, 0);
4777 struct imsm_map *new_map = get_imsm_map(&u->dev, 0);
4778 struct disk_info *inf = get_disk_info(u);
4779 struct imsm_disk *disk;
4780 int i;
4781 int j;
4782
4783 for (i = 0; i < map->num_members; i++) {
4784 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
4785 for (j = 0; j < new_map->num_members; j++)
4786 if (serialcmp(disk->serial, inf[j].serial) == 0)
4787 return 1;
4788 }
4789
4790 return 0;
4791 }
4792
4793 static void imsm_delete(struct intel_super *super, struct dl **dlp, int index);
4794
4795 static void imsm_process_update(struct supertype *st,
4796 struct metadata_update *update)
4797 {
4798 /**
4799 * crack open the metadata_update envelope to find the update record
4800 * update can be one of:
4801 * update_activate_spare - a spare device has replaced a failed
4802 * device in an array, update the disk_ord_tbl. If this disk is
4803 * present in all member arrays then also clear the SPARE_DISK
4804 * flag
4805 */
4806 struct intel_super *super = st->sb;
4807 struct imsm_super *mpb;
4808 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
4809
4810 /* update requires a larger buf but the allocation failed */
4811 if (super->next_len && !super->next_buf) {
4812 super->next_len = 0;
4813 return;
4814 }
4815
4816 if (super->next_buf) {
4817 memcpy(super->next_buf, super->buf, super->len);
4818 free(super->buf);
4819 super->len = super->next_len;
4820 super->buf = super->next_buf;
4821
4822 super->next_len = 0;
4823 super->next_buf = NULL;
4824 }
4825
4826 mpb = super->anchor;
4827
4828 switch (type) {
4829 case update_activate_spare: {
4830 struct imsm_update_activate_spare *u = (void *) update->buf;
4831 struct imsm_dev *dev = get_imsm_dev(super, u->array);
4832 struct imsm_map *map = get_imsm_map(dev, 0);
4833 struct imsm_map *migr_map;
4834 struct active_array *a;
4835 struct imsm_disk *disk;
4836 __u8 to_state;
4837 struct dl *dl;
4838 unsigned int found;
4839 int failed;
4840 int victim = get_imsm_disk_idx(dev, u->slot);
4841 int i;
4842
4843 for (dl = super->disks; dl; dl = dl->next)
4844 if (dl == u->dl)
4845 break;
4846
4847 if (!dl) {
4848 fprintf(stderr, "error: imsm_activate_spare passed "
4849 "an unknown disk (index: %d)\n",
4850 u->dl->index);
4851 return;
4852 }
4853
4854 super->updates_pending++;
4855
4856 /* count failures (excluding rebuilds and the victim)
4857 * to determine map[0] state
4858 */
4859 failed = 0;
4860 for (i = 0; i < map->num_members; i++) {
4861 if (i == u->slot)
4862 continue;
4863 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
4864 if (!disk || is_failed(disk))
4865 failed++;
4866 }
4867
4868 /* adding a pristine spare, assign a new index */
4869 if (dl->index < 0) {
4870 dl->index = super->anchor->num_disks;
4871 super->anchor->num_disks++;
4872 }
4873 disk = &dl->disk;
4874 disk->status |= CONFIGURED_DISK;
4875 disk->status &= ~SPARE_DISK;
4876
4877 /* mark rebuild */
4878 to_state = imsm_check_degraded(super, dev, failed);
4879 map->map_state = IMSM_T_STATE_DEGRADED;
4880 migrate(dev, to_state, MIGR_REBUILD);
4881 migr_map = get_imsm_map(dev, 1);
4882 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
4883 set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD);
4884
4885 /* update the family_num to mark a new container
4886 * generation, being careful to record the existing
4887 * family_num in orig_family_num to clean up after
4888 * earlier mdadm versions that neglected to set it.
4889 */
4890 if (mpb->orig_family_num == 0)
4891 mpb->orig_family_num = mpb->family_num;
4892 mpb->family_num += super->random;
4893
4894 /* count arrays using the victim in the metadata */
4895 found = 0;
4896 for (a = st->arrays; a ; a = a->next) {
4897 dev = get_imsm_dev(super, a->info.container_member);
4898 map = get_imsm_map(dev, 0);
4899
4900 if (get_imsm_disk_slot(map, victim) >= 0)
4901 found++;
4902 }
4903
4904 /* delete the victim if it is no longer being
4905 * utilized anywhere
4906 */
4907 if (!found) {
4908 struct dl **dlp;
4909
4910 /* We know that 'manager' isn't touching anything,
4911 * so it is safe to delete
4912 */
4913 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
4914 if ((*dlp)->index == victim)
4915 break;
4916
4917 /* victim may be on the missing list */
4918 if (!*dlp)
4919 for (dlp = &super->missing; *dlp; dlp = &(*dlp)->next)
4920 if ((*dlp)->index == victim)
4921 break;
4922 imsm_delete(super, dlp, victim);
4923 }
4924 break;
4925 }
4926 case update_create_array: {
4927 /* someone wants to create a new array, we need to be aware of
4928 * a few races/collisions:
4929 * 1/ 'Create' called by two separate instances of mdadm
4930 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
4931 * devices that have since been assimilated via
4932 * activate_spare.
4933 * In the event this update can not be carried out mdadm will
4934 * (FIX ME) notice that its update did not take hold.
4935 */
4936 struct imsm_update_create_array *u = (void *) update->buf;
4937 struct intel_dev *dv;
4938 struct imsm_dev *dev;
4939 struct imsm_map *map, *new_map;
4940 unsigned long long start, end;
4941 unsigned long long new_start, new_end;
4942 int i;
4943 struct disk_info *inf;
4944 struct dl *dl;
4945
4946 /* handle racing creates: first come first serve */
4947 if (u->dev_idx < mpb->num_raid_devs) {
4948 dprintf("%s: subarray %d already defined\n",
4949 __func__, u->dev_idx);
4950 goto create_error;
4951 }
4952
4953 /* check update is next in sequence */
4954 if (u->dev_idx != mpb->num_raid_devs) {
4955 dprintf("%s: can not create array %d expected index %d\n",
4956 __func__, u->dev_idx, mpb->num_raid_devs);
4957 goto create_error;
4958 }
4959
4960 new_map = get_imsm_map(&u->dev, 0);
4961 new_start = __le32_to_cpu(new_map->pba_of_lba0);
4962 new_end = new_start + __le32_to_cpu(new_map->blocks_per_member);
4963 inf = get_disk_info(u);
4964
4965 /* handle activate_spare versus create race:
4966 * check to make sure that overlapping arrays do not include
4967 * overalpping disks
4968 */
4969 for (i = 0; i < mpb->num_raid_devs; i++) {
4970 dev = get_imsm_dev(super, i);
4971 map = get_imsm_map(dev, 0);
4972 start = __le32_to_cpu(map->pba_of_lba0);
4973 end = start + __le32_to_cpu(map->blocks_per_member);
4974 if ((new_start >= start && new_start <= end) ||
4975 (start >= new_start && start <= new_end))
4976 /* overlap */;
4977 else
4978 continue;
4979
4980 if (disks_overlap(super, i, u)) {
4981 dprintf("%s: arrays overlap\n", __func__);
4982 goto create_error;
4983 }
4984 }
4985
4986 /* check that prepare update was successful */
4987 if (!update->space) {
4988 dprintf("%s: prepare update failed\n", __func__);
4989 goto create_error;
4990 }
4991
4992 /* check that all disks are still active before committing
4993 * changes. FIXME: could we instead handle this by creating a
4994 * degraded array? That's probably not what the user expects,
4995 * so better to drop this update on the floor.
4996 */
4997 for (i = 0; i < new_map->num_members; i++) {
4998 dl = serial_to_dl(inf[i].serial, super);
4999 if (!dl) {
5000 dprintf("%s: disk disappeared\n", __func__);
5001 goto create_error;
5002 }
5003 }
5004
5005 super->updates_pending++;
5006
5007 /* convert spares to members and fixup ord_tbl */
5008 for (i = 0; i < new_map->num_members; i++) {
5009 dl = serial_to_dl(inf[i].serial, super);
5010 if (dl->index == -1) {
5011 dl->index = mpb->num_disks;
5012 mpb->num_disks++;
5013 dl->disk.status |= CONFIGURED_DISK;
5014 dl->disk.status &= ~SPARE_DISK;
5015 }
5016 set_imsm_ord_tbl_ent(new_map, i, dl->index);
5017 }
5018
5019 dv = update->space;
5020 dev = dv->dev;
5021 update->space = NULL;
5022 imsm_copy_dev(dev, &u->dev);
5023 dv->index = u->dev_idx;
5024 dv->next = super->devlist;
5025 super->devlist = dv;
5026 mpb->num_raid_devs++;
5027
5028 imsm_update_version_info(super);
5029 break;
5030 create_error:
5031 /* mdmon knows how to release update->space, but not
5032 * ((struct intel_dev *) update->space)->dev
5033 */
5034 if (update->space) {
5035 dv = update->space;
5036 free(dv->dev);
5037 }
5038 break;
5039 }
5040 case update_add_disk:
5041
5042 /* we may be able to repair some arrays if disks are
5043 * being added */
5044 if (super->add) {
5045 struct active_array *a;
5046
5047 super->updates_pending++;
5048 for (a = st->arrays; a; a = a->next)
5049 a->check_degraded = 1;
5050 }
5051 /* add some spares to the metadata */
5052 while (super->add) {
5053 struct dl *al;
5054
5055 al = super->add;
5056 super->add = al->next;
5057 al->next = super->disks;
5058 super->disks = al;
5059 dprintf("%s: added %x:%x\n",
5060 __func__, al->major, al->minor);
5061 }
5062
5063 break;
5064 }
5065 }
5066
5067 static void imsm_prepare_update(struct supertype *st,
5068 struct metadata_update *update)
5069 {
5070 /**
5071 * Allocate space to hold new disk entries, raid-device entries or a new
5072 * mpb if necessary. The manager synchronously waits for updates to
5073 * complete in the monitor, so new mpb buffers allocated here can be
5074 * integrated by the monitor thread without worrying about live pointers
5075 * in the manager thread.
5076 */
5077 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
5078 struct intel_super *super = st->sb;
5079 struct imsm_super *mpb = super->anchor;
5080 size_t buf_len;
5081 size_t len = 0;
5082
5083 switch (type) {
5084 case update_create_array: {
5085 struct imsm_update_create_array *u = (void *) update->buf;
5086 struct intel_dev *dv;
5087 struct imsm_dev *dev = &u->dev;
5088 struct imsm_map *map = get_imsm_map(dev, 0);
5089 struct dl *dl;
5090 struct disk_info *inf;
5091 int i;
5092 int activate = 0;
5093
5094 inf = get_disk_info(u);
5095 len = sizeof_imsm_dev(dev, 1);
5096 /* allocate a new super->devlist entry */
5097 dv = malloc(sizeof(*dv));
5098 if (dv) {
5099 dv->dev = malloc(len);
5100 if (dv->dev)
5101 update->space = dv;
5102 else {
5103 free(dv);
5104 update->space = NULL;
5105 }
5106 }
5107
5108 /* count how many spares will be converted to members */
5109 for (i = 0; i < map->num_members; i++) {
5110 dl = serial_to_dl(inf[i].serial, super);
5111 if (!dl) {
5112 /* hmm maybe it failed?, nothing we can do about
5113 * it here
5114 */
5115 continue;
5116 }
5117 if (count_memberships(dl, super) == 0)
5118 activate++;
5119 }
5120 len += activate * sizeof(struct imsm_disk);
5121 break;
5122 default:
5123 break;
5124 }
5125 }
5126
5127 /* check if we need a larger metadata buffer */
5128 if (super->next_buf)
5129 buf_len = super->next_len;
5130 else
5131 buf_len = super->len;
5132
5133 if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) {
5134 /* ok we need a larger buf than what is currently allocated
5135 * if this allocation fails process_update will notice that
5136 * ->next_len is set and ->next_buf is NULL
5137 */
5138 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512);
5139 if (super->next_buf)
5140 free(super->next_buf);
5141
5142 super->next_len = buf_len;
5143 if (posix_memalign(&super->next_buf, 512, buf_len) == 0)
5144 memset(super->next_buf, 0, buf_len);
5145 else
5146 super->next_buf = NULL;
5147 }
5148 }
5149
5150 /* must be called while manager is quiesced */
5151 static void imsm_delete(struct intel_super *super, struct dl **dlp, int index)
5152 {
5153 struct imsm_super *mpb = super->anchor;
5154 struct dl *iter;
5155 struct imsm_dev *dev;
5156 struct imsm_map *map;
5157 int i, j, num_members;
5158 __u32 ord;
5159
5160 dprintf("%s: deleting device[%d] from imsm_super\n",
5161 __func__, index);
5162
5163 /* shift all indexes down one */
5164 for (iter = super->disks; iter; iter = iter->next)
5165 if (iter->index > index)
5166 iter->index--;
5167 for (iter = super->missing; iter; iter = iter->next)
5168 if (iter->index > index)
5169 iter->index--;
5170
5171 for (i = 0; i < mpb->num_raid_devs; i++) {
5172 dev = get_imsm_dev(super, i);
5173 map = get_imsm_map(dev, 0);
5174 num_members = map->num_members;
5175 for (j = 0; j < num_members; j++) {
5176 /* update ord entries being careful not to propagate
5177 * ord-flags to the first map
5178 */
5179 ord = get_imsm_ord_tbl_ent(dev, j);
5180
5181 if (ord_to_idx(ord) <= index)
5182 continue;
5183
5184 map = get_imsm_map(dev, 0);
5185 set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1));
5186 map = get_imsm_map(dev, 1);
5187 if (map)
5188 set_imsm_ord_tbl_ent(map, j, ord - 1);
5189 }
5190 }
5191
5192 mpb->num_disks--;
5193 super->updates_pending++;
5194 if (*dlp) {
5195 struct dl *dl = *dlp;
5196
5197 *dlp = (*dlp)->next;
5198 __free_imsm_disk(dl);
5199 }
5200 }
5201 #endif /* MDASSEMBLE */
5202
5203 struct superswitch super_imsm = {
5204 #ifndef MDASSEMBLE
5205 .examine_super = examine_super_imsm,
5206 .brief_examine_super = brief_examine_super_imsm,
5207 .brief_examine_subarrays = brief_examine_subarrays_imsm,
5208 .export_examine_super = export_examine_super_imsm,
5209 .detail_super = detail_super_imsm,
5210 .brief_detail_super = brief_detail_super_imsm,
5211 .write_init_super = write_init_super_imsm,
5212 .validate_geometry = validate_geometry_imsm,
5213 .add_to_super = add_to_super_imsm,
5214 .detail_platform = detail_platform_imsm,
5215 #endif
5216 .match_home = match_home_imsm,
5217 .uuid_from_super= uuid_from_super_imsm,
5218 .getinfo_super = getinfo_super_imsm,
5219 .update_super = update_super_imsm,
5220
5221 .avail_size = avail_size_imsm,
5222
5223 .compare_super = compare_super_imsm,
5224
5225 .load_super = load_super_imsm,
5226 .init_super = init_super_imsm,
5227 .store_super = store_super_imsm,
5228 .free_super = free_super_imsm,
5229 .match_metadata_desc = match_metadata_desc_imsm,
5230 .container_content = container_content_imsm,
5231 .default_layout = imsm_level_to_layout,
5232
5233 .external = 1,
5234 .name = "imsm",
5235
5236 #ifndef MDASSEMBLE
5237 /* for mdmon */
5238 .open_new = imsm_open_new,
5239 .load_super = load_super_imsm,
5240 .set_array_state= imsm_set_array_state,
5241 .set_disk = imsm_set_disk,
5242 .sync_metadata = imsm_sync_metadata,
5243 .activate_spare = imsm_activate_spare,
5244 .process_update = imsm_process_update,
5245 .prepare_update = imsm_prepare_update,
5246 #endif /* MDASSEMBLE */
5247 };