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