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