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