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