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