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