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