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