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