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