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