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