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