introduce --detail-platform to display platform raid capabilities
[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>
28
29/* MPB == Metadata Parameter Block */
30#define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
31#define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
32#define MPB_VERSION_RAID0 "1.0.00"
33#define MPB_VERSION_RAID1 "1.1.00"
fe7ed8cb
DW
34#define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
35#define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
cdddbdbc 36#define MPB_VERSION_RAID5 "1.2.02"
fe7ed8cb
DW
37#define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
38#define MPB_VERSION_CNG "1.2.06"
39#define MPB_VERSION_ATTRIBS "1.3.00"
cdddbdbc
DW
40#define MAX_SIGNATURE_LENGTH 32
41#define MAX_RAID_SERIAL_LEN 16
fe7ed8cb
DW
42
43#define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
44#define MPB_ATTRIB_PM __cpu_to_le32(0x40000000)
45#define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
46#define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
47#define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
48#define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
49#define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
50#define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
51#define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
52
c2c087e6
DW
53#define MPB_SECTOR_CNT 418
54#define IMSM_RESERVED_SECTORS 4096
cdddbdbc
DW
55
56/* Disk configuration info. */
57#define IMSM_MAX_DEVICES 255
58struct imsm_disk {
59 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
60 __u32 total_blocks; /* 0xE8 - 0xEB total blocks */
61 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
f2f27e63
DW
62#define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
63#define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
64#define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
65#define USABLE_DISK __cpu_to_le32(0x08) /* Fully usable unless FAILED_DISK is set */
cdddbdbc 66 __u32 status; /* 0xF0 - 0xF3 */
fe7ed8cb
DW
67 __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
68#define IMSM_DISK_FILLERS 4
cdddbdbc
DW
69 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */
70};
71
72/* RAID map configuration infos. */
73struct imsm_map {
74 __u32 pba_of_lba0; /* start address of partition */
75 __u32 blocks_per_member;/* blocks per member */
76 __u32 num_data_stripes; /* number of data stripes */
77 __u16 blocks_per_strip;
78 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
79#define IMSM_T_STATE_NORMAL 0
80#define IMSM_T_STATE_UNINITIALIZED 1
e3bba0e0
DW
81#define IMSM_T_STATE_DEGRADED 2
82#define IMSM_T_STATE_FAILED 3
cdddbdbc
DW
83 __u8 raid_level;
84#define IMSM_T_RAID0 0
85#define IMSM_T_RAID1 1
86#define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
87 __u8 num_members; /* number of member disks */
fe7ed8cb
DW
88 __u8 num_domains; /* number of parity domains */
89 __u8 failed_disk_num; /* valid only when state is degraded */
90 __u8 reserved[1];
cdddbdbc 91 __u32 filler[7]; /* expansion area */
7eef0453 92#define IMSM_ORD_REBUILD (1 << 24)
cdddbdbc 93 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
7eef0453
DW
94 * top byte contains some flags
95 */
cdddbdbc
DW
96} __attribute__ ((packed));
97
98struct imsm_vol {
f8f603f1 99 __u32 curr_migr_unit;
fe7ed8cb 100 __u32 checkpoint_id; /* id to access curr_migr_unit */
cdddbdbc 101 __u8 migr_state; /* Normal or Migrating */
e3bba0e0
DW
102#define MIGR_INIT 0
103#define MIGR_REBUILD 1
104#define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
105#define MIGR_GEN_MIGR 3
106#define MIGR_STATE_CHANGE 4
cdddbdbc
DW
107 __u8 migr_type; /* Initializing, Rebuilding, ... */
108 __u8 dirty;
fe7ed8cb
DW
109 __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
110 __u16 verify_errors; /* number of mismatches */
111 __u16 bad_blocks; /* number of bad blocks during verify */
112 __u32 filler[4];
cdddbdbc
DW
113 struct imsm_map map[1];
114 /* here comes another one if migr_state */
115} __attribute__ ((packed));
116
117struct imsm_dev {
fe7ed8cb 118 __u8 volume[MAX_RAID_SERIAL_LEN];
cdddbdbc
DW
119 __u32 size_low;
120 __u32 size_high;
fe7ed8cb
DW
121#define DEV_BOOTABLE __cpu_to_le32(0x01)
122#define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
123#define DEV_READ_COALESCING __cpu_to_le32(0x04)
124#define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
125#define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
126#define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
127#define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
128#define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
129#define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
130#define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
131#define DEV_CLONE_N_GO __cpu_to_le32(0x400)
132#define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
133#define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
cdddbdbc
DW
134 __u32 status; /* Persistent RaidDev status */
135 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
fe7ed8cb
DW
136 __u8 migr_priority;
137 __u8 num_sub_vols;
138 __u8 tid;
139 __u8 cng_master_disk;
140 __u16 cache_policy;
141 __u8 cng_state;
142 __u8 cng_sub_state;
143#define IMSM_DEV_FILLERS 10
cdddbdbc
DW
144 __u32 filler[IMSM_DEV_FILLERS];
145 struct imsm_vol vol;
146} __attribute__ ((packed));
147
148struct imsm_super {
149 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
150 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
151 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
152 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
153 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
604b746f
JD
154 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
155 __u32 attributes; /* 0x34 - 0x37 */
cdddbdbc
DW
156 __u8 num_disks; /* 0x38 Number of configured disks */
157 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
604b746f
JD
158 __u8 error_log_pos; /* 0x3A */
159 __u8 fill[1]; /* 0x3B */
160 __u32 cache_size; /* 0x3c - 0x40 in mb */
161 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
162 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
163 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
164#define IMSM_FILLERS 35
165 __u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
cdddbdbc
DW
166 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
167 /* here comes imsm_dev[num_raid_devs] */
604b746f 168 /* here comes BBM logs */
cdddbdbc
DW
169} __attribute__ ((packed));
170
604b746f
JD
171#define BBM_LOG_MAX_ENTRIES 254
172
173struct bbm_log_entry {
174 __u64 defective_block_start;
175#define UNREADABLE 0xFFFFFFFF
176 __u32 spare_block_offset;
177 __u16 remapped_marked_count;
178 __u16 disk_ordinal;
179} __attribute__ ((__packed__));
180
181struct bbm_log {
182 __u32 signature; /* 0xABADB10C */
183 __u32 entry_count;
184 __u32 reserved_spare_block_count; /* 0 */
185 __u32 reserved; /* 0xFFFF */
186 __u64 first_spare_lba;
187 struct bbm_log_entry mapped_block_entries[BBM_LOG_MAX_ENTRIES];
188} __attribute__ ((__packed__));
189
190
cdddbdbc
DW
191#ifndef MDASSEMBLE
192static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
193#endif
194
87eb16df 195static unsigned int sector_count(__u32 bytes)
cdddbdbc 196{
87eb16df
DW
197 return ((bytes + (512-1)) & (~(512-1))) / 512;
198}
cdddbdbc 199
87eb16df
DW
200static unsigned int mpb_sectors(struct imsm_super *mpb)
201{
202 return sector_count(__le32_to_cpu(mpb->mpb_size));
cdddbdbc
DW
203}
204
205/* internal representation of IMSM metadata */
206struct intel_super {
207 union {
949c47a0
DW
208 void *buf; /* O_DIRECT buffer for reading/writing metadata */
209 struct imsm_super *anchor; /* immovable parameters */
cdddbdbc 210 };
949c47a0 211 size_t len; /* size of the 'buf' allocation */
4d7b1503
DW
212 void *next_buf; /* for realloc'ing buf from the manager */
213 size_t next_len;
c2c087e6
DW
214 int updates_pending; /* count of pending updates for mdmon */
215 int creating_imsm; /* flag to indicate container creation */
bf5a934a 216 int current_vol; /* index of raid device undergoing creation */
0dcecb2e 217 __u32 create_offset; /* common start for 'current_vol' */
949c47a0
DW
218 #define IMSM_MAX_RAID_DEVS 2
219 struct imsm_dev *dev_tbl[IMSM_MAX_RAID_DEVS];
cdddbdbc
DW
220 struct dl {
221 struct dl *next;
222 int index;
223 __u8 serial[MAX_RAID_SERIAL_LEN];
224 int major, minor;
225 char *devname;
b9f594fe 226 struct imsm_disk disk;
cdddbdbc 227 int fd;
0dcecb2e
DW
228 int extent_cnt;
229 struct extent *e; /* for determining freespace @ create */
cdddbdbc 230 } *disks;
43dad3d6 231 struct dl *add; /* list of disks to add while mdmon active */
47ee5a45 232 struct dl *missing; /* disks removed while we weren't looking */
43dad3d6 233 struct bbm_log *bbm_log;
88c32bb1
DW
234 const char *hba; /* device path of the raid controller for this metadata */
235 const struct imsm_orom *orom; /* platform firmware support */
cdddbdbc
DW
236};
237
c2c087e6
DW
238struct extent {
239 unsigned long long start, size;
240};
241
88758e9d
DW
242/* definition of messages passed to imsm_process_update */
243enum imsm_update_type {
244 update_activate_spare,
8273f55e 245 update_create_array,
43dad3d6 246 update_add_disk,
88758e9d
DW
247};
248
249struct imsm_update_activate_spare {
250 enum imsm_update_type type;
d23fe947 251 struct dl *dl;
88758e9d
DW
252 int slot;
253 int array;
254 struct imsm_update_activate_spare *next;
255};
256
54c2c1ea
DW
257struct disk_info {
258 __u8 serial[MAX_RAID_SERIAL_LEN];
259};
260
8273f55e
DW
261struct imsm_update_create_array {
262 enum imsm_update_type type;
8273f55e 263 int dev_idx;
6a3e913e 264 struct imsm_dev dev;
8273f55e
DW
265};
266
43dad3d6
DW
267struct imsm_update_add_disk {
268 enum imsm_update_type type;
269};
270
cdddbdbc
DW
271static struct supertype *match_metadata_desc_imsm(char *arg)
272{
273 struct supertype *st;
274
275 if (strcmp(arg, "imsm") != 0 &&
276 strcmp(arg, "default") != 0
277 )
278 return NULL;
279
280 st = malloc(sizeof(*st));
ef609477 281 memset(st, 0, sizeof(*st));
cdddbdbc
DW
282 st->ss = &super_imsm;
283 st->max_devs = IMSM_MAX_DEVICES;
284 st->minor_version = 0;
285 st->sb = NULL;
286 return st;
287}
288
0e600426 289#ifndef MDASSEMBLE
cdddbdbc
DW
290static __u8 *get_imsm_version(struct imsm_super *mpb)
291{
292 return &mpb->sig[MPB_SIG_LEN];
293}
0e600426 294#endif
cdddbdbc 295
949c47a0
DW
296/* retrieve a disk directly from the anchor when the anchor is known to be
297 * up-to-date, currently only at load time
298 */
299static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
cdddbdbc 300{
949c47a0 301 if (index >= mpb->num_disks)
cdddbdbc
DW
302 return NULL;
303 return &mpb->disk[index];
304}
305
0e600426 306#ifndef MDASSEMBLE
b9f594fe 307/* retrieve a disk from the parsed metadata */
949c47a0
DW
308static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
309{
b9f594fe
DW
310 struct dl *d;
311
312 for (d = super->disks; d; d = d->next)
313 if (d->index == index)
314 return &d->disk;
315
316 return NULL;
949c47a0 317}
0e600426 318#endif
949c47a0
DW
319
320/* generate a checksum directly from the anchor when the anchor is known to be
321 * up-to-date, currently only at load or write_super after coalescing
322 */
323static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
cdddbdbc
DW
324{
325 __u32 end = mpb->mpb_size / sizeof(end);
326 __u32 *p = (__u32 *) mpb;
327 __u32 sum = 0;
328
97f734fd
N
329 while (end--) {
330 sum += __le32_to_cpu(*p);
331 p++;
332 }
cdddbdbc
DW
333
334 return sum - __le32_to_cpu(mpb->check_sum);
335}
336
a965f303
DW
337static size_t sizeof_imsm_map(struct imsm_map *map)
338{
339 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
340}
341
342struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
cdddbdbc 343{
a965f303
DW
344 struct imsm_map *map = &dev->vol.map[0];
345
346 if (second_map && !dev->vol.migr_state)
347 return NULL;
348 else if (second_map) {
349 void *ptr = map;
350
351 return ptr + sizeof_imsm_map(map);
352 } else
353 return map;
354
355}
cdddbdbc 356
3393c6af
DW
357/* return the size of the device.
358 * migr_state increases the returned size if map[0] were to be duplicated
359 */
360static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
a965f303
DW
361{
362 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
363 sizeof_imsm_map(get_imsm_map(dev, 0));
cdddbdbc
DW
364
365 /* migrating means an additional map */
a965f303
DW
366 if (dev->vol.migr_state)
367 size += sizeof_imsm_map(get_imsm_map(dev, 1));
3393c6af
DW
368 else if (migr_state)
369 size += sizeof_imsm_map(get_imsm_map(dev, 0));
cdddbdbc
DW
370
371 return size;
372}
373
54c2c1ea
DW
374#ifndef MDASSEMBLE
375/* retrieve disk serial number list from a metadata update */
376static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
377{
378 void *u = update;
379 struct disk_info *inf;
380
381 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
382 sizeof_imsm_dev(&update->dev, 0);
383
384 return inf;
385}
386#endif
387
949c47a0 388static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
cdddbdbc
DW
389{
390 int offset;
391 int i;
392 void *_mpb = mpb;
393
949c47a0 394 if (index >= mpb->num_raid_devs)
cdddbdbc
DW
395 return NULL;
396
397 /* devices start after all disks */
398 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
399
400 for (i = 0; i <= index; i++)
401 if (i == index)
402 return _mpb + offset;
403 else
3393c6af 404 offset += sizeof_imsm_dev(_mpb + offset, 0);
cdddbdbc
DW
405
406 return NULL;
407}
408
949c47a0
DW
409static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
410{
411 if (index >= super->anchor->num_raid_devs)
412 return NULL;
413 return super->dev_tbl[index];
414}
415
7eef0453
DW
416static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, int slot)
417{
418 struct imsm_map *map;
419
420 if (dev->vol.migr_state)
7eef0453 421 map = get_imsm_map(dev, 1);
fb9bf0d3
DW
422 else
423 map = get_imsm_map(dev, 0);
7eef0453 424
ff077194
DW
425 /* top byte identifies disk under rebuild */
426 return __le32_to_cpu(map->disk_ord_tbl[slot]);
427}
428
429#define ord_to_idx(ord) (((ord) << 8) >> 8)
430static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot)
431{
432 __u32 ord = get_imsm_ord_tbl_ent(dev, slot);
433
434 return ord_to_idx(ord);
7eef0453
DW
435}
436
be73972f
DW
437static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
438{
439 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
440}
441
cdddbdbc
DW
442static int get_imsm_raid_level(struct imsm_map *map)
443{
444 if (map->raid_level == 1) {
445 if (map->num_members == 2)
446 return 1;
447 else
448 return 10;
449 }
450
451 return map->raid_level;
452}
453
c2c087e6
DW
454static int cmp_extent(const void *av, const void *bv)
455{
456 const struct extent *a = av;
457 const struct extent *b = bv;
458 if (a->start < b->start)
459 return -1;
460 if (a->start > b->start)
461 return 1;
462 return 0;
463}
464
0dcecb2e 465static int count_memberships(struct dl *dl, struct intel_super *super)
c2c087e6 466{
c2c087e6 467 int memberships = 0;
0dcecb2e 468 int i, j;
c2c087e6 469
949c47a0
DW
470 for (i = 0; i < super->anchor->num_raid_devs; i++) {
471 struct imsm_dev *dev = get_imsm_dev(super, i);
a965f303 472 struct imsm_map *map = get_imsm_map(dev, 0);
c2c087e6
DW
473
474 for (j = 0; j < map->num_members; j++) {
ff077194 475 __u32 index = get_imsm_disk_idx(dev, j);
c2c087e6
DW
476
477 if (index == dl->index)
478 memberships++;
479 }
480 }
0dcecb2e
DW
481
482 return memberships;
483}
484
485static struct extent *get_extents(struct intel_super *super, struct dl *dl)
486{
487 /* find a list of used extents on the given physical device */
488 struct extent *rv, *e;
489 int i, j;
490 int memberships = count_memberships(dl, super);
491 __u32 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
492
c2c087e6
DW
493 rv = malloc(sizeof(struct extent) * (memberships + 1));
494 if (!rv)
495 return NULL;
496 e = rv;
497
949c47a0
DW
498 for (i = 0; i < super->anchor->num_raid_devs; i++) {
499 struct imsm_dev *dev = get_imsm_dev(super, i);
a965f303 500 struct imsm_map *map = get_imsm_map(dev, 0);
c2c087e6
DW
501
502 for (j = 0; j < map->num_members; j++) {
ff077194 503 __u32 index = get_imsm_disk_idx(dev, j);
c2c087e6
DW
504
505 if (index == dl->index) {
506 e->start = __le32_to_cpu(map->pba_of_lba0);
507 e->size = __le32_to_cpu(map->blocks_per_member);
508 e++;
509 }
510 }
511 }
512 qsort(rv, memberships, sizeof(*rv), cmp_extent);
513
14e8215b
DW
514 /* determine the start of the metadata
515 * when no raid devices are defined use the default
516 * ...otherwise allow the metadata to truncate the value
517 * as is the case with older versions of imsm
518 */
519 if (memberships) {
520 struct extent *last = &rv[memberships - 1];
521 __u32 remainder;
522
523 remainder = __le32_to_cpu(dl->disk.total_blocks) -
524 (last->start + last->size);
dda5855f
DW
525 /* round down to 1k block to satisfy precision of the kernel
526 * 'size' interface
527 */
528 remainder &= ~1UL;
529 /* make sure remainder is still sane */
530 if (remainder < ROUND_UP(super->len, 512) >> 9)
531 remainder = ROUND_UP(super->len, 512) >> 9;
14e8215b
DW
532 if (reservation > remainder)
533 reservation = remainder;
534 }
535 e->start = __le32_to_cpu(dl->disk.total_blocks) - reservation;
c2c087e6
DW
536 e->size = 0;
537 return rv;
538}
539
14e8215b
DW
540/* try to determine how much space is reserved for metadata from
541 * the last get_extents() entry, otherwise fallback to the
542 * default
543 */
544static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
545{
546 struct extent *e;
547 int i;
548 __u32 rv;
549
550 /* for spares just return a minimal reservation which will grow
551 * once the spare is picked up by an array
552 */
553 if (dl->index == -1)
554 return MPB_SECTOR_CNT;
555
556 e = get_extents(super, dl);
557 if (!e)
558 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
559
560 /* scroll to last entry */
561 for (i = 0; e[i].size; i++)
562 continue;
563
564 rv = __le32_to_cpu(dl->disk.total_blocks) - e[i].start;
565
566 free(e);
567
568 return rv;
569}
570
571#ifndef MDASSEMBLE
44470971 572static void print_imsm_dev(struct imsm_dev *dev, char *uuid, int disk_idx)
cdddbdbc
DW
573{
574 __u64 sz;
575 int slot;
a965f303 576 struct imsm_map *map = get_imsm_map(dev, 0);
b10b37b8 577 __u32 ord;
cdddbdbc
DW
578
579 printf("\n");
1e7bc0ed 580 printf("[%.16s]:\n", dev->volume);
44470971 581 printf(" UUID : %s\n", uuid);
cdddbdbc
DW
582 printf(" RAID Level : %d\n", get_imsm_raid_level(map));
583 printf(" Members : %d\n", map->num_members);
584 for (slot = 0; slot < map->num_members; slot++)
44470971 585 if (disk_idx== get_imsm_disk_idx(dev, slot))
cdddbdbc 586 break;
b10b37b8
DW
587 if (slot < map->num_members) {
588 ord = get_imsm_ord_tbl_ent(dev, slot);
589 printf(" This Slot : %d%s\n", slot,
590 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
591 } else
cdddbdbc
DW
592 printf(" This Slot : ?\n");
593 sz = __le32_to_cpu(dev->size_high);
594 sz <<= 32;
595 sz += __le32_to_cpu(dev->size_low);
596 printf(" Array Size : %llu%s\n", (unsigned long long)sz,
597 human_size(sz * 512));
598 sz = __le32_to_cpu(map->blocks_per_member);
599 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz,
600 human_size(sz * 512));
601 printf(" Sector Offset : %u\n",
602 __le32_to_cpu(map->pba_of_lba0));
603 printf(" Num Stripes : %u\n",
604 __le32_to_cpu(map->num_data_stripes));
605 printf(" Chunk Size : %u KiB\n",
606 __le16_to_cpu(map->blocks_per_strip) / 2);
607 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
3393c6af
DW
608 printf(" Migrate State : %s", dev->vol.migr_state ? "migrating" : "idle");
609 if (dev->vol.migr_state)
610 printf(": %s", dev->vol.migr_type ? "rebuilding" : "initializing");
611 printf("\n");
612 printf(" Map State : %s", map_state_str[map->map_state]);
613 if (dev->vol.migr_state) {
614 struct imsm_map *map = get_imsm_map(dev, 1);
b10b37b8 615 printf(" <-- %s", map_state_str[map->map_state]);
3393c6af
DW
616 }
617 printf("\n");
cdddbdbc 618 printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
cdddbdbc
DW
619}
620
14e8215b 621static void print_imsm_disk(struct imsm_super *mpb, int index, __u32 reserved)
cdddbdbc 622{
949c47a0 623 struct imsm_disk *disk = __get_imsm_disk(mpb, index);
1f24f035 624 char str[MAX_RAID_SERIAL_LEN + 1];
cdddbdbc
DW
625 __u32 s;
626 __u64 sz;
627
e9d82038
DW
628 if (index < 0)
629 return;
630
cdddbdbc 631 printf("\n");
1f24f035 632 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
cdddbdbc 633 printf(" Disk%02d Serial : %s\n", index, str);
f2f27e63 634 s = disk->status;
cdddbdbc
DW
635 printf(" State :%s%s%s%s\n", s&SPARE_DISK ? " spare" : "",
636 s&CONFIGURED_DISK ? " active" : "",
637 s&FAILED_DISK ? " failed" : "",
638 s&USABLE_DISK ? " usable" : "");
639 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
14e8215b 640 sz = __le32_to_cpu(disk->total_blocks) - reserved;
cdddbdbc
DW
641 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
642 human_size(sz * 512));
643}
644
44470971
DW
645static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info);
646
cdddbdbc
DW
647static void examine_super_imsm(struct supertype *st, char *homehost)
648{
649 struct intel_super *super = st->sb;
949c47a0 650 struct imsm_super *mpb = super->anchor;
cdddbdbc
DW
651 char str[MAX_SIGNATURE_LENGTH];
652 int i;
27fd6274
DW
653 struct mdinfo info;
654 char nbuf[64];
cdddbdbc 655 __u32 sum;
14e8215b 656 __u32 reserved = imsm_reserved_sectors(super, super->disks);
cdddbdbc 657
27fd6274 658
cdddbdbc
DW
659 snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
660 printf(" Magic : %s\n", str);
661 snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
662 printf(" Version : %s\n", get_imsm_version(mpb));
663 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
664 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
27fd6274
DW
665 getinfo_super_imsm(st, &info);
666 fname_from_uuid(st, &info, nbuf,'-');
667 printf(" UUID : %s\n", nbuf + 5);
cdddbdbc
DW
668 sum = __le32_to_cpu(mpb->check_sum);
669 printf(" Checksum : %08x %s\n", sum,
949c47a0 670 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
87eb16df 671 printf(" MPB Sectors : %d\n", mpb_sectors(mpb));
cdddbdbc
DW
672 printf(" Disks : %d\n", mpb->num_disks);
673 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
14e8215b 674 print_imsm_disk(mpb, super->disks->index, reserved);
604b746f
JD
675 if (super->bbm_log) {
676 struct bbm_log *log = super->bbm_log;
677
678 printf("\n");
679 printf("Bad Block Management Log:\n");
680 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
681 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
682 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
683 printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count));
684 printf(" First Spare : %llx\n", __le64_to_cpu(log->first_spare_lba));
685 }
44470971
DW
686 for (i = 0; i < mpb->num_raid_devs; i++) {
687 struct mdinfo info;
688 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
689
690 super->current_vol = i;
691 getinfo_super_imsm(st, &info);
692 fname_from_uuid(st, &info, nbuf, '-');
693 print_imsm_dev(dev, nbuf + 5, super->disks->index);
694 }
cdddbdbc
DW
695 for (i = 0; i < mpb->num_disks; i++) {
696 if (i == super->disks->index)
697 continue;
14e8215b 698 print_imsm_disk(mpb, i, reserved);
cdddbdbc
DW
699 }
700}
701
702static void brief_examine_super_imsm(struct supertype *st)
703{
27fd6274 704 /* We just write a generic IMSM ARRAY entry */
ff54de6e
N
705 struct mdinfo info;
706 char nbuf[64];
cf8de691 707 char nbuf1[64];
1e7bc0ed
DW
708 struct intel_super *super = st->sb;
709 int i;
710
711 if (!super->anchor->num_raid_devs)
712 return;
ff54de6e
N
713
714 getinfo_super_imsm(st, &info);
715 fname_from_uuid(st, &info, nbuf,'-');
cf8de691 716 printf("ARRAY metadata=imsm auto=md UUID=%s\n", nbuf + 5);
1e7bc0ed
DW
717 for (i = 0; i < super->anchor->num_raid_devs; i++) {
718 struct imsm_dev *dev = get_imsm_dev(super, i);
719
720 super->current_vol = i;
721 getinfo_super_imsm(st, &info);
cf8de691 722 fname_from_uuid(st, &info, nbuf1,'-');
44470971
DW
723 printf("ARRAY /dev/md/%.16s container=%s\n"
724 " member=%d auto=mdp UUID=%s\n",
cf8de691 725 dev->volume, nbuf + 5, i, nbuf1 + 5);
1e7bc0ed 726 }
cdddbdbc
DW
727}
728
729static void detail_super_imsm(struct supertype *st, char *homehost)
730{
3ebe00a1
DW
731 struct mdinfo info;
732 char nbuf[64];
733
734 getinfo_super_imsm(st, &info);
735 fname_from_uuid(st, &info, nbuf,'-');
736 printf("\n UUID : %s\n", nbuf + 5);
cdddbdbc
DW
737}
738
739static void brief_detail_super_imsm(struct supertype *st)
740{
ff54de6e
N
741 struct mdinfo info;
742 char nbuf[64];
743 getinfo_super_imsm(st, &info);
744 fname_from_uuid(st, &info, nbuf,'-');
745 printf(" UUID=%s", nbuf + 5);
cdddbdbc
DW
746}
747#endif
748
749static int match_home_imsm(struct supertype *st, char *homehost)
750{
5115ca67
DW
751 /* the imsm metadata format does not specify any host
752 * identification information. We return -1 since we can never
753 * confirm nor deny whether a given array is "meant" for this
754 * host. We rely on compare_super and the 'family_num' field to
755 * exclude member disks that do not belong, and we rely on
756 * mdadm.conf to specify the arrays that should be assembled.
757 * Auto-assembly may still pick up "foreign" arrays.
758 */
cdddbdbc 759
9362c1c8 760 return -1;
cdddbdbc
DW
761}
762
763static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
764{
51006d85
N
765 /* The uuid returned here is used for:
766 * uuid to put into bitmap file (Create, Grow)
767 * uuid for backup header when saving critical section (Grow)
768 * comparing uuids when re-adding a device into an array
769 * In these cases the uuid required is that of the data-array,
770 * not the device-set.
771 * uuid to recognise same set when adding a missing device back
772 * to an array. This is a uuid for the device-set.
773 *
774 * For each of these we can make do with a truncated
775 * or hashed uuid rather than the original, as long as
776 * everyone agrees.
777 * In each case the uuid required is that of the data-array,
778 * not the device-set.
43dad3d6 779 */
51006d85
N
780 /* imsm does not track uuid's so we synthesis one using sha1 on
781 * - The signature (Which is constant for all imsm array, but no matter)
782 * - the family_num of the container
783 * - the index number of the volume
784 * - the 'serial' number of the volume.
785 * Hopefully these are all constant.
786 */
787 struct intel_super *super = st->sb;
43dad3d6 788
51006d85
N
789 char buf[20];
790 struct sha1_ctx ctx;
791 struct imsm_dev *dev = NULL;
792
793 sha1_init_ctx(&ctx);
92bd8f8d 794 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
51006d85
N
795 sha1_process_bytes(&super->anchor->family_num, sizeof(__u32), &ctx);
796 if (super->current_vol >= 0)
797 dev = get_imsm_dev(super, super->current_vol);
798 if (dev) {
799 __u32 vol = super->current_vol;
800 sha1_process_bytes(&vol, sizeof(vol), &ctx);
801 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
802 }
803 sha1_finish_ctx(&ctx, buf);
804 memcpy(uuid, buf, 4*4);
cdddbdbc
DW
805}
806
0d481d37 807#if 0
4f5bc454
DW
808static void
809get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
cdddbdbc 810{
cdddbdbc
DW
811 __u8 *v = get_imsm_version(mpb);
812 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
813 char major[] = { 0, 0, 0 };
814 char minor[] = { 0 ,0, 0 };
815 char patch[] = { 0, 0, 0 };
816 char *ver_parse[] = { major, minor, patch };
817 int i, j;
818
819 i = j = 0;
820 while (*v != '\0' && v < end) {
821 if (*v != '.' && j < 2)
822 ver_parse[i][j++] = *v;
823 else {
824 i++;
825 j = 0;
826 }
827 v++;
828 }
829
4f5bc454
DW
830 *m = strtol(minor, NULL, 0);
831 *p = strtol(patch, NULL, 0);
832}
0d481d37 833#endif
4f5bc454 834
c2c087e6
DW
835static int imsm_level_to_layout(int level)
836{
837 switch (level) {
838 case 0:
839 case 1:
840 return 0;
841 case 5:
842 case 6:
a380c027 843 return ALGORITHM_LEFT_ASYMMETRIC;
c2c087e6 844 case 10:
c92a2527 845 return 0x102;
c2c087e6
DW
846 }
847 return -1;
848}
849
bf5a934a
DW
850static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info)
851{
852 struct intel_super *super = st->sb;
949c47a0 853 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
a965f303 854 struct imsm_map *map = get_imsm_map(dev, 0);
bf5a934a
DW
855
856 info->container_member = super->current_vol;
857 info->array.raid_disks = map->num_members;
858 info->array.level = get_imsm_raid_level(map);
859 info->array.layout = imsm_level_to_layout(info->array.level);
860 info->array.md_minor = -1;
861 info->array.ctime = 0;
862 info->array.utime = 0;
301406c9
DW
863 info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9;
864 info->array.state = !dev->vol.dirty;
865
866 info->disk.major = 0;
867 info->disk.minor = 0;
bf5a934a
DW
868
869 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
870 info->component_size = __le32_to_cpu(map->blocks_per_member);
301406c9 871 memset(info->uuid, 0, sizeof(info->uuid));
bf5a934a 872
f8f603f1 873 if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty)
301406c9 874 info->resync_start = 0;
f8f603f1
DW
875 else if (dev->vol.migr_state)
876 info->resync_start = __le32_to_cpu(dev->vol.curr_migr_unit);
301406c9
DW
877 else
878 info->resync_start = ~0ULL;
879
880 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
881 info->name[MAX_RAID_SERIAL_LEN] = 0;
bf5a934a 882
f35f2525
N
883 info->array.major_version = -1;
884 info->array.minor_version = -2;
bf5a934a
DW
885 sprintf(info->text_version, "/%s/%d",
886 devnum2devname(st->container_dev),
887 info->container_member);
a67dd8cc 888 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
51006d85 889 uuid_from_super_imsm(st, info->uuid);
bf5a934a
DW
890}
891
892
4f5bc454
DW
893static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
894{
895 struct intel_super *super = st->sb;
4f5bc454
DW
896 struct imsm_disk *disk;
897 __u32 s;
4f5bc454 898
bf5a934a
DW
899 if (super->current_vol >= 0) {
900 getinfo_super_imsm_volume(st, info);
901 return;
902 }
d23fe947
DW
903
904 /* Set raid_disks to zero so that Assemble will always pull in valid
905 * spares
906 */
907 info->array.raid_disks = 0;
cdddbdbc
DW
908 info->array.level = LEVEL_CONTAINER;
909 info->array.layout = 0;
910 info->array.md_minor = -1;
c2c087e6 911 info->array.ctime = 0; /* N/A for imsm */
cdddbdbc
DW
912 info->array.utime = 0;
913 info->array.chunk_size = 0;
914
915 info->disk.major = 0;
916 info->disk.minor = 0;
cdddbdbc 917 info->disk.raid_disk = -1;
c2c087e6 918 info->reshape_active = 0;
f35f2525
N
919 info->array.major_version = -1;
920 info->array.minor_version = -2;
c2c087e6 921 strcpy(info->text_version, "imsm");
a67dd8cc 922 info->safe_mode_delay = 0;
c2c087e6
DW
923 info->disk.number = -1;
924 info->disk.state = 0;
c5afc314 925 info->name[0] = 0;
c2c087e6 926
4a04ec6c 927 if (super->disks) {
14e8215b
DW
928 __u32 reserved = imsm_reserved_sectors(super, super->disks);
929
b9f594fe 930 disk = &super->disks->disk;
14e8215b
DW
931 info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved;
932 info->component_size = reserved;
f2f27e63 933 s = disk->status;
4a04ec6c
DW
934 info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
935 info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
032e9e29 936 info->disk.state |= s & SPARE_DISK ? 0 : (1 << MD_DISK_SYNC);
cdddbdbc 937 }
a575e2a7
DW
938
939 /* only call uuid_from_super_imsm when this disk is part of a populated container,
940 * ->compare_super may have updated the 'num_raid_devs' field for spares
941 */
942 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
36ba7d48 943 uuid_from_super_imsm(st, info->uuid);
032e9e29
DW
944 else
945 memcpy(info->uuid, uuid_match_any, sizeof(int[4]));
cdddbdbc
DW
946}
947
cdddbdbc
DW
948static int update_super_imsm(struct supertype *st, struct mdinfo *info,
949 char *update, char *devname, int verbose,
950 int uuid_set, char *homehost)
951{
f352c545
DW
952 /* FIXME */
953
954 /* For 'assemble' and 'force' we need to return non-zero if any
955 * change was made. For others, the return value is ignored.
956 * Update options are:
957 * force-one : This device looks a bit old but needs to be included,
958 * update age info appropriately.
959 * assemble: clear any 'faulty' flag to allow this device to
960 * be assembled.
961 * force-array: Array is degraded but being forced, mark it clean
962 * if that will be needed to assemble it.
963 *
964 * newdev: not used ????
965 * grow: Array has gained a new device - this is currently for
966 * linear only
967 * resync: mark as dirty so a resync will happen.
968 * name: update the name - preserving the homehost
969 *
970 * Following are not relevant for this imsm:
971 * sparc2.2 : update from old dodgey metadata
972 * super-minor: change the preferred_minor number
973 * summaries: update redundant counters.
974 * uuid: Change the uuid of the array to match watch is given
975 * homehost: update the recorded homehost
976 * _reshape_progress: record new reshape_progress position.
977 */
978 int rv = 0;
979 //struct intel_super *super = st->sb;
980 //struct imsm_super *mpb = super->mpb;
981
982 if (strcmp(update, "grow") == 0) {
983 }
984 if (strcmp(update, "resync") == 0) {
985 /* dev->vol.dirty = 1; */
986 }
987
988 /* IMSM has no concept of UUID or homehost */
989
990 return rv;
cdddbdbc
DW
991}
992
c2c087e6 993static size_t disks_to_mpb_size(int disks)
cdddbdbc 994{
c2c087e6 995 size_t size;
cdddbdbc 996
c2c087e6
DW
997 size = sizeof(struct imsm_super);
998 size += (disks - 1) * sizeof(struct imsm_disk);
999 size += 2 * sizeof(struct imsm_dev);
1000 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1001 size += (4 - 2) * sizeof(struct imsm_map);
1002 /* 4 possible disk_ord_tbl's */
1003 size += 4 * (disks - 1) * sizeof(__u32);
1004
1005 return size;
1006}
1007
1008static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
1009{
1010 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
1011 return 0;
1012
1013 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
cdddbdbc
DW
1014}
1015
1016static int compare_super_imsm(struct supertype *st, struct supertype *tst)
1017{
1018 /*
1019 * return:
1020 * 0 same, or first was empty, and second was copied
1021 * 1 second had wrong number
1022 * 2 wrong uuid
1023 * 3 wrong other info
1024 */
1025 struct intel_super *first = st->sb;
1026 struct intel_super *sec = tst->sb;
1027
1028 if (!first) {
1029 st->sb = tst->sb;
1030 tst->sb = NULL;
1031 return 0;
1032 }
1033
949c47a0 1034 if (memcmp(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH) != 0)
cdddbdbc 1035 return 3;
d23fe947
DW
1036
1037 /* if an anchor does not have num_raid_devs set then it is a free
1038 * floating spare
1039 */
1040 if (first->anchor->num_raid_devs > 0 &&
1041 sec->anchor->num_raid_devs > 0) {
1042 if (first->anchor->family_num != sec->anchor->family_num)
1043 return 3;
d23fe947 1044 }
cdddbdbc 1045
3e372e5a
DW
1046 /* if 'first' is a spare promote it to a populated mpb with sec's
1047 * family number
1048 */
1049 if (first->anchor->num_raid_devs == 0 &&
1050 sec->anchor->num_raid_devs > 0) {
78d30f94
DW
1051 int i;
1052
1053 /* we need to copy raid device info from sec if an allocation
1054 * fails here we don't associate the spare
1055 */
1056 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
1057 first->dev_tbl[i] = malloc(sizeof(struct imsm_dev));
1058 if (!first->dev_tbl) {
1059 while (--i >= 0) {
1060 free(first->dev_tbl[i]);
1061 first->dev_tbl[i] = NULL;
1062 }
1063 fprintf(stderr, "imsm: failed to associate spare\n");
1064 return 3;
1065 }
1066 *first->dev_tbl[i] = *sec->dev_tbl[i];
1067 }
1068
3e372e5a
DW
1069 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
1070 first->anchor->family_num = sec->anchor->family_num;
1071 }
1072
cdddbdbc
DW
1073 return 0;
1074}
1075
0030e8d6
DW
1076static void fd2devname(int fd, char *name)
1077{
1078 struct stat st;
1079 char path[256];
1080 char dname[100];
1081 char *nm;
1082 int rv;
1083
1084 name[0] = '\0';
1085 if (fstat(fd, &st) != 0)
1086 return;
1087 sprintf(path, "/sys/dev/block/%d:%d",
1088 major(st.st_rdev), minor(st.st_rdev));
1089
1090 rv = readlink(path, dname, sizeof(dname));
1091 if (rv <= 0)
1092 return;
1093
1094 dname[rv] = '\0';
1095 nm = strrchr(dname, '/');
1096 nm++;
1097 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
1098}
1099
1100
cdddbdbc
DW
1101extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
1102
1103static int imsm_read_serial(int fd, char *devname,
1104 __u8 serial[MAX_RAID_SERIAL_LEN])
1105{
1106 unsigned char scsi_serial[255];
cdddbdbc
DW
1107 int rv;
1108 int rsp_len;
1f24f035
DW
1109 int len;
1110 char *c, *rsp_buf;
cdddbdbc
DW
1111
1112 memset(scsi_serial, 0, sizeof(scsi_serial));
cdddbdbc 1113
f9ba0ff1
DW
1114 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
1115
40ebbb9c 1116 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
f9ba0ff1
DW
1117 memset(serial, 0, MAX_RAID_SERIAL_LEN);
1118 fd2devname(fd, (char *) serial);
0030e8d6
DW
1119 return 0;
1120 }
1121
cdddbdbc
DW
1122 if (rv != 0) {
1123 if (devname)
1124 fprintf(stderr,
1125 Name ": Failed to retrieve serial for %s\n",
1126 devname);
1127 return rv;
1128 }
1129
5c3db629 1130 /* trim leading whitespace */
cdddbdbc 1131 rsp_len = scsi_serial[3];
1f24f035
DW
1132 rsp_buf = (char *) &scsi_serial[4];
1133 c = rsp_buf;
1134 while (isspace(*c))
1135 c++;
5c3db629
DW
1136
1137 /* truncate len to the end of rsp_buf if necessary */
1f24f035
DW
1138 if (c + MAX_RAID_SERIAL_LEN > rsp_buf + rsp_len)
1139 len = rsp_len - (c - rsp_buf);
1140 else
1141 len = MAX_RAID_SERIAL_LEN;
5c3db629
DW
1142
1143 /* initialize the buffer and copy rsp_buf characters */
1144 memset(serial, 0, MAX_RAID_SERIAL_LEN);
1f24f035 1145 memcpy(serial, c, len);
5c3db629
DW
1146
1147 /* trim trailing whitespace starting with the last character copied */
1f24f035
DW
1148 c = (char *) &serial[len - 1];
1149 while (isspace(*c) || *c == '\0')
1150 *c-- = '\0';
cdddbdbc
DW
1151
1152 return 0;
1153}
1154
1f24f035
DW
1155static int serialcmp(__u8 *s1, __u8 *s2)
1156{
1157 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
1158}
1159
1160static void serialcpy(__u8 *dest, __u8 *src)
1161{
1162 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
1163}
1164
54c2c1ea
DW
1165static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
1166{
1167 struct dl *dl;
1168
1169 for (dl = super->disks; dl; dl = dl->next)
1170 if (serialcmp(dl->serial, serial) == 0)
1171 break;
1172
1173 return dl;
1174}
1175
cdddbdbc
DW
1176static int
1177load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
1178{
cdddbdbc
DW
1179 struct dl *dl;
1180 struct stat stb;
cdddbdbc
DW
1181 int rv;
1182 int i;
d23fe947
DW
1183 int alloc = 1;
1184 __u8 serial[MAX_RAID_SERIAL_LEN];
1185
1186 rv = imsm_read_serial(fd, devname, serial);
1187
1188 if (rv != 0)
1189 return 2;
1190
1191 /* check if this is a disk we have seen before. it may be a spare in
1192 * super->disks while the current anchor believes it is a raid member,
1193 * check if we need to update dl->index
1194 */
54c2c1ea 1195 dl = serial_to_dl(serial, super);
d23fe947
DW
1196 if (!dl)
1197 dl = malloc(sizeof(*dl));
1198 else
1199 alloc = 0;
cdddbdbc 1200
b9f594fe 1201 if (!dl) {
cdddbdbc
DW
1202 if (devname)
1203 fprintf(stderr,
1204 Name ": failed to allocate disk buffer for %s\n",
1205 devname);
1206 return 2;
1207 }
cdddbdbc 1208
d23fe947
DW
1209 if (alloc) {
1210 fstat(fd, &stb);
1211 dl->major = major(stb.st_rdev);
1212 dl->minor = minor(stb.st_rdev);
1213 dl->next = super->disks;
1214 dl->fd = keep_fd ? fd : -1;
1215 dl->devname = devname ? strdup(devname) : NULL;
1f24f035 1216 serialcpy(dl->serial, serial);
8796fdc4 1217 dl->index = -2;
0dcecb2e 1218 dl->e = NULL;
d23fe947
DW
1219 } else if (keep_fd) {
1220 close(dl->fd);
1221 dl->fd = fd;
1222 }
cdddbdbc 1223
d23fe947 1224 /* look up this disk's index in the current anchor */
949c47a0
DW
1225 for (i = 0; i < super->anchor->num_disks; i++) {
1226 struct imsm_disk *disk_iter;
1227
1228 disk_iter = __get_imsm_disk(super->anchor, i);
cdddbdbc 1229
1f24f035 1230 if (serialcmp(disk_iter->serial, dl->serial) == 0) {
b9f594fe 1231 dl->disk = *disk_iter;
d23fe947
DW
1232 /* only set index on disks that are a member of a
1233 * populated contianer, i.e. one with raid_devs
1234 */
f2f27e63 1235 if (dl->disk.status & FAILED_DISK)
6c386dd3 1236 dl->index = -2;
f2f27e63 1237 else if (dl->disk.status & SPARE_DISK)
d23fe947
DW
1238 dl->index = -1;
1239 else
1240 dl->index = i;
8796fdc4 1241
cdddbdbc 1242 break;
949c47a0 1243 }
cdddbdbc
DW
1244 }
1245
3f6efecc
DW
1246 /* no match, maybe a stale failed drive */
1247 if (i == super->anchor->num_disks && dl->index >= 0) {
1248 dl->disk = *__get_imsm_disk(super->anchor, dl->index);
f2f27e63 1249 if (dl->disk.status & FAILED_DISK)
3f6efecc
DW
1250 dl->index = -2;
1251 }
1252
d23fe947
DW
1253 if (alloc)
1254 super->disks = dl;
6c386dd3 1255
949c47a0
DW
1256 return 0;
1257}
1258
1259static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
1260{
3393c6af
DW
1261 memcpy(dest, src, sizeof_imsm_dev(src, 0));
1262}
1263
0e600426 1264#ifndef MDASSEMBLE
0c046afd
DW
1265/* When migrating map0 contains the 'destination' state while map1
1266 * contains the current state. When not migrating map0 contains the
1267 * current state. This routine assumes that map[0].map_state is set to
1268 * the current array state before being called.
1269 *
1270 * Migration is indicated by one of the following states
1271 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
e3bba0e0 1272 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
0c046afd 1273 * map1state=unitialized)
e3bba0e0 1274 * 3/ Verify (Resync) (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
0c046afd 1275 * map1state=normal)
e3bba0e0 1276 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
0c046afd
DW
1277 * map1state=degraded)
1278 */
1279static void migrate(struct imsm_dev *dev, __u8 to_state, int rebuild_resync)
3393c6af 1280{
0c046afd 1281 struct imsm_map *dest;
3393c6af
DW
1282 struct imsm_map *src = get_imsm_map(dev, 0);
1283
0c046afd
DW
1284 dev->vol.migr_state = 1;
1285 dev->vol.migr_type = rebuild_resync;
f8f603f1 1286 dev->vol.curr_migr_unit = 0;
0c046afd
DW
1287 dest = get_imsm_map(dev, 1);
1288
3393c6af 1289 memcpy(dest, src, sizeof_imsm_map(src));
0c046afd 1290 src->map_state = to_state;
949c47a0 1291}
f8f603f1
DW
1292
1293static void end_migration(struct imsm_dev *dev, __u8 map_state)
1294{
1295 struct imsm_map *map = get_imsm_map(dev, 0);
1296
1297 dev->vol.migr_state = 0;
1298 dev->vol.curr_migr_unit = 0;
1299 map->map_state = map_state;
1300}
0e600426 1301#endif
949c47a0
DW
1302
1303static int parse_raid_devices(struct intel_super *super)
1304{
1305 int i;
1306 struct imsm_dev *dev_new;
4d7b1503
DW
1307 size_t len, len_migr;
1308 size_t space_needed = 0;
1309 struct imsm_super *mpb = super->anchor;
949c47a0
DW
1310
1311 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1312 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
1313
4d7b1503
DW
1314 len = sizeof_imsm_dev(dev_iter, 0);
1315 len_migr = sizeof_imsm_dev(dev_iter, 1);
1316 if (len_migr > len)
1317 space_needed += len_migr - len;
1318
1319 dev_new = malloc(len_migr);
949c47a0
DW
1320 if (!dev_new)
1321 return 1;
1322 imsm_copy_dev(dev_new, dev_iter);
1323 super->dev_tbl[i] = dev_new;
1324 }
cdddbdbc 1325
4d7b1503
DW
1326 /* ensure that super->buf is large enough when all raid devices
1327 * are migrating
1328 */
1329 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
1330 void *buf;
1331
1332 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512);
1333 if (posix_memalign(&buf, 512, len) != 0)
1334 return 1;
1335
1336 memcpy(buf, super->buf, len);
1337 free(super->buf);
1338 super->buf = buf;
1339 super->len = len;
1340 }
1341
cdddbdbc
DW
1342 return 0;
1343}
1344
604b746f
JD
1345/* retrieve a pointer to the bbm log which starts after all raid devices */
1346struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
1347{
1348 void *ptr = NULL;
1349
1350 if (__le32_to_cpu(mpb->bbm_log_size)) {
1351 ptr = mpb;
1352 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
1353 }
1354
1355 return ptr;
1356}
1357
d23fe947 1358static void __free_imsm(struct intel_super *super, int free_disks);
9ca2c81c 1359
cdddbdbc
DW
1360/* load_imsm_mpb - read matrix metadata
1361 * allocates super->mpb to be freed by free_super
1362 */
1363static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
1364{
1365 unsigned long long dsize;
cdddbdbc
DW
1366 unsigned long long sectors;
1367 struct stat;
6416d527 1368 struct imsm_super *anchor;
cdddbdbc 1369 __u32 check_sum;
949c47a0 1370 int rc;
cdddbdbc 1371
cdddbdbc
DW
1372 get_dev_size(fd, NULL, &dsize);
1373
1374 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
1375 if (devname)
1376 fprintf(stderr,
1377 Name ": Cannot seek to anchor block on %s: %s\n",
1378 devname, strerror(errno));
1379 return 1;
1380 }
1381
949c47a0 1382 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
ad97895e
DW
1383 if (devname)
1384 fprintf(stderr,
1385 Name ": Failed to allocate imsm anchor buffer"
1386 " on %s\n", devname);
1387 return 1;
1388 }
949c47a0 1389 if (read(fd, anchor, 512) != 512) {
cdddbdbc
DW
1390 if (devname)
1391 fprintf(stderr,
1392 Name ": Cannot read anchor block on %s: %s\n",
1393 devname, strerror(errno));
6416d527 1394 free(anchor);
cdddbdbc
DW
1395 return 1;
1396 }
1397
6416d527 1398 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
cdddbdbc
DW
1399 if (devname)
1400 fprintf(stderr,
1401 Name ": no IMSM anchor on %s\n", devname);
6416d527 1402 free(anchor);
cdddbdbc
DW
1403 return 2;
1404 }
1405
d23fe947 1406 __free_imsm(super, 0);
949c47a0
DW
1407 super->len = ROUND_UP(anchor->mpb_size, 512);
1408 if (posix_memalign(&super->buf, 512, super->len) != 0) {
cdddbdbc
DW
1409 if (devname)
1410 fprintf(stderr,
1411 Name ": unable to allocate %zu byte mpb buffer\n",
949c47a0 1412 super->len);
6416d527 1413 free(anchor);
cdddbdbc
DW
1414 return 2;
1415 }
949c47a0 1416 memcpy(super->buf, anchor, 512);
cdddbdbc 1417
6416d527
NB
1418 sectors = mpb_sectors(anchor) - 1;
1419 free(anchor);
949c47a0
DW
1420 if (!sectors) {
1421 rc = load_imsm_disk(fd, super, devname, 0);
1422 if (rc == 0)
1423 rc = parse_raid_devices(super);
1424 return rc;
1425 }
cdddbdbc
DW
1426
1427 /* read the extended mpb */
1428 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
1429 if (devname)
1430 fprintf(stderr,
1431 Name ": Cannot seek to extended mpb on %s: %s\n",
1432 devname, strerror(errno));
1433 return 1;
1434 }
1435
949c47a0 1436 if (read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
cdddbdbc
DW
1437 if (devname)
1438 fprintf(stderr,
1439 Name ": Cannot read extended mpb on %s: %s\n",
1440 devname, strerror(errno));
1441 return 2;
1442 }
1443
949c47a0
DW
1444 check_sum = __gen_imsm_checksum(super->anchor);
1445 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
cdddbdbc
DW
1446 if (devname)
1447 fprintf(stderr,
1448 Name ": IMSM checksum %x != %x on %s\n",
949c47a0 1449 check_sum, __le32_to_cpu(super->anchor->check_sum),
cdddbdbc
DW
1450 devname);
1451 return 2;
1452 }
1453
604b746f
JD
1454 /* FIXME the BBM log is disk specific so we cannot use this global
1455 * buffer for all disks. Ok for now since we only look at the global
1456 * bbm_log_size parameter to gate assembly
1457 */
1458 super->bbm_log = __get_imsm_bbm_log(super->anchor);
1459
949c47a0
DW
1460 rc = load_imsm_disk(fd, super, devname, 0);
1461 if (rc == 0)
1462 rc = parse_raid_devices(super);
4d7b1503 1463
949c47a0 1464 return rc;
cdddbdbc
DW
1465}
1466
ae6aad82
DW
1467static void __free_imsm_disk(struct dl *d)
1468{
1469 if (d->fd >= 0)
1470 close(d->fd);
1471 if (d->devname)
1472 free(d->devname);
0dcecb2e
DW
1473 if (d->e)
1474 free(d->e);
ae6aad82
DW
1475 free(d);
1476
1477}
cdddbdbc
DW
1478static void free_imsm_disks(struct intel_super *super)
1479{
47ee5a45 1480 struct dl *d;
cdddbdbc 1481
47ee5a45
DW
1482 while (super->disks) {
1483 d = super->disks;
cdddbdbc 1484 super->disks = d->next;
ae6aad82 1485 __free_imsm_disk(d);
cdddbdbc 1486 }
47ee5a45
DW
1487 while (super->missing) {
1488 d = super->missing;
1489 super->missing = d->next;
1490 __free_imsm_disk(d);
1491 }
1492
cdddbdbc
DW
1493}
1494
9ca2c81c 1495/* free all the pieces hanging off of a super pointer */
d23fe947 1496static void __free_imsm(struct intel_super *super, int free_disks)
cdddbdbc 1497{
949c47a0
DW
1498 int i;
1499
9ca2c81c 1500 if (super->buf) {
949c47a0 1501 free(super->buf);
9ca2c81c
DW
1502 super->buf = NULL;
1503 }
d23fe947
DW
1504 if (free_disks)
1505 free_imsm_disks(super);
949c47a0 1506 for (i = 0; i < IMSM_MAX_RAID_DEVS; i++)
9ca2c81c 1507 if (super->dev_tbl[i]) {
949c47a0 1508 free(super->dev_tbl[i]);
9ca2c81c
DW
1509 super->dev_tbl[i] = NULL;
1510 }
88c32bb1
DW
1511 if (super->hba) {
1512 free((void *) super->hba);
1513 super->hba = NULL;
1514 }
cdddbdbc
DW
1515}
1516
9ca2c81c
DW
1517static void free_imsm(struct intel_super *super)
1518{
d23fe947 1519 __free_imsm(super, 1);
9ca2c81c
DW
1520 free(super);
1521}
cdddbdbc
DW
1522
1523static void free_super_imsm(struct supertype *st)
1524{
1525 struct intel_super *super = st->sb;
1526
1527 if (!super)
1528 return;
1529
1530 free_imsm(super);
1531 st->sb = NULL;
1532}
1533
c2c087e6
DW
1534static struct intel_super *alloc_super(int creating_imsm)
1535{
1536 struct intel_super *super = malloc(sizeof(*super));
1537
1538 if (super) {
1539 memset(super, 0, sizeof(*super));
1540 super->creating_imsm = creating_imsm;
bf5a934a 1541 super->current_vol = -1;
0dcecb2e 1542 super->create_offset = ~((__u32 ) 0);
88c32bb1
DW
1543 if (!check_env("IMSM_NO_PLATFORM"))
1544 super->orom = find_imsm_orom();
1545 if (super->orom) {
1546 struct sys_dev *list, *ent;
1547
1548 /* find the first intel ahci controller */
1549 list = find_driver_devices("pci", "ahci");
1550 for (ent = list; ent; ent = ent->next)
1551 if (devpath_to_vendor(ent->path) == 0x8086)
1552 break;
1553 if (ent) {
1554 super->hba = ent->path;
1555 ent->path = NULL;
1556 }
1557 free_sys_dev(&list);
1558 }
c2c087e6
DW
1559 }
1560
1561 return super;
1562}
1563
cdddbdbc 1564#ifndef MDASSEMBLE
47ee5a45
DW
1565/* find_missing - helper routine for load_super_imsm_all that identifies
1566 * disks that have disappeared from the system. This routine relies on
1567 * the mpb being uptodate, which it is at load time.
1568 */
1569static int find_missing(struct intel_super *super)
1570{
1571 int i;
1572 struct imsm_super *mpb = super->anchor;
1573 struct dl *dl;
1574 struct imsm_disk *disk;
47ee5a45
DW
1575
1576 for (i = 0; i < mpb->num_disks; i++) {
1577 disk = __get_imsm_disk(mpb, i);
54c2c1ea 1578 dl = serial_to_dl(disk->serial, super);
47ee5a45
DW
1579 if (dl)
1580 continue;
1581 /* ok we have a 'disk' without a live entry in
1582 * super->disks
1583 */
f2f27e63 1584 if (disk->status & FAILED_DISK || !(disk->status & USABLE_DISK))
47ee5a45
DW
1585 continue; /* never mind, already marked */
1586
1587 dl = malloc(sizeof(*dl));
1588 if (!dl)
1589 return 1;
1590 dl->major = 0;
1591 dl->minor = 0;
1592 dl->fd = -1;
1593 dl->devname = strdup("missing");
1594 dl->index = i;
1595 serialcpy(dl->serial, disk->serial);
1596 dl->disk = *disk;
1597 dl->next = super->missing;
1598 super->missing = dl;
1599 }
1600
1601 return 0;
1602}
1603
cdddbdbc
DW
1604static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
1605 char *devname, int keep_fd)
1606{
1607 struct mdinfo *sra;
1608 struct intel_super *super;
1609 struct mdinfo *sd, *best = NULL;
1610 __u32 bestgen = 0;
1611 __u32 gen;
1612 char nm[20];
1613 int dfd;
1614 int rv;
1615
1616 /* check if this disk is a member of an active array */
1617 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
1618 if (!sra)
1619 return 1;
1620
1621 if (sra->array.major_version != -1 ||
1622 sra->array.minor_version != -2 ||
1623 strcmp(sra->text_version, "imsm") != 0)
1624 return 1;
1625
c2c087e6 1626 super = alloc_super(0);
cdddbdbc
DW
1627 if (!super)
1628 return 1;
1629
d23fe947 1630 /* find the most up to date disk in this array, skipping spares */
cdddbdbc
DW
1631 for (sd = sra->devs; sd; sd = sd->next) {
1632 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
1633 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
1634 if (!dfd) {
1635 free_imsm(super);
1636 return 2;
1637 }
1638 rv = load_imsm_mpb(dfd, super, NULL);
1639 if (!keep_fd)
1640 close(dfd);
1641 if (rv == 0) {
d23fe947
DW
1642 if (super->anchor->num_raid_devs == 0)
1643 gen = 0;
1644 else
1645 gen = __le32_to_cpu(super->anchor->generation_num);
cdddbdbc
DW
1646 if (!best || gen > bestgen) {
1647 bestgen = gen;
1648 best = sd;
1649 }
1650 } else {
1651 free_imsm(super);
1652 return 2;
1653 }
1654 }
1655
1656 if (!best) {
1657 free_imsm(super);
1658 return 1;
1659 }
1660
1661 /* load the most up to date anchor */
1662 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
1663 dfd = dev_open(nm, O_RDONLY);
1664 if (!dfd) {
1665 free_imsm(super);
1666 return 1;
1667 }
1668 rv = load_imsm_mpb(dfd, super, NULL);
1669 close(dfd);
1670 if (rv != 0) {
1671 free_imsm(super);
1672 return 2;
1673 }
1674
d23fe947 1675 /* re-parse the disk list with the current anchor */
cdddbdbc
DW
1676 for (sd = sra->devs ; sd ; sd = sd->next) {
1677 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
1678 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
1679 if (!dfd) {
1680 free_imsm(super);
1681 return 2;
1682 }
1683 load_imsm_disk(dfd, super, NULL, keep_fd);
1684 if (!keep_fd)
1685 close(dfd);
1686 }
1687
47ee5a45
DW
1688
1689 if (find_missing(super) != 0) {
1690 free_imsm(super);
1691 return 2;
1692 }
1693
f7e7067b 1694 if (st->subarray[0]) {
949c47a0 1695 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
bf5a934a
DW
1696 super->current_vol = atoi(st->subarray);
1697 else
1698 return 1;
f7e7067b
NB
1699 }
1700
cdddbdbc 1701 *sbp = super;
43dad3d6 1702 st->container_dev = fd2devnum(fd);
cdddbdbc 1703 if (st->ss == NULL) {
bf5a934a 1704 st->ss = &super_imsm;
cdddbdbc
DW
1705 st->minor_version = 0;
1706 st->max_devs = IMSM_MAX_DEVICES;
1707 }
352452c3 1708 st->loaded_container = 1;
cdddbdbc
DW
1709
1710 return 0;
1711}
1712#endif
1713
1714static int load_super_imsm(struct supertype *st, int fd, char *devname)
1715{
1716 struct intel_super *super;
1717 int rv;
1718
1719#ifndef MDASSEMBLE
3dbccbcf 1720 if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0)
cdddbdbc
DW
1721 return 0;
1722#endif
f7e7067b
NB
1723 if (st->subarray[0])
1724 return 1; /* FIXME */
cdddbdbc 1725
c2c087e6 1726 super = alloc_super(0);
cdddbdbc
DW
1727 if (!super) {
1728 fprintf(stderr,
1729 Name ": malloc of %zu failed.\n",
1730 sizeof(*super));
1731 return 1;
1732 }
1733
1734 rv = load_imsm_mpb(fd, super, devname);
1735
1736 if (rv) {
1737 if (devname)
1738 fprintf(stderr,
1739 Name ": Failed to load all information "
1740 "sections on %s\n", devname);
1741 free_imsm(super);
1742 return rv;
1743 }
1744
1745 st->sb = super;
1746 if (st->ss == NULL) {
1747 st->ss = &super_imsm;
1748 st->minor_version = 0;
1749 st->max_devs = IMSM_MAX_DEVICES;
1750 }
352452c3 1751 st->loaded_container = 0;
cdddbdbc
DW
1752
1753 return 0;
1754}
1755
ef6ffade
DW
1756static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
1757{
1758 if (info->level == 1)
1759 return 128;
1760 return info->chunk_size >> 9;
1761}
1762
1763static __u32 info_to_num_data_stripes(mdu_array_info_t *info)
1764{
1765 __u32 num_stripes;
1766
1767 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
1768 if (info->level == 1)
1769 num_stripes /= 2;
1770
1771 return num_stripes;
1772}
1773
fcfd9599
DW
1774static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
1775{
1776 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
1777}
1778
4d1313e9
DW
1779static void imsm_update_version_info(struct intel_super *super)
1780{
1781 /* update the version and attributes */
1782 struct imsm_super *mpb = super->anchor;
1783 char *version;
1784 struct imsm_dev *dev;
1785 struct imsm_map *map;
1786 int i;
1787
1788 for (i = 0; i < mpb->num_raid_devs; i++) {
1789 dev = get_imsm_dev(super, i);
1790 map = get_imsm_map(dev, 0);
1791 if (__le32_to_cpu(dev->size_high) > 0)
1792 mpb->attributes |= MPB_ATTRIB_2TB;
1793
1794 /* FIXME detect when an array spans a port multiplier */
1795 #if 0
1796 mpb->attributes |= MPB_ATTRIB_PM;
1797 #endif
1798
1799 if (mpb->num_raid_devs > 1 ||
1800 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
1801 version = MPB_VERSION_ATTRIBS;
1802 switch (get_imsm_raid_level(map)) {
1803 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
1804 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
1805 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
1806 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
1807 }
1808 } else {
1809 if (map->num_members >= 5)
1810 version = MPB_VERSION_5OR6_DISK_ARRAY;
1811 else if (dev->status == DEV_CLONE_N_GO)
1812 version = MPB_VERSION_CNG;
1813 else if (get_imsm_raid_level(map) == 5)
1814 version = MPB_VERSION_RAID5;
1815 else if (map->num_members >= 3)
1816 version = MPB_VERSION_3OR4_DISK_ARRAY;
1817 else if (get_imsm_raid_level(map) == 1)
1818 version = MPB_VERSION_RAID1;
1819 else
1820 version = MPB_VERSION_RAID0;
1821 }
1822 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
1823 }
1824}
1825
8b353278
DW
1826static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
1827 unsigned long long size, char *name,
1828 char *homehost, int *uuid)
cdddbdbc 1829{
c2c087e6
DW
1830 /* We are creating a volume inside a pre-existing container.
1831 * so st->sb is already set.
1832 */
1833 struct intel_super *super = st->sb;
949c47a0 1834 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
1835 struct imsm_dev *dev;
1836 struct imsm_vol *vol;
1837 struct imsm_map *map;
1838 int idx = mpb->num_raid_devs;
1839 int i;
1840 unsigned long long array_blocks;
2c092cad 1841 size_t size_old, size_new;
cdddbdbc 1842
88c32bb1 1843 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
c2c087e6 1844 fprintf(stderr, Name": This imsm-container already has the "
88c32bb1 1845 "maximum of %d volumes\n", super->orom->vpa);
c2c087e6
DW
1846 return 0;
1847 }
1848
2c092cad
DW
1849 /* ensure the mpb is large enough for the new data */
1850 size_old = __le32_to_cpu(mpb->mpb_size);
1851 size_new = disks_to_mpb_size(info->nr_disks);
1852 if (size_new > size_old) {
1853 void *mpb_new;
1854 size_t size_round = ROUND_UP(size_new, 512);
1855
1856 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
1857 fprintf(stderr, Name": could not allocate new mpb\n");
1858 return 0;
1859 }
1860 memcpy(mpb_new, mpb, size_old);
1861 free(mpb);
1862 mpb = mpb_new;
949c47a0 1863 super->anchor = mpb_new;
2c092cad
DW
1864 mpb->mpb_size = __cpu_to_le32(size_new);
1865 memset(mpb_new + size_old, 0, size_round - size_old);
1866 }
bf5a934a 1867 super->current_vol = idx;
d23fe947
DW
1868 /* when creating the first raid device in this container set num_disks
1869 * to zero, i.e. delete this spare and add raid member devices in
1870 * add_to_super_imsm_volume()
1871 */
1872 if (super->current_vol == 0)
1873 mpb->num_disks = 0;
bf5a934a 1874 sprintf(st->subarray, "%d", idx);
949c47a0
DW
1875 dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
1876 if (!dev) {
1877 fprintf(stderr, Name": could not allocate raid device\n");
1878 return 0;
1879 }
c2c087e6 1880 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
03bcbc65
DW
1881 if (info->level == 1)
1882 array_blocks = info_to_blocks_per_member(info);
1883 else
1884 array_blocks = calc_array_size(info->level, info->raid_disks,
1885 info->layout, info->chunk_size,
1886 info->size*2);
c2c087e6
DW
1887 dev->size_low = __cpu_to_le32((__u32) array_blocks);
1888 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
1889 dev->status = __cpu_to_le32(0);
1890 dev->reserved_blocks = __cpu_to_le32(0);
1891 vol = &dev->vol;
1892 vol->migr_state = 0;
e3bba0e0 1893 vol->migr_type = MIGR_INIT;
c2c087e6 1894 vol->dirty = 0;
f8f603f1 1895 vol->curr_migr_unit = 0;
a965f303 1896 map = get_imsm_map(dev, 0);
0dcecb2e 1897 map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
fcfd9599 1898 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
ef6ffade
DW
1899 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
1900 map->num_data_stripes = __cpu_to_le32(info_to_num_data_stripes(info));
c2c087e6
DW
1901 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
1902 IMSM_T_STATE_NORMAL;
ef6ffade
DW
1903
1904 if (info->level == 1 && info->raid_disks > 2) {
1905 fprintf(stderr, Name": imsm does not support more than 2 disks"
1906 "in a raid1 volume\n");
1907 return 0;
1908 }
4d1313e9 1909 if (info->level == 10) {
c2c087e6 1910 map->raid_level = 1;
4d1313e9
DW
1911 map->num_domains = info->raid_disks / 2;
1912 } else {
c2c087e6 1913 map->raid_level = info->level;
4d1313e9
DW
1914 map->num_domains = !!map->raid_level;
1915 }
ef6ffade 1916
c2c087e6
DW
1917 map->num_members = info->raid_disks;
1918 for (i = 0; i < map->num_members; i++) {
1919 /* initialized in add_to_super */
be73972f 1920 set_imsm_ord_tbl_ent(map, i, 0);
c2c087e6 1921 }
949c47a0
DW
1922 mpb->num_raid_devs++;
1923 super->dev_tbl[super->current_vol] = dev;
c2c087e6 1924
4d1313e9
DW
1925 imsm_update_version_info(super);
1926
c2c087e6 1927 return 1;
cdddbdbc
DW
1928}
1929
bf5a934a
DW
1930static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
1931 unsigned long long size, char *name,
1932 char *homehost, int *uuid)
1933{
1934 /* This is primarily called by Create when creating a new array.
1935 * We will then get add_to_super called for each component, and then
1936 * write_init_super called to write it out to each device.
1937 * For IMSM, Create can create on fresh devices or on a pre-existing
1938 * array.
1939 * To create on a pre-existing array a different method will be called.
1940 * This one is just for fresh drives.
1941 */
1942 struct intel_super *super;
1943 struct imsm_super *mpb;
1944 size_t mpb_size;
4d1313e9 1945 char *version;
bf5a934a
DW
1946
1947 if (!info) {
1948 st->sb = NULL;
1949 return 0;
1950 }
1951 if (st->sb)
1952 return init_super_imsm_volume(st, info, size, name, homehost,
1953 uuid);
1954
1955 super = alloc_super(1);
1956 if (!super)
1957 return 0;
1958 mpb_size = disks_to_mpb_size(info->nr_disks);
ef649044 1959 if (posix_memalign(&super->buf, 512, mpb_size) != 0) {
bf5a934a
DW
1960 free(super);
1961 return 0;
1962 }
ef649044 1963 mpb = super->buf;
bf5a934a
DW
1964 memset(mpb, 0, mpb_size);
1965
4d1313e9
DW
1966 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
1967
1968 version = (char *) mpb->sig;
1969 strcpy(version, MPB_SIGNATURE);
1970 version += strlen(MPB_SIGNATURE);
1971 strcpy(version, MPB_VERSION_RAID0);
bf5a934a
DW
1972 mpb->mpb_size = mpb_size;
1973
bf5a934a
DW
1974 st->sb = super;
1975 return 1;
1976}
1977
0e600426 1978#ifndef MDASSEMBLE
f20c3968 1979static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
bf5a934a
DW
1980 int fd, char *devname)
1981{
1982 struct intel_super *super = st->sb;
d23fe947 1983 struct imsm_super *mpb = super->anchor;
bf5a934a
DW
1984 struct dl *dl;
1985 struct imsm_dev *dev;
1986 struct imsm_map *map;
bf5a934a 1987
949c47a0 1988 dev = get_imsm_dev(super, super->current_vol);
a965f303 1989 map = get_imsm_map(dev, 0);
bf5a934a 1990
208933a7
N
1991 if (! (dk->state & (1<<MD_DISK_SYNC))) {
1992 fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
1993 devname);
1994 return 1;
1995 }
1996
bf5a934a
DW
1997 for (dl = super->disks; dl ; dl = dl->next)
1998 if (dl->major == dk->major &&
1999 dl->minor == dk->minor)
2000 break;
d23fe947 2001
208933a7
N
2002 if (!dl) {
2003 fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
f20c3968 2004 return 1;
208933a7 2005 }
bf5a934a 2006
d23fe947
DW
2007 /* add a pristine spare to the metadata */
2008 if (dl->index < 0) {
2009 dl->index = super->anchor->num_disks;
2010 super->anchor->num_disks++;
2011 }
be73972f 2012 set_imsm_ord_tbl_ent(map, dk->number, dl->index);
f2f27e63 2013 dl->disk.status = CONFIGURED_DISK | USABLE_DISK;
d23fe947
DW
2014
2015 /* if we are creating the first raid device update the family number */
2016 if (super->current_vol == 0) {
2017 __u32 sum;
2018 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
2019 struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
2020
2021 *_dev = *dev;
2022 *_disk = dl->disk;
2023 sum = __gen_imsm_checksum(mpb);
2024 mpb->family_num = __cpu_to_le32(sum);
2025 }
f20c3968
DW
2026
2027 return 0;
bf5a934a
DW
2028}
2029
f20c3968 2030static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
cdddbdbc
DW
2031 int fd, char *devname)
2032{
c2c087e6 2033 struct intel_super *super = st->sb;
c2c087e6
DW
2034 struct dl *dd;
2035 unsigned long long size;
f2f27e63 2036 __u32 id;
c2c087e6
DW
2037 int rv;
2038 struct stat stb;
2039
88c32bb1
DW
2040 /* if we are on an RAID enabled platform check that the disk is
2041 * attached to the raid controller
2042 */
2043 if (super->hba && !disk_attached_to_hba(fd, super->hba)) {
2044 fprintf(stderr,
2045 Name ": %s is not attached to the raid controller: %s\n",
2046 devname ? : "disk", super->hba);
2047 return 1;
2048 }
2049
f20c3968
DW
2050 if (super->current_vol >= 0)
2051 return add_to_super_imsm_volume(st, dk, fd, devname);
bf5a934a 2052
c2c087e6
DW
2053 fstat(fd, &stb);
2054 dd = malloc(sizeof(*dd));
b9f594fe 2055 if (!dd) {
c2c087e6
DW
2056 fprintf(stderr,
2057 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
f20c3968 2058 return 1;
c2c087e6
DW
2059 }
2060 memset(dd, 0, sizeof(*dd));
2061 dd->major = major(stb.st_rdev);
2062 dd->minor = minor(stb.st_rdev);
b9f594fe 2063 dd->index = -1;
c2c087e6 2064 dd->devname = devname ? strdup(devname) : NULL;
c2c087e6
DW
2065 dd->fd = fd;
2066 rv = imsm_read_serial(fd, devname, dd->serial);
2067 if (rv) {
2068 fprintf(stderr,
0030e8d6 2069 Name ": failed to retrieve scsi serial, aborting\n");
949c47a0 2070 free(dd);
0030e8d6 2071 abort();
c2c087e6
DW
2072 }
2073
c2c087e6
DW
2074 get_dev_size(fd, NULL, &size);
2075 size /= 512;
1f24f035 2076 serialcpy(dd->disk.serial, dd->serial);
b9f594fe 2077 dd->disk.total_blocks = __cpu_to_le32(size);
f2f27e63 2078 dd->disk.status = USABLE_DISK | SPARE_DISK;
c2c087e6 2079 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
b9f594fe 2080 dd->disk.scsi_id = __cpu_to_le32(id);
c2c087e6 2081 else
b9f594fe 2082 dd->disk.scsi_id = __cpu_to_le32(0);
43dad3d6
DW
2083
2084 if (st->update_tail) {
2085 dd->next = super->add;
2086 super->add = dd;
2087 } else {
2088 dd->next = super->disks;
2089 super->disks = dd;
2090 }
f20c3968
DW
2091
2092 return 0;
cdddbdbc
DW
2093}
2094
c2c087e6
DW
2095static int store_imsm_mpb(int fd, struct intel_super *super);
2096
d23fe947
DW
2097/* spare records have their own family number and do not have any defined raid
2098 * devices
2099 */
2100static int write_super_imsm_spares(struct intel_super *super, int doclose)
2101{
2102 struct imsm_super mpb_save;
2103 struct imsm_super *mpb = super->anchor;
2104 __u32 sum;
2105 struct dl *d;
2106
2107 mpb_save = *mpb;
2108 mpb->num_raid_devs = 0;
2109 mpb->num_disks = 1;
2110 mpb->mpb_size = sizeof(struct imsm_super);
2111 mpb->generation_num = __cpu_to_le32(1UL);
2112
2113 for (d = super->disks; d; d = d->next) {
8796fdc4 2114 if (d->index != -1)
d23fe947
DW
2115 continue;
2116
2117 mpb->disk[0] = d->disk;
2118 sum = __gen_imsm_checksum(mpb);
2119 mpb->family_num = __cpu_to_le32(sum);
2120 sum = __gen_imsm_checksum(mpb);
2121 mpb->check_sum = __cpu_to_le32(sum);
2122
2123 if (store_imsm_mpb(d->fd, super)) {
2124 fprintf(stderr, "%s: failed for device %d:%d %s\n",
2125 __func__, d->major, d->minor, strerror(errno));
2126 *mpb = mpb_save;
e74255d9 2127 return 1;
d23fe947
DW
2128 }
2129 if (doclose) {
2130 close(d->fd);
2131 d->fd = -1;
2132 }
2133 }
2134
2135 *mpb = mpb_save;
e74255d9 2136 return 0;
d23fe947
DW
2137}
2138
c2c087e6 2139static int write_super_imsm(struct intel_super *super, int doclose)
cdddbdbc 2140{
949c47a0 2141 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
2142 struct dl *d;
2143 __u32 generation;
2144 __u32 sum;
d23fe947 2145 int spares = 0;
949c47a0 2146 int i;
a48ac0a8 2147 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
cdddbdbc 2148
c2c087e6
DW
2149 /* 'generation' is incremented everytime the metadata is written */
2150 generation = __le32_to_cpu(mpb->generation_num);
2151 generation++;
2152 mpb->generation_num = __cpu_to_le32(generation);
2153
1ee1e9fc 2154 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
d23fe947 2155 for (d = super->disks; d; d = d->next) {
8796fdc4 2156 if (d->index == -1)
d23fe947 2157 spares++;
1ee1e9fc 2158 else
d23fe947 2159 mpb->disk[d->index] = d->disk;
d23fe947 2160 }
47ee5a45
DW
2161 for (d = super->missing; d; d = d->next)
2162 mpb->disk[d->index] = d->disk;
b9f594fe 2163
949c47a0
DW
2164 for (i = 0; i < mpb->num_raid_devs; i++) {
2165 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2166
2167 imsm_copy_dev(dev, super->dev_tbl[i]);
a48ac0a8 2168 mpb_size += sizeof_imsm_dev(dev, 0);
949c47a0 2169 }
a48ac0a8
DW
2170 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
2171 mpb->mpb_size = __cpu_to_le32(mpb_size);
949c47a0 2172
c2c087e6 2173 /* recalculate checksum */
949c47a0 2174 sum = __gen_imsm_checksum(mpb);
c2c087e6
DW
2175 mpb->check_sum = __cpu_to_le32(sum);
2176
d23fe947 2177 /* write the mpb for disks that compose raid devices */
c2c087e6 2178 for (d = super->disks; d ; d = d->next) {
d23fe947
DW
2179 if (d->index < 0)
2180 continue;
8796fdc4 2181 if (store_imsm_mpb(d->fd, super))
c2c087e6
DW
2182 fprintf(stderr, "%s: failed for device %d:%d %s\n",
2183 __func__, d->major, d->minor, strerror(errno));
c2c087e6
DW
2184 if (doclose) {
2185 close(d->fd);
2186 d->fd = -1;
2187 }
2188 }
2189
d23fe947
DW
2190 if (spares)
2191 return write_super_imsm_spares(super, doclose);
2192
e74255d9 2193 return 0;
c2c087e6
DW
2194}
2195
0e600426 2196
43dad3d6
DW
2197static int create_array(struct supertype *st)
2198{
2199 size_t len;
2200 struct imsm_update_create_array *u;
2201 struct intel_super *super = st->sb;
2202 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
54c2c1ea
DW
2203 struct imsm_map *map = get_imsm_map(dev, 0);
2204 struct disk_info *inf;
2205 struct imsm_disk *disk;
2206 int i;
2207 int idx;
43dad3d6 2208
54c2c1ea
DW
2209 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
2210 sizeof(*inf) * map->num_members;
43dad3d6
DW
2211 u = malloc(len);
2212 if (!u) {
2213 fprintf(stderr, "%s: failed to allocate update buffer\n",
2214 __func__);
2215 return 1;
2216 }
2217
2218 u->type = update_create_array;
2219 u->dev_idx = super->current_vol;
2220 imsm_copy_dev(&u->dev, dev);
54c2c1ea
DW
2221 inf = get_disk_info(u);
2222 for (i = 0; i < map->num_members; i++) {
2223 idx = get_imsm_disk_idx(dev, i);
2224 disk = get_imsm_disk(super, idx);
2225 serialcpy(inf[i].serial, disk->serial);
2226 }
43dad3d6
DW
2227 append_metadata_update(st, u, len);
2228
2229 return 0;
2230}
2231
7801ac20 2232static int _add_disk(struct supertype *st)
43dad3d6
DW
2233{
2234 struct intel_super *super = st->sb;
2235 size_t len;
2236 struct imsm_update_add_disk *u;
2237
2238 if (!super->add)
2239 return 0;
2240
2241 len = sizeof(*u);
2242 u = malloc(len);
2243 if (!u) {
2244 fprintf(stderr, "%s: failed to allocate update buffer\n",
2245 __func__);
2246 return 1;
2247 }
2248
2249 u->type = update_add_disk;
2250 append_metadata_update(st, u, len);
2251
2252 return 0;
2253}
2254
c2c087e6
DW
2255static int write_init_super_imsm(struct supertype *st)
2256{
8273f55e 2257 if (st->update_tail) {
43dad3d6
DW
2258 /* queue the recently created array / added disk
2259 * as a metadata update */
8273f55e 2260 struct intel_super *super = st->sb;
8273f55e 2261 struct dl *d;
43dad3d6 2262 int rv;
8273f55e 2263
43dad3d6
DW
2264 /* determine if we are creating a volume or adding a disk */
2265 if (super->current_vol < 0) {
2266 /* in the add disk case we are running in mdmon
2267 * context, so don't close fd's
2268 */
7801ac20 2269 return _add_disk(st);
43dad3d6
DW
2270 } else
2271 rv = create_array(st);
8273f55e
DW
2272
2273 for (d = super->disks; d ; d = d->next) {
2274 close(d->fd);
2275 d->fd = -1;
2276 }
2277
43dad3d6 2278 return rv;
8273f55e
DW
2279 } else
2280 return write_super_imsm(st->sb, 1);
cdddbdbc 2281}
0e600426 2282#endif
cdddbdbc
DW
2283
2284static int store_zero_imsm(struct supertype *st, int fd)
2285{
551c80c1 2286 unsigned long long dsize;
6416d527 2287 void *buf;
551c80c1
DW
2288
2289 get_dev_size(fd, NULL, &dsize);
2290
2291 /* first block is stored on second to last sector of the disk */
2292 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
2293 return 1;
2294
ad97895e
DW
2295 if (posix_memalign(&buf, 512, 512) != 0)
2296 return 1;
2297
eb7ea463
DW
2298 memset(buf, 0, 512);
2299 if (write(fd, buf, 512) != 512)
551c80c1 2300 return 1;
cdddbdbc
DW
2301 return 0;
2302}
2303
0e600426
N
2304static int imsm_bbm_log_size(struct imsm_super *mpb)
2305{
2306 return __le32_to_cpu(mpb->bbm_log_size);
2307}
2308
2309#ifndef MDASSEMBLE
cdddbdbc
DW
2310static int validate_geometry_imsm_container(struct supertype *st, int level,
2311 int layout, int raiddisks, int chunk,
c2c087e6 2312 unsigned long long size, char *dev,
2c514b71
NB
2313 unsigned long long *freesize,
2314 int verbose)
cdddbdbc 2315{
c2c087e6
DW
2316 int fd;
2317 unsigned long long ldsize;
88c32bb1 2318 const struct imsm_orom *orom;
cdddbdbc 2319
c2c087e6
DW
2320 if (level != LEVEL_CONTAINER)
2321 return 0;
2322 if (!dev)
2323 return 1;
2324
88c32bb1
DW
2325 if (check_env("IMSM_NO_PLATFORM"))
2326 orom = NULL;
2327 else
2328 orom = find_imsm_orom();
2329 if (orom && raiddisks > orom->tds) {
2330 if (verbose)
2331 fprintf(stderr, Name ": %d exceeds maximum number of"
2332 " platform supported disks: %d\n",
2333 raiddisks, orom->tds);
2334 return 0;
2335 }
2336
c2c087e6
DW
2337 fd = open(dev, O_RDONLY|O_EXCL, 0);
2338 if (fd < 0) {
2c514b71
NB
2339 if (verbose)
2340 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
2341 dev, strerror(errno));
c2c087e6
DW
2342 return 0;
2343 }
2344 if (!get_dev_size(fd, dev, &ldsize)) {
2345 close(fd);
2346 return 0;
2347 }
2348 close(fd);
2349
2350 *freesize = avail_size_imsm(st, ldsize >> 9);
2351
2352 return 1;
cdddbdbc
DW
2353}
2354
0dcecb2e
DW
2355static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
2356{
2357 const unsigned long long base_start = e[*idx].start;
2358 unsigned long long end = base_start + e[*idx].size;
2359 int i;
2360
2361 if (base_start == end)
2362 return 0;
2363
2364 *idx = *idx + 1;
2365 for (i = *idx; i < num_extents; i++) {
2366 /* extend overlapping extents */
2367 if (e[i].start >= base_start &&
2368 e[i].start <= end) {
2369 if (e[i].size == 0)
2370 return 0;
2371 if (e[i].start + e[i].size > end)
2372 end = e[i].start + e[i].size;
2373 } else if (e[i].start > end) {
2374 *idx = i;
2375 break;
2376 }
2377 }
2378
2379 return end - base_start;
2380}
2381
2382static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
2383{
2384 /* build a composite disk with all known extents and generate a new
2385 * 'maxsize' given the "all disks in an array must share a common start
2386 * offset" constraint
2387 */
2388 struct extent *e = calloc(sum_extents, sizeof(*e));
2389 struct dl *dl;
2390 int i, j;
2391 int start_extent;
2392 unsigned long long pos;
2393 unsigned long long start;
2394 unsigned long long maxsize;
2395 unsigned long reserve;
2396
2397 if (!e)
2398 return ~0ULL; /* error */
2399
2400 /* coalesce and sort all extents. also, check to see if we need to
2401 * reserve space between member arrays
2402 */
2403 j = 0;
2404 for (dl = super->disks; dl; dl = dl->next) {
2405 if (!dl->e)
2406 continue;
2407 for (i = 0; i < dl->extent_cnt; i++)
2408 e[j++] = dl->e[i];
2409 }
2410 qsort(e, sum_extents, sizeof(*e), cmp_extent);
2411
2412 /* merge extents */
2413 i = 0;
2414 j = 0;
2415 while (i < sum_extents) {
2416 e[j].start = e[i].start;
2417 e[j].size = find_size(e, &i, sum_extents);
2418 j++;
2419 if (e[j-1].size == 0)
2420 break;
2421 }
2422
2423 pos = 0;
2424 maxsize = 0;
2425 start_extent = 0;
2426 i = 0;
2427 do {
2428 unsigned long long esize;
2429
2430 esize = e[i].start - pos;
2431 if (esize >= maxsize) {
2432 maxsize = esize;
2433 start = pos;
2434 start_extent = i;
2435 }
2436 pos = e[i].start + e[i].size;
2437 i++;
2438 } while (e[i-1].size);
2439 free(e);
2440
2441 if (start_extent > 0)
2442 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
2443 else
2444 reserve = 0;
2445
2446 if (maxsize < reserve)
2447 return ~0ULL;
2448
2449 super->create_offset = ~((__u32) 0);
2450 if (start + reserve > super->create_offset)
2451 return ~0ULL; /* start overflows create_offset */
2452 super->create_offset = start + reserve;
2453
2454 return maxsize - reserve;
2455}
2456
88c32bb1
DW
2457static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
2458{
2459 if (level < 0 || level == 6 || level == 4)
2460 return 0;
2461
2462 /* if we have an orom prevent invalid raid levels */
2463 if (orom)
2464 switch (level) {
2465 case 0: return imsm_orom_has_raid0(orom);
2466 case 1:
2467 if (raiddisks > 2)
2468 return imsm_orom_has_raid1e(orom);
2469 else
2470 return imsm_orom_has_raid1(orom);
2471 case 10: return imsm_orom_has_raid10(orom);
2472 case 5: return imsm_orom_has_raid5(orom);
2473 }
2474 else
2475 return 1; /* not on an Intel RAID platform so anything goes */
2476
2477 return 0;
2478}
2479
2480#define vprintf(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
c2c087e6
DW
2481/* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
2482 * FIX ME add ahci details
2483 */
8b353278
DW
2484static int validate_geometry_imsm_volume(struct supertype *st, int level,
2485 int layout, int raiddisks, int chunk,
c2c087e6 2486 unsigned long long size, char *dev,
2c514b71
NB
2487 unsigned long long *freesize,
2488 int verbose)
cdddbdbc 2489{
c2c087e6
DW
2490 struct stat stb;
2491 struct intel_super *super = st->sb;
2492 struct dl *dl;
2493 unsigned long long pos = 0;
2494 unsigned long long maxsize;
2495 struct extent *e;
2496 int i;
cdddbdbc 2497
88c32bb1
DW
2498 /* We must have the container info already read in. */
2499 if (!super)
c2c087e6
DW
2500 return 0;
2501
88c32bb1
DW
2502 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
2503 vprintf(": platform does not support raid level: %d\n", level);
c2c087e6
DW
2504 return 0;
2505 }
88c32bb1
DW
2506 if (super->orom && !imsm_orom_has_chunk(super->orom, chunk)) {
2507 vprintf(": platform does not support a chunk size of: %d\n", chunk);
c2c087e6 2508 return 0;
88c32bb1
DW
2509 }
2510 if (layout != imsm_level_to_layout(level)) {
2511 if (level == 5)
2512 vprintf(": imsm raid 5 only supports the left-asymmetric layout\n");
2513 else if (level == 10)
2514 vprintf(": imsm raid 10 only supports the n2 layout\n");
2515 else
2516 vprintf(": imsm unknown layout %#x for this raid level %d\n",
2517 layout, level);
2518 return 0;
2519 }
c2c087e6
DW
2520
2521 if (!dev) {
2522 /* General test: make sure there is space for
2da8544a
DW
2523 * 'raiddisks' device extents of size 'size' at a given
2524 * offset
c2c087e6
DW
2525 */
2526 unsigned long long minsize = size*2 /* convert to blocks */;
2da8544a 2527 unsigned long long start_offset = ~0ULL;
c2c087e6
DW
2528 int dcnt = 0;
2529 if (minsize == 0)
2530 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
2531 for (dl = super->disks; dl ; dl = dl->next) {
2532 int found = 0;
2533
bf5a934a 2534 pos = 0;
c2c087e6
DW
2535 i = 0;
2536 e = get_extents(super, dl);
2537 if (!e) continue;
2538 do {
2539 unsigned long long esize;
2540 esize = e[i].start - pos;
2541 if (esize >= minsize)
2542 found = 1;
2da8544a
DW
2543 if (found && start_offset == ~0ULL) {
2544 start_offset = pos;
2545 break;
2546 } else if (found && pos != start_offset) {
2547 found = 0;
2548 break;
2549 }
c2c087e6
DW
2550 pos = e[i].start + e[i].size;
2551 i++;
2552 } while (e[i-1].size);
2553 if (found)
2554 dcnt++;
2555 free(e);
2556 }
2557 if (dcnt < raiddisks) {
2c514b71
NB
2558 if (verbose)
2559 fprintf(stderr, Name ": imsm: Not enough "
2560 "devices with space for this array "
2561 "(%d < %d)\n",
2562 dcnt, raiddisks);
c2c087e6
DW
2563 return 0;
2564 }
2565 return 1;
2566 }
0dcecb2e 2567
c2c087e6
DW
2568 /* This device must be a member of the set */
2569 if (stat(dev, &stb) < 0)
2570 return 0;
2571 if ((S_IFMT & stb.st_mode) != S_IFBLK)
2572 return 0;
2573 for (dl = super->disks ; dl ; dl = dl->next) {
2574 if (dl->major == major(stb.st_rdev) &&
2575 dl->minor == minor(stb.st_rdev))
2576 break;
2577 }
2578 if (!dl) {
2c514b71
NB
2579 if (verbose)
2580 fprintf(stderr, Name ": %s is not in the "
2581 "same imsm set\n", dev);
c2c087e6
DW
2582 return 0;
2583 }
0dcecb2e
DW
2584
2585 /* retrieve the largest free space block */
c2c087e6
DW
2586 e = get_extents(super, dl);
2587 maxsize = 0;
2588 i = 0;
0dcecb2e
DW
2589 if (e) {
2590 do {
2591 unsigned long long esize;
2592
2593 esize = e[i].start - pos;
2594 if (esize >= maxsize)
2595 maxsize = esize;
2596 pos = e[i].start + e[i].size;
2597 i++;
2598 } while (e[i-1].size);
2599 dl->e = e;
2600 dl->extent_cnt = i;
2601 } else {
2602 if (verbose)
2603 fprintf(stderr, Name ": unable to determine free space for: %s\n",
2604 dev);
2605 return 0;
2606 }
2607 if (maxsize < size) {
2608 if (verbose)
2609 fprintf(stderr, Name ": %s not enough space (%llu < %llu)\n",
2610 dev, maxsize, size);
2611 return 0;
2612 }
2613
2614 /* count total number of extents for merge */
2615 i = 0;
2616 for (dl = super->disks; dl; dl = dl->next)
2617 if (dl->e)
2618 i += dl->extent_cnt;
2619
2620 maxsize = merge_extents(super, i);
2621 if (maxsize < size) {
2622 if (verbose)
2623 fprintf(stderr, Name ": not enough space after merge (%llu < %llu)\n",
2624 maxsize, size);
2625 return 0;
2626 } else if (maxsize == ~0ULL) {
2627 if (verbose)
2628 fprintf(stderr, Name ": failed to merge %d extents\n", i);
2629 return 0;
2630 }
2631
c2c087e6
DW
2632 *freesize = maxsize;
2633
2634 return 1;
cdddbdbc
DW
2635}
2636
bf5a934a
DW
2637static int validate_geometry_imsm(struct supertype *st, int level, int layout,
2638 int raiddisks, int chunk, unsigned long long size,
2639 char *dev, unsigned long long *freesize,
2640 int verbose)
2641{
2642 int fd, cfd;
2643 struct mdinfo *sra;
2644
2645 /* if given unused devices create a container
2646 * if given given devices in a container create a member volume
2647 */
2648 if (level == LEVEL_CONTAINER) {
2649 /* Must be a fresh device to add to a container */
2650 return validate_geometry_imsm_container(st, level, layout,
2651 raiddisks, chunk, size,
2652 dev, freesize,
2653 verbose);
2654 }
2655
2656 if (st->sb) {
2657 /* creating in a given container */
2658 return validate_geometry_imsm_volume(st, level, layout,
2659 raiddisks, chunk, size,
2660 dev, freesize, verbose);
2661 }
2662
2663 /* limit creation to the following levels */
2664 if (!dev)
2665 switch (level) {
2666 case 0:
2667 case 1:
2668 case 10:
2669 case 5:
2670 break;
2671 default:
2672 return 1;
2673 }
2674
2675 /* This device needs to be a device in an 'imsm' container */
2676 fd = open(dev, O_RDONLY|O_EXCL, 0);
2677 if (fd >= 0) {
2678 if (verbose)
2679 fprintf(stderr,
2680 Name ": Cannot create this array on device %s\n",
2681 dev);
2682 close(fd);
2683 return 0;
2684 }
2685 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
2686 if (verbose)
2687 fprintf(stderr, Name ": Cannot open %s: %s\n",
2688 dev, strerror(errno));
2689 return 0;
2690 }
2691 /* Well, it is in use by someone, maybe an 'imsm' container. */
2692 cfd = open_container(fd);
2693 if (cfd < 0) {
2694 close(fd);
2695 if (verbose)
2696 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
2697 dev);
2698 return 0;
2699 }
2700 sra = sysfs_read(cfd, 0, GET_VERSION);
2701 close(fd);
2702 if (sra && sra->array.major_version == -1 &&
2703 strcmp(sra->text_version, "imsm") == 0) {
2704 /* This is a member of a imsm container. Load the container
2705 * and try to create a volume
2706 */
2707 struct intel_super *super;
2708
2709 if (load_super_imsm_all(st, cfd, (void **) &super, NULL, 1) == 0) {
2710 st->sb = super;
2711 st->container_dev = fd2devnum(cfd);
2712 close(cfd);
2713 return validate_geometry_imsm_volume(st, level, layout,
2714 raiddisks, chunk,
2715 size, dev,
2716 freesize, verbose);
2717 }
2718 close(cfd);
2719 } else /* may belong to another container */
2720 return 0;
2721
2722 return 1;
2723}
0e600426 2724#endif /* MDASSEMBLE */
bf5a934a 2725
cdddbdbc
DW
2726static struct mdinfo *container_content_imsm(struct supertype *st)
2727{
4f5bc454
DW
2728 /* Given a container loaded by load_super_imsm_all,
2729 * extract information about all the arrays into
2730 * an mdinfo tree.
2731 *
2732 * For each imsm_dev create an mdinfo, fill it in,
2733 * then look for matching devices in super->disks
2734 * and create appropriate device mdinfo.
2735 */
2736 struct intel_super *super = st->sb;
949c47a0 2737 struct imsm_super *mpb = super->anchor;
4f5bc454
DW
2738 struct mdinfo *rest = NULL;
2739 int i;
cdddbdbc 2740
604b746f
JD
2741 /* do not assemble arrays that might have bad blocks */
2742 if (imsm_bbm_log_size(super->anchor)) {
2743 fprintf(stderr, Name ": BBM log found in metadata. "
2744 "Cannot activate array(s).\n");
2745 return NULL;
2746 }
2747
4f5bc454 2748 for (i = 0; i < mpb->num_raid_devs; i++) {
949c47a0 2749 struct imsm_dev *dev = get_imsm_dev(super, i);
a965f303 2750 struct imsm_map *map = get_imsm_map(dev, 0);
4f5bc454 2751 struct mdinfo *this;
4f5bc454
DW
2752 int slot;
2753
2754 this = malloc(sizeof(*this));
2755 memset(this, 0, sizeof(*this));
2756 this->next = rest;
4f5bc454 2757
301406c9
DW
2758 super->current_vol = i;
2759 getinfo_super_imsm_volume(st, this);
4f5bc454 2760 for (slot = 0 ; slot < map->num_members; slot++) {
4f5bc454
DW
2761 struct mdinfo *info_d;
2762 struct dl *d;
2763 int idx;
9a1608e5 2764 int skip;
4f5bc454 2765 __u32 s;
7eef0453 2766 __u32 ord;
4f5bc454 2767
9a1608e5 2768 skip = 0;
ff077194 2769 idx = get_imsm_disk_idx(dev, slot);
7eef0453 2770 ord = get_imsm_ord_tbl_ent(dev, slot);
4f5bc454
DW
2771 for (d = super->disks; d ; d = d->next)
2772 if (d->index == idx)
2773 break;
2774
2775 if (d == NULL)
9a1608e5
DW
2776 skip = 1;
2777
f2f27e63 2778 s = d ? d->disk.status : 0;
9a1608e5
DW
2779 if (s & FAILED_DISK)
2780 skip = 1;
2781 if (!(s & USABLE_DISK))
2782 skip = 1;
7eef0453
DW
2783 if (ord & IMSM_ORD_REBUILD)
2784 skip = 1;
9a1608e5
DW
2785
2786 /*
2787 * if we skip some disks the array will be assmebled degraded;
2788 * reset resync start to avoid a dirty-degraded situation
2789 *
2790 * FIXME handle dirty degraded
2791 */
2792 if (skip && !dev->vol.dirty)
2793 this->resync_start = ~0ULL;
2794 if (skip)
2795 continue;
4f5bc454
DW
2796
2797 info_d = malloc(sizeof(*info_d));
9a1608e5
DW
2798 if (!info_d) {
2799 fprintf(stderr, Name ": failed to allocate disk"
2800 " for volume %s\n", (char *) dev->volume);
2801 free(this);
2802 this = rest;
2803 break;
2804 }
4f5bc454
DW
2805 memset(info_d, 0, sizeof(*info_d));
2806 info_d->next = this->devs;
2807 this->devs = info_d;
2808
4f5bc454
DW
2809 info_d->disk.number = d->index;
2810 info_d->disk.major = d->major;
2811 info_d->disk.minor = d->minor;
2812 info_d->disk.raid_disk = slot;
4f5bc454
DW
2813
2814 this->array.working_disks++;
2815
2816 info_d->events = __le32_to_cpu(mpb->generation_num);
2817 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
2818 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
2819 if (d->devname)
2820 strcpy(info_d->name, d->devname);
2821 }
9a1608e5 2822 rest = this;
4f5bc454
DW
2823 }
2824
2825 return rest;
cdddbdbc
DW
2826}
2827
845dea95 2828
0e600426 2829#ifndef MDASSEMBLE
cba0191b
NB
2830static int imsm_open_new(struct supertype *c, struct active_array *a,
2831 char *inst)
845dea95 2832{
0372d5a2 2833 struct intel_super *super = c->sb;
949c47a0 2834 struct imsm_super *mpb = super->anchor;
0372d5a2 2835
949c47a0 2836 if (atoi(inst) >= mpb->num_raid_devs) {
0372d5a2
DW
2837 fprintf(stderr, "%s: subarry index %d, out of range\n",
2838 __func__, atoi(inst));
2839 return -ENODEV;
2840 }
2841
4e6e574a 2842 dprintf("imsm: open_new %s\n", inst);
cba0191b 2843 a->info.container_member = atoi(inst);
845dea95
NB
2844 return 0;
2845}
2846
fb49eef2 2847static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed)
c2a1e7da 2848{
a965f303 2849 struct imsm_map *map = get_imsm_map(dev, 0);
c2a1e7da
DW
2850
2851 if (!failed)
3393c6af
DW
2852 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
2853 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
c2a1e7da
DW
2854
2855 switch (get_imsm_raid_level(map)) {
2856 case 0:
2857 return IMSM_T_STATE_FAILED;
2858 break;
2859 case 1:
2860 if (failed < map->num_members)
2861 return IMSM_T_STATE_DEGRADED;
2862 else
2863 return IMSM_T_STATE_FAILED;
2864 break;
2865 case 10:
2866 {
2867 /**
c92a2527
DW
2868 * check to see if any mirrors have failed, otherwise we
2869 * are degraded. Even numbered slots are mirrored on
2870 * slot+1
c2a1e7da 2871 */
c2a1e7da 2872 int i;
d9b420a5
N
2873 /* gcc -Os complains that this is unused */
2874 int insync = insync;
c2a1e7da
DW
2875
2876 for (i = 0; i < map->num_members; i++) {
c92a2527
DW
2877 __u32 ord = get_imsm_ord_tbl_ent(dev, i);
2878 int idx = ord_to_idx(ord);
2879 struct imsm_disk *disk;
c2a1e7da 2880
c92a2527
DW
2881 /* reset the potential in-sync count on even-numbered
2882 * slots. num_copies is always 2 for imsm raid10
2883 */
2884 if ((i & 1) == 0)
2885 insync = 2;
c2a1e7da 2886
c92a2527 2887 disk = get_imsm_disk(super, idx);
f2f27e63 2888 if (!disk || disk->status & FAILED_DISK ||
c92a2527
DW
2889 ord & IMSM_ORD_REBUILD)
2890 insync--;
c2a1e7da 2891
c92a2527
DW
2892 /* no in-sync disks left in this mirror the
2893 * array has failed
2894 */
2895 if (insync == 0)
2896 return IMSM_T_STATE_FAILED;
c2a1e7da
DW
2897 }
2898
2899 return IMSM_T_STATE_DEGRADED;
2900 }
2901 case 5:
2902 if (failed < 2)
2903 return IMSM_T_STATE_DEGRADED;
2904 else
2905 return IMSM_T_STATE_FAILED;
2906 break;
2907 default:
2908 break;
2909 }
2910
2911 return map->map_state;
2912}
2913
ff077194 2914static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev)
c2a1e7da
DW
2915{
2916 int i;
2917 int failed = 0;
2918 struct imsm_disk *disk;
ff077194 2919 struct imsm_map *map = get_imsm_map(dev, 0);
c2a1e7da
DW
2920
2921 for (i = 0; i < map->num_members; i++) {
b10b37b8
DW
2922 __u32 ord = get_imsm_ord_tbl_ent(dev, i);
2923 int idx = ord_to_idx(ord);
c2a1e7da 2924
949c47a0 2925 disk = get_imsm_disk(super, idx);
f2f27e63 2926 if (!disk || disk->status & FAILED_DISK ||
b10b37b8 2927 ord & IMSM_ORD_REBUILD)
fcb84475 2928 failed++;
c2a1e7da
DW
2929 }
2930
2931 return failed;
845dea95
NB
2932}
2933
0c046afd
DW
2934static int is_resyncing(struct imsm_dev *dev)
2935{
2936 struct imsm_map *migr_map;
2937
2938 if (!dev->vol.migr_state)
2939 return 0;
2940
e3bba0e0 2941 if (dev->vol.migr_type == MIGR_INIT)
0c046afd
DW
2942 return 1;
2943
2944 migr_map = get_imsm_map(dev, 1);
2945
2946 if (migr_map->map_state == IMSM_T_STATE_NORMAL)
2947 return 1;
2948 else
2949 return 0;
2950}
2951
2952static int is_rebuilding(struct imsm_dev *dev)
2953{
2954 struct imsm_map *migr_map;
2955
2956 if (!dev->vol.migr_state)
2957 return 0;
2958
e3bba0e0 2959 if (dev->vol.migr_type != MIGR_REBUILD)
0c046afd
DW
2960 return 0;
2961
2962 migr_map = get_imsm_map(dev, 1);
2963
2964 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
2965 return 1;
2966 else
2967 return 0;
2968}
2969
47ee5a45
DW
2970static void mark_failure(struct imsm_disk *disk)
2971{
f2f27e63 2972 if (disk->status & FAILED_DISK)
47ee5a45 2973 return;
f2f27e63 2974 disk->status |= FAILED_DISK;
47ee5a45
DW
2975 disk->scsi_id = __cpu_to_le32(~(__u32)0);
2976 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
2977}
2978
0c046afd
DW
2979/* Handle dirty -> clean transititions and resync. Degraded and rebuild
2980 * states are handled in imsm_set_disk() with one exception, when a
2981 * resync is stopped due to a new failure this routine will set the
2982 * 'degraded' state for the array.
2983 */
01f157d7 2984static int imsm_set_array_state(struct active_array *a, int consistent)
a862209d
DW
2985{
2986 int inst = a->info.container_member;
2987 struct intel_super *super = a->container->sb;
949c47a0 2988 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 2989 struct imsm_map *map = get_imsm_map(dev, 0);
0c046afd
DW
2990 int failed = imsm_count_failed(super, dev);
2991 __u8 map_state = imsm_check_degraded(super, dev, failed);
a862209d 2992
47ee5a45
DW
2993 /* before we activate this array handle any missing disks */
2994 if (consistent == 2 && super->missing) {
2995 struct dl *dl;
2996
2997 dprintf("imsm: mark missing\n");
2998 end_migration(dev, map_state);
2999 for (dl = super->missing; dl; dl = dl->next)
3000 mark_failure(&dl->disk);
3001 super->updates_pending++;
3002 }
3003
0c046afd 3004 if (consistent == 2 &&
593add1b 3005 (!is_resync_complete(a) ||
0c046afd
DW
3006 map_state != IMSM_T_STATE_NORMAL ||
3007 dev->vol.migr_state))
01f157d7 3008 consistent = 0;
272906ef 3009
593add1b 3010 if (is_resync_complete(a)) {
0c046afd
DW
3011 /* complete intialization / resync,
3012 * recovery is completed in ->set_disk
3013 */
3014 if (is_resyncing(dev)) {
3015 dprintf("imsm: mark resync done\n");
f8f603f1 3016 end_migration(dev, map_state);
115c3803 3017 super->updates_pending++;
115c3803 3018 }
0c046afd
DW
3019 } else if (!is_resyncing(dev) && !failed) {
3020 /* mark the start of the init process if nothing is failed */
3021 dprintf("imsm: mark resync start (%llu)\n", a->resync_start);
e3bba0e0
DW
3022 if (map->map_state == IMSM_T_STATE_NORMAL)
3023 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_REBUILD);
3024 else
3025 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_INIT);
3393c6af 3026 super->updates_pending++;
115c3803 3027 }
a862209d 3028
f8f603f1
DW
3029 /* check if we can update the migration checkpoint */
3030 if (dev->vol.migr_state &&
3031 __le32_to_cpu(dev->vol.curr_migr_unit) != a->resync_start) {
3032 dprintf("imsm: checkpoint migration (%llu)\n", a->resync_start);
3033 dev->vol.curr_migr_unit = __cpu_to_le32(a->resync_start);
3034 super->updates_pending++;
3035 }
3036
3393c6af 3037 /* mark dirty / clean */
0c046afd 3038 if (dev->vol.dirty != !consistent) {
3393c6af 3039 dprintf("imsm: mark '%s' (%llu)\n",
0c046afd
DW
3040 consistent ? "clean" : "dirty", a->resync_start);
3041 if (consistent)
3042 dev->vol.dirty = 0;
3043 else
3044 dev->vol.dirty = 1;
a862209d
DW
3045 super->updates_pending++;
3046 }
01f157d7 3047 return consistent;
a862209d
DW
3048}
3049
8d45d196 3050static void imsm_set_disk(struct active_array *a, int n, int state)
845dea95 3051{
8d45d196
DW
3052 int inst = a->info.container_member;
3053 struct intel_super *super = a->container->sb;
949c47a0 3054 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 3055 struct imsm_map *map = get_imsm_map(dev, 0);
8d45d196 3056 struct imsm_disk *disk;
0c046afd 3057 int failed;
b10b37b8 3058 __u32 ord;
0c046afd 3059 __u8 map_state;
8d45d196
DW
3060
3061 if (n > map->num_members)
3062 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
3063 n, map->num_members - 1);
3064
3065 if (n < 0)
3066 return;
3067
4e6e574a 3068 dprintf("imsm: set_disk %d:%x\n", n, state);
8d45d196 3069
b10b37b8
DW
3070 ord = get_imsm_ord_tbl_ent(dev, n);
3071 disk = get_imsm_disk(super, ord_to_idx(ord));
8d45d196 3072
5802a811 3073 /* check for new failures */
f2f27e63 3074 if ((state & DS_FAULTY) && !(disk->status & FAILED_DISK)) {
47ee5a45 3075 mark_failure(disk);
5802a811 3076 super->updates_pending++;
8d45d196 3077 }
47ee5a45 3078
19859edc 3079 /* check if in_sync */
b10b37b8
DW
3080 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD) {
3081 struct imsm_map *migr_map = get_imsm_map(dev, 1);
3082
3083 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
19859edc
DW
3084 super->updates_pending++;
3085 }
8d45d196 3086
0c046afd
DW
3087 failed = imsm_count_failed(super, dev);
3088 map_state = imsm_check_degraded(super, dev, failed);
5802a811 3089
0c046afd
DW
3090 /* check if recovery complete, newly degraded, or failed */
3091 if (map_state == IMSM_T_STATE_NORMAL && is_rebuilding(dev)) {
f8f603f1 3092 end_migration(dev, map_state);
0c046afd
DW
3093 super->updates_pending++;
3094 } else if (map_state == IMSM_T_STATE_DEGRADED &&
3095 map->map_state != map_state &&
3096 !dev->vol.migr_state) {
3097 dprintf("imsm: mark degraded\n");
3098 map->map_state = map_state;
3099 super->updates_pending++;
3100 } else if (map_state == IMSM_T_STATE_FAILED &&
3101 map->map_state != map_state) {
3102 dprintf("imsm: mark failed\n");
f8f603f1 3103 end_migration(dev, map_state);
0c046afd 3104 super->updates_pending++;
5802a811 3105 }
845dea95
NB
3106}
3107
c2a1e7da
DW
3108static int store_imsm_mpb(int fd, struct intel_super *super)
3109{
949c47a0 3110 struct imsm_super *mpb = super->anchor;
c2a1e7da
DW
3111 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
3112 unsigned long long dsize;
3113 unsigned long long sectors;
3114
3115 get_dev_size(fd, NULL, &dsize);
3116
272f648f
DW
3117 if (mpb_size > 512) {
3118 /* -1 to account for anchor */
3119 sectors = mpb_sectors(mpb) - 1;
c2a1e7da 3120
272f648f
DW
3121 /* write the extended mpb to the sectors preceeding the anchor */
3122 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
3123 return 1;
c2a1e7da 3124
99e29264 3125 if (write(fd, super->buf + 512, 512 * sectors) != 512 * sectors)
272f648f
DW
3126 return 1;
3127 }
c2a1e7da 3128
272f648f
DW
3129 /* first block is stored on second to last sector of the disk */
3130 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
c2a1e7da
DW
3131 return 1;
3132
272f648f 3133 if (write(fd, super->buf, 512) != 512)
c2a1e7da
DW
3134 return 1;
3135
c2a1e7da
DW
3136 return 0;
3137}
3138
2e735d19 3139static void imsm_sync_metadata(struct supertype *container)
845dea95 3140{
2e735d19 3141 struct intel_super *super = container->sb;
c2a1e7da
DW
3142
3143 if (!super->updates_pending)
3144 return;
3145
c2c087e6 3146 write_super_imsm(super, 0);
c2a1e7da
DW
3147
3148 super->updates_pending = 0;
845dea95
NB
3149}
3150
272906ef
DW
3151static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
3152{
3153 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
ff077194 3154 int i = get_imsm_disk_idx(dev, idx);
272906ef
DW
3155 struct dl *dl;
3156
3157 for (dl = super->disks; dl; dl = dl->next)
3158 if (dl->index == i)
3159 break;
3160
f2f27e63 3161 if (dl && dl->disk.status & FAILED_DISK)
272906ef
DW
3162 dl = NULL;
3163
3164 if (dl)
3165 dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor);
3166
3167 return dl;
3168}
3169
e553d2a4 3170static struct dl *imsm_add_spare(struct intel_super *super, int slot, struct active_array *a)
272906ef
DW
3171{
3172 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
e553d2a4 3173 int idx = get_imsm_disk_idx(dev, slot);
272906ef
DW
3174 struct imsm_map *map = get_imsm_map(dev, 0);
3175 unsigned long long esize;
3176 unsigned long long pos;
3177 struct mdinfo *d;
3178 struct extent *ex;
3179 int j;
3180 int found;
3181 __u32 array_start;
3182 struct dl *dl;
3183
3184 for (dl = super->disks; dl; dl = dl->next) {
3185 /* If in this array, skip */
3186 for (d = a->info.devs ; d ; d = d->next)
e553d2a4
DW
3187 if (d->state_fd >= 0 &&
3188 d->disk.major == dl->major &&
272906ef
DW
3189 d->disk.minor == dl->minor) {
3190 dprintf("%x:%x already in array\n", dl->major, dl->minor);
3191 break;
3192 }
3193 if (d)
3194 continue;
3195
e553d2a4 3196 /* skip in use or failed drives */
f2f27e63 3197 if (dl->disk.status & FAILED_DISK || idx == dl->index) {
9a1608e5
DW
3198 dprintf("%x:%x status ( %s%s)\n",
3199 dl->major, dl->minor,
f2f27e63 3200 dl->disk.status & FAILED_DISK ? "failed " : "",
e553d2a4 3201 idx == dl->index ? "in use " : "");
9a1608e5
DW
3202 continue;
3203 }
3204
272906ef
DW
3205 /* Does this unused device have the requisite free space?
3206 * We need a->info.component_size sectors
3207 */
3208 ex = get_extents(super, dl);
3209 if (!ex) {
3210 dprintf("cannot get extents\n");
3211 continue;
3212 }
3213 found = 0;
3214 j = 0;
3215 pos = 0;
3216 array_start = __le32_to_cpu(map->pba_of_lba0);
3217
3218 do {
3219 /* check that we can start at pba_of_lba0 with
3220 * a->info.component_size of space
3221 */
3222 esize = ex[j].start - pos;
3223 if (array_start >= pos &&
3224 array_start + a->info.component_size < ex[j].start) {
3225 found = 1;
3226 break;
3227 }
3228 pos = ex[j].start + ex[j].size;
3229 j++;
3230
3231 } while (ex[j-1].size);
3232
3233 free(ex);
3234 if (!found) {
3235 dprintf("%x:%x does not have %llu at %d\n",
3236 dl->major, dl->minor,
3237 a->info.component_size,
3238 __le32_to_cpu(map->pba_of_lba0));
3239 /* No room */
3240 continue;
3241 } else
3242 break;
3243 }
3244
3245 return dl;
3246}
3247
88758e9d
DW
3248static struct mdinfo *imsm_activate_spare(struct active_array *a,
3249 struct metadata_update **updates)
3250{
3251 /**
d23fe947
DW
3252 * Find a device with unused free space and use it to replace a
3253 * failed/vacant region in an array. We replace failed regions one a
3254 * array at a time. The result is that a new spare disk will be added
3255 * to the first failed array and after the monitor has finished
3256 * propagating failures the remainder will be consumed.
88758e9d 3257 *
d23fe947
DW
3258 * FIXME add a capability for mdmon to request spares from another
3259 * container.
88758e9d
DW
3260 */
3261
3262 struct intel_super *super = a->container->sb;
88758e9d 3263 int inst = a->info.container_member;
949c47a0 3264 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 3265 struct imsm_map *map = get_imsm_map(dev, 0);
88758e9d
DW
3266 int failed = a->info.array.raid_disks;
3267 struct mdinfo *rv = NULL;
3268 struct mdinfo *d;
3269 struct mdinfo *di;
3270 struct metadata_update *mu;
3271 struct dl *dl;
3272 struct imsm_update_activate_spare *u;
3273 int num_spares = 0;
3274 int i;
3275
3276 for (d = a->info.devs ; d ; d = d->next) {
3277 if ((d->curr_state & DS_FAULTY) &&
3278 d->state_fd >= 0)
3279 /* wait for Removal to happen */
3280 return NULL;
3281 if (d->state_fd >= 0)
3282 failed--;
3283 }
3284
3285 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
3286 inst, failed, a->info.array.raid_disks, a->info.array.level);
fb49eef2 3287 if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED)
88758e9d
DW
3288 return NULL;
3289
3290 /* For each slot, if it is not working, find a spare */
88758e9d
DW
3291 for (i = 0; i < a->info.array.raid_disks; i++) {
3292 for (d = a->info.devs ; d ; d = d->next)
3293 if (d->disk.raid_disk == i)
3294 break;
3295 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
3296 if (d && (d->state_fd >= 0))
3297 continue;
3298
272906ef
DW
3299 /*
3300 * OK, this device needs recovery. Try to re-add the previous
3301 * occupant of this slot, if this fails add a new spare
3302 */
3303 dl = imsm_readd(super, i, a);
3304 if (!dl)
3305 dl = imsm_add_spare(super, i, a);
3306 if (!dl)
3307 continue;
3308
3309 /* found a usable disk with enough space */
3310 di = malloc(sizeof(*di));
79244939
DW
3311 if (!di)
3312 continue;
272906ef
DW
3313 memset(di, 0, sizeof(*di));
3314
3315 /* dl->index will be -1 in the case we are activating a
3316 * pristine spare. imsm_process_update() will create a
3317 * new index in this case. Once a disk is found to be
3318 * failed in all member arrays it is kicked from the
3319 * metadata
3320 */
3321 di->disk.number = dl->index;
d23fe947 3322
272906ef
DW
3323 /* (ab)use di->devs to store a pointer to the device
3324 * we chose
3325 */
3326 di->devs = (struct mdinfo *) dl;
3327
3328 di->disk.raid_disk = i;
3329 di->disk.major = dl->major;
3330 di->disk.minor = dl->minor;
3331 di->disk.state = 0;
3332 di->data_offset = __le32_to_cpu(map->pba_of_lba0);
3333 di->component_size = a->info.component_size;
3334 di->container_member = inst;
3335 di->next = rv;
3336 rv = di;
3337 num_spares++;
3338 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
3339 i, di->data_offset);
88758e9d 3340
272906ef 3341 break;
88758e9d
DW
3342 }
3343
3344 if (!rv)
3345 /* No spares found */
3346 return rv;
3347 /* Now 'rv' has a list of devices to return.
3348 * Create a metadata_update record to update the
3349 * disk_ord_tbl for the array
3350 */
3351 mu = malloc(sizeof(*mu));
79244939
DW
3352 if (mu) {
3353 mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
3354 if (mu->buf == NULL) {
3355 free(mu);
3356 mu = NULL;
3357 }
3358 }
3359 if (!mu) {
3360 while (rv) {
3361 struct mdinfo *n = rv->next;
3362
3363 free(rv);
3364 rv = n;
3365 }
3366 return NULL;
3367 }
3368
88758e9d
DW
3369 mu->space = NULL;
3370 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
3371 mu->next = *updates;
3372 u = (struct imsm_update_activate_spare *) mu->buf;
3373
3374 for (di = rv ; di ; di = di->next) {
3375 u->type = update_activate_spare;
d23fe947
DW
3376 u->dl = (struct dl *) di->devs;
3377 di->devs = NULL;
88758e9d
DW
3378 u->slot = di->disk.raid_disk;
3379 u->array = inst;
3380 u->next = u + 1;
3381 u++;
3382 }
3383 (u-1)->next = NULL;
3384 *updates = mu;
3385
3386 return rv;
3387}
3388
54c2c1ea 3389static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
8273f55e 3390{
54c2c1ea
DW
3391 struct imsm_dev *dev = get_imsm_dev(super, idx);
3392 struct imsm_map *map = get_imsm_map(dev, 0);
3393 struct imsm_map *new_map = get_imsm_map(&u->dev, 0);
3394 struct disk_info *inf = get_disk_info(u);
3395 struct imsm_disk *disk;
8273f55e
DW
3396 int i;
3397 int j;
8273f55e 3398
54c2c1ea
DW
3399 for (i = 0; i < map->num_members; i++) {
3400 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
3401 for (j = 0; j < new_map->num_members; j++)
3402 if (serialcmp(disk->serial, inf[j].serial) == 0)
8273f55e
DW
3403 return 1;
3404 }
3405
3406 return 0;
3407}
3408
24565c9a 3409static void imsm_delete(struct intel_super *super, struct dl **dlp, int index);
ae6aad82 3410
e8319a19
DW
3411static void imsm_process_update(struct supertype *st,
3412 struct metadata_update *update)
3413{
3414 /**
3415 * crack open the metadata_update envelope to find the update record
3416 * update can be one of:
3417 * update_activate_spare - a spare device has replaced a failed
3418 * device in an array, update the disk_ord_tbl. If this disk is
3419 * present in all member arrays then also clear the SPARE_DISK
3420 * flag
3421 */
3422 struct intel_super *super = st->sb;
4d7b1503 3423 struct imsm_super *mpb;
e8319a19
DW
3424 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
3425
4d7b1503
DW
3426 /* update requires a larger buf but the allocation failed */
3427 if (super->next_len && !super->next_buf) {
3428 super->next_len = 0;
3429 return;
3430 }
3431
3432 if (super->next_buf) {
3433 memcpy(super->next_buf, super->buf, super->len);
3434 free(super->buf);
3435 super->len = super->next_len;
3436 super->buf = super->next_buf;
3437
3438 super->next_len = 0;
3439 super->next_buf = NULL;
3440 }
3441
3442 mpb = super->anchor;
3443
e8319a19
DW
3444 switch (type) {
3445 case update_activate_spare: {
3446 struct imsm_update_activate_spare *u = (void *) update->buf;
949c47a0 3447 struct imsm_dev *dev = get_imsm_dev(super, u->array);
a965f303 3448 struct imsm_map *map = get_imsm_map(dev, 0);
0c046afd 3449 struct imsm_map *migr_map;
e8319a19
DW
3450 struct active_array *a;
3451 struct imsm_disk *disk;
0c046afd 3452 __u8 to_state;
e8319a19 3453 struct dl *dl;
e8319a19 3454 unsigned int found;
0c046afd
DW
3455 int failed;
3456 int victim = get_imsm_disk_idx(dev, u->slot);
e8319a19
DW
3457 int i;
3458
3459 for (dl = super->disks; dl; dl = dl->next)
d23fe947 3460 if (dl == u->dl)
e8319a19
DW
3461 break;
3462
3463 if (!dl) {
3464 fprintf(stderr, "error: imsm_activate_spare passed "
1f24f035
DW
3465 "an unknown disk (index: %d)\n",
3466 u->dl->index);
e8319a19
DW
3467 return;
3468 }
3469
3470 super->updates_pending++;
3471
0c046afd
DW
3472 /* count failures (excluding rebuilds and the victim)
3473 * to determine map[0] state
3474 */
3475 failed = 0;
3476 for (i = 0; i < map->num_members; i++) {
3477 if (i == u->slot)
3478 continue;
3479 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
f2f27e63 3480 if (!disk || disk->status & FAILED_DISK)
0c046afd
DW
3481 failed++;
3482 }
3483
d23fe947
DW
3484 /* adding a pristine spare, assign a new index */
3485 if (dl->index < 0) {
3486 dl->index = super->anchor->num_disks;
3487 super->anchor->num_disks++;
3488 }
d23fe947 3489 disk = &dl->disk;
f2f27e63
DW
3490 disk->status |= CONFIGURED_DISK;
3491 disk->status &= ~SPARE_DISK;
e8319a19 3492
0c046afd
DW
3493 /* mark rebuild */
3494 to_state = imsm_check_degraded(super, dev, failed);
3495 map->map_state = IMSM_T_STATE_DEGRADED;
e3bba0e0 3496 migrate(dev, to_state, MIGR_REBUILD);
0c046afd
DW
3497 migr_map = get_imsm_map(dev, 1);
3498 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
3499 set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD);
3500
e8319a19
DW
3501 /* count arrays using the victim in the metadata */
3502 found = 0;
3503 for (a = st->arrays; a ; a = a->next) {
949c47a0 3504 dev = get_imsm_dev(super, a->info.container_member);
e8319a19 3505 for (i = 0; i < map->num_members; i++)
ff077194 3506 if (victim == get_imsm_disk_idx(dev, i))
e8319a19
DW
3507 found++;
3508 }