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