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