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