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