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