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