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