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