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