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