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