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