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