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