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