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