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