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