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