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