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