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