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