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