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