]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super-intel.c
Fix for resource leak on error path.
[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;
e207da2f 1247 char *devname;
bf5a934a 1248
efb30e7f
DW
1249 for (dl = super->disks; dl; dl = dl->next)
1250 if (dl->raiddisk == info->disk.raid_disk)
1251 break;
bf5a934a
DW
1252 info->container_member = super->current_vol;
1253 info->array.raid_disks = map->num_members;
1254 info->array.level = get_imsm_raid_level(map);
1255 info->array.layout = imsm_level_to_layout(info->array.level);
1256 info->array.md_minor = -1;
1257 info->array.ctime = 0;
1258 info->array.utime = 0;
301406c9
DW
1259 info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9;
1260 info->array.state = !dev->vol.dirty;
da9b4a62
DW
1261 info->custom_array_size = __le32_to_cpu(dev->size_high);
1262 info->custom_array_size <<= 32;
1263 info->custom_array_size |= __le32_to_cpu(dev->size_low);
301406c9
DW
1264
1265 info->disk.major = 0;
1266 info->disk.minor = 0;
efb30e7f
DW
1267 if (dl) {
1268 info->disk.major = dl->major;
1269 info->disk.minor = dl->minor;
1270 }
bf5a934a
DW
1271
1272 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
1273 info->component_size = __le32_to_cpu(map->blocks_per_member);
301406c9 1274 memset(info->uuid, 0, sizeof(info->uuid));
bf5a934a 1275
f8f603f1 1276 if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty)
301406c9 1277 info->resync_start = 0;
f8f603f1 1278 else if (dev->vol.migr_state)
da188789
DW
1279 /* FIXME add curr_migr_unit to resync_start conversion */
1280 info->resync_start = 0;
301406c9
DW
1281 else
1282 info->resync_start = ~0ULL;
1283
1284 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1285 info->name[MAX_RAID_SERIAL_LEN] = 0;
bf5a934a 1286
f35f2525
N
1287 info->array.major_version = -1;
1288 info->array.minor_version = -2;
e207da2f
AW
1289 devname = devnum2devname(st->container_dev);
1290 *info->text_version = '\0';
1291 if (devname)
1292 sprintf(info->text_version, "/%s/%d", devname, info->container_member);
1293 free(devname);
a67dd8cc 1294 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
51006d85 1295 uuid_from_super_imsm(st, info->uuid);
bf5a934a
DW
1296}
1297
7a70e8aa
DW
1298/* check the config file to see if we can return a real uuid for this spare */
1299static void fixup_container_spare_uuid(struct mdinfo *inf)
1300{
1301 struct mddev_ident_s *array_list;
1302
1303 if (inf->array.level != LEVEL_CONTAINER ||
1304 memcmp(inf->uuid, uuid_match_any, sizeof(int[4])) != 0)
1305 return;
1306
1307 array_list = conf_get_ident(NULL);
1308
1309 for (; array_list; array_list = array_list->next) {
1310 if (array_list->uuid_set) {
1311 struct supertype *_sst; /* spare supertype */
1312 struct supertype *_cst; /* container supertype */
1313
1314 _cst = array_list->st;
7e8545e9
DW
1315 if (_cst)
1316 _sst = _cst->ss->match_metadata_desc(inf->text_version);
1317 else
1318 _sst = NULL;
1319
7a70e8aa
DW
1320 if (_sst) {
1321 memcpy(inf->uuid, array_list->uuid, sizeof(int[4]));
1322 free(_sst);
1323 break;
1324 }
1325 }
1326 }
1327}
bf5a934a 1328
4f5bc454
DW
1329static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
1330{
1331 struct intel_super *super = st->sb;
4f5bc454 1332 struct imsm_disk *disk;
4f5bc454 1333
bf5a934a
DW
1334 if (super->current_vol >= 0) {
1335 getinfo_super_imsm_volume(st, info);
1336 return;
1337 }
d23fe947
DW
1338
1339 /* Set raid_disks to zero so that Assemble will always pull in valid
1340 * spares
1341 */
1342 info->array.raid_disks = 0;
cdddbdbc
DW
1343 info->array.level = LEVEL_CONTAINER;
1344 info->array.layout = 0;
1345 info->array.md_minor = -1;
c2c087e6 1346 info->array.ctime = 0; /* N/A for imsm */
cdddbdbc
DW
1347 info->array.utime = 0;
1348 info->array.chunk_size = 0;
1349
1350 info->disk.major = 0;
1351 info->disk.minor = 0;
cdddbdbc 1352 info->disk.raid_disk = -1;
c2c087e6 1353 info->reshape_active = 0;
f35f2525
N
1354 info->array.major_version = -1;
1355 info->array.minor_version = -2;
c2c087e6 1356 strcpy(info->text_version, "imsm");
a67dd8cc 1357 info->safe_mode_delay = 0;
c2c087e6
DW
1358 info->disk.number = -1;
1359 info->disk.state = 0;
c5afc314 1360 info->name[0] = 0;
c2c087e6 1361
4a04ec6c 1362 if (super->disks) {
14e8215b
DW
1363 __u32 reserved = imsm_reserved_sectors(super, super->disks);
1364
b9f594fe 1365 disk = &super->disks->disk;
14e8215b
DW
1366 info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved;
1367 info->component_size = reserved;
25ed7e59 1368 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
df474657
DW
1369 /* we don't change info->disk.raid_disk here because
1370 * this state will be finalized in mdmon after we have
1371 * found the 'most fresh' version of the metadata
1372 */
25ed7e59
DW
1373 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
1374 info->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
cdddbdbc 1375 }
a575e2a7
DW
1376
1377 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1378 * ->compare_super may have updated the 'num_raid_devs' field for spares
1379 */
1380 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
36ba7d48 1381 uuid_from_super_imsm(st, info->uuid);
7a70e8aa 1382 else {
032e9e29 1383 memcpy(info->uuid, uuid_match_any, sizeof(int[4]));
7a70e8aa
DW
1384 fixup_container_spare_uuid(info);
1385 }
cdddbdbc
DW
1386}
1387
cdddbdbc
DW
1388static int update_super_imsm(struct supertype *st, struct mdinfo *info,
1389 char *update, char *devname, int verbose,
1390 int uuid_set, char *homehost)
1391{
f352c545
DW
1392 /* For 'assemble' and 'force' we need to return non-zero if any
1393 * change was made. For others, the return value is ignored.
1394 * Update options are:
1395 * force-one : This device looks a bit old but needs to be included,
1396 * update age info appropriately.
1397 * assemble: clear any 'faulty' flag to allow this device to
1398 * be assembled.
1399 * force-array: Array is degraded but being forced, mark it clean
1400 * if that will be needed to assemble it.
1401 *
1402 * newdev: not used ????
1403 * grow: Array has gained a new device - this is currently for
1404 * linear only
1405 * resync: mark as dirty so a resync will happen.
1406 * name: update the name - preserving the homehost
6e46bf34 1407 * uuid: Change the uuid of the array to match watch is given
f352c545
DW
1408 *
1409 * Following are not relevant for this imsm:
1410 * sparc2.2 : update from old dodgey metadata
1411 * super-minor: change the preferred_minor number
1412 * summaries: update redundant counters.
f352c545
DW
1413 * homehost: update the recorded homehost
1414 * _reshape_progress: record new reshape_progress position.
1415 */
6e46bf34
DW
1416 int rv = 1;
1417 struct intel_super *super = st->sb;
1418 struct imsm_super *mpb;
f352c545 1419
6e46bf34
DW
1420 /* we can only update container info */
1421 if (!super || super->current_vol >= 0 || !super->anchor)
1422 return 1;
1423
1424 mpb = super->anchor;
1425
1426 if (strcmp(update, "uuid") == 0 && uuid_set && !info->update_private)
1427 fprintf(stderr,
1428 Name ": '--uuid' not supported for imsm metadata\n");
1429 else if (strcmp(update, "uuid") == 0 && uuid_set && info->update_private) {
1430 mpb->orig_family_num = *((__u32 *) info->update_private);
1431 rv = 0;
1432 } else if (strcmp(update, "uuid") == 0) {
1433 __u32 *new_family = malloc(sizeof(*new_family));
1434
1435 /* update orig_family_number with the incoming random
1436 * data, report the new effective uuid, and store the
1437 * new orig_family_num for future updates.
1438 */
1439 if (new_family) {
1440 memcpy(&mpb->orig_family_num, info->uuid, sizeof(__u32));
1441 uuid_from_super_imsm(st, info->uuid);
1442 *new_family = mpb->orig_family_num;
1443 info->update_private = new_family;
1444 rv = 0;
1445 }
1446 } else if (strcmp(update, "assemble") == 0)
1447 rv = 0;
1448 else
1449 fprintf(stderr,
1450 Name ": '--update=%s' not supported for imsm metadata\n",
1451 update);
f352c545 1452
6e46bf34
DW
1453 /* successful update? recompute checksum */
1454 if (rv == 0)
1455 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
f352c545
DW
1456
1457 return rv;
cdddbdbc
DW
1458}
1459
c2c087e6 1460static size_t disks_to_mpb_size(int disks)
cdddbdbc 1461{
c2c087e6 1462 size_t size;
cdddbdbc 1463
c2c087e6
DW
1464 size = sizeof(struct imsm_super);
1465 size += (disks - 1) * sizeof(struct imsm_disk);
1466 size += 2 * sizeof(struct imsm_dev);
1467 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1468 size += (4 - 2) * sizeof(struct imsm_map);
1469 /* 4 possible disk_ord_tbl's */
1470 size += 4 * (disks - 1) * sizeof(__u32);
1471
1472 return size;
1473}
1474
1475static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
1476{
1477 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
1478 return 0;
1479
1480 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
cdddbdbc
DW
1481}
1482
ba2de7ba
DW
1483static void free_devlist(struct intel_super *super)
1484{
1485 struct intel_dev *dv;
1486
1487 while (super->devlist) {
1488 dv = super->devlist->next;
1489 free(super->devlist->dev);
1490 free(super->devlist);
1491 super->devlist = dv;
1492 }
1493}
1494
1495static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
1496{
1497 memcpy(dest, src, sizeof_imsm_dev(src, 0));
1498}
1499
cdddbdbc
DW
1500static int compare_super_imsm(struct supertype *st, struct supertype *tst)
1501{
1502 /*
1503 * return:
1504 * 0 same, or first was empty, and second was copied
1505 * 1 second had wrong number
1506 * 2 wrong uuid
1507 * 3 wrong other info
1508 */
1509 struct intel_super *first = st->sb;
1510 struct intel_super *sec = tst->sb;
1511
1512 if (!first) {
1513 st->sb = tst->sb;
1514 tst->sb = NULL;
1515 return 0;
1516 }
1517
d23fe947
DW
1518 /* if an anchor does not have num_raid_devs set then it is a free
1519 * floating spare
1520 */
1521 if (first->anchor->num_raid_devs > 0 &&
1522 sec->anchor->num_raid_devs > 0) {
a2b97981
DW
1523 /* Determine if these disks might ever have been
1524 * related. Further disambiguation can only take place
1525 * in load_super_imsm_all
1526 */
1527 __u32 first_family = first->anchor->orig_family_num;
1528 __u32 sec_family = sec->anchor->orig_family_num;
1529
f796af5d
DW
1530 if (memcmp(first->anchor->sig, sec->anchor->sig,
1531 MAX_SIGNATURE_LENGTH) != 0)
1532 return 3;
1533
a2b97981
DW
1534 if (first_family == 0)
1535 first_family = first->anchor->family_num;
1536 if (sec_family == 0)
1537 sec_family = sec->anchor->family_num;
1538
1539 if (first_family != sec_family)
d23fe947 1540 return 3;
f796af5d 1541
d23fe947 1542 }
cdddbdbc 1543
f796af5d 1544
3e372e5a
DW
1545 /* if 'first' is a spare promote it to a populated mpb with sec's
1546 * family number
1547 */
1548 if (first->anchor->num_raid_devs == 0 &&
1549 sec->anchor->num_raid_devs > 0) {
78d30f94 1550 int i;
ba2de7ba
DW
1551 struct intel_dev *dv;
1552 struct imsm_dev *dev;
78d30f94
DW
1553
1554 /* we need to copy raid device info from sec if an allocation
1555 * fails here we don't associate the spare
1556 */
1557 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
ba2de7ba
DW
1558 dv = malloc(sizeof(*dv));
1559 if (!dv)
1560 break;
1561 dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
1562 if (!dev) {
1563 free(dv);
1564 break;
78d30f94 1565 }
ba2de7ba
DW
1566 dv->dev = dev;
1567 dv->index = i;
1568 dv->next = first->devlist;
1569 first->devlist = dv;
78d30f94 1570 }
709743c5 1571 if (i < sec->anchor->num_raid_devs) {
ba2de7ba
DW
1572 /* allocation failure */
1573 free_devlist(first);
1574 fprintf(stderr, "imsm: failed to associate spare\n");
1575 return 3;
78d30f94 1576 }
3e372e5a 1577 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
148acb7b 1578 first->anchor->orig_family_num = sec->anchor->orig_family_num;
3e372e5a 1579 first->anchor->family_num = sec->anchor->family_num;
709743c5
DW
1580 for (i = 0; i < sec->anchor->num_raid_devs; i++)
1581 imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
3e372e5a
DW
1582 }
1583
cdddbdbc
DW
1584 return 0;
1585}
1586
0030e8d6
DW
1587static void fd2devname(int fd, char *name)
1588{
1589 struct stat st;
1590 char path[256];
1591 char dname[100];
1592 char *nm;
1593 int rv;
1594
1595 name[0] = '\0';
1596 if (fstat(fd, &st) != 0)
1597 return;
1598 sprintf(path, "/sys/dev/block/%d:%d",
1599 major(st.st_rdev), minor(st.st_rdev));
1600
1601 rv = readlink(path, dname, sizeof(dname));
1602 if (rv <= 0)
1603 return;
1604
1605 dname[rv] = '\0';
1606 nm = strrchr(dname, '/');
1607 nm++;
1608 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
1609}
1610
cdddbdbc
DW
1611extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
1612
1613static int imsm_read_serial(int fd, char *devname,
1614 __u8 serial[MAX_RAID_SERIAL_LEN])
1615{
1616 unsigned char scsi_serial[255];
cdddbdbc
DW
1617 int rv;
1618 int rsp_len;
1f24f035 1619 int len;
316e2bf4
DW
1620 char *dest;
1621 char *src;
1622 char *rsp_buf;
1623 int i;
cdddbdbc
DW
1624
1625 memset(scsi_serial, 0, sizeof(scsi_serial));
cdddbdbc 1626
f9ba0ff1
DW
1627 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
1628
40ebbb9c 1629 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
f9ba0ff1
DW
1630 memset(serial, 0, MAX_RAID_SERIAL_LEN);
1631 fd2devname(fd, (char *) serial);
0030e8d6
DW
1632 return 0;
1633 }
1634
cdddbdbc
DW
1635 if (rv != 0) {
1636 if (devname)
1637 fprintf(stderr,
1638 Name ": Failed to retrieve serial for %s\n",
1639 devname);
1640 return rv;
1641 }
1642
1643 rsp_len = scsi_serial[3];
03cd4cc8
DW
1644 if (!rsp_len) {
1645 if (devname)
1646 fprintf(stderr,
1647 Name ": Failed to retrieve serial for %s\n",
1648 devname);
1649 return 2;
1650 }
1f24f035 1651 rsp_buf = (char *) &scsi_serial[4];
5c3db629 1652
316e2bf4
DW
1653 /* trim all whitespace and non-printable characters and convert
1654 * ':' to ';'
1655 */
1656 for (i = 0, dest = rsp_buf; i < rsp_len; i++) {
1657 src = &rsp_buf[i];
1658 if (*src > 0x20) {
1659 /* ':' is reserved for use in placeholder serial
1660 * numbers for missing disks
1661 */
1662 if (*src == ':')
1663 *dest++ = ';';
1664 else
1665 *dest++ = *src;
1666 }
1667 }
1668 len = dest - rsp_buf;
1669 dest = rsp_buf;
1670
1671 /* truncate leading characters */
1672 if (len > MAX_RAID_SERIAL_LEN) {
1673 dest += len - MAX_RAID_SERIAL_LEN;
1f24f035 1674 len = MAX_RAID_SERIAL_LEN;
316e2bf4 1675 }
5c3db629 1676
5c3db629 1677 memset(serial, 0, MAX_RAID_SERIAL_LEN);
316e2bf4 1678 memcpy(serial, dest, len);
cdddbdbc
DW
1679
1680 return 0;
1681}
1682
1f24f035
DW
1683static int serialcmp(__u8 *s1, __u8 *s2)
1684{
1685 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
1686}
1687
1688static void serialcpy(__u8 *dest, __u8 *src)
1689{
1690 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
1691}
1692
1799c9e8 1693#ifndef MDASSEMBLE
54c2c1ea
DW
1694static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
1695{
1696 struct dl *dl;
1697
1698 for (dl = super->disks; dl; dl = dl->next)
1699 if (serialcmp(dl->serial, serial) == 0)
1700 break;
1701
1702 return dl;
1703}
1799c9e8 1704#endif
54c2c1ea 1705
a2b97981
DW
1706static struct imsm_disk *
1707__serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
1708{
1709 int i;
1710
1711 for (i = 0; i < mpb->num_disks; i++) {
1712 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
1713
1714 if (serialcmp(disk->serial, serial) == 0) {
1715 if (idx)
1716 *idx = i;
1717 return disk;
1718 }
1719 }
1720
1721 return NULL;
1722}
1723
cdddbdbc
DW
1724static int
1725load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
1726{
a2b97981 1727 struct imsm_disk *disk;
cdddbdbc
DW
1728 struct dl *dl;
1729 struct stat stb;
cdddbdbc 1730 int rv;
a2b97981 1731 char name[40];
d23fe947
DW
1732 __u8 serial[MAX_RAID_SERIAL_LEN];
1733
1734 rv = imsm_read_serial(fd, devname, serial);
1735
1736 if (rv != 0)
1737 return 2;
1738
a2b97981 1739 dl = calloc(1, sizeof(*dl));
b9f594fe 1740 if (!dl) {
cdddbdbc
DW
1741 if (devname)
1742 fprintf(stderr,
1743 Name ": failed to allocate disk buffer for %s\n",
1744 devname);
1745 return 2;
1746 }
cdddbdbc 1747
a2b97981
DW
1748 fstat(fd, &stb);
1749 dl->major = major(stb.st_rdev);
1750 dl->minor = minor(stb.st_rdev);
1751 dl->next = super->disks;
1752 dl->fd = keep_fd ? fd : -1;
1753 assert(super->disks == NULL);
1754 super->disks = dl;
1755 serialcpy(dl->serial, serial);
1756 dl->index = -2;
1757 dl->e = NULL;
1758 fd2devname(fd, name);
1759 if (devname)
1760 dl->devname = strdup(devname);
1761 else
1762 dl->devname = strdup(name);
cdddbdbc 1763
d23fe947 1764 /* look up this disk's index in the current anchor */
a2b97981
DW
1765 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
1766 if (disk) {
1767 dl->disk = *disk;
1768 /* only set index on disks that are a member of a
1769 * populated contianer, i.e. one with raid_devs
1770 */
1771 if (is_failed(&dl->disk))
3f6efecc 1772 dl->index = -2;
a2b97981
DW
1773 else if (is_spare(&dl->disk))
1774 dl->index = -1;
3f6efecc
DW
1775 }
1776
949c47a0
DW
1777 return 0;
1778}
1779
0e600426 1780#ifndef MDASSEMBLE
0c046afd
DW
1781/* When migrating map0 contains the 'destination' state while map1
1782 * contains the current state. When not migrating map0 contains the
1783 * current state. This routine assumes that map[0].map_state is set to
1784 * the current array state before being called.
1785 *
1786 * Migration is indicated by one of the following states
1787 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
e3bba0e0 1788 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
0c046afd 1789 * map1state=unitialized)
1484e727 1790 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
0c046afd 1791 * map1state=normal)
e3bba0e0 1792 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
0c046afd
DW
1793 * map1state=degraded)
1794 */
0556e1a2 1795static void migrate(struct imsm_dev *dev, __u8 to_state, int migr_type)
3393c6af 1796{
0c046afd 1797 struct imsm_map *dest;
3393c6af
DW
1798 struct imsm_map *src = get_imsm_map(dev, 0);
1799
0c046afd 1800 dev->vol.migr_state = 1;
1484e727 1801 set_migr_type(dev, migr_type);
f8f603f1 1802 dev->vol.curr_migr_unit = 0;
0c046afd
DW
1803 dest = get_imsm_map(dev, 1);
1804
0556e1a2 1805 /* duplicate and then set the target end state in map[0] */
3393c6af 1806 memcpy(dest, src, sizeof_imsm_map(src));
0556e1a2
DW
1807 if (migr_type == MIGR_REBUILD) {
1808 __u32 ord;
1809 int i;
1810
1811 for (i = 0; i < src->num_members; i++) {
1812 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
1813 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
1814 }
1815 }
1816
0c046afd 1817 src->map_state = to_state;
949c47a0 1818}
f8f603f1
DW
1819
1820static void end_migration(struct imsm_dev *dev, __u8 map_state)
1821{
1822 struct imsm_map *map = get_imsm_map(dev, 0);
0556e1a2
DW
1823 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
1824 int i;
1825
1826 /* merge any IMSM_ORD_REBUILD bits that were not successfully
1827 * completed in the last migration.
1828 *
1829 * FIXME add support for online capacity expansion and
1830 * raid-level-migration
1831 */
1832 for (i = 0; i < prev->num_members; i++)
1833 map->disk_ord_tbl[i] |= prev->disk_ord_tbl[i];
f8f603f1
DW
1834
1835 dev->vol.migr_state = 0;
1836 dev->vol.curr_migr_unit = 0;
1837 map->map_state = map_state;
1838}
0e600426 1839#endif
949c47a0
DW
1840
1841static int parse_raid_devices(struct intel_super *super)
1842{
1843 int i;
1844 struct imsm_dev *dev_new;
4d7b1503
DW
1845 size_t len, len_migr;
1846 size_t space_needed = 0;
1847 struct imsm_super *mpb = super->anchor;
949c47a0
DW
1848
1849 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1850 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
ba2de7ba 1851 struct intel_dev *dv;
949c47a0 1852
4d7b1503
DW
1853 len = sizeof_imsm_dev(dev_iter, 0);
1854 len_migr = sizeof_imsm_dev(dev_iter, 1);
1855 if (len_migr > len)
1856 space_needed += len_migr - len;
1857
ba2de7ba
DW
1858 dv = malloc(sizeof(*dv));
1859 if (!dv)
1860 return 1;
4d7b1503 1861 dev_new = malloc(len_migr);
ba2de7ba
DW
1862 if (!dev_new) {
1863 free(dv);
949c47a0 1864 return 1;
ba2de7ba 1865 }
949c47a0 1866 imsm_copy_dev(dev_new, dev_iter);
ba2de7ba
DW
1867 dv->dev = dev_new;
1868 dv->index = i;
1869 dv->next = super->devlist;
1870 super->devlist = dv;
949c47a0 1871 }
cdddbdbc 1872
4d7b1503
DW
1873 /* ensure that super->buf is large enough when all raid devices
1874 * are migrating
1875 */
1876 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
1877 void *buf;
1878
1879 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512);
1880 if (posix_memalign(&buf, 512, len) != 0)
1881 return 1;
1882
1f45a8ad
DW
1883 memcpy(buf, super->buf, super->len);
1884 memset(buf + super->len, 0, len - super->len);
4d7b1503
DW
1885 free(super->buf);
1886 super->buf = buf;
1887 super->len = len;
1888 }
1889
cdddbdbc
DW
1890 return 0;
1891}
1892
604b746f
JD
1893/* retrieve a pointer to the bbm log which starts after all raid devices */
1894struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
1895{
1896 void *ptr = NULL;
1897
1898 if (__le32_to_cpu(mpb->bbm_log_size)) {
1899 ptr = mpb;
1900 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
1901 }
1902
1903 return ptr;
1904}
1905
d23fe947 1906static void __free_imsm(struct intel_super *super, int free_disks);
9ca2c81c 1907
cdddbdbc
DW
1908/* load_imsm_mpb - read matrix metadata
1909 * allocates super->mpb to be freed by free_super
1910 */
1911static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
1912{
1913 unsigned long long dsize;
cdddbdbc
DW
1914 unsigned long long sectors;
1915 struct stat;
6416d527 1916 struct imsm_super *anchor;
cdddbdbc
DW
1917 __u32 check_sum;
1918
cdddbdbc
DW
1919 get_dev_size(fd, NULL, &dsize);
1920
1921 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
1922 if (devname)
1923 fprintf(stderr,
1924 Name ": Cannot seek to anchor block on %s: %s\n",
1925 devname, strerror(errno));
1926 return 1;
1927 }
1928
949c47a0 1929 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
ad97895e
DW
1930 if (devname)
1931 fprintf(stderr,
1932 Name ": Failed to allocate imsm anchor buffer"
1933 " on %s\n", devname);
1934 return 1;
1935 }
949c47a0 1936 if (read(fd, anchor, 512) != 512) {
cdddbdbc
DW
1937 if (devname)
1938 fprintf(stderr,
1939 Name ": Cannot read anchor block on %s: %s\n",
1940 devname, strerror(errno));
6416d527 1941 free(anchor);
cdddbdbc
DW
1942 return 1;
1943 }
1944
6416d527 1945 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
cdddbdbc
DW
1946 if (devname)
1947 fprintf(stderr,
1948 Name ": no IMSM anchor on %s\n", devname);
6416d527 1949 free(anchor);
cdddbdbc
DW
1950 return 2;
1951 }
1952
d23fe947 1953 __free_imsm(super, 0);
949c47a0
DW
1954 super->len = ROUND_UP(anchor->mpb_size, 512);
1955 if (posix_memalign(&super->buf, 512, super->len) != 0) {
cdddbdbc
DW
1956 if (devname)
1957 fprintf(stderr,
1958 Name ": unable to allocate %zu byte mpb buffer\n",
949c47a0 1959 super->len);
6416d527 1960 free(anchor);
cdddbdbc
DW
1961 return 2;
1962 }
949c47a0 1963 memcpy(super->buf, anchor, 512);
cdddbdbc 1964
6416d527
NB
1965 sectors = mpb_sectors(anchor) - 1;
1966 free(anchor);
949c47a0 1967 if (!sectors) {
ecf45690
DW
1968 check_sum = __gen_imsm_checksum(super->anchor);
1969 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
1970 if (devname)
1971 fprintf(stderr,
1972 Name ": IMSM checksum %x != %x on %s\n",
1973 check_sum,
1974 __le32_to_cpu(super->anchor->check_sum),
1975 devname);
1976 return 2;
1977 }
1978
a2b97981 1979 return 0;
949c47a0 1980 }
cdddbdbc
DW
1981
1982 /* read the extended mpb */
1983 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
1984 if (devname)
1985 fprintf(stderr,
1986 Name ": Cannot seek to extended mpb on %s: %s\n",
1987 devname, strerror(errno));
1988 return 1;
1989 }
1990
949c47a0 1991 if (read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
cdddbdbc
DW
1992 if (devname)
1993 fprintf(stderr,
1994 Name ": Cannot read extended mpb on %s: %s\n",
1995 devname, strerror(errno));
1996 return 2;
1997 }
1998
949c47a0
DW
1999 check_sum = __gen_imsm_checksum(super->anchor);
2000 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
cdddbdbc
DW
2001 if (devname)
2002 fprintf(stderr,
2003 Name ": IMSM checksum %x != %x on %s\n",
949c47a0 2004 check_sum, __le32_to_cpu(super->anchor->check_sum),
cdddbdbc 2005 devname);
db575f3b 2006 return 3;
cdddbdbc
DW
2007 }
2008
604b746f
JD
2009 /* FIXME the BBM log is disk specific so we cannot use this global
2010 * buffer for all disks. Ok for now since we only look at the global
2011 * bbm_log_size parameter to gate assembly
2012 */
2013 super->bbm_log = __get_imsm_bbm_log(super->anchor);
2014
a2b97981
DW
2015 return 0;
2016}
2017
2018static int
2019load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
2020{
2021 int err;
2022
2023 err = load_imsm_mpb(fd, super, devname);
2024 if (err)
2025 return err;
2026 err = load_imsm_disk(fd, super, devname, keep_fd);
2027 if (err)
2028 return err;
2029 err = parse_raid_devices(super);
4d7b1503 2030
a2b97981 2031 return err;
cdddbdbc
DW
2032}
2033
ae6aad82
DW
2034static void __free_imsm_disk(struct dl *d)
2035{
2036 if (d->fd >= 0)
2037 close(d->fd);
2038 if (d->devname)
2039 free(d->devname);
0dcecb2e
DW
2040 if (d->e)
2041 free(d->e);
ae6aad82
DW
2042 free(d);
2043
2044}
cdddbdbc
DW
2045static void free_imsm_disks(struct intel_super *super)
2046{
47ee5a45 2047 struct dl *d;
cdddbdbc 2048
47ee5a45
DW
2049 while (super->disks) {
2050 d = super->disks;
cdddbdbc 2051 super->disks = d->next;
ae6aad82 2052 __free_imsm_disk(d);
cdddbdbc 2053 }
47ee5a45
DW
2054 while (super->missing) {
2055 d = super->missing;
2056 super->missing = d->next;
2057 __free_imsm_disk(d);
2058 }
2059
cdddbdbc
DW
2060}
2061
9ca2c81c 2062/* free all the pieces hanging off of a super pointer */
d23fe947 2063static void __free_imsm(struct intel_super *super, int free_disks)
cdddbdbc 2064{
9ca2c81c 2065 if (super->buf) {
949c47a0 2066 free(super->buf);
9ca2c81c
DW
2067 super->buf = NULL;
2068 }
d23fe947
DW
2069 if (free_disks)
2070 free_imsm_disks(super);
ba2de7ba 2071 free_devlist(super);
88c32bb1
DW
2072 if (super->hba) {
2073 free((void *) super->hba);
2074 super->hba = NULL;
2075 }
cdddbdbc
DW
2076}
2077
9ca2c81c
DW
2078static void free_imsm(struct intel_super *super)
2079{
d23fe947 2080 __free_imsm(super, 1);
9ca2c81c
DW
2081 free(super);
2082}
cdddbdbc
DW
2083
2084static void free_super_imsm(struct supertype *st)
2085{
2086 struct intel_super *super = st->sb;
2087
2088 if (!super)
2089 return;
2090
2091 free_imsm(super);
2092 st->sb = NULL;
2093}
2094
c2c087e6
DW
2095static struct intel_super *alloc_super(int creating_imsm)
2096{
2097 struct intel_super *super = malloc(sizeof(*super));
2098
2099 if (super) {
2100 memset(super, 0, sizeof(*super));
2101 super->creating_imsm = creating_imsm;
bf5a934a 2102 super->current_vol = -1;
0dcecb2e 2103 super->create_offset = ~((__u32 ) 0);
88c32bb1
DW
2104 if (!check_env("IMSM_NO_PLATFORM"))
2105 super->orom = find_imsm_orom();
cceebc67 2106 if (super->orom && !check_env("IMSM_TEST_OROM")) {
88c32bb1
DW
2107 struct sys_dev *list, *ent;
2108
2109 /* find the first intel ahci controller */
2110 list = find_driver_devices("pci", "ahci");
2111 for (ent = list; ent; ent = ent->next)
2112 if (devpath_to_vendor(ent->path) == 0x8086)
2113 break;
2114 if (ent) {
2115 super->hba = ent->path;
2116 ent->path = NULL;
2117 }
2118 free_sys_dev(&list);
2119 }
c2c087e6
DW
2120 }
2121
2122 return super;
2123}
2124
cdddbdbc 2125#ifndef MDASSEMBLE
47ee5a45
DW
2126/* find_missing - helper routine for load_super_imsm_all that identifies
2127 * disks that have disappeared from the system. This routine relies on
2128 * the mpb being uptodate, which it is at load time.
2129 */
2130static int find_missing(struct intel_super *super)
2131{
2132 int i;
2133 struct imsm_super *mpb = super->anchor;
2134 struct dl *dl;
2135 struct imsm_disk *disk;
47ee5a45
DW
2136
2137 for (i = 0; i < mpb->num_disks; i++) {
2138 disk = __get_imsm_disk(mpb, i);
54c2c1ea 2139 dl = serial_to_dl(disk->serial, super);
47ee5a45
DW
2140 if (dl)
2141 continue;
47ee5a45
DW
2142
2143 dl = malloc(sizeof(*dl));
2144 if (!dl)
2145 return 1;
2146 dl->major = 0;
2147 dl->minor = 0;
2148 dl->fd = -1;
2149 dl->devname = strdup("missing");
2150 dl->index = i;
2151 serialcpy(dl->serial, disk->serial);
2152 dl->disk = *disk;
689c9bf3 2153 dl->e = NULL;
47ee5a45
DW
2154 dl->next = super->missing;
2155 super->missing = dl;
2156 }
2157
2158 return 0;
2159}
2160
a2b97981
DW
2161static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
2162{
2163 struct intel_disk *idisk = disk_list;
2164
2165 while (idisk) {
2166 if (serialcmp(idisk->disk.serial, serial) == 0)
2167 break;
2168 idisk = idisk->next;
2169 }
2170
2171 return idisk;
2172}
2173
2174static int __prep_thunderdome(struct intel_super **table, int tbl_size,
2175 struct intel_super *super,
2176 struct intel_disk **disk_list)
2177{
2178 struct imsm_disk *d = &super->disks->disk;
2179 struct imsm_super *mpb = super->anchor;
2180 int i, j;
2181
2182 for (i = 0; i < tbl_size; i++) {
2183 struct imsm_super *tbl_mpb = table[i]->anchor;
2184 struct imsm_disk *tbl_d = &table[i]->disks->disk;
2185
2186 if (tbl_mpb->family_num == mpb->family_num) {
2187 if (tbl_mpb->check_sum == mpb->check_sum) {
2188 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2189 __func__, super->disks->major,
2190 super->disks->minor,
2191 table[i]->disks->major,
2192 table[i]->disks->minor);
2193 break;
2194 }
2195
2196 if (((is_configured(d) && !is_configured(tbl_d)) ||
2197 is_configured(d) == is_configured(tbl_d)) &&
2198 tbl_mpb->generation_num < mpb->generation_num) {
2199 /* current version of the mpb is a
2200 * better candidate than the one in
2201 * super_table, but copy over "cross
2202 * generational" status
2203 */
2204 struct intel_disk *idisk;
2205
2206 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2207 __func__, super->disks->major,
2208 super->disks->minor,
2209 table[i]->disks->major,
2210 table[i]->disks->minor);
2211
2212 idisk = disk_list_get(tbl_d->serial, *disk_list);
2213 if (idisk && is_failed(&idisk->disk))
2214 tbl_d->status |= FAILED_DISK;
2215 break;
2216 } else {
2217 struct intel_disk *idisk;
2218 struct imsm_disk *disk;
2219
2220 /* tbl_mpb is more up to date, but copy
2221 * over cross generational status before
2222 * returning
2223 */
2224 disk = __serial_to_disk(d->serial, mpb, NULL);
2225 if (disk && is_failed(disk))
2226 d->status |= FAILED_DISK;
2227
2228 idisk = disk_list_get(d->serial, *disk_list);
2229 if (idisk) {
2230 idisk->owner = i;
2231 if (disk && is_configured(disk))
2232 idisk->disk.status |= CONFIGURED_DISK;
2233 }
2234
2235 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2236 __func__, super->disks->major,
2237 super->disks->minor,
2238 table[i]->disks->major,
2239 table[i]->disks->minor);
2240
2241 return tbl_size;
2242 }
2243 }
2244 }
2245
2246 if (i >= tbl_size)
2247 table[tbl_size++] = super;
2248 else
2249 table[i] = super;
2250
2251 /* update/extend the merged list of imsm_disk records */
2252 for (j = 0; j < mpb->num_disks; j++) {
2253 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
2254 struct intel_disk *idisk;
2255
2256 idisk = disk_list_get(disk->serial, *disk_list);
2257 if (idisk) {
2258 idisk->disk.status |= disk->status;
2259 if (is_configured(&idisk->disk) ||
2260 is_failed(&idisk->disk))
2261 idisk->disk.status &= ~(SPARE_DISK);
2262 } else {
2263 idisk = calloc(1, sizeof(*idisk));
2264 if (!idisk)
2265 return -1;
2266 idisk->owner = IMSM_UNKNOWN_OWNER;
2267 idisk->disk = *disk;
2268 idisk->next = *disk_list;
2269 *disk_list = idisk;
2270 }
2271
2272 if (serialcmp(idisk->disk.serial, d->serial) == 0)
2273 idisk->owner = i;
2274 }
2275
2276 return tbl_size;
2277}
2278
2279static struct intel_super *
2280validate_members(struct intel_super *super, struct intel_disk *disk_list,
2281 const int owner)
2282{
2283 struct imsm_super *mpb = super->anchor;
2284 int ok_count = 0;
2285 int i;
2286
2287 for (i = 0; i < mpb->num_disks; i++) {
2288 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
2289 struct intel_disk *idisk;
2290
2291 idisk = disk_list_get(disk->serial, disk_list);
2292 if (idisk) {
2293 if (idisk->owner == owner ||
2294 idisk->owner == IMSM_UNKNOWN_OWNER)
2295 ok_count++;
2296 else
2297 dprintf("%s: '%.16s' owner %d != %d\n",
2298 __func__, disk->serial, idisk->owner,
2299 owner);
2300 } else {
2301 dprintf("%s: unknown disk %x [%d]: %.16s\n",
2302 __func__, __le32_to_cpu(mpb->family_num), i,
2303 disk->serial);
2304 break;
2305 }
2306 }
2307
2308 if (ok_count == mpb->num_disks)
2309 return super;
2310 return NULL;
2311}
2312
2313static void show_conflicts(__u32 family_num, struct intel_super *super_list)
2314{
2315 struct intel_super *s;
2316
2317 for (s = super_list; s; s = s->next) {
2318 if (family_num != s->anchor->family_num)
2319 continue;
2320 fprintf(stderr, "Conflict, offlining family %#x on '%s'\n",
2321 __le32_to_cpu(family_num), s->disks->devname);
2322 }
2323}
2324
2325static struct intel_super *
2326imsm_thunderdome(struct intel_super **super_list, int len)
2327{
2328 struct intel_super *super_table[len];
2329 struct intel_disk *disk_list = NULL;
2330 struct intel_super *champion, *spare;
2331 struct intel_super *s, **del;
2332 int tbl_size = 0;
2333 int conflict;
2334 int i;
2335
2336 memset(super_table, 0, sizeof(super_table));
2337 for (s = *super_list; s; s = s->next)
2338 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
2339
2340 for (i = 0; i < tbl_size; i++) {
2341 struct imsm_disk *d;
2342 struct intel_disk *idisk;
2343 struct imsm_super *mpb = super_table[i]->anchor;
2344
2345 s = super_table[i];
2346 d = &s->disks->disk;
2347
2348 /* 'd' must appear in merged disk list for its
2349 * configuration to be valid
2350 */
2351 idisk = disk_list_get(d->serial, disk_list);
2352 if (idisk && idisk->owner == i)
2353 s = validate_members(s, disk_list, i);
2354 else
2355 s = NULL;
2356
2357 if (!s)
2358 dprintf("%s: marking family: %#x from %d:%d offline\n",
2359 __func__, mpb->family_num,
2360 super_table[i]->disks->major,
2361 super_table[i]->disks->minor);
2362 super_table[i] = s;
2363 }
2364
2365 /* This is where the mdadm implementation differs from the Windows
2366 * driver which has no strict concept of a container. We can only
2367 * assemble one family from a container, so when returning a prodigal
2368 * array member to this system the code will not be able to disambiguate
2369 * the container contents that should be assembled ("foreign" versus
2370 * "local"). It requires user intervention to set the orig_family_num
2371 * to a new value to establish a new container. The Windows driver in
2372 * this situation fixes up the volume name in place and manages the
2373 * foreign array as an independent entity.
2374 */
2375 s = NULL;
2376 spare = NULL;
2377 conflict = 0;
2378 for (i = 0; i < tbl_size; i++) {
2379 struct intel_super *tbl_ent = super_table[i];
2380 int is_spare = 0;
2381
2382 if (!tbl_ent)
2383 continue;
2384
2385 if (tbl_ent->anchor->num_raid_devs == 0) {
2386 spare = tbl_ent;
2387 is_spare = 1;
2388 }
2389
2390 if (s && !is_spare) {
2391 show_conflicts(tbl_ent->anchor->family_num, *super_list);
2392 conflict++;
2393 } else if (!s && !is_spare)
2394 s = tbl_ent;
2395 }
2396
2397 if (!s)
2398 s = spare;
2399 if (!s) {
2400 champion = NULL;
2401 goto out;
2402 }
2403 champion = s;
2404
2405 if (conflict)
2406 fprintf(stderr, "Chose family %#x on '%s', "
2407 "assemble conflicts to new container with '--update=uuid'\n",
2408 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
2409
2410 /* collect all dl's onto 'champion', and update them to
2411 * champion's version of the status
2412 */
2413 for (s = *super_list; s; s = s->next) {
2414 struct imsm_super *mpb = champion->anchor;
2415 struct dl *dl = s->disks;
2416
2417 if (s == champion)
2418 continue;
2419
2420 for (i = 0; i < mpb->num_disks; i++) {
2421 struct imsm_disk *disk;
2422
2423 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
2424 if (disk) {
2425 dl->disk = *disk;
2426 /* only set index on disks that are a member of
2427 * a populated contianer, i.e. one with
2428 * raid_devs
2429 */
2430 if (is_failed(&dl->disk))
2431 dl->index = -2;
2432 else if (is_spare(&dl->disk))
2433 dl->index = -1;
2434 break;
2435 }
2436 }
2437
2438 if (i >= mpb->num_disks) {
2439 struct intel_disk *idisk;
2440
2441 idisk = disk_list_get(dl->serial, disk_list);
2442 if (is_spare(&idisk->disk) &&
2443 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
2444 dl->index = -1;
2445 else {
2446 dl->index = -2;
2447 continue;
2448 }
2449 }
2450
2451 dl->next = champion->disks;
2452 champion->disks = dl;
2453 s->disks = NULL;
2454 }
2455
2456 /* delete 'champion' from super_list */
2457 for (del = super_list; *del; ) {
2458 if (*del == champion) {
2459 *del = (*del)->next;
2460 break;
2461 } else
2462 del = &(*del)->next;
2463 }
2464 champion->next = NULL;
2465
2466 out:
2467 while (disk_list) {
2468 struct intel_disk *idisk = disk_list;
2469
2470 disk_list = disk_list->next;
2471 free(idisk);
2472 }
2473
2474 return champion;
2475}
2476
cdddbdbc
DW
2477static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
2478 char *devname, int keep_fd)
2479{
2480 struct mdinfo *sra;
a2b97981
DW
2481 struct intel_super *super_list = NULL;
2482 struct intel_super *super = NULL;
db575f3b 2483 int devnum = fd2devnum(fd);
a2b97981 2484 struct mdinfo *sd;
db575f3b 2485 int retry;
a2b97981
DW
2486 int err = 0;
2487 int i;
dab4a513 2488 enum sysfs_read_flags flags;
cdddbdbc 2489
dab4a513
DW
2490 flags = GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE;
2491 if (mdmon_running(devnum))
2492 flags |= SKIP_GONE_DEVS;
2493
2494 /* check if 'fd' an opened container */
2495 sra = sysfs_read(fd, 0, flags);
cdddbdbc
DW
2496 if (!sra)
2497 return 1;
2498
2499 if (sra->array.major_version != -1 ||
2500 sra->array.minor_version != -2 ||
1602d52c
AW
2501 strcmp(sra->text_version, "imsm") != 0) {
2502 err = 1;
2503 goto error;
2504 }
a2b97981
DW
2505 /* load all mpbs */
2506 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
2507 struct intel_super *s = alloc_super(0);
7a6ecd55 2508 char nm[32];
a2b97981
DW
2509 int dfd;
2510
2511 err = 1;
2512 if (!s)
2513 goto error;
2514 s->next = super_list;
2515 super_list = s;
cdddbdbc 2516
a2b97981 2517 err = 2;
cdddbdbc
DW
2518 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2519 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
a2b97981
DW
2520 if (dfd < 0)
2521 goto error;
2522
2523 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
db575f3b
DW
2524
2525 /* retry the load if we might have raced against mdmon */
a2b97981 2526 if (err == 3 && mdmon_running(devnum))
db575f3b
DW
2527 for (retry = 0; retry < 3; retry++) {
2528 usleep(3000);
a2b97981
DW
2529 err = load_and_parse_mpb(dfd, s, NULL, keep_fd);
2530 if (err != 3)
db575f3b
DW
2531 break;
2532 }
cdddbdbc
DW
2533 if (!keep_fd)
2534 close(dfd);
a2b97981
DW
2535 if (err)
2536 goto error;
cdddbdbc
DW
2537 }
2538
a2b97981
DW
2539 /* all mpbs enter, maybe one leaves */
2540 super = imsm_thunderdome(&super_list, i);
2541 if (!super) {
2542 err = 1;
2543 goto error;
cdddbdbc
DW
2544 }
2545
47ee5a45
DW
2546 if (find_missing(super) != 0) {
2547 free_imsm(super);
a2b97981
DW
2548 err = 2;
2549 goto error;
47ee5a45
DW
2550 }
2551
f7e7067b 2552 if (st->subarray[0]) {
949c47a0 2553 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
bf5a934a 2554 super->current_vol = atoi(st->subarray);
af99d9ca
DW
2555 else {
2556 free_imsm(super);
a2b97981
DW
2557 err = 1;
2558 goto error;
af99d9ca 2559 }
f7e7067b 2560 }
a2b97981
DW
2561 err = 0;
2562
2563 error:
2564 while (super_list) {
2565 struct intel_super *s = super_list;
2566
2567 super_list = super_list->next;
2568 free_imsm(s);
2569 }
1602d52c 2570 sysfs_free(sra);
a2b97981
DW
2571
2572 if (err)
2573 return err;
f7e7067b 2574
cdddbdbc 2575 *sbp = super;
db575f3b 2576 st->container_dev = devnum;
a2b97981 2577 if (err == 0 && st->ss == NULL) {
bf5a934a 2578 st->ss = &super_imsm;
cdddbdbc
DW
2579 st->minor_version = 0;
2580 st->max_devs = IMSM_MAX_DEVICES;
2581 }
352452c3 2582 st->loaded_container = 1;
cdddbdbc
DW
2583
2584 return 0;
2585}
2586#endif
2587
2588static int load_super_imsm(struct supertype *st, int fd, char *devname)
2589{
2590 struct intel_super *super;
2591 int rv;
2592
2593#ifndef MDASSEMBLE
3dbccbcf 2594 if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0)
cdddbdbc
DW
2595 return 0;
2596#endif
2597
37424f13
DW
2598 free_super_imsm(st);
2599
c2c087e6 2600 super = alloc_super(0);
cdddbdbc
DW
2601 if (!super) {
2602 fprintf(stderr,
2603 Name ": malloc of %zu failed.\n",
2604 sizeof(*super));
2605 return 1;
2606 }
2607
a2b97981 2608 rv = load_and_parse_mpb(fd, super, devname, 0);
cdddbdbc
DW
2609
2610 if (rv) {
2611 if (devname)
2612 fprintf(stderr,
2613 Name ": Failed to load all information "
2614 "sections on %s\n", devname);
2615 free_imsm(super);
2616 return rv;
2617 }
2618
af99d9ca
DW
2619 if (st->subarray[0]) {
2620 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
2621 super->current_vol = atoi(st->subarray);
2622 else {
2623 free_imsm(super);
2624 return 1;
2625 }
2626 }
2627
cdddbdbc
DW
2628 st->sb = super;
2629 if (st->ss == NULL) {
2630 st->ss = &super_imsm;
2631 st->minor_version = 0;
2632 st->max_devs = IMSM_MAX_DEVICES;
2633 }
352452c3 2634 st->loaded_container = 0;
cdddbdbc
DW
2635
2636 return 0;
2637}
2638
ef6ffade
DW
2639static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
2640{
2641 if (info->level == 1)
2642 return 128;
2643 return info->chunk_size >> 9;
2644}
2645
ff596308 2646static __u32 info_to_num_data_stripes(mdu_array_info_t *info, int num_domains)
ef6ffade
DW
2647{
2648 __u32 num_stripes;
2649
2650 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
ff596308 2651 num_stripes /= num_domains;
ef6ffade
DW
2652
2653 return num_stripes;
2654}
2655
fcfd9599
DW
2656static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
2657{
4025c288
DW
2658 if (info->level == 1)
2659 return info->size * 2;
2660 else
2661 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
fcfd9599
DW
2662}
2663
4d1313e9
DW
2664static void imsm_update_version_info(struct intel_super *super)
2665{
2666 /* update the version and attributes */
2667 struct imsm_super *mpb = super->anchor;
2668 char *version;
2669 struct imsm_dev *dev;
2670 struct imsm_map *map;
2671 int i;
2672
2673 for (i = 0; i < mpb->num_raid_devs; i++) {
2674 dev = get_imsm_dev(super, i);
2675 map = get_imsm_map(dev, 0);
2676 if (__le32_to_cpu(dev->size_high) > 0)
2677 mpb->attributes |= MPB_ATTRIB_2TB;
2678
2679 /* FIXME detect when an array spans a port multiplier */
2680 #if 0
2681 mpb->attributes |= MPB_ATTRIB_PM;
2682 #endif
2683
2684 if (mpb->num_raid_devs > 1 ||
2685 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
2686 version = MPB_VERSION_ATTRIBS;
2687 switch (get_imsm_raid_level(map)) {
2688 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
2689 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
2690 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
2691 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
2692 }
2693 } else {
2694 if (map->num_members >= 5)
2695 version = MPB_VERSION_5OR6_DISK_ARRAY;
2696 else if (dev->status == DEV_CLONE_N_GO)
2697 version = MPB_VERSION_CNG;
2698 else if (get_imsm_raid_level(map) == 5)
2699 version = MPB_VERSION_RAID5;
2700 else if (map->num_members >= 3)
2701 version = MPB_VERSION_3OR4_DISK_ARRAY;
2702 else if (get_imsm_raid_level(map) == 1)
2703 version = MPB_VERSION_RAID1;
2704 else
2705 version = MPB_VERSION_RAID0;
2706 }
2707 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
2708 }
2709}
2710
8b353278
DW
2711static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
2712 unsigned long long size, char *name,
2713 char *homehost, int *uuid)
cdddbdbc 2714{
c2c087e6
DW
2715 /* We are creating a volume inside a pre-existing container.
2716 * so st->sb is already set.
2717 */
2718 struct intel_super *super = st->sb;
949c47a0 2719 struct imsm_super *mpb = super->anchor;
ba2de7ba 2720 struct intel_dev *dv;
c2c087e6
DW
2721 struct imsm_dev *dev;
2722 struct imsm_vol *vol;
2723 struct imsm_map *map;
2724 int idx = mpb->num_raid_devs;
2725 int i;
2726 unsigned long long array_blocks;
2c092cad 2727 size_t size_old, size_new;
ff596308 2728 __u32 num_data_stripes;
cdddbdbc 2729
88c32bb1 2730 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
c2c087e6 2731 fprintf(stderr, Name": This imsm-container already has the "
88c32bb1 2732 "maximum of %d volumes\n", super->orom->vpa);
c2c087e6
DW
2733 return 0;
2734 }
2735
2c092cad
DW
2736 /* ensure the mpb is large enough for the new data */
2737 size_old = __le32_to_cpu(mpb->mpb_size);
2738 size_new = disks_to_mpb_size(info->nr_disks);
2739 if (size_new > size_old) {
2740 void *mpb_new;
2741 size_t size_round = ROUND_UP(size_new, 512);
2742
2743 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
2744 fprintf(stderr, Name": could not allocate new mpb\n");
2745 return 0;
2746 }
2747 memcpy(mpb_new, mpb, size_old);
2748 free(mpb);
2749 mpb = mpb_new;
949c47a0 2750 super->anchor = mpb_new;
2c092cad
DW
2751 mpb->mpb_size = __cpu_to_le32(size_new);
2752 memset(mpb_new + size_old, 0, size_round - size_old);
2753 }
bf5a934a 2754 super->current_vol = idx;
d23fe947
DW
2755 /* when creating the first raid device in this container set num_disks
2756 * to zero, i.e. delete this spare and add raid member devices in
2757 * add_to_super_imsm_volume()
2758 */
2759 if (super->current_vol == 0)
2760 mpb->num_disks = 0;
5a038140
DW
2761
2762 for (i = 0; i < super->current_vol; i++) {
2763 dev = get_imsm_dev(super, i);
2764 if (strncmp((char *) dev->volume, name,
2765 MAX_RAID_SERIAL_LEN) == 0) {
2766 fprintf(stderr, Name": '%s' is already defined for this container\n",
2767 name);
2768 return 0;
2769 }
2770 }
2771
bf5a934a 2772 sprintf(st->subarray, "%d", idx);
ba2de7ba
DW
2773 dv = malloc(sizeof(*dv));
2774 if (!dv) {
2775 fprintf(stderr, Name ": failed to allocate device list entry\n");
2776 return 0;
2777 }
949c47a0
DW
2778 dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
2779 if (!dev) {
ba2de7ba 2780 free(dv);
949c47a0
DW
2781 fprintf(stderr, Name": could not allocate raid device\n");
2782 return 0;
2783 }
c2c087e6 2784 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
03bcbc65
DW
2785 if (info->level == 1)
2786 array_blocks = info_to_blocks_per_member(info);
2787 else
2788 array_blocks = calc_array_size(info->level, info->raid_disks,
2789 info->layout, info->chunk_size,
2790 info->size*2);
979d38be
DW
2791 /* round array size down to closest MB */
2792 array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
2793
c2c087e6
DW
2794 dev->size_low = __cpu_to_le32((__u32) array_blocks);
2795 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
2796 dev->status = __cpu_to_le32(0);
2797 dev->reserved_blocks = __cpu_to_le32(0);
2798 vol = &dev->vol;
2799 vol->migr_state = 0;
1484e727 2800 set_migr_type(dev, MIGR_INIT);
c2c087e6 2801 vol->dirty = 0;
f8f603f1 2802 vol->curr_migr_unit = 0;
a965f303 2803 map = get_imsm_map(dev, 0);
0dcecb2e 2804 map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
fcfd9599 2805 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
ef6ffade 2806 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
0556e1a2 2807 map->failed_disk_num = ~0;
c2c087e6
DW
2808 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
2809 IMSM_T_STATE_NORMAL;
252d23c0 2810 map->ddf = 1;
ef6ffade
DW
2811
2812 if (info->level == 1 && info->raid_disks > 2) {
38950822
AW
2813 free(dev);
2814 free(dv);
ef6ffade
DW
2815 fprintf(stderr, Name": imsm does not support more than 2 disks"
2816 "in a raid1 volume\n");
2817 return 0;
2818 }
81062a36
DW
2819
2820 map->raid_level = info->level;
4d1313e9 2821 if (info->level == 10) {
c2c087e6 2822 map->raid_level = 1;
4d1313e9 2823 map->num_domains = info->raid_disks / 2;
81062a36
DW
2824 } else if (info->level == 1)
2825 map->num_domains = info->raid_disks;
2826 else
ff596308 2827 map->num_domains = 1;
81062a36 2828
ff596308
DW
2829 num_data_stripes = info_to_num_data_stripes(info, map->num_domains);
2830 map->num_data_stripes = __cpu_to_le32(num_data_stripes);
ef6ffade 2831
c2c087e6
DW
2832 map->num_members = info->raid_disks;
2833 for (i = 0; i < map->num_members; i++) {
2834 /* initialized in add_to_super */
be73972f 2835 set_imsm_ord_tbl_ent(map, i, 0);
c2c087e6 2836 }
949c47a0 2837 mpb->num_raid_devs++;
ba2de7ba
DW
2838
2839 dv->dev = dev;
2840 dv->index = super->current_vol;
2841 dv->next = super->devlist;
2842 super->devlist = dv;
c2c087e6 2843
4d1313e9
DW
2844 imsm_update_version_info(super);
2845
c2c087e6 2846 return 1;
cdddbdbc
DW
2847}
2848
bf5a934a
DW
2849static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
2850 unsigned long long size, char *name,
2851 char *homehost, int *uuid)
2852{
2853 /* This is primarily called by Create when creating a new array.
2854 * We will then get add_to_super called for each component, and then
2855 * write_init_super called to write it out to each device.
2856 * For IMSM, Create can create on fresh devices or on a pre-existing
2857 * array.
2858 * To create on a pre-existing array a different method will be called.
2859 * This one is just for fresh drives.
2860 */
2861 struct intel_super *super;
2862 struct imsm_super *mpb;
2863 size_t mpb_size;
4d1313e9 2864 char *version;
bf5a934a 2865
bf5a934a 2866 if (st->sb)
e683ca88
DW
2867 return init_super_imsm_volume(st, info, size, name, homehost, uuid);
2868
2869 if (info)
2870 mpb_size = disks_to_mpb_size(info->nr_disks);
2871 else
2872 mpb_size = 512;
bf5a934a
DW
2873
2874 super = alloc_super(1);
e683ca88 2875 if (super && posix_memalign(&super->buf, 512, mpb_size) != 0) {
bf5a934a 2876 free(super);
e683ca88
DW
2877 super = NULL;
2878 }
2879 if (!super) {
2880 fprintf(stderr, Name
2881 ": %s could not allocate superblock\n", __func__);
bf5a934a
DW
2882 return 0;
2883 }
e683ca88 2884 memset(super->buf, 0, mpb_size);
ef649044 2885 mpb = super->buf;
e683ca88
DW
2886 mpb->mpb_size = __cpu_to_le32(mpb_size);
2887 st->sb = super;
2888
2889 if (info == NULL) {
2890 /* zeroing superblock */
2891 return 0;
2892 }
bf5a934a 2893
4d1313e9
DW
2894 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
2895
2896 version = (char *) mpb->sig;
2897 strcpy(version, MPB_SIGNATURE);
2898 version += strlen(MPB_SIGNATURE);
2899 strcpy(version, MPB_VERSION_RAID0);
bf5a934a 2900
bf5a934a
DW
2901 return 1;
2902}
2903
0e600426 2904#ifndef MDASSEMBLE
f20c3968 2905static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
bf5a934a
DW
2906 int fd, char *devname)
2907{
2908 struct intel_super *super = st->sb;
d23fe947 2909 struct imsm_super *mpb = super->anchor;
bf5a934a
DW
2910 struct dl *dl;
2911 struct imsm_dev *dev;
2912 struct imsm_map *map;
bf5a934a 2913
949c47a0 2914 dev = get_imsm_dev(super, super->current_vol);
a965f303 2915 map = get_imsm_map(dev, 0);
bf5a934a 2916
208933a7
N
2917 if (! (dk->state & (1<<MD_DISK_SYNC))) {
2918 fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
2919 devname);
2920 return 1;
2921 }
2922
efb30e7f
DW
2923 if (fd == -1) {
2924 /* we're doing autolayout so grab the pre-marked (in
2925 * validate_geometry) raid_disk
2926 */
2927 for (dl = super->disks; dl; dl = dl->next)
2928 if (dl->raiddisk == dk->raid_disk)
2929 break;
2930 } else {
2931 for (dl = super->disks; dl ; dl = dl->next)
2932 if (dl->major == dk->major &&
2933 dl->minor == dk->minor)
2934 break;
2935 }
d23fe947 2936
208933a7
N
2937 if (!dl) {
2938 fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
f20c3968 2939 return 1;
208933a7 2940 }
bf5a934a 2941
d23fe947
DW
2942 /* add a pristine spare to the metadata */
2943 if (dl->index < 0) {
2944 dl->index = super->anchor->num_disks;
2945 super->anchor->num_disks++;
2946 }
be73972f 2947 set_imsm_ord_tbl_ent(map, dk->number, dl->index);
ee5aad5a 2948 dl->disk.status = CONFIGURED_DISK;
d23fe947
DW
2949
2950 /* if we are creating the first raid device update the family number */
2951 if (super->current_vol == 0) {
2952 __u32 sum;
2953 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
2954 struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
2955
791b666a
AW
2956 if (!_dev || !_disk) {
2957 fprintf(stderr, Name ": BUG mpb setup error\n");
2958 return 1;
2959 }
d23fe947
DW
2960 *_dev = *dev;
2961 *_disk = dl->disk;
148acb7b
DW
2962 sum = random32();
2963 sum += __gen_imsm_checksum(mpb);
d23fe947 2964 mpb->family_num = __cpu_to_le32(sum);
148acb7b 2965 mpb->orig_family_num = mpb->family_num;
d23fe947 2966 }
f20c3968
DW
2967
2968 return 0;
bf5a934a
DW
2969}
2970
f20c3968 2971static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
cdddbdbc
DW
2972 int fd, char *devname)
2973{
c2c087e6 2974 struct intel_super *super = st->sb;
c2c087e6
DW
2975 struct dl *dd;
2976 unsigned long long size;
f2f27e63 2977 __u32 id;
c2c087e6
DW
2978 int rv;
2979 struct stat stb;
2980
88c32bb1
DW
2981 /* if we are on an RAID enabled platform check that the disk is
2982 * attached to the raid controller
2983 */
2984 if (super->hba && !disk_attached_to_hba(fd, super->hba)) {
2985 fprintf(stderr,
2986 Name ": %s is not attached to the raid controller: %s\n",
2987 devname ? : "disk", super->hba);
2988 return 1;
2989 }
2990
f20c3968
DW
2991 if (super->current_vol >= 0)
2992 return add_to_super_imsm_volume(st, dk, fd, devname);
bf5a934a 2993
c2c087e6
DW
2994 fstat(fd, &stb);
2995 dd = malloc(sizeof(*dd));
b9f594fe 2996 if (!dd) {
c2c087e6
DW
2997 fprintf(stderr,
2998 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
f20c3968 2999 return 1;
c2c087e6
DW
3000 }
3001 memset(dd, 0, sizeof(*dd));
3002 dd->major = major(stb.st_rdev);
3003 dd->minor = minor(stb.st_rdev);
b9f594fe 3004 dd->index = -1;
c2c087e6 3005 dd->devname = devname ? strdup(devname) : NULL;
c2c087e6 3006 dd->fd = fd;
689c9bf3 3007 dd->e = NULL;
c2c087e6
DW
3008 rv = imsm_read_serial(fd, devname, dd->serial);
3009 if (rv) {
3010 fprintf(stderr,
0030e8d6 3011 Name ": failed to retrieve scsi serial, aborting\n");
949c47a0 3012 free(dd);
0030e8d6 3013 abort();
c2c087e6
DW
3014 }
3015
c2c087e6
DW
3016 get_dev_size(fd, NULL, &size);
3017 size /= 512;
1f24f035 3018 serialcpy(dd->disk.serial, dd->serial);
b9f594fe 3019 dd->disk.total_blocks = __cpu_to_le32(size);
ee5aad5a 3020 dd->disk.status = SPARE_DISK;
c2c087e6 3021 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
b9f594fe 3022 dd->disk.scsi_id = __cpu_to_le32(id);
c2c087e6 3023 else
b9f594fe 3024 dd->disk.scsi_id = __cpu_to_le32(0);
43dad3d6
DW
3025
3026 if (st->update_tail) {
3027 dd->next = super->add;
3028 super->add = dd;
3029 } else {
3030 dd->next = super->disks;
3031 super->disks = dd;
3032 }
f20c3968
DW
3033
3034 return 0;
cdddbdbc
DW
3035}
3036
f796af5d
DW
3037static int store_imsm_mpb(int fd, struct imsm_super *mpb);
3038
3039static union {
3040 char buf[512];
3041 struct imsm_super anchor;
3042} spare_record __attribute__ ((aligned(512)));
c2c087e6 3043
d23fe947
DW
3044/* spare records have their own family number and do not have any defined raid
3045 * devices
3046 */
3047static int write_super_imsm_spares(struct intel_super *super, int doclose)
3048{
d23fe947 3049 struct imsm_super *mpb = super->anchor;
f796af5d 3050 struct imsm_super *spare = &spare_record.anchor;
d23fe947
DW
3051 __u32 sum;
3052 struct dl *d;
3053
f796af5d
DW
3054 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)),
3055 spare->generation_num = __cpu_to_le32(1UL),
3056 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
3057 spare->num_disks = 1,
3058 spare->num_raid_devs = 0,
3059 spare->cache_size = mpb->cache_size,
3060 spare->pwr_cycle_count = __cpu_to_le32(1),
3061
3062 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
3063 MPB_SIGNATURE MPB_VERSION_RAID0);
d23fe947
DW
3064
3065 for (d = super->disks; d; d = d->next) {
8796fdc4 3066 if (d->index != -1)
d23fe947
DW
3067 continue;
3068
f796af5d
DW
3069 spare->disk[0] = d->disk;
3070 sum = __gen_imsm_checksum(spare);
3071 spare->family_num = __cpu_to_le32(sum);
3072 spare->orig_family_num = 0;
3073 sum = __gen_imsm_checksum(spare);
3074 spare->check_sum = __cpu_to_le32(sum);
d23fe947 3075
f796af5d 3076 if (store_imsm_mpb(d->fd, spare)) {
d23fe947
DW
3077 fprintf(stderr, "%s: failed for device %d:%d %s\n",
3078 __func__, d->major, d->minor, strerror(errno));
e74255d9 3079 return 1;
d23fe947
DW
3080 }
3081 if (doclose) {
3082 close(d->fd);
3083 d->fd = -1;
3084 }
3085 }
3086
e74255d9 3087 return 0;
d23fe947
DW
3088}
3089
c2c087e6 3090static int write_super_imsm(struct intel_super *super, int doclose)
cdddbdbc 3091{
949c47a0 3092 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
3093 struct dl *d;
3094 __u32 generation;
3095 __u32 sum;
d23fe947 3096 int spares = 0;
949c47a0 3097 int i;
a48ac0a8 3098 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
cdddbdbc 3099
c2c087e6
DW
3100 /* 'generation' is incremented everytime the metadata is written */
3101 generation = __le32_to_cpu(mpb->generation_num);
3102 generation++;
3103 mpb->generation_num = __cpu_to_le32(generation);
3104
148acb7b
DW
3105 /* fix up cases where previous mdadm releases failed to set
3106 * orig_family_num
3107 */
3108 if (mpb->orig_family_num == 0)
3109 mpb->orig_family_num = mpb->family_num;
3110
1ee1e9fc 3111 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
d23fe947 3112 for (d = super->disks; d; d = d->next) {
8796fdc4 3113 if (d->index == -1)
d23fe947 3114 spares++;
1ee1e9fc 3115 else
d23fe947 3116 mpb->disk[d->index] = d->disk;
d23fe947 3117 }
47ee5a45
DW
3118 for (d = super->missing; d; d = d->next)
3119 mpb->disk[d->index] = d->disk;
b9f594fe 3120
949c47a0
DW
3121 for (i = 0; i < mpb->num_raid_devs; i++) {
3122 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
3123
ba2de7ba 3124 imsm_copy_dev(dev, get_imsm_dev(super, i));
a48ac0a8 3125 mpb_size += sizeof_imsm_dev(dev, 0);
949c47a0 3126 }
a48ac0a8
DW
3127 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
3128 mpb->mpb_size = __cpu_to_le32(mpb_size);
949c47a0 3129
c2c087e6 3130 /* recalculate checksum */
949c47a0 3131 sum = __gen_imsm_checksum(mpb);
c2c087e6
DW
3132 mpb->check_sum = __cpu_to_le32(sum);
3133
d23fe947 3134 /* write the mpb for disks that compose raid devices */
c2c087e6 3135 for (d = super->disks; d ; d = d->next) {
d23fe947
DW
3136 if (d->index < 0)
3137 continue;
f796af5d 3138 if (store_imsm_mpb(d->fd, mpb))
c2c087e6
DW
3139 fprintf(stderr, "%s: failed for device %d:%d %s\n",
3140 __func__, d->major, d->minor, strerror(errno));
c2c087e6
DW
3141 if (doclose) {
3142 close(d->fd);
3143 d->fd = -1;
3144 }
3145 }
3146
d23fe947
DW
3147 if (spares)
3148 return write_super_imsm_spares(super, doclose);
3149
e74255d9 3150 return 0;
c2c087e6
DW
3151}
3152
0e600426 3153
9b1fb677 3154static int create_array(struct supertype *st, int dev_idx)
43dad3d6
DW
3155{
3156 size_t len;
3157 struct imsm_update_create_array *u;
3158 struct intel_super *super = st->sb;
9b1fb677 3159 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
54c2c1ea
DW
3160 struct imsm_map *map = get_imsm_map(dev, 0);
3161 struct disk_info *inf;
3162 struct imsm_disk *disk;
3163 int i;
43dad3d6 3164
54c2c1ea
DW
3165 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
3166 sizeof(*inf) * map->num_members;
43dad3d6
DW
3167 u = malloc(len);
3168 if (!u) {
3169 fprintf(stderr, "%s: failed to allocate update buffer\n",
3170 __func__);
3171 return 1;
3172 }
3173
3174 u->type = update_create_array;
9b1fb677 3175 u->dev_idx = dev_idx;
43dad3d6 3176 imsm_copy_dev(&u->dev, dev);
54c2c1ea
DW
3177 inf = get_disk_info(u);
3178 for (i = 0; i < map->num_members; i++) {
9b1fb677
DW
3179 int idx = get_imsm_disk_idx(dev, i);
3180
54c2c1ea
DW
3181 disk = get_imsm_disk(super, idx);
3182 serialcpy(inf[i].serial, disk->serial);
3183 }
43dad3d6
DW
3184 append_metadata_update(st, u, len);
3185
3186 return 0;
3187}
3188
7801ac20 3189static int _add_disk(struct supertype *st)
43dad3d6
DW
3190{
3191 struct intel_super *super = st->sb;
3192 size_t len;
3193 struct imsm_update_add_disk *u;
3194
3195 if (!super->add)
3196 return 0;
3197
3198 len = sizeof(*u);
3199 u = malloc(len);
3200 if (!u) {
3201 fprintf(stderr, "%s: failed to allocate update buffer\n",
3202 __func__);
3203 return 1;
3204 }
3205
3206 u->type = update_add_disk;
3207 append_metadata_update(st, u, len);
3208
3209 return 0;
3210}
3211
c2c087e6
DW
3212static int write_init_super_imsm(struct supertype *st)
3213{
9b1fb677
DW
3214 struct intel_super *super = st->sb;
3215 int current_vol = super->current_vol;
3216
3217 /* we are done with current_vol reset it to point st at the container */
3218 super->current_vol = -1;
3219
8273f55e 3220 if (st->update_tail) {
43dad3d6
DW
3221 /* queue the recently created array / added disk
3222 * as a metadata update */
8273f55e 3223 struct dl *d;
43dad3d6 3224 int rv;
8273f55e 3225
43dad3d6 3226 /* determine if we are creating a volume or adding a disk */
9b1fb677 3227 if (current_vol < 0) {
43dad3d6
DW
3228 /* in the add disk case we are running in mdmon
3229 * context, so don't close fd's
3230 */
7801ac20 3231 return _add_disk(st);
43dad3d6 3232 } else
9b1fb677 3233 rv = create_array(st, current_vol);
8273f55e
DW
3234
3235 for (d = super->disks; d ; d = d->next) {
3236 close(d->fd);
3237 d->fd = -1;
3238 }
3239
43dad3d6 3240 return rv;
8273f55e
DW
3241 } else
3242 return write_super_imsm(st->sb, 1);
cdddbdbc 3243}
0e600426 3244#endif
cdddbdbc 3245
e683ca88 3246static int store_super_imsm(struct supertype *st, int fd)
cdddbdbc 3247{
e683ca88
DW
3248 struct intel_super *super = st->sb;
3249 struct imsm_super *mpb = super ? super->anchor : NULL;
551c80c1 3250
e683ca88 3251 if (!mpb)
ad97895e
DW
3252 return 1;
3253
1799c9e8 3254#ifndef MDASSEMBLE
e683ca88 3255 return store_imsm_mpb(fd, mpb);
1799c9e8
N
3256#else
3257 return 1;
3258#endif
cdddbdbc
DW
3259}
3260
0e600426
N
3261static int imsm_bbm_log_size(struct imsm_super *mpb)
3262{
3263 return __le32_to_cpu(mpb->bbm_log_size);
3264}
3265
3266#ifndef MDASSEMBLE
cdddbdbc
DW
3267static int validate_geometry_imsm_container(struct supertype *st, int level,
3268 int layout, int raiddisks, int chunk,
c2c087e6 3269 unsigned long long size, char *dev,
2c514b71
NB
3270 unsigned long long *freesize,
3271 int verbose)
cdddbdbc 3272{
c2c087e6
DW
3273 int fd;
3274 unsigned long long ldsize;
88c32bb1 3275 const struct imsm_orom *orom;
cdddbdbc 3276
c2c087e6
DW
3277 if (level != LEVEL_CONTAINER)
3278 return 0;
3279 if (!dev)
3280 return 1;
3281
88c32bb1
DW
3282 if (check_env("IMSM_NO_PLATFORM"))
3283 orom = NULL;
3284 else
3285 orom = find_imsm_orom();
3286 if (orom && raiddisks > orom->tds) {
3287 if (verbose)
3288 fprintf(stderr, Name ": %d exceeds maximum number of"
3289 " platform supported disks: %d\n",
3290 raiddisks, orom->tds);
3291 return 0;
3292 }
3293
c2c087e6
DW
3294 fd = open(dev, O_RDONLY|O_EXCL, 0);
3295 if (fd < 0) {
2c514b71
NB
3296 if (verbose)
3297 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
3298 dev, strerror(errno));
c2c087e6
DW
3299 return 0;
3300 }
3301 if (!get_dev_size(fd, dev, &ldsize)) {
3302 close(fd);
3303 return 0;
3304 }
3305 close(fd);
3306
3307 *freesize = avail_size_imsm(st, ldsize >> 9);
3308
3309 return 1;
cdddbdbc
DW
3310}
3311
0dcecb2e
DW
3312static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
3313{
3314 const unsigned long long base_start = e[*idx].start;
3315 unsigned long long end = base_start + e[*idx].size;
3316 int i;
3317
3318 if (base_start == end)
3319 return 0;
3320
3321 *idx = *idx + 1;
3322 for (i = *idx; i < num_extents; i++) {
3323 /* extend overlapping extents */
3324 if (e[i].start >= base_start &&
3325 e[i].start <= end) {
3326 if (e[i].size == 0)
3327 return 0;
3328 if (e[i].start + e[i].size > end)
3329 end = e[i].start + e[i].size;
3330 } else if (e[i].start > end) {
3331 *idx = i;
3332 break;
3333 }
3334 }
3335
3336 return end - base_start;
3337}
3338
3339static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
3340{
3341 /* build a composite disk with all known extents and generate a new
3342 * 'maxsize' given the "all disks in an array must share a common start
3343 * offset" constraint
3344 */
3345 struct extent *e = calloc(sum_extents, sizeof(*e));
3346 struct dl *dl;
3347 int i, j;
3348 int start_extent;
3349 unsigned long long pos;
b9d77223 3350 unsigned long long start = 0;
0dcecb2e
DW
3351 unsigned long long maxsize;
3352 unsigned long reserve;
3353
3354 if (!e)
3355 return ~0ULL; /* error */
3356
3357 /* coalesce and sort all extents. also, check to see if we need to
3358 * reserve space between member arrays
3359 */
3360 j = 0;
3361 for (dl = super->disks; dl; dl = dl->next) {
3362 if (!dl->e)
3363 continue;
3364 for (i = 0; i < dl->extent_cnt; i++)
3365 e[j++] = dl->e[i];
3366 }
3367 qsort(e, sum_extents, sizeof(*e), cmp_extent);
3368
3369 /* merge extents */
3370 i = 0;
3371 j = 0;
3372 while (i < sum_extents) {
3373 e[j].start = e[i].start;
3374 e[j].size = find_size(e, &i, sum_extents);
3375 j++;
3376 if (e[j-1].size == 0)
3377 break;
3378 }
3379
3380 pos = 0;
3381 maxsize = 0;
3382 start_extent = 0;
3383 i = 0;
3384 do {
3385 unsigned long long esize;
3386
3387 esize = e[i].start - pos;
3388 if (esize >= maxsize) {
3389 maxsize = esize;
3390 start = pos;
3391 start_extent = i;
3392 }
3393 pos = e[i].start + e[i].size;
3394 i++;
3395 } while (e[i-1].size);
3396 free(e);
3397
3398 if (start_extent > 0)
3399 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
3400 else
3401 reserve = 0;
3402
3403 if (maxsize < reserve)
3404 return ~0ULL;
3405
3406 super->create_offset = ~((__u32) 0);
3407 if (start + reserve > super->create_offset)
3408 return ~0ULL; /* start overflows create_offset */
3409 super->create_offset = start + reserve;
3410
3411 return maxsize - reserve;
3412}
3413
88c32bb1
DW
3414static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
3415{
3416 if (level < 0 || level == 6 || level == 4)
3417 return 0;
3418
3419 /* if we have an orom prevent invalid raid levels */
3420 if (orom)
3421 switch (level) {
3422 case 0: return imsm_orom_has_raid0(orom);
3423 case 1:
3424 if (raiddisks > 2)
3425 return imsm_orom_has_raid1e(orom);
1c556e92
DW
3426 return imsm_orom_has_raid1(orom) && raiddisks == 2;
3427 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
3428 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
88c32bb1
DW
3429 }
3430 else
3431 return 1; /* not on an Intel RAID platform so anything goes */
3432
3433 return 0;
3434}
3435
35f81cbb 3436#define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
c2c087e6
DW
3437/* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
3438 * FIX ME add ahci details
3439 */
8b353278
DW
3440static int validate_geometry_imsm_volume(struct supertype *st, int level,
3441 int layout, int raiddisks, int chunk,
c2c087e6 3442 unsigned long long size, char *dev,
2c514b71
NB
3443 unsigned long long *freesize,
3444 int verbose)
cdddbdbc 3445{
c2c087e6
DW
3446 struct stat stb;
3447 struct intel_super *super = st->sb;
d10d56fe 3448 struct imsm_super *mpb;
c2c087e6
DW
3449 struct dl *dl;
3450 unsigned long long pos = 0;
3451 unsigned long long maxsize;
3452 struct extent *e;
3453 int i;
cdddbdbc 3454
88c32bb1
DW
3455 /* We must have the container info already read in. */
3456 if (!super)
c2c087e6 3457 return 0;
d10d56fe 3458 mpb = super->anchor;
c2c087e6 3459
88c32bb1 3460 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
1c556e92
DW
3461 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3462 level, raiddisks, raiddisks > 1 ? "s" : "");
c2c087e6
DW
3463 return 0;
3464 }
78757ce8
DW
3465 if (super->orom && level != 1 &&
3466 !imsm_orom_has_chunk(super->orom, chunk)) {
35f81cbb 3467 pr_vrb(": platform does not support a chunk size of: %d\n", chunk);
c2c087e6 3468 return 0;
88c32bb1
DW
3469 }
3470 if (layout != imsm_level_to_layout(level)) {
3471 if (level == 5)
35f81cbb 3472 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
88c32bb1 3473 else if (level == 10)
35f81cbb 3474 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
88c32bb1 3475 else
35f81cbb 3476 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
88c32bb1 3477 layout, level);
c2c087e6 3478 return 0;
88c32bb1 3479 }
c2c087e6
DW
3480
3481 if (!dev) {
3482 /* General test: make sure there is space for
2da8544a
DW
3483 * 'raiddisks' device extents of size 'size' at a given
3484 * offset
c2c087e6 3485 */
e46273eb 3486 unsigned long long minsize = size;
2da8544a 3487 unsigned long long start_offset = ~0ULL;
c2c087e6
DW
3488 int dcnt = 0;
3489 if (minsize == 0)
3490 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
3491 for (dl = super->disks; dl ; dl = dl->next) {
3492 int found = 0;
3493
bf5a934a 3494 pos = 0;
c2c087e6
DW
3495 i = 0;
3496 e = get_extents(super, dl);
3497 if (!e) continue;
3498 do {
3499 unsigned long long esize;
3500 esize = e[i].start - pos;
3501 if (esize >= minsize)
3502 found = 1;
2da8544a
DW
3503 if (found && start_offset == ~0ULL) {
3504 start_offset = pos;
3505 break;
3506 } else if (found && pos != start_offset) {
3507 found = 0;
3508 break;
3509 }
c2c087e6
DW
3510 pos = e[i].start + e[i].size;
3511 i++;
3512 } while (e[i-1].size);
3513 if (found)
3514 dcnt++;
3515 free(e);
3516 }
3517 if (dcnt < raiddisks) {
2c514b71
NB
3518 if (verbose)
3519 fprintf(stderr, Name ": imsm: Not enough "
3520 "devices with space for this array "
3521 "(%d < %d)\n",
3522 dcnt, raiddisks);
c2c087e6
DW
3523 return 0;
3524 }
3525 return 1;
3526 }
0dcecb2e 3527
c2c087e6
DW
3528 /* This device must be a member of the set */
3529 if (stat(dev, &stb) < 0)
3530 return 0;
3531 if ((S_IFMT & stb.st_mode) != S_IFBLK)
3532 return 0;
3533 for (dl = super->disks ; dl ; dl = dl->next) {
3534 if (dl->major == major(stb.st_rdev) &&
3535 dl->minor == minor(stb.st_rdev))
3536 break;
3537 }
3538 if (!dl) {
2c514b71
NB
3539 if (verbose)
3540 fprintf(stderr, Name ": %s is not in the "
3541 "same imsm set\n", dev);
c2c087e6 3542 return 0;
a20d2ba5
DW
3543 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
3544 /* If a volume is present then the current creation attempt
3545 * cannot incorporate new spares because the orom may not
3546 * understand this configuration (all member disks must be
3547 * members of each array in the container).
3548 */
3549 fprintf(stderr, Name ": %s is a spare and a volume"
3550 " is already defined for this container\n", dev);
3551 fprintf(stderr, Name ": The option-rom requires all member"
3552 " disks to be a member of all volumes\n");
3553 return 0;
c2c087e6 3554 }
0dcecb2e
DW
3555
3556 /* retrieve the largest free space block */
c2c087e6
DW
3557 e = get_extents(super, dl);
3558 maxsize = 0;
3559 i = 0;
0dcecb2e
DW
3560 if (e) {
3561 do {
3562 unsigned long long esize;
3563
3564 esize = e[i].start - pos;
3565 if (esize >= maxsize)
3566 maxsize = esize;
3567 pos = e[i].start + e[i].size;
3568 i++;
3569 } while (e[i-1].size);
3570 dl->e = e;
3571 dl->extent_cnt = i;
3572 } else {
3573 if (verbose)
3574 fprintf(stderr, Name ": unable to determine free space for: %s\n",
3575 dev);
3576 return 0;
3577 }
3578 if (maxsize < size) {
3579 if (verbose)
3580 fprintf(stderr, Name ": %s not enough space (%llu < %llu)\n",
3581 dev, maxsize, size);
3582 return 0;
3583 }
3584
3585 /* count total number of extents for merge */
3586 i = 0;
3587 for (dl = super->disks; dl; dl = dl->next)
3588 if (dl->e)
3589 i += dl->extent_cnt;
3590
3591 maxsize = merge_extents(super, i);
3592 if (maxsize < size) {
3593 if (verbose)
3594 fprintf(stderr, Name ": not enough space after merge (%llu < %llu)\n",
3595 maxsize, size);
3596 return 0;
3597 } else if (maxsize == ~0ULL) {
3598 if (verbose)
3599 fprintf(stderr, Name ": failed to merge %d extents\n", i);
3600 return 0;
3601 }
3602
c2c087e6
DW
3603 *freesize = maxsize;
3604
3605 return 1;
cdddbdbc
DW
3606}
3607
efb30e7f
DW
3608static int reserve_space(struct supertype *st, int raiddisks,
3609 unsigned long long size, int chunk,
3610 unsigned long long *freesize)
3611{
3612 struct intel_super *super = st->sb;
3613 struct imsm_super *mpb = super->anchor;
3614 struct dl *dl;
3615 int i;
3616 int extent_cnt;
3617 struct extent *e;
3618 unsigned long long maxsize;
3619 unsigned long long minsize;
3620 int cnt;
3621 int used;
3622
3623 /* find the largest common start free region of the possible disks */
3624 used = 0;
3625 extent_cnt = 0;
3626 cnt = 0;
3627 for (dl = super->disks; dl; dl = dl->next) {
3628 dl->raiddisk = -1;
3629
3630 if (dl->index >= 0)
3631 used++;
3632
3633 /* don't activate new spares if we are orom constrained
3634 * and there is already a volume active in the container
3635 */
3636 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
3637 continue;
3638
3639 e = get_extents(super, dl);
3640 if (!e)
3641 continue;
3642 for (i = 1; e[i-1].size; i++)
3643 ;
3644 dl->e = e;
3645 dl->extent_cnt = i;
3646 extent_cnt += i;
3647 cnt++;
3648 }
3649
3650 maxsize = merge_extents(super, extent_cnt);
3651 minsize = size;
3652 if (size == 0)
3653 minsize = chunk;
3654
3655 if (cnt < raiddisks ||
3656 (super->orom && used && used != raiddisks) ||
3657 maxsize < minsize) {
3658 fprintf(stderr, Name ": not enough devices with space to create array.\n");
3659 return 0; /* No enough free spaces large enough */
3660 }
3661
3662 if (size == 0) {
3663 size = maxsize;
3664 if (chunk) {
3665 size /= chunk;
3666 size *= chunk;
3667 }
3668 }
3669
3670 cnt = 0;
3671 for (dl = super->disks; dl; dl = dl->next)
3672 if (dl->e)
3673 dl->raiddisk = cnt++;
3674
3675 *freesize = size;
3676
3677 return 1;
3678}
3679
bf5a934a
DW
3680static int validate_geometry_imsm(struct supertype *st, int level, int layout,
3681 int raiddisks, int chunk, unsigned long long size,
3682 char *dev, unsigned long long *freesize,
3683 int verbose)
3684{
3685 int fd, cfd;
3686 struct mdinfo *sra;
20cbe8d2 3687 int is_member = 0;
bf5a934a
DW
3688
3689 /* if given unused devices create a container
3690 * if given given devices in a container create a member volume
3691 */
3692 if (level == LEVEL_CONTAINER) {
3693 /* Must be a fresh device to add to a container */
3694 return validate_geometry_imsm_container(st, level, layout,
3695 raiddisks, chunk, size,
3696 dev, freesize,
3697 verbose);
3698 }
3699
8592f29d
N
3700 if (!dev) {
3701 if (st->sb && freesize) {
efb30e7f
DW
3702 /* we are being asked to automatically layout a
3703 * new volume based on the current contents of
3704 * the container. If the the parameters can be
3705 * satisfied reserve_space will record the disks,
3706 * start offset, and size of the volume to be
3707 * created. add_to_super and getinfo_super
3708 * detect when autolayout is in progress.
3709 */
3710 return reserve_space(st, raiddisks, size, chunk, freesize);
8592f29d
N
3711 }
3712 return 1;
3713 }
bf5a934a
DW
3714 if (st->sb) {
3715 /* creating in a given container */
3716 return validate_geometry_imsm_volume(st, level, layout,
3717 raiddisks, chunk, size,
3718 dev, freesize, verbose);
3719 }
3720
3721 /* limit creation to the following levels */
3722 if (!dev)
3723 switch (level) {
3724 case 0:
3725 case 1:
3726 case 10:
3727 case 5:
b42f577a 3728 return 0;
bf5a934a 3729 default:
b42f577a
N
3730 if (verbose)
3731 fprintf(stderr, Name
3732 ": IMSM only supports levels 0,1,5,10\n");
bf5a934a
DW
3733 return 1;
3734 }
3735
3736 /* This device needs to be a device in an 'imsm' container */
3737 fd = open(dev, O_RDONLY|O_EXCL, 0);
3738 if (fd >= 0) {
3739 if (verbose)
3740 fprintf(stderr,
3741 Name ": Cannot create this array on device %s\n",
3742 dev);
3743 close(fd);
3744 return 0;
3745 }
3746 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
3747 if (verbose)
3748 fprintf(stderr, Name ": Cannot open %s: %s\n",
3749 dev, strerror(errno));
3750 return 0;
3751 }
3752 /* Well, it is in use by someone, maybe an 'imsm' container. */
3753 cfd = open_container(fd);
20cbe8d2 3754 close(fd);
bf5a934a 3755 if (cfd < 0) {
bf5a934a
DW
3756 if (verbose)
3757 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
3758 dev);
3759 return 0;
3760 }
3761 sra = sysfs_read(cfd, 0, GET_VERSION);
bf5a934a 3762 if (sra && sra->array.major_version == -1 &&
20cbe8d2
AW
3763 strcmp(sra->text_version, "imsm") == 0)
3764 is_member = 1;
3765 sysfs_free(sra);
3766 if (is_member) {
bf5a934a
DW
3767 /* This is a member of a imsm container. Load the container
3768 * and try to create a volume
3769 */
3770 struct intel_super *super;
3771
3772 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, 1) == 0) {
3773 st->sb = super;
3774 st->container_dev = fd2devnum(cfd);
3775 close(cfd);
3776 return validate_geometry_imsm_volume(st, level, layout,
3777 raiddisks, chunk,
3778 size, dev,
3779 freesize, verbose);
3780 }
20cbe8d2 3781 }
bf5a934a 3782
20cbe8d2
AW
3783 if (verbose)
3784 fprintf(stderr, Name ": failed container membership check\n");
3785
3786 close(cfd);
3787 return 0;
bf5a934a 3788}
0e600426 3789#endif /* MDASSEMBLE */
bf5a934a 3790
cdddbdbc
DW
3791static struct mdinfo *container_content_imsm(struct supertype *st)
3792{
4f5bc454
DW
3793 /* Given a container loaded by load_super_imsm_all,
3794 * extract information about all the arrays into
3795 * an mdinfo tree.
3796 *
3797 * For each imsm_dev create an mdinfo, fill it in,
3798 * then look for matching devices in super->disks
3799 * and create appropriate device mdinfo.
3800 */
3801 struct intel_super *super = st->sb;
949c47a0 3802 struct imsm_super *mpb = super->anchor;
4f5bc454
DW
3803 struct mdinfo *rest = NULL;
3804 int i;
cdddbdbc 3805
604b746f
JD
3806 /* do not assemble arrays that might have bad blocks */
3807 if (imsm_bbm_log_size(super->anchor)) {
3808 fprintf(stderr, Name ": BBM log found in metadata. "
3809 "Cannot activate array(s).\n");
3810 return NULL;
3811 }
3812
4f5bc454 3813 for (i = 0; i < mpb->num_raid_devs; i++) {
949c47a0 3814 struct imsm_dev *dev = get_imsm_dev(super, i);
a965f303 3815 struct imsm_map *map = get_imsm_map(dev, 0);
4f5bc454 3816 struct mdinfo *this;
4f5bc454
DW
3817 int slot;
3818
1ce0101c
DW
3819 /* do not publish arrays that are in the middle of an
3820 * unsupported migration
3821 */
3822 if (dev->vol.migr_state &&
3823 (migr_type(dev) == MIGR_GEN_MIGR ||
3824 migr_type(dev) == MIGR_STATE_CHANGE)) {
3825 fprintf(stderr, Name ": cannot assemble volume '%.16s':"
3826 " unsupported migration in progress\n",
3827 dev->volume);
3828 continue;
3829 }
3830
4f5bc454 3831 this = malloc(sizeof(*this));
0fbd635c
AW
3832 if (!this) {
3833 fprintf(stderr, Name ": failed to allocate %lu bytes\n",
3834 sizeof(*this));
3835 break;
3836 }
4f5bc454
DW
3837 memset(this, 0, sizeof(*this));
3838 this->next = rest;
4f5bc454 3839
301406c9
DW
3840 super->current_vol = i;
3841 getinfo_super_imsm_volume(st, this);
4f5bc454 3842 for (slot = 0 ; slot < map->num_members; slot++) {
4f5bc454
DW
3843 struct mdinfo *info_d;
3844 struct dl *d;
3845 int idx;
9a1608e5 3846 int skip;
7eef0453 3847 __u32 ord;
4f5bc454 3848
9a1608e5 3849 skip = 0;
ff077194 3850 idx = get_imsm_disk_idx(dev, slot);
7eef0453 3851 ord = get_imsm_ord_tbl_ent(dev, slot);
4f5bc454
DW
3852 for (d = super->disks; d ; d = d->next)
3853 if (d->index == idx)
0fbd635c 3854 break;
4f5bc454
DW
3855
3856 if (d == NULL)
9a1608e5 3857 skip = 1;
25ed7e59 3858 if (d && is_failed(&d->disk))
9a1608e5 3859 skip = 1;
7eef0453
DW
3860 if (ord & IMSM_ORD_REBUILD)
3861 skip = 1;
9a1608e5
DW
3862
3863 /*
3864 * if we skip some disks the array will be assmebled degraded;
3865 * reset resync start to avoid a dirty-degraded situation
3866 *
3867 * FIXME handle dirty degraded
3868 */
3869 if (skip && !dev->vol.dirty)
3870 this->resync_start = ~0ULL;
3871 if (skip)
3872 continue;
4f5bc454
DW
3873
3874 info_d = malloc(sizeof(*info_d));
9a1608e5
DW
3875 if (!info_d) {
3876 fprintf(stderr, Name ": failed to allocate disk"
1ce0101c 3877 " for volume %.16s\n", dev->volume);
9a1608e5
DW
3878 free(this);
3879 this = rest;
3880 break;
3881 }
4f5bc454
DW
3882 memset(info_d, 0, sizeof(*info_d));
3883 info_d->next = this->devs;
3884 this->devs = info_d;
3885
4f5bc454
DW
3886 info_d->disk.number = d->index;
3887 info_d->disk.major = d->major;
3888 info_d->disk.minor = d->minor;
3889 info_d->disk.raid_disk = slot;
4f5bc454
DW
3890
3891 this->array.working_disks++;
3892
3893 info_d->events = __le32_to_cpu(mpb->generation_num);
3894 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
3895 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
4f5bc454 3896 }
9a1608e5 3897 rest = this;
4f5bc454
DW
3898 }
3899
3900 return rest;
cdddbdbc
DW
3901}
3902
845dea95 3903
0e600426 3904#ifndef MDASSEMBLE
cba0191b
NB
3905static int imsm_open_new(struct supertype *c, struct active_array *a,
3906 char *inst)
845dea95 3907{
0372d5a2 3908 struct intel_super *super = c->sb;
949c47a0 3909 struct imsm_super *mpb = super->anchor;
0372d5a2 3910
949c47a0 3911 if (atoi(inst) >= mpb->num_raid_devs) {
0372d5a2
DW
3912 fprintf(stderr, "%s: subarry index %d, out of range\n",
3913 __func__, atoi(inst));
3914 return -ENODEV;
3915 }
3916
4e6e574a 3917 dprintf("imsm: open_new %s\n", inst);
cba0191b 3918 a->info.container_member = atoi(inst);
845dea95
NB
3919 return 0;
3920}
3921
fb49eef2 3922static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed)
c2a1e7da 3923{
a965f303 3924 struct imsm_map *map = get_imsm_map(dev, 0);
c2a1e7da
DW
3925
3926 if (!failed)
3393c6af
DW
3927 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
3928 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
c2a1e7da
DW
3929
3930 switch (get_imsm_raid_level(map)) {
3931 case 0:
3932 return IMSM_T_STATE_FAILED;
3933 break;
3934 case 1:
3935 if (failed < map->num_members)
3936 return IMSM_T_STATE_DEGRADED;
3937 else
3938 return IMSM_T_STATE_FAILED;
3939 break;
3940 case 10:
3941 {
3942 /**
c92a2527
DW
3943 * check to see if any mirrors have failed, otherwise we
3944 * are degraded. Even numbered slots are mirrored on
3945 * slot+1
c2a1e7da 3946 */
c2a1e7da 3947 int i;
d9b420a5
N
3948 /* gcc -Os complains that this is unused */
3949 int insync = insync;
c2a1e7da
DW
3950
3951 for (i = 0; i < map->num_members; i++) {
c92a2527
DW
3952 __u32 ord = get_imsm_ord_tbl_ent(dev, i);
3953 int idx = ord_to_idx(ord);
3954 struct imsm_disk *disk;
c2a1e7da 3955
c92a2527
DW
3956 /* reset the potential in-sync count on even-numbered
3957 * slots. num_copies is always 2 for imsm raid10
3958 */
3959 if ((i & 1) == 0)
3960 insync = 2;
c2a1e7da 3961
c92a2527 3962 disk = get_imsm_disk(super, idx);
25ed7e59 3963 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
c92a2527 3964 insync--;
c2a1e7da 3965
c92a2527
DW
3966 /* no in-sync disks left in this mirror the
3967 * array has failed
3968 */
3969 if (insync == 0)
3970 return IMSM_T_STATE_FAILED;
c2a1e7da
DW
3971 }
3972
3973 return IMSM_T_STATE_DEGRADED;
3974 }
3975 case 5:
3976 if (failed < 2)
3977 return IMSM_T_STATE_DEGRADED;
3978 else
3979 return IMSM_T_STATE_FAILED;
3980 break;
3981 default:
3982 break;
3983 }
3984
3985 return map->map_state;
3986}
3987
ff077194 3988static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev)
c2a1e7da
DW
3989{
3990 int i;
3991 int failed = 0;
3992 struct imsm_disk *disk;
ff077194 3993 struct imsm_map *map = get_imsm_map(dev, 0);
0556e1a2
DW
3994 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
3995 __u32 ord;
3996 int idx;
c2a1e7da 3997
0556e1a2
DW
3998 /* at the beginning of migration we set IMSM_ORD_REBUILD on
3999 * disks that are being rebuilt. New failures are recorded to
4000 * map[0]. So we look through all the disks we started with and
4001 * see if any failures are still present, or if any new ones
4002 * have arrived
4003 *
4004 * FIXME add support for online capacity expansion and
4005 * raid-level-migration
4006 */
4007 for (i = 0; i < prev->num_members; i++) {
4008 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
4009 ord |= __le32_to_cpu(map->disk_ord_tbl[i]);
4010 idx = ord_to_idx(ord);
c2a1e7da 4011
949c47a0 4012 disk = get_imsm_disk(super, idx);
25ed7e59 4013 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
fcb84475 4014 failed++;
c2a1e7da
DW
4015 }
4016
4017 return failed;
845dea95
NB
4018}
4019
0c046afd
DW
4020static int is_resyncing(struct imsm_dev *dev)
4021{
4022 struct imsm_map *migr_map;
4023
4024 if (!dev->vol.migr_state)
4025 return 0;
4026
1484e727
DW
4027 if (migr_type(dev) == MIGR_INIT ||
4028 migr_type(dev) == MIGR_REPAIR)
0c046afd
DW
4029 return 1;
4030
4031 migr_map = get_imsm_map(dev, 1);
4032
4033 if (migr_map->map_state == IMSM_T_STATE_NORMAL)
4034 return 1;
4035 else
4036 return 0;
4037}
4038
4039static int is_rebuilding(struct imsm_dev *dev)
4040{
4041 struct imsm_map *migr_map;
4042
4043 if (!dev->vol.migr_state)
4044 return 0;
4045
1484e727 4046 if (migr_type(dev) != MIGR_REBUILD)
0c046afd
DW
4047 return 0;
4048
4049 migr_map = get_imsm_map(dev, 1);
4050
4051 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
4052 return 1;
4053 else
4054 return 0;
4055}
4056
0556e1a2
DW
4057/* return true if we recorded new information */
4058static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
47ee5a45 4059{
0556e1a2
DW
4060 __u32 ord;
4061 int slot;
4062 struct imsm_map *map;
4063
4064 /* new failures are always set in map[0] */
4065 map = get_imsm_map(dev, 0);
4066
4067 slot = get_imsm_disk_slot(map, idx);
4068 if (slot < 0)
4069 return 0;
4070
4071 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
25ed7e59 4072 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
0556e1a2
DW
4073 return 0;
4074
f2f27e63 4075 disk->status |= FAILED_DISK;
cf53434e 4076 disk->status &= ~CONFIGURED_DISK;
0556e1a2 4077 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
4291d691 4078 if (~map->failed_disk_num == 0)
0556e1a2
DW
4079 map->failed_disk_num = slot;
4080 return 1;
4081}
4082
4083static void mark_missing(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
4084{
4085 mark_failure(dev, disk, idx);
4086
4087 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
4088 return;
4089
47ee5a45
DW
4090 disk->scsi_id = __cpu_to_le32(~(__u32)0);
4091 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
4092}
4093
0c046afd
DW
4094/* Handle dirty -> clean transititions and resync. Degraded and rebuild
4095 * states are handled in imsm_set_disk() with one exception, when a
4096 * resync is stopped due to a new failure this routine will set the
4097 * 'degraded' state for the array.
4098 */
01f157d7 4099static int imsm_set_array_state(struct active_array *a, int consistent)
a862209d
DW
4100{
4101 int inst = a->info.container_member;
4102 struct intel_super *super = a->container->sb;
949c47a0 4103 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 4104 struct imsm_map *map = get_imsm_map(dev, 0);
0c046afd
DW
4105 int failed = imsm_count_failed(super, dev);
4106 __u8 map_state = imsm_check_degraded(super, dev, failed);
a862209d 4107
47ee5a45
DW
4108 /* before we activate this array handle any missing disks */
4109 if (consistent == 2 && super->missing) {
4110 struct dl *dl;
4111
4112 dprintf("imsm: mark missing\n");
4113 end_migration(dev, map_state);
4114 for (dl = super->missing; dl; dl = dl->next)
0556e1a2 4115 mark_missing(dev, &dl->disk, dl->index);
47ee5a45
DW
4116 super->updates_pending++;
4117 }
4118
0c046afd 4119 if (consistent == 2 &&
593add1b 4120 (!is_resync_complete(a) ||
0c046afd
DW
4121 map_state != IMSM_T_STATE_NORMAL ||
4122 dev->vol.migr_state))
01f157d7 4123 consistent = 0;
272906ef 4124
593add1b 4125 if (is_resync_complete(a)) {
0c046afd 4126 /* complete intialization / resync,
0556e1a2
DW
4127 * recovery and interrupted recovery is completed in
4128 * ->set_disk
0c046afd
DW
4129 */
4130 if (is_resyncing(dev)) {
4131 dprintf("imsm: mark resync done\n");
f8f603f1 4132 end_migration(dev, map_state);
115c3803 4133 super->updates_pending++;
115c3803 4134 }
0c046afd
DW
4135 } else if (!is_resyncing(dev) && !failed) {
4136 /* mark the start of the init process if nothing is failed */
4137 dprintf("imsm: mark resync start (%llu)\n", a->resync_start);
1484e727 4138 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
e3bba0e0 4139 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_INIT);
1484e727
DW
4140 else
4141 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
3393c6af 4142 super->updates_pending++;
115c3803 4143 }
a862209d 4144
da188789 4145 /* FIXME check if we can update curr_migr_unit from resync_start */
f8f603f1 4146
3393c6af 4147 /* mark dirty / clean */
0c046afd 4148 if (dev->vol.dirty != !consistent) {
3393c6af 4149 dprintf("imsm: mark '%s' (%llu)\n",
0c046afd
DW
4150 consistent ? "clean" : "dirty", a->resync_start);
4151 if (consistent)
4152 dev->vol.dirty = 0;
4153 else
4154 dev->vol.dirty = 1;
a862209d
DW
4155 super->updates_pending++;
4156 }
01f157d7 4157 return consistent;
a862209d
DW
4158}
4159
8d45d196 4160static void imsm_set_disk(struct active_array *a, int n, int state)
845dea95 4161{
8d45d196
DW
4162 int inst = a->info.container_member;
4163 struct intel_super *super = a->container->sb;
949c47a0 4164 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 4165 struct imsm_map *map = get_imsm_map(dev, 0);
8d45d196 4166 struct imsm_disk *disk;
0c046afd 4167 int failed;
b10b37b8 4168 __u32 ord;
0c046afd 4169 __u8 map_state;
8d45d196
DW
4170
4171 if (n > map->num_members)
4172 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
4173 n, map->num_members - 1);
4174
4175 if (n < 0)
4176 return;
4177
4e6e574a 4178 dprintf("imsm: set_disk %d:%x\n", n, state);
8d45d196 4179
b10b37b8
DW
4180 ord = get_imsm_ord_tbl_ent(dev, n);
4181 disk = get_imsm_disk(super, ord_to_idx(ord));
8d45d196 4182
5802a811 4183 /* check for new failures */
0556e1a2
DW
4184 if (state & DS_FAULTY) {
4185 if (mark_failure(dev, disk, ord_to_idx(ord)))
4186 super->updates_pending++;
8d45d196 4187 }
47ee5a45 4188
19859edc 4189 /* check if in_sync */
0556e1a2 4190 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
b10b37b8
DW
4191 struct imsm_map *migr_map = get_imsm_map(dev, 1);
4192
4193 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
19859edc
DW
4194 super->updates_pending++;
4195 }
8d45d196 4196
0c046afd
DW
4197 failed = imsm_count_failed(super, dev);
4198 map_state = imsm_check_degraded(super, dev, failed);
5802a811 4199
0c046afd
DW
4200 /* check if recovery complete, newly degraded, or failed */
4201 if (map_state == IMSM_T_STATE_NORMAL && is_rebuilding(dev)) {
f8f603f1 4202 end_migration(dev, map_state);
0556e1a2
DW
4203 map = get_imsm_map(dev, 0);
4204 map->failed_disk_num = ~0;
0c046afd
DW
4205 super->updates_pending++;
4206 } else if (map_state == IMSM_T_STATE_DEGRADED &&
4207 map->map_state != map_state &&
4208 !dev->vol.migr_state) {
4209 dprintf("imsm: mark degraded\n");
4210 map->map_state = map_state;
4211 super->updates_pending++;
4212 } else if (map_state == IMSM_T_STATE_FAILED &&
4213 map->map_state != map_state) {
4214 dprintf("imsm: mark failed\n");
f8f603f1 4215 end_migration(dev, map_state);
0c046afd 4216 super->updates_pending++;
5802a811 4217 }
845dea95
NB
4218}
4219
f796af5d 4220static int store_imsm_mpb(int fd, struct imsm_super *mpb)
c2a1e7da 4221{
f796af5d 4222 void *buf = mpb;
c2a1e7da
DW
4223 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
4224 unsigned long long dsize;
4225 unsigned long long sectors;
4226
4227 get_dev_size(fd, NULL, &dsize);
4228
272f648f
DW
4229 if (mpb_size > 512) {
4230 /* -1 to account for anchor */
4231 sectors = mpb_sectors(mpb) - 1;
c2a1e7da 4232
272f648f
DW
4233 /* write the extended mpb to the sectors preceeding the anchor */
4234 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
4235 return 1;
c2a1e7da 4236
f796af5d 4237 if (write(fd, buf + 512, 512 * sectors) != 512 * sectors)
272f648f
DW
4238 return 1;
4239 }
c2a1e7da 4240
272f648f
DW
4241 /* first block is stored on second to last sector of the disk */
4242 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
c2a1e7da
DW
4243 return 1;
4244
f796af5d 4245 if (write(fd, buf, 512) != 512)
c2a1e7da
DW
4246 return 1;
4247
c2a1e7da
DW
4248 return 0;
4249}
4250
2e735d19 4251static void imsm_sync_metadata(struct supertype *container)
845dea95 4252{
2e735d19 4253 struct intel_super *super = container->sb;
c2a1e7da
DW
4254
4255 if (!super->updates_pending)
4256 return;
4257
c2c087e6 4258 write_super_imsm(super, 0);
c2a1e7da
DW
4259
4260 super->updates_pending = 0;
845dea95
NB
4261}
4262
272906ef
DW
4263static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
4264{
4265 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
ff077194 4266 int i = get_imsm_disk_idx(dev, idx);
272906ef
DW
4267 struct dl *dl;
4268
4269 for (dl = super->disks; dl; dl = dl->next)
4270 if (dl->index == i)
4271 break;
4272
25ed7e59 4273 if (dl && is_failed(&dl->disk))
272906ef
DW
4274 dl = NULL;
4275
4276 if (dl)
4277 dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor);
4278
4279 return dl;
4280}
4281
a20d2ba5
DW
4282static struct dl *imsm_add_spare(struct intel_super *super, int slot,
4283 struct active_array *a, int activate_new)
272906ef
DW
4284{
4285 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
e553d2a4 4286 int idx = get_imsm_disk_idx(dev, slot);
a20d2ba5
DW
4287 struct imsm_super *mpb = super->anchor;
4288 struct imsm_map *map;
272906ef
DW
4289 unsigned long long pos;
4290 struct mdinfo *d;
4291 struct extent *ex;
a20d2ba5 4292 int i, j;
272906ef
DW
4293 int found;
4294 __u32 array_start;
329c8278 4295 __u32 array_end;
272906ef
DW
4296 struct dl *dl;
4297
4298 for (dl = super->disks; dl; dl = dl->next) {
4299 /* If in this array, skip */
4300 for (d = a->info.devs ; d ; d = d->next)
e553d2a4
DW
4301 if (d->state_fd >= 0 &&
4302 d->disk.major == dl->major &&
272906ef
DW
4303 d->disk.minor == dl->minor) {
4304 dprintf("%x:%x already in array\n", dl->major, dl->minor);
4305 break;
4306 }
4307 if (d)
4308 continue;
4309
e553d2a4 4310 /* skip in use or failed drives */
25ed7e59 4311 if (is_failed(&dl->disk) || idx == dl->index ||
df474657
DW
4312 dl->index == -2) {
4313 dprintf("%x:%x status (failed: %d index: %d)\n",
25ed7e59 4314 dl->major, dl->minor, is_failed(&dl->disk), idx);
9a1608e5
DW
4315 continue;
4316 }
4317
a20d2ba5
DW
4318 /* skip pure spares when we are looking for partially
4319 * assimilated drives
4320 */
4321 if (dl->index == -1 && !activate_new)
4322 continue;
4323
272906ef 4324 /* Does this unused device have the requisite free space?
a20d2ba5 4325 * It needs to be able to cover all member volumes
272906ef
DW
4326 */
4327 ex = get_extents(super, dl);
4328 if (!ex) {
4329 dprintf("cannot get extents\n");
4330 continue;
4331 }
a20d2ba5
DW
4332 for (i = 0; i < mpb->num_raid_devs; i++) {
4333 dev = get_imsm_dev(super, i);
4334 map = get_imsm_map(dev, 0);
272906ef 4335
a20d2ba5
DW
4336 /* check if this disk is already a member of
4337 * this array
272906ef 4338 */
620b1713 4339 if (get_imsm_disk_slot(map, dl->index) >= 0)
a20d2ba5
DW
4340 continue;
4341
4342 found = 0;
4343 j = 0;
4344 pos = 0;
4345 array_start = __le32_to_cpu(map->pba_of_lba0);
329c8278
DW
4346 array_end = array_start +
4347 __le32_to_cpu(map->blocks_per_member) - 1;
a20d2ba5
DW
4348
4349 do {
4350 /* check that we can start at pba_of_lba0 with
4351 * blocks_per_member of space
4352 */
329c8278 4353 if (array_start >= pos && array_end < ex[j].start) {
a20d2ba5
DW
4354 found = 1;
4355 break;
4356 }
4357 pos = ex[j].start + ex[j].size;
4358 j++;
4359 } while (ex[j-1].size);
4360
4361 if (!found)
272906ef 4362 break;
a20d2ba5 4363 }
272906ef
DW
4364
4365 free(ex);
a20d2ba5 4366 if (i < mpb->num_raid_devs) {
329c8278
DW
4367 dprintf("%x:%x does not have %u to %u available\n",
4368 dl->major, dl->minor, array_start, array_end);
272906ef
DW
4369 /* No room */
4370 continue;
a20d2ba5
DW
4371 }
4372 return dl;
272906ef
DW
4373 }
4374
4375 return dl;
4376}
4377
88758e9d
DW
4378static struct mdinfo *imsm_activate_spare(struct active_array *a,
4379 struct metadata_update **updates)
4380{
4381 /**
d23fe947
DW
4382 * Find a device with unused free space and use it to replace a
4383 * failed/vacant region in an array. We replace failed regions one a
4384 * array at a time. The result is that a new spare disk will be added
4385 * to the first failed array and after the monitor has finished
4386 * propagating failures the remainder will be consumed.
88758e9d 4387 *
d23fe947
DW
4388 * FIXME add a capability for mdmon to request spares from another
4389 * container.
88758e9d
DW
4390 */
4391
4392 struct intel_super *super = a->container->sb;
88758e9d 4393 int inst = a->info.container_member;
949c47a0 4394 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 4395 struct imsm_map *map = get_imsm_map(dev, 0);
88758e9d
DW
4396 int failed = a->info.array.raid_disks;
4397 struct mdinfo *rv = NULL;
4398 struct mdinfo *d;
4399 struct mdinfo *di;
4400 struct metadata_update *mu;
4401 struct dl *dl;
4402 struct imsm_update_activate_spare *u;
4403 int num_spares = 0;
4404 int i;
4405
4406 for (d = a->info.devs ; d ; d = d->next) {
4407 if ((d->curr_state & DS_FAULTY) &&
4408 d->state_fd >= 0)
4409 /* wait for Removal to happen */
4410 return NULL;
4411 if (d->state_fd >= 0)
4412 failed--;
4413 }
4414
4415 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
4416 inst, failed, a->info.array.raid_disks, a->info.array.level);
fb49eef2 4417 if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED)
88758e9d
DW
4418 return NULL;
4419
4420 /* For each slot, if it is not working, find a spare */
88758e9d
DW
4421 for (i = 0; i < a->info.array.raid_disks; i++) {
4422 for (d = a->info.devs ; d ; d = d->next)
4423 if (d->disk.raid_disk == i)
4424 break;
4425 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
4426 if (d && (d->state_fd >= 0))
4427 continue;
4428
272906ef 4429 /*
a20d2ba5
DW
4430 * OK, this device needs recovery. Try to re-add the
4431 * previous occupant of this slot, if this fails see if
4432 * we can continue the assimilation of a spare that was
4433 * partially assimilated, finally try to activate a new
4434 * spare.
272906ef
DW
4435 */
4436 dl = imsm_readd(super, i, a);
4437 if (!dl)
a20d2ba5
DW
4438 dl = imsm_add_spare(super, i, a, 0);
4439 if (!dl)
4440 dl = imsm_add_spare(super, i, a, 1);
272906ef
DW
4441 if (!dl)
4442 continue;
4443
4444 /* found a usable disk with enough space */
4445 di = malloc(sizeof(*di));
79244939
DW
4446 if (!di)
4447 continue;
272906ef
DW
4448 memset(di, 0, sizeof(*di));
4449
4450 /* dl->index will be -1 in the case we are activating a
4451 * pristine spare. imsm_process_update() will create a
4452 * new index in this case. Once a disk is found to be
4453 * failed in all member arrays it is kicked from the
4454 * metadata
4455 */
4456 di->disk.number = dl->index;
d23fe947 4457
272906ef
DW
4458 /* (ab)use di->devs to store a pointer to the device
4459 * we chose
4460 */
4461 di->devs = (struct mdinfo *) dl;
4462
4463 di->disk.raid_disk = i;
4464 di->disk.major = dl->major;
4465 di->disk.minor = dl->minor;
4466 di->disk.state = 0;
4467 di->data_offset = __le32_to_cpu(map->pba_of_lba0);
4468 di->component_size = a->info.component_size;
4469 di->container_member = inst;
148acb7b 4470 super->random = random32();
272906ef
DW
4471 di->next = rv;
4472 rv = di;
4473 num_spares++;
4474 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
4475 i, di->data_offset);
88758e9d 4476
272906ef 4477 break;
88758e9d
DW
4478 }
4479
4480 if (!rv)
4481 /* No spares found */
4482 return rv;
4483 /* Now 'rv' has a list of devices to return.
4484 * Create a metadata_update record to update the
4485 * disk_ord_tbl for the array
4486 */
4487 mu = malloc(sizeof(*mu));
79244939
DW
4488 if (mu) {
4489 mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
4490 if (mu->buf == NULL) {
4491 free(mu);
4492 mu = NULL;
4493 }
4494 }
4495 if (!mu) {
4496 while (rv) {
4497 struct mdinfo *n = rv->next;
4498
4499 free(rv);
4500 rv = n;
4501 }
4502 return NULL;
4503 }
4504
88758e9d
DW
4505 mu->space = NULL;
4506 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
4507 mu->next = *updates;
4508 u = (struct imsm_update_activate_spare *) mu->buf;
4509
4510 for (di = rv ; di ; di = di->next) {
4511 u->type = update_activate_spare;
d23fe947
DW
4512 u->dl = (struct dl *) di->devs;
4513 di->devs = NULL;
88758e9d
DW
4514 u->slot = di->disk.raid_disk;
4515 u->array = inst;
4516 u->next = u + 1;
4517 u++;
4518 }
4519 (u-1)->next = NULL;
4520 *updates = mu;
4521
4522 return rv;
4523}
4524
54c2c1ea 4525static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
8273f55e 4526{
54c2c1ea
DW
4527 struct imsm_dev *dev = get_imsm_dev(super, idx);
4528 struct imsm_map *map = get_imsm_map(dev, 0);
4529 struct imsm_map *new_map = get_imsm_map(&u->dev, 0);
4530 struct disk_info *inf = get_disk_info(u);
4531 struct imsm_disk *disk;
8273f55e
DW
4532 int i;
4533 int j;
8273f55e 4534
54c2c1ea
DW
4535 for (i = 0; i < map->num_members; i++) {
4536 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
4537 for (j = 0; j < new_map->num_members; j++)
4538 if (serialcmp(disk->serial, inf[j].serial) == 0)
8273f55e
DW
4539 return 1;
4540 }
4541
4542 return 0;
4543}
4544
24565c9a 4545static void imsm_delete(struct intel_super *super, struct dl **dlp, int index);
ae6aad82 4546
e8319a19
DW
4547static void imsm_process_update(struct supertype *st,
4548 struct metadata_update *update)
4549{
4550 /**
4551 * crack open the metadata_update envelope to find the update record
4552 * update can be one of:
4553 * update_activate_spare - a spare device has replaced a failed
4554 * device in an array, update the disk_ord_tbl. If this disk is
4555 * present in all member arrays then also clear the SPARE_DISK
4556 * flag
4557 */
4558 struct intel_super *super = st->sb;
4d7b1503 4559 struct imsm_super *mpb;
e8319a19
DW
4560 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
4561
4d7b1503
DW
4562 /* update requires a larger buf but the allocation failed */
4563 if (super->next_len && !super->next_buf) {
4564 super->next_len = 0;
4565 return;
4566 }
4567
4568 if (super->next_buf) {
4569 memcpy(super->next_buf, super->buf, super->len);
4570 free(super->buf);
4571 super->len = super->next_len;
4572 super->buf = super->next_buf;
4573
4574 super->next_len = 0;
4575 super->next_buf = NULL;
4576 }
4577
4578 mpb = super->anchor;
4579
e8319a19
DW
4580 switch (type) {
4581 case update_activate_spare: {
4582 struct imsm_update_activate_spare *u = (void *) update->buf;
949c47a0 4583 struct imsm_dev *dev = get_imsm_dev(super, u->array);
a965f303 4584 struct imsm_map *map = get_imsm_map(dev, 0);
0c046afd 4585 struct imsm_map *migr_map;
e8319a19
DW
4586 struct active_array *a;
4587 struct imsm_disk *disk;
0c046afd 4588 __u8 to_state;
e8319a19 4589 struct dl *dl;
e8319a19 4590 unsigned int found;
0c046afd
DW
4591 int failed;
4592 int victim = get_imsm_disk_idx(dev, u->slot);
e8319a19
DW
4593 int i;
4594
4595 for (dl = super->disks; dl; dl = dl->next)
d23fe947 4596 if (dl == u->dl)
e8319a19
DW
4597 break;
4598
4599 if (!dl) {
4600 fprintf(stderr, "error: imsm_activate_spare passed "
1f24f035
DW
4601 "an unknown disk (index: %d)\n",
4602 u->dl->index);
e8319a19
DW
4603 return;
4604 }
4605
4606 super->updates_pending++;
4607
0c046afd
DW
4608 /* count failures (excluding rebuilds and the victim)
4609 * to determine map[0] state
4610 */
4611 failed = 0;
4612 for (i = 0; i < map->num_members; i++) {
4613 if (i == u->slot)
4614 continue;
4615 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
25ed7e59 4616 if (!disk || is_failed(disk))
0c046afd
DW
4617 failed++;
4618 }
4619
d23fe947
DW
4620 /* adding a pristine spare, assign a new index */
4621 if (dl->index < 0) {
4622 dl->index = super->anchor->num_disks;
4623 super->anchor->num_disks++;
4624 }
d23fe947 4625 disk = &dl->disk;
f2f27e63
DW
4626 disk->status |= CONFIGURED_DISK;
4627 disk->status &= ~SPARE_DISK;
e8319a19 4628
0c046afd
DW
4629 /* mark rebuild */
4630 to_state = imsm_check_degraded(super, dev, failed);
4631 map->map_state = IMSM_T_STATE_DEGRADED;
e3bba0e0 4632 migrate(dev, to_state, MIGR_REBUILD);
0c046afd
DW
4633 migr_map = get_imsm_map(dev, 1);
4634 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
4635 set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD);
4636
148acb7b
DW
4637 /* update the family_num to mark a new container
4638 * generation, being careful to record the existing
4639 * family_num in orig_family_num to clean up after
4640 * earlier mdadm versions that neglected to set it.
4641 */
4642 if (mpb->orig_family_num == 0)
4643 mpb->orig_family_num = mpb->family_num;
4644 mpb->family_num += super->random;
4645
e8319a19
DW
4646 /* count arrays using the victim in the metadata */
4647 found = 0;
4648 for (a = st->arrays; a ; a = a->next) {
949c47a0 4649 dev = get_imsm_dev(super, a->info.container_member);
620b1713
DW
4650 map = get_imsm_map(dev, 0);
4651
4652 if (get_imsm_disk_slot(map, victim) >= 0)
4653 found++;
e8319a19
DW
4654 }
4655
24565c9a 4656 /* delete the victim if it is no longer being
e8319a19
DW
4657 * utilized anywhere
4658 */
e8319a19 4659 if (!found) {
ae6aad82 4660 struct dl **dlp;
24565c9a 4661
47ee5a45
DW
4662 /* We know that 'manager' isn't touching anything,
4663 * so it is safe to delete
4664 */
24565c9a 4665 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
ae6aad82
DW
4666 if ((*dlp)->index == victim)
4667 break;
47ee5a45
DW
4668
4669 /* victim may be on the missing list */
4670 if (!*dlp)
4671 for (dlp = &super->missing; *dlp; dlp = &(*dlp)->next)
4672 if ((*dlp)->index == victim)
4673 break;
24565c9a 4674 imsm_delete(super, dlp, victim);
e8319a19 4675 }
8273f55e
DW
4676 break;
4677 }
4678 case update_create_array: {
4679 /* someone wants to create a new array, we need to be aware of
4680 * a few races/collisions:
4681 * 1/ 'Create' called by two separate instances of mdadm
4682 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
4683 * devices that have since been assimilated via
4684 * activate_spare.
4685 * In the event this update can not be carried out mdadm will
4686 * (FIX ME) notice that its update did not take hold.
4687 */
4688 struct imsm_update_create_array *u = (void *) update->buf;
ba2de7ba 4689 struct intel_dev *dv;
8273f55e
DW
4690 struct imsm_dev *dev;
4691 struct imsm_map *map, *new_map;
4692 unsigned long long start, end;
4693 unsigned long long new_start, new_end;
4694 int i;
54c2c1ea
DW
4695 struct disk_info *inf;
4696 struct dl *dl;
8273f55e
DW
4697
4698 /* handle racing creates: first come first serve */
4699 if (u->dev_idx < mpb->num_raid_devs) {
4700 dprintf("%s: subarray %d already defined\n",
4701 __func__, u->dev_idx);
ba2de7ba 4702 goto create_error;
8273f55e
DW
4703 }
4704
4705 /* check update is next in sequence */
4706 if (u->dev_idx != mpb->num_raid_devs) {
6a3e913e
DW
4707 dprintf("%s: can not create array %d expected index %d\n",
4708 __func__, u->dev_idx, mpb->num_raid_devs);
ba2de7ba 4709 goto create_error;
8273f55e
DW
4710 }
4711
a965f303 4712 new_map = get_imsm_map(&u->dev, 0);
8273f55e
DW
4713 new_start = __le32_to_cpu(new_map->pba_of_lba0);
4714 new_end = new_start + __le32_to_cpu(new_map->blocks_per_member);
54c2c1ea 4715 inf = get_disk_info(u);
8273f55e
DW
4716
4717 /* handle activate_spare versus create race:
4718 * check to make sure that overlapping arrays do not include
4719 * overalpping disks
4720 */
4721 for (i = 0; i < mpb->num_raid_devs; i++) {
949c47a0 4722 dev = get_imsm_dev(super, i);
a965f303 4723 map = get_imsm_map(dev, 0);
8273f55e
DW
4724 start = __le32_to_cpu(map->pba_of_lba0);
4725 end = start + __le32_to_cpu(map->blocks_per_member);
4726 if ((new_start >= start && new_start <= end) ||
4727 (start >= new_start && start <= new_end))
54c2c1ea
DW
4728 /* overlap */;
4729 else
4730 continue;
4731
4732 if (disks_overlap(super, i, u)) {
8273f55e 4733 dprintf("%s: arrays overlap\n", __func__);
ba2de7ba 4734 goto create_error;
8273f55e
DW
4735 }
4736 }
8273f55e 4737
949c47a0
DW
4738 /* check that prepare update was successful */
4739 if (!update->space) {
4740 dprintf("%s: prepare update failed\n", __func__);
ba2de7ba 4741 goto create_error;
949c47a0
DW
4742 }
4743
54c2c1ea
DW
4744 /* check that all disks are still active before committing
4745 * changes. FIXME: could we instead handle this by creating a
4746 * degraded array? That's probably not what the user expects,
4747 * so better to drop this update on the floor.
4748 */
4749 for (i = 0; i < new_map->num_members; i++) {
4750 dl = serial_to_dl(inf[i].serial, super);
4751 if (!dl) {
4752 dprintf("%s: disk disappeared\n", __func__);
ba2de7ba 4753 goto create_error;
54c2c1ea 4754 }
949c47a0
DW
4755 }
4756
8273f55e 4757 super->updates_pending++;
54c2c1ea
DW
4758
4759 /* convert spares to members and fixup ord_tbl */
4760 for (i = 0; i < new_map->num_members; i++) {
4761 dl = serial_to_dl(inf[i].serial, super);
4762 if (dl->index == -1) {
4763 dl->index = mpb->num_disks;
4764 mpb->num_disks++;
4765 dl->disk.status |= CONFIGURED_DISK;
4766 dl->disk.status &= ~SPARE_DISK;
4767 }
4768 set_imsm_ord_tbl_ent(new_map, i, dl->index);
4769 }
4770
ba2de7ba
DW
4771 dv = update->space;
4772 dev = dv->dev;
949c47a0
DW
4773 update->space = NULL;
4774 imsm_copy_dev(dev, &u->dev);
ba2de7ba
DW
4775 dv->index = u->dev_idx;
4776 dv->next = super->devlist;
4777 super->devlist = dv;
8273f55e 4778 mpb->num_raid_devs++;
8273f55e 4779
4d1313e9 4780 imsm_update_version_info(super);
8273f55e 4781 break;
ba2de7ba
DW
4782 create_error:
4783 /* mdmon knows how to release update->space, but not
4784 * ((struct intel_dev *) update->space)->dev
4785 */
4786 if (update->space) {
4787 dv = update->space;
4788 free(dv->dev);
4789 }
8273f55e 4790 break;
e8319a19 4791 }
43dad3d6
DW
4792 case update_add_disk:
4793
4794 /* we may be able to repair some arrays if disks are
4795 * being added */
4796 if (super->add) {
4797 struct active_array *a;
072b727f
DW
4798
4799 super->updates_pending++;
43dad3d6
DW
4800 for (a = st->arrays; a; a = a->next)
4801 a->check_degraded = 1;
4802 }
e553d2a4 4803 /* add some spares to the metadata */
43dad3d6 4804 while (super->add) {
e553d2a4
DW
4805 struct dl *al;
4806
43dad3d6
DW
4807 al = super->add;
4808 super->add = al->next;
43dad3d6
DW
4809 al->next = super->disks;
4810 super->disks = al;
e553d2a4
DW
4811 dprintf("%s: added %x:%x\n",
4812 __func__, al->major, al->minor);
43dad3d6
DW
4813 }
4814
4815 break;
e8319a19
DW
4816 }
4817}
88758e9d 4818
8273f55e
DW
4819static void imsm_prepare_update(struct supertype *st,
4820 struct metadata_update *update)
4821{
949c47a0 4822 /**
4d7b1503
DW
4823 * Allocate space to hold new disk entries, raid-device entries or a new
4824 * mpb if necessary. The manager synchronously waits for updates to
4825 * complete in the monitor, so new mpb buffers allocated here can be
4826 * integrated by the monitor thread without worrying about live pointers
4827 * in the manager thread.
8273f55e 4828 */
949c47a0 4829 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
4d7b1503
DW
4830 struct intel_super *super = st->sb;
4831 struct imsm_super *mpb = super->anchor;
4832 size_t buf_len;
4833 size_t len = 0;
949c47a0
DW
4834
4835 switch (type) {
4836 case update_create_array: {
4837 struct imsm_update_create_array *u = (void *) update->buf;
ba2de7ba 4838 struct intel_dev *dv;
54c2c1ea
DW
4839 struct imsm_dev *dev = &u->dev;
4840 struct imsm_map *map = get_imsm_map(dev, 0);
4841 struct dl *dl;
4842 struct disk_info *inf;
4843 int i;
4844 int activate = 0;
949c47a0 4845
54c2c1ea
DW
4846 inf = get_disk_info(u);
4847 len = sizeof_imsm_dev(dev, 1);
ba2de7ba
DW
4848 /* allocate a new super->devlist entry */
4849 dv = malloc(sizeof(*dv));
4850 if (dv) {
4851 dv->dev = malloc(len);
4852 if (dv->dev)
4853 update->space = dv;
4854 else {
4855 free(dv);
4856 update->space = NULL;
4857 }
4858 }
949c47a0 4859
54c2c1ea
DW
4860 /* count how many spares will be converted to members */
4861 for (i = 0; i < map->num_members; i++) {
4862 dl = serial_to_dl(inf[i].serial, super);
4863 if (!dl) {
4864 /* hmm maybe it failed?, nothing we can do about
4865 * it here
4866 */
4867 continue;
4868 }
4869 if (count_memberships(dl, super) == 0)
4870 activate++;
4871 }
4872 len += activate * sizeof(struct imsm_disk);
949c47a0
DW
4873 break;
4874 default:
4875 break;
4876 }
4877 }
8273f55e 4878
4d7b1503
DW
4879 /* check if we need a larger metadata buffer */
4880 if (super->next_buf)
4881 buf_len = super->next_len;
4882 else
4883 buf_len = super->len;
4884
4885 if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) {
4886 /* ok we need a larger buf than what is currently allocated
4887 * if this allocation fails process_update will notice that
4888 * ->next_len is set and ->next_buf is NULL
4889 */
4890 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512);
4891 if (super->next_buf)
4892 free(super->next_buf);
4893
4894 super->next_len = buf_len;
1f45a8ad
DW
4895 if (posix_memalign(&super->next_buf, 512, buf_len) == 0)
4896 memset(super->next_buf, 0, buf_len);
4897 else
4d7b1503
DW
4898 super->next_buf = NULL;
4899 }
8273f55e
DW
4900}
4901
ae6aad82 4902/* must be called while manager is quiesced */
24565c9a 4903static void imsm_delete(struct intel_super *super, struct dl **dlp, int index)
ae6aad82
DW
4904{
4905 struct imsm_super *mpb = super->anchor;
ae6aad82
DW
4906 struct dl *iter;
4907 struct imsm_dev *dev;
4908 struct imsm_map *map;
24565c9a
DW
4909 int i, j, num_members;
4910 __u32 ord;
ae6aad82 4911
24565c9a
DW
4912 dprintf("%s: deleting device[%d] from imsm_super\n",
4913 __func__, index);
ae6aad82
DW
4914
4915 /* shift all indexes down one */
4916 for (iter = super->disks; iter; iter = iter->next)
24565c9a 4917 if (iter->index > index)
ae6aad82 4918 iter->index--;
47ee5a45
DW
4919 for (iter = super->missing; iter; iter = iter->next)
4920 if (iter->index > index)
4921 iter->index--;
ae6aad82
DW
4922
4923 for (i = 0; i < mpb->num_raid_devs; i++) {
4924 dev = get_imsm_dev(super, i);
4925 map = get_imsm_map(dev, 0);
24565c9a
DW
4926 num_members = map->num_members;
4927 for (j = 0; j < num_members; j++) {
4928 /* update ord entries being careful not to propagate
4929 * ord-flags to the first map
4930 */
4931 ord = get_imsm_ord_tbl_ent(dev, j);
ae6aad82 4932
24565c9a
DW
4933 if (ord_to_idx(ord) <= index)
4934 continue;
ae6aad82 4935
24565c9a
DW
4936 map = get_imsm_map(dev, 0);
4937 set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1));
4938 map = get_imsm_map(dev, 1);
4939 if (map)
4940 set_imsm_ord_tbl_ent(map, j, ord - 1);
ae6aad82
DW
4941 }
4942 }
4943
4944 mpb->num_disks--;
4945 super->updates_pending++;
24565c9a
DW
4946 if (*dlp) {
4947 struct dl *dl = *dlp;
4948
4949 *dlp = (*dlp)->next;
4950 __free_imsm_disk(dl);
4951 }
ae6aad82 4952}
0e600426 4953#endif /* MDASSEMBLE */
ae6aad82 4954
cdddbdbc
DW
4955struct superswitch super_imsm = {
4956#ifndef MDASSEMBLE
4957 .examine_super = examine_super_imsm,
4958 .brief_examine_super = brief_examine_super_imsm,
4737ae25 4959 .brief_examine_subarrays = brief_examine_subarrays_imsm,
9d84c8ea 4960 .export_examine_super = export_examine_super_imsm,
cdddbdbc
DW
4961 .detail_super = detail_super_imsm,
4962 .brief_detail_super = brief_detail_super_imsm,
bf5a934a 4963 .write_init_super = write_init_super_imsm,
0e600426
N
4964 .validate_geometry = validate_geometry_imsm,
4965 .add_to_super = add_to_super_imsm,
d665cc31 4966 .detail_platform = detail_platform_imsm,
cdddbdbc
DW
4967#endif
4968 .match_home = match_home_imsm,
4969 .uuid_from_super= uuid_from_super_imsm,
4970 .getinfo_super = getinfo_super_imsm,
4971 .update_super = update_super_imsm,
4972
4973 .avail_size = avail_size_imsm,
4974
4975 .compare_super = compare_super_imsm,
4976
4977 .load_super = load_super_imsm,
bf5a934a 4978 .init_super = init_super_imsm,
e683ca88 4979 .store_super = store_super_imsm,
cdddbdbc
DW
4980 .free_super = free_super_imsm,
4981 .match_metadata_desc = match_metadata_desc_imsm,
bf5a934a 4982 .container_content = container_content_imsm,
a18a888e 4983 .default_layout = imsm_level_to_layout,
cdddbdbc 4984
cdddbdbc 4985 .external = 1,
4cce4069 4986 .name = "imsm",
845dea95 4987
0e600426 4988#ifndef MDASSEMBLE
845dea95
NB
4989/* for mdmon */
4990 .open_new = imsm_open_new,
4991 .load_super = load_super_imsm,
ed9d66aa 4992 .set_array_state= imsm_set_array_state,
845dea95
NB
4993 .set_disk = imsm_set_disk,
4994 .sync_metadata = imsm_sync_metadata,
88758e9d 4995 .activate_spare = imsm_activate_spare,
e8319a19 4996 .process_update = imsm_process_update,
8273f55e 4997 .prepare_update = imsm_prepare_update,
0e600426 4998#endif /* MDASSEMBLE */
cdddbdbc 4999};