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