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