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