]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-intel.c
imsm: verify that component disks are attached to the same type of HBA
[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 struct intel_hba {
237 enum sys_dev_type type;
238 char *path;
239 char *pci_id;
240 struct intel_hba *next;
241 };
242
243 enum action {
244 DISK_REMOVE = 1,
245 DISK_ADD
246 };
247 /* internal representation of IMSM metadata */
248 struct intel_super {
249 union {
250 void *buf; /* O_DIRECT buffer for reading/writing metadata */
251 struct imsm_super *anchor; /* immovable parameters */
252 };
253 size_t len; /* size of the 'buf' allocation */
254 void *next_buf; /* for realloc'ing buf from the manager */
255 size_t next_len;
256 int updates_pending; /* count of pending updates for mdmon */
257 int current_vol; /* index of raid device undergoing creation */
258 __u32 create_offset; /* common start for 'current_vol' */
259 __u32 random; /* random data for seeding new family numbers */
260 struct intel_dev *devlist;
261 struct dl {
262 struct dl *next;
263 int index;
264 __u8 serial[MAX_RAID_SERIAL_LEN];
265 int major, minor;
266 char *devname;
267 struct imsm_disk disk;
268 int fd;
269 int extent_cnt;
270 struct extent *e; /* for determining freespace @ create */
271 int raiddisk; /* slot to fill in autolayout */
272 enum action action;
273 } *disks;
274 struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
275 active */
276 struct dl *missing; /* disks removed while we weren't looking */
277 struct bbm_log *bbm_log;
278 struct intel_hba *hba; /* device path of the raid controller for this metadata */
279 const struct imsm_orom *orom; /* platform firmware support */
280 struct intel_super *next; /* (temp) list for disambiguating family_num */
281 };
282
283 struct intel_disk {
284 struct imsm_disk disk;
285 #define IMSM_UNKNOWN_OWNER (-1)
286 int owner;
287 struct intel_disk *next;
288 };
289
290 struct extent {
291 unsigned long long start, size;
292 };
293
294 /* definitions of reshape process types */
295 enum imsm_reshape_type {
296 CH_TAKEOVER,
297 CH_MIGRATION,
298 };
299
300 /* definition of messages passed to imsm_process_update */
301 enum imsm_update_type {
302 update_activate_spare,
303 update_create_array,
304 update_kill_array,
305 update_rename_array,
306 update_add_remove_disk,
307 update_reshape_container_disks,
308 update_takeover
309 };
310
311 struct imsm_update_activate_spare {
312 enum imsm_update_type type;
313 struct dl *dl;
314 int slot;
315 int array;
316 struct imsm_update_activate_spare *next;
317 };
318
319 struct geo_params {
320 int dev_id;
321 char *dev_name;
322 long long size;
323 int level;
324 int layout;
325 int chunksize;
326 int raid_disks;
327 };
328
329 enum takeover_direction {
330 R10_TO_R0,
331 R0_TO_R10
332 };
333 struct imsm_update_takeover {
334 enum imsm_update_type type;
335 int subarray;
336 enum takeover_direction direction;
337 };
338
339 struct imsm_update_reshape {
340 enum imsm_update_type type;
341 int old_raid_disks;
342 int new_raid_disks;
343 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
344 };
345
346 struct disk_info {
347 __u8 serial[MAX_RAID_SERIAL_LEN];
348 };
349
350 struct imsm_update_create_array {
351 enum imsm_update_type type;
352 int dev_idx;
353 struct imsm_dev dev;
354 };
355
356 struct imsm_update_kill_array {
357 enum imsm_update_type type;
358 int dev_idx;
359 };
360
361 struct imsm_update_rename_array {
362 enum imsm_update_type type;
363 __u8 name[MAX_RAID_SERIAL_LEN];
364 int dev_idx;
365 };
366
367 struct imsm_update_add_remove_disk {
368 enum imsm_update_type type;
369 };
370
371
372 static const char *_sys_dev_type[] = {
373 [SYS_DEV_UNKNOWN] = "Unknown",
374 [SYS_DEV_SAS] = "SAS",
375 [SYS_DEV_SATA] = "SATA"
376 };
377
378 const char *get_sys_dev_type(enum sys_dev_type type)
379 {
380 if (type >= SYS_DEV_MAX)
381 type = SYS_DEV_UNKNOWN;
382
383 return _sys_dev_type[type];
384 }
385
386 #ifndef MDASSEMBLE
387 static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
388 {
389 struct intel_hba *result = malloc(sizeof(*result));
390 if (result) {
391 result->type = device->type;
392 result->path = strdup(device->path);
393 result->next = NULL;
394 if (result->path && (result->pci_id = strrchr(result->path, '/')) != NULL)
395 result->pci_id++;
396 }
397 return result;
398 }
399
400 static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
401 {
402 struct intel_hba *result=NULL;
403 for (result = hba; result; result = result->next) {
404 if (result->type == device->type && strcmp(result->path, device->path) == 0)
405 break;
406 }
407 return result;
408 }
409
410
411 static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device)
412 {
413 struct intel_hba *hba;
414
415 /* check if disk attached to Intel HBA */
416 hba = find_intel_hba(super->hba, device);
417 if (hba != NULL)
418 return 1;
419 /* Check if HBA is already attached to super */
420 if (super->hba == NULL) {
421 super->hba = alloc_intel_hba(device);
422 return 1;
423 }
424
425 hba = super->hba;
426 /* Intel metadata allows for all disks attached to the same type HBA.
427 * Do not sypport odf HBA types mixing
428 */
429 if (device->type != hba->type)
430 return 2;
431
432 while (hba->next)
433 hba = hba->next;
434
435 hba->next = alloc_intel_hba(device);
436 return 1;
437 }
438
439 static struct sys_dev* find_disk_attached_hba(int fd, const char *devname)
440 {
441 struct sys_dev *list, *elem, *prev;
442 char *disk_path;
443
444 if ((list = find_intel_devices()) == NULL)
445 return 0;
446
447 if (fd < 0)
448 disk_path = (char *) devname;
449 else
450 disk_path = diskfd_to_devpath(fd);
451
452 if (!disk_path) {
453 free_sys_dev(&list);
454 return 0;
455 }
456
457 for (prev = NULL, elem = list; elem; prev = elem, elem = elem->next) {
458 if (path_attached_to_hba(disk_path, elem->path)) {
459 if (prev == NULL)
460 list = list->next;
461 else
462 prev->next = elem->next;
463 elem->next = NULL;
464 if (disk_path != devname)
465 free(disk_path);
466 free_sys_dev(&list);
467 return elem;
468 }
469 }
470 if (disk_path != devname)
471 free(disk_path);
472 free_sys_dev(&list);
473
474 return NULL;
475 }
476 #endif /* MDASSEMBLE */
477
478
479 static struct supertype *match_metadata_desc_imsm(char *arg)
480 {
481 struct supertype *st;
482
483 if (strcmp(arg, "imsm") != 0 &&
484 strcmp(arg, "default") != 0
485 )
486 return NULL;
487
488 st = malloc(sizeof(*st));
489 if (!st)
490 return NULL;
491 memset(st, 0, sizeof(*st));
492 st->container_dev = NoMdDev;
493 st->ss = &super_imsm;
494 st->max_devs = IMSM_MAX_DEVICES;
495 st->minor_version = 0;
496 st->sb = NULL;
497 return st;
498 }
499
500 #ifndef MDASSEMBLE
501 static __u8 *get_imsm_version(struct imsm_super *mpb)
502 {
503 return &mpb->sig[MPB_SIG_LEN];
504 }
505 #endif
506
507 /* retrieve a disk directly from the anchor when the anchor is known to be
508 * up-to-date, currently only at load time
509 */
510 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
511 {
512 if (index >= mpb->num_disks)
513 return NULL;
514 return &mpb->disk[index];
515 }
516
517 /* retrieve the disk description based on a index of the disk
518 * in the sub-array
519 */
520 static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
521 {
522 struct dl *d;
523
524 for (d = super->disks; d; d = d->next)
525 if (d->index == index)
526 return d;
527
528 return NULL;
529 }
530 /* retrieve a disk from the parsed metadata */
531 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
532 {
533 struct dl *dl;
534
535 dl = get_imsm_dl_disk(super, index);
536 if (dl)
537 return &dl->disk;
538
539 return NULL;
540 }
541
542 /* generate a checksum directly from the anchor when the anchor is known to be
543 * up-to-date, currently only at load or write_super after coalescing
544 */
545 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
546 {
547 __u32 end = mpb->mpb_size / sizeof(end);
548 __u32 *p = (__u32 *) mpb;
549 __u32 sum = 0;
550
551 while (end--) {
552 sum += __le32_to_cpu(*p);
553 p++;
554 }
555
556 return sum - __le32_to_cpu(mpb->check_sum);
557 }
558
559 static size_t sizeof_imsm_map(struct imsm_map *map)
560 {
561 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
562 }
563
564 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
565 {
566 /* A device can have 2 maps if it is in the middle of a migration.
567 * If second_map is:
568 * 0 - we return the first map
569 * 1 - we return the second map if it exists, else NULL
570 * -1 - we return the second map if it exists, else the first
571 */
572 struct imsm_map *map = &dev->vol.map[0];
573
574 if (second_map == 1 && !dev->vol.migr_state)
575 return NULL;
576 else if (second_map == 1 ||
577 (second_map < 0 && dev->vol.migr_state)) {
578 void *ptr = map;
579
580 return ptr + sizeof_imsm_map(map);
581 } else
582 return map;
583
584 }
585
586 /* return the size of the device.
587 * migr_state increases the returned size if map[0] were to be duplicated
588 */
589 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
590 {
591 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
592 sizeof_imsm_map(get_imsm_map(dev, 0));
593
594 /* migrating means an additional map */
595 if (dev->vol.migr_state)
596 size += sizeof_imsm_map(get_imsm_map(dev, 1));
597 else if (migr_state)
598 size += sizeof_imsm_map(get_imsm_map(dev, 0));
599
600 return size;
601 }
602
603 #ifndef MDASSEMBLE
604 /* retrieve disk serial number list from a metadata update */
605 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
606 {
607 void *u = update;
608 struct disk_info *inf;
609
610 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
611 sizeof_imsm_dev(&update->dev, 0);
612
613 return inf;
614 }
615 #endif
616
617 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
618 {
619 int offset;
620 int i;
621 void *_mpb = mpb;
622
623 if (index >= mpb->num_raid_devs)
624 return NULL;
625
626 /* devices start after all disks */
627 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
628
629 for (i = 0; i <= index; i++)
630 if (i == index)
631 return _mpb + offset;
632 else
633 offset += sizeof_imsm_dev(_mpb + offset, 0);
634
635 return NULL;
636 }
637
638 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
639 {
640 struct intel_dev *dv;
641
642 if (index >= super->anchor->num_raid_devs)
643 return NULL;
644 for (dv = super->devlist; dv; dv = dv->next)
645 if (dv->index == index)
646 return dv->dev;
647 return NULL;
648 }
649
650 /*
651 * for second_map:
652 * == 0 get first map
653 * == 1 get second map
654 * == -1 than get map according to the current migr_state
655 */
656 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
657 int slot,
658 int second_map)
659 {
660 struct imsm_map *map;
661
662 map = get_imsm_map(dev, second_map);
663
664 /* top byte identifies disk under rebuild */
665 return __le32_to_cpu(map->disk_ord_tbl[slot]);
666 }
667
668 #define ord_to_idx(ord) (((ord) << 8) >> 8)
669 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
670 {
671 __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
672
673 return ord_to_idx(ord);
674 }
675
676 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
677 {
678 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
679 }
680
681 static int get_imsm_disk_slot(struct imsm_map *map, unsigned idx)
682 {
683 int slot;
684 __u32 ord;
685
686 for (slot = 0; slot < map->num_members; slot++) {
687 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
688 if (ord_to_idx(ord) == idx)
689 return slot;
690 }
691
692 return -1;
693 }
694
695 static int get_imsm_raid_level(struct imsm_map *map)
696 {
697 if (map->raid_level == 1) {
698 if (map->num_members == 2)
699 return 1;
700 else
701 return 10;
702 }
703
704 return map->raid_level;
705 }
706
707 static int cmp_extent(const void *av, const void *bv)
708 {
709 const struct extent *a = av;
710 const struct extent *b = bv;
711 if (a->start < b->start)
712 return -1;
713 if (a->start > b->start)
714 return 1;
715 return 0;
716 }
717
718 static int count_memberships(struct dl *dl, struct intel_super *super)
719 {
720 int memberships = 0;
721 int i;
722
723 for (i = 0; i < super->anchor->num_raid_devs; i++) {
724 struct imsm_dev *dev = get_imsm_dev(super, i);
725 struct imsm_map *map = get_imsm_map(dev, 0);
726
727 if (get_imsm_disk_slot(map, dl->index) >= 0)
728 memberships++;
729 }
730
731 return memberships;
732 }
733
734 static struct extent *get_extents(struct intel_super *super, struct dl *dl)
735 {
736 /* find a list of used extents on the given physical device */
737 struct extent *rv, *e;
738 int i;
739 int memberships = count_memberships(dl, super);
740 __u32 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
741
742 rv = malloc(sizeof(struct extent) * (memberships + 1));
743 if (!rv)
744 return NULL;
745 e = rv;
746
747 for (i = 0; i < super->anchor->num_raid_devs; i++) {
748 struct imsm_dev *dev = get_imsm_dev(super, i);
749 struct imsm_map *map = get_imsm_map(dev, 0);
750
751 if (get_imsm_disk_slot(map, dl->index) >= 0) {
752 e->start = __le32_to_cpu(map->pba_of_lba0);
753 e->size = __le32_to_cpu(map->blocks_per_member);
754 e++;
755 }
756 }
757 qsort(rv, memberships, sizeof(*rv), cmp_extent);
758
759 /* determine the start of the metadata
760 * when no raid devices are defined use the default
761 * ...otherwise allow the metadata to truncate the value
762 * as is the case with older versions of imsm
763 */
764 if (memberships) {
765 struct extent *last = &rv[memberships - 1];
766 __u32 remainder;
767
768 remainder = __le32_to_cpu(dl->disk.total_blocks) -
769 (last->start + last->size);
770 /* round down to 1k block to satisfy precision of the kernel
771 * 'size' interface
772 */
773 remainder &= ~1UL;
774 /* make sure remainder is still sane */
775 if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
776 remainder = ROUND_UP(super->len, 512) >> 9;
777 if (reservation > remainder)
778 reservation = remainder;
779 }
780 e->start = __le32_to_cpu(dl->disk.total_blocks) - reservation;
781 e->size = 0;
782 return rv;
783 }
784
785 /* try to determine how much space is reserved for metadata from
786 * the last get_extents() entry, otherwise fallback to the
787 * default
788 */
789 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
790 {
791 struct extent *e;
792 int i;
793 __u32 rv;
794
795 /* for spares just return a minimal reservation which will grow
796 * once the spare is picked up by an array
797 */
798 if (dl->index == -1)
799 return MPB_SECTOR_CNT;
800
801 e = get_extents(super, dl);
802 if (!e)
803 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
804
805 /* scroll to last entry */
806 for (i = 0; e[i].size; i++)
807 continue;
808
809 rv = __le32_to_cpu(dl->disk.total_blocks) - e[i].start;
810
811 free(e);
812
813 return rv;
814 }
815
816 static int is_spare(struct imsm_disk *disk)
817 {
818 return (disk->status & SPARE_DISK) == SPARE_DISK;
819 }
820
821 static int is_configured(struct imsm_disk *disk)
822 {
823 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
824 }
825
826 static int is_failed(struct imsm_disk *disk)
827 {
828 return (disk->status & FAILED_DISK) == FAILED_DISK;
829 }
830
831 /* Return minimum size of a spare that can be used in this array*/
832 static unsigned long long min_acceptable_spare_size_imsm(struct supertype *st)
833 {
834 struct intel_super *super = st->sb;
835 struct dl *dl;
836 struct extent *e;
837 int i;
838 unsigned long long rv = 0;
839
840 if (!super)
841 return rv;
842 /* find first active disk in array */
843 dl = super->disks;
844 while (dl && (is_failed(&dl->disk) || dl->index == -1))
845 dl = dl->next;
846 if (!dl)
847 return rv;
848 /* find last lba used by subarrays */
849 e = get_extents(super, dl);
850 if (!e)
851 return rv;
852 for (i = 0; e[i].size; i++)
853 continue;
854 if (i > 0)
855 rv = e[i-1].start + e[i-1].size;
856 free(e);
857 /* add the amount of space needed for metadata */
858 rv = rv + MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
859 return rv * 512;
860 }
861
862 #ifndef MDASSEMBLE
863 static __u64 blocks_per_migr_unit(struct imsm_dev *dev);
864
865 static void print_imsm_dev(struct imsm_dev *dev, char *uuid, int disk_idx)
866 {
867 __u64 sz;
868 int slot, i;
869 struct imsm_map *map = get_imsm_map(dev, 0);
870 struct imsm_map *map2 = get_imsm_map(dev, 1);
871 __u32 ord;
872
873 printf("\n");
874 printf("[%.16s]:\n", dev->volume);
875 printf(" UUID : %s\n", uuid);
876 printf(" RAID Level : %d", get_imsm_raid_level(map));
877 if (map2)
878 printf(" <-- %d", get_imsm_raid_level(map2));
879 printf("\n");
880 printf(" Members : %d", map->num_members);
881 if (map2)
882 printf(" <-- %d", map2->num_members);
883 printf("\n");
884 printf(" Slots : [");
885 for (i = 0; i < map->num_members; i++) {
886 ord = get_imsm_ord_tbl_ent(dev, i, 0);
887 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
888 }
889 printf("]");
890 if (map2) {
891 printf(" <-- [");
892 for (i = 0; i < map2->num_members; i++) {
893 ord = get_imsm_ord_tbl_ent(dev, i, 1);
894 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
895 }
896 printf("]");
897 }
898 printf("\n");
899 printf(" Failed disk : ");
900 if (map->failed_disk_num == 0xff)
901 printf("none");
902 else
903 printf("%i", map->failed_disk_num);
904 printf("\n");
905 slot = get_imsm_disk_slot(map, disk_idx);
906 if (slot >= 0) {
907 ord = get_imsm_ord_tbl_ent(dev, slot, -1);
908 printf(" This Slot : %d%s\n", slot,
909 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
910 } else
911 printf(" This Slot : ?\n");
912 sz = __le32_to_cpu(dev->size_high);
913 sz <<= 32;
914 sz += __le32_to_cpu(dev->size_low);
915 printf(" Array Size : %llu%s\n", (unsigned long long)sz,
916 human_size(sz * 512));
917 sz = __le32_to_cpu(map->blocks_per_member);
918 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz,
919 human_size(sz * 512));
920 printf(" Sector Offset : %u\n",
921 __le32_to_cpu(map->pba_of_lba0));
922 printf(" Num Stripes : %u\n",
923 __le32_to_cpu(map->num_data_stripes));
924 printf(" Chunk Size : %u KiB",
925 __le16_to_cpu(map->blocks_per_strip) / 2);
926 if (map2)
927 printf(" <-- %u KiB",
928 __le16_to_cpu(map2->blocks_per_strip) / 2);
929 printf("\n");
930 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
931 printf(" Migrate State : ");
932 if (dev->vol.migr_state) {
933 if (migr_type(dev) == MIGR_INIT)
934 printf("initialize\n");
935 else if (migr_type(dev) == MIGR_REBUILD)
936 printf("rebuild\n");
937 else if (migr_type(dev) == MIGR_VERIFY)
938 printf("check\n");
939 else if (migr_type(dev) == MIGR_GEN_MIGR)
940 printf("general migration\n");
941 else if (migr_type(dev) == MIGR_STATE_CHANGE)
942 printf("state change\n");
943 else if (migr_type(dev) == MIGR_REPAIR)
944 printf("repair\n");
945 else
946 printf("<unknown:%d>\n", migr_type(dev));
947 } else
948 printf("idle\n");
949 printf(" Map State : %s", map_state_str[map->map_state]);
950 if (dev->vol.migr_state) {
951 struct imsm_map *map = get_imsm_map(dev, 1);
952
953 printf(" <-- %s", map_state_str[map->map_state]);
954 printf("\n Checkpoint : %u (%llu)",
955 __le32_to_cpu(dev->vol.curr_migr_unit),
956 (unsigned long long)blocks_per_migr_unit(dev));
957 }
958 printf("\n");
959 printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
960 }
961
962 static void print_imsm_disk(struct imsm_super *mpb, int index, __u32 reserved)
963 {
964 struct imsm_disk *disk = __get_imsm_disk(mpb, index);
965 char str[MAX_RAID_SERIAL_LEN + 1];
966 __u64 sz;
967
968 if (index < 0 || !disk)
969 return;
970
971 printf("\n");
972 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
973 printf(" Disk%02d Serial : %s\n", index, str);
974 printf(" State :%s%s%s\n", is_spare(disk) ? " spare" : "",
975 is_configured(disk) ? " active" : "",
976 is_failed(disk) ? " failed" : "");
977 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
978 sz = __le32_to_cpu(disk->total_blocks) - reserved;
979 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
980 human_size(sz * 512));
981 }
982
983 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
984
985 static void examine_super_imsm(struct supertype *st, char *homehost)
986 {
987 struct intel_super *super = st->sb;
988 struct imsm_super *mpb = super->anchor;
989 char str[MAX_SIGNATURE_LENGTH];
990 int i;
991 struct mdinfo info;
992 char nbuf[64];
993 __u32 sum;
994 __u32 reserved = imsm_reserved_sectors(super, super->disks);
995 struct dl *dl;
996
997 snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
998 printf(" Magic : %s\n", str);
999 snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
1000 printf(" Version : %s\n", get_imsm_version(mpb));
1001 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
1002 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
1003 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
1004 getinfo_super_imsm(st, &info, NULL);
1005 fname_from_uuid(st, &info, nbuf, ':');
1006 printf(" UUID : %s\n", nbuf + 5);
1007 sum = __le32_to_cpu(mpb->check_sum);
1008 printf(" Checksum : %08x %s\n", sum,
1009 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
1010 printf(" MPB Sectors : %d\n", mpb_sectors(mpb));
1011 printf(" Disks : %d\n", mpb->num_disks);
1012 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
1013 print_imsm_disk(mpb, super->disks->index, reserved);
1014 if (super->bbm_log) {
1015 struct bbm_log *log = super->bbm_log;
1016
1017 printf("\n");
1018 printf("Bad Block Management Log:\n");
1019 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
1020 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
1021 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
1022 printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count));
1023 printf(" First Spare : %llx\n",
1024 (unsigned long long) __le64_to_cpu(log->first_spare_lba));
1025 }
1026 for (i = 0; i < mpb->num_raid_devs; i++) {
1027 struct mdinfo info;
1028 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
1029
1030 super->current_vol = i;
1031 getinfo_super_imsm(st, &info, NULL);
1032 fname_from_uuid(st, &info, nbuf, ':');
1033 print_imsm_dev(dev, nbuf + 5, super->disks->index);
1034 }
1035 for (i = 0; i < mpb->num_disks; i++) {
1036 if (i == super->disks->index)
1037 continue;
1038 print_imsm_disk(mpb, i, reserved);
1039 }
1040 for (dl = super->disks ; dl; dl = dl->next) {
1041 struct imsm_disk *disk;
1042 char str[MAX_RAID_SERIAL_LEN + 1];
1043 __u64 sz;
1044
1045 if (dl->index >= 0)
1046 continue;
1047
1048 disk = &dl->disk;
1049 printf("\n");
1050 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
1051 printf(" Disk Serial : %s\n", str);
1052 printf(" State :%s%s%s\n", is_spare(disk) ? " spare" : "",
1053 is_configured(disk) ? " active" : "",
1054 is_failed(disk) ? " failed" : "");
1055 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
1056 sz = __le32_to_cpu(disk->total_blocks) - reserved;
1057 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
1058 human_size(sz * 512));
1059 }
1060 }
1061
1062 static void brief_examine_super_imsm(struct supertype *st, int verbose)
1063 {
1064 /* We just write a generic IMSM ARRAY entry */
1065 struct mdinfo info;
1066 char nbuf[64];
1067 struct intel_super *super = st->sb;
1068
1069 if (!super->anchor->num_raid_devs) {
1070 printf("ARRAY metadata=imsm\n");
1071 return;
1072 }
1073
1074 getinfo_super_imsm(st, &info, NULL);
1075 fname_from_uuid(st, &info, nbuf, ':');
1076 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
1077 }
1078
1079 static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
1080 {
1081 /* We just write a generic IMSM ARRAY entry */
1082 struct mdinfo info;
1083 char nbuf[64];
1084 char nbuf1[64];
1085 struct intel_super *super = st->sb;
1086 int i;
1087
1088 if (!super->anchor->num_raid_devs)
1089 return;
1090
1091 getinfo_super_imsm(st, &info, NULL);
1092 fname_from_uuid(st, &info, nbuf, ':');
1093 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1094 struct imsm_dev *dev = get_imsm_dev(super, i);
1095
1096 super->current_vol = i;
1097 getinfo_super_imsm(st, &info, NULL);
1098 fname_from_uuid(st, &info, nbuf1, ':');
1099 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1100 dev->volume, nbuf + 5, i, nbuf1 + 5);
1101 }
1102 }
1103
1104 static void export_examine_super_imsm(struct supertype *st)
1105 {
1106 struct intel_super *super = st->sb;
1107 struct imsm_super *mpb = super->anchor;
1108 struct mdinfo info;
1109 char nbuf[64];
1110
1111 getinfo_super_imsm(st, &info, NULL);
1112 fname_from_uuid(st, &info, nbuf, ':');
1113 printf("MD_METADATA=imsm\n");
1114 printf("MD_LEVEL=container\n");
1115 printf("MD_UUID=%s\n", nbuf+5);
1116 printf("MD_DEVICES=%u\n", mpb->num_disks);
1117 }
1118
1119 static void detail_super_imsm(struct supertype *st, char *homehost)
1120 {
1121 struct mdinfo info;
1122 char nbuf[64];
1123
1124 getinfo_super_imsm(st, &info, NULL);
1125 fname_from_uuid(st, &info, nbuf, ':');
1126 printf("\n UUID : %s\n", nbuf + 5);
1127 }
1128
1129 static void brief_detail_super_imsm(struct supertype *st)
1130 {
1131 struct mdinfo info;
1132 char nbuf[64];
1133 getinfo_super_imsm(st, &info, NULL);
1134 fname_from_uuid(st, &info, nbuf, ':');
1135 printf(" UUID=%s", nbuf + 5);
1136 }
1137
1138 static int imsm_read_serial(int fd, char *devname, __u8 *serial);
1139 static void fd2devname(int fd, char *name);
1140
1141 static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
1142 {
1143 /* dump an unsorted list of devices attached to AHCI Intel storage
1144 * controller, as well as non-connected ports
1145 */
1146 int hba_len = strlen(hba_path) + 1;
1147 struct dirent *ent;
1148 DIR *dir;
1149 char *path = NULL;
1150 int err = 0;
1151 unsigned long port_mask = (1 << port_count) - 1;
1152
1153 if (port_count > (int)sizeof(port_mask) * 8) {
1154 if (verbose)
1155 fprintf(stderr, Name ": port_count %d out of range\n", port_count);
1156 return 2;
1157 }
1158
1159 /* scroll through /sys/dev/block looking for devices attached to
1160 * this hba
1161 */
1162 dir = opendir("/sys/dev/block");
1163 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
1164 int fd;
1165 char model[64];
1166 char vendor[64];
1167 char buf[1024];
1168 int major, minor;
1169 char *device;
1170 char *c;
1171 int port;
1172 int type;
1173
1174 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
1175 continue;
1176 path = devt_to_devpath(makedev(major, minor));
1177 if (!path)
1178 continue;
1179 if (!path_attached_to_hba(path, hba_path)) {
1180 free(path);
1181 path = NULL;
1182 continue;
1183 }
1184
1185 /* retrieve the scsi device type */
1186 if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) {
1187 if (verbose)
1188 fprintf(stderr, Name ": failed to allocate 'device'\n");
1189 err = 2;
1190 break;
1191 }
1192 sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
1193 if (load_sys(device, buf) != 0) {
1194 if (verbose)
1195 fprintf(stderr, Name ": failed to read device type for %s\n",
1196 path);
1197 err = 2;
1198 free(device);
1199 break;
1200 }
1201 type = strtoul(buf, NULL, 10);
1202
1203 /* if it's not a disk print the vendor and model */
1204 if (!(type == 0 || type == 7 || type == 14)) {
1205 vendor[0] = '\0';
1206 model[0] = '\0';
1207 sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
1208 if (load_sys(device, buf) == 0) {
1209 strncpy(vendor, buf, sizeof(vendor));
1210 vendor[sizeof(vendor) - 1] = '\0';
1211 c = (char *) &vendor[sizeof(vendor) - 1];
1212 while (isspace(*c) || *c == '\0')
1213 *c-- = '\0';
1214
1215 }
1216 sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
1217 if (load_sys(device, buf) == 0) {
1218 strncpy(model, buf, sizeof(model));
1219 model[sizeof(model) - 1] = '\0';
1220 c = (char *) &model[sizeof(model) - 1];
1221 while (isspace(*c) || *c == '\0')
1222 *c-- = '\0';
1223 }
1224
1225 if (vendor[0] && model[0])
1226 sprintf(buf, "%.64s %.64s", vendor, model);
1227 else
1228 switch (type) { /* numbers from hald/linux/device.c */
1229 case 1: sprintf(buf, "tape"); break;
1230 case 2: sprintf(buf, "printer"); break;
1231 case 3: sprintf(buf, "processor"); break;
1232 case 4:
1233 case 5: sprintf(buf, "cdrom"); break;
1234 case 6: sprintf(buf, "scanner"); break;
1235 case 8: sprintf(buf, "media_changer"); break;
1236 case 9: sprintf(buf, "comm"); break;
1237 case 12: sprintf(buf, "raid"); break;
1238 default: sprintf(buf, "unknown");
1239 }
1240 } else
1241 buf[0] = '\0';
1242 free(device);
1243
1244 /* chop device path to 'host%d' and calculate the port number */
1245 c = strchr(&path[hba_len], '/');
1246 if (!c) {
1247 if (verbose)
1248 fprintf(stderr, Name ": %s - invalid path name\n", path + hba_len);
1249 err = 2;
1250 break;
1251 }
1252 *c = '\0';
1253 if (sscanf(&path[hba_len], "host%d", &port) == 1)
1254 port -= host_base;
1255 else {
1256 if (verbose) {
1257 *c = '/'; /* repair the full string */
1258 fprintf(stderr, Name ": failed to determine port number for %s\n",
1259 path);
1260 }
1261 err = 2;
1262 break;
1263 }
1264
1265 /* mark this port as used */
1266 port_mask &= ~(1 << port);
1267
1268 /* print out the device information */
1269 if (buf[0]) {
1270 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
1271 continue;
1272 }
1273
1274 fd = dev_open(ent->d_name, O_RDONLY);
1275 if (fd < 0)
1276 printf(" Port%d : - disk info unavailable -\n", port);
1277 else {
1278 fd2devname(fd, buf);
1279 printf(" Port%d : %s", port, buf);
1280 if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0)
1281 printf(" (%s)\n", buf);
1282 else
1283 printf("()\n");
1284 }
1285 close(fd);
1286 free(path);
1287 path = NULL;
1288 }
1289 if (path)
1290 free(path);
1291 if (dir)
1292 closedir(dir);
1293 if (err == 0) {
1294 int i;
1295
1296 for (i = 0; i < port_count; i++)
1297 if (port_mask & (1 << i))
1298 printf(" Port%d : - no device attached -\n", i);
1299 }
1300
1301 return err;
1302 }
1303
1304
1305
1306 static void print_found_intel_controllers(struct sys_dev *elem)
1307 {
1308 for (; elem; elem = elem->next) {
1309 fprintf(stderr, Name ": found Intel(R) ");
1310 if (elem->type == SYS_DEV_SATA)
1311 fprintf(stderr, "SATA ");
1312 else if (elem->type == SYS_DEV_SAS)
1313 fprintf(stderr, "SAS ");
1314 fprintf(stderr, "RAID controller");
1315 if (elem->pci_id)
1316 fprintf(stderr, " at %s", elem->pci_id);
1317 fprintf(stderr, ".\n");
1318 }
1319 fflush(stderr);
1320 }
1321
1322 static int ahci_get_port_count(const char *hba_path, int *port_count)
1323 {
1324 struct dirent *ent;
1325 DIR *dir;
1326 int host_base = -1;
1327
1328 *port_count = 0;
1329 if ((dir = opendir(hba_path)) == NULL)
1330 return -1;
1331
1332 for (ent = readdir(dir); ent; ent = readdir(dir)) {
1333 int host;
1334
1335 if (sscanf(ent->d_name, "host%d", &host) != 1)
1336 continue;
1337 if (*port_count == 0)
1338 host_base = host;
1339 else if (host < host_base)
1340 host_base = host;
1341
1342 if (host + 1 > *port_count + host_base)
1343 *port_count = host + 1 - host_base;
1344 }
1345 closedir(dir);
1346 return host_base;
1347 }
1348
1349 static void print_imsm_capability(const struct imsm_orom *orom)
1350 {
1351 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1352 printf(" Version : %d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
1353 orom->hotfix_ver, orom->build);
1354 printf(" RAID Levels :%s%s%s%s%s\n",
1355 imsm_orom_has_raid0(orom) ? " raid0" : "",
1356 imsm_orom_has_raid1(orom) ? " raid1" : "",
1357 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
1358 imsm_orom_has_raid10(orom) ? " raid10" : "",
1359 imsm_orom_has_raid5(orom) ? " raid5" : "");
1360 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1361 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
1362 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
1363 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
1364 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
1365 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
1366 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
1367 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
1368 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
1369 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
1370 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
1371 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
1372 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
1373 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
1374 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
1375 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
1376 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
1377 printf(" Max Disks : %d\n", orom->tds);
1378 printf(" Max Volumes : %d\n", orom->vpa);
1379 return;
1380 }
1381
1382 static int detail_platform_imsm(int verbose, int enumerate_only)
1383 {
1384 /* There are two components to imsm platform support, the ahci SATA
1385 * controller and the option-rom. To find the SATA controller we
1386 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1387 * controller with the Intel vendor id is present. This approach
1388 * allows mdadm to leverage the kernel's ahci detection logic, with the
1389 * caveat that if ahci.ko is not loaded mdadm will not be able to
1390 * detect platform raid capabilities. The option-rom resides in a
1391 * platform "Adapter ROM". We scan for its signature to retrieve the
1392 * platform capabilities. If raid support is disabled in the BIOS the
1393 * option-rom capability structure will not be available.
1394 */
1395 const struct imsm_orom *orom;
1396 struct sys_dev *list, *hba;
1397 int host_base = 0;
1398 int port_count = 0;
1399 int result=0;
1400
1401 if (enumerate_only) {
1402 if (check_env("IMSM_NO_PLATFORM"))
1403 return 0;
1404 list = find_intel_devices();
1405 if (!list)
1406 return 2;
1407 for (hba = list; hba; hba = hba->next) {
1408 orom = find_imsm_capability(hba->type);
1409 if (!orom) {
1410 result = 2;
1411 break;
1412 }
1413 }
1414 free_sys_dev(&list);
1415 return result;
1416 }
1417
1418 list = find_intel_devices();
1419 if (!list) {
1420 if (verbose)
1421 fprintf(stderr, Name ": no active Intel(R) RAID "
1422 "controller found.\n");
1423 free_sys_dev(&list);
1424 return 2;
1425 } else if (verbose)
1426 print_found_intel_controllers(list);
1427
1428 for (hba = list; hba; hba = hba->next) {
1429 orom = find_imsm_capability(hba->type);
1430 if (!orom)
1431 fprintf(stderr, Name ": imsm capabilities not found for controller: %s (type %s)\n",
1432 hba->path, get_sys_dev_type(hba->type));
1433 else
1434 print_imsm_capability(orom);
1435 }
1436
1437 for (hba = list; hba; hba = hba->next) {
1438 printf(" I/O Controller : %s (%s)\n",
1439 hba->path, get_sys_dev_type(hba->type));
1440
1441 if (hba->type == SYS_DEV_SATA) {
1442 host_base = ahci_get_port_count(hba->path, &port_count);
1443 if (ahci_enumerate_ports(hba->path, port_count, host_base, verbose)) {
1444 if (verbose)
1445 fprintf(stderr, Name ": failed to enumerate "
1446 "ports on SATA controller at %s.", hba->pci_id);
1447 result |= 2;
1448 }
1449 }
1450 }
1451
1452 free_sys_dev(&list);
1453 return result;
1454 }
1455 #endif
1456
1457 static int match_home_imsm(struct supertype *st, char *homehost)
1458 {
1459 /* the imsm metadata format does not specify any host
1460 * identification information. We return -1 since we can never
1461 * confirm nor deny whether a given array is "meant" for this
1462 * host. We rely on compare_super and the 'family_num' fields to
1463 * exclude member disks that do not belong, and we rely on
1464 * mdadm.conf to specify the arrays that should be assembled.
1465 * Auto-assembly may still pick up "foreign" arrays.
1466 */
1467
1468 return -1;
1469 }
1470
1471 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
1472 {
1473 /* The uuid returned here is used for:
1474 * uuid to put into bitmap file (Create, Grow)
1475 * uuid for backup header when saving critical section (Grow)
1476 * comparing uuids when re-adding a device into an array
1477 * In these cases the uuid required is that of the data-array,
1478 * not the device-set.
1479 * uuid to recognise same set when adding a missing device back
1480 * to an array. This is a uuid for the device-set.
1481 *
1482 * For each of these we can make do with a truncated
1483 * or hashed uuid rather than the original, as long as
1484 * everyone agrees.
1485 * In each case the uuid required is that of the data-array,
1486 * not the device-set.
1487 */
1488 /* imsm does not track uuid's so we synthesis one using sha1 on
1489 * - The signature (Which is constant for all imsm array, but no matter)
1490 * - the orig_family_num of the container
1491 * - the index number of the volume
1492 * - the 'serial' number of the volume.
1493 * Hopefully these are all constant.
1494 */
1495 struct intel_super *super = st->sb;
1496
1497 char buf[20];
1498 struct sha1_ctx ctx;
1499 struct imsm_dev *dev = NULL;
1500 __u32 family_num;
1501
1502 /* some mdadm versions failed to set ->orig_family_num, in which
1503 * case fall back to ->family_num. orig_family_num will be
1504 * fixed up with the first metadata update.
1505 */
1506 family_num = super->anchor->orig_family_num;
1507 if (family_num == 0)
1508 family_num = super->anchor->family_num;
1509 sha1_init_ctx(&ctx);
1510 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
1511 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
1512 if (super->current_vol >= 0)
1513 dev = get_imsm_dev(super, super->current_vol);
1514 if (dev) {
1515 __u32 vol = super->current_vol;
1516 sha1_process_bytes(&vol, sizeof(vol), &ctx);
1517 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
1518 }
1519 sha1_finish_ctx(&ctx, buf);
1520 memcpy(uuid, buf, 4*4);
1521 }
1522
1523 #if 0
1524 static void
1525 get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
1526 {
1527 __u8 *v = get_imsm_version(mpb);
1528 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
1529 char major[] = { 0, 0, 0 };
1530 char minor[] = { 0 ,0, 0 };
1531 char patch[] = { 0, 0, 0 };
1532 char *ver_parse[] = { major, minor, patch };
1533 int i, j;
1534
1535 i = j = 0;
1536 while (*v != '\0' && v < end) {
1537 if (*v != '.' && j < 2)
1538 ver_parse[i][j++] = *v;
1539 else {
1540 i++;
1541 j = 0;
1542 }
1543 v++;
1544 }
1545
1546 *m = strtol(minor, NULL, 0);
1547 *p = strtol(patch, NULL, 0);
1548 }
1549 #endif
1550
1551 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
1552 {
1553 /* migr_strip_size when repairing or initializing parity */
1554 struct imsm_map *map = get_imsm_map(dev, 0);
1555 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1556
1557 switch (get_imsm_raid_level(map)) {
1558 case 5:
1559 case 10:
1560 return chunk;
1561 default:
1562 return 128*1024 >> 9;
1563 }
1564 }
1565
1566 static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
1567 {
1568 /* migr_strip_size when rebuilding a degraded disk, no idea why
1569 * this is different than migr_strip_size_resync(), but it's good
1570 * to be compatible
1571 */
1572 struct imsm_map *map = get_imsm_map(dev, 1);
1573 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1574
1575 switch (get_imsm_raid_level(map)) {
1576 case 1:
1577 case 10:
1578 if (map->num_members % map->num_domains == 0)
1579 return 128*1024 >> 9;
1580 else
1581 return chunk;
1582 case 5:
1583 return max((__u32) 64*1024 >> 9, chunk);
1584 default:
1585 return 128*1024 >> 9;
1586 }
1587 }
1588
1589 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
1590 {
1591 struct imsm_map *lo = get_imsm_map(dev, 0);
1592 struct imsm_map *hi = get_imsm_map(dev, 1);
1593 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
1594 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
1595
1596 return max((__u32) 1, hi_chunk / lo_chunk);
1597 }
1598
1599 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
1600 {
1601 struct imsm_map *lo = get_imsm_map(dev, 0);
1602 int level = get_imsm_raid_level(lo);
1603
1604 if (level == 1 || level == 10) {
1605 struct imsm_map *hi = get_imsm_map(dev, 1);
1606
1607 return hi->num_domains;
1608 } else
1609 return num_stripes_per_unit_resync(dev);
1610 }
1611
1612 static __u8 imsm_num_data_members(struct imsm_dev *dev, int second_map)
1613 {
1614 /* named 'imsm_' because raid0, raid1 and raid10
1615 * counter-intuitively have the same number of data disks
1616 */
1617 struct imsm_map *map = get_imsm_map(dev, second_map);
1618
1619 switch (get_imsm_raid_level(map)) {
1620 case 0:
1621 case 1:
1622 case 10:
1623 return map->num_members;
1624 case 5:
1625 return map->num_members - 1;
1626 default:
1627 dprintf("%s: unsupported raid level\n", __func__);
1628 return 0;
1629 }
1630 }
1631
1632 static __u32 parity_segment_depth(struct imsm_dev *dev)
1633 {
1634 struct imsm_map *map = get_imsm_map(dev, 0);
1635 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1636
1637 switch(get_imsm_raid_level(map)) {
1638 case 1:
1639 case 10:
1640 return chunk * map->num_domains;
1641 case 5:
1642 return chunk * map->num_members;
1643 default:
1644 return chunk;
1645 }
1646 }
1647
1648 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
1649 {
1650 struct imsm_map *map = get_imsm_map(dev, 1);
1651 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1652 __u32 strip = block / chunk;
1653
1654 switch (get_imsm_raid_level(map)) {
1655 case 1:
1656 case 10: {
1657 __u32 vol_strip = (strip * map->num_domains) + 1;
1658 __u32 vol_stripe = vol_strip / map->num_members;
1659
1660 return vol_stripe * chunk + block % chunk;
1661 } case 5: {
1662 __u32 stripe = strip / (map->num_members - 1);
1663
1664 return stripe * chunk + block % chunk;
1665 }
1666 default:
1667 return 0;
1668 }
1669 }
1670
1671 static __u64 blocks_per_migr_unit(struct imsm_dev *dev)
1672 {
1673 /* calculate the conversion factor between per member 'blocks'
1674 * (md/{resync,rebuild}_start) and imsm migration units, return
1675 * 0 for the 'not migrating' and 'unsupported migration' cases
1676 */
1677 if (!dev->vol.migr_state)
1678 return 0;
1679
1680 switch (migr_type(dev)) {
1681 case MIGR_GEN_MIGR:
1682 case MIGR_VERIFY:
1683 case MIGR_REPAIR:
1684 case MIGR_INIT: {
1685 struct imsm_map *map = get_imsm_map(dev, 0);
1686 __u32 stripes_per_unit;
1687 __u32 blocks_per_unit;
1688 __u32 parity_depth;
1689 __u32 migr_chunk;
1690 __u32 block_map;
1691 __u32 block_rel;
1692 __u32 segment;
1693 __u32 stripe;
1694 __u8 disks;
1695
1696 /* yes, this is really the translation of migr_units to
1697 * per-member blocks in the 'resync' case
1698 */
1699 stripes_per_unit = num_stripes_per_unit_resync(dev);
1700 migr_chunk = migr_strip_blocks_resync(dev);
1701 disks = imsm_num_data_members(dev, 0);
1702 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
1703 if (migr_type(dev) == MIGR_GEN_MIGR)
1704 return blocks_per_unit;
1705 stripe = __le32_to_cpu(map->blocks_per_strip) * disks;
1706 segment = blocks_per_unit / stripe;
1707 block_rel = blocks_per_unit - segment * stripe;
1708 parity_depth = parity_segment_depth(dev);
1709 block_map = map_migr_block(dev, block_rel);
1710 return block_map + parity_depth * segment;
1711 }
1712 case MIGR_REBUILD: {
1713 __u32 stripes_per_unit;
1714 __u32 migr_chunk;
1715
1716 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
1717 migr_chunk = migr_strip_blocks_rebuild(dev);
1718 return migr_chunk * stripes_per_unit;
1719 }
1720 case MIGR_STATE_CHANGE:
1721 default:
1722 return 0;
1723 }
1724 }
1725
1726 static int imsm_level_to_layout(int level)
1727 {
1728 switch (level) {
1729 case 0:
1730 case 1:
1731 return 0;
1732 case 5:
1733 case 6:
1734 return ALGORITHM_LEFT_ASYMMETRIC;
1735 case 10:
1736 return 0x102;
1737 }
1738 return UnSet;
1739 }
1740
1741 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
1742 {
1743 struct intel_super *super = st->sb;
1744 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
1745 struct imsm_map *map = get_imsm_map(dev, 0);
1746 struct imsm_map *prev_map = get_imsm_map(dev, 1);
1747 struct imsm_map *map_to_analyse = map;
1748 struct dl *dl;
1749 char *devname;
1750 int map_disks = info->array.raid_disks;
1751
1752 if (prev_map)
1753 map_to_analyse = prev_map;
1754
1755 for (dl = super->disks; dl; dl = dl->next)
1756 if (dl->raiddisk == info->disk.raid_disk)
1757 break;
1758 info->container_member = super->current_vol;
1759 info->array.raid_disks = map_to_analyse->num_members;
1760 info->array.level = get_imsm_raid_level(map_to_analyse);
1761 info->array.layout = imsm_level_to_layout(info->array.level);
1762 info->array.md_minor = -1;
1763 info->array.ctime = 0;
1764 info->array.utime = 0;
1765 info->array.chunk_size =
1766 __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
1767 info->array.state = !dev->vol.dirty;
1768 info->custom_array_size = __le32_to_cpu(dev->size_high);
1769 info->custom_array_size <<= 32;
1770 info->custom_array_size |= __le32_to_cpu(dev->size_low);
1771 if (prev_map && map->map_state == prev_map->map_state) {
1772 info->reshape_active = 1;
1773 info->new_level = get_imsm_raid_level(map);
1774 info->new_layout = imsm_level_to_layout(info->new_level);
1775 info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
1776 info->delta_disks = map->num_members - prev_map->num_members;
1777 if (info->delta_disks) {
1778 /* this needs to be applied to every array
1779 * in the container.
1780 */
1781 info->reshape_active = 2;
1782 }
1783 /* We shape information that we give to md might have to be
1784 * modify to cope with md's requirement for reshaping arrays.
1785 * For example, when reshaping a RAID0, md requires it to be
1786 * presented as a degraded RAID4.
1787 * Also if a RAID0 is migrating to a RAID5 we need to specify
1788 * the array as already being RAID5, but the 'before' layout
1789 * is a RAID4-like layout.
1790 */
1791 switch (info->array.level) {
1792 case 0:
1793 switch(info->new_level) {
1794 case 0:
1795 /* conversion is happening as RAID4 */
1796 info->array.level = 4;
1797 info->array.raid_disks += 1;
1798 break;
1799 case 5:
1800 /* conversion is happening as RAID5 */
1801 info->array.level = 5;
1802 info->array.layout = ALGORITHM_PARITY_N;
1803 info->array.raid_disks += 1;
1804 info->delta_disks -= 1;
1805 break;
1806 default:
1807 /* FIXME error message */
1808 info->array.level = UnSet;
1809 break;
1810 }
1811 break;
1812 }
1813 } else {
1814 info->new_level = UnSet;
1815 info->new_layout = UnSet;
1816 info->new_chunk = info->array.chunk_size;
1817 info->delta_disks = 0;
1818 }
1819 info->disk.major = 0;
1820 info->disk.minor = 0;
1821 if (dl) {
1822 info->disk.major = dl->major;
1823 info->disk.minor = dl->minor;
1824 }
1825
1826 info->data_offset = __le32_to_cpu(map_to_analyse->pba_of_lba0);
1827 info->component_size =
1828 __le32_to_cpu(map_to_analyse->blocks_per_member);
1829 memset(info->uuid, 0, sizeof(info->uuid));
1830 info->recovery_start = MaxSector;
1831
1832 info->reshape_progress = 0;
1833 info->resync_start = MaxSector;
1834 if (map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
1835 dev->vol.dirty) {
1836 info->resync_start = 0;
1837 }
1838 if (dev->vol.migr_state) {
1839 switch (migr_type(dev)) {
1840 case MIGR_REPAIR:
1841 case MIGR_INIT: {
1842 __u64 blocks_per_unit = blocks_per_migr_unit(dev);
1843 __u64 units = __le32_to_cpu(dev->vol.curr_migr_unit);
1844
1845 info->resync_start = blocks_per_unit * units;
1846 break;
1847 }
1848 case MIGR_GEN_MIGR: {
1849 __u64 blocks_per_unit = blocks_per_migr_unit(dev);
1850 __u64 units = __le32_to_cpu(dev->vol.curr_migr_unit);
1851 unsigned long long array_blocks;
1852 int used_disks;
1853
1854 info->reshape_progress = blocks_per_unit * units;
1855 dprintf("IMSM: General Migration checkpoint : %llu "
1856 "(%llu) -> read reshape progress : %llu\n",
1857 units, blocks_per_unit, info->reshape_progress);
1858
1859 used_disks = imsm_num_data_members(dev, 1);
1860 if (used_disks > 0) {
1861 array_blocks = map->blocks_per_member *
1862 used_disks;
1863 /* round array size down to closest MB
1864 */
1865 info->custom_array_size = (array_blocks
1866 >> SECT_PER_MB_SHIFT)
1867 << SECT_PER_MB_SHIFT;
1868 }
1869 }
1870 case MIGR_VERIFY:
1871 /* we could emulate the checkpointing of
1872 * 'sync_action=check' migrations, but for now
1873 * we just immediately complete them
1874 */
1875 case MIGR_REBUILD:
1876 /* this is handled by container_content_imsm() */
1877 case MIGR_STATE_CHANGE:
1878 /* FIXME handle other migrations */
1879 default:
1880 /* we are not dirty, so... */
1881 info->resync_start = MaxSector;
1882 }
1883 }
1884
1885 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1886 info->name[MAX_RAID_SERIAL_LEN] = 0;
1887
1888 info->array.major_version = -1;
1889 info->array.minor_version = -2;
1890 devname = devnum2devname(st->container_dev);
1891 *info->text_version = '\0';
1892 if (devname)
1893 sprintf(info->text_version, "/%s/%d", devname, info->container_member);
1894 free(devname);
1895 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
1896 uuid_from_super_imsm(st, info->uuid);
1897
1898 if (dmap) {
1899 int i, j;
1900 for (i=0; i<map_disks; i++) {
1901 dmap[i] = 0;
1902 if (i < info->array.raid_disks) {
1903 struct imsm_disk *dsk;
1904 j = get_imsm_disk_idx(dev, i, -1);
1905 dsk = get_imsm_disk(super, j);
1906 if (dsk && (dsk->status & CONFIGURED_DISK))
1907 dmap[i] = 1;
1908 }
1909 }
1910 }
1911 }
1912
1913 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed);
1914 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev);
1915
1916 static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
1917 {
1918 struct dl *d;
1919
1920 for (d = super->missing; d; d = d->next)
1921 if (d->index == index)
1922 return &d->disk;
1923 return NULL;
1924 }
1925
1926 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
1927 {
1928 struct intel_super *super = st->sb;
1929 struct imsm_disk *disk;
1930 int map_disks = info->array.raid_disks;
1931 int max_enough = -1;
1932 int i;
1933 struct imsm_super *mpb;
1934
1935 if (super->current_vol >= 0) {
1936 getinfo_super_imsm_volume(st, info, map);
1937 return;
1938 }
1939
1940 /* Set raid_disks to zero so that Assemble will always pull in valid
1941 * spares
1942 */
1943 info->array.raid_disks = 0;
1944 info->array.level = LEVEL_CONTAINER;
1945 info->array.layout = 0;
1946 info->array.md_minor = -1;
1947 info->array.ctime = 0; /* N/A for imsm */
1948 info->array.utime = 0;
1949 info->array.chunk_size = 0;
1950
1951 info->disk.major = 0;
1952 info->disk.minor = 0;
1953 info->disk.raid_disk = -1;
1954 info->reshape_active = 0;
1955 info->array.major_version = -1;
1956 info->array.minor_version = -2;
1957 strcpy(info->text_version, "imsm");
1958 info->safe_mode_delay = 0;
1959 info->disk.number = -1;
1960 info->disk.state = 0;
1961 info->name[0] = 0;
1962 info->recovery_start = MaxSector;
1963
1964 /* do we have the all the insync disks that we expect? */
1965 mpb = super->anchor;
1966
1967 for (i = 0; i < mpb->num_raid_devs; i++) {
1968 struct imsm_dev *dev = get_imsm_dev(super, i);
1969 int failed, enough, j, missing = 0;
1970 struct imsm_map *map;
1971 __u8 state;
1972
1973 failed = imsm_count_failed(super, dev);
1974 state = imsm_check_degraded(super, dev, failed);
1975 map = get_imsm_map(dev, dev->vol.migr_state);
1976
1977 /* any newly missing disks?
1978 * (catches single-degraded vs double-degraded)
1979 */
1980 for (j = 0; j < map->num_members; j++) {
1981 __u32 ord = get_imsm_ord_tbl_ent(dev, i, -1);
1982 __u32 idx = ord_to_idx(ord);
1983
1984 if (!(ord & IMSM_ORD_REBUILD) &&
1985 get_imsm_missing(super, idx)) {
1986 missing = 1;
1987 break;
1988 }
1989 }
1990
1991 if (state == IMSM_T_STATE_FAILED)
1992 enough = -1;
1993 else if (state == IMSM_T_STATE_DEGRADED &&
1994 (state != map->map_state || missing))
1995 enough = 0;
1996 else /* we're normal, or already degraded */
1997 enough = 1;
1998
1999 /* in the missing/failed disk case check to see
2000 * if at least one array is runnable
2001 */
2002 max_enough = max(max_enough, enough);
2003 }
2004 dprintf("%s: enough: %d\n", __func__, max_enough);
2005 info->container_enough = max_enough;
2006
2007 if (super->disks) {
2008 __u32 reserved = imsm_reserved_sectors(super, super->disks);
2009
2010 disk = &super->disks->disk;
2011 info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved;
2012 info->component_size = reserved;
2013 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
2014 /* we don't change info->disk.raid_disk here because
2015 * this state will be finalized in mdmon after we have
2016 * found the 'most fresh' version of the metadata
2017 */
2018 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
2019 info->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
2020 }
2021
2022 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2023 * ->compare_super may have updated the 'num_raid_devs' field for spares
2024 */
2025 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
2026 uuid_from_super_imsm(st, info->uuid);
2027 else
2028 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
2029
2030 /* I don't know how to compute 'map' on imsm, so use safe default */
2031 if (map) {
2032 int i;
2033 for (i = 0; i < map_disks; i++)
2034 map[i] = 1;
2035 }
2036
2037 }
2038
2039 /* allocates memory and fills disk in mdinfo structure
2040 * for each disk in array */
2041 struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
2042 {
2043 struct mdinfo *mddev = NULL;
2044 struct intel_super *super = st->sb;
2045 struct imsm_disk *disk;
2046 int count = 0;
2047 struct dl *dl;
2048 if (!super || !super->disks)
2049 return NULL;
2050 dl = super->disks;
2051 mddev = malloc(sizeof(*mddev));
2052 if (!mddev) {
2053 fprintf(stderr, Name ": Failed to allocate memory.\n");
2054 return NULL;
2055 }
2056 memset(mddev, 0, sizeof(*mddev));
2057 while (dl) {
2058 struct mdinfo *tmp;
2059 disk = &dl->disk;
2060 tmp = malloc(sizeof(*tmp));
2061 if (!tmp) {
2062 fprintf(stderr, Name ": Failed to allocate memory.\n");
2063 if (mddev)
2064 sysfs_free(mddev);
2065 return NULL;
2066 }
2067 memset(tmp, 0, sizeof(*tmp));
2068 if (mddev->devs)
2069 tmp->next = mddev->devs;
2070 mddev->devs = tmp;
2071 tmp->disk.number = count++;
2072 tmp->disk.major = dl->major;
2073 tmp->disk.minor = dl->minor;
2074 tmp->disk.state = is_configured(disk) ?
2075 (1 << MD_DISK_ACTIVE) : 0;
2076 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
2077 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
2078 tmp->disk.raid_disk = -1;
2079 dl = dl->next;
2080 }
2081 return mddev;
2082 }
2083
2084 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
2085 char *update, char *devname, int verbose,
2086 int uuid_set, char *homehost)
2087 {
2088 /* For 'assemble' and 'force' we need to return non-zero if any
2089 * change was made. For others, the return value is ignored.
2090 * Update options are:
2091 * force-one : This device looks a bit old but needs to be included,
2092 * update age info appropriately.
2093 * assemble: clear any 'faulty' flag to allow this device to
2094 * be assembled.
2095 * force-array: Array is degraded but being forced, mark it clean
2096 * if that will be needed to assemble it.
2097 *
2098 * newdev: not used ????
2099 * grow: Array has gained a new device - this is currently for
2100 * linear only
2101 * resync: mark as dirty so a resync will happen.
2102 * name: update the name - preserving the homehost
2103 * uuid: Change the uuid of the array to match watch is given
2104 *
2105 * Following are not relevant for this imsm:
2106 * sparc2.2 : update from old dodgey metadata
2107 * super-minor: change the preferred_minor number
2108 * summaries: update redundant counters.
2109 * homehost: update the recorded homehost
2110 * _reshape_progress: record new reshape_progress position.
2111 */
2112 int rv = 1;
2113 struct intel_super *super = st->sb;
2114 struct imsm_super *mpb;
2115
2116 /* we can only update container info */
2117 if (!super || super->current_vol >= 0 || !super->anchor)
2118 return 1;
2119
2120 mpb = super->anchor;
2121
2122 if (strcmp(update, "uuid") == 0 && uuid_set && !info->update_private)
2123 rv = -1;
2124 else if (strcmp(update, "uuid") == 0 && uuid_set && info->update_private) {
2125 mpb->orig_family_num = *((__u32 *) info->update_private);
2126 rv = 0;
2127 } else if (strcmp(update, "uuid") == 0) {
2128 __u32 *new_family = malloc(sizeof(*new_family));
2129
2130 /* update orig_family_number with the incoming random
2131 * data, report the new effective uuid, and store the
2132 * new orig_family_num for future updates.
2133 */
2134 if (new_family) {
2135 memcpy(&mpb->orig_family_num, info->uuid, sizeof(__u32));
2136 uuid_from_super_imsm(st, info->uuid);
2137 *new_family = mpb->orig_family_num;
2138 info->update_private = new_family;
2139 rv = 0;
2140 }
2141 } else if (strcmp(update, "assemble") == 0)
2142 rv = 0;
2143 else
2144 rv = -1;
2145
2146 /* successful update? recompute checksum */
2147 if (rv == 0)
2148 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
2149
2150 return rv;
2151 }
2152
2153 static size_t disks_to_mpb_size(int disks)
2154 {
2155 size_t size;
2156
2157 size = sizeof(struct imsm_super);
2158 size += (disks - 1) * sizeof(struct imsm_disk);
2159 size += 2 * sizeof(struct imsm_dev);
2160 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2161 size += (4 - 2) * sizeof(struct imsm_map);
2162 /* 4 possible disk_ord_tbl's */
2163 size += 4 * (disks - 1) * sizeof(__u32);
2164
2165 return size;
2166 }
2167
2168 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
2169 {
2170 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
2171 return 0;
2172
2173 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
2174 }
2175
2176 static void free_devlist(struct intel_super *super)
2177 {
2178 struct intel_dev *dv;
2179
2180 while (super->devlist) {
2181 dv = super->devlist->next;
2182 free(super->devlist->dev);
2183 free(super->devlist);
2184 super->devlist = dv;
2185 }
2186 }
2187
2188 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
2189 {
2190 memcpy(dest, src, sizeof_imsm_dev(src, 0));
2191 }
2192
2193 static int compare_super_imsm(struct supertype *st, struct supertype *tst)
2194 {
2195 /*
2196 * return:
2197 * 0 same, or first was empty, and second was copied
2198 * 1 second had wrong number
2199 * 2 wrong uuid
2200 * 3 wrong other info
2201 */
2202 struct intel_super *first = st->sb;
2203 struct intel_super *sec = tst->sb;
2204
2205 if (!first) {
2206 st->sb = tst->sb;
2207 tst->sb = NULL;
2208 return 0;
2209 }
2210 /* in platform dependent environment test if the disks
2211 * use the same Intel hba
2212 */
2213 if (!check_env("IMSM_NO_PLATFORM")) {
2214 if (first->hba->type != sec->hba->type) {
2215 fprintf(stderr,
2216 "HBAs of devices does not match %s != %s\n",
2217 get_sys_dev_type(first->hba->type),
2218 get_sys_dev_type(sec->hba->type));
2219 return 3;
2220 }
2221 }
2222
2223 /* if an anchor does not have num_raid_devs set then it is a free
2224 * floating spare
2225 */
2226 if (first->anchor->num_raid_devs > 0 &&
2227 sec->anchor->num_raid_devs > 0) {
2228 /* Determine if these disks might ever have been
2229 * related. Further disambiguation can only take place
2230 * in load_super_imsm_all
2231 */
2232 __u32 first_family = first->anchor->orig_family_num;
2233 __u32 sec_family = sec->anchor->orig_family_num;
2234
2235 if (memcmp(first->anchor->sig, sec->anchor->sig,
2236 MAX_SIGNATURE_LENGTH) != 0)
2237 return 3;
2238
2239 if (first_family == 0)
2240 first_family = first->anchor->family_num;
2241 if (sec_family == 0)
2242 sec_family = sec->anchor->family_num;
2243
2244 if (first_family != sec_family)
2245 return 3;
2246
2247 }
2248
2249
2250 /* if 'first' is a spare promote it to a populated mpb with sec's
2251 * family number
2252 */
2253 if (first->anchor->num_raid_devs == 0 &&
2254 sec->anchor->num_raid_devs > 0) {
2255 int i;
2256 struct intel_dev *dv;
2257 struct imsm_dev *dev;
2258
2259 /* we need to copy raid device info from sec if an allocation
2260 * fails here we don't associate the spare
2261 */
2262 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
2263 dv = malloc(sizeof(*dv));
2264 if (!dv)
2265 break;
2266 dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
2267 if (!dev) {
2268 free(dv);
2269 break;
2270 }
2271 dv->dev = dev;
2272 dv->index = i;
2273 dv->next = first->devlist;
2274 first->devlist = dv;
2275 }
2276 if (i < sec->anchor->num_raid_devs) {
2277 /* allocation failure */
2278 free_devlist(first);
2279 fprintf(stderr, "imsm: failed to associate spare\n");
2280 return 3;
2281 }
2282 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
2283 first->anchor->orig_family_num = sec->anchor->orig_family_num;
2284 first->anchor->family_num = sec->anchor->family_num;
2285 memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH);
2286 for (i = 0; i < sec->anchor->num_raid_devs; i++)
2287 imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
2288 }
2289
2290 return 0;
2291 }
2292
2293 static void fd2devname(int fd, char *name)
2294 {
2295 struct stat st;
2296 char path[256];
2297 char dname[PATH_MAX];
2298 char *nm;
2299 int rv;
2300
2301 name[0] = '\0';
2302 if (fstat(fd, &st) != 0)
2303 return;
2304 sprintf(path, "/sys/dev/block/%d:%d",
2305 major(st.st_rdev), minor(st.st_rdev));
2306
2307 rv = readlink(path, dname, sizeof(dname));
2308 if (rv <= 0)
2309 return;
2310
2311 dname[rv] = '\0';
2312 nm = strrchr(dname, '/');
2313 nm++;
2314 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
2315 }
2316
2317 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
2318
2319 static int imsm_read_serial(int fd, char *devname,
2320 __u8 serial[MAX_RAID_SERIAL_LEN])
2321 {
2322 unsigned char scsi_serial[255];
2323 int rv;
2324 int rsp_len;
2325 int len;
2326 char *dest;
2327 char *src;
2328 char *rsp_buf;
2329 int i;
2330
2331 memset(scsi_serial, 0, sizeof(scsi_serial));
2332
2333 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
2334
2335 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
2336 memset(serial, 0, MAX_RAID_SERIAL_LEN);
2337 fd2devname(fd, (char *) serial);
2338 return 0;
2339 }
2340
2341 if (rv != 0) {
2342 if (devname)
2343 fprintf(stderr,
2344 Name ": Failed to retrieve serial for %s\n",
2345 devname);
2346 return rv;
2347 }
2348
2349 rsp_len = scsi_serial[3];
2350 if (!rsp_len) {
2351 if (devname)
2352 fprintf(stderr,
2353 Name ": Failed to retrieve serial for %s\n",
2354 devname);
2355 return 2;
2356 }
2357 rsp_buf = (char *) &scsi_serial[4];
2358
2359 /* trim all whitespace and non-printable characters and convert
2360 * ':' to ';'
2361 */
2362 for (i = 0, dest = rsp_buf; i < rsp_len; i++) {
2363 src = &rsp_buf[i];
2364 if (*src > 0x20) {
2365 /* ':' is reserved for use in placeholder serial
2366 * numbers for missing disks
2367 */
2368 if (*src == ':')
2369 *dest++ = ';';
2370 else
2371 *dest++ = *src;
2372 }
2373 }
2374 len = dest - rsp_buf;
2375 dest = rsp_buf;
2376
2377 /* truncate leading characters */
2378 if (len > MAX_RAID_SERIAL_LEN) {
2379 dest += len - MAX_RAID_SERIAL_LEN;
2380 len = MAX_RAID_SERIAL_LEN;
2381 }
2382
2383 memset(serial, 0, MAX_RAID_SERIAL_LEN);
2384 memcpy(serial, dest, len);
2385
2386 return 0;
2387 }
2388
2389 static int serialcmp(__u8 *s1, __u8 *s2)
2390 {
2391 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
2392 }
2393
2394 static void serialcpy(__u8 *dest, __u8 *src)
2395 {
2396 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
2397 }
2398
2399 #ifndef MDASSEMBLE
2400 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
2401 {
2402 struct dl *dl;
2403
2404 for (dl = super->disks; dl; dl = dl->next)
2405 if (serialcmp(dl->serial, serial) == 0)
2406 break;
2407
2408 return dl;
2409 }
2410 #endif
2411
2412 static struct imsm_disk *
2413 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
2414 {
2415 int i;
2416
2417 for (i = 0; i < mpb->num_disks; i++) {
2418 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
2419
2420 if (serialcmp(disk->serial, serial) == 0) {
2421 if (idx)
2422 *idx = i;
2423 return disk;
2424 }
2425 }
2426
2427 return NULL;
2428 }
2429
2430 static int
2431 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
2432 {
2433 struct imsm_disk *disk;
2434 struct dl *dl;
2435 struct stat stb;
2436 int rv;
2437 char name[40];
2438 __u8 serial[MAX_RAID_SERIAL_LEN];
2439
2440 rv = imsm_read_serial(fd, devname, serial);
2441
2442 if (rv != 0)
2443 return 2;
2444
2445 dl = calloc(1, sizeof(*dl));
2446 if (!dl) {
2447 if (devname)
2448 fprintf(stderr,
2449 Name ": failed to allocate disk buffer for %s\n",
2450 devname);
2451 return 2;
2452 }
2453
2454 fstat(fd, &stb);
2455 dl->major = major(stb.st_rdev);
2456 dl->minor = minor(stb.st_rdev);
2457 dl->next = super->disks;
2458 dl->fd = keep_fd ? fd : -1;
2459 assert(super->disks == NULL);
2460 super->disks = dl;
2461 serialcpy(dl->serial, serial);
2462 dl->index = -2;
2463 dl->e = NULL;
2464 fd2devname(fd, name);
2465 if (devname)
2466 dl->devname = strdup(devname);
2467 else
2468 dl->devname = strdup(name);
2469
2470 /* look up this disk's index in the current anchor */
2471 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
2472 if (disk) {
2473 dl->disk = *disk;
2474 /* only set index on disks that are a member of a
2475 * populated contianer, i.e. one with raid_devs
2476 */
2477 if (is_failed(&dl->disk))
2478 dl->index = -2;
2479 else if (is_spare(&dl->disk))
2480 dl->index = -1;
2481 }
2482
2483 return 0;
2484 }
2485
2486 #ifndef MDASSEMBLE
2487 /* When migrating map0 contains the 'destination' state while map1
2488 * contains the current state. When not migrating map0 contains the
2489 * current state. This routine assumes that map[0].map_state is set to
2490 * the current array state before being called.
2491 *
2492 * Migration is indicated by one of the following states
2493 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
2494 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
2495 * map1state=unitialized)
2496 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
2497 * map1state=normal)
2498 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
2499 * map1state=degraded)
2500 */
2501 static void migrate(struct imsm_dev *dev, __u8 to_state, int migr_type)
2502 {
2503 struct imsm_map *dest;
2504 struct imsm_map *src = get_imsm_map(dev, 0);
2505
2506 dev->vol.migr_state = 1;
2507 set_migr_type(dev, migr_type);
2508 dev->vol.curr_migr_unit = 0;
2509 dest = get_imsm_map(dev, 1);
2510
2511 /* duplicate and then set the target end state in map[0] */
2512 memcpy(dest, src, sizeof_imsm_map(src));
2513 if ((migr_type == MIGR_REBUILD) ||
2514 (migr_type == MIGR_GEN_MIGR)) {
2515 __u32 ord;
2516 int i;
2517
2518 for (i = 0; i < src->num_members; i++) {
2519 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
2520 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
2521 }
2522 }
2523
2524 src->map_state = to_state;
2525 }
2526
2527 static void end_migration(struct imsm_dev *dev, __u8 map_state)
2528 {
2529 struct imsm_map *map = get_imsm_map(dev, 0);
2530 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
2531 int i, j;
2532
2533 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2534 * completed in the last migration.
2535 *
2536 * FIXME add support for raid-level-migration
2537 */
2538 for (i = 0; i < prev->num_members; i++)
2539 for (j = 0; j < map->num_members; j++)
2540 /* during online capacity expansion
2541 * disks position can be changed if takeover is used
2542 */
2543 if (ord_to_idx(map->disk_ord_tbl[j]) ==
2544 ord_to_idx(prev->disk_ord_tbl[i])) {
2545 map->disk_ord_tbl[j] |= prev->disk_ord_tbl[i];
2546 break;
2547 }
2548
2549 dev->vol.migr_state = 0;
2550 dev->vol.migr_type = 0;
2551 dev->vol.curr_migr_unit = 0;
2552 map->map_state = map_state;
2553 }
2554 #endif
2555
2556 static int parse_raid_devices(struct intel_super *super)
2557 {
2558 int i;
2559 struct imsm_dev *dev_new;
2560 size_t len, len_migr;
2561 size_t max_len = 0;
2562 size_t space_needed = 0;
2563 struct imsm_super *mpb = super->anchor;
2564
2565 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2566 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
2567 struct intel_dev *dv;
2568
2569 len = sizeof_imsm_dev(dev_iter, 0);
2570 len_migr = sizeof_imsm_dev(dev_iter, 1);
2571 if (len_migr > len)
2572 space_needed += len_migr - len;
2573
2574 dv = malloc(sizeof(*dv));
2575 if (!dv)
2576 return 1;
2577 if (max_len < len_migr)
2578 max_len = len_migr;
2579 if (max_len > len_migr)
2580 space_needed += max_len - len_migr;
2581 dev_new = malloc(max_len);
2582 if (!dev_new) {
2583 free(dv);
2584 return 1;
2585 }
2586 imsm_copy_dev(dev_new, dev_iter);
2587 dv->dev = dev_new;
2588 dv->index = i;
2589 dv->next = super->devlist;
2590 super->devlist = dv;
2591 }
2592
2593 /* ensure that super->buf is large enough when all raid devices
2594 * are migrating
2595 */
2596 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
2597 void *buf;
2598
2599 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512);
2600 if (posix_memalign(&buf, 512, len) != 0)
2601 return 1;
2602
2603 memcpy(buf, super->buf, super->len);
2604 memset(buf + super->len, 0, len - super->len);
2605 free(super->buf);
2606 super->buf = buf;
2607 super->len = len;
2608 }
2609
2610 return 0;
2611 }
2612
2613 /* retrieve a pointer to the bbm log which starts after all raid devices */
2614 struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
2615 {
2616 void *ptr = NULL;
2617
2618 if (__le32_to_cpu(mpb->bbm_log_size)) {
2619 ptr = mpb;
2620 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
2621 }
2622
2623 return ptr;
2624 }
2625
2626 static void __free_imsm(struct intel_super *super, int free_disks);
2627
2628 /* load_imsm_mpb - read matrix metadata
2629 * allocates super->mpb to be freed by free_super
2630 */
2631 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
2632 {
2633 unsigned long long dsize;
2634 unsigned long long sectors;
2635 struct stat;
2636 struct imsm_super *anchor;
2637 __u32 check_sum;
2638
2639 get_dev_size(fd, NULL, &dsize);
2640 if (dsize < 1024) {
2641 if (devname)
2642 fprintf(stderr,
2643 Name ": %s: device to small for imsm\n",
2644 devname);
2645 return 1;
2646 }
2647
2648 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
2649 if (devname)
2650 fprintf(stderr,
2651 Name ": Cannot seek to anchor block on %s: %s\n",
2652 devname, strerror(errno));
2653 return 1;
2654 }
2655
2656 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
2657 if (devname)
2658 fprintf(stderr,
2659 Name ": Failed to allocate imsm anchor buffer"
2660 " on %s\n", devname);
2661 return 1;
2662 }
2663 if (read(fd, anchor, 512) != 512) {
2664 if (devname)
2665 fprintf(stderr,
2666 Name ": Cannot read anchor block on %s: %s\n",
2667 devname, strerror(errno));
2668 free(anchor);
2669 return 1;
2670 }
2671
2672 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
2673 if (devname)
2674 fprintf(stderr,
2675 Name ": no IMSM anchor on %s\n", devname);
2676 free(anchor);
2677 return 2;
2678 }
2679
2680 __free_imsm(super, 0);
2681 super->len = ROUND_UP(anchor->mpb_size, 512);
2682 if (posix_memalign(&super->buf, 512, super->len) != 0) {
2683 if (devname)
2684 fprintf(stderr,
2685 Name ": unable to allocate %zu byte mpb buffer\n",
2686 super->len);
2687 free(anchor);
2688 return 2;
2689 }
2690 memcpy(super->buf, anchor, 512);
2691
2692 sectors = mpb_sectors(anchor) - 1;
2693 free(anchor);
2694 if (!sectors) {
2695 check_sum = __gen_imsm_checksum(super->anchor);
2696 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
2697 if (devname)
2698 fprintf(stderr,
2699 Name ": IMSM checksum %x != %x on %s\n",
2700 check_sum,
2701 __le32_to_cpu(super->anchor->check_sum),
2702 devname);
2703 return 2;
2704 }
2705
2706 return 0;
2707 }
2708
2709 /* read the extended mpb */
2710 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
2711 if (devname)
2712 fprintf(stderr,
2713 Name ": Cannot seek to extended mpb on %s: %s\n",
2714 devname, strerror(errno));
2715 return 1;
2716 }
2717
2718 if ((unsigned)read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
2719 if (devname)
2720 fprintf(stderr,
2721 Name ": Cannot read extended mpb on %s: %s\n",
2722 devname, strerror(errno));
2723 return 2;
2724 }
2725
2726 check_sum = __gen_imsm_checksum(super->anchor);
2727 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
2728 if (devname)
2729 fprintf(stderr,
2730 Name ": IMSM checksum %x != %x on %s\n",
2731 check_sum, __le32_to_cpu(super->anchor->check_sum),
2732 devname);
2733 return 3;
2734 }
2735
2736 /* FIXME the BBM log is disk specific so we cannot use this global
2737 * buffer for all disks. Ok for now since we only look at the global
2738 * bbm_log_size parameter to gate assembly
2739 */
2740 super->bbm_log = __get_imsm_bbm_log(super->anchor);
2741
2742 return 0;
2743 }
2744
2745 static int
2746 load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
2747 {
2748 int err;
2749
2750 err = load_imsm_mpb(fd, super, devname);
2751 if (err)
2752 return err;
2753 err = load_imsm_disk(fd, super, devname, keep_fd);
2754 if (err)
2755 return err;
2756 err = parse_raid_devices(super);
2757
2758 return err;
2759 }
2760
2761 static void __free_imsm_disk(struct dl *d)
2762 {
2763 if (d->fd >= 0)
2764 close(d->fd);
2765 if (d->devname)
2766 free(d->devname);
2767 if (d->e)
2768 free(d->e);
2769 free(d);
2770
2771 }
2772
2773 static void free_imsm_disks(struct intel_super *super)
2774 {
2775 struct dl *d;
2776
2777 while (super->disks) {
2778 d = super->disks;
2779 super->disks = d->next;
2780 __free_imsm_disk(d);
2781 }
2782 while (super->disk_mgmt_list) {
2783 d = super->disk_mgmt_list;
2784 super->disk_mgmt_list = d->next;
2785 __free_imsm_disk(d);
2786 }
2787 while (super->missing) {
2788 d = super->missing;
2789 super->missing = d->next;
2790 __free_imsm_disk(d);
2791 }
2792
2793 }
2794
2795 /* free all the pieces hanging off of a super pointer */
2796 static void __free_imsm(struct intel_super *super, int free_disks)
2797 {
2798 struct intel_hba *elem, *next;
2799
2800 if (super->buf) {
2801 free(super->buf);
2802 super->buf = NULL;
2803 }
2804 if (free_disks)
2805 free_imsm_disks(super);
2806 free_devlist(super);
2807 elem = super->hba;
2808 while (elem) {
2809 if (elem->path)
2810 free((void *)elem->path);
2811 next = elem->next;
2812 free(elem);
2813 elem = next;
2814 }
2815 super->hba = NULL;
2816 }
2817
2818 static void free_imsm(struct intel_super *super)
2819 {
2820 __free_imsm(super, 1);
2821 free(super);
2822 }
2823
2824 static void free_super_imsm(struct supertype *st)
2825 {
2826 struct intel_super *super = st->sb;
2827
2828 if (!super)
2829 return;
2830
2831 free_imsm(super);
2832 st->sb = NULL;
2833 }
2834
2835 static struct intel_super *alloc_super(void)
2836 {
2837 struct intel_super *super = malloc(sizeof(*super));
2838
2839 if (super) {
2840 memset(super, 0, sizeof(*super));
2841 super->current_vol = -1;
2842 super->create_offset = ~((__u32 ) 0);
2843 if (!check_env("IMSM_NO_PLATFORM"))
2844 super->orom = find_imsm_orom();
2845 }
2846
2847 return super;
2848 }
2849
2850 #ifndef MDASSEMBLE
2851 /* find_missing - helper routine for load_super_imsm_all that identifies
2852 * disks that have disappeared from the system. This routine relies on
2853 * the mpb being uptodate, which it is at load time.
2854 */
2855 static int find_missing(struct intel_super *super)
2856 {
2857 int i;
2858 struct imsm_super *mpb = super->anchor;
2859 struct dl *dl;
2860 struct imsm_disk *disk;
2861
2862 for (i = 0; i < mpb->num_disks; i++) {
2863 disk = __get_imsm_disk(mpb, i);
2864 dl = serial_to_dl(disk->serial, super);
2865 if (dl)
2866 continue;
2867
2868 dl = malloc(sizeof(*dl));
2869 if (!dl)
2870 return 1;
2871 dl->major = 0;
2872 dl->minor = 0;
2873 dl->fd = -1;
2874 dl->devname = strdup("missing");
2875 dl->index = i;
2876 serialcpy(dl->serial, disk->serial);
2877 dl->disk = *disk;
2878 dl->e = NULL;
2879 dl->next = super->missing;
2880 super->missing = dl;
2881 }
2882
2883 return 0;
2884 }
2885
2886 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
2887 {
2888 struct intel_disk *idisk = disk_list;
2889
2890 while (idisk) {
2891 if (serialcmp(idisk->disk.serial, serial) == 0)
2892 break;
2893 idisk = idisk->next;
2894 }
2895
2896 return idisk;
2897 }
2898
2899 static int __prep_thunderdome(struct intel_super **table, int tbl_size,
2900 struct intel_super *super,
2901 struct intel_disk **disk_list)
2902 {
2903 struct imsm_disk *d = &super->disks->disk;
2904 struct imsm_super *mpb = super->anchor;
2905 int i, j;
2906
2907 for (i = 0; i < tbl_size; i++) {
2908 struct imsm_super *tbl_mpb = table[i]->anchor;
2909 struct imsm_disk *tbl_d = &table[i]->disks->disk;
2910
2911 if (tbl_mpb->family_num == mpb->family_num) {
2912 if (tbl_mpb->check_sum == mpb->check_sum) {
2913 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2914 __func__, super->disks->major,
2915 super->disks->minor,
2916 table[i]->disks->major,
2917 table[i]->disks->minor);
2918 break;
2919 }
2920
2921 if (((is_configured(d) && !is_configured(tbl_d)) ||
2922 is_configured(d) == is_configured(tbl_d)) &&
2923 tbl_mpb->generation_num < mpb->generation_num) {
2924 /* current version of the mpb is a
2925 * better candidate than the one in
2926 * super_table, but copy over "cross
2927 * generational" status
2928 */
2929 struct intel_disk *idisk;
2930
2931 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2932 __func__, super->disks->major,
2933 super->disks->minor,
2934 table[i]->disks->major,
2935 table[i]->disks->minor);
2936
2937 idisk = disk_list_get(tbl_d->serial, *disk_list);
2938 if (idisk && is_failed(&idisk->disk))
2939 tbl_d->status |= FAILED_DISK;
2940 break;
2941 } else {
2942 struct intel_disk *idisk;
2943 struct imsm_disk *disk;
2944
2945 /* tbl_mpb is more up to date, but copy
2946 * over cross generational status before
2947 * returning
2948 */
2949 disk = __serial_to_disk(d->serial, mpb, NULL);
2950 if (disk && is_failed(disk))
2951 d->status |= FAILED_DISK;
2952
2953 idisk = disk_list_get(d->serial, *disk_list);
2954 if (idisk) {
2955 idisk->owner = i;
2956 if (disk && is_configured(disk))
2957 idisk->disk.status |= CONFIGURED_DISK;
2958 }
2959
2960 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2961 __func__, super->disks->major,
2962 super->disks->minor,
2963 table[i]->disks->major,
2964 table[i]->disks->minor);
2965
2966 return tbl_size;
2967 }
2968 }
2969 }
2970
2971 if (i >= tbl_size)
2972 table[tbl_size++] = super;
2973 else
2974 table[i] = super;
2975
2976 /* update/extend the merged list of imsm_disk records */
2977 for (j = 0; j < mpb->num_disks; j++) {
2978 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
2979 struct intel_disk *idisk;
2980
2981 idisk = disk_list_get(disk->serial, *disk_list);
2982 if (idisk) {
2983 idisk->disk.status |= disk->status;
2984 if (is_configured(&idisk->disk) ||
2985 is_failed(&idisk->disk))
2986 idisk->disk.status &= ~(SPARE_DISK);
2987 } else {
2988 idisk = calloc(1, sizeof(*idisk));
2989 if (!idisk)
2990 return -1;
2991 idisk->owner = IMSM_UNKNOWN_OWNER;
2992 idisk->disk = *disk;
2993 idisk->next = *disk_list;
2994 *disk_list = idisk;
2995 }
2996
2997 if (serialcmp(idisk->disk.serial, d->serial) == 0)
2998 idisk->owner = i;
2999 }
3000
3001 return tbl_size;
3002 }
3003
3004 static struct intel_super *
3005 validate_members(struct intel_super *super, struct intel_disk *disk_list,
3006 const int owner)
3007 {
3008 struct imsm_super *mpb = super->anchor;
3009 int ok_count = 0;
3010 int i;
3011
3012 for (i = 0; i < mpb->num_disks; i++) {
3013 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
3014 struct intel_disk *idisk;
3015
3016 idisk = disk_list_get(disk->serial, disk_list);
3017 if (idisk) {
3018 if (idisk->owner == owner ||
3019 idisk->owner == IMSM_UNKNOWN_OWNER)
3020 ok_count++;
3021 else
3022 dprintf("%s: '%.16s' owner %d != %d\n",
3023 __func__, disk->serial, idisk->owner,
3024 owner);
3025 } else {
3026 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3027 __func__, __le32_to_cpu(mpb->family_num), i,
3028 disk->serial);
3029 break;
3030 }
3031 }
3032
3033 if (ok_count == mpb->num_disks)
3034 return super;
3035 return NULL;
3036 }
3037
3038 static void show_conflicts(__u32 family_num, struct intel_super *super_list)
3039 {
3040 struct intel_super *s;
3041
3042 for (s = super_list; s; s = s->next) {
3043 if (family_num != s->anchor->family_num)
3044 continue;
3045 fprintf(stderr, "Conflict, offlining family %#x on '%s'\n",
3046 __le32_to_cpu(family_num), s->disks->devname);
3047 }
3048 }
3049
3050 static struct intel_super *
3051 imsm_thunderdome(struct intel_super **super_list, int len)
3052 {
3053 struct intel_super *super_table[len];
3054 struct intel_disk *disk_list = NULL;
3055 struct intel_super *champion, *spare;
3056 struct intel_super *s, **del;
3057 int tbl_size = 0;
3058 int conflict;
3059 int i;
3060
3061 memset(super_table, 0, sizeof(super_table));
3062 for (s = *super_list; s; s = s->next)
3063 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
3064
3065 for (i = 0; i < tbl_size; i++) {
3066 struct imsm_disk *d;
3067 struct intel_disk *idisk;
3068 struct imsm_super *mpb = super_table[i]->anchor;
3069
3070 s = super_table[i];
3071 d = &s->disks->disk;
3072
3073 /* 'd' must appear in merged disk list for its
3074 * configuration to be valid
3075 */
3076 idisk = disk_list_get(d->serial, disk_list);
3077 if (idisk && idisk->owner == i)
3078 s = validate_members(s, disk_list, i);
3079 else
3080 s = NULL;
3081
3082 if (!s)
3083 dprintf("%s: marking family: %#x from %d:%d offline\n",
3084 __func__, mpb->family_num,
3085 super_table[i]->disks->major,
3086 super_table[i]->disks->minor);
3087 super_table[i] = s;
3088 }
3089
3090 /* This is where the mdadm implementation differs from the Windows
3091 * driver which has no strict concept of a container. We can only
3092 * assemble one family from a container, so when returning a prodigal
3093 * array member to this system the code will not be able to disambiguate
3094 * the container contents that should be assembled ("foreign" versus
3095 * "local"). It requires user intervention to set the orig_family_num
3096 * to a new value to establish a new container. The Windows driver in
3097 * this situation fixes up the volume name in place and manages the
3098 * foreign array as an independent entity.
3099 */
3100 s = NULL;
3101 spare = NULL;
3102 conflict = 0;
3103 for (i = 0; i < tbl_size; i++) {
3104 struct intel_super *tbl_ent = super_table[i];
3105 int is_spare = 0;
3106
3107 if (!tbl_ent)
3108 continue;
3109
3110 if (tbl_ent->anchor->num_raid_devs == 0) {
3111 spare = tbl_ent;
3112 is_spare = 1;
3113 }
3114
3115 if (s && !is_spare) {
3116 show_conflicts(tbl_ent->anchor->family_num, *super_list);
3117 conflict++;
3118 } else if (!s && !is_spare)
3119 s = tbl_ent;
3120 }
3121
3122 if (!s)
3123 s = spare;
3124 if (!s) {
3125 champion = NULL;
3126 goto out;
3127 }
3128 champion = s;
3129
3130 if (conflict)
3131 fprintf(stderr, "Chose family %#x on '%s', "
3132 "assemble conflicts to new container with '--update=uuid'\n",
3133 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
3134
3135 /* collect all dl's onto 'champion', and update them to
3136 * champion's version of the status
3137 */
3138 for (s = *super_list; s; s = s->next) {
3139 struct imsm_super *mpb = champion->anchor;
3140 struct dl *dl = s->disks;
3141
3142 if (s == champion)
3143 continue;
3144
3145 for (i = 0; i < mpb->num_disks; i++) {
3146 struct imsm_disk *disk;
3147
3148 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
3149 if (disk) {
3150 dl->disk = *disk;
3151 /* only set index on disks that are a member of
3152 * a populated contianer, i.e. one with
3153 * raid_devs
3154 */
3155 if (is_failed(&dl->disk))
3156 dl->index = -2;
3157 else if (is_spare(&dl->disk))
3158 dl->index = -1;
3159 break;
3160 }
3161 }
3162
3163 if (i >= mpb->num_disks) {
3164 struct intel_disk *idisk;
3165
3166 idisk = disk_list_get(dl->serial, disk_list);
3167 if (idisk && is_spare(&idisk->disk) &&
3168 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
3169 dl->index = -1;
3170 else {
3171 dl->index = -2;
3172 continue;
3173 }
3174 }
3175
3176 dl->next = champion->disks;
3177 champion->disks = dl;
3178 s->disks = NULL;
3179 }
3180
3181 /* delete 'champion' from super_list */
3182 for (del = super_list; *del; ) {
3183 if (*del == champion) {
3184 *del = (*del)->next;
3185 break;
3186 } else
3187 del = &(*del)->next;
3188 }
3189 champion->next = NULL;
3190
3191 out:
3192 while (disk_list) {
3193 struct intel_disk *idisk = disk_list;
3194
3195 disk_list = disk_list->next;
3196 free(idisk);
3197 }
3198
3199 return champion;
3200 }
3201
3202 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
3203 char *devname)
3204 {
3205 struct mdinfo *sra;
3206 struct intel_super *super_list = NULL;
3207 struct intel_super *super = NULL;
3208 int devnum = fd2devnum(fd);
3209 struct mdinfo *sd;
3210 int retry;
3211 int err = 0;
3212 int i;
3213
3214 /* check if 'fd' an opened container */
3215 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
3216 if (!sra)
3217 return 1;
3218
3219 if (sra->array.major_version != -1 ||
3220 sra->array.minor_version != -2 ||
3221 strcmp(sra->text_version, "imsm") != 0) {
3222 err = 1;
3223 goto error;
3224 }
3225 /* load all mpbs */
3226 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
3227 struct intel_super *s = alloc_super();
3228 char nm[32];
3229 int dfd;
3230
3231 err = 1;
3232 if (!s)
3233 goto error;
3234 s->next = super_list;
3235 super_list = s;
3236
3237 err = 2;
3238 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
3239 dfd = dev_open(nm, O_RDWR);
3240 if (dfd < 0)
3241 goto error;
3242
3243 err = load_and_parse_mpb(dfd, s, NULL, 1);
3244
3245 /* retry the load if we might have raced against mdmon */
3246 if (err == 3 && mdmon_running(devnum))
3247 for (retry = 0; retry < 3; retry++) {
3248 usleep(3000);
3249 err = load_and_parse_mpb(dfd, s, NULL, 1);
3250 if (err != 3)
3251 break;
3252 }
3253 if (err)
3254 goto error;
3255 }
3256
3257 /* all mpbs enter, maybe one leaves */
3258 super = imsm_thunderdome(&super_list, i);
3259 if (!super) {
3260 err = 1;
3261 goto error;
3262 }
3263
3264 if (find_missing(super) != 0) {
3265 free_imsm(super);
3266 err = 2;
3267 goto error;
3268 }
3269 err = 0;
3270
3271 error:
3272 while (super_list) {
3273 struct intel_super *s = super_list;
3274
3275 super_list = super_list->next;
3276 free_imsm(s);
3277 }
3278 sysfs_free(sra);
3279
3280 if (err)
3281 return err;
3282
3283 *sbp = super;
3284 st->container_dev = devnum;
3285 if (err == 0 && st->ss == NULL) {
3286 st->ss = &super_imsm;
3287 st->minor_version = 0;
3288 st->max_devs = IMSM_MAX_DEVICES;
3289 }
3290 return 0;
3291 }
3292
3293 static int load_container_imsm(struct supertype *st, int fd, char *devname)
3294 {
3295 return load_super_imsm_all(st, fd, &st->sb, devname);
3296 }
3297 #endif
3298
3299 static int load_super_imsm(struct supertype *st, int fd, char *devname)
3300 {
3301 struct intel_super *super;
3302 int rv;
3303
3304 if (test_partition(fd))
3305 /* IMSM not allowed on partitions */
3306 return 1;
3307
3308 free_super_imsm(st);
3309
3310 super = alloc_super();
3311 if (!super) {
3312 fprintf(stderr,
3313 Name ": malloc of %zu failed.\n",
3314 sizeof(*super));
3315 return 1;
3316 }
3317
3318 rv = load_and_parse_mpb(fd, super, devname, 0);
3319
3320 if (rv) {
3321 if (devname)
3322 fprintf(stderr,
3323 Name ": Failed to load all information "
3324 "sections on %s\n", devname);
3325 free_imsm(super);
3326 return rv;
3327 }
3328
3329 st->sb = super;
3330 if (st->ss == NULL) {
3331 st->ss = &super_imsm;
3332 st->minor_version = 0;
3333 st->max_devs = IMSM_MAX_DEVICES;
3334 }
3335 return 0;
3336 }
3337
3338 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
3339 {
3340 if (info->level == 1)
3341 return 128;
3342 return info->chunk_size >> 9;
3343 }
3344
3345 static __u32 info_to_num_data_stripes(mdu_array_info_t *info, int num_domains)
3346 {
3347 __u32 num_stripes;
3348
3349 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
3350 num_stripes /= num_domains;
3351
3352 return num_stripes;
3353 }
3354
3355 static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
3356 {
3357 if (info->level == 1)
3358 return info->size * 2;
3359 else
3360 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
3361 }
3362
3363 static void imsm_update_version_info(struct intel_super *super)
3364 {
3365 /* update the version and attributes */
3366 struct imsm_super *mpb = super->anchor;
3367 char *version;
3368 struct imsm_dev *dev;
3369 struct imsm_map *map;
3370 int i;
3371
3372 for (i = 0; i < mpb->num_raid_devs; i++) {
3373 dev = get_imsm_dev(super, i);
3374 map = get_imsm_map(dev, 0);
3375 if (__le32_to_cpu(dev->size_high) > 0)
3376 mpb->attributes |= MPB_ATTRIB_2TB;
3377
3378 /* FIXME detect when an array spans a port multiplier */
3379 #if 0
3380 mpb->attributes |= MPB_ATTRIB_PM;
3381 #endif
3382
3383 if (mpb->num_raid_devs > 1 ||
3384 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
3385 version = MPB_VERSION_ATTRIBS;
3386 switch (get_imsm_raid_level(map)) {
3387 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
3388 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
3389 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
3390 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
3391 }
3392 } else {
3393 if (map->num_members >= 5)
3394 version = MPB_VERSION_5OR6_DISK_ARRAY;
3395 else if (dev->status == DEV_CLONE_N_GO)
3396 version = MPB_VERSION_CNG;
3397 else if (get_imsm_raid_level(map) == 5)
3398 version = MPB_VERSION_RAID5;
3399 else if (map->num_members >= 3)
3400 version = MPB_VERSION_3OR4_DISK_ARRAY;
3401 else if (get_imsm_raid_level(map) == 1)
3402 version = MPB_VERSION_RAID1;
3403 else
3404 version = MPB_VERSION_RAID0;
3405 }
3406 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
3407 }
3408 }
3409
3410 static int check_name(struct intel_super *super, char *name, int quiet)
3411 {
3412 struct imsm_super *mpb = super->anchor;
3413 char *reason = NULL;
3414 int i;
3415
3416 if (strlen(name) > MAX_RAID_SERIAL_LEN)
3417 reason = "must be 16 characters or less";
3418
3419 for (i = 0; i < mpb->num_raid_devs; i++) {
3420 struct imsm_dev *dev = get_imsm_dev(super, i);
3421
3422 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
3423 reason = "already exists";
3424 break;
3425 }
3426 }
3427
3428 if (reason && !quiet)
3429 fprintf(stderr, Name ": imsm volume name %s\n", reason);
3430
3431 return !reason;
3432 }
3433
3434 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
3435 unsigned long long size, char *name,
3436 char *homehost, int *uuid)
3437 {
3438 /* We are creating a volume inside a pre-existing container.
3439 * so st->sb is already set.
3440 */
3441 struct intel_super *super = st->sb;
3442 struct imsm_super *mpb = super->anchor;
3443 struct intel_dev *dv;
3444 struct imsm_dev *dev;
3445 struct imsm_vol *vol;
3446 struct imsm_map *map;
3447 int idx = mpb->num_raid_devs;
3448 int i;
3449 unsigned long long array_blocks;
3450 size_t size_old, size_new;
3451 __u32 num_data_stripes;
3452
3453 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
3454 fprintf(stderr, Name": This imsm-container already has the "
3455 "maximum of %d volumes\n", super->orom->vpa);
3456 return 0;
3457 }
3458
3459 /* ensure the mpb is large enough for the new data */
3460 size_old = __le32_to_cpu(mpb->mpb_size);
3461 size_new = disks_to_mpb_size(info->nr_disks);
3462 if (size_new > size_old) {
3463 void *mpb_new;
3464 size_t size_round = ROUND_UP(size_new, 512);
3465
3466 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
3467 fprintf(stderr, Name": could not allocate new mpb\n");
3468 return 0;
3469 }
3470 memcpy(mpb_new, mpb, size_old);
3471 free(mpb);
3472 mpb = mpb_new;
3473 super->anchor = mpb_new;
3474 mpb->mpb_size = __cpu_to_le32(size_new);
3475 memset(mpb_new + size_old, 0, size_round - size_old);
3476 }
3477 super->current_vol = idx;
3478 /* when creating the first raid device in this container set num_disks
3479 * to zero, i.e. delete this spare and add raid member devices in
3480 * add_to_super_imsm_volume()
3481 */
3482 if (super->current_vol == 0)
3483 mpb->num_disks = 0;
3484
3485 if (!check_name(super, name, 0))
3486 return 0;
3487 dv = malloc(sizeof(*dv));
3488 if (!dv) {
3489 fprintf(stderr, Name ": failed to allocate device list entry\n");
3490 return 0;
3491 }
3492 dev = calloc(1, sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
3493 if (!dev) {
3494 free(dv);
3495 fprintf(stderr, Name": could not allocate raid device\n");
3496 return 0;
3497 }
3498
3499 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
3500 if (info->level == 1)
3501 array_blocks = info_to_blocks_per_member(info);
3502 else
3503 array_blocks = calc_array_size(info->level, info->raid_disks,
3504 info->layout, info->chunk_size,
3505 info->size*2);
3506 /* round array size down to closest MB */
3507 array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
3508
3509 dev->size_low = __cpu_to_le32((__u32) array_blocks);
3510 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
3511 dev->status = (DEV_READ_COALESCING | DEV_WRITE_COALESCING);
3512 vol = &dev->vol;
3513 vol->migr_state = 0;
3514 set_migr_type(dev, MIGR_INIT);
3515 vol->dirty = 0;
3516 vol->curr_migr_unit = 0;
3517 map = get_imsm_map(dev, 0);
3518 map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
3519 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
3520 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
3521 map->failed_disk_num = ~0;
3522 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
3523 IMSM_T_STATE_NORMAL;
3524 map->ddf = 1;
3525
3526 if (info->level == 1 && info->raid_disks > 2) {
3527 free(dev);
3528 free(dv);
3529 fprintf(stderr, Name": imsm does not support more than 2 disks"
3530 "in a raid1 volume\n");
3531 return 0;
3532 }
3533
3534 map->raid_level = info->level;
3535 if (info->level == 10) {
3536 map->raid_level = 1;
3537 map->num_domains = info->raid_disks / 2;
3538 } else if (info->level == 1)
3539 map->num_domains = info->raid_disks;
3540 else
3541 map->num_domains = 1;
3542
3543 num_data_stripes = info_to_num_data_stripes(info, map->num_domains);
3544 map->num_data_stripes = __cpu_to_le32(num_data_stripes);
3545
3546 map->num_members = info->raid_disks;
3547 for (i = 0; i < map->num_members; i++) {
3548 /* initialized in add_to_super */
3549 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
3550 }
3551 mpb->num_raid_devs++;
3552
3553 dv->dev = dev;
3554 dv->index = super->current_vol;
3555 dv->next = super->devlist;
3556 super->devlist = dv;
3557
3558 imsm_update_version_info(super);
3559
3560 return 1;
3561 }
3562
3563 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
3564 unsigned long long size, char *name,
3565 char *homehost, int *uuid)
3566 {
3567 /* This is primarily called by Create when creating a new array.
3568 * We will then get add_to_super called for each component, and then
3569 * write_init_super called to write it out to each device.
3570 * For IMSM, Create can create on fresh devices or on a pre-existing
3571 * array.
3572 * To create on a pre-existing array a different method will be called.
3573 * This one is just for fresh drives.
3574 */
3575 struct intel_super *super;
3576 struct imsm_super *mpb;
3577 size_t mpb_size;
3578 char *version;
3579
3580 if (st->sb)
3581 return init_super_imsm_volume(st, info, size, name, homehost, uuid);
3582
3583 if (info)
3584 mpb_size = disks_to_mpb_size(info->nr_disks);
3585 else
3586 mpb_size = 512;
3587
3588 super = alloc_super();
3589 if (super && posix_memalign(&super->buf, 512, mpb_size) != 0) {
3590 free(super);
3591 super = NULL;
3592 }
3593 if (!super) {
3594 fprintf(stderr, Name
3595 ": %s could not allocate superblock\n", __func__);
3596 return 0;
3597 }
3598 memset(super->buf, 0, mpb_size);
3599 mpb = super->buf;
3600 mpb->mpb_size = __cpu_to_le32(mpb_size);
3601 st->sb = super;
3602
3603 if (info == NULL) {
3604 /* zeroing superblock */
3605 return 0;
3606 }
3607
3608 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
3609
3610 version = (char *) mpb->sig;
3611 strcpy(version, MPB_SIGNATURE);
3612 version += strlen(MPB_SIGNATURE);
3613 strcpy(version, MPB_VERSION_RAID0);
3614
3615 return 1;
3616 }
3617
3618 #ifndef MDASSEMBLE
3619 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
3620 int fd, char *devname)
3621 {
3622 struct intel_super *super = st->sb;
3623 struct imsm_super *mpb = super->anchor;
3624 struct dl *dl;
3625 struct imsm_dev *dev;
3626 struct imsm_map *map;
3627 int slot;
3628
3629 dev = get_imsm_dev(super, super->current_vol);
3630 map = get_imsm_map(dev, 0);
3631
3632 if (! (dk->state & (1<<MD_DISK_SYNC))) {
3633 fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
3634 devname);
3635 return 1;
3636 }
3637
3638 if (fd == -1) {
3639 /* we're doing autolayout so grab the pre-marked (in
3640 * validate_geometry) raid_disk
3641 */
3642 for (dl = super->disks; dl; dl = dl->next)
3643 if (dl->raiddisk == dk->raid_disk)
3644 break;
3645 } else {
3646 for (dl = super->disks; dl ; dl = dl->next)
3647 if (dl->major == dk->major &&
3648 dl->minor == dk->minor)
3649 break;
3650 }
3651
3652 if (!dl) {
3653 fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
3654 return 1;
3655 }
3656
3657 /* add a pristine spare to the metadata */
3658 if (dl->index < 0) {
3659 dl->index = super->anchor->num_disks;
3660 super->anchor->num_disks++;
3661 }
3662 /* Check the device has not already been added */
3663 slot = get_imsm_disk_slot(map, dl->index);
3664 if (slot >= 0 &&
3665 (get_imsm_ord_tbl_ent(dev, slot, -1) & IMSM_ORD_REBUILD) == 0) {
3666 fprintf(stderr, Name ": %s has been included in this array twice\n",
3667 devname);
3668 return 1;
3669 }
3670 set_imsm_ord_tbl_ent(map, dk->number, dl->index);
3671 dl->disk.status = CONFIGURED_DISK;
3672
3673 /* if we are creating the first raid device update the family number */
3674 if (super->current_vol == 0) {
3675 __u32 sum;
3676 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
3677 struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
3678
3679 if (!_dev || !_disk) {
3680 fprintf(stderr, Name ": BUG mpb setup error\n");
3681 return 1;
3682 }
3683 *_dev = *dev;
3684 *_disk = dl->disk;
3685 sum = random32();
3686 sum += __gen_imsm_checksum(mpb);
3687 mpb->family_num = __cpu_to_le32(sum);
3688 mpb->orig_family_num = mpb->family_num;
3689 }
3690
3691 return 0;
3692 }
3693
3694
3695 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
3696 int fd, char *devname)
3697 {
3698 struct intel_super *super = st->sb;
3699 struct dl *dd;
3700 unsigned long long size;
3701 __u32 id;
3702 int rv;
3703 struct stat stb;
3704
3705 /* If we are on an RAID enabled platform check that the disk is
3706 * attached to the raid controller.
3707 * We do not need to test disks attachment for container based additions,
3708 * they shall be already tested when container was created/assembled.
3709 */
3710 if ((fd != -1) && !check_env("IMSM_NO_PLATFORM")) {
3711 struct sys_dev *hba_name;
3712 struct intel_hba *hba;
3713
3714 hba_name = find_disk_attached_hba(fd, NULL);
3715 if (!hba_name) {
3716 fprintf(stderr,
3717 Name ": %s is not attached to Intel(R) RAID controller.\n",
3718 devname ? : "disk");
3719 return 1;
3720 }
3721 rv = attach_hba_to_super(super, hba_name);
3722 switch (rv) {
3723 case 2:
3724 fprintf(stderr, Name ": %s is attached to Intel(R) %s RAID "
3725 "controller (%s),\n but the container is assigned to Intel(R) "
3726 "%s RAID controller (",
3727 devname,
3728 get_sys_dev_type(hba_name->type),
3729 hba_name->pci_id ? : "Err!",
3730 get_sys_dev_type(hba_name->type));
3731
3732 hba = super->hba;
3733 while (hba) {
3734 fprintf(stderr, "%s", hba->pci_id ? : "Err!");
3735 if (hba->next)
3736 fprintf(stderr, ", ");
3737 hba = hba->next;
3738 }
3739
3740 fprintf(stderr, ").\n"
3741 " Mixing devices attached to different controllers "
3742 "is not allowed.\n");
3743 free_sys_dev(&hba_name);
3744 return 1;
3745 }
3746 free_sys_dev(&hba_name);
3747 }
3748
3749 if (super->current_vol >= 0)
3750 return add_to_super_imsm_volume(st, dk, fd, devname);
3751
3752 fstat(fd, &stb);
3753 dd = malloc(sizeof(*dd));
3754 if (!dd) {
3755 fprintf(stderr,
3756 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
3757 return 1;
3758 }
3759 memset(dd, 0, sizeof(*dd));
3760 dd->major = major(stb.st_rdev);
3761 dd->minor = minor(stb.st_rdev);
3762 dd->index = -1;
3763 dd->devname = devname ? strdup(devname) : NULL;
3764 dd->fd = fd;
3765 dd->e = NULL;
3766 dd->action = DISK_ADD;
3767 rv = imsm_read_serial(fd, devname, dd->serial);
3768 if (rv) {
3769 fprintf(stderr,
3770 Name ": failed to retrieve scsi serial, aborting\n");
3771 free(dd);
3772 abort();
3773 }
3774
3775 get_dev_size(fd, NULL, &size);
3776 size /= 512;
3777 serialcpy(dd->disk.serial, dd->serial);
3778 dd->disk.total_blocks = __cpu_to_le32(size);
3779 dd->disk.status = SPARE_DISK;
3780 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
3781 dd->disk.scsi_id = __cpu_to_le32(id);
3782 else
3783 dd->disk.scsi_id = __cpu_to_le32(0);
3784
3785 if (st->update_tail) {
3786 dd->next = super->disk_mgmt_list;
3787 super->disk_mgmt_list = dd;
3788 } else {
3789 dd->next = super->disks;
3790 super->disks = dd;
3791 }
3792
3793 return 0;
3794 }
3795
3796
3797 static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
3798 {
3799 struct intel_super *super = st->sb;
3800 struct dl *dd;
3801
3802 /* remove from super works only in mdmon - for communication
3803 * manager - monitor. Check if communication memory buffer
3804 * is prepared.
3805 */
3806 if (!st->update_tail) {
3807 fprintf(stderr,
3808 Name ": %s shall be used in mdmon context only"
3809 "(line %d).\n", __func__, __LINE__);
3810 return 1;
3811 }
3812 dd = malloc(sizeof(*dd));
3813 if (!dd) {
3814 fprintf(stderr,
3815 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
3816 return 1;
3817 }
3818 memset(dd, 0, sizeof(*dd));
3819 dd->major = dk->major;
3820 dd->minor = dk->minor;
3821 dd->index = -1;
3822 dd->fd = -1;
3823 dd->disk.status = SPARE_DISK;
3824 dd->action = DISK_REMOVE;
3825
3826 dd->next = super->disk_mgmt_list;
3827 super->disk_mgmt_list = dd;
3828
3829
3830 return 0;
3831 }
3832
3833 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
3834
3835 static union {
3836 char buf[512];
3837 struct imsm_super anchor;
3838 } spare_record __attribute__ ((aligned(512)));
3839
3840 /* spare records have their own family number and do not have any defined raid
3841 * devices
3842 */
3843 static int write_super_imsm_spares(struct intel_super *super, int doclose)
3844 {
3845 struct imsm_super *mpb = super->anchor;
3846 struct imsm_super *spare = &spare_record.anchor;
3847 __u32 sum;
3848 struct dl *d;
3849
3850 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)),
3851 spare->generation_num = __cpu_to_le32(1UL),
3852 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
3853 spare->num_disks = 1,
3854 spare->num_raid_devs = 0,
3855 spare->cache_size = mpb->cache_size,
3856 spare->pwr_cycle_count = __cpu_to_le32(1),
3857
3858 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
3859 MPB_SIGNATURE MPB_VERSION_RAID0);
3860
3861 for (d = super->disks; d; d = d->next) {
3862 if (d->index != -1)
3863 continue;
3864
3865 spare->disk[0] = d->disk;
3866 sum = __gen_imsm_checksum(spare);
3867 spare->family_num = __cpu_to_le32(sum);
3868 spare->orig_family_num = 0;
3869 sum = __gen_imsm_checksum(spare);
3870 spare->check_sum = __cpu_to_le32(sum);
3871
3872 if (store_imsm_mpb(d->fd, spare)) {
3873 fprintf(stderr, "%s: failed for device %d:%d %s\n",
3874 __func__, d->major, d->minor, strerror(errno));
3875 return 1;
3876 }
3877 if (doclose) {
3878 close(d->fd);
3879 d->fd = -1;
3880 }
3881 }
3882
3883 return 0;
3884 }
3885
3886 static int write_super_imsm(struct supertype *st, int doclose)
3887 {
3888 struct intel_super *super = st->sb;
3889 struct imsm_super *mpb = super->anchor;
3890 struct dl *d;
3891 __u32 generation;
3892 __u32 sum;
3893 int spares = 0;
3894 int i;
3895 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
3896 int num_disks = 0;
3897
3898 /* 'generation' is incremented everytime the metadata is written */
3899 generation = __le32_to_cpu(mpb->generation_num);
3900 generation++;
3901 mpb->generation_num = __cpu_to_le32(generation);
3902
3903 /* fix up cases where previous mdadm releases failed to set
3904 * orig_family_num
3905 */
3906 if (mpb->orig_family_num == 0)
3907 mpb->orig_family_num = mpb->family_num;
3908
3909 for (d = super->disks; d; d = d->next) {
3910 if (d->index == -1)
3911 spares++;
3912 else {
3913 mpb->disk[d->index] = d->disk;
3914 num_disks++;
3915 }
3916 }
3917 for (d = super->missing; d; d = d->next) {
3918 mpb->disk[d->index] = d->disk;
3919 num_disks++;
3920 }
3921 mpb->num_disks = num_disks;
3922 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
3923
3924 for (i = 0; i < mpb->num_raid_devs; i++) {
3925 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
3926 struct imsm_dev *dev2 = get_imsm_dev(super, i);
3927 if (dev && dev2) {
3928 imsm_copy_dev(dev, dev2);
3929 mpb_size += sizeof_imsm_dev(dev, 0);
3930 }
3931 }
3932 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
3933 mpb->mpb_size = __cpu_to_le32(mpb_size);
3934
3935 /* recalculate checksum */
3936 sum = __gen_imsm_checksum(mpb);
3937 mpb->check_sum = __cpu_to_le32(sum);
3938
3939 /* write the mpb for disks that compose raid devices */
3940 for (d = super->disks; d ; d = d->next) {
3941 if (d->index < 0)
3942 continue;
3943 if (store_imsm_mpb(d->fd, mpb))
3944 fprintf(stderr, "%s: failed for device %d:%d %s\n",
3945 __func__, d->major, d->minor, strerror(errno));
3946 if (doclose) {
3947 close(d->fd);
3948 d->fd = -1;
3949 }
3950 }
3951
3952 if (spares)
3953 return write_super_imsm_spares(super, doclose);
3954
3955 return 0;
3956 }
3957
3958
3959 static int create_array(struct supertype *st, int dev_idx)
3960 {
3961 size_t len;
3962 struct imsm_update_create_array *u;
3963 struct intel_super *super = st->sb;
3964 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3965 struct imsm_map *map = get_imsm_map(dev, 0);
3966 struct disk_info *inf;
3967 struct imsm_disk *disk;
3968 int i;
3969
3970 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
3971 sizeof(*inf) * map->num_members;
3972 u = malloc(len);
3973 if (!u) {
3974 fprintf(stderr, "%s: failed to allocate update buffer\n",
3975 __func__);
3976 return 1;
3977 }
3978
3979 u->type = update_create_array;
3980 u->dev_idx = dev_idx;
3981 imsm_copy_dev(&u->dev, dev);
3982 inf = get_disk_info(u);
3983 for (i = 0; i < map->num_members; i++) {
3984 int idx = get_imsm_disk_idx(dev, i, -1);
3985
3986 disk = get_imsm_disk(super, idx);
3987 serialcpy(inf[i].serial, disk->serial);
3988 }
3989 append_metadata_update(st, u, len);
3990
3991 return 0;
3992 }
3993
3994 static int mgmt_disk(struct supertype *st)
3995 {
3996 struct intel_super *super = st->sb;
3997 size_t len;
3998 struct imsm_update_add_remove_disk *u;
3999
4000 if (!super->disk_mgmt_list)
4001 return 0;
4002
4003 len = sizeof(*u);
4004 u = malloc(len);
4005 if (!u) {
4006 fprintf(stderr, "%s: failed to allocate update buffer\n",
4007 __func__);
4008 return 1;
4009 }
4010
4011 u->type = update_add_remove_disk;
4012 append_metadata_update(st, u, len);
4013
4014 return 0;
4015 }
4016
4017 static int write_init_super_imsm(struct supertype *st)
4018 {
4019 struct intel_super *super = st->sb;
4020 int current_vol = super->current_vol;
4021
4022 /* we are done with current_vol reset it to point st at the container */
4023 super->current_vol = -1;
4024
4025 if (st->update_tail) {
4026 /* queue the recently created array / added disk
4027 * as a metadata update */
4028 int rv;
4029
4030 /* determine if we are creating a volume or adding a disk */
4031 if (current_vol < 0) {
4032 /* in the mgmt (add/remove) disk case we are running
4033 * in mdmon context, so don't close fd's
4034 */
4035 return mgmt_disk(st);
4036 } else
4037 rv = create_array(st, current_vol);
4038
4039 return rv;
4040 } else {
4041 struct dl *d;
4042 for (d = super->disks; d; d = d->next)
4043 Kill(d->devname, NULL, 0, 1, 1);
4044 return write_super_imsm(st, 1);
4045 }
4046 }
4047 #endif
4048
4049 static int store_super_imsm(struct supertype *st, int fd)
4050 {
4051 struct intel_super *super = st->sb;
4052 struct imsm_super *mpb = super ? super->anchor : NULL;
4053
4054 if (!mpb)
4055 return 1;
4056
4057 #ifndef MDASSEMBLE
4058 return store_imsm_mpb(fd, mpb);
4059 #else
4060 return 1;
4061 #endif
4062 }
4063
4064 static int imsm_bbm_log_size(struct imsm_super *mpb)
4065 {
4066 return __le32_to_cpu(mpb->bbm_log_size);
4067 }
4068
4069 #ifndef MDASSEMBLE
4070 static int validate_geometry_imsm_container(struct supertype *st, int level,
4071 int layout, int raiddisks, int chunk,
4072 unsigned long long size, char *dev,
4073 unsigned long long *freesize,
4074 int verbose)
4075 {
4076 int fd;
4077 unsigned long long ldsize;
4078 const struct imsm_orom *orom;
4079
4080 if (level != LEVEL_CONTAINER)
4081 return 0;
4082 if (!dev)
4083 return 1;
4084
4085 if (check_env("IMSM_NO_PLATFORM"))
4086 orom = NULL;
4087 else
4088 orom = find_imsm_orom();
4089 if (orom && raiddisks > orom->tds) {
4090 if (verbose)
4091 fprintf(stderr, Name ": %d exceeds maximum number of"
4092 " platform supported disks: %d\n",
4093 raiddisks, orom->tds);
4094 return 0;
4095 }
4096
4097 fd = open(dev, O_RDONLY|O_EXCL, 0);
4098 if (fd < 0) {
4099 if (verbose)
4100 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
4101 dev, strerror(errno));
4102 return 0;
4103 }
4104 if (!get_dev_size(fd, dev, &ldsize)) {
4105 close(fd);
4106 return 0;
4107 }
4108 close(fd);
4109
4110 *freesize = avail_size_imsm(st, ldsize >> 9);
4111
4112 return 1;
4113 }
4114
4115 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
4116 {
4117 const unsigned long long base_start = e[*idx].start;
4118 unsigned long long end = base_start + e[*idx].size;
4119 int i;
4120
4121 if (base_start == end)
4122 return 0;
4123
4124 *idx = *idx + 1;
4125 for (i = *idx; i < num_extents; i++) {
4126 /* extend overlapping extents */
4127 if (e[i].start >= base_start &&
4128 e[i].start <= end) {
4129 if (e[i].size == 0)
4130 return 0;
4131 if (e[i].start + e[i].size > end)
4132 end = e[i].start + e[i].size;
4133 } else if (e[i].start > end) {
4134 *idx = i;
4135 break;
4136 }
4137 }
4138
4139 return end - base_start;
4140 }
4141
4142 static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
4143 {
4144 /* build a composite disk with all known extents and generate a new
4145 * 'maxsize' given the "all disks in an array must share a common start
4146 * offset" constraint
4147 */
4148 struct extent *e = calloc(sum_extents, sizeof(*e));
4149 struct dl *dl;
4150 int i, j;
4151 int start_extent;
4152 unsigned long long pos;
4153 unsigned long long start = 0;
4154 unsigned long long maxsize;
4155 unsigned long reserve;
4156
4157 if (!e)
4158 return 0;
4159
4160 /* coalesce and sort all extents. also, check to see if we need to
4161 * reserve space between member arrays
4162 */
4163 j = 0;
4164 for (dl = super->disks; dl; dl = dl->next) {
4165 if (!dl->e)
4166 continue;
4167 for (i = 0; i < dl->extent_cnt; i++)
4168 e[j++] = dl->e[i];
4169 }
4170 qsort(e, sum_extents, sizeof(*e), cmp_extent);
4171
4172 /* merge extents */
4173 i = 0;
4174 j = 0;
4175 while (i < sum_extents) {
4176 e[j].start = e[i].start;
4177 e[j].size = find_size(e, &i, sum_extents);
4178 j++;
4179 if (e[j-1].size == 0)
4180 break;
4181 }
4182
4183 pos = 0;
4184 maxsize = 0;
4185 start_extent = 0;
4186 i = 0;
4187 do {
4188 unsigned long long esize;
4189
4190 esize = e[i].start - pos;
4191 if (esize >= maxsize) {
4192 maxsize = esize;
4193 start = pos;
4194 start_extent = i;
4195 }
4196 pos = e[i].start + e[i].size;
4197 i++;
4198 } while (e[i-1].size);
4199 free(e);
4200
4201 if (maxsize == 0)
4202 return 0;
4203
4204 /* FIXME assumes volume at offset 0 is the first volume in a
4205 * container
4206 */
4207 if (start_extent > 0)
4208 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
4209 else
4210 reserve = 0;
4211
4212 if (maxsize < reserve)
4213 return 0;
4214
4215 super->create_offset = ~((__u32) 0);
4216 if (start + reserve > super->create_offset)
4217 return 0; /* start overflows create_offset */
4218 super->create_offset = start + reserve;
4219
4220 return maxsize - reserve;
4221 }
4222
4223 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
4224 {
4225 if (level < 0 || level == 6 || level == 4)
4226 return 0;
4227
4228 /* if we have an orom prevent invalid raid levels */
4229 if (orom)
4230 switch (level) {
4231 case 0: return imsm_orom_has_raid0(orom);
4232 case 1:
4233 if (raiddisks > 2)
4234 return imsm_orom_has_raid1e(orom);
4235 return imsm_orom_has_raid1(orom) && raiddisks == 2;
4236 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
4237 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
4238 }
4239 else
4240 return 1; /* not on an Intel RAID platform so anything goes */
4241
4242 return 0;
4243 }
4244
4245
4246 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
4247 /*
4248 * validate volume parameters with OROM/EFI capabilities
4249 */
4250 static int
4251 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
4252 int raiddisks, int *chunk, int verbose)
4253 {
4254 #if DEBUG
4255 verbose = 1;
4256 #endif
4257 /* validate container capabilities */
4258 if (super->orom && raiddisks > super->orom->tds) {
4259 if (verbose)
4260 fprintf(stderr, Name ": %d exceeds maximum number of"
4261 " platform supported disks: %d\n",
4262 raiddisks, super->orom->tds);
4263 return 0;
4264 }
4265
4266 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
4267 if (super->orom && (!is_raid_level_supported(super->orom, level,
4268 raiddisks))) {
4269 pr_vrb(": platform does not support raid%d with %d disk%s\n",
4270 level, raiddisks, raiddisks > 1 ? "s" : "");
4271 return 0;
4272 }
4273 if (super->orom && level != 1) {
4274 if (chunk && (*chunk == 0 || *chunk == UnSet))
4275 *chunk = imsm_orom_default_chunk(super->orom);
4276 else if (chunk && !imsm_orom_has_chunk(super->orom, *chunk)) {
4277 pr_vrb(": platform does not support a chunk size of: "
4278 "%d\n", *chunk);
4279 return 0;
4280 }
4281 }
4282 if (layout != imsm_level_to_layout(level)) {
4283 if (level == 5)
4284 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
4285 else if (level == 10)
4286 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
4287 else
4288 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
4289 layout, level);
4290 return 0;
4291 }
4292 return 1;
4293 }
4294
4295 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
4296 * FIX ME add ahci details
4297 */
4298 static int validate_geometry_imsm_volume(struct supertype *st, int level,
4299 int layout, int raiddisks, int *chunk,
4300 unsigned long long size, char *dev,
4301 unsigned long long *freesize,
4302 int verbose)
4303 {
4304 struct stat stb;
4305 struct intel_super *super = st->sb;
4306 struct imsm_super *mpb = super->anchor;
4307 struct dl *dl;
4308 unsigned long long pos = 0;
4309 unsigned long long maxsize;
4310 struct extent *e;
4311 int i;
4312
4313 /* We must have the container info already read in. */
4314 if (!super)
4315 return 0;
4316
4317 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, verbose)) {
4318 fprintf(stderr, Name ": RAID gemetry validation failed. "
4319 "Cannot proceed with the action(s).\n");
4320 return 0;
4321 }
4322 if (!dev) {
4323 /* General test: make sure there is space for
4324 * 'raiddisks' device extents of size 'size' at a given
4325 * offset
4326 */
4327 unsigned long long minsize = size;
4328 unsigned long long start_offset = MaxSector;
4329 int dcnt = 0;
4330 if (minsize == 0)
4331 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
4332 for (dl = super->disks; dl ; dl = dl->next) {
4333 int found = 0;
4334
4335 pos = 0;
4336 i = 0;
4337 e = get_extents(super, dl);
4338 if (!e) continue;
4339 do {
4340 unsigned long long esize;
4341 esize = e[i].start - pos;
4342 if (esize >= minsize)
4343 found = 1;
4344 if (found && start_offset == MaxSector) {
4345 start_offset = pos;
4346 break;
4347 } else if (found && pos != start_offset) {
4348 found = 0;
4349 break;
4350 }
4351 pos = e[i].start + e[i].size;
4352 i++;
4353 } while (e[i-1].size);
4354 if (found)
4355 dcnt++;
4356 free(e);
4357 }
4358 if (dcnt < raiddisks) {
4359 if (verbose)
4360 fprintf(stderr, Name ": imsm: Not enough "
4361 "devices with space for this array "
4362 "(%d < %d)\n",
4363 dcnt, raiddisks);
4364 return 0;
4365 }
4366 return 1;
4367 }
4368
4369 /* This device must be a member of the set */
4370 if (stat(dev, &stb) < 0)
4371 return 0;
4372 if ((S_IFMT & stb.st_mode) != S_IFBLK)
4373 return 0;
4374 for (dl = super->disks ; dl ; dl = dl->next) {
4375 if (dl->major == (int)major(stb.st_rdev) &&
4376 dl->minor == (int)minor(stb.st_rdev))
4377 break;
4378 }
4379 if (!dl) {
4380 if (verbose)
4381 fprintf(stderr, Name ": %s is not in the "
4382 "same imsm set\n", dev);
4383 return 0;
4384 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
4385 /* If a volume is present then the current creation attempt
4386 * cannot incorporate new spares because the orom may not
4387 * understand this configuration (all member disks must be
4388 * members of each array in the container).
4389 */
4390 fprintf(stderr, Name ": %s is a spare and a volume"
4391 " is already defined for this container\n", dev);
4392 fprintf(stderr, Name ": The option-rom requires all member"
4393 " disks to be a member of all volumes\n");
4394 return 0;
4395 }
4396
4397 /* retrieve the largest free space block */
4398 e = get_extents(super, dl);
4399 maxsize = 0;
4400 i = 0;
4401 if (e) {
4402 do {
4403 unsigned long long esize;
4404
4405 esize = e[i].start - pos;
4406 if (esize >= maxsize)
4407 maxsize = esize;
4408 pos = e[i].start + e[i].size;
4409 i++;
4410 } while (e[i-1].size);
4411 dl->e = e;
4412 dl->extent_cnt = i;
4413 } else {
4414 if (verbose)
4415 fprintf(stderr, Name ": unable to determine free space for: %s\n",
4416 dev);
4417 return 0;
4418 }
4419 if (maxsize < size) {
4420 if (verbose)
4421 fprintf(stderr, Name ": %s not enough space (%llu < %llu)\n",
4422 dev, maxsize, size);
4423 return 0;
4424 }
4425
4426 /* count total number of extents for merge */
4427 i = 0;
4428 for (dl = super->disks; dl; dl = dl->next)
4429 if (dl->e)
4430 i += dl->extent_cnt;
4431
4432 maxsize = merge_extents(super, i);
4433 if (maxsize < size || maxsize == 0) {
4434 if (verbose)
4435 fprintf(stderr, Name ": not enough space after merge (%llu < %llu)\n",
4436 maxsize, size);
4437 return 0;
4438 }
4439
4440 *freesize = maxsize;
4441
4442 return 1;
4443 }
4444
4445 static int reserve_space(struct supertype *st, int raiddisks,
4446 unsigned long long size, int chunk,
4447 unsigned long long *freesize)
4448 {
4449 struct intel_super *super = st->sb;
4450 struct imsm_super *mpb = super->anchor;
4451 struct dl *dl;
4452 int i;
4453 int extent_cnt;
4454 struct extent *e;
4455 unsigned long long maxsize;
4456 unsigned long long minsize;
4457 int cnt;
4458 int used;
4459
4460 /* find the largest common start free region of the possible disks */
4461 used = 0;
4462 extent_cnt = 0;
4463 cnt = 0;
4464 for (dl = super->disks; dl; dl = dl->next) {
4465 dl->raiddisk = -1;
4466
4467 if (dl->index >= 0)
4468 used++;
4469
4470 /* don't activate new spares if we are orom constrained
4471 * and there is already a volume active in the container
4472 */
4473 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
4474 continue;
4475
4476 e = get_extents(super, dl);
4477 if (!e)
4478 continue;
4479 for (i = 1; e[i-1].size; i++)
4480 ;
4481 dl->e = e;
4482 dl->extent_cnt = i;
4483 extent_cnt += i;
4484 cnt++;
4485 }
4486
4487 maxsize = merge_extents(super, extent_cnt);
4488 minsize = size;
4489 if (size == 0)
4490 /* chunk is in K */
4491 minsize = chunk * 2;
4492
4493 if (cnt < raiddisks ||
4494 (super->orom && used && used != raiddisks) ||
4495 maxsize < minsize ||
4496 maxsize == 0) {
4497 fprintf(stderr, Name ": not enough devices with space to create array.\n");
4498 return 0; /* No enough free spaces large enough */
4499 }
4500
4501 if (size == 0) {
4502 size = maxsize;
4503 if (chunk) {
4504 size /= 2 * chunk;
4505 size *= 2 * chunk;
4506 }
4507 }
4508
4509 cnt = 0;
4510 for (dl = super->disks; dl; dl = dl->next)
4511 if (dl->e)
4512 dl->raiddisk = cnt++;
4513
4514 *freesize = size;
4515
4516 return 1;
4517 }
4518
4519 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
4520 int raiddisks, int *chunk, unsigned long long size,
4521 char *dev, unsigned long long *freesize,
4522 int verbose)
4523 {
4524 int fd, cfd;
4525 struct mdinfo *sra;
4526 int is_member = 0;
4527
4528 /* load capability
4529 * if given unused devices create a container
4530 * if given given devices in a container create a member volume
4531 */
4532 if (level == LEVEL_CONTAINER) {
4533 /* Must be a fresh device to add to a container */
4534 return validate_geometry_imsm_container(st, level, layout,
4535 raiddisks,
4536 chunk?*chunk:0, size,
4537 dev, freesize,
4538 verbose);
4539 }
4540
4541 if (!dev) {
4542 if (st->sb && freesize) {
4543 /* we are being asked to automatically layout a
4544 * new volume based on the current contents of
4545 * the container. If the the parameters can be
4546 * satisfied reserve_space will record the disks,
4547 * start offset, and size of the volume to be
4548 * created. add_to_super and getinfo_super
4549 * detect when autolayout is in progress.
4550 */
4551 if (!validate_geometry_imsm_orom(st->sb, level, layout,
4552 raiddisks, chunk,
4553 verbose))
4554 return 0;
4555 return reserve_space(st, raiddisks, size,
4556 chunk?*chunk:0, freesize);
4557 }
4558 return 1;
4559 }
4560 if (st->sb) {
4561 /* creating in a given container */
4562 return validate_geometry_imsm_volume(st, level, layout,
4563 raiddisks, chunk, size,
4564 dev, freesize, verbose);
4565 }
4566
4567 /* This device needs to be a device in an 'imsm' container */
4568 fd = open(dev, O_RDONLY|O_EXCL, 0);
4569 if (fd >= 0) {
4570 if (verbose)
4571 fprintf(stderr,
4572 Name ": Cannot create this array on device %s\n",
4573 dev);
4574 close(fd);
4575 return 0;
4576 }
4577 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
4578 if (verbose)
4579 fprintf(stderr, Name ": Cannot open %s: %s\n",
4580 dev, strerror(errno));
4581 return 0;
4582 }
4583 /* Well, it is in use by someone, maybe an 'imsm' container. */
4584 cfd = open_container(fd);
4585 close(fd);
4586 if (cfd < 0) {
4587 if (verbose)
4588 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
4589 dev);
4590 return 0;
4591 }
4592 sra = sysfs_read(cfd, 0, GET_VERSION);
4593 if (sra && sra->array.major_version == -1 &&
4594 strcmp(sra->text_version, "imsm") == 0)
4595 is_member = 1;
4596 sysfs_free(sra);
4597 if (is_member) {
4598 /* This is a member of a imsm container. Load the container
4599 * and try to create a volume
4600 */
4601 struct intel_super *super;
4602
4603 if (load_super_imsm_all(st, cfd, (void **) &super, NULL) == 0) {
4604 st->sb = super;
4605 st->container_dev = fd2devnum(cfd);
4606 close(cfd);
4607 return validate_geometry_imsm_volume(st, level, layout,
4608 raiddisks, chunk,
4609 size, dev,
4610 freesize, verbose);
4611 }
4612 }
4613
4614 if (verbose)
4615 fprintf(stderr, Name ": failed container membership check\n");
4616
4617 close(cfd);
4618 return 0;
4619 }
4620
4621 static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
4622 {
4623 struct intel_super *super = st->sb;
4624
4625 if (level && *level == UnSet)
4626 *level = LEVEL_CONTAINER;
4627
4628 if (level && layout && *layout == UnSet)
4629 *layout = imsm_level_to_layout(*level);
4630
4631 if (chunk && (*chunk == UnSet || *chunk == 0) &&
4632 super && super->orom)
4633 *chunk = imsm_orom_default_chunk(super->orom);
4634 }
4635
4636 static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
4637
4638 static int kill_subarray_imsm(struct supertype *st)
4639 {
4640 /* remove the subarray currently referenced by ->current_vol */
4641 __u8 i;
4642 struct intel_dev **dp;
4643 struct intel_super *super = st->sb;
4644 __u8 current_vol = super->current_vol;
4645 struct imsm_super *mpb = super->anchor;
4646
4647 if (super->current_vol < 0)
4648 return 2;
4649 super->current_vol = -1; /* invalidate subarray cursor */
4650
4651 /* block deletions that would change the uuid of active subarrays
4652 *
4653 * FIXME when immutable ids are available, but note that we'll
4654 * also need to fixup the invalidated/active subarray indexes in
4655 * mdstat
4656 */
4657 for (i = 0; i < mpb->num_raid_devs; i++) {
4658 char subarray[4];
4659
4660 if (i < current_vol)
4661 continue;
4662 sprintf(subarray, "%u", i);
4663 if (is_subarray_active(subarray, st->devname)) {
4664 fprintf(stderr,
4665 Name ": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
4666 current_vol, i);
4667
4668 return 2;
4669 }
4670 }
4671
4672 if (st->update_tail) {
4673 struct imsm_update_kill_array *u = malloc(sizeof(*u));
4674
4675 if (!u)
4676 return 2;
4677 u->type = update_kill_array;
4678 u->dev_idx = current_vol;
4679 append_metadata_update(st, u, sizeof(*u));
4680
4681 return 0;
4682 }
4683
4684 for (dp = &super->devlist; *dp;)
4685 if ((*dp)->index == current_vol) {
4686 *dp = (*dp)->next;
4687 } else {
4688 handle_missing(super, (*dp)->dev);
4689 if ((*dp)->index > current_vol)
4690 (*dp)->index--;
4691 dp = &(*dp)->next;
4692 }
4693
4694 /* no more raid devices, all active components are now spares,
4695 * but of course failed are still failed
4696 */
4697 if (--mpb->num_raid_devs == 0) {
4698 struct dl *d;
4699
4700 for (d = super->disks; d; d = d->next)
4701 if (d->index > -2) {
4702 d->index = -1;
4703 d->disk.status = SPARE_DISK;
4704 }
4705 }
4706
4707 super->updates_pending++;
4708
4709 return 0;
4710 }
4711
4712 static int update_subarray_imsm(struct supertype *st, char *subarray,
4713 char *update, struct mddev_ident *ident)
4714 {
4715 /* update the subarray currently referenced by ->current_vol */
4716 struct intel_super *super = st->sb;
4717 struct imsm_super *mpb = super->anchor;
4718
4719 if (strcmp(update, "name") == 0) {
4720 char *name = ident->name;
4721 char *ep;
4722 int vol;
4723
4724 if (is_subarray_active(subarray, st->devname)) {
4725 fprintf(stderr,
4726 Name ": Unable to update name of active subarray\n");
4727 return 2;
4728 }
4729
4730 if (!check_name(super, name, 0))
4731 return 2;
4732
4733 vol = strtoul(subarray, &ep, 10);
4734 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
4735 return 2;
4736
4737 if (st->update_tail) {
4738 struct imsm_update_rename_array *u = malloc(sizeof(*u));
4739
4740 if (!u)
4741 return 2;
4742 u->type = update_rename_array;
4743 u->dev_idx = vol;
4744 snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name);
4745 append_metadata_update(st, u, sizeof(*u));
4746 } else {
4747 struct imsm_dev *dev;
4748 int i;
4749
4750 dev = get_imsm_dev(super, vol);
4751 snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name);
4752 for (i = 0; i < mpb->num_raid_devs; i++) {
4753 dev = get_imsm_dev(super, i);
4754 handle_missing(super, dev);
4755 }
4756 super->updates_pending++;
4757 }
4758 } else
4759 return 2;
4760
4761 return 0;
4762 }
4763
4764 static int is_gen_migration(struct imsm_dev *dev)
4765 {
4766 if (!dev->vol.migr_state)
4767 return 0;
4768
4769 if (migr_type(dev) == MIGR_GEN_MIGR)
4770 return 1;
4771
4772 return 0;
4773 }
4774 #endif /* MDASSEMBLE */
4775
4776 static int is_rebuilding(struct imsm_dev *dev)
4777 {
4778 struct imsm_map *migr_map;
4779
4780 if (!dev->vol.migr_state)
4781 return 0;
4782
4783 if (migr_type(dev) != MIGR_REBUILD)
4784 return 0;
4785
4786 migr_map = get_imsm_map(dev, 1);
4787
4788 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
4789 return 1;
4790 else
4791 return 0;
4792 }
4793
4794 static void update_recovery_start(struct imsm_dev *dev, struct mdinfo *array)
4795 {
4796 struct mdinfo *rebuild = NULL;
4797 struct mdinfo *d;
4798 __u32 units;
4799
4800 if (!is_rebuilding(dev))
4801 return;
4802
4803 /* Find the rebuild target, but punt on the dual rebuild case */
4804 for (d = array->devs; d; d = d->next)
4805 if (d->recovery_start == 0) {
4806 if (rebuild)
4807 return;
4808 rebuild = d;
4809 }
4810
4811 if (!rebuild) {
4812 /* (?) none of the disks are marked with
4813 * IMSM_ORD_REBUILD, so assume they are missing and the
4814 * disk_ord_tbl was not correctly updated
4815 */
4816 dprintf("%s: failed to locate out-of-sync disk\n", __func__);
4817 return;
4818 }
4819
4820 units = __le32_to_cpu(dev->vol.curr_migr_unit);
4821 rebuild->recovery_start = units * blocks_per_migr_unit(dev);
4822 }
4823
4824
4825 static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
4826 {
4827 /* Given a container loaded by load_super_imsm_all,
4828 * extract information about all the arrays into
4829 * an mdinfo tree.
4830 * If 'subarray' is given, just extract info about that array.
4831 *
4832 * For each imsm_dev create an mdinfo, fill it in,
4833 * then look for matching devices in super->disks
4834 * and create appropriate device mdinfo.
4835 */
4836 struct intel_super *super = st->sb;
4837 struct imsm_super *mpb = super->anchor;
4838 struct mdinfo *rest = NULL;
4839 unsigned int i;
4840 int bbm_errors = 0;
4841 struct dl *d;
4842 int spare_disks = 0;
4843
4844 /* check for bad blocks */
4845 if (imsm_bbm_log_size(super->anchor))
4846 bbm_errors = 1;
4847
4848 /* count spare devices, not used in maps
4849 */
4850 for (d = super->disks; d; d = d->next)
4851 if (d->index == -1)
4852 spare_disks++;
4853
4854 for (i = 0; i < mpb->num_raid_devs; i++) {
4855 struct imsm_dev *dev;
4856 struct imsm_map *map;
4857 struct imsm_map *map2;
4858 struct mdinfo *this;
4859 int slot, chunk;
4860 char *ep;
4861
4862 if (subarray &&
4863 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
4864 continue;
4865
4866 dev = get_imsm_dev(super, i);
4867 map = get_imsm_map(dev, 0);
4868 map2 = get_imsm_map(dev, 1);
4869
4870 /* do not publish arrays that are in the middle of an
4871 * unsupported migration
4872 */
4873 if (dev->vol.migr_state &&
4874 (migr_type(dev) == MIGR_STATE_CHANGE)) {
4875 fprintf(stderr, Name ": cannot assemble volume '%.16s':"
4876 " unsupported migration in progress\n",
4877 dev->volume);
4878 continue;
4879 }
4880 /* do not publish arrays that are not support by controller's
4881 * OROM/EFI
4882 */
4883
4884 chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
4885 if (!validate_geometry_imsm_orom(super,
4886 get_imsm_raid_level(map), /* RAID level */
4887 imsm_level_to_layout(get_imsm_raid_level(map)),
4888 map->num_members, /* raid disks */
4889 &chunk,
4890 1 /* verbose */)) {
4891 fprintf(stderr, Name ": RAID gemetry validation failed. "
4892 "Cannot proceed with the action(s).\n");
4893 continue;
4894 }
4895 this = malloc(sizeof(*this));
4896 if (!this) {
4897 fprintf(stderr, Name ": failed to allocate %zu bytes\n",
4898 sizeof(*this));
4899 break;
4900 }
4901 memset(this, 0, sizeof(*this));
4902 this->next = rest;
4903
4904 super->current_vol = i;
4905 getinfo_super_imsm_volume(st, this, NULL);
4906 for (slot = 0 ; slot < map->num_members; slot++) {
4907 unsigned long long recovery_start;
4908 struct mdinfo *info_d;
4909 struct dl *d;
4910 int idx;
4911 int skip;
4912 __u32 ord;
4913
4914 skip = 0;
4915 idx = get_imsm_disk_idx(dev, slot, 0);
4916 ord = get_imsm_ord_tbl_ent(dev, slot, -1);
4917 for (d = super->disks; d ; d = d->next)
4918 if (d->index == idx)
4919 break;
4920
4921 recovery_start = MaxSector;
4922 if (d == NULL)
4923 skip = 1;
4924 if (d && is_failed(&d->disk))
4925 skip = 1;
4926 if (ord & IMSM_ORD_REBUILD)
4927 recovery_start = 0;
4928
4929 /*
4930 * if we skip some disks the array will be assmebled degraded;
4931 * reset resync start to avoid a dirty-degraded
4932 * situation when performing the intial sync
4933 *
4934 * FIXME handle dirty degraded
4935 */
4936 if ((skip || recovery_start == 0) && !dev->vol.dirty)
4937 this->resync_start = MaxSector;
4938 if (skip)
4939 continue;
4940
4941 info_d = calloc(1, sizeof(*info_d));
4942 if (!info_d) {
4943 fprintf(stderr, Name ": failed to allocate disk"
4944 " for volume %.16s\n", dev->volume);
4945 info_d = this->devs;
4946 while (info_d) {
4947 struct mdinfo *d = info_d->next;
4948
4949 free(info_d);
4950 info_d = d;
4951 }
4952 free(this);
4953 this = rest;
4954 break;
4955 }
4956 info_d->next = this->devs;
4957 this->devs = info_d;
4958
4959 info_d->disk.number = d->index;
4960 info_d->disk.major = d->major;
4961 info_d->disk.minor = d->minor;
4962 info_d->disk.raid_disk = slot;
4963 info_d->recovery_start = recovery_start;
4964 if (map2) {
4965 if (slot < map2->num_members)
4966 info_d->disk.state = (1 << MD_DISK_ACTIVE);
4967 else
4968 this->array.spare_disks++;
4969 } else {
4970 if (slot < map->num_members)
4971 info_d->disk.state = (1 << MD_DISK_ACTIVE);
4972 else
4973 this->array.spare_disks++;
4974 }
4975 if (info_d->recovery_start == MaxSector)
4976 this->array.working_disks++;
4977
4978 info_d->events = __le32_to_cpu(mpb->generation_num);
4979 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
4980 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
4981 }
4982 /* now that the disk list is up-to-date fixup recovery_start */
4983 update_recovery_start(dev, this);
4984 this->array.spare_disks += spare_disks;
4985 rest = this;
4986 }
4987
4988 /* if array has bad blocks, set suitable bit in array status */
4989 if (bbm_errors)
4990 rest->array.state |= (1<<MD_SB_BBM_ERRORS);
4991
4992 return rest;
4993 }
4994
4995
4996 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed)
4997 {
4998 struct imsm_map *map = get_imsm_map(dev, 0);
4999
5000 if (!failed)
5001 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
5002 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
5003
5004 switch (get_imsm_raid_level(map)) {
5005 case 0:
5006 return IMSM_T_STATE_FAILED;
5007 break;
5008 case 1:
5009 if (failed < map->num_members)
5010 return IMSM_T_STATE_DEGRADED;
5011 else
5012 return IMSM_T_STATE_FAILED;
5013 break;
5014 case 10:
5015 {
5016 /**
5017 * check to see if any mirrors have failed, otherwise we
5018 * are degraded. Even numbered slots are mirrored on
5019 * slot+1
5020 */
5021 int i;
5022 /* gcc -Os complains that this is unused */
5023 int insync = insync;
5024
5025 for (i = 0; i < map->num_members; i++) {
5026 __u32 ord = get_imsm_ord_tbl_ent(dev, i, -1);
5027 int idx = ord_to_idx(ord);
5028 struct imsm_disk *disk;
5029
5030 /* reset the potential in-sync count on even-numbered
5031 * slots. num_copies is always 2 for imsm raid10
5032 */
5033 if ((i & 1) == 0)
5034 insync = 2;
5035
5036 disk = get_imsm_disk(super, idx);
5037 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
5038 insync--;
5039
5040 /* no in-sync disks left in this mirror the
5041 * array has failed
5042 */
5043 if (insync == 0)
5044 return IMSM_T_STATE_FAILED;
5045 }
5046
5047 return IMSM_T_STATE_DEGRADED;
5048 }
5049 case 5:
5050 if (failed < 2)
5051 return IMSM_T_STATE_DEGRADED;
5052 else
5053 return IMSM_T_STATE_FAILED;
5054 break;
5055 default:
5056 break;
5057 }
5058
5059 return map->map_state;
5060 }
5061
5062 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev)
5063 {
5064 int i;
5065 int failed = 0;
5066 struct imsm_disk *disk;
5067 struct imsm_map *map = get_imsm_map(dev, 0);
5068 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
5069 __u32 ord;
5070 int idx;
5071
5072 /* at the beginning of migration we set IMSM_ORD_REBUILD on
5073 * disks that are being rebuilt. New failures are recorded to
5074 * map[0]. So we look through all the disks we started with and
5075 * see if any failures are still present, or if any new ones
5076 * have arrived
5077 *
5078 * FIXME add support for online capacity expansion and
5079 * raid-level-migration
5080 */
5081 for (i = 0; i < prev->num_members; i++) {
5082 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
5083 ord |= __le32_to_cpu(map->disk_ord_tbl[i]);
5084 idx = ord_to_idx(ord);
5085
5086 disk = get_imsm_disk(super, idx);
5087 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
5088 failed++;
5089 }
5090
5091 return failed;
5092 }
5093
5094 #ifndef MDASSEMBLE
5095 static int imsm_open_new(struct supertype *c, struct active_array *a,
5096 char *inst)
5097 {
5098 struct intel_super *super = c->sb;
5099 struct imsm_super *mpb = super->anchor;
5100
5101 if (atoi(inst) >= mpb->num_raid_devs) {
5102 fprintf(stderr, "%s: subarry index %d, out of range\n",
5103 __func__, atoi(inst));
5104 return -ENODEV;
5105 }
5106
5107 dprintf("imsm: open_new %s\n", inst);
5108 a->info.container_member = atoi(inst);
5109 return 0;
5110 }
5111
5112 static int is_resyncing(struct imsm_dev *dev)
5113 {
5114 struct imsm_map *migr_map;
5115
5116 if (!dev->vol.migr_state)
5117 return 0;
5118
5119 if (migr_type(dev) == MIGR_INIT ||
5120 migr_type(dev) == MIGR_REPAIR)
5121 return 1;
5122
5123 if (migr_type(dev) == MIGR_GEN_MIGR)
5124 return 0;
5125
5126 migr_map = get_imsm_map(dev, 1);
5127
5128 if ((migr_map->map_state == IMSM_T_STATE_NORMAL) &&
5129 (dev->vol.migr_type != MIGR_GEN_MIGR))
5130 return 1;
5131 else
5132 return 0;
5133 }
5134
5135 /* return true if we recorded new information */
5136 static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
5137 {
5138 __u32 ord;
5139 int slot;
5140 struct imsm_map *map;
5141 char buf[MAX_RAID_SERIAL_LEN+3];
5142 unsigned int len, shift = 0;
5143
5144 /* new failures are always set in map[0] */
5145 map = get_imsm_map(dev, 0);
5146
5147 slot = get_imsm_disk_slot(map, idx);
5148 if (slot < 0)
5149 return 0;
5150
5151 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
5152 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
5153 return 0;
5154
5155 sprintf(buf, "%s:0", disk->serial);
5156 if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
5157 shift = len - MAX_RAID_SERIAL_LEN + 1;
5158 strncpy((char *)disk->serial, &buf[shift], MAX_RAID_SERIAL_LEN);
5159
5160 disk->status |= FAILED_DISK;
5161 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
5162 if (map->failed_disk_num == 0xff)
5163 map->failed_disk_num = slot;
5164 return 1;
5165 }
5166
5167 static void mark_missing(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
5168 {
5169 mark_failure(dev, disk, idx);
5170
5171 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
5172 return;
5173
5174 disk->scsi_id = __cpu_to_le32(~(__u32)0);
5175 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
5176 }
5177
5178 static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
5179 {
5180 __u8 map_state;
5181 struct dl *dl;
5182 int failed;
5183
5184 if (!super->missing)
5185 return;
5186 failed = imsm_count_failed(super, dev);
5187 map_state = imsm_check_degraded(super, dev, failed);
5188
5189 dprintf("imsm: mark missing\n");
5190 end_migration(dev, map_state);
5191 for (dl = super->missing; dl; dl = dl->next)
5192 mark_missing(dev, &dl->disk, dl->index);
5193 super->updates_pending++;
5194 }
5195
5196 static unsigned long long imsm_set_array_size(struct imsm_dev *dev)
5197 {
5198 int used_disks = imsm_num_data_members(dev, 0);
5199 unsigned long long array_blocks;
5200 struct imsm_map *map;
5201
5202 if (used_disks == 0) {
5203 /* when problems occures
5204 * return current array_blocks value
5205 */
5206 array_blocks = __le32_to_cpu(dev->size_high);
5207 array_blocks = array_blocks << 32;
5208 array_blocks += __le32_to_cpu(dev->size_low);
5209
5210 return array_blocks;
5211 }
5212
5213 /* set array size in metadata
5214 */
5215 map = get_imsm_map(dev, 0);
5216 array_blocks = map->blocks_per_member * used_disks;
5217
5218 /* round array size down to closest MB
5219 */
5220 array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
5221 dev->size_low = __cpu_to_le32((__u32)array_blocks);
5222 dev->size_high = __cpu_to_le32((__u32)(array_blocks >> 32));
5223
5224 return array_blocks;
5225 }
5226
5227 static void imsm_set_disk(struct active_array *a, int n, int state);
5228
5229 static void imsm_progress_container_reshape(struct intel_super *super)
5230 {
5231 /* if no device has a migr_state, but some device has a
5232 * different number of members than the previous device, start
5233 * changing the number of devices in this device to match
5234 * previous.
5235 */
5236 struct imsm_super *mpb = super->anchor;
5237 int prev_disks = -1;
5238 int i;
5239 int copy_map_size;
5240
5241 for (i = 0; i < mpb->num_raid_devs; i++) {
5242 struct imsm_dev *dev = get_imsm_dev(super, i);
5243 struct imsm_map *map = get_imsm_map(dev, 0);
5244 struct imsm_map *map2;
5245 int prev_num_members;
5246
5247 if (dev->vol.migr_state)
5248 return;
5249
5250 if (prev_disks == -1)
5251 prev_disks = map->num_members;
5252 if (prev_disks == map->num_members)
5253 continue;
5254
5255 /* OK, this array needs to enter reshape mode.
5256 * i.e it needs a migr_state
5257 */
5258
5259 copy_map_size = sizeof_imsm_map(map);
5260 prev_num_members = map->num_members;
5261 map->num_members = prev_disks;
5262 dev->vol.migr_state = 1;
5263 dev->vol.curr_migr_unit = 0;
5264 dev->vol.migr_type = MIGR_GEN_MIGR;
5265 for (i = prev_num_members;
5266 i < map->num_members; i++)
5267 set_imsm_ord_tbl_ent(map, i, i);
5268 map2 = get_imsm_map(dev, 1);
5269 /* Copy the current map */
5270 memcpy(map2, map, copy_map_size);
5271 map2->num_members = prev_num_members;
5272
5273 imsm_set_array_size(dev);
5274 super->updates_pending++;
5275 }
5276 }
5277
5278 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
5279 * states are handled in imsm_set_disk() with one exception, when a
5280 * resync is stopped due to a new failure this routine will set the
5281 * 'degraded' state for the array.
5282 */
5283 static int imsm_set_array_state(struct active_array *a, int consistent)
5284 {
5285 int inst = a->info.container_member;
5286 struct intel_super *super = a->container->sb;
5287 struct imsm_dev *dev = get_imsm_dev(super, inst);
5288 struct imsm_map *map = get_imsm_map(dev, 0);
5289 int failed = imsm_count_failed(super, dev);
5290 __u8 map_state = imsm_check_degraded(super, dev, failed);
5291 __u32 blocks_per_unit;
5292
5293 if (dev->vol.migr_state &&
5294 dev->vol.migr_type == MIGR_GEN_MIGR) {
5295 /* array state change is blocked due to reshape action
5296 * We might need to
5297 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
5298 * - finish the reshape (if last_checkpoint is big and action != reshape)
5299 * - update curr_migr_unit
5300 */
5301 if (a->curr_action == reshape) {
5302 /* still reshaping, maybe update curr_migr_unit */
5303 goto mark_checkpoint;
5304 } else {
5305 if (a->last_checkpoint == 0 && a->prev_action == reshape) {
5306 /* for some reason we aborted the reshape.
5307 * Better clean up
5308 */
5309 struct imsm_map *map2 = get_imsm_map(dev, 1);
5310 dev->vol.migr_state = 0;
5311 dev->vol.migr_type = 0;
5312 dev->vol.curr_migr_unit = 0;
5313 memcpy(map, map2, sizeof_imsm_map(map2));
5314 super->updates_pending++;
5315 }
5316 if (a->last_checkpoint >= a->info.component_size) {
5317 unsigned long long array_blocks;
5318 int used_disks;
5319 struct mdinfo *mdi;
5320
5321 used_disks = imsm_num_data_members(dev, 0);
5322 if (used_disks > 0) {
5323 array_blocks =
5324 map->blocks_per_member *
5325 used_disks;
5326 /* round array size down to closest MB
5327 */
5328 array_blocks = (array_blocks
5329 >> SECT_PER_MB_SHIFT)
5330 << SECT_PER_MB_SHIFT;
5331 a->info.custom_array_size = array_blocks;
5332 /* encourage manager to update array
5333 * size
5334 */
5335
5336 a->check_reshape = 1;
5337 }
5338 /* finalize online capacity expansion/reshape */
5339 for (mdi = a->info.devs; mdi; mdi = mdi->next)
5340 imsm_set_disk(a,
5341 mdi->disk.raid_disk,
5342 mdi->curr_state);
5343
5344 imsm_progress_container_reshape(super);
5345 }
5346 }
5347 }
5348
5349 /* before we activate this array handle any missing disks */
5350 if (consistent == 2)
5351 handle_missing(super, dev);
5352
5353 if (consistent == 2 &&
5354 (!is_resync_complete(&a->info) ||
5355 map_state != IMSM_T_STATE_NORMAL ||
5356 dev->vol.migr_state))
5357 consistent = 0;
5358
5359 if (is_resync_complete(&a->info)) {
5360 /* complete intialization / resync,
5361 * recovery and interrupted recovery is completed in
5362 * ->set_disk
5363 */
5364 if (is_resyncing(dev)) {
5365 dprintf("imsm: mark resync done\n");
5366 end_migration(dev, map_state);
5367 super->updates_pending++;
5368 a->last_checkpoint = 0;
5369 }
5370 } else if (!is_resyncing(dev) && !failed) {
5371 /* mark the start of the init process if nothing is failed */
5372 dprintf("imsm: mark resync start\n");
5373 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
5374 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_INIT);
5375 else
5376 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
5377 super->updates_pending++;
5378 }
5379
5380 mark_checkpoint:
5381 /* check if we can update curr_migr_unit from resync_start, recovery_start */
5382 blocks_per_unit = blocks_per_migr_unit(dev);
5383 if (blocks_per_unit) {
5384 __u32 units32;
5385 __u64 units;
5386
5387 units = a->last_checkpoint / blocks_per_unit;
5388 units32 = units;
5389
5390 /* check that we did not overflow 32-bits, and that
5391 * curr_migr_unit needs updating
5392 */
5393 if (units32 == units &&
5394 __le32_to_cpu(dev->vol.curr_migr_unit) != units32) {
5395 dprintf("imsm: mark checkpoint (%u)\n", units32);
5396 dev->vol.curr_migr_unit = __cpu_to_le32(units32);
5397 super->updates_pending++;
5398 }
5399 }
5400
5401 /* mark dirty / clean */
5402 if (dev->vol.dirty != !consistent) {
5403 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
5404 if (consistent)
5405 dev->vol.dirty = 0;
5406 else
5407 dev->vol.dirty = 1;
5408 super->updates_pending++;
5409 }
5410
5411 return consistent;
5412 }
5413
5414 static void imsm_set_disk(struct active_array *a, int n, int state)
5415 {
5416 int inst = a->info.container_member;
5417 struct intel_super *super = a->container->sb;
5418 struct imsm_dev *dev = get_imsm_dev(super, inst);
5419 struct imsm_map *map = get_imsm_map(dev, 0);
5420 struct imsm_disk *disk;
5421 int failed;
5422 __u32 ord;
5423 __u8 map_state;
5424
5425 if (n > map->num_members)
5426 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
5427 n, map->num_members - 1);
5428
5429 if (n < 0)
5430 return;
5431
5432 dprintf("imsm: set_disk %d:%x\n", n, state);
5433
5434 ord = get_imsm_ord_tbl_ent(dev, n, -1);
5435 disk = get_imsm_disk(super, ord_to_idx(ord));
5436
5437 /* check for new failures */
5438 if (state & DS_FAULTY) {
5439 if (mark_failure(dev, disk, ord_to_idx(ord)))
5440 super->updates_pending++;
5441 }
5442
5443 /* check if in_sync */
5444 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
5445 struct imsm_map *migr_map = get_imsm_map(dev, 1);
5446
5447 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
5448 super->updates_pending++;
5449 }
5450
5451 failed = imsm_count_failed(super, dev);
5452 map_state = imsm_check_degraded(super, dev, failed);
5453
5454 /* check if recovery complete, newly degraded, or failed */
5455 if (map_state == IMSM_T_STATE_NORMAL && is_rebuilding(dev)) {
5456 end_migration(dev, map_state);
5457 map = get_imsm_map(dev, 0);
5458 map->failed_disk_num = ~0;
5459 super->updates_pending++;
5460 a->last_checkpoint = 0;
5461 } else if (map_state == IMSM_T_STATE_DEGRADED &&
5462 map->map_state != map_state &&
5463 !dev->vol.migr_state) {
5464 dprintf("imsm: mark degraded\n");
5465 map->map_state = map_state;
5466 super->updates_pending++;
5467 a->last_checkpoint = 0;
5468 } else if (map_state == IMSM_T_STATE_FAILED &&
5469 map->map_state != map_state) {
5470 dprintf("imsm: mark failed\n");
5471 end_migration(dev, map_state);
5472 super->updates_pending++;
5473 a->last_checkpoint = 0;
5474 } else if (is_gen_migration(dev)) {
5475 dprintf("imsm: Detected General Migration in state: ");
5476 if (map_state == IMSM_T_STATE_NORMAL) {
5477 end_migration(dev, map_state);
5478 map = get_imsm_map(dev, 0);
5479 map->failed_disk_num = ~0;
5480 dprintf("normal\n");
5481 } else {
5482 if (map_state == IMSM_T_STATE_DEGRADED) {
5483 printf("degraded\n");
5484 end_migration(dev, map_state);
5485 } else {
5486 dprintf("failed\n");
5487 }
5488 map->map_state = map_state;
5489 }
5490 super->updates_pending++;
5491 }
5492 }
5493
5494 static int store_imsm_mpb(int fd, struct imsm_super *mpb)
5495 {
5496 void *buf = mpb;
5497 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
5498 unsigned long long dsize;
5499 unsigned long long sectors;
5500
5501 get_dev_size(fd, NULL, &dsize);
5502
5503 if (mpb_size > 512) {
5504 /* -1 to account for anchor */
5505 sectors = mpb_sectors(mpb) - 1;
5506
5507 /* write the extended mpb to the sectors preceeding the anchor */
5508 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
5509 return 1;
5510
5511 if ((unsigned long long)write(fd, buf + 512, 512 * sectors)
5512 != 512 * sectors)
5513 return 1;
5514 }
5515
5516 /* first block is stored on second to last sector of the disk */
5517 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
5518 return 1;
5519
5520 if (write(fd, buf, 512) != 512)
5521 return 1;
5522
5523 return 0;
5524 }
5525
5526 static void imsm_sync_metadata(struct supertype *container)
5527 {
5528 struct intel_super *super = container->sb;
5529
5530 dprintf("sync metadata: %d\n", super->updates_pending);
5531 if (!super->updates_pending)
5532 return;
5533
5534 write_super_imsm(container, 0);
5535
5536 super->updates_pending = 0;
5537 }
5538
5539 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
5540 {
5541 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
5542 int i = get_imsm_disk_idx(dev, idx, -1);
5543 struct dl *dl;
5544
5545 for (dl = super->disks; dl; dl = dl->next)
5546 if (dl->index == i)
5547 break;
5548
5549 if (dl && is_failed(&dl->disk))
5550 dl = NULL;
5551
5552 if (dl)
5553 dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor);
5554
5555 return dl;
5556 }
5557
5558 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
5559 struct active_array *a, int activate_new,
5560 struct mdinfo *additional_test_list)
5561 {
5562 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
5563 int idx = get_imsm_disk_idx(dev, slot, -1);
5564 struct imsm_super *mpb = super->anchor;
5565 struct imsm_map *map;
5566 unsigned long long pos;
5567 struct mdinfo *d;
5568 struct extent *ex;
5569 int i, j;
5570 int found;
5571 __u32 array_start = 0;
5572 __u32 array_end = 0;
5573 struct dl *dl;
5574 struct mdinfo *test_list;
5575
5576 for (dl = super->disks; dl; dl = dl->next) {
5577 /* If in this array, skip */
5578 for (d = a->info.devs ; d ; d = d->next)
5579 if (d->state_fd >= 0 &&
5580 d->disk.major == dl->major &&
5581 d->disk.minor == dl->minor) {
5582 dprintf("%x:%x already in array\n",
5583 dl->major, dl->minor);
5584 break;
5585 }
5586 if (d)
5587 continue;
5588 test_list = additional_test_list;
5589 while (test_list) {
5590 if (test_list->disk.major == dl->major &&
5591 test_list->disk.minor == dl->minor) {
5592 dprintf("%x:%x already in additional test list\n",
5593 dl->major, dl->minor);
5594 break;
5595 }
5596 test_list = test_list->next;
5597 }
5598 if (test_list)
5599 continue;
5600
5601 /* skip in use or failed drives */
5602 if (is_failed(&dl->disk) || idx == dl->index ||
5603 dl->index == -2) {
5604 dprintf("%x:%x status (failed: %d index: %d)\n",
5605 dl->major, dl->minor, is_failed(&dl->disk), idx);
5606 continue;
5607 }
5608
5609 /* skip pure spares when we are looking for partially
5610 * assimilated drives
5611 */
5612 if (dl->index == -1 && !activate_new)
5613 continue;
5614
5615 /* Does this unused device have the requisite free space?
5616 * It needs to be able to cover all member volumes
5617 */
5618 ex = get_extents(super, dl);
5619 if (!ex) {
5620 dprintf("cannot get extents\n");
5621 continue;
5622 }
5623 for (i = 0; i < mpb->num_raid_devs; i++) {
5624 dev = get_imsm_dev(super, i);
5625 map = get_imsm_map(dev, 0);
5626
5627 /* check if this disk is already a member of
5628 * this array
5629 */
5630 if (get_imsm_disk_slot(map, dl->index) >= 0)
5631 continue;
5632
5633 found = 0;
5634 j = 0;
5635 pos = 0;
5636 array_start = __le32_to_cpu(map->pba_of_lba0);
5637 array_end = array_start +
5638 __le32_to_cpu(map->blocks_per_member) - 1;
5639
5640 do {
5641 /* check that we can start at pba_of_lba0 with
5642 * blocks_per_member of space
5643 */
5644 if (array_start >= pos && array_end < ex[j].start) {
5645 found = 1;
5646 break;
5647 }
5648 pos = ex[j].start + ex[j].size;
5649 j++;
5650 } while (ex[j-1].size);
5651
5652 if (!found)
5653 break;
5654 }
5655
5656 free(ex);
5657 if (i < mpb->num_raid_devs) {
5658 dprintf("%x:%x does not have %u to %u available\n",
5659 dl->major, dl->minor, array_start, array_end);
5660 /* No room */
5661 continue;
5662 }
5663 return dl;
5664 }
5665
5666 return dl;
5667 }
5668
5669
5670 static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
5671 {
5672 struct imsm_dev *dev2;
5673 struct imsm_map *map;
5674 struct dl *idisk;
5675 int slot;
5676 int idx;
5677 __u8 state;
5678
5679 dev2 = get_imsm_dev(cont->sb, dev_idx);
5680 if (dev2) {
5681 state = imsm_check_degraded(cont->sb, dev2, failed);
5682 if (state == IMSM_T_STATE_FAILED) {
5683 map = get_imsm_map(dev2, 0);
5684 if (!map)
5685 return 1;
5686 for (slot = 0; slot < map->num_members; slot++) {
5687 /*
5688 * Check if failed disks are deleted from intel
5689 * disk list or are marked to be deleted
5690 */
5691 idx = get_imsm_disk_idx(dev2, slot, -1);
5692 idisk = get_imsm_dl_disk(cont->sb, idx);
5693 /*
5694 * Do not rebuild the array if failed disks
5695 * from failed sub-array are not removed from
5696 * container.
5697 */
5698 if (idisk &&
5699 is_failed(&idisk->disk) &&
5700 (idisk->action != DISK_REMOVE))
5701 return 0;
5702 }
5703 }
5704 }
5705 return 1;
5706 }
5707
5708 static struct mdinfo *imsm_activate_spare(struct active_array *a,
5709 struct metadata_update **updates)
5710 {
5711 /**
5712 * Find a device with unused free space and use it to replace a
5713 * failed/vacant region in an array. We replace failed regions one a
5714 * array at a time. The result is that a new spare disk will be added
5715 * to the first failed array and after the monitor has finished
5716 * propagating failures the remainder will be consumed.
5717 *
5718 * FIXME add a capability for mdmon to request spares from another
5719 * container.
5720 */
5721
5722 struct intel_super *super = a->container->sb;
5723 int inst = a->info.container_member;
5724 struct imsm_dev *dev = get_imsm_dev(super, inst);
5725 struct imsm_map *map = get_imsm_map(dev, 0);
5726 int failed = a->info.array.raid_disks;
5727 struct mdinfo *rv = NULL;
5728 struct mdinfo *d;
5729 struct mdinfo *di;
5730 struct metadata_update *mu;
5731 struct dl *dl;
5732 struct imsm_update_activate_spare *u;
5733 int num_spares = 0;
5734 int i;
5735 int allowed;
5736
5737 for (d = a->info.devs ; d ; d = d->next) {
5738 if ((d->curr_state & DS_FAULTY) &&
5739 d->state_fd >= 0)
5740 /* wait for Removal to happen */
5741 return NULL;
5742 if (d->state_fd >= 0)
5743 failed--;
5744 }
5745
5746 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
5747 inst, failed, a->info.array.raid_disks, a->info.array.level);
5748
5749 if (dev->vol.migr_state &&
5750 dev->vol.migr_type == MIGR_GEN_MIGR)
5751 /* No repair during migration */
5752 return NULL;
5753
5754 if (a->info.array.level == 4)
5755 /* No repair for takeovered array
5756 * imsm doesn't support raid4
5757 */
5758 return NULL;
5759
5760 if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED)
5761 return NULL;
5762
5763 /*
5764 * If there are any failed disks check state of the other volume.
5765 * Block rebuild if the another one is failed until failed disks
5766 * are removed from container.
5767 */
5768 if (failed) {
5769 dprintf("found failed disks in %s, check if there another"
5770 "failed sub-array.\n",
5771 dev->volume);
5772 /* check if states of the other volumes allow for rebuild */
5773 for (i = 0; i < super->anchor->num_raid_devs; i++) {
5774 if (i != inst) {
5775 allowed = imsm_rebuild_allowed(a->container,
5776 i, failed);
5777 if (!allowed)
5778 return NULL;
5779 }
5780 }
5781 }
5782
5783 /* For each slot, if it is not working, find a spare */
5784 for (i = 0; i < a->info.array.raid_disks; i++) {
5785 for (d = a->info.devs ; d ; d = d->next)
5786 if (d->disk.raid_disk == i)
5787 break;
5788 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
5789 if (d && (d->state_fd >= 0))
5790 continue;
5791
5792 /*
5793 * OK, this device needs recovery. Try to re-add the
5794 * previous occupant of this slot, if this fails see if
5795 * we can continue the assimilation of a spare that was
5796 * partially assimilated, finally try to activate a new
5797 * spare.
5798 */
5799 dl = imsm_readd(super, i, a);
5800 if (!dl)
5801 dl = imsm_add_spare(super, i, a, 0, NULL);
5802 if (!dl)
5803 dl = imsm_add_spare(super, i, a, 1, NULL);
5804 if (!dl)
5805 continue;
5806
5807 /* found a usable disk with enough space */
5808 di = malloc(sizeof(*di));
5809 if (!di)
5810 continue;
5811 memset(di, 0, sizeof(*di));
5812
5813 /* dl->index will be -1 in the case we are activating a
5814 * pristine spare. imsm_process_update() will create a
5815 * new index in this case. Once a disk is found to be
5816 * failed in all member arrays it is kicked from the
5817 * metadata
5818 */
5819 di->disk.number = dl->index;
5820
5821 /* (ab)use di->devs to store a pointer to the device
5822 * we chose
5823 */
5824 di->devs = (struct mdinfo *) dl;
5825
5826 di->disk.raid_disk = i;
5827 di->disk.major = dl->major;
5828 di->disk.minor = dl->minor;
5829 di->disk.state = 0;
5830 di->recovery_start = 0;
5831 di->data_offset = __le32_to_cpu(map->pba_of_lba0);
5832 di->component_size = a->info.component_size;
5833 di->container_member = inst;
5834 super->random = random32();
5835 di->next = rv;
5836 rv = di;
5837 num_spares++;
5838 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
5839 i, di->data_offset);
5840
5841 break;
5842 }
5843
5844 if (!rv)
5845 /* No spares found */
5846 return rv;
5847 /* Now 'rv' has a list of devices to return.
5848 * Create a metadata_update record to update the
5849 * disk_ord_tbl for the array
5850 */
5851 mu = malloc(sizeof(*mu));
5852 if (mu) {
5853 mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
5854 if (mu->buf == NULL) {
5855 free(mu);
5856 mu = NULL;
5857 }
5858 }
5859 if (!mu) {
5860 while (rv) {
5861 struct mdinfo *n = rv->next;
5862
5863 free(rv);
5864 rv = n;
5865 }
5866 return NULL;
5867 }
5868
5869 mu->space = NULL;
5870 mu->space_list = NULL;
5871 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
5872 mu->next = *updates;
5873 u = (struct imsm_update_activate_spare *) mu->buf;
5874
5875 for (di = rv ; di ; di = di->next) {
5876 u->type = update_activate_spare;
5877 u->dl = (struct dl *) di->devs;
5878 di->devs = NULL;
5879 u->slot = di->disk.raid_disk;
5880 u->array = inst;
5881 u->next = u + 1;
5882 u++;
5883 }
5884 (u-1)->next = NULL;
5885 *updates = mu;
5886
5887 return rv;
5888 }
5889
5890 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
5891 {
5892 struct imsm_dev *dev = get_imsm_dev(super, idx);
5893 struct imsm_map *map = get_imsm_map(dev, 0);
5894 struct imsm_map *new_map = get_imsm_map(&u->dev, 0);
5895 struct disk_info *inf = get_disk_info(u);
5896 struct imsm_disk *disk;
5897 int i;
5898 int j;
5899
5900 for (i = 0; i < map->num_members; i++) {
5901 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, -1));
5902 for (j = 0; j < new_map->num_members; j++)
5903 if (serialcmp(disk->serial, inf[j].serial) == 0)
5904 return 1;
5905 }
5906
5907 return 0;
5908 }
5909
5910
5911 static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
5912 {
5913 struct dl *dl = NULL;
5914 for (dl = super->disks; dl; dl = dl->next)
5915 if ((dl->major == major) && (dl->minor == minor))
5916 return dl;
5917 return NULL;
5918 }
5919
5920 static int remove_disk_super(struct intel_super *super, int major, int minor)
5921 {
5922 struct dl *prev = NULL;
5923 struct dl *dl;
5924
5925 prev = NULL;
5926 for (dl = super->disks; dl; dl = dl->next) {
5927 if ((dl->major == major) && (dl->minor == minor)) {
5928 /* remove */
5929 if (prev)
5930 prev->next = dl->next;
5931 else
5932 super->disks = dl->next;
5933 dl->next = NULL;
5934 __free_imsm_disk(dl);
5935 dprintf("%s: removed %x:%x\n",
5936 __func__, major, minor);
5937 break;
5938 }
5939 prev = dl;
5940 }
5941 return 0;
5942 }
5943
5944 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
5945
5946 static int add_remove_disk_update(struct intel_super *super)
5947 {
5948 int check_degraded = 0;
5949 struct dl *disk = NULL;
5950 /* add/remove some spares to/from the metadata/contrainer */
5951 while (super->disk_mgmt_list) {
5952 struct dl *disk_cfg;
5953
5954 disk_cfg = super->disk_mgmt_list;
5955 super->disk_mgmt_list = disk_cfg->next;
5956 disk_cfg->next = NULL;
5957
5958 if (disk_cfg->action == DISK_ADD) {
5959 disk_cfg->next = super->disks;
5960 super->disks = disk_cfg;
5961 check_degraded = 1;
5962 dprintf("%s: added %x:%x\n",
5963 __func__, disk_cfg->major,
5964 disk_cfg->minor);
5965 } else if (disk_cfg->action == DISK_REMOVE) {
5966 dprintf("Disk remove action processed: %x.%x\n",
5967 disk_cfg->major, disk_cfg->minor);
5968 disk = get_disk_super(super,
5969 disk_cfg->major,
5970 disk_cfg->minor);
5971 if (disk) {
5972 /* store action status */
5973 disk->action = DISK_REMOVE;
5974 /* remove spare disks only */
5975 if (disk->index == -1) {
5976 remove_disk_super(super,
5977 disk_cfg->major,
5978 disk_cfg->minor);
5979 }
5980 }
5981 /* release allocate disk structure */
5982 __free_imsm_disk(disk_cfg);
5983 }
5984 }
5985 return check_degraded;
5986 }
5987
5988 static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
5989 struct intel_super *super,
5990 void ***space_list)
5991 {
5992 struct dl *new_disk;
5993 struct intel_dev *id;
5994 int i;
5995 int delta_disks = u->new_raid_disks - u->old_raid_disks;
5996 int disk_count = u->old_raid_disks;
5997 void **tofree = NULL;
5998 int devices_to_reshape = 1;
5999 struct imsm_super *mpb = super->anchor;
6000 int ret_val = 0;
6001 unsigned int dev_id;
6002
6003 dprintf("imsm: apply_reshape_container_disks_update()\n");
6004
6005 /* enable spares to use in array */
6006 for (i = 0; i < delta_disks; i++) {
6007 new_disk = get_disk_super(super,
6008 major(u->new_disks[i]),
6009 minor(u->new_disks[i]));
6010 dprintf("imsm: new disk for reshape is: %i:%i "
6011 "(%p, index = %i)\n",
6012 major(u->new_disks[i]), minor(u->new_disks[i]),
6013 new_disk, new_disk->index);
6014 if ((new_disk == NULL) ||
6015 ((new_disk->index >= 0) &&
6016 (new_disk->index < u->old_raid_disks)))
6017 goto update_reshape_exit;
6018 new_disk->index = disk_count++;
6019 /* slot to fill in autolayout
6020 */
6021 new_disk->raiddisk = new_disk->index;
6022 new_disk->disk.status |=
6023 CONFIGURED_DISK;
6024 new_disk->disk.status &= ~SPARE_DISK;
6025 }
6026
6027 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
6028 mpb->num_raid_devs);
6029 /* manage changes in volume
6030 */
6031 for (dev_id = 0; dev_id < mpb->num_raid_devs; dev_id++) {
6032 void **sp = *space_list;
6033 struct imsm_dev *newdev;
6034 struct imsm_map *newmap, *oldmap;
6035
6036 for (id = super->devlist ; id; id = id->next) {
6037 if (id->index == dev_id)
6038 break;
6039 }
6040 if (id == NULL)
6041 break;
6042 if (!sp)
6043 continue;
6044 *space_list = *sp;
6045 newdev = (void*)sp;
6046 /* Copy the dev, but not (all of) the map */
6047 memcpy(newdev, id->dev, sizeof(*newdev));
6048 oldmap = get_imsm_map(id->dev, 0);
6049 newmap = get_imsm_map(newdev, 0);
6050 /* Copy the current map */
6051 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
6052 /* update one device only
6053 */
6054 if (devices_to_reshape) {
6055 dprintf("imsm: modifying subdev: %i\n",
6056 id->index);
6057 devices_to_reshape--;
6058 newdev->vol.migr_state = 1;
6059 newdev->vol.curr_migr_unit = 0;
6060 newdev->vol.migr_type = MIGR_GEN_MIGR;
6061 newmap->num_members = u->new_raid_disks;
6062 for (i = 0; i < delta_disks; i++) {
6063 set_imsm_ord_tbl_ent(newmap,
6064 u->old_raid_disks + i,
6065 u->old_raid_disks + i);
6066 }
6067 /* New map is correct, now need to save old map
6068 */
6069 newmap = get_imsm_map(newdev, 1);
6070 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
6071
6072 imsm_set_array_size(newdev);
6073 }
6074
6075 sp = (void **)id->dev;
6076 id->dev = newdev;
6077 *sp = tofree;
6078 tofree = sp;
6079 }
6080 if (tofree)
6081 *space_list = tofree;
6082 ret_val = 1;
6083
6084 update_reshape_exit:
6085
6086 return ret_val;
6087 }
6088
6089 static int apply_takeover_update(struct imsm_update_takeover *u,
6090 struct intel_super *super,
6091 void ***space_list)
6092 {
6093 struct imsm_dev *dev = NULL;
6094 struct intel_dev *dv;
6095 struct imsm_dev *dev_new;
6096 struct imsm_map *map;
6097 struct dl *dm, *du;
6098 int i;
6099
6100 for (dv = super->devlist; dv; dv = dv->next)
6101 if (dv->index == (unsigned int)u->subarray) {
6102 dev = dv->dev;
6103 break;
6104 }
6105
6106 if (dev == NULL)
6107 return 0;
6108
6109 map = get_imsm_map(dev, 0);
6110
6111 if (u->direction == R10_TO_R0) {
6112 /* Number of failed disks must be half of initial disk number */
6113 if (imsm_count_failed(super, dev) != (map->num_members / 2))
6114 return 0;
6115
6116 /* iterate through devices to mark removed disks as spare */
6117 for (dm = super->disks; dm; dm = dm->next) {
6118 if (dm->disk.status & FAILED_DISK) {
6119 int idx = dm->index;
6120 /* update indexes on the disk list */
6121 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
6122 the index values will end up being correct.... NB */
6123 for (du = super->disks; du; du = du->next)
6124 if (du->index > idx)
6125 du->index--;
6126 /* mark as spare disk */
6127 dm->disk.status = SPARE_DISK;
6128 dm->index = -1;
6129 }
6130 }
6131 /* update map */
6132 map->num_members = map->num_members / 2;
6133 map->map_state = IMSM_T_STATE_NORMAL;
6134 map->num_domains = 1;
6135 map->raid_level = 0;
6136 map->failed_disk_num = -1;
6137 }
6138
6139 if (u->direction == R0_TO_R10) {
6140 void **space;
6141 /* update slots in current disk list */
6142 for (dm = super->disks; dm; dm = dm->next) {
6143 if (dm->index >= 0)
6144 dm->index *= 2;
6145 }
6146 /* create new *missing* disks */
6147 for (i = 0; i < map->num_members; i++) {
6148 space = *space_list;
6149 if (!space)
6150 continue;
6151 *space_list = *space;
6152 du = (void *)space;
6153 memcpy(du, super->disks, sizeof(*du));
6154 du->fd = -1;
6155 du->minor = 0;
6156 du->major = 0;
6157 du->index = (i * 2) + 1;
6158 sprintf((char *)du->disk.serial,
6159 " MISSING_%d", du->index);
6160 sprintf((char *)du->serial,
6161 "MISSING_%d", du->index);
6162 du->next = super->missing;
6163 super->missing = du;
6164 }
6165 /* create new dev and map */
6166 space = *space_list;
6167 if (!space)
6168 return 0;
6169 *space_list = *space;
6170 dev_new = (void *)space;
6171 memcpy(dev_new, dev, sizeof(*dev));
6172 /* update new map */
6173 map = get_imsm_map(dev_new, 0);
6174 map->num_members = map->num_members * 2;
6175 map->map_state = IMSM_T_STATE_DEGRADED;
6176 map->num_domains = 2;
6177 map->raid_level = 1;
6178 /* replace dev<->dev_new */
6179 dv->dev = dev_new;
6180 }
6181 /* update disk order table */
6182 for (du = super->disks; du; du = du->next)
6183 if (du->index >= 0)
6184 set_imsm_ord_tbl_ent(map, du->index, du->index);
6185 for (du = super->missing; du; du = du->next)
6186 if (du->index >= 0) {
6187 set_imsm_ord_tbl_ent(map, du->index, du->index);
6188 mark_missing(dev_new, &du->disk, du->index);
6189 }
6190
6191 return 1;
6192 }
6193
6194 static void imsm_process_update(struct supertype *st,
6195 struct metadata_update *update)
6196 {
6197 /**
6198 * crack open the metadata_update envelope to find the update record
6199 * update can be one of:
6200 * update_reshape_container_disks - all the arrays in the container
6201 * are being reshaped to have more devices. We need to mark
6202 * the arrays for general migration and convert selected spares
6203 * into active devices.
6204 * update_activate_spare - a spare device has replaced a failed
6205 * device in an array, update the disk_ord_tbl. If this disk is
6206 * present in all member arrays then also clear the SPARE_DISK
6207 * flag
6208 * update_create_array
6209 * update_kill_array
6210 * update_rename_array
6211 * update_add_remove_disk
6212 */
6213 struct intel_super *super = st->sb;
6214 struct imsm_super *mpb;
6215 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
6216
6217 /* update requires a larger buf but the allocation failed */
6218 if (super->next_len && !super->next_buf) {
6219 super->next_len = 0;
6220 return;
6221 }
6222
6223 if (super->next_buf) {
6224 memcpy(super->next_buf, super->buf, super->len);
6225 free(super->buf);
6226 super->len = super->next_len;
6227 super->buf = super->next_buf;
6228
6229 super->next_len = 0;
6230 super->next_buf = NULL;
6231 }
6232
6233 mpb = super->anchor;
6234
6235 switch (type) {
6236 case update_takeover: {
6237 struct imsm_update_takeover *u = (void *)update->buf;
6238 if (apply_takeover_update(u, super, &update->space_list)) {
6239 imsm_update_version_info(super);
6240 super->updates_pending++;
6241 }
6242 break;
6243 }
6244
6245 case update_reshape_container_disks: {
6246 struct imsm_update_reshape *u = (void *)update->buf;
6247 if (apply_reshape_container_disks_update(
6248 u, super, &update->space_list))
6249 super->updates_pending++;
6250 break;
6251 }
6252 case update_activate_spare: {
6253 struct imsm_update_activate_spare *u = (void *) update->buf;
6254 struct imsm_dev *dev = get_imsm_dev(super, u->array);
6255 struct imsm_map *map = get_imsm_map(dev, 0);
6256 struct imsm_map *migr_map;
6257 struct active_array *a;
6258 struct imsm_disk *disk;
6259 __u8 to_state;
6260 struct dl *dl;
6261 unsigned int found;
6262 int failed;
6263 int victim = get_imsm_disk_idx(dev, u->slot, -1);
6264 int i;
6265
6266 for (dl = super->disks; dl; dl = dl->next)
6267 if (dl == u->dl)
6268 break;
6269
6270 if (!dl) {
6271 fprintf(stderr, "error: imsm_activate_spare passed "
6272 "an unknown disk (index: %d)\n",
6273 u->dl->index);
6274 return;
6275 }
6276
6277 super->updates_pending++;
6278
6279 /* count failures (excluding rebuilds and the victim)
6280 * to determine map[0] state
6281 */
6282 failed = 0;
6283 for (i = 0; i < map->num_members; i++) {
6284 if (i == u->slot)
6285 continue;
6286 disk = get_imsm_disk(super,
6287 get_imsm_disk_idx(dev, i, -1));
6288 if (!disk || is_failed(disk))
6289 failed++;
6290 }
6291
6292 /* adding a pristine spare, assign a new index */
6293 if (dl->index < 0) {
6294 dl->index = super->anchor->num_disks;
6295 super->anchor->num_disks++;
6296 }
6297 disk = &dl->disk;
6298 disk->status |= CONFIGURED_DISK;
6299 disk->status &= ~SPARE_DISK;
6300
6301 /* mark rebuild */
6302 to_state = imsm_check_degraded(super, dev, failed);
6303 map->map_state = IMSM_T_STATE_DEGRADED;
6304 migrate(dev, to_state, MIGR_REBUILD);
6305 migr_map = get_imsm_map(dev, 1);
6306 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
6307 set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD);
6308
6309 /* update the family_num to mark a new container
6310 * generation, being careful to record the existing
6311 * family_num in orig_family_num to clean up after
6312 * earlier mdadm versions that neglected to set it.
6313 */
6314 if (mpb->orig_family_num == 0)
6315 mpb->orig_family_num = mpb->family_num;
6316 mpb->family_num += super->random;
6317
6318 /* count arrays using the victim in the metadata */
6319 found = 0;
6320 for (a = st->arrays; a ; a = a->next) {
6321 dev = get_imsm_dev(super, a->info.container_member);
6322 map = get_imsm_map(dev, 0);
6323
6324 if (get_imsm_disk_slot(map, victim) >= 0)
6325 found++;
6326 }
6327
6328 /* delete the victim if it is no longer being
6329 * utilized anywhere
6330 */
6331 if (!found) {
6332 struct dl **dlp;
6333
6334 /* We know that 'manager' isn't touching anything,
6335 * so it is safe to delete
6336 */
6337 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
6338 if ((*dlp)->index == victim)
6339 break;
6340
6341 /* victim may be on the missing list */
6342 if (!*dlp)
6343 for (dlp = &super->missing; *dlp; dlp = &(*dlp)->next)
6344 if ((*dlp)->index == victim)
6345 break;
6346 imsm_delete(super, dlp, victim);
6347 }
6348 break;
6349 }
6350 case update_create_array: {
6351 /* someone wants to create a new array, we need to be aware of
6352 * a few races/collisions:
6353 * 1/ 'Create' called by two separate instances of mdadm
6354 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
6355 * devices that have since been assimilated via
6356 * activate_spare.
6357 * In the event this update can not be carried out mdadm will
6358 * (FIX ME) notice that its update did not take hold.
6359 */
6360 struct imsm_update_create_array *u = (void *) update->buf;
6361 struct intel_dev *dv;
6362 struct imsm_dev *dev;
6363 struct imsm_map *map, *new_map;
6364 unsigned long long start, end;
6365 unsigned long long new_start, new_end;
6366 int i;
6367 struct disk_info *inf;
6368 struct dl *dl;
6369
6370 /* handle racing creates: first come first serve */
6371 if (u->dev_idx < mpb->num_raid_devs) {
6372 dprintf("%s: subarray %d already defined\n",
6373 __func__, u->dev_idx);
6374 goto create_error;
6375 }
6376
6377 /* check update is next in sequence */
6378 if (u->dev_idx != mpb->num_raid_devs) {
6379 dprintf("%s: can not create array %d expected index %d\n",
6380 __func__, u->dev_idx, mpb->num_raid_devs);
6381 goto create_error;
6382 }
6383
6384 new_map = get_imsm_map(&u->dev, 0);
6385 new_start = __le32_to_cpu(new_map->pba_of_lba0);
6386 new_end = new_start + __le32_to_cpu(new_map->blocks_per_member);
6387 inf = get_disk_info(u);
6388
6389 /* handle activate_spare versus create race:
6390 * check to make sure that overlapping arrays do not include
6391 * overalpping disks
6392 */
6393 for (i = 0; i < mpb->num_raid_devs; i++) {
6394 dev = get_imsm_dev(super, i);
6395 map = get_imsm_map(dev, 0);
6396 start = __le32_to_cpu(map->pba_of_lba0);
6397 end = start + __le32_to_cpu(map->blocks_per_member);
6398 if ((new_start >= start && new_start <= end) ||
6399 (start >= new_start && start <= new_end))
6400 /* overlap */;
6401 else
6402 continue;
6403
6404 if (disks_overlap(super, i, u)) {
6405 dprintf("%s: arrays overlap\n", __func__);
6406 goto create_error;
6407 }
6408 }
6409
6410 /* check that prepare update was successful */
6411 if (!update->space) {
6412 dprintf("%s: prepare update failed\n", __func__);
6413 goto create_error;
6414 }
6415
6416 /* check that all disks are still active before committing
6417 * changes. FIXME: could we instead handle this by creating a
6418 * degraded array? That's probably not what the user expects,
6419 * so better to drop this update on the floor.
6420 */
6421 for (i = 0; i < new_map->num_members; i++) {
6422 dl = serial_to_dl(inf[i].serial, super);
6423 if (!dl) {
6424 dprintf("%s: disk disappeared\n", __func__);
6425 goto create_error;
6426 }
6427 }
6428
6429 super->updates_pending++;
6430
6431 /* convert spares to members and fixup ord_tbl */
6432 for (i = 0; i < new_map->num_members; i++) {
6433 dl = serial_to_dl(inf[i].serial, super);
6434 if (dl->index == -1) {
6435 dl->index = mpb->num_disks;
6436 mpb->num_disks++;
6437 dl->disk.status |= CONFIGURED_DISK;
6438 dl->disk.status &= ~SPARE_DISK;
6439 }
6440 set_imsm_ord_tbl_ent(new_map, i, dl->index);
6441 }
6442
6443 dv = update->space;
6444 dev = dv->dev;
6445 update->space = NULL;
6446 imsm_copy_dev(dev, &u->dev);
6447 dv->index = u->dev_idx;
6448 dv->next = super->devlist;
6449 super->devlist = dv;
6450 mpb->num_raid_devs++;
6451
6452 imsm_update_version_info(super);
6453 break;
6454 create_error:
6455 /* mdmon knows how to release update->space, but not
6456 * ((struct intel_dev *) update->space)->dev
6457 */
6458 if (update->space) {
6459 dv = update->space;
6460 free(dv->dev);
6461 }
6462 break;
6463 }
6464 case update_kill_array: {
6465 struct imsm_update_kill_array *u = (void *) update->buf;
6466 int victim = u->dev_idx;
6467 struct active_array *a;
6468 struct intel_dev **dp;
6469 struct imsm_dev *dev;
6470
6471 /* sanity check that we are not affecting the uuid of
6472 * active arrays, or deleting an active array
6473 *
6474 * FIXME when immutable ids are available, but note that
6475 * we'll also need to fixup the invalidated/active
6476 * subarray indexes in mdstat
6477 */
6478 for (a = st->arrays; a; a = a->next)
6479 if (a->info.container_member >= victim)
6480 break;
6481 /* by definition if mdmon is running at least one array
6482 * is active in the container, so checking
6483 * mpb->num_raid_devs is just extra paranoia
6484 */
6485 dev = get_imsm_dev(super, victim);
6486 if (a || !dev || mpb->num_raid_devs == 1) {
6487 dprintf("failed to delete subarray-%d\n", victim);
6488 break;
6489 }
6490
6491 for (dp = &super->devlist; *dp;)
6492 if ((*dp)->index == (unsigned)super->current_vol) {
6493 *dp = (*dp)->next;
6494 } else {
6495 if ((*dp)->index > (unsigned)victim)
6496 (*dp)->index--;
6497 dp = &(*dp)->next;
6498 }
6499 mpb->num_raid_devs--;
6500 super->updates_pending++;
6501 break;
6502 }
6503 case update_rename_array: {
6504 struct imsm_update_rename_array *u = (void *) update->buf;
6505 char name[MAX_RAID_SERIAL_LEN+1];
6506 int target = u->dev_idx;
6507 struct active_array *a;
6508 struct imsm_dev *dev;
6509
6510 /* sanity check that we are not affecting the uuid of
6511 * an active array
6512 */
6513 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
6514 name[MAX_RAID_SERIAL_LEN] = '\0';
6515 for (a = st->arrays; a; a = a->next)
6516 if (a->info.container_member == target)
6517 break;
6518 dev = get_imsm_dev(super, u->dev_idx);
6519 if (a || !dev || !check_name(super, name, 1)) {
6520 dprintf("failed to rename subarray-%d\n", target);
6521 break;
6522 }
6523
6524 snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name);
6525 super->updates_pending++;
6526 break;
6527 }
6528 case update_add_remove_disk: {
6529 /* we may be able to repair some arrays if disks are
6530 * being added, check teh status of add_remove_disk
6531 * if discs has been added.
6532 */
6533 if (add_remove_disk_update(super)) {
6534 struct active_array *a;
6535
6536 super->updates_pending++;
6537 for (a = st->arrays; a; a = a->next)
6538 a->check_degraded = 1;
6539 }
6540 break;
6541 }
6542 default:
6543 fprintf(stderr, "error: unsuported process update type:"
6544 "(type: %d)\n", type);
6545 }
6546 }
6547
6548 static void imsm_prepare_update(struct supertype *st,
6549 struct metadata_update *update)
6550 {
6551 /**
6552 * Allocate space to hold new disk entries, raid-device entries or a new
6553 * mpb if necessary. The manager synchronously waits for updates to
6554 * complete in the monitor, so new mpb buffers allocated here can be
6555 * integrated by the monitor thread without worrying about live pointers
6556 * in the manager thread.
6557 */
6558 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
6559 struct intel_super *super = st->sb;
6560 struct imsm_super *mpb = super->anchor;
6561 size_t buf_len;
6562 size_t len = 0;
6563
6564 switch (type) {
6565 case update_takeover: {
6566 struct imsm_update_takeover *u = (void *)update->buf;
6567 if (u->direction == R0_TO_R10) {
6568 void **tail = (void **)&update->space_list;
6569 struct imsm_dev *dev = get_imsm_dev(super, u->subarray);
6570 struct imsm_map *map = get_imsm_map(dev, 0);
6571 int num_members = map->num_members;
6572 void *space;
6573 int size, i;
6574 int err = 0;
6575 /* allocate memory for added disks */
6576 for (i = 0; i < num_members; i++) {
6577 size = sizeof(struct dl);
6578 space = malloc(size);
6579 if (!space) {
6580 err++;
6581 break;
6582 }
6583 *tail = space;
6584 tail = space;
6585 *tail = NULL;
6586 }
6587 /* allocate memory for new device */
6588 size = sizeof_imsm_dev(super->devlist->dev, 0) +
6589 (num_members * sizeof(__u32));
6590 space = malloc(size);
6591 if (!space)
6592 err++;
6593 else {
6594 *tail = space;
6595 tail = space;
6596 *tail = NULL;
6597 }
6598 if (!err) {
6599 len = disks_to_mpb_size(num_members * 2);
6600 } else {
6601 /* if allocation didn't success, free buffer */
6602 while (update->space_list) {
6603 void **sp = update->space_list;
6604 update->space_list = *sp;
6605 free(sp);
6606 }
6607 }
6608 }
6609
6610 break;
6611 }
6612 case update_reshape_container_disks: {
6613 /* Every raid device in the container is about to
6614 * gain some more devices, and we will enter a
6615 * reconfiguration.
6616 * So each 'imsm_map' will be bigger, and the imsm_vol
6617 * will now hold 2 of them.
6618 * Thus we need new 'struct imsm_dev' allocations sized
6619 * as sizeof_imsm_dev but with more devices in both maps.
6620 */
6621 struct imsm_update_reshape *u = (void *)update->buf;
6622 struct intel_dev *dl;
6623 void **space_tail = (void**)&update->space_list;
6624
6625 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
6626
6627 for (dl = super->devlist; dl; dl = dl->next) {
6628 int size = sizeof_imsm_dev(dl->dev, 1);
6629 void *s;
6630 if (u->new_raid_disks > u->old_raid_disks)
6631 size += sizeof(__u32)*2*
6632 (u->new_raid_disks - u->old_raid_disks);
6633 s = malloc(size);
6634 if (!s)
6635 break;
6636 *space_tail = s;
6637 space_tail = s;
6638 *space_tail = NULL;
6639 }
6640
6641 len = disks_to_mpb_size(u->new_raid_disks);
6642 dprintf("New anchor length is %llu\n", (unsigned long long)len);
6643 break;
6644 }
6645 case update_create_array: {
6646 struct imsm_update_create_array *u = (void *) update->buf;
6647 struct intel_dev *dv;
6648 struct imsm_dev *dev = &u->dev;
6649 struct imsm_map *map = get_imsm_map(dev, 0);
6650 struct dl *dl;
6651 struct disk_info *inf;
6652 int i;
6653 int activate = 0;
6654
6655 inf = get_disk_info(u);
6656 len = sizeof_imsm_dev(dev, 1);
6657 /* allocate a new super->devlist entry */
6658 dv = malloc(sizeof(*dv));
6659 if (dv) {
6660 dv->dev = malloc(len);
6661 if (dv->dev)
6662 update->space = dv;
6663 else {
6664 free(dv);
6665 update->space = NULL;
6666 }
6667 }
6668
6669 /* count how many spares will be converted to members */
6670 for (i = 0; i < map->num_members; i++) {
6671 dl = serial_to_dl(inf[i].serial, super);
6672 if (!dl) {
6673 /* hmm maybe it failed?, nothing we can do about
6674 * it here
6675 */
6676 continue;
6677 }
6678 if (count_memberships(dl, super) == 0)
6679 activate++;
6680 }
6681 len += activate * sizeof(struct imsm_disk);
6682 break;
6683 default:
6684 break;
6685 }
6686 }
6687
6688 /* check if we need a larger metadata buffer */
6689 if (super->next_buf)
6690 buf_len = super->next_len;
6691 else
6692 buf_len = super->len;
6693
6694 if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) {
6695 /* ok we need a larger buf than what is currently allocated
6696 * if this allocation fails process_update will notice that
6697 * ->next_len is set and ->next_buf is NULL
6698 */
6699 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512);
6700 if (super->next_buf)
6701 free(super->next_buf);
6702
6703 super->next_len = buf_len;
6704 if (posix_memalign(&super->next_buf, 512, buf_len) == 0)
6705 memset(super->next_buf, 0, buf_len);
6706 else
6707 super->next_buf = NULL;
6708 }
6709 }
6710
6711 /* must be called while manager is quiesced */
6712 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
6713 {
6714 struct imsm_super *mpb = super->anchor;
6715 struct dl *iter;
6716 struct imsm_dev *dev;
6717 struct imsm_map *map;
6718 int i, j, num_members;
6719 __u32 ord;
6720
6721 dprintf("%s: deleting device[%d] from imsm_super\n",
6722 __func__, index);
6723
6724 /* shift all indexes down one */
6725 for (iter = super->disks; iter; iter = iter->next)
6726 if (iter->index > (int)index)
6727 iter->index--;
6728 for (iter = super->missing; iter; iter = iter->next)
6729 if (iter->index > (int)index)
6730 iter->index--;
6731
6732 for (i = 0; i < mpb->num_raid_devs; i++) {
6733 dev = get_imsm_dev(super, i);
6734 map = get_imsm_map(dev, 0);
6735 num_members = map->num_members;
6736 for (j = 0; j < num_members; j++) {
6737 /* update ord entries being careful not to propagate
6738 * ord-flags to the first map
6739 */
6740 ord = get_imsm_ord_tbl_ent(dev, j, -1);
6741
6742 if (ord_to_idx(ord) <= index)
6743 continue;
6744
6745 map = get_imsm_map(dev, 0);
6746 set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1));
6747 map = get_imsm_map(dev, 1);
6748 if (map)
6749 set_imsm_ord_tbl_ent(map, j, ord - 1);
6750 }
6751 }
6752
6753 mpb->num_disks--;
6754 super->updates_pending++;
6755 if (*dlp) {
6756 struct dl *dl = *dlp;
6757
6758 *dlp = (*dlp)->next;
6759 __free_imsm_disk(dl);
6760 }
6761 }
6762
6763 static char disk_by_path[] = "/dev/disk/by-path/";
6764
6765 static const char *imsm_get_disk_controller_domain(const char *path)
6766 {
6767 char disk_path[PATH_MAX];
6768 char *drv=NULL;
6769 struct stat st;
6770
6771 strncpy(disk_path, disk_by_path, PATH_MAX - 1);
6772 strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
6773 if (stat(disk_path, &st) == 0) {
6774 struct sys_dev* hba;
6775 char *path=NULL;
6776
6777 path = devt_to_devpath(st.st_rdev);
6778 if (path == NULL)
6779 return "unknown";
6780 hba = find_disk_attached_hba(-1, path);
6781 if (hba && hba->type == SYS_DEV_SAS)
6782 drv = "isci";
6783 else if (hba && hba->type == SYS_DEV_SATA)
6784 drv = "ahci";
6785 else
6786 drv = "unknown";
6787 dprintf("path: %s hba: %s attached: %s\n",
6788 path, (hba) ? hba->path : "NULL", drv);
6789 free(path);
6790 if (hba)
6791 free_sys_dev(&hba);
6792 }
6793 return drv;
6794 }
6795
6796 static int imsm_find_array_minor_by_subdev(int subdev, int container, int *minor)
6797 {
6798 char subdev_name[20];
6799 struct mdstat_ent *mdstat;
6800
6801 sprintf(subdev_name, "%d", subdev);
6802 mdstat = mdstat_by_subdev(subdev_name, container);
6803 if (!mdstat)
6804 return -1;
6805
6806 *minor = mdstat->devnum;
6807 free_mdstat(mdstat);
6808 return 0;
6809 }
6810
6811 static int imsm_reshape_is_allowed_on_container(struct supertype *st,
6812 struct geo_params *geo,
6813 int *old_raid_disks)
6814 {
6815 /* currently we only support increasing the number of devices
6816 * for a container. This increases the number of device for each
6817 * member array. They must all be RAID0 or RAID5.
6818 */
6819 int ret_val = 0;
6820 struct mdinfo *info, *member;
6821 int devices_that_can_grow = 0;
6822
6823 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
6824 "st->devnum = (%i)\n",
6825 st->devnum);
6826
6827 if (geo->size != -1 ||
6828 geo->level != UnSet ||
6829 geo->layout != UnSet ||
6830 geo->chunksize != 0 ||
6831 geo->raid_disks == UnSet) {
6832 dprintf("imsm: Container operation is allowed for "
6833 "raid disks number change only.\n");
6834 return ret_val;
6835 }
6836
6837 info = container_content_imsm(st, NULL);
6838 for (member = info; member; member = member->next) {
6839 int result;
6840 int minor;
6841
6842 dprintf("imsm: checking device_num: %i\n",
6843 member->container_member);
6844
6845 if (geo->raid_disks <= member->array.raid_disks) {
6846 /* we work on container for Online Capacity Expansion
6847 * only so raid_disks has to grow
6848 */
6849 dprintf("imsm: for container operation raid disks "
6850 "increase is required\n");
6851 break;
6852 }
6853
6854 if ((info->array.level != 0) &&
6855 (info->array.level != 5)) {
6856 /* we cannot use this container with other raid level
6857 */
6858 dprintf("imsm: for container operation wrong"
6859 " raid level (%i) detected\n",
6860 info->array.level);
6861 break;
6862 } else {
6863 /* check for platform support
6864 * for this raid level configuration
6865 */
6866 struct intel_super *super = st->sb;
6867 if (!is_raid_level_supported(super->orom,
6868 member->array.level,
6869 geo->raid_disks)) {
6870 dprintf("platform does not support raid%d with"
6871 " %d disk%s\n",
6872 info->array.level,
6873 geo->raid_disks,
6874 geo->raid_disks > 1 ? "s" : "");
6875 break;
6876 }
6877 }
6878
6879 if (*old_raid_disks &&
6880 info->array.raid_disks != *old_raid_disks)
6881 break;
6882 *old_raid_disks = info->array.raid_disks;
6883
6884 /* All raid5 and raid0 volumes in container
6885 * have to be ready for Online Capacity Expansion
6886 * so they need to be assembled. We have already
6887 * checked that no recovery etc is happening.
6888 */
6889 result = imsm_find_array_minor_by_subdev(member->container_member,
6890 st->container_dev,
6891 &minor);
6892 if (result < 0) {
6893 dprintf("imsm: cannot find array\n");
6894 break;
6895 }
6896 devices_that_can_grow++;
6897 }
6898 sysfs_free(info);
6899 if (!member && devices_that_can_grow)
6900 ret_val = 1;
6901
6902 if (ret_val)
6903 dprintf("\tContainer operation allowed\n");
6904 else
6905 dprintf("\tError: %i\n", ret_val);
6906
6907 return ret_val;
6908 }
6909
6910 /* Function: get_spares_for_grow
6911 * Description: Allocates memory and creates list of spare devices
6912 * avaliable in container. Checks if spare drive size is acceptable.
6913 * Parameters: Pointer to the supertype structure
6914 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
6915 * NULL if fail
6916 */
6917 static struct mdinfo *get_spares_for_grow(struct supertype *st)
6918 {
6919 unsigned long long min_size = min_acceptable_spare_size_imsm(st);
6920 return container_choose_spares(st, min_size, NULL, NULL, NULL, 0);
6921 }
6922
6923 /******************************************************************************
6924 * function: imsm_create_metadata_update_for_reshape
6925 * Function creates update for whole IMSM container.
6926 *
6927 ******************************************************************************/
6928 static int imsm_create_metadata_update_for_reshape(
6929 struct supertype *st,
6930 struct geo_params *geo,
6931 int old_raid_disks,
6932 struct imsm_update_reshape **updatep)
6933 {
6934 struct intel_super *super = st->sb;
6935 struct imsm_super *mpb = super->anchor;
6936 int update_memory_size = 0;
6937 struct imsm_update_reshape *u = NULL;
6938 struct mdinfo *spares = NULL;
6939 int i;
6940 int delta_disks = 0;
6941 struct mdinfo *dev;
6942
6943 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
6944 geo->raid_disks);
6945
6946 delta_disks = geo->raid_disks - old_raid_disks;
6947
6948 /* size of all update data without anchor */
6949 update_memory_size = sizeof(struct imsm_update_reshape);
6950
6951 /* now add space for spare disks that we need to add. */
6952 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
6953
6954 u = calloc(1, update_memory_size);
6955 if (u == NULL) {
6956 dprintf("error: "
6957 "cannot get memory for imsm_update_reshape update\n");
6958 return 0;
6959 }
6960 u->type = update_reshape_container_disks;
6961 u->old_raid_disks = old_raid_disks;
6962 u->new_raid_disks = geo->raid_disks;
6963
6964 /* now get spare disks list
6965 */
6966 spares = get_spares_for_grow(st);
6967
6968 if (spares == NULL
6969 || delta_disks > spares->array.spare_disks) {
6970 fprintf(stderr, Name ": imsm: ERROR: Cannot get spare devices "
6971 "for %s.\n", geo->dev_name);
6972 goto abort;
6973 }
6974
6975 /* we have got spares
6976 * update disk list in imsm_disk list table in anchor
6977 */
6978 dprintf("imsm: %i spares are available.\n\n",
6979 spares->array.spare_disks);
6980
6981 dev = spares->devs;
6982 for (i = 0; i < delta_disks; i++) {
6983 struct dl *dl;
6984
6985 if (dev == NULL)
6986 break;
6987 u->new_disks[i] = makedev(dev->disk.major,
6988 dev->disk.minor);
6989 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
6990 dl->index = mpb->num_disks;
6991 mpb->num_disks++;
6992 dev = dev->next;
6993 }
6994
6995 abort:
6996 /* free spares
6997 */
6998 sysfs_free(spares);
6999
7000 dprintf("imsm: reshape update preparation :");
7001 if (i == delta_disks) {
7002 dprintf(" OK\n");
7003 *updatep = u;
7004 return update_memory_size;
7005 }
7006 free(u);
7007 dprintf(" Error\n");
7008
7009 return 0;
7010 }
7011
7012 static void imsm_update_metadata_locally(struct supertype *st,
7013 void *buf, int len)
7014 {
7015 struct metadata_update mu;
7016
7017 mu.buf = buf;
7018 mu.len = len;
7019 mu.space = NULL;
7020 mu.space_list = NULL;
7021 mu.next = NULL;
7022 imsm_prepare_update(st, &mu);
7023 imsm_process_update(st, &mu);
7024
7025 while (mu.space_list) {
7026 void **space = mu.space_list;
7027 mu.space_list = *space;
7028 free(space);
7029 }
7030 }
7031
7032 /***************************************************************************
7033 * Function: imsm_analyze_change
7034 * Description: Function analyze change for single volume
7035 * and validate if transition is supported
7036 * Parameters: Geometry parameters, supertype structure
7037 * Returns: Operation type code on success, -1 if fail
7038 ****************************************************************************/
7039 enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
7040 struct geo_params *geo)
7041 {
7042 struct mdinfo info;
7043 int change = -1;
7044 int check_devs = 0;
7045 int chunk;
7046
7047 getinfo_super_imsm_volume(st, &info, NULL);
7048
7049 if ((geo->level != info.array.level) &&
7050 (geo->level >= 0) &&
7051 (geo->level != UnSet)) {
7052 switch (info.array.level) {
7053 case 0:
7054 if (geo->level == 5) {
7055 change = CH_MIGRATION;
7056 check_devs = 1;
7057 }
7058 if (geo->level == 10) {
7059 change = CH_TAKEOVER;
7060 check_devs = 1;
7061 }
7062 break;
7063 case 1:
7064 if (geo->level == 0) {
7065 change = CH_TAKEOVER;
7066 check_devs = 1;
7067 }
7068 break;
7069 case 5:
7070 if (geo->level == 0)
7071 change = CH_MIGRATION;
7072 break;
7073 case 10:
7074 if (geo->level == 0) {
7075 change = CH_TAKEOVER;
7076 check_devs = 1;
7077 }
7078 break;
7079 }
7080 if (change == -1) {
7081 fprintf(stderr,
7082 Name " Error. Level Migration from %d to %d "
7083 "not supported!\n",
7084 info.array.level, geo->level);
7085 goto analyse_change_exit;
7086 }
7087 } else
7088 geo->level = info.array.level;
7089
7090 if ((geo->layout != info.array.layout)
7091 && ((geo->layout != UnSet) && (geo->layout != -1))) {
7092 change = CH_MIGRATION;
7093 if ((info.array.layout == 0)
7094 && (info.array.level == 5)
7095 && (geo->layout == 5)) {
7096 /* reshape 5 -> 4 */
7097 } else if ((info.array.layout == 5)
7098 && (info.array.level == 5)
7099 && (geo->layout == 0)) {
7100 /* reshape 4 -> 5 */
7101 geo->layout = 0;
7102 geo->level = 5;
7103 } else {
7104 fprintf(stderr,
7105 Name " Error. Layout Migration from %d to %d "
7106 "not supported!\n",
7107 info.array.layout, geo->layout);
7108 change = -1;
7109 goto analyse_change_exit;
7110 }
7111 } else
7112 geo->layout = info.array.layout;
7113
7114 if ((geo->chunksize > 0) && (geo->chunksize != UnSet)
7115 && (geo->chunksize != info.array.chunk_size))
7116 change = CH_MIGRATION;
7117 else
7118 geo->chunksize = info.array.chunk_size;
7119
7120 chunk = geo->chunksize / 1024;
7121 if (!validate_geometry_imsm(st,
7122 geo->level,
7123 geo->layout,
7124 geo->raid_disks,
7125 &chunk,
7126 geo->size,
7127 0, 0, 1))
7128 change = -1;
7129
7130 if (check_devs) {
7131 struct intel_super *super = st->sb;
7132 struct imsm_super *mpb = super->anchor;
7133
7134 if (mpb->num_raid_devs > 1) {
7135 fprintf(stderr,
7136 Name " Error. Cannot perform operation on %s"
7137 "- for this operation it MUST be single "
7138 "array in container\n",
7139 geo->dev_name);
7140 change = -1;
7141 }
7142 }
7143
7144 analyse_change_exit:
7145
7146 return change;
7147 }
7148
7149 int imsm_takeover(struct supertype *st, struct geo_params *geo)
7150 {
7151 struct intel_super *super = st->sb;
7152 struct imsm_update_takeover *u;
7153
7154 u = malloc(sizeof(struct imsm_update_takeover));
7155 if (u == NULL)
7156 return 1;
7157
7158 u->type = update_takeover;
7159 u->subarray = super->current_vol;
7160
7161 /* 10->0 transition */
7162 if (geo->level == 0)
7163 u->direction = R10_TO_R0;
7164
7165 /* 0->10 transition */
7166 if (geo->level == 10)
7167 u->direction = R0_TO_R10;
7168
7169 /* update metadata locally */
7170 imsm_update_metadata_locally(st, u,
7171 sizeof(struct imsm_update_takeover));
7172 /* and possibly remotely */
7173 if (st->update_tail)
7174 append_metadata_update(st, u,
7175 sizeof(struct imsm_update_takeover));
7176 else
7177 free(u);
7178
7179 return 0;
7180 }
7181
7182 static int imsm_reshape_super(struct supertype *st, long long size, int level,
7183 int layout, int chunksize, int raid_disks,
7184 int delta_disks, char *backup, char *dev,
7185 int verbose)
7186 {
7187 int ret_val = 1;
7188 struct geo_params geo;
7189
7190 dprintf("imsm: reshape_super called.\n");
7191
7192 memset(&geo, 0, sizeof(struct geo_params));
7193
7194 geo.dev_name = dev;
7195 geo.dev_id = st->devnum;
7196 geo.size = size;
7197 geo.level = level;
7198 geo.layout = layout;
7199 geo.chunksize = chunksize;
7200 geo.raid_disks = raid_disks;
7201 if (delta_disks != UnSet)
7202 geo.raid_disks += delta_disks;
7203
7204 dprintf("\tfor level : %i\n", geo.level);
7205 dprintf("\tfor raid_disks : %i\n", geo.raid_disks);
7206
7207 if (experimental() == 0)
7208 return ret_val;
7209
7210 if (st->container_dev == st->devnum) {
7211 /* On container level we can only increase number of devices. */
7212 dprintf("imsm: info: Container operation\n");
7213 int old_raid_disks = 0;
7214 if (imsm_reshape_is_allowed_on_container(
7215 st, &geo, &old_raid_disks)) {
7216 struct imsm_update_reshape *u = NULL;
7217 int len;
7218
7219 len = imsm_create_metadata_update_for_reshape(
7220 st, &geo, old_raid_disks, &u);
7221
7222 if (len <= 0) {
7223 dprintf("imsm: Cannot prepare update\n");
7224 goto exit_imsm_reshape_super;
7225 }
7226
7227 ret_val = 0;
7228 /* update metadata locally */
7229 imsm_update_metadata_locally(st, u, len);
7230 /* and possibly remotely */
7231 if (st->update_tail)
7232 append_metadata_update(st, u, len);
7233 else
7234 free(u);
7235
7236 } else {
7237 fprintf(stderr, Name ": (imsm) Operation "
7238 "is not allowed on this container\n");
7239 }
7240 } else {
7241 /* On volume level we support following operations
7242 * - takeover: raid10 -> raid0; raid0 -> raid10
7243 * - chunk size migration
7244 * - migration: raid5 -> raid0; raid0 -> raid5
7245 */
7246 struct intel_super *super = st->sb;
7247 struct intel_dev *dev = super->devlist;
7248 int change, devnum;
7249 dprintf("imsm: info: Volume operation\n");
7250 /* find requested device */
7251 while (dev) {
7252 imsm_find_array_minor_by_subdev(dev->index, st->container_dev, &devnum);
7253 if (devnum == geo.dev_id)
7254 break;
7255 dev = dev->next;
7256 }
7257 if (dev == NULL) {
7258 fprintf(stderr, Name " Cannot find %s (%i) subarray\n",
7259 geo.dev_name, geo.dev_id);
7260 goto exit_imsm_reshape_super;
7261 }
7262 super->current_vol = dev->index;
7263 change = imsm_analyze_change(st, &geo);
7264 switch (change) {
7265 case CH_TAKEOVER:
7266 ret_val = imsm_takeover(st, &geo);
7267 break;
7268 case CH_MIGRATION:
7269 ret_val = 0;
7270 break;
7271 default:
7272 ret_val = 1;
7273 }
7274 }
7275
7276 exit_imsm_reshape_super:
7277 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
7278 return ret_val;
7279 }
7280
7281 static int imsm_manage_reshape(
7282 int afd, struct mdinfo *sra, struct reshape *reshape,
7283 struct supertype *st, unsigned long stripes,
7284 int *fds, unsigned long long *offsets,
7285 int dests, int *destfd, unsigned long long *destoffsets)
7286 {
7287 /* Just use child_monitor for now */
7288 return child_monitor(
7289 afd, sra, reshape, st, stripes,
7290 fds, offsets, dests, destfd, destoffsets);
7291 }
7292 #endif /* MDASSEMBLE */
7293
7294 struct superswitch super_imsm = {
7295 #ifndef MDASSEMBLE
7296 .examine_super = examine_super_imsm,
7297 .brief_examine_super = brief_examine_super_imsm,
7298 .brief_examine_subarrays = brief_examine_subarrays_imsm,
7299 .export_examine_super = export_examine_super_imsm,
7300 .detail_super = detail_super_imsm,
7301 .brief_detail_super = brief_detail_super_imsm,
7302 .write_init_super = write_init_super_imsm,
7303 .validate_geometry = validate_geometry_imsm,
7304 .add_to_super = add_to_super_imsm,
7305 .remove_from_super = remove_from_super_imsm,
7306 .detail_platform = detail_platform_imsm,
7307 .kill_subarray = kill_subarray_imsm,
7308 .update_subarray = update_subarray_imsm,
7309 .load_container = load_container_imsm,
7310 .default_geometry = default_geometry_imsm,
7311 .get_disk_controller_domain = imsm_get_disk_controller_domain,
7312 .reshape_super = imsm_reshape_super,
7313 .manage_reshape = imsm_manage_reshape,
7314 #endif
7315 .match_home = match_home_imsm,
7316 .uuid_from_super= uuid_from_super_imsm,
7317 .getinfo_super = getinfo_super_imsm,
7318 .getinfo_super_disks = getinfo_super_disks_imsm,
7319 .update_super = update_super_imsm,
7320
7321 .avail_size = avail_size_imsm,
7322 .min_acceptable_spare_size = min_acceptable_spare_size_imsm,
7323
7324 .compare_super = compare_super_imsm,
7325
7326 .load_super = load_super_imsm,
7327 .init_super = init_super_imsm,
7328 .store_super = store_super_imsm,
7329 .free_super = free_super_imsm,
7330 .match_metadata_desc = match_metadata_desc_imsm,
7331 .container_content = container_content_imsm,
7332
7333 .external = 1,
7334 .name = "imsm",
7335
7336 #ifndef MDASSEMBLE
7337 /* for mdmon */
7338 .open_new = imsm_open_new,
7339 .set_array_state= imsm_set_array_state,
7340 .set_disk = imsm_set_disk,
7341 .sync_metadata = imsm_sync_metadata,
7342 .activate_spare = imsm_activate_spare,
7343 .process_update = imsm_process_update,
7344 .prepare_update = imsm_prepare_update,
7345 #endif /* MDASSEMBLE */
7346 };