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