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