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