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