imsm: add support for checkpointing via 'curr_migr_unit'
[thirdparty/mdadm.git] / super-intel.c
CommitLineData
cdddbdbc
DW
1/*
2 * mdadm - Intel(R) Matrix Storage Manager Support
3 *
a54d5262 4 * Copyright (C) 2002-2008 Intel Corporation
cdddbdbc
DW
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
51006d85 20#define HAVE_STDINT_H 1
cdddbdbc 21#include "mdadm.h"
c2a1e7da 22#include "mdmon.h"
51006d85 23#include "sha1.h"
88c32bb1 24#include "platform-intel.h"
cdddbdbc
DW
25#include <values.h>
26#include <scsi/sg.h>
27#include <ctype.h>
d665cc31 28#include <dirent.h>
cdddbdbc
DW
29
30/* MPB == Metadata Parameter Block */
31#define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
32#define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
33#define MPB_VERSION_RAID0 "1.0.00"
34#define MPB_VERSION_RAID1 "1.1.00"
fe7ed8cb
DW
35#define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
36#define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
cdddbdbc 37#define MPB_VERSION_RAID5 "1.2.02"
fe7ed8cb
DW
38#define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
39#define MPB_VERSION_CNG "1.2.06"
40#define MPB_VERSION_ATTRIBS "1.3.00"
cdddbdbc
DW
41#define MAX_SIGNATURE_LENGTH 32
42#define MAX_RAID_SERIAL_LEN 16
fe7ed8cb
DW
43
44#define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
45#define MPB_ATTRIB_PM __cpu_to_le32(0x40000000)
46#define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
47#define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
48#define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49#define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
50#define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
51#define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
52#define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
53
c2c087e6
DW
54#define MPB_SECTOR_CNT 418
55#define IMSM_RESERVED_SECTORS 4096
979d38be 56#define SECT_PER_MB_SHIFT 11
cdddbdbc
DW
57
58/* Disk configuration info. */
59#define IMSM_MAX_DEVICES 255
60struct imsm_disk {
61 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
62 __u32 total_blocks; /* 0xE8 - 0xEB total blocks */
63 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
f2f27e63
DW
64#define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
65#define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
66#define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
cdddbdbc 67 __u32 status; /* 0xF0 - 0xF3 */
fe7ed8cb
DW
68 __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
69#define IMSM_DISK_FILLERS 4
cdddbdbc
DW
70 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */
71};
72
73/* RAID map configuration infos. */
74struct imsm_map {
75 __u32 pba_of_lba0; /* start address of partition */
76 __u32 blocks_per_member;/* blocks per member */
77 __u32 num_data_stripes; /* number of data stripes */
78 __u16 blocks_per_strip;
79 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
80#define IMSM_T_STATE_NORMAL 0
81#define IMSM_T_STATE_UNINITIALIZED 1
e3bba0e0
DW
82#define IMSM_T_STATE_DEGRADED 2
83#define IMSM_T_STATE_FAILED 3
cdddbdbc
DW
84 __u8 raid_level;
85#define IMSM_T_RAID0 0
86#define IMSM_T_RAID1 1
87#define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
88 __u8 num_members; /* number of member disks */
fe7ed8cb
DW
89 __u8 num_domains; /* number of parity domains */
90 __u8 failed_disk_num; /* valid only when state is degraded */
252d23c0 91 __u8 ddf;
cdddbdbc 92 __u32 filler[7]; /* expansion area */
7eef0453 93#define IMSM_ORD_REBUILD (1 << 24)
cdddbdbc 94 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
7eef0453
DW
95 * top byte contains some flags
96 */
cdddbdbc
DW
97} __attribute__ ((packed));
98
99struct imsm_vol {
f8f603f1 100 __u32 curr_migr_unit;
fe7ed8cb 101 __u32 checkpoint_id; /* id to access curr_migr_unit */
cdddbdbc 102 __u8 migr_state; /* Normal or Migrating */
e3bba0e0
DW
103#define MIGR_INIT 0
104#define MIGR_REBUILD 1
105#define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
106#define MIGR_GEN_MIGR 3
107#define MIGR_STATE_CHANGE 4
1484e727 108#define MIGR_REPAIR 5
cdddbdbc
DW
109 __u8 migr_type; /* Initializing, Rebuilding, ... */
110 __u8 dirty;
fe7ed8cb
DW
111 __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
112 __u16 verify_errors; /* number of mismatches */
113 __u16 bad_blocks; /* number of bad blocks during verify */
114 __u32 filler[4];
cdddbdbc
DW
115 struct imsm_map map[1];
116 /* here comes another one if migr_state */
117} __attribute__ ((packed));
118
119struct imsm_dev {
fe7ed8cb 120 __u8 volume[MAX_RAID_SERIAL_LEN];
cdddbdbc
DW
121 __u32 size_low;
122 __u32 size_high;
fe7ed8cb
DW
123#define DEV_BOOTABLE __cpu_to_le32(0x01)
124#define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
125#define DEV_READ_COALESCING __cpu_to_le32(0x04)
126#define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
127#define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
128#define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
129#define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
130#define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
131#define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
132#define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
133#define DEV_CLONE_N_GO __cpu_to_le32(0x400)
134#define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
135#define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
cdddbdbc
DW
136 __u32 status; /* Persistent RaidDev status */
137 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
fe7ed8cb
DW
138 __u8 migr_priority;
139 __u8 num_sub_vols;
140 __u8 tid;
141 __u8 cng_master_disk;
142 __u16 cache_policy;
143 __u8 cng_state;
144 __u8 cng_sub_state;
145#define IMSM_DEV_FILLERS 10
cdddbdbc
DW
146 __u32 filler[IMSM_DEV_FILLERS];
147 struct imsm_vol vol;
148} __attribute__ ((packed));
149
150struct imsm_super {
151 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
152 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
153 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
154 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
155 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
604b746f
JD
156 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
157 __u32 attributes; /* 0x34 - 0x37 */
cdddbdbc
DW
158 __u8 num_disks; /* 0x38 Number of configured disks */
159 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
604b746f
JD
160 __u8 error_log_pos; /* 0x3A */
161 __u8 fill[1]; /* 0x3B */
162 __u32 cache_size; /* 0x3c - 0x40 in mb */
163 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
164 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
165 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
166#define IMSM_FILLERS 35
167 __u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
cdddbdbc
DW
168 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
169 /* here comes imsm_dev[num_raid_devs] */
604b746f 170 /* here comes BBM logs */
cdddbdbc
DW
171} __attribute__ ((packed));
172
604b746f
JD
173#define BBM_LOG_MAX_ENTRIES 254
174
175struct bbm_log_entry {
176 __u64 defective_block_start;
177#define UNREADABLE 0xFFFFFFFF
178 __u32 spare_block_offset;
179 __u16 remapped_marked_count;
180 __u16 disk_ordinal;
181} __attribute__ ((__packed__));
182
183struct bbm_log {
184 __u32 signature; /* 0xABADB10C */
185 __u32 entry_count;
186 __u32 reserved_spare_block_count; /* 0 */
187 __u32 reserved; /* 0xFFFF */
188 __u64 first_spare_lba;
189 struct bbm_log_entry mapped_block_entries[BBM_LOG_MAX_ENTRIES];
190} __attribute__ ((__packed__));
191
192
cdddbdbc
DW
193#ifndef MDASSEMBLE
194static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
195#endif
196
1484e727
DW
197static __u8 migr_type(struct imsm_dev *dev)
198{
199 if (dev->vol.migr_type == MIGR_VERIFY &&
200 dev->status & DEV_VERIFY_AND_FIX)
201 return MIGR_REPAIR;
202 else
203 return dev->vol.migr_type;
204}
205
206static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
207{
208 /* for compatibility with older oroms convert MIGR_REPAIR, into
209 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
210 */
211 if (migr_type == MIGR_REPAIR) {
212 dev->vol.migr_type = MIGR_VERIFY;
213 dev->status |= DEV_VERIFY_AND_FIX;
214 } else {
215 dev->vol.migr_type = migr_type;
216 dev->status &= ~DEV_VERIFY_AND_FIX;
217 }
218}
219
87eb16df 220static unsigned int sector_count(__u32 bytes)
cdddbdbc 221{
87eb16df
DW
222 return ((bytes + (512-1)) & (~(512-1))) / 512;
223}
cdddbdbc 224
87eb16df
DW
225static unsigned int mpb_sectors(struct imsm_super *mpb)
226{
227 return sector_count(__le32_to_cpu(mpb->mpb_size));
cdddbdbc
DW
228}
229
ba2de7ba
DW
230struct intel_dev {
231 struct imsm_dev *dev;
232 struct intel_dev *next;
233 int index;
234};
235
cdddbdbc
DW
236/* internal representation of IMSM metadata */
237struct intel_super {
238 union {
949c47a0
DW
239 void *buf; /* O_DIRECT buffer for reading/writing metadata */
240 struct imsm_super *anchor; /* immovable parameters */
cdddbdbc 241 };
949c47a0 242 size_t len; /* size of the 'buf' allocation */
4d7b1503
DW
243 void *next_buf; /* for realloc'ing buf from the manager */
244 size_t next_len;
c2c087e6
DW
245 int updates_pending; /* count of pending updates for mdmon */
246 int creating_imsm; /* flag to indicate container creation */
bf5a934a 247 int current_vol; /* index of raid device undergoing creation */
0dcecb2e 248 __u32 create_offset; /* common start for 'current_vol' */
148acb7b 249 __u32 random; /* random data for seeding new family numbers */
ba2de7ba 250 struct intel_dev *devlist;
cdddbdbc
DW
251 struct dl {
252 struct dl *next;
253 int index;
254 __u8 serial[MAX_RAID_SERIAL_LEN];
255 int major, minor;
256 char *devname;
b9f594fe 257 struct imsm_disk disk;
cdddbdbc 258 int fd;
0dcecb2e
DW
259 int extent_cnt;
260 struct extent *e; /* for determining freespace @ create */
efb30e7f 261 int raiddisk; /* slot to fill in autolayout */
cdddbdbc 262 } *disks;
43dad3d6 263 struct dl *add; /* list of disks to add while mdmon active */
47ee5a45 264 struct dl *missing; /* disks removed while we weren't looking */
43dad3d6 265 struct bbm_log *bbm_log;
88c32bb1
DW
266 const char *hba; /* device path of the raid controller for this metadata */
267 const struct imsm_orom *orom; /* platform firmware support */
a2b97981
DW
268 struct intel_super *next; /* (temp) list for disambiguating family_num */
269};
270
271struct intel_disk {
272 struct imsm_disk disk;
273 #define IMSM_UNKNOWN_OWNER (-1)
274 int owner;
275 struct intel_disk *next;
cdddbdbc
DW
276};
277
c2c087e6
DW
278struct extent {
279 unsigned long long start, size;
280};
281
88758e9d
DW
282/* definition of messages passed to imsm_process_update */
283enum imsm_update_type {
284 update_activate_spare,
8273f55e 285 update_create_array,
43dad3d6 286 update_add_disk,
88758e9d
DW
287};
288
289struct imsm_update_activate_spare {
290 enum imsm_update_type type;
d23fe947 291 struct dl *dl;
88758e9d
DW
292 int slot;
293 int array;
294 struct imsm_update_activate_spare *next;
295};
296
54c2c1ea
DW
297struct disk_info {
298 __u8 serial[MAX_RAID_SERIAL_LEN];
299};
300
8273f55e
DW
301struct imsm_update_create_array {
302 enum imsm_update_type type;
8273f55e 303 int dev_idx;
6a3e913e 304 struct imsm_dev dev;
8273f55e
DW
305};
306
43dad3d6
DW
307struct imsm_update_add_disk {
308 enum imsm_update_type type;
309};
310
cdddbdbc
DW
311static struct supertype *match_metadata_desc_imsm(char *arg)
312{
313 struct supertype *st;
314
315 if (strcmp(arg, "imsm") != 0 &&
316 strcmp(arg, "default") != 0
317 )
318 return NULL;
319
320 st = malloc(sizeof(*st));
ef609477 321 memset(st, 0, sizeof(*st));
cdddbdbc
DW
322 st->ss = &super_imsm;
323 st->max_devs = IMSM_MAX_DEVICES;
324 st->minor_version = 0;
325 st->sb = NULL;
326 return st;
327}
328
0e600426 329#ifndef MDASSEMBLE
cdddbdbc
DW
330static __u8 *get_imsm_version(struct imsm_super *mpb)
331{
332 return &mpb->sig[MPB_SIG_LEN];
333}
0e600426 334#endif
cdddbdbc 335
949c47a0
DW
336/* retrieve a disk directly from the anchor when the anchor is known to be
337 * up-to-date, currently only at load time
338 */
339static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
cdddbdbc 340{
949c47a0 341 if (index >= mpb->num_disks)
cdddbdbc
DW
342 return NULL;
343 return &mpb->disk[index];
344}
345
0e600426 346#ifndef MDASSEMBLE
b9f594fe 347/* retrieve a disk from the parsed metadata */
949c47a0
DW
348static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
349{
b9f594fe
DW
350 struct dl *d;
351
352 for (d = super->disks; d; d = d->next)
353 if (d->index == index)
354 return &d->disk;
355
356 return NULL;
949c47a0 357}
0e600426 358#endif
949c47a0
DW
359
360/* generate a checksum directly from the anchor when the anchor is known to be
361 * up-to-date, currently only at load or write_super after coalescing
362 */
363static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
cdddbdbc
DW
364{
365 __u32 end = mpb->mpb_size / sizeof(end);
366 __u32 *p = (__u32 *) mpb;
367 __u32 sum = 0;
368
97f734fd
N
369 while (end--) {
370 sum += __le32_to_cpu(*p);
371 p++;
372 }
cdddbdbc
DW
373
374 return sum - __le32_to_cpu(mpb->check_sum);
375}
376
a965f303
DW
377static size_t sizeof_imsm_map(struct imsm_map *map)
378{
379 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
380}
381
382struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
cdddbdbc 383{
a965f303
DW
384 struct imsm_map *map = &dev->vol.map[0];
385
386 if (second_map && !dev->vol.migr_state)
387 return NULL;
388 else if (second_map) {
389 void *ptr = map;
390
391 return ptr + sizeof_imsm_map(map);
392 } else
393 return map;
394
395}
cdddbdbc 396
3393c6af
DW
397/* return the size of the device.
398 * migr_state increases the returned size if map[0] were to be duplicated
399 */
400static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
a965f303
DW
401{
402 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
403 sizeof_imsm_map(get_imsm_map(dev, 0));
cdddbdbc
DW
404
405 /* migrating means an additional map */
a965f303
DW
406 if (dev->vol.migr_state)
407 size += sizeof_imsm_map(get_imsm_map(dev, 1));
3393c6af
DW
408 else if (migr_state)
409 size += sizeof_imsm_map(get_imsm_map(dev, 0));
cdddbdbc
DW
410
411 return size;
412}
413
54c2c1ea
DW
414#ifndef MDASSEMBLE
415/* retrieve disk serial number list from a metadata update */
416static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
417{
418 void *u = update;
419 struct disk_info *inf;
420
421 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
422 sizeof_imsm_dev(&update->dev, 0);
423
424 return inf;
425}
426#endif
427
949c47a0 428static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
cdddbdbc
DW
429{
430 int offset;
431 int i;
432 void *_mpb = mpb;
433
949c47a0 434 if (index >= mpb->num_raid_devs)
cdddbdbc
DW
435 return NULL;
436
437 /* devices start after all disks */
438 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
439
440 for (i = 0; i <= index; i++)
441 if (i == index)
442 return _mpb + offset;
443 else
3393c6af 444 offset += sizeof_imsm_dev(_mpb + offset, 0);
cdddbdbc
DW
445
446 return NULL;
447}
448
949c47a0
DW
449static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
450{
ba2de7ba
DW
451 struct intel_dev *dv;
452
949c47a0
DW
453 if (index >= super->anchor->num_raid_devs)
454 return NULL;
ba2de7ba
DW
455 for (dv = super->devlist; dv; dv = dv->next)
456 if (dv->index == index)
457 return dv->dev;
458 return NULL;
949c47a0
DW
459}
460
7eef0453
DW
461static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, int slot)
462{
463 struct imsm_map *map;
464
465 if (dev->vol.migr_state)
7eef0453 466 map = get_imsm_map(dev, 1);
fb9bf0d3
DW
467 else
468 map = get_imsm_map(dev, 0);
7eef0453 469
ff077194
DW
470 /* top byte identifies disk under rebuild */
471 return __le32_to_cpu(map->disk_ord_tbl[slot]);
472}
473
474#define ord_to_idx(ord) (((ord) << 8) >> 8)
475static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot)
476{
477 __u32 ord = get_imsm_ord_tbl_ent(dev, slot);
478
479 return ord_to_idx(ord);
7eef0453
DW
480}
481
be73972f
DW
482static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
483{
484 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
485}
486
620b1713
DW
487static int get_imsm_disk_slot(struct imsm_map *map, int idx)
488{
489 int slot;
490 __u32 ord;
491
492 for (slot = 0; slot < map->num_members; slot++) {
493 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
494 if (ord_to_idx(ord) == idx)
495 return slot;
496 }
497
498 return -1;
499}
500
cdddbdbc
DW
501static int get_imsm_raid_level(struct imsm_map *map)
502{
503 if (map->raid_level == 1) {
504 if (map->num_members == 2)
505 return 1;
506 else
507 return 10;
508 }
509
510 return map->raid_level;
511}
512
c2c087e6
DW
513static int cmp_extent(const void *av, const void *bv)
514{
515 const struct extent *a = av;
516 const struct extent *b = bv;
517 if (a->start < b->start)
518 return -1;
519 if (a->start > b->start)
520 return 1;
521 return 0;
522}
523
0dcecb2e 524static int count_memberships(struct dl *dl, struct intel_super *super)
c2c087e6 525{
c2c087e6 526 int memberships = 0;
620b1713 527 int i;
c2c087e6 528
949c47a0
DW
529 for (i = 0; i < super->anchor->num_raid_devs; i++) {
530 struct imsm_dev *dev = get_imsm_dev(super, i);
a965f303 531 struct imsm_map *map = get_imsm_map(dev, 0);
c2c087e6 532
620b1713
DW
533 if (get_imsm_disk_slot(map, dl->index) >= 0)
534 memberships++;
c2c087e6 535 }
0dcecb2e
DW
536
537 return memberships;
538}
539
540static struct extent *get_extents(struct intel_super *super, struct dl *dl)
541{
542 /* find a list of used extents on the given physical device */
543 struct extent *rv, *e;
620b1713 544 int i;
0dcecb2e
DW
545 int memberships = count_memberships(dl, super);
546 __u32 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
547
c2c087e6
DW
548 rv = malloc(sizeof(struct extent) * (memberships + 1));
549 if (!rv)
550 return NULL;
551 e = rv;
552
949c47a0
DW
553 for (i = 0; i < super->anchor->num_raid_devs; i++) {
554 struct imsm_dev *dev = get_imsm_dev(super, i);
a965f303 555 struct imsm_map *map = get_imsm_map(dev, 0);
c2c087e6 556
620b1713
DW
557 if (get_imsm_disk_slot(map, dl->index) >= 0) {
558 e->start = __le32_to_cpu(map->pba_of_lba0);
559 e->size = __le32_to_cpu(map->blocks_per_member);
560 e++;
c2c087e6
DW
561 }
562 }
563 qsort(rv, memberships, sizeof(*rv), cmp_extent);
564
14e8215b
DW
565 /* determine the start of the metadata
566 * when no raid devices are defined use the default
567 * ...otherwise allow the metadata to truncate the value
568 * as is the case with older versions of imsm
569 */
570 if (memberships) {
571 struct extent *last = &rv[memberships - 1];
572 __u32 remainder;
573
574 remainder = __le32_to_cpu(dl->disk.total_blocks) -
575 (last->start + last->size);
dda5855f
DW
576 /* round down to 1k block to satisfy precision of the kernel
577 * 'size' interface
578 */
579 remainder &= ~1UL;
580 /* make sure remainder is still sane */
581 if (remainder < ROUND_UP(super->len, 512) >> 9)
582 remainder = ROUND_UP(super->len, 512) >> 9;
14e8215b
DW
583 if (reservation > remainder)
584 reservation = remainder;
585 }
586 e->start = __le32_to_cpu(dl->disk.total_blocks) - reservation;
c2c087e6
DW
587 e->size = 0;
588 return rv;
589}
590
14e8215b
DW
591/* try to determine how much space is reserved for metadata from
592 * the last get_extents() entry, otherwise fallback to the
593 * default
594 */
595static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
596{
597 struct extent *e;
598 int i;
599 __u32 rv;
600
601 /* for spares just return a minimal reservation which will grow
602 * once the spare is picked up by an array
603 */
604 if (dl->index == -1)
605 return MPB_SECTOR_CNT;
606
607 e = get_extents(super, dl);
608 if (!e)
609 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
610
611 /* scroll to last entry */
612 for (i = 0; e[i].size; i++)
613 continue;
614
615 rv = __le32_to_cpu(dl->disk.total_blocks) - e[i].start;
616
617 free(e);
618
619 return rv;
620}
621
25ed7e59
DW
622static int is_spare(struct imsm_disk *disk)
623{
624 return (disk->status & SPARE_DISK) == SPARE_DISK;
625}
626
627static int is_configured(struct imsm_disk *disk)
628{
629 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
630}
631
632static int is_failed(struct imsm_disk *disk)
633{
634 return (disk->status & FAILED_DISK) == FAILED_DISK;
635}
636
1799c9e8 637#ifndef MDASSEMBLE
1e5c6983
DW
638static __u64 blocks_per_migr_unit(struct imsm_dev *dev);
639
44470971 640static void print_imsm_dev(struct imsm_dev *dev, char *uuid, int disk_idx)
cdddbdbc
DW
641{
642 __u64 sz;
643 int slot;
a965f303 644 struct imsm_map *map = get_imsm_map(dev, 0);
b10b37b8 645 __u32 ord;
cdddbdbc
DW
646
647 printf("\n");
1e7bc0ed 648 printf("[%.16s]:\n", dev->volume);
44470971 649 printf(" UUID : %s\n", uuid);
cdddbdbc
DW
650 printf(" RAID Level : %d\n", get_imsm_raid_level(map));
651 printf(" Members : %d\n", map->num_members);
620b1713
DW
652 slot = get_imsm_disk_slot(map, disk_idx);
653 if (slot >= 0) {
b10b37b8
DW
654 ord = get_imsm_ord_tbl_ent(dev, slot);
655 printf(" This Slot : %d%s\n", slot,
656 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
657 } else
cdddbdbc
DW
658 printf(" This Slot : ?\n");
659 sz = __le32_to_cpu(dev->size_high);
660 sz <<= 32;
661 sz += __le32_to_cpu(dev->size_low);
662 printf(" Array Size : %llu%s\n", (unsigned long long)sz,
663 human_size(sz * 512));
664 sz = __le32_to_cpu(map->blocks_per_member);
665 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz,
666 human_size(sz * 512));
667 printf(" Sector Offset : %u\n",
668 __le32_to_cpu(map->pba_of_lba0));
669 printf(" Num Stripes : %u\n",
670 __le32_to_cpu(map->num_data_stripes));
671 printf(" Chunk Size : %u KiB\n",
672 __le16_to_cpu(map->blocks_per_strip) / 2);
673 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
8655a7b1 674 printf(" Migrate State : ");
1484e727
DW
675 if (dev->vol.migr_state) {
676 if (migr_type(dev) == MIGR_INIT)
8655a7b1 677 printf("initialize\n");
1484e727 678 else if (migr_type(dev) == MIGR_REBUILD)
8655a7b1 679 printf("rebuild\n");
1484e727 680 else if (migr_type(dev) == MIGR_VERIFY)
8655a7b1 681 printf("check\n");
1484e727 682 else if (migr_type(dev) == MIGR_GEN_MIGR)
8655a7b1 683 printf("general migration\n");
1484e727 684 else if (migr_type(dev) == MIGR_STATE_CHANGE)
8655a7b1 685 printf("state change\n");
1484e727 686 else if (migr_type(dev) == MIGR_REPAIR)
8655a7b1 687 printf("repair\n");
1484e727 688 else
8655a7b1
DW
689 printf("<unknown:%d>\n", migr_type(dev));
690 } else
691 printf("idle\n");
3393c6af
DW
692 printf(" Map State : %s", map_state_str[map->map_state]);
693 if (dev->vol.migr_state) {
694 struct imsm_map *map = get_imsm_map(dev, 1);
1e5c6983 695
b10b37b8 696 printf(" <-- %s", map_state_str[map->map_state]);
1e5c6983
DW
697 printf("\n Checkpoint : %u (%llu)",
698 __le32_to_cpu(dev->vol.curr_migr_unit),
699 blocks_per_migr_unit(dev));
3393c6af
DW
700 }
701 printf("\n");
cdddbdbc 702 printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
cdddbdbc
DW
703}
704
14e8215b 705static void print_imsm_disk(struct imsm_super *mpb, int index, __u32 reserved)
cdddbdbc 706{
949c47a0 707 struct imsm_disk *disk = __get_imsm_disk(mpb, index);
1f24f035 708 char str[MAX_RAID_SERIAL_LEN + 1];
cdddbdbc
DW
709 __u64 sz;
710
e9d82038
DW
711 if (index < 0)
712 return;
713
cdddbdbc 714 printf("\n");
1f24f035 715 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
cdddbdbc 716 printf(" Disk%02d Serial : %s\n", index, str);
25ed7e59
DW
717 printf(" State :%s%s%s\n", is_spare(disk) ? " spare" : "",
718 is_configured(disk) ? " active" : "",
719 is_failed(disk) ? " failed" : "");
cdddbdbc 720 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
14e8215b 721 sz = __le32_to_cpu(disk->total_blocks) - reserved;
cdddbdbc
DW
722 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
723 human_size(sz * 512));
724}
725
44470971
DW
726static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info);
727
cdddbdbc
DW
728static void examine_super_imsm(struct supertype *st, char *homehost)
729{
730 struct intel_super *super = st->sb;
949c47a0 731 struct imsm_super *mpb = super->anchor;
cdddbdbc
DW
732 char str[MAX_SIGNATURE_LENGTH];
733 int i;
27fd6274
DW
734 struct mdinfo info;
735 char nbuf[64];
cdddbdbc 736 __u32 sum;
14e8215b 737 __u32 reserved = imsm_reserved_sectors(super, super->disks);
cdddbdbc 738
27fd6274 739
cdddbdbc
DW
740 snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
741 printf(" Magic : %s\n", str);
742 snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
743 printf(" Version : %s\n", get_imsm_version(mpb));
148acb7b 744 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
cdddbdbc
DW
745 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
746 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
27fd6274 747 getinfo_super_imsm(st, &info);
ae2bfd4e 748 fname_from_uuid(st, &info, nbuf, ':');
27fd6274 749 printf(" UUID : %s\n", nbuf + 5);
cdddbdbc
DW
750 sum = __le32_to_cpu(mpb->check_sum);
751 printf(" Checksum : %08x %s\n", sum,
949c47a0 752 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
87eb16df 753 printf(" MPB Sectors : %d\n", mpb_sectors(mpb));
cdddbdbc
DW
754 printf(" Disks : %d\n", mpb->num_disks);
755 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
14e8215b 756 print_imsm_disk(mpb, super->disks->index, reserved);
604b746f
JD
757 if (super->bbm_log) {
758 struct bbm_log *log = super->bbm_log;
759
760 printf("\n");
761 printf("Bad Block Management Log:\n");
762 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
763 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
764 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
765 printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count));
13a3b65d
N
766 printf(" First Spare : %llx\n",
767 (unsigned long long) __le64_to_cpu(log->first_spare_lba));
604b746f 768 }
44470971
DW
769 for (i = 0; i < mpb->num_raid_devs; i++) {
770 struct mdinfo info;
771 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
772
773 super->current_vol = i;
774 getinfo_super_imsm(st, &info);
ae2bfd4e 775 fname_from_uuid(st, &info, nbuf, ':');
44470971
DW
776 print_imsm_dev(dev, nbuf + 5, super->disks->index);
777 }
cdddbdbc
DW
778 for (i = 0; i < mpb->num_disks; i++) {
779 if (i == super->disks->index)
780 continue;
14e8215b 781 print_imsm_disk(mpb, i, reserved);
cdddbdbc
DW
782 }
783}
784
061f2c6a 785static void brief_examine_super_imsm(struct supertype *st, int verbose)
cdddbdbc 786{
27fd6274 787 /* We just write a generic IMSM ARRAY entry */
ff54de6e
N
788 struct mdinfo info;
789 char nbuf[64];
1e7bc0ed 790 struct intel_super *super = st->sb;
1e7bc0ed 791
0d5a423f
DW
792 if (!super->anchor->num_raid_devs) {
793 printf("ARRAY metadata=imsm\n");
1e7bc0ed 794 return;
0d5a423f 795 }
ff54de6e 796
4737ae25
N
797 getinfo_super_imsm(st, &info);
798 fname_from_uuid(st, &info, nbuf, ':');
799 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
800}
801
802static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
803{
804 /* We just write a generic IMSM ARRAY entry */
805 struct mdinfo info;
806 char nbuf[64];
807 char nbuf1[64];
808 struct intel_super *super = st->sb;
809 int i;
810
811 if (!super->anchor->num_raid_devs)
812 return;
813
ff54de6e 814 getinfo_super_imsm(st, &info);
ae2bfd4e 815 fname_from_uuid(st, &info, nbuf, ':');
1e7bc0ed
DW
816 for (i = 0; i < super->anchor->num_raid_devs; i++) {
817 struct imsm_dev *dev = get_imsm_dev(super, i);
818
819 super->current_vol = i;
820 getinfo_super_imsm(st, &info);
ae2bfd4e 821 fname_from_uuid(st, &info, nbuf1, ':');
1124b3cf 822 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
cf8de691 823 dev->volume, nbuf + 5, i, nbuf1 + 5);
1e7bc0ed 824 }
cdddbdbc
DW
825}
826
9d84c8ea
DW
827static void export_examine_super_imsm(struct supertype *st)
828{
829 struct intel_super *super = st->sb;
830 struct imsm_super *mpb = super->anchor;
831 struct mdinfo info;
832 char nbuf[64];
833
834 getinfo_super_imsm(st, &info);
835 fname_from_uuid(st, &info, nbuf, ':');
836 printf("MD_METADATA=imsm\n");
837 printf("MD_LEVEL=container\n");
838 printf("MD_UUID=%s\n", nbuf+5);
839 printf("MD_DEVICES=%u\n", mpb->num_disks);
840}
841
cdddbdbc
DW
842static void detail_super_imsm(struct supertype *st, char *homehost)
843{
3ebe00a1
DW
844 struct mdinfo info;
845 char nbuf[64];
846
847 getinfo_super_imsm(st, &info);
ae2bfd4e 848 fname_from_uuid(st, &info, nbuf, ':');
3ebe00a1 849 printf("\n UUID : %s\n", nbuf + 5);
cdddbdbc
DW
850}
851
852static void brief_detail_super_imsm(struct supertype *st)
853{
ff54de6e
N
854 struct mdinfo info;
855 char nbuf[64];
856 getinfo_super_imsm(st, &info);
ae2bfd4e 857 fname_from_uuid(st, &info, nbuf, ':');
ff54de6e 858 printf(" UUID=%s", nbuf + 5);
cdddbdbc 859}
d665cc31
DW
860
861static int imsm_read_serial(int fd, char *devname, __u8 *serial);
862static void fd2devname(int fd, char *name);
863
864static int imsm_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
865{
866 /* dump an unsorted list of devices attached to ahci, as well as
867 * non-connected ports
868 */
869 int hba_len = strlen(hba_path) + 1;
870 struct dirent *ent;
871 DIR *dir;
872 char *path = NULL;
873 int err = 0;
874 unsigned long port_mask = (1 << port_count) - 1;
875
876 if (port_count > sizeof(port_mask) * 8) {
877 if (verbose)
878 fprintf(stderr, Name ": port_count %d out of range\n", port_count);
879 return 2;
880 }
881
882 /* scroll through /sys/dev/block looking for devices attached to
883 * this hba
884 */
885 dir = opendir("/sys/dev/block");
886 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
887 int fd;
888 char model[64];
889 char vendor[64];
890 char buf[1024];
891 int major, minor;
892 char *device;
893 char *c;
894 int port;
895 int type;
896
897 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
898 continue;
899 path = devt_to_devpath(makedev(major, minor));
900 if (!path)
901 continue;
902 if (!path_attached_to_hba(path, hba_path)) {
903 free(path);
904 path = NULL;
905 continue;
906 }
907
908 /* retrieve the scsi device type */
909 if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) {
910 if (verbose)
911 fprintf(stderr, Name ": failed to allocate 'device'\n");
912 err = 2;
913 break;
914 }
915 sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
916 if (load_sys(device, buf) != 0) {
917 if (verbose)
918 fprintf(stderr, Name ": failed to read device type for %s\n",
919 path);
920 err = 2;
921 free(device);
922 break;
923 }
924 type = strtoul(buf, NULL, 10);
925
926 /* if it's not a disk print the vendor and model */
927 if (!(type == 0 || type == 7 || type == 14)) {
928 vendor[0] = '\0';
929 model[0] = '\0';
930 sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
931 if (load_sys(device, buf) == 0) {
932 strncpy(vendor, buf, sizeof(vendor));
933 vendor[sizeof(vendor) - 1] = '\0';
934 c = (char *) &vendor[sizeof(vendor) - 1];
935 while (isspace(*c) || *c == '\0')
936 *c-- = '\0';
937
938 }
939 sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
940 if (load_sys(device, buf) == 0) {
941 strncpy(model, buf, sizeof(model));
942 model[sizeof(model) - 1] = '\0';
943 c = (char *) &model[sizeof(model) - 1];
944 while (isspace(*c) || *c == '\0')
945 *c-- = '\0';
946 }
947
948 if (vendor[0] && model[0])
949 sprintf(buf, "%.64s %.64s", vendor, model);
950 else
951 switch (type) { /* numbers from hald/linux/device.c */
952 case 1: sprintf(buf, "tape"); break;
953 case 2: sprintf(buf, "printer"); break;
954 case 3: sprintf(buf, "processor"); break;
955 case 4:
956 case 5: sprintf(buf, "cdrom"); break;
957 case 6: sprintf(buf, "scanner"); break;
958 case 8: sprintf(buf, "media_changer"); break;
959 case 9: sprintf(buf, "comm"); break;
960 case 12: sprintf(buf, "raid"); break;
961 default: sprintf(buf, "unknown");
962 }
963 } else
964 buf[0] = '\0';
965 free(device);
966
967 /* chop device path to 'host%d' and calculate the port number */
968 c = strchr(&path[hba_len], '/');
969 *c = '\0';
970 if (sscanf(&path[hba_len], "host%d", &port) == 1)
971 port -= host_base;
972 else {
973 if (verbose) {
974 *c = '/'; /* repair the full string */
975 fprintf(stderr, Name ": failed to determine port number for %s\n",
976 path);
977 }
978 err = 2;
979 break;
980 }
981
982 /* mark this port as used */
983 port_mask &= ~(1 << port);
984
985 /* print out the device information */
986 if (buf[0]) {
987 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
988 continue;
989 }
990
991 fd = dev_open(ent->d_name, O_RDONLY);
992 if (fd < 0)
993 printf(" Port%d : - disk info unavailable -\n", port);
994 else {
995 fd2devname(fd, buf);
996 printf(" Port%d : %s", port, buf);
997 if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0)
998 printf(" (%s)\n", buf);
999 else
1000 printf("()\n");
1001 }
1002 close(fd);
1003 free(path);
1004 path = NULL;
1005 }
1006 if (path)
1007 free(path);
1008 if (dir)
1009 closedir(dir);
1010 if (err == 0) {
1011 int i;
1012
1013 for (i = 0; i < port_count; i++)
1014 if (port_mask & (1 << i))
1015 printf(" Port%d : - no device attached -\n", i);
1016 }
1017
1018 return err;
1019}
1020
5615172f 1021static int detail_platform_imsm(int verbose, int enumerate_only)
d665cc31
DW
1022{
1023 /* There are two components to imsm platform support, the ahci SATA
1024 * controller and the option-rom. To find the SATA controller we
1025 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1026 * controller with the Intel vendor id is present. This approach
1027 * allows mdadm to leverage the kernel's ahci detection logic, with the
1028 * caveat that if ahci.ko is not loaded mdadm will not be able to
1029 * detect platform raid capabilities. The option-rom resides in a
1030 * platform "Adapter ROM". We scan for its signature to retrieve the
1031 * platform capabilities. If raid support is disabled in the BIOS the
1032 * option-rom capability structure will not be available.
1033 */
1034 const struct imsm_orom *orom;
1035 struct sys_dev *list, *hba;
1036 DIR *dir;
1037 struct dirent *ent;
1038 const char *hba_path;
1039 int host_base = 0;
1040 int port_count = 0;
1041
5615172f
DW
1042 if (enumerate_only) {
1043 if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
1044 return 0;
1045 return 2;
1046 }
1047
d665cc31
DW
1048 list = find_driver_devices("pci", "ahci");
1049 for (hba = list; hba; hba = hba->next)
1050 if (devpath_to_vendor(hba->path) == 0x8086)
1051 break;
1052
1053 if (!hba) {
1054 if (verbose)
1055 fprintf(stderr, Name ": unable to find active ahci controller\n");
1056 free_sys_dev(&list);
1057 return 2;
1058 } else if (verbose)
1059 fprintf(stderr, Name ": found Intel SATA AHCI Controller\n");
1060 hba_path = hba->path;
1061 hba->path = NULL;
1062 free_sys_dev(&list);
1063
1064 orom = find_imsm_orom();
1065 if (!orom) {
1066 if (verbose)
1067 fprintf(stderr, Name ": imsm option-rom not found\n");
1068 return 2;
1069 }
1070
1071 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1072 printf(" Version : %d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
1073 orom->hotfix_ver, orom->build);
1074 printf(" RAID Levels :%s%s%s%s%s\n",
1075 imsm_orom_has_raid0(orom) ? " raid0" : "",
1076 imsm_orom_has_raid1(orom) ? " raid1" : "",
1077 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
1078 imsm_orom_has_raid10(orom) ? " raid10" : "",
1079 imsm_orom_has_raid5(orom) ? " raid5" : "");
8be094f0
DW
1080 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1081 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
1082 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
1083 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
1084 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
1085 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
1086 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
1087 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
1088 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
1089 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
1090 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
1091 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
1092 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
1093 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
1094 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
1095 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
1096 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
d665cc31
DW
1097 printf(" Max Disks : %d\n", orom->tds);
1098 printf(" Max Volumes : %d\n", orom->vpa);
1099 printf(" I/O Controller : %s\n", hba_path);
1100
1101 /* find the smallest scsi host number to determine a port number base */
1102 dir = opendir(hba_path);
1103 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
1104 int host;
1105
1106 if (sscanf(ent->d_name, "host%d", &host) != 1)
1107 continue;
1108 if (port_count == 0)
1109 host_base = host;
1110 else if (host < host_base)
1111 host_base = host;
1112
1113 if (host + 1 > port_count + host_base)
1114 port_count = host + 1 - host_base;
1115
1116 }
1117 if (dir)
1118 closedir(dir);
1119
1120 if (!port_count || imsm_enumerate_ports(hba_path, port_count,
1121 host_base, verbose) != 0) {
1122 if (verbose)
1123 fprintf(stderr, Name ": failed to enumerate ports\n");
1124 return 2;
1125 }
1126
1127 return 0;
1128}
cdddbdbc
DW
1129#endif
1130
1131static int match_home_imsm(struct supertype *st, char *homehost)
1132{
5115ca67
DW
1133 /* the imsm metadata format does not specify any host
1134 * identification information. We return -1 since we can never
1135 * confirm nor deny whether a given array is "meant" for this
148acb7b 1136 * host. We rely on compare_super and the 'family_num' fields to
5115ca67
DW
1137 * exclude member disks that do not belong, and we rely on
1138 * mdadm.conf to specify the arrays that should be assembled.
1139 * Auto-assembly may still pick up "foreign" arrays.
1140 */
cdddbdbc 1141
9362c1c8 1142 return -1;
cdddbdbc
DW
1143}
1144
1145static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
1146{
51006d85
N
1147 /* The uuid returned here is used for:
1148 * uuid to put into bitmap file (Create, Grow)
1149 * uuid for backup header when saving critical section (Grow)
1150 * comparing uuids when re-adding a device into an array
1151 * In these cases the uuid required is that of the data-array,
1152 * not the device-set.
1153 * uuid to recognise same set when adding a missing device back
1154 * to an array. This is a uuid for the device-set.
1155 *
1156 * For each of these we can make do with a truncated
1157 * or hashed uuid rather than the original, as long as
1158 * everyone agrees.
1159 * In each case the uuid required is that of the data-array,
1160 * not the device-set.
43dad3d6 1161 */
51006d85
N
1162 /* imsm does not track uuid's so we synthesis one using sha1 on
1163 * - The signature (Which is constant for all imsm array, but no matter)
148acb7b 1164 * - the orig_family_num of the container
51006d85
N
1165 * - the index number of the volume
1166 * - the 'serial' number of the volume.
1167 * Hopefully these are all constant.
1168 */
1169 struct intel_super *super = st->sb;
43dad3d6 1170
51006d85
N
1171 char buf[20];
1172 struct sha1_ctx ctx;
1173 struct imsm_dev *dev = NULL;
148acb7b 1174 __u32 family_num;
51006d85 1175
148acb7b
DW
1176 /* some mdadm versions failed to set ->orig_family_num, in which
1177 * case fall back to ->family_num. orig_family_num will be
1178 * fixed up with the first metadata update.
1179 */
1180 family_num = super->anchor->orig_family_num;
1181 if (family_num == 0)
1182 family_num = super->anchor->family_num;
51006d85 1183 sha1_init_ctx(&ctx);
92bd8f8d 1184 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
148acb7b 1185 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
51006d85
N
1186 if (super->current_vol >= 0)
1187 dev = get_imsm_dev(super, super->current_vol);
1188 if (dev) {
1189 __u32 vol = super->current_vol;
1190 sha1_process_bytes(&vol, sizeof(vol), &ctx);
1191 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
1192 }
1193 sha1_finish_ctx(&ctx, buf);
1194 memcpy(uuid, buf, 4*4);
cdddbdbc
DW
1195}
1196
0d481d37 1197#if 0
4f5bc454
DW
1198static void
1199get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
cdddbdbc 1200{
cdddbdbc
DW
1201 __u8 *v = get_imsm_version(mpb);
1202 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
1203 char major[] = { 0, 0, 0 };
1204 char minor[] = { 0 ,0, 0 };
1205 char patch[] = { 0, 0, 0 };
1206 char *ver_parse[] = { major, minor, patch };
1207 int i, j;
1208
1209 i = j = 0;
1210 while (*v != '\0' && v < end) {
1211 if (*v != '.' && j < 2)
1212 ver_parse[i][j++] = *v;
1213 else {
1214 i++;
1215 j = 0;
1216 }
1217 v++;
1218 }
1219
4f5bc454
DW
1220 *m = strtol(minor, NULL, 0);
1221 *p = strtol(patch, NULL, 0);
1222}
0d481d37 1223#endif
4f5bc454 1224
1e5c6983
DW
1225static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
1226{
1227 /* migr_strip_size when repairing or initializing parity */
1228 struct imsm_map *map = get_imsm_map(dev, 0);
1229 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1230
1231 switch (get_imsm_raid_level(map)) {
1232 case 5:
1233 case 10:
1234 return chunk;
1235 default:
1236 return 128*1024 >> 9;
1237 }
1238}
1239
1240static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
1241{
1242 /* migr_strip_size when rebuilding a degraded disk, no idea why
1243 * this is different than migr_strip_size_resync(), but it's good
1244 * to be compatible
1245 */
1246 struct imsm_map *map = get_imsm_map(dev, 1);
1247 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1248
1249 switch (get_imsm_raid_level(map)) {
1250 case 1:
1251 case 10:
1252 if (map->num_members % map->num_domains == 0)
1253 return 128*1024 >> 9;
1254 else
1255 return chunk;
1256 case 5:
1257 return max((__u32) 64*1024 >> 9, chunk);
1258 default:
1259 return 128*1024 >> 9;
1260 }
1261}
1262
1263static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
1264{
1265 struct imsm_map *lo = get_imsm_map(dev, 0);
1266 struct imsm_map *hi = get_imsm_map(dev, 1);
1267 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
1268 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
1269
1270 return max((__u32) 1, hi_chunk / lo_chunk);
1271}
1272
1273static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
1274{
1275 struct imsm_map *lo = get_imsm_map(dev, 0);
1276 int level = get_imsm_raid_level(lo);
1277
1278 if (level == 1 || level == 10) {
1279 struct imsm_map *hi = get_imsm_map(dev, 1);
1280
1281 return hi->num_domains;
1282 } else
1283 return num_stripes_per_unit_resync(dev);
1284}
1285
1286static __u8 imsm_num_data_members(struct imsm_dev *dev)
1287{
1288 /* named 'imsm_' because raid0, raid1 and raid10
1289 * counter-intuitively have the same number of data disks
1290 */
1291 struct imsm_map *map = get_imsm_map(dev, 0);
1292
1293 switch (get_imsm_raid_level(map)) {
1294 case 0:
1295 case 1:
1296 case 10:
1297 return map->num_members;
1298 case 5:
1299 return map->num_members - 1;
1300 default:
1301 dprintf("%s: unsupported raid level\n", __func__);
1302 return 0;
1303 }
1304}
1305
1306static __u32 parity_segment_depth(struct imsm_dev *dev)
1307{
1308 struct imsm_map *map = get_imsm_map(dev, 0);
1309 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1310
1311 switch(get_imsm_raid_level(map)) {
1312 case 1:
1313 case 10:
1314 return chunk * map->num_domains;
1315 case 5:
1316 return chunk * map->num_members;
1317 default:
1318 return chunk;
1319 }
1320}
1321
1322static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
1323{
1324 struct imsm_map *map = get_imsm_map(dev, 1);
1325 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1326 __u32 strip = block / chunk;
1327
1328 switch (get_imsm_raid_level(map)) {
1329 case 1:
1330 case 10: {
1331 __u32 vol_strip = (strip * map->num_domains) + 1;
1332 __u32 vol_stripe = vol_strip / map->num_members;
1333
1334 return vol_stripe * chunk + block % chunk;
1335 } case 5: {
1336 __u32 stripe = strip / (map->num_members - 1);
1337
1338 return stripe * chunk + block % chunk;
1339 }
1340 default:
1341 return 0;
1342 }
1343}
1344
1345static __u64 blocks_per_migr_unit(struct imsm_dev *dev)
1346{
1347 /* calculate the conversion factor between per member 'blocks'
1348 * (md/{resync,rebuild}_start) and imsm migration units, return
1349 * 0 for the 'not migrating' and 'unsupported migration' cases
1350 */
1351 if (!dev->vol.migr_state)
1352 return 0;
1353
1354 switch (migr_type(dev)) {
1355 case MIGR_VERIFY:
1356 case MIGR_REPAIR:
1357 case MIGR_INIT: {
1358 struct imsm_map *map = get_imsm_map(dev, 0);
1359 __u32 stripes_per_unit;
1360 __u32 blocks_per_unit;
1361 __u32 parity_depth;
1362 __u32 migr_chunk;
1363 __u32 block_map;
1364 __u32 block_rel;
1365 __u32 segment;
1366 __u32 stripe;
1367 __u8 disks;
1368
1369 /* yes, this is really the translation of migr_units to
1370 * per-member blocks in the 'resync' case
1371 */
1372 stripes_per_unit = num_stripes_per_unit_resync(dev);
1373 migr_chunk = migr_strip_blocks_resync(dev);
1374 disks = imsm_num_data_members(dev);
1375 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
1376 stripe = __le32_to_cpu(map->blocks_per_strip) * disks;
1377 segment = blocks_per_unit / stripe;
1378 block_rel = blocks_per_unit - segment * stripe;
1379 parity_depth = parity_segment_depth(dev);
1380 block_map = map_migr_block(dev, block_rel);
1381 return block_map + parity_depth * segment;
1382 }
1383 case MIGR_REBUILD: {
1384 __u32 stripes_per_unit;
1385 __u32 migr_chunk;
1386
1387 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
1388 migr_chunk = migr_strip_blocks_rebuild(dev);
1389 return migr_chunk * stripes_per_unit;
1390 }
1391 case MIGR_GEN_MIGR:
1392 case MIGR_STATE_CHANGE:
1393 default:
1394 return 0;
1395 }
1396}
1397
c2c087e6
DW
1398static int imsm_level_to_layout(int level)
1399{
1400 switch (level) {
1401 case 0:
1402 case 1:
1403 return 0;
1404 case 5:
1405 case 6:
a380c027 1406 return ALGORITHM_LEFT_ASYMMETRIC;
c2c087e6 1407 case 10:
c92a2527 1408 return 0x102;
c2c087e6 1409 }
a18a888e 1410 return UnSet;
c2c087e6
DW
1411}
1412
bf5a934a
DW
1413static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info)
1414{
1415 struct intel_super *super = st->sb;
949c47a0 1416 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
a965f303 1417 struct imsm_map *map = get_imsm_map(dev, 0);
efb30e7f 1418 struct dl *dl;
bf5a934a 1419
efb30e7f
DW
1420 for (dl = super->disks; dl; dl = dl->next)
1421 if (dl->raiddisk == info->disk.raid_disk)
1422 break;
bf5a934a
DW
1423 info->container_member = super->current_vol;
1424 info->array.raid_disks = map->num_members;
1425 info->array.level = get_imsm_raid_level(map);
1426 info->array.layout = imsm_level_to_layout(info->array.level);
1427 info->array.md_minor = -1;
1428 info->array.ctime = 0;
1429 info->array.utime = 0;
301406c9
DW
1430 info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9;
1431 info->array.state = !dev->vol.dirty;
da9b4a62
DW
1432 info->custom_array_size = __le32_to_cpu(dev->size_high);
1433 info->custom_array_size <<= 32;
1434 info->custom_array_size |= __le32_to_cpu(dev->size_low);
301406c9
DW
1435
1436 info->disk.major = 0;
1437 info->disk.minor = 0;
efb30e7f
DW
1438 if (dl) {
1439 info->disk.major = dl->major;
1440 info->disk.minor = dl->minor;
1441 }
bf5a934a
DW
1442
1443 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
1444 info->component_size = __le32_to_cpu(map->blocks_per_member);
301406c9 1445 memset(info->uuid, 0, sizeof(info->uuid));
bf5a934a 1446
1e5c6983 1447 if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty) {
301406c9 1448 info->resync_start = 0;
1e5c6983
DW
1449 } else if (dev->vol.migr_state) {
1450 switch (migr_type(dev)) {
1451 case MIGR_REPAIR:
1452 case MIGR_INIT: {
1453 __u64 blocks_per_unit = blocks_per_migr_unit(dev);
1454 __u64 units = __le32_to_cpu(dev->vol.curr_migr_unit);
1455
1456 info->resync_start = blocks_per_unit * units;
1457 break;
1458 }
1459 case MIGR_VERIFY:
1460 /* we could emulate the checkpointing of
1461 * 'sync_action=check' migrations, but for now
1462 * we just immediately complete them
1463 */
1464 case MIGR_REBUILD:
1465 /* this is handled by container_content_imsm() */
1466 case MIGR_GEN_MIGR:
1467 case MIGR_STATE_CHANGE:
1468 /* FIXME handle other migrations */
1469 default:
1470 /* we are not dirty, so... */
1471 info->resync_start = MaxSector;
1472 }
1473 } else
b7528a20 1474 info->resync_start = MaxSector;
301406c9
DW
1475
1476 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1477 info->name[MAX_RAID_SERIAL_LEN] = 0;
bf5a934a 1478
f35f2525
N
1479 info->array.major_version = -1;
1480 info->array.minor_version = -2;
bf5a934a
DW
1481 sprintf(info->text_version, "/%s/%d",
1482 devnum2devname(st->container_dev),
1483 info->container_member);
a67dd8cc 1484 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
51006d85 1485 uuid_from_super_imsm(st, info->uuid);
bf5a934a
DW
1486}
1487
7a70e8aa
DW
1488/* check the config file to see if we can return a real uuid for this spare */
1489static void fixup_container_spare_uuid(struct mdinfo *inf)
1490{
1491 struct mddev_ident_s *array_list;
1492
1493 if (inf->array.level != LEVEL_CONTAINER ||
1494 memcmp(inf->uuid, uuid_match_any, sizeof(int[4])) != 0)
1495 return;
1496
1497 array_list = conf_get_ident(NULL);
1498
1499 for (; array_list; array_list = array_list->next) {
1500 if (array_list->uuid_set) {
1501 struct supertype *_sst; /* spare supertype */
1502 struct supertype *_cst; /* container supertype */
1503
1504 _cst = array_list->st;
7e8545e9
DW
1505 if (_cst)
1506 _sst = _cst->ss->match_metadata_desc(inf->text_version);
1507 else
1508 _sst = NULL;
1509
7a70e8aa
DW
1510 if (_sst) {
1511 memcpy(inf->uuid, array_list->uuid, sizeof(int[4]));
1512 free(_sst);
1513 break;
1514 }
1515 }
1516 }
1517}
bf5a934a 1518
4f5bc454
DW
1519static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
1520{
1521 struct intel_super *super = st->sb;
4f5bc454 1522 struct imsm_disk *disk;
4f5bc454 1523
bf5a934a
DW
1524 if (super->current_vol >= 0) {
1525 getinfo_super_imsm_volume(st, info);
1526 return;
1527 }
d23fe947
DW
1528
1529 /* Set raid_disks to zero so that Assemble will always pull in valid
1530 * spares
1531 */
1532 info->array.raid_disks = 0;
cdddbdbc
DW
1533 info->array.level = LEVEL_CONTAINER;
1534 info->array.layout = 0;
1535 info->array.md_minor = -1;
c2c087e6 1536 info->array.ctime = 0; /* N/A for imsm */
cdddbdbc
DW
1537 info->array.utime = 0;
1538 info->array.chunk_size = 0;
1539
1540 info->disk.major = 0;
1541 info->disk.minor = 0;
cdddbdbc 1542 info->disk.raid_disk = -1;
c2c087e6 1543 info->reshape_active = 0;
f35f2525
N
1544 info->array.major_version = -1;
1545 info->array.minor_version = -2;
c2c087e6 1546 strcpy(info->text_version, "imsm");
a67dd8cc 1547 info->safe_mode_delay = 0;
c2c087e6
DW
1548 info->disk.number = -1;
1549 info->disk.state = 0;
c5afc314 1550 info->name[0] = 0;
c2c087e6 1551
4a04ec6c 1552 if (super->disks) {
14e8215b
DW
1553 __u32 reserved = imsm_reserved_sectors(super, super->disks);
1554
b9f594fe 1555 disk = &super->disks->disk;
14e8215b
DW
1556 info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved;
1557 info->component_size = reserved;
25ed7e59 1558 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
df474657
DW
1559 /* we don't change info->disk.raid_disk here because
1560 * this state will be finalized in mdmon after we have
1561 * found the 'most fresh' version of the metadata
1562 */
25ed7e59
DW
1563 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
1564 info->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
cdddbdbc 1565 }
a575e2a7
DW
1566
1567 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1568 * ->compare_super may have updated the 'num_raid_devs' field for spares
1569 */
1570 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
36ba7d48 1571 uuid_from_super_imsm(st, info->uuid);
7a70e8aa 1572 else {
032e9e29 1573 memcpy(info->uuid, uuid_match_any, sizeof(int[4]));
7a70e8aa
DW
1574 fixup_container_spare_uuid(info);
1575 }
cdddbdbc
DW
1576}
1577
cdddbdbc
DW
1578static int update_super_imsm(struct supertype *st, struct mdinfo *info,
1579 char *update, char *devname, int verbose,
1580 int uuid_set, char *homehost)
1581{
f352c545
DW
1582 /* For 'assemble' and 'force' we need to return non-zero if any
1583 * change was made. For others, the return value is ignored.
1584 * Update options are:
1585 * force-one : This device looks a bit old but needs to be included,
1586 * update age info appropriately.
1587 * assemble: clear any 'faulty' flag to allow this device to
1588 * be assembled.
1589 * force-array: Array is degraded but being forced, mark it clean
1590 * if that will be needed to assemble it.
1591 *
1592 * newdev: not used ????
1593 * grow: Array has gained a new device - this is currently for
1594 * linear only
1595 * resync: mark as dirty so a resync will happen.
1596 * name: update the name - preserving the homehost
6e46bf34 1597 * uuid: Change the uuid of the array to match watch is given
f352c545
DW
1598 *
1599 * Following are not relevant for this imsm:
1600 * sparc2.2 : update from old dodgey metadata
1601 * super-minor: change the preferred_minor number
1602 * summaries: update redundant counters.
f352c545
DW
1603 * homehost: update the recorded homehost
1604 * _reshape_progress: record new reshape_progress position.
1605 */
6e46bf34
DW
1606 int rv = 1;
1607 struct intel_super *super = st->sb;
1608 struct imsm_super *mpb;
f352c545 1609
6e46bf34
DW
1610 /* we can only update container info */
1611 if (!super || super->current_vol >= 0 || !super->anchor)
1612 return 1;
1613
1614 mpb = super->anchor;
1615
1616 if (strcmp(update, "uuid") == 0 && uuid_set && !info->update_private)
1617 fprintf(stderr,
1618 Name ": '--uuid' not supported for imsm metadata\n");
1619 else if (strcmp(update, "uuid") == 0 && uuid_set && info->update_private) {
1620 mpb->orig_family_num = *((__u32 *) info->update_private);
1621 rv = 0;
1622 } else if (strcmp(update, "uuid") == 0) {
1623 __u32 *new_family = malloc(sizeof(*new_family));
1624
1625 /* update orig_family_number with the incoming random
1626 * data, report the new effective uuid, and store the
1627 * new orig_family_num for future updates.
1628 */
1629 if (new_family) {
1630 memcpy(&mpb->orig_family_num, info->uuid, sizeof(__u32));
1631 uuid_from_super_imsm(st, info->uuid);
1632 *new_family = mpb->orig_family_num;
1633 info->update_private = new_family;
1634 rv = 0;
1635 }
1636 } else if (strcmp(update, "assemble") == 0)
1637 rv = 0;
1638 else
1639 fprintf(stderr,
1640 Name ": '--update=%s' not supported for imsm metadata\n",
1641 update);
f352c545 1642
6e46bf34
DW
1643 /* successful update? recompute checksum */
1644 if (rv == 0)
1645 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
f352c545
DW
1646
1647 return rv;
cdddbdbc
DW
1648}
1649
c2c087e6 1650static size_t disks_to_mpb_size(int disks)
cdddbdbc 1651{
c2c087e6 1652 size_t size;
cdddbdbc 1653
c2c087e6
DW
1654 size = sizeof(struct imsm_super);
1655 size += (disks - 1) * sizeof(struct imsm_disk);
1656 size += 2 * sizeof(struct imsm_dev);
1657 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1658 size += (4 - 2) * sizeof(struct imsm_map);
1659 /* 4 possible disk_ord_tbl's */
1660 size += 4 * (disks - 1) * sizeof(__u32);
1661
1662 return size;
1663}
1664
1665static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
1666{
1667 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
1668 return 0;
1669
1670 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
cdddbdbc
DW
1671}
1672
ba2de7ba
DW
1673static void free_devlist(struct intel_super *super)
1674{
1675 struct intel_dev *dv;
1676
1677 while (super->devlist) {
1678 dv = super->devlist->next;
1679 free(super->devlist->dev);
1680 free(super->devlist);
1681 super->devlist = dv;
1682 }
1683}
1684
1685static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
1686{
1687 memcpy(dest, src, sizeof_imsm_dev(src, 0));
1688}
1689
cdddbdbc
DW
1690static int compare_super_imsm(struct supertype *st, struct supertype *tst)
1691{
1692 /*
1693 * return:
1694 * 0 same, or first was empty, and second was copied
1695 * 1 second had wrong number
1696 * 2 wrong uuid
1697 * 3 wrong other info
1698 */
1699 struct intel_super *first = st->sb;
1700 struct intel_super *sec = tst->sb;
1701
1702 if (!first) {
1703 st->sb = tst->sb;
1704 tst->sb = NULL;
1705 return 0;
1706 }
1707
d23fe947
DW
1708 /* if an anchor does not have num_raid_devs set then it is a free
1709 * floating spare
1710 */
1711 if (first->anchor->num_raid_devs > 0 &&
1712 sec->anchor->num_raid_devs > 0) {
a2b97981
DW
1713 /* Determine if these disks might ever have been
1714 * related. Further disambiguation can only take place
1715 * in load_super_imsm_all
1716 */
1717 __u32 first_family = first->anchor->orig_family_num;
1718 __u32 sec_family = sec->anchor->orig_family_num;
1719
f796af5d
DW
1720 if (memcmp(first->anchor->sig, sec->anchor->sig,
1721 MAX_SIGNATURE_LENGTH) != 0)
1722 return 3;
1723
a2b97981
DW
1724 if (first_family == 0)
1725 first_family = first->anchor->family_num;
1726 if (sec_family == 0)
1727 sec_family = sec->anchor->family_num;
1728
1729 if (first_family != sec_family)
d23fe947 1730 return 3;
f796af5d 1731
d23fe947 1732 }
cdddbdbc 1733
f796af5d 1734
3e372e5a
DW
1735 /* if 'first' is a spare promote it to a populated mpb with sec's
1736 * family number
1737 */
1738 if (first->anchor->num_raid_devs == 0 &&
1739 sec->anchor->num_raid_devs > 0) {
78d30f94 1740 int i;
ba2de7ba
DW
1741 struct intel_dev *dv;
1742 struct imsm_dev *dev;
78d30f94
DW
1743
1744 /* we need to copy raid device info from sec if an allocation
1745 * fails here we don't associate the spare
1746 */
1747 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
ba2de7ba
DW
1748 dv = malloc(sizeof(*dv));
1749 if (!dv)
1750 break;
1751 dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
1752 if (!dev) {
1753 free(dv);
1754 break;
78d30f94 1755 }
ba2de7ba
DW
1756 dv->dev = dev;
1757 dv->index = i;
1758 dv->next = first->devlist;
1759 first->devlist = dv;
78d30f94 1760 }
709743c5 1761 if (i < sec->anchor->num_raid_devs) {
ba2de7ba
DW
1762 /* allocation failure */
1763 free_devlist(first);
1764 fprintf(stderr, "imsm: failed to associate spare\n");
1765 return 3;
78d30f94 1766 }
3e372e5a 1767 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
148acb7b 1768 first->anchor->orig_family_num = sec->anchor->orig_family_num;
3e372e5a 1769 first->anchor->family_num = sec->anchor->family_num;
ac6449be 1770 memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH);
709743c5
DW
1771 for (i = 0; i < sec->anchor->num_raid_devs; i++)
1772 imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
3e372e5a
DW
1773 }
1774
cdddbdbc
DW
1775 return 0;
1776}
1777
0030e8d6
DW
1778static void fd2devname(int fd, char *name)
1779{
1780 struct stat st;
1781 char path[256];
1782 char dname[100];
1783 char *nm;
1784 int rv;
1785
1786 name[0] = '\0';
1787 if (fstat(fd, &st) != 0)
1788 return;
1789 sprintf(path, "/sys/dev/block/%d:%d",
1790 major(st.st_rdev), minor(st.st_rdev));
1791
1792 rv = readlink(path, dname, sizeof(dname));
1793 if (rv <= 0)
1794 return;
1795
1796 dname[rv] = '\0';
1797 nm = strrchr(dname, '/');
1798 nm++;
1799 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
1800}
1801
cdddbdbc
DW
1802extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
1803
1804static int imsm_read_serial(int fd, char *devname,
1805 __u8 serial[MAX_RAID_SERIAL_LEN])
1806{
1807 unsigned char scsi_serial[255];
cdddbdbc
DW
1808 int rv;
1809 int rsp_len;
1f24f035 1810 int len;
316e2bf4
DW
1811 char *dest;
1812 char *src;
1813 char *rsp_buf;
1814 int i;
cdddbdbc
DW
1815
1816 memset(scsi_serial, 0, sizeof(scsi_serial));
cdddbdbc 1817
f9ba0ff1
DW
1818 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
1819
40ebbb9c 1820 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
f9ba0ff1
DW
1821 memset(serial, 0, MAX_RAID_SERIAL_LEN);
1822 fd2devname(fd, (char *) serial);
0030e8d6
DW
1823 return 0;
1824 }
1825
cdddbdbc
DW
1826 if (rv != 0) {
1827 if (devname)
1828 fprintf(stderr,
1829 Name ": Failed to retrieve serial for %s\n",
1830 devname);
1831 return rv;
1832 }
1833
1834 rsp_len = scsi_serial[3];
03cd4cc8
DW
1835 if (!rsp_len) {
1836 if (devname)
1837 fprintf(stderr,
1838 Name ": Failed to retrieve serial for %s\n",
1839 devname);
1840 return 2;
1841 }
1f24f035 1842 rsp_buf = (char *) &scsi_serial[4];
5c3db629 1843
316e2bf4
DW
1844 /* trim all whitespace and non-printable characters and convert
1845 * ':' to ';'
1846 */
1847 for (i = 0, dest = rsp_buf; i < rsp_len; i++) {
1848 src = &rsp_buf[i];
1849 if (*src > 0x20) {
1850 /* ':' is reserved for use in placeholder serial
1851 * numbers for missing disks
1852 */
1853 if (*src == ':')
1854 *dest++ = ';';
1855 else
1856 *dest++ = *src;
1857 }
1858 }
1859 len = dest - rsp_buf;
1860 dest = rsp_buf;
1861
1862 /* truncate leading characters */
1863 if (len > MAX_RAID_SERIAL_LEN) {
1864 dest += len - MAX_RAID_SERIAL_LEN;
1f24f035 1865 len = MAX_RAID_SERIAL_LEN;
316e2bf4 1866 }
5c3db629 1867
5c3db629 1868 memset(serial, 0, MAX_RAID_SERIAL_LEN);
316e2bf4 1869 memcpy(serial, dest, len);
cdddbdbc
DW
1870
1871 return 0;
1872}
1873
1f24f035
DW
1874static int serialcmp(__u8 *s1, __u8 *s2)
1875{
1876 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
1877}
1878
1879static void serialcpy(__u8 *dest, __u8 *src)
1880{
1881 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
1882}
1883
1799c9e8 1884#ifndef MDASSEMBLE
54c2c1ea
DW
1885static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
1886{
1887 struct dl *dl;
1888
1889 for (dl = super->disks; dl; dl = dl->next)
1890 if (serialcmp(dl->serial, serial) == 0)
1891 break;
1892
1893 return dl;
1894}
1799c9e8 1895#endif
54c2c1ea 1896
a2b97981
DW
1897static struct imsm_disk *
1898__serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
1899{
1900 int i;
1901
1902 for (i = 0; i < mpb->num_disks; i++) {
1903 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
1904
1905 if (serialcmp(disk->serial, serial) == 0) {
1906 if (idx)
1907 *idx = i;
1908 return disk;
1909 }
1910 }
1911
1912 return NULL;
1913}
1914
cdddbdbc
DW
1915static int
1916load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
1917{
a2b97981 1918 struct imsm_disk *disk;
cdddbdbc
DW
1919 struct dl *dl;
1920 struct stat stb;
cdddbdbc 1921 int rv;
a2b97981 1922 char name[40];
d23fe947
DW
1923 __u8 serial[MAX_RAID_SERIAL_LEN];
1924
1925 rv = imsm_read_serial(fd, devname, serial);
1926
1927 if (rv != 0)
1928 return 2;
1929
a2b97981 1930 dl = calloc(1, sizeof(*dl));
b9f594fe 1931 if (!dl) {
cdddbdbc
DW
1932 if (devname)
1933 fprintf(stderr,
1934 Name ": failed to allocate disk buffer for %s\n",
1935 devname);
1936 return 2;
1937 }
cdddbdbc 1938
a2b97981
DW
1939 fstat(fd, &stb);
1940 dl->major = major(stb.st_rdev);
1941 dl->minor = minor(stb.st_rdev);
1942 dl->next = super->disks;
1943 dl->fd = keep_fd ? fd : -1;
1944 assert(super->disks == NULL);
1945 super->disks = dl;
1946 serialcpy(dl->serial, serial);
1947 dl->index = -2;
1948 dl->e = NULL;
1949 fd2devname(fd, name);
1950 if (devname)
1951 dl->devname = strdup(devname);
1952 else
1953 dl->devname = strdup(name);
cdddbdbc 1954
d23fe947 1955 /* look up this disk's index in the current anchor */
a2b97981
DW
1956 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
1957 if (disk) {
1958 dl->disk = *disk;
1959 /* only set index on disks that are a member of a
1960 * populated contianer, i.e. one with raid_devs
1961 */
1962 if (is_failed(&dl->disk))
3f6efecc 1963 dl->index = -2;
a2b97981
DW
1964 else if (is_spare(&dl->disk))
1965 dl->index = -1;
3f6efecc
DW
1966 }
1967
949c47a0
DW
1968 return 0;
1969}
1970
0e600426 1971#ifndef MDASSEMBLE
0c046afd
DW
1972/* When migrating map0 contains the 'destination' state while map1
1973 * contains the current state. When not migrating map0 contains the
1974 * current state. This routine assumes that map[0].map_state is set to
1975 * the current array state before being called.
1976 *
1977 * Migration is indicated by one of the following states
1978 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
e3bba0e0 1979 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
0c046afd 1980 * map1state=unitialized)
1484e727 1981 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
0c046afd 1982 * map1state=normal)
e3bba0e0 1983 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
0c046afd
DW
1984 * map1state=degraded)
1985 */
0556e1a2 1986static void migrate(struct imsm_dev *dev, __u8 to_state, int migr_type)
3393c6af 1987{
0c046afd 1988 struct imsm_map *dest;
3393c6af
DW
1989 struct imsm_map *src = get_imsm_map(dev, 0);
1990
0c046afd 1991 dev->vol.migr_state = 1;
1484e727 1992 set_migr_type(dev, migr_type);
f8f603f1 1993 dev->vol.curr_migr_unit = 0;
0c046afd
DW
1994 dest = get_imsm_map(dev, 1);
1995
0556e1a2 1996 /* duplicate and then set the target end state in map[0] */
3393c6af 1997 memcpy(dest, src, sizeof_imsm_map(src));
0556e1a2
DW
1998 if (migr_type == MIGR_REBUILD) {
1999 __u32 ord;
2000 int i;
2001
2002 for (i = 0; i < src->num_members; i++) {
2003 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
2004 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
2005 }
2006 }
2007
0c046afd 2008 src->map_state = to_state;
949c47a0 2009}
f8f603f1
DW
2010
2011static void end_migration(struct imsm_dev *dev, __u8 map_state)
2012{
2013 struct imsm_map *map = get_imsm_map(dev, 0);
0556e1a2
DW
2014 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
2015 int i;
2016
2017 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2018 * completed in the last migration.
2019 *
2020 * FIXME add support for online capacity expansion and
2021 * raid-level-migration
2022 */
2023 for (i = 0; i < prev->num_members; i++)
2024 map->disk_ord_tbl[i] |= prev->disk_ord_tbl[i];
f8f603f1
DW
2025
2026 dev->vol.migr_state = 0;
2027 dev->vol.curr_migr_unit = 0;
2028 map->map_state = map_state;
2029}
0e600426 2030#endif
949c47a0
DW
2031
2032static int parse_raid_devices(struct intel_super *super)
2033{
2034 int i;
2035 struct imsm_dev *dev_new;
4d7b1503
DW
2036 size_t len, len_migr;
2037 size_t space_needed = 0;
2038 struct imsm_super *mpb = super->anchor;
949c47a0
DW
2039
2040 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2041 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
ba2de7ba 2042 struct intel_dev *dv;
949c47a0 2043
4d7b1503
DW
2044 len = sizeof_imsm_dev(dev_iter, 0);
2045 len_migr = sizeof_imsm_dev(dev_iter, 1);
2046 if (len_migr > len)
2047 space_needed += len_migr - len;
2048
ba2de7ba
DW
2049 dv = malloc(sizeof(*dv));
2050 if (!dv)
2051 return 1;
4d7b1503 2052 dev_new = malloc(len_migr);
ba2de7ba
DW
2053 if (!dev_new) {
2054 free(dv);
949c47a0 2055 return 1;
ba2de7ba 2056 }
949c47a0 2057 imsm_copy_dev(dev_new, dev_iter);
ba2de7ba
DW
2058 dv->dev = dev_new;
2059 dv->index = i;
2060 dv->next = super->devlist;
2061 super->devlist = dv;
949c47a0 2062 }
cdddbdbc 2063
4d7b1503
DW
2064 /* ensure that super->buf is large enough when all raid devices
2065 * are migrating
2066 */
2067 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
2068 void *buf;
2069
2070 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512);
2071 if (posix_memalign(&buf, 512, len) != 0)
2072 return 1;
2073
1f45a8ad
DW
2074 memcpy(buf, super->buf, super->len);
2075 memset(buf + super->len, 0, len - super->len);
4d7b1503
DW
2076 free(super->buf);
2077 super->buf = buf;
2078 super->len = len;
2079 }
2080
cdddbdbc
DW
2081 return 0;
2082}
2083
604b746f
JD
2084/* retrieve a pointer to the bbm log which starts after all raid devices */
2085struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
2086{
2087 void *ptr = NULL;
2088
2089 if (__le32_to_cpu(mpb->bbm_log_size)) {
2090 ptr = mpb;
2091 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
2092 }
2093
2094 return ptr;
2095}
2096
d23fe947 2097static void __free_imsm(struct intel_super *super, int free_disks);
9ca2c81c 2098
cdddbdbc
DW
2099/* load_imsm_mpb - read matrix metadata
2100 * allocates super->mpb to be freed by free_super
2101 */
2102static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
2103{
2104 unsigned long long dsize;
cdddbdbc
DW
2105 unsigned long long sectors;
2106 struct stat;
6416d527 2107 struct imsm_super *anchor;
cdddbdbc
DW
2108 __u32 check_sum;
2109
cdddbdbc
DW
2110 get_dev_size(fd, NULL, &dsize);
2111
2112 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
2113 if (devname)
2114 fprintf(stderr,
2115 Name ": Cannot seek to anchor block on %s: %s\n",
2116 devname, strerror(errno));
2117 return 1;
2118 }
2119
949c47a0 2120 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
ad97895e
DW
2121 if (devname)
2122 fprintf(stderr,
2123 Name ": Failed to allocate imsm anchor buffer"
2124 " on %s\n", devname);
2125 return 1;
2126 }
949c47a0 2127 if (read(fd, anchor, 512) != 512) {
cdddbdbc
DW
2128 if (devname)
2129 fprintf(stderr,
2130 Name ": Cannot read anchor block on %s: %s\n",
2131 devname, strerror(errno));
6416d527 2132 free(anchor);
cdddbdbc
DW
2133 return 1;
2134 }
2135
6416d527 2136 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
cdddbdbc
DW
2137 if (devname)
2138 fprintf(stderr,
2139 Name ": no IMSM anchor on %s\n", devname);
6416d527 2140 free(anchor);
cdddbdbc
DW
2141 return 2;
2142 }
2143
d23fe947 2144 __free_imsm(super, 0);
949c47a0
DW
2145 super->len = ROUND_UP(anchor->mpb_size, 512);
2146 if (posix_memalign(&super->buf, 512, super->len) != 0) {
cdddbdbc
DW
2147 if (devname)
2148 fprintf(stderr,
2149 Name ": unable to allocate %zu byte mpb buffer\n",
949c47a0 2150 super->len);
6416d527 2151 free(anchor);
cdddbdbc
DW
2152 return 2;
2153 }
949c47a0 2154 memcpy(super->buf, anchor, 512);
cdddbdbc 2155
6416d527
NB
2156 sectors = mpb_sectors(anchor) - 1;
2157 free(anchor);
949c47a0 2158 if (!sectors) {
ecf45690
DW
2159 check_sum = __gen_imsm_checksum(super->anchor);
2160 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
2161 if (devname)
2162 fprintf(stderr,
2163 Name ": IMSM checksum %x != %x on %s\n",
2164 check_sum,
2165 __le32_to_cpu(super->anchor->check_sum),
2166 devname);
2167 return 2;
2168 }
2169
a2b97981 2170 return 0;
949c47a0 2171 }
cdddbdbc
DW
2172
2173 /* read the extended mpb */
2174 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
2175 if (devname)
2176 fprintf(stderr,
2177 Name ": Cannot seek to extended mpb on %s: %s\n",
2178 devname, strerror(errno));
2179 return 1;
2180 }
2181
949c47a0 2182 if (read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
cdddbdbc
DW
2183 if (devname)
2184 fprintf(stderr,
2185 Name ": Cannot read extended mpb on %s: %s\n",
2186 devname, strerror(errno));
2187 return 2;
2188 }
2189
949c47a0
DW
2190 check_sum = __gen_imsm_checksum(super->anchor);
2191 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
cdddbdbc
DW
2192 if (devname)
2193 fprintf(stderr,
2194 Name ": IMSM checksum %x != %x on %s\n",
949c47a0 2195 check_sum, __le32_to_cpu(super->anchor->check_sum),
cdddbdbc 2196 devname);
db575f3b 2197 return 3;
cdddbdbc
DW
2198 }
2199
604b746f
JD
2200 /* FIXME the BBM log is disk specific so we cannot use this global
2201 * buffer for all disks. Ok for now since we only look at the global
2202 * bbm_log_size parameter to gate assembly
2203 */
2204 super->bbm_log = __get_imsm_bbm_log(super->anchor);
2205
a2b97981
DW
2206 return 0;
2207}
2208
2209static int
2210load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
2211{
2212 int err;
2213
2214 err = load_imsm_mpb(fd, super, devname);
2215 if (err)
2216 return err;
2217 err = load_imsm_disk(fd, super, devname, keep_fd);
2218 if (err)
2219 return err;
2220 err = parse_raid_devices(super);
4d7b1503 2221
a2b97981 2222 return err;
cdddbdbc
DW
2223}
2224
ae6aad82
DW
2225static void __free_imsm_disk(struct dl *d)
2226{
2227 if (d->fd >= 0)
2228 close(d->fd);
2229 if (d->devname)
2230 free(d->devname);
0dcecb2e
DW
2231 if (d->e)
2232 free(d->e);
ae6aad82
DW
2233 free(d);
2234
2235}
cdddbdbc
DW
2236static void free_imsm_disks(struct intel_super *super)
2237{
47ee5a45 2238 struct dl *d;
cdddbdbc 2239
47ee5a45
DW
2240 while (super->disks) {
2241 d = super->disks;
cdddbdbc 2242 super->disks = d->next;
ae6aad82 2243 __free_imsm_disk(d);
cdddbdbc 2244 }
47ee5a45
DW
2245 while (super->missing) {
2246 d = super->missing;
2247 super->missing = d->next;
2248 __free_imsm_disk(d);
2249 }
2250
cdddbdbc
DW
2251}
2252
9ca2c81c 2253/* free all the pieces hanging off of a super pointer */
d23fe947 2254static void __free_imsm(struct intel_super *super, int free_disks)
cdddbdbc 2255{
9ca2c81c 2256 if (super->buf) {
949c47a0 2257 free(super->buf);
9ca2c81c
DW
2258 super->buf = NULL;
2259 }
d23fe947
DW
2260 if (free_disks)
2261 free_imsm_disks(super);
ba2de7ba 2262 free_devlist(super);
88c32bb1
DW
2263 if (super->hba) {
2264 free((void *) super->hba);
2265 super->hba = NULL;
2266 }
cdddbdbc
DW
2267}
2268
9ca2c81c
DW
2269static void free_imsm(struct intel_super *super)
2270{
d23fe947 2271 __free_imsm(super, 1);
9ca2c81c
DW
2272 free(super);
2273}
cdddbdbc
DW
2274
2275static void free_super_imsm(struct supertype *st)
2276{
2277 struct intel_super *super = st->sb;
2278
2279 if (!super)
2280 return;
2281
2282 free_imsm(super);
2283 st->sb = NULL;
2284}
2285
c2c087e6
DW
2286static struct intel_super *alloc_super(int creating_imsm)
2287{
2288 struct intel_super *super = malloc(sizeof(*super));
2289
2290 if (super) {
2291 memset(super, 0, sizeof(*super));
2292 super->creating_imsm = creating_imsm;
bf5a934a 2293 super->current_vol = -1;
0dcecb2e 2294 super->create_offset = ~((__u32 ) 0);
88c32bb1
DW
2295 if (!check_env("IMSM_NO_PLATFORM"))
2296 super->orom = find_imsm_orom();
cceebc67 2297 if (super->orom && !check_env("IMSM_TEST_OROM")) {
88c32bb1
DW
2298 struct sys_dev *list, *ent;
2299
2300 /* find the first intel ahci controller */
2301 list = find_driver_devices("pci", "ahci");
2302 for (ent = list; ent; ent = ent->next)
2303 if (devpath_to_vendor(ent->path) == 0x8086)
2304 break;
2305 if (ent) {
2306 super->hba = ent->path;
2307 ent->path = NULL;
2308 }
2309 free_sys_dev(&list);
2310 }
c2c087e6
DW
2311 }
2312
2313 return super;
2314}
2315
cdddbdbc 2316#ifndef MDASSEMBLE
47ee5a45
DW
2317/* find_missing - helper routine for load_super_imsm_all that identifies
2318 * disks that have disappeared from the system. This routine relies on
2319 * the mpb being uptodate, which it is at load time.
2320 */
2321static int find_missing(struct intel_super *super)
2322{
2323 int i;
2324 struct imsm_super *mpb = super->anchor;
2325 struct dl *dl;
2326 struct imsm_disk *disk;
47ee5a45
DW
2327
2328 for (i = 0; i < mpb->num_disks; i++) {
2329 disk = __get_imsm_disk(mpb, i);
54c2c1ea 2330 dl = serial_to_dl(disk->serial, super);
47ee5a45
DW
2331 if (dl)
2332 continue;
47ee5a45
DW
2333
2334 dl = malloc(sizeof(*dl));
2335 if (!dl)
2336 return 1;
2337 dl->major = 0;
2338 dl->minor = 0;
2339 dl->fd = -1;
2340 dl->devname = strdup("missing");
2341 dl->index = i;
2342 serialcpy(dl->serial, disk->serial);
2343 dl->disk = *disk;
689c9bf3 2344 dl->e = NULL;
47ee5a45
DW
2345 dl->next = super->missing;
2346 super->missing = dl;
2347 }
2348
2349 return 0;
2350}
2351
a2b97981
DW
2352static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
2353{
2354 struct intel_disk *idisk = disk_list;
2355
2356 while (idisk) {
2357 if (serialcmp(idisk->disk.serial, serial) == 0)
2358 break;
2359 idisk = idisk->next;
2360 }
2361
2362 return idisk;
2363}
2364
2365static int __prep_thunderdome(struct intel_super **table, int tbl_size,
2366 struct intel_super *super,
2367 struct intel_disk **disk_list)
2368{
2369 struct imsm_disk *d = &super->disks->disk;
2370 struct imsm_super *mpb = super->anchor;
2371 int i, j;
2372
2373 for (i = 0; i < tbl_size; i++) {
2374 struct imsm_super *tbl_mpb = table[i]->anchor;
2375 struct imsm_disk *tbl_d = &table[i]->disks->disk;
2376
2377 if (tbl_mpb->family_num == mpb->family_num) {
2378 if (tbl_mpb->check_sum == mpb->check_sum) {
2379 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2380 __func__, super->disks->major,
2381 super->disks->minor,
2382 table[i]->disks->major,
2383 table[i]->disks->minor);
2384 break;
2385 }
2386
2387 if (((is_configured(d) && !is_configured(tbl_d)) ||
2388 is_configured(d) == is_configured(tbl_d)) &&
2389 tbl_mpb->generation_num < mpb->generation_num) {
2390 /* current version of the mpb is a
2391 * better candidate than the one in
2392 * super_table, but copy over "cross
2393 * generational" status
2394 */
2395 struct intel_disk *idisk;
2396
2397 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2398 __func__, super->disks->major,
2399 super->disks->minor,
2400 table[i]->disks->major,
2401 table[i]->disks->minor);
2402
2403 idisk = disk_list_get(tbl_d->serial, *disk_list);
2404 if (idisk && is_failed(&idisk->disk))
2405 tbl_d->status |= FAILED_DISK;
2406 break;
2407 } else {
2408 struct intel_disk *idisk;
2409 struct imsm_disk *disk;
2410
2411 /* tbl_mpb is more up to date, but copy
2412 * over cross generational status before
2413 * returning
2414 */
2415 disk = __serial_to_disk(d->serial, mpb, NULL);
2416 if (disk && is_failed(disk))
2417 d->status |= FAILED_DISK;
2418
2419 idisk = disk_list_get(d->serial, *disk_list);
2420 if (idisk) {
2421 idisk->owner = i;
2422 if (disk && is_configured(disk))
2423 idisk->disk.status |= CONFIGURED_DISK;
2424 }
2425
2426 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2427 __func__, super->disks->major,
2428 super->disks->minor,
2429 table[i]->disks->major,
2430 table[i]->disks->minor);
2431
2432 return tbl_size;
2433 }
2434 }
2435 }
2436
2437 if (i >= tbl_size)
2438 table[tbl_size++] = super;
2439 else
2440 table[i] = super;
2441
2442 /* update/extend the merged list of imsm_disk records */
2443 for (j = 0; j < mpb->num_disks; j++) {
2444 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
2445 struct intel_disk *idisk;
2446
2447 idisk = disk_list_get(disk->serial, *disk_list);
2448 if (idisk) {
2449 idisk->disk.status |= disk->status;
2450 if (is_configured(&idisk->disk) ||
2451 is_failed(&idisk->disk))
2452 idisk->disk.status &= ~(SPARE_DISK);
2453 } else {
2454 idisk = calloc(1, sizeof(*idisk));
2455 if (!idisk)
2456 return -1;
2457 idisk->owner = IMSM_UNKNOWN_OWNER;
2458 idisk->disk = *disk;
2459 idisk->next = *disk_list;
2460 *disk_list = idisk;
2461 }
2462
2463 if (serialcmp(idisk->disk.serial, d->serial) == 0)
2464 idisk->owner = i;
2465 }
2466
2467 return tbl_size;
2468}
2469
2470static struct intel_super *
2471validate_members(struct intel_super *super, struct intel_disk *disk_list,
2472 const int owner)
2473{
2474 struct imsm_super *mpb = super->anchor;
2475 int ok_count = 0;
2476 int i;
2477
2478 for (i = 0; i < mpb->num_disks; i++) {
2479 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
2480 struct intel_disk *idisk;
2481
2482 idisk = disk_list_get(disk->serial, disk_list);
2483 if (idisk) {
2484 if (idisk->owner == owner ||
2485 idisk->owner == IMSM_UNKNOWN_OWNER)
2486 ok_count++;
2487 else
2488 dprintf("%s: '%.16s' owner %d != %d\n",
2489 __func__, disk->serial, idisk->owner,
2490 owner);
2491 } else {
2492 dprintf("%s: unknown disk %x [%d]: %.16s\n",
2493 __func__, __le32_to_cpu(mpb->family_num), i,
2494 disk->serial);
2495 break;
2496 }
2497 }
2498
2499 if (ok_count == mpb->num_disks)
2500 return super;
2501 return NULL;
2502}
2503
2504static void show_conflicts(__u32 family_num, struct intel_super *super_list)
2505{
2506 struct intel_super *s;
2507
2508 for (s = super_list; s; s = s->next) {
2509 if (family_num != s->anchor->family_num)
2510 continue;
2511 fprintf(stderr, "Conflict, offlining family %#x on '%s'\n",
2512 __le32_to_cpu(family_num), s->disks->devname);
2513 }
2514}
2515
2516static struct intel_super *
2517imsm_thunderdome(struct intel_super **super_list, int len)
2518{
2519 struct intel_super *super_table[len];
2520 struct intel_disk *disk_list = NULL;
2521 struct intel_super *champion, *spare;
2522 struct intel_super *s, **del;
2523 int tbl_size = 0;
2524 int conflict;
2525 int i;
2526
2527 memset(super_table, 0, sizeof(super_table));
2528 for (s = *super_list; s; s = s->next)
2529 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
2530
2531 for (i = 0; i < tbl_size; i++) {
2532 struct imsm_disk *d;
2533 struct intel_disk *idisk;
2534 struct imsm_super *mpb = super_table[i]->anchor;
2535
2536 s = super_table[i];
2537 d = &s->disks->disk;
2538
2539 /* 'd' must appear in merged disk list for its
2540 * configuration to be valid
2541 */
2542 idisk = disk_list_get(d->serial, disk_list);
2543 if (idisk && idisk->owner == i)
2544 s = validate_members(s, disk_list, i);
2545 else
2546 s = NULL;
2547
2548 if (!s)
2549 dprintf("%s: marking family: %#x from %d:%d offline\n",
2550 __func__, mpb->family_num,
2551 super_table[i]->disks->major,
2552 super_table[i]->disks->minor);
2553 super_table[i] = s;
2554 }
2555
2556 /* This is where the mdadm implementation differs from the Windows
2557 * driver which has no strict concept of a container. We can only
2558 * assemble one family from a container, so when returning a prodigal
2559 * array member to this system the code will not be able to disambiguate
2560 * the container contents that should be assembled ("foreign" versus
2561 * "local"). It requires user intervention to set the orig_family_num
2562 * to a new value to establish a new container. The Windows driver in
2563 * this situation fixes up the volume name in place and manages the
2564 * foreign array as an independent entity.
2565 */
2566 s = NULL;
2567 spare = NULL;
2568 conflict = 0;
2569 for (i = 0; i < tbl_size; i++) {
2570 struct intel_super *tbl_ent = super_table[i];
2571 int is_spare = 0;
2572
2573 if (!tbl_ent)
2574 continue;
2575
2576 if (tbl_ent->anchor->num_raid_devs == 0) {
2577 spare = tbl_ent;
2578 is_spare = 1;
2579 }
2580
2581 if (s && !is_spare) {
2582 show_conflicts(tbl_ent->anchor->family_num, *super_list);
2583 conflict++;
2584 } else if (!s && !is_spare)
2585 s = tbl_ent;
2586 }
2587
2588 if (!s)
2589 s = spare;
2590 if (!s) {
2591 champion = NULL;
2592 goto out;
2593 }
2594 champion = s;
2595
2596 if (conflict)
2597 fprintf(stderr, "Chose family %#x on '%s', "
2598 "assemble conflicts to new container with '--update=uuid'\n",
2599 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
2600
2601 /* collect all dl's onto 'champion', and update them to
2602 * champion's version of the status
2603 */
2604 for (s = *super_list; s; s = s->next) {
2605 struct imsm_super *mpb = champion->anchor;
2606 struct dl *dl = s->disks;
2607
2608 if (s == champion)
2609 continue;
2610
2611 for (i = 0; i < mpb->num_disks; i++) {
2612 struct imsm_disk *disk;
2613
2614 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
2615 if (disk) {
2616 dl->disk = *disk;
2617 /* only set index on disks that are a member of
2618 * a populated contianer, i.e. one with
2619 * raid_devs
2620 */
2621 if (is_failed(&dl->disk))
2622 dl->index = -2;
2623 else if (is_spare(&dl->disk))
2624 dl->index = -1;
2625 break;
2626 }
2627 }
2628
2629 if (i >= mpb->num_disks) {
2630 struct intel_disk *idisk;
2631
2632 idisk = disk_list_get(dl->serial, disk_list);
ecf408e9 2633 if (idisk && is_spare(&idisk->disk) &&
a2b97981
DW
2634 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
2635 dl->index = -1;
2636 else {
2637 dl->index = -2;
2638 continue;
2639 }
2640 }
2641
2642 dl->next = champion->disks;
2643 champion->disks = dl;
2644 s->disks = NULL;
2645 }
2646
2647 /* delete 'champion' from super_list */
2648 for (del = super_list; *del; ) {
2649 if (*del == champion) {
2650 *del = (*del)->next;
2651 break;
2652 } else
2653 del = &(*del)->next;
2654 }
2655 champion->next = NULL;
2656
2657 out:
2658 while (disk_list) {
2659 struct intel_disk *idisk = disk_list;
2660
2661 disk_list = disk_list->next;
2662 free(idisk);
2663 }
2664
2665 return champion;
2666}
2667
cdddbdbc
DW
2668static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
2669 char *devname, int keep_fd)
2670{
2671 struct mdinfo *sra;
a2b97981
DW
2672 struct intel_super *super_list = NULL;
2673 struct intel_super *super = NULL;
db575f3b 2674 int devnum = fd2devnum(fd);
a2b97981 2675 struct mdinfo *sd;
db575f3b 2676 int retry;
a2b97981
DW
2677 int err = 0;
2678 int i;
dab4a513 2679 enum sysfs_read_flags flags;
cdddbdbc 2680
dab4a513
DW
2681 flags = GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE;
2682 if (mdmon_running(devnum))
2683 flags |= SKIP_GONE_DEVS;
2684
2685 /* check if 'fd' an opened container */
2686 sra = sysfs_read(fd, 0, flags);
cdddbdbc
DW
2687 if (!sra)
2688 return 1;
2689
2690 if (sra->array.major_version != -1 ||
2691 sra->array.minor_version != -2 ||
2692 strcmp(sra->text_version, "imsm") != 0)
2693 return 1;
2694
a2b97981
DW
2695 /* load all mpbs */
2696 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
2697 struct intel_super *s = alloc_super(0);
2698 char nm[20];
2699 int dfd;
2700
2701 err = 1;
2702 if (!s)
2703 goto error;
2704 s->next = super_list;
2705 super_list = s;
cdddbdbc 2706
a2b97981 2707 err = 2;
cdddbdbc
DW
2708 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2709 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
a2b97981
DW
2710 if (dfd < 0)
2711 goto error;
2712
2713 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
db575f3b
DW
2714
2715 /* retry the load if we might have raced against mdmon */
a2b97981 2716 if (err == 3 && mdmon_running(devnum))
db575f3b
DW
2717 for (retry = 0; retry < 3; retry++) {
2718 usleep(3000);
a2b97981
DW
2719 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
2720 if (err != 3)
db575f3b
DW
2721 break;
2722 }
cdddbdbc
DW
2723 if (!keep_fd)
2724 close(dfd);
a2b97981
DW
2725 if (err)
2726 goto error;
cdddbdbc
DW
2727 }
2728
a2b97981
DW
2729 /* all mpbs enter, maybe one leaves */
2730 super = imsm_thunderdome(&super_list, i);
2731 if (!super) {
2732 err = 1;
2733 goto error;
cdddbdbc
DW
2734 }
2735
47ee5a45
DW
2736 if (find_missing(super) != 0) {
2737 free_imsm(super);
a2b97981
DW
2738 err = 2;
2739 goto error;
47ee5a45
DW
2740 }
2741
f7e7067b 2742 if (st->subarray[0]) {
949c47a0 2743 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
bf5a934a 2744 super->current_vol = atoi(st->subarray);
af99d9ca
DW
2745 else {
2746 free_imsm(super);
a2b97981
DW
2747 err = 1;
2748 goto error;
af99d9ca 2749 }
f7e7067b 2750 }
a2b97981
DW
2751 err = 0;
2752
2753 error:
2754 while (super_list) {
2755 struct intel_super *s = super_list;
2756
2757 super_list = super_list->next;
2758 free_imsm(s);
2759 }
2760
2761 if (err)
2762 return err;
f7e7067b 2763
cdddbdbc 2764 *sbp = super;
db575f3b 2765 st->container_dev = devnum;
a2b97981 2766 if (err == 0 && st->ss == NULL) {
bf5a934a 2767 st->ss = &super_imsm;
cdddbdbc
DW
2768 st->minor_version = 0;
2769 st->max_devs = IMSM_MAX_DEVICES;
2770 }
352452c3 2771 st->loaded_container = 1;
cdddbdbc
DW
2772
2773 return 0;
2774}
2775#endif
2776
2777static int load_super_imsm(struct supertype *st, int fd, char *devname)
2778{
2779 struct intel_super *super;
2780 int rv;
2781
2782#ifndef MDASSEMBLE
3dbccbcf 2783 if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0)
cdddbdbc
DW
2784 return 0;
2785#endif
2786
37424f13
DW
2787 free_super_imsm(st);
2788
c2c087e6 2789 super = alloc_super(0);
cdddbdbc
DW
2790 if (!super) {
2791 fprintf(stderr,
2792 Name ": malloc of %zu failed.\n",
2793 sizeof(*super));
2794 return 1;
2795 }
2796
a2b97981 2797 rv = load_and_parse_mpb(fd, super, devname, 0);
cdddbdbc
DW
2798
2799 if (rv) {
2800 if (devname)
2801 fprintf(stderr,
2802 Name ": Failed to load all information "
2803 "sections on %s\n", devname);
2804 free_imsm(super);
2805 return rv;
2806 }
2807
af99d9ca
DW
2808 if (st->subarray[0]) {
2809 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
2810 super->current_vol = atoi(st->subarray);
2811 else {
2812 free_imsm(super);
2813 return 1;
2814 }
2815 }
2816
cdddbdbc
DW
2817 st->sb = super;
2818 if (st->ss == NULL) {
2819 st->ss = &super_imsm;
2820 st->minor_version = 0;
2821 st->max_devs = IMSM_MAX_DEVICES;
2822 }
352452c3 2823 st->loaded_container = 0;
cdddbdbc
DW
2824
2825 return 0;
2826}
2827
ef6ffade
DW
2828static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
2829{
2830 if (info->level == 1)
2831 return 128;
2832 return info->chunk_size >> 9;
2833}
2834
ff596308 2835static __u32 info_to_num_data_stripes(mdu_array_info_t *info, int num_domains)
ef6ffade
DW
2836{
2837 __u32 num_stripes;
2838
2839 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
ff596308 2840 num_stripes /= num_domains;
ef6ffade
DW
2841
2842 return num_stripes;
2843}
2844
fcfd9599
DW
2845static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
2846{
4025c288
DW
2847 if (info->level == 1)
2848 return info->size * 2;
2849 else
2850 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
fcfd9599
DW
2851}
2852
4d1313e9
DW
2853static void imsm_update_version_info(struct intel_super *super)
2854{
2855 /* update the version and attributes */
2856 struct imsm_super *mpb = super->anchor;
2857 char *version;
2858 struct imsm_dev *dev;
2859 struct imsm_map *map;
2860 int i;
2861
2862 for (i = 0; i < mpb->num_raid_devs; i++) {
2863 dev = get_imsm_dev(super, i);
2864 map = get_imsm_map(dev, 0);
2865 if (__le32_to_cpu(dev->size_high) > 0)
2866 mpb->attributes |= MPB_ATTRIB_2TB;
2867
2868 /* FIXME detect when an array spans a port multiplier */
2869 #if 0
2870 mpb->attributes |= MPB_ATTRIB_PM;
2871 #endif
2872
2873 if (mpb->num_raid_devs > 1 ||
2874 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
2875 version = MPB_VERSION_ATTRIBS;
2876 switch (get_imsm_raid_level(map)) {
2877 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
2878 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
2879 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
2880 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
2881 }
2882 } else {
2883 if (map->num_members >= 5)
2884 version = MPB_VERSION_5OR6_DISK_ARRAY;
2885 else if (dev->status == DEV_CLONE_N_GO)
2886 version = MPB_VERSION_CNG;
2887 else if (get_imsm_raid_level(map) == 5)
2888 version = MPB_VERSION_RAID5;
2889 else if (map->num_members >= 3)
2890 version = MPB_VERSION_3OR4_DISK_ARRAY;
2891 else if (get_imsm_raid_level(map) == 1)
2892 version = MPB_VERSION_RAID1;
2893 else
2894 version = MPB_VERSION_RAID0;
2895 }
2896 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
2897 }
2898}
2899
8b353278
DW
2900static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
2901 unsigned long long size, char *name,
2902 char *homehost, int *uuid)
cdddbdbc 2903{
c2c087e6
DW
2904 /* We are creating a volume inside a pre-existing container.
2905 * so st->sb is already set.
2906 */
2907 struct intel_super *super = st->sb;
949c47a0 2908 struct imsm_super *mpb = super->anchor;
ba2de7ba 2909 struct intel_dev *dv;
c2c087e6
DW
2910 struct imsm_dev *dev;
2911 struct imsm_vol *vol;
2912 struct imsm_map *map;
2913 int idx = mpb->num_raid_devs;
2914 int i;
2915 unsigned long long array_blocks;
2c092cad 2916 size_t size_old, size_new;
ff596308 2917 __u32 num_data_stripes;
cdddbdbc 2918
88c32bb1 2919 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
c2c087e6 2920 fprintf(stderr, Name": This imsm-container already has the "
88c32bb1 2921 "maximum of %d volumes\n", super->orom->vpa);
c2c087e6
DW
2922 return 0;
2923 }
2924
2c092cad
DW
2925 /* ensure the mpb is large enough for the new data */
2926 size_old = __le32_to_cpu(mpb->mpb_size);
2927 size_new = disks_to_mpb_size(info->nr_disks);
2928 if (size_new > size_old) {
2929 void *mpb_new;
2930 size_t size_round = ROUND_UP(size_new, 512);
2931
2932 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
2933 fprintf(stderr, Name": could not allocate new mpb\n");
2934 return 0;
2935 }
2936 memcpy(mpb_new, mpb, size_old);
2937 free(mpb);
2938 mpb = mpb_new;
949c47a0 2939 super->anchor = mpb_new;
2c092cad
DW
2940 mpb->mpb_size = __cpu_to_le32(size_new);
2941 memset(mpb_new + size_old, 0, size_round - size_old);
2942 }
bf5a934a 2943 super->current_vol = idx;
d23fe947
DW
2944 /* when creating the first raid device in this container set num_disks
2945 * to zero, i.e. delete this spare and add raid member devices in
2946 * add_to_super_imsm_volume()
2947 */
2948 if (super->current_vol == 0)
2949 mpb->num_disks = 0;
5a038140
DW
2950
2951 for (i = 0; i < super->current_vol; i++) {
2952 dev = get_imsm_dev(super, i);
2953 if (strncmp((char *) dev->volume, name,
2954 MAX_RAID_SERIAL_LEN) == 0) {
2955 fprintf(stderr, Name": '%s' is already defined for this container\n",
2956 name);
2957 return 0;
2958 }
2959 }
2960
bf5a934a 2961 sprintf(st->subarray, "%d", idx);
ba2de7ba
DW
2962 dv = malloc(sizeof(*dv));
2963 if (!dv) {
2964 fprintf(stderr, Name ": failed to allocate device list entry\n");
2965 return 0;
2966 }
949c47a0
DW
2967 dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
2968 if (!dev) {
ba2de7ba 2969 free(dv);
949c47a0
DW
2970 fprintf(stderr, Name": could not allocate raid device\n");
2971 return 0;
2972 }
c2c087e6 2973 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
03bcbc65
DW
2974 if (info->level == 1)
2975 array_blocks = info_to_blocks_per_member(info);
2976 else
2977 array_blocks = calc_array_size(info->level, info->raid_disks,
2978 info->layout, info->chunk_size,
2979 info->size*2);
979d38be
DW
2980 /* round array size down to closest MB */
2981 array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
2982
c2c087e6
DW
2983 dev->size_low = __cpu_to_le32((__u32) array_blocks);
2984 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
2985 dev->status = __cpu_to_le32(0);
2986 dev->reserved_blocks = __cpu_to_le32(0);
2987 vol = &dev->vol;
2988 vol->migr_state = 0;
1484e727 2989 set_migr_type(dev, MIGR_INIT);
c2c087e6 2990 vol->dirty = 0;
f8f603f1 2991 vol->curr_migr_unit = 0;
a965f303 2992 map = get_imsm_map(dev, 0);
0dcecb2e 2993 map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
fcfd9599 2994 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
ef6ffade 2995 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
0556e1a2 2996 map->failed_disk_num = ~0;
c2c087e6
DW
2997 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
2998 IMSM_T_STATE_NORMAL;
252d23c0 2999 map->ddf = 1;
ef6ffade
DW
3000
3001 if (info->level == 1 && info->raid_disks > 2) {
3002 fprintf(stderr, Name": imsm does not support more than 2 disks"
3003 "in a raid1 volume\n");
3004 return 0;
3005 }
81062a36
DW
3006
3007 map->raid_level = info->level;
4d1313e9 3008 if (info->level == 10) {
c2c087e6 3009 map->raid_level = 1;
4d1313e9 3010 map->num_domains = info->raid_disks / 2;
81062a36
DW
3011 } else if (info->level == 1)
3012 map->num_domains = info->raid_disks;
3013 else
ff596308 3014 map->num_domains = 1;
81062a36 3015
ff596308
DW
3016 num_data_stripes = info_to_num_data_stripes(info, map->num_domains);
3017 map->num_data_stripes = __cpu_to_le32(num_data_stripes);
ef6ffade 3018
c2c087e6
DW
3019 map->num_members = info->raid_disks;
3020 for (i = 0; i < map->num_members; i++) {
3021 /* initialized in add_to_super */
be73972f 3022 set_imsm_ord_tbl_ent(map, i, 0);
c2c087e6 3023 }
949c47a0 3024 mpb->num_raid_devs++;
ba2de7ba
DW
3025
3026 dv->dev = dev;
3027 dv->index = super->current_vol;
3028 dv->next = super->devlist;
3029 super->devlist = dv;
c2c087e6 3030
4d1313e9
DW
3031 imsm_update_version_info(super);
3032
c2c087e6 3033 return 1;
cdddbdbc
DW
3034}
3035
bf5a934a
DW
3036static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
3037 unsigned long long size, char *name,
3038 char *homehost, int *uuid)
3039{
3040 /* This is primarily called by Create when creating a new array.
3041 * We will then get add_to_super called for each component, and then
3042 * write_init_super called to write it out to each device.
3043 * For IMSM, Create can create on fresh devices or on a pre-existing
3044 * array.
3045 * To create on a pre-existing array a different method will be called.
3046 * This one is just for fresh drives.
3047 */
3048 struct intel_super *super;
3049 struct imsm_super *mpb;
3050 size_t mpb_size;
4d1313e9 3051 char *version;
bf5a934a 3052
bf5a934a 3053 if (st->sb)
e683ca88
DW
3054 return init_super_imsm_volume(st, info, size, name, homehost, uuid);
3055
3056 if (info)
3057 mpb_size = disks_to_mpb_size(info->nr_disks);
3058 else
3059 mpb_size = 512;
bf5a934a
DW
3060
3061 super = alloc_super(1);
e683ca88 3062 if (super && posix_memalign(&super->buf, 512, mpb_size) != 0) {
bf5a934a 3063 free(super);
e683ca88
DW
3064 super = NULL;
3065 }
3066 if (!super) {
3067 fprintf(stderr, Name
3068 ": %s could not allocate superblock\n", __func__);
bf5a934a
DW
3069 return 0;
3070 }
e683ca88 3071 memset(super->buf, 0, mpb_size);
ef649044 3072 mpb = super->buf;
e683ca88
DW
3073 mpb->mpb_size = __cpu_to_le32(mpb_size);
3074 st->sb = super;
3075
3076 if (info == NULL) {
3077 /* zeroing superblock */
3078 return 0;
3079 }
bf5a934a 3080
4d1313e9
DW
3081 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
3082
3083 version = (char *) mpb->sig;
3084 strcpy(version, MPB_SIGNATURE);
3085 version += strlen(MPB_SIGNATURE);
3086 strcpy(version, MPB_VERSION_RAID0);
bf5a934a 3087
bf5a934a
DW
3088 return 1;
3089}
3090
0e600426 3091#ifndef MDASSEMBLE
f20c3968 3092static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
bf5a934a
DW
3093 int fd, char *devname)
3094{
3095 struct intel_super *super = st->sb;
d23fe947 3096 struct imsm_super *mpb = super->anchor;
bf5a934a
DW
3097 struct dl *dl;
3098 struct imsm_dev *dev;
3099 struct imsm_map *map;
bf5a934a 3100
949c47a0 3101 dev = get_imsm_dev(super, super->current_vol);
a965f303 3102 map = get_imsm_map(dev, 0);
bf5a934a 3103
208933a7
N
3104 if (! (dk->state & (1<<MD_DISK_SYNC))) {
3105 fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
3106 devname);
3107 return 1;
3108 }
3109
efb30e7f
DW
3110 if (fd == -1) {
3111 /* we're doing autolayout so grab the pre-marked (in
3112 * validate_geometry) raid_disk
3113 */
3114 for (dl = super->disks; dl; dl = dl->next)
3115 if (dl->raiddisk == dk->raid_disk)
3116 break;
3117 } else {
3118 for (dl = super->disks; dl ; dl = dl->next)
3119 if (dl->major == dk->major &&
3120 dl->minor == dk->minor)
3121 break;
3122 }
d23fe947 3123
208933a7
N
3124 if (!dl) {
3125 fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
f20c3968 3126 return 1;
208933a7 3127 }
bf5a934a 3128
d23fe947
DW
3129 /* add a pristine spare to the metadata */
3130 if (dl->index < 0) {
3131 dl->index = super->anchor->num_disks;
3132 super->anchor->num_disks++;
3133 }
be73972f 3134 set_imsm_ord_tbl_ent(map, dk->number, dl->index);
ee5aad5a 3135 dl->disk.status = CONFIGURED_DISK;
d23fe947
DW
3136
3137 /* if we are creating the first raid device update the family number */
3138 if (super->current_vol == 0) {
3139 __u32 sum;
3140 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
3141 struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
3142
3143 *_dev = *dev;
3144 *_disk = dl->disk;
148acb7b
DW
3145 sum = random32();
3146 sum += __gen_imsm_checksum(mpb);
d23fe947 3147 mpb->family_num = __cpu_to_le32(sum);
148acb7b 3148 mpb->orig_family_num = mpb->family_num;
d23fe947 3149 }
f20c3968
DW
3150
3151 return 0;
bf5a934a
DW
3152}
3153
f20c3968 3154static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
cdddbdbc
DW
3155 int fd, char *devname)
3156{
c2c087e6 3157 struct intel_super *super = st->sb;
c2c087e6
DW
3158 struct dl *dd;
3159 unsigned long long size;
f2f27e63 3160 __u32 id;
c2c087e6
DW
3161 int rv;
3162 struct stat stb;
3163
88c32bb1
DW
3164 /* if we are on an RAID enabled platform check that the disk is
3165 * attached to the raid controller
3166 */
3167 if (super->hba && !disk_attached_to_hba(fd, super->hba)) {
3168 fprintf(stderr,
3169 Name ": %s is not attached to the raid controller: %s\n",
3170 devname ? : "disk", super->hba);
3171 return 1;
3172 }
3173
f20c3968
DW
3174 if (super->current_vol >= 0)
3175 return add_to_super_imsm_volume(st, dk, fd, devname);
bf5a934a 3176
c2c087e6
DW
3177 fstat(fd, &stb);
3178 dd = malloc(sizeof(*dd));
b9f594fe 3179 if (!dd) {
c2c087e6
DW
3180 fprintf(stderr,
3181 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
f20c3968 3182 return 1;
c2c087e6
DW
3183 }
3184 memset(dd, 0, sizeof(*dd));
3185 dd->major = major(stb.st_rdev);
3186 dd->minor = minor(stb.st_rdev);
b9f594fe 3187 dd->index = -1;
c2c087e6 3188 dd->devname = devname ? strdup(devname) : NULL;
c2c087e6 3189 dd->fd = fd;
689c9bf3 3190 dd->e = NULL;
c2c087e6
DW
3191 rv = imsm_read_serial(fd, devname, dd->serial);
3192 if (rv) {
3193 fprintf(stderr,
0030e8d6 3194 Name ": failed to retrieve scsi serial, aborting\n");
949c47a0 3195 free(dd);
0030e8d6 3196 abort();
c2c087e6
DW
3197 }
3198
c2c087e6
DW
3199 get_dev_size(fd, NULL, &size);
3200 size /= 512;
1f24f035 3201 serialcpy(dd->disk.serial, dd->serial);
b9f594fe 3202 dd->disk.total_blocks = __cpu_to_le32(size);
ee5aad5a 3203 dd->disk.status = SPARE_DISK;
c2c087e6 3204 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
b9f594fe 3205 dd->disk.scsi_id = __cpu_to_le32(id);
c2c087e6 3206 else
b9f594fe 3207 dd->disk.scsi_id = __cpu_to_le32(0);
43dad3d6
DW
3208
3209 if (st->update_tail) {
3210 dd->next = super->add;
3211 super->add = dd;
3212 } else {
3213 dd->next = super->disks;
3214 super->disks = dd;
3215 }
f20c3968
DW
3216
3217 return 0;
cdddbdbc
DW
3218}
3219
f796af5d
DW
3220static int store_imsm_mpb(int fd, struct imsm_super *mpb);
3221
3222static union {
3223 char buf[512];
3224 struct imsm_super anchor;
3225} spare_record __attribute__ ((aligned(512)));
c2c087e6 3226
d23fe947
DW
3227/* spare records have their own family number and do not have any defined raid
3228 * devices
3229 */
3230static int write_super_imsm_spares(struct intel_super *super, int doclose)
3231{
d23fe947 3232 struct imsm_super *mpb = super->anchor;
f796af5d 3233 struct imsm_super *spare = &spare_record.anchor;
d23fe947
DW
3234 __u32 sum;
3235 struct dl *d;
3236
f796af5d
DW
3237 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)),
3238 spare->generation_num = __cpu_to_le32(1UL),
3239 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
3240 spare->num_disks = 1,
3241 spare->num_raid_devs = 0,
3242 spare->cache_size = mpb->cache_size,
3243 spare->pwr_cycle_count = __cpu_to_le32(1),
3244
3245 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
3246 MPB_SIGNATURE MPB_VERSION_RAID0);
d23fe947
DW
3247
3248 for (d = super->disks; d; d = d->next) {
8796fdc4 3249 if (d->index != -1)
d23fe947
DW
3250 continue;
3251
f796af5d
DW
3252 spare->disk[0] = d->disk;
3253 sum = __gen_imsm_checksum(spare);
3254 spare->family_num = __cpu_to_le32(sum);
3255 spare->orig_family_num = 0;
3256 sum = __gen_imsm_checksum(spare);
3257 spare->check_sum = __cpu_to_le32(sum);
d23fe947 3258
f796af5d 3259 if (store_imsm_mpb(d->fd, spare)) {
d23fe947
DW
3260 fprintf(stderr, "%s: failed for device %d:%d %s\n",
3261 __func__, d->major, d->minor, strerror(errno));
e74255d9 3262 return 1;
d23fe947
DW
3263 }
3264 if (doclose) {
3265 close(d->fd);
3266 d->fd = -1;
3267 }
3268 }
3269
e74255d9 3270 return 0;
d23fe947
DW
3271}
3272
c2c087e6 3273static int write_super_imsm(struct intel_super *super, int doclose)
cdddbdbc 3274{
949c47a0 3275 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
3276 struct dl *d;
3277 __u32 generation;
3278 __u32 sum;
d23fe947 3279 int spares = 0;
949c47a0 3280 int i;
a48ac0a8 3281 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
cdddbdbc 3282
c2c087e6
DW
3283 /* 'generation' is incremented everytime the metadata is written */
3284 generation = __le32_to_cpu(mpb->generation_num);
3285 generation++;
3286 mpb->generation_num = __cpu_to_le32(generation);
3287
148acb7b
DW
3288 /* fix up cases where previous mdadm releases failed to set
3289 * orig_family_num
3290 */
3291 if (mpb->orig_family_num == 0)
3292 mpb->orig_family_num = mpb->family_num;
3293
1ee1e9fc 3294 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
d23fe947 3295 for (d = super->disks; d; d = d->next) {
8796fdc4 3296 if (d->index == -1)
d23fe947 3297 spares++;
1ee1e9fc 3298 else
d23fe947 3299 mpb->disk[d->index] = d->disk;
d23fe947 3300 }
47ee5a45
DW
3301 for (d = super->missing; d; d = d->next)
3302 mpb->disk[d->index] = d->disk;
b9f594fe 3303
949c47a0
DW
3304 for (i = 0; i < mpb->num_raid_devs; i++) {
3305 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
3306
ba2de7ba 3307 imsm_copy_dev(dev, get_imsm_dev(super, i));
a48ac0a8 3308 mpb_size += sizeof_imsm_dev(dev, 0);
949c47a0 3309 }
a48ac0a8
DW
3310 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
3311 mpb->mpb_size = __cpu_to_le32(mpb_size);
949c47a0 3312
c2c087e6 3313 /* recalculate checksum */
949c47a0 3314 sum = __gen_imsm_checksum(mpb);
c2c087e6
DW
3315 mpb->check_sum = __cpu_to_le32(sum);
3316
d23fe947 3317 /* write the mpb for disks that compose raid devices */
c2c087e6 3318 for (d = super->disks; d ; d = d->next) {
d23fe947
DW
3319 if (d->index < 0)
3320 continue;
f796af5d 3321 if (store_imsm_mpb(d->fd, mpb))
c2c087e6
DW
3322 fprintf(stderr, "%s: failed for device %d:%d %s\n",
3323 __func__, d->major, d->minor, strerror(errno));
c2c087e6
DW
3324 if (doclose) {
3325 close(d->fd);
3326 d->fd = -1;
3327 }
3328 }
3329
d23fe947
DW
3330 if (spares)
3331 return write_super_imsm_spares(super, doclose);
3332
e74255d9 3333 return 0;
c2c087e6
DW
3334}
3335
0e600426 3336
9b1fb677 3337static int create_array(struct supertype *st, int dev_idx)
43dad3d6
DW
3338{
3339 size_t len;
3340 struct imsm_update_create_array *u;
3341 struct intel_super *super = st->sb;
9b1fb677 3342 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
54c2c1ea
DW
3343 struct imsm_map *map = get_imsm_map(dev, 0);
3344 struct disk_info *inf;
3345 struct imsm_disk *disk;
3346 int i;
43dad3d6 3347
54c2c1ea
DW
3348 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
3349 sizeof(*inf) * map->num_members;
43dad3d6
DW
3350 u = malloc(len);
3351 if (!u) {
3352 fprintf(stderr, "%s: failed to allocate update buffer\n",
3353 __func__);
3354 return 1;
3355 }
3356
3357 u->type = update_create_array;
9b1fb677 3358 u->dev_idx = dev_idx;
43dad3d6 3359 imsm_copy_dev(&u->dev, dev);
54c2c1ea
DW
3360 inf = get_disk_info(u);
3361 for (i = 0; i < map->num_members; i++) {
9b1fb677
DW
3362 int idx = get_imsm_disk_idx(dev, i);
3363
54c2c1ea
DW
3364 disk = get_imsm_disk(super, idx);
3365 serialcpy(inf[i].serial, disk->serial);
3366 }
43dad3d6
DW
3367 append_metadata_update(st, u, len);
3368
3369 return 0;
3370}
3371
7801ac20 3372static int _add_disk(struct supertype *st)
43dad3d6
DW
3373{
3374 struct intel_super *super = st->sb;
3375 size_t len;
3376 struct imsm_update_add_disk *u;
3377
3378 if (!super->add)
3379 return 0;
3380
3381 len = sizeof(*u);
3382 u = malloc(len);
3383 if (!u) {
3384 fprintf(stderr, "%s: failed to allocate update buffer\n",
3385 __func__);
3386 return 1;
3387 }
3388
3389 u->type = update_add_disk;
3390 append_metadata_update(st, u, len);
3391
3392 return 0;
3393}
3394
c2c087e6
DW
3395static int write_init_super_imsm(struct supertype *st)
3396{
9b1fb677
DW
3397 struct intel_super *super = st->sb;
3398 int current_vol = super->current_vol;
3399
3400 /* we are done with current_vol reset it to point st at the container */
3401 super->current_vol = -1;
3402
8273f55e 3403 if (st->update_tail) {
43dad3d6
DW
3404 /* queue the recently created array / added disk
3405 * as a metadata update */
8273f55e 3406 struct dl *d;
43dad3d6 3407 int rv;
8273f55e 3408
43dad3d6 3409 /* determine if we are creating a volume or adding a disk */
9b1fb677 3410 if (current_vol < 0) {
43dad3d6
DW
3411 /* in the add disk case we are running in mdmon
3412 * context, so don't close fd's
3413 */
7801ac20 3414 return _add_disk(st);
43dad3d6 3415 } else
9b1fb677 3416 rv = create_array(st, current_vol);
8273f55e
DW
3417
3418 for (d = super->disks; d ; d = d->next) {
3419 close(d->fd);
3420 d->fd = -1;
3421 }
3422
43dad3d6 3423 return rv;
8273f55e
DW
3424 } else
3425 return write_super_imsm(st->sb, 1);
cdddbdbc 3426}
0e600426 3427#endif
cdddbdbc 3428
e683ca88 3429static int store_super_imsm(struct supertype *st, int fd)
cdddbdbc 3430{
e683ca88
DW
3431 struct intel_super *super = st->sb;
3432 struct imsm_super *mpb = super ? super->anchor : NULL;
551c80c1 3433
e683ca88 3434 if (!mpb)
ad97895e
DW
3435 return 1;
3436
1799c9e8 3437#ifndef MDASSEMBLE
e683ca88 3438 return store_imsm_mpb(fd, mpb);
1799c9e8
N
3439#else
3440 return 1;
3441#endif
cdddbdbc
DW
3442}
3443
0e600426
N
3444static int imsm_bbm_log_size(struct imsm_super *mpb)
3445{
3446 return __le32_to_cpu(mpb->bbm_log_size);
3447}
3448
3449#ifndef MDASSEMBLE
cdddbdbc
DW
3450static int validate_geometry_imsm_container(struct supertype *st, int level,
3451 int layout, int raiddisks, int chunk,
c2c087e6 3452 unsigned long long size, char *dev,
2c514b71
NB
3453 unsigned long long *freesize,
3454 int verbose)
cdddbdbc 3455{
c2c087e6
DW
3456 int fd;
3457 unsigned long long ldsize;
88c32bb1 3458 const struct imsm_orom *orom;
cdddbdbc 3459
c2c087e6
DW
3460 if (level != LEVEL_CONTAINER)
3461 return 0;
3462 if (!dev)
3463 return 1;
3464
88c32bb1
DW
3465 if (check_env("IMSM_NO_PLATFORM"))
3466 orom = NULL;
3467 else
3468 orom = find_imsm_orom();
3469 if (orom && raiddisks > orom->tds) {
3470 if (verbose)
3471 fprintf(stderr, Name ": %d exceeds maximum number of"
3472 " platform supported disks: %d\n",
3473 raiddisks, orom->tds);
3474 return 0;
3475 }
3476
c2c087e6
DW
3477 fd = open(dev, O_RDONLY|O_EXCL, 0);
3478 if (fd < 0) {
2c514b71
NB
3479 if (verbose)
3480 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
3481 dev, strerror(errno));
c2c087e6
DW
3482 return 0;
3483 }
3484 if (!get_dev_size(fd, dev, &ldsize)) {
3485 close(fd);
3486 return 0;
3487 }
3488 close(fd);
3489
3490 *freesize = avail_size_imsm(st, ldsize >> 9);
3491
3492 return 1;
cdddbdbc
DW
3493}
3494
0dcecb2e
DW
3495static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
3496{
3497 const unsigned long long base_start = e[*idx].start;
3498 unsigned long long end = base_start + e[*idx].size;
3499 int i;
3500
3501 if (base_start == end)
3502 return 0;
3503
3504 *idx = *idx + 1;
3505 for (i = *idx; i < num_extents; i++) {
3506 /* extend overlapping extents */
3507 if (e[i].start >= base_start &&
3508 e[i].start <= end) {
3509 if (e[i].size == 0)
3510 return 0;
3511 if (e[i].start + e[i].size > end)
3512 end = e[i].start + e[i].size;
3513 } else if (e[i].start > end) {
3514 *idx = i;
3515 break;
3516 }
3517 }
3518
3519 return end - base_start;
3520}
3521
3522static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
3523{
3524 /* build a composite disk with all known extents and generate a new
3525 * 'maxsize' given the "all disks in an array must share a common start
3526 * offset" constraint
3527 */
3528 struct extent *e = calloc(sum_extents, sizeof(*e));
3529 struct dl *dl;
3530 int i, j;
3531 int start_extent;
3532 unsigned long long pos;
b9d77223 3533 unsigned long long start = 0;
0dcecb2e
DW
3534 unsigned long long maxsize;
3535 unsigned long reserve;
3536
3537 if (!e)
a7dd165b 3538 return 0;
0dcecb2e
DW
3539
3540 /* coalesce and sort all extents. also, check to see if we need to
3541 * reserve space between member arrays
3542 */
3543 j = 0;
3544 for (dl = super->disks; dl; dl = dl->next) {
3545 if (!dl->e)
3546 continue;
3547 for (i = 0; i < dl->extent_cnt; i++)
3548 e[j++] = dl->e[i];
3549 }
3550 qsort(e, sum_extents, sizeof(*e), cmp_extent);
3551
3552 /* merge extents */
3553 i = 0;
3554 j = 0;
3555 while (i < sum_extents) {
3556 e[j].start = e[i].start;
3557 e[j].size = find_size(e, &i, sum_extents);
3558 j++;
3559 if (e[j-1].size == 0)
3560 break;
3561 }
3562
3563 pos = 0;
3564 maxsize = 0;
3565 start_extent = 0;
3566 i = 0;
3567 do {
3568 unsigned long long esize;
3569
3570 esize = e[i].start - pos;
3571 if (esize >= maxsize) {
3572 maxsize = esize;
3573 start = pos;
3574 start_extent = i;
3575 }
3576 pos = e[i].start + e[i].size;
3577