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