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