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