imsm: defend against unsupported migrations (temporary)
[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;
1206
1207 info->disk.major = 0;
1208 info->disk.minor = 0;
efb30e7f
DW
1209 if (dl) {
1210 info->disk.major = dl->major;
1211 info->disk.minor = dl->minor;
1212 }
bf5a934a
DW
1213
1214 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
1215 info->component_size = __le32_to_cpu(map->blocks_per_member);
301406c9 1216 memset(info->uuid, 0, sizeof(info->uuid));
bf5a934a 1217
f8f603f1 1218 if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty)
301406c9 1219 info->resync_start = 0;
f8f603f1
DW
1220 else if (dev->vol.migr_state)
1221 info->resync_start = __le32_to_cpu(dev->vol.curr_migr_unit);
301406c9
DW
1222 else
1223 info->resync_start = ~0ULL;
1224
1225 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1226 info->name[MAX_RAID_SERIAL_LEN] = 0;
bf5a934a 1227
f35f2525
N
1228 info->array.major_version = -1;
1229 info->array.minor_version = -2;
bf5a934a
DW
1230 sprintf(info->text_version, "/%s/%d",
1231 devnum2devname(st->container_dev),
1232 info->container_member);
a67dd8cc 1233 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
51006d85 1234 uuid_from_super_imsm(st, info->uuid);
bf5a934a
DW
1235}
1236
7a70e8aa
DW
1237/* check the config file to see if we can return a real uuid for this spare */
1238static void fixup_container_spare_uuid(struct mdinfo *inf)
1239{
1240 struct mddev_ident_s *array_list;
1241
1242 if (inf->array.level != LEVEL_CONTAINER ||
1243 memcmp(inf->uuid, uuid_match_any, sizeof(int[4])) != 0)
1244 return;
1245
1246 array_list = conf_get_ident(NULL);
1247
1248 for (; array_list; array_list = array_list->next) {
1249 if (array_list->uuid_set) {
1250 struct supertype *_sst; /* spare supertype */
1251 struct supertype *_cst; /* container supertype */
1252
1253 _cst = array_list->st;
1254 _sst = _cst->ss->match_metadata_desc(inf->text_version);
1255 if (_sst) {
1256 memcpy(inf->uuid, array_list->uuid, sizeof(int[4]));
1257 free(_sst);
1258 break;
1259 }
1260 }
1261 }
1262}
bf5a934a 1263
4f5bc454
DW
1264static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
1265{
1266 struct intel_super *super = st->sb;
4f5bc454
DW
1267 struct imsm_disk *disk;
1268 __u32 s;
4f5bc454 1269
bf5a934a
DW
1270 if (super->current_vol >= 0) {
1271 getinfo_super_imsm_volume(st, info);
1272 return;
1273 }
d23fe947
DW
1274
1275 /* Set raid_disks to zero so that Assemble will always pull in valid
1276 * spares
1277 */
1278 info->array.raid_disks = 0;
cdddbdbc
DW
1279 info->array.level = LEVEL_CONTAINER;
1280 info->array.layout = 0;
1281 info->array.md_minor = -1;
c2c087e6 1282 info->array.ctime = 0; /* N/A for imsm */
cdddbdbc
DW
1283 info->array.utime = 0;
1284 info->array.chunk_size = 0;
1285
1286 info->disk.major = 0;
1287 info->disk.minor = 0;
cdddbdbc 1288 info->disk.raid_disk = -1;
c2c087e6 1289 info->reshape_active = 0;
f35f2525
N
1290 info->array.major_version = -1;
1291 info->array.minor_version = -2;
c2c087e6 1292 strcpy(info->text_version, "imsm");
a67dd8cc 1293 info->safe_mode_delay = 0;
c2c087e6
DW
1294 info->disk.number = -1;
1295 info->disk.state = 0;
c5afc314 1296 info->name[0] = 0;
c2c087e6 1297
4a04ec6c 1298 if (super->disks) {
14e8215b
DW
1299 __u32 reserved = imsm_reserved_sectors(super, super->disks);
1300
b9f594fe 1301 disk = &super->disks->disk;
14e8215b
DW
1302 info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved;
1303 info->component_size = reserved;
f2f27e63 1304 s = disk->status;
4a04ec6c 1305 info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
df474657
DW
1306 /* we don't change info->disk.raid_disk here because
1307 * this state will be finalized in mdmon after we have
1308 * found the 'most fresh' version of the metadata
1309 */
1310 info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
032e9e29 1311 info->disk.state |= s & SPARE_DISK ? 0 : (1 << MD_DISK_SYNC);
cdddbdbc 1312 }
a575e2a7
DW
1313
1314 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1315 * ->compare_super may have updated the 'num_raid_devs' field for spares
1316 */
1317 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
36ba7d48 1318 uuid_from_super_imsm(st, info->uuid);
7a70e8aa 1319 else {
032e9e29 1320 memcpy(info->uuid, uuid_match_any, sizeof(int[4]));
7a70e8aa
DW
1321 fixup_container_spare_uuid(info);
1322 }
cdddbdbc
DW
1323}
1324
cdddbdbc
DW
1325static int update_super_imsm(struct supertype *st, struct mdinfo *info,
1326 char *update, char *devname, int verbose,
1327 int uuid_set, char *homehost)
1328{
f352c545
DW
1329 /* FIXME */
1330
1331 /* For 'assemble' and 'force' we need to return non-zero if any
1332 * change was made. For others, the return value is ignored.
1333 * Update options are:
1334 * force-one : This device looks a bit old but needs to be included,
1335 * update age info appropriately.
1336 * assemble: clear any 'faulty' flag to allow this device to
1337 * be assembled.
1338 * force-array: Array is degraded but being forced, mark it clean
1339 * if that will be needed to assemble it.
1340 *
1341 * newdev: not used ????
1342 * grow: Array has gained a new device - this is currently for
1343 * linear only
1344 * resync: mark as dirty so a resync will happen.
1345 * name: update the name - preserving the homehost
1346 *
1347 * Following are not relevant for this imsm:
1348 * sparc2.2 : update from old dodgey metadata
1349 * super-minor: change the preferred_minor number
1350 * summaries: update redundant counters.
1351 * uuid: Change the uuid of the array to match watch is given
1352 * homehost: update the recorded homehost
1353 * _reshape_progress: record new reshape_progress position.
1354 */
1355 int rv = 0;
1356 //struct intel_super *super = st->sb;
1357 //struct imsm_super *mpb = super->mpb;
1358
1359 if (strcmp(update, "grow") == 0) {
1360 }
1361 if (strcmp(update, "resync") == 0) {
1362 /* dev->vol.dirty = 1; */
1363 }
1364
1365 /* IMSM has no concept of UUID or homehost */
1366
1367 return rv;
cdddbdbc
DW
1368}
1369
c2c087e6 1370static size_t disks_to_mpb_size(int disks)
cdddbdbc 1371{
c2c087e6 1372 size_t size;
cdddbdbc 1373
c2c087e6
DW
1374 size = sizeof(struct imsm_super);
1375 size += (disks - 1) * sizeof(struct imsm_disk);
1376 size += 2 * sizeof(struct imsm_dev);
1377 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1378 size += (4 - 2) * sizeof(struct imsm_map);
1379 /* 4 possible disk_ord_tbl's */
1380 size += 4 * (disks - 1) * sizeof(__u32);
1381
1382 return size;
1383}
1384
1385static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
1386{
1387 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
1388 return 0;
1389
1390 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
cdddbdbc
DW
1391}
1392
ba2de7ba
DW
1393static void free_devlist(struct intel_super *super)
1394{
1395 struct intel_dev *dv;
1396
1397 while (super->devlist) {
1398 dv = super->devlist->next;
1399 free(super->devlist->dev);
1400 free(super->devlist);
1401 super->devlist = dv;
1402 }
1403}
1404
1405static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
1406{
1407 memcpy(dest, src, sizeof_imsm_dev(src, 0));
1408}
1409
cdddbdbc
DW
1410static int compare_super_imsm(struct supertype *st, struct supertype *tst)
1411{
1412 /*
1413 * return:
1414 * 0 same, or first was empty, and second was copied
1415 * 1 second had wrong number
1416 * 2 wrong uuid
1417 * 3 wrong other info
1418 */
1419 struct intel_super *first = st->sb;
1420 struct intel_super *sec = tst->sb;
1421
1422 if (!first) {
1423 st->sb = tst->sb;
1424 tst->sb = NULL;
1425 return 0;
1426 }
1427
949c47a0 1428 if (memcmp(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH) != 0)
cdddbdbc 1429 return 3;
d23fe947
DW
1430
1431 /* if an anchor does not have num_raid_devs set then it is a free
1432 * floating spare
1433 */
1434 if (first->anchor->num_raid_devs > 0 &&
1435 sec->anchor->num_raid_devs > 0) {
1436 if (first->anchor->family_num != sec->anchor->family_num)
1437 return 3;
d23fe947 1438 }
cdddbdbc 1439
3e372e5a
DW
1440 /* if 'first' is a spare promote it to a populated mpb with sec's
1441 * family number
1442 */
1443 if (first->anchor->num_raid_devs == 0 &&
1444 sec->anchor->num_raid_devs > 0) {
78d30f94 1445 int i;
ba2de7ba
DW
1446 struct intel_dev *dv;
1447 struct imsm_dev *dev;
78d30f94
DW
1448
1449 /* we need to copy raid device info from sec if an allocation
1450 * fails here we don't associate the spare
1451 */
1452 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
ba2de7ba
DW
1453 dv = malloc(sizeof(*dv));
1454 if (!dv)
1455 break;
1456 dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
1457 if (!dev) {
1458 free(dv);
1459 break;
78d30f94 1460 }
ba2de7ba
DW
1461 dv->dev = dev;
1462 dv->index = i;
1463 dv->next = first->devlist;
1464 first->devlist = dv;
78d30f94 1465 }
ba2de7ba
DW
1466 if (i <= sec->anchor->num_raid_devs) {
1467 /* allocation failure */
1468 free_devlist(first);
1469 fprintf(stderr, "imsm: failed to associate spare\n");
1470 return 3;
78d30f94 1471 }
ba2de7ba
DW
1472 for (i = 0; i < sec->anchor->num_raid_devs; i++)
1473 imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
78d30f94 1474
3e372e5a
DW
1475 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
1476 first->anchor->family_num = sec->anchor->family_num;
1477 }
1478
cdddbdbc
DW
1479 return 0;
1480}
1481
0030e8d6
DW
1482static void fd2devname(int fd, char *name)
1483{
1484 struct stat st;
1485 char path[256];
1486 char dname[100];
1487 char *nm;
1488 int rv;
1489
1490 name[0] = '\0';
1491 if (fstat(fd, &st) != 0)
1492 return;
1493 sprintf(path, "/sys/dev/block/%d:%d",
1494 major(st.st_rdev), minor(st.st_rdev));
1495
1496 rv = readlink(path, dname, sizeof(dname));
1497 if (rv <= 0)
1498 return;
1499
1500 dname[rv] = '\0';
1501 nm = strrchr(dname, '/');
1502 nm++;
1503 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
1504}
1505
1506
cdddbdbc
DW
1507extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
1508
1509static int imsm_read_serial(int fd, char *devname,
1510 __u8 serial[MAX_RAID_SERIAL_LEN])
1511{
1512 unsigned char scsi_serial[255];
cdddbdbc
DW
1513 int rv;
1514 int rsp_len;
1f24f035 1515 int len;
316e2bf4
DW
1516 char *dest;
1517 char *src;
1518 char *rsp_buf;
1519 int i;
cdddbdbc
DW
1520
1521 memset(scsi_serial, 0, sizeof(scsi_serial));
cdddbdbc 1522
f9ba0ff1
DW
1523 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
1524
40ebbb9c 1525 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
f9ba0ff1
DW
1526 memset(serial, 0, MAX_RAID_SERIAL_LEN);
1527 fd2devname(fd, (char *) serial);
0030e8d6
DW
1528 return 0;
1529 }
1530
cdddbdbc
DW
1531 if (rv != 0) {
1532 if (devname)
1533 fprintf(stderr,
1534 Name ": Failed to retrieve serial for %s\n",
1535 devname);
1536 return rv;
1537 }
1538
1539 rsp_len = scsi_serial[3];
03cd4cc8
DW
1540 if (!rsp_len) {
1541 if (devname)
1542 fprintf(stderr,
1543 Name ": Failed to retrieve serial for %s\n",
1544 devname);
1545 return 2;
1546 }
1f24f035 1547 rsp_buf = (char *) &scsi_serial[4];
5c3db629 1548
316e2bf4
DW
1549 /* trim all whitespace and non-printable characters and convert
1550 * ':' to ';'
1551 */
1552 for (i = 0, dest = rsp_buf; i < rsp_len; i++) {
1553 src = &rsp_buf[i];
1554 if (*src > 0x20) {
1555 /* ':' is reserved for use in placeholder serial
1556 * numbers for missing disks
1557 */
1558 if (*src == ':')
1559 *dest++ = ';';
1560 else
1561 *dest++ = *src;
1562 }
1563 }
1564 len = dest - rsp_buf;
1565 dest = rsp_buf;
1566
1567 /* truncate leading characters */
1568 if (len > MAX_RAID_SERIAL_LEN) {
1569 dest += len - MAX_RAID_SERIAL_LEN;
1f24f035 1570 len = MAX_RAID_SERIAL_LEN;
316e2bf4 1571 }
5c3db629 1572
5c3db629 1573 memset(serial, 0, MAX_RAID_SERIAL_LEN);
316e2bf4 1574 memcpy(serial, dest, len);
cdddbdbc
DW
1575
1576 return 0;
1577}
1578
1f24f035
DW
1579static int serialcmp(__u8 *s1, __u8 *s2)
1580{
1581 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
1582}
1583
1584static void serialcpy(__u8 *dest, __u8 *src)
1585{
1586 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
1587}
1588
54c2c1ea
DW
1589static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
1590{
1591 struct dl *dl;
1592
1593 for (dl = super->disks; dl; dl = dl->next)
1594 if (serialcmp(dl->serial, serial) == 0)
1595 break;
1596
1597 return dl;
1598}
1599
cdddbdbc
DW
1600static int
1601load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
1602{
cdddbdbc
DW
1603 struct dl *dl;
1604 struct stat stb;
cdddbdbc
DW
1605 int rv;
1606 int i;
d23fe947
DW
1607 int alloc = 1;
1608 __u8 serial[MAX_RAID_SERIAL_LEN];
1609
1610 rv = imsm_read_serial(fd, devname, serial);
1611
1612 if (rv != 0)
1613 return 2;
1614
1615 /* check if this is a disk we have seen before. it may be a spare in
1616 * super->disks while the current anchor believes it is a raid member,
1617 * check if we need to update dl->index
1618 */
54c2c1ea 1619 dl = serial_to_dl(serial, super);
d23fe947
DW
1620 if (!dl)
1621 dl = malloc(sizeof(*dl));
1622 else
1623 alloc = 0;
cdddbdbc 1624
b9f594fe 1625 if (!dl) {
cdddbdbc
DW
1626 if (devname)
1627 fprintf(stderr,
1628 Name ": failed to allocate disk buffer for %s\n",
1629 devname);
1630 return 2;
1631 }
cdddbdbc 1632
d23fe947
DW
1633 if (alloc) {
1634 fstat(fd, &stb);
1635 dl->major = major(stb.st_rdev);
1636 dl->minor = minor(stb.st_rdev);
1637 dl->next = super->disks;
1638 dl->fd = keep_fd ? fd : -1;
1639 dl->devname = devname ? strdup(devname) : NULL;
1f24f035 1640 serialcpy(dl->serial, serial);
8796fdc4 1641 dl->index = -2;
0dcecb2e 1642 dl->e = NULL;
d23fe947
DW
1643 } else if (keep_fd) {
1644 close(dl->fd);
1645 dl->fd = fd;
1646 }
cdddbdbc 1647
d23fe947 1648 /* look up this disk's index in the current anchor */
949c47a0
DW
1649 for (i = 0; i < super->anchor->num_disks; i++) {
1650 struct imsm_disk *disk_iter;
1651
1652 disk_iter = __get_imsm_disk(super->anchor, i);
cdddbdbc 1653
1f24f035 1654 if (serialcmp(disk_iter->serial, dl->serial) == 0) {
b9f594fe 1655 dl->disk = *disk_iter;
d23fe947
DW
1656 /* only set index on disks that are a member of a
1657 * populated contianer, i.e. one with raid_devs
1658 */
f2f27e63 1659 if (dl->disk.status & FAILED_DISK)
6c386dd3 1660 dl->index = -2;
f2f27e63 1661 else if (dl->disk.status & SPARE_DISK)
d23fe947
DW
1662 dl->index = -1;
1663 else
1664 dl->index = i;
8796fdc4 1665
cdddbdbc 1666 break;
949c47a0 1667 }
cdddbdbc
DW
1668 }
1669
3f6efecc
DW
1670 /* no match, maybe a stale failed drive */
1671 if (i == super->anchor->num_disks && dl->index >= 0) {
1672 dl->disk = *__get_imsm_disk(super->anchor, dl->index);
f2f27e63 1673 if (dl->disk.status & FAILED_DISK)
3f6efecc
DW
1674 dl->index = -2;
1675 }
1676
d23fe947
DW
1677 if (alloc)
1678 super->disks = dl;
6c386dd3 1679
949c47a0
DW
1680 return 0;
1681}
1682
0e600426 1683#ifndef MDASSEMBLE
0c046afd
DW
1684/* When migrating map0 contains the 'destination' state while map1
1685 * contains the current state. When not migrating map0 contains the
1686 * current state. This routine assumes that map[0].map_state is set to
1687 * the current array state before being called.
1688 *
1689 * Migration is indicated by one of the following states
1690 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
e3bba0e0 1691 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
0c046afd 1692 * map1state=unitialized)
1484e727 1693 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
0c046afd 1694 * map1state=normal)
e3bba0e0 1695 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
0c046afd
DW
1696 * map1state=degraded)
1697 */
0556e1a2 1698static void migrate(struct imsm_dev *dev, __u8 to_state, int migr_type)
3393c6af 1699{
0c046afd 1700 struct imsm_map *dest;
3393c6af
DW
1701 struct imsm_map *src = get_imsm_map(dev, 0);
1702
0c046afd 1703 dev->vol.migr_state = 1;
1484e727 1704 set_migr_type(dev, migr_type);
f8f603f1 1705 dev->vol.curr_migr_unit = 0;
0c046afd
DW
1706 dest = get_imsm_map(dev, 1);
1707
0556e1a2 1708 /* duplicate and then set the target end state in map[0] */
3393c6af 1709 memcpy(dest, src, sizeof_imsm_map(src));
0556e1a2
DW
1710 if (migr_type == MIGR_REBUILD) {
1711 __u32 ord;
1712 int i;
1713
1714 for (i = 0; i < src->num_members; i++) {
1715 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
1716 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
1717 }
1718 }
1719
0c046afd 1720 src->map_state = to_state;
949c47a0 1721}
f8f603f1
DW
1722
1723static void end_migration(struct imsm_dev *dev, __u8 map_state)
1724{
1725 struct imsm_map *map = get_imsm_map(dev, 0);
0556e1a2
DW
1726 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
1727 int i;
1728
1729 /* merge any IMSM_ORD_REBUILD bits that were not successfully
1730 * completed in the last migration.
1731 *
1732 * FIXME add support for online capacity expansion and
1733 * raid-level-migration
1734 */
1735 for (i = 0; i < prev->num_members; i++)
1736 map->disk_ord_tbl[i] |= prev->disk_ord_tbl[i];
f8f603f1
DW
1737
1738 dev->vol.migr_state = 0;
1739 dev->vol.curr_migr_unit = 0;
1740 map->map_state = map_state;
1741}
0e600426 1742#endif
949c47a0
DW
1743
1744static int parse_raid_devices(struct intel_super *super)
1745{
1746 int i;
1747 struct imsm_dev *dev_new;
4d7b1503
DW
1748 size_t len, len_migr;
1749 size_t space_needed = 0;
1750 struct imsm_super *mpb = super->anchor;
949c47a0
DW
1751
1752 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1753 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
ba2de7ba 1754 struct intel_dev *dv;
949c47a0 1755
4d7b1503
DW
1756 len = sizeof_imsm_dev(dev_iter, 0);
1757 len_migr = sizeof_imsm_dev(dev_iter, 1);
1758 if (len_migr > len)
1759 space_needed += len_migr - len;
1760
ba2de7ba
DW
1761 dv = malloc(sizeof(*dv));
1762 if (!dv)
1763 return 1;
4d7b1503 1764 dev_new = malloc(len_migr);
ba2de7ba
DW
1765 if (!dev_new) {
1766 free(dv);
949c47a0 1767 return 1;
ba2de7ba 1768 }
949c47a0 1769 imsm_copy_dev(dev_new, dev_iter);
ba2de7ba
DW
1770 dv->dev = dev_new;
1771 dv->index = i;
1772 dv->next = super->devlist;
1773 super->devlist = dv;
949c47a0 1774 }
cdddbdbc 1775
4d7b1503
DW
1776 /* ensure that super->buf is large enough when all raid devices
1777 * are migrating
1778 */
1779 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
1780 void *buf;
1781
1782 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512);
1783 if (posix_memalign(&buf, 512, len) != 0)
1784 return 1;
1785
1f45a8ad
DW
1786 memcpy(buf, super->buf, super->len);
1787 memset(buf + super->len, 0, len - super->len);
4d7b1503
DW
1788 free(super->buf);
1789 super->buf = buf;
1790 super->len = len;
1791 }
1792
cdddbdbc
DW
1793 return 0;
1794}
1795
604b746f
JD
1796/* retrieve a pointer to the bbm log which starts after all raid devices */
1797struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
1798{
1799 void *ptr = NULL;
1800
1801 if (__le32_to_cpu(mpb->bbm_log_size)) {
1802 ptr = mpb;
1803 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
1804 }
1805
1806 return ptr;
1807}
1808
d23fe947 1809static void __free_imsm(struct intel_super *super, int free_disks);
9ca2c81c 1810
cdddbdbc
DW
1811/* load_imsm_mpb - read matrix metadata
1812 * allocates super->mpb to be freed by free_super
1813 */
1814static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
1815{
1816 unsigned long long dsize;
cdddbdbc
DW
1817 unsigned long long sectors;
1818 struct stat;
6416d527 1819 struct imsm_super *anchor;
cdddbdbc 1820 __u32 check_sum;
949c47a0 1821 int rc;
cdddbdbc 1822
cdddbdbc
DW
1823 get_dev_size(fd, NULL, &dsize);
1824
1825 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
1826 if (devname)
1827 fprintf(stderr,
1828 Name ": Cannot seek to anchor block on %s: %s\n",
1829 devname, strerror(errno));
1830 return 1;
1831 }
1832
949c47a0 1833 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
ad97895e
DW
1834 if (devname)
1835 fprintf(stderr,
1836 Name ": Failed to allocate imsm anchor buffer"
1837 " on %s\n", devname);
1838 return 1;
1839 }
949c47a0 1840 if (read(fd, anchor, 512) != 512) {
cdddbdbc
DW
1841 if (devname)
1842 fprintf(stderr,
1843 Name ": Cannot read anchor block on %s: %s\n",
1844 devname, strerror(errno));
6416d527 1845 free(anchor);
cdddbdbc
DW
1846 return 1;
1847 }
1848
6416d527 1849 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
cdddbdbc
DW
1850 if (devname)
1851 fprintf(stderr,
1852 Name ": no IMSM anchor on %s\n", devname);
6416d527 1853 free(anchor);
cdddbdbc
DW
1854 return 2;
1855 }
1856
d23fe947 1857 __free_imsm(super, 0);
949c47a0
DW
1858 super->len = ROUND_UP(anchor->mpb_size, 512);
1859 if (posix_memalign(&super->buf, 512, super->len) != 0) {
cdddbdbc
DW
1860 if (devname)
1861 fprintf(stderr,
1862 Name ": unable to allocate %zu byte mpb buffer\n",
949c47a0 1863 super->len);
6416d527 1864 free(anchor);
cdddbdbc
DW
1865 return 2;
1866 }
949c47a0 1867 memcpy(super->buf, anchor, 512);
cdddbdbc 1868
6416d527
NB
1869 sectors = mpb_sectors(anchor) - 1;
1870 free(anchor);
949c47a0 1871 if (!sectors) {
ecf45690
DW
1872 check_sum = __gen_imsm_checksum(super->anchor);
1873 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
1874 if (devname)
1875 fprintf(stderr,
1876 Name ": IMSM checksum %x != %x on %s\n",
1877 check_sum,
1878 __le32_to_cpu(super->anchor->check_sum),
1879 devname);
1880 return 2;
1881 }
1882
949c47a0
DW
1883 rc = load_imsm_disk(fd, super, devname, 0);
1884 if (rc == 0)
1885 rc = parse_raid_devices(super);
1886 return rc;
1887 }
cdddbdbc
DW
1888
1889 /* read the extended mpb */
1890 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
1891 if (devname)
1892 fprintf(stderr,
1893 Name ": Cannot seek to extended mpb on %s: %s\n",
1894 devname, strerror(errno));
1895 return 1;
1896 }
1897
949c47a0 1898 if (read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
cdddbdbc
DW
1899 if (devname)
1900 fprintf(stderr,
1901 Name ": Cannot read extended mpb on %s: %s\n",
1902 devname, strerror(errno));
1903 return 2;
1904 }
1905
949c47a0
DW
1906 check_sum = __gen_imsm_checksum(super->anchor);
1907 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
cdddbdbc
DW
1908 if (devname)
1909 fprintf(stderr,
1910 Name ": IMSM checksum %x != %x on %s\n",
949c47a0 1911 check_sum, __le32_to_cpu(super->anchor->check_sum),
cdddbdbc 1912 devname);
db575f3b 1913 return 3;
cdddbdbc
DW
1914 }
1915
604b746f
JD
1916 /* FIXME the BBM log is disk specific so we cannot use this global
1917 * buffer for all disks. Ok for now since we only look at the global
1918 * bbm_log_size parameter to gate assembly
1919 */
1920 super->bbm_log = __get_imsm_bbm_log(super->anchor);
1921
949c47a0
DW
1922 rc = load_imsm_disk(fd, super, devname, 0);
1923 if (rc == 0)
1924 rc = parse_raid_devices(super);
4d7b1503 1925
949c47a0 1926 return rc;
cdddbdbc
DW
1927}
1928
ae6aad82
DW
1929static void __free_imsm_disk(struct dl *d)
1930{
1931 if (d->fd >= 0)
1932 close(d->fd);
1933 if (d->devname)
1934 free(d->devname);
0dcecb2e
DW
1935 if (d->e)
1936 free(d->e);
ae6aad82
DW
1937 free(d);
1938
1939}
cdddbdbc
DW
1940static void free_imsm_disks(struct intel_super *super)
1941{
47ee5a45 1942 struct dl *d;
cdddbdbc 1943
47ee5a45
DW
1944 while (super->disks) {
1945 d = super->disks;
cdddbdbc 1946 super->disks = d->next;
ae6aad82 1947 __free_imsm_disk(d);
cdddbdbc 1948 }
47ee5a45
DW
1949 while (super->missing) {
1950 d = super->missing;
1951 super->missing = d->next;
1952 __free_imsm_disk(d);
1953 }
1954
cdddbdbc
DW
1955}
1956
9ca2c81c 1957/* free all the pieces hanging off of a super pointer */
d23fe947 1958static void __free_imsm(struct intel_super *super, int free_disks)
cdddbdbc 1959{
9ca2c81c 1960 if (super->buf) {
949c47a0 1961 free(super->buf);
9ca2c81c
DW
1962 super->buf = NULL;
1963 }
d23fe947
DW
1964 if (free_disks)
1965 free_imsm_disks(super);
ba2de7ba 1966 free_devlist(super);
88c32bb1
DW
1967 if (super->hba) {
1968 free((void *) super->hba);
1969 super->hba = NULL;
1970 }
cdddbdbc
DW
1971}
1972
9ca2c81c
DW
1973static void free_imsm(struct intel_super *super)
1974{
d23fe947 1975 __free_imsm(super, 1);
9ca2c81c
DW
1976 free(super);
1977}
cdddbdbc
DW
1978
1979static void free_super_imsm(struct supertype *st)
1980{
1981 struct intel_super *super = st->sb;
1982
1983 if (!super)
1984 return;
1985
1986 free_imsm(super);
1987 st->sb = NULL;
1988}
1989
c2c087e6
DW
1990static struct intel_super *alloc_super(int creating_imsm)
1991{
1992 struct intel_super *super = malloc(sizeof(*super));
1993
1994 if (super) {
1995 memset(super, 0, sizeof(*super));
1996 super->creating_imsm = creating_imsm;
bf5a934a 1997 super->current_vol = -1;
0dcecb2e 1998 super->create_offset = ~((__u32 ) 0);
88c32bb1
DW
1999 if (!check_env("IMSM_NO_PLATFORM"))
2000 super->orom = find_imsm_orom();
cceebc67 2001 if (super->orom && !check_env("IMSM_TEST_OROM")) {
88c32bb1
DW
2002 struct sys_dev *list, *ent;
2003
2004 /* find the first intel ahci controller */
2005 list = find_driver_devices("pci", "ahci");
2006 for (ent = list; ent; ent = ent->next)
2007 if (devpath_to_vendor(ent->path) == 0x8086)
2008 break;
2009 if (ent) {
2010 super->hba = ent->path;
2011 ent->path = NULL;
2012 }
2013 free_sys_dev(&list);
2014 }
c2c087e6
DW
2015 }
2016
2017 return super;
2018}
2019
cdddbdbc 2020#ifndef MDASSEMBLE
47ee5a45
DW
2021/* find_missing - helper routine for load_super_imsm_all that identifies
2022 * disks that have disappeared from the system. This routine relies on
2023 * the mpb being uptodate, which it is at load time.
2024 */
2025static int find_missing(struct intel_super *super)
2026{
2027 int i;
2028 struct imsm_super *mpb = super->anchor;
2029 struct dl *dl;
2030 struct imsm_disk *disk;
47ee5a45
DW
2031
2032 for (i = 0; i < mpb->num_disks; i++) {
2033 disk = __get_imsm_disk(mpb, i);
54c2c1ea 2034 dl = serial_to_dl(disk->serial, super);
47ee5a45
DW
2035 if (dl)
2036 continue;
47ee5a45
DW
2037
2038 dl = malloc(sizeof(*dl));
2039 if (!dl)
2040 return 1;
2041 dl->major = 0;
2042 dl->minor = 0;
2043 dl->fd = -1;
2044 dl->devname = strdup("missing");
2045 dl->index = i;
2046 serialcpy(dl->serial, disk->serial);
2047 dl->disk = *disk;
689c9bf3 2048 dl->e = NULL;
47ee5a45
DW
2049 dl->next = super->missing;
2050 super->missing = dl;
2051 }
2052
2053 return 0;
2054}
2055
cdddbdbc
DW
2056static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
2057 char *devname, int keep_fd)
2058{
2059 struct mdinfo *sra;
2060 struct intel_super *super;
2061 struct mdinfo *sd, *best = NULL;
2062 __u32 bestgen = 0;
2063 __u32 gen;
2064 char nm[20];
2065 int dfd;
2066 int rv;
db575f3b
DW
2067 int devnum = fd2devnum(fd);
2068 int retry;
dab4a513 2069 enum sysfs_read_flags flags;
cdddbdbc 2070
dab4a513
DW
2071 flags = GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE;
2072 if (mdmon_running(devnum))
2073 flags |= SKIP_GONE_DEVS;
2074
2075 /* check if 'fd' an opened container */
2076 sra = sysfs_read(fd, 0, flags);
cdddbdbc
DW
2077 if (!sra)
2078 return 1;
2079
2080 if (sra->array.major_version != -1 ||
2081 sra->array.minor_version != -2 ||
2082 strcmp(sra->text_version, "imsm") != 0)
2083 return 1;
2084
c2c087e6 2085 super = alloc_super(0);
cdddbdbc
DW
2086 if (!super)
2087 return 1;
2088
d23fe947 2089 /* find the most up to date disk in this array, skipping spares */
cdddbdbc
DW
2090 for (sd = sra->devs; sd; sd = sd->next) {
2091 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2092 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
be2c0e38 2093 if (dfd < 0) {
cdddbdbc
DW
2094 free_imsm(super);
2095 return 2;
2096 }
2097 rv = load_imsm_mpb(dfd, super, NULL);
db575f3b
DW
2098
2099 /* retry the load if we might have raced against mdmon */
2100 if (rv == 3 && mdmon_running(devnum))
2101 for (retry = 0; retry < 3; retry++) {
2102 usleep(3000);
2103 rv = load_imsm_mpb(dfd, super, NULL);
2104 if (rv != 3)
2105 break;
2106 }
cdddbdbc
DW
2107 if (!keep_fd)
2108 close(dfd);
2109 if (rv == 0) {
d23fe947
DW
2110 if (super->anchor->num_raid_devs == 0)
2111 gen = 0;
2112 else
2113 gen = __le32_to_cpu(super->anchor->generation_num);
cdddbdbc
DW
2114 if (!best || gen > bestgen) {
2115 bestgen = gen;
2116 best = sd;
2117 }
2118 } else {
2119 free_imsm(super);
db575f3b 2120 return rv;
cdddbdbc
DW
2121 }
2122 }
2123
2124 if (!best) {
2125 free_imsm(super);
2126 return 1;
2127 }
2128
2129 /* load the most up to date anchor */
2130 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
2131 dfd = dev_open(nm, O_RDONLY);
be2c0e38 2132 if (dfd < 0) {
cdddbdbc
DW
2133 free_imsm(super);
2134 return 1;
2135 }
2136 rv = load_imsm_mpb(dfd, super, NULL);
2137 close(dfd);
2138 if (rv != 0) {
2139 free_imsm(super);
2140 return 2;
2141 }
2142
d23fe947 2143 /* re-parse the disk list with the current anchor */
cdddbdbc
DW
2144 for (sd = sra->devs ; sd ; sd = sd->next) {
2145 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2146 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
be2c0e38 2147 if (dfd < 0) {
cdddbdbc
DW
2148 free_imsm(super);
2149 return 2;
2150 }
2151 load_imsm_disk(dfd, super, NULL, keep_fd);
2152 if (!keep_fd)
2153 close(dfd);
2154 }
2155
47ee5a45
DW
2156
2157 if (find_missing(super) != 0) {
2158 free_imsm(super);
2159 return 2;
2160 }
2161
f7e7067b 2162 if (st->subarray[0]) {
949c47a0 2163 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
bf5a934a
DW
2164 super->current_vol = atoi(st->subarray);
2165 else
2166 return 1;
f7e7067b
NB
2167 }
2168
cdddbdbc 2169 *sbp = super;
db575f3b 2170 st->container_dev = devnum;
cdddbdbc 2171 if (st->ss == NULL) {
bf5a934a 2172 st->ss = &super_imsm;
cdddbdbc
DW
2173 st->minor_version = 0;
2174 st->max_devs = IMSM_MAX_DEVICES;
2175 }
352452c3 2176 st->loaded_container = 1;
cdddbdbc
DW
2177
2178 return 0;
2179}
2180#endif
2181
2182static int load_super_imsm(struct supertype *st, int fd, char *devname)
2183{
2184 struct intel_super *super;
2185 int rv;
2186
2187#ifndef MDASSEMBLE
3dbccbcf 2188 if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0)
cdddbdbc
DW
2189 return 0;
2190#endif
f7e7067b
NB
2191 if (st->subarray[0])
2192 return 1; /* FIXME */
cdddbdbc 2193
c2c087e6 2194 super = alloc_super(0);
cdddbdbc
DW
2195 if (!super) {
2196 fprintf(stderr,
2197 Name ": malloc of %zu failed.\n",
2198 sizeof(*super));
2199 return 1;
2200 }
2201
2202 rv = load_imsm_mpb(fd, super, devname);
2203
2204 if (rv) {
2205 if (devname)
2206 fprintf(stderr,
2207 Name ": Failed to load all information "
2208 "sections on %s\n", devname);
2209 free_imsm(super);
2210 return rv;
2211 }
2212
2213 st->sb = super;
2214 if (st->ss == NULL) {
2215 st->ss = &super_imsm;
2216 st->minor_version = 0;
2217 st->max_devs = IMSM_MAX_DEVICES;
2218 }
352452c3 2219 st->loaded_container = 0;
cdddbdbc
DW
2220
2221 return 0;
2222}
2223
ef6ffade
DW
2224static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
2225{
2226 if (info->level == 1)
2227 return 128;
2228 return info->chunk_size >> 9;
2229}
2230
ff596308 2231static __u32 info_to_num_data_stripes(mdu_array_info_t *info, int num_domains)
ef6ffade
DW
2232{
2233 __u32 num_stripes;
2234
2235 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
ff596308 2236 num_stripes /= num_domains;
ef6ffade
DW
2237
2238 return num_stripes;
2239}
2240
fcfd9599
DW
2241static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
2242{
4025c288
DW
2243 if (info->level == 1)
2244 return info->size * 2;
2245 else
2246 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
fcfd9599
DW
2247}
2248
4d1313e9
DW
2249static void imsm_update_version_info(struct intel_super *super)
2250{
2251 /* update the version and attributes */
2252 struct imsm_super *mpb = super->anchor;
2253 char *version;
2254 struct imsm_dev *dev;
2255 struct imsm_map *map;
2256 int i;
2257
2258 for (i = 0; i < mpb->num_raid_devs; i++) {
2259 dev = get_imsm_dev(super, i);
2260 map = get_imsm_map(dev, 0);
2261 if (__le32_to_cpu(dev->size_high) > 0)
2262 mpb->attributes |= MPB_ATTRIB_2TB;
2263
2264 /* FIXME detect when an array spans a port multiplier */
2265 #if 0
2266 mpb->attributes |= MPB_ATTRIB_PM;
2267 #endif
2268
2269 if (mpb->num_raid_devs > 1 ||
2270 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
2271 version = MPB_VERSION_ATTRIBS;
2272 switch (get_imsm_raid_level(map)) {
2273 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
2274 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
2275 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
2276 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
2277 }
2278 } else {
2279 if (map->num_members >= 5)
2280 version = MPB_VERSION_5OR6_DISK_ARRAY;
2281 else if (dev->status == DEV_CLONE_N_GO)
2282 version = MPB_VERSION_CNG;
2283 else if (get_imsm_raid_level(map) == 5)
2284 version = MPB_VERSION_RAID5;
2285 else if (map->num_members >= 3)
2286 version = MPB_VERSION_3OR4_DISK_ARRAY;
2287 else if (get_imsm_raid_level(map) == 1)
2288 version = MPB_VERSION_RAID1;
2289 else
2290 version = MPB_VERSION_RAID0;
2291 }
2292 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
2293 }
2294}
2295
8b353278
DW
2296static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
2297 unsigned long long size, char *name,
2298 char *homehost, int *uuid)
cdddbdbc 2299{
c2c087e6
DW
2300 /* We are creating a volume inside a pre-existing container.
2301 * so st->sb is already set.
2302 */
2303 struct intel_super *super = st->sb;
949c47a0 2304 struct imsm_super *mpb = super->anchor;
ba2de7ba 2305 struct intel_dev *dv;
c2c087e6
DW
2306 struct imsm_dev *dev;
2307 struct imsm_vol *vol;
2308 struct imsm_map *map;
2309 int idx = mpb->num_raid_devs;
2310 int i;
2311 unsigned long long array_blocks;
2c092cad 2312 size_t size_old, size_new;
ff596308 2313 __u32 num_data_stripes;
cdddbdbc 2314
88c32bb1 2315 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
c2c087e6 2316 fprintf(stderr, Name": This imsm-container already has the "
88c32bb1 2317 "maximum of %d volumes\n", super->orom->vpa);
c2c087e6
DW
2318 return 0;
2319 }
2320
2c092cad
DW
2321 /* ensure the mpb is large enough for the new data */
2322 size_old = __le32_to_cpu(mpb->mpb_size);
2323 size_new = disks_to_mpb_size(info->nr_disks);
2324 if (size_new > size_old) {
2325 void *mpb_new;
2326 size_t size_round = ROUND_UP(size_new, 512);
2327
2328 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
2329 fprintf(stderr, Name": could not allocate new mpb\n");
2330 return 0;
2331 }
2332 memcpy(mpb_new, mpb, size_old);
2333 free(mpb);
2334 mpb = mpb_new;
949c47a0 2335 super->anchor = mpb_new;
2c092cad
DW
2336 mpb->mpb_size = __cpu_to_le32(size_new);
2337 memset(mpb_new + size_old, 0, size_round - size_old);
2338 }
bf5a934a 2339 super->current_vol = idx;
d23fe947
DW
2340 /* when creating the first raid device in this container set num_disks
2341 * to zero, i.e. delete this spare and add raid member devices in
2342 * add_to_super_imsm_volume()
2343 */
2344 if (super->current_vol == 0)
2345 mpb->num_disks = 0;
5a038140
DW
2346
2347 for (i = 0; i < super->current_vol; i++) {
2348 dev = get_imsm_dev(super, i);
2349 if (strncmp((char *) dev->volume, name,
2350 MAX_RAID_SERIAL_LEN) == 0) {
2351 fprintf(stderr, Name": '%s' is already defined for this container\n",
2352 name);
2353 return 0;
2354 }
2355 }
2356
bf5a934a 2357 sprintf(st->subarray, "%d", idx);
ba2de7ba
DW
2358 dv = malloc(sizeof(*dv));
2359 if (!dv) {
2360 fprintf(stderr, Name ": failed to allocate device list entry\n");
2361 return 0;
2362 }
949c47a0
DW
2363 dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
2364 if (!dev) {
ba2de7ba 2365 free(dv);
949c47a0
DW
2366 fprintf(stderr, Name": could not allocate raid device\n");
2367 return 0;
2368 }
c2c087e6 2369 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
03bcbc65
DW
2370 if (info->level == 1)
2371 array_blocks = info_to_blocks_per_member(info);
2372 else
2373 array_blocks = calc_array_size(info->level, info->raid_disks,
2374 info->layout, info->chunk_size,
2375 info->size*2);
c2c087e6
DW
2376 dev->size_low = __cpu_to_le32((__u32) array_blocks);
2377 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
2378 dev->status = __cpu_to_le32(0);
2379 dev->reserved_blocks = __cpu_to_le32(0);
2380 vol = &dev->vol;
2381 vol->migr_state = 0;
1484e727 2382 set_migr_type(dev, MIGR_INIT);
c2c087e6 2383 vol->dirty = 0;
f8f603f1 2384 vol->curr_migr_unit = 0;
a965f303 2385 map = get_imsm_map(dev, 0);
0dcecb2e 2386 map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
fcfd9599 2387 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
ef6ffade 2388 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
0556e1a2 2389 map->failed_disk_num = ~0;
c2c087e6
DW
2390 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
2391 IMSM_T_STATE_NORMAL;
ef6ffade
DW
2392
2393 if (info->level == 1 && info->raid_disks > 2) {
2394 fprintf(stderr, Name": imsm does not support more than 2 disks"
2395 "in a raid1 volume\n");
2396 return 0;
2397 }
4d1313e9 2398 if (info->level == 10) {
c2c087e6 2399 map->raid_level = 1;
4d1313e9
DW
2400 map->num_domains = info->raid_disks / 2;
2401 } else {
c2c087e6 2402 map->raid_level = info->level;
ff596308 2403 map->num_domains = 1;
4d1313e9 2404 }
ff596308
DW
2405 num_data_stripes = info_to_num_data_stripes(info, map->num_domains);
2406 map->num_data_stripes = __cpu_to_le32(num_data_stripes);
ef6ffade 2407
c2c087e6
DW
2408 map->num_members = info->raid_disks;
2409 for (i = 0; i < map->num_members; i++) {
2410 /* initialized in add_to_super */
be73972f 2411 set_imsm_ord_tbl_ent(map, i, 0);
c2c087e6 2412 }
949c47a0 2413 mpb->num_raid_devs++;
ba2de7ba
DW
2414
2415 dv->dev = dev;
2416 dv->index = super->current_vol;
2417 dv->next = super->devlist;
2418 super->devlist = dv;
c2c087e6 2419
4d1313e9
DW
2420 imsm_update_version_info(super);
2421
c2c087e6 2422 return 1;
cdddbdbc
DW
2423}
2424
bf5a934a
DW
2425static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
2426 unsigned long long size, char *name,
2427 char *homehost, int *uuid)
2428{
2429 /* This is primarily called by Create when creating a new array.
2430 * We will then get add_to_super called for each component, and then
2431 * write_init_super called to write it out to each device.
2432 * For IMSM, Create can create on fresh devices or on a pre-existing
2433 * array.
2434 * To create on a pre-existing array a different method will be called.
2435 * This one is just for fresh drives.
2436 */
2437 struct intel_super *super;
2438 struct imsm_super *mpb;
2439 size_t mpb_size;
4d1313e9 2440 char *version;
bf5a934a
DW
2441
2442 if (!info) {
2443 st->sb = NULL;
2444 return 0;
2445 }
2446 if (st->sb)
2447 return init_super_imsm_volume(st, info, size, name, homehost,
2448 uuid);
2449
2450 super = alloc_super(1);
2451 if (!super)
2452 return 0;
2453 mpb_size = disks_to_mpb_size(info->nr_disks);
ef649044 2454 if (posix_memalign(&super->buf, 512, mpb_size) != 0) {
bf5a934a
DW
2455 free(super);
2456 return 0;
2457 }
ef649044 2458 mpb = super->buf;
bf5a934a
DW
2459 memset(mpb, 0, mpb_size);
2460
4d1313e9
DW
2461 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
2462
2463 version = (char *) mpb->sig;
2464 strcpy(version, MPB_SIGNATURE);
2465 version += strlen(MPB_SIGNATURE);
2466 strcpy(version, MPB_VERSION_RAID0);
bf5a934a
DW
2467 mpb->mpb_size = mpb_size;
2468
bf5a934a
DW
2469 st->sb = super;
2470 return 1;
2471}
2472
0e600426 2473#ifndef MDASSEMBLE
f20c3968 2474static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
bf5a934a
DW
2475 int fd, char *devname)
2476{
2477 struct intel_super *super = st->sb;
d23fe947 2478 struct imsm_super *mpb = super->anchor;
bf5a934a
DW
2479 struct dl *dl;
2480 struct imsm_dev *dev;
2481 struct imsm_map *map;
bf5a934a 2482
949c47a0 2483 dev = get_imsm_dev(super, super->current_vol);
a965f303 2484 map = get_imsm_map(dev, 0);
bf5a934a 2485
208933a7
N
2486 if (! (dk->state & (1<<MD_DISK_SYNC))) {
2487 fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
2488 devname);
2489 return 1;
2490 }
2491
efb30e7f
DW
2492 if (fd == -1) {
2493 /* we're doing autolayout so grab the pre-marked (in
2494 * validate_geometry) raid_disk
2495 */
2496 for (dl = super->disks; dl; dl = dl->next)
2497 if (dl->raiddisk == dk->raid_disk)
2498 break;
2499 } else {
2500 for (dl = super->disks; dl ; dl = dl->next)
2501 if (dl->major == dk->major &&
2502 dl->minor == dk->minor)
2503 break;
2504 }
d23fe947 2505
208933a7
N
2506 if (!dl) {
2507 fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
f20c3968 2508 return 1;
208933a7 2509 }
bf5a934a 2510
d23fe947
DW
2511 /* add a pristine spare to the metadata */
2512 if (dl->index < 0) {
2513 dl->index = super->anchor->num_disks;
2514 super->anchor->num_disks++;
2515 }
be73972f 2516 set_imsm_ord_tbl_ent(map, dk->number, dl->index);
f2f27e63 2517 dl->disk.status = CONFIGURED_DISK | USABLE_DISK;
d23fe947
DW
2518
2519 /* if we are creating the first raid device update the family number */
2520 if (super->current_vol == 0) {
2521 __u32 sum;
2522 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
2523 struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
2524
2525 *_dev = *dev;
2526 *_disk = dl->disk;
2527 sum = __gen_imsm_checksum(mpb);
2528 mpb->family_num = __cpu_to_le32(sum);
2529 }
f20c3968
DW
2530
2531 return 0;
bf5a934a
DW
2532}
2533
f20c3968 2534static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
cdddbdbc
DW
2535 int fd, char *devname)
2536{
c2c087e6 2537 struct intel_super *super = st->sb;
c2c087e6
DW
2538 struct dl *dd;
2539 unsigned long long size;
f2f27e63 2540 __u32 id;
c2c087e6
DW
2541 int rv;
2542 struct stat stb;
2543
88c32bb1
DW
2544 /* if we are on an RAID enabled platform check that the disk is
2545 * attached to the raid controller
2546 */
2547 if (super->hba && !disk_attached_to_hba(fd, super->hba)) {
2548 fprintf(stderr,
2549 Name ": %s is not attached to the raid controller: %s\n",
2550 devname ? : "disk", super->hba);
2551 return 1;
2552 }
2553
f20c3968
DW
2554 if (super->current_vol >= 0)
2555 return add_to_super_imsm_volume(st, dk, fd, devname);
bf5a934a 2556
c2c087e6
DW
2557 fstat(fd, &stb);
2558 dd = malloc(sizeof(*dd));
b9f594fe 2559 if (!dd) {
c2c087e6
DW
2560 fprintf(stderr,
2561 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
f20c3968 2562 return 1;
c2c087e6
DW
2563 }
2564 memset(dd, 0, sizeof(*dd));
2565 dd->major = major(stb.st_rdev);
2566 dd->minor = minor(stb.st_rdev);
b9f594fe 2567 dd->index = -1;
c2c087e6 2568 dd->devname = devname ? strdup(devname) : NULL;
c2c087e6 2569 dd->fd = fd;
689c9bf3 2570 dd->e = NULL;
c2c087e6
DW
2571 rv = imsm_read_serial(fd, devname, dd->serial);
2572 if (rv) {
2573 fprintf(stderr,
0030e8d6 2574 Name ": failed to retrieve scsi serial, aborting\n");
949c47a0 2575 free(dd);
0030e8d6 2576 abort();
c2c087e6
DW
2577 }
2578
c2c087e6
DW
2579 get_dev_size(fd, NULL, &size);
2580 size /= 512;
1f24f035 2581 serialcpy(dd->disk.serial, dd->serial);
b9f594fe 2582 dd->disk.total_blocks = __cpu_to_le32(size);
f2f27e63 2583 dd->disk.status = USABLE_DISK | SPARE_DISK;
c2c087e6 2584 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
b9f594fe 2585 dd->disk.scsi_id = __cpu_to_le32(id);
c2c087e6 2586 else
b9f594fe 2587 dd->disk.scsi_id = __cpu_to_le32(0);
43dad3d6
DW
2588
2589 if (st->update_tail) {
2590 dd->next = super->add;
2591 super->add = dd;
2592 } else {
2593 dd->next = super->disks;
2594 super->disks = dd;
2595 }
f20c3968
DW
2596
2597 return 0;
cdddbdbc
DW
2598}
2599
c2c087e6
DW
2600static int store_imsm_mpb(int fd, struct intel_super *super);
2601
d23fe947
DW
2602/* spare records have their own family number and do not have any defined raid
2603 * devices
2604 */
2605static int write_super_imsm_spares(struct intel_super *super, int doclose)
2606{
2607 struct imsm_super mpb_save;
2608 struct imsm_super *mpb = super->anchor;
2609 __u32 sum;
2610 struct dl *d;
2611
2612 mpb_save = *mpb;
2613 mpb->num_raid_devs = 0;
2614 mpb->num_disks = 1;
2615 mpb->mpb_size = sizeof(struct imsm_super);
2616 mpb->generation_num = __cpu_to_le32(1UL);
2617
2618 for (d = super->disks; d; d = d->next) {
8796fdc4 2619 if (d->index != -1)
d23fe947
DW
2620 continue;
2621
2622 mpb->disk[0] = d->disk;
2623 sum = __gen_imsm_checksum(mpb);
2624 mpb->family_num = __cpu_to_le32(sum);
2625 sum = __gen_imsm_checksum(mpb);
2626 mpb->check_sum = __cpu_to_le32(sum);
2627
2628 if (store_imsm_mpb(d->fd, super)) {
2629 fprintf(stderr, "%s: failed for device %d:%d %s\n",
2630 __func__, d->major, d->minor, strerror(errno));
2631 *mpb = mpb_save;
e74255d9 2632 return 1;
d23fe947
DW
2633 }
2634 if (doclose) {
2635 close(d->fd);
2636 d->fd = -1;
2637 }
2638 }
2639
2640 *mpb = mpb_save;
e74255d9 2641 return 0;
d23fe947
DW
2642}
2643
c2c087e6 2644static int write_super_imsm(struct intel_super *super, int doclose)
cdddbdbc 2645{
949c47a0 2646 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
2647 struct dl *d;
2648 __u32 generation;
2649 __u32 sum;
d23fe947 2650 int spares = 0;
949c47a0 2651 int i;
a48ac0a8 2652 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
cdddbdbc 2653
c2c087e6
DW
2654 /* 'generation' is incremented everytime the metadata is written */
2655 generation = __le32_to_cpu(mpb->generation_num);
2656 generation++;
2657 mpb->generation_num = __cpu_to_le32(generation);
2658
1ee1e9fc 2659 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
d23fe947 2660 for (d = super->disks; d; d = d->next) {
8796fdc4 2661 if (d->index == -1)
d23fe947 2662 spares++;
1ee1e9fc 2663 else
d23fe947 2664 mpb->disk[d->index] = d->disk;
d23fe947 2665 }
47ee5a45
DW
2666 for (d = super->missing; d; d = d->next)
2667 mpb->disk[d->index] = d->disk;
b9f594fe 2668
949c47a0
DW
2669 for (i = 0; i < mpb->num_raid_devs; i++) {
2670 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2671
ba2de7ba 2672 imsm_copy_dev(dev, get_imsm_dev(super, i));
a48ac0a8 2673 mpb_size += sizeof_imsm_dev(dev, 0);
949c47a0 2674 }
a48ac0a8
DW
2675 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
2676 mpb->mpb_size = __cpu_to_le32(mpb_size);
949c47a0 2677
c2c087e6 2678 /* recalculate checksum */
949c47a0 2679 sum = __gen_imsm_checksum(mpb);
c2c087e6
DW
2680 mpb->check_sum = __cpu_to_le32(sum);
2681
d23fe947 2682 /* write the mpb for disks that compose raid devices */
c2c087e6 2683 for (d = super->disks; d ; d = d->next) {
d23fe947
DW
2684 if (d->index < 0)
2685 continue;
8796fdc4 2686 if (store_imsm_mpb(d->fd, super))
c2c087e6
DW
2687 fprintf(stderr, "%s: failed for device %d:%d %s\n",
2688 __func__, d->major, d->minor, strerror(errno));
c2c087e6
DW
2689 if (doclose) {
2690 close(d->fd);
2691 d->fd = -1;
2692 }
2693 }
2694
d23fe947
DW
2695 if (spares)
2696 return write_super_imsm_spares(super, doclose);
2697
e74255d9 2698 return 0;
c2c087e6
DW
2699}
2700
0e600426 2701
43dad3d6
DW
2702static int create_array(struct supertype *st)
2703{
2704 size_t len;
2705 struct imsm_update_create_array *u;
2706 struct intel_super *super = st->sb;
2707 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
54c2c1ea
DW
2708 struct imsm_map *map = get_imsm_map(dev, 0);
2709 struct disk_info *inf;
2710 struct imsm_disk *disk;
2711 int i;
2712 int idx;
43dad3d6 2713
54c2c1ea
DW
2714 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
2715 sizeof(*inf) * map->num_members;
43dad3d6
DW
2716 u = malloc(len);
2717 if (!u) {
2718 fprintf(stderr, "%s: failed to allocate update buffer\n",
2719 __func__);
2720 return 1;
2721 }
2722
2723 u->type = update_create_array;
2724 u->dev_idx = super->current_vol;
2725 imsm_copy_dev(&u->dev, dev);
54c2c1ea
DW
2726 inf = get_disk_info(u);
2727 for (i = 0; i < map->num_members; i++) {
2728 idx = get_imsm_disk_idx(dev, i);
2729 disk = get_imsm_disk(super, idx);
2730 serialcpy(inf[i].serial, disk->serial);
2731 }
43dad3d6
DW
2732 append_metadata_update(st, u, len);
2733
2734 return 0;
2735}
2736
7801ac20 2737static int _add_disk(struct supertype *st)
43dad3d6
DW
2738{
2739 struct intel_super *super = st->sb;
2740 size_t len;
2741 struct imsm_update_add_disk *u;
2742
2743 if (!super->add)
2744 return 0;
2745
2746 len = sizeof(*u);
2747 u = malloc(len);
2748 if (!u) {
2749 fprintf(stderr, "%s: failed to allocate update buffer\n",
2750 __func__);
2751 return 1;
2752 }
2753
2754 u->type = update_add_disk;
2755 append_metadata_update(st, u, len);
2756
2757 return 0;
2758}
2759
c2c087e6
DW
2760static int write_init_super_imsm(struct supertype *st)
2761{
8273f55e 2762 if (st->update_tail) {
43dad3d6
DW
2763 /* queue the recently created array / added disk
2764 * as a metadata update */
8273f55e 2765 struct intel_super *super = st->sb;
8273f55e 2766 struct dl *d;
43dad3d6 2767 int rv;
8273f55e 2768
43dad3d6
DW
2769 /* determine if we are creating a volume or adding a disk */
2770 if (super->current_vol < 0) {
2771 /* in the add disk case we are running in mdmon
2772 * context, so don't close fd's
2773 */
7801ac20 2774 return _add_disk(st);
43dad3d6
DW
2775 } else
2776 rv = create_array(st);
8273f55e
DW
2777
2778 for (d = super->disks; d ; d = d->next) {
2779 close(d->fd);
2780 d->fd = -1;
2781 }
2782
43dad3d6 2783 return rv;
8273f55e
DW
2784 } else
2785 return write_super_imsm(st->sb, 1);
cdddbdbc 2786}
0e600426 2787#endif
cdddbdbc
DW
2788
2789static int store_zero_imsm(struct supertype *st, int fd)
2790{
551c80c1 2791 unsigned long long dsize;
6416d527 2792 void *buf;
551c80c1
DW
2793
2794 get_dev_size(fd, NULL, &dsize);
2795
2796 /* first block is stored on second to last sector of the disk */
2797 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
2798 return 1;
2799
ad97895e
DW
2800 if (posix_memalign(&buf, 512, 512) != 0)
2801 return 1;
2802
eb7ea463
DW
2803 memset(buf, 0, 512);
2804 if (write(fd, buf, 512) != 512)
551c80c1 2805 return 1;
cdddbdbc
DW
2806 return 0;
2807}
2808
0e600426
N
2809static int imsm_bbm_log_size(struct imsm_super *mpb)
2810{
2811 return __le32_to_cpu(mpb->bbm_log_size);
2812}
2813
2814#ifndef MDASSEMBLE
cdddbdbc
DW
2815static int validate_geometry_imsm_container(struct supertype *st, int level,
2816 int layout, int raiddisks, int chunk,
c2c087e6 2817 unsigned long long size, char *dev,
2c514b71
NB
2818 unsigned long long *freesize,
2819 int verbose)
cdddbdbc 2820{
c2c087e6
DW
2821 int fd;
2822 unsigned long long ldsize;
88c32bb1 2823 const struct imsm_orom *orom;
cdddbdbc 2824
c2c087e6
DW
2825 if (level != LEVEL_CONTAINER)
2826 return 0;
2827 if (!dev)
2828 return 1;
2829
88c32bb1
DW
2830 if (check_env("IMSM_NO_PLATFORM"))
2831 orom = NULL;
2832 else
2833 orom = find_imsm_orom();
2834 if (orom && raiddisks > orom->tds) {
2835 if (verbose)
2836 fprintf(stderr, Name ": %d exceeds maximum number of"
2837 " platform supported disks: %d\n",
2838 raiddisks, orom->tds);
2839 return 0;
2840 }
2841
c2c087e6
DW
2842 fd = open(dev, O_RDONLY|O_EXCL, 0);
2843 if (fd < 0) {
2c514b71
NB
2844 if (verbose)
2845 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
2846 dev, strerror(errno));
c2c087e6
DW
2847 return 0;
2848 }
2849 if (!get_dev_size(fd, dev, &ldsize)) {
2850 close(fd);
2851 return 0;
2852 }
2853 close(fd);
2854
2855 *freesize = avail_size_imsm(st, ldsize >> 9);
2856
2857 return 1;
cdddbdbc
DW
2858}
2859
0dcecb2e
DW
2860static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
2861{
2862 const unsigned long long base_start = e[*idx].start;
2863 unsigned long long end = base_start + e[*idx].size;
2864 int i;
2865
2866 if (base_start == end)
2867 return 0;
2868
2869 *idx = *idx + 1;
2870 for (i = *idx; i < num_extents; i++) {
2871 /* extend overlapping extents */
2872 if (e[i].start >= base_start &&
2873 e[i].start <= end) {
2874 if (e[i].size == 0)
2875 return 0;
2876 if (e[i].start + e[i].size > end)
2877 end = e[i].start + e[i].size;
2878 } else if (e[i].start > end) {
2879 *idx = i;
2880 break;
2881 }
2882 }
2883
2884 return end - base_start;
2885}
2886
2887static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
2888{
2889 /* build a composite disk with all known extents and generate a new
2890 * 'maxsize' given the "all disks in an array must share a common start
2891 * offset" constraint
2892 */
2893 struct extent *e = calloc(sum_extents, sizeof(*e));
2894 struct dl *dl;
2895 int i, j;
2896 int start_extent;
2897 unsigned long long pos;
b9d77223 2898 unsigned long long start = 0;
0dcecb2e
DW
2899 unsigned long long maxsize;
2900 unsigned long reserve;
2901
2902 if (!e)
2903 return ~0ULL; /* error */
2904
2905 /* coalesce and sort all extents. also, check to see if we need to
2906 * reserve space between member arrays
2907 */
2908 j = 0;
2909 for (dl = super->disks; dl; dl = dl->next) {
2910 if (!dl->e)
2911 continue;
2912 for (i = 0; i < dl->extent_cnt; i++)
2913 e[j++] = dl->e[i];
2914 }
2915 qsort(e, sum_extents, sizeof(*e), cmp_extent);
2916
2917 /* merge extents */
2918 i = 0;
2919 j = 0;
2920 while (i < sum_extents) {
2921 e[j].start = e[i].start;
2922 e[j].size = find_size(e, &i, sum_extents);
2923 j++;
2924 if (e[j-1].size == 0)
2925 break;
2926 }
2927
2928 pos = 0;
2929 maxsize = 0;
2930 start_extent = 0;
2931 i = 0;
2932 do {
2933 unsigned long long esize;
2934
2935 esize = e[i].start - pos;
2936 if (esize >= maxsize) {
2937 maxsize = esize;
2938 start = pos;
2939 start_extent = i;
2940 }
2941 pos = e[i].start + e[i].size;
2942 i++;
2943 } while (e[i-1].size);
2944 free(e);
2945
2946 if (start_extent > 0)
2947 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
2948 else
2949 reserve = 0;
2950
2951 if (maxsize < reserve)
2952 return ~0ULL;
2953
2954 super->create_offset = ~((__u32) 0);
2955 if (start + reserve > super->create_offset)
2956 return ~0ULL; /* start overflows create_offset */
2957 super->create_offset = start + reserve;
2958
2959 return maxsize - reserve;
2960}
2961
88c32bb1
DW
2962static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
2963{
2964 if (level < 0 || level == 6 || level == 4)
2965 return 0;
2966
2967 /* if we have an orom prevent invalid raid levels */
2968 if (orom)
2969 switch (level) {
2970 case 0: return imsm_orom_has_raid0(orom);
2971 case 1:
2972 if (raiddisks > 2)
2973 return imsm_orom_has_raid1e(orom);
1c556e92
DW
2974 return imsm_orom_has_raid1(orom) && raiddisks == 2;
2975 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
2976 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
88c32bb1
DW
2977 }
2978 else
2979 return 1; /* not on an Intel RAID platform so anything goes */
2980
2981 return 0;
2982}
2983
35f81cbb 2984#define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
c2c087e6
DW
2985/* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
2986 * FIX ME add ahci details
2987 */
8b353278
DW
2988static int validate_geometry_imsm_volume(struct supertype *st, int level,
2989 int layout, int raiddisks, int chunk,
c2c087e6 2990 unsigned long long size, char *dev,
2c514b71
NB
2991 unsigned long long *freesize,
2992 int verbose)
cdddbdbc 2993{
c2c087e6
DW
2994 struct stat stb;
2995 struct intel_super *super = st->sb;
a20d2ba5 2996 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
2997 struct dl *dl;
2998 unsigned long long pos = 0;
2999 unsigned long long maxsize;
3000 struct extent *e;
3001 int i;
cdddbdbc 3002
88c32bb1
DW
3003 /* We must have the container info already read in. */
3004 if (!super)
c2c087e6
DW
3005 return 0;
3006
88c32bb1 3007 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
1c556e92
DW
3008 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3009 level, raiddisks, raiddisks > 1 ? "s" : "");
c2c087e6
DW
3010 return 0;
3011 }
78757ce8
DW
3012 if (super->orom && level != 1 &&
3013 !imsm_orom_has_chunk(super->orom, chunk)) {
35f81cbb 3014 pr_vrb(": platform does not support a chunk size of: %d\n", chunk);
c2c087e6 3015 return 0;
88c32bb1
DW
3016 }
3017 if (layout != imsm_level_to_layout(level)) {
3018 if (level == 5)
35f81cbb 3019 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
88c32bb1 3020 else if (level == 10)
35f81cbb 3021 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
88c32bb1 3022 else
35f81cbb 3023 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
88c32bb1 3024 layout, level);
c2c087e6 3025 return 0;
88c32bb1 3026 }
c2c087e6
DW
3027
3028 if (!dev) {
3029 /* General test: make sure there is space for
2da8544a
DW
3030 * 'raiddisks' device extents of size 'size' at a given
3031 * offset
c2c087e6 3032 */
e46273eb 3033 unsigned long long minsize = size;
2da8544a 3034 unsigned long long start_offset = ~0ULL;
c2c087e6
DW
3035 int dcnt = 0;
3036 if (minsize == 0)
3037 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
3038 for (dl = super->disks; dl ; dl = dl->next) {
3039 int found = 0;
3040
bf5a934a 3041 pos = 0;
c2c087e6
DW
3042 i = 0;
3043 e = get_extents(super, dl);
3044 if (!e) continue;
3045 do {
3046 unsigned long long esize;
3047 esize = e[i].start - pos;
3048 if (esize >= minsize)
3049 found = 1;
2da8544a
DW
3050 if (found && start_offset == ~0ULL) {
3051 start_offset = pos;
3052 break;
3053 } else if (found && pos != start_offset) {
3054 found = 0;
3055 break;
3056 }
c2c087e6
DW
3057 pos = e[i].start + e[i].size;
3058 i++;
3059 } while (e[i-1].size);
3060 if (found)
3061 dcnt++;
3062 free(e);
3063 }
3064 if (dcnt < raiddisks) {
2c514b71
NB
3065 if (verbose)
3066 fprintf(stderr, Name ": imsm: Not enough "
3067 "devices with space for this array "
3068 "(%d < %d)\n",
3069 dcnt, raiddisks);
c2c087e6
DW
3070 return 0;
3071 }
3072 return 1;
3073 }
0dcecb2e 3074
c2c087e6
DW
3075 /* This device must be a member of the set */
3076 if (stat(dev, &stb) < 0)
3077 return 0;
3078 if ((S_IFMT & stb.st_mode) != S_IFBLK)
3079 return 0;
3080 for (dl = super->disks ; dl ; dl = dl->next) {
3081 if (dl->major == major(stb.st_rdev) &&
3082 dl->minor == minor(stb.st_rdev))
3083 break;
3084 }
3085 if (!dl) {
2c514b71
NB
3086 if (verbose)
3087 fprintf(stderr, Name ": %s is not in the "
3088 "same imsm set\n", dev);
c2c087e6 3089 return 0;
a20d2ba5
DW
3090 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
3091 /* If a volume is present then the current creation attempt
3092 * cannot incorporate new spares because the orom may not
3093 * understand this configuration (all member disks must be
3094 * members of each array in the container).
3095 */
3096 fprintf(stderr, Name ": %s is a spare and a volume"
3097 " is already defined for this container\n", dev);
3098 fprintf(stderr, Name ": The option-rom requires all member"
3099 " disks to be a member of all volumes\n");
3100 return 0;
c2c087e6 3101 }
0dcecb2e
DW
3102
3103 /* retrieve the largest free space block */
c2c087e6
DW
3104 e = get_extents(super, dl);
3105 maxsize = 0;
3106 i = 0;
0dcecb2e
DW
3107 if (e) {
3108 do {
3109 unsigned long long esize;
3110
3111 esize = e[i].start - pos;
3112 if (esize >= maxsize)
3113 maxsize = esize;
3114 pos = e[i].start + e[i].size;
3115 i++;
3116 } while (e[i-1].size);
3117 dl->e = e;
3118 dl->extent_cnt = i;
3119 } else {
3120 if (verbose)
3121 fprintf(stderr, Name ": unable to determine free space for: %s\n",
3122 dev);
3123 return 0;
3124 }
3125 if (maxsize < size) {
3126 if (verbose)
3127 fprintf(stderr, Name ": %s not enough space (%llu < %llu)\n",
3128 dev, maxsize, size);
3129 return 0;
3130 }
3131
3132 /* count total number of extents for merge */
3133 i = 0;
3134 for (dl = super->disks; dl; dl = dl->next)
3135 if (dl->e)
3136 i += dl->extent_cnt;
3137
3138 maxsize = merge_extents(super, i);
3139 if (maxsize < size) {
3140 if (verbose)
3141 fprintf(stderr, Name ": not enough space after merge (%llu < %llu)\n",
3142 maxsize, size);
3143 return 0;
3144 } else if (maxsize == ~0ULL) {
3145 if (verbose)
3146 fprintf(stderr, Name ": failed to merge %d extents\n", i);
3147 return 0;
3148 }
3149
c2c087e6
DW
3150 *freesize = maxsize;
3151
3152 return 1;
cdddbdbc
DW
3153}
3154
efb30e7f
DW
3155static int reserve_space(struct supertype *st, int raiddisks,
3156 unsigned long long size, int chunk,
3157 unsigned long long *freesize)
3158{
3159 struct intel_super *super = st->sb;
3160 struct imsm_super *mpb = super->anchor;
3161 struct dl *dl;
3162 int i;
3163 int extent_cnt;
3164 struct extent *e;
3165 unsigned long long maxsize;
3166 unsigned long long minsize;
3167 int cnt;
3168 int used;
3169
3170 /* find the largest common start free region of the possible disks */
3171 used = 0;
3172 extent_cnt = 0;
3173 cnt = 0;
3174 for (dl = super->disks; dl; dl = dl->next) {
3175 dl->raiddisk = -1;
3176
3177 if (dl->index >= 0)
3178 used++;
3179
3180 /* don't activate new spares if we are orom constrained
3181 * and there is already a volume active in the container
3182 */
3183 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
3184 continue;
3185
3186 e = get_extents(super, dl);
3187 if (!e)
3188 continue;
3189 for (i = 1; e[i-1].size; i++)
3190 ;
3191 dl->e = e;
3192 dl->extent_cnt = i;
3193 extent_cnt += i;
3194 cnt++;
3195 }
3196
3197 maxsize = merge_extents(super, extent_cnt);
3198 minsize = size;
3199 if (size == 0)
3200 minsize = chunk;
3201
3202 if (cnt < raiddisks ||
3203 (super->orom && used && used != raiddisks) ||
3204 maxsize < minsize) {
3205 fprintf(stderr, Name ": not enough devices with space to create array.\n");
3206 return 0; /* No enough free spaces large enough */
3207 }
3208
3209 if (size == 0) {
3210 size = maxsize;
3211 if (chunk) {
3212 size /= chunk;
3213 size *= chunk;
3214 }
3215 }
3216
3217 cnt = 0;
3218 for (dl = super->disks; dl; dl = dl->next)
3219 if (dl->e)
3220 dl->raiddisk = cnt++;
3221
3222 *freesize = size;
3223
3224 return 1;
3225}
3226
bf5a934a
DW
3227static int validate_geometry_imsm(struct supertype *st, int level, int layout,
3228 int raiddisks, int chunk, unsigned long long size,
3229 char *dev, unsigned long long *freesize,
3230 int verbose)
3231{
3232 int fd, cfd;
3233 struct mdinfo *sra;
3234
3235 /* if given unused devices create a container
3236 * if given given devices in a container create a member volume
3237 */
3238 if (level == LEVEL_CONTAINER) {
3239 /* Must be a fresh device to add to a container */
3240 return validate_geometry_imsm_container(st, level, layout,
3241 raiddisks, chunk, size,
3242 dev, freesize,
3243 verbose);
3244 }
3245
8592f29d
N
3246 if (!dev) {
3247 if (st->sb && freesize) {
efb30e7f
DW
3248 /* we are being asked to automatically layout a
3249 * new volume based on the current contents of
3250 * the container. If the the parameters can be
3251 * satisfied reserve_space will record the disks,
3252 * start offset, and size of the volume to be
3253 * created. add_to_super and getinfo_super
3254 * detect when autolayout is in progress.
3255 */
3256 return reserve_space(st, raiddisks, size, chunk, freesize);
8592f29d
N
3257 }
3258 return 1;
3259 }
bf5a934a
DW
3260 if (st->sb) {
3261 /* creating in a given container */
3262 return validate_geometry_imsm_volume(st, level, layout,
3263 raiddisks, chunk, size,
3264 dev, freesize, verbose);
3265 }
3266
3267 /* limit creation to the following levels */
3268 if (!dev)
3269 switch (level) {
3270 case 0:
3271 case 1:
3272 case 10:
3273 case 5:
3274 break;
3275 default:
3276 return 1;
3277 }
3278
3279 /* This device needs to be a device in an 'imsm' container */
3280 fd = open(dev, O_RDONLY|O_EXCL, 0);
3281 if (fd >= 0) {
3282 if (verbose)
3283 fprintf(stderr,
3284 Name ": Cannot create this array on device %s\n",
3285 dev);
3286 close(fd);
3287 return 0;
3288 }
3289 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
3290 if (verbose)
3291 fprintf(stderr, Name ": Cannot open %s: %s\n",
3292 dev, strerror(errno));
3293 return 0;
3294 }
3295 /* Well, it is in use by someone, maybe an 'imsm' container. */
3296 cfd = open_container(fd);
3297 if (cfd < 0) {
3298 close(fd);
3299 if (verbose)
3300 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
3301 dev);
3302 return 0;
3303 }
3304 sra = sysfs_read(cfd, 0, GET_VERSION);
3305 close(fd);
3306 if (sra && sra->array.major_version == -1 &&
3307 strcmp(sra->text_version, "imsm") == 0) {
3308 /* This is a member of a imsm container. Load the container
3309 * and try to create a volume
3310 */
3311 struct intel_super *super;
3312
3313 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, 1) == 0) {
3314 st->sb = super;
3315 st->container_dev = fd2devnum(cfd);
3316 close(cfd);
3317 return validate_geometry_imsm_volume(st, level, layout,
3318 raiddisks, chunk,
3319 size, dev,
3320 freesize, verbose);
3321 }
3322 close(cfd);
3323 } else /* may belong to another container */
3324 return 0;
3325
3326 return 1;
3327}
0e600426 3328#endif /* MDASSEMBLE */
bf5a934a 3329
cdddbdbc
DW
3330static struct mdinfo *container_content_imsm(struct supertype *st)
3331{
4f5bc454
DW
3332 /* Given a container loaded by load_super_imsm_all,
3333 * extract information about all the arrays into
3334 * an mdinfo tree.
3335 *
3336 * For each imsm_dev create an mdinfo, fill it in,
3337 * then look for matching devices in super->disks
3338 * and create appropriate device mdinfo.
3339 */
3340 struct intel_super *super = st->sb;
949c47a0 3341 struct imsm_super *mpb = super->anchor;
4f5bc454
DW
3342 struct mdinfo *rest = NULL;
3343 int i;
cdddbdbc 3344
604b746f
JD
3345 /* do not assemble arrays that might have bad blocks */
3346 if (imsm_bbm_log_size(super->anchor)) {
3347 fprintf(stderr, Name ": BBM log found in metadata. "
3348 "Cannot activate array(s).\n");
3349 return NULL;
3350 }
3351
4f5bc454 3352 for (i = 0; i < mpb->num_raid_devs; i++) {
949c47a0 3353 struct imsm_dev *dev = get_imsm_dev(super, i);
a965f303 3354 struct imsm_map *map = get_imsm_map(dev, 0);
4f5bc454 3355 struct mdinfo *this;
4f5bc454
DW
3356 int slot;
3357
1ce0101c
DW
3358 /* do not publish arrays that are in the middle of an
3359 * unsupported migration
3360 */
3361 if (dev->vol.migr_state &&
3362 (migr_type(dev) == MIGR_GEN_MIGR ||
3363 migr_type(dev) == MIGR_STATE_CHANGE)) {
3364 fprintf(stderr, Name ": cannot assemble volume '%.16s':"
3365 " unsupported migration in progress\n",
3366 dev->volume);
3367 continue;
3368 }
3369
4f5bc454
DW
3370 this = malloc(sizeof(*this));
3371 memset(this, 0, sizeof(*this));
3372 this->next = rest;
4f5bc454 3373
301406c9
DW
3374 super->current_vol = i;
3375 getinfo_super_imsm_volume(st, this);
4f5bc454 3376 for (slot = 0 ; slot < map->num_members; slot++) {
4f5bc454
DW
3377 struct mdinfo *info_d;
3378 struct dl *d;
3379 int idx;
9a1608e5 3380 int skip;
4f5bc454 3381 __u32 s;
7eef0453 3382 __u32 ord;
4f5bc454 3383
9a1608e5 3384 skip = 0;
ff077194 3385 idx = get_imsm_disk_idx(dev, slot);
7eef0453 3386 ord = get_imsm_ord_tbl_ent(dev, slot);
4f5bc454
DW
3387 for (d = super->disks; d ; d = d->next)
3388 if (d->index == idx)
3389 break;
3390
3391 if (d == NULL)
9a1608e5
DW
3392 skip = 1;
3393
f2f27e63 3394 s = d ? d->disk.status : 0;
9a1608e5
DW
3395 if (s & FAILED_DISK)
3396 skip = 1;
3397 if (!(s & USABLE_DISK))
3398 skip = 1;
7eef0453
DW
3399 if (ord & IMSM_ORD_REBUILD)
3400 skip = 1;
9a1608e5
DW
3401
3402 /*
3403 * if we skip some disks the array will be assmebled degraded;
3404 * reset resync start to avoid a dirty-degraded situation
3405 *
3406 * FIXME handle dirty degraded
3407 */
3408 if (skip && !dev->vol.dirty)
3409 this->resync_start = ~0ULL;
3410 if (skip)
3411 continue;
4f5bc454
DW
3412
3413 info_d = malloc(sizeof(*info_d));
9a1608e5
DW
3414 if (!info_d) {
3415 fprintf(stderr, Name ": failed to allocate disk"
1ce0101c 3416 " for volume %.16s\n", dev->volume);
9a1608e5
DW
3417 free(this);
3418 this = rest;
3419 break;
3420 }
4f5bc454
DW
3421 memset(info_d, 0, sizeof(*info_d));
3422 info_d->next = this->devs;
3423 this->devs = info_d;
3424
4f5bc454
DW
3425 info_d->disk.number = d->index;
3426 info_d->disk.major = d->major;
3427 info_d->disk.minor = d->minor;
3428 info_d->disk.raid_disk = slot;
4f5bc454
DW
3429
3430 this->array.working_disks++;
3431
3432 info_d->events = __le32_to_cpu(mpb->generation_num);
3433 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
3434 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
3435 if (d->devname)
3436 strcpy(info_d->name, d->devname);
3437 }
9a1608e5 3438 rest = this;
4f5bc454
DW
3439 }
3440
3441 return rest;
cdddbdbc
DW
3442}
3443
845dea95 3444
0e600426 3445#ifndef MDASSEMBLE
cba0191b
NB
3446static int imsm_open_new(struct supertype *c, struct active_array *a,
3447 char *inst)
845dea95 3448{
0372d5a2 3449 struct intel_super *super = c->sb;
949c47a0 3450 struct imsm_super *mpb = super->anchor;
0372d5a2 3451
949c47a0 3452 if (atoi(inst) >= mpb->num_raid_devs) {
0372d5a2
DW
3453 fprintf(stderr, "%s: subarry index %d, out of range\n",
3454 __func__, atoi(inst));
3455 return -ENODEV;
3456 }
3457
4e6e574a 3458 dprintf("imsm: open_new %s\n", inst);
cba0191b 3459 a->info.container_member = atoi(inst);
845dea95
NB
3460 return 0;
3461}
3462
fb49eef2 3463static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed)
c2a1e7da 3464{
a965f303 3465 struct imsm_map *map = get_imsm_map(dev, 0);
c2a1e7da
DW
3466
3467 if (!failed)
3393c6af
DW
3468 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
3469 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
c2a1e7da
DW
3470
3471 switch (get_imsm_raid_level(map)) {
3472 case 0:
3473 return IMSM_T_STATE_FAILED;
3474 break;
3475 case 1:
3476 if (failed < map->num_members)
3477 return IMSM_T_STATE_DEGRADED;
3478 else
3479 return IMSM_T_STATE_FAILED;
3480 break;
3481 case 10:
3482 {
3483 /**
c92a2527
DW
3484 * check to see if any mirrors have failed, otherwise we
3485 * are degraded. Even numbered slots are mirrored on
3486 * slot+1
c2a1e7da 3487 */
c2a1e7da 3488 int i;
d9b420a5
N
3489 /* gcc -Os complains that this is unused */
3490 int insync = insync;
c2a1e7da
DW
3491
3492 for (i = 0; i < map->num_members; i++) {
c92a2527
DW
3493 __u32 ord = get_imsm_ord_tbl_ent(dev, i);
3494 int idx = ord_to_idx(ord);
3495 struct imsm_disk *disk;
c2a1e7da 3496
c92a2527
DW
3497 /* reset the potential in-sync count on even-numbered
3498 * slots. num_copies is always 2 for imsm raid10
3499 */
3500 if ((i & 1) == 0)
3501 insync = 2;
c2a1e7da 3502
c92a2527 3503 disk = get_imsm_disk(super, idx);
f2f27e63 3504 if (!disk || disk->status & FAILED_DISK ||
c92a2527
DW
3505 ord & IMSM_ORD_REBUILD)
3506 insync--;
c2a1e7da 3507
c92a2527
DW
3508 /* no in-sync disks left in this mirror the
3509 * array has failed
3510 */
3511 if (insync == 0)
3512 return IMSM_T_STATE_FAILED;
c2a1e7da
DW
3513 }
3514
3515 return IMSM_T_STATE_DEGRADED;
3516 }
3517 case 5:
3518 if (failed < 2)
3519 return IMSM_T_STATE_DEGRADED;
3520 else
3521 return IMSM_T_STATE_FAILED;
3522 break;
3523 default:
3524 break;
3525 }
3526
3527 return map->map_state;
3528}
3529
ff077194 3530static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev)
c2a1e7da
DW
3531{
3532 int i;
3533 int failed = 0;
3534 struct imsm_disk *disk;
ff077194 3535 struct imsm_map *map = get_imsm_map(dev, 0);
0556e1a2
DW
3536 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
3537 __u32 ord;
3538 int idx;
c2a1e7da 3539
0556e1a2
DW
3540 /* at the beginning of migration we set IMSM_ORD_REBUILD on
3541 * disks that are being rebuilt. New failures are recorded to
3542 * map[0]. So we look through all the disks we started with and
3543 * see if any failures are still present, or if any new ones
3544 * have arrived
3545