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