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