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