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