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